* g++.dg/cpp0x/pr79296.C: Move dg-do compile directive first.
[official-gcc.git] / gcc / fold-const.c
bloba8bb8af45c3aedce5cfb98873e1e6548bf953d20
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
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 (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && wi::popcount (wi::abs (TREE_OPERAND (t, 0))) != 1)
458 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
459 && wi::popcount (wi::abs (TREE_OPERAND (t, 1))) != 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 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2726 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2728 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2729 any operand with side effect. This is unnecesarily conservative in the
2730 case we know that arg0 and arg1 are in disjoint code paths (such as in
2731 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2732 addresses with TREE_CONSTANT flag set so we know that &var == &var
2733 even if var is volatile. */
2736 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2738 /* When checking, verify at the outermost operand_equal_p call that
2739 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2740 hash value. */
2741 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2743 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2745 if (arg0 != arg1)
2747 inchash::hash hstate0 (0), hstate1 (0);
2748 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2749 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2750 hashval_t h0 = hstate0.end ();
2751 hashval_t h1 = hstate1.end ();
2752 gcc_assert (h0 == h1);
2754 return 1;
2756 else
2757 return 0;
2760 /* If either is ERROR_MARK, they aren't equal. */
2761 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2762 || TREE_TYPE (arg0) == error_mark_node
2763 || TREE_TYPE (arg1) == error_mark_node)
2764 return 0;
2766 /* Similar, if either does not have a type (like a released SSA name),
2767 they aren't equal. */
2768 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2769 return 0;
2771 /* We cannot consider pointers to different address space equal. */
2772 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2773 && POINTER_TYPE_P (TREE_TYPE (arg1))
2774 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2775 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2776 return 0;
2778 /* Check equality of integer constants before bailing out due to
2779 precision differences. */
2780 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2782 /* Address of INTEGER_CST is not defined; check that we did not forget
2783 to drop the OEP_ADDRESS_OF flags. */
2784 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2785 return tree_int_cst_equal (arg0, arg1);
2788 if (!(flags & OEP_ADDRESS_OF))
2790 /* If both types don't have the same signedness, then we can't consider
2791 them equal. We must check this before the STRIP_NOPS calls
2792 because they may change the signedness of the arguments. As pointers
2793 strictly don't have a signedness, require either two pointers or
2794 two non-pointers as well. */
2795 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2796 || POINTER_TYPE_P (TREE_TYPE (arg0))
2797 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2798 return 0;
2800 /* If both types don't have the same precision, then it is not safe
2801 to strip NOPs. */
2802 if (element_precision (TREE_TYPE (arg0))
2803 != element_precision (TREE_TYPE (arg1)))
2804 return 0;
2806 STRIP_NOPS (arg0);
2807 STRIP_NOPS (arg1);
2809 #if 0
2810 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2811 sanity check once the issue is solved. */
2812 else
2813 /* Addresses of conversions and SSA_NAMEs (and many other things)
2814 are not defined. Check that we did not forget to drop the
2815 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2816 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2817 && TREE_CODE (arg0) != SSA_NAME);
2818 #endif
2820 /* In case both args are comparisons but with different comparison
2821 code, try to swap the comparison operands of one arg to produce
2822 a match and compare that variant. */
2823 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2824 && COMPARISON_CLASS_P (arg0)
2825 && COMPARISON_CLASS_P (arg1))
2827 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2829 if (TREE_CODE (arg0) == swap_code)
2830 return operand_equal_p (TREE_OPERAND (arg0, 0),
2831 TREE_OPERAND (arg1, 1), flags)
2832 && operand_equal_p (TREE_OPERAND (arg0, 1),
2833 TREE_OPERAND (arg1, 0), flags);
2836 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2838 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2839 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2841 else if (flags & OEP_ADDRESS_OF)
2843 /* If we are interested in comparing addresses ignore
2844 MEM_REF wrappings of the base that can appear just for
2845 TBAA reasons. */
2846 if (TREE_CODE (arg0) == MEM_REF
2847 && DECL_P (arg1)
2848 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2849 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2850 && integer_zerop (TREE_OPERAND (arg0, 1)))
2851 return 1;
2852 else if (TREE_CODE (arg1) == MEM_REF
2853 && DECL_P (arg0)
2854 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2855 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2856 && integer_zerop (TREE_OPERAND (arg1, 1)))
2857 return 1;
2858 return 0;
2860 else
2861 return 0;
2864 /* When not checking adddresses, this is needed for conversions and for
2865 COMPONENT_REF. Might as well play it safe and always test this. */
2866 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2867 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2868 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2869 && !(flags & OEP_ADDRESS_OF)))
2870 return 0;
2872 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2873 We don't care about side effects in that case because the SAVE_EXPR
2874 takes care of that for us. In all other cases, two expressions are
2875 equal if they have no side effects. If we have two identical
2876 expressions with side effects that should be treated the same due
2877 to the only side effects being identical SAVE_EXPR's, that will
2878 be detected in the recursive calls below.
2879 If we are taking an invariant address of two identical objects
2880 they are necessarily equal as well. */
2881 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2882 && (TREE_CODE (arg0) == SAVE_EXPR
2883 || (flags & OEP_MATCH_SIDE_EFFECTS)
2884 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2885 return 1;
2887 /* Next handle constant cases, those for which we can return 1 even
2888 if ONLY_CONST is set. */
2889 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2890 switch (TREE_CODE (arg0))
2892 case INTEGER_CST:
2893 return tree_int_cst_equal (arg0, arg1);
2895 case FIXED_CST:
2896 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2897 TREE_FIXED_CST (arg1));
2899 case REAL_CST:
2900 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2901 return 1;
2904 if (!HONOR_SIGNED_ZEROS (arg0))
2906 /* If we do not distinguish between signed and unsigned zero,
2907 consider them equal. */
2908 if (real_zerop (arg0) && real_zerop (arg1))
2909 return 1;
2911 return 0;
2913 case VECTOR_CST:
2915 unsigned i;
2917 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2918 return 0;
2920 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2922 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2923 VECTOR_CST_ELT (arg1, i), flags))
2924 return 0;
2926 return 1;
2929 case COMPLEX_CST:
2930 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2931 flags)
2932 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2933 flags));
2935 case STRING_CST:
2936 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2937 && ! memcmp (TREE_STRING_POINTER (arg0),
2938 TREE_STRING_POINTER (arg1),
2939 TREE_STRING_LENGTH (arg0)));
2941 case ADDR_EXPR:
2942 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2943 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2944 flags | OEP_ADDRESS_OF
2945 | OEP_MATCH_SIDE_EFFECTS);
2946 case CONSTRUCTOR:
2947 /* In GIMPLE empty constructors are allowed in initializers of
2948 aggregates. */
2949 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2950 default:
2951 break;
2954 if (flags & OEP_ONLY_CONST)
2955 return 0;
2957 /* Define macros to test an operand from arg0 and arg1 for equality and a
2958 variant that allows null and views null as being different from any
2959 non-null value. In the latter case, if either is null, the both
2960 must be; otherwise, do the normal comparison. */
2961 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2962 TREE_OPERAND (arg1, N), flags)
2964 #define OP_SAME_WITH_NULL(N) \
2965 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2966 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2968 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2970 case tcc_unary:
2971 /* Two conversions are equal only if signedness and modes match. */
2972 switch (TREE_CODE (arg0))
2974 CASE_CONVERT:
2975 case FIX_TRUNC_EXPR:
2976 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2977 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2978 return 0;
2979 break;
2980 default:
2981 break;
2984 return OP_SAME (0);
2987 case tcc_comparison:
2988 case tcc_binary:
2989 if (OP_SAME (0) && OP_SAME (1))
2990 return 1;
2992 /* For commutative ops, allow the other order. */
2993 return (commutative_tree_code (TREE_CODE (arg0))
2994 && operand_equal_p (TREE_OPERAND (arg0, 0),
2995 TREE_OPERAND (arg1, 1), flags)
2996 && operand_equal_p (TREE_OPERAND (arg0, 1),
2997 TREE_OPERAND (arg1, 0), flags));
2999 case tcc_reference:
3000 /* If either of the pointer (or reference) expressions we are
3001 dereferencing contain a side effect, these cannot be equal,
3002 but their addresses can be. */
3003 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3004 && (TREE_SIDE_EFFECTS (arg0)
3005 || TREE_SIDE_EFFECTS (arg1)))
3006 return 0;
3008 switch (TREE_CODE (arg0))
3010 case INDIRECT_REF:
3011 if (!(flags & OEP_ADDRESS_OF)
3012 && (TYPE_ALIGN (TREE_TYPE (arg0))
3013 != TYPE_ALIGN (TREE_TYPE (arg1))))
3014 return 0;
3015 flags &= ~OEP_ADDRESS_OF;
3016 return OP_SAME (0);
3018 case IMAGPART_EXPR:
3019 /* Require the same offset. */
3020 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3021 TYPE_SIZE (TREE_TYPE (arg1)),
3022 flags & ~OEP_ADDRESS_OF))
3023 return 0;
3025 /* Fallthru. */
3026 case REALPART_EXPR:
3027 case VIEW_CONVERT_EXPR:
3028 return OP_SAME (0);
3030 case TARGET_MEM_REF:
3031 case MEM_REF:
3032 if (!(flags & OEP_ADDRESS_OF))
3034 /* Require equal access sizes */
3035 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3036 && (!TYPE_SIZE (TREE_TYPE (arg0))
3037 || !TYPE_SIZE (TREE_TYPE (arg1))
3038 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3039 TYPE_SIZE (TREE_TYPE (arg1)),
3040 flags)))
3041 return 0;
3042 /* Verify that access happens in similar types. */
3043 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3044 return 0;
3045 /* Verify that accesses are TBAA compatible. */
3046 if (!alias_ptr_types_compatible_p
3047 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3048 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3049 || (MR_DEPENDENCE_CLIQUE (arg0)
3050 != MR_DEPENDENCE_CLIQUE (arg1))
3051 || (MR_DEPENDENCE_BASE (arg0)
3052 != MR_DEPENDENCE_BASE (arg1)))
3053 return 0;
3054 /* Verify that alignment is compatible. */
3055 if (TYPE_ALIGN (TREE_TYPE (arg0))
3056 != TYPE_ALIGN (TREE_TYPE (arg1)))
3057 return 0;
3059 flags &= ~OEP_ADDRESS_OF;
3060 return (OP_SAME (0) && OP_SAME (1)
3061 /* TARGET_MEM_REF require equal extra operands. */
3062 && (TREE_CODE (arg0) != TARGET_MEM_REF
3063 || (OP_SAME_WITH_NULL (2)
3064 && OP_SAME_WITH_NULL (3)
3065 && OP_SAME_WITH_NULL (4))));
3067 case ARRAY_REF:
3068 case ARRAY_RANGE_REF:
3069 if (!OP_SAME (0))
3070 return 0;
3071 flags &= ~OEP_ADDRESS_OF;
3072 /* Compare the array index by value if it is constant first as we
3073 may have different types but same value here. */
3074 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3075 TREE_OPERAND (arg1, 1))
3076 || OP_SAME (1))
3077 && OP_SAME_WITH_NULL (2)
3078 && OP_SAME_WITH_NULL (3)
3079 /* Compare low bound and element size as with OEP_ADDRESS_OF
3080 we have to account for the offset of the ref. */
3081 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3082 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3083 || (operand_equal_p (array_ref_low_bound
3084 (CONST_CAST_TREE (arg0)),
3085 array_ref_low_bound
3086 (CONST_CAST_TREE (arg1)), flags)
3087 && operand_equal_p (array_ref_element_size
3088 (CONST_CAST_TREE (arg0)),
3089 array_ref_element_size
3090 (CONST_CAST_TREE (arg1)),
3091 flags))));
3093 case COMPONENT_REF:
3094 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3095 may be NULL when we're called to compare MEM_EXPRs. */
3096 if (!OP_SAME_WITH_NULL (0)
3097 || !OP_SAME (1))
3098 return 0;
3099 flags &= ~OEP_ADDRESS_OF;
3100 return OP_SAME_WITH_NULL (2);
3102 case BIT_FIELD_REF:
3103 if (!OP_SAME (0))
3104 return 0;
3105 flags &= ~OEP_ADDRESS_OF;
3106 return OP_SAME (1) && OP_SAME (2);
3108 default:
3109 return 0;
3112 case tcc_expression:
3113 switch (TREE_CODE (arg0))
3115 case ADDR_EXPR:
3116 /* Be sure we pass right ADDRESS_OF flag. */
3117 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3118 return operand_equal_p (TREE_OPERAND (arg0, 0),
3119 TREE_OPERAND (arg1, 0),
3120 flags | OEP_ADDRESS_OF);
3122 case TRUTH_NOT_EXPR:
3123 return OP_SAME (0);
3125 case TRUTH_ANDIF_EXPR:
3126 case TRUTH_ORIF_EXPR:
3127 return OP_SAME (0) && OP_SAME (1);
3129 case FMA_EXPR:
3130 case WIDEN_MULT_PLUS_EXPR:
3131 case WIDEN_MULT_MINUS_EXPR:
3132 if (!OP_SAME (2))
3133 return 0;
3134 /* The multiplcation operands are commutative. */
3135 /* FALLTHRU */
3137 case TRUTH_AND_EXPR:
3138 case TRUTH_OR_EXPR:
3139 case TRUTH_XOR_EXPR:
3140 if (OP_SAME (0) && OP_SAME (1))
3141 return 1;
3143 /* Otherwise take into account this is a commutative operation. */
3144 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3145 TREE_OPERAND (arg1, 1), flags)
3146 && operand_equal_p (TREE_OPERAND (arg0, 1),
3147 TREE_OPERAND (arg1, 0), flags));
3149 case COND_EXPR:
3150 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3151 return 0;
3152 flags &= ~OEP_ADDRESS_OF;
3153 return OP_SAME (0);
3155 case VEC_COND_EXPR:
3156 case DOT_PROD_EXPR:
3157 case BIT_INSERT_EXPR:
3158 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3160 case MODIFY_EXPR:
3161 case INIT_EXPR:
3162 case COMPOUND_EXPR:
3163 case PREDECREMENT_EXPR:
3164 case PREINCREMENT_EXPR:
3165 case POSTDECREMENT_EXPR:
3166 case POSTINCREMENT_EXPR:
3167 if (flags & OEP_LEXICOGRAPHIC)
3168 return OP_SAME (0) && OP_SAME (1);
3169 return 0;
3171 case CLEANUP_POINT_EXPR:
3172 case EXPR_STMT:
3173 if (flags & OEP_LEXICOGRAPHIC)
3174 return OP_SAME (0);
3175 return 0;
3177 default:
3178 return 0;
3181 case tcc_vl_exp:
3182 switch (TREE_CODE (arg0))
3184 case CALL_EXPR:
3185 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3186 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3187 /* If not both CALL_EXPRs are either internal or normal function
3188 functions, then they are not equal. */
3189 return 0;
3190 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3192 /* If the CALL_EXPRs call different internal functions, then they
3193 are not equal. */
3194 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3195 return 0;
3197 else
3199 /* If the CALL_EXPRs call different functions, then they are not
3200 equal. */
3201 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3202 flags))
3203 return 0;
3206 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3208 unsigned int cef = call_expr_flags (arg0);
3209 if (flags & OEP_PURE_SAME)
3210 cef &= ECF_CONST | ECF_PURE;
3211 else
3212 cef &= ECF_CONST;
3213 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3214 return 0;
3217 /* Now see if all the arguments are the same. */
3219 const_call_expr_arg_iterator iter0, iter1;
3220 const_tree a0, a1;
3221 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3222 a1 = first_const_call_expr_arg (arg1, &iter1);
3223 a0 && a1;
3224 a0 = next_const_call_expr_arg (&iter0),
3225 a1 = next_const_call_expr_arg (&iter1))
3226 if (! operand_equal_p (a0, a1, flags))
3227 return 0;
3229 /* If we get here and both argument lists are exhausted
3230 then the CALL_EXPRs are equal. */
3231 return ! (a0 || a1);
3233 default:
3234 return 0;
3237 case tcc_declaration:
3238 /* Consider __builtin_sqrt equal to sqrt. */
3239 return (TREE_CODE (arg0) == FUNCTION_DECL
3240 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3241 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3242 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3244 case tcc_exceptional:
3245 if (TREE_CODE (arg0) == CONSTRUCTOR)
3247 /* In GIMPLE constructors are used only to build vectors from
3248 elements. Individual elements in the constructor must be
3249 indexed in increasing order and form an initial sequence.
3251 We make no effort to compare constructors in generic.
3252 (see sem_variable::equals in ipa-icf which can do so for
3253 constants). */
3254 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3255 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3256 return 0;
3258 /* Be sure that vectors constructed have the same representation.
3259 We only tested element precision and modes to match.
3260 Vectors may be BLKmode and thus also check that the number of
3261 parts match. */
3262 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3263 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3264 return 0;
3266 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3267 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3268 unsigned int len = vec_safe_length (v0);
3270 if (len != vec_safe_length (v1))
3271 return 0;
3273 for (unsigned int i = 0; i < len; i++)
3275 constructor_elt *c0 = &(*v0)[i];
3276 constructor_elt *c1 = &(*v1)[i];
3278 if (!operand_equal_p (c0->value, c1->value, flags)
3279 /* In GIMPLE the indexes can be either NULL or matching i.
3280 Double check this so we won't get false
3281 positives for GENERIC. */
3282 || (c0->index
3283 && (TREE_CODE (c0->index) != INTEGER_CST
3284 || !compare_tree_int (c0->index, i)))
3285 || (c1->index
3286 && (TREE_CODE (c1->index) != INTEGER_CST
3287 || !compare_tree_int (c1->index, i))))
3288 return 0;
3290 return 1;
3292 else if (TREE_CODE (arg0) == STATEMENT_LIST
3293 && (flags & OEP_LEXICOGRAPHIC))
3295 /* Compare the STATEMENT_LISTs. */
3296 tree_stmt_iterator tsi1, tsi2;
3297 tree body1 = CONST_CAST_TREE (arg0);
3298 tree body2 = CONST_CAST_TREE (arg1);
3299 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3300 tsi_next (&tsi1), tsi_next (&tsi2))
3302 /* The lists don't have the same number of statements. */
3303 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3304 return 0;
3305 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3306 return 1;
3307 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3308 OEP_LEXICOGRAPHIC))
3309 return 0;
3312 return 0;
3314 case tcc_statement:
3315 switch (TREE_CODE (arg0))
3317 case RETURN_EXPR:
3318 if (flags & OEP_LEXICOGRAPHIC)
3319 return OP_SAME_WITH_NULL (0);
3320 return 0;
3321 default:
3322 return 0;
3325 default:
3326 return 0;
3329 #undef OP_SAME
3330 #undef OP_SAME_WITH_NULL
3333 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3334 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3336 When in doubt, return 0. */
3338 static int
3339 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3341 int unsignedp1, unsignedpo;
3342 tree primarg0, primarg1, primother;
3343 unsigned int correct_width;
3345 if (operand_equal_p (arg0, arg1, 0))
3346 return 1;
3348 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3349 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3350 return 0;
3352 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3353 and see if the inner values are the same. This removes any
3354 signedness comparison, which doesn't matter here. */
3355 primarg0 = arg0, primarg1 = arg1;
3356 STRIP_NOPS (primarg0);
3357 STRIP_NOPS (primarg1);
3358 if (operand_equal_p (primarg0, primarg1, 0))
3359 return 1;
3361 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3362 actual comparison operand, ARG0.
3364 First throw away any conversions to wider types
3365 already present in the operands. */
3367 primarg1 = get_narrower (arg1, &unsignedp1);
3368 primother = get_narrower (other, &unsignedpo);
3370 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3371 if (unsignedp1 == unsignedpo
3372 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3373 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3375 tree type = TREE_TYPE (arg0);
3377 /* Make sure shorter operand is extended the right way
3378 to match the longer operand. */
3379 primarg1 = fold_convert (signed_or_unsigned_type_for
3380 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3382 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3383 return 1;
3386 return 0;
3389 /* See if ARG is an expression that is either a comparison or is performing
3390 arithmetic on comparisons. The comparisons must only be comparing
3391 two different values, which will be stored in *CVAL1 and *CVAL2; if
3392 they are nonzero it means that some operands have already been found.
3393 No variables may be used anywhere else in the expression except in the
3394 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3395 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3397 If this is true, return 1. Otherwise, return zero. */
3399 static int
3400 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3402 enum tree_code code = TREE_CODE (arg);
3403 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3405 /* We can handle some of the tcc_expression cases here. */
3406 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3407 tclass = tcc_unary;
3408 else if (tclass == tcc_expression
3409 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3410 || code == COMPOUND_EXPR))
3411 tclass = tcc_binary;
3413 else if (tclass == tcc_expression && code == SAVE_EXPR
3414 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3416 /* If we've already found a CVAL1 or CVAL2, this expression is
3417 two complex to handle. */
3418 if (*cval1 || *cval2)
3419 return 0;
3421 tclass = tcc_unary;
3422 *save_p = 1;
3425 switch (tclass)
3427 case tcc_unary:
3428 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3430 case tcc_binary:
3431 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3432 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3433 cval1, cval2, save_p));
3435 case tcc_constant:
3436 return 1;
3438 case tcc_expression:
3439 if (code == COND_EXPR)
3440 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3441 cval1, cval2, save_p)
3442 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3443 cval1, cval2, save_p)
3444 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3445 cval1, cval2, save_p));
3446 return 0;
3448 case tcc_comparison:
3449 /* First see if we can handle the first operand, then the second. For
3450 the second operand, we know *CVAL1 can't be zero. It must be that
3451 one side of the comparison is each of the values; test for the
3452 case where this isn't true by failing if the two operands
3453 are the same. */
3455 if (operand_equal_p (TREE_OPERAND (arg, 0),
3456 TREE_OPERAND (arg, 1), 0))
3457 return 0;
3459 if (*cval1 == 0)
3460 *cval1 = TREE_OPERAND (arg, 0);
3461 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3463 else if (*cval2 == 0)
3464 *cval2 = TREE_OPERAND (arg, 0);
3465 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3467 else
3468 return 0;
3470 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3472 else if (*cval2 == 0)
3473 *cval2 = TREE_OPERAND (arg, 1);
3474 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3476 else
3477 return 0;
3479 return 1;
3481 default:
3482 return 0;
3486 /* ARG is a tree that is known to contain just arithmetic operations and
3487 comparisons. Evaluate the operations in the tree substituting NEW0 for
3488 any occurrence of OLD0 as an operand of a comparison and likewise for
3489 NEW1 and OLD1. */
3491 static tree
3492 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3493 tree old1, tree new1)
3495 tree type = TREE_TYPE (arg);
3496 enum tree_code code = TREE_CODE (arg);
3497 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3499 /* We can handle some of the tcc_expression cases here. */
3500 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3501 tclass = tcc_unary;
3502 else if (tclass == tcc_expression
3503 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3504 tclass = tcc_binary;
3506 switch (tclass)
3508 case tcc_unary:
3509 return fold_build1_loc (loc, code, type,
3510 eval_subst (loc, TREE_OPERAND (arg, 0),
3511 old0, new0, old1, new1));
3513 case tcc_binary:
3514 return fold_build2_loc (loc, code, type,
3515 eval_subst (loc, TREE_OPERAND (arg, 0),
3516 old0, new0, old1, new1),
3517 eval_subst (loc, TREE_OPERAND (arg, 1),
3518 old0, new0, old1, new1));
3520 case tcc_expression:
3521 switch (code)
3523 case SAVE_EXPR:
3524 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3525 old1, new1);
3527 case COMPOUND_EXPR:
3528 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3529 old1, new1);
3531 case COND_EXPR:
3532 return fold_build3_loc (loc, code, type,
3533 eval_subst (loc, TREE_OPERAND (arg, 0),
3534 old0, new0, old1, new1),
3535 eval_subst (loc, TREE_OPERAND (arg, 1),
3536 old0, new0, old1, new1),
3537 eval_subst (loc, TREE_OPERAND (arg, 2),
3538 old0, new0, old1, new1));
3539 default:
3540 break;
3542 /* Fall through - ??? */
3544 case tcc_comparison:
3546 tree arg0 = TREE_OPERAND (arg, 0);
3547 tree arg1 = TREE_OPERAND (arg, 1);
3549 /* We need to check both for exact equality and tree equality. The
3550 former will be true if the operand has a side-effect. In that
3551 case, we know the operand occurred exactly once. */
3553 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3554 arg0 = new0;
3555 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3556 arg0 = new1;
3558 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3559 arg1 = new0;
3560 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3561 arg1 = new1;
3563 return fold_build2_loc (loc, code, type, arg0, arg1);
3566 default:
3567 return arg;
3571 /* Return a tree for the case when the result of an expression is RESULT
3572 converted to TYPE and OMITTED was previously an operand of the expression
3573 but is now not needed (e.g., we folded OMITTED * 0).
3575 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3576 the conversion of RESULT to TYPE. */
3578 tree
3579 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3581 tree t = fold_convert_loc (loc, type, result);
3583 /* If the resulting operand is an empty statement, just return the omitted
3584 statement casted to void. */
3585 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3586 return build1_loc (loc, NOP_EXPR, void_type_node,
3587 fold_ignored_result (omitted));
3589 if (TREE_SIDE_EFFECTS (omitted))
3590 return build2_loc (loc, COMPOUND_EXPR, type,
3591 fold_ignored_result (omitted), t);
3593 return non_lvalue_loc (loc, t);
3596 /* Return a tree for the case when the result of an expression is RESULT
3597 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3598 of the expression but are now not needed.
3600 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3601 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3602 evaluated before OMITTED2. Otherwise, if neither has side effects,
3603 just do the conversion of RESULT to TYPE. */
3605 tree
3606 omit_two_operands_loc (location_t loc, tree type, tree result,
3607 tree omitted1, tree omitted2)
3609 tree t = fold_convert_loc (loc, type, result);
3611 if (TREE_SIDE_EFFECTS (omitted2))
3612 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3613 if (TREE_SIDE_EFFECTS (omitted1))
3614 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3616 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3620 /* Return a simplified tree node for the truth-negation of ARG. This
3621 never alters ARG itself. We assume that ARG is an operation that
3622 returns a truth value (0 or 1).
3624 FIXME: one would think we would fold the result, but it causes
3625 problems with the dominator optimizer. */
3627 static tree
3628 fold_truth_not_expr (location_t loc, tree arg)
3630 tree type = TREE_TYPE (arg);
3631 enum tree_code code = TREE_CODE (arg);
3632 location_t loc1, loc2;
3634 /* If this is a comparison, we can simply invert it, except for
3635 floating-point non-equality comparisons, in which case we just
3636 enclose a TRUTH_NOT_EXPR around what we have. */
3638 if (TREE_CODE_CLASS (code) == tcc_comparison)
3640 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3641 if (FLOAT_TYPE_P (op_type)
3642 && flag_trapping_math
3643 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3644 && code != NE_EXPR && code != EQ_EXPR)
3645 return NULL_TREE;
3647 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3648 if (code == ERROR_MARK)
3649 return NULL_TREE;
3651 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3652 TREE_OPERAND (arg, 1));
3653 if (TREE_NO_WARNING (arg))
3654 TREE_NO_WARNING (ret) = 1;
3655 return ret;
3658 switch (code)
3660 case INTEGER_CST:
3661 return constant_boolean_node (integer_zerop (arg), type);
3663 case TRUTH_AND_EXPR:
3664 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3665 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3666 return build2_loc (loc, TRUTH_OR_EXPR, type,
3667 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3668 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3670 case TRUTH_OR_EXPR:
3671 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3672 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3673 return build2_loc (loc, TRUTH_AND_EXPR, type,
3674 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3675 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3677 case TRUTH_XOR_EXPR:
3678 /* Here we can invert either operand. We invert the first operand
3679 unless the second operand is a TRUTH_NOT_EXPR in which case our
3680 result is the XOR of the first operand with the inside of the
3681 negation of the second operand. */
3683 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3684 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3685 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3686 else
3687 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3688 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3689 TREE_OPERAND (arg, 1));
3691 case TRUTH_ANDIF_EXPR:
3692 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3693 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3694 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3695 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3696 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3698 case TRUTH_ORIF_EXPR:
3699 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3700 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3701 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3702 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3703 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3705 case TRUTH_NOT_EXPR:
3706 return TREE_OPERAND (arg, 0);
3708 case COND_EXPR:
3710 tree arg1 = TREE_OPERAND (arg, 1);
3711 tree arg2 = TREE_OPERAND (arg, 2);
3713 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3714 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3716 /* A COND_EXPR may have a throw as one operand, which
3717 then has void type. Just leave void operands
3718 as they are. */
3719 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3720 VOID_TYPE_P (TREE_TYPE (arg1))
3721 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3722 VOID_TYPE_P (TREE_TYPE (arg2))
3723 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3726 case COMPOUND_EXPR:
3727 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3728 return build2_loc (loc, COMPOUND_EXPR, type,
3729 TREE_OPERAND (arg, 0),
3730 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3732 case NON_LVALUE_EXPR:
3733 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3734 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3736 CASE_CONVERT:
3737 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3738 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3740 /* fall through */
3742 case FLOAT_EXPR:
3743 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3744 return build1_loc (loc, TREE_CODE (arg), type,
3745 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3747 case BIT_AND_EXPR:
3748 if (!integer_onep (TREE_OPERAND (arg, 1)))
3749 return NULL_TREE;
3750 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3752 case SAVE_EXPR:
3753 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3755 case CLEANUP_POINT_EXPR:
3756 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3757 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3758 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3760 default:
3761 return NULL_TREE;
3765 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3766 assume that ARG is an operation that returns a truth value (0 or 1
3767 for scalars, 0 or -1 for vectors). Return the folded expression if
3768 folding is successful. Otherwise, return NULL_TREE. */
3770 static tree
3771 fold_invert_truthvalue (location_t loc, tree arg)
3773 tree type = TREE_TYPE (arg);
3774 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3775 ? BIT_NOT_EXPR
3776 : TRUTH_NOT_EXPR,
3777 type, arg);
3780 /* Return a simplified tree node for the truth-negation of ARG. This
3781 never alters ARG itself. We assume that ARG is an operation that
3782 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3784 tree
3785 invert_truthvalue_loc (location_t loc, tree arg)
3787 if (TREE_CODE (arg) == ERROR_MARK)
3788 return arg;
3790 tree type = TREE_TYPE (arg);
3791 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3792 ? BIT_NOT_EXPR
3793 : TRUTH_NOT_EXPR,
3794 type, arg);
3797 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3798 with code CODE. This optimization is unsafe. */
3799 static tree
3800 distribute_real_division (location_t loc, enum tree_code code, tree type,
3801 tree arg0, tree arg1)
3803 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3804 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3806 /* (A / C) +- (B / C) -> (A +- B) / C. */
3807 if (mul0 == mul1
3808 && operand_equal_p (TREE_OPERAND (arg0, 1),
3809 TREE_OPERAND (arg1, 1), 0))
3810 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3811 fold_build2_loc (loc, code, type,
3812 TREE_OPERAND (arg0, 0),
3813 TREE_OPERAND (arg1, 0)),
3814 TREE_OPERAND (arg0, 1));
3816 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3817 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3818 TREE_OPERAND (arg1, 0), 0)
3819 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3820 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3822 REAL_VALUE_TYPE r0, r1;
3823 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3824 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3825 if (!mul0)
3826 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3827 if (!mul1)
3828 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3829 real_arithmetic (&r0, code, &r0, &r1);
3830 return fold_build2_loc (loc, MULT_EXPR, type,
3831 TREE_OPERAND (arg0, 0),
3832 build_real (type, r0));
3835 return NULL_TREE;
3838 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3839 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3840 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3841 is the original memory reference used to preserve the alias set of
3842 the access. */
3844 static tree
3845 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3846 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3847 int unsignedp, int reversep)
3849 tree result, bftype;
3851 alias_set_type iset = get_alias_set (orig_inner);
3852 if (iset == 0 && get_alias_set (inner) != iset)
3853 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3854 build_fold_addr_expr (inner),
3855 build_int_cst (ptr_type_node, 0));
3857 if (bitpos == 0 && !reversep)
3859 tree size = TYPE_SIZE (TREE_TYPE (inner));
3860 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3861 || POINTER_TYPE_P (TREE_TYPE (inner)))
3862 && tree_fits_shwi_p (size)
3863 && tree_to_shwi (size) == bitsize)
3864 return fold_convert_loc (loc, type, inner);
3867 bftype = type;
3868 if (TYPE_PRECISION (bftype) != bitsize
3869 || TYPE_UNSIGNED (bftype) == !unsignedp)
3870 bftype = build_nonstandard_integer_type (bitsize, 0);
3872 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3873 size_int (bitsize), bitsize_int (bitpos));
3874 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3876 if (bftype != type)
3877 result = fold_convert_loc (loc, type, result);
3879 return result;
3882 /* Optimize a bit-field compare.
3884 There are two cases: First is a compare against a constant and the
3885 second is a comparison of two items where the fields are at the same
3886 bit position relative to the start of a chunk (byte, halfword, word)
3887 large enough to contain it. In these cases we can avoid the shift
3888 implicit in bitfield extractions.
3890 For constants, we emit a compare of the shifted constant with the
3891 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3892 compared. For two fields at the same position, we do the ANDs with the
3893 similar mask and compare the result of the ANDs.
3895 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3896 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3897 are the left and right operands of the comparison, respectively.
3899 If the optimization described above can be done, we return the resulting
3900 tree. Otherwise we return zero. */
3902 static tree
3903 optimize_bit_field_compare (location_t loc, enum tree_code code,
3904 tree compare_type, tree lhs, tree rhs)
3906 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3907 tree type = TREE_TYPE (lhs);
3908 tree unsigned_type;
3909 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3910 machine_mode lmode, rmode, nmode;
3911 int lunsignedp, runsignedp;
3912 int lreversep, rreversep;
3913 int lvolatilep = 0, rvolatilep = 0;
3914 tree linner, rinner = NULL_TREE;
3915 tree mask;
3916 tree offset;
3918 /* Get all the information about the extractions being done. If the bit size
3919 if the same as the size of the underlying object, we aren't doing an
3920 extraction at all and so can do nothing. We also don't want to
3921 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3922 then will no longer be able to replace it. */
3923 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3924 &lunsignedp, &lreversep, &lvolatilep);
3925 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3926 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3927 return 0;
3929 if (const_p)
3930 rreversep = lreversep;
3931 else
3933 /* If this is not a constant, we can only do something if bit positions,
3934 sizes, signedness and storage order are the same. */
3935 rinner
3936 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3937 &runsignedp, &rreversep, &rvolatilep);
3939 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3940 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3941 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3942 return 0;
3945 /* Honor the C++ memory model and mimic what RTL expansion does. */
3946 unsigned HOST_WIDE_INT bitstart = 0;
3947 unsigned HOST_WIDE_INT bitend = 0;
3948 if (TREE_CODE (lhs) == COMPONENT_REF)
3950 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3951 if (offset != NULL_TREE)
3952 return 0;
3955 /* See if we can find a mode to refer to this field. We should be able to,
3956 but fail if we can't. */
3957 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3958 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3959 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3960 TYPE_ALIGN (TREE_TYPE (rinner))),
3961 word_mode, false);
3962 if (nmode == VOIDmode)
3963 return 0;
3965 /* Set signed and unsigned types of the precision of this mode for the
3966 shifts below. */
3967 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3969 /* Compute the bit position and size for the new reference and our offset
3970 within it. If the new reference is the same size as the original, we
3971 won't optimize anything, so return zero. */
3972 nbitsize = GET_MODE_BITSIZE (nmode);
3973 nbitpos = lbitpos & ~ (nbitsize - 1);
3974 lbitpos -= nbitpos;
3975 if (nbitsize == lbitsize)
3976 return 0;
3978 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3979 lbitpos = nbitsize - lbitsize - lbitpos;
3981 /* Make the mask to be used against the extracted field. */
3982 mask = build_int_cst_type (unsigned_type, -1);
3983 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3984 mask = const_binop (RSHIFT_EXPR, mask,
3985 size_int (nbitsize - lbitsize - lbitpos));
3987 if (! const_p)
3988 /* If not comparing with constant, just rework the comparison
3989 and return. */
3990 return fold_build2_loc (loc, code, compare_type,
3991 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3992 make_bit_field_ref (loc, linner, lhs,
3993 unsigned_type,
3994 nbitsize, nbitpos,
3995 1, lreversep),
3996 mask),
3997 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3998 make_bit_field_ref (loc, rinner, rhs,
3999 unsigned_type,
4000 nbitsize, nbitpos,
4001 1, rreversep),
4002 mask));
4004 /* Otherwise, we are handling the constant case. See if the constant is too
4005 big for the field. Warn and return a tree for 0 (false) if so. We do
4006 this not only for its own sake, but to avoid having to test for this
4007 error case below. If we didn't, we might generate wrong code.
4009 For unsigned fields, the constant shifted right by the field length should
4010 be all zero. For signed fields, the high-order bits should agree with
4011 the sign bit. */
4013 if (lunsignedp)
4015 if (wi::lrshift (rhs, lbitsize) != 0)
4017 warning (0, "comparison is always %d due to width of bit-field",
4018 code == NE_EXPR);
4019 return constant_boolean_node (code == NE_EXPR, compare_type);
4022 else
4024 wide_int tem = wi::arshift (rhs, lbitsize - 1);
4025 if (tem != 0 && tem != -1)
4027 warning (0, "comparison is always %d due to width of bit-field",
4028 code == NE_EXPR);
4029 return constant_boolean_node (code == NE_EXPR, compare_type);
4033 /* Single-bit compares should always be against zero. */
4034 if (lbitsize == 1 && ! integer_zerop (rhs))
4036 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4037 rhs = build_int_cst (type, 0);
4040 /* Make a new bitfield reference, shift the constant over the
4041 appropriate number of bits and mask it with the computed mask
4042 (in case this was a signed field). If we changed it, make a new one. */
4043 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4044 nbitsize, nbitpos, 1, lreversep);
4046 rhs = const_binop (BIT_AND_EXPR,
4047 const_binop (LSHIFT_EXPR,
4048 fold_convert_loc (loc, unsigned_type, rhs),
4049 size_int (lbitpos)),
4050 mask);
4052 lhs = build2_loc (loc, code, compare_type,
4053 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4054 return lhs;
4057 /* Subroutine for fold_truth_andor_1: decode a field reference.
4059 If EXP is a comparison reference, we return the innermost reference.
4061 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4062 set to the starting bit number.
4064 If the innermost field can be completely contained in a mode-sized
4065 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4067 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4068 otherwise it is not changed.
4070 *PUNSIGNEDP is set to the signedness of the field.
4072 *PREVERSEP is set to the storage order of the field.
4074 *PMASK is set to the mask used. This is either contained in a
4075 BIT_AND_EXPR or derived from the width of the field.
4077 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4079 Return 0 if this is not a component reference or is one that we can't
4080 do anything with. */
4082 static tree
4083 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4084 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4085 int *punsignedp, int *preversep, int *pvolatilep,
4086 tree *pmask, tree *pand_mask)
4088 tree exp = *exp_;
4089 tree outer_type = 0;
4090 tree and_mask = 0;
4091 tree mask, inner, offset;
4092 tree unsigned_type;
4093 unsigned int precision;
4095 /* All the optimizations using this function assume integer fields.
4096 There are problems with FP fields since the type_for_size call
4097 below can fail for, e.g., XFmode. */
4098 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4099 return 0;
4101 /* We are interested in the bare arrangement of bits, so strip everything
4102 that doesn't affect the machine mode. However, record the type of the
4103 outermost expression if it may matter below. */
4104 if (CONVERT_EXPR_P (exp)
4105 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4106 outer_type = TREE_TYPE (exp);
4107 STRIP_NOPS (exp);
4109 if (TREE_CODE (exp) == BIT_AND_EXPR)
4111 and_mask = TREE_OPERAND (exp, 1);
4112 exp = TREE_OPERAND (exp, 0);
4113 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4114 if (TREE_CODE (and_mask) != INTEGER_CST)
4115 return 0;
4118 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4119 punsignedp, preversep, pvolatilep);
4120 if ((inner == exp && and_mask == 0)
4121 || *pbitsize < 0 || offset != 0
4122 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4123 return 0;
4125 *exp_ = exp;
4127 /* If the number of bits in the reference is the same as the bitsize of
4128 the outer type, then the outer type gives the signedness. Otherwise
4129 (in case of a small bitfield) the signedness is unchanged. */
4130 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4131 *punsignedp = TYPE_UNSIGNED (outer_type);
4133 /* Compute the mask to access the bitfield. */
4134 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4135 precision = TYPE_PRECISION (unsigned_type);
4137 mask = build_int_cst_type (unsigned_type, -1);
4139 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4140 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4142 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4143 if (and_mask != 0)
4144 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4145 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4147 *pmask = mask;
4148 *pand_mask = and_mask;
4149 return inner;
4152 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4153 bit positions and MASK is SIGNED. */
4155 static int
4156 all_ones_mask_p (const_tree mask, unsigned int size)
4158 tree type = TREE_TYPE (mask);
4159 unsigned int precision = TYPE_PRECISION (type);
4161 /* If this function returns true when the type of the mask is
4162 UNSIGNED, then there will be errors. In particular see
4163 gcc.c-torture/execute/990326-1.c. There does not appear to be
4164 any documentation paper trail as to why this is so. But the pre
4165 wide-int worked with that restriction and it has been preserved
4166 here. */
4167 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4168 return false;
4170 return wi::mask (size, false, precision) == mask;
4173 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4174 represents the sign bit of EXP's type. If EXP represents a sign
4175 or zero extension, also test VAL against the unextended type.
4176 The return value is the (sub)expression whose sign bit is VAL,
4177 or NULL_TREE otherwise. */
4179 tree
4180 sign_bit_p (tree exp, const_tree val)
4182 int width;
4183 tree t;
4185 /* Tree EXP must have an integral type. */
4186 t = TREE_TYPE (exp);
4187 if (! INTEGRAL_TYPE_P (t))
4188 return NULL_TREE;
4190 /* Tree VAL must be an integer constant. */
4191 if (TREE_CODE (val) != INTEGER_CST
4192 || TREE_OVERFLOW (val))
4193 return NULL_TREE;
4195 width = TYPE_PRECISION (t);
4196 if (wi::only_sign_bit_p (val, width))
4197 return exp;
4199 /* Handle extension from a narrower type. */
4200 if (TREE_CODE (exp) == NOP_EXPR
4201 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4202 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4204 return NULL_TREE;
4207 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4208 to be evaluated unconditionally. */
4210 static int
4211 simple_operand_p (const_tree exp)
4213 /* Strip any conversions that don't change the machine mode. */
4214 STRIP_NOPS (exp);
4216 return (CONSTANT_CLASS_P (exp)
4217 || TREE_CODE (exp) == SSA_NAME
4218 || (DECL_P (exp)
4219 && ! TREE_ADDRESSABLE (exp)
4220 && ! TREE_THIS_VOLATILE (exp)
4221 && ! DECL_NONLOCAL (exp)
4222 /* Don't regard global variables as simple. They may be
4223 allocated in ways unknown to the compiler (shared memory,
4224 #pragma weak, etc). */
4225 && ! TREE_PUBLIC (exp)
4226 && ! DECL_EXTERNAL (exp)
4227 /* Weakrefs are not safe to be read, since they can be NULL.
4228 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4229 have DECL_WEAK flag set. */
4230 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4231 /* Loading a static variable is unduly expensive, but global
4232 registers aren't expensive. */
4233 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4236 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4237 to be evaluated unconditionally.
4238 I addition to simple_operand_p, we assume that comparisons, conversions,
4239 and logic-not operations are simple, if their operands are simple, too. */
4241 static bool
4242 simple_operand_p_2 (tree exp)
4244 enum tree_code code;
4246 if (TREE_SIDE_EFFECTS (exp)
4247 || tree_could_trap_p (exp))
4248 return false;
4250 while (CONVERT_EXPR_P (exp))
4251 exp = TREE_OPERAND (exp, 0);
4253 code = TREE_CODE (exp);
4255 if (TREE_CODE_CLASS (code) == tcc_comparison)
4256 return (simple_operand_p (TREE_OPERAND (exp, 0))
4257 && simple_operand_p (TREE_OPERAND (exp, 1)));
4259 if (code == TRUTH_NOT_EXPR)
4260 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4262 return simple_operand_p (exp);
4266 /* The following functions are subroutines to fold_range_test and allow it to
4267 try to change a logical combination of comparisons into a range test.
4269 For example, both
4270 X == 2 || X == 3 || X == 4 || X == 5
4272 X >= 2 && X <= 5
4273 are converted to
4274 (unsigned) (X - 2) <= 3
4276 We describe each set of comparisons as being either inside or outside
4277 a range, using a variable named like IN_P, and then describe the
4278 range with a lower and upper bound. If one of the bounds is omitted,
4279 it represents either the highest or lowest value of the type.
4281 In the comments below, we represent a range by two numbers in brackets
4282 preceded by a "+" to designate being inside that range, or a "-" to
4283 designate being outside that range, so the condition can be inverted by
4284 flipping the prefix. An omitted bound is represented by a "-". For
4285 example, "- [-, 10]" means being outside the range starting at the lowest
4286 possible value and ending at 10, in other words, being greater than 10.
4287 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4288 always false.
4290 We set up things so that the missing bounds are handled in a consistent
4291 manner so neither a missing bound nor "true" and "false" need to be
4292 handled using a special case. */
4294 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4295 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4296 and UPPER1_P are nonzero if the respective argument is an upper bound
4297 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4298 must be specified for a comparison. ARG1 will be converted to ARG0's
4299 type if both are specified. */
4301 static tree
4302 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4303 tree arg1, int upper1_p)
4305 tree tem;
4306 int result;
4307 int sgn0, sgn1;
4309 /* If neither arg represents infinity, do the normal operation.
4310 Else, if not a comparison, return infinity. Else handle the special
4311 comparison rules. Note that most of the cases below won't occur, but
4312 are handled for consistency. */
4314 if (arg0 != 0 && arg1 != 0)
4316 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4317 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4318 STRIP_NOPS (tem);
4319 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4322 if (TREE_CODE_CLASS (code) != tcc_comparison)
4323 return 0;
4325 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4326 for neither. In real maths, we cannot assume open ended ranges are
4327 the same. But, this is computer arithmetic, where numbers are finite.
4328 We can therefore make the transformation of any unbounded range with
4329 the value Z, Z being greater than any representable number. This permits
4330 us to treat unbounded ranges as equal. */
4331 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4332 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4333 switch (code)
4335 case EQ_EXPR:
4336 result = sgn0 == sgn1;
4337 break;
4338 case NE_EXPR:
4339 result = sgn0 != sgn1;
4340 break;
4341 case LT_EXPR:
4342 result = sgn0 < sgn1;
4343 break;
4344 case LE_EXPR:
4345 result = sgn0 <= sgn1;
4346 break;
4347 case GT_EXPR:
4348 result = sgn0 > sgn1;
4349 break;
4350 case GE_EXPR:
4351 result = sgn0 >= sgn1;
4352 break;
4353 default:
4354 gcc_unreachable ();
4357 return constant_boolean_node (result, type);
4360 /* Helper routine for make_range. Perform one step for it, return
4361 new expression if the loop should continue or NULL_TREE if it should
4362 stop. */
4364 tree
4365 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4366 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4367 bool *strict_overflow_p)
4369 tree arg0_type = TREE_TYPE (arg0);
4370 tree n_low, n_high, low = *p_low, high = *p_high;
4371 int in_p = *p_in_p, n_in_p;
4373 switch (code)
4375 case TRUTH_NOT_EXPR:
4376 /* We can only do something if the range is testing for zero. */
4377 if (low == NULL_TREE || high == NULL_TREE
4378 || ! integer_zerop (low) || ! integer_zerop (high))
4379 return NULL_TREE;
4380 *p_in_p = ! in_p;
4381 return arg0;
4383 case EQ_EXPR: case NE_EXPR:
4384 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4385 /* We can only do something if the range is testing for zero
4386 and if the second operand is an integer constant. Note that
4387 saying something is "in" the range we make is done by
4388 complementing IN_P since it will set in the initial case of
4389 being not equal to zero; "out" is leaving it alone. */
4390 if (low == NULL_TREE || high == NULL_TREE
4391 || ! integer_zerop (low) || ! integer_zerop (high)
4392 || TREE_CODE (arg1) != INTEGER_CST)
4393 return NULL_TREE;
4395 switch (code)
4397 case NE_EXPR: /* - [c, c] */
4398 low = high = arg1;
4399 break;
4400 case EQ_EXPR: /* + [c, c] */
4401 in_p = ! in_p, low = high = arg1;
4402 break;
4403 case GT_EXPR: /* - [-, c] */
4404 low = 0, high = arg1;
4405 break;
4406 case GE_EXPR: /* + [c, -] */
4407 in_p = ! in_p, low = arg1, high = 0;
4408 break;
4409 case LT_EXPR: /* - [c, -] */
4410 low = arg1, high = 0;
4411 break;
4412 case LE_EXPR: /* + [-, c] */
4413 in_p = ! in_p, low = 0, high = arg1;
4414 break;
4415 default:
4416 gcc_unreachable ();
4419 /* If this is an unsigned comparison, we also know that EXP is
4420 greater than or equal to zero. We base the range tests we make
4421 on that fact, so we record it here so we can parse existing
4422 range tests. We test arg0_type since often the return type
4423 of, e.g. EQ_EXPR, is boolean. */
4424 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4426 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4427 in_p, low, high, 1,
4428 build_int_cst (arg0_type, 0),
4429 NULL_TREE))
4430 return NULL_TREE;
4432 in_p = n_in_p, low = n_low, high = n_high;
4434 /* If the high bound is missing, but we have a nonzero low
4435 bound, reverse the range so it goes from zero to the low bound
4436 minus 1. */
4437 if (high == 0 && low && ! integer_zerop (low))
4439 in_p = ! in_p;
4440 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4441 build_int_cst (TREE_TYPE (low), 1), 0);
4442 low = build_int_cst (arg0_type, 0);
4446 *p_low = low;
4447 *p_high = high;
4448 *p_in_p = in_p;
4449 return arg0;
4451 case NEGATE_EXPR:
4452 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4453 low and high are non-NULL, then normalize will DTRT. */
4454 if (!TYPE_UNSIGNED (arg0_type)
4455 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4457 if (low == NULL_TREE)
4458 low = TYPE_MIN_VALUE (arg0_type);
4459 if (high == NULL_TREE)
4460 high = TYPE_MAX_VALUE (arg0_type);
4463 /* (-x) IN [a,b] -> x in [-b, -a] */
4464 n_low = range_binop (MINUS_EXPR, exp_type,
4465 build_int_cst (exp_type, 0),
4466 0, high, 1);
4467 n_high = range_binop (MINUS_EXPR, exp_type,
4468 build_int_cst (exp_type, 0),
4469 0, low, 0);
4470 if (n_high != 0 && TREE_OVERFLOW (n_high))
4471 return NULL_TREE;
4472 goto normalize;
4474 case BIT_NOT_EXPR:
4475 /* ~ X -> -X - 1 */
4476 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4477 build_int_cst (exp_type, 1));
4479 case PLUS_EXPR:
4480 case MINUS_EXPR:
4481 if (TREE_CODE (arg1) != INTEGER_CST)
4482 return NULL_TREE;
4484 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4485 move a constant to the other side. */
4486 if (!TYPE_UNSIGNED (arg0_type)
4487 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4488 return NULL_TREE;
4490 /* If EXP is signed, any overflow in the computation is undefined,
4491 so we don't worry about it so long as our computations on
4492 the bounds don't overflow. For unsigned, overflow is defined
4493 and this is exactly the right thing. */
4494 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4495 arg0_type, low, 0, arg1, 0);
4496 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4497 arg0_type, high, 1, arg1, 0);
4498 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4499 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4500 return NULL_TREE;
4502 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4503 *strict_overflow_p = true;
4505 normalize:
4506 /* Check for an unsigned range which has wrapped around the maximum
4507 value thus making n_high < n_low, and normalize it. */
4508 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4510 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4511 build_int_cst (TREE_TYPE (n_high), 1), 0);
4512 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4513 build_int_cst (TREE_TYPE (n_low), 1), 0);
4515 /* If the range is of the form +/- [ x+1, x ], we won't
4516 be able to normalize it. But then, it represents the
4517 whole range or the empty set, so make it
4518 +/- [ -, - ]. */
4519 if (tree_int_cst_equal (n_low, low)
4520 && tree_int_cst_equal (n_high, high))
4521 low = high = 0;
4522 else
4523 in_p = ! in_p;
4525 else
4526 low = n_low, high = n_high;
4528 *p_low = low;
4529 *p_high = high;
4530 *p_in_p = in_p;
4531 return arg0;
4533 CASE_CONVERT:
4534 case NON_LVALUE_EXPR:
4535 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4536 return NULL_TREE;
4538 if (! INTEGRAL_TYPE_P (arg0_type)
4539 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4540 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4541 return NULL_TREE;
4543 n_low = low, n_high = high;
4545 if (n_low != 0)
4546 n_low = fold_convert_loc (loc, arg0_type, n_low);
4548 if (n_high != 0)
4549 n_high = fold_convert_loc (loc, arg0_type, n_high);
4551 /* If we're converting arg0 from an unsigned type, to exp,
4552 a signed type, we will be doing the comparison as unsigned.
4553 The tests above have already verified that LOW and HIGH
4554 are both positive.
4556 So we have to ensure that we will handle large unsigned
4557 values the same way that the current signed bounds treat
4558 negative values. */
4560 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4562 tree high_positive;
4563 tree equiv_type;
4564 /* For fixed-point modes, we need to pass the saturating flag
4565 as the 2nd parameter. */
4566 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4567 equiv_type
4568 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4569 TYPE_SATURATING (arg0_type));
4570 else
4571 equiv_type
4572 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4574 /* A range without an upper bound is, naturally, unbounded.
4575 Since convert would have cropped a very large value, use
4576 the max value for the destination type. */
4577 high_positive
4578 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4579 : TYPE_MAX_VALUE (arg0_type);
4581 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4582 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4583 fold_convert_loc (loc, arg0_type,
4584 high_positive),
4585 build_int_cst (arg0_type, 1));
4587 /* If the low bound is specified, "and" the range with the
4588 range for which the original unsigned value will be
4589 positive. */
4590 if (low != 0)
4592 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4593 1, fold_convert_loc (loc, arg0_type,
4594 integer_zero_node),
4595 high_positive))
4596 return NULL_TREE;
4598 in_p = (n_in_p == in_p);
4600 else
4602 /* Otherwise, "or" the range with the range of the input
4603 that will be interpreted as negative. */
4604 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4605 1, fold_convert_loc (loc, arg0_type,
4606 integer_zero_node),
4607 high_positive))
4608 return NULL_TREE;
4610 in_p = (in_p != n_in_p);
4614 *p_low = n_low;
4615 *p_high = n_high;
4616 *p_in_p = in_p;
4617 return arg0;
4619 default:
4620 return NULL_TREE;
4624 /* Given EXP, a logical expression, set the range it is testing into
4625 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4626 actually being tested. *PLOW and *PHIGH will be made of the same
4627 type as the returned expression. If EXP is not a comparison, we
4628 will most likely not be returning a useful value and range. Set
4629 *STRICT_OVERFLOW_P to true if the return value is only valid
4630 because signed overflow is undefined; otherwise, do not change
4631 *STRICT_OVERFLOW_P. */
4633 tree
4634 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4635 bool *strict_overflow_p)
4637 enum tree_code code;
4638 tree arg0, arg1 = NULL_TREE;
4639 tree exp_type, nexp;
4640 int in_p;
4641 tree low, high;
4642 location_t loc = EXPR_LOCATION (exp);
4644 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4645 and see if we can refine the range. Some of the cases below may not
4646 happen, but it doesn't seem worth worrying about this. We "continue"
4647 the outer loop when we've changed something; otherwise we "break"
4648 the switch, which will "break" the while. */
4650 in_p = 0;
4651 low = high = build_int_cst (TREE_TYPE (exp), 0);
4653 while (1)
4655 code = TREE_CODE (exp);
4656 exp_type = TREE_TYPE (exp);
4657 arg0 = NULL_TREE;
4659 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4661 if (TREE_OPERAND_LENGTH (exp) > 0)
4662 arg0 = TREE_OPERAND (exp, 0);
4663 if (TREE_CODE_CLASS (code) == tcc_binary
4664 || TREE_CODE_CLASS (code) == tcc_comparison
4665 || (TREE_CODE_CLASS (code) == tcc_expression
4666 && TREE_OPERAND_LENGTH (exp) > 1))
4667 arg1 = TREE_OPERAND (exp, 1);
4669 if (arg0 == NULL_TREE)
4670 break;
4672 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4673 &high, &in_p, strict_overflow_p);
4674 if (nexp == NULL_TREE)
4675 break;
4676 exp = nexp;
4679 /* If EXP is a constant, we can evaluate whether this is true or false. */
4680 if (TREE_CODE (exp) == INTEGER_CST)
4682 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4683 exp, 0, low, 0))
4684 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4685 exp, 1, high, 1)));
4686 low = high = 0;
4687 exp = 0;
4690 *pin_p = in_p, *plow = low, *phigh = high;
4691 return exp;
4694 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4695 type, TYPE, return an expression to test if EXP is in (or out of, depending
4696 on IN_P) the range. Return 0 if the test couldn't be created. */
4698 tree
4699 build_range_check (location_t loc, tree type, tree exp, int in_p,
4700 tree low, tree high)
4702 tree etype = TREE_TYPE (exp), value;
4704 /* Disable this optimization for function pointer expressions
4705 on targets that require function pointer canonicalization. */
4706 if (targetm.have_canonicalize_funcptr_for_compare ()
4707 && TREE_CODE (etype) == POINTER_TYPE
4708 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4709 return NULL_TREE;
4711 if (! in_p)
4713 value = build_range_check (loc, type, exp, 1, low, high);
4714 if (value != 0)
4715 return invert_truthvalue_loc (loc, value);
4717 return 0;
4720 if (low == 0 && high == 0)
4721 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4723 if (low == 0)
4724 return fold_build2_loc (loc, LE_EXPR, type, exp,
4725 fold_convert_loc (loc, etype, high));
4727 if (high == 0)
4728 return fold_build2_loc (loc, GE_EXPR, type, exp,
4729 fold_convert_loc (loc, etype, low));
4731 if (operand_equal_p (low, high, 0))
4732 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4733 fold_convert_loc (loc, etype, low));
4735 if (integer_zerop (low))
4737 if (! TYPE_UNSIGNED (etype))
4739 etype = unsigned_type_for (etype);
4740 high = fold_convert_loc (loc, etype, high);
4741 exp = fold_convert_loc (loc, etype, exp);
4743 return build_range_check (loc, type, exp, 1, 0, high);
4746 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4747 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4749 int prec = TYPE_PRECISION (etype);
4751 if (wi::mask (prec - 1, false, prec) == high)
4753 if (TYPE_UNSIGNED (etype))
4755 tree signed_etype = signed_type_for (etype);
4756 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4757 etype
4758 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4759 else
4760 etype = signed_etype;
4761 exp = fold_convert_loc (loc, etype, exp);
4763 return fold_build2_loc (loc, GT_EXPR, type, exp,
4764 build_int_cst (etype, 0));
4768 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4769 This requires wrap-around arithmetics for the type of the expression.
4770 First make sure that arithmetics in this type is valid, then make sure
4771 that it wraps around. */
4772 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4773 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4774 TYPE_UNSIGNED (etype));
4776 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4778 tree utype, minv, maxv;
4780 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4781 for the type in question, as we rely on this here. */
4782 utype = unsigned_type_for (etype);
4783 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4784 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4785 build_int_cst (TREE_TYPE (maxv), 1), 1);
4786 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4788 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4789 minv, 1, maxv, 1)))
4790 etype = utype;
4791 else
4792 return 0;
4795 high = fold_convert_loc (loc, etype, high);
4796 low = fold_convert_loc (loc, etype, low);
4797 exp = fold_convert_loc (loc, etype, exp);
4799 value = const_binop (MINUS_EXPR, high, low);
4802 if (POINTER_TYPE_P (etype))
4804 if (value != 0 && !TREE_OVERFLOW (value))
4806 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4807 return build_range_check (loc, type,
4808 fold_build_pointer_plus_loc (loc, exp, low),
4809 1, build_int_cst (etype, 0), value);
4811 return 0;
4814 if (value != 0 && !TREE_OVERFLOW (value))
4815 return build_range_check (loc, type,
4816 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4817 1, build_int_cst (etype, 0), value);
4819 return 0;
4822 /* Return the predecessor of VAL in its type, handling the infinite case. */
4824 static tree
4825 range_predecessor (tree val)
4827 tree type = TREE_TYPE (val);
4829 if (INTEGRAL_TYPE_P (type)
4830 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4831 return 0;
4832 else
4833 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4834 build_int_cst (TREE_TYPE (val), 1), 0);
4837 /* Return the successor of VAL in its type, handling the infinite case. */
4839 static tree
4840 range_successor (tree val)
4842 tree type = TREE_TYPE (val);
4844 if (INTEGRAL_TYPE_P (type)
4845 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4846 return 0;
4847 else
4848 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4849 build_int_cst (TREE_TYPE (val), 1), 0);
4852 /* Given two ranges, see if we can merge them into one. Return 1 if we
4853 can, 0 if we can't. Set the output range into the specified parameters. */
4855 bool
4856 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4857 tree high0, int in1_p, tree low1, tree high1)
4859 int no_overlap;
4860 int subset;
4861 int temp;
4862 tree tem;
4863 int in_p;
4864 tree low, high;
4865 int lowequal = ((low0 == 0 && low1 == 0)
4866 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4867 low0, 0, low1, 0)));
4868 int highequal = ((high0 == 0 && high1 == 0)
4869 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4870 high0, 1, high1, 1)));
4872 /* Make range 0 be the range that starts first, or ends last if they
4873 start at the same value. Swap them if it isn't. */
4874 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4875 low0, 0, low1, 0))
4876 || (lowequal
4877 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4878 high1, 1, high0, 1))))
4880 temp = in0_p, in0_p = in1_p, in1_p = temp;
4881 tem = low0, low0 = low1, low1 = tem;
4882 tem = high0, high0 = high1, high1 = tem;
4885 /* Now flag two cases, whether the ranges are disjoint or whether the
4886 second range is totally subsumed in the first. Note that the tests
4887 below are simplified by the ones above. */
4888 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4889 high0, 1, low1, 0));
4890 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4891 high1, 1, high0, 1));
4893 /* We now have four cases, depending on whether we are including or
4894 excluding the two ranges. */
4895 if (in0_p && in1_p)
4897 /* If they don't overlap, the result is false. If the second range
4898 is a subset it is the result. Otherwise, the range is from the start
4899 of the second to the end of the first. */
4900 if (no_overlap)
4901 in_p = 0, low = high = 0;
4902 else if (subset)
4903 in_p = 1, low = low1, high = high1;
4904 else
4905 in_p = 1, low = low1, high = high0;
4908 else if (in0_p && ! in1_p)
4910 /* If they don't overlap, the result is the first range. If they are
4911 equal, the result is false. If the second range is a subset of the
4912 first, and the ranges begin at the same place, we go from just after
4913 the end of the second range to the end of the first. If the second
4914 range is not a subset of the first, or if it is a subset and both
4915 ranges end at the same place, the range starts at the start of the
4916 first range and ends just before the second range.
4917 Otherwise, we can't describe this as a single range. */
4918 if (no_overlap)
4919 in_p = 1, low = low0, high = high0;
4920 else if (lowequal && highequal)
4921 in_p = 0, low = high = 0;
4922 else if (subset && lowequal)
4924 low = range_successor (high1);
4925 high = high0;
4926 in_p = 1;
4927 if (low == 0)
4929 /* We are in the weird situation where high0 > high1 but
4930 high1 has no successor. Punt. */
4931 return 0;
4934 else if (! subset || highequal)
4936 low = low0;
4937 high = range_predecessor (low1);
4938 in_p = 1;
4939 if (high == 0)
4941 /* low0 < low1 but low1 has no predecessor. Punt. */
4942 return 0;
4945 else
4946 return 0;
4949 else if (! in0_p && in1_p)
4951 /* If they don't overlap, the result is the second range. If the second
4952 is a subset of the first, the result is false. Otherwise,
4953 the range starts just after the first range and ends at the
4954 end of the second. */
4955 if (no_overlap)
4956 in_p = 1, low = low1, high = high1;
4957 else if (subset || highequal)
4958 in_p = 0, low = high = 0;
4959 else
4961 low = range_successor (high0);
4962 high = high1;
4963 in_p = 1;
4964 if (low == 0)
4966 /* high1 > high0 but high0 has no successor. Punt. */
4967 return 0;
4972 else
4974 /* The case where we are excluding both ranges. Here the complex case
4975 is if they don't overlap. In that case, the only time we have a
4976 range is if they are adjacent. If the second is a subset of the
4977 first, the result is the first. Otherwise, the range to exclude
4978 starts at the beginning of the first range and ends at the end of the
4979 second. */
4980 if (no_overlap)
4982 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4983 range_successor (high0),
4984 1, low1, 0)))
4985 in_p = 0, low = low0, high = high1;
4986 else
4988 /* Canonicalize - [min, x] into - [-, x]. */
4989 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4990 switch (TREE_CODE (TREE_TYPE (low0)))
4992 case ENUMERAL_TYPE:
4993 if (TYPE_PRECISION (TREE_TYPE (low0))
4994 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4995 break;
4996 /* FALLTHROUGH */
4997 case INTEGER_TYPE:
4998 if (tree_int_cst_equal (low0,
4999 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5000 low0 = 0;
5001 break;
5002 case POINTER_TYPE:
5003 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5004 && integer_zerop (low0))
5005 low0 = 0;
5006 break;
5007 default:
5008 break;
5011 /* Canonicalize - [x, max] into - [x, -]. */
5012 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5013 switch (TREE_CODE (TREE_TYPE (high1)))
5015 case ENUMERAL_TYPE:
5016 if (TYPE_PRECISION (TREE_TYPE (high1))
5017 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5018 break;
5019 /* FALLTHROUGH */
5020 case INTEGER_TYPE:
5021 if (tree_int_cst_equal (high1,
5022 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5023 high1 = 0;
5024 break;
5025 case POINTER_TYPE:
5026 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5027 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5028 high1, 1,
5029 build_int_cst (TREE_TYPE (high1), 1),
5030 1)))
5031 high1 = 0;
5032 break;
5033 default:
5034 break;
5037 /* The ranges might be also adjacent between the maximum and
5038 minimum values of the given type. For
5039 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5040 return + [x + 1, y - 1]. */
5041 if (low0 == 0 && high1 == 0)
5043 low = range_successor (high0);
5044 high = range_predecessor (low1);
5045 if (low == 0 || high == 0)
5046 return 0;
5048 in_p = 1;
5050 else
5051 return 0;
5054 else if (subset)
5055 in_p = 0, low = low0, high = high0;
5056 else
5057 in_p = 0, low = low0, high = high1;
5060 *pin_p = in_p, *plow = low, *phigh = high;
5061 return 1;
5065 /* Subroutine of fold, looking inside expressions of the form
5066 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5067 of the COND_EXPR. This function is being used also to optimize
5068 A op B ? C : A, by reversing the comparison first.
5070 Return a folded expression whose code is not a COND_EXPR
5071 anymore, or NULL_TREE if no folding opportunity is found. */
5073 static tree
5074 fold_cond_expr_with_comparison (location_t loc, tree type,
5075 tree arg0, tree arg1, tree arg2)
5077 enum tree_code comp_code = TREE_CODE (arg0);
5078 tree arg00 = TREE_OPERAND (arg0, 0);
5079 tree arg01 = TREE_OPERAND (arg0, 1);
5080 tree arg1_type = TREE_TYPE (arg1);
5081 tree tem;
5083 STRIP_NOPS (arg1);
5084 STRIP_NOPS (arg2);
5086 /* If we have A op 0 ? A : -A, consider applying the following
5087 transformations:
5089 A == 0? A : -A same as -A
5090 A != 0? A : -A same as A
5091 A >= 0? A : -A same as abs (A)
5092 A > 0? A : -A same as abs (A)
5093 A <= 0? A : -A same as -abs (A)
5094 A < 0? A : -A same as -abs (A)
5096 None of these transformations work for modes with signed
5097 zeros. If A is +/-0, the first two transformations will
5098 change the sign of the result (from +0 to -0, or vice
5099 versa). The last four will fix the sign of the result,
5100 even though the original expressions could be positive or
5101 negative, depending on the sign of A.
5103 Note that all these transformations are correct if A is
5104 NaN, since the two alternatives (A and -A) are also NaNs. */
5105 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5106 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5107 ? real_zerop (arg01)
5108 : integer_zerop (arg01))
5109 && ((TREE_CODE (arg2) == NEGATE_EXPR
5110 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5111 /* In the case that A is of the form X-Y, '-A' (arg2) may
5112 have already been folded to Y-X, check for that. */
5113 || (TREE_CODE (arg1) == MINUS_EXPR
5114 && TREE_CODE (arg2) == MINUS_EXPR
5115 && operand_equal_p (TREE_OPERAND (arg1, 0),
5116 TREE_OPERAND (arg2, 1), 0)
5117 && operand_equal_p (TREE_OPERAND (arg1, 1),
5118 TREE_OPERAND (arg2, 0), 0))))
5119 switch (comp_code)
5121 case EQ_EXPR:
5122 case UNEQ_EXPR:
5123 tem = fold_convert_loc (loc, arg1_type, arg1);
5124 return fold_convert_loc (loc, type, negate_expr (tem));
5125 case NE_EXPR:
5126 case LTGT_EXPR:
5127 return fold_convert_loc (loc, type, arg1);
5128 case UNGE_EXPR:
5129 case UNGT_EXPR:
5130 if (flag_trapping_math)
5131 break;
5132 /* Fall through. */
5133 case GE_EXPR:
5134 case GT_EXPR:
5135 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5136 break;
5137 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5138 return fold_convert_loc (loc, type, tem);
5139 case UNLE_EXPR:
5140 case UNLT_EXPR:
5141 if (flag_trapping_math)
5142 break;
5143 /* FALLTHRU */
5144 case LE_EXPR:
5145 case LT_EXPR:
5146 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5147 break;
5148 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5149 return negate_expr (fold_convert_loc (loc, type, tem));
5150 default:
5151 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5152 break;
5155 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5156 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5157 both transformations are correct when A is NaN: A != 0
5158 is then true, and A == 0 is false. */
5160 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5161 && integer_zerop (arg01) && integer_zerop (arg2))
5163 if (comp_code == NE_EXPR)
5164 return fold_convert_loc (loc, type, arg1);
5165 else if (comp_code == EQ_EXPR)
5166 return build_zero_cst (type);
5169 /* Try some transformations of A op B ? A : B.
5171 A == B? A : B same as B
5172 A != B? A : B same as A
5173 A >= B? A : B same as max (A, B)
5174 A > B? A : B same as max (B, A)
5175 A <= B? A : B same as min (A, B)
5176 A < B? A : B same as min (B, A)
5178 As above, these transformations don't work in the presence
5179 of signed zeros. For example, if A and B are zeros of
5180 opposite sign, the first two transformations will change
5181 the sign of the result. In the last four, the original
5182 expressions give different results for (A=+0, B=-0) and
5183 (A=-0, B=+0), but the transformed expressions do not.
5185 The first two transformations are correct if either A or B
5186 is a NaN. In the first transformation, the condition will
5187 be false, and B will indeed be chosen. In the case of the
5188 second transformation, the condition A != B will be true,
5189 and A will be chosen.
5191 The conversions to max() and min() are not correct if B is
5192 a number and A is not. The conditions in the original
5193 expressions will be false, so all four give B. The min()
5194 and max() versions would give a NaN instead. */
5195 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5196 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5197 /* Avoid these transformations if the COND_EXPR may be used
5198 as an lvalue in the C++ front-end. PR c++/19199. */
5199 && (in_gimple_form
5200 || VECTOR_TYPE_P (type)
5201 || (! lang_GNU_CXX ()
5202 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5203 || ! maybe_lvalue_p (arg1)
5204 || ! maybe_lvalue_p (arg2)))
5206 tree comp_op0 = arg00;
5207 tree comp_op1 = arg01;
5208 tree comp_type = TREE_TYPE (comp_op0);
5210 switch (comp_code)
5212 case EQ_EXPR:
5213 return fold_convert_loc (loc, type, arg2);
5214 case NE_EXPR:
5215 return fold_convert_loc (loc, type, arg1);
5216 case LE_EXPR:
5217 case LT_EXPR:
5218 case UNLE_EXPR:
5219 case UNLT_EXPR:
5220 /* In C++ a ?: expression can be an lvalue, so put the
5221 operand which will be used if they are equal first
5222 so that we can convert this back to the
5223 corresponding COND_EXPR. */
5224 if (!HONOR_NANS (arg1))
5226 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5227 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5228 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5229 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5230 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5231 comp_op1, comp_op0);
5232 return fold_convert_loc (loc, type, tem);
5234 break;
5235 case GE_EXPR:
5236 case GT_EXPR:
5237 case UNGE_EXPR:
5238 case UNGT_EXPR:
5239 if (!HONOR_NANS (arg1))
5241 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5242 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5243 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5244 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5245 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5246 comp_op1, comp_op0);
5247 return fold_convert_loc (loc, type, tem);
5249 break;
5250 case UNEQ_EXPR:
5251 if (!HONOR_NANS (arg1))
5252 return fold_convert_loc (loc, type, arg2);
5253 break;
5254 case LTGT_EXPR:
5255 if (!HONOR_NANS (arg1))
5256 return fold_convert_loc (loc, type, arg1);
5257 break;
5258 default:
5259 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5260 break;
5264 return NULL_TREE;
5269 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5270 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5271 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5272 false) >= 2)
5273 #endif
5275 /* EXP is some logical combination of boolean tests. See if we can
5276 merge it into some range test. Return the new tree if so. */
5278 static tree
5279 fold_range_test (location_t loc, enum tree_code code, tree type,
5280 tree op0, tree op1)
5282 int or_op = (code == TRUTH_ORIF_EXPR
5283 || code == TRUTH_OR_EXPR);
5284 int in0_p, in1_p, in_p;
5285 tree low0, low1, low, high0, high1, high;
5286 bool strict_overflow_p = false;
5287 tree tem, lhs, rhs;
5288 const char * const warnmsg = G_("assuming signed overflow does not occur "
5289 "when simplifying range test");
5291 if (!INTEGRAL_TYPE_P (type))
5292 return 0;
5294 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5295 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5297 /* If this is an OR operation, invert both sides; we will invert
5298 again at the end. */
5299 if (or_op)
5300 in0_p = ! in0_p, in1_p = ! in1_p;
5302 /* If both expressions are the same, if we can merge the ranges, and we
5303 can build the range test, return it or it inverted. If one of the
5304 ranges is always true or always false, consider it to be the same
5305 expression as the other. */
5306 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5307 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5308 in1_p, low1, high1)
5309 && 0 != (tem = (build_range_check (loc, type,
5310 lhs != 0 ? lhs
5311 : rhs != 0 ? rhs : integer_zero_node,
5312 in_p, low, high))))
5314 if (strict_overflow_p)
5315 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5316 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5319 /* On machines where the branch cost is expensive, if this is a
5320 short-circuited branch and the underlying object on both sides
5321 is the same, make a non-short-circuit operation. */
5322 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5323 && lhs != 0 && rhs != 0
5324 && (code == TRUTH_ANDIF_EXPR
5325 || code == TRUTH_ORIF_EXPR)
5326 && operand_equal_p (lhs, rhs, 0))
5328 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5329 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5330 which cases we can't do this. */
5331 if (simple_operand_p (lhs))
5332 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5333 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5334 type, op0, op1);
5336 else if (!lang_hooks.decls.global_bindings_p ()
5337 && !CONTAINS_PLACEHOLDER_P (lhs))
5339 tree common = save_expr (lhs);
5341 if (0 != (lhs = build_range_check (loc, type, common,
5342 or_op ? ! in0_p : in0_p,
5343 low0, high0))
5344 && (0 != (rhs = build_range_check (loc, type, common,
5345 or_op ? ! in1_p : in1_p,
5346 low1, high1))))
5348 if (strict_overflow_p)
5349 fold_overflow_warning (warnmsg,
5350 WARN_STRICT_OVERFLOW_COMPARISON);
5351 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5352 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5353 type, lhs, rhs);
5358 return 0;
5361 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5362 bit value. Arrange things so the extra bits will be set to zero if and
5363 only if C is signed-extended to its full width. If MASK is nonzero,
5364 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5366 static tree
5367 unextend (tree c, int p, int unsignedp, tree mask)
5369 tree type = TREE_TYPE (c);
5370 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5371 tree temp;
5373 if (p == modesize || unsignedp)
5374 return c;
5376 /* We work by getting just the sign bit into the low-order bit, then
5377 into the high-order bit, then sign-extend. We then XOR that value
5378 with C. */
5379 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5381 /* We must use a signed type in order to get an arithmetic right shift.
5382 However, we must also avoid introducing accidental overflows, so that
5383 a subsequent call to integer_zerop will work. Hence we must
5384 do the type conversion here. At this point, the constant is either
5385 zero or one, and the conversion to a signed type can never overflow.
5386 We could get an overflow if this conversion is done anywhere else. */
5387 if (TYPE_UNSIGNED (type))
5388 temp = fold_convert (signed_type_for (type), temp);
5390 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5391 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5392 if (mask != 0)
5393 temp = const_binop (BIT_AND_EXPR, temp,
5394 fold_convert (TREE_TYPE (c), mask));
5395 /* If necessary, convert the type back to match the type of C. */
5396 if (TYPE_UNSIGNED (type))
5397 temp = fold_convert (type, temp);
5399 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5402 /* For an expression that has the form
5403 (A && B) || ~B
5405 (A || B) && ~B,
5406 we can drop one of the inner expressions and simplify to
5407 A || ~B
5409 A && ~B
5410 LOC is the location of the resulting expression. OP is the inner
5411 logical operation; the left-hand side in the examples above, while CMPOP
5412 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5413 removing a condition that guards another, as in
5414 (A != NULL && A->...) || A == NULL
5415 which we must not transform. If RHS_ONLY is true, only eliminate the
5416 right-most operand of the inner logical operation. */
5418 static tree
5419 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5420 bool rhs_only)
5422 tree type = TREE_TYPE (cmpop);
5423 enum tree_code code = TREE_CODE (cmpop);
5424 enum tree_code truthop_code = TREE_CODE (op);
5425 tree lhs = TREE_OPERAND (op, 0);
5426 tree rhs = TREE_OPERAND (op, 1);
5427 tree orig_lhs = lhs, orig_rhs = rhs;
5428 enum tree_code rhs_code = TREE_CODE (rhs);
5429 enum tree_code lhs_code = TREE_CODE (lhs);
5430 enum tree_code inv_code;
5432 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5433 return NULL_TREE;
5435 if (TREE_CODE_CLASS (code) != tcc_comparison)
5436 return NULL_TREE;
5438 if (rhs_code == truthop_code)
5440 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5441 if (newrhs != NULL_TREE)
5443 rhs = newrhs;
5444 rhs_code = TREE_CODE (rhs);
5447 if (lhs_code == truthop_code && !rhs_only)
5449 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5450 if (newlhs != NULL_TREE)
5452 lhs = newlhs;
5453 lhs_code = TREE_CODE (lhs);
5457 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5458 if (inv_code == rhs_code
5459 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5460 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5461 return lhs;
5462 if (!rhs_only && inv_code == lhs_code
5463 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5464 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5465 return rhs;
5466 if (rhs != orig_rhs || lhs != orig_lhs)
5467 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5468 lhs, rhs);
5469 return NULL_TREE;
5472 /* Find ways of folding logical expressions of LHS and RHS:
5473 Try to merge two comparisons to the same innermost item.
5474 Look for range tests like "ch >= '0' && ch <= '9'".
5475 Look for combinations of simple terms on machines with expensive branches
5476 and evaluate the RHS unconditionally.
5478 For example, if we have p->a == 2 && p->b == 4 and we can make an
5479 object large enough to span both A and B, we can do this with a comparison
5480 against the object ANDed with the a mask.
5482 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5483 operations to do this with one comparison.
5485 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5486 function and the one above.
5488 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5489 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5491 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5492 two operands.
5494 We return the simplified tree or 0 if no optimization is possible. */
5496 static tree
5497 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5498 tree lhs, tree rhs)
5500 /* If this is the "or" of two comparisons, we can do something if
5501 the comparisons are NE_EXPR. If this is the "and", we can do something
5502 if the comparisons are EQ_EXPR. I.e.,
5503 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5505 WANTED_CODE is this operation code. For single bit fields, we can
5506 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5507 comparison for one-bit fields. */
5509 enum tree_code wanted_code;
5510 enum tree_code lcode, rcode;
5511 tree ll_arg, lr_arg, rl_arg, rr_arg;
5512 tree ll_inner, lr_inner, rl_inner, rr_inner;
5513 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5514 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5515 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5516 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5517 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5518 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5519 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5520 machine_mode lnmode, rnmode;
5521 tree ll_mask, lr_mask, rl_mask, rr_mask;
5522 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5523 tree l_const, r_const;
5524 tree lntype, rntype, result;
5525 HOST_WIDE_INT first_bit, end_bit;
5526 int volatilep;
5528 /* Start by getting the comparison codes. Fail if anything is volatile.
5529 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5530 it were surrounded with a NE_EXPR. */
5532 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5533 return 0;
5535 lcode = TREE_CODE (lhs);
5536 rcode = TREE_CODE (rhs);
5538 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5540 lhs = build2 (NE_EXPR, truth_type, lhs,
5541 build_int_cst (TREE_TYPE (lhs), 0));
5542 lcode = NE_EXPR;
5545 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5547 rhs = build2 (NE_EXPR, truth_type, rhs,
5548 build_int_cst (TREE_TYPE (rhs), 0));
5549 rcode = NE_EXPR;
5552 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5553 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5554 return 0;
5556 ll_arg = TREE_OPERAND (lhs, 0);
5557 lr_arg = TREE_OPERAND (lhs, 1);
5558 rl_arg = TREE_OPERAND (rhs, 0);
5559 rr_arg = TREE_OPERAND (rhs, 1);
5561 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5562 if (simple_operand_p (ll_arg)
5563 && simple_operand_p (lr_arg))
5565 if (operand_equal_p (ll_arg, rl_arg, 0)
5566 && operand_equal_p (lr_arg, rr_arg, 0))
5568 result = combine_comparisons (loc, code, lcode, rcode,
5569 truth_type, ll_arg, lr_arg);
5570 if (result)
5571 return result;
5573 else if (operand_equal_p (ll_arg, rr_arg, 0)
5574 && operand_equal_p (lr_arg, rl_arg, 0))
5576 result = combine_comparisons (loc, code, lcode,
5577 swap_tree_comparison (rcode),
5578 truth_type, ll_arg, lr_arg);
5579 if (result)
5580 return result;
5584 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5585 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5587 /* If the RHS can be evaluated unconditionally and its operands are
5588 simple, it wins to evaluate the RHS unconditionally on machines
5589 with expensive branches. In this case, this isn't a comparison
5590 that can be merged. */
5592 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5593 false) >= 2
5594 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5595 && simple_operand_p (rl_arg)
5596 && simple_operand_p (rr_arg))
5598 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5599 if (code == TRUTH_OR_EXPR
5600 && lcode == NE_EXPR && integer_zerop (lr_arg)
5601 && rcode == NE_EXPR && integer_zerop (rr_arg)
5602 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5603 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5604 return build2_loc (loc, NE_EXPR, truth_type,
5605 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5606 ll_arg, rl_arg),
5607 build_int_cst (TREE_TYPE (ll_arg), 0));
5609 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5610 if (code == TRUTH_AND_EXPR
5611 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5612 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5613 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5614 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5615 return build2_loc (loc, EQ_EXPR, truth_type,
5616 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5617 ll_arg, rl_arg),
5618 build_int_cst (TREE_TYPE (ll_arg), 0));
5621 /* See if the comparisons can be merged. Then get all the parameters for
5622 each side. */
5624 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5625 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5626 return 0;
5628 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5629 volatilep = 0;
5630 ll_inner = decode_field_reference (loc, &ll_arg,
5631 &ll_bitsize, &ll_bitpos, &ll_mode,
5632 &ll_unsignedp, &ll_reversep, &volatilep,
5633 &ll_mask, &ll_and_mask);
5634 lr_inner = decode_field_reference (loc, &lr_arg,
5635 &lr_bitsize, &lr_bitpos, &lr_mode,
5636 &lr_unsignedp, &lr_reversep, &volatilep,
5637 &lr_mask, &lr_and_mask);
5638 rl_inner = decode_field_reference (loc, &rl_arg,
5639 &rl_bitsize, &rl_bitpos, &rl_mode,
5640 &rl_unsignedp, &rl_reversep, &volatilep,
5641 &rl_mask, &rl_and_mask);
5642 rr_inner = decode_field_reference (loc, &rr_arg,
5643 &rr_bitsize, &rr_bitpos, &rr_mode,
5644 &rr_unsignedp, &rr_reversep, &volatilep,
5645 &rr_mask, &rr_and_mask);
5647 /* It must be true that the inner operation on the lhs of each
5648 comparison must be the same if we are to be able to do anything.
5649 Then see if we have constants. If not, the same must be true for
5650 the rhs's. */
5651 if (volatilep
5652 || ll_reversep != rl_reversep
5653 || ll_inner == 0 || rl_inner == 0
5654 || ! operand_equal_p (ll_inner, rl_inner, 0))
5655 return 0;
5657 if (TREE_CODE (lr_arg) == INTEGER_CST
5658 && TREE_CODE (rr_arg) == INTEGER_CST)
5660 l_const = lr_arg, r_const = rr_arg;
5661 lr_reversep = ll_reversep;
5663 else if (lr_reversep != rr_reversep
5664 || lr_inner == 0 || rr_inner == 0
5665 || ! operand_equal_p (lr_inner, rr_inner, 0))
5666 return 0;
5667 else
5668 l_const = r_const = 0;
5670 /* If either comparison code is not correct for our logical operation,
5671 fail. However, we can convert a one-bit comparison against zero into
5672 the opposite comparison against that bit being set in the field. */
5674 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5675 if (lcode != wanted_code)
5677 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5679 /* Make the left operand unsigned, since we are only interested
5680 in the value of one bit. Otherwise we are doing the wrong
5681 thing below. */
5682 ll_unsignedp = 1;
5683 l_const = ll_mask;
5685 else
5686 return 0;
5689 /* This is analogous to the code for l_const above. */
5690 if (rcode != wanted_code)
5692 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5694 rl_unsignedp = 1;
5695 r_const = rl_mask;
5697 else
5698 return 0;
5701 /* See if we can find a mode that contains both fields being compared on
5702 the left. If we can't, fail. Otherwise, update all constants and masks
5703 to be relative to a field of that size. */
5704 first_bit = MIN (ll_bitpos, rl_bitpos);
5705 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5706 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5707 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5708 volatilep);
5709 if (lnmode == VOIDmode)
5710 return 0;
5712 lnbitsize = GET_MODE_BITSIZE (lnmode);
5713 lnbitpos = first_bit & ~ (lnbitsize - 1);
5714 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5715 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5717 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5719 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5720 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5723 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5724 size_int (xll_bitpos));
5725 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5726 size_int (xrl_bitpos));
5728 if (l_const)
5730 l_const = fold_convert_loc (loc, lntype, l_const);
5731 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5732 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5733 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5734 fold_build1_loc (loc, BIT_NOT_EXPR,
5735 lntype, ll_mask))))
5737 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5739 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5742 if (r_const)
5744 r_const = fold_convert_loc (loc, lntype, r_const);
5745 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5746 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5747 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5748 fold_build1_loc (loc, BIT_NOT_EXPR,
5749 lntype, rl_mask))))
5751 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5753 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5757 /* If the right sides are not constant, do the same for it. Also,
5758 disallow this optimization if a size or signedness mismatch occurs
5759 between the left and right sides. */
5760 if (l_const == 0)
5762 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5763 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5764 /* Make sure the two fields on the right
5765 correspond to the left without being swapped. */
5766 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5767 return 0;
5769 first_bit = MIN (lr_bitpos, rr_bitpos);
5770 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5771 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5772 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5773 volatilep);
5774 if (rnmode == VOIDmode)
5775 return 0;
5777 rnbitsize = GET_MODE_BITSIZE (rnmode);
5778 rnbitpos = first_bit & ~ (rnbitsize - 1);
5779 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5780 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5782 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5784 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5785 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5788 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5789 rntype, lr_mask),
5790 size_int (xlr_bitpos));
5791 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5792 rntype, rr_mask),
5793 size_int (xrr_bitpos));
5795 /* Make a mask that corresponds to both fields being compared.
5796 Do this for both items being compared. If the operands are the
5797 same size and the bits being compared are in the same position
5798 then we can do this by masking both and comparing the masked
5799 results. */
5800 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5801 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5802 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5804 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5805 lntype, lnbitsize, lnbitpos,
5806 ll_unsignedp || rl_unsignedp, ll_reversep);
5807 if (! all_ones_mask_p (ll_mask, lnbitsize))
5808 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5810 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5811 rntype, rnbitsize, rnbitpos,
5812 lr_unsignedp || rr_unsignedp, lr_reversep);
5813 if (! all_ones_mask_p (lr_mask, rnbitsize))
5814 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5816 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5819 /* There is still another way we can do something: If both pairs of
5820 fields being compared are adjacent, we may be able to make a wider
5821 field containing them both.
5823 Note that we still must mask the lhs/rhs expressions. Furthermore,
5824 the mask must be shifted to account for the shift done by
5825 make_bit_field_ref. */
5826 if ((ll_bitsize + ll_bitpos == rl_bitpos
5827 && lr_bitsize + lr_bitpos == rr_bitpos)
5828 || (ll_bitpos == rl_bitpos + rl_bitsize
5829 && lr_bitpos == rr_bitpos + rr_bitsize))
5831 tree type;
5833 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5834 ll_bitsize + rl_bitsize,
5835 MIN (ll_bitpos, rl_bitpos),
5836 ll_unsignedp, ll_reversep);
5837 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5838 lr_bitsize + rr_bitsize,
5839 MIN (lr_bitpos, rr_bitpos),
5840 lr_unsignedp, lr_reversep);
5842 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5843 size_int (MIN (xll_bitpos, xrl_bitpos)));
5844 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5845 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5847 /* Convert to the smaller type before masking out unwanted bits. */
5848 type = lntype;
5849 if (lntype != rntype)
5851 if (lnbitsize > rnbitsize)
5853 lhs = fold_convert_loc (loc, rntype, lhs);
5854 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5855 type = rntype;
5857 else if (lnbitsize < rnbitsize)
5859 rhs = fold_convert_loc (loc, lntype, rhs);
5860 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5861 type = lntype;
5865 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5866 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5868 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5869 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5871 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5874 return 0;
5877 /* Handle the case of comparisons with constants. If there is something in
5878 common between the masks, those bits of the constants must be the same.
5879 If not, the condition is always false. Test for this to avoid generating
5880 incorrect code below. */
5881 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5882 if (! integer_zerop (result)
5883 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5884 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5886 if (wanted_code == NE_EXPR)
5888 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5889 return constant_boolean_node (true, truth_type);
5891 else
5893 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5894 return constant_boolean_node (false, truth_type);
5898 /* Construct the expression we will return. First get the component
5899 reference we will make. Unless the mask is all ones the width of
5900 that field, perform the mask operation. Then compare with the
5901 merged constant. */
5902 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5903 lntype, lnbitsize, lnbitpos,
5904 ll_unsignedp || rl_unsignedp, ll_reversep);
5906 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5907 if (! all_ones_mask_p (ll_mask, lnbitsize))
5908 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5910 return build2_loc (loc, wanted_code, truth_type, result,
5911 const_binop (BIT_IOR_EXPR, l_const, r_const));
5914 /* T is an integer expression that is being multiplied, divided, or taken a
5915 modulus (CODE says which and what kind of divide or modulus) by a
5916 constant C. See if we can eliminate that operation by folding it with
5917 other operations already in T. WIDE_TYPE, if non-null, is a type that
5918 should be used for the computation if wider than our type.
5920 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5921 (X * 2) + (Y * 4). We must, however, be assured that either the original
5922 expression would not overflow or that overflow is undefined for the type
5923 in the language in question.
5925 If we return a non-null expression, it is an equivalent form of the
5926 original computation, but need not be in the original type.
5928 We set *STRICT_OVERFLOW_P to true if the return values depends on
5929 signed overflow being undefined. Otherwise we do not change
5930 *STRICT_OVERFLOW_P. */
5932 static tree
5933 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5934 bool *strict_overflow_p)
5936 /* To avoid exponential search depth, refuse to allow recursion past
5937 three levels. Beyond that (1) it's highly unlikely that we'll find
5938 something interesting and (2) we've probably processed it before
5939 when we built the inner expression. */
5941 static int depth;
5942 tree ret;
5944 if (depth > 3)
5945 return NULL;
5947 depth++;
5948 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5949 depth--;
5951 return ret;
5954 static tree
5955 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5956 bool *strict_overflow_p)
5958 tree type = TREE_TYPE (t);
5959 enum tree_code tcode = TREE_CODE (t);
5960 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5961 > GET_MODE_SIZE (TYPE_MODE (type)))
5962 ? wide_type : type);
5963 tree t1, t2;
5964 int same_p = tcode == code;
5965 tree op0 = NULL_TREE, op1 = NULL_TREE;
5966 bool sub_strict_overflow_p;
5968 /* Don't deal with constants of zero here; they confuse the code below. */
5969 if (integer_zerop (c))
5970 return NULL_TREE;
5972 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5973 op0 = TREE_OPERAND (t, 0);
5975 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5976 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5978 /* Note that we need not handle conditional operations here since fold
5979 already handles those cases. So just do arithmetic here. */
5980 switch (tcode)
5982 case INTEGER_CST:
5983 /* For a constant, we can always simplify if we are a multiply
5984 or (for divide and modulus) if it is a multiple of our constant. */
5985 if (code == MULT_EXPR
5986 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5988 tree tem = const_binop (code, fold_convert (ctype, t),
5989 fold_convert (ctype, c));
5990 /* If the multiplication overflowed, we lost information on it.
5991 See PR68142 and PR69845. */
5992 if (TREE_OVERFLOW (tem))
5993 return NULL_TREE;
5994 return tem;
5996 break;
5998 CASE_CONVERT: case NON_LVALUE_EXPR:
5999 /* If op0 is an expression ... */
6000 if ((COMPARISON_CLASS_P (op0)
6001 || UNARY_CLASS_P (op0)
6002 || BINARY_CLASS_P (op0)
6003 || VL_EXP_CLASS_P (op0)
6004 || EXPRESSION_CLASS_P (op0))
6005 /* ... and has wrapping overflow, and its type is smaller
6006 than ctype, then we cannot pass through as widening. */
6007 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6008 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6009 && (TYPE_PRECISION (ctype)
6010 > TYPE_PRECISION (TREE_TYPE (op0))))
6011 /* ... or this is a truncation (t is narrower than op0),
6012 then we cannot pass through this narrowing. */
6013 || (TYPE_PRECISION (type)
6014 < TYPE_PRECISION (TREE_TYPE (op0)))
6015 /* ... or signedness changes for division or modulus,
6016 then we cannot pass through this conversion. */
6017 || (code != MULT_EXPR
6018 && (TYPE_UNSIGNED (ctype)
6019 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6020 /* ... or has undefined overflow while the converted to
6021 type has not, we cannot do the operation in the inner type
6022 as that would introduce undefined overflow. */
6023 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6024 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6025 && !TYPE_OVERFLOW_UNDEFINED (type))))
6026 break;
6028 /* Pass the constant down and see if we can make a simplification. If
6029 we can, replace this expression with the inner simplification for
6030 possible later conversion to our or some other type. */
6031 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6032 && TREE_CODE (t2) == INTEGER_CST
6033 && !TREE_OVERFLOW (t2)
6034 && (0 != (t1 = extract_muldiv (op0, t2, code,
6035 code == MULT_EXPR
6036 ? ctype : NULL_TREE,
6037 strict_overflow_p))))
6038 return t1;
6039 break;
6041 case ABS_EXPR:
6042 /* If widening the type changes it from signed to unsigned, then we
6043 must avoid building ABS_EXPR itself as unsigned. */
6044 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6046 tree cstype = (*signed_type_for) (ctype);
6047 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6048 != 0)
6050 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6051 return fold_convert (ctype, t1);
6053 break;
6055 /* If the constant is negative, we cannot simplify this. */
6056 if (tree_int_cst_sgn (c) == -1)
6057 break;
6058 /* FALLTHROUGH */
6059 case NEGATE_EXPR:
6060 /* For division and modulus, type can't be unsigned, as e.g.
6061 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6062 For signed types, even with wrapping overflow, this is fine. */
6063 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6064 break;
6065 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6066 != 0)
6067 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6068 break;
6070 case MIN_EXPR: case MAX_EXPR:
6071 /* If widening the type changes the signedness, then we can't perform
6072 this optimization as that changes the result. */
6073 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6074 break;
6076 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6077 sub_strict_overflow_p = false;
6078 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6079 &sub_strict_overflow_p)) != 0
6080 && (t2 = extract_muldiv (op1, c, code, wide_type,
6081 &sub_strict_overflow_p)) != 0)
6083 if (tree_int_cst_sgn (c) < 0)
6084 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6085 if (sub_strict_overflow_p)
6086 *strict_overflow_p = true;
6087 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6088 fold_convert (ctype, t2));
6090 break;
6092 case LSHIFT_EXPR: case RSHIFT_EXPR:
6093 /* If the second operand is constant, this is a multiplication
6094 or floor division, by a power of two, so we can treat it that
6095 way unless the multiplier or divisor overflows. Signed
6096 left-shift overflow is implementation-defined rather than
6097 undefined in C90, so do not convert signed left shift into
6098 multiplication. */
6099 if (TREE_CODE (op1) == INTEGER_CST
6100 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6101 /* const_binop may not detect overflow correctly,
6102 so check for it explicitly here. */
6103 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6104 && 0 != (t1 = fold_convert (ctype,
6105 const_binop (LSHIFT_EXPR,
6106 size_one_node,
6107 op1)))
6108 && !TREE_OVERFLOW (t1))
6109 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6110 ? MULT_EXPR : FLOOR_DIV_EXPR,
6111 ctype,
6112 fold_convert (ctype, op0),
6113 t1),
6114 c, code, wide_type, strict_overflow_p);
6115 break;
6117 case PLUS_EXPR: case MINUS_EXPR:
6118 /* See if we can eliminate the operation on both sides. If we can, we
6119 can return a new PLUS or MINUS. If we can't, the only remaining
6120 cases where we can do anything are if the second operand is a
6121 constant. */
6122 sub_strict_overflow_p = false;
6123 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6124 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6125 if (t1 != 0 && t2 != 0
6126 && (code == MULT_EXPR
6127 /* If not multiplication, we can only do this if both operands
6128 are divisible by c. */
6129 || (multiple_of_p (ctype, op0, c)
6130 && multiple_of_p (ctype, op1, c))))
6132 if (sub_strict_overflow_p)
6133 *strict_overflow_p = true;
6134 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6135 fold_convert (ctype, t2));
6138 /* If this was a subtraction, negate OP1 and set it to be an addition.
6139 This simplifies the logic below. */
6140 if (tcode == MINUS_EXPR)
6142 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6143 /* If OP1 was not easily negatable, the constant may be OP0. */
6144 if (TREE_CODE (op0) == INTEGER_CST)
6146 std::swap (op0, op1);
6147 std::swap (t1, t2);
6151 if (TREE_CODE (op1) != INTEGER_CST)
6152 break;
6154 /* If either OP1 or C are negative, this optimization is not safe for
6155 some of the division and remainder types while for others we need
6156 to change the code. */
6157 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6159 if (code == CEIL_DIV_EXPR)
6160 code = FLOOR_DIV_EXPR;
6161 else if (code == FLOOR_DIV_EXPR)
6162 code = CEIL_DIV_EXPR;
6163 else if (code != MULT_EXPR
6164 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6165 break;
6168 /* If it's a multiply or a division/modulus operation of a multiple
6169 of our constant, do the operation and verify it doesn't overflow. */
6170 if (code == MULT_EXPR
6171 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6173 op1 = const_binop (code, fold_convert (ctype, op1),
6174 fold_convert (ctype, c));
6175 /* We allow the constant to overflow with wrapping semantics. */
6176 if (op1 == 0
6177 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6178 break;
6180 else
6181 break;
6183 /* If we have an unsigned type, we cannot widen the operation since it
6184 will change the result if the original computation overflowed. */
6185 if (TYPE_UNSIGNED (ctype) && ctype != type)
6186 break;
6188 /* If we were able to eliminate our operation from the first side,
6189 apply our operation to the second side and reform the PLUS. */
6190 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6191 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6193 /* The last case is if we are a multiply. In that case, we can
6194 apply the distributive law to commute the multiply and addition
6195 if the multiplication of the constants doesn't overflow
6196 and overflow is defined. With undefined overflow
6197 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6198 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6199 return fold_build2 (tcode, ctype,
6200 fold_build2 (code, ctype,
6201 fold_convert (ctype, op0),
6202 fold_convert (ctype, c)),
6203 op1);
6205 break;
6207 case MULT_EXPR:
6208 /* We have a special case here if we are doing something like
6209 (C * 8) % 4 since we know that's zero. */
6210 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6211 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6212 /* If the multiplication can overflow we cannot optimize this. */
6213 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6214 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6215 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6217 *strict_overflow_p = true;
6218 return omit_one_operand (type, integer_zero_node, op0);
6221 /* ... fall through ... */
6223 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6224 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6225 /* If we can extract our operation from the LHS, do so and return a
6226 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6227 do something only if the second operand is a constant. */
6228 if (same_p
6229 && (t1 = extract_muldiv (op0, c, code, wide_type,
6230 strict_overflow_p)) != 0)
6231 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6232 fold_convert (ctype, op1));
6233 else if (tcode == MULT_EXPR && code == MULT_EXPR
6234 && (t1 = extract_muldiv (op1, c, code, wide_type,
6235 strict_overflow_p)) != 0)
6236 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6237 fold_convert (ctype, t1));
6238 else if (TREE_CODE (op1) != INTEGER_CST)
6239 return 0;
6241 /* If these are the same operation types, we can associate them
6242 assuming no overflow. */
6243 if (tcode == code)
6245 bool overflow_p = false;
6246 bool overflow_mul_p;
6247 signop sign = TYPE_SIGN (ctype);
6248 unsigned prec = TYPE_PRECISION (ctype);
6249 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6250 wi::to_wide (c, prec),
6251 sign, &overflow_mul_p);
6252 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6253 if (overflow_mul_p
6254 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6255 overflow_p = true;
6256 if (!overflow_p)
6257 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6258 wide_int_to_tree (ctype, mul));
6261 /* If these operations "cancel" each other, we have the main
6262 optimizations of this pass, which occur when either constant is a
6263 multiple of the other, in which case we replace this with either an
6264 operation or CODE or TCODE.
6266 If we have an unsigned type, we cannot do this since it will change
6267 the result if the original computation overflowed. */
6268 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6269 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6270 || (tcode == MULT_EXPR
6271 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6272 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6273 && code != MULT_EXPR)))
6275 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6277 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6278 *strict_overflow_p = true;
6279 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6280 fold_convert (ctype,
6281 const_binop (TRUNC_DIV_EXPR,
6282 op1, c)));
6284 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6286 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6287 *strict_overflow_p = true;
6288 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6289 fold_convert (ctype,
6290 const_binop (TRUNC_DIV_EXPR,
6291 c, op1)));
6294 break;
6296 default:
6297 break;
6300 return 0;
6303 /* Return a node which has the indicated constant VALUE (either 0 or
6304 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6305 and is of the indicated TYPE. */
6307 tree
6308 constant_boolean_node (bool value, tree type)
6310 if (type == integer_type_node)
6311 return value ? integer_one_node : integer_zero_node;
6312 else if (type == boolean_type_node)
6313 return value ? boolean_true_node : boolean_false_node;
6314 else if (TREE_CODE (type) == VECTOR_TYPE)
6315 return build_vector_from_val (type,
6316 build_int_cst (TREE_TYPE (type),
6317 value ? -1 : 0));
6318 else
6319 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6323 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6324 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6325 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6326 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6327 COND is the first argument to CODE; otherwise (as in the example
6328 given here), it is the second argument. TYPE is the type of the
6329 original expression. Return NULL_TREE if no simplification is
6330 possible. */
6332 static tree
6333 fold_binary_op_with_conditional_arg (location_t loc,
6334 enum tree_code code,
6335 tree type, tree op0, tree op1,
6336 tree cond, tree arg, int cond_first_p)
6338 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6339 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6340 tree test, true_value, false_value;
6341 tree lhs = NULL_TREE;
6342 tree rhs = NULL_TREE;
6343 enum tree_code cond_code = COND_EXPR;
6345 if (TREE_CODE (cond) == COND_EXPR
6346 || TREE_CODE (cond) == VEC_COND_EXPR)
6348 test = TREE_OPERAND (cond, 0);
6349 true_value = TREE_OPERAND (cond, 1);
6350 false_value = TREE_OPERAND (cond, 2);
6351 /* If this operand throws an expression, then it does not make
6352 sense to try to perform a logical or arithmetic operation
6353 involving it. */
6354 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6355 lhs = true_value;
6356 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6357 rhs = false_value;
6359 else if (!(TREE_CODE (type) != VECTOR_TYPE
6360 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6362 tree testtype = TREE_TYPE (cond);
6363 test = cond;
6364 true_value = constant_boolean_node (true, testtype);
6365 false_value = constant_boolean_node (false, testtype);
6367 else
6368 /* Detect the case of mixing vector and scalar types - bail out. */
6369 return NULL_TREE;
6371 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6372 cond_code = VEC_COND_EXPR;
6374 /* This transformation is only worthwhile if we don't have to wrap ARG
6375 in a SAVE_EXPR and the operation can be simplified without recursing
6376 on at least one of the branches once its pushed inside the COND_EXPR. */
6377 if (!TREE_CONSTANT (arg)
6378 && (TREE_SIDE_EFFECTS (arg)
6379 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6380 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6381 return NULL_TREE;
6383 arg = fold_convert_loc (loc, arg_type, arg);
6384 if (lhs == 0)
6386 true_value = fold_convert_loc (loc, cond_type, true_value);
6387 if (cond_first_p)
6388 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6389 else
6390 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6392 if (rhs == 0)
6394 false_value = fold_convert_loc (loc, cond_type, false_value);
6395 if (cond_first_p)
6396 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6397 else
6398 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6401 /* Check that we have simplified at least one of the branches. */
6402 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6403 return NULL_TREE;
6405 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6409 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6411 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6412 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6413 ADDEND is the same as X.
6415 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6416 and finite. The problematic cases are when X is zero, and its mode
6417 has signed zeros. In the case of rounding towards -infinity,
6418 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6419 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6421 bool
6422 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6424 if (!real_zerop (addend))
6425 return false;
6427 /* Don't allow the fold with -fsignaling-nans. */
6428 if (HONOR_SNANS (element_mode (type)))
6429 return false;
6431 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6432 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6433 return true;
6435 /* In a vector or complex, we would need to check the sign of all zeros. */
6436 if (TREE_CODE (addend) != REAL_CST)
6437 return false;
6439 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6440 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6441 negate = !negate;
6443 /* The mode has signed zeros, and we have to honor their sign.
6444 In this situation, there is only one case we can return true for.
6445 X - 0 is the same as X unless rounding towards -infinity is
6446 supported. */
6447 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6450 /* Subroutine of fold() that optimizes comparisons of a division by
6451 a nonzero integer constant against an integer constant, i.e.
6452 X/C1 op C2.
6454 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6455 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6456 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6458 The function returns the constant folded tree if a simplification
6459 can be made, and NULL_TREE otherwise. */
6461 static tree
6462 fold_div_compare (location_t loc,
6463 enum tree_code code, tree type, tree arg0, tree arg1)
6465 tree prod, tmp, hi, lo;
6466 tree arg00 = TREE_OPERAND (arg0, 0);
6467 tree arg01 = TREE_OPERAND (arg0, 1);
6468 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6469 bool neg_overflow = false;
6470 bool overflow;
6472 /* We have to do this the hard way to detect unsigned overflow.
6473 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6474 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6475 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6476 neg_overflow = false;
6478 if (sign == UNSIGNED)
6480 tmp = int_const_binop (MINUS_EXPR, arg01,
6481 build_int_cst (TREE_TYPE (arg01), 1));
6482 lo = prod;
6484 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6485 val = wi::add (prod, tmp, sign, &overflow);
6486 hi = force_fit_type (TREE_TYPE (arg00), val,
6487 -1, overflow | TREE_OVERFLOW (prod));
6489 else if (tree_int_cst_sgn (arg01) >= 0)
6491 tmp = int_const_binop (MINUS_EXPR, arg01,
6492 build_int_cst (TREE_TYPE (arg01), 1));
6493 switch (tree_int_cst_sgn (arg1))
6495 case -1:
6496 neg_overflow = true;
6497 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6498 hi = prod;
6499 break;
6501 case 0:
6502 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6503 hi = tmp;
6504 break;
6506 case 1:
6507 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6508 lo = prod;
6509 break;
6511 default:
6512 gcc_unreachable ();
6515 else
6517 /* A negative divisor reverses the relational operators. */
6518 code = swap_tree_comparison (code);
6520 tmp = int_const_binop (PLUS_EXPR, arg01,
6521 build_int_cst (TREE_TYPE (arg01), 1));
6522 switch (tree_int_cst_sgn (arg1))
6524 case -1:
6525 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6526 lo = prod;
6527 break;
6529 case 0:
6530 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6531 lo = tmp;
6532 break;
6534 case 1:
6535 neg_overflow = true;
6536 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6537 hi = prod;
6538 break;
6540 default:
6541 gcc_unreachable ();
6545 switch (code)
6547 case EQ_EXPR:
6548 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6549 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6550 if (TREE_OVERFLOW (hi))
6551 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6552 if (TREE_OVERFLOW (lo))
6553 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6554 return build_range_check (loc, type, arg00, 1, lo, hi);
6556 case NE_EXPR:
6557 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6558 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6559 if (TREE_OVERFLOW (hi))
6560 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6561 if (TREE_OVERFLOW (lo))
6562 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6563 return build_range_check (loc, type, arg00, 0, lo, hi);
6565 case LT_EXPR:
6566 if (TREE_OVERFLOW (lo))
6568 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6569 return omit_one_operand_loc (loc, type, tmp, arg00);
6571 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6573 case LE_EXPR:
6574 if (TREE_OVERFLOW (hi))
6576 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6577 return omit_one_operand_loc (loc, type, tmp, arg00);
6579 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6581 case GT_EXPR:
6582 if (TREE_OVERFLOW (hi))
6584 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6585 return omit_one_operand_loc (loc, type, tmp, arg00);
6587 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6589 case GE_EXPR:
6590 if (TREE_OVERFLOW (lo))
6592 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6593 return omit_one_operand_loc (loc, type, tmp, arg00);
6595 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6597 default:
6598 break;
6601 return NULL_TREE;
6605 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6606 equality/inequality test, then return a simplified form of the test
6607 using a sign testing. Otherwise return NULL. TYPE is the desired
6608 result type. */
6610 static tree
6611 fold_single_bit_test_into_sign_test (location_t loc,
6612 enum tree_code code, tree arg0, tree arg1,
6613 tree result_type)
6615 /* If this is testing a single bit, we can optimize the test. */
6616 if ((code == NE_EXPR || code == EQ_EXPR)
6617 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6618 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6620 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6621 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6622 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6624 if (arg00 != NULL_TREE
6625 /* This is only a win if casting to a signed type is cheap,
6626 i.e. when arg00's type is not a partial mode. */
6627 && TYPE_PRECISION (TREE_TYPE (arg00))
6628 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6630 tree stype = signed_type_for (TREE_TYPE (arg00));
6631 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6632 result_type,
6633 fold_convert_loc (loc, stype, arg00),
6634 build_int_cst (stype, 0));
6638 return NULL_TREE;
6641 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6642 equality/inequality test, then return a simplified form of
6643 the test using shifts and logical operations. Otherwise return
6644 NULL. TYPE is the desired result type. */
6646 tree
6647 fold_single_bit_test (location_t loc, enum tree_code code,
6648 tree arg0, tree arg1, tree result_type)
6650 /* If this is testing a single bit, we can optimize the test. */
6651 if ((code == NE_EXPR || code == EQ_EXPR)
6652 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6653 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6655 tree inner = TREE_OPERAND (arg0, 0);
6656 tree type = TREE_TYPE (arg0);
6657 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6658 machine_mode operand_mode = TYPE_MODE (type);
6659 int ops_unsigned;
6660 tree signed_type, unsigned_type, intermediate_type;
6661 tree tem, one;
6663 /* First, see if we can fold the single bit test into a sign-bit
6664 test. */
6665 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6666 result_type);
6667 if (tem)
6668 return tem;
6670 /* Otherwise we have (A & C) != 0 where C is a single bit,
6671 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6672 Similarly for (A & C) == 0. */
6674 /* If INNER is a right shift of a constant and it plus BITNUM does
6675 not overflow, adjust BITNUM and INNER. */
6676 if (TREE_CODE (inner) == RSHIFT_EXPR
6677 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6678 && bitnum < TYPE_PRECISION (type)
6679 && wi::ltu_p (TREE_OPERAND (inner, 1),
6680 TYPE_PRECISION (type) - bitnum))
6682 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6683 inner = TREE_OPERAND (inner, 0);
6686 /* If we are going to be able to omit the AND below, we must do our
6687 operations as unsigned. If we must use the AND, we have a choice.
6688 Normally unsigned is faster, but for some machines signed is. */
6689 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6690 && !flag_syntax_only) ? 0 : 1;
6692 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6693 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6694 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6695 inner = fold_convert_loc (loc, intermediate_type, inner);
6697 if (bitnum != 0)
6698 inner = build2 (RSHIFT_EXPR, intermediate_type,
6699 inner, size_int (bitnum));
6701 one = build_int_cst (intermediate_type, 1);
6703 if (code == EQ_EXPR)
6704 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6706 /* Put the AND last so it can combine with more things. */
6707 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6709 /* Make sure to return the proper type. */
6710 inner = fold_convert_loc (loc, result_type, inner);
6712 return inner;
6714 return NULL_TREE;
6717 /* Test whether it is preferable two swap two operands, ARG0 and
6718 ARG1, for example because ARG0 is an integer constant and ARG1
6719 isn't. */
6721 bool
6722 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6724 if (CONSTANT_CLASS_P (arg1))
6725 return 0;
6726 if (CONSTANT_CLASS_P (arg0))
6727 return 1;
6729 STRIP_NOPS (arg0);
6730 STRIP_NOPS (arg1);
6732 if (TREE_CONSTANT (arg1))
6733 return 0;
6734 if (TREE_CONSTANT (arg0))
6735 return 1;
6737 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6738 for commutative and comparison operators. Ensuring a canonical
6739 form allows the optimizers to find additional redundancies without
6740 having to explicitly check for both orderings. */
6741 if (TREE_CODE (arg0) == SSA_NAME
6742 && TREE_CODE (arg1) == SSA_NAME
6743 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6744 return 1;
6746 /* Put SSA_NAMEs last. */
6747 if (TREE_CODE (arg1) == SSA_NAME)
6748 return 0;
6749 if (TREE_CODE (arg0) == SSA_NAME)
6750 return 1;
6752 /* Put variables last. */
6753 if (DECL_P (arg1))
6754 return 0;
6755 if (DECL_P (arg0))
6756 return 1;
6758 return 0;
6762 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6763 means A >= Y && A != MAX, but in this case we know that
6764 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6766 static tree
6767 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6769 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6771 if (TREE_CODE (bound) == LT_EXPR)
6772 a = TREE_OPERAND (bound, 0);
6773 else if (TREE_CODE (bound) == GT_EXPR)
6774 a = TREE_OPERAND (bound, 1);
6775 else
6776 return NULL_TREE;
6778 typea = TREE_TYPE (a);
6779 if (!INTEGRAL_TYPE_P (typea)
6780 && !POINTER_TYPE_P (typea))
6781 return NULL_TREE;
6783 if (TREE_CODE (ineq) == LT_EXPR)
6785 a1 = TREE_OPERAND (ineq, 1);
6786 y = TREE_OPERAND (ineq, 0);
6788 else if (TREE_CODE (ineq) == GT_EXPR)
6790 a1 = TREE_OPERAND (ineq, 0);
6791 y = TREE_OPERAND (ineq, 1);
6793 else
6794 return NULL_TREE;
6796 if (TREE_TYPE (a1) != typea)
6797 return NULL_TREE;
6799 if (POINTER_TYPE_P (typea))
6801 /* Convert the pointer types into integer before taking the difference. */
6802 tree ta = fold_convert_loc (loc, ssizetype, a);
6803 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6804 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6806 else
6807 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6809 if (!diff || !integer_onep (diff))
6810 return NULL_TREE;
6812 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6815 /* Fold a sum or difference of at least one multiplication.
6816 Returns the folded tree or NULL if no simplification could be made. */
6818 static tree
6819 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6820 tree arg0, tree arg1)
6822 tree arg00, arg01, arg10, arg11;
6823 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6825 /* (A * C) +- (B * C) -> (A+-B) * C.
6826 (A * C) +- A -> A * (C+-1).
6827 We are most concerned about the case where C is a constant,
6828 but other combinations show up during loop reduction. Since
6829 it is not difficult, try all four possibilities. */
6831 if (TREE_CODE (arg0) == MULT_EXPR)
6833 arg00 = TREE_OPERAND (arg0, 0);
6834 arg01 = TREE_OPERAND (arg0, 1);
6836 else if (TREE_CODE (arg0) == INTEGER_CST)
6838 arg00 = build_one_cst (type);
6839 arg01 = arg0;
6841 else
6843 /* We cannot generate constant 1 for fract. */
6844 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6845 return NULL_TREE;
6846 arg00 = arg0;
6847 arg01 = build_one_cst (type);
6849 if (TREE_CODE (arg1) == MULT_EXPR)
6851 arg10 = TREE_OPERAND (arg1, 0);
6852 arg11 = TREE_OPERAND (arg1, 1);
6854 else if (TREE_CODE (arg1) == INTEGER_CST)
6856 arg10 = build_one_cst (type);
6857 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6858 the purpose of this canonicalization. */
6859 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6860 && negate_expr_p (arg1)
6861 && code == PLUS_EXPR)
6863 arg11 = negate_expr (arg1);
6864 code = MINUS_EXPR;
6866 else
6867 arg11 = arg1;
6869 else
6871 /* We cannot generate constant 1 for fract. */
6872 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6873 return NULL_TREE;
6874 arg10 = arg1;
6875 arg11 = build_one_cst (type);
6877 same = NULL_TREE;
6879 if (operand_equal_p (arg01, arg11, 0))
6880 same = arg01, alt0 = arg00, alt1 = arg10;
6881 else if (operand_equal_p (arg00, arg10, 0))
6882 same = arg00, alt0 = arg01, alt1 = arg11;
6883 else if (operand_equal_p (arg00, arg11, 0))
6884 same = arg00, alt0 = arg01, alt1 = arg10;
6885 else if (operand_equal_p (arg01, arg10, 0))
6886 same = arg01, alt0 = arg00, alt1 = arg11;
6888 /* No identical multiplicands; see if we can find a common
6889 power-of-two factor in non-power-of-two multiplies. This
6890 can help in multi-dimensional array access. */
6891 else if (tree_fits_shwi_p (arg01)
6892 && tree_fits_shwi_p (arg11))
6894 HOST_WIDE_INT int01, int11, tmp;
6895 bool swap = false;
6896 tree maybe_same;
6897 int01 = tree_to_shwi (arg01);
6898 int11 = tree_to_shwi (arg11);
6900 /* Move min of absolute values to int11. */
6901 if (absu_hwi (int01) < absu_hwi (int11))
6903 tmp = int01, int01 = int11, int11 = tmp;
6904 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6905 maybe_same = arg01;
6906 swap = true;
6908 else
6909 maybe_same = arg11;
6911 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6912 /* The remainder should not be a constant, otherwise we
6913 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6914 increased the number of multiplications necessary. */
6915 && TREE_CODE (arg10) != INTEGER_CST)
6917 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6918 build_int_cst (TREE_TYPE (arg00),
6919 int01 / int11));
6920 alt1 = arg10;
6921 same = maybe_same;
6922 if (swap)
6923 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6927 if (same)
6928 return fold_build2_loc (loc, MULT_EXPR, type,
6929 fold_build2_loc (loc, code, type,
6930 fold_convert_loc (loc, type, alt0),
6931 fold_convert_loc (loc, type, alt1)),
6932 fold_convert_loc (loc, type, same));
6934 return NULL_TREE;
6937 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6938 specified by EXPR into the buffer PTR of length LEN bytes.
6939 Return the number of bytes placed in the buffer, or zero
6940 upon failure. */
6942 static int
6943 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6945 tree type = TREE_TYPE (expr);
6946 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6947 int byte, offset, word, words;
6948 unsigned char value;
6950 if ((off == -1 && total_bytes > len)
6951 || off >= total_bytes)
6952 return 0;
6953 if (off == -1)
6954 off = 0;
6955 words = total_bytes / UNITS_PER_WORD;
6957 for (byte = 0; byte < total_bytes; byte++)
6959 int bitpos = byte * BITS_PER_UNIT;
6960 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6961 number of bytes. */
6962 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
6964 if (total_bytes > UNITS_PER_WORD)
6966 word = byte / UNITS_PER_WORD;
6967 if (WORDS_BIG_ENDIAN)
6968 word = (words - 1) - word;
6969 offset = word * UNITS_PER_WORD;
6970 if (BYTES_BIG_ENDIAN)
6971 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6972 else
6973 offset += byte % UNITS_PER_WORD;
6975 else
6976 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
6977 if (offset >= off
6978 && offset - off < len)
6979 ptr[offset - off] = value;
6981 return MIN (len, total_bytes - off);
6985 /* Subroutine of native_encode_expr. Encode the FIXED_CST
6986 specified by EXPR into the buffer PTR of length LEN bytes.
6987 Return the number of bytes placed in the buffer, or zero
6988 upon failure. */
6990 static int
6991 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
6993 tree type = TREE_TYPE (expr);
6994 machine_mode mode = TYPE_MODE (type);
6995 int total_bytes = GET_MODE_SIZE (mode);
6996 FIXED_VALUE_TYPE value;
6997 tree i_value, i_type;
6999 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7000 return 0;
7002 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7004 if (NULL_TREE == i_type
7005 || TYPE_PRECISION (i_type) != total_bytes)
7006 return 0;
7008 value = TREE_FIXED_CST (expr);
7009 i_value = double_int_to_tree (i_type, value.data);
7011 return native_encode_int (i_value, ptr, len, off);
7015 /* Subroutine of native_encode_expr. Encode the REAL_CST
7016 specified by EXPR into the buffer PTR of length LEN bytes.
7017 Return the number of bytes placed in the buffer, or zero
7018 upon failure. */
7020 static int
7021 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7023 tree type = TREE_TYPE (expr);
7024 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7025 int byte, offset, word, words, bitpos;
7026 unsigned char value;
7028 /* There are always 32 bits in each long, no matter the size of
7029 the hosts long. We handle floating point representations with
7030 up to 192 bits. */
7031 long tmp[6];
7033 if ((off == -1 && total_bytes > len)
7034 || off >= total_bytes)
7035 return 0;
7036 if (off == -1)
7037 off = 0;
7038 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7040 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7042 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7043 bitpos += BITS_PER_UNIT)
7045 byte = (bitpos / BITS_PER_UNIT) & 3;
7046 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7048 if (UNITS_PER_WORD < 4)
7050 word = byte / UNITS_PER_WORD;
7051 if (WORDS_BIG_ENDIAN)
7052 word = (words - 1) - word;
7053 offset = word * UNITS_PER_WORD;
7054 if (BYTES_BIG_ENDIAN)
7055 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7056 else
7057 offset += byte % UNITS_PER_WORD;
7059 else
7061 offset = byte;
7062 if (BYTES_BIG_ENDIAN)
7064 /* Reverse bytes within each long, or within the entire float
7065 if it's smaller than a long (for HFmode). */
7066 offset = MIN (3, total_bytes - 1) - offset;
7067 gcc_assert (offset >= 0);
7070 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7071 if (offset >= off
7072 && offset - off < len)
7073 ptr[offset - off] = value;
7075 return MIN (len, total_bytes - off);
7078 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7079 specified by EXPR into the buffer PTR of length LEN bytes.
7080 Return the number of bytes placed in the buffer, or zero
7081 upon failure. */
7083 static int
7084 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7086 int rsize, isize;
7087 tree part;
7089 part = TREE_REALPART (expr);
7090 rsize = native_encode_expr (part, ptr, len, off);
7091 if (off == -1
7092 && rsize == 0)
7093 return 0;
7094 part = TREE_IMAGPART (expr);
7095 if (off != -1)
7096 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7097 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7098 if (off == -1
7099 && isize != rsize)
7100 return 0;
7101 return rsize + isize;
7105 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7106 specified by EXPR into the buffer PTR of length LEN bytes.
7107 Return the number of bytes placed in the buffer, or zero
7108 upon failure. */
7110 static int
7111 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7113 unsigned i, count;
7114 int size, offset;
7115 tree itype, elem;
7117 offset = 0;
7118 count = VECTOR_CST_NELTS (expr);
7119 itype = TREE_TYPE (TREE_TYPE (expr));
7120 size = GET_MODE_SIZE (TYPE_MODE (itype));
7121 for (i = 0; i < count; i++)
7123 if (off >= size)
7125 off -= size;
7126 continue;
7128 elem = VECTOR_CST_ELT (expr, i);
7129 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7130 if ((off == -1 && res != size)
7131 || res == 0)
7132 return 0;
7133 offset += res;
7134 if (offset >= len)
7135 return offset;
7136 if (off != -1)
7137 off = 0;
7139 return offset;
7143 /* Subroutine of native_encode_expr. Encode the STRING_CST
7144 specified by EXPR into the buffer PTR of length LEN bytes.
7145 Return the number of bytes placed in the buffer, or zero
7146 upon failure. */
7148 static int
7149 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7151 tree type = TREE_TYPE (expr);
7152 HOST_WIDE_INT total_bytes;
7154 if (TREE_CODE (type) != ARRAY_TYPE
7155 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7156 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7157 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7158 return 0;
7159 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7160 if ((off == -1 && total_bytes > len)
7161 || off >= total_bytes)
7162 return 0;
7163 if (off == -1)
7164 off = 0;
7165 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7167 int written = 0;
7168 if (off < TREE_STRING_LENGTH (expr))
7170 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7171 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7173 memset (ptr + written, 0,
7174 MIN (total_bytes - written, len - written));
7176 else
7177 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7178 return MIN (total_bytes - off, len);
7182 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7183 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7184 buffer PTR of length LEN bytes. If OFF is not -1 then start
7185 the encoding at byte offset OFF and encode at most LEN bytes.
7186 Return the number of bytes placed in the buffer, or zero upon failure. */
7189 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7191 /* We don't support starting at negative offset and -1 is special. */
7192 if (off < -1)
7193 return 0;
7195 switch (TREE_CODE (expr))
7197 case INTEGER_CST:
7198 return native_encode_int (expr, ptr, len, off);
7200 case REAL_CST:
7201 return native_encode_real (expr, ptr, len, off);
7203 case FIXED_CST:
7204 return native_encode_fixed (expr, ptr, len, off);
7206 case COMPLEX_CST:
7207 return native_encode_complex (expr, ptr, len, off);
7209 case VECTOR_CST:
7210 return native_encode_vector (expr, ptr, len, off);
7212 case STRING_CST:
7213 return native_encode_string (expr, ptr, len, off);
7215 default:
7216 return 0;
7221 /* Subroutine of native_interpret_expr. Interpret the contents of
7222 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7223 If the buffer cannot be interpreted, return NULL_TREE. */
7225 static tree
7226 native_interpret_int (tree type, const unsigned char *ptr, int len)
7228 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7230 if (total_bytes > len
7231 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7232 return NULL_TREE;
7234 wide_int result = wi::from_buffer (ptr, total_bytes);
7236 return wide_int_to_tree (type, result);
7240 /* Subroutine of native_interpret_expr. Interpret the contents of
7241 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7242 If the buffer cannot be interpreted, return NULL_TREE. */
7244 static tree
7245 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7247 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7248 double_int result;
7249 FIXED_VALUE_TYPE fixed_value;
7251 if (total_bytes > len
7252 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7253 return NULL_TREE;
7255 result = double_int::from_buffer (ptr, total_bytes);
7256 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7258 return build_fixed (type, fixed_value);
7262 /* Subroutine of native_interpret_expr. Interpret the contents of
7263 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7264 If the buffer cannot be interpreted, return NULL_TREE. */
7266 static tree
7267 native_interpret_real (tree type, const unsigned char *ptr, int len)
7269 machine_mode mode = TYPE_MODE (type);
7270 int total_bytes = GET_MODE_SIZE (mode);
7271 unsigned char value;
7272 /* There are always 32 bits in each long, no matter the size of
7273 the hosts long. We handle floating point representations with
7274 up to 192 bits. */
7275 REAL_VALUE_TYPE r;
7276 long tmp[6];
7278 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7279 if (total_bytes > len || total_bytes > 24)
7280 return NULL_TREE;
7281 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7283 memset (tmp, 0, sizeof (tmp));
7284 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7285 bitpos += BITS_PER_UNIT)
7287 /* Both OFFSET and BYTE index within a long;
7288 bitpos indexes the whole float. */
7289 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7290 if (UNITS_PER_WORD < 4)
7292 int word = byte / UNITS_PER_WORD;
7293 if (WORDS_BIG_ENDIAN)
7294 word = (words - 1) - word;
7295 offset = word * UNITS_PER_WORD;
7296 if (BYTES_BIG_ENDIAN)
7297 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7298 else
7299 offset += byte % UNITS_PER_WORD;
7301 else
7303 offset = byte;
7304 if (BYTES_BIG_ENDIAN)
7306 /* Reverse bytes within each long, or within the entire float
7307 if it's smaller than a long (for HFmode). */
7308 offset = MIN (3, total_bytes - 1) - offset;
7309 gcc_assert (offset >= 0);
7312 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7314 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7317 real_from_target (&r, tmp, mode);
7318 return build_real (type, r);
7322 /* Subroutine of native_interpret_expr. Interpret the contents of
7323 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7324 If the buffer cannot be interpreted, return NULL_TREE. */
7326 static tree
7327 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7329 tree etype, rpart, ipart;
7330 int size;
7332 etype = TREE_TYPE (type);
7333 size = GET_MODE_SIZE (TYPE_MODE (etype));
7334 if (size * 2 > len)
7335 return NULL_TREE;
7336 rpart = native_interpret_expr (etype, ptr, size);
7337 if (!rpart)
7338 return NULL_TREE;
7339 ipart = native_interpret_expr (etype, ptr+size, size);
7340 if (!ipart)
7341 return NULL_TREE;
7342 return build_complex (type, rpart, ipart);
7346 /* Subroutine of native_interpret_expr. Interpret the contents of
7347 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7348 If the buffer cannot be interpreted, return NULL_TREE. */
7350 static tree
7351 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7353 tree etype, elem;
7354 int i, size, count;
7355 tree *elements;
7357 etype = TREE_TYPE (type);
7358 size = GET_MODE_SIZE (TYPE_MODE (etype));
7359 count = TYPE_VECTOR_SUBPARTS (type);
7360 if (size * count > len)
7361 return NULL_TREE;
7363 elements = XALLOCAVEC (tree, count);
7364 for (i = count - 1; i >= 0; i--)
7366 elem = native_interpret_expr (etype, ptr+(i*size), size);
7367 if (!elem)
7368 return NULL_TREE;
7369 elements[i] = elem;
7371 return build_vector (type, elements);
7375 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7376 the buffer PTR of length LEN as a constant of type TYPE. For
7377 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7378 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7379 return NULL_TREE. */
7381 tree
7382 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7384 switch (TREE_CODE (type))
7386 case INTEGER_TYPE:
7387 case ENUMERAL_TYPE:
7388 case BOOLEAN_TYPE:
7389 case POINTER_TYPE:
7390 case REFERENCE_TYPE:
7391 return native_interpret_int (type, ptr, len);
7393 case REAL_TYPE:
7394 return native_interpret_real (type, ptr, len);
7396 case FIXED_POINT_TYPE:
7397 return native_interpret_fixed (type, ptr, len);
7399 case COMPLEX_TYPE:
7400 return native_interpret_complex (type, ptr, len);
7402 case VECTOR_TYPE:
7403 return native_interpret_vector (type, ptr, len);
7405 default:
7406 return NULL_TREE;
7410 /* Returns true if we can interpret the contents of a native encoding
7411 as TYPE. */
7413 static bool
7414 can_native_interpret_type_p (tree type)
7416 switch (TREE_CODE (type))
7418 case INTEGER_TYPE:
7419 case ENUMERAL_TYPE:
7420 case BOOLEAN_TYPE:
7421 case POINTER_TYPE:
7422 case REFERENCE_TYPE:
7423 case FIXED_POINT_TYPE:
7424 case REAL_TYPE:
7425 case COMPLEX_TYPE:
7426 case VECTOR_TYPE:
7427 return true;
7428 default:
7429 return false;
7433 /* Return true iff a constant of type TYPE is accepted by
7434 native_encode_expr. */
7436 bool
7437 can_native_encode_type_p (tree type)
7439 switch (TREE_CODE (type))
7441 case INTEGER_TYPE:
7442 case REAL_TYPE:
7443 case FIXED_POINT_TYPE:
7444 case COMPLEX_TYPE:
7445 case VECTOR_TYPE:
7446 case POINTER_TYPE:
7447 return true;
7448 default:
7449 return false;
7453 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7454 TYPE at compile-time. If we're unable to perform the conversion
7455 return NULL_TREE. */
7457 static tree
7458 fold_view_convert_expr (tree type, tree expr)
7460 /* We support up to 512-bit values (for V8DFmode). */
7461 unsigned char buffer[64];
7462 int len;
7464 /* Check that the host and target are sane. */
7465 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7466 return NULL_TREE;
7468 len = native_encode_expr (expr, buffer, sizeof (buffer));
7469 if (len == 0)
7470 return NULL_TREE;
7472 return native_interpret_expr (type, buffer, len);
7475 /* Build an expression for the address of T. Folds away INDIRECT_REF
7476 to avoid confusing the gimplify process. */
7478 tree
7479 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7481 /* The size of the object is not relevant when talking about its address. */
7482 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7483 t = TREE_OPERAND (t, 0);
7485 if (TREE_CODE (t) == INDIRECT_REF)
7487 t = TREE_OPERAND (t, 0);
7489 if (TREE_TYPE (t) != ptrtype)
7490 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7492 else if (TREE_CODE (t) == MEM_REF
7493 && integer_zerop (TREE_OPERAND (t, 1)))
7494 return TREE_OPERAND (t, 0);
7495 else if (TREE_CODE (t) == MEM_REF
7496 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7497 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7498 TREE_OPERAND (t, 0),
7499 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7500 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7502 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7504 if (TREE_TYPE (t) != ptrtype)
7505 t = fold_convert_loc (loc, ptrtype, t);
7507 else
7508 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7510 return t;
7513 /* Build an expression for the address of T. */
7515 tree
7516 build_fold_addr_expr_loc (location_t loc, tree t)
7518 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7520 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7523 /* Fold a unary expression of code CODE and type TYPE with operand
7524 OP0. Return the folded expression if folding is successful.
7525 Otherwise, return NULL_TREE. */
7527 tree
7528 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7530 tree tem;
7531 tree arg0;
7532 enum tree_code_class kind = TREE_CODE_CLASS (code);
7534 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7535 && TREE_CODE_LENGTH (code) == 1);
7537 arg0 = op0;
7538 if (arg0)
7540 if (CONVERT_EXPR_CODE_P (code)
7541 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7543 /* Don't use STRIP_NOPS, because signedness of argument type
7544 matters. */
7545 STRIP_SIGN_NOPS (arg0);
7547 else
7549 /* Strip any conversions that don't change the mode. This
7550 is safe for every expression, except for a comparison
7551 expression because its signedness is derived from its
7552 operands.
7554 Note that this is done as an internal manipulation within
7555 the constant folder, in order to find the simplest
7556 representation of the arguments so that their form can be
7557 studied. In any cases, the appropriate type conversions
7558 should be put back in the tree that will get out of the
7559 constant folder. */
7560 STRIP_NOPS (arg0);
7563 if (CONSTANT_CLASS_P (arg0))
7565 tree tem = const_unop (code, type, arg0);
7566 if (tem)
7568 if (TREE_TYPE (tem) != type)
7569 tem = fold_convert_loc (loc, type, tem);
7570 return tem;
7575 tem = generic_simplify (loc, code, type, op0);
7576 if (tem)
7577 return tem;
7579 if (TREE_CODE_CLASS (code) == tcc_unary)
7581 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7582 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7583 fold_build1_loc (loc, code, type,
7584 fold_convert_loc (loc, TREE_TYPE (op0),
7585 TREE_OPERAND (arg0, 1))));
7586 else if (TREE_CODE (arg0) == COND_EXPR)
7588 tree arg01 = TREE_OPERAND (arg0, 1);
7589 tree arg02 = TREE_OPERAND (arg0, 2);
7590 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7591 arg01 = fold_build1_loc (loc, code, type,
7592 fold_convert_loc (loc,
7593 TREE_TYPE (op0), arg01));
7594 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7595 arg02 = fold_build1_loc (loc, code, type,
7596 fold_convert_loc (loc,
7597 TREE_TYPE (op0), arg02));
7598 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7599 arg01, arg02);
7601 /* If this was a conversion, and all we did was to move into
7602 inside the COND_EXPR, bring it back out. But leave it if
7603 it is a conversion from integer to integer and the
7604 result precision is no wider than a word since such a
7605 conversion is cheap and may be optimized away by combine,
7606 while it couldn't if it were outside the COND_EXPR. Then return
7607 so we don't get into an infinite recursion loop taking the
7608 conversion out and then back in. */
7610 if ((CONVERT_EXPR_CODE_P (code)
7611 || code == NON_LVALUE_EXPR)
7612 && TREE_CODE (tem) == COND_EXPR
7613 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7614 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7615 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7616 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7617 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7618 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7619 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7620 && (INTEGRAL_TYPE_P
7621 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7622 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7623 || flag_syntax_only))
7624 tem = build1_loc (loc, code, type,
7625 build3 (COND_EXPR,
7626 TREE_TYPE (TREE_OPERAND
7627 (TREE_OPERAND (tem, 1), 0)),
7628 TREE_OPERAND (tem, 0),
7629 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7630 TREE_OPERAND (TREE_OPERAND (tem, 2),
7631 0)));
7632 return tem;
7636 switch (code)
7638 case NON_LVALUE_EXPR:
7639 if (!maybe_lvalue_p (op0))
7640 return fold_convert_loc (loc, type, op0);
7641 return NULL_TREE;
7643 CASE_CONVERT:
7644 case FLOAT_EXPR:
7645 case FIX_TRUNC_EXPR:
7646 if (COMPARISON_CLASS_P (op0))
7648 /* If we have (type) (a CMP b) and type is an integral type, return
7649 new expression involving the new type. Canonicalize
7650 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7651 non-integral type.
7652 Do not fold the result as that would not simplify further, also
7653 folding again results in recursions. */
7654 if (TREE_CODE (type) == BOOLEAN_TYPE)
7655 return build2_loc (loc, TREE_CODE (op0), type,
7656 TREE_OPERAND (op0, 0),
7657 TREE_OPERAND (op0, 1));
7658 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7659 && TREE_CODE (type) != VECTOR_TYPE)
7660 return build3_loc (loc, COND_EXPR, type, op0,
7661 constant_boolean_node (true, type),
7662 constant_boolean_node (false, type));
7665 /* Handle (T *)&A.B.C for A being of type T and B and C
7666 living at offset zero. This occurs frequently in
7667 C++ upcasting and then accessing the base. */
7668 if (TREE_CODE (op0) == ADDR_EXPR
7669 && POINTER_TYPE_P (type)
7670 && handled_component_p (TREE_OPERAND (op0, 0)))
7672 HOST_WIDE_INT bitsize, bitpos;
7673 tree offset;
7674 machine_mode mode;
7675 int unsignedp, reversep, volatilep;
7676 tree base
7677 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7678 &offset, &mode, &unsignedp, &reversep,
7679 &volatilep);
7680 /* If the reference was to a (constant) zero offset, we can use
7681 the address of the base if it has the same base type
7682 as the result type and the pointer type is unqualified. */
7683 if (! offset && bitpos == 0
7684 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7685 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7686 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7687 return fold_convert_loc (loc, type,
7688 build_fold_addr_expr_loc (loc, base));
7691 if (TREE_CODE (op0) == MODIFY_EXPR
7692 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7693 /* Detect assigning a bitfield. */
7694 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7695 && DECL_BIT_FIELD
7696 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7698 /* Don't leave an assignment inside a conversion
7699 unless assigning a bitfield. */
7700 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7701 /* First do the assignment, then return converted constant. */
7702 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7703 TREE_NO_WARNING (tem) = 1;
7704 TREE_USED (tem) = 1;
7705 return tem;
7708 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7709 constants (if x has signed type, the sign bit cannot be set
7710 in c). This folds extension into the BIT_AND_EXPR.
7711 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7712 very likely don't have maximal range for their precision and this
7713 transformation effectively doesn't preserve non-maximal ranges. */
7714 if (TREE_CODE (type) == INTEGER_TYPE
7715 && TREE_CODE (op0) == BIT_AND_EXPR
7716 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7718 tree and_expr = op0;
7719 tree and0 = TREE_OPERAND (and_expr, 0);
7720 tree and1 = TREE_OPERAND (and_expr, 1);
7721 int change = 0;
7723 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7724 || (TYPE_PRECISION (type)
7725 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7726 change = 1;
7727 else if (TYPE_PRECISION (TREE_TYPE (and1))
7728 <= HOST_BITS_PER_WIDE_INT
7729 && tree_fits_uhwi_p (and1))
7731 unsigned HOST_WIDE_INT cst;
7733 cst = tree_to_uhwi (and1);
7734 cst &= HOST_WIDE_INT_M1U
7735 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7736 change = (cst == 0);
7737 if (change
7738 && !flag_syntax_only
7739 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7740 == ZERO_EXTEND))
7742 tree uns = unsigned_type_for (TREE_TYPE (and0));
7743 and0 = fold_convert_loc (loc, uns, and0);
7744 and1 = fold_convert_loc (loc, uns, and1);
7747 if (change)
7749 tem = force_fit_type (type, wi::to_widest (and1), 0,
7750 TREE_OVERFLOW (and1));
7751 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7752 fold_convert_loc (loc, type, and0), tem);
7756 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7757 cast (T1)X will fold away. We assume that this happens when X itself
7758 is a cast. */
7759 if (POINTER_TYPE_P (type)
7760 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7761 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7763 tree arg00 = TREE_OPERAND (arg0, 0);
7764 tree arg01 = TREE_OPERAND (arg0, 1);
7766 return fold_build_pointer_plus_loc
7767 (loc, fold_convert_loc (loc, type, arg00), arg01);
7770 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7771 of the same precision, and X is an integer type not narrower than
7772 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7773 if (INTEGRAL_TYPE_P (type)
7774 && TREE_CODE (op0) == BIT_NOT_EXPR
7775 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7776 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7777 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7779 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7780 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7781 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7782 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7783 fold_convert_loc (loc, type, tem));
7786 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7787 type of X and Y (integer types only). */
7788 if (INTEGRAL_TYPE_P (type)
7789 && TREE_CODE (op0) == MULT_EXPR
7790 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7791 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7793 /* Be careful not to introduce new overflows. */
7794 tree mult_type;
7795 if (TYPE_OVERFLOW_WRAPS (type))
7796 mult_type = type;
7797 else
7798 mult_type = unsigned_type_for (type);
7800 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7802 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7803 fold_convert_loc (loc, mult_type,
7804 TREE_OPERAND (op0, 0)),
7805 fold_convert_loc (loc, mult_type,
7806 TREE_OPERAND (op0, 1)));
7807 return fold_convert_loc (loc, type, tem);
7811 return NULL_TREE;
7813 case VIEW_CONVERT_EXPR:
7814 if (TREE_CODE (op0) == MEM_REF)
7816 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7817 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7818 tem = fold_build2_loc (loc, MEM_REF, type,
7819 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7820 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7821 return tem;
7824 return NULL_TREE;
7826 case NEGATE_EXPR:
7827 tem = fold_negate_expr (loc, arg0);
7828 if (tem)
7829 return fold_convert_loc (loc, type, tem);
7830 return NULL_TREE;
7832 case ABS_EXPR:
7833 /* Convert fabs((double)float) into (double)fabsf(float). */
7834 if (TREE_CODE (arg0) == NOP_EXPR
7835 && TREE_CODE (type) == REAL_TYPE)
7837 tree targ0 = strip_float_extensions (arg0);
7838 if (targ0 != arg0)
7839 return fold_convert_loc (loc, type,
7840 fold_build1_loc (loc, ABS_EXPR,
7841 TREE_TYPE (targ0),
7842 targ0));
7844 return NULL_TREE;
7846 case BIT_NOT_EXPR:
7847 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7848 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7849 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7850 fold_convert_loc (loc, type,
7851 TREE_OPERAND (arg0, 0)))))
7852 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7853 fold_convert_loc (loc, type,
7854 TREE_OPERAND (arg0, 1)));
7855 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7856 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7857 fold_convert_loc (loc, type,
7858 TREE_OPERAND (arg0, 1)))))
7859 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7860 fold_convert_loc (loc, type,
7861 TREE_OPERAND (arg0, 0)), tem);
7863 return NULL_TREE;
7865 case TRUTH_NOT_EXPR:
7866 /* Note that the operand of this must be an int
7867 and its values must be 0 or 1.
7868 ("true" is a fixed value perhaps depending on the language,
7869 but we don't handle values other than 1 correctly yet.) */
7870 tem = fold_truth_not_expr (loc, arg0);
7871 if (!tem)
7872 return NULL_TREE;
7873 return fold_convert_loc (loc, type, tem);
7875 case INDIRECT_REF:
7876 /* Fold *&X to X if X is an lvalue. */
7877 if (TREE_CODE (op0) == ADDR_EXPR)
7879 tree op00 = TREE_OPERAND (op0, 0);
7880 if ((VAR_P (op00)
7881 || TREE_CODE (op00) == PARM_DECL
7882 || TREE_CODE (op00) == RESULT_DECL)
7883 && !TREE_READONLY (op00))
7884 return op00;
7886 return NULL_TREE;
7888 default:
7889 return NULL_TREE;
7890 } /* switch (code) */
7894 /* If the operation was a conversion do _not_ mark a resulting constant
7895 with TREE_OVERFLOW if the original constant was not. These conversions
7896 have implementation defined behavior and retaining the TREE_OVERFLOW
7897 flag here would confuse later passes such as VRP. */
7898 tree
7899 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7900 tree type, tree op0)
7902 tree res = fold_unary_loc (loc, code, type, op0);
7903 if (res
7904 && TREE_CODE (res) == INTEGER_CST
7905 && TREE_CODE (op0) == INTEGER_CST
7906 && CONVERT_EXPR_CODE_P (code))
7907 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7909 return res;
7912 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7913 operands OP0 and OP1. LOC is the location of the resulting expression.
7914 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7915 Return the folded expression if folding is successful. Otherwise,
7916 return NULL_TREE. */
7917 static tree
7918 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7919 tree arg0, tree arg1, tree op0, tree op1)
7921 tree tem;
7923 /* We only do these simplifications if we are optimizing. */
7924 if (!optimize)
7925 return NULL_TREE;
7927 /* Check for things like (A || B) && (A || C). We can convert this
7928 to A || (B && C). Note that either operator can be any of the four
7929 truth and/or operations and the transformation will still be
7930 valid. Also note that we only care about order for the
7931 ANDIF and ORIF operators. If B contains side effects, this
7932 might change the truth-value of A. */
7933 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7934 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7935 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7936 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7937 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7938 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7940 tree a00 = TREE_OPERAND (arg0, 0);
7941 tree a01 = TREE_OPERAND (arg0, 1);
7942 tree a10 = TREE_OPERAND (arg1, 0);
7943 tree a11 = TREE_OPERAND (arg1, 1);
7944 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7945 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7946 && (code == TRUTH_AND_EXPR
7947 || code == TRUTH_OR_EXPR));
7949 if (operand_equal_p (a00, a10, 0))
7950 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7951 fold_build2_loc (loc, code, type, a01, a11));
7952 else if (commutative && operand_equal_p (a00, a11, 0))
7953 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7954 fold_build2_loc (loc, code, type, a01, a10));
7955 else if (commutative && operand_equal_p (a01, a10, 0))
7956 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
7957 fold_build2_loc (loc, code, type, a00, a11));
7959 /* This case if tricky because we must either have commutative
7960 operators or else A10 must not have side-effects. */
7962 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7963 && operand_equal_p (a01, a11, 0))
7964 return fold_build2_loc (loc, TREE_CODE (arg0), type,
7965 fold_build2_loc (loc, code, type, a00, a10),
7966 a01);
7969 /* See if we can build a range comparison. */
7970 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
7971 return tem;
7973 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
7974 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
7976 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
7977 if (tem)
7978 return fold_build2_loc (loc, code, type, tem, arg1);
7981 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
7982 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
7984 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
7985 if (tem)
7986 return fold_build2_loc (loc, code, type, arg0, tem);
7989 /* Check for the possibility of merging component references. If our
7990 lhs is another similar operation, try to merge its rhs with our
7991 rhs. Then try to merge our lhs and rhs. */
7992 if (TREE_CODE (arg0) == code
7993 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
7994 TREE_OPERAND (arg0, 1), arg1)))
7995 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
7997 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
7998 return tem;
8000 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8001 && (code == TRUTH_AND_EXPR
8002 || code == TRUTH_ANDIF_EXPR
8003 || code == TRUTH_OR_EXPR
8004 || code == TRUTH_ORIF_EXPR))
8006 enum tree_code ncode, icode;
8008 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8009 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8010 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8012 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8013 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8014 We don't want to pack more than two leafs to a non-IF AND/OR
8015 expression.
8016 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8017 equal to IF-CODE, then we don't want to add right-hand operand.
8018 If the inner right-hand side of left-hand operand has
8019 side-effects, or isn't simple, then we can't add to it,
8020 as otherwise we might destroy if-sequence. */
8021 if (TREE_CODE (arg0) == icode
8022 && simple_operand_p_2 (arg1)
8023 /* Needed for sequence points to handle trappings, and
8024 side-effects. */
8025 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8027 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8028 arg1);
8029 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8030 tem);
8032 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8033 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8034 else if (TREE_CODE (arg1) == icode
8035 && simple_operand_p_2 (arg0)
8036 /* Needed for sequence points to handle trappings, and
8037 side-effects. */
8038 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8040 tem = fold_build2_loc (loc, ncode, type,
8041 arg0, TREE_OPERAND (arg1, 0));
8042 return fold_build2_loc (loc, icode, type, tem,
8043 TREE_OPERAND (arg1, 1));
8045 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8046 into (A OR B).
8047 For sequence point consistancy, we need to check for trapping,
8048 and side-effects. */
8049 else if (code == icode && simple_operand_p_2 (arg0)
8050 && simple_operand_p_2 (arg1))
8051 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8054 return NULL_TREE;
8057 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8058 by changing CODE to reduce the magnitude of constants involved in
8059 ARG0 of the comparison.
8060 Returns a canonicalized comparison tree if a simplification was
8061 possible, otherwise returns NULL_TREE.
8062 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8063 valid if signed overflow is undefined. */
8065 static tree
8066 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8067 tree arg0, tree arg1,
8068 bool *strict_overflow_p)
8070 enum tree_code code0 = TREE_CODE (arg0);
8071 tree t, cst0 = NULL_TREE;
8072 int sgn0;
8074 /* Match A +- CST code arg1. We can change this only if overflow
8075 is undefined. */
8076 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8077 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8078 /* In principle pointers also have undefined overflow behavior,
8079 but that causes problems elsewhere. */
8080 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8081 && (code0 == MINUS_EXPR
8082 || code0 == PLUS_EXPR)
8083 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8084 return NULL_TREE;
8086 /* Identify the constant in arg0 and its sign. */
8087 cst0 = TREE_OPERAND (arg0, 1);
8088 sgn0 = tree_int_cst_sgn (cst0);
8090 /* Overflowed constants and zero will cause problems. */
8091 if (integer_zerop (cst0)
8092 || TREE_OVERFLOW (cst0))
8093 return NULL_TREE;
8095 /* See if we can reduce the magnitude of the constant in
8096 arg0 by changing the comparison code. */
8097 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8098 if (code == LT_EXPR
8099 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8100 code = LE_EXPR;
8101 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8102 else if (code == GT_EXPR
8103 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8104 code = GE_EXPR;
8105 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8106 else if (code == LE_EXPR
8107 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8108 code = LT_EXPR;
8109 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8110 else if (code == GE_EXPR
8111 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8112 code = GT_EXPR;
8113 else
8114 return NULL_TREE;
8115 *strict_overflow_p = true;
8117 /* Now build the constant reduced in magnitude. But not if that
8118 would produce one outside of its types range. */
8119 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8120 && ((sgn0 == 1
8121 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8122 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8123 || (sgn0 == -1
8124 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8125 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8126 return NULL_TREE;
8128 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8129 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8130 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8131 t = fold_convert (TREE_TYPE (arg1), t);
8133 return fold_build2_loc (loc, code, type, t, arg1);
8136 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8137 overflow further. Try to decrease the magnitude of constants involved
8138 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8139 and put sole constants at the second argument position.
8140 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8142 static tree
8143 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8144 tree arg0, tree arg1)
8146 tree t;
8147 bool strict_overflow_p;
8148 const char * const warnmsg = G_("assuming signed overflow does not occur "
8149 "when reducing constant in comparison");
8151 /* Try canonicalization by simplifying arg0. */
8152 strict_overflow_p = false;
8153 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8154 &strict_overflow_p);
8155 if (t)
8157 if (strict_overflow_p)
8158 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8159 return t;
8162 /* Try canonicalization by simplifying arg1 using the swapped
8163 comparison. */
8164 code = swap_tree_comparison (code);
8165 strict_overflow_p = false;
8166 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8167 &strict_overflow_p);
8168 if (t && strict_overflow_p)
8169 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8170 return t;
8173 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8174 space. This is used to avoid issuing overflow warnings for
8175 expressions like &p->x which can not wrap. */
8177 static bool
8178 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8180 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8181 return true;
8183 if (bitpos < 0)
8184 return true;
8186 wide_int wi_offset;
8187 int precision = TYPE_PRECISION (TREE_TYPE (base));
8188 if (offset == NULL_TREE)
8189 wi_offset = wi::zero (precision);
8190 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8191 return true;
8192 else
8193 wi_offset = offset;
8195 bool overflow;
8196 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8197 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8198 if (overflow)
8199 return true;
8201 if (!wi::fits_uhwi_p (total))
8202 return true;
8204 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8205 if (size <= 0)
8206 return true;
8208 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8209 array. */
8210 if (TREE_CODE (base) == ADDR_EXPR)
8212 HOST_WIDE_INT base_size;
8214 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8215 if (base_size > 0 && size < base_size)
8216 size = base_size;
8219 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8222 /* Return a positive integer when the symbol DECL is known to have
8223 a nonzero address, zero when it's known not to (e.g., it's a weak
8224 symbol), and a negative integer when the symbol is not yet in the
8225 symbol table and so whether or not its address is zero is unknown.
8226 For function local objects always return positive integer. */
8227 static int
8228 maybe_nonzero_address (tree decl)
8230 if (DECL_P (decl) && decl_in_symtab_p (decl))
8231 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8232 return symbol->nonzero_address ();
8234 /* Function local objects are never NULL. */
8235 if (DECL_P (decl)
8236 && (DECL_CONTEXT (decl)
8237 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8238 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8239 return 1;
8241 return -1;
8244 /* Subroutine of fold_binary. This routine performs all of the
8245 transformations that are common to the equality/inequality
8246 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8247 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8248 fold_binary should call fold_binary. Fold a comparison with
8249 tree code CODE and type TYPE with operands OP0 and OP1. Return
8250 the folded comparison or NULL_TREE. */
8252 static tree
8253 fold_comparison (location_t loc, enum tree_code code, tree type,
8254 tree op0, tree op1)
8256 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8257 tree arg0, arg1, tem;
8259 arg0 = op0;
8260 arg1 = op1;
8262 STRIP_SIGN_NOPS (arg0);
8263 STRIP_SIGN_NOPS (arg1);
8265 /* For comparisons of pointers we can decompose it to a compile time
8266 comparison of the base objects and the offsets into the object.
8267 This requires at least one operand being an ADDR_EXPR or a
8268 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8269 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8270 && (TREE_CODE (arg0) == ADDR_EXPR
8271 || TREE_CODE (arg1) == ADDR_EXPR
8272 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8273 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8275 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8276 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8277 machine_mode mode;
8278 int volatilep, reversep, unsignedp;
8279 bool indirect_base0 = false, indirect_base1 = false;
8281 /* Get base and offset for the access. Strip ADDR_EXPR for
8282 get_inner_reference, but put it back by stripping INDIRECT_REF
8283 off the base object if possible. indirect_baseN will be true
8284 if baseN is not an address but refers to the object itself. */
8285 base0 = arg0;
8286 if (TREE_CODE (arg0) == ADDR_EXPR)
8288 base0
8289 = get_inner_reference (TREE_OPERAND (arg0, 0),
8290 &bitsize, &bitpos0, &offset0, &mode,
8291 &unsignedp, &reversep, &volatilep);
8292 if (TREE_CODE (base0) == INDIRECT_REF)
8293 base0 = TREE_OPERAND (base0, 0);
8294 else
8295 indirect_base0 = true;
8297 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8299 base0 = TREE_OPERAND (arg0, 0);
8300 STRIP_SIGN_NOPS (base0);
8301 if (TREE_CODE (base0) == ADDR_EXPR)
8303 base0
8304 = get_inner_reference (TREE_OPERAND (base0, 0),
8305 &bitsize, &bitpos0, &offset0, &mode,
8306 &unsignedp, &reversep, &volatilep);
8307 if (TREE_CODE (base0) == INDIRECT_REF)
8308 base0 = TREE_OPERAND (base0, 0);
8309 else
8310 indirect_base0 = true;
8312 if (offset0 == NULL_TREE || integer_zerop (offset0))
8313 offset0 = TREE_OPERAND (arg0, 1);
8314 else
8315 offset0 = size_binop (PLUS_EXPR, offset0,
8316 TREE_OPERAND (arg0, 1));
8317 if (TREE_CODE (offset0) == INTEGER_CST)
8319 offset_int tem = wi::sext (wi::to_offset (offset0),
8320 TYPE_PRECISION (sizetype));
8321 tem <<= LOG2_BITS_PER_UNIT;
8322 tem += bitpos0;
8323 if (wi::fits_shwi_p (tem))
8325 bitpos0 = tem.to_shwi ();
8326 offset0 = NULL_TREE;
8331 base1 = arg1;
8332 if (TREE_CODE (arg1) == ADDR_EXPR)
8334 base1
8335 = get_inner_reference (TREE_OPERAND (arg1, 0),
8336 &bitsize, &bitpos1, &offset1, &mode,
8337 &unsignedp, &reversep, &volatilep);
8338 if (TREE_CODE (base1) == INDIRECT_REF)
8339 base1 = TREE_OPERAND (base1, 0);
8340 else
8341 indirect_base1 = true;
8343 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8345 base1 = TREE_OPERAND (arg1, 0);
8346 STRIP_SIGN_NOPS (base1);
8347 if (TREE_CODE (base1) == ADDR_EXPR)
8349 base1
8350 = get_inner_reference (TREE_OPERAND (base1, 0),
8351 &bitsize, &bitpos1, &offset1, &mode,
8352 &unsignedp, &reversep, &volatilep);
8353 if (TREE_CODE (base1) == INDIRECT_REF)
8354 base1 = TREE_OPERAND (base1, 0);
8355 else
8356 indirect_base1 = true;
8358 if (offset1 == NULL_TREE || integer_zerop (offset1))
8359 offset1 = TREE_OPERAND (arg1, 1);
8360 else
8361 offset1 = size_binop (PLUS_EXPR, offset1,
8362 TREE_OPERAND (arg1, 1));
8363 if (TREE_CODE (offset1) == INTEGER_CST)
8365 offset_int tem = wi::sext (wi::to_offset (offset1),
8366 TYPE_PRECISION (sizetype));
8367 tem <<= LOG2_BITS_PER_UNIT;
8368 tem += bitpos1;
8369 if (wi::fits_shwi_p (tem))
8371 bitpos1 = tem.to_shwi ();
8372 offset1 = NULL_TREE;
8377 /* If we have equivalent bases we might be able to simplify. */
8378 if (indirect_base0 == indirect_base1
8379 && operand_equal_p (base0, base1,
8380 indirect_base0 ? OEP_ADDRESS_OF : 0))
8382 /* We can fold this expression to a constant if the non-constant
8383 offset parts are equal. */
8384 if ((offset0 == offset1
8385 || (offset0 && offset1
8386 && operand_equal_p (offset0, offset1, 0)))
8387 && (equality_code
8388 || (indirect_base0
8389 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8390 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8393 if (!equality_code
8394 && bitpos0 != bitpos1
8395 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8396 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8397 fold_overflow_warning (("assuming pointer wraparound does not "
8398 "occur when comparing P +- C1 with "
8399 "P +- C2"),
8400 WARN_STRICT_OVERFLOW_CONDITIONAL);
8402 switch (code)
8404 case EQ_EXPR:
8405 return constant_boolean_node (bitpos0 == bitpos1, type);
8406 case NE_EXPR:
8407 return constant_boolean_node (bitpos0 != bitpos1, type);
8408 case LT_EXPR:
8409 return constant_boolean_node (bitpos0 < bitpos1, type);
8410 case LE_EXPR:
8411 return constant_boolean_node (bitpos0 <= bitpos1, type);
8412 case GE_EXPR:
8413 return constant_boolean_node (bitpos0 >= bitpos1, type);
8414 case GT_EXPR:
8415 return constant_boolean_node (bitpos0 > bitpos1, type);
8416 default:;
8419 /* We can simplify the comparison to a comparison of the variable
8420 offset parts if the constant offset parts are equal.
8421 Be careful to use signed sizetype here because otherwise we
8422 mess with array offsets in the wrong way. This is possible
8423 because pointer arithmetic is restricted to retain within an
8424 object and overflow on pointer differences is undefined as of
8425 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8426 else if (bitpos0 == bitpos1
8427 && (equality_code
8428 || (indirect_base0
8429 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8430 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8432 /* By converting to signed sizetype we cover middle-end pointer
8433 arithmetic which operates on unsigned pointer types of size
8434 type size and ARRAY_REF offsets which are properly sign or
8435 zero extended from their type in case it is narrower than
8436 sizetype. */
8437 if (offset0 == NULL_TREE)
8438 offset0 = build_int_cst (ssizetype, 0);
8439 else
8440 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8441 if (offset1 == NULL_TREE)
8442 offset1 = build_int_cst (ssizetype, 0);
8443 else
8444 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8446 if (!equality_code
8447 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8448 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8449 fold_overflow_warning (("assuming pointer wraparound does not "
8450 "occur when comparing P +- C1 with "
8451 "P +- C2"),
8452 WARN_STRICT_OVERFLOW_COMPARISON);
8454 return fold_build2_loc (loc, code, type, offset0, offset1);
8457 /* For equal offsets we can simplify to a comparison of the
8458 base addresses. */
8459 else if (bitpos0 == bitpos1
8460 && (indirect_base0
8461 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8462 && (indirect_base1
8463 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8464 && ((offset0 == offset1)
8465 || (offset0 && offset1
8466 && operand_equal_p (offset0, offset1, 0))))
8468 if (indirect_base0)
8469 base0 = build_fold_addr_expr_loc (loc, base0);
8470 if (indirect_base1)
8471 base1 = build_fold_addr_expr_loc (loc, base1);
8472 return fold_build2_loc (loc, code, type, base0, base1);
8474 /* Comparison between an ordinary (non-weak) symbol and a null
8475 pointer can be eliminated since such symbols must have a non
8476 null address. In C, relational expressions between pointers
8477 to objects and null pointers are undefined. The results
8478 below follow the C++ rules with the additional property that
8479 every object pointer compares greater than a null pointer.
8481 else if (((DECL_P (base0)
8482 && maybe_nonzero_address (base0) > 0
8483 /* Avoid folding references to struct members at offset 0 to
8484 prevent tests like '&ptr->firstmember == 0' from getting
8485 eliminated. When ptr is null, although the -> expression
8486 is strictly speaking invalid, GCC retains it as a matter
8487 of QoI. See PR c/44555. */
8488 && (offset0 == NULL_TREE && bitpos0 != 0))
8489 || CONSTANT_CLASS_P (base0))
8490 && indirect_base0
8491 /* The caller guarantees that when one of the arguments is
8492 constant (i.e., null in this case) it is second. */
8493 && integer_zerop (arg1))
8495 switch (code)
8497 case EQ_EXPR:
8498 case LE_EXPR:
8499 case LT_EXPR:
8500 return constant_boolean_node (false, type);
8501 case GE_EXPR:
8502 case GT_EXPR:
8503 case NE_EXPR:
8504 return constant_boolean_node (true, type);
8505 default:
8506 gcc_unreachable ();
8511 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8512 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8513 the resulting offset is smaller in absolute value than the
8514 original one and has the same sign. */
8515 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8516 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8517 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8518 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8519 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8520 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8521 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8522 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8524 tree const1 = TREE_OPERAND (arg0, 1);
8525 tree const2 = TREE_OPERAND (arg1, 1);
8526 tree variable1 = TREE_OPERAND (arg0, 0);
8527 tree variable2 = TREE_OPERAND (arg1, 0);
8528 tree cst;
8529 const char * const warnmsg = G_("assuming signed overflow does not "
8530 "occur when combining constants around "
8531 "a comparison");
8533 /* Put the constant on the side where it doesn't overflow and is
8534 of lower absolute value and of same sign than before. */
8535 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8536 ? MINUS_EXPR : PLUS_EXPR,
8537 const2, const1);
8538 if (!TREE_OVERFLOW (cst)
8539 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8540 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8542 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8543 return fold_build2_loc (loc, code, type,
8544 variable1,
8545 fold_build2_loc (loc, TREE_CODE (arg1),
8546 TREE_TYPE (arg1),
8547 variable2, cst));
8550 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8551 ? MINUS_EXPR : PLUS_EXPR,
8552 const1, const2);
8553 if (!TREE_OVERFLOW (cst)
8554 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8555 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8557 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8558 return fold_build2_loc (loc, code, type,
8559 fold_build2_loc (loc, TREE_CODE (arg0),
8560 TREE_TYPE (arg0),
8561 variable1, cst),
8562 variable2);
8566 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8567 if (tem)
8568 return tem;
8570 /* If we are comparing an expression that just has comparisons
8571 of two integer values, arithmetic expressions of those comparisons,
8572 and constants, we can simplify it. There are only three cases
8573 to check: the two values can either be equal, the first can be
8574 greater, or the second can be greater. Fold the expression for
8575 those three values. Since each value must be 0 or 1, we have
8576 eight possibilities, each of which corresponds to the constant 0
8577 or 1 or one of the six possible comparisons.
8579 This handles common cases like (a > b) == 0 but also handles
8580 expressions like ((x > y) - (y > x)) > 0, which supposedly
8581 occur in macroized code. */
8583 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8585 tree cval1 = 0, cval2 = 0;
8586 int save_p = 0;
8588 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8589 /* Don't handle degenerate cases here; they should already
8590 have been handled anyway. */
8591 && cval1 != 0 && cval2 != 0
8592 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8593 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8594 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8595 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8596 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8597 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8598 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8600 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8601 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8603 /* We can't just pass T to eval_subst in case cval1 or cval2
8604 was the same as ARG1. */
8606 tree high_result
8607 = fold_build2_loc (loc, code, type,
8608 eval_subst (loc, arg0, cval1, maxval,
8609 cval2, minval),
8610 arg1);
8611 tree equal_result
8612 = fold_build2_loc (loc, code, type,
8613 eval_subst (loc, arg0, cval1, maxval,
8614 cval2, maxval),
8615 arg1);
8616 tree low_result
8617 = fold_build2_loc (loc, code, type,
8618 eval_subst (loc, arg0, cval1, minval,
8619 cval2, maxval),
8620 arg1);
8622 /* All three of these results should be 0 or 1. Confirm they are.
8623 Then use those values to select the proper code to use. */
8625 if (TREE_CODE (high_result) == INTEGER_CST
8626 && TREE_CODE (equal_result) == INTEGER_CST
8627 && TREE_CODE (low_result) == INTEGER_CST)
8629 /* Make a 3-bit mask with the high-order bit being the
8630 value for `>', the next for '=', and the low for '<'. */
8631 switch ((integer_onep (high_result) * 4)
8632 + (integer_onep (equal_result) * 2)
8633 + integer_onep (low_result))
8635 case 0:
8636 /* Always false. */
8637 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8638 case 1:
8639 code = LT_EXPR;
8640 break;
8641 case 2:
8642 code = EQ_EXPR;
8643 break;
8644 case 3:
8645 code = LE_EXPR;
8646 break;
8647 case 4:
8648 code = GT_EXPR;
8649 break;
8650 case 5:
8651 code = NE_EXPR;
8652 break;
8653 case 6:
8654 code = GE_EXPR;
8655 break;
8656 case 7:
8657 /* Always true. */
8658 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8661 if (save_p)
8663 tem = save_expr (build2 (code, type, cval1, cval2));
8664 SET_EXPR_LOCATION (tem, loc);
8665 return tem;
8667 return fold_build2_loc (loc, code, type, cval1, cval2);
8672 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8673 into a single range test. */
8674 if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8675 && TREE_CODE (arg1) == INTEGER_CST
8676 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8677 && !integer_zerop (TREE_OPERAND (arg0, 1))
8678 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8679 && !TREE_OVERFLOW (arg1))
8681 tem = fold_div_compare (loc, code, type, arg0, arg1);
8682 if (tem != NULL_TREE)
8683 return tem;
8686 return NULL_TREE;
8690 /* Subroutine of fold_binary. Optimize complex multiplications of the
8691 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8692 argument EXPR represents the expression "z" of type TYPE. */
8694 static tree
8695 fold_mult_zconjz (location_t loc, tree type, tree expr)
8697 tree itype = TREE_TYPE (type);
8698 tree rpart, ipart, tem;
8700 if (TREE_CODE (expr) == COMPLEX_EXPR)
8702 rpart = TREE_OPERAND (expr, 0);
8703 ipart = TREE_OPERAND (expr, 1);
8705 else if (TREE_CODE (expr) == COMPLEX_CST)
8707 rpart = TREE_REALPART (expr);
8708 ipart = TREE_IMAGPART (expr);
8710 else
8712 expr = save_expr (expr);
8713 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8714 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8717 rpart = save_expr (rpart);
8718 ipart = save_expr (ipart);
8719 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8720 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8721 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8722 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8723 build_zero_cst (itype));
8727 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8728 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8730 static bool
8731 vec_cst_ctor_to_array (tree arg, tree *elts)
8733 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8735 if (TREE_CODE (arg) == VECTOR_CST)
8737 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8738 elts[i] = VECTOR_CST_ELT (arg, i);
8740 else if (TREE_CODE (arg) == CONSTRUCTOR)
8742 constructor_elt *elt;
8744 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8745 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8746 return false;
8747 else
8748 elts[i] = elt->value;
8750 else
8751 return false;
8752 for (; i < nelts; i++)
8753 elts[i]
8754 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8755 return true;
8758 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8759 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8760 NULL_TREE otherwise. */
8762 static tree
8763 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8765 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8766 tree *elts;
8767 bool need_ctor = false;
8769 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8770 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8771 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8772 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8773 return NULL_TREE;
8775 elts = XALLOCAVEC (tree, nelts * 3);
8776 if (!vec_cst_ctor_to_array (arg0, elts)
8777 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8778 return NULL_TREE;
8780 for (i = 0; i < nelts; i++)
8782 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8783 need_ctor = true;
8784 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8787 if (need_ctor)
8789 vec<constructor_elt, va_gc> *v;
8790 vec_alloc (v, nelts);
8791 for (i = 0; i < nelts; i++)
8792 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8793 return build_constructor (type, v);
8795 else
8796 return build_vector (type, &elts[2 * nelts]);
8799 /* Try to fold a pointer difference of type TYPE two address expressions of
8800 array references AREF0 and AREF1 using location LOC. Return a
8801 simplified expression for the difference or NULL_TREE. */
8803 static tree
8804 fold_addr_of_array_ref_difference (location_t loc, tree type,
8805 tree aref0, tree aref1)
8807 tree base0 = TREE_OPERAND (aref0, 0);
8808 tree base1 = TREE_OPERAND (aref1, 0);
8809 tree base_offset = build_int_cst (type, 0);
8811 /* If the bases are array references as well, recurse. If the bases
8812 are pointer indirections compute the difference of the pointers.
8813 If the bases are equal, we are set. */
8814 if ((TREE_CODE (base0) == ARRAY_REF
8815 && TREE_CODE (base1) == ARRAY_REF
8816 && (base_offset
8817 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8818 || (INDIRECT_REF_P (base0)
8819 && INDIRECT_REF_P (base1)
8820 && (base_offset
8821 = fold_binary_loc (loc, MINUS_EXPR, type,
8822 fold_convert (type, TREE_OPERAND (base0, 0)),
8823 fold_convert (type,
8824 TREE_OPERAND (base1, 0)))))
8825 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8827 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8828 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8829 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8830 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8831 return fold_build2_loc (loc, PLUS_EXPR, type,
8832 base_offset,
8833 fold_build2_loc (loc, MULT_EXPR, type,
8834 diff, esz));
8836 return NULL_TREE;
8839 /* If the real or vector real constant CST of type TYPE has an exact
8840 inverse, return it, else return NULL. */
8842 tree
8843 exact_inverse (tree type, tree cst)
8845 REAL_VALUE_TYPE r;
8846 tree unit_type, *elts;
8847 machine_mode mode;
8848 unsigned vec_nelts, i;
8850 switch (TREE_CODE (cst))
8852 case REAL_CST:
8853 r = TREE_REAL_CST (cst);
8855 if (exact_real_inverse (TYPE_MODE (type), &r))
8856 return build_real (type, r);
8858 return NULL_TREE;
8860 case VECTOR_CST:
8861 vec_nelts = VECTOR_CST_NELTS (cst);
8862 elts = XALLOCAVEC (tree, vec_nelts);
8863 unit_type = TREE_TYPE (type);
8864 mode = TYPE_MODE (unit_type);
8866 for (i = 0; i < vec_nelts; i++)
8868 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8869 if (!exact_real_inverse (mode, &r))
8870 return NULL_TREE;
8871 elts[i] = build_real (unit_type, r);
8874 return build_vector (type, elts);
8876 default:
8877 return NULL_TREE;
8881 /* Mask out the tz least significant bits of X of type TYPE where
8882 tz is the number of trailing zeroes in Y. */
8883 static wide_int
8884 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8886 int tz = wi::ctz (y);
8887 if (tz > 0)
8888 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8889 return x;
8892 /* Return true when T is an address and is known to be nonzero.
8893 For floating point we further ensure that T is not denormal.
8894 Similar logic is present in nonzero_address in rtlanal.h.
8896 If the return value is based on the assumption that signed overflow
8897 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8898 change *STRICT_OVERFLOW_P. */
8900 static bool
8901 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8903 tree type = TREE_TYPE (t);
8904 enum tree_code code;
8906 /* Doing something useful for floating point would need more work. */
8907 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8908 return false;
8910 code = TREE_CODE (t);
8911 switch (TREE_CODE_CLASS (code))
8913 case tcc_unary:
8914 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8915 strict_overflow_p);
8916 case tcc_binary:
8917 case tcc_comparison:
8918 return tree_binary_nonzero_warnv_p (code, type,
8919 TREE_OPERAND (t, 0),
8920 TREE_OPERAND (t, 1),
8921 strict_overflow_p);
8922 case tcc_constant:
8923 case tcc_declaration:
8924 case tcc_reference:
8925 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8927 default:
8928 break;
8931 switch (code)
8933 case TRUTH_NOT_EXPR:
8934 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8935 strict_overflow_p);
8937 case TRUTH_AND_EXPR:
8938 case TRUTH_OR_EXPR:
8939 case TRUTH_XOR_EXPR:
8940 return tree_binary_nonzero_warnv_p (code, type,
8941 TREE_OPERAND (t, 0),
8942 TREE_OPERAND (t, 1),
8943 strict_overflow_p);
8945 case COND_EXPR:
8946 case CONSTRUCTOR:
8947 case OBJ_TYPE_REF:
8948 case ASSERT_EXPR:
8949 case ADDR_EXPR:
8950 case WITH_SIZE_EXPR:
8951 case SSA_NAME:
8952 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8954 case COMPOUND_EXPR:
8955 case MODIFY_EXPR:
8956 case BIND_EXPR:
8957 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
8958 strict_overflow_p);
8960 case SAVE_EXPR:
8961 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
8962 strict_overflow_p);
8964 case CALL_EXPR:
8966 tree fndecl = get_callee_fndecl (t);
8967 if (!fndecl) return false;
8968 if (flag_delete_null_pointer_checks && !flag_check_new
8969 && DECL_IS_OPERATOR_NEW (fndecl)
8970 && !TREE_NOTHROW (fndecl))
8971 return true;
8972 if (flag_delete_null_pointer_checks
8973 && lookup_attribute ("returns_nonnull",
8974 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
8975 return true;
8976 return alloca_call_p (t);
8979 default:
8980 break;
8982 return false;
8985 /* Return true when T is an address and is known to be nonzero.
8986 Handle warnings about undefined signed overflow. */
8988 bool
8989 tree_expr_nonzero_p (tree t)
8991 bool ret, strict_overflow_p;
8993 strict_overflow_p = false;
8994 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
8995 if (strict_overflow_p)
8996 fold_overflow_warning (("assuming signed overflow does not occur when "
8997 "determining that expression is always "
8998 "non-zero"),
8999 WARN_STRICT_OVERFLOW_MISC);
9000 return ret;
9003 /* Return true if T is known not to be equal to an integer W. */
9005 bool
9006 expr_not_equal_to (tree t, const wide_int &w)
9008 wide_int min, max, nz;
9009 value_range_type rtype;
9010 switch (TREE_CODE (t))
9012 case INTEGER_CST:
9013 return wi::ne_p (t, w);
9015 case SSA_NAME:
9016 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9017 return false;
9018 rtype = get_range_info (t, &min, &max);
9019 if (rtype == VR_RANGE)
9021 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9022 return true;
9023 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9024 return true;
9026 else if (rtype == VR_ANTI_RANGE
9027 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9028 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9029 return true;
9030 /* If T has some known zero bits and W has any of those bits set,
9031 then T is known not to be equal to W. */
9032 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9033 TYPE_PRECISION (TREE_TYPE (t))), 0))
9034 return true;
9035 return false;
9037 default:
9038 return false;
9042 /* Fold a binary expression of code CODE and type TYPE with operands
9043 OP0 and OP1. LOC is the location of the resulting expression.
9044 Return the folded expression if folding is successful. Otherwise,
9045 return NULL_TREE. */
9047 tree
9048 fold_binary_loc (location_t loc,
9049 enum tree_code code, tree type, tree op0, tree op1)
9051 enum tree_code_class kind = TREE_CODE_CLASS (code);
9052 tree arg0, arg1, tem;
9053 tree t1 = NULL_TREE;
9054 bool strict_overflow_p;
9055 unsigned int prec;
9057 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9058 && TREE_CODE_LENGTH (code) == 2
9059 && op0 != NULL_TREE
9060 && op1 != NULL_TREE);
9062 arg0 = op0;
9063 arg1 = op1;
9065 /* Strip any conversions that don't change the mode. This is
9066 safe for every expression, except for a comparison expression
9067 because its signedness is derived from its operands. So, in
9068 the latter case, only strip conversions that don't change the
9069 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9070 preserved.
9072 Note that this is done as an internal manipulation within the
9073 constant folder, in order to find the simplest representation
9074 of the arguments so that their form can be studied. In any
9075 cases, the appropriate type conversions should be put back in
9076 the tree that will get out of the constant folder. */
9078 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9080 STRIP_SIGN_NOPS (arg0);
9081 STRIP_SIGN_NOPS (arg1);
9083 else
9085 STRIP_NOPS (arg0);
9086 STRIP_NOPS (arg1);
9089 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9090 constant but we can't do arithmetic on them. */
9091 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9093 tem = const_binop (code, type, arg0, arg1);
9094 if (tem != NULL_TREE)
9096 if (TREE_TYPE (tem) != type)
9097 tem = fold_convert_loc (loc, type, tem);
9098 return tem;
9102 /* If this is a commutative operation, and ARG0 is a constant, move it
9103 to ARG1 to reduce the number of tests below. */
9104 if (commutative_tree_code (code)
9105 && tree_swap_operands_p (arg0, arg1))
9106 return fold_build2_loc (loc, code, type, op1, op0);
9108 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9109 to ARG1 to reduce the number of tests below. */
9110 if (kind == tcc_comparison
9111 && tree_swap_operands_p (arg0, arg1))
9112 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9114 tem = generic_simplify (loc, code, type, op0, op1);
9115 if (tem)
9116 return tem;
9118 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9120 First check for cases where an arithmetic operation is applied to a
9121 compound, conditional, or comparison operation. Push the arithmetic
9122 operation inside the compound or conditional to see if any folding
9123 can then be done. Convert comparison to conditional for this purpose.
9124 The also optimizes non-constant cases that used to be done in
9125 expand_expr.
9127 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9128 one of the operands is a comparison and the other is a comparison, a
9129 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9130 code below would make the expression more complex. Change it to a
9131 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9132 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9134 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9135 || code == EQ_EXPR || code == NE_EXPR)
9136 && TREE_CODE (type) != VECTOR_TYPE
9137 && ((truth_value_p (TREE_CODE (arg0))
9138 && (truth_value_p (TREE_CODE (arg1))
9139 || (TREE_CODE (arg1) == BIT_AND_EXPR
9140 && integer_onep (TREE_OPERAND (arg1, 1)))))
9141 || (truth_value_p (TREE_CODE (arg1))
9142 && (truth_value_p (TREE_CODE (arg0))
9143 || (TREE_CODE (arg0) == BIT_AND_EXPR
9144 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9146 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9147 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9148 : TRUTH_XOR_EXPR,
9149 boolean_type_node,
9150 fold_convert_loc (loc, boolean_type_node, arg0),
9151 fold_convert_loc (loc, boolean_type_node, arg1));
9153 if (code == EQ_EXPR)
9154 tem = invert_truthvalue_loc (loc, tem);
9156 return fold_convert_loc (loc, type, tem);
9159 if (TREE_CODE_CLASS (code) == tcc_binary
9160 || TREE_CODE_CLASS (code) == tcc_comparison)
9162 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9164 tem = fold_build2_loc (loc, code, type,
9165 fold_convert_loc (loc, TREE_TYPE (op0),
9166 TREE_OPERAND (arg0, 1)), op1);
9167 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9168 tem);
9170 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9172 tem = fold_build2_loc (loc, code, type, op0,
9173 fold_convert_loc (loc, TREE_TYPE (op1),
9174 TREE_OPERAND (arg1, 1)));
9175 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9176 tem);
9179 if (TREE_CODE (arg0) == COND_EXPR
9180 || TREE_CODE (arg0) == VEC_COND_EXPR
9181 || COMPARISON_CLASS_P (arg0))
9183 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9184 arg0, arg1,
9185 /*cond_first_p=*/1);
9186 if (tem != NULL_TREE)
9187 return tem;
9190 if (TREE_CODE (arg1) == COND_EXPR
9191 || TREE_CODE (arg1) == VEC_COND_EXPR
9192 || COMPARISON_CLASS_P (arg1))
9194 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9195 arg1, arg0,
9196 /*cond_first_p=*/0);
9197 if (tem != NULL_TREE)
9198 return tem;
9202 switch (code)
9204 case MEM_REF:
9205 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9206 if (TREE_CODE (arg0) == ADDR_EXPR
9207 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9209 tree iref = TREE_OPERAND (arg0, 0);
9210 return fold_build2 (MEM_REF, type,
9211 TREE_OPERAND (iref, 0),
9212 int_const_binop (PLUS_EXPR, arg1,
9213 TREE_OPERAND (iref, 1)));
9216 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9217 if (TREE_CODE (arg0) == ADDR_EXPR
9218 && handled_component_p (TREE_OPERAND (arg0, 0)))
9220 tree base;
9221 HOST_WIDE_INT coffset;
9222 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9223 &coffset);
9224 if (!base)
9225 return NULL_TREE;
9226 return fold_build2 (MEM_REF, type,
9227 build_fold_addr_expr (base),
9228 int_const_binop (PLUS_EXPR, arg1,
9229 size_int (coffset)));
9232 return NULL_TREE;
9234 case POINTER_PLUS_EXPR:
9235 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9236 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9237 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9238 return fold_convert_loc (loc, type,
9239 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9240 fold_convert_loc (loc, sizetype,
9241 arg1),
9242 fold_convert_loc (loc, sizetype,
9243 arg0)));
9245 return NULL_TREE;
9247 case PLUS_EXPR:
9248 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9250 /* X + (X / CST) * -CST is X % CST. */
9251 if (TREE_CODE (arg1) == MULT_EXPR
9252 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9253 && operand_equal_p (arg0,
9254 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9256 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9257 tree cst1 = TREE_OPERAND (arg1, 1);
9258 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9259 cst1, cst0);
9260 if (sum && integer_zerop (sum))
9261 return fold_convert_loc (loc, type,
9262 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9263 TREE_TYPE (arg0), arg0,
9264 cst0));
9268 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9269 one. Make sure the type is not saturating and has the signedness of
9270 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9271 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9272 if ((TREE_CODE (arg0) == MULT_EXPR
9273 || TREE_CODE (arg1) == MULT_EXPR)
9274 && !TYPE_SATURATING (type)
9275 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9276 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9277 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9279 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9280 if (tem)
9281 return tem;
9284 if (! FLOAT_TYPE_P (type))
9286 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9287 (plus (plus (mult) (mult)) (foo)) so that we can
9288 take advantage of the factoring cases below. */
9289 if (ANY_INTEGRAL_TYPE_P (type)
9290 && TYPE_OVERFLOW_WRAPS (type)
9291 && (((TREE_CODE (arg0) == PLUS_EXPR
9292 || TREE_CODE (arg0) == MINUS_EXPR)
9293 && TREE_CODE (arg1) == MULT_EXPR)
9294 || ((TREE_CODE (arg1) == PLUS_EXPR
9295 || TREE_CODE (arg1) == MINUS_EXPR)
9296 && TREE_CODE (arg0) == MULT_EXPR)))
9298 tree parg0, parg1, parg, marg;
9299 enum tree_code pcode;
9301 if (TREE_CODE (arg1) == MULT_EXPR)
9302 parg = arg0, marg = arg1;
9303 else
9304 parg = arg1, marg = arg0;
9305 pcode = TREE_CODE (parg);
9306 parg0 = TREE_OPERAND (parg, 0);
9307 parg1 = TREE_OPERAND (parg, 1);
9308 STRIP_NOPS (parg0);
9309 STRIP_NOPS (parg1);
9311 if (TREE_CODE (parg0) == MULT_EXPR
9312 && TREE_CODE (parg1) != MULT_EXPR)
9313 return fold_build2_loc (loc, pcode, type,
9314 fold_build2_loc (loc, PLUS_EXPR, type,
9315 fold_convert_loc (loc, type,
9316 parg0),
9317 fold_convert_loc (loc, type,
9318 marg)),
9319 fold_convert_loc (loc, type, parg1));
9320 if (TREE_CODE (parg0) != MULT_EXPR
9321 && TREE_CODE (parg1) == MULT_EXPR)
9322 return
9323 fold_build2_loc (loc, PLUS_EXPR, type,
9324 fold_convert_loc (loc, type, parg0),
9325 fold_build2_loc (loc, pcode, type,
9326 fold_convert_loc (loc, type, marg),
9327 fold_convert_loc (loc, type,
9328 parg1)));
9331 else
9333 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9334 to __complex__ ( x, y ). This is not the same for SNaNs or
9335 if signed zeros are involved. */
9336 if (!HONOR_SNANS (element_mode (arg0))
9337 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9338 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9340 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9341 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9342 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9343 bool arg0rz = false, arg0iz = false;
9344 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9345 || (arg0i && (arg0iz = real_zerop (arg0i))))
9347 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9348 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9349 if (arg0rz && arg1i && real_zerop (arg1i))
9351 tree rp = arg1r ? arg1r
9352 : build1 (REALPART_EXPR, rtype, arg1);
9353 tree ip = arg0i ? arg0i
9354 : build1 (IMAGPART_EXPR, rtype, arg0);
9355 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9357 else if (arg0iz && arg1r && real_zerop (arg1r))
9359 tree rp = arg0r ? arg0r
9360 : build1 (REALPART_EXPR, rtype, arg0);
9361 tree ip = arg1i ? arg1i
9362 : build1 (IMAGPART_EXPR, rtype, arg1);
9363 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9368 if (flag_unsafe_math_optimizations
9369 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9370 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9371 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9372 return tem;
9374 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9375 We associate floats only if the user has specified
9376 -fassociative-math. */
9377 if (flag_associative_math
9378 && TREE_CODE (arg1) == PLUS_EXPR
9379 && TREE_CODE (arg0) != MULT_EXPR)
9381 tree tree10 = TREE_OPERAND (arg1, 0);
9382 tree tree11 = TREE_OPERAND (arg1, 1);
9383 if (TREE_CODE (tree11) == MULT_EXPR
9384 && TREE_CODE (tree10) == MULT_EXPR)
9386 tree tree0;
9387 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9388 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9391 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9392 We associate floats only if the user has specified
9393 -fassociative-math. */
9394 if (flag_associative_math
9395 && TREE_CODE (arg0) == PLUS_EXPR
9396 && TREE_CODE (arg1) != MULT_EXPR)
9398 tree tree00 = TREE_OPERAND (arg0, 0);
9399 tree tree01 = TREE_OPERAND (arg0, 1);
9400 if (TREE_CODE (tree01) == MULT_EXPR
9401 && TREE_CODE (tree00) == MULT_EXPR)
9403 tree tree0;
9404 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9405 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9410 bit_rotate:
9411 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9412 is a rotate of A by C1 bits. */
9413 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9414 is a rotate of A by B bits. */
9416 enum tree_code code0, code1;
9417 tree rtype;
9418 code0 = TREE_CODE (arg0);
9419 code1 = TREE_CODE (arg1);
9420 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9421 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9422 && operand_equal_p (TREE_OPERAND (arg0, 0),
9423 TREE_OPERAND (arg1, 0), 0)
9424 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9425 TYPE_UNSIGNED (rtype))
9426 /* Only create rotates in complete modes. Other cases are not
9427 expanded properly. */
9428 && (element_precision (rtype)
9429 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9431 tree tree01, tree11;
9432 enum tree_code code01, code11;
9434 tree01 = TREE_OPERAND (arg0, 1);
9435 tree11 = TREE_OPERAND (arg1, 1);
9436 STRIP_NOPS (tree01);
9437 STRIP_NOPS (tree11);
9438 code01 = TREE_CODE (tree01);
9439 code11 = TREE_CODE (tree11);
9440 if (code01 == INTEGER_CST
9441 && code11 == INTEGER_CST
9442 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9443 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9445 tem = build2_loc (loc, LROTATE_EXPR,
9446 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9447 TREE_OPERAND (arg0, 0),
9448 code0 == LSHIFT_EXPR
9449 ? TREE_OPERAND (arg0, 1)
9450 : TREE_OPERAND (arg1, 1));
9451 return fold_convert_loc (loc, type, tem);
9453 else if (code11 == MINUS_EXPR)
9455 tree tree110, tree111;
9456 tree110 = TREE_OPERAND (tree11, 0);
9457 tree111 = TREE_OPERAND (tree11, 1);
9458 STRIP_NOPS (tree110);
9459 STRIP_NOPS (tree111);
9460 if (TREE_CODE (tree110) == INTEGER_CST
9461 && 0 == compare_tree_int (tree110,
9462 element_precision
9463 (TREE_TYPE (TREE_OPERAND
9464 (arg0, 0))))
9465 && operand_equal_p (tree01, tree111, 0))
9466 return
9467 fold_convert_loc (loc, type,
9468 build2 ((code0 == LSHIFT_EXPR
9469 ? LROTATE_EXPR
9470 : RROTATE_EXPR),
9471 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9472 TREE_OPERAND (arg0, 0),
9473 TREE_OPERAND (arg0, 1)));
9475 else if (code01 == MINUS_EXPR)
9477 tree tree010, tree011;
9478 tree010 = TREE_OPERAND (tree01, 0);
9479 tree011 = TREE_OPERAND (tree01, 1);
9480 STRIP_NOPS (tree010);
9481 STRIP_NOPS (tree011);
9482 if (TREE_CODE (tree010) == INTEGER_CST
9483 && 0 == compare_tree_int (tree010,
9484 element_precision
9485 (TREE_TYPE (TREE_OPERAND
9486 (arg0, 0))))
9487 && operand_equal_p (tree11, tree011, 0))
9488 return fold_convert_loc
9489 (loc, type,
9490 build2 ((code0 != LSHIFT_EXPR
9491 ? LROTATE_EXPR
9492 : RROTATE_EXPR),
9493 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9494 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9499 associate:
9500 /* In most languages, can't associate operations on floats through
9501 parentheses. Rather than remember where the parentheses were, we
9502 don't associate floats at all, unless the user has specified
9503 -fassociative-math.
9504 And, we need to make sure type is not saturating. */
9506 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9507 && !TYPE_SATURATING (type))
9509 tree var0, con0, lit0, minus_lit0;
9510 tree var1, con1, lit1, minus_lit1;
9511 tree atype = type;
9512 bool ok = true;
9514 /* Split both trees into variables, constants, and literals. Then
9515 associate each group together, the constants with literals,
9516 then the result with variables. This increases the chances of
9517 literals being recombined later and of generating relocatable
9518 expressions for the sum of a constant and literal. */
9519 var0 = split_tree (loc, arg0, type, code,
9520 &con0, &lit0, &minus_lit0, 0);
9521 var1 = split_tree (loc, arg1, type, code,
9522 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9524 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9525 if (code == MINUS_EXPR)
9526 code = PLUS_EXPR;
9528 /* With undefined overflow prefer doing association in a type
9529 which wraps on overflow, if that is one of the operand types. */
9530 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9531 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9533 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9534 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9535 atype = TREE_TYPE (arg0);
9536 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9537 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9538 atype = TREE_TYPE (arg1);
9539 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9542 /* With undefined overflow we can only associate constants with one
9543 variable, and constants whose association doesn't overflow. */
9544 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9545 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9547 if (var0 && var1)
9549 tree tmp0 = var0;
9550 tree tmp1 = var1;
9551 bool one_neg = false;
9553 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9555 tmp0 = TREE_OPERAND (tmp0, 0);
9556 one_neg = !one_neg;
9558 if (CONVERT_EXPR_P (tmp0)
9559 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9560 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9561 <= TYPE_PRECISION (atype)))
9562 tmp0 = TREE_OPERAND (tmp0, 0);
9563 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9565 tmp1 = TREE_OPERAND (tmp1, 0);
9566 one_neg = !one_neg;
9568 if (CONVERT_EXPR_P (tmp1)
9569 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9570 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9571 <= TYPE_PRECISION (atype)))
9572 tmp1 = TREE_OPERAND (tmp1, 0);
9573 /* The only case we can still associate with two variables
9574 is if they cancel out. */
9575 if (!one_neg
9576 || !operand_equal_p (tmp0, tmp1, 0))
9577 ok = false;
9581 /* Only do something if we found more than two objects. Otherwise,
9582 nothing has changed and we risk infinite recursion. */
9583 if (ok
9584 && (2 < ((var0 != 0) + (var1 != 0)
9585 + (con0 != 0) + (con1 != 0)
9586 + (lit0 != 0) + (lit1 != 0)
9587 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9589 bool any_overflows = false;
9590 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9591 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9592 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9593 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9594 var0 = associate_trees (loc, var0, var1, code, atype);
9595 con0 = associate_trees (loc, con0, con1, code, atype);
9596 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9597 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9598 code, atype);
9600 /* Preserve the MINUS_EXPR if the negative part of the literal is
9601 greater than the positive part. Otherwise, the multiplicative
9602 folding code (i.e extract_muldiv) may be fooled in case
9603 unsigned constants are subtracted, like in the following
9604 example: ((X*2 + 4) - 8U)/2. */
9605 if (minus_lit0 && lit0)
9607 if (TREE_CODE (lit0) == INTEGER_CST
9608 && TREE_CODE (minus_lit0) == INTEGER_CST
9609 && tree_int_cst_lt (lit0, minus_lit0))
9611 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9612 MINUS_EXPR, atype);
9613 lit0 = 0;
9615 else
9617 lit0 = associate_trees (loc, lit0, minus_lit0,
9618 MINUS_EXPR, atype);
9619 minus_lit0 = 0;
9623 /* Don't introduce overflows through reassociation. */
9624 if (!any_overflows
9625 && ((lit0 && TREE_OVERFLOW_P (lit0))
9626 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9627 return NULL_TREE;
9629 if (minus_lit0)
9631 if (con0 == 0)
9632 return
9633 fold_convert_loc (loc, type,
9634 associate_trees (loc, var0, minus_lit0,
9635 MINUS_EXPR, atype));
9636 else
9638 con0 = associate_trees (loc, con0, minus_lit0,
9639 MINUS_EXPR, atype);
9640 return
9641 fold_convert_loc (loc, type,
9642 associate_trees (loc, var0, con0,
9643 PLUS_EXPR, atype));
9647 con0 = associate_trees (loc, con0, lit0, code, atype);
9648 return
9649 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9650 code, atype));
9654 return NULL_TREE;
9656 case MINUS_EXPR:
9657 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9658 if (TREE_CODE (arg0) == NEGATE_EXPR
9659 && negate_expr_p (op1))
9660 return fold_build2_loc (loc, MINUS_EXPR, type,
9661 negate_expr (op1),
9662 fold_convert_loc (loc, type,
9663 TREE_OPERAND (arg0, 0)));
9665 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9666 __complex__ ( x, -y ). This is not the same for SNaNs or if
9667 signed zeros are involved. */
9668 if (!HONOR_SNANS (element_mode (arg0))
9669 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9670 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9672 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9673 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9674 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9675 bool arg0rz = false, arg0iz = false;
9676 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9677 || (arg0i && (arg0iz = real_zerop (arg0i))))
9679 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9680 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9681 if (arg0rz && arg1i && real_zerop (arg1i))
9683 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9684 arg1r ? arg1r
9685 : build1 (REALPART_EXPR, rtype, arg1));
9686 tree ip = arg0i ? arg0i
9687 : build1 (IMAGPART_EXPR, rtype, arg0);
9688 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9690 else if (arg0iz && arg1r && real_zerop (arg1r))
9692 tree rp = arg0r ? arg0r
9693 : build1 (REALPART_EXPR, rtype, arg0);
9694 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9695 arg1i ? arg1i
9696 : build1 (IMAGPART_EXPR, rtype, arg1));
9697 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9702 /* A - B -> A + (-B) if B is easily negatable. */
9703 if (negate_expr_p (op1)
9704 && ! TYPE_OVERFLOW_SANITIZED (type)
9705 && ((FLOAT_TYPE_P (type)
9706 /* Avoid this transformation if B is a positive REAL_CST. */
9707 && (TREE_CODE (op1) != REAL_CST
9708 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9709 || INTEGRAL_TYPE_P (type)))
9710 return fold_build2_loc (loc, PLUS_EXPR, type,
9711 fold_convert_loc (loc, type, arg0),
9712 negate_expr (op1));
9714 /* Fold &a[i] - &a[j] to i-j. */
9715 if (TREE_CODE (arg0) == ADDR_EXPR
9716 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9717 && TREE_CODE (arg1) == ADDR_EXPR
9718 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9720 tree tem = fold_addr_of_array_ref_difference (loc, type,
9721 TREE_OPERAND (arg0, 0),
9722 TREE_OPERAND (arg1, 0));
9723 if (tem)
9724 return tem;
9727 if (FLOAT_TYPE_P (type)
9728 && flag_unsafe_math_optimizations
9729 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9730 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9731 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9732 return tem;
9734 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9735 one. Make sure the type is not saturating and has the signedness of
9736 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9737 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9738 if ((TREE_CODE (arg0) == MULT_EXPR
9739 || TREE_CODE (arg1) == MULT_EXPR)
9740 && !TYPE_SATURATING (type)
9741 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9742 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9743 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9745 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9746 if (tem)
9747 return tem;
9750 goto associate;
9752 case MULT_EXPR:
9753 if (! FLOAT_TYPE_P (type))
9755 /* Transform x * -C into -x * C if x is easily negatable. */
9756 if (TREE_CODE (op1) == INTEGER_CST
9757 && tree_int_cst_sgn (op1) == -1
9758 && negate_expr_p (op0)
9759 && (tem = negate_expr (op1)) != op1
9760 && ! TREE_OVERFLOW (tem))
9761 return fold_build2_loc (loc, MULT_EXPR, type,
9762 fold_convert_loc (loc, type,
9763 negate_expr (op0)), tem);
9765 strict_overflow_p = false;
9766 if (TREE_CODE (arg1) == INTEGER_CST
9767 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9768 &strict_overflow_p)))
9770 if (strict_overflow_p)
9771 fold_overflow_warning (("assuming signed overflow does not "
9772 "occur when simplifying "
9773 "multiplication"),
9774 WARN_STRICT_OVERFLOW_MISC);
9775 return fold_convert_loc (loc, type, tem);
9778 /* Optimize z * conj(z) for integer complex numbers. */
9779 if (TREE_CODE (arg0) == CONJ_EXPR
9780 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9781 return fold_mult_zconjz (loc, type, arg1);
9782 if (TREE_CODE (arg1) == CONJ_EXPR
9783 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9784 return fold_mult_zconjz (loc, type, arg0);
9786 else
9788 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9789 This is not the same for NaNs or if signed zeros are
9790 involved. */
9791 if (!HONOR_NANS (arg0)
9792 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9793 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9794 && TREE_CODE (arg1) == COMPLEX_CST
9795 && real_zerop (TREE_REALPART (arg1)))
9797 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9798 if (real_onep (TREE_IMAGPART (arg1)))
9799 return
9800 fold_build2_loc (loc, COMPLEX_EXPR, type,
9801 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9802 rtype, arg0)),
9803 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9804 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9805 return
9806 fold_build2_loc (loc, COMPLEX_EXPR, type,
9807 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9808 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9809 rtype, arg0)));
9812 /* Optimize z * conj(z) for floating point complex numbers.
9813 Guarded by flag_unsafe_math_optimizations as non-finite
9814 imaginary components don't produce scalar results. */
9815 if (flag_unsafe_math_optimizations
9816 && TREE_CODE (arg0) == CONJ_EXPR
9817 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9818 return fold_mult_zconjz (loc, type, arg1);
9819 if (flag_unsafe_math_optimizations
9820 && TREE_CODE (arg1) == CONJ_EXPR
9821 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9822 return fold_mult_zconjz (loc, type, arg0);
9824 goto associate;
9826 case BIT_IOR_EXPR:
9827 /* Canonicalize (X & C1) | C2. */
9828 if (TREE_CODE (arg0) == BIT_AND_EXPR
9829 && TREE_CODE (arg1) == INTEGER_CST
9830 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9832 int width = TYPE_PRECISION (type), w;
9833 wide_int c1 = TREE_OPERAND (arg0, 1);
9834 wide_int c2 = arg1;
9836 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9837 if ((c1 & c2) == c1)
9838 return omit_one_operand_loc (loc, type, arg1,
9839 TREE_OPERAND (arg0, 0));
9841 wide_int msk = wi::mask (width, false,
9842 TYPE_PRECISION (TREE_TYPE (arg1)));
9844 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9845 if (msk.and_not (c1 | c2) == 0)
9846 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9847 TREE_OPERAND (arg0, 0), arg1);
9849 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9850 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9851 mode which allows further optimizations. */
9852 c1 &= msk;
9853 c2 &= msk;
9854 wide_int c3 = c1.and_not (c2);
9855 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9857 wide_int mask = wi::mask (w, false,
9858 TYPE_PRECISION (type));
9859 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9861 c3 = mask;
9862 break;
9866 if (c3 != c1)
9867 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9868 fold_build2_loc (loc, BIT_AND_EXPR, type,
9869 TREE_OPERAND (arg0, 0),
9870 wide_int_to_tree (type,
9871 c3)),
9872 arg1);
9875 /* See if this can be simplified into a rotate first. If that
9876 is unsuccessful continue in the association code. */
9877 goto bit_rotate;
9879 case BIT_XOR_EXPR:
9880 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9881 if (TREE_CODE (arg0) == BIT_AND_EXPR
9882 && INTEGRAL_TYPE_P (type)
9883 && integer_onep (TREE_OPERAND (arg0, 1))
9884 && integer_onep (arg1))
9885 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9886 build_zero_cst (TREE_TYPE (arg0)));
9888 /* See if this can be simplified into a rotate first. If that
9889 is unsuccessful continue in the association code. */
9890 goto bit_rotate;
9892 case BIT_AND_EXPR:
9893 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9894 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9895 && INTEGRAL_TYPE_P (type)
9896 && integer_onep (TREE_OPERAND (arg0, 1))
9897 && integer_onep (arg1))
9899 tree tem2;
9900 tem = TREE_OPERAND (arg0, 0);
9901 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9902 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9903 tem, tem2);
9904 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9905 build_zero_cst (TREE_TYPE (tem)));
9907 /* Fold ~X & 1 as (X & 1) == 0. */
9908 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9909 && INTEGRAL_TYPE_P (type)
9910 && integer_onep (arg1))
9912 tree tem2;
9913 tem = TREE_OPERAND (arg0, 0);
9914 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9915 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9916 tem, tem2);
9917 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9918 build_zero_cst (TREE_TYPE (tem)));
9920 /* Fold !X & 1 as X == 0. */
9921 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9922 && integer_onep (arg1))
9924 tem = TREE_OPERAND (arg0, 0);
9925 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9926 build_zero_cst (TREE_TYPE (tem)));
9929 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9930 multiple of 1 << CST. */
9931 if (TREE_CODE (arg1) == INTEGER_CST)
9933 wide_int cst1 = arg1;
9934 wide_int ncst1 = -cst1;
9935 if ((cst1 & ncst1) == ncst1
9936 && multiple_of_p (type, arg0,
9937 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
9938 return fold_convert_loc (loc, type, arg0);
9941 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9942 bits from CST2. */
9943 if (TREE_CODE (arg1) == INTEGER_CST
9944 && TREE_CODE (arg0) == MULT_EXPR
9945 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9947 wide_int warg1 = arg1;
9948 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
9950 if (masked == 0)
9951 return omit_two_operands_loc (loc, type, build_zero_cst (type),
9952 arg0, arg1);
9953 else if (masked != warg1)
9955 /* Avoid the transform if arg1 is a mask of some
9956 mode which allows further optimizations. */
9957 int pop = wi::popcount (warg1);
9958 if (!(pop >= BITS_PER_UNIT
9959 && pow2p_hwi (pop)
9960 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
9961 return fold_build2_loc (loc, code, type, op0,
9962 wide_int_to_tree (type, masked));
9966 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
9967 ((A & N) + B) & M -> (A + B) & M
9968 Similarly if (N & M) == 0,
9969 ((A | N) + B) & M -> (A + B) & M
9970 and for - instead of + (or unary - instead of +)
9971 and/or ^ instead of |.
9972 If B is constant and (B & M) == 0, fold into A & M. */
9973 if (TREE_CODE (arg1) == INTEGER_CST)
9975 wide_int cst1 = arg1;
9976 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
9977 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9978 && (TREE_CODE (arg0) == PLUS_EXPR
9979 || TREE_CODE (arg0) == MINUS_EXPR
9980 || TREE_CODE (arg0) == NEGATE_EXPR)
9981 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
9982 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
9984 tree pmop[2];
9985 int which = 0;
9986 wide_int cst0;
9988 /* Now we know that arg0 is (C + D) or (C - D) or
9989 -C and arg1 (M) is == (1LL << cst) - 1.
9990 Store C into PMOP[0] and D into PMOP[1]. */
9991 pmop[0] = TREE_OPERAND (arg0, 0);
9992 pmop[1] = NULL;
9993 if (TREE_CODE (arg0) != NEGATE_EXPR)
9995 pmop[1] = TREE_OPERAND (arg0, 1);
9996 which = 1;
9999 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10000 which = -1;
10002 for (; which >= 0; which--)
10003 switch (TREE_CODE (pmop[which]))
10005 case BIT_AND_EXPR:
10006 case BIT_IOR_EXPR:
10007 case BIT_XOR_EXPR:
10008 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10009 != INTEGER_CST)
10010 break;
10011 cst0 = TREE_OPERAND (pmop[which], 1);
10012 cst0 &= cst1;
10013 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10015 if (cst0 != cst1)
10016 break;
10018 else if (cst0 != 0)
10019 break;
10020 /* If C or D is of the form (A & N) where
10021 (N & M) == M, or of the form (A | N) or
10022 (A ^ N) where (N & M) == 0, replace it with A. */
10023 pmop[which] = TREE_OPERAND (pmop[which], 0);
10024 break;
10025 case INTEGER_CST:
10026 /* If C or D is a N where (N & M) == 0, it can be
10027 omitted (assumed 0). */
10028 if ((TREE_CODE (arg0) == PLUS_EXPR
10029 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10030 && (cst1 & pmop[which]) == 0)
10031 pmop[which] = NULL;
10032 break;
10033 default:
10034 break;
10037 /* Only build anything new if we optimized one or both arguments
10038 above. */
10039 if (pmop[0] != TREE_OPERAND (arg0, 0)
10040 || (TREE_CODE (arg0) != NEGATE_EXPR
10041 && pmop[1] != TREE_OPERAND (arg0, 1)))
10043 tree utype = TREE_TYPE (arg0);
10044 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10046 /* Perform the operations in a type that has defined
10047 overflow behavior. */
10048 utype = unsigned_type_for (TREE_TYPE (arg0));
10049 if (pmop[0] != NULL)
10050 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10051 if (pmop[1] != NULL)
10052 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10055 if (TREE_CODE (arg0) == NEGATE_EXPR)
10056 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10057 else if (TREE_CODE (arg0) == PLUS_EXPR)
10059 if (pmop[0] != NULL && pmop[1] != NULL)
10060 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10061 pmop[0], pmop[1]);
10062 else if (pmop[0] != NULL)
10063 tem = pmop[0];
10064 else if (pmop[1] != NULL)
10065 tem = pmop[1];
10066 else
10067 return build_int_cst (type, 0);
10069 else if (pmop[0] == NULL)
10070 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10071 else
10072 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10073 pmop[0], pmop[1]);
10074 /* TEM is now the new binary +, - or unary - replacement. */
10075 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10076 fold_convert_loc (loc, utype, arg1));
10077 return fold_convert_loc (loc, type, tem);
10082 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10083 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10084 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10086 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10088 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10089 if (mask == -1)
10090 return
10091 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10094 goto associate;
10096 case RDIV_EXPR:
10097 /* Don't touch a floating-point divide by zero unless the mode
10098 of the constant can represent infinity. */
10099 if (TREE_CODE (arg1) == REAL_CST
10100 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10101 && real_zerop (arg1))
10102 return NULL_TREE;
10104 /* (-A) / (-B) -> A / B */
10105 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10106 return fold_build2_loc (loc, RDIV_EXPR, type,
10107 TREE_OPERAND (arg0, 0),
10108 negate_expr (arg1));
10109 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10110 return fold_build2_loc (loc, RDIV_EXPR, type,
10111 negate_expr (arg0),
10112 TREE_OPERAND (arg1, 0));
10113 return NULL_TREE;
10115 case TRUNC_DIV_EXPR:
10116 /* Fall through */
10118 case FLOOR_DIV_EXPR:
10119 /* Simplify A / (B << N) where A and B are positive and B is
10120 a power of 2, to A >> (N + log2(B)). */
10121 strict_overflow_p = false;
10122 if (TREE_CODE (arg1) == LSHIFT_EXPR
10123 && (TYPE_UNSIGNED (type)
10124 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10126 tree sval = TREE_OPERAND (arg1, 0);
10127 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10129 tree sh_cnt = TREE_OPERAND (arg1, 1);
10130 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10131 wi::exact_log2 (sval));
10133 if (strict_overflow_p)
10134 fold_overflow_warning (("assuming signed overflow does not "
10135 "occur when simplifying A / (B << N)"),
10136 WARN_STRICT_OVERFLOW_MISC);
10138 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10139 sh_cnt, pow2);
10140 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10141 fold_convert_loc (loc, type, arg0), sh_cnt);
10145 /* Fall through */
10147 case ROUND_DIV_EXPR:
10148 case CEIL_DIV_EXPR:
10149 case EXACT_DIV_EXPR:
10150 if (integer_zerop (arg1))
10151 return NULL_TREE;
10153 /* Convert -A / -B to A / B when the type is signed and overflow is
10154 undefined. */
10155 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10156 && TREE_CODE (arg0) == NEGATE_EXPR
10157 && negate_expr_p (op1))
10159 if (INTEGRAL_TYPE_P (type))
10160 fold_overflow_warning (("assuming signed overflow does not occur "
10161 "when distributing negation across "
10162 "division"),
10163 WARN_STRICT_OVERFLOW_MISC);
10164 return fold_build2_loc (loc, code, type,
10165 fold_convert_loc (loc, type,
10166 TREE_OPERAND (arg0, 0)),
10167 negate_expr (op1));
10169 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10170 && TREE_CODE (arg1) == NEGATE_EXPR
10171 && negate_expr_p (op0))
10173 if (INTEGRAL_TYPE_P (type))
10174 fold_overflow_warning (("assuming signed overflow does not occur "
10175 "when distributing negation across "
10176 "division"),
10177 WARN_STRICT_OVERFLOW_MISC);
10178 return fold_build2_loc (loc, code, type,
10179 negate_expr (op0),
10180 fold_convert_loc (loc, type,
10181 TREE_OPERAND (arg1, 0)));
10184 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10185 operation, EXACT_DIV_EXPR.
10187 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10188 At one time others generated faster code, it's not clear if they do
10189 after the last round to changes to the DIV code in expmed.c. */
10190 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10191 && multiple_of_p (type, arg0, arg1))
10192 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10193 fold_convert (type, arg0),
10194 fold_convert (type, arg1));
10196 strict_overflow_p = false;
10197 if (TREE_CODE (arg1) == INTEGER_CST
10198 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10199 &strict_overflow_p)))
10201 if (strict_overflow_p)
10202 fold_overflow_warning (("assuming signed overflow does not occur "
10203 "when simplifying division"),
10204 WARN_STRICT_OVERFLOW_MISC);
10205 return fold_convert_loc (loc, type, tem);
10208 return NULL_TREE;
10210 case CEIL_MOD_EXPR:
10211 case FLOOR_MOD_EXPR:
10212 case ROUND_MOD_EXPR:
10213 case TRUNC_MOD_EXPR:
10214 strict_overflow_p = false;
10215 if (TREE_CODE (arg1) == INTEGER_CST
10216 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10217 &strict_overflow_p)))
10219 if (strict_overflow_p)
10220 fold_overflow_warning (("assuming signed overflow does not occur "
10221 "when simplifying modulus"),
10222 WARN_STRICT_OVERFLOW_MISC);
10223 return fold_convert_loc (loc, type, tem);
10226 return NULL_TREE;
10228 case LROTATE_EXPR:
10229 case RROTATE_EXPR:
10230 case RSHIFT_EXPR:
10231 case LSHIFT_EXPR:
10232 /* Since negative shift count is not well-defined,
10233 don't try to compute it in the compiler. */
10234 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10235 return NULL_TREE;
10237 prec = element_precision (type);
10239 /* If we have a rotate of a bit operation with the rotate count and
10240 the second operand of the bit operation both constant,
10241 permute the two operations. */
10242 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10243 && (TREE_CODE (arg0) == BIT_AND_EXPR
10244 || TREE_CODE (arg0) == BIT_IOR_EXPR
10245 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10246 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10248 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10249 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10250 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10251 fold_build2_loc (loc, code, type,
10252 arg00, arg1),
10253 fold_build2_loc (loc, code, type,
10254 arg01, arg1));
10257 /* Two consecutive rotates adding up to the some integer
10258 multiple of the precision of the type can be ignored. */
10259 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10260 && TREE_CODE (arg0) == RROTATE_EXPR
10261 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10262 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10263 prec) == 0)
10264 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10266 return NULL_TREE;
10268 case MIN_EXPR:
10269 case MAX_EXPR:
10270 goto associate;
10272 case TRUTH_ANDIF_EXPR:
10273 /* Note that the operands of this must be ints
10274 and their values must be 0 or 1.
10275 ("true" is a fixed value perhaps depending on the language.) */
10276 /* If first arg is constant zero, return it. */
10277 if (integer_zerop (arg0))
10278 return fold_convert_loc (loc, type, arg0);
10279 /* FALLTHRU */
10280 case TRUTH_AND_EXPR:
10281 /* If either arg is constant true, drop it. */
10282 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10283 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10284 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10285 /* Preserve sequence points. */
10286 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10287 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10288 /* If second arg is constant zero, result is zero, but first arg
10289 must be evaluated. */
10290 if (integer_zerop (arg1))
10291 return omit_one_operand_loc (loc, type, arg1, arg0);
10292 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10293 case will be handled here. */
10294 if (integer_zerop (arg0))
10295 return omit_one_operand_loc (loc, type, arg0, arg1);
10297 /* !X && X is always false. */
10298 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10299 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10300 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10301 /* X && !X is always false. */
10302 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10303 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10304 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10306 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10307 means A >= Y && A != MAX, but in this case we know that
10308 A < X <= MAX. */
10310 if (!TREE_SIDE_EFFECTS (arg0)
10311 && !TREE_SIDE_EFFECTS (arg1))
10313 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10314 if (tem && !operand_equal_p (tem, arg0, 0))
10315 return fold_build2_loc (loc, code, type, tem, arg1);
10317 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10318 if (tem && !operand_equal_p (tem, arg1, 0))
10319 return fold_build2_loc (loc, code, type, arg0, tem);
10322 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10323 != NULL_TREE)
10324 return tem;
10326 return NULL_TREE;
10328 case TRUTH_ORIF_EXPR:
10329 /* Note that the operands of this must be ints
10330 and their values must be 0 or true.
10331 ("true" is a fixed value perhaps depending on the language.) */
10332 /* If first arg is constant true, return it. */
10333 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10334 return fold_convert_loc (loc, type, arg0);
10335 /* FALLTHRU */
10336 case TRUTH_OR_EXPR:
10337 /* If either arg is constant zero, drop it. */
10338 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10339 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10340 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10341 /* Preserve sequence points. */
10342 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10343 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10344 /* If second arg is constant true, result is true, but we must
10345 evaluate first arg. */
10346 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10347 return omit_one_operand_loc (loc, type, arg1, arg0);
10348 /* Likewise for first arg, but note this only occurs here for
10349 TRUTH_OR_EXPR. */
10350 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10351 return omit_one_operand_loc (loc, type, arg0, arg1);
10353 /* !X || X is always true. */
10354 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10355 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10356 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10357 /* X || !X is always true. */
10358 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10359 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10360 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10362 /* (X && !Y) || (!X && Y) is X ^ Y */
10363 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10364 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10366 tree a0, a1, l0, l1, n0, n1;
10368 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10369 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10371 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10372 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10374 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10375 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10377 if ((operand_equal_p (n0, a0, 0)
10378 && operand_equal_p (n1, a1, 0))
10379 || (operand_equal_p (n0, a1, 0)
10380 && operand_equal_p (n1, a0, 0)))
10381 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10384 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10385 != NULL_TREE)
10386 return tem;
10388 return NULL_TREE;
10390 case TRUTH_XOR_EXPR:
10391 /* If the second arg is constant zero, drop it. */
10392 if (integer_zerop (arg1))
10393 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10394 /* If the second arg is constant true, this is a logical inversion. */
10395 if (integer_onep (arg1))
10397 tem = invert_truthvalue_loc (loc, arg0);
10398 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10400 /* Identical arguments cancel to zero. */
10401 if (operand_equal_p (arg0, arg1, 0))
10402 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10404 /* !X ^ X is always true. */
10405 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10406 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10407 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10409 /* X ^ !X is always true. */
10410 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10411 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10412 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10414 return NULL_TREE;
10416 case EQ_EXPR:
10417 case NE_EXPR:
10418 STRIP_NOPS (arg0);
10419 STRIP_NOPS (arg1);
10421 tem = fold_comparison (loc, code, type, op0, op1);
10422 if (tem != NULL_TREE)
10423 return tem;
10425 /* bool_var != 1 becomes !bool_var. */
10426 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10427 && code == NE_EXPR)
10428 return fold_convert_loc (loc, type,
10429 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10430 TREE_TYPE (arg0), arg0));
10432 /* bool_var == 0 becomes !bool_var. */
10433 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10434 && code == EQ_EXPR)
10435 return fold_convert_loc (loc, type,
10436 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10437 TREE_TYPE (arg0), arg0));
10439 /* !exp != 0 becomes !exp */
10440 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10441 && code == NE_EXPR)
10442 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10444 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10445 if ((TREE_CODE (arg0) == PLUS_EXPR
10446 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10447 || TREE_CODE (arg0) == MINUS_EXPR)
10448 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10449 0)),
10450 arg1, 0)
10451 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10452 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10454 tree val = TREE_OPERAND (arg0, 1);
10455 val = fold_build2_loc (loc, code, type, val,
10456 build_int_cst (TREE_TYPE (val), 0));
10457 return omit_two_operands_loc (loc, type, val,
10458 TREE_OPERAND (arg0, 0), arg1);
10461 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10462 if ((TREE_CODE (arg1) == PLUS_EXPR
10463 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10464 || TREE_CODE (arg1) == MINUS_EXPR)
10465 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10466 0)),
10467 arg0, 0)
10468 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10469 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10471 tree val = TREE_OPERAND (arg1, 1);
10472 val = fold_build2_loc (loc, code, type, val,
10473 build_int_cst (TREE_TYPE (val), 0));
10474 return omit_two_operands_loc (loc, type, val,
10475 TREE_OPERAND (arg1, 0), arg0);
10478 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10479 if (TREE_CODE (arg0) == MINUS_EXPR
10480 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10481 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10482 1)),
10483 arg1, 0)
10484 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10485 return omit_two_operands_loc (loc, type,
10486 code == NE_EXPR
10487 ? boolean_true_node : boolean_false_node,
10488 TREE_OPERAND (arg0, 1), arg1);
10490 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10491 if (TREE_CODE (arg1) == MINUS_EXPR
10492 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10493 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10494 1)),
10495 arg0, 0)
10496 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10497 return omit_two_operands_loc (loc, type,
10498 code == NE_EXPR
10499 ? boolean_true_node : boolean_false_node,
10500 TREE_OPERAND (arg1, 1), arg0);
10502 /* If this is an EQ or NE comparison with zero and ARG0 is
10503 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10504 two operations, but the latter can be done in one less insn
10505 on machines that have only two-operand insns or on which a
10506 constant cannot be the first operand. */
10507 if (TREE_CODE (arg0) == BIT_AND_EXPR
10508 && integer_zerop (arg1))
10510 tree arg00 = TREE_OPERAND (arg0, 0);
10511 tree arg01 = TREE_OPERAND (arg0, 1);
10512 if (TREE_CODE (arg00) == LSHIFT_EXPR
10513 && integer_onep (TREE_OPERAND (arg00, 0)))
10515 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10516 arg01, TREE_OPERAND (arg00, 1));
10517 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10518 build_int_cst (TREE_TYPE (arg0), 1));
10519 return fold_build2_loc (loc, code, type,
10520 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10521 arg1);
10523 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10524 && integer_onep (TREE_OPERAND (arg01, 0)))
10526 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10527 arg00, TREE_OPERAND (arg01, 1));
10528 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10529 build_int_cst (TREE_TYPE (arg0), 1));
10530 return fold_build2_loc (loc, code, type,
10531 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10532 arg1);
10536 /* If this is an NE or EQ comparison of zero against the result of a
10537 signed MOD operation whose second operand is a power of 2, make
10538 the MOD operation unsigned since it is simpler and equivalent. */
10539 if (integer_zerop (arg1)
10540 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10541 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10542 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10543 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10544 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10545 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10547 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10548 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10549 fold_convert_loc (loc, newtype,
10550 TREE_OPERAND (arg0, 0)),
10551 fold_convert_loc (loc, newtype,
10552 TREE_OPERAND (arg0, 1)));
10554 return fold_build2_loc (loc, code, type, newmod,
10555 fold_convert_loc (loc, newtype, arg1));
10558 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10559 C1 is a valid shift constant, and C2 is a power of two, i.e.
10560 a single bit. */
10561 if (TREE_CODE (arg0) == BIT_AND_EXPR
10562 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10563 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10564 == INTEGER_CST
10565 && integer_pow2p (TREE_OPERAND (arg0, 1))
10566 && integer_zerop (arg1))
10568 tree itype = TREE_TYPE (arg0);
10569 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10570 prec = TYPE_PRECISION (itype);
10572 /* Check for a valid shift count. */
10573 if (wi::ltu_p (arg001, prec))
10575 tree arg01 = TREE_OPERAND (arg0, 1);
10576 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10577 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10578 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10579 can be rewritten as (X & (C2 << C1)) != 0. */
10580 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10582 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10583 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10584 return fold_build2_loc (loc, code, type, tem,
10585 fold_convert_loc (loc, itype, arg1));
10587 /* Otherwise, for signed (arithmetic) shifts,
10588 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10589 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10590 else if (!TYPE_UNSIGNED (itype))
10591 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10592 arg000, build_int_cst (itype, 0));
10593 /* Otherwise, of unsigned (logical) shifts,
10594 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10595 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10596 else
10597 return omit_one_operand_loc (loc, type,
10598 code == EQ_EXPR ? integer_one_node
10599 : integer_zero_node,
10600 arg000);
10604 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10605 Similarly for NE_EXPR. */
10606 if (TREE_CODE (arg0) == BIT_AND_EXPR
10607 && TREE_CODE (arg1) == INTEGER_CST
10608 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10610 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10611 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10612 TREE_OPERAND (arg0, 1));
10613 tree dandnotc
10614 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10615 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10616 notc);
10617 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10618 if (integer_nonzerop (dandnotc))
10619 return omit_one_operand_loc (loc, type, rslt, arg0);
10622 /* If this is a comparison of a field, we may be able to simplify it. */
10623 if ((TREE_CODE (arg0) == COMPONENT_REF
10624 || TREE_CODE (arg0) == BIT_FIELD_REF)
10625 /* Handle the constant case even without -O
10626 to make sure the warnings are given. */
10627 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10629 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10630 if (t1)
10631 return t1;
10634 /* Optimize comparisons of strlen vs zero to a compare of the
10635 first character of the string vs zero. To wit,
10636 strlen(ptr) == 0 => *ptr == 0
10637 strlen(ptr) != 0 => *ptr != 0
10638 Other cases should reduce to one of these two (or a constant)
10639 due to the return value of strlen being unsigned. */
10640 if (TREE_CODE (arg0) == CALL_EXPR
10641 && integer_zerop (arg1))
10643 tree fndecl = get_callee_fndecl (arg0);
10645 if (fndecl
10646 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10647 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10648 && call_expr_nargs (arg0) == 1
10649 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10651 tree iref = build_fold_indirect_ref_loc (loc,
10652 CALL_EXPR_ARG (arg0, 0));
10653 return fold_build2_loc (loc, code, type, iref,
10654 build_int_cst (TREE_TYPE (iref), 0));
10658 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10659 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10660 if (TREE_CODE (arg0) == RSHIFT_EXPR
10661 && integer_zerop (arg1)
10662 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10664 tree arg00 = TREE_OPERAND (arg0, 0);
10665 tree arg01 = TREE_OPERAND (arg0, 1);
10666 tree itype = TREE_TYPE (arg00);
10667 if (wi::eq_p (arg01, element_precision (itype) - 1))
10669 if (TYPE_UNSIGNED (itype))
10671 itype = signed_type_for (itype);
10672 arg00 = fold_convert_loc (loc, itype, arg00);
10674 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10675 type, arg00, build_zero_cst (itype));
10679 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10680 (X & C) == 0 when C is a single bit. */
10681 if (TREE_CODE (arg0) == BIT_AND_EXPR
10682 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10683 && integer_zerop (arg1)
10684 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10686 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10687 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10688 TREE_OPERAND (arg0, 1));
10689 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10690 type, tem,
10691 fold_convert_loc (loc, TREE_TYPE (arg0),
10692 arg1));
10695 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10696 constant C is a power of two, i.e. a single bit. */
10697 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10698 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10699 && integer_zerop (arg1)
10700 && integer_pow2p (TREE_OPERAND (arg0, 1))
10701 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10702 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10704 tree arg00 = TREE_OPERAND (arg0, 0);
10705 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10706 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10709 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10710 when is C is a power of two, i.e. a single bit. */
10711 if (TREE_CODE (arg0) == BIT_AND_EXPR
10712 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10713 && integer_zerop (arg1)
10714 && integer_pow2p (TREE_OPERAND (arg0, 1))
10715 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10716 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10718 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10719 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10720 arg000, TREE_OPERAND (arg0, 1));
10721 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10722 tem, build_int_cst (TREE_TYPE (tem), 0));
10725 if (integer_zerop (arg1)
10726 && tree_expr_nonzero_p (arg0))
10728 tree res = constant_boolean_node (code==NE_EXPR, type);
10729 return omit_one_operand_loc (loc, type, res, arg0);
10732 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10733 if (TREE_CODE (arg0) == BIT_AND_EXPR
10734 && TREE_CODE (arg1) == BIT_AND_EXPR)
10736 tree arg00 = TREE_OPERAND (arg0, 0);
10737 tree arg01 = TREE_OPERAND (arg0, 1);
10738 tree arg10 = TREE_OPERAND (arg1, 0);
10739 tree arg11 = TREE_OPERAND (arg1, 1);
10740 tree itype = TREE_TYPE (arg0);
10742 if (operand_equal_p (arg01, arg11, 0))
10743 return fold_build2_loc (loc, code, type,
10744 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10745 fold_build2_loc (loc,
10746 BIT_XOR_EXPR, itype,
10747 arg00, arg10),
10748 arg01),
10749 build_zero_cst (itype));
10751 if (operand_equal_p (arg01, arg10, 0))
10752 return fold_build2_loc (loc, code, type,
10753 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10754 fold_build2_loc (loc,
10755 BIT_XOR_EXPR, itype,
10756 arg00, arg11),
10757 arg01),
10758 build_zero_cst (itype));
10760 if (operand_equal_p (arg00, arg11, 0))
10761 return fold_build2_loc (loc, code, type,
10762 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10763 fold_build2_loc (loc,
10764 BIT_XOR_EXPR, itype,
10765 arg01, arg10),
10766 arg00),
10767 build_zero_cst (itype));
10769 if (operand_equal_p (arg00, arg10, 0))
10770 return fold_build2_loc (loc, code, type,
10771 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10772 fold_build2_loc (loc,
10773 BIT_XOR_EXPR, itype,
10774 arg01, arg11),
10775 arg00),
10776 build_zero_cst (itype));
10779 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10780 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10782 tree arg00 = TREE_OPERAND (arg0, 0);
10783 tree arg01 = TREE_OPERAND (arg0, 1);
10784 tree arg10 = TREE_OPERAND (arg1, 0);
10785 tree arg11 = TREE_OPERAND (arg1, 1);
10786 tree itype = TREE_TYPE (arg0);
10788 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10789 operand_equal_p guarantees no side-effects so we don't need
10790 to use omit_one_operand on Z. */
10791 if (operand_equal_p (arg01, arg11, 0))
10792 return fold_build2_loc (loc, code, type, arg00,
10793 fold_convert_loc (loc, TREE_TYPE (arg00),
10794 arg10));
10795 if (operand_equal_p (arg01, arg10, 0))
10796 return fold_build2_loc (loc, code, type, arg00,
10797 fold_convert_loc (loc, TREE_TYPE (arg00),
10798 arg11));
10799 if (operand_equal_p (arg00, arg11, 0))
10800 return fold_build2_loc (loc, code, type, arg01,
10801 fold_convert_loc (loc, TREE_TYPE (arg01),
10802 arg10));
10803 if (operand_equal_p (arg00, arg10, 0))
10804 return fold_build2_loc (loc, code, type, arg01,
10805 fold_convert_loc (loc, TREE_TYPE (arg01),
10806 arg11));
10808 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10809 if (TREE_CODE (arg01) == INTEGER_CST
10810 && TREE_CODE (arg11) == INTEGER_CST)
10812 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10813 fold_convert_loc (loc, itype, arg11));
10814 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10815 return fold_build2_loc (loc, code, type, tem,
10816 fold_convert_loc (loc, itype, arg10));
10820 /* Attempt to simplify equality/inequality comparisons of complex
10821 values. Only lower the comparison if the result is known or
10822 can be simplified to a single scalar comparison. */
10823 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10824 || TREE_CODE (arg0) == COMPLEX_CST)
10825 && (TREE_CODE (arg1) == COMPLEX_EXPR
10826 || TREE_CODE (arg1) == COMPLEX_CST))
10828 tree real0, imag0, real1, imag1;
10829 tree rcond, icond;
10831 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10833 real0 = TREE_OPERAND (arg0, 0);
10834 imag0 = TREE_OPERAND (arg0, 1);
10836 else
10838 real0 = TREE_REALPART (arg0);
10839 imag0 = TREE_IMAGPART (arg0);
10842 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10844 real1 = TREE_OPERAND (arg1, 0);
10845 imag1 = TREE_OPERAND (arg1, 1);
10847 else
10849 real1 = TREE_REALPART (arg1);
10850 imag1 = TREE_IMAGPART (arg1);
10853 rcond = fold_binary_loc (loc, code, type, real0, real1);
10854 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10856 if (integer_zerop (rcond))
10858 if (code == EQ_EXPR)
10859 return omit_two_operands_loc (loc, type, boolean_false_node,
10860 imag0, imag1);
10861 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10863 else
10865 if (code == NE_EXPR)
10866 return omit_two_operands_loc (loc, type, boolean_true_node,
10867 imag0, imag1);
10868 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10872 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10873 if (icond && TREE_CODE (icond) == INTEGER_CST)
10875 if (integer_zerop (icond))
10877 if (code == EQ_EXPR)
10878 return omit_two_operands_loc (loc, type, boolean_false_node,
10879 real0, real1);
10880 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10882 else
10884 if (code == NE_EXPR)
10885 return omit_two_operands_loc (loc, type, boolean_true_node,
10886 real0, real1);
10887 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10892 return NULL_TREE;
10894 case LT_EXPR:
10895 case GT_EXPR:
10896 case LE_EXPR:
10897 case GE_EXPR:
10898 tem = fold_comparison (loc, code, type, op0, op1);
10899 if (tem != NULL_TREE)
10900 return tem;
10902 /* Transform comparisons of the form X +- C CMP X. */
10903 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10904 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10905 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10906 && !HONOR_SNANS (arg0))
10907 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10908 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10910 tree arg01 = TREE_OPERAND (arg0, 1);
10911 enum tree_code code0 = TREE_CODE (arg0);
10912 int is_positive;
10914 if (TREE_CODE (arg01) == REAL_CST)
10915 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10916 else
10917 is_positive = tree_int_cst_sgn (arg01);
10919 /* (X - c) > X becomes false. */
10920 if (code == GT_EXPR
10921 && ((code0 == MINUS_EXPR && is_positive >= 0)
10922 || (code0 == PLUS_EXPR && is_positive <= 0)))
10924 if (TREE_CODE (arg01) == INTEGER_CST
10925 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10926 fold_overflow_warning (("assuming signed overflow does not "
10927 "occur when assuming that (X - c) > X "
10928 "is always false"),
10929 WARN_STRICT_OVERFLOW_ALL);
10930 return constant_boolean_node (0, type);
10933 /* Likewise (X + c) < X becomes false. */
10934 if (code == LT_EXPR
10935 && ((code0 == PLUS_EXPR && is_positive >= 0)
10936 || (code0 == MINUS_EXPR && is_positive <= 0)))
10938 if (TREE_CODE (arg01) == INTEGER_CST
10939 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10940 fold_overflow_warning (("assuming signed overflow does not "
10941 "occur when assuming that "
10942 "(X + c) < X is always false"),
10943 WARN_STRICT_OVERFLOW_ALL);
10944 return constant_boolean_node (0, type);
10947 /* Convert (X - c) <= X to true. */
10948 if (!HONOR_NANS (arg1)
10949 && code == LE_EXPR
10950 && ((code0 == MINUS_EXPR && is_positive >= 0)
10951 || (code0 == PLUS_EXPR && is_positive <= 0)))
10953 if (TREE_CODE (arg01) == INTEGER_CST
10954 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10955 fold_overflow_warning (("assuming signed overflow does not "
10956 "occur when assuming that "
10957 "(X - c) <= X is always true"),
10958 WARN_STRICT_OVERFLOW_ALL);
10959 return constant_boolean_node (1, type);
10962 /* Convert (X + c) >= X to true. */
10963 if (!HONOR_NANS (arg1)
10964 && code == GE_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 true"),
10973 WARN_STRICT_OVERFLOW_ALL);
10974 return constant_boolean_node (1, type);
10977 if (TREE_CODE (arg01) == INTEGER_CST)
10979 /* Convert X + c > X and X - c < X to true for integers. */
10980 if (code == GT_EXPR
10981 && ((code0 == PLUS_EXPR && is_positive > 0)
10982 || (code0 == MINUS_EXPR && is_positive < 0)))
10984 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10985 fold_overflow_warning (("assuming signed overflow does "
10986 "not occur when assuming that "
10987 "(X + c) > X is always true"),
10988 WARN_STRICT_OVERFLOW_ALL);
10989 return constant_boolean_node (1, type);
10992 if (code == LT_EXPR
10993 && ((code0 == MINUS_EXPR && is_positive > 0)
10994 || (code0 == PLUS_EXPR && is_positive < 0)))
10996 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10997 fold_overflow_warning (("assuming signed overflow does "
10998 "not occur when assuming that "
10999 "(X - c) < X is always true"),
11000 WARN_STRICT_OVERFLOW_ALL);
11001 return constant_boolean_node (1, type);
11004 /* Convert X + c <= X and X - c >= X to false for integers. */
11005 if (code == LE_EXPR
11006 && ((code0 == PLUS_EXPR && is_positive > 0)
11007 || (code0 == MINUS_EXPR && is_positive < 0)))
11009 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11010 fold_overflow_warning (("assuming signed overflow does "
11011 "not occur when assuming that "
11012 "(X + c) <= X is always false"),
11013 WARN_STRICT_OVERFLOW_ALL);
11014 return constant_boolean_node (0, type);
11017 if (code == GE_EXPR
11018 && ((code0 == MINUS_EXPR && is_positive > 0)
11019 || (code0 == PLUS_EXPR && is_positive < 0)))
11021 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11022 fold_overflow_warning (("assuming signed overflow does "
11023 "not occur when assuming that "
11024 "(X - c) >= X is always false"),
11025 WARN_STRICT_OVERFLOW_ALL);
11026 return constant_boolean_node (0, type);
11031 /* If we are comparing an ABS_EXPR with a constant, we can
11032 convert all the cases into explicit comparisons, but they may
11033 well not be faster than doing the ABS and one comparison.
11034 But ABS (X) <= C is a range comparison, which becomes a subtraction
11035 and a comparison, and is probably faster. */
11036 if (code == LE_EXPR
11037 && TREE_CODE (arg1) == INTEGER_CST
11038 && TREE_CODE (arg0) == ABS_EXPR
11039 && ! TREE_SIDE_EFFECTS (arg0)
11040 && (0 != (tem = negate_expr (arg1)))
11041 && TREE_CODE (tem) == INTEGER_CST
11042 && !TREE_OVERFLOW (tem))
11043 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11044 build2 (GE_EXPR, type,
11045 TREE_OPERAND (arg0, 0), tem),
11046 build2 (LE_EXPR, type,
11047 TREE_OPERAND (arg0, 0), arg1));
11049 /* Convert ABS_EXPR<x> >= 0 to true. */
11050 strict_overflow_p = false;
11051 if (code == GE_EXPR
11052 && (integer_zerop (arg1)
11053 || (! HONOR_NANS (arg0)
11054 && real_zerop (arg1)))
11055 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11057 if (strict_overflow_p)
11058 fold_overflow_warning (("assuming signed overflow does not occur "
11059 "when simplifying comparison of "
11060 "absolute value and zero"),
11061 WARN_STRICT_OVERFLOW_CONDITIONAL);
11062 return omit_one_operand_loc (loc, type,
11063 constant_boolean_node (true, type),
11064 arg0);
11067 /* Convert ABS_EXPR<x> < 0 to false. */
11068 strict_overflow_p = false;
11069 if (code == LT_EXPR
11070 && (integer_zerop (arg1) || real_zerop (arg1))
11071 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11073 if (strict_overflow_p)
11074 fold_overflow_warning (("assuming signed overflow does not occur "
11075 "when simplifying comparison of "
11076 "absolute value and zero"),
11077 WARN_STRICT_OVERFLOW_CONDITIONAL);
11078 return omit_one_operand_loc (loc, type,
11079 constant_boolean_node (false, type),
11080 arg0);
11083 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11084 and similarly for >= into !=. */
11085 if ((code == LT_EXPR || code == GE_EXPR)
11086 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11087 && TREE_CODE (arg1) == LSHIFT_EXPR
11088 && integer_onep (TREE_OPERAND (arg1, 0)))
11089 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11090 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11091 TREE_OPERAND (arg1, 1)),
11092 build_zero_cst (TREE_TYPE (arg0)));
11094 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11095 otherwise Y might be >= # of bits in X's type and thus e.g.
11096 (unsigned char) (1 << Y) for Y 15 might be 0.
11097 If the cast is widening, then 1 << Y should have unsigned type,
11098 otherwise if Y is number of bits in the signed shift type minus 1,
11099 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11100 31 might be 0xffffffff80000000. */
11101 if ((code == LT_EXPR || code == GE_EXPR)
11102 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11103 && CONVERT_EXPR_P (arg1)
11104 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11105 && (element_precision (TREE_TYPE (arg1))
11106 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11107 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11108 || (element_precision (TREE_TYPE (arg1))
11109 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11110 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11112 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11113 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11114 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11115 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11116 build_zero_cst (TREE_TYPE (arg0)));
11119 return NULL_TREE;
11121 case UNORDERED_EXPR:
11122 case ORDERED_EXPR:
11123 case UNLT_EXPR:
11124 case UNLE_EXPR:
11125 case UNGT_EXPR:
11126 case UNGE_EXPR:
11127 case UNEQ_EXPR:
11128 case LTGT_EXPR:
11129 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11131 tree targ0 = strip_float_extensions (arg0);
11132 tree targ1 = strip_float_extensions (arg1);
11133 tree newtype = TREE_TYPE (targ0);
11135 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11136 newtype = TREE_TYPE (targ1);
11138 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11139 return fold_build2_loc (loc, code, type,
11140 fold_convert_loc (loc, newtype, targ0),
11141 fold_convert_loc (loc, newtype, targ1));
11144 return NULL_TREE;
11146 case COMPOUND_EXPR:
11147 /* When pedantic, a compound expression can be neither an lvalue
11148 nor an integer constant expression. */
11149 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11150 return NULL_TREE;
11151 /* Don't let (0, 0) be null pointer constant. */
11152 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11153 : fold_convert_loc (loc, type, arg1);
11154 return pedantic_non_lvalue_loc (loc, tem);
11156 case ASSERT_EXPR:
11157 /* An ASSERT_EXPR should never be passed to fold_binary. */
11158 gcc_unreachable ();
11160 default:
11161 return NULL_TREE;
11162 } /* switch (code) */
11165 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11166 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11167 of GOTO_EXPR. */
11169 static tree
11170 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11172 switch (TREE_CODE (*tp))
11174 case LABEL_EXPR:
11175 return *tp;
11177 case GOTO_EXPR:
11178 *walk_subtrees = 0;
11180 /* fall through */
11182 default:
11183 return NULL_TREE;
11187 /* Return whether the sub-tree ST contains a label which is accessible from
11188 outside the sub-tree. */
11190 static bool
11191 contains_label_p (tree st)
11193 return
11194 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11197 /* Fold a ternary expression of code CODE and type TYPE with operands
11198 OP0, OP1, and OP2. Return the folded expression if folding is
11199 successful. Otherwise, return NULL_TREE. */
11201 tree
11202 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11203 tree op0, tree op1, tree op2)
11205 tree tem;
11206 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11207 enum tree_code_class kind = TREE_CODE_CLASS (code);
11209 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11210 && TREE_CODE_LENGTH (code) == 3);
11212 /* If this is a commutative operation, and OP0 is a constant, move it
11213 to OP1 to reduce the number of tests below. */
11214 if (commutative_ternary_tree_code (code)
11215 && tree_swap_operands_p (op0, op1))
11216 return fold_build3_loc (loc, code, type, op1, op0, op2);
11218 tem = generic_simplify (loc, code, type, op0, op1, op2);
11219 if (tem)
11220 return tem;
11222 /* Strip any conversions that don't change the mode. This is safe
11223 for every expression, except for a comparison expression because
11224 its signedness is derived from its operands. So, in the latter
11225 case, only strip conversions that don't change the signedness.
11227 Note that this is done as an internal manipulation within the
11228 constant folder, in order to find the simplest representation of
11229 the arguments so that their form can be studied. In any cases,
11230 the appropriate type conversions should be put back in the tree
11231 that will get out of the constant folder. */
11232 if (op0)
11234 arg0 = op0;
11235 STRIP_NOPS (arg0);
11238 if (op1)
11240 arg1 = op1;
11241 STRIP_NOPS (arg1);
11244 if (op2)
11246 arg2 = op2;
11247 STRIP_NOPS (arg2);
11250 switch (code)
11252 case COMPONENT_REF:
11253 if (TREE_CODE (arg0) == CONSTRUCTOR
11254 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11256 unsigned HOST_WIDE_INT idx;
11257 tree field, value;
11258 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11259 if (field == arg1)
11260 return value;
11262 return NULL_TREE;
11264 case COND_EXPR:
11265 case VEC_COND_EXPR:
11266 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11267 so all simple results must be passed through pedantic_non_lvalue. */
11268 if (TREE_CODE (arg0) == INTEGER_CST)
11270 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11271 tem = integer_zerop (arg0) ? op2 : op1;
11272 /* Only optimize constant conditions when the selected branch
11273 has the same type as the COND_EXPR. This avoids optimizing
11274 away "c ? x : throw", where the throw has a void type.
11275 Avoid throwing away that operand which contains label. */
11276 if ((!TREE_SIDE_EFFECTS (unused_op)
11277 || !contains_label_p (unused_op))
11278 && (! VOID_TYPE_P (TREE_TYPE (tem))
11279 || VOID_TYPE_P (type)))
11280 return pedantic_non_lvalue_loc (loc, tem);
11281 return NULL_TREE;
11283 else if (TREE_CODE (arg0) == VECTOR_CST)
11285 if ((TREE_CODE (arg1) == VECTOR_CST
11286 || TREE_CODE (arg1) == CONSTRUCTOR)
11287 && (TREE_CODE (arg2) == VECTOR_CST
11288 || TREE_CODE (arg2) == CONSTRUCTOR))
11290 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11291 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11292 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11293 for (i = 0; i < nelts; i++)
11295 tree val = VECTOR_CST_ELT (arg0, i);
11296 if (integer_all_onesp (val))
11297 sel[i] = i;
11298 else if (integer_zerop (val))
11299 sel[i] = nelts + i;
11300 else /* Currently unreachable. */
11301 return NULL_TREE;
11303 tree t = fold_vec_perm (type, arg1, arg2, sel);
11304 if (t != NULL_TREE)
11305 return t;
11309 /* If we have A op B ? A : C, we may be able to convert this to a
11310 simpler expression, depending on the operation and the values
11311 of B and C. Signed zeros prevent all of these transformations,
11312 for reasons given above each one.
11314 Also try swapping the arguments and inverting the conditional. */
11315 if (COMPARISON_CLASS_P (arg0)
11316 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11317 arg1, TREE_OPERAND (arg0, 1))
11318 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11320 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11321 if (tem)
11322 return tem;
11325 if (COMPARISON_CLASS_P (arg0)
11326 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11327 op2,
11328 TREE_OPERAND (arg0, 1))
11329 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11331 location_t loc0 = expr_location_or (arg0, loc);
11332 tem = fold_invert_truthvalue (loc0, arg0);
11333 if (tem && COMPARISON_CLASS_P (tem))
11335 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11336 if (tem)
11337 return tem;
11341 /* If the second operand is simpler than the third, swap them
11342 since that produces better jump optimization results. */
11343 if (truth_value_p (TREE_CODE (arg0))
11344 && tree_swap_operands_p (op1, op2))
11346 location_t loc0 = expr_location_or (arg0, loc);
11347 /* See if this can be inverted. If it can't, possibly because
11348 it was a floating-point inequality comparison, don't do
11349 anything. */
11350 tem = fold_invert_truthvalue (loc0, arg0);
11351 if (tem)
11352 return fold_build3_loc (loc, code, type, tem, op2, op1);
11355 /* Convert A ? 1 : 0 to simply A. */
11356 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11357 : (integer_onep (op1)
11358 && !VECTOR_TYPE_P (type)))
11359 && integer_zerop (op2)
11360 /* If we try to convert OP0 to our type, the
11361 call to fold will try to move the conversion inside
11362 a COND, which will recurse. In that case, the COND_EXPR
11363 is probably the best choice, so leave it alone. */
11364 && type == TREE_TYPE (arg0))
11365 return pedantic_non_lvalue_loc (loc, arg0);
11367 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11368 over COND_EXPR in cases such as floating point comparisons. */
11369 if (integer_zerop (op1)
11370 && code == COND_EXPR
11371 && integer_onep (op2)
11372 && !VECTOR_TYPE_P (type)
11373 && truth_value_p (TREE_CODE (arg0)))
11374 return pedantic_non_lvalue_loc (loc,
11375 fold_convert_loc (loc, type,
11376 invert_truthvalue_loc (loc,
11377 arg0)));
11379 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11380 if (TREE_CODE (arg0) == LT_EXPR
11381 && integer_zerop (TREE_OPERAND (arg0, 1))
11382 && integer_zerop (op2)
11383 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11385 /* sign_bit_p looks through both zero and sign extensions,
11386 but for this optimization only sign extensions are
11387 usable. */
11388 tree tem2 = TREE_OPERAND (arg0, 0);
11389 while (tem != tem2)
11391 if (TREE_CODE (tem2) != NOP_EXPR
11392 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11394 tem = NULL_TREE;
11395 break;
11397 tem2 = TREE_OPERAND (tem2, 0);
11399 /* sign_bit_p only checks ARG1 bits within A's precision.
11400 If <sign bit of A> has wider type than A, bits outside
11401 of A's precision in <sign bit of A> need to be checked.
11402 If they are all 0, this optimization needs to be done
11403 in unsigned A's type, if they are all 1 in signed A's type,
11404 otherwise this can't be done. */
11405 if (tem
11406 && TYPE_PRECISION (TREE_TYPE (tem))
11407 < TYPE_PRECISION (TREE_TYPE (arg1))
11408 && TYPE_PRECISION (TREE_TYPE (tem))
11409 < TYPE_PRECISION (type))
11411 int inner_width, outer_width;
11412 tree tem_type;
11414 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11415 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11416 if (outer_width > TYPE_PRECISION (type))
11417 outer_width = TYPE_PRECISION (type);
11419 wide_int mask = wi::shifted_mask
11420 (inner_width, outer_width - inner_width, false,
11421 TYPE_PRECISION (TREE_TYPE (arg1)));
11423 wide_int common = mask & arg1;
11424 if (common == mask)
11426 tem_type = signed_type_for (TREE_TYPE (tem));
11427 tem = fold_convert_loc (loc, tem_type, tem);
11429 else if (common == 0)
11431 tem_type = unsigned_type_for (TREE_TYPE (tem));
11432 tem = fold_convert_loc (loc, tem_type, tem);
11434 else
11435 tem = NULL;
11438 if (tem)
11439 return
11440 fold_convert_loc (loc, type,
11441 fold_build2_loc (loc, BIT_AND_EXPR,
11442 TREE_TYPE (tem), tem,
11443 fold_convert_loc (loc,
11444 TREE_TYPE (tem),
11445 arg1)));
11448 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11449 already handled above. */
11450 if (TREE_CODE (arg0) == BIT_AND_EXPR
11451 && integer_onep (TREE_OPERAND (arg0, 1))
11452 && integer_zerop (op2)
11453 && integer_pow2p (arg1))
11455 tree tem = TREE_OPERAND (arg0, 0);
11456 STRIP_NOPS (tem);
11457 if (TREE_CODE (tem) == RSHIFT_EXPR
11458 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11459 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11460 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11461 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11462 TREE_OPERAND (tem, 0), arg1);
11465 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11466 is probably obsolete because the first operand should be a
11467 truth value (that's why we have the two cases above), but let's
11468 leave it in until we can confirm this for all front-ends. */
11469 if (integer_zerop (op2)
11470 && TREE_CODE (arg0) == NE_EXPR
11471 && integer_zerop (TREE_OPERAND (arg0, 1))
11472 && integer_pow2p (arg1)
11473 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11474 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11475 arg1, OEP_ONLY_CONST))
11476 return pedantic_non_lvalue_loc (loc,
11477 fold_convert_loc (loc, type,
11478 TREE_OPERAND (arg0, 0)));
11480 /* Disable the transformations below for vectors, since
11481 fold_binary_op_with_conditional_arg may undo them immediately,
11482 yielding an infinite loop. */
11483 if (code == VEC_COND_EXPR)
11484 return NULL_TREE;
11486 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11487 if (integer_zerop (op2)
11488 && truth_value_p (TREE_CODE (arg0))
11489 && truth_value_p (TREE_CODE (arg1))
11490 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11491 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11492 : TRUTH_ANDIF_EXPR,
11493 type, fold_convert_loc (loc, type, arg0), arg1);
11495 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11496 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11497 && truth_value_p (TREE_CODE (arg0))
11498 && truth_value_p (TREE_CODE (arg1))
11499 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11501 location_t loc0 = expr_location_or (arg0, loc);
11502 /* Only perform transformation if ARG0 is easily inverted. */
11503 tem = fold_invert_truthvalue (loc0, arg0);
11504 if (tem)
11505 return fold_build2_loc (loc, code == VEC_COND_EXPR
11506 ? BIT_IOR_EXPR
11507 : TRUTH_ORIF_EXPR,
11508 type, fold_convert_loc (loc, type, tem),
11509 arg1);
11512 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11513 if (integer_zerop (arg1)
11514 && truth_value_p (TREE_CODE (arg0))
11515 && truth_value_p (TREE_CODE (op2))
11516 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11518 location_t loc0 = expr_location_or (arg0, loc);
11519 /* Only perform transformation if ARG0 is easily inverted. */
11520 tem = fold_invert_truthvalue (loc0, arg0);
11521 if (tem)
11522 return fold_build2_loc (loc, code == VEC_COND_EXPR
11523 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11524 type, fold_convert_loc (loc, type, tem),
11525 op2);
11528 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11529 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11530 && truth_value_p (TREE_CODE (arg0))
11531 && truth_value_p (TREE_CODE (op2))
11532 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11533 return fold_build2_loc (loc, code == VEC_COND_EXPR
11534 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11535 type, fold_convert_loc (loc, type, arg0), op2);
11537 return NULL_TREE;
11539 case CALL_EXPR:
11540 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11541 of fold_ternary on them. */
11542 gcc_unreachable ();
11544 case BIT_FIELD_REF:
11545 if (TREE_CODE (arg0) == VECTOR_CST
11546 && (type == TREE_TYPE (TREE_TYPE (arg0))
11547 || (TREE_CODE (type) == VECTOR_TYPE
11548 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11550 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11551 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11552 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11553 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11555 if (n != 0
11556 && (idx % width) == 0
11557 && (n % width) == 0
11558 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11560 idx = idx / width;
11561 n = n / width;
11563 if (TREE_CODE (arg0) == VECTOR_CST)
11565 if (n == 1)
11566 return VECTOR_CST_ELT (arg0, idx);
11568 tree *vals = XALLOCAVEC (tree, n);
11569 for (unsigned i = 0; i < n; ++i)
11570 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11571 return build_vector (type, vals);
11576 /* On constants we can use native encode/interpret to constant
11577 fold (nearly) all BIT_FIELD_REFs. */
11578 if (CONSTANT_CLASS_P (arg0)
11579 && can_native_interpret_type_p (type)
11580 && BITS_PER_UNIT == 8)
11582 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11583 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11584 /* Limit us to a reasonable amount of work. To relax the
11585 other limitations we need bit-shifting of the buffer
11586 and rounding up the size. */
11587 if (bitpos % BITS_PER_UNIT == 0
11588 && bitsize % BITS_PER_UNIT == 0
11589 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11591 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11592 unsigned HOST_WIDE_INT len
11593 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11594 bitpos / BITS_PER_UNIT);
11595 if (len > 0
11596 && len * BITS_PER_UNIT >= bitsize)
11598 tree v = native_interpret_expr (type, b,
11599 bitsize / BITS_PER_UNIT);
11600 if (v)
11601 return v;
11606 return NULL_TREE;
11608 case FMA_EXPR:
11609 /* For integers we can decompose the FMA if possible. */
11610 if (TREE_CODE (arg0) == INTEGER_CST
11611 && TREE_CODE (arg1) == INTEGER_CST)
11612 return fold_build2_loc (loc, PLUS_EXPR, type,
11613 const_binop (MULT_EXPR, arg0, arg1), arg2);
11614 if (integer_zerop (arg2))
11615 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11617 return fold_fma (loc, type, arg0, arg1, arg2);
11619 case VEC_PERM_EXPR:
11620 if (TREE_CODE (arg2) == VECTOR_CST)
11622 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11623 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11624 unsigned char *sel2 = sel + nelts;
11625 bool need_mask_canon = false;
11626 bool need_mask_canon2 = false;
11627 bool all_in_vec0 = true;
11628 bool all_in_vec1 = true;
11629 bool maybe_identity = true;
11630 bool single_arg = (op0 == op1);
11631 bool changed = false;
11633 mask2 = 2 * nelts - 1;
11634 mask = single_arg ? (nelts - 1) : mask2;
11635 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11636 for (i = 0; i < nelts; i++)
11638 tree val = VECTOR_CST_ELT (arg2, i);
11639 if (TREE_CODE (val) != INTEGER_CST)
11640 return NULL_TREE;
11642 /* Make sure that the perm value is in an acceptable
11643 range. */
11644 wide_int t = val;
11645 need_mask_canon |= wi::gtu_p (t, mask);
11646 need_mask_canon2 |= wi::gtu_p (t, mask2);
11647 sel[i] = t.to_uhwi () & mask;
11648 sel2[i] = t.to_uhwi () & mask2;
11650 if (sel[i] < nelts)
11651 all_in_vec1 = false;
11652 else
11653 all_in_vec0 = false;
11655 if ((sel[i] & (nelts-1)) != i)
11656 maybe_identity = false;
11659 if (maybe_identity)
11661 if (all_in_vec0)
11662 return op0;
11663 if (all_in_vec1)
11664 return op1;
11667 if (all_in_vec0)
11668 op1 = op0;
11669 else if (all_in_vec1)
11671 op0 = op1;
11672 for (i = 0; i < nelts; i++)
11673 sel[i] -= nelts;
11674 need_mask_canon = true;
11677 if ((TREE_CODE (op0) == VECTOR_CST
11678 || TREE_CODE (op0) == CONSTRUCTOR)
11679 && (TREE_CODE (op1) == VECTOR_CST
11680 || TREE_CODE (op1) == CONSTRUCTOR))
11682 tree t = fold_vec_perm (type, op0, op1, sel);
11683 if (t != NULL_TREE)
11684 return t;
11687 if (op0 == op1 && !single_arg)
11688 changed = true;
11690 /* Some targets are deficient and fail to expand a single
11691 argument permutation while still allowing an equivalent
11692 2-argument version. */
11693 if (need_mask_canon && arg2 == op2
11694 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11695 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11697 need_mask_canon = need_mask_canon2;
11698 sel = sel2;
11701 if (need_mask_canon && arg2 == op2)
11703 tree *tsel = XALLOCAVEC (tree, nelts);
11704 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11705 for (i = 0; i < nelts; i++)
11706 tsel[i] = build_int_cst (eltype, sel[i]);
11707 op2 = build_vector (TREE_TYPE (arg2), tsel);
11708 changed = true;
11711 if (changed)
11712 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11714 return NULL_TREE;
11716 case BIT_INSERT_EXPR:
11717 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11718 if (TREE_CODE (arg0) == INTEGER_CST
11719 && TREE_CODE (arg1) == INTEGER_CST)
11721 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11722 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11723 wide_int tem = wi::bit_and (arg0,
11724 wi::shifted_mask (bitpos, bitsize, true,
11725 TYPE_PRECISION (type)));
11726 wide_int tem2
11727 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11728 bitsize), bitpos);
11729 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11731 else if (TREE_CODE (arg0) == VECTOR_CST
11732 && CONSTANT_CLASS_P (arg1)
11733 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11734 TREE_TYPE (arg1)))
11736 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11737 unsigned HOST_WIDE_INT elsize
11738 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11739 if (bitpos % elsize == 0)
11741 unsigned k = bitpos / elsize;
11742 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11743 return arg0;
11744 else
11746 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11747 memcpy (elts, VECTOR_CST_ELTS (arg0),
11748 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11749 elts[k] = arg1;
11750 return build_vector (type, elts);
11754 return NULL_TREE;
11756 default:
11757 return NULL_TREE;
11758 } /* switch (code) */
11761 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11762 of an array (or vector). */
11764 tree
11765 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11767 tree index_type = NULL_TREE;
11768 offset_int low_bound = 0;
11770 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11772 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11773 if (domain_type && TYPE_MIN_VALUE (domain_type))
11775 /* Static constructors for variably sized objects makes no sense. */
11776 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11777 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11778 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11782 if (index_type)
11783 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11784 TYPE_SIGN (index_type));
11786 offset_int index = low_bound - 1;
11787 if (index_type)
11788 index = wi::ext (index, TYPE_PRECISION (index_type),
11789 TYPE_SIGN (index_type));
11791 offset_int max_index;
11792 unsigned HOST_WIDE_INT cnt;
11793 tree cfield, cval;
11795 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11797 /* Array constructor might explicitly set index, or specify a range,
11798 or leave index NULL meaning that it is next index after previous
11799 one. */
11800 if (cfield)
11802 if (TREE_CODE (cfield) == INTEGER_CST)
11803 max_index = index = wi::to_offset (cfield);
11804 else
11806 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11807 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11808 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11811 else
11813 index += 1;
11814 if (index_type)
11815 index = wi::ext (index, TYPE_PRECISION (index_type),
11816 TYPE_SIGN (index_type));
11817 max_index = index;
11820 /* Do we have match? */
11821 if (wi::cmpu (access_index, index) >= 0
11822 && wi::cmpu (access_index, max_index) <= 0)
11823 return cval;
11825 return NULL_TREE;
11828 /* Perform constant folding and related simplification of EXPR.
11829 The related simplifications include x*1 => x, x*0 => 0, etc.,
11830 and application of the associative law.
11831 NOP_EXPR conversions may be removed freely (as long as we
11832 are careful not to change the type of the overall expression).
11833 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11834 but we can constant-fold them if they have constant operands. */
11836 #ifdef ENABLE_FOLD_CHECKING
11837 # define fold(x) fold_1 (x)
11838 static tree fold_1 (tree);
11839 static
11840 #endif
11841 tree
11842 fold (tree expr)
11844 const tree t = expr;
11845 enum tree_code code = TREE_CODE (t);
11846 enum tree_code_class kind = TREE_CODE_CLASS (code);
11847 tree tem;
11848 location_t loc = EXPR_LOCATION (expr);
11850 /* Return right away if a constant. */
11851 if (kind == tcc_constant)
11852 return t;
11854 /* CALL_EXPR-like objects with variable numbers of operands are
11855 treated specially. */
11856 if (kind == tcc_vl_exp)
11858 if (code == CALL_EXPR)
11860 tem = fold_call_expr (loc, expr, false);
11861 return tem ? tem : expr;
11863 return expr;
11866 if (IS_EXPR_CODE_CLASS (kind))
11868 tree type = TREE_TYPE (t);
11869 tree op0, op1, op2;
11871 switch (TREE_CODE_LENGTH (code))
11873 case 1:
11874 op0 = TREE_OPERAND (t, 0);
11875 tem = fold_unary_loc (loc, code, type, op0);
11876 return tem ? tem : expr;
11877 case 2:
11878 op0 = TREE_OPERAND (t, 0);
11879 op1 = TREE_OPERAND (t, 1);
11880 tem = fold_binary_loc (loc, code, type, op0, op1);
11881 return tem ? tem : expr;
11882 case 3:
11883 op0 = TREE_OPERAND (t, 0);
11884 op1 = TREE_OPERAND (t, 1);
11885 op2 = TREE_OPERAND (t, 2);
11886 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11887 return tem ? tem : expr;
11888 default:
11889 break;
11893 switch (code)
11895 case ARRAY_REF:
11897 tree op0 = TREE_OPERAND (t, 0);
11898 tree op1 = TREE_OPERAND (t, 1);
11900 if (TREE_CODE (op1) == INTEGER_CST
11901 && TREE_CODE (op0) == CONSTRUCTOR
11902 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11904 tree val = get_array_ctor_element_at_index (op0,
11905 wi::to_offset (op1));
11906 if (val)
11907 return val;
11910 return t;
11913 /* Return a VECTOR_CST if possible. */
11914 case CONSTRUCTOR:
11916 tree type = TREE_TYPE (t);
11917 if (TREE_CODE (type) != VECTOR_TYPE)
11918 return t;
11920 unsigned i;
11921 tree val;
11922 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11923 if (! CONSTANT_CLASS_P (val))
11924 return t;
11926 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11929 case CONST_DECL:
11930 return fold (DECL_INITIAL (t));
11932 default:
11933 return t;
11934 } /* switch (code) */
11937 #ifdef ENABLE_FOLD_CHECKING
11938 #undef fold
11940 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11941 hash_table<nofree_ptr_hash<const tree_node> > *);
11942 static void fold_check_failed (const_tree, const_tree);
11943 void print_fold_checksum (const_tree);
11945 /* When --enable-checking=fold, compute a digest of expr before
11946 and after actual fold call to see if fold did not accidentally
11947 change original expr. */
11949 tree
11950 fold (tree expr)
11952 tree ret;
11953 struct md5_ctx ctx;
11954 unsigned char checksum_before[16], checksum_after[16];
11955 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11957 md5_init_ctx (&ctx);
11958 fold_checksum_tree (expr, &ctx, &ht);
11959 md5_finish_ctx (&ctx, checksum_before);
11960 ht.empty ();
11962 ret = fold_1 (expr);
11964 md5_init_ctx (&ctx);
11965 fold_checksum_tree (expr, &ctx, &ht);
11966 md5_finish_ctx (&ctx, checksum_after);
11968 if (memcmp (checksum_before, checksum_after, 16))
11969 fold_check_failed (expr, ret);
11971 return ret;
11974 void
11975 print_fold_checksum (const_tree expr)
11977 struct md5_ctx ctx;
11978 unsigned char checksum[16], cnt;
11979 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11981 md5_init_ctx (&ctx);
11982 fold_checksum_tree (expr, &ctx, &ht);
11983 md5_finish_ctx (&ctx, checksum);
11984 for (cnt = 0; cnt < 16; ++cnt)
11985 fprintf (stderr, "%02x", checksum[cnt]);
11986 putc ('\n', stderr);
11989 static void
11990 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
11992 internal_error ("fold check: original tree changed by fold");
11995 static void
11996 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
11997 hash_table<nofree_ptr_hash <const tree_node> > *ht)
11999 const tree_node **slot;
12000 enum tree_code code;
12001 union tree_node buf;
12002 int i, len;
12004 recursive_label:
12005 if (expr == NULL)
12006 return;
12007 slot = ht->find_slot (expr, INSERT);
12008 if (*slot != NULL)
12009 return;
12010 *slot = expr;
12011 code = TREE_CODE (expr);
12012 if (TREE_CODE_CLASS (code) == tcc_declaration
12013 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12015 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12016 memcpy ((char *) &buf, expr, tree_size (expr));
12017 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12018 buf.decl_with_vis.symtab_node = NULL;
12019 expr = (tree) &buf;
12021 else if (TREE_CODE_CLASS (code) == tcc_type
12022 && (TYPE_POINTER_TO (expr)
12023 || TYPE_REFERENCE_TO (expr)
12024 || TYPE_CACHED_VALUES_P (expr)
12025 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12026 || TYPE_NEXT_VARIANT (expr)
12027 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12029 /* Allow these fields to be modified. */
12030 tree tmp;
12031 memcpy ((char *) &buf, expr, tree_size (expr));
12032 expr = tmp = (tree) &buf;
12033 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12034 TYPE_POINTER_TO (tmp) = NULL;
12035 TYPE_REFERENCE_TO (tmp) = NULL;
12036 TYPE_NEXT_VARIANT (tmp) = NULL;
12037 TYPE_ALIAS_SET (tmp) = -1;
12038 if (TYPE_CACHED_VALUES_P (tmp))
12040 TYPE_CACHED_VALUES_P (tmp) = 0;
12041 TYPE_CACHED_VALUES (tmp) = NULL;
12044 md5_process_bytes (expr, tree_size (expr), ctx);
12045 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12046 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12047 if (TREE_CODE_CLASS (code) != tcc_type
12048 && TREE_CODE_CLASS (code) != tcc_declaration
12049 && code != TREE_LIST
12050 && code != SSA_NAME
12051 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12052 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12053 switch (TREE_CODE_CLASS (code))
12055 case tcc_constant:
12056 switch (code)
12058 case STRING_CST:
12059 md5_process_bytes (TREE_STRING_POINTER (expr),
12060 TREE_STRING_LENGTH (expr), ctx);
12061 break;
12062 case COMPLEX_CST:
12063 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12064 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12065 break;
12066 case VECTOR_CST:
12067 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12068 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12069 break;
12070 default:
12071 break;
12073 break;
12074 case tcc_exceptional:
12075 switch (code)
12077 case TREE_LIST:
12078 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12079 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12080 expr = TREE_CHAIN (expr);
12081 goto recursive_label;
12082 break;
12083 case TREE_VEC:
12084 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12085 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12086 break;
12087 default:
12088 break;
12090 break;
12091 case tcc_expression:
12092 case tcc_reference:
12093 case tcc_comparison:
12094 case tcc_unary:
12095 case tcc_binary:
12096 case tcc_statement:
12097 case tcc_vl_exp:
12098 len = TREE_OPERAND_LENGTH (expr);
12099 for (i = 0; i < len; ++i)
12100 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12101 break;
12102 case tcc_declaration:
12103 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12104 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12105 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12107 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12108 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12109 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12110 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12111 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12114 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12116 if (TREE_CODE (expr) == FUNCTION_DECL)
12118 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12119 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12121 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12123 break;
12124 case tcc_type:
12125 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12126 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12127 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12128 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12129 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12130 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12131 if (INTEGRAL_TYPE_P (expr)
12132 || SCALAR_FLOAT_TYPE_P (expr))
12134 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12135 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12137 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12138 if (TREE_CODE (expr) == RECORD_TYPE
12139 || TREE_CODE (expr) == UNION_TYPE
12140 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12141 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12142 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12143 break;
12144 default:
12145 break;
12149 /* Helper function for outputting the checksum of a tree T. When
12150 debugging with gdb, you can "define mynext" to be "next" followed
12151 by "call debug_fold_checksum (op0)", then just trace down till the
12152 outputs differ. */
12154 DEBUG_FUNCTION void
12155 debug_fold_checksum (const_tree t)
12157 int i;
12158 unsigned char checksum[16];
12159 struct md5_ctx ctx;
12160 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12162 md5_init_ctx (&ctx);
12163 fold_checksum_tree (t, &ctx, &ht);
12164 md5_finish_ctx (&ctx, checksum);
12165 ht.empty ();
12167 for (i = 0; i < 16; i++)
12168 fprintf (stderr, "%d ", checksum[i]);
12170 fprintf (stderr, "\n");
12173 #endif
12175 /* Fold a unary tree expression with code CODE of type TYPE with an
12176 operand OP0. LOC is the location of the resulting expression.
12177 Return a folded expression if successful. Otherwise, return a tree
12178 expression with code CODE of type TYPE with an operand OP0. */
12180 tree
12181 fold_build1_stat_loc (location_t loc,
12182 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12184 tree tem;
12185 #ifdef ENABLE_FOLD_CHECKING
12186 unsigned char checksum_before[16], checksum_after[16];
12187 struct md5_ctx ctx;
12188 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12190 md5_init_ctx (&ctx);
12191 fold_checksum_tree (op0, &ctx, &ht);
12192 md5_finish_ctx (&ctx, checksum_before);
12193 ht.empty ();
12194 #endif
12196 tem = fold_unary_loc (loc, code, type, op0);
12197 if (!tem)
12198 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12200 #ifdef ENABLE_FOLD_CHECKING
12201 md5_init_ctx (&ctx);
12202 fold_checksum_tree (op0, &ctx, &ht);
12203 md5_finish_ctx (&ctx, checksum_after);
12205 if (memcmp (checksum_before, checksum_after, 16))
12206 fold_check_failed (op0, tem);
12207 #endif
12208 return tem;
12211 /* Fold a binary tree expression with code CODE of type TYPE with
12212 operands OP0 and OP1. LOC is the location of the resulting
12213 expression. Return a folded expression if successful. Otherwise,
12214 return a tree expression with code CODE of type TYPE with operands
12215 OP0 and OP1. */
12217 tree
12218 fold_build2_stat_loc (location_t loc,
12219 enum tree_code code, tree type, tree op0, tree op1
12220 MEM_STAT_DECL)
12222 tree tem;
12223 #ifdef ENABLE_FOLD_CHECKING
12224 unsigned char checksum_before_op0[16],
12225 checksum_before_op1[16],
12226 checksum_after_op0[16],
12227 checksum_after_op1[16];
12228 struct md5_ctx ctx;
12229 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12231 md5_init_ctx (&ctx);
12232 fold_checksum_tree (op0, &ctx, &ht);
12233 md5_finish_ctx (&ctx, checksum_before_op0);
12234 ht.empty ();
12236 md5_init_ctx (&ctx);
12237 fold_checksum_tree (op1, &ctx, &ht);
12238 md5_finish_ctx (&ctx, checksum_before_op1);
12239 ht.empty ();
12240 #endif
12242 tem = fold_binary_loc (loc, code, type, op0, op1);
12243 if (!tem)
12244 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12246 #ifdef ENABLE_FOLD_CHECKING
12247 md5_init_ctx (&ctx);
12248 fold_checksum_tree (op0, &ctx, &ht);
12249 md5_finish_ctx (&ctx, checksum_after_op0);
12250 ht.empty ();
12252 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12253 fold_check_failed (op0, tem);
12255 md5_init_ctx (&ctx);
12256 fold_checksum_tree (op1, &ctx, &ht);
12257 md5_finish_ctx (&ctx, checksum_after_op1);
12259 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12260 fold_check_failed (op1, tem);
12261 #endif
12262 return tem;
12265 /* Fold a ternary tree expression with code CODE of type TYPE with
12266 operands OP0, OP1, and OP2. Return a folded expression if
12267 successful. Otherwise, return a tree expression with code CODE of
12268 type TYPE with operands OP0, OP1, and OP2. */
12270 tree
12271 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12272 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12274 tree tem;
12275 #ifdef ENABLE_FOLD_CHECKING
12276 unsigned char checksum_before_op0[16],
12277 checksum_before_op1[16],
12278 checksum_before_op2[16],
12279 checksum_after_op0[16],
12280 checksum_after_op1[16],
12281 checksum_after_op2[16];
12282 struct md5_ctx ctx;
12283 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12285 md5_init_ctx (&ctx);
12286 fold_checksum_tree (op0, &ctx, &ht);
12287 md5_finish_ctx (&ctx, checksum_before_op0);
12288 ht.empty ();
12290 md5_init_ctx (&ctx);
12291 fold_checksum_tree (op1, &ctx, &ht);
12292 md5_finish_ctx (&ctx, checksum_before_op1);
12293 ht.empty ();
12295 md5_init_ctx (&ctx);
12296 fold_checksum_tree (op2, &ctx, &ht);
12297 md5_finish_ctx (&ctx, checksum_before_op2);
12298 ht.empty ();
12299 #endif
12301 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12302 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12303 if (!tem)
12304 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12306 #ifdef ENABLE_FOLD_CHECKING
12307 md5_init_ctx (&ctx);
12308 fold_checksum_tree (op0, &ctx, &ht);
12309 md5_finish_ctx (&ctx, checksum_after_op0);
12310 ht.empty ();
12312 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12313 fold_check_failed (op0, tem);
12315 md5_init_ctx (&ctx);
12316 fold_checksum_tree (op1, &ctx, &ht);
12317 md5_finish_ctx (&ctx, checksum_after_op1);
12318 ht.empty ();
12320 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12321 fold_check_failed (op1, tem);
12323 md5_init_ctx (&ctx);
12324 fold_checksum_tree (op2, &ctx, &ht);
12325 md5_finish_ctx (&ctx, checksum_after_op2);
12327 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12328 fold_check_failed (op2, tem);
12329 #endif
12330 return tem;
12333 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12334 arguments in ARGARRAY, and a null static chain.
12335 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12336 of type TYPE from the given operands as constructed by build_call_array. */
12338 tree
12339 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12340 int nargs, tree *argarray)
12342 tree tem;
12343 #ifdef ENABLE_FOLD_CHECKING
12344 unsigned char checksum_before_fn[16],
12345 checksum_before_arglist[16],
12346 checksum_after_fn[16],
12347 checksum_after_arglist[16];
12348 struct md5_ctx ctx;
12349 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12350 int i;
12352 md5_init_ctx (&ctx);
12353 fold_checksum_tree (fn, &ctx, &ht);
12354 md5_finish_ctx (&ctx, checksum_before_fn);
12355 ht.empty ();
12357 md5_init_ctx (&ctx);
12358 for (i = 0; i < nargs; i++)
12359 fold_checksum_tree (argarray[i], &ctx, &ht);
12360 md5_finish_ctx (&ctx, checksum_before_arglist);
12361 ht.empty ();
12362 #endif
12364 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12365 if (!tem)
12366 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12368 #ifdef ENABLE_FOLD_CHECKING
12369 md5_init_ctx (&ctx);
12370 fold_checksum_tree (fn, &ctx, &ht);
12371 md5_finish_ctx (&ctx, checksum_after_fn);
12372 ht.empty ();
12374 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12375 fold_check_failed (fn, tem);
12377 md5_init_ctx (&ctx);
12378 for (i = 0; i < nargs; i++)
12379 fold_checksum_tree (argarray[i], &ctx, &ht);
12380 md5_finish_ctx (&ctx, checksum_after_arglist);
12382 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12383 fold_check_failed (NULL_TREE, tem);
12384 #endif
12385 return tem;
12388 /* Perform constant folding and related simplification of initializer
12389 expression EXPR. These behave identically to "fold_buildN" but ignore
12390 potential run-time traps and exceptions that fold must preserve. */
12392 #define START_FOLD_INIT \
12393 int saved_signaling_nans = flag_signaling_nans;\
12394 int saved_trapping_math = flag_trapping_math;\
12395 int saved_rounding_math = flag_rounding_math;\
12396 int saved_trapv = flag_trapv;\
12397 int saved_folding_initializer = folding_initializer;\
12398 flag_signaling_nans = 0;\
12399 flag_trapping_math = 0;\
12400 flag_rounding_math = 0;\
12401 flag_trapv = 0;\
12402 folding_initializer = 1;
12404 #define END_FOLD_INIT \
12405 flag_signaling_nans = saved_signaling_nans;\
12406 flag_trapping_math = saved_trapping_math;\
12407 flag_rounding_math = saved_rounding_math;\
12408 flag_trapv = saved_trapv;\
12409 folding_initializer = saved_folding_initializer;
12411 tree
12412 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12413 tree type, tree op)
12415 tree result;
12416 START_FOLD_INIT;
12418 result = fold_build1_loc (loc, code, type, op);
12420 END_FOLD_INIT;
12421 return result;
12424 tree
12425 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12426 tree type, tree op0, tree op1)
12428 tree result;
12429 START_FOLD_INIT;
12431 result = fold_build2_loc (loc, code, type, op0, op1);
12433 END_FOLD_INIT;
12434 return result;
12437 tree
12438 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12439 int nargs, tree *argarray)
12441 tree result;
12442 START_FOLD_INIT;
12444 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12446 END_FOLD_INIT;
12447 return result;
12450 #undef START_FOLD_INIT
12451 #undef END_FOLD_INIT
12453 /* Determine if first argument is a multiple of second argument. Return 0 if
12454 it is not, or we cannot easily determined it to be.
12456 An example of the sort of thing we care about (at this point; this routine
12457 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12458 fold cases do now) is discovering that
12460 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12462 is a multiple of
12464 SAVE_EXPR (J * 8)
12466 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12468 This code also handles discovering that
12470 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12472 is a multiple of 8 so we don't have to worry about dealing with a
12473 possible remainder.
12475 Note that we *look* inside a SAVE_EXPR only to determine how it was
12476 calculated; it is not safe for fold to do much of anything else with the
12477 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12478 at run time. For example, the latter example above *cannot* be implemented
12479 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12480 evaluation time of the original SAVE_EXPR is not necessarily the same at
12481 the time the new expression is evaluated. The only optimization of this
12482 sort that would be valid is changing
12484 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12486 divided by 8 to
12488 SAVE_EXPR (I) * SAVE_EXPR (J)
12490 (where the same SAVE_EXPR (J) is used in the original and the
12491 transformed version). */
12494 multiple_of_p (tree type, const_tree top, const_tree bottom)
12496 gimple *stmt;
12497 tree t1, op1, op2;
12499 if (operand_equal_p (top, bottom, 0))
12500 return 1;
12502 if (TREE_CODE (type) != INTEGER_TYPE)
12503 return 0;
12505 switch (TREE_CODE (top))
12507 case BIT_AND_EXPR:
12508 /* Bitwise and provides a power of two multiple. If the mask is
12509 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12510 if (!integer_pow2p (bottom))
12511 return 0;
12512 /* FALLTHRU */
12514 case MULT_EXPR:
12515 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12516 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12518 case MINUS_EXPR:
12519 /* It is impossible to prove if op0 - op1 is multiple of bottom
12520 precisely, so be conservative here checking if both op0 and op1
12521 are multiple of bottom. Note we check the second operand first
12522 since it's usually simpler. */
12523 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12524 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12526 case PLUS_EXPR:
12527 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12528 as op0 - 3 if the expression has unsigned type. For example,
12529 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12530 op1 = TREE_OPERAND (top, 1);
12531 if (TYPE_UNSIGNED (type)
12532 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12533 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12534 return (multiple_of_p (type, op1, bottom)
12535 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12537 case LSHIFT_EXPR:
12538 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12540 op1 = TREE_OPERAND (top, 1);
12541 /* const_binop may not detect overflow correctly,
12542 so check for it explicitly here. */
12543 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12544 && 0 != (t1 = fold_convert (type,
12545 const_binop (LSHIFT_EXPR,
12546 size_one_node,
12547 op1)))
12548 && !TREE_OVERFLOW (t1))
12549 return multiple_of_p (type, t1, bottom);
12551 return 0;
12553 case NOP_EXPR:
12554 /* Can't handle conversions from non-integral or wider integral type. */
12555 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12556 || (TYPE_PRECISION (type)
12557 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12558 return 0;
12560 /* fall through */
12562 case SAVE_EXPR:
12563 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12565 case COND_EXPR:
12566 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12567 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12569 case INTEGER_CST:
12570 if (TREE_CODE (bottom) != INTEGER_CST
12571 || integer_zerop (bottom)
12572 || (TYPE_UNSIGNED (type)
12573 && (tree_int_cst_sgn (top) < 0
12574 || tree_int_cst_sgn (bottom) < 0)))
12575 return 0;
12576 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12577 SIGNED);
12579 case SSA_NAME:
12580 if (TREE_CODE (bottom) == INTEGER_CST
12581 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12582 && gimple_code (stmt) == GIMPLE_ASSIGN)
12584 enum tree_code code = gimple_assign_rhs_code (stmt);
12586 /* Check for special cases to see if top is defined as multiple
12587 of bottom:
12589 top = (X & ~(bottom - 1) ; bottom is power of 2
12593 Y = X % bottom
12594 top = X - Y. */
12595 if (code == BIT_AND_EXPR
12596 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12597 && TREE_CODE (op2) == INTEGER_CST
12598 && integer_pow2p (bottom)
12599 && wi::multiple_of_p (wi::to_widest (op2),
12600 wi::to_widest (bottom), UNSIGNED))
12601 return 1;
12603 op1 = gimple_assign_rhs1 (stmt);
12604 if (code == MINUS_EXPR
12605 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12606 && TREE_CODE (op2) == SSA_NAME
12607 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12608 && gimple_code (stmt) == GIMPLE_ASSIGN
12609 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12610 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12611 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12612 return 1;
12615 /* fall through */
12617 default:
12618 return 0;
12622 #define tree_expr_nonnegative_warnv_p(X, Y) \
12623 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12625 #define RECURSE(X) \
12626 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12628 /* Return true if CODE or TYPE is known to be non-negative. */
12630 static bool
12631 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12633 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12634 && truth_value_p (code))
12635 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12636 have a signed:1 type (where the value is -1 and 0). */
12637 return true;
12638 return false;
12641 /* Return true if (CODE OP0) is known to be non-negative. If the return
12642 value is based on the assumption that signed overflow is undefined,
12643 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12644 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12646 bool
12647 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12648 bool *strict_overflow_p, int depth)
12650 if (TYPE_UNSIGNED (type))
12651 return true;
12653 switch (code)
12655 case ABS_EXPR:
12656 /* We can't return 1 if flag_wrapv is set because
12657 ABS_EXPR<INT_MIN> = INT_MIN. */
12658 if (!ANY_INTEGRAL_TYPE_P (type))
12659 return true;
12660 if (TYPE_OVERFLOW_UNDEFINED (type))
12662 *strict_overflow_p = true;
12663 return true;
12665 break;
12667 case NON_LVALUE_EXPR:
12668 case FLOAT_EXPR:
12669 case FIX_TRUNC_EXPR:
12670 return RECURSE (op0);
12672 CASE_CONVERT:
12674 tree inner_type = TREE_TYPE (op0);
12675 tree outer_type = type;
12677 if (TREE_CODE (outer_type) == REAL_TYPE)
12679 if (TREE_CODE (inner_type) == REAL_TYPE)
12680 return RECURSE (op0);
12681 if (INTEGRAL_TYPE_P (inner_type))
12683 if (TYPE_UNSIGNED (inner_type))
12684 return true;
12685 return RECURSE (op0);
12688 else if (INTEGRAL_TYPE_P (outer_type))
12690 if (TREE_CODE (inner_type) == REAL_TYPE)
12691 return RECURSE (op0);
12692 if (INTEGRAL_TYPE_P (inner_type))
12693 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12694 && TYPE_UNSIGNED (inner_type);
12697 break;
12699 default:
12700 return tree_simple_nonnegative_warnv_p (code, type);
12703 /* We don't know sign of `t', so be conservative and return false. */
12704 return false;
12707 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12708 value is based on the assumption that signed overflow is undefined,
12709 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12710 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12712 bool
12713 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12714 tree op1, bool *strict_overflow_p,
12715 int depth)
12717 if (TYPE_UNSIGNED (type))
12718 return true;
12720 switch (code)
12722 case POINTER_PLUS_EXPR:
12723 case PLUS_EXPR:
12724 if (FLOAT_TYPE_P (type))
12725 return RECURSE (op0) && RECURSE (op1);
12727 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12728 both unsigned and at least 2 bits shorter than the result. */
12729 if (TREE_CODE (type) == INTEGER_TYPE
12730 && TREE_CODE (op0) == NOP_EXPR
12731 && TREE_CODE (op1) == NOP_EXPR)
12733 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12734 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12735 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12736 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12738 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12739 TYPE_PRECISION (inner2)) + 1;
12740 return prec < TYPE_PRECISION (type);
12743 break;
12745 case MULT_EXPR:
12746 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12748 /* x * x is always non-negative for floating point x
12749 or without overflow. */
12750 if (operand_equal_p (op0, op1, 0)
12751 || (RECURSE (op0) && RECURSE (op1)))
12753 if (ANY_INTEGRAL_TYPE_P (type)
12754 && TYPE_OVERFLOW_UNDEFINED (type))
12755 *strict_overflow_p = true;
12756 return true;
12760 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12761 both unsigned and their total bits is shorter than the result. */
12762 if (TREE_CODE (type) == INTEGER_TYPE
12763 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12764 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12766 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12767 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12768 : TREE_TYPE (op0);
12769 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12770 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12771 : TREE_TYPE (op1);
12773 bool unsigned0 = TYPE_UNSIGNED (inner0);
12774 bool unsigned1 = TYPE_UNSIGNED (inner1);
12776 if (TREE_CODE (op0) == INTEGER_CST)
12777 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12779 if (TREE_CODE (op1) == INTEGER_CST)
12780 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12782 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12783 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12785 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12786 ? tree_int_cst_min_precision (op0, UNSIGNED)
12787 : TYPE_PRECISION (inner0);
12789 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12790 ? tree_int_cst_min_precision (op1, UNSIGNED)
12791 : TYPE_PRECISION (inner1);
12793 return precision0 + precision1 < TYPE_PRECISION (type);
12796 return false;
12798 case BIT_AND_EXPR:
12799 case MAX_EXPR:
12800 return RECURSE (op0) || RECURSE (op1);
12802 case BIT_IOR_EXPR:
12803 case BIT_XOR_EXPR:
12804 case MIN_EXPR:
12805 case RDIV_EXPR:
12806 case TRUNC_DIV_EXPR:
12807 case CEIL_DIV_EXPR:
12808 case FLOOR_DIV_EXPR:
12809 case ROUND_DIV_EXPR:
12810 return RECURSE (op0) && RECURSE (op1);
12812 case TRUNC_MOD_EXPR:
12813 return RECURSE (op0);
12815 case FLOOR_MOD_EXPR:
12816 return RECURSE (op1);
12818 case CEIL_MOD_EXPR:
12819 case ROUND_MOD_EXPR:
12820 default:
12821 return tree_simple_nonnegative_warnv_p (code, type);
12824 /* We don't know sign of `t', so be conservative and return false. */
12825 return false;
12828 /* Return true if T is known to be non-negative. If the return
12829 value is based on the assumption that signed overflow is undefined,
12830 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12831 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12833 bool
12834 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12836 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12837 return true;
12839 switch (TREE_CODE (t))
12841 case INTEGER_CST:
12842 return tree_int_cst_sgn (t) >= 0;
12844 case REAL_CST:
12845 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12847 case FIXED_CST:
12848 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12850 case COND_EXPR:
12851 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12853 case SSA_NAME:
12854 /* Limit the depth of recursion to avoid quadratic behavior.
12855 This is expected to catch almost all occurrences in practice.
12856 If this code misses important cases that unbounded recursion
12857 would not, passes that need this information could be revised
12858 to provide it through dataflow propagation. */
12859 return (!name_registered_for_update_p (t)
12860 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12861 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12862 strict_overflow_p, depth));
12864 default:
12865 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12869 /* Return true if T is known to be non-negative. If the return
12870 value is based on the assumption that signed overflow is undefined,
12871 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12872 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12874 bool
12875 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12876 bool *strict_overflow_p, int depth)
12878 switch (fn)
12880 CASE_CFN_ACOS:
12881 CASE_CFN_ACOSH:
12882 CASE_CFN_CABS:
12883 CASE_CFN_COSH:
12884 CASE_CFN_ERFC:
12885 CASE_CFN_EXP:
12886 CASE_CFN_EXP10:
12887 CASE_CFN_EXP2:
12888 CASE_CFN_FABS:
12889 CASE_CFN_FDIM:
12890 CASE_CFN_HYPOT:
12891 CASE_CFN_POW10:
12892 CASE_CFN_FFS:
12893 CASE_CFN_PARITY:
12894 CASE_CFN_POPCOUNT:
12895 CASE_CFN_CLZ:
12896 CASE_CFN_CLRSB:
12897 case CFN_BUILT_IN_BSWAP32:
12898 case CFN_BUILT_IN_BSWAP64:
12899 /* Always true. */
12900 return true;
12902 CASE_CFN_SQRT:
12903 /* sqrt(-0.0) is -0.0. */
12904 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12905 return true;
12906 return RECURSE (arg0);
12908 CASE_CFN_ASINH:
12909 CASE_CFN_ATAN:
12910 CASE_CFN_ATANH:
12911 CASE_CFN_CBRT:
12912 CASE_CFN_CEIL:
12913 CASE_CFN_ERF:
12914 CASE_CFN_EXPM1:
12915 CASE_CFN_FLOOR:
12916 CASE_CFN_FMOD:
12917 CASE_CFN_FREXP:
12918 CASE_CFN_ICEIL:
12919 CASE_CFN_IFLOOR:
12920 CASE_CFN_IRINT:
12921 CASE_CFN_IROUND:
12922 CASE_CFN_LCEIL:
12923 CASE_CFN_LDEXP:
12924 CASE_CFN_LFLOOR:
12925 CASE_CFN_LLCEIL:
12926 CASE_CFN_LLFLOOR:
12927 CASE_CFN_LLRINT:
12928 CASE_CFN_LLROUND:
12929 CASE_CFN_LRINT:
12930 CASE_CFN_LROUND:
12931 CASE_CFN_MODF:
12932 CASE_CFN_NEARBYINT:
12933 CASE_CFN_RINT:
12934 CASE_CFN_ROUND:
12935 CASE_CFN_SCALB:
12936 CASE_CFN_SCALBLN:
12937 CASE_CFN_SCALBN:
12938 CASE_CFN_SIGNBIT:
12939 CASE_CFN_SIGNIFICAND:
12940 CASE_CFN_SINH:
12941 CASE_CFN_TANH:
12942 CASE_CFN_TRUNC:
12943 /* True if the 1st argument is nonnegative. */
12944 return RECURSE (arg0);
12946 CASE_CFN_FMAX:
12947 /* True if the 1st OR 2nd arguments are nonnegative. */
12948 return RECURSE (arg0) || RECURSE (arg1);
12950 CASE_CFN_FMIN:
12951 /* True if the 1st AND 2nd arguments are nonnegative. */
12952 return RECURSE (arg0) && RECURSE (arg1);
12954 CASE_CFN_COPYSIGN:
12955 /* True if the 2nd argument is nonnegative. */
12956 return RECURSE (arg1);
12958 CASE_CFN_POWI:
12959 /* True if the 1st argument is nonnegative or the second
12960 argument is an even integer. */
12961 if (TREE_CODE (arg1) == INTEGER_CST
12962 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12963 return true;
12964 return RECURSE (arg0);
12966 CASE_CFN_POW:
12967 /* True if the 1st argument is nonnegative or the second
12968 argument is an even integer valued real. */
12969 if (TREE_CODE (arg1) == REAL_CST)
12971 REAL_VALUE_TYPE c;
12972 HOST_WIDE_INT n;
12974 c = TREE_REAL_CST (arg1);
12975 n = real_to_integer (&c);
12976 if ((n & 1) == 0)
12978 REAL_VALUE_TYPE cint;
12979 real_from_integer (&cint, VOIDmode, n, SIGNED);
12980 if (real_identical (&c, &cint))
12981 return true;
12984 return RECURSE (arg0);
12986 default:
12987 break;
12989 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
12992 /* Return true if T is known to be non-negative. If the return
12993 value is based on the assumption that signed overflow is undefined,
12994 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12995 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12997 static bool
12998 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13000 enum tree_code code = TREE_CODE (t);
13001 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13002 return true;
13004 switch (code)
13006 case TARGET_EXPR:
13008 tree temp = TARGET_EXPR_SLOT (t);
13009 t = TARGET_EXPR_INITIAL (t);
13011 /* If the initializer is non-void, then it's a normal expression
13012 that will be assigned to the slot. */
13013 if (!VOID_TYPE_P (t))
13014 return RECURSE (t);
13016 /* Otherwise, the initializer sets the slot in some way. One common
13017 way is an assignment statement at the end of the initializer. */
13018 while (1)
13020 if (TREE_CODE (t) == BIND_EXPR)
13021 t = expr_last (BIND_EXPR_BODY (t));
13022 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13023 || TREE_CODE (t) == TRY_CATCH_EXPR)
13024 t = expr_last (TREE_OPERAND (t, 0));
13025 else if (TREE_CODE (t) == STATEMENT_LIST)
13026 t = expr_last (t);
13027 else
13028 break;
13030 if (TREE_CODE (t) == MODIFY_EXPR
13031 && TREE_OPERAND (t, 0) == temp)
13032 return RECURSE (TREE_OPERAND (t, 1));
13034 return false;
13037 case CALL_EXPR:
13039 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13040 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13042 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13043 get_call_combined_fn (t),
13044 arg0,
13045 arg1,
13046 strict_overflow_p, depth);
13048 case COMPOUND_EXPR:
13049 case MODIFY_EXPR:
13050 return RECURSE (TREE_OPERAND (t, 1));
13052 case BIND_EXPR:
13053 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13055 case SAVE_EXPR:
13056 return RECURSE (TREE_OPERAND (t, 0));
13058 default:
13059 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13063 #undef RECURSE
13064 #undef tree_expr_nonnegative_warnv_p
13066 /* Return true if T is known to be non-negative. If the return
13067 value is based on the assumption that signed overflow is undefined,
13068 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13069 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13071 bool
13072 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13074 enum tree_code code;
13075 if (t == error_mark_node)
13076 return false;
13078 code = TREE_CODE (t);
13079 switch (TREE_CODE_CLASS (code))
13081 case tcc_binary:
13082 case tcc_comparison:
13083 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13084 TREE_TYPE (t),
13085 TREE_OPERAND (t, 0),
13086 TREE_OPERAND (t, 1),
13087 strict_overflow_p, depth);
13089 case tcc_unary:
13090 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13091 TREE_TYPE (t),
13092 TREE_OPERAND (t, 0),
13093 strict_overflow_p, depth);
13095 case tcc_constant:
13096 case tcc_declaration:
13097 case tcc_reference:
13098 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13100 default:
13101 break;
13104 switch (code)
13106 case TRUTH_AND_EXPR:
13107 case TRUTH_OR_EXPR:
13108 case TRUTH_XOR_EXPR:
13109 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13110 TREE_TYPE (t),
13111 TREE_OPERAND (t, 0),
13112 TREE_OPERAND (t, 1),
13113 strict_overflow_p, depth);
13114 case TRUTH_NOT_EXPR:
13115 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13116 TREE_TYPE (t),
13117 TREE_OPERAND (t, 0),
13118 strict_overflow_p, depth);
13120 case COND_EXPR:
13121 case CONSTRUCTOR:
13122 case OBJ_TYPE_REF:
13123 case ASSERT_EXPR:
13124 case ADDR_EXPR:
13125 case WITH_SIZE_EXPR:
13126 case SSA_NAME:
13127 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13129 default:
13130 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13134 /* Return true if `t' is known to be non-negative. Handle warnings
13135 about undefined signed overflow. */
13137 bool
13138 tree_expr_nonnegative_p (tree t)
13140 bool ret, strict_overflow_p;
13142 strict_overflow_p = false;
13143 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13144 if (strict_overflow_p)
13145 fold_overflow_warning (("assuming signed overflow does not occur when "
13146 "determining that expression is always "
13147 "non-negative"),
13148 WARN_STRICT_OVERFLOW_MISC);
13149 return ret;
13153 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13154 For floating point we further ensure that T is not denormal.
13155 Similar logic is present in nonzero_address in rtlanal.h.
13157 If the return value is based on the assumption that signed overflow
13158 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13159 change *STRICT_OVERFLOW_P. */
13161 bool
13162 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13163 bool *strict_overflow_p)
13165 switch (code)
13167 case ABS_EXPR:
13168 return tree_expr_nonzero_warnv_p (op0,
13169 strict_overflow_p);
13171 case NOP_EXPR:
13173 tree inner_type = TREE_TYPE (op0);
13174 tree outer_type = type;
13176 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13177 && tree_expr_nonzero_warnv_p (op0,
13178 strict_overflow_p));
13180 break;
13182 case NON_LVALUE_EXPR:
13183 return tree_expr_nonzero_warnv_p (op0,
13184 strict_overflow_p);
13186 default:
13187 break;
13190 return false;
13193 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13194 For floating point we further ensure that T is not denormal.
13195 Similar logic is present in nonzero_address in rtlanal.h.
13197 If the return value is based on the assumption that signed overflow
13198 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13199 change *STRICT_OVERFLOW_P. */
13201 bool
13202 tree_binary_nonzero_warnv_p (enum tree_code code,
13203 tree type,
13204 tree op0,
13205 tree op1, bool *strict_overflow_p)
13207 bool sub_strict_overflow_p;
13208 switch (code)
13210 case POINTER_PLUS_EXPR:
13211 case PLUS_EXPR:
13212 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13214 /* With the presence of negative values it is hard
13215 to say something. */
13216 sub_strict_overflow_p = false;
13217 if (!tree_expr_nonnegative_warnv_p (op0,
13218 &sub_strict_overflow_p)
13219 || !tree_expr_nonnegative_warnv_p (op1,
13220 &sub_strict_overflow_p))
13221 return false;
13222 /* One of operands must be positive and the other non-negative. */
13223 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13224 overflows, on a twos-complement machine the sum of two
13225 nonnegative numbers can never be zero. */
13226 return (tree_expr_nonzero_warnv_p (op0,
13227 strict_overflow_p)
13228 || tree_expr_nonzero_warnv_p (op1,
13229 strict_overflow_p));
13231 break;
13233 case MULT_EXPR:
13234 if (TYPE_OVERFLOW_UNDEFINED (type))
13236 if (tree_expr_nonzero_warnv_p (op0,
13237 strict_overflow_p)
13238 && tree_expr_nonzero_warnv_p (op1,
13239 strict_overflow_p))
13241 *strict_overflow_p = true;
13242 return true;
13245 break;
13247 case MIN_EXPR:
13248 sub_strict_overflow_p = false;
13249 if (tree_expr_nonzero_warnv_p (op0,
13250 &sub_strict_overflow_p)
13251 && tree_expr_nonzero_warnv_p (op1,
13252 &sub_strict_overflow_p))
13254 if (sub_strict_overflow_p)
13255 *strict_overflow_p = true;
13257 break;
13259 case MAX_EXPR:
13260 sub_strict_overflow_p = false;
13261 if (tree_expr_nonzero_warnv_p (op0,
13262 &sub_strict_overflow_p))
13264 if (sub_strict_overflow_p)
13265 *strict_overflow_p = true;
13267 /* When both operands are nonzero, then MAX must be too. */
13268 if (tree_expr_nonzero_warnv_p (op1,
13269 strict_overflow_p))
13270 return true;
13272 /* MAX where operand 0 is positive is positive. */
13273 return tree_expr_nonnegative_warnv_p (op0,
13274 strict_overflow_p);
13276 /* MAX where operand 1 is positive is positive. */
13277 else if (tree_expr_nonzero_warnv_p (op1,
13278 &sub_strict_overflow_p)
13279 && tree_expr_nonnegative_warnv_p (op1,
13280 &sub_strict_overflow_p))
13282 if (sub_strict_overflow_p)
13283 *strict_overflow_p = true;
13284 return true;
13286 break;
13288 case BIT_IOR_EXPR:
13289 return (tree_expr_nonzero_warnv_p (op1,
13290 strict_overflow_p)
13291 || tree_expr_nonzero_warnv_p (op0,
13292 strict_overflow_p));
13294 default:
13295 break;
13298 return false;
13301 /* Return true when T is an address and is known to be nonzero.
13302 For floating point we further ensure that T is not denormal.
13303 Similar logic is present in nonzero_address in rtlanal.h.
13305 If the return value is based on the assumption that signed overflow
13306 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13307 change *STRICT_OVERFLOW_P. */
13309 bool
13310 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13312 bool sub_strict_overflow_p;
13313 switch (TREE_CODE (t))
13315 case INTEGER_CST:
13316 return !integer_zerop (t);
13318 case ADDR_EXPR:
13320 tree base = TREE_OPERAND (t, 0);
13322 if (!DECL_P (base))
13323 base = get_base_address (base);
13325 if (base && TREE_CODE (base) == TARGET_EXPR)
13326 base = TARGET_EXPR_SLOT (base);
13328 if (!base)
13329 return false;
13331 /* For objects in symbol table check if we know they are non-zero.
13332 Don't do anything for variables and functions before symtab is built;
13333 it is quite possible that they will be declared weak later. */
13334 int nonzero_addr = maybe_nonzero_address (base);
13335 if (nonzero_addr >= 0)
13336 return nonzero_addr;
13338 /* Constants are never weak. */
13339 if (CONSTANT_CLASS_P (base))
13340 return true;
13342 return false;
13345 case COND_EXPR:
13346 sub_strict_overflow_p = false;
13347 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13348 &sub_strict_overflow_p)
13349 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13350 &sub_strict_overflow_p))
13352 if (sub_strict_overflow_p)
13353 *strict_overflow_p = true;
13354 return true;
13356 break;
13358 default:
13359 break;
13361 return false;
13364 #define integer_valued_real_p(X) \
13365 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13367 #define RECURSE(X) \
13368 ((integer_valued_real_p) (X, depth + 1))
13370 /* Return true if the floating point result of (CODE OP0) has an
13371 integer value. We also allow +Inf, -Inf and NaN to be considered
13372 integer values. Return false for signaling NaN.
13374 DEPTH is the current nesting depth of the query. */
13376 bool
13377 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13379 switch (code)
13381 case FLOAT_EXPR:
13382 return true;
13384 case ABS_EXPR:
13385 return RECURSE (op0);
13387 CASE_CONVERT:
13389 tree type = TREE_TYPE (op0);
13390 if (TREE_CODE (type) == INTEGER_TYPE)
13391 return true;
13392 if (TREE_CODE (type) == REAL_TYPE)
13393 return RECURSE (op0);
13394 break;
13397 default:
13398 break;
13400 return false;
13403 /* Return true if the floating point result of (CODE OP0 OP1) has an
13404 integer value. We also allow +Inf, -Inf and NaN to be considered
13405 integer values. Return false for signaling NaN.
13407 DEPTH is the current nesting depth of the query. */
13409 bool
13410 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13412 switch (code)
13414 case PLUS_EXPR:
13415 case MINUS_EXPR:
13416 case MULT_EXPR:
13417 case MIN_EXPR:
13418 case MAX_EXPR:
13419 return RECURSE (op0) && RECURSE (op1);
13421 default:
13422 break;
13424 return false;
13427 /* Return true if the floating point result of calling FNDECL with arguments
13428 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13429 considered integer values. Return false for signaling NaN. If FNDECL
13430 takes fewer than 2 arguments, the remaining ARGn are null.
13432 DEPTH is the current nesting depth of the query. */
13434 bool
13435 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13437 switch (fn)
13439 CASE_CFN_CEIL:
13440 CASE_CFN_FLOOR:
13441 CASE_CFN_NEARBYINT:
13442 CASE_CFN_RINT:
13443 CASE_CFN_ROUND:
13444 CASE_CFN_TRUNC:
13445 return true;
13447 CASE_CFN_FMIN:
13448 CASE_CFN_FMAX:
13449 return RECURSE (arg0) && RECURSE (arg1);
13451 default:
13452 break;
13454 return false;
13457 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13458 has an integer value. We also allow +Inf, -Inf and NaN to be
13459 considered integer values. Return false for signaling NaN.
13461 DEPTH is the current nesting depth of the query. */
13463 bool
13464 integer_valued_real_single_p (tree t, int depth)
13466 switch (TREE_CODE (t))
13468 case REAL_CST:
13469 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13471 case COND_EXPR:
13472 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13474 case SSA_NAME:
13475 /* Limit the depth of recursion to avoid quadratic behavior.
13476 This is expected to catch almost all occurrences in practice.
13477 If this code misses important cases that unbounded recursion
13478 would not, passes that need this information could be revised
13479 to provide it through dataflow propagation. */
13480 return (!name_registered_for_update_p (t)
13481 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13482 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13483 depth));
13485 default:
13486 break;
13488 return false;
13491 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13492 has an integer value. We also allow +Inf, -Inf and NaN to be
13493 considered integer values. Return false for signaling NaN.
13495 DEPTH is the current nesting depth of the query. */
13497 static bool
13498 integer_valued_real_invalid_p (tree t, int depth)
13500 switch (TREE_CODE (t))
13502 case COMPOUND_EXPR:
13503 case MODIFY_EXPR:
13504 case BIND_EXPR:
13505 return RECURSE (TREE_OPERAND (t, 1));
13507 case SAVE_EXPR:
13508 return RECURSE (TREE_OPERAND (t, 0));
13510 default:
13511 break;
13513 return false;
13516 #undef RECURSE
13517 #undef integer_valued_real_p
13519 /* Return true if the floating point expression T has an integer value.
13520 We also allow +Inf, -Inf and NaN to be considered integer values.
13521 Return false for signaling NaN.
13523 DEPTH is the current nesting depth of the query. */
13525 bool
13526 integer_valued_real_p (tree t, int depth)
13528 if (t == error_mark_node)
13529 return false;
13531 tree_code code = TREE_CODE (t);
13532 switch (TREE_CODE_CLASS (code))
13534 case tcc_binary:
13535 case tcc_comparison:
13536 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13537 TREE_OPERAND (t, 1), depth);
13539 case tcc_unary:
13540 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13542 case tcc_constant:
13543 case tcc_declaration:
13544 case tcc_reference:
13545 return integer_valued_real_single_p (t, depth);
13547 default:
13548 break;
13551 switch (code)
13553 case COND_EXPR:
13554 case SSA_NAME:
13555 return integer_valued_real_single_p (t, depth);
13557 case CALL_EXPR:
13559 tree arg0 = (call_expr_nargs (t) > 0
13560 ? CALL_EXPR_ARG (t, 0)
13561 : NULL_TREE);
13562 tree arg1 = (call_expr_nargs (t) > 1
13563 ? CALL_EXPR_ARG (t, 1)
13564 : NULL_TREE);
13565 return integer_valued_real_call_p (get_call_combined_fn (t),
13566 arg0, arg1, depth);
13569 default:
13570 return integer_valued_real_invalid_p (t, depth);
13574 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13575 attempt to fold the expression to a constant without modifying TYPE,
13576 OP0 or OP1.
13578 If the expression could be simplified to a constant, then return
13579 the constant. If the expression would not be simplified to a
13580 constant, then return NULL_TREE. */
13582 tree
13583 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13585 tree tem = fold_binary (code, type, op0, op1);
13586 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13589 /* Given the components of a unary expression CODE, TYPE and OP0,
13590 attempt to fold the expression to a constant without modifying
13591 TYPE or OP0.
13593 If the expression could be simplified to a constant, then return
13594 the constant. If the expression would not be simplified to a
13595 constant, then return NULL_TREE. */
13597 tree
13598 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13600 tree tem = fold_unary (code, type, op0);
13601 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13604 /* If EXP represents referencing an element in a constant string
13605 (either via pointer arithmetic or array indexing), return the
13606 tree representing the value accessed, otherwise return NULL. */
13608 tree
13609 fold_read_from_constant_string (tree exp)
13611 if ((TREE_CODE (exp) == INDIRECT_REF
13612 || TREE_CODE (exp) == ARRAY_REF)
13613 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13615 tree exp1 = TREE_OPERAND (exp, 0);
13616 tree index;
13617 tree string;
13618 location_t loc = EXPR_LOCATION (exp);
13620 if (TREE_CODE (exp) == INDIRECT_REF)
13621 string = string_constant (exp1, &index);
13622 else
13624 tree low_bound = array_ref_low_bound (exp);
13625 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13627 /* Optimize the special-case of a zero lower bound.
13629 We convert the low_bound to sizetype to avoid some problems
13630 with constant folding. (E.g. suppose the lower bound is 1,
13631 and its mode is QI. Without the conversion,l (ARRAY
13632 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13633 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13634 if (! integer_zerop (low_bound))
13635 index = size_diffop_loc (loc, index,
13636 fold_convert_loc (loc, sizetype, low_bound));
13638 string = exp1;
13641 if (string
13642 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13643 && TREE_CODE (string) == STRING_CST
13644 && TREE_CODE (index) == INTEGER_CST
13645 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13646 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13647 == MODE_INT)
13648 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13649 return build_int_cst_type (TREE_TYPE (exp),
13650 (TREE_STRING_POINTER (string)
13651 [TREE_INT_CST_LOW (index)]));
13653 return NULL;
13656 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13657 an integer constant, real, or fixed-point constant.
13659 TYPE is the type of the result. */
13661 static tree
13662 fold_negate_const (tree arg0, tree type)
13664 tree t = NULL_TREE;
13666 switch (TREE_CODE (arg0))
13668 case INTEGER_CST:
13670 bool overflow;
13671 wide_int val = wi::neg (arg0, &overflow);
13672 t = force_fit_type (type, val, 1,
13673 (overflow | TREE_OVERFLOW (arg0))
13674 && !TYPE_UNSIGNED (type));
13675 break;
13678 case REAL_CST:
13679 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13680 break;
13682 case FIXED_CST:
13684 FIXED_VALUE_TYPE f;
13685 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13686 &(TREE_FIXED_CST (arg0)), NULL,
13687 TYPE_SATURATING (type));
13688 t = build_fixed (type, f);
13689 /* Propagate overflow flags. */
13690 if (overflow_p | TREE_OVERFLOW (arg0))
13691 TREE_OVERFLOW (t) = 1;
13692 break;
13695 default:
13696 gcc_unreachable ();
13699 return t;
13702 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13703 an integer constant or real constant.
13705 TYPE is the type of the result. */
13707 tree
13708 fold_abs_const (tree arg0, tree type)
13710 tree t = NULL_TREE;
13712 switch (TREE_CODE (arg0))
13714 case INTEGER_CST:
13716 /* If the value is unsigned or non-negative, then the absolute value
13717 is the same as the ordinary value. */
13718 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13719 t = arg0;
13721 /* If the value is negative, then the absolute value is
13722 its negation. */
13723 else
13725 bool overflow;
13726 wide_int val = wi::neg (arg0, &overflow);
13727 t = force_fit_type (type, val, -1,
13728 overflow | TREE_OVERFLOW (arg0));
13731 break;
13733 case REAL_CST:
13734 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13735 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13736 else
13737 t = arg0;
13738 break;
13740 default:
13741 gcc_unreachable ();
13744 return t;
13747 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13748 constant. TYPE is the type of the result. */
13750 static tree
13751 fold_not_const (const_tree arg0, tree type)
13753 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13755 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13758 /* Given CODE, a relational operator, the target type, TYPE and two
13759 constant operands OP0 and OP1, return the result of the
13760 relational operation. If the result is not a compile time
13761 constant, then return NULL_TREE. */
13763 static tree
13764 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13766 int result, invert;
13768 /* From here on, the only cases we handle are when the result is
13769 known to be a constant. */
13771 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13773 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13774 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13776 /* Handle the cases where either operand is a NaN. */
13777 if (real_isnan (c0) || real_isnan (c1))
13779 switch (code)
13781 case EQ_EXPR:
13782 case ORDERED_EXPR:
13783 result = 0;
13784 break;
13786 case NE_EXPR:
13787 case UNORDERED_EXPR:
13788 case UNLT_EXPR:
13789 case UNLE_EXPR:
13790 case UNGT_EXPR:
13791 case UNGE_EXPR:
13792 case UNEQ_EXPR:
13793 result = 1;
13794 break;
13796 case LT_EXPR:
13797 case LE_EXPR:
13798 case GT_EXPR:
13799 case GE_EXPR:
13800 case LTGT_EXPR:
13801 if (flag_trapping_math)
13802 return NULL_TREE;
13803 result = 0;
13804 break;
13806 default:
13807 gcc_unreachable ();
13810 return constant_boolean_node (result, type);
13813 return constant_boolean_node (real_compare (code, c0, c1), type);
13816 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13818 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13819 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13820 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13823 /* Handle equality/inequality of complex constants. */
13824 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13826 tree rcond = fold_relational_const (code, type,
13827 TREE_REALPART (op0),
13828 TREE_REALPART (op1));
13829 tree icond = fold_relational_const (code, type,
13830 TREE_IMAGPART (op0),
13831 TREE_IMAGPART (op1));
13832 if (code == EQ_EXPR)
13833 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13834 else if (code == NE_EXPR)
13835 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13836 else
13837 return NULL_TREE;
13840 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13842 if (!VECTOR_TYPE_P (type))
13844 /* Have vector comparison with scalar boolean result. */
13845 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13846 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13847 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13849 tree elem0 = VECTOR_CST_ELT (op0, i);
13850 tree elem1 = VECTOR_CST_ELT (op1, i);
13851 tree tmp = fold_relational_const (code, type, elem0, elem1);
13852 if (tmp == NULL_TREE)
13853 return NULL_TREE;
13854 if (integer_zerop (tmp))
13855 return constant_boolean_node (false, type);
13857 return constant_boolean_node (true, type);
13859 unsigned count = VECTOR_CST_NELTS (op0);
13860 tree *elts = XALLOCAVEC (tree, count);
13861 gcc_assert (VECTOR_CST_NELTS (op1) == count
13862 && TYPE_VECTOR_SUBPARTS (type) == count);
13864 for (unsigned i = 0; i < count; i++)
13866 tree elem_type = TREE_TYPE (type);
13867 tree elem0 = VECTOR_CST_ELT (op0, i);
13868 tree elem1 = VECTOR_CST_ELT (op1, i);
13870 tree tem = fold_relational_const (code, elem_type,
13871 elem0, elem1);
13873 if (tem == NULL_TREE)
13874 return NULL_TREE;
13876 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13879 return build_vector (type, elts);
13882 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13884 To compute GT, swap the arguments and do LT.
13885 To compute GE, do LT and invert the result.
13886 To compute LE, swap the arguments, do LT and invert the result.
13887 To compute NE, do EQ and invert the result.
13889 Therefore, the code below must handle only EQ and LT. */
13891 if (code == LE_EXPR || code == GT_EXPR)
13893 std::swap (op0, op1);
13894 code = swap_tree_comparison (code);
13897 /* Note that it is safe to invert for real values here because we
13898 have already handled the one case that it matters. */
13900 invert = 0;
13901 if (code == NE_EXPR || code == GE_EXPR)
13903 invert = 1;
13904 code = invert_tree_comparison (code, false);
13907 /* Compute a result for LT or EQ if args permit;
13908 Otherwise return T. */
13909 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13911 if (code == EQ_EXPR)
13912 result = tree_int_cst_equal (op0, op1);
13913 else
13914 result = tree_int_cst_lt (op0, op1);
13916 else
13917 return NULL_TREE;
13919 if (invert)
13920 result ^= 1;
13921 return constant_boolean_node (result, type);
13924 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13925 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13926 itself. */
13928 tree
13929 fold_build_cleanup_point_expr (tree type, tree expr)
13931 /* If the expression does not have side effects then we don't have to wrap
13932 it with a cleanup point expression. */
13933 if (!TREE_SIDE_EFFECTS (expr))
13934 return expr;
13936 /* If the expression is a return, check to see if the expression inside the
13937 return has no side effects or the right hand side of the modify expression
13938 inside the return. If either don't have side effects set we don't need to
13939 wrap the expression in a cleanup point expression. Note we don't check the
13940 left hand side of the modify because it should always be a return decl. */
13941 if (TREE_CODE (expr) == RETURN_EXPR)
13943 tree op = TREE_OPERAND (expr, 0);
13944 if (!op || !TREE_SIDE_EFFECTS (op))
13945 return expr;
13946 op = TREE_OPERAND (op, 1);
13947 if (!TREE_SIDE_EFFECTS (op))
13948 return expr;
13951 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
13954 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13955 of an indirection through OP0, or NULL_TREE if no simplification is
13956 possible. */
13958 tree
13959 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13961 tree sub = op0;
13962 tree subtype;
13964 STRIP_NOPS (sub);
13965 subtype = TREE_TYPE (sub);
13966 if (!POINTER_TYPE_P (subtype))
13967 return NULL_TREE;
13969 if (TREE_CODE (sub) == ADDR_EXPR)
13971 tree op = TREE_OPERAND (sub, 0);
13972 tree optype = TREE_TYPE (op);
13973 /* *&CONST_DECL -> to the value of the const decl. */
13974 if (TREE_CODE (op) == CONST_DECL)
13975 return DECL_INITIAL (op);
13976 /* *&p => p; make sure to handle *&"str"[cst] here. */
13977 if (type == optype)
13979 tree fop = fold_read_from_constant_string (op);
13980 if (fop)
13981 return fop;
13982 else
13983 return op;
13985 /* *(foo *)&fooarray => fooarray[0] */
13986 else if (TREE_CODE (optype) == ARRAY_TYPE
13987 && type == TREE_TYPE (optype)
13988 && (!in_gimple_form
13989 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13991 tree type_domain = TYPE_DOMAIN (optype);
13992 tree min_val = size_zero_node;
13993 if (type_domain && TYPE_MIN_VALUE (type_domain))
13994 min_val = TYPE_MIN_VALUE (type_domain);
13995 if (in_gimple_form
13996 && TREE_CODE (min_val) != INTEGER_CST)
13997 return NULL_TREE;
13998 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13999 NULL_TREE, NULL_TREE);
14001 /* *(foo *)&complexfoo => __real__ complexfoo */
14002 else if (TREE_CODE (optype) == COMPLEX_TYPE
14003 && type == TREE_TYPE (optype))
14004 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14005 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14006 else if (TREE_CODE (optype) == VECTOR_TYPE
14007 && type == TREE_TYPE (optype))
14009 tree part_width = TYPE_SIZE (type);
14010 tree index = bitsize_int (0);
14011 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14015 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14016 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14018 tree op00 = TREE_OPERAND (sub, 0);
14019 tree op01 = TREE_OPERAND (sub, 1);
14021 STRIP_NOPS (op00);
14022 if (TREE_CODE (op00) == ADDR_EXPR)
14024 tree op00type;
14025 op00 = TREE_OPERAND (op00, 0);
14026 op00type = TREE_TYPE (op00);
14028 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14029 if (TREE_CODE (op00type) == VECTOR_TYPE
14030 && type == TREE_TYPE (op00type))
14032 tree part_width = TYPE_SIZE (type);
14033 unsigned HOST_WIDE_INT max_offset
14034 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14035 * TYPE_VECTOR_SUBPARTS (op00type));
14036 if (tree_int_cst_sign_bit (op01) == 0
14037 && compare_tree_int (op01, max_offset) == -1)
14039 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14040 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14041 tree index = bitsize_int (indexi);
14042 return fold_build3_loc (loc,
14043 BIT_FIELD_REF, type, op00,
14044 part_width, index);
14047 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14048 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14049 && type == TREE_TYPE (op00type))
14051 tree size = TYPE_SIZE_UNIT (type);
14052 if (tree_int_cst_equal (size, op01))
14053 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14055 /* ((foo *)&fooarray)[1] => fooarray[1] */
14056 else if (TREE_CODE (op00type) == ARRAY_TYPE
14057 && type == TREE_TYPE (op00type))
14059 tree type_domain = TYPE_DOMAIN (op00type);
14060 tree min_val = size_zero_node;
14061 if (type_domain && TYPE_MIN_VALUE (type_domain))
14062 min_val = TYPE_MIN_VALUE (type_domain);
14063 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14064 TYPE_SIZE_UNIT (type));
14065 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14066 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14067 NULL_TREE, NULL_TREE);
14072 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14073 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14074 && type == TREE_TYPE (TREE_TYPE (subtype))
14075 && (!in_gimple_form
14076 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14078 tree type_domain;
14079 tree min_val = size_zero_node;
14080 sub = build_fold_indirect_ref_loc (loc, sub);
14081 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14082 if (type_domain && TYPE_MIN_VALUE (type_domain))
14083 min_val = TYPE_MIN_VALUE (type_domain);
14084 if (in_gimple_form
14085 && TREE_CODE (min_val) != INTEGER_CST)
14086 return NULL_TREE;
14087 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14088 NULL_TREE);
14091 return NULL_TREE;
14094 /* Builds an expression for an indirection through T, simplifying some
14095 cases. */
14097 tree
14098 build_fold_indirect_ref_loc (location_t loc, tree t)
14100 tree type = TREE_TYPE (TREE_TYPE (t));
14101 tree sub = fold_indirect_ref_1 (loc, type, t);
14103 if (sub)
14104 return sub;
14106 return build1_loc (loc, INDIRECT_REF, type, t);
14109 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14111 tree
14112 fold_indirect_ref_loc (location_t loc, tree t)
14114 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14116 if (sub)
14117 return sub;
14118 else
14119 return t;
14122 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14123 whose result is ignored. The type of the returned tree need not be
14124 the same as the original expression. */
14126 tree
14127 fold_ignored_result (tree t)
14129 if (!TREE_SIDE_EFFECTS (t))
14130 return integer_zero_node;
14132 for (;;)
14133 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14135 case tcc_unary:
14136 t = TREE_OPERAND (t, 0);
14137 break;
14139 case tcc_binary:
14140 case tcc_comparison:
14141 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14142 t = TREE_OPERAND (t, 0);
14143 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14144 t = TREE_OPERAND (t, 1);
14145 else
14146 return t;
14147 break;
14149 case tcc_expression:
14150 switch (TREE_CODE (t))
14152 case COMPOUND_EXPR:
14153 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14154 return t;
14155 t = TREE_OPERAND (t, 0);
14156 break;
14158 case COND_EXPR:
14159 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14160 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14161 return t;
14162 t = TREE_OPERAND (t, 0);
14163 break;
14165 default:
14166 return t;
14168 break;
14170 default:
14171 return t;
14175 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14177 tree
14178 round_up_loc (location_t loc, tree value, unsigned int divisor)
14180 tree div = NULL_TREE;
14182 if (divisor == 1)
14183 return value;
14185 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14186 have to do anything. Only do this when we are not given a const,
14187 because in that case, this check is more expensive than just
14188 doing it. */
14189 if (TREE_CODE (value) != INTEGER_CST)
14191 div = build_int_cst (TREE_TYPE (value), divisor);
14193 if (multiple_of_p (TREE_TYPE (value), value, div))
14194 return value;
14197 /* If divisor is a power of two, simplify this to bit manipulation. */
14198 if (pow2_or_zerop (divisor))
14200 if (TREE_CODE (value) == INTEGER_CST)
14202 wide_int val = value;
14203 bool overflow_p;
14205 if ((val & (divisor - 1)) == 0)
14206 return value;
14208 overflow_p = TREE_OVERFLOW (value);
14209 val += divisor - 1;
14210 val &= - (int) divisor;
14211 if (val == 0)
14212 overflow_p = true;
14214 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14216 else
14218 tree t;
14220 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14221 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14222 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14223 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14226 else
14228 if (!div)
14229 div = build_int_cst (TREE_TYPE (value), divisor);
14230 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14231 value = size_binop_loc (loc, MULT_EXPR, value, div);
14234 return value;
14237 /* Likewise, but round down. */
14239 tree
14240 round_down_loc (location_t loc, tree value, int divisor)
14242 tree div = NULL_TREE;
14244 gcc_assert (divisor > 0);
14245 if (divisor == 1)
14246 return value;
14248 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14249 have to do anything. Only do this when we are not given a const,
14250 because in that case, this check is more expensive than just
14251 doing it. */
14252 if (TREE_CODE (value) != INTEGER_CST)
14254 div = build_int_cst (TREE_TYPE (value), divisor);
14256 if (multiple_of_p (TREE_TYPE (value), value, div))
14257 return value;
14260 /* If divisor is a power of two, simplify this to bit manipulation. */
14261 if (pow2_or_zerop (divisor))
14263 tree t;
14265 t = build_int_cst (TREE_TYPE (value), -divisor);
14266 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14268 else
14270 if (!div)
14271 div = build_int_cst (TREE_TYPE (value), divisor);
14272 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14273 value = size_binop_loc (loc, MULT_EXPR, value, div);
14276 return value;
14279 /* Returns the pointer to the base of the object addressed by EXP and
14280 extracts the information about the offset of the access, storing it
14281 to PBITPOS and POFFSET. */
14283 static tree
14284 split_address_to_core_and_offset (tree exp,
14285 HOST_WIDE_INT *pbitpos, tree *poffset)
14287 tree core;
14288 machine_mode mode;
14289 int unsignedp, reversep, volatilep;
14290 HOST_WIDE_INT bitsize;
14291 location_t loc = EXPR_LOCATION (exp);
14293 if (TREE_CODE (exp) == ADDR_EXPR)
14295 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14296 poffset, &mode, &unsignedp, &reversep,
14297 &volatilep);
14298 core = build_fold_addr_expr_loc (loc, core);
14300 else
14302 core = exp;
14303 *pbitpos = 0;
14304 *poffset = NULL_TREE;
14307 return core;
14310 /* Returns true if addresses of E1 and E2 differ by a constant, false
14311 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14313 bool
14314 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14316 tree core1, core2;
14317 HOST_WIDE_INT bitpos1, bitpos2;
14318 tree toffset1, toffset2, tdiff, type;
14320 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14321 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14323 if (bitpos1 % BITS_PER_UNIT != 0
14324 || bitpos2 % BITS_PER_UNIT != 0
14325 || !operand_equal_p (core1, core2, 0))
14326 return false;
14328 if (toffset1 && toffset2)
14330 type = TREE_TYPE (toffset1);
14331 if (type != TREE_TYPE (toffset2))
14332 toffset2 = fold_convert (type, toffset2);
14334 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14335 if (!cst_and_fits_in_hwi (tdiff))
14336 return false;
14338 *diff = int_cst_value (tdiff);
14340 else if (toffset1 || toffset2)
14342 /* If only one of the offsets is non-constant, the difference cannot
14343 be a constant. */
14344 return false;
14346 else
14347 *diff = 0;
14349 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14350 return true;
14353 /* Return OFF converted to a pointer offset type suitable as offset for
14354 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14355 tree
14356 convert_to_ptrofftype_loc (location_t loc, tree off)
14358 return fold_convert_loc (loc, sizetype, off);
14361 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14362 tree
14363 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14365 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14366 ptr, convert_to_ptrofftype_loc (loc, off));
14369 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14370 tree
14371 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14373 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14374 ptr, size_int (off));
14377 /* Return a char pointer for a C string if it is a string constant
14378 or sum of string constant and integer constant. We only support
14379 string constants properly terminated with '\0' character.
14380 If STRLEN is a valid pointer, length (including terminating character)
14381 of returned string is stored to the argument. */
14383 const char *
14384 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14386 tree offset_node;
14388 if (strlen)
14389 *strlen = 0;
14391 src = string_constant (src, &offset_node);
14392 if (src == 0)
14393 return NULL;
14395 unsigned HOST_WIDE_INT offset = 0;
14396 if (offset_node != NULL_TREE)
14398 if (!tree_fits_uhwi_p (offset_node))
14399 return NULL;
14400 else
14401 offset = tree_to_uhwi (offset_node);
14404 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14405 const char *string = TREE_STRING_POINTER (src);
14407 /* Support only properly null-terminated strings. */
14408 if (string_length == 0
14409 || string[string_length - 1] != '\0'
14410 || offset >= string_length)
14411 return NULL;
14413 if (strlen)
14414 *strlen = string_length - offset;
14415 return string + offset;
14418 #if CHECKING_P
14420 namespace selftest {
14422 /* Helper functions for writing tests of folding trees. */
14424 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14426 static void
14427 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14428 tree constant)
14430 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14433 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14434 wrapping WRAPPED_EXPR. */
14436 static void
14437 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14438 tree wrapped_expr)
14440 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14441 ASSERT_NE (wrapped_expr, result);
14442 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14443 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14446 /* Verify that various arithmetic binary operations are folded
14447 correctly. */
14449 static void
14450 test_arithmetic_folding ()
14452 tree type = integer_type_node;
14453 tree x = create_tmp_var_raw (type, "x");
14454 tree zero = build_zero_cst (type);
14455 tree one = build_int_cst (type, 1);
14457 /* Addition. */
14458 /* 1 <-- (0 + 1) */
14459 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14460 one);
14461 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14462 one);
14464 /* (nonlvalue)x <-- (x + 0) */
14465 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14468 /* Subtraction. */
14469 /* 0 <-- (x - x) */
14470 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14471 zero);
14472 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14475 /* Multiplication. */
14476 /* 0 <-- (x * 0) */
14477 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14478 zero);
14480 /* (nonlvalue)x <-- (x * 1) */
14481 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14485 /* Verify that various binary operations on vectors are folded
14486 correctly. */
14488 static void
14489 test_vector_folding ()
14491 tree inner_type = integer_type_node;
14492 tree type = build_vector_type (inner_type, 4);
14493 tree zero = build_zero_cst (type);
14494 tree one = build_one_cst (type);
14496 /* Verify equality tests that return a scalar boolean result. */
14497 tree res_type = boolean_type_node;
14498 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14499 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14500 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14501 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14504 /* Run all of the selftests within this file. */
14506 void
14507 fold_const_c_tests ()
14509 test_arithmetic_folding ();
14510 test_vector_folding ();
14513 } // namespace selftest
14515 #endif /* CHECKING_P */