/cp
[official-gcc.git] / gcc / fold-const.c
blob014c7d4aab6731352de5859706f1b754294d0d29
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
113 static bool negate_expr_p (tree);
114 static tree negate_expr (tree);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int twoval_comparison_p (tree, tree *, tree *, int *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree optimize_bit_field_compare (location_t, enum tree_code,
121 tree, tree, tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
132 static tree fold_binary_op_with_conditional_arg (location_t,
133 enum tree_code, tree,
134 tree, tree,
135 tree, tree, int);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static tree fold_negate_expr (location_t, tree);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
147 static location_t
148 expr_location_or (tree t, location_t loc)
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc == UNKNOWN_LOCATION ? loc : tloc;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
166 x = copy_node (x);
167 SET_EXPR_LOCATION (x, loc);
169 return x;
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
174 NULL_TREE. */
176 tree
177 div_if_zero_remainder (const_tree arg1, const_tree arg2)
179 widest_int quo;
181 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
182 SIGNED, &quo))
183 return wide_int_to_tree (TREE_TYPE (arg1), quo);
185 return NULL_TREE;
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
195 used. */
197 static int fold_deferring_overflow_warnings;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
214 void
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
227 deferred code. */
229 void
230 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
232 const char *warnmsg;
233 location_t locus;
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
239 if (fold_deferred_overflow_warning != NULL
240 && code != 0
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243 return;
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
249 if (!issue || warnmsg == NULL)
250 return;
252 if (gimple_no_warning_p (stmt))
253 return;
255 /* Use the smallest code level when deciding to issue the
256 warning. */
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
260 if (!issue_strict_overflow_warning (code))
261 return;
263 if (stmt == NULL)
264 locus = input_location;
265 else
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
270 /* Stop deferring overflow warnings, ignoring any deferred
271 warnings. */
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL, 0);
279 /* Whether we are deferring overflow warnings. */
281 bool
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings > 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
290 void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 if (fold_deferring_overflow_warnings > 0)
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
309 bool
310 negate_mathfn_p (combined_fn fn)
312 switch (fn)
314 CASE_CFN_ASIN:
315 CASE_CFN_ASINH:
316 CASE_CFN_ATAN:
317 CASE_CFN_ATANH:
318 CASE_CFN_CASIN:
319 CASE_CFN_CASINH:
320 CASE_CFN_CATAN:
321 CASE_CFN_CATANH:
322 CASE_CFN_CBRT:
323 CASE_CFN_CPROJ:
324 CASE_CFN_CSIN:
325 CASE_CFN_CSINH:
326 CASE_CFN_CTAN:
327 CASE_CFN_CTANH:
328 CASE_CFN_ERF:
329 CASE_CFN_LLROUND:
330 CASE_CFN_LROUND:
331 CASE_CFN_ROUND:
332 CASE_CFN_SIN:
333 CASE_CFN_SINH:
334 CASE_CFN_TAN:
335 CASE_CFN_TANH:
336 CASE_CFN_TRUNC:
337 return true;
339 CASE_CFN_LLRINT:
340 CASE_CFN_LRINT:
341 CASE_CFN_NEARBYINT:
342 CASE_CFN_RINT:
343 return !flag_rounding_math;
345 default:
346 break;
348 return false;
351 /* Check whether we may negate an integer constant T without causing
352 overflow. */
354 bool
355 may_negate_without_overflow_p (const_tree t)
357 tree type;
359 gcc_assert (TREE_CODE (t) == INTEGER_CST);
361 type = TREE_TYPE (t);
362 if (TYPE_UNSIGNED (type))
363 return false;
365 return !wi::only_sign_bit_p (wi::to_wide (t));
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
371 static bool
372 negate_expr_p (tree t)
374 tree type;
376 if (t == 0)
377 return false;
379 type = TREE_TYPE (t);
381 STRIP_SIGN_NOPS (t);
382 switch (TREE_CODE (t))
384 case INTEGER_CST:
385 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
386 return true;
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t);
390 case BIT_NOT_EXPR:
391 return (INTEGRAL_TYPE_P (type)
392 && TYPE_OVERFLOW_WRAPS (type));
394 case FIXED_CST:
395 return true;
397 case NEGATE_EXPR:
398 return !TYPE_OVERFLOW_SANITIZED (type);
400 case REAL_CST:
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
405 case COMPLEX_CST:
406 return negate_expr_p (TREE_REALPART (t))
407 && negate_expr_p (TREE_IMAGPART (t));
409 case VECTOR_CST:
411 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
412 return true;
414 /* Steps don't prevent negation. */
415 unsigned int count = vector_cst_encoded_nelts (t);
416 for (unsigned int i = 0; i < count; ++i)
417 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
418 return false;
420 return true;
423 case COMPLEX_EXPR:
424 return negate_expr_p (TREE_OPERAND (t, 0))
425 && negate_expr_p (TREE_OPERAND (t, 1));
427 case CONJ_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0));
430 case PLUS_EXPR:
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
432 || HONOR_SIGNED_ZEROS (element_mode (type))
433 || (ANY_INTEGRAL_TYPE_P (type)
434 && ! TYPE_OVERFLOW_WRAPS (type)))
435 return false;
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t, 1)))
438 return true;
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t, 0));
442 case MINUS_EXPR:
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
445 && !HONOR_SIGNED_ZEROS (element_mode (type))
446 && (! ANY_INTEGRAL_TYPE_P (type)
447 || TYPE_OVERFLOW_WRAPS (type));
449 case MULT_EXPR:
450 if (TYPE_UNSIGNED (type))
451 break;
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && (wi::popcount
458 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
460 && (wi::popcount
461 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
462 break;
464 /* Fall through. */
466 case RDIV_EXPR:
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
470 break;
472 case TRUNC_DIV_EXPR:
473 case ROUND_DIV_EXPR:
474 case EXACT_DIV_EXPR:
475 if (TYPE_UNSIGNED (type))
476 break;
477 if (negate_expr_p (TREE_OPERAND (t, 0)))
478 return true;
479 /* In general we can't negate B in A / B, because if A is INT_MIN and
480 B is 1, we may turn this into INT_MIN / -1 which is undefined
481 and actually traps on some architectures. */
482 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
483 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
484 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
485 && ! integer_onep (TREE_OPERAND (t, 1))))
486 return negate_expr_p (TREE_OPERAND (t, 1));
487 break;
489 case NOP_EXPR:
490 /* Negate -((double)float) as (double)(-float). */
491 if (TREE_CODE (type) == REAL_TYPE)
493 tree tem = strip_float_extensions (t);
494 if (tem != t)
495 return negate_expr_p (tem);
497 break;
499 case CALL_EXPR:
500 /* Negate -f(x) as f(-x). */
501 if (negate_mathfn_p (get_call_combined_fn (t)))
502 return negate_expr_p (CALL_EXPR_ARG (t, 0));
503 break;
505 case RSHIFT_EXPR:
506 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
507 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
509 tree op1 = TREE_OPERAND (t, 1);
510 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
511 return true;
513 break;
515 default:
516 break;
518 return false;
521 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
522 simplification is possible.
523 If negate_expr_p would return true for T, NULL_TREE will never be
524 returned. */
526 static tree
527 fold_negate_expr_1 (location_t loc, tree t)
529 tree type = TREE_TYPE (t);
530 tree tem;
532 switch (TREE_CODE (t))
534 /* Convert - (~A) to A + 1. */
535 case BIT_NOT_EXPR:
536 if (INTEGRAL_TYPE_P (type))
537 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
538 build_one_cst (type));
539 break;
541 case INTEGER_CST:
542 tem = fold_negate_const (t, type);
543 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
544 || (ANY_INTEGRAL_TYPE_P (type)
545 && !TYPE_OVERFLOW_TRAPS (type)
546 && TYPE_OVERFLOW_WRAPS (type))
547 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
548 return tem;
549 break;
551 case POLY_INT_CST:
552 case REAL_CST:
553 case FIXED_CST:
554 tem = fold_negate_const (t, type);
555 return tem;
557 case COMPLEX_CST:
559 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
560 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
561 if (rpart && ipart)
562 return build_complex (type, rpart, ipart);
564 break;
566 case VECTOR_CST:
568 tree_vector_builder elts;
569 elts.new_unary_operation (type, t, true);
570 unsigned int count = elts.encoded_nelts ();
571 for (unsigned int i = 0; i < count; ++i)
573 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
574 if (elt == NULL_TREE)
575 return NULL_TREE;
576 elts.quick_push (elt);
579 return elts.build ();
582 case COMPLEX_EXPR:
583 if (negate_expr_p (t))
584 return fold_build2_loc (loc, COMPLEX_EXPR, type,
585 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
586 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
587 break;
589 case CONJ_EXPR:
590 if (negate_expr_p (t))
591 return fold_build1_loc (loc, CONJ_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
593 break;
595 case NEGATE_EXPR:
596 if (!TYPE_OVERFLOW_SANITIZED (type))
597 return TREE_OPERAND (t, 0);
598 break;
600 case PLUS_EXPR:
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
602 && !HONOR_SIGNED_ZEROS (element_mode (type)))
604 /* -(A + B) -> (-B) - A. */
605 if (negate_expr_p (TREE_OPERAND (t, 1)))
607 tem = negate_expr (TREE_OPERAND (t, 1));
608 return fold_build2_loc (loc, MINUS_EXPR, type,
609 tem, TREE_OPERAND (t, 0));
612 /* -(A + B) -> (-A) - B. */
613 if (negate_expr_p (TREE_OPERAND (t, 0)))
615 tem = negate_expr (TREE_OPERAND (t, 0));
616 return fold_build2_loc (loc, MINUS_EXPR, type,
617 tem, TREE_OPERAND (t, 1));
620 break;
622 case MINUS_EXPR:
623 /* - (A - B) -> B - A */
624 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
625 && !HONOR_SIGNED_ZEROS (element_mode (type)))
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
628 break;
630 case MULT_EXPR:
631 if (TYPE_UNSIGNED (type))
632 break;
634 /* Fall through. */
636 case RDIV_EXPR:
637 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
639 tem = TREE_OPERAND (t, 1);
640 if (negate_expr_p (tem))
641 return fold_build2_loc (loc, TREE_CODE (t), type,
642 TREE_OPERAND (t, 0), negate_expr (tem));
643 tem = TREE_OPERAND (t, 0);
644 if (negate_expr_p (tem))
645 return fold_build2_loc (loc, TREE_CODE (t), type,
646 negate_expr (tem), TREE_OPERAND (t, 1));
648 break;
650 case TRUNC_DIV_EXPR:
651 case ROUND_DIV_EXPR:
652 case EXACT_DIV_EXPR:
653 if (TYPE_UNSIGNED (type))
654 break;
655 if (negate_expr_p (TREE_OPERAND (t, 0)))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (TREE_OPERAND (t, 0)),
658 TREE_OPERAND (t, 1));
659 /* In general we can't negate B in A / B, because if A is INT_MIN and
660 B is 1, we may turn this into INT_MIN / -1 which is undefined
661 and actually traps on some architectures. */
662 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
663 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
664 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
665 && ! integer_onep (TREE_OPERAND (t, 1))))
666 && negate_expr_p (TREE_OPERAND (t, 1)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 TREE_OPERAND (t, 0),
669 negate_expr (TREE_OPERAND (t, 1)));
670 break;
672 case NOP_EXPR:
673 /* Convert -((double)float) into (double)(-float). */
674 if (TREE_CODE (type) == REAL_TYPE)
676 tem = strip_float_extensions (t);
677 if (tem != t && negate_expr_p (tem))
678 return fold_convert_loc (loc, type, negate_expr (tem));
680 break;
682 case CALL_EXPR:
683 /* Negate -f(x) as f(-x). */
684 if (negate_mathfn_p (get_call_combined_fn (t))
685 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
687 tree fndecl, arg;
689 fndecl = get_callee_fndecl (t);
690 arg = negate_expr (CALL_EXPR_ARG (t, 0));
691 return build_call_expr_loc (loc, fndecl, 1, arg);
693 break;
695 case RSHIFT_EXPR:
696 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
697 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
699 tree op1 = TREE_OPERAND (t, 1);
700 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
702 tree ntype = TYPE_UNSIGNED (type)
703 ? signed_type_for (type)
704 : unsigned_type_for (type);
705 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
706 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
707 return fold_convert_loc (loc, type, temp);
710 break;
712 default:
713 break;
716 return NULL_TREE;
719 /* A wrapper for fold_negate_expr_1. */
721 static tree
722 fold_negate_expr (location_t loc, tree t)
724 tree type = TREE_TYPE (t);
725 STRIP_SIGN_NOPS (t);
726 tree tem = fold_negate_expr_1 (loc, t);
727 if (tem == NULL_TREE)
728 return NULL_TREE;
729 return fold_convert_loc (loc, type, tem);
732 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
733 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
734 return NULL_TREE. */
736 static tree
737 negate_expr (tree t)
739 tree type, tem;
740 location_t loc;
742 if (t == NULL_TREE)
743 return NULL_TREE;
745 loc = EXPR_LOCATION (t);
746 type = TREE_TYPE (t);
747 STRIP_SIGN_NOPS (t);
749 tem = fold_negate_expr (loc, t);
750 if (!tem)
751 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
752 return fold_convert_loc (loc, type, tem);
755 /* Split a tree IN into a constant, literal and variable parts that could be
756 combined with CODE to make IN. "constant" means an expression with
757 TREE_CONSTANT but that isn't an actual constant. CODE must be a
758 commutative arithmetic operation. Store the constant part into *CONP,
759 the literal in *LITP and return the variable part. If a part isn't
760 present, set it to null. If the tree does not decompose in this way,
761 return the entire tree as the variable part and the other parts as null.
763 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
764 case, we negate an operand that was subtracted. Except if it is a
765 literal for which we use *MINUS_LITP instead.
767 If NEGATE_P is true, we are negating all of IN, again except a literal
768 for which we use *MINUS_LITP instead. If a variable part is of pointer
769 type, it is negated after converting to TYPE. This prevents us from
770 generating illegal MINUS pointer expression. LOC is the location of
771 the converted variable part.
773 If IN is itself a literal or constant, return it as appropriate.
775 Note that we do not guarantee that any of the three values will be the
776 same type as IN, but they will have the same signedness and mode. */
778 static tree
779 split_tree (tree in, tree type, enum tree_code code,
780 tree *minus_varp, tree *conp, tree *minus_conp,
781 tree *litp, tree *minus_litp, int negate_p)
783 tree var = 0;
784 *minus_varp = 0;
785 *conp = 0;
786 *minus_conp = 0;
787 *litp = 0;
788 *minus_litp = 0;
790 /* Strip any conversions that don't change the machine mode or signedness. */
791 STRIP_SIGN_NOPS (in);
793 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
794 || TREE_CODE (in) == FIXED_CST)
795 *litp = in;
796 else if (TREE_CODE (in) == code
797 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
798 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
799 /* We can associate addition and subtraction together (even
800 though the C standard doesn't say so) for integers because
801 the value is not affected. For reals, the value might be
802 affected, so we can't. */
803 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
804 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805 || (code == MINUS_EXPR
806 && (TREE_CODE (in) == PLUS_EXPR
807 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
809 tree op0 = TREE_OPERAND (in, 0);
810 tree op1 = TREE_OPERAND (in, 1);
811 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
812 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
814 /* First see if either of the operands is a literal, then a constant. */
815 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
816 || TREE_CODE (op0) == FIXED_CST)
817 *litp = op0, op0 = 0;
818 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
819 || TREE_CODE (op1) == FIXED_CST)
820 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
822 if (op0 != 0 && TREE_CONSTANT (op0))
823 *conp = op0, op0 = 0;
824 else if (op1 != 0 && TREE_CONSTANT (op1))
825 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
827 /* If we haven't dealt with either operand, this is not a case we can
828 decompose. Otherwise, VAR is either of the ones remaining, if any. */
829 if (op0 != 0 && op1 != 0)
830 var = in;
831 else if (op0 != 0)
832 var = op0;
833 else
834 var = op1, neg_var_p = neg1_p;
836 /* Now do any needed negations. */
837 if (neg_litp_p)
838 *minus_litp = *litp, *litp = 0;
839 if (neg_conp_p && *conp)
840 *minus_conp = *conp, *conp = 0;
841 if (neg_var_p && var)
842 *minus_varp = var, var = 0;
844 else if (TREE_CONSTANT (in))
845 *conp = in;
846 else if (TREE_CODE (in) == BIT_NOT_EXPR
847 && code == PLUS_EXPR)
849 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
850 when IN is constant. */
851 *litp = build_minus_one_cst (type);
852 *minus_varp = TREE_OPERAND (in, 0);
854 else
855 var = in;
857 if (negate_p)
859 if (*litp)
860 *minus_litp = *litp, *litp = 0;
861 else if (*minus_litp)
862 *litp = *minus_litp, *minus_litp = 0;
863 if (*conp)
864 *minus_conp = *conp, *conp = 0;
865 else if (*minus_conp)
866 *conp = *minus_conp, *minus_conp = 0;
867 if (var)
868 *minus_varp = var, var = 0;
869 else if (*minus_varp)
870 var = *minus_varp, *minus_varp = 0;
873 if (*litp
874 && TREE_OVERFLOW_P (*litp))
875 *litp = drop_tree_overflow (*litp);
876 if (*minus_litp
877 && TREE_OVERFLOW_P (*minus_litp))
878 *minus_litp = drop_tree_overflow (*minus_litp);
880 return var;
883 /* Re-associate trees split by the above function. T1 and T2 are
884 either expressions to associate or null. Return the new
885 expression, if any. LOC is the location of the new expression. If
886 we build an operation, do it in TYPE and with CODE. */
888 static tree
889 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
891 if (t1 == 0)
893 gcc_assert (t2 == 0 || code != MINUS_EXPR);
894 return t2;
896 else if (t2 == 0)
897 return t1;
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
903 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
904 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
906 if (code == PLUS_EXPR)
908 if (TREE_CODE (t1) == NEGATE_EXPR)
909 return build2_loc (loc, MINUS_EXPR, type,
910 fold_convert_loc (loc, type, t2),
911 fold_convert_loc (loc, type,
912 TREE_OPERAND (t1, 0)));
913 else if (TREE_CODE (t2) == NEGATE_EXPR)
914 return build2_loc (loc, MINUS_EXPR, type,
915 fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type,
917 TREE_OPERAND (t2, 0)));
918 else if (integer_zerop (t2))
919 return fold_convert_loc (loc, type, t1);
921 else if (code == MINUS_EXPR)
923 if (integer_zerop (t2))
924 return fold_convert_loc (loc, type, t1);
927 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
928 fold_convert_loc (loc, type, t2));
931 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 fold_convert_loc (loc, type, t2));
935 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
936 for use in int_const_binop, size_binop and size_diffop. */
938 static bool
939 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
941 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
942 return false;
943 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
944 return false;
946 switch (code)
948 case LSHIFT_EXPR:
949 case RSHIFT_EXPR:
950 case LROTATE_EXPR:
951 case RROTATE_EXPR:
952 return true;
954 default:
955 break;
958 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
959 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
960 && TYPE_MODE (type1) == TYPE_MODE (type2);
963 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
965 static tree
966 int_const_binop_2 (enum tree_code code, const_tree parg1, const_tree parg2,
967 int overflowable)
969 wide_int res;
970 tree t;
971 tree type = TREE_TYPE (parg1);
972 signop sign = TYPE_SIGN (type);
973 bool overflow = false;
975 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
976 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
978 switch (code)
980 case BIT_IOR_EXPR:
981 res = wi::bit_or (arg1, arg2);
982 break;
984 case BIT_XOR_EXPR:
985 res = wi::bit_xor (arg1, arg2);
986 break;
988 case BIT_AND_EXPR:
989 res = wi::bit_and (arg1, arg2);
990 break;
992 case RSHIFT_EXPR:
993 case LSHIFT_EXPR:
994 if (wi::neg_p (arg2))
996 arg2 = -arg2;
997 if (code == RSHIFT_EXPR)
998 code = LSHIFT_EXPR;
999 else
1000 code = RSHIFT_EXPR;
1003 if (code == RSHIFT_EXPR)
1004 /* It's unclear from the C standard whether shifts can overflow.
1005 The following code ignores overflow; perhaps a C standard
1006 interpretation ruling is needed. */
1007 res = wi::rshift (arg1, arg2, sign);
1008 else
1009 res = wi::lshift (arg1, arg2);
1010 break;
1012 case RROTATE_EXPR:
1013 case LROTATE_EXPR:
1014 if (wi::neg_p (arg2))
1016 arg2 = -arg2;
1017 if (code == RROTATE_EXPR)
1018 code = LROTATE_EXPR;
1019 else
1020 code = RROTATE_EXPR;
1023 if (code == RROTATE_EXPR)
1024 res = wi::rrotate (arg1, arg2);
1025 else
1026 res = wi::lrotate (arg1, arg2);
1027 break;
1029 case PLUS_EXPR:
1030 res = wi::add (arg1, arg2, sign, &overflow);
1031 break;
1033 case MINUS_EXPR:
1034 res = wi::sub (arg1, arg2, sign, &overflow);
1035 break;
1037 case MULT_EXPR:
1038 res = wi::mul (arg1, arg2, sign, &overflow);
1039 break;
1041 case MULT_HIGHPART_EXPR:
1042 res = wi::mul_high (arg1, arg2, sign);
1043 break;
1045 case TRUNC_DIV_EXPR:
1046 case EXACT_DIV_EXPR:
1047 if (arg2 == 0)
1048 return NULL_TREE;
1049 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1050 break;
1052 case FLOOR_DIV_EXPR:
1053 if (arg2 == 0)
1054 return NULL_TREE;
1055 res = wi::div_floor (arg1, arg2, sign, &overflow);
1056 break;
1058 case CEIL_DIV_EXPR:
1059 if (arg2 == 0)
1060 return NULL_TREE;
1061 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1062 break;
1064 case ROUND_DIV_EXPR:
1065 if (arg2 == 0)
1066 return NULL_TREE;
1067 res = wi::div_round (arg1, arg2, sign, &overflow);
1068 break;
1070 case TRUNC_MOD_EXPR:
1071 if (arg2 == 0)
1072 return NULL_TREE;
1073 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1074 break;
1076 case FLOOR_MOD_EXPR:
1077 if (arg2 == 0)
1078 return NULL_TREE;
1079 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1080 break;
1082 case CEIL_MOD_EXPR:
1083 if (arg2 == 0)
1084 return NULL_TREE;
1085 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1086 break;
1088 case ROUND_MOD_EXPR:
1089 if (arg2 == 0)
1090 return NULL_TREE;
1091 res = wi::mod_round (arg1, arg2, sign, &overflow);
1092 break;
1094 case MIN_EXPR:
1095 res = wi::min (arg1, arg2, sign);
1096 break;
1098 case MAX_EXPR:
1099 res = wi::max (arg1, arg2, sign);
1100 break;
1102 default:
1103 return NULL_TREE;
1106 t = force_fit_type (type, res, overflowable,
1107 (((sign == SIGNED || overflowable == -1)
1108 && overflow)
1109 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1111 return t;
1114 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1115 to produce a new constant. Return NULL_TREE if we don't know how
1116 to evaluate CODE at compile-time. */
1118 static tree
1119 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
1120 int overflowable)
1122 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1123 return int_const_binop_2 (code, arg1, arg2, overflowable);
1125 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1127 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1129 poly_wide_int res;
1130 bool overflow;
1131 tree type = TREE_TYPE (arg1);
1132 signop sign = TYPE_SIGN (type);
1133 switch (code)
1135 case PLUS_EXPR:
1136 res = wi::add (wi::to_poly_wide (arg1),
1137 wi::to_poly_wide (arg2), sign, &overflow);
1138 break;
1140 case MINUS_EXPR:
1141 res = wi::sub (wi::to_poly_wide (arg1),
1142 wi::to_poly_wide (arg2), sign, &overflow);
1143 break;
1145 case MULT_EXPR:
1146 if (TREE_CODE (arg2) == INTEGER_CST)
1147 res = wi::mul (wi::to_poly_wide (arg1),
1148 wi::to_wide (arg2), sign, &overflow);
1149 else if (TREE_CODE (arg1) == INTEGER_CST)
1150 res = wi::mul (wi::to_poly_wide (arg2),
1151 wi::to_wide (arg1), sign, &overflow);
1152 else
1153 return NULL_TREE;
1154 break;
1156 case LSHIFT_EXPR:
1157 if (TREE_CODE (arg2) == INTEGER_CST)
1158 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1159 else
1160 return NULL_TREE;
1161 break;
1163 case BIT_IOR_EXPR:
1164 if (TREE_CODE (arg2) != INTEGER_CST
1165 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1166 &res))
1167 return NULL_TREE;
1168 break;
1170 default:
1171 return NULL_TREE;
1173 return force_fit_type (type, res, overflowable,
1174 (((sign == SIGNED || overflowable == -1)
1175 && overflow)
1176 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1179 return NULL_TREE;
1182 tree
1183 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1185 return int_const_binop_1 (code, arg1, arg2, 1);
1188 /* Return true if binary operation OP distributes over addition in operand
1189 OPNO, with the other operand being held constant. OPNO counts from 1. */
1191 static bool
1192 distributes_over_addition_p (tree_code op, int opno)
1194 switch (op)
1196 case PLUS_EXPR:
1197 case MINUS_EXPR:
1198 case MULT_EXPR:
1199 return true;
1201 case LSHIFT_EXPR:
1202 return opno == 1;
1204 default:
1205 return false;
1209 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1210 constant. We assume ARG1 and ARG2 have the same data type, or at least
1211 are the same kind of constant and the same machine mode. Return zero if
1212 combining the constants is not allowed in the current operating mode. */
1214 static tree
1215 const_binop (enum tree_code code, tree arg1, tree arg2)
1217 /* Sanity check for the recursive cases. */
1218 if (!arg1 || !arg2)
1219 return NULL_TREE;
1221 STRIP_NOPS (arg1);
1222 STRIP_NOPS (arg2);
1224 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1226 if (code == POINTER_PLUS_EXPR)
1227 return int_const_binop (PLUS_EXPR,
1228 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1230 return int_const_binop (code, arg1, arg2);
1233 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1235 machine_mode mode;
1236 REAL_VALUE_TYPE d1;
1237 REAL_VALUE_TYPE d2;
1238 REAL_VALUE_TYPE value;
1239 REAL_VALUE_TYPE result;
1240 bool inexact;
1241 tree t, type;
1243 /* The following codes are handled by real_arithmetic. */
1244 switch (code)
1246 case PLUS_EXPR:
1247 case MINUS_EXPR:
1248 case MULT_EXPR:
1249 case RDIV_EXPR:
1250 case MIN_EXPR:
1251 case MAX_EXPR:
1252 break;
1254 default:
1255 return NULL_TREE;
1258 d1 = TREE_REAL_CST (arg1);
1259 d2 = TREE_REAL_CST (arg2);
1261 type = TREE_TYPE (arg1);
1262 mode = TYPE_MODE (type);
1264 /* Don't perform operation if we honor signaling NaNs and
1265 either operand is a signaling NaN. */
1266 if (HONOR_SNANS (mode)
1267 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1268 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1269 return NULL_TREE;
1271 /* Don't perform operation if it would raise a division
1272 by zero exception. */
1273 if (code == RDIV_EXPR
1274 && real_equal (&d2, &dconst0)
1275 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1276 return NULL_TREE;
1278 /* If either operand is a NaN, just return it. Otherwise, set up
1279 for floating-point trap; we return an overflow. */
1280 if (REAL_VALUE_ISNAN (d1))
1282 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1283 is off. */
1284 d1.signalling = 0;
1285 t = build_real (type, d1);
1286 return t;
1288 else if (REAL_VALUE_ISNAN (d2))
1290 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1291 is off. */
1292 d2.signalling = 0;
1293 t = build_real (type, d2);
1294 return t;
1297 inexact = real_arithmetic (&value, code, &d1, &d2);
1298 real_convert (&result, mode, &value);
1300 /* Don't constant fold this floating point operation if
1301 the result has overflowed and flag_trapping_math. */
1302 if (flag_trapping_math
1303 && MODE_HAS_INFINITIES (mode)
1304 && REAL_VALUE_ISINF (result)
1305 && !REAL_VALUE_ISINF (d1)
1306 && !REAL_VALUE_ISINF (d2))
1307 return NULL_TREE;
1309 /* Don't constant fold this floating point operation if the
1310 result may dependent upon the run-time rounding mode and
1311 flag_rounding_math is set, or if GCC's software emulation
1312 is unable to accurately represent the result. */
1313 if ((flag_rounding_math
1314 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1315 && (inexact || !real_identical (&result, &value)))
1316 return NULL_TREE;
1318 t = build_real (type, result);
1320 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1321 return t;
1324 if (TREE_CODE (arg1) == FIXED_CST)
1326 FIXED_VALUE_TYPE f1;
1327 FIXED_VALUE_TYPE f2;
1328 FIXED_VALUE_TYPE result;
1329 tree t, type;
1330 int sat_p;
1331 bool overflow_p;
1333 /* The following codes are handled by fixed_arithmetic. */
1334 switch (code)
1336 case PLUS_EXPR:
1337 case MINUS_EXPR:
1338 case MULT_EXPR:
1339 case TRUNC_DIV_EXPR:
1340 if (TREE_CODE (arg2) != FIXED_CST)
1341 return NULL_TREE;
1342 f2 = TREE_FIXED_CST (arg2);
1343 break;
1345 case LSHIFT_EXPR:
1346 case RSHIFT_EXPR:
1348 if (TREE_CODE (arg2) != INTEGER_CST)
1349 return NULL_TREE;
1350 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1351 f2.data.high = w2.elt (1);
1352 f2.data.low = w2.ulow ();
1353 f2.mode = SImode;
1355 break;
1357 default:
1358 return NULL_TREE;
1361 f1 = TREE_FIXED_CST (arg1);
1362 type = TREE_TYPE (arg1);
1363 sat_p = TYPE_SATURATING (type);
1364 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1365 t = build_fixed (type, result);
1366 /* Propagate overflow flags. */
1367 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1368 TREE_OVERFLOW (t) = 1;
1369 return t;
1372 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1374 tree type = TREE_TYPE (arg1);
1375 tree r1 = TREE_REALPART (arg1);
1376 tree i1 = TREE_IMAGPART (arg1);
1377 tree r2 = TREE_REALPART (arg2);
1378 tree i2 = TREE_IMAGPART (arg2);
1379 tree real, imag;
1381 switch (code)
1383 case PLUS_EXPR:
1384 case MINUS_EXPR:
1385 real = const_binop (code, r1, r2);
1386 imag = const_binop (code, i1, i2);
1387 break;
1389 case MULT_EXPR:
1390 if (COMPLEX_FLOAT_TYPE_P (type))
1391 return do_mpc_arg2 (arg1, arg2, type,
1392 /* do_nonfinite= */ folding_initializer,
1393 mpc_mul);
1395 real = const_binop (MINUS_EXPR,
1396 const_binop (MULT_EXPR, r1, r2),
1397 const_binop (MULT_EXPR, i1, i2));
1398 imag = const_binop (PLUS_EXPR,
1399 const_binop (MULT_EXPR, r1, i2),
1400 const_binop (MULT_EXPR, i1, r2));
1401 break;
1403 case RDIV_EXPR:
1404 if (COMPLEX_FLOAT_TYPE_P (type))
1405 return do_mpc_arg2 (arg1, arg2, type,
1406 /* do_nonfinite= */ folding_initializer,
1407 mpc_div);
1408 /* Fallthru. */
1409 case TRUNC_DIV_EXPR:
1410 case CEIL_DIV_EXPR:
1411 case FLOOR_DIV_EXPR:
1412 case ROUND_DIV_EXPR:
1413 if (flag_complex_method == 0)
1415 /* Keep this algorithm in sync with
1416 tree-complex.c:expand_complex_div_straight().
1418 Expand complex division to scalars, straightforward algorithm.
1419 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1420 t = br*br + bi*bi
1422 tree magsquared
1423 = const_binop (PLUS_EXPR,
1424 const_binop (MULT_EXPR, r2, r2),
1425 const_binop (MULT_EXPR, i2, i2));
1426 tree t1
1427 = const_binop (PLUS_EXPR,
1428 const_binop (MULT_EXPR, r1, r2),
1429 const_binop (MULT_EXPR, i1, i2));
1430 tree t2
1431 = const_binop (MINUS_EXPR,
1432 const_binop (MULT_EXPR, i1, r2),
1433 const_binop (MULT_EXPR, r1, i2));
1435 real = const_binop (code, t1, magsquared);
1436 imag = const_binop (code, t2, magsquared);
1438 else
1440 /* Keep this algorithm in sync with
1441 tree-complex.c:expand_complex_div_wide().
1443 Expand complex division to scalars, modified algorithm to minimize
1444 overflow with wide input ranges. */
1445 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1446 fold_abs_const (r2, TREE_TYPE (type)),
1447 fold_abs_const (i2, TREE_TYPE (type)));
1449 if (integer_nonzerop (compare))
1451 /* In the TRUE branch, we compute
1452 ratio = br/bi;
1453 div = (br * ratio) + bi;
1454 tr = (ar * ratio) + ai;
1455 ti = (ai * ratio) - ar;
1456 tr = tr / div;
1457 ti = ti / div; */
1458 tree ratio = const_binop (code, r2, i2);
1459 tree div = const_binop (PLUS_EXPR, i2,
1460 const_binop (MULT_EXPR, r2, ratio));
1461 real = const_binop (MULT_EXPR, r1, ratio);
1462 real = const_binop (PLUS_EXPR, real, i1);
1463 real = const_binop (code, real, div);
1465 imag = const_binop (MULT_EXPR, i1, ratio);
1466 imag = const_binop (MINUS_EXPR, imag, r1);
1467 imag = const_binop (code, imag, div);
1469 else
1471 /* In the FALSE branch, we compute
1472 ratio = d/c;
1473 divisor = (d * ratio) + c;
1474 tr = (b * ratio) + a;
1475 ti = b - (a * ratio);
1476 tr = tr / div;
1477 ti = ti / div; */
1478 tree ratio = const_binop (code, i2, r2);
1479 tree div = const_binop (PLUS_EXPR, r2,
1480 const_binop (MULT_EXPR, i2, ratio));
1482 real = const_binop (MULT_EXPR, i1, ratio);
1483 real = const_binop (PLUS_EXPR, real, r1);
1484 real = const_binop (code, real, div);
1486 imag = const_binop (MULT_EXPR, r1, ratio);
1487 imag = const_binop (MINUS_EXPR, i1, imag);
1488 imag = const_binop (code, imag, div);
1491 break;
1493 default:
1494 return NULL_TREE;
1497 if (real && imag)
1498 return build_complex (type, real, imag);
1501 if (TREE_CODE (arg1) == VECTOR_CST
1502 && TREE_CODE (arg2) == VECTOR_CST
1503 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1504 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1506 tree type = TREE_TYPE (arg1);
1507 bool step_ok_p;
1508 if (VECTOR_CST_STEPPED_P (arg1)
1509 && VECTOR_CST_STEPPED_P (arg2))
1510 /* We can operate directly on the encoding if:
1512 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1513 implies
1514 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1516 Addition and subtraction are the supported operators
1517 for which this is true. */
1518 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1519 else if (VECTOR_CST_STEPPED_P (arg1))
1520 /* We can operate directly on stepped encodings if:
1522 a3 - a2 == a2 - a1
1523 implies:
1524 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1526 which is true if (x -> x op c) distributes over addition. */
1527 step_ok_p = distributes_over_addition_p (code, 1);
1528 else
1529 /* Similarly in reverse. */
1530 step_ok_p = distributes_over_addition_p (code, 2);
1531 tree_vector_builder elts;
1532 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1533 return NULL_TREE;
1534 unsigned int count = elts.encoded_nelts ();
1535 for (unsigned int i = 0; i < count; ++i)
1537 tree elem1 = VECTOR_CST_ELT (arg1, i);
1538 tree elem2 = VECTOR_CST_ELT (arg2, i);
1540 tree elt = const_binop (code, elem1, elem2);
1542 /* It is possible that const_binop cannot handle the given
1543 code and return NULL_TREE */
1544 if (elt == NULL_TREE)
1545 return NULL_TREE;
1546 elts.quick_push (elt);
1549 return elts.build ();
1552 /* Shifts allow a scalar offset for a vector. */
1553 if (TREE_CODE (arg1) == VECTOR_CST
1554 && TREE_CODE (arg2) == INTEGER_CST)
1556 tree type = TREE_TYPE (arg1);
1557 bool step_ok_p = distributes_over_addition_p (code, 1);
1558 tree_vector_builder elts;
1559 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1560 return NULL_TREE;
1561 unsigned int count = elts.encoded_nelts ();
1562 for (unsigned int i = 0; i < count; ++i)
1564 tree elem1 = VECTOR_CST_ELT (arg1, i);
1566 tree elt = const_binop (code, elem1, arg2);
1568 /* It is possible that const_binop cannot handle the given
1569 code and return NULL_TREE. */
1570 if (elt == NULL_TREE)
1571 return NULL_TREE;
1572 elts.quick_push (elt);
1575 return elts.build ();
1577 return NULL_TREE;
1580 /* Overload that adds a TYPE parameter to be able to dispatch
1581 to fold_relational_const. */
1583 tree
1584 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1586 if (TREE_CODE_CLASS (code) == tcc_comparison)
1587 return fold_relational_const (code, type, arg1, arg2);
1589 /* ??? Until we make the const_binop worker take the type of the
1590 result as argument put those cases that need it here. */
1591 switch (code)
1593 case VEC_SERIES_EXPR:
1594 if (CONSTANT_CLASS_P (arg1)
1595 && CONSTANT_CLASS_P (arg2))
1596 return build_vec_series (type, arg1, arg2);
1597 return NULL_TREE;
1599 case COMPLEX_EXPR:
1600 if ((TREE_CODE (arg1) == REAL_CST
1601 && TREE_CODE (arg2) == REAL_CST)
1602 || (TREE_CODE (arg1) == INTEGER_CST
1603 && TREE_CODE (arg2) == INTEGER_CST))
1604 return build_complex (type, arg1, arg2);
1605 return NULL_TREE;
1607 case POINTER_DIFF_EXPR:
1608 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1610 offset_int res = wi::sub (wi::to_offset (arg1),
1611 wi::to_offset (arg2));
1612 return force_fit_type (type, res, 1,
1613 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1615 return NULL_TREE;
1617 case VEC_PACK_TRUNC_EXPR:
1618 case VEC_PACK_FIX_TRUNC_EXPR:
1620 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1622 if (TREE_CODE (arg1) != VECTOR_CST
1623 || TREE_CODE (arg2) != VECTOR_CST)
1624 return NULL_TREE;
1626 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1627 return NULL_TREE;
1629 out_nelts = in_nelts * 2;
1630 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1631 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1633 tree_vector_builder elts (type, out_nelts, 1);
1634 for (i = 0; i < out_nelts; i++)
1636 tree elt = (i < in_nelts
1637 ? VECTOR_CST_ELT (arg1, i)
1638 : VECTOR_CST_ELT (arg2, i - in_nelts));
1639 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1640 ? NOP_EXPR : FIX_TRUNC_EXPR,
1641 TREE_TYPE (type), elt);
1642 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1643 return NULL_TREE;
1644 elts.quick_push (elt);
1647 return elts.build ();
1650 case VEC_WIDEN_MULT_LO_EXPR:
1651 case VEC_WIDEN_MULT_HI_EXPR:
1652 case VEC_WIDEN_MULT_EVEN_EXPR:
1653 case VEC_WIDEN_MULT_ODD_EXPR:
1655 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1657 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1658 return NULL_TREE;
1660 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1661 return NULL_TREE;
1662 out_nelts = in_nelts / 2;
1663 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1664 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1666 if (code == VEC_WIDEN_MULT_LO_EXPR)
1667 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1668 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1669 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1670 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1671 scale = 1, ofs = 0;
1672 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1673 scale = 1, ofs = 1;
1675 tree_vector_builder elts (type, out_nelts, 1);
1676 for (out = 0; out < out_nelts; out++)
1678 unsigned int in = (out << scale) + ofs;
1679 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1680 VECTOR_CST_ELT (arg1, in));
1681 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1682 VECTOR_CST_ELT (arg2, in));
1684 if (t1 == NULL_TREE || t2 == NULL_TREE)
1685 return NULL_TREE;
1686 tree elt = const_binop (MULT_EXPR, t1, t2);
1687 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1688 return NULL_TREE;
1689 elts.quick_push (elt);
1692 return elts.build ();
1695 default:;
1698 if (TREE_CODE_CLASS (code) != tcc_binary)
1699 return NULL_TREE;
1701 /* Make sure type and arg0 have the same saturating flag. */
1702 gcc_checking_assert (TYPE_SATURATING (type)
1703 == TYPE_SATURATING (TREE_TYPE (arg1)));
1705 return const_binop (code, arg1, arg2);
1708 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1709 Return zero if computing the constants is not possible. */
1711 tree
1712 const_unop (enum tree_code code, tree type, tree arg0)
1714 /* Don't perform the operation, other than NEGATE and ABS, if
1715 flag_signaling_nans is on and the operand is a signaling NaN. */
1716 if (TREE_CODE (arg0) == REAL_CST
1717 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1718 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1719 && code != NEGATE_EXPR
1720 && code != ABS_EXPR)
1721 return NULL_TREE;
1723 switch (code)
1725 CASE_CONVERT:
1726 case FLOAT_EXPR:
1727 case FIX_TRUNC_EXPR:
1728 case FIXED_CONVERT_EXPR:
1729 return fold_convert_const (code, type, arg0);
1731 case ADDR_SPACE_CONVERT_EXPR:
1732 /* If the source address is 0, and the source address space
1733 cannot have a valid object at 0, fold to dest type null. */
1734 if (integer_zerop (arg0)
1735 && !(targetm.addr_space.zero_address_valid
1736 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1737 return fold_convert_const (code, type, arg0);
1738 break;
1740 case VIEW_CONVERT_EXPR:
1741 return fold_view_convert_expr (type, arg0);
1743 case NEGATE_EXPR:
1745 /* Can't call fold_negate_const directly here as that doesn't
1746 handle all cases and we might not be able to negate some
1747 constants. */
1748 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1749 if (tem && CONSTANT_CLASS_P (tem))
1750 return tem;
1751 break;
1754 case ABS_EXPR:
1755 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1756 return fold_abs_const (arg0, type);
1757 break;
1759 case CONJ_EXPR:
1760 if (TREE_CODE (arg0) == COMPLEX_CST)
1762 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1763 TREE_TYPE (type));
1764 return build_complex (type, TREE_REALPART (arg0), ipart);
1766 break;
1768 case BIT_NOT_EXPR:
1769 if (TREE_CODE (arg0) == INTEGER_CST)
1770 return fold_not_const (arg0, type);
1771 else if (POLY_INT_CST_P (arg0))
1772 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1773 /* Perform BIT_NOT_EXPR on each element individually. */
1774 else if (TREE_CODE (arg0) == VECTOR_CST)
1776 tree elem;
1778 /* This can cope with stepped encodings because ~x == -1 - x. */
1779 tree_vector_builder elements;
1780 elements.new_unary_operation (type, arg0, true);
1781 unsigned int i, count = elements.encoded_nelts ();
1782 for (i = 0; i < count; ++i)
1784 elem = VECTOR_CST_ELT (arg0, i);
1785 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1786 if (elem == NULL_TREE)
1787 break;
1788 elements.quick_push (elem);
1790 if (i == count)
1791 return elements.build ();
1793 break;
1795 case TRUTH_NOT_EXPR:
1796 if (TREE_CODE (arg0) == INTEGER_CST)
1797 return constant_boolean_node (integer_zerop (arg0), type);
1798 break;
1800 case REALPART_EXPR:
1801 if (TREE_CODE (arg0) == COMPLEX_CST)
1802 return fold_convert (type, TREE_REALPART (arg0));
1803 break;
1805 case IMAGPART_EXPR:
1806 if (TREE_CODE (arg0) == COMPLEX_CST)
1807 return fold_convert (type, TREE_IMAGPART (arg0));
1808 break;
1810 case VEC_UNPACK_LO_EXPR:
1811 case VEC_UNPACK_HI_EXPR:
1812 case VEC_UNPACK_FLOAT_LO_EXPR:
1813 case VEC_UNPACK_FLOAT_HI_EXPR:
1815 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1816 enum tree_code subcode;
1818 if (TREE_CODE (arg0) != VECTOR_CST)
1819 return NULL_TREE;
1821 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1822 return NULL_TREE;
1823 out_nelts = in_nelts / 2;
1824 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1826 unsigned int offset = 0;
1827 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1828 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1829 offset = out_nelts;
1831 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1832 subcode = NOP_EXPR;
1833 else
1834 subcode = FLOAT_EXPR;
1836 tree_vector_builder elts (type, out_nelts, 1);
1837 for (i = 0; i < out_nelts; i++)
1839 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1840 VECTOR_CST_ELT (arg0, i + offset));
1841 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1842 return NULL_TREE;
1843 elts.quick_push (elt);
1846 return elts.build ();
1849 case VEC_DUPLICATE_EXPR:
1850 if (CONSTANT_CLASS_P (arg0))
1851 return build_vector_from_val (type, arg0);
1852 return NULL_TREE;
1854 default:
1855 break;
1858 return NULL_TREE;
1861 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1862 indicates which particular sizetype to create. */
1864 tree
1865 size_int_kind (poly_int64 number, enum size_type_kind kind)
1867 return build_int_cst (sizetype_tab[(int) kind], number);
1870 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1871 is a tree code. The type of the result is taken from the operands.
1872 Both must be equivalent integer types, ala int_binop_types_match_p.
1873 If the operands are constant, so is the result. */
1875 tree
1876 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1878 tree type = TREE_TYPE (arg0);
1880 if (arg0 == error_mark_node || arg1 == error_mark_node)
1881 return error_mark_node;
1883 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1884 TREE_TYPE (arg1)));
1886 /* Handle the special case of two poly_int constants faster. */
1887 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1889 /* And some specific cases even faster than that. */
1890 if (code == PLUS_EXPR)
1892 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1893 return arg1;
1894 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1895 return arg0;
1897 else if (code == MINUS_EXPR)
1899 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1900 return arg0;
1902 else if (code == MULT_EXPR)
1904 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1905 return arg1;
1908 /* Handle general case of two integer constants. For sizetype
1909 constant calculations we always want to know about overflow,
1910 even in the unsigned case. */
1911 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1912 if (res != NULL_TREE)
1913 return res;
1916 return fold_build2_loc (loc, code, type, arg0, arg1);
1919 /* Given two values, either both of sizetype or both of bitsizetype,
1920 compute the difference between the two values. Return the value
1921 in signed type corresponding to the type of the operands. */
1923 tree
1924 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1926 tree type = TREE_TYPE (arg0);
1927 tree ctype;
1929 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1930 TREE_TYPE (arg1)));
1932 /* If the type is already signed, just do the simple thing. */
1933 if (!TYPE_UNSIGNED (type))
1934 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1936 if (type == sizetype)
1937 ctype = ssizetype;
1938 else if (type == bitsizetype)
1939 ctype = sbitsizetype;
1940 else
1941 ctype = signed_type_for (type);
1943 /* If either operand is not a constant, do the conversions to the signed
1944 type and subtract. The hardware will do the right thing with any
1945 overflow in the subtraction. */
1946 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1947 return size_binop_loc (loc, MINUS_EXPR,
1948 fold_convert_loc (loc, ctype, arg0),
1949 fold_convert_loc (loc, ctype, arg1));
1951 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1952 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1953 overflow) and negate (which can't either). Special-case a result
1954 of zero while we're here. */
1955 if (tree_int_cst_equal (arg0, arg1))
1956 return build_int_cst (ctype, 0);
1957 else if (tree_int_cst_lt (arg1, arg0))
1958 return fold_convert_loc (loc, ctype,
1959 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1960 else
1961 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1962 fold_convert_loc (loc, ctype,
1963 size_binop_loc (loc,
1964 MINUS_EXPR,
1965 arg1, arg0)));
1968 /* A subroutine of fold_convert_const handling conversions of an
1969 INTEGER_CST to another integer type. */
1971 static tree
1972 fold_convert_const_int_from_int (tree type, const_tree arg1)
1974 /* Given an integer constant, make new constant with new type,
1975 appropriately sign-extended or truncated. Use widest_int
1976 so that any extension is done according ARG1's type. */
1977 return force_fit_type (type, wi::to_widest (arg1),
1978 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1979 TREE_OVERFLOW (arg1));
1982 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1983 to an integer type. */
1985 static tree
1986 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1988 bool overflow = false;
1989 tree t;
1991 /* The following code implements the floating point to integer
1992 conversion rules required by the Java Language Specification,
1993 that IEEE NaNs are mapped to zero and values that overflow
1994 the target precision saturate, i.e. values greater than
1995 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1996 are mapped to INT_MIN. These semantics are allowed by the
1997 C and C++ standards that simply state that the behavior of
1998 FP-to-integer conversion is unspecified upon overflow. */
2000 wide_int val;
2001 REAL_VALUE_TYPE r;
2002 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2004 switch (code)
2006 case FIX_TRUNC_EXPR:
2007 real_trunc (&r, VOIDmode, &x);
2008 break;
2010 default:
2011 gcc_unreachable ();
2014 /* If R is NaN, return zero and show we have an overflow. */
2015 if (REAL_VALUE_ISNAN (r))
2017 overflow = true;
2018 val = wi::zero (TYPE_PRECISION (type));
2021 /* See if R is less than the lower bound or greater than the
2022 upper bound. */
2024 if (! overflow)
2026 tree lt = TYPE_MIN_VALUE (type);
2027 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2028 if (real_less (&r, &l))
2030 overflow = true;
2031 val = wi::to_wide (lt);
2035 if (! overflow)
2037 tree ut = TYPE_MAX_VALUE (type);
2038 if (ut)
2040 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2041 if (real_less (&u, &r))
2043 overflow = true;
2044 val = wi::to_wide (ut);
2049 if (! overflow)
2050 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2052 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2053 return t;
2056 /* A subroutine of fold_convert_const handling conversions of a
2057 FIXED_CST to an integer type. */
2059 static tree
2060 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2062 tree t;
2063 double_int temp, temp_trunc;
2064 scalar_mode mode;
2066 /* Right shift FIXED_CST to temp by fbit. */
2067 temp = TREE_FIXED_CST (arg1).data;
2068 mode = TREE_FIXED_CST (arg1).mode;
2069 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2071 temp = temp.rshift (GET_MODE_FBIT (mode),
2072 HOST_BITS_PER_DOUBLE_INT,
2073 SIGNED_FIXED_POINT_MODE_P (mode));
2075 /* Left shift temp to temp_trunc by fbit. */
2076 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2077 HOST_BITS_PER_DOUBLE_INT,
2078 SIGNED_FIXED_POINT_MODE_P (mode));
2080 else
2082 temp = double_int_zero;
2083 temp_trunc = double_int_zero;
2086 /* If FIXED_CST is negative, we need to round the value toward 0.
2087 By checking if the fractional bits are not zero to add 1 to temp. */
2088 if (SIGNED_FIXED_POINT_MODE_P (mode)
2089 && temp_trunc.is_negative ()
2090 && TREE_FIXED_CST (arg1).data != temp_trunc)
2091 temp += double_int_one;
2093 /* Given a fixed-point constant, make new constant with new type,
2094 appropriately sign-extended or truncated. */
2095 t = force_fit_type (type, temp, -1,
2096 (temp.is_negative ()
2097 && (TYPE_UNSIGNED (type)
2098 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2099 | TREE_OVERFLOW (arg1));
2101 return t;
2104 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2105 to another floating point type. */
2107 static tree
2108 fold_convert_const_real_from_real (tree type, const_tree arg1)
2110 REAL_VALUE_TYPE value;
2111 tree t;
2113 /* Don't perform the operation if flag_signaling_nans is on
2114 and the operand is a signaling NaN. */
2115 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2116 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2117 return NULL_TREE;
2119 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2120 t = build_real (type, value);
2122 /* If converting an infinity or NAN to a representation that doesn't
2123 have one, set the overflow bit so that we can produce some kind of
2124 error message at the appropriate point if necessary. It's not the
2125 most user-friendly message, but it's better than nothing. */
2126 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2127 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2128 TREE_OVERFLOW (t) = 1;
2129 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2130 && !MODE_HAS_NANS (TYPE_MODE (type)))
2131 TREE_OVERFLOW (t) = 1;
2132 /* Regular overflow, conversion produced an infinity in a mode that
2133 can't represent them. */
2134 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2135 && REAL_VALUE_ISINF (value)
2136 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2137 TREE_OVERFLOW (t) = 1;
2138 else
2139 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2140 return t;
2143 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2144 to a floating point type. */
2146 static tree
2147 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2149 REAL_VALUE_TYPE value;
2150 tree t;
2152 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2153 &TREE_FIXED_CST (arg1));
2154 t = build_real (type, value);
2156 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2157 return t;
2160 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2161 to another fixed-point type. */
2163 static tree
2164 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2166 FIXED_VALUE_TYPE value;
2167 tree t;
2168 bool overflow_p;
2170 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2171 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2172 t = build_fixed (type, value);
2174 /* Propagate overflow flags. */
2175 if (overflow_p | TREE_OVERFLOW (arg1))
2176 TREE_OVERFLOW (t) = 1;
2177 return t;
2180 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2181 to a fixed-point type. */
2183 static tree
2184 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2186 FIXED_VALUE_TYPE value;
2187 tree t;
2188 bool overflow_p;
2189 double_int di;
2191 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2193 di.low = TREE_INT_CST_ELT (arg1, 0);
2194 if (TREE_INT_CST_NUNITS (arg1) == 1)
2195 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2196 else
2197 di.high = TREE_INT_CST_ELT (arg1, 1);
2199 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2200 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2201 TYPE_SATURATING (type));
2202 t = build_fixed (type, value);
2204 /* Propagate overflow flags. */
2205 if (overflow_p | TREE_OVERFLOW (arg1))
2206 TREE_OVERFLOW (t) = 1;
2207 return t;
2210 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2211 to a fixed-point type. */
2213 static tree
2214 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2216 FIXED_VALUE_TYPE value;
2217 tree t;
2218 bool overflow_p;
2220 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2221 &TREE_REAL_CST (arg1),
2222 TYPE_SATURATING (type));
2223 t = build_fixed (type, value);
2225 /* Propagate overflow flags. */
2226 if (overflow_p | TREE_OVERFLOW (arg1))
2227 TREE_OVERFLOW (t) = 1;
2228 return t;
2231 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2232 type TYPE. If no simplification can be done return NULL_TREE. */
2234 static tree
2235 fold_convert_const (enum tree_code code, tree type, tree arg1)
2237 tree arg_type = TREE_TYPE (arg1);
2238 if (arg_type == type)
2239 return arg1;
2241 /* We can't widen types, since the runtime value could overflow the
2242 original type before being extended to the new type. */
2243 if (POLY_INT_CST_P (arg1)
2244 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2245 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2246 return build_poly_int_cst (type,
2247 poly_wide_int::from (poly_int_cst_value (arg1),
2248 TYPE_PRECISION (type),
2249 TYPE_SIGN (arg_type)));
2251 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2252 || TREE_CODE (type) == OFFSET_TYPE)
2254 if (TREE_CODE (arg1) == INTEGER_CST)
2255 return fold_convert_const_int_from_int (type, arg1);
2256 else if (TREE_CODE (arg1) == REAL_CST)
2257 return fold_convert_const_int_from_real (code, type, arg1);
2258 else if (TREE_CODE (arg1) == FIXED_CST)
2259 return fold_convert_const_int_from_fixed (type, arg1);
2261 else if (TREE_CODE (type) == REAL_TYPE)
2263 if (TREE_CODE (arg1) == INTEGER_CST)
2264 return build_real_from_int_cst (type, arg1);
2265 else if (TREE_CODE (arg1) == REAL_CST)
2266 return fold_convert_const_real_from_real (type, arg1);
2267 else if (TREE_CODE (arg1) == FIXED_CST)
2268 return fold_convert_const_real_from_fixed (type, arg1);
2270 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2272 if (TREE_CODE (arg1) == FIXED_CST)
2273 return fold_convert_const_fixed_from_fixed (type, arg1);
2274 else if (TREE_CODE (arg1) == INTEGER_CST)
2275 return fold_convert_const_fixed_from_int (type, arg1);
2276 else if (TREE_CODE (arg1) == REAL_CST)
2277 return fold_convert_const_fixed_from_real (type, arg1);
2279 else if (TREE_CODE (type) == VECTOR_TYPE)
2281 if (TREE_CODE (arg1) == VECTOR_CST
2282 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2284 tree elttype = TREE_TYPE (type);
2285 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2286 /* We can't handle steps directly when extending, since the
2287 values need to wrap at the original precision first. */
2288 bool step_ok_p
2289 = (INTEGRAL_TYPE_P (elttype)
2290 && INTEGRAL_TYPE_P (arg1_elttype)
2291 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2292 tree_vector_builder v;
2293 if (!v.new_unary_operation (type, arg1, step_ok_p))
2294 return NULL_TREE;
2295 unsigned int len = v.encoded_nelts ();
2296 for (unsigned int i = 0; i < len; ++i)
2298 tree elt = VECTOR_CST_ELT (arg1, i);
2299 tree cvt = fold_convert_const (code, elttype, elt);
2300 if (cvt == NULL_TREE)
2301 return NULL_TREE;
2302 v.quick_push (cvt);
2304 return v.build ();
2307 return NULL_TREE;
2310 /* Construct a vector of zero elements of vector type TYPE. */
2312 static tree
2313 build_zero_vector (tree type)
2315 tree t;
2317 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2318 return build_vector_from_val (type, t);
2321 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2323 bool
2324 fold_convertible_p (const_tree type, const_tree arg)
2326 tree orig = TREE_TYPE (arg);
2328 if (type == orig)
2329 return true;
2331 if (TREE_CODE (arg) == ERROR_MARK
2332 || TREE_CODE (type) == ERROR_MARK
2333 || TREE_CODE (orig) == ERROR_MARK)
2334 return false;
2336 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2337 return true;
2339 switch (TREE_CODE (type))
2341 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2342 case POINTER_TYPE: case REFERENCE_TYPE:
2343 case OFFSET_TYPE:
2344 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2345 || TREE_CODE (orig) == OFFSET_TYPE);
2347 case REAL_TYPE:
2348 case FIXED_POINT_TYPE:
2349 case VECTOR_TYPE:
2350 case VOID_TYPE:
2351 return TREE_CODE (type) == TREE_CODE (orig);
2353 default:
2354 return false;
2358 /* Convert expression ARG to type TYPE. Used by the middle-end for
2359 simple conversions in preference to calling the front-end's convert. */
2361 tree
2362 fold_convert_loc (location_t loc, tree type, tree arg)
2364 tree orig = TREE_TYPE (arg);
2365 tree tem;
2367 if (type == orig)
2368 return arg;
2370 if (TREE_CODE (arg) == ERROR_MARK
2371 || TREE_CODE (type) == ERROR_MARK
2372 || TREE_CODE (orig) == ERROR_MARK)
2373 return error_mark_node;
2375 switch (TREE_CODE (type))
2377 case POINTER_TYPE:
2378 case REFERENCE_TYPE:
2379 /* Handle conversions between pointers to different address spaces. */
2380 if (POINTER_TYPE_P (orig)
2381 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2382 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2383 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2384 /* fall through */
2386 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2387 case OFFSET_TYPE:
2388 if (TREE_CODE (arg) == INTEGER_CST)
2390 tem = fold_convert_const (NOP_EXPR, type, arg);
2391 if (tem != NULL_TREE)
2392 return tem;
2394 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2395 || TREE_CODE (orig) == OFFSET_TYPE)
2396 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2397 if (TREE_CODE (orig) == COMPLEX_TYPE)
2398 return fold_convert_loc (loc, type,
2399 fold_build1_loc (loc, REALPART_EXPR,
2400 TREE_TYPE (orig), arg));
2401 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2402 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2403 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2405 case REAL_TYPE:
2406 if (TREE_CODE (arg) == INTEGER_CST)
2408 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2409 if (tem != NULL_TREE)
2410 return tem;
2412 else if (TREE_CODE (arg) == REAL_CST)
2414 tem = fold_convert_const (NOP_EXPR, type, arg);
2415 if (tem != NULL_TREE)
2416 return tem;
2418 else if (TREE_CODE (arg) == FIXED_CST)
2420 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2421 if (tem != NULL_TREE)
2422 return tem;
2425 switch (TREE_CODE (orig))
2427 case INTEGER_TYPE:
2428 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2429 case POINTER_TYPE: case REFERENCE_TYPE:
2430 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2432 case REAL_TYPE:
2433 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2435 case FIXED_POINT_TYPE:
2436 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2438 case COMPLEX_TYPE:
2439 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2440 return fold_convert_loc (loc, type, tem);
2442 default:
2443 gcc_unreachable ();
2446 case FIXED_POINT_TYPE:
2447 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2448 || TREE_CODE (arg) == REAL_CST)
2450 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2451 if (tem != NULL_TREE)
2452 goto fold_convert_exit;
2455 switch (TREE_CODE (orig))
2457 case FIXED_POINT_TYPE:
2458 case INTEGER_TYPE:
2459 case ENUMERAL_TYPE:
2460 case BOOLEAN_TYPE:
2461 case REAL_TYPE:
2462 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2464 case COMPLEX_TYPE:
2465 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2466 return fold_convert_loc (loc, type, tem);
2468 default:
2469 gcc_unreachable ();
2472 case COMPLEX_TYPE:
2473 switch (TREE_CODE (orig))
2475 case INTEGER_TYPE:
2476 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2477 case POINTER_TYPE: case REFERENCE_TYPE:
2478 case REAL_TYPE:
2479 case FIXED_POINT_TYPE:
2480 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2481 fold_convert_loc (loc, TREE_TYPE (type), arg),
2482 fold_convert_loc (loc, TREE_TYPE (type),
2483 integer_zero_node));
2484 case COMPLEX_TYPE:
2486 tree rpart, ipart;
2488 if (TREE_CODE (arg) == COMPLEX_EXPR)
2490 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2491 TREE_OPERAND (arg, 0));
2492 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2493 TREE_OPERAND (arg, 1));
2494 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2497 arg = save_expr (arg);
2498 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2499 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2500 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2501 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2502 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2505 default:
2506 gcc_unreachable ();
2509 case VECTOR_TYPE:
2510 if (integer_zerop (arg))
2511 return build_zero_vector (type);
2512 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2513 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2514 || TREE_CODE (orig) == VECTOR_TYPE);
2515 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2517 case VOID_TYPE:
2518 tem = fold_ignored_result (arg);
2519 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2521 default:
2522 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2523 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2524 gcc_unreachable ();
2526 fold_convert_exit:
2527 protected_set_expr_location_unshare (tem, loc);
2528 return tem;
2531 /* Return false if expr can be assumed not to be an lvalue, true
2532 otherwise. */
2534 static bool
2535 maybe_lvalue_p (const_tree x)
2537 /* We only need to wrap lvalue tree codes. */
2538 switch (TREE_CODE (x))
2540 case VAR_DECL:
2541 case PARM_DECL:
2542 case RESULT_DECL:
2543 case LABEL_DECL:
2544 case FUNCTION_DECL:
2545 case SSA_NAME:
2547 case COMPONENT_REF:
2548 case MEM_REF:
2549 case INDIRECT_REF:
2550 case ARRAY_REF:
2551 case ARRAY_RANGE_REF:
2552 case BIT_FIELD_REF:
2553 case OBJ_TYPE_REF:
2555 case REALPART_EXPR:
2556 case IMAGPART_EXPR:
2557 case PREINCREMENT_EXPR:
2558 case PREDECREMENT_EXPR:
2559 case SAVE_EXPR:
2560 case TRY_CATCH_EXPR:
2561 case WITH_CLEANUP_EXPR:
2562 case COMPOUND_EXPR:
2563 case MODIFY_EXPR:
2564 case TARGET_EXPR:
2565 case COND_EXPR:
2566 case BIND_EXPR:
2567 break;
2569 default:
2570 /* Assume the worst for front-end tree codes. */
2571 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2572 break;
2573 return false;
2576 return true;
2579 /* Return an expr equal to X but certainly not valid as an lvalue. */
2581 tree
2582 non_lvalue_loc (location_t loc, tree x)
2584 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2585 us. */
2586 if (in_gimple_form)
2587 return x;
2589 if (! maybe_lvalue_p (x))
2590 return x;
2591 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2594 /* When pedantic, return an expr equal to X but certainly not valid as a
2595 pedantic lvalue. Otherwise, return X. */
2597 static tree
2598 pedantic_non_lvalue_loc (location_t loc, tree x)
2600 return protected_set_expr_location_unshare (x, loc);
2603 /* Given a tree comparison code, return the code that is the logical inverse.
2604 It is generally not safe to do this for floating-point comparisons, except
2605 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2606 ERROR_MARK in this case. */
2608 enum tree_code
2609 invert_tree_comparison (enum tree_code code, bool honor_nans)
2611 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2612 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2613 return ERROR_MARK;
2615 switch (code)
2617 case EQ_EXPR:
2618 return NE_EXPR;
2619 case NE_EXPR:
2620 return EQ_EXPR;
2621 case GT_EXPR:
2622 return honor_nans ? UNLE_EXPR : LE_EXPR;
2623 case GE_EXPR:
2624 return honor_nans ? UNLT_EXPR : LT_EXPR;
2625 case LT_EXPR:
2626 return honor_nans ? UNGE_EXPR : GE_EXPR;
2627 case LE_EXPR:
2628 return honor_nans ? UNGT_EXPR : GT_EXPR;
2629 case LTGT_EXPR:
2630 return UNEQ_EXPR;
2631 case UNEQ_EXPR:
2632 return LTGT_EXPR;
2633 case UNGT_EXPR:
2634 return LE_EXPR;
2635 case UNGE_EXPR:
2636 return LT_EXPR;
2637 case UNLT_EXPR:
2638 return GE_EXPR;
2639 case UNLE_EXPR:
2640 return GT_EXPR;
2641 case ORDERED_EXPR:
2642 return UNORDERED_EXPR;
2643 case UNORDERED_EXPR:
2644 return ORDERED_EXPR;
2645 default:
2646 gcc_unreachable ();
2650 /* Similar, but return the comparison that results if the operands are
2651 swapped. This is safe for floating-point. */
2653 enum tree_code
2654 swap_tree_comparison (enum tree_code code)
2656 switch (code)
2658 case EQ_EXPR:
2659 case NE_EXPR:
2660 case ORDERED_EXPR:
2661 case UNORDERED_EXPR:
2662 case LTGT_EXPR:
2663 case UNEQ_EXPR:
2664 return code;
2665 case GT_EXPR:
2666 return LT_EXPR;
2667 case GE_EXPR:
2668 return LE_EXPR;
2669 case LT_EXPR:
2670 return GT_EXPR;
2671 case LE_EXPR:
2672 return GE_EXPR;
2673 case UNGT_EXPR:
2674 return UNLT_EXPR;
2675 case UNGE_EXPR:
2676 return UNLE_EXPR;
2677 case UNLT_EXPR:
2678 return UNGT_EXPR;
2679 case UNLE_EXPR:
2680 return UNGE_EXPR;
2681 default:
2682 gcc_unreachable ();
2687 /* Convert a comparison tree code from an enum tree_code representation
2688 into a compcode bit-based encoding. This function is the inverse of
2689 compcode_to_comparison. */
2691 static enum comparison_code
2692 comparison_to_compcode (enum tree_code code)
2694 switch (code)
2696 case LT_EXPR:
2697 return COMPCODE_LT;
2698 case EQ_EXPR:
2699 return COMPCODE_EQ;
2700 case LE_EXPR:
2701 return COMPCODE_LE;
2702 case GT_EXPR:
2703 return COMPCODE_GT;
2704 case NE_EXPR:
2705 return COMPCODE_NE;
2706 case GE_EXPR:
2707 return COMPCODE_GE;
2708 case ORDERED_EXPR:
2709 return COMPCODE_ORD;
2710 case UNORDERED_EXPR:
2711 return COMPCODE_UNORD;
2712 case UNLT_EXPR:
2713 return COMPCODE_UNLT;
2714 case UNEQ_EXPR:
2715 return COMPCODE_UNEQ;
2716 case UNLE_EXPR:
2717 return COMPCODE_UNLE;
2718 case UNGT_EXPR:
2719 return COMPCODE_UNGT;
2720 case LTGT_EXPR:
2721 return COMPCODE_LTGT;
2722 case UNGE_EXPR:
2723 return COMPCODE_UNGE;
2724 default:
2725 gcc_unreachable ();
2729 /* Convert a compcode bit-based encoding of a comparison operator back
2730 to GCC's enum tree_code representation. This function is the
2731 inverse of comparison_to_compcode. */
2733 static enum tree_code
2734 compcode_to_comparison (enum comparison_code code)
2736 switch (code)
2738 case COMPCODE_LT:
2739 return LT_EXPR;
2740 case COMPCODE_EQ:
2741 return EQ_EXPR;
2742 case COMPCODE_LE:
2743 return LE_EXPR;
2744 case COMPCODE_GT:
2745 return GT_EXPR;
2746 case COMPCODE_NE:
2747 return NE_EXPR;
2748 case COMPCODE_GE:
2749 return GE_EXPR;
2750 case COMPCODE_ORD:
2751 return ORDERED_EXPR;
2752 case COMPCODE_UNORD:
2753 return UNORDERED_EXPR;
2754 case COMPCODE_UNLT:
2755 return UNLT_EXPR;
2756 case COMPCODE_UNEQ:
2757 return UNEQ_EXPR;
2758 case COMPCODE_UNLE:
2759 return UNLE_EXPR;
2760 case COMPCODE_UNGT:
2761 return UNGT_EXPR;
2762 case COMPCODE_LTGT:
2763 return LTGT_EXPR;
2764 case COMPCODE_UNGE:
2765 return UNGE_EXPR;
2766 default:
2767 gcc_unreachable ();
2771 /* Return a tree for the comparison which is the combination of
2772 doing the AND or OR (depending on CODE) of the two operations LCODE
2773 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2774 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2775 if this makes the transformation invalid. */
2777 tree
2778 combine_comparisons (location_t loc,
2779 enum tree_code code, enum tree_code lcode,
2780 enum tree_code rcode, tree truth_type,
2781 tree ll_arg, tree lr_arg)
2783 bool honor_nans = HONOR_NANS (ll_arg);
2784 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2785 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2786 int compcode;
2788 switch (code)
2790 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2791 compcode = lcompcode & rcompcode;
2792 break;
2794 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2795 compcode = lcompcode | rcompcode;
2796 break;
2798 default:
2799 return NULL_TREE;
2802 if (!honor_nans)
2804 /* Eliminate unordered comparisons, as well as LTGT and ORD
2805 which are not used unless the mode has NaNs. */
2806 compcode &= ~COMPCODE_UNORD;
2807 if (compcode == COMPCODE_LTGT)
2808 compcode = COMPCODE_NE;
2809 else if (compcode == COMPCODE_ORD)
2810 compcode = COMPCODE_TRUE;
2812 else if (flag_trapping_math)
2814 /* Check that the original operation and the optimized ones will trap
2815 under the same condition. */
2816 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2817 && (lcompcode != COMPCODE_EQ)
2818 && (lcompcode != COMPCODE_ORD);
2819 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2820 && (rcompcode != COMPCODE_EQ)
2821 && (rcompcode != COMPCODE_ORD);
2822 bool trap = (compcode & COMPCODE_UNORD) == 0
2823 && (compcode != COMPCODE_EQ)
2824 && (compcode != COMPCODE_ORD);
2826 /* In a short-circuited boolean expression the LHS might be
2827 such that the RHS, if evaluated, will never trap. For
2828 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2829 if neither x nor y is NaN. (This is a mixed blessing: for
2830 example, the expression above will never trap, hence
2831 optimizing it to x < y would be invalid). */
2832 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2833 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2834 rtrap = false;
2836 /* If the comparison was short-circuited, and only the RHS
2837 trapped, we may now generate a spurious trap. */
2838 if (rtrap && !ltrap
2839 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2840 return NULL_TREE;
2842 /* If we changed the conditions that cause a trap, we lose. */
2843 if ((ltrap || rtrap) != trap)
2844 return NULL_TREE;
2847 if (compcode == COMPCODE_TRUE)
2848 return constant_boolean_node (true, truth_type);
2849 else if (compcode == COMPCODE_FALSE)
2850 return constant_boolean_node (false, truth_type);
2851 else
2853 enum tree_code tcode;
2855 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2856 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2860 /* Return nonzero if two operands (typically of the same tree node)
2861 are necessarily equal. FLAGS modifies behavior as follows:
2863 If OEP_ONLY_CONST is set, only return nonzero for constants.
2864 This function tests whether the operands are indistinguishable;
2865 it does not test whether they are equal using C's == operation.
2866 The distinction is important for IEEE floating point, because
2867 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2868 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2870 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2871 even though it may hold multiple values during a function.
2872 This is because a GCC tree node guarantees that nothing else is
2873 executed between the evaluation of its "operands" (which may often
2874 be evaluated in arbitrary order). Hence if the operands themselves
2875 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2876 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2877 unset means assuming isochronic (or instantaneous) tree equivalence.
2878 Unless comparing arbitrary expression trees, such as from different
2879 statements, this flag can usually be left unset.
2881 If OEP_PURE_SAME is set, then pure functions with identical arguments
2882 are considered the same. It is used when the caller has other ways
2883 to ensure that global memory is unchanged in between.
2885 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2886 not values of expressions.
2888 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2889 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2891 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2892 any operand with side effect. This is unnecesarily conservative in the
2893 case we know that arg0 and arg1 are in disjoint code paths (such as in
2894 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2895 addresses with TREE_CONSTANT flag set so we know that &var == &var
2896 even if var is volatile. */
2899 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2901 /* When checking, verify at the outermost operand_equal_p call that
2902 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2903 hash value. */
2904 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2906 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2908 if (arg0 != arg1)
2910 inchash::hash hstate0 (0), hstate1 (0);
2911 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2912 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2913 hashval_t h0 = hstate0.end ();
2914 hashval_t h1 = hstate1.end ();
2915 gcc_assert (h0 == h1);
2917 return 1;
2919 else
2920 return 0;
2923 /* If either is ERROR_MARK, they aren't equal. */
2924 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2925 || TREE_TYPE (arg0) == error_mark_node
2926 || TREE_TYPE (arg1) == error_mark_node)
2927 return 0;
2929 /* Similar, if either does not have a type (like a released SSA name),
2930 they aren't equal. */
2931 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2932 return 0;
2934 /* We cannot consider pointers to different address space equal. */
2935 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2936 && POINTER_TYPE_P (TREE_TYPE (arg1))
2937 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2938 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2939 return 0;
2941 /* Check equality of integer constants before bailing out due to
2942 precision differences. */
2943 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2945 /* Address of INTEGER_CST is not defined; check that we did not forget
2946 to drop the OEP_ADDRESS_OF flags. */
2947 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2948 return tree_int_cst_equal (arg0, arg1);
2951 if (!(flags & OEP_ADDRESS_OF))
2953 /* If both types don't have the same signedness, then we can't consider
2954 them equal. We must check this before the STRIP_NOPS calls
2955 because they may change the signedness of the arguments. As pointers
2956 strictly don't have a signedness, require either two pointers or
2957 two non-pointers as well. */
2958 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2959 || POINTER_TYPE_P (TREE_TYPE (arg0))
2960 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2961 return 0;
2963 /* If both types don't have the same precision, then it is not safe
2964 to strip NOPs. */
2965 if (element_precision (TREE_TYPE (arg0))
2966 != element_precision (TREE_TYPE (arg1)))
2967 return 0;
2969 STRIP_NOPS (arg0);
2970 STRIP_NOPS (arg1);
2972 #if 0
2973 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2974 sanity check once the issue is solved. */
2975 else
2976 /* Addresses of conversions and SSA_NAMEs (and many other things)
2977 are not defined. Check that we did not forget to drop the
2978 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2979 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2980 && TREE_CODE (arg0) != SSA_NAME);
2981 #endif
2983 /* In case both args are comparisons but with different comparison
2984 code, try to swap the comparison operands of one arg to produce
2985 a match and compare that variant. */
2986 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2987 && COMPARISON_CLASS_P (arg0)
2988 && COMPARISON_CLASS_P (arg1))
2990 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2992 if (TREE_CODE (arg0) == swap_code)
2993 return operand_equal_p (TREE_OPERAND (arg0, 0),
2994 TREE_OPERAND (arg1, 1), flags)
2995 && operand_equal_p (TREE_OPERAND (arg0, 1),
2996 TREE_OPERAND (arg1, 0), flags);
2999 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3001 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3002 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3004 else if (flags & OEP_ADDRESS_OF)
3006 /* If we are interested in comparing addresses ignore
3007 MEM_REF wrappings of the base that can appear just for
3008 TBAA reasons. */
3009 if (TREE_CODE (arg0) == MEM_REF
3010 && DECL_P (arg1)
3011 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3012 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3013 && integer_zerop (TREE_OPERAND (arg0, 1)))
3014 return 1;
3015 else if (TREE_CODE (arg1) == MEM_REF
3016 && DECL_P (arg0)
3017 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3018 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3019 && integer_zerop (TREE_OPERAND (arg1, 1)))
3020 return 1;
3021 return 0;
3023 else
3024 return 0;
3027 /* When not checking adddresses, this is needed for conversions and for
3028 COMPONENT_REF. Might as well play it safe and always test this. */
3029 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3030 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3031 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3032 && !(flags & OEP_ADDRESS_OF)))
3033 return 0;
3035 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3036 We don't care about side effects in that case because the SAVE_EXPR
3037 takes care of that for us. In all other cases, two expressions are
3038 equal if they have no side effects. If we have two identical
3039 expressions with side effects that should be treated the same due
3040 to the only side effects being identical SAVE_EXPR's, that will
3041 be detected in the recursive calls below.
3042 If we are taking an invariant address of two identical objects
3043 they are necessarily equal as well. */
3044 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3045 && (TREE_CODE (arg0) == SAVE_EXPR
3046 || (flags & OEP_MATCH_SIDE_EFFECTS)
3047 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3048 return 1;
3050 /* Next handle constant cases, those for which we can return 1 even
3051 if ONLY_CONST is set. */
3052 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3053 switch (TREE_CODE (arg0))
3055 case INTEGER_CST:
3056 return tree_int_cst_equal (arg0, arg1);
3058 case FIXED_CST:
3059 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3060 TREE_FIXED_CST (arg1));
3062 case REAL_CST:
3063 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3064 return 1;
3067 if (!HONOR_SIGNED_ZEROS (arg0))
3069 /* If we do not distinguish between signed and unsigned zero,
3070 consider them equal. */
3071 if (real_zerop (arg0) && real_zerop (arg1))
3072 return 1;
3074 return 0;
3076 case VECTOR_CST:
3078 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3079 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3080 return 0;
3082 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3083 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3084 return 0;
3086 unsigned int count = vector_cst_encoded_nelts (arg0);
3087 for (unsigned int i = 0; i < count; ++i)
3088 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3089 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3090 return 0;
3091 return 1;
3094 case COMPLEX_CST:
3095 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3096 flags)
3097 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3098 flags));
3100 case STRING_CST:
3101 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3102 && ! memcmp (TREE_STRING_POINTER (arg0),
3103 TREE_STRING_POINTER (arg1),
3104 TREE_STRING_LENGTH (arg0)));
3106 case ADDR_EXPR:
3107 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3108 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3109 flags | OEP_ADDRESS_OF
3110 | OEP_MATCH_SIDE_EFFECTS);
3111 case CONSTRUCTOR:
3112 /* In GIMPLE empty constructors are allowed in initializers of
3113 aggregates. */
3114 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3115 default:
3116 break;
3119 if (flags & OEP_ONLY_CONST)
3120 return 0;
3122 /* Define macros to test an operand from arg0 and arg1 for equality and a
3123 variant that allows null and views null as being different from any
3124 non-null value. In the latter case, if either is null, the both
3125 must be; otherwise, do the normal comparison. */
3126 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3127 TREE_OPERAND (arg1, N), flags)
3129 #define OP_SAME_WITH_NULL(N) \
3130 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3131 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3133 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3135 case tcc_unary:
3136 /* Two conversions are equal only if signedness and modes match. */
3137 switch (TREE_CODE (arg0))
3139 CASE_CONVERT:
3140 case FIX_TRUNC_EXPR:
3141 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3142 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3143 return 0;
3144 break;
3145 default:
3146 break;
3149 return OP_SAME (0);
3152 case tcc_comparison:
3153 case tcc_binary:
3154 if (OP_SAME (0) && OP_SAME (1))
3155 return 1;
3157 /* For commutative ops, allow the other order. */
3158 return (commutative_tree_code (TREE_CODE (arg0))
3159 && operand_equal_p (TREE_OPERAND (arg0, 0),
3160 TREE_OPERAND (arg1, 1), flags)
3161 && operand_equal_p (TREE_OPERAND (arg0, 1),
3162 TREE_OPERAND (arg1, 0), flags));
3164 case tcc_reference:
3165 /* If either of the pointer (or reference) expressions we are
3166 dereferencing contain a side effect, these cannot be equal,
3167 but their addresses can be. */
3168 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3169 && (TREE_SIDE_EFFECTS (arg0)
3170 || TREE_SIDE_EFFECTS (arg1)))
3171 return 0;
3173 switch (TREE_CODE (arg0))
3175 case INDIRECT_REF:
3176 if (!(flags & OEP_ADDRESS_OF)
3177 && (TYPE_ALIGN (TREE_TYPE (arg0))
3178 != TYPE_ALIGN (TREE_TYPE (arg1))))
3179 return 0;
3180 flags &= ~OEP_ADDRESS_OF;
3181 return OP_SAME (0);
3183 case IMAGPART_EXPR:
3184 /* Require the same offset. */
3185 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3186 TYPE_SIZE (TREE_TYPE (arg1)),
3187 flags & ~OEP_ADDRESS_OF))
3188 return 0;
3190 /* Fallthru. */
3191 case REALPART_EXPR:
3192 case VIEW_CONVERT_EXPR:
3193 return OP_SAME (0);
3195 case TARGET_MEM_REF:
3196 case MEM_REF:
3197 if (!(flags & OEP_ADDRESS_OF))
3199 /* Require equal access sizes */
3200 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3201 && (!TYPE_SIZE (TREE_TYPE (arg0))
3202 || !TYPE_SIZE (TREE_TYPE (arg1))
3203 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3204 TYPE_SIZE (TREE_TYPE (arg1)),
3205 flags)))
3206 return 0;
3207 /* Verify that access happens in similar types. */
3208 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3209 return 0;
3210 /* Verify that accesses are TBAA compatible. */
3211 if (!alias_ptr_types_compatible_p
3212 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3213 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3214 || (MR_DEPENDENCE_CLIQUE (arg0)
3215 != MR_DEPENDENCE_CLIQUE (arg1))
3216 || (MR_DEPENDENCE_BASE (arg0)
3217 != MR_DEPENDENCE_BASE (arg1)))
3218 return 0;
3219 /* Verify that alignment is compatible. */
3220 if (TYPE_ALIGN (TREE_TYPE (arg0))
3221 != TYPE_ALIGN (TREE_TYPE (arg1)))
3222 return 0;
3224 flags &= ~OEP_ADDRESS_OF;
3225 return (OP_SAME (0) && OP_SAME (1)
3226 /* TARGET_MEM_REF require equal extra operands. */
3227 && (TREE_CODE (arg0) != TARGET_MEM_REF
3228 || (OP_SAME_WITH_NULL (2)
3229 && OP_SAME_WITH_NULL (3)
3230 && OP_SAME_WITH_NULL (4))));
3232 case ARRAY_REF:
3233 case ARRAY_RANGE_REF:
3234 if (!OP_SAME (0))
3235 return 0;
3236 flags &= ~OEP_ADDRESS_OF;
3237 /* Compare the array index by value if it is constant first as we
3238 may have different types but same value here. */
3239 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3240 TREE_OPERAND (arg1, 1))
3241 || OP_SAME (1))
3242 && OP_SAME_WITH_NULL (2)
3243 && OP_SAME_WITH_NULL (3)
3244 /* Compare low bound and element size as with OEP_ADDRESS_OF
3245 we have to account for the offset of the ref. */
3246 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3247 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3248 || (operand_equal_p (array_ref_low_bound
3249 (CONST_CAST_TREE (arg0)),
3250 array_ref_low_bound
3251 (CONST_CAST_TREE (arg1)), flags)
3252 && operand_equal_p (array_ref_element_size
3253 (CONST_CAST_TREE (arg0)),
3254 array_ref_element_size
3255 (CONST_CAST_TREE (arg1)),
3256 flags))));
3258 case COMPONENT_REF:
3259 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3260 may be NULL when we're called to compare MEM_EXPRs. */
3261 if (!OP_SAME_WITH_NULL (0)
3262 || !OP_SAME (1))
3263 return 0;
3264 flags &= ~OEP_ADDRESS_OF;
3265 return OP_SAME_WITH_NULL (2);
3267 case BIT_FIELD_REF:
3268 if (!OP_SAME (0))
3269 return 0;
3270 flags &= ~OEP_ADDRESS_OF;
3271 return OP_SAME (1) && OP_SAME (2);
3273 default:
3274 return 0;
3277 case tcc_expression:
3278 switch (TREE_CODE (arg0))
3280 case ADDR_EXPR:
3281 /* Be sure we pass right ADDRESS_OF flag. */
3282 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3283 return operand_equal_p (TREE_OPERAND (arg0, 0),
3284 TREE_OPERAND (arg1, 0),
3285 flags | OEP_ADDRESS_OF);
3287 case TRUTH_NOT_EXPR:
3288 return OP_SAME (0);
3290 case TRUTH_ANDIF_EXPR:
3291 case TRUTH_ORIF_EXPR:
3292 return OP_SAME (0) && OP_SAME (1);
3294 case FMA_EXPR:
3295 case WIDEN_MULT_PLUS_EXPR:
3296 case WIDEN_MULT_MINUS_EXPR:
3297 if (!OP_SAME (2))
3298 return 0;
3299 /* The multiplcation operands are commutative. */
3300 /* FALLTHRU */
3302 case TRUTH_AND_EXPR:
3303 case TRUTH_OR_EXPR:
3304 case TRUTH_XOR_EXPR:
3305 if (OP_SAME (0) && OP_SAME (1))
3306 return 1;
3308 /* Otherwise take into account this is a commutative operation. */
3309 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3310 TREE_OPERAND (arg1, 1), flags)
3311 && operand_equal_p (TREE_OPERAND (arg0, 1),
3312 TREE_OPERAND (arg1, 0), flags));
3314 case COND_EXPR:
3315 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3316 return 0;
3317 flags &= ~OEP_ADDRESS_OF;
3318 return OP_SAME (0);
3320 case BIT_INSERT_EXPR:
3321 /* BIT_INSERT_EXPR has an implict operand as the type precision
3322 of op1. Need to check to make sure they are the same. */
3323 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3324 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3325 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3326 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3327 return false;
3328 /* FALLTHRU */
3330 case VEC_COND_EXPR:
3331 case DOT_PROD_EXPR:
3332 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3334 case MODIFY_EXPR:
3335 case INIT_EXPR:
3336 case COMPOUND_EXPR:
3337 case PREDECREMENT_EXPR:
3338 case PREINCREMENT_EXPR:
3339 case POSTDECREMENT_EXPR:
3340 case POSTINCREMENT_EXPR:
3341 if (flags & OEP_LEXICOGRAPHIC)
3342 return OP_SAME (0) && OP_SAME (1);
3343 return 0;
3345 case CLEANUP_POINT_EXPR:
3346 case EXPR_STMT:
3347 if (flags & OEP_LEXICOGRAPHIC)
3348 return OP_SAME (0);
3349 return 0;
3351 default:
3352 return 0;
3355 case tcc_vl_exp:
3356 switch (TREE_CODE (arg0))
3358 case CALL_EXPR:
3359 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3360 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3361 /* If not both CALL_EXPRs are either internal or normal function
3362 functions, then they are not equal. */
3363 return 0;
3364 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3366 /* If the CALL_EXPRs call different internal functions, then they
3367 are not equal. */
3368 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3369 return 0;
3371 else
3373 /* If the CALL_EXPRs call different functions, then they are not
3374 equal. */
3375 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3376 flags))
3377 return 0;
3380 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3382 unsigned int cef = call_expr_flags (arg0);
3383 if (flags & OEP_PURE_SAME)
3384 cef &= ECF_CONST | ECF_PURE;
3385 else
3386 cef &= ECF_CONST;
3387 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3388 return 0;
3391 /* Now see if all the arguments are the same. */
3393 const_call_expr_arg_iterator iter0, iter1;
3394 const_tree a0, a1;
3395 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3396 a1 = first_const_call_expr_arg (arg1, &iter1);
3397 a0 && a1;
3398 a0 = next_const_call_expr_arg (&iter0),
3399 a1 = next_const_call_expr_arg (&iter1))
3400 if (! operand_equal_p (a0, a1, flags))
3401 return 0;
3403 /* If we get here and both argument lists are exhausted
3404 then the CALL_EXPRs are equal. */
3405 return ! (a0 || a1);
3407 default:
3408 return 0;
3411 case tcc_declaration:
3412 /* Consider __builtin_sqrt equal to sqrt. */
3413 return (TREE_CODE (arg0) == FUNCTION_DECL
3414 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3415 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3416 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3418 case tcc_exceptional:
3419 if (TREE_CODE (arg0) == CONSTRUCTOR)
3421 /* In GIMPLE constructors are used only to build vectors from
3422 elements. Individual elements in the constructor must be
3423 indexed in increasing order and form an initial sequence.
3425 We make no effort to compare constructors in generic.
3426 (see sem_variable::equals in ipa-icf which can do so for
3427 constants). */
3428 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3429 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3430 return 0;
3432 /* Be sure that vectors constructed have the same representation.
3433 We only tested element precision and modes to match.
3434 Vectors may be BLKmode and thus also check that the number of
3435 parts match. */
3436 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3437 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3438 return 0;
3440 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3441 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3442 unsigned int len = vec_safe_length (v0);
3444 if (len != vec_safe_length (v1))
3445 return 0;
3447 for (unsigned int i = 0; i < len; i++)
3449 constructor_elt *c0 = &(*v0)[i];
3450 constructor_elt *c1 = &(*v1)[i];
3452 if (!operand_equal_p (c0->value, c1->value, flags)
3453 /* In GIMPLE the indexes can be either NULL or matching i.
3454 Double check this so we won't get false
3455 positives for GENERIC. */
3456 || (c0->index
3457 && (TREE_CODE (c0->index) != INTEGER_CST
3458 || !compare_tree_int (c0->index, i)))
3459 || (c1->index
3460 && (TREE_CODE (c1->index) != INTEGER_CST
3461 || !compare_tree_int (c1->index, i))))
3462 return 0;
3464 return 1;
3466 else if (TREE_CODE (arg0) == STATEMENT_LIST
3467 && (flags & OEP_LEXICOGRAPHIC))
3469 /* Compare the STATEMENT_LISTs. */
3470 tree_stmt_iterator tsi1, tsi2;
3471 tree body1 = CONST_CAST_TREE (arg0);
3472 tree body2 = CONST_CAST_TREE (arg1);
3473 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3474 tsi_next (&tsi1), tsi_next (&tsi2))
3476 /* The lists don't have the same number of statements. */
3477 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3478 return 0;
3479 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3480 return 1;
3481 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3482 flags & (OEP_LEXICOGRAPHIC
3483 | OEP_NO_HASH_CHECK)))
3484 return 0;
3487 return 0;
3489 case tcc_statement:
3490 switch (TREE_CODE (arg0))
3492 case RETURN_EXPR:
3493 if (flags & OEP_LEXICOGRAPHIC)
3494 return OP_SAME_WITH_NULL (0);
3495 return 0;
3496 case DEBUG_BEGIN_STMT:
3497 if (flags & OEP_LEXICOGRAPHIC)
3498 return 1;
3499 return 0;
3500 default:
3501 return 0;
3504 default:
3505 return 0;
3508 #undef OP_SAME
3509 #undef OP_SAME_WITH_NULL
3512 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3513 with a different signedness or a narrower precision. */
3515 static bool
3516 operand_equal_for_comparison_p (tree arg0, tree arg1)
3518 if (operand_equal_p (arg0, arg1, 0))
3519 return true;
3521 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3522 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3523 return false;
3525 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3526 and see if the inner values are the same. This removes any
3527 signedness comparison, which doesn't matter here. */
3528 tree op0 = arg0;
3529 tree op1 = arg1;
3530 STRIP_NOPS (op0);
3531 STRIP_NOPS (op1);
3532 if (operand_equal_p (op0, op1, 0))
3533 return true;
3535 /* Discard a single widening conversion from ARG1 and see if the inner
3536 value is the same as ARG0. */
3537 if (CONVERT_EXPR_P (arg1)
3538 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3539 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3540 < TYPE_PRECISION (TREE_TYPE (arg1))
3541 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3542 return true;
3544 return false;
3547 /* See if ARG is an expression that is either a comparison or is performing
3548 arithmetic on comparisons. The comparisons must only be comparing
3549 two different values, which will be stored in *CVAL1 and *CVAL2; if
3550 they are nonzero it means that some operands have already been found.
3551 No variables may be used anywhere else in the expression except in the
3552 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3553 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3555 If this is true, return 1. Otherwise, return zero. */
3557 static int
3558 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3560 enum tree_code code = TREE_CODE (arg);
3561 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3563 /* We can handle some of the tcc_expression cases here. */
3564 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3565 tclass = tcc_unary;
3566 else if (tclass == tcc_expression
3567 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3568 || code == COMPOUND_EXPR))
3569 tclass = tcc_binary;
3571 else if (tclass == tcc_expression && code == SAVE_EXPR
3572 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3574 /* If we've already found a CVAL1 or CVAL2, this expression is
3575 two complex to handle. */
3576 if (*cval1 || *cval2)
3577 return 0;
3579 tclass = tcc_unary;
3580 *save_p = 1;
3583 switch (tclass)
3585 case tcc_unary:
3586 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3588 case tcc_binary:
3589 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3590 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3591 cval1, cval2, save_p));
3593 case tcc_constant:
3594 return 1;
3596 case tcc_expression:
3597 if (code == COND_EXPR)
3598 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3599 cval1, cval2, save_p)
3600 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3601 cval1, cval2, save_p)
3602 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3603 cval1, cval2, save_p));
3604 return 0;
3606 case tcc_comparison:
3607 /* First see if we can handle the first operand, then the second. For
3608 the second operand, we know *CVAL1 can't be zero. It must be that
3609 one side of the comparison is each of the values; test for the
3610 case where this isn't true by failing if the two operands
3611 are the same. */
3613 if (operand_equal_p (TREE_OPERAND (arg, 0),
3614 TREE_OPERAND (arg, 1), 0))
3615 return 0;
3617 if (*cval1 == 0)
3618 *cval1 = TREE_OPERAND (arg, 0);
3619 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3621 else if (*cval2 == 0)
3622 *cval2 = TREE_OPERAND (arg, 0);
3623 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3625 else
3626 return 0;
3628 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3630 else if (*cval2 == 0)
3631 *cval2 = TREE_OPERAND (arg, 1);
3632 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3634 else
3635 return 0;
3637 return 1;
3639 default:
3640 return 0;
3644 /* ARG is a tree that is known to contain just arithmetic operations and
3645 comparisons. Evaluate the operations in the tree substituting NEW0 for
3646 any occurrence of OLD0 as an operand of a comparison and likewise for
3647 NEW1 and OLD1. */
3649 static tree
3650 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3651 tree old1, tree new1)
3653 tree type = TREE_TYPE (arg);
3654 enum tree_code code = TREE_CODE (arg);
3655 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3657 /* We can handle some of the tcc_expression cases here. */
3658 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3659 tclass = tcc_unary;
3660 else if (tclass == tcc_expression
3661 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3662 tclass = tcc_binary;
3664 switch (tclass)
3666 case tcc_unary:
3667 return fold_build1_loc (loc, code, type,
3668 eval_subst (loc, TREE_OPERAND (arg, 0),
3669 old0, new0, old1, new1));
3671 case tcc_binary:
3672 return fold_build2_loc (loc, code, type,
3673 eval_subst (loc, TREE_OPERAND (arg, 0),
3674 old0, new0, old1, new1),
3675 eval_subst (loc, TREE_OPERAND (arg, 1),
3676 old0, new0, old1, new1));
3678 case tcc_expression:
3679 switch (code)
3681 case SAVE_EXPR:
3682 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3683 old1, new1);
3685 case COMPOUND_EXPR:
3686 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3687 old1, new1);
3689 case COND_EXPR:
3690 return fold_build3_loc (loc, code, type,
3691 eval_subst (loc, TREE_OPERAND (arg, 0),
3692 old0, new0, old1, new1),
3693 eval_subst (loc, TREE_OPERAND (arg, 1),
3694 old0, new0, old1, new1),
3695 eval_subst (loc, TREE_OPERAND (arg, 2),
3696 old0, new0, old1, new1));
3697 default:
3698 break;
3700 /* Fall through - ??? */
3702 case tcc_comparison:
3704 tree arg0 = TREE_OPERAND (arg, 0);
3705 tree arg1 = TREE_OPERAND (arg, 1);
3707 /* We need to check both for exact equality and tree equality. The
3708 former will be true if the operand has a side-effect. In that
3709 case, we know the operand occurred exactly once. */
3711 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3712 arg0 = new0;
3713 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3714 arg0 = new1;
3716 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3717 arg1 = new0;
3718 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3719 arg1 = new1;
3721 return fold_build2_loc (loc, code, type, arg0, arg1);
3724 default:
3725 return arg;
3729 /* Return a tree for the case when the result of an expression is RESULT
3730 converted to TYPE and OMITTED was previously an operand of the expression
3731 but is now not needed (e.g., we folded OMITTED * 0).
3733 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3734 the conversion of RESULT to TYPE. */
3736 tree
3737 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3739 tree t = fold_convert_loc (loc, type, result);
3741 /* If the resulting operand is an empty statement, just return the omitted
3742 statement casted to void. */
3743 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3744 return build1_loc (loc, NOP_EXPR, void_type_node,
3745 fold_ignored_result (omitted));
3747 if (TREE_SIDE_EFFECTS (omitted))
3748 return build2_loc (loc, COMPOUND_EXPR, type,
3749 fold_ignored_result (omitted), t);
3751 return non_lvalue_loc (loc, t);
3754 /* Return a tree for the case when the result of an expression is RESULT
3755 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3756 of the expression but are now not needed.
3758 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3759 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3760 evaluated before OMITTED2. Otherwise, if neither has side effects,
3761 just do the conversion of RESULT to TYPE. */
3763 tree
3764 omit_two_operands_loc (location_t loc, tree type, tree result,
3765 tree omitted1, tree omitted2)
3767 tree t = fold_convert_loc (loc, type, result);
3769 if (TREE_SIDE_EFFECTS (omitted2))
3770 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3771 if (TREE_SIDE_EFFECTS (omitted1))
3772 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3774 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3778 /* Return a simplified tree node for the truth-negation of ARG. This
3779 never alters ARG itself. We assume that ARG is an operation that
3780 returns a truth value (0 or 1).
3782 FIXME: one would think we would fold the result, but it causes
3783 problems with the dominator optimizer. */
3785 static tree
3786 fold_truth_not_expr (location_t loc, tree arg)
3788 tree type = TREE_TYPE (arg);
3789 enum tree_code code = TREE_CODE (arg);
3790 location_t loc1, loc2;
3792 /* If this is a comparison, we can simply invert it, except for
3793 floating-point non-equality comparisons, in which case we just
3794 enclose a TRUTH_NOT_EXPR around what we have. */
3796 if (TREE_CODE_CLASS (code) == tcc_comparison)
3798 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3799 if (FLOAT_TYPE_P (op_type)
3800 && flag_trapping_math
3801 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3802 && code != NE_EXPR && code != EQ_EXPR)
3803 return NULL_TREE;
3805 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3806 if (code == ERROR_MARK)
3807 return NULL_TREE;
3809 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3810 TREE_OPERAND (arg, 1));
3811 if (TREE_NO_WARNING (arg))
3812 TREE_NO_WARNING (ret) = 1;
3813 return ret;
3816 switch (code)
3818 case INTEGER_CST:
3819 return constant_boolean_node (integer_zerop (arg), type);
3821 case TRUTH_AND_EXPR:
3822 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3823 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3824 return build2_loc (loc, TRUTH_OR_EXPR, type,
3825 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3826 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3828 case TRUTH_OR_EXPR:
3829 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3830 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3831 return build2_loc (loc, TRUTH_AND_EXPR, type,
3832 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3833 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3835 case TRUTH_XOR_EXPR:
3836 /* Here we can invert either operand. We invert the first operand
3837 unless the second operand is a TRUTH_NOT_EXPR in which case our
3838 result is the XOR of the first operand with the inside of the
3839 negation of the second operand. */
3841 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3842 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3843 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3844 else
3845 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3846 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3847 TREE_OPERAND (arg, 1));
3849 case TRUTH_ANDIF_EXPR:
3850 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3851 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3852 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3853 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3854 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3856 case TRUTH_ORIF_EXPR:
3857 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3858 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3859 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3860 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3861 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3863 case TRUTH_NOT_EXPR:
3864 return TREE_OPERAND (arg, 0);
3866 case COND_EXPR:
3868 tree arg1 = TREE_OPERAND (arg, 1);
3869 tree arg2 = TREE_OPERAND (arg, 2);
3871 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3872 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3874 /* A COND_EXPR may have a throw as one operand, which
3875 then has void type. Just leave void operands
3876 as they are. */
3877 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3878 VOID_TYPE_P (TREE_TYPE (arg1))
3879 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3880 VOID_TYPE_P (TREE_TYPE (arg2))
3881 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3884 case COMPOUND_EXPR:
3885 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3886 return build2_loc (loc, COMPOUND_EXPR, type,
3887 TREE_OPERAND (arg, 0),
3888 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3890 case NON_LVALUE_EXPR:
3891 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3892 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3894 CASE_CONVERT:
3895 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3896 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3898 /* fall through */
3900 case FLOAT_EXPR:
3901 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3902 return build1_loc (loc, TREE_CODE (arg), type,
3903 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3905 case BIT_AND_EXPR:
3906 if (!integer_onep (TREE_OPERAND (arg, 1)))
3907 return NULL_TREE;
3908 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3910 case SAVE_EXPR:
3911 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3913 case CLEANUP_POINT_EXPR:
3914 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3915 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3916 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3918 default:
3919 return NULL_TREE;
3923 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3924 assume that ARG is an operation that returns a truth value (0 or 1
3925 for scalars, 0 or -1 for vectors). Return the folded expression if
3926 folding is successful. Otherwise, return NULL_TREE. */
3928 static tree
3929 fold_invert_truthvalue (location_t loc, tree arg)
3931 tree type = TREE_TYPE (arg);
3932 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3933 ? BIT_NOT_EXPR
3934 : TRUTH_NOT_EXPR,
3935 type, arg);
3938 /* Return a simplified tree node for the truth-negation of ARG. This
3939 never alters ARG itself. We assume that ARG is an operation that
3940 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3942 tree
3943 invert_truthvalue_loc (location_t loc, tree arg)
3945 if (TREE_CODE (arg) == ERROR_MARK)
3946 return arg;
3948 tree type = TREE_TYPE (arg);
3949 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3950 ? BIT_NOT_EXPR
3951 : TRUTH_NOT_EXPR,
3952 type, arg);
3955 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3956 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3957 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3958 is the original memory reference used to preserve the alias set of
3959 the access. */
3961 static tree
3962 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3963 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3964 int unsignedp, int reversep)
3966 tree result, bftype;
3968 /* Attempt not to lose the access path if possible. */
3969 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3971 tree ninner = TREE_OPERAND (orig_inner, 0);
3972 machine_mode nmode;
3973 poly_int64 nbitsize, nbitpos;
3974 tree noffset;
3975 int nunsignedp, nreversep, nvolatilep = 0;
3976 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3977 &noffset, &nmode, &nunsignedp,
3978 &nreversep, &nvolatilep);
3979 if (base == inner
3980 && noffset == NULL_TREE
3981 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3982 && !reversep
3983 && !nreversep
3984 && !nvolatilep)
3986 inner = ninner;
3987 bitpos -= nbitpos;
3991 alias_set_type iset = get_alias_set (orig_inner);
3992 if (iset == 0 && get_alias_set (inner) != iset)
3993 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3994 build_fold_addr_expr (inner),
3995 build_int_cst (ptr_type_node, 0));
3997 if (known_eq (bitpos, 0) && !reversep)
3999 tree size = TYPE_SIZE (TREE_TYPE (inner));
4000 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4001 || POINTER_TYPE_P (TREE_TYPE (inner)))
4002 && tree_fits_shwi_p (size)
4003 && tree_to_shwi (size) == bitsize)
4004 return fold_convert_loc (loc, type, inner);
4007 bftype = type;
4008 if (TYPE_PRECISION (bftype) != bitsize
4009 || TYPE_UNSIGNED (bftype) == !unsignedp)
4010 bftype = build_nonstandard_integer_type (bitsize, 0);
4012 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4013 bitsize_int (bitsize), bitsize_int (bitpos));
4014 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4016 if (bftype != type)
4017 result = fold_convert_loc (loc, type, result);
4019 return result;
4022 /* Optimize a bit-field compare.
4024 There are two cases: First is a compare against a constant and the
4025 second is a comparison of two items where the fields are at the same
4026 bit position relative to the start of a chunk (byte, halfword, word)
4027 large enough to contain it. In these cases we can avoid the shift
4028 implicit in bitfield extractions.
4030 For constants, we emit a compare of the shifted constant with the
4031 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4032 compared. For two fields at the same position, we do the ANDs with the
4033 similar mask and compare the result of the ANDs.
4035 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4036 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4037 are the left and right operands of the comparison, respectively.
4039 If the optimization described above can be done, we return the resulting
4040 tree. Otherwise we return zero. */
4042 static tree
4043 optimize_bit_field_compare (location_t loc, enum tree_code code,
4044 tree compare_type, tree lhs, tree rhs)
4046 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4047 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4048 tree type = TREE_TYPE (lhs);
4049 tree unsigned_type;
4050 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4051 machine_mode lmode, rmode;
4052 scalar_int_mode nmode;
4053 int lunsignedp, runsignedp;
4054 int lreversep, rreversep;
4055 int lvolatilep = 0, rvolatilep = 0;
4056 tree linner, rinner = NULL_TREE;
4057 tree mask;
4058 tree offset;
4060 /* Get all the information about the extractions being done. If the bit size
4061 is the same as the size of the underlying object, we aren't doing an
4062 extraction at all and so can do nothing. We also don't want to
4063 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4064 then will no longer be able to replace it. */
4065 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4066 &lunsignedp, &lreversep, &lvolatilep);
4067 if (linner == lhs
4068 || !known_size_p (plbitsize)
4069 || !plbitsize.is_constant (&lbitsize)
4070 || !plbitpos.is_constant (&lbitpos)
4071 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4072 || offset != 0
4073 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4074 || lvolatilep)
4075 return 0;
4077 if (const_p)
4078 rreversep = lreversep;
4079 else
4081 /* If this is not a constant, we can only do something if bit positions,
4082 sizes, signedness and storage order are the same. */
4083 rinner
4084 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4085 &runsignedp, &rreversep, &rvolatilep);
4087 if (rinner == rhs
4088 || maybe_ne (lbitpos, rbitpos)
4089 || maybe_ne (lbitsize, rbitsize)
4090 || lunsignedp != runsignedp
4091 || lreversep != rreversep
4092 || offset != 0
4093 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4094 || rvolatilep)
4095 return 0;
4098 /* Honor the C++ memory model and mimic what RTL expansion does. */
4099 poly_uint64 bitstart = 0;
4100 poly_uint64 bitend = 0;
4101 if (TREE_CODE (lhs) == COMPONENT_REF)
4103 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4104 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4105 return 0;
4108 /* See if we can find a mode to refer to this field. We should be able to,
4109 but fail if we can't. */
4110 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4111 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4112 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4113 TYPE_ALIGN (TREE_TYPE (rinner))),
4114 BITS_PER_WORD, false, &nmode))
4115 return 0;
4117 /* Set signed and unsigned types of the precision of this mode for the
4118 shifts below. */
4119 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4121 /* Compute the bit position and size for the new reference and our offset
4122 within it. If the new reference is the same size as the original, we
4123 won't optimize anything, so return zero. */
4124 nbitsize = GET_MODE_BITSIZE (nmode);
4125 nbitpos = lbitpos & ~ (nbitsize - 1);
4126 lbitpos -= nbitpos;
4127 if (nbitsize == lbitsize)
4128 return 0;
4130 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4131 lbitpos = nbitsize - lbitsize - lbitpos;
4133 /* Make the mask to be used against the extracted field. */
4134 mask = build_int_cst_type (unsigned_type, -1);
4135 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4136 mask = const_binop (RSHIFT_EXPR, mask,
4137 size_int (nbitsize - lbitsize - lbitpos));
4139 if (! const_p)
4141 if (nbitpos < 0)
4142 return 0;
4144 /* If not comparing with constant, just rework the comparison
4145 and return. */
4146 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4147 nbitsize, nbitpos, 1, lreversep);
4148 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4149 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4150 nbitsize, nbitpos, 1, rreversep);
4151 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4152 return fold_build2_loc (loc, code, compare_type, t1, t2);
4155 /* Otherwise, we are handling the constant case. See if the constant is too
4156 big for the field. Warn and return a tree for 0 (false) if so. We do
4157 this not only for its own sake, but to avoid having to test for this
4158 error case below. If we didn't, we might generate wrong code.
4160 For unsigned fields, the constant shifted right by the field length should
4161 be all zero. For signed fields, the high-order bits should agree with
4162 the sign bit. */
4164 if (lunsignedp)
4166 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4168 warning (0, "comparison is always %d due to width of bit-field",
4169 code == NE_EXPR);
4170 return constant_boolean_node (code == NE_EXPR, compare_type);
4173 else
4175 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4176 if (tem != 0 && tem != -1)
4178 warning (0, "comparison is always %d due to width of bit-field",
4179 code == NE_EXPR);
4180 return constant_boolean_node (code == NE_EXPR, compare_type);
4184 if (nbitpos < 0)
4185 return 0;
4187 /* Single-bit compares should always be against zero. */
4188 if (lbitsize == 1 && ! integer_zerop (rhs))
4190 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4191 rhs = build_int_cst (type, 0);
4194 /* Make a new bitfield reference, shift the constant over the
4195 appropriate number of bits and mask it with the computed mask
4196 (in case this was a signed field). If we changed it, make a new one. */
4197 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4198 nbitsize, nbitpos, 1, lreversep);
4200 rhs = const_binop (BIT_AND_EXPR,
4201 const_binop (LSHIFT_EXPR,
4202 fold_convert_loc (loc, unsigned_type, rhs),
4203 size_int (lbitpos)),
4204 mask);
4206 lhs = build2_loc (loc, code, compare_type,
4207 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4208 return lhs;
4211 /* Subroutine for fold_truth_andor_1: decode a field reference.
4213 If EXP is a comparison reference, we return the innermost reference.
4215 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4216 set to the starting bit number.
4218 If the innermost field can be completely contained in a mode-sized
4219 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4221 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4222 otherwise it is not changed.
4224 *PUNSIGNEDP is set to the signedness of the field.
4226 *PREVERSEP is set to the storage order of the field.
4228 *PMASK is set to the mask used. This is either contained in a
4229 BIT_AND_EXPR or derived from the width of the field.
4231 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4233 Return 0 if this is not a component reference or is one that we can't
4234 do anything with. */
4236 static tree
4237 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4238 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4239 int *punsignedp, int *preversep, int *pvolatilep,
4240 tree *pmask, tree *pand_mask)
4242 tree exp = *exp_;
4243 tree outer_type = 0;
4244 tree and_mask = 0;
4245 tree mask, inner, offset;
4246 tree unsigned_type;
4247 unsigned int precision;
4249 /* All the optimizations using this function assume integer fields.
4250 There are problems with FP fields since the type_for_size call
4251 below can fail for, e.g., XFmode. */
4252 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4253 return 0;
4255 /* We are interested in the bare arrangement of bits, so strip everything
4256 that doesn't affect the machine mode. However, record the type of the
4257 outermost expression if it may matter below. */
4258 if (CONVERT_EXPR_P (exp)
4259 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4260 outer_type = TREE_TYPE (exp);
4261 STRIP_NOPS (exp);
4263 if (TREE_CODE (exp) == BIT_AND_EXPR)
4265 and_mask = TREE_OPERAND (exp, 1);
4266 exp = TREE_OPERAND (exp, 0);
4267 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4268 if (TREE_CODE (and_mask) != INTEGER_CST)
4269 return 0;
4272 poly_int64 poly_bitsize, poly_bitpos;
4273 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4274 pmode, punsignedp, preversep, pvolatilep);
4275 if ((inner == exp && and_mask == 0)
4276 || !poly_bitsize.is_constant (pbitsize)
4277 || !poly_bitpos.is_constant (pbitpos)
4278 || *pbitsize < 0
4279 || offset != 0
4280 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4281 /* Reject out-of-bound accesses (PR79731). */
4282 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4283 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4284 *pbitpos + *pbitsize) < 0))
4285 return 0;
4287 *exp_ = exp;
4289 /* If the number of bits in the reference is the same as the bitsize of
4290 the outer type, then the outer type gives the signedness. Otherwise
4291 (in case of a small bitfield) the signedness is unchanged. */
4292 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4293 *punsignedp = TYPE_UNSIGNED (outer_type);
4295 /* Compute the mask to access the bitfield. */
4296 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4297 precision = TYPE_PRECISION (unsigned_type);
4299 mask = build_int_cst_type (unsigned_type, -1);
4301 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4302 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4304 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4305 if (and_mask != 0)
4306 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4307 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4309 *pmask = mask;
4310 *pand_mask = and_mask;
4311 return inner;
4314 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4315 bit positions and MASK is SIGNED. */
4317 static int
4318 all_ones_mask_p (const_tree mask, unsigned int size)
4320 tree type = TREE_TYPE (mask);
4321 unsigned int precision = TYPE_PRECISION (type);
4323 /* If this function returns true when the type of the mask is
4324 UNSIGNED, then there will be errors. In particular see
4325 gcc.c-torture/execute/990326-1.c. There does not appear to be
4326 any documentation paper trail as to why this is so. But the pre
4327 wide-int worked with that restriction and it has been preserved
4328 here. */
4329 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4330 return false;
4332 return wi::mask (size, false, precision) == wi::to_wide (mask);
4335 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4336 represents the sign bit of EXP's type. If EXP represents a sign
4337 or zero extension, also test VAL against the unextended type.
4338 The return value is the (sub)expression whose sign bit is VAL,
4339 or NULL_TREE otherwise. */
4341 tree
4342 sign_bit_p (tree exp, const_tree val)
4344 int width;
4345 tree t;
4347 /* Tree EXP must have an integral type. */
4348 t = TREE_TYPE (exp);
4349 if (! INTEGRAL_TYPE_P (t))
4350 return NULL_TREE;
4352 /* Tree VAL must be an integer constant. */
4353 if (TREE_CODE (val) != INTEGER_CST
4354 || TREE_OVERFLOW (val))
4355 return NULL_TREE;
4357 width = TYPE_PRECISION (t);
4358 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4359 return exp;
4361 /* Handle extension from a narrower type. */
4362 if (TREE_CODE (exp) == NOP_EXPR
4363 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4364 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4366 return NULL_TREE;
4369 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4370 to be evaluated unconditionally. */
4372 static int
4373 simple_operand_p (const_tree exp)
4375 /* Strip any conversions that don't change the machine mode. */
4376 STRIP_NOPS (exp);
4378 return (CONSTANT_CLASS_P (exp)
4379 || TREE_CODE (exp) == SSA_NAME
4380 || (DECL_P (exp)
4381 && ! TREE_ADDRESSABLE (exp)
4382 && ! TREE_THIS_VOLATILE (exp)
4383 && ! DECL_NONLOCAL (exp)
4384 /* Don't regard global variables as simple. They may be
4385 allocated in ways unknown to the compiler (shared memory,
4386 #pragma weak, etc). */
4387 && ! TREE_PUBLIC (exp)
4388 && ! DECL_EXTERNAL (exp)
4389 /* Weakrefs are not safe to be read, since they can be NULL.
4390 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4391 have DECL_WEAK flag set. */
4392 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4393 /* Loading a static variable is unduly expensive, but global
4394 registers aren't expensive. */
4395 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4398 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4399 to be evaluated unconditionally.
4400 I addition to simple_operand_p, we assume that comparisons, conversions,
4401 and logic-not operations are simple, if their operands are simple, too. */
4403 static bool
4404 simple_operand_p_2 (tree exp)
4406 enum tree_code code;
4408 if (TREE_SIDE_EFFECTS (exp)
4409 || tree_could_trap_p (exp))
4410 return false;
4412 while (CONVERT_EXPR_P (exp))
4413 exp = TREE_OPERAND (exp, 0);
4415 code = TREE_CODE (exp);
4417 if (TREE_CODE_CLASS (code) == tcc_comparison)
4418 return (simple_operand_p (TREE_OPERAND (exp, 0))
4419 && simple_operand_p (TREE_OPERAND (exp, 1)));
4421 if (code == TRUTH_NOT_EXPR)
4422 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4424 return simple_operand_p (exp);
4428 /* The following functions are subroutines to fold_range_test and allow it to
4429 try to change a logical combination of comparisons into a range test.
4431 For example, both
4432 X == 2 || X == 3 || X == 4 || X == 5
4434 X >= 2 && X <= 5
4435 are converted to
4436 (unsigned) (X - 2) <= 3
4438 We describe each set of comparisons as being either inside or outside
4439 a range, using a variable named like IN_P, and then describe the
4440 range with a lower and upper bound. If one of the bounds is omitted,
4441 it represents either the highest or lowest value of the type.
4443 In the comments below, we represent a range by two numbers in brackets
4444 preceded by a "+" to designate being inside that range, or a "-" to
4445 designate being outside that range, so the condition can be inverted by
4446 flipping the prefix. An omitted bound is represented by a "-". For
4447 example, "- [-, 10]" means being outside the range starting at the lowest
4448 possible value and ending at 10, in other words, being greater than 10.
4449 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4450 always false.
4452 We set up things so that the missing bounds are handled in a consistent
4453 manner so neither a missing bound nor "true" and "false" need to be
4454 handled using a special case. */
4456 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4457 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4458 and UPPER1_P are nonzero if the respective argument is an upper bound
4459 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4460 must be specified for a comparison. ARG1 will be converted to ARG0's
4461 type if both are specified. */
4463 static tree
4464 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4465 tree arg1, int upper1_p)
4467 tree tem;
4468 int result;
4469 int sgn0, sgn1;
4471 /* If neither arg represents infinity, do the normal operation.
4472 Else, if not a comparison, return infinity. Else handle the special
4473 comparison rules. Note that most of the cases below won't occur, but
4474 are handled for consistency. */
4476 if (arg0 != 0 && arg1 != 0)
4478 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4479 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4480 STRIP_NOPS (tem);
4481 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4484 if (TREE_CODE_CLASS (code) != tcc_comparison)
4485 return 0;
4487 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4488 for neither. In real maths, we cannot assume open ended ranges are
4489 the same. But, this is computer arithmetic, where numbers are finite.
4490 We can therefore make the transformation of any unbounded range with
4491 the value Z, Z being greater than any representable number. This permits
4492 us to treat unbounded ranges as equal. */
4493 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4494 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4495 switch (code)
4497 case EQ_EXPR:
4498 result = sgn0 == sgn1;
4499 break;
4500 case NE_EXPR:
4501 result = sgn0 != sgn1;
4502 break;
4503 case LT_EXPR:
4504 result = sgn0 < sgn1;
4505 break;
4506 case LE_EXPR:
4507 result = sgn0 <= sgn1;
4508 break;
4509 case GT_EXPR:
4510 result = sgn0 > sgn1;
4511 break;
4512 case GE_EXPR:
4513 result = sgn0 >= sgn1;
4514 break;
4515 default:
4516 gcc_unreachable ();
4519 return constant_boolean_node (result, type);
4522 /* Helper routine for make_range. Perform one step for it, return
4523 new expression if the loop should continue or NULL_TREE if it should
4524 stop. */
4526 tree
4527 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4528 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4529 bool *strict_overflow_p)
4531 tree arg0_type = TREE_TYPE (arg0);
4532 tree n_low, n_high, low = *p_low, high = *p_high;
4533 int in_p = *p_in_p, n_in_p;
4535 switch (code)
4537 case TRUTH_NOT_EXPR:
4538 /* We can only do something if the range is testing for zero. */
4539 if (low == NULL_TREE || high == NULL_TREE
4540 || ! integer_zerop (low) || ! integer_zerop (high))
4541 return NULL_TREE;
4542 *p_in_p = ! in_p;
4543 return arg0;
4545 case EQ_EXPR: case NE_EXPR:
4546 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4547 /* We can only do something if the range is testing for zero
4548 and if the second operand is an integer constant. Note that
4549 saying something is "in" the range we make is done by
4550 complementing IN_P since it will set in the initial case of
4551 being not equal to zero; "out" is leaving it alone. */
4552 if (low == NULL_TREE || high == NULL_TREE
4553 || ! integer_zerop (low) || ! integer_zerop (high)
4554 || TREE_CODE (arg1) != INTEGER_CST)
4555 return NULL_TREE;
4557 switch (code)
4559 case NE_EXPR: /* - [c, c] */
4560 low = high = arg1;
4561 break;
4562 case EQ_EXPR: /* + [c, c] */
4563 in_p = ! in_p, low = high = arg1;
4564 break;
4565 case GT_EXPR: /* - [-, c] */
4566 low = 0, high = arg1;
4567 break;
4568 case GE_EXPR: /* + [c, -] */
4569 in_p = ! in_p, low = arg1, high = 0;
4570 break;
4571 case LT_EXPR: /* - [c, -] */
4572 low = arg1, high = 0;
4573 break;
4574 case LE_EXPR: /* + [-, c] */
4575 in_p = ! in_p, low = 0, high = arg1;
4576 break;
4577 default:
4578 gcc_unreachable ();
4581 /* If this is an unsigned comparison, we also know that EXP is
4582 greater than or equal to zero. We base the range tests we make
4583 on that fact, so we record it here so we can parse existing
4584 range tests. We test arg0_type since often the return type
4585 of, e.g. EQ_EXPR, is boolean. */
4586 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4588 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4589 in_p, low, high, 1,
4590 build_int_cst (arg0_type, 0),
4591 NULL_TREE))
4592 return NULL_TREE;
4594 in_p = n_in_p, low = n_low, high = n_high;
4596 /* If the high bound is missing, but we have a nonzero low
4597 bound, reverse the range so it goes from zero to the low bound
4598 minus 1. */
4599 if (high == 0 && low && ! integer_zerop (low))
4601 in_p = ! in_p;
4602 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4603 build_int_cst (TREE_TYPE (low), 1), 0);
4604 low = build_int_cst (arg0_type, 0);
4608 *p_low = low;
4609 *p_high = high;
4610 *p_in_p = in_p;
4611 return arg0;
4613 case NEGATE_EXPR:
4614 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4615 low and high are non-NULL, then normalize will DTRT. */
4616 if (!TYPE_UNSIGNED (arg0_type)
4617 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4619 if (low == NULL_TREE)
4620 low = TYPE_MIN_VALUE (arg0_type);
4621 if (high == NULL_TREE)
4622 high = TYPE_MAX_VALUE (arg0_type);
4625 /* (-x) IN [a,b] -> x in [-b, -a] */
4626 n_low = range_binop (MINUS_EXPR, exp_type,
4627 build_int_cst (exp_type, 0),
4628 0, high, 1);
4629 n_high = range_binop (MINUS_EXPR, exp_type,
4630 build_int_cst (exp_type, 0),
4631 0, low, 0);
4632 if (n_high != 0 && TREE_OVERFLOW (n_high))
4633 return NULL_TREE;
4634 goto normalize;
4636 case BIT_NOT_EXPR:
4637 /* ~ X -> -X - 1 */
4638 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4639 build_int_cst (exp_type, 1));
4641 case PLUS_EXPR:
4642 case MINUS_EXPR:
4643 if (TREE_CODE (arg1) != INTEGER_CST)
4644 return NULL_TREE;
4646 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4647 move a constant to the other side. */
4648 if (!TYPE_UNSIGNED (arg0_type)
4649 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4650 return NULL_TREE;
4652 /* If EXP is signed, any overflow in the computation is undefined,
4653 so we don't worry about it so long as our computations on
4654 the bounds don't overflow. For unsigned, overflow is defined
4655 and this is exactly the right thing. */
4656 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4657 arg0_type, low, 0, arg1, 0);
4658 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4659 arg0_type, high, 1, arg1, 0);
4660 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4661 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4662 return NULL_TREE;
4664 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4665 *strict_overflow_p = true;
4667 normalize:
4668 /* Check for an unsigned range which has wrapped around the maximum
4669 value thus making n_high < n_low, and normalize it. */
4670 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4672 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4673 build_int_cst (TREE_TYPE (n_high), 1), 0);
4674 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4675 build_int_cst (TREE_TYPE (n_low), 1), 0);
4677 /* If the range is of the form +/- [ x+1, x ], we won't
4678 be able to normalize it. But then, it represents the
4679 whole range or the empty set, so make it
4680 +/- [ -, - ]. */
4681 if (tree_int_cst_equal (n_low, low)
4682 && tree_int_cst_equal (n_high, high))
4683 low = high = 0;
4684 else
4685 in_p = ! in_p;
4687 else
4688 low = n_low, high = n_high;
4690 *p_low = low;
4691 *p_high = high;
4692 *p_in_p = in_p;
4693 return arg0;
4695 CASE_CONVERT:
4696 case NON_LVALUE_EXPR:
4697 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4698 return NULL_TREE;
4700 if (! INTEGRAL_TYPE_P (arg0_type)
4701 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4702 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4703 return NULL_TREE;
4705 n_low = low, n_high = high;
4707 if (n_low != 0)
4708 n_low = fold_convert_loc (loc, arg0_type, n_low);
4710 if (n_high != 0)
4711 n_high = fold_convert_loc (loc, arg0_type, n_high);
4713 /* If we're converting arg0 from an unsigned type, to exp,
4714 a signed type, we will be doing the comparison as unsigned.
4715 The tests above have already verified that LOW and HIGH
4716 are both positive.
4718 So we have to ensure that we will handle large unsigned
4719 values the same way that the current signed bounds treat
4720 negative values. */
4722 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4724 tree high_positive;
4725 tree equiv_type;
4726 /* For fixed-point modes, we need to pass the saturating flag
4727 as the 2nd parameter. */
4728 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4729 equiv_type
4730 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4731 TYPE_SATURATING (arg0_type));
4732 else
4733 equiv_type
4734 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4736 /* A range without an upper bound is, naturally, unbounded.
4737 Since convert would have cropped a very large value, use
4738 the max value for the destination type. */
4739 high_positive
4740 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4741 : TYPE_MAX_VALUE (arg0_type);
4743 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4744 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4745 fold_convert_loc (loc, arg0_type,
4746 high_positive),
4747 build_int_cst (arg0_type, 1));
4749 /* If the low bound is specified, "and" the range with the
4750 range for which the original unsigned value will be
4751 positive. */
4752 if (low != 0)
4754 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4755 1, fold_convert_loc (loc, arg0_type,
4756 integer_zero_node),
4757 high_positive))
4758 return NULL_TREE;
4760 in_p = (n_in_p == in_p);
4762 else
4764 /* Otherwise, "or" the range with the range of the input
4765 that will be interpreted as negative. */
4766 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4767 1, fold_convert_loc (loc, arg0_type,
4768 integer_zero_node),
4769 high_positive))
4770 return NULL_TREE;
4772 in_p = (in_p != n_in_p);
4776 *p_low = n_low;
4777 *p_high = n_high;
4778 *p_in_p = in_p;
4779 return arg0;
4781 default:
4782 return NULL_TREE;
4786 /* Given EXP, a logical expression, set the range it is testing into
4787 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4788 actually being tested. *PLOW and *PHIGH will be made of the same
4789 type as the returned expression. If EXP is not a comparison, we
4790 will most likely not be returning a useful value and range. Set
4791 *STRICT_OVERFLOW_P to true if the return value is only valid
4792 because signed overflow is undefined; otherwise, do not change
4793 *STRICT_OVERFLOW_P. */
4795 tree
4796 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4797 bool *strict_overflow_p)
4799 enum tree_code code;
4800 tree arg0, arg1 = NULL_TREE;
4801 tree exp_type, nexp;
4802 int in_p;
4803 tree low, high;
4804 location_t loc = EXPR_LOCATION (exp);
4806 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4807 and see if we can refine the range. Some of the cases below may not
4808 happen, but it doesn't seem worth worrying about this. We "continue"
4809 the outer loop when we've changed something; otherwise we "break"
4810 the switch, which will "break" the while. */
4812 in_p = 0;
4813 low = high = build_int_cst (TREE_TYPE (exp), 0);
4815 while (1)
4817 code = TREE_CODE (exp);
4818 exp_type = TREE_TYPE (exp);
4819 arg0 = NULL_TREE;
4821 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4823 if (TREE_OPERAND_LENGTH (exp) > 0)
4824 arg0 = TREE_OPERAND (exp, 0);
4825 if (TREE_CODE_CLASS (code) == tcc_binary
4826 || TREE_CODE_CLASS (code) == tcc_comparison
4827 || (TREE_CODE_CLASS (code) == tcc_expression
4828 && TREE_OPERAND_LENGTH (exp) > 1))
4829 arg1 = TREE_OPERAND (exp, 1);
4831 if (arg0 == NULL_TREE)
4832 break;
4834 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4835 &high, &in_p, strict_overflow_p);
4836 if (nexp == NULL_TREE)
4837 break;
4838 exp = nexp;
4841 /* If EXP is a constant, we can evaluate whether this is true or false. */
4842 if (TREE_CODE (exp) == INTEGER_CST)
4844 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4845 exp, 0, low, 0))
4846 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4847 exp, 1, high, 1)));
4848 low = high = 0;
4849 exp = 0;
4852 *pin_p = in_p, *plow = low, *phigh = high;
4853 return exp;
4856 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4857 a bitwise check i.e. when
4858 LOW == 0xXX...X00...0
4859 HIGH == 0xXX...X11...1
4860 Return corresponding mask in MASK and stem in VALUE. */
4862 static bool
4863 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4864 tree *value)
4866 if (TREE_CODE (low) != INTEGER_CST
4867 || TREE_CODE (high) != INTEGER_CST)
4868 return false;
4870 unsigned prec = TYPE_PRECISION (type);
4871 wide_int lo = wi::to_wide (low, prec);
4872 wide_int hi = wi::to_wide (high, prec);
4874 wide_int end_mask = lo ^ hi;
4875 if ((end_mask & (end_mask + 1)) != 0
4876 || (lo & end_mask) != 0)
4877 return false;
4879 wide_int stem_mask = ~end_mask;
4880 wide_int stem = lo & stem_mask;
4881 if (stem != (hi & stem_mask))
4882 return false;
4884 *mask = wide_int_to_tree (type, stem_mask);
4885 *value = wide_int_to_tree (type, stem);
4887 return true;
4890 /* Helper routine for build_range_check and match.pd. Return the type to
4891 perform the check or NULL if it shouldn't be optimized. */
4893 tree
4894 range_check_type (tree etype)
4896 /* First make sure that arithmetics in this type is valid, then make sure
4897 that it wraps around. */
4898 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4899 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4900 TYPE_UNSIGNED (etype));
4902 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4904 tree utype, minv, maxv;
4906 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4907 for the type in question, as we rely on this here. */
4908 utype = unsigned_type_for (etype);
4909 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4910 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4911 build_int_cst (TREE_TYPE (maxv), 1), 1);
4912 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4914 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4915 minv, 1, maxv, 1)))
4916 etype = utype;
4917 else
4918 return NULL_TREE;
4920 return etype;
4923 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4924 type, TYPE, return an expression to test if EXP is in (or out of, depending
4925 on IN_P) the range. Return 0 if the test couldn't be created. */
4927 tree
4928 build_range_check (location_t loc, tree type, tree exp, int in_p,
4929 tree low, tree high)
4931 tree etype = TREE_TYPE (exp), mask, value;
4933 /* Disable this optimization for function pointer expressions
4934 on targets that require function pointer canonicalization. */
4935 if (targetm.have_canonicalize_funcptr_for_compare ()
4936 && TREE_CODE (etype) == POINTER_TYPE
4937 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4938 return NULL_TREE;
4940 if (! in_p)
4942 value = build_range_check (loc, type, exp, 1, low, high);
4943 if (value != 0)
4944 return invert_truthvalue_loc (loc, value);
4946 return 0;
4949 if (low == 0 && high == 0)
4950 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4952 if (low == 0)
4953 return fold_build2_loc (loc, LE_EXPR, type, exp,
4954 fold_convert_loc (loc, etype, high));
4956 if (high == 0)
4957 return fold_build2_loc (loc, GE_EXPR, type, exp,
4958 fold_convert_loc (loc, etype, low));
4960 if (operand_equal_p (low, high, 0))
4961 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4962 fold_convert_loc (loc, etype, low));
4964 if (TREE_CODE (exp) == BIT_AND_EXPR
4965 && maskable_range_p (low, high, etype, &mask, &value))
4966 return fold_build2_loc (loc, EQ_EXPR, type,
4967 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4968 exp, mask),
4969 value);
4971 if (integer_zerop (low))
4973 if (! TYPE_UNSIGNED (etype))
4975 etype = unsigned_type_for (etype);
4976 high = fold_convert_loc (loc, etype, high);
4977 exp = fold_convert_loc (loc, etype, exp);
4979 return build_range_check (loc, type, exp, 1, 0, high);
4982 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4983 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4985 int prec = TYPE_PRECISION (etype);
4987 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4989 if (TYPE_UNSIGNED (etype))
4991 tree signed_etype = signed_type_for (etype);
4992 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4993 etype
4994 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4995 else
4996 etype = signed_etype;
4997 exp = fold_convert_loc (loc, etype, exp);
4999 return fold_build2_loc (loc, GT_EXPR, type, exp,
5000 build_int_cst (etype, 0));
5004 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5005 This requires wrap-around arithmetics for the type of the expression. */
5006 etype = range_check_type (etype);
5007 if (etype == NULL_TREE)
5008 return NULL_TREE;
5010 if (POINTER_TYPE_P (etype))
5011 etype = unsigned_type_for (etype);
5013 high = fold_convert_loc (loc, etype, high);
5014 low = fold_convert_loc (loc, etype, low);
5015 exp = fold_convert_loc (loc, etype, exp);
5017 value = const_binop (MINUS_EXPR, high, low);
5019 if (value != 0 && !TREE_OVERFLOW (value))
5020 return build_range_check (loc, type,
5021 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5022 1, build_int_cst (etype, 0), value);
5024 return 0;
5027 /* Return the predecessor of VAL in its type, handling the infinite case. */
5029 static tree
5030 range_predecessor (tree val)
5032 tree type = TREE_TYPE (val);
5034 if (INTEGRAL_TYPE_P (type)
5035 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5036 return 0;
5037 else
5038 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5039 build_int_cst (TREE_TYPE (val), 1), 0);
5042 /* Return the successor of VAL in its type, handling the infinite case. */
5044 static tree
5045 range_successor (tree val)
5047 tree type = TREE_TYPE (val);
5049 if (INTEGRAL_TYPE_P (type)
5050 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5051 return 0;
5052 else
5053 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5054 build_int_cst (TREE_TYPE (val), 1), 0);
5057 /* Given two ranges, see if we can merge them into one. Return 1 if we
5058 can, 0 if we can't. Set the output range into the specified parameters. */
5060 bool
5061 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5062 tree high0, int in1_p, tree low1, tree high1)
5064 int no_overlap;
5065 int subset;
5066 int temp;
5067 tree tem;
5068 int in_p;
5069 tree low, high;
5070 int lowequal = ((low0 == 0 && low1 == 0)
5071 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5072 low0, 0, low1, 0)));
5073 int highequal = ((high0 == 0 && high1 == 0)
5074 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5075 high0, 1, high1, 1)));
5077 /* Make range 0 be the range that starts first, or ends last if they
5078 start at the same value. Swap them if it isn't. */
5079 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5080 low0, 0, low1, 0))
5081 || (lowequal
5082 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5083 high1, 1, high0, 1))))
5085 temp = in0_p, in0_p = in1_p, in1_p = temp;
5086 tem = low0, low0 = low1, low1 = tem;
5087 tem = high0, high0 = high1, high1 = tem;
5090 /* Now flag two cases, whether the ranges are disjoint or whether the
5091 second range is totally subsumed in the first. Note that the tests
5092 below are simplified by the ones above. */
5093 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5094 high0, 1, low1, 0));
5095 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5096 high1, 1, high0, 1));
5098 /* We now have four cases, depending on whether we are including or
5099 excluding the two ranges. */
5100 if (in0_p && in1_p)
5102 /* If they don't overlap, the result is false. If the second range
5103 is a subset it is the result. Otherwise, the range is from the start
5104 of the second to the end of the first. */
5105 if (no_overlap)
5106 in_p = 0, low = high = 0;
5107 else if (subset)
5108 in_p = 1, low = low1, high = high1;
5109 else
5110 in_p = 1, low = low1, high = high0;
5113 else if (in0_p && ! in1_p)
5115 /* If they don't overlap, the result is the first range. If they are
5116 equal, the result is false. If the second range is a subset of the
5117 first, and the ranges begin at the same place, we go from just after
5118 the end of the second range to the end of the first. If the second
5119 range is not a subset of the first, or if it is a subset and both
5120 ranges end at the same place, the range starts at the start of the
5121 first range and ends just before the second range.
5122 Otherwise, we can't describe this as a single range. */
5123 if (no_overlap)
5124 in_p = 1, low = low0, high = high0;
5125 else if (lowequal && highequal)
5126 in_p = 0, low = high = 0;
5127 else if (subset && lowequal)
5129 low = range_successor (high1);
5130 high = high0;
5131 in_p = 1;
5132 if (low == 0)
5134 /* We are in the weird situation where high0 > high1 but
5135 high1 has no successor. Punt. */
5136 return 0;
5139 else if (! subset || highequal)
5141 low = low0;
5142 high = range_predecessor (low1);
5143 in_p = 1;
5144 if (high == 0)
5146 /* low0 < low1 but low1 has no predecessor. Punt. */
5147 return 0;
5150 else
5151 return 0;
5154 else if (! in0_p && in1_p)
5156 /* If they don't overlap, the result is the second range. If the second
5157 is a subset of the first, the result is false. Otherwise,
5158 the range starts just after the first range and ends at the
5159 end of the second. */
5160 if (no_overlap)
5161 in_p = 1, low = low1, high = high1;
5162 else if (subset || highequal)
5163 in_p = 0, low = high = 0;
5164 else
5166 low = range_successor (high0);
5167 high = high1;
5168 in_p = 1;
5169 if (low == 0)
5171 /* high1 > high0 but high0 has no successor. Punt. */
5172 return 0;
5177 else
5179 /* The case where we are excluding both ranges. Here the complex case
5180 is if they don't overlap. In that case, the only time we have a
5181 range is if they are adjacent. If the second is a subset of the
5182 first, the result is the first. Otherwise, the range to exclude
5183 starts at the beginning of the first range and ends at the end of the
5184 second. */
5185 if (no_overlap)
5187 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5188 range_successor (high0),
5189 1, low1, 0)))
5190 in_p = 0, low = low0, high = high1;
5191 else
5193 /* Canonicalize - [min, x] into - [-, x]. */
5194 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5195 switch (TREE_CODE (TREE_TYPE (low0)))
5197 case ENUMERAL_TYPE:
5198 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5199 GET_MODE_BITSIZE
5200 (TYPE_MODE (TREE_TYPE (low0)))))
5201 break;
5202 /* FALLTHROUGH */
5203 case INTEGER_TYPE:
5204 if (tree_int_cst_equal (low0,
5205 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5206 low0 = 0;
5207 break;
5208 case POINTER_TYPE:
5209 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5210 && integer_zerop (low0))
5211 low0 = 0;
5212 break;
5213 default:
5214 break;
5217 /* Canonicalize - [x, max] into - [x, -]. */
5218 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5219 switch (TREE_CODE (TREE_TYPE (high1)))
5221 case ENUMERAL_TYPE:
5222 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5223 GET_MODE_BITSIZE
5224 (TYPE_MODE (TREE_TYPE (high1)))))
5225 break;
5226 /* FALLTHROUGH */
5227 case INTEGER_TYPE:
5228 if (tree_int_cst_equal (high1,
5229 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5230 high1 = 0;
5231 break;
5232 case POINTER_TYPE:
5233 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5234 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5235 high1, 1,
5236 build_int_cst (TREE_TYPE (high1), 1),
5237 1)))
5238 high1 = 0;
5239 break;
5240 default:
5241 break;
5244 /* The ranges might be also adjacent between the maximum and
5245 minimum values of the given type. For
5246 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5247 return + [x + 1, y - 1]. */
5248 if (low0 == 0 && high1 == 0)
5250 low = range_successor (high0);
5251 high = range_predecessor (low1);
5252 if (low == 0 || high == 0)
5253 return 0;
5255 in_p = 1;
5257 else
5258 return 0;
5261 else if (subset)
5262 in_p = 0, low = low0, high = high0;
5263 else
5264 in_p = 0, low = low0, high = high1;
5267 *pin_p = in_p, *plow = low, *phigh = high;
5268 return 1;
5272 /* Subroutine of fold, looking inside expressions of the form
5273 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5274 of the COND_EXPR. This function is being used also to optimize
5275 A op B ? C : A, by reversing the comparison first.
5277 Return a folded expression whose code is not a COND_EXPR
5278 anymore, or NULL_TREE if no folding opportunity is found. */
5280 static tree
5281 fold_cond_expr_with_comparison (location_t loc, tree type,
5282 tree arg0, tree arg1, tree arg2)
5284 enum tree_code comp_code = TREE_CODE (arg0);
5285 tree arg00 = TREE_OPERAND (arg0, 0);
5286 tree arg01 = TREE_OPERAND (arg0, 1);
5287 tree arg1_type = TREE_TYPE (arg1);
5288 tree tem;
5290 STRIP_NOPS (arg1);
5291 STRIP_NOPS (arg2);
5293 /* If we have A op 0 ? A : -A, consider applying the following
5294 transformations:
5296 A == 0? A : -A same as -A
5297 A != 0? A : -A same as A
5298 A >= 0? A : -A same as abs (A)
5299 A > 0? A : -A same as abs (A)
5300 A <= 0? A : -A same as -abs (A)
5301 A < 0? A : -A same as -abs (A)
5303 None of these transformations work for modes with signed
5304 zeros. If A is +/-0, the first two transformations will
5305 change the sign of the result (from +0 to -0, or vice
5306 versa). The last four will fix the sign of the result,
5307 even though the original expressions could be positive or
5308 negative, depending on the sign of A.
5310 Note that all these transformations are correct if A is
5311 NaN, since the two alternatives (A and -A) are also NaNs. */
5312 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5313 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5314 ? real_zerop (arg01)
5315 : integer_zerop (arg01))
5316 && ((TREE_CODE (arg2) == NEGATE_EXPR
5317 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5318 /* In the case that A is of the form X-Y, '-A' (arg2) may
5319 have already been folded to Y-X, check for that. */
5320 || (TREE_CODE (arg1) == MINUS_EXPR
5321 && TREE_CODE (arg2) == MINUS_EXPR
5322 && operand_equal_p (TREE_OPERAND (arg1, 0),
5323 TREE_OPERAND (arg2, 1), 0)
5324 && operand_equal_p (TREE_OPERAND (arg1, 1),
5325 TREE_OPERAND (arg2, 0), 0))))
5326 switch (comp_code)
5328 case EQ_EXPR:
5329 case UNEQ_EXPR:
5330 tem = fold_convert_loc (loc, arg1_type, arg1);
5331 return fold_convert_loc (loc, type, negate_expr (tem));
5332 case NE_EXPR:
5333 case LTGT_EXPR:
5334 return fold_convert_loc (loc, type, arg1);
5335 case UNGE_EXPR:
5336 case UNGT_EXPR:
5337 if (flag_trapping_math)
5338 break;
5339 /* Fall through. */
5340 case GE_EXPR:
5341 case GT_EXPR:
5342 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5343 break;
5344 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5345 return fold_convert_loc (loc, type, tem);
5346 case UNLE_EXPR:
5347 case UNLT_EXPR:
5348 if (flag_trapping_math)
5349 break;
5350 /* FALLTHRU */
5351 case LE_EXPR:
5352 case LT_EXPR:
5353 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5354 break;
5355 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5356 return negate_expr (fold_convert_loc (loc, type, tem));
5357 default:
5358 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5359 break;
5362 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5363 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5364 both transformations are correct when A is NaN: A != 0
5365 is then true, and A == 0 is false. */
5367 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5368 && integer_zerop (arg01) && integer_zerop (arg2))
5370 if (comp_code == NE_EXPR)
5371 return fold_convert_loc (loc, type, arg1);
5372 else if (comp_code == EQ_EXPR)
5373 return build_zero_cst (type);
5376 /* Try some transformations of A op B ? A : B.
5378 A == B? A : B same as B
5379 A != B? A : B same as A
5380 A >= B? A : B same as max (A, B)
5381 A > B? A : B same as max (B, A)
5382 A <= B? A : B same as min (A, B)
5383 A < B? A : B same as min (B, A)
5385 As above, these transformations don't work in the presence
5386 of signed zeros. For example, if A and B are zeros of
5387 opposite sign, the first two transformations will change
5388 the sign of the result. In the last four, the original
5389 expressions give different results for (A=+0, B=-0) and
5390 (A=-0, B=+0), but the transformed expressions do not.
5392 The first two transformations are correct if either A or B
5393 is a NaN. In the first transformation, the condition will
5394 be false, and B will indeed be chosen. In the case of the
5395 second transformation, the condition A != B will be true,
5396 and A will be chosen.
5398 The conversions to max() and min() are not correct if B is
5399 a number and A is not. The conditions in the original
5400 expressions will be false, so all four give B. The min()
5401 and max() versions would give a NaN instead. */
5402 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5403 && operand_equal_for_comparison_p (arg01, arg2)
5404 /* Avoid these transformations if the COND_EXPR may be used
5405 as an lvalue in the C++ front-end. PR c++/19199. */
5406 && (in_gimple_form
5407 || VECTOR_TYPE_P (type)
5408 || (! lang_GNU_CXX ()
5409 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5410 || ! maybe_lvalue_p (arg1)
5411 || ! maybe_lvalue_p (arg2)))
5413 tree comp_op0 = arg00;
5414 tree comp_op1 = arg01;
5415 tree comp_type = TREE_TYPE (comp_op0);
5417 switch (comp_code)
5419 case EQ_EXPR:
5420 return fold_convert_loc (loc, type, arg2);
5421 case NE_EXPR:
5422 return fold_convert_loc (loc, type, arg1);
5423 case LE_EXPR:
5424 case LT_EXPR:
5425 case UNLE_EXPR:
5426 case UNLT_EXPR:
5427 /* In C++ a ?: expression can be an lvalue, so put the
5428 operand which will be used if they are equal first
5429 so that we can convert this back to the
5430 corresponding COND_EXPR. */
5431 if (!HONOR_NANS (arg1))
5433 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5434 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5435 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5436 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5437 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5438 comp_op1, comp_op0);
5439 return fold_convert_loc (loc, type, tem);
5441 break;
5442 case GE_EXPR:
5443 case GT_EXPR:
5444 case UNGE_EXPR:
5445 case UNGT_EXPR:
5446 if (!HONOR_NANS (arg1))
5448 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5449 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5450 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5451 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5452 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5453 comp_op1, comp_op0);
5454 return fold_convert_loc (loc, type, tem);
5456 break;
5457 case UNEQ_EXPR:
5458 if (!HONOR_NANS (arg1))
5459 return fold_convert_loc (loc, type, arg2);
5460 break;
5461 case LTGT_EXPR:
5462 if (!HONOR_NANS (arg1))
5463 return fold_convert_loc (loc, type, arg1);
5464 break;
5465 default:
5466 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5467 break;
5471 return NULL_TREE;
5476 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5477 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5478 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5479 false) >= 2)
5480 #endif
5482 /* EXP is some logical combination of boolean tests. See if we can
5483 merge it into some range test. Return the new tree if so. */
5485 static tree
5486 fold_range_test (location_t loc, enum tree_code code, tree type,
5487 tree op0, tree op1)
5489 int or_op = (code == TRUTH_ORIF_EXPR
5490 || code == TRUTH_OR_EXPR);
5491 int in0_p, in1_p, in_p;
5492 tree low0, low1, low, high0, high1, high;
5493 bool strict_overflow_p = false;
5494 tree tem, lhs, rhs;
5495 const char * const warnmsg = G_("assuming signed overflow does not occur "
5496 "when simplifying range test");
5498 if (!INTEGRAL_TYPE_P (type))
5499 return 0;
5501 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5502 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5504 /* If this is an OR operation, invert both sides; we will invert
5505 again at the end. */
5506 if (or_op)
5507 in0_p = ! in0_p, in1_p = ! in1_p;
5509 /* If both expressions are the same, if we can merge the ranges, and we
5510 can build the range test, return it or it inverted. If one of the
5511 ranges is always true or always false, consider it to be the same
5512 expression as the other. */
5513 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5514 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5515 in1_p, low1, high1)
5516 && (tem = (build_range_check (loc, type,
5517 lhs != 0 ? lhs
5518 : rhs != 0 ? rhs : integer_zero_node,
5519 in_p, low, high))) != 0)
5521 if (strict_overflow_p)
5522 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5523 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5526 /* On machines where the branch cost is expensive, if this is a
5527 short-circuited branch and the underlying object on both sides
5528 is the same, make a non-short-circuit operation. */
5529 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5530 && !flag_sanitize_coverage
5531 && lhs != 0 && rhs != 0
5532 && (code == TRUTH_ANDIF_EXPR
5533 || code == TRUTH_ORIF_EXPR)
5534 && operand_equal_p (lhs, rhs, 0))
5536 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5537 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5538 which cases we can't do this. */
5539 if (simple_operand_p (lhs))
5540 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5541 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5542 type, op0, op1);
5544 else if (!lang_hooks.decls.global_bindings_p ()
5545 && !CONTAINS_PLACEHOLDER_P (lhs))
5547 tree common = save_expr (lhs);
5549 if ((lhs = build_range_check (loc, type, common,
5550 or_op ? ! in0_p : in0_p,
5551 low0, high0)) != 0
5552 && (rhs = build_range_check (loc, type, common,
5553 or_op ? ! in1_p : in1_p,
5554 low1, high1)) != 0)
5556 if (strict_overflow_p)
5557 fold_overflow_warning (warnmsg,
5558 WARN_STRICT_OVERFLOW_COMPARISON);
5559 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5560 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5561 type, lhs, rhs);
5566 return 0;
5569 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5570 bit value. Arrange things so the extra bits will be set to zero if and
5571 only if C is signed-extended to its full width. If MASK is nonzero,
5572 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5574 static tree
5575 unextend (tree c, int p, int unsignedp, tree mask)
5577 tree type = TREE_TYPE (c);
5578 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5579 tree temp;
5581 if (p == modesize || unsignedp)
5582 return c;
5584 /* We work by getting just the sign bit into the low-order bit, then
5585 into the high-order bit, then sign-extend. We then XOR that value
5586 with C. */
5587 temp = build_int_cst (TREE_TYPE (c),
5588 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5590 /* We must use a signed type in order to get an arithmetic right shift.
5591 However, we must also avoid introducing accidental overflows, so that
5592 a subsequent call to integer_zerop will work. Hence we must
5593 do the type conversion here. At this point, the constant is either
5594 zero or one, and the conversion to a signed type can never overflow.
5595 We could get an overflow if this conversion is done anywhere else. */
5596 if (TYPE_UNSIGNED (type))
5597 temp = fold_convert (signed_type_for (type), temp);
5599 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5600 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5601 if (mask != 0)
5602 temp = const_binop (BIT_AND_EXPR, temp,
5603 fold_convert (TREE_TYPE (c), mask));
5604 /* If necessary, convert the type back to match the type of C. */
5605 if (TYPE_UNSIGNED (type))
5606 temp = fold_convert (type, temp);
5608 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5611 /* For an expression that has the form
5612 (A && B) || ~B
5614 (A || B) && ~B,
5615 we can drop one of the inner expressions and simplify to
5616 A || ~B
5618 A && ~B
5619 LOC is the location of the resulting expression. OP is the inner
5620 logical operation; the left-hand side in the examples above, while CMPOP
5621 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5622 removing a condition that guards another, as in
5623 (A != NULL && A->...) || A == NULL
5624 which we must not transform. If RHS_ONLY is true, only eliminate the
5625 right-most operand of the inner logical operation. */
5627 static tree
5628 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5629 bool rhs_only)
5631 tree type = TREE_TYPE (cmpop);
5632 enum tree_code code = TREE_CODE (cmpop);
5633 enum tree_code truthop_code = TREE_CODE (op);
5634 tree lhs = TREE_OPERAND (op, 0);
5635 tree rhs = TREE_OPERAND (op, 1);
5636 tree orig_lhs = lhs, orig_rhs = rhs;
5637 enum tree_code rhs_code = TREE_CODE (rhs);
5638 enum tree_code lhs_code = TREE_CODE (lhs);
5639 enum tree_code inv_code;
5641 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5642 return NULL_TREE;
5644 if (TREE_CODE_CLASS (code) != tcc_comparison)
5645 return NULL_TREE;
5647 if (rhs_code == truthop_code)
5649 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5650 if (newrhs != NULL_TREE)
5652 rhs = newrhs;
5653 rhs_code = TREE_CODE (rhs);
5656 if (lhs_code == truthop_code && !rhs_only)
5658 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5659 if (newlhs != NULL_TREE)
5661 lhs = newlhs;
5662 lhs_code = TREE_CODE (lhs);
5666 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5667 if (inv_code == rhs_code
5668 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5669 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5670 return lhs;
5671 if (!rhs_only && inv_code == lhs_code
5672 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5673 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5674 return rhs;
5675 if (rhs != orig_rhs || lhs != orig_lhs)
5676 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5677 lhs, rhs);
5678 return NULL_TREE;
5681 /* Find ways of folding logical expressions of LHS and RHS:
5682 Try to merge two comparisons to the same innermost item.
5683 Look for range tests like "ch >= '0' && ch <= '9'".
5684 Look for combinations of simple terms on machines with expensive branches
5685 and evaluate the RHS unconditionally.
5687 For example, if we have p->a == 2 && p->b == 4 and we can make an
5688 object large enough to span both A and B, we can do this with a comparison
5689 against the object ANDed with the a mask.
5691 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5692 operations to do this with one comparison.
5694 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5695 function and the one above.
5697 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5698 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5700 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5701 two operands.
5703 We return the simplified tree or 0 if no optimization is possible. */
5705 static tree
5706 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5707 tree lhs, tree rhs)
5709 /* If this is the "or" of two comparisons, we can do something if
5710 the comparisons are NE_EXPR. If this is the "and", we can do something
5711 if the comparisons are EQ_EXPR. I.e.,
5712 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5714 WANTED_CODE is this operation code. For single bit fields, we can
5715 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5716 comparison for one-bit fields. */
5718 enum tree_code wanted_code;
5719 enum tree_code lcode, rcode;
5720 tree ll_arg, lr_arg, rl_arg, rr_arg;
5721 tree ll_inner, lr_inner, rl_inner, rr_inner;
5722 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5723 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5724 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5725 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5726 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5727 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5728 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5729 scalar_int_mode lnmode, rnmode;
5730 tree ll_mask, lr_mask, rl_mask, rr_mask;
5731 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5732 tree l_const, r_const;
5733 tree lntype, rntype, result;
5734 HOST_WIDE_INT first_bit, end_bit;
5735 int volatilep;
5737 /* Start by getting the comparison codes. Fail if anything is volatile.
5738 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5739 it were surrounded with a NE_EXPR. */
5741 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5742 return 0;
5744 lcode = TREE_CODE (lhs);
5745 rcode = TREE_CODE (rhs);
5747 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5749 lhs = build2 (NE_EXPR, truth_type, lhs,
5750 build_int_cst (TREE_TYPE (lhs), 0));
5751 lcode = NE_EXPR;
5754 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5756 rhs = build2 (NE_EXPR, truth_type, rhs,
5757 build_int_cst (TREE_TYPE (rhs), 0));
5758 rcode = NE_EXPR;
5761 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5762 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5763 return 0;
5765 ll_arg = TREE_OPERAND (lhs, 0);
5766 lr_arg = TREE_OPERAND (lhs, 1);
5767 rl_arg = TREE_OPERAND (rhs, 0);
5768 rr_arg = TREE_OPERAND (rhs, 1);
5770 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5771 if (simple_operand_p (ll_arg)
5772 && simple_operand_p (lr_arg))
5774 if (operand_equal_p (ll_arg, rl_arg, 0)
5775 && operand_equal_p (lr_arg, rr_arg, 0))
5777 result = combine_comparisons (loc, code, lcode, rcode,
5778 truth_type, ll_arg, lr_arg);
5779 if (result)
5780 return result;
5782 else if (operand_equal_p (ll_arg, rr_arg, 0)
5783 && operand_equal_p (lr_arg, rl_arg, 0))
5785 result = combine_comparisons (loc, code, lcode,
5786 swap_tree_comparison (rcode),
5787 truth_type, ll_arg, lr_arg);
5788 if (result)
5789 return result;
5793 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5794 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5796 /* If the RHS can be evaluated unconditionally and its operands are
5797 simple, it wins to evaluate the RHS unconditionally on machines
5798 with expensive branches. In this case, this isn't a comparison
5799 that can be merged. */
5801 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5802 false) >= 2
5803 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5804 && simple_operand_p (rl_arg)
5805 && simple_operand_p (rr_arg))
5807 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5808 if (code == TRUTH_OR_EXPR
5809 && lcode == NE_EXPR && integer_zerop (lr_arg)
5810 && rcode == NE_EXPR && integer_zerop (rr_arg)
5811 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5812 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5813 return build2_loc (loc, NE_EXPR, truth_type,
5814 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5815 ll_arg, rl_arg),
5816 build_int_cst (TREE_TYPE (ll_arg), 0));
5818 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5819 if (code == TRUTH_AND_EXPR
5820 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5821 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5822 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5823 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5824 return build2_loc (loc, EQ_EXPR, truth_type,
5825 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5826 ll_arg, rl_arg),
5827 build_int_cst (TREE_TYPE (ll_arg), 0));
5830 /* See if the comparisons can be merged. Then get all the parameters for
5831 each side. */
5833 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5834 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5835 return 0;
5837 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5838 volatilep = 0;
5839 ll_inner = decode_field_reference (loc, &ll_arg,
5840 &ll_bitsize, &ll_bitpos, &ll_mode,
5841 &ll_unsignedp, &ll_reversep, &volatilep,
5842 &ll_mask, &ll_and_mask);
5843 lr_inner = decode_field_reference (loc, &lr_arg,
5844 &lr_bitsize, &lr_bitpos, &lr_mode,
5845 &lr_unsignedp, &lr_reversep, &volatilep,
5846 &lr_mask, &lr_and_mask);
5847 rl_inner = decode_field_reference (loc, &rl_arg,
5848 &rl_bitsize, &rl_bitpos, &rl_mode,
5849 &rl_unsignedp, &rl_reversep, &volatilep,
5850 &rl_mask, &rl_and_mask);
5851 rr_inner = decode_field_reference (loc, &rr_arg,
5852 &rr_bitsize, &rr_bitpos, &rr_mode,
5853 &rr_unsignedp, &rr_reversep, &volatilep,
5854 &rr_mask, &rr_and_mask);
5856 /* It must be true that the inner operation on the lhs of each
5857 comparison must be the same if we are to be able to do anything.
5858 Then see if we have constants. If not, the same must be true for
5859 the rhs's. */
5860 if (volatilep
5861 || ll_reversep != rl_reversep
5862 || ll_inner == 0 || rl_inner == 0
5863 || ! operand_equal_p (ll_inner, rl_inner, 0))
5864 return 0;
5866 if (TREE_CODE (lr_arg) == INTEGER_CST
5867 && TREE_CODE (rr_arg) == INTEGER_CST)
5869 l_const = lr_arg, r_const = rr_arg;
5870 lr_reversep = ll_reversep;
5872 else if (lr_reversep != rr_reversep
5873 || lr_inner == 0 || rr_inner == 0
5874 || ! operand_equal_p (lr_inner, rr_inner, 0))
5875 return 0;
5876 else
5877 l_const = r_const = 0;
5879 /* If either comparison code is not correct for our logical operation,
5880 fail. However, we can convert a one-bit comparison against zero into
5881 the opposite comparison against that bit being set in the field. */
5883 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5884 if (lcode != wanted_code)
5886 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5888 /* Make the left operand unsigned, since we are only interested
5889 in the value of one bit. Otherwise we are doing the wrong
5890 thing below. */
5891 ll_unsignedp = 1;
5892 l_const = ll_mask;
5894 else
5895 return 0;
5898 /* This is analogous to the code for l_const above. */
5899 if (rcode != wanted_code)
5901 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5903 rl_unsignedp = 1;
5904 r_const = rl_mask;
5906 else
5907 return 0;
5910 /* See if we can find a mode that contains both fields being compared on
5911 the left. If we can't, fail. Otherwise, update all constants and masks
5912 to be relative to a field of that size. */
5913 first_bit = MIN (ll_bitpos, rl_bitpos);
5914 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5915 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5916 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5917 volatilep, &lnmode))
5918 return 0;
5920 lnbitsize = GET_MODE_BITSIZE (lnmode);
5921 lnbitpos = first_bit & ~ (lnbitsize - 1);
5922 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5923 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5925 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5927 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5928 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5931 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5932 size_int (xll_bitpos));
5933 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5934 size_int (xrl_bitpos));
5936 if (l_const)
5938 l_const = fold_convert_loc (loc, lntype, l_const);
5939 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5940 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5941 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5942 fold_build1_loc (loc, BIT_NOT_EXPR,
5943 lntype, ll_mask))))
5945 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5947 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5950 if (r_const)
5952 r_const = fold_convert_loc (loc, lntype, r_const);
5953 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5954 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5955 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5956 fold_build1_loc (loc, BIT_NOT_EXPR,
5957 lntype, rl_mask))))
5959 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5961 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5965 /* If the right sides are not constant, do the same for it. Also,
5966 disallow this optimization if a size or signedness mismatch occurs
5967 between the left and right sides. */
5968 if (l_const == 0)
5970 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5971 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5972 /* Make sure the two fields on the right
5973 correspond to the left without being swapped. */
5974 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5975 return 0;
5977 first_bit = MIN (lr_bitpos, rr_bitpos);
5978 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5979 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5980 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5981 volatilep, &rnmode))
5982 return 0;
5984 rnbitsize = GET_MODE_BITSIZE (rnmode);
5985 rnbitpos = first_bit & ~ (rnbitsize - 1);
5986 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5987 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5989 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5991 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5992 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5995 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5996 rntype, lr_mask),
5997 size_int (xlr_bitpos));
5998 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5999 rntype, rr_mask),
6000 size_int (xrr_bitpos));
6002 /* Make a mask that corresponds to both fields being compared.
6003 Do this for both items being compared. If the operands are the
6004 same size and the bits being compared are in the same position
6005 then we can do this by masking both and comparing the masked
6006 results. */
6007 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6008 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6009 if (lnbitsize == rnbitsize
6010 && xll_bitpos == xlr_bitpos
6011 && lnbitpos >= 0
6012 && rnbitpos >= 0)
6014 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6015 lntype, lnbitsize, lnbitpos,
6016 ll_unsignedp || rl_unsignedp, ll_reversep);
6017 if (! all_ones_mask_p (ll_mask, lnbitsize))
6018 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6020 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6021 rntype, rnbitsize, rnbitpos,
6022 lr_unsignedp || rr_unsignedp, lr_reversep);
6023 if (! all_ones_mask_p (lr_mask, rnbitsize))
6024 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6026 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6029 /* There is still another way we can do something: If both pairs of
6030 fields being compared are adjacent, we may be able to make a wider
6031 field containing them both.
6033 Note that we still must mask the lhs/rhs expressions. Furthermore,
6034 the mask must be shifted to account for the shift done by
6035 make_bit_field_ref. */
6036 if (((ll_bitsize + ll_bitpos == rl_bitpos
6037 && lr_bitsize + lr_bitpos == rr_bitpos)
6038 || (ll_bitpos == rl_bitpos + rl_bitsize
6039 && lr_bitpos == rr_bitpos + rr_bitsize))
6040 && ll_bitpos >= 0
6041 && rl_bitpos >= 0
6042 && lr_bitpos >= 0
6043 && rr_bitpos >= 0)
6045 tree type;
6047 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6048 ll_bitsize + rl_bitsize,
6049 MIN (ll_bitpos, rl_bitpos),
6050 ll_unsignedp, ll_reversep);
6051 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6052 lr_bitsize + rr_bitsize,
6053 MIN (lr_bitpos, rr_bitpos),
6054 lr_unsignedp, lr_reversep);
6056 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6057 size_int (MIN (xll_bitpos, xrl_bitpos)));
6058 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6059 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6061 /* Convert to the smaller type before masking out unwanted bits. */
6062 type = lntype;
6063 if (lntype != rntype)
6065 if (lnbitsize > rnbitsize)
6067 lhs = fold_convert_loc (loc, rntype, lhs);
6068 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6069 type = rntype;
6071 else if (lnbitsize < rnbitsize)
6073 rhs = fold_convert_loc (loc, lntype, rhs);
6074 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6075 type = lntype;
6079 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6080 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6082 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6083 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6085 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6088 return 0;
6091 /* Handle the case of comparisons with constants. If there is something in
6092 common between the masks, those bits of the constants must be the same.
6093 If not, the condition is always false. Test for this to avoid generating
6094 incorrect code below. */
6095 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6096 if (! integer_zerop (result)
6097 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6098 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6100 if (wanted_code == NE_EXPR)
6102 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6103 return constant_boolean_node (true, truth_type);
6105 else
6107 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6108 return constant_boolean_node (false, truth_type);
6112 if (lnbitpos < 0)
6113 return 0;
6115 /* Construct the expression we will return. First get the component
6116 reference we will make. Unless the mask is all ones the width of
6117 that field, perform the mask operation. Then compare with the
6118 merged constant. */
6119 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6120 lntype, lnbitsize, lnbitpos,
6121 ll_unsignedp || rl_unsignedp, ll_reversep);
6123 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6124 if (! all_ones_mask_p (ll_mask, lnbitsize))
6125 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6127 return build2_loc (loc, wanted_code, truth_type, result,
6128 const_binop (BIT_IOR_EXPR, l_const, r_const));
6131 /* T is an integer expression that is being multiplied, divided, or taken a
6132 modulus (CODE says which and what kind of divide or modulus) by a
6133 constant C. See if we can eliminate that operation by folding it with
6134 other operations already in T. WIDE_TYPE, if non-null, is a type that
6135 should be used for the computation if wider than our type.
6137 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6138 (X * 2) + (Y * 4). We must, however, be assured that either the original
6139 expression would not overflow or that overflow is undefined for the type
6140 in the language in question.
6142 If we return a non-null expression, it is an equivalent form of the
6143 original computation, but need not be in the original type.
6145 We set *STRICT_OVERFLOW_P to true if the return values depends on
6146 signed overflow being undefined. Otherwise we do not change
6147 *STRICT_OVERFLOW_P. */
6149 static tree
6150 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6151 bool *strict_overflow_p)
6153 /* To avoid exponential search depth, refuse to allow recursion past
6154 three levels. Beyond that (1) it's highly unlikely that we'll find
6155 something interesting and (2) we've probably processed it before
6156 when we built the inner expression. */
6158 static int depth;
6159 tree ret;
6161 if (depth > 3)
6162 return NULL;
6164 depth++;
6165 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6166 depth--;
6168 return ret;
6171 static tree
6172 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6173 bool *strict_overflow_p)
6175 tree type = TREE_TYPE (t);
6176 enum tree_code tcode = TREE_CODE (t);
6177 tree ctype = (wide_type != 0
6178 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6179 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6180 ? wide_type : type);
6181 tree t1, t2;
6182 int same_p = tcode == code;
6183 tree op0 = NULL_TREE, op1 = NULL_TREE;
6184 bool sub_strict_overflow_p;
6186 /* Don't deal with constants of zero here; they confuse the code below. */
6187 if (integer_zerop (c))
6188 return NULL_TREE;
6190 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6191 op0 = TREE_OPERAND (t, 0);
6193 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6194 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6196 /* Note that we need not handle conditional operations here since fold
6197 already handles those cases. So just do arithmetic here. */
6198 switch (tcode)
6200 case INTEGER_CST:
6201 /* For a constant, we can always simplify if we are a multiply
6202 or (for divide and modulus) if it is a multiple of our constant. */
6203 if (code == MULT_EXPR
6204 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6205 TYPE_SIGN (type)))
6207 tree tem = const_binop (code, fold_convert (ctype, t),
6208 fold_convert (ctype, c));
6209 /* If the multiplication overflowed, we lost information on it.
6210 See PR68142 and PR69845. */
6211 if (TREE_OVERFLOW (tem))
6212 return NULL_TREE;
6213 return tem;
6215 break;
6217 CASE_CONVERT: case NON_LVALUE_EXPR:
6218 /* If op0 is an expression ... */
6219 if ((COMPARISON_CLASS_P (op0)
6220 || UNARY_CLASS_P (op0)
6221 || BINARY_CLASS_P (op0)
6222 || VL_EXP_CLASS_P (op0)
6223 || EXPRESSION_CLASS_P (op0))
6224 /* ... and has wrapping overflow, and its type is smaller
6225 than ctype, then we cannot pass through as widening. */
6226 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6227 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6228 && (TYPE_PRECISION (ctype)
6229 > TYPE_PRECISION (TREE_TYPE (op0))))
6230 /* ... or this is a truncation (t is narrower than op0),
6231 then we cannot pass through this narrowing. */
6232 || (TYPE_PRECISION (type)
6233 < TYPE_PRECISION (TREE_TYPE (op0)))
6234 /* ... or signedness changes for division or modulus,
6235 then we cannot pass through this conversion. */
6236 || (code != MULT_EXPR
6237 && (TYPE_UNSIGNED (ctype)
6238 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6239 /* ... or has undefined overflow while the converted to
6240 type has not, we cannot do the operation in the inner type
6241 as that would introduce undefined overflow. */
6242 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6243 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6244 && !TYPE_OVERFLOW_UNDEFINED (type))))
6245 break;
6247 /* Pass the constant down and see if we can make a simplification. If
6248 we can, replace this expression with the inner simplification for
6249 possible later conversion to our or some other type. */
6250 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6251 && TREE_CODE (t2) == INTEGER_CST
6252 && !TREE_OVERFLOW (t2)
6253 && (t1 = extract_muldiv (op0, t2, code,
6254 code == MULT_EXPR ? ctype : NULL_TREE,
6255 strict_overflow_p)) != 0)
6256 return t1;
6257 break;
6259 case ABS_EXPR:
6260 /* If widening the type changes it from signed to unsigned, then we
6261 must avoid building ABS_EXPR itself as unsigned. */
6262 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6264 tree cstype = (*signed_type_for) (ctype);
6265 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6266 != 0)
6268 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6269 return fold_convert (ctype, t1);
6271 break;
6273 /* If the constant is negative, we cannot simplify this. */
6274 if (tree_int_cst_sgn (c) == -1)
6275 break;
6276 /* FALLTHROUGH */
6277 case NEGATE_EXPR:
6278 /* For division and modulus, type can't be unsigned, as e.g.
6279 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6280 For signed types, even with wrapping overflow, this is fine. */
6281 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6282 break;
6283 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6284 != 0)
6285 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6286 break;
6288 case MIN_EXPR: case MAX_EXPR:
6289 /* If widening the type changes the signedness, then we can't perform
6290 this optimization as that changes the result. */
6291 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6292 break;
6294 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6295 sub_strict_overflow_p = false;
6296 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6297 &sub_strict_overflow_p)) != 0
6298 && (t2 = extract_muldiv (op1, c, code, wide_type,
6299 &sub_strict_overflow_p)) != 0)
6301 if (tree_int_cst_sgn (c) < 0)
6302 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6303 if (sub_strict_overflow_p)
6304 *strict_overflow_p = true;
6305 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6306 fold_convert (ctype, t2));
6308 break;
6310 case LSHIFT_EXPR: case RSHIFT_EXPR:
6311 /* If the second operand is constant, this is a multiplication
6312 or floor division, by a power of two, so we can treat it that
6313 way unless the multiplier or divisor overflows. Signed
6314 left-shift overflow is implementation-defined rather than
6315 undefined in C90, so do not convert signed left shift into
6316 multiplication. */
6317 if (TREE_CODE (op1) == INTEGER_CST
6318 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6319 /* const_binop may not detect overflow correctly,
6320 so check for it explicitly here. */
6321 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6322 wi::to_wide (op1))
6323 && (t1 = fold_convert (ctype,
6324 const_binop (LSHIFT_EXPR, size_one_node,
6325 op1))) != 0
6326 && !TREE_OVERFLOW (t1))
6327 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6328 ? MULT_EXPR : FLOOR_DIV_EXPR,
6329 ctype,
6330 fold_convert (ctype, op0),
6331 t1),
6332 c, code, wide_type, strict_overflow_p);
6333 break;
6335 case PLUS_EXPR: case MINUS_EXPR:
6336 /* See if we can eliminate the operation on both sides. If we can, we
6337 can return a new PLUS or MINUS. If we can't, the only remaining
6338 cases where we can do anything are if the second operand is a
6339 constant. */
6340 sub_strict_overflow_p = false;
6341 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6342 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6343 if (t1 != 0 && t2 != 0
6344 && TYPE_OVERFLOW_WRAPS (ctype)
6345 && (code == MULT_EXPR
6346 /* If not multiplication, we can only do this if both operands
6347 are divisible by c. */
6348 || (multiple_of_p (ctype, op0, c)
6349 && multiple_of_p (ctype, op1, c))))
6351 if (sub_strict_overflow_p)
6352 *strict_overflow_p = true;
6353 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6354 fold_convert (ctype, t2));
6357 /* If this was a subtraction, negate OP1 and set it to be an addition.
6358 This simplifies the logic below. */
6359 if (tcode == MINUS_EXPR)
6361 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6362 /* If OP1 was not easily negatable, the constant may be OP0. */
6363 if (TREE_CODE (op0) == INTEGER_CST)
6365 std::swap (op0, op1);
6366 std::swap (t1, t2);
6370 if (TREE_CODE (op1) != INTEGER_CST)
6371 break;
6373 /* If either OP1 or C are negative, this optimization is not safe for
6374 some of the division and remainder types while for others we need
6375 to change the code. */
6376 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6378 if (code == CEIL_DIV_EXPR)
6379 code = FLOOR_DIV_EXPR;
6380 else if (code == FLOOR_DIV_EXPR)
6381 code = CEIL_DIV_EXPR;
6382 else if (code != MULT_EXPR
6383 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6384 break;
6387 /* If it's a multiply or a division/modulus operation of a multiple
6388 of our constant, do the operation and verify it doesn't overflow. */
6389 if (code == MULT_EXPR
6390 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6391 TYPE_SIGN (type)))
6393 op1 = const_binop (code, fold_convert (ctype, op1),
6394 fold_convert (ctype, c));
6395 /* We allow the constant to overflow with wrapping semantics. */
6396 if (op1 == 0
6397 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6398 break;
6400 else
6401 break;
6403 /* If we have an unsigned type, we cannot widen the operation since it
6404 will change the result if the original computation overflowed. */
6405 if (TYPE_UNSIGNED (ctype) && ctype != type)
6406 break;
6408 /* The last case is if we are a multiply. In that case, we can
6409 apply the distributive law to commute the multiply and addition
6410 if the multiplication of the constants doesn't overflow
6411 and overflow is defined. With undefined overflow
6412 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6413 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6414 return fold_build2 (tcode, ctype,
6415 fold_build2 (code, ctype,
6416 fold_convert (ctype, op0),
6417 fold_convert (ctype, c)),
6418 op1);
6420 break;
6422 case MULT_EXPR:
6423 /* We have a special case here if we are doing something like
6424 (C * 8) % 4 since we know that's zero. */
6425 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6426 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6427 /* If the multiplication can overflow we cannot optimize this. */
6428 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6429 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6430 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6431 TYPE_SIGN (type)))
6433 *strict_overflow_p = true;
6434 return omit_one_operand (type, integer_zero_node, op0);
6437 /* ... fall through ... */
6439 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6440 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6441 /* If we can extract our operation from the LHS, do so and return a
6442 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6443 do something only if the second operand is a constant. */
6444 if (same_p
6445 && TYPE_OVERFLOW_WRAPS (ctype)
6446 && (t1 = extract_muldiv (op0, c, code, wide_type,
6447 strict_overflow_p)) != 0)
6448 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6449 fold_convert (ctype, op1));
6450 else if (tcode == MULT_EXPR && code == MULT_EXPR
6451 && TYPE_OVERFLOW_WRAPS (ctype)
6452 && (t1 = extract_muldiv (op1, c, code, wide_type,
6453 strict_overflow_p)) != 0)
6454 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6455 fold_convert (ctype, t1));
6456 else if (TREE_CODE (op1) != INTEGER_CST)
6457 return 0;
6459 /* If these are the same operation types, we can associate them
6460 assuming no overflow. */
6461 if (tcode == code)
6463 bool overflow_p = false;
6464 bool overflow_mul_p;
6465 signop sign = TYPE_SIGN (ctype);
6466 unsigned prec = TYPE_PRECISION (ctype);
6467 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6468 wi::to_wide (c, prec),
6469 sign, &overflow_mul_p);
6470 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6471 if (overflow_mul_p
6472 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6473 overflow_p = true;
6474 if (!overflow_p)
6475 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6476 wide_int_to_tree (ctype, mul));
6479 /* If these operations "cancel" each other, we have the main
6480 optimizations of this pass, which occur when either constant is a
6481 multiple of the other, in which case we replace this with either an
6482 operation or CODE or TCODE.
6484 If we have an unsigned type, we cannot do this since it will change
6485 the result if the original computation overflowed. */
6486 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6487 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6488 || (tcode == MULT_EXPR
6489 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6490 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6491 && code != MULT_EXPR)))
6493 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6494 TYPE_SIGN (type)))
6496 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6497 *strict_overflow_p = true;
6498 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6499 fold_convert (ctype,
6500 const_binop (TRUNC_DIV_EXPR,
6501 op1, c)));
6503 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6504 TYPE_SIGN (type)))
6506 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6507 *strict_overflow_p = true;
6508 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6509 fold_convert (ctype,
6510 const_binop (TRUNC_DIV_EXPR,
6511 c, op1)));
6514 break;
6516 default:
6517 break;
6520 return 0;
6523 /* Return a node which has the indicated constant VALUE (either 0 or
6524 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6525 and is of the indicated TYPE. */
6527 tree
6528 constant_boolean_node (bool value, tree type)
6530 if (type == integer_type_node)
6531 return value ? integer_one_node : integer_zero_node;
6532 else if (type == boolean_type_node)
6533 return value ? boolean_true_node : boolean_false_node;
6534 else if (TREE_CODE (type) == VECTOR_TYPE)
6535 return build_vector_from_val (type,
6536 build_int_cst (TREE_TYPE (type),
6537 value ? -1 : 0));
6538 else
6539 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6543 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6544 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6545 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6546 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6547 COND is the first argument to CODE; otherwise (as in the example
6548 given here), it is the second argument. TYPE is the type of the
6549 original expression. Return NULL_TREE if no simplification is
6550 possible. */
6552 static tree
6553 fold_binary_op_with_conditional_arg (location_t loc,
6554 enum tree_code code,
6555 tree type, tree op0, tree op1,
6556 tree cond, tree arg, int cond_first_p)
6558 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6559 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6560 tree test, true_value, false_value;
6561 tree lhs = NULL_TREE;
6562 tree rhs = NULL_TREE;
6563 enum tree_code cond_code = COND_EXPR;
6565 if (TREE_CODE (cond) == COND_EXPR
6566 || TREE_CODE (cond) == VEC_COND_EXPR)
6568 test = TREE_OPERAND (cond, 0);
6569 true_value = TREE_OPERAND (cond, 1);
6570 false_value = TREE_OPERAND (cond, 2);
6571 /* If this operand throws an expression, then it does not make
6572 sense to try to perform a logical or arithmetic operation
6573 involving it. */
6574 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6575 lhs = true_value;
6576 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6577 rhs = false_value;
6579 else if (!(TREE_CODE (type) != VECTOR_TYPE
6580 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6582 tree testtype = TREE_TYPE (cond);
6583 test = cond;
6584 true_value = constant_boolean_node (true, testtype);
6585 false_value = constant_boolean_node (false, testtype);
6587 else
6588 /* Detect the case of mixing vector and scalar types - bail out. */
6589 return NULL_TREE;
6591 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6592 cond_code = VEC_COND_EXPR;
6594 /* This transformation is only worthwhile if we don't have to wrap ARG
6595 in a SAVE_EXPR and the operation can be simplified without recursing
6596 on at least one of the branches once its pushed inside the COND_EXPR. */
6597 if (!TREE_CONSTANT (arg)
6598 && (TREE_SIDE_EFFECTS (arg)
6599 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6600 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6601 return NULL_TREE;
6603 arg = fold_convert_loc (loc, arg_type, arg);
6604 if (lhs == 0)
6606 true_value = fold_convert_loc (loc, cond_type, true_value);
6607 if (cond_first_p)
6608 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6609 else
6610 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6612 if (rhs == 0)
6614 false_value = fold_convert_loc (loc, cond_type, false_value);
6615 if (cond_first_p)
6616 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6617 else
6618 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6621 /* Check that we have simplified at least one of the branches. */
6622 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6623 return NULL_TREE;
6625 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6629 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6631 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6632 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6633 ADDEND is the same as X.
6635 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6636 and finite. The problematic cases are when X is zero, and its mode
6637 has signed zeros. In the case of rounding towards -infinity,
6638 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6639 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6641 bool
6642 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6644 if (!real_zerop (addend))
6645 return false;
6647 /* Don't allow the fold with -fsignaling-nans. */
6648 if (HONOR_SNANS (element_mode (type)))
6649 return false;
6651 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6652 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6653 return true;
6655 /* In a vector or complex, we would need to check the sign of all zeros. */
6656 if (TREE_CODE (addend) != REAL_CST)
6657 return false;
6659 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6660 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6661 negate = !negate;
6663 /* The mode has signed zeros, and we have to honor their sign.
6664 In this situation, there is only one case we can return true for.
6665 X - 0 is the same as X unless rounding towards -infinity is
6666 supported. */
6667 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6670 /* Subroutine of match.pd that optimizes comparisons of a division by
6671 a nonzero integer constant against an integer constant, i.e.
6672 X/C1 op C2.
6674 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6675 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6677 enum tree_code
6678 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6679 tree *hi, bool *neg_overflow)
6681 tree prod, tmp, type = TREE_TYPE (c1);
6682 signop sign = TYPE_SIGN (type);
6683 bool overflow;
6685 /* We have to do this the hard way to detect unsigned overflow.
6686 prod = int_const_binop (MULT_EXPR, c1, c2); */
6687 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6688 prod = force_fit_type (type, val, -1, overflow);
6689 *neg_overflow = false;
6691 if (sign == UNSIGNED)
6693 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6694 *lo = prod;
6696 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6697 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6698 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6700 else if (tree_int_cst_sgn (c1) >= 0)
6702 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6703 switch (tree_int_cst_sgn (c2))
6705 case -1:
6706 *neg_overflow = true;
6707 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6708 *hi = prod;
6709 break;
6711 case 0:
6712 *lo = fold_negate_const (tmp, type);
6713 *hi = tmp;
6714 break;
6716 case 1:
6717 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6718 *lo = prod;
6719 break;
6721 default:
6722 gcc_unreachable ();
6725 else
6727 /* A negative divisor reverses the relational operators. */
6728 code = swap_tree_comparison (code);
6730 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6731 switch (tree_int_cst_sgn (c2))
6733 case -1:
6734 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6735 *lo = prod;
6736 break;
6738 case 0:
6739 *hi = fold_negate_const (tmp, type);
6740 *lo = tmp;
6741 break;
6743 case 1:
6744 *neg_overflow = true;
6745 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6746 *hi = prod;
6747 break;
6749 default:
6750 gcc_unreachable ();
6754 if (code != EQ_EXPR && code != NE_EXPR)
6755 return code;
6757 if (TREE_OVERFLOW (*lo)
6758 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6759 *lo = NULL_TREE;
6760 if (TREE_OVERFLOW (*hi)
6761 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6762 *hi = NULL_TREE;
6764 return code;
6768 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6769 equality/inequality test, then return a simplified form of the test
6770 using a sign testing. Otherwise return NULL. TYPE is the desired
6771 result type. */
6773 static tree
6774 fold_single_bit_test_into_sign_test (location_t loc,
6775 enum tree_code code, tree arg0, tree arg1,
6776 tree result_type)
6778 /* If this is testing a single bit, we can optimize the test. */
6779 if ((code == NE_EXPR || code == EQ_EXPR)
6780 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6781 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6783 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6784 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6785 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6787 if (arg00 != NULL_TREE
6788 /* This is only a win if casting to a signed type is cheap,
6789 i.e. when arg00's type is not a partial mode. */
6790 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6792 tree stype = signed_type_for (TREE_TYPE (arg00));
6793 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6794 result_type,
6795 fold_convert_loc (loc, stype, arg00),
6796 build_int_cst (stype, 0));
6800 return NULL_TREE;
6803 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6804 equality/inequality test, then return a simplified form of
6805 the test using shifts and logical operations. Otherwise return
6806 NULL. TYPE is the desired result type. */
6808 tree
6809 fold_single_bit_test (location_t loc, enum tree_code code,
6810 tree arg0, tree arg1, tree result_type)
6812 /* If this is testing a single bit, we can optimize the test. */
6813 if ((code == NE_EXPR || code == EQ_EXPR)
6814 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6815 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6817 tree inner = TREE_OPERAND (arg0, 0);
6818 tree type = TREE_TYPE (arg0);
6819 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6820 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6821 int ops_unsigned;
6822 tree signed_type, unsigned_type, intermediate_type;
6823 tree tem, one;
6825 /* First, see if we can fold the single bit test into a sign-bit
6826 test. */
6827 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6828 result_type);
6829 if (tem)
6830 return tem;
6832 /* Otherwise we have (A & C) != 0 where C is a single bit,
6833 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6834 Similarly for (A & C) == 0. */
6836 /* If INNER is a right shift of a constant and it plus BITNUM does
6837 not overflow, adjust BITNUM and INNER. */
6838 if (TREE_CODE (inner) == RSHIFT_EXPR
6839 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6840 && bitnum < TYPE_PRECISION (type)
6841 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6842 TYPE_PRECISION (type) - bitnum))
6844 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6845 inner = TREE_OPERAND (inner, 0);
6848 /* If we are going to be able to omit the AND below, we must do our
6849 operations as unsigned. If we must use the AND, we have a choice.
6850 Normally unsigned is faster, but for some machines signed is. */
6851 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6852 && !flag_syntax_only) ? 0 : 1;
6854 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6855 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6856 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6857 inner = fold_convert_loc (loc, intermediate_type, inner);
6859 if (bitnum != 0)
6860 inner = build2 (RSHIFT_EXPR, intermediate_type,
6861 inner, size_int (bitnum));
6863 one = build_int_cst (intermediate_type, 1);
6865 if (code == EQ_EXPR)
6866 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6868 /* Put the AND last so it can combine with more things. */
6869 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6871 /* Make sure to return the proper type. */
6872 inner = fold_convert_loc (loc, result_type, inner);
6874 return inner;
6876 return NULL_TREE;
6879 /* Test whether it is preferable two swap two operands, ARG0 and
6880 ARG1, for example because ARG0 is an integer constant and ARG1
6881 isn't. */
6883 bool
6884 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6886 if (CONSTANT_CLASS_P (arg1))
6887 return 0;
6888 if (CONSTANT_CLASS_P (arg0))
6889 return 1;
6891 STRIP_NOPS (arg0);
6892 STRIP_NOPS (arg1);
6894 if (TREE_CONSTANT (arg1))
6895 return 0;
6896 if (TREE_CONSTANT (arg0))
6897 return 1;
6899 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6900 for commutative and comparison operators. Ensuring a canonical
6901 form allows the optimizers to find additional redundancies without
6902 having to explicitly check for both orderings. */
6903 if (TREE_CODE (arg0) == SSA_NAME
6904 && TREE_CODE (arg1) == SSA_NAME
6905 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6906 return 1;
6908 /* Put SSA_NAMEs last. */
6909 if (TREE_CODE (arg1) == SSA_NAME)
6910 return 0;
6911 if (TREE_CODE (arg0) == SSA_NAME)
6912 return 1;
6914 /* Put variables last. */
6915 if (DECL_P (arg1))
6916 return 0;
6917 if (DECL_P (arg0))
6918 return 1;
6920 return 0;
6924 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6925 means A >= Y && A != MAX, but in this case we know that
6926 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6928 static tree
6929 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6931 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6933 if (TREE_CODE (bound) == LT_EXPR)
6934 a = TREE_OPERAND (bound, 0);
6935 else if (TREE_CODE (bound) == GT_EXPR)
6936 a = TREE_OPERAND (bound, 1);
6937 else
6938 return NULL_TREE;
6940 typea = TREE_TYPE (a);
6941 if (!INTEGRAL_TYPE_P (typea)
6942 && !POINTER_TYPE_P (typea))
6943 return NULL_TREE;
6945 if (TREE_CODE (ineq) == LT_EXPR)
6947 a1 = TREE_OPERAND (ineq, 1);
6948 y = TREE_OPERAND (ineq, 0);
6950 else if (TREE_CODE (ineq) == GT_EXPR)
6952 a1 = TREE_OPERAND (ineq, 0);
6953 y = TREE_OPERAND (ineq, 1);
6955 else
6956 return NULL_TREE;
6958 if (TREE_TYPE (a1) != typea)
6959 return NULL_TREE;
6961 if (POINTER_TYPE_P (typea))
6963 /* Convert the pointer types into integer before taking the difference. */
6964 tree ta = fold_convert_loc (loc, ssizetype, a);
6965 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6966 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6968 else
6969 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6971 if (!diff || !integer_onep (diff))
6972 return NULL_TREE;
6974 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6977 /* Fold a sum or difference of at least one multiplication.
6978 Returns the folded tree or NULL if no simplification could be made. */
6980 static tree
6981 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6982 tree arg0, tree arg1)
6984 tree arg00, arg01, arg10, arg11;
6985 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6987 /* (A * C) +- (B * C) -> (A+-B) * C.
6988 (A * C) +- A -> A * (C+-1).
6989 We are most concerned about the case where C is a constant,
6990 but other combinations show up during loop reduction. Since
6991 it is not difficult, try all four possibilities. */
6993 if (TREE_CODE (arg0) == MULT_EXPR)
6995 arg00 = TREE_OPERAND (arg0, 0);
6996 arg01 = TREE_OPERAND (arg0, 1);
6998 else if (TREE_CODE (arg0) == INTEGER_CST)
7000 arg00 = build_one_cst (type);
7001 arg01 = arg0;
7003 else
7005 /* We cannot generate constant 1 for fract. */
7006 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7007 return NULL_TREE;
7008 arg00 = arg0;
7009 arg01 = build_one_cst (type);
7011 if (TREE_CODE (arg1) == MULT_EXPR)
7013 arg10 = TREE_OPERAND (arg1, 0);
7014 arg11 = TREE_OPERAND (arg1, 1);
7016 else if (TREE_CODE (arg1) == INTEGER_CST)
7018 arg10 = build_one_cst (type);
7019 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7020 the purpose of this canonicalization. */
7021 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7022 && negate_expr_p (arg1)
7023 && code == PLUS_EXPR)
7025 arg11 = negate_expr (arg1);
7026 code = MINUS_EXPR;
7028 else
7029 arg11 = arg1;
7031 else
7033 /* We cannot generate constant 1 for fract. */
7034 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7035 return NULL_TREE;
7036 arg10 = arg1;
7037 arg11 = build_one_cst (type);
7039 same = NULL_TREE;
7041 /* Prefer factoring a common non-constant. */
7042 if (operand_equal_p (arg00, arg10, 0))
7043 same = arg00, alt0 = arg01, alt1 = arg11;
7044 else if (operand_equal_p (arg01, arg11, 0))
7045 same = arg01, alt0 = arg00, alt1 = arg10;
7046 else if (operand_equal_p (arg00, arg11, 0))
7047 same = arg00, alt0 = arg01, alt1 = arg10;
7048 else if (operand_equal_p (arg01, arg10, 0))
7049 same = arg01, alt0 = arg00, alt1 = arg11;
7051 /* No identical multiplicands; see if we can find a common
7052 power-of-two factor in non-power-of-two multiplies. This
7053 can help in multi-dimensional array access. */
7054 else if (tree_fits_shwi_p (arg01)
7055 && tree_fits_shwi_p (arg11))
7057 HOST_WIDE_INT int01, int11, tmp;
7058 bool swap = false;
7059 tree maybe_same;
7060 int01 = tree_to_shwi (arg01);
7061 int11 = tree_to_shwi (arg11);
7063 /* Move min of absolute values to int11. */
7064 if (absu_hwi (int01) < absu_hwi (int11))
7066 tmp = int01, int01 = int11, int11 = tmp;
7067 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7068 maybe_same = arg01;
7069 swap = true;
7071 else
7072 maybe_same = arg11;
7074 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7075 /* The remainder should not be a constant, otherwise we
7076 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7077 increased the number of multiplications necessary. */
7078 && TREE_CODE (arg10) != INTEGER_CST)
7080 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7081 build_int_cst (TREE_TYPE (arg00),
7082 int01 / int11));
7083 alt1 = arg10;
7084 same = maybe_same;
7085 if (swap)
7086 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7090 if (!same)
7091 return NULL_TREE;
7093 if (! INTEGRAL_TYPE_P (type)
7094 || TYPE_OVERFLOW_WRAPS (type)
7095 /* We are neither factoring zero nor minus one. */
7096 || TREE_CODE (same) == INTEGER_CST)
7097 return fold_build2_loc (loc, MULT_EXPR, type,
7098 fold_build2_loc (loc, code, type,
7099 fold_convert_loc (loc, type, alt0),
7100 fold_convert_loc (loc, type, alt1)),
7101 fold_convert_loc (loc, type, same));
7103 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7104 same may be minus one and thus the multiplication may overflow. Perform
7105 the sum operation in an unsigned type. */
7106 tree utype = unsigned_type_for (type);
7107 tree tem = fold_build2_loc (loc, code, utype,
7108 fold_convert_loc (loc, utype, alt0),
7109 fold_convert_loc (loc, utype, alt1));
7110 /* If the sum evaluated to a constant that is not -INF the multiplication
7111 cannot overflow. */
7112 if (TREE_CODE (tem) == INTEGER_CST
7113 && (wi::to_wide (tem)
7114 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7115 return fold_build2_loc (loc, MULT_EXPR, type,
7116 fold_convert (type, tem), same);
7118 /* Do not resort to unsigned multiplication because
7119 we lose the no-overflow property of the expression. */
7120 return NULL_TREE;
7123 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7124 specified by EXPR into the buffer PTR of length LEN bytes.
7125 Return the number of bytes placed in the buffer, or zero
7126 upon failure. */
7128 static int
7129 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7131 tree type = TREE_TYPE (expr);
7132 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7133 int byte, offset, word, words;
7134 unsigned char value;
7136 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7137 return 0;
7138 if (off == -1)
7139 off = 0;
7141 if (ptr == NULL)
7142 /* Dry run. */
7143 return MIN (len, total_bytes - off);
7145 words = total_bytes / UNITS_PER_WORD;
7147 for (byte = 0; byte < total_bytes; byte++)
7149 int bitpos = byte * BITS_PER_UNIT;
7150 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7151 number of bytes. */
7152 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7154 if (total_bytes > UNITS_PER_WORD)
7156 word = byte / UNITS_PER_WORD;
7157 if (WORDS_BIG_ENDIAN)
7158 word = (words - 1) - word;
7159 offset = word * UNITS_PER_WORD;
7160 if (BYTES_BIG_ENDIAN)
7161 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7162 else
7163 offset += byte % UNITS_PER_WORD;
7165 else
7166 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7167 if (offset >= off && offset - off < len)
7168 ptr[offset - off] = value;
7170 return MIN (len, total_bytes - off);
7174 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7175 specified by EXPR into the buffer PTR of length LEN bytes.
7176 Return the number of bytes placed in the buffer, or zero
7177 upon failure. */
7179 static int
7180 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7182 tree type = TREE_TYPE (expr);
7183 scalar_mode mode = SCALAR_TYPE_MODE (type);
7184 int total_bytes = GET_MODE_SIZE (mode);
7185 FIXED_VALUE_TYPE value;
7186 tree i_value, i_type;
7188 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7189 return 0;
7191 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7193 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7194 return 0;
7196 value = TREE_FIXED_CST (expr);
7197 i_value = double_int_to_tree (i_type, value.data);
7199 return native_encode_int (i_value, ptr, len, off);
7203 /* Subroutine of native_encode_expr. Encode the REAL_CST
7204 specified by EXPR into the buffer PTR of length LEN bytes.
7205 Return the number of bytes placed in the buffer, or zero
7206 upon failure. */
7208 static int
7209 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7211 tree type = TREE_TYPE (expr);
7212 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7213 int byte, offset, word, words, bitpos;
7214 unsigned char value;
7216 /* There are always 32 bits in each long, no matter the size of
7217 the hosts long. We handle floating point representations with
7218 up to 192 bits. */
7219 long tmp[6];
7221 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7222 return 0;
7223 if (off == -1)
7224 off = 0;
7226 if (ptr == NULL)
7227 /* Dry run. */
7228 return MIN (len, total_bytes - off);
7230 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7232 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7234 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7235 bitpos += BITS_PER_UNIT)
7237 byte = (bitpos / BITS_PER_UNIT) & 3;
7238 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7240 if (UNITS_PER_WORD < 4)
7242 word = byte / UNITS_PER_WORD;
7243 if (WORDS_BIG_ENDIAN)
7244 word = (words - 1) - word;
7245 offset = word * UNITS_PER_WORD;
7246 if (BYTES_BIG_ENDIAN)
7247 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7248 else
7249 offset += byte % UNITS_PER_WORD;
7251 else
7253 offset = byte;
7254 if (BYTES_BIG_ENDIAN)
7256 /* Reverse bytes within each long, or within the entire float
7257 if it's smaller than a long (for HFmode). */
7258 offset = MIN (3, total_bytes - 1) - offset;
7259 gcc_assert (offset >= 0);
7262 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7263 if (offset >= off
7264 && offset - off < len)
7265 ptr[offset - off] = value;
7267 return MIN (len, total_bytes - off);
7270 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7271 specified by EXPR into the buffer PTR of length LEN bytes.
7272 Return the number of bytes placed in the buffer, or zero
7273 upon failure. */
7275 static int
7276 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7278 int rsize, isize;
7279 tree part;
7281 part = TREE_REALPART (expr);
7282 rsize = native_encode_expr (part, ptr, len, off);
7283 if (off == -1 && rsize == 0)
7284 return 0;
7285 part = TREE_IMAGPART (expr);
7286 if (off != -1)
7287 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7288 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7289 len - rsize, off);
7290 if (off == -1 && isize != rsize)
7291 return 0;
7292 return rsize + isize;
7296 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7297 specified by EXPR into the buffer PTR of length LEN bytes.
7298 Return the number of bytes placed in the buffer, or zero
7299 upon failure. */
7301 static int
7302 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7304 unsigned HOST_WIDE_INT i, count;
7305 int size, offset;
7306 tree itype, elem;
7308 offset = 0;
7309 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7310 return 0;
7311 itype = TREE_TYPE (TREE_TYPE (expr));
7312 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7313 for (i = 0; i < count; i++)
7315 if (off >= size)
7317 off -= size;
7318 continue;
7320 elem = VECTOR_CST_ELT (expr, i);
7321 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7322 len - offset, off);
7323 if ((off == -1 && res != size) || res == 0)
7324 return 0;
7325 offset += res;
7326 if (offset >= len)
7327 return offset;
7328 if (off != -1)
7329 off = 0;
7331 return offset;
7335 /* Subroutine of native_encode_expr. Encode the STRING_CST
7336 specified by EXPR into the buffer PTR of length LEN bytes.
7337 Return the number of bytes placed in the buffer, or zero
7338 upon failure. */
7340 static int
7341 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7343 tree type = TREE_TYPE (expr);
7345 /* Wide-char strings are encoded in target byte-order so native
7346 encoding them is trivial. */
7347 if (BITS_PER_UNIT != CHAR_BIT
7348 || TREE_CODE (type) != ARRAY_TYPE
7349 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7350 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7351 return 0;
7353 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7354 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7355 return 0;
7356 if (off == -1)
7357 off = 0;
7358 if (ptr == NULL)
7359 /* Dry run. */;
7360 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7362 int written = 0;
7363 if (off < TREE_STRING_LENGTH (expr))
7365 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7366 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7368 memset (ptr + written, 0,
7369 MIN (total_bytes - written, len - written));
7371 else
7372 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7373 return MIN (total_bytes - off, len);
7377 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7378 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7379 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7380 anything, just do a dry run. If OFF is not -1 then start
7381 the encoding at byte offset OFF and encode at most LEN bytes.
7382 Return the number of bytes placed in the buffer, or zero upon failure. */
7385 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7387 /* We don't support starting at negative offset and -1 is special. */
7388 if (off < -1)
7389 return 0;
7391 switch (TREE_CODE (expr))
7393 case INTEGER_CST:
7394 return native_encode_int (expr, ptr, len, off);
7396 case REAL_CST:
7397 return native_encode_real (expr, ptr, len, off);
7399 case FIXED_CST:
7400 return native_encode_fixed (expr, ptr, len, off);
7402 case COMPLEX_CST:
7403 return native_encode_complex (expr, ptr, len, off);
7405 case VECTOR_CST:
7406 return native_encode_vector (expr, ptr, len, off);
7408 case STRING_CST:
7409 return native_encode_string (expr, ptr, len, off);
7411 default:
7412 return 0;
7417 /* Subroutine of native_interpret_expr. Interpret the contents of
7418 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7419 If the buffer cannot be interpreted, return NULL_TREE. */
7421 static tree
7422 native_interpret_int (tree type, const unsigned char *ptr, int len)
7424 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7426 if (total_bytes > len
7427 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7428 return NULL_TREE;
7430 wide_int result = wi::from_buffer (ptr, total_bytes);
7432 return wide_int_to_tree (type, result);
7436 /* Subroutine of native_interpret_expr. Interpret the contents of
7437 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7438 If the buffer cannot be interpreted, return NULL_TREE. */
7440 static tree
7441 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7443 scalar_mode mode = SCALAR_TYPE_MODE (type);
7444 int total_bytes = GET_MODE_SIZE (mode);
7445 double_int result;
7446 FIXED_VALUE_TYPE fixed_value;
7448 if (total_bytes > len
7449 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7450 return NULL_TREE;
7452 result = double_int::from_buffer (ptr, total_bytes);
7453 fixed_value = fixed_from_double_int (result, mode);
7455 return build_fixed (type, fixed_value);
7459 /* Subroutine of native_interpret_expr. Interpret the contents of
7460 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7461 If the buffer cannot be interpreted, return NULL_TREE. */
7463 static tree
7464 native_interpret_real (tree type, const unsigned char *ptr, int len)
7466 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7467 int total_bytes = GET_MODE_SIZE (mode);
7468 unsigned char value;
7469 /* There are always 32 bits in each long, no matter the size of
7470 the hosts long. We handle floating point representations with
7471 up to 192 bits. */
7472 REAL_VALUE_TYPE r;
7473 long tmp[6];
7475 if (total_bytes > len || total_bytes > 24)
7476 return NULL_TREE;
7477 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7479 memset (tmp, 0, sizeof (tmp));
7480 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7481 bitpos += BITS_PER_UNIT)
7483 /* Both OFFSET and BYTE index within a long;
7484 bitpos indexes the whole float. */
7485 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7486 if (UNITS_PER_WORD < 4)
7488 int word = byte / UNITS_PER_WORD;
7489 if (WORDS_BIG_ENDIAN)
7490 word = (words - 1) - word;
7491 offset = word * UNITS_PER_WORD;
7492 if (BYTES_BIG_ENDIAN)
7493 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7494 else
7495 offset += byte % UNITS_PER_WORD;
7497 else
7499 offset = byte;
7500 if (BYTES_BIG_ENDIAN)
7502 /* Reverse bytes within each long, or within the entire float
7503 if it's smaller than a long (for HFmode). */
7504 offset = MIN (3, total_bytes - 1) - offset;
7505 gcc_assert (offset >= 0);
7508 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7510 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7513 real_from_target (&r, tmp, mode);
7514 return build_real (type, r);
7518 /* Subroutine of native_interpret_expr. Interpret the contents of
7519 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7520 If the buffer cannot be interpreted, return NULL_TREE. */
7522 static tree
7523 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7525 tree etype, rpart, ipart;
7526 int size;
7528 etype = TREE_TYPE (type);
7529 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7530 if (size * 2 > len)
7531 return NULL_TREE;
7532 rpart = native_interpret_expr (etype, ptr, size);
7533 if (!rpart)
7534 return NULL_TREE;
7535 ipart = native_interpret_expr (etype, ptr+size, size);
7536 if (!ipart)
7537 return NULL_TREE;
7538 return build_complex (type, rpart, ipart);
7542 /* Subroutine of native_interpret_expr. Interpret the contents of
7543 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7544 If the buffer cannot be interpreted, return NULL_TREE. */
7546 static tree
7547 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7549 tree etype, elem;
7550 unsigned int i, size;
7551 unsigned HOST_WIDE_INT count;
7553 etype = TREE_TYPE (type);
7554 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7555 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7556 || size * count > len)
7557 return NULL_TREE;
7559 tree_vector_builder elements (type, count, 1);
7560 for (i = 0; i < count; ++i)
7562 elem = native_interpret_expr (etype, ptr+(i*size), size);
7563 if (!elem)
7564 return NULL_TREE;
7565 elements.quick_push (elem);
7567 return elements.build ();
7571 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7572 the buffer PTR of length LEN as a constant of type TYPE. For
7573 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7574 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7575 return NULL_TREE. */
7577 tree
7578 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7580 switch (TREE_CODE (type))
7582 case INTEGER_TYPE:
7583 case ENUMERAL_TYPE:
7584 case BOOLEAN_TYPE:
7585 case POINTER_TYPE:
7586 case REFERENCE_TYPE:
7587 return native_interpret_int (type, ptr, len);
7589 case REAL_TYPE:
7590 return native_interpret_real (type, ptr, len);
7592 case FIXED_POINT_TYPE:
7593 return native_interpret_fixed (type, ptr, len);
7595 case COMPLEX_TYPE:
7596 return native_interpret_complex (type, ptr, len);
7598 case VECTOR_TYPE:
7599 return native_interpret_vector (type, ptr, len);
7601 default:
7602 return NULL_TREE;
7606 /* Returns true if we can interpret the contents of a native encoding
7607 as TYPE. */
7609 static bool
7610 can_native_interpret_type_p (tree type)
7612 switch (TREE_CODE (type))
7614 case INTEGER_TYPE:
7615 case ENUMERAL_TYPE:
7616 case BOOLEAN_TYPE:
7617 case POINTER_TYPE:
7618 case REFERENCE_TYPE:
7619 case FIXED_POINT_TYPE:
7620 case REAL_TYPE:
7621 case COMPLEX_TYPE:
7622 case VECTOR_TYPE:
7623 return true;
7624 default:
7625 return false;
7630 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7631 TYPE at compile-time. If we're unable to perform the conversion
7632 return NULL_TREE. */
7634 static tree
7635 fold_view_convert_expr (tree type, tree expr)
7637 /* We support up to 512-bit values (for V8DFmode). */
7638 unsigned char buffer[64];
7639 int len;
7641 /* Check that the host and target are sane. */
7642 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7643 return NULL_TREE;
7645 len = native_encode_expr (expr, buffer, sizeof (buffer));
7646 if (len == 0)
7647 return NULL_TREE;
7649 return native_interpret_expr (type, buffer, len);
7652 /* Build an expression for the address of T. Folds away INDIRECT_REF
7653 to avoid confusing the gimplify process. */
7655 tree
7656 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7658 /* The size of the object is not relevant when talking about its address. */
7659 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7660 t = TREE_OPERAND (t, 0);
7662 if (TREE_CODE (t) == INDIRECT_REF)
7664 t = TREE_OPERAND (t, 0);
7666 if (TREE_TYPE (t) != ptrtype)
7667 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7669 else if (TREE_CODE (t) == MEM_REF
7670 && integer_zerop (TREE_OPERAND (t, 1)))
7671 return TREE_OPERAND (t, 0);
7672 else if (TREE_CODE (t) == MEM_REF
7673 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7674 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7675 TREE_OPERAND (t, 0),
7676 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7677 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7679 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7681 if (TREE_TYPE (t) != ptrtype)
7682 t = fold_convert_loc (loc, ptrtype, t);
7684 else
7685 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7687 return t;
7690 /* Build an expression for the address of T. */
7692 tree
7693 build_fold_addr_expr_loc (location_t loc, tree t)
7695 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7697 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7700 /* Fold a unary expression of code CODE and type TYPE with operand
7701 OP0. Return the folded expression if folding is successful.
7702 Otherwise, return NULL_TREE. */
7704 tree
7705 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7707 tree tem;
7708 tree arg0;
7709 enum tree_code_class kind = TREE_CODE_CLASS (code);
7711 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7712 && TREE_CODE_LENGTH (code) == 1);
7714 arg0 = op0;
7715 if (arg0)
7717 if (CONVERT_EXPR_CODE_P (code)
7718 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7720 /* Don't use STRIP_NOPS, because signedness of argument type
7721 matters. */
7722 STRIP_SIGN_NOPS (arg0);
7724 else
7726 /* Strip any conversions that don't change the mode. This
7727 is safe for every expression, except for a comparison
7728 expression because its signedness is derived from its
7729 operands.
7731 Note that this is done as an internal manipulation within
7732 the constant folder, in order to find the simplest
7733 representation of the arguments so that their form can be
7734 studied. In any cases, the appropriate type conversions
7735 should be put back in the tree that will get out of the
7736 constant folder. */
7737 STRIP_NOPS (arg0);
7740 if (CONSTANT_CLASS_P (arg0))
7742 tree tem = const_unop (code, type, arg0);
7743 if (tem)
7745 if (TREE_TYPE (tem) != type)
7746 tem = fold_convert_loc (loc, type, tem);
7747 return tem;
7752 tem = generic_simplify (loc, code, type, op0);
7753 if (tem)
7754 return tem;
7756 if (TREE_CODE_CLASS (code) == tcc_unary)
7758 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7759 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7760 fold_build1_loc (loc, code, type,
7761 fold_convert_loc (loc, TREE_TYPE (op0),
7762 TREE_OPERAND (arg0, 1))));
7763 else if (TREE_CODE (arg0) == COND_EXPR)
7765 tree arg01 = TREE_OPERAND (arg0, 1);
7766 tree arg02 = TREE_OPERAND (arg0, 2);
7767 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7768 arg01 = fold_build1_loc (loc, code, type,
7769 fold_convert_loc (loc,
7770 TREE_TYPE (op0), arg01));
7771 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7772 arg02 = fold_build1_loc (loc, code, type,
7773 fold_convert_loc (loc,
7774 TREE_TYPE (op0), arg02));
7775 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7776 arg01, arg02);
7778 /* If this was a conversion, and all we did was to move into
7779 inside the COND_EXPR, bring it back out. But leave it if
7780 it is a conversion from integer to integer and the
7781 result precision is no wider than a word since such a
7782 conversion is cheap and may be optimized away by combine,
7783 while it couldn't if it were outside the COND_EXPR. Then return
7784 so we don't get into an infinite recursion loop taking the
7785 conversion out and then back in. */
7787 if ((CONVERT_EXPR_CODE_P (code)
7788 || code == NON_LVALUE_EXPR)
7789 && TREE_CODE (tem) == COND_EXPR
7790 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7791 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7792 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7793 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7794 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7795 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7796 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7797 && (INTEGRAL_TYPE_P
7798 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7799 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7800 || flag_syntax_only))
7801 tem = build1_loc (loc, code, type,
7802 build3 (COND_EXPR,
7803 TREE_TYPE (TREE_OPERAND
7804 (TREE_OPERAND (tem, 1), 0)),
7805 TREE_OPERAND (tem, 0),
7806 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7807 TREE_OPERAND (TREE_OPERAND (tem, 2),
7808 0)));
7809 return tem;
7813 switch (code)
7815 case NON_LVALUE_EXPR:
7816 if (!maybe_lvalue_p (op0))
7817 return fold_convert_loc (loc, type, op0);
7818 return NULL_TREE;
7820 CASE_CONVERT:
7821 case FLOAT_EXPR:
7822 case FIX_TRUNC_EXPR:
7823 if (COMPARISON_CLASS_P (op0))
7825 /* If we have (type) (a CMP b) and type is an integral type, return
7826 new expression involving the new type. Canonicalize
7827 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7828 non-integral type.
7829 Do not fold the result as that would not simplify further, also
7830 folding again results in recursions. */
7831 if (TREE_CODE (type) == BOOLEAN_TYPE)
7832 return build2_loc (loc, TREE_CODE (op0), type,
7833 TREE_OPERAND (op0, 0),
7834 TREE_OPERAND (op0, 1));
7835 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7836 && TREE_CODE (type) != VECTOR_TYPE)
7837 return build3_loc (loc, COND_EXPR, type, op0,
7838 constant_boolean_node (true, type),
7839 constant_boolean_node (false, type));
7842 /* Handle (T *)&A.B.C for A being of type T and B and C
7843 living at offset zero. This occurs frequently in
7844 C++ upcasting and then accessing the base. */
7845 if (TREE_CODE (op0) == ADDR_EXPR
7846 && POINTER_TYPE_P (type)
7847 && handled_component_p (TREE_OPERAND (op0, 0)))
7849 poly_int64 bitsize, bitpos;
7850 tree offset;
7851 machine_mode mode;
7852 int unsignedp, reversep, volatilep;
7853 tree base
7854 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7855 &offset, &mode, &unsignedp, &reversep,
7856 &volatilep);
7857 /* If the reference was to a (constant) zero offset, we can use
7858 the address of the base if it has the same base type
7859 as the result type and the pointer type is unqualified. */
7860 if (!offset
7861 && known_eq (bitpos, 0)
7862 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7863 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7864 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7865 return fold_convert_loc (loc, type,
7866 build_fold_addr_expr_loc (loc, base));
7869 if (TREE_CODE (op0) == MODIFY_EXPR
7870 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7871 /* Detect assigning a bitfield. */
7872 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7873 && DECL_BIT_FIELD
7874 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7876 /* Don't leave an assignment inside a conversion
7877 unless assigning a bitfield. */
7878 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7879 /* First do the assignment, then return converted constant. */
7880 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7881 TREE_NO_WARNING (tem) = 1;
7882 TREE_USED (tem) = 1;
7883 return tem;
7886 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7887 constants (if x has signed type, the sign bit cannot be set
7888 in c). This folds extension into the BIT_AND_EXPR.
7889 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7890 very likely don't have maximal range for their precision and this
7891 transformation effectively doesn't preserve non-maximal ranges. */
7892 if (TREE_CODE (type) == INTEGER_TYPE
7893 && TREE_CODE (op0) == BIT_AND_EXPR
7894 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7896 tree and_expr = op0;
7897 tree and0 = TREE_OPERAND (and_expr, 0);
7898 tree and1 = TREE_OPERAND (and_expr, 1);
7899 int change = 0;
7901 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7902 || (TYPE_PRECISION (type)
7903 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7904 change = 1;
7905 else if (TYPE_PRECISION (TREE_TYPE (and1))
7906 <= HOST_BITS_PER_WIDE_INT
7907 && tree_fits_uhwi_p (and1))
7909 unsigned HOST_WIDE_INT cst;
7911 cst = tree_to_uhwi (and1);
7912 cst &= HOST_WIDE_INT_M1U
7913 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7914 change = (cst == 0);
7915 if (change
7916 && !flag_syntax_only
7917 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7918 == ZERO_EXTEND))
7920 tree uns = unsigned_type_for (TREE_TYPE (and0));
7921 and0 = fold_convert_loc (loc, uns, and0);
7922 and1 = fold_convert_loc (loc, uns, and1);
7925 if (change)
7927 tem = force_fit_type (type, wi::to_widest (and1), 0,
7928 TREE_OVERFLOW (and1));
7929 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7930 fold_convert_loc (loc, type, and0), tem);
7934 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7935 cast (T1)X will fold away. We assume that this happens when X itself
7936 is a cast. */
7937 if (POINTER_TYPE_P (type)
7938 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7939 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7941 tree arg00 = TREE_OPERAND (arg0, 0);
7942 tree arg01 = TREE_OPERAND (arg0, 1);
7944 return fold_build_pointer_plus_loc
7945 (loc, fold_convert_loc (loc, type, arg00), arg01);
7948 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7949 of the same precision, and X is an integer type not narrower than
7950 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7951 if (INTEGRAL_TYPE_P (type)
7952 && TREE_CODE (op0) == BIT_NOT_EXPR
7953 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7954 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7955 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7957 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7958 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7959 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7960 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7961 fold_convert_loc (loc, type, tem));
7964 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7965 type of X and Y (integer types only). */
7966 if (INTEGRAL_TYPE_P (type)
7967 && TREE_CODE (op0) == MULT_EXPR
7968 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7969 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7971 /* Be careful not to introduce new overflows. */
7972 tree mult_type;
7973 if (TYPE_OVERFLOW_WRAPS (type))
7974 mult_type = type;
7975 else
7976 mult_type = unsigned_type_for (type);
7978 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7980 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7981 fold_convert_loc (loc, mult_type,
7982 TREE_OPERAND (op0, 0)),
7983 fold_convert_loc (loc, mult_type,
7984 TREE_OPERAND (op0, 1)));
7985 return fold_convert_loc (loc, type, tem);
7989 return NULL_TREE;
7991 case VIEW_CONVERT_EXPR:
7992 if (TREE_CODE (op0) == MEM_REF)
7994 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7995 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7996 tem = fold_build2_loc (loc, MEM_REF, type,
7997 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7998 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7999 return tem;
8002 return NULL_TREE;
8004 case NEGATE_EXPR:
8005 tem = fold_negate_expr (loc, arg0);
8006 if (tem)
8007 return fold_convert_loc (loc, type, tem);
8008 return NULL_TREE;
8010 case ABS_EXPR:
8011 /* Convert fabs((double)float) into (double)fabsf(float). */
8012 if (TREE_CODE (arg0) == NOP_EXPR
8013 && TREE_CODE (type) == REAL_TYPE)
8015 tree targ0 = strip_float_extensions (arg0);
8016 if (targ0 != arg0)
8017 return fold_convert_loc (loc, type,
8018 fold_build1_loc (loc, ABS_EXPR,
8019 TREE_TYPE (targ0),
8020 targ0));
8022 return NULL_TREE;
8024 case BIT_NOT_EXPR:
8025 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8026 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8027 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8028 fold_convert_loc (loc, type,
8029 TREE_OPERAND (arg0, 0)))))
8030 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8031 fold_convert_loc (loc, type,
8032 TREE_OPERAND (arg0, 1)));
8033 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8034 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8035 fold_convert_loc (loc, type,
8036 TREE_OPERAND (arg0, 1)))))
8037 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8038 fold_convert_loc (loc, type,
8039 TREE_OPERAND (arg0, 0)), tem);
8041 return NULL_TREE;
8043 case TRUTH_NOT_EXPR:
8044 /* Note that the operand of this must be an int
8045 and its values must be 0 or 1.
8046 ("true" is a fixed value perhaps depending on the language,
8047 but we don't handle values other than 1 correctly yet.) */
8048 tem = fold_truth_not_expr (loc, arg0);
8049 if (!tem)
8050 return NULL_TREE;
8051 return fold_convert_loc (loc, type, tem);
8053 case INDIRECT_REF:
8054 /* Fold *&X to X if X is an lvalue. */
8055 if (TREE_CODE (op0) == ADDR_EXPR)
8057 tree op00 = TREE_OPERAND (op0, 0);
8058 if ((VAR_P (op00)
8059 || TREE_CODE (op00) == PARM_DECL
8060 || TREE_CODE (op00) == RESULT_DECL)
8061 && !TREE_READONLY (op00))
8062 return op00;
8064 return NULL_TREE;
8066 default:
8067 return NULL_TREE;
8068 } /* switch (code) */
8072 /* If the operation was a conversion do _not_ mark a resulting constant
8073 with TREE_OVERFLOW if the original constant was not. These conversions
8074 have implementation defined behavior and retaining the TREE_OVERFLOW
8075 flag here would confuse later passes such as VRP. */
8076 tree
8077 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8078 tree type, tree op0)
8080 tree res = fold_unary_loc (loc, code, type, op0);
8081 if (res
8082 && TREE_CODE (res) == INTEGER_CST
8083 && TREE_CODE (op0) == INTEGER_CST
8084 && CONVERT_EXPR_CODE_P (code))
8085 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8087 return res;
8090 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8091 operands OP0 and OP1. LOC is the location of the resulting expression.
8092 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8093 Return the folded expression if folding is successful. Otherwise,
8094 return NULL_TREE. */
8095 static tree
8096 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8097 tree arg0, tree arg1, tree op0, tree op1)
8099 tree tem;
8101 /* We only do these simplifications if we are optimizing. */
8102 if (!optimize)
8103 return NULL_TREE;
8105 /* Check for things like (A || B) && (A || C). We can convert this
8106 to A || (B && C). Note that either operator can be any of the four
8107 truth and/or operations and the transformation will still be
8108 valid. Also note that we only care about order for the
8109 ANDIF and ORIF operators. If B contains side effects, this
8110 might change the truth-value of A. */
8111 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8112 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8113 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8114 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8115 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8116 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8118 tree a00 = TREE_OPERAND (arg0, 0);
8119 tree a01 = TREE_OPERAND (arg0, 1);
8120 tree a10 = TREE_OPERAND (arg1, 0);
8121 tree a11 = TREE_OPERAND (arg1, 1);
8122 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8123 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8124 && (code == TRUTH_AND_EXPR
8125 || code == TRUTH_OR_EXPR));
8127 if (operand_equal_p (a00, a10, 0))
8128 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8129 fold_build2_loc (loc, code, type, a01, a11));
8130 else if (commutative && operand_equal_p (a00, a11, 0))
8131 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8132 fold_build2_loc (loc, code, type, a01, a10));
8133 else if (commutative && operand_equal_p (a01, a10, 0))
8134 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8135 fold_build2_loc (loc, code, type, a00, a11));
8137 /* This case if tricky because we must either have commutative
8138 operators or else A10 must not have side-effects. */
8140 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8141 && operand_equal_p (a01, a11, 0))
8142 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8143 fold_build2_loc (loc, code, type, a00, a10),
8144 a01);
8147 /* See if we can build a range comparison. */
8148 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8149 return tem;
8151 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8152 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8154 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8155 if (tem)
8156 return fold_build2_loc (loc, code, type, tem, arg1);
8159 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8160 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8162 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8163 if (tem)
8164 return fold_build2_loc (loc, code, type, arg0, tem);
8167 /* Check for the possibility of merging component references. If our
8168 lhs is another similar operation, try to merge its rhs with our
8169 rhs. Then try to merge our lhs and rhs. */
8170 if (TREE_CODE (arg0) == code
8171 && (tem = fold_truth_andor_1 (loc, code, type,
8172 TREE_OPERAND (arg0, 1), arg1)) != 0)
8173 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8175 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8176 return tem;
8178 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8179 && !flag_sanitize_coverage
8180 && (code == TRUTH_AND_EXPR
8181 || code == TRUTH_ANDIF_EXPR
8182 || code == TRUTH_OR_EXPR
8183 || code == TRUTH_ORIF_EXPR))
8185 enum tree_code ncode, icode;
8187 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8188 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8189 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8191 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8192 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8193 We don't want to pack more than two leafs to a non-IF AND/OR
8194 expression.
8195 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8196 equal to IF-CODE, then we don't want to add right-hand operand.
8197 If the inner right-hand side of left-hand operand has
8198 side-effects, or isn't simple, then we can't add to it,
8199 as otherwise we might destroy if-sequence. */
8200 if (TREE_CODE (arg0) == icode
8201 && simple_operand_p_2 (arg1)
8202 /* Needed for sequence points to handle trappings, and
8203 side-effects. */
8204 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8206 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8207 arg1);
8208 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8209 tem);
8211 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8212 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8213 else if (TREE_CODE (arg1) == icode
8214 && simple_operand_p_2 (arg0)
8215 /* Needed for sequence points to handle trappings, and
8216 side-effects. */
8217 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8219 tem = fold_build2_loc (loc, ncode, type,
8220 arg0, TREE_OPERAND (arg1, 0));
8221 return fold_build2_loc (loc, icode, type, tem,
8222 TREE_OPERAND (arg1, 1));
8224 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8225 into (A OR B).
8226 For sequence point consistancy, we need to check for trapping,
8227 and side-effects. */
8228 else if (code == icode && simple_operand_p_2 (arg0)
8229 && simple_operand_p_2 (arg1))
8230 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8233 return NULL_TREE;
8236 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8237 by changing CODE to reduce the magnitude of constants involved in
8238 ARG0 of the comparison.
8239 Returns a canonicalized comparison tree if a simplification was
8240 possible, otherwise returns NULL_TREE.
8241 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8242 valid if signed overflow is undefined. */
8244 static tree
8245 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8246 tree arg0, tree arg1,
8247 bool *strict_overflow_p)
8249 enum tree_code code0 = TREE_CODE (arg0);
8250 tree t, cst0 = NULL_TREE;
8251 int sgn0;
8253 /* Match A +- CST code arg1. We can change this only if overflow
8254 is undefined. */
8255 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8256 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8257 /* In principle pointers also have undefined overflow behavior,
8258 but that causes problems elsewhere. */
8259 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8260 && (code0 == MINUS_EXPR
8261 || code0 == PLUS_EXPR)
8262 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8263 return NULL_TREE;
8265 /* Identify the constant in arg0 and its sign. */
8266 cst0 = TREE_OPERAND (arg0, 1);
8267 sgn0 = tree_int_cst_sgn (cst0);
8269 /* Overflowed constants and zero will cause problems. */
8270 if (integer_zerop (cst0)
8271 || TREE_OVERFLOW (cst0))
8272 return NULL_TREE;
8274 /* See if we can reduce the magnitude of the constant in
8275 arg0 by changing the comparison code. */
8276 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8277 if (code == LT_EXPR
8278 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8279 code = LE_EXPR;
8280 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8281 else if (code == GT_EXPR
8282 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8283 code = GE_EXPR;
8284 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8285 else if (code == LE_EXPR
8286 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8287 code = LT_EXPR;
8288 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8289 else if (code == GE_EXPR
8290 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8291 code = GT_EXPR;
8292 else
8293 return NULL_TREE;
8294 *strict_overflow_p = true;
8296 /* Now build the constant reduced in magnitude. But not if that
8297 would produce one outside of its types range. */
8298 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8299 && ((sgn0 == 1
8300 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8301 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8302 || (sgn0 == -1
8303 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8304 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8305 return NULL_TREE;
8307 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8308 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8309 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8310 t = fold_convert (TREE_TYPE (arg1), t);
8312 return fold_build2_loc (loc, code, type, t, arg1);
8315 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8316 overflow further. Try to decrease the magnitude of constants involved
8317 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8318 and put sole constants at the second argument position.
8319 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8321 static tree
8322 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8323 tree arg0, tree arg1)
8325 tree t;
8326 bool strict_overflow_p;
8327 const char * const warnmsg = G_("assuming signed overflow does not occur "
8328 "when reducing constant in comparison");
8330 /* Try canonicalization by simplifying arg0. */
8331 strict_overflow_p = false;
8332 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8333 &strict_overflow_p);
8334 if (t)
8336 if (strict_overflow_p)
8337 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8338 return t;
8341 /* Try canonicalization by simplifying arg1 using the swapped
8342 comparison. */
8343 code = swap_tree_comparison (code);
8344 strict_overflow_p = false;
8345 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8346 &strict_overflow_p);
8347 if (t && strict_overflow_p)
8348 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8349 return t;
8352 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8353 space. This is used to avoid issuing overflow warnings for
8354 expressions like &p->x which can not wrap. */
8356 static bool
8357 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8359 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8360 return true;
8362 if (maybe_lt (bitpos, 0))
8363 return true;
8365 poly_wide_int wi_offset;
8366 int precision = TYPE_PRECISION (TREE_TYPE (base));
8367 if (offset == NULL_TREE)
8368 wi_offset = wi::zero (precision);
8369 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8370 return true;
8371 else
8372 wi_offset = wi::to_poly_wide (offset);
8374 bool overflow;
8375 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8376 precision);
8377 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8378 if (overflow)
8379 return true;
8381 poly_uint64 total_hwi, size;
8382 if (!total.to_uhwi (&total_hwi)
8383 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8384 &size)
8385 || known_eq (size, 0U))
8386 return true;
8388 if (known_le (total_hwi, size))
8389 return false;
8391 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8392 array. */
8393 if (TREE_CODE (base) == ADDR_EXPR
8394 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8395 &size)
8396 && maybe_ne (size, 0U)
8397 && known_le (total_hwi, size))
8398 return false;
8400 return true;
8403 /* Return a positive integer when the symbol DECL is known to have
8404 a nonzero address, zero when it's known not to (e.g., it's a weak
8405 symbol), and a negative integer when the symbol is not yet in the
8406 symbol table and so whether or not its address is zero is unknown.
8407 For function local objects always return positive integer. */
8408 static int
8409 maybe_nonzero_address (tree decl)
8411 if (DECL_P (decl) && decl_in_symtab_p (decl))
8412 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8413 return symbol->nonzero_address ();
8415 /* Function local objects are never NULL. */
8416 if (DECL_P (decl)
8417 && (DECL_CONTEXT (decl)
8418 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8419 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8420 return 1;
8422 return -1;
8425 /* Subroutine of fold_binary. This routine performs all of the
8426 transformations that are common to the equality/inequality
8427 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8428 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8429 fold_binary should call fold_binary. Fold a comparison with
8430 tree code CODE and type TYPE with operands OP0 and OP1. Return
8431 the folded comparison or NULL_TREE. */
8433 static tree
8434 fold_comparison (location_t loc, enum tree_code code, tree type,
8435 tree op0, tree op1)
8437 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8438 tree arg0, arg1, tem;
8440 arg0 = op0;
8441 arg1 = op1;
8443 STRIP_SIGN_NOPS (arg0);
8444 STRIP_SIGN_NOPS (arg1);
8446 /* For comparisons of pointers we can decompose it to a compile time
8447 comparison of the base objects and the offsets into the object.
8448 This requires at least one operand being an ADDR_EXPR or a
8449 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8450 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8451 && (TREE_CODE (arg0) == ADDR_EXPR
8452 || TREE_CODE (arg1) == ADDR_EXPR
8453 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8454 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8456 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8457 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8458 machine_mode mode;
8459 int volatilep, reversep, unsignedp;
8460 bool indirect_base0 = false, indirect_base1 = false;
8462 /* Get base and offset for the access. Strip ADDR_EXPR for
8463 get_inner_reference, but put it back by stripping INDIRECT_REF
8464 off the base object if possible. indirect_baseN will be true
8465 if baseN is not an address but refers to the object itself. */
8466 base0 = arg0;
8467 if (TREE_CODE (arg0) == ADDR_EXPR)
8469 base0
8470 = get_inner_reference (TREE_OPERAND (arg0, 0),
8471 &bitsize, &bitpos0, &offset0, &mode,
8472 &unsignedp, &reversep, &volatilep);
8473 if (TREE_CODE (base0) == INDIRECT_REF)
8474 base0 = TREE_OPERAND (base0, 0);
8475 else
8476 indirect_base0 = true;
8478 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8480 base0 = TREE_OPERAND (arg0, 0);
8481 STRIP_SIGN_NOPS (base0);
8482 if (TREE_CODE (base0) == ADDR_EXPR)
8484 base0
8485 = get_inner_reference (TREE_OPERAND (base0, 0),
8486 &bitsize, &bitpos0, &offset0, &mode,
8487 &unsignedp, &reversep, &volatilep);
8488 if (TREE_CODE (base0) == INDIRECT_REF)
8489 base0 = TREE_OPERAND (base0, 0);
8490 else
8491 indirect_base0 = true;
8493 if (offset0 == NULL_TREE || integer_zerop (offset0))
8494 offset0 = TREE_OPERAND (arg0, 1);
8495 else
8496 offset0 = size_binop (PLUS_EXPR, offset0,
8497 TREE_OPERAND (arg0, 1));
8498 if (poly_int_tree_p (offset0))
8500 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8501 TYPE_PRECISION (sizetype));
8502 tem <<= LOG2_BITS_PER_UNIT;
8503 tem += bitpos0;
8504 if (tem.to_shwi (&bitpos0))
8505 offset0 = NULL_TREE;
8509 base1 = arg1;
8510 if (TREE_CODE (arg1) == ADDR_EXPR)
8512 base1
8513 = get_inner_reference (TREE_OPERAND (arg1, 0),
8514 &bitsize, &bitpos1, &offset1, &mode,
8515 &unsignedp, &reversep, &volatilep);
8516 if (TREE_CODE (base1) == INDIRECT_REF)
8517 base1 = TREE_OPERAND (base1, 0);
8518 else
8519 indirect_base1 = true;
8521 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8523 base1 = TREE_OPERAND (arg1, 0);
8524 STRIP_SIGN_NOPS (base1);
8525 if (TREE_CODE (base1) == ADDR_EXPR)
8527 base1
8528 = get_inner_reference (TREE_OPERAND (base1, 0),
8529 &bitsize, &bitpos1, &offset1, &mode,
8530 &unsignedp, &reversep, &volatilep);
8531 if (TREE_CODE (base1) == INDIRECT_REF)
8532 base1 = TREE_OPERAND (base1, 0);
8533 else
8534 indirect_base1 = true;
8536 if (offset1 == NULL_TREE || integer_zerop (offset1))
8537 offset1 = TREE_OPERAND (arg1, 1);
8538 else
8539 offset1 = size_binop (PLUS_EXPR, offset1,
8540 TREE_OPERAND (arg1, 1));
8541 if (poly_int_tree_p (offset1))
8543 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8544 TYPE_PRECISION (sizetype));
8545 tem <<= LOG2_BITS_PER_UNIT;
8546 tem += bitpos1;
8547 if (tem.to_shwi (&bitpos1))
8548 offset1 = NULL_TREE;
8552 /* If we have equivalent bases we might be able to simplify. */
8553 if (indirect_base0 == indirect_base1
8554 && operand_equal_p (base0, base1,
8555 indirect_base0 ? OEP_ADDRESS_OF : 0))
8557 /* We can fold this expression to a constant if the non-constant
8558 offset parts are equal. */
8559 if ((offset0 == offset1
8560 || (offset0 && offset1
8561 && operand_equal_p (offset0, offset1, 0)))
8562 && (equality_code
8563 || (indirect_base0
8564 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8565 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8567 if (!equality_code
8568 && maybe_ne (bitpos0, bitpos1)
8569 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8570 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8571 fold_overflow_warning (("assuming pointer wraparound does not "
8572 "occur when comparing P +- C1 with "
8573 "P +- C2"),
8574 WARN_STRICT_OVERFLOW_CONDITIONAL);
8576 switch (code)
8578 case EQ_EXPR:
8579 if (known_eq (bitpos0, bitpos1))
8580 return constant_boolean_node (true, type);
8581 if (known_ne (bitpos0, bitpos1))
8582 return constant_boolean_node (false, type);
8583 break;
8584 case NE_EXPR:
8585 if (known_ne (bitpos0, bitpos1))
8586 return constant_boolean_node (true, type);
8587 if (known_eq (bitpos0, bitpos1))
8588 return constant_boolean_node (false, type);
8589 break;
8590 case LT_EXPR:
8591 if (known_lt (bitpos0, bitpos1))
8592 return constant_boolean_node (true, type);
8593 if (known_ge (bitpos0, bitpos1))
8594 return constant_boolean_node (false, type);
8595 break;
8596 case LE_EXPR:
8597 if (known_le (bitpos0, bitpos1))
8598 return constant_boolean_node (true, type);
8599 if (known_gt (bitpos0, bitpos1))
8600 return constant_boolean_node (false, type);
8601 break;
8602 case GE_EXPR:
8603 if (known_ge (bitpos0, bitpos1))
8604 return constant_boolean_node (true, type);
8605 if (known_lt (bitpos0, bitpos1))
8606 return constant_boolean_node (false, type);
8607 break;
8608 case GT_EXPR:
8609 if (known_gt (bitpos0, bitpos1))
8610 return constant_boolean_node (true, type);
8611 if (known_le (bitpos0, bitpos1))
8612 return constant_boolean_node (false, type);
8613 break;
8614 default:;
8617 /* We can simplify the comparison to a comparison of the variable
8618 offset parts if the constant offset parts are equal.
8619 Be careful to use signed sizetype here because otherwise we
8620 mess with array offsets in the wrong way. This is possible
8621 because pointer arithmetic is restricted to retain within an
8622 object and overflow on pointer differences is undefined as of
8623 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8624 else if (known_eq (bitpos0, bitpos1)
8625 && (equality_code
8626 || (indirect_base0
8627 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8628 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8630 /* By converting to signed sizetype we cover middle-end pointer
8631 arithmetic which operates on unsigned pointer types of size
8632 type size and ARRAY_REF offsets which are properly sign or
8633 zero extended from their type in case it is narrower than
8634 sizetype. */
8635 if (offset0 == NULL_TREE)
8636 offset0 = build_int_cst (ssizetype, 0);
8637 else
8638 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8639 if (offset1 == NULL_TREE)
8640 offset1 = build_int_cst (ssizetype, 0);
8641 else
8642 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8644 if (!equality_code
8645 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8646 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8647 fold_overflow_warning (("assuming pointer wraparound does not "
8648 "occur when comparing P +- C1 with "
8649 "P +- C2"),
8650 WARN_STRICT_OVERFLOW_COMPARISON);
8652 return fold_build2_loc (loc, code, type, offset0, offset1);
8655 /* For equal offsets we can simplify to a comparison of the
8656 base addresses. */
8657 else if (known_eq (bitpos0, bitpos1)
8658 && (indirect_base0
8659 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8660 && (indirect_base1
8661 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8662 && ((offset0 == offset1)
8663 || (offset0 && offset1
8664 && operand_equal_p (offset0, offset1, 0))))
8666 if (indirect_base0)
8667 base0 = build_fold_addr_expr_loc (loc, base0);
8668 if (indirect_base1)
8669 base1 = build_fold_addr_expr_loc (loc, base1);
8670 return fold_build2_loc (loc, code, type, base0, base1);
8672 /* Comparison between an ordinary (non-weak) symbol and a null
8673 pointer can be eliminated since such symbols must have a non
8674 null address. In C, relational expressions between pointers
8675 to objects and null pointers are undefined. The results
8676 below follow the C++ rules with the additional property that
8677 every object pointer compares greater than a null pointer.
8679 else if (((DECL_P (base0)
8680 && maybe_nonzero_address (base0) > 0
8681 /* Avoid folding references to struct members at offset 0 to
8682 prevent tests like '&ptr->firstmember == 0' from getting
8683 eliminated. When ptr is null, although the -> expression
8684 is strictly speaking invalid, GCC retains it as a matter
8685 of QoI. See PR c/44555. */
8686 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8687 || CONSTANT_CLASS_P (base0))
8688 && indirect_base0
8689 /* The caller guarantees that when one of the arguments is
8690 constant (i.e., null in this case) it is second. */
8691 && integer_zerop (arg1))
8693 switch (code)
8695 case EQ_EXPR:
8696 case LE_EXPR:
8697 case LT_EXPR:
8698 return constant_boolean_node (false, type);
8699 case GE_EXPR:
8700 case GT_EXPR:
8701 case NE_EXPR:
8702 return constant_boolean_node (true, type);
8703 default:
8704 gcc_unreachable ();
8709 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8710 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8711 the resulting offset is smaller in absolute value than the
8712 original one and has the same sign. */
8713 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8714 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8715 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8716 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8717 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8718 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8719 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8720 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8722 tree const1 = TREE_OPERAND (arg0, 1);
8723 tree const2 = TREE_OPERAND (arg1, 1);
8724 tree variable1 = TREE_OPERAND (arg0, 0);
8725 tree variable2 = TREE_OPERAND (arg1, 0);
8726 tree cst;
8727 const char * const warnmsg = G_("assuming signed overflow does not "
8728 "occur when combining constants around "
8729 "a comparison");
8731 /* Put the constant on the side where it doesn't overflow and is
8732 of lower absolute value and of same sign than before. */
8733 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8734 ? MINUS_EXPR : PLUS_EXPR,
8735 const2, const1);
8736 if (!TREE_OVERFLOW (cst)
8737 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8738 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8740 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8741 return fold_build2_loc (loc, code, type,
8742 variable1,
8743 fold_build2_loc (loc, TREE_CODE (arg1),
8744 TREE_TYPE (arg1),
8745 variable2, cst));
8748 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8749 ? MINUS_EXPR : PLUS_EXPR,
8750 const1, const2);
8751 if (!TREE_OVERFLOW (cst)
8752 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8753 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8755 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8756 return fold_build2_loc (loc, code, type,
8757 fold_build2_loc (loc, TREE_CODE (arg0),
8758 TREE_TYPE (arg0),
8759 variable1, cst),
8760 variable2);
8764 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8765 if (tem)
8766 return tem;
8768 /* If we are comparing an expression that just has comparisons
8769 of two integer values, arithmetic expressions of those comparisons,
8770 and constants, we can simplify it. There are only three cases
8771 to check: the two values can either be equal, the first can be
8772 greater, or the second can be greater. Fold the expression for
8773 those three values. Since each value must be 0 or 1, we have
8774 eight possibilities, each of which corresponds to the constant 0
8775 or 1 or one of the six possible comparisons.
8777 This handles common cases like (a > b) == 0 but also handles
8778 expressions like ((x > y) - (y > x)) > 0, which supposedly
8779 occur in macroized code. */
8781 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8783 tree cval1 = 0, cval2 = 0;
8784 int save_p = 0;
8786 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8787 /* Don't handle degenerate cases here; they should already
8788 have been handled anyway. */
8789 && cval1 != 0 && cval2 != 0
8790 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8791 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8792 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8793 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8794 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8795 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8796 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8798 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8799 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8801 /* We can't just pass T to eval_subst in case cval1 or cval2
8802 was the same as ARG1. */
8804 tree high_result
8805 = fold_build2_loc (loc, code, type,
8806 eval_subst (loc, arg0, cval1, maxval,
8807 cval2, minval),
8808 arg1);
8809 tree equal_result
8810 = fold_build2_loc (loc, code, type,
8811 eval_subst (loc, arg0, cval1, maxval,
8812 cval2, maxval),
8813 arg1);
8814 tree low_result
8815 = fold_build2_loc (loc, code, type,
8816 eval_subst (loc, arg0, cval1, minval,
8817 cval2, maxval),
8818 arg1);
8820 /* All three of these results should be 0 or 1. Confirm they are.
8821 Then use those values to select the proper code to use. */
8823 if (TREE_CODE (high_result) == INTEGER_CST
8824 && TREE_CODE (equal_result) == INTEGER_CST
8825 && TREE_CODE (low_result) == INTEGER_CST)
8827 /* Make a 3-bit mask with the high-order bit being the
8828 value for `>', the next for '=', and the low for '<'. */
8829 switch ((integer_onep (high_result) * 4)
8830 + (integer_onep (equal_result) * 2)
8831 + integer_onep (low_result))
8833 case 0:
8834 /* Always false. */
8835 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8836 case 1:
8837 code = LT_EXPR;
8838 break;
8839 case 2:
8840 code = EQ_EXPR;
8841 break;
8842 case 3:
8843 code = LE_EXPR;
8844 break;
8845 case 4:
8846 code = GT_EXPR;
8847 break;
8848 case 5:
8849 code = NE_EXPR;
8850 break;
8851 case 6:
8852 code = GE_EXPR;
8853 break;
8854 case 7:
8855 /* Always true. */
8856 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8859 if (save_p)
8861 tem = save_expr (build2 (code, type, cval1, cval2));
8862 protected_set_expr_location (tem, loc);
8863 return tem;
8865 return fold_build2_loc (loc, code, type, cval1, cval2);
8870 return NULL_TREE;
8874 /* Subroutine of fold_binary. Optimize complex multiplications of the
8875 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8876 argument EXPR represents the expression "z" of type TYPE. */
8878 static tree
8879 fold_mult_zconjz (location_t loc, tree type, tree expr)
8881 tree itype = TREE_TYPE (type);
8882 tree rpart, ipart, tem;
8884 if (TREE_CODE (expr) == COMPLEX_EXPR)
8886 rpart = TREE_OPERAND (expr, 0);
8887 ipart = TREE_OPERAND (expr, 1);
8889 else if (TREE_CODE (expr) == COMPLEX_CST)
8891 rpart = TREE_REALPART (expr);
8892 ipart = TREE_IMAGPART (expr);
8894 else
8896 expr = save_expr (expr);
8897 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8898 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8901 rpart = save_expr (rpart);
8902 ipart = save_expr (ipart);
8903 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8904 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8905 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8906 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8907 build_zero_cst (itype));
8911 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8912 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8913 true if successful. */
8915 static bool
8916 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8918 unsigned HOST_WIDE_INT i, nunits;
8920 if (TREE_CODE (arg) == VECTOR_CST
8921 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8923 for (i = 0; i < nunits; ++i)
8924 elts[i] = VECTOR_CST_ELT (arg, i);
8926 else if (TREE_CODE (arg) == CONSTRUCTOR)
8928 constructor_elt *elt;
8930 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8931 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8932 return false;
8933 else
8934 elts[i] = elt->value;
8936 else
8937 return false;
8938 for (; i < nelts; i++)
8939 elts[i]
8940 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8941 return true;
8944 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8945 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8946 NULL_TREE otherwise. */
8948 static tree
8949 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8951 unsigned int i;
8952 unsigned HOST_WIDE_INT nelts;
8953 bool need_ctor = false;
8955 if (!sel.length ().is_constant (&nelts))
8956 return NULL_TREE;
8957 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
8958 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
8959 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
8960 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8961 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8962 return NULL_TREE;
8964 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8965 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8966 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8967 return NULL_TREE;
8969 tree_vector_builder out_elts (type, nelts, 1);
8970 for (i = 0; i < nelts; i++)
8972 HOST_WIDE_INT index;
8973 if (!sel[i].is_constant (&index))
8974 return NULL_TREE;
8975 if (!CONSTANT_CLASS_P (in_elts[index]))
8976 need_ctor = true;
8977 out_elts.quick_push (unshare_expr (in_elts[index]));
8980 if (need_ctor)
8982 vec<constructor_elt, va_gc> *v;
8983 vec_alloc (v, nelts);
8984 for (i = 0; i < nelts; i++)
8985 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8986 return build_constructor (type, v);
8988 else
8989 return out_elts.build ();
8992 /* Try to fold a pointer difference of type TYPE two address expressions of
8993 array references AREF0 and AREF1 using location LOC. Return a
8994 simplified expression for the difference or NULL_TREE. */
8996 static tree
8997 fold_addr_of_array_ref_difference (location_t loc, tree type,
8998 tree aref0, tree aref1,
8999 bool use_pointer_diff)
9001 tree base0 = TREE_OPERAND (aref0, 0);
9002 tree base1 = TREE_OPERAND (aref1, 0);
9003 tree base_offset = build_int_cst (type, 0);
9005 /* If the bases are array references as well, recurse. If the bases
9006 are pointer indirections compute the difference of the pointers.
9007 If the bases are equal, we are set. */
9008 if ((TREE_CODE (base0) == ARRAY_REF
9009 && TREE_CODE (base1) == ARRAY_REF
9010 && (base_offset
9011 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9012 use_pointer_diff)))
9013 || (INDIRECT_REF_P (base0)
9014 && INDIRECT_REF_P (base1)
9015 && (base_offset
9016 = use_pointer_diff
9017 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9018 TREE_OPERAND (base0, 0),
9019 TREE_OPERAND (base1, 0))
9020 : fold_binary_loc (loc, MINUS_EXPR, type,
9021 fold_convert (type,
9022 TREE_OPERAND (base0, 0)),
9023 fold_convert (type,
9024 TREE_OPERAND (base1, 0)))))
9025 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9027 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9028 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9029 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9030 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9031 return fold_build2_loc (loc, PLUS_EXPR, type,
9032 base_offset,
9033 fold_build2_loc (loc, MULT_EXPR, type,
9034 diff, esz));
9036 return NULL_TREE;
9039 /* If the real or vector real constant CST of type TYPE has an exact
9040 inverse, return it, else return NULL. */
9042 tree
9043 exact_inverse (tree type, tree cst)
9045 REAL_VALUE_TYPE r;
9046 tree unit_type;
9047 machine_mode mode;
9049 switch (TREE_CODE (cst))
9051 case REAL_CST:
9052 r = TREE_REAL_CST (cst);
9054 if (exact_real_inverse (TYPE_MODE (type), &r))
9055 return build_real (type, r);
9057 return NULL_TREE;
9059 case VECTOR_CST:
9061 unit_type = TREE_TYPE (type);
9062 mode = TYPE_MODE (unit_type);
9064 tree_vector_builder elts;
9065 if (!elts.new_unary_operation (type, cst, false))
9066 return NULL_TREE;
9067 unsigned int count = elts.encoded_nelts ();
9068 for (unsigned int i = 0; i < count; ++i)
9070 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9071 if (!exact_real_inverse (mode, &r))
9072 return NULL_TREE;
9073 elts.quick_push (build_real (unit_type, r));
9076 return elts.build ();
9079 default:
9080 return NULL_TREE;
9084 /* Mask out the tz least significant bits of X of type TYPE where
9085 tz is the number of trailing zeroes in Y. */
9086 static wide_int
9087 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9089 int tz = wi::ctz (y);
9090 if (tz > 0)
9091 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9092 return x;
9095 /* Return true when T is an address and is known to be nonzero.
9096 For floating point we further ensure that T is not denormal.
9097 Similar logic is present in nonzero_address in rtlanal.h.
9099 If the return value is based on the assumption that signed overflow
9100 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9101 change *STRICT_OVERFLOW_P. */
9103 static bool
9104 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9106 tree type = TREE_TYPE (t);
9107 enum tree_code code;
9109 /* Doing something useful for floating point would need more work. */
9110 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9111 return false;
9113 code = TREE_CODE (t);
9114 switch (TREE_CODE_CLASS (code))
9116 case tcc_unary:
9117 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9118 strict_overflow_p);
9119 case tcc_binary:
9120 case tcc_comparison:
9121 return tree_binary_nonzero_warnv_p (code, type,
9122 TREE_OPERAND (t, 0),
9123 TREE_OPERAND (t, 1),
9124 strict_overflow_p);
9125 case tcc_constant:
9126 case tcc_declaration:
9127 case tcc_reference:
9128 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9130 default:
9131 break;
9134 switch (code)
9136 case TRUTH_NOT_EXPR:
9137 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9138 strict_overflow_p);
9140 case TRUTH_AND_EXPR:
9141 case TRUTH_OR_EXPR:
9142 case TRUTH_XOR_EXPR:
9143 return tree_binary_nonzero_warnv_p (code, type,
9144 TREE_OPERAND (t, 0),
9145 TREE_OPERAND (t, 1),
9146 strict_overflow_p);
9148 case COND_EXPR:
9149 case CONSTRUCTOR:
9150 case OBJ_TYPE_REF:
9151 case ASSERT_EXPR:
9152 case ADDR_EXPR:
9153 case WITH_SIZE_EXPR:
9154 case SSA_NAME:
9155 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9157 case COMPOUND_EXPR:
9158 case MODIFY_EXPR:
9159 case BIND_EXPR:
9160 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9161 strict_overflow_p);
9163 case SAVE_EXPR:
9164 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9165 strict_overflow_p);
9167 case CALL_EXPR:
9169 tree fndecl = get_callee_fndecl (t);
9170 if (!fndecl) return false;
9171 if (flag_delete_null_pointer_checks && !flag_check_new
9172 && DECL_IS_OPERATOR_NEW (fndecl)
9173 && !TREE_NOTHROW (fndecl))
9174 return true;
9175 if (flag_delete_null_pointer_checks
9176 && lookup_attribute ("returns_nonnull",
9177 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9178 return true;
9179 return alloca_call_p (t);
9182 default:
9183 break;
9185 return false;
9188 /* Return true when T is an address and is known to be nonzero.
9189 Handle warnings about undefined signed overflow. */
9191 bool
9192 tree_expr_nonzero_p (tree t)
9194 bool ret, strict_overflow_p;
9196 strict_overflow_p = false;
9197 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9198 if (strict_overflow_p)
9199 fold_overflow_warning (("assuming signed overflow does not occur when "
9200 "determining that expression is always "
9201 "non-zero"),
9202 WARN_STRICT_OVERFLOW_MISC);
9203 return ret;
9206 /* Return true if T is known not to be equal to an integer W. */
9208 bool
9209 expr_not_equal_to (tree t, const wide_int &w)
9211 wide_int min, max, nz;
9212 value_range_type rtype;
9213 switch (TREE_CODE (t))
9215 case INTEGER_CST:
9216 return wi::to_wide (t) != w;
9218 case SSA_NAME:
9219 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9220 return false;
9221 rtype = get_range_info (t, &min, &max);
9222 if (rtype == VR_RANGE)
9224 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9225 return true;
9226 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9227 return true;
9229 else if (rtype == VR_ANTI_RANGE
9230 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9231 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9232 return true;
9233 /* If T has some known zero bits and W has any of those bits set,
9234 then T is known not to be equal to W. */
9235 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9236 TYPE_PRECISION (TREE_TYPE (t))), 0))
9237 return true;
9238 return false;
9240 default:
9241 return false;
9245 /* Fold a binary expression of code CODE and type TYPE with operands
9246 OP0 and OP1. LOC is the location of the resulting expression.
9247 Return the folded expression if folding is successful. Otherwise,
9248 return NULL_TREE. */
9250 tree
9251 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9252 tree op0, tree op1)
9254 enum tree_code_class kind = TREE_CODE_CLASS (code);
9255 tree arg0, arg1, tem;
9256 tree t1 = NULL_TREE;
9257 bool strict_overflow_p;
9258 unsigned int prec;
9260 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9261 && TREE_CODE_LENGTH (code) == 2
9262 && op0 != NULL_TREE
9263 && op1 != NULL_TREE);
9265 arg0 = op0;
9266 arg1 = op1;
9268 /* Strip any conversions that don't change the mode. This is
9269 safe for every expression, except for a comparison expression
9270 because its signedness is derived from its operands. So, in
9271 the latter case, only strip conversions that don't change the
9272 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9273 preserved.
9275 Note that this is done as an internal manipulation within the
9276 constant folder, in order to find the simplest representation
9277 of the arguments so that their form can be studied. In any
9278 cases, the appropriate type conversions should be put back in
9279 the tree that will get out of the constant folder. */
9281 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9283 STRIP_SIGN_NOPS (arg0);
9284 STRIP_SIGN_NOPS (arg1);
9286 else
9288 STRIP_NOPS (arg0);
9289 STRIP_NOPS (arg1);
9292 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9293 constant but we can't do arithmetic on them. */
9294 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9296 tem = const_binop (code, type, arg0, arg1);
9297 if (tem != NULL_TREE)
9299 if (TREE_TYPE (tem) != type)
9300 tem = fold_convert_loc (loc, type, tem);
9301 return tem;
9305 /* If this is a commutative operation, and ARG0 is a constant, move it
9306 to ARG1 to reduce the number of tests below. */
9307 if (commutative_tree_code (code)
9308 && tree_swap_operands_p (arg0, arg1))
9309 return fold_build2_loc (loc, code, type, op1, op0);
9311 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9312 to ARG1 to reduce the number of tests below. */
9313 if (kind == tcc_comparison
9314 && tree_swap_operands_p (arg0, arg1))
9315 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9317 tem = generic_simplify (loc, code, type, op0, op1);
9318 if (tem)
9319 return tem;
9321 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9323 First check for cases where an arithmetic operation is applied to a
9324 compound, conditional, or comparison operation. Push the arithmetic
9325 operation inside the compound or conditional to see if any folding
9326 can then be done. Convert comparison to conditional for this purpose.
9327 The also optimizes non-constant cases that used to be done in
9328 expand_expr.
9330 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9331 one of the operands is a comparison and the other is a comparison, a
9332 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9333 code below would make the expression more complex. Change it to a
9334 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9335 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9337 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9338 || code == EQ_EXPR || code == NE_EXPR)
9339 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9340 && ((truth_value_p (TREE_CODE (arg0))
9341 && (truth_value_p (TREE_CODE (arg1))
9342 || (TREE_CODE (arg1) == BIT_AND_EXPR
9343 && integer_onep (TREE_OPERAND (arg1, 1)))))
9344 || (truth_value_p (TREE_CODE (arg1))
9345 && (truth_value_p (TREE_CODE (arg0))
9346 || (TREE_CODE (arg0) == BIT_AND_EXPR
9347 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9349 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9350 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9351 : TRUTH_XOR_EXPR,
9352 boolean_type_node,
9353 fold_convert_loc (loc, boolean_type_node, arg0),
9354 fold_convert_loc (loc, boolean_type_node, arg1));
9356 if (code == EQ_EXPR)
9357 tem = invert_truthvalue_loc (loc, tem);
9359 return fold_convert_loc (loc, type, tem);
9362 if (TREE_CODE_CLASS (code) == tcc_binary
9363 || TREE_CODE_CLASS (code) == tcc_comparison)
9365 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9367 tem = fold_build2_loc (loc, code, type,
9368 fold_convert_loc (loc, TREE_TYPE (op0),
9369 TREE_OPERAND (arg0, 1)), op1);
9370 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9371 tem);
9373 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9375 tem = fold_build2_loc (loc, code, type, op0,
9376 fold_convert_loc (loc, TREE_TYPE (op1),
9377 TREE_OPERAND (arg1, 1)));
9378 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9379 tem);
9382 if (TREE_CODE (arg0) == COND_EXPR
9383 || TREE_CODE (arg0) == VEC_COND_EXPR
9384 || COMPARISON_CLASS_P (arg0))
9386 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9387 arg0, arg1,
9388 /*cond_first_p=*/1);
9389 if (tem != NULL_TREE)
9390 return tem;
9393 if (TREE_CODE (arg1) == COND_EXPR
9394 || TREE_CODE (arg1) == VEC_COND_EXPR
9395 || COMPARISON_CLASS_P (arg1))
9397 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9398 arg1, arg0,
9399 /*cond_first_p=*/0);
9400 if (tem != NULL_TREE)
9401 return tem;
9405 switch (code)
9407 case MEM_REF:
9408 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9409 if (TREE_CODE (arg0) == ADDR_EXPR
9410 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9412 tree iref = TREE_OPERAND (arg0, 0);
9413 return fold_build2 (MEM_REF, type,
9414 TREE_OPERAND (iref, 0),
9415 int_const_binop (PLUS_EXPR, arg1,
9416 TREE_OPERAND (iref, 1)));
9419 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9420 if (TREE_CODE (arg0) == ADDR_EXPR
9421 && handled_component_p (TREE_OPERAND (arg0, 0)))
9423 tree base;
9424 poly_int64 coffset;
9425 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9426 &coffset);
9427 if (!base)
9428 return NULL_TREE;
9429 return fold_build2 (MEM_REF, type,
9430 build_fold_addr_expr (base),
9431 int_const_binop (PLUS_EXPR, arg1,
9432 size_int (coffset)));
9435 return NULL_TREE;
9437 case POINTER_PLUS_EXPR:
9438 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9439 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9440 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9441 return fold_convert_loc (loc, type,
9442 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9443 fold_convert_loc (loc, sizetype,
9444 arg1),
9445 fold_convert_loc (loc, sizetype,
9446 arg0)));
9448 return NULL_TREE;
9450 case PLUS_EXPR:
9451 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9453 /* X + (X / CST) * -CST is X % CST. */
9454 if (TREE_CODE (arg1) == MULT_EXPR
9455 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9456 && operand_equal_p (arg0,
9457 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9459 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9460 tree cst1 = TREE_OPERAND (arg1, 1);
9461 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9462 cst1, cst0);
9463 if (sum && integer_zerop (sum))
9464 return fold_convert_loc (loc, type,
9465 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9466 TREE_TYPE (arg0), arg0,
9467 cst0));
9471 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9472 one. Make sure the type is not saturating and has the signedness of
9473 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9474 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9475 if ((TREE_CODE (arg0) == MULT_EXPR
9476 || TREE_CODE (arg1) == MULT_EXPR)
9477 && !TYPE_SATURATING (type)
9478 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9479 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9480 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9482 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9483 if (tem)
9484 return tem;
9487 if (! FLOAT_TYPE_P (type))
9489 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9490 (plus (plus (mult) (mult)) (foo)) so that we can
9491 take advantage of the factoring cases below. */
9492 if (ANY_INTEGRAL_TYPE_P (type)
9493 && TYPE_OVERFLOW_WRAPS (type)
9494 && (((TREE_CODE (arg0) == PLUS_EXPR
9495 || TREE_CODE (arg0) == MINUS_EXPR)
9496 && TREE_CODE (arg1) == MULT_EXPR)
9497 || ((TREE_CODE (arg1) == PLUS_EXPR
9498 || TREE_CODE (arg1) == MINUS_EXPR)
9499 && TREE_CODE (arg0) == MULT_EXPR)))
9501 tree parg0, parg1, parg, marg;
9502 enum tree_code pcode;
9504 if (TREE_CODE (arg1) == MULT_EXPR)
9505 parg = arg0, marg = arg1;
9506 else
9507 parg = arg1, marg = arg0;
9508 pcode = TREE_CODE (parg);
9509 parg0 = TREE_OPERAND (parg, 0);
9510 parg1 = TREE_OPERAND (parg, 1);
9511 STRIP_NOPS (parg0);
9512 STRIP_NOPS (parg1);
9514 if (TREE_CODE (parg0) == MULT_EXPR
9515 && TREE_CODE (parg1) != MULT_EXPR)
9516 return fold_build2_loc (loc, pcode, type,
9517 fold_build2_loc (loc, PLUS_EXPR, type,
9518 fold_convert_loc (loc, type,
9519 parg0),
9520 fold_convert_loc (loc, type,
9521 marg)),
9522 fold_convert_loc (loc, type, parg1));
9523 if (TREE_CODE (parg0) != MULT_EXPR
9524 && TREE_CODE (parg1) == MULT_EXPR)
9525 return
9526 fold_build2_loc (loc, PLUS_EXPR, type,
9527 fold_convert_loc (loc, type, parg0),
9528 fold_build2_loc (loc, pcode, type,
9529 fold_convert_loc (loc, type, marg),
9530 fold_convert_loc (loc, type,
9531 parg1)));
9534 else
9536 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9537 to __complex__ ( x, y ). This is not the same for SNaNs or
9538 if signed zeros are involved. */
9539 if (!HONOR_SNANS (element_mode (arg0))
9540 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9541 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9543 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9544 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9545 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9546 bool arg0rz = false, arg0iz = false;
9547 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9548 || (arg0i && (arg0iz = real_zerop (arg0i))))
9550 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9551 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9552 if (arg0rz && arg1i && real_zerop (arg1i))
9554 tree rp = arg1r ? arg1r
9555 : build1 (REALPART_EXPR, rtype, arg1);
9556 tree ip = arg0i ? arg0i
9557 : build1 (IMAGPART_EXPR, rtype, arg0);
9558 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9560 else if (arg0iz && arg1r && real_zerop (arg1r))
9562 tree rp = arg0r ? arg0r
9563 : build1 (REALPART_EXPR, rtype, arg0);
9564 tree ip = arg1i ? arg1i
9565 : build1 (IMAGPART_EXPR, rtype, arg1);
9566 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9571 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9572 We associate floats only if the user has specified
9573 -fassociative-math. */
9574 if (flag_associative_math
9575 && TREE_CODE (arg1) == PLUS_EXPR
9576 && TREE_CODE (arg0) != MULT_EXPR)
9578 tree tree10 = TREE_OPERAND (arg1, 0);
9579 tree tree11 = TREE_OPERAND (arg1, 1);
9580 if (TREE_CODE (tree11) == MULT_EXPR
9581 && TREE_CODE (tree10) == MULT_EXPR)
9583 tree tree0;
9584 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9585 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9588 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9589 We associate floats only if the user has specified
9590 -fassociative-math. */
9591 if (flag_associative_math
9592 && TREE_CODE (arg0) == PLUS_EXPR
9593 && TREE_CODE (arg1) != MULT_EXPR)
9595 tree tree00 = TREE_OPERAND (arg0, 0);
9596 tree tree01 = TREE_OPERAND (arg0, 1);
9597 if (TREE_CODE (tree01) == MULT_EXPR
9598 && TREE_CODE (tree00) == MULT_EXPR)
9600 tree tree0;
9601 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9602 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9607 bit_rotate:
9608 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9609 is a rotate of A by C1 bits. */
9610 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9611 is a rotate of A by B bits.
9612 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9613 though in this case CODE must be | and not + or ^, otherwise
9614 it doesn't return A when B is 0. */
9616 enum tree_code code0, code1;
9617 tree rtype;
9618 code0 = TREE_CODE (arg0);
9619 code1 = TREE_CODE (arg1);
9620 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9621 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9622 && operand_equal_p (TREE_OPERAND (arg0, 0),
9623 TREE_OPERAND (arg1, 0), 0)
9624 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9625 TYPE_UNSIGNED (rtype))
9626 /* Only create rotates in complete modes. Other cases are not
9627 expanded properly. */
9628 && (element_precision (rtype)
9629 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9631 tree tree01, tree11;
9632 tree orig_tree01, orig_tree11;
9633 enum tree_code code01, code11;
9635 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9636 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9637 STRIP_NOPS (tree01);
9638 STRIP_NOPS (tree11);
9639 code01 = TREE_CODE (tree01);
9640 code11 = TREE_CODE (tree11);
9641 if (code11 != MINUS_EXPR
9642 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9644 std::swap (code0, code1);
9645 std::swap (code01, code11);
9646 std::swap (tree01, tree11);
9647 std::swap (orig_tree01, orig_tree11);
9649 if (code01 == INTEGER_CST
9650 && code11 == INTEGER_CST
9651 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9652 == element_precision (rtype)))
9654 tem = build2_loc (loc, LROTATE_EXPR,
9655 rtype, TREE_OPERAND (arg0, 0),
9656 code0 == LSHIFT_EXPR
9657 ? orig_tree01 : orig_tree11);
9658 return fold_convert_loc (loc, type, tem);
9660 else if (code11 == MINUS_EXPR)
9662 tree tree110, tree111;
9663 tree110 = TREE_OPERAND (tree11, 0);
9664 tree111 = TREE_OPERAND (tree11, 1);
9665 STRIP_NOPS (tree110);
9666 STRIP_NOPS (tree111);
9667 if (TREE_CODE (tree110) == INTEGER_CST
9668 && compare_tree_int (tree110,
9669 element_precision (rtype)) == 0
9670 && operand_equal_p (tree01, tree111, 0))
9672 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9673 ? LROTATE_EXPR : RROTATE_EXPR),
9674 rtype, TREE_OPERAND (arg0, 0),
9675 orig_tree01);
9676 return fold_convert_loc (loc, type, tem);
9679 else if (code == BIT_IOR_EXPR
9680 && code11 == BIT_AND_EXPR
9681 && pow2p_hwi (element_precision (rtype)))
9683 tree tree110, tree111;
9684 tree110 = TREE_OPERAND (tree11, 0);
9685 tree111 = TREE_OPERAND (tree11, 1);
9686 STRIP_NOPS (tree110);
9687 STRIP_NOPS (tree111);
9688 if (TREE_CODE (tree110) == NEGATE_EXPR
9689 && TREE_CODE (tree111) == INTEGER_CST
9690 && compare_tree_int (tree111,
9691 element_precision (rtype) - 1) == 0
9692 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9694 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9695 ? LROTATE_EXPR : RROTATE_EXPR),
9696 rtype, TREE_OPERAND (arg0, 0),
9697 orig_tree01);
9698 return fold_convert_loc (loc, type, tem);
9704 associate:
9705 /* In most languages, can't associate operations on floats through
9706 parentheses. Rather than remember where the parentheses were, we
9707 don't associate floats at all, unless the user has specified
9708 -fassociative-math.
9709 And, we need to make sure type is not saturating. */
9711 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9712 && !TYPE_SATURATING (type))
9714 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9715 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9716 tree atype = type;
9717 bool ok = true;
9719 /* Split both trees into variables, constants, and literals. Then
9720 associate each group together, the constants with literals,
9721 then the result with variables. This increases the chances of
9722 literals being recombined later and of generating relocatable
9723 expressions for the sum of a constant and literal. */
9724 var0 = split_tree (arg0, type, code,
9725 &minus_var0, &con0, &minus_con0,
9726 &lit0, &minus_lit0, 0);
9727 var1 = split_tree (arg1, type, code,
9728 &minus_var1, &con1, &minus_con1,
9729 &lit1, &minus_lit1, code == MINUS_EXPR);
9731 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9732 if (code == MINUS_EXPR)
9733 code = PLUS_EXPR;
9735 /* With undefined overflow prefer doing association in a type
9736 which wraps on overflow, if that is one of the operand types. */
9737 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9738 && !TYPE_OVERFLOW_WRAPS (type))
9740 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9741 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9742 atype = TREE_TYPE (arg0);
9743 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9744 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9745 atype = TREE_TYPE (arg1);
9746 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9749 /* With undefined overflow we can only associate constants with one
9750 variable, and constants whose association doesn't overflow. */
9751 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9752 && !TYPE_OVERFLOW_WRAPS (atype))
9754 if ((var0 && var1) || (minus_var0 && minus_var1))
9756 /* ??? If split_tree would handle NEGATE_EXPR we could
9757 simply reject these cases and the allowed cases would
9758 be the var0/minus_var1 ones. */
9759 tree tmp0 = var0 ? var0 : minus_var0;
9760 tree tmp1 = var1 ? var1 : minus_var1;
9761 bool one_neg = false;
9763 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9765 tmp0 = TREE_OPERAND (tmp0, 0);
9766 one_neg = !one_neg;
9768 if (CONVERT_EXPR_P (tmp0)
9769 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9770 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9771 <= TYPE_PRECISION (atype)))
9772 tmp0 = TREE_OPERAND (tmp0, 0);
9773 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9775 tmp1 = TREE_OPERAND (tmp1, 0);
9776 one_neg = !one_neg;
9778 if (CONVERT_EXPR_P (tmp1)
9779 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9780 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9781 <= TYPE_PRECISION (atype)))
9782 tmp1 = TREE_OPERAND (tmp1, 0);
9783 /* The only case we can still associate with two variables
9784 is if they cancel out. */
9785 if (!one_neg
9786 || !operand_equal_p (tmp0, tmp1, 0))
9787 ok = false;
9789 else if ((var0 && minus_var1
9790 && ! operand_equal_p (var0, minus_var1, 0))
9791 || (minus_var0 && var1
9792 && ! operand_equal_p (minus_var0, var1, 0)))
9793 ok = false;
9796 /* Only do something if we found more than two objects. Otherwise,
9797 nothing has changed and we risk infinite recursion. */
9798 if (ok
9799 && ((var0 != 0) + (var1 != 0)
9800 + (minus_var0 != 0) + (minus_var1 != 0)
9801 + (con0 != 0) + (con1 != 0)
9802 + (minus_con0 != 0) + (minus_con1 != 0)
9803 + (lit0 != 0) + (lit1 != 0)
9804 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9806 var0 = associate_trees (loc, var0, var1, code, atype);
9807 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9808 code, atype);
9809 con0 = associate_trees (loc, con0, con1, code, atype);
9810 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9811 code, atype);
9812 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9813 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9814 code, atype);
9816 if (minus_var0 && var0)
9818 var0 = associate_trees (loc, var0, minus_var0,
9819 MINUS_EXPR, atype);
9820 minus_var0 = 0;
9822 if (minus_con0 && con0)
9824 con0 = associate_trees (loc, con0, minus_con0,
9825 MINUS_EXPR, atype);
9826 minus_con0 = 0;
9829 /* Preserve the MINUS_EXPR if the negative part of the literal is
9830 greater than the positive part. Otherwise, the multiplicative
9831 folding code (i.e extract_muldiv) may be fooled in case
9832 unsigned constants are subtracted, like in the following
9833 example: ((X*2 + 4) - 8U)/2. */
9834 if (minus_lit0 && lit0)
9836 if (TREE_CODE (lit0) == INTEGER_CST
9837 && TREE_CODE (minus_lit0) == INTEGER_CST
9838 && tree_int_cst_lt (lit0, minus_lit0)
9839 /* But avoid ending up with only negated parts. */
9840 && (var0 || con0))
9842 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9843 MINUS_EXPR, atype);
9844 lit0 = 0;
9846 else
9848 lit0 = associate_trees (loc, lit0, minus_lit0,
9849 MINUS_EXPR, atype);
9850 minus_lit0 = 0;
9854 /* Don't introduce overflows through reassociation. */
9855 if ((lit0 && TREE_OVERFLOW_P (lit0))
9856 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9857 return NULL_TREE;
9859 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9860 con0 = associate_trees (loc, con0, lit0, code, atype);
9861 lit0 = 0;
9862 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9863 code, atype);
9864 minus_lit0 = 0;
9866 /* Eliminate minus_con0. */
9867 if (minus_con0)
9869 if (con0)
9870 con0 = associate_trees (loc, con0, minus_con0,
9871 MINUS_EXPR, atype);
9872 else if (var0)
9873 var0 = associate_trees (loc, var0, minus_con0,
9874 MINUS_EXPR, atype);
9875 else
9876 gcc_unreachable ();
9877 minus_con0 = 0;
9880 /* Eliminate minus_var0. */
9881 if (minus_var0)
9883 if (con0)
9884 con0 = associate_trees (loc, con0, minus_var0,
9885 MINUS_EXPR, atype);
9886 else
9887 gcc_unreachable ();
9888 minus_var0 = 0;
9891 return
9892 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9893 code, atype));
9897 return NULL_TREE;
9899 case POINTER_DIFF_EXPR:
9900 case MINUS_EXPR:
9901 /* Fold &a[i] - &a[j] to i-j. */
9902 if (TREE_CODE (arg0) == ADDR_EXPR
9903 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9904 && TREE_CODE (arg1) == ADDR_EXPR
9905 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9907 tree tem = fold_addr_of_array_ref_difference (loc, type,
9908 TREE_OPERAND (arg0, 0),
9909 TREE_OPERAND (arg1, 0),
9910 code
9911 == POINTER_DIFF_EXPR);
9912 if (tem)
9913 return tem;
9916 /* Further transformations are not for pointers. */
9917 if (code == POINTER_DIFF_EXPR)
9918 return NULL_TREE;
9920 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9921 if (TREE_CODE (arg0) == NEGATE_EXPR
9922 && negate_expr_p (op1)
9923 /* If arg0 is e.g. unsigned int and type is int, then this could
9924 introduce UB, because if A is INT_MIN at runtime, the original
9925 expression can be well defined while the latter is not.
9926 See PR83269. */
9927 && !(ANY_INTEGRAL_TYPE_P (type)
9928 && TYPE_OVERFLOW_UNDEFINED (type)
9929 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9930 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9931 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9932 fold_convert_loc (loc, type,
9933 TREE_OPERAND (arg0, 0)));
9935 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9936 __complex__ ( x, -y ). This is not the same for SNaNs or if
9937 signed zeros are involved. */
9938 if (!HONOR_SNANS (element_mode (arg0))
9939 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9940 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9942 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9943 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9944 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9945 bool arg0rz = false, arg0iz = false;
9946 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9947 || (arg0i && (arg0iz = real_zerop (arg0i))))
9949 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9950 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9951 if (arg0rz && arg1i && real_zerop (arg1i))
9953 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9954 arg1r ? arg1r
9955 : build1 (REALPART_EXPR, rtype, arg1));
9956 tree ip = arg0i ? arg0i
9957 : build1 (IMAGPART_EXPR, rtype, arg0);
9958 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9960 else if (arg0iz && arg1r && real_zerop (arg1r))
9962 tree rp = arg0r ? arg0r
9963 : build1 (REALPART_EXPR, rtype, arg0);
9964 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9965 arg1i ? arg1i
9966 : build1 (IMAGPART_EXPR, rtype, arg1));
9967 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9972 /* A - B -> A + (-B) if B is easily negatable. */
9973 if (negate_expr_p (op1)
9974 && ! TYPE_OVERFLOW_SANITIZED (type)
9975 && ((FLOAT_TYPE_P (type)
9976 /* Avoid this transformation if B is a positive REAL_CST. */
9977 && (TREE_CODE (op1) != REAL_CST
9978 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9979 || INTEGRAL_TYPE_P (type)))
9980 return fold_build2_loc (loc, PLUS_EXPR, type,
9981 fold_convert_loc (loc, type, arg0),
9982 negate_expr (op1));
9984 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9985 one. Make sure the type is not saturating and has the signedness of
9986 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9987 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9988 if ((TREE_CODE (arg0) == MULT_EXPR
9989 || TREE_CODE (arg1) == MULT_EXPR)
9990 && !TYPE_SATURATING (type)
9991 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9992 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9993 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9995 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9996 if (tem)
9997 return tem;
10000 goto associate;
10002 case MULT_EXPR:
10003 if (! FLOAT_TYPE_P (type))
10005 /* Transform x * -C into -x * C if x is easily negatable. */
10006 if (TREE_CODE (op1) == INTEGER_CST
10007 && tree_int_cst_sgn (op1) == -1
10008 && negate_expr_p (op0)
10009 && negate_expr_p (op1)
10010 && (tem = negate_expr (op1)) != op1
10011 && ! TREE_OVERFLOW (tem))
10012 return fold_build2_loc (loc, MULT_EXPR, type,
10013 fold_convert_loc (loc, type,
10014 negate_expr (op0)), tem);
10016 strict_overflow_p = false;
10017 if (TREE_CODE (arg1) == INTEGER_CST
10018 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10019 &strict_overflow_p)) != 0)
10021 if (strict_overflow_p)
10022 fold_overflow_warning (("assuming signed overflow does not "
10023 "occur when simplifying "
10024 "multiplication"),
10025 WARN_STRICT_OVERFLOW_MISC);
10026 return fold_convert_loc (loc, type, tem);
10029 /* Optimize z * conj(z) for integer complex numbers. */
10030 if (TREE_CODE (arg0) == CONJ_EXPR
10031 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10032 return fold_mult_zconjz (loc, type, arg1);
10033 if (TREE_CODE (arg1) == CONJ_EXPR
10034 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10035 return fold_mult_zconjz (loc, type, arg0);
10037 else
10039 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10040 This is not the same for NaNs or if signed zeros are
10041 involved. */
10042 if (!HONOR_NANS (arg0)
10043 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10044 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10045 && TREE_CODE (arg1) == COMPLEX_CST
10046 && real_zerop (TREE_REALPART (arg1)))
10048 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10049 if (real_onep (TREE_IMAGPART (arg1)))
10050 return
10051 fold_build2_loc (loc, COMPLEX_EXPR, type,
10052 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10053 rtype, arg0)),
10054 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10055 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10056 return
10057 fold_build2_loc (loc, COMPLEX_EXPR, type,
10058 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10059 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10060 rtype, arg0)));
10063 /* Optimize z * conj(z) for floating point complex numbers.
10064 Guarded by flag_unsafe_math_optimizations as non-finite
10065 imaginary components don't produce scalar results. */
10066 if (flag_unsafe_math_optimizations
10067 && TREE_CODE (arg0) == CONJ_EXPR
10068 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10069 return fold_mult_zconjz (loc, type, arg1);
10070 if (flag_unsafe_math_optimizations
10071 && TREE_CODE (arg1) == CONJ_EXPR
10072 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10073 return fold_mult_zconjz (loc, type, arg0);
10075 goto associate;
10077 case BIT_IOR_EXPR:
10078 /* Canonicalize (X & C1) | C2. */
10079 if (TREE_CODE (arg0) == BIT_AND_EXPR
10080 && TREE_CODE (arg1) == INTEGER_CST
10081 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10083 int width = TYPE_PRECISION (type), w;
10084 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10085 wide_int c2 = wi::to_wide (arg1);
10087 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10088 if ((c1 & c2) == c1)
10089 return omit_one_operand_loc (loc, type, arg1,
10090 TREE_OPERAND (arg0, 0));
10092 wide_int msk = wi::mask (width, false,
10093 TYPE_PRECISION (TREE_TYPE (arg1)));
10095 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10096 if (wi::bit_and_not (msk, c1 | c2) == 0)
10098 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10099 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10102 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10103 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10104 mode which allows further optimizations. */
10105 c1 &= msk;
10106 c2 &= msk;
10107 wide_int c3 = wi::bit_and_not (c1, c2);
10108 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10110 wide_int mask = wi::mask (w, false,
10111 TYPE_PRECISION (type));
10112 if (((c1 | c2) & mask) == mask
10113 && wi::bit_and_not (c1, mask) == 0)
10115 c3 = mask;
10116 break;
10120 if (c3 != c1)
10122 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10123 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10124 wide_int_to_tree (type, c3));
10125 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10129 /* See if this can be simplified into a rotate first. If that
10130 is unsuccessful continue in the association code. */
10131 goto bit_rotate;
10133 case BIT_XOR_EXPR:
10134 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10135 if (TREE_CODE (arg0) == BIT_AND_EXPR
10136 && INTEGRAL_TYPE_P (type)
10137 && integer_onep (TREE_OPERAND (arg0, 1))
10138 && integer_onep (arg1))
10139 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10140 build_zero_cst (TREE_TYPE (arg0)));
10142 /* See if this can be simplified into a rotate first. If that
10143 is unsuccessful continue in the association code. */
10144 goto bit_rotate;
10146 case BIT_AND_EXPR:
10147 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10148 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10149 && INTEGRAL_TYPE_P (type)
10150 && integer_onep (TREE_OPERAND (arg0, 1))
10151 && integer_onep (arg1))
10153 tree tem2;
10154 tem = TREE_OPERAND (arg0, 0);
10155 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10156 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10157 tem, tem2);
10158 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10159 build_zero_cst (TREE_TYPE (tem)));
10161 /* Fold ~X & 1 as (X & 1) == 0. */
10162 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10163 && INTEGRAL_TYPE_P (type)
10164 && integer_onep (arg1))
10166 tree tem2;
10167 tem = TREE_OPERAND (arg0, 0);
10168 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10169 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10170 tem, tem2);
10171 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10172 build_zero_cst (TREE_TYPE (tem)));
10174 /* Fold !X & 1 as X == 0. */
10175 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10176 && integer_onep (arg1))
10178 tem = TREE_OPERAND (arg0, 0);
10179 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10180 build_zero_cst (TREE_TYPE (tem)));
10183 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10184 multiple of 1 << CST. */
10185 if (TREE_CODE (arg1) == INTEGER_CST)
10187 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10188 wide_int ncst1 = -cst1;
10189 if ((cst1 & ncst1) == ncst1
10190 && multiple_of_p (type, arg0,
10191 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10192 return fold_convert_loc (loc, type, arg0);
10195 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10196 bits from CST2. */
10197 if (TREE_CODE (arg1) == INTEGER_CST
10198 && TREE_CODE (arg0) == MULT_EXPR
10199 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10201 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10202 wide_int masked
10203 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10205 if (masked == 0)
10206 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10207 arg0, arg1);
10208 else if (masked != warg1)
10210 /* Avoid the transform if arg1 is a mask of some
10211 mode which allows further optimizations. */
10212 int pop = wi::popcount (warg1);
10213 if (!(pop >= BITS_PER_UNIT
10214 && pow2p_hwi (pop)
10215 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10216 return fold_build2_loc (loc, code, type, op0,
10217 wide_int_to_tree (type, masked));
10221 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10222 ((A & N) + B) & M -> (A + B) & M
10223 Similarly if (N & M) == 0,
10224 ((A | N) + B) & M -> (A + B) & M
10225 and for - instead of + (or unary - instead of +)
10226 and/or ^ instead of |.
10227 If B is constant and (B & M) == 0, fold into A & M. */
10228 if (TREE_CODE (arg1) == INTEGER_CST)
10230 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10231 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10232 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10233 && (TREE_CODE (arg0) == PLUS_EXPR
10234 || TREE_CODE (arg0) == MINUS_EXPR
10235 || TREE_CODE (arg0) == NEGATE_EXPR)
10236 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10237 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10239 tree pmop[2];
10240 int which = 0;
10241 wide_int cst0;
10243 /* Now we know that arg0 is (C + D) or (C - D) or
10244 -C and arg1 (M) is == (1LL << cst) - 1.
10245 Store C into PMOP[0] and D into PMOP[1]. */
10246 pmop[0] = TREE_OPERAND (arg0, 0);
10247 pmop[1] = NULL;
10248 if (TREE_CODE (arg0) != NEGATE_EXPR)
10250 pmop[1] = TREE_OPERAND (arg0, 1);
10251 which = 1;
10254 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10255 which = -1;
10257 for (; which >= 0; which--)
10258 switch (TREE_CODE (pmop[which]))
10260 case BIT_AND_EXPR:
10261 case BIT_IOR_EXPR:
10262 case BIT_XOR_EXPR:
10263 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10264 != INTEGER_CST)
10265 break;
10266 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10267 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10269 if (cst0 != cst1)
10270 break;
10272 else if (cst0 != 0)
10273 break;
10274 /* If C or D is of the form (A & N) where
10275 (N & M) == M, or of the form (A | N) or
10276 (A ^ N) where (N & M) == 0, replace it with A. */
10277 pmop[which] = TREE_OPERAND (pmop[which], 0);
10278 break;
10279 case INTEGER_CST:
10280 /* If C or D is a N where (N & M) == 0, it can be
10281 omitted (assumed 0). */
10282 if ((TREE_CODE (arg0) == PLUS_EXPR
10283 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10284 && (cst1 & wi::to_wide (pmop[which])) == 0)
10285 pmop[which] = NULL;
10286 break;
10287 default:
10288 break;
10291 /* Only build anything new if we optimized one or both arguments
10292 above. */
10293 if (pmop[0] != TREE_OPERAND (arg0, 0)
10294 || (TREE_CODE (arg0) != NEGATE_EXPR
10295 && pmop[1] != TREE_OPERAND (arg0, 1)))
10297 tree utype = TREE_TYPE (arg0);
10298 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10300 /* Perform the operations in a type that has defined
10301 overflow behavior. */
10302 utype = unsigned_type_for (TREE_TYPE (arg0));
10303 if (pmop[0] != NULL)
10304 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10305 if (pmop[1] != NULL)
10306 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10309 if (TREE_CODE (arg0) == NEGATE_EXPR)
10310 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10311 else if (TREE_CODE (arg0) == PLUS_EXPR)
10313 if (pmop[0] != NULL && pmop[1] != NULL)
10314 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10315 pmop[0], pmop[1]);
10316 else if (pmop[0] != NULL)
10317 tem = pmop[0];
10318 else if (pmop[1] != NULL)
10319 tem = pmop[1];
10320 else
10321 return build_int_cst (type, 0);
10323 else if (pmop[0] == NULL)
10324 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10325 else
10326 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10327 pmop[0], pmop[1]);
10328 /* TEM is now the new binary +, - or unary - replacement. */
10329 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10330 fold_convert_loc (loc, utype, arg1));
10331 return fold_convert_loc (loc, type, tem);
10336 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10337 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10338 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10340 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10342 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10343 if (mask == -1)
10344 return
10345 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10348 goto associate;
10350 case RDIV_EXPR:
10351 /* Don't touch a floating-point divide by zero unless the mode
10352 of the constant can represent infinity. */
10353 if (TREE_CODE (arg1) == REAL_CST
10354 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10355 && real_zerop (arg1))
10356 return NULL_TREE;
10358 /* (-A) / (-B) -> A / B */
10359 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10360 return fold_build2_loc (loc, RDIV_EXPR, type,
10361 TREE_OPERAND (arg0, 0),
10362 negate_expr (arg1));
10363 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10364 return fold_build2_loc (loc, RDIV_EXPR, type,
10365 negate_expr (arg0),
10366 TREE_OPERAND (arg1, 0));
10367 return NULL_TREE;
10369 case TRUNC_DIV_EXPR:
10370 /* Fall through */
10372 case FLOOR_DIV_EXPR:
10373 /* Simplify A / (B << N) where A and B are positive and B is
10374 a power of 2, to A >> (N + log2(B)). */
10375 strict_overflow_p = false;
10376 if (TREE_CODE (arg1) == LSHIFT_EXPR
10377 && (TYPE_UNSIGNED (type)
10378 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10380 tree sval = TREE_OPERAND (arg1, 0);
10381 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10383 tree sh_cnt = TREE_OPERAND (arg1, 1);
10384 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10385 wi::exact_log2 (wi::to_wide (sval)));
10387 if (strict_overflow_p)
10388 fold_overflow_warning (("assuming signed overflow does not "
10389 "occur when simplifying A / (B << N)"),
10390 WARN_STRICT_OVERFLOW_MISC);
10392 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10393 sh_cnt, pow2);
10394 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10395 fold_convert_loc (loc, type, arg0), sh_cnt);
10399 /* Fall through */
10401 case ROUND_DIV_EXPR:
10402 case CEIL_DIV_EXPR:
10403 case EXACT_DIV_EXPR:
10404 if (integer_zerop (arg1))
10405 return NULL_TREE;
10407 /* Convert -A / -B to A / B when the type is signed and overflow is
10408 undefined. */
10409 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10410 && TREE_CODE (op0) == NEGATE_EXPR
10411 && negate_expr_p (op1))
10413 if (INTEGRAL_TYPE_P (type))
10414 fold_overflow_warning (("assuming signed overflow does not occur "
10415 "when distributing negation across "
10416 "division"),
10417 WARN_STRICT_OVERFLOW_MISC);
10418 return fold_build2_loc (loc, code, type,
10419 fold_convert_loc (loc, type,
10420 TREE_OPERAND (arg0, 0)),
10421 negate_expr (op1));
10423 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10424 && TREE_CODE (arg1) == NEGATE_EXPR
10425 && negate_expr_p (op0))
10427 if (INTEGRAL_TYPE_P (type))
10428 fold_overflow_warning (("assuming signed overflow does not occur "
10429 "when distributing negation across "
10430 "division"),
10431 WARN_STRICT_OVERFLOW_MISC);
10432 return fold_build2_loc (loc, code, type,
10433 negate_expr (op0),
10434 fold_convert_loc (loc, type,
10435 TREE_OPERAND (arg1, 0)));
10438 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10439 operation, EXACT_DIV_EXPR.
10441 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10442 At one time others generated faster code, it's not clear if they do
10443 after the last round to changes to the DIV code in expmed.c. */
10444 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10445 && multiple_of_p (type, arg0, arg1))
10446 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10447 fold_convert (type, arg0),
10448 fold_convert (type, arg1));
10450 strict_overflow_p = false;
10451 if (TREE_CODE (arg1) == INTEGER_CST
10452 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10453 &strict_overflow_p)) != 0)
10455 if (strict_overflow_p)
10456 fold_overflow_warning (("assuming signed overflow does not occur "
10457 "when simplifying division"),
10458 WARN_STRICT_OVERFLOW_MISC);
10459 return fold_convert_loc (loc, type, tem);
10462 return NULL_TREE;
10464 case CEIL_MOD_EXPR:
10465 case FLOOR_MOD_EXPR:
10466 case ROUND_MOD_EXPR:
10467 case TRUNC_MOD_EXPR:
10468 strict_overflow_p = false;
10469 if (TREE_CODE (arg1) == INTEGER_CST
10470 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10471 &strict_overflow_p)) != 0)
10473 if (strict_overflow_p)
10474 fold_overflow_warning (("assuming signed overflow does not occur "
10475 "when simplifying modulus"),
10476 WARN_STRICT_OVERFLOW_MISC);
10477 return fold_convert_loc (loc, type, tem);
10480 return NULL_TREE;
10482 case LROTATE_EXPR:
10483 case RROTATE_EXPR:
10484 case RSHIFT_EXPR:
10485 case LSHIFT_EXPR:
10486 /* Since negative shift count is not well-defined,
10487 don't try to compute it in the compiler. */
10488 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10489 return NULL_TREE;
10491 prec = element_precision (type);
10493 /* If we have a rotate of a bit operation with the rotate count and
10494 the second operand of the bit operation both constant,
10495 permute the two operations. */
10496 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10497 && (TREE_CODE (arg0) == BIT_AND_EXPR
10498 || TREE_CODE (arg0) == BIT_IOR_EXPR
10499 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10500 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10502 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10503 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10504 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10505 fold_build2_loc (loc, code, type,
10506 arg00, arg1),
10507 fold_build2_loc (loc, code, type,
10508 arg01, arg1));
10511 /* Two consecutive rotates adding up to the some integer
10512 multiple of the precision of the type can be ignored. */
10513 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10514 && TREE_CODE (arg0) == RROTATE_EXPR
10515 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10516 && wi::umod_trunc (wi::to_wide (arg1)
10517 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10518 prec) == 0)
10519 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10521 return NULL_TREE;
10523 case MIN_EXPR:
10524 case MAX_EXPR:
10525 goto associate;
10527 case TRUTH_ANDIF_EXPR:
10528 /* Note that the operands of this must be ints
10529 and their values must be 0 or 1.
10530 ("true" is a fixed value perhaps depending on the language.) */
10531 /* If first arg is constant zero, return it. */
10532 if (integer_zerop (arg0))
10533 return fold_convert_loc (loc, type, arg0);
10534 /* FALLTHRU */
10535 case TRUTH_AND_EXPR:
10536 /* If either arg is constant true, drop it. */
10537 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10538 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10539 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10540 /* Preserve sequence points. */
10541 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10542 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10543 /* If second arg is constant zero, result is zero, but first arg
10544 must be evaluated. */
10545 if (integer_zerop (arg1))
10546 return omit_one_operand_loc (loc, type, arg1, arg0);
10547 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10548 case will be handled here. */
10549 if (integer_zerop (arg0))
10550 return omit_one_operand_loc (loc, type, arg0, arg1);
10552 /* !X && X is always false. */
10553 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10554 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10555 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10556 /* X && !X is always false. */
10557 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10558 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10559 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10561 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10562 means A >= Y && A != MAX, but in this case we know that
10563 A < X <= MAX. */
10565 if (!TREE_SIDE_EFFECTS (arg0)
10566 && !TREE_SIDE_EFFECTS (arg1))
10568 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10569 if (tem && !operand_equal_p (tem, arg0, 0))
10570 return fold_build2_loc (loc, code, type, tem, arg1);
10572 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10573 if (tem && !operand_equal_p (tem, arg1, 0))
10574 return fold_build2_loc (loc, code, type, arg0, tem);
10577 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10578 != NULL_TREE)
10579 return tem;
10581 return NULL_TREE;
10583 case TRUTH_ORIF_EXPR:
10584 /* Note that the operands of this must be ints
10585 and their values must be 0 or true.
10586 ("true" is a fixed value perhaps depending on the language.) */
10587 /* If first arg is constant true, return it. */
10588 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10589 return fold_convert_loc (loc, type, arg0);
10590 /* FALLTHRU */
10591 case TRUTH_OR_EXPR:
10592 /* If either arg is constant zero, drop it. */
10593 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10594 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10595 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10596 /* Preserve sequence points. */
10597 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10598 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10599 /* If second arg is constant true, result is true, but we must
10600 evaluate first arg. */
10601 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10602 return omit_one_operand_loc (loc, type, arg1, arg0);
10603 /* Likewise for first arg, but note this only occurs here for
10604 TRUTH_OR_EXPR. */
10605 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10606 return omit_one_operand_loc (loc, type, arg0, arg1);
10608 /* !X || X is always true. */
10609 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10610 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10611 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10612 /* X || !X is always true. */
10613 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10614 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10615 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10617 /* (X && !Y) || (!X && Y) is X ^ Y */
10618 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10619 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10621 tree a0, a1, l0, l1, n0, n1;
10623 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10624 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10626 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10627 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10629 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10630 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10632 if ((operand_equal_p (n0, a0, 0)
10633 && operand_equal_p (n1, a1, 0))
10634 || (operand_equal_p (n0, a1, 0)
10635 && operand_equal_p (n1, a0, 0)))
10636 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10639 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10640 != NULL_TREE)
10641 return tem;
10643 return NULL_TREE;
10645 case TRUTH_XOR_EXPR:
10646 /* If the second arg is constant zero, drop it. */
10647 if (integer_zerop (arg1))
10648 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10649 /* If the second arg is constant true, this is a logical inversion. */
10650 if (integer_onep (arg1))
10652 tem = invert_truthvalue_loc (loc, arg0);
10653 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10655 /* Identical arguments cancel to zero. */
10656 if (operand_equal_p (arg0, arg1, 0))
10657 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10659 /* !X ^ X is always true. */
10660 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10661 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10662 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10664 /* X ^ !X is always true. */
10665 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10666 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10667 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10669 return NULL_TREE;
10671 case EQ_EXPR:
10672 case NE_EXPR:
10673 STRIP_NOPS (arg0);
10674 STRIP_NOPS (arg1);
10676 tem = fold_comparison (loc, code, type, op0, op1);
10677 if (tem != NULL_TREE)
10678 return tem;
10680 /* bool_var != 1 becomes !bool_var. */
10681 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10682 && code == NE_EXPR)
10683 return fold_convert_loc (loc, type,
10684 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10685 TREE_TYPE (arg0), arg0));
10687 /* bool_var == 0 becomes !bool_var. */
10688 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10689 && code == EQ_EXPR)
10690 return fold_convert_loc (loc, type,
10691 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10692 TREE_TYPE (arg0), arg0));
10694 /* !exp != 0 becomes !exp */
10695 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10696 && code == NE_EXPR)
10697 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10699 /* If this is an EQ or NE comparison with zero and ARG0 is
10700 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10701 two operations, but the latter can be done in one less insn
10702 on machines that have only two-operand insns or on which a
10703 constant cannot be the first operand. */
10704 if (TREE_CODE (arg0) == BIT_AND_EXPR
10705 && integer_zerop (arg1))
10707 tree arg00 = TREE_OPERAND (arg0, 0);
10708 tree arg01 = TREE_OPERAND (arg0, 1);
10709 if (TREE_CODE (arg00) == LSHIFT_EXPR
10710 && integer_onep (TREE_OPERAND (arg00, 0)))
10712 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10713 arg01, TREE_OPERAND (arg00, 1));
10714 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10715 build_int_cst (TREE_TYPE (arg0), 1));
10716 return fold_build2_loc (loc, code, type,
10717 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10718 arg1);
10720 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10721 && integer_onep (TREE_OPERAND (arg01, 0)))
10723 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10724 arg00, TREE_OPERAND (arg01, 1));
10725 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10726 build_int_cst (TREE_TYPE (arg0), 1));
10727 return fold_build2_loc (loc, code, type,
10728 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10729 arg1);
10733 /* If this is an NE or EQ comparison of zero against the result of a
10734 signed MOD operation whose second operand is a power of 2, make
10735 the MOD operation unsigned since it is simpler and equivalent. */
10736 if (integer_zerop (arg1)
10737 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10738 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10739 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10740 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10741 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10742 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10744 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10745 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10746 fold_convert_loc (loc, newtype,
10747 TREE_OPERAND (arg0, 0)),
10748 fold_convert_loc (loc, newtype,
10749 TREE_OPERAND (arg0, 1)));
10751 return fold_build2_loc (loc, code, type, newmod,
10752 fold_convert_loc (loc, newtype, arg1));
10755 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10756 C1 is a valid shift constant, and C2 is a power of two, i.e.
10757 a single bit. */
10758 if (TREE_CODE (arg0) == BIT_AND_EXPR
10759 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10760 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10761 == INTEGER_CST
10762 && integer_pow2p (TREE_OPERAND (arg0, 1))
10763 && integer_zerop (arg1))
10765 tree itype = TREE_TYPE (arg0);
10766 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10767 prec = TYPE_PRECISION (itype);
10769 /* Check for a valid shift count. */
10770 if (wi::ltu_p (wi::to_wide (arg001), prec))
10772 tree arg01 = TREE_OPERAND (arg0, 1);
10773 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10774 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10775 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10776 can be rewritten as (X & (C2 << C1)) != 0. */
10777 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10779 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10780 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10781 return fold_build2_loc (loc, code, type, tem,
10782 fold_convert_loc (loc, itype, arg1));
10784 /* Otherwise, for signed (arithmetic) shifts,
10785 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10786 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10787 else if (!TYPE_UNSIGNED (itype))
10788 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10789 arg000, build_int_cst (itype, 0));
10790 /* Otherwise, of unsigned (logical) shifts,
10791 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10792 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10793 else
10794 return omit_one_operand_loc (loc, type,
10795 code == EQ_EXPR ? integer_one_node
10796 : integer_zero_node,
10797 arg000);
10801 /* If this is a comparison of a field, we may be able to simplify it. */
10802 if ((TREE_CODE (arg0) == COMPONENT_REF
10803 || TREE_CODE (arg0) == BIT_FIELD_REF)
10804 /* Handle the constant case even without -O
10805 to make sure the warnings are given. */
10806 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10808 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10809 if (t1)
10810 return t1;
10813 /* Optimize comparisons of strlen vs zero to a compare of the
10814 first character of the string vs zero. To wit,
10815 strlen(ptr) == 0 => *ptr == 0
10816 strlen(ptr) != 0 => *ptr != 0
10817 Other cases should reduce to one of these two (or a constant)
10818 due to the return value of strlen being unsigned. */
10819 if (TREE_CODE (arg0) == CALL_EXPR
10820 && integer_zerop (arg1))
10822 tree fndecl = get_callee_fndecl (arg0);
10824 if (fndecl
10825 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10826 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10827 && call_expr_nargs (arg0) == 1
10828 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10830 tree iref = build_fold_indirect_ref_loc (loc,
10831 CALL_EXPR_ARG (arg0, 0));
10832 return fold_build2_loc (loc, code, type, iref,
10833 build_int_cst (TREE_TYPE (iref), 0));
10837 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10838 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10839 if (TREE_CODE (arg0) == RSHIFT_EXPR
10840 && integer_zerop (arg1)
10841 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10843 tree arg00 = TREE_OPERAND (arg0, 0);
10844 tree arg01 = TREE_OPERAND (arg0, 1);
10845 tree itype = TREE_TYPE (arg00);
10846 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10848 if (TYPE_UNSIGNED (itype))
10850 itype = signed_type_for (itype);
10851 arg00 = fold_convert_loc (loc, itype, arg00);
10853 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10854 type, arg00, build_zero_cst (itype));
10858 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10859 (X & C) == 0 when C is a single bit. */
10860 if (TREE_CODE (arg0) == BIT_AND_EXPR
10861 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10862 && integer_zerop (arg1)
10863 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10865 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10866 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10867 TREE_OPERAND (arg0, 1));
10868 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10869 type, tem,
10870 fold_convert_loc (loc, TREE_TYPE (arg0),
10871 arg1));
10874 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10875 constant C is a power of two, i.e. a single bit. */
10876 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10877 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10878 && integer_zerop (arg1)
10879 && integer_pow2p (TREE_OPERAND (arg0, 1))
10880 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10881 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10883 tree arg00 = TREE_OPERAND (arg0, 0);
10884 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10885 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10888 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10889 when is C is a power of two, i.e. a single bit. */
10890 if (TREE_CODE (arg0) == BIT_AND_EXPR
10891 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10892 && integer_zerop (arg1)
10893 && integer_pow2p (TREE_OPERAND (arg0, 1))
10894 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10895 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10897 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10898 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10899 arg000, TREE_OPERAND (arg0, 1));
10900 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10901 tem, build_int_cst (TREE_TYPE (tem), 0));
10904 if (integer_zerop (arg1)
10905 && tree_expr_nonzero_p (arg0))
10907 tree res = constant_boolean_node (code==NE_EXPR, type);
10908 return omit_one_operand_loc (loc, type, res, arg0);
10911 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10912 if (TREE_CODE (arg0) == BIT_AND_EXPR
10913 && TREE_CODE (arg1) == BIT_AND_EXPR)
10915 tree arg00 = TREE_OPERAND (arg0, 0);
10916 tree arg01 = TREE_OPERAND (arg0, 1);
10917 tree arg10 = TREE_OPERAND (arg1, 0);
10918 tree arg11 = TREE_OPERAND (arg1, 1);
10919 tree itype = TREE_TYPE (arg0);
10921 if (operand_equal_p (arg01, arg11, 0))
10923 tem = fold_convert_loc (loc, itype, arg10);
10924 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10925 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10926 return fold_build2_loc (loc, code, type, tem,
10927 build_zero_cst (itype));
10929 if (operand_equal_p (arg01, arg10, 0))
10931 tem = fold_convert_loc (loc, itype, arg11);
10932 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10933 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10934 return fold_build2_loc (loc, code, type, tem,
10935 build_zero_cst (itype));
10937 if (operand_equal_p (arg00, arg11, 0))
10939 tem = fold_convert_loc (loc, itype, arg10);
10940 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10941 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10942 return fold_build2_loc (loc, code, type, tem,
10943 build_zero_cst (itype));
10945 if (operand_equal_p (arg00, arg10, 0))
10947 tem = fold_convert_loc (loc, itype, arg11);
10948 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10949 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10950 return fold_build2_loc (loc, code, type, tem,
10951 build_zero_cst (itype));
10955 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10956 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10958 tree arg00 = TREE_OPERAND (arg0, 0);
10959 tree arg01 = TREE_OPERAND (arg0, 1);
10960 tree arg10 = TREE_OPERAND (arg1, 0);
10961 tree arg11 = TREE_OPERAND (arg1, 1);
10962 tree itype = TREE_TYPE (arg0);
10964 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10965 operand_equal_p guarantees no side-effects so we don't need
10966 to use omit_one_operand on Z. */
10967 if (operand_equal_p (arg01, arg11, 0))
10968 return fold_build2_loc (loc, code, type, arg00,
10969 fold_convert_loc (loc, TREE_TYPE (arg00),
10970 arg10));
10971 if (operand_equal_p (arg01, arg10, 0))
10972 return fold_build2_loc (loc, code, type, arg00,
10973 fold_convert_loc (loc, TREE_TYPE (arg00),
10974 arg11));
10975 if (operand_equal_p (arg00, arg11, 0))
10976 return fold_build2_loc (loc, code, type, arg01,
10977 fold_convert_loc (loc, TREE_TYPE (arg01),
10978 arg10));
10979 if (operand_equal_p (arg00, arg10, 0))
10980 return fold_build2_loc (loc, code, type, arg01,
10981 fold_convert_loc (loc, TREE_TYPE (arg01),
10982 arg11));
10984 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10985 if (TREE_CODE (arg01) == INTEGER_CST
10986 && TREE_CODE (arg11) == INTEGER_CST)
10988 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10989 fold_convert_loc (loc, itype, arg11));
10990 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10991 return fold_build2_loc (loc, code, type, tem,
10992 fold_convert_loc (loc, itype, arg10));
10996 /* Attempt to simplify equality/inequality comparisons of complex
10997 values. Only lower the comparison if the result is known or
10998 can be simplified to a single scalar comparison. */
10999 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11000 || TREE_CODE (arg0) == COMPLEX_CST)
11001 && (TREE_CODE (arg1) == COMPLEX_EXPR
11002 || TREE_CODE (arg1) == COMPLEX_CST))
11004 tree real0, imag0, real1, imag1;
11005 tree rcond, icond;
11007 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11009 real0 = TREE_OPERAND (arg0, 0);
11010 imag0 = TREE_OPERAND (arg0, 1);
11012 else
11014 real0 = TREE_REALPART (arg0);
11015 imag0 = TREE_IMAGPART (arg0);
11018 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11020 real1 = TREE_OPERAND (arg1, 0);
11021 imag1 = TREE_OPERAND (arg1, 1);
11023 else
11025 real1 = TREE_REALPART (arg1);
11026 imag1 = TREE_IMAGPART (arg1);
11029 rcond = fold_binary_loc (loc, code, type, real0, real1);
11030 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11032 if (integer_zerop (rcond))
11034 if (code == EQ_EXPR)
11035 return omit_two_operands_loc (loc, type, boolean_false_node,
11036 imag0, imag1);
11037 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11039 else
11041 if (code == NE_EXPR)
11042 return omit_two_operands_loc (loc, type, boolean_true_node,
11043 imag0, imag1);
11044 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11048 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11049 if (icond && TREE_CODE (icond) == INTEGER_CST)
11051 if (integer_zerop (icond))
11053 if (code == EQ_EXPR)
11054 return omit_two_operands_loc (loc, type, boolean_false_node,
11055 real0, real1);
11056 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11058 else
11060 if (code == NE_EXPR)
11061 return omit_two_operands_loc (loc, type, boolean_true_node,
11062 real0, real1);
11063 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11068 return NULL_TREE;
11070 case LT_EXPR:
11071 case GT_EXPR:
11072 case LE_EXPR:
11073 case GE_EXPR:
11074 tem = fold_comparison (loc, code, type, op0, op1);
11075 if (tem != NULL_TREE)
11076 return tem;
11078 /* Transform comparisons of the form X +- C CMP X. */
11079 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11080 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11081 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11082 && !HONOR_SNANS (arg0))
11084 tree arg01 = TREE_OPERAND (arg0, 1);
11085 enum tree_code code0 = TREE_CODE (arg0);
11086 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11088 /* (X - c) > X becomes false. */
11089 if (code == GT_EXPR
11090 && ((code0 == MINUS_EXPR && is_positive >= 0)
11091 || (code0 == PLUS_EXPR && is_positive <= 0)))
11092 return constant_boolean_node (0, type);
11094 /* Likewise (X + c) < X becomes false. */
11095 if (code == LT_EXPR
11096 && ((code0 == PLUS_EXPR && is_positive >= 0)
11097 || (code0 == MINUS_EXPR && is_positive <= 0)))
11098 return constant_boolean_node (0, type);
11100 /* Convert (X - c) <= X to true. */
11101 if (!HONOR_NANS (arg1)
11102 && code == LE_EXPR
11103 && ((code0 == MINUS_EXPR && is_positive >= 0)
11104 || (code0 == PLUS_EXPR && is_positive <= 0)))
11105 return constant_boolean_node (1, type);
11107 /* Convert (X + c) >= X to true. */
11108 if (!HONOR_NANS (arg1)
11109 && code == GE_EXPR
11110 && ((code0 == PLUS_EXPR && is_positive >= 0)
11111 || (code0 == MINUS_EXPR && is_positive <= 0)))
11112 return constant_boolean_node (1, type);
11115 /* If we are comparing an ABS_EXPR with a constant, we can
11116 convert all the cases into explicit comparisons, but they may
11117 well not be faster than doing the ABS and one comparison.
11118 But ABS (X) <= C is a range comparison, which becomes a subtraction
11119 and a comparison, and is probably faster. */
11120 if (code == LE_EXPR
11121 && TREE_CODE (arg1) == INTEGER_CST
11122 && TREE_CODE (arg0) == ABS_EXPR
11123 && ! TREE_SIDE_EFFECTS (arg0)
11124 && (tem = negate_expr (arg1)) != 0
11125 && TREE_CODE (tem) == INTEGER_CST
11126 && !TREE_OVERFLOW (tem))
11127 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11128 build2 (GE_EXPR, type,
11129 TREE_OPERAND (arg0, 0), tem),
11130 build2 (LE_EXPR, type,
11131 TREE_OPERAND (arg0, 0), arg1));
11133 /* Convert ABS_EXPR<x> >= 0 to true. */
11134 strict_overflow_p = false;
11135 if (code == GE_EXPR
11136 && (integer_zerop (arg1)
11137 || (! HONOR_NANS (arg0)
11138 && real_zerop (arg1)))
11139 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11141 if (strict_overflow_p)
11142 fold_overflow_warning (("assuming signed overflow does not occur "
11143 "when simplifying comparison of "
11144 "absolute value and zero"),
11145 WARN_STRICT_OVERFLOW_CONDITIONAL);
11146 return omit_one_operand_loc (loc, type,
11147 constant_boolean_node (true, type),
11148 arg0);
11151 /* Convert ABS_EXPR<x> < 0 to false. */
11152 strict_overflow_p = false;
11153 if (code == LT_EXPR
11154 && (integer_zerop (arg1) || real_zerop (arg1))
11155 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11157 if (strict_overflow_p)
11158 fold_overflow_warning (("assuming signed overflow does not occur "
11159 "when simplifying comparison of "
11160 "absolute value and zero"),
11161 WARN_STRICT_OVERFLOW_CONDITIONAL);
11162 return omit_one_operand_loc (loc, type,
11163 constant_boolean_node (false, type),
11164 arg0);
11167 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11168 and similarly for >= into !=. */
11169 if ((code == LT_EXPR || code == GE_EXPR)
11170 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11171 && TREE_CODE (arg1) == LSHIFT_EXPR
11172 && integer_onep (TREE_OPERAND (arg1, 0)))
11173 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11174 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11175 TREE_OPERAND (arg1, 1)),
11176 build_zero_cst (TREE_TYPE (arg0)));
11178 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11179 otherwise Y might be >= # of bits in X's type and thus e.g.
11180 (unsigned char) (1 << Y) for Y 15 might be 0.
11181 If the cast is widening, then 1 << Y should have unsigned type,
11182 otherwise if Y is number of bits in the signed shift type minus 1,
11183 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11184 31 might be 0xffffffff80000000. */
11185 if ((code == LT_EXPR || code == GE_EXPR)
11186 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11187 && CONVERT_EXPR_P (arg1)
11188 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11189 && (element_precision (TREE_TYPE (arg1))
11190 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11191 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11192 || (element_precision (TREE_TYPE (arg1))
11193 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11194 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11196 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11197 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11198 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11199 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11200 build_zero_cst (TREE_TYPE (arg0)));
11203 return NULL_TREE;
11205 case UNORDERED_EXPR:
11206 case ORDERED_EXPR:
11207 case UNLT_EXPR:
11208 case UNLE_EXPR:
11209 case UNGT_EXPR:
11210 case UNGE_EXPR:
11211 case UNEQ_EXPR:
11212 case LTGT_EXPR:
11213 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11215 tree targ0 = strip_float_extensions (arg0);
11216 tree targ1 = strip_float_extensions (arg1);
11217 tree newtype = TREE_TYPE (targ0);
11219 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11220 newtype = TREE_TYPE (targ1);
11222 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11223 return fold_build2_loc (loc, code, type,
11224 fold_convert_loc (loc, newtype, targ0),
11225 fold_convert_loc (loc, newtype, targ1));
11228 return NULL_TREE;
11230 case COMPOUND_EXPR:
11231 /* When pedantic, a compound expression can be neither an lvalue
11232 nor an integer constant expression. */
11233 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11234 return NULL_TREE;
11235 /* Don't let (0, 0) be null pointer constant. */
11236 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11237 : fold_convert_loc (loc, type, arg1);
11238 return pedantic_non_lvalue_loc (loc, tem);
11240 case ASSERT_EXPR:
11241 /* An ASSERT_EXPR should never be passed to fold_binary. */
11242 gcc_unreachable ();
11244 default:
11245 return NULL_TREE;
11246 } /* switch (code) */
11249 /* Used by contains_label_[p1]. */
11251 struct contains_label_data
11253 hash_set<tree> *pset;
11254 bool inside_switch_p;
11257 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11258 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11259 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11261 static tree
11262 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11264 contains_label_data *d = (contains_label_data *) data;
11265 switch (TREE_CODE (*tp))
11267 case LABEL_EXPR:
11268 return *tp;
11270 case CASE_LABEL_EXPR:
11271 if (!d->inside_switch_p)
11272 return *tp;
11273 return NULL_TREE;
11275 case SWITCH_EXPR:
11276 if (!d->inside_switch_p)
11278 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11279 return *tp;
11280 d->inside_switch_p = true;
11281 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11282 return *tp;
11283 d->inside_switch_p = false;
11284 *walk_subtrees = 0;
11286 return NULL_TREE;
11288 case GOTO_EXPR:
11289 *walk_subtrees = 0;
11290 return NULL_TREE;
11292 default:
11293 return NULL_TREE;
11297 /* Return whether the sub-tree ST contains a label which is accessible from
11298 outside the sub-tree. */
11300 static bool
11301 contains_label_p (tree st)
11303 hash_set<tree> pset;
11304 contains_label_data data = { &pset, false };
11305 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11308 /* Fold a ternary expression of code CODE and type TYPE with operands
11309 OP0, OP1, and OP2. Return the folded expression if folding is
11310 successful. Otherwise, return NULL_TREE. */
11312 tree
11313 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11314 tree op0, tree op1, tree op2)
11316 tree tem;
11317 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11318 enum tree_code_class kind = TREE_CODE_CLASS (code);
11320 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11321 && TREE_CODE_LENGTH (code) == 3);
11323 /* If this is a commutative operation, and OP0 is a constant, move it
11324 to OP1 to reduce the number of tests below. */
11325 if (commutative_ternary_tree_code (code)
11326 && tree_swap_operands_p (op0, op1))
11327 return fold_build3_loc (loc, code, type, op1, op0, op2);
11329 tem = generic_simplify (loc, code, type, op0, op1, op2);
11330 if (tem)
11331 return tem;
11333 /* Strip any conversions that don't change the mode. This is safe
11334 for every expression, except for a comparison expression because
11335 its signedness is derived from its operands. So, in the latter
11336 case, only strip conversions that don't change the signedness.
11338 Note that this is done as an internal manipulation within the
11339 constant folder, in order to find the simplest representation of
11340 the arguments so that their form can be studied. In any cases,
11341 the appropriate type conversions should be put back in the tree
11342 that will get out of the constant folder. */
11343 if (op0)
11345 arg0 = op0;
11346 STRIP_NOPS (arg0);
11349 if (op1)
11351 arg1 = op1;
11352 STRIP_NOPS (arg1);
11355 if (op2)
11357 arg2 = op2;
11358 STRIP_NOPS (arg2);
11361 switch (code)
11363 case COMPONENT_REF:
11364 if (TREE_CODE (arg0) == CONSTRUCTOR
11365 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11367 unsigned HOST_WIDE_INT idx;
11368 tree field, value;
11369 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11370 if (field == arg1)
11371 return value;
11373 return NULL_TREE;
11375 case COND_EXPR:
11376 case VEC_COND_EXPR:
11377 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11378 so all simple results must be passed through pedantic_non_lvalue. */
11379 if (TREE_CODE (arg0) == INTEGER_CST)
11381 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11382 tem = integer_zerop (arg0) ? op2 : op1;
11383 /* Only optimize constant conditions when the selected branch
11384 has the same type as the COND_EXPR. This avoids optimizing
11385 away "c ? x : throw", where the throw has a void type.
11386 Avoid throwing away that operand which contains label. */
11387 if ((!TREE_SIDE_EFFECTS (unused_op)
11388 || !contains_label_p (unused_op))
11389 && (! VOID_TYPE_P (TREE_TYPE (tem))
11390 || VOID_TYPE_P (type)))
11391 return pedantic_non_lvalue_loc (loc, tem);
11392 return NULL_TREE;
11394 else if (TREE_CODE (arg0) == VECTOR_CST)
11396 unsigned HOST_WIDE_INT nelts;
11397 if ((TREE_CODE (arg1) == VECTOR_CST
11398 || TREE_CODE (arg1) == CONSTRUCTOR)
11399 && (TREE_CODE (arg2) == VECTOR_CST
11400 || TREE_CODE (arg2) == CONSTRUCTOR)
11401 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11403 vec_perm_builder sel (nelts, nelts, 1);
11404 for (unsigned int i = 0; i < nelts; i++)
11406 tree val = VECTOR_CST_ELT (arg0, i);
11407 if (integer_all_onesp (val))
11408 sel.quick_push (i);
11409 else if (integer_zerop (val))
11410 sel.quick_push (nelts + i);
11411 else /* Currently unreachable. */
11412 return NULL_TREE;
11414 vec_perm_indices indices (sel, 2, nelts);
11415 tree t = fold_vec_perm (type, arg1, arg2, indices);
11416 if (t != NULL_TREE)
11417 return t;
11421 /* If we have A op B ? A : C, we may be able to convert this to a
11422 simpler expression, depending on the operation and the values
11423 of B and C. Signed zeros prevent all of these transformations,
11424 for reasons given above each one.
11426 Also try swapping the arguments and inverting the conditional. */
11427 if (COMPARISON_CLASS_P (arg0)
11428 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11429 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11431 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11432 if (tem)
11433 return tem;
11436 if (COMPARISON_CLASS_P (arg0)
11437 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11438 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11440 location_t loc0 = expr_location_or (arg0, loc);
11441 tem = fold_invert_truthvalue (loc0, arg0);
11442 if (tem && COMPARISON_CLASS_P (tem))
11444 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11445 if (tem)
11446 return tem;
11450 /* If the second operand is simpler than the third, swap them
11451 since that produces better jump optimization results. */
11452 if (truth_value_p (TREE_CODE (arg0))
11453 && tree_swap_operands_p (op1, op2))
11455 location_t loc0 = expr_location_or (arg0, loc);
11456 /* See if this can be inverted. If it can't, possibly because
11457 it was a floating-point inequality comparison, don't do
11458 anything. */
11459 tem = fold_invert_truthvalue (loc0, arg0);
11460 if (tem)
11461 return fold_build3_loc (loc, code, type, tem, op2, op1);
11464 /* Convert A ? 1 : 0 to simply A. */
11465 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11466 : (integer_onep (op1)
11467 && !VECTOR_TYPE_P (type)))
11468 && integer_zerop (op2)
11469 /* If we try to convert OP0 to our type, the
11470 call to fold will try to move the conversion inside
11471 a COND, which will recurse. In that case, the COND_EXPR
11472 is probably the best choice, so leave it alone. */
11473 && type == TREE_TYPE (arg0))
11474 return pedantic_non_lvalue_loc (loc, arg0);
11476 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11477 over COND_EXPR in cases such as floating point comparisons. */
11478 if (integer_zerop (op1)
11479 && code == COND_EXPR
11480 && integer_onep (op2)
11481 && !VECTOR_TYPE_P (type)
11482 && truth_value_p (TREE_CODE (arg0)))
11483 return pedantic_non_lvalue_loc (loc,
11484 fold_convert_loc (loc, type,
11485 invert_truthvalue_loc (loc,
11486 arg0)));
11488 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11489 if (TREE_CODE (arg0) == LT_EXPR
11490 && integer_zerop (TREE_OPERAND (arg0, 1))
11491 && integer_zerop (op2)
11492 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11494 /* sign_bit_p looks through both zero and sign extensions,
11495 but for this optimization only sign extensions are
11496 usable. */
11497 tree tem2 = TREE_OPERAND (arg0, 0);
11498 while (tem != tem2)
11500 if (TREE_CODE (tem2) != NOP_EXPR
11501 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11503 tem = NULL_TREE;
11504 break;
11506 tem2 = TREE_OPERAND (tem2, 0);
11508 /* sign_bit_p only checks ARG1 bits within A's precision.
11509 If <sign bit of A> has wider type than A, bits outside
11510 of A's precision in <sign bit of A> need to be checked.
11511 If they are all 0, this optimization needs to be done
11512 in unsigned A's type, if they are all 1 in signed A's type,
11513 otherwise this can't be done. */
11514 if (tem
11515 && TYPE_PRECISION (TREE_TYPE (tem))
11516 < TYPE_PRECISION (TREE_TYPE (arg1))
11517 && TYPE_PRECISION (TREE_TYPE (tem))
11518 < TYPE_PRECISION (type))
11520 int inner_width, outer_width;
11521 tree tem_type;
11523 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11524 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11525 if (outer_width > TYPE_PRECISION (type))
11526 outer_width = TYPE_PRECISION (type);
11528 wide_int mask = wi::shifted_mask
11529 (inner_width, outer_width - inner_width, false,
11530 TYPE_PRECISION (TREE_TYPE (arg1)));
11532 wide_int common = mask & wi::to_wide (arg1);
11533 if (common == mask)
11535 tem_type = signed_type_for (TREE_TYPE (tem));
11536 tem = fold_convert_loc (loc, tem_type, tem);
11538 else if (common == 0)
11540 tem_type = unsigned_type_for (TREE_TYPE (tem));
11541 tem = fold_convert_loc (loc, tem_type, tem);
11543 else
11544 tem = NULL;
11547 if (tem)
11548 return
11549 fold_convert_loc (loc, type,
11550 fold_build2_loc (loc, BIT_AND_EXPR,
11551 TREE_TYPE (tem), tem,
11552 fold_convert_loc (loc,
11553 TREE_TYPE (tem),
11554 arg1)));
11557 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11558 already handled above. */
11559 if (TREE_CODE (arg0) == BIT_AND_EXPR
11560 && integer_onep (TREE_OPERAND (arg0, 1))
11561 && integer_zerop (op2)
11562 && integer_pow2p (arg1))
11564 tree tem = TREE_OPERAND (arg0, 0);
11565 STRIP_NOPS (tem);
11566 if (TREE_CODE (tem) == RSHIFT_EXPR
11567 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11568 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11569 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11570 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11571 fold_convert_loc (loc, type,
11572 TREE_OPERAND (tem, 0)),
11573 op1);
11576 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11577 is probably obsolete because the first operand should be a
11578 truth value (that's why we have the two cases above), but let's
11579 leave it in until we can confirm this for all front-ends. */
11580 if (integer_zerop (op2)
11581 && TREE_CODE (arg0) == NE_EXPR
11582 && integer_zerop (TREE_OPERAND (arg0, 1))
11583 && integer_pow2p (arg1)
11584 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11585 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11586 arg1, OEP_ONLY_CONST))
11587 return pedantic_non_lvalue_loc (loc,
11588 fold_convert_loc (loc, type,
11589 TREE_OPERAND (arg0, 0)));
11591 /* Disable the transformations below for vectors, since
11592 fold_binary_op_with_conditional_arg may undo them immediately,
11593 yielding an infinite loop. */
11594 if (code == VEC_COND_EXPR)
11595 return NULL_TREE;
11597 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11598 if (integer_zerop (op2)
11599 && truth_value_p (TREE_CODE (arg0))
11600 && truth_value_p (TREE_CODE (arg1))
11601 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11602 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11603 : TRUTH_ANDIF_EXPR,
11604 type, fold_convert_loc (loc, type, arg0), op1);
11606 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11607 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11608 && truth_value_p (TREE_CODE (arg0))
11609 && truth_value_p (TREE_CODE (arg1))
11610 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11612 location_t loc0 = expr_location_or (arg0, loc);
11613 /* Only perform transformation if ARG0 is easily inverted. */
11614 tem = fold_invert_truthvalue (loc0, arg0);
11615 if (tem)
11616 return fold_build2_loc (loc, code == VEC_COND_EXPR
11617 ? BIT_IOR_EXPR
11618 : TRUTH_ORIF_EXPR,
11619 type, fold_convert_loc (loc, type, tem),
11620 op1);
11623 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11624 if (integer_zerop (arg1)
11625 && truth_value_p (TREE_CODE (arg0))
11626 && truth_value_p (TREE_CODE (op2))
11627 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11629 location_t loc0 = expr_location_or (arg0, loc);
11630 /* Only perform transformation if ARG0 is easily inverted. */
11631 tem = fold_invert_truthvalue (loc0, arg0);
11632 if (tem)
11633 return fold_build2_loc (loc, code == VEC_COND_EXPR
11634 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11635 type, fold_convert_loc (loc, type, tem),
11636 op2);
11639 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11640 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11641 && truth_value_p (TREE_CODE (arg0))
11642 && truth_value_p (TREE_CODE (op2))
11643 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11644 return fold_build2_loc (loc, code == VEC_COND_EXPR
11645 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11646 type, fold_convert_loc (loc, type, arg0), op2);
11648 return NULL_TREE;
11650 case CALL_EXPR:
11651 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11652 of fold_ternary on them. */
11653 gcc_unreachable ();
11655 case BIT_FIELD_REF:
11656 if (TREE_CODE (arg0) == VECTOR_CST
11657 && (type == TREE_TYPE (TREE_TYPE (arg0))
11658 || (TREE_CODE (type) == VECTOR_TYPE
11659 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11660 && tree_fits_uhwi_p (op1)
11661 && tree_fits_uhwi_p (op2))
11663 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11664 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11665 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11666 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11668 if (n != 0
11669 && (idx % width) == 0
11670 && (n % width) == 0
11671 && known_le ((idx + n) / width,
11672 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11674 idx = idx / width;
11675 n = n / width;
11677 if (TREE_CODE (arg0) == VECTOR_CST)
11679 if (n == 1)
11680 return VECTOR_CST_ELT (arg0, idx);
11682 tree_vector_builder vals (type, n, 1);
11683 for (unsigned i = 0; i < n; ++i)
11684 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11685 return vals.build ();
11690 /* On constants we can use native encode/interpret to constant
11691 fold (nearly) all BIT_FIELD_REFs. */
11692 if (CONSTANT_CLASS_P (arg0)
11693 && can_native_interpret_type_p (type)
11694 && BITS_PER_UNIT == 8
11695 && tree_fits_uhwi_p (op1)
11696 && tree_fits_uhwi_p (op2))
11698 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11699 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11700 /* Limit us to a reasonable amount of work. To relax the
11701 other limitations we need bit-shifting of the buffer
11702 and rounding up the size. */
11703 if (bitpos % BITS_PER_UNIT == 0
11704 && bitsize % BITS_PER_UNIT == 0
11705 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11707 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11708 unsigned HOST_WIDE_INT len
11709 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11710 bitpos / BITS_PER_UNIT);
11711 if (len > 0
11712 && len * BITS_PER_UNIT >= bitsize)
11714 tree v = native_interpret_expr (type, b,
11715 bitsize / BITS_PER_UNIT);
11716 if (v)
11717 return v;
11722 return NULL_TREE;
11724 case FMA_EXPR:
11725 /* For integers we can decompose the FMA if possible. */
11726 if (TREE_CODE (arg0) == INTEGER_CST
11727 && TREE_CODE (arg1) == INTEGER_CST)
11728 return fold_build2_loc (loc, PLUS_EXPR, type,
11729 const_binop (MULT_EXPR, arg0, arg1), arg2);
11730 if (integer_zerop (arg2))
11731 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11733 return fold_fma (loc, type, arg0, arg1, arg2);
11735 case VEC_PERM_EXPR:
11736 if (TREE_CODE (arg2) == VECTOR_CST)
11738 /* Build a vector of integers from the tree mask. */
11739 vec_perm_builder builder;
11740 if (!tree_to_vec_perm_builder (&builder, arg2))
11741 return NULL_TREE;
11743 /* Create a vec_perm_indices for the integer vector. */
11744 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11745 bool single_arg = (op0 == op1);
11746 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11748 /* Check for cases that fold to OP0 or OP1 in their original
11749 element order. */
11750 if (sel.series_p (0, 1, 0, 1))
11751 return op0;
11752 if (sel.series_p (0, 1, nelts, 1))
11753 return op1;
11755 if (!single_arg)
11757 if (sel.all_from_input_p (0))
11758 op1 = op0;
11759 else if (sel.all_from_input_p (1))
11761 op0 = op1;
11762 sel.rotate_inputs (1);
11766 if ((TREE_CODE (op0) == VECTOR_CST
11767 || TREE_CODE (op0) == CONSTRUCTOR)
11768 && (TREE_CODE (op1) == VECTOR_CST
11769 || TREE_CODE (op1) == CONSTRUCTOR))
11771 tree t = fold_vec_perm (type, op0, op1, sel);
11772 if (t != NULL_TREE)
11773 return t;
11776 bool changed = (op0 == op1 && !single_arg);
11778 /* Generate a canonical form of the selector. */
11779 if (arg2 == op2 && sel.encoding () != builder)
11781 /* Some targets are deficient and fail to expand a single
11782 argument permutation while still allowing an equivalent
11783 2-argument version. */
11784 if (sel.ninputs () == 2
11785 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11786 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11787 else
11789 vec_perm_indices sel2 (builder, 2, nelts);
11790 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11791 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11792 else
11793 /* Not directly supported with either encoding,
11794 so use the preferred form. */
11795 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11797 changed = true;
11800 if (changed)
11801 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11803 return NULL_TREE;
11805 case BIT_INSERT_EXPR:
11806 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11807 if (TREE_CODE (arg0) == INTEGER_CST
11808 && TREE_CODE (arg1) == INTEGER_CST)
11810 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11811 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11812 wide_int tem = (wi::to_wide (arg0)
11813 & wi::shifted_mask (bitpos, bitsize, true,
11814 TYPE_PRECISION (type)));
11815 wide_int tem2
11816 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11817 bitsize), bitpos);
11818 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11820 else if (TREE_CODE (arg0) == VECTOR_CST
11821 && CONSTANT_CLASS_P (arg1)
11822 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11823 TREE_TYPE (arg1)))
11825 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11826 unsigned HOST_WIDE_INT elsize
11827 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11828 if (bitpos % elsize == 0)
11830 unsigned k = bitpos / elsize;
11831 unsigned HOST_WIDE_INT nelts;
11832 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11833 return arg0;
11834 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11836 tree_vector_builder elts (type, nelts, 1);
11837 elts.quick_grow (nelts);
11838 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11839 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11840 return elts.build ();
11844 return NULL_TREE;
11846 default:
11847 return NULL_TREE;
11848 } /* switch (code) */
11851 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11852 of an array (or vector). */
11854 tree
11855 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11857 tree index_type = NULL_TREE;
11858 offset_int low_bound = 0;
11860 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11862 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11863 if (domain_type && TYPE_MIN_VALUE (domain_type))
11865 /* Static constructors for variably sized objects makes no sense. */
11866 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11867 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11868 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11872 if (index_type)
11873 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11874 TYPE_SIGN (index_type));
11876 offset_int index = low_bound - 1;
11877 if (index_type)
11878 index = wi::ext (index, TYPE_PRECISION (index_type),
11879 TYPE_SIGN (index_type));
11881 offset_int max_index;
11882 unsigned HOST_WIDE_INT cnt;
11883 tree cfield, cval;
11885 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11887 /* Array constructor might explicitly set index, or specify a range,
11888 or leave index NULL meaning that it is next index after previous
11889 one. */
11890 if (cfield)
11892 if (TREE_CODE (cfield) == INTEGER_CST)
11893 max_index = index = wi::to_offset (cfield);
11894 else
11896 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11897 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11898 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11901 else
11903 index += 1;
11904 if (index_type)
11905 index = wi::ext (index, TYPE_PRECISION (index_type),
11906 TYPE_SIGN (index_type));
11907 max_index = index;
11910 /* Do we have match? */
11911 if (wi::cmpu (access_index, index) >= 0
11912 && wi::cmpu (access_index, max_index) <= 0)
11913 return cval;
11915 return NULL_TREE;
11918 /* Perform constant folding and related simplification of EXPR.
11919 The related simplifications include x*1 => x, x*0 => 0, etc.,
11920 and application of the associative law.
11921 NOP_EXPR conversions may be removed freely (as long as we
11922 are careful not to change the type of the overall expression).
11923 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11924 but we can constant-fold them if they have constant operands. */
11926 #ifdef ENABLE_FOLD_CHECKING
11927 # define fold(x) fold_1 (x)
11928 static tree fold_1 (tree);
11929 static
11930 #endif
11931 tree
11932 fold (tree expr)
11934 const tree t = expr;
11935 enum tree_code code = TREE_CODE (t);
11936 enum tree_code_class kind = TREE_CODE_CLASS (code);
11937 tree tem;
11938 location_t loc = EXPR_LOCATION (expr);
11940 /* Return right away if a constant. */
11941 if (kind == tcc_constant)
11942 return t;
11944 /* CALL_EXPR-like objects with variable numbers of operands are
11945 treated specially. */
11946 if (kind == tcc_vl_exp)
11948 if (code == CALL_EXPR)
11950 tem = fold_call_expr (loc, expr, false);
11951 return tem ? tem : expr;
11953 return expr;
11956 if (IS_EXPR_CODE_CLASS (kind))
11958 tree type = TREE_TYPE (t);
11959 tree op0, op1, op2;
11961 switch (TREE_CODE_LENGTH (code))
11963 case 1:
11964 op0 = TREE_OPERAND (t, 0);
11965 tem = fold_unary_loc (loc, code, type, op0);
11966 return tem ? tem : expr;
11967 case 2:
11968 op0 = TREE_OPERAND (t, 0);
11969 op1 = TREE_OPERAND (t, 1);
11970 tem = fold_binary_loc (loc, code, type, op0, op1);
11971 return tem ? tem : expr;
11972 case 3:
11973 op0 = TREE_OPERAND (t, 0);
11974 op1 = TREE_OPERAND (t, 1);
11975 op2 = TREE_OPERAND (t, 2);
11976 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11977 return tem ? tem : expr;
11978 default:
11979 break;
11983 switch (code)
11985 case ARRAY_REF:
11987 tree op0 = TREE_OPERAND (t, 0);
11988 tree op1 = TREE_OPERAND (t, 1);
11990 if (TREE_CODE (op1) == INTEGER_CST
11991 && TREE_CODE (op0) == CONSTRUCTOR
11992 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11994 tree val = get_array_ctor_element_at_index (op0,
11995 wi::to_offset (op1));
11996 if (val)
11997 return val;
12000 return t;
12003 /* Return a VECTOR_CST if possible. */
12004 case CONSTRUCTOR:
12006 tree type = TREE_TYPE (t);
12007 if (TREE_CODE (type) != VECTOR_TYPE)
12008 return t;
12010 unsigned i;
12011 tree val;
12012 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12013 if (! CONSTANT_CLASS_P (val))
12014 return t;
12016 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12019 case CONST_DECL:
12020 return fold (DECL_INITIAL (t));
12022 default:
12023 return t;
12024 } /* switch (code) */
12027 #ifdef ENABLE_FOLD_CHECKING
12028 #undef fold
12030 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12031 hash_table<nofree_ptr_hash<const tree_node> > *);
12032 static void fold_check_failed (const_tree, const_tree);
12033 void print_fold_checksum (const_tree);
12035 /* When --enable-checking=fold, compute a digest of expr before
12036 and after actual fold call to see if fold did not accidentally
12037 change original expr. */
12039 tree
12040 fold (tree expr)
12042 tree ret;
12043 struct md5_ctx ctx;
12044 unsigned char checksum_before[16], checksum_after[16];
12045 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12047 md5_init_ctx (&ctx);
12048 fold_checksum_tree (expr, &ctx, &ht);
12049 md5_finish_ctx (&ctx, checksum_before);
12050 ht.empty ();
12052 ret = fold_1 (expr);
12054 md5_init_ctx (&ctx);
12055 fold_checksum_tree (expr, &ctx, &ht);
12056 md5_finish_ctx (&ctx, checksum_after);
12058 if (memcmp (checksum_before, checksum_after, 16))
12059 fold_check_failed (expr, ret);
12061 return ret;
12064 void
12065 print_fold_checksum (const_tree expr)
12067 struct md5_ctx ctx;
12068 unsigned char checksum[16], cnt;
12069 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12071 md5_init_ctx (&ctx);
12072 fold_checksum_tree (expr, &ctx, &ht);
12073 md5_finish_ctx (&ctx, checksum);
12074 for (cnt = 0; cnt < 16; ++cnt)
12075 fprintf (stderr, "%02x", checksum[cnt]);
12076 putc ('\n', stderr);
12079 static void
12080 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12082 internal_error ("fold check: original tree changed by fold");
12085 static void
12086 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12087 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12089 const tree_node **slot;
12090 enum tree_code code;
12091 union tree_node buf;
12092 int i, len;
12094 recursive_label:
12095 if (expr == NULL)
12096 return;
12097 slot = ht->find_slot (expr, INSERT);
12098 if (*slot != NULL)
12099 return;
12100 *slot = expr;
12101 code = TREE_CODE (expr);
12102 if (TREE_CODE_CLASS (code) == tcc_declaration
12103 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12105 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12106 memcpy ((char *) &buf, expr, tree_size (expr));
12107 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12108 buf.decl_with_vis.symtab_node = NULL;
12109 expr = (tree) &buf;
12111 else if (TREE_CODE_CLASS (code) == tcc_type
12112 && (TYPE_POINTER_TO (expr)
12113 || TYPE_REFERENCE_TO (expr)
12114 || TYPE_CACHED_VALUES_P (expr)
12115 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12116 || TYPE_NEXT_VARIANT (expr)
12117 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12119 /* Allow these fields to be modified. */
12120 tree tmp;
12121 memcpy ((char *) &buf, expr, tree_size (expr));
12122 expr = tmp = (tree) &buf;
12123 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12124 TYPE_POINTER_TO (tmp) = NULL;
12125 TYPE_REFERENCE_TO (tmp) = NULL;
12126 TYPE_NEXT_VARIANT (tmp) = NULL;
12127 TYPE_ALIAS_SET (tmp) = -1;
12128 if (TYPE_CACHED_VALUES_P (tmp))
12130 TYPE_CACHED_VALUES_P (tmp) = 0;
12131 TYPE_CACHED_VALUES (tmp) = NULL;
12134 md5_process_bytes (expr, tree_size (expr), ctx);
12135 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12136 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12137 if (TREE_CODE_CLASS (code) != tcc_type
12138 && TREE_CODE_CLASS (code) != tcc_declaration
12139 && code != TREE_LIST
12140 && code != SSA_NAME
12141 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12142 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12143 switch (TREE_CODE_CLASS (code))
12145 case tcc_constant:
12146 switch (code)
12148 case STRING_CST:
12149 md5_process_bytes (TREE_STRING_POINTER (expr),
12150 TREE_STRING_LENGTH (expr), ctx);
12151 break;
12152 case COMPLEX_CST:
12153 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12154 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12155 break;
12156 case VECTOR_CST:
12157 len = vector_cst_encoded_nelts (expr);
12158 for (i = 0; i < len; ++i)
12159 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12160 break;
12161 default:
12162 break;
12164 break;
12165 case tcc_exceptional:
12166 switch (code)
12168 case TREE_LIST:
12169 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12170 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12171 expr = TREE_CHAIN (expr);
12172 goto recursive_label;
12173 break;
12174 case TREE_VEC:
12175 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12176 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12177 break;
12178 default:
12179 break;
12181 break;
12182 case tcc_expression:
12183 case tcc_reference:
12184 case tcc_comparison:
12185 case tcc_unary:
12186 case tcc_binary:
12187 case tcc_statement:
12188 case tcc_vl_exp:
12189 len = TREE_OPERAND_LENGTH (expr);
12190 for (i = 0; i < len; ++i)
12191 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12192 break;
12193 case tcc_declaration:
12194 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12195 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12196 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12198 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12199 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12200 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12201 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12202 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12205 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12207 if (TREE_CODE (expr) == FUNCTION_DECL)
12209 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12210 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12212 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12214 break;
12215 case tcc_type:
12216 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12217 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12218 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12219 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12220 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12221 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12222 if (INTEGRAL_TYPE_P (expr)
12223 || SCALAR_FLOAT_TYPE_P (expr))
12225 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12226 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12228 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12229 if (TREE_CODE (expr) == RECORD_TYPE
12230 || TREE_CODE (expr) == UNION_TYPE
12231 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12232 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12233 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12234 break;
12235 default:
12236 break;
12240 /* Helper function for outputting the checksum of a tree T. When
12241 debugging with gdb, you can "define mynext" to be "next" followed
12242 by "call debug_fold_checksum (op0)", then just trace down till the
12243 outputs differ. */
12245 DEBUG_FUNCTION void
12246 debug_fold_checksum (const_tree t)
12248 int i;
12249 unsigned char checksum[16];
12250 struct md5_ctx ctx;
12251 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12253 md5_init_ctx (&ctx);
12254 fold_checksum_tree (t, &ctx, &ht);
12255 md5_finish_ctx (&ctx, checksum);
12256 ht.empty ();
12258 for (i = 0; i < 16; i++)
12259 fprintf (stderr, "%d ", checksum[i]);
12261 fprintf (stderr, "\n");
12264 #endif
12266 /* Fold a unary tree expression with code CODE of type TYPE with an
12267 operand OP0. LOC is the location of the resulting expression.
12268 Return a folded expression if successful. Otherwise, return a tree
12269 expression with code CODE of type TYPE with an operand OP0. */
12271 tree
12272 fold_build1_loc (location_t loc,
12273 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12275 tree tem;
12276 #ifdef ENABLE_FOLD_CHECKING
12277 unsigned char checksum_before[16], checksum_after[16];
12278 struct md5_ctx ctx;
12279 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12281 md5_init_ctx (&ctx);
12282 fold_checksum_tree (op0, &ctx, &ht);
12283 md5_finish_ctx (&ctx, checksum_before);
12284 ht.empty ();
12285 #endif
12287 tem = fold_unary_loc (loc, code, type, op0);
12288 if (!tem)
12289 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12291 #ifdef ENABLE_FOLD_CHECKING
12292 md5_init_ctx (&ctx);
12293 fold_checksum_tree (op0, &ctx, &ht);
12294 md5_finish_ctx (&ctx, checksum_after);
12296 if (memcmp (checksum_before, checksum_after, 16))
12297 fold_check_failed (op0, tem);
12298 #endif
12299 return tem;
12302 /* Fold a binary tree expression with code CODE of type TYPE with
12303 operands OP0 and OP1. LOC is the location of the resulting
12304 expression. Return a folded expression if successful. Otherwise,
12305 return a tree expression with code CODE of type TYPE with operands
12306 OP0 and OP1. */
12308 tree
12309 fold_build2_loc (location_t loc,
12310 enum tree_code code, tree type, tree op0, tree op1
12311 MEM_STAT_DECL)
12313 tree tem;
12314 #ifdef ENABLE_FOLD_CHECKING
12315 unsigned char checksum_before_op0[16],
12316 checksum_before_op1[16],
12317 checksum_after_op0[16],
12318 checksum_after_op1[16];
12319 struct md5_ctx ctx;
12320 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12322 md5_init_ctx (&ctx);
12323 fold_checksum_tree (op0, &ctx, &ht);
12324 md5_finish_ctx (&ctx, checksum_before_op0);
12325 ht.empty ();
12327 md5_init_ctx (&ctx);
12328 fold_checksum_tree (op1, &ctx, &ht);
12329 md5_finish_ctx (&ctx, checksum_before_op1);
12330 ht.empty ();
12331 #endif
12333 tem = fold_binary_loc (loc, code, type, op0, op1);
12334 if (!tem)
12335 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12337 #ifdef ENABLE_FOLD_CHECKING
12338 md5_init_ctx (&ctx);
12339 fold_checksum_tree (op0, &ctx, &ht);
12340 md5_finish_ctx (&ctx, checksum_after_op0);
12341 ht.empty ();
12343 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12344 fold_check_failed (op0, tem);
12346 md5_init_ctx (&ctx);
12347 fold_checksum_tree (op1, &ctx, &ht);
12348 md5_finish_ctx (&ctx, checksum_after_op1);
12350 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12351 fold_check_failed (op1, tem);
12352 #endif
12353 return tem;
12356 /* Fold a ternary tree expression with code CODE of type TYPE with
12357 operands OP0, OP1, and OP2. Return a folded expression if
12358 successful. Otherwise, return a tree expression with code CODE of
12359 type TYPE with operands OP0, OP1, and OP2. */
12361 tree
12362 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12363 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12365 tree tem;
12366 #ifdef ENABLE_FOLD_CHECKING
12367 unsigned char checksum_before_op0[16],
12368 checksum_before_op1[16],
12369 checksum_before_op2[16],
12370 checksum_after_op0[16],
12371 checksum_after_op1[16],
12372 checksum_after_op2[16];
12373 struct md5_ctx ctx;
12374 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12376 md5_init_ctx (&ctx);
12377 fold_checksum_tree (op0, &ctx, &ht);
12378 md5_finish_ctx (&ctx, checksum_before_op0);
12379 ht.empty ();
12381 md5_init_ctx (&ctx);
12382 fold_checksum_tree (op1, &ctx, &ht);
12383 md5_finish_ctx (&ctx, checksum_before_op1);
12384 ht.empty ();
12386 md5_init_ctx (&ctx);
12387 fold_checksum_tree (op2, &ctx, &ht);
12388 md5_finish_ctx (&ctx, checksum_before_op2);
12389 ht.empty ();
12390 #endif
12392 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12393 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12394 if (!tem)
12395 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12397 #ifdef ENABLE_FOLD_CHECKING
12398 md5_init_ctx (&ctx);
12399 fold_checksum_tree (op0, &ctx, &ht);
12400 md5_finish_ctx (&ctx, checksum_after_op0);
12401 ht.empty ();
12403 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12404 fold_check_failed (op0, tem);
12406 md5_init_ctx (&ctx);
12407 fold_checksum_tree (op1, &ctx, &ht);
12408 md5_finish_ctx (&ctx, checksum_after_op1);
12409 ht.empty ();
12411 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12412 fold_check_failed (op1, tem);
12414 md5_init_ctx (&ctx);
12415 fold_checksum_tree (op2, &ctx, &ht);
12416 md5_finish_ctx (&ctx, checksum_after_op2);
12418 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12419 fold_check_failed (op2, tem);
12420 #endif
12421 return tem;
12424 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12425 arguments in ARGARRAY, and a null static chain.
12426 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12427 of type TYPE from the given operands as constructed by build_call_array. */
12429 tree
12430 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12431 int nargs, tree *argarray)
12433 tree tem;
12434 #ifdef ENABLE_FOLD_CHECKING
12435 unsigned char checksum_before_fn[16],
12436 checksum_before_arglist[16],
12437 checksum_after_fn[16],
12438 checksum_after_arglist[16];
12439 struct md5_ctx ctx;
12440 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12441 int i;
12443 md5_init_ctx (&ctx);
12444 fold_checksum_tree (fn, &ctx, &ht);
12445 md5_finish_ctx (&ctx, checksum_before_fn);
12446 ht.empty ();
12448 md5_init_ctx (&ctx);
12449 for (i = 0; i < nargs; i++)
12450 fold_checksum_tree (argarray[i], &ctx, &ht);
12451 md5_finish_ctx (&ctx, checksum_before_arglist);
12452 ht.empty ();
12453 #endif
12455 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12456 if (!tem)
12457 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12459 #ifdef ENABLE_FOLD_CHECKING
12460 md5_init_ctx (&ctx);
12461 fold_checksum_tree (fn, &ctx, &ht);
12462 md5_finish_ctx (&ctx, checksum_after_fn);
12463 ht.empty ();
12465 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12466 fold_check_failed (fn, tem);
12468 md5_init_ctx (&ctx);
12469 for (i = 0; i < nargs; i++)
12470 fold_checksum_tree (argarray[i], &ctx, &ht);
12471 md5_finish_ctx (&ctx, checksum_after_arglist);
12473 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12474 fold_check_failed (NULL_TREE, tem);
12475 #endif
12476 return tem;
12479 /* Perform constant folding and related simplification of initializer
12480 expression EXPR. These behave identically to "fold_buildN" but ignore
12481 potential run-time traps and exceptions that fold must preserve. */
12483 #define START_FOLD_INIT \
12484 int saved_signaling_nans = flag_signaling_nans;\
12485 int saved_trapping_math = flag_trapping_math;\
12486 int saved_rounding_math = flag_rounding_math;\
12487 int saved_trapv = flag_trapv;\
12488 int saved_folding_initializer = folding_initializer;\
12489 flag_signaling_nans = 0;\
12490 flag_trapping_math = 0;\
12491 flag_rounding_math = 0;\
12492 flag_trapv = 0;\
12493 folding_initializer = 1;
12495 #define END_FOLD_INIT \
12496 flag_signaling_nans = saved_signaling_nans;\
12497 flag_trapping_math = saved_trapping_math;\
12498 flag_rounding_math = saved_rounding_math;\
12499 flag_trapv = saved_trapv;\
12500 folding_initializer = saved_folding_initializer;
12502 tree
12503 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12504 tree type, tree op)
12506 tree result;
12507 START_FOLD_INIT;
12509 result = fold_build1_loc (loc, code, type, op);
12511 END_FOLD_INIT;
12512 return result;
12515 tree
12516 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12517 tree type, tree op0, tree op1)
12519 tree result;
12520 START_FOLD_INIT;
12522 result = fold_build2_loc (loc, code, type, op0, op1);
12524 END_FOLD_INIT;
12525 return result;
12528 tree
12529 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12530 int nargs, tree *argarray)
12532 tree result;
12533 START_FOLD_INIT;
12535 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12537 END_FOLD_INIT;
12538 return result;
12541 #undef START_FOLD_INIT
12542 #undef END_FOLD_INIT
12544 /* Determine if first argument is a multiple of second argument. Return 0 if
12545 it is not, or we cannot easily determined it to be.
12547 An example of the sort of thing we care about (at this point; this routine
12548 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12549 fold cases do now) is discovering that
12551 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12553 is a multiple of
12555 SAVE_EXPR (J * 8)
12557 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12559 This code also handles discovering that
12561 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12563 is a multiple of 8 so we don't have to worry about dealing with a
12564 possible remainder.
12566 Note that we *look* inside a SAVE_EXPR only to determine how it was
12567 calculated; it is not safe for fold to do much of anything else with the
12568 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12569 at run time. For example, the latter example above *cannot* be implemented
12570 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12571 evaluation time of the original SAVE_EXPR is not necessarily the same at
12572 the time the new expression is evaluated. The only optimization of this
12573 sort that would be valid is changing
12575 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12577 divided by 8 to
12579 SAVE_EXPR (I) * SAVE_EXPR (J)
12581 (where the same SAVE_EXPR (J) is used in the original and the
12582 transformed version). */
12585 multiple_of_p (tree type, const_tree top, const_tree bottom)
12587 gimple *stmt;
12588 tree t1, op1, op2;
12590 if (operand_equal_p (top, bottom, 0))
12591 return 1;
12593 if (TREE_CODE (type) != INTEGER_TYPE)
12594 return 0;
12596 switch (TREE_CODE (top))
12598 case BIT_AND_EXPR:
12599 /* Bitwise and provides a power of two multiple. If the mask is
12600 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12601 if (!integer_pow2p (bottom))
12602 return 0;
12603 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12604 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12606 case MULT_EXPR:
12607 if (TREE_CODE (bottom) == INTEGER_CST)
12609 op1 = TREE_OPERAND (top, 0);
12610 op2 = TREE_OPERAND (top, 1);
12611 if (TREE_CODE (op1) == INTEGER_CST)
12612 std::swap (op1, op2);
12613 if (TREE_CODE (op2) == INTEGER_CST)
12615 if (multiple_of_p (type, op2, bottom))
12616 return 1;
12617 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12618 if (multiple_of_p (type, bottom, op2))
12620 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12621 wi::to_widest (op2));
12622 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12624 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12625 return multiple_of_p (type, op1, op2);
12628 return multiple_of_p (type, op1, bottom);
12631 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12632 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12634 case MINUS_EXPR:
12635 /* It is impossible to prove if op0 - op1 is multiple of bottom
12636 precisely, so be conservative here checking if both op0 and op1
12637 are multiple of bottom. Note we check the second operand first
12638 since it's usually simpler. */
12639 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12640 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12642 case PLUS_EXPR:
12643 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12644 as op0 - 3 if the expression has unsigned type. For example,
12645 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12646 op1 = TREE_OPERAND (top, 1);
12647 if (TYPE_UNSIGNED (type)
12648 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12649 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12650 return (multiple_of_p (type, op1, bottom)
12651 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12653 case LSHIFT_EXPR:
12654 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12656 op1 = TREE_OPERAND (top, 1);
12657 /* const_binop may not detect overflow correctly,
12658 so check for it explicitly here. */
12659 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12660 wi::to_wide (op1))
12661 && (t1 = fold_convert (type,
12662 const_binop (LSHIFT_EXPR, size_one_node,
12663 op1))) != 0
12664 && !TREE_OVERFLOW (t1))
12665 return multiple_of_p (type, t1, bottom);
12667 return 0;
12669 case NOP_EXPR:
12670 /* Can't handle conversions from non-integral or wider integral type. */
12671 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12672 || (TYPE_PRECISION (type)
12673 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12674 return 0;
12676 /* fall through */
12678 case SAVE_EXPR:
12679 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12681 case COND_EXPR:
12682 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12683 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12685 case INTEGER_CST:
12686 if (TREE_CODE (bottom) != INTEGER_CST
12687 || integer_zerop (bottom)
12688 || (TYPE_UNSIGNED (type)
12689 && (tree_int_cst_sgn (top) < 0
12690 || tree_int_cst_sgn (bottom) < 0)))
12691 return 0;
12692 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12693 SIGNED);
12695 case SSA_NAME:
12696 if (TREE_CODE (bottom) == INTEGER_CST
12697 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12698 && gimple_code (stmt) == GIMPLE_ASSIGN)
12700 enum tree_code code = gimple_assign_rhs_code (stmt);
12702 /* Check for special cases to see if top is defined as multiple
12703 of bottom:
12705 top = (X & ~(bottom - 1) ; bottom is power of 2
12709 Y = X % bottom
12710 top = X - Y. */
12711 if (code == BIT_AND_EXPR
12712 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12713 && TREE_CODE (op2) == INTEGER_CST
12714 && integer_pow2p (bottom)
12715 && wi::multiple_of_p (wi::to_widest (op2),
12716 wi::to_widest (bottom), UNSIGNED))
12717 return 1;
12719 op1 = gimple_assign_rhs1 (stmt);
12720 if (code == MINUS_EXPR
12721 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12722 && TREE_CODE (op2) == SSA_NAME
12723 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12724 && gimple_code (stmt) == GIMPLE_ASSIGN
12725 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12726 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12727 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12728 return 1;
12731 /* fall through */
12733 default:
12734 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12735 return multiple_p (wi::to_poly_widest (top),
12736 wi::to_poly_widest (bottom));
12738 return 0;
12742 #define tree_expr_nonnegative_warnv_p(X, Y) \
12743 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12745 #define RECURSE(X) \
12746 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12748 /* Return true if CODE or TYPE is known to be non-negative. */
12750 static bool
12751 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12753 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12754 && truth_value_p (code))
12755 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12756 have a signed:1 type (where the value is -1 and 0). */
12757 return true;
12758 return false;
12761 /* Return true if (CODE OP0) is known to be non-negative. If the return
12762 value is based on the assumption that signed overflow is undefined,
12763 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12764 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12766 bool
12767 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12768 bool *strict_overflow_p, int depth)
12770 if (TYPE_UNSIGNED (type))
12771 return true;
12773 switch (code)
12775 case ABS_EXPR:
12776 /* We can't return 1 if flag_wrapv is set because
12777 ABS_EXPR<INT_MIN> = INT_MIN. */
12778 if (!ANY_INTEGRAL_TYPE_P (type))
12779 return true;
12780 if (TYPE_OVERFLOW_UNDEFINED (type))
12782 *strict_overflow_p = true;
12783 return true;
12785 break;
12787 case NON_LVALUE_EXPR:
12788 case FLOAT_EXPR:
12789 case FIX_TRUNC_EXPR:
12790 return RECURSE (op0);
12792 CASE_CONVERT:
12794 tree inner_type = TREE_TYPE (op0);
12795 tree outer_type = type;
12797 if (TREE_CODE (outer_type) == REAL_TYPE)
12799 if (TREE_CODE (inner_type) == REAL_TYPE)
12800 return RECURSE (op0);
12801 if (INTEGRAL_TYPE_P (inner_type))
12803 if (TYPE_UNSIGNED (inner_type))
12804 return true;
12805 return RECURSE (op0);
12808 else if (INTEGRAL_TYPE_P (outer_type))
12810 if (TREE_CODE (inner_type) == REAL_TYPE)
12811 return RECURSE (op0);
12812 if (INTEGRAL_TYPE_P (inner_type))
12813 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12814 && TYPE_UNSIGNED (inner_type);
12817 break;
12819 default:
12820 return tree_simple_nonnegative_warnv_p (code, type);
12823 /* We don't know sign of `t', so be conservative and return false. */
12824 return false;
12827 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12828 value is based on the assumption that signed overflow is undefined,
12829 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12830 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12832 bool
12833 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12834 tree op1, bool *strict_overflow_p,
12835 int depth)
12837 if (TYPE_UNSIGNED (type))
12838 return true;
12840 switch (code)
12842 case POINTER_PLUS_EXPR:
12843 case PLUS_EXPR:
12844 if (FLOAT_TYPE_P (type))
12845 return RECURSE (op0) && RECURSE (op1);
12847 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12848 both unsigned and at least 2 bits shorter than the result. */
12849 if (TREE_CODE (type) == INTEGER_TYPE
12850 && TREE_CODE (op0) == NOP_EXPR
12851 && TREE_CODE (op1) == NOP_EXPR)
12853 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12854 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12855 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12856 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12858 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12859 TYPE_PRECISION (inner2)) + 1;
12860 return prec < TYPE_PRECISION (type);
12863 break;
12865 case MULT_EXPR:
12866 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12868 /* x * x is always non-negative for floating point x
12869 or without overflow. */
12870 if (operand_equal_p (op0, op1, 0)
12871 || (RECURSE (op0) && RECURSE (op1)))
12873 if (ANY_INTEGRAL_TYPE_P (type)
12874 && TYPE_OVERFLOW_UNDEFINED (type))
12875 *strict_overflow_p = true;
12876 return true;
12880 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12881 both unsigned and their total bits is shorter than the result. */
12882 if (TREE_CODE (type) == INTEGER_TYPE
12883 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12884 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12886 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12887 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12888 : TREE_TYPE (op0);
12889 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12890 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12891 : TREE_TYPE (op1);
12893 bool unsigned0 = TYPE_UNSIGNED (inner0);
12894 bool unsigned1 = TYPE_UNSIGNED (inner1);
12896 if (TREE_CODE (op0) == INTEGER_CST)
12897 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12899 if (TREE_CODE (op1) == INTEGER_CST)
12900 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12902 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12903 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12905 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12906 ? tree_int_cst_min_precision (op0, UNSIGNED)
12907 : TYPE_PRECISION (inner0);
12909 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12910 ? tree_int_cst_min_precision (op1, UNSIGNED)
12911 : TYPE_PRECISION (inner1);
12913 return precision0 + precision1 < TYPE_PRECISION (type);
12916 return false;
12918 case BIT_AND_EXPR:
12919 case MAX_EXPR:
12920 return RECURSE (op0) || RECURSE (op1);
12922 case BIT_IOR_EXPR:
12923 case BIT_XOR_EXPR:
12924 case MIN_EXPR:
12925 case RDIV_EXPR:
12926 case TRUNC_DIV_EXPR:
12927 case CEIL_DIV_EXPR:
12928 case FLOOR_DIV_EXPR:
12929 case ROUND_DIV_EXPR:
12930 return RECURSE (op0) && RECURSE (op1);
12932 case TRUNC_MOD_EXPR:
12933 return RECURSE (op0);
12935 case FLOOR_MOD_EXPR:
12936 return RECURSE (op1);
12938 case CEIL_MOD_EXPR:
12939 case ROUND_MOD_EXPR:
12940 default:
12941 return tree_simple_nonnegative_warnv_p (code, type);
12944 /* We don't know sign of `t', so be conservative and return false. */
12945 return false;
12948 /* Return true if T is known to be non-negative. If the return
12949 value is based on the assumption that signed overflow is undefined,
12950 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12951 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12953 bool
12954 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12956 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12957 return true;
12959 switch (TREE_CODE (t))
12961 case INTEGER_CST:
12962 return tree_int_cst_sgn (t) >= 0;
12964 case REAL_CST:
12965 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12967 case FIXED_CST:
12968 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12970 case COND_EXPR:
12971 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12973 case SSA_NAME:
12974 /* Limit the depth of recursion to avoid quadratic behavior.
12975 This is expected to catch almost all occurrences in practice.
12976 If this code misses important cases that unbounded recursion
12977 would not, passes that need this information could be revised
12978 to provide it through dataflow propagation. */
12979 return (!name_registered_for_update_p (t)
12980 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12981 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12982 strict_overflow_p, depth));
12984 default:
12985 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12989 /* Return true if T is known to be non-negative. If the return
12990 value is based on the assumption that signed overflow is undefined,
12991 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12992 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12994 bool
12995 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12996 bool *strict_overflow_p, int depth)
12998 switch (fn)
13000 CASE_CFN_ACOS:
13001 CASE_CFN_ACOSH:
13002 CASE_CFN_CABS:
13003 CASE_CFN_COSH:
13004 CASE_CFN_ERFC:
13005 CASE_CFN_EXP:
13006 CASE_CFN_EXP10:
13007 CASE_CFN_EXP2:
13008 CASE_CFN_FABS:
13009 CASE_CFN_FDIM:
13010 CASE_CFN_HYPOT:
13011 CASE_CFN_POW10:
13012 CASE_CFN_FFS:
13013 CASE_CFN_PARITY:
13014 CASE_CFN_POPCOUNT:
13015 CASE_CFN_CLZ:
13016 CASE_CFN_CLRSB:
13017 case CFN_BUILT_IN_BSWAP32:
13018 case CFN_BUILT_IN_BSWAP64:
13019 /* Always true. */
13020 return true;
13022 CASE_CFN_SQRT:
13023 CASE_CFN_SQRT_FN:
13024 /* sqrt(-0.0) is -0.0. */
13025 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13026 return true;
13027 return RECURSE (arg0);
13029 CASE_CFN_ASINH:
13030 CASE_CFN_ATAN:
13031 CASE_CFN_ATANH:
13032 CASE_CFN_CBRT:
13033 CASE_CFN_CEIL:
13034 CASE_CFN_CEIL_FN:
13035 CASE_CFN_ERF:
13036 CASE_CFN_EXPM1:
13037 CASE_CFN_FLOOR:
13038 CASE_CFN_FLOOR_FN:
13039 CASE_CFN_FMOD:
13040 CASE_CFN_FREXP:
13041 CASE_CFN_ICEIL:
13042 CASE_CFN_IFLOOR:
13043 CASE_CFN_IRINT:
13044 CASE_CFN_IROUND:
13045 CASE_CFN_LCEIL:
13046 CASE_CFN_LDEXP:
13047 CASE_CFN_LFLOOR:
13048 CASE_CFN_LLCEIL:
13049 CASE_CFN_LLFLOOR:
13050 CASE_CFN_LLRINT:
13051 CASE_CFN_LLROUND:
13052 CASE_CFN_LRINT:
13053 CASE_CFN_LROUND:
13054 CASE_CFN_MODF:
13055 CASE_CFN_NEARBYINT:
13056 CASE_CFN_NEARBYINT_FN:
13057 CASE_CFN_RINT:
13058 CASE_CFN_RINT_FN:
13059 CASE_CFN_ROUND:
13060 CASE_CFN_ROUND_FN:
13061 CASE_CFN_SCALB:
13062 CASE_CFN_SCALBLN:
13063 CASE_CFN_SCALBN:
13064 CASE_CFN_SIGNBIT:
13065 CASE_CFN_SIGNIFICAND:
13066 CASE_CFN_SINH:
13067 CASE_CFN_TANH:
13068 CASE_CFN_TRUNC:
13069 CASE_CFN_TRUNC_FN:
13070 /* True if the 1st argument is nonnegative. */
13071 return RECURSE (arg0);
13073 CASE_CFN_FMAX:
13074 CASE_CFN_FMAX_FN:
13075 /* True if the 1st OR 2nd arguments are nonnegative. */
13076 return RECURSE (arg0) || RECURSE (arg1);
13078 CASE_CFN_FMIN:
13079 CASE_CFN_FMIN_FN:
13080 /* True if the 1st AND 2nd arguments are nonnegative. */
13081 return RECURSE (arg0) && RECURSE (arg1);
13083 CASE_CFN_COPYSIGN:
13084 CASE_CFN_COPYSIGN_FN:
13085 /* True if the 2nd argument is nonnegative. */
13086 return RECURSE (arg1);
13088 CASE_CFN_POWI:
13089 /* True if the 1st argument is nonnegative or the second
13090 argument is an even integer. */
13091 if (TREE_CODE (arg1) == INTEGER_CST
13092 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13093 return true;
13094 return RECURSE (arg0);
13096 CASE_CFN_POW:
13097 /* True if the 1st argument is nonnegative or the second
13098 argument is an even integer valued real. */
13099 if (TREE_CODE (arg1) == REAL_CST)
13101 REAL_VALUE_TYPE c;
13102 HOST_WIDE_INT n;
13104 c = TREE_REAL_CST (arg1);
13105 n = real_to_integer (&c);
13106 if ((n & 1) == 0)
13108 REAL_VALUE_TYPE cint;
13109 real_from_integer (&cint, VOIDmode, n, SIGNED);
13110 if (real_identical (&c, &cint))
13111 return true;
13114 return RECURSE (arg0);
13116 default:
13117 break;
13119 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13122 /* Return true if T is known to be non-negative. If the return
13123 value is based on the assumption that signed overflow is undefined,
13124 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13125 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13127 static bool
13128 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13130 enum tree_code code = TREE_CODE (t);
13131 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13132 return true;
13134 switch (code)
13136 case TARGET_EXPR:
13138 tree temp = TARGET_EXPR_SLOT (t);
13139 t = TARGET_EXPR_INITIAL (t);
13141 /* If the initializer is non-void, then it's a normal expression
13142 that will be assigned to the slot. */
13143 if (!VOID_TYPE_P (t))
13144 return RECURSE (t);
13146 /* Otherwise, the initializer sets the slot in some way. One common
13147 way is an assignment statement at the end of the initializer. */
13148 while (1)
13150 if (TREE_CODE (t) == BIND_EXPR)
13151 t = expr_last (BIND_EXPR_BODY (t));
13152 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13153 || TREE_CODE (t) == TRY_CATCH_EXPR)
13154 t = expr_last (TREE_OPERAND (t, 0));
13155 else if (TREE_CODE (t) == STATEMENT_LIST)
13156 t = expr_last (t);
13157 else
13158 break;
13160 if (TREE_CODE (t) == MODIFY_EXPR
13161 && TREE_OPERAND (t, 0) == temp)
13162 return RECURSE (TREE_OPERAND (t, 1));
13164 return false;
13167 case CALL_EXPR:
13169 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13170 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13172 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13173 get_call_combined_fn (t),
13174 arg0,
13175 arg1,
13176 strict_overflow_p, depth);
13178 case COMPOUND_EXPR:
13179 case MODIFY_EXPR:
13180 return RECURSE (TREE_OPERAND (t, 1));
13182 case BIND_EXPR:
13183 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13185 case SAVE_EXPR:
13186 return RECURSE (TREE_OPERAND (t, 0));
13188 default:
13189 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13193 #undef RECURSE
13194 #undef tree_expr_nonnegative_warnv_p
13196 /* Return true if T is known to be non-negative. If the return
13197 value is based on the assumption that signed overflow is undefined,
13198 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13199 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13201 bool
13202 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13204 enum tree_code code;
13205 if (t == error_mark_node)
13206 return false;
13208 code = TREE_CODE (t);
13209 switch (TREE_CODE_CLASS (code))
13211 case tcc_binary:
13212 case tcc_comparison:
13213 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13214 TREE_TYPE (t),
13215 TREE_OPERAND (t, 0),
13216 TREE_OPERAND (t, 1),
13217 strict_overflow_p, depth);
13219 case tcc_unary:
13220 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13221 TREE_TYPE (t),
13222 TREE_OPERAND (t, 0),
13223 strict_overflow_p, depth);
13225 case tcc_constant:
13226 case tcc_declaration:
13227 case tcc_reference:
13228 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13230 default:
13231 break;
13234 switch (code)
13236 case TRUTH_AND_EXPR:
13237 case TRUTH_OR_EXPR:
13238 case TRUTH_XOR_EXPR:
13239 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13240 TREE_TYPE (t),
13241 TREE_OPERAND (t, 0),
13242 TREE_OPERAND (t, 1),
13243 strict_overflow_p, depth);
13244 case TRUTH_NOT_EXPR:
13245 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13246 TREE_TYPE (t),
13247 TREE_OPERAND (t, 0),
13248 strict_overflow_p, depth);
13250 case COND_EXPR:
13251 case CONSTRUCTOR:
13252 case OBJ_TYPE_REF:
13253 case ASSERT_EXPR:
13254 case ADDR_EXPR:
13255 case WITH_SIZE_EXPR:
13256 case SSA_NAME:
13257 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13259 default:
13260 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13264 /* Return true if `t' is known to be non-negative. Handle warnings
13265 about undefined signed overflow. */
13267 bool
13268 tree_expr_nonnegative_p (tree t)
13270 bool ret, strict_overflow_p;
13272 strict_overflow_p = false;
13273 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13274 if (strict_overflow_p)
13275 fold_overflow_warning (("assuming signed overflow does not occur when "
13276 "determining that expression is always "
13277 "non-negative"),
13278 WARN_STRICT_OVERFLOW_MISC);
13279 return ret;
13283 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13284 For floating point we further ensure that T is not denormal.
13285 Similar logic is present in nonzero_address in rtlanal.h.
13287 If the return value is based on the assumption that signed overflow
13288 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13289 change *STRICT_OVERFLOW_P. */
13291 bool
13292 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13293 bool *strict_overflow_p)
13295 switch (code)
13297 case ABS_EXPR:
13298 return tree_expr_nonzero_warnv_p (op0,
13299 strict_overflow_p);
13301 case NOP_EXPR:
13303 tree inner_type = TREE_TYPE (op0);
13304 tree outer_type = type;
13306 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13307 && tree_expr_nonzero_warnv_p (op0,
13308 strict_overflow_p));
13310 break;
13312 case NON_LVALUE_EXPR:
13313 return tree_expr_nonzero_warnv_p (op0,
13314 strict_overflow_p);
13316 default:
13317 break;
13320 return false;
13323 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13324 For floating point we further ensure that T is not denormal.
13325 Similar logic is present in nonzero_address in rtlanal.h.
13327 If the return value is based on the assumption that signed overflow
13328 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13329 change *STRICT_OVERFLOW_P. */
13331 bool
13332 tree_binary_nonzero_warnv_p (enum tree_code code,
13333 tree type,
13334 tree op0,
13335 tree op1, bool *strict_overflow_p)
13337 bool sub_strict_overflow_p;
13338 switch (code)
13340 case POINTER_PLUS_EXPR:
13341 case PLUS_EXPR:
13342 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13344 /* With the presence of negative values it is hard
13345 to say something. */
13346 sub_strict_overflow_p = false;
13347 if (!tree_expr_nonnegative_warnv_p (op0,
13348 &sub_strict_overflow_p)
13349 || !tree_expr_nonnegative_warnv_p (op1,
13350 &sub_strict_overflow_p))
13351 return false;
13352 /* One of operands must be positive and the other non-negative. */
13353 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13354 overflows, on a twos-complement machine the sum of two
13355 nonnegative numbers can never be zero. */
13356 return (tree_expr_nonzero_warnv_p (op0,
13357 strict_overflow_p)
13358 || tree_expr_nonzero_warnv_p (op1,
13359 strict_overflow_p));
13361 break;
13363 case MULT_EXPR:
13364 if (TYPE_OVERFLOW_UNDEFINED (type))
13366 if (tree_expr_nonzero_warnv_p (op0,
13367 strict_overflow_p)
13368 && tree_expr_nonzero_warnv_p (op1,
13369 strict_overflow_p))
13371 *strict_overflow_p = true;
13372 return true;
13375 break;
13377 case MIN_EXPR:
13378 sub_strict_overflow_p = false;
13379 if (tree_expr_nonzero_warnv_p (op0,
13380 &sub_strict_overflow_p)
13381 && tree_expr_nonzero_warnv_p (op1,
13382 &sub_strict_overflow_p))
13384 if (sub_strict_overflow_p)
13385 *strict_overflow_p = true;
13387 break;
13389 case MAX_EXPR:
13390 sub_strict_overflow_p = false;
13391 if (tree_expr_nonzero_warnv_p (op0,
13392 &sub_strict_overflow_p))
13394 if (sub_strict_overflow_p)
13395 *strict_overflow_p = true;
13397 /* When both operands are nonzero, then MAX must be too. */
13398 if (tree_expr_nonzero_warnv_p (op1,
13399 strict_overflow_p))
13400 return true;
13402 /* MAX where operand 0 is positive is positive. */
13403 return tree_expr_nonnegative_warnv_p (op0,
13404 strict_overflow_p);
13406 /* MAX where operand 1 is positive is positive. */
13407 else if (tree_expr_nonzero_warnv_p (op1,
13408 &sub_strict_overflow_p)
13409 && tree_expr_nonnegative_warnv_p (op1,
13410 &sub_strict_overflow_p))
13412 if (sub_strict_overflow_p)
13413 *strict_overflow_p = true;
13414 return true;
13416 break;
13418 case BIT_IOR_EXPR:
13419 return (tree_expr_nonzero_warnv_p (op1,
13420 strict_overflow_p)
13421 || tree_expr_nonzero_warnv_p (op0,
13422 strict_overflow_p));
13424 default:
13425 break;
13428 return false;
13431 /* Return true when T is an address and is known to be nonzero.
13432 For floating point we further ensure that T is not denormal.
13433 Similar logic is present in nonzero_address in rtlanal.h.
13435 If the return value is based on the assumption that signed overflow
13436 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13437 change *STRICT_OVERFLOW_P. */
13439 bool
13440 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13442 bool sub_strict_overflow_p;
13443 switch (TREE_CODE (t))
13445 case INTEGER_CST:
13446 return !integer_zerop (t);
13448 case ADDR_EXPR:
13450 tree base = TREE_OPERAND (t, 0);
13452 if (!DECL_P (base))
13453 base = get_base_address (base);
13455 if (base && TREE_CODE (base) == TARGET_EXPR)
13456 base = TARGET_EXPR_SLOT (base);
13458 if (!base)
13459 return false;
13461 /* For objects in symbol table check if we know they are non-zero.
13462 Don't do anything for variables and functions before symtab is built;
13463 it is quite possible that they will be declared weak later. */
13464 int nonzero_addr = maybe_nonzero_address (base);
13465 if (nonzero_addr >= 0)
13466 return nonzero_addr;
13468 /* Constants are never weak. */
13469 if (CONSTANT_CLASS_P (base))
13470 return true;
13472 return false;
13475 case COND_EXPR:
13476 sub_strict_overflow_p = false;
13477 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13478 &sub_strict_overflow_p)
13479 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13480 &sub_strict_overflow_p))
13482 if (sub_strict_overflow_p)
13483 *strict_overflow_p = true;
13484 return true;
13486 break;
13488 case SSA_NAME:
13489 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13490 break;
13491 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13493 default:
13494 break;
13496 return false;
13499 #define integer_valued_real_p(X) \
13500 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13502 #define RECURSE(X) \
13503 ((integer_valued_real_p) (X, depth + 1))
13505 /* Return true if the floating point result of (CODE OP0) has an
13506 integer value. We also allow +Inf, -Inf and NaN to be considered
13507 integer values. Return false for signaling NaN.
13509 DEPTH is the current nesting depth of the query. */
13511 bool
13512 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13514 switch (code)
13516 case FLOAT_EXPR:
13517 return true;
13519 case ABS_EXPR:
13520 return RECURSE (op0);
13522 CASE_CONVERT:
13524 tree type = TREE_TYPE (op0);
13525 if (TREE_CODE (type) == INTEGER_TYPE)
13526 return true;
13527 if (TREE_CODE (type) == REAL_TYPE)
13528 return RECURSE (op0);
13529 break;
13532 default:
13533 break;
13535 return false;
13538 /* Return true if the floating point result of (CODE OP0 OP1) has an
13539 integer value. We also allow +Inf, -Inf and NaN to be considered
13540 integer values. Return false for signaling NaN.
13542 DEPTH is the current nesting depth of the query. */
13544 bool
13545 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13547 switch (code)
13549 case PLUS_EXPR:
13550 case MINUS_EXPR:
13551 case MULT_EXPR:
13552 case MIN_EXPR:
13553 case MAX_EXPR:
13554 return RECURSE (op0) && RECURSE (op1);
13556 default:
13557 break;
13559 return false;
13562 /* Return true if the floating point result of calling FNDECL with arguments
13563 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13564 considered integer values. Return false for signaling NaN. If FNDECL
13565 takes fewer than 2 arguments, the remaining ARGn are null.
13567 DEPTH is the current nesting depth of the query. */
13569 bool
13570 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13572 switch (fn)
13574 CASE_CFN_CEIL:
13575 CASE_CFN_CEIL_FN:
13576 CASE_CFN_FLOOR:
13577 CASE_CFN_FLOOR_FN:
13578 CASE_CFN_NEARBYINT:
13579 CASE_CFN_NEARBYINT_FN:
13580 CASE_CFN_RINT:
13581 CASE_CFN_RINT_FN:
13582 CASE_CFN_ROUND:
13583 CASE_CFN_ROUND_FN:
13584 CASE_CFN_TRUNC:
13585 CASE_CFN_TRUNC_FN:
13586 return true;
13588 CASE_CFN_FMIN:
13589 CASE_CFN_FMIN_FN:
13590 CASE_CFN_FMAX:
13591 CASE_CFN_FMAX_FN:
13592 return RECURSE (arg0) && RECURSE (arg1);
13594 default:
13595 break;
13597 return false;
13600 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13601 has an integer value. We also allow +Inf, -Inf and NaN to be
13602 considered integer values. Return false for signaling NaN.
13604 DEPTH is the current nesting depth of the query. */
13606 bool
13607 integer_valued_real_single_p (tree t, int depth)
13609 switch (TREE_CODE (t))
13611 case REAL_CST:
13612 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13614 case COND_EXPR:
13615 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13617 case SSA_NAME:
13618 /* Limit the depth of recursion to avoid quadratic behavior.
13619 This is expected to catch almost all occurrences in practice.
13620 If this code misses important cases that unbounded recursion
13621 would not, passes that need this information could be revised
13622 to provide it through dataflow propagation. */
13623 return (!name_registered_for_update_p (t)
13624 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13625 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13626 depth));
13628 default:
13629 break;
13631 return false;
13634 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13635 has an integer value. We also allow +Inf, -Inf and NaN to be
13636 considered integer values. Return false for signaling NaN.
13638 DEPTH is the current nesting depth of the query. */
13640 static bool
13641 integer_valued_real_invalid_p (tree t, int depth)
13643 switch (TREE_CODE (t))
13645 case COMPOUND_EXPR:
13646 case MODIFY_EXPR:
13647 case BIND_EXPR:
13648 return RECURSE (TREE_OPERAND (t, 1));
13650 case SAVE_EXPR:
13651 return RECURSE (TREE_OPERAND (t, 0));
13653 default:
13654 break;
13656 return false;
13659 #undef RECURSE
13660 #undef integer_valued_real_p
13662 /* Return true if the floating point expression T has an integer value.
13663 We also allow +Inf, -Inf and NaN to be considered integer values.
13664 Return false for signaling NaN.
13666 DEPTH is the current nesting depth of the query. */
13668 bool
13669 integer_valued_real_p (tree t, int depth)
13671 if (t == error_mark_node)
13672 return false;
13674 tree_code code = TREE_CODE (t);
13675 switch (TREE_CODE_CLASS (code))
13677 case tcc_binary:
13678 case tcc_comparison:
13679 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13680 TREE_OPERAND (t, 1), depth);
13682 case tcc_unary:
13683 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13685 case tcc_constant:
13686 case tcc_declaration:
13687 case tcc_reference:
13688 return integer_valued_real_single_p (t, depth);
13690 default:
13691 break;
13694 switch (code)
13696 case COND_EXPR:
13697 case SSA_NAME:
13698 return integer_valued_real_single_p (t, depth);
13700 case CALL_EXPR:
13702 tree arg0 = (call_expr_nargs (t) > 0
13703 ? CALL_EXPR_ARG (t, 0)
13704 : NULL_TREE);
13705 tree arg1 = (call_expr_nargs (t) > 1
13706 ? CALL_EXPR_ARG (t, 1)
13707 : NULL_TREE);
13708 return integer_valued_real_call_p (get_call_combined_fn (t),
13709 arg0, arg1, depth);
13712 default:
13713 return integer_valued_real_invalid_p (t, depth);
13717 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13718 attempt to fold the expression to a constant without modifying TYPE,
13719 OP0 or OP1.
13721 If the expression could be simplified to a constant, then return
13722 the constant. If the expression would not be simplified to a
13723 constant, then return NULL_TREE. */
13725 tree
13726 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13728 tree tem = fold_binary (code, type, op0, op1);
13729 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13732 /* Given the components of a unary expression CODE, TYPE and OP0,
13733 attempt to fold the expression to a constant without modifying
13734 TYPE or OP0.
13736 If the expression could be simplified to a constant, then return
13737 the constant. If the expression would not be simplified to a
13738 constant, then return NULL_TREE. */
13740 tree
13741 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13743 tree tem = fold_unary (code, type, op0);
13744 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13747 /* If EXP represents referencing an element in a constant string
13748 (either via pointer arithmetic or array indexing), return the
13749 tree representing the value accessed, otherwise return NULL. */
13751 tree
13752 fold_read_from_constant_string (tree exp)
13754 if ((TREE_CODE (exp) == INDIRECT_REF
13755 || TREE_CODE (exp) == ARRAY_REF)
13756 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13758 tree exp1 = TREE_OPERAND (exp, 0);
13759 tree index;
13760 tree string;
13761 location_t loc = EXPR_LOCATION (exp);
13763 if (TREE_CODE (exp) == INDIRECT_REF)
13764 string = string_constant (exp1, &index);
13765 else
13767 tree low_bound = array_ref_low_bound (exp);
13768 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13770 /* Optimize the special-case of a zero lower bound.
13772 We convert the low_bound to sizetype to avoid some problems
13773 with constant folding. (E.g. suppose the lower bound is 1,
13774 and its mode is QI. Without the conversion,l (ARRAY
13775 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13776 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13777 if (! integer_zerop (low_bound))
13778 index = size_diffop_loc (loc, index,
13779 fold_convert_loc (loc, sizetype, low_bound));
13781 string = exp1;
13784 scalar_int_mode char_mode;
13785 if (string
13786 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13787 && TREE_CODE (string) == STRING_CST
13788 && TREE_CODE (index) == INTEGER_CST
13789 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13790 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13791 &char_mode)
13792 && GET_MODE_SIZE (char_mode) == 1)
13793 return build_int_cst_type (TREE_TYPE (exp),
13794 (TREE_STRING_POINTER (string)
13795 [TREE_INT_CST_LOW (index)]));
13797 return NULL;
13800 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13801 an integer constant, real, or fixed-point constant.
13803 TYPE is the type of the result. */
13805 static tree
13806 fold_negate_const (tree arg0, tree type)
13808 tree t = NULL_TREE;
13810 switch (TREE_CODE (arg0))
13812 case REAL_CST:
13813 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13814 break;
13816 case FIXED_CST:
13818 FIXED_VALUE_TYPE f;
13819 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13820 &(TREE_FIXED_CST (arg0)), NULL,
13821 TYPE_SATURATING (type));
13822 t = build_fixed (type, f);
13823 /* Propagate overflow flags. */
13824 if (overflow_p | TREE_OVERFLOW (arg0))
13825 TREE_OVERFLOW (t) = 1;
13826 break;
13829 default:
13830 if (poly_int_tree_p (arg0))
13832 bool overflow;
13833 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13834 t = force_fit_type (type, res, 1,
13835 (overflow && ! TYPE_UNSIGNED (type))
13836 || TREE_OVERFLOW (arg0));
13837 break;
13840 gcc_unreachable ();
13843 return t;
13846 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13847 an integer constant or real constant.
13849 TYPE is the type of the result. */
13851 tree
13852 fold_abs_const (tree arg0, tree type)
13854 tree t = NULL_TREE;
13856 switch (TREE_CODE (arg0))
13858 case INTEGER_CST:
13860 /* If the value is unsigned or non-negative, then the absolute value
13861 is the same as the ordinary value. */
13862 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13863 t = arg0;
13865 /* If the value is negative, then the absolute value is
13866 its negation. */
13867 else
13869 bool overflow;
13870 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13871 t = force_fit_type (type, val, -1,
13872 overflow | TREE_OVERFLOW (arg0));
13875 break;
13877 case REAL_CST:
13878 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13879 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13880 else
13881 t = arg0;
13882 break;
13884 default:
13885 gcc_unreachable ();
13888 return t;
13891 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13892 constant. TYPE is the type of the result. */
13894 static tree
13895 fold_not_const (const_tree arg0, tree type)
13897 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13899 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13902 /* Given CODE, a relational operator, the target type, TYPE and two
13903 constant operands OP0 and OP1, return the result of the
13904 relational operation. If the result is not a compile time
13905 constant, then return NULL_TREE. */
13907 static tree
13908 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13910 int result, invert;
13912 /* From here on, the only cases we handle are when the result is
13913 known to be a constant. */
13915 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13917 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13918 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13920 /* Handle the cases where either operand is a NaN. */
13921 if (real_isnan (c0) || real_isnan (c1))
13923 switch (code)
13925 case EQ_EXPR:
13926 case ORDERED_EXPR:
13927 result = 0;
13928 break;
13930 case NE_EXPR:
13931 case UNORDERED_EXPR:
13932 case UNLT_EXPR:
13933 case UNLE_EXPR:
13934 case UNGT_EXPR:
13935 case UNGE_EXPR:
13936 case UNEQ_EXPR:
13937 result = 1;
13938 break;
13940 case LT_EXPR:
13941 case LE_EXPR:
13942 case GT_EXPR:
13943 case GE_EXPR:
13944 case LTGT_EXPR:
13945 if (flag_trapping_math)
13946 return NULL_TREE;
13947 result = 0;
13948 break;
13950 default:
13951 gcc_unreachable ();
13954 return constant_boolean_node (result, type);
13957 return constant_boolean_node (real_compare (code, c0, c1), type);
13960 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13962 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13963 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13964 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13967 /* Handle equality/inequality of complex constants. */
13968 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13970 tree rcond = fold_relational_const (code, type,
13971 TREE_REALPART (op0),
13972 TREE_REALPART (op1));
13973 tree icond = fold_relational_const (code, type,
13974 TREE_IMAGPART (op0),
13975 TREE_IMAGPART (op1));
13976 if (code == EQ_EXPR)
13977 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13978 else if (code == NE_EXPR)
13979 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13980 else
13981 return NULL_TREE;
13984 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13986 if (!VECTOR_TYPE_P (type))
13988 /* Have vector comparison with scalar boolean result. */
13989 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13990 && known_eq (VECTOR_CST_NELTS (op0),
13991 VECTOR_CST_NELTS (op1)));
13992 unsigned HOST_WIDE_INT nunits;
13993 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
13994 return NULL_TREE;
13995 for (unsigned i = 0; i < nunits; i++)
13997 tree elem0 = VECTOR_CST_ELT (op0, i);
13998 tree elem1 = VECTOR_CST_ELT (op1, i);
13999 tree tmp = fold_relational_const (code, type, elem0, elem1);
14000 if (tmp == NULL_TREE)
14001 return NULL_TREE;
14002 if (integer_zerop (tmp))
14003 return constant_boolean_node (false, type);
14005 return constant_boolean_node (true, type);
14007 tree_vector_builder elts;
14008 if (!elts.new_binary_operation (type, op0, op1, false))
14009 return NULL_TREE;
14010 unsigned int count = elts.encoded_nelts ();
14011 for (unsigned i = 0; i < count; i++)
14013 tree elem_type = TREE_TYPE (type);
14014 tree elem0 = VECTOR_CST_ELT (op0, i);
14015 tree elem1 = VECTOR_CST_ELT (op1, i);
14017 tree tem = fold_relational_const (code, elem_type,
14018 elem0, elem1);
14020 if (tem == NULL_TREE)
14021 return NULL_TREE;
14023 elts.quick_push (build_int_cst (elem_type,
14024 integer_zerop (tem) ? 0 : -1));
14027 return elts.build ();
14030 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14032 To compute GT, swap the arguments and do LT.
14033 To compute GE, do LT and invert the result.
14034 To compute LE, swap the arguments, do LT and invert the result.
14035 To compute NE, do EQ and invert the result.
14037 Therefore, the code below must handle only EQ and LT. */
14039 if (code == LE_EXPR || code == GT_EXPR)
14041 std::swap (op0, op1);
14042 code = swap_tree_comparison (code);
14045 /* Note that it is safe to invert for real values here because we
14046 have already handled the one case that it matters. */
14048 invert = 0;
14049 if (code == NE_EXPR || code == GE_EXPR)
14051 invert = 1;
14052 code = invert_tree_comparison (code, false);
14055 /* Compute a result for LT or EQ if args permit;
14056 Otherwise return T. */
14057 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14059 if (code == EQ_EXPR)
14060 result = tree_int_cst_equal (op0, op1);
14061 else
14062 result = tree_int_cst_lt (op0, op1);
14064 else
14065 return NULL_TREE;
14067 if (invert)
14068 result ^= 1;
14069 return constant_boolean_node (result, type);
14072 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14073 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14074 itself. */
14076 tree
14077 fold_build_cleanup_point_expr (tree type, tree expr)
14079 /* If the expression does not have side effects then we don't have to wrap
14080 it with a cleanup point expression. */
14081 if (!TREE_SIDE_EFFECTS (expr))
14082 return expr;
14084 /* If the expression is a return, check to see if the expression inside the
14085 return has no side effects or the right hand side of the modify expression
14086 inside the return. If either don't have side effects set we don't need to
14087 wrap the expression in a cleanup point expression. Note we don't check the
14088 left hand side of the modify because it should always be a return decl. */
14089 if (TREE_CODE (expr) == RETURN_EXPR)
14091 tree op = TREE_OPERAND (expr, 0);
14092 if (!op || !TREE_SIDE_EFFECTS (op))
14093 return expr;
14094 op = TREE_OPERAND (op, 1);
14095 if (!TREE_SIDE_EFFECTS (op))
14096 return expr;
14099 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14102 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14103 of an indirection through OP0, or NULL_TREE if no simplification is
14104 possible. */
14106 tree
14107 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14109 tree sub = op0;
14110 tree subtype;
14111 poly_uint64 const_op01;
14113 STRIP_NOPS (sub);
14114 subtype = TREE_TYPE (sub);
14115 if (!POINTER_TYPE_P (subtype)
14116 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14117 return NULL_TREE;
14119 if (TREE_CODE (sub) == ADDR_EXPR)
14121 tree op = TREE_OPERAND (sub, 0);
14122 tree optype = TREE_TYPE (op);
14124 /* *&CONST_DECL -> to the value of the const decl. */
14125 if (TREE_CODE (op) == CONST_DECL)
14126 return DECL_INITIAL (op);
14127 /* *&p => p; make sure to handle *&"str"[cst] here. */
14128 if (type == optype)
14130 tree fop = fold_read_from_constant_string (op);
14131 if (fop)
14132 return fop;
14133 else
14134 return op;
14136 /* *(foo *)&fooarray => fooarray[0] */
14137 else if (TREE_CODE (optype) == ARRAY_TYPE
14138 && type == TREE_TYPE (optype)
14139 && (!in_gimple_form
14140 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14142 tree type_domain = TYPE_DOMAIN (optype);
14143 tree min_val = size_zero_node;
14144 if (type_domain && TYPE_MIN_VALUE (type_domain))
14145 min_val = TYPE_MIN_VALUE (type_domain);
14146 if (in_gimple_form
14147 && TREE_CODE (min_val) != INTEGER_CST)
14148 return NULL_TREE;
14149 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14150 NULL_TREE, NULL_TREE);
14152 /* *(foo *)&complexfoo => __real__ complexfoo */
14153 else if (TREE_CODE (optype) == COMPLEX_TYPE
14154 && type == TREE_TYPE (optype))
14155 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14156 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14157 else if (VECTOR_TYPE_P (optype)
14158 && type == TREE_TYPE (optype))
14160 tree part_width = TYPE_SIZE (type);
14161 tree index = bitsize_int (0);
14162 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14163 index);
14167 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14168 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14170 tree op00 = TREE_OPERAND (sub, 0);
14171 tree op01 = TREE_OPERAND (sub, 1);
14173 STRIP_NOPS (op00);
14174 if (TREE_CODE (op00) == ADDR_EXPR)
14176 tree op00type;
14177 op00 = TREE_OPERAND (op00, 0);
14178 op00type = TREE_TYPE (op00);
14180 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14181 if (VECTOR_TYPE_P (op00type)
14182 && type == TREE_TYPE (op00type)
14183 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14184 but we want to treat offsets with MSB set as negative.
14185 For the code below negative offsets are invalid and
14186 TYPE_SIZE of the element is something unsigned, so
14187 check whether op01 fits into poly_int64, which implies
14188 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14189 then just use poly_uint64 because we want to treat the
14190 value as unsigned. */
14191 && tree_fits_poly_int64_p (op01))
14193 tree part_width = TYPE_SIZE (type);
14194 poly_uint64 max_offset
14195 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14196 * TYPE_VECTOR_SUBPARTS (op00type));
14197 if (known_lt (const_op01, max_offset))
14199 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14200 return fold_build3_loc (loc,
14201 BIT_FIELD_REF, type, op00,
14202 part_width, index);
14205 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14206 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14207 && type == TREE_TYPE (op00type))
14209 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14210 const_op01))
14211 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14213 /* ((foo *)&fooarray)[1] => fooarray[1] */
14214 else if (TREE_CODE (op00type) == ARRAY_TYPE
14215 && type == TREE_TYPE (op00type))
14217 tree type_domain = TYPE_DOMAIN (op00type);
14218 tree min_val = size_zero_node;
14219 if (type_domain && TYPE_MIN_VALUE (type_domain))
14220 min_val = TYPE_MIN_VALUE (type_domain);
14221 offset_int off = wi::to_offset (op01);
14222 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14223 offset_int remainder;
14224 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14225 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
14227 off = off + wi::to_offset (min_val);
14228 op01 = wide_int_to_tree (sizetype, off);
14229 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14230 NULL_TREE, NULL_TREE);
14236 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14237 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14238 && type == TREE_TYPE (TREE_TYPE (subtype))
14239 && (!in_gimple_form
14240 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14242 tree type_domain;
14243 tree min_val = size_zero_node;
14244 sub = build_fold_indirect_ref_loc (loc, sub);
14245 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14246 if (type_domain && TYPE_MIN_VALUE (type_domain))
14247 min_val = TYPE_MIN_VALUE (type_domain);
14248 if (in_gimple_form
14249 && TREE_CODE (min_val) != INTEGER_CST)
14250 return NULL_TREE;
14251 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14252 NULL_TREE);
14255 return NULL_TREE;
14258 /* Builds an expression for an indirection through T, simplifying some
14259 cases. */
14261 tree
14262 build_fold_indirect_ref_loc (location_t loc, tree t)
14264 tree type = TREE_TYPE (TREE_TYPE (t));
14265 tree sub = fold_indirect_ref_1 (loc, type, t);
14267 if (sub)
14268 return sub;
14270 return build1_loc (loc, INDIRECT_REF, type, t);
14273 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14275 tree
14276 fold_indirect_ref_loc (location_t loc, tree t)
14278 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14280 if (sub)
14281 return sub;
14282 else
14283 return t;
14286 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14287 whose result is ignored. The type of the returned tree need not be
14288 the same as the original expression. */
14290 tree
14291 fold_ignored_result (tree t)
14293 if (!TREE_SIDE_EFFECTS (t))
14294 return integer_zero_node;
14296 for (;;)
14297 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14299 case tcc_unary:
14300 t = TREE_OPERAND (t, 0);
14301 break;
14303 case tcc_binary:
14304 case tcc_comparison:
14305 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14306 t = TREE_OPERAND (t, 0);
14307 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14308 t = TREE_OPERAND (t, 1);
14309 else
14310 return t;
14311 break;
14313 case tcc_expression:
14314 switch (TREE_CODE (t))
14316 case COMPOUND_EXPR:
14317 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14318 return t;
14319 t = TREE_OPERAND (t, 0);
14320 break;
14322 case COND_EXPR:
14323 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14324 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14325 return t;
14326 t = TREE_OPERAND (t, 0);
14327 break;
14329 default:
14330 return t;
14332 break;
14334 default:
14335 return t;
14339 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14341 tree
14342 round_up_loc (location_t loc, tree value, unsigned int divisor)
14344 tree div = NULL_TREE;
14346 if (divisor == 1)
14347 return value;
14349 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14350 have to do anything. Only do this when we are not given a const,
14351 because in that case, this check is more expensive than just
14352 doing it. */
14353 if (TREE_CODE (value) != INTEGER_CST)
14355 div = build_int_cst (TREE_TYPE (value), divisor);
14357 if (multiple_of_p (TREE_TYPE (value), value, div))
14358 return value;
14361 /* If divisor is a power of two, simplify this to bit manipulation. */
14362 if (pow2_or_zerop (divisor))
14364 if (TREE_CODE (value) == INTEGER_CST)
14366 wide_int val = wi::to_wide (value);
14367 bool overflow_p;
14369 if ((val & (divisor - 1)) == 0)
14370 return value;
14372 overflow_p = TREE_OVERFLOW (value);
14373 val += divisor - 1;
14374 val &= (int) -divisor;
14375 if (val == 0)
14376 overflow_p = true;
14378 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14380 else
14382 tree t;
14384 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14385 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14386 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14387 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14390 else
14392 if (!div)
14393 div = build_int_cst (TREE_TYPE (value), divisor);
14394 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14395 value = size_binop_loc (loc, MULT_EXPR, value, div);
14398 return value;
14401 /* Likewise, but round down. */
14403 tree
14404 round_down_loc (location_t loc, tree value, int divisor)
14406 tree div = NULL_TREE;
14408 gcc_assert (divisor > 0);
14409 if (divisor == 1)
14410 return value;
14412 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14413 have to do anything. Only do this when we are not given a const,
14414 because in that case, this check is more expensive than just
14415 doing it. */
14416 if (TREE_CODE (value) != INTEGER_CST)
14418 div = build_int_cst (TREE_TYPE (value), divisor);
14420 if (multiple_of_p (TREE_TYPE (value), value, div))
14421 return value;
14424 /* If divisor is a power of two, simplify this to bit manipulation. */
14425 if (pow2_or_zerop (divisor))
14427 tree t;
14429 t = build_int_cst (TREE_TYPE (value), -divisor);
14430 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14432 else
14434 if (!div)
14435 div = build_int_cst (TREE_TYPE (value), divisor);
14436 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14437 value = size_binop_loc (loc, MULT_EXPR, value, div);
14440 return value;
14443 /* Returns the pointer to the base of the object addressed by EXP and
14444 extracts the information about the offset of the access, storing it
14445 to PBITPOS and POFFSET. */
14447 static tree
14448 split_address_to_core_and_offset (tree exp,
14449 poly_int64_pod *pbitpos, tree *poffset)
14451 tree core;
14452 machine_mode mode;
14453 int unsignedp, reversep, volatilep;
14454 poly_int64 bitsize;
14455 location_t loc = EXPR_LOCATION (exp);
14457 if (TREE_CODE (exp) == ADDR_EXPR)
14459 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14460 poffset, &mode, &unsignedp, &reversep,
14461 &volatilep);
14462 core = build_fold_addr_expr_loc (loc, core);
14464 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14466 core = TREE_OPERAND (exp, 0);
14467 STRIP_NOPS (core);
14468 *pbitpos = 0;
14469 *poffset = TREE_OPERAND (exp, 1);
14470 if (poly_int_tree_p (*poffset))
14472 poly_offset_int tem
14473 = wi::sext (wi::to_poly_offset (*poffset),
14474 TYPE_PRECISION (TREE_TYPE (*poffset)));
14475 tem <<= LOG2_BITS_PER_UNIT;
14476 if (tem.to_shwi (pbitpos))
14477 *poffset = NULL_TREE;
14480 else
14482 core = exp;
14483 *pbitpos = 0;
14484 *poffset = NULL_TREE;
14487 return core;
14490 /* Returns true if addresses of E1 and E2 differ by a constant, false
14491 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14493 bool
14494 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14496 tree core1, core2;
14497 poly_int64 bitpos1, bitpos2;
14498 tree toffset1, toffset2, tdiff, type;
14500 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14501 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14503 poly_int64 bytepos1, bytepos2;
14504 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14505 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14506 || !operand_equal_p (core1, core2, 0))
14507 return false;
14509 if (toffset1 && toffset2)
14511 type = TREE_TYPE (toffset1);
14512 if (type != TREE_TYPE (toffset2))
14513 toffset2 = fold_convert (type, toffset2);
14515 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14516 if (!cst_and_fits_in_hwi (tdiff))
14517 return false;
14519 *diff = int_cst_value (tdiff);
14521 else if (toffset1 || toffset2)
14523 /* If only one of the offsets is non-constant, the difference cannot
14524 be a constant. */
14525 return false;
14527 else
14528 *diff = 0;
14530 *diff += bytepos1 - bytepos2;
14531 return true;
14534 /* Return OFF converted to a pointer offset type suitable as offset for
14535 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14536 tree
14537 convert_to_ptrofftype_loc (location_t loc, tree off)
14539 return fold_convert_loc (loc, sizetype, off);
14542 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14543 tree
14544 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14546 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14547 ptr, convert_to_ptrofftype_loc (loc, off));
14550 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14551 tree
14552 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14554 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14555 ptr, size_int (off));
14558 /* Return a char pointer for a C string if it is a string constant
14559 or sum of string constant and integer constant. We only support
14560 string constants properly terminated with '\0' character.
14561 If STRLEN is a valid pointer, length (including terminating character)
14562 of returned string is stored to the argument. */
14564 const char *
14565 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14567 tree offset_node;
14569 if (strlen)
14570 *strlen = 0;
14572 src = string_constant (src, &offset_node);
14573 if (src == 0)
14574 return NULL;
14576 unsigned HOST_WIDE_INT offset = 0;
14577 if (offset_node != NULL_TREE)
14579 if (!tree_fits_uhwi_p (offset_node))
14580 return NULL;
14581 else
14582 offset = tree_to_uhwi (offset_node);
14585 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14586 const char *string = TREE_STRING_POINTER (src);
14588 /* Support only properly null-terminated strings. */
14589 if (string_length == 0
14590 || string[string_length - 1] != '\0'
14591 || offset >= string_length)
14592 return NULL;
14594 if (strlen)
14595 *strlen = string_length - offset;
14596 return string + offset;
14599 #if CHECKING_P
14601 namespace selftest {
14603 /* Helper functions for writing tests of folding trees. */
14605 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14607 static void
14608 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14609 tree constant)
14611 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14614 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14615 wrapping WRAPPED_EXPR. */
14617 static void
14618 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14619 tree wrapped_expr)
14621 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14622 ASSERT_NE (wrapped_expr, result);
14623 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14624 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14627 /* Verify that various arithmetic binary operations are folded
14628 correctly. */
14630 static void
14631 test_arithmetic_folding ()
14633 tree type = integer_type_node;
14634 tree x = create_tmp_var_raw (type, "x");
14635 tree zero = build_zero_cst (type);
14636 tree one = build_int_cst (type, 1);
14638 /* Addition. */
14639 /* 1 <-- (0 + 1) */
14640 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14641 one);
14642 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14643 one);
14645 /* (nonlvalue)x <-- (x + 0) */
14646 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14649 /* Subtraction. */
14650 /* 0 <-- (x - x) */
14651 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14652 zero);
14653 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14656 /* Multiplication. */
14657 /* 0 <-- (x * 0) */
14658 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14659 zero);
14661 /* (nonlvalue)x <-- (x * 1) */
14662 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14666 /* Verify that various binary operations on vectors are folded
14667 correctly. */
14669 static void
14670 test_vector_folding ()
14672 tree inner_type = integer_type_node;
14673 tree type = build_vector_type (inner_type, 4);
14674 tree zero = build_zero_cst (type);
14675 tree one = build_one_cst (type);
14677 /* Verify equality tests that return a scalar boolean result. */
14678 tree res_type = boolean_type_node;
14679 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14680 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14681 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14682 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14685 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14687 static void
14688 test_vec_duplicate_folding ()
14690 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14691 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14692 /* This will be 1 if VEC_MODE isn't a vector mode. */
14693 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14695 tree type = build_vector_type (ssizetype, nunits);
14696 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14697 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14698 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14701 /* Run all of the selftests within this file. */
14703 void
14704 fold_const_c_tests ()
14706 test_arithmetic_folding ();
14707 test_vector_folding ();
14708 test_vec_duplicate_folding ();
14711 } // namespace selftest
14713 #endif /* CHECKING_P */