Use hard_regno_nregs instead of HARD_REGNO_NREGS
[official-gcc.git] / gcc / fold-const.c
blob16bf45694bfe5e301caa527542d861be49c0647d
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
85 /* Nonzero if we are folding constants inside an initializer; zero
86 otherwise. */
87 int folding_initializer = 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code {
93 COMPCODE_FALSE = 0,
94 COMPCODE_LT = 1,
95 COMPCODE_EQ = 2,
96 COMPCODE_LE = 3,
97 COMPCODE_GT = 4,
98 COMPCODE_LTGT = 5,
99 COMPCODE_GE = 6,
100 COMPCODE_ORD = 7,
101 COMPCODE_UNORD = 8,
102 COMPCODE_UNLT = 9,
103 COMPCODE_UNEQ = 10,
104 COMPCODE_UNLE = 11,
105 COMPCODE_UNGT = 12,
106 COMPCODE_NE = 13,
107 COMPCODE_UNGE = 14,
108 COMPCODE_TRUE = 15
111 static bool negate_expr_p (tree);
112 static tree negate_expr (tree);
113 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
114 static enum comparison_code comparison_to_compcode (enum tree_code);
115 static enum tree_code compcode_to_comparison (enum comparison_code);
116 static int twoval_comparison_p (tree, tree *, tree *, int *);
117 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
118 static tree optimize_bit_field_compare (location_t, enum tree_code,
119 tree, tree, tree);
120 static int simple_operand_p (const_tree);
121 static bool simple_operand_p_2 (tree);
122 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
123 static tree range_predecessor (tree);
124 static tree range_successor (tree);
125 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
126 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
127 static tree unextend (tree, int, int, tree);
128 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
129 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
130 static tree fold_binary_op_with_conditional_arg (location_t,
131 enum tree_code, tree,
132 tree, tree,
133 tree, tree, int);
134 static tree fold_negate_const (tree, tree);
135 static tree fold_not_const (const_tree, tree);
136 static tree fold_relational_const (enum tree_code, tree, tree, tree);
137 static tree fold_convert_const (enum tree_code, tree, tree);
138 static tree fold_view_convert_expr (tree, tree);
139 static bool vec_cst_ctor_to_array (tree, tree *);
140 static tree fold_negate_expr (location_t, tree);
143 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
144 Otherwise, return LOC. */
146 static location_t
147 expr_location_or (tree t, location_t loc)
149 location_t tloc = EXPR_LOCATION (t);
150 return tloc == UNKNOWN_LOCATION ? loc : tloc;
153 /* Similar to protected_set_expr_location, but never modify x in place,
154 if location can and needs to be set, unshare it. */
156 static inline tree
157 protected_set_expr_location_unshare (tree x, location_t loc)
159 if (CAN_HAVE_LOCATION_P (x)
160 && EXPR_LOCATION (x) != loc
161 && !(TREE_CODE (x) == SAVE_EXPR
162 || TREE_CODE (x) == TARGET_EXPR
163 || TREE_CODE (x) == BIND_EXPR))
165 x = copy_node (x);
166 SET_EXPR_LOCATION (x, loc);
168 return x;
171 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
172 division and returns the quotient. Otherwise returns
173 NULL_TREE. */
175 tree
176 div_if_zero_remainder (const_tree arg1, const_tree arg2)
178 widest_int quo;
180 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
181 SIGNED, &quo))
182 return wide_int_to_tree (TREE_TYPE (arg1), quo);
184 return NULL_TREE;
187 /* This is nonzero if we should defer warnings about undefined
188 overflow. This facility exists because these warnings are a
189 special case. The code to estimate loop iterations does not want
190 to issue any warnings, since it works with expressions which do not
191 occur in user code. Various bits of cleanup code call fold(), but
192 only use the result if it has certain characteristics (e.g., is a
193 constant); that code only wants to issue a warning if the result is
194 used. */
196 static int fold_deferring_overflow_warnings;
198 /* If a warning about undefined overflow is deferred, this is the
199 warning. Note that this may cause us to turn two warnings into
200 one, but that is fine since it is sufficient to only give one
201 warning per expression. */
203 static const char* fold_deferred_overflow_warning;
205 /* If a warning about undefined overflow is deferred, this is the
206 level at which the warning should be emitted. */
208 static enum warn_strict_overflow_code fold_deferred_overflow_code;
210 /* Start deferring overflow warnings. We could use a stack here to
211 permit nested calls, but at present it is not necessary. */
213 void
214 fold_defer_overflow_warnings (void)
216 ++fold_deferring_overflow_warnings;
219 /* Stop deferring overflow warnings. If there is a pending warning,
220 and ISSUE is true, then issue the warning if appropriate. STMT is
221 the statement with which the warning should be associated (used for
222 location information); STMT may be NULL. CODE is the level of the
223 warning--a warn_strict_overflow_code value. This function will use
224 the smaller of CODE and the deferred code when deciding whether to
225 issue the warning. CODE may be zero to mean to always use the
226 deferred code. */
228 void
229 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
231 const char *warnmsg;
232 location_t locus;
234 gcc_assert (fold_deferring_overflow_warnings > 0);
235 --fold_deferring_overflow_warnings;
236 if (fold_deferring_overflow_warnings > 0)
238 if (fold_deferred_overflow_warning != NULL
239 && code != 0
240 && code < (int) fold_deferred_overflow_code)
241 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
242 return;
245 warnmsg = fold_deferred_overflow_warning;
246 fold_deferred_overflow_warning = NULL;
248 if (!issue || warnmsg == NULL)
249 return;
251 if (gimple_no_warning_p (stmt))
252 return;
254 /* Use the smallest code level when deciding to issue the
255 warning. */
256 if (code == 0 || code > (int) fold_deferred_overflow_code)
257 code = fold_deferred_overflow_code;
259 if (!issue_strict_overflow_warning (code))
260 return;
262 if (stmt == NULL)
263 locus = input_location;
264 else
265 locus = gimple_location (stmt);
266 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
269 /* Stop deferring overflow warnings, ignoring any deferred
270 warnings. */
272 void
273 fold_undefer_and_ignore_overflow_warnings (void)
275 fold_undefer_overflow_warnings (false, NULL, 0);
278 /* Whether we are deferring overflow warnings. */
280 bool
281 fold_deferring_overflow_warnings_p (void)
283 return fold_deferring_overflow_warnings > 0;
286 /* This is called when we fold something based on the fact that signed
287 overflow is undefined. */
289 void
290 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
292 if (fold_deferring_overflow_warnings > 0)
294 if (fold_deferred_overflow_warning == NULL
295 || wc < fold_deferred_overflow_code)
297 fold_deferred_overflow_warning = gmsgid;
298 fold_deferred_overflow_code = wc;
301 else if (issue_strict_overflow_warning (wc))
302 warning (OPT_Wstrict_overflow, gmsgid);
305 /* Return true if the built-in mathematical function specified by CODE
306 is odd, i.e. -f(x) == f(-x). */
308 bool
309 negate_mathfn_p (combined_fn fn)
311 switch (fn)
313 CASE_CFN_ASIN:
314 CASE_CFN_ASINH:
315 CASE_CFN_ATAN:
316 CASE_CFN_ATANH:
317 CASE_CFN_CASIN:
318 CASE_CFN_CASINH:
319 CASE_CFN_CATAN:
320 CASE_CFN_CATANH:
321 CASE_CFN_CBRT:
322 CASE_CFN_CPROJ:
323 CASE_CFN_CSIN:
324 CASE_CFN_CSINH:
325 CASE_CFN_CTAN:
326 CASE_CFN_CTANH:
327 CASE_CFN_ERF:
328 CASE_CFN_LLROUND:
329 CASE_CFN_LROUND:
330 CASE_CFN_ROUND:
331 CASE_CFN_SIN:
332 CASE_CFN_SINH:
333 CASE_CFN_TAN:
334 CASE_CFN_TANH:
335 CASE_CFN_TRUNC:
336 return true;
338 CASE_CFN_LLRINT:
339 CASE_CFN_LRINT:
340 CASE_CFN_NEARBYINT:
341 CASE_CFN_RINT:
342 return !flag_rounding_math;
344 default:
345 break;
347 return false;
350 /* Check whether we may negate an integer constant T without causing
351 overflow. */
353 bool
354 may_negate_without_overflow_p (const_tree t)
356 tree type;
358 gcc_assert (TREE_CODE (t) == INTEGER_CST);
360 type = TREE_TYPE (t);
361 if (TYPE_UNSIGNED (type))
362 return false;
364 return !wi::only_sign_bit_p (t);
367 /* Determine whether an expression T can be cheaply negated using
368 the function negate_expr without introducing undefined overflow. */
370 static bool
371 negate_expr_p (tree t)
373 tree type;
375 if (t == 0)
376 return false;
378 type = TREE_TYPE (t);
380 STRIP_SIGN_NOPS (t);
381 switch (TREE_CODE (t))
383 case INTEGER_CST:
384 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
385 return true;
387 /* Check that -CST will not overflow type. */
388 return may_negate_without_overflow_p (t);
389 case BIT_NOT_EXPR:
390 return (INTEGRAL_TYPE_P (type)
391 && TYPE_OVERFLOW_WRAPS (type));
393 case FIXED_CST:
394 return true;
396 case NEGATE_EXPR:
397 return !TYPE_OVERFLOW_SANITIZED (type);
399 case REAL_CST:
400 /* We want to canonicalize to positive real constants. Pretend
401 that only negative ones can be easily negated. */
402 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
404 case COMPLEX_CST:
405 return negate_expr_p (TREE_REALPART (t))
406 && negate_expr_p (TREE_IMAGPART (t));
408 case VECTOR_CST:
410 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
411 return true;
413 int count = TYPE_VECTOR_SUBPARTS (type), i;
415 for (i = 0; i < count; i++)
416 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
417 return false;
419 return true;
422 case COMPLEX_EXPR:
423 return negate_expr_p (TREE_OPERAND (t, 0))
424 && negate_expr_p (TREE_OPERAND (t, 1));
426 case CONJ_EXPR:
427 return negate_expr_p (TREE_OPERAND (t, 0));
429 case PLUS_EXPR:
430 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
431 || HONOR_SIGNED_ZEROS (element_mode (type))
432 || (INTEGRAL_TYPE_P (type)
433 && ! TYPE_OVERFLOW_WRAPS (type)))
434 return false;
435 /* -(A + B) -> (-B) - A. */
436 if (negate_expr_p (TREE_OPERAND (t, 1)))
437 return true;
438 /* -(A + B) -> (-A) - B. */
439 return negate_expr_p (TREE_OPERAND (t, 0));
441 case MINUS_EXPR:
442 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
443 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
444 && !HONOR_SIGNED_ZEROS (element_mode (type))
445 && (! INTEGRAL_TYPE_P (type)
446 || TYPE_OVERFLOW_WRAPS (type));
448 case MULT_EXPR:
449 if (TYPE_UNSIGNED (type))
450 break;
451 /* INT_MIN/n * n doesn't overflow while negating one operand it does
452 if n is a (negative) power of two. */
453 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
454 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
455 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
456 && wi::popcount (wi::abs (TREE_OPERAND (t, 0))) != 1)
457 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
458 && wi::popcount (wi::abs (TREE_OPERAND (t, 1))) != 1)))
459 break;
461 /* Fall through. */
463 case RDIV_EXPR:
464 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
465 return negate_expr_p (TREE_OPERAND (t, 1))
466 || negate_expr_p (TREE_OPERAND (t, 0));
467 break;
469 case TRUNC_DIV_EXPR:
470 case ROUND_DIV_EXPR:
471 case EXACT_DIV_EXPR:
472 if (TYPE_UNSIGNED (type))
473 break;
474 if (negate_expr_p (TREE_OPERAND (t, 0)))
475 return true;
476 /* In general we can't negate B in A / B, because if A is INT_MIN and
477 B is 1, we may turn this into INT_MIN / -1 which is undefined
478 and actually traps on some architectures. */
479 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
480 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
481 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
482 && ! integer_onep (TREE_OPERAND (t, 1))))
483 return negate_expr_p (TREE_OPERAND (t, 1));
484 break;
486 case NOP_EXPR:
487 /* Negate -((double)float) as (double)(-float). */
488 if (TREE_CODE (type) == REAL_TYPE)
490 tree tem = strip_float_extensions (t);
491 if (tem != t)
492 return negate_expr_p (tem);
494 break;
496 case CALL_EXPR:
497 /* Negate -f(x) as f(-x). */
498 if (negate_mathfn_p (get_call_combined_fn (t)))
499 return negate_expr_p (CALL_EXPR_ARG (t, 0));
500 break;
502 case RSHIFT_EXPR:
503 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
504 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
506 tree op1 = TREE_OPERAND (t, 1);
507 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
508 return true;
510 break;
512 default:
513 break;
515 return false;
518 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
519 simplification is possible.
520 If negate_expr_p would return true for T, NULL_TREE will never be
521 returned. */
523 static tree
524 fold_negate_expr_1 (location_t loc, tree t)
526 tree type = TREE_TYPE (t);
527 tree tem;
529 switch (TREE_CODE (t))
531 /* Convert - (~A) to A + 1. */
532 case BIT_NOT_EXPR:
533 if (INTEGRAL_TYPE_P (type))
534 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
535 build_one_cst (type));
536 break;
538 case INTEGER_CST:
539 tem = fold_negate_const (t, type);
540 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
541 || (ANY_INTEGRAL_TYPE_P (type)
542 && !TYPE_OVERFLOW_TRAPS (type)
543 && TYPE_OVERFLOW_WRAPS (type))
544 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
545 return tem;
546 break;
548 case REAL_CST:
549 tem = fold_negate_const (t, type);
550 return tem;
552 case FIXED_CST:
553 tem = fold_negate_const (t, type);
554 return tem;
556 case COMPLEX_CST:
558 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
559 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
560 if (rpart && ipart)
561 return build_complex (type, rpart, ipart);
563 break;
565 case VECTOR_CST:
567 int count = TYPE_VECTOR_SUBPARTS (type), i;
568 tree *elts = XALLOCAVEC (tree, count);
570 for (i = 0; i < count; i++)
572 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
573 if (elts[i] == NULL_TREE)
574 return NULL_TREE;
577 return build_vector (type, elts);
580 case COMPLEX_EXPR:
581 if (negate_expr_p (t))
582 return fold_build2_loc (loc, COMPLEX_EXPR, type,
583 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
584 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
585 break;
587 case CONJ_EXPR:
588 if (negate_expr_p (t))
589 return fold_build1_loc (loc, CONJ_EXPR, type,
590 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
591 break;
593 case NEGATE_EXPR:
594 if (!TYPE_OVERFLOW_SANITIZED (type))
595 return TREE_OPERAND (t, 0);
596 break;
598 case PLUS_EXPR:
599 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
600 && !HONOR_SIGNED_ZEROS (element_mode (type)))
602 /* -(A + B) -> (-B) - A. */
603 if (negate_expr_p (TREE_OPERAND (t, 1)))
605 tem = negate_expr (TREE_OPERAND (t, 1));
606 return fold_build2_loc (loc, MINUS_EXPR, type,
607 tem, TREE_OPERAND (t, 0));
610 /* -(A + B) -> (-A) - B. */
611 if (negate_expr_p (TREE_OPERAND (t, 0)))
613 tem = negate_expr (TREE_OPERAND (t, 0));
614 return fold_build2_loc (loc, MINUS_EXPR, type,
615 tem, TREE_OPERAND (t, 1));
618 break;
620 case MINUS_EXPR:
621 /* - (A - B) -> B - A */
622 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
623 && !HONOR_SIGNED_ZEROS (element_mode (type)))
624 return fold_build2_loc (loc, MINUS_EXPR, type,
625 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
626 break;
628 case MULT_EXPR:
629 if (TYPE_UNSIGNED (type))
630 break;
632 /* Fall through. */
634 case RDIV_EXPR:
635 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
637 tem = TREE_OPERAND (t, 1);
638 if (negate_expr_p (tem))
639 return fold_build2_loc (loc, TREE_CODE (t), type,
640 TREE_OPERAND (t, 0), negate_expr (tem));
641 tem = TREE_OPERAND (t, 0);
642 if (negate_expr_p (tem))
643 return fold_build2_loc (loc, TREE_CODE (t), type,
644 negate_expr (tem), TREE_OPERAND (t, 1));
646 break;
648 case TRUNC_DIV_EXPR:
649 case ROUND_DIV_EXPR:
650 case EXACT_DIV_EXPR:
651 if (TYPE_UNSIGNED (type))
652 break;
653 if (negate_expr_p (TREE_OPERAND (t, 0)))
654 return fold_build2_loc (loc, TREE_CODE (t), type,
655 negate_expr (TREE_OPERAND (t, 0)),
656 TREE_OPERAND (t, 1));
657 /* In general we can't negate B in A / B, because if A is INT_MIN and
658 B is 1, we may turn this into INT_MIN / -1 which is undefined
659 and actually traps on some architectures. */
660 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
661 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
662 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
663 && ! integer_onep (TREE_OPERAND (t, 1))))
664 && negate_expr_p (TREE_OPERAND (t, 1)))
665 return fold_build2_loc (loc, TREE_CODE (t), type,
666 TREE_OPERAND (t, 0),
667 negate_expr (TREE_OPERAND (t, 1)));
668 break;
670 case NOP_EXPR:
671 /* Convert -((double)float) into (double)(-float). */
672 if (TREE_CODE (type) == REAL_TYPE)
674 tem = strip_float_extensions (t);
675 if (tem != t && negate_expr_p (tem))
676 return fold_convert_loc (loc, type, negate_expr (tem));
678 break;
680 case CALL_EXPR:
681 /* Negate -f(x) as f(-x). */
682 if (negate_mathfn_p (get_call_combined_fn (t))
683 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
685 tree fndecl, arg;
687 fndecl = get_callee_fndecl (t);
688 arg = negate_expr (CALL_EXPR_ARG (t, 0));
689 return build_call_expr_loc (loc, fndecl, 1, arg);
691 break;
693 case RSHIFT_EXPR:
694 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
695 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
697 tree op1 = TREE_OPERAND (t, 1);
698 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
700 tree ntype = TYPE_UNSIGNED (type)
701 ? signed_type_for (type)
702 : unsigned_type_for (type);
703 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
704 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
705 return fold_convert_loc (loc, type, temp);
708 break;
710 default:
711 break;
714 return NULL_TREE;
717 /* A wrapper for fold_negate_expr_1. */
719 static tree
720 fold_negate_expr (location_t loc, tree t)
722 tree type = TREE_TYPE (t);
723 STRIP_SIGN_NOPS (t);
724 tree tem = fold_negate_expr_1 (loc, t);
725 if (tem == NULL_TREE)
726 return NULL_TREE;
727 return fold_convert_loc (loc, type, tem);
730 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
731 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
732 return NULL_TREE. */
734 static tree
735 negate_expr (tree t)
737 tree type, tem;
738 location_t loc;
740 if (t == NULL_TREE)
741 return NULL_TREE;
743 loc = EXPR_LOCATION (t);
744 type = TREE_TYPE (t);
745 STRIP_SIGN_NOPS (t);
747 tem = fold_negate_expr (loc, t);
748 if (!tem)
749 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
750 return fold_convert_loc (loc, type, tem);
753 /* Split a tree IN into a constant, literal and variable parts that could be
754 combined with CODE to make IN. "constant" means an expression with
755 TREE_CONSTANT but that isn't an actual constant. CODE must be a
756 commutative arithmetic operation. Store the constant part into *CONP,
757 the literal in *LITP and return the variable part. If a part isn't
758 present, set it to null. If the tree does not decompose in this way,
759 return the entire tree as the variable part and the other parts as null.
761 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
762 case, we negate an operand that was subtracted. Except if it is a
763 literal for which we use *MINUS_LITP instead.
765 If NEGATE_P is true, we are negating all of IN, again except a literal
766 for which we use *MINUS_LITP instead. If a variable part is of pointer
767 type, it is negated after converting to TYPE. This prevents us from
768 generating illegal MINUS pointer expression. LOC is the location of
769 the converted variable part.
771 If IN is itself a literal or constant, return it as appropriate.
773 Note that we do not guarantee that any of the three values will be the
774 same type as IN, but they will have the same signedness and mode. */
776 static tree
777 split_tree (tree in, tree type, enum tree_code code,
778 tree *minus_varp, tree *conp, tree *minus_conp,
779 tree *litp, tree *minus_litp, int negate_p)
781 tree var = 0;
782 *minus_varp = 0;
783 *conp = 0;
784 *minus_conp = 0;
785 *litp = 0;
786 *minus_litp = 0;
788 /* Strip any conversions that don't change the machine mode or signedness. */
789 STRIP_SIGN_NOPS (in);
791 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
792 || TREE_CODE (in) == FIXED_CST)
793 *litp = in;
794 else if (TREE_CODE (in) == code
795 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
796 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
797 /* We can associate addition and subtraction together (even
798 though the C standard doesn't say so) for integers because
799 the value is not affected. For reals, the value might be
800 affected, so we can't. */
801 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
802 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
803 || (code == MINUS_EXPR
804 && (TREE_CODE (in) == PLUS_EXPR
805 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
807 tree op0 = TREE_OPERAND (in, 0);
808 tree op1 = TREE_OPERAND (in, 1);
809 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
810 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
812 /* First see if either of the operands is a literal, then a constant. */
813 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
814 || TREE_CODE (op0) == FIXED_CST)
815 *litp = op0, op0 = 0;
816 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
817 || TREE_CODE (op1) == FIXED_CST)
818 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
820 if (op0 != 0 && TREE_CONSTANT (op0))
821 *conp = op0, op0 = 0;
822 else if (op1 != 0 && TREE_CONSTANT (op1))
823 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
825 /* If we haven't dealt with either operand, this is not a case we can
826 decompose. Otherwise, VAR is either of the ones remaining, if any. */
827 if (op0 != 0 && op1 != 0)
828 var = in;
829 else if (op0 != 0)
830 var = op0;
831 else
832 var = op1, neg_var_p = neg1_p;
834 /* Now do any needed negations. */
835 if (neg_litp_p)
836 *minus_litp = *litp, *litp = 0;
837 if (neg_conp_p && *conp)
838 *minus_conp = *conp, *conp = 0;
839 if (neg_var_p && var)
840 *minus_varp = var, var = 0;
842 else if (TREE_CONSTANT (in))
843 *conp = in;
844 else if (TREE_CODE (in) == BIT_NOT_EXPR
845 && code == PLUS_EXPR)
847 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
848 when IN is constant. */
849 *litp = build_minus_one_cst (type);
850 *minus_varp = TREE_OPERAND (in, 0);
852 else
853 var = in;
855 if (negate_p)
857 if (*litp)
858 *minus_litp = *litp, *litp = 0;
859 else if (*minus_litp)
860 *litp = *minus_litp, *minus_litp = 0;
861 if (*conp)
862 *minus_conp = *conp, *conp = 0;
863 else if (*minus_conp)
864 *conp = *minus_conp, *minus_conp = 0;
865 if (var)
866 *minus_varp = var, var = 0;
867 else if (*minus_varp)
868 var = *minus_varp, *minus_varp = 0;
871 if (*litp
872 && TREE_OVERFLOW_P (*litp))
873 *litp = drop_tree_overflow (*litp);
874 if (*minus_litp
875 && TREE_OVERFLOW_P (*minus_litp))
876 *minus_litp = drop_tree_overflow (*minus_litp);
878 return var;
881 /* Re-associate trees split by the above function. T1 and T2 are
882 either expressions to associate or null. Return the new
883 expression, if any. LOC is the location of the new expression. If
884 we build an operation, do it in TYPE and with CODE. */
886 static tree
887 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
889 if (t1 == 0)
891 gcc_assert (t2 == 0 || code != MINUS_EXPR);
892 return t2;
894 else if (t2 == 0)
895 return t1;
897 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
898 try to fold this since we will have infinite recursion. But do
899 deal with any NEGATE_EXPRs. */
900 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
901 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
902 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
904 if (code == PLUS_EXPR)
906 if (TREE_CODE (t1) == NEGATE_EXPR)
907 return build2_loc (loc, MINUS_EXPR, type,
908 fold_convert_loc (loc, type, t2),
909 fold_convert_loc (loc, type,
910 TREE_OPERAND (t1, 0)));
911 else if (TREE_CODE (t2) == NEGATE_EXPR)
912 return build2_loc (loc, MINUS_EXPR, type,
913 fold_convert_loc (loc, type, t1),
914 fold_convert_loc (loc, type,
915 TREE_OPERAND (t2, 0)));
916 else if (integer_zerop (t2))
917 return fold_convert_loc (loc, type, t1);
919 else if (code == MINUS_EXPR)
921 if (integer_zerop (t2))
922 return fold_convert_loc (loc, type, t1);
925 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
926 fold_convert_loc (loc, type, t2));
929 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
930 fold_convert_loc (loc, type, t2));
933 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
934 for use in int_const_binop, size_binop and size_diffop. */
936 static bool
937 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
939 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
940 return false;
941 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
942 return false;
944 switch (code)
946 case LSHIFT_EXPR:
947 case RSHIFT_EXPR:
948 case LROTATE_EXPR:
949 case RROTATE_EXPR:
950 return true;
952 default:
953 break;
956 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
957 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
958 && TYPE_MODE (type1) == TYPE_MODE (type2);
962 /* Combine two integer constants ARG1 and ARG2 under operation CODE
963 to produce a new constant. Return NULL_TREE if we don't know how
964 to evaluate CODE at compile-time. */
966 static tree
967 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
968 int overflowable)
970 wide_int res;
971 tree t;
972 tree type = TREE_TYPE (arg1);
973 signop sign = TYPE_SIGN (type);
974 bool overflow = false;
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 (arg1) | TREE_OVERFLOW (parg2)));
1111 return t;
1114 tree
1115 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1117 return int_const_binop_1 (code, arg1, arg2, 1);
1120 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1121 constant. We assume ARG1 and ARG2 have the same data type, or at least
1122 are the same kind of constant and the same machine mode. Return zero if
1123 combining the constants is not allowed in the current operating mode. */
1125 static tree
1126 const_binop (enum tree_code code, tree arg1, tree arg2)
1128 /* Sanity check for the recursive cases. */
1129 if (!arg1 || !arg2)
1130 return NULL_TREE;
1132 STRIP_NOPS (arg1);
1133 STRIP_NOPS (arg2);
1135 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1137 if (code == POINTER_PLUS_EXPR)
1138 return int_const_binop (PLUS_EXPR,
1139 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1141 return int_const_binop (code, arg1, arg2);
1144 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1146 machine_mode mode;
1147 REAL_VALUE_TYPE d1;
1148 REAL_VALUE_TYPE d2;
1149 REAL_VALUE_TYPE value;
1150 REAL_VALUE_TYPE result;
1151 bool inexact;
1152 tree t, type;
1154 /* The following codes are handled by real_arithmetic. */
1155 switch (code)
1157 case PLUS_EXPR:
1158 case MINUS_EXPR:
1159 case MULT_EXPR:
1160 case RDIV_EXPR:
1161 case MIN_EXPR:
1162 case MAX_EXPR:
1163 break;
1165 default:
1166 return NULL_TREE;
1169 d1 = TREE_REAL_CST (arg1);
1170 d2 = TREE_REAL_CST (arg2);
1172 type = TREE_TYPE (arg1);
1173 mode = TYPE_MODE (type);
1175 /* Don't perform operation if we honor signaling NaNs and
1176 either operand is a signaling NaN. */
1177 if (HONOR_SNANS (mode)
1178 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1179 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1180 return NULL_TREE;
1182 /* Don't perform operation if it would raise a division
1183 by zero exception. */
1184 if (code == RDIV_EXPR
1185 && real_equal (&d2, &dconst0)
1186 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1187 return NULL_TREE;
1189 /* If either operand is a NaN, just return it. Otherwise, set up
1190 for floating-point trap; we return an overflow. */
1191 if (REAL_VALUE_ISNAN (d1))
1193 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1194 is off. */
1195 d1.signalling = 0;
1196 t = build_real (type, d1);
1197 return t;
1199 else if (REAL_VALUE_ISNAN (d2))
1201 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1202 is off. */
1203 d2.signalling = 0;
1204 t = build_real (type, d2);
1205 return t;
1208 inexact = real_arithmetic (&value, code, &d1, &d2);
1209 real_convert (&result, mode, &value);
1211 /* Don't constant fold this floating point operation if
1212 the result has overflowed and flag_trapping_math. */
1213 if (flag_trapping_math
1214 && MODE_HAS_INFINITIES (mode)
1215 && REAL_VALUE_ISINF (result)
1216 && !REAL_VALUE_ISINF (d1)
1217 && !REAL_VALUE_ISINF (d2))
1218 return NULL_TREE;
1220 /* Don't constant fold this floating point operation if the
1221 result may dependent upon the run-time rounding mode and
1222 flag_rounding_math is set, or if GCC's software emulation
1223 is unable to accurately represent the result. */
1224 if ((flag_rounding_math
1225 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1226 && (inexact || !real_identical (&result, &value)))
1227 return NULL_TREE;
1229 t = build_real (type, result);
1231 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1232 return t;
1235 if (TREE_CODE (arg1) == FIXED_CST)
1237 FIXED_VALUE_TYPE f1;
1238 FIXED_VALUE_TYPE f2;
1239 FIXED_VALUE_TYPE result;
1240 tree t, type;
1241 int sat_p;
1242 bool overflow_p;
1244 /* The following codes are handled by fixed_arithmetic. */
1245 switch (code)
1247 case PLUS_EXPR:
1248 case MINUS_EXPR:
1249 case MULT_EXPR:
1250 case TRUNC_DIV_EXPR:
1251 if (TREE_CODE (arg2) != FIXED_CST)
1252 return NULL_TREE;
1253 f2 = TREE_FIXED_CST (arg2);
1254 break;
1256 case LSHIFT_EXPR:
1257 case RSHIFT_EXPR:
1259 if (TREE_CODE (arg2) != INTEGER_CST)
1260 return NULL_TREE;
1261 wide_int w2 = arg2;
1262 f2.data.high = w2.elt (1);
1263 f2.data.low = w2.ulow ();
1264 f2.mode = SImode;
1266 break;
1268 default:
1269 return NULL_TREE;
1272 f1 = TREE_FIXED_CST (arg1);
1273 type = TREE_TYPE (arg1);
1274 sat_p = TYPE_SATURATING (type);
1275 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1276 t = build_fixed (type, result);
1277 /* Propagate overflow flags. */
1278 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1279 TREE_OVERFLOW (t) = 1;
1280 return t;
1283 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1285 tree type = TREE_TYPE (arg1);
1286 tree r1 = TREE_REALPART (arg1);
1287 tree i1 = TREE_IMAGPART (arg1);
1288 tree r2 = TREE_REALPART (arg2);
1289 tree i2 = TREE_IMAGPART (arg2);
1290 tree real, imag;
1292 switch (code)
1294 case PLUS_EXPR:
1295 case MINUS_EXPR:
1296 real = const_binop (code, r1, r2);
1297 imag = const_binop (code, i1, i2);
1298 break;
1300 case MULT_EXPR:
1301 if (COMPLEX_FLOAT_TYPE_P (type))
1302 return do_mpc_arg2 (arg1, arg2, type,
1303 /* do_nonfinite= */ folding_initializer,
1304 mpc_mul);
1306 real = const_binop (MINUS_EXPR,
1307 const_binop (MULT_EXPR, r1, r2),
1308 const_binop (MULT_EXPR, i1, i2));
1309 imag = const_binop (PLUS_EXPR,
1310 const_binop (MULT_EXPR, r1, i2),
1311 const_binop (MULT_EXPR, i1, r2));
1312 break;
1314 case RDIV_EXPR:
1315 if (COMPLEX_FLOAT_TYPE_P (type))
1316 return do_mpc_arg2 (arg1, arg2, type,
1317 /* do_nonfinite= */ folding_initializer,
1318 mpc_div);
1319 /* Fallthru. */
1320 case TRUNC_DIV_EXPR:
1321 case CEIL_DIV_EXPR:
1322 case FLOOR_DIV_EXPR:
1323 case ROUND_DIV_EXPR:
1324 if (flag_complex_method == 0)
1326 /* Keep this algorithm in sync with
1327 tree-complex.c:expand_complex_div_straight().
1329 Expand complex division to scalars, straightforward algorithm.
1330 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1331 t = br*br + bi*bi
1333 tree magsquared
1334 = const_binop (PLUS_EXPR,
1335 const_binop (MULT_EXPR, r2, r2),
1336 const_binop (MULT_EXPR, i2, i2));
1337 tree t1
1338 = const_binop (PLUS_EXPR,
1339 const_binop (MULT_EXPR, r1, r2),
1340 const_binop (MULT_EXPR, i1, i2));
1341 tree t2
1342 = const_binop (MINUS_EXPR,
1343 const_binop (MULT_EXPR, i1, r2),
1344 const_binop (MULT_EXPR, r1, i2));
1346 real = const_binop (code, t1, magsquared);
1347 imag = const_binop (code, t2, magsquared);
1349 else
1351 /* Keep this algorithm in sync with
1352 tree-complex.c:expand_complex_div_wide().
1354 Expand complex division to scalars, modified algorithm to minimize
1355 overflow with wide input ranges. */
1356 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1357 fold_abs_const (r2, TREE_TYPE (type)),
1358 fold_abs_const (i2, TREE_TYPE (type)));
1360 if (integer_nonzerop (compare))
1362 /* In the TRUE branch, we compute
1363 ratio = br/bi;
1364 div = (br * ratio) + bi;
1365 tr = (ar * ratio) + ai;
1366 ti = (ai * ratio) - ar;
1367 tr = tr / div;
1368 ti = ti / div; */
1369 tree ratio = const_binop (code, r2, i2);
1370 tree div = const_binop (PLUS_EXPR, i2,
1371 const_binop (MULT_EXPR, r2, ratio));
1372 real = const_binop (MULT_EXPR, r1, ratio);
1373 real = const_binop (PLUS_EXPR, real, i1);
1374 real = const_binop (code, real, div);
1376 imag = const_binop (MULT_EXPR, i1, ratio);
1377 imag = const_binop (MINUS_EXPR, imag, r1);
1378 imag = const_binop (code, imag, div);
1380 else
1382 /* In the FALSE branch, we compute
1383 ratio = d/c;
1384 divisor = (d * ratio) + c;
1385 tr = (b * ratio) + a;
1386 ti = b - (a * ratio);
1387 tr = tr / div;
1388 ti = ti / div; */
1389 tree ratio = const_binop (code, i2, r2);
1390 tree div = const_binop (PLUS_EXPR, r2,
1391 const_binop (MULT_EXPR, i2, ratio));
1393 real = const_binop (MULT_EXPR, i1, ratio);
1394 real = const_binop (PLUS_EXPR, real, r1);
1395 real = const_binop (code, real, div);
1397 imag = const_binop (MULT_EXPR, r1, ratio);
1398 imag = const_binop (MINUS_EXPR, i1, imag);
1399 imag = const_binop (code, imag, div);
1402 break;
1404 default:
1405 return NULL_TREE;
1408 if (real && imag)
1409 return build_complex (type, real, imag);
1412 if (TREE_CODE (arg1) == VECTOR_CST
1413 && TREE_CODE (arg2) == VECTOR_CST)
1415 tree type = TREE_TYPE (arg1);
1416 int count = TYPE_VECTOR_SUBPARTS (type), i;
1417 tree *elts = XALLOCAVEC (tree, count);
1419 for (i = 0; i < count; i++)
1421 tree elem1 = VECTOR_CST_ELT (arg1, i);
1422 tree elem2 = VECTOR_CST_ELT (arg2, i);
1424 elts[i] = const_binop (code, elem1, elem2);
1426 /* It is possible that const_binop cannot handle the given
1427 code and return NULL_TREE */
1428 if (elts[i] == NULL_TREE)
1429 return NULL_TREE;
1432 return build_vector (type, elts);
1435 /* Shifts allow a scalar offset for a vector. */
1436 if (TREE_CODE (arg1) == VECTOR_CST
1437 && TREE_CODE (arg2) == INTEGER_CST)
1439 tree type = TREE_TYPE (arg1);
1440 int count = TYPE_VECTOR_SUBPARTS (type), i;
1441 tree *elts = XALLOCAVEC (tree, count);
1443 for (i = 0; i < count; i++)
1445 tree elem1 = VECTOR_CST_ELT (arg1, i);
1447 elts[i] = const_binop (code, elem1, arg2);
1449 /* It is possible that const_binop cannot handle the given
1450 code and return NULL_TREE. */
1451 if (elts[i] == NULL_TREE)
1452 return NULL_TREE;
1455 return build_vector (type, elts);
1457 return NULL_TREE;
1460 /* Overload that adds a TYPE parameter to be able to dispatch
1461 to fold_relational_const. */
1463 tree
1464 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1466 if (TREE_CODE_CLASS (code) == tcc_comparison)
1467 return fold_relational_const (code, type, arg1, arg2);
1469 /* ??? Until we make the const_binop worker take the type of the
1470 result as argument put those cases that need it here. */
1471 switch (code)
1473 case COMPLEX_EXPR:
1474 if ((TREE_CODE (arg1) == REAL_CST
1475 && TREE_CODE (arg2) == REAL_CST)
1476 || (TREE_CODE (arg1) == INTEGER_CST
1477 && TREE_CODE (arg2) == INTEGER_CST))
1478 return build_complex (type, arg1, arg2);
1479 return NULL_TREE;
1481 case VEC_PACK_TRUNC_EXPR:
1482 case VEC_PACK_FIX_TRUNC_EXPR:
1484 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1485 tree *elts;
1487 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1488 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1489 if (TREE_CODE (arg1) != VECTOR_CST
1490 || TREE_CODE (arg2) != VECTOR_CST)
1491 return NULL_TREE;
1493 elts = XALLOCAVEC (tree, nelts);
1494 if (!vec_cst_ctor_to_array (arg1, elts)
1495 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1496 return NULL_TREE;
1498 for (i = 0; i < nelts; i++)
1500 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1501 ? NOP_EXPR : FIX_TRUNC_EXPR,
1502 TREE_TYPE (type), elts[i]);
1503 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1504 return NULL_TREE;
1507 return build_vector (type, elts);
1510 case VEC_WIDEN_MULT_LO_EXPR:
1511 case VEC_WIDEN_MULT_HI_EXPR:
1512 case VEC_WIDEN_MULT_EVEN_EXPR:
1513 case VEC_WIDEN_MULT_ODD_EXPR:
1515 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1516 unsigned int out, ofs, scale;
1517 tree *elts;
1519 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1520 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1521 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1522 return NULL_TREE;
1524 elts = XALLOCAVEC (tree, nelts * 4);
1525 if (!vec_cst_ctor_to_array (arg1, elts)
1526 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1527 return NULL_TREE;
1529 if (code == VEC_WIDEN_MULT_LO_EXPR)
1530 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1531 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1532 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1533 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1534 scale = 1, ofs = 0;
1535 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1536 scale = 1, ofs = 1;
1538 for (out = 0; out < nelts; out++)
1540 unsigned int in1 = (out << scale) + ofs;
1541 unsigned int in2 = in1 + nelts * 2;
1542 tree t1, t2;
1544 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1545 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1547 if (t1 == NULL_TREE || t2 == NULL_TREE)
1548 return NULL_TREE;
1549 elts[out] = const_binop (MULT_EXPR, t1, t2);
1550 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1551 return NULL_TREE;
1554 return build_vector (type, elts);
1557 default:;
1560 if (TREE_CODE_CLASS (code) != tcc_binary)
1561 return NULL_TREE;
1563 /* Make sure type and arg0 have the same saturating flag. */
1564 gcc_checking_assert (TYPE_SATURATING (type)
1565 == TYPE_SATURATING (TREE_TYPE (arg1)));
1567 return const_binop (code, arg1, arg2);
1570 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1571 Return zero if computing the constants is not possible. */
1573 tree
1574 const_unop (enum tree_code code, tree type, tree arg0)
1576 /* Don't perform the operation, other than NEGATE and ABS, if
1577 flag_signaling_nans is on and the operand is a signaling NaN. */
1578 if (TREE_CODE (arg0) == REAL_CST
1579 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1580 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1581 && code != NEGATE_EXPR
1582 && code != ABS_EXPR)
1583 return NULL_TREE;
1585 switch (code)
1587 CASE_CONVERT:
1588 case FLOAT_EXPR:
1589 case FIX_TRUNC_EXPR:
1590 case FIXED_CONVERT_EXPR:
1591 return fold_convert_const (code, type, arg0);
1593 case ADDR_SPACE_CONVERT_EXPR:
1594 /* If the source address is 0, and the source address space
1595 cannot have a valid object at 0, fold to dest type null. */
1596 if (integer_zerop (arg0)
1597 && !(targetm.addr_space.zero_address_valid
1598 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1599 return fold_convert_const (code, type, arg0);
1600 break;
1602 case VIEW_CONVERT_EXPR:
1603 return fold_view_convert_expr (type, arg0);
1605 case NEGATE_EXPR:
1607 /* Can't call fold_negate_const directly here as that doesn't
1608 handle all cases and we might not be able to negate some
1609 constants. */
1610 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1611 if (tem && CONSTANT_CLASS_P (tem))
1612 return tem;
1613 break;
1616 case ABS_EXPR:
1617 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1618 return fold_abs_const (arg0, type);
1619 break;
1621 case CONJ_EXPR:
1622 if (TREE_CODE (arg0) == COMPLEX_CST)
1624 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1625 TREE_TYPE (type));
1626 return build_complex (type, TREE_REALPART (arg0), ipart);
1628 break;
1630 case BIT_NOT_EXPR:
1631 if (TREE_CODE (arg0) == INTEGER_CST)
1632 return fold_not_const (arg0, type);
1633 /* Perform BIT_NOT_EXPR on each element individually. */
1634 else if (TREE_CODE (arg0) == VECTOR_CST)
1636 tree *elements;
1637 tree elem;
1638 unsigned count = VECTOR_CST_NELTS (arg0), i;
1640 elements = XALLOCAVEC (tree, count);
1641 for (i = 0; i < count; i++)
1643 elem = VECTOR_CST_ELT (arg0, i);
1644 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1645 if (elem == NULL_TREE)
1646 break;
1647 elements[i] = elem;
1649 if (i == count)
1650 return build_vector (type, elements);
1652 break;
1654 case TRUTH_NOT_EXPR:
1655 if (TREE_CODE (arg0) == INTEGER_CST)
1656 return constant_boolean_node (integer_zerop (arg0), type);
1657 break;
1659 case REALPART_EXPR:
1660 if (TREE_CODE (arg0) == COMPLEX_CST)
1661 return fold_convert (type, TREE_REALPART (arg0));
1662 break;
1664 case IMAGPART_EXPR:
1665 if (TREE_CODE (arg0) == COMPLEX_CST)
1666 return fold_convert (type, TREE_IMAGPART (arg0));
1667 break;
1669 case VEC_UNPACK_LO_EXPR:
1670 case VEC_UNPACK_HI_EXPR:
1671 case VEC_UNPACK_FLOAT_LO_EXPR:
1672 case VEC_UNPACK_FLOAT_HI_EXPR:
1674 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1675 tree *elts;
1676 enum tree_code subcode;
1678 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1679 if (TREE_CODE (arg0) != VECTOR_CST)
1680 return NULL_TREE;
1682 elts = XALLOCAVEC (tree, nelts * 2);
1683 if (!vec_cst_ctor_to_array (arg0, elts))
1684 return NULL_TREE;
1686 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1687 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1688 elts += nelts;
1690 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1691 subcode = NOP_EXPR;
1692 else
1693 subcode = FLOAT_EXPR;
1695 for (i = 0; i < nelts; i++)
1697 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1698 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1699 return NULL_TREE;
1702 return build_vector (type, elts);
1705 case REDUC_MIN_EXPR:
1706 case REDUC_MAX_EXPR:
1707 case REDUC_PLUS_EXPR:
1709 unsigned int nelts, i;
1710 tree *elts;
1711 enum tree_code subcode;
1713 if (TREE_CODE (arg0) != VECTOR_CST)
1714 return NULL_TREE;
1715 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1717 elts = XALLOCAVEC (tree, nelts);
1718 if (!vec_cst_ctor_to_array (arg0, elts))
1719 return NULL_TREE;
1721 switch (code)
1723 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1724 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1725 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1726 default: gcc_unreachable ();
1729 for (i = 1; i < nelts; i++)
1731 elts[0] = const_binop (subcode, elts[0], elts[i]);
1732 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1733 return NULL_TREE;
1736 return elts[0];
1739 default:
1740 break;
1743 return NULL_TREE;
1746 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1747 indicates which particular sizetype to create. */
1749 tree
1750 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1752 return build_int_cst (sizetype_tab[(int) kind], number);
1755 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1756 is a tree code. The type of the result is taken from the operands.
1757 Both must be equivalent integer types, ala int_binop_types_match_p.
1758 If the operands are constant, so is the result. */
1760 tree
1761 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1763 tree type = TREE_TYPE (arg0);
1765 if (arg0 == error_mark_node || arg1 == error_mark_node)
1766 return error_mark_node;
1768 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1769 TREE_TYPE (arg1)));
1771 /* Handle the special case of two integer constants faster. */
1772 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1774 /* And some specific cases even faster than that. */
1775 if (code == PLUS_EXPR)
1777 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1778 return arg1;
1779 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1780 return arg0;
1782 else if (code == MINUS_EXPR)
1784 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1785 return arg0;
1787 else if (code == MULT_EXPR)
1789 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1790 return arg1;
1793 /* Handle general case of two integer constants. For sizetype
1794 constant calculations we always want to know about overflow,
1795 even in the unsigned case. */
1796 return int_const_binop_1 (code, arg0, arg1, -1);
1799 return fold_build2_loc (loc, code, type, arg0, arg1);
1802 /* Given two values, either both of sizetype or both of bitsizetype,
1803 compute the difference between the two values. Return the value
1804 in signed type corresponding to the type of the operands. */
1806 tree
1807 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1809 tree type = TREE_TYPE (arg0);
1810 tree ctype;
1812 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1813 TREE_TYPE (arg1)));
1815 /* If the type is already signed, just do the simple thing. */
1816 if (!TYPE_UNSIGNED (type))
1817 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1819 if (type == sizetype)
1820 ctype = ssizetype;
1821 else if (type == bitsizetype)
1822 ctype = sbitsizetype;
1823 else
1824 ctype = signed_type_for (type);
1826 /* If either operand is not a constant, do the conversions to the signed
1827 type and subtract. The hardware will do the right thing with any
1828 overflow in the subtraction. */
1829 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1830 return size_binop_loc (loc, MINUS_EXPR,
1831 fold_convert_loc (loc, ctype, arg0),
1832 fold_convert_loc (loc, ctype, arg1));
1834 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1835 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1836 overflow) and negate (which can't either). Special-case a result
1837 of zero while we're here. */
1838 if (tree_int_cst_equal (arg0, arg1))
1839 return build_int_cst (ctype, 0);
1840 else if (tree_int_cst_lt (arg1, arg0))
1841 return fold_convert_loc (loc, ctype,
1842 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1843 else
1844 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1845 fold_convert_loc (loc, ctype,
1846 size_binop_loc (loc,
1847 MINUS_EXPR,
1848 arg1, arg0)));
1851 /* A subroutine of fold_convert_const handling conversions of an
1852 INTEGER_CST to another integer type. */
1854 static tree
1855 fold_convert_const_int_from_int (tree type, const_tree arg1)
1857 /* Given an integer constant, make new constant with new type,
1858 appropriately sign-extended or truncated. Use widest_int
1859 so that any extension is done according ARG1's type. */
1860 return force_fit_type (type, wi::to_widest (arg1),
1861 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1862 TREE_OVERFLOW (arg1));
1865 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1866 to an integer type. */
1868 static tree
1869 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1871 bool overflow = false;
1872 tree t;
1874 /* The following code implements the floating point to integer
1875 conversion rules required by the Java Language Specification,
1876 that IEEE NaNs are mapped to zero and values that overflow
1877 the target precision saturate, i.e. values greater than
1878 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1879 are mapped to INT_MIN. These semantics are allowed by the
1880 C and C++ standards that simply state that the behavior of
1881 FP-to-integer conversion is unspecified upon overflow. */
1883 wide_int val;
1884 REAL_VALUE_TYPE r;
1885 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1887 switch (code)
1889 case FIX_TRUNC_EXPR:
1890 real_trunc (&r, VOIDmode, &x);
1891 break;
1893 default:
1894 gcc_unreachable ();
1897 /* If R is NaN, return zero and show we have an overflow. */
1898 if (REAL_VALUE_ISNAN (r))
1900 overflow = true;
1901 val = wi::zero (TYPE_PRECISION (type));
1904 /* See if R is less than the lower bound or greater than the
1905 upper bound. */
1907 if (! overflow)
1909 tree lt = TYPE_MIN_VALUE (type);
1910 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1911 if (real_less (&r, &l))
1913 overflow = true;
1914 val = lt;
1918 if (! overflow)
1920 tree ut = TYPE_MAX_VALUE (type);
1921 if (ut)
1923 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1924 if (real_less (&u, &r))
1926 overflow = true;
1927 val = ut;
1932 if (! overflow)
1933 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1935 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1936 return t;
1939 /* A subroutine of fold_convert_const handling conversions of a
1940 FIXED_CST to an integer type. */
1942 static tree
1943 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1945 tree t;
1946 double_int temp, temp_trunc;
1947 scalar_mode mode;
1949 /* Right shift FIXED_CST to temp by fbit. */
1950 temp = TREE_FIXED_CST (arg1).data;
1951 mode = TREE_FIXED_CST (arg1).mode;
1952 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1954 temp = temp.rshift (GET_MODE_FBIT (mode),
1955 HOST_BITS_PER_DOUBLE_INT,
1956 SIGNED_FIXED_POINT_MODE_P (mode));
1958 /* Left shift temp to temp_trunc by fbit. */
1959 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1960 HOST_BITS_PER_DOUBLE_INT,
1961 SIGNED_FIXED_POINT_MODE_P (mode));
1963 else
1965 temp = double_int_zero;
1966 temp_trunc = double_int_zero;
1969 /* If FIXED_CST is negative, we need to round the value toward 0.
1970 By checking if the fractional bits are not zero to add 1 to temp. */
1971 if (SIGNED_FIXED_POINT_MODE_P (mode)
1972 && temp_trunc.is_negative ()
1973 && TREE_FIXED_CST (arg1).data != temp_trunc)
1974 temp += double_int_one;
1976 /* Given a fixed-point constant, make new constant with new type,
1977 appropriately sign-extended or truncated. */
1978 t = force_fit_type (type, temp, -1,
1979 (temp.is_negative ()
1980 && (TYPE_UNSIGNED (type)
1981 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1982 | TREE_OVERFLOW (arg1));
1984 return t;
1987 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1988 to another floating point type. */
1990 static tree
1991 fold_convert_const_real_from_real (tree type, const_tree arg1)
1993 REAL_VALUE_TYPE value;
1994 tree t;
1996 /* Don't perform the operation if flag_signaling_nans is on
1997 and the operand is a signaling NaN. */
1998 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1999 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2000 return NULL_TREE;
2002 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2003 t = build_real (type, value);
2005 /* If converting an infinity or NAN to a representation that doesn't
2006 have one, set the overflow bit so that we can produce some kind of
2007 error message at the appropriate point if necessary. It's not the
2008 most user-friendly message, but it's better than nothing. */
2009 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2010 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2011 TREE_OVERFLOW (t) = 1;
2012 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2013 && !MODE_HAS_NANS (TYPE_MODE (type)))
2014 TREE_OVERFLOW (t) = 1;
2015 /* Regular overflow, conversion produced an infinity in a mode that
2016 can't represent them. */
2017 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2018 && REAL_VALUE_ISINF (value)
2019 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2020 TREE_OVERFLOW (t) = 1;
2021 else
2022 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2023 return t;
2026 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2027 to a floating point type. */
2029 static tree
2030 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2032 REAL_VALUE_TYPE value;
2033 tree t;
2035 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2036 &TREE_FIXED_CST (arg1));
2037 t = build_real (type, value);
2039 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2040 return t;
2043 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2044 to another fixed-point type. */
2046 static tree
2047 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2049 FIXED_VALUE_TYPE value;
2050 tree t;
2051 bool overflow_p;
2053 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2054 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2055 t = build_fixed (type, value);
2057 /* Propagate overflow flags. */
2058 if (overflow_p | TREE_OVERFLOW (arg1))
2059 TREE_OVERFLOW (t) = 1;
2060 return t;
2063 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2064 to a fixed-point type. */
2066 static tree
2067 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2069 FIXED_VALUE_TYPE value;
2070 tree t;
2071 bool overflow_p;
2072 double_int di;
2074 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2076 di.low = TREE_INT_CST_ELT (arg1, 0);
2077 if (TREE_INT_CST_NUNITS (arg1) == 1)
2078 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2079 else
2080 di.high = TREE_INT_CST_ELT (arg1, 1);
2082 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2083 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2084 TYPE_SATURATING (type));
2085 t = build_fixed (type, value);
2087 /* Propagate overflow flags. */
2088 if (overflow_p | TREE_OVERFLOW (arg1))
2089 TREE_OVERFLOW (t) = 1;
2090 return t;
2093 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2094 to a fixed-point type. */
2096 static tree
2097 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2099 FIXED_VALUE_TYPE value;
2100 tree t;
2101 bool overflow_p;
2103 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2104 &TREE_REAL_CST (arg1),
2105 TYPE_SATURATING (type));
2106 t = build_fixed (type, value);
2108 /* Propagate overflow flags. */
2109 if (overflow_p | TREE_OVERFLOW (arg1))
2110 TREE_OVERFLOW (t) = 1;
2111 return t;
2114 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2115 type TYPE. If no simplification can be done return NULL_TREE. */
2117 static tree
2118 fold_convert_const (enum tree_code code, tree type, tree arg1)
2120 if (TREE_TYPE (arg1) == type)
2121 return arg1;
2123 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2124 || TREE_CODE (type) == OFFSET_TYPE)
2126 if (TREE_CODE (arg1) == INTEGER_CST)
2127 return fold_convert_const_int_from_int (type, arg1);
2128 else if (TREE_CODE (arg1) == REAL_CST)
2129 return fold_convert_const_int_from_real (code, type, arg1);
2130 else if (TREE_CODE (arg1) == FIXED_CST)
2131 return fold_convert_const_int_from_fixed (type, arg1);
2133 else if (TREE_CODE (type) == REAL_TYPE)
2135 if (TREE_CODE (arg1) == INTEGER_CST)
2136 return build_real_from_int_cst (type, arg1);
2137 else if (TREE_CODE (arg1) == REAL_CST)
2138 return fold_convert_const_real_from_real (type, arg1);
2139 else if (TREE_CODE (arg1) == FIXED_CST)
2140 return fold_convert_const_real_from_fixed (type, arg1);
2142 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2144 if (TREE_CODE (arg1) == FIXED_CST)
2145 return fold_convert_const_fixed_from_fixed (type, arg1);
2146 else if (TREE_CODE (arg1) == INTEGER_CST)
2147 return fold_convert_const_fixed_from_int (type, arg1);
2148 else if (TREE_CODE (arg1) == REAL_CST)
2149 return fold_convert_const_fixed_from_real (type, arg1);
2151 else if (TREE_CODE (type) == VECTOR_TYPE)
2153 if (TREE_CODE (arg1) == VECTOR_CST
2154 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2156 int len = TYPE_VECTOR_SUBPARTS (type);
2157 tree elttype = TREE_TYPE (type);
2158 tree *v = XALLOCAVEC (tree, len);
2159 for (int i = 0; i < len; ++i)
2161 tree elt = VECTOR_CST_ELT (arg1, i);
2162 tree cvt = fold_convert_const (code, elttype, elt);
2163 if (cvt == NULL_TREE)
2164 return NULL_TREE;
2165 v[i] = cvt;
2167 return build_vector (type, v);
2170 return NULL_TREE;
2173 /* Construct a vector of zero elements of vector type TYPE. */
2175 static tree
2176 build_zero_vector (tree type)
2178 tree t;
2180 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2181 return build_vector_from_val (type, t);
2184 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2186 bool
2187 fold_convertible_p (const_tree type, const_tree arg)
2189 tree orig = TREE_TYPE (arg);
2191 if (type == orig)
2192 return true;
2194 if (TREE_CODE (arg) == ERROR_MARK
2195 || TREE_CODE (type) == ERROR_MARK
2196 || TREE_CODE (orig) == ERROR_MARK)
2197 return false;
2199 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2200 return true;
2202 switch (TREE_CODE (type))
2204 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2205 case POINTER_TYPE: case REFERENCE_TYPE:
2206 case OFFSET_TYPE:
2207 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2208 || TREE_CODE (orig) == OFFSET_TYPE);
2210 case REAL_TYPE:
2211 case FIXED_POINT_TYPE:
2212 case VECTOR_TYPE:
2213 case VOID_TYPE:
2214 return TREE_CODE (type) == TREE_CODE (orig);
2216 default:
2217 return false;
2221 /* Convert expression ARG to type TYPE. Used by the middle-end for
2222 simple conversions in preference to calling the front-end's convert. */
2224 tree
2225 fold_convert_loc (location_t loc, tree type, tree arg)
2227 tree orig = TREE_TYPE (arg);
2228 tree tem;
2230 if (type == orig)
2231 return arg;
2233 if (TREE_CODE (arg) == ERROR_MARK
2234 || TREE_CODE (type) == ERROR_MARK
2235 || TREE_CODE (orig) == ERROR_MARK)
2236 return error_mark_node;
2238 switch (TREE_CODE (type))
2240 case POINTER_TYPE:
2241 case REFERENCE_TYPE:
2242 /* Handle conversions between pointers to different address spaces. */
2243 if (POINTER_TYPE_P (orig)
2244 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2245 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2246 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2247 /* fall through */
2249 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2250 case OFFSET_TYPE:
2251 if (TREE_CODE (arg) == INTEGER_CST)
2253 tem = fold_convert_const (NOP_EXPR, type, arg);
2254 if (tem != NULL_TREE)
2255 return tem;
2257 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2258 || TREE_CODE (orig) == OFFSET_TYPE)
2259 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2260 if (TREE_CODE (orig) == COMPLEX_TYPE)
2261 return fold_convert_loc (loc, type,
2262 fold_build1_loc (loc, REALPART_EXPR,
2263 TREE_TYPE (orig), arg));
2264 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2265 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2266 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2268 case REAL_TYPE:
2269 if (TREE_CODE (arg) == INTEGER_CST)
2271 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2272 if (tem != NULL_TREE)
2273 return tem;
2275 else if (TREE_CODE (arg) == REAL_CST)
2277 tem = fold_convert_const (NOP_EXPR, type, arg);
2278 if (tem != NULL_TREE)
2279 return tem;
2281 else if (TREE_CODE (arg) == FIXED_CST)
2283 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2284 if (tem != NULL_TREE)
2285 return tem;
2288 switch (TREE_CODE (orig))
2290 case INTEGER_TYPE:
2291 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2292 case POINTER_TYPE: case REFERENCE_TYPE:
2293 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2295 case REAL_TYPE:
2296 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2298 case FIXED_POINT_TYPE:
2299 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2301 case COMPLEX_TYPE:
2302 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2303 return fold_convert_loc (loc, type, tem);
2305 default:
2306 gcc_unreachable ();
2309 case FIXED_POINT_TYPE:
2310 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2311 || TREE_CODE (arg) == REAL_CST)
2313 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2314 if (tem != NULL_TREE)
2315 goto fold_convert_exit;
2318 switch (TREE_CODE (orig))
2320 case FIXED_POINT_TYPE:
2321 case INTEGER_TYPE:
2322 case ENUMERAL_TYPE:
2323 case BOOLEAN_TYPE:
2324 case REAL_TYPE:
2325 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2327 case COMPLEX_TYPE:
2328 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2329 return fold_convert_loc (loc, type, tem);
2331 default:
2332 gcc_unreachable ();
2335 case COMPLEX_TYPE:
2336 switch (TREE_CODE (orig))
2338 case INTEGER_TYPE:
2339 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2340 case POINTER_TYPE: case REFERENCE_TYPE:
2341 case REAL_TYPE:
2342 case FIXED_POINT_TYPE:
2343 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2344 fold_convert_loc (loc, TREE_TYPE (type), arg),
2345 fold_convert_loc (loc, TREE_TYPE (type),
2346 integer_zero_node));
2347 case COMPLEX_TYPE:
2349 tree rpart, ipart;
2351 if (TREE_CODE (arg) == COMPLEX_EXPR)
2353 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2354 TREE_OPERAND (arg, 0));
2355 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2356 TREE_OPERAND (arg, 1));
2357 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2360 arg = save_expr (arg);
2361 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2362 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2363 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2364 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2365 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2368 default:
2369 gcc_unreachable ();
2372 case VECTOR_TYPE:
2373 if (integer_zerop (arg))
2374 return build_zero_vector (type);
2375 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2376 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2377 || TREE_CODE (orig) == VECTOR_TYPE);
2378 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2380 case VOID_TYPE:
2381 tem = fold_ignored_result (arg);
2382 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2384 default:
2385 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2386 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2387 gcc_unreachable ();
2389 fold_convert_exit:
2390 protected_set_expr_location_unshare (tem, loc);
2391 return tem;
2394 /* Return false if expr can be assumed not to be an lvalue, true
2395 otherwise. */
2397 static bool
2398 maybe_lvalue_p (const_tree x)
2400 /* We only need to wrap lvalue tree codes. */
2401 switch (TREE_CODE (x))
2403 case VAR_DECL:
2404 case PARM_DECL:
2405 case RESULT_DECL:
2406 case LABEL_DECL:
2407 case FUNCTION_DECL:
2408 case SSA_NAME:
2410 case COMPONENT_REF:
2411 case MEM_REF:
2412 case INDIRECT_REF:
2413 case ARRAY_REF:
2414 case ARRAY_RANGE_REF:
2415 case BIT_FIELD_REF:
2416 case OBJ_TYPE_REF:
2418 case REALPART_EXPR:
2419 case IMAGPART_EXPR:
2420 case PREINCREMENT_EXPR:
2421 case PREDECREMENT_EXPR:
2422 case SAVE_EXPR:
2423 case TRY_CATCH_EXPR:
2424 case WITH_CLEANUP_EXPR:
2425 case COMPOUND_EXPR:
2426 case MODIFY_EXPR:
2427 case TARGET_EXPR:
2428 case COND_EXPR:
2429 case BIND_EXPR:
2430 break;
2432 default:
2433 /* Assume the worst for front-end tree codes. */
2434 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2435 break;
2436 return false;
2439 return true;
2442 /* Return an expr equal to X but certainly not valid as an lvalue. */
2444 tree
2445 non_lvalue_loc (location_t loc, tree x)
2447 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2448 us. */
2449 if (in_gimple_form)
2450 return x;
2452 if (! maybe_lvalue_p (x))
2453 return x;
2454 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2457 /* When pedantic, return an expr equal to X but certainly not valid as a
2458 pedantic lvalue. Otherwise, return X. */
2460 static tree
2461 pedantic_non_lvalue_loc (location_t loc, tree x)
2463 return protected_set_expr_location_unshare (x, loc);
2466 /* Given a tree comparison code, return the code that is the logical inverse.
2467 It is generally not safe to do this for floating-point comparisons, except
2468 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2469 ERROR_MARK in this case. */
2471 enum tree_code
2472 invert_tree_comparison (enum tree_code code, bool honor_nans)
2474 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2475 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2476 return ERROR_MARK;
2478 switch (code)
2480 case EQ_EXPR:
2481 return NE_EXPR;
2482 case NE_EXPR:
2483 return EQ_EXPR;
2484 case GT_EXPR:
2485 return honor_nans ? UNLE_EXPR : LE_EXPR;
2486 case GE_EXPR:
2487 return honor_nans ? UNLT_EXPR : LT_EXPR;
2488 case LT_EXPR:
2489 return honor_nans ? UNGE_EXPR : GE_EXPR;
2490 case LE_EXPR:
2491 return honor_nans ? UNGT_EXPR : GT_EXPR;
2492 case LTGT_EXPR:
2493 return UNEQ_EXPR;
2494 case UNEQ_EXPR:
2495 return LTGT_EXPR;
2496 case UNGT_EXPR:
2497 return LE_EXPR;
2498 case UNGE_EXPR:
2499 return LT_EXPR;
2500 case UNLT_EXPR:
2501 return GE_EXPR;
2502 case UNLE_EXPR:
2503 return GT_EXPR;
2504 case ORDERED_EXPR:
2505 return UNORDERED_EXPR;
2506 case UNORDERED_EXPR:
2507 return ORDERED_EXPR;
2508 default:
2509 gcc_unreachable ();
2513 /* Similar, but return the comparison that results if the operands are
2514 swapped. This is safe for floating-point. */
2516 enum tree_code
2517 swap_tree_comparison (enum tree_code code)
2519 switch (code)
2521 case EQ_EXPR:
2522 case NE_EXPR:
2523 case ORDERED_EXPR:
2524 case UNORDERED_EXPR:
2525 case LTGT_EXPR:
2526 case UNEQ_EXPR:
2527 return code;
2528 case GT_EXPR:
2529 return LT_EXPR;
2530 case GE_EXPR:
2531 return LE_EXPR;
2532 case LT_EXPR:
2533 return GT_EXPR;
2534 case LE_EXPR:
2535 return GE_EXPR;
2536 case UNGT_EXPR:
2537 return UNLT_EXPR;
2538 case UNGE_EXPR:
2539 return UNLE_EXPR;
2540 case UNLT_EXPR:
2541 return UNGT_EXPR;
2542 case UNLE_EXPR:
2543 return UNGE_EXPR;
2544 default:
2545 gcc_unreachable ();
2550 /* Convert a comparison tree code from an enum tree_code representation
2551 into a compcode bit-based encoding. This function is the inverse of
2552 compcode_to_comparison. */
2554 static enum comparison_code
2555 comparison_to_compcode (enum tree_code code)
2557 switch (code)
2559 case LT_EXPR:
2560 return COMPCODE_LT;
2561 case EQ_EXPR:
2562 return COMPCODE_EQ;
2563 case LE_EXPR:
2564 return COMPCODE_LE;
2565 case GT_EXPR:
2566 return COMPCODE_GT;
2567 case NE_EXPR:
2568 return COMPCODE_NE;
2569 case GE_EXPR:
2570 return COMPCODE_GE;
2571 case ORDERED_EXPR:
2572 return COMPCODE_ORD;
2573 case UNORDERED_EXPR:
2574 return COMPCODE_UNORD;
2575 case UNLT_EXPR:
2576 return COMPCODE_UNLT;
2577 case UNEQ_EXPR:
2578 return COMPCODE_UNEQ;
2579 case UNLE_EXPR:
2580 return COMPCODE_UNLE;
2581 case UNGT_EXPR:
2582 return COMPCODE_UNGT;
2583 case LTGT_EXPR:
2584 return COMPCODE_LTGT;
2585 case UNGE_EXPR:
2586 return COMPCODE_UNGE;
2587 default:
2588 gcc_unreachable ();
2592 /* Convert a compcode bit-based encoding of a comparison operator back
2593 to GCC's enum tree_code representation. This function is the
2594 inverse of comparison_to_compcode. */
2596 static enum tree_code
2597 compcode_to_comparison (enum comparison_code code)
2599 switch (code)
2601 case COMPCODE_LT:
2602 return LT_EXPR;
2603 case COMPCODE_EQ:
2604 return EQ_EXPR;
2605 case COMPCODE_LE:
2606 return LE_EXPR;
2607 case COMPCODE_GT:
2608 return GT_EXPR;
2609 case COMPCODE_NE:
2610 return NE_EXPR;
2611 case COMPCODE_GE:
2612 return GE_EXPR;
2613 case COMPCODE_ORD:
2614 return ORDERED_EXPR;
2615 case COMPCODE_UNORD:
2616 return UNORDERED_EXPR;
2617 case COMPCODE_UNLT:
2618 return UNLT_EXPR;
2619 case COMPCODE_UNEQ:
2620 return UNEQ_EXPR;
2621 case COMPCODE_UNLE:
2622 return UNLE_EXPR;
2623 case COMPCODE_UNGT:
2624 return UNGT_EXPR;
2625 case COMPCODE_LTGT:
2626 return LTGT_EXPR;
2627 case COMPCODE_UNGE:
2628 return UNGE_EXPR;
2629 default:
2630 gcc_unreachable ();
2634 /* Return a tree for the comparison which is the combination of
2635 doing the AND or OR (depending on CODE) of the two operations LCODE
2636 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2637 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2638 if this makes the transformation invalid. */
2640 tree
2641 combine_comparisons (location_t loc,
2642 enum tree_code code, enum tree_code lcode,
2643 enum tree_code rcode, tree truth_type,
2644 tree ll_arg, tree lr_arg)
2646 bool honor_nans = HONOR_NANS (ll_arg);
2647 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2648 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2649 int compcode;
2651 switch (code)
2653 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2654 compcode = lcompcode & rcompcode;
2655 break;
2657 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2658 compcode = lcompcode | rcompcode;
2659 break;
2661 default:
2662 return NULL_TREE;
2665 if (!honor_nans)
2667 /* Eliminate unordered comparisons, as well as LTGT and ORD
2668 which are not used unless the mode has NaNs. */
2669 compcode &= ~COMPCODE_UNORD;
2670 if (compcode == COMPCODE_LTGT)
2671 compcode = COMPCODE_NE;
2672 else if (compcode == COMPCODE_ORD)
2673 compcode = COMPCODE_TRUE;
2675 else if (flag_trapping_math)
2677 /* Check that the original operation and the optimized ones will trap
2678 under the same condition. */
2679 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2680 && (lcompcode != COMPCODE_EQ)
2681 && (lcompcode != COMPCODE_ORD);
2682 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2683 && (rcompcode != COMPCODE_EQ)
2684 && (rcompcode != COMPCODE_ORD);
2685 bool trap = (compcode & COMPCODE_UNORD) == 0
2686 && (compcode != COMPCODE_EQ)
2687 && (compcode != COMPCODE_ORD);
2689 /* In a short-circuited boolean expression the LHS might be
2690 such that the RHS, if evaluated, will never trap. For
2691 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2692 if neither x nor y is NaN. (This is a mixed blessing: for
2693 example, the expression above will never trap, hence
2694 optimizing it to x < y would be invalid). */
2695 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2696 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2697 rtrap = false;
2699 /* If the comparison was short-circuited, and only the RHS
2700 trapped, we may now generate a spurious trap. */
2701 if (rtrap && !ltrap
2702 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2703 return NULL_TREE;
2705 /* If we changed the conditions that cause a trap, we lose. */
2706 if ((ltrap || rtrap) != trap)
2707 return NULL_TREE;
2710 if (compcode == COMPCODE_TRUE)
2711 return constant_boolean_node (true, truth_type);
2712 else if (compcode == COMPCODE_FALSE)
2713 return constant_boolean_node (false, truth_type);
2714 else
2716 enum tree_code tcode;
2718 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2719 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2723 /* Return nonzero if two operands (typically of the same tree node)
2724 are necessarily equal. FLAGS modifies behavior as follows:
2726 If OEP_ONLY_CONST is set, only return nonzero for constants.
2727 This function tests whether the operands are indistinguishable;
2728 it does not test whether they are equal using C's == operation.
2729 The distinction is important for IEEE floating point, because
2730 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2731 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2733 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2734 even though it may hold multiple values during a function.
2735 This is because a GCC tree node guarantees that nothing else is
2736 executed between the evaluation of its "operands" (which may often
2737 be evaluated in arbitrary order). Hence if the operands themselves
2738 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2739 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2740 unset means assuming isochronic (or instantaneous) tree equivalence.
2741 Unless comparing arbitrary expression trees, such as from different
2742 statements, this flag can usually be left unset.
2744 If OEP_PURE_SAME is set, then pure functions with identical arguments
2745 are considered the same. It is used when the caller has other ways
2746 to ensure that global memory is unchanged in between.
2748 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2749 not values of expressions.
2751 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2752 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2754 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2755 any operand with side effect. This is unnecesarily conservative in the
2756 case we know that arg0 and arg1 are in disjoint code paths (such as in
2757 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2758 addresses with TREE_CONSTANT flag set so we know that &var == &var
2759 even if var is volatile. */
2762 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2764 /* When checking, verify at the outermost operand_equal_p call that
2765 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2766 hash value. */
2767 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2769 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2771 if (arg0 != arg1)
2773 inchash::hash hstate0 (0), hstate1 (0);
2774 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2775 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2776 hashval_t h0 = hstate0.end ();
2777 hashval_t h1 = hstate1.end ();
2778 gcc_assert (h0 == h1);
2780 return 1;
2782 else
2783 return 0;
2786 /* If either is ERROR_MARK, they aren't equal. */
2787 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2788 || TREE_TYPE (arg0) == error_mark_node
2789 || TREE_TYPE (arg1) == error_mark_node)
2790 return 0;
2792 /* Similar, if either does not have a type (like a released SSA name),
2793 they aren't equal. */
2794 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2795 return 0;
2797 /* We cannot consider pointers to different address space equal. */
2798 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2799 && POINTER_TYPE_P (TREE_TYPE (arg1))
2800 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2801 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2802 return 0;
2804 /* Check equality of integer constants before bailing out due to
2805 precision differences. */
2806 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2808 /* Address of INTEGER_CST is not defined; check that we did not forget
2809 to drop the OEP_ADDRESS_OF flags. */
2810 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2811 return tree_int_cst_equal (arg0, arg1);
2814 if (!(flags & OEP_ADDRESS_OF))
2816 /* If both types don't have the same signedness, then we can't consider
2817 them equal. We must check this before the STRIP_NOPS calls
2818 because they may change the signedness of the arguments. As pointers
2819 strictly don't have a signedness, require either two pointers or
2820 two non-pointers as well. */
2821 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2822 || POINTER_TYPE_P (TREE_TYPE (arg0))
2823 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2824 return 0;
2826 /* If both types don't have the same precision, then it is not safe
2827 to strip NOPs. */
2828 if (element_precision (TREE_TYPE (arg0))
2829 != element_precision (TREE_TYPE (arg1)))
2830 return 0;
2832 STRIP_NOPS (arg0);
2833 STRIP_NOPS (arg1);
2835 #if 0
2836 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2837 sanity check once the issue is solved. */
2838 else
2839 /* Addresses of conversions and SSA_NAMEs (and many other things)
2840 are not defined. Check that we did not forget to drop the
2841 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2842 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2843 && TREE_CODE (arg0) != SSA_NAME);
2844 #endif
2846 /* In case both args are comparisons but with different comparison
2847 code, try to swap the comparison operands of one arg to produce
2848 a match and compare that variant. */
2849 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2850 && COMPARISON_CLASS_P (arg0)
2851 && COMPARISON_CLASS_P (arg1))
2853 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2855 if (TREE_CODE (arg0) == swap_code)
2856 return operand_equal_p (TREE_OPERAND (arg0, 0),
2857 TREE_OPERAND (arg1, 1), flags)
2858 && operand_equal_p (TREE_OPERAND (arg0, 1),
2859 TREE_OPERAND (arg1, 0), flags);
2862 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2864 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2865 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2867 else if (flags & OEP_ADDRESS_OF)
2869 /* If we are interested in comparing addresses ignore
2870 MEM_REF wrappings of the base that can appear just for
2871 TBAA reasons. */
2872 if (TREE_CODE (arg0) == MEM_REF
2873 && DECL_P (arg1)
2874 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2875 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2876 && integer_zerop (TREE_OPERAND (arg0, 1)))
2877 return 1;
2878 else if (TREE_CODE (arg1) == MEM_REF
2879 && DECL_P (arg0)
2880 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2881 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2882 && integer_zerop (TREE_OPERAND (arg1, 1)))
2883 return 1;
2884 return 0;
2886 else
2887 return 0;
2890 /* When not checking adddresses, this is needed for conversions and for
2891 COMPONENT_REF. Might as well play it safe and always test this. */
2892 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2893 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2894 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2895 && !(flags & OEP_ADDRESS_OF)))
2896 return 0;
2898 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2899 We don't care about side effects in that case because the SAVE_EXPR
2900 takes care of that for us. In all other cases, two expressions are
2901 equal if they have no side effects. If we have two identical
2902 expressions with side effects that should be treated the same due
2903 to the only side effects being identical SAVE_EXPR's, that will
2904 be detected in the recursive calls below.
2905 If we are taking an invariant address of two identical objects
2906 they are necessarily equal as well. */
2907 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2908 && (TREE_CODE (arg0) == SAVE_EXPR
2909 || (flags & OEP_MATCH_SIDE_EFFECTS)
2910 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2911 return 1;
2913 /* Next handle constant cases, those for which we can return 1 even
2914 if ONLY_CONST is set. */
2915 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2916 switch (TREE_CODE (arg0))
2918 case INTEGER_CST:
2919 return tree_int_cst_equal (arg0, arg1);
2921 case FIXED_CST:
2922 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2923 TREE_FIXED_CST (arg1));
2925 case REAL_CST:
2926 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2927 return 1;
2930 if (!HONOR_SIGNED_ZEROS (arg0))
2932 /* If we do not distinguish between signed and unsigned zero,
2933 consider them equal. */
2934 if (real_zerop (arg0) && real_zerop (arg1))
2935 return 1;
2937 return 0;
2939 case VECTOR_CST:
2941 unsigned i;
2943 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2944 return 0;
2946 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2948 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2949 VECTOR_CST_ELT (arg1, i), flags))
2950 return 0;
2952 return 1;
2955 case COMPLEX_CST:
2956 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2957 flags)
2958 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2959 flags));
2961 case STRING_CST:
2962 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2963 && ! memcmp (TREE_STRING_POINTER (arg0),
2964 TREE_STRING_POINTER (arg1),
2965 TREE_STRING_LENGTH (arg0)));
2967 case ADDR_EXPR:
2968 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2969 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2970 flags | OEP_ADDRESS_OF
2971 | OEP_MATCH_SIDE_EFFECTS);
2972 case CONSTRUCTOR:
2973 /* In GIMPLE empty constructors are allowed in initializers of
2974 aggregates. */
2975 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2976 default:
2977 break;
2980 if (flags & OEP_ONLY_CONST)
2981 return 0;
2983 /* Define macros to test an operand from arg0 and arg1 for equality and a
2984 variant that allows null and views null as being different from any
2985 non-null value. In the latter case, if either is null, the both
2986 must be; otherwise, do the normal comparison. */
2987 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2988 TREE_OPERAND (arg1, N), flags)
2990 #define OP_SAME_WITH_NULL(N) \
2991 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2992 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2994 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2996 case tcc_unary:
2997 /* Two conversions are equal only if signedness and modes match. */
2998 switch (TREE_CODE (arg0))
3000 CASE_CONVERT:
3001 case FIX_TRUNC_EXPR:
3002 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3003 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3004 return 0;
3005 break;
3006 default:
3007 break;
3010 return OP_SAME (0);
3013 case tcc_comparison:
3014 case tcc_binary:
3015 if (OP_SAME (0) && OP_SAME (1))
3016 return 1;
3018 /* For commutative ops, allow the other order. */
3019 return (commutative_tree_code (TREE_CODE (arg0))
3020 && operand_equal_p (TREE_OPERAND (arg0, 0),
3021 TREE_OPERAND (arg1, 1), flags)
3022 && operand_equal_p (TREE_OPERAND (arg0, 1),
3023 TREE_OPERAND (arg1, 0), flags));
3025 case tcc_reference:
3026 /* If either of the pointer (or reference) expressions we are
3027 dereferencing contain a side effect, these cannot be equal,
3028 but their addresses can be. */
3029 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3030 && (TREE_SIDE_EFFECTS (arg0)
3031 || TREE_SIDE_EFFECTS (arg1)))
3032 return 0;
3034 switch (TREE_CODE (arg0))
3036 case INDIRECT_REF:
3037 if (!(flags & OEP_ADDRESS_OF)
3038 && (TYPE_ALIGN (TREE_TYPE (arg0))
3039 != TYPE_ALIGN (TREE_TYPE (arg1))))
3040 return 0;
3041 flags &= ~OEP_ADDRESS_OF;
3042 return OP_SAME (0);
3044 case IMAGPART_EXPR:
3045 /* Require the same offset. */
3046 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3047 TYPE_SIZE (TREE_TYPE (arg1)),
3048 flags & ~OEP_ADDRESS_OF))
3049 return 0;
3051 /* Fallthru. */
3052 case REALPART_EXPR:
3053 case VIEW_CONVERT_EXPR:
3054 return OP_SAME (0);
3056 case TARGET_MEM_REF:
3057 case MEM_REF:
3058 if (!(flags & OEP_ADDRESS_OF))
3060 /* Require equal access sizes */
3061 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3062 && (!TYPE_SIZE (TREE_TYPE (arg0))
3063 || !TYPE_SIZE (TREE_TYPE (arg1))
3064 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3065 TYPE_SIZE (TREE_TYPE (arg1)),
3066 flags)))
3067 return 0;
3068 /* Verify that access happens in similar types. */
3069 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3070 return 0;
3071 /* Verify that accesses are TBAA compatible. */
3072 if (!alias_ptr_types_compatible_p
3073 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3074 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3075 || (MR_DEPENDENCE_CLIQUE (arg0)
3076 != MR_DEPENDENCE_CLIQUE (arg1))
3077 || (MR_DEPENDENCE_BASE (arg0)
3078 != MR_DEPENDENCE_BASE (arg1)))
3079 return 0;
3080 /* Verify that alignment is compatible. */
3081 if (TYPE_ALIGN (TREE_TYPE (arg0))
3082 != TYPE_ALIGN (TREE_TYPE (arg1)))
3083 return 0;
3085 flags &= ~OEP_ADDRESS_OF;
3086 return (OP_SAME (0) && OP_SAME (1)
3087 /* TARGET_MEM_REF require equal extra operands. */
3088 && (TREE_CODE (arg0) != TARGET_MEM_REF
3089 || (OP_SAME_WITH_NULL (2)
3090 && OP_SAME_WITH_NULL (3)
3091 && OP_SAME_WITH_NULL (4))));
3093 case ARRAY_REF:
3094 case ARRAY_RANGE_REF:
3095 if (!OP_SAME (0))
3096 return 0;
3097 flags &= ~OEP_ADDRESS_OF;
3098 /* Compare the array index by value if it is constant first as we
3099 may have different types but same value here. */
3100 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3101 TREE_OPERAND (arg1, 1))
3102 || OP_SAME (1))
3103 && OP_SAME_WITH_NULL (2)
3104 && OP_SAME_WITH_NULL (3)
3105 /* Compare low bound and element size as with OEP_ADDRESS_OF
3106 we have to account for the offset of the ref. */
3107 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3108 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3109 || (operand_equal_p (array_ref_low_bound
3110 (CONST_CAST_TREE (arg0)),
3111 array_ref_low_bound
3112 (CONST_CAST_TREE (arg1)), flags)
3113 && operand_equal_p (array_ref_element_size
3114 (CONST_CAST_TREE (arg0)),
3115 array_ref_element_size
3116 (CONST_CAST_TREE (arg1)),
3117 flags))));
3119 case COMPONENT_REF:
3120 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3121 may be NULL when we're called to compare MEM_EXPRs. */
3122 if (!OP_SAME_WITH_NULL (0)
3123 || !OP_SAME (1))
3124 return 0;
3125 flags &= ~OEP_ADDRESS_OF;
3126 return OP_SAME_WITH_NULL (2);
3128 case BIT_FIELD_REF:
3129 if (!OP_SAME (0))
3130 return 0;
3131 flags &= ~OEP_ADDRESS_OF;
3132 return OP_SAME (1) && OP_SAME (2);
3134 default:
3135 return 0;
3138 case tcc_expression:
3139 switch (TREE_CODE (arg0))
3141 case ADDR_EXPR:
3142 /* Be sure we pass right ADDRESS_OF flag. */
3143 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3144 return operand_equal_p (TREE_OPERAND (arg0, 0),
3145 TREE_OPERAND (arg1, 0),
3146 flags | OEP_ADDRESS_OF);
3148 case TRUTH_NOT_EXPR:
3149 return OP_SAME (0);
3151 case TRUTH_ANDIF_EXPR:
3152 case TRUTH_ORIF_EXPR:
3153 return OP_SAME (0) && OP_SAME (1);
3155 case FMA_EXPR:
3156 case WIDEN_MULT_PLUS_EXPR:
3157 case WIDEN_MULT_MINUS_EXPR:
3158 if (!OP_SAME (2))
3159 return 0;
3160 /* The multiplcation operands are commutative. */
3161 /* FALLTHRU */
3163 case TRUTH_AND_EXPR:
3164 case TRUTH_OR_EXPR:
3165 case TRUTH_XOR_EXPR:
3166 if (OP_SAME (0) && OP_SAME (1))
3167 return 1;
3169 /* Otherwise take into account this is a commutative operation. */
3170 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3171 TREE_OPERAND (arg1, 1), flags)
3172 && operand_equal_p (TREE_OPERAND (arg0, 1),
3173 TREE_OPERAND (arg1, 0), flags));
3175 case COND_EXPR:
3176 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3177 return 0;
3178 flags &= ~OEP_ADDRESS_OF;
3179 return OP_SAME (0);
3181 case BIT_INSERT_EXPR:
3182 /* BIT_INSERT_EXPR has an implict operand as the type precision
3183 of op1. Need to check to make sure they are the same. */
3184 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3185 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3186 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3187 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3188 return false;
3189 /* FALLTHRU */
3191 case VEC_COND_EXPR:
3192 case DOT_PROD_EXPR:
3193 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3195 case MODIFY_EXPR:
3196 case INIT_EXPR:
3197 case COMPOUND_EXPR:
3198 case PREDECREMENT_EXPR:
3199 case PREINCREMENT_EXPR:
3200 case POSTDECREMENT_EXPR:
3201 case POSTINCREMENT_EXPR:
3202 if (flags & OEP_LEXICOGRAPHIC)
3203 return OP_SAME (0) && OP_SAME (1);
3204 return 0;
3206 case CLEANUP_POINT_EXPR:
3207 case EXPR_STMT:
3208 if (flags & OEP_LEXICOGRAPHIC)
3209 return OP_SAME (0);
3210 return 0;
3212 default:
3213 return 0;
3216 case tcc_vl_exp:
3217 switch (TREE_CODE (arg0))
3219 case CALL_EXPR:
3220 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3221 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3222 /* If not both CALL_EXPRs are either internal or normal function
3223 functions, then they are not equal. */
3224 return 0;
3225 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3227 /* If the CALL_EXPRs call different internal functions, then they
3228 are not equal. */
3229 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3230 return 0;
3232 else
3234 /* If the CALL_EXPRs call different functions, then they are not
3235 equal. */
3236 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3237 flags))
3238 return 0;
3241 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3243 unsigned int cef = call_expr_flags (arg0);
3244 if (flags & OEP_PURE_SAME)
3245 cef &= ECF_CONST | ECF_PURE;
3246 else
3247 cef &= ECF_CONST;
3248 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3249 return 0;
3252 /* Now see if all the arguments are the same. */
3254 const_call_expr_arg_iterator iter0, iter1;
3255 const_tree a0, a1;
3256 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3257 a1 = first_const_call_expr_arg (arg1, &iter1);
3258 a0 && a1;
3259 a0 = next_const_call_expr_arg (&iter0),
3260 a1 = next_const_call_expr_arg (&iter1))
3261 if (! operand_equal_p (a0, a1, flags))
3262 return 0;
3264 /* If we get here and both argument lists are exhausted
3265 then the CALL_EXPRs are equal. */
3266 return ! (a0 || a1);
3268 default:
3269 return 0;
3272 case tcc_declaration:
3273 /* Consider __builtin_sqrt equal to sqrt. */
3274 return (TREE_CODE (arg0) == FUNCTION_DECL
3275 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3276 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3277 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3279 case tcc_exceptional:
3280 if (TREE_CODE (arg0) == CONSTRUCTOR)
3282 /* In GIMPLE constructors are used only to build vectors from
3283 elements. Individual elements in the constructor must be
3284 indexed in increasing order and form an initial sequence.
3286 We make no effort to compare constructors in generic.
3287 (see sem_variable::equals in ipa-icf which can do so for
3288 constants). */
3289 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3290 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3291 return 0;
3293 /* Be sure that vectors constructed have the same representation.
3294 We only tested element precision and modes to match.
3295 Vectors may be BLKmode and thus also check that the number of
3296 parts match. */
3297 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3298 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3299 return 0;
3301 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3302 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3303 unsigned int len = vec_safe_length (v0);
3305 if (len != vec_safe_length (v1))
3306 return 0;
3308 for (unsigned int i = 0; i < len; i++)
3310 constructor_elt *c0 = &(*v0)[i];
3311 constructor_elt *c1 = &(*v1)[i];
3313 if (!operand_equal_p (c0->value, c1->value, flags)
3314 /* In GIMPLE the indexes can be either NULL or matching i.
3315 Double check this so we won't get false
3316 positives for GENERIC. */
3317 || (c0->index
3318 && (TREE_CODE (c0->index) != INTEGER_CST
3319 || !compare_tree_int (c0->index, i)))
3320 || (c1->index
3321 && (TREE_CODE (c1->index) != INTEGER_CST
3322 || !compare_tree_int (c1->index, i))))
3323 return 0;
3325 return 1;
3327 else if (TREE_CODE (arg0) == STATEMENT_LIST
3328 && (flags & OEP_LEXICOGRAPHIC))
3330 /* Compare the STATEMENT_LISTs. */
3331 tree_stmt_iterator tsi1, tsi2;
3332 tree body1 = CONST_CAST_TREE (arg0);
3333 tree body2 = CONST_CAST_TREE (arg1);
3334 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3335 tsi_next (&tsi1), tsi_next (&tsi2))
3337 /* The lists don't have the same number of statements. */
3338 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3339 return 0;
3340 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3341 return 1;
3342 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3343 OEP_LEXICOGRAPHIC))
3344 return 0;
3347 return 0;
3349 case tcc_statement:
3350 switch (TREE_CODE (arg0))
3352 case RETURN_EXPR:
3353 if (flags & OEP_LEXICOGRAPHIC)
3354 return OP_SAME_WITH_NULL (0);
3355 return 0;
3356 default:
3357 return 0;
3360 default:
3361 return 0;
3364 #undef OP_SAME
3365 #undef OP_SAME_WITH_NULL
3368 /* Similar to operand_equal_p, but strip nops first. */
3370 static bool
3371 operand_equal_for_comparison_p (tree arg0, tree arg1)
3373 if (operand_equal_p (arg0, arg1, 0))
3374 return true;
3376 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3377 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3378 return false;
3380 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3381 and see if the inner values are the same. This removes any
3382 signedness comparison, which doesn't matter here. */
3383 STRIP_NOPS (arg0);
3384 STRIP_NOPS (arg1);
3385 if (operand_equal_p (arg0, arg1, 0))
3386 return true;
3388 return false;
3391 /* See if ARG is an expression that is either a comparison or is performing
3392 arithmetic on comparisons. The comparisons must only be comparing
3393 two different values, which will be stored in *CVAL1 and *CVAL2; if
3394 they are nonzero it means that some operands have already been found.
3395 No variables may be used anywhere else in the expression except in the
3396 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3397 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3399 If this is true, return 1. Otherwise, return zero. */
3401 static int
3402 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3404 enum tree_code code = TREE_CODE (arg);
3405 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3407 /* We can handle some of the tcc_expression cases here. */
3408 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3409 tclass = tcc_unary;
3410 else if (tclass == tcc_expression
3411 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3412 || code == COMPOUND_EXPR))
3413 tclass = tcc_binary;
3415 else if (tclass == tcc_expression && code == SAVE_EXPR
3416 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3418 /* If we've already found a CVAL1 or CVAL2, this expression is
3419 two complex to handle. */
3420 if (*cval1 || *cval2)
3421 return 0;
3423 tclass = tcc_unary;
3424 *save_p = 1;
3427 switch (tclass)
3429 case tcc_unary:
3430 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3432 case tcc_binary:
3433 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3434 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3435 cval1, cval2, save_p));
3437 case tcc_constant:
3438 return 1;
3440 case tcc_expression:
3441 if (code == COND_EXPR)
3442 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3443 cval1, cval2, save_p)
3444 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3445 cval1, cval2, save_p)
3446 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3447 cval1, cval2, save_p));
3448 return 0;
3450 case tcc_comparison:
3451 /* First see if we can handle the first operand, then the second. For
3452 the second operand, we know *CVAL1 can't be zero. It must be that
3453 one side of the comparison is each of the values; test for the
3454 case where this isn't true by failing if the two operands
3455 are the same. */
3457 if (operand_equal_p (TREE_OPERAND (arg, 0),
3458 TREE_OPERAND (arg, 1), 0))
3459 return 0;
3461 if (*cval1 == 0)
3462 *cval1 = TREE_OPERAND (arg, 0);
3463 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3465 else if (*cval2 == 0)
3466 *cval2 = TREE_OPERAND (arg, 0);
3467 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3469 else
3470 return 0;
3472 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3474 else if (*cval2 == 0)
3475 *cval2 = TREE_OPERAND (arg, 1);
3476 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3478 else
3479 return 0;
3481 return 1;
3483 default:
3484 return 0;
3488 /* ARG is a tree that is known to contain just arithmetic operations and
3489 comparisons. Evaluate the operations in the tree substituting NEW0 for
3490 any occurrence of OLD0 as an operand of a comparison and likewise for
3491 NEW1 and OLD1. */
3493 static tree
3494 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3495 tree old1, tree new1)
3497 tree type = TREE_TYPE (arg);
3498 enum tree_code code = TREE_CODE (arg);
3499 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3501 /* We can handle some of the tcc_expression cases here. */
3502 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3503 tclass = tcc_unary;
3504 else if (tclass == tcc_expression
3505 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3506 tclass = tcc_binary;
3508 switch (tclass)
3510 case tcc_unary:
3511 return fold_build1_loc (loc, code, type,
3512 eval_subst (loc, TREE_OPERAND (arg, 0),
3513 old0, new0, old1, new1));
3515 case tcc_binary:
3516 return fold_build2_loc (loc, code, type,
3517 eval_subst (loc, TREE_OPERAND (arg, 0),
3518 old0, new0, old1, new1),
3519 eval_subst (loc, TREE_OPERAND (arg, 1),
3520 old0, new0, old1, new1));
3522 case tcc_expression:
3523 switch (code)
3525 case SAVE_EXPR:
3526 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3527 old1, new1);
3529 case COMPOUND_EXPR:
3530 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3531 old1, new1);
3533 case COND_EXPR:
3534 return fold_build3_loc (loc, code, type,
3535 eval_subst (loc, TREE_OPERAND (arg, 0),
3536 old0, new0, old1, new1),
3537 eval_subst (loc, TREE_OPERAND (arg, 1),
3538 old0, new0, old1, new1),
3539 eval_subst (loc, TREE_OPERAND (arg, 2),
3540 old0, new0, old1, new1));
3541 default:
3542 break;
3544 /* Fall through - ??? */
3546 case tcc_comparison:
3548 tree arg0 = TREE_OPERAND (arg, 0);
3549 tree arg1 = TREE_OPERAND (arg, 1);
3551 /* We need to check both for exact equality and tree equality. The
3552 former will be true if the operand has a side-effect. In that
3553 case, we know the operand occurred exactly once. */
3555 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3556 arg0 = new0;
3557 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3558 arg0 = new1;
3560 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3561 arg1 = new0;
3562 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3563 arg1 = new1;
3565 return fold_build2_loc (loc, code, type, arg0, arg1);
3568 default:
3569 return arg;
3573 /* Return a tree for the case when the result of an expression is RESULT
3574 converted to TYPE and OMITTED was previously an operand of the expression
3575 but is now not needed (e.g., we folded OMITTED * 0).
3577 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3578 the conversion of RESULT to TYPE. */
3580 tree
3581 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3583 tree t = fold_convert_loc (loc, type, result);
3585 /* If the resulting operand is an empty statement, just return the omitted
3586 statement casted to void. */
3587 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3588 return build1_loc (loc, NOP_EXPR, void_type_node,
3589 fold_ignored_result (omitted));
3591 if (TREE_SIDE_EFFECTS (omitted))
3592 return build2_loc (loc, COMPOUND_EXPR, type,
3593 fold_ignored_result (omitted), t);
3595 return non_lvalue_loc (loc, t);
3598 /* Return a tree for the case when the result of an expression is RESULT
3599 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3600 of the expression but are now not needed.
3602 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3603 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3604 evaluated before OMITTED2. Otherwise, if neither has side effects,
3605 just do the conversion of RESULT to TYPE. */
3607 tree
3608 omit_two_operands_loc (location_t loc, tree type, tree result,
3609 tree omitted1, tree omitted2)
3611 tree t = fold_convert_loc (loc, type, result);
3613 if (TREE_SIDE_EFFECTS (omitted2))
3614 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3615 if (TREE_SIDE_EFFECTS (omitted1))
3616 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3618 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3622 /* Return a simplified tree node for the truth-negation of ARG. This
3623 never alters ARG itself. We assume that ARG is an operation that
3624 returns a truth value (0 or 1).
3626 FIXME: one would think we would fold the result, but it causes
3627 problems with the dominator optimizer. */
3629 static tree
3630 fold_truth_not_expr (location_t loc, tree arg)
3632 tree type = TREE_TYPE (arg);
3633 enum tree_code code = TREE_CODE (arg);
3634 location_t loc1, loc2;
3636 /* If this is a comparison, we can simply invert it, except for
3637 floating-point non-equality comparisons, in which case we just
3638 enclose a TRUTH_NOT_EXPR around what we have. */
3640 if (TREE_CODE_CLASS (code) == tcc_comparison)
3642 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3643 if (FLOAT_TYPE_P (op_type)
3644 && flag_trapping_math
3645 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3646 && code != NE_EXPR && code != EQ_EXPR)
3647 return NULL_TREE;
3649 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3650 if (code == ERROR_MARK)
3651 return NULL_TREE;
3653 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3654 TREE_OPERAND (arg, 1));
3655 if (TREE_NO_WARNING (arg))
3656 TREE_NO_WARNING (ret) = 1;
3657 return ret;
3660 switch (code)
3662 case INTEGER_CST:
3663 return constant_boolean_node (integer_zerop (arg), type);
3665 case TRUTH_AND_EXPR:
3666 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3667 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3668 return build2_loc (loc, TRUTH_OR_EXPR, type,
3669 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3670 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3672 case TRUTH_OR_EXPR:
3673 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3674 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3675 return build2_loc (loc, TRUTH_AND_EXPR, type,
3676 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3677 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3679 case TRUTH_XOR_EXPR:
3680 /* Here we can invert either operand. We invert the first operand
3681 unless the second operand is a TRUTH_NOT_EXPR in which case our
3682 result is the XOR of the first operand with the inside of the
3683 negation of the second operand. */
3685 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3686 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3687 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3688 else
3689 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3690 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3691 TREE_OPERAND (arg, 1));
3693 case TRUTH_ANDIF_EXPR:
3694 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3695 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3696 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3697 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3698 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3700 case TRUTH_ORIF_EXPR:
3701 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3702 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3703 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3704 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3705 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3707 case TRUTH_NOT_EXPR:
3708 return TREE_OPERAND (arg, 0);
3710 case COND_EXPR:
3712 tree arg1 = TREE_OPERAND (arg, 1);
3713 tree arg2 = TREE_OPERAND (arg, 2);
3715 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3716 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3718 /* A COND_EXPR may have a throw as one operand, which
3719 then has void type. Just leave void operands
3720 as they are. */
3721 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3722 VOID_TYPE_P (TREE_TYPE (arg1))
3723 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3724 VOID_TYPE_P (TREE_TYPE (arg2))
3725 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3728 case COMPOUND_EXPR:
3729 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3730 return build2_loc (loc, COMPOUND_EXPR, type,
3731 TREE_OPERAND (arg, 0),
3732 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3734 case NON_LVALUE_EXPR:
3735 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3736 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3738 CASE_CONVERT:
3739 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3740 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3742 /* fall through */
3744 case FLOAT_EXPR:
3745 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3746 return build1_loc (loc, TREE_CODE (arg), type,
3747 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3749 case BIT_AND_EXPR:
3750 if (!integer_onep (TREE_OPERAND (arg, 1)))
3751 return NULL_TREE;
3752 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3754 case SAVE_EXPR:
3755 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3757 case CLEANUP_POINT_EXPR:
3758 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3759 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3760 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3762 default:
3763 return NULL_TREE;
3767 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3768 assume that ARG is an operation that returns a truth value (0 or 1
3769 for scalars, 0 or -1 for vectors). Return the folded expression if
3770 folding is successful. Otherwise, return NULL_TREE. */
3772 static tree
3773 fold_invert_truthvalue (location_t loc, tree arg)
3775 tree type = TREE_TYPE (arg);
3776 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3777 ? BIT_NOT_EXPR
3778 : TRUTH_NOT_EXPR,
3779 type, arg);
3782 /* Return a simplified tree node for the truth-negation of ARG. This
3783 never alters ARG itself. We assume that ARG is an operation that
3784 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3786 tree
3787 invert_truthvalue_loc (location_t loc, tree arg)
3789 if (TREE_CODE (arg) == ERROR_MARK)
3790 return arg;
3792 tree type = TREE_TYPE (arg);
3793 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3794 ? BIT_NOT_EXPR
3795 : TRUTH_NOT_EXPR,
3796 type, arg);
3799 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3800 with code CODE. This optimization is unsafe. */
3801 static tree
3802 distribute_real_division (location_t loc, enum tree_code code, tree type,
3803 tree arg0, tree arg1)
3805 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3806 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3808 /* (A / C) +- (B / C) -> (A +- B) / C. */
3809 if (mul0 == mul1
3810 && operand_equal_p (TREE_OPERAND (arg0, 1),
3811 TREE_OPERAND (arg1, 1), 0))
3812 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3813 fold_build2_loc (loc, code, type,
3814 TREE_OPERAND (arg0, 0),
3815 TREE_OPERAND (arg1, 0)),
3816 TREE_OPERAND (arg0, 1));
3818 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3819 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3820 TREE_OPERAND (arg1, 0), 0)
3821 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3822 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3824 REAL_VALUE_TYPE r0, r1;
3825 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3826 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3827 if (!mul0)
3828 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3829 if (!mul1)
3830 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3831 real_arithmetic (&r0, code, &r0, &r1);
3832 return fold_build2_loc (loc, MULT_EXPR, type,
3833 TREE_OPERAND (arg0, 0),
3834 build_real (type, r0));
3837 return NULL_TREE;
3840 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3841 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3842 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3843 is the original memory reference used to preserve the alias set of
3844 the access. */
3846 static tree
3847 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3848 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3849 int unsignedp, int reversep)
3851 tree result, bftype;
3853 /* Attempt not to lose the access path if possible. */
3854 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3856 tree ninner = TREE_OPERAND (orig_inner, 0);
3857 machine_mode nmode;
3858 HOST_WIDE_INT nbitsize, nbitpos;
3859 tree noffset;
3860 int nunsignedp, nreversep, nvolatilep = 0;
3861 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3862 &noffset, &nmode, &nunsignedp,
3863 &nreversep, &nvolatilep);
3864 if (base == inner
3865 && noffset == NULL_TREE
3866 && nbitsize >= bitsize
3867 && nbitpos <= bitpos
3868 && bitpos + bitsize <= nbitpos + nbitsize
3869 && !reversep
3870 && !nreversep
3871 && !nvolatilep)
3873 inner = ninner;
3874 bitpos -= nbitpos;
3878 alias_set_type iset = get_alias_set (orig_inner);
3879 if (iset == 0 && get_alias_set (inner) != iset)
3880 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3881 build_fold_addr_expr (inner),
3882 build_int_cst (ptr_type_node, 0));
3884 if (bitpos == 0 && !reversep)
3886 tree size = TYPE_SIZE (TREE_TYPE (inner));
3887 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3888 || POINTER_TYPE_P (TREE_TYPE (inner)))
3889 && tree_fits_shwi_p (size)
3890 && tree_to_shwi (size) == bitsize)
3891 return fold_convert_loc (loc, type, inner);
3894 bftype = type;
3895 if (TYPE_PRECISION (bftype) != bitsize
3896 || TYPE_UNSIGNED (bftype) == !unsignedp)
3897 bftype = build_nonstandard_integer_type (bitsize, 0);
3899 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3900 bitsize_int (bitsize), bitsize_int (bitpos));
3901 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3903 if (bftype != type)
3904 result = fold_convert_loc (loc, type, result);
3906 return result;
3909 /* Optimize a bit-field compare.
3911 There are two cases: First is a compare against a constant and the
3912 second is a comparison of two items where the fields are at the same
3913 bit position relative to the start of a chunk (byte, halfword, word)
3914 large enough to contain it. In these cases we can avoid the shift
3915 implicit in bitfield extractions.
3917 For constants, we emit a compare of the shifted constant with the
3918 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3919 compared. For two fields at the same position, we do the ANDs with the
3920 similar mask and compare the result of the ANDs.
3922 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3923 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3924 are the left and right operands of the comparison, respectively.
3926 If the optimization described above can be done, we return the resulting
3927 tree. Otherwise we return zero. */
3929 static tree
3930 optimize_bit_field_compare (location_t loc, enum tree_code code,
3931 tree compare_type, tree lhs, tree rhs)
3933 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3934 tree type = TREE_TYPE (lhs);
3935 tree unsigned_type;
3936 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3937 machine_mode lmode, rmode;
3938 scalar_int_mode nmode;
3939 int lunsignedp, runsignedp;
3940 int lreversep, rreversep;
3941 int lvolatilep = 0, rvolatilep = 0;
3942 tree linner, rinner = NULL_TREE;
3943 tree mask;
3944 tree offset;
3946 /* Get all the information about the extractions being done. If the bit size
3947 if the same as the size of the underlying object, we aren't doing an
3948 extraction at all and so can do nothing. We also don't want to
3949 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3950 then will no longer be able to replace it. */
3951 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3952 &lunsignedp, &lreversep, &lvolatilep);
3953 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3954 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3955 return 0;
3957 if (const_p)
3958 rreversep = lreversep;
3959 else
3961 /* If this is not a constant, we can only do something if bit positions,
3962 sizes, signedness and storage order are the same. */
3963 rinner
3964 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3965 &runsignedp, &rreversep, &rvolatilep);
3967 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3968 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3969 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3970 return 0;
3973 /* Honor the C++ memory model and mimic what RTL expansion does. */
3974 unsigned HOST_WIDE_INT bitstart = 0;
3975 unsigned HOST_WIDE_INT bitend = 0;
3976 if (TREE_CODE (lhs) == COMPONENT_REF)
3978 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3979 if (offset != NULL_TREE)
3980 return 0;
3983 /* See if we can find a mode to refer to this field. We should be able to,
3984 but fail if we can't. */
3985 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3986 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3987 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3988 TYPE_ALIGN (TREE_TYPE (rinner))),
3989 BITS_PER_WORD, false, &nmode))
3990 return 0;
3992 /* Set signed and unsigned types of the precision of this mode for the
3993 shifts below. */
3994 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3996 /* Compute the bit position and size for the new reference and our offset
3997 within it. If the new reference is the same size as the original, we
3998 won't optimize anything, so return zero. */
3999 nbitsize = GET_MODE_BITSIZE (nmode);
4000 nbitpos = lbitpos & ~ (nbitsize - 1);
4001 lbitpos -= nbitpos;
4002 if (nbitsize == lbitsize)
4003 return 0;
4005 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4006 lbitpos = nbitsize - lbitsize - lbitpos;
4008 /* Make the mask to be used against the extracted field. */
4009 mask = build_int_cst_type (unsigned_type, -1);
4010 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4011 mask = const_binop (RSHIFT_EXPR, mask,
4012 size_int (nbitsize - lbitsize - lbitpos));
4014 if (! const_p)
4015 /* If not comparing with constant, just rework the comparison
4016 and return. */
4017 return fold_build2_loc (loc, code, compare_type,
4018 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4019 make_bit_field_ref (loc, linner, lhs,
4020 unsigned_type,
4021 nbitsize, nbitpos,
4022 1, lreversep),
4023 mask),
4024 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4025 make_bit_field_ref (loc, rinner, rhs,
4026 unsigned_type,
4027 nbitsize, nbitpos,
4028 1, rreversep),
4029 mask));
4031 /* Otherwise, we are handling the constant case. See if the constant is too
4032 big for the field. Warn and return a tree for 0 (false) if so. We do
4033 this not only for its own sake, but to avoid having to test for this
4034 error case below. If we didn't, we might generate wrong code.
4036 For unsigned fields, the constant shifted right by the field length should
4037 be all zero. For signed fields, the high-order bits should agree with
4038 the sign bit. */
4040 if (lunsignedp)
4042 if (wi::lrshift (rhs, lbitsize) != 0)
4044 warning (0, "comparison is always %d due to width of bit-field",
4045 code == NE_EXPR);
4046 return constant_boolean_node (code == NE_EXPR, compare_type);
4049 else
4051 wide_int tem = wi::arshift (rhs, lbitsize - 1);
4052 if (tem != 0 && tem != -1)
4054 warning (0, "comparison is always %d due to width of bit-field",
4055 code == NE_EXPR);
4056 return constant_boolean_node (code == NE_EXPR, compare_type);
4060 /* Single-bit compares should always be against zero. */
4061 if (lbitsize == 1 && ! integer_zerop (rhs))
4063 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4064 rhs = build_int_cst (type, 0);
4067 /* Make a new bitfield reference, shift the constant over the
4068 appropriate number of bits and mask it with the computed mask
4069 (in case this was a signed field). If we changed it, make a new one. */
4070 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4071 nbitsize, nbitpos, 1, lreversep);
4073 rhs = const_binop (BIT_AND_EXPR,
4074 const_binop (LSHIFT_EXPR,
4075 fold_convert_loc (loc, unsigned_type, rhs),
4076 size_int (lbitpos)),
4077 mask);
4079 lhs = build2_loc (loc, code, compare_type,
4080 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4081 return lhs;
4084 /* Subroutine for fold_truth_andor_1: decode a field reference.
4086 If EXP is a comparison reference, we return the innermost reference.
4088 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4089 set to the starting bit number.
4091 If the innermost field can be completely contained in a mode-sized
4092 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4094 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4095 otherwise it is not changed.
4097 *PUNSIGNEDP is set to the signedness of the field.
4099 *PREVERSEP is set to the storage order of the field.
4101 *PMASK is set to the mask used. This is either contained in a
4102 BIT_AND_EXPR or derived from the width of the field.
4104 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4106 Return 0 if this is not a component reference or is one that we can't
4107 do anything with. */
4109 static tree
4110 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4111 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4112 int *punsignedp, int *preversep, int *pvolatilep,
4113 tree *pmask, tree *pand_mask)
4115 tree exp = *exp_;
4116 tree outer_type = 0;
4117 tree and_mask = 0;
4118 tree mask, inner, offset;
4119 tree unsigned_type;
4120 unsigned int precision;
4122 /* All the optimizations using this function assume integer fields.
4123 There are problems with FP fields since the type_for_size call
4124 below can fail for, e.g., XFmode. */
4125 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4126 return 0;
4128 /* We are interested in the bare arrangement of bits, so strip everything
4129 that doesn't affect the machine mode. However, record the type of the
4130 outermost expression if it may matter below. */
4131 if (CONVERT_EXPR_P (exp)
4132 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4133 outer_type = TREE_TYPE (exp);
4134 STRIP_NOPS (exp);
4136 if (TREE_CODE (exp) == BIT_AND_EXPR)
4138 and_mask = TREE_OPERAND (exp, 1);
4139 exp = TREE_OPERAND (exp, 0);
4140 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4141 if (TREE_CODE (and_mask) != INTEGER_CST)
4142 return 0;
4145 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4146 punsignedp, preversep, pvolatilep);
4147 if ((inner == exp && and_mask == 0)
4148 || *pbitsize < 0 || offset != 0
4149 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4150 /* Reject out-of-bound accesses (PR79731). */
4151 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4152 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4153 *pbitpos + *pbitsize) < 0))
4154 return 0;
4156 *exp_ = exp;
4158 /* If the number of bits in the reference is the same as the bitsize of
4159 the outer type, then the outer type gives the signedness. Otherwise
4160 (in case of a small bitfield) the signedness is unchanged. */
4161 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4162 *punsignedp = TYPE_UNSIGNED (outer_type);
4164 /* Compute the mask to access the bitfield. */
4165 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4166 precision = TYPE_PRECISION (unsigned_type);
4168 mask = build_int_cst_type (unsigned_type, -1);
4170 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4171 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4173 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4174 if (and_mask != 0)
4175 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4176 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4178 *pmask = mask;
4179 *pand_mask = and_mask;
4180 return inner;
4183 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4184 bit positions and MASK is SIGNED. */
4186 static int
4187 all_ones_mask_p (const_tree mask, unsigned int size)
4189 tree type = TREE_TYPE (mask);
4190 unsigned int precision = TYPE_PRECISION (type);
4192 /* If this function returns true when the type of the mask is
4193 UNSIGNED, then there will be errors. In particular see
4194 gcc.c-torture/execute/990326-1.c. There does not appear to be
4195 any documentation paper trail as to why this is so. But the pre
4196 wide-int worked with that restriction and it has been preserved
4197 here. */
4198 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4199 return false;
4201 return wi::mask (size, false, precision) == mask;
4204 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4205 represents the sign bit of EXP's type. If EXP represents a sign
4206 or zero extension, also test VAL against the unextended type.
4207 The return value is the (sub)expression whose sign bit is VAL,
4208 or NULL_TREE otherwise. */
4210 tree
4211 sign_bit_p (tree exp, const_tree val)
4213 int width;
4214 tree t;
4216 /* Tree EXP must have an integral type. */
4217 t = TREE_TYPE (exp);
4218 if (! INTEGRAL_TYPE_P (t))
4219 return NULL_TREE;
4221 /* Tree VAL must be an integer constant. */
4222 if (TREE_CODE (val) != INTEGER_CST
4223 || TREE_OVERFLOW (val))
4224 return NULL_TREE;
4226 width = TYPE_PRECISION (t);
4227 if (wi::only_sign_bit_p (val, width))
4228 return exp;
4230 /* Handle extension from a narrower type. */
4231 if (TREE_CODE (exp) == NOP_EXPR
4232 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4233 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4235 return NULL_TREE;
4238 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4239 to be evaluated unconditionally. */
4241 static int
4242 simple_operand_p (const_tree exp)
4244 /* Strip any conversions that don't change the machine mode. */
4245 STRIP_NOPS (exp);
4247 return (CONSTANT_CLASS_P (exp)
4248 || TREE_CODE (exp) == SSA_NAME
4249 || (DECL_P (exp)
4250 && ! TREE_ADDRESSABLE (exp)
4251 && ! TREE_THIS_VOLATILE (exp)
4252 && ! DECL_NONLOCAL (exp)
4253 /* Don't regard global variables as simple. They may be
4254 allocated in ways unknown to the compiler (shared memory,
4255 #pragma weak, etc). */
4256 && ! TREE_PUBLIC (exp)
4257 && ! DECL_EXTERNAL (exp)
4258 /* Weakrefs are not safe to be read, since they can be NULL.
4259 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4260 have DECL_WEAK flag set. */
4261 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4262 /* Loading a static variable is unduly expensive, but global
4263 registers aren't expensive. */
4264 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4267 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4268 to be evaluated unconditionally.
4269 I addition to simple_operand_p, we assume that comparisons, conversions,
4270 and logic-not operations are simple, if their operands are simple, too. */
4272 static bool
4273 simple_operand_p_2 (tree exp)
4275 enum tree_code code;
4277 if (TREE_SIDE_EFFECTS (exp)
4278 || tree_could_trap_p (exp))
4279 return false;
4281 while (CONVERT_EXPR_P (exp))
4282 exp = TREE_OPERAND (exp, 0);
4284 code = TREE_CODE (exp);
4286 if (TREE_CODE_CLASS (code) == tcc_comparison)
4287 return (simple_operand_p (TREE_OPERAND (exp, 0))
4288 && simple_operand_p (TREE_OPERAND (exp, 1)));
4290 if (code == TRUTH_NOT_EXPR)
4291 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4293 return simple_operand_p (exp);
4297 /* The following functions are subroutines to fold_range_test and allow it to
4298 try to change a logical combination of comparisons into a range test.
4300 For example, both
4301 X == 2 || X == 3 || X == 4 || X == 5
4303 X >= 2 && X <= 5
4304 are converted to
4305 (unsigned) (X - 2) <= 3
4307 We describe each set of comparisons as being either inside or outside
4308 a range, using a variable named like IN_P, and then describe the
4309 range with a lower and upper bound. If one of the bounds is omitted,
4310 it represents either the highest or lowest value of the type.
4312 In the comments below, we represent a range by two numbers in brackets
4313 preceded by a "+" to designate being inside that range, or a "-" to
4314 designate being outside that range, so the condition can be inverted by
4315 flipping the prefix. An omitted bound is represented by a "-". For
4316 example, "- [-, 10]" means being outside the range starting at the lowest
4317 possible value and ending at 10, in other words, being greater than 10.
4318 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4319 always false.
4321 We set up things so that the missing bounds are handled in a consistent
4322 manner so neither a missing bound nor "true" and "false" need to be
4323 handled using a special case. */
4325 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4326 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4327 and UPPER1_P are nonzero if the respective argument is an upper bound
4328 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4329 must be specified for a comparison. ARG1 will be converted to ARG0's
4330 type if both are specified. */
4332 static tree
4333 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4334 tree arg1, int upper1_p)
4336 tree tem;
4337 int result;
4338 int sgn0, sgn1;
4340 /* If neither arg represents infinity, do the normal operation.
4341 Else, if not a comparison, return infinity. Else handle the special
4342 comparison rules. Note that most of the cases below won't occur, but
4343 are handled for consistency. */
4345 if (arg0 != 0 && arg1 != 0)
4347 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4348 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4349 STRIP_NOPS (tem);
4350 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4353 if (TREE_CODE_CLASS (code) != tcc_comparison)
4354 return 0;
4356 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4357 for neither. In real maths, we cannot assume open ended ranges are
4358 the same. But, this is computer arithmetic, where numbers are finite.
4359 We can therefore make the transformation of any unbounded range with
4360 the value Z, Z being greater than any representable number. This permits
4361 us to treat unbounded ranges as equal. */
4362 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4363 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4364 switch (code)
4366 case EQ_EXPR:
4367 result = sgn0 == sgn1;
4368 break;
4369 case NE_EXPR:
4370 result = sgn0 != sgn1;
4371 break;
4372 case LT_EXPR:
4373 result = sgn0 < sgn1;
4374 break;
4375 case LE_EXPR:
4376 result = sgn0 <= sgn1;
4377 break;
4378 case GT_EXPR:
4379 result = sgn0 > sgn1;
4380 break;
4381 case GE_EXPR:
4382 result = sgn0 >= sgn1;
4383 break;
4384 default:
4385 gcc_unreachable ();
4388 return constant_boolean_node (result, type);
4391 /* Helper routine for make_range. Perform one step for it, return
4392 new expression if the loop should continue or NULL_TREE if it should
4393 stop. */
4395 tree
4396 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4397 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4398 bool *strict_overflow_p)
4400 tree arg0_type = TREE_TYPE (arg0);
4401 tree n_low, n_high, low = *p_low, high = *p_high;
4402 int in_p = *p_in_p, n_in_p;
4404 switch (code)
4406 case TRUTH_NOT_EXPR:
4407 /* We can only do something if the range is testing for zero. */
4408 if (low == NULL_TREE || high == NULL_TREE
4409 || ! integer_zerop (low) || ! integer_zerop (high))
4410 return NULL_TREE;
4411 *p_in_p = ! in_p;
4412 return arg0;
4414 case EQ_EXPR: case NE_EXPR:
4415 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4416 /* We can only do something if the range is testing for zero
4417 and if the second operand is an integer constant. Note that
4418 saying something is "in" the range we make is done by
4419 complementing IN_P since it will set in the initial case of
4420 being not equal to zero; "out" is leaving it alone. */
4421 if (low == NULL_TREE || high == NULL_TREE
4422 || ! integer_zerop (low) || ! integer_zerop (high)
4423 || TREE_CODE (arg1) != INTEGER_CST)
4424 return NULL_TREE;
4426 switch (code)
4428 case NE_EXPR: /* - [c, c] */
4429 low = high = arg1;
4430 break;
4431 case EQ_EXPR: /* + [c, c] */
4432 in_p = ! in_p, low = high = arg1;
4433 break;
4434 case GT_EXPR: /* - [-, c] */
4435 low = 0, high = arg1;
4436 break;
4437 case GE_EXPR: /* + [c, -] */
4438 in_p = ! in_p, low = arg1, high = 0;
4439 break;
4440 case LT_EXPR: /* - [c, -] */
4441 low = arg1, high = 0;
4442 break;
4443 case LE_EXPR: /* + [-, c] */
4444 in_p = ! in_p, low = 0, high = arg1;
4445 break;
4446 default:
4447 gcc_unreachable ();
4450 /* If this is an unsigned comparison, we also know that EXP is
4451 greater than or equal to zero. We base the range tests we make
4452 on that fact, so we record it here so we can parse existing
4453 range tests. We test arg0_type since often the return type
4454 of, e.g. EQ_EXPR, is boolean. */
4455 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4457 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4458 in_p, low, high, 1,
4459 build_int_cst (arg0_type, 0),
4460 NULL_TREE))
4461 return NULL_TREE;
4463 in_p = n_in_p, low = n_low, high = n_high;
4465 /* If the high bound is missing, but we have a nonzero low
4466 bound, reverse the range so it goes from zero to the low bound
4467 minus 1. */
4468 if (high == 0 && low && ! integer_zerop (low))
4470 in_p = ! in_p;
4471 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4472 build_int_cst (TREE_TYPE (low), 1), 0);
4473 low = build_int_cst (arg0_type, 0);
4477 *p_low = low;
4478 *p_high = high;
4479 *p_in_p = in_p;
4480 return arg0;
4482 case NEGATE_EXPR:
4483 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4484 low and high are non-NULL, then normalize will DTRT. */
4485 if (!TYPE_UNSIGNED (arg0_type)
4486 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4488 if (low == NULL_TREE)
4489 low = TYPE_MIN_VALUE (arg0_type);
4490 if (high == NULL_TREE)
4491 high = TYPE_MAX_VALUE (arg0_type);
4494 /* (-x) IN [a,b] -> x in [-b, -a] */
4495 n_low = range_binop (MINUS_EXPR, exp_type,
4496 build_int_cst (exp_type, 0),
4497 0, high, 1);
4498 n_high = range_binop (MINUS_EXPR, exp_type,
4499 build_int_cst (exp_type, 0),
4500 0, low, 0);
4501 if (n_high != 0 && TREE_OVERFLOW (n_high))
4502 return NULL_TREE;
4503 goto normalize;
4505 case BIT_NOT_EXPR:
4506 /* ~ X -> -X - 1 */
4507 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4508 build_int_cst (exp_type, 1));
4510 case PLUS_EXPR:
4511 case MINUS_EXPR:
4512 if (TREE_CODE (arg1) != INTEGER_CST)
4513 return NULL_TREE;
4515 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4516 move a constant to the other side. */
4517 if (!TYPE_UNSIGNED (arg0_type)
4518 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4519 return NULL_TREE;
4521 /* If EXP is signed, any overflow in the computation is undefined,
4522 so we don't worry about it so long as our computations on
4523 the bounds don't overflow. For unsigned, overflow is defined
4524 and this is exactly the right thing. */
4525 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4526 arg0_type, low, 0, arg1, 0);
4527 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4528 arg0_type, high, 1, arg1, 0);
4529 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4530 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4531 return NULL_TREE;
4533 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4534 *strict_overflow_p = true;
4536 normalize:
4537 /* Check for an unsigned range which has wrapped around the maximum
4538 value thus making n_high < n_low, and normalize it. */
4539 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4541 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4542 build_int_cst (TREE_TYPE (n_high), 1), 0);
4543 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4544 build_int_cst (TREE_TYPE (n_low), 1), 0);
4546 /* If the range is of the form +/- [ x+1, x ], we won't
4547 be able to normalize it. But then, it represents the
4548 whole range or the empty set, so make it
4549 +/- [ -, - ]. */
4550 if (tree_int_cst_equal (n_low, low)
4551 && tree_int_cst_equal (n_high, high))
4552 low = high = 0;
4553 else
4554 in_p = ! in_p;
4556 else
4557 low = n_low, high = n_high;
4559 *p_low = low;
4560 *p_high = high;
4561 *p_in_p = in_p;
4562 return arg0;
4564 CASE_CONVERT:
4565 case NON_LVALUE_EXPR:
4566 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4567 return NULL_TREE;
4569 if (! INTEGRAL_TYPE_P (arg0_type)
4570 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4571 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4572 return NULL_TREE;
4574 n_low = low, n_high = high;
4576 if (n_low != 0)
4577 n_low = fold_convert_loc (loc, arg0_type, n_low);
4579 if (n_high != 0)
4580 n_high = fold_convert_loc (loc, arg0_type, n_high);
4582 /* If we're converting arg0 from an unsigned type, to exp,
4583 a signed type, we will be doing the comparison as unsigned.
4584 The tests above have already verified that LOW and HIGH
4585 are both positive.
4587 So we have to ensure that we will handle large unsigned
4588 values the same way that the current signed bounds treat
4589 negative values. */
4591 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4593 tree high_positive;
4594 tree equiv_type;
4595 /* For fixed-point modes, we need to pass the saturating flag
4596 as the 2nd parameter. */
4597 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4598 equiv_type
4599 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4600 TYPE_SATURATING (arg0_type));
4601 else
4602 equiv_type
4603 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4605 /* A range without an upper bound is, naturally, unbounded.
4606 Since convert would have cropped a very large value, use
4607 the max value for the destination type. */
4608 high_positive
4609 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4610 : TYPE_MAX_VALUE (arg0_type);
4612 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4613 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4614 fold_convert_loc (loc, arg0_type,
4615 high_positive),
4616 build_int_cst (arg0_type, 1));
4618 /* If the low bound is specified, "and" the range with the
4619 range for which the original unsigned value will be
4620 positive. */
4621 if (low != 0)
4623 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4624 1, fold_convert_loc (loc, arg0_type,
4625 integer_zero_node),
4626 high_positive))
4627 return NULL_TREE;
4629 in_p = (n_in_p == in_p);
4631 else
4633 /* Otherwise, "or" the range with the range of the input
4634 that will be interpreted as negative. */
4635 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4636 1, fold_convert_loc (loc, arg0_type,
4637 integer_zero_node),
4638 high_positive))
4639 return NULL_TREE;
4641 in_p = (in_p != n_in_p);
4645 *p_low = n_low;
4646 *p_high = n_high;
4647 *p_in_p = in_p;
4648 return arg0;
4650 default:
4651 return NULL_TREE;
4655 /* Given EXP, a logical expression, set the range it is testing into
4656 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4657 actually being tested. *PLOW and *PHIGH will be made of the same
4658 type as the returned expression. If EXP is not a comparison, we
4659 will most likely not be returning a useful value and range. Set
4660 *STRICT_OVERFLOW_P to true if the return value is only valid
4661 because signed overflow is undefined; otherwise, do not change
4662 *STRICT_OVERFLOW_P. */
4664 tree
4665 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4666 bool *strict_overflow_p)
4668 enum tree_code code;
4669 tree arg0, arg1 = NULL_TREE;
4670 tree exp_type, nexp;
4671 int in_p;
4672 tree low, high;
4673 location_t loc = EXPR_LOCATION (exp);
4675 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4676 and see if we can refine the range. Some of the cases below may not
4677 happen, but it doesn't seem worth worrying about this. We "continue"
4678 the outer loop when we've changed something; otherwise we "break"
4679 the switch, which will "break" the while. */
4681 in_p = 0;
4682 low = high = build_int_cst (TREE_TYPE (exp), 0);
4684 while (1)
4686 code = TREE_CODE (exp);
4687 exp_type = TREE_TYPE (exp);
4688 arg0 = NULL_TREE;
4690 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4692 if (TREE_OPERAND_LENGTH (exp) > 0)
4693 arg0 = TREE_OPERAND (exp, 0);
4694 if (TREE_CODE_CLASS (code) == tcc_binary
4695 || TREE_CODE_CLASS (code) == tcc_comparison
4696 || (TREE_CODE_CLASS (code) == tcc_expression
4697 && TREE_OPERAND_LENGTH (exp) > 1))
4698 arg1 = TREE_OPERAND (exp, 1);
4700 if (arg0 == NULL_TREE)
4701 break;
4703 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4704 &high, &in_p, strict_overflow_p);
4705 if (nexp == NULL_TREE)
4706 break;
4707 exp = nexp;
4710 /* If EXP is a constant, we can evaluate whether this is true or false. */
4711 if (TREE_CODE (exp) == INTEGER_CST)
4713 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4714 exp, 0, low, 0))
4715 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4716 exp, 1, high, 1)));
4717 low = high = 0;
4718 exp = 0;
4721 *pin_p = in_p, *plow = low, *phigh = high;
4722 return exp;
4725 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4726 a bitwise check i.e. when
4727 LOW == 0xXX...X00...0
4728 HIGH == 0xXX...X11...1
4729 Return corresponding mask in MASK and stem in VALUE. */
4731 static bool
4732 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4733 tree *value)
4735 if (TREE_CODE (low) != INTEGER_CST
4736 || TREE_CODE (high) != INTEGER_CST)
4737 return false;
4739 unsigned prec = TYPE_PRECISION (type);
4740 wide_int lo = wi::to_wide (low, prec);
4741 wide_int hi = wi::to_wide (high, prec);
4743 wide_int end_mask = lo ^ hi;
4744 if ((end_mask & (end_mask + 1)) != 0
4745 || (lo & end_mask) != 0)
4746 return false;
4748 wide_int stem_mask = ~end_mask;
4749 wide_int stem = lo & stem_mask;
4750 if (stem != (hi & stem_mask))
4751 return false;
4753 *mask = wide_int_to_tree (type, stem_mask);
4754 *value = wide_int_to_tree (type, stem);
4756 return true;
4759 /* Helper routine for build_range_check and match.pd. Return the type to
4760 perform the check or NULL if it shouldn't be optimized. */
4762 tree
4763 range_check_type (tree etype)
4765 /* First make sure that arithmetics in this type is valid, then make sure
4766 that it wraps around. */
4767 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4768 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4769 TYPE_UNSIGNED (etype));
4771 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4773 tree utype, minv, maxv;
4775 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4776 for the type in question, as we rely on this here. */
4777 utype = unsigned_type_for (etype);
4778 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4779 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4780 build_int_cst (TREE_TYPE (maxv), 1), 1);
4781 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4783 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4784 minv, 1, maxv, 1)))
4785 etype = utype;
4786 else
4787 return NULL_TREE;
4789 return etype;
4792 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4793 type, TYPE, return an expression to test if EXP is in (or out of, depending
4794 on IN_P) the range. Return 0 if the test couldn't be created. */
4796 tree
4797 build_range_check (location_t loc, tree type, tree exp, int in_p,
4798 tree low, tree high)
4800 tree etype = TREE_TYPE (exp), mask, value;
4802 /* Disable this optimization for function pointer expressions
4803 on targets that require function pointer canonicalization. */
4804 if (targetm.have_canonicalize_funcptr_for_compare ()
4805 && TREE_CODE (etype) == POINTER_TYPE
4806 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4807 return NULL_TREE;
4809 if (! in_p)
4811 value = build_range_check (loc, type, exp, 1, low, high);
4812 if (value != 0)
4813 return invert_truthvalue_loc (loc, value);
4815 return 0;
4818 if (low == 0 && high == 0)
4819 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4821 if (low == 0)
4822 return fold_build2_loc (loc, LE_EXPR, type, exp,
4823 fold_convert_loc (loc, etype, high));
4825 if (high == 0)
4826 return fold_build2_loc (loc, GE_EXPR, type, exp,
4827 fold_convert_loc (loc, etype, low));
4829 if (operand_equal_p (low, high, 0))
4830 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4831 fold_convert_loc (loc, etype, low));
4833 if (TREE_CODE (exp) == BIT_AND_EXPR
4834 && maskable_range_p (low, high, etype, &mask, &value))
4835 return fold_build2_loc (loc, EQ_EXPR, type,
4836 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4837 exp, mask),
4838 value);
4840 if (integer_zerop (low))
4842 if (! TYPE_UNSIGNED (etype))
4844 etype = unsigned_type_for (etype);
4845 high = fold_convert_loc (loc, etype, high);
4846 exp = fold_convert_loc (loc, etype, exp);
4848 return build_range_check (loc, type, exp, 1, 0, high);
4851 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4852 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4854 int prec = TYPE_PRECISION (etype);
4856 if (wi::mask (prec - 1, false, prec) == high)
4858 if (TYPE_UNSIGNED (etype))
4860 tree signed_etype = signed_type_for (etype);
4861 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4862 etype
4863 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4864 else
4865 etype = signed_etype;
4866 exp = fold_convert_loc (loc, etype, exp);
4868 return fold_build2_loc (loc, GT_EXPR, type, exp,
4869 build_int_cst (etype, 0));
4873 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4874 This requires wrap-around arithmetics for the type of the expression. */
4875 etype = range_check_type (etype);
4876 if (etype == NULL_TREE)
4877 return NULL_TREE;
4879 if (POINTER_TYPE_P (etype))
4880 etype = unsigned_type_for (etype);
4882 high = fold_convert_loc (loc, etype, high);
4883 low = fold_convert_loc (loc, etype, low);
4884 exp = fold_convert_loc (loc, etype, exp);
4886 value = const_binop (MINUS_EXPR, high, low);
4888 if (value != 0 && !TREE_OVERFLOW (value))
4889 return build_range_check (loc, type,
4890 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4891 1, build_int_cst (etype, 0), value);
4893 return 0;
4896 /* Return the predecessor of VAL in its type, handling the infinite case. */
4898 static tree
4899 range_predecessor (tree val)
4901 tree type = TREE_TYPE (val);
4903 if (INTEGRAL_TYPE_P (type)
4904 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4905 return 0;
4906 else
4907 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4908 build_int_cst (TREE_TYPE (val), 1), 0);
4911 /* Return the successor of VAL in its type, handling the infinite case. */
4913 static tree
4914 range_successor (tree val)
4916 tree type = TREE_TYPE (val);
4918 if (INTEGRAL_TYPE_P (type)
4919 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4920 return 0;
4921 else
4922 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4923 build_int_cst (TREE_TYPE (val), 1), 0);
4926 /* Given two ranges, see if we can merge them into one. Return 1 if we
4927 can, 0 if we can't. Set the output range into the specified parameters. */
4929 bool
4930 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4931 tree high0, int in1_p, tree low1, tree high1)
4933 int no_overlap;
4934 int subset;
4935 int temp;
4936 tree tem;
4937 int in_p;
4938 tree low, high;
4939 int lowequal = ((low0 == 0 && low1 == 0)
4940 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4941 low0, 0, low1, 0)));
4942 int highequal = ((high0 == 0 && high1 == 0)
4943 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4944 high0, 1, high1, 1)));
4946 /* Make range 0 be the range that starts first, or ends last if they
4947 start at the same value. Swap them if it isn't. */
4948 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4949 low0, 0, low1, 0))
4950 || (lowequal
4951 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4952 high1, 1, high0, 1))))
4954 temp = in0_p, in0_p = in1_p, in1_p = temp;
4955 tem = low0, low0 = low1, low1 = tem;
4956 tem = high0, high0 = high1, high1 = tem;
4959 /* Now flag two cases, whether the ranges are disjoint or whether the
4960 second range is totally subsumed in the first. Note that the tests
4961 below are simplified by the ones above. */
4962 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4963 high0, 1, low1, 0));
4964 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4965 high1, 1, high0, 1));
4967 /* We now have four cases, depending on whether we are including or
4968 excluding the two ranges. */
4969 if (in0_p && in1_p)
4971 /* If they don't overlap, the result is false. If the second range
4972 is a subset it is the result. Otherwise, the range is from the start
4973 of the second to the end of the first. */
4974 if (no_overlap)
4975 in_p = 0, low = high = 0;
4976 else if (subset)
4977 in_p = 1, low = low1, high = high1;
4978 else
4979 in_p = 1, low = low1, high = high0;
4982 else if (in0_p && ! in1_p)
4984 /* If they don't overlap, the result is the first range. If they are
4985 equal, the result is false. If the second range is a subset of the
4986 first, and the ranges begin at the same place, we go from just after
4987 the end of the second range to the end of the first. If the second
4988 range is not a subset of the first, or if it is a subset and both
4989 ranges end at the same place, the range starts at the start of the
4990 first range and ends just before the second range.
4991 Otherwise, we can't describe this as a single range. */
4992 if (no_overlap)
4993 in_p = 1, low = low0, high = high0;
4994 else if (lowequal && highequal)
4995 in_p = 0, low = high = 0;
4996 else if (subset && lowequal)
4998 low = range_successor (high1);
4999 high = high0;
5000 in_p = 1;
5001 if (low == 0)
5003 /* We are in the weird situation where high0 > high1 but
5004 high1 has no successor. Punt. */
5005 return 0;
5008 else if (! subset || highequal)
5010 low = low0;
5011 high = range_predecessor (low1);
5012 in_p = 1;
5013 if (high == 0)
5015 /* low0 < low1 but low1 has no predecessor. Punt. */
5016 return 0;
5019 else
5020 return 0;
5023 else if (! in0_p && in1_p)
5025 /* If they don't overlap, the result is the second range. If the second
5026 is a subset of the first, the result is false. Otherwise,
5027 the range starts just after the first range and ends at the
5028 end of the second. */
5029 if (no_overlap)
5030 in_p = 1, low = low1, high = high1;
5031 else if (subset || highequal)
5032 in_p = 0, low = high = 0;
5033 else
5035 low = range_successor (high0);
5036 high = high1;
5037 in_p = 1;
5038 if (low == 0)
5040 /* high1 > high0 but high0 has no successor. Punt. */
5041 return 0;
5046 else
5048 /* The case where we are excluding both ranges. Here the complex case
5049 is if they don't overlap. In that case, the only time we have a
5050 range is if they are adjacent. If the second is a subset of the
5051 first, the result is the first. Otherwise, the range to exclude
5052 starts at the beginning of the first range and ends at the end of the
5053 second. */
5054 if (no_overlap)
5056 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5057 range_successor (high0),
5058 1, low1, 0)))
5059 in_p = 0, low = low0, high = high1;
5060 else
5062 /* Canonicalize - [min, x] into - [-, x]. */
5063 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5064 switch (TREE_CODE (TREE_TYPE (low0)))
5066 case ENUMERAL_TYPE:
5067 if (TYPE_PRECISION (TREE_TYPE (low0))
5068 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5069 break;
5070 /* FALLTHROUGH */
5071 case INTEGER_TYPE:
5072 if (tree_int_cst_equal (low0,
5073 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5074 low0 = 0;
5075 break;
5076 case POINTER_TYPE:
5077 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5078 && integer_zerop (low0))
5079 low0 = 0;
5080 break;
5081 default:
5082 break;
5085 /* Canonicalize - [x, max] into - [x, -]. */
5086 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5087 switch (TREE_CODE (TREE_TYPE (high1)))
5089 case ENUMERAL_TYPE:
5090 if (TYPE_PRECISION (TREE_TYPE (high1))
5091 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5092 break;
5093 /* FALLTHROUGH */
5094 case INTEGER_TYPE:
5095 if (tree_int_cst_equal (high1,
5096 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5097 high1 = 0;
5098 break;
5099 case POINTER_TYPE:
5100 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5101 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5102 high1, 1,
5103 build_int_cst (TREE_TYPE (high1), 1),
5104 1)))
5105 high1 = 0;
5106 break;
5107 default:
5108 break;
5111 /* The ranges might be also adjacent between the maximum and
5112 minimum values of the given type. For
5113 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5114 return + [x + 1, y - 1]. */
5115 if (low0 == 0 && high1 == 0)
5117 low = range_successor (high0);
5118 high = range_predecessor (low1);
5119 if (low == 0 || high == 0)
5120 return 0;
5122 in_p = 1;
5124 else
5125 return 0;
5128 else if (subset)
5129 in_p = 0, low = low0, high = high0;
5130 else
5131 in_p = 0, low = low0, high = high1;
5134 *pin_p = in_p, *plow = low, *phigh = high;
5135 return 1;
5139 /* Subroutine of fold, looking inside expressions of the form
5140 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5141 of the COND_EXPR. This function is being used also to optimize
5142 A op B ? C : A, by reversing the comparison first.
5144 Return a folded expression whose code is not a COND_EXPR
5145 anymore, or NULL_TREE if no folding opportunity is found. */
5147 static tree
5148 fold_cond_expr_with_comparison (location_t loc, tree type,
5149 tree arg0, tree arg1, tree arg2)
5151 enum tree_code comp_code = TREE_CODE (arg0);
5152 tree arg00 = TREE_OPERAND (arg0, 0);
5153 tree arg01 = TREE_OPERAND (arg0, 1);
5154 tree arg1_type = TREE_TYPE (arg1);
5155 tree tem;
5157 STRIP_NOPS (arg1);
5158 STRIP_NOPS (arg2);
5160 /* If we have A op 0 ? A : -A, consider applying the following
5161 transformations:
5163 A == 0? A : -A same as -A
5164 A != 0? A : -A same as A
5165 A >= 0? A : -A same as abs (A)
5166 A > 0? A : -A same as abs (A)
5167 A <= 0? A : -A same as -abs (A)
5168 A < 0? A : -A same as -abs (A)
5170 None of these transformations work for modes with signed
5171 zeros. If A is +/-0, the first two transformations will
5172 change the sign of the result (from +0 to -0, or vice
5173 versa). The last four will fix the sign of the result,
5174 even though the original expressions could be positive or
5175 negative, depending on the sign of A.
5177 Note that all these transformations are correct if A is
5178 NaN, since the two alternatives (A and -A) are also NaNs. */
5179 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5180 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5181 ? real_zerop (arg01)
5182 : integer_zerop (arg01))
5183 && ((TREE_CODE (arg2) == NEGATE_EXPR
5184 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5185 /* In the case that A is of the form X-Y, '-A' (arg2) may
5186 have already been folded to Y-X, check for that. */
5187 || (TREE_CODE (arg1) == MINUS_EXPR
5188 && TREE_CODE (arg2) == MINUS_EXPR
5189 && operand_equal_p (TREE_OPERAND (arg1, 0),
5190 TREE_OPERAND (arg2, 1), 0)
5191 && operand_equal_p (TREE_OPERAND (arg1, 1),
5192 TREE_OPERAND (arg2, 0), 0))))
5193 switch (comp_code)
5195 case EQ_EXPR:
5196 case UNEQ_EXPR:
5197 tem = fold_convert_loc (loc, arg1_type, arg1);
5198 return fold_convert_loc (loc, type, negate_expr (tem));
5199 case NE_EXPR:
5200 case LTGT_EXPR:
5201 return fold_convert_loc (loc, type, arg1);
5202 case UNGE_EXPR:
5203 case UNGT_EXPR:
5204 if (flag_trapping_math)
5205 break;
5206 /* Fall through. */
5207 case GE_EXPR:
5208 case GT_EXPR:
5209 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5210 break;
5211 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5212 return fold_convert_loc (loc, type, tem);
5213 case UNLE_EXPR:
5214 case UNLT_EXPR:
5215 if (flag_trapping_math)
5216 break;
5217 /* FALLTHRU */
5218 case LE_EXPR:
5219 case LT_EXPR:
5220 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5221 break;
5222 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5223 return negate_expr (fold_convert_loc (loc, type, tem));
5224 default:
5225 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5226 break;
5229 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5230 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5231 both transformations are correct when A is NaN: A != 0
5232 is then true, and A == 0 is false. */
5234 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5235 && integer_zerop (arg01) && integer_zerop (arg2))
5237 if (comp_code == NE_EXPR)
5238 return fold_convert_loc (loc, type, arg1);
5239 else if (comp_code == EQ_EXPR)
5240 return build_zero_cst (type);
5243 /* Try some transformations of A op B ? A : B.
5245 A == B? A : B same as B
5246 A != B? A : B same as A
5247 A >= B? A : B same as max (A, B)
5248 A > B? A : B same as max (B, A)
5249 A <= B? A : B same as min (A, B)
5250 A < B? A : B same as min (B, A)
5252 As above, these transformations don't work in the presence
5253 of signed zeros. For example, if A and B are zeros of
5254 opposite sign, the first two transformations will change
5255 the sign of the result. In the last four, the original
5256 expressions give different results for (A=+0, B=-0) and
5257 (A=-0, B=+0), but the transformed expressions do not.
5259 The first two transformations are correct if either A or B
5260 is a NaN. In the first transformation, the condition will
5261 be false, and B will indeed be chosen. In the case of the
5262 second transformation, the condition A != B will be true,
5263 and A will be chosen.
5265 The conversions to max() and min() are not correct if B is
5266 a number and A is not. The conditions in the original
5267 expressions will be false, so all four give B. The min()
5268 and max() versions would give a NaN instead. */
5269 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5270 && operand_equal_for_comparison_p (arg01, arg2)
5271 /* Avoid these transformations if the COND_EXPR may be used
5272 as an lvalue in the C++ front-end. PR c++/19199. */
5273 && (in_gimple_form
5274 || VECTOR_TYPE_P (type)
5275 || (! lang_GNU_CXX ()
5276 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5277 || ! maybe_lvalue_p (arg1)
5278 || ! maybe_lvalue_p (arg2)))
5280 tree comp_op0 = arg00;
5281 tree comp_op1 = arg01;
5282 tree comp_type = TREE_TYPE (comp_op0);
5284 switch (comp_code)
5286 case EQ_EXPR:
5287 return fold_convert_loc (loc, type, arg2);
5288 case NE_EXPR:
5289 return fold_convert_loc (loc, type, arg1);
5290 case LE_EXPR:
5291 case LT_EXPR:
5292 case UNLE_EXPR:
5293 case UNLT_EXPR:
5294 /* In C++ a ?: expression can be an lvalue, so put the
5295 operand which will be used if they are equal first
5296 so that we can convert this back to the
5297 corresponding COND_EXPR. */
5298 if (!HONOR_NANS (arg1))
5300 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5301 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5302 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5303 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5304 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5305 comp_op1, comp_op0);
5306 return fold_convert_loc (loc, type, tem);
5308 break;
5309 case GE_EXPR:
5310 case GT_EXPR:
5311 case UNGE_EXPR:
5312 case UNGT_EXPR:
5313 if (!HONOR_NANS (arg1))
5315 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5316 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5317 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5318 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5319 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5320 comp_op1, comp_op0);
5321 return fold_convert_loc (loc, type, tem);
5323 break;
5324 case UNEQ_EXPR:
5325 if (!HONOR_NANS (arg1))
5326 return fold_convert_loc (loc, type, arg2);
5327 break;
5328 case LTGT_EXPR:
5329 if (!HONOR_NANS (arg1))
5330 return fold_convert_loc (loc, type, arg1);
5331 break;
5332 default:
5333 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5334 break;
5338 return NULL_TREE;
5343 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5344 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5345 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5346 false) >= 2)
5347 #endif
5349 /* EXP is some logical combination of boolean tests. See if we can
5350 merge it into some range test. Return the new tree if so. */
5352 static tree
5353 fold_range_test (location_t loc, enum tree_code code, tree type,
5354 tree op0, tree op1)
5356 int or_op = (code == TRUTH_ORIF_EXPR
5357 || code == TRUTH_OR_EXPR);
5358 int in0_p, in1_p, in_p;
5359 tree low0, low1, low, high0, high1, high;
5360 bool strict_overflow_p = false;
5361 tree tem, lhs, rhs;
5362 const char * const warnmsg = G_("assuming signed overflow does not occur "
5363 "when simplifying range test");
5365 if (!INTEGRAL_TYPE_P (type))
5366 return 0;
5368 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5369 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5371 /* If this is an OR operation, invert both sides; we will invert
5372 again at the end. */
5373 if (or_op)
5374 in0_p = ! in0_p, in1_p = ! in1_p;
5376 /* If both expressions are the same, if we can merge the ranges, and we
5377 can build the range test, return it or it inverted. If one of the
5378 ranges is always true or always false, consider it to be the same
5379 expression as the other. */
5380 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5381 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5382 in1_p, low1, high1)
5383 && 0 != (tem = (build_range_check (loc, type,
5384 lhs != 0 ? lhs
5385 : rhs != 0 ? rhs : integer_zero_node,
5386 in_p, low, high))))
5388 if (strict_overflow_p)
5389 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5390 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5393 /* On machines where the branch cost is expensive, if this is a
5394 short-circuited branch and the underlying object on both sides
5395 is the same, make a non-short-circuit operation. */
5396 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5397 && !flag_sanitize_coverage
5398 && lhs != 0 && rhs != 0
5399 && (code == TRUTH_ANDIF_EXPR
5400 || code == TRUTH_ORIF_EXPR)
5401 && operand_equal_p (lhs, rhs, 0))
5403 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5404 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5405 which cases we can't do this. */
5406 if (simple_operand_p (lhs))
5407 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5408 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5409 type, op0, op1);
5411 else if (!lang_hooks.decls.global_bindings_p ()
5412 && !CONTAINS_PLACEHOLDER_P (lhs))
5414 tree common = save_expr (lhs);
5416 if (0 != (lhs = build_range_check (loc, type, common,
5417 or_op ? ! in0_p : in0_p,
5418 low0, high0))
5419 && (0 != (rhs = build_range_check (loc, type, common,
5420 or_op ? ! in1_p : in1_p,
5421 low1, high1))))
5423 if (strict_overflow_p)
5424 fold_overflow_warning (warnmsg,
5425 WARN_STRICT_OVERFLOW_COMPARISON);
5426 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5427 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5428 type, lhs, rhs);
5433 return 0;
5436 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5437 bit value. Arrange things so the extra bits will be set to zero if and
5438 only if C is signed-extended to its full width. If MASK is nonzero,
5439 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5441 static tree
5442 unextend (tree c, int p, int unsignedp, tree mask)
5444 tree type = TREE_TYPE (c);
5445 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5446 tree temp;
5448 if (p == modesize || unsignedp)
5449 return c;
5451 /* We work by getting just the sign bit into the low-order bit, then
5452 into the high-order bit, then sign-extend. We then XOR that value
5453 with C. */
5454 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5456 /* We must use a signed type in order to get an arithmetic right shift.
5457 However, we must also avoid introducing accidental overflows, so that
5458 a subsequent call to integer_zerop will work. Hence we must
5459 do the type conversion here. At this point, the constant is either
5460 zero or one, and the conversion to a signed type can never overflow.
5461 We could get an overflow if this conversion is done anywhere else. */
5462 if (TYPE_UNSIGNED (type))
5463 temp = fold_convert (signed_type_for (type), temp);
5465 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5466 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5467 if (mask != 0)
5468 temp = const_binop (BIT_AND_EXPR, temp,
5469 fold_convert (TREE_TYPE (c), mask));
5470 /* If necessary, convert the type back to match the type of C. */
5471 if (TYPE_UNSIGNED (type))
5472 temp = fold_convert (type, temp);
5474 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5477 /* For an expression that has the form
5478 (A && B) || ~B
5480 (A || B) && ~B,
5481 we can drop one of the inner expressions and simplify to
5482 A || ~B
5484 A && ~B
5485 LOC is the location of the resulting expression. OP is the inner
5486 logical operation; the left-hand side in the examples above, while CMPOP
5487 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5488 removing a condition that guards another, as in
5489 (A != NULL && A->...) || A == NULL
5490 which we must not transform. If RHS_ONLY is true, only eliminate the
5491 right-most operand of the inner logical operation. */
5493 static tree
5494 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5495 bool rhs_only)
5497 tree type = TREE_TYPE (cmpop);
5498 enum tree_code code = TREE_CODE (cmpop);
5499 enum tree_code truthop_code = TREE_CODE (op);
5500 tree lhs = TREE_OPERAND (op, 0);
5501 tree rhs = TREE_OPERAND (op, 1);
5502 tree orig_lhs = lhs, orig_rhs = rhs;
5503 enum tree_code rhs_code = TREE_CODE (rhs);
5504 enum tree_code lhs_code = TREE_CODE (lhs);
5505 enum tree_code inv_code;
5507 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5508 return NULL_TREE;
5510 if (TREE_CODE_CLASS (code) != tcc_comparison)
5511 return NULL_TREE;
5513 if (rhs_code == truthop_code)
5515 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5516 if (newrhs != NULL_TREE)
5518 rhs = newrhs;
5519 rhs_code = TREE_CODE (rhs);
5522 if (lhs_code == truthop_code && !rhs_only)
5524 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5525 if (newlhs != NULL_TREE)
5527 lhs = newlhs;
5528 lhs_code = TREE_CODE (lhs);
5532 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5533 if (inv_code == rhs_code
5534 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5535 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5536 return lhs;
5537 if (!rhs_only && inv_code == lhs_code
5538 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5539 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5540 return rhs;
5541 if (rhs != orig_rhs || lhs != orig_lhs)
5542 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5543 lhs, rhs);
5544 return NULL_TREE;
5547 /* Find ways of folding logical expressions of LHS and RHS:
5548 Try to merge two comparisons to the same innermost item.
5549 Look for range tests like "ch >= '0' && ch <= '9'".
5550 Look for combinations of simple terms on machines with expensive branches
5551 and evaluate the RHS unconditionally.
5553 For example, if we have p->a == 2 && p->b == 4 and we can make an
5554 object large enough to span both A and B, we can do this with a comparison
5555 against the object ANDed with the a mask.
5557 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5558 operations to do this with one comparison.
5560 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5561 function and the one above.
5563 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5564 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5566 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5567 two operands.
5569 We return the simplified tree or 0 if no optimization is possible. */
5571 static tree
5572 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5573 tree lhs, tree rhs)
5575 /* If this is the "or" of two comparisons, we can do something if
5576 the comparisons are NE_EXPR. If this is the "and", we can do something
5577 if the comparisons are EQ_EXPR. I.e.,
5578 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5580 WANTED_CODE is this operation code. For single bit fields, we can
5581 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5582 comparison for one-bit fields. */
5584 enum tree_code wanted_code;
5585 enum tree_code lcode, rcode;
5586 tree ll_arg, lr_arg, rl_arg, rr_arg;
5587 tree ll_inner, lr_inner, rl_inner, rr_inner;
5588 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5589 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5590 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5591 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5592 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5593 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5594 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5595 scalar_int_mode lnmode, rnmode;
5596 tree ll_mask, lr_mask, rl_mask, rr_mask;
5597 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5598 tree l_const, r_const;
5599 tree lntype, rntype, result;
5600 HOST_WIDE_INT first_bit, end_bit;
5601 int volatilep;
5603 /* Start by getting the comparison codes. Fail if anything is volatile.
5604 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5605 it were surrounded with a NE_EXPR. */
5607 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5608 return 0;
5610 lcode = TREE_CODE (lhs);
5611 rcode = TREE_CODE (rhs);
5613 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5615 lhs = build2 (NE_EXPR, truth_type, lhs,
5616 build_int_cst (TREE_TYPE (lhs), 0));
5617 lcode = NE_EXPR;
5620 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5622 rhs = build2 (NE_EXPR, truth_type, rhs,
5623 build_int_cst (TREE_TYPE (rhs), 0));
5624 rcode = NE_EXPR;
5627 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5628 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5629 return 0;
5631 ll_arg = TREE_OPERAND (lhs, 0);
5632 lr_arg = TREE_OPERAND (lhs, 1);
5633 rl_arg = TREE_OPERAND (rhs, 0);
5634 rr_arg = TREE_OPERAND (rhs, 1);
5636 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5637 if (simple_operand_p (ll_arg)
5638 && simple_operand_p (lr_arg))
5640 if (operand_equal_p (ll_arg, rl_arg, 0)
5641 && operand_equal_p (lr_arg, rr_arg, 0))
5643 result = combine_comparisons (loc, code, lcode, rcode,
5644 truth_type, ll_arg, lr_arg);
5645 if (result)
5646 return result;
5648 else if (operand_equal_p (ll_arg, rr_arg, 0)
5649 && operand_equal_p (lr_arg, rl_arg, 0))
5651 result = combine_comparisons (loc, code, lcode,
5652 swap_tree_comparison (rcode),
5653 truth_type, ll_arg, lr_arg);
5654 if (result)
5655 return result;
5659 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5660 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5662 /* If the RHS can be evaluated unconditionally and its operands are
5663 simple, it wins to evaluate the RHS unconditionally on machines
5664 with expensive branches. In this case, this isn't a comparison
5665 that can be merged. */
5667 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5668 false) >= 2
5669 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5670 && simple_operand_p (rl_arg)
5671 && simple_operand_p (rr_arg))
5673 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5674 if (code == TRUTH_OR_EXPR
5675 && lcode == NE_EXPR && integer_zerop (lr_arg)
5676 && rcode == NE_EXPR && integer_zerop (rr_arg)
5677 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5678 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5679 return build2_loc (loc, NE_EXPR, truth_type,
5680 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5681 ll_arg, rl_arg),
5682 build_int_cst (TREE_TYPE (ll_arg), 0));
5684 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5685 if (code == TRUTH_AND_EXPR
5686 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5687 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5688 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5689 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5690 return build2_loc (loc, EQ_EXPR, truth_type,
5691 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5692 ll_arg, rl_arg),
5693 build_int_cst (TREE_TYPE (ll_arg), 0));
5696 /* See if the comparisons can be merged. Then get all the parameters for
5697 each side. */
5699 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5700 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5701 return 0;
5703 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5704 volatilep = 0;
5705 ll_inner = decode_field_reference (loc, &ll_arg,
5706 &ll_bitsize, &ll_bitpos, &ll_mode,
5707 &ll_unsignedp, &ll_reversep, &volatilep,
5708 &ll_mask, &ll_and_mask);
5709 lr_inner = decode_field_reference (loc, &lr_arg,
5710 &lr_bitsize, &lr_bitpos, &lr_mode,
5711 &lr_unsignedp, &lr_reversep, &volatilep,
5712 &lr_mask, &lr_and_mask);
5713 rl_inner = decode_field_reference (loc, &rl_arg,
5714 &rl_bitsize, &rl_bitpos, &rl_mode,
5715 &rl_unsignedp, &rl_reversep, &volatilep,
5716 &rl_mask, &rl_and_mask);
5717 rr_inner = decode_field_reference (loc, &rr_arg,
5718 &rr_bitsize, &rr_bitpos, &rr_mode,
5719 &rr_unsignedp, &rr_reversep, &volatilep,
5720 &rr_mask, &rr_and_mask);
5722 /* It must be true that the inner operation on the lhs of each
5723 comparison must be the same if we are to be able to do anything.
5724 Then see if we have constants. If not, the same must be true for
5725 the rhs's. */
5726 if (volatilep
5727 || ll_reversep != rl_reversep
5728 || ll_inner == 0 || rl_inner == 0
5729 || ! operand_equal_p (ll_inner, rl_inner, 0))
5730 return 0;
5732 if (TREE_CODE (lr_arg) == INTEGER_CST
5733 && TREE_CODE (rr_arg) == INTEGER_CST)
5735 l_const = lr_arg, r_const = rr_arg;
5736 lr_reversep = ll_reversep;
5738 else if (lr_reversep != rr_reversep
5739 || lr_inner == 0 || rr_inner == 0
5740 || ! operand_equal_p (lr_inner, rr_inner, 0))
5741 return 0;
5742 else
5743 l_const = r_const = 0;
5745 /* If either comparison code is not correct for our logical operation,
5746 fail. However, we can convert a one-bit comparison against zero into
5747 the opposite comparison against that bit being set in the field. */
5749 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5750 if (lcode != wanted_code)
5752 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5754 /* Make the left operand unsigned, since we are only interested
5755 in the value of one bit. Otherwise we are doing the wrong
5756 thing below. */
5757 ll_unsignedp = 1;
5758 l_const = ll_mask;
5760 else
5761 return 0;
5764 /* This is analogous to the code for l_const above. */
5765 if (rcode != wanted_code)
5767 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5769 rl_unsignedp = 1;
5770 r_const = rl_mask;
5772 else
5773 return 0;
5776 /* See if we can find a mode that contains both fields being compared on
5777 the left. If we can't, fail. Otherwise, update all constants and masks
5778 to be relative to a field of that size. */
5779 first_bit = MIN (ll_bitpos, rl_bitpos);
5780 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5781 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5782 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5783 volatilep, &lnmode))
5784 return 0;
5786 lnbitsize = GET_MODE_BITSIZE (lnmode);
5787 lnbitpos = first_bit & ~ (lnbitsize - 1);
5788 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5789 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5791 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5793 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5794 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5797 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5798 size_int (xll_bitpos));
5799 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5800 size_int (xrl_bitpos));
5802 if (l_const)
5804 l_const = fold_convert_loc (loc, lntype, l_const);
5805 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5806 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5807 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5808 fold_build1_loc (loc, BIT_NOT_EXPR,
5809 lntype, ll_mask))))
5811 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5813 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5816 if (r_const)
5818 r_const = fold_convert_loc (loc, lntype, r_const);
5819 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5820 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5821 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5822 fold_build1_loc (loc, BIT_NOT_EXPR,
5823 lntype, rl_mask))))
5825 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5827 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5831 /* If the right sides are not constant, do the same for it. Also,
5832 disallow this optimization if a size or signedness mismatch occurs
5833 between the left and right sides. */
5834 if (l_const == 0)
5836 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5837 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5838 /* Make sure the two fields on the right
5839 correspond to the left without being swapped. */
5840 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5841 return 0;
5843 first_bit = MIN (lr_bitpos, rr_bitpos);
5844 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5845 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5846 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5847 volatilep, &rnmode))
5848 return 0;
5850 rnbitsize = GET_MODE_BITSIZE (rnmode);
5851 rnbitpos = first_bit & ~ (rnbitsize - 1);
5852 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5853 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5855 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5857 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5858 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5861 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5862 rntype, lr_mask),
5863 size_int (xlr_bitpos));
5864 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5865 rntype, rr_mask),
5866 size_int (xrr_bitpos));
5868 /* Make a mask that corresponds to both fields being compared.
5869 Do this for both items being compared. If the operands are the
5870 same size and the bits being compared are in the same position
5871 then we can do this by masking both and comparing the masked
5872 results. */
5873 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5874 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5875 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5877 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5878 lntype, lnbitsize, lnbitpos,
5879 ll_unsignedp || rl_unsignedp, ll_reversep);
5880 if (! all_ones_mask_p (ll_mask, lnbitsize))
5881 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5883 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5884 rntype, rnbitsize, rnbitpos,
5885 lr_unsignedp || rr_unsignedp, lr_reversep);
5886 if (! all_ones_mask_p (lr_mask, rnbitsize))
5887 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5889 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5892 /* There is still another way we can do something: If both pairs of
5893 fields being compared are adjacent, we may be able to make a wider
5894 field containing them both.
5896 Note that we still must mask the lhs/rhs expressions. Furthermore,
5897 the mask must be shifted to account for the shift done by
5898 make_bit_field_ref. */
5899 if ((ll_bitsize + ll_bitpos == rl_bitpos
5900 && lr_bitsize + lr_bitpos == rr_bitpos)
5901 || (ll_bitpos == rl_bitpos + rl_bitsize
5902 && lr_bitpos == rr_bitpos + rr_bitsize))
5904 tree type;
5906 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5907 ll_bitsize + rl_bitsize,
5908 MIN (ll_bitpos, rl_bitpos),
5909 ll_unsignedp, ll_reversep);
5910 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5911 lr_bitsize + rr_bitsize,
5912 MIN (lr_bitpos, rr_bitpos),
5913 lr_unsignedp, lr_reversep);
5915 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5916 size_int (MIN (xll_bitpos, xrl_bitpos)));
5917 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5918 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5920 /* Convert to the smaller type before masking out unwanted bits. */
5921 type = lntype;
5922 if (lntype != rntype)
5924 if (lnbitsize > rnbitsize)
5926 lhs = fold_convert_loc (loc, rntype, lhs);
5927 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5928 type = rntype;
5930 else if (lnbitsize < rnbitsize)
5932 rhs = fold_convert_loc (loc, lntype, rhs);
5933 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5934 type = lntype;
5938 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5939 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5941 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5942 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5944 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5947 return 0;
5950 /* Handle the case of comparisons with constants. If there is something in
5951 common between the masks, those bits of the constants must be the same.
5952 If not, the condition is always false. Test for this to avoid generating
5953 incorrect code below. */
5954 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5955 if (! integer_zerop (result)
5956 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5957 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5959 if (wanted_code == NE_EXPR)
5961 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5962 return constant_boolean_node (true, truth_type);
5964 else
5966 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5967 return constant_boolean_node (false, truth_type);
5971 /* Construct the expression we will return. First get the component
5972 reference we will make. Unless the mask is all ones the width of
5973 that field, perform the mask operation. Then compare with the
5974 merged constant. */
5975 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5976 lntype, lnbitsize, lnbitpos,
5977 ll_unsignedp || rl_unsignedp, ll_reversep);
5979 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5980 if (! all_ones_mask_p (ll_mask, lnbitsize))
5981 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5983 return build2_loc (loc, wanted_code, truth_type, result,
5984 const_binop (BIT_IOR_EXPR, l_const, r_const));
5987 /* T is an integer expression that is being multiplied, divided, or taken a
5988 modulus (CODE says which and what kind of divide or modulus) by a
5989 constant C. See if we can eliminate that operation by folding it with
5990 other operations already in T. WIDE_TYPE, if non-null, is a type that
5991 should be used for the computation if wider than our type.
5993 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5994 (X * 2) + (Y * 4). We must, however, be assured that either the original
5995 expression would not overflow or that overflow is undefined for the type
5996 in the language in question.
5998 If we return a non-null expression, it is an equivalent form of the
5999 original computation, but need not be in the original type.
6001 We set *STRICT_OVERFLOW_P to true if the return values depends on
6002 signed overflow being undefined. Otherwise we do not change
6003 *STRICT_OVERFLOW_P. */
6005 static tree
6006 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6007 bool *strict_overflow_p)
6009 /* To avoid exponential search depth, refuse to allow recursion past
6010 three levels. Beyond that (1) it's highly unlikely that we'll find
6011 something interesting and (2) we've probably processed it before
6012 when we built the inner expression. */
6014 static int depth;
6015 tree ret;
6017 if (depth > 3)
6018 return NULL;
6020 depth++;
6021 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6022 depth--;
6024 return ret;
6027 static tree
6028 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6029 bool *strict_overflow_p)
6031 tree type = TREE_TYPE (t);
6032 enum tree_code tcode = TREE_CODE (t);
6033 tree ctype = (wide_type != 0
6034 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6035 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6036 ? wide_type : type);
6037 tree t1, t2;
6038 int same_p = tcode == code;
6039 tree op0 = NULL_TREE, op1 = NULL_TREE;
6040 bool sub_strict_overflow_p;
6042 /* Don't deal with constants of zero here; they confuse the code below. */
6043 if (integer_zerop (c))
6044 return NULL_TREE;
6046 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6047 op0 = TREE_OPERAND (t, 0);
6049 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6050 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6052 /* Note that we need not handle conditional operations here since fold
6053 already handles those cases. So just do arithmetic here. */
6054 switch (tcode)
6056 case INTEGER_CST:
6057 /* For a constant, we can always simplify if we are a multiply
6058 or (for divide and modulus) if it is a multiple of our constant. */
6059 if (code == MULT_EXPR
6060 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6062 tree tem = const_binop (code, fold_convert (ctype, t),
6063 fold_convert (ctype, c));
6064 /* If the multiplication overflowed, we lost information on it.
6065 See PR68142 and PR69845. */
6066 if (TREE_OVERFLOW (tem))
6067 return NULL_TREE;
6068 return tem;
6070 break;
6072 CASE_CONVERT: case NON_LVALUE_EXPR:
6073 /* If op0 is an expression ... */
6074 if ((COMPARISON_CLASS_P (op0)
6075 || UNARY_CLASS_P (op0)
6076 || BINARY_CLASS_P (op0)
6077 || VL_EXP_CLASS_P (op0)
6078 || EXPRESSION_CLASS_P (op0))
6079 /* ... and has wrapping overflow, and its type is smaller
6080 than ctype, then we cannot pass through as widening. */
6081 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6082 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6083 && (TYPE_PRECISION (ctype)
6084 > TYPE_PRECISION (TREE_TYPE (op0))))
6085 /* ... or this is a truncation (t is narrower than op0),
6086 then we cannot pass through this narrowing. */
6087 || (TYPE_PRECISION (type)
6088 < TYPE_PRECISION (TREE_TYPE (op0)))
6089 /* ... or signedness changes for division or modulus,
6090 then we cannot pass through this conversion. */
6091 || (code != MULT_EXPR
6092 && (TYPE_UNSIGNED (ctype)
6093 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6094 /* ... or has undefined overflow while the converted to
6095 type has not, we cannot do the operation in the inner type
6096 as that would introduce undefined overflow. */
6097 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6098 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6099 && !TYPE_OVERFLOW_UNDEFINED (type))))
6100 break;
6102 /* Pass the constant down and see if we can make a simplification. If
6103 we can, replace this expression with the inner simplification for
6104 possible later conversion to our or some other type. */
6105 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6106 && TREE_CODE (t2) == INTEGER_CST
6107 && !TREE_OVERFLOW (t2)
6108 && (0 != (t1 = extract_muldiv (op0, t2, code,
6109 code == MULT_EXPR
6110 ? ctype : NULL_TREE,
6111 strict_overflow_p))))
6112 return t1;
6113 break;
6115 case ABS_EXPR:
6116 /* If widening the type changes it from signed to unsigned, then we
6117 must avoid building ABS_EXPR itself as unsigned. */
6118 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6120 tree cstype = (*signed_type_for) (ctype);
6121 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6122 != 0)
6124 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6125 return fold_convert (ctype, t1);
6127 break;
6129 /* If the constant is negative, we cannot simplify this. */
6130 if (tree_int_cst_sgn (c) == -1)
6131 break;
6132 /* FALLTHROUGH */
6133 case NEGATE_EXPR:
6134 /* For division and modulus, type can't be unsigned, as e.g.
6135 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6136 For signed types, even with wrapping overflow, this is fine. */
6137 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6138 break;
6139 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6140 != 0)
6141 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6142 break;
6144 case MIN_EXPR: case MAX_EXPR:
6145 /* If widening the type changes the signedness, then we can't perform
6146 this optimization as that changes the result. */
6147 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6148 break;
6150 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6151 sub_strict_overflow_p = false;
6152 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6153 &sub_strict_overflow_p)) != 0
6154 && (t2 = extract_muldiv (op1, c, code, wide_type,
6155 &sub_strict_overflow_p)) != 0)
6157 if (tree_int_cst_sgn (c) < 0)
6158 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6159 if (sub_strict_overflow_p)
6160 *strict_overflow_p = true;
6161 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6162 fold_convert (ctype, t2));
6164 break;
6166 case LSHIFT_EXPR: case RSHIFT_EXPR:
6167 /* If the second operand is constant, this is a multiplication
6168 or floor division, by a power of two, so we can treat it that
6169 way unless the multiplier or divisor overflows. Signed
6170 left-shift overflow is implementation-defined rather than
6171 undefined in C90, so do not convert signed left shift into
6172 multiplication. */
6173 if (TREE_CODE (op1) == INTEGER_CST
6174 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6175 /* const_binop may not detect overflow correctly,
6176 so check for it explicitly here. */
6177 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6178 && 0 != (t1 = fold_convert (ctype,
6179 const_binop (LSHIFT_EXPR,
6180 size_one_node,
6181 op1)))
6182 && !TREE_OVERFLOW (t1))
6183 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6184 ? MULT_EXPR : FLOOR_DIV_EXPR,
6185 ctype,
6186 fold_convert (ctype, op0),
6187 t1),
6188 c, code, wide_type, strict_overflow_p);
6189 break;
6191 case PLUS_EXPR: case MINUS_EXPR:
6192 /* See if we can eliminate the operation on both sides. If we can, we
6193 can return a new PLUS or MINUS. If we can't, the only remaining
6194 cases where we can do anything are if the second operand is a
6195 constant. */
6196 sub_strict_overflow_p = false;
6197 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6198 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6199 if (t1 != 0 && t2 != 0
6200 && TYPE_OVERFLOW_WRAPS (ctype)
6201 && (code == MULT_EXPR
6202 /* If not multiplication, we can only do this if both operands
6203 are divisible by c. */
6204 || (multiple_of_p (ctype, op0, c)
6205 && multiple_of_p (ctype, op1, c))))
6207 if (sub_strict_overflow_p)
6208 *strict_overflow_p = true;
6209 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6210 fold_convert (ctype, t2));
6213 /* If this was a subtraction, negate OP1 and set it to be an addition.
6214 This simplifies the logic below. */
6215 if (tcode == MINUS_EXPR)
6217 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6218 /* If OP1 was not easily negatable, the constant may be OP0. */
6219 if (TREE_CODE (op0) == INTEGER_CST)
6221 std::swap (op0, op1);
6222 std::swap (t1, t2);
6226 if (TREE_CODE (op1) != INTEGER_CST)
6227 break;
6229 /* If either OP1 or C are negative, this optimization is not safe for
6230 some of the division and remainder types while for others we need
6231 to change the code. */
6232 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6234 if (code == CEIL_DIV_EXPR)
6235 code = FLOOR_DIV_EXPR;
6236 else if (code == FLOOR_DIV_EXPR)
6237 code = CEIL_DIV_EXPR;
6238 else if (code != MULT_EXPR
6239 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6240 break;
6243 /* If it's a multiply or a division/modulus operation of a multiple
6244 of our constant, do the operation and verify it doesn't overflow. */
6245 if (code == MULT_EXPR
6246 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6248 op1 = const_binop (code, fold_convert (ctype, op1),
6249 fold_convert (ctype, c));
6250 /* We allow the constant to overflow with wrapping semantics. */
6251 if (op1 == 0
6252 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6253 break;
6255 else
6256 break;
6258 /* If we have an unsigned type, we cannot widen the operation since it
6259 will change the result if the original computation overflowed. */
6260 if (TYPE_UNSIGNED (ctype) && ctype != type)
6261 break;
6263 /* The last case is if we are a multiply. In that case, we can
6264 apply the distributive law to commute the multiply and addition
6265 if the multiplication of the constants doesn't overflow
6266 and overflow is defined. With undefined overflow
6267 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6268 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6269 return fold_build2 (tcode, ctype,
6270 fold_build2 (code, ctype,
6271 fold_convert (ctype, op0),
6272 fold_convert (ctype, c)),
6273 op1);
6275 break;
6277 case MULT_EXPR:
6278 /* We have a special case here if we are doing something like
6279 (C * 8) % 4 since we know that's zero. */
6280 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6281 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6282 /* If the multiplication can overflow we cannot optimize this. */
6283 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6284 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6285 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6287 *strict_overflow_p = true;
6288 return omit_one_operand (type, integer_zero_node, op0);
6291 /* ... fall through ... */
6293 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6294 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6295 /* If we can extract our operation from the LHS, do so and return a
6296 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6297 do something only if the second operand is a constant. */
6298 if (same_p
6299 && TYPE_OVERFLOW_WRAPS (ctype)
6300 && (t1 = extract_muldiv (op0, c, code, wide_type,
6301 strict_overflow_p)) != 0)
6302 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6303 fold_convert (ctype, op1));
6304 else if (tcode == MULT_EXPR && code == MULT_EXPR
6305 && TYPE_OVERFLOW_WRAPS (ctype)
6306 && (t1 = extract_muldiv (op1, c, code, wide_type,
6307 strict_overflow_p)) != 0)
6308 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6309 fold_convert (ctype, t1));
6310 else if (TREE_CODE (op1) != INTEGER_CST)
6311 return 0;
6313 /* If these are the same operation types, we can associate them
6314 assuming no overflow. */
6315 if (tcode == code)
6317 bool overflow_p = false;
6318 bool overflow_mul_p;
6319 signop sign = TYPE_SIGN (ctype);
6320 unsigned prec = TYPE_PRECISION (ctype);
6321 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6322 wi::to_wide (c, prec),
6323 sign, &overflow_mul_p);
6324 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6325 if (overflow_mul_p
6326 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6327 overflow_p = true;
6328 if (!overflow_p)
6329 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6330 wide_int_to_tree (ctype, mul));
6333 /* If these operations "cancel" each other, we have the main
6334 optimizations of this pass, which occur when either constant is a
6335 multiple of the other, in which case we replace this with either an
6336 operation or CODE or TCODE.
6338 If we have an unsigned type, we cannot do this since it will change
6339 the result if the original computation overflowed. */
6340 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6341 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6342 || (tcode == MULT_EXPR
6343 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6344 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6345 && code != MULT_EXPR)))
6347 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6349 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6350 *strict_overflow_p = true;
6351 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6352 fold_convert (ctype,
6353 const_binop (TRUNC_DIV_EXPR,
6354 op1, c)));
6356 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6358 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6359 *strict_overflow_p = true;
6360 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6361 fold_convert (ctype,
6362 const_binop (TRUNC_DIV_EXPR,
6363 c, op1)));
6366 break;
6368 default:
6369 break;
6372 return 0;
6375 /* Return a node which has the indicated constant VALUE (either 0 or
6376 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6377 and is of the indicated TYPE. */
6379 tree
6380 constant_boolean_node (bool value, tree type)
6382 if (type == integer_type_node)
6383 return value ? integer_one_node : integer_zero_node;
6384 else if (type == boolean_type_node)
6385 return value ? boolean_true_node : boolean_false_node;
6386 else if (TREE_CODE (type) == VECTOR_TYPE)
6387 return build_vector_from_val (type,
6388 build_int_cst (TREE_TYPE (type),
6389 value ? -1 : 0));
6390 else
6391 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6395 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6396 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6397 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6398 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6399 COND is the first argument to CODE; otherwise (as in the example
6400 given here), it is the second argument. TYPE is the type of the
6401 original expression. Return NULL_TREE if no simplification is
6402 possible. */
6404 static tree
6405 fold_binary_op_with_conditional_arg (location_t loc,
6406 enum tree_code code,
6407 tree type, tree op0, tree op1,
6408 tree cond, tree arg, int cond_first_p)
6410 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6411 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6412 tree test, true_value, false_value;
6413 tree lhs = NULL_TREE;
6414 tree rhs = NULL_TREE;
6415 enum tree_code cond_code = COND_EXPR;
6417 if (TREE_CODE (cond) == COND_EXPR
6418 || TREE_CODE (cond) == VEC_COND_EXPR)
6420 test = TREE_OPERAND (cond, 0);
6421 true_value = TREE_OPERAND (cond, 1);
6422 false_value = TREE_OPERAND (cond, 2);
6423 /* If this operand throws an expression, then it does not make
6424 sense to try to perform a logical or arithmetic operation
6425 involving it. */
6426 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6427 lhs = true_value;
6428 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6429 rhs = false_value;
6431 else if (!(TREE_CODE (type) != VECTOR_TYPE
6432 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6434 tree testtype = TREE_TYPE (cond);
6435 test = cond;
6436 true_value = constant_boolean_node (true, testtype);
6437 false_value = constant_boolean_node (false, testtype);
6439 else
6440 /* Detect the case of mixing vector and scalar types - bail out. */
6441 return NULL_TREE;
6443 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6444 cond_code = VEC_COND_EXPR;
6446 /* This transformation is only worthwhile if we don't have to wrap ARG
6447 in a SAVE_EXPR and the operation can be simplified without recursing
6448 on at least one of the branches once its pushed inside the COND_EXPR. */
6449 if (!TREE_CONSTANT (arg)
6450 && (TREE_SIDE_EFFECTS (arg)
6451 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6452 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6453 return NULL_TREE;
6455 arg = fold_convert_loc (loc, arg_type, arg);
6456 if (lhs == 0)
6458 true_value = fold_convert_loc (loc, cond_type, true_value);
6459 if (cond_first_p)
6460 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6461 else
6462 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6464 if (rhs == 0)
6466 false_value = fold_convert_loc (loc, cond_type, false_value);
6467 if (cond_first_p)
6468 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6469 else
6470 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6473 /* Check that we have simplified at least one of the branches. */
6474 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6475 return NULL_TREE;
6477 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6481 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6483 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6484 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6485 ADDEND is the same as X.
6487 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6488 and finite. The problematic cases are when X is zero, and its mode
6489 has signed zeros. In the case of rounding towards -infinity,
6490 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6491 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6493 bool
6494 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6496 if (!real_zerop (addend))
6497 return false;
6499 /* Don't allow the fold with -fsignaling-nans. */
6500 if (HONOR_SNANS (element_mode (type)))
6501 return false;
6503 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6504 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6505 return true;
6507 /* In a vector or complex, we would need to check the sign of all zeros. */
6508 if (TREE_CODE (addend) != REAL_CST)
6509 return false;
6511 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6512 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6513 negate = !negate;
6515 /* The mode has signed zeros, and we have to honor their sign.
6516 In this situation, there is only one case we can return true for.
6517 X - 0 is the same as X unless rounding towards -infinity is
6518 supported. */
6519 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6522 /* Subroutine of match.pd that optimizes comparisons of a division by
6523 a nonzero integer constant against an integer constant, i.e.
6524 X/C1 op C2.
6526 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6527 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6529 enum tree_code
6530 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6531 tree *hi, bool *neg_overflow)
6533 tree prod, tmp, type = TREE_TYPE (c1);
6534 signop sign = TYPE_SIGN (type);
6535 bool overflow;
6537 /* We have to do this the hard way to detect unsigned overflow.
6538 prod = int_const_binop (MULT_EXPR, c1, c2); */
6539 wide_int val = wi::mul (c1, c2, sign, &overflow);
6540 prod = force_fit_type (type, val, -1, overflow);
6541 *neg_overflow = false;
6543 if (sign == UNSIGNED)
6545 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6546 *lo = prod;
6548 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6549 val = wi::add (prod, tmp, sign, &overflow);
6550 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6552 else if (tree_int_cst_sgn (c1) >= 0)
6554 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6555 switch (tree_int_cst_sgn (c2))
6557 case -1:
6558 *neg_overflow = true;
6559 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6560 *hi = prod;
6561 break;
6563 case 0:
6564 *lo = fold_negate_const (tmp, type);
6565 *hi = tmp;
6566 break;
6568 case 1:
6569 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6570 *lo = prod;
6571 break;
6573 default:
6574 gcc_unreachable ();
6577 else
6579 /* A negative divisor reverses the relational operators. */
6580 code = swap_tree_comparison (code);
6582 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6583 switch (tree_int_cst_sgn (c2))
6585 case -1:
6586 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6587 *lo = prod;
6588 break;
6590 case 0:
6591 *hi = fold_negate_const (tmp, type);
6592 *lo = tmp;
6593 break;
6595 case 1:
6596 *neg_overflow = true;
6597 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6598 *hi = prod;
6599 break;
6601 default:
6602 gcc_unreachable ();
6606 if (code != EQ_EXPR && code != NE_EXPR)
6607 return code;
6609 if (TREE_OVERFLOW (*lo)
6610 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6611 *lo = NULL_TREE;
6612 if (TREE_OVERFLOW (*hi)
6613 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6614 *hi = NULL_TREE;
6616 return code;
6620 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6621 equality/inequality test, then return a simplified form of the test
6622 using a sign testing. Otherwise return NULL. TYPE is the desired
6623 result type. */
6625 static tree
6626 fold_single_bit_test_into_sign_test (location_t loc,
6627 enum tree_code code, tree arg0, tree arg1,
6628 tree result_type)
6630 /* If this is testing a single bit, we can optimize the test. */
6631 if ((code == NE_EXPR || code == EQ_EXPR)
6632 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6633 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6635 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6636 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6637 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6639 if (arg00 != NULL_TREE
6640 /* This is only a win if casting to a signed type is cheap,
6641 i.e. when arg00's type is not a partial mode. */
6642 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6644 tree stype = signed_type_for (TREE_TYPE (arg00));
6645 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6646 result_type,
6647 fold_convert_loc (loc, stype, arg00),
6648 build_int_cst (stype, 0));
6652 return NULL_TREE;
6655 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6656 equality/inequality test, then return a simplified form of
6657 the test using shifts and logical operations. Otherwise return
6658 NULL. TYPE is the desired result type. */
6660 tree
6661 fold_single_bit_test (location_t loc, enum tree_code code,
6662 tree arg0, tree arg1, tree result_type)
6664 /* If this is testing a single bit, we can optimize the test. */
6665 if ((code == NE_EXPR || code == EQ_EXPR)
6666 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6667 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6669 tree inner = TREE_OPERAND (arg0, 0);
6670 tree type = TREE_TYPE (arg0);
6671 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6672 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6673 int ops_unsigned;
6674 tree signed_type, unsigned_type, intermediate_type;
6675 tree tem, one;
6677 /* First, see if we can fold the single bit test into a sign-bit
6678 test. */
6679 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6680 result_type);
6681 if (tem)
6682 return tem;
6684 /* Otherwise we have (A & C) != 0 where C is a single bit,
6685 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6686 Similarly for (A & C) == 0. */
6688 /* If INNER is a right shift of a constant and it plus BITNUM does
6689 not overflow, adjust BITNUM and INNER. */
6690 if (TREE_CODE (inner) == RSHIFT_EXPR
6691 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6692 && bitnum < TYPE_PRECISION (type)
6693 && wi::ltu_p (TREE_OPERAND (inner, 1),
6694 TYPE_PRECISION (type) - bitnum))
6696 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6697 inner = TREE_OPERAND (inner, 0);
6700 /* If we are going to be able to omit the AND below, we must do our
6701 operations as unsigned. If we must use the AND, we have a choice.
6702 Normally unsigned is faster, but for some machines signed is. */
6703 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6704 && !flag_syntax_only) ? 0 : 1;
6706 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6707 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6708 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6709 inner = fold_convert_loc (loc, intermediate_type, inner);
6711 if (bitnum != 0)
6712 inner = build2 (RSHIFT_EXPR, intermediate_type,
6713 inner, size_int (bitnum));
6715 one = build_int_cst (intermediate_type, 1);
6717 if (code == EQ_EXPR)
6718 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6720 /* Put the AND last so it can combine with more things. */
6721 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6723 /* Make sure to return the proper type. */
6724 inner = fold_convert_loc (loc, result_type, inner);
6726 return inner;
6728 return NULL_TREE;
6731 /* Test whether it is preferable two swap two operands, ARG0 and
6732 ARG1, for example because ARG0 is an integer constant and ARG1
6733 isn't. */
6735 bool
6736 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6738 if (CONSTANT_CLASS_P (arg1))
6739 return 0;
6740 if (CONSTANT_CLASS_P (arg0))
6741 return 1;
6743 STRIP_NOPS (arg0);
6744 STRIP_NOPS (arg1);
6746 if (TREE_CONSTANT (arg1))
6747 return 0;
6748 if (TREE_CONSTANT (arg0))
6749 return 1;
6751 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6752 for commutative and comparison operators. Ensuring a canonical
6753 form allows the optimizers to find additional redundancies without
6754 having to explicitly check for both orderings. */
6755 if (TREE_CODE (arg0) == SSA_NAME
6756 && TREE_CODE (arg1) == SSA_NAME
6757 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6758 return 1;
6760 /* Put SSA_NAMEs last. */
6761 if (TREE_CODE (arg1) == SSA_NAME)
6762 return 0;
6763 if (TREE_CODE (arg0) == SSA_NAME)
6764 return 1;
6766 /* Put variables last. */
6767 if (DECL_P (arg1))
6768 return 0;
6769 if (DECL_P (arg0))
6770 return 1;
6772 return 0;
6776 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6777 means A >= Y && A != MAX, but in this case we know that
6778 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6780 static tree
6781 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6783 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6785 if (TREE_CODE (bound) == LT_EXPR)
6786 a = TREE_OPERAND (bound, 0);
6787 else if (TREE_CODE (bound) == GT_EXPR)
6788 a = TREE_OPERAND (bound, 1);
6789 else
6790 return NULL_TREE;
6792 typea = TREE_TYPE (a);
6793 if (!INTEGRAL_TYPE_P (typea)
6794 && !POINTER_TYPE_P (typea))
6795 return NULL_TREE;
6797 if (TREE_CODE (ineq) == LT_EXPR)
6799 a1 = TREE_OPERAND (ineq, 1);
6800 y = TREE_OPERAND (ineq, 0);
6802 else if (TREE_CODE (ineq) == GT_EXPR)
6804 a1 = TREE_OPERAND (ineq, 0);
6805 y = TREE_OPERAND (ineq, 1);
6807 else
6808 return NULL_TREE;
6810 if (TREE_TYPE (a1) != typea)
6811 return NULL_TREE;
6813 if (POINTER_TYPE_P (typea))
6815 /* Convert the pointer types into integer before taking the difference. */
6816 tree ta = fold_convert_loc (loc, ssizetype, a);
6817 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6818 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6820 else
6821 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6823 if (!diff || !integer_onep (diff))
6824 return NULL_TREE;
6826 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6829 /* Fold a sum or difference of at least one multiplication.
6830 Returns the folded tree or NULL if no simplification could be made. */
6832 static tree
6833 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6834 tree arg0, tree arg1)
6836 tree arg00, arg01, arg10, arg11;
6837 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6839 /* (A * C) +- (B * C) -> (A+-B) * C.
6840 (A * C) +- A -> A * (C+-1).
6841 We are most concerned about the case where C is a constant,
6842 but other combinations show up during loop reduction. Since
6843 it is not difficult, try all four possibilities. */
6845 if (TREE_CODE (arg0) == MULT_EXPR)
6847 arg00 = TREE_OPERAND (arg0, 0);
6848 arg01 = TREE_OPERAND (arg0, 1);
6850 else if (TREE_CODE (arg0) == INTEGER_CST)
6852 arg00 = build_one_cst (type);
6853 arg01 = arg0;
6855 else
6857 /* We cannot generate constant 1 for fract. */
6858 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6859 return NULL_TREE;
6860 arg00 = arg0;
6861 arg01 = build_one_cst (type);
6863 if (TREE_CODE (arg1) == MULT_EXPR)
6865 arg10 = TREE_OPERAND (arg1, 0);
6866 arg11 = TREE_OPERAND (arg1, 1);
6868 else if (TREE_CODE (arg1) == INTEGER_CST)
6870 arg10 = build_one_cst (type);
6871 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6872 the purpose of this canonicalization. */
6873 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6874 && negate_expr_p (arg1)
6875 && code == PLUS_EXPR)
6877 arg11 = negate_expr (arg1);
6878 code = MINUS_EXPR;
6880 else
6881 arg11 = arg1;
6883 else
6885 /* We cannot generate constant 1 for fract. */
6886 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6887 return NULL_TREE;
6888 arg10 = arg1;
6889 arg11 = build_one_cst (type);
6891 same = NULL_TREE;
6893 /* Prefer factoring a common non-constant. */
6894 if (operand_equal_p (arg00, arg10, 0))
6895 same = arg00, alt0 = arg01, alt1 = arg11;
6896 else if (operand_equal_p (arg01, arg11, 0))
6897 same = arg01, alt0 = arg00, alt1 = arg10;
6898 else if (operand_equal_p (arg00, arg11, 0))
6899 same = arg00, alt0 = arg01, alt1 = arg10;
6900 else if (operand_equal_p (arg01, arg10, 0))
6901 same = arg01, alt0 = arg00, alt1 = arg11;
6903 /* No identical multiplicands; see if we can find a common
6904 power-of-two factor in non-power-of-two multiplies. This
6905 can help in multi-dimensional array access. */
6906 else if (tree_fits_shwi_p (arg01)
6907 && tree_fits_shwi_p (arg11))
6909 HOST_WIDE_INT int01, int11, tmp;
6910 bool swap = false;
6911 tree maybe_same;
6912 int01 = tree_to_shwi (arg01);
6913 int11 = tree_to_shwi (arg11);
6915 /* Move min of absolute values to int11. */
6916 if (absu_hwi (int01) < absu_hwi (int11))
6918 tmp = int01, int01 = int11, int11 = tmp;
6919 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6920 maybe_same = arg01;
6921 swap = true;
6923 else
6924 maybe_same = arg11;
6926 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6927 /* The remainder should not be a constant, otherwise we
6928 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6929 increased the number of multiplications necessary. */
6930 && TREE_CODE (arg10) != INTEGER_CST)
6932 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6933 build_int_cst (TREE_TYPE (arg00),
6934 int01 / int11));
6935 alt1 = arg10;
6936 same = maybe_same;
6937 if (swap)
6938 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6942 if (!same)
6943 return NULL_TREE;
6945 if (! INTEGRAL_TYPE_P (type)
6946 || TYPE_OVERFLOW_WRAPS (type)
6947 /* We are neither factoring zero nor minus one. */
6948 || TREE_CODE (same) == INTEGER_CST)
6949 return fold_build2_loc (loc, MULT_EXPR, type,
6950 fold_build2_loc (loc, code, type,
6951 fold_convert_loc (loc, type, alt0),
6952 fold_convert_loc (loc, type, alt1)),
6953 fold_convert_loc (loc, type, same));
6955 /* Same may be zero and thus the operation 'code' may overflow. Likewise
6956 same may be minus one and thus the multiplication may overflow. Perform
6957 the operations in an unsigned type. */
6958 tree utype = unsigned_type_for (type);
6959 tree tem = fold_build2_loc (loc, code, utype,
6960 fold_convert_loc (loc, utype, alt0),
6961 fold_convert_loc (loc, utype, alt1));
6962 /* If the sum evaluated to a constant that is not -INF the multiplication
6963 cannot overflow. */
6964 if (TREE_CODE (tem) == INTEGER_CST
6965 && ! wi::eq_p (tem, wi::min_value (TYPE_PRECISION (utype), SIGNED)))
6966 return fold_build2_loc (loc, MULT_EXPR, type,
6967 fold_convert (type, tem), same);
6969 return fold_convert_loc (loc, type,
6970 fold_build2_loc (loc, MULT_EXPR, utype, tem,
6971 fold_convert_loc (loc, utype, same)));
6974 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6975 specified by EXPR into the buffer PTR of length LEN bytes.
6976 Return the number of bytes placed in the buffer, or zero
6977 upon failure. */
6979 static int
6980 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6982 tree type = TREE_TYPE (expr);
6983 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
6984 int byte, offset, word, words;
6985 unsigned char value;
6987 if ((off == -1 && total_bytes > len)
6988 || off >= total_bytes)
6989 return 0;
6990 if (off == -1)
6991 off = 0;
6992 words = total_bytes / UNITS_PER_WORD;
6994 for (byte = 0; byte < total_bytes; byte++)
6996 int bitpos = byte * BITS_PER_UNIT;
6997 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6998 number of bytes. */
6999 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7001 if (total_bytes > UNITS_PER_WORD)
7003 word = byte / UNITS_PER_WORD;
7004 if (WORDS_BIG_ENDIAN)
7005 word = (words - 1) - word;
7006 offset = word * UNITS_PER_WORD;
7007 if (BYTES_BIG_ENDIAN)
7008 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7009 else
7010 offset += byte % UNITS_PER_WORD;
7012 else
7013 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7014 if (offset >= off
7015 && offset - off < len)
7016 ptr[offset - off] = value;
7018 return MIN (len, total_bytes - off);
7022 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7023 specified by EXPR into the buffer PTR of length LEN bytes.
7024 Return the number of bytes placed in the buffer, or zero
7025 upon failure. */
7027 static int
7028 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7030 tree type = TREE_TYPE (expr);
7031 scalar_mode mode = SCALAR_TYPE_MODE (type);
7032 int total_bytes = GET_MODE_SIZE (mode);
7033 FIXED_VALUE_TYPE value;
7034 tree i_value, i_type;
7036 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7037 return 0;
7039 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7041 if (NULL_TREE == i_type
7042 || TYPE_PRECISION (i_type) != total_bytes)
7043 return 0;
7045 value = TREE_FIXED_CST (expr);
7046 i_value = double_int_to_tree (i_type, value.data);
7048 return native_encode_int (i_value, ptr, len, off);
7052 /* Subroutine of native_encode_expr. Encode the REAL_CST
7053 specified by EXPR into the buffer PTR of length LEN bytes.
7054 Return the number of bytes placed in the buffer, or zero
7055 upon failure. */
7057 static int
7058 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7060 tree type = TREE_TYPE (expr);
7061 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7062 int byte, offset, word, words, bitpos;
7063 unsigned char value;
7065 /* There are always 32 bits in each long, no matter the size of
7066 the hosts long. We handle floating point representations with
7067 up to 192 bits. */
7068 long tmp[6];
7070 if ((off == -1 && total_bytes > len)
7071 || off >= total_bytes)
7072 return 0;
7073 if (off == -1)
7074 off = 0;
7075 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7077 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7079 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7080 bitpos += BITS_PER_UNIT)
7082 byte = (bitpos / BITS_PER_UNIT) & 3;
7083 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7085 if (UNITS_PER_WORD < 4)
7087 word = byte / UNITS_PER_WORD;
7088 if (WORDS_BIG_ENDIAN)
7089 word = (words - 1) - word;
7090 offset = word * UNITS_PER_WORD;
7091 if (BYTES_BIG_ENDIAN)
7092 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7093 else
7094 offset += byte % UNITS_PER_WORD;
7096 else
7098 offset = byte;
7099 if (BYTES_BIG_ENDIAN)
7101 /* Reverse bytes within each long, or within the entire float
7102 if it's smaller than a long (for HFmode). */
7103 offset = MIN (3, total_bytes - 1) - offset;
7104 gcc_assert (offset >= 0);
7107 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7108 if (offset >= off
7109 && offset - off < len)
7110 ptr[offset - off] = value;
7112 return MIN (len, total_bytes - off);
7115 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7116 specified by EXPR into the buffer PTR of length LEN bytes.
7117 Return the number of bytes placed in the buffer, or zero
7118 upon failure. */
7120 static int
7121 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7123 int rsize, isize;
7124 tree part;
7126 part = TREE_REALPART (expr);
7127 rsize = native_encode_expr (part, ptr, len, off);
7128 if (off == -1
7129 && rsize == 0)
7130 return 0;
7131 part = TREE_IMAGPART (expr);
7132 if (off != -1)
7133 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7134 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7135 if (off == -1
7136 && isize != rsize)
7137 return 0;
7138 return rsize + isize;
7142 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7143 specified by EXPR into the buffer PTR of length LEN bytes.
7144 Return the number of bytes placed in the buffer, or zero
7145 upon failure. */
7147 static int
7148 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7150 unsigned i, count;
7151 int size, offset;
7152 tree itype, elem;
7154 offset = 0;
7155 count = VECTOR_CST_NELTS (expr);
7156 itype = TREE_TYPE (TREE_TYPE (expr));
7157 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7158 for (i = 0; i < count; i++)
7160 if (off >= size)
7162 off -= size;
7163 continue;
7165 elem = VECTOR_CST_ELT (expr, i);
7166 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7167 if ((off == -1 && res != size)
7168 || res == 0)
7169 return 0;
7170 offset += res;
7171 if (offset >= len)
7172 return offset;
7173 if (off != -1)
7174 off = 0;
7176 return offset;
7180 /* Subroutine of native_encode_expr. Encode the STRING_CST
7181 specified by EXPR into the buffer PTR of length LEN bytes.
7182 Return the number of bytes placed in the buffer, or zero
7183 upon failure. */
7185 static int
7186 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7188 if (! can_native_encode_string_p (expr))
7189 return 0;
7191 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7192 if ((off == -1 && total_bytes > len)
7193 || off >= total_bytes)
7194 return 0;
7195 if (off == -1)
7196 off = 0;
7197 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7199 int written = 0;
7200 if (off < TREE_STRING_LENGTH (expr))
7202 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7203 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7205 memset (ptr + written, 0,
7206 MIN (total_bytes - written, len - written));
7208 else
7209 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7210 return MIN (total_bytes - off, len);
7214 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7215 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7216 buffer PTR of length LEN bytes. If OFF is not -1 then start
7217 the encoding at byte offset OFF and encode at most LEN bytes.
7218 Return the number of bytes placed in the buffer, or zero upon failure. */
7221 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7223 /* We don't support starting at negative offset and -1 is special. */
7224 if (off < -1)
7225 return 0;
7227 switch (TREE_CODE (expr))
7229 case INTEGER_CST:
7230 return native_encode_int (expr, ptr, len, off);
7232 case REAL_CST:
7233 return native_encode_real (expr, ptr, len, off);
7235 case FIXED_CST:
7236 return native_encode_fixed (expr, ptr, len, off);
7238 case COMPLEX_CST:
7239 return native_encode_complex (expr, ptr, len, off);
7241 case VECTOR_CST:
7242 return native_encode_vector (expr, ptr, len, off);
7244 case STRING_CST:
7245 return native_encode_string (expr, ptr, len, off);
7247 default:
7248 return 0;
7253 /* Subroutine of native_interpret_expr. Interpret the contents of
7254 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7255 If the buffer cannot be interpreted, return NULL_TREE. */
7257 static tree
7258 native_interpret_int (tree type, const unsigned char *ptr, int len)
7260 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7262 if (total_bytes > len
7263 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7264 return NULL_TREE;
7266 wide_int result = wi::from_buffer (ptr, total_bytes);
7268 return wide_int_to_tree (type, result);
7272 /* Subroutine of native_interpret_expr. Interpret the contents of
7273 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7274 If the buffer cannot be interpreted, return NULL_TREE. */
7276 static tree
7277 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7279 scalar_mode mode = SCALAR_TYPE_MODE (type);
7280 int total_bytes = GET_MODE_SIZE (mode);
7281 double_int result;
7282 FIXED_VALUE_TYPE fixed_value;
7284 if (total_bytes > len
7285 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7286 return NULL_TREE;
7288 result = double_int::from_buffer (ptr, total_bytes);
7289 fixed_value = fixed_from_double_int (result, mode);
7291 return build_fixed (type, fixed_value);
7295 /* Subroutine of native_interpret_expr. Interpret the contents of
7296 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7297 If the buffer cannot be interpreted, return NULL_TREE. */
7299 static tree
7300 native_interpret_real (tree type, const unsigned char *ptr, int len)
7302 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7303 int total_bytes = GET_MODE_SIZE (mode);
7304 unsigned char value;
7305 /* There are always 32 bits in each long, no matter the size of
7306 the hosts long. We handle floating point representations with
7307 up to 192 bits. */
7308 REAL_VALUE_TYPE r;
7309 long tmp[6];
7311 if (total_bytes > len || total_bytes > 24)
7312 return NULL_TREE;
7313 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7315 memset (tmp, 0, sizeof (tmp));
7316 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7317 bitpos += BITS_PER_UNIT)
7319 /* Both OFFSET and BYTE index within a long;
7320 bitpos indexes the whole float. */
7321 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7322 if (UNITS_PER_WORD < 4)
7324 int word = byte / UNITS_PER_WORD;
7325 if (WORDS_BIG_ENDIAN)
7326 word = (words - 1) - word;
7327 offset = word * UNITS_PER_WORD;
7328 if (BYTES_BIG_ENDIAN)
7329 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7330 else
7331 offset += byte % UNITS_PER_WORD;
7333 else
7335 offset = byte;
7336 if (BYTES_BIG_ENDIAN)
7338 /* Reverse bytes within each long, or within the entire float
7339 if it's smaller than a long (for HFmode). */
7340 offset = MIN (3, total_bytes - 1) - offset;
7341 gcc_assert (offset >= 0);
7344 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7346 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7349 real_from_target (&r, tmp, mode);
7350 return build_real (type, r);
7354 /* Subroutine of native_interpret_expr. Interpret the contents of
7355 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7356 If the buffer cannot be interpreted, return NULL_TREE. */
7358 static tree
7359 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7361 tree etype, rpart, ipart;
7362 int size;
7364 etype = TREE_TYPE (type);
7365 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7366 if (size * 2 > len)
7367 return NULL_TREE;
7368 rpart = native_interpret_expr (etype, ptr, size);
7369 if (!rpart)
7370 return NULL_TREE;
7371 ipart = native_interpret_expr (etype, ptr+size, size);
7372 if (!ipart)
7373 return NULL_TREE;
7374 return build_complex (type, rpart, ipart);
7378 /* Subroutine of native_interpret_expr. Interpret the contents of
7379 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7380 If the buffer cannot be interpreted, return NULL_TREE. */
7382 static tree
7383 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7385 tree etype, elem;
7386 int i, size, count;
7387 tree *elements;
7389 etype = TREE_TYPE (type);
7390 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7391 count = TYPE_VECTOR_SUBPARTS (type);
7392 if (size * count > len)
7393 return NULL_TREE;
7395 elements = XALLOCAVEC (tree, count);
7396 for (i = count - 1; i >= 0; i--)
7398 elem = native_interpret_expr (etype, ptr+(i*size), size);
7399 if (!elem)
7400 return NULL_TREE;
7401 elements[i] = elem;
7403 return build_vector (type, elements);
7407 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7408 the buffer PTR of length LEN as a constant of type TYPE. For
7409 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7410 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7411 return NULL_TREE. */
7413 tree
7414 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7416 switch (TREE_CODE (type))
7418 case INTEGER_TYPE:
7419 case ENUMERAL_TYPE:
7420 case BOOLEAN_TYPE:
7421 case POINTER_TYPE:
7422 case REFERENCE_TYPE:
7423 return native_interpret_int (type, ptr, len);
7425 case REAL_TYPE:
7426 return native_interpret_real (type, ptr, len);
7428 case FIXED_POINT_TYPE:
7429 return native_interpret_fixed (type, ptr, len);
7431 case COMPLEX_TYPE:
7432 return native_interpret_complex (type, ptr, len);
7434 case VECTOR_TYPE:
7435 return native_interpret_vector (type, ptr, len);
7437 default:
7438 return NULL_TREE;
7442 /* Returns true if we can interpret the contents of a native encoding
7443 as TYPE. */
7445 static bool
7446 can_native_interpret_type_p (tree type)
7448 switch (TREE_CODE (type))
7450 case INTEGER_TYPE:
7451 case ENUMERAL_TYPE:
7452 case BOOLEAN_TYPE:
7453 case POINTER_TYPE:
7454 case REFERENCE_TYPE:
7455 case FIXED_POINT_TYPE:
7456 case REAL_TYPE:
7457 case COMPLEX_TYPE:
7458 case VECTOR_TYPE:
7459 return true;
7460 default:
7461 return false;
7465 /* Return true iff a constant of type TYPE is accepted by
7466 native_encode_expr. */
7468 bool
7469 can_native_encode_type_p (tree type)
7471 switch (TREE_CODE (type))
7473 case INTEGER_TYPE:
7474 case REAL_TYPE:
7475 case FIXED_POINT_TYPE:
7476 case COMPLEX_TYPE:
7477 case VECTOR_TYPE:
7478 case POINTER_TYPE:
7479 return true;
7480 default:
7481 return false;
7485 /* Return true iff a STRING_CST S is accepted by
7486 native_encode_expr. */
7488 bool
7489 can_native_encode_string_p (const_tree expr)
7491 tree type = TREE_TYPE (expr);
7493 /* Wide-char strings are encoded in target byte-order so native
7494 encoding them is trivial. */
7495 if (BITS_PER_UNIT != CHAR_BIT
7496 || TREE_CODE (type) != ARRAY_TYPE
7497 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7498 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7499 return false;
7500 return true;
7503 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7504 TYPE at compile-time. If we're unable to perform the conversion
7505 return NULL_TREE. */
7507 static tree
7508 fold_view_convert_expr (tree type, tree expr)
7510 /* We support up to 512-bit values (for V8DFmode). */
7511 unsigned char buffer[64];
7512 int len;
7514 /* Check that the host and target are sane. */
7515 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7516 return NULL_TREE;
7518 len = native_encode_expr (expr, buffer, sizeof (buffer));
7519 if (len == 0)
7520 return NULL_TREE;
7522 return native_interpret_expr (type, buffer, len);
7525 /* Build an expression for the address of T. Folds away INDIRECT_REF
7526 to avoid confusing the gimplify process. */
7528 tree
7529 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7531 /* The size of the object is not relevant when talking about its address. */
7532 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7533 t = TREE_OPERAND (t, 0);
7535 if (TREE_CODE (t) == INDIRECT_REF)
7537 t = TREE_OPERAND (t, 0);
7539 if (TREE_TYPE (t) != ptrtype)
7540 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7542 else if (TREE_CODE (t) == MEM_REF
7543 && integer_zerop (TREE_OPERAND (t, 1)))
7544 return TREE_OPERAND (t, 0);
7545 else if (TREE_CODE (t) == MEM_REF
7546 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7547 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7548 TREE_OPERAND (t, 0),
7549 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7550 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7552 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7554 if (TREE_TYPE (t) != ptrtype)
7555 t = fold_convert_loc (loc, ptrtype, t);
7557 else
7558 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7560 return t;
7563 /* Build an expression for the address of T. */
7565 tree
7566 build_fold_addr_expr_loc (location_t loc, tree t)
7568 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7570 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7573 /* Fold a unary expression of code CODE and type TYPE with operand
7574 OP0. Return the folded expression if folding is successful.
7575 Otherwise, return NULL_TREE. */
7577 tree
7578 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7580 tree tem;
7581 tree arg0;
7582 enum tree_code_class kind = TREE_CODE_CLASS (code);
7584 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7585 && TREE_CODE_LENGTH (code) == 1);
7587 arg0 = op0;
7588 if (arg0)
7590 if (CONVERT_EXPR_CODE_P (code)
7591 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7593 /* Don't use STRIP_NOPS, because signedness of argument type
7594 matters. */
7595 STRIP_SIGN_NOPS (arg0);
7597 else
7599 /* Strip any conversions that don't change the mode. This
7600 is safe for every expression, except for a comparison
7601 expression because its signedness is derived from its
7602 operands.
7604 Note that this is done as an internal manipulation within
7605 the constant folder, in order to find the simplest
7606 representation of the arguments so that their form can be
7607 studied. In any cases, the appropriate type conversions
7608 should be put back in the tree that will get out of the
7609 constant folder. */
7610 STRIP_NOPS (arg0);
7613 if (CONSTANT_CLASS_P (arg0))
7615 tree tem = const_unop (code, type, arg0);
7616 if (tem)
7618 if (TREE_TYPE (tem) != type)
7619 tem = fold_convert_loc (loc, type, tem);
7620 return tem;
7625 tem = generic_simplify (loc, code, type, op0);
7626 if (tem)
7627 return tem;
7629 if (TREE_CODE_CLASS (code) == tcc_unary)
7631 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7632 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7633 fold_build1_loc (loc, code, type,
7634 fold_convert_loc (loc, TREE_TYPE (op0),
7635 TREE_OPERAND (arg0, 1))));
7636 else if (TREE_CODE (arg0) == COND_EXPR)
7638 tree arg01 = TREE_OPERAND (arg0, 1);
7639 tree arg02 = TREE_OPERAND (arg0, 2);
7640 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7641 arg01 = fold_build1_loc (loc, code, type,
7642 fold_convert_loc (loc,
7643 TREE_TYPE (op0), arg01));
7644 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7645 arg02 = fold_build1_loc (loc, code, type,
7646 fold_convert_loc (loc,
7647 TREE_TYPE (op0), arg02));
7648 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7649 arg01, arg02);
7651 /* If this was a conversion, and all we did was to move into
7652 inside the COND_EXPR, bring it back out. But leave it if
7653 it is a conversion from integer to integer and the
7654 result precision is no wider than a word since such a
7655 conversion is cheap and may be optimized away by combine,
7656 while it couldn't if it were outside the COND_EXPR. Then return
7657 so we don't get into an infinite recursion loop taking the
7658 conversion out and then back in. */
7660 if ((CONVERT_EXPR_CODE_P (code)
7661 || code == NON_LVALUE_EXPR)
7662 && TREE_CODE (tem) == COND_EXPR
7663 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7664 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7665 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7666 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7667 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7668 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7669 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7670 && (INTEGRAL_TYPE_P
7671 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7672 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7673 || flag_syntax_only))
7674 tem = build1_loc (loc, code, type,
7675 build3 (COND_EXPR,
7676 TREE_TYPE (TREE_OPERAND
7677 (TREE_OPERAND (tem, 1), 0)),
7678 TREE_OPERAND (tem, 0),
7679 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7680 TREE_OPERAND (TREE_OPERAND (tem, 2),
7681 0)));
7682 return tem;
7686 switch (code)
7688 case NON_LVALUE_EXPR:
7689 if (!maybe_lvalue_p (op0))
7690 return fold_convert_loc (loc, type, op0);
7691 return NULL_TREE;
7693 CASE_CONVERT:
7694 case FLOAT_EXPR:
7695 case FIX_TRUNC_EXPR:
7696 if (COMPARISON_CLASS_P (op0))
7698 /* If we have (type) (a CMP b) and type is an integral type, return
7699 new expression involving the new type. Canonicalize
7700 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7701 non-integral type.
7702 Do not fold the result as that would not simplify further, also
7703 folding again results in recursions. */
7704 if (TREE_CODE (type) == BOOLEAN_TYPE)
7705 return build2_loc (loc, TREE_CODE (op0), type,
7706 TREE_OPERAND (op0, 0),
7707 TREE_OPERAND (op0, 1));
7708 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7709 && TREE_CODE (type) != VECTOR_TYPE)
7710 return build3_loc (loc, COND_EXPR, type, op0,
7711 constant_boolean_node (true, type),
7712 constant_boolean_node (false, type));
7715 /* Handle (T *)&A.B.C for A being of type T and B and C
7716 living at offset zero. This occurs frequently in
7717 C++ upcasting and then accessing the base. */
7718 if (TREE_CODE (op0) == ADDR_EXPR
7719 && POINTER_TYPE_P (type)
7720 && handled_component_p (TREE_OPERAND (op0, 0)))
7722 HOST_WIDE_INT bitsize, bitpos;
7723 tree offset;
7724 machine_mode mode;
7725 int unsignedp, reversep, volatilep;
7726 tree base
7727 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7728 &offset, &mode, &unsignedp, &reversep,
7729 &volatilep);
7730 /* If the reference was to a (constant) zero offset, we can use
7731 the address of the base if it has the same base type
7732 as the result type and the pointer type is unqualified. */
7733 if (! offset && bitpos == 0
7734 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7735 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7736 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7737 return fold_convert_loc (loc, type,
7738 build_fold_addr_expr_loc (loc, base));
7741 if (TREE_CODE (op0) == MODIFY_EXPR
7742 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7743 /* Detect assigning a bitfield. */
7744 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7745 && DECL_BIT_FIELD
7746 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7748 /* Don't leave an assignment inside a conversion
7749 unless assigning a bitfield. */
7750 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7751 /* First do the assignment, then return converted constant. */
7752 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7753 TREE_NO_WARNING (tem) = 1;
7754 TREE_USED (tem) = 1;
7755 return tem;
7758 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7759 constants (if x has signed type, the sign bit cannot be set
7760 in c). This folds extension into the BIT_AND_EXPR.
7761 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7762 very likely don't have maximal range for their precision and this
7763 transformation effectively doesn't preserve non-maximal ranges. */
7764 if (TREE_CODE (type) == INTEGER_TYPE
7765 && TREE_CODE (op0) == BIT_AND_EXPR
7766 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7768 tree and_expr = op0;
7769 tree and0 = TREE_OPERAND (and_expr, 0);
7770 tree and1 = TREE_OPERAND (and_expr, 1);
7771 int change = 0;
7773 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7774 || (TYPE_PRECISION (type)
7775 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7776 change = 1;
7777 else if (TYPE_PRECISION (TREE_TYPE (and1))
7778 <= HOST_BITS_PER_WIDE_INT
7779 && tree_fits_uhwi_p (and1))
7781 unsigned HOST_WIDE_INT cst;
7783 cst = tree_to_uhwi (and1);
7784 cst &= HOST_WIDE_INT_M1U
7785 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7786 change = (cst == 0);
7787 if (change
7788 && !flag_syntax_only
7789 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7790 == ZERO_EXTEND))
7792 tree uns = unsigned_type_for (TREE_TYPE (and0));
7793 and0 = fold_convert_loc (loc, uns, and0);
7794 and1 = fold_convert_loc (loc, uns, and1);
7797 if (change)
7799 tem = force_fit_type (type, wi::to_widest (and1), 0,
7800 TREE_OVERFLOW (and1));
7801 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7802 fold_convert_loc (loc, type, and0), tem);
7806 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7807 cast (T1)X will fold away. We assume that this happens when X itself
7808 is a cast. */
7809 if (POINTER_TYPE_P (type)
7810 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7811 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7813 tree arg00 = TREE_OPERAND (arg0, 0);
7814 tree arg01 = TREE_OPERAND (arg0, 1);
7816 return fold_build_pointer_plus_loc
7817 (loc, fold_convert_loc (loc, type, arg00), arg01);
7820 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7821 of the same precision, and X is an integer type not narrower than
7822 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7823 if (INTEGRAL_TYPE_P (type)
7824 && TREE_CODE (op0) == BIT_NOT_EXPR
7825 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7826 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7827 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7829 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7830 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7831 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7832 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7833 fold_convert_loc (loc, type, tem));
7836 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7837 type of X and Y (integer types only). */
7838 if (INTEGRAL_TYPE_P (type)
7839 && TREE_CODE (op0) == MULT_EXPR
7840 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7841 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7843 /* Be careful not to introduce new overflows. */
7844 tree mult_type;
7845 if (TYPE_OVERFLOW_WRAPS (type))
7846 mult_type = type;
7847 else
7848 mult_type = unsigned_type_for (type);
7850 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7852 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7853 fold_convert_loc (loc, mult_type,
7854 TREE_OPERAND (op0, 0)),
7855 fold_convert_loc (loc, mult_type,
7856 TREE_OPERAND (op0, 1)));
7857 return fold_convert_loc (loc, type, tem);
7861 return NULL_TREE;
7863 case VIEW_CONVERT_EXPR:
7864 if (TREE_CODE (op0) == MEM_REF)
7866 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7867 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7868 tem = fold_build2_loc (loc, MEM_REF, type,
7869 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7870 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7871 return tem;
7874 return NULL_TREE;
7876 case NEGATE_EXPR:
7877 tem = fold_negate_expr (loc, arg0);
7878 if (tem)
7879 return fold_convert_loc (loc, type, tem);
7880 return NULL_TREE;
7882 case ABS_EXPR:
7883 /* Convert fabs((double)float) into (double)fabsf(float). */
7884 if (TREE_CODE (arg0) == NOP_EXPR
7885 && TREE_CODE (type) == REAL_TYPE)
7887 tree targ0 = strip_float_extensions (arg0);
7888 if (targ0 != arg0)
7889 return fold_convert_loc (loc, type,
7890 fold_build1_loc (loc, ABS_EXPR,
7891 TREE_TYPE (targ0),
7892 targ0));
7894 return NULL_TREE;
7896 case BIT_NOT_EXPR:
7897 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7898 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7899 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7900 fold_convert_loc (loc, type,
7901 TREE_OPERAND (arg0, 0)))))
7902 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7903 fold_convert_loc (loc, type,
7904 TREE_OPERAND (arg0, 1)));
7905 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7906 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7907 fold_convert_loc (loc, type,
7908 TREE_OPERAND (arg0, 1)))))
7909 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7910 fold_convert_loc (loc, type,
7911 TREE_OPERAND (arg0, 0)), tem);
7913 return NULL_TREE;
7915 case TRUTH_NOT_EXPR:
7916 /* Note that the operand of this must be an int
7917 and its values must be 0 or 1.
7918 ("true" is a fixed value perhaps depending on the language,
7919 but we don't handle values other than 1 correctly yet.) */
7920 tem = fold_truth_not_expr (loc, arg0);
7921 if (!tem)
7922 return NULL_TREE;
7923 return fold_convert_loc (loc, type, tem);
7925 case INDIRECT_REF:
7926 /* Fold *&X to X if X is an lvalue. */
7927 if (TREE_CODE (op0) == ADDR_EXPR)
7929 tree op00 = TREE_OPERAND (op0, 0);
7930 if ((VAR_P (op00)
7931 || TREE_CODE (op00) == PARM_DECL
7932 || TREE_CODE (op00) == RESULT_DECL)
7933 && !TREE_READONLY (op00))
7934 return op00;
7936 return NULL_TREE;
7938 default:
7939 return NULL_TREE;
7940 } /* switch (code) */
7944 /* If the operation was a conversion do _not_ mark a resulting constant
7945 with TREE_OVERFLOW if the original constant was not. These conversions
7946 have implementation defined behavior and retaining the TREE_OVERFLOW
7947 flag here would confuse later passes such as VRP. */
7948 tree
7949 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7950 tree type, tree op0)
7952 tree res = fold_unary_loc (loc, code, type, op0);
7953 if (res
7954 && TREE_CODE (res) == INTEGER_CST
7955 && TREE_CODE (op0) == INTEGER_CST
7956 && CONVERT_EXPR_CODE_P (code))
7957 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7959 return res;
7962 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7963 operands OP0 and OP1. LOC is the location of the resulting expression.
7964 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7965 Return the folded expression if folding is successful. Otherwise,
7966 return NULL_TREE. */
7967 static tree
7968 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7969 tree arg0, tree arg1, tree op0, tree op1)
7971 tree tem;
7973 /* We only do these simplifications if we are optimizing. */
7974 if (!optimize)
7975 return NULL_TREE;
7977 /* Check for things like (A || B) && (A || C). We can convert this
7978 to A || (B && C). Note that either operator can be any of the four
7979 truth and/or operations and the transformation will still be
7980 valid. Also note that we only care about order for the
7981 ANDIF and ORIF operators. If B contains side effects, this
7982 might change the truth-value of A. */
7983 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7984 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7985 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7986 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7987 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7988 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7990 tree a00 = TREE_OPERAND (arg0, 0);
7991 tree a01 = TREE_OPERAND (arg0, 1);
7992 tree a10 = TREE_OPERAND (arg1, 0);
7993 tree a11 = TREE_OPERAND (arg1, 1);
7994 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7995 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7996 && (code == TRUTH_AND_EXPR
7997 || code == TRUTH_OR_EXPR));
7999 if (operand_equal_p (a00, a10, 0))
8000 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8001 fold_build2_loc (loc, code, type, a01, a11));
8002 else if (commutative && operand_equal_p (a00, a11, 0))
8003 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8004 fold_build2_loc (loc, code, type, a01, a10));
8005 else if (commutative && operand_equal_p (a01, a10, 0))
8006 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8007 fold_build2_loc (loc, code, type, a00, a11));
8009 /* This case if tricky because we must either have commutative
8010 operators or else A10 must not have side-effects. */
8012 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8013 && operand_equal_p (a01, a11, 0))
8014 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8015 fold_build2_loc (loc, code, type, a00, a10),
8016 a01);
8019 /* See if we can build a range comparison. */
8020 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8021 return tem;
8023 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8024 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8026 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8027 if (tem)
8028 return fold_build2_loc (loc, code, type, tem, arg1);
8031 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8032 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8034 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8035 if (tem)
8036 return fold_build2_loc (loc, code, type, arg0, tem);
8039 /* Check for the possibility of merging component references. If our
8040 lhs is another similar operation, try to merge its rhs with our
8041 rhs. Then try to merge our lhs and rhs. */
8042 if (TREE_CODE (arg0) == code
8043 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8044 TREE_OPERAND (arg0, 1), arg1)))
8045 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8047 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8048 return tem;
8050 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8051 && !flag_sanitize_coverage
8052 && (code == TRUTH_AND_EXPR
8053 || code == TRUTH_ANDIF_EXPR
8054 || code == TRUTH_OR_EXPR
8055 || code == TRUTH_ORIF_EXPR))
8057 enum tree_code ncode, icode;
8059 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8060 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8061 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8063 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8064 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8065 We don't want to pack more than two leafs to a non-IF AND/OR
8066 expression.
8067 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8068 equal to IF-CODE, then we don't want to add right-hand operand.
8069 If the inner right-hand side of left-hand operand has
8070 side-effects, or isn't simple, then we can't add to it,
8071 as otherwise we might destroy if-sequence. */
8072 if (TREE_CODE (arg0) == icode
8073 && simple_operand_p_2 (arg1)
8074 /* Needed for sequence points to handle trappings, and
8075 side-effects. */
8076 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8078 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8079 arg1);
8080 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8081 tem);
8083 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8084 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8085 else if (TREE_CODE (arg1) == icode
8086 && simple_operand_p_2 (arg0)
8087 /* Needed for sequence points to handle trappings, and
8088 side-effects. */
8089 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8091 tem = fold_build2_loc (loc, ncode, type,
8092 arg0, TREE_OPERAND (arg1, 0));
8093 return fold_build2_loc (loc, icode, type, tem,
8094 TREE_OPERAND (arg1, 1));
8096 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8097 into (A OR B).
8098 For sequence point consistancy, we need to check for trapping,
8099 and side-effects. */
8100 else if (code == icode && simple_operand_p_2 (arg0)
8101 && simple_operand_p_2 (arg1))
8102 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8105 return NULL_TREE;
8108 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8109 by changing CODE to reduce the magnitude of constants involved in
8110 ARG0 of the comparison.
8111 Returns a canonicalized comparison tree if a simplification was
8112 possible, otherwise returns NULL_TREE.
8113 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8114 valid if signed overflow is undefined. */
8116 static tree
8117 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8118 tree arg0, tree arg1,
8119 bool *strict_overflow_p)
8121 enum tree_code code0 = TREE_CODE (arg0);
8122 tree t, cst0 = NULL_TREE;
8123 int sgn0;
8125 /* Match A +- CST code arg1. We can change this only if overflow
8126 is undefined. */
8127 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8128 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8129 /* In principle pointers also have undefined overflow behavior,
8130 but that causes problems elsewhere. */
8131 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8132 && (code0 == MINUS_EXPR
8133 || code0 == PLUS_EXPR)
8134 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8135 return NULL_TREE;
8137 /* Identify the constant in arg0 and its sign. */
8138 cst0 = TREE_OPERAND (arg0, 1);
8139 sgn0 = tree_int_cst_sgn (cst0);
8141 /* Overflowed constants and zero will cause problems. */
8142 if (integer_zerop (cst0)
8143 || TREE_OVERFLOW (cst0))
8144 return NULL_TREE;
8146 /* See if we can reduce the magnitude of the constant in
8147 arg0 by changing the comparison code. */
8148 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8149 if (code == LT_EXPR
8150 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8151 code = LE_EXPR;
8152 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8153 else if (code == GT_EXPR
8154 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8155 code = GE_EXPR;
8156 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8157 else if (code == LE_EXPR
8158 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8159 code = LT_EXPR;
8160 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8161 else if (code == GE_EXPR
8162 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8163 code = GT_EXPR;
8164 else
8165 return NULL_TREE;
8166 *strict_overflow_p = true;
8168 /* Now build the constant reduced in magnitude. But not if that
8169 would produce one outside of its types range. */
8170 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8171 && ((sgn0 == 1
8172 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8173 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8174 || (sgn0 == -1
8175 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8176 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8177 return NULL_TREE;
8179 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8180 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8181 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8182 t = fold_convert (TREE_TYPE (arg1), t);
8184 return fold_build2_loc (loc, code, type, t, arg1);
8187 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8188 overflow further. Try to decrease the magnitude of constants involved
8189 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8190 and put sole constants at the second argument position.
8191 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8193 static tree
8194 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8195 tree arg0, tree arg1)
8197 tree t;
8198 bool strict_overflow_p;
8199 const char * const warnmsg = G_("assuming signed overflow does not occur "
8200 "when reducing constant in comparison");
8202 /* Try canonicalization by simplifying arg0. */
8203 strict_overflow_p = false;
8204 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8205 &strict_overflow_p);
8206 if (t)
8208 if (strict_overflow_p)
8209 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8210 return t;
8213 /* Try canonicalization by simplifying arg1 using the swapped
8214 comparison. */
8215 code = swap_tree_comparison (code);
8216 strict_overflow_p = false;
8217 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8218 &strict_overflow_p);
8219 if (t && strict_overflow_p)
8220 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8221 return t;
8224 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8225 space. This is used to avoid issuing overflow warnings for
8226 expressions like &p->x which can not wrap. */
8228 static bool
8229 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8231 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8232 return true;
8234 if (bitpos < 0)
8235 return true;
8237 wide_int wi_offset;
8238 int precision = TYPE_PRECISION (TREE_TYPE (base));
8239 if (offset == NULL_TREE)
8240 wi_offset = wi::zero (precision);
8241 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8242 return true;
8243 else
8244 wi_offset = offset;
8246 bool overflow;
8247 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8248 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8249 if (overflow)
8250 return true;
8252 if (!wi::fits_uhwi_p (total))
8253 return true;
8255 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8256 if (size <= 0)
8257 return true;
8259 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8260 array. */
8261 if (TREE_CODE (base) == ADDR_EXPR)
8263 HOST_WIDE_INT base_size;
8265 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8266 if (base_size > 0 && size < base_size)
8267 size = base_size;
8270 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8273 /* Return a positive integer when the symbol DECL is known to have
8274 a nonzero address, zero when it's known not to (e.g., it's a weak
8275 symbol), and a negative integer when the symbol is not yet in the
8276 symbol table and so whether or not its address is zero is unknown.
8277 For function local objects always return positive integer. */
8278 static int
8279 maybe_nonzero_address (tree decl)
8281 if (DECL_P (decl) && decl_in_symtab_p (decl))
8282 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8283 return symbol->nonzero_address ();
8285 /* Function local objects are never NULL. */
8286 if (DECL_P (decl)
8287 && (DECL_CONTEXT (decl)
8288 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8289 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8290 return 1;
8292 return -1;
8295 /* Subroutine of fold_binary. This routine performs all of the
8296 transformations that are common to the equality/inequality
8297 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8298 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8299 fold_binary should call fold_binary. Fold a comparison with
8300 tree code CODE and type TYPE with operands OP0 and OP1. Return
8301 the folded comparison or NULL_TREE. */
8303 static tree
8304 fold_comparison (location_t loc, enum tree_code code, tree type,
8305 tree op0, tree op1)
8307 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8308 tree arg0, arg1, tem;
8310 arg0 = op0;
8311 arg1 = op1;
8313 STRIP_SIGN_NOPS (arg0);
8314 STRIP_SIGN_NOPS (arg1);
8316 /* For comparisons of pointers we can decompose it to a compile time
8317 comparison of the base objects and the offsets into the object.
8318 This requires at least one operand being an ADDR_EXPR or a
8319 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8320 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8321 && (TREE_CODE (arg0) == ADDR_EXPR
8322 || TREE_CODE (arg1) == ADDR_EXPR
8323 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8324 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8326 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8327 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8328 machine_mode mode;
8329 int volatilep, reversep, unsignedp;
8330 bool indirect_base0 = false, indirect_base1 = false;
8332 /* Get base and offset for the access. Strip ADDR_EXPR for
8333 get_inner_reference, but put it back by stripping INDIRECT_REF
8334 off the base object if possible. indirect_baseN will be true
8335 if baseN is not an address but refers to the object itself. */
8336 base0 = arg0;
8337 if (TREE_CODE (arg0) == ADDR_EXPR)
8339 base0
8340 = get_inner_reference (TREE_OPERAND (arg0, 0),
8341 &bitsize, &bitpos0, &offset0, &mode,
8342 &unsignedp, &reversep, &volatilep);
8343 if (TREE_CODE (base0) == INDIRECT_REF)
8344 base0 = TREE_OPERAND (base0, 0);
8345 else
8346 indirect_base0 = true;
8348 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8350 base0 = TREE_OPERAND (arg0, 0);
8351 STRIP_SIGN_NOPS (base0);
8352 if (TREE_CODE (base0) == ADDR_EXPR)
8354 base0
8355 = get_inner_reference (TREE_OPERAND (base0, 0),
8356 &bitsize, &bitpos0, &offset0, &mode,
8357 &unsignedp, &reversep, &volatilep);
8358 if (TREE_CODE (base0) == INDIRECT_REF)
8359 base0 = TREE_OPERAND (base0, 0);
8360 else
8361 indirect_base0 = true;
8363 if (offset0 == NULL_TREE || integer_zerop (offset0))
8364 offset0 = TREE_OPERAND (arg0, 1);
8365 else
8366 offset0 = size_binop (PLUS_EXPR, offset0,
8367 TREE_OPERAND (arg0, 1));
8368 if (TREE_CODE (offset0) == INTEGER_CST)
8370 offset_int tem = wi::sext (wi::to_offset (offset0),
8371 TYPE_PRECISION (sizetype));
8372 tem <<= LOG2_BITS_PER_UNIT;
8373 tem += bitpos0;
8374 if (wi::fits_shwi_p (tem))
8376 bitpos0 = tem.to_shwi ();
8377 offset0 = NULL_TREE;
8382 base1 = arg1;
8383 if (TREE_CODE (arg1) == ADDR_EXPR)
8385 base1
8386 = get_inner_reference (TREE_OPERAND (arg1, 0),
8387 &bitsize, &bitpos1, &offset1, &mode,
8388 &unsignedp, &reversep, &volatilep);
8389 if (TREE_CODE (base1) == INDIRECT_REF)
8390 base1 = TREE_OPERAND (base1, 0);
8391 else
8392 indirect_base1 = true;
8394 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8396 base1 = TREE_OPERAND (arg1, 0);
8397 STRIP_SIGN_NOPS (base1);
8398 if (TREE_CODE (base1) == ADDR_EXPR)
8400 base1
8401 = get_inner_reference (TREE_OPERAND (base1, 0),
8402 &bitsize, &bitpos1, &offset1, &mode,
8403 &unsignedp, &reversep, &volatilep);
8404 if (TREE_CODE (base1) == INDIRECT_REF)
8405 base1 = TREE_OPERAND (base1, 0);
8406 else
8407 indirect_base1 = true;
8409 if (offset1 == NULL_TREE || integer_zerop (offset1))
8410 offset1 = TREE_OPERAND (arg1, 1);
8411 else
8412 offset1 = size_binop (PLUS_EXPR, offset1,
8413 TREE_OPERAND (arg1, 1));
8414 if (TREE_CODE (offset1) == INTEGER_CST)
8416 offset_int tem = wi::sext (wi::to_offset (offset1),
8417 TYPE_PRECISION (sizetype));
8418 tem <<= LOG2_BITS_PER_UNIT;
8419 tem += bitpos1;
8420 if (wi::fits_shwi_p (tem))
8422 bitpos1 = tem.to_shwi ();
8423 offset1 = NULL_TREE;
8428 /* If we have equivalent bases we might be able to simplify. */
8429 if (indirect_base0 == indirect_base1
8430 && operand_equal_p (base0, base1,
8431 indirect_base0 ? OEP_ADDRESS_OF : 0))
8433 /* We can fold this expression to a constant if the non-constant
8434 offset parts are equal. */
8435 if (offset0 == offset1
8436 || (offset0 && offset1
8437 && operand_equal_p (offset0, offset1, 0)))
8439 if (!equality_code
8440 && bitpos0 != bitpos1
8441 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8442 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8443 fold_overflow_warning (("assuming pointer wraparound does not "
8444 "occur when comparing P +- C1 with "
8445 "P +- C2"),
8446 WARN_STRICT_OVERFLOW_CONDITIONAL);
8448 switch (code)
8450 case EQ_EXPR:
8451 return constant_boolean_node (bitpos0 == bitpos1, type);
8452 case NE_EXPR:
8453 return constant_boolean_node (bitpos0 != bitpos1, type);
8454 case LT_EXPR:
8455 return constant_boolean_node (bitpos0 < bitpos1, type);
8456 case LE_EXPR:
8457 return constant_boolean_node (bitpos0 <= bitpos1, type);
8458 case GE_EXPR:
8459 return constant_boolean_node (bitpos0 >= bitpos1, type);
8460 case GT_EXPR:
8461 return constant_boolean_node (bitpos0 > bitpos1, type);
8462 default:;
8465 /* We can simplify the comparison to a comparison of the variable
8466 offset parts if the constant offset parts are equal.
8467 Be careful to use signed sizetype here because otherwise we
8468 mess with array offsets in the wrong way. This is possible
8469 because pointer arithmetic is restricted to retain within an
8470 object and overflow on pointer differences is undefined as of
8471 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8472 else if (bitpos0 == bitpos1)
8474 /* By converting to signed sizetype we cover middle-end pointer
8475 arithmetic which operates on unsigned pointer types of size
8476 type size and ARRAY_REF offsets which are properly sign or
8477 zero extended from their type in case it is narrower than
8478 sizetype. */
8479 if (offset0 == NULL_TREE)
8480 offset0 = build_int_cst (ssizetype, 0);
8481 else
8482 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8483 if (offset1 == NULL_TREE)
8484 offset1 = build_int_cst (ssizetype, 0);
8485 else
8486 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8488 if (!equality_code
8489 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8490 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8491 fold_overflow_warning (("assuming pointer wraparound does not "
8492 "occur when comparing P +- C1 with "
8493 "P +- C2"),
8494 WARN_STRICT_OVERFLOW_COMPARISON);
8496 return fold_build2_loc (loc, code, type, offset0, offset1);
8499 /* For equal offsets we can simplify to a comparison of the
8500 base addresses. */
8501 else if (bitpos0 == bitpos1
8502 && (indirect_base0
8503 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8504 && (indirect_base1
8505 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8506 && ((offset0 == offset1)
8507 || (offset0 && offset1
8508 && operand_equal_p (offset0, offset1, 0))))
8510 if (indirect_base0)
8511 base0 = build_fold_addr_expr_loc (loc, base0);
8512 if (indirect_base1)
8513 base1 = build_fold_addr_expr_loc (loc, base1);
8514 return fold_build2_loc (loc, code, type, base0, base1);
8516 /* Comparison between an ordinary (non-weak) symbol and a null
8517 pointer can be eliminated since such symbols must have a non
8518 null address. In C, relational expressions between pointers
8519 to objects and null pointers are undefined. The results
8520 below follow the C++ rules with the additional property that
8521 every object pointer compares greater than a null pointer.
8523 else if (((DECL_P (base0)
8524 && maybe_nonzero_address (base0) > 0
8525 /* Avoid folding references to struct members at offset 0 to
8526 prevent tests like '&ptr->firstmember == 0' from getting
8527 eliminated. When ptr is null, although the -> expression
8528 is strictly speaking invalid, GCC retains it as a matter
8529 of QoI. See PR c/44555. */
8530 && (offset0 == NULL_TREE && bitpos0 != 0))
8531 || CONSTANT_CLASS_P (base0))
8532 && indirect_base0
8533 /* The caller guarantees that when one of the arguments is
8534 constant (i.e., null in this case) it is second. */
8535 && integer_zerop (arg1))
8537 switch (code)
8539 case EQ_EXPR:
8540 case LE_EXPR:
8541 case LT_EXPR:
8542 return constant_boolean_node (false, type);
8543 case GE_EXPR:
8544 case GT_EXPR:
8545 case NE_EXPR:
8546 return constant_boolean_node (true, type);
8547 default:
8548 gcc_unreachable ();
8553 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8554 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8555 the resulting offset is smaller in absolute value than the
8556 original one and has the same sign. */
8557 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8558 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8559 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8560 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8561 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8562 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8563 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8564 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8566 tree const1 = TREE_OPERAND (arg0, 1);
8567 tree const2 = TREE_OPERAND (arg1, 1);
8568 tree variable1 = TREE_OPERAND (arg0, 0);
8569 tree variable2 = TREE_OPERAND (arg1, 0);
8570 tree cst;
8571 const char * const warnmsg = G_("assuming signed overflow does not "
8572 "occur when combining constants around "
8573 "a comparison");
8575 /* Put the constant on the side where it doesn't overflow and is
8576 of lower absolute value and of same sign than before. */
8577 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8578 ? MINUS_EXPR : PLUS_EXPR,
8579 const2, const1);
8580 if (!TREE_OVERFLOW (cst)
8581 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8582 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8584 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8585 return fold_build2_loc (loc, code, type,
8586 variable1,
8587 fold_build2_loc (loc, TREE_CODE (arg1),
8588 TREE_TYPE (arg1),
8589 variable2, cst));
8592 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8593 ? MINUS_EXPR : PLUS_EXPR,
8594 const1, const2);
8595 if (!TREE_OVERFLOW (cst)
8596 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8597 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8599 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8600 return fold_build2_loc (loc, code, type,
8601 fold_build2_loc (loc, TREE_CODE (arg0),
8602 TREE_TYPE (arg0),
8603 variable1, cst),
8604 variable2);
8608 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8609 if (tem)
8610 return tem;
8612 /* If we are comparing an expression that just has comparisons
8613 of two integer values, arithmetic expressions of those comparisons,
8614 and constants, we can simplify it. There are only three cases
8615 to check: the two values can either be equal, the first can be
8616 greater, or the second can be greater. Fold the expression for
8617 those three values. Since each value must be 0 or 1, we have
8618 eight possibilities, each of which corresponds to the constant 0
8619 or 1 or one of the six possible comparisons.
8621 This handles common cases like (a > b) == 0 but also handles
8622 expressions like ((x > y) - (y > x)) > 0, which supposedly
8623 occur in macroized code. */
8625 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8627 tree cval1 = 0, cval2 = 0;
8628 int save_p = 0;
8630 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8631 /* Don't handle degenerate cases here; they should already
8632 have been handled anyway. */
8633 && cval1 != 0 && cval2 != 0
8634 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8635 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8636 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8637 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8638 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8639 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8640 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8642 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8643 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8645 /* We can't just pass T to eval_subst in case cval1 or cval2
8646 was the same as ARG1. */
8648 tree high_result
8649 = fold_build2_loc (loc, code, type,
8650 eval_subst (loc, arg0, cval1, maxval,
8651 cval2, minval),
8652 arg1);
8653 tree equal_result
8654 = fold_build2_loc (loc, code, type,
8655 eval_subst (loc, arg0, cval1, maxval,
8656 cval2, maxval),
8657 arg1);
8658 tree low_result
8659 = fold_build2_loc (loc, code, type,
8660 eval_subst (loc, arg0, cval1, minval,
8661 cval2, maxval),
8662 arg1);
8664 /* All three of these results should be 0 or 1. Confirm they are.
8665 Then use those values to select the proper code to use. */
8667 if (TREE_CODE (high_result) == INTEGER_CST
8668 && TREE_CODE (equal_result) == INTEGER_CST
8669 && TREE_CODE (low_result) == INTEGER_CST)
8671 /* Make a 3-bit mask with the high-order bit being the
8672 value for `>', the next for '=', and the low for '<'. */
8673 switch ((integer_onep (high_result) * 4)
8674 + (integer_onep (equal_result) * 2)
8675 + integer_onep (low_result))
8677 case 0:
8678 /* Always false. */
8679 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8680 case 1:
8681 code = LT_EXPR;
8682 break;
8683 case 2:
8684 code = EQ_EXPR;
8685 break;
8686 case 3:
8687 code = LE_EXPR;
8688 break;
8689 case 4:
8690 code = GT_EXPR;
8691 break;
8692 case 5:
8693 code = NE_EXPR;
8694 break;
8695 case 6:
8696 code = GE_EXPR;
8697 break;
8698 case 7:
8699 /* Always true. */
8700 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8703 if (save_p)
8705 tem = save_expr (build2 (code, type, cval1, cval2));
8706 protected_set_expr_location (tem, loc);
8707 return tem;
8709 return fold_build2_loc (loc, code, type, cval1, cval2);
8714 return NULL_TREE;
8718 /* Subroutine of fold_binary. Optimize complex multiplications of the
8719 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8720 argument EXPR represents the expression "z" of type TYPE. */
8722 static tree
8723 fold_mult_zconjz (location_t loc, tree type, tree expr)
8725 tree itype = TREE_TYPE (type);
8726 tree rpart, ipart, tem;
8728 if (TREE_CODE (expr) == COMPLEX_EXPR)
8730 rpart = TREE_OPERAND (expr, 0);
8731 ipart = TREE_OPERAND (expr, 1);
8733 else if (TREE_CODE (expr) == COMPLEX_CST)
8735 rpart = TREE_REALPART (expr);
8736 ipart = TREE_IMAGPART (expr);
8738 else
8740 expr = save_expr (expr);
8741 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8742 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8745 rpart = save_expr (rpart);
8746 ipart = save_expr (ipart);
8747 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8748 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8749 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8750 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8751 build_zero_cst (itype));
8755 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8756 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8758 static bool
8759 vec_cst_ctor_to_array (tree arg, tree *elts)
8761 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8763 if (TREE_CODE (arg) == VECTOR_CST)
8765 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8766 elts[i] = VECTOR_CST_ELT (arg, i);
8768 else if (TREE_CODE (arg) == CONSTRUCTOR)
8770 constructor_elt *elt;
8772 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8773 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8774 return false;
8775 else
8776 elts[i] = elt->value;
8778 else
8779 return false;
8780 for (; i < nelts; i++)
8781 elts[i]
8782 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8783 return true;
8786 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8787 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8788 NULL_TREE otherwise. */
8790 static tree
8791 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8793 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8794 tree *elts;
8795 bool need_ctor = false;
8797 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8798 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8799 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8800 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8801 return NULL_TREE;
8803 elts = XALLOCAVEC (tree, nelts * 3);
8804 if (!vec_cst_ctor_to_array (arg0, elts)
8805 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8806 return NULL_TREE;
8808 for (i = 0; i < nelts; i++)
8810 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8811 need_ctor = true;
8812 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8815 if (need_ctor)
8817 vec<constructor_elt, va_gc> *v;
8818 vec_alloc (v, nelts);
8819 for (i = 0; i < nelts; i++)
8820 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8821 return build_constructor (type, v);
8823 else
8824 return build_vector (type, &elts[2 * nelts]);
8827 /* Try to fold a pointer difference of type TYPE two address expressions of
8828 array references AREF0 and AREF1 using location LOC. Return a
8829 simplified expression for the difference or NULL_TREE. */
8831 static tree
8832 fold_addr_of_array_ref_difference (location_t loc, tree type,
8833 tree aref0, tree aref1)
8835 tree base0 = TREE_OPERAND (aref0, 0);
8836 tree base1 = TREE_OPERAND (aref1, 0);
8837 tree base_offset = build_int_cst (type, 0);
8839 /* If the bases are array references as well, recurse. If the bases
8840 are pointer indirections compute the difference of the pointers.
8841 If the bases are equal, we are set. */
8842 if ((TREE_CODE (base0) == ARRAY_REF
8843 && TREE_CODE (base1) == ARRAY_REF
8844 && (base_offset
8845 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8846 || (INDIRECT_REF_P (base0)
8847 && INDIRECT_REF_P (base1)
8848 && (base_offset
8849 = fold_binary_loc (loc, MINUS_EXPR, type,
8850 fold_convert (type, TREE_OPERAND (base0, 0)),
8851 fold_convert (type,
8852 TREE_OPERAND (base1, 0)))))
8853 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8855 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8856 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8857 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8858 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
8859 return fold_build2_loc (loc, PLUS_EXPR, type,
8860 base_offset,
8861 fold_build2_loc (loc, MULT_EXPR, type,
8862 diff, esz));
8864 return NULL_TREE;
8867 /* If the real or vector real constant CST of type TYPE has an exact
8868 inverse, return it, else return NULL. */
8870 tree
8871 exact_inverse (tree type, tree cst)
8873 REAL_VALUE_TYPE r;
8874 tree unit_type, *elts;
8875 machine_mode mode;
8876 unsigned vec_nelts, i;
8878 switch (TREE_CODE (cst))
8880 case REAL_CST:
8881 r = TREE_REAL_CST (cst);
8883 if (exact_real_inverse (TYPE_MODE (type), &r))
8884 return build_real (type, r);
8886 return NULL_TREE;
8888 case VECTOR_CST:
8889 vec_nelts = VECTOR_CST_NELTS (cst);
8890 elts = XALLOCAVEC (tree, vec_nelts);
8891 unit_type = TREE_TYPE (type);
8892 mode = TYPE_MODE (unit_type);
8894 for (i = 0; i < vec_nelts; i++)
8896 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8897 if (!exact_real_inverse (mode, &r))
8898 return NULL_TREE;
8899 elts[i] = build_real (unit_type, r);
8902 return build_vector (type, elts);
8904 default:
8905 return NULL_TREE;
8909 /* Mask out the tz least significant bits of X of type TYPE where
8910 tz is the number of trailing zeroes in Y. */
8911 static wide_int
8912 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8914 int tz = wi::ctz (y);
8915 if (tz > 0)
8916 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8917 return x;
8920 /* Return true when T is an address and is known to be nonzero.
8921 For floating point we further ensure that T is not denormal.
8922 Similar logic is present in nonzero_address in rtlanal.h.
8924 If the return value is based on the assumption that signed overflow
8925 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8926 change *STRICT_OVERFLOW_P. */
8928 static bool
8929 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8931 tree type = TREE_TYPE (t);
8932 enum tree_code code;
8934 /* Doing something useful for floating point would need more work. */
8935 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8936 return false;
8938 code = TREE_CODE (t);
8939 switch (TREE_CODE_CLASS (code))
8941 case tcc_unary:
8942 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8943 strict_overflow_p);
8944 case tcc_binary:
8945 case tcc_comparison:
8946 return tree_binary_nonzero_warnv_p (code, type,
8947 TREE_OPERAND (t, 0),
8948 TREE_OPERAND (t, 1),
8949 strict_overflow_p);
8950 case tcc_constant:
8951 case tcc_declaration:
8952 case tcc_reference:
8953 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8955 default:
8956 break;
8959 switch (code)
8961 case TRUTH_NOT_EXPR:
8962 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8963 strict_overflow_p);
8965 case TRUTH_AND_EXPR:
8966 case TRUTH_OR_EXPR:
8967 case TRUTH_XOR_EXPR:
8968 return tree_binary_nonzero_warnv_p (code, type,
8969 TREE_OPERAND (t, 0),
8970 TREE_OPERAND (t, 1),
8971 strict_overflow_p);
8973 case COND_EXPR:
8974 case CONSTRUCTOR:
8975 case OBJ_TYPE_REF:
8976 case ASSERT_EXPR:
8977 case ADDR_EXPR:
8978 case WITH_SIZE_EXPR:
8979 case SSA_NAME:
8980 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8982 case COMPOUND_EXPR:
8983 case MODIFY_EXPR:
8984 case BIND_EXPR:
8985 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
8986 strict_overflow_p);
8988 case SAVE_EXPR:
8989 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
8990 strict_overflow_p);
8992 case CALL_EXPR:
8994 tree fndecl = get_callee_fndecl (t);
8995 if (!fndecl) return false;
8996 if (flag_delete_null_pointer_checks && !flag_check_new
8997 && DECL_IS_OPERATOR_NEW (fndecl)
8998 && !TREE_NOTHROW (fndecl))
8999 return true;
9000 if (flag_delete_null_pointer_checks
9001 && lookup_attribute ("returns_nonnull",
9002 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9003 return true;
9004 return alloca_call_p (t);
9007 default:
9008 break;
9010 return false;
9013 /* Return true when T is an address and is known to be nonzero.
9014 Handle warnings about undefined signed overflow. */
9016 bool
9017 tree_expr_nonzero_p (tree t)
9019 bool ret, strict_overflow_p;
9021 strict_overflow_p = false;
9022 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9023 if (strict_overflow_p)
9024 fold_overflow_warning (("assuming signed overflow does not occur when "
9025 "determining that expression is always "
9026 "non-zero"),
9027 WARN_STRICT_OVERFLOW_MISC);
9028 return ret;
9031 /* Return true if T is known not to be equal to an integer W. */
9033 bool
9034 expr_not_equal_to (tree t, const wide_int &w)
9036 wide_int min, max, nz;
9037 value_range_type rtype;
9038 switch (TREE_CODE (t))
9040 case INTEGER_CST:
9041 return wi::ne_p (t, w);
9043 case SSA_NAME:
9044 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9045 return false;
9046 rtype = get_range_info (t, &min, &max);
9047 if (rtype == VR_RANGE)
9049 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9050 return true;
9051 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9052 return true;
9054 else if (rtype == VR_ANTI_RANGE
9055 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9056 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9057 return true;
9058 /* If T has some known zero bits and W has any of those bits set,
9059 then T is known not to be equal to W. */
9060 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9061 TYPE_PRECISION (TREE_TYPE (t))), 0))
9062 return true;
9063 return false;
9065 default:
9066 return false;
9070 /* Fold a binary expression of code CODE and type TYPE with operands
9071 OP0 and OP1. LOC is the location of the resulting expression.
9072 Return the folded expression if folding is successful. Otherwise,
9073 return NULL_TREE. */
9075 tree
9076 fold_binary_loc (location_t loc,
9077 enum tree_code code, tree type, tree op0, tree op1)
9079 enum tree_code_class kind = TREE_CODE_CLASS (code);
9080 tree arg0, arg1, tem;
9081 tree t1 = NULL_TREE;
9082 bool strict_overflow_p;
9083 unsigned int prec;
9085 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9086 && TREE_CODE_LENGTH (code) == 2
9087 && op0 != NULL_TREE
9088 && op1 != NULL_TREE);
9090 arg0 = op0;
9091 arg1 = op1;
9093 /* Strip any conversions that don't change the mode. This is
9094 safe for every expression, except for a comparison expression
9095 because its signedness is derived from its operands. So, in
9096 the latter case, only strip conversions that don't change the
9097 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9098 preserved.
9100 Note that this is done as an internal manipulation within the
9101 constant folder, in order to find the simplest representation
9102 of the arguments so that their form can be studied. In any
9103 cases, the appropriate type conversions should be put back in
9104 the tree that will get out of the constant folder. */
9106 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9108 STRIP_SIGN_NOPS (arg0);
9109 STRIP_SIGN_NOPS (arg1);
9111 else
9113 STRIP_NOPS (arg0);
9114 STRIP_NOPS (arg1);
9117 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9118 constant but we can't do arithmetic on them. */
9119 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9121 tem = const_binop (code, type, arg0, arg1);
9122 if (tem != NULL_TREE)
9124 if (TREE_TYPE (tem) != type)
9125 tem = fold_convert_loc (loc, type, tem);
9126 return tem;
9130 /* If this is a commutative operation, and ARG0 is a constant, move it
9131 to ARG1 to reduce the number of tests below. */
9132 if (commutative_tree_code (code)
9133 && tree_swap_operands_p (arg0, arg1))
9134 return fold_build2_loc (loc, code, type, op1, op0);
9136 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9137 to ARG1 to reduce the number of tests below. */
9138 if (kind == tcc_comparison
9139 && tree_swap_operands_p (arg0, arg1))
9140 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9142 tem = generic_simplify (loc, code, type, op0, op1);
9143 if (tem)
9144 return tem;
9146 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9148 First check for cases where an arithmetic operation is applied to a
9149 compound, conditional, or comparison operation. Push the arithmetic
9150 operation inside the compound or conditional to see if any folding
9151 can then be done. Convert comparison to conditional for this purpose.
9152 The also optimizes non-constant cases that used to be done in
9153 expand_expr.
9155 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9156 one of the operands is a comparison and the other is a comparison, a
9157 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9158 code below would make the expression more complex. Change it to a
9159 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9160 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9162 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9163 || code == EQ_EXPR || code == NE_EXPR)
9164 && TREE_CODE (type) != VECTOR_TYPE
9165 && ((truth_value_p (TREE_CODE (arg0))
9166 && (truth_value_p (TREE_CODE (arg1))
9167 || (TREE_CODE (arg1) == BIT_AND_EXPR
9168 && integer_onep (TREE_OPERAND (arg1, 1)))))
9169 || (truth_value_p (TREE_CODE (arg1))
9170 && (truth_value_p (TREE_CODE (arg0))
9171 || (TREE_CODE (arg0) == BIT_AND_EXPR
9172 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9174 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9175 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9176 : TRUTH_XOR_EXPR,
9177 boolean_type_node,
9178 fold_convert_loc (loc, boolean_type_node, arg0),
9179 fold_convert_loc (loc, boolean_type_node, arg1));
9181 if (code == EQ_EXPR)
9182 tem = invert_truthvalue_loc (loc, tem);
9184 return fold_convert_loc (loc, type, tem);
9187 if (TREE_CODE_CLASS (code) == tcc_binary
9188 || TREE_CODE_CLASS (code) == tcc_comparison)
9190 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9192 tem = fold_build2_loc (loc, code, type,
9193 fold_convert_loc (loc, TREE_TYPE (op0),
9194 TREE_OPERAND (arg0, 1)), op1);
9195 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9196 tem);
9198 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9200 tem = fold_build2_loc (loc, code, type, op0,
9201 fold_convert_loc (loc, TREE_TYPE (op1),
9202 TREE_OPERAND (arg1, 1)));
9203 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9204 tem);
9207 if (TREE_CODE (arg0) == COND_EXPR
9208 || TREE_CODE (arg0) == VEC_COND_EXPR
9209 || COMPARISON_CLASS_P (arg0))
9211 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9212 arg0, arg1,
9213 /*cond_first_p=*/1);
9214 if (tem != NULL_TREE)
9215 return tem;
9218 if (TREE_CODE (arg1) == COND_EXPR
9219 || TREE_CODE (arg1) == VEC_COND_EXPR
9220 || COMPARISON_CLASS_P (arg1))
9222 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9223 arg1, arg0,
9224 /*cond_first_p=*/0);
9225 if (tem != NULL_TREE)
9226 return tem;
9230 switch (code)
9232 case MEM_REF:
9233 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9234 if (TREE_CODE (arg0) == ADDR_EXPR
9235 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9237 tree iref = TREE_OPERAND (arg0, 0);
9238 return fold_build2 (MEM_REF, type,
9239 TREE_OPERAND (iref, 0),
9240 int_const_binop (PLUS_EXPR, arg1,
9241 TREE_OPERAND (iref, 1)));
9244 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9245 if (TREE_CODE (arg0) == ADDR_EXPR
9246 && handled_component_p (TREE_OPERAND (arg0, 0)))
9248 tree base;
9249 HOST_WIDE_INT coffset;
9250 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9251 &coffset);
9252 if (!base)
9253 return NULL_TREE;
9254 return fold_build2 (MEM_REF, type,
9255 build_fold_addr_expr (base),
9256 int_const_binop (PLUS_EXPR, arg1,
9257 size_int (coffset)));
9260 return NULL_TREE;
9262 case POINTER_PLUS_EXPR:
9263 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9264 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9265 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9266 return fold_convert_loc (loc, type,
9267 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9268 fold_convert_loc (loc, sizetype,
9269 arg1),
9270 fold_convert_loc (loc, sizetype,
9271 arg0)));
9273 return NULL_TREE;
9275 case PLUS_EXPR:
9276 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9278 /* X + (X / CST) * -CST is X % CST. */
9279 if (TREE_CODE (arg1) == MULT_EXPR
9280 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9281 && operand_equal_p (arg0,
9282 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9284 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9285 tree cst1 = TREE_OPERAND (arg1, 1);
9286 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9287 cst1, cst0);
9288 if (sum && integer_zerop (sum))
9289 return fold_convert_loc (loc, type,
9290 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9291 TREE_TYPE (arg0), arg0,
9292 cst0));
9296 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9297 one. Make sure the type is not saturating and has the signedness of
9298 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9299 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9300 if ((TREE_CODE (arg0) == MULT_EXPR
9301 || TREE_CODE (arg1) == MULT_EXPR)
9302 && !TYPE_SATURATING (type)
9303 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9304 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9305 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9307 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9308 if (tem)
9309 return tem;
9312 if (! FLOAT_TYPE_P (type))
9314 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9315 (plus (plus (mult) (mult)) (foo)) so that we can
9316 take advantage of the factoring cases below. */
9317 if (ANY_INTEGRAL_TYPE_P (type)
9318 && TYPE_OVERFLOW_WRAPS (type)
9319 && (((TREE_CODE (arg0) == PLUS_EXPR
9320 || TREE_CODE (arg0) == MINUS_EXPR)
9321 && TREE_CODE (arg1) == MULT_EXPR)
9322 || ((TREE_CODE (arg1) == PLUS_EXPR
9323 || TREE_CODE (arg1) == MINUS_EXPR)
9324 && TREE_CODE (arg0) == MULT_EXPR)))
9326 tree parg0, parg1, parg, marg;
9327 enum tree_code pcode;
9329 if (TREE_CODE (arg1) == MULT_EXPR)
9330 parg = arg0, marg = arg1;
9331 else
9332 parg = arg1, marg = arg0;
9333 pcode = TREE_CODE (parg);
9334 parg0 = TREE_OPERAND (parg, 0);
9335 parg1 = TREE_OPERAND (parg, 1);
9336 STRIP_NOPS (parg0);
9337 STRIP_NOPS (parg1);
9339 if (TREE_CODE (parg0) == MULT_EXPR
9340 && TREE_CODE (parg1) != MULT_EXPR)
9341 return fold_build2_loc (loc, pcode, type,
9342 fold_build2_loc (loc, PLUS_EXPR, type,
9343 fold_convert_loc (loc, type,
9344 parg0),
9345 fold_convert_loc (loc, type,
9346 marg)),
9347 fold_convert_loc (loc, type, parg1));
9348 if (TREE_CODE (parg0) != MULT_EXPR
9349 && TREE_CODE (parg1) == MULT_EXPR)
9350 return
9351 fold_build2_loc (loc, PLUS_EXPR, type,
9352 fold_convert_loc (loc, type, parg0),
9353 fold_build2_loc (loc, pcode, type,
9354 fold_convert_loc (loc, type, marg),
9355 fold_convert_loc (loc, type,
9356 parg1)));
9359 else
9361 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9362 to __complex__ ( x, y ). This is not the same for SNaNs or
9363 if signed zeros are involved. */
9364 if (!HONOR_SNANS (element_mode (arg0))
9365 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9366 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9368 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9369 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9370 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9371 bool arg0rz = false, arg0iz = false;
9372 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9373 || (arg0i && (arg0iz = real_zerop (arg0i))))
9375 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9376 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9377 if (arg0rz && arg1i && real_zerop (arg1i))
9379 tree rp = arg1r ? arg1r
9380 : build1 (REALPART_EXPR, rtype, arg1);
9381 tree ip = arg0i ? arg0i
9382 : build1 (IMAGPART_EXPR, rtype, arg0);
9383 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9385 else if (arg0iz && arg1r && real_zerop (arg1r))
9387 tree rp = arg0r ? arg0r
9388 : build1 (REALPART_EXPR, rtype, arg0);
9389 tree ip = arg1i ? arg1i
9390 : build1 (IMAGPART_EXPR, rtype, arg1);
9391 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9396 if (flag_unsafe_math_optimizations
9397 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9398 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9399 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9400 return tem;
9402 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9403 We associate floats only if the user has specified
9404 -fassociative-math. */
9405 if (flag_associative_math
9406 && TREE_CODE (arg1) == PLUS_EXPR
9407 && TREE_CODE (arg0) != MULT_EXPR)
9409 tree tree10 = TREE_OPERAND (arg1, 0);
9410 tree tree11 = TREE_OPERAND (arg1, 1);
9411 if (TREE_CODE (tree11) == MULT_EXPR
9412 && TREE_CODE (tree10) == MULT_EXPR)
9414 tree tree0;
9415 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9416 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9419 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9420 We associate floats only if the user has specified
9421 -fassociative-math. */
9422 if (flag_associative_math
9423 && TREE_CODE (arg0) == PLUS_EXPR
9424 && TREE_CODE (arg1) != MULT_EXPR)
9426 tree tree00 = TREE_OPERAND (arg0, 0);
9427 tree tree01 = TREE_OPERAND (arg0, 1);
9428 if (TREE_CODE (tree01) == MULT_EXPR
9429 && TREE_CODE (tree00) == MULT_EXPR)
9431 tree tree0;
9432 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9433 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9438 bit_rotate:
9439 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9440 is a rotate of A by C1 bits. */
9441 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9442 is a rotate of A by B bits. */
9444 enum tree_code code0, code1;
9445 tree rtype;
9446 code0 = TREE_CODE (arg0);
9447 code1 = TREE_CODE (arg1);
9448 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9449 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9450 && operand_equal_p (TREE_OPERAND (arg0, 0),
9451 TREE_OPERAND (arg1, 0), 0)
9452 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9453 TYPE_UNSIGNED (rtype))
9454 /* Only create rotates in complete modes. Other cases are not
9455 expanded properly. */
9456 && (element_precision (rtype)
9457 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9459 tree tree01, tree11;
9460 enum tree_code code01, code11;
9462 tree01 = TREE_OPERAND (arg0, 1);
9463 tree11 = TREE_OPERAND (arg1, 1);
9464 STRIP_NOPS (tree01);
9465 STRIP_NOPS (tree11);
9466 code01 = TREE_CODE (tree01);
9467 code11 = TREE_CODE (tree11);
9468 if (code01 == INTEGER_CST
9469 && code11 == INTEGER_CST
9470 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9471 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9473 tem = build2_loc (loc, LROTATE_EXPR,
9474 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9475 TREE_OPERAND (arg0, 0),
9476 code0 == LSHIFT_EXPR
9477 ? TREE_OPERAND (arg0, 1)
9478 : TREE_OPERAND (arg1, 1));
9479 return fold_convert_loc (loc, type, tem);
9481 else if (code11 == MINUS_EXPR)
9483 tree tree110, tree111;
9484 tree110 = TREE_OPERAND (tree11, 0);
9485 tree111 = TREE_OPERAND (tree11, 1);
9486 STRIP_NOPS (tree110);
9487 STRIP_NOPS (tree111);
9488 if (TREE_CODE (tree110) == INTEGER_CST
9489 && 0 == compare_tree_int (tree110,
9490 element_precision
9491 (TREE_TYPE (TREE_OPERAND
9492 (arg0, 0))))
9493 && operand_equal_p (tree01, tree111, 0))
9494 return
9495 fold_convert_loc (loc, type,
9496 build2 ((code0 == LSHIFT_EXPR
9497 ? LROTATE_EXPR
9498 : RROTATE_EXPR),
9499 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9500 TREE_OPERAND (arg0, 0),
9501 TREE_OPERAND (arg0, 1)));
9503 else if (code01 == MINUS_EXPR)
9505 tree tree010, tree011;
9506 tree010 = TREE_OPERAND (tree01, 0);
9507 tree011 = TREE_OPERAND (tree01, 1);
9508 STRIP_NOPS (tree010);
9509 STRIP_NOPS (tree011);
9510 if (TREE_CODE (tree010) == INTEGER_CST
9511 && 0 == compare_tree_int (tree010,
9512 element_precision
9513 (TREE_TYPE (TREE_OPERAND
9514 (arg0, 0))))
9515 && operand_equal_p (tree11, tree011, 0))
9516 return fold_convert_loc
9517 (loc, type,
9518 build2 ((code0 != LSHIFT_EXPR
9519 ? LROTATE_EXPR
9520 : RROTATE_EXPR),
9521 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9522 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9527 associate:
9528 /* In most languages, can't associate operations on floats through
9529 parentheses. Rather than remember where the parentheses were, we
9530 don't associate floats at all, unless the user has specified
9531 -fassociative-math.
9532 And, we need to make sure type is not saturating. */
9534 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9535 && !TYPE_SATURATING (type))
9537 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9538 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9539 tree atype = type;
9540 bool ok = true;
9542 /* Split both trees into variables, constants, and literals. Then
9543 associate each group together, the constants with literals,
9544 then the result with variables. This increases the chances of
9545 literals being recombined later and of generating relocatable
9546 expressions for the sum of a constant and literal. */
9547 var0 = split_tree (arg0, type, code,
9548 &minus_var0, &con0, &minus_con0,
9549 &lit0, &minus_lit0, 0);
9550 var1 = split_tree (arg1, type, code,
9551 &minus_var1, &con1, &minus_con1,
9552 &lit1, &minus_lit1, code == MINUS_EXPR);
9554 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9555 if (code == MINUS_EXPR)
9556 code = PLUS_EXPR;
9558 /* With undefined overflow prefer doing association in a type
9559 which wraps on overflow, if that is one of the operand types. */
9560 if (POINTER_TYPE_P (type)
9561 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9563 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9564 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9565 atype = TREE_TYPE (arg0);
9566 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9567 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9568 atype = TREE_TYPE (arg1);
9569 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9572 /* With undefined overflow we can only associate constants with one
9573 variable, and constants whose association doesn't overflow. */
9574 if (POINTER_TYPE_P (atype)
9575 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9577 if ((var0 && var1) || (minus_var0 && minus_var1))
9579 /* ??? If split_tree would handle NEGATE_EXPR we could
9580 simply reject these cases and the allowed cases would
9581 be the var0/minus_var1 ones. */
9582 tree tmp0 = var0 ? var0 : minus_var0;
9583 tree tmp1 = var1 ? var1 : minus_var1;
9584 bool one_neg = false;
9586 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9588 tmp0 = TREE_OPERAND (tmp0, 0);
9589 one_neg = !one_neg;
9591 if (CONVERT_EXPR_P (tmp0)
9592 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9593 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9594 <= TYPE_PRECISION (atype)))
9595 tmp0 = TREE_OPERAND (tmp0, 0);
9596 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9598 tmp1 = TREE_OPERAND (tmp1, 0);
9599 one_neg = !one_neg;
9601 if (CONVERT_EXPR_P (tmp1)
9602 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9603 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9604 <= TYPE_PRECISION (atype)))
9605 tmp1 = TREE_OPERAND (tmp1, 0);
9606 /* The only case we can still associate with two variables
9607 is if they cancel out. */
9608 if (!one_neg
9609 || !operand_equal_p (tmp0, tmp1, 0))
9610 ok = false;
9612 else if ((var0 && minus_var1
9613 && ! operand_equal_p (var0, minus_var1, 0))
9614 || (minus_var0 && var1
9615 && ! operand_equal_p (minus_var0, var1, 0)))
9616 ok = false;
9619 /* Only do something if we found more than two objects. Otherwise,
9620 nothing has changed and we risk infinite recursion. */
9621 if (ok
9622 && (2 < ((var0 != 0) + (var1 != 0)
9623 + (minus_var0 != 0) + (minus_var1 != 0)
9624 + (con0 != 0) + (con1 != 0)
9625 + (minus_con0 != 0) + (minus_con1 != 0)
9626 + (lit0 != 0) + (lit1 != 0)
9627 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9629 var0 = associate_trees (loc, var0, var1, code, atype);
9630 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9631 code, atype);
9632 con0 = associate_trees (loc, con0, con1, code, atype);
9633 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9634 code, atype);
9635 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9636 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9637 code, atype);
9639 if (minus_var0 && var0)
9641 var0 = associate_trees (loc, var0, minus_var0,
9642 MINUS_EXPR, atype);
9643 minus_var0 = 0;
9645 if (minus_con0 && con0)
9647 con0 = associate_trees (loc, con0, minus_con0,
9648 MINUS_EXPR, atype);
9649 minus_con0 = 0;
9652 /* Preserve the MINUS_EXPR if the negative part of the literal is
9653 greater than the positive part. Otherwise, the multiplicative
9654 folding code (i.e extract_muldiv) may be fooled in case
9655 unsigned constants are subtracted, like in the following
9656 example: ((X*2 + 4) - 8U)/2. */
9657 if (minus_lit0 && lit0)
9659 if (TREE_CODE (lit0) == INTEGER_CST
9660 && TREE_CODE (minus_lit0) == INTEGER_CST
9661 && tree_int_cst_lt (lit0, minus_lit0)
9662 /* But avoid ending up with only negated parts. */
9663 && (var0 || con0))
9665 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9666 MINUS_EXPR, atype);
9667 lit0 = 0;
9669 else
9671 lit0 = associate_trees (loc, lit0, minus_lit0,
9672 MINUS_EXPR, atype);
9673 minus_lit0 = 0;
9677 /* Don't introduce overflows through reassociation. */
9678 if ((lit0 && TREE_OVERFLOW_P (lit0))
9679 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9680 return NULL_TREE;
9682 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9683 con0 = associate_trees (loc, con0, lit0, code, atype);
9684 lit0 = 0;
9685 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9686 code, atype);
9687 minus_lit0 = 0;
9689 /* Eliminate minus_con0. */
9690 if (minus_con0)
9692 if (con0)
9693 con0 = associate_trees (loc, con0, minus_con0,
9694 MINUS_EXPR, atype);
9695 else if (var0)
9696 var0 = associate_trees (loc, var0, minus_con0,
9697 MINUS_EXPR, atype);
9698 else
9699 gcc_unreachable ();
9700 minus_con0 = 0;
9703 /* Eliminate minus_var0. */
9704 if (minus_var0)
9706 if (con0)
9707 con0 = associate_trees (loc, con0, minus_var0,
9708 MINUS_EXPR, atype);
9709 else
9710 gcc_unreachable ();
9711 minus_var0 = 0;
9714 return
9715 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9716 code, atype));
9720 return NULL_TREE;
9722 case MINUS_EXPR:
9723 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9724 if (TREE_CODE (arg0) == NEGATE_EXPR
9725 && negate_expr_p (op1))
9726 return fold_build2_loc (loc, MINUS_EXPR, type,
9727 negate_expr (op1),
9728 fold_convert_loc (loc, type,
9729 TREE_OPERAND (arg0, 0)));
9731 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9732 __complex__ ( x, -y ). This is not the same for SNaNs or if
9733 signed zeros are involved. */
9734 if (!HONOR_SNANS (element_mode (arg0))
9735 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9736 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9738 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9739 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9740 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9741 bool arg0rz = false, arg0iz = false;
9742 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9743 || (arg0i && (arg0iz = real_zerop (arg0i))))
9745 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9746 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9747 if (arg0rz && arg1i && real_zerop (arg1i))
9749 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9750 arg1r ? arg1r
9751 : build1 (REALPART_EXPR, rtype, arg1));
9752 tree ip = arg0i ? arg0i
9753 : build1 (IMAGPART_EXPR, rtype, arg0);
9754 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9756 else if (arg0iz && arg1r && real_zerop (arg1r))
9758 tree rp = arg0r ? arg0r
9759 : build1 (REALPART_EXPR, rtype, arg0);
9760 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9761 arg1i ? arg1i
9762 : build1 (IMAGPART_EXPR, rtype, arg1));
9763 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9768 /* A - B -> A + (-B) if B is easily negatable. */
9769 if (negate_expr_p (op1)
9770 && ! TYPE_OVERFLOW_SANITIZED (type)
9771 && ((FLOAT_TYPE_P (type)
9772 /* Avoid this transformation if B is a positive REAL_CST. */
9773 && (TREE_CODE (op1) != REAL_CST
9774 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9775 || INTEGRAL_TYPE_P (type)))
9776 return fold_build2_loc (loc, PLUS_EXPR, type,
9777 fold_convert_loc (loc, type, arg0),
9778 negate_expr (op1));
9780 /* Fold &a[i] - &a[j] to i-j. */
9781 if (TREE_CODE (arg0) == ADDR_EXPR
9782 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9783 && TREE_CODE (arg1) == ADDR_EXPR
9784 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9786 tree tem = fold_addr_of_array_ref_difference (loc, type,
9787 TREE_OPERAND (arg0, 0),
9788 TREE_OPERAND (arg1, 0));
9789 if (tem)
9790 return tem;
9793 if (FLOAT_TYPE_P (type)
9794 && flag_unsafe_math_optimizations
9795 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9796 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9797 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9798 return tem;
9800 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9801 one. Make sure the type is not saturating and has the signedness of
9802 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9803 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9804 if ((TREE_CODE (arg0) == MULT_EXPR
9805 || TREE_CODE (arg1) == MULT_EXPR)
9806 && !TYPE_SATURATING (type)
9807 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9808 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9809 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9811 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9812 if (tem)
9813 return tem;
9816 goto associate;
9818 case MULT_EXPR:
9819 if (! FLOAT_TYPE_P (type))
9821 /* Transform x * -C into -x * C if x is easily negatable. */
9822 if (TREE_CODE (op1) == INTEGER_CST
9823 && tree_int_cst_sgn (op1) == -1
9824 && negate_expr_p (op0)
9825 && negate_expr_p (op1)
9826 && (tem = negate_expr (op1)) != op1
9827 && ! TREE_OVERFLOW (tem))
9828 return fold_build2_loc (loc, MULT_EXPR, type,
9829 fold_convert_loc (loc, type,
9830 negate_expr (op0)), tem);
9832 strict_overflow_p = false;
9833 if (TREE_CODE (arg1) == INTEGER_CST
9834 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9835 &strict_overflow_p)))
9837 if (strict_overflow_p)
9838 fold_overflow_warning (("assuming signed overflow does not "
9839 "occur when simplifying "
9840 "multiplication"),
9841 WARN_STRICT_OVERFLOW_MISC);
9842 return fold_convert_loc (loc, type, tem);
9845 /* Optimize z * conj(z) for integer complex numbers. */
9846 if (TREE_CODE (arg0) == CONJ_EXPR
9847 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9848 return fold_mult_zconjz (loc, type, arg1);
9849 if (TREE_CODE (arg1) == CONJ_EXPR
9850 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9851 return fold_mult_zconjz (loc, type, arg0);
9853 else
9855 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9856 This is not the same for NaNs or if signed zeros are
9857 involved. */
9858 if (!HONOR_NANS (arg0)
9859 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9860 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9861 && TREE_CODE (arg1) == COMPLEX_CST
9862 && real_zerop (TREE_REALPART (arg1)))
9864 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9865 if (real_onep (TREE_IMAGPART (arg1)))
9866 return
9867 fold_build2_loc (loc, COMPLEX_EXPR, type,
9868 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9869 rtype, arg0)),
9870 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9871 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9872 return
9873 fold_build2_loc (loc, COMPLEX_EXPR, type,
9874 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9875 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9876 rtype, arg0)));
9879 /* Optimize z * conj(z) for floating point complex numbers.
9880 Guarded by flag_unsafe_math_optimizations as non-finite
9881 imaginary components don't produce scalar results. */
9882 if (flag_unsafe_math_optimizations
9883 && TREE_CODE (arg0) == CONJ_EXPR
9884 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9885 return fold_mult_zconjz (loc, type, arg1);
9886 if (flag_unsafe_math_optimizations
9887 && TREE_CODE (arg1) == CONJ_EXPR
9888 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9889 return fold_mult_zconjz (loc, type, arg0);
9891 goto associate;
9893 case BIT_IOR_EXPR:
9894 /* Canonicalize (X & C1) | C2. */
9895 if (TREE_CODE (arg0) == BIT_AND_EXPR
9896 && TREE_CODE (arg1) == INTEGER_CST
9897 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9899 int width = TYPE_PRECISION (type), w;
9900 wide_int c1 = TREE_OPERAND (arg0, 1);
9901 wide_int c2 = arg1;
9903 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9904 if ((c1 & c2) == c1)
9905 return omit_one_operand_loc (loc, type, arg1,
9906 TREE_OPERAND (arg0, 0));
9908 wide_int msk = wi::mask (width, false,
9909 TYPE_PRECISION (TREE_TYPE (arg1)));
9911 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9912 if (msk.and_not (c1 | c2) == 0)
9914 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9915 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9918 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9919 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9920 mode which allows further optimizations. */
9921 c1 &= msk;
9922 c2 &= msk;
9923 wide_int c3 = c1.and_not (c2);
9924 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9926 wide_int mask = wi::mask (w, false,
9927 TYPE_PRECISION (type));
9928 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9930 c3 = mask;
9931 break;
9935 if (c3 != c1)
9937 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9938 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
9939 wide_int_to_tree (type, c3));
9940 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9944 /* See if this can be simplified into a rotate first. If that
9945 is unsuccessful continue in the association code. */
9946 goto bit_rotate;
9948 case BIT_XOR_EXPR:
9949 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9950 if (TREE_CODE (arg0) == BIT_AND_EXPR
9951 && INTEGRAL_TYPE_P (type)
9952 && integer_onep (TREE_OPERAND (arg0, 1))
9953 && integer_onep (arg1))
9954 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9955 build_zero_cst (TREE_TYPE (arg0)));
9957 /* See if this can be simplified into a rotate first. If that
9958 is unsuccessful continue in the association code. */
9959 goto bit_rotate;
9961 case BIT_AND_EXPR:
9962 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9963 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9964 && INTEGRAL_TYPE_P (type)
9965 && integer_onep (TREE_OPERAND (arg0, 1))
9966 && integer_onep (arg1))
9968 tree tem2;
9969 tem = TREE_OPERAND (arg0, 0);
9970 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9971 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9972 tem, tem2);
9973 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9974 build_zero_cst (TREE_TYPE (tem)));
9976 /* Fold ~X & 1 as (X & 1) == 0. */
9977 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9978 && INTEGRAL_TYPE_P (type)
9979 && integer_onep (arg1))
9981 tree tem2;
9982 tem = TREE_OPERAND (arg0, 0);
9983 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9984 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9985 tem, tem2);
9986 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9987 build_zero_cst (TREE_TYPE (tem)));
9989 /* Fold !X & 1 as X == 0. */
9990 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9991 && integer_onep (arg1))
9993 tem = TREE_OPERAND (arg0, 0);
9994 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9995 build_zero_cst (TREE_TYPE (tem)));
9998 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9999 multiple of 1 << CST. */
10000 if (TREE_CODE (arg1) == INTEGER_CST)
10002 wide_int cst1 = arg1;
10003 wide_int ncst1 = -cst1;
10004 if ((cst1 & ncst1) == ncst1
10005 && multiple_of_p (type, arg0,
10006 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10007 return fold_convert_loc (loc, type, arg0);
10010 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10011 bits from CST2. */
10012 if (TREE_CODE (arg1) == INTEGER_CST
10013 && TREE_CODE (arg0) == MULT_EXPR
10014 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10016 wide_int warg1 = arg1;
10017 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10019 if (masked == 0)
10020 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10021 arg0, arg1);
10022 else if (masked != warg1)
10024 /* Avoid the transform if arg1 is a mask of some
10025 mode which allows further optimizations. */
10026 int pop = wi::popcount (warg1);
10027 if (!(pop >= BITS_PER_UNIT
10028 && pow2p_hwi (pop)
10029 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10030 return fold_build2_loc (loc, code, type, op0,
10031 wide_int_to_tree (type, masked));
10035 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10036 ((A & N) + B) & M -> (A + B) & M
10037 Similarly if (N & M) == 0,
10038 ((A | N) + B) & M -> (A + B) & M
10039 and for - instead of + (or unary - instead of +)
10040 and/or ^ instead of |.
10041 If B is constant and (B & M) == 0, fold into A & M. */
10042 if (TREE_CODE (arg1) == INTEGER_CST)
10044 wide_int cst1 = arg1;
10045 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10046 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10047 && (TREE_CODE (arg0) == PLUS_EXPR
10048 || TREE_CODE (arg0) == MINUS_EXPR
10049 || TREE_CODE (arg0) == NEGATE_EXPR)
10050 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10051 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10053 tree pmop[2];
10054 int which = 0;
10055 wide_int cst0;
10057 /* Now we know that arg0 is (C + D) or (C - D) or
10058 -C and arg1 (M) is == (1LL << cst) - 1.
10059 Store C into PMOP[0] and D into PMOP[1]. */
10060 pmop[0] = TREE_OPERAND (arg0, 0);
10061 pmop[1] = NULL;
10062 if (TREE_CODE (arg0) != NEGATE_EXPR)
10064 pmop[1] = TREE_OPERAND (arg0, 1);
10065 which = 1;
10068 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10069 which = -1;
10071 for (; which >= 0; which--)
10072 switch (TREE_CODE (pmop[which]))
10074 case BIT_AND_EXPR:
10075 case BIT_IOR_EXPR:
10076 case BIT_XOR_EXPR:
10077 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10078 != INTEGER_CST)
10079 break;
10080 cst0 = TREE_OPERAND (pmop[which], 1);
10081 cst0 &= cst1;
10082 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10084 if (cst0 != cst1)
10085 break;
10087 else if (cst0 != 0)
10088 break;
10089 /* If C or D is of the form (A & N) where
10090 (N & M) == M, or of the form (A | N) or
10091 (A ^ N) where (N & M) == 0, replace it with A. */
10092 pmop[which] = TREE_OPERAND (pmop[which], 0);
10093 break;
10094 case INTEGER_CST:
10095 /* If C or D is a N where (N & M) == 0, it can be
10096 omitted (assumed 0). */
10097 if ((TREE_CODE (arg0) == PLUS_EXPR
10098 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10099 && (cst1 & pmop[which]) == 0)
10100 pmop[which] = NULL;
10101 break;
10102 default:
10103 break;
10106 /* Only build anything new if we optimized one or both arguments
10107 above. */
10108 if (pmop[0] != TREE_OPERAND (arg0, 0)
10109 || (TREE_CODE (arg0) != NEGATE_EXPR
10110 && pmop[1] != TREE_OPERAND (arg0, 1)))
10112 tree utype = TREE_TYPE (arg0);
10113 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10115 /* Perform the operations in a type that has defined
10116 overflow behavior. */
10117 utype = unsigned_type_for (TREE_TYPE (arg0));
10118 if (pmop[0] != NULL)
10119 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10120 if (pmop[1] != NULL)
10121 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10124 if (TREE_CODE (arg0) == NEGATE_EXPR)
10125 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10126 else if (TREE_CODE (arg0) == PLUS_EXPR)
10128 if (pmop[0] != NULL && pmop[1] != NULL)
10129 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10130 pmop[0], pmop[1]);
10131 else if (pmop[0] != NULL)
10132 tem = pmop[0];
10133 else if (pmop[1] != NULL)
10134 tem = pmop[1];
10135 else
10136 return build_int_cst (type, 0);
10138 else if (pmop[0] == NULL)
10139 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10140 else
10141 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10142 pmop[0], pmop[1]);
10143 /* TEM is now the new binary +, - or unary - replacement. */
10144 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10145 fold_convert_loc (loc, utype, arg1));
10146 return fold_convert_loc (loc, type, tem);
10151 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10152 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10153 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10155 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10157 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10158 if (mask == -1)
10159 return
10160 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10163 goto associate;
10165 case RDIV_EXPR:
10166 /* Don't touch a floating-point divide by zero unless the mode
10167 of the constant can represent infinity. */
10168 if (TREE_CODE (arg1) == REAL_CST
10169 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10170 && real_zerop (arg1))
10171 return NULL_TREE;
10173 /* (-A) / (-B) -> A / B */
10174 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10175 return fold_build2_loc (loc, RDIV_EXPR, type,
10176 TREE_OPERAND (arg0, 0),
10177 negate_expr (arg1));
10178 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10179 return fold_build2_loc (loc, RDIV_EXPR, type,
10180 negate_expr (arg0),
10181 TREE_OPERAND (arg1, 0));
10182 return NULL_TREE;
10184 case TRUNC_DIV_EXPR:
10185 /* Fall through */
10187 case FLOOR_DIV_EXPR:
10188 /* Simplify A / (B << N) where A and B are positive and B is
10189 a power of 2, to A >> (N + log2(B)). */
10190 strict_overflow_p = false;
10191 if (TREE_CODE (arg1) == LSHIFT_EXPR
10192 && (TYPE_UNSIGNED (type)
10193 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10195 tree sval = TREE_OPERAND (arg1, 0);
10196 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10198 tree sh_cnt = TREE_OPERAND (arg1, 1);
10199 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10200 wi::exact_log2 (sval));
10202 if (strict_overflow_p)
10203 fold_overflow_warning (("assuming signed overflow does not "
10204 "occur when simplifying A / (B << N)"),
10205 WARN_STRICT_OVERFLOW_MISC);
10207 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10208 sh_cnt, pow2);
10209 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10210 fold_convert_loc (loc, type, arg0), sh_cnt);
10214 /* Fall through */
10216 case ROUND_DIV_EXPR:
10217 case CEIL_DIV_EXPR:
10218 case EXACT_DIV_EXPR:
10219 if (integer_zerop (arg1))
10220 return NULL_TREE;
10222 /* Convert -A / -B to A / B when the type is signed and overflow is
10223 undefined. */
10224 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10225 && TREE_CODE (op0) == NEGATE_EXPR
10226 && negate_expr_p (op1))
10228 if (INTEGRAL_TYPE_P (type))
10229 fold_overflow_warning (("assuming signed overflow does not occur "
10230 "when distributing negation across "
10231 "division"),
10232 WARN_STRICT_OVERFLOW_MISC);
10233 return fold_build2_loc (loc, code, type,
10234 fold_convert_loc (loc, type,
10235 TREE_OPERAND (arg0, 0)),
10236 negate_expr (op1));
10238 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10239 && TREE_CODE (arg1) == NEGATE_EXPR
10240 && negate_expr_p (op0))
10242 if (INTEGRAL_TYPE_P (type))
10243 fold_overflow_warning (("assuming signed overflow does not occur "
10244 "when distributing negation across "
10245 "division"),
10246 WARN_STRICT_OVERFLOW_MISC);
10247 return fold_build2_loc (loc, code, type,
10248 negate_expr (op0),
10249 fold_convert_loc (loc, type,
10250 TREE_OPERAND (arg1, 0)));
10253 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10254 operation, EXACT_DIV_EXPR.
10256 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10257 At one time others generated faster code, it's not clear if they do
10258 after the last round to changes to the DIV code in expmed.c. */
10259 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10260 && multiple_of_p (type, arg0, arg1))
10261 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10262 fold_convert (type, arg0),
10263 fold_convert (type, arg1));
10265 strict_overflow_p = false;
10266 if (TREE_CODE (arg1) == INTEGER_CST
10267 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10268 &strict_overflow_p)))
10270 if (strict_overflow_p)
10271 fold_overflow_warning (("assuming signed overflow does not occur "
10272 "when simplifying division"),
10273 WARN_STRICT_OVERFLOW_MISC);
10274 return fold_convert_loc (loc, type, tem);
10277 return NULL_TREE;
10279 case CEIL_MOD_EXPR:
10280 case FLOOR_MOD_EXPR:
10281 case ROUND_MOD_EXPR:
10282 case TRUNC_MOD_EXPR:
10283 strict_overflow_p = false;
10284 if (TREE_CODE (arg1) == INTEGER_CST
10285 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10286 &strict_overflow_p)))
10288 if (strict_overflow_p)
10289 fold_overflow_warning (("assuming signed overflow does not occur "
10290 "when simplifying modulus"),
10291 WARN_STRICT_OVERFLOW_MISC);
10292 return fold_convert_loc (loc, type, tem);
10295 return NULL_TREE;
10297 case LROTATE_EXPR:
10298 case RROTATE_EXPR:
10299 case RSHIFT_EXPR:
10300 case LSHIFT_EXPR:
10301 /* Since negative shift count is not well-defined,
10302 don't try to compute it in the compiler. */
10303 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10304 return NULL_TREE;
10306 prec = element_precision (type);
10308 /* If we have a rotate of a bit operation with the rotate count and
10309 the second operand of the bit operation both constant,
10310 permute the two operations. */
10311 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10312 && (TREE_CODE (arg0) == BIT_AND_EXPR
10313 || TREE_CODE (arg0) == BIT_IOR_EXPR
10314 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10315 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10317 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10318 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10319 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10320 fold_build2_loc (loc, code, type,
10321 arg00, arg1),
10322 fold_build2_loc (loc, code, type,
10323 arg01, arg1));
10326 /* Two consecutive rotates adding up to the some integer
10327 multiple of the precision of the type can be ignored. */
10328 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10329 && TREE_CODE (arg0) == RROTATE_EXPR
10330 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10331 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10332 prec) == 0)
10333 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10335 return NULL_TREE;
10337 case MIN_EXPR:
10338 case MAX_EXPR:
10339 goto associate;
10341 case TRUTH_ANDIF_EXPR:
10342 /* Note that the operands of this must be ints
10343 and their values must be 0 or 1.
10344 ("true" is a fixed value perhaps depending on the language.) */
10345 /* If first arg is constant zero, return it. */
10346 if (integer_zerop (arg0))
10347 return fold_convert_loc (loc, type, arg0);
10348 /* FALLTHRU */
10349 case TRUTH_AND_EXPR:
10350 /* If either arg is constant true, drop it. */
10351 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10352 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10353 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10354 /* Preserve sequence points. */
10355 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10356 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10357 /* If second arg is constant zero, result is zero, but first arg
10358 must be evaluated. */
10359 if (integer_zerop (arg1))
10360 return omit_one_operand_loc (loc, type, arg1, arg0);
10361 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10362 case will be handled here. */
10363 if (integer_zerop (arg0))
10364 return omit_one_operand_loc (loc, type, arg0, arg1);
10366 /* !X && X is always false. */
10367 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10368 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10369 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10370 /* X && !X is always false. */
10371 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10372 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10373 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10375 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10376 means A >= Y && A != MAX, but in this case we know that
10377 A < X <= MAX. */
10379 if (!TREE_SIDE_EFFECTS (arg0)
10380 && !TREE_SIDE_EFFECTS (arg1))
10382 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10383 if (tem && !operand_equal_p (tem, arg0, 0))
10384 return fold_build2_loc (loc, code, type, tem, arg1);
10386 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10387 if (tem && !operand_equal_p (tem, arg1, 0))
10388 return fold_build2_loc (loc, code, type, arg0, tem);
10391 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10392 != NULL_TREE)
10393 return tem;
10395 return NULL_TREE;
10397 case TRUTH_ORIF_EXPR:
10398 /* Note that the operands of this must be ints
10399 and their values must be 0 or true.
10400 ("true" is a fixed value perhaps depending on the language.) */
10401 /* If first arg is constant true, return it. */
10402 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10403 return fold_convert_loc (loc, type, arg0);
10404 /* FALLTHRU */
10405 case TRUTH_OR_EXPR:
10406 /* If either arg is constant zero, drop it. */
10407 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10408 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10409 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10410 /* Preserve sequence points. */
10411 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10412 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10413 /* If second arg is constant true, result is true, but we must
10414 evaluate first arg. */
10415 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10416 return omit_one_operand_loc (loc, type, arg1, arg0);
10417 /* Likewise for first arg, but note this only occurs here for
10418 TRUTH_OR_EXPR. */
10419 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10420 return omit_one_operand_loc (loc, type, arg0, arg1);
10422 /* !X || X is always true. */
10423 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10424 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10425 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10426 /* X || !X is always true. */
10427 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10428 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10429 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10431 /* (X && !Y) || (!X && Y) is X ^ Y */
10432 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10433 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10435 tree a0, a1, l0, l1, n0, n1;
10437 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10438 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10440 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10441 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10443 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10444 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10446 if ((operand_equal_p (n0, a0, 0)
10447 && operand_equal_p (n1, a1, 0))
10448 || (operand_equal_p (n0, a1, 0)
10449 && operand_equal_p (n1, a0, 0)))
10450 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10453 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10454 != NULL_TREE)
10455 return tem;
10457 return NULL_TREE;
10459 case TRUTH_XOR_EXPR:
10460 /* If the second arg is constant zero, drop it. */
10461 if (integer_zerop (arg1))
10462 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10463 /* If the second arg is constant true, this is a logical inversion. */
10464 if (integer_onep (arg1))
10466 tem = invert_truthvalue_loc (loc, arg0);
10467 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10469 /* Identical arguments cancel to zero. */
10470 if (operand_equal_p (arg0, arg1, 0))
10471 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10473 /* !X ^ X is always true. */
10474 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10475 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10476 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10478 /* X ^ !X is always true. */
10479 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10480 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10481 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10483 return NULL_TREE;
10485 case EQ_EXPR:
10486 case NE_EXPR:
10487 STRIP_NOPS (arg0);
10488 STRIP_NOPS (arg1);
10490 tem = fold_comparison (loc, code, type, op0, op1);
10491 if (tem != NULL_TREE)
10492 return tem;
10494 /* bool_var != 1 becomes !bool_var. */
10495 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10496 && code == NE_EXPR)
10497 return fold_convert_loc (loc, type,
10498 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10499 TREE_TYPE (arg0), arg0));
10501 /* bool_var == 0 becomes !bool_var. */
10502 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10503 && code == EQ_EXPR)
10504 return fold_convert_loc (loc, type,
10505 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10506 TREE_TYPE (arg0), arg0));
10508 /* !exp != 0 becomes !exp */
10509 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10510 && code == NE_EXPR)
10511 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10513 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10514 if ((TREE_CODE (arg0) == PLUS_EXPR
10515 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10516 || TREE_CODE (arg0) == MINUS_EXPR)
10517 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10518 0)),
10519 arg1, 0)
10520 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10521 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10523 tree val = TREE_OPERAND (arg0, 1);
10524 val = fold_build2_loc (loc, code, type, val,
10525 build_int_cst (TREE_TYPE (val), 0));
10526 return omit_two_operands_loc (loc, type, val,
10527 TREE_OPERAND (arg0, 0), arg1);
10530 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10531 if ((TREE_CODE (arg1) == PLUS_EXPR
10532 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10533 || TREE_CODE (arg1) == MINUS_EXPR)
10534 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10535 0)),
10536 arg0, 0)
10537 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10538 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10540 tree val = TREE_OPERAND (arg1, 1);
10541 val = fold_build2_loc (loc, code, type, val,
10542 build_int_cst (TREE_TYPE (val), 0));
10543 return omit_two_operands_loc (loc, type, val,
10544 TREE_OPERAND (arg1, 0), arg0);
10547 /* If this is an EQ or NE comparison with zero and ARG0 is
10548 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10549 two operations, but the latter can be done in one less insn
10550 on machines that have only two-operand insns or on which a
10551 constant cannot be the first operand. */
10552 if (TREE_CODE (arg0) == BIT_AND_EXPR
10553 && integer_zerop (arg1))
10555 tree arg00 = TREE_OPERAND (arg0, 0);
10556 tree arg01 = TREE_OPERAND (arg0, 1);
10557 if (TREE_CODE (arg00) == LSHIFT_EXPR
10558 && integer_onep (TREE_OPERAND (arg00, 0)))
10560 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10561 arg01, TREE_OPERAND (arg00, 1));
10562 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10563 build_int_cst (TREE_TYPE (arg0), 1));
10564 return fold_build2_loc (loc, code, type,
10565 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10566 arg1);
10568 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10569 && integer_onep (TREE_OPERAND (arg01, 0)))
10571 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10572 arg00, TREE_OPERAND (arg01, 1));
10573 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10574 build_int_cst (TREE_TYPE (arg0), 1));
10575 return fold_build2_loc (loc, code, type,
10576 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10577 arg1);
10581 /* If this is an NE or EQ comparison of zero against the result of a
10582 signed MOD operation whose second operand is a power of 2, make
10583 the MOD operation unsigned since it is simpler and equivalent. */
10584 if (integer_zerop (arg1)
10585 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10586 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10587 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10588 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10589 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10590 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10592 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10593 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10594 fold_convert_loc (loc, newtype,
10595 TREE_OPERAND (arg0, 0)),
10596 fold_convert_loc (loc, newtype,
10597 TREE_OPERAND (arg0, 1)));
10599 return fold_build2_loc (loc, code, type, newmod,
10600 fold_convert_loc (loc, newtype, arg1));
10603 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10604 C1 is a valid shift constant, and C2 is a power of two, i.e.
10605 a single bit. */
10606 if (TREE_CODE (arg0) == BIT_AND_EXPR
10607 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10608 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10609 == INTEGER_CST
10610 && integer_pow2p (TREE_OPERAND (arg0, 1))
10611 && integer_zerop (arg1))
10613 tree itype = TREE_TYPE (arg0);
10614 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10615 prec = TYPE_PRECISION (itype);
10617 /* Check for a valid shift count. */
10618 if (wi::ltu_p (arg001, prec))
10620 tree arg01 = TREE_OPERAND (arg0, 1);
10621 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10622 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10623 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10624 can be rewritten as (X & (C2 << C1)) != 0. */
10625 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10627 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10628 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10629 return fold_build2_loc (loc, code, type, tem,
10630 fold_convert_loc (loc, itype, arg1));
10632 /* Otherwise, for signed (arithmetic) shifts,
10633 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10634 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10635 else if (!TYPE_UNSIGNED (itype))
10636 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10637 arg000, build_int_cst (itype, 0));
10638 /* Otherwise, of unsigned (logical) shifts,
10639 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10640 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10641 else
10642 return omit_one_operand_loc (loc, type,
10643 code == EQ_EXPR ? integer_one_node
10644 : integer_zero_node,
10645 arg000);
10649 /* If this is a comparison of a field, we may be able to simplify it. */
10650 if ((TREE_CODE (arg0) == COMPONENT_REF
10651 || TREE_CODE (arg0) == BIT_FIELD_REF)
10652 /* Handle the constant case even without -O
10653 to make sure the warnings are given. */
10654 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10656 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10657 if (t1)
10658 return t1;
10661 /* Optimize comparisons of strlen vs zero to a compare of the
10662 first character of the string vs zero. To wit,
10663 strlen(ptr) == 0 => *ptr == 0
10664 strlen(ptr) != 0 => *ptr != 0
10665 Other cases should reduce to one of these two (or a constant)
10666 due to the return value of strlen being unsigned. */
10667 if (TREE_CODE (arg0) == CALL_EXPR
10668 && integer_zerop (arg1))
10670 tree fndecl = get_callee_fndecl (arg0);
10672 if (fndecl
10673 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10674 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10675 && call_expr_nargs (arg0) == 1
10676 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10678 tree iref = build_fold_indirect_ref_loc (loc,
10679 CALL_EXPR_ARG (arg0, 0));
10680 return fold_build2_loc (loc, code, type, iref,
10681 build_int_cst (TREE_TYPE (iref), 0));
10685 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10686 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10687 if (TREE_CODE (arg0) == RSHIFT_EXPR
10688 && integer_zerop (arg1)
10689 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10691 tree arg00 = TREE_OPERAND (arg0, 0);
10692 tree arg01 = TREE_OPERAND (arg0, 1);
10693 tree itype = TREE_TYPE (arg00);
10694 if (wi::eq_p (arg01, element_precision (itype) - 1))
10696 if (TYPE_UNSIGNED (itype))
10698 itype = signed_type_for (itype);
10699 arg00 = fold_convert_loc (loc, itype, arg00);
10701 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10702 type, arg00, build_zero_cst (itype));
10706 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10707 (X & C) == 0 when C is a single bit. */
10708 if (TREE_CODE (arg0) == BIT_AND_EXPR
10709 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10710 && integer_zerop (arg1)
10711 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10713 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10714 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10715 TREE_OPERAND (arg0, 1));
10716 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10717 type, tem,
10718 fold_convert_loc (loc, TREE_TYPE (arg0),
10719 arg1));
10722 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10723 constant C is a power of two, i.e. a single bit. */
10724 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10725 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10726 && integer_zerop (arg1)
10727 && integer_pow2p (TREE_OPERAND (arg0, 1))
10728 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10729 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10731 tree arg00 = TREE_OPERAND (arg0, 0);
10732 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10733 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10736 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10737 when is C is a power of two, i.e. a single bit. */
10738 if (TREE_CODE (arg0) == BIT_AND_EXPR
10739 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10740 && integer_zerop (arg1)
10741 && integer_pow2p (TREE_OPERAND (arg0, 1))
10742 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10743 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10745 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10746 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10747 arg000, TREE_OPERAND (arg0, 1));
10748 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10749 tem, build_int_cst (TREE_TYPE (tem), 0));
10752 if (integer_zerop (arg1)
10753 && tree_expr_nonzero_p (arg0))
10755 tree res = constant_boolean_node (code==NE_EXPR, type);
10756 return omit_one_operand_loc (loc, type, res, arg0);
10759 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10760 if (TREE_CODE (arg0) == BIT_AND_EXPR
10761 && TREE_CODE (arg1) == BIT_AND_EXPR)
10763 tree arg00 = TREE_OPERAND (arg0, 0);
10764 tree arg01 = TREE_OPERAND (arg0, 1);
10765 tree arg10 = TREE_OPERAND (arg1, 0);
10766 tree arg11 = TREE_OPERAND (arg1, 1);
10767 tree itype = TREE_TYPE (arg0);
10769 if (operand_equal_p (arg01, arg11, 0))
10771 tem = fold_convert_loc (loc, itype, arg10);
10772 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10773 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10774 return fold_build2_loc (loc, code, type, tem,
10775 build_zero_cst (itype));
10777 if (operand_equal_p (arg01, arg10, 0))
10779 tem = fold_convert_loc (loc, itype, arg11);
10780 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10781 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10782 return fold_build2_loc (loc, code, type, tem,
10783 build_zero_cst (itype));
10785 if (operand_equal_p (arg00, arg11, 0))
10787 tem = fold_convert_loc (loc, itype, arg10);
10788 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10789 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10790 return fold_build2_loc (loc, code, type, tem,
10791 build_zero_cst (itype));
10793 if (operand_equal_p (arg00, arg10, 0))
10795 tem = fold_convert_loc (loc, itype, arg11);
10796 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10797 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10798 return fold_build2_loc (loc, code, type, tem,
10799 build_zero_cst (itype));
10803 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10804 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10806 tree arg00 = TREE_OPERAND (arg0, 0);
10807 tree arg01 = TREE_OPERAND (arg0, 1);
10808 tree arg10 = TREE_OPERAND (arg1, 0);
10809 tree arg11 = TREE_OPERAND (arg1, 1);
10810 tree itype = TREE_TYPE (arg0);
10812 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10813 operand_equal_p guarantees no side-effects so we don't need
10814 to use omit_one_operand on Z. */
10815 if (operand_equal_p (arg01, arg11, 0))
10816 return fold_build2_loc (loc, code, type, arg00,
10817 fold_convert_loc (loc, TREE_TYPE (arg00),
10818 arg10));
10819 if (operand_equal_p (arg01, arg10, 0))
10820 return fold_build2_loc (loc, code, type, arg00,
10821 fold_convert_loc (loc, TREE_TYPE (arg00),
10822 arg11));
10823 if (operand_equal_p (arg00, arg11, 0))
10824 return fold_build2_loc (loc, code, type, arg01,
10825 fold_convert_loc (loc, TREE_TYPE (arg01),
10826 arg10));
10827 if (operand_equal_p (arg00, arg10, 0))
10828 return fold_build2_loc (loc, code, type, arg01,
10829 fold_convert_loc (loc, TREE_TYPE (arg01),
10830 arg11));
10832 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10833 if (TREE_CODE (arg01) == INTEGER_CST
10834 && TREE_CODE (arg11) == INTEGER_CST)
10836 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10837 fold_convert_loc (loc, itype, arg11));
10838 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10839 return fold_build2_loc (loc, code, type, tem,
10840 fold_convert_loc (loc, itype, arg10));
10844 /* Attempt to simplify equality/inequality comparisons of complex
10845 values. Only lower the comparison if the result is known or
10846 can be simplified to a single scalar comparison. */
10847 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10848 || TREE_CODE (arg0) == COMPLEX_CST)
10849 && (TREE_CODE (arg1) == COMPLEX_EXPR
10850 || TREE_CODE (arg1) == COMPLEX_CST))
10852 tree real0, imag0, real1, imag1;
10853 tree rcond, icond;
10855 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10857 real0 = TREE_OPERAND (arg0, 0);
10858 imag0 = TREE_OPERAND (arg0, 1);
10860 else
10862 real0 = TREE_REALPART (arg0);
10863 imag0 = TREE_IMAGPART (arg0);
10866 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10868 real1 = TREE_OPERAND (arg1, 0);
10869 imag1 = TREE_OPERAND (arg1, 1);
10871 else
10873 real1 = TREE_REALPART (arg1);
10874 imag1 = TREE_IMAGPART (arg1);
10877 rcond = fold_binary_loc (loc, code, type, real0, real1);
10878 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10880 if (integer_zerop (rcond))
10882 if (code == EQ_EXPR)
10883 return omit_two_operands_loc (loc, type, boolean_false_node,
10884 imag0, imag1);
10885 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10887 else
10889 if (code == NE_EXPR)
10890 return omit_two_operands_loc (loc, type, boolean_true_node,
10891 imag0, imag1);
10892 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10896 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10897 if (icond && TREE_CODE (icond) == INTEGER_CST)
10899 if (integer_zerop (icond))
10901 if (code == EQ_EXPR)
10902 return omit_two_operands_loc (loc, type, boolean_false_node,
10903 real0, real1);
10904 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10906 else
10908 if (code == NE_EXPR)
10909 return omit_two_operands_loc (loc, type, boolean_true_node,
10910 real0, real1);
10911 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10916 return NULL_TREE;
10918 case LT_EXPR:
10919 case GT_EXPR:
10920 case LE_EXPR:
10921 case GE_EXPR:
10922 tem = fold_comparison (loc, code, type, op0, op1);
10923 if (tem != NULL_TREE)
10924 return tem;
10926 /* Transform comparisons of the form X +- C CMP X. */
10927 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10928 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10929 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10930 && !HONOR_SNANS (arg0))
10931 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10932 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10934 tree arg01 = TREE_OPERAND (arg0, 1);
10935 enum tree_code code0 = TREE_CODE (arg0);
10936 int is_positive;
10938 if (TREE_CODE (arg01) == REAL_CST)
10939 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10940 else
10941 is_positive = tree_int_cst_sgn (arg01);
10943 /* (X - c) > X becomes false. */
10944 if (code == GT_EXPR
10945 && ((code0 == MINUS_EXPR && is_positive >= 0)
10946 || (code0 == PLUS_EXPR && is_positive <= 0)))
10948 if (TREE_CODE (arg01) == INTEGER_CST
10949 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10950 fold_overflow_warning (("assuming signed overflow does not "
10951 "occur when assuming that (X - c) > X "
10952 "is always false"),
10953 WARN_STRICT_OVERFLOW_ALL);
10954 return constant_boolean_node (0, type);
10957 /* Likewise (X + c) < X becomes false. */
10958 if (code == LT_EXPR
10959 && ((code0 == PLUS_EXPR && is_positive >= 0)
10960 || (code0 == MINUS_EXPR && is_positive <= 0)))
10962 if (TREE_CODE (arg01) == INTEGER_CST
10963 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10964 fold_overflow_warning (("assuming signed overflow does not "
10965 "occur when assuming that "
10966 "(X + c) < X is always false"),
10967 WARN_STRICT_OVERFLOW_ALL);
10968 return constant_boolean_node (0, type);
10971 /* Convert (X - c) <= X to true. */
10972 if (!HONOR_NANS (arg1)
10973 && code == LE_EXPR
10974 && ((code0 == MINUS_EXPR && is_positive >= 0)
10975 || (code0 == PLUS_EXPR && is_positive <= 0)))
10977 if (TREE_CODE (arg01) == INTEGER_CST
10978 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10979 fold_overflow_warning (("assuming signed overflow does not "
10980 "occur when assuming that "
10981 "(X - c) <= X is always true"),
10982 WARN_STRICT_OVERFLOW_ALL);
10983 return constant_boolean_node (1, type);
10986 /* Convert (X + c) >= X to true. */
10987 if (!HONOR_NANS (arg1)
10988 && code == GE_EXPR
10989 && ((code0 == PLUS_EXPR && is_positive >= 0)
10990 || (code0 == MINUS_EXPR && is_positive <= 0)))
10992 if (TREE_CODE (arg01) == INTEGER_CST
10993 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10994 fold_overflow_warning (("assuming signed overflow does not "
10995 "occur when assuming that "
10996 "(X + c) >= X is always true"),
10997 WARN_STRICT_OVERFLOW_ALL);
10998 return constant_boolean_node (1, type);
11001 if (TREE_CODE (arg01) == INTEGER_CST)
11003 /* Convert X + c > X and X - c < X to true for integers. */
11004 if (code == GT_EXPR
11005 && ((code0 == PLUS_EXPR && is_positive > 0)
11006 || (code0 == MINUS_EXPR && is_positive < 0)))
11008 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11009 fold_overflow_warning (("assuming signed overflow does "
11010 "not occur when assuming that "
11011 "(X + c) > X is always true"),
11012 WARN_STRICT_OVERFLOW_ALL);
11013 return constant_boolean_node (1, type);
11016 if (code == LT_EXPR
11017 && ((code0 == MINUS_EXPR && is_positive > 0)
11018 || (code0 == PLUS_EXPR && is_positive < 0)))
11020 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11021 fold_overflow_warning (("assuming signed overflow does "
11022 "not occur when assuming that "
11023 "(X - c) < X is always true"),
11024 WARN_STRICT_OVERFLOW_ALL);
11025 return constant_boolean_node (1, type);
11028 /* Convert X + c <= X and X - c >= X to false for integers. */
11029 if (code == LE_EXPR
11030 && ((code0 == PLUS_EXPR && is_positive > 0)
11031 || (code0 == MINUS_EXPR && is_positive < 0)))
11033 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11034 fold_overflow_warning (("assuming signed overflow does "
11035 "not occur when assuming that "
11036 "(X + c) <= X is always false"),
11037 WARN_STRICT_OVERFLOW_ALL);
11038 return constant_boolean_node (0, type);
11041 if (code == GE_EXPR
11042 && ((code0 == MINUS_EXPR && is_positive > 0)
11043 || (code0 == PLUS_EXPR && is_positive < 0)))
11045 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11046 fold_overflow_warning (("assuming signed overflow does "
11047 "not occur when assuming that "
11048 "(X - c) >= X is always false"),
11049 WARN_STRICT_OVERFLOW_ALL);
11050 return constant_boolean_node (0, type);
11055 /* If we are comparing an ABS_EXPR with a constant, we can
11056 convert all the cases into explicit comparisons, but they may
11057 well not be faster than doing the ABS and one comparison.
11058 But ABS (X) <= C is a range comparison, which becomes a subtraction
11059 and a comparison, and is probably faster. */
11060 if (code == LE_EXPR
11061 && TREE_CODE (arg1) == INTEGER_CST
11062 && TREE_CODE (arg0) == ABS_EXPR
11063 && ! TREE_SIDE_EFFECTS (arg0)
11064 && (0 != (tem = negate_expr (arg1)))
11065 && TREE_CODE (tem) == INTEGER_CST
11066 && !TREE_OVERFLOW (tem))
11067 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11068 build2 (GE_EXPR, type,
11069 TREE_OPERAND (arg0, 0), tem),
11070 build2 (LE_EXPR, type,
11071 TREE_OPERAND (arg0, 0), arg1));
11073 /* Convert ABS_EXPR<x> >= 0 to true. */
11074 strict_overflow_p = false;
11075 if (code == GE_EXPR
11076 && (integer_zerop (arg1)
11077 || (! HONOR_NANS (arg0)
11078 && real_zerop (arg1)))
11079 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11081 if (strict_overflow_p)
11082 fold_overflow_warning (("assuming signed overflow does not occur "
11083 "when simplifying comparison of "
11084 "absolute value and zero"),
11085 WARN_STRICT_OVERFLOW_CONDITIONAL);
11086 return omit_one_operand_loc (loc, type,
11087 constant_boolean_node (true, type),
11088 arg0);
11091 /* Convert ABS_EXPR<x> < 0 to false. */
11092 strict_overflow_p = false;
11093 if (code == LT_EXPR
11094 && (integer_zerop (arg1) || real_zerop (arg1))
11095 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11097 if (strict_overflow_p)
11098 fold_overflow_warning (("assuming signed overflow does not occur "
11099 "when simplifying comparison of "
11100 "absolute value and zero"),
11101 WARN_STRICT_OVERFLOW_CONDITIONAL);
11102 return omit_one_operand_loc (loc, type,
11103 constant_boolean_node (false, type),
11104 arg0);
11107 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11108 and similarly for >= into !=. */
11109 if ((code == LT_EXPR || code == GE_EXPR)
11110 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11111 && TREE_CODE (arg1) == LSHIFT_EXPR
11112 && integer_onep (TREE_OPERAND (arg1, 0)))
11113 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11114 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11115 TREE_OPERAND (arg1, 1)),
11116 build_zero_cst (TREE_TYPE (arg0)));
11118 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11119 otherwise Y might be >= # of bits in X's type and thus e.g.
11120 (unsigned char) (1 << Y) for Y 15 might be 0.
11121 If the cast is widening, then 1 << Y should have unsigned type,
11122 otherwise if Y is number of bits in the signed shift type minus 1,
11123 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11124 31 might be 0xffffffff80000000. */
11125 if ((code == LT_EXPR || code == GE_EXPR)
11126 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11127 && CONVERT_EXPR_P (arg1)
11128 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11129 && (element_precision (TREE_TYPE (arg1))
11130 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11131 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11132 || (element_precision (TREE_TYPE (arg1))
11133 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11134 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11136 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11137 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11138 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11139 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11140 build_zero_cst (TREE_TYPE (arg0)));
11143 return NULL_TREE;
11145 case UNORDERED_EXPR:
11146 case ORDERED_EXPR:
11147 case UNLT_EXPR:
11148 case UNLE_EXPR:
11149 case UNGT_EXPR:
11150 case UNGE_EXPR:
11151 case UNEQ_EXPR:
11152 case LTGT_EXPR:
11153 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11155 tree targ0 = strip_float_extensions (arg0);
11156 tree targ1 = strip_float_extensions (arg1);
11157 tree newtype = TREE_TYPE (targ0);
11159 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11160 newtype = TREE_TYPE (targ1);
11162 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11163 return fold_build2_loc (loc, code, type,
11164 fold_convert_loc (loc, newtype, targ0),
11165 fold_convert_loc (loc, newtype, targ1));
11168 return NULL_TREE;
11170 case COMPOUND_EXPR:
11171 /* When pedantic, a compound expression can be neither an lvalue
11172 nor an integer constant expression. */
11173 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11174 return NULL_TREE;
11175 /* Don't let (0, 0) be null pointer constant. */
11176 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11177 : fold_convert_loc (loc, type, arg1);
11178 return pedantic_non_lvalue_loc (loc, tem);
11180 case ASSERT_EXPR:
11181 /* An ASSERT_EXPR should never be passed to fold_binary. */
11182 gcc_unreachable ();
11184 default:
11185 return NULL_TREE;
11186 } /* switch (code) */
11189 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11190 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11191 of GOTO_EXPR. */
11193 static tree
11194 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11196 switch (TREE_CODE (*tp))
11198 case LABEL_EXPR:
11199 return *tp;
11201 case GOTO_EXPR:
11202 *walk_subtrees = 0;
11204 /* fall through */
11206 default:
11207 return NULL_TREE;
11211 /* Return whether the sub-tree ST contains a label which is accessible from
11212 outside the sub-tree. */
11214 static bool
11215 contains_label_p (tree st)
11217 return
11218 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11221 /* Fold a ternary expression of code CODE and type TYPE with operands
11222 OP0, OP1, and OP2. Return the folded expression if folding is
11223 successful. Otherwise, return NULL_TREE. */
11225 tree
11226 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11227 tree op0, tree op1, tree op2)
11229 tree tem;
11230 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11231 enum tree_code_class kind = TREE_CODE_CLASS (code);
11233 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11234 && TREE_CODE_LENGTH (code) == 3);
11236 /* If this is a commutative operation, and OP0 is a constant, move it
11237 to OP1 to reduce the number of tests below. */
11238 if (commutative_ternary_tree_code (code)
11239 && tree_swap_operands_p (op0, op1))
11240 return fold_build3_loc (loc, code, type, op1, op0, op2);
11242 tem = generic_simplify (loc, code, type, op0, op1, op2);
11243 if (tem)
11244 return tem;
11246 /* Strip any conversions that don't change the mode. This is safe
11247 for every expression, except for a comparison expression because
11248 its signedness is derived from its operands. So, in the latter
11249 case, only strip conversions that don't change the signedness.
11251 Note that this is done as an internal manipulation within the
11252 constant folder, in order to find the simplest representation of
11253 the arguments so that their form can be studied. In any cases,
11254 the appropriate type conversions should be put back in the tree
11255 that will get out of the constant folder. */
11256 if (op0)
11258 arg0 = op0;
11259 STRIP_NOPS (arg0);
11262 if (op1)
11264 arg1 = op1;
11265 STRIP_NOPS (arg1);
11268 if (op2)
11270 arg2 = op2;
11271 STRIP_NOPS (arg2);
11274 switch (code)
11276 case COMPONENT_REF:
11277 if (TREE_CODE (arg0) == CONSTRUCTOR
11278 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11280 unsigned HOST_WIDE_INT idx;
11281 tree field, value;
11282 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11283 if (field == arg1)
11284 return value;
11286 return NULL_TREE;
11288 case COND_EXPR:
11289 case VEC_COND_EXPR:
11290 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11291 so all simple results must be passed through pedantic_non_lvalue. */
11292 if (TREE_CODE (arg0) == INTEGER_CST)
11294 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11295 tem = integer_zerop (arg0) ? op2 : op1;
11296 /* Only optimize constant conditions when the selected branch
11297 has the same type as the COND_EXPR. This avoids optimizing
11298 away "c ? x : throw", where the throw has a void type.
11299 Avoid throwing away that operand which contains label. */
11300 if ((!TREE_SIDE_EFFECTS (unused_op)
11301 || !contains_label_p (unused_op))
11302 && (! VOID_TYPE_P (TREE_TYPE (tem))
11303 || VOID_TYPE_P (type)))
11304 return pedantic_non_lvalue_loc (loc, tem);
11305 return NULL_TREE;
11307 else if (TREE_CODE (arg0) == VECTOR_CST)
11309 if ((TREE_CODE (arg1) == VECTOR_CST
11310 || TREE_CODE (arg1) == CONSTRUCTOR)
11311 && (TREE_CODE (arg2) == VECTOR_CST
11312 || TREE_CODE (arg2) == CONSTRUCTOR))
11314 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11315 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11316 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11317 for (i = 0; i < nelts; i++)
11319 tree val = VECTOR_CST_ELT (arg0, i);
11320 if (integer_all_onesp (val))
11321 sel[i] = i;
11322 else if (integer_zerop (val))
11323 sel[i] = nelts + i;
11324 else /* Currently unreachable. */
11325 return NULL_TREE;
11327 tree t = fold_vec_perm (type, arg1, arg2, sel);
11328 if (t != NULL_TREE)
11329 return t;
11333 /* If we have A op B ? A : C, we may be able to convert this to a
11334 simpler expression, depending on the operation and the values
11335 of B and C. Signed zeros prevent all of these transformations,
11336 for reasons given above each one.
11338 Also try swapping the arguments and inverting the conditional. */
11339 if (COMPARISON_CLASS_P (arg0)
11340 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), arg1)
11341 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11343 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11344 if (tem)
11345 return tem;
11348 if (COMPARISON_CLASS_P (arg0)
11349 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11350 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11352 location_t loc0 = expr_location_or (arg0, loc);
11353 tem = fold_invert_truthvalue (loc0, arg0);
11354 if (tem && COMPARISON_CLASS_P (tem))
11356 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11357 if (tem)
11358 return tem;
11362 /* If the second operand is simpler than the third, swap them
11363 since that produces better jump optimization results. */
11364 if (truth_value_p (TREE_CODE (arg0))
11365 && tree_swap_operands_p (op1, op2))
11367 location_t loc0 = expr_location_or (arg0, loc);
11368 /* See if this can be inverted. If it can't, possibly because
11369 it was a floating-point inequality comparison, don't do
11370 anything. */
11371 tem = fold_invert_truthvalue (loc0, arg0);
11372 if (tem)
11373 return fold_build3_loc (loc, code, type, tem, op2, op1);
11376 /* Convert A ? 1 : 0 to simply A. */
11377 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11378 : (integer_onep (op1)
11379 && !VECTOR_TYPE_P (type)))
11380 && integer_zerop (op2)
11381 /* If we try to convert OP0 to our type, the
11382 call to fold will try to move the conversion inside
11383 a COND, which will recurse. In that case, the COND_EXPR
11384 is probably the best choice, so leave it alone. */
11385 && type == TREE_TYPE (arg0))
11386 return pedantic_non_lvalue_loc (loc, arg0);
11388 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11389 over COND_EXPR in cases such as floating point comparisons. */
11390 if (integer_zerop (op1)
11391 && code == COND_EXPR
11392 && integer_onep (op2)
11393 && !VECTOR_TYPE_P (type)
11394 && truth_value_p (TREE_CODE (arg0)))
11395 return pedantic_non_lvalue_loc (loc,
11396 fold_convert_loc (loc, type,
11397 invert_truthvalue_loc (loc,
11398 arg0)));
11400 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11401 if (TREE_CODE (arg0) == LT_EXPR
11402 && integer_zerop (TREE_OPERAND (arg0, 1))
11403 && integer_zerop (op2)
11404 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11406 /* sign_bit_p looks through both zero and sign extensions,
11407 but for this optimization only sign extensions are
11408 usable. */
11409 tree tem2 = TREE_OPERAND (arg0, 0);
11410 while (tem != tem2)
11412 if (TREE_CODE (tem2) != NOP_EXPR
11413 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11415 tem = NULL_TREE;
11416 break;
11418 tem2 = TREE_OPERAND (tem2, 0);
11420 /* sign_bit_p only checks ARG1 bits within A's precision.
11421 If <sign bit of A> has wider type than A, bits outside
11422 of A's precision in <sign bit of A> need to be checked.
11423 If they are all 0, this optimization needs to be done
11424 in unsigned A's type, if they are all 1 in signed A's type,
11425 otherwise this can't be done. */
11426 if (tem
11427 && TYPE_PRECISION (TREE_TYPE (tem))
11428 < TYPE_PRECISION (TREE_TYPE (arg1))
11429 && TYPE_PRECISION (TREE_TYPE (tem))
11430 < TYPE_PRECISION (type))
11432 int inner_width, outer_width;
11433 tree tem_type;
11435 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11436 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11437 if (outer_width > TYPE_PRECISION (type))
11438 outer_width = TYPE_PRECISION (type);
11440 wide_int mask = wi::shifted_mask
11441 (inner_width, outer_width - inner_width, false,
11442 TYPE_PRECISION (TREE_TYPE (arg1)));
11444 wide_int common = mask & arg1;
11445 if (common == mask)
11447 tem_type = signed_type_for (TREE_TYPE (tem));
11448 tem = fold_convert_loc (loc, tem_type, tem);
11450 else if (common == 0)
11452 tem_type = unsigned_type_for (TREE_TYPE (tem));
11453 tem = fold_convert_loc (loc, tem_type, tem);
11455 else
11456 tem = NULL;
11459 if (tem)
11460 return
11461 fold_convert_loc (loc, type,
11462 fold_build2_loc (loc, BIT_AND_EXPR,
11463 TREE_TYPE (tem), tem,
11464 fold_convert_loc (loc,
11465 TREE_TYPE (tem),
11466 arg1)));
11469 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11470 already handled above. */
11471 if (TREE_CODE (arg0) == BIT_AND_EXPR
11472 && integer_onep (TREE_OPERAND (arg0, 1))
11473 && integer_zerop (op2)
11474 && integer_pow2p (arg1))
11476 tree tem = TREE_OPERAND (arg0, 0);
11477 STRIP_NOPS (tem);
11478 if (TREE_CODE (tem) == RSHIFT_EXPR
11479 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11480 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11481 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11482 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11483 fold_convert_loc (loc, type,
11484 TREE_OPERAND (tem, 0)),
11485 op1);
11488 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11489 is probably obsolete because the first operand should be a
11490 truth value (that's why we have the two cases above), but let's
11491 leave it in until we can confirm this for all front-ends. */
11492 if (integer_zerop (op2)
11493 && TREE_CODE (arg0) == NE_EXPR
11494 && integer_zerop (TREE_OPERAND (arg0, 1))
11495 && integer_pow2p (arg1)
11496 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11497 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11498 arg1, OEP_ONLY_CONST))
11499 return pedantic_non_lvalue_loc (loc,
11500 fold_convert_loc (loc, type,
11501 TREE_OPERAND (arg0, 0)));
11503 /* Disable the transformations below for vectors, since
11504 fold_binary_op_with_conditional_arg may undo them immediately,
11505 yielding an infinite loop. */
11506 if (code == VEC_COND_EXPR)
11507 return NULL_TREE;
11509 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11510 if (integer_zerop (op2)
11511 && truth_value_p (TREE_CODE (arg0))
11512 && truth_value_p (TREE_CODE (arg1))
11513 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11514 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11515 : TRUTH_ANDIF_EXPR,
11516 type, fold_convert_loc (loc, type, arg0), op1);
11518 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11519 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11520 && truth_value_p (TREE_CODE (arg0))
11521 && truth_value_p (TREE_CODE (arg1))
11522 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11524 location_t loc0 = expr_location_or (arg0, loc);
11525 /* Only perform transformation if ARG0 is easily inverted. */
11526 tem = fold_invert_truthvalue (loc0, arg0);
11527 if (tem)
11528 return fold_build2_loc (loc, code == VEC_COND_EXPR
11529 ? BIT_IOR_EXPR
11530 : TRUTH_ORIF_EXPR,
11531 type, fold_convert_loc (loc, type, tem),
11532 op1);
11535 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11536 if (integer_zerop (arg1)
11537 && truth_value_p (TREE_CODE (arg0))
11538 && truth_value_p (TREE_CODE (op2))
11539 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11541 location_t loc0 = expr_location_or (arg0, loc);
11542 /* Only perform transformation if ARG0 is easily inverted. */
11543 tem = fold_invert_truthvalue (loc0, arg0);
11544 if (tem)
11545 return fold_build2_loc (loc, code == VEC_COND_EXPR
11546 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11547 type, fold_convert_loc (loc, type, tem),
11548 op2);
11551 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11552 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11553 && truth_value_p (TREE_CODE (arg0))
11554 && truth_value_p (TREE_CODE (op2))
11555 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11556 return fold_build2_loc (loc, code == VEC_COND_EXPR
11557 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11558 type, fold_convert_loc (loc, type, arg0), op2);
11560 return NULL_TREE;
11562 case CALL_EXPR:
11563 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11564 of fold_ternary on them. */
11565 gcc_unreachable ();
11567 case BIT_FIELD_REF:
11568 if (TREE_CODE (arg0) == VECTOR_CST
11569 && (type == TREE_TYPE (TREE_TYPE (arg0))
11570 || (TREE_CODE (type) == VECTOR_TYPE
11571 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11573 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11574 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11575 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11576 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11578 if (n != 0
11579 && (idx % width) == 0
11580 && (n % width) == 0
11581 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11583 idx = idx / width;
11584 n = n / width;
11586 if (TREE_CODE (arg0) == VECTOR_CST)
11588 if (n == 1)
11589 return VECTOR_CST_ELT (arg0, idx);
11591 tree *vals = XALLOCAVEC (tree, n);
11592 for (unsigned i = 0; i < n; ++i)
11593 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11594 return build_vector (type, vals);
11599 /* On constants we can use native encode/interpret to constant
11600 fold (nearly) all BIT_FIELD_REFs. */
11601 if (CONSTANT_CLASS_P (arg0)
11602 && can_native_interpret_type_p (type)
11603 && BITS_PER_UNIT == 8)
11605 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11606 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11607 /* Limit us to a reasonable amount of work. To relax the
11608 other limitations we need bit-shifting of the buffer
11609 and rounding up the size. */
11610 if (bitpos % BITS_PER_UNIT == 0
11611 && bitsize % BITS_PER_UNIT == 0
11612 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11614 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11615 unsigned HOST_WIDE_INT len
11616 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11617 bitpos / BITS_PER_UNIT);
11618 if (len > 0
11619 && len * BITS_PER_UNIT >= bitsize)
11621 tree v = native_interpret_expr (type, b,
11622 bitsize / BITS_PER_UNIT);
11623 if (v)
11624 return v;
11629 return NULL_TREE;
11631 case FMA_EXPR:
11632 /* For integers we can decompose the FMA if possible. */
11633 if (TREE_CODE (arg0) == INTEGER_CST
11634 && TREE_CODE (arg1) == INTEGER_CST)
11635 return fold_build2_loc (loc, PLUS_EXPR, type,
11636 const_binop (MULT_EXPR, arg0, arg1), arg2);
11637 if (integer_zerop (arg2))
11638 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11640 return fold_fma (loc, type, arg0, arg1, arg2);
11642 case VEC_PERM_EXPR:
11643 if (TREE_CODE (arg2) == VECTOR_CST)
11645 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11646 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11647 unsigned char *sel2 = sel + nelts;
11648 bool need_mask_canon = false;
11649 bool need_mask_canon2 = false;
11650 bool all_in_vec0 = true;
11651 bool all_in_vec1 = true;
11652 bool maybe_identity = true;
11653 bool single_arg = (op0 == op1);
11654 bool changed = false;
11656 mask2 = 2 * nelts - 1;
11657 mask = single_arg ? (nelts - 1) : mask2;
11658 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11659 for (i = 0; i < nelts; i++)
11661 tree val = VECTOR_CST_ELT (arg2, i);
11662 if (TREE_CODE (val) != INTEGER_CST)
11663 return NULL_TREE;
11665 /* Make sure that the perm value is in an acceptable
11666 range. */
11667 wide_int t = val;
11668 need_mask_canon |= wi::gtu_p (t, mask);
11669 need_mask_canon2 |= wi::gtu_p (t, mask2);
11670 sel[i] = t.to_uhwi () & mask;
11671 sel2[i] = t.to_uhwi () & mask2;
11673 if (sel[i] < nelts)
11674 all_in_vec1 = false;
11675 else
11676 all_in_vec0 = false;
11678 if ((sel[i] & (nelts-1)) != i)
11679 maybe_identity = false;
11682 if (maybe_identity)
11684 if (all_in_vec0)
11685 return op0;
11686 if (all_in_vec1)
11687 return op1;
11690 if (all_in_vec0)
11691 op1 = op0;
11692 else if (all_in_vec1)
11694 op0 = op1;
11695 for (i = 0; i < nelts; i++)
11696 sel[i] -= nelts;
11697 need_mask_canon = true;
11700 if ((TREE_CODE (op0) == VECTOR_CST
11701 || TREE_CODE (op0) == CONSTRUCTOR)
11702 && (TREE_CODE (op1) == VECTOR_CST
11703 || TREE_CODE (op1) == CONSTRUCTOR))
11705 tree t = fold_vec_perm (type, op0, op1, sel);
11706 if (t != NULL_TREE)
11707 return t;
11710 if (op0 == op1 && !single_arg)
11711 changed = true;
11713 /* Some targets are deficient and fail to expand a single
11714 argument permutation while still allowing an equivalent
11715 2-argument version. */
11716 if (need_mask_canon && arg2 == op2
11717 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11718 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11720 need_mask_canon = need_mask_canon2;
11721 sel = sel2;
11724 if (need_mask_canon && arg2 == op2)
11726 tree *tsel = XALLOCAVEC (tree, nelts);
11727 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11728 for (i = 0; i < nelts; i++)
11729 tsel[i] = build_int_cst (eltype, sel[i]);
11730 op2 = build_vector (TREE_TYPE (arg2), tsel);
11731 changed = true;
11734 if (changed)
11735 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11737 return NULL_TREE;
11739 case BIT_INSERT_EXPR:
11740 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11741 if (TREE_CODE (arg0) == INTEGER_CST
11742 && TREE_CODE (arg1) == INTEGER_CST)
11744 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11745 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11746 wide_int tem = wi::bit_and (arg0,
11747 wi::shifted_mask (bitpos, bitsize, true,
11748 TYPE_PRECISION (type)));
11749 wide_int tem2
11750 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11751 bitsize), bitpos);
11752 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11754 else if (TREE_CODE (arg0) == VECTOR_CST
11755 && CONSTANT_CLASS_P (arg1)
11756 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11757 TREE_TYPE (arg1)))
11759 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11760 unsigned HOST_WIDE_INT elsize
11761 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11762 if (bitpos % elsize == 0)
11764 unsigned k = bitpos / elsize;
11765 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11766 return arg0;
11767 else
11769 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11770 memcpy (elts, VECTOR_CST_ELTS (arg0),
11771 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11772 elts[k] = arg1;
11773 return build_vector (type, elts);
11777 return NULL_TREE;
11779 default:
11780 return NULL_TREE;
11781 } /* switch (code) */
11784 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11785 of an array (or vector). */
11787 tree
11788 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11790 tree index_type = NULL_TREE;
11791 offset_int low_bound = 0;
11793 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11795 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11796 if (domain_type && TYPE_MIN_VALUE (domain_type))
11798 /* Static constructors for variably sized objects makes no sense. */
11799 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11800 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11801 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11805 if (index_type)
11806 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11807 TYPE_SIGN (index_type));
11809 offset_int index = low_bound - 1;
11810 if (index_type)
11811 index = wi::ext (index, TYPE_PRECISION (index_type),
11812 TYPE_SIGN (index_type));
11814 offset_int max_index;
11815 unsigned HOST_WIDE_INT cnt;
11816 tree cfield, cval;
11818 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11820 /* Array constructor might explicitly set index, or specify a range,
11821 or leave index NULL meaning that it is next index after previous
11822 one. */
11823 if (cfield)
11825 if (TREE_CODE (cfield) == INTEGER_CST)
11826 max_index = index = wi::to_offset (cfield);
11827 else
11829 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11830 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11831 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11834 else
11836 index += 1;
11837 if (index_type)
11838 index = wi::ext (index, TYPE_PRECISION (index_type),
11839 TYPE_SIGN (index_type));
11840 max_index = index;
11843 /* Do we have match? */
11844 if (wi::cmpu (access_index, index) >= 0
11845 && wi::cmpu (access_index, max_index) <= 0)
11846 return cval;
11848 return NULL_TREE;
11851 /* Perform constant folding and related simplification of EXPR.
11852 The related simplifications include x*1 => x, x*0 => 0, etc.,
11853 and application of the associative law.
11854 NOP_EXPR conversions may be removed freely (as long as we
11855 are careful not to change the type of the overall expression).
11856 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11857 but we can constant-fold them if they have constant operands. */
11859 #ifdef ENABLE_FOLD_CHECKING
11860 # define fold(x) fold_1 (x)
11861 static tree fold_1 (tree);
11862 static
11863 #endif
11864 tree
11865 fold (tree expr)
11867 const tree t = expr;
11868 enum tree_code code = TREE_CODE (t);
11869 enum tree_code_class kind = TREE_CODE_CLASS (code);
11870 tree tem;
11871 location_t loc = EXPR_LOCATION (expr);
11873 /* Return right away if a constant. */
11874 if (kind == tcc_constant)
11875 return t;
11877 /* CALL_EXPR-like objects with variable numbers of operands are
11878 treated specially. */
11879 if (kind == tcc_vl_exp)
11881 if (code == CALL_EXPR)
11883 tem = fold_call_expr (loc, expr, false);
11884 return tem ? tem : expr;
11886 return expr;
11889 if (IS_EXPR_CODE_CLASS (kind))
11891 tree type = TREE_TYPE (t);
11892 tree op0, op1, op2;
11894 switch (TREE_CODE_LENGTH (code))
11896 case 1:
11897 op0 = TREE_OPERAND (t, 0);
11898 tem = fold_unary_loc (loc, code, type, op0);
11899 return tem ? tem : expr;
11900 case 2:
11901 op0 = TREE_OPERAND (t, 0);
11902 op1 = TREE_OPERAND (t, 1);
11903 tem = fold_binary_loc (loc, code, type, op0, op1);
11904 return tem ? tem : expr;
11905 case 3:
11906 op0 = TREE_OPERAND (t, 0);
11907 op1 = TREE_OPERAND (t, 1);
11908 op2 = TREE_OPERAND (t, 2);
11909 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11910 return tem ? tem : expr;
11911 default:
11912 break;
11916 switch (code)
11918 case ARRAY_REF:
11920 tree op0 = TREE_OPERAND (t, 0);
11921 tree op1 = TREE_OPERAND (t, 1);
11923 if (TREE_CODE (op1) == INTEGER_CST
11924 && TREE_CODE (op0) == CONSTRUCTOR
11925 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11927 tree val = get_array_ctor_element_at_index (op0,
11928 wi::to_offset (op1));
11929 if (val)
11930 return val;
11933 return t;
11936 /* Return a VECTOR_CST if possible. */
11937 case CONSTRUCTOR:
11939 tree type = TREE_TYPE (t);
11940 if (TREE_CODE (type) != VECTOR_TYPE)
11941 return t;
11943 unsigned i;
11944 tree val;
11945 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11946 if (! CONSTANT_CLASS_P (val))
11947 return t;
11949 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11952 case CONST_DECL:
11953 return fold (DECL_INITIAL (t));
11955 default:
11956 return t;
11957 } /* switch (code) */
11960 #ifdef ENABLE_FOLD_CHECKING
11961 #undef fold
11963 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11964 hash_table<nofree_ptr_hash<const tree_node> > *);
11965 static void fold_check_failed (const_tree, const_tree);
11966 void print_fold_checksum (const_tree);
11968 /* When --enable-checking=fold, compute a digest of expr before
11969 and after actual fold call to see if fold did not accidentally
11970 change original expr. */
11972 tree
11973 fold (tree expr)
11975 tree ret;
11976 struct md5_ctx ctx;
11977 unsigned char checksum_before[16], checksum_after[16];
11978 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11980 md5_init_ctx (&ctx);
11981 fold_checksum_tree (expr, &ctx, &ht);
11982 md5_finish_ctx (&ctx, checksum_before);
11983 ht.empty ();
11985 ret = fold_1 (expr);
11987 md5_init_ctx (&ctx);
11988 fold_checksum_tree (expr, &ctx, &ht);
11989 md5_finish_ctx (&ctx, checksum_after);
11991 if (memcmp (checksum_before, checksum_after, 16))
11992 fold_check_failed (expr, ret);
11994 return ret;
11997 void
11998 print_fold_checksum (const_tree expr)
12000 struct md5_ctx ctx;
12001 unsigned char checksum[16], cnt;
12002 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12004 md5_init_ctx (&ctx);
12005 fold_checksum_tree (expr, &ctx, &ht);
12006 md5_finish_ctx (&ctx, checksum);
12007 for (cnt = 0; cnt < 16; ++cnt)
12008 fprintf (stderr, "%02x", checksum[cnt]);
12009 putc ('\n', stderr);
12012 static void
12013 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12015 internal_error ("fold check: original tree changed by fold");
12018 static void
12019 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12020 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12022 const tree_node **slot;
12023 enum tree_code code;
12024 union tree_node buf;
12025 int i, len;
12027 recursive_label:
12028 if (expr == NULL)
12029 return;
12030 slot = ht->find_slot (expr, INSERT);
12031 if (*slot != NULL)
12032 return;
12033 *slot = expr;
12034 code = TREE_CODE (expr);
12035 if (TREE_CODE_CLASS (code) == tcc_declaration
12036 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12038 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12039 memcpy ((char *) &buf, expr, tree_size (expr));
12040 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12041 buf.decl_with_vis.symtab_node = NULL;
12042 expr = (tree) &buf;
12044 else if (TREE_CODE_CLASS (code) == tcc_type
12045 && (TYPE_POINTER_TO (expr)
12046 || TYPE_REFERENCE_TO (expr)
12047 || TYPE_CACHED_VALUES_P (expr)
12048 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12049 || TYPE_NEXT_VARIANT (expr)
12050 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12052 /* Allow these fields to be modified. */
12053 tree tmp;
12054 memcpy ((char *) &buf, expr, tree_size (expr));
12055 expr = tmp = (tree) &buf;
12056 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12057 TYPE_POINTER_TO (tmp) = NULL;
12058 TYPE_REFERENCE_TO (tmp) = NULL;
12059 TYPE_NEXT_VARIANT (tmp) = NULL;
12060 TYPE_ALIAS_SET (tmp) = -1;
12061 if (TYPE_CACHED_VALUES_P (tmp))
12063 TYPE_CACHED_VALUES_P (tmp) = 0;
12064 TYPE_CACHED_VALUES (tmp) = NULL;
12067 md5_process_bytes (expr, tree_size (expr), ctx);
12068 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12069 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12070 if (TREE_CODE_CLASS (code) != tcc_type
12071 && TREE_CODE_CLASS (code) != tcc_declaration
12072 && code != TREE_LIST
12073 && code != SSA_NAME
12074 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12075 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12076 switch (TREE_CODE_CLASS (code))
12078 case tcc_constant:
12079 switch (code)
12081 case STRING_CST:
12082 md5_process_bytes (TREE_STRING_POINTER (expr),
12083 TREE_STRING_LENGTH (expr), ctx);
12084 break;
12085 case COMPLEX_CST:
12086 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12087 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12088 break;
12089 case VECTOR_CST:
12090 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12091 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12092 break;
12093 default:
12094 break;
12096 break;
12097 case tcc_exceptional:
12098 switch (code)
12100 case TREE_LIST:
12101 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12102 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12103 expr = TREE_CHAIN (expr);
12104 goto recursive_label;
12105 break;
12106 case TREE_VEC:
12107 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12108 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12109 break;
12110 default:
12111 break;
12113 break;
12114 case tcc_expression:
12115 case tcc_reference:
12116 case tcc_comparison:
12117 case tcc_unary:
12118 case tcc_binary:
12119 case tcc_statement:
12120 case tcc_vl_exp:
12121 len = TREE_OPERAND_LENGTH (expr);
12122 for (i = 0; i < len; ++i)
12123 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12124 break;
12125 case tcc_declaration:
12126 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12127 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12128 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12130 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12131 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12132 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12133 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12134 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12137 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12139 if (TREE_CODE (expr) == FUNCTION_DECL)
12141 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12142 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12144 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12146 break;
12147 case tcc_type:
12148 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12149 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12150 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12151 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12152 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12153 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12154 if (INTEGRAL_TYPE_P (expr)
12155 || SCALAR_FLOAT_TYPE_P (expr))
12157 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12158 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12160 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12161 if (TREE_CODE (expr) == RECORD_TYPE
12162 || TREE_CODE (expr) == UNION_TYPE
12163 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12164 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12165 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12166 break;
12167 default:
12168 break;
12172 /* Helper function for outputting the checksum of a tree T. When
12173 debugging with gdb, you can "define mynext" to be "next" followed
12174 by "call debug_fold_checksum (op0)", then just trace down till the
12175 outputs differ. */
12177 DEBUG_FUNCTION void
12178 debug_fold_checksum (const_tree t)
12180 int i;
12181 unsigned char checksum[16];
12182 struct md5_ctx ctx;
12183 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12185 md5_init_ctx (&ctx);
12186 fold_checksum_tree (t, &ctx, &ht);
12187 md5_finish_ctx (&ctx, checksum);
12188 ht.empty ();
12190 for (i = 0; i < 16; i++)
12191 fprintf (stderr, "%d ", checksum[i]);
12193 fprintf (stderr, "\n");
12196 #endif
12198 /* Fold a unary tree expression with code CODE of type TYPE with an
12199 operand OP0. LOC is the location of the resulting expression.
12200 Return a folded expression if successful. Otherwise, return a tree
12201 expression with code CODE of type TYPE with an operand OP0. */
12203 tree
12204 fold_build1_loc (location_t loc,
12205 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12207 tree tem;
12208 #ifdef ENABLE_FOLD_CHECKING
12209 unsigned char checksum_before[16], checksum_after[16];
12210 struct md5_ctx ctx;
12211 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12213 md5_init_ctx (&ctx);
12214 fold_checksum_tree (op0, &ctx, &ht);
12215 md5_finish_ctx (&ctx, checksum_before);
12216 ht.empty ();
12217 #endif
12219 tem = fold_unary_loc (loc, code, type, op0);
12220 if (!tem)
12221 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12223 #ifdef ENABLE_FOLD_CHECKING
12224 md5_init_ctx (&ctx);
12225 fold_checksum_tree (op0, &ctx, &ht);
12226 md5_finish_ctx (&ctx, checksum_after);
12228 if (memcmp (checksum_before, checksum_after, 16))
12229 fold_check_failed (op0, tem);
12230 #endif
12231 return tem;
12234 /* Fold a binary tree expression with code CODE of type TYPE with
12235 operands OP0 and OP1. LOC is the location of the resulting
12236 expression. Return a folded expression if successful. Otherwise,
12237 return a tree expression with code CODE of type TYPE with operands
12238 OP0 and OP1. */
12240 tree
12241 fold_build2_loc (location_t loc,
12242 enum tree_code code, tree type, tree op0, tree op1
12243 MEM_STAT_DECL)
12245 tree tem;
12246 #ifdef ENABLE_FOLD_CHECKING
12247 unsigned char checksum_before_op0[16],
12248 checksum_before_op1[16],
12249 checksum_after_op0[16],
12250 checksum_after_op1[16];
12251 struct md5_ctx ctx;
12252 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12254 md5_init_ctx (&ctx);
12255 fold_checksum_tree (op0, &ctx, &ht);
12256 md5_finish_ctx (&ctx, checksum_before_op0);
12257 ht.empty ();
12259 md5_init_ctx (&ctx);
12260 fold_checksum_tree (op1, &ctx, &ht);
12261 md5_finish_ctx (&ctx, checksum_before_op1);
12262 ht.empty ();
12263 #endif
12265 tem = fold_binary_loc (loc, code, type, op0, op1);
12266 if (!tem)
12267 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12269 #ifdef ENABLE_FOLD_CHECKING
12270 md5_init_ctx (&ctx);
12271 fold_checksum_tree (op0, &ctx, &ht);
12272 md5_finish_ctx (&ctx, checksum_after_op0);
12273 ht.empty ();
12275 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12276 fold_check_failed (op0, tem);
12278 md5_init_ctx (&ctx);
12279 fold_checksum_tree (op1, &ctx, &ht);
12280 md5_finish_ctx (&ctx, checksum_after_op1);
12282 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12283 fold_check_failed (op1, tem);
12284 #endif
12285 return tem;
12288 /* Fold a ternary tree expression with code CODE of type TYPE with
12289 operands OP0, OP1, and OP2. Return a folded expression if
12290 successful. Otherwise, return a tree expression with code CODE of
12291 type TYPE with operands OP0, OP1, and OP2. */
12293 tree
12294 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12295 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12297 tree tem;
12298 #ifdef ENABLE_FOLD_CHECKING
12299 unsigned char checksum_before_op0[16],
12300 checksum_before_op1[16],
12301 checksum_before_op2[16],
12302 checksum_after_op0[16],
12303 checksum_after_op1[16],
12304 checksum_after_op2[16];
12305 struct md5_ctx ctx;
12306 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12308 md5_init_ctx (&ctx);
12309 fold_checksum_tree (op0, &ctx, &ht);
12310 md5_finish_ctx (&ctx, checksum_before_op0);
12311 ht.empty ();
12313 md5_init_ctx (&ctx);
12314 fold_checksum_tree (op1, &ctx, &ht);
12315 md5_finish_ctx (&ctx, checksum_before_op1);
12316 ht.empty ();
12318 md5_init_ctx (&ctx);
12319 fold_checksum_tree (op2, &ctx, &ht);
12320 md5_finish_ctx (&ctx, checksum_before_op2);
12321 ht.empty ();
12322 #endif
12324 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12325 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12326 if (!tem)
12327 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12329 #ifdef ENABLE_FOLD_CHECKING
12330 md5_init_ctx (&ctx);
12331 fold_checksum_tree (op0, &ctx, &ht);
12332 md5_finish_ctx (&ctx, checksum_after_op0);
12333 ht.empty ();
12335 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12336 fold_check_failed (op0, tem);
12338 md5_init_ctx (&ctx);
12339 fold_checksum_tree (op1, &ctx, &ht);
12340 md5_finish_ctx (&ctx, checksum_after_op1);
12341 ht.empty ();
12343 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12344 fold_check_failed (op1, tem);
12346 md5_init_ctx (&ctx);
12347 fold_checksum_tree (op2, &ctx, &ht);
12348 md5_finish_ctx (&ctx, checksum_after_op2);
12350 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12351 fold_check_failed (op2, tem);
12352 #endif
12353 return tem;
12356 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12357 arguments in ARGARRAY, and a null static chain.
12358 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12359 of type TYPE from the given operands as constructed by build_call_array. */
12361 tree
12362 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12363 int nargs, tree *argarray)
12365 tree tem;
12366 #ifdef ENABLE_FOLD_CHECKING
12367 unsigned char checksum_before_fn[16],
12368 checksum_before_arglist[16],
12369 checksum_after_fn[16],
12370 checksum_after_arglist[16];
12371 struct md5_ctx ctx;
12372 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12373 int i;
12375 md5_init_ctx (&ctx);
12376 fold_checksum_tree (fn, &ctx, &ht);
12377 md5_finish_ctx (&ctx, checksum_before_fn);
12378 ht.empty ();
12380 md5_init_ctx (&ctx);
12381 for (i = 0; i < nargs; i++)
12382 fold_checksum_tree (argarray[i], &ctx, &ht);
12383 md5_finish_ctx (&ctx, checksum_before_arglist);
12384 ht.empty ();
12385 #endif
12387 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12388 if (!tem)
12389 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12391 #ifdef ENABLE_FOLD_CHECKING
12392 md5_init_ctx (&ctx);
12393 fold_checksum_tree (fn, &ctx, &ht);
12394 md5_finish_ctx (&ctx, checksum_after_fn);
12395 ht.empty ();
12397 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12398 fold_check_failed (fn, tem);
12400 md5_init_ctx (&ctx);
12401 for (i = 0; i < nargs; i++)
12402 fold_checksum_tree (argarray[i], &ctx, &ht);
12403 md5_finish_ctx (&ctx, checksum_after_arglist);
12405 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12406 fold_check_failed (NULL_TREE, tem);
12407 #endif
12408 return tem;
12411 /* Perform constant folding and related simplification of initializer
12412 expression EXPR. These behave identically to "fold_buildN" but ignore
12413 potential run-time traps and exceptions that fold must preserve. */
12415 #define START_FOLD_INIT \
12416 int saved_signaling_nans = flag_signaling_nans;\
12417 int saved_trapping_math = flag_trapping_math;\
12418 int saved_rounding_math = flag_rounding_math;\
12419 int saved_trapv = flag_trapv;\
12420 int saved_folding_initializer = folding_initializer;\
12421 flag_signaling_nans = 0;\
12422 flag_trapping_math = 0;\
12423 flag_rounding_math = 0;\
12424 flag_trapv = 0;\
12425 folding_initializer = 1;
12427 #define END_FOLD_INIT \
12428 flag_signaling_nans = saved_signaling_nans;\
12429 flag_trapping_math = saved_trapping_math;\
12430 flag_rounding_math = saved_rounding_math;\
12431 flag_trapv = saved_trapv;\
12432 folding_initializer = saved_folding_initializer;
12434 tree
12435 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12436 tree type, tree op)
12438 tree result;
12439 START_FOLD_INIT;
12441 result = fold_build1_loc (loc, code, type, op);
12443 END_FOLD_INIT;
12444 return result;
12447 tree
12448 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12449 tree type, tree op0, tree op1)
12451 tree result;
12452 START_FOLD_INIT;
12454 result = fold_build2_loc (loc, code, type, op0, op1);
12456 END_FOLD_INIT;
12457 return result;
12460 tree
12461 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12462 int nargs, tree *argarray)
12464 tree result;
12465 START_FOLD_INIT;
12467 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12469 END_FOLD_INIT;
12470 return result;
12473 #undef START_FOLD_INIT
12474 #undef END_FOLD_INIT
12476 /* Determine if first argument is a multiple of second argument. Return 0 if
12477 it is not, or we cannot easily determined it to be.
12479 An example of the sort of thing we care about (at this point; this routine
12480 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12481 fold cases do now) is discovering that
12483 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12485 is a multiple of
12487 SAVE_EXPR (J * 8)
12489 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12491 This code also handles discovering that
12493 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12495 is a multiple of 8 so we don't have to worry about dealing with a
12496 possible remainder.
12498 Note that we *look* inside a SAVE_EXPR only to determine how it was
12499 calculated; it is not safe for fold to do much of anything else with the
12500 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12501 at run time. For example, the latter example above *cannot* be implemented
12502 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12503 evaluation time of the original SAVE_EXPR is not necessarily the same at
12504 the time the new expression is evaluated. The only optimization of this
12505 sort that would be valid is changing
12507 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12509 divided by 8 to
12511 SAVE_EXPR (I) * SAVE_EXPR (J)
12513 (where the same SAVE_EXPR (J) is used in the original and the
12514 transformed version). */
12517 multiple_of_p (tree type, const_tree top, const_tree bottom)
12519 gimple *stmt;
12520 tree t1, op1, op2;
12522 if (operand_equal_p (top, bottom, 0))
12523 return 1;
12525 if (TREE_CODE (type) != INTEGER_TYPE)
12526 return 0;
12528 switch (TREE_CODE (top))
12530 case BIT_AND_EXPR:
12531 /* Bitwise and provides a power of two multiple. If the mask is
12532 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12533 if (!integer_pow2p (bottom))
12534 return 0;
12535 /* FALLTHRU */
12537 case MULT_EXPR:
12538 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12539 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12541 case MINUS_EXPR:
12542 /* It is impossible to prove if op0 - op1 is multiple of bottom
12543 precisely, so be conservative here checking if both op0 and op1
12544 are multiple of bottom. Note we check the second operand first
12545 since it's usually simpler. */
12546 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12547 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12549 case PLUS_EXPR:
12550 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12551 as op0 - 3 if the expression has unsigned type. For example,
12552 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12553 op1 = TREE_OPERAND (top, 1);
12554 if (TYPE_UNSIGNED (type)
12555 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12556 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12557 return (multiple_of_p (type, op1, bottom)
12558 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12560 case LSHIFT_EXPR:
12561 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12563 op1 = TREE_OPERAND (top, 1);
12564 /* const_binop may not detect overflow correctly,
12565 so check for it explicitly here. */
12566 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12567 && 0 != (t1 = fold_convert (type,
12568 const_binop (LSHIFT_EXPR,
12569 size_one_node,
12570 op1)))
12571 && !TREE_OVERFLOW (t1))
12572 return multiple_of_p (type, t1, bottom);
12574 return 0;
12576 case NOP_EXPR:
12577 /* Can't handle conversions from non-integral or wider integral type. */
12578 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12579 || (TYPE_PRECISION (type)
12580 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12581 return 0;
12583 /* fall through */
12585 case SAVE_EXPR:
12586 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12588 case COND_EXPR:
12589 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12590 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12592 case INTEGER_CST:
12593 if (TREE_CODE (bottom) != INTEGER_CST
12594 || integer_zerop (bottom)
12595 || (TYPE_UNSIGNED (type)
12596 && (tree_int_cst_sgn (top) < 0
12597 || tree_int_cst_sgn (bottom) < 0)))
12598 return 0;
12599 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12600 SIGNED);
12602 case SSA_NAME:
12603 if (TREE_CODE (bottom) == INTEGER_CST
12604 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12605 && gimple_code (stmt) == GIMPLE_ASSIGN)
12607 enum tree_code code = gimple_assign_rhs_code (stmt);
12609 /* Check for special cases to see if top is defined as multiple
12610 of bottom:
12612 top = (X & ~(bottom - 1) ; bottom is power of 2
12616 Y = X % bottom
12617 top = X - Y. */
12618 if (code == BIT_AND_EXPR
12619 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12620 && TREE_CODE (op2) == INTEGER_CST
12621 && integer_pow2p (bottom)
12622 && wi::multiple_of_p (wi::to_widest (op2),
12623 wi::to_widest (bottom), UNSIGNED))
12624 return 1;
12626 op1 = gimple_assign_rhs1 (stmt);
12627 if (code == MINUS_EXPR
12628 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12629 && TREE_CODE (op2) == SSA_NAME
12630 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12631 && gimple_code (stmt) == GIMPLE_ASSIGN
12632 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12633 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12634 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12635 return 1;
12638 /* fall through */
12640 default:
12641 return 0;
12645 #define tree_expr_nonnegative_warnv_p(X, Y) \
12646 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12648 #define RECURSE(X) \
12649 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12651 /* Return true if CODE or TYPE is known to be non-negative. */
12653 static bool
12654 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12656 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12657 && truth_value_p (code))
12658 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12659 have a signed:1 type (where the value is -1 and 0). */
12660 return true;
12661 return false;
12664 /* Return true if (CODE OP0) is known to be non-negative. If the return
12665 value is based on the assumption that signed overflow is undefined,
12666 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12667 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12669 bool
12670 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12671 bool *strict_overflow_p, int depth)
12673 if (TYPE_UNSIGNED (type))
12674 return true;
12676 switch (code)
12678 case ABS_EXPR:
12679 /* We can't return 1 if flag_wrapv is set because
12680 ABS_EXPR<INT_MIN> = INT_MIN. */
12681 if (!ANY_INTEGRAL_TYPE_P (type))
12682 return true;
12683 if (TYPE_OVERFLOW_UNDEFINED (type))
12685 *strict_overflow_p = true;
12686 return true;
12688 break;
12690 case NON_LVALUE_EXPR:
12691 case FLOAT_EXPR:
12692 case FIX_TRUNC_EXPR:
12693 return RECURSE (op0);
12695 CASE_CONVERT:
12697 tree inner_type = TREE_TYPE (op0);
12698 tree outer_type = type;
12700 if (TREE_CODE (outer_type) == REAL_TYPE)
12702 if (TREE_CODE (inner_type) == REAL_TYPE)
12703 return RECURSE (op0);
12704 if (INTEGRAL_TYPE_P (inner_type))
12706 if (TYPE_UNSIGNED (inner_type))
12707 return true;
12708 return RECURSE (op0);
12711 else if (INTEGRAL_TYPE_P (outer_type))
12713 if (TREE_CODE (inner_type) == REAL_TYPE)
12714 return RECURSE (op0);
12715 if (INTEGRAL_TYPE_P (inner_type))
12716 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12717 && TYPE_UNSIGNED (inner_type);
12720 break;
12722 default:
12723 return tree_simple_nonnegative_warnv_p (code, type);
12726 /* We don't know sign of `t', so be conservative and return false. */
12727 return false;
12730 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12731 value is based on the assumption that signed overflow is undefined,
12732 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12733 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12735 bool
12736 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12737 tree op1, bool *strict_overflow_p,
12738 int depth)
12740 if (TYPE_UNSIGNED (type))
12741 return true;
12743 switch (code)
12745 case POINTER_PLUS_EXPR:
12746 case PLUS_EXPR:
12747 if (FLOAT_TYPE_P (type))
12748 return RECURSE (op0) && RECURSE (op1);
12750 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12751 both unsigned and at least 2 bits shorter than the result. */
12752 if (TREE_CODE (type) == INTEGER_TYPE
12753 && TREE_CODE (op0) == NOP_EXPR
12754 && TREE_CODE (op1) == NOP_EXPR)
12756 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12757 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12758 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12759 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12761 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12762 TYPE_PRECISION (inner2)) + 1;
12763 return prec < TYPE_PRECISION (type);
12766 break;
12768 case MULT_EXPR:
12769 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12771 /* x * x is always non-negative for floating point x
12772 or without overflow. */
12773 if (operand_equal_p (op0, op1, 0)
12774 || (RECURSE (op0) && RECURSE (op1)))
12776 if (ANY_INTEGRAL_TYPE_P (type)
12777 && TYPE_OVERFLOW_UNDEFINED (type))
12778 *strict_overflow_p = true;
12779 return true;
12783 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12784 both unsigned and their total bits is shorter than the result. */
12785 if (TREE_CODE (type) == INTEGER_TYPE
12786 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12787 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12789 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12790 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12791 : TREE_TYPE (op0);
12792 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12793 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12794 : TREE_TYPE (op1);
12796 bool unsigned0 = TYPE_UNSIGNED (inner0);
12797 bool unsigned1 = TYPE_UNSIGNED (inner1);
12799 if (TREE_CODE (op0) == INTEGER_CST)
12800 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12802 if (TREE_CODE (op1) == INTEGER_CST)
12803 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12805 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12806 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12808 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12809 ? tree_int_cst_min_precision (op0, UNSIGNED)
12810 : TYPE_PRECISION (inner0);
12812 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12813 ? tree_int_cst_min_precision (op1, UNSIGNED)
12814 : TYPE_PRECISION (inner1);
12816 return precision0 + precision1 < TYPE_PRECISION (type);
12819 return false;
12821 case BIT_AND_EXPR:
12822 case MAX_EXPR:
12823 return RECURSE (op0) || RECURSE (op1);
12825 case BIT_IOR_EXPR:
12826 case BIT_XOR_EXPR:
12827 case MIN_EXPR:
12828 case RDIV_EXPR:
12829 case TRUNC_DIV_EXPR:
12830 case CEIL_DIV_EXPR:
12831 case FLOOR_DIV_EXPR:
12832 case ROUND_DIV_EXPR:
12833 return RECURSE (op0) && RECURSE (op1);
12835 case TRUNC_MOD_EXPR:
12836 return RECURSE (op0);
12838 case FLOOR_MOD_EXPR:
12839 return RECURSE (op1);
12841 case CEIL_MOD_EXPR:
12842 case ROUND_MOD_EXPR:
12843 default:
12844 return tree_simple_nonnegative_warnv_p (code, type);
12847 /* We don't know sign of `t', so be conservative and return false. */
12848 return false;
12851 /* Return true if T is known to be non-negative. If the return
12852 value is based on the assumption that signed overflow is undefined,
12853 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12854 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12856 bool
12857 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12859 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12860 return true;
12862 switch (TREE_CODE (t))
12864 case INTEGER_CST:
12865 return tree_int_cst_sgn (t) >= 0;
12867 case REAL_CST:
12868 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12870 case FIXED_CST:
12871 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12873 case COND_EXPR:
12874 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12876 case SSA_NAME:
12877 /* Limit the depth of recursion to avoid quadratic behavior.
12878 This is expected to catch almost all occurrences in practice.
12879 If this code misses important cases that unbounded recursion
12880 would not, passes that need this information could be revised
12881 to provide it through dataflow propagation. */
12882 return (!name_registered_for_update_p (t)
12883 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12884 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12885 strict_overflow_p, depth));
12887 default:
12888 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12892 /* Return true if T is known to be non-negative. If the return
12893 value is based on the assumption that signed overflow is undefined,
12894 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12895 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12897 bool
12898 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12899 bool *strict_overflow_p, int depth)
12901 switch (fn)
12903 CASE_CFN_ACOS:
12904 CASE_CFN_ACOSH:
12905 CASE_CFN_CABS:
12906 CASE_CFN_COSH:
12907 CASE_CFN_ERFC:
12908 CASE_CFN_EXP:
12909 CASE_CFN_EXP10:
12910 CASE_CFN_EXP2:
12911 CASE_CFN_FABS:
12912 CASE_CFN_FDIM:
12913 CASE_CFN_HYPOT:
12914 CASE_CFN_POW10:
12915 CASE_CFN_FFS:
12916 CASE_CFN_PARITY:
12917 CASE_CFN_POPCOUNT:
12918 CASE_CFN_CLZ:
12919 CASE_CFN_CLRSB:
12920 case CFN_BUILT_IN_BSWAP32:
12921 case CFN_BUILT_IN_BSWAP64:
12922 /* Always true. */
12923 return true;
12925 CASE_CFN_SQRT:
12926 /* sqrt(-0.0) is -0.0. */
12927 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12928 return true;
12929 return RECURSE (arg0);
12931 CASE_CFN_ASINH:
12932 CASE_CFN_ATAN:
12933 CASE_CFN_ATANH:
12934 CASE_CFN_CBRT:
12935 CASE_CFN_CEIL:
12936 CASE_CFN_ERF:
12937 CASE_CFN_EXPM1:
12938 CASE_CFN_FLOOR:
12939 CASE_CFN_FMOD:
12940 CASE_CFN_FREXP:
12941 CASE_CFN_ICEIL:
12942 CASE_CFN_IFLOOR:
12943 CASE_CFN_IRINT:
12944 CASE_CFN_IROUND:
12945 CASE_CFN_LCEIL:
12946 CASE_CFN_LDEXP:
12947 CASE_CFN_LFLOOR:
12948 CASE_CFN_LLCEIL:
12949 CASE_CFN_LLFLOOR:
12950 CASE_CFN_LLRINT:
12951 CASE_CFN_LLROUND:
12952 CASE_CFN_LRINT:
12953 CASE_CFN_LROUND:
12954 CASE_CFN_MODF:
12955 CASE_CFN_NEARBYINT:
12956 CASE_CFN_RINT:
12957 CASE_CFN_ROUND:
12958 CASE_CFN_SCALB:
12959 CASE_CFN_SCALBLN:
12960 CASE_CFN_SCALBN:
12961 CASE_CFN_SIGNBIT:
12962 CASE_CFN_SIGNIFICAND:
12963 CASE_CFN_SINH:
12964 CASE_CFN_TANH:
12965 CASE_CFN_TRUNC:
12966 /* True if the 1st argument is nonnegative. */
12967 return RECURSE (arg0);
12969 CASE_CFN_FMAX:
12970 /* True if the 1st OR 2nd arguments are nonnegative. */
12971 return RECURSE (arg0) || RECURSE (arg1);
12973 CASE_CFN_FMIN:
12974 /* True if the 1st AND 2nd arguments are nonnegative. */
12975 return RECURSE (arg0) && RECURSE (arg1);
12977 CASE_CFN_COPYSIGN:
12978 /* True if the 2nd argument is nonnegative. */
12979 return RECURSE (arg1);
12981 CASE_CFN_POWI:
12982 /* True if the 1st argument is nonnegative or the second
12983 argument is an even integer. */
12984 if (TREE_CODE (arg1) == INTEGER_CST
12985 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12986 return true;
12987 return RECURSE (arg0);
12989 CASE_CFN_POW:
12990 /* True if the 1st argument is nonnegative or the second
12991 argument is an even integer valued real. */
12992 if (TREE_CODE (arg1) == REAL_CST)
12994 REAL_VALUE_TYPE c;
12995 HOST_WIDE_INT n;
12997 c = TREE_REAL_CST (arg1);
12998 n = real_to_integer (&c);
12999 if ((n & 1) == 0)
13001 REAL_VALUE_TYPE cint;
13002 real_from_integer (&cint, VOIDmode, n, SIGNED);
13003 if (real_identical (&c, &cint))
13004 return true;
13007 return RECURSE (arg0);
13009 default:
13010 break;
13012 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13015 /* Return true if T is known to be non-negative. If the return
13016 value is based on the assumption that signed overflow is undefined,
13017 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13018 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13020 static bool
13021 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13023 enum tree_code code = TREE_CODE (t);
13024 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13025 return true;
13027 switch (code)
13029 case TARGET_EXPR:
13031 tree temp = TARGET_EXPR_SLOT (t);
13032 t = TARGET_EXPR_INITIAL (t);
13034 /* If the initializer is non-void, then it's a normal expression
13035 that will be assigned to the slot. */
13036 if (!VOID_TYPE_P (t))
13037 return RECURSE (t);
13039 /* Otherwise, the initializer sets the slot in some way. One common
13040 way is an assignment statement at the end of the initializer. */
13041 while (1)
13043 if (TREE_CODE (t) == BIND_EXPR)
13044 t = expr_last (BIND_EXPR_BODY (t));
13045 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13046 || TREE_CODE (t) == TRY_CATCH_EXPR)
13047 t = expr_last (TREE_OPERAND (t, 0));
13048 else if (TREE_CODE (t) == STATEMENT_LIST)
13049 t = expr_last (t);
13050 else
13051 break;
13053 if (TREE_CODE (t) == MODIFY_EXPR
13054 && TREE_OPERAND (t, 0) == temp)
13055 return RECURSE (TREE_OPERAND (t, 1));
13057 return false;
13060 case CALL_EXPR:
13062 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13063 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13065 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13066 get_call_combined_fn (t),
13067 arg0,
13068 arg1,
13069 strict_overflow_p, depth);
13071 case COMPOUND_EXPR:
13072 case MODIFY_EXPR:
13073 return RECURSE (TREE_OPERAND (t, 1));
13075 case BIND_EXPR:
13076 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13078 case SAVE_EXPR:
13079 return RECURSE (TREE_OPERAND (t, 0));
13081 default:
13082 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13086 #undef RECURSE
13087 #undef tree_expr_nonnegative_warnv_p
13089 /* Return true if T is known to be non-negative. If the return
13090 value is based on the assumption that signed overflow is undefined,
13091 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13092 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13094 bool
13095 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13097 enum tree_code code;
13098 if (t == error_mark_node)
13099 return false;
13101 code = TREE_CODE (t);
13102 switch (TREE_CODE_CLASS (code))
13104 case tcc_binary:
13105 case tcc_comparison:
13106 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13107 TREE_TYPE (t),
13108 TREE_OPERAND (t, 0),
13109 TREE_OPERAND (t, 1),
13110 strict_overflow_p, depth);
13112 case tcc_unary:
13113 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13114 TREE_TYPE (t),
13115 TREE_OPERAND (t, 0),
13116 strict_overflow_p, depth);
13118 case tcc_constant:
13119 case tcc_declaration:
13120 case tcc_reference:
13121 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13123 default:
13124 break;
13127 switch (code)
13129 case TRUTH_AND_EXPR:
13130 case TRUTH_OR_EXPR:
13131 case TRUTH_XOR_EXPR:
13132 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13133 TREE_TYPE (t),
13134 TREE_OPERAND (t, 0),
13135 TREE_OPERAND (t, 1),
13136 strict_overflow_p, depth);
13137 case TRUTH_NOT_EXPR:
13138 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13139 TREE_TYPE (t),
13140 TREE_OPERAND (t, 0),
13141 strict_overflow_p, depth);
13143 case COND_EXPR:
13144 case CONSTRUCTOR:
13145 case OBJ_TYPE_REF:
13146 case ASSERT_EXPR:
13147 case ADDR_EXPR:
13148 case WITH_SIZE_EXPR:
13149 case SSA_NAME:
13150 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13152 default:
13153 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13157 /* Return true if `t' is known to be non-negative. Handle warnings
13158 about undefined signed overflow. */
13160 bool
13161 tree_expr_nonnegative_p (tree t)
13163 bool ret, strict_overflow_p;
13165 strict_overflow_p = false;
13166 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13167 if (strict_overflow_p)
13168 fold_overflow_warning (("assuming signed overflow does not occur when "
13169 "determining that expression is always "
13170 "non-negative"),
13171 WARN_STRICT_OVERFLOW_MISC);
13172 return ret;
13176 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13177 For floating point we further ensure that T is not denormal.
13178 Similar logic is present in nonzero_address in rtlanal.h.
13180 If the return value is based on the assumption that signed overflow
13181 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13182 change *STRICT_OVERFLOW_P. */
13184 bool
13185 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13186 bool *strict_overflow_p)
13188 switch (code)
13190 case ABS_EXPR:
13191 return tree_expr_nonzero_warnv_p (op0,
13192 strict_overflow_p);
13194 case NOP_EXPR:
13196 tree inner_type = TREE_TYPE (op0);
13197 tree outer_type = type;
13199 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13200 && tree_expr_nonzero_warnv_p (op0,
13201 strict_overflow_p));
13203 break;
13205 case NON_LVALUE_EXPR:
13206 return tree_expr_nonzero_warnv_p (op0,
13207 strict_overflow_p);
13209 default:
13210 break;
13213 return false;
13216 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13217 For floating point we further ensure that T is not denormal.
13218 Similar logic is present in nonzero_address in rtlanal.h.
13220 If the return value is based on the assumption that signed overflow
13221 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13222 change *STRICT_OVERFLOW_P. */
13224 bool
13225 tree_binary_nonzero_warnv_p (enum tree_code code,
13226 tree type,
13227 tree op0,
13228 tree op1, bool *strict_overflow_p)
13230 bool sub_strict_overflow_p;
13231 switch (code)
13233 case POINTER_PLUS_EXPR:
13234 case PLUS_EXPR:
13235 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13237 /* With the presence of negative values it is hard
13238 to say something. */
13239 sub_strict_overflow_p = false;
13240 if (!tree_expr_nonnegative_warnv_p (op0,
13241 &sub_strict_overflow_p)
13242 || !tree_expr_nonnegative_warnv_p (op1,
13243 &sub_strict_overflow_p))
13244 return false;
13245 /* One of operands must be positive and the other non-negative. */
13246 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13247 overflows, on a twos-complement machine the sum of two
13248 nonnegative numbers can never be zero. */
13249 return (tree_expr_nonzero_warnv_p (op0,
13250 strict_overflow_p)
13251 || tree_expr_nonzero_warnv_p (op1,
13252 strict_overflow_p));
13254 break;
13256 case MULT_EXPR:
13257 if (TYPE_OVERFLOW_UNDEFINED (type))
13259 if (tree_expr_nonzero_warnv_p (op0,
13260 strict_overflow_p)
13261 && tree_expr_nonzero_warnv_p (op1,
13262 strict_overflow_p))
13264 *strict_overflow_p = true;
13265 return true;
13268 break;
13270 case MIN_EXPR:
13271 sub_strict_overflow_p = false;
13272 if (tree_expr_nonzero_warnv_p (op0,
13273 &sub_strict_overflow_p)
13274 && tree_expr_nonzero_warnv_p (op1,
13275 &sub_strict_overflow_p))
13277 if (sub_strict_overflow_p)
13278 *strict_overflow_p = true;
13280 break;
13282 case MAX_EXPR:
13283 sub_strict_overflow_p = false;
13284 if (tree_expr_nonzero_warnv_p (op0,
13285 &sub_strict_overflow_p))
13287 if (sub_strict_overflow_p)
13288 *strict_overflow_p = true;
13290 /* When both operands are nonzero, then MAX must be too. */
13291 if (tree_expr_nonzero_warnv_p (op1,
13292 strict_overflow_p))
13293 return true;
13295 /* MAX where operand 0 is positive is positive. */
13296 return tree_expr_nonnegative_warnv_p (op0,
13297 strict_overflow_p);
13299 /* MAX where operand 1 is positive is positive. */
13300 else if (tree_expr_nonzero_warnv_p (op1,
13301 &sub_strict_overflow_p)
13302 && tree_expr_nonnegative_warnv_p (op1,
13303 &sub_strict_overflow_p))
13305 if (sub_strict_overflow_p)
13306 *strict_overflow_p = true;
13307 return true;
13309 break;
13311 case BIT_IOR_EXPR:
13312 return (tree_expr_nonzero_warnv_p (op1,
13313 strict_overflow_p)
13314 || tree_expr_nonzero_warnv_p (op0,
13315 strict_overflow_p));
13317 default:
13318 break;
13321 return false;
13324 /* Return true when T is an address and is known to be nonzero.
13325 For floating point we further ensure that T is not denormal.
13326 Similar logic is present in nonzero_address in rtlanal.h.
13328 If the return value is based on the assumption that signed overflow
13329 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13330 change *STRICT_OVERFLOW_P. */
13332 bool
13333 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13335 bool sub_strict_overflow_p;
13336 switch (TREE_CODE (t))
13338 case INTEGER_CST:
13339 return !integer_zerop (t);
13341 case ADDR_EXPR:
13343 tree base = TREE_OPERAND (t, 0);
13345 if (!DECL_P (base))
13346 base = get_base_address (base);
13348 if (base && TREE_CODE (base) == TARGET_EXPR)
13349 base = TARGET_EXPR_SLOT (base);
13351 if (!base)
13352 return false;
13354 /* For objects in symbol table check if we know they are non-zero.
13355 Don't do anything for variables and functions before symtab is built;
13356 it is quite possible that they will be declared weak later. */
13357 int nonzero_addr = maybe_nonzero_address (base);
13358 if (nonzero_addr >= 0)
13359 return nonzero_addr;
13361 /* Constants are never weak. */
13362 if (CONSTANT_CLASS_P (base))
13363 return true;
13365 return false;
13368 case COND_EXPR:
13369 sub_strict_overflow_p = false;
13370 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13371 &sub_strict_overflow_p)
13372 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13373 &sub_strict_overflow_p))
13375 if (sub_strict_overflow_p)
13376 *strict_overflow_p = true;
13377 return true;
13379 break;
13381 case SSA_NAME:
13382 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13383 break;
13384 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13386 default:
13387 break;
13389 return false;
13392 #define integer_valued_real_p(X) \
13393 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13395 #define RECURSE(X) \
13396 ((integer_valued_real_p) (X, depth + 1))
13398 /* Return true if the floating point result of (CODE OP0) has an
13399 integer value. We also allow +Inf, -Inf and NaN to be considered
13400 integer values. Return false for signaling NaN.
13402 DEPTH is the current nesting depth of the query. */
13404 bool
13405 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13407 switch (code)
13409 case FLOAT_EXPR:
13410 return true;
13412 case ABS_EXPR:
13413 return RECURSE (op0);
13415 CASE_CONVERT:
13417 tree type = TREE_TYPE (op0);
13418 if (TREE_CODE (type) == INTEGER_TYPE)
13419 return true;
13420 if (TREE_CODE (type) == REAL_TYPE)
13421 return RECURSE (op0);
13422 break;
13425 default:
13426 break;
13428 return false;
13431 /* Return true if the floating point result of (CODE OP0 OP1) has an
13432 integer value. We also allow +Inf, -Inf and NaN to be considered
13433 integer values. Return false for signaling NaN.
13435 DEPTH is the current nesting depth of the query. */
13437 bool
13438 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13440 switch (code)
13442 case PLUS_EXPR:
13443 case MINUS_EXPR:
13444 case MULT_EXPR:
13445 case MIN_EXPR:
13446 case MAX_EXPR:
13447 return RECURSE (op0) && RECURSE (op1);
13449 default:
13450 break;
13452 return false;
13455 /* Return true if the floating point result of calling FNDECL with arguments
13456 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13457 considered integer values. Return false for signaling NaN. If FNDECL
13458 takes fewer than 2 arguments, the remaining ARGn are null.
13460 DEPTH is the current nesting depth of the query. */
13462 bool
13463 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13465 switch (fn)
13467 CASE_CFN_CEIL:
13468 CASE_CFN_FLOOR:
13469 CASE_CFN_NEARBYINT:
13470 CASE_CFN_RINT:
13471 CASE_CFN_ROUND:
13472 CASE_CFN_TRUNC:
13473 return true;
13475 CASE_CFN_FMIN:
13476 CASE_CFN_FMAX:
13477 return RECURSE (arg0) && RECURSE (arg1);
13479 default:
13480 break;
13482 return false;
13485 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13486 has an integer value. We also allow +Inf, -Inf and NaN to be
13487 considered integer values. Return false for signaling NaN.
13489 DEPTH is the current nesting depth of the query. */
13491 bool
13492 integer_valued_real_single_p (tree t, int depth)
13494 switch (TREE_CODE (t))
13496 case REAL_CST:
13497 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13499 case COND_EXPR:
13500 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13502 case SSA_NAME:
13503 /* Limit the depth of recursion to avoid quadratic behavior.
13504 This is expected to catch almost all occurrences in practice.
13505 If this code misses important cases that unbounded recursion
13506 would not, passes that need this information could be revised
13507 to provide it through dataflow propagation. */
13508 return (!name_registered_for_update_p (t)
13509 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13510 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13511 depth));
13513 default:
13514 break;
13516 return false;
13519 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13520 has an integer value. We also allow +Inf, -Inf and NaN to be
13521 considered integer values. Return false for signaling NaN.
13523 DEPTH is the current nesting depth of the query. */
13525 static bool
13526 integer_valued_real_invalid_p (tree t, int depth)
13528 switch (TREE_CODE (t))
13530 case COMPOUND_EXPR:
13531 case MODIFY_EXPR:
13532 case BIND_EXPR:
13533 return RECURSE (TREE_OPERAND (t, 1));
13535 case SAVE_EXPR:
13536 return RECURSE (TREE_OPERAND (t, 0));
13538 default:
13539 break;
13541 return false;
13544 #undef RECURSE
13545 #undef integer_valued_real_p
13547 /* Return true if the floating point expression T has an integer value.
13548 We also allow +Inf, -Inf and NaN to be considered integer values.
13549 Return false for signaling NaN.
13551 DEPTH is the current nesting depth of the query. */
13553 bool
13554 integer_valued_real_p (tree t, int depth)
13556 if (t == error_mark_node)
13557 return false;
13559 tree_code code = TREE_CODE (t);
13560 switch (TREE_CODE_CLASS (code))
13562 case tcc_binary:
13563 case tcc_comparison:
13564 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13565 TREE_OPERAND (t, 1), depth);
13567 case tcc_unary:
13568 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13570 case tcc_constant:
13571 case tcc_declaration:
13572 case tcc_reference:
13573 return integer_valued_real_single_p (t, depth);
13575 default:
13576 break;
13579 switch (code)
13581 case COND_EXPR:
13582 case SSA_NAME:
13583 return integer_valued_real_single_p (t, depth);
13585 case CALL_EXPR:
13587 tree arg0 = (call_expr_nargs (t) > 0
13588 ? CALL_EXPR_ARG (t, 0)
13589 : NULL_TREE);
13590 tree arg1 = (call_expr_nargs (t) > 1
13591 ? CALL_EXPR_ARG (t, 1)
13592 : NULL_TREE);
13593 return integer_valued_real_call_p (get_call_combined_fn (t),
13594 arg0, arg1, depth);
13597 default:
13598 return integer_valued_real_invalid_p (t, depth);
13602 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13603 attempt to fold the expression to a constant without modifying TYPE,
13604 OP0 or OP1.
13606 If the expression could be simplified to a constant, then return
13607 the constant. If the expression would not be simplified to a
13608 constant, then return NULL_TREE. */
13610 tree
13611 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13613 tree tem = fold_binary (code, type, op0, op1);
13614 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13617 /* Given the components of a unary expression CODE, TYPE and OP0,
13618 attempt to fold the expression to a constant without modifying
13619 TYPE or OP0.
13621 If the expression could be simplified to a constant, then return
13622 the constant. If the expression would not be simplified to a
13623 constant, then return NULL_TREE. */
13625 tree
13626 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13628 tree tem = fold_unary (code, type, op0);
13629 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13632 /* If EXP represents referencing an element in a constant string
13633 (either via pointer arithmetic or array indexing), return the
13634 tree representing the value accessed, otherwise return NULL. */
13636 tree
13637 fold_read_from_constant_string (tree exp)
13639 if ((TREE_CODE (exp) == INDIRECT_REF
13640 || TREE_CODE (exp) == ARRAY_REF)
13641 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13643 tree exp1 = TREE_OPERAND (exp, 0);
13644 tree index;
13645 tree string;
13646 location_t loc = EXPR_LOCATION (exp);
13648 if (TREE_CODE (exp) == INDIRECT_REF)
13649 string = string_constant (exp1, &index);
13650 else
13652 tree low_bound = array_ref_low_bound (exp);
13653 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13655 /* Optimize the special-case of a zero lower bound.
13657 We convert the low_bound to sizetype to avoid some problems
13658 with constant folding. (E.g. suppose the lower bound is 1,
13659 and its mode is QI. Without the conversion,l (ARRAY
13660 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13661 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13662 if (! integer_zerop (low_bound))
13663 index = size_diffop_loc (loc, index,
13664 fold_convert_loc (loc, sizetype, low_bound));
13666 string = exp1;
13669 scalar_int_mode char_mode;
13670 if (string
13671 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13672 && TREE_CODE (string) == STRING_CST
13673 && TREE_CODE (index) == INTEGER_CST
13674 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13675 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13676 &char_mode)
13677 && GET_MODE_SIZE (char_mode) == 1)
13678 return build_int_cst_type (TREE_TYPE (exp),
13679 (TREE_STRING_POINTER (string)
13680 [TREE_INT_CST_LOW (index)]));
13682 return NULL;
13685 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13686 an integer constant, real, or fixed-point constant.
13688 TYPE is the type of the result. */
13690 static tree
13691 fold_negate_const (tree arg0, tree type)
13693 tree t = NULL_TREE;
13695 switch (TREE_CODE (arg0))
13697 case INTEGER_CST:
13699 bool overflow;
13700 wide_int val = wi::neg (arg0, &overflow);
13701 t = force_fit_type (type, val, 1,
13702 (overflow && ! TYPE_UNSIGNED (type))
13703 || TREE_OVERFLOW (arg0));
13704 break;
13707 case REAL_CST:
13708 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13709 break;
13711 case FIXED_CST:
13713 FIXED_VALUE_TYPE f;
13714 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13715 &(TREE_FIXED_CST (arg0)), NULL,
13716 TYPE_SATURATING (type));
13717 t = build_fixed (type, f);
13718 /* Propagate overflow flags. */
13719 if (overflow_p | TREE_OVERFLOW (arg0))
13720 TREE_OVERFLOW (t) = 1;
13721 break;
13724 default:
13725 gcc_unreachable ();
13728 return t;
13731 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13732 an integer constant or real constant.
13734 TYPE is the type of the result. */
13736 tree
13737 fold_abs_const (tree arg0, tree type)
13739 tree t = NULL_TREE;
13741 switch (TREE_CODE (arg0))
13743 case INTEGER_CST:
13745 /* If the value is unsigned or non-negative, then the absolute value
13746 is the same as the ordinary value. */
13747 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13748 t = arg0;
13750 /* If the value is negative, then the absolute value is
13751 its negation. */
13752 else
13754 bool overflow;
13755 wide_int val = wi::neg (arg0, &overflow);
13756 t = force_fit_type (type, val, -1,
13757 overflow | TREE_OVERFLOW (arg0));
13760 break;
13762 case REAL_CST:
13763 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13764 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13765 else
13766 t = arg0;
13767 break;
13769 default:
13770 gcc_unreachable ();
13773 return t;
13776 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13777 constant. TYPE is the type of the result. */
13779 static tree
13780 fold_not_const (const_tree arg0, tree type)
13782 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13784 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13787 /* Given CODE, a relational operator, the target type, TYPE and two
13788 constant operands OP0 and OP1, return the result of the
13789 relational operation. If the result is not a compile time
13790 constant, then return NULL_TREE. */
13792 static tree
13793 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13795 int result, invert;
13797 /* From here on, the only cases we handle are when the result is
13798 known to be a constant. */
13800 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13802 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13803 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13805 /* Handle the cases where either operand is a NaN. */
13806 if (real_isnan (c0) || real_isnan (c1))
13808 switch (code)
13810 case EQ_EXPR:
13811 case ORDERED_EXPR:
13812 result = 0;
13813 break;
13815 case NE_EXPR:
13816 case UNORDERED_EXPR:
13817 case UNLT_EXPR:
13818 case UNLE_EXPR:
13819 case UNGT_EXPR:
13820 case UNGE_EXPR:
13821 case UNEQ_EXPR:
13822 result = 1;
13823 break;
13825 case LT_EXPR:
13826 case LE_EXPR:
13827 case GT_EXPR:
13828 case GE_EXPR:
13829 case LTGT_EXPR:
13830 if (flag_trapping_math)
13831 return NULL_TREE;
13832 result = 0;
13833 break;
13835 default:
13836 gcc_unreachable ();
13839 return constant_boolean_node (result, type);
13842 return constant_boolean_node (real_compare (code, c0, c1), type);
13845 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13847 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13848 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13849 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13852 /* Handle equality/inequality of complex constants. */
13853 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13855 tree rcond = fold_relational_const (code, type,
13856 TREE_REALPART (op0),
13857 TREE_REALPART (op1));
13858 tree icond = fold_relational_const (code, type,
13859 TREE_IMAGPART (op0),
13860 TREE_IMAGPART (op1));
13861 if (code == EQ_EXPR)
13862 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13863 else if (code == NE_EXPR)
13864 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13865 else
13866 return NULL_TREE;
13869 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13871 if (!VECTOR_TYPE_P (type))
13873 /* Have vector comparison with scalar boolean result. */
13874 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13875 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13876 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13878 tree elem0 = VECTOR_CST_ELT (op0, i);
13879 tree elem1 = VECTOR_CST_ELT (op1, i);
13880 tree tmp = fold_relational_const (code, type, elem0, elem1);
13881 if (tmp == NULL_TREE)
13882 return NULL_TREE;
13883 if (integer_zerop (tmp))
13884 return constant_boolean_node (false, type);
13886 return constant_boolean_node (true, type);
13888 unsigned count = VECTOR_CST_NELTS (op0);
13889 tree *elts = XALLOCAVEC (tree, count);
13890 gcc_assert (VECTOR_CST_NELTS (op1) == count
13891 && TYPE_VECTOR_SUBPARTS (type) == count);
13893 for (unsigned i = 0; i < count; i++)
13895 tree elem_type = TREE_TYPE (type);
13896 tree elem0 = VECTOR_CST_ELT (op0, i);
13897 tree elem1 = VECTOR_CST_ELT (op1, i);
13899 tree tem = fold_relational_const (code, elem_type,
13900 elem0, elem1);
13902 if (tem == NULL_TREE)
13903 return NULL_TREE;
13905 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13908 return build_vector (type, elts);
13911 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13913 To compute GT, swap the arguments and do LT.
13914 To compute GE, do LT and invert the result.
13915 To compute LE, swap the arguments, do LT and invert the result.
13916 To compute NE, do EQ and invert the result.
13918 Therefore, the code below must handle only EQ and LT. */
13920 if (code == LE_EXPR || code == GT_EXPR)
13922 std::swap (op0, op1);
13923 code = swap_tree_comparison (code);
13926 /* Note that it is safe to invert for real values here because we
13927 have already handled the one case that it matters. */
13929 invert = 0;
13930 if (code == NE_EXPR || code == GE_EXPR)
13932 invert = 1;
13933 code = invert_tree_comparison (code, false);
13936 /* Compute a result for LT or EQ if args permit;
13937 Otherwise return T. */
13938 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13940 if (code == EQ_EXPR)
13941 result = tree_int_cst_equal (op0, op1);
13942 else
13943 result = tree_int_cst_lt (op0, op1);
13945 else
13946 return NULL_TREE;
13948 if (invert)
13949 result ^= 1;
13950 return constant_boolean_node (result, type);
13953 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13954 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13955 itself. */
13957 tree
13958 fold_build_cleanup_point_expr (tree type, tree expr)
13960 /* If the expression does not have side effects then we don't have to wrap
13961 it with a cleanup point expression. */
13962 if (!TREE_SIDE_EFFECTS (expr))
13963 return expr;
13965 /* If the expression is a return, check to see if the expression inside the
13966 return has no side effects or the right hand side of the modify expression
13967 inside the return. If either don't have side effects set we don't need to
13968 wrap the expression in a cleanup point expression. Note we don't check the
13969 left hand side of the modify because it should always be a return decl. */
13970 if (TREE_CODE (expr) == RETURN_EXPR)
13972 tree op = TREE_OPERAND (expr, 0);
13973 if (!op || !TREE_SIDE_EFFECTS (op))
13974 return expr;
13975 op = TREE_OPERAND (op, 1);
13976 if (!TREE_SIDE_EFFECTS (op))
13977 return expr;
13980 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
13983 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13984 of an indirection through OP0, or NULL_TREE if no simplification is
13985 possible. */
13987 tree
13988 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13990 tree sub = op0;
13991 tree subtype;
13993 STRIP_NOPS (sub);
13994 subtype = TREE_TYPE (sub);
13995 if (!POINTER_TYPE_P (subtype)
13996 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
13997 return NULL_TREE;
13999 if (TREE_CODE (sub) == ADDR_EXPR)
14001 tree op = TREE_OPERAND (sub, 0);
14002 tree optype = TREE_TYPE (op);
14003 /* *&CONST_DECL -> to the value of the const decl. */
14004 if (TREE_CODE (op) == CONST_DECL)
14005 return DECL_INITIAL (op);
14006 /* *&p => p; make sure to handle *&"str"[cst] here. */
14007 if (type == optype)
14009 tree fop = fold_read_from_constant_string (op);
14010 if (fop)
14011 return fop;
14012 else
14013 return op;
14015 /* *(foo *)&fooarray => fooarray[0] */
14016 else if (TREE_CODE (optype) == ARRAY_TYPE
14017 && type == TREE_TYPE (optype)
14018 && (!in_gimple_form
14019 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14021 tree type_domain = TYPE_DOMAIN (optype);
14022 tree min_val = size_zero_node;
14023 if (type_domain && TYPE_MIN_VALUE (type_domain))
14024 min_val = TYPE_MIN_VALUE (type_domain);
14025 if (in_gimple_form
14026 && TREE_CODE (min_val) != INTEGER_CST)
14027 return NULL_TREE;
14028 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14029 NULL_TREE, NULL_TREE);
14031 /* *(foo *)&complexfoo => __real__ complexfoo */
14032 else if (TREE_CODE (optype) == COMPLEX_TYPE
14033 && type == TREE_TYPE (optype))
14034 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14035 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14036 else if (TREE_CODE (optype) == VECTOR_TYPE
14037 && type == TREE_TYPE (optype))
14039 tree part_width = TYPE_SIZE (type);
14040 tree index = bitsize_int (0);
14041 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14045 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14046 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14048 tree op00 = TREE_OPERAND (sub, 0);
14049 tree op01 = TREE_OPERAND (sub, 1);
14051 STRIP_NOPS (op00);
14052 if (TREE_CODE (op00) == ADDR_EXPR)
14054 tree op00type;
14055 op00 = TREE_OPERAND (op00, 0);
14056 op00type = TREE_TYPE (op00);
14058 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14059 if (TREE_CODE (op00type) == VECTOR_TYPE
14060 && type == TREE_TYPE (op00type))
14062 tree part_width = TYPE_SIZE (type);
14063 unsigned HOST_WIDE_INT max_offset
14064 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14065 * TYPE_VECTOR_SUBPARTS (op00type));
14066 if (tree_int_cst_sign_bit (op01) == 0
14067 && compare_tree_int (op01, max_offset) == -1)
14069 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14070 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14071 tree index = bitsize_int (indexi);
14072 return fold_build3_loc (loc,
14073 BIT_FIELD_REF, type, op00,
14074 part_width, index);
14077 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14078 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14079 && type == TREE_TYPE (op00type))
14081 tree size = TYPE_SIZE_UNIT (type);
14082 if (tree_int_cst_equal (size, op01))
14083 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14085 /* ((foo *)&fooarray)[1] => fooarray[1] */
14086 else if (TREE_CODE (op00type) == ARRAY_TYPE
14087 && type == TREE_TYPE (op00type))
14089 tree type_domain = TYPE_DOMAIN (op00type);
14090 tree min = size_zero_node;
14091 if (type_domain && TYPE_MIN_VALUE (type_domain))
14092 min = TYPE_MIN_VALUE (type_domain);
14093 offset_int off = wi::to_offset (op01);
14094 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14095 offset_int remainder;
14096 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14097 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
14099 off = off + wi::to_offset (min);
14100 op01 = wide_int_to_tree (sizetype, off);
14101 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14102 NULL_TREE, NULL_TREE);
14108 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14109 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14110 && type == TREE_TYPE (TREE_TYPE (subtype))
14111 && (!in_gimple_form
14112 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14114 tree type_domain;
14115 tree min_val = size_zero_node;
14116 sub = build_fold_indirect_ref_loc (loc, sub);
14117 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14118 if (type_domain && TYPE_MIN_VALUE (type_domain))
14119 min_val = TYPE_MIN_VALUE (type_domain);
14120 if (in_gimple_form
14121 && TREE_CODE (min_val) != INTEGER_CST)
14122 return NULL_TREE;
14123 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14124 NULL_TREE);
14127 return NULL_TREE;
14130 /* Builds an expression for an indirection through T, simplifying some
14131 cases. */
14133 tree
14134 build_fold_indirect_ref_loc (location_t loc, tree t)
14136 tree type = TREE_TYPE (TREE_TYPE (t));
14137 tree sub = fold_indirect_ref_1 (loc, type, t);
14139 if (sub)
14140 return sub;
14142 return build1_loc (loc, INDIRECT_REF, type, t);
14145 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14147 tree
14148 fold_indirect_ref_loc (location_t loc, tree t)
14150 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14152 if (sub)
14153 return sub;
14154 else
14155 return t;
14158 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14159 whose result is ignored. The type of the returned tree need not be
14160 the same as the original expression. */
14162 tree
14163 fold_ignored_result (tree t)
14165 if (!TREE_SIDE_EFFECTS (t))
14166 return integer_zero_node;
14168 for (;;)
14169 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14171 case tcc_unary:
14172 t = TREE_OPERAND (t, 0);
14173 break;
14175 case tcc_binary:
14176 case tcc_comparison:
14177 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14178 t = TREE_OPERAND (t, 0);
14179 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14180 t = TREE_OPERAND (t, 1);
14181 else
14182 return t;
14183 break;
14185 case tcc_expression:
14186 switch (TREE_CODE (t))
14188 case COMPOUND_EXPR:
14189 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14190 return t;
14191 t = TREE_OPERAND (t, 0);
14192 break;
14194 case COND_EXPR:
14195 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14196 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14197 return t;
14198 t = TREE_OPERAND (t, 0);
14199 break;
14201 default:
14202 return t;
14204 break;
14206 default:
14207 return t;
14211 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14213 tree
14214 round_up_loc (location_t loc, tree value, unsigned int divisor)
14216 tree div = NULL_TREE;
14218 if (divisor == 1)
14219 return value;
14221 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14222 have to do anything. Only do this when we are not given a const,
14223 because in that case, this check is more expensive than just
14224 doing it. */
14225 if (TREE_CODE (value) != INTEGER_CST)
14227 div = build_int_cst (TREE_TYPE (value), divisor);
14229 if (multiple_of_p (TREE_TYPE (value), value, div))
14230 return value;
14233 /* If divisor is a power of two, simplify this to bit manipulation. */
14234 if (pow2_or_zerop (divisor))
14236 if (TREE_CODE (value) == INTEGER_CST)
14238 wide_int val = value;
14239 bool overflow_p;
14241 if ((val & (divisor - 1)) == 0)
14242 return value;
14244 overflow_p = TREE_OVERFLOW (value);
14245 val += divisor - 1;
14246 val &= (int) -divisor;
14247 if (val == 0)
14248 overflow_p = true;
14250 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14252 else
14254 tree t;
14256 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14257 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14258 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14259 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14262 else
14264 if (!div)
14265 div = build_int_cst (TREE_TYPE (value), divisor);
14266 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14267 value = size_binop_loc (loc, MULT_EXPR, value, div);
14270 return value;
14273 /* Likewise, but round down. */
14275 tree
14276 round_down_loc (location_t loc, tree value, int divisor)
14278 tree div = NULL_TREE;
14280 gcc_assert (divisor > 0);
14281 if (divisor == 1)
14282 return value;
14284 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14285 have to do anything. Only do this when we are not given a const,
14286 because in that case, this check is more expensive than just
14287 doing it. */
14288 if (TREE_CODE (value) != INTEGER_CST)
14290 div = build_int_cst (TREE_TYPE (value), divisor);
14292 if (multiple_of_p (TREE_TYPE (value), value, div))
14293 return value;
14296 /* If divisor is a power of two, simplify this to bit manipulation. */
14297 if (pow2_or_zerop (divisor))
14299 tree t;
14301 t = build_int_cst (TREE_TYPE (value), -divisor);
14302 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14304 else
14306 if (!div)
14307 div = build_int_cst (TREE_TYPE (value), divisor);
14308 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14309 value = size_binop_loc (loc, MULT_EXPR, value, div);
14312 return value;
14315 /* Returns the pointer to the base of the object addressed by EXP and
14316 extracts the information about the offset of the access, storing it
14317 to PBITPOS and POFFSET. */
14319 static tree
14320 split_address_to_core_and_offset (tree exp,
14321 HOST_WIDE_INT *pbitpos, tree *poffset)
14323 tree core;
14324 machine_mode mode;
14325 int unsignedp, reversep, volatilep;
14326 HOST_WIDE_INT bitsize;
14327 location_t loc = EXPR_LOCATION (exp);
14329 if (TREE_CODE (exp) == ADDR_EXPR)
14331 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14332 poffset, &mode, &unsignedp, &reversep,
14333 &volatilep);
14334 core = build_fold_addr_expr_loc (loc, core);
14336 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14338 core = TREE_OPERAND (exp, 0);
14339 STRIP_NOPS (core);
14340 *pbitpos = 0;
14341 *poffset = TREE_OPERAND (exp, 1);
14342 if (TREE_CODE (*poffset) == INTEGER_CST)
14344 offset_int tem = wi::sext (wi::to_offset (*poffset),
14345 TYPE_PRECISION (TREE_TYPE (*poffset)));
14346 tem <<= LOG2_BITS_PER_UNIT;
14347 if (wi::fits_shwi_p (tem))
14349 *pbitpos = tem.to_shwi ();
14350 *poffset = NULL_TREE;
14354 else
14356 core = exp;
14357 *pbitpos = 0;
14358 *poffset = NULL_TREE;
14361 return core;
14364 /* Returns true if addresses of E1 and E2 differ by a constant, false
14365 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14367 bool
14368 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14370 tree core1, core2;
14371 HOST_WIDE_INT bitpos1, bitpos2;
14372 tree toffset1, toffset2, tdiff, type;
14374 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14375 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14377 if (bitpos1 % BITS_PER_UNIT != 0
14378 || bitpos2 % BITS_PER_UNIT != 0
14379 || !operand_equal_p (core1, core2, 0))
14380 return false;
14382 if (toffset1 && toffset2)
14384 type = TREE_TYPE (toffset1);
14385 if (type != TREE_TYPE (toffset2))
14386 toffset2 = fold_convert (type, toffset2);
14388 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14389 if (!cst_and_fits_in_hwi (tdiff))
14390 return false;
14392 *diff = int_cst_value (tdiff);
14394 else if (toffset1 || toffset2)
14396 /* If only one of the offsets is non-constant, the difference cannot
14397 be a constant. */
14398 return false;
14400 else
14401 *diff = 0;
14403 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14404 return true;
14407 /* Return OFF converted to a pointer offset type suitable as offset for
14408 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14409 tree
14410 convert_to_ptrofftype_loc (location_t loc, tree off)
14412 return fold_convert_loc (loc, sizetype, off);
14415 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14416 tree
14417 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14419 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14420 ptr, convert_to_ptrofftype_loc (loc, off));
14423 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14424 tree
14425 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14427 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14428 ptr, size_int (off));
14431 /* Return a char pointer for a C string if it is a string constant
14432 or sum of string constant and integer constant. We only support
14433 string constants properly terminated with '\0' character.
14434 If STRLEN is a valid pointer, length (including terminating character)
14435 of returned string is stored to the argument. */
14437 const char *
14438 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14440 tree offset_node;
14442 if (strlen)
14443 *strlen = 0;
14445 src = string_constant (src, &offset_node);
14446 if (src == 0)
14447 return NULL;
14449 unsigned HOST_WIDE_INT offset = 0;
14450 if (offset_node != NULL_TREE)
14452 if (!tree_fits_uhwi_p (offset_node))
14453 return NULL;
14454 else
14455 offset = tree_to_uhwi (offset_node);
14458 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14459 const char *string = TREE_STRING_POINTER (src);
14461 /* Support only properly null-terminated strings. */
14462 if (string_length == 0
14463 || string[string_length - 1] != '\0'
14464 || offset >= string_length)
14465 return NULL;
14467 if (strlen)
14468 *strlen = string_length - offset;
14469 return string + offset;
14472 #if CHECKING_P
14474 namespace selftest {
14476 /* Helper functions for writing tests of folding trees. */
14478 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14480 static void
14481 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14482 tree constant)
14484 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14487 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14488 wrapping WRAPPED_EXPR. */
14490 static void
14491 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14492 tree wrapped_expr)
14494 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14495 ASSERT_NE (wrapped_expr, result);
14496 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14497 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14500 /* Verify that various arithmetic binary operations are folded
14501 correctly. */
14503 static void
14504 test_arithmetic_folding ()
14506 tree type = integer_type_node;
14507 tree x = create_tmp_var_raw (type, "x");
14508 tree zero = build_zero_cst (type);
14509 tree one = build_int_cst (type, 1);
14511 /* Addition. */
14512 /* 1 <-- (0 + 1) */
14513 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14514 one);
14515 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14516 one);
14518 /* (nonlvalue)x <-- (x + 0) */
14519 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14522 /* Subtraction. */
14523 /* 0 <-- (x - x) */
14524 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14525 zero);
14526 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14529 /* Multiplication. */
14530 /* 0 <-- (x * 0) */
14531 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14532 zero);
14534 /* (nonlvalue)x <-- (x * 1) */
14535 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14539 /* Verify that various binary operations on vectors are folded
14540 correctly. */
14542 static void
14543 test_vector_folding ()
14545 tree inner_type = integer_type_node;
14546 tree type = build_vector_type (inner_type, 4);
14547 tree zero = build_zero_cst (type);
14548 tree one = build_one_cst (type);
14550 /* Verify equality tests that return a scalar boolean result. */
14551 tree res_type = boolean_type_node;
14552 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14553 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14554 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14555 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14558 /* Run all of the selftests within this file. */
14560 void
14561 fold_const_c_tests ()
14563 test_arithmetic_folding ();
14564 test_vector_folding ();
14567 } // namespace selftest
14569 #endif /* CHECKING_P */