2016-11-10 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / fold-const.c
blobe14471ed8d35c66d97223b70bfd21973cd1b29d0
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 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"
83 /* Nonzero if we are folding constants inside an initializer; zero
84 otherwise. */
85 int folding_initializer = 0;
87 /* The following constants represent a bit based encoding of GCC's
88 comparison operators. This encoding simplifies transformations
89 on relational comparison operators, such as AND and OR. */
90 enum comparison_code {
91 COMPCODE_FALSE = 0,
92 COMPCODE_LT = 1,
93 COMPCODE_EQ = 2,
94 COMPCODE_LE = 3,
95 COMPCODE_GT = 4,
96 COMPCODE_LTGT = 5,
97 COMPCODE_GE = 6,
98 COMPCODE_ORD = 7,
99 COMPCODE_UNORD = 8,
100 COMPCODE_UNLT = 9,
101 COMPCODE_UNEQ = 10,
102 COMPCODE_UNLE = 11,
103 COMPCODE_UNGT = 12,
104 COMPCODE_NE = 13,
105 COMPCODE_UNGE = 14,
106 COMPCODE_TRUE = 15
109 static bool negate_expr_p (tree);
110 static tree negate_expr (tree);
111 static tree split_tree (location_t, tree, tree, enum tree_code,
112 tree *, tree *, tree *, int);
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 operand_equal_for_comparison_p (tree, tree, tree);
117 static int twoval_comparison_p (tree, tree *, tree *, int *);
118 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
119 static tree optimize_bit_field_compare (location_t, enum tree_code,
120 tree, tree, tree);
121 static int simple_operand_p (const_tree);
122 static bool simple_operand_p_2 (tree);
123 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
124 static tree range_predecessor (tree);
125 static tree range_successor (tree);
126 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
130 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
131 static tree fold_binary_op_with_conditional_arg (location_t,
132 enum tree_code, tree,
133 tree, tree,
134 tree, tree, int);
135 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static bool vec_cst_ctor_to_array (tree, tree *);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
147 static location_t
148 expr_location_or (tree t, location_t loc)
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc == UNKNOWN_LOCATION ? loc : tloc;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
166 x = copy_node (x);
167 SET_EXPR_LOCATION (x, loc);
169 return x;
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
174 NULL_TREE. */
176 tree
177 div_if_zero_remainder (const_tree arg1, const_tree arg2)
179 widest_int quo;
181 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
182 SIGNED, &quo))
183 return wide_int_to_tree (TREE_TYPE (arg1), quo);
185 return NULL_TREE;
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
195 used. */
197 static int fold_deferring_overflow_warnings;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
214 void
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
227 deferred code. */
229 void
230 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
232 const char *warnmsg;
233 location_t locus;
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
239 if (fold_deferred_overflow_warning != NULL
240 && code != 0
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243 return;
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
249 if (!issue || warnmsg == NULL)
250 return;
252 if (gimple_no_warning_p (stmt))
253 return;
255 /* Use the smallest code level when deciding to issue the
256 warning. */
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
260 if (!issue_strict_overflow_warning (code))
261 return;
263 if (stmt == NULL)
264 locus = input_location;
265 else
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
270 /* Stop deferring overflow warnings, ignoring any deferred
271 warnings. */
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL, 0);
279 /* Whether we are deferring overflow warnings. */
281 bool
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings > 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
290 void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 if (fold_deferring_overflow_warnings > 0)
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
309 bool
310 negate_mathfn_p (combined_fn fn)
312 switch (fn)
314 CASE_CFN_ASIN:
315 CASE_CFN_ASINH:
316 CASE_CFN_ATAN:
317 CASE_CFN_ATANH:
318 CASE_CFN_CASIN:
319 CASE_CFN_CASINH:
320 CASE_CFN_CATAN:
321 CASE_CFN_CATANH:
322 CASE_CFN_CBRT:
323 CASE_CFN_CPROJ:
324 CASE_CFN_CSIN:
325 CASE_CFN_CSINH:
326 CASE_CFN_CTAN:
327 CASE_CFN_CTANH:
328 CASE_CFN_ERF:
329 CASE_CFN_LLROUND:
330 CASE_CFN_LROUND:
331 CASE_CFN_ROUND:
332 CASE_CFN_SIN:
333 CASE_CFN_SINH:
334 CASE_CFN_TAN:
335 CASE_CFN_TANH:
336 CASE_CFN_TRUNC:
337 return true;
339 CASE_CFN_LLRINT:
340 CASE_CFN_LRINT:
341 CASE_CFN_NEARBYINT:
342 CASE_CFN_RINT:
343 return !flag_rounding_math;
345 default:
346 break;
348 return false;
351 /* Check whether we may negate an integer constant T without causing
352 overflow. */
354 bool
355 may_negate_without_overflow_p (const_tree t)
357 tree type;
359 gcc_assert (TREE_CODE (t) == INTEGER_CST);
361 type = TREE_TYPE (t);
362 if (TYPE_UNSIGNED (type))
363 return false;
365 return !wi::only_sign_bit_p (t);
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
371 static bool
372 negate_expr_p (tree t)
374 tree type;
376 if (t == 0)
377 return false;
379 type = TREE_TYPE (t);
381 STRIP_SIGN_NOPS (t);
382 switch (TREE_CODE (t))
384 case INTEGER_CST:
385 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
386 return true;
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t);
390 case BIT_NOT_EXPR:
391 return (INTEGRAL_TYPE_P (type)
392 && TYPE_OVERFLOW_WRAPS (type));
394 case FIXED_CST:
395 return true;
397 case NEGATE_EXPR:
398 return !TYPE_OVERFLOW_SANITIZED (type);
400 case REAL_CST:
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
405 case COMPLEX_CST:
406 return negate_expr_p (TREE_REALPART (t))
407 && negate_expr_p (TREE_IMAGPART (t));
409 case VECTOR_CST:
411 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
412 return true;
414 int count = TYPE_VECTOR_SUBPARTS (type), i;
416 for (i = 0; i < count; i++)
417 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
418 return false;
420 return true;
423 case COMPLEX_EXPR:
424 return negate_expr_p (TREE_OPERAND (t, 0))
425 && negate_expr_p (TREE_OPERAND (t, 1));
427 case CONJ_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0));
430 case PLUS_EXPR:
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
432 || HONOR_SIGNED_ZEROS (element_mode (type))
433 || (INTEGRAL_TYPE_P (type)
434 && ! TYPE_OVERFLOW_WRAPS (type)))
435 return false;
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t, 1)))
438 return true;
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t, 0));
442 case MINUS_EXPR:
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
445 && !HONOR_SIGNED_ZEROS (element_mode (type))
446 && (! INTEGRAL_TYPE_P (type)
447 || TYPE_OVERFLOW_WRAPS (type));
449 case MULT_EXPR:
450 if (TYPE_UNSIGNED (type))
451 break;
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && ! integer_pow2p (TREE_OPERAND (t, 0)))
458 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
459 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
460 break;
462 /* Fall through. */
464 case RDIV_EXPR:
465 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
466 return negate_expr_p (TREE_OPERAND (t, 1))
467 || negate_expr_p (TREE_OPERAND (t, 0));
468 break;
470 case TRUNC_DIV_EXPR:
471 case ROUND_DIV_EXPR:
472 case EXACT_DIV_EXPR:
473 if (TYPE_UNSIGNED (type))
474 break;
475 if (negate_expr_p (TREE_OPERAND (t, 0)))
476 return true;
477 /* In general we can't negate B in A / B, because if A is INT_MIN and
478 B is 1, we may turn this into INT_MIN / -1 which is undefined
479 and actually traps on some architectures. */
480 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
481 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
482 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
483 && ! integer_onep (TREE_OPERAND (t, 1))))
484 return negate_expr_p (TREE_OPERAND (t, 1));
485 break;
487 case NOP_EXPR:
488 /* Negate -((double)float) as (double)(-float). */
489 if (TREE_CODE (type) == REAL_TYPE)
491 tree tem = strip_float_extensions (t);
492 if (tem != t)
493 return negate_expr_p (tem);
495 break;
497 case CALL_EXPR:
498 /* Negate -f(x) as f(-x). */
499 if (negate_mathfn_p (get_call_combined_fn (t)))
500 return negate_expr_p (CALL_EXPR_ARG (t, 0));
501 break;
503 case RSHIFT_EXPR:
504 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
505 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
507 tree op1 = TREE_OPERAND (t, 1);
508 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
509 return true;
511 break;
513 default:
514 break;
516 return false;
519 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
520 simplification is possible.
521 If negate_expr_p would return true for T, NULL_TREE will never be
522 returned. */
524 static tree
525 fold_negate_expr (location_t loc, tree t)
527 tree type = TREE_TYPE (t);
528 tree tem;
530 switch (TREE_CODE (t))
532 /* Convert - (~A) to A + 1. */
533 case BIT_NOT_EXPR:
534 if (INTEGRAL_TYPE_P (type))
535 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
536 build_one_cst (type));
537 break;
539 case INTEGER_CST:
540 tem = fold_negate_const (t, type);
541 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
542 || (ANY_INTEGRAL_TYPE_P (type)
543 && !TYPE_OVERFLOW_TRAPS (type)
544 && TYPE_OVERFLOW_WRAPS (type))
545 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
546 return tem;
547 break;
549 case REAL_CST:
550 tem = fold_negate_const (t, type);
551 return tem;
553 case FIXED_CST:
554 tem = fold_negate_const (t, type);
555 return tem;
557 case COMPLEX_CST:
559 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
560 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
561 if (rpart && ipart)
562 return build_complex (type, rpart, ipart);
564 break;
566 case VECTOR_CST:
568 int count = TYPE_VECTOR_SUBPARTS (type), i;
569 tree *elts = XALLOCAVEC (tree, count);
571 for (i = 0; i < count; i++)
573 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
574 if (elts[i] == NULL_TREE)
575 return NULL_TREE;
578 return build_vector (type, elts);
581 case COMPLEX_EXPR:
582 if (negate_expr_p (t))
583 return fold_build2_loc (loc, COMPLEX_EXPR, type,
584 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
585 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
586 break;
588 case CONJ_EXPR:
589 if (negate_expr_p (t))
590 return fold_build1_loc (loc, CONJ_EXPR, type,
591 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
592 break;
594 case NEGATE_EXPR:
595 if (!TYPE_OVERFLOW_SANITIZED (type))
596 return TREE_OPERAND (t, 0);
597 break;
599 case PLUS_EXPR:
600 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
601 && !HONOR_SIGNED_ZEROS (element_mode (type)))
603 /* -(A + B) -> (-B) - A. */
604 if (negate_expr_p (TREE_OPERAND (t, 1)))
606 tem = negate_expr (TREE_OPERAND (t, 1));
607 return fold_build2_loc (loc, MINUS_EXPR, type,
608 tem, TREE_OPERAND (t, 0));
611 /* -(A + B) -> (-A) - B. */
612 if (negate_expr_p (TREE_OPERAND (t, 0)))
614 tem = negate_expr (TREE_OPERAND (t, 0));
615 return fold_build2_loc (loc, MINUS_EXPR, type,
616 tem, TREE_OPERAND (t, 1));
619 break;
621 case MINUS_EXPR:
622 /* - (A - B) -> B - A */
623 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
624 && !HONOR_SIGNED_ZEROS (element_mode (type)))
625 return fold_build2_loc (loc, MINUS_EXPR, type,
626 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
627 break;
629 case MULT_EXPR:
630 if (TYPE_UNSIGNED (type))
631 break;
633 /* Fall through. */
635 case RDIV_EXPR:
636 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
638 tem = TREE_OPERAND (t, 1);
639 if (negate_expr_p (tem))
640 return fold_build2_loc (loc, TREE_CODE (t), type,
641 TREE_OPERAND (t, 0), negate_expr (tem));
642 tem = TREE_OPERAND (t, 0);
643 if (negate_expr_p (tem))
644 return fold_build2_loc (loc, TREE_CODE (t), type,
645 negate_expr (tem), TREE_OPERAND (t, 1));
647 break;
649 case TRUNC_DIV_EXPR:
650 case ROUND_DIV_EXPR:
651 case EXACT_DIV_EXPR:
652 if (TYPE_UNSIGNED (type))
653 break;
654 if (negate_expr_p (TREE_OPERAND (t, 0)))
655 return fold_build2_loc (loc, TREE_CODE (t), type,
656 negate_expr (TREE_OPERAND (t, 0)),
657 TREE_OPERAND (t, 1));
658 /* In general we can't negate B in A / B, because if A is INT_MIN and
659 B is 1, we may turn this into INT_MIN / -1 which is undefined
660 and actually traps on some architectures. */
661 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
662 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
663 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
664 && ! integer_onep (TREE_OPERAND (t, 1))))
665 && negate_expr_p (TREE_OPERAND (t, 1)))
666 return fold_build2_loc (loc, TREE_CODE (t), type,
667 TREE_OPERAND (t, 0),
668 negate_expr (TREE_OPERAND (t, 1)));
669 break;
671 case NOP_EXPR:
672 /* Convert -((double)float) into (double)(-float). */
673 if (TREE_CODE (type) == REAL_TYPE)
675 tem = strip_float_extensions (t);
676 if (tem != t && negate_expr_p (tem))
677 return fold_convert_loc (loc, type, negate_expr (tem));
679 break;
681 case CALL_EXPR:
682 /* Negate -f(x) as f(-x). */
683 if (negate_mathfn_p (get_call_combined_fn (t))
684 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
686 tree fndecl, arg;
688 fndecl = get_callee_fndecl (t);
689 arg = negate_expr (CALL_EXPR_ARG (t, 0));
690 return build_call_expr_loc (loc, fndecl, 1, arg);
692 break;
694 case RSHIFT_EXPR:
695 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
696 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
698 tree op1 = TREE_OPERAND (t, 1);
699 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
701 tree ntype = TYPE_UNSIGNED (type)
702 ? signed_type_for (type)
703 : unsigned_type_for (type);
704 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
705 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
706 return fold_convert_loc (loc, type, temp);
709 break;
711 default:
712 break;
715 return NULL_TREE;
718 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
719 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
720 return NULL_TREE. */
722 static tree
723 negate_expr (tree t)
725 tree type, tem;
726 location_t loc;
728 if (t == NULL_TREE)
729 return NULL_TREE;
731 loc = EXPR_LOCATION (t);
732 type = TREE_TYPE (t);
733 STRIP_SIGN_NOPS (t);
735 tem = fold_negate_expr (loc, t);
736 if (!tem)
737 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
738 return fold_convert_loc (loc, type, tem);
741 /* Split a tree IN into a constant, literal and variable parts that could be
742 combined with CODE to make IN. "constant" means an expression with
743 TREE_CONSTANT but that isn't an actual constant. CODE must be a
744 commutative arithmetic operation. Store the constant part into *CONP,
745 the literal in *LITP and return the variable part. If a part isn't
746 present, set it to null. If the tree does not decompose in this way,
747 return the entire tree as the variable part and the other parts as null.
749 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
750 case, we negate an operand that was subtracted. Except if it is a
751 literal for which we use *MINUS_LITP instead.
753 If NEGATE_P is true, we are negating all of IN, again except a literal
754 for which we use *MINUS_LITP instead. If a variable part is of pointer
755 type, it is negated after converting to TYPE. This prevents us from
756 generating illegal MINUS pointer expression. LOC is the location of
757 the converted variable part.
759 If IN is itself a literal or constant, return it as appropriate.
761 Note that we do not guarantee that any of the three values will be the
762 same type as IN, but they will have the same signedness and mode. */
764 static tree
765 split_tree (location_t loc, tree in, tree type, enum tree_code code,
766 tree *conp, tree *litp, tree *minus_litp, int negate_p)
768 tree var = 0;
770 *conp = 0;
771 *litp = 0;
772 *minus_litp = 0;
774 /* Strip any conversions that don't change the machine mode or signedness. */
775 STRIP_SIGN_NOPS (in);
777 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
778 || TREE_CODE (in) == FIXED_CST)
779 *litp = in;
780 else if (TREE_CODE (in) == code
781 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
782 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
783 /* We can associate addition and subtraction together (even
784 though the C standard doesn't say so) for integers because
785 the value is not affected. For reals, the value might be
786 affected, so we can't. */
787 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
788 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
790 tree op0 = TREE_OPERAND (in, 0);
791 tree op1 = TREE_OPERAND (in, 1);
792 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
793 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
795 /* First see if either of the operands is a literal, then a constant. */
796 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
797 || TREE_CODE (op0) == FIXED_CST)
798 *litp = op0, op0 = 0;
799 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
800 || TREE_CODE (op1) == FIXED_CST)
801 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
803 if (op0 != 0 && TREE_CONSTANT (op0))
804 *conp = op0, op0 = 0;
805 else if (op1 != 0 && TREE_CONSTANT (op1))
806 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
808 /* If we haven't dealt with either operand, this is not a case we can
809 decompose. Otherwise, VAR is either of the ones remaining, if any. */
810 if (op0 != 0 && op1 != 0)
811 var = in;
812 else if (op0 != 0)
813 var = op0;
814 else
815 var = op1, neg_var_p = neg1_p;
817 /* Now do any needed negations. */
818 if (neg_litp_p)
819 *minus_litp = *litp, *litp = 0;
820 if (neg_conp_p)
821 *conp = negate_expr (*conp);
822 if (neg_var_p && var)
824 /* Convert to TYPE before negating. */
825 var = fold_convert_loc (loc, type, var);
826 var = negate_expr (var);
829 else if (TREE_CONSTANT (in))
830 *conp = in;
831 else if (TREE_CODE (in) == BIT_NOT_EXPR
832 && code == PLUS_EXPR)
834 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
835 when IN is constant. */
836 *minus_litp = build_one_cst (TREE_TYPE (in));
837 var = negate_expr (TREE_OPERAND (in, 0));
839 else
840 var = in;
842 if (negate_p)
844 if (*litp)
845 *minus_litp = *litp, *litp = 0;
846 else if (*minus_litp)
847 *litp = *minus_litp, *minus_litp = 0;
848 *conp = negate_expr (*conp);
849 if (var)
851 /* Convert to TYPE before negating. */
852 var = fold_convert_loc (loc, type, var);
853 var = negate_expr (var);
857 return var;
860 /* Re-associate trees split by the above function. T1 and T2 are
861 either expressions to associate or null. Return the new
862 expression, if any. LOC is the location of the new expression. If
863 we build an operation, do it in TYPE and with CODE. */
865 static tree
866 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
868 if (t1 == 0)
869 return t2;
870 else if (t2 == 0)
871 return t1;
873 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
874 try to fold this since we will have infinite recursion. But do
875 deal with any NEGATE_EXPRs. */
876 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
877 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
879 if (code == PLUS_EXPR)
881 if (TREE_CODE (t1) == NEGATE_EXPR)
882 return build2_loc (loc, MINUS_EXPR, type,
883 fold_convert_loc (loc, type, t2),
884 fold_convert_loc (loc, type,
885 TREE_OPERAND (t1, 0)));
886 else if (TREE_CODE (t2) == NEGATE_EXPR)
887 return build2_loc (loc, MINUS_EXPR, type,
888 fold_convert_loc (loc, type, t1),
889 fold_convert_loc (loc, type,
890 TREE_OPERAND (t2, 0)));
891 else if (integer_zerop (t2))
892 return fold_convert_loc (loc, type, t1);
894 else if (code == MINUS_EXPR)
896 if (integer_zerop (t2))
897 return fold_convert_loc (loc, type, t1);
900 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
901 fold_convert_loc (loc, type, t2));
904 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
905 fold_convert_loc (loc, type, t2));
908 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
909 for use in int_const_binop, size_binop and size_diffop. */
911 static bool
912 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
914 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
915 return false;
916 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
917 return false;
919 switch (code)
921 case LSHIFT_EXPR:
922 case RSHIFT_EXPR:
923 case LROTATE_EXPR:
924 case RROTATE_EXPR:
925 return true;
927 default:
928 break;
931 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
932 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
933 && TYPE_MODE (type1) == TYPE_MODE (type2);
937 /* Combine two integer constants ARG1 and ARG2 under operation CODE
938 to produce a new constant. Return NULL_TREE if we don't know how
939 to evaluate CODE at compile-time. */
941 static tree
942 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
943 int overflowable)
945 wide_int res;
946 tree t;
947 tree type = TREE_TYPE (arg1);
948 signop sign = TYPE_SIGN (type);
949 bool overflow = false;
951 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
953 switch (code)
955 case BIT_IOR_EXPR:
956 res = wi::bit_or (arg1, arg2);
957 break;
959 case BIT_XOR_EXPR:
960 res = wi::bit_xor (arg1, arg2);
961 break;
963 case BIT_AND_EXPR:
964 res = wi::bit_and (arg1, arg2);
965 break;
967 case RSHIFT_EXPR:
968 case LSHIFT_EXPR:
969 if (wi::neg_p (arg2))
971 arg2 = -arg2;
972 if (code == RSHIFT_EXPR)
973 code = LSHIFT_EXPR;
974 else
975 code = RSHIFT_EXPR;
978 if (code == RSHIFT_EXPR)
979 /* It's unclear from the C standard whether shifts can overflow.
980 The following code ignores overflow; perhaps a C standard
981 interpretation ruling is needed. */
982 res = wi::rshift (arg1, arg2, sign);
983 else
984 res = wi::lshift (arg1, arg2);
985 break;
987 case RROTATE_EXPR:
988 case LROTATE_EXPR:
989 if (wi::neg_p (arg2))
991 arg2 = -arg2;
992 if (code == RROTATE_EXPR)
993 code = LROTATE_EXPR;
994 else
995 code = RROTATE_EXPR;
998 if (code == RROTATE_EXPR)
999 res = wi::rrotate (arg1, arg2);
1000 else
1001 res = wi::lrotate (arg1, arg2);
1002 break;
1004 case PLUS_EXPR:
1005 res = wi::add (arg1, arg2, sign, &overflow);
1006 break;
1008 case MINUS_EXPR:
1009 res = wi::sub (arg1, arg2, sign, &overflow);
1010 break;
1012 case MULT_EXPR:
1013 res = wi::mul (arg1, arg2, sign, &overflow);
1014 break;
1016 case MULT_HIGHPART_EXPR:
1017 res = wi::mul_high (arg1, arg2, sign);
1018 break;
1020 case TRUNC_DIV_EXPR:
1021 case EXACT_DIV_EXPR:
1022 if (arg2 == 0)
1023 return NULL_TREE;
1024 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1025 break;
1027 case FLOOR_DIV_EXPR:
1028 if (arg2 == 0)
1029 return NULL_TREE;
1030 res = wi::div_floor (arg1, arg2, sign, &overflow);
1031 break;
1033 case CEIL_DIV_EXPR:
1034 if (arg2 == 0)
1035 return NULL_TREE;
1036 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1037 break;
1039 case ROUND_DIV_EXPR:
1040 if (arg2 == 0)
1041 return NULL_TREE;
1042 res = wi::div_round (arg1, arg2, sign, &overflow);
1043 break;
1045 case TRUNC_MOD_EXPR:
1046 if (arg2 == 0)
1047 return NULL_TREE;
1048 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1049 break;
1051 case FLOOR_MOD_EXPR:
1052 if (arg2 == 0)
1053 return NULL_TREE;
1054 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1055 break;
1057 case CEIL_MOD_EXPR:
1058 if (arg2 == 0)
1059 return NULL_TREE;
1060 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1061 break;
1063 case ROUND_MOD_EXPR:
1064 if (arg2 == 0)
1065 return NULL_TREE;
1066 res = wi::mod_round (arg1, arg2, sign, &overflow);
1067 break;
1069 case MIN_EXPR:
1070 res = wi::min (arg1, arg2, sign);
1071 break;
1073 case MAX_EXPR:
1074 res = wi::max (arg1, arg2, sign);
1075 break;
1077 default:
1078 return NULL_TREE;
1081 t = force_fit_type (type, res, overflowable,
1082 (((sign == SIGNED || overflowable == -1)
1083 && overflow)
1084 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1086 return t;
1089 tree
1090 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1092 return int_const_binop_1 (code, arg1, arg2, 1);
1095 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1096 constant. We assume ARG1 and ARG2 have the same data type, or at least
1097 are the same kind of constant and the same machine mode. Return zero if
1098 combining the constants is not allowed in the current operating mode. */
1100 static tree
1101 const_binop (enum tree_code code, tree arg1, tree arg2)
1103 /* Sanity check for the recursive cases. */
1104 if (!arg1 || !arg2)
1105 return NULL_TREE;
1107 STRIP_NOPS (arg1);
1108 STRIP_NOPS (arg2);
1110 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1112 if (code == POINTER_PLUS_EXPR)
1113 return int_const_binop (PLUS_EXPR,
1114 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1116 return int_const_binop (code, arg1, arg2);
1119 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1121 machine_mode mode;
1122 REAL_VALUE_TYPE d1;
1123 REAL_VALUE_TYPE d2;
1124 REAL_VALUE_TYPE value;
1125 REAL_VALUE_TYPE result;
1126 bool inexact;
1127 tree t, type;
1129 /* The following codes are handled by real_arithmetic. */
1130 switch (code)
1132 case PLUS_EXPR:
1133 case MINUS_EXPR:
1134 case MULT_EXPR:
1135 case RDIV_EXPR:
1136 case MIN_EXPR:
1137 case MAX_EXPR:
1138 break;
1140 default:
1141 return NULL_TREE;
1144 d1 = TREE_REAL_CST (arg1);
1145 d2 = TREE_REAL_CST (arg2);
1147 type = TREE_TYPE (arg1);
1148 mode = TYPE_MODE (type);
1150 /* Don't perform operation if we honor signaling NaNs and
1151 either operand is a signaling NaN. */
1152 if (HONOR_SNANS (mode)
1153 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1154 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1155 return NULL_TREE;
1157 /* Don't perform operation if it would raise a division
1158 by zero exception. */
1159 if (code == RDIV_EXPR
1160 && real_equal (&d2, &dconst0)
1161 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1162 return NULL_TREE;
1164 /* If either operand is a NaN, just return it. Otherwise, set up
1165 for floating-point trap; we return an overflow. */
1166 if (REAL_VALUE_ISNAN (d1))
1168 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1169 is off. */
1170 d1.signalling = 0;
1171 t = build_real (type, d1);
1172 return t;
1174 else if (REAL_VALUE_ISNAN (d2))
1176 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1177 is off. */
1178 d2.signalling = 0;
1179 t = build_real (type, d2);
1180 return t;
1183 inexact = real_arithmetic (&value, code, &d1, &d2);
1184 real_convert (&result, mode, &value);
1186 /* Don't constant fold this floating point operation if
1187 the result has overflowed and flag_trapping_math. */
1188 if (flag_trapping_math
1189 && MODE_HAS_INFINITIES (mode)
1190 && REAL_VALUE_ISINF (result)
1191 && !REAL_VALUE_ISINF (d1)
1192 && !REAL_VALUE_ISINF (d2))
1193 return NULL_TREE;
1195 /* Don't constant fold this floating point operation if the
1196 result may dependent upon the run-time rounding mode and
1197 flag_rounding_math is set, or if GCC's software emulation
1198 is unable to accurately represent the result. */
1199 if ((flag_rounding_math
1200 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1201 && (inexact || !real_identical (&result, &value)))
1202 return NULL_TREE;
1204 t = build_real (type, result);
1206 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1207 return t;
1210 if (TREE_CODE (arg1) == FIXED_CST)
1212 FIXED_VALUE_TYPE f1;
1213 FIXED_VALUE_TYPE f2;
1214 FIXED_VALUE_TYPE result;
1215 tree t, type;
1216 int sat_p;
1217 bool overflow_p;
1219 /* The following codes are handled by fixed_arithmetic. */
1220 switch (code)
1222 case PLUS_EXPR:
1223 case MINUS_EXPR:
1224 case MULT_EXPR:
1225 case TRUNC_DIV_EXPR:
1226 if (TREE_CODE (arg2) != FIXED_CST)
1227 return NULL_TREE;
1228 f2 = TREE_FIXED_CST (arg2);
1229 break;
1231 case LSHIFT_EXPR:
1232 case RSHIFT_EXPR:
1234 if (TREE_CODE (arg2) != INTEGER_CST)
1235 return NULL_TREE;
1236 wide_int w2 = arg2;
1237 f2.data.high = w2.elt (1);
1238 f2.data.low = w2.elt (0);
1239 f2.mode = SImode;
1241 break;
1243 default:
1244 return NULL_TREE;
1247 f1 = TREE_FIXED_CST (arg1);
1248 type = TREE_TYPE (arg1);
1249 sat_p = TYPE_SATURATING (type);
1250 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1251 t = build_fixed (type, result);
1252 /* Propagate overflow flags. */
1253 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1254 TREE_OVERFLOW (t) = 1;
1255 return t;
1258 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1260 tree type = TREE_TYPE (arg1);
1261 tree r1 = TREE_REALPART (arg1);
1262 tree i1 = TREE_IMAGPART (arg1);
1263 tree r2 = TREE_REALPART (arg2);
1264 tree i2 = TREE_IMAGPART (arg2);
1265 tree real, imag;
1267 switch (code)
1269 case PLUS_EXPR:
1270 case MINUS_EXPR:
1271 real = const_binop (code, r1, r2);
1272 imag = const_binop (code, i1, i2);
1273 break;
1275 case MULT_EXPR:
1276 if (COMPLEX_FLOAT_TYPE_P (type))
1277 return do_mpc_arg2 (arg1, arg2, type,
1278 /* do_nonfinite= */ folding_initializer,
1279 mpc_mul);
1281 real = const_binop (MINUS_EXPR,
1282 const_binop (MULT_EXPR, r1, r2),
1283 const_binop (MULT_EXPR, i1, i2));
1284 imag = const_binop (PLUS_EXPR,
1285 const_binop (MULT_EXPR, r1, i2),
1286 const_binop (MULT_EXPR, i1, r2));
1287 break;
1289 case RDIV_EXPR:
1290 if (COMPLEX_FLOAT_TYPE_P (type))
1291 return do_mpc_arg2 (arg1, arg2, type,
1292 /* do_nonfinite= */ folding_initializer,
1293 mpc_div);
1294 /* Fallthru. */
1295 case TRUNC_DIV_EXPR:
1296 case CEIL_DIV_EXPR:
1297 case FLOOR_DIV_EXPR:
1298 case ROUND_DIV_EXPR:
1299 if (flag_complex_method == 0)
1301 /* Keep this algorithm in sync with
1302 tree-complex.c:expand_complex_div_straight().
1304 Expand complex division to scalars, straightforward algorithm.
1305 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1306 t = br*br + bi*bi
1308 tree magsquared
1309 = const_binop (PLUS_EXPR,
1310 const_binop (MULT_EXPR, r2, r2),
1311 const_binop (MULT_EXPR, i2, i2));
1312 tree t1
1313 = const_binop (PLUS_EXPR,
1314 const_binop (MULT_EXPR, r1, r2),
1315 const_binop (MULT_EXPR, i1, i2));
1316 tree t2
1317 = const_binop (MINUS_EXPR,
1318 const_binop (MULT_EXPR, i1, r2),
1319 const_binop (MULT_EXPR, r1, i2));
1321 real = const_binop (code, t1, magsquared);
1322 imag = const_binop (code, t2, magsquared);
1324 else
1326 /* Keep this algorithm in sync with
1327 tree-complex.c:expand_complex_div_wide().
1329 Expand complex division to scalars, modified algorithm to minimize
1330 overflow with wide input ranges. */
1331 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1332 fold_abs_const (r2, TREE_TYPE (type)),
1333 fold_abs_const (i2, TREE_TYPE (type)));
1335 if (integer_nonzerop (compare))
1337 /* In the TRUE branch, we compute
1338 ratio = br/bi;
1339 div = (br * ratio) + bi;
1340 tr = (ar * ratio) + ai;
1341 ti = (ai * ratio) - ar;
1342 tr = tr / div;
1343 ti = ti / div; */
1344 tree ratio = const_binop (code, r2, i2);
1345 tree div = const_binop (PLUS_EXPR, i2,
1346 const_binop (MULT_EXPR, r2, ratio));
1347 real = const_binop (MULT_EXPR, r1, ratio);
1348 real = const_binop (PLUS_EXPR, real, i1);
1349 real = const_binop (code, real, div);
1351 imag = const_binop (MULT_EXPR, i1, ratio);
1352 imag = const_binop (MINUS_EXPR, imag, r1);
1353 imag = const_binop (code, imag, div);
1355 else
1357 /* In the FALSE branch, we compute
1358 ratio = d/c;
1359 divisor = (d * ratio) + c;
1360 tr = (b * ratio) + a;
1361 ti = b - (a * ratio);
1362 tr = tr / div;
1363 ti = ti / div; */
1364 tree ratio = const_binop (code, i2, r2);
1365 tree div = const_binop (PLUS_EXPR, r2,
1366 const_binop (MULT_EXPR, i2, ratio));
1368 real = const_binop (MULT_EXPR, i1, ratio);
1369 real = const_binop (PLUS_EXPR, real, r1);
1370 real = const_binop (code, real, div);
1372 imag = const_binop (MULT_EXPR, r1, ratio);
1373 imag = const_binop (MINUS_EXPR, i1, imag);
1374 imag = const_binop (code, imag, div);
1377 break;
1379 default:
1380 return NULL_TREE;
1383 if (real && imag)
1384 return build_complex (type, real, imag);
1387 if (TREE_CODE (arg1) == VECTOR_CST
1388 && TREE_CODE (arg2) == VECTOR_CST)
1390 tree type = TREE_TYPE (arg1);
1391 int count = TYPE_VECTOR_SUBPARTS (type), i;
1392 tree *elts = XALLOCAVEC (tree, count);
1394 for (i = 0; i < count; i++)
1396 tree elem1 = VECTOR_CST_ELT (arg1, i);
1397 tree elem2 = VECTOR_CST_ELT (arg2, i);
1399 elts[i] = const_binop (code, elem1, elem2);
1401 /* It is possible that const_binop cannot handle the given
1402 code and return NULL_TREE */
1403 if (elts[i] == NULL_TREE)
1404 return NULL_TREE;
1407 return build_vector (type, elts);
1410 /* Shifts allow a scalar offset for a vector. */
1411 if (TREE_CODE (arg1) == VECTOR_CST
1412 && TREE_CODE (arg2) == INTEGER_CST)
1414 tree type = TREE_TYPE (arg1);
1415 int count = TYPE_VECTOR_SUBPARTS (type), i;
1416 tree *elts = XALLOCAVEC (tree, count);
1418 for (i = 0; i < count; i++)
1420 tree elem1 = VECTOR_CST_ELT (arg1, i);
1422 elts[i] = const_binop (code, elem1, arg2);
1424 /* It is possible that const_binop cannot handle the given
1425 code and return NULL_TREE. */
1426 if (elts[i] == NULL_TREE)
1427 return NULL_TREE;
1430 return build_vector (type, elts);
1432 return NULL_TREE;
1435 /* Overload that adds a TYPE parameter to be able to dispatch
1436 to fold_relational_const. */
1438 tree
1439 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1441 if (TREE_CODE_CLASS (code) == tcc_comparison)
1442 return fold_relational_const (code, type, arg1, arg2);
1444 /* ??? Until we make the const_binop worker take the type of the
1445 result as argument put those cases that need it here. */
1446 switch (code)
1448 case COMPLEX_EXPR:
1449 if ((TREE_CODE (arg1) == REAL_CST
1450 && TREE_CODE (arg2) == REAL_CST)
1451 || (TREE_CODE (arg1) == INTEGER_CST
1452 && TREE_CODE (arg2) == INTEGER_CST))
1453 return build_complex (type, arg1, arg2);
1454 return NULL_TREE;
1456 case VEC_PACK_TRUNC_EXPR:
1457 case VEC_PACK_FIX_TRUNC_EXPR:
1459 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1460 tree *elts;
1462 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1463 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1464 if (TREE_CODE (arg1) != VECTOR_CST
1465 || TREE_CODE (arg2) != VECTOR_CST)
1466 return NULL_TREE;
1468 elts = XALLOCAVEC (tree, nelts);
1469 if (!vec_cst_ctor_to_array (arg1, elts)
1470 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1471 return NULL_TREE;
1473 for (i = 0; i < nelts; i++)
1475 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1476 ? NOP_EXPR : FIX_TRUNC_EXPR,
1477 TREE_TYPE (type), elts[i]);
1478 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1479 return NULL_TREE;
1482 return build_vector (type, elts);
1485 case VEC_WIDEN_MULT_LO_EXPR:
1486 case VEC_WIDEN_MULT_HI_EXPR:
1487 case VEC_WIDEN_MULT_EVEN_EXPR:
1488 case VEC_WIDEN_MULT_ODD_EXPR:
1490 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1491 unsigned int out, ofs, scale;
1492 tree *elts;
1494 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1495 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1496 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1497 return NULL_TREE;
1499 elts = XALLOCAVEC (tree, nelts * 4);
1500 if (!vec_cst_ctor_to_array (arg1, elts)
1501 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1502 return NULL_TREE;
1504 if (code == VEC_WIDEN_MULT_LO_EXPR)
1505 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1506 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1507 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1508 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1509 scale = 1, ofs = 0;
1510 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1511 scale = 1, ofs = 1;
1513 for (out = 0; out < nelts; out++)
1515 unsigned int in1 = (out << scale) + ofs;
1516 unsigned int in2 = in1 + nelts * 2;
1517 tree t1, t2;
1519 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1520 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1522 if (t1 == NULL_TREE || t2 == NULL_TREE)
1523 return NULL_TREE;
1524 elts[out] = const_binop (MULT_EXPR, t1, t2);
1525 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1526 return NULL_TREE;
1529 return build_vector (type, elts);
1532 default:;
1535 if (TREE_CODE_CLASS (code) != tcc_binary)
1536 return NULL_TREE;
1538 /* Make sure type and arg0 have the same saturating flag. */
1539 gcc_checking_assert (TYPE_SATURATING (type)
1540 == TYPE_SATURATING (TREE_TYPE (arg1)));
1542 return const_binop (code, arg1, arg2);
1545 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1546 Return zero if computing the constants is not possible. */
1548 tree
1549 const_unop (enum tree_code code, tree type, tree arg0)
1551 /* Don't perform the operation, other than NEGATE and ABS, if
1552 flag_signaling_nans is on and the operand is a signaling NaN. */
1553 if (TREE_CODE (arg0) == REAL_CST
1554 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1555 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1556 && code != NEGATE_EXPR
1557 && code != ABS_EXPR)
1558 return NULL_TREE;
1560 switch (code)
1562 CASE_CONVERT:
1563 case FLOAT_EXPR:
1564 case FIX_TRUNC_EXPR:
1565 case FIXED_CONVERT_EXPR:
1566 return fold_convert_const (code, type, arg0);
1568 case ADDR_SPACE_CONVERT_EXPR:
1569 /* If the source address is 0, and the source address space
1570 cannot have a valid object at 0, fold to dest type null. */
1571 if (integer_zerop (arg0)
1572 && !(targetm.addr_space.zero_address_valid
1573 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1574 return fold_convert_const (code, type, arg0);
1575 break;
1577 case VIEW_CONVERT_EXPR:
1578 return fold_view_convert_expr (type, arg0);
1580 case NEGATE_EXPR:
1582 /* Can't call fold_negate_const directly here as that doesn't
1583 handle all cases and we might not be able to negate some
1584 constants. */
1585 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1586 if (tem && CONSTANT_CLASS_P (tem))
1587 return tem;
1588 break;
1591 case ABS_EXPR:
1592 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1593 return fold_abs_const (arg0, type);
1594 break;
1596 case CONJ_EXPR:
1597 if (TREE_CODE (arg0) == COMPLEX_CST)
1599 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1600 TREE_TYPE (type));
1601 return build_complex (type, TREE_REALPART (arg0), ipart);
1603 break;
1605 case BIT_NOT_EXPR:
1606 if (TREE_CODE (arg0) == INTEGER_CST)
1607 return fold_not_const (arg0, type);
1608 /* Perform BIT_NOT_EXPR on each element individually. */
1609 else if (TREE_CODE (arg0) == VECTOR_CST)
1611 tree *elements;
1612 tree elem;
1613 unsigned count = VECTOR_CST_NELTS (arg0), i;
1615 elements = XALLOCAVEC (tree, count);
1616 for (i = 0; i < count; i++)
1618 elem = VECTOR_CST_ELT (arg0, i);
1619 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1620 if (elem == NULL_TREE)
1621 break;
1622 elements[i] = elem;
1624 if (i == count)
1625 return build_vector (type, elements);
1627 break;
1629 case TRUTH_NOT_EXPR:
1630 if (TREE_CODE (arg0) == INTEGER_CST)
1631 return constant_boolean_node (integer_zerop (arg0), type);
1632 break;
1634 case REALPART_EXPR:
1635 if (TREE_CODE (arg0) == COMPLEX_CST)
1636 return fold_convert (type, TREE_REALPART (arg0));
1637 break;
1639 case IMAGPART_EXPR:
1640 if (TREE_CODE (arg0) == COMPLEX_CST)
1641 return fold_convert (type, TREE_IMAGPART (arg0));
1642 break;
1644 case VEC_UNPACK_LO_EXPR:
1645 case VEC_UNPACK_HI_EXPR:
1646 case VEC_UNPACK_FLOAT_LO_EXPR:
1647 case VEC_UNPACK_FLOAT_HI_EXPR:
1649 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1650 tree *elts;
1651 enum tree_code subcode;
1653 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1654 if (TREE_CODE (arg0) != VECTOR_CST)
1655 return NULL_TREE;
1657 elts = XALLOCAVEC (tree, nelts * 2);
1658 if (!vec_cst_ctor_to_array (arg0, elts))
1659 return NULL_TREE;
1661 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1662 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1663 elts += nelts;
1665 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1666 subcode = NOP_EXPR;
1667 else
1668 subcode = FLOAT_EXPR;
1670 for (i = 0; i < nelts; i++)
1672 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1673 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1674 return NULL_TREE;
1677 return build_vector (type, elts);
1680 case REDUC_MIN_EXPR:
1681 case REDUC_MAX_EXPR:
1682 case REDUC_PLUS_EXPR:
1684 unsigned int nelts, i;
1685 tree *elts;
1686 enum tree_code subcode;
1688 if (TREE_CODE (arg0) != VECTOR_CST)
1689 return NULL_TREE;
1690 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1692 elts = XALLOCAVEC (tree, nelts);
1693 if (!vec_cst_ctor_to_array (arg0, elts))
1694 return NULL_TREE;
1696 switch (code)
1698 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1699 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1700 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1701 default: gcc_unreachable ();
1704 for (i = 1; i < nelts; i++)
1706 elts[0] = const_binop (subcode, elts[0], elts[i]);
1707 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1708 return NULL_TREE;
1711 return elts[0];
1714 default:
1715 break;
1718 return NULL_TREE;
1721 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1722 indicates which particular sizetype to create. */
1724 tree
1725 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1727 return build_int_cst (sizetype_tab[(int) kind], number);
1730 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1731 is a tree code. The type of the result is taken from the operands.
1732 Both must be equivalent integer types, ala int_binop_types_match_p.
1733 If the operands are constant, so is the result. */
1735 tree
1736 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1738 tree type = TREE_TYPE (arg0);
1740 if (arg0 == error_mark_node || arg1 == error_mark_node)
1741 return error_mark_node;
1743 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1744 TREE_TYPE (arg1)));
1746 /* Handle the special case of two integer constants faster. */
1747 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1749 /* And some specific cases even faster than that. */
1750 if (code == PLUS_EXPR)
1752 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1753 return arg1;
1754 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1755 return arg0;
1757 else if (code == MINUS_EXPR)
1759 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1760 return arg0;
1762 else if (code == MULT_EXPR)
1764 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1765 return arg1;
1768 /* Handle general case of two integer constants. For sizetype
1769 constant calculations we always want to know about overflow,
1770 even in the unsigned case. */
1771 return int_const_binop_1 (code, arg0, arg1, -1);
1774 return fold_build2_loc (loc, code, type, arg0, arg1);
1777 /* Given two values, either both of sizetype or both of bitsizetype,
1778 compute the difference between the two values. Return the value
1779 in signed type corresponding to the type of the operands. */
1781 tree
1782 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1784 tree type = TREE_TYPE (arg0);
1785 tree ctype;
1787 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1788 TREE_TYPE (arg1)));
1790 /* If the type is already signed, just do the simple thing. */
1791 if (!TYPE_UNSIGNED (type))
1792 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1794 if (type == sizetype)
1795 ctype = ssizetype;
1796 else if (type == bitsizetype)
1797 ctype = sbitsizetype;
1798 else
1799 ctype = signed_type_for (type);
1801 /* If either operand is not a constant, do the conversions to the signed
1802 type and subtract. The hardware will do the right thing with any
1803 overflow in the subtraction. */
1804 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1805 return size_binop_loc (loc, MINUS_EXPR,
1806 fold_convert_loc (loc, ctype, arg0),
1807 fold_convert_loc (loc, ctype, arg1));
1809 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1810 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1811 overflow) and negate (which can't either). Special-case a result
1812 of zero while we're here. */
1813 if (tree_int_cst_equal (arg0, arg1))
1814 return build_int_cst (ctype, 0);
1815 else if (tree_int_cst_lt (arg1, arg0))
1816 return fold_convert_loc (loc, ctype,
1817 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1818 else
1819 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1820 fold_convert_loc (loc, ctype,
1821 size_binop_loc (loc,
1822 MINUS_EXPR,
1823 arg1, arg0)));
1826 /* A subroutine of fold_convert_const handling conversions of an
1827 INTEGER_CST to another integer type. */
1829 static tree
1830 fold_convert_const_int_from_int (tree type, const_tree arg1)
1832 /* Given an integer constant, make new constant with new type,
1833 appropriately sign-extended or truncated. Use widest_int
1834 so that any extension is done according ARG1's type. */
1835 return force_fit_type (type, wi::to_widest (arg1),
1836 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1837 TREE_OVERFLOW (arg1));
1840 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1841 to an integer type. */
1843 static tree
1844 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1846 bool overflow = false;
1847 tree t;
1849 /* The following code implements the floating point to integer
1850 conversion rules required by the Java Language Specification,
1851 that IEEE NaNs are mapped to zero and values that overflow
1852 the target precision saturate, i.e. values greater than
1853 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1854 are mapped to INT_MIN. These semantics are allowed by the
1855 C and C++ standards that simply state that the behavior of
1856 FP-to-integer conversion is unspecified upon overflow. */
1858 wide_int val;
1859 REAL_VALUE_TYPE r;
1860 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1862 switch (code)
1864 case FIX_TRUNC_EXPR:
1865 real_trunc (&r, VOIDmode, &x);
1866 break;
1868 default:
1869 gcc_unreachable ();
1872 /* If R is NaN, return zero and show we have an overflow. */
1873 if (REAL_VALUE_ISNAN (r))
1875 overflow = true;
1876 val = wi::zero (TYPE_PRECISION (type));
1879 /* See if R is less than the lower bound or greater than the
1880 upper bound. */
1882 if (! overflow)
1884 tree lt = TYPE_MIN_VALUE (type);
1885 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1886 if (real_less (&r, &l))
1888 overflow = true;
1889 val = lt;
1893 if (! overflow)
1895 tree ut = TYPE_MAX_VALUE (type);
1896 if (ut)
1898 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1899 if (real_less (&u, &r))
1901 overflow = true;
1902 val = ut;
1907 if (! overflow)
1908 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1910 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1911 return t;
1914 /* A subroutine of fold_convert_const handling conversions of a
1915 FIXED_CST to an integer type. */
1917 static tree
1918 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1920 tree t;
1921 double_int temp, temp_trunc;
1922 unsigned int mode;
1924 /* Right shift FIXED_CST to temp by fbit. */
1925 temp = TREE_FIXED_CST (arg1).data;
1926 mode = TREE_FIXED_CST (arg1).mode;
1927 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1929 temp = temp.rshift (GET_MODE_FBIT (mode),
1930 HOST_BITS_PER_DOUBLE_INT,
1931 SIGNED_FIXED_POINT_MODE_P (mode));
1933 /* Left shift temp to temp_trunc by fbit. */
1934 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1935 HOST_BITS_PER_DOUBLE_INT,
1936 SIGNED_FIXED_POINT_MODE_P (mode));
1938 else
1940 temp = double_int_zero;
1941 temp_trunc = double_int_zero;
1944 /* If FIXED_CST is negative, we need to round the value toward 0.
1945 By checking if the fractional bits are not zero to add 1 to temp. */
1946 if (SIGNED_FIXED_POINT_MODE_P (mode)
1947 && temp_trunc.is_negative ()
1948 && TREE_FIXED_CST (arg1).data != temp_trunc)
1949 temp += double_int_one;
1951 /* Given a fixed-point constant, make new constant with new type,
1952 appropriately sign-extended or truncated. */
1953 t = force_fit_type (type, temp, -1,
1954 (temp.is_negative ()
1955 && (TYPE_UNSIGNED (type)
1956 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1957 | TREE_OVERFLOW (arg1));
1959 return t;
1962 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1963 to another floating point type. */
1965 static tree
1966 fold_convert_const_real_from_real (tree type, const_tree arg1)
1968 REAL_VALUE_TYPE value;
1969 tree t;
1971 /* Don't perform the operation if flag_signaling_nans is on
1972 and the operand is a signaling NaN. */
1973 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1974 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1975 return NULL_TREE;
1977 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1978 t = build_real (type, value);
1980 /* If converting an infinity or NAN to a representation that doesn't
1981 have one, set the overflow bit so that we can produce some kind of
1982 error message at the appropriate point if necessary. It's not the
1983 most user-friendly message, but it's better than nothing. */
1984 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1985 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1986 TREE_OVERFLOW (t) = 1;
1987 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1988 && !MODE_HAS_NANS (TYPE_MODE (type)))
1989 TREE_OVERFLOW (t) = 1;
1990 /* Regular overflow, conversion produced an infinity in a mode that
1991 can't represent them. */
1992 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1993 && REAL_VALUE_ISINF (value)
1994 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1995 TREE_OVERFLOW (t) = 1;
1996 else
1997 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1998 return t;
2001 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2002 to a floating point type. */
2004 static tree
2005 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2007 REAL_VALUE_TYPE value;
2008 tree t;
2010 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2011 t = build_real (type, value);
2013 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2014 return t;
2017 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2018 to another fixed-point type. */
2020 static tree
2021 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2023 FIXED_VALUE_TYPE value;
2024 tree t;
2025 bool overflow_p;
2027 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2028 TYPE_SATURATING (type));
2029 t = build_fixed (type, value);
2031 /* Propagate overflow flags. */
2032 if (overflow_p | TREE_OVERFLOW (arg1))
2033 TREE_OVERFLOW (t) = 1;
2034 return t;
2037 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2038 to a fixed-point type. */
2040 static tree
2041 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2043 FIXED_VALUE_TYPE value;
2044 tree t;
2045 bool overflow_p;
2046 double_int di;
2048 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2050 di.low = TREE_INT_CST_ELT (arg1, 0);
2051 if (TREE_INT_CST_NUNITS (arg1) == 1)
2052 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2053 else
2054 di.high = TREE_INT_CST_ELT (arg1, 1);
2056 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2057 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2058 TYPE_SATURATING (type));
2059 t = build_fixed (type, value);
2061 /* Propagate overflow flags. */
2062 if (overflow_p | TREE_OVERFLOW (arg1))
2063 TREE_OVERFLOW (t) = 1;
2064 return t;
2067 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2068 to a fixed-point type. */
2070 static tree
2071 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2073 FIXED_VALUE_TYPE value;
2074 tree t;
2075 bool overflow_p;
2077 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2078 &TREE_REAL_CST (arg1),
2079 TYPE_SATURATING (type));
2080 t = build_fixed (type, value);
2082 /* Propagate overflow flags. */
2083 if (overflow_p | TREE_OVERFLOW (arg1))
2084 TREE_OVERFLOW (t) = 1;
2085 return t;
2088 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2089 type TYPE. If no simplification can be done return NULL_TREE. */
2091 static tree
2092 fold_convert_const (enum tree_code code, tree type, tree arg1)
2094 if (TREE_TYPE (arg1) == type)
2095 return arg1;
2097 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2098 || TREE_CODE (type) == OFFSET_TYPE)
2100 if (TREE_CODE (arg1) == INTEGER_CST)
2101 return fold_convert_const_int_from_int (type, arg1);
2102 else if (TREE_CODE (arg1) == REAL_CST)
2103 return fold_convert_const_int_from_real (code, type, arg1);
2104 else if (TREE_CODE (arg1) == FIXED_CST)
2105 return fold_convert_const_int_from_fixed (type, arg1);
2107 else if (TREE_CODE (type) == REAL_TYPE)
2109 if (TREE_CODE (arg1) == INTEGER_CST)
2110 return build_real_from_int_cst (type, arg1);
2111 else if (TREE_CODE (arg1) == REAL_CST)
2112 return fold_convert_const_real_from_real (type, arg1);
2113 else if (TREE_CODE (arg1) == FIXED_CST)
2114 return fold_convert_const_real_from_fixed (type, arg1);
2116 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2118 if (TREE_CODE (arg1) == FIXED_CST)
2119 return fold_convert_const_fixed_from_fixed (type, arg1);
2120 else if (TREE_CODE (arg1) == INTEGER_CST)
2121 return fold_convert_const_fixed_from_int (type, arg1);
2122 else if (TREE_CODE (arg1) == REAL_CST)
2123 return fold_convert_const_fixed_from_real (type, arg1);
2125 else if (TREE_CODE (type) == VECTOR_TYPE)
2127 if (TREE_CODE (arg1) == VECTOR_CST
2128 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2130 int len = TYPE_VECTOR_SUBPARTS (type);
2131 tree elttype = TREE_TYPE (type);
2132 tree *v = XALLOCAVEC (tree, len);
2133 for (int i = 0; i < len; ++i)
2135 tree elt = VECTOR_CST_ELT (arg1, i);
2136 tree cvt = fold_convert_const (code, elttype, elt);
2137 if (cvt == NULL_TREE)
2138 return NULL_TREE;
2139 v[i] = cvt;
2141 return build_vector (type, v);
2144 return NULL_TREE;
2147 /* Construct a vector of zero elements of vector type TYPE. */
2149 static tree
2150 build_zero_vector (tree type)
2152 tree t;
2154 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2155 return build_vector_from_val (type, t);
2158 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2160 bool
2161 fold_convertible_p (const_tree type, const_tree arg)
2163 tree orig = TREE_TYPE (arg);
2165 if (type == orig)
2166 return true;
2168 if (TREE_CODE (arg) == ERROR_MARK
2169 || TREE_CODE (type) == ERROR_MARK
2170 || TREE_CODE (orig) == ERROR_MARK)
2171 return false;
2173 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2174 return true;
2176 switch (TREE_CODE (type))
2178 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2179 case POINTER_TYPE: case REFERENCE_TYPE:
2180 case OFFSET_TYPE:
2181 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2182 || TREE_CODE (orig) == OFFSET_TYPE);
2184 case REAL_TYPE:
2185 case FIXED_POINT_TYPE:
2186 case VECTOR_TYPE:
2187 case VOID_TYPE:
2188 return TREE_CODE (type) == TREE_CODE (orig);
2190 default:
2191 return false;
2195 /* Convert expression ARG to type TYPE. Used by the middle-end for
2196 simple conversions in preference to calling the front-end's convert. */
2198 tree
2199 fold_convert_loc (location_t loc, tree type, tree arg)
2201 tree orig = TREE_TYPE (arg);
2202 tree tem;
2204 if (type == orig)
2205 return arg;
2207 if (TREE_CODE (arg) == ERROR_MARK
2208 || TREE_CODE (type) == ERROR_MARK
2209 || TREE_CODE (orig) == ERROR_MARK)
2210 return error_mark_node;
2212 switch (TREE_CODE (type))
2214 case POINTER_TYPE:
2215 case REFERENCE_TYPE:
2216 /* Handle conversions between pointers to different address spaces. */
2217 if (POINTER_TYPE_P (orig)
2218 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2219 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2220 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2221 /* fall through */
2223 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2224 case OFFSET_TYPE:
2225 if (TREE_CODE (arg) == INTEGER_CST)
2227 tem = fold_convert_const (NOP_EXPR, type, arg);
2228 if (tem != NULL_TREE)
2229 return tem;
2231 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2232 || TREE_CODE (orig) == OFFSET_TYPE)
2233 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2234 if (TREE_CODE (orig) == COMPLEX_TYPE)
2235 return fold_convert_loc (loc, type,
2236 fold_build1_loc (loc, REALPART_EXPR,
2237 TREE_TYPE (orig), arg));
2238 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2239 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2240 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2242 case REAL_TYPE:
2243 if (TREE_CODE (arg) == INTEGER_CST)
2245 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2246 if (tem != NULL_TREE)
2247 return tem;
2249 else if (TREE_CODE (arg) == REAL_CST)
2251 tem = fold_convert_const (NOP_EXPR, type, arg);
2252 if (tem != NULL_TREE)
2253 return tem;
2255 else if (TREE_CODE (arg) == FIXED_CST)
2257 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2258 if (tem != NULL_TREE)
2259 return tem;
2262 switch (TREE_CODE (orig))
2264 case INTEGER_TYPE:
2265 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2266 case POINTER_TYPE: case REFERENCE_TYPE:
2267 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2269 case REAL_TYPE:
2270 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2272 case FIXED_POINT_TYPE:
2273 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2275 case COMPLEX_TYPE:
2276 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2277 return fold_convert_loc (loc, type, tem);
2279 default:
2280 gcc_unreachable ();
2283 case FIXED_POINT_TYPE:
2284 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2285 || TREE_CODE (arg) == REAL_CST)
2287 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2288 if (tem != NULL_TREE)
2289 goto fold_convert_exit;
2292 switch (TREE_CODE (orig))
2294 case FIXED_POINT_TYPE:
2295 case INTEGER_TYPE:
2296 case ENUMERAL_TYPE:
2297 case BOOLEAN_TYPE:
2298 case REAL_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 COMPLEX_TYPE:
2310 switch (TREE_CODE (orig))
2312 case INTEGER_TYPE:
2313 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2314 case POINTER_TYPE: case REFERENCE_TYPE:
2315 case REAL_TYPE:
2316 case FIXED_POINT_TYPE:
2317 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2318 fold_convert_loc (loc, TREE_TYPE (type), arg),
2319 fold_convert_loc (loc, TREE_TYPE (type),
2320 integer_zero_node));
2321 case COMPLEX_TYPE:
2323 tree rpart, ipart;
2325 if (TREE_CODE (arg) == COMPLEX_EXPR)
2327 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2328 TREE_OPERAND (arg, 0));
2329 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2330 TREE_OPERAND (arg, 1));
2331 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2334 arg = save_expr (arg);
2335 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2336 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2337 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2338 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2339 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2342 default:
2343 gcc_unreachable ();
2346 case VECTOR_TYPE:
2347 if (integer_zerop (arg))
2348 return build_zero_vector (type);
2349 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2350 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2351 || TREE_CODE (orig) == VECTOR_TYPE);
2352 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2354 case VOID_TYPE:
2355 tem = fold_ignored_result (arg);
2356 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2358 default:
2359 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2360 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2361 gcc_unreachable ();
2363 fold_convert_exit:
2364 protected_set_expr_location_unshare (tem, loc);
2365 return tem;
2368 /* Return false if expr can be assumed not to be an lvalue, true
2369 otherwise. */
2371 static bool
2372 maybe_lvalue_p (const_tree x)
2374 /* We only need to wrap lvalue tree codes. */
2375 switch (TREE_CODE (x))
2377 case VAR_DECL:
2378 case PARM_DECL:
2379 case RESULT_DECL:
2380 case LABEL_DECL:
2381 case FUNCTION_DECL:
2382 case SSA_NAME:
2384 case COMPONENT_REF:
2385 case MEM_REF:
2386 case INDIRECT_REF:
2387 case ARRAY_REF:
2388 case ARRAY_RANGE_REF:
2389 case BIT_FIELD_REF:
2390 case OBJ_TYPE_REF:
2392 case REALPART_EXPR:
2393 case IMAGPART_EXPR:
2394 case PREINCREMENT_EXPR:
2395 case PREDECREMENT_EXPR:
2396 case SAVE_EXPR:
2397 case TRY_CATCH_EXPR:
2398 case WITH_CLEANUP_EXPR:
2399 case COMPOUND_EXPR:
2400 case MODIFY_EXPR:
2401 case TARGET_EXPR:
2402 case COND_EXPR:
2403 case BIND_EXPR:
2404 break;
2406 default:
2407 /* Assume the worst for front-end tree codes. */
2408 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2409 break;
2410 return false;
2413 return true;
2416 /* Return an expr equal to X but certainly not valid as an lvalue. */
2418 tree
2419 non_lvalue_loc (location_t loc, tree x)
2421 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2422 us. */
2423 if (in_gimple_form)
2424 return x;
2426 if (! maybe_lvalue_p (x))
2427 return x;
2428 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2431 /* When pedantic, return an expr equal to X but certainly not valid as a
2432 pedantic lvalue. Otherwise, return X. */
2434 static tree
2435 pedantic_non_lvalue_loc (location_t loc, tree x)
2437 return protected_set_expr_location_unshare (x, loc);
2440 /* Given a tree comparison code, return the code that is the logical inverse.
2441 It is generally not safe to do this for floating-point comparisons, except
2442 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2443 ERROR_MARK in this case. */
2445 enum tree_code
2446 invert_tree_comparison (enum tree_code code, bool honor_nans)
2448 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2449 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2450 return ERROR_MARK;
2452 switch (code)
2454 case EQ_EXPR:
2455 return NE_EXPR;
2456 case NE_EXPR:
2457 return EQ_EXPR;
2458 case GT_EXPR:
2459 return honor_nans ? UNLE_EXPR : LE_EXPR;
2460 case GE_EXPR:
2461 return honor_nans ? UNLT_EXPR : LT_EXPR;
2462 case LT_EXPR:
2463 return honor_nans ? UNGE_EXPR : GE_EXPR;
2464 case LE_EXPR:
2465 return honor_nans ? UNGT_EXPR : GT_EXPR;
2466 case LTGT_EXPR:
2467 return UNEQ_EXPR;
2468 case UNEQ_EXPR:
2469 return LTGT_EXPR;
2470 case UNGT_EXPR:
2471 return LE_EXPR;
2472 case UNGE_EXPR:
2473 return LT_EXPR;
2474 case UNLT_EXPR:
2475 return GE_EXPR;
2476 case UNLE_EXPR:
2477 return GT_EXPR;
2478 case ORDERED_EXPR:
2479 return UNORDERED_EXPR;
2480 case UNORDERED_EXPR:
2481 return ORDERED_EXPR;
2482 default:
2483 gcc_unreachable ();
2487 /* Similar, but return the comparison that results if the operands are
2488 swapped. This is safe for floating-point. */
2490 enum tree_code
2491 swap_tree_comparison (enum tree_code code)
2493 switch (code)
2495 case EQ_EXPR:
2496 case NE_EXPR:
2497 case ORDERED_EXPR:
2498 case UNORDERED_EXPR:
2499 case LTGT_EXPR:
2500 case UNEQ_EXPR:
2501 return code;
2502 case GT_EXPR:
2503 return LT_EXPR;
2504 case GE_EXPR:
2505 return LE_EXPR;
2506 case LT_EXPR:
2507 return GT_EXPR;
2508 case LE_EXPR:
2509 return GE_EXPR;
2510 case UNGT_EXPR:
2511 return UNLT_EXPR;
2512 case UNGE_EXPR:
2513 return UNLE_EXPR;
2514 case UNLT_EXPR:
2515 return UNGT_EXPR;
2516 case UNLE_EXPR:
2517 return UNGE_EXPR;
2518 default:
2519 gcc_unreachable ();
2524 /* Convert a comparison tree code from an enum tree_code representation
2525 into a compcode bit-based encoding. This function is the inverse of
2526 compcode_to_comparison. */
2528 static enum comparison_code
2529 comparison_to_compcode (enum tree_code code)
2531 switch (code)
2533 case LT_EXPR:
2534 return COMPCODE_LT;
2535 case EQ_EXPR:
2536 return COMPCODE_EQ;
2537 case LE_EXPR:
2538 return COMPCODE_LE;
2539 case GT_EXPR:
2540 return COMPCODE_GT;
2541 case NE_EXPR:
2542 return COMPCODE_NE;
2543 case GE_EXPR:
2544 return COMPCODE_GE;
2545 case ORDERED_EXPR:
2546 return COMPCODE_ORD;
2547 case UNORDERED_EXPR:
2548 return COMPCODE_UNORD;
2549 case UNLT_EXPR:
2550 return COMPCODE_UNLT;
2551 case UNEQ_EXPR:
2552 return COMPCODE_UNEQ;
2553 case UNLE_EXPR:
2554 return COMPCODE_UNLE;
2555 case UNGT_EXPR:
2556 return COMPCODE_UNGT;
2557 case LTGT_EXPR:
2558 return COMPCODE_LTGT;
2559 case UNGE_EXPR:
2560 return COMPCODE_UNGE;
2561 default:
2562 gcc_unreachable ();
2566 /* Convert a compcode bit-based encoding of a comparison operator back
2567 to GCC's enum tree_code representation. This function is the
2568 inverse of comparison_to_compcode. */
2570 static enum tree_code
2571 compcode_to_comparison (enum comparison_code code)
2573 switch (code)
2575 case COMPCODE_LT:
2576 return LT_EXPR;
2577 case COMPCODE_EQ:
2578 return EQ_EXPR;
2579 case COMPCODE_LE:
2580 return LE_EXPR;
2581 case COMPCODE_GT:
2582 return GT_EXPR;
2583 case COMPCODE_NE:
2584 return NE_EXPR;
2585 case COMPCODE_GE:
2586 return GE_EXPR;
2587 case COMPCODE_ORD:
2588 return ORDERED_EXPR;
2589 case COMPCODE_UNORD:
2590 return UNORDERED_EXPR;
2591 case COMPCODE_UNLT:
2592 return UNLT_EXPR;
2593 case COMPCODE_UNEQ:
2594 return UNEQ_EXPR;
2595 case COMPCODE_UNLE:
2596 return UNLE_EXPR;
2597 case COMPCODE_UNGT:
2598 return UNGT_EXPR;
2599 case COMPCODE_LTGT:
2600 return LTGT_EXPR;
2601 case COMPCODE_UNGE:
2602 return UNGE_EXPR;
2603 default:
2604 gcc_unreachable ();
2608 /* Return a tree for the comparison which is the combination of
2609 doing the AND or OR (depending on CODE) of the two operations LCODE
2610 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2611 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2612 if this makes the transformation invalid. */
2614 tree
2615 combine_comparisons (location_t loc,
2616 enum tree_code code, enum tree_code lcode,
2617 enum tree_code rcode, tree truth_type,
2618 tree ll_arg, tree lr_arg)
2620 bool honor_nans = HONOR_NANS (ll_arg);
2621 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2622 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2623 int compcode;
2625 switch (code)
2627 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2628 compcode = lcompcode & rcompcode;
2629 break;
2631 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2632 compcode = lcompcode | rcompcode;
2633 break;
2635 default:
2636 return NULL_TREE;
2639 if (!honor_nans)
2641 /* Eliminate unordered comparisons, as well as LTGT and ORD
2642 which are not used unless the mode has NaNs. */
2643 compcode &= ~COMPCODE_UNORD;
2644 if (compcode == COMPCODE_LTGT)
2645 compcode = COMPCODE_NE;
2646 else if (compcode == COMPCODE_ORD)
2647 compcode = COMPCODE_TRUE;
2649 else if (flag_trapping_math)
2651 /* Check that the original operation and the optimized ones will trap
2652 under the same condition. */
2653 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2654 && (lcompcode != COMPCODE_EQ)
2655 && (lcompcode != COMPCODE_ORD);
2656 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2657 && (rcompcode != COMPCODE_EQ)
2658 && (rcompcode != COMPCODE_ORD);
2659 bool trap = (compcode & COMPCODE_UNORD) == 0
2660 && (compcode != COMPCODE_EQ)
2661 && (compcode != COMPCODE_ORD);
2663 /* In a short-circuited boolean expression the LHS might be
2664 such that the RHS, if evaluated, will never trap. For
2665 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2666 if neither x nor y is NaN. (This is a mixed blessing: for
2667 example, the expression above will never trap, hence
2668 optimizing it to x < y would be invalid). */
2669 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2670 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2671 rtrap = false;
2673 /* If the comparison was short-circuited, and only the RHS
2674 trapped, we may now generate a spurious trap. */
2675 if (rtrap && !ltrap
2676 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2677 return NULL_TREE;
2679 /* If we changed the conditions that cause a trap, we lose. */
2680 if ((ltrap || rtrap) != trap)
2681 return NULL_TREE;
2684 if (compcode == COMPCODE_TRUE)
2685 return constant_boolean_node (true, truth_type);
2686 else if (compcode == COMPCODE_FALSE)
2687 return constant_boolean_node (false, truth_type);
2688 else
2690 enum tree_code tcode;
2692 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2693 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2697 /* Return nonzero if two operands (typically of the same tree node)
2698 are necessarily equal. FLAGS modifies behavior as follows:
2700 If OEP_ONLY_CONST is set, only return nonzero for constants.
2701 This function tests whether the operands are indistinguishable;
2702 it does not test whether they are equal using C's == operation.
2703 The distinction is important for IEEE floating point, because
2704 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2705 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2707 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2708 even though it may hold multiple values during a function.
2709 This is because a GCC tree node guarantees that nothing else is
2710 executed between the evaluation of its "operands" (which may often
2711 be evaluated in arbitrary order). Hence if the operands themselves
2712 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2713 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2714 unset means assuming isochronic (or instantaneous) tree equivalence.
2715 Unless comparing arbitrary expression trees, such as from different
2716 statements, this flag can usually be left unset.
2718 If OEP_PURE_SAME is set, then pure functions with identical arguments
2719 are considered the same. It is used when the caller has other ways
2720 to ensure that global memory is unchanged in between.
2722 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2723 not values of expressions.
2725 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2726 any operand with side effect. This is unnecesarily conservative in the
2727 case we know that arg0 and arg1 are in disjoint code paths (such as in
2728 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2729 addresses with TREE_CONSTANT flag set so we know that &var == &var
2730 even if var is volatile. */
2733 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2735 /* When checking, verify at the outermost operand_equal_p call that
2736 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2737 hash value. */
2738 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2740 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2742 if (arg0 != arg1)
2744 inchash::hash hstate0 (0), hstate1 (0);
2745 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2746 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2747 hashval_t h0 = hstate0.end ();
2748 hashval_t h1 = hstate1.end ();
2749 gcc_assert (h0 == h1);
2751 return 1;
2753 else
2754 return 0;
2757 /* If either is ERROR_MARK, they aren't equal. */
2758 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2759 || TREE_TYPE (arg0) == error_mark_node
2760 || TREE_TYPE (arg1) == error_mark_node)
2761 return 0;
2763 /* Similar, if either does not have a type (like a released SSA name),
2764 they aren't equal. */
2765 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2766 return 0;
2768 /* We cannot consider pointers to different address space equal. */
2769 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2770 && POINTER_TYPE_P (TREE_TYPE (arg1))
2771 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2772 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2773 return 0;
2775 /* Check equality of integer constants before bailing out due to
2776 precision differences. */
2777 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2779 /* Address of INTEGER_CST is not defined; check that we did not forget
2780 to drop the OEP_ADDRESS_OF flags. */
2781 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2782 return tree_int_cst_equal (arg0, arg1);
2785 if (!(flags & OEP_ADDRESS_OF))
2787 /* If both types don't have the same signedness, then we can't consider
2788 them equal. We must check this before the STRIP_NOPS calls
2789 because they may change the signedness of the arguments. As pointers
2790 strictly don't have a signedness, require either two pointers or
2791 two non-pointers as well. */
2792 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2793 || POINTER_TYPE_P (TREE_TYPE (arg0))
2794 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2795 return 0;
2797 /* If both types don't have the same precision, then it is not safe
2798 to strip NOPs. */
2799 if (element_precision (TREE_TYPE (arg0))
2800 != element_precision (TREE_TYPE (arg1)))
2801 return 0;
2803 STRIP_NOPS (arg0);
2804 STRIP_NOPS (arg1);
2806 #if 0
2807 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2808 sanity check once the issue is solved. */
2809 else
2810 /* Addresses of conversions and SSA_NAMEs (and many other things)
2811 are not defined. Check that we did not forget to drop the
2812 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2813 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2814 && TREE_CODE (arg0) != SSA_NAME);
2815 #endif
2817 /* In case both args are comparisons but with different comparison
2818 code, try to swap the comparison operands of one arg to produce
2819 a match and compare that variant. */
2820 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2821 && COMPARISON_CLASS_P (arg0)
2822 && COMPARISON_CLASS_P (arg1))
2824 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2826 if (TREE_CODE (arg0) == swap_code)
2827 return operand_equal_p (TREE_OPERAND (arg0, 0),
2828 TREE_OPERAND (arg1, 1), flags)
2829 && operand_equal_p (TREE_OPERAND (arg0, 1),
2830 TREE_OPERAND (arg1, 0), flags);
2833 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2835 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2836 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2838 else if (flags & OEP_ADDRESS_OF)
2840 /* If we are interested in comparing addresses ignore
2841 MEM_REF wrappings of the base that can appear just for
2842 TBAA reasons. */
2843 if (TREE_CODE (arg0) == MEM_REF
2844 && DECL_P (arg1)
2845 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2846 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2847 && integer_zerop (TREE_OPERAND (arg0, 1)))
2848 return 1;
2849 else if (TREE_CODE (arg1) == MEM_REF
2850 && DECL_P (arg0)
2851 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2852 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2853 && integer_zerop (TREE_OPERAND (arg1, 1)))
2854 return 1;
2855 return 0;
2857 else
2858 return 0;
2861 /* When not checking adddresses, this is needed for conversions and for
2862 COMPONENT_REF. Might as well play it safe and always test this. */
2863 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2864 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2865 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2866 && !(flags & OEP_ADDRESS_OF)))
2867 return 0;
2869 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2870 We don't care about side effects in that case because the SAVE_EXPR
2871 takes care of that for us. In all other cases, two expressions are
2872 equal if they have no side effects. If we have two identical
2873 expressions with side effects that should be treated the same due
2874 to the only side effects being identical SAVE_EXPR's, that will
2875 be detected in the recursive calls below.
2876 If we are taking an invariant address of two identical objects
2877 they are necessarily equal as well. */
2878 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2879 && (TREE_CODE (arg0) == SAVE_EXPR
2880 || (flags & OEP_MATCH_SIDE_EFFECTS)
2881 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2882 return 1;
2884 /* Next handle constant cases, those for which we can return 1 even
2885 if ONLY_CONST is set. */
2886 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2887 switch (TREE_CODE (arg0))
2889 case INTEGER_CST:
2890 return tree_int_cst_equal (arg0, arg1);
2892 case FIXED_CST:
2893 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2894 TREE_FIXED_CST (arg1));
2896 case REAL_CST:
2897 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2898 return 1;
2901 if (!HONOR_SIGNED_ZEROS (arg0))
2903 /* If we do not distinguish between signed and unsigned zero,
2904 consider them equal. */
2905 if (real_zerop (arg0) && real_zerop (arg1))
2906 return 1;
2908 return 0;
2910 case VECTOR_CST:
2912 unsigned i;
2914 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2915 return 0;
2917 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2919 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2920 VECTOR_CST_ELT (arg1, i), flags))
2921 return 0;
2923 return 1;
2926 case COMPLEX_CST:
2927 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2928 flags)
2929 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2930 flags));
2932 case STRING_CST:
2933 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2934 && ! memcmp (TREE_STRING_POINTER (arg0),
2935 TREE_STRING_POINTER (arg1),
2936 TREE_STRING_LENGTH (arg0)));
2938 case ADDR_EXPR:
2939 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2940 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2941 flags | OEP_ADDRESS_OF
2942 | OEP_MATCH_SIDE_EFFECTS);
2943 case CONSTRUCTOR:
2944 /* In GIMPLE empty constructors are allowed in initializers of
2945 aggregates. */
2946 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2947 default:
2948 break;
2951 if (flags & OEP_ONLY_CONST)
2952 return 0;
2954 /* Define macros to test an operand from arg0 and arg1 for equality and a
2955 variant that allows null and views null as being different from any
2956 non-null value. In the latter case, if either is null, the both
2957 must be; otherwise, do the normal comparison. */
2958 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2959 TREE_OPERAND (arg1, N), flags)
2961 #define OP_SAME_WITH_NULL(N) \
2962 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2963 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2965 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2967 case tcc_unary:
2968 /* Two conversions are equal only if signedness and modes match. */
2969 switch (TREE_CODE (arg0))
2971 CASE_CONVERT:
2972 case FIX_TRUNC_EXPR:
2973 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2974 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2975 return 0;
2976 break;
2977 default:
2978 break;
2981 return OP_SAME (0);
2984 case tcc_comparison:
2985 case tcc_binary:
2986 if (OP_SAME (0) && OP_SAME (1))
2987 return 1;
2989 /* For commutative ops, allow the other order. */
2990 return (commutative_tree_code (TREE_CODE (arg0))
2991 && operand_equal_p (TREE_OPERAND (arg0, 0),
2992 TREE_OPERAND (arg1, 1), flags)
2993 && operand_equal_p (TREE_OPERAND (arg0, 1),
2994 TREE_OPERAND (arg1, 0), flags));
2996 case tcc_reference:
2997 /* If either of the pointer (or reference) expressions we are
2998 dereferencing contain a side effect, these cannot be equal,
2999 but their addresses can be. */
3000 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3001 && (TREE_SIDE_EFFECTS (arg0)
3002 || TREE_SIDE_EFFECTS (arg1)))
3003 return 0;
3005 switch (TREE_CODE (arg0))
3007 case INDIRECT_REF:
3008 if (!(flags & OEP_ADDRESS_OF)
3009 && (TYPE_ALIGN (TREE_TYPE (arg0))
3010 != TYPE_ALIGN (TREE_TYPE (arg1))))
3011 return 0;
3012 flags &= ~OEP_ADDRESS_OF;
3013 return OP_SAME (0);
3015 case IMAGPART_EXPR:
3016 /* Require the same offset. */
3017 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3018 TYPE_SIZE (TREE_TYPE (arg1)),
3019 flags & ~OEP_ADDRESS_OF))
3020 return 0;
3022 /* Fallthru. */
3023 case REALPART_EXPR:
3024 case VIEW_CONVERT_EXPR:
3025 return OP_SAME (0);
3027 case TARGET_MEM_REF:
3028 case MEM_REF:
3029 if (!(flags & OEP_ADDRESS_OF))
3031 /* Require equal access sizes */
3032 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3033 && (!TYPE_SIZE (TREE_TYPE (arg0))
3034 || !TYPE_SIZE (TREE_TYPE (arg1))
3035 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3036 TYPE_SIZE (TREE_TYPE (arg1)),
3037 flags)))
3038 return 0;
3039 /* Verify that access happens in similar types. */
3040 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3041 return 0;
3042 /* Verify that accesses are TBAA compatible. */
3043 if (!alias_ptr_types_compatible_p
3044 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3045 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3046 || (MR_DEPENDENCE_CLIQUE (arg0)
3047 != MR_DEPENDENCE_CLIQUE (arg1))
3048 || (MR_DEPENDENCE_BASE (arg0)
3049 != MR_DEPENDENCE_BASE (arg1)))
3050 return 0;
3051 /* Verify that alignment is compatible. */
3052 if (TYPE_ALIGN (TREE_TYPE (arg0))
3053 != TYPE_ALIGN (TREE_TYPE (arg1)))
3054 return 0;
3056 flags &= ~OEP_ADDRESS_OF;
3057 return (OP_SAME (0) && OP_SAME (1)
3058 /* TARGET_MEM_REF require equal extra operands. */
3059 && (TREE_CODE (arg0) != TARGET_MEM_REF
3060 || (OP_SAME_WITH_NULL (2)
3061 && OP_SAME_WITH_NULL (3)
3062 && OP_SAME_WITH_NULL (4))));
3064 case ARRAY_REF:
3065 case ARRAY_RANGE_REF:
3066 if (!OP_SAME (0))
3067 return 0;
3068 flags &= ~OEP_ADDRESS_OF;
3069 /* Compare the array index by value if it is constant first as we
3070 may have different types but same value here. */
3071 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3072 TREE_OPERAND (arg1, 1))
3073 || OP_SAME (1))
3074 && OP_SAME_WITH_NULL (2)
3075 && OP_SAME_WITH_NULL (3)
3076 /* Compare low bound and element size as with OEP_ADDRESS_OF
3077 we have to account for the offset of the ref. */
3078 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3079 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3080 || (operand_equal_p (array_ref_low_bound
3081 (CONST_CAST_TREE (arg0)),
3082 array_ref_low_bound
3083 (CONST_CAST_TREE (arg1)), flags)
3084 && operand_equal_p (array_ref_element_size
3085 (CONST_CAST_TREE (arg0)),
3086 array_ref_element_size
3087 (CONST_CAST_TREE (arg1)),
3088 flags))));
3090 case COMPONENT_REF:
3091 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3092 may be NULL when we're called to compare MEM_EXPRs. */
3093 if (!OP_SAME_WITH_NULL (0)
3094 || !OP_SAME (1))
3095 return 0;
3096 flags &= ~OEP_ADDRESS_OF;
3097 return OP_SAME_WITH_NULL (2);
3099 case BIT_FIELD_REF:
3100 if (!OP_SAME (0))
3101 return 0;
3102 flags &= ~OEP_ADDRESS_OF;
3103 return OP_SAME (1) && OP_SAME (2);
3105 default:
3106 return 0;
3109 case tcc_expression:
3110 switch (TREE_CODE (arg0))
3112 case ADDR_EXPR:
3113 /* Be sure we pass right ADDRESS_OF flag. */
3114 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3115 return operand_equal_p (TREE_OPERAND (arg0, 0),
3116 TREE_OPERAND (arg1, 0),
3117 flags | OEP_ADDRESS_OF);
3119 case TRUTH_NOT_EXPR:
3120 return OP_SAME (0);
3122 case TRUTH_ANDIF_EXPR:
3123 case TRUTH_ORIF_EXPR:
3124 return OP_SAME (0) && OP_SAME (1);
3126 case FMA_EXPR:
3127 case WIDEN_MULT_PLUS_EXPR:
3128 case WIDEN_MULT_MINUS_EXPR:
3129 if (!OP_SAME (2))
3130 return 0;
3131 /* The multiplcation operands are commutative. */
3132 /* FALLTHRU */
3134 case TRUTH_AND_EXPR:
3135 case TRUTH_OR_EXPR:
3136 case TRUTH_XOR_EXPR:
3137 if (OP_SAME (0) && OP_SAME (1))
3138 return 1;
3140 /* Otherwise take into account this is a commutative operation. */
3141 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3142 TREE_OPERAND (arg1, 1), flags)
3143 && operand_equal_p (TREE_OPERAND (arg0, 1),
3144 TREE_OPERAND (arg1, 0), flags));
3146 case COND_EXPR:
3147 if (! OP_SAME (1) || ! OP_SAME (2))
3148 return 0;
3149 flags &= ~OEP_ADDRESS_OF;
3150 return OP_SAME (0);
3152 case VEC_COND_EXPR:
3153 case DOT_PROD_EXPR:
3154 case BIT_INSERT_EXPR:
3155 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3157 default:
3158 return 0;
3161 case tcc_vl_exp:
3162 switch (TREE_CODE (arg0))
3164 case CALL_EXPR:
3165 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3166 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3167 /* If not both CALL_EXPRs are either internal or normal function
3168 functions, then they are not equal. */
3169 return 0;
3170 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3172 /* If the CALL_EXPRs call different internal functions, then they
3173 are not equal. */
3174 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3175 return 0;
3177 else
3179 /* If the CALL_EXPRs call different functions, then they are not
3180 equal. */
3181 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3182 flags))
3183 return 0;
3186 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3188 unsigned int cef = call_expr_flags (arg0);
3189 if (flags & OEP_PURE_SAME)
3190 cef &= ECF_CONST | ECF_PURE;
3191 else
3192 cef &= ECF_CONST;
3193 if (!cef)
3194 return 0;
3197 /* Now see if all the arguments are the same. */
3199 const_call_expr_arg_iterator iter0, iter1;
3200 const_tree a0, a1;
3201 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3202 a1 = first_const_call_expr_arg (arg1, &iter1);
3203 a0 && a1;
3204 a0 = next_const_call_expr_arg (&iter0),
3205 a1 = next_const_call_expr_arg (&iter1))
3206 if (! operand_equal_p (a0, a1, flags))
3207 return 0;
3209 /* If we get here and both argument lists are exhausted
3210 then the CALL_EXPRs are equal. */
3211 return ! (a0 || a1);
3213 default:
3214 return 0;
3217 case tcc_declaration:
3218 /* Consider __builtin_sqrt equal to sqrt. */
3219 return (TREE_CODE (arg0) == FUNCTION_DECL
3220 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3221 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3222 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3224 case tcc_exceptional:
3225 if (TREE_CODE (arg0) == CONSTRUCTOR)
3227 /* In GIMPLE constructors are used only to build vectors from
3228 elements. Individual elements in the constructor must be
3229 indexed in increasing order and form an initial sequence.
3231 We make no effort to compare constructors in generic.
3232 (see sem_variable::equals in ipa-icf which can do so for
3233 constants). */
3234 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3235 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3236 return 0;
3238 /* Be sure that vectors constructed have the same representation.
3239 We only tested element precision and modes to match.
3240 Vectors may be BLKmode and thus also check that the number of
3241 parts match. */
3242 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3243 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3244 return 0;
3246 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3247 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3248 unsigned int len = vec_safe_length (v0);
3250 if (len != vec_safe_length (v1))
3251 return 0;
3253 for (unsigned int i = 0; i < len; i++)
3255 constructor_elt *c0 = &(*v0)[i];
3256 constructor_elt *c1 = &(*v1)[i];
3258 if (!operand_equal_p (c0->value, c1->value, flags)
3259 /* In GIMPLE the indexes can be either NULL or matching i.
3260 Double check this so we won't get false
3261 positives for GENERIC. */
3262 || (c0->index
3263 && (TREE_CODE (c0->index) != INTEGER_CST
3264 || !compare_tree_int (c0->index, i)))
3265 || (c1->index
3266 && (TREE_CODE (c1->index) != INTEGER_CST
3267 || !compare_tree_int (c1->index, i))))
3268 return 0;
3270 return 1;
3272 return 0;
3274 default:
3275 return 0;
3278 #undef OP_SAME
3279 #undef OP_SAME_WITH_NULL
3282 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3283 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3285 When in doubt, return 0. */
3287 static int
3288 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3290 int unsignedp1, unsignedpo;
3291 tree primarg0, primarg1, primother;
3292 unsigned int correct_width;
3294 if (operand_equal_p (arg0, arg1, 0))
3295 return 1;
3297 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3298 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3299 return 0;
3301 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3302 and see if the inner values are the same. This removes any
3303 signedness comparison, which doesn't matter here. */
3304 primarg0 = arg0, primarg1 = arg1;
3305 STRIP_NOPS (primarg0);
3306 STRIP_NOPS (primarg1);
3307 if (operand_equal_p (primarg0, primarg1, 0))
3308 return 1;
3310 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3311 actual comparison operand, ARG0.
3313 First throw away any conversions to wider types
3314 already present in the operands. */
3316 primarg1 = get_narrower (arg1, &unsignedp1);
3317 primother = get_narrower (other, &unsignedpo);
3319 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3320 if (unsignedp1 == unsignedpo
3321 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3322 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3324 tree type = TREE_TYPE (arg0);
3326 /* Make sure shorter operand is extended the right way
3327 to match the longer operand. */
3328 primarg1 = fold_convert (signed_or_unsigned_type_for
3329 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3331 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3332 return 1;
3335 return 0;
3338 /* See if ARG is an expression that is either a comparison or is performing
3339 arithmetic on comparisons. The comparisons must only be comparing
3340 two different values, which will be stored in *CVAL1 and *CVAL2; if
3341 they are nonzero it means that some operands have already been found.
3342 No variables may be used anywhere else in the expression except in the
3343 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3344 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3346 If this is true, return 1. Otherwise, return zero. */
3348 static int
3349 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3351 enum tree_code code = TREE_CODE (arg);
3352 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3354 /* We can handle some of the tcc_expression cases here. */
3355 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3356 tclass = tcc_unary;
3357 else if (tclass == tcc_expression
3358 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3359 || code == COMPOUND_EXPR))
3360 tclass = tcc_binary;
3362 else if (tclass == tcc_expression && code == SAVE_EXPR
3363 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3365 /* If we've already found a CVAL1 or CVAL2, this expression is
3366 two complex to handle. */
3367 if (*cval1 || *cval2)
3368 return 0;
3370 tclass = tcc_unary;
3371 *save_p = 1;
3374 switch (tclass)
3376 case tcc_unary:
3377 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3379 case tcc_binary:
3380 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3381 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3382 cval1, cval2, save_p));
3384 case tcc_constant:
3385 return 1;
3387 case tcc_expression:
3388 if (code == COND_EXPR)
3389 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3390 cval1, cval2, save_p)
3391 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3392 cval1, cval2, save_p)
3393 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3394 cval1, cval2, save_p));
3395 return 0;
3397 case tcc_comparison:
3398 /* First see if we can handle the first operand, then the second. For
3399 the second operand, we know *CVAL1 can't be zero. It must be that
3400 one side of the comparison is each of the values; test for the
3401 case where this isn't true by failing if the two operands
3402 are the same. */
3404 if (operand_equal_p (TREE_OPERAND (arg, 0),
3405 TREE_OPERAND (arg, 1), 0))
3406 return 0;
3408 if (*cval1 == 0)
3409 *cval1 = TREE_OPERAND (arg, 0);
3410 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3412 else if (*cval2 == 0)
3413 *cval2 = TREE_OPERAND (arg, 0);
3414 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3416 else
3417 return 0;
3419 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3421 else if (*cval2 == 0)
3422 *cval2 = TREE_OPERAND (arg, 1);
3423 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3425 else
3426 return 0;
3428 return 1;
3430 default:
3431 return 0;
3435 /* ARG is a tree that is known to contain just arithmetic operations and
3436 comparisons. Evaluate the operations in the tree substituting NEW0 for
3437 any occurrence of OLD0 as an operand of a comparison and likewise for
3438 NEW1 and OLD1. */
3440 static tree
3441 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3442 tree old1, tree new1)
3444 tree type = TREE_TYPE (arg);
3445 enum tree_code code = TREE_CODE (arg);
3446 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3448 /* We can handle some of the tcc_expression cases here. */
3449 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3450 tclass = tcc_unary;
3451 else if (tclass == tcc_expression
3452 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3453 tclass = tcc_binary;
3455 switch (tclass)
3457 case tcc_unary:
3458 return fold_build1_loc (loc, code, type,
3459 eval_subst (loc, TREE_OPERAND (arg, 0),
3460 old0, new0, old1, new1));
3462 case tcc_binary:
3463 return fold_build2_loc (loc, code, type,
3464 eval_subst (loc, TREE_OPERAND (arg, 0),
3465 old0, new0, old1, new1),
3466 eval_subst (loc, TREE_OPERAND (arg, 1),
3467 old0, new0, old1, new1));
3469 case tcc_expression:
3470 switch (code)
3472 case SAVE_EXPR:
3473 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3474 old1, new1);
3476 case COMPOUND_EXPR:
3477 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3478 old1, new1);
3480 case COND_EXPR:
3481 return fold_build3_loc (loc, code, type,
3482 eval_subst (loc, TREE_OPERAND (arg, 0),
3483 old0, new0, old1, new1),
3484 eval_subst (loc, TREE_OPERAND (arg, 1),
3485 old0, new0, old1, new1),
3486 eval_subst (loc, TREE_OPERAND (arg, 2),
3487 old0, new0, old1, new1));
3488 default:
3489 break;
3491 /* Fall through - ??? */
3493 case tcc_comparison:
3495 tree arg0 = TREE_OPERAND (arg, 0);
3496 tree arg1 = TREE_OPERAND (arg, 1);
3498 /* We need to check both for exact equality and tree equality. The
3499 former will be true if the operand has a side-effect. In that
3500 case, we know the operand occurred exactly once. */
3502 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3503 arg0 = new0;
3504 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3505 arg0 = new1;
3507 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3508 arg1 = new0;
3509 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3510 arg1 = new1;
3512 return fold_build2_loc (loc, code, type, arg0, arg1);
3515 default:
3516 return arg;
3520 /* Return a tree for the case when the result of an expression is RESULT
3521 converted to TYPE and OMITTED was previously an operand of the expression
3522 but is now not needed (e.g., we folded OMITTED * 0).
3524 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3525 the conversion of RESULT to TYPE. */
3527 tree
3528 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3530 tree t = fold_convert_loc (loc, type, result);
3532 /* If the resulting operand is an empty statement, just return the omitted
3533 statement casted to void. */
3534 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3535 return build1_loc (loc, NOP_EXPR, void_type_node,
3536 fold_ignored_result (omitted));
3538 if (TREE_SIDE_EFFECTS (omitted))
3539 return build2_loc (loc, COMPOUND_EXPR, type,
3540 fold_ignored_result (omitted), t);
3542 return non_lvalue_loc (loc, t);
3545 /* Return a tree for the case when the result of an expression is RESULT
3546 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3547 of the expression but are now not needed.
3549 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3550 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3551 evaluated before OMITTED2. Otherwise, if neither has side effects,
3552 just do the conversion of RESULT to TYPE. */
3554 tree
3555 omit_two_operands_loc (location_t loc, tree type, tree result,
3556 tree omitted1, tree omitted2)
3558 tree t = fold_convert_loc (loc, type, result);
3560 if (TREE_SIDE_EFFECTS (omitted2))
3561 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3562 if (TREE_SIDE_EFFECTS (omitted1))
3563 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3565 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3569 /* Return a simplified tree node for the truth-negation of ARG. This
3570 never alters ARG itself. We assume that ARG is an operation that
3571 returns a truth value (0 or 1).
3573 FIXME: one would think we would fold the result, but it causes
3574 problems with the dominator optimizer. */
3576 static tree
3577 fold_truth_not_expr (location_t loc, tree arg)
3579 tree type = TREE_TYPE (arg);
3580 enum tree_code code = TREE_CODE (arg);
3581 location_t loc1, loc2;
3583 /* If this is a comparison, we can simply invert it, except for
3584 floating-point non-equality comparisons, in which case we just
3585 enclose a TRUTH_NOT_EXPR around what we have. */
3587 if (TREE_CODE_CLASS (code) == tcc_comparison)
3589 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3590 if (FLOAT_TYPE_P (op_type)
3591 && flag_trapping_math
3592 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3593 && code != NE_EXPR && code != EQ_EXPR)
3594 return NULL_TREE;
3596 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3597 if (code == ERROR_MARK)
3598 return NULL_TREE;
3600 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3601 TREE_OPERAND (arg, 1));
3602 if (TREE_NO_WARNING (arg))
3603 TREE_NO_WARNING (ret) = 1;
3604 return ret;
3607 switch (code)
3609 case INTEGER_CST:
3610 return constant_boolean_node (integer_zerop (arg), type);
3612 case TRUTH_AND_EXPR:
3613 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3614 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3615 return build2_loc (loc, TRUTH_OR_EXPR, type,
3616 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3617 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3619 case TRUTH_OR_EXPR:
3620 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3621 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3622 return build2_loc (loc, TRUTH_AND_EXPR, type,
3623 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3624 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3626 case TRUTH_XOR_EXPR:
3627 /* Here we can invert either operand. We invert the first operand
3628 unless the second operand is a TRUTH_NOT_EXPR in which case our
3629 result is the XOR of the first operand with the inside of the
3630 negation of the second operand. */
3632 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3633 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3634 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3635 else
3636 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3637 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3638 TREE_OPERAND (arg, 1));
3640 case TRUTH_ANDIF_EXPR:
3641 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3642 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3643 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3644 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3645 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3647 case TRUTH_ORIF_EXPR:
3648 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3649 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3650 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3651 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3652 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3654 case TRUTH_NOT_EXPR:
3655 return TREE_OPERAND (arg, 0);
3657 case COND_EXPR:
3659 tree arg1 = TREE_OPERAND (arg, 1);
3660 tree arg2 = TREE_OPERAND (arg, 2);
3662 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3663 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3665 /* A COND_EXPR may have a throw as one operand, which
3666 then has void type. Just leave void operands
3667 as they are. */
3668 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3669 VOID_TYPE_P (TREE_TYPE (arg1))
3670 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3671 VOID_TYPE_P (TREE_TYPE (arg2))
3672 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3675 case COMPOUND_EXPR:
3676 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3677 return build2_loc (loc, COMPOUND_EXPR, type,
3678 TREE_OPERAND (arg, 0),
3679 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3681 case NON_LVALUE_EXPR:
3682 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3683 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3685 CASE_CONVERT:
3686 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3687 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3689 /* fall through */
3691 case FLOAT_EXPR:
3692 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3693 return build1_loc (loc, TREE_CODE (arg), type,
3694 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3696 case BIT_AND_EXPR:
3697 if (!integer_onep (TREE_OPERAND (arg, 1)))
3698 return NULL_TREE;
3699 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3701 case SAVE_EXPR:
3702 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3704 case CLEANUP_POINT_EXPR:
3705 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3706 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3707 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3709 default:
3710 return NULL_TREE;
3714 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3715 assume that ARG is an operation that returns a truth value (0 or 1
3716 for scalars, 0 or -1 for vectors). Return the folded expression if
3717 folding is successful. Otherwise, return NULL_TREE. */
3719 static tree
3720 fold_invert_truthvalue (location_t loc, tree arg)
3722 tree type = TREE_TYPE (arg);
3723 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3724 ? BIT_NOT_EXPR
3725 : TRUTH_NOT_EXPR,
3726 type, arg);
3729 /* Return a simplified tree node for the truth-negation of ARG. This
3730 never alters ARG itself. We assume that ARG is an operation that
3731 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3733 tree
3734 invert_truthvalue_loc (location_t loc, tree arg)
3736 if (TREE_CODE (arg) == ERROR_MARK)
3737 return arg;
3739 tree type = TREE_TYPE (arg);
3740 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3741 ? BIT_NOT_EXPR
3742 : TRUTH_NOT_EXPR,
3743 type, arg);
3746 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3747 with code CODE. This optimization is unsafe. */
3748 static tree
3749 distribute_real_division (location_t loc, enum tree_code code, tree type,
3750 tree arg0, tree arg1)
3752 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3753 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3755 /* (A / C) +- (B / C) -> (A +- B) / C. */
3756 if (mul0 == mul1
3757 && operand_equal_p (TREE_OPERAND (arg0, 1),
3758 TREE_OPERAND (arg1, 1), 0))
3759 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3760 fold_build2_loc (loc, code, type,
3761 TREE_OPERAND (arg0, 0),
3762 TREE_OPERAND (arg1, 0)),
3763 TREE_OPERAND (arg0, 1));
3765 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3766 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3767 TREE_OPERAND (arg1, 0), 0)
3768 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3769 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3771 REAL_VALUE_TYPE r0, r1;
3772 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3773 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3774 if (!mul0)
3775 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3776 if (!mul1)
3777 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3778 real_arithmetic (&r0, code, &r0, &r1);
3779 return fold_build2_loc (loc, MULT_EXPR, type,
3780 TREE_OPERAND (arg0, 0),
3781 build_real (type, r0));
3784 return NULL_TREE;
3787 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3788 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3789 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3790 is the original memory reference used to preserve the alias set of
3791 the access. */
3793 static tree
3794 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3795 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3796 int unsignedp, int reversep)
3798 tree result, bftype;
3800 alias_set_type iset = get_alias_set (orig_inner);
3801 if (iset == 0 && get_alias_set (inner) != iset)
3802 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3803 build_fold_addr_expr (inner),
3804 build_int_cst (ptr_type_node, 0));
3806 if (bitpos == 0 && !reversep)
3808 tree size = TYPE_SIZE (TREE_TYPE (inner));
3809 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3810 || POINTER_TYPE_P (TREE_TYPE (inner)))
3811 && tree_fits_shwi_p (size)
3812 && tree_to_shwi (size) == bitsize)
3813 return fold_convert_loc (loc, type, inner);
3816 bftype = type;
3817 if (TYPE_PRECISION (bftype) != bitsize
3818 || TYPE_UNSIGNED (bftype) == !unsignedp)
3819 bftype = build_nonstandard_integer_type (bitsize, 0);
3821 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3822 size_int (bitsize), bitsize_int (bitpos));
3823 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3825 if (bftype != type)
3826 result = fold_convert_loc (loc, type, result);
3828 return result;
3831 /* Optimize a bit-field compare.
3833 There are two cases: First is a compare against a constant and the
3834 second is a comparison of two items where the fields are at the same
3835 bit position relative to the start of a chunk (byte, halfword, word)
3836 large enough to contain it. In these cases we can avoid the shift
3837 implicit in bitfield extractions.
3839 For constants, we emit a compare of the shifted constant with the
3840 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3841 compared. For two fields at the same position, we do the ANDs with the
3842 similar mask and compare the result of the ANDs.
3844 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3845 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3846 are the left and right operands of the comparison, respectively.
3848 If the optimization described above can be done, we return the resulting
3849 tree. Otherwise we return zero. */
3851 static tree
3852 optimize_bit_field_compare (location_t loc, enum tree_code code,
3853 tree compare_type, tree lhs, tree rhs)
3855 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3856 tree type = TREE_TYPE (lhs);
3857 tree unsigned_type;
3858 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3859 machine_mode lmode, rmode, nmode;
3860 int lunsignedp, runsignedp;
3861 int lreversep, rreversep;
3862 int lvolatilep = 0, rvolatilep = 0;
3863 tree linner, rinner = NULL_TREE;
3864 tree mask;
3865 tree offset;
3867 /* Get all the information about the extractions being done. If the bit size
3868 if the same as the size of the underlying object, we aren't doing an
3869 extraction at all and so can do nothing. We also don't want to
3870 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3871 then will no longer be able to replace it. */
3872 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3873 &lunsignedp, &lreversep, &lvolatilep);
3874 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3875 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3876 return 0;
3878 if (const_p)
3879 rreversep = lreversep;
3880 else
3882 /* If this is not a constant, we can only do something if bit positions,
3883 sizes, signedness and storage order are the same. */
3884 rinner
3885 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3886 &runsignedp, &rreversep, &rvolatilep);
3888 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3889 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3890 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3891 return 0;
3894 /* Honor the C++ memory model and mimic what RTL expansion does. */
3895 unsigned HOST_WIDE_INT bitstart = 0;
3896 unsigned HOST_WIDE_INT bitend = 0;
3897 if (TREE_CODE (lhs) == COMPONENT_REF)
3899 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3900 if (offset != NULL_TREE)
3901 return 0;
3904 /* See if we can find a mode to refer to this field. We should be able to,
3905 but fail if we can't. */
3906 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3907 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3908 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3909 TYPE_ALIGN (TREE_TYPE (rinner))),
3910 word_mode, false);
3911 if (nmode == VOIDmode)
3912 return 0;
3914 /* Set signed and unsigned types of the precision of this mode for the
3915 shifts below. */
3916 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3918 /* Compute the bit position and size for the new reference and our offset
3919 within it. If the new reference is the same size as the original, we
3920 won't optimize anything, so return zero. */
3921 nbitsize = GET_MODE_BITSIZE (nmode);
3922 nbitpos = lbitpos & ~ (nbitsize - 1);
3923 lbitpos -= nbitpos;
3924 if (nbitsize == lbitsize)
3925 return 0;
3927 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3928 lbitpos = nbitsize - lbitsize - lbitpos;
3930 /* Make the mask to be used against the extracted field. */
3931 mask = build_int_cst_type (unsigned_type, -1);
3932 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3933 mask = const_binop (RSHIFT_EXPR, mask,
3934 size_int (nbitsize - lbitsize - lbitpos));
3936 if (! const_p)
3937 /* If not comparing with constant, just rework the comparison
3938 and return. */
3939 return fold_build2_loc (loc, code, compare_type,
3940 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3941 make_bit_field_ref (loc, linner, lhs,
3942 unsigned_type,
3943 nbitsize, nbitpos,
3944 1, lreversep),
3945 mask),
3946 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3947 make_bit_field_ref (loc, rinner, rhs,
3948 unsigned_type,
3949 nbitsize, nbitpos,
3950 1, rreversep),
3951 mask));
3953 /* Otherwise, we are handling the constant case. See if the constant is too
3954 big for the field. Warn and return a tree for 0 (false) if so. We do
3955 this not only for its own sake, but to avoid having to test for this
3956 error case below. If we didn't, we might generate wrong code.
3958 For unsigned fields, the constant shifted right by the field length should
3959 be all zero. For signed fields, the high-order bits should agree with
3960 the sign bit. */
3962 if (lunsignedp)
3964 if (wi::lrshift (rhs, lbitsize) != 0)
3966 warning (0, "comparison is always %d due to width of bit-field",
3967 code == NE_EXPR);
3968 return constant_boolean_node (code == NE_EXPR, compare_type);
3971 else
3973 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3974 if (tem != 0 && tem != -1)
3976 warning (0, "comparison is always %d due to width of bit-field",
3977 code == NE_EXPR);
3978 return constant_boolean_node (code == NE_EXPR, compare_type);
3982 /* Single-bit compares should always be against zero. */
3983 if (lbitsize == 1 && ! integer_zerop (rhs))
3985 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3986 rhs = build_int_cst (type, 0);
3989 /* Make a new bitfield reference, shift the constant over the
3990 appropriate number of bits and mask it with the computed mask
3991 (in case this was a signed field). If we changed it, make a new one. */
3992 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
3993 nbitsize, nbitpos, 1, lreversep);
3995 rhs = const_binop (BIT_AND_EXPR,
3996 const_binop (LSHIFT_EXPR,
3997 fold_convert_loc (loc, unsigned_type, rhs),
3998 size_int (lbitpos)),
3999 mask);
4001 lhs = build2_loc (loc, code, compare_type,
4002 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4003 return lhs;
4006 /* Subroutine for fold_truth_andor_1: decode a field reference.
4008 If EXP is a comparison reference, we return the innermost reference.
4010 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4011 set to the starting bit number.
4013 If the innermost field can be completely contained in a mode-sized
4014 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4016 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4017 otherwise it is not changed.
4019 *PUNSIGNEDP is set to the signedness of the field.
4021 *PREVERSEP is set to the storage order of the field.
4023 *PMASK is set to the mask used. This is either contained in a
4024 BIT_AND_EXPR or derived from the width of the field.
4026 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4028 Return 0 if this is not a component reference or is one that we can't
4029 do anything with. */
4031 static tree
4032 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4033 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4034 int *punsignedp, int *preversep, int *pvolatilep,
4035 tree *pmask, tree *pand_mask)
4037 tree exp = *exp_;
4038 tree outer_type = 0;
4039 tree and_mask = 0;
4040 tree mask, inner, offset;
4041 tree unsigned_type;
4042 unsigned int precision;
4044 /* All the optimizations using this function assume integer fields.
4045 There are problems with FP fields since the type_for_size call
4046 below can fail for, e.g., XFmode. */
4047 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4048 return 0;
4050 /* We are interested in the bare arrangement of bits, so strip everything
4051 that doesn't affect the machine mode. However, record the type of the
4052 outermost expression if it may matter below. */
4053 if (CONVERT_EXPR_P (exp)
4054 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4055 outer_type = TREE_TYPE (exp);
4056 STRIP_NOPS (exp);
4058 if (TREE_CODE (exp) == BIT_AND_EXPR)
4060 and_mask = TREE_OPERAND (exp, 1);
4061 exp = TREE_OPERAND (exp, 0);
4062 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4063 if (TREE_CODE (and_mask) != INTEGER_CST)
4064 return 0;
4067 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4068 punsignedp, preversep, pvolatilep);
4069 if ((inner == exp && and_mask == 0)
4070 || *pbitsize < 0 || offset != 0
4071 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4072 return 0;
4074 *exp_ = exp;
4076 /* If the number of bits in the reference is the same as the bitsize of
4077 the outer type, then the outer type gives the signedness. Otherwise
4078 (in case of a small bitfield) the signedness is unchanged. */
4079 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4080 *punsignedp = TYPE_UNSIGNED (outer_type);
4082 /* Compute the mask to access the bitfield. */
4083 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4084 precision = TYPE_PRECISION (unsigned_type);
4086 mask = build_int_cst_type (unsigned_type, -1);
4088 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4089 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4091 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4092 if (and_mask != 0)
4093 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4094 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4096 *pmask = mask;
4097 *pand_mask = and_mask;
4098 return inner;
4101 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4102 bit positions and MASK is SIGNED. */
4104 static int
4105 all_ones_mask_p (const_tree mask, unsigned int size)
4107 tree type = TREE_TYPE (mask);
4108 unsigned int precision = TYPE_PRECISION (type);
4110 /* If this function returns true when the type of the mask is
4111 UNSIGNED, then there will be errors. In particular see
4112 gcc.c-torture/execute/990326-1.c. There does not appear to be
4113 any documentation paper trail as to why this is so. But the pre
4114 wide-int worked with that restriction and it has been preserved
4115 here. */
4116 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4117 return false;
4119 return wi::mask (size, false, precision) == mask;
4122 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4123 represents the sign bit of EXP's type. If EXP represents a sign
4124 or zero extension, also test VAL against the unextended type.
4125 The return value is the (sub)expression whose sign bit is VAL,
4126 or NULL_TREE otherwise. */
4128 tree
4129 sign_bit_p (tree exp, const_tree val)
4131 int width;
4132 tree t;
4134 /* Tree EXP must have an integral type. */
4135 t = TREE_TYPE (exp);
4136 if (! INTEGRAL_TYPE_P (t))
4137 return NULL_TREE;
4139 /* Tree VAL must be an integer constant. */
4140 if (TREE_CODE (val) != INTEGER_CST
4141 || TREE_OVERFLOW (val))
4142 return NULL_TREE;
4144 width = TYPE_PRECISION (t);
4145 if (wi::only_sign_bit_p (val, width))
4146 return exp;
4148 /* Handle extension from a narrower type. */
4149 if (TREE_CODE (exp) == NOP_EXPR
4150 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4151 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4153 return NULL_TREE;
4156 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4157 to be evaluated unconditionally. */
4159 static int
4160 simple_operand_p (const_tree exp)
4162 /* Strip any conversions that don't change the machine mode. */
4163 STRIP_NOPS (exp);
4165 return (CONSTANT_CLASS_P (exp)
4166 || TREE_CODE (exp) == SSA_NAME
4167 || (DECL_P (exp)
4168 && ! TREE_ADDRESSABLE (exp)
4169 && ! TREE_THIS_VOLATILE (exp)
4170 && ! DECL_NONLOCAL (exp)
4171 /* Don't regard global variables as simple. They may be
4172 allocated in ways unknown to the compiler (shared memory,
4173 #pragma weak, etc). */
4174 && ! TREE_PUBLIC (exp)
4175 && ! DECL_EXTERNAL (exp)
4176 /* Weakrefs are not safe to be read, since they can be NULL.
4177 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4178 have DECL_WEAK flag set. */
4179 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4180 /* Loading a static variable is unduly expensive, but global
4181 registers aren't expensive. */
4182 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4185 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4186 to be evaluated unconditionally.
4187 I addition to simple_operand_p, we assume that comparisons, conversions,
4188 and logic-not operations are simple, if their operands are simple, too. */
4190 static bool
4191 simple_operand_p_2 (tree exp)
4193 enum tree_code code;
4195 if (TREE_SIDE_EFFECTS (exp)
4196 || tree_could_trap_p (exp))
4197 return false;
4199 while (CONVERT_EXPR_P (exp))
4200 exp = TREE_OPERAND (exp, 0);
4202 code = TREE_CODE (exp);
4204 if (TREE_CODE_CLASS (code) == tcc_comparison)
4205 return (simple_operand_p (TREE_OPERAND (exp, 0))
4206 && simple_operand_p (TREE_OPERAND (exp, 1)));
4208 if (code == TRUTH_NOT_EXPR)
4209 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4211 return simple_operand_p (exp);
4215 /* The following functions are subroutines to fold_range_test and allow it to
4216 try to change a logical combination of comparisons into a range test.
4218 For example, both
4219 X == 2 || X == 3 || X == 4 || X == 5
4221 X >= 2 && X <= 5
4222 are converted to
4223 (unsigned) (X - 2) <= 3
4225 We describe each set of comparisons as being either inside or outside
4226 a range, using a variable named like IN_P, and then describe the
4227 range with a lower and upper bound. If one of the bounds is omitted,
4228 it represents either the highest or lowest value of the type.
4230 In the comments below, we represent a range by two numbers in brackets
4231 preceded by a "+" to designate being inside that range, or a "-" to
4232 designate being outside that range, so the condition can be inverted by
4233 flipping the prefix. An omitted bound is represented by a "-". For
4234 example, "- [-, 10]" means being outside the range starting at the lowest
4235 possible value and ending at 10, in other words, being greater than 10.
4236 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4237 always false.
4239 We set up things so that the missing bounds are handled in a consistent
4240 manner so neither a missing bound nor "true" and "false" need to be
4241 handled using a special case. */
4243 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4244 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4245 and UPPER1_P are nonzero if the respective argument is an upper bound
4246 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4247 must be specified for a comparison. ARG1 will be converted to ARG0's
4248 type if both are specified. */
4250 static tree
4251 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4252 tree arg1, int upper1_p)
4254 tree tem;
4255 int result;
4256 int sgn0, sgn1;
4258 /* If neither arg represents infinity, do the normal operation.
4259 Else, if not a comparison, return infinity. Else handle the special
4260 comparison rules. Note that most of the cases below won't occur, but
4261 are handled for consistency. */
4263 if (arg0 != 0 && arg1 != 0)
4265 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4266 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4267 STRIP_NOPS (tem);
4268 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4271 if (TREE_CODE_CLASS (code) != tcc_comparison)
4272 return 0;
4274 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4275 for neither. In real maths, we cannot assume open ended ranges are
4276 the same. But, this is computer arithmetic, where numbers are finite.
4277 We can therefore make the transformation of any unbounded range with
4278 the value Z, Z being greater than any representable number. This permits
4279 us to treat unbounded ranges as equal. */
4280 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4281 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4282 switch (code)
4284 case EQ_EXPR:
4285 result = sgn0 == sgn1;
4286 break;
4287 case NE_EXPR:
4288 result = sgn0 != sgn1;
4289 break;
4290 case LT_EXPR:
4291 result = sgn0 < sgn1;
4292 break;
4293 case LE_EXPR:
4294 result = sgn0 <= sgn1;
4295 break;
4296 case GT_EXPR:
4297 result = sgn0 > sgn1;
4298 break;
4299 case GE_EXPR:
4300 result = sgn0 >= sgn1;
4301 break;
4302 default:
4303 gcc_unreachable ();
4306 return constant_boolean_node (result, type);
4309 /* Helper routine for make_range. Perform one step for it, return
4310 new expression if the loop should continue or NULL_TREE if it should
4311 stop. */
4313 tree
4314 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4315 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4316 bool *strict_overflow_p)
4318 tree arg0_type = TREE_TYPE (arg0);
4319 tree n_low, n_high, low = *p_low, high = *p_high;
4320 int in_p = *p_in_p, n_in_p;
4322 switch (code)
4324 case TRUTH_NOT_EXPR:
4325 /* We can only do something if the range is testing for zero. */
4326 if (low == NULL_TREE || high == NULL_TREE
4327 || ! integer_zerop (low) || ! integer_zerop (high))
4328 return NULL_TREE;
4329 *p_in_p = ! in_p;
4330 return arg0;
4332 case EQ_EXPR: case NE_EXPR:
4333 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4334 /* We can only do something if the range is testing for zero
4335 and if the second operand is an integer constant. Note that
4336 saying something is "in" the range we make is done by
4337 complementing IN_P since it will set in the initial case of
4338 being not equal to zero; "out" is leaving it alone. */
4339 if (low == NULL_TREE || high == NULL_TREE
4340 || ! integer_zerop (low) || ! integer_zerop (high)
4341 || TREE_CODE (arg1) != INTEGER_CST)
4342 return NULL_TREE;
4344 switch (code)
4346 case NE_EXPR: /* - [c, c] */
4347 low = high = arg1;
4348 break;
4349 case EQ_EXPR: /* + [c, c] */
4350 in_p = ! in_p, low = high = arg1;
4351 break;
4352 case GT_EXPR: /* - [-, c] */
4353 low = 0, high = arg1;
4354 break;
4355 case GE_EXPR: /* + [c, -] */
4356 in_p = ! in_p, low = arg1, high = 0;
4357 break;
4358 case LT_EXPR: /* - [c, -] */
4359 low = arg1, high = 0;
4360 break;
4361 case LE_EXPR: /* + [-, c] */
4362 in_p = ! in_p, low = 0, high = arg1;
4363 break;
4364 default:
4365 gcc_unreachable ();
4368 /* If this is an unsigned comparison, we also know that EXP is
4369 greater than or equal to zero. We base the range tests we make
4370 on that fact, so we record it here so we can parse existing
4371 range tests. We test arg0_type since often the return type
4372 of, e.g. EQ_EXPR, is boolean. */
4373 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4375 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4376 in_p, low, high, 1,
4377 build_int_cst (arg0_type, 0),
4378 NULL_TREE))
4379 return NULL_TREE;
4381 in_p = n_in_p, low = n_low, high = n_high;
4383 /* If the high bound is missing, but we have a nonzero low
4384 bound, reverse the range so it goes from zero to the low bound
4385 minus 1. */
4386 if (high == 0 && low && ! integer_zerop (low))
4388 in_p = ! in_p;
4389 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4390 build_int_cst (TREE_TYPE (low), 1), 0);
4391 low = build_int_cst (arg0_type, 0);
4395 *p_low = low;
4396 *p_high = high;
4397 *p_in_p = in_p;
4398 return arg0;
4400 case NEGATE_EXPR:
4401 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4402 low and high are non-NULL, then normalize will DTRT. */
4403 if (!TYPE_UNSIGNED (arg0_type)
4404 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4406 if (low == NULL_TREE)
4407 low = TYPE_MIN_VALUE (arg0_type);
4408 if (high == NULL_TREE)
4409 high = TYPE_MAX_VALUE (arg0_type);
4412 /* (-x) IN [a,b] -> x in [-b, -a] */
4413 n_low = range_binop (MINUS_EXPR, exp_type,
4414 build_int_cst (exp_type, 0),
4415 0, high, 1);
4416 n_high = range_binop (MINUS_EXPR, exp_type,
4417 build_int_cst (exp_type, 0),
4418 0, low, 0);
4419 if (n_high != 0 && TREE_OVERFLOW (n_high))
4420 return NULL_TREE;
4421 goto normalize;
4423 case BIT_NOT_EXPR:
4424 /* ~ X -> -X - 1 */
4425 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4426 build_int_cst (exp_type, 1));
4428 case PLUS_EXPR:
4429 case MINUS_EXPR:
4430 if (TREE_CODE (arg1) != INTEGER_CST)
4431 return NULL_TREE;
4433 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4434 move a constant to the other side. */
4435 if (!TYPE_UNSIGNED (arg0_type)
4436 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4437 return NULL_TREE;
4439 /* If EXP is signed, any overflow in the computation is undefined,
4440 so we don't worry about it so long as our computations on
4441 the bounds don't overflow. For unsigned, overflow is defined
4442 and this is exactly the right thing. */
4443 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4444 arg0_type, low, 0, arg1, 0);
4445 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4446 arg0_type, high, 1, arg1, 0);
4447 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4448 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4449 return NULL_TREE;
4451 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4452 *strict_overflow_p = true;
4454 normalize:
4455 /* Check for an unsigned range which has wrapped around the maximum
4456 value thus making n_high < n_low, and normalize it. */
4457 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4459 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4460 build_int_cst (TREE_TYPE (n_high), 1), 0);
4461 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4462 build_int_cst (TREE_TYPE (n_low), 1), 0);
4464 /* If the range is of the form +/- [ x+1, x ], we won't
4465 be able to normalize it. But then, it represents the
4466 whole range or the empty set, so make it
4467 +/- [ -, - ]. */
4468 if (tree_int_cst_equal (n_low, low)
4469 && tree_int_cst_equal (n_high, high))
4470 low = high = 0;
4471 else
4472 in_p = ! in_p;
4474 else
4475 low = n_low, high = n_high;
4477 *p_low = low;
4478 *p_high = high;
4479 *p_in_p = in_p;
4480 return arg0;
4482 CASE_CONVERT:
4483 case NON_LVALUE_EXPR:
4484 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4485 return NULL_TREE;
4487 if (! INTEGRAL_TYPE_P (arg0_type)
4488 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4489 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4490 return NULL_TREE;
4492 n_low = low, n_high = high;
4494 if (n_low != 0)
4495 n_low = fold_convert_loc (loc, arg0_type, n_low);
4497 if (n_high != 0)
4498 n_high = fold_convert_loc (loc, arg0_type, n_high);
4500 /* If we're converting arg0 from an unsigned type, to exp,
4501 a signed type, we will be doing the comparison as unsigned.
4502 The tests above have already verified that LOW and HIGH
4503 are both positive.
4505 So we have to ensure that we will handle large unsigned
4506 values the same way that the current signed bounds treat
4507 negative values. */
4509 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4511 tree high_positive;
4512 tree equiv_type;
4513 /* For fixed-point modes, we need to pass the saturating flag
4514 as the 2nd parameter. */
4515 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4516 equiv_type
4517 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4518 TYPE_SATURATING (arg0_type));
4519 else
4520 equiv_type
4521 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4523 /* A range without an upper bound is, naturally, unbounded.
4524 Since convert would have cropped a very large value, use
4525 the max value for the destination type. */
4526 high_positive
4527 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4528 : TYPE_MAX_VALUE (arg0_type);
4530 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4531 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4532 fold_convert_loc (loc, arg0_type,
4533 high_positive),
4534 build_int_cst (arg0_type, 1));
4536 /* If the low bound is specified, "and" the range with the
4537 range for which the original unsigned value will be
4538 positive. */
4539 if (low != 0)
4541 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4542 1, fold_convert_loc (loc, arg0_type,
4543 integer_zero_node),
4544 high_positive))
4545 return NULL_TREE;
4547 in_p = (n_in_p == in_p);
4549 else
4551 /* Otherwise, "or" the range with the range of the input
4552 that will be interpreted as negative. */
4553 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4554 1, fold_convert_loc (loc, arg0_type,
4555 integer_zero_node),
4556 high_positive))
4557 return NULL_TREE;
4559 in_p = (in_p != n_in_p);
4563 *p_low = n_low;
4564 *p_high = n_high;
4565 *p_in_p = in_p;
4566 return arg0;
4568 default:
4569 return NULL_TREE;
4573 /* Given EXP, a logical expression, set the range it is testing into
4574 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4575 actually being tested. *PLOW and *PHIGH will be made of the same
4576 type as the returned expression. If EXP is not a comparison, we
4577 will most likely not be returning a useful value and range. Set
4578 *STRICT_OVERFLOW_P to true if the return value is only valid
4579 because signed overflow is undefined; otherwise, do not change
4580 *STRICT_OVERFLOW_P. */
4582 tree
4583 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4584 bool *strict_overflow_p)
4586 enum tree_code code;
4587 tree arg0, arg1 = NULL_TREE;
4588 tree exp_type, nexp;
4589 int in_p;
4590 tree low, high;
4591 location_t loc = EXPR_LOCATION (exp);
4593 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4594 and see if we can refine the range. Some of the cases below may not
4595 happen, but it doesn't seem worth worrying about this. We "continue"
4596 the outer loop when we've changed something; otherwise we "break"
4597 the switch, which will "break" the while. */
4599 in_p = 0;
4600 low = high = build_int_cst (TREE_TYPE (exp), 0);
4602 while (1)
4604 code = TREE_CODE (exp);
4605 exp_type = TREE_TYPE (exp);
4606 arg0 = NULL_TREE;
4608 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4610 if (TREE_OPERAND_LENGTH (exp) > 0)
4611 arg0 = TREE_OPERAND (exp, 0);
4612 if (TREE_CODE_CLASS (code) == tcc_binary
4613 || TREE_CODE_CLASS (code) == tcc_comparison
4614 || (TREE_CODE_CLASS (code) == tcc_expression
4615 && TREE_OPERAND_LENGTH (exp) > 1))
4616 arg1 = TREE_OPERAND (exp, 1);
4618 if (arg0 == NULL_TREE)
4619 break;
4621 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4622 &high, &in_p, strict_overflow_p);
4623 if (nexp == NULL_TREE)
4624 break;
4625 exp = nexp;
4628 /* If EXP is a constant, we can evaluate whether this is true or false. */
4629 if (TREE_CODE (exp) == INTEGER_CST)
4631 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4632 exp, 0, low, 0))
4633 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4634 exp, 1, high, 1)));
4635 low = high = 0;
4636 exp = 0;
4639 *pin_p = in_p, *plow = low, *phigh = high;
4640 return exp;
4643 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4644 type, TYPE, return an expression to test if EXP is in (or out of, depending
4645 on IN_P) the range. Return 0 if the test couldn't be created. */
4647 tree
4648 build_range_check (location_t loc, tree type, tree exp, int in_p,
4649 tree low, tree high)
4651 tree etype = TREE_TYPE (exp), value;
4653 /* Disable this optimization for function pointer expressions
4654 on targets that require function pointer canonicalization. */
4655 if (targetm.have_canonicalize_funcptr_for_compare ()
4656 && TREE_CODE (etype) == POINTER_TYPE
4657 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4658 return NULL_TREE;
4660 if (! in_p)
4662 value = build_range_check (loc, type, exp, 1, low, high);
4663 if (value != 0)
4664 return invert_truthvalue_loc (loc, value);
4666 return 0;
4669 if (low == 0 && high == 0)
4670 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4672 if (low == 0)
4673 return fold_build2_loc (loc, LE_EXPR, type, exp,
4674 fold_convert_loc (loc, etype, high));
4676 if (high == 0)
4677 return fold_build2_loc (loc, GE_EXPR, type, exp,
4678 fold_convert_loc (loc, etype, low));
4680 if (operand_equal_p (low, high, 0))
4681 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4682 fold_convert_loc (loc, etype, low));
4684 if (integer_zerop (low))
4686 if (! TYPE_UNSIGNED (etype))
4688 etype = unsigned_type_for (etype);
4689 high = fold_convert_loc (loc, etype, high);
4690 exp = fold_convert_loc (loc, etype, exp);
4692 return build_range_check (loc, type, exp, 1, 0, high);
4695 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4696 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4698 int prec = TYPE_PRECISION (etype);
4700 if (wi::mask (prec - 1, false, prec) == high)
4702 if (TYPE_UNSIGNED (etype))
4704 tree signed_etype = signed_type_for (etype);
4705 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4706 etype
4707 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4708 else
4709 etype = signed_etype;
4710 exp = fold_convert_loc (loc, etype, exp);
4712 return fold_build2_loc (loc, GT_EXPR, type, exp,
4713 build_int_cst (etype, 0));
4717 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4718 This requires wrap-around arithmetics for the type of the expression.
4719 First make sure that arithmetics in this type is valid, then make sure
4720 that it wraps around. */
4721 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4722 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4723 TYPE_UNSIGNED (etype));
4725 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4727 tree utype, minv, maxv;
4729 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4730 for the type in question, as we rely on this here. */
4731 utype = unsigned_type_for (etype);
4732 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4733 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4734 build_int_cst (TREE_TYPE (maxv), 1), 1);
4735 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4737 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4738 minv, 1, maxv, 1)))
4739 etype = utype;
4740 else
4741 return 0;
4744 high = fold_convert_loc (loc, etype, high);
4745 low = fold_convert_loc (loc, etype, low);
4746 exp = fold_convert_loc (loc, etype, exp);
4748 value = const_binop (MINUS_EXPR, high, low);
4751 if (POINTER_TYPE_P (etype))
4753 if (value != 0 && !TREE_OVERFLOW (value))
4755 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4756 return build_range_check (loc, type,
4757 fold_build_pointer_plus_loc (loc, exp, low),
4758 1, build_int_cst (etype, 0), value);
4760 return 0;
4763 if (value != 0 && !TREE_OVERFLOW (value))
4764 return build_range_check (loc, type,
4765 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4766 1, build_int_cst (etype, 0), value);
4768 return 0;
4771 /* Return the predecessor of VAL in its type, handling the infinite case. */
4773 static tree
4774 range_predecessor (tree val)
4776 tree type = TREE_TYPE (val);
4778 if (INTEGRAL_TYPE_P (type)
4779 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4780 return 0;
4781 else
4782 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4783 build_int_cst (TREE_TYPE (val), 1), 0);
4786 /* Return the successor of VAL in its type, handling the infinite case. */
4788 static tree
4789 range_successor (tree val)
4791 tree type = TREE_TYPE (val);
4793 if (INTEGRAL_TYPE_P (type)
4794 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4795 return 0;
4796 else
4797 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4798 build_int_cst (TREE_TYPE (val), 1), 0);
4801 /* Given two ranges, see if we can merge them into one. Return 1 if we
4802 can, 0 if we can't. Set the output range into the specified parameters. */
4804 bool
4805 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4806 tree high0, int in1_p, tree low1, tree high1)
4808 int no_overlap;
4809 int subset;
4810 int temp;
4811 tree tem;
4812 int in_p;
4813 tree low, high;
4814 int lowequal = ((low0 == 0 && low1 == 0)
4815 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4816 low0, 0, low1, 0)));
4817 int highequal = ((high0 == 0 && high1 == 0)
4818 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4819 high0, 1, high1, 1)));
4821 /* Make range 0 be the range that starts first, or ends last if they
4822 start at the same value. Swap them if it isn't. */
4823 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4824 low0, 0, low1, 0))
4825 || (lowequal
4826 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4827 high1, 1, high0, 1))))
4829 temp = in0_p, in0_p = in1_p, in1_p = temp;
4830 tem = low0, low0 = low1, low1 = tem;
4831 tem = high0, high0 = high1, high1 = tem;
4834 /* Now flag two cases, whether the ranges are disjoint or whether the
4835 second range is totally subsumed in the first. Note that the tests
4836 below are simplified by the ones above. */
4837 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4838 high0, 1, low1, 0));
4839 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4840 high1, 1, high0, 1));
4842 /* We now have four cases, depending on whether we are including or
4843 excluding the two ranges. */
4844 if (in0_p && in1_p)
4846 /* If they don't overlap, the result is false. If the second range
4847 is a subset it is the result. Otherwise, the range is from the start
4848 of the second to the end of the first. */
4849 if (no_overlap)
4850 in_p = 0, low = high = 0;
4851 else if (subset)
4852 in_p = 1, low = low1, high = high1;
4853 else
4854 in_p = 1, low = low1, high = high0;
4857 else if (in0_p && ! in1_p)
4859 /* If they don't overlap, the result is the first range. If they are
4860 equal, the result is false. If the second range is a subset of the
4861 first, and the ranges begin at the same place, we go from just after
4862 the end of the second range to the end of the first. If the second
4863 range is not a subset of the first, or if it is a subset and both
4864 ranges end at the same place, the range starts at the start of the
4865 first range and ends just before the second range.
4866 Otherwise, we can't describe this as a single range. */
4867 if (no_overlap)
4868 in_p = 1, low = low0, high = high0;
4869 else if (lowequal && highequal)
4870 in_p = 0, low = high = 0;
4871 else if (subset && lowequal)
4873 low = range_successor (high1);
4874 high = high0;
4875 in_p = 1;
4876 if (low == 0)
4878 /* We are in the weird situation where high0 > high1 but
4879 high1 has no successor. Punt. */
4880 return 0;
4883 else if (! subset || highequal)
4885 low = low0;
4886 high = range_predecessor (low1);
4887 in_p = 1;
4888 if (high == 0)
4890 /* low0 < low1 but low1 has no predecessor. Punt. */
4891 return 0;
4894 else
4895 return 0;
4898 else if (! in0_p && in1_p)
4900 /* If they don't overlap, the result is the second range. If the second
4901 is a subset of the first, the result is false. Otherwise,
4902 the range starts just after the first range and ends at the
4903 end of the second. */
4904 if (no_overlap)
4905 in_p = 1, low = low1, high = high1;
4906 else if (subset || highequal)
4907 in_p = 0, low = high = 0;
4908 else
4910 low = range_successor (high0);
4911 high = high1;
4912 in_p = 1;
4913 if (low == 0)
4915 /* high1 > high0 but high0 has no successor. Punt. */
4916 return 0;
4921 else
4923 /* The case where we are excluding both ranges. Here the complex case
4924 is if they don't overlap. In that case, the only time we have a
4925 range is if they are adjacent. If the second is a subset of the
4926 first, the result is the first. Otherwise, the range to exclude
4927 starts at the beginning of the first range and ends at the end of the
4928 second. */
4929 if (no_overlap)
4931 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4932 range_successor (high0),
4933 1, low1, 0)))
4934 in_p = 0, low = low0, high = high1;
4935 else
4937 /* Canonicalize - [min, x] into - [-, x]. */
4938 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4939 switch (TREE_CODE (TREE_TYPE (low0)))
4941 case ENUMERAL_TYPE:
4942 if (TYPE_PRECISION (TREE_TYPE (low0))
4943 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4944 break;
4945 /* FALLTHROUGH */
4946 case INTEGER_TYPE:
4947 if (tree_int_cst_equal (low0,
4948 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4949 low0 = 0;
4950 break;
4951 case POINTER_TYPE:
4952 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4953 && integer_zerop (low0))
4954 low0 = 0;
4955 break;
4956 default:
4957 break;
4960 /* Canonicalize - [x, max] into - [x, -]. */
4961 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4962 switch (TREE_CODE (TREE_TYPE (high1)))
4964 case ENUMERAL_TYPE:
4965 if (TYPE_PRECISION (TREE_TYPE (high1))
4966 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4967 break;
4968 /* FALLTHROUGH */
4969 case INTEGER_TYPE:
4970 if (tree_int_cst_equal (high1,
4971 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4972 high1 = 0;
4973 break;
4974 case POINTER_TYPE:
4975 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4976 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4977 high1, 1,
4978 build_int_cst (TREE_TYPE (high1), 1),
4979 1)))
4980 high1 = 0;
4981 break;
4982 default:
4983 break;
4986 /* The ranges might be also adjacent between the maximum and
4987 minimum values of the given type. For
4988 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4989 return + [x + 1, y - 1]. */
4990 if (low0 == 0 && high1 == 0)
4992 low = range_successor (high0);
4993 high = range_predecessor (low1);
4994 if (low == 0 || high == 0)
4995 return 0;
4997 in_p = 1;
4999 else
5000 return 0;
5003 else if (subset)
5004 in_p = 0, low = low0, high = high0;
5005 else
5006 in_p = 0, low = low0, high = high1;
5009 *pin_p = in_p, *plow = low, *phigh = high;
5010 return 1;
5014 /* Subroutine of fold, looking inside expressions of the form
5015 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5016 of the COND_EXPR. This function is being used also to optimize
5017 A op B ? C : A, by reversing the comparison first.
5019 Return a folded expression whose code is not a COND_EXPR
5020 anymore, or NULL_TREE if no folding opportunity is found. */
5022 static tree
5023 fold_cond_expr_with_comparison (location_t loc, tree type,
5024 tree arg0, tree arg1, tree arg2)
5026 enum tree_code comp_code = TREE_CODE (arg0);
5027 tree arg00 = TREE_OPERAND (arg0, 0);
5028 tree arg01 = TREE_OPERAND (arg0, 1);
5029 tree arg1_type = TREE_TYPE (arg1);
5030 tree tem;
5032 STRIP_NOPS (arg1);
5033 STRIP_NOPS (arg2);
5035 /* If we have A op 0 ? A : -A, consider applying the following
5036 transformations:
5038 A == 0? A : -A same as -A
5039 A != 0? A : -A same as A
5040 A >= 0? A : -A same as abs (A)
5041 A > 0? A : -A same as abs (A)
5042 A <= 0? A : -A same as -abs (A)
5043 A < 0? A : -A same as -abs (A)
5045 None of these transformations work for modes with signed
5046 zeros. If A is +/-0, the first two transformations will
5047 change the sign of the result (from +0 to -0, or vice
5048 versa). The last four will fix the sign of the result,
5049 even though the original expressions could be positive or
5050 negative, depending on the sign of A.
5052 Note that all these transformations are correct if A is
5053 NaN, since the two alternatives (A and -A) are also NaNs. */
5054 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5055 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5056 ? real_zerop (arg01)
5057 : integer_zerop (arg01))
5058 && ((TREE_CODE (arg2) == NEGATE_EXPR
5059 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5060 /* In the case that A is of the form X-Y, '-A' (arg2) may
5061 have already been folded to Y-X, check for that. */
5062 || (TREE_CODE (arg1) == MINUS_EXPR
5063 && TREE_CODE (arg2) == MINUS_EXPR
5064 && operand_equal_p (TREE_OPERAND (arg1, 0),
5065 TREE_OPERAND (arg2, 1), 0)
5066 && operand_equal_p (TREE_OPERAND (arg1, 1),
5067 TREE_OPERAND (arg2, 0), 0))))
5068 switch (comp_code)
5070 case EQ_EXPR:
5071 case UNEQ_EXPR:
5072 tem = fold_convert_loc (loc, arg1_type, arg1);
5073 return fold_convert_loc (loc, type, negate_expr (tem));
5074 case NE_EXPR:
5075 case LTGT_EXPR:
5076 return fold_convert_loc (loc, type, arg1);
5077 case UNGE_EXPR:
5078 case UNGT_EXPR:
5079 if (flag_trapping_math)
5080 break;
5081 /* Fall through. */
5082 case GE_EXPR:
5083 case GT_EXPR:
5084 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5085 break;
5086 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5087 return fold_convert_loc (loc, type, tem);
5088 case UNLE_EXPR:
5089 case UNLT_EXPR:
5090 if (flag_trapping_math)
5091 break;
5092 /* FALLTHRU */
5093 case LE_EXPR:
5094 case LT_EXPR:
5095 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5096 break;
5097 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5098 return negate_expr (fold_convert_loc (loc, type, tem));
5099 default:
5100 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5101 break;
5104 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5105 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5106 both transformations are correct when A is NaN: A != 0
5107 is then true, and A == 0 is false. */
5109 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5110 && integer_zerop (arg01) && integer_zerop (arg2))
5112 if (comp_code == NE_EXPR)
5113 return fold_convert_loc (loc, type, arg1);
5114 else if (comp_code == EQ_EXPR)
5115 return build_zero_cst (type);
5118 /* Try some transformations of A op B ? A : B.
5120 A == B? A : B same as B
5121 A != B? A : B same as A
5122 A >= B? A : B same as max (A, B)
5123 A > B? A : B same as max (B, A)
5124 A <= B? A : B same as min (A, B)
5125 A < B? A : B same as min (B, A)
5127 As above, these transformations don't work in the presence
5128 of signed zeros. For example, if A and B are zeros of
5129 opposite sign, the first two transformations will change
5130 the sign of the result. In the last four, the original
5131 expressions give different results for (A=+0, B=-0) and
5132 (A=-0, B=+0), but the transformed expressions do not.
5134 The first two transformations are correct if either A or B
5135 is a NaN. In the first transformation, the condition will
5136 be false, and B will indeed be chosen. In the case of the
5137 second transformation, the condition A != B will be true,
5138 and A will be chosen.
5140 The conversions to max() and min() are not correct if B is
5141 a number and A is not. The conditions in the original
5142 expressions will be false, so all four give B. The min()
5143 and max() versions would give a NaN instead. */
5144 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5145 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5146 /* Avoid these transformations if the COND_EXPR may be used
5147 as an lvalue in the C++ front-end. PR c++/19199. */
5148 && (in_gimple_form
5149 || VECTOR_TYPE_P (type)
5150 || (! lang_GNU_CXX ()
5151 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5152 || ! maybe_lvalue_p (arg1)
5153 || ! maybe_lvalue_p (arg2)))
5155 tree comp_op0 = arg00;
5156 tree comp_op1 = arg01;
5157 tree comp_type = TREE_TYPE (comp_op0);
5159 switch (comp_code)
5161 case EQ_EXPR:
5162 return fold_convert_loc (loc, type, arg2);
5163 case NE_EXPR:
5164 return fold_convert_loc (loc, type, arg1);
5165 case LE_EXPR:
5166 case LT_EXPR:
5167 case UNLE_EXPR:
5168 case UNLT_EXPR:
5169 /* In C++ a ?: expression can be an lvalue, so put the
5170 operand which will be used if they are equal first
5171 so that we can convert this back to the
5172 corresponding COND_EXPR. */
5173 if (!HONOR_NANS (arg1))
5175 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5176 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5177 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5178 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5179 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5180 comp_op1, comp_op0);
5181 return fold_convert_loc (loc, type, tem);
5183 break;
5184 case GE_EXPR:
5185 case GT_EXPR:
5186 case UNGE_EXPR:
5187 case UNGT_EXPR:
5188 if (!HONOR_NANS (arg1))
5190 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5191 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5192 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5193 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5194 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5195 comp_op1, comp_op0);
5196 return fold_convert_loc (loc, type, tem);
5198 break;
5199 case UNEQ_EXPR:
5200 if (!HONOR_NANS (arg1))
5201 return fold_convert_loc (loc, type, arg2);
5202 break;
5203 case LTGT_EXPR:
5204 if (!HONOR_NANS (arg1))
5205 return fold_convert_loc (loc, type, arg1);
5206 break;
5207 default:
5208 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5209 break;
5213 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5214 we might still be able to simplify this. For example,
5215 if C1 is one less or one more than C2, this might have started
5216 out as a MIN or MAX and been transformed by this function.
5217 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5219 if (INTEGRAL_TYPE_P (type)
5220 && TREE_CODE (arg01) == INTEGER_CST
5221 && TREE_CODE (arg2) == INTEGER_CST)
5222 switch (comp_code)
5224 case EQ_EXPR:
5225 if (TREE_CODE (arg1) == INTEGER_CST)
5226 break;
5227 /* We can replace A with C1 in this case. */
5228 arg1 = fold_convert_loc (loc, type, arg01);
5229 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5231 case LT_EXPR:
5232 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5233 MIN_EXPR, to preserve the signedness of the comparison. */
5234 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5235 OEP_ONLY_CONST)
5236 && operand_equal_p (arg01,
5237 const_binop (PLUS_EXPR, arg2,
5238 build_int_cst (type, 1)),
5239 OEP_ONLY_CONST))
5241 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5242 fold_convert_loc (loc, TREE_TYPE (arg00),
5243 arg2));
5244 return fold_convert_loc (loc, type, tem);
5246 break;
5248 case LE_EXPR:
5249 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5250 as above. */
5251 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5252 OEP_ONLY_CONST)
5253 && operand_equal_p (arg01,
5254 const_binop (MINUS_EXPR, arg2,
5255 build_int_cst (type, 1)),
5256 OEP_ONLY_CONST))
5258 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5259 fold_convert_loc (loc, TREE_TYPE (arg00),
5260 arg2));
5261 return fold_convert_loc (loc, type, tem);
5263 break;
5265 case GT_EXPR:
5266 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5267 MAX_EXPR, to preserve the signedness of the comparison. */
5268 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5269 OEP_ONLY_CONST)
5270 && operand_equal_p (arg01,
5271 const_binop (MINUS_EXPR, arg2,
5272 build_int_cst (type, 1)),
5273 OEP_ONLY_CONST))
5275 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5276 fold_convert_loc (loc, TREE_TYPE (arg00),
5277 arg2));
5278 return fold_convert_loc (loc, type, tem);
5280 break;
5282 case GE_EXPR:
5283 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5284 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5285 OEP_ONLY_CONST)
5286 && operand_equal_p (arg01,
5287 const_binop (PLUS_EXPR, arg2,
5288 build_int_cst (type, 1)),
5289 OEP_ONLY_CONST))
5291 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5292 fold_convert_loc (loc, TREE_TYPE (arg00),
5293 arg2));
5294 return fold_convert_loc (loc, type, tem);
5296 break;
5297 case NE_EXPR:
5298 break;
5299 default:
5300 gcc_unreachable ();
5303 return NULL_TREE;
5308 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5309 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5310 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5311 false) >= 2)
5312 #endif
5314 /* EXP is some logical combination of boolean tests. See if we can
5315 merge it into some range test. Return the new tree if so. */
5317 static tree
5318 fold_range_test (location_t loc, enum tree_code code, tree type,
5319 tree op0, tree op1)
5321 int or_op = (code == TRUTH_ORIF_EXPR
5322 || code == TRUTH_OR_EXPR);
5323 int in0_p, in1_p, in_p;
5324 tree low0, low1, low, high0, high1, high;
5325 bool strict_overflow_p = false;
5326 tree tem, lhs, rhs;
5327 const char * const warnmsg = G_("assuming signed overflow does not occur "
5328 "when simplifying range test");
5330 if (!INTEGRAL_TYPE_P (type))
5331 return 0;
5333 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5334 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5336 /* If this is an OR operation, invert both sides; we will invert
5337 again at the end. */
5338 if (or_op)
5339 in0_p = ! in0_p, in1_p = ! in1_p;
5341 /* If both expressions are the same, if we can merge the ranges, and we
5342 can build the range test, return it or it inverted. If one of the
5343 ranges is always true or always false, consider it to be the same
5344 expression as the other. */
5345 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5346 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5347 in1_p, low1, high1)
5348 && 0 != (tem = (build_range_check (loc, type,
5349 lhs != 0 ? lhs
5350 : rhs != 0 ? rhs : integer_zero_node,
5351 in_p, low, high))))
5353 if (strict_overflow_p)
5354 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5355 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5358 /* On machines where the branch cost is expensive, if this is a
5359 short-circuited branch and the underlying object on both sides
5360 is the same, make a non-short-circuit operation. */
5361 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5362 && lhs != 0 && rhs != 0
5363 && (code == TRUTH_ANDIF_EXPR
5364 || code == TRUTH_ORIF_EXPR)
5365 && operand_equal_p (lhs, rhs, 0))
5367 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5368 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5369 which cases we can't do this. */
5370 if (simple_operand_p (lhs))
5371 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5372 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5373 type, op0, op1);
5375 else if (!lang_hooks.decls.global_bindings_p ()
5376 && !CONTAINS_PLACEHOLDER_P (lhs))
5378 tree common = save_expr (lhs);
5380 if (0 != (lhs = build_range_check (loc, type, common,
5381 or_op ? ! in0_p : in0_p,
5382 low0, high0))
5383 && (0 != (rhs = build_range_check (loc, type, common,
5384 or_op ? ! in1_p : in1_p,
5385 low1, high1))))
5387 if (strict_overflow_p)
5388 fold_overflow_warning (warnmsg,
5389 WARN_STRICT_OVERFLOW_COMPARISON);
5390 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5391 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5392 type, lhs, rhs);
5397 return 0;
5400 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5401 bit value. Arrange things so the extra bits will be set to zero if and
5402 only if C is signed-extended to its full width. If MASK is nonzero,
5403 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5405 static tree
5406 unextend (tree c, int p, int unsignedp, tree mask)
5408 tree type = TREE_TYPE (c);
5409 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5410 tree temp;
5412 if (p == modesize || unsignedp)
5413 return c;
5415 /* We work by getting just the sign bit into the low-order bit, then
5416 into the high-order bit, then sign-extend. We then XOR that value
5417 with C. */
5418 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5420 /* We must use a signed type in order to get an arithmetic right shift.
5421 However, we must also avoid introducing accidental overflows, so that
5422 a subsequent call to integer_zerop will work. Hence we must
5423 do the type conversion here. At this point, the constant is either
5424 zero or one, and the conversion to a signed type can never overflow.
5425 We could get an overflow if this conversion is done anywhere else. */
5426 if (TYPE_UNSIGNED (type))
5427 temp = fold_convert (signed_type_for (type), temp);
5429 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5430 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5431 if (mask != 0)
5432 temp = const_binop (BIT_AND_EXPR, temp,
5433 fold_convert (TREE_TYPE (c), mask));
5434 /* If necessary, convert the type back to match the type of C. */
5435 if (TYPE_UNSIGNED (type))
5436 temp = fold_convert (type, temp);
5438 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5441 /* For an expression that has the form
5442 (A && B) || ~B
5444 (A || B) && ~B,
5445 we can drop one of the inner expressions and simplify to
5446 A || ~B
5448 A && ~B
5449 LOC is the location of the resulting expression. OP is the inner
5450 logical operation; the left-hand side in the examples above, while CMPOP
5451 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5452 removing a condition that guards another, as in
5453 (A != NULL && A->...) || A == NULL
5454 which we must not transform. If RHS_ONLY is true, only eliminate the
5455 right-most operand of the inner logical operation. */
5457 static tree
5458 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5459 bool rhs_only)
5461 tree type = TREE_TYPE (cmpop);
5462 enum tree_code code = TREE_CODE (cmpop);
5463 enum tree_code truthop_code = TREE_CODE (op);
5464 tree lhs = TREE_OPERAND (op, 0);
5465 tree rhs = TREE_OPERAND (op, 1);
5466 tree orig_lhs = lhs, orig_rhs = rhs;
5467 enum tree_code rhs_code = TREE_CODE (rhs);
5468 enum tree_code lhs_code = TREE_CODE (lhs);
5469 enum tree_code inv_code;
5471 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5472 return NULL_TREE;
5474 if (TREE_CODE_CLASS (code) != tcc_comparison)
5475 return NULL_TREE;
5477 if (rhs_code == truthop_code)
5479 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5480 if (newrhs != NULL_TREE)
5482 rhs = newrhs;
5483 rhs_code = TREE_CODE (rhs);
5486 if (lhs_code == truthop_code && !rhs_only)
5488 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5489 if (newlhs != NULL_TREE)
5491 lhs = newlhs;
5492 lhs_code = TREE_CODE (lhs);
5496 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5497 if (inv_code == rhs_code
5498 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5499 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5500 return lhs;
5501 if (!rhs_only && inv_code == lhs_code
5502 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5503 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5504 return rhs;
5505 if (rhs != orig_rhs || lhs != orig_lhs)
5506 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5507 lhs, rhs);
5508 return NULL_TREE;
5511 /* Find ways of folding logical expressions of LHS and RHS:
5512 Try to merge two comparisons to the same innermost item.
5513 Look for range tests like "ch >= '0' && ch <= '9'".
5514 Look for combinations of simple terms on machines with expensive branches
5515 and evaluate the RHS unconditionally.
5517 For example, if we have p->a == 2 && p->b == 4 and we can make an
5518 object large enough to span both A and B, we can do this with a comparison
5519 against the object ANDed with the a mask.
5521 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5522 operations to do this with one comparison.
5524 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5525 function and the one above.
5527 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5528 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5530 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5531 two operands.
5533 We return the simplified tree or 0 if no optimization is possible. */
5535 static tree
5536 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5537 tree lhs, tree rhs)
5539 /* If this is the "or" of two comparisons, we can do something if
5540 the comparisons are NE_EXPR. If this is the "and", we can do something
5541 if the comparisons are EQ_EXPR. I.e.,
5542 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5544 WANTED_CODE is this operation code. For single bit fields, we can
5545 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5546 comparison for one-bit fields. */
5548 enum tree_code wanted_code;
5549 enum tree_code lcode, rcode;
5550 tree ll_arg, lr_arg, rl_arg, rr_arg;
5551 tree ll_inner, lr_inner, rl_inner, rr_inner;
5552 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5553 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5554 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5555 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5556 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5557 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5558 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5559 machine_mode lnmode, rnmode;
5560 tree ll_mask, lr_mask, rl_mask, rr_mask;
5561 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5562 tree l_const, r_const;
5563 tree lntype, rntype, result;
5564 HOST_WIDE_INT first_bit, end_bit;
5565 int volatilep;
5567 /* Start by getting the comparison codes. Fail if anything is volatile.
5568 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5569 it were surrounded with a NE_EXPR. */
5571 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5572 return 0;
5574 lcode = TREE_CODE (lhs);
5575 rcode = TREE_CODE (rhs);
5577 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5579 lhs = build2 (NE_EXPR, truth_type, lhs,
5580 build_int_cst (TREE_TYPE (lhs), 0));
5581 lcode = NE_EXPR;
5584 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5586 rhs = build2 (NE_EXPR, truth_type, rhs,
5587 build_int_cst (TREE_TYPE (rhs), 0));
5588 rcode = NE_EXPR;
5591 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5592 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5593 return 0;
5595 ll_arg = TREE_OPERAND (lhs, 0);
5596 lr_arg = TREE_OPERAND (lhs, 1);
5597 rl_arg = TREE_OPERAND (rhs, 0);
5598 rr_arg = TREE_OPERAND (rhs, 1);
5600 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5601 if (simple_operand_p (ll_arg)
5602 && simple_operand_p (lr_arg))
5604 if (operand_equal_p (ll_arg, rl_arg, 0)
5605 && operand_equal_p (lr_arg, rr_arg, 0))
5607 result = combine_comparisons (loc, code, lcode, rcode,
5608 truth_type, ll_arg, lr_arg);
5609 if (result)
5610 return result;
5612 else if (operand_equal_p (ll_arg, rr_arg, 0)
5613 && operand_equal_p (lr_arg, rl_arg, 0))
5615 result = combine_comparisons (loc, code, lcode,
5616 swap_tree_comparison (rcode),
5617 truth_type, ll_arg, lr_arg);
5618 if (result)
5619 return result;
5623 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5624 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5626 /* If the RHS can be evaluated unconditionally and its operands are
5627 simple, it wins to evaluate the RHS unconditionally on machines
5628 with expensive branches. In this case, this isn't a comparison
5629 that can be merged. */
5631 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5632 false) >= 2
5633 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5634 && simple_operand_p (rl_arg)
5635 && simple_operand_p (rr_arg))
5637 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5638 if (code == TRUTH_OR_EXPR
5639 && lcode == NE_EXPR && integer_zerop (lr_arg)
5640 && rcode == NE_EXPR && integer_zerop (rr_arg)
5641 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5642 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5643 return build2_loc (loc, NE_EXPR, truth_type,
5644 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5645 ll_arg, rl_arg),
5646 build_int_cst (TREE_TYPE (ll_arg), 0));
5648 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5649 if (code == TRUTH_AND_EXPR
5650 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5651 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5652 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5653 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5654 return build2_loc (loc, EQ_EXPR, truth_type,
5655 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5656 ll_arg, rl_arg),
5657 build_int_cst (TREE_TYPE (ll_arg), 0));
5660 /* See if the comparisons can be merged. Then get all the parameters for
5661 each side. */
5663 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5664 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5665 return 0;
5667 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5668 volatilep = 0;
5669 ll_inner = decode_field_reference (loc, &ll_arg,
5670 &ll_bitsize, &ll_bitpos, &ll_mode,
5671 &ll_unsignedp, &ll_reversep, &volatilep,
5672 &ll_mask, &ll_and_mask);
5673 lr_inner = decode_field_reference (loc, &lr_arg,
5674 &lr_bitsize, &lr_bitpos, &lr_mode,
5675 &lr_unsignedp, &lr_reversep, &volatilep,
5676 &lr_mask, &lr_and_mask);
5677 rl_inner = decode_field_reference (loc, &rl_arg,
5678 &rl_bitsize, &rl_bitpos, &rl_mode,
5679 &rl_unsignedp, &rl_reversep, &volatilep,
5680 &rl_mask, &rl_and_mask);
5681 rr_inner = decode_field_reference (loc, &rr_arg,
5682 &rr_bitsize, &rr_bitpos, &rr_mode,
5683 &rr_unsignedp, &rr_reversep, &volatilep,
5684 &rr_mask, &rr_and_mask);
5686 /* It must be true that the inner operation on the lhs of each
5687 comparison must be the same if we are to be able to do anything.
5688 Then see if we have constants. If not, the same must be true for
5689 the rhs's. */
5690 if (volatilep
5691 || ll_reversep != rl_reversep
5692 || ll_inner == 0 || rl_inner == 0
5693 || ! operand_equal_p (ll_inner, rl_inner, 0))
5694 return 0;
5696 if (TREE_CODE (lr_arg) == INTEGER_CST
5697 && TREE_CODE (rr_arg) == INTEGER_CST)
5699 l_const = lr_arg, r_const = rr_arg;
5700 lr_reversep = ll_reversep;
5702 else if (lr_reversep != rr_reversep
5703 || lr_inner == 0 || rr_inner == 0
5704 || ! operand_equal_p (lr_inner, rr_inner, 0))
5705 return 0;
5706 else
5707 l_const = r_const = 0;
5709 /* If either comparison code is not correct for our logical operation,
5710 fail. However, we can convert a one-bit comparison against zero into
5711 the opposite comparison against that bit being set in the field. */
5713 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5714 if (lcode != wanted_code)
5716 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5718 /* Make the left operand unsigned, since we are only interested
5719 in the value of one bit. Otherwise we are doing the wrong
5720 thing below. */
5721 ll_unsignedp = 1;
5722 l_const = ll_mask;
5724 else
5725 return 0;
5728 /* This is analogous to the code for l_const above. */
5729 if (rcode != wanted_code)
5731 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5733 rl_unsignedp = 1;
5734 r_const = rl_mask;
5736 else
5737 return 0;
5740 /* See if we can find a mode that contains both fields being compared on
5741 the left. If we can't, fail. Otherwise, update all constants and masks
5742 to be relative to a field of that size. */
5743 first_bit = MIN (ll_bitpos, rl_bitpos);
5744 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5745 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5746 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5747 volatilep);
5748 if (lnmode == VOIDmode)
5749 return 0;
5751 lnbitsize = GET_MODE_BITSIZE (lnmode);
5752 lnbitpos = first_bit & ~ (lnbitsize - 1);
5753 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5754 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5756 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5758 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5759 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5762 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5763 size_int (xll_bitpos));
5764 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5765 size_int (xrl_bitpos));
5767 if (l_const)
5769 l_const = fold_convert_loc (loc, lntype, l_const);
5770 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5771 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5772 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5773 fold_build1_loc (loc, BIT_NOT_EXPR,
5774 lntype, ll_mask))))
5776 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5778 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5781 if (r_const)
5783 r_const = fold_convert_loc (loc, lntype, r_const);
5784 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5785 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5786 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5787 fold_build1_loc (loc, BIT_NOT_EXPR,
5788 lntype, rl_mask))))
5790 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5792 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5796 /* If the right sides are not constant, do the same for it. Also,
5797 disallow this optimization if a size or signedness mismatch occurs
5798 between the left and right sides. */
5799 if (l_const == 0)
5801 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5802 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5803 /* Make sure the two fields on the right
5804 correspond to the left without being swapped. */
5805 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5806 return 0;
5808 first_bit = MIN (lr_bitpos, rr_bitpos);
5809 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5810 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5811 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5812 volatilep);
5813 if (rnmode == VOIDmode)
5814 return 0;
5816 rnbitsize = GET_MODE_BITSIZE (rnmode);
5817 rnbitpos = first_bit & ~ (rnbitsize - 1);
5818 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5819 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5821 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5823 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5824 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5827 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5828 rntype, lr_mask),
5829 size_int (xlr_bitpos));
5830 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5831 rntype, rr_mask),
5832 size_int (xrr_bitpos));
5834 /* Make a mask that corresponds to both fields being compared.
5835 Do this for both items being compared. If the operands are the
5836 same size and the bits being compared are in the same position
5837 then we can do this by masking both and comparing the masked
5838 results. */
5839 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5840 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5841 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5843 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5844 lntype, lnbitsize, lnbitpos,
5845 ll_unsignedp || rl_unsignedp, ll_reversep);
5846 if (! all_ones_mask_p (ll_mask, lnbitsize))
5847 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5849 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5850 rntype, rnbitsize, rnbitpos,
5851 lr_unsignedp || rr_unsignedp, lr_reversep);
5852 if (! all_ones_mask_p (lr_mask, rnbitsize))
5853 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5855 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5858 /* There is still another way we can do something: If both pairs of
5859 fields being compared are adjacent, we may be able to make a wider
5860 field containing them both.
5862 Note that we still must mask the lhs/rhs expressions. Furthermore,
5863 the mask must be shifted to account for the shift done by
5864 make_bit_field_ref. */
5865 if ((ll_bitsize + ll_bitpos == rl_bitpos
5866 && lr_bitsize + lr_bitpos == rr_bitpos)
5867 || (ll_bitpos == rl_bitpos + rl_bitsize
5868 && lr_bitpos == rr_bitpos + rr_bitsize))
5870 tree type;
5872 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5873 ll_bitsize + rl_bitsize,
5874 MIN (ll_bitpos, rl_bitpos),
5875 ll_unsignedp, ll_reversep);
5876 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5877 lr_bitsize + rr_bitsize,
5878 MIN (lr_bitpos, rr_bitpos),
5879 lr_unsignedp, lr_reversep);
5881 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5882 size_int (MIN (xll_bitpos, xrl_bitpos)));
5883 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5884 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5886 /* Convert to the smaller type before masking out unwanted bits. */
5887 type = lntype;
5888 if (lntype != rntype)
5890 if (lnbitsize > rnbitsize)
5892 lhs = fold_convert_loc (loc, rntype, lhs);
5893 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5894 type = rntype;
5896 else if (lnbitsize < rnbitsize)
5898 rhs = fold_convert_loc (loc, lntype, rhs);
5899 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5900 type = lntype;
5904 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5905 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5907 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5908 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5910 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5913 return 0;
5916 /* Handle the case of comparisons with constants. If there is something in
5917 common between the masks, those bits of the constants must be the same.
5918 If not, the condition is always false. Test for this to avoid generating
5919 incorrect code below. */
5920 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5921 if (! integer_zerop (result)
5922 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5923 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5925 if (wanted_code == NE_EXPR)
5927 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5928 return constant_boolean_node (true, truth_type);
5930 else
5932 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5933 return constant_boolean_node (false, truth_type);
5937 /* Construct the expression we will return. First get the component
5938 reference we will make. Unless the mask is all ones the width of
5939 that field, perform the mask operation. Then compare with the
5940 merged constant. */
5941 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5942 lntype, lnbitsize, lnbitpos,
5943 ll_unsignedp || rl_unsignedp, ll_reversep);
5945 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5946 if (! all_ones_mask_p (ll_mask, lnbitsize))
5947 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5949 return build2_loc (loc, wanted_code, truth_type, result,
5950 const_binop (BIT_IOR_EXPR, l_const, r_const));
5953 /* T is an integer expression that is being multiplied, divided, or taken a
5954 modulus (CODE says which and what kind of divide or modulus) by a
5955 constant C. See if we can eliminate that operation by folding it with
5956 other operations already in T. WIDE_TYPE, if non-null, is a type that
5957 should be used for the computation if wider than our type.
5959 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5960 (X * 2) + (Y * 4). We must, however, be assured that either the original
5961 expression would not overflow or that overflow is undefined for the type
5962 in the language in question.
5964 If we return a non-null expression, it is an equivalent form of the
5965 original computation, but need not be in the original type.
5967 We set *STRICT_OVERFLOW_P to true if the return values depends on
5968 signed overflow being undefined. Otherwise we do not change
5969 *STRICT_OVERFLOW_P. */
5971 static tree
5972 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5973 bool *strict_overflow_p)
5975 /* To avoid exponential search depth, refuse to allow recursion past
5976 three levels. Beyond that (1) it's highly unlikely that we'll find
5977 something interesting and (2) we've probably processed it before
5978 when we built the inner expression. */
5980 static int depth;
5981 tree ret;
5983 if (depth > 3)
5984 return NULL;
5986 depth++;
5987 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5988 depth--;
5990 return ret;
5993 static tree
5994 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5995 bool *strict_overflow_p)
5997 tree type = TREE_TYPE (t);
5998 enum tree_code tcode = TREE_CODE (t);
5999 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6000 > GET_MODE_SIZE (TYPE_MODE (type)))
6001 ? wide_type : type);
6002 tree t1, t2;
6003 int same_p = tcode == code;
6004 tree op0 = NULL_TREE, op1 = NULL_TREE;
6005 bool sub_strict_overflow_p;
6007 /* Don't deal with constants of zero here; they confuse the code below. */
6008 if (integer_zerop (c))
6009 return NULL_TREE;
6011 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6012 op0 = TREE_OPERAND (t, 0);
6014 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6015 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6017 /* Note that we need not handle conditional operations here since fold
6018 already handles those cases. So just do arithmetic here. */
6019 switch (tcode)
6021 case INTEGER_CST:
6022 /* For a constant, we can always simplify if we are a multiply
6023 or (for divide and modulus) if it is a multiple of our constant. */
6024 if (code == MULT_EXPR
6025 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6027 tree tem = const_binop (code, fold_convert (ctype, t),
6028 fold_convert (ctype, c));
6029 /* If the multiplication overflowed, we lost information on it.
6030 See PR68142 and PR69845. */
6031 if (TREE_OVERFLOW (tem))
6032 return NULL_TREE;
6033 return tem;
6035 break;
6037 CASE_CONVERT: case NON_LVALUE_EXPR:
6038 /* If op0 is an expression ... */
6039 if ((COMPARISON_CLASS_P (op0)
6040 || UNARY_CLASS_P (op0)
6041 || BINARY_CLASS_P (op0)
6042 || VL_EXP_CLASS_P (op0)
6043 || EXPRESSION_CLASS_P (op0))
6044 /* ... and has wrapping overflow, and its type is smaller
6045 than ctype, then we cannot pass through as widening. */
6046 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6047 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6048 && (TYPE_PRECISION (ctype)
6049 > TYPE_PRECISION (TREE_TYPE (op0))))
6050 /* ... or this is a truncation (t is narrower than op0),
6051 then we cannot pass through this narrowing. */
6052 || (TYPE_PRECISION (type)
6053 < TYPE_PRECISION (TREE_TYPE (op0)))
6054 /* ... or signedness changes for division or modulus,
6055 then we cannot pass through this conversion. */
6056 || (code != MULT_EXPR
6057 && (TYPE_UNSIGNED (ctype)
6058 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6059 /* ... or has undefined overflow while the converted to
6060 type has not, we cannot do the operation in the inner type
6061 as that would introduce undefined overflow. */
6062 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6063 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6064 && !TYPE_OVERFLOW_UNDEFINED (type))))
6065 break;
6067 /* Pass the constant down and see if we can make a simplification. If
6068 we can, replace this expression with the inner simplification for
6069 possible later conversion to our or some other type. */
6070 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6071 && TREE_CODE (t2) == INTEGER_CST
6072 && !TREE_OVERFLOW (t2)
6073 && (0 != (t1 = extract_muldiv (op0, t2, code,
6074 code == MULT_EXPR
6075 ? ctype : NULL_TREE,
6076 strict_overflow_p))))
6077 return t1;
6078 break;
6080 case ABS_EXPR:
6081 /* If widening the type changes it from signed to unsigned, then we
6082 must avoid building ABS_EXPR itself as unsigned. */
6083 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6085 tree cstype = (*signed_type_for) (ctype);
6086 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6087 != 0)
6089 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6090 return fold_convert (ctype, t1);
6092 break;
6094 /* If the constant is negative, we cannot simplify this. */
6095 if (tree_int_cst_sgn (c) == -1)
6096 break;
6097 /* FALLTHROUGH */
6098 case NEGATE_EXPR:
6099 /* For division and modulus, type can't be unsigned, as e.g.
6100 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6101 For signed types, even with wrapping overflow, this is fine. */
6102 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6103 break;
6104 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6105 != 0)
6106 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6107 break;
6109 case MIN_EXPR: case MAX_EXPR:
6110 /* If widening the type changes the signedness, then we can't perform
6111 this optimization as that changes the result. */
6112 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6113 break;
6115 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6116 sub_strict_overflow_p = false;
6117 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6118 &sub_strict_overflow_p)) != 0
6119 && (t2 = extract_muldiv (op1, c, code, wide_type,
6120 &sub_strict_overflow_p)) != 0)
6122 if (tree_int_cst_sgn (c) < 0)
6123 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6124 if (sub_strict_overflow_p)
6125 *strict_overflow_p = true;
6126 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6127 fold_convert (ctype, t2));
6129 break;
6131 case LSHIFT_EXPR: case RSHIFT_EXPR:
6132 /* If the second operand is constant, this is a multiplication
6133 or floor division, by a power of two, so we can treat it that
6134 way unless the multiplier or divisor overflows. Signed
6135 left-shift overflow is implementation-defined rather than
6136 undefined in C90, so do not convert signed left shift into
6137 multiplication. */
6138 if (TREE_CODE (op1) == INTEGER_CST
6139 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6140 /* const_binop may not detect overflow correctly,
6141 so check for it explicitly here. */
6142 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6143 && 0 != (t1 = fold_convert (ctype,
6144 const_binop (LSHIFT_EXPR,
6145 size_one_node,
6146 op1)))
6147 && !TREE_OVERFLOW (t1))
6148 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6149 ? MULT_EXPR : FLOOR_DIV_EXPR,
6150 ctype,
6151 fold_convert (ctype, op0),
6152 t1),
6153 c, code, wide_type, strict_overflow_p);
6154 break;
6156 case PLUS_EXPR: case MINUS_EXPR:
6157 /* See if we can eliminate the operation on both sides. If we can, we
6158 can return a new PLUS or MINUS. If we can't, the only remaining
6159 cases where we can do anything are if the second operand is a
6160 constant. */
6161 sub_strict_overflow_p = false;
6162 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6163 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6164 if (t1 != 0 && t2 != 0
6165 && (code == MULT_EXPR
6166 /* If not multiplication, we can only do this if both operands
6167 are divisible by c. */
6168 || (multiple_of_p (ctype, op0, c)
6169 && multiple_of_p (ctype, op1, c))))
6171 if (sub_strict_overflow_p)
6172 *strict_overflow_p = true;
6173 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6174 fold_convert (ctype, t2));
6177 /* If this was a subtraction, negate OP1 and set it to be an addition.
6178 This simplifies the logic below. */
6179 if (tcode == MINUS_EXPR)
6181 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6182 /* If OP1 was not easily negatable, the constant may be OP0. */
6183 if (TREE_CODE (op0) == INTEGER_CST)
6185 std::swap (op0, op1);
6186 std::swap (t1, t2);
6190 if (TREE_CODE (op1) != INTEGER_CST)
6191 break;
6193 /* If either OP1 or C are negative, this optimization is not safe for
6194 some of the division and remainder types while for others we need
6195 to change the code. */
6196 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6198 if (code == CEIL_DIV_EXPR)
6199 code = FLOOR_DIV_EXPR;
6200 else if (code == FLOOR_DIV_EXPR)
6201 code = CEIL_DIV_EXPR;
6202 else if (code != MULT_EXPR
6203 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6204 break;
6207 /* If it's a multiply or a division/modulus operation of a multiple
6208 of our constant, do the operation and verify it doesn't overflow. */
6209 if (code == MULT_EXPR
6210 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6212 op1 = const_binop (code, fold_convert (ctype, op1),
6213 fold_convert (ctype, c));
6214 /* We allow the constant to overflow with wrapping semantics. */
6215 if (op1 == 0
6216 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6217 break;
6219 else
6220 break;
6222 /* If we have an unsigned type, we cannot widen the operation since it
6223 will change the result if the original computation overflowed. */
6224 if (TYPE_UNSIGNED (ctype) && ctype != type)
6225 break;
6227 /* If we were able to eliminate our operation from the first side,
6228 apply our operation to the second side and reform the PLUS. */
6229 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6230 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6232 /* The last case is if we are a multiply. In that case, we can
6233 apply the distributive law to commute the multiply and addition
6234 if the multiplication of the constants doesn't overflow
6235 and overflow is defined. With undefined overflow
6236 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6237 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6238 return fold_build2 (tcode, ctype,
6239 fold_build2 (code, ctype,
6240 fold_convert (ctype, op0),
6241 fold_convert (ctype, c)),
6242 op1);
6244 break;
6246 case MULT_EXPR:
6247 /* We have a special case here if we are doing something like
6248 (C * 8) % 4 since we know that's zero. */
6249 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6250 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6251 /* If the multiplication can overflow we cannot optimize this. */
6252 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6253 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6254 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6256 *strict_overflow_p = true;
6257 return omit_one_operand (type, integer_zero_node, op0);
6260 /* ... fall through ... */
6262 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6263 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6264 /* If we can extract our operation from the LHS, do so and return a
6265 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6266 do something only if the second operand is a constant. */
6267 if (same_p
6268 && (t1 = extract_muldiv (op0, c, code, wide_type,
6269 strict_overflow_p)) != 0)
6270 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6271 fold_convert (ctype, op1));
6272 else if (tcode == MULT_EXPR && code == MULT_EXPR
6273 && (t1 = extract_muldiv (op1, c, code, wide_type,
6274 strict_overflow_p)) != 0)
6275 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6276 fold_convert (ctype, t1));
6277 else if (TREE_CODE (op1) != INTEGER_CST)
6278 return 0;
6280 /* If these are the same operation types, we can associate them
6281 assuming no overflow. */
6282 if (tcode == code)
6284 bool overflow_p = false;
6285 bool overflow_mul_p;
6286 signop sign = TYPE_SIGN (ctype);
6287 unsigned prec = TYPE_PRECISION (ctype);
6288 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6289 wi::to_wide (c, prec),
6290 sign, &overflow_mul_p);
6291 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6292 if (overflow_mul_p
6293 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6294 overflow_p = true;
6295 if (!overflow_p)
6296 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6297 wide_int_to_tree (ctype, mul));
6300 /* If these operations "cancel" each other, we have the main
6301 optimizations of this pass, which occur when either constant is a
6302 multiple of the other, in which case we replace this with either an
6303 operation or CODE or TCODE.
6305 If we have an unsigned type, we cannot do this since it will change
6306 the result if the original computation overflowed. */
6307 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6308 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6309 || (tcode == MULT_EXPR
6310 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6311 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6312 && code != MULT_EXPR)))
6314 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6316 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6317 *strict_overflow_p = true;
6318 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6319 fold_convert (ctype,
6320 const_binop (TRUNC_DIV_EXPR,
6321 op1, c)));
6323 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6325 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6326 *strict_overflow_p = true;
6327 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6328 fold_convert (ctype,
6329 const_binop (TRUNC_DIV_EXPR,
6330 c, op1)));
6333 break;
6335 default:
6336 break;
6339 return 0;
6342 /* Return a node which has the indicated constant VALUE (either 0 or
6343 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6344 and is of the indicated TYPE. */
6346 tree
6347 constant_boolean_node (bool value, tree type)
6349 if (type == integer_type_node)
6350 return value ? integer_one_node : integer_zero_node;
6351 else if (type == boolean_type_node)
6352 return value ? boolean_true_node : boolean_false_node;
6353 else if (TREE_CODE (type) == VECTOR_TYPE)
6354 return build_vector_from_val (type,
6355 build_int_cst (TREE_TYPE (type),
6356 value ? -1 : 0));
6357 else
6358 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6362 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6363 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6364 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6365 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6366 COND is the first argument to CODE; otherwise (as in the example
6367 given here), it is the second argument. TYPE is the type of the
6368 original expression. Return NULL_TREE if no simplification is
6369 possible. */
6371 static tree
6372 fold_binary_op_with_conditional_arg (location_t loc,
6373 enum tree_code code,
6374 tree type, tree op0, tree op1,
6375 tree cond, tree arg, int cond_first_p)
6377 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6378 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6379 tree test, true_value, false_value;
6380 tree lhs = NULL_TREE;
6381 tree rhs = NULL_TREE;
6382 enum tree_code cond_code = COND_EXPR;
6384 if (TREE_CODE (cond) == COND_EXPR
6385 || TREE_CODE (cond) == VEC_COND_EXPR)
6387 test = TREE_OPERAND (cond, 0);
6388 true_value = TREE_OPERAND (cond, 1);
6389 false_value = TREE_OPERAND (cond, 2);
6390 /* If this operand throws an expression, then it does not make
6391 sense to try to perform a logical or arithmetic operation
6392 involving it. */
6393 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6394 lhs = true_value;
6395 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6396 rhs = false_value;
6398 else if (!(TREE_CODE (type) != VECTOR_TYPE
6399 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6401 tree testtype = TREE_TYPE (cond);
6402 test = cond;
6403 true_value = constant_boolean_node (true, testtype);
6404 false_value = constant_boolean_node (false, testtype);
6406 else
6407 /* Detect the case of mixing vector and scalar types - bail out. */
6408 return NULL_TREE;
6410 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6411 cond_code = VEC_COND_EXPR;
6413 /* This transformation is only worthwhile if we don't have to wrap ARG
6414 in a SAVE_EXPR and the operation can be simplified without recursing
6415 on at least one of the branches once its pushed inside the COND_EXPR. */
6416 if (!TREE_CONSTANT (arg)
6417 && (TREE_SIDE_EFFECTS (arg)
6418 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6419 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6420 return NULL_TREE;
6422 arg = fold_convert_loc (loc, arg_type, arg);
6423 if (lhs == 0)
6425 true_value = fold_convert_loc (loc, cond_type, true_value);
6426 if (cond_first_p)
6427 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6428 else
6429 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6431 if (rhs == 0)
6433 false_value = fold_convert_loc (loc, cond_type, false_value);
6434 if (cond_first_p)
6435 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6436 else
6437 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6440 /* Check that we have simplified at least one of the branches. */
6441 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6442 return NULL_TREE;
6444 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6448 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6450 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6451 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6452 ADDEND is the same as X.
6454 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6455 and finite. The problematic cases are when X is zero, and its mode
6456 has signed zeros. In the case of rounding towards -infinity,
6457 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6458 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6460 bool
6461 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6463 if (!real_zerop (addend))
6464 return false;
6466 /* Don't allow the fold with -fsignaling-nans. */
6467 if (HONOR_SNANS (element_mode (type)))
6468 return false;
6470 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6471 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6472 return true;
6474 /* In a vector or complex, we would need to check the sign of all zeros. */
6475 if (TREE_CODE (addend) != REAL_CST)
6476 return false;
6478 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6479 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6480 negate = !negate;
6482 /* The mode has signed zeros, and we have to honor their sign.
6483 In this situation, there is only one case we can return true for.
6484 X - 0 is the same as X unless rounding towards -infinity is
6485 supported. */
6486 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6489 /* Subroutine of fold() that optimizes comparisons of a division by
6490 a nonzero integer constant against an integer constant, i.e.
6491 X/C1 op C2.
6493 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6494 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6495 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6497 The function returns the constant folded tree if a simplification
6498 can be made, and NULL_TREE otherwise. */
6500 static tree
6501 fold_div_compare (location_t loc,
6502 enum tree_code code, tree type, tree arg0, tree arg1)
6504 tree prod, tmp, hi, lo;
6505 tree arg00 = TREE_OPERAND (arg0, 0);
6506 tree arg01 = TREE_OPERAND (arg0, 1);
6507 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6508 bool neg_overflow = false;
6509 bool overflow;
6511 /* We have to do this the hard way to detect unsigned overflow.
6512 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6513 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6514 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6515 neg_overflow = false;
6517 if (sign == UNSIGNED)
6519 tmp = int_const_binop (MINUS_EXPR, arg01,
6520 build_int_cst (TREE_TYPE (arg01), 1));
6521 lo = prod;
6523 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6524 val = wi::add (prod, tmp, sign, &overflow);
6525 hi = force_fit_type (TREE_TYPE (arg00), val,
6526 -1, overflow | TREE_OVERFLOW (prod));
6528 else if (tree_int_cst_sgn (arg01) >= 0)
6530 tmp = int_const_binop (MINUS_EXPR, arg01,
6531 build_int_cst (TREE_TYPE (arg01), 1));
6532 switch (tree_int_cst_sgn (arg1))
6534 case -1:
6535 neg_overflow = true;
6536 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6537 hi = prod;
6538 break;
6540 case 0:
6541 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6542 hi = tmp;
6543 break;
6545 case 1:
6546 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6547 lo = prod;
6548 break;
6550 default:
6551 gcc_unreachable ();
6554 else
6556 /* A negative divisor reverses the relational operators. */
6557 code = swap_tree_comparison (code);
6559 tmp = int_const_binop (PLUS_EXPR, arg01,
6560 build_int_cst (TREE_TYPE (arg01), 1));
6561 switch (tree_int_cst_sgn (arg1))
6563 case -1:
6564 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6565 lo = prod;
6566 break;
6568 case 0:
6569 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6570 lo = tmp;
6571 break;
6573 case 1:
6574 neg_overflow = true;
6575 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6576 hi = prod;
6577 break;
6579 default:
6580 gcc_unreachable ();
6584 switch (code)
6586 case EQ_EXPR:
6587 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6588 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6589 if (TREE_OVERFLOW (hi))
6590 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6591 if (TREE_OVERFLOW (lo))
6592 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6593 return build_range_check (loc, type, arg00, 1, lo, hi);
6595 case NE_EXPR:
6596 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6597 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6598 if (TREE_OVERFLOW (hi))
6599 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6600 if (TREE_OVERFLOW (lo))
6601 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6602 return build_range_check (loc, type, arg00, 0, lo, hi);
6604 case LT_EXPR:
6605 if (TREE_OVERFLOW (lo))
6607 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6608 return omit_one_operand_loc (loc, type, tmp, arg00);
6610 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6612 case LE_EXPR:
6613 if (TREE_OVERFLOW (hi))
6615 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6616 return omit_one_operand_loc (loc, type, tmp, arg00);
6618 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6620 case GT_EXPR:
6621 if (TREE_OVERFLOW (hi))
6623 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6624 return omit_one_operand_loc (loc, type, tmp, arg00);
6626 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6628 case GE_EXPR:
6629 if (TREE_OVERFLOW (lo))
6631 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6632 return omit_one_operand_loc (loc, type, tmp, arg00);
6634 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6636 default:
6637 break;
6640 return NULL_TREE;
6644 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6645 equality/inequality test, then return a simplified form of the test
6646 using a sign testing. Otherwise return NULL. TYPE is the desired
6647 result type. */
6649 static tree
6650 fold_single_bit_test_into_sign_test (location_t loc,
6651 enum tree_code code, tree arg0, tree arg1,
6652 tree result_type)
6654 /* If this is testing a single bit, we can optimize the test. */
6655 if ((code == NE_EXPR || code == EQ_EXPR)
6656 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6657 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6659 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6660 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6661 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6663 if (arg00 != NULL_TREE
6664 /* This is only a win if casting to a signed type is cheap,
6665 i.e. when arg00's type is not a partial mode. */
6666 && TYPE_PRECISION (TREE_TYPE (arg00))
6667 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6669 tree stype = signed_type_for (TREE_TYPE (arg00));
6670 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6671 result_type,
6672 fold_convert_loc (loc, stype, arg00),
6673 build_int_cst (stype, 0));
6677 return NULL_TREE;
6680 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6681 equality/inequality test, then return a simplified form of
6682 the test using shifts and logical operations. Otherwise return
6683 NULL. TYPE is the desired result type. */
6685 tree
6686 fold_single_bit_test (location_t loc, enum tree_code code,
6687 tree arg0, tree arg1, tree result_type)
6689 /* If this is testing a single bit, we can optimize the test. */
6690 if ((code == NE_EXPR || code == EQ_EXPR)
6691 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6692 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6694 tree inner = TREE_OPERAND (arg0, 0);
6695 tree type = TREE_TYPE (arg0);
6696 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6697 machine_mode operand_mode = TYPE_MODE (type);
6698 int ops_unsigned;
6699 tree signed_type, unsigned_type, intermediate_type;
6700 tree tem, one;
6702 /* First, see if we can fold the single bit test into a sign-bit
6703 test. */
6704 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6705 result_type);
6706 if (tem)
6707 return tem;
6709 /* Otherwise we have (A & C) != 0 where C is a single bit,
6710 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6711 Similarly for (A & C) == 0. */
6713 /* If INNER is a right shift of a constant and it plus BITNUM does
6714 not overflow, adjust BITNUM and INNER. */
6715 if (TREE_CODE (inner) == RSHIFT_EXPR
6716 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6717 && bitnum < TYPE_PRECISION (type)
6718 && wi::ltu_p (TREE_OPERAND (inner, 1),
6719 TYPE_PRECISION (type) - bitnum))
6721 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6722 inner = TREE_OPERAND (inner, 0);
6725 /* If we are going to be able to omit the AND below, we must do our
6726 operations as unsigned. If we must use the AND, we have a choice.
6727 Normally unsigned is faster, but for some machines signed is. */
6728 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6729 && !flag_syntax_only) ? 0 : 1;
6731 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6732 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6733 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6734 inner = fold_convert_loc (loc, intermediate_type, inner);
6736 if (bitnum != 0)
6737 inner = build2 (RSHIFT_EXPR, intermediate_type,
6738 inner, size_int (bitnum));
6740 one = build_int_cst (intermediate_type, 1);
6742 if (code == EQ_EXPR)
6743 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6745 /* Put the AND last so it can combine with more things. */
6746 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6748 /* Make sure to return the proper type. */
6749 inner = fold_convert_loc (loc, result_type, inner);
6751 return inner;
6753 return NULL_TREE;
6756 /* Test whether it is preferable two swap two operands, ARG0 and
6757 ARG1, for example because ARG0 is an integer constant and ARG1
6758 isn't. */
6760 bool
6761 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6763 if (CONSTANT_CLASS_P (arg1))
6764 return 0;
6765 if (CONSTANT_CLASS_P (arg0))
6766 return 1;
6768 STRIP_NOPS (arg0);
6769 STRIP_NOPS (arg1);
6771 if (TREE_CONSTANT (arg1))
6772 return 0;
6773 if (TREE_CONSTANT (arg0))
6774 return 1;
6776 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6777 for commutative and comparison operators. Ensuring a canonical
6778 form allows the optimizers to find additional redundancies without
6779 having to explicitly check for both orderings. */
6780 if (TREE_CODE (arg0) == SSA_NAME
6781 && TREE_CODE (arg1) == SSA_NAME
6782 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6783 return 1;
6785 /* Put SSA_NAMEs last. */
6786 if (TREE_CODE (arg1) == SSA_NAME)
6787 return 0;
6788 if (TREE_CODE (arg0) == SSA_NAME)
6789 return 1;
6791 /* Put variables last. */
6792 if (DECL_P (arg1))
6793 return 0;
6794 if (DECL_P (arg0))
6795 return 1;
6797 return 0;
6801 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6802 means A >= Y && A != MAX, but in this case we know that
6803 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6805 static tree
6806 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6808 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6810 if (TREE_CODE (bound) == LT_EXPR)
6811 a = TREE_OPERAND (bound, 0);
6812 else if (TREE_CODE (bound) == GT_EXPR)
6813 a = TREE_OPERAND (bound, 1);
6814 else
6815 return NULL_TREE;
6817 typea = TREE_TYPE (a);
6818 if (!INTEGRAL_TYPE_P (typea)
6819 && !POINTER_TYPE_P (typea))
6820 return NULL_TREE;
6822 if (TREE_CODE (ineq) == LT_EXPR)
6824 a1 = TREE_OPERAND (ineq, 1);
6825 y = TREE_OPERAND (ineq, 0);
6827 else if (TREE_CODE (ineq) == GT_EXPR)
6829 a1 = TREE_OPERAND (ineq, 0);
6830 y = TREE_OPERAND (ineq, 1);
6832 else
6833 return NULL_TREE;
6835 if (TREE_TYPE (a1) != typea)
6836 return NULL_TREE;
6838 if (POINTER_TYPE_P (typea))
6840 /* Convert the pointer types into integer before taking the difference. */
6841 tree ta = fold_convert_loc (loc, ssizetype, a);
6842 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6843 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6845 else
6846 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6848 if (!diff || !integer_onep (diff))
6849 return NULL_TREE;
6851 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6854 /* Fold a sum or difference of at least one multiplication.
6855 Returns the folded tree or NULL if no simplification could be made. */
6857 static tree
6858 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6859 tree arg0, tree arg1)
6861 tree arg00, arg01, arg10, arg11;
6862 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6864 /* (A * C) +- (B * C) -> (A+-B) * C.
6865 (A * C) +- A -> A * (C+-1).
6866 We are most concerned about the case where C is a constant,
6867 but other combinations show up during loop reduction. Since
6868 it is not difficult, try all four possibilities. */
6870 if (TREE_CODE (arg0) == MULT_EXPR)
6872 arg00 = TREE_OPERAND (arg0, 0);
6873 arg01 = TREE_OPERAND (arg0, 1);
6875 else if (TREE_CODE (arg0) == INTEGER_CST)
6877 arg00 = build_one_cst (type);
6878 arg01 = arg0;
6880 else
6882 /* We cannot generate constant 1 for fract. */
6883 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6884 return NULL_TREE;
6885 arg00 = arg0;
6886 arg01 = build_one_cst (type);
6888 if (TREE_CODE (arg1) == MULT_EXPR)
6890 arg10 = TREE_OPERAND (arg1, 0);
6891 arg11 = TREE_OPERAND (arg1, 1);
6893 else if (TREE_CODE (arg1) == INTEGER_CST)
6895 arg10 = build_one_cst (type);
6896 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6897 the purpose of this canonicalization. */
6898 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6899 && negate_expr_p (arg1)
6900 && code == PLUS_EXPR)
6902 arg11 = negate_expr (arg1);
6903 code = MINUS_EXPR;
6905 else
6906 arg11 = arg1;
6908 else
6910 /* We cannot generate constant 1 for fract. */
6911 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6912 return NULL_TREE;
6913 arg10 = arg1;
6914 arg11 = build_one_cst (type);
6916 same = NULL_TREE;
6918 if (operand_equal_p (arg01, arg11, 0))
6919 same = arg01, alt0 = arg00, alt1 = arg10;
6920 else if (operand_equal_p (arg00, arg10, 0))
6921 same = arg00, alt0 = arg01, alt1 = arg11;
6922 else if (operand_equal_p (arg00, arg11, 0))
6923 same = arg00, alt0 = arg01, alt1 = arg10;
6924 else if (operand_equal_p (arg01, arg10, 0))
6925 same = arg01, alt0 = arg00, alt1 = arg11;
6927 /* No identical multiplicands; see if we can find a common
6928 power-of-two factor in non-power-of-two multiplies. This
6929 can help in multi-dimensional array access. */
6930 else if (tree_fits_shwi_p (arg01)
6931 && tree_fits_shwi_p (arg11))
6933 HOST_WIDE_INT int01, int11, tmp;
6934 bool swap = false;
6935 tree maybe_same;
6936 int01 = tree_to_shwi (arg01);
6937 int11 = tree_to_shwi (arg11);
6939 /* Move min of absolute values to int11. */
6940 if (absu_hwi (int01) < absu_hwi (int11))
6942 tmp = int01, int01 = int11, int11 = tmp;
6943 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6944 maybe_same = arg01;
6945 swap = true;
6947 else
6948 maybe_same = arg11;
6950 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6951 /* The remainder should not be a constant, otherwise we
6952 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6953 increased the number of multiplications necessary. */
6954 && TREE_CODE (arg10) != INTEGER_CST)
6956 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6957 build_int_cst (TREE_TYPE (arg00),
6958 int01 / int11));
6959 alt1 = arg10;
6960 same = maybe_same;
6961 if (swap)
6962 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6966 if (same)
6967 return fold_build2_loc (loc, MULT_EXPR, type,
6968 fold_build2_loc (loc, code, type,
6969 fold_convert_loc (loc, type, alt0),
6970 fold_convert_loc (loc, type, alt1)),
6971 fold_convert_loc (loc, type, same));
6973 return NULL_TREE;
6976 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6977 specified by EXPR into the buffer PTR of length LEN bytes.
6978 Return the number of bytes placed in the buffer, or zero
6979 upon failure. */
6981 static int
6982 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6984 tree type = TREE_TYPE (expr);
6985 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6986 int byte, offset, word, words;
6987 unsigned char value;
6989 if ((off == -1 && total_bytes > len)
6990 || off >= total_bytes)
6991 return 0;
6992 if (off == -1)
6993 off = 0;
6994 words = total_bytes / UNITS_PER_WORD;
6996 for (byte = 0; byte < total_bytes; byte++)
6998 int bitpos = byte * BITS_PER_UNIT;
6999 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7000 number of bytes. */
7001 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7003 if (total_bytes > UNITS_PER_WORD)
7005 word = byte / UNITS_PER_WORD;
7006 if (WORDS_BIG_ENDIAN)
7007 word = (words - 1) - word;
7008 offset = word * UNITS_PER_WORD;
7009 if (BYTES_BIG_ENDIAN)
7010 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7011 else
7012 offset += byte % UNITS_PER_WORD;
7014 else
7015 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7016 if (offset >= off
7017 && offset - off < len)
7018 ptr[offset - off] = value;
7020 return MIN (len, total_bytes - off);
7024 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7025 specified by EXPR into the buffer PTR of length LEN bytes.
7026 Return the number of bytes placed in the buffer, or zero
7027 upon failure. */
7029 static int
7030 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7032 tree type = TREE_TYPE (expr);
7033 machine_mode mode = TYPE_MODE (type);
7034 int total_bytes = GET_MODE_SIZE (mode);
7035 FIXED_VALUE_TYPE value;
7036 tree i_value, i_type;
7038 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7039 return 0;
7041 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7043 if (NULL_TREE == i_type
7044 || TYPE_PRECISION (i_type) != total_bytes)
7045 return 0;
7047 value = TREE_FIXED_CST (expr);
7048 i_value = double_int_to_tree (i_type, value.data);
7050 return native_encode_int (i_value, ptr, len, off);
7054 /* Subroutine of native_encode_expr. Encode the REAL_CST
7055 specified by EXPR into the buffer PTR of length LEN bytes.
7056 Return the number of bytes placed in the buffer, or zero
7057 upon failure. */
7059 static int
7060 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7062 tree type = TREE_TYPE (expr);
7063 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7064 int byte, offset, word, words, bitpos;
7065 unsigned char value;
7067 /* There are always 32 bits in each long, no matter the size of
7068 the hosts long. We handle floating point representations with
7069 up to 192 bits. */
7070 long tmp[6];
7072 if ((off == -1 && total_bytes > len)
7073 || off >= total_bytes)
7074 return 0;
7075 if (off == -1)
7076 off = 0;
7077 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7079 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7081 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7082 bitpos += BITS_PER_UNIT)
7084 byte = (bitpos / BITS_PER_UNIT) & 3;
7085 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7087 if (UNITS_PER_WORD < 4)
7089 word = byte / UNITS_PER_WORD;
7090 if (WORDS_BIG_ENDIAN)
7091 word = (words - 1) - word;
7092 offset = word * UNITS_PER_WORD;
7093 if (BYTES_BIG_ENDIAN)
7094 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7095 else
7096 offset += byte % UNITS_PER_WORD;
7098 else
7100 offset = byte;
7101 if (BYTES_BIG_ENDIAN)
7103 /* Reverse bytes within each long, or within the entire float
7104 if it's smaller than a long (for HFmode). */
7105 offset = MIN (3, total_bytes - 1) - offset;
7106 gcc_assert (offset >= 0);
7109 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7110 if (offset >= off
7111 && offset - off < len)
7112 ptr[offset - off] = value;
7114 return MIN (len, total_bytes - off);
7117 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7118 specified by EXPR into the buffer PTR of length LEN bytes.
7119 Return the number of bytes placed in the buffer, or zero
7120 upon failure. */
7122 static int
7123 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7125 int rsize, isize;
7126 tree part;
7128 part = TREE_REALPART (expr);
7129 rsize = native_encode_expr (part, ptr, len, off);
7130 if (off == -1
7131 && rsize == 0)
7132 return 0;
7133 part = TREE_IMAGPART (expr);
7134 if (off != -1)
7135 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7136 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7137 if (off == -1
7138 && isize != rsize)
7139 return 0;
7140 return rsize + isize;
7144 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7145 specified by EXPR into the buffer PTR of length LEN bytes.
7146 Return the number of bytes placed in the buffer, or zero
7147 upon failure. */
7149 static int
7150 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7152 unsigned i, count;
7153 int size, offset;
7154 tree itype, elem;
7156 offset = 0;
7157 count = VECTOR_CST_NELTS (expr);
7158 itype = TREE_TYPE (TREE_TYPE (expr));
7159 size = GET_MODE_SIZE (TYPE_MODE (itype));
7160 for (i = 0; i < count; i++)
7162 if (off >= size)
7164 off -= size;
7165 continue;
7167 elem = VECTOR_CST_ELT (expr, i);
7168 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7169 if ((off == -1 && res != size)
7170 || res == 0)
7171 return 0;
7172 offset += res;
7173 if (offset >= len)
7174 return offset;
7175 if (off != -1)
7176 off = 0;
7178 return offset;
7182 /* Subroutine of native_encode_expr. Encode the STRING_CST
7183 specified by EXPR into the buffer PTR of length LEN bytes.
7184 Return the number of bytes placed in the buffer, or zero
7185 upon failure. */
7187 static int
7188 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7190 tree type = TREE_TYPE (expr);
7191 HOST_WIDE_INT total_bytes;
7193 if (TREE_CODE (type) != ARRAY_TYPE
7194 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7195 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7196 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7197 return 0;
7198 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7199 if ((off == -1 && total_bytes > len)
7200 || off >= total_bytes)
7201 return 0;
7202 if (off == -1)
7203 off = 0;
7204 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7206 int written = 0;
7207 if (off < TREE_STRING_LENGTH (expr))
7209 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7210 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7212 memset (ptr + written, 0,
7213 MIN (total_bytes - written, len - written));
7215 else
7216 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7217 return MIN (total_bytes - off, len);
7221 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7222 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7223 buffer PTR of length LEN bytes. If OFF is not -1 then start
7224 the encoding at byte offset OFF and encode at most LEN bytes.
7225 Return the number of bytes placed in the buffer, or zero upon failure. */
7228 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7230 /* We don't support starting at negative offset and -1 is special. */
7231 if (off < -1)
7232 return 0;
7234 switch (TREE_CODE (expr))
7236 case INTEGER_CST:
7237 return native_encode_int (expr, ptr, len, off);
7239 case REAL_CST:
7240 return native_encode_real (expr, ptr, len, off);
7242 case FIXED_CST:
7243 return native_encode_fixed (expr, ptr, len, off);
7245 case COMPLEX_CST:
7246 return native_encode_complex (expr, ptr, len, off);
7248 case VECTOR_CST:
7249 return native_encode_vector (expr, ptr, len, off);
7251 case STRING_CST:
7252 return native_encode_string (expr, ptr, len, off);
7254 default:
7255 return 0;
7260 /* Subroutine of native_interpret_expr. Interpret the contents of
7261 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7262 If the buffer cannot be interpreted, return NULL_TREE. */
7264 static tree
7265 native_interpret_int (tree type, const unsigned char *ptr, int len)
7267 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7269 if (total_bytes > len
7270 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7271 return NULL_TREE;
7273 wide_int result = wi::from_buffer (ptr, total_bytes);
7275 return wide_int_to_tree (type, result);
7279 /* Subroutine of native_interpret_expr. Interpret the contents of
7280 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7281 If the buffer cannot be interpreted, return NULL_TREE. */
7283 static tree
7284 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7286 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7287 double_int result;
7288 FIXED_VALUE_TYPE fixed_value;
7290 if (total_bytes > len
7291 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7292 return NULL_TREE;
7294 result = double_int::from_buffer (ptr, total_bytes);
7295 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7297 return build_fixed (type, fixed_value);
7301 /* Subroutine of native_interpret_expr. Interpret the contents of
7302 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7303 If the buffer cannot be interpreted, return NULL_TREE. */
7305 static tree
7306 native_interpret_real (tree type, const unsigned char *ptr, int len)
7308 machine_mode mode = TYPE_MODE (type);
7309 int total_bytes = GET_MODE_SIZE (mode);
7310 unsigned char value;
7311 /* There are always 32 bits in each long, no matter the size of
7312 the hosts long. We handle floating point representations with
7313 up to 192 bits. */
7314 REAL_VALUE_TYPE r;
7315 long tmp[6];
7317 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7318 if (total_bytes > len || total_bytes > 24)
7319 return NULL_TREE;
7320 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7322 memset (tmp, 0, sizeof (tmp));
7323 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7324 bitpos += BITS_PER_UNIT)
7326 /* Both OFFSET and BYTE index within a long;
7327 bitpos indexes the whole float. */
7328 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7329 if (UNITS_PER_WORD < 4)
7331 int word = byte / UNITS_PER_WORD;
7332 if (WORDS_BIG_ENDIAN)
7333 word = (words - 1) - word;
7334 offset = word * UNITS_PER_WORD;
7335 if (BYTES_BIG_ENDIAN)
7336 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7337 else
7338 offset += byte % UNITS_PER_WORD;
7340 else
7342 offset = byte;
7343 if (BYTES_BIG_ENDIAN)
7345 /* Reverse bytes within each long, or within the entire float
7346 if it's smaller than a long (for HFmode). */
7347 offset = MIN (3, total_bytes - 1) - offset;
7348 gcc_assert (offset >= 0);
7351 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7353 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7356 real_from_target (&r, tmp, mode);
7357 return build_real (type, r);
7361 /* Subroutine of native_interpret_expr. Interpret the contents of
7362 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7363 If the buffer cannot be interpreted, return NULL_TREE. */
7365 static tree
7366 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7368 tree etype, rpart, ipart;
7369 int size;
7371 etype = TREE_TYPE (type);
7372 size = GET_MODE_SIZE (TYPE_MODE (etype));
7373 if (size * 2 > len)
7374 return NULL_TREE;
7375 rpart = native_interpret_expr (etype, ptr, size);
7376 if (!rpart)
7377 return NULL_TREE;
7378 ipart = native_interpret_expr (etype, ptr+size, size);
7379 if (!ipart)
7380 return NULL_TREE;
7381 return build_complex (type, rpart, ipart);
7385 /* Subroutine of native_interpret_expr. Interpret the contents of
7386 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7387 If the buffer cannot be interpreted, return NULL_TREE. */
7389 static tree
7390 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7392 tree etype, elem;
7393 int i, size, count;
7394 tree *elements;
7396 etype = TREE_TYPE (type);
7397 size = GET_MODE_SIZE (TYPE_MODE (etype));
7398 count = TYPE_VECTOR_SUBPARTS (type);
7399 if (size * count > len)
7400 return NULL_TREE;
7402 elements = XALLOCAVEC (tree, count);
7403 for (i = count - 1; i >= 0; i--)
7405 elem = native_interpret_expr (etype, ptr+(i*size), size);
7406 if (!elem)
7407 return NULL_TREE;
7408 elements[i] = elem;
7410 return build_vector (type, elements);
7414 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7415 the buffer PTR of length LEN as a constant of type TYPE. For
7416 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7417 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7418 return NULL_TREE. */
7420 tree
7421 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7423 switch (TREE_CODE (type))
7425 case INTEGER_TYPE:
7426 case ENUMERAL_TYPE:
7427 case BOOLEAN_TYPE:
7428 case POINTER_TYPE:
7429 case REFERENCE_TYPE:
7430 return native_interpret_int (type, ptr, len);
7432 case REAL_TYPE:
7433 return native_interpret_real (type, ptr, len);
7435 case FIXED_POINT_TYPE:
7436 return native_interpret_fixed (type, ptr, len);
7438 case COMPLEX_TYPE:
7439 return native_interpret_complex (type, ptr, len);
7441 case VECTOR_TYPE:
7442 return native_interpret_vector (type, ptr, len);
7444 default:
7445 return NULL_TREE;
7449 /* Returns true if we can interpret the contents of a native encoding
7450 as TYPE. */
7452 static bool
7453 can_native_interpret_type_p (tree type)
7455 switch (TREE_CODE (type))
7457 case INTEGER_TYPE:
7458 case ENUMERAL_TYPE:
7459 case BOOLEAN_TYPE:
7460 case POINTER_TYPE:
7461 case REFERENCE_TYPE:
7462 case FIXED_POINT_TYPE:
7463 case REAL_TYPE:
7464 case COMPLEX_TYPE:
7465 case VECTOR_TYPE:
7466 return true;
7467 default:
7468 return false;
7472 /* Return true iff a constant of type TYPE is accepted by
7473 native_encode_expr. */
7475 bool
7476 can_native_encode_type_p (tree type)
7478 switch (TREE_CODE (type))
7480 case INTEGER_TYPE:
7481 case REAL_TYPE:
7482 case FIXED_POINT_TYPE:
7483 case COMPLEX_TYPE:
7484 case VECTOR_TYPE:
7485 case POINTER_TYPE:
7486 return true;
7487 default:
7488 return false;
7492 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7493 TYPE at compile-time. If we're unable to perform the conversion
7494 return NULL_TREE. */
7496 static tree
7497 fold_view_convert_expr (tree type, tree expr)
7499 /* We support up to 512-bit values (for V8DFmode). */
7500 unsigned char buffer[64];
7501 int len;
7503 /* Check that the host and target are sane. */
7504 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7505 return NULL_TREE;
7507 len = native_encode_expr (expr, buffer, sizeof (buffer));
7508 if (len == 0)
7509 return NULL_TREE;
7511 return native_interpret_expr (type, buffer, len);
7514 /* Build an expression for the address of T. Folds away INDIRECT_REF
7515 to avoid confusing the gimplify process. */
7517 tree
7518 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7520 /* The size of the object is not relevant when talking about its address. */
7521 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7522 t = TREE_OPERAND (t, 0);
7524 if (TREE_CODE (t) == INDIRECT_REF)
7526 t = TREE_OPERAND (t, 0);
7528 if (TREE_TYPE (t) != ptrtype)
7529 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7531 else if (TREE_CODE (t) == MEM_REF
7532 && integer_zerop (TREE_OPERAND (t, 1)))
7533 return TREE_OPERAND (t, 0);
7534 else if (TREE_CODE (t) == MEM_REF
7535 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7536 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7537 TREE_OPERAND (t, 0),
7538 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7539 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7541 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7543 if (TREE_TYPE (t) != ptrtype)
7544 t = fold_convert_loc (loc, ptrtype, t);
7546 else
7547 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7549 return t;
7552 /* Build an expression for the address of T. */
7554 tree
7555 build_fold_addr_expr_loc (location_t loc, tree t)
7557 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7559 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7562 /* Fold a unary expression of code CODE and type TYPE with operand
7563 OP0. Return the folded expression if folding is successful.
7564 Otherwise, return NULL_TREE. */
7566 tree
7567 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7569 tree tem;
7570 tree arg0;
7571 enum tree_code_class kind = TREE_CODE_CLASS (code);
7573 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7574 && TREE_CODE_LENGTH (code) == 1);
7576 arg0 = op0;
7577 if (arg0)
7579 if (CONVERT_EXPR_CODE_P (code)
7580 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7582 /* Don't use STRIP_NOPS, because signedness of argument type
7583 matters. */
7584 STRIP_SIGN_NOPS (arg0);
7586 else
7588 /* Strip any conversions that don't change the mode. This
7589 is safe for every expression, except for a comparison
7590 expression because its signedness is derived from its
7591 operands.
7593 Note that this is done as an internal manipulation within
7594 the constant folder, in order to find the simplest
7595 representation of the arguments so that their form can be
7596 studied. In any cases, the appropriate type conversions
7597 should be put back in the tree that will get out of the
7598 constant folder. */
7599 STRIP_NOPS (arg0);
7602 if (CONSTANT_CLASS_P (arg0))
7604 tree tem = const_unop (code, type, arg0);
7605 if (tem)
7607 if (TREE_TYPE (tem) != type)
7608 tem = fold_convert_loc (loc, type, tem);
7609 return tem;
7614 tem = generic_simplify (loc, code, type, op0);
7615 if (tem)
7616 return tem;
7618 if (TREE_CODE_CLASS (code) == tcc_unary)
7620 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7621 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7622 fold_build1_loc (loc, code, type,
7623 fold_convert_loc (loc, TREE_TYPE (op0),
7624 TREE_OPERAND (arg0, 1))));
7625 else if (TREE_CODE (arg0) == COND_EXPR)
7627 tree arg01 = TREE_OPERAND (arg0, 1);
7628 tree arg02 = TREE_OPERAND (arg0, 2);
7629 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7630 arg01 = fold_build1_loc (loc, code, type,
7631 fold_convert_loc (loc,
7632 TREE_TYPE (op0), arg01));
7633 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7634 arg02 = fold_build1_loc (loc, code, type,
7635 fold_convert_loc (loc,
7636 TREE_TYPE (op0), arg02));
7637 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7638 arg01, arg02);
7640 /* If this was a conversion, and all we did was to move into
7641 inside the COND_EXPR, bring it back out. But leave it if
7642 it is a conversion from integer to integer and the
7643 result precision is no wider than a word since such a
7644 conversion is cheap and may be optimized away by combine,
7645 while it couldn't if it were outside the COND_EXPR. Then return
7646 so we don't get into an infinite recursion loop taking the
7647 conversion out and then back in. */
7649 if ((CONVERT_EXPR_CODE_P (code)
7650 || code == NON_LVALUE_EXPR)
7651 && TREE_CODE (tem) == COND_EXPR
7652 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7653 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7654 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7655 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7656 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7657 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7658 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7659 && (INTEGRAL_TYPE_P
7660 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7661 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7662 || flag_syntax_only))
7663 tem = build1_loc (loc, code, type,
7664 build3 (COND_EXPR,
7665 TREE_TYPE (TREE_OPERAND
7666 (TREE_OPERAND (tem, 1), 0)),
7667 TREE_OPERAND (tem, 0),
7668 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7669 TREE_OPERAND (TREE_OPERAND (tem, 2),
7670 0)));
7671 return tem;
7675 switch (code)
7677 case NON_LVALUE_EXPR:
7678 if (!maybe_lvalue_p (op0))
7679 return fold_convert_loc (loc, type, op0);
7680 return NULL_TREE;
7682 CASE_CONVERT:
7683 case FLOAT_EXPR:
7684 case FIX_TRUNC_EXPR:
7685 if (COMPARISON_CLASS_P (op0))
7687 /* If we have (type) (a CMP b) and type is an integral type, return
7688 new expression involving the new type. Canonicalize
7689 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7690 non-integral type.
7691 Do not fold the result as that would not simplify further, also
7692 folding again results in recursions. */
7693 if (TREE_CODE (type) == BOOLEAN_TYPE)
7694 return build2_loc (loc, TREE_CODE (op0), type,
7695 TREE_OPERAND (op0, 0),
7696 TREE_OPERAND (op0, 1));
7697 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7698 && TREE_CODE (type) != VECTOR_TYPE)
7699 return build3_loc (loc, COND_EXPR, type, op0,
7700 constant_boolean_node (true, type),
7701 constant_boolean_node (false, type));
7704 /* Handle (T *)&A.B.C for A being of type T and B and C
7705 living at offset zero. This occurs frequently in
7706 C++ upcasting and then accessing the base. */
7707 if (TREE_CODE (op0) == ADDR_EXPR
7708 && POINTER_TYPE_P (type)
7709 && handled_component_p (TREE_OPERAND (op0, 0)))
7711 HOST_WIDE_INT bitsize, bitpos;
7712 tree offset;
7713 machine_mode mode;
7714 int unsignedp, reversep, volatilep;
7715 tree base
7716 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7717 &offset, &mode, &unsignedp, &reversep,
7718 &volatilep);
7719 /* If the reference was to a (constant) zero offset, we can use
7720 the address of the base if it has the same base type
7721 as the result type and the pointer type is unqualified. */
7722 if (! offset && bitpos == 0
7723 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7724 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7725 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7726 return fold_convert_loc (loc, type,
7727 build_fold_addr_expr_loc (loc, base));
7730 if (TREE_CODE (op0) == MODIFY_EXPR
7731 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7732 /* Detect assigning a bitfield. */
7733 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7734 && DECL_BIT_FIELD
7735 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7737 /* Don't leave an assignment inside a conversion
7738 unless assigning a bitfield. */
7739 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7740 /* First do the assignment, then return converted constant. */
7741 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7742 TREE_NO_WARNING (tem) = 1;
7743 TREE_USED (tem) = 1;
7744 return tem;
7747 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7748 constants (if x has signed type, the sign bit cannot be set
7749 in c). This folds extension into the BIT_AND_EXPR.
7750 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7751 very likely don't have maximal range for their precision and this
7752 transformation effectively doesn't preserve non-maximal ranges. */
7753 if (TREE_CODE (type) == INTEGER_TYPE
7754 && TREE_CODE (op0) == BIT_AND_EXPR
7755 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7757 tree and_expr = op0;
7758 tree and0 = TREE_OPERAND (and_expr, 0);
7759 tree and1 = TREE_OPERAND (and_expr, 1);
7760 int change = 0;
7762 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7763 || (TYPE_PRECISION (type)
7764 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7765 change = 1;
7766 else if (TYPE_PRECISION (TREE_TYPE (and1))
7767 <= HOST_BITS_PER_WIDE_INT
7768 && tree_fits_uhwi_p (and1))
7770 unsigned HOST_WIDE_INT cst;
7772 cst = tree_to_uhwi (and1);
7773 cst &= HOST_WIDE_INT_M1U
7774 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7775 change = (cst == 0);
7776 if (change
7777 && !flag_syntax_only
7778 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7779 == ZERO_EXTEND))
7781 tree uns = unsigned_type_for (TREE_TYPE (and0));
7782 and0 = fold_convert_loc (loc, uns, and0);
7783 and1 = fold_convert_loc (loc, uns, and1);
7786 if (change)
7788 tem = force_fit_type (type, wi::to_widest (and1), 0,
7789 TREE_OVERFLOW (and1));
7790 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7791 fold_convert_loc (loc, type, and0), tem);
7795 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7796 cast (T1)X will fold away. We assume that this happens when X itself
7797 is a cast. */
7798 if (POINTER_TYPE_P (type)
7799 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7800 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7802 tree arg00 = TREE_OPERAND (arg0, 0);
7803 tree arg01 = TREE_OPERAND (arg0, 1);
7805 return fold_build_pointer_plus_loc
7806 (loc, fold_convert_loc (loc, type, arg00), arg01);
7809 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7810 of the same precision, and X is an integer type not narrower than
7811 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7812 if (INTEGRAL_TYPE_P (type)
7813 && TREE_CODE (op0) == BIT_NOT_EXPR
7814 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7815 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7816 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7818 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7819 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7820 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7821 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7822 fold_convert_loc (loc, type, tem));
7825 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7826 type of X and Y (integer types only). */
7827 if (INTEGRAL_TYPE_P (type)
7828 && TREE_CODE (op0) == MULT_EXPR
7829 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7830 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7832 /* Be careful not to introduce new overflows. */
7833 tree mult_type;
7834 if (TYPE_OVERFLOW_WRAPS (type))
7835 mult_type = type;
7836 else
7837 mult_type = unsigned_type_for (type);
7839 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7841 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7842 fold_convert_loc (loc, mult_type,
7843 TREE_OPERAND (op0, 0)),
7844 fold_convert_loc (loc, mult_type,
7845 TREE_OPERAND (op0, 1)));
7846 return fold_convert_loc (loc, type, tem);
7850 return NULL_TREE;
7852 case VIEW_CONVERT_EXPR:
7853 if (TREE_CODE (op0) == MEM_REF)
7855 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7856 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7857 tem = fold_build2_loc (loc, MEM_REF, type,
7858 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7859 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7860 return tem;
7863 return NULL_TREE;
7865 case NEGATE_EXPR:
7866 tem = fold_negate_expr (loc, arg0);
7867 if (tem)
7868 return fold_convert_loc (loc, type, tem);
7869 return NULL_TREE;
7871 case ABS_EXPR:
7872 /* Convert fabs((double)float) into (double)fabsf(float). */
7873 if (TREE_CODE (arg0) == NOP_EXPR
7874 && TREE_CODE (type) == REAL_TYPE)
7876 tree targ0 = strip_float_extensions (arg0);
7877 if (targ0 != arg0)
7878 return fold_convert_loc (loc, type,
7879 fold_build1_loc (loc, ABS_EXPR,
7880 TREE_TYPE (targ0),
7881 targ0));
7883 return NULL_TREE;
7885 case BIT_NOT_EXPR:
7886 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7887 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7888 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7889 fold_convert_loc (loc, type,
7890 TREE_OPERAND (arg0, 0)))))
7891 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7892 fold_convert_loc (loc, type,
7893 TREE_OPERAND (arg0, 1)));
7894 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7895 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7896 fold_convert_loc (loc, type,
7897 TREE_OPERAND (arg0, 1)))))
7898 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7899 fold_convert_loc (loc, type,
7900 TREE_OPERAND (arg0, 0)), tem);
7902 return NULL_TREE;
7904 case TRUTH_NOT_EXPR:
7905 /* Note that the operand of this must be an int
7906 and its values must be 0 or 1.
7907 ("true" is a fixed value perhaps depending on the language,
7908 but we don't handle values other than 1 correctly yet.) */
7909 tem = fold_truth_not_expr (loc, arg0);
7910 if (!tem)
7911 return NULL_TREE;
7912 return fold_convert_loc (loc, type, tem);
7914 case INDIRECT_REF:
7915 /* Fold *&X to X if X is an lvalue. */
7916 if (TREE_CODE (op0) == ADDR_EXPR)
7918 tree op00 = TREE_OPERAND (op0, 0);
7919 if ((VAR_P (op00)
7920 || TREE_CODE (op00) == PARM_DECL
7921 || TREE_CODE (op00) == RESULT_DECL)
7922 && !TREE_READONLY (op00))
7923 return op00;
7925 return NULL_TREE;
7927 default:
7928 return NULL_TREE;
7929 } /* switch (code) */
7933 /* If the operation was a conversion do _not_ mark a resulting constant
7934 with TREE_OVERFLOW if the original constant was not. These conversions
7935 have implementation defined behavior and retaining the TREE_OVERFLOW
7936 flag here would confuse later passes such as VRP. */
7937 tree
7938 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7939 tree type, tree op0)
7941 tree res = fold_unary_loc (loc, code, type, op0);
7942 if (res
7943 && TREE_CODE (res) == INTEGER_CST
7944 && TREE_CODE (op0) == INTEGER_CST
7945 && CONVERT_EXPR_CODE_P (code))
7946 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7948 return res;
7951 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7952 operands OP0 and OP1. LOC is the location of the resulting expression.
7953 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7954 Return the folded expression if folding is successful. Otherwise,
7955 return NULL_TREE. */
7956 static tree
7957 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7958 tree arg0, tree arg1, tree op0, tree op1)
7960 tree tem;
7962 /* We only do these simplifications if we are optimizing. */
7963 if (!optimize)
7964 return NULL_TREE;
7966 /* Check for things like (A || B) && (A || C). We can convert this
7967 to A || (B && C). Note that either operator can be any of the four
7968 truth and/or operations and the transformation will still be
7969 valid. Also note that we only care about order for the
7970 ANDIF and ORIF operators. If B contains side effects, this
7971 might change the truth-value of A. */
7972 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7973 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7974 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7975 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7976 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7977 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7979 tree a00 = TREE_OPERAND (arg0, 0);
7980 tree a01 = TREE_OPERAND (arg0, 1);
7981 tree a10 = TREE_OPERAND (arg1, 0);
7982 tree a11 = TREE_OPERAND (arg1, 1);
7983 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7984 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7985 && (code == TRUTH_AND_EXPR
7986 || code == TRUTH_OR_EXPR));
7988 if (operand_equal_p (a00, a10, 0))
7989 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7990 fold_build2_loc (loc, code, type, a01, a11));
7991 else if (commutative && operand_equal_p (a00, a11, 0))
7992 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7993 fold_build2_loc (loc, code, type, a01, a10));
7994 else if (commutative && operand_equal_p (a01, a10, 0))
7995 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
7996 fold_build2_loc (loc, code, type, a00, a11));
7998 /* This case if tricky because we must either have commutative
7999 operators or else A10 must not have side-effects. */
8001 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8002 && operand_equal_p (a01, a11, 0))
8003 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8004 fold_build2_loc (loc, code, type, a00, a10),
8005 a01);
8008 /* See if we can build a range comparison. */
8009 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8010 return tem;
8012 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8013 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8015 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8016 if (tem)
8017 return fold_build2_loc (loc, code, type, tem, arg1);
8020 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8021 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8023 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8024 if (tem)
8025 return fold_build2_loc (loc, code, type, arg0, tem);
8028 /* Check for the possibility of merging component references. If our
8029 lhs is another similar operation, try to merge its rhs with our
8030 rhs. Then try to merge our lhs and rhs. */
8031 if (TREE_CODE (arg0) == code
8032 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8033 TREE_OPERAND (arg0, 1), arg1)))
8034 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8036 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8037 return tem;
8039 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8040 && (code == TRUTH_AND_EXPR
8041 || code == TRUTH_ANDIF_EXPR
8042 || code == TRUTH_OR_EXPR
8043 || code == TRUTH_ORIF_EXPR))
8045 enum tree_code ncode, icode;
8047 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8048 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8049 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8051 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8052 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8053 We don't want to pack more than two leafs to a non-IF AND/OR
8054 expression.
8055 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8056 equal to IF-CODE, then we don't want to add right-hand operand.
8057 If the inner right-hand side of left-hand operand has
8058 side-effects, or isn't simple, then we can't add to it,
8059 as otherwise we might destroy if-sequence. */
8060 if (TREE_CODE (arg0) == icode
8061 && simple_operand_p_2 (arg1)
8062 /* Needed for sequence points to handle trappings, and
8063 side-effects. */
8064 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8066 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8067 arg1);
8068 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8069 tem);
8071 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8072 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8073 else if (TREE_CODE (arg1) == icode
8074 && simple_operand_p_2 (arg0)
8075 /* Needed for sequence points to handle trappings, and
8076 side-effects. */
8077 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8079 tem = fold_build2_loc (loc, ncode, type,
8080 arg0, TREE_OPERAND (arg1, 0));
8081 return fold_build2_loc (loc, icode, type, tem,
8082 TREE_OPERAND (arg1, 1));
8084 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8085 into (A OR B).
8086 For sequence point consistancy, we need to check for trapping,
8087 and side-effects. */
8088 else if (code == icode && simple_operand_p_2 (arg0)
8089 && simple_operand_p_2 (arg1))
8090 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8093 return NULL_TREE;
8096 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8097 by changing CODE to reduce the magnitude of constants involved in
8098 ARG0 of the comparison.
8099 Returns a canonicalized comparison tree if a simplification was
8100 possible, otherwise returns NULL_TREE.
8101 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8102 valid if signed overflow is undefined. */
8104 static tree
8105 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8106 tree arg0, tree arg1,
8107 bool *strict_overflow_p)
8109 enum tree_code code0 = TREE_CODE (arg0);
8110 tree t, cst0 = NULL_TREE;
8111 int sgn0;
8113 /* Match A +- CST code arg1. We can change this only if overflow
8114 is undefined. */
8115 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8116 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8117 /* In principle pointers also have undefined overflow behavior,
8118 but that causes problems elsewhere. */
8119 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8120 && (code0 == MINUS_EXPR
8121 || code0 == PLUS_EXPR)
8122 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8123 return NULL_TREE;
8125 /* Identify the constant in arg0 and its sign. */
8126 cst0 = TREE_OPERAND (arg0, 1);
8127 sgn0 = tree_int_cst_sgn (cst0);
8129 /* Overflowed constants and zero will cause problems. */
8130 if (integer_zerop (cst0)
8131 || TREE_OVERFLOW (cst0))
8132 return NULL_TREE;
8134 /* See if we can reduce the magnitude of the constant in
8135 arg0 by changing the comparison code. */
8136 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8137 if (code == LT_EXPR
8138 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8139 code = LE_EXPR;
8140 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8141 else if (code == GT_EXPR
8142 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8143 code = GE_EXPR;
8144 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8145 else if (code == LE_EXPR
8146 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8147 code = LT_EXPR;
8148 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8149 else if (code == GE_EXPR
8150 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8151 code = GT_EXPR;
8152 else
8153 return NULL_TREE;
8154 *strict_overflow_p = true;
8156 /* Now build the constant reduced in magnitude. But not if that
8157 would produce one outside of its types range. */
8158 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8159 && ((sgn0 == 1
8160 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8161 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8162 || (sgn0 == -1
8163 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8164 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8165 return NULL_TREE;
8167 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8168 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8169 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8170 t = fold_convert (TREE_TYPE (arg1), t);
8172 return fold_build2_loc (loc, code, type, t, arg1);
8175 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8176 overflow further. Try to decrease the magnitude of constants involved
8177 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8178 and put sole constants at the second argument position.
8179 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8181 static tree
8182 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8183 tree arg0, tree arg1)
8185 tree t;
8186 bool strict_overflow_p;
8187 const char * const warnmsg = G_("assuming signed overflow does not occur "
8188 "when reducing constant in comparison");
8190 /* Try canonicalization by simplifying arg0. */
8191 strict_overflow_p = false;
8192 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8193 &strict_overflow_p);
8194 if (t)
8196 if (strict_overflow_p)
8197 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8198 return t;
8201 /* Try canonicalization by simplifying arg1 using the swapped
8202 comparison. */
8203 code = swap_tree_comparison (code);
8204 strict_overflow_p = false;
8205 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8206 &strict_overflow_p);
8207 if (t && strict_overflow_p)
8208 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8209 return t;
8212 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8213 space. This is used to avoid issuing overflow warnings for
8214 expressions like &p->x which can not wrap. */
8216 static bool
8217 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8219 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8220 return true;
8222 if (bitpos < 0)
8223 return true;
8225 wide_int wi_offset;
8226 int precision = TYPE_PRECISION (TREE_TYPE (base));
8227 if (offset == NULL_TREE)
8228 wi_offset = wi::zero (precision);
8229 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8230 return true;
8231 else
8232 wi_offset = offset;
8234 bool overflow;
8235 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8236 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8237 if (overflow)
8238 return true;
8240 if (!wi::fits_uhwi_p (total))
8241 return true;
8243 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8244 if (size <= 0)
8245 return true;
8247 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8248 array. */
8249 if (TREE_CODE (base) == ADDR_EXPR)
8251 HOST_WIDE_INT base_size;
8253 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8254 if (base_size > 0 && size < base_size)
8255 size = base_size;
8258 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8261 /* Return a positive integer when the symbol DECL is known to have
8262 a nonzero address, zero when it's known not to (e.g., it's a weak
8263 symbol), and a negative integer when the symbol is not yet in the
8264 symbol table and so whether or not its address is zero is unknown. */
8265 static int
8266 maybe_nonzero_address (tree decl)
8268 if (DECL_P (decl) && decl_in_symtab_p (decl))
8269 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8270 return symbol->nonzero_address ();
8272 return -1;
8275 /* Subroutine of fold_binary. This routine performs all of the
8276 transformations that are common to the equality/inequality
8277 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8278 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8279 fold_binary should call fold_binary. Fold a comparison with
8280 tree code CODE and type TYPE with operands OP0 and OP1. Return
8281 the folded comparison or NULL_TREE. */
8283 static tree
8284 fold_comparison (location_t loc, enum tree_code code, tree type,
8285 tree op0, tree op1)
8287 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8288 tree arg0, arg1, tem;
8290 arg0 = op0;
8291 arg1 = op1;
8293 STRIP_SIGN_NOPS (arg0);
8294 STRIP_SIGN_NOPS (arg1);
8296 /* For comparisons of pointers we can decompose it to a compile time
8297 comparison of the base objects and the offsets into the object.
8298 This requires at least one operand being an ADDR_EXPR or a
8299 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8300 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8301 && (TREE_CODE (arg0) == ADDR_EXPR
8302 || TREE_CODE (arg1) == ADDR_EXPR
8303 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8304 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8306 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8307 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8308 machine_mode mode;
8309 int volatilep, reversep, unsignedp;
8310 bool indirect_base0 = false, indirect_base1 = false;
8312 /* Get base and offset for the access. Strip ADDR_EXPR for
8313 get_inner_reference, but put it back by stripping INDIRECT_REF
8314 off the base object if possible. indirect_baseN will be true
8315 if baseN is not an address but refers to the object itself. */
8316 base0 = arg0;
8317 if (TREE_CODE (arg0) == ADDR_EXPR)
8319 base0
8320 = get_inner_reference (TREE_OPERAND (arg0, 0),
8321 &bitsize, &bitpos0, &offset0, &mode,
8322 &unsignedp, &reversep, &volatilep);
8323 if (TREE_CODE (base0) == INDIRECT_REF)
8324 base0 = TREE_OPERAND (base0, 0);
8325 else
8326 indirect_base0 = true;
8328 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8330 base0 = TREE_OPERAND (arg0, 0);
8331 STRIP_SIGN_NOPS (base0);
8332 if (TREE_CODE (base0) == ADDR_EXPR)
8334 base0
8335 = get_inner_reference (TREE_OPERAND (base0, 0),
8336 &bitsize, &bitpos0, &offset0, &mode,
8337 &unsignedp, &reversep, &volatilep);
8338 if (TREE_CODE (base0) == INDIRECT_REF)
8339 base0 = TREE_OPERAND (base0, 0);
8340 else
8341 indirect_base0 = true;
8343 if (offset0 == NULL_TREE || integer_zerop (offset0))
8344 offset0 = TREE_OPERAND (arg0, 1);
8345 else
8346 offset0 = size_binop (PLUS_EXPR, offset0,
8347 TREE_OPERAND (arg0, 1));
8348 if (TREE_CODE (offset0) == INTEGER_CST)
8350 offset_int tem = wi::sext (wi::to_offset (offset0),
8351 TYPE_PRECISION (sizetype));
8352 tem <<= LOG2_BITS_PER_UNIT;
8353 tem += bitpos0;
8354 if (wi::fits_shwi_p (tem))
8356 bitpos0 = tem.to_shwi ();
8357 offset0 = NULL_TREE;
8362 base1 = arg1;
8363 if (TREE_CODE (arg1) == ADDR_EXPR)
8365 base1
8366 = get_inner_reference (TREE_OPERAND (arg1, 0),
8367 &bitsize, &bitpos1, &offset1, &mode,
8368 &unsignedp, &reversep, &volatilep);
8369 if (TREE_CODE (base1) == INDIRECT_REF)
8370 base1 = TREE_OPERAND (base1, 0);
8371 else
8372 indirect_base1 = true;
8374 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8376 base1 = TREE_OPERAND (arg1, 0);
8377 STRIP_SIGN_NOPS (base1);
8378 if (TREE_CODE (base1) == ADDR_EXPR)
8380 base1
8381 = get_inner_reference (TREE_OPERAND (base1, 0),
8382 &bitsize, &bitpos1, &offset1, &mode,
8383 &unsignedp, &reversep, &volatilep);
8384 if (TREE_CODE (base1) == INDIRECT_REF)
8385 base1 = TREE_OPERAND (base1, 0);
8386 else
8387 indirect_base1 = true;
8389 if (offset1 == NULL_TREE || integer_zerop (offset1))
8390 offset1 = TREE_OPERAND (arg1, 1);
8391 else
8392 offset1 = size_binop (PLUS_EXPR, offset1,
8393 TREE_OPERAND (arg1, 1));
8394 if (TREE_CODE (offset1) == INTEGER_CST)
8396 offset_int tem = wi::sext (wi::to_offset (offset1),
8397 TYPE_PRECISION (sizetype));
8398 tem <<= LOG2_BITS_PER_UNIT;
8399 tem += bitpos1;
8400 if (wi::fits_shwi_p (tem))
8402 bitpos1 = tem.to_shwi ();
8403 offset1 = NULL_TREE;
8408 /* If we have equivalent bases we might be able to simplify. */
8409 if (indirect_base0 == indirect_base1
8410 && operand_equal_p (base0, base1,
8411 indirect_base0 ? OEP_ADDRESS_OF : 0))
8413 /* We can fold this expression to a constant if the non-constant
8414 offset parts are equal. */
8415 if ((offset0 == offset1
8416 || (offset0 && offset1
8417 && operand_equal_p (offset0, offset1, 0)))
8418 && (equality_code
8419 || (indirect_base0
8420 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8421 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8424 if (!equality_code
8425 && bitpos0 != bitpos1
8426 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8427 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8428 fold_overflow_warning (("assuming pointer wraparound does not "
8429 "occur when comparing P +- C1 with "
8430 "P +- C2"),
8431 WARN_STRICT_OVERFLOW_CONDITIONAL);
8433 switch (code)
8435 case EQ_EXPR:
8436 return constant_boolean_node (bitpos0 == bitpos1, type);
8437 case NE_EXPR:
8438 return constant_boolean_node (bitpos0 != bitpos1, type);
8439 case LT_EXPR:
8440 return constant_boolean_node (bitpos0 < bitpos1, type);
8441 case LE_EXPR:
8442 return constant_boolean_node (bitpos0 <= bitpos1, type);
8443 case GE_EXPR:
8444 return constant_boolean_node (bitpos0 >= bitpos1, type);
8445 case GT_EXPR:
8446 return constant_boolean_node (bitpos0 > bitpos1, type);
8447 default:;
8450 /* We can simplify the comparison to a comparison of the variable
8451 offset parts if the constant offset parts are equal.
8452 Be careful to use signed sizetype here because otherwise we
8453 mess with array offsets in the wrong way. This is possible
8454 because pointer arithmetic is restricted to retain within an
8455 object and overflow on pointer differences is undefined as of
8456 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8457 else if (bitpos0 == bitpos1
8458 && (equality_code
8459 || (indirect_base0
8460 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8461 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8463 /* By converting to signed sizetype we cover middle-end pointer
8464 arithmetic which operates on unsigned pointer types of size
8465 type size and ARRAY_REF offsets which are properly sign or
8466 zero extended from their type in case it is narrower than
8467 sizetype. */
8468 if (offset0 == NULL_TREE)
8469 offset0 = build_int_cst (ssizetype, 0);
8470 else
8471 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8472 if (offset1 == NULL_TREE)
8473 offset1 = build_int_cst (ssizetype, 0);
8474 else
8475 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8477 if (!equality_code
8478 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8479 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8480 fold_overflow_warning (("assuming pointer wraparound does not "
8481 "occur when comparing P +- C1 with "
8482 "P +- C2"),
8483 WARN_STRICT_OVERFLOW_COMPARISON);
8485 return fold_build2_loc (loc, code, type, offset0, offset1);
8488 /* For equal offsets we can simplify to a comparison of the
8489 base addresses. */
8490 else if (bitpos0 == bitpos1
8491 && (indirect_base0
8492 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8493 && (indirect_base1
8494 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8495 && ((offset0 == offset1)
8496 || (offset0 && offset1
8497 && operand_equal_p (offset0, offset1, 0))))
8499 if (indirect_base0)
8500 base0 = build_fold_addr_expr_loc (loc, base0);
8501 if (indirect_base1)
8502 base1 = build_fold_addr_expr_loc (loc, base1);
8503 return fold_build2_loc (loc, code, type, base0, base1);
8505 /* Comparison between an ordinary (non-weak) symbol and a null
8506 pointer can be eliminated since such symbols must have a non
8507 null address. In C, relational expressions between pointers
8508 to objects and null pointers are undefined. The results
8509 below follow the C++ rules with the additional property that
8510 every object pointer compares greater than a null pointer.
8512 else if (DECL_P (base0)
8513 && maybe_nonzero_address (base0) > 0
8514 /* Avoid folding references to struct members at offset 0 to
8515 prevent tests like '&ptr->firstmember == 0' from getting
8516 eliminated. When ptr is null, although the -> expression
8517 is strictly speaking invalid, GCC retains it as a matter
8518 of QoI. See PR c/44555. */
8519 && (offset0 == NULL_TREE && bitpos0 != 0)
8520 /* The caller guarantees that when one of the arguments is
8521 constant (i.e., null in this case) it is second. */
8522 && integer_zerop (arg1))
8524 switch (code)
8526 case EQ_EXPR:
8527 case LE_EXPR:
8528 case LT_EXPR:
8529 return constant_boolean_node (false, type);
8530 case GE_EXPR:
8531 case GT_EXPR:
8532 case NE_EXPR:
8533 return constant_boolean_node (true, type);
8534 default:
8535 gcc_unreachable ();
8540 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8541 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8542 the resulting offset is smaller in absolute value than the
8543 original one and has the same sign. */
8544 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8545 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8546 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8547 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8548 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8549 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8550 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8551 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8553 tree const1 = TREE_OPERAND (arg0, 1);
8554 tree const2 = TREE_OPERAND (arg1, 1);
8555 tree variable1 = TREE_OPERAND (arg0, 0);
8556 tree variable2 = TREE_OPERAND (arg1, 0);
8557 tree cst;
8558 const char * const warnmsg = G_("assuming signed overflow does not "
8559 "occur when combining constants around "
8560 "a comparison");
8562 /* Put the constant on the side where it doesn't overflow and is
8563 of lower absolute value and of same sign than before. */
8564 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8565 ? MINUS_EXPR : PLUS_EXPR,
8566 const2, const1);
8567 if (!TREE_OVERFLOW (cst)
8568 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8569 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8571 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8572 return fold_build2_loc (loc, code, type,
8573 variable1,
8574 fold_build2_loc (loc, TREE_CODE (arg1),
8575 TREE_TYPE (arg1),
8576 variable2, cst));
8579 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8580 ? MINUS_EXPR : PLUS_EXPR,
8581 const1, const2);
8582 if (!TREE_OVERFLOW (cst)
8583 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8584 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8586 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8587 return fold_build2_loc (loc, code, type,
8588 fold_build2_loc (loc, TREE_CODE (arg0),
8589 TREE_TYPE (arg0),
8590 variable1, cst),
8591 variable2);
8595 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8596 if (tem)
8597 return tem;
8599 /* If we are comparing an expression that just has comparisons
8600 of two integer values, arithmetic expressions of those comparisons,
8601 and constants, we can simplify it. There are only three cases
8602 to check: the two values can either be equal, the first can be
8603 greater, or the second can be greater. Fold the expression for
8604 those three values. Since each value must be 0 or 1, we have
8605 eight possibilities, each of which corresponds to the constant 0
8606 or 1 or one of the six possible comparisons.
8608 This handles common cases like (a > b) == 0 but also handles
8609 expressions like ((x > y) - (y > x)) > 0, which supposedly
8610 occur in macroized code. */
8612 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8614 tree cval1 = 0, cval2 = 0;
8615 int save_p = 0;
8617 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8618 /* Don't handle degenerate cases here; they should already
8619 have been handled anyway. */
8620 && cval1 != 0 && cval2 != 0
8621 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8622 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8623 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8624 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8625 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8626 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8627 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8629 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8630 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8632 /* We can't just pass T to eval_subst in case cval1 or cval2
8633 was the same as ARG1. */
8635 tree high_result
8636 = fold_build2_loc (loc, code, type,
8637 eval_subst (loc, arg0, cval1, maxval,
8638 cval2, minval),
8639 arg1);
8640 tree equal_result
8641 = fold_build2_loc (loc, code, type,
8642 eval_subst (loc, arg0, cval1, maxval,
8643 cval2, maxval),
8644 arg1);
8645 tree low_result
8646 = fold_build2_loc (loc, code, type,
8647 eval_subst (loc, arg0, cval1, minval,
8648 cval2, maxval),
8649 arg1);
8651 /* All three of these results should be 0 or 1. Confirm they are.
8652 Then use those values to select the proper code to use. */
8654 if (TREE_CODE (high_result) == INTEGER_CST
8655 && TREE_CODE (equal_result) == INTEGER_CST
8656 && TREE_CODE (low_result) == INTEGER_CST)
8658 /* Make a 3-bit mask with the high-order bit being the
8659 value for `>', the next for '=', and the low for '<'. */
8660 switch ((integer_onep (high_result) * 4)
8661 + (integer_onep (equal_result) * 2)
8662 + integer_onep (low_result))
8664 case 0:
8665 /* Always false. */
8666 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8667 case 1:
8668 code = LT_EXPR;
8669 break;
8670 case 2:
8671 code = EQ_EXPR;
8672 break;
8673 case 3:
8674 code = LE_EXPR;
8675 break;
8676 case 4:
8677 code = GT_EXPR;
8678 break;
8679 case 5:
8680 code = NE_EXPR;
8681 break;
8682 case 6:
8683 code = GE_EXPR;
8684 break;
8685 case 7:
8686 /* Always true. */
8687 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8690 if (save_p)
8692 tem = save_expr (build2 (code, type, cval1, cval2));
8693 SET_EXPR_LOCATION (tem, loc);
8694 return tem;
8696 return fold_build2_loc (loc, code, type, cval1, cval2);
8701 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8702 into a single range test. */
8703 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8704 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8705 && TREE_CODE (arg1) == INTEGER_CST
8706 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8707 && !integer_zerop (TREE_OPERAND (arg0, 1))
8708 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8709 && !TREE_OVERFLOW (arg1))
8711 tem = fold_div_compare (loc, code, type, arg0, arg1);
8712 if (tem != NULL_TREE)
8713 return tem;
8716 return NULL_TREE;
8720 /* Subroutine of fold_binary. Optimize complex multiplications of the
8721 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8722 argument EXPR represents the expression "z" of type TYPE. */
8724 static tree
8725 fold_mult_zconjz (location_t loc, tree type, tree expr)
8727 tree itype = TREE_TYPE (type);
8728 tree rpart, ipart, tem;
8730 if (TREE_CODE (expr) == COMPLEX_EXPR)
8732 rpart = TREE_OPERAND (expr, 0);
8733 ipart = TREE_OPERAND (expr, 1);
8735 else if (TREE_CODE (expr) == COMPLEX_CST)
8737 rpart = TREE_REALPART (expr);
8738 ipart = TREE_IMAGPART (expr);
8740 else
8742 expr = save_expr (expr);
8743 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8744 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8747 rpart = save_expr (rpart);
8748 ipart = save_expr (ipart);
8749 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8750 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8751 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8752 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8753 build_zero_cst (itype));
8757 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8758 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8760 static bool
8761 vec_cst_ctor_to_array (tree arg, tree *elts)
8763 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8765 if (TREE_CODE (arg) == VECTOR_CST)
8767 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8768 elts[i] = VECTOR_CST_ELT (arg, i);
8770 else if (TREE_CODE (arg) == CONSTRUCTOR)
8772 constructor_elt *elt;
8774 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8775 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8776 return false;
8777 else
8778 elts[i] = elt->value;
8780 else
8781 return false;
8782 for (; i < nelts; i++)
8783 elts[i]
8784 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8785 return true;
8788 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8789 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8790 NULL_TREE otherwise. */
8792 static tree
8793 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8795 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8796 tree *elts;
8797 bool need_ctor = false;
8799 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8800 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8801 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8802 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8803 return NULL_TREE;
8805 elts = XALLOCAVEC (tree, nelts * 3);
8806 if (!vec_cst_ctor_to_array (arg0, elts)
8807 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8808 return NULL_TREE;
8810 for (i = 0; i < nelts; i++)
8812 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8813 need_ctor = true;
8814 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8817 if (need_ctor)
8819 vec<constructor_elt, va_gc> *v;
8820 vec_alloc (v, nelts);
8821 for (i = 0; i < nelts; i++)
8822 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8823 return build_constructor (type, v);
8825 else
8826 return build_vector (type, &elts[2 * nelts]);
8829 /* Try to fold a pointer difference of type TYPE two address expressions of
8830 array references AREF0 and AREF1 using location LOC. Return a
8831 simplified expression for the difference or NULL_TREE. */
8833 static tree
8834 fold_addr_of_array_ref_difference (location_t loc, tree type,
8835 tree aref0, tree aref1)
8837 tree base0 = TREE_OPERAND (aref0, 0);
8838 tree base1 = TREE_OPERAND (aref1, 0);
8839 tree base_offset = build_int_cst (type, 0);
8841 /* If the bases are array references as well, recurse. If the bases
8842 are pointer indirections compute the difference of the pointers.
8843 If the bases are equal, we are set. */
8844 if ((TREE_CODE (base0) == ARRAY_REF
8845 && TREE_CODE (base1) == ARRAY_REF
8846 && (base_offset
8847 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8848 || (INDIRECT_REF_P (base0)
8849 && INDIRECT_REF_P (base1)
8850 && (base_offset
8851 = fold_binary_loc (loc, MINUS_EXPR, type,
8852 fold_convert (type, TREE_OPERAND (base0, 0)),
8853 fold_convert (type,
8854 TREE_OPERAND (base1, 0)))))
8855 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8857 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8858 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8859 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8860 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8861 return fold_build2_loc (loc, PLUS_EXPR, type,
8862 base_offset,
8863 fold_build2_loc (loc, MULT_EXPR, type,
8864 diff, esz));
8866 return NULL_TREE;
8869 /* If the real or vector real constant CST of type TYPE has an exact
8870 inverse, return it, else return NULL. */
8872 tree
8873 exact_inverse (tree type, tree cst)
8875 REAL_VALUE_TYPE r;
8876 tree unit_type, *elts;
8877 machine_mode mode;
8878 unsigned vec_nelts, i;
8880 switch (TREE_CODE (cst))
8882 case REAL_CST:
8883 r = TREE_REAL_CST (cst);
8885 if (exact_real_inverse (TYPE_MODE (type), &r))
8886 return build_real (type, r);
8888 return NULL_TREE;
8890 case VECTOR_CST:
8891 vec_nelts = VECTOR_CST_NELTS (cst);
8892 elts = XALLOCAVEC (tree, vec_nelts);
8893 unit_type = TREE_TYPE (type);
8894 mode = TYPE_MODE (unit_type);
8896 for (i = 0; i < vec_nelts; i++)
8898 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8899 if (!exact_real_inverse (mode, &r))
8900 return NULL_TREE;
8901 elts[i] = build_real (unit_type, r);
8904 return build_vector (type, elts);
8906 default:
8907 return NULL_TREE;
8911 /* Mask out the tz least significant bits of X of type TYPE where
8912 tz is the number of trailing zeroes in Y. */
8913 static wide_int
8914 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8916 int tz = wi::ctz (y);
8917 if (tz > 0)
8918 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8919 return x;
8922 /* Return true when T is an address and is known to be nonzero.
8923 For floating point we further ensure that T is not denormal.
8924 Similar logic is present in nonzero_address in rtlanal.h.
8926 If the return value is based on the assumption that signed overflow
8927 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8928 change *STRICT_OVERFLOW_P. */
8930 static bool
8931 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8933 tree type = TREE_TYPE (t);
8934 enum tree_code code;
8936 /* Doing something useful for floating point would need more work. */
8937 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8938 return false;
8940 code = TREE_CODE (t);
8941 switch (TREE_CODE_CLASS (code))
8943 case tcc_unary:
8944 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8945 strict_overflow_p);
8946 case tcc_binary:
8947 case tcc_comparison:
8948 return tree_binary_nonzero_warnv_p (code, type,
8949 TREE_OPERAND (t, 0),
8950 TREE_OPERAND (t, 1),
8951 strict_overflow_p);
8952 case tcc_constant:
8953 case tcc_declaration:
8954 case tcc_reference:
8955 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8957 default:
8958 break;
8961 switch (code)
8963 case TRUTH_NOT_EXPR:
8964 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8965 strict_overflow_p);
8967 case TRUTH_AND_EXPR:
8968 case TRUTH_OR_EXPR:
8969 case TRUTH_XOR_EXPR:
8970 return tree_binary_nonzero_warnv_p (code, type,
8971 TREE_OPERAND (t, 0),
8972 TREE_OPERAND (t, 1),
8973 strict_overflow_p);
8975 case COND_EXPR:
8976 case CONSTRUCTOR:
8977 case OBJ_TYPE_REF:
8978 case ASSERT_EXPR:
8979 case ADDR_EXPR:
8980 case WITH_SIZE_EXPR:
8981 case SSA_NAME:
8982 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8984 case COMPOUND_EXPR:
8985 case MODIFY_EXPR:
8986 case BIND_EXPR:
8987 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
8988 strict_overflow_p);
8990 case SAVE_EXPR:
8991 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
8992 strict_overflow_p);
8994 case CALL_EXPR:
8996 tree fndecl = get_callee_fndecl (t);
8997 if (!fndecl) return false;
8998 if (flag_delete_null_pointer_checks && !flag_check_new
8999 && DECL_IS_OPERATOR_NEW (fndecl)
9000 && !TREE_NOTHROW (fndecl))
9001 return true;
9002 if (flag_delete_null_pointer_checks
9003 && lookup_attribute ("returns_nonnull",
9004 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9005 return true;
9006 return alloca_call_p (t);
9009 default:
9010 break;
9012 return false;
9015 /* Return true when T is an address and is known to be nonzero.
9016 Handle warnings about undefined signed overflow. */
9018 static bool
9019 tree_expr_nonzero_p (tree t)
9021 bool ret, strict_overflow_p;
9023 strict_overflow_p = false;
9024 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9025 if (strict_overflow_p)
9026 fold_overflow_warning (("assuming signed overflow does not occur when "
9027 "determining that expression is always "
9028 "non-zero"),
9029 WARN_STRICT_OVERFLOW_MISC);
9030 return ret;
9033 /* Return true if T is known not to be equal to an integer W. */
9035 bool
9036 expr_not_equal_to (tree t, const wide_int &w)
9038 wide_int min, max, nz;
9039 value_range_type rtype;
9040 switch (TREE_CODE (t))
9042 case INTEGER_CST:
9043 return wi::ne_p (t, w);
9045 case SSA_NAME:
9046 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9047 return false;
9048 rtype = get_range_info (t, &min, &max);
9049 if (rtype == VR_RANGE)
9051 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9052 return true;
9053 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9054 return true;
9056 else if (rtype == VR_ANTI_RANGE
9057 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9058 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9059 return true;
9060 /* If T has some known zero bits and W has any of those bits set,
9061 then T is known not to be equal to W. */
9062 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9063 TYPE_PRECISION (TREE_TYPE (t))), 0))
9064 return true;
9065 return false;
9067 default:
9068 return false;
9072 /* Fold a binary expression of code CODE and type TYPE with operands
9073 OP0 and OP1. LOC is the location of the resulting expression.
9074 Return the folded expression if folding is successful. Otherwise,
9075 return NULL_TREE. */
9077 tree
9078 fold_binary_loc (location_t loc,
9079 enum tree_code code, tree type, tree op0, tree op1)
9081 enum tree_code_class kind = TREE_CODE_CLASS (code);
9082 tree arg0, arg1, tem;
9083 tree t1 = NULL_TREE;
9084 bool strict_overflow_p;
9085 unsigned int prec;
9087 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9088 && TREE_CODE_LENGTH (code) == 2
9089 && op0 != NULL_TREE
9090 && op1 != NULL_TREE);
9092 arg0 = op0;
9093 arg1 = op1;
9095 /* Strip any conversions that don't change the mode. This is
9096 safe for every expression, except for a comparison expression
9097 because its signedness is derived from its operands. So, in
9098 the latter case, only strip conversions that don't change the
9099 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9100 preserved.
9102 Note that this is done as an internal manipulation within the
9103 constant folder, in order to find the simplest representation
9104 of the arguments so that their form can be studied. In any
9105 cases, the appropriate type conversions should be put back in
9106 the tree that will get out of the constant folder. */
9108 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9110 STRIP_SIGN_NOPS (arg0);
9111 STRIP_SIGN_NOPS (arg1);
9113 else
9115 STRIP_NOPS (arg0);
9116 STRIP_NOPS (arg1);
9119 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9120 constant but we can't do arithmetic on them. */
9121 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9123 tem = const_binop (code, type, arg0, arg1);
9124 if (tem != NULL_TREE)
9126 if (TREE_TYPE (tem) != type)
9127 tem = fold_convert_loc (loc, type, tem);
9128 return tem;
9132 /* If this is a commutative operation, and ARG0 is a constant, move it
9133 to ARG1 to reduce the number of tests below. */
9134 if (commutative_tree_code (code)
9135 && tree_swap_operands_p (arg0, arg1))
9136 return fold_build2_loc (loc, code, type, op1, op0);
9138 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9139 to ARG1 to reduce the number of tests below. */
9140 if (kind == tcc_comparison
9141 && tree_swap_operands_p (arg0, arg1))
9142 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9144 tem = generic_simplify (loc, code, type, op0, op1);
9145 if (tem)
9146 return tem;
9148 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9150 First check for cases where an arithmetic operation is applied to a
9151 compound, conditional, or comparison operation. Push the arithmetic
9152 operation inside the compound or conditional to see if any folding
9153 can then be done. Convert comparison to conditional for this purpose.
9154 The also optimizes non-constant cases that used to be done in
9155 expand_expr.
9157 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9158 one of the operands is a comparison and the other is a comparison, a
9159 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9160 code below would make the expression more complex. Change it to a
9161 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9162 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9164 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9165 || code == EQ_EXPR || code == NE_EXPR)
9166 && TREE_CODE (type) != VECTOR_TYPE
9167 && ((truth_value_p (TREE_CODE (arg0))
9168 && (truth_value_p (TREE_CODE (arg1))
9169 || (TREE_CODE (arg1) == BIT_AND_EXPR
9170 && integer_onep (TREE_OPERAND (arg1, 1)))))
9171 || (truth_value_p (TREE_CODE (arg1))
9172 && (truth_value_p (TREE_CODE (arg0))
9173 || (TREE_CODE (arg0) == BIT_AND_EXPR
9174 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9176 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9177 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9178 : TRUTH_XOR_EXPR,
9179 boolean_type_node,
9180 fold_convert_loc (loc, boolean_type_node, arg0),
9181 fold_convert_loc (loc, boolean_type_node, arg1));
9183 if (code == EQ_EXPR)
9184 tem = invert_truthvalue_loc (loc, tem);
9186 return fold_convert_loc (loc, type, tem);
9189 if (TREE_CODE_CLASS (code) == tcc_binary
9190 || TREE_CODE_CLASS (code) == tcc_comparison)
9192 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9194 tem = fold_build2_loc (loc, code, type,
9195 fold_convert_loc (loc, TREE_TYPE (op0),
9196 TREE_OPERAND (arg0, 1)), op1);
9197 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9198 tem);
9200 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9202 tem = fold_build2_loc (loc, code, type, op0,
9203 fold_convert_loc (loc, TREE_TYPE (op1),
9204 TREE_OPERAND (arg1, 1)));
9205 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9206 tem);
9209 if (TREE_CODE (arg0) == COND_EXPR
9210 || TREE_CODE (arg0) == VEC_COND_EXPR
9211 || COMPARISON_CLASS_P (arg0))
9213 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9214 arg0, arg1,
9215 /*cond_first_p=*/1);
9216 if (tem != NULL_TREE)
9217 return tem;
9220 if (TREE_CODE (arg1) == COND_EXPR
9221 || TREE_CODE (arg1) == VEC_COND_EXPR
9222 || COMPARISON_CLASS_P (arg1))
9224 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9225 arg1, arg0,
9226 /*cond_first_p=*/0);
9227 if (tem != NULL_TREE)
9228 return tem;
9232 switch (code)
9234 case MEM_REF:
9235 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9236 if (TREE_CODE (arg0) == ADDR_EXPR
9237 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9239 tree iref = TREE_OPERAND (arg0, 0);
9240 return fold_build2 (MEM_REF, type,
9241 TREE_OPERAND (iref, 0),
9242 int_const_binop (PLUS_EXPR, arg1,
9243 TREE_OPERAND (iref, 1)));
9246 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9247 if (TREE_CODE (arg0) == ADDR_EXPR
9248 && handled_component_p (TREE_OPERAND (arg0, 0)))
9250 tree base;
9251 HOST_WIDE_INT coffset;
9252 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9253 &coffset);
9254 if (!base)
9255 return NULL_TREE;
9256 return fold_build2 (MEM_REF, type,
9257 build_fold_addr_expr (base),
9258 int_const_binop (PLUS_EXPR, arg1,
9259 size_int (coffset)));
9262 return NULL_TREE;
9264 case POINTER_PLUS_EXPR:
9265 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9266 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9267 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9268 return fold_convert_loc (loc, type,
9269 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9270 fold_convert_loc (loc, sizetype,
9271 arg1),
9272 fold_convert_loc (loc, sizetype,
9273 arg0)));
9275 return NULL_TREE;
9277 case PLUS_EXPR:
9278 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9280 /* X + (X / CST) * -CST is X % CST. */
9281 if (TREE_CODE (arg1) == MULT_EXPR
9282 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9283 && operand_equal_p (arg0,
9284 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9286 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9287 tree cst1 = TREE_OPERAND (arg1, 1);
9288 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9289 cst1, cst0);
9290 if (sum && integer_zerop (sum))
9291 return fold_convert_loc (loc, type,
9292 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9293 TREE_TYPE (arg0), arg0,
9294 cst0));
9298 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9299 one. Make sure the type is not saturating and has the signedness of
9300 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9301 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9302 if ((TREE_CODE (arg0) == MULT_EXPR
9303 || TREE_CODE (arg1) == MULT_EXPR)
9304 && !TYPE_SATURATING (type)
9305 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9306 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9307 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9309 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9310 if (tem)
9311 return tem;
9314 if (! FLOAT_TYPE_P (type))
9316 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9317 (plus (plus (mult) (mult)) (foo)) so that we can
9318 take advantage of the factoring cases below. */
9319 if (ANY_INTEGRAL_TYPE_P (type)
9320 && TYPE_OVERFLOW_WRAPS (type)
9321 && (((TREE_CODE (arg0) == PLUS_EXPR
9322 || TREE_CODE (arg0) == MINUS_EXPR)
9323 && TREE_CODE (arg1) == MULT_EXPR)
9324 || ((TREE_CODE (arg1) == PLUS_EXPR
9325 || TREE_CODE (arg1) == MINUS_EXPR)
9326 && TREE_CODE (arg0) == MULT_EXPR)))
9328 tree parg0, parg1, parg, marg;
9329 enum tree_code pcode;
9331 if (TREE_CODE (arg1) == MULT_EXPR)
9332 parg = arg0, marg = arg1;
9333 else
9334 parg = arg1, marg = arg0;
9335 pcode = TREE_CODE (parg);
9336 parg0 = TREE_OPERAND (parg, 0);
9337 parg1 = TREE_OPERAND (parg, 1);
9338 STRIP_NOPS (parg0);
9339 STRIP_NOPS (parg1);
9341 if (TREE_CODE (parg0) == MULT_EXPR
9342 && TREE_CODE (parg1) != MULT_EXPR)
9343 return fold_build2_loc (loc, pcode, type,
9344 fold_build2_loc (loc, PLUS_EXPR, type,
9345 fold_convert_loc (loc, type,
9346 parg0),
9347 fold_convert_loc (loc, type,
9348 marg)),
9349 fold_convert_loc (loc, type, parg1));
9350 if (TREE_CODE (parg0) != MULT_EXPR
9351 && TREE_CODE (parg1) == MULT_EXPR)
9352 return
9353 fold_build2_loc (loc, PLUS_EXPR, type,
9354 fold_convert_loc (loc, type, parg0),
9355 fold_build2_loc (loc, pcode, type,
9356 fold_convert_loc (loc, type, marg),
9357 fold_convert_loc (loc, type,
9358 parg1)));
9361 else
9363 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9364 to __complex__ ( x, y ). This is not the same for SNaNs or
9365 if signed zeros are involved. */
9366 if (!HONOR_SNANS (element_mode (arg0))
9367 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9368 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9370 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9371 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9372 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9373 bool arg0rz = false, arg0iz = false;
9374 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9375 || (arg0i && (arg0iz = real_zerop (arg0i))))
9377 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9378 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9379 if (arg0rz && arg1i && real_zerop (arg1i))
9381 tree rp = arg1r ? arg1r
9382 : build1 (REALPART_EXPR, rtype, arg1);
9383 tree ip = arg0i ? arg0i
9384 : build1 (IMAGPART_EXPR, rtype, arg0);
9385 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9387 else if (arg0iz && arg1r && real_zerop (arg1r))
9389 tree rp = arg0r ? arg0r
9390 : build1 (REALPART_EXPR, rtype, arg0);
9391 tree ip = arg1i ? arg1i
9392 : build1 (IMAGPART_EXPR, rtype, arg1);
9393 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9398 if (flag_unsafe_math_optimizations
9399 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9400 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9401 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9402 return tem;
9404 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9405 We associate floats only if the user has specified
9406 -fassociative-math. */
9407 if (flag_associative_math
9408 && TREE_CODE (arg1) == PLUS_EXPR
9409 && TREE_CODE (arg0) != MULT_EXPR)
9411 tree tree10 = TREE_OPERAND (arg1, 0);
9412 tree tree11 = TREE_OPERAND (arg1, 1);
9413 if (TREE_CODE (tree11) == MULT_EXPR
9414 && TREE_CODE (tree10) == MULT_EXPR)
9416 tree tree0;
9417 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9418 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9421 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9422 We associate floats only if the user has specified
9423 -fassociative-math. */
9424 if (flag_associative_math
9425 && TREE_CODE (arg0) == PLUS_EXPR
9426 && TREE_CODE (arg1) != MULT_EXPR)
9428 tree tree00 = TREE_OPERAND (arg0, 0);
9429 tree tree01 = TREE_OPERAND (arg0, 1);
9430 if (TREE_CODE (tree01) == MULT_EXPR
9431 && TREE_CODE (tree00) == MULT_EXPR)
9433 tree tree0;
9434 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9435 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9440 bit_rotate:
9441 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9442 is a rotate of A by C1 bits. */
9443 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9444 is a rotate of A by B bits. */
9446 enum tree_code code0, code1;
9447 tree rtype;
9448 code0 = TREE_CODE (arg0);
9449 code1 = TREE_CODE (arg1);
9450 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9451 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9452 && operand_equal_p (TREE_OPERAND (arg0, 0),
9453 TREE_OPERAND (arg1, 0), 0)
9454 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9455 TYPE_UNSIGNED (rtype))
9456 /* Only create rotates in complete modes. Other cases are not
9457 expanded properly. */
9458 && (element_precision (rtype)
9459 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9461 tree tree01, tree11;
9462 enum tree_code code01, code11;
9464 tree01 = TREE_OPERAND (arg0, 1);
9465 tree11 = TREE_OPERAND (arg1, 1);
9466 STRIP_NOPS (tree01);
9467 STRIP_NOPS (tree11);
9468 code01 = TREE_CODE (tree01);
9469 code11 = TREE_CODE (tree11);
9470 if (code01 == INTEGER_CST
9471 && code11 == INTEGER_CST
9472 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9473 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9475 tem = build2_loc (loc, LROTATE_EXPR,
9476 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9477 TREE_OPERAND (arg0, 0),
9478 code0 == LSHIFT_EXPR
9479 ? TREE_OPERAND (arg0, 1)
9480 : TREE_OPERAND (arg1, 1));
9481 return fold_convert_loc (loc, type, tem);
9483 else if (code11 == MINUS_EXPR)
9485 tree tree110, tree111;
9486 tree110 = TREE_OPERAND (tree11, 0);
9487 tree111 = TREE_OPERAND (tree11, 1);
9488 STRIP_NOPS (tree110);
9489 STRIP_NOPS (tree111);
9490 if (TREE_CODE (tree110) == INTEGER_CST
9491 && 0 == compare_tree_int (tree110,
9492 element_precision
9493 (TREE_TYPE (TREE_OPERAND
9494 (arg0, 0))))
9495 && operand_equal_p (tree01, tree111, 0))
9496 return
9497 fold_convert_loc (loc, type,
9498 build2 ((code0 == LSHIFT_EXPR
9499 ? LROTATE_EXPR
9500 : RROTATE_EXPR),
9501 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9502 TREE_OPERAND (arg0, 0),
9503 TREE_OPERAND (arg0, 1)));
9505 else if (code01 == MINUS_EXPR)
9507 tree tree010, tree011;
9508 tree010 = TREE_OPERAND (tree01, 0);
9509 tree011 = TREE_OPERAND (tree01, 1);
9510 STRIP_NOPS (tree010);
9511 STRIP_NOPS (tree011);
9512 if (TREE_CODE (tree010) == INTEGER_CST
9513 && 0 == compare_tree_int (tree010,
9514 element_precision
9515 (TREE_TYPE (TREE_OPERAND
9516 (arg0, 0))))
9517 && operand_equal_p (tree11, tree011, 0))
9518 return fold_convert_loc
9519 (loc, type,
9520 build2 ((code0 != LSHIFT_EXPR
9521 ? LROTATE_EXPR
9522 : RROTATE_EXPR),
9523 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9524 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9529 associate:
9530 /* In most languages, can't associate operations on floats through
9531 parentheses. Rather than remember where the parentheses were, we
9532 don't associate floats at all, unless the user has specified
9533 -fassociative-math.
9534 And, we need to make sure type is not saturating. */
9536 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9537 && !TYPE_SATURATING (type))
9539 tree var0, con0, lit0, minus_lit0;
9540 tree var1, con1, lit1, minus_lit1;
9541 tree atype = type;
9542 bool ok = true;
9544 /* Split both trees into variables, constants, and literals. Then
9545 associate each group together, the constants with literals,
9546 then the result with variables. This increases the chances of
9547 literals being recombined later and of generating relocatable
9548 expressions for the sum of a constant and literal. */
9549 var0 = split_tree (loc, arg0, type, code,
9550 &con0, &lit0, &minus_lit0, 0);
9551 var1 = split_tree (loc, arg1, type, code,
9552 &con1, &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) && POINTER_TYPE_OVERFLOW_UNDEFINED)
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) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9575 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9577 if (var0 && var1)
9579 tree tmp0 = var0;
9580 tree tmp1 = var1;
9581 bool one_neg = false;
9583 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9585 tmp0 = TREE_OPERAND (tmp0, 0);
9586 one_neg = !one_neg;
9588 if (CONVERT_EXPR_P (tmp0)
9589 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9590 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9591 <= TYPE_PRECISION (atype)))
9592 tmp0 = TREE_OPERAND (tmp0, 0);
9593 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9595 tmp1 = TREE_OPERAND (tmp1, 0);
9596 one_neg = !one_neg;
9598 if (CONVERT_EXPR_P (tmp1)
9599 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9600 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9601 <= TYPE_PRECISION (atype)))
9602 tmp1 = TREE_OPERAND (tmp1, 0);
9603 /* The only case we can still associate with two variables
9604 is if they cancel out. */
9605 if (!one_neg
9606 || !operand_equal_p (tmp0, tmp1, 0))
9607 ok = false;
9611 /* Only do something if we found more than two objects. Otherwise,
9612 nothing has changed and we risk infinite recursion. */
9613 if (ok
9614 && (2 < ((var0 != 0) + (var1 != 0)
9615 + (con0 != 0) + (con1 != 0)
9616 + (lit0 != 0) + (lit1 != 0)
9617 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9619 bool any_overflows = false;
9620 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9621 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9622 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9623 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9624 var0 = associate_trees (loc, var0, var1, code, atype);
9625 con0 = associate_trees (loc, con0, con1, code, atype);
9626 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9627 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9628 code, atype);
9630 /* Preserve the MINUS_EXPR if the negative part of the literal is
9631 greater than the positive part. Otherwise, the multiplicative
9632 folding code (i.e extract_muldiv) may be fooled in case
9633 unsigned constants are subtracted, like in the following
9634 example: ((X*2 + 4) - 8U)/2. */
9635 if (minus_lit0 && lit0)
9637 if (TREE_CODE (lit0) == INTEGER_CST
9638 && TREE_CODE (minus_lit0) == INTEGER_CST
9639 && tree_int_cst_lt (lit0, minus_lit0))
9641 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9642 MINUS_EXPR, atype);
9643 lit0 = 0;
9645 else
9647 lit0 = associate_trees (loc, lit0, minus_lit0,
9648 MINUS_EXPR, atype);
9649 minus_lit0 = 0;
9653 /* Don't introduce overflows through reassociation. */
9654 if (!any_overflows
9655 && ((lit0 && TREE_OVERFLOW_P (lit0))
9656 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9657 return NULL_TREE;
9659 if (minus_lit0)
9661 if (con0 == 0)
9662 return
9663 fold_convert_loc (loc, type,
9664 associate_trees (loc, var0, minus_lit0,
9665 MINUS_EXPR, atype));
9666 else
9668 con0 = associate_trees (loc, con0, minus_lit0,
9669 MINUS_EXPR, atype);
9670 return
9671 fold_convert_loc (loc, type,
9672 associate_trees (loc, var0, con0,
9673 PLUS_EXPR, atype));
9677 con0 = associate_trees (loc, con0, lit0, code, atype);
9678 return
9679 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9680 code, atype));
9684 return NULL_TREE;
9686 case MINUS_EXPR:
9687 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9688 if (TREE_CODE (arg0) == NEGATE_EXPR
9689 && negate_expr_p (op1))
9690 return fold_build2_loc (loc, MINUS_EXPR, type,
9691 negate_expr (op1),
9692 fold_convert_loc (loc, type,
9693 TREE_OPERAND (arg0, 0)));
9695 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9696 __complex__ ( x, -y ). This is not the same for SNaNs or if
9697 signed zeros are involved. */
9698 if (!HONOR_SNANS (element_mode (arg0))
9699 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9700 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9702 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9703 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9704 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9705 bool arg0rz = false, arg0iz = false;
9706 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9707 || (arg0i && (arg0iz = real_zerop (arg0i))))
9709 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9710 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9711 if (arg0rz && arg1i && real_zerop (arg1i))
9713 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9714 arg1r ? arg1r
9715 : build1 (REALPART_EXPR, rtype, arg1));
9716 tree ip = arg0i ? arg0i
9717 : build1 (IMAGPART_EXPR, rtype, arg0);
9718 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9720 else if (arg0iz && arg1r && real_zerop (arg1r))
9722 tree rp = arg0r ? arg0r
9723 : build1 (REALPART_EXPR, rtype, arg0);
9724 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9725 arg1i ? arg1i
9726 : build1 (IMAGPART_EXPR, rtype, arg1));
9727 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9732 /* A - B -> A + (-B) if B is easily negatable. */
9733 if (negate_expr_p (op1)
9734 && ! TYPE_OVERFLOW_SANITIZED (type)
9735 && ((FLOAT_TYPE_P (type)
9736 /* Avoid this transformation if B is a positive REAL_CST. */
9737 && (TREE_CODE (op1) != REAL_CST
9738 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9739 || INTEGRAL_TYPE_P (type)))
9740 return fold_build2_loc (loc, PLUS_EXPR, type,
9741 fold_convert_loc (loc, type, arg0),
9742 negate_expr (op1));
9744 /* Fold &a[i] - &a[j] to i-j. */
9745 if (TREE_CODE (arg0) == ADDR_EXPR
9746 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9747 && TREE_CODE (arg1) == ADDR_EXPR
9748 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9750 tree tem = fold_addr_of_array_ref_difference (loc, type,
9751 TREE_OPERAND (arg0, 0),
9752 TREE_OPERAND (arg1, 0));
9753 if (tem)
9754 return tem;
9757 if (FLOAT_TYPE_P (type)
9758 && flag_unsafe_math_optimizations
9759 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9760 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9761 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9762 return tem;
9764 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9765 one. Make sure the type is not saturating and has the signedness of
9766 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9767 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9768 if ((TREE_CODE (arg0) == MULT_EXPR
9769 || TREE_CODE (arg1) == MULT_EXPR)
9770 && !TYPE_SATURATING (type)
9771 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9772 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9773 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9775 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9776 if (tem)
9777 return tem;
9780 goto associate;
9782 case MULT_EXPR:
9783 if (! FLOAT_TYPE_P (type))
9785 /* Transform x * -C into -x * C if x is easily negatable. */
9786 if (TREE_CODE (op1) == INTEGER_CST
9787 && tree_int_cst_sgn (op1) == -1
9788 && negate_expr_p (op0)
9789 && (tem = negate_expr (op1)) != op1
9790 && ! TREE_OVERFLOW (tem))
9791 return fold_build2_loc (loc, MULT_EXPR, type,
9792 fold_convert_loc (loc, type,
9793 negate_expr (op0)), tem);
9795 strict_overflow_p = false;
9796 if (TREE_CODE (arg1) == INTEGER_CST
9797 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9798 &strict_overflow_p)))
9800 if (strict_overflow_p)
9801 fold_overflow_warning (("assuming signed overflow does not "
9802 "occur when simplifying "
9803 "multiplication"),
9804 WARN_STRICT_OVERFLOW_MISC);
9805 return fold_convert_loc (loc, type, tem);
9808 /* Optimize z * conj(z) for integer complex numbers. */
9809 if (TREE_CODE (arg0) == CONJ_EXPR
9810 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9811 return fold_mult_zconjz (loc, type, arg1);
9812 if (TREE_CODE (arg1) == CONJ_EXPR
9813 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9814 return fold_mult_zconjz (loc, type, arg0);
9816 else
9818 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9819 This is not the same for NaNs or if signed zeros are
9820 involved. */
9821 if (!HONOR_NANS (arg0)
9822 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9823 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9824 && TREE_CODE (arg1) == COMPLEX_CST
9825 && real_zerop (TREE_REALPART (arg1)))
9827 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9828 if (real_onep (TREE_IMAGPART (arg1)))
9829 return
9830 fold_build2_loc (loc, COMPLEX_EXPR, type,
9831 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9832 rtype, arg0)),
9833 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9834 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9835 return
9836 fold_build2_loc (loc, COMPLEX_EXPR, type,
9837 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9838 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9839 rtype, arg0)));
9842 /* Optimize z * conj(z) for floating point complex numbers.
9843 Guarded by flag_unsafe_math_optimizations as non-finite
9844 imaginary components don't produce scalar results. */
9845 if (flag_unsafe_math_optimizations
9846 && 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 (flag_unsafe_math_optimizations
9850 && TREE_CODE (arg1) == CONJ_EXPR
9851 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9852 return fold_mult_zconjz (loc, type, arg0);
9854 goto associate;
9856 case BIT_IOR_EXPR:
9857 /* Canonicalize (X & C1) | C2. */
9858 if (TREE_CODE (arg0) == BIT_AND_EXPR
9859 && TREE_CODE (arg1) == INTEGER_CST
9860 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9862 int width = TYPE_PRECISION (type), w;
9863 wide_int c1 = TREE_OPERAND (arg0, 1);
9864 wide_int c2 = arg1;
9866 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9867 if ((c1 & c2) == c1)
9868 return omit_one_operand_loc (loc, type, arg1,
9869 TREE_OPERAND (arg0, 0));
9871 wide_int msk = wi::mask (width, false,
9872 TYPE_PRECISION (TREE_TYPE (arg1)));
9874 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9875 if (msk.and_not (c1 | c2) == 0)
9876 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9877 TREE_OPERAND (arg0, 0), arg1);
9879 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9880 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9881 mode which allows further optimizations. */
9882 c1 &= msk;
9883 c2 &= msk;
9884 wide_int c3 = c1.and_not (c2);
9885 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9887 wide_int mask = wi::mask (w, false,
9888 TYPE_PRECISION (type));
9889 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9891 c3 = mask;
9892 break;
9896 if (c3 != c1)
9897 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9898 fold_build2_loc (loc, BIT_AND_EXPR, type,
9899 TREE_OPERAND (arg0, 0),
9900 wide_int_to_tree (type,
9901 c3)),
9902 arg1);
9905 /* See if this can be simplified into a rotate first. If that
9906 is unsuccessful continue in the association code. */
9907 goto bit_rotate;
9909 case BIT_XOR_EXPR:
9910 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9911 if (TREE_CODE (arg0) == BIT_AND_EXPR
9912 && INTEGRAL_TYPE_P (type)
9913 && integer_onep (TREE_OPERAND (arg0, 1))
9914 && integer_onep (arg1))
9915 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9916 build_zero_cst (TREE_TYPE (arg0)));
9918 /* See if this can be simplified into a rotate first. If that
9919 is unsuccessful continue in the association code. */
9920 goto bit_rotate;
9922 case BIT_AND_EXPR:
9923 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9924 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9925 && INTEGRAL_TYPE_P (type)
9926 && integer_onep (TREE_OPERAND (arg0, 1))
9927 && integer_onep (arg1))
9929 tree tem2;
9930 tem = TREE_OPERAND (arg0, 0);
9931 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9932 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9933 tem, tem2);
9934 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9935 build_zero_cst (TREE_TYPE (tem)));
9937 /* Fold ~X & 1 as (X & 1) == 0. */
9938 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9939 && INTEGRAL_TYPE_P (type)
9940 && integer_onep (arg1))
9942 tree tem2;
9943 tem = TREE_OPERAND (arg0, 0);
9944 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9945 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9946 tem, tem2);
9947 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9948 build_zero_cst (TREE_TYPE (tem)));
9950 /* Fold !X & 1 as X == 0. */
9951 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9952 && integer_onep (arg1))
9954 tem = TREE_OPERAND (arg0, 0);
9955 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9956 build_zero_cst (TREE_TYPE (tem)));
9959 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9960 multiple of 1 << CST. */
9961 if (TREE_CODE (arg1) == INTEGER_CST)
9963 wide_int cst1 = arg1;
9964 wide_int ncst1 = -cst1;
9965 if ((cst1 & ncst1) == ncst1
9966 && multiple_of_p (type, arg0,
9967 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
9968 return fold_convert_loc (loc, type, arg0);
9971 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9972 bits from CST2. */
9973 if (TREE_CODE (arg1) == INTEGER_CST
9974 && TREE_CODE (arg0) == MULT_EXPR
9975 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9977 wide_int warg1 = arg1;
9978 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
9980 if (masked == 0)
9981 return omit_two_operands_loc (loc, type, build_zero_cst (type),
9982 arg0, arg1);
9983 else if (masked != warg1)
9985 /* Avoid the transform if arg1 is a mask of some
9986 mode which allows further optimizations. */
9987 int pop = wi::popcount (warg1);
9988 if (!(pop >= BITS_PER_UNIT
9989 && pow2p_hwi (pop)
9990 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
9991 return fold_build2_loc (loc, code, type, op0,
9992 wide_int_to_tree (type, masked));
9996 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
9997 ((A & N) + B) & M -> (A + B) & M
9998 Similarly if (N & M) == 0,
9999 ((A | N) + B) & M -> (A + B) & M
10000 and for - instead of + (or unary - instead of +)
10001 and/or ^ instead of |.
10002 If B is constant and (B & M) == 0, fold into A & M. */
10003 if (TREE_CODE (arg1) == INTEGER_CST)
10005 wide_int cst1 = arg1;
10006 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10007 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10008 && (TREE_CODE (arg0) == PLUS_EXPR
10009 || TREE_CODE (arg0) == MINUS_EXPR
10010 || TREE_CODE (arg0) == NEGATE_EXPR)
10011 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10012 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10014 tree pmop[2];
10015 int which = 0;
10016 wide_int cst0;
10018 /* Now we know that arg0 is (C + D) or (C - D) or
10019 -C and arg1 (M) is == (1LL << cst) - 1.
10020 Store C into PMOP[0] and D into PMOP[1]. */
10021 pmop[0] = TREE_OPERAND (arg0, 0);
10022 pmop[1] = NULL;
10023 if (TREE_CODE (arg0) != NEGATE_EXPR)
10025 pmop[1] = TREE_OPERAND (arg0, 1);
10026 which = 1;
10029 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10030 which = -1;
10032 for (; which >= 0; which--)
10033 switch (TREE_CODE (pmop[which]))
10035 case BIT_AND_EXPR:
10036 case BIT_IOR_EXPR:
10037 case BIT_XOR_EXPR:
10038 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10039 != INTEGER_CST)
10040 break;
10041 cst0 = TREE_OPERAND (pmop[which], 1);
10042 cst0 &= cst1;
10043 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10045 if (cst0 != cst1)
10046 break;
10048 else if (cst0 != 0)
10049 break;
10050 /* If C or D is of the form (A & N) where
10051 (N & M) == M, or of the form (A | N) or
10052 (A ^ N) where (N & M) == 0, replace it with A. */
10053 pmop[which] = TREE_OPERAND (pmop[which], 0);
10054 break;
10055 case INTEGER_CST:
10056 /* If C or D is a N where (N & M) == 0, it can be
10057 omitted (assumed 0). */
10058 if ((TREE_CODE (arg0) == PLUS_EXPR
10059 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10060 && (cst1 & pmop[which]) == 0)
10061 pmop[which] = NULL;
10062 break;
10063 default:
10064 break;
10067 /* Only build anything new if we optimized one or both arguments
10068 above. */
10069 if (pmop[0] != TREE_OPERAND (arg0, 0)
10070 || (TREE_CODE (arg0) != NEGATE_EXPR
10071 && pmop[1] != TREE_OPERAND (arg0, 1)))
10073 tree utype = TREE_TYPE (arg0);
10074 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10076 /* Perform the operations in a type that has defined
10077 overflow behavior. */
10078 utype = unsigned_type_for (TREE_TYPE (arg0));
10079 if (pmop[0] != NULL)
10080 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10081 if (pmop[1] != NULL)
10082 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10085 if (TREE_CODE (arg0) == NEGATE_EXPR)
10086 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10087 else if (TREE_CODE (arg0) == PLUS_EXPR)
10089 if (pmop[0] != NULL && pmop[1] != NULL)
10090 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10091 pmop[0], pmop[1]);
10092 else if (pmop[0] != NULL)
10093 tem = pmop[0];
10094 else if (pmop[1] != NULL)
10095 tem = pmop[1];
10096 else
10097 return build_int_cst (type, 0);
10099 else if (pmop[0] == NULL)
10100 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10101 else
10102 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10103 pmop[0], pmop[1]);
10104 /* TEM is now the new binary +, - or unary - replacement. */
10105 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10106 fold_convert_loc (loc, utype, arg1));
10107 return fold_convert_loc (loc, type, tem);
10112 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10113 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10114 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10116 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10118 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10119 if (mask == -1)
10120 return
10121 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10124 goto associate;
10126 case RDIV_EXPR:
10127 /* Don't touch a floating-point divide by zero unless the mode
10128 of the constant can represent infinity. */
10129 if (TREE_CODE (arg1) == REAL_CST
10130 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10131 && real_zerop (arg1))
10132 return NULL_TREE;
10134 /* (-A) / (-B) -> A / B */
10135 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10136 return fold_build2_loc (loc, RDIV_EXPR, type,
10137 TREE_OPERAND (arg0, 0),
10138 negate_expr (arg1));
10139 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10140 return fold_build2_loc (loc, RDIV_EXPR, type,
10141 negate_expr (arg0),
10142 TREE_OPERAND (arg1, 0));
10143 return NULL_TREE;
10145 case TRUNC_DIV_EXPR:
10146 /* Fall through */
10148 case FLOOR_DIV_EXPR:
10149 /* Simplify A / (B << N) where A and B are positive and B is
10150 a power of 2, to A >> (N + log2(B)). */
10151 strict_overflow_p = false;
10152 if (TREE_CODE (arg1) == LSHIFT_EXPR
10153 && (TYPE_UNSIGNED (type)
10154 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10156 tree sval = TREE_OPERAND (arg1, 0);
10157 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10159 tree sh_cnt = TREE_OPERAND (arg1, 1);
10160 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10161 wi::exact_log2 (sval));
10163 if (strict_overflow_p)
10164 fold_overflow_warning (("assuming signed overflow does not "
10165 "occur when simplifying A / (B << N)"),
10166 WARN_STRICT_OVERFLOW_MISC);
10168 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10169 sh_cnt, pow2);
10170 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10171 fold_convert_loc (loc, type, arg0), sh_cnt);
10175 /* Fall through */
10177 case ROUND_DIV_EXPR:
10178 case CEIL_DIV_EXPR:
10179 case EXACT_DIV_EXPR:
10180 if (integer_zerop (arg1))
10181 return NULL_TREE;
10183 /* Convert -A / -B to A / B when the type is signed and overflow is
10184 undefined. */
10185 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10186 && TREE_CODE (arg0) == NEGATE_EXPR
10187 && negate_expr_p (op1))
10189 if (INTEGRAL_TYPE_P (type))
10190 fold_overflow_warning (("assuming signed overflow does not occur "
10191 "when distributing negation across "
10192 "division"),
10193 WARN_STRICT_OVERFLOW_MISC);
10194 return fold_build2_loc (loc, code, type,
10195 fold_convert_loc (loc, type,
10196 TREE_OPERAND (arg0, 0)),
10197 negate_expr (op1));
10199 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10200 && TREE_CODE (arg1) == NEGATE_EXPR
10201 && negate_expr_p (op0))
10203 if (INTEGRAL_TYPE_P (type))
10204 fold_overflow_warning (("assuming signed overflow does not occur "
10205 "when distributing negation across "
10206 "division"),
10207 WARN_STRICT_OVERFLOW_MISC);
10208 return fold_build2_loc (loc, code, type,
10209 negate_expr (op0),
10210 fold_convert_loc (loc, type,
10211 TREE_OPERAND (arg1, 0)));
10214 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10215 operation, EXACT_DIV_EXPR.
10217 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10218 At one time others generated faster code, it's not clear if they do
10219 after the last round to changes to the DIV code in expmed.c. */
10220 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10221 && multiple_of_p (type, arg0, arg1))
10222 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10223 fold_convert (type, arg0),
10224 fold_convert (type, arg1));
10226 strict_overflow_p = false;
10227 if (TREE_CODE (arg1) == INTEGER_CST
10228 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10229 &strict_overflow_p)))
10231 if (strict_overflow_p)
10232 fold_overflow_warning (("assuming signed overflow does not occur "
10233 "when simplifying division"),
10234 WARN_STRICT_OVERFLOW_MISC);
10235 return fold_convert_loc (loc, type, tem);
10238 return NULL_TREE;
10240 case CEIL_MOD_EXPR:
10241 case FLOOR_MOD_EXPR:
10242 case ROUND_MOD_EXPR:
10243 case TRUNC_MOD_EXPR:
10244 strict_overflow_p = false;
10245 if (TREE_CODE (arg1) == INTEGER_CST
10246 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10247 &strict_overflow_p)))
10249 if (strict_overflow_p)
10250 fold_overflow_warning (("assuming signed overflow does not occur "
10251 "when simplifying modulus"),
10252 WARN_STRICT_OVERFLOW_MISC);
10253 return fold_convert_loc (loc, type, tem);
10256 return NULL_TREE;
10258 case LROTATE_EXPR:
10259 case RROTATE_EXPR:
10260 case RSHIFT_EXPR:
10261 case LSHIFT_EXPR:
10262 /* Since negative shift count is not well-defined,
10263 don't try to compute it in the compiler. */
10264 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10265 return NULL_TREE;
10267 prec = element_precision (type);
10269 /* If we have a rotate of a bit operation with the rotate count and
10270 the second operand of the bit operation both constant,
10271 permute the two operations. */
10272 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10273 && (TREE_CODE (arg0) == BIT_AND_EXPR
10274 || TREE_CODE (arg0) == BIT_IOR_EXPR
10275 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10276 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10278 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10279 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10280 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10281 fold_build2_loc (loc, code, type,
10282 arg00, arg1),
10283 fold_build2_loc (loc, code, type,
10284 arg01, arg1));
10287 /* Two consecutive rotates adding up to the some integer
10288 multiple of the precision of the type can be ignored. */
10289 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10290 && TREE_CODE (arg0) == RROTATE_EXPR
10291 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10292 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10293 prec) == 0)
10294 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10296 return NULL_TREE;
10298 case MIN_EXPR:
10299 case MAX_EXPR:
10300 goto associate;
10302 case TRUTH_ANDIF_EXPR:
10303 /* Note that the operands of this must be ints
10304 and their values must be 0 or 1.
10305 ("true" is a fixed value perhaps depending on the language.) */
10306 /* If first arg is constant zero, return it. */
10307 if (integer_zerop (arg0))
10308 return fold_convert_loc (loc, type, arg0);
10309 /* FALLTHRU */
10310 case TRUTH_AND_EXPR:
10311 /* If either arg is constant true, drop it. */
10312 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10313 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10314 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10315 /* Preserve sequence points. */
10316 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10317 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10318 /* If second arg is constant zero, result is zero, but first arg
10319 must be evaluated. */
10320 if (integer_zerop (arg1))
10321 return omit_one_operand_loc (loc, type, arg1, arg0);
10322 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10323 case will be handled here. */
10324 if (integer_zerop (arg0))
10325 return omit_one_operand_loc (loc, type, arg0, arg1);
10327 /* !X && X is always false. */
10328 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10329 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10330 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10331 /* X && !X is always false. */
10332 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10333 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10334 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10336 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10337 means A >= Y && A != MAX, but in this case we know that
10338 A < X <= MAX. */
10340 if (!TREE_SIDE_EFFECTS (arg0)
10341 && !TREE_SIDE_EFFECTS (arg1))
10343 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10344 if (tem && !operand_equal_p (tem, arg0, 0))
10345 return fold_build2_loc (loc, code, type, tem, arg1);
10347 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10348 if (tem && !operand_equal_p (tem, arg1, 0))
10349 return fold_build2_loc (loc, code, type, arg0, tem);
10352 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10353 != NULL_TREE)
10354 return tem;
10356 return NULL_TREE;
10358 case TRUTH_ORIF_EXPR:
10359 /* Note that the operands of this must be ints
10360 and their values must be 0 or true.
10361 ("true" is a fixed value perhaps depending on the language.) */
10362 /* If first arg is constant true, return it. */
10363 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10364 return fold_convert_loc (loc, type, arg0);
10365 /* FALLTHRU */
10366 case TRUTH_OR_EXPR:
10367 /* If either arg is constant zero, drop it. */
10368 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10369 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10370 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10371 /* Preserve sequence points. */
10372 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10373 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10374 /* If second arg is constant true, result is true, but we must
10375 evaluate first arg. */
10376 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10377 return omit_one_operand_loc (loc, type, arg1, arg0);
10378 /* Likewise for first arg, but note this only occurs here for
10379 TRUTH_OR_EXPR. */
10380 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10381 return omit_one_operand_loc (loc, type, arg0, arg1);
10383 /* !X || X is always true. */
10384 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10385 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10386 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10387 /* X || !X is always true. */
10388 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10389 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10390 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10392 /* (X && !Y) || (!X && Y) is X ^ Y */
10393 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10394 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10396 tree a0, a1, l0, l1, n0, n1;
10398 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10399 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10401 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10402 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10404 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10405 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10407 if ((operand_equal_p (n0, a0, 0)
10408 && operand_equal_p (n1, a1, 0))
10409 || (operand_equal_p (n0, a1, 0)
10410 && operand_equal_p (n1, a0, 0)))
10411 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10414 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10415 != NULL_TREE)
10416 return tem;
10418 return NULL_TREE;
10420 case TRUTH_XOR_EXPR:
10421 /* If the second arg is constant zero, drop it. */
10422 if (integer_zerop (arg1))
10423 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10424 /* If the second arg is constant true, this is a logical inversion. */
10425 if (integer_onep (arg1))
10427 tem = invert_truthvalue_loc (loc, arg0);
10428 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10430 /* Identical arguments cancel to zero. */
10431 if (operand_equal_p (arg0, arg1, 0))
10432 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10434 /* !X ^ X is always true. */
10435 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10436 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10437 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10439 /* X ^ !X is always true. */
10440 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10441 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10442 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10444 return NULL_TREE;
10446 case EQ_EXPR:
10447 case NE_EXPR:
10448 STRIP_NOPS (arg0);
10449 STRIP_NOPS (arg1);
10451 tem = fold_comparison (loc, code, type, op0, op1);
10452 if (tem != NULL_TREE)
10453 return tem;
10455 /* bool_var != 1 becomes !bool_var. */
10456 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10457 && code == NE_EXPR)
10458 return fold_convert_loc (loc, type,
10459 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10460 TREE_TYPE (arg0), arg0));
10462 /* bool_var == 0 becomes !bool_var. */
10463 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10464 && code == EQ_EXPR)
10465 return fold_convert_loc (loc, type,
10466 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10467 TREE_TYPE (arg0), arg0));
10469 /* !exp != 0 becomes !exp */
10470 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10471 && code == NE_EXPR)
10472 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10474 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10475 if ((TREE_CODE (arg0) == PLUS_EXPR
10476 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10477 || TREE_CODE (arg0) == MINUS_EXPR)
10478 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10479 0)),
10480 arg1, 0)
10481 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10482 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10484 tree val = TREE_OPERAND (arg0, 1);
10485 val = fold_build2_loc (loc, code, type, val,
10486 build_int_cst (TREE_TYPE (val), 0));
10487 return omit_two_operands_loc (loc, type, val,
10488 TREE_OPERAND (arg0, 0), arg1);
10491 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10492 if ((TREE_CODE (arg1) == PLUS_EXPR
10493 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10494 || TREE_CODE (arg1) == MINUS_EXPR)
10495 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10496 0)),
10497 arg0, 0)
10498 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10499 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10501 tree val = TREE_OPERAND (arg1, 1);
10502 val = fold_build2_loc (loc, code, type, val,
10503 build_int_cst (TREE_TYPE (val), 0));
10504 return omit_two_operands_loc (loc, type, val,
10505 TREE_OPERAND (arg1, 0), arg0);
10508 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10509 if (TREE_CODE (arg0) == MINUS_EXPR
10510 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10511 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10512 1)),
10513 arg1, 0)
10514 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10515 return omit_two_operands_loc (loc, type,
10516 code == NE_EXPR
10517 ? boolean_true_node : boolean_false_node,
10518 TREE_OPERAND (arg0, 1), arg1);
10520 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10521 if (TREE_CODE (arg1) == MINUS_EXPR
10522 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10523 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10524 1)),
10525 arg0, 0)
10526 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10527 return omit_two_operands_loc (loc, type,
10528 code == NE_EXPR
10529 ? boolean_true_node : boolean_false_node,
10530 TREE_OPERAND (arg1, 1), arg0);
10532 /* If this is an EQ or NE comparison with zero and ARG0 is
10533 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10534 two operations, but the latter can be done in one less insn
10535 on machines that have only two-operand insns or on which a
10536 constant cannot be the first operand. */
10537 if (TREE_CODE (arg0) == BIT_AND_EXPR
10538 && integer_zerop (arg1))
10540 tree arg00 = TREE_OPERAND (arg0, 0);
10541 tree arg01 = TREE_OPERAND (arg0, 1);
10542 if (TREE_CODE (arg00) == LSHIFT_EXPR
10543 && integer_onep (TREE_OPERAND (arg00, 0)))
10545 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10546 arg01, TREE_OPERAND (arg00, 1));
10547 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10548 build_int_cst (TREE_TYPE (arg0), 1));
10549 return fold_build2_loc (loc, code, type,
10550 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10551 arg1);
10553 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10554 && integer_onep (TREE_OPERAND (arg01, 0)))
10556 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10557 arg00, TREE_OPERAND (arg01, 1));
10558 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10559 build_int_cst (TREE_TYPE (arg0), 1));
10560 return fold_build2_loc (loc, code, type,
10561 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10562 arg1);
10566 /* If this is an NE or EQ comparison of zero against the result of a
10567 signed MOD operation whose second operand is a power of 2, make
10568 the MOD operation unsigned since it is simpler and equivalent. */
10569 if (integer_zerop (arg1)
10570 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10571 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10572 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10573 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10574 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10575 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10577 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10578 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10579 fold_convert_loc (loc, newtype,
10580 TREE_OPERAND (arg0, 0)),
10581 fold_convert_loc (loc, newtype,
10582 TREE_OPERAND (arg0, 1)));
10584 return fold_build2_loc (loc, code, type, newmod,
10585 fold_convert_loc (loc, newtype, arg1));
10588 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10589 C1 is a valid shift constant, and C2 is a power of two, i.e.
10590 a single bit. */
10591 if (TREE_CODE (arg0) == BIT_AND_EXPR
10592 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10593 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10594 == INTEGER_CST
10595 && integer_pow2p (TREE_OPERAND (arg0, 1))
10596 && integer_zerop (arg1))
10598 tree itype = TREE_TYPE (arg0);
10599 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10600 prec = TYPE_PRECISION (itype);
10602 /* Check for a valid shift count. */
10603 if (wi::ltu_p (arg001, prec))
10605 tree arg01 = TREE_OPERAND (arg0, 1);
10606 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10607 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10608 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10609 can be rewritten as (X & (C2 << C1)) != 0. */
10610 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10612 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10613 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10614 return fold_build2_loc (loc, code, type, tem,
10615 fold_convert_loc (loc, itype, arg1));
10617 /* Otherwise, for signed (arithmetic) shifts,
10618 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10619 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10620 else if (!TYPE_UNSIGNED (itype))
10621 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10622 arg000, build_int_cst (itype, 0));
10623 /* Otherwise, of unsigned (logical) shifts,
10624 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10625 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10626 else
10627 return omit_one_operand_loc (loc, type,
10628 code == EQ_EXPR ? integer_one_node
10629 : integer_zero_node,
10630 arg000);
10634 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10635 Similarly for NE_EXPR. */
10636 if (TREE_CODE (arg0) == BIT_AND_EXPR
10637 && TREE_CODE (arg1) == INTEGER_CST
10638 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10640 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10641 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10642 TREE_OPERAND (arg0, 1));
10643 tree dandnotc
10644 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10645 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10646 notc);
10647 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10648 if (integer_nonzerop (dandnotc))
10649 return omit_one_operand_loc (loc, type, rslt, arg0);
10652 /* If this is a comparison of a field, we may be able to simplify it. */
10653 if ((TREE_CODE (arg0) == COMPONENT_REF
10654 || TREE_CODE (arg0) == BIT_FIELD_REF)
10655 /* Handle the constant case even without -O
10656 to make sure the warnings are given. */
10657 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10659 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10660 if (t1)
10661 return t1;
10664 /* Optimize comparisons of strlen vs zero to a compare of the
10665 first character of the string vs zero. To wit,
10666 strlen(ptr) == 0 => *ptr == 0
10667 strlen(ptr) != 0 => *ptr != 0
10668 Other cases should reduce to one of these two (or a constant)
10669 due to the return value of strlen being unsigned. */
10670 if (TREE_CODE (arg0) == CALL_EXPR
10671 && integer_zerop (arg1))
10673 tree fndecl = get_callee_fndecl (arg0);
10675 if (fndecl
10676 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10677 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10678 && call_expr_nargs (arg0) == 1
10679 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10681 tree iref = build_fold_indirect_ref_loc (loc,
10682 CALL_EXPR_ARG (arg0, 0));
10683 return fold_build2_loc (loc, code, type, iref,
10684 build_int_cst (TREE_TYPE (iref), 0));
10688 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10689 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10690 if (TREE_CODE (arg0) == RSHIFT_EXPR
10691 && integer_zerop (arg1)
10692 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10694 tree arg00 = TREE_OPERAND (arg0, 0);
10695 tree arg01 = TREE_OPERAND (arg0, 1);
10696 tree itype = TREE_TYPE (arg00);
10697 if (wi::eq_p (arg01, element_precision (itype) - 1))
10699 if (TYPE_UNSIGNED (itype))
10701 itype = signed_type_for (itype);
10702 arg00 = fold_convert_loc (loc, itype, arg00);
10704 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10705 type, arg00, build_zero_cst (itype));
10709 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10710 (X & C) == 0 when C is a single bit. */
10711 if (TREE_CODE (arg0) == BIT_AND_EXPR
10712 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10713 && integer_zerop (arg1)
10714 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10716 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10717 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10718 TREE_OPERAND (arg0, 1));
10719 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10720 type, tem,
10721 fold_convert_loc (loc, TREE_TYPE (arg0),
10722 arg1));
10725 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10726 constant C is a power of two, i.e. a single bit. */
10727 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10728 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10729 && integer_zerop (arg1)
10730 && integer_pow2p (TREE_OPERAND (arg0, 1))
10731 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10732 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10734 tree arg00 = TREE_OPERAND (arg0, 0);
10735 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10736 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10739 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10740 when is C is a power of two, i.e. a single bit. */
10741 if (TREE_CODE (arg0) == BIT_AND_EXPR
10742 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10743 && integer_zerop (arg1)
10744 && integer_pow2p (TREE_OPERAND (arg0, 1))
10745 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10746 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10748 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10749 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10750 arg000, TREE_OPERAND (arg0, 1));
10751 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10752 tem, build_int_cst (TREE_TYPE (tem), 0));
10755 if (integer_zerop (arg1)
10756 && tree_expr_nonzero_p (arg0))
10758 tree res = constant_boolean_node (code==NE_EXPR, type);
10759 return omit_one_operand_loc (loc, type, res, arg0);
10762 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10763 if (TREE_CODE (arg0) == BIT_AND_EXPR
10764 && TREE_CODE (arg1) == BIT_AND_EXPR)
10766 tree arg00 = TREE_OPERAND (arg0, 0);
10767 tree arg01 = TREE_OPERAND (arg0, 1);
10768 tree arg10 = TREE_OPERAND (arg1, 0);
10769 tree arg11 = TREE_OPERAND (arg1, 1);
10770 tree itype = TREE_TYPE (arg0);
10772 if (operand_equal_p (arg01, arg11, 0))
10773 return fold_build2_loc (loc, code, type,
10774 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10775 fold_build2_loc (loc,
10776 BIT_XOR_EXPR, itype,
10777 arg00, arg10),
10778 arg01),
10779 build_zero_cst (itype));
10781 if (operand_equal_p (arg01, arg10, 0))
10782 return fold_build2_loc (loc, code, type,
10783 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10784 fold_build2_loc (loc,
10785 BIT_XOR_EXPR, itype,
10786 arg00, arg11),
10787 arg01),
10788 build_zero_cst (itype));
10790 if (operand_equal_p (arg00, arg11, 0))
10791 return fold_build2_loc (loc, code, type,
10792 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10793 fold_build2_loc (loc,
10794 BIT_XOR_EXPR, itype,
10795 arg01, arg10),
10796 arg00),
10797 build_zero_cst (itype));
10799 if (operand_equal_p (arg00, arg10, 0))
10800 return fold_build2_loc (loc, code, type,
10801 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10802 fold_build2_loc (loc,
10803 BIT_XOR_EXPR, itype,
10804 arg01, arg11),
10805 arg00),
10806 build_zero_cst (itype));
10809 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10810 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10812 tree arg00 = TREE_OPERAND (arg0, 0);
10813 tree arg01 = TREE_OPERAND (arg0, 1);
10814 tree arg10 = TREE_OPERAND (arg1, 0);
10815 tree arg11 = TREE_OPERAND (arg1, 1);
10816 tree itype = TREE_TYPE (arg0);
10818 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10819 operand_equal_p guarantees no side-effects so we don't need
10820 to use omit_one_operand on Z. */
10821 if (operand_equal_p (arg01, arg11, 0))
10822 return fold_build2_loc (loc, code, type, arg00,
10823 fold_convert_loc (loc, TREE_TYPE (arg00),
10824 arg10));
10825 if (operand_equal_p (arg01, arg10, 0))
10826 return fold_build2_loc (loc, code, type, arg00,
10827 fold_convert_loc (loc, TREE_TYPE (arg00),
10828 arg11));
10829 if (operand_equal_p (arg00, arg11, 0))
10830 return fold_build2_loc (loc, code, type, arg01,
10831 fold_convert_loc (loc, TREE_TYPE (arg01),
10832 arg10));
10833 if (operand_equal_p (arg00, arg10, 0))
10834 return fold_build2_loc (loc, code, type, arg01,
10835 fold_convert_loc (loc, TREE_TYPE (arg01),
10836 arg11));
10838 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10839 if (TREE_CODE (arg01) == INTEGER_CST
10840 && TREE_CODE (arg11) == INTEGER_CST)
10842 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10843 fold_convert_loc (loc, itype, arg11));
10844 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10845 return fold_build2_loc (loc, code, type, tem,
10846 fold_convert_loc (loc, itype, arg10));
10850 /* Attempt to simplify equality/inequality comparisons of complex
10851 values. Only lower the comparison if the result is known or
10852 can be simplified to a single scalar comparison. */
10853 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10854 || TREE_CODE (arg0) == COMPLEX_CST)
10855 && (TREE_CODE (arg1) == COMPLEX_EXPR
10856 || TREE_CODE (arg1) == COMPLEX_CST))
10858 tree real0, imag0, real1, imag1;
10859 tree rcond, icond;
10861 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10863 real0 = TREE_OPERAND (arg0, 0);
10864 imag0 = TREE_OPERAND (arg0, 1);
10866 else
10868 real0 = TREE_REALPART (arg0);
10869 imag0 = TREE_IMAGPART (arg0);
10872 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10874 real1 = TREE_OPERAND (arg1, 0);
10875 imag1 = TREE_OPERAND (arg1, 1);
10877 else
10879 real1 = TREE_REALPART (arg1);
10880 imag1 = TREE_IMAGPART (arg1);
10883 rcond = fold_binary_loc (loc, code, type, real0, real1);
10884 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10886 if (integer_zerop (rcond))
10888 if (code == EQ_EXPR)
10889 return omit_two_operands_loc (loc, type, boolean_false_node,
10890 imag0, imag1);
10891 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10893 else
10895 if (code == NE_EXPR)
10896 return omit_two_operands_loc (loc, type, boolean_true_node,
10897 imag0, imag1);
10898 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10902 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10903 if (icond && TREE_CODE (icond) == INTEGER_CST)
10905 if (integer_zerop (icond))
10907 if (code == EQ_EXPR)
10908 return omit_two_operands_loc (loc, type, boolean_false_node,
10909 real0, real1);
10910 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10912 else
10914 if (code == NE_EXPR)
10915 return omit_two_operands_loc (loc, type, boolean_true_node,
10916 real0, real1);
10917 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10922 return NULL_TREE;
10924 case LT_EXPR:
10925 case GT_EXPR:
10926 case LE_EXPR:
10927 case GE_EXPR:
10928 tem = fold_comparison (loc, code, type, op0, op1);
10929 if (tem != NULL_TREE)
10930 return tem;
10932 /* Transform comparisons of the form X +- C CMP X. */
10933 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10934 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10935 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10936 && !HONOR_SNANS (arg0))
10937 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10938 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10940 tree arg01 = TREE_OPERAND (arg0, 1);
10941 enum tree_code code0 = TREE_CODE (arg0);
10942 int is_positive;
10944 if (TREE_CODE (arg01) == REAL_CST)
10945 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10946 else
10947 is_positive = tree_int_cst_sgn (arg01);
10949 /* (X - c) > X becomes false. */
10950 if (code == GT_EXPR
10951 && ((code0 == MINUS_EXPR && is_positive >= 0)
10952 || (code0 == PLUS_EXPR && is_positive <= 0)))
10954 if (TREE_CODE (arg01) == INTEGER_CST
10955 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10956 fold_overflow_warning (("assuming signed overflow does not "
10957 "occur when assuming that (X - c) > X "
10958 "is always false"),
10959 WARN_STRICT_OVERFLOW_ALL);
10960 return constant_boolean_node (0, type);
10963 /* Likewise (X + c) < X becomes false. */
10964 if (code == LT_EXPR
10965 && ((code0 == PLUS_EXPR && is_positive >= 0)
10966 || (code0 == MINUS_EXPR && is_positive <= 0)))
10968 if (TREE_CODE (arg01) == INTEGER_CST
10969 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10970 fold_overflow_warning (("assuming signed overflow does not "
10971 "occur when assuming that "
10972 "(X + c) < X is always false"),
10973 WARN_STRICT_OVERFLOW_ALL);
10974 return constant_boolean_node (0, type);
10977 /* Convert (X - c) <= X to true. */
10978 if (!HONOR_NANS (arg1)
10979 && code == LE_EXPR
10980 && ((code0 == MINUS_EXPR && is_positive >= 0)
10981 || (code0 == PLUS_EXPR && is_positive <= 0)))
10983 if (TREE_CODE (arg01) == INTEGER_CST
10984 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10985 fold_overflow_warning (("assuming signed overflow does not "
10986 "occur when assuming that "
10987 "(X - c) <= X is always true"),
10988 WARN_STRICT_OVERFLOW_ALL);
10989 return constant_boolean_node (1, type);
10992 /* Convert (X + c) >= X to true. */
10993 if (!HONOR_NANS (arg1)
10994 && code == GE_EXPR
10995 && ((code0 == PLUS_EXPR && is_positive >= 0)
10996 || (code0 == MINUS_EXPR && is_positive <= 0)))
10998 if (TREE_CODE (arg01) == INTEGER_CST
10999 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11000 fold_overflow_warning (("assuming signed overflow does not "
11001 "occur when assuming that "
11002 "(X + c) >= X is always true"),
11003 WARN_STRICT_OVERFLOW_ALL);
11004 return constant_boolean_node (1, type);
11007 if (TREE_CODE (arg01) == INTEGER_CST)
11009 /* Convert X + c > X and X - c < X to true for integers. */
11010 if (code == GT_EXPR
11011 && ((code0 == PLUS_EXPR && is_positive > 0)
11012 || (code0 == MINUS_EXPR && is_positive < 0)))
11014 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11015 fold_overflow_warning (("assuming signed overflow does "
11016 "not occur when assuming that "
11017 "(X + c) > X is always true"),
11018 WARN_STRICT_OVERFLOW_ALL);
11019 return constant_boolean_node (1, type);
11022 if (code == LT_EXPR
11023 && ((code0 == MINUS_EXPR && is_positive > 0)
11024 || (code0 == PLUS_EXPR && is_positive < 0)))
11026 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11027 fold_overflow_warning (("assuming signed overflow does "
11028 "not occur when assuming that "
11029 "(X - c) < X is always true"),
11030 WARN_STRICT_OVERFLOW_ALL);
11031 return constant_boolean_node (1, type);
11034 /* Convert X + c <= X and X - c >= X to false for integers. */
11035 if (code == LE_EXPR
11036 && ((code0 == PLUS_EXPR && is_positive > 0)
11037 || (code0 == MINUS_EXPR && is_positive < 0)))
11039 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11040 fold_overflow_warning (("assuming signed overflow does "
11041 "not occur when assuming that "
11042 "(X + c) <= X is always false"),
11043 WARN_STRICT_OVERFLOW_ALL);
11044 return constant_boolean_node (0, type);
11047 if (code == GE_EXPR
11048 && ((code0 == MINUS_EXPR && is_positive > 0)
11049 || (code0 == PLUS_EXPR && is_positive < 0)))
11051 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11052 fold_overflow_warning (("assuming signed overflow does "
11053 "not occur when assuming that "
11054 "(X - c) >= X is always false"),
11055 WARN_STRICT_OVERFLOW_ALL);
11056 return constant_boolean_node (0, type);
11061 /* If we are comparing an ABS_EXPR with a constant, we can
11062 convert all the cases into explicit comparisons, but they may
11063 well not be faster than doing the ABS and one comparison.
11064 But ABS (X) <= C is a range comparison, which becomes a subtraction
11065 and a comparison, and is probably faster. */
11066 if (code == LE_EXPR
11067 && TREE_CODE (arg1) == INTEGER_CST
11068 && TREE_CODE (arg0) == ABS_EXPR
11069 && ! TREE_SIDE_EFFECTS (arg0)
11070 && (0 != (tem = negate_expr (arg1)))
11071 && TREE_CODE (tem) == INTEGER_CST
11072 && !TREE_OVERFLOW (tem))
11073 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11074 build2 (GE_EXPR, type,
11075 TREE_OPERAND (arg0, 0), tem),
11076 build2 (LE_EXPR, type,
11077 TREE_OPERAND (arg0, 0), arg1));
11079 /* Convert ABS_EXPR<x> >= 0 to true. */
11080 strict_overflow_p = false;
11081 if (code == GE_EXPR
11082 && (integer_zerop (arg1)
11083 || (! HONOR_NANS (arg0)
11084 && real_zerop (arg1)))
11085 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11087 if (strict_overflow_p)
11088 fold_overflow_warning (("assuming signed overflow does not occur "
11089 "when simplifying comparison of "
11090 "absolute value and zero"),
11091 WARN_STRICT_OVERFLOW_CONDITIONAL);
11092 return omit_one_operand_loc (loc, type,
11093 constant_boolean_node (true, type),
11094 arg0);
11097 /* Convert ABS_EXPR<x> < 0 to false. */
11098 strict_overflow_p = false;
11099 if (code == LT_EXPR
11100 && (integer_zerop (arg1) || real_zerop (arg1))
11101 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11103 if (strict_overflow_p)
11104 fold_overflow_warning (("assuming signed overflow does not occur "
11105 "when simplifying comparison of "
11106 "absolute value and zero"),
11107 WARN_STRICT_OVERFLOW_CONDITIONAL);
11108 return omit_one_operand_loc (loc, type,
11109 constant_boolean_node (false, type),
11110 arg0);
11113 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11114 and similarly for >= into !=. */
11115 if ((code == LT_EXPR || code == GE_EXPR)
11116 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11117 && TREE_CODE (arg1) == LSHIFT_EXPR
11118 && integer_onep (TREE_OPERAND (arg1, 0)))
11119 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11120 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11121 TREE_OPERAND (arg1, 1)),
11122 build_zero_cst (TREE_TYPE (arg0)));
11124 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11125 otherwise Y might be >= # of bits in X's type and thus e.g.
11126 (unsigned char) (1 << Y) for Y 15 might be 0.
11127 If the cast is widening, then 1 << Y should have unsigned type,
11128 otherwise if Y is number of bits in the signed shift type minus 1,
11129 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11130 31 might be 0xffffffff80000000. */
11131 if ((code == LT_EXPR || code == GE_EXPR)
11132 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11133 && CONVERT_EXPR_P (arg1)
11134 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11135 && (element_precision (TREE_TYPE (arg1))
11136 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11137 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11138 || (element_precision (TREE_TYPE (arg1))
11139 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11140 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11142 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11143 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11144 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11145 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11146 build_zero_cst (TREE_TYPE (arg0)));
11149 return NULL_TREE;
11151 case UNORDERED_EXPR:
11152 case ORDERED_EXPR:
11153 case UNLT_EXPR:
11154 case UNLE_EXPR:
11155 case UNGT_EXPR:
11156 case UNGE_EXPR:
11157 case UNEQ_EXPR:
11158 case LTGT_EXPR:
11159 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11161 tree targ0 = strip_float_extensions (arg0);
11162 tree targ1 = strip_float_extensions (arg1);
11163 tree newtype = TREE_TYPE (targ0);
11165 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11166 newtype = TREE_TYPE (targ1);
11168 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11169 return fold_build2_loc (loc, code, type,
11170 fold_convert_loc (loc, newtype, targ0),
11171 fold_convert_loc (loc, newtype, targ1));
11174 return NULL_TREE;
11176 case COMPOUND_EXPR:
11177 /* When pedantic, a compound expression can be neither an lvalue
11178 nor an integer constant expression. */
11179 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11180 return NULL_TREE;
11181 /* Don't let (0, 0) be null pointer constant. */
11182 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11183 : fold_convert_loc (loc, type, arg1);
11184 return pedantic_non_lvalue_loc (loc, tem);
11186 case ASSERT_EXPR:
11187 /* An ASSERT_EXPR should never be passed to fold_binary. */
11188 gcc_unreachable ();
11190 default:
11191 return NULL_TREE;
11192 } /* switch (code) */
11195 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11196 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11197 of GOTO_EXPR. */
11199 static tree
11200 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11202 switch (TREE_CODE (*tp))
11204 case LABEL_EXPR:
11205 return *tp;
11207 case GOTO_EXPR:
11208 *walk_subtrees = 0;
11210 /* fall through */
11212 default:
11213 return NULL_TREE;
11217 /* Return whether the sub-tree ST contains a label which is accessible from
11218 outside the sub-tree. */
11220 static bool
11221 contains_label_p (tree st)
11223 return
11224 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11227 /* Fold a ternary expression of code CODE and type TYPE with operands
11228 OP0, OP1, and OP2. Return the folded expression if folding is
11229 successful. Otherwise, return NULL_TREE. */
11231 tree
11232 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11233 tree op0, tree op1, tree op2)
11235 tree tem;
11236 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11237 enum tree_code_class kind = TREE_CODE_CLASS (code);
11239 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11240 && TREE_CODE_LENGTH (code) == 3);
11242 /* If this is a commutative operation, and OP0 is a constant, move it
11243 to OP1 to reduce the number of tests below. */
11244 if (commutative_ternary_tree_code (code)
11245 && tree_swap_operands_p (op0, op1))
11246 return fold_build3_loc (loc, code, type, op1, op0, op2);
11248 tem = generic_simplify (loc, code, type, op0, op1, op2);
11249 if (tem)
11250 return tem;
11252 /* Strip any conversions that don't change the mode. This is safe
11253 for every expression, except for a comparison expression because
11254 its signedness is derived from its operands. So, in the latter
11255 case, only strip conversions that don't change the signedness.
11257 Note that this is done as an internal manipulation within the
11258 constant folder, in order to find the simplest representation of
11259 the arguments so that their form can be studied. In any cases,
11260 the appropriate type conversions should be put back in the tree
11261 that will get out of the constant folder. */
11262 if (op0)
11264 arg0 = op0;
11265 STRIP_NOPS (arg0);
11268 if (op1)
11270 arg1 = op1;
11271 STRIP_NOPS (arg1);
11274 if (op2)
11276 arg2 = op2;
11277 STRIP_NOPS (arg2);
11280 switch (code)
11282 case COMPONENT_REF:
11283 if (TREE_CODE (arg0) == CONSTRUCTOR
11284 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11286 unsigned HOST_WIDE_INT idx;
11287 tree field, value;
11288 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11289 if (field == arg1)
11290 return value;
11292 return NULL_TREE;
11294 case COND_EXPR:
11295 case VEC_COND_EXPR:
11296 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11297 so all simple results must be passed through pedantic_non_lvalue. */
11298 if (TREE_CODE (arg0) == INTEGER_CST)
11300 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11301 tem = integer_zerop (arg0) ? op2 : op1;
11302 /* Only optimize constant conditions when the selected branch
11303 has the same type as the COND_EXPR. This avoids optimizing
11304 away "c ? x : throw", where the throw has a void type.
11305 Avoid throwing away that operand which contains label. */
11306 if ((!TREE_SIDE_EFFECTS (unused_op)
11307 || !contains_label_p (unused_op))
11308 && (! VOID_TYPE_P (TREE_TYPE (tem))
11309 || VOID_TYPE_P (type)))
11310 return pedantic_non_lvalue_loc (loc, tem);
11311 return NULL_TREE;
11313 else if (TREE_CODE (arg0) == VECTOR_CST)
11315 if ((TREE_CODE (arg1) == VECTOR_CST
11316 || TREE_CODE (arg1) == CONSTRUCTOR)
11317 && (TREE_CODE (arg2) == VECTOR_CST
11318 || TREE_CODE (arg2) == CONSTRUCTOR))
11320 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11321 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11322 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11323 for (i = 0; i < nelts; i++)
11325 tree val = VECTOR_CST_ELT (arg0, i);
11326 if (integer_all_onesp (val))
11327 sel[i] = i;
11328 else if (integer_zerop (val))
11329 sel[i] = nelts + i;
11330 else /* Currently unreachable. */
11331 return NULL_TREE;
11333 tree t = fold_vec_perm (type, arg1, arg2, sel);
11334 if (t != NULL_TREE)
11335 return t;
11339 /* If we have A op B ? A : C, we may be able to convert this to a
11340 simpler expression, depending on the operation and the values
11341 of B and C. Signed zeros prevent all of these transformations,
11342 for reasons given above each one.
11344 Also try swapping the arguments and inverting the conditional. */
11345 if (COMPARISON_CLASS_P (arg0)
11346 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11347 arg1, TREE_OPERAND (arg0, 1))
11348 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11350 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11351 if (tem)
11352 return tem;
11355 if (COMPARISON_CLASS_P (arg0)
11356 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11357 op2,
11358 TREE_OPERAND (arg0, 1))
11359 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11361 location_t loc0 = expr_location_or (arg0, loc);
11362 tem = fold_invert_truthvalue (loc0, arg0);
11363 if (tem && COMPARISON_CLASS_P (tem))
11365 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11366 if (tem)
11367 return tem;
11371 /* If the second operand is simpler than the third, swap them
11372 since that produces better jump optimization results. */
11373 if (truth_value_p (TREE_CODE (arg0))
11374 && tree_swap_operands_p (op1, op2))
11376 location_t loc0 = expr_location_or (arg0, loc);
11377 /* See if this can be inverted. If it can't, possibly because
11378 it was a floating-point inequality comparison, don't do
11379 anything. */
11380 tem = fold_invert_truthvalue (loc0, arg0);
11381 if (tem)
11382 return fold_build3_loc (loc, code, type, tem, op2, op1);
11385 /* Convert A ? 1 : 0 to simply A. */
11386 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11387 : (integer_onep (op1)
11388 && !VECTOR_TYPE_P (type)))
11389 && integer_zerop (op2)
11390 /* If we try to convert OP0 to our type, the
11391 call to fold will try to move the conversion inside
11392 a COND, which will recurse. In that case, the COND_EXPR
11393 is probably the best choice, so leave it alone. */
11394 && type == TREE_TYPE (arg0))
11395 return pedantic_non_lvalue_loc (loc, arg0);
11397 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11398 over COND_EXPR in cases such as floating point comparisons. */
11399 if (integer_zerop (op1)
11400 && code == COND_EXPR
11401 && integer_onep (op2)
11402 && !VECTOR_TYPE_P (type)
11403 && truth_value_p (TREE_CODE (arg0)))
11404 return pedantic_non_lvalue_loc (loc,
11405 fold_convert_loc (loc, type,
11406 invert_truthvalue_loc (loc,
11407 arg0)));
11409 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11410 if (TREE_CODE (arg0) == LT_EXPR
11411 && integer_zerop (TREE_OPERAND (arg0, 1))
11412 && integer_zerop (op2)
11413 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11415 /* sign_bit_p looks through both zero and sign extensions,
11416 but for this optimization only sign extensions are
11417 usable. */
11418 tree tem2 = TREE_OPERAND (arg0, 0);
11419 while (tem != tem2)
11421 if (TREE_CODE (tem2) != NOP_EXPR
11422 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11424 tem = NULL_TREE;
11425 break;
11427 tem2 = TREE_OPERAND (tem2, 0);
11429 /* sign_bit_p only checks ARG1 bits within A's precision.
11430 If <sign bit of A> has wider type than A, bits outside
11431 of A's precision in <sign bit of A> need to be checked.
11432 If they are all 0, this optimization needs to be done
11433 in unsigned A's type, if they are all 1 in signed A's type,
11434 otherwise this can't be done. */
11435 if (tem
11436 && TYPE_PRECISION (TREE_TYPE (tem))
11437 < TYPE_PRECISION (TREE_TYPE (arg1))
11438 && TYPE_PRECISION (TREE_TYPE (tem))
11439 < TYPE_PRECISION (type))
11441 int inner_width, outer_width;
11442 tree tem_type;
11444 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11445 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11446 if (outer_width > TYPE_PRECISION (type))
11447 outer_width = TYPE_PRECISION (type);
11449 wide_int mask = wi::shifted_mask
11450 (inner_width, outer_width - inner_width, false,
11451 TYPE_PRECISION (TREE_TYPE (arg1)));
11453 wide_int common = mask & arg1;
11454 if (common == mask)
11456 tem_type = signed_type_for (TREE_TYPE (tem));
11457 tem = fold_convert_loc (loc, tem_type, tem);
11459 else if (common == 0)
11461 tem_type = unsigned_type_for (TREE_TYPE (tem));
11462 tem = fold_convert_loc (loc, tem_type, tem);
11464 else
11465 tem = NULL;
11468 if (tem)
11469 return
11470 fold_convert_loc (loc, type,
11471 fold_build2_loc (loc, BIT_AND_EXPR,
11472 TREE_TYPE (tem), tem,
11473 fold_convert_loc (loc,
11474 TREE_TYPE (tem),
11475 arg1)));
11478 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11479 already handled above. */
11480 if (TREE_CODE (arg0) == BIT_AND_EXPR
11481 && integer_onep (TREE_OPERAND (arg0, 1))
11482 && integer_zerop (op2)
11483 && integer_pow2p (arg1))
11485 tree tem = TREE_OPERAND (arg0, 0);
11486 STRIP_NOPS (tem);
11487 if (TREE_CODE (tem) == RSHIFT_EXPR
11488 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11489 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11490 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11491 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11492 TREE_OPERAND (tem, 0), arg1);
11495 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11496 is probably obsolete because the first operand should be a
11497 truth value (that's why we have the two cases above), but let's
11498 leave it in until we can confirm this for all front-ends. */
11499 if (integer_zerop (op2)
11500 && TREE_CODE (arg0) == NE_EXPR
11501 && integer_zerop (TREE_OPERAND (arg0, 1))
11502 && integer_pow2p (arg1)
11503 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11504 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11505 arg1, OEP_ONLY_CONST))
11506 return pedantic_non_lvalue_loc (loc,
11507 fold_convert_loc (loc, type,
11508 TREE_OPERAND (arg0, 0)));
11510 /* Disable the transformations below for vectors, since
11511 fold_binary_op_with_conditional_arg may undo them immediately,
11512 yielding an infinite loop. */
11513 if (code == VEC_COND_EXPR)
11514 return NULL_TREE;
11516 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11517 if (integer_zerop (op2)
11518 && truth_value_p (TREE_CODE (arg0))
11519 && truth_value_p (TREE_CODE (arg1))
11520 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11521 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11522 : TRUTH_ANDIF_EXPR,
11523 type, fold_convert_loc (loc, type, arg0), arg1);
11525 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11526 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11527 && truth_value_p (TREE_CODE (arg0))
11528 && truth_value_p (TREE_CODE (arg1))
11529 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11531 location_t loc0 = expr_location_or (arg0, loc);
11532 /* Only perform transformation if ARG0 is easily inverted. */
11533 tem = fold_invert_truthvalue (loc0, arg0);
11534 if (tem)
11535 return fold_build2_loc (loc, code == VEC_COND_EXPR
11536 ? BIT_IOR_EXPR
11537 : TRUTH_ORIF_EXPR,
11538 type, fold_convert_loc (loc, type, tem),
11539 arg1);
11542 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11543 if (integer_zerop (arg1)
11544 && truth_value_p (TREE_CODE (arg0))
11545 && truth_value_p (TREE_CODE (op2))
11546 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11548 location_t loc0 = expr_location_or (arg0, loc);
11549 /* Only perform transformation if ARG0 is easily inverted. */
11550 tem = fold_invert_truthvalue (loc0, arg0);
11551 if (tem)
11552 return fold_build2_loc (loc, code == VEC_COND_EXPR
11553 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11554 type, fold_convert_loc (loc, type, tem),
11555 op2);
11558 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11559 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11560 && truth_value_p (TREE_CODE (arg0))
11561 && truth_value_p (TREE_CODE (op2))
11562 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11563 return fold_build2_loc (loc, code == VEC_COND_EXPR
11564 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11565 type, fold_convert_loc (loc, type, arg0), op2);
11567 return NULL_TREE;
11569 case CALL_EXPR:
11570 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11571 of fold_ternary on them. */
11572 gcc_unreachable ();
11574 case BIT_FIELD_REF:
11575 if (TREE_CODE (arg0) == VECTOR_CST
11576 && (type == TREE_TYPE (TREE_TYPE (arg0))
11577 || (TREE_CODE (type) == VECTOR_TYPE
11578 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11580 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11581 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11582 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11583 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11585 if (n != 0
11586 && (idx % width) == 0
11587 && (n % width) == 0
11588 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11590 idx = idx / width;
11591 n = n / width;
11593 if (TREE_CODE (arg0) == VECTOR_CST)
11595 if (n == 1)
11596 return VECTOR_CST_ELT (arg0, idx);
11598 tree *vals = XALLOCAVEC (tree, n);
11599 for (unsigned i = 0; i < n; ++i)
11600 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11601 return build_vector (type, vals);
11606 /* On constants we can use native encode/interpret to constant
11607 fold (nearly) all BIT_FIELD_REFs. */
11608 if (CONSTANT_CLASS_P (arg0)
11609 && can_native_interpret_type_p (type)
11610 && BITS_PER_UNIT == 8)
11612 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11613 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11614 /* Limit us to a reasonable amount of work. To relax the
11615 other limitations we need bit-shifting of the buffer
11616 and rounding up the size. */
11617 if (bitpos % BITS_PER_UNIT == 0
11618 && bitsize % BITS_PER_UNIT == 0
11619 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11621 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11622 unsigned HOST_WIDE_INT len
11623 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11624 bitpos / BITS_PER_UNIT);
11625 if (len > 0
11626 && len * BITS_PER_UNIT >= bitsize)
11628 tree v = native_interpret_expr (type, b,
11629 bitsize / BITS_PER_UNIT);
11630 if (v)
11631 return v;
11636 return NULL_TREE;
11638 case FMA_EXPR:
11639 /* For integers we can decompose the FMA if possible. */
11640 if (TREE_CODE (arg0) == INTEGER_CST
11641 && TREE_CODE (arg1) == INTEGER_CST)
11642 return fold_build2_loc (loc, PLUS_EXPR, type,
11643 const_binop (MULT_EXPR, arg0, arg1), arg2);
11644 if (integer_zerop (arg2))
11645 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11647 return fold_fma (loc, type, arg0, arg1, arg2);
11649 case VEC_PERM_EXPR:
11650 if (TREE_CODE (arg2) == VECTOR_CST)
11652 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11653 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11654 unsigned char *sel2 = sel + nelts;
11655 bool need_mask_canon = false;
11656 bool need_mask_canon2 = false;
11657 bool all_in_vec0 = true;
11658 bool all_in_vec1 = true;
11659 bool maybe_identity = true;
11660 bool single_arg = (op0 == op1);
11661 bool changed = false;
11663 mask2 = 2 * nelts - 1;
11664 mask = single_arg ? (nelts - 1) : mask2;
11665 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11666 for (i = 0; i < nelts; i++)
11668 tree val = VECTOR_CST_ELT (arg2, i);
11669 if (TREE_CODE (val) != INTEGER_CST)
11670 return NULL_TREE;
11672 /* Make sure that the perm value is in an acceptable
11673 range. */
11674 wide_int t = val;
11675 need_mask_canon |= wi::gtu_p (t, mask);
11676 need_mask_canon2 |= wi::gtu_p (t, mask2);
11677 sel[i] = t.to_uhwi () & mask;
11678 sel2[i] = t.to_uhwi () & mask2;
11680 if (sel[i] < nelts)
11681 all_in_vec1 = false;
11682 else
11683 all_in_vec0 = false;
11685 if ((sel[i] & (nelts-1)) != i)
11686 maybe_identity = false;
11689 if (maybe_identity)
11691 if (all_in_vec0)
11692 return op0;
11693 if (all_in_vec1)
11694 return op1;
11697 if (all_in_vec0)
11698 op1 = op0;
11699 else if (all_in_vec1)
11701 op0 = op1;
11702 for (i = 0; i < nelts; i++)
11703 sel[i] -= nelts;
11704 need_mask_canon = true;
11707 if ((TREE_CODE (op0) == VECTOR_CST
11708 || TREE_CODE (op0) == CONSTRUCTOR)
11709 && (TREE_CODE (op1) == VECTOR_CST
11710 || TREE_CODE (op1) == CONSTRUCTOR))
11712 tree t = fold_vec_perm (type, op0, op1, sel);
11713 if (t != NULL_TREE)
11714 return t;
11717 if (op0 == op1 && !single_arg)
11718 changed = true;
11720 /* Some targets are deficient and fail to expand a single
11721 argument permutation while still allowing an equivalent
11722 2-argument version. */
11723 if (need_mask_canon && arg2 == op2
11724 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11725 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11727 need_mask_canon = need_mask_canon2;
11728 sel = sel2;
11731 if (need_mask_canon && arg2 == op2)
11733 tree *tsel = XALLOCAVEC (tree, nelts);
11734 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11735 for (i = 0; i < nelts; i++)
11736 tsel[i] = build_int_cst (eltype, sel[i]);
11737 op2 = build_vector (TREE_TYPE (arg2), tsel);
11738 changed = true;
11741 if (changed)
11742 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11744 return NULL_TREE;
11746 case BIT_INSERT_EXPR:
11747 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11748 if (TREE_CODE (arg0) == INTEGER_CST
11749 && TREE_CODE (arg1) == INTEGER_CST)
11751 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11752 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11753 wide_int tem = wi::bit_and (arg0,
11754 wi::shifted_mask (bitpos, bitsize, true,
11755 TYPE_PRECISION (type)));
11756 wide_int tem2
11757 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11758 bitsize), bitpos);
11759 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11761 else if (TREE_CODE (arg0) == VECTOR_CST
11762 && CONSTANT_CLASS_P (arg1)
11763 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11764 TREE_TYPE (arg1)))
11766 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11767 unsigned HOST_WIDE_INT elsize
11768 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11769 if (bitpos % elsize == 0)
11771 unsigned k = bitpos / elsize;
11772 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11773 return arg0;
11774 else
11776 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11777 memcpy (elts, VECTOR_CST_ELTS (arg0),
11778 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11779 elts[k] = arg1;
11780 return build_vector (type, elts);
11784 return NULL_TREE;
11786 default:
11787 return NULL_TREE;
11788 } /* switch (code) */
11791 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11792 of an array (or vector). */
11794 tree
11795 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11797 tree index_type = NULL_TREE;
11798 offset_int low_bound = 0;
11800 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11802 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11803 if (domain_type && TYPE_MIN_VALUE (domain_type))
11805 /* Static constructors for variably sized objects makes no sense. */
11806 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11807 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11808 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11812 if (index_type)
11813 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11814 TYPE_SIGN (index_type));
11816 offset_int index = low_bound - 1;
11817 if (index_type)
11818 index = wi::ext (index, TYPE_PRECISION (index_type),
11819 TYPE_SIGN (index_type));
11821 offset_int max_index;
11822 unsigned HOST_WIDE_INT cnt;
11823 tree cfield, cval;
11825 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11827 /* Array constructor might explicitly set index, or specify a range,
11828 or leave index NULL meaning that it is next index after previous
11829 one. */
11830 if (cfield)
11832 if (TREE_CODE (cfield) == INTEGER_CST)
11833 max_index = index = wi::to_offset (cfield);
11834 else
11836 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11837 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11838 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11841 else
11843 index += 1;
11844 if (index_type)
11845 index = wi::ext (index, TYPE_PRECISION (index_type),
11846 TYPE_SIGN (index_type));
11847 max_index = index;
11850 /* Do we have match? */
11851 if (wi::cmpu (access_index, index) >= 0
11852 && wi::cmpu (access_index, max_index) <= 0)
11853 return cval;
11855 return NULL_TREE;
11858 /* Perform constant folding and related simplification of EXPR.
11859 The related simplifications include x*1 => x, x*0 => 0, etc.,
11860 and application of the associative law.
11861 NOP_EXPR conversions may be removed freely (as long as we
11862 are careful not to change the type of the overall expression).
11863 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11864 but we can constant-fold them if they have constant operands. */
11866 #ifdef ENABLE_FOLD_CHECKING
11867 # define fold(x) fold_1 (x)
11868 static tree fold_1 (tree);
11869 static
11870 #endif
11871 tree
11872 fold (tree expr)
11874 const tree t = expr;
11875 enum tree_code code = TREE_CODE (t);
11876 enum tree_code_class kind = TREE_CODE_CLASS (code);
11877 tree tem;
11878 location_t loc = EXPR_LOCATION (expr);
11880 /* Return right away if a constant. */
11881 if (kind == tcc_constant)
11882 return t;
11884 /* CALL_EXPR-like objects with variable numbers of operands are
11885 treated specially. */
11886 if (kind == tcc_vl_exp)
11888 if (code == CALL_EXPR)
11890 tem = fold_call_expr (loc, expr, false);
11891 return tem ? tem : expr;
11893 return expr;
11896 if (IS_EXPR_CODE_CLASS (kind))
11898 tree type = TREE_TYPE (t);
11899 tree op0, op1, op2;
11901 switch (TREE_CODE_LENGTH (code))
11903 case 1:
11904 op0 = TREE_OPERAND (t, 0);
11905 tem = fold_unary_loc (loc, code, type, op0);
11906 return tem ? tem : expr;
11907 case 2:
11908 op0 = TREE_OPERAND (t, 0);
11909 op1 = TREE_OPERAND (t, 1);
11910 tem = fold_binary_loc (loc, code, type, op0, op1);
11911 return tem ? tem : expr;
11912 case 3:
11913 op0 = TREE_OPERAND (t, 0);
11914 op1 = TREE_OPERAND (t, 1);
11915 op2 = TREE_OPERAND (t, 2);
11916 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11917 return tem ? tem : expr;
11918 default:
11919 break;
11923 switch (code)
11925 case ARRAY_REF:
11927 tree op0 = TREE_OPERAND (t, 0);
11928 tree op1 = TREE_OPERAND (t, 1);
11930 if (TREE_CODE (op1) == INTEGER_CST
11931 && TREE_CODE (op0) == CONSTRUCTOR
11932 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11934 tree val = get_array_ctor_element_at_index (op0,
11935 wi::to_offset (op1));
11936 if (val)
11937 return val;
11940 return t;
11943 /* Return a VECTOR_CST if possible. */
11944 case CONSTRUCTOR:
11946 tree type = TREE_TYPE (t);
11947 if (TREE_CODE (type) != VECTOR_TYPE)
11948 return t;
11950 unsigned i;
11951 tree val;
11952 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11953 if (! CONSTANT_CLASS_P (val))
11954 return t;
11956 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11959 case CONST_DECL:
11960 return fold (DECL_INITIAL (t));
11962 default:
11963 return t;
11964 } /* switch (code) */
11967 #ifdef ENABLE_FOLD_CHECKING
11968 #undef fold
11970 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11971 hash_table<nofree_ptr_hash<const tree_node> > *);
11972 static void fold_check_failed (const_tree, const_tree);
11973 void print_fold_checksum (const_tree);
11975 /* When --enable-checking=fold, compute a digest of expr before
11976 and after actual fold call to see if fold did not accidentally
11977 change original expr. */
11979 tree
11980 fold (tree expr)
11982 tree ret;
11983 struct md5_ctx ctx;
11984 unsigned char checksum_before[16], checksum_after[16];
11985 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11987 md5_init_ctx (&ctx);
11988 fold_checksum_tree (expr, &ctx, &ht);
11989 md5_finish_ctx (&ctx, checksum_before);
11990 ht.empty ();
11992 ret = fold_1 (expr);
11994 md5_init_ctx (&ctx);
11995 fold_checksum_tree (expr, &ctx, &ht);
11996 md5_finish_ctx (&ctx, checksum_after);
11998 if (memcmp (checksum_before, checksum_after, 16))
11999 fold_check_failed (expr, ret);
12001 return ret;
12004 void
12005 print_fold_checksum (const_tree expr)
12007 struct md5_ctx ctx;
12008 unsigned char checksum[16], cnt;
12009 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12011 md5_init_ctx (&ctx);
12012 fold_checksum_tree (expr, &ctx, &ht);
12013 md5_finish_ctx (&ctx, checksum);
12014 for (cnt = 0; cnt < 16; ++cnt)
12015 fprintf (stderr, "%02x", checksum[cnt]);
12016 putc ('\n', stderr);
12019 static void
12020 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12022 internal_error ("fold check: original tree changed by fold");
12025 static void
12026 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12027 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12029 const tree_node **slot;
12030 enum tree_code code;
12031 union tree_node buf;
12032 int i, len;
12034 recursive_label:
12035 if (expr == NULL)
12036 return;
12037 slot = ht->find_slot (expr, INSERT);
12038 if (*slot != NULL)
12039 return;
12040 *slot = expr;
12041 code = TREE_CODE (expr);
12042 if (TREE_CODE_CLASS (code) == tcc_declaration
12043 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12045 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12046 memcpy ((char *) &buf, expr, tree_size (expr));
12047 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12048 buf.decl_with_vis.symtab_node = NULL;
12049 expr = (tree) &buf;
12051 else if (TREE_CODE_CLASS (code) == tcc_type
12052 && (TYPE_POINTER_TO (expr)
12053 || TYPE_REFERENCE_TO (expr)
12054 || TYPE_CACHED_VALUES_P (expr)
12055 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12056 || TYPE_NEXT_VARIANT (expr)
12057 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12059 /* Allow these fields to be modified. */
12060 tree tmp;
12061 memcpy ((char *) &buf, expr, tree_size (expr));
12062 expr = tmp = (tree) &buf;
12063 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12064 TYPE_POINTER_TO (tmp) = NULL;
12065 TYPE_REFERENCE_TO (tmp) = NULL;
12066 TYPE_NEXT_VARIANT (tmp) = NULL;
12067 TYPE_ALIAS_SET (tmp) = -1;
12068 if (TYPE_CACHED_VALUES_P (tmp))
12070 TYPE_CACHED_VALUES_P (tmp) = 0;
12071 TYPE_CACHED_VALUES (tmp) = NULL;
12074 md5_process_bytes (expr, tree_size (expr), ctx);
12075 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12076 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12077 if (TREE_CODE_CLASS (code) != tcc_type
12078 && TREE_CODE_CLASS (code) != tcc_declaration
12079 && code != TREE_LIST
12080 && code != SSA_NAME
12081 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12082 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12083 switch (TREE_CODE_CLASS (code))
12085 case tcc_constant:
12086 switch (code)
12088 case STRING_CST:
12089 md5_process_bytes (TREE_STRING_POINTER (expr),
12090 TREE_STRING_LENGTH (expr), ctx);
12091 break;
12092 case COMPLEX_CST:
12093 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12094 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12095 break;
12096 case VECTOR_CST:
12097 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12098 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12099 break;
12100 default:
12101 break;
12103 break;
12104 case tcc_exceptional:
12105 switch (code)
12107 case TREE_LIST:
12108 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12109 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12110 expr = TREE_CHAIN (expr);
12111 goto recursive_label;
12112 break;
12113 case TREE_VEC:
12114 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12115 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12116 break;
12117 default:
12118 break;
12120 break;
12121 case tcc_expression:
12122 case tcc_reference:
12123 case tcc_comparison:
12124 case tcc_unary:
12125 case tcc_binary:
12126 case tcc_statement:
12127 case tcc_vl_exp:
12128 len = TREE_OPERAND_LENGTH (expr);
12129 for (i = 0; i < len; ++i)
12130 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12131 break;
12132 case tcc_declaration:
12133 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12134 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12135 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12137 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12138 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12139 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12140 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12141 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12144 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12146 if (TREE_CODE (expr) == FUNCTION_DECL)
12148 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12149 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12151 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12153 break;
12154 case tcc_type:
12155 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12156 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12157 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12158 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12159 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12160 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12161 if (INTEGRAL_TYPE_P (expr)
12162 || SCALAR_FLOAT_TYPE_P (expr))
12164 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12165 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12167 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12168 if (TREE_CODE (expr) == RECORD_TYPE
12169 || TREE_CODE (expr) == UNION_TYPE
12170 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12171 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12172 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12173 break;
12174 default:
12175 break;
12179 /* Helper function for outputting the checksum of a tree T. When
12180 debugging with gdb, you can "define mynext" to be "next" followed
12181 by "call debug_fold_checksum (op0)", then just trace down till the
12182 outputs differ. */
12184 DEBUG_FUNCTION void
12185 debug_fold_checksum (const_tree t)
12187 int i;
12188 unsigned char checksum[16];
12189 struct md5_ctx ctx;
12190 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12192 md5_init_ctx (&ctx);
12193 fold_checksum_tree (t, &ctx, &ht);
12194 md5_finish_ctx (&ctx, checksum);
12195 ht.empty ();
12197 for (i = 0; i < 16; i++)
12198 fprintf (stderr, "%d ", checksum[i]);
12200 fprintf (stderr, "\n");
12203 #endif
12205 /* Fold a unary tree expression with code CODE of type TYPE with an
12206 operand OP0. LOC is the location of the resulting expression.
12207 Return a folded expression if successful. Otherwise, return a tree
12208 expression with code CODE of type TYPE with an operand OP0. */
12210 tree
12211 fold_build1_stat_loc (location_t loc,
12212 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12214 tree tem;
12215 #ifdef ENABLE_FOLD_CHECKING
12216 unsigned char checksum_before[16], checksum_after[16];
12217 struct md5_ctx ctx;
12218 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12220 md5_init_ctx (&ctx);
12221 fold_checksum_tree (op0, &ctx, &ht);
12222 md5_finish_ctx (&ctx, checksum_before);
12223 ht.empty ();
12224 #endif
12226 tem = fold_unary_loc (loc, code, type, op0);
12227 if (!tem)
12228 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12230 #ifdef ENABLE_FOLD_CHECKING
12231 md5_init_ctx (&ctx);
12232 fold_checksum_tree (op0, &ctx, &ht);
12233 md5_finish_ctx (&ctx, checksum_after);
12235 if (memcmp (checksum_before, checksum_after, 16))
12236 fold_check_failed (op0, tem);
12237 #endif
12238 return tem;
12241 /* Fold a binary tree expression with code CODE of type TYPE with
12242 operands OP0 and OP1. LOC is the location of the resulting
12243 expression. Return a folded expression if successful. Otherwise,
12244 return a tree expression with code CODE of type TYPE with operands
12245 OP0 and OP1. */
12247 tree
12248 fold_build2_stat_loc (location_t loc,
12249 enum tree_code code, tree type, tree op0, tree op1
12250 MEM_STAT_DECL)
12252 tree tem;
12253 #ifdef ENABLE_FOLD_CHECKING
12254 unsigned char checksum_before_op0[16],
12255 checksum_before_op1[16],
12256 checksum_after_op0[16],
12257 checksum_after_op1[16];
12258 struct md5_ctx ctx;
12259 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12261 md5_init_ctx (&ctx);
12262 fold_checksum_tree (op0, &ctx, &ht);
12263 md5_finish_ctx (&ctx, checksum_before_op0);
12264 ht.empty ();
12266 md5_init_ctx (&ctx);
12267 fold_checksum_tree (op1, &ctx, &ht);
12268 md5_finish_ctx (&ctx, checksum_before_op1);
12269 ht.empty ();
12270 #endif
12272 tem = fold_binary_loc (loc, code, type, op0, op1);
12273 if (!tem)
12274 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12276 #ifdef ENABLE_FOLD_CHECKING
12277 md5_init_ctx (&ctx);
12278 fold_checksum_tree (op0, &ctx, &ht);
12279 md5_finish_ctx (&ctx, checksum_after_op0);
12280 ht.empty ();
12282 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12283 fold_check_failed (op0, tem);
12285 md5_init_ctx (&ctx);
12286 fold_checksum_tree (op1, &ctx, &ht);
12287 md5_finish_ctx (&ctx, checksum_after_op1);
12289 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12290 fold_check_failed (op1, tem);
12291 #endif
12292 return tem;
12295 /* Fold a ternary tree expression with code CODE of type TYPE with
12296 operands OP0, OP1, and OP2. Return a folded expression if
12297 successful. Otherwise, return a tree expression with code CODE of
12298 type TYPE with operands OP0, OP1, and OP2. */
12300 tree
12301 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12302 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12304 tree tem;
12305 #ifdef ENABLE_FOLD_CHECKING
12306 unsigned char checksum_before_op0[16],
12307 checksum_before_op1[16],
12308 checksum_before_op2[16],
12309 checksum_after_op0[16],
12310 checksum_after_op1[16],
12311 checksum_after_op2[16];
12312 struct md5_ctx ctx;
12313 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12315 md5_init_ctx (&ctx);
12316 fold_checksum_tree (op0, &ctx, &ht);
12317 md5_finish_ctx (&ctx, checksum_before_op0);
12318 ht.empty ();
12320 md5_init_ctx (&ctx);
12321 fold_checksum_tree (op1, &ctx, &ht);
12322 md5_finish_ctx (&ctx, checksum_before_op1);
12323 ht.empty ();
12325 md5_init_ctx (&ctx);
12326 fold_checksum_tree (op2, &ctx, &ht);
12327 md5_finish_ctx (&ctx, checksum_before_op2);
12328 ht.empty ();
12329 #endif
12331 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12332 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12333 if (!tem)
12334 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12336 #ifdef ENABLE_FOLD_CHECKING
12337 md5_init_ctx (&ctx);
12338 fold_checksum_tree (op0, &ctx, &ht);
12339 md5_finish_ctx (&ctx, checksum_after_op0);
12340 ht.empty ();
12342 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12343 fold_check_failed (op0, tem);
12345 md5_init_ctx (&ctx);
12346 fold_checksum_tree (op1, &ctx, &ht);
12347 md5_finish_ctx (&ctx, checksum_after_op1);
12348 ht.empty ();
12350 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12351 fold_check_failed (op1, tem);
12353 md5_init_ctx (&ctx);
12354 fold_checksum_tree (op2, &ctx, &ht);
12355 md5_finish_ctx (&ctx, checksum_after_op2);
12357 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12358 fold_check_failed (op2, tem);
12359 #endif
12360 return tem;
12363 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12364 arguments in ARGARRAY, and a null static chain.
12365 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12366 of type TYPE from the given operands as constructed by build_call_array. */
12368 tree
12369 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12370 int nargs, tree *argarray)
12372 tree tem;
12373 #ifdef ENABLE_FOLD_CHECKING
12374 unsigned char checksum_before_fn[16],
12375 checksum_before_arglist[16],
12376 checksum_after_fn[16],
12377 checksum_after_arglist[16];
12378 struct md5_ctx ctx;
12379 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12380 int i;
12382 md5_init_ctx (&ctx);
12383 fold_checksum_tree (fn, &ctx, &ht);
12384 md5_finish_ctx (&ctx, checksum_before_fn);
12385 ht.empty ();
12387 md5_init_ctx (&ctx);
12388 for (i = 0; i < nargs; i++)
12389 fold_checksum_tree (argarray[i], &ctx, &ht);
12390 md5_finish_ctx (&ctx, checksum_before_arglist);
12391 ht.empty ();
12392 #endif
12394 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12395 if (!tem)
12396 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12398 #ifdef ENABLE_FOLD_CHECKING
12399 md5_init_ctx (&ctx);
12400 fold_checksum_tree (fn, &ctx, &ht);
12401 md5_finish_ctx (&ctx, checksum_after_fn);
12402 ht.empty ();
12404 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12405 fold_check_failed (fn, tem);
12407 md5_init_ctx (&ctx);
12408 for (i = 0; i < nargs; i++)
12409 fold_checksum_tree (argarray[i], &ctx, &ht);
12410 md5_finish_ctx (&ctx, checksum_after_arglist);
12412 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12413 fold_check_failed (NULL_TREE, tem);
12414 #endif
12415 return tem;
12418 /* Perform constant folding and related simplification of initializer
12419 expression EXPR. These behave identically to "fold_buildN" but ignore
12420 potential run-time traps and exceptions that fold must preserve. */
12422 #define START_FOLD_INIT \
12423 int saved_signaling_nans = flag_signaling_nans;\
12424 int saved_trapping_math = flag_trapping_math;\
12425 int saved_rounding_math = flag_rounding_math;\
12426 int saved_trapv = flag_trapv;\
12427 int saved_folding_initializer = folding_initializer;\
12428 flag_signaling_nans = 0;\
12429 flag_trapping_math = 0;\
12430 flag_rounding_math = 0;\
12431 flag_trapv = 0;\
12432 folding_initializer = 1;
12434 #define END_FOLD_INIT \
12435 flag_signaling_nans = saved_signaling_nans;\
12436 flag_trapping_math = saved_trapping_math;\
12437 flag_rounding_math = saved_rounding_math;\
12438 flag_trapv = saved_trapv;\
12439 folding_initializer = saved_folding_initializer;
12441 tree
12442 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12443 tree type, tree op)
12445 tree result;
12446 START_FOLD_INIT;
12448 result = fold_build1_loc (loc, code, type, op);
12450 END_FOLD_INIT;
12451 return result;
12454 tree
12455 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12456 tree type, tree op0, tree op1)
12458 tree result;
12459 START_FOLD_INIT;
12461 result = fold_build2_loc (loc, code, type, op0, op1);
12463 END_FOLD_INIT;
12464 return result;
12467 tree
12468 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12469 int nargs, tree *argarray)
12471 tree result;
12472 START_FOLD_INIT;
12474 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12476 END_FOLD_INIT;
12477 return result;
12480 #undef START_FOLD_INIT
12481 #undef END_FOLD_INIT
12483 /* Determine if first argument is a multiple of second argument. Return 0 if
12484 it is not, or we cannot easily determined it to be.
12486 An example of the sort of thing we care about (at this point; this routine
12487 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12488 fold cases do now) is discovering that
12490 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12492 is a multiple of
12494 SAVE_EXPR (J * 8)
12496 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12498 This code also handles discovering that
12500 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12502 is a multiple of 8 so we don't have to worry about dealing with a
12503 possible remainder.
12505 Note that we *look* inside a SAVE_EXPR only to determine how it was
12506 calculated; it is not safe for fold to do much of anything else with the
12507 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12508 at run time. For example, the latter example above *cannot* be implemented
12509 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12510 evaluation time of the original SAVE_EXPR is not necessarily the same at
12511 the time the new expression is evaluated. The only optimization of this
12512 sort that would be valid is changing
12514 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12516 divided by 8 to
12518 SAVE_EXPR (I) * SAVE_EXPR (J)
12520 (where the same SAVE_EXPR (J) is used in the original and the
12521 transformed version). */
12524 multiple_of_p (tree type, const_tree top, const_tree bottom)
12526 gimple *stmt;
12527 tree t1, op1, op2;
12529 if (operand_equal_p (top, bottom, 0))
12530 return 1;
12532 if (TREE_CODE (type) != INTEGER_TYPE)
12533 return 0;
12535 switch (TREE_CODE (top))
12537 case BIT_AND_EXPR:
12538 /* Bitwise and provides a power of two multiple. If the mask is
12539 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12540 if (!integer_pow2p (bottom))
12541 return 0;
12542 /* FALLTHRU */
12544 case MULT_EXPR:
12545 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12546 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12548 case MINUS_EXPR:
12549 /* It is impossible to prove if op0 - op1 is multiple of bottom
12550 precisely, so be conservative here checking if both op0 and op1
12551 are multiple of bottom. Note we check the second operand first
12552 since it's usually simpler. */
12553 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12554 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12556 case PLUS_EXPR:
12557 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12558 as op0 - 3 if the expression has unsigned type. For example,
12559 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12560 op1 = TREE_OPERAND (top, 1);
12561 if (TYPE_UNSIGNED (type)
12562 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12563 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12564 return (multiple_of_p (type, op1, bottom)
12565 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12567 case LSHIFT_EXPR:
12568 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12570 op1 = TREE_OPERAND (top, 1);
12571 /* const_binop may not detect overflow correctly,
12572 so check for it explicitly here. */
12573 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12574 && 0 != (t1 = fold_convert (type,
12575 const_binop (LSHIFT_EXPR,
12576 size_one_node,
12577 op1)))
12578 && !TREE_OVERFLOW (t1))
12579 return multiple_of_p (type, t1, bottom);
12581 return 0;
12583 case NOP_EXPR:
12584 /* Can't handle conversions from non-integral or wider integral type. */
12585 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12586 || (TYPE_PRECISION (type)
12587 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12588 return 0;
12590 /* fall through */
12592 case SAVE_EXPR:
12593 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12595 case COND_EXPR:
12596 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12597 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12599 case INTEGER_CST:
12600 if (TREE_CODE (bottom) != INTEGER_CST
12601 || integer_zerop (bottom)
12602 || (TYPE_UNSIGNED (type)
12603 && (tree_int_cst_sgn (top) < 0
12604 || tree_int_cst_sgn (bottom) < 0)))
12605 return 0;
12606 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12607 SIGNED);
12609 case SSA_NAME:
12610 if (TREE_CODE (bottom) == INTEGER_CST
12611 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12612 && gimple_code (stmt) == GIMPLE_ASSIGN)
12614 enum tree_code code = gimple_assign_rhs_code (stmt);
12616 /* Check for special cases to see if top is defined as multiple
12617 of bottom:
12619 top = (X & ~(bottom - 1) ; bottom is power of 2
12623 Y = X % bottom
12624 top = X - Y. */
12625 if (code == BIT_AND_EXPR
12626 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12627 && TREE_CODE (op2) == INTEGER_CST
12628 && integer_pow2p (bottom)
12629 && wi::multiple_of_p (wi::to_widest (op2),
12630 wi::to_widest (bottom), UNSIGNED))
12631 return 1;
12633 op1 = gimple_assign_rhs1 (stmt);
12634 if (code == MINUS_EXPR
12635 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12636 && TREE_CODE (op2) == SSA_NAME
12637 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12638 && gimple_code (stmt) == GIMPLE_ASSIGN
12639 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12640 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12641 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12642 return 1;
12645 /* fall through */
12647 default:
12648 return 0;
12652 #define tree_expr_nonnegative_warnv_p(X, Y) \
12653 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12655 #define RECURSE(X) \
12656 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12658 /* Return true if CODE or TYPE is known to be non-negative. */
12660 static bool
12661 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12663 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12664 && truth_value_p (code))
12665 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12666 have a signed:1 type (where the value is -1 and 0). */
12667 return true;
12668 return false;
12671 /* Return true if (CODE OP0) is known to be non-negative. If the return
12672 value is based on the assumption that signed overflow is undefined,
12673 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12674 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12676 bool
12677 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12678 bool *strict_overflow_p, int depth)
12680 if (TYPE_UNSIGNED (type))
12681 return true;
12683 switch (code)
12685 case ABS_EXPR:
12686 /* We can't return 1 if flag_wrapv is set because
12687 ABS_EXPR<INT_MIN> = INT_MIN. */
12688 if (!ANY_INTEGRAL_TYPE_P (type))
12689 return true;
12690 if (TYPE_OVERFLOW_UNDEFINED (type))
12692 *strict_overflow_p = true;
12693 return true;
12695 break;
12697 case NON_LVALUE_EXPR:
12698 case FLOAT_EXPR:
12699 case FIX_TRUNC_EXPR:
12700 return RECURSE (op0);
12702 CASE_CONVERT:
12704 tree inner_type = TREE_TYPE (op0);
12705 tree outer_type = type;
12707 if (TREE_CODE (outer_type) == REAL_TYPE)
12709 if (TREE_CODE (inner_type) == REAL_TYPE)
12710 return RECURSE (op0);
12711 if (INTEGRAL_TYPE_P (inner_type))
12713 if (TYPE_UNSIGNED (inner_type))
12714 return true;
12715 return RECURSE (op0);
12718 else if (INTEGRAL_TYPE_P (outer_type))
12720 if (TREE_CODE (inner_type) == REAL_TYPE)
12721 return RECURSE (op0);
12722 if (INTEGRAL_TYPE_P (inner_type))
12723 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12724 && TYPE_UNSIGNED (inner_type);
12727 break;
12729 default:
12730 return tree_simple_nonnegative_warnv_p (code, type);
12733 /* We don't know sign of `t', so be conservative and return false. */
12734 return false;
12737 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12738 value is based on the assumption that signed overflow is undefined,
12739 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12740 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12742 bool
12743 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12744 tree op1, bool *strict_overflow_p,
12745 int depth)
12747 if (TYPE_UNSIGNED (type))
12748 return true;
12750 switch (code)
12752 case POINTER_PLUS_EXPR:
12753 case PLUS_EXPR:
12754 if (FLOAT_TYPE_P (type))
12755 return RECURSE (op0) && RECURSE (op1);
12757 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12758 both unsigned and at least 2 bits shorter than the result. */
12759 if (TREE_CODE (type) == INTEGER_TYPE
12760 && TREE_CODE (op0) == NOP_EXPR
12761 && TREE_CODE (op1) == NOP_EXPR)
12763 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12764 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12765 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12766 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12768 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12769 TYPE_PRECISION (inner2)) + 1;
12770 return prec < TYPE_PRECISION (type);
12773 break;
12775 case MULT_EXPR:
12776 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12778 /* x * x is always non-negative for floating point x
12779 or without overflow. */
12780 if (operand_equal_p (op0, op1, 0)
12781 || (RECURSE (op0) && RECURSE (op1)))
12783 if (ANY_INTEGRAL_TYPE_P (type)
12784 && TYPE_OVERFLOW_UNDEFINED (type))
12785 *strict_overflow_p = true;
12786 return true;
12790 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12791 both unsigned and their total bits is shorter than the result. */
12792 if (TREE_CODE (type) == INTEGER_TYPE
12793 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12794 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12796 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12797 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12798 : TREE_TYPE (op0);
12799 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12800 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12801 : TREE_TYPE (op1);
12803 bool unsigned0 = TYPE_UNSIGNED (inner0);
12804 bool unsigned1 = TYPE_UNSIGNED (inner1);
12806 if (TREE_CODE (op0) == INTEGER_CST)
12807 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12809 if (TREE_CODE (op1) == INTEGER_CST)
12810 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12812 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12813 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12815 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12816 ? tree_int_cst_min_precision (op0, UNSIGNED)
12817 : TYPE_PRECISION (inner0);
12819 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12820 ? tree_int_cst_min_precision (op1, UNSIGNED)
12821 : TYPE_PRECISION (inner1);
12823 return precision0 + precision1 < TYPE_PRECISION (type);
12826 return false;
12828 case BIT_AND_EXPR:
12829 case MAX_EXPR:
12830 return RECURSE (op0) || RECURSE (op1);
12832 case BIT_IOR_EXPR:
12833 case BIT_XOR_EXPR:
12834 case MIN_EXPR:
12835 case RDIV_EXPR:
12836 case TRUNC_DIV_EXPR:
12837 case CEIL_DIV_EXPR:
12838 case FLOOR_DIV_EXPR:
12839 case ROUND_DIV_EXPR:
12840 return RECURSE (op0) && RECURSE (op1);
12842 case TRUNC_MOD_EXPR:
12843 return RECURSE (op0);
12845 case FLOOR_MOD_EXPR:
12846 return RECURSE (op1);
12848 case CEIL_MOD_EXPR:
12849 case ROUND_MOD_EXPR:
12850 default:
12851 return tree_simple_nonnegative_warnv_p (code, type);
12854 /* We don't know sign of `t', so be conservative and return false. */
12855 return false;
12858 /* Return true if T is known to be non-negative. If the return
12859 value is based on the assumption that signed overflow is undefined,
12860 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12861 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12863 bool
12864 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12866 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12867 return true;
12869 switch (TREE_CODE (t))
12871 case INTEGER_CST:
12872 return tree_int_cst_sgn (t) >= 0;
12874 case REAL_CST:
12875 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12877 case FIXED_CST:
12878 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12880 case COND_EXPR:
12881 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12883 case SSA_NAME:
12884 /* Limit the depth of recursion to avoid quadratic behavior.
12885 This is expected to catch almost all occurrences in practice.
12886 If this code misses important cases that unbounded recursion
12887 would not, passes that need this information could be revised
12888 to provide it through dataflow propagation. */
12889 return (!name_registered_for_update_p (t)
12890 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12891 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12892 strict_overflow_p, depth));
12894 default:
12895 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12899 /* Return true if T is known to be non-negative. If the return
12900 value is based on the assumption that signed overflow is undefined,
12901 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12902 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12904 bool
12905 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12906 bool *strict_overflow_p, int depth)
12908 switch (fn)
12910 CASE_CFN_ACOS:
12911 CASE_CFN_ACOSH:
12912 CASE_CFN_CABS:
12913 CASE_CFN_COSH:
12914 CASE_CFN_ERFC:
12915 CASE_CFN_EXP:
12916 CASE_CFN_EXP10:
12917 CASE_CFN_EXP2:
12918 CASE_CFN_FABS:
12919 CASE_CFN_FDIM:
12920 CASE_CFN_HYPOT:
12921 CASE_CFN_POW10:
12922 CASE_CFN_FFS:
12923 CASE_CFN_PARITY:
12924 CASE_CFN_POPCOUNT:
12925 CASE_CFN_CLZ:
12926 CASE_CFN_CLRSB:
12927 case CFN_BUILT_IN_BSWAP32:
12928 case CFN_BUILT_IN_BSWAP64:
12929 /* Always true. */
12930 return true;
12932 CASE_CFN_SQRT:
12933 /* sqrt(-0.0) is -0.0. */
12934 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12935 return true;
12936 return RECURSE (arg0);
12938 CASE_CFN_ASINH:
12939 CASE_CFN_ATAN:
12940 CASE_CFN_ATANH:
12941 CASE_CFN_CBRT:
12942 CASE_CFN_CEIL:
12943 CASE_CFN_ERF:
12944 CASE_CFN_EXPM1:
12945 CASE_CFN_FLOOR:
12946 CASE_CFN_FMOD:
12947 CASE_CFN_FREXP:
12948 CASE_CFN_ICEIL:
12949 CASE_CFN_IFLOOR:
12950 CASE_CFN_IRINT:
12951 CASE_CFN_IROUND:
12952 CASE_CFN_LCEIL:
12953 CASE_CFN_LDEXP:
12954 CASE_CFN_LFLOOR:
12955 CASE_CFN_LLCEIL:
12956 CASE_CFN_LLFLOOR:
12957 CASE_CFN_LLRINT:
12958 CASE_CFN_LLROUND:
12959 CASE_CFN_LRINT:
12960 CASE_CFN_LROUND:
12961 CASE_CFN_MODF:
12962 CASE_CFN_NEARBYINT:
12963 CASE_CFN_RINT:
12964 CASE_CFN_ROUND:
12965 CASE_CFN_SCALB:
12966 CASE_CFN_SCALBLN:
12967 CASE_CFN_SCALBN:
12968 CASE_CFN_SIGNBIT:
12969 CASE_CFN_SIGNIFICAND:
12970 CASE_CFN_SINH:
12971 CASE_CFN_TANH:
12972 CASE_CFN_TRUNC:
12973 /* True if the 1st argument is nonnegative. */
12974 return RECURSE (arg0);
12976 CASE_CFN_FMAX:
12977 /* True if the 1st OR 2nd arguments are nonnegative. */
12978 return RECURSE (arg0) || RECURSE (arg1);
12980 CASE_CFN_FMIN:
12981 /* True if the 1st AND 2nd arguments are nonnegative. */
12982 return RECURSE (arg0) && RECURSE (arg1);
12984 CASE_CFN_COPYSIGN:
12985 /* True if the 2nd argument is nonnegative. */
12986 return RECURSE (arg1);
12988 CASE_CFN_POWI:
12989 /* True if the 1st argument is nonnegative or the second
12990 argument is an even integer. */
12991 if (TREE_CODE (arg1) == INTEGER_CST
12992 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12993 return true;
12994 return RECURSE (arg0);
12996 CASE_CFN_POW:
12997 /* True if the 1st argument is nonnegative or the second
12998 argument is an even integer valued real. */
12999 if (TREE_CODE (arg1) == REAL_CST)
13001 REAL_VALUE_TYPE c;
13002 HOST_WIDE_INT n;
13004 c = TREE_REAL_CST (arg1);
13005 n = real_to_integer (&c);
13006 if ((n & 1) == 0)
13008 REAL_VALUE_TYPE cint;
13009 real_from_integer (&cint, VOIDmode, n, SIGNED);
13010 if (real_identical (&c, &cint))
13011 return true;
13014 return RECURSE (arg0);
13016 default:
13017 break;
13019 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13022 /* Return true if T is known to be non-negative. If the return
13023 value is based on the assumption that signed overflow is undefined,
13024 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13025 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13027 static bool
13028 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13030 enum tree_code code = TREE_CODE (t);
13031 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13032 return true;
13034 switch (code)
13036 case TARGET_EXPR:
13038 tree temp = TARGET_EXPR_SLOT (t);
13039 t = TARGET_EXPR_INITIAL (t);
13041 /* If the initializer is non-void, then it's a normal expression
13042 that will be assigned to the slot. */
13043 if (!VOID_TYPE_P (t))
13044 return RECURSE (t);
13046 /* Otherwise, the initializer sets the slot in some way. One common
13047 way is an assignment statement at the end of the initializer. */
13048 while (1)
13050 if (TREE_CODE (t) == BIND_EXPR)
13051 t = expr_last (BIND_EXPR_BODY (t));
13052 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13053 || TREE_CODE (t) == TRY_CATCH_EXPR)
13054 t = expr_last (TREE_OPERAND (t, 0));
13055 else if (TREE_CODE (t) == STATEMENT_LIST)
13056 t = expr_last (t);
13057 else
13058 break;
13060 if (TREE_CODE (t) == MODIFY_EXPR
13061 && TREE_OPERAND (t, 0) == temp)
13062 return RECURSE (TREE_OPERAND (t, 1));
13064 return false;
13067 case CALL_EXPR:
13069 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13070 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13072 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13073 get_call_combined_fn (t),
13074 arg0,
13075 arg1,
13076 strict_overflow_p, depth);
13078 case COMPOUND_EXPR:
13079 case MODIFY_EXPR:
13080 return RECURSE (TREE_OPERAND (t, 1));
13082 case BIND_EXPR:
13083 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13085 case SAVE_EXPR:
13086 return RECURSE (TREE_OPERAND (t, 0));
13088 default:
13089 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13093 #undef RECURSE
13094 #undef tree_expr_nonnegative_warnv_p
13096 /* Return true if T is known to be non-negative. If the return
13097 value is based on the assumption that signed overflow is undefined,
13098 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13099 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13101 bool
13102 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13104 enum tree_code code;
13105 if (t == error_mark_node)
13106 return false;
13108 code = TREE_CODE (t);
13109 switch (TREE_CODE_CLASS (code))
13111 case tcc_binary:
13112 case tcc_comparison:
13113 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13114 TREE_TYPE (t),
13115 TREE_OPERAND (t, 0),
13116 TREE_OPERAND (t, 1),
13117 strict_overflow_p, depth);
13119 case tcc_unary:
13120 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13121 TREE_TYPE (t),
13122 TREE_OPERAND (t, 0),
13123 strict_overflow_p, depth);
13125 case tcc_constant:
13126 case tcc_declaration:
13127 case tcc_reference:
13128 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13130 default:
13131 break;
13134 switch (code)
13136 case TRUTH_AND_EXPR:
13137 case TRUTH_OR_EXPR:
13138 case TRUTH_XOR_EXPR:
13139 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13140 TREE_TYPE (t),
13141 TREE_OPERAND (t, 0),
13142 TREE_OPERAND (t, 1),
13143 strict_overflow_p, depth);
13144 case TRUTH_NOT_EXPR:
13145 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13146 TREE_TYPE (t),
13147 TREE_OPERAND (t, 0),
13148 strict_overflow_p, depth);
13150 case COND_EXPR:
13151 case CONSTRUCTOR:
13152 case OBJ_TYPE_REF:
13153 case ASSERT_EXPR:
13154 case ADDR_EXPR:
13155 case WITH_SIZE_EXPR:
13156 case SSA_NAME:
13157 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13159 default:
13160 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13164 /* Return true if `t' is known to be non-negative. Handle warnings
13165 about undefined signed overflow. */
13167 bool
13168 tree_expr_nonnegative_p (tree t)
13170 bool ret, strict_overflow_p;
13172 strict_overflow_p = false;
13173 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13174 if (strict_overflow_p)
13175 fold_overflow_warning (("assuming signed overflow does not occur when "
13176 "determining that expression is always "
13177 "non-negative"),
13178 WARN_STRICT_OVERFLOW_MISC);
13179 return ret;
13183 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13184 For floating point we further ensure that T is not denormal.
13185 Similar logic is present in nonzero_address in rtlanal.h.
13187 If the return value is based on the assumption that signed overflow
13188 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13189 change *STRICT_OVERFLOW_P. */
13191 bool
13192 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13193 bool *strict_overflow_p)
13195 switch (code)
13197 case ABS_EXPR:
13198 return tree_expr_nonzero_warnv_p (op0,
13199 strict_overflow_p);
13201 case NOP_EXPR:
13203 tree inner_type = TREE_TYPE (op0);
13204 tree outer_type = type;
13206 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13207 && tree_expr_nonzero_warnv_p (op0,
13208 strict_overflow_p));
13210 break;
13212 case NON_LVALUE_EXPR:
13213 return tree_expr_nonzero_warnv_p (op0,
13214 strict_overflow_p);
13216 default:
13217 break;
13220 return false;
13223 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13224 For floating point we further ensure that T is not denormal.
13225 Similar logic is present in nonzero_address in rtlanal.h.
13227 If the return value is based on the assumption that signed overflow
13228 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13229 change *STRICT_OVERFLOW_P. */
13231 bool
13232 tree_binary_nonzero_warnv_p (enum tree_code code,
13233 tree type,
13234 tree op0,
13235 tree op1, bool *strict_overflow_p)
13237 bool sub_strict_overflow_p;
13238 switch (code)
13240 case POINTER_PLUS_EXPR:
13241 case PLUS_EXPR:
13242 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13244 /* With the presence of negative values it is hard
13245 to say something. */
13246 sub_strict_overflow_p = false;
13247 if (!tree_expr_nonnegative_warnv_p (op0,
13248 &sub_strict_overflow_p)
13249 || !tree_expr_nonnegative_warnv_p (op1,
13250 &sub_strict_overflow_p))
13251 return false;
13252 /* One of operands must be positive and the other non-negative. */
13253 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13254 overflows, on a twos-complement machine the sum of two
13255 nonnegative numbers can never be zero. */
13256 return (tree_expr_nonzero_warnv_p (op0,
13257 strict_overflow_p)
13258 || tree_expr_nonzero_warnv_p (op1,
13259 strict_overflow_p));
13261 break;
13263 case MULT_EXPR:
13264 if (TYPE_OVERFLOW_UNDEFINED (type))
13266 if (tree_expr_nonzero_warnv_p (op0,
13267 strict_overflow_p)
13268 && tree_expr_nonzero_warnv_p (op1,
13269 strict_overflow_p))
13271 *strict_overflow_p = true;
13272 return true;
13275 break;
13277 case MIN_EXPR:
13278 sub_strict_overflow_p = false;
13279 if (tree_expr_nonzero_warnv_p (op0,
13280 &sub_strict_overflow_p)
13281 && tree_expr_nonzero_warnv_p (op1,
13282 &sub_strict_overflow_p))
13284 if (sub_strict_overflow_p)
13285 *strict_overflow_p = true;
13287 break;
13289 case MAX_EXPR:
13290 sub_strict_overflow_p = false;
13291 if (tree_expr_nonzero_warnv_p (op0,
13292 &sub_strict_overflow_p))
13294 if (sub_strict_overflow_p)
13295 *strict_overflow_p = true;
13297 /* When both operands are nonzero, then MAX must be too. */
13298 if (tree_expr_nonzero_warnv_p (op1,
13299 strict_overflow_p))
13300 return true;
13302 /* MAX where operand 0 is positive is positive. */
13303 return tree_expr_nonnegative_warnv_p (op0,
13304 strict_overflow_p);
13306 /* MAX where operand 1 is positive is positive. */
13307 else if (tree_expr_nonzero_warnv_p (op1,
13308 &sub_strict_overflow_p)
13309 && tree_expr_nonnegative_warnv_p (op1,
13310 &sub_strict_overflow_p))
13312 if (sub_strict_overflow_p)
13313 *strict_overflow_p = true;
13314 return true;
13316 break;
13318 case BIT_IOR_EXPR:
13319 return (tree_expr_nonzero_warnv_p (op1,
13320 strict_overflow_p)
13321 || tree_expr_nonzero_warnv_p (op0,
13322 strict_overflow_p));
13324 default:
13325 break;
13328 return false;
13331 /* Return true when T is an address and is known to be nonzero.
13332 For floating point we further ensure that T is not denormal.
13333 Similar logic is present in nonzero_address in rtlanal.h.
13335 If the return value is based on the assumption that signed overflow
13336 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13337 change *STRICT_OVERFLOW_P. */
13339 bool
13340 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13342 bool sub_strict_overflow_p;
13343 switch (TREE_CODE (t))
13345 case INTEGER_CST:
13346 return !integer_zerop (t);
13348 case ADDR_EXPR:
13350 tree base = TREE_OPERAND (t, 0);
13352 if (!DECL_P (base))
13353 base = get_base_address (base);
13355 if (base && TREE_CODE (base) == TARGET_EXPR)
13356 base = TARGET_EXPR_SLOT (base);
13358 if (!base)
13359 return false;
13361 /* For objects in symbol table check if we know they are non-zero.
13362 Don't do anything for variables and functions before symtab is built;
13363 it is quite possible that they will be declared weak later. */
13364 int nonzero_addr = maybe_nonzero_address (base);
13365 if (nonzero_addr >= 0)
13366 return nonzero_addr;
13368 /* Function local objects are never NULL. */
13369 if (DECL_P (base)
13370 && (DECL_CONTEXT (base)
13371 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13372 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13373 return true;
13375 /* Constants are never weak. */
13376 if (CONSTANT_CLASS_P (base))
13377 return true;
13379 return false;
13382 case COND_EXPR:
13383 sub_strict_overflow_p = false;
13384 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13385 &sub_strict_overflow_p)
13386 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13387 &sub_strict_overflow_p))
13389 if (sub_strict_overflow_p)
13390 *strict_overflow_p = true;
13391 return true;
13393 break;
13395 default:
13396 break;
13398 return false;
13401 #define integer_valued_real_p(X) \
13402 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13404 #define RECURSE(X) \
13405 ((integer_valued_real_p) (X, depth + 1))
13407 /* Return true if the floating point result of (CODE OP0) has an
13408 integer value. We also allow +Inf, -Inf and NaN to be considered
13409 integer values. Return false for signaling NaN.
13411 DEPTH is the current nesting depth of the query. */
13413 bool
13414 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13416 switch (code)
13418 case FLOAT_EXPR:
13419 return true;
13421 case ABS_EXPR:
13422 return RECURSE (op0);
13424 CASE_CONVERT:
13426 tree type = TREE_TYPE (op0);
13427 if (TREE_CODE (type) == INTEGER_TYPE)
13428 return true;
13429 if (TREE_CODE (type) == REAL_TYPE)
13430 return RECURSE (op0);
13431 break;
13434 default:
13435 break;
13437 return false;
13440 /* Return true if the floating point result of (CODE OP0 OP1) has an
13441 integer value. We also allow +Inf, -Inf and NaN to be considered
13442 integer values. Return false for signaling NaN.
13444 DEPTH is the current nesting depth of the query. */
13446 bool
13447 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13449 switch (code)
13451 case PLUS_EXPR:
13452 case MINUS_EXPR:
13453 case MULT_EXPR:
13454 case MIN_EXPR:
13455 case MAX_EXPR:
13456 return RECURSE (op0) && RECURSE (op1);
13458 default:
13459 break;
13461 return false;
13464 /* Return true if the floating point result of calling FNDECL with arguments
13465 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13466 considered integer values. Return false for signaling NaN. If FNDECL
13467 takes fewer than 2 arguments, the remaining ARGn are null.
13469 DEPTH is the current nesting depth of the query. */
13471 bool
13472 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13474 switch (fn)
13476 CASE_CFN_CEIL:
13477 CASE_CFN_FLOOR:
13478 CASE_CFN_NEARBYINT:
13479 CASE_CFN_RINT:
13480 CASE_CFN_ROUND:
13481 CASE_CFN_TRUNC:
13482 return true;
13484 CASE_CFN_FMIN:
13485 CASE_CFN_FMAX:
13486 return RECURSE (arg0) && RECURSE (arg1);
13488 default:
13489 break;
13491 return false;
13494 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13495 has an integer value. We also allow +Inf, -Inf and NaN to be
13496 considered integer values. Return false for signaling NaN.
13498 DEPTH is the current nesting depth of the query. */
13500 bool
13501 integer_valued_real_single_p (tree t, int depth)
13503 switch (TREE_CODE (t))
13505 case REAL_CST:
13506 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13508 case COND_EXPR:
13509 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13511 case SSA_NAME:
13512 /* Limit the depth of recursion to avoid quadratic behavior.
13513 This is expected to catch almost all occurrences in practice.
13514 If this code misses important cases that unbounded recursion
13515 would not, passes that need this information could be revised
13516 to provide it through dataflow propagation. */
13517 return (!name_registered_for_update_p (t)
13518 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13519 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13520 depth));
13522 default:
13523 break;
13525 return false;
13528 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13529 has an integer value. We also allow +Inf, -Inf and NaN to be
13530 considered integer values. Return false for signaling NaN.
13532 DEPTH is the current nesting depth of the query. */
13534 static bool
13535 integer_valued_real_invalid_p (tree t, int depth)
13537 switch (TREE_CODE (t))
13539 case COMPOUND_EXPR:
13540 case MODIFY_EXPR:
13541 case BIND_EXPR:
13542 return RECURSE (TREE_OPERAND (t, 1));
13544 case SAVE_EXPR:
13545 return RECURSE (TREE_OPERAND (t, 0));
13547 default:
13548 break;
13550 return false;
13553 #undef RECURSE
13554 #undef integer_valued_real_p
13556 /* Return true if the floating point expression T has an integer value.
13557 We also allow +Inf, -Inf and NaN to be considered integer values.
13558 Return false for signaling NaN.
13560 DEPTH is the current nesting depth of the query. */
13562 bool
13563 integer_valued_real_p (tree t, int depth)
13565 if (t == error_mark_node)
13566 return false;
13568 tree_code code = TREE_CODE (t);
13569 switch (TREE_CODE_CLASS (code))
13571 case tcc_binary:
13572 case tcc_comparison:
13573 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13574 TREE_OPERAND (t, 1), depth);
13576 case tcc_unary:
13577 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13579 case tcc_constant:
13580 case tcc_declaration:
13581 case tcc_reference:
13582 return integer_valued_real_single_p (t, depth);
13584 default:
13585 break;
13588 switch (code)
13590 case COND_EXPR:
13591 case SSA_NAME:
13592 return integer_valued_real_single_p (t, depth);
13594 case CALL_EXPR:
13596 tree arg0 = (call_expr_nargs (t) > 0
13597 ? CALL_EXPR_ARG (t, 0)
13598 : NULL_TREE);
13599 tree arg1 = (call_expr_nargs (t) > 1
13600 ? CALL_EXPR_ARG (t, 1)
13601 : NULL_TREE);
13602 return integer_valued_real_call_p (get_call_combined_fn (t),
13603 arg0, arg1, depth);
13606 default:
13607 return integer_valued_real_invalid_p (t, depth);
13611 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13612 attempt to fold the expression to a constant without modifying TYPE,
13613 OP0 or OP1.
13615 If the expression could be simplified to a constant, then return
13616 the constant. If the expression would not be simplified to a
13617 constant, then return NULL_TREE. */
13619 tree
13620 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13622 tree tem = fold_binary (code, type, op0, op1);
13623 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13626 /* Given the components of a unary expression CODE, TYPE and OP0,
13627 attempt to fold the expression to a constant without modifying
13628 TYPE or OP0.
13630 If the expression could be simplified to a constant, then return
13631 the constant. If the expression would not be simplified to a
13632 constant, then return NULL_TREE. */
13634 tree
13635 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13637 tree tem = fold_unary (code, type, op0);
13638 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13641 /* If EXP represents referencing an element in a constant string
13642 (either via pointer arithmetic or array indexing), return the
13643 tree representing the value accessed, otherwise return NULL. */
13645 tree
13646 fold_read_from_constant_string (tree exp)
13648 if ((TREE_CODE (exp) == INDIRECT_REF
13649 || TREE_CODE (exp) == ARRAY_REF)
13650 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13652 tree exp1 = TREE_OPERAND (exp, 0);
13653 tree index;
13654 tree string;
13655 location_t loc = EXPR_LOCATION (exp);
13657 if (TREE_CODE (exp) == INDIRECT_REF)
13658 string = string_constant (exp1, &index);
13659 else
13661 tree low_bound = array_ref_low_bound (exp);
13662 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13664 /* Optimize the special-case of a zero lower bound.
13666 We convert the low_bound to sizetype to avoid some problems
13667 with constant folding. (E.g. suppose the lower bound is 1,
13668 and its mode is QI. Without the conversion,l (ARRAY
13669 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13670 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13671 if (! integer_zerop (low_bound))
13672 index = size_diffop_loc (loc, index,
13673 fold_convert_loc (loc, sizetype, low_bound));
13675 string = exp1;
13678 if (string
13679 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13680 && TREE_CODE (string) == STRING_CST
13681 && TREE_CODE (index) == INTEGER_CST
13682 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13683 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13684 == MODE_INT)
13685 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13686 return build_int_cst_type (TREE_TYPE (exp),
13687 (TREE_STRING_POINTER (string)
13688 [TREE_INT_CST_LOW (index)]));
13690 return NULL;
13693 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13694 an integer constant, real, or fixed-point constant.
13696 TYPE is the type of the result. */
13698 static tree
13699 fold_negate_const (tree arg0, tree type)
13701 tree t = NULL_TREE;
13703 switch (TREE_CODE (arg0))
13705 case INTEGER_CST:
13707 bool overflow;
13708 wide_int val = wi::neg (arg0, &overflow);
13709 t = force_fit_type (type, val, 1,
13710 (overflow | TREE_OVERFLOW (arg0))
13711 && !TYPE_UNSIGNED (type));
13712 break;
13715 case REAL_CST:
13716 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13717 break;
13719 case FIXED_CST:
13721 FIXED_VALUE_TYPE f;
13722 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13723 &(TREE_FIXED_CST (arg0)), NULL,
13724 TYPE_SATURATING (type));
13725 t = build_fixed (type, f);
13726 /* Propagate overflow flags. */
13727 if (overflow_p | TREE_OVERFLOW (arg0))
13728 TREE_OVERFLOW (t) = 1;
13729 break;
13732 default:
13733 gcc_unreachable ();
13736 return t;
13739 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13740 an integer constant or real constant.
13742 TYPE is the type of the result. */
13744 tree
13745 fold_abs_const (tree arg0, tree type)
13747 tree t = NULL_TREE;
13749 switch (TREE_CODE (arg0))
13751 case INTEGER_CST:
13753 /* If the value is unsigned or non-negative, then the absolute value
13754 is the same as the ordinary value. */
13755 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13756 t = arg0;
13758 /* If the value is negative, then the absolute value is
13759 its negation. */
13760 else
13762 bool overflow;
13763 wide_int val = wi::neg (arg0, &overflow);
13764 t = force_fit_type (type, val, -1,
13765 overflow | TREE_OVERFLOW (arg0));
13768 break;
13770 case REAL_CST:
13771 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13772 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13773 else
13774 t = arg0;
13775 break;
13777 default:
13778 gcc_unreachable ();
13781 return t;
13784 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13785 constant. TYPE is the type of the result. */
13787 static tree
13788 fold_not_const (const_tree arg0, tree type)
13790 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13792 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13795 /* Given CODE, a relational operator, the target type, TYPE and two
13796 constant operands OP0 and OP1, return the result of the
13797 relational operation. If the result is not a compile time
13798 constant, then return NULL_TREE. */
13800 static tree
13801 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13803 int result, invert;
13805 /* From here on, the only cases we handle are when the result is
13806 known to be a constant. */
13808 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13810 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13811 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13813 /* Handle the cases where either operand is a NaN. */
13814 if (real_isnan (c0) || real_isnan (c1))
13816 switch (code)
13818 case EQ_EXPR:
13819 case ORDERED_EXPR:
13820 result = 0;
13821 break;
13823 case NE_EXPR:
13824 case UNORDERED_EXPR:
13825 case UNLT_EXPR:
13826 case UNLE_EXPR:
13827 case UNGT_EXPR:
13828 case UNGE_EXPR:
13829 case UNEQ_EXPR:
13830 result = 1;
13831 break;
13833 case LT_EXPR:
13834 case LE_EXPR:
13835 case GT_EXPR:
13836 case GE_EXPR:
13837 case LTGT_EXPR:
13838 if (flag_trapping_math)
13839 return NULL_TREE;
13840 result = 0;
13841 break;
13843 default:
13844 gcc_unreachable ();
13847 return constant_boolean_node (result, type);
13850 return constant_boolean_node (real_compare (code, c0, c1), type);
13853 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13855 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13856 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13857 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13860 /* Handle equality/inequality of complex constants. */
13861 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13863 tree rcond = fold_relational_const (code, type,
13864 TREE_REALPART (op0),
13865 TREE_REALPART (op1));
13866 tree icond = fold_relational_const (code, type,
13867 TREE_IMAGPART (op0),
13868 TREE_IMAGPART (op1));
13869 if (code == EQ_EXPR)
13870 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13871 else if (code == NE_EXPR)
13872 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13873 else
13874 return NULL_TREE;
13877 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13879 if (!VECTOR_TYPE_P (type))
13881 /* Have vector comparison with scalar boolean result. */
13882 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13883 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13884 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13886 tree elem0 = VECTOR_CST_ELT (op0, i);
13887 tree elem1 = VECTOR_CST_ELT (op1, i);
13888 tree tmp = fold_relational_const (code, type, elem0, elem1);
13889 if (tmp == NULL_TREE)
13890 return NULL_TREE;
13891 if (integer_zerop (tmp))
13892 return constant_boolean_node (false, type);
13894 return constant_boolean_node (true, type);
13896 unsigned count = VECTOR_CST_NELTS (op0);
13897 tree *elts = XALLOCAVEC (tree, count);
13898 gcc_assert (VECTOR_CST_NELTS (op1) == count
13899 && TYPE_VECTOR_SUBPARTS (type) == count);
13901 for (unsigned i = 0; i < count; i++)
13903 tree elem_type = TREE_TYPE (type);
13904 tree elem0 = VECTOR_CST_ELT (op0, i);
13905 tree elem1 = VECTOR_CST_ELT (op1, i);
13907 tree tem = fold_relational_const (code, elem_type,
13908 elem0, elem1);
13910 if (tem == NULL_TREE)
13911 return NULL_TREE;
13913 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13916 return build_vector (type, elts);
13919 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13921 To compute GT, swap the arguments and do LT.
13922 To compute GE, do LT and invert the result.
13923 To compute LE, swap the arguments, do LT and invert the result.
13924 To compute NE, do EQ and invert the result.
13926 Therefore, the code below must handle only EQ and LT. */
13928 if (code == LE_EXPR || code == GT_EXPR)
13930 std::swap (op0, op1);
13931 code = swap_tree_comparison (code);
13934 /* Note that it is safe to invert for real values here because we
13935 have already handled the one case that it matters. */
13937 invert = 0;
13938 if (code == NE_EXPR || code == GE_EXPR)
13940 invert = 1;
13941 code = invert_tree_comparison (code, false);
13944 /* Compute a result for LT or EQ if args permit;
13945 Otherwise return T. */
13946 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13948 if (code == EQ_EXPR)
13949 result = tree_int_cst_equal (op0, op1);
13950 else
13951 result = tree_int_cst_lt (op0, op1);
13953 else
13954 return NULL_TREE;
13956 if (invert)
13957 result ^= 1;
13958 return constant_boolean_node (result, type);
13961 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13962 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13963 itself. */
13965 tree
13966 fold_build_cleanup_point_expr (tree type, tree expr)
13968 /* If the expression does not have side effects then we don't have to wrap
13969 it with a cleanup point expression. */
13970 if (!TREE_SIDE_EFFECTS (expr))
13971 return expr;
13973 /* If the expression is a return, check to see if the expression inside the
13974 return has no side effects or the right hand side of the modify expression
13975 inside the return. If either don't have side effects set we don't need to
13976 wrap the expression in a cleanup point expression. Note we don't check the
13977 left hand side of the modify because it should always be a return decl. */
13978 if (TREE_CODE (expr) == RETURN_EXPR)
13980 tree op = TREE_OPERAND (expr, 0);
13981 if (!op || !TREE_SIDE_EFFECTS (op))
13982 return expr;
13983 op = TREE_OPERAND (op, 1);
13984 if (!TREE_SIDE_EFFECTS (op))
13985 return expr;
13988 return build1 (CLEANUP_POINT_EXPR, type, expr);
13991 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13992 of an indirection through OP0, or NULL_TREE if no simplification is
13993 possible. */
13995 tree
13996 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13998 tree sub = op0;
13999 tree subtype;
14001 STRIP_NOPS (sub);
14002 subtype = TREE_TYPE (sub);
14003 if (!POINTER_TYPE_P (subtype))
14004 return NULL_TREE;
14006 if (TREE_CODE (sub) == ADDR_EXPR)
14008 tree op = TREE_OPERAND (sub, 0);
14009 tree optype = TREE_TYPE (op);
14010 /* *&CONST_DECL -> to the value of the const decl. */
14011 if (TREE_CODE (op) == CONST_DECL)
14012 return DECL_INITIAL (op);
14013 /* *&p => p; make sure to handle *&"str"[cst] here. */
14014 if (type == optype)
14016 tree fop = fold_read_from_constant_string (op);
14017 if (fop)
14018 return fop;
14019 else
14020 return op;
14022 /* *(foo *)&fooarray => fooarray[0] */
14023 else if (TREE_CODE (optype) == ARRAY_TYPE
14024 && type == TREE_TYPE (optype)
14025 && (!in_gimple_form
14026 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14028 tree type_domain = TYPE_DOMAIN (optype);
14029 tree min_val = size_zero_node;
14030 if (type_domain && TYPE_MIN_VALUE (type_domain))
14031 min_val = TYPE_MIN_VALUE (type_domain);
14032 if (in_gimple_form
14033 && TREE_CODE (min_val) != INTEGER_CST)
14034 return NULL_TREE;
14035 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14036 NULL_TREE, NULL_TREE);
14038 /* *(foo *)&complexfoo => __real__ complexfoo */
14039 else if (TREE_CODE (optype) == COMPLEX_TYPE
14040 && type == TREE_TYPE (optype))
14041 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14042 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14043 else if (TREE_CODE (optype) == VECTOR_TYPE
14044 && type == TREE_TYPE (optype))
14046 tree part_width = TYPE_SIZE (type);
14047 tree index = bitsize_int (0);
14048 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14052 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14053 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14055 tree op00 = TREE_OPERAND (sub, 0);
14056 tree op01 = TREE_OPERAND (sub, 1);
14058 STRIP_NOPS (op00);
14059 if (TREE_CODE (op00) == ADDR_EXPR)
14061 tree op00type;
14062 op00 = TREE_OPERAND (op00, 0);
14063 op00type = TREE_TYPE (op00);
14065 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14066 if (TREE_CODE (op00type) == VECTOR_TYPE
14067 && type == TREE_TYPE (op00type))
14069 tree part_width = TYPE_SIZE (type);
14070 unsigned HOST_WIDE_INT max_offset
14071 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14072 * TYPE_VECTOR_SUBPARTS (op00type));
14073 if (tree_int_cst_sign_bit (op01) == 0
14074 && compare_tree_int (op01, max_offset) == -1)
14076 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14077 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14078 tree index = bitsize_int (indexi);
14079 return fold_build3_loc (loc,
14080 BIT_FIELD_REF, type, op00,
14081 part_width, index);
14084 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14085 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14086 && type == TREE_TYPE (op00type))
14088 tree size = TYPE_SIZE_UNIT (type);
14089 if (tree_int_cst_equal (size, op01))
14090 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14092 /* ((foo *)&fooarray)[1] => fooarray[1] */
14093 else if (TREE_CODE (op00type) == ARRAY_TYPE
14094 && type == TREE_TYPE (op00type))
14096 tree type_domain = TYPE_DOMAIN (op00type);
14097 tree min_val = size_zero_node;
14098 if (type_domain && TYPE_MIN_VALUE (type_domain))
14099 min_val = TYPE_MIN_VALUE (type_domain);
14100 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14101 TYPE_SIZE_UNIT (type));
14102 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14103 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14104 NULL_TREE, NULL_TREE);
14109 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14110 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14111 && type == TREE_TYPE (TREE_TYPE (subtype))
14112 && (!in_gimple_form
14113 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14115 tree type_domain;
14116 tree min_val = size_zero_node;
14117 sub = build_fold_indirect_ref_loc (loc, sub);
14118 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14119 if (type_domain && TYPE_MIN_VALUE (type_domain))
14120 min_val = TYPE_MIN_VALUE (type_domain);
14121 if (in_gimple_form
14122 && TREE_CODE (min_val) != INTEGER_CST)
14123 return NULL_TREE;
14124 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14125 NULL_TREE);
14128 return NULL_TREE;
14131 /* Builds an expression for an indirection through T, simplifying some
14132 cases. */
14134 tree
14135 build_fold_indirect_ref_loc (location_t loc, tree t)
14137 tree type = TREE_TYPE (TREE_TYPE (t));
14138 tree sub = fold_indirect_ref_1 (loc, type, t);
14140 if (sub)
14141 return sub;
14143 return build1_loc (loc, INDIRECT_REF, type, t);
14146 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14148 tree
14149 fold_indirect_ref_loc (location_t loc, tree t)
14151 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14153 if (sub)
14154 return sub;
14155 else
14156 return t;
14159 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14160 whose result is ignored. The type of the returned tree need not be
14161 the same as the original expression. */
14163 tree
14164 fold_ignored_result (tree t)
14166 if (!TREE_SIDE_EFFECTS (t))
14167 return integer_zero_node;
14169 for (;;)
14170 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14172 case tcc_unary:
14173 t = TREE_OPERAND (t, 0);
14174 break;
14176 case tcc_binary:
14177 case tcc_comparison:
14178 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14179 t = TREE_OPERAND (t, 0);
14180 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14181 t = TREE_OPERAND (t, 1);
14182 else
14183 return t;
14184 break;
14186 case tcc_expression:
14187 switch (TREE_CODE (t))
14189 case COMPOUND_EXPR:
14190 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14191 return t;
14192 t = TREE_OPERAND (t, 0);
14193 break;
14195 case COND_EXPR:
14196 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14197 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14198 return t;
14199 t = TREE_OPERAND (t, 0);
14200 break;
14202 default:
14203 return t;
14205 break;
14207 default:
14208 return t;
14212 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14214 tree
14215 round_up_loc (location_t loc, tree value, unsigned int divisor)
14217 tree div = NULL_TREE;
14219 if (divisor == 1)
14220 return value;
14222 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14223 have to do anything. Only do this when we are not given a const,
14224 because in that case, this check is more expensive than just
14225 doing it. */
14226 if (TREE_CODE (value) != INTEGER_CST)
14228 div = build_int_cst (TREE_TYPE (value), divisor);
14230 if (multiple_of_p (TREE_TYPE (value), value, div))
14231 return value;
14234 /* If divisor is a power of two, simplify this to bit manipulation. */
14235 if (pow2_or_zerop (divisor))
14237 if (TREE_CODE (value) == INTEGER_CST)
14239 wide_int val = value;
14240 bool overflow_p;
14242 if ((val & (divisor - 1)) == 0)
14243 return value;
14245 overflow_p = TREE_OVERFLOW (value);
14246 val += divisor - 1;
14247 val &= - (int) divisor;
14248 if (val == 0)
14249 overflow_p = true;
14251 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14253 else
14255 tree t;
14257 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14258 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14259 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14260 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14263 else
14265 if (!div)
14266 div = build_int_cst (TREE_TYPE (value), divisor);
14267 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14268 value = size_binop_loc (loc, MULT_EXPR, value, div);
14271 return value;
14274 /* Likewise, but round down. */
14276 tree
14277 round_down_loc (location_t loc, tree value, int divisor)
14279 tree div = NULL_TREE;
14281 gcc_assert (divisor > 0);
14282 if (divisor == 1)
14283 return value;
14285 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14286 have to do anything. Only do this when we are not given a const,
14287 because in that case, this check is more expensive than just
14288 doing it. */
14289 if (TREE_CODE (value) != INTEGER_CST)
14291 div = build_int_cst (TREE_TYPE (value), divisor);
14293 if (multiple_of_p (TREE_TYPE (value), value, div))
14294 return value;
14297 /* If divisor is a power of two, simplify this to bit manipulation. */
14298 if (pow2_or_zerop (divisor))
14300 tree t;
14302 t = build_int_cst (TREE_TYPE (value), -divisor);
14303 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14305 else
14307 if (!div)
14308 div = build_int_cst (TREE_TYPE (value), divisor);
14309 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14310 value = size_binop_loc (loc, MULT_EXPR, value, div);
14313 return value;
14316 /* Returns the pointer to the base of the object addressed by EXP and
14317 extracts the information about the offset of the access, storing it
14318 to PBITPOS and POFFSET. */
14320 static tree
14321 split_address_to_core_and_offset (tree exp,
14322 HOST_WIDE_INT *pbitpos, tree *poffset)
14324 tree core;
14325 machine_mode mode;
14326 int unsignedp, reversep, volatilep;
14327 HOST_WIDE_INT bitsize;
14328 location_t loc = EXPR_LOCATION (exp);
14330 if (TREE_CODE (exp) == ADDR_EXPR)
14332 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14333 poffset, &mode, &unsignedp, &reversep,
14334 &volatilep);
14335 core = build_fold_addr_expr_loc (loc, core);
14337 else
14339 core = exp;
14340 *pbitpos = 0;
14341 *poffset = NULL_TREE;
14344 return core;
14347 /* Returns true if addresses of E1 and E2 differ by a constant, false
14348 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14350 bool
14351 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14353 tree core1, core2;
14354 HOST_WIDE_INT bitpos1, bitpos2;
14355 tree toffset1, toffset2, tdiff, type;
14357 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14358 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14360 if (bitpos1 % BITS_PER_UNIT != 0
14361 || bitpos2 % BITS_PER_UNIT != 0
14362 || !operand_equal_p (core1, core2, 0))
14363 return false;
14365 if (toffset1 && toffset2)
14367 type = TREE_TYPE (toffset1);
14368 if (type != TREE_TYPE (toffset2))
14369 toffset2 = fold_convert (type, toffset2);
14371 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14372 if (!cst_and_fits_in_hwi (tdiff))
14373 return false;
14375 *diff = int_cst_value (tdiff);
14377 else if (toffset1 || toffset2)
14379 /* If only one of the offsets is non-constant, the difference cannot
14380 be a constant. */
14381 return false;
14383 else
14384 *diff = 0;
14386 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14387 return true;
14390 /* Return OFF converted to a pointer offset type suitable as offset for
14391 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14392 tree
14393 convert_to_ptrofftype_loc (location_t loc, tree off)
14395 return fold_convert_loc (loc, sizetype, off);
14398 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14399 tree
14400 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14402 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14403 ptr, convert_to_ptrofftype_loc (loc, off));
14406 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14407 tree
14408 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14410 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14411 ptr, size_int (off));
14414 /* Return a char pointer for a C string if it is a string constant
14415 or sum of string constant and integer constant. We only support
14416 string constants properly terminated with '\0' character.
14417 If STRLEN is a valid pointer, length (including terminating character)
14418 of returned string is stored to the argument. */
14420 const char *
14421 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14423 tree offset_node;
14425 if (strlen)
14426 *strlen = 0;
14428 src = string_constant (src, &offset_node);
14429 if (src == 0)
14430 return NULL;
14432 unsigned HOST_WIDE_INT offset = 0;
14433 if (offset_node != NULL_TREE)
14435 if (!tree_fits_uhwi_p (offset_node))
14436 return NULL;
14437 else
14438 offset = tree_to_uhwi (offset_node);
14441 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14442 const char *string = TREE_STRING_POINTER (src);
14444 /* Support only properly null-terminated strings. */
14445 if (string_length == 0
14446 || string[string_length - 1] != '\0'
14447 || offset >= string_length)
14448 return NULL;
14450 if (strlen)
14451 *strlen = string_length - offset;
14452 return string + offset;
14455 #if CHECKING_P
14457 namespace selftest {
14459 /* Helper functions for writing tests of folding trees. */
14461 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14463 static void
14464 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14465 tree constant)
14467 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14470 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14471 wrapping WRAPPED_EXPR. */
14473 static void
14474 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14475 tree wrapped_expr)
14477 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14478 ASSERT_NE (wrapped_expr, result);
14479 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14480 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14483 /* Verify that various arithmetic binary operations are folded
14484 correctly. */
14486 static void
14487 test_arithmetic_folding ()
14489 tree type = integer_type_node;
14490 tree x = create_tmp_var_raw (type, "x");
14491 tree zero = build_zero_cst (type);
14492 tree one = build_int_cst (type, 1);
14494 /* Addition. */
14495 /* 1 <-- (0 + 1) */
14496 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14497 one);
14498 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14499 one);
14501 /* (nonlvalue)x <-- (x + 0) */
14502 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14505 /* Subtraction. */
14506 /* 0 <-- (x - x) */
14507 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14508 zero);
14509 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14512 /* Multiplication. */
14513 /* 0 <-- (x * 0) */
14514 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14515 zero);
14517 /* (nonlvalue)x <-- (x * 1) */
14518 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14522 /* Verify that various binary operations on vectors are folded
14523 correctly. */
14525 static void
14526 test_vector_folding ()
14528 tree inner_type = integer_type_node;
14529 tree type = build_vector_type (inner_type, 4);
14530 tree zero = build_zero_cst (type);
14531 tree one = build_one_cst (type);
14533 /* Verify equality tests that return a scalar boolean result. */
14534 tree res_type = boolean_type_node;
14535 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14536 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14537 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14538 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14541 /* Run all of the selftests within this file. */
14543 void
14544 fold_const_c_tests ()
14546 test_arithmetic_folding ();
14547 test_vector_folding ();
14550 } // namespace selftest
14552 #endif /* CHECKING_P */