gcc/testsuite
[official-gcc.git] / gcc / fold-const.c
blob603aff096590ac2cd65e2c23a08972675e59039d
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
83 /* Nonzero if we are folding constants inside an initializer; zero
84 otherwise. */
85 int folding_initializer = 0;
87 /* The following constants represent a bit based encoding of GCC's
88 comparison operators. This encoding simplifies transformations
89 on relational comparison operators, such as AND and OR. */
90 enum comparison_code {
91 COMPCODE_FALSE = 0,
92 COMPCODE_LT = 1,
93 COMPCODE_EQ = 2,
94 COMPCODE_LE = 3,
95 COMPCODE_GT = 4,
96 COMPCODE_LTGT = 5,
97 COMPCODE_GE = 6,
98 COMPCODE_ORD = 7,
99 COMPCODE_UNORD = 8,
100 COMPCODE_UNLT = 9,
101 COMPCODE_UNEQ = 10,
102 COMPCODE_UNLE = 11,
103 COMPCODE_UNGT = 12,
104 COMPCODE_NE = 13,
105 COMPCODE_UNGE = 14,
106 COMPCODE_TRUE = 15
109 static bool negate_expr_p (tree);
110 static tree negate_expr (tree);
111 static tree split_tree (location_t, tree, tree, enum tree_code,
112 tree *, tree *, tree *, int);
113 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
114 static enum comparison_code comparison_to_compcode (enum tree_code);
115 static enum tree_code compcode_to_comparison (enum comparison_code);
116 static int operand_equal_for_comparison_p (tree, tree, tree);
117 static int twoval_comparison_p (tree, tree *, tree *, int *);
118 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
119 static tree optimize_bit_field_compare (location_t, enum tree_code,
120 tree, tree, tree);
121 static int simple_operand_p (const_tree);
122 static bool simple_operand_p_2 (tree);
123 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
124 static tree range_predecessor (tree);
125 static tree range_successor (tree);
126 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
130 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
131 static tree fold_binary_op_with_conditional_arg (location_t,
132 enum tree_code, tree,
133 tree, tree,
134 tree, tree, int);
135 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
136 static bool reorder_operands_p (const_tree, const_tree);
137 static tree fold_negate_const (tree, tree);
138 static tree fold_not_const (const_tree, tree);
139 static tree fold_relational_const (enum tree_code, tree, tree, tree);
140 static tree fold_convert_const (enum tree_code, tree, tree);
141 static tree fold_view_convert_expr (tree, tree);
142 static bool vec_cst_ctor_to_array (tree, tree *);
145 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
146 Otherwise, return LOC. */
148 static location_t
149 expr_location_or (tree t, location_t loc)
151 location_t tloc = EXPR_LOCATION (t);
152 return tloc == UNKNOWN_LOCATION ? loc : tloc;
155 /* Similar to protected_set_expr_location, but never modify x in place,
156 if location can and needs to be set, unshare it. */
158 static inline tree
159 protected_set_expr_location_unshare (tree x, location_t loc)
161 if (CAN_HAVE_LOCATION_P (x)
162 && EXPR_LOCATION (x) != loc
163 && !(TREE_CODE (x) == SAVE_EXPR
164 || TREE_CODE (x) == TARGET_EXPR
165 || TREE_CODE (x) == BIND_EXPR))
167 x = copy_node (x);
168 SET_EXPR_LOCATION (x, loc);
170 return x;
173 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
174 division and returns the quotient. Otherwise returns
175 NULL_TREE. */
177 tree
178 div_if_zero_remainder (const_tree arg1, const_tree arg2)
180 widest_int quo;
182 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
183 SIGNED, &quo))
184 return wide_int_to_tree (TREE_TYPE (arg1), quo);
186 return NULL_TREE;
189 /* This is nonzero if we should defer warnings about undefined
190 overflow. This facility exists because these warnings are a
191 special case. The code to estimate loop iterations does not want
192 to issue any warnings, since it works with expressions which do not
193 occur in user code. Various bits of cleanup code call fold(), but
194 only use the result if it has certain characteristics (e.g., is a
195 constant); that code only wants to issue a warning if the result is
196 used. */
198 static int fold_deferring_overflow_warnings;
200 /* If a warning about undefined overflow is deferred, this is the
201 warning. Note that this may cause us to turn two warnings into
202 one, but that is fine since it is sufficient to only give one
203 warning per expression. */
205 static const char* fold_deferred_overflow_warning;
207 /* If a warning about undefined overflow is deferred, this is the
208 level at which the warning should be emitted. */
210 static enum warn_strict_overflow_code fold_deferred_overflow_code;
212 /* Start deferring overflow warnings. We could use a stack here to
213 permit nested calls, but at present it is not necessary. */
215 void
216 fold_defer_overflow_warnings (void)
218 ++fold_deferring_overflow_warnings;
221 /* Stop deferring overflow warnings. If there is a pending warning,
222 and ISSUE is true, then issue the warning if appropriate. STMT is
223 the statement with which the warning should be associated (used for
224 location information); STMT may be NULL. CODE is the level of the
225 warning--a warn_strict_overflow_code value. This function will use
226 the smaller of CODE and the deferred code when deciding whether to
227 issue the warning. CODE may be zero to mean to always use the
228 deferred code. */
230 void
231 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
233 const char *warnmsg;
234 location_t locus;
236 gcc_assert (fold_deferring_overflow_warnings > 0);
237 --fold_deferring_overflow_warnings;
238 if (fold_deferring_overflow_warnings > 0)
240 if (fold_deferred_overflow_warning != NULL
241 && code != 0
242 && code < (int) fold_deferred_overflow_code)
243 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
244 return;
247 warnmsg = fold_deferred_overflow_warning;
248 fold_deferred_overflow_warning = NULL;
250 if (!issue || warnmsg == NULL)
251 return;
253 if (gimple_no_warning_p (stmt))
254 return;
256 /* Use the smallest code level when deciding to issue the
257 warning. */
258 if (code == 0 || code > (int) fold_deferred_overflow_code)
259 code = fold_deferred_overflow_code;
261 if (!issue_strict_overflow_warning (code))
262 return;
264 if (stmt == NULL)
265 locus = input_location;
266 else
267 locus = gimple_location (stmt);
268 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
271 /* Stop deferring overflow warnings, ignoring any deferred
272 warnings. */
274 void
275 fold_undefer_and_ignore_overflow_warnings (void)
277 fold_undefer_overflow_warnings (false, NULL, 0);
280 /* Whether we are deferring overflow warnings. */
282 bool
283 fold_deferring_overflow_warnings_p (void)
285 return fold_deferring_overflow_warnings > 0;
288 /* This is called when we fold something based on the fact that signed
289 overflow is undefined. */
291 void
292 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
294 if (fold_deferring_overflow_warnings > 0)
296 if (fold_deferred_overflow_warning == NULL
297 || wc < fold_deferred_overflow_code)
299 fold_deferred_overflow_warning = gmsgid;
300 fold_deferred_overflow_code = wc;
303 else if (issue_strict_overflow_warning (wc))
304 warning (OPT_Wstrict_overflow, gmsgid);
307 /* Return true if the built-in mathematical function specified by CODE
308 is odd, i.e. -f(x) == f(-x). */
310 bool
311 negate_mathfn_p (combined_fn fn)
313 switch (fn)
315 CASE_CFN_ASIN:
316 CASE_CFN_ASINH:
317 CASE_CFN_ATAN:
318 CASE_CFN_ATANH:
319 CASE_CFN_CASIN:
320 CASE_CFN_CASINH:
321 CASE_CFN_CATAN:
322 CASE_CFN_CATANH:
323 CASE_CFN_CBRT:
324 CASE_CFN_CPROJ:
325 CASE_CFN_CSIN:
326 CASE_CFN_CSINH:
327 CASE_CFN_CTAN:
328 CASE_CFN_CTANH:
329 CASE_CFN_ERF:
330 CASE_CFN_LLROUND:
331 CASE_CFN_LROUND:
332 CASE_CFN_ROUND:
333 CASE_CFN_SIN:
334 CASE_CFN_SINH:
335 CASE_CFN_TAN:
336 CASE_CFN_TANH:
337 CASE_CFN_TRUNC:
338 return true;
340 CASE_CFN_LLRINT:
341 CASE_CFN_LRINT:
342 CASE_CFN_NEARBYINT:
343 CASE_CFN_RINT:
344 return !flag_rounding_math;
346 default:
347 break;
349 return false;
352 /* Check whether we may negate an integer constant T without causing
353 overflow. */
355 bool
356 may_negate_without_overflow_p (const_tree t)
358 tree type;
360 gcc_assert (TREE_CODE (t) == INTEGER_CST);
362 type = TREE_TYPE (t);
363 if (TYPE_UNSIGNED (type))
364 return false;
366 return !wi::only_sign_bit_p (t);
369 /* Determine whether an expression T can be cheaply negated using
370 the function negate_expr without introducing undefined overflow. */
372 static bool
373 negate_expr_p (tree t)
375 tree type;
377 if (t == 0)
378 return false;
380 type = TREE_TYPE (t);
382 STRIP_SIGN_NOPS (t);
383 switch (TREE_CODE (t))
385 case INTEGER_CST:
386 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
387 return true;
389 /* Check that -CST will not overflow type. */
390 return may_negate_without_overflow_p (t);
391 case BIT_NOT_EXPR:
392 return (INTEGRAL_TYPE_P (type)
393 && TYPE_OVERFLOW_WRAPS (type));
395 case FIXED_CST:
396 return true;
398 case NEGATE_EXPR:
399 return !TYPE_OVERFLOW_SANITIZED (type);
401 case REAL_CST:
402 /* We want to canonicalize to positive real constants. Pretend
403 that only negative ones can be easily negated. */
404 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
406 case COMPLEX_CST:
407 return negate_expr_p (TREE_REALPART (t))
408 && negate_expr_p (TREE_IMAGPART (t));
410 case VECTOR_CST:
412 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
413 return true;
415 int count = TYPE_VECTOR_SUBPARTS (type), i;
417 for (i = 0; i < count; i++)
418 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
419 return false;
421 return true;
424 case COMPLEX_EXPR:
425 return negate_expr_p (TREE_OPERAND (t, 0))
426 && negate_expr_p (TREE_OPERAND (t, 1));
428 case CONJ_EXPR:
429 return negate_expr_p (TREE_OPERAND (t, 0));
431 case PLUS_EXPR:
432 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
433 || HONOR_SIGNED_ZEROS (element_mode (type))
434 || (INTEGRAL_TYPE_P (type)
435 && ! TYPE_OVERFLOW_WRAPS (type)))
436 return false;
437 /* -(A + B) -> (-B) - A. */
438 if (negate_expr_p (TREE_OPERAND (t, 1))
439 && reorder_operands_p (TREE_OPERAND (t, 0),
440 TREE_OPERAND (t, 1)))
441 return true;
442 /* -(A + B) -> (-A) - B. */
443 return negate_expr_p (TREE_OPERAND (t, 0));
445 case MINUS_EXPR:
446 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
447 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
448 && !HONOR_SIGNED_ZEROS (element_mode (type))
449 && (! INTEGRAL_TYPE_P (type)
450 || TYPE_OVERFLOW_WRAPS (type))
451 && reorder_operands_p (TREE_OPERAND (t, 0),
452 TREE_OPERAND (t, 1));
454 case MULT_EXPR:
455 if (TYPE_UNSIGNED (type))
456 break;
457 /* INT_MIN/n * n doesn't overflow while negating one operand it does
458 if n is a power of two. */
459 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
460 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
461 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
462 && ! integer_pow2p (TREE_OPERAND (t, 0)))
463 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
464 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
465 break;
467 /* Fall through. */
469 case RDIV_EXPR:
470 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
471 return negate_expr_p (TREE_OPERAND (t, 1))
472 || negate_expr_p (TREE_OPERAND (t, 0));
473 break;
475 case TRUNC_DIV_EXPR:
476 case ROUND_DIV_EXPR:
477 case EXACT_DIV_EXPR:
478 if (TYPE_UNSIGNED (type))
479 break;
480 if (negate_expr_p (TREE_OPERAND (t, 0)))
481 return true;
482 /* In general we can't negate B in A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. */
485 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
486 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
487 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
488 && ! integer_onep (TREE_OPERAND (t, 1))))
489 return negate_expr_p (TREE_OPERAND (t, 1));
490 break;
492 case NOP_EXPR:
493 /* Negate -((double)float) as (double)(-float). */
494 if (TREE_CODE (type) == REAL_TYPE)
496 tree tem = strip_float_extensions (t);
497 if (tem != t)
498 return negate_expr_p (tem);
500 break;
502 case CALL_EXPR:
503 /* Negate -f(x) as f(-x). */
504 if (negate_mathfn_p (get_call_combined_fn (t)))
505 return negate_expr_p (CALL_EXPR_ARG (t, 0));
506 break;
508 case RSHIFT_EXPR:
509 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
510 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
512 tree op1 = TREE_OPERAND (t, 1);
513 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
514 return true;
516 break;
518 default:
519 break;
521 return false;
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
527 returned. */
529 static tree
530 fold_negate_expr (location_t loc, tree t)
532 tree type = TREE_TYPE (t);
533 tree tem;
535 switch (TREE_CODE (t))
537 /* Convert - (~A) to A + 1. */
538 case BIT_NOT_EXPR:
539 if (INTEGRAL_TYPE_P (type))
540 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
541 build_one_cst (type));
542 break;
544 case INTEGER_CST:
545 tem = fold_negate_const (t, type);
546 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
547 || (ANY_INTEGRAL_TYPE_P (type)
548 && !TYPE_OVERFLOW_TRAPS (type)
549 && TYPE_OVERFLOW_WRAPS (type))
550 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
551 return tem;
552 break;
554 case REAL_CST:
555 tem = fold_negate_const (t, type);
556 return tem;
558 case FIXED_CST:
559 tem = fold_negate_const (t, type);
560 return tem;
562 case COMPLEX_CST:
564 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
565 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
566 if (rpart && ipart)
567 return build_complex (type, rpart, ipart);
569 break;
571 case VECTOR_CST:
573 int count = TYPE_VECTOR_SUBPARTS (type), i;
574 tree *elts = XALLOCAVEC (tree, count);
576 for (i = 0; i < count; i++)
578 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
579 if (elts[i] == NULL_TREE)
580 return NULL_TREE;
583 return build_vector (type, elts);
586 case COMPLEX_EXPR:
587 if (negate_expr_p (t))
588 return fold_build2_loc (loc, COMPLEX_EXPR, type,
589 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
590 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
591 break;
593 case CONJ_EXPR:
594 if (negate_expr_p (t))
595 return fold_build1_loc (loc, CONJ_EXPR, type,
596 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
597 break;
599 case NEGATE_EXPR:
600 if (!TYPE_OVERFLOW_SANITIZED (type))
601 return TREE_OPERAND (t, 0);
602 break;
604 case PLUS_EXPR:
605 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
606 && !HONOR_SIGNED_ZEROS (element_mode (type)))
608 /* -(A + B) -> (-B) - A. */
609 if (negate_expr_p (TREE_OPERAND (t, 1))
610 && reorder_operands_p (TREE_OPERAND (t, 0),
611 TREE_OPERAND (t, 1)))
613 tem = negate_expr (TREE_OPERAND (t, 1));
614 return fold_build2_loc (loc, MINUS_EXPR, type,
615 tem, TREE_OPERAND (t, 0));
618 /* -(A + B) -> (-A) - B. */
619 if (negate_expr_p (TREE_OPERAND (t, 0)))
621 tem = negate_expr (TREE_OPERAND (t, 0));
622 return fold_build2_loc (loc, MINUS_EXPR, type,
623 tem, TREE_OPERAND (t, 1));
626 break;
628 case MINUS_EXPR:
629 /* - (A - B) -> B - A */
630 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
631 && !HONOR_SIGNED_ZEROS (element_mode (type))
632 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
633 return fold_build2_loc (loc, MINUS_EXPR, type,
634 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
635 break;
637 case MULT_EXPR:
638 if (TYPE_UNSIGNED (type))
639 break;
641 /* Fall through. */
643 case RDIV_EXPR:
644 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
646 tem = TREE_OPERAND (t, 1);
647 if (negate_expr_p (tem))
648 return fold_build2_loc (loc, TREE_CODE (t), type,
649 TREE_OPERAND (t, 0), negate_expr (tem));
650 tem = TREE_OPERAND (t, 0);
651 if (negate_expr_p (tem))
652 return fold_build2_loc (loc, TREE_CODE (t), type,
653 negate_expr (tem), TREE_OPERAND (t, 1));
655 break;
657 case TRUNC_DIV_EXPR:
658 case ROUND_DIV_EXPR:
659 case EXACT_DIV_EXPR:
660 if (TYPE_UNSIGNED (type))
661 break;
662 if (negate_expr_p (TREE_OPERAND (t, 0)))
663 return fold_build2_loc (loc, TREE_CODE (t), type,
664 negate_expr (TREE_OPERAND (t, 0)),
665 TREE_OPERAND (t, 1));
666 /* In general we can't negate B in A / B, because if A is INT_MIN and
667 B is 1, we may turn this into INT_MIN / -1 which is undefined
668 and actually traps on some architectures. */
669 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
670 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
671 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
672 && ! integer_onep (TREE_OPERAND (t, 1))))
673 && negate_expr_p (TREE_OPERAND (t, 1)))
674 return fold_build2_loc (loc, TREE_CODE (t), type,
675 TREE_OPERAND (t, 0),
676 negate_expr (TREE_OPERAND (t, 1)));
677 break;
679 case NOP_EXPR:
680 /* Convert -((double)float) into (double)(-float). */
681 if (TREE_CODE (type) == REAL_TYPE)
683 tem = strip_float_extensions (t);
684 if (tem != t && negate_expr_p (tem))
685 return fold_convert_loc (loc, type, negate_expr (tem));
687 break;
689 case CALL_EXPR:
690 /* Negate -f(x) as f(-x). */
691 if (negate_mathfn_p (get_call_combined_fn (t))
692 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
694 tree fndecl, arg;
696 fndecl = get_callee_fndecl (t);
697 arg = negate_expr (CALL_EXPR_ARG (t, 0));
698 return build_call_expr_loc (loc, fndecl, 1, arg);
700 break;
702 case RSHIFT_EXPR:
703 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
704 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
706 tree op1 = TREE_OPERAND (t, 1);
707 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
709 tree ntype = TYPE_UNSIGNED (type)
710 ? signed_type_for (type)
711 : unsigned_type_for (type);
712 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
713 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
714 return fold_convert_loc (loc, type, temp);
717 break;
719 default:
720 break;
723 return NULL_TREE;
726 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
727 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
728 return NULL_TREE. */
730 static tree
731 negate_expr (tree t)
733 tree type, tem;
734 location_t loc;
736 if (t == NULL_TREE)
737 return NULL_TREE;
739 loc = EXPR_LOCATION (t);
740 type = TREE_TYPE (t);
741 STRIP_SIGN_NOPS (t);
743 tem = fold_negate_expr (loc, t);
744 if (!tem)
745 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
746 return fold_convert_loc (loc, type, tem);
749 /* Split a tree IN into a constant, literal and variable parts that could be
750 combined with CODE to make IN. "constant" means an expression with
751 TREE_CONSTANT but that isn't an actual constant. CODE must be a
752 commutative arithmetic operation. Store the constant part into *CONP,
753 the literal in *LITP and return the variable part. If a part isn't
754 present, set it to null. If the tree does not decompose in this way,
755 return the entire tree as the variable part and the other parts as null.
757 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
758 case, we negate an operand that was subtracted. Except if it is a
759 literal for which we use *MINUS_LITP instead.
761 If NEGATE_P is true, we are negating all of IN, again except a literal
762 for which we use *MINUS_LITP instead. If a variable part is of pointer
763 type, it is negated after converting to TYPE. This prevents us from
764 generating illegal MINUS pointer expression. LOC is the location of
765 the converted variable part.
767 If IN is itself a literal or constant, return it as appropriate.
769 Note that we do not guarantee that any of the three values will be the
770 same type as IN, but they will have the same signedness and mode. */
772 static tree
773 split_tree (location_t loc, tree in, tree type, enum tree_code code,
774 tree *conp, tree *litp, tree *minus_litp, int negate_p)
776 tree var = 0;
778 *conp = 0;
779 *litp = 0;
780 *minus_litp = 0;
782 /* Strip any conversions that don't change the machine mode or signedness. */
783 STRIP_SIGN_NOPS (in);
785 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
786 || TREE_CODE (in) == FIXED_CST)
787 *litp = in;
788 else if (TREE_CODE (in) == code
789 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
790 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
791 /* We can associate addition and subtraction together (even
792 though the C standard doesn't say so) for integers because
793 the value is not affected. For reals, the value might be
794 affected, so we can't. */
795 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
796 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
798 tree op0 = TREE_OPERAND (in, 0);
799 tree op1 = TREE_OPERAND (in, 1);
800 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
801 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
803 /* First see if either of the operands is a literal, then a constant. */
804 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
805 || TREE_CODE (op0) == FIXED_CST)
806 *litp = op0, op0 = 0;
807 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
808 || TREE_CODE (op1) == FIXED_CST)
809 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
811 if (op0 != 0 && TREE_CONSTANT (op0))
812 *conp = op0, op0 = 0;
813 else if (op1 != 0 && TREE_CONSTANT (op1))
814 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
816 /* If we haven't dealt with either operand, this is not a case we can
817 decompose. Otherwise, VAR is either of the ones remaining, if any. */
818 if (op0 != 0 && op1 != 0)
819 var = in;
820 else if (op0 != 0)
821 var = op0;
822 else
823 var = op1, neg_var_p = neg1_p;
825 /* Now do any needed negations. */
826 if (neg_litp_p)
827 *minus_litp = *litp, *litp = 0;
828 if (neg_conp_p)
829 *conp = negate_expr (*conp);
830 if (neg_var_p && var)
832 /* Convert to TYPE before negating. */
833 var = fold_convert_loc (loc, type, var);
834 var = negate_expr (var);
837 else if (TREE_CONSTANT (in))
838 *conp = in;
839 else if (TREE_CODE (in) == BIT_NOT_EXPR
840 && code == PLUS_EXPR)
842 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
843 when IN is constant. */
844 *minus_litp = build_one_cst (TREE_TYPE (in));
845 var = negate_expr (TREE_OPERAND (in, 0));
847 else
848 var = in;
850 if (negate_p)
852 if (*litp)
853 *minus_litp = *litp, *litp = 0;
854 else if (*minus_litp)
855 *litp = *minus_litp, *minus_litp = 0;
856 *conp = negate_expr (*conp);
857 if (var)
859 /* Convert to TYPE before negating. */
860 var = fold_convert_loc (loc, type, var);
861 var = negate_expr (var);
865 return var;
868 /* Re-associate trees split by the above function. T1 and T2 are
869 either expressions to associate or null. Return the new
870 expression, if any. LOC is the location of the new expression. If
871 we build an operation, do it in TYPE and with CODE. */
873 static tree
874 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
876 if (t1 == 0)
877 return t2;
878 else if (t2 == 0)
879 return t1;
881 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
882 try to fold this since we will have infinite recursion. But do
883 deal with any NEGATE_EXPRs. */
884 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
885 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
887 if (code == PLUS_EXPR)
889 if (TREE_CODE (t1) == NEGATE_EXPR)
890 return build2_loc (loc, MINUS_EXPR, type,
891 fold_convert_loc (loc, type, t2),
892 fold_convert_loc (loc, type,
893 TREE_OPERAND (t1, 0)));
894 else if (TREE_CODE (t2) == NEGATE_EXPR)
895 return build2_loc (loc, MINUS_EXPR, type,
896 fold_convert_loc (loc, type, t1),
897 fold_convert_loc (loc, type,
898 TREE_OPERAND (t2, 0)));
899 else if (integer_zerop (t2))
900 return fold_convert_loc (loc, type, t1);
902 else if (code == MINUS_EXPR)
904 if (integer_zerop (t2))
905 return fold_convert_loc (loc, type, t1);
908 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
909 fold_convert_loc (loc, type, t2));
912 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
913 fold_convert_loc (loc, type, t2));
916 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
917 for use in int_const_binop, size_binop and size_diffop. */
919 static bool
920 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
922 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
923 return false;
924 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
925 return false;
927 switch (code)
929 case LSHIFT_EXPR:
930 case RSHIFT_EXPR:
931 case LROTATE_EXPR:
932 case RROTATE_EXPR:
933 return true;
935 default:
936 break;
939 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
940 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
941 && TYPE_MODE (type1) == TYPE_MODE (type2);
945 /* Combine two integer constants ARG1 and ARG2 under operation CODE
946 to produce a new constant. Return NULL_TREE if we don't know how
947 to evaluate CODE at compile-time. */
949 static tree
950 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
951 int overflowable)
953 wide_int res;
954 tree t;
955 tree type = TREE_TYPE (arg1);
956 signop sign = TYPE_SIGN (type);
957 bool overflow = false;
959 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
961 switch (code)
963 case BIT_IOR_EXPR:
964 res = wi::bit_or (arg1, arg2);
965 break;
967 case BIT_XOR_EXPR:
968 res = wi::bit_xor (arg1, arg2);
969 break;
971 case BIT_AND_EXPR:
972 res = wi::bit_and (arg1, arg2);
973 break;
975 case RSHIFT_EXPR:
976 case LSHIFT_EXPR:
977 if (wi::neg_p (arg2))
979 arg2 = -arg2;
980 if (code == RSHIFT_EXPR)
981 code = LSHIFT_EXPR;
982 else
983 code = RSHIFT_EXPR;
986 if (code == RSHIFT_EXPR)
987 /* It's unclear from the C standard whether shifts can overflow.
988 The following code ignores overflow; perhaps a C standard
989 interpretation ruling is needed. */
990 res = wi::rshift (arg1, arg2, sign);
991 else
992 res = wi::lshift (arg1, arg2);
993 break;
995 case RROTATE_EXPR:
996 case LROTATE_EXPR:
997 if (wi::neg_p (arg2))
999 arg2 = -arg2;
1000 if (code == RROTATE_EXPR)
1001 code = LROTATE_EXPR;
1002 else
1003 code = RROTATE_EXPR;
1006 if (code == RROTATE_EXPR)
1007 res = wi::rrotate (arg1, arg2);
1008 else
1009 res = wi::lrotate (arg1, arg2);
1010 break;
1012 case PLUS_EXPR:
1013 res = wi::add (arg1, arg2, sign, &overflow);
1014 break;
1016 case MINUS_EXPR:
1017 res = wi::sub (arg1, arg2, sign, &overflow);
1018 break;
1020 case MULT_EXPR:
1021 res = wi::mul (arg1, arg2, sign, &overflow);
1022 break;
1024 case MULT_HIGHPART_EXPR:
1025 res = wi::mul_high (arg1, arg2, sign);
1026 break;
1028 case TRUNC_DIV_EXPR:
1029 case EXACT_DIV_EXPR:
1030 if (arg2 == 0)
1031 return NULL_TREE;
1032 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1033 break;
1035 case FLOOR_DIV_EXPR:
1036 if (arg2 == 0)
1037 return NULL_TREE;
1038 res = wi::div_floor (arg1, arg2, sign, &overflow);
1039 break;
1041 case CEIL_DIV_EXPR:
1042 if (arg2 == 0)
1043 return NULL_TREE;
1044 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1045 break;
1047 case ROUND_DIV_EXPR:
1048 if (arg2 == 0)
1049 return NULL_TREE;
1050 res = wi::div_round (arg1, arg2, sign, &overflow);
1051 break;
1053 case TRUNC_MOD_EXPR:
1054 if (arg2 == 0)
1055 return NULL_TREE;
1056 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1057 break;
1059 case FLOOR_MOD_EXPR:
1060 if (arg2 == 0)
1061 return NULL_TREE;
1062 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1063 break;
1065 case CEIL_MOD_EXPR:
1066 if (arg2 == 0)
1067 return NULL_TREE;
1068 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1069 break;
1071 case ROUND_MOD_EXPR:
1072 if (arg2 == 0)
1073 return NULL_TREE;
1074 res = wi::mod_round (arg1, arg2, sign, &overflow);
1075 break;
1077 case MIN_EXPR:
1078 res = wi::min (arg1, arg2, sign);
1079 break;
1081 case MAX_EXPR:
1082 res = wi::max (arg1, arg2, sign);
1083 break;
1085 default:
1086 return NULL_TREE;
1089 t = force_fit_type (type, res, overflowable,
1090 (((sign == SIGNED || overflowable == -1)
1091 && overflow)
1092 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1094 return t;
1097 tree
1098 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1100 return int_const_binop_1 (code, arg1, arg2, 1);
1103 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1104 constant. We assume ARG1 and ARG2 have the same data type, or at least
1105 are the same kind of constant and the same machine mode. Return zero if
1106 combining the constants is not allowed in the current operating mode. */
1108 static tree
1109 const_binop (enum tree_code code, tree arg1, tree arg2)
1111 /* Sanity check for the recursive cases. */
1112 if (!arg1 || !arg2)
1113 return NULL_TREE;
1115 STRIP_NOPS (arg1);
1116 STRIP_NOPS (arg2);
1118 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1120 if (code == POINTER_PLUS_EXPR)
1121 return int_const_binop (PLUS_EXPR,
1122 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1124 return int_const_binop (code, arg1, arg2);
1127 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1129 machine_mode mode;
1130 REAL_VALUE_TYPE d1;
1131 REAL_VALUE_TYPE d2;
1132 REAL_VALUE_TYPE value;
1133 REAL_VALUE_TYPE result;
1134 bool inexact;
1135 tree t, type;
1137 /* The following codes are handled by real_arithmetic. */
1138 switch (code)
1140 case PLUS_EXPR:
1141 case MINUS_EXPR:
1142 case MULT_EXPR:
1143 case RDIV_EXPR:
1144 case MIN_EXPR:
1145 case MAX_EXPR:
1146 break;
1148 default:
1149 return NULL_TREE;
1152 d1 = TREE_REAL_CST (arg1);
1153 d2 = TREE_REAL_CST (arg2);
1155 type = TREE_TYPE (arg1);
1156 mode = TYPE_MODE (type);
1158 /* Don't perform operation if we honor signaling NaNs and
1159 either operand is a signaling NaN. */
1160 if (HONOR_SNANS (mode)
1161 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1162 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1163 return NULL_TREE;
1165 /* Don't perform operation if it would raise a division
1166 by zero exception. */
1167 if (code == RDIV_EXPR
1168 && real_equal (&d2, &dconst0)
1169 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1170 return NULL_TREE;
1172 /* If either operand is a NaN, just return it. Otherwise, set up
1173 for floating-point trap; we return an overflow. */
1174 if (REAL_VALUE_ISNAN (d1))
1176 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1177 is off. */
1178 d1.signalling = 0;
1179 t = build_real (type, d1);
1180 return t;
1182 else if (REAL_VALUE_ISNAN (d2))
1184 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1185 is off. */
1186 d2.signalling = 0;
1187 t = build_real (type, d2);
1188 return t;
1191 inexact = real_arithmetic (&value, code, &d1, &d2);
1192 real_convert (&result, mode, &value);
1194 /* Don't constant fold this floating point operation if
1195 the result has overflowed and flag_trapping_math. */
1196 if (flag_trapping_math
1197 && MODE_HAS_INFINITIES (mode)
1198 && REAL_VALUE_ISINF (result)
1199 && !REAL_VALUE_ISINF (d1)
1200 && !REAL_VALUE_ISINF (d2))
1201 return NULL_TREE;
1203 /* Don't constant fold this floating point operation if the
1204 result may dependent upon the run-time rounding mode and
1205 flag_rounding_math is set, or if GCC's software emulation
1206 is unable to accurately represent the result. */
1207 if ((flag_rounding_math
1208 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1209 && (inexact || !real_identical (&result, &value)))
1210 return NULL_TREE;
1212 t = build_real (type, result);
1214 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1215 return t;
1218 if (TREE_CODE (arg1) == FIXED_CST)
1220 FIXED_VALUE_TYPE f1;
1221 FIXED_VALUE_TYPE f2;
1222 FIXED_VALUE_TYPE result;
1223 tree t, type;
1224 int sat_p;
1225 bool overflow_p;
1227 /* The following codes are handled by fixed_arithmetic. */
1228 switch (code)
1230 case PLUS_EXPR:
1231 case MINUS_EXPR:
1232 case MULT_EXPR:
1233 case TRUNC_DIV_EXPR:
1234 if (TREE_CODE (arg2) != FIXED_CST)
1235 return NULL_TREE;
1236 f2 = TREE_FIXED_CST (arg2);
1237 break;
1239 case LSHIFT_EXPR:
1240 case RSHIFT_EXPR:
1242 if (TREE_CODE (arg2) != INTEGER_CST)
1243 return NULL_TREE;
1244 wide_int w2 = arg2;
1245 f2.data.high = w2.elt (1);
1246 f2.data.low = w2.elt (0);
1247 f2.mode = SImode;
1249 break;
1251 default:
1252 return NULL_TREE;
1255 f1 = TREE_FIXED_CST (arg1);
1256 type = TREE_TYPE (arg1);
1257 sat_p = TYPE_SATURATING (type);
1258 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1259 t = build_fixed (type, result);
1260 /* Propagate overflow flags. */
1261 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1262 TREE_OVERFLOW (t) = 1;
1263 return t;
1266 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1268 tree type = TREE_TYPE (arg1);
1269 tree r1 = TREE_REALPART (arg1);
1270 tree i1 = TREE_IMAGPART (arg1);
1271 tree r2 = TREE_REALPART (arg2);
1272 tree i2 = TREE_IMAGPART (arg2);
1273 tree real, imag;
1275 switch (code)
1277 case PLUS_EXPR:
1278 case MINUS_EXPR:
1279 real = const_binop (code, r1, r2);
1280 imag = const_binop (code, i1, i2);
1281 break;
1283 case MULT_EXPR:
1284 if (COMPLEX_FLOAT_TYPE_P (type))
1285 return do_mpc_arg2 (arg1, arg2, type,
1286 /* do_nonfinite= */ folding_initializer,
1287 mpc_mul);
1289 real = const_binop (MINUS_EXPR,
1290 const_binop (MULT_EXPR, r1, r2),
1291 const_binop (MULT_EXPR, i1, i2));
1292 imag = const_binop (PLUS_EXPR,
1293 const_binop (MULT_EXPR, r1, i2),
1294 const_binop (MULT_EXPR, i1, r2));
1295 break;
1297 case RDIV_EXPR:
1298 if (COMPLEX_FLOAT_TYPE_P (type))
1299 return do_mpc_arg2 (arg1, arg2, type,
1300 /* do_nonfinite= */ folding_initializer,
1301 mpc_div);
1302 /* Fallthru. */
1303 case TRUNC_DIV_EXPR:
1304 case CEIL_DIV_EXPR:
1305 case FLOOR_DIV_EXPR:
1306 case ROUND_DIV_EXPR:
1307 if (flag_complex_method == 0)
1309 /* Keep this algorithm in sync with
1310 tree-complex.c:expand_complex_div_straight().
1312 Expand complex division to scalars, straightforward algorithm.
1313 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1314 t = br*br + bi*bi
1316 tree magsquared
1317 = const_binop (PLUS_EXPR,
1318 const_binop (MULT_EXPR, r2, r2),
1319 const_binop (MULT_EXPR, i2, i2));
1320 tree t1
1321 = const_binop (PLUS_EXPR,
1322 const_binop (MULT_EXPR, r1, r2),
1323 const_binop (MULT_EXPR, i1, i2));
1324 tree t2
1325 = const_binop (MINUS_EXPR,
1326 const_binop (MULT_EXPR, i1, r2),
1327 const_binop (MULT_EXPR, r1, i2));
1329 real = const_binop (code, t1, magsquared);
1330 imag = const_binop (code, t2, magsquared);
1332 else
1334 /* Keep this algorithm in sync with
1335 tree-complex.c:expand_complex_div_wide().
1337 Expand complex division to scalars, modified algorithm to minimize
1338 overflow with wide input ranges. */
1339 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1340 fold_abs_const (r2, TREE_TYPE (type)),
1341 fold_abs_const (i2, TREE_TYPE (type)));
1343 if (integer_nonzerop (compare))
1345 /* In the TRUE branch, we compute
1346 ratio = br/bi;
1347 div = (br * ratio) + bi;
1348 tr = (ar * ratio) + ai;
1349 ti = (ai * ratio) - ar;
1350 tr = tr / div;
1351 ti = ti / div; */
1352 tree ratio = const_binop (code, r2, i2);
1353 tree div = const_binop (PLUS_EXPR, i2,
1354 const_binop (MULT_EXPR, r2, ratio));
1355 real = const_binop (MULT_EXPR, r1, ratio);
1356 real = const_binop (PLUS_EXPR, real, i1);
1357 real = const_binop (code, real, div);
1359 imag = const_binop (MULT_EXPR, i1, ratio);
1360 imag = const_binop (MINUS_EXPR, imag, r1);
1361 imag = const_binop (code, imag, div);
1363 else
1365 /* In the FALSE branch, we compute
1366 ratio = d/c;
1367 divisor = (d * ratio) + c;
1368 tr = (b * ratio) + a;
1369 ti = b - (a * ratio);
1370 tr = tr / div;
1371 ti = ti / div; */
1372 tree ratio = const_binop (code, i2, r2);
1373 tree div = const_binop (PLUS_EXPR, r2,
1374 const_binop (MULT_EXPR, i2, ratio));
1376 real = const_binop (MULT_EXPR, i1, ratio);
1377 real = const_binop (PLUS_EXPR, real, r1);
1378 real = const_binop (code, real, div);
1380 imag = const_binop (MULT_EXPR, r1, ratio);
1381 imag = const_binop (MINUS_EXPR, i1, imag);
1382 imag = const_binop (code, imag, div);
1385 break;
1387 default:
1388 return NULL_TREE;
1391 if (real && imag)
1392 return build_complex (type, real, imag);
1395 if (TREE_CODE (arg1) == VECTOR_CST
1396 && TREE_CODE (arg2) == VECTOR_CST)
1398 tree type = TREE_TYPE (arg1);
1399 int count = TYPE_VECTOR_SUBPARTS (type), i;
1400 tree *elts = XALLOCAVEC (tree, count);
1402 for (i = 0; i < count; i++)
1404 tree elem1 = VECTOR_CST_ELT (arg1, i);
1405 tree elem2 = VECTOR_CST_ELT (arg2, i);
1407 elts[i] = const_binop (code, elem1, elem2);
1409 /* It is possible that const_binop cannot handle the given
1410 code and return NULL_TREE */
1411 if (elts[i] == NULL_TREE)
1412 return NULL_TREE;
1415 return build_vector (type, elts);
1418 /* Shifts allow a scalar offset for a vector. */
1419 if (TREE_CODE (arg1) == VECTOR_CST
1420 && TREE_CODE (arg2) == INTEGER_CST)
1422 tree type = TREE_TYPE (arg1);
1423 int count = TYPE_VECTOR_SUBPARTS (type), i;
1424 tree *elts = XALLOCAVEC (tree, count);
1426 for (i = 0; i < count; i++)
1428 tree elem1 = VECTOR_CST_ELT (arg1, i);
1430 elts[i] = const_binop (code, elem1, arg2);
1432 /* It is possible that const_binop cannot handle the given
1433 code and return NULL_TREE. */
1434 if (elts[i] == NULL_TREE)
1435 return NULL_TREE;
1438 return build_vector (type, elts);
1440 return NULL_TREE;
1443 /* Overload that adds a TYPE parameter to be able to dispatch
1444 to fold_relational_const. */
1446 tree
1447 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1449 if (TREE_CODE_CLASS (code) == tcc_comparison)
1450 return fold_relational_const (code, type, arg1, arg2);
1452 /* ??? Until we make the const_binop worker take the type of the
1453 result as argument put those cases that need it here. */
1454 switch (code)
1456 case COMPLEX_EXPR:
1457 if ((TREE_CODE (arg1) == REAL_CST
1458 && TREE_CODE (arg2) == REAL_CST)
1459 || (TREE_CODE (arg1) == INTEGER_CST
1460 && TREE_CODE (arg2) == INTEGER_CST))
1461 return build_complex (type, arg1, arg2);
1462 return NULL_TREE;
1464 case VEC_PACK_TRUNC_EXPR:
1465 case VEC_PACK_FIX_TRUNC_EXPR:
1467 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1468 tree *elts;
1470 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1471 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1472 if (TREE_CODE (arg1) != VECTOR_CST
1473 || TREE_CODE (arg2) != VECTOR_CST)
1474 return NULL_TREE;
1476 elts = XALLOCAVEC (tree, nelts);
1477 if (!vec_cst_ctor_to_array (arg1, elts)
1478 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1479 return NULL_TREE;
1481 for (i = 0; i < nelts; i++)
1483 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1484 ? NOP_EXPR : FIX_TRUNC_EXPR,
1485 TREE_TYPE (type), elts[i]);
1486 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1487 return NULL_TREE;
1490 return build_vector (type, elts);
1493 case VEC_WIDEN_MULT_LO_EXPR:
1494 case VEC_WIDEN_MULT_HI_EXPR:
1495 case VEC_WIDEN_MULT_EVEN_EXPR:
1496 case VEC_WIDEN_MULT_ODD_EXPR:
1498 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1499 unsigned int out, ofs, scale;
1500 tree *elts;
1502 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1503 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1504 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1505 return NULL_TREE;
1507 elts = XALLOCAVEC (tree, nelts * 4);
1508 if (!vec_cst_ctor_to_array (arg1, elts)
1509 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1510 return NULL_TREE;
1512 if (code == VEC_WIDEN_MULT_LO_EXPR)
1513 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1514 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1515 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1516 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1517 scale = 1, ofs = 0;
1518 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1519 scale = 1, ofs = 1;
1521 for (out = 0; out < nelts; out++)
1523 unsigned int in1 = (out << scale) + ofs;
1524 unsigned int in2 = in1 + nelts * 2;
1525 tree t1, t2;
1527 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1528 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1530 if (t1 == NULL_TREE || t2 == NULL_TREE)
1531 return NULL_TREE;
1532 elts[out] = const_binop (MULT_EXPR, t1, t2);
1533 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1534 return NULL_TREE;
1537 return build_vector (type, elts);
1540 default:;
1543 if (TREE_CODE_CLASS (code) != tcc_binary)
1544 return NULL_TREE;
1546 /* Make sure type and arg0 have the same saturating flag. */
1547 gcc_checking_assert (TYPE_SATURATING (type)
1548 == TYPE_SATURATING (TREE_TYPE (arg1)));
1550 return const_binop (code, arg1, arg2);
1553 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1554 Return zero if computing the constants is not possible. */
1556 tree
1557 const_unop (enum tree_code code, tree type, tree arg0)
1559 /* Don't perform the operation, other than NEGATE and ABS, if
1560 flag_signaling_nans is on and the operand is a signaling NaN. */
1561 if (TREE_CODE (arg0) == REAL_CST
1562 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1563 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1564 && code != NEGATE_EXPR
1565 && code != ABS_EXPR)
1566 return NULL_TREE;
1568 switch (code)
1570 CASE_CONVERT:
1571 case FLOAT_EXPR:
1572 case FIX_TRUNC_EXPR:
1573 case FIXED_CONVERT_EXPR:
1574 return fold_convert_const (code, type, arg0);
1576 case ADDR_SPACE_CONVERT_EXPR:
1577 /* If the source address is 0, and the source address space
1578 cannot have a valid object at 0, fold to dest type null. */
1579 if (integer_zerop (arg0)
1580 && !(targetm.addr_space.zero_address_valid
1581 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1582 return fold_convert_const (code, type, arg0);
1583 break;
1585 case VIEW_CONVERT_EXPR:
1586 return fold_view_convert_expr (type, arg0);
1588 case NEGATE_EXPR:
1590 /* Can't call fold_negate_const directly here as that doesn't
1591 handle all cases and we might not be able to negate some
1592 constants. */
1593 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1594 if (tem && CONSTANT_CLASS_P (tem))
1595 return tem;
1596 break;
1599 case ABS_EXPR:
1600 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1601 return fold_abs_const (arg0, type);
1602 break;
1604 case CONJ_EXPR:
1605 if (TREE_CODE (arg0) == COMPLEX_CST)
1607 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1608 TREE_TYPE (type));
1609 return build_complex (type, TREE_REALPART (arg0), ipart);
1611 break;
1613 case BIT_NOT_EXPR:
1614 if (TREE_CODE (arg0) == INTEGER_CST)
1615 return fold_not_const (arg0, type);
1616 /* Perform BIT_NOT_EXPR on each element individually. */
1617 else if (TREE_CODE (arg0) == VECTOR_CST)
1619 tree *elements;
1620 tree elem;
1621 unsigned count = VECTOR_CST_NELTS (arg0), i;
1623 elements = XALLOCAVEC (tree, count);
1624 for (i = 0; i < count; i++)
1626 elem = VECTOR_CST_ELT (arg0, i);
1627 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1628 if (elem == NULL_TREE)
1629 break;
1630 elements[i] = elem;
1632 if (i == count)
1633 return build_vector (type, elements);
1635 break;
1637 case TRUTH_NOT_EXPR:
1638 if (TREE_CODE (arg0) == INTEGER_CST)
1639 return constant_boolean_node (integer_zerop (arg0), type);
1640 break;
1642 case REALPART_EXPR:
1643 if (TREE_CODE (arg0) == COMPLEX_CST)
1644 return fold_convert (type, TREE_REALPART (arg0));
1645 break;
1647 case IMAGPART_EXPR:
1648 if (TREE_CODE (arg0) == COMPLEX_CST)
1649 return fold_convert (type, TREE_IMAGPART (arg0));
1650 break;
1652 case VEC_UNPACK_LO_EXPR:
1653 case VEC_UNPACK_HI_EXPR:
1654 case VEC_UNPACK_FLOAT_LO_EXPR:
1655 case VEC_UNPACK_FLOAT_HI_EXPR:
1657 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1658 tree *elts;
1659 enum tree_code subcode;
1661 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1662 if (TREE_CODE (arg0) != VECTOR_CST)
1663 return NULL_TREE;
1665 elts = XALLOCAVEC (tree, nelts * 2);
1666 if (!vec_cst_ctor_to_array (arg0, elts))
1667 return NULL_TREE;
1669 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1670 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1671 elts += nelts;
1673 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1674 subcode = NOP_EXPR;
1675 else
1676 subcode = FLOAT_EXPR;
1678 for (i = 0; i < nelts; i++)
1680 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1681 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1682 return NULL_TREE;
1685 return build_vector (type, elts);
1688 case REDUC_MIN_EXPR:
1689 case REDUC_MAX_EXPR:
1690 case REDUC_PLUS_EXPR:
1692 unsigned int nelts, i;
1693 tree *elts;
1694 enum tree_code subcode;
1696 if (TREE_CODE (arg0) != VECTOR_CST)
1697 return NULL_TREE;
1698 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1700 elts = XALLOCAVEC (tree, nelts);
1701 if (!vec_cst_ctor_to_array (arg0, elts))
1702 return NULL_TREE;
1704 switch (code)
1706 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1707 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1708 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1709 default: gcc_unreachable ();
1712 for (i = 1; i < nelts; i++)
1714 elts[0] = const_binop (subcode, elts[0], elts[i]);
1715 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1716 return NULL_TREE;
1719 return elts[0];
1722 default:
1723 break;
1726 return NULL_TREE;
1729 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1730 indicates which particular sizetype to create. */
1732 tree
1733 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1735 return build_int_cst (sizetype_tab[(int) kind], number);
1738 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1739 is a tree code. The type of the result is taken from the operands.
1740 Both must be equivalent integer types, ala int_binop_types_match_p.
1741 If the operands are constant, so is the result. */
1743 tree
1744 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1746 tree type = TREE_TYPE (arg0);
1748 if (arg0 == error_mark_node || arg1 == error_mark_node)
1749 return error_mark_node;
1751 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1752 TREE_TYPE (arg1)));
1754 /* Handle the special case of two integer constants faster. */
1755 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1757 /* And some specific cases even faster than that. */
1758 if (code == PLUS_EXPR)
1760 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1761 return arg1;
1762 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1763 return arg0;
1765 else if (code == MINUS_EXPR)
1767 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1768 return arg0;
1770 else if (code == MULT_EXPR)
1772 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1773 return arg1;
1776 /* Handle general case of two integer constants. For sizetype
1777 constant calculations we always want to know about overflow,
1778 even in the unsigned case. */
1779 return int_const_binop_1 (code, arg0, arg1, -1);
1782 return fold_build2_loc (loc, code, type, arg0, arg1);
1785 /* Given two values, either both of sizetype or both of bitsizetype,
1786 compute the difference between the two values. Return the value
1787 in signed type corresponding to the type of the operands. */
1789 tree
1790 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1792 tree type = TREE_TYPE (arg0);
1793 tree ctype;
1795 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1796 TREE_TYPE (arg1)));
1798 /* If the type is already signed, just do the simple thing. */
1799 if (!TYPE_UNSIGNED (type))
1800 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1802 if (type == sizetype)
1803 ctype = ssizetype;
1804 else if (type == bitsizetype)
1805 ctype = sbitsizetype;
1806 else
1807 ctype = signed_type_for (type);
1809 /* If either operand is not a constant, do the conversions to the signed
1810 type and subtract. The hardware will do the right thing with any
1811 overflow in the subtraction. */
1812 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1813 return size_binop_loc (loc, MINUS_EXPR,
1814 fold_convert_loc (loc, ctype, arg0),
1815 fold_convert_loc (loc, ctype, arg1));
1817 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1818 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1819 overflow) and negate (which can't either). Special-case a result
1820 of zero while we're here. */
1821 if (tree_int_cst_equal (arg0, arg1))
1822 return build_int_cst (ctype, 0);
1823 else if (tree_int_cst_lt (arg1, arg0))
1824 return fold_convert_loc (loc, ctype,
1825 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1826 else
1827 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1828 fold_convert_loc (loc, ctype,
1829 size_binop_loc (loc,
1830 MINUS_EXPR,
1831 arg1, arg0)));
1834 /* A subroutine of fold_convert_const handling conversions of an
1835 INTEGER_CST to another integer type. */
1837 static tree
1838 fold_convert_const_int_from_int (tree type, const_tree arg1)
1840 /* Given an integer constant, make new constant with new type,
1841 appropriately sign-extended or truncated. Use widest_int
1842 so that any extension is done according ARG1's type. */
1843 return force_fit_type (type, wi::to_widest (arg1),
1844 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1845 TREE_OVERFLOW (arg1));
1848 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1849 to an integer type. */
1851 static tree
1852 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1854 bool overflow = false;
1855 tree t;
1857 /* The following code implements the floating point to integer
1858 conversion rules required by the Java Language Specification,
1859 that IEEE NaNs are mapped to zero and values that overflow
1860 the target precision saturate, i.e. values greater than
1861 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1862 are mapped to INT_MIN. These semantics are allowed by the
1863 C and C++ standards that simply state that the behavior of
1864 FP-to-integer conversion is unspecified upon overflow. */
1866 wide_int val;
1867 REAL_VALUE_TYPE r;
1868 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1870 switch (code)
1872 case FIX_TRUNC_EXPR:
1873 real_trunc (&r, VOIDmode, &x);
1874 break;
1876 default:
1877 gcc_unreachable ();
1880 /* If R is NaN, return zero and show we have an overflow. */
1881 if (REAL_VALUE_ISNAN (r))
1883 overflow = true;
1884 val = wi::zero (TYPE_PRECISION (type));
1887 /* See if R is less than the lower bound or greater than the
1888 upper bound. */
1890 if (! overflow)
1892 tree lt = TYPE_MIN_VALUE (type);
1893 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1894 if (real_less (&r, &l))
1896 overflow = true;
1897 val = lt;
1901 if (! overflow)
1903 tree ut = TYPE_MAX_VALUE (type);
1904 if (ut)
1906 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1907 if (real_less (&u, &r))
1909 overflow = true;
1910 val = ut;
1915 if (! overflow)
1916 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1918 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1919 return t;
1922 /* A subroutine of fold_convert_const handling conversions of a
1923 FIXED_CST to an integer type. */
1925 static tree
1926 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1928 tree t;
1929 double_int temp, temp_trunc;
1930 unsigned int mode;
1932 /* Right shift FIXED_CST to temp by fbit. */
1933 temp = TREE_FIXED_CST (arg1).data;
1934 mode = TREE_FIXED_CST (arg1).mode;
1935 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1937 temp = temp.rshift (GET_MODE_FBIT (mode),
1938 HOST_BITS_PER_DOUBLE_INT,
1939 SIGNED_FIXED_POINT_MODE_P (mode));
1941 /* Left shift temp to temp_trunc by fbit. */
1942 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1943 HOST_BITS_PER_DOUBLE_INT,
1944 SIGNED_FIXED_POINT_MODE_P (mode));
1946 else
1948 temp = double_int_zero;
1949 temp_trunc = double_int_zero;
1952 /* If FIXED_CST is negative, we need to round the value toward 0.
1953 By checking if the fractional bits are not zero to add 1 to temp. */
1954 if (SIGNED_FIXED_POINT_MODE_P (mode)
1955 && temp_trunc.is_negative ()
1956 && TREE_FIXED_CST (arg1).data != temp_trunc)
1957 temp += double_int_one;
1959 /* Given a fixed-point constant, make new constant with new type,
1960 appropriately sign-extended or truncated. */
1961 t = force_fit_type (type, temp, -1,
1962 (temp.is_negative ()
1963 && (TYPE_UNSIGNED (type)
1964 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1965 | TREE_OVERFLOW (arg1));
1967 return t;
1970 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1971 to another floating point type. */
1973 static tree
1974 fold_convert_const_real_from_real (tree type, const_tree arg1)
1976 REAL_VALUE_TYPE value;
1977 tree t;
1979 /* Don't perform the operation if flag_signaling_nans is on
1980 and the operand is a signaling NaN. */
1981 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1982 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1983 return NULL_TREE;
1985 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1986 t = build_real (type, value);
1988 /* If converting an infinity or NAN to a representation that doesn't
1989 have one, set the overflow bit so that we can produce some kind of
1990 error message at the appropriate point if necessary. It's not the
1991 most user-friendly message, but it's better than nothing. */
1992 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1993 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1994 TREE_OVERFLOW (t) = 1;
1995 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1996 && !MODE_HAS_NANS (TYPE_MODE (type)))
1997 TREE_OVERFLOW (t) = 1;
1998 /* Regular overflow, conversion produced an infinity in a mode that
1999 can't represent them. */
2000 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2001 && REAL_VALUE_ISINF (value)
2002 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2003 TREE_OVERFLOW (t) = 1;
2004 else
2005 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2006 return t;
2009 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2010 to a floating point type. */
2012 static tree
2013 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2015 REAL_VALUE_TYPE value;
2016 tree t;
2018 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2019 t = build_real (type, value);
2021 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2022 return t;
2025 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2026 to another fixed-point type. */
2028 static tree
2029 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2031 FIXED_VALUE_TYPE value;
2032 tree t;
2033 bool overflow_p;
2035 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2036 TYPE_SATURATING (type));
2037 t = build_fixed (type, value);
2039 /* Propagate overflow flags. */
2040 if (overflow_p | TREE_OVERFLOW (arg1))
2041 TREE_OVERFLOW (t) = 1;
2042 return t;
2045 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2046 to a fixed-point type. */
2048 static tree
2049 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2051 FIXED_VALUE_TYPE value;
2052 tree t;
2053 bool overflow_p;
2054 double_int di;
2056 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2058 di.low = TREE_INT_CST_ELT (arg1, 0);
2059 if (TREE_INT_CST_NUNITS (arg1) == 1)
2060 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2061 else
2062 di.high = TREE_INT_CST_ELT (arg1, 1);
2064 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2065 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2066 TYPE_SATURATING (type));
2067 t = build_fixed (type, value);
2069 /* Propagate overflow flags. */
2070 if (overflow_p | TREE_OVERFLOW (arg1))
2071 TREE_OVERFLOW (t) = 1;
2072 return t;
2075 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2076 to a fixed-point type. */
2078 static tree
2079 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2081 FIXED_VALUE_TYPE value;
2082 tree t;
2083 bool overflow_p;
2085 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2086 &TREE_REAL_CST (arg1),
2087 TYPE_SATURATING (type));
2088 t = build_fixed (type, value);
2090 /* Propagate overflow flags. */
2091 if (overflow_p | TREE_OVERFLOW (arg1))
2092 TREE_OVERFLOW (t) = 1;
2093 return t;
2096 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2097 type TYPE. If no simplification can be done return NULL_TREE. */
2099 static tree
2100 fold_convert_const (enum tree_code code, tree type, tree arg1)
2102 if (TREE_TYPE (arg1) == type)
2103 return arg1;
2105 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2106 || TREE_CODE (type) == OFFSET_TYPE)
2108 if (TREE_CODE (arg1) == INTEGER_CST)
2109 return fold_convert_const_int_from_int (type, arg1);
2110 else if (TREE_CODE (arg1) == REAL_CST)
2111 return fold_convert_const_int_from_real (code, type, arg1);
2112 else if (TREE_CODE (arg1) == FIXED_CST)
2113 return fold_convert_const_int_from_fixed (type, arg1);
2115 else if (TREE_CODE (type) == REAL_TYPE)
2117 if (TREE_CODE (arg1) == INTEGER_CST)
2118 return build_real_from_int_cst (type, arg1);
2119 else if (TREE_CODE (arg1) == REAL_CST)
2120 return fold_convert_const_real_from_real (type, arg1);
2121 else if (TREE_CODE (arg1) == FIXED_CST)
2122 return fold_convert_const_real_from_fixed (type, arg1);
2124 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2126 if (TREE_CODE (arg1) == FIXED_CST)
2127 return fold_convert_const_fixed_from_fixed (type, arg1);
2128 else if (TREE_CODE (arg1) == INTEGER_CST)
2129 return fold_convert_const_fixed_from_int (type, arg1);
2130 else if (TREE_CODE (arg1) == REAL_CST)
2131 return fold_convert_const_fixed_from_real (type, arg1);
2133 else if (TREE_CODE (type) == VECTOR_TYPE)
2135 if (TREE_CODE (arg1) == VECTOR_CST
2136 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2138 int len = TYPE_VECTOR_SUBPARTS (type);
2139 tree elttype = TREE_TYPE (type);
2140 tree *v = XALLOCAVEC (tree, len);
2141 for (int i = 0; i < len; ++i)
2143 tree elt = VECTOR_CST_ELT (arg1, i);
2144 tree cvt = fold_convert_const (code, elttype, elt);
2145 if (cvt == NULL_TREE)
2146 return NULL_TREE;
2147 v[i] = cvt;
2149 return build_vector (type, v);
2152 return NULL_TREE;
2155 /* Construct a vector of zero elements of vector type TYPE. */
2157 static tree
2158 build_zero_vector (tree type)
2160 tree t;
2162 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2163 return build_vector_from_val (type, t);
2166 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2168 bool
2169 fold_convertible_p (const_tree type, const_tree arg)
2171 tree orig = TREE_TYPE (arg);
2173 if (type == orig)
2174 return true;
2176 if (TREE_CODE (arg) == ERROR_MARK
2177 || TREE_CODE (type) == ERROR_MARK
2178 || TREE_CODE (orig) == ERROR_MARK)
2179 return false;
2181 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2182 return true;
2184 switch (TREE_CODE (type))
2186 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2187 case POINTER_TYPE: case REFERENCE_TYPE:
2188 case OFFSET_TYPE:
2189 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2190 || TREE_CODE (orig) == OFFSET_TYPE);
2192 case REAL_TYPE:
2193 case FIXED_POINT_TYPE:
2194 case VECTOR_TYPE:
2195 case VOID_TYPE:
2196 return TREE_CODE (type) == TREE_CODE (orig);
2198 default:
2199 return false;
2203 /* Convert expression ARG to type TYPE. Used by the middle-end for
2204 simple conversions in preference to calling the front-end's convert. */
2206 tree
2207 fold_convert_loc (location_t loc, tree type, tree arg)
2209 tree orig = TREE_TYPE (arg);
2210 tree tem;
2212 if (type == orig)
2213 return arg;
2215 if (TREE_CODE (arg) == ERROR_MARK
2216 || TREE_CODE (type) == ERROR_MARK
2217 || TREE_CODE (orig) == ERROR_MARK)
2218 return error_mark_node;
2220 switch (TREE_CODE (type))
2222 case POINTER_TYPE:
2223 case REFERENCE_TYPE:
2224 /* Handle conversions between pointers to different address spaces. */
2225 if (POINTER_TYPE_P (orig)
2226 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2227 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2228 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2229 /* fall through */
2231 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2232 case OFFSET_TYPE:
2233 if (TREE_CODE (arg) == INTEGER_CST)
2235 tem = fold_convert_const (NOP_EXPR, type, arg);
2236 if (tem != NULL_TREE)
2237 return tem;
2239 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2240 || TREE_CODE (orig) == OFFSET_TYPE)
2241 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2242 if (TREE_CODE (orig) == COMPLEX_TYPE)
2243 return fold_convert_loc (loc, type,
2244 fold_build1_loc (loc, REALPART_EXPR,
2245 TREE_TYPE (orig), arg));
2246 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2247 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2248 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2250 case REAL_TYPE:
2251 if (TREE_CODE (arg) == INTEGER_CST)
2253 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2254 if (tem != NULL_TREE)
2255 return tem;
2257 else if (TREE_CODE (arg) == REAL_CST)
2259 tem = fold_convert_const (NOP_EXPR, type, arg);
2260 if (tem != NULL_TREE)
2261 return tem;
2263 else if (TREE_CODE (arg) == FIXED_CST)
2265 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2266 if (tem != NULL_TREE)
2267 return tem;
2270 switch (TREE_CODE (orig))
2272 case INTEGER_TYPE:
2273 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2274 case POINTER_TYPE: case REFERENCE_TYPE:
2275 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2277 case REAL_TYPE:
2278 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2280 case FIXED_POINT_TYPE:
2281 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2283 case COMPLEX_TYPE:
2284 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2285 return fold_convert_loc (loc, type, tem);
2287 default:
2288 gcc_unreachable ();
2291 case FIXED_POINT_TYPE:
2292 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2293 || TREE_CODE (arg) == REAL_CST)
2295 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2296 if (tem != NULL_TREE)
2297 goto fold_convert_exit;
2300 switch (TREE_CODE (orig))
2302 case FIXED_POINT_TYPE:
2303 case INTEGER_TYPE:
2304 case ENUMERAL_TYPE:
2305 case BOOLEAN_TYPE:
2306 case REAL_TYPE:
2307 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2309 case COMPLEX_TYPE:
2310 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2311 return fold_convert_loc (loc, type, tem);
2313 default:
2314 gcc_unreachable ();
2317 case COMPLEX_TYPE:
2318 switch (TREE_CODE (orig))
2320 case INTEGER_TYPE:
2321 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2322 case POINTER_TYPE: case REFERENCE_TYPE:
2323 case REAL_TYPE:
2324 case FIXED_POINT_TYPE:
2325 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2326 fold_convert_loc (loc, TREE_TYPE (type), arg),
2327 fold_convert_loc (loc, TREE_TYPE (type),
2328 integer_zero_node));
2329 case COMPLEX_TYPE:
2331 tree rpart, ipart;
2333 if (TREE_CODE (arg) == COMPLEX_EXPR)
2335 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2336 TREE_OPERAND (arg, 0));
2337 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2338 TREE_OPERAND (arg, 1));
2339 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2342 arg = save_expr (arg);
2343 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2344 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2345 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2346 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2347 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2350 default:
2351 gcc_unreachable ();
2354 case VECTOR_TYPE:
2355 if (integer_zerop (arg))
2356 return build_zero_vector (type);
2357 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2358 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2359 || TREE_CODE (orig) == VECTOR_TYPE);
2360 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2362 case VOID_TYPE:
2363 tem = fold_ignored_result (arg);
2364 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2366 default:
2367 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2368 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2369 gcc_unreachable ();
2371 fold_convert_exit:
2372 protected_set_expr_location_unshare (tem, loc);
2373 return tem;
2376 /* Return false if expr can be assumed not to be an lvalue, true
2377 otherwise. */
2379 static bool
2380 maybe_lvalue_p (const_tree x)
2382 /* We only need to wrap lvalue tree codes. */
2383 switch (TREE_CODE (x))
2385 case VAR_DECL:
2386 case PARM_DECL:
2387 case RESULT_DECL:
2388 case LABEL_DECL:
2389 case FUNCTION_DECL:
2390 case SSA_NAME:
2392 case COMPONENT_REF:
2393 case MEM_REF:
2394 case INDIRECT_REF:
2395 case ARRAY_REF:
2396 case ARRAY_RANGE_REF:
2397 case BIT_FIELD_REF:
2398 case OBJ_TYPE_REF:
2400 case REALPART_EXPR:
2401 case IMAGPART_EXPR:
2402 case PREINCREMENT_EXPR:
2403 case PREDECREMENT_EXPR:
2404 case SAVE_EXPR:
2405 case TRY_CATCH_EXPR:
2406 case WITH_CLEANUP_EXPR:
2407 case COMPOUND_EXPR:
2408 case MODIFY_EXPR:
2409 case TARGET_EXPR:
2410 case COND_EXPR:
2411 case BIND_EXPR:
2412 break;
2414 default:
2415 /* Assume the worst for front-end tree codes. */
2416 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2417 break;
2418 return false;
2421 return true;
2424 /* Return an expr equal to X but certainly not valid as an lvalue. */
2426 tree
2427 non_lvalue_loc (location_t loc, tree x)
2429 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2430 us. */
2431 if (in_gimple_form)
2432 return x;
2434 if (! maybe_lvalue_p (x))
2435 return x;
2436 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2439 /* When pedantic, return an expr equal to X but certainly not valid as a
2440 pedantic lvalue. Otherwise, return X. */
2442 static tree
2443 pedantic_non_lvalue_loc (location_t loc, tree x)
2445 return protected_set_expr_location_unshare (x, loc);
2448 /* Given a tree comparison code, return the code that is the logical inverse.
2449 It is generally not safe to do this for floating-point comparisons, except
2450 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2451 ERROR_MARK in this case. */
2453 enum tree_code
2454 invert_tree_comparison (enum tree_code code, bool honor_nans)
2456 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2457 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2458 return ERROR_MARK;
2460 switch (code)
2462 case EQ_EXPR:
2463 return NE_EXPR;
2464 case NE_EXPR:
2465 return EQ_EXPR;
2466 case GT_EXPR:
2467 return honor_nans ? UNLE_EXPR : LE_EXPR;
2468 case GE_EXPR:
2469 return honor_nans ? UNLT_EXPR : LT_EXPR;
2470 case LT_EXPR:
2471 return honor_nans ? UNGE_EXPR : GE_EXPR;
2472 case LE_EXPR:
2473 return honor_nans ? UNGT_EXPR : GT_EXPR;
2474 case LTGT_EXPR:
2475 return UNEQ_EXPR;
2476 case UNEQ_EXPR:
2477 return LTGT_EXPR;
2478 case UNGT_EXPR:
2479 return LE_EXPR;
2480 case UNGE_EXPR:
2481 return LT_EXPR;
2482 case UNLT_EXPR:
2483 return GE_EXPR;
2484 case UNLE_EXPR:
2485 return GT_EXPR;
2486 case ORDERED_EXPR:
2487 return UNORDERED_EXPR;
2488 case UNORDERED_EXPR:
2489 return ORDERED_EXPR;
2490 default:
2491 gcc_unreachable ();
2495 /* Similar, but return the comparison that results if the operands are
2496 swapped. This is safe for floating-point. */
2498 enum tree_code
2499 swap_tree_comparison (enum tree_code code)
2501 switch (code)
2503 case EQ_EXPR:
2504 case NE_EXPR:
2505 case ORDERED_EXPR:
2506 case UNORDERED_EXPR:
2507 case LTGT_EXPR:
2508 case UNEQ_EXPR:
2509 return code;
2510 case GT_EXPR:
2511 return LT_EXPR;
2512 case GE_EXPR:
2513 return LE_EXPR;
2514 case LT_EXPR:
2515 return GT_EXPR;
2516 case LE_EXPR:
2517 return GE_EXPR;
2518 case UNGT_EXPR:
2519 return UNLT_EXPR;
2520 case UNGE_EXPR:
2521 return UNLE_EXPR;
2522 case UNLT_EXPR:
2523 return UNGT_EXPR;
2524 case UNLE_EXPR:
2525 return UNGE_EXPR;
2526 default:
2527 gcc_unreachable ();
2532 /* Convert a comparison tree code from an enum tree_code representation
2533 into a compcode bit-based encoding. This function is the inverse of
2534 compcode_to_comparison. */
2536 static enum comparison_code
2537 comparison_to_compcode (enum tree_code code)
2539 switch (code)
2541 case LT_EXPR:
2542 return COMPCODE_LT;
2543 case EQ_EXPR:
2544 return COMPCODE_EQ;
2545 case LE_EXPR:
2546 return COMPCODE_LE;
2547 case GT_EXPR:
2548 return COMPCODE_GT;
2549 case NE_EXPR:
2550 return COMPCODE_NE;
2551 case GE_EXPR:
2552 return COMPCODE_GE;
2553 case ORDERED_EXPR:
2554 return COMPCODE_ORD;
2555 case UNORDERED_EXPR:
2556 return COMPCODE_UNORD;
2557 case UNLT_EXPR:
2558 return COMPCODE_UNLT;
2559 case UNEQ_EXPR:
2560 return COMPCODE_UNEQ;
2561 case UNLE_EXPR:
2562 return COMPCODE_UNLE;
2563 case UNGT_EXPR:
2564 return COMPCODE_UNGT;
2565 case LTGT_EXPR:
2566 return COMPCODE_LTGT;
2567 case UNGE_EXPR:
2568 return COMPCODE_UNGE;
2569 default:
2570 gcc_unreachable ();
2574 /* Convert a compcode bit-based encoding of a comparison operator back
2575 to GCC's enum tree_code representation. This function is the
2576 inverse of comparison_to_compcode. */
2578 static enum tree_code
2579 compcode_to_comparison (enum comparison_code code)
2581 switch (code)
2583 case COMPCODE_LT:
2584 return LT_EXPR;
2585 case COMPCODE_EQ:
2586 return EQ_EXPR;
2587 case COMPCODE_LE:
2588 return LE_EXPR;
2589 case COMPCODE_GT:
2590 return GT_EXPR;
2591 case COMPCODE_NE:
2592 return NE_EXPR;
2593 case COMPCODE_GE:
2594 return GE_EXPR;
2595 case COMPCODE_ORD:
2596 return ORDERED_EXPR;
2597 case COMPCODE_UNORD:
2598 return UNORDERED_EXPR;
2599 case COMPCODE_UNLT:
2600 return UNLT_EXPR;
2601 case COMPCODE_UNEQ:
2602 return UNEQ_EXPR;
2603 case COMPCODE_UNLE:
2604 return UNLE_EXPR;
2605 case COMPCODE_UNGT:
2606 return UNGT_EXPR;
2607 case COMPCODE_LTGT:
2608 return LTGT_EXPR;
2609 case COMPCODE_UNGE:
2610 return UNGE_EXPR;
2611 default:
2612 gcc_unreachable ();
2616 /* Return a tree for the comparison which is the combination of
2617 doing the AND or OR (depending on CODE) of the two operations LCODE
2618 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2619 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2620 if this makes the transformation invalid. */
2622 tree
2623 combine_comparisons (location_t loc,
2624 enum tree_code code, enum tree_code lcode,
2625 enum tree_code rcode, tree truth_type,
2626 tree ll_arg, tree lr_arg)
2628 bool honor_nans = HONOR_NANS (ll_arg);
2629 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2630 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2631 int compcode;
2633 switch (code)
2635 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2636 compcode = lcompcode & rcompcode;
2637 break;
2639 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2640 compcode = lcompcode | rcompcode;
2641 break;
2643 default:
2644 return NULL_TREE;
2647 if (!honor_nans)
2649 /* Eliminate unordered comparisons, as well as LTGT and ORD
2650 which are not used unless the mode has NaNs. */
2651 compcode &= ~COMPCODE_UNORD;
2652 if (compcode == COMPCODE_LTGT)
2653 compcode = COMPCODE_NE;
2654 else if (compcode == COMPCODE_ORD)
2655 compcode = COMPCODE_TRUE;
2657 else if (flag_trapping_math)
2659 /* Check that the original operation and the optimized ones will trap
2660 under the same condition. */
2661 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2662 && (lcompcode != COMPCODE_EQ)
2663 && (lcompcode != COMPCODE_ORD);
2664 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2665 && (rcompcode != COMPCODE_EQ)
2666 && (rcompcode != COMPCODE_ORD);
2667 bool trap = (compcode & COMPCODE_UNORD) == 0
2668 && (compcode != COMPCODE_EQ)
2669 && (compcode != COMPCODE_ORD);
2671 /* In a short-circuited boolean expression the LHS might be
2672 such that the RHS, if evaluated, will never trap. For
2673 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2674 if neither x nor y is NaN. (This is a mixed blessing: for
2675 example, the expression above will never trap, hence
2676 optimizing it to x < y would be invalid). */
2677 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2678 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2679 rtrap = false;
2681 /* If the comparison was short-circuited, and only the RHS
2682 trapped, we may now generate a spurious trap. */
2683 if (rtrap && !ltrap
2684 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2685 return NULL_TREE;
2687 /* If we changed the conditions that cause a trap, we lose. */
2688 if ((ltrap || rtrap) != trap)
2689 return NULL_TREE;
2692 if (compcode == COMPCODE_TRUE)
2693 return constant_boolean_node (true, truth_type);
2694 else if (compcode == COMPCODE_FALSE)
2695 return constant_boolean_node (false, truth_type);
2696 else
2698 enum tree_code tcode;
2700 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2701 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2705 /* Return nonzero if two operands (typically of the same tree node)
2706 are necessarily equal. FLAGS modifies behavior as follows:
2708 If OEP_ONLY_CONST is set, only return nonzero for constants.
2709 This function tests whether the operands are indistinguishable;
2710 it does not test whether they are equal using C's == operation.
2711 The distinction is important for IEEE floating point, because
2712 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2713 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2715 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2716 even though it may hold multiple values during a function.
2717 This is because a GCC tree node guarantees that nothing else is
2718 executed between the evaluation of its "operands" (which may often
2719 be evaluated in arbitrary order). Hence if the operands themselves
2720 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2721 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2722 unset means assuming isochronic (or instantaneous) tree equivalence.
2723 Unless comparing arbitrary expression trees, such as from different
2724 statements, this flag can usually be left unset.
2726 If OEP_PURE_SAME is set, then pure functions with identical arguments
2727 are considered the same. It is used when the caller has other ways
2728 to ensure that global memory is unchanged in between.
2730 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2731 not values of expressions.
2733 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2734 any operand with side effect. This is unnecesarily conservative in the
2735 case we know that arg0 and arg1 are in disjoint code paths (such as in
2736 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2737 addresses with TREE_CONSTANT flag set so we know that &var == &var
2738 even if var is volatile. */
2741 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2743 /* When checking, verify at the outermost operand_equal_p call that
2744 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2745 hash value. */
2746 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2748 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2750 if (arg0 != arg1)
2752 inchash::hash hstate0 (0), hstate1 (0);
2753 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2754 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2755 hashval_t h0 = hstate0.end ();
2756 hashval_t h1 = hstate1.end ();
2757 gcc_assert (h0 == h1);
2759 return 1;
2761 else
2762 return 0;
2765 /* If either is ERROR_MARK, they aren't equal. */
2766 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2767 || TREE_TYPE (arg0) == error_mark_node
2768 || TREE_TYPE (arg1) == error_mark_node)
2769 return 0;
2771 /* Similar, if either does not have a type (like a released SSA name),
2772 they aren't equal. */
2773 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2774 return 0;
2776 /* We cannot consider pointers to different address space equal. */
2777 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2778 && POINTER_TYPE_P (TREE_TYPE (arg1))
2779 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2780 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2781 return 0;
2783 /* Check equality of integer constants before bailing out due to
2784 precision differences. */
2785 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2787 /* Address of INTEGER_CST is not defined; check that we did not forget
2788 to drop the OEP_ADDRESS_OF flags. */
2789 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2790 return tree_int_cst_equal (arg0, arg1);
2793 if (!(flags & OEP_ADDRESS_OF))
2795 /* If both types don't have the same signedness, then we can't consider
2796 them equal. We must check this before the STRIP_NOPS calls
2797 because they may change the signedness of the arguments. As pointers
2798 strictly don't have a signedness, require either two pointers or
2799 two non-pointers as well. */
2800 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2801 || POINTER_TYPE_P (TREE_TYPE (arg0))
2802 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2803 return 0;
2805 /* If both types don't have the same precision, then it is not safe
2806 to strip NOPs. */
2807 if (element_precision (TREE_TYPE (arg0))
2808 != element_precision (TREE_TYPE (arg1)))
2809 return 0;
2811 STRIP_NOPS (arg0);
2812 STRIP_NOPS (arg1);
2814 #if 0
2815 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2816 sanity check once the issue is solved. */
2817 else
2818 /* Addresses of conversions and SSA_NAMEs (and many other things)
2819 are not defined. Check that we did not forget to drop the
2820 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2821 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2822 && TREE_CODE (arg0) != SSA_NAME);
2823 #endif
2825 /* In case both args are comparisons but with different comparison
2826 code, try to swap the comparison operands of one arg to produce
2827 a match and compare that variant. */
2828 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2829 && COMPARISON_CLASS_P (arg0)
2830 && COMPARISON_CLASS_P (arg1))
2832 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2834 if (TREE_CODE (arg0) == swap_code)
2835 return operand_equal_p (TREE_OPERAND (arg0, 0),
2836 TREE_OPERAND (arg1, 1), flags)
2837 && operand_equal_p (TREE_OPERAND (arg0, 1),
2838 TREE_OPERAND (arg1, 0), flags);
2841 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2843 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2844 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2846 else if (flags & OEP_ADDRESS_OF)
2848 /* If we are interested in comparing addresses ignore
2849 MEM_REF wrappings of the base that can appear just for
2850 TBAA reasons. */
2851 if (TREE_CODE (arg0) == MEM_REF
2852 && DECL_P (arg1)
2853 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2854 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2855 && integer_zerop (TREE_OPERAND (arg0, 1)))
2856 return 1;
2857 else if (TREE_CODE (arg1) == MEM_REF
2858 && DECL_P (arg0)
2859 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2860 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2861 && integer_zerop (TREE_OPERAND (arg1, 1)))
2862 return 1;
2863 return 0;
2865 else
2866 return 0;
2869 /* When not checking adddresses, this is needed for conversions and for
2870 COMPONENT_REF. Might as well play it safe and always test this. */
2871 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2872 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2873 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2874 && !(flags & OEP_ADDRESS_OF)))
2875 return 0;
2877 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2878 We don't care about side effects in that case because the SAVE_EXPR
2879 takes care of that for us. In all other cases, two expressions are
2880 equal if they have no side effects. If we have two identical
2881 expressions with side effects that should be treated the same due
2882 to the only side effects being identical SAVE_EXPR's, that will
2883 be detected in the recursive calls below.
2884 If we are taking an invariant address of two identical objects
2885 they are necessarily equal as well. */
2886 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2887 && (TREE_CODE (arg0) == SAVE_EXPR
2888 || (flags & OEP_MATCH_SIDE_EFFECTS)
2889 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2890 return 1;
2892 /* Next handle constant cases, those for which we can return 1 even
2893 if ONLY_CONST is set. */
2894 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2895 switch (TREE_CODE (arg0))
2897 case INTEGER_CST:
2898 return tree_int_cst_equal (arg0, arg1);
2900 case FIXED_CST:
2901 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2902 TREE_FIXED_CST (arg1));
2904 case REAL_CST:
2905 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2906 return 1;
2909 if (!HONOR_SIGNED_ZEROS (arg0))
2911 /* If we do not distinguish between signed and unsigned zero,
2912 consider them equal. */
2913 if (real_zerop (arg0) && real_zerop (arg1))
2914 return 1;
2916 return 0;
2918 case VECTOR_CST:
2920 unsigned i;
2922 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2923 return 0;
2925 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2927 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2928 VECTOR_CST_ELT (arg1, i), flags))
2929 return 0;
2931 return 1;
2934 case COMPLEX_CST:
2935 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2936 flags)
2937 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2938 flags));
2940 case STRING_CST:
2941 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2942 && ! memcmp (TREE_STRING_POINTER (arg0),
2943 TREE_STRING_POINTER (arg1),
2944 TREE_STRING_LENGTH (arg0)));
2946 case ADDR_EXPR:
2947 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2948 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2949 flags | OEP_ADDRESS_OF
2950 | OEP_MATCH_SIDE_EFFECTS);
2951 case CONSTRUCTOR:
2952 /* In GIMPLE empty constructors are allowed in initializers of
2953 aggregates. */
2954 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2955 default:
2956 break;
2959 if (flags & OEP_ONLY_CONST)
2960 return 0;
2962 /* Define macros to test an operand from arg0 and arg1 for equality and a
2963 variant that allows null and views null as being different from any
2964 non-null value. In the latter case, if either is null, the both
2965 must be; otherwise, do the normal comparison. */
2966 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2967 TREE_OPERAND (arg1, N), flags)
2969 #define OP_SAME_WITH_NULL(N) \
2970 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2971 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2973 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2975 case tcc_unary:
2976 /* Two conversions are equal only if signedness and modes match. */
2977 switch (TREE_CODE (arg0))
2979 CASE_CONVERT:
2980 case FIX_TRUNC_EXPR:
2981 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2982 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2983 return 0;
2984 break;
2985 default:
2986 break;
2989 return OP_SAME (0);
2992 case tcc_comparison:
2993 case tcc_binary:
2994 if (OP_SAME (0) && OP_SAME (1))
2995 return 1;
2997 /* For commutative ops, allow the other order. */
2998 return (commutative_tree_code (TREE_CODE (arg0))
2999 && operand_equal_p (TREE_OPERAND (arg0, 0),
3000 TREE_OPERAND (arg1, 1), flags)
3001 && operand_equal_p (TREE_OPERAND (arg0, 1),
3002 TREE_OPERAND (arg1, 0), flags));
3004 case tcc_reference:
3005 /* If either of the pointer (or reference) expressions we are
3006 dereferencing contain a side effect, these cannot be equal,
3007 but their addresses can be. */
3008 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3009 && (TREE_SIDE_EFFECTS (arg0)
3010 || TREE_SIDE_EFFECTS (arg1)))
3011 return 0;
3013 switch (TREE_CODE (arg0))
3015 case INDIRECT_REF:
3016 if (!(flags & OEP_ADDRESS_OF)
3017 && (TYPE_ALIGN (TREE_TYPE (arg0))
3018 != TYPE_ALIGN (TREE_TYPE (arg1))))
3019 return 0;
3020 flags &= ~OEP_ADDRESS_OF;
3021 return OP_SAME (0);
3023 case IMAGPART_EXPR:
3024 /* Require the same offset. */
3025 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3026 TYPE_SIZE (TREE_TYPE (arg1)),
3027 flags & ~OEP_ADDRESS_OF))
3028 return 0;
3030 /* Fallthru. */
3031 case REALPART_EXPR:
3032 case VIEW_CONVERT_EXPR:
3033 return OP_SAME (0);
3035 case TARGET_MEM_REF:
3036 case MEM_REF:
3037 if (!(flags & OEP_ADDRESS_OF))
3039 /* Require equal access sizes */
3040 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3041 && (!TYPE_SIZE (TREE_TYPE (arg0))
3042 || !TYPE_SIZE (TREE_TYPE (arg1))
3043 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3044 TYPE_SIZE (TREE_TYPE (arg1)),
3045 flags)))
3046 return 0;
3047 /* Verify that access happens in similar types. */
3048 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3049 return 0;
3050 /* Verify that accesses are TBAA compatible. */
3051 if (!alias_ptr_types_compatible_p
3052 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3053 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3054 || (MR_DEPENDENCE_CLIQUE (arg0)
3055 != MR_DEPENDENCE_CLIQUE (arg1))
3056 || (MR_DEPENDENCE_BASE (arg0)
3057 != MR_DEPENDENCE_BASE (arg1)))
3058 return 0;
3059 /* Verify that alignment is compatible. */
3060 if (TYPE_ALIGN (TREE_TYPE (arg0))
3061 != TYPE_ALIGN (TREE_TYPE (arg1)))
3062 return 0;
3064 flags &= ~OEP_ADDRESS_OF;
3065 return (OP_SAME (0) && OP_SAME (1)
3066 /* TARGET_MEM_REF require equal extra operands. */
3067 && (TREE_CODE (arg0) != TARGET_MEM_REF
3068 || (OP_SAME_WITH_NULL (2)
3069 && OP_SAME_WITH_NULL (3)
3070 && OP_SAME_WITH_NULL (4))));
3072 case ARRAY_REF:
3073 case ARRAY_RANGE_REF:
3074 if (!OP_SAME (0))
3075 return 0;
3076 flags &= ~OEP_ADDRESS_OF;
3077 /* Compare the array index by value if it is constant first as we
3078 may have different types but same value here. */
3079 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3080 TREE_OPERAND (arg1, 1))
3081 || OP_SAME (1))
3082 && OP_SAME_WITH_NULL (2)
3083 && OP_SAME_WITH_NULL (3)
3084 /* Compare low bound and element size as with OEP_ADDRESS_OF
3085 we have to account for the offset of the ref. */
3086 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3087 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3088 || (operand_equal_p (array_ref_low_bound
3089 (CONST_CAST_TREE (arg0)),
3090 array_ref_low_bound
3091 (CONST_CAST_TREE (arg1)), flags)
3092 && operand_equal_p (array_ref_element_size
3093 (CONST_CAST_TREE (arg0)),
3094 array_ref_element_size
3095 (CONST_CAST_TREE (arg1)),
3096 flags))));
3098 case COMPONENT_REF:
3099 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3100 may be NULL when we're called to compare MEM_EXPRs. */
3101 if (!OP_SAME_WITH_NULL (0)
3102 || !OP_SAME (1))
3103 return 0;
3104 flags &= ~OEP_ADDRESS_OF;
3105 return OP_SAME_WITH_NULL (2);
3107 case BIT_FIELD_REF:
3108 if (!OP_SAME (0))
3109 return 0;
3110 flags &= ~OEP_ADDRESS_OF;
3111 return OP_SAME (1) && OP_SAME (2);
3113 default:
3114 return 0;
3117 case tcc_expression:
3118 switch (TREE_CODE (arg0))
3120 case ADDR_EXPR:
3121 /* Be sure we pass right ADDRESS_OF flag. */
3122 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3123 return operand_equal_p (TREE_OPERAND (arg0, 0),
3124 TREE_OPERAND (arg1, 0),
3125 flags | OEP_ADDRESS_OF);
3127 case TRUTH_NOT_EXPR:
3128 return OP_SAME (0);
3130 case TRUTH_ANDIF_EXPR:
3131 case TRUTH_ORIF_EXPR:
3132 return OP_SAME (0) && OP_SAME (1);
3134 case FMA_EXPR:
3135 case WIDEN_MULT_PLUS_EXPR:
3136 case WIDEN_MULT_MINUS_EXPR:
3137 if (!OP_SAME (2))
3138 return 0;
3139 /* The multiplcation operands are commutative. */
3140 /* FALLTHRU */
3142 case TRUTH_AND_EXPR:
3143 case TRUTH_OR_EXPR:
3144 case TRUTH_XOR_EXPR:
3145 if (OP_SAME (0) && OP_SAME (1))
3146 return 1;
3148 /* Otherwise take into account this is a commutative operation. */
3149 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3150 TREE_OPERAND (arg1, 1), flags)
3151 && operand_equal_p (TREE_OPERAND (arg0, 1),
3152 TREE_OPERAND (arg1, 0), flags));
3154 case COND_EXPR:
3155 if (! OP_SAME (1) || ! OP_SAME (2))
3156 return 0;
3157 flags &= ~OEP_ADDRESS_OF;
3158 return OP_SAME (0);
3160 case VEC_COND_EXPR:
3161 case DOT_PROD_EXPR:
3162 case BIT_INSERT_EXPR:
3163 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3165 default:
3166 return 0;
3169 case tcc_vl_exp:
3170 switch (TREE_CODE (arg0))
3172 case CALL_EXPR:
3173 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3174 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3175 /* If not both CALL_EXPRs are either internal or normal function
3176 functions, then they are not equal. */
3177 return 0;
3178 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3180 /* If the CALL_EXPRs call different internal functions, then they
3181 are not equal. */
3182 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3183 return 0;
3185 else
3187 /* If the CALL_EXPRs call different functions, then they are not
3188 equal. */
3189 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3190 flags))
3191 return 0;
3194 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3196 unsigned int cef = call_expr_flags (arg0);
3197 if (flags & OEP_PURE_SAME)
3198 cef &= ECF_CONST | ECF_PURE;
3199 else
3200 cef &= ECF_CONST;
3201 if (!cef)
3202 return 0;
3205 /* Now see if all the arguments are the same. */
3207 const_call_expr_arg_iterator iter0, iter1;
3208 const_tree a0, a1;
3209 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3210 a1 = first_const_call_expr_arg (arg1, &iter1);
3211 a0 && a1;
3212 a0 = next_const_call_expr_arg (&iter0),
3213 a1 = next_const_call_expr_arg (&iter1))
3214 if (! operand_equal_p (a0, a1, flags))
3215 return 0;
3217 /* If we get here and both argument lists are exhausted
3218 then the CALL_EXPRs are equal. */
3219 return ! (a0 || a1);
3221 default:
3222 return 0;
3225 case tcc_declaration:
3226 /* Consider __builtin_sqrt equal to sqrt. */
3227 return (TREE_CODE (arg0) == FUNCTION_DECL
3228 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3229 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3230 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3232 case tcc_exceptional:
3233 if (TREE_CODE (arg0) == CONSTRUCTOR)
3235 /* In GIMPLE constructors are used only to build vectors from
3236 elements. Individual elements in the constructor must be
3237 indexed in increasing order and form an initial sequence.
3239 We make no effort to compare constructors in generic.
3240 (see sem_variable::equals in ipa-icf which can do so for
3241 constants). */
3242 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3243 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3244 return 0;
3246 /* Be sure that vectors constructed have the same representation.
3247 We only tested element precision and modes to match.
3248 Vectors may be BLKmode and thus also check that the number of
3249 parts match. */
3250 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3251 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3252 return 0;
3254 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3255 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3256 unsigned int len = vec_safe_length (v0);
3258 if (len != vec_safe_length (v1))
3259 return 0;
3261 for (unsigned int i = 0; i < len; i++)
3263 constructor_elt *c0 = &(*v0)[i];
3264 constructor_elt *c1 = &(*v1)[i];
3266 if (!operand_equal_p (c0->value, c1->value, flags)
3267 /* In GIMPLE the indexes can be either NULL or matching i.
3268 Double check this so we won't get false
3269 positives for GENERIC. */
3270 || (c0->index
3271 && (TREE_CODE (c0->index) != INTEGER_CST
3272 || !compare_tree_int (c0->index, i)))
3273 || (c1->index
3274 && (TREE_CODE (c1->index) != INTEGER_CST
3275 || !compare_tree_int (c1->index, i))))
3276 return 0;
3278 return 1;
3280 return 0;
3282 default:
3283 return 0;
3286 #undef OP_SAME
3287 #undef OP_SAME_WITH_NULL
3290 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3291 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3293 When in doubt, return 0. */
3295 static int
3296 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3298 int unsignedp1, unsignedpo;
3299 tree primarg0, primarg1, primother;
3300 unsigned int correct_width;
3302 if (operand_equal_p (arg0, arg1, 0))
3303 return 1;
3305 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3306 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3307 return 0;
3309 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3310 and see if the inner values are the same. This removes any
3311 signedness comparison, which doesn't matter here. */
3312 primarg0 = arg0, primarg1 = arg1;
3313 STRIP_NOPS (primarg0);
3314 STRIP_NOPS (primarg1);
3315 if (operand_equal_p (primarg0, primarg1, 0))
3316 return 1;
3318 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3319 actual comparison operand, ARG0.
3321 First throw away any conversions to wider types
3322 already present in the operands. */
3324 primarg1 = get_narrower (arg1, &unsignedp1);
3325 primother = get_narrower (other, &unsignedpo);
3327 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3328 if (unsignedp1 == unsignedpo
3329 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3330 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3332 tree type = TREE_TYPE (arg0);
3334 /* Make sure shorter operand is extended the right way
3335 to match the longer operand. */
3336 primarg1 = fold_convert (signed_or_unsigned_type_for
3337 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3339 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3340 return 1;
3343 return 0;
3346 /* See if ARG is an expression that is either a comparison or is performing
3347 arithmetic on comparisons. The comparisons must only be comparing
3348 two different values, which will be stored in *CVAL1 and *CVAL2; if
3349 they are nonzero it means that some operands have already been found.
3350 No variables may be used anywhere else in the expression except in the
3351 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3352 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3354 If this is true, return 1. Otherwise, return zero. */
3356 static int
3357 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3359 enum tree_code code = TREE_CODE (arg);
3360 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3362 /* We can handle some of the tcc_expression cases here. */
3363 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3364 tclass = tcc_unary;
3365 else if (tclass == tcc_expression
3366 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3367 || code == COMPOUND_EXPR))
3368 tclass = tcc_binary;
3370 else if (tclass == tcc_expression && code == SAVE_EXPR
3371 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3373 /* If we've already found a CVAL1 or CVAL2, this expression is
3374 two complex to handle. */
3375 if (*cval1 || *cval2)
3376 return 0;
3378 tclass = tcc_unary;
3379 *save_p = 1;
3382 switch (tclass)
3384 case tcc_unary:
3385 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3387 case tcc_binary:
3388 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3389 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3390 cval1, cval2, save_p));
3392 case tcc_constant:
3393 return 1;
3395 case tcc_expression:
3396 if (code == COND_EXPR)
3397 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3398 cval1, cval2, save_p)
3399 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3400 cval1, cval2, save_p)
3401 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3402 cval1, cval2, save_p));
3403 return 0;
3405 case tcc_comparison:
3406 /* First see if we can handle the first operand, then the second. For
3407 the second operand, we know *CVAL1 can't be zero. It must be that
3408 one side of the comparison is each of the values; test for the
3409 case where this isn't true by failing if the two operands
3410 are the same. */
3412 if (operand_equal_p (TREE_OPERAND (arg, 0),
3413 TREE_OPERAND (arg, 1), 0))
3414 return 0;
3416 if (*cval1 == 0)
3417 *cval1 = TREE_OPERAND (arg, 0);
3418 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3420 else if (*cval2 == 0)
3421 *cval2 = TREE_OPERAND (arg, 0);
3422 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3424 else
3425 return 0;
3427 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3429 else if (*cval2 == 0)
3430 *cval2 = TREE_OPERAND (arg, 1);
3431 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3433 else
3434 return 0;
3436 return 1;
3438 default:
3439 return 0;
3443 /* ARG is a tree that is known to contain just arithmetic operations and
3444 comparisons. Evaluate the operations in the tree substituting NEW0 for
3445 any occurrence of OLD0 as an operand of a comparison and likewise for
3446 NEW1 and OLD1. */
3448 static tree
3449 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3450 tree old1, tree new1)
3452 tree type = TREE_TYPE (arg);
3453 enum tree_code code = TREE_CODE (arg);
3454 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3456 /* We can handle some of the tcc_expression cases here. */
3457 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3458 tclass = tcc_unary;
3459 else if (tclass == tcc_expression
3460 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3461 tclass = tcc_binary;
3463 switch (tclass)
3465 case tcc_unary:
3466 return fold_build1_loc (loc, code, type,
3467 eval_subst (loc, TREE_OPERAND (arg, 0),
3468 old0, new0, old1, new1));
3470 case tcc_binary:
3471 return fold_build2_loc (loc, code, type,
3472 eval_subst (loc, TREE_OPERAND (arg, 0),
3473 old0, new0, old1, new1),
3474 eval_subst (loc, TREE_OPERAND (arg, 1),
3475 old0, new0, old1, new1));
3477 case tcc_expression:
3478 switch (code)
3480 case SAVE_EXPR:
3481 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3482 old1, new1);
3484 case COMPOUND_EXPR:
3485 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3486 old1, new1);
3488 case COND_EXPR:
3489 return fold_build3_loc (loc, code, type,
3490 eval_subst (loc, TREE_OPERAND (arg, 0),
3491 old0, new0, old1, new1),
3492 eval_subst (loc, TREE_OPERAND (arg, 1),
3493 old0, new0, old1, new1),
3494 eval_subst (loc, TREE_OPERAND (arg, 2),
3495 old0, new0, old1, new1));
3496 default:
3497 break;
3499 /* Fall through - ??? */
3501 case tcc_comparison:
3503 tree arg0 = TREE_OPERAND (arg, 0);
3504 tree arg1 = TREE_OPERAND (arg, 1);
3506 /* We need to check both for exact equality and tree equality. The
3507 former will be true if the operand has a side-effect. In that
3508 case, we know the operand occurred exactly once. */
3510 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3511 arg0 = new0;
3512 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3513 arg0 = new1;
3515 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3516 arg1 = new0;
3517 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3518 arg1 = new1;
3520 return fold_build2_loc (loc, code, type, arg0, arg1);
3523 default:
3524 return arg;
3528 /* Return a tree for the case when the result of an expression is RESULT
3529 converted to TYPE and OMITTED was previously an operand of the expression
3530 but is now not needed (e.g., we folded OMITTED * 0).
3532 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3533 the conversion of RESULT to TYPE. */
3535 tree
3536 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3538 tree t = fold_convert_loc (loc, type, result);
3540 /* If the resulting operand is an empty statement, just return the omitted
3541 statement casted to void. */
3542 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3543 return build1_loc (loc, NOP_EXPR, void_type_node,
3544 fold_ignored_result (omitted));
3546 if (TREE_SIDE_EFFECTS (omitted))
3547 return build2_loc (loc, COMPOUND_EXPR, type,
3548 fold_ignored_result (omitted), t);
3550 return non_lvalue_loc (loc, t);
3553 /* Return a tree for the case when the result of an expression is RESULT
3554 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3555 of the expression but are now not needed.
3557 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3558 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3559 evaluated before OMITTED2. Otherwise, if neither has side effects,
3560 just do the conversion of RESULT to TYPE. */
3562 tree
3563 omit_two_operands_loc (location_t loc, tree type, tree result,
3564 tree omitted1, tree omitted2)
3566 tree t = fold_convert_loc (loc, type, result);
3568 if (TREE_SIDE_EFFECTS (omitted2))
3569 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3570 if (TREE_SIDE_EFFECTS (omitted1))
3571 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3573 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3577 /* Return a simplified tree node for the truth-negation of ARG. This
3578 never alters ARG itself. We assume that ARG is an operation that
3579 returns a truth value (0 or 1).
3581 FIXME: one would think we would fold the result, but it causes
3582 problems with the dominator optimizer. */
3584 static tree
3585 fold_truth_not_expr (location_t loc, tree arg)
3587 tree type = TREE_TYPE (arg);
3588 enum tree_code code = TREE_CODE (arg);
3589 location_t loc1, loc2;
3591 /* If this is a comparison, we can simply invert it, except for
3592 floating-point non-equality comparisons, in which case we just
3593 enclose a TRUTH_NOT_EXPR around what we have. */
3595 if (TREE_CODE_CLASS (code) == tcc_comparison)
3597 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3598 if (FLOAT_TYPE_P (op_type)
3599 && flag_trapping_math
3600 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3601 && code != NE_EXPR && code != EQ_EXPR)
3602 return NULL_TREE;
3604 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3605 if (code == ERROR_MARK)
3606 return NULL_TREE;
3608 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3609 TREE_OPERAND (arg, 1));
3610 if (TREE_NO_WARNING (arg))
3611 TREE_NO_WARNING (ret) = 1;
3612 return ret;
3615 switch (code)
3617 case INTEGER_CST:
3618 return constant_boolean_node (integer_zerop (arg), type);
3620 case TRUTH_AND_EXPR:
3621 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3622 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3623 return build2_loc (loc, TRUTH_OR_EXPR, type,
3624 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3625 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3627 case TRUTH_OR_EXPR:
3628 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3629 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3630 return build2_loc (loc, TRUTH_AND_EXPR, type,
3631 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3632 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3634 case TRUTH_XOR_EXPR:
3635 /* Here we can invert either operand. We invert the first operand
3636 unless the second operand is a TRUTH_NOT_EXPR in which case our
3637 result is the XOR of the first operand with the inside of the
3638 negation of the second operand. */
3640 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3641 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3642 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3643 else
3644 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3645 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3646 TREE_OPERAND (arg, 1));
3648 case TRUTH_ANDIF_EXPR:
3649 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3650 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3651 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3652 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3653 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3655 case TRUTH_ORIF_EXPR:
3656 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3657 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3658 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3659 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3660 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3662 case TRUTH_NOT_EXPR:
3663 return TREE_OPERAND (arg, 0);
3665 case COND_EXPR:
3667 tree arg1 = TREE_OPERAND (arg, 1);
3668 tree arg2 = TREE_OPERAND (arg, 2);
3670 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3671 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3673 /* A COND_EXPR may have a throw as one operand, which
3674 then has void type. Just leave void operands
3675 as they are. */
3676 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3677 VOID_TYPE_P (TREE_TYPE (arg1))
3678 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3679 VOID_TYPE_P (TREE_TYPE (arg2))
3680 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3683 case COMPOUND_EXPR:
3684 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3685 return build2_loc (loc, COMPOUND_EXPR, type,
3686 TREE_OPERAND (arg, 0),
3687 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3689 case NON_LVALUE_EXPR:
3690 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3691 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3693 CASE_CONVERT:
3694 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3695 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3697 /* fall through */
3699 case FLOAT_EXPR:
3700 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3701 return build1_loc (loc, TREE_CODE (arg), type,
3702 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3704 case BIT_AND_EXPR:
3705 if (!integer_onep (TREE_OPERAND (arg, 1)))
3706 return NULL_TREE;
3707 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3709 case SAVE_EXPR:
3710 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3712 case CLEANUP_POINT_EXPR:
3713 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3714 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3715 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3717 default:
3718 return NULL_TREE;
3722 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3723 assume that ARG is an operation that returns a truth value (0 or 1
3724 for scalars, 0 or -1 for vectors). Return the folded expression if
3725 folding is successful. Otherwise, return NULL_TREE. */
3727 static tree
3728 fold_invert_truthvalue (location_t loc, tree arg)
3730 tree type = TREE_TYPE (arg);
3731 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3732 ? BIT_NOT_EXPR
3733 : TRUTH_NOT_EXPR,
3734 type, arg);
3737 /* Return a simplified tree node for the truth-negation of ARG. This
3738 never alters ARG itself. We assume that ARG is an operation that
3739 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3741 tree
3742 invert_truthvalue_loc (location_t loc, tree arg)
3744 if (TREE_CODE (arg) == ERROR_MARK)
3745 return arg;
3747 tree type = TREE_TYPE (arg);
3748 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3749 ? BIT_NOT_EXPR
3750 : TRUTH_NOT_EXPR,
3751 type, arg);
3754 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3755 with code CODE. This optimization is unsafe. */
3756 static tree
3757 distribute_real_division (location_t loc, enum tree_code code, tree type,
3758 tree arg0, tree arg1)
3760 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3761 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3763 /* (A / C) +- (B / C) -> (A +- B) / C. */
3764 if (mul0 == mul1
3765 && operand_equal_p (TREE_OPERAND (arg0, 1),
3766 TREE_OPERAND (arg1, 1), 0))
3767 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3768 fold_build2_loc (loc, code, type,
3769 TREE_OPERAND (arg0, 0),
3770 TREE_OPERAND (arg1, 0)),
3771 TREE_OPERAND (arg0, 1));
3773 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3774 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3775 TREE_OPERAND (arg1, 0), 0)
3776 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3777 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3779 REAL_VALUE_TYPE r0, r1;
3780 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3781 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3782 if (!mul0)
3783 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3784 if (!mul1)
3785 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3786 real_arithmetic (&r0, code, &r0, &r1);
3787 return fold_build2_loc (loc, MULT_EXPR, type,
3788 TREE_OPERAND (arg0, 0),
3789 build_real (type, r0));
3792 return NULL_TREE;
3795 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3796 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3797 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3798 is the original memory reference used to preserve the alias set of
3799 the access. */
3801 static tree
3802 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3803 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3804 int unsignedp, int reversep)
3806 tree result, bftype;
3808 alias_set_type iset = get_alias_set (orig_inner);
3809 if (iset == 0 && get_alias_set (inner) != iset)
3810 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3811 build_fold_addr_expr (inner),
3812 build_int_cst (ptr_type_node, 0));
3814 if (bitpos == 0 && !reversep)
3816 tree size = TYPE_SIZE (TREE_TYPE (inner));
3817 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3818 || POINTER_TYPE_P (TREE_TYPE (inner)))
3819 && tree_fits_shwi_p (size)
3820 && tree_to_shwi (size) == bitsize)
3821 return fold_convert_loc (loc, type, inner);
3824 bftype = type;
3825 if (TYPE_PRECISION (bftype) != bitsize
3826 || TYPE_UNSIGNED (bftype) == !unsignedp)
3827 bftype = build_nonstandard_integer_type (bitsize, 0);
3829 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3830 size_int (bitsize), bitsize_int (bitpos));
3831 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3833 if (bftype != type)
3834 result = fold_convert_loc (loc, type, result);
3836 return result;
3839 /* Optimize a bit-field compare.
3841 There are two cases: First is a compare against a constant and the
3842 second is a comparison of two items where the fields are at the same
3843 bit position relative to the start of a chunk (byte, halfword, word)
3844 large enough to contain it. In these cases we can avoid the shift
3845 implicit in bitfield extractions.
3847 For constants, we emit a compare of the shifted constant with the
3848 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3849 compared. For two fields at the same position, we do the ANDs with the
3850 similar mask and compare the result of the ANDs.
3852 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3853 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3854 are the left and right operands of the comparison, respectively.
3856 If the optimization described above can be done, we return the resulting
3857 tree. Otherwise we return zero. */
3859 static tree
3860 optimize_bit_field_compare (location_t loc, enum tree_code code,
3861 tree compare_type, tree lhs, tree rhs)
3863 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3864 tree type = TREE_TYPE (lhs);
3865 tree unsigned_type;
3866 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3867 machine_mode lmode, rmode, nmode;
3868 int lunsignedp, runsignedp;
3869 int lreversep, rreversep;
3870 int lvolatilep = 0, rvolatilep = 0;
3871 tree linner, rinner = NULL_TREE;
3872 tree mask;
3873 tree offset;
3875 /* Get all the information about the extractions being done. If the bit size
3876 if the same as the size of the underlying object, we aren't doing an
3877 extraction at all and so can do nothing. We also don't want to
3878 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3879 then will no longer be able to replace it. */
3880 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3881 &lunsignedp, &lreversep, &lvolatilep);
3882 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3883 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3884 return 0;
3886 if (const_p)
3887 rreversep = lreversep;
3888 else
3890 /* If this is not a constant, we can only do something if bit positions,
3891 sizes, signedness and storage order are the same. */
3892 rinner
3893 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3894 &runsignedp, &rreversep, &rvolatilep);
3896 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3897 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3898 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3899 return 0;
3902 /* Honor the C++ memory model and mimic what RTL expansion does. */
3903 unsigned HOST_WIDE_INT bitstart = 0;
3904 unsigned HOST_WIDE_INT bitend = 0;
3905 if (TREE_CODE (lhs) == COMPONENT_REF)
3907 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3908 if (offset != NULL_TREE)
3909 return 0;
3912 /* See if we can find a mode to refer to this field. We should be able to,
3913 but fail if we can't. */
3914 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3915 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3916 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3917 TYPE_ALIGN (TREE_TYPE (rinner))),
3918 word_mode, false);
3919 if (nmode == VOIDmode)
3920 return 0;
3922 /* Set signed and unsigned types of the precision of this mode for the
3923 shifts below. */
3924 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3926 /* Compute the bit position and size for the new reference and our offset
3927 within it. If the new reference is the same size as the original, we
3928 won't optimize anything, so return zero. */
3929 nbitsize = GET_MODE_BITSIZE (nmode);
3930 nbitpos = lbitpos & ~ (nbitsize - 1);
3931 lbitpos -= nbitpos;
3932 if (nbitsize == lbitsize)
3933 return 0;
3935 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3936 lbitpos = nbitsize - lbitsize - lbitpos;
3938 /* Make the mask to be used against the extracted field. */
3939 mask = build_int_cst_type (unsigned_type, -1);
3940 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3941 mask = const_binop (RSHIFT_EXPR, mask,
3942 size_int (nbitsize - lbitsize - lbitpos));
3944 if (! const_p)
3945 /* If not comparing with constant, just rework the comparison
3946 and return. */
3947 return fold_build2_loc (loc, code, compare_type,
3948 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3949 make_bit_field_ref (loc, linner, lhs,
3950 unsigned_type,
3951 nbitsize, nbitpos,
3952 1, lreversep),
3953 mask),
3954 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3955 make_bit_field_ref (loc, rinner, rhs,
3956 unsigned_type,
3957 nbitsize, nbitpos,
3958 1, rreversep),
3959 mask));
3961 /* Otherwise, we are handling the constant case. See if the constant is too
3962 big for the field. Warn and return a tree for 0 (false) if so. We do
3963 this not only for its own sake, but to avoid having to test for this
3964 error case below. If we didn't, we might generate wrong code.
3966 For unsigned fields, the constant shifted right by the field length should
3967 be all zero. For signed fields, the high-order bits should agree with
3968 the sign bit. */
3970 if (lunsignedp)
3972 if (wi::lrshift (rhs, lbitsize) != 0)
3974 warning (0, "comparison is always %d due to width of bit-field",
3975 code == NE_EXPR);
3976 return constant_boolean_node (code == NE_EXPR, compare_type);
3979 else
3981 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3982 if (tem != 0 && tem != -1)
3984 warning (0, "comparison is always %d due to width of bit-field",
3985 code == NE_EXPR);
3986 return constant_boolean_node (code == NE_EXPR, compare_type);
3990 /* Single-bit compares should always be against zero. */
3991 if (lbitsize == 1 && ! integer_zerop (rhs))
3993 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3994 rhs = build_int_cst (type, 0);
3997 /* Make a new bitfield reference, shift the constant over the
3998 appropriate number of bits and mask it with the computed mask
3999 (in case this was a signed field). If we changed it, make a new one. */
4000 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4001 nbitsize, nbitpos, 1, lreversep);
4003 rhs = const_binop (BIT_AND_EXPR,
4004 const_binop (LSHIFT_EXPR,
4005 fold_convert_loc (loc, unsigned_type, rhs),
4006 size_int (lbitpos)),
4007 mask);
4009 lhs = build2_loc (loc, code, compare_type,
4010 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4011 return lhs;
4014 /* Subroutine for fold_truth_andor_1: decode a field reference.
4016 If EXP is a comparison reference, we return the innermost reference.
4018 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4019 set to the starting bit number.
4021 If the innermost field can be completely contained in a mode-sized
4022 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4024 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4025 otherwise it is not changed.
4027 *PUNSIGNEDP is set to the signedness of the field.
4029 *PREVERSEP is set to the storage order of the field.
4031 *PMASK is set to the mask used. This is either contained in a
4032 BIT_AND_EXPR or derived from the width of the field.
4034 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4036 Return 0 if this is not a component reference or is one that we can't
4037 do anything with. */
4039 static tree
4040 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4041 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4042 int *punsignedp, int *preversep, int *pvolatilep,
4043 tree *pmask, tree *pand_mask)
4045 tree exp = *exp_;
4046 tree outer_type = 0;
4047 tree and_mask = 0;
4048 tree mask, inner, offset;
4049 tree unsigned_type;
4050 unsigned int precision;
4052 /* All the optimizations using this function assume integer fields.
4053 There are problems with FP fields since the type_for_size call
4054 below can fail for, e.g., XFmode. */
4055 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4056 return 0;
4058 /* We are interested in the bare arrangement of bits, so strip everything
4059 that doesn't affect the machine mode. However, record the type of the
4060 outermost expression if it may matter below. */
4061 if (CONVERT_EXPR_P (exp)
4062 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4063 outer_type = TREE_TYPE (exp);
4064 STRIP_NOPS (exp);
4066 if (TREE_CODE (exp) == BIT_AND_EXPR)
4068 and_mask = TREE_OPERAND (exp, 1);
4069 exp = TREE_OPERAND (exp, 0);
4070 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4071 if (TREE_CODE (and_mask) != INTEGER_CST)
4072 return 0;
4075 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4076 punsignedp, preversep, pvolatilep);
4077 if ((inner == exp && and_mask == 0)
4078 || *pbitsize < 0 || offset != 0
4079 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4080 return 0;
4082 *exp_ = exp;
4084 /* If the number of bits in the reference is the same as the bitsize of
4085 the outer type, then the outer type gives the signedness. Otherwise
4086 (in case of a small bitfield) the signedness is unchanged. */
4087 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4088 *punsignedp = TYPE_UNSIGNED (outer_type);
4090 /* Compute the mask to access the bitfield. */
4091 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4092 precision = TYPE_PRECISION (unsigned_type);
4094 mask = build_int_cst_type (unsigned_type, -1);
4096 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4097 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4099 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4100 if (and_mask != 0)
4101 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4102 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4104 *pmask = mask;
4105 *pand_mask = and_mask;
4106 return inner;
4109 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4110 bit positions and MASK is SIGNED. */
4112 static int
4113 all_ones_mask_p (const_tree mask, unsigned int size)
4115 tree type = TREE_TYPE (mask);
4116 unsigned int precision = TYPE_PRECISION (type);
4118 /* If this function returns true when the type of the mask is
4119 UNSIGNED, then there will be errors. In particular see
4120 gcc.c-torture/execute/990326-1.c. There does not appear to be
4121 any documentation paper trail as to why this is so. But the pre
4122 wide-int worked with that restriction and it has been preserved
4123 here. */
4124 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4125 return false;
4127 return wi::mask (size, false, precision) == mask;
4130 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4131 represents the sign bit of EXP's type. If EXP represents a sign
4132 or zero extension, also test VAL against the unextended type.
4133 The return value is the (sub)expression whose sign bit is VAL,
4134 or NULL_TREE otherwise. */
4136 tree
4137 sign_bit_p (tree exp, const_tree val)
4139 int width;
4140 tree t;
4142 /* Tree EXP must have an integral type. */
4143 t = TREE_TYPE (exp);
4144 if (! INTEGRAL_TYPE_P (t))
4145 return NULL_TREE;
4147 /* Tree VAL must be an integer constant. */
4148 if (TREE_CODE (val) != INTEGER_CST
4149 || TREE_OVERFLOW (val))
4150 return NULL_TREE;
4152 width = TYPE_PRECISION (t);
4153 if (wi::only_sign_bit_p (val, width))
4154 return exp;
4156 /* Handle extension from a narrower type. */
4157 if (TREE_CODE (exp) == NOP_EXPR
4158 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4159 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4161 return NULL_TREE;
4164 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4165 to be evaluated unconditionally. */
4167 static int
4168 simple_operand_p (const_tree exp)
4170 /* Strip any conversions that don't change the machine mode. */
4171 STRIP_NOPS (exp);
4173 return (CONSTANT_CLASS_P (exp)
4174 || TREE_CODE (exp) == SSA_NAME
4175 || (DECL_P (exp)
4176 && ! TREE_ADDRESSABLE (exp)
4177 && ! TREE_THIS_VOLATILE (exp)
4178 && ! DECL_NONLOCAL (exp)
4179 /* Don't regard global variables as simple. They may be
4180 allocated in ways unknown to the compiler (shared memory,
4181 #pragma weak, etc). */
4182 && ! TREE_PUBLIC (exp)
4183 && ! DECL_EXTERNAL (exp)
4184 /* Weakrefs are not safe to be read, since they can be NULL.
4185 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4186 have DECL_WEAK flag set. */
4187 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4188 /* Loading a static variable is unduly expensive, but global
4189 registers aren't expensive. */
4190 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4193 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4194 to be evaluated unconditionally.
4195 I addition to simple_operand_p, we assume that comparisons, conversions,
4196 and logic-not operations are simple, if their operands are simple, too. */
4198 static bool
4199 simple_operand_p_2 (tree exp)
4201 enum tree_code code;
4203 if (TREE_SIDE_EFFECTS (exp)
4204 || tree_could_trap_p (exp))
4205 return false;
4207 while (CONVERT_EXPR_P (exp))
4208 exp = TREE_OPERAND (exp, 0);
4210 code = TREE_CODE (exp);
4212 if (TREE_CODE_CLASS (code) == tcc_comparison)
4213 return (simple_operand_p (TREE_OPERAND (exp, 0))
4214 && simple_operand_p (TREE_OPERAND (exp, 1)));
4216 if (code == TRUTH_NOT_EXPR)
4217 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4219 return simple_operand_p (exp);
4223 /* The following functions are subroutines to fold_range_test and allow it to
4224 try to change a logical combination of comparisons into a range test.
4226 For example, both
4227 X == 2 || X == 3 || X == 4 || X == 5
4229 X >= 2 && X <= 5
4230 are converted to
4231 (unsigned) (X - 2) <= 3
4233 We describe each set of comparisons as being either inside or outside
4234 a range, using a variable named like IN_P, and then describe the
4235 range with a lower and upper bound. If one of the bounds is omitted,
4236 it represents either the highest or lowest value of the type.
4238 In the comments below, we represent a range by two numbers in brackets
4239 preceded by a "+" to designate being inside that range, or a "-" to
4240 designate being outside that range, so the condition can be inverted by
4241 flipping the prefix. An omitted bound is represented by a "-". For
4242 example, "- [-, 10]" means being outside the range starting at the lowest
4243 possible value and ending at 10, in other words, being greater than 10.
4244 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4245 always false.
4247 We set up things so that the missing bounds are handled in a consistent
4248 manner so neither a missing bound nor "true" and "false" need to be
4249 handled using a special case. */
4251 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4252 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4253 and UPPER1_P are nonzero if the respective argument is an upper bound
4254 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4255 must be specified for a comparison. ARG1 will be converted to ARG0's
4256 type if both are specified. */
4258 static tree
4259 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4260 tree arg1, int upper1_p)
4262 tree tem;
4263 int result;
4264 int sgn0, sgn1;
4266 /* If neither arg represents infinity, do the normal operation.
4267 Else, if not a comparison, return infinity. Else handle the special
4268 comparison rules. Note that most of the cases below won't occur, but
4269 are handled for consistency. */
4271 if (arg0 != 0 && arg1 != 0)
4273 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4274 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4275 STRIP_NOPS (tem);
4276 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4279 if (TREE_CODE_CLASS (code) != tcc_comparison)
4280 return 0;
4282 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4283 for neither. In real maths, we cannot assume open ended ranges are
4284 the same. But, this is computer arithmetic, where numbers are finite.
4285 We can therefore make the transformation of any unbounded range with
4286 the value Z, Z being greater than any representable number. This permits
4287 us to treat unbounded ranges as equal. */
4288 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4289 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4290 switch (code)
4292 case EQ_EXPR:
4293 result = sgn0 == sgn1;
4294 break;
4295 case NE_EXPR:
4296 result = sgn0 != sgn1;
4297 break;
4298 case LT_EXPR:
4299 result = sgn0 < sgn1;
4300 break;
4301 case LE_EXPR:
4302 result = sgn0 <= sgn1;
4303 break;
4304 case GT_EXPR:
4305 result = sgn0 > sgn1;
4306 break;
4307 case GE_EXPR:
4308 result = sgn0 >= sgn1;
4309 break;
4310 default:
4311 gcc_unreachable ();
4314 return constant_boolean_node (result, type);
4317 /* Helper routine for make_range. Perform one step for it, return
4318 new expression if the loop should continue or NULL_TREE if it should
4319 stop. */
4321 tree
4322 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4323 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4324 bool *strict_overflow_p)
4326 tree arg0_type = TREE_TYPE (arg0);
4327 tree n_low, n_high, low = *p_low, high = *p_high;
4328 int in_p = *p_in_p, n_in_p;
4330 switch (code)
4332 case TRUTH_NOT_EXPR:
4333 /* We can only do something if the range is testing for zero. */
4334 if (low == NULL_TREE || high == NULL_TREE
4335 || ! integer_zerop (low) || ! integer_zerop (high))
4336 return NULL_TREE;
4337 *p_in_p = ! in_p;
4338 return arg0;
4340 case EQ_EXPR: case NE_EXPR:
4341 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4342 /* We can only do something if the range is testing for zero
4343 and if the second operand is an integer constant. Note that
4344 saying something is "in" the range we make is done by
4345 complementing IN_P since it will set in the initial case of
4346 being not equal to zero; "out" is leaving it alone. */
4347 if (low == NULL_TREE || high == NULL_TREE
4348 || ! integer_zerop (low) || ! integer_zerop (high)
4349 || TREE_CODE (arg1) != INTEGER_CST)
4350 return NULL_TREE;
4352 switch (code)
4354 case NE_EXPR: /* - [c, c] */
4355 low = high = arg1;
4356 break;
4357 case EQ_EXPR: /* + [c, c] */
4358 in_p = ! in_p, low = high = arg1;
4359 break;
4360 case GT_EXPR: /* - [-, c] */
4361 low = 0, high = arg1;
4362 break;
4363 case GE_EXPR: /* + [c, -] */
4364 in_p = ! in_p, low = arg1, high = 0;
4365 break;
4366 case LT_EXPR: /* - [c, -] */
4367 low = arg1, high = 0;
4368 break;
4369 case LE_EXPR: /* + [-, c] */
4370 in_p = ! in_p, low = 0, high = arg1;
4371 break;
4372 default:
4373 gcc_unreachable ();
4376 /* If this is an unsigned comparison, we also know that EXP is
4377 greater than or equal to zero. We base the range tests we make
4378 on that fact, so we record it here so we can parse existing
4379 range tests. We test arg0_type since often the return type
4380 of, e.g. EQ_EXPR, is boolean. */
4381 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4383 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4384 in_p, low, high, 1,
4385 build_int_cst (arg0_type, 0),
4386 NULL_TREE))
4387 return NULL_TREE;
4389 in_p = n_in_p, low = n_low, high = n_high;
4391 /* If the high bound is missing, but we have a nonzero low
4392 bound, reverse the range so it goes from zero to the low bound
4393 minus 1. */
4394 if (high == 0 && low && ! integer_zerop (low))
4396 in_p = ! in_p;
4397 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4398 build_int_cst (TREE_TYPE (low), 1), 0);
4399 low = build_int_cst (arg0_type, 0);
4403 *p_low = low;
4404 *p_high = high;
4405 *p_in_p = in_p;
4406 return arg0;
4408 case NEGATE_EXPR:
4409 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4410 low and high are non-NULL, then normalize will DTRT. */
4411 if (!TYPE_UNSIGNED (arg0_type)
4412 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4414 if (low == NULL_TREE)
4415 low = TYPE_MIN_VALUE (arg0_type);
4416 if (high == NULL_TREE)
4417 high = TYPE_MAX_VALUE (arg0_type);
4420 /* (-x) IN [a,b] -> x in [-b, -a] */
4421 n_low = range_binop (MINUS_EXPR, exp_type,
4422 build_int_cst (exp_type, 0),
4423 0, high, 1);
4424 n_high = range_binop (MINUS_EXPR, exp_type,
4425 build_int_cst (exp_type, 0),
4426 0, low, 0);
4427 if (n_high != 0 && TREE_OVERFLOW (n_high))
4428 return NULL_TREE;
4429 goto normalize;
4431 case BIT_NOT_EXPR:
4432 /* ~ X -> -X - 1 */
4433 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4434 build_int_cst (exp_type, 1));
4436 case PLUS_EXPR:
4437 case MINUS_EXPR:
4438 if (TREE_CODE (arg1) != INTEGER_CST)
4439 return NULL_TREE;
4441 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4442 move a constant to the other side. */
4443 if (!TYPE_UNSIGNED (arg0_type)
4444 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4445 return NULL_TREE;
4447 /* If EXP is signed, any overflow in the computation is undefined,
4448 so we don't worry about it so long as our computations on
4449 the bounds don't overflow. For unsigned, overflow is defined
4450 and this is exactly the right thing. */
4451 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4452 arg0_type, low, 0, arg1, 0);
4453 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4454 arg0_type, high, 1, arg1, 0);
4455 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4456 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4457 return NULL_TREE;
4459 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4460 *strict_overflow_p = true;
4462 normalize:
4463 /* Check for an unsigned range which has wrapped around the maximum
4464 value thus making n_high < n_low, and normalize it. */
4465 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4467 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4468 build_int_cst (TREE_TYPE (n_high), 1), 0);
4469 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4470 build_int_cst (TREE_TYPE (n_low), 1), 0);
4472 /* If the range is of the form +/- [ x+1, x ], we won't
4473 be able to normalize it. But then, it represents the
4474 whole range or the empty set, so make it
4475 +/- [ -, - ]. */
4476 if (tree_int_cst_equal (n_low, low)
4477 && tree_int_cst_equal (n_high, high))
4478 low = high = 0;
4479 else
4480 in_p = ! in_p;
4482 else
4483 low = n_low, high = n_high;
4485 *p_low = low;
4486 *p_high = high;
4487 *p_in_p = in_p;
4488 return arg0;
4490 CASE_CONVERT:
4491 case NON_LVALUE_EXPR:
4492 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4493 return NULL_TREE;
4495 if (! INTEGRAL_TYPE_P (arg0_type)
4496 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4497 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4498 return NULL_TREE;
4500 n_low = low, n_high = high;
4502 if (n_low != 0)
4503 n_low = fold_convert_loc (loc, arg0_type, n_low);
4505 if (n_high != 0)
4506 n_high = fold_convert_loc (loc, arg0_type, n_high);
4508 /* If we're converting arg0 from an unsigned type, to exp,
4509 a signed type, we will be doing the comparison as unsigned.
4510 The tests above have already verified that LOW and HIGH
4511 are both positive.
4513 So we have to ensure that we will handle large unsigned
4514 values the same way that the current signed bounds treat
4515 negative values. */
4517 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4519 tree high_positive;
4520 tree equiv_type;
4521 /* For fixed-point modes, we need to pass the saturating flag
4522 as the 2nd parameter. */
4523 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4524 equiv_type
4525 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4526 TYPE_SATURATING (arg0_type));
4527 else
4528 equiv_type
4529 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4531 /* A range without an upper bound is, naturally, unbounded.
4532 Since convert would have cropped a very large value, use
4533 the max value for the destination type. */
4534 high_positive
4535 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4536 : TYPE_MAX_VALUE (arg0_type);
4538 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4539 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4540 fold_convert_loc (loc, arg0_type,
4541 high_positive),
4542 build_int_cst (arg0_type, 1));
4544 /* If the low bound is specified, "and" the range with the
4545 range for which the original unsigned value will be
4546 positive. */
4547 if (low != 0)
4549 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4550 1, fold_convert_loc (loc, arg0_type,
4551 integer_zero_node),
4552 high_positive))
4553 return NULL_TREE;
4555 in_p = (n_in_p == in_p);
4557 else
4559 /* Otherwise, "or" the range with the range of the input
4560 that will be interpreted as negative. */
4561 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4562 1, fold_convert_loc (loc, arg0_type,
4563 integer_zero_node),
4564 high_positive))
4565 return NULL_TREE;
4567 in_p = (in_p != n_in_p);
4571 *p_low = n_low;
4572 *p_high = n_high;
4573 *p_in_p = in_p;
4574 return arg0;
4576 default:
4577 return NULL_TREE;
4581 /* Given EXP, a logical expression, set the range it is testing into
4582 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4583 actually being tested. *PLOW and *PHIGH will be made of the same
4584 type as the returned expression. If EXP is not a comparison, we
4585 will most likely not be returning a useful value and range. Set
4586 *STRICT_OVERFLOW_P to true if the return value is only valid
4587 because signed overflow is undefined; otherwise, do not change
4588 *STRICT_OVERFLOW_P. */
4590 tree
4591 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4592 bool *strict_overflow_p)
4594 enum tree_code code;
4595 tree arg0, arg1 = NULL_TREE;
4596 tree exp_type, nexp;
4597 int in_p;
4598 tree low, high;
4599 location_t loc = EXPR_LOCATION (exp);
4601 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4602 and see if we can refine the range. Some of the cases below may not
4603 happen, but it doesn't seem worth worrying about this. We "continue"
4604 the outer loop when we've changed something; otherwise we "break"
4605 the switch, which will "break" the while. */
4607 in_p = 0;
4608 low = high = build_int_cst (TREE_TYPE (exp), 0);
4610 while (1)
4612 code = TREE_CODE (exp);
4613 exp_type = TREE_TYPE (exp);
4614 arg0 = NULL_TREE;
4616 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4618 if (TREE_OPERAND_LENGTH (exp) > 0)
4619 arg0 = TREE_OPERAND (exp, 0);
4620 if (TREE_CODE_CLASS (code) == tcc_binary
4621 || TREE_CODE_CLASS (code) == tcc_comparison
4622 || (TREE_CODE_CLASS (code) == tcc_expression
4623 && TREE_OPERAND_LENGTH (exp) > 1))
4624 arg1 = TREE_OPERAND (exp, 1);
4626 if (arg0 == NULL_TREE)
4627 break;
4629 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4630 &high, &in_p, strict_overflow_p);
4631 if (nexp == NULL_TREE)
4632 break;
4633 exp = nexp;
4636 /* If EXP is a constant, we can evaluate whether this is true or false. */
4637 if (TREE_CODE (exp) == INTEGER_CST)
4639 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4640 exp, 0, low, 0))
4641 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4642 exp, 1, high, 1)));
4643 low = high = 0;
4644 exp = 0;
4647 *pin_p = in_p, *plow = low, *phigh = high;
4648 return exp;
4651 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4652 type, TYPE, return an expression to test if EXP is in (or out of, depending
4653 on IN_P) the range. Return 0 if the test couldn't be created. */
4655 tree
4656 build_range_check (location_t loc, tree type, tree exp, int in_p,
4657 tree low, tree high)
4659 tree etype = TREE_TYPE (exp), value;
4661 /* Disable this optimization for function pointer expressions
4662 on targets that require function pointer canonicalization. */
4663 if (targetm.have_canonicalize_funcptr_for_compare ()
4664 && TREE_CODE (etype) == POINTER_TYPE
4665 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4666 return NULL_TREE;
4668 if (! in_p)
4670 value = build_range_check (loc, type, exp, 1, low, high);
4671 if (value != 0)
4672 return invert_truthvalue_loc (loc, value);
4674 return 0;
4677 if (low == 0 && high == 0)
4678 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4680 if (low == 0)
4681 return fold_build2_loc (loc, LE_EXPR, type, exp,
4682 fold_convert_loc (loc, etype, high));
4684 if (high == 0)
4685 return fold_build2_loc (loc, GE_EXPR, type, exp,
4686 fold_convert_loc (loc, etype, low));
4688 if (operand_equal_p (low, high, 0))
4689 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4690 fold_convert_loc (loc, etype, low));
4692 if (integer_zerop (low))
4694 if (! TYPE_UNSIGNED (etype))
4696 etype = unsigned_type_for (etype);
4697 high = fold_convert_loc (loc, etype, high);
4698 exp = fold_convert_loc (loc, etype, exp);
4700 return build_range_check (loc, type, exp, 1, 0, high);
4703 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4704 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4706 int prec = TYPE_PRECISION (etype);
4708 if (wi::mask (prec - 1, false, prec) == high)
4710 if (TYPE_UNSIGNED (etype))
4712 tree signed_etype = signed_type_for (etype);
4713 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4714 etype
4715 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4716 else
4717 etype = signed_etype;
4718 exp = fold_convert_loc (loc, etype, exp);
4720 return fold_build2_loc (loc, GT_EXPR, type, exp,
4721 build_int_cst (etype, 0));
4725 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4726 This requires wrap-around arithmetics for the type of the expression.
4727 First make sure that arithmetics in this type is valid, then make sure
4728 that it wraps around. */
4729 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4730 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4731 TYPE_UNSIGNED (etype));
4733 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4735 tree utype, minv, maxv;
4737 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4738 for the type in question, as we rely on this here. */
4739 utype = unsigned_type_for (etype);
4740 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4741 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4742 build_int_cst (TREE_TYPE (maxv), 1), 1);
4743 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4745 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4746 minv, 1, maxv, 1)))
4747 etype = utype;
4748 else
4749 return 0;
4752 high = fold_convert_loc (loc, etype, high);
4753 low = fold_convert_loc (loc, etype, low);
4754 exp = fold_convert_loc (loc, etype, exp);
4756 value = const_binop (MINUS_EXPR, high, low);
4759 if (POINTER_TYPE_P (etype))
4761 if (value != 0 && !TREE_OVERFLOW (value))
4763 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4764 return build_range_check (loc, type,
4765 fold_build_pointer_plus_loc (loc, exp, low),
4766 1, build_int_cst (etype, 0), value);
4768 return 0;
4771 if (value != 0 && !TREE_OVERFLOW (value))
4772 return build_range_check (loc, type,
4773 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4774 1, build_int_cst (etype, 0), value);
4776 return 0;
4779 /* Return the predecessor of VAL in its type, handling the infinite case. */
4781 static tree
4782 range_predecessor (tree val)
4784 tree type = TREE_TYPE (val);
4786 if (INTEGRAL_TYPE_P (type)
4787 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4788 return 0;
4789 else
4790 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4791 build_int_cst (TREE_TYPE (val), 1), 0);
4794 /* Return the successor of VAL in its type, handling the infinite case. */
4796 static tree
4797 range_successor (tree val)
4799 tree type = TREE_TYPE (val);
4801 if (INTEGRAL_TYPE_P (type)
4802 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4803 return 0;
4804 else
4805 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4806 build_int_cst (TREE_TYPE (val), 1), 0);
4809 /* Given two ranges, see if we can merge them into one. Return 1 if we
4810 can, 0 if we can't. Set the output range into the specified parameters. */
4812 bool
4813 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4814 tree high0, int in1_p, tree low1, tree high1)
4816 int no_overlap;
4817 int subset;
4818 int temp;
4819 tree tem;
4820 int in_p;
4821 tree low, high;
4822 int lowequal = ((low0 == 0 && low1 == 0)
4823 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4824 low0, 0, low1, 0)));
4825 int highequal = ((high0 == 0 && high1 == 0)
4826 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4827 high0, 1, high1, 1)));
4829 /* Make range 0 be the range that starts first, or ends last if they
4830 start at the same value. Swap them if it isn't. */
4831 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4832 low0, 0, low1, 0))
4833 || (lowequal
4834 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4835 high1, 1, high0, 1))))
4837 temp = in0_p, in0_p = in1_p, in1_p = temp;
4838 tem = low0, low0 = low1, low1 = tem;
4839 tem = high0, high0 = high1, high1 = tem;
4842 /* Now flag two cases, whether the ranges are disjoint or whether the
4843 second range is totally subsumed in the first. Note that the tests
4844 below are simplified by the ones above. */
4845 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4846 high0, 1, low1, 0));
4847 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4848 high1, 1, high0, 1));
4850 /* We now have four cases, depending on whether we are including or
4851 excluding the two ranges. */
4852 if (in0_p && in1_p)
4854 /* If they don't overlap, the result is false. If the second range
4855 is a subset it is the result. Otherwise, the range is from the start
4856 of the second to the end of the first. */
4857 if (no_overlap)
4858 in_p = 0, low = high = 0;
4859 else if (subset)
4860 in_p = 1, low = low1, high = high1;
4861 else
4862 in_p = 1, low = low1, high = high0;
4865 else if (in0_p && ! in1_p)
4867 /* If they don't overlap, the result is the first range. If they are
4868 equal, the result is false. If the second range is a subset of the
4869 first, and the ranges begin at the same place, we go from just after
4870 the end of the second range to the end of the first. If the second
4871 range is not a subset of the first, or if it is a subset and both
4872 ranges end at the same place, the range starts at the start of the
4873 first range and ends just before the second range.
4874 Otherwise, we can't describe this as a single range. */
4875 if (no_overlap)
4876 in_p = 1, low = low0, high = high0;
4877 else if (lowequal && highequal)
4878 in_p = 0, low = high = 0;
4879 else if (subset && lowequal)
4881 low = range_successor (high1);
4882 high = high0;
4883 in_p = 1;
4884 if (low == 0)
4886 /* We are in the weird situation where high0 > high1 but
4887 high1 has no successor. Punt. */
4888 return 0;
4891 else if (! subset || highequal)
4893 low = low0;
4894 high = range_predecessor (low1);
4895 in_p = 1;
4896 if (high == 0)
4898 /* low0 < low1 but low1 has no predecessor. Punt. */
4899 return 0;
4902 else
4903 return 0;
4906 else if (! in0_p && in1_p)
4908 /* If they don't overlap, the result is the second range. If the second
4909 is a subset of the first, the result is false. Otherwise,
4910 the range starts just after the first range and ends at the
4911 end of the second. */
4912 if (no_overlap)
4913 in_p = 1, low = low1, high = high1;
4914 else if (subset || highequal)
4915 in_p = 0, low = high = 0;
4916 else
4918 low = range_successor (high0);
4919 high = high1;
4920 in_p = 1;
4921 if (low == 0)
4923 /* high1 > high0 but high0 has no successor. Punt. */
4924 return 0;
4929 else
4931 /* The case where we are excluding both ranges. Here the complex case
4932 is if they don't overlap. In that case, the only time we have a
4933 range is if they are adjacent. If the second is a subset of the
4934 first, the result is the first. Otherwise, the range to exclude
4935 starts at the beginning of the first range and ends at the end of the
4936 second. */
4937 if (no_overlap)
4939 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4940 range_successor (high0),
4941 1, low1, 0)))
4942 in_p = 0, low = low0, high = high1;
4943 else
4945 /* Canonicalize - [min, x] into - [-, x]. */
4946 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4947 switch (TREE_CODE (TREE_TYPE (low0)))
4949 case ENUMERAL_TYPE:
4950 if (TYPE_PRECISION (TREE_TYPE (low0))
4951 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4952 break;
4953 /* FALLTHROUGH */
4954 case INTEGER_TYPE:
4955 if (tree_int_cst_equal (low0,
4956 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4957 low0 = 0;
4958 break;
4959 case POINTER_TYPE:
4960 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4961 && integer_zerop (low0))
4962 low0 = 0;
4963 break;
4964 default:
4965 break;
4968 /* Canonicalize - [x, max] into - [x, -]. */
4969 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4970 switch (TREE_CODE (TREE_TYPE (high1)))
4972 case ENUMERAL_TYPE:
4973 if (TYPE_PRECISION (TREE_TYPE (high1))
4974 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4975 break;
4976 /* FALLTHROUGH */
4977 case INTEGER_TYPE:
4978 if (tree_int_cst_equal (high1,
4979 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4980 high1 = 0;
4981 break;
4982 case POINTER_TYPE:
4983 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4984 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4985 high1, 1,
4986 build_int_cst (TREE_TYPE (high1), 1),
4987 1)))
4988 high1 = 0;
4989 break;
4990 default:
4991 break;
4994 /* The ranges might be also adjacent between the maximum and
4995 minimum values of the given type. For
4996 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4997 return + [x + 1, y - 1]. */
4998 if (low0 == 0 && high1 == 0)
5000 low = range_successor (high0);
5001 high = range_predecessor (low1);
5002 if (low == 0 || high == 0)
5003 return 0;
5005 in_p = 1;
5007 else
5008 return 0;
5011 else if (subset)
5012 in_p = 0, low = low0, high = high0;
5013 else
5014 in_p = 0, low = low0, high = high1;
5017 *pin_p = in_p, *plow = low, *phigh = high;
5018 return 1;
5022 /* Subroutine of fold, looking inside expressions of the form
5023 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5024 of the COND_EXPR. This function is being used also to optimize
5025 A op B ? C : A, by reversing the comparison first.
5027 Return a folded expression whose code is not a COND_EXPR
5028 anymore, or NULL_TREE if no folding opportunity is found. */
5030 static tree
5031 fold_cond_expr_with_comparison (location_t loc, tree type,
5032 tree arg0, tree arg1, tree arg2)
5034 enum tree_code comp_code = TREE_CODE (arg0);
5035 tree arg00 = TREE_OPERAND (arg0, 0);
5036 tree arg01 = TREE_OPERAND (arg0, 1);
5037 tree arg1_type = TREE_TYPE (arg1);
5038 tree tem;
5040 STRIP_NOPS (arg1);
5041 STRIP_NOPS (arg2);
5043 /* If we have A op 0 ? A : -A, consider applying the following
5044 transformations:
5046 A == 0? A : -A same as -A
5047 A != 0? A : -A same as A
5048 A >= 0? A : -A same as abs (A)
5049 A > 0? A : -A same as abs (A)
5050 A <= 0? A : -A same as -abs (A)
5051 A < 0? A : -A same as -abs (A)
5053 None of these transformations work for modes with signed
5054 zeros. If A is +/-0, the first two transformations will
5055 change the sign of the result (from +0 to -0, or vice
5056 versa). The last four will fix the sign of the result,
5057 even though the original expressions could be positive or
5058 negative, depending on the sign of A.
5060 Note that all these transformations are correct if A is
5061 NaN, since the two alternatives (A and -A) are also NaNs. */
5062 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5063 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5064 ? real_zerop (arg01)
5065 : integer_zerop (arg01))
5066 && ((TREE_CODE (arg2) == NEGATE_EXPR
5067 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5068 /* In the case that A is of the form X-Y, '-A' (arg2) may
5069 have already been folded to Y-X, check for that. */
5070 || (TREE_CODE (arg1) == MINUS_EXPR
5071 && TREE_CODE (arg2) == MINUS_EXPR
5072 && operand_equal_p (TREE_OPERAND (arg1, 0),
5073 TREE_OPERAND (arg2, 1), 0)
5074 && operand_equal_p (TREE_OPERAND (arg1, 1),
5075 TREE_OPERAND (arg2, 0), 0))))
5076 switch (comp_code)
5078 case EQ_EXPR:
5079 case UNEQ_EXPR:
5080 tem = fold_convert_loc (loc, arg1_type, arg1);
5081 return fold_convert_loc (loc, type, negate_expr (tem));
5082 case NE_EXPR:
5083 case LTGT_EXPR:
5084 return fold_convert_loc (loc, type, arg1);
5085 case UNGE_EXPR:
5086 case UNGT_EXPR:
5087 if (flag_trapping_math)
5088 break;
5089 /* Fall through. */
5090 case GE_EXPR:
5091 case GT_EXPR:
5092 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5093 break;
5094 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5095 return fold_convert_loc (loc, type, tem);
5096 case UNLE_EXPR:
5097 case UNLT_EXPR:
5098 if (flag_trapping_math)
5099 break;
5100 /* FALLTHRU */
5101 case LE_EXPR:
5102 case LT_EXPR:
5103 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5104 break;
5105 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5106 return negate_expr (fold_convert_loc (loc, type, tem));
5107 default:
5108 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5109 break;
5112 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5113 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5114 both transformations are correct when A is NaN: A != 0
5115 is then true, and A == 0 is false. */
5117 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5118 && integer_zerop (arg01) && integer_zerop (arg2))
5120 if (comp_code == NE_EXPR)
5121 return fold_convert_loc (loc, type, arg1);
5122 else if (comp_code == EQ_EXPR)
5123 return build_zero_cst (type);
5126 /* Try some transformations of A op B ? A : B.
5128 A == B? A : B same as B
5129 A != B? A : B same as A
5130 A >= B? A : B same as max (A, B)
5131 A > B? A : B same as max (B, A)
5132 A <= B? A : B same as min (A, B)
5133 A < B? A : B same as min (B, A)
5135 As above, these transformations don't work in the presence
5136 of signed zeros. For example, if A and B are zeros of
5137 opposite sign, the first two transformations will change
5138 the sign of the result. In the last four, the original
5139 expressions give different results for (A=+0, B=-0) and
5140 (A=-0, B=+0), but the transformed expressions do not.
5142 The first two transformations are correct if either A or B
5143 is a NaN. In the first transformation, the condition will
5144 be false, and B will indeed be chosen. In the case of the
5145 second transformation, the condition A != B will be true,
5146 and A will be chosen.
5148 The conversions to max() and min() are not correct if B is
5149 a number and A is not. The conditions in the original
5150 expressions will be false, so all four give B. The min()
5151 and max() versions would give a NaN instead. */
5152 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5153 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5154 /* Avoid these transformations if the COND_EXPR may be used
5155 as an lvalue in the C++ front-end. PR c++/19199. */
5156 && (in_gimple_form
5157 || VECTOR_TYPE_P (type)
5158 || (! lang_GNU_CXX ()
5159 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5160 || ! maybe_lvalue_p (arg1)
5161 || ! maybe_lvalue_p (arg2)))
5163 tree comp_op0 = arg00;
5164 tree comp_op1 = arg01;
5165 tree comp_type = TREE_TYPE (comp_op0);
5167 switch (comp_code)
5169 case EQ_EXPR:
5170 return fold_convert_loc (loc, type, arg2);
5171 case NE_EXPR:
5172 return fold_convert_loc (loc, type, arg1);
5173 case LE_EXPR:
5174 case LT_EXPR:
5175 case UNLE_EXPR:
5176 case UNLT_EXPR:
5177 /* In C++ a ?: expression can be an lvalue, so put the
5178 operand which will be used if they are equal first
5179 so that we can convert this back to the
5180 corresponding COND_EXPR. */
5181 if (!HONOR_NANS (arg1))
5183 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5184 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5185 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5186 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5187 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5188 comp_op1, comp_op0);
5189 return fold_convert_loc (loc, type, tem);
5191 break;
5192 case GE_EXPR:
5193 case GT_EXPR:
5194 case UNGE_EXPR:
5195 case UNGT_EXPR:
5196 if (!HONOR_NANS (arg1))
5198 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5199 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5200 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5201 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5202 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5203 comp_op1, comp_op0);
5204 return fold_convert_loc (loc, type, tem);
5206 break;
5207 case UNEQ_EXPR:
5208 if (!HONOR_NANS (arg1))
5209 return fold_convert_loc (loc, type, arg2);
5210 break;
5211 case LTGT_EXPR:
5212 if (!HONOR_NANS (arg1))
5213 return fold_convert_loc (loc, type, arg1);
5214 break;
5215 default:
5216 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5217 break;
5221 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5222 we might still be able to simplify this. For example,
5223 if C1 is one less or one more than C2, this might have started
5224 out as a MIN or MAX and been transformed by this function.
5225 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5227 if (INTEGRAL_TYPE_P (type)
5228 && TREE_CODE (arg01) == INTEGER_CST
5229 && TREE_CODE (arg2) == INTEGER_CST)
5230 switch (comp_code)
5232 case EQ_EXPR:
5233 if (TREE_CODE (arg1) == INTEGER_CST)
5234 break;
5235 /* We can replace A with C1 in this case. */
5236 arg1 = fold_convert_loc (loc, type, arg01);
5237 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5239 case LT_EXPR:
5240 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5241 MIN_EXPR, to preserve the signedness of the comparison. */
5242 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5243 OEP_ONLY_CONST)
5244 && operand_equal_p (arg01,
5245 const_binop (PLUS_EXPR, arg2,
5246 build_int_cst (type, 1)),
5247 OEP_ONLY_CONST))
5249 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5250 fold_convert_loc (loc, TREE_TYPE (arg00),
5251 arg2));
5252 return fold_convert_loc (loc, type, tem);
5254 break;
5256 case LE_EXPR:
5257 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5258 as above. */
5259 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5260 OEP_ONLY_CONST)
5261 && operand_equal_p (arg01,
5262 const_binop (MINUS_EXPR, arg2,
5263 build_int_cst (type, 1)),
5264 OEP_ONLY_CONST))
5266 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5267 fold_convert_loc (loc, TREE_TYPE (arg00),
5268 arg2));
5269 return fold_convert_loc (loc, type, tem);
5271 break;
5273 case GT_EXPR:
5274 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5275 MAX_EXPR, to preserve the signedness of the comparison. */
5276 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5277 OEP_ONLY_CONST)
5278 && operand_equal_p (arg01,
5279 const_binop (MINUS_EXPR, arg2,
5280 build_int_cst (type, 1)),
5281 OEP_ONLY_CONST))
5283 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5284 fold_convert_loc (loc, TREE_TYPE (arg00),
5285 arg2));
5286 return fold_convert_loc (loc, type, tem);
5288 break;
5290 case GE_EXPR:
5291 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5292 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5293 OEP_ONLY_CONST)
5294 && operand_equal_p (arg01,
5295 const_binop (PLUS_EXPR, arg2,
5296 build_int_cst (type, 1)),
5297 OEP_ONLY_CONST))
5299 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5300 fold_convert_loc (loc, TREE_TYPE (arg00),
5301 arg2));
5302 return fold_convert_loc (loc, type, tem);
5304 break;
5305 case NE_EXPR:
5306 break;
5307 default:
5308 gcc_unreachable ();
5311 return NULL_TREE;
5316 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5317 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5318 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5319 false) >= 2)
5320 #endif
5322 /* EXP is some logical combination of boolean tests. See if we can
5323 merge it into some range test. Return the new tree if so. */
5325 static tree
5326 fold_range_test (location_t loc, enum tree_code code, tree type,
5327 tree op0, tree op1)
5329 int or_op = (code == TRUTH_ORIF_EXPR
5330 || code == TRUTH_OR_EXPR);
5331 int in0_p, in1_p, in_p;
5332 tree low0, low1, low, high0, high1, high;
5333 bool strict_overflow_p = false;
5334 tree tem, lhs, rhs;
5335 const char * const warnmsg = G_("assuming signed overflow does not occur "
5336 "when simplifying range test");
5338 if (!INTEGRAL_TYPE_P (type))
5339 return 0;
5341 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5342 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5344 /* If this is an OR operation, invert both sides; we will invert
5345 again at the end. */
5346 if (or_op)
5347 in0_p = ! in0_p, in1_p = ! in1_p;
5349 /* If both expressions are the same, if we can merge the ranges, and we
5350 can build the range test, return it or it inverted. If one of the
5351 ranges is always true or always false, consider it to be the same
5352 expression as the other. */
5353 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5354 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5355 in1_p, low1, high1)
5356 && 0 != (tem = (build_range_check (loc, type,
5357 lhs != 0 ? lhs
5358 : rhs != 0 ? rhs : integer_zero_node,
5359 in_p, low, high))))
5361 if (strict_overflow_p)
5362 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5363 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5366 /* On machines where the branch cost is expensive, if this is a
5367 short-circuited branch and the underlying object on both sides
5368 is the same, make a non-short-circuit operation. */
5369 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5370 && lhs != 0 && rhs != 0
5371 && (code == TRUTH_ANDIF_EXPR
5372 || code == TRUTH_ORIF_EXPR)
5373 && operand_equal_p (lhs, rhs, 0))
5375 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5376 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5377 which cases we can't do this. */
5378 if (simple_operand_p (lhs))
5379 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5380 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5381 type, op0, op1);
5383 else if (!lang_hooks.decls.global_bindings_p ()
5384 && !CONTAINS_PLACEHOLDER_P (lhs))
5386 tree common = save_expr (lhs);
5388 if (0 != (lhs = build_range_check (loc, type, common,
5389 or_op ? ! in0_p : in0_p,
5390 low0, high0))
5391 && (0 != (rhs = build_range_check (loc, type, common,
5392 or_op ? ! in1_p : in1_p,
5393 low1, high1))))
5395 if (strict_overflow_p)
5396 fold_overflow_warning (warnmsg,
5397 WARN_STRICT_OVERFLOW_COMPARISON);
5398 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5399 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5400 type, lhs, rhs);
5405 return 0;
5408 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5409 bit value. Arrange things so the extra bits will be set to zero if and
5410 only if C is signed-extended to its full width. If MASK is nonzero,
5411 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5413 static tree
5414 unextend (tree c, int p, int unsignedp, tree mask)
5416 tree type = TREE_TYPE (c);
5417 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5418 tree temp;
5420 if (p == modesize || unsignedp)
5421 return c;
5423 /* We work by getting just the sign bit into the low-order bit, then
5424 into the high-order bit, then sign-extend. We then XOR that value
5425 with C. */
5426 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5428 /* We must use a signed type in order to get an arithmetic right shift.
5429 However, we must also avoid introducing accidental overflows, so that
5430 a subsequent call to integer_zerop will work. Hence we must
5431 do the type conversion here. At this point, the constant is either
5432 zero or one, and the conversion to a signed type can never overflow.
5433 We could get an overflow if this conversion is done anywhere else. */
5434 if (TYPE_UNSIGNED (type))
5435 temp = fold_convert (signed_type_for (type), temp);
5437 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5438 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5439 if (mask != 0)
5440 temp = const_binop (BIT_AND_EXPR, temp,
5441 fold_convert (TREE_TYPE (c), mask));
5442 /* If necessary, convert the type back to match the type of C. */
5443 if (TYPE_UNSIGNED (type))
5444 temp = fold_convert (type, temp);
5446 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5449 /* For an expression that has the form
5450 (A && B) || ~B
5452 (A || B) && ~B,
5453 we can drop one of the inner expressions and simplify to
5454 A || ~B
5456 A && ~B
5457 LOC is the location of the resulting expression. OP is the inner
5458 logical operation; the left-hand side in the examples above, while CMPOP
5459 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5460 removing a condition that guards another, as in
5461 (A != NULL && A->...) || A == NULL
5462 which we must not transform. If RHS_ONLY is true, only eliminate the
5463 right-most operand of the inner logical operation. */
5465 static tree
5466 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5467 bool rhs_only)
5469 tree type = TREE_TYPE (cmpop);
5470 enum tree_code code = TREE_CODE (cmpop);
5471 enum tree_code truthop_code = TREE_CODE (op);
5472 tree lhs = TREE_OPERAND (op, 0);
5473 tree rhs = TREE_OPERAND (op, 1);
5474 tree orig_lhs = lhs, orig_rhs = rhs;
5475 enum tree_code rhs_code = TREE_CODE (rhs);
5476 enum tree_code lhs_code = TREE_CODE (lhs);
5477 enum tree_code inv_code;
5479 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5480 return NULL_TREE;
5482 if (TREE_CODE_CLASS (code) != tcc_comparison)
5483 return NULL_TREE;
5485 if (rhs_code == truthop_code)
5487 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5488 if (newrhs != NULL_TREE)
5490 rhs = newrhs;
5491 rhs_code = TREE_CODE (rhs);
5494 if (lhs_code == truthop_code && !rhs_only)
5496 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5497 if (newlhs != NULL_TREE)
5499 lhs = newlhs;
5500 lhs_code = TREE_CODE (lhs);
5504 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5505 if (inv_code == rhs_code
5506 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5507 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5508 return lhs;
5509 if (!rhs_only && inv_code == lhs_code
5510 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5511 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5512 return rhs;
5513 if (rhs != orig_rhs || lhs != orig_lhs)
5514 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5515 lhs, rhs);
5516 return NULL_TREE;
5519 /* Find ways of folding logical expressions of LHS and RHS:
5520 Try to merge two comparisons to the same innermost item.
5521 Look for range tests like "ch >= '0' && ch <= '9'".
5522 Look for combinations of simple terms on machines with expensive branches
5523 and evaluate the RHS unconditionally.
5525 For example, if we have p->a == 2 && p->b == 4 and we can make an
5526 object large enough to span both A and B, we can do this with a comparison
5527 against the object ANDed with the a mask.
5529 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5530 operations to do this with one comparison.
5532 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5533 function and the one above.
5535 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5536 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5538 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5539 two operands.
5541 We return the simplified tree or 0 if no optimization is possible. */
5543 static tree
5544 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5545 tree lhs, tree rhs)
5547 /* If this is the "or" of two comparisons, we can do something if
5548 the comparisons are NE_EXPR. If this is the "and", we can do something
5549 if the comparisons are EQ_EXPR. I.e.,
5550 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5552 WANTED_CODE is this operation code. For single bit fields, we can
5553 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5554 comparison for one-bit fields. */
5556 enum tree_code wanted_code;
5557 enum tree_code lcode, rcode;
5558 tree ll_arg, lr_arg, rl_arg, rr_arg;
5559 tree ll_inner, lr_inner, rl_inner, rr_inner;
5560 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5561 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5562 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5563 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5564 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5565 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5566 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5567 machine_mode lnmode, rnmode;
5568 tree ll_mask, lr_mask, rl_mask, rr_mask;
5569 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5570 tree l_const, r_const;
5571 tree lntype, rntype, result;
5572 HOST_WIDE_INT first_bit, end_bit;
5573 int volatilep;
5575 /* Start by getting the comparison codes. Fail if anything is volatile.
5576 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5577 it were surrounded with a NE_EXPR. */
5579 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5580 return 0;
5582 lcode = TREE_CODE (lhs);
5583 rcode = TREE_CODE (rhs);
5585 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5587 lhs = build2 (NE_EXPR, truth_type, lhs,
5588 build_int_cst (TREE_TYPE (lhs), 0));
5589 lcode = NE_EXPR;
5592 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5594 rhs = build2 (NE_EXPR, truth_type, rhs,
5595 build_int_cst (TREE_TYPE (rhs), 0));
5596 rcode = NE_EXPR;
5599 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5600 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5601 return 0;
5603 ll_arg = TREE_OPERAND (lhs, 0);
5604 lr_arg = TREE_OPERAND (lhs, 1);
5605 rl_arg = TREE_OPERAND (rhs, 0);
5606 rr_arg = TREE_OPERAND (rhs, 1);
5608 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5609 if (simple_operand_p (ll_arg)
5610 && simple_operand_p (lr_arg))
5612 if (operand_equal_p (ll_arg, rl_arg, 0)
5613 && operand_equal_p (lr_arg, rr_arg, 0))
5615 result = combine_comparisons (loc, code, lcode, rcode,
5616 truth_type, ll_arg, lr_arg);
5617 if (result)
5618 return result;
5620 else if (operand_equal_p (ll_arg, rr_arg, 0)
5621 && operand_equal_p (lr_arg, rl_arg, 0))
5623 result = combine_comparisons (loc, code, lcode,
5624 swap_tree_comparison (rcode),
5625 truth_type, ll_arg, lr_arg);
5626 if (result)
5627 return result;
5631 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5632 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5634 /* If the RHS can be evaluated unconditionally and its operands are
5635 simple, it wins to evaluate the RHS unconditionally on machines
5636 with expensive branches. In this case, this isn't a comparison
5637 that can be merged. */
5639 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5640 false) >= 2
5641 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5642 && simple_operand_p (rl_arg)
5643 && simple_operand_p (rr_arg))
5645 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5646 if (code == TRUTH_OR_EXPR
5647 && lcode == NE_EXPR && integer_zerop (lr_arg)
5648 && rcode == NE_EXPR && integer_zerop (rr_arg)
5649 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5650 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5651 return build2_loc (loc, NE_EXPR, truth_type,
5652 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5653 ll_arg, rl_arg),
5654 build_int_cst (TREE_TYPE (ll_arg), 0));
5656 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5657 if (code == TRUTH_AND_EXPR
5658 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5659 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5660 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5661 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5662 return build2_loc (loc, EQ_EXPR, truth_type,
5663 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5664 ll_arg, rl_arg),
5665 build_int_cst (TREE_TYPE (ll_arg), 0));
5668 /* See if the comparisons can be merged. Then get all the parameters for
5669 each side. */
5671 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5672 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5673 return 0;
5675 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5676 volatilep = 0;
5677 ll_inner = decode_field_reference (loc, &ll_arg,
5678 &ll_bitsize, &ll_bitpos, &ll_mode,
5679 &ll_unsignedp, &ll_reversep, &volatilep,
5680 &ll_mask, &ll_and_mask);
5681 lr_inner = decode_field_reference (loc, &lr_arg,
5682 &lr_bitsize, &lr_bitpos, &lr_mode,
5683 &lr_unsignedp, &lr_reversep, &volatilep,
5684 &lr_mask, &lr_and_mask);
5685 rl_inner = decode_field_reference (loc, &rl_arg,
5686 &rl_bitsize, &rl_bitpos, &rl_mode,
5687 &rl_unsignedp, &rl_reversep, &volatilep,
5688 &rl_mask, &rl_and_mask);
5689 rr_inner = decode_field_reference (loc, &rr_arg,
5690 &rr_bitsize, &rr_bitpos, &rr_mode,
5691 &rr_unsignedp, &rr_reversep, &volatilep,
5692 &rr_mask, &rr_and_mask);
5694 /* It must be true that the inner operation on the lhs of each
5695 comparison must be the same if we are to be able to do anything.
5696 Then see if we have constants. If not, the same must be true for
5697 the rhs's. */
5698 if (volatilep
5699 || ll_reversep != rl_reversep
5700 || ll_inner == 0 || rl_inner == 0
5701 || ! operand_equal_p (ll_inner, rl_inner, 0))
5702 return 0;
5704 if (TREE_CODE (lr_arg) == INTEGER_CST
5705 && TREE_CODE (rr_arg) == INTEGER_CST)
5707 l_const = lr_arg, r_const = rr_arg;
5708 lr_reversep = ll_reversep;
5710 else if (lr_reversep != rr_reversep
5711 || lr_inner == 0 || rr_inner == 0
5712 || ! operand_equal_p (lr_inner, rr_inner, 0))
5713 return 0;
5714 else
5715 l_const = r_const = 0;
5717 /* If either comparison code is not correct for our logical operation,
5718 fail. However, we can convert a one-bit comparison against zero into
5719 the opposite comparison against that bit being set in the field. */
5721 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5722 if (lcode != wanted_code)
5724 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5726 /* Make the left operand unsigned, since we are only interested
5727 in the value of one bit. Otherwise we are doing the wrong
5728 thing below. */
5729 ll_unsignedp = 1;
5730 l_const = ll_mask;
5732 else
5733 return 0;
5736 /* This is analogous to the code for l_const above. */
5737 if (rcode != wanted_code)
5739 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5741 rl_unsignedp = 1;
5742 r_const = rl_mask;
5744 else
5745 return 0;
5748 /* See if we can find a mode that contains both fields being compared on
5749 the left. If we can't, fail. Otherwise, update all constants and masks
5750 to be relative to a field of that size. */
5751 first_bit = MIN (ll_bitpos, rl_bitpos);
5752 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5753 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5754 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5755 volatilep);
5756 if (lnmode == VOIDmode)
5757 return 0;
5759 lnbitsize = GET_MODE_BITSIZE (lnmode);
5760 lnbitpos = first_bit & ~ (lnbitsize - 1);
5761 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5762 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5764 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5766 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5767 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5770 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5771 size_int (xll_bitpos));
5772 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5773 size_int (xrl_bitpos));
5775 if (l_const)
5777 l_const = fold_convert_loc (loc, lntype, l_const);
5778 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5779 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5780 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5781 fold_build1_loc (loc, BIT_NOT_EXPR,
5782 lntype, ll_mask))))
5784 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5786 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5789 if (r_const)
5791 r_const = fold_convert_loc (loc, lntype, r_const);
5792 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5793 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5794 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5795 fold_build1_loc (loc, BIT_NOT_EXPR,
5796 lntype, rl_mask))))
5798 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5800 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5804 /* If the right sides are not constant, do the same for it. Also,
5805 disallow this optimization if a size or signedness mismatch occurs
5806 between the left and right sides. */
5807 if (l_const == 0)
5809 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5810 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5811 /* Make sure the two fields on the right
5812 correspond to the left without being swapped. */
5813 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5814 return 0;
5816 first_bit = MIN (lr_bitpos, rr_bitpos);
5817 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5818 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5819 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5820 volatilep);
5821 if (rnmode == VOIDmode)
5822 return 0;
5824 rnbitsize = GET_MODE_BITSIZE (rnmode);
5825 rnbitpos = first_bit & ~ (rnbitsize - 1);
5826 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5827 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5829 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5831 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5832 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5835 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5836 rntype, lr_mask),
5837 size_int (xlr_bitpos));
5838 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5839 rntype, rr_mask),
5840 size_int (xrr_bitpos));
5842 /* Make a mask that corresponds to both fields being compared.
5843 Do this for both items being compared. If the operands are the
5844 same size and the bits being compared are in the same position
5845 then we can do this by masking both and comparing the masked
5846 results. */
5847 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5848 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5849 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5851 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5852 lntype, lnbitsize, lnbitpos,
5853 ll_unsignedp || rl_unsignedp, ll_reversep);
5854 if (! all_ones_mask_p (ll_mask, lnbitsize))
5855 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5857 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5858 rntype, rnbitsize, rnbitpos,
5859 lr_unsignedp || rr_unsignedp, lr_reversep);
5860 if (! all_ones_mask_p (lr_mask, rnbitsize))
5861 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5863 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5866 /* There is still another way we can do something: If both pairs of
5867 fields being compared are adjacent, we may be able to make a wider
5868 field containing them both.
5870 Note that we still must mask the lhs/rhs expressions. Furthermore,
5871 the mask must be shifted to account for the shift done by
5872 make_bit_field_ref. */
5873 if ((ll_bitsize + ll_bitpos == rl_bitpos
5874 && lr_bitsize + lr_bitpos == rr_bitpos)
5875 || (ll_bitpos == rl_bitpos + rl_bitsize
5876 && lr_bitpos == rr_bitpos + rr_bitsize))
5878 tree type;
5880 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5881 ll_bitsize + rl_bitsize,
5882 MIN (ll_bitpos, rl_bitpos),
5883 ll_unsignedp, ll_reversep);
5884 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5885 lr_bitsize + rr_bitsize,
5886 MIN (lr_bitpos, rr_bitpos),
5887 lr_unsignedp, lr_reversep);
5889 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5890 size_int (MIN (xll_bitpos, xrl_bitpos)));
5891 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5892 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5894 /* Convert to the smaller type before masking out unwanted bits. */
5895 type = lntype;
5896 if (lntype != rntype)
5898 if (lnbitsize > rnbitsize)
5900 lhs = fold_convert_loc (loc, rntype, lhs);
5901 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5902 type = rntype;
5904 else if (lnbitsize < rnbitsize)
5906 rhs = fold_convert_loc (loc, lntype, rhs);
5907 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5908 type = lntype;
5912 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5913 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5915 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5916 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5918 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5921 return 0;
5924 /* Handle the case of comparisons with constants. If there is something in
5925 common between the masks, those bits of the constants must be the same.
5926 If not, the condition is always false. Test for this to avoid generating
5927 incorrect code below. */
5928 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5929 if (! integer_zerop (result)
5930 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5931 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5933 if (wanted_code == NE_EXPR)
5935 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5936 return constant_boolean_node (true, truth_type);
5938 else
5940 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5941 return constant_boolean_node (false, truth_type);
5945 /* Construct the expression we will return. First get the component
5946 reference we will make. Unless the mask is all ones the width of
5947 that field, perform the mask operation. Then compare with the
5948 merged constant. */
5949 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5950 lntype, lnbitsize, lnbitpos,
5951 ll_unsignedp || rl_unsignedp, ll_reversep);
5953 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5954 if (! all_ones_mask_p (ll_mask, lnbitsize))
5955 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5957 return build2_loc (loc, wanted_code, truth_type, result,
5958 const_binop (BIT_IOR_EXPR, l_const, r_const));
5961 /* T is an integer expression that is being multiplied, divided, or taken a
5962 modulus (CODE says which and what kind of divide or modulus) by a
5963 constant C. See if we can eliminate that operation by folding it with
5964 other operations already in T. WIDE_TYPE, if non-null, is a type that
5965 should be used for the computation if wider than our type.
5967 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5968 (X * 2) + (Y * 4). We must, however, be assured that either the original
5969 expression would not overflow or that overflow is undefined for the type
5970 in the language in question.
5972 If we return a non-null expression, it is an equivalent form of the
5973 original computation, but need not be in the original type.
5975 We set *STRICT_OVERFLOW_P to true if the return values depends on
5976 signed overflow being undefined. Otherwise we do not change
5977 *STRICT_OVERFLOW_P. */
5979 static tree
5980 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5981 bool *strict_overflow_p)
5983 /* To avoid exponential search depth, refuse to allow recursion past
5984 three levels. Beyond that (1) it's highly unlikely that we'll find
5985 something interesting and (2) we've probably processed it before
5986 when we built the inner expression. */
5988 static int depth;
5989 tree ret;
5991 if (depth > 3)
5992 return NULL;
5994 depth++;
5995 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5996 depth--;
5998 return ret;
6001 static tree
6002 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6003 bool *strict_overflow_p)
6005 tree type = TREE_TYPE (t);
6006 enum tree_code tcode = TREE_CODE (t);
6007 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6008 > GET_MODE_SIZE (TYPE_MODE (type)))
6009 ? wide_type : type);
6010 tree t1, t2;
6011 int same_p = tcode == code;
6012 tree op0 = NULL_TREE, op1 = NULL_TREE;
6013 bool sub_strict_overflow_p;
6015 /* Don't deal with constants of zero here; they confuse the code below. */
6016 if (integer_zerop (c))
6017 return NULL_TREE;
6019 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6020 op0 = TREE_OPERAND (t, 0);
6022 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6023 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6025 /* Note that we need not handle conditional operations here since fold
6026 already handles those cases. So just do arithmetic here. */
6027 switch (tcode)
6029 case INTEGER_CST:
6030 /* For a constant, we can always simplify if we are a multiply
6031 or (for divide and modulus) if it is a multiple of our constant. */
6032 if (code == MULT_EXPR
6033 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6035 tree tem = const_binop (code, fold_convert (ctype, t),
6036 fold_convert (ctype, c));
6037 /* If the multiplication overflowed, we lost information on it.
6038 See PR68142 and PR69845. */
6039 if (TREE_OVERFLOW (tem))
6040 return NULL_TREE;
6041 return tem;
6043 break;
6045 CASE_CONVERT: case NON_LVALUE_EXPR:
6046 /* If op0 is an expression ... */
6047 if ((COMPARISON_CLASS_P (op0)
6048 || UNARY_CLASS_P (op0)
6049 || BINARY_CLASS_P (op0)
6050 || VL_EXP_CLASS_P (op0)
6051 || EXPRESSION_CLASS_P (op0))
6052 /* ... and has wrapping overflow, and its type is smaller
6053 than ctype, then we cannot pass through as widening. */
6054 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6055 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6056 && (TYPE_PRECISION (ctype)
6057 > TYPE_PRECISION (TREE_TYPE (op0))))
6058 /* ... or this is a truncation (t is narrower than op0),
6059 then we cannot pass through this narrowing. */
6060 || (TYPE_PRECISION (type)
6061 < TYPE_PRECISION (TREE_TYPE (op0)))
6062 /* ... or signedness changes for division or modulus,
6063 then we cannot pass through this conversion. */
6064 || (code != MULT_EXPR
6065 && (TYPE_UNSIGNED (ctype)
6066 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6067 /* ... or has undefined overflow while the converted to
6068 type has not, we cannot do the operation in the inner type
6069 as that would introduce undefined overflow. */
6070 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6071 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6072 && !TYPE_OVERFLOW_UNDEFINED (type))))
6073 break;
6075 /* Pass the constant down and see if we can make a simplification. If
6076 we can, replace this expression with the inner simplification for
6077 possible later conversion to our or some other type. */
6078 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6079 && TREE_CODE (t2) == INTEGER_CST
6080 && !TREE_OVERFLOW (t2)
6081 && (0 != (t1 = extract_muldiv (op0, t2, code,
6082 code == MULT_EXPR
6083 ? ctype : NULL_TREE,
6084 strict_overflow_p))))
6085 return t1;
6086 break;
6088 case ABS_EXPR:
6089 /* If widening the type changes it from signed to unsigned, then we
6090 must avoid building ABS_EXPR itself as unsigned. */
6091 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6093 tree cstype = (*signed_type_for) (ctype);
6094 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6095 != 0)
6097 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6098 return fold_convert (ctype, t1);
6100 break;
6102 /* If the constant is negative, we cannot simplify this. */
6103 if (tree_int_cst_sgn (c) == -1)
6104 break;
6105 /* FALLTHROUGH */
6106 case NEGATE_EXPR:
6107 /* For division and modulus, type can't be unsigned, as e.g.
6108 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6109 For signed types, even with wrapping overflow, this is fine. */
6110 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6111 break;
6112 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6113 != 0)
6114 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6115 break;
6117 case MIN_EXPR: case MAX_EXPR:
6118 /* If widening the type changes the signedness, then we can't perform
6119 this optimization as that changes the result. */
6120 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6121 break;
6123 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6124 sub_strict_overflow_p = false;
6125 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6126 &sub_strict_overflow_p)) != 0
6127 && (t2 = extract_muldiv (op1, c, code, wide_type,
6128 &sub_strict_overflow_p)) != 0)
6130 if (tree_int_cst_sgn (c) < 0)
6131 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
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));
6137 break;
6139 case LSHIFT_EXPR: case RSHIFT_EXPR:
6140 /* If the second operand is constant, this is a multiplication
6141 or floor division, by a power of two, so we can treat it that
6142 way unless the multiplier or divisor overflows. Signed
6143 left-shift overflow is implementation-defined rather than
6144 undefined in C90, so do not convert signed left shift into
6145 multiplication. */
6146 if (TREE_CODE (op1) == INTEGER_CST
6147 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6148 /* const_binop may not detect overflow correctly,
6149 so check for it explicitly here. */
6150 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6151 && 0 != (t1 = fold_convert (ctype,
6152 const_binop (LSHIFT_EXPR,
6153 size_one_node,
6154 op1)))
6155 && !TREE_OVERFLOW (t1))
6156 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6157 ? MULT_EXPR : FLOOR_DIV_EXPR,
6158 ctype,
6159 fold_convert (ctype, op0),
6160 t1),
6161 c, code, wide_type, strict_overflow_p);
6162 break;
6164 case PLUS_EXPR: case MINUS_EXPR:
6165 /* See if we can eliminate the operation on both sides. If we can, we
6166 can return a new PLUS or MINUS. If we can't, the only remaining
6167 cases where we can do anything are if the second operand is a
6168 constant. */
6169 sub_strict_overflow_p = false;
6170 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6171 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6172 if (t1 != 0 && t2 != 0
6173 && (code == MULT_EXPR
6174 /* If not multiplication, we can only do this if both operands
6175 are divisible by c. */
6176 || (multiple_of_p (ctype, op0, c)
6177 && multiple_of_p (ctype, op1, c))))
6179 if (sub_strict_overflow_p)
6180 *strict_overflow_p = true;
6181 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6182 fold_convert (ctype, t2));
6185 /* If this was a subtraction, negate OP1 and set it to be an addition.
6186 This simplifies the logic below. */
6187 if (tcode == MINUS_EXPR)
6189 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6190 /* If OP1 was not easily negatable, the constant may be OP0. */
6191 if (TREE_CODE (op0) == INTEGER_CST)
6193 std::swap (op0, op1);
6194 std::swap (t1, t2);
6198 if (TREE_CODE (op1) != INTEGER_CST)
6199 break;
6201 /* If either OP1 or C are negative, this optimization is not safe for
6202 some of the division and remainder types while for others we need
6203 to change the code. */
6204 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6206 if (code == CEIL_DIV_EXPR)
6207 code = FLOOR_DIV_EXPR;
6208 else if (code == FLOOR_DIV_EXPR)
6209 code = CEIL_DIV_EXPR;
6210 else if (code != MULT_EXPR
6211 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6212 break;
6215 /* If it's a multiply or a division/modulus operation of a multiple
6216 of our constant, do the operation and verify it doesn't overflow. */
6217 if (code == MULT_EXPR
6218 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6220 op1 = const_binop (code, fold_convert (ctype, op1),
6221 fold_convert (ctype, c));
6222 /* We allow the constant to overflow with wrapping semantics. */
6223 if (op1 == 0
6224 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6225 break;
6227 else
6228 break;
6230 /* If we have an unsigned type, we cannot widen the operation since it
6231 will change the result if the original computation overflowed. */
6232 if (TYPE_UNSIGNED (ctype) && ctype != type)
6233 break;
6235 /* If we were able to eliminate our operation from the first side,
6236 apply our operation to the second side and reform the PLUS. */
6237 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6238 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6240 /* The last case is if we are a multiply. In that case, we can
6241 apply the distributive law to commute the multiply and addition
6242 if the multiplication of the constants doesn't overflow
6243 and overflow is defined. With undefined overflow
6244 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6245 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6246 return fold_build2 (tcode, ctype,
6247 fold_build2 (code, ctype,
6248 fold_convert (ctype, op0),
6249 fold_convert (ctype, c)),
6250 op1);
6252 break;
6254 case MULT_EXPR:
6255 /* We have a special case here if we are doing something like
6256 (C * 8) % 4 since we know that's zero. */
6257 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6258 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6259 /* If the multiplication can overflow we cannot optimize this. */
6260 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6261 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6262 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6264 *strict_overflow_p = true;
6265 return omit_one_operand (type, integer_zero_node, op0);
6268 /* ... fall through ... */
6270 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6271 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6272 /* If we can extract our operation from the LHS, do so and return a
6273 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6274 do something only if the second operand is a constant. */
6275 if (same_p
6276 && (t1 = extract_muldiv (op0, c, code, wide_type,
6277 strict_overflow_p)) != 0)
6278 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6279 fold_convert (ctype, op1));
6280 else if (tcode == MULT_EXPR && code == MULT_EXPR
6281 && (t1 = extract_muldiv (op1, c, code, wide_type,
6282 strict_overflow_p)) != 0)
6283 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6284 fold_convert (ctype, t1));
6285 else if (TREE_CODE (op1) != INTEGER_CST)
6286 return 0;
6288 /* If these are the same operation types, we can associate them
6289 assuming no overflow. */
6290 if (tcode == code)
6292 bool overflow_p = false;
6293 bool overflow_mul_p;
6294 signop sign = TYPE_SIGN (ctype);
6295 unsigned prec = TYPE_PRECISION (ctype);
6296 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6297 wi::to_wide (c, prec),
6298 sign, &overflow_mul_p);
6299 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6300 if (overflow_mul_p
6301 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6302 overflow_p = true;
6303 if (!overflow_p)
6304 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6305 wide_int_to_tree (ctype, mul));
6308 /* If these operations "cancel" each other, we have the main
6309 optimizations of this pass, which occur when either constant is a
6310 multiple of the other, in which case we replace this with either an
6311 operation or CODE or TCODE.
6313 If we have an unsigned type, we cannot do this since it will change
6314 the result if the original computation overflowed. */
6315 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6316 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6317 || (tcode == MULT_EXPR
6318 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6319 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6320 && code != MULT_EXPR)))
6322 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6324 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6325 *strict_overflow_p = true;
6326 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6327 fold_convert (ctype,
6328 const_binop (TRUNC_DIV_EXPR,
6329 op1, c)));
6331 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6333 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6334 *strict_overflow_p = true;
6335 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6336 fold_convert (ctype,
6337 const_binop (TRUNC_DIV_EXPR,
6338 c, op1)));
6341 break;
6343 default:
6344 break;
6347 return 0;
6350 /* Return a node which has the indicated constant VALUE (either 0 or
6351 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6352 and is of the indicated TYPE. */
6354 tree
6355 constant_boolean_node (bool value, tree type)
6357 if (type == integer_type_node)
6358 return value ? integer_one_node : integer_zero_node;
6359 else if (type == boolean_type_node)
6360 return value ? boolean_true_node : boolean_false_node;
6361 else if (TREE_CODE (type) == VECTOR_TYPE)
6362 return build_vector_from_val (type,
6363 build_int_cst (TREE_TYPE (type),
6364 value ? -1 : 0));
6365 else
6366 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6370 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6371 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6372 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6373 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6374 COND is the first argument to CODE; otherwise (as in the example
6375 given here), it is the second argument. TYPE is the type of the
6376 original expression. Return NULL_TREE if no simplification is
6377 possible. */
6379 static tree
6380 fold_binary_op_with_conditional_arg (location_t loc,
6381 enum tree_code code,
6382 tree type, tree op0, tree op1,
6383 tree cond, tree arg, int cond_first_p)
6385 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6386 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6387 tree test, true_value, false_value;
6388 tree lhs = NULL_TREE;
6389 tree rhs = NULL_TREE;
6390 enum tree_code cond_code = COND_EXPR;
6392 if (TREE_CODE (cond) == COND_EXPR
6393 || TREE_CODE (cond) == VEC_COND_EXPR)
6395 test = TREE_OPERAND (cond, 0);
6396 true_value = TREE_OPERAND (cond, 1);
6397 false_value = TREE_OPERAND (cond, 2);
6398 /* If this operand throws an expression, then it does not make
6399 sense to try to perform a logical or arithmetic operation
6400 involving it. */
6401 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6402 lhs = true_value;
6403 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6404 rhs = false_value;
6406 else if (!(TREE_CODE (type) != VECTOR_TYPE
6407 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6409 tree testtype = TREE_TYPE (cond);
6410 test = cond;
6411 true_value = constant_boolean_node (true, testtype);
6412 false_value = constant_boolean_node (false, testtype);
6414 else
6415 /* Detect the case of mixing vector and scalar types - bail out. */
6416 return NULL_TREE;
6418 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6419 cond_code = VEC_COND_EXPR;
6421 /* This transformation is only worthwhile if we don't have to wrap ARG
6422 in a SAVE_EXPR and the operation can be simplified without recursing
6423 on at least one of the branches once its pushed inside the COND_EXPR. */
6424 if (!TREE_CONSTANT (arg)
6425 && (TREE_SIDE_EFFECTS (arg)
6426 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6427 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6428 return NULL_TREE;
6430 arg = fold_convert_loc (loc, arg_type, arg);
6431 if (lhs == 0)
6433 true_value = fold_convert_loc (loc, cond_type, true_value);
6434 if (cond_first_p)
6435 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6436 else
6437 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6439 if (rhs == 0)
6441 false_value = fold_convert_loc (loc, cond_type, false_value);
6442 if (cond_first_p)
6443 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6444 else
6445 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6448 /* Check that we have simplified at least one of the branches. */
6449 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6450 return NULL_TREE;
6452 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6456 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6458 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6459 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6460 ADDEND is the same as X.
6462 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6463 and finite. The problematic cases are when X is zero, and its mode
6464 has signed zeros. In the case of rounding towards -infinity,
6465 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6466 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6468 bool
6469 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6471 if (!real_zerop (addend))
6472 return false;
6474 /* Don't allow the fold with -fsignaling-nans. */
6475 if (HONOR_SNANS (element_mode (type)))
6476 return false;
6478 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6479 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6480 return true;
6482 /* In a vector or complex, we would need to check the sign of all zeros. */
6483 if (TREE_CODE (addend) != REAL_CST)
6484 return false;
6486 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6487 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6488 negate = !negate;
6490 /* The mode has signed zeros, and we have to honor their sign.
6491 In this situation, there is only one case we can return true for.
6492 X - 0 is the same as X unless rounding towards -infinity is
6493 supported. */
6494 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6497 /* Subroutine of fold() that optimizes comparisons of a division by
6498 a nonzero integer constant against an integer constant, i.e.
6499 X/C1 op C2.
6501 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6502 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6503 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6505 The function returns the constant folded tree if a simplification
6506 can be made, and NULL_TREE otherwise. */
6508 static tree
6509 fold_div_compare (location_t loc,
6510 enum tree_code code, tree type, tree arg0, tree arg1)
6512 tree prod, tmp, hi, lo;
6513 tree arg00 = TREE_OPERAND (arg0, 0);
6514 tree arg01 = TREE_OPERAND (arg0, 1);
6515 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6516 bool neg_overflow = false;
6517 bool overflow;
6519 /* We have to do this the hard way to detect unsigned overflow.
6520 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6521 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6522 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6523 neg_overflow = false;
6525 if (sign == UNSIGNED)
6527 tmp = int_const_binop (MINUS_EXPR, arg01,
6528 build_int_cst (TREE_TYPE (arg01), 1));
6529 lo = prod;
6531 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6532 val = wi::add (prod, tmp, sign, &overflow);
6533 hi = force_fit_type (TREE_TYPE (arg00), val,
6534 -1, overflow | TREE_OVERFLOW (prod));
6536 else if (tree_int_cst_sgn (arg01) >= 0)
6538 tmp = int_const_binop (MINUS_EXPR, arg01,
6539 build_int_cst (TREE_TYPE (arg01), 1));
6540 switch (tree_int_cst_sgn (arg1))
6542 case -1:
6543 neg_overflow = true;
6544 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6545 hi = prod;
6546 break;
6548 case 0:
6549 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6550 hi = tmp;
6551 break;
6553 case 1:
6554 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6555 lo = prod;
6556 break;
6558 default:
6559 gcc_unreachable ();
6562 else
6564 /* A negative divisor reverses the relational operators. */
6565 code = swap_tree_comparison (code);
6567 tmp = int_const_binop (PLUS_EXPR, arg01,
6568 build_int_cst (TREE_TYPE (arg01), 1));
6569 switch (tree_int_cst_sgn (arg1))
6571 case -1:
6572 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6573 lo = prod;
6574 break;
6576 case 0:
6577 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6578 lo = tmp;
6579 break;
6581 case 1:
6582 neg_overflow = true;
6583 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6584 hi = prod;
6585 break;
6587 default:
6588 gcc_unreachable ();
6592 switch (code)
6594 case EQ_EXPR:
6595 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6596 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6597 if (TREE_OVERFLOW (hi))
6598 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6599 if (TREE_OVERFLOW (lo))
6600 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6601 return build_range_check (loc, type, arg00, 1, lo, hi);
6603 case NE_EXPR:
6604 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6605 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6606 if (TREE_OVERFLOW (hi))
6607 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6608 if (TREE_OVERFLOW (lo))
6609 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6610 return build_range_check (loc, type, arg00, 0, lo, hi);
6612 case LT_EXPR:
6613 if (TREE_OVERFLOW (lo))
6615 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6616 return omit_one_operand_loc (loc, type, tmp, arg00);
6618 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6620 case LE_EXPR:
6621 if (TREE_OVERFLOW (hi))
6623 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6624 return omit_one_operand_loc (loc, type, tmp, arg00);
6626 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6628 case GT_EXPR:
6629 if (TREE_OVERFLOW (hi))
6631 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6632 return omit_one_operand_loc (loc, type, tmp, arg00);
6634 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6636 case GE_EXPR:
6637 if (TREE_OVERFLOW (lo))
6639 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6640 return omit_one_operand_loc (loc, type, tmp, arg00);
6642 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6644 default:
6645 break;
6648 return NULL_TREE;
6652 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6653 equality/inequality test, then return a simplified form of the test
6654 using a sign testing. Otherwise return NULL. TYPE is the desired
6655 result type. */
6657 static tree
6658 fold_single_bit_test_into_sign_test (location_t loc,
6659 enum tree_code code, tree arg0, tree arg1,
6660 tree result_type)
6662 /* If this is testing a single bit, we can optimize the test. */
6663 if ((code == NE_EXPR || code == EQ_EXPR)
6664 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6665 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6667 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6668 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6669 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6671 if (arg00 != NULL_TREE
6672 /* This is only a win if casting to a signed type is cheap,
6673 i.e. when arg00's type is not a partial mode. */
6674 && TYPE_PRECISION (TREE_TYPE (arg00))
6675 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6677 tree stype = signed_type_for (TREE_TYPE (arg00));
6678 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6679 result_type,
6680 fold_convert_loc (loc, stype, arg00),
6681 build_int_cst (stype, 0));
6685 return NULL_TREE;
6688 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6689 equality/inequality test, then return a simplified form of
6690 the test using shifts and logical operations. Otherwise return
6691 NULL. TYPE is the desired result type. */
6693 tree
6694 fold_single_bit_test (location_t loc, enum tree_code code,
6695 tree arg0, tree arg1, tree result_type)
6697 /* If this is testing a single bit, we can optimize the test. */
6698 if ((code == NE_EXPR || code == EQ_EXPR)
6699 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6700 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6702 tree inner = TREE_OPERAND (arg0, 0);
6703 tree type = TREE_TYPE (arg0);
6704 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6705 machine_mode operand_mode = TYPE_MODE (type);
6706 int ops_unsigned;
6707 tree signed_type, unsigned_type, intermediate_type;
6708 tree tem, one;
6710 /* First, see if we can fold the single bit test into a sign-bit
6711 test. */
6712 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6713 result_type);
6714 if (tem)
6715 return tem;
6717 /* Otherwise we have (A & C) != 0 where C is a single bit,
6718 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6719 Similarly for (A & C) == 0. */
6721 /* If INNER is a right shift of a constant and it plus BITNUM does
6722 not overflow, adjust BITNUM and INNER. */
6723 if (TREE_CODE (inner) == RSHIFT_EXPR
6724 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6725 && bitnum < TYPE_PRECISION (type)
6726 && wi::ltu_p (TREE_OPERAND (inner, 1),
6727 TYPE_PRECISION (type) - bitnum))
6729 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6730 inner = TREE_OPERAND (inner, 0);
6733 /* If we are going to be able to omit the AND below, we must do our
6734 operations as unsigned. If we must use the AND, we have a choice.
6735 Normally unsigned is faster, but for some machines signed is. */
6736 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6737 && !flag_syntax_only) ? 0 : 1;
6739 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6740 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6741 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6742 inner = fold_convert_loc (loc, intermediate_type, inner);
6744 if (bitnum != 0)
6745 inner = build2 (RSHIFT_EXPR, intermediate_type,
6746 inner, size_int (bitnum));
6748 one = build_int_cst (intermediate_type, 1);
6750 if (code == EQ_EXPR)
6751 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6753 /* Put the AND last so it can combine with more things. */
6754 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6756 /* Make sure to return the proper type. */
6757 inner = fold_convert_loc (loc, result_type, inner);
6759 return inner;
6761 return NULL_TREE;
6764 /* Check whether we are allowed to reorder operands arg0 and arg1,
6765 such that the evaluation of arg1 occurs before arg0. */
6767 static bool
6768 reorder_operands_p (const_tree arg0, const_tree arg1)
6770 if (! flag_evaluation_order)
6771 return true;
6772 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6773 return true;
6774 return ! TREE_SIDE_EFFECTS (arg0)
6775 && ! TREE_SIDE_EFFECTS (arg1);
6778 /* Test whether it is preferable two swap two operands, ARG0 and
6779 ARG1, for example because ARG0 is an integer constant and ARG1
6780 isn't. If REORDER is true, only recommend swapping if we can
6781 evaluate the operands in reverse order. */
6783 bool
6784 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6786 if (CONSTANT_CLASS_P (arg1))
6787 return 0;
6788 if (CONSTANT_CLASS_P (arg0))
6789 return 1;
6791 STRIP_NOPS (arg0);
6792 STRIP_NOPS (arg1);
6794 if (TREE_CONSTANT (arg1))
6795 return 0;
6796 if (TREE_CONSTANT (arg0))
6797 return 1;
6799 if (reorder && flag_evaluation_order
6800 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6801 return 0;
6803 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6804 for commutative and comparison operators. Ensuring a canonical
6805 form allows the optimizers to find additional redundancies without
6806 having to explicitly check for both orderings. */
6807 if (TREE_CODE (arg0) == SSA_NAME
6808 && TREE_CODE (arg1) == SSA_NAME
6809 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6810 return 1;
6812 /* Put SSA_NAMEs last. */
6813 if (TREE_CODE (arg1) == SSA_NAME)
6814 return 0;
6815 if (TREE_CODE (arg0) == SSA_NAME)
6816 return 1;
6818 /* Put variables last. */
6819 if (DECL_P (arg1))
6820 return 0;
6821 if (DECL_P (arg0))
6822 return 1;
6824 return 0;
6828 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6829 means A >= Y && A != MAX, but in this case we know that
6830 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6832 static tree
6833 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6835 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6837 if (TREE_CODE (bound) == LT_EXPR)
6838 a = TREE_OPERAND (bound, 0);
6839 else if (TREE_CODE (bound) == GT_EXPR)
6840 a = TREE_OPERAND (bound, 1);
6841 else
6842 return NULL_TREE;
6844 typea = TREE_TYPE (a);
6845 if (!INTEGRAL_TYPE_P (typea)
6846 && !POINTER_TYPE_P (typea))
6847 return NULL_TREE;
6849 if (TREE_CODE (ineq) == LT_EXPR)
6851 a1 = TREE_OPERAND (ineq, 1);
6852 y = TREE_OPERAND (ineq, 0);
6854 else if (TREE_CODE (ineq) == GT_EXPR)
6856 a1 = TREE_OPERAND (ineq, 0);
6857 y = TREE_OPERAND (ineq, 1);
6859 else
6860 return NULL_TREE;
6862 if (TREE_TYPE (a1) != typea)
6863 return NULL_TREE;
6865 if (POINTER_TYPE_P (typea))
6867 /* Convert the pointer types into integer before taking the difference. */
6868 tree ta = fold_convert_loc (loc, ssizetype, a);
6869 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6870 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6872 else
6873 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6875 if (!diff || !integer_onep (diff))
6876 return NULL_TREE;
6878 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6881 /* Fold a sum or difference of at least one multiplication.
6882 Returns the folded tree or NULL if no simplification could be made. */
6884 static tree
6885 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6886 tree arg0, tree arg1)
6888 tree arg00, arg01, arg10, arg11;
6889 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6891 /* (A * C) +- (B * C) -> (A+-B) * C.
6892 (A * C) +- A -> A * (C+-1).
6893 We are most concerned about the case where C is a constant,
6894 but other combinations show up during loop reduction. Since
6895 it is not difficult, try all four possibilities. */
6897 if (TREE_CODE (arg0) == MULT_EXPR)
6899 arg00 = TREE_OPERAND (arg0, 0);
6900 arg01 = TREE_OPERAND (arg0, 1);
6902 else if (TREE_CODE (arg0) == INTEGER_CST)
6904 arg00 = build_one_cst (type);
6905 arg01 = arg0;
6907 else
6909 /* We cannot generate constant 1 for fract. */
6910 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6911 return NULL_TREE;
6912 arg00 = arg0;
6913 arg01 = build_one_cst (type);
6915 if (TREE_CODE (arg1) == MULT_EXPR)
6917 arg10 = TREE_OPERAND (arg1, 0);
6918 arg11 = TREE_OPERAND (arg1, 1);
6920 else if (TREE_CODE (arg1) == INTEGER_CST)
6922 arg10 = build_one_cst (type);
6923 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6924 the purpose of this canonicalization. */
6925 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6926 && negate_expr_p (arg1)
6927 && code == PLUS_EXPR)
6929 arg11 = negate_expr (arg1);
6930 code = MINUS_EXPR;
6932 else
6933 arg11 = arg1;
6935 else
6937 /* We cannot generate constant 1 for fract. */
6938 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6939 return NULL_TREE;
6940 arg10 = arg1;
6941 arg11 = build_one_cst (type);
6943 same = NULL_TREE;
6945 if (operand_equal_p (arg01, arg11, 0))
6946 same = arg01, alt0 = arg00, alt1 = arg10;
6947 else if (operand_equal_p (arg00, arg10, 0))
6948 same = arg00, alt0 = arg01, alt1 = arg11;
6949 else if (operand_equal_p (arg00, arg11, 0))
6950 same = arg00, alt0 = arg01, alt1 = arg10;
6951 else if (operand_equal_p (arg01, arg10, 0))
6952 same = arg01, alt0 = arg00, alt1 = arg11;
6954 /* No identical multiplicands; see if we can find a common
6955 power-of-two factor in non-power-of-two multiplies. This
6956 can help in multi-dimensional array access. */
6957 else if (tree_fits_shwi_p (arg01)
6958 && tree_fits_shwi_p (arg11))
6960 HOST_WIDE_INT int01, int11, tmp;
6961 bool swap = false;
6962 tree maybe_same;
6963 int01 = tree_to_shwi (arg01);
6964 int11 = tree_to_shwi (arg11);
6966 /* Move min of absolute values to int11. */
6967 if (absu_hwi (int01) < absu_hwi (int11))
6969 tmp = int01, int01 = int11, int11 = tmp;
6970 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6971 maybe_same = arg01;
6972 swap = true;
6974 else
6975 maybe_same = arg11;
6977 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6978 /* The remainder should not be a constant, otherwise we
6979 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6980 increased the number of multiplications necessary. */
6981 && TREE_CODE (arg10) != INTEGER_CST)
6983 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6984 build_int_cst (TREE_TYPE (arg00),
6985 int01 / int11));
6986 alt1 = arg10;
6987 same = maybe_same;
6988 if (swap)
6989 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6993 if (same)
6994 return fold_build2_loc (loc, MULT_EXPR, type,
6995 fold_build2_loc (loc, code, type,
6996 fold_convert_loc (loc, type, alt0),
6997 fold_convert_loc (loc, type, alt1)),
6998 fold_convert_loc (loc, type, same));
7000 return NULL_TREE;
7003 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7004 specified by EXPR into the buffer PTR of length LEN bytes.
7005 Return the number of bytes placed in the buffer, or zero
7006 upon failure. */
7008 static int
7009 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7011 tree type = TREE_TYPE (expr);
7012 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7013 int byte, offset, word, words;
7014 unsigned char value;
7016 if ((off == -1 && total_bytes > len)
7017 || off >= total_bytes)
7018 return 0;
7019 if (off == -1)
7020 off = 0;
7021 words = total_bytes / UNITS_PER_WORD;
7023 for (byte = 0; byte < total_bytes; byte++)
7025 int bitpos = byte * BITS_PER_UNIT;
7026 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7027 number of bytes. */
7028 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7030 if (total_bytes > UNITS_PER_WORD)
7032 word = byte / UNITS_PER_WORD;
7033 if (WORDS_BIG_ENDIAN)
7034 word = (words - 1) - word;
7035 offset = word * UNITS_PER_WORD;
7036 if (BYTES_BIG_ENDIAN)
7037 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7038 else
7039 offset += byte % UNITS_PER_WORD;
7041 else
7042 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7043 if (offset >= off
7044 && offset - off < len)
7045 ptr[offset - off] = value;
7047 return MIN (len, total_bytes - off);
7051 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7052 specified by EXPR into the buffer PTR of length LEN bytes.
7053 Return the number of bytes placed in the buffer, or zero
7054 upon failure. */
7056 static int
7057 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7059 tree type = TREE_TYPE (expr);
7060 machine_mode mode = TYPE_MODE (type);
7061 int total_bytes = GET_MODE_SIZE (mode);
7062 FIXED_VALUE_TYPE value;
7063 tree i_value, i_type;
7065 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7066 return 0;
7068 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7070 if (NULL_TREE == i_type
7071 || TYPE_PRECISION (i_type) != total_bytes)
7072 return 0;
7074 value = TREE_FIXED_CST (expr);
7075 i_value = double_int_to_tree (i_type, value.data);
7077 return native_encode_int (i_value, ptr, len, off);
7081 /* Subroutine of native_encode_expr. Encode the REAL_CST
7082 specified by EXPR into the buffer PTR of length LEN bytes.
7083 Return the number of bytes placed in the buffer, or zero
7084 upon failure. */
7086 static int
7087 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7089 tree type = TREE_TYPE (expr);
7090 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7091 int byte, offset, word, words, bitpos;
7092 unsigned char value;
7094 /* There are always 32 bits in each long, no matter the size of
7095 the hosts long. We handle floating point representations with
7096 up to 192 bits. */
7097 long tmp[6];
7099 if ((off == -1 && total_bytes > len)
7100 || off >= total_bytes)
7101 return 0;
7102 if (off == -1)
7103 off = 0;
7104 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7106 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7108 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7109 bitpos += BITS_PER_UNIT)
7111 byte = (bitpos / BITS_PER_UNIT) & 3;
7112 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7114 if (UNITS_PER_WORD < 4)
7116 word = byte / UNITS_PER_WORD;
7117 if (WORDS_BIG_ENDIAN)
7118 word = (words - 1) - word;
7119 offset = word * UNITS_PER_WORD;
7120 if (BYTES_BIG_ENDIAN)
7121 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7122 else
7123 offset += byte % UNITS_PER_WORD;
7125 else
7127 offset = byte;
7128 if (BYTES_BIG_ENDIAN)
7130 /* Reverse bytes within each long, or within the entire float
7131 if it's smaller than a long (for HFmode). */
7132 offset = MIN (3, total_bytes - 1) - offset;
7133 gcc_assert (offset >= 0);
7136 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7137 if (offset >= off
7138 && offset - off < len)
7139 ptr[offset - off] = value;
7141 return MIN (len, total_bytes - off);
7144 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7145 specified by EXPR into the buffer PTR of length LEN bytes.
7146 Return the number of bytes placed in the buffer, or zero
7147 upon failure. */
7149 static int
7150 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7152 int rsize, isize;
7153 tree part;
7155 part = TREE_REALPART (expr);
7156 rsize = native_encode_expr (part, ptr, len, off);
7157 if (off == -1
7158 && rsize == 0)
7159 return 0;
7160 part = TREE_IMAGPART (expr);
7161 if (off != -1)
7162 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7163 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7164 if (off == -1
7165 && isize != rsize)
7166 return 0;
7167 return rsize + isize;
7171 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7172 specified by EXPR into the buffer PTR of length LEN bytes.
7173 Return the number of bytes placed in the buffer, or zero
7174 upon failure. */
7176 static int
7177 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7179 unsigned i, count;
7180 int size, offset;
7181 tree itype, elem;
7183 offset = 0;
7184 count = VECTOR_CST_NELTS (expr);
7185 itype = TREE_TYPE (TREE_TYPE (expr));
7186 size = GET_MODE_SIZE (TYPE_MODE (itype));
7187 for (i = 0; i < count; i++)
7189 if (off >= size)
7191 off -= size;
7192 continue;
7194 elem = VECTOR_CST_ELT (expr, i);
7195 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7196 if ((off == -1 && res != size)
7197 || res == 0)
7198 return 0;
7199 offset += res;
7200 if (offset >= len)
7201 return offset;
7202 if (off != -1)
7203 off = 0;
7205 return offset;
7209 /* Subroutine of native_encode_expr. Encode the STRING_CST
7210 specified by EXPR into the buffer PTR of length LEN bytes.
7211 Return the number of bytes placed in the buffer, or zero
7212 upon failure. */
7214 static int
7215 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7217 tree type = TREE_TYPE (expr);
7218 HOST_WIDE_INT total_bytes;
7220 if (TREE_CODE (type) != ARRAY_TYPE
7221 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7222 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7223 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7224 return 0;
7225 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7226 if ((off == -1 && total_bytes > len)
7227 || off >= total_bytes)
7228 return 0;
7229 if (off == -1)
7230 off = 0;
7231 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7233 int written = 0;
7234 if (off < TREE_STRING_LENGTH (expr))
7236 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7237 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7239 memset (ptr + written, 0,
7240 MIN (total_bytes - written, len - written));
7242 else
7243 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7244 return MIN (total_bytes - off, len);
7248 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7249 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7250 buffer PTR of length LEN bytes. If OFF is not -1 then start
7251 the encoding at byte offset OFF and encode at most LEN bytes.
7252 Return the number of bytes placed in the buffer, or zero upon failure. */
7255 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7257 /* We don't support starting at negative offset and -1 is special. */
7258 if (off < -1)
7259 return 0;
7261 switch (TREE_CODE (expr))
7263 case INTEGER_CST:
7264 return native_encode_int (expr, ptr, len, off);
7266 case REAL_CST:
7267 return native_encode_real (expr, ptr, len, off);
7269 case FIXED_CST:
7270 return native_encode_fixed (expr, ptr, len, off);
7272 case COMPLEX_CST:
7273 return native_encode_complex (expr, ptr, len, off);
7275 case VECTOR_CST:
7276 return native_encode_vector (expr, ptr, len, off);
7278 case STRING_CST:
7279 return native_encode_string (expr, ptr, len, off);
7281 default:
7282 return 0;
7287 /* Subroutine of native_interpret_expr. Interpret the contents of
7288 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7289 If the buffer cannot be interpreted, return NULL_TREE. */
7291 static tree
7292 native_interpret_int (tree type, const unsigned char *ptr, int len)
7294 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7296 if (total_bytes > len
7297 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7298 return NULL_TREE;
7300 wide_int result = wi::from_buffer (ptr, total_bytes);
7302 return wide_int_to_tree (type, result);
7306 /* Subroutine of native_interpret_expr. Interpret the contents of
7307 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7308 If the buffer cannot be interpreted, return NULL_TREE. */
7310 static tree
7311 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7313 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7314 double_int result;
7315 FIXED_VALUE_TYPE fixed_value;
7317 if (total_bytes > len
7318 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7319 return NULL_TREE;
7321 result = double_int::from_buffer (ptr, total_bytes);
7322 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7324 return build_fixed (type, fixed_value);
7328 /* Subroutine of native_interpret_expr. Interpret the contents of
7329 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7330 If the buffer cannot be interpreted, return NULL_TREE. */
7332 static tree
7333 native_interpret_real (tree type, const unsigned char *ptr, int len)
7335 machine_mode mode = TYPE_MODE (type);
7336 int total_bytes = GET_MODE_SIZE (mode);
7337 unsigned char value;
7338 /* There are always 32 bits in each long, no matter the size of
7339 the hosts long. We handle floating point representations with
7340 up to 192 bits. */
7341 REAL_VALUE_TYPE r;
7342 long tmp[6];
7344 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7345 if (total_bytes > len || total_bytes > 24)
7346 return NULL_TREE;
7347 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7349 memset (tmp, 0, sizeof (tmp));
7350 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7351 bitpos += BITS_PER_UNIT)
7353 /* Both OFFSET and BYTE index within a long;
7354 bitpos indexes the whole float. */
7355 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7356 if (UNITS_PER_WORD < 4)
7358 int word = byte / UNITS_PER_WORD;
7359 if (WORDS_BIG_ENDIAN)
7360 word = (words - 1) - word;
7361 offset = word * UNITS_PER_WORD;
7362 if (BYTES_BIG_ENDIAN)
7363 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7364 else
7365 offset += byte % UNITS_PER_WORD;
7367 else
7369 offset = byte;
7370 if (BYTES_BIG_ENDIAN)
7372 /* Reverse bytes within each long, or within the entire float
7373 if it's smaller than a long (for HFmode). */
7374 offset = MIN (3, total_bytes - 1) - offset;
7375 gcc_assert (offset >= 0);
7378 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7380 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7383 real_from_target (&r, tmp, mode);
7384 return build_real (type, r);
7388 /* Subroutine of native_interpret_expr. Interpret the contents of
7389 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7390 If the buffer cannot be interpreted, return NULL_TREE. */
7392 static tree
7393 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7395 tree etype, rpart, ipart;
7396 int size;
7398 etype = TREE_TYPE (type);
7399 size = GET_MODE_SIZE (TYPE_MODE (etype));
7400 if (size * 2 > len)
7401 return NULL_TREE;
7402 rpart = native_interpret_expr (etype, ptr, size);
7403 if (!rpart)
7404 return NULL_TREE;
7405 ipart = native_interpret_expr (etype, ptr+size, size);
7406 if (!ipart)
7407 return NULL_TREE;
7408 return build_complex (type, rpart, ipart);
7412 /* Subroutine of native_interpret_expr. Interpret the contents of
7413 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7414 If the buffer cannot be interpreted, return NULL_TREE. */
7416 static tree
7417 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7419 tree etype, elem;
7420 int i, size, count;
7421 tree *elements;
7423 etype = TREE_TYPE (type);
7424 size = GET_MODE_SIZE (TYPE_MODE (etype));
7425 count = TYPE_VECTOR_SUBPARTS (type);
7426 if (size * count > len)
7427 return NULL_TREE;
7429 elements = XALLOCAVEC (tree, count);
7430 for (i = count - 1; i >= 0; i--)
7432 elem = native_interpret_expr (etype, ptr+(i*size), size);
7433 if (!elem)
7434 return NULL_TREE;
7435 elements[i] = elem;
7437 return build_vector (type, elements);
7441 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7442 the buffer PTR of length LEN as a constant of type TYPE. For
7443 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7444 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7445 return NULL_TREE. */
7447 tree
7448 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7450 switch (TREE_CODE (type))
7452 case INTEGER_TYPE:
7453 case ENUMERAL_TYPE:
7454 case BOOLEAN_TYPE:
7455 case POINTER_TYPE:
7456 case REFERENCE_TYPE:
7457 return native_interpret_int (type, ptr, len);
7459 case REAL_TYPE:
7460 return native_interpret_real (type, ptr, len);
7462 case FIXED_POINT_TYPE:
7463 return native_interpret_fixed (type, ptr, len);
7465 case COMPLEX_TYPE:
7466 return native_interpret_complex (type, ptr, len);
7468 case VECTOR_TYPE:
7469 return native_interpret_vector (type, ptr, len);
7471 default:
7472 return NULL_TREE;
7476 /* Returns true if we can interpret the contents of a native encoding
7477 as TYPE. */
7479 static bool
7480 can_native_interpret_type_p (tree type)
7482 switch (TREE_CODE (type))
7484 case INTEGER_TYPE:
7485 case ENUMERAL_TYPE:
7486 case BOOLEAN_TYPE:
7487 case POINTER_TYPE:
7488 case REFERENCE_TYPE:
7489 case FIXED_POINT_TYPE:
7490 case REAL_TYPE:
7491 case COMPLEX_TYPE:
7492 case VECTOR_TYPE:
7493 return true;
7494 default:
7495 return false;
7499 /* Return true iff a constant of type TYPE is accepted by
7500 native_encode_expr. */
7502 bool
7503 can_native_encode_type_p (tree type)
7505 switch (TREE_CODE (type))
7507 case INTEGER_TYPE:
7508 case REAL_TYPE:
7509 case FIXED_POINT_TYPE:
7510 case COMPLEX_TYPE:
7511 case VECTOR_TYPE:
7512 case POINTER_TYPE:
7513 return true;
7514 default:
7515 return false;
7519 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7520 TYPE at compile-time. If we're unable to perform the conversion
7521 return NULL_TREE. */
7523 static tree
7524 fold_view_convert_expr (tree type, tree expr)
7526 /* We support up to 512-bit values (for V8DFmode). */
7527 unsigned char buffer[64];
7528 int len;
7530 /* Check that the host and target are sane. */
7531 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7532 return NULL_TREE;
7534 len = native_encode_expr (expr, buffer, sizeof (buffer));
7535 if (len == 0)
7536 return NULL_TREE;
7538 return native_interpret_expr (type, buffer, len);
7541 /* Build an expression for the address of T. Folds away INDIRECT_REF
7542 to avoid confusing the gimplify process. */
7544 tree
7545 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7547 /* The size of the object is not relevant when talking about its address. */
7548 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7549 t = TREE_OPERAND (t, 0);
7551 if (TREE_CODE (t) == INDIRECT_REF)
7553 t = TREE_OPERAND (t, 0);
7555 if (TREE_TYPE (t) != ptrtype)
7556 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7558 else if (TREE_CODE (t) == MEM_REF
7559 && integer_zerop (TREE_OPERAND (t, 1)))
7560 return TREE_OPERAND (t, 0);
7561 else if (TREE_CODE (t) == MEM_REF
7562 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7563 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7564 TREE_OPERAND (t, 0),
7565 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7566 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7568 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7570 if (TREE_TYPE (t) != ptrtype)
7571 t = fold_convert_loc (loc, ptrtype, t);
7573 else
7574 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7576 return t;
7579 /* Build an expression for the address of T. */
7581 tree
7582 build_fold_addr_expr_loc (location_t loc, tree t)
7584 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7586 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7589 /* Fold a unary expression of code CODE and type TYPE with operand
7590 OP0. Return the folded expression if folding is successful.
7591 Otherwise, return NULL_TREE. */
7593 tree
7594 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7596 tree tem;
7597 tree arg0;
7598 enum tree_code_class kind = TREE_CODE_CLASS (code);
7600 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7601 && TREE_CODE_LENGTH (code) == 1);
7603 arg0 = op0;
7604 if (arg0)
7606 if (CONVERT_EXPR_CODE_P (code)
7607 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7609 /* Don't use STRIP_NOPS, because signedness of argument type
7610 matters. */
7611 STRIP_SIGN_NOPS (arg0);
7613 else
7615 /* Strip any conversions that don't change the mode. This
7616 is safe for every expression, except for a comparison
7617 expression because its signedness is derived from its
7618 operands.
7620 Note that this is done as an internal manipulation within
7621 the constant folder, in order to find the simplest
7622 representation of the arguments so that their form can be
7623 studied. In any cases, the appropriate type conversions
7624 should be put back in the tree that will get out of the
7625 constant folder. */
7626 STRIP_NOPS (arg0);
7629 if (CONSTANT_CLASS_P (arg0))
7631 tree tem = const_unop (code, type, arg0);
7632 if (tem)
7634 if (TREE_TYPE (tem) != type)
7635 tem = fold_convert_loc (loc, type, tem);
7636 return tem;
7641 tem = generic_simplify (loc, code, type, op0);
7642 if (tem)
7643 return tem;
7645 if (TREE_CODE_CLASS (code) == tcc_unary)
7647 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7648 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7649 fold_build1_loc (loc, code, type,
7650 fold_convert_loc (loc, TREE_TYPE (op0),
7651 TREE_OPERAND (arg0, 1))));
7652 else if (TREE_CODE (arg0) == COND_EXPR)
7654 tree arg01 = TREE_OPERAND (arg0, 1);
7655 tree arg02 = TREE_OPERAND (arg0, 2);
7656 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7657 arg01 = fold_build1_loc (loc, code, type,
7658 fold_convert_loc (loc,
7659 TREE_TYPE (op0), arg01));
7660 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7661 arg02 = fold_build1_loc (loc, code, type,
7662 fold_convert_loc (loc,
7663 TREE_TYPE (op0), arg02));
7664 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7665 arg01, arg02);
7667 /* If this was a conversion, and all we did was to move into
7668 inside the COND_EXPR, bring it back out. But leave it if
7669 it is a conversion from integer to integer and the
7670 result precision is no wider than a word since such a
7671 conversion is cheap and may be optimized away by combine,
7672 while it couldn't if it were outside the COND_EXPR. Then return
7673 so we don't get into an infinite recursion loop taking the
7674 conversion out and then back in. */
7676 if ((CONVERT_EXPR_CODE_P (code)
7677 || code == NON_LVALUE_EXPR)
7678 && TREE_CODE (tem) == COND_EXPR
7679 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7680 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7681 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7682 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7683 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7684 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7685 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7686 && (INTEGRAL_TYPE_P
7687 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7688 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7689 || flag_syntax_only))
7690 tem = build1_loc (loc, code, type,
7691 build3 (COND_EXPR,
7692 TREE_TYPE (TREE_OPERAND
7693 (TREE_OPERAND (tem, 1), 0)),
7694 TREE_OPERAND (tem, 0),
7695 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7696 TREE_OPERAND (TREE_OPERAND (tem, 2),
7697 0)));
7698 return tem;
7702 switch (code)
7704 case NON_LVALUE_EXPR:
7705 if (!maybe_lvalue_p (op0))
7706 return fold_convert_loc (loc, type, op0);
7707 return NULL_TREE;
7709 CASE_CONVERT:
7710 case FLOAT_EXPR:
7711 case FIX_TRUNC_EXPR:
7712 if (COMPARISON_CLASS_P (op0))
7714 /* If we have (type) (a CMP b) and type is an integral type, return
7715 new expression involving the new type. Canonicalize
7716 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7717 non-integral type.
7718 Do not fold the result as that would not simplify further, also
7719 folding again results in recursions. */
7720 if (TREE_CODE (type) == BOOLEAN_TYPE)
7721 return build2_loc (loc, TREE_CODE (op0), type,
7722 TREE_OPERAND (op0, 0),
7723 TREE_OPERAND (op0, 1));
7724 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7725 && TREE_CODE (type) != VECTOR_TYPE)
7726 return build3_loc (loc, COND_EXPR, type, op0,
7727 constant_boolean_node (true, type),
7728 constant_boolean_node (false, type));
7731 /* Handle (T *)&A.B.C for A being of type T and B and C
7732 living at offset zero. This occurs frequently in
7733 C++ upcasting and then accessing the base. */
7734 if (TREE_CODE (op0) == ADDR_EXPR
7735 && POINTER_TYPE_P (type)
7736 && handled_component_p (TREE_OPERAND (op0, 0)))
7738 HOST_WIDE_INT bitsize, bitpos;
7739 tree offset;
7740 machine_mode mode;
7741 int unsignedp, reversep, volatilep;
7742 tree base
7743 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7744 &offset, &mode, &unsignedp, &reversep,
7745 &volatilep);
7746 /* If the reference was to a (constant) zero offset, we can use
7747 the address of the base if it has the same base type
7748 as the result type and the pointer type is unqualified. */
7749 if (! offset && bitpos == 0
7750 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7751 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7752 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7753 return fold_convert_loc (loc, type,
7754 build_fold_addr_expr_loc (loc, base));
7757 if (TREE_CODE (op0) == MODIFY_EXPR
7758 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7759 /* Detect assigning a bitfield. */
7760 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7761 && DECL_BIT_FIELD
7762 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7764 /* Don't leave an assignment inside a conversion
7765 unless assigning a bitfield. */
7766 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7767 /* First do the assignment, then return converted constant. */
7768 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7769 TREE_NO_WARNING (tem) = 1;
7770 TREE_USED (tem) = 1;
7771 return tem;
7774 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7775 constants (if x has signed type, the sign bit cannot be set
7776 in c). This folds extension into the BIT_AND_EXPR.
7777 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7778 very likely don't have maximal range for their precision and this
7779 transformation effectively doesn't preserve non-maximal ranges. */
7780 if (TREE_CODE (type) == INTEGER_TYPE
7781 && TREE_CODE (op0) == BIT_AND_EXPR
7782 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7784 tree and_expr = op0;
7785 tree and0 = TREE_OPERAND (and_expr, 0);
7786 tree and1 = TREE_OPERAND (and_expr, 1);
7787 int change = 0;
7789 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7790 || (TYPE_PRECISION (type)
7791 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7792 change = 1;
7793 else if (TYPE_PRECISION (TREE_TYPE (and1))
7794 <= HOST_BITS_PER_WIDE_INT
7795 && tree_fits_uhwi_p (and1))
7797 unsigned HOST_WIDE_INT cst;
7799 cst = tree_to_uhwi (and1);
7800 cst &= HOST_WIDE_INT_M1U
7801 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7802 change = (cst == 0);
7803 if (change
7804 && !flag_syntax_only
7805 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7806 == ZERO_EXTEND))
7808 tree uns = unsigned_type_for (TREE_TYPE (and0));
7809 and0 = fold_convert_loc (loc, uns, and0);
7810 and1 = fold_convert_loc (loc, uns, and1);
7813 if (change)
7815 tem = force_fit_type (type, wi::to_widest (and1), 0,
7816 TREE_OVERFLOW (and1));
7817 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7818 fold_convert_loc (loc, type, and0), tem);
7822 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7823 cast (T1)X will fold away. We assume that this happens when X itself
7824 is a cast. */
7825 if (POINTER_TYPE_P (type)
7826 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7827 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7829 tree arg00 = TREE_OPERAND (arg0, 0);
7830 tree arg01 = TREE_OPERAND (arg0, 1);
7832 return fold_build_pointer_plus_loc
7833 (loc, fold_convert_loc (loc, type, arg00), arg01);
7836 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7837 of the same precision, and X is an integer type not narrower than
7838 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7839 if (INTEGRAL_TYPE_P (type)
7840 && TREE_CODE (op0) == BIT_NOT_EXPR
7841 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7842 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7843 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7845 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7846 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7847 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7848 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7849 fold_convert_loc (loc, type, tem));
7852 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7853 type of X and Y (integer types only). */
7854 if (INTEGRAL_TYPE_P (type)
7855 && TREE_CODE (op0) == MULT_EXPR
7856 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7857 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7859 /* Be careful not to introduce new overflows. */
7860 tree mult_type;
7861 if (TYPE_OVERFLOW_WRAPS (type))
7862 mult_type = type;
7863 else
7864 mult_type = unsigned_type_for (type);
7866 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7868 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7869 fold_convert_loc (loc, mult_type,
7870 TREE_OPERAND (op0, 0)),
7871 fold_convert_loc (loc, mult_type,
7872 TREE_OPERAND (op0, 1)));
7873 return fold_convert_loc (loc, type, tem);
7877 return NULL_TREE;
7879 case VIEW_CONVERT_EXPR:
7880 if (TREE_CODE (op0) == MEM_REF)
7882 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7883 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7884 tem = fold_build2_loc (loc, MEM_REF, type,
7885 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7886 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7887 return tem;
7890 return NULL_TREE;
7892 case NEGATE_EXPR:
7893 tem = fold_negate_expr (loc, arg0);
7894 if (tem)
7895 return fold_convert_loc (loc, type, tem);
7896 return NULL_TREE;
7898 case ABS_EXPR:
7899 /* Convert fabs((double)float) into (double)fabsf(float). */
7900 if (TREE_CODE (arg0) == NOP_EXPR
7901 && TREE_CODE (type) == REAL_TYPE)
7903 tree targ0 = strip_float_extensions (arg0);
7904 if (targ0 != arg0)
7905 return fold_convert_loc (loc, type,
7906 fold_build1_loc (loc, ABS_EXPR,
7907 TREE_TYPE (targ0),
7908 targ0));
7910 return NULL_TREE;
7912 case BIT_NOT_EXPR:
7913 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7914 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7915 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7916 fold_convert_loc (loc, type,
7917 TREE_OPERAND (arg0, 0)))))
7918 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7919 fold_convert_loc (loc, type,
7920 TREE_OPERAND (arg0, 1)));
7921 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7922 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7923 fold_convert_loc (loc, type,
7924 TREE_OPERAND (arg0, 1)))))
7925 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7926 fold_convert_loc (loc, type,
7927 TREE_OPERAND (arg0, 0)), tem);
7929 return NULL_TREE;
7931 case TRUTH_NOT_EXPR:
7932 /* Note that the operand of this must be an int
7933 and its values must be 0 or 1.
7934 ("true" is a fixed value perhaps depending on the language,
7935 but we don't handle values other than 1 correctly yet.) */
7936 tem = fold_truth_not_expr (loc, arg0);
7937 if (!tem)
7938 return NULL_TREE;
7939 return fold_convert_loc (loc, type, tem);
7941 case INDIRECT_REF:
7942 /* Fold *&X to X if X is an lvalue. */
7943 if (TREE_CODE (op0) == ADDR_EXPR)
7945 tree op00 = TREE_OPERAND (op0, 0);
7946 if ((VAR_P (op00)
7947 || TREE_CODE (op00) == PARM_DECL
7948 || TREE_CODE (op00) == RESULT_DECL)
7949 && !TREE_READONLY (op00))
7950 return op00;
7952 return NULL_TREE;
7954 default:
7955 return NULL_TREE;
7956 } /* switch (code) */
7960 /* If the operation was a conversion do _not_ mark a resulting constant
7961 with TREE_OVERFLOW if the original constant was not. These conversions
7962 have implementation defined behavior and retaining the TREE_OVERFLOW
7963 flag here would confuse later passes such as VRP. */
7964 tree
7965 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7966 tree type, tree op0)
7968 tree res = fold_unary_loc (loc, code, type, op0);
7969 if (res
7970 && TREE_CODE (res) == INTEGER_CST
7971 && TREE_CODE (op0) == INTEGER_CST
7972 && CONVERT_EXPR_CODE_P (code))
7973 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7975 return res;
7978 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7979 operands OP0 and OP1. LOC is the location of the resulting expression.
7980 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7981 Return the folded expression if folding is successful. Otherwise,
7982 return NULL_TREE. */
7983 static tree
7984 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7985 tree arg0, tree arg1, tree op0, tree op1)
7987 tree tem;
7989 /* We only do these simplifications if we are optimizing. */
7990 if (!optimize)
7991 return NULL_TREE;
7993 /* Check for things like (A || B) && (A || C). We can convert this
7994 to A || (B && C). Note that either operator can be any of the four
7995 truth and/or operations and the transformation will still be
7996 valid. Also note that we only care about order for the
7997 ANDIF and ORIF operators. If B contains side effects, this
7998 might change the truth-value of A. */
7999 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8000 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8001 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8002 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8003 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8004 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8006 tree a00 = TREE_OPERAND (arg0, 0);
8007 tree a01 = TREE_OPERAND (arg0, 1);
8008 tree a10 = TREE_OPERAND (arg1, 0);
8009 tree a11 = TREE_OPERAND (arg1, 1);
8010 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8011 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8012 && (code == TRUTH_AND_EXPR
8013 || code == TRUTH_OR_EXPR));
8015 if (operand_equal_p (a00, a10, 0))
8016 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8017 fold_build2_loc (loc, code, type, a01, a11));
8018 else if (commutative && operand_equal_p (a00, a11, 0))
8019 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8020 fold_build2_loc (loc, code, type, a01, a10));
8021 else if (commutative && operand_equal_p (a01, a10, 0))
8022 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8023 fold_build2_loc (loc, code, type, a00, a11));
8025 /* This case if tricky because we must either have commutative
8026 operators or else A10 must not have side-effects. */
8028 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8029 && operand_equal_p (a01, a11, 0))
8030 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8031 fold_build2_loc (loc, code, type, a00, a10),
8032 a01);
8035 /* See if we can build a range comparison. */
8036 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8037 return tem;
8039 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8040 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8042 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8043 if (tem)
8044 return fold_build2_loc (loc, code, type, tem, arg1);
8047 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8048 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8050 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8051 if (tem)
8052 return fold_build2_loc (loc, code, type, arg0, tem);
8055 /* Check for the possibility of merging component references. If our
8056 lhs is another similar operation, try to merge its rhs with our
8057 rhs. Then try to merge our lhs and rhs. */
8058 if (TREE_CODE (arg0) == code
8059 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8060 TREE_OPERAND (arg0, 1), arg1)))
8061 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8063 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8064 return tem;
8066 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8067 && (code == TRUTH_AND_EXPR
8068 || code == TRUTH_ANDIF_EXPR
8069 || code == TRUTH_OR_EXPR
8070 || code == TRUTH_ORIF_EXPR))
8072 enum tree_code ncode, icode;
8074 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8075 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8076 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8078 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8079 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8080 We don't want to pack more than two leafs to a non-IF AND/OR
8081 expression.
8082 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8083 equal to IF-CODE, then we don't want to add right-hand operand.
8084 If the inner right-hand side of left-hand operand has
8085 side-effects, or isn't simple, then we can't add to it,
8086 as otherwise we might destroy if-sequence. */
8087 if (TREE_CODE (arg0) == icode
8088 && simple_operand_p_2 (arg1)
8089 /* Needed for sequence points to handle trappings, and
8090 side-effects. */
8091 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8093 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8094 arg1);
8095 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8096 tem);
8098 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8099 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8100 else if (TREE_CODE (arg1) == icode
8101 && simple_operand_p_2 (arg0)
8102 /* Needed for sequence points to handle trappings, and
8103 side-effects. */
8104 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8106 tem = fold_build2_loc (loc, ncode, type,
8107 arg0, TREE_OPERAND (arg1, 0));
8108 return fold_build2_loc (loc, icode, type, tem,
8109 TREE_OPERAND (arg1, 1));
8111 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8112 into (A OR B).
8113 For sequence point consistancy, we need to check for trapping,
8114 and side-effects. */
8115 else if (code == icode && simple_operand_p_2 (arg0)
8116 && simple_operand_p_2 (arg1))
8117 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8120 return NULL_TREE;
8123 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8124 by changing CODE to reduce the magnitude of constants involved in
8125 ARG0 of the comparison.
8126 Returns a canonicalized comparison tree if a simplification was
8127 possible, otherwise returns NULL_TREE.
8128 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8129 valid if signed overflow is undefined. */
8131 static tree
8132 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8133 tree arg0, tree arg1,
8134 bool *strict_overflow_p)
8136 enum tree_code code0 = TREE_CODE (arg0);
8137 tree t, cst0 = NULL_TREE;
8138 int sgn0;
8140 /* Match A +- CST code arg1. We can change this only if overflow
8141 is undefined. */
8142 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8143 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8144 /* In principle pointers also have undefined overflow behavior,
8145 but that causes problems elsewhere. */
8146 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8147 && (code0 == MINUS_EXPR
8148 || code0 == PLUS_EXPR)
8149 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8150 return NULL_TREE;
8152 /* Identify the constant in arg0 and its sign. */
8153 cst0 = TREE_OPERAND (arg0, 1);
8154 sgn0 = tree_int_cst_sgn (cst0);
8156 /* Overflowed constants and zero will cause problems. */
8157 if (integer_zerop (cst0)
8158 || TREE_OVERFLOW (cst0))
8159 return NULL_TREE;
8161 /* See if we can reduce the magnitude of the constant in
8162 arg0 by changing the comparison code. */
8163 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8164 if (code == LT_EXPR
8165 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8166 code = LE_EXPR;
8167 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8168 else if (code == GT_EXPR
8169 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8170 code = GE_EXPR;
8171 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8172 else if (code == LE_EXPR
8173 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8174 code = LT_EXPR;
8175 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8176 else if (code == GE_EXPR
8177 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8178 code = GT_EXPR;
8179 else
8180 return NULL_TREE;
8181 *strict_overflow_p = true;
8183 /* Now build the constant reduced in magnitude. But not if that
8184 would produce one outside of its types range. */
8185 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8186 && ((sgn0 == 1
8187 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8188 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8189 || (sgn0 == -1
8190 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8191 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8192 return NULL_TREE;
8194 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8195 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8196 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8197 t = fold_convert (TREE_TYPE (arg1), t);
8199 return fold_build2_loc (loc, code, type, t, arg1);
8202 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8203 overflow further. Try to decrease the magnitude of constants involved
8204 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8205 and put sole constants at the second argument position.
8206 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8208 static tree
8209 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8210 tree arg0, tree arg1)
8212 tree t;
8213 bool strict_overflow_p;
8214 const char * const warnmsg = G_("assuming signed overflow does not occur "
8215 "when reducing constant in comparison");
8217 /* Try canonicalization by simplifying arg0. */
8218 strict_overflow_p = false;
8219 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8220 &strict_overflow_p);
8221 if (t)
8223 if (strict_overflow_p)
8224 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8225 return t;
8228 /* Try canonicalization by simplifying arg1 using the swapped
8229 comparison. */
8230 code = swap_tree_comparison (code);
8231 strict_overflow_p = false;
8232 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8233 &strict_overflow_p);
8234 if (t && strict_overflow_p)
8235 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8236 return t;
8239 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8240 space. This is used to avoid issuing overflow warnings for
8241 expressions like &p->x which can not wrap. */
8243 static bool
8244 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8246 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8247 return true;
8249 if (bitpos < 0)
8250 return true;
8252 wide_int wi_offset;
8253 int precision = TYPE_PRECISION (TREE_TYPE (base));
8254 if (offset == NULL_TREE)
8255 wi_offset = wi::zero (precision);
8256 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8257 return true;
8258 else
8259 wi_offset = offset;
8261 bool overflow;
8262 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8263 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8264 if (overflow)
8265 return true;
8267 if (!wi::fits_uhwi_p (total))
8268 return true;
8270 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8271 if (size <= 0)
8272 return true;
8274 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8275 array. */
8276 if (TREE_CODE (base) == ADDR_EXPR)
8278 HOST_WIDE_INT base_size;
8280 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8281 if (base_size > 0 && size < base_size)
8282 size = base_size;
8285 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8288 /* Return a positive integer when the symbol DECL is known to have
8289 a nonzero address, zero when it's known not to (e.g., it's a weak
8290 symbol), and a negative integer when the symbol is not yet in the
8291 symbol table and so whether or not its address is zero is unknown. */
8292 static int
8293 maybe_nonzero_address (tree decl)
8295 if (DECL_P (decl) && decl_in_symtab_p (decl))
8296 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8297 return symbol->nonzero_address ();
8299 return -1;
8302 /* Subroutine of fold_binary. This routine performs all of the
8303 transformations that are common to the equality/inequality
8304 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8305 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8306 fold_binary should call fold_binary. Fold a comparison with
8307 tree code CODE and type TYPE with operands OP0 and OP1. Return
8308 the folded comparison or NULL_TREE. */
8310 static tree
8311 fold_comparison (location_t loc, enum tree_code code, tree type,
8312 tree op0, tree op1)
8314 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8315 tree arg0, arg1, tem;
8317 arg0 = op0;
8318 arg1 = op1;
8320 STRIP_SIGN_NOPS (arg0);
8321 STRIP_SIGN_NOPS (arg1);
8323 /* For comparisons of pointers we can decompose it to a compile time
8324 comparison of the base objects and the offsets into the object.
8325 This requires at least one operand being an ADDR_EXPR or a
8326 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8327 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8328 && (TREE_CODE (arg0) == ADDR_EXPR
8329 || TREE_CODE (arg1) == ADDR_EXPR
8330 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8331 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8333 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8334 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8335 machine_mode mode;
8336 int volatilep, reversep, unsignedp;
8337 bool indirect_base0 = false, indirect_base1 = false;
8339 /* Get base and offset for the access. Strip ADDR_EXPR for
8340 get_inner_reference, but put it back by stripping INDIRECT_REF
8341 off the base object if possible. indirect_baseN will be true
8342 if baseN is not an address but refers to the object itself. */
8343 base0 = arg0;
8344 if (TREE_CODE (arg0) == ADDR_EXPR)
8346 base0
8347 = get_inner_reference (TREE_OPERAND (arg0, 0),
8348 &bitsize, &bitpos0, &offset0, &mode,
8349 &unsignedp, &reversep, &volatilep);
8350 if (TREE_CODE (base0) == INDIRECT_REF)
8351 base0 = TREE_OPERAND (base0, 0);
8352 else
8353 indirect_base0 = true;
8355 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8357 base0 = TREE_OPERAND (arg0, 0);
8358 STRIP_SIGN_NOPS (base0);
8359 if (TREE_CODE (base0) == ADDR_EXPR)
8361 base0
8362 = get_inner_reference (TREE_OPERAND (base0, 0),
8363 &bitsize, &bitpos0, &offset0, &mode,
8364 &unsignedp, &reversep, &volatilep);
8365 if (TREE_CODE (base0) == INDIRECT_REF)
8366 base0 = TREE_OPERAND (base0, 0);
8367 else
8368 indirect_base0 = true;
8370 if (offset0 == NULL_TREE || integer_zerop (offset0))
8371 offset0 = TREE_OPERAND (arg0, 1);
8372 else
8373 offset0 = size_binop (PLUS_EXPR, offset0,
8374 TREE_OPERAND (arg0, 1));
8375 if (TREE_CODE (offset0) == INTEGER_CST)
8377 offset_int tem = wi::sext (wi::to_offset (offset0),
8378 TYPE_PRECISION (sizetype));
8379 tem <<= LOG2_BITS_PER_UNIT;
8380 tem += bitpos0;
8381 if (wi::fits_shwi_p (tem))
8383 bitpos0 = tem.to_shwi ();
8384 offset0 = NULL_TREE;
8389 base1 = arg1;
8390 if (TREE_CODE (arg1) == ADDR_EXPR)
8392 base1
8393 = get_inner_reference (TREE_OPERAND (arg1, 0),
8394 &bitsize, &bitpos1, &offset1, &mode,
8395 &unsignedp, &reversep, &volatilep);
8396 if (TREE_CODE (base1) == INDIRECT_REF)
8397 base1 = TREE_OPERAND (base1, 0);
8398 else
8399 indirect_base1 = true;
8401 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8403 base1 = TREE_OPERAND (arg1, 0);
8404 STRIP_SIGN_NOPS (base1);
8405 if (TREE_CODE (base1) == ADDR_EXPR)
8407 base1
8408 = get_inner_reference (TREE_OPERAND (base1, 0),
8409 &bitsize, &bitpos1, &offset1, &mode,
8410 &unsignedp, &reversep, &volatilep);
8411 if (TREE_CODE (base1) == INDIRECT_REF)
8412 base1 = TREE_OPERAND (base1, 0);
8413 else
8414 indirect_base1 = true;
8416 if (offset1 == NULL_TREE || integer_zerop (offset1))
8417 offset1 = TREE_OPERAND (arg1, 1);
8418 else
8419 offset1 = size_binop (PLUS_EXPR, offset1,
8420 TREE_OPERAND (arg1, 1));
8421 if (TREE_CODE (offset1) == INTEGER_CST)
8423 offset_int tem = wi::sext (wi::to_offset (offset1),
8424 TYPE_PRECISION (sizetype));
8425 tem <<= LOG2_BITS_PER_UNIT;
8426 tem += bitpos1;
8427 if (wi::fits_shwi_p (tem))
8429 bitpos1 = tem.to_shwi ();
8430 offset1 = NULL_TREE;
8435 /* If we have equivalent bases we might be able to simplify. */
8436 if (indirect_base0 == indirect_base1
8437 && operand_equal_p (base0, base1,
8438 indirect_base0 ? OEP_ADDRESS_OF : 0))
8440 /* We can fold this expression to a constant if the non-constant
8441 offset parts are equal. */
8442 if ((offset0 == offset1
8443 || (offset0 && offset1
8444 && operand_equal_p (offset0, offset1, 0)))
8445 && (equality_code
8446 || (indirect_base0
8447 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8448 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8451 if (!equality_code
8452 && bitpos0 != bitpos1
8453 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8454 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8455 fold_overflow_warning (("assuming pointer wraparound does not "
8456 "occur when comparing P +- C1 with "
8457 "P +- C2"),
8458 WARN_STRICT_OVERFLOW_CONDITIONAL);
8460 switch (code)
8462 case EQ_EXPR:
8463 return constant_boolean_node (bitpos0 == bitpos1, type);
8464 case NE_EXPR:
8465 return constant_boolean_node (bitpos0 != bitpos1, type);
8466 case LT_EXPR:
8467 return constant_boolean_node (bitpos0 < bitpos1, type);
8468 case LE_EXPR:
8469 return constant_boolean_node (bitpos0 <= bitpos1, type);
8470 case GE_EXPR:
8471 return constant_boolean_node (bitpos0 >= bitpos1, type);
8472 case GT_EXPR:
8473 return constant_boolean_node (bitpos0 > bitpos1, type);
8474 default:;
8477 /* We can simplify the comparison to a comparison of the variable
8478 offset parts if the constant offset parts are equal.
8479 Be careful to use signed sizetype here because otherwise we
8480 mess with array offsets in the wrong way. This is possible
8481 because pointer arithmetic is restricted to retain within an
8482 object and overflow on pointer differences is undefined as of
8483 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8484 else if (bitpos0 == bitpos1
8485 && (equality_code
8486 || (indirect_base0
8487 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8488 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8490 /* By converting to signed sizetype we cover middle-end pointer
8491 arithmetic which operates on unsigned pointer types of size
8492 type size and ARRAY_REF offsets which are properly sign or
8493 zero extended from their type in case it is narrower than
8494 sizetype. */
8495 if (offset0 == NULL_TREE)
8496 offset0 = build_int_cst (ssizetype, 0);
8497 else
8498 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8499 if (offset1 == NULL_TREE)
8500 offset1 = build_int_cst (ssizetype, 0);
8501 else
8502 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8504 if (!equality_code
8505 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8506 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8507 fold_overflow_warning (("assuming pointer wraparound does not "
8508 "occur when comparing P +- C1 with "
8509 "P +- C2"),
8510 WARN_STRICT_OVERFLOW_COMPARISON);
8512 return fold_build2_loc (loc, code, type, offset0, offset1);
8515 /* For equal offsets we can simplify to a comparison of the
8516 base addresses. */
8517 else if (bitpos0 == bitpos1
8518 && (indirect_base0
8519 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8520 && (indirect_base1
8521 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8522 && ((offset0 == offset1)
8523 || (offset0 && offset1
8524 && operand_equal_p (offset0, offset1, 0))))
8526 if (indirect_base0)
8527 base0 = build_fold_addr_expr_loc (loc, base0);
8528 if (indirect_base1)
8529 base1 = build_fold_addr_expr_loc (loc, base1);
8530 return fold_build2_loc (loc, code, type, base0, base1);
8532 /* Comparison between an ordinary (non-weak) symbol and a null
8533 pointer can be eliminated since such symbols must have a non
8534 null address. In C, relational expressions between pointers
8535 to objects and null pointers are undefined. The results
8536 below follow the C++ rules with the additional property that
8537 every object pointer compares greater than a null pointer.
8539 else if (DECL_P (base0)
8540 && maybe_nonzero_address (base0) > 0
8541 /* Avoid folding references to struct members at offset 0 to
8542 prevent tests like '&ptr->firstmember == 0' from getting
8543 eliminated. When ptr is null, although the -> expression
8544 is strictly speaking invalid, GCC retains it as a matter
8545 of QoI. See PR c/44555. */
8546 && (offset0 == NULL_TREE && bitpos0 != 0)
8547 /* The caller guarantees that when one of the arguments is
8548 constant (i.e., null in this case) it is second. */
8549 && integer_zerop (arg1))
8551 switch (code)
8553 case EQ_EXPR:
8554 case LE_EXPR:
8555 case LT_EXPR:
8556 return constant_boolean_node (false, type);
8557 case GE_EXPR:
8558 case GT_EXPR:
8559 case NE_EXPR:
8560 return constant_boolean_node (true, type);
8561 default:
8562 gcc_unreachable ();
8567 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8568 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8569 the resulting offset is smaller in absolute value than the
8570 original one and has the same sign. */
8571 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8572 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8573 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8574 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8575 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8576 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8577 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8578 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8580 tree const1 = TREE_OPERAND (arg0, 1);
8581 tree const2 = TREE_OPERAND (arg1, 1);
8582 tree variable1 = TREE_OPERAND (arg0, 0);
8583 tree variable2 = TREE_OPERAND (arg1, 0);
8584 tree cst;
8585 const char * const warnmsg = G_("assuming signed overflow does not "
8586 "occur when combining constants around "
8587 "a comparison");
8589 /* Put the constant on the side where it doesn't overflow and is
8590 of lower absolute value and of same sign than before. */
8591 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8592 ? MINUS_EXPR : PLUS_EXPR,
8593 const2, const1);
8594 if (!TREE_OVERFLOW (cst)
8595 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8596 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8598 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8599 return fold_build2_loc (loc, code, type,
8600 variable1,
8601 fold_build2_loc (loc, TREE_CODE (arg1),
8602 TREE_TYPE (arg1),
8603 variable2, cst));
8606 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8607 ? MINUS_EXPR : PLUS_EXPR,
8608 const1, const2);
8609 if (!TREE_OVERFLOW (cst)
8610 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8611 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8613 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8614 return fold_build2_loc (loc, code, type,
8615 fold_build2_loc (loc, TREE_CODE (arg0),
8616 TREE_TYPE (arg0),
8617 variable1, cst),
8618 variable2);
8622 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8623 if (tem)
8624 return tem;
8626 /* If we are comparing an expression that just has comparisons
8627 of two integer values, arithmetic expressions of those comparisons,
8628 and constants, we can simplify it. There are only three cases
8629 to check: the two values can either be equal, the first can be
8630 greater, or the second can be greater. Fold the expression for
8631 those three values. Since each value must be 0 or 1, we have
8632 eight possibilities, each of which corresponds to the constant 0
8633 or 1 or one of the six possible comparisons.
8635 This handles common cases like (a > b) == 0 but also handles
8636 expressions like ((x > y) - (y > x)) > 0, which supposedly
8637 occur in macroized code. */
8639 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8641 tree cval1 = 0, cval2 = 0;
8642 int save_p = 0;
8644 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8645 /* Don't handle degenerate cases here; they should already
8646 have been handled anyway. */
8647 && cval1 != 0 && cval2 != 0
8648 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8649 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8650 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8651 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8652 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8653 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8654 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8656 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8657 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8659 /* We can't just pass T to eval_subst in case cval1 or cval2
8660 was the same as ARG1. */
8662 tree high_result
8663 = fold_build2_loc (loc, code, type,
8664 eval_subst (loc, arg0, cval1, maxval,
8665 cval2, minval),
8666 arg1);
8667 tree equal_result
8668 = fold_build2_loc (loc, code, type,
8669 eval_subst (loc, arg0, cval1, maxval,
8670 cval2, maxval),
8671 arg1);
8672 tree low_result
8673 = fold_build2_loc (loc, code, type,
8674 eval_subst (loc, arg0, cval1, minval,
8675 cval2, maxval),
8676 arg1);
8678 /* All three of these results should be 0 or 1. Confirm they are.
8679 Then use those values to select the proper code to use. */
8681 if (TREE_CODE (high_result) == INTEGER_CST
8682 && TREE_CODE (equal_result) == INTEGER_CST
8683 && TREE_CODE (low_result) == INTEGER_CST)
8685 /* Make a 3-bit mask with the high-order bit being the
8686 value for `>', the next for '=', and the low for '<'. */
8687 switch ((integer_onep (high_result) * 4)
8688 + (integer_onep (equal_result) * 2)
8689 + integer_onep (low_result))
8691 case 0:
8692 /* Always false. */
8693 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8694 case 1:
8695 code = LT_EXPR;
8696 break;
8697 case 2:
8698 code = EQ_EXPR;
8699 break;
8700 case 3:
8701 code = LE_EXPR;
8702 break;
8703 case 4:
8704 code = GT_EXPR;
8705 break;
8706 case 5:
8707 code = NE_EXPR;
8708 break;
8709 case 6:
8710 code = GE_EXPR;
8711 break;
8712 case 7:
8713 /* Always true. */
8714 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8717 if (save_p)
8719 tem = save_expr (build2 (code, type, cval1, cval2));
8720 SET_EXPR_LOCATION (tem, loc);
8721 return tem;
8723 return fold_build2_loc (loc, code, type, cval1, cval2);
8728 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8729 into a single range test. */
8730 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8731 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8732 && TREE_CODE (arg1) == INTEGER_CST
8733 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8734 && !integer_zerop (TREE_OPERAND (arg0, 1))
8735 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8736 && !TREE_OVERFLOW (arg1))
8738 tem = fold_div_compare (loc, code, type, arg0, arg1);
8739 if (tem != NULL_TREE)
8740 return tem;
8743 return NULL_TREE;
8747 /* Subroutine of fold_binary. Optimize complex multiplications of the
8748 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8749 argument EXPR represents the expression "z" of type TYPE. */
8751 static tree
8752 fold_mult_zconjz (location_t loc, tree type, tree expr)
8754 tree itype = TREE_TYPE (type);
8755 tree rpart, ipart, tem;
8757 if (TREE_CODE (expr) == COMPLEX_EXPR)
8759 rpart = TREE_OPERAND (expr, 0);
8760 ipart = TREE_OPERAND (expr, 1);
8762 else if (TREE_CODE (expr) == COMPLEX_CST)
8764 rpart = TREE_REALPART (expr);
8765 ipart = TREE_IMAGPART (expr);
8767 else
8769 expr = save_expr (expr);
8770 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8771 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8774 rpart = save_expr (rpart);
8775 ipart = save_expr (ipart);
8776 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8777 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8778 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8779 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8780 build_zero_cst (itype));
8784 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8785 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8787 static bool
8788 vec_cst_ctor_to_array (tree arg, tree *elts)
8790 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8792 if (TREE_CODE (arg) == VECTOR_CST)
8794 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8795 elts[i] = VECTOR_CST_ELT (arg, i);
8797 else if (TREE_CODE (arg) == CONSTRUCTOR)
8799 constructor_elt *elt;
8801 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8802 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8803 return false;
8804 else
8805 elts[i] = elt->value;
8807 else
8808 return false;
8809 for (; i < nelts; i++)
8810 elts[i]
8811 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8812 return true;
8815 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8816 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8817 NULL_TREE otherwise. */
8819 static tree
8820 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8822 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8823 tree *elts;
8824 bool need_ctor = false;
8826 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8827 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8828 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8829 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8830 return NULL_TREE;
8832 elts = XALLOCAVEC (tree, nelts * 3);
8833 if (!vec_cst_ctor_to_array (arg0, elts)
8834 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8835 return NULL_TREE;
8837 for (i = 0; i < nelts; i++)
8839 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8840 need_ctor = true;
8841 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8844 if (need_ctor)
8846 vec<constructor_elt, va_gc> *v;
8847 vec_alloc (v, nelts);
8848 for (i = 0; i < nelts; i++)
8849 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8850 return build_constructor (type, v);
8852 else
8853 return build_vector (type, &elts[2 * nelts]);
8856 /* Try to fold a pointer difference of type TYPE two address expressions of
8857 array references AREF0 and AREF1 using location LOC. Return a
8858 simplified expression for the difference or NULL_TREE. */
8860 static tree
8861 fold_addr_of_array_ref_difference (location_t loc, tree type,
8862 tree aref0, tree aref1)
8864 tree base0 = TREE_OPERAND (aref0, 0);
8865 tree base1 = TREE_OPERAND (aref1, 0);
8866 tree base_offset = build_int_cst (type, 0);
8868 /* If the bases are array references as well, recurse. If the bases
8869 are pointer indirections compute the difference of the pointers.
8870 If the bases are equal, we are set. */
8871 if ((TREE_CODE (base0) == ARRAY_REF
8872 && TREE_CODE (base1) == ARRAY_REF
8873 && (base_offset
8874 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8875 || (INDIRECT_REF_P (base0)
8876 && INDIRECT_REF_P (base1)
8877 && (base_offset
8878 = fold_binary_loc (loc, MINUS_EXPR, type,
8879 fold_convert (type, TREE_OPERAND (base0, 0)),
8880 fold_convert (type,
8881 TREE_OPERAND (base1, 0)))))
8882 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8884 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8885 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8886 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8887 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8888 return fold_build2_loc (loc, PLUS_EXPR, type,
8889 base_offset,
8890 fold_build2_loc (loc, MULT_EXPR, type,
8891 diff, esz));
8893 return NULL_TREE;
8896 /* If the real or vector real constant CST of type TYPE has an exact
8897 inverse, return it, else return NULL. */
8899 tree
8900 exact_inverse (tree type, tree cst)
8902 REAL_VALUE_TYPE r;
8903 tree unit_type, *elts;
8904 machine_mode mode;
8905 unsigned vec_nelts, i;
8907 switch (TREE_CODE (cst))
8909 case REAL_CST:
8910 r = TREE_REAL_CST (cst);
8912 if (exact_real_inverse (TYPE_MODE (type), &r))
8913 return build_real (type, r);
8915 return NULL_TREE;
8917 case VECTOR_CST:
8918 vec_nelts = VECTOR_CST_NELTS (cst);
8919 elts = XALLOCAVEC (tree, vec_nelts);
8920 unit_type = TREE_TYPE (type);
8921 mode = TYPE_MODE (unit_type);
8923 for (i = 0; i < vec_nelts; i++)
8925 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8926 if (!exact_real_inverse (mode, &r))
8927 return NULL_TREE;
8928 elts[i] = build_real (unit_type, r);
8931 return build_vector (type, elts);
8933 default:
8934 return NULL_TREE;
8938 /* Mask out the tz least significant bits of X of type TYPE where
8939 tz is the number of trailing zeroes in Y. */
8940 static wide_int
8941 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8943 int tz = wi::ctz (y);
8944 if (tz > 0)
8945 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8946 return x;
8949 /* Return true when T is an address and is known to be nonzero.
8950 For floating point we further ensure that T is not denormal.
8951 Similar logic is present in nonzero_address in rtlanal.h.
8953 If the return value is based on the assumption that signed overflow
8954 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8955 change *STRICT_OVERFLOW_P. */
8957 static bool
8958 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8960 tree type = TREE_TYPE (t);
8961 enum tree_code code;
8963 /* Doing something useful for floating point would need more work. */
8964 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8965 return false;
8967 code = TREE_CODE (t);
8968 switch (TREE_CODE_CLASS (code))
8970 case tcc_unary:
8971 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8972 strict_overflow_p);
8973 case tcc_binary:
8974 case tcc_comparison:
8975 return tree_binary_nonzero_warnv_p (code, type,
8976 TREE_OPERAND (t, 0),
8977 TREE_OPERAND (t, 1),
8978 strict_overflow_p);
8979 case tcc_constant:
8980 case tcc_declaration:
8981 case tcc_reference:
8982 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8984 default:
8985 break;
8988 switch (code)
8990 case TRUTH_NOT_EXPR:
8991 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8992 strict_overflow_p);
8994 case TRUTH_AND_EXPR:
8995 case TRUTH_OR_EXPR:
8996 case TRUTH_XOR_EXPR:
8997 return tree_binary_nonzero_warnv_p (code, type,
8998 TREE_OPERAND (t, 0),
8999 TREE_OPERAND (t, 1),
9000 strict_overflow_p);
9002 case COND_EXPR:
9003 case CONSTRUCTOR:
9004 case OBJ_TYPE_REF:
9005 case ASSERT_EXPR:
9006 case ADDR_EXPR:
9007 case WITH_SIZE_EXPR:
9008 case SSA_NAME:
9009 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9011 case COMPOUND_EXPR:
9012 case MODIFY_EXPR:
9013 case BIND_EXPR:
9014 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9015 strict_overflow_p);
9017 case SAVE_EXPR:
9018 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9019 strict_overflow_p);
9021 case CALL_EXPR:
9023 tree fndecl = get_callee_fndecl (t);
9024 if (!fndecl) return false;
9025 if (flag_delete_null_pointer_checks && !flag_check_new
9026 && DECL_IS_OPERATOR_NEW (fndecl)
9027 && !TREE_NOTHROW (fndecl))
9028 return true;
9029 if (flag_delete_null_pointer_checks
9030 && lookup_attribute ("returns_nonnull",
9031 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9032 return true;
9033 return alloca_call_p (t);
9036 default:
9037 break;
9039 return false;
9042 /* Return true when T is an address and is known to be nonzero.
9043 Handle warnings about undefined signed overflow. */
9045 static bool
9046 tree_expr_nonzero_p (tree t)
9048 bool ret, strict_overflow_p;
9050 strict_overflow_p = false;
9051 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9052 if (strict_overflow_p)
9053 fold_overflow_warning (("assuming signed overflow does not occur when "
9054 "determining that expression is always "
9055 "non-zero"),
9056 WARN_STRICT_OVERFLOW_MISC);
9057 return ret;
9060 /* Return true if T is known not to be equal to an integer W. */
9062 bool
9063 expr_not_equal_to (tree t, const wide_int &w)
9065 wide_int min, max, nz;
9066 value_range_type rtype;
9067 switch (TREE_CODE (t))
9069 case INTEGER_CST:
9070 return wi::ne_p (t, w);
9072 case SSA_NAME:
9073 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9074 return false;
9075 rtype = get_range_info (t, &min, &max);
9076 if (rtype == VR_RANGE)
9078 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9079 return true;
9080 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9081 return true;
9083 else if (rtype == VR_ANTI_RANGE
9084 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9085 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9086 return true;
9087 /* If T has some known zero bits and W has any of those bits set,
9088 then T is known not to be equal to W. */
9089 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9090 TYPE_PRECISION (TREE_TYPE (t))), 0))
9091 return true;
9092 return false;
9094 default:
9095 return false;
9099 /* Fold a binary expression of code CODE and type TYPE with operands
9100 OP0 and OP1. LOC is the location of the resulting expression.
9101 Return the folded expression if folding is successful. Otherwise,
9102 return NULL_TREE. */
9104 tree
9105 fold_binary_loc (location_t loc,
9106 enum tree_code code, tree type, tree op0, tree op1)
9108 enum tree_code_class kind = TREE_CODE_CLASS (code);
9109 tree arg0, arg1, tem;
9110 tree t1 = NULL_TREE;
9111 bool strict_overflow_p;
9112 unsigned int prec;
9114 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9115 && TREE_CODE_LENGTH (code) == 2
9116 && op0 != NULL_TREE
9117 && op1 != NULL_TREE);
9119 arg0 = op0;
9120 arg1 = op1;
9122 /* Strip any conversions that don't change the mode. This is
9123 safe for every expression, except for a comparison expression
9124 because its signedness is derived from its operands. So, in
9125 the latter case, only strip conversions that don't change the
9126 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9127 preserved.
9129 Note that this is done as an internal manipulation within the
9130 constant folder, in order to find the simplest representation
9131 of the arguments so that their form can be studied. In any
9132 cases, the appropriate type conversions should be put back in
9133 the tree that will get out of the constant folder. */
9135 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9137 STRIP_SIGN_NOPS (arg0);
9138 STRIP_SIGN_NOPS (arg1);
9140 else
9142 STRIP_NOPS (arg0);
9143 STRIP_NOPS (arg1);
9146 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9147 constant but we can't do arithmetic on them. */
9148 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9150 tem = const_binop (code, type, arg0, arg1);
9151 if (tem != NULL_TREE)
9153 if (TREE_TYPE (tem) != type)
9154 tem = fold_convert_loc (loc, type, tem);
9155 return tem;
9159 /* If this is a commutative operation, and ARG0 is a constant, move it
9160 to ARG1 to reduce the number of tests below. */
9161 if (commutative_tree_code (code)
9162 && tree_swap_operands_p (arg0, arg1, true))
9163 return fold_build2_loc (loc, code, type, op1, op0);
9165 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9166 to ARG1 to reduce the number of tests below. */
9167 if (kind == tcc_comparison
9168 && tree_swap_operands_p (arg0, arg1, true))
9169 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9171 tem = generic_simplify (loc, code, type, op0, op1);
9172 if (tem)
9173 return tem;
9175 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9177 First check for cases where an arithmetic operation is applied to a
9178 compound, conditional, or comparison operation. Push the arithmetic
9179 operation inside the compound or conditional to see if any folding
9180 can then be done. Convert comparison to conditional for this purpose.
9181 The also optimizes non-constant cases that used to be done in
9182 expand_expr.
9184 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9185 one of the operands is a comparison and the other is a comparison, a
9186 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9187 code below would make the expression more complex. Change it to a
9188 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9189 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9191 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9192 || code == EQ_EXPR || code == NE_EXPR)
9193 && TREE_CODE (type) != VECTOR_TYPE
9194 && ((truth_value_p (TREE_CODE (arg0))
9195 && (truth_value_p (TREE_CODE (arg1))
9196 || (TREE_CODE (arg1) == BIT_AND_EXPR
9197 && integer_onep (TREE_OPERAND (arg1, 1)))))
9198 || (truth_value_p (TREE_CODE (arg1))
9199 && (truth_value_p (TREE_CODE (arg0))
9200 || (TREE_CODE (arg0) == BIT_AND_EXPR
9201 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9203 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9204 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9205 : TRUTH_XOR_EXPR,
9206 boolean_type_node,
9207 fold_convert_loc (loc, boolean_type_node, arg0),
9208 fold_convert_loc (loc, boolean_type_node, arg1));
9210 if (code == EQ_EXPR)
9211 tem = invert_truthvalue_loc (loc, tem);
9213 return fold_convert_loc (loc, type, tem);
9216 if (TREE_CODE_CLASS (code) == tcc_binary
9217 || TREE_CODE_CLASS (code) == tcc_comparison)
9219 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9221 tem = fold_build2_loc (loc, code, type,
9222 fold_convert_loc (loc, TREE_TYPE (op0),
9223 TREE_OPERAND (arg0, 1)), op1);
9224 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9225 tem);
9227 if (TREE_CODE (arg1) == COMPOUND_EXPR
9228 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9230 tem = fold_build2_loc (loc, code, type, op0,
9231 fold_convert_loc (loc, TREE_TYPE (op1),
9232 TREE_OPERAND (arg1, 1)));
9233 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9234 tem);
9237 if (TREE_CODE (arg0) == COND_EXPR
9238 || TREE_CODE (arg0) == VEC_COND_EXPR
9239 || COMPARISON_CLASS_P (arg0))
9241 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9242 arg0, arg1,
9243 /*cond_first_p=*/1);
9244 if (tem != NULL_TREE)
9245 return tem;
9248 if (TREE_CODE (arg1) == COND_EXPR
9249 || TREE_CODE (arg1) == VEC_COND_EXPR
9250 || COMPARISON_CLASS_P (arg1))
9252 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9253 arg1, arg0,
9254 /*cond_first_p=*/0);
9255 if (tem != NULL_TREE)
9256 return tem;
9260 switch (code)
9262 case MEM_REF:
9263 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9264 if (TREE_CODE (arg0) == ADDR_EXPR
9265 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9267 tree iref = TREE_OPERAND (arg0, 0);
9268 return fold_build2 (MEM_REF, type,
9269 TREE_OPERAND (iref, 0),
9270 int_const_binop (PLUS_EXPR, arg1,
9271 TREE_OPERAND (iref, 1)));
9274 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9275 if (TREE_CODE (arg0) == ADDR_EXPR
9276 && handled_component_p (TREE_OPERAND (arg0, 0)))
9278 tree base;
9279 HOST_WIDE_INT coffset;
9280 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9281 &coffset);
9282 if (!base)
9283 return NULL_TREE;
9284 return fold_build2 (MEM_REF, type,
9285 build_fold_addr_expr (base),
9286 int_const_binop (PLUS_EXPR, arg1,
9287 size_int (coffset)));
9290 return NULL_TREE;
9292 case POINTER_PLUS_EXPR:
9293 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9294 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9295 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9296 return fold_convert_loc (loc, type,
9297 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9298 fold_convert_loc (loc, sizetype,
9299 arg1),
9300 fold_convert_loc (loc, sizetype,
9301 arg0)));
9303 return NULL_TREE;
9305 case PLUS_EXPR:
9306 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9308 /* X + (X / CST) * -CST is X % CST. */
9309 if (TREE_CODE (arg1) == MULT_EXPR
9310 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9311 && operand_equal_p (arg0,
9312 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9314 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9315 tree cst1 = TREE_OPERAND (arg1, 1);
9316 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9317 cst1, cst0);
9318 if (sum && integer_zerop (sum))
9319 return fold_convert_loc (loc, type,
9320 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9321 TREE_TYPE (arg0), arg0,
9322 cst0));
9326 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9327 one. Make sure the type is not saturating and has the signedness of
9328 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9329 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9330 if ((TREE_CODE (arg0) == MULT_EXPR
9331 || TREE_CODE (arg1) == MULT_EXPR)
9332 && !TYPE_SATURATING (type)
9333 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9334 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9335 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9337 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9338 if (tem)
9339 return tem;
9342 if (! FLOAT_TYPE_P (type))
9344 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9345 (plus (plus (mult) (mult)) (foo)) so that we can
9346 take advantage of the factoring cases below. */
9347 if (ANY_INTEGRAL_TYPE_P (type)
9348 && TYPE_OVERFLOW_WRAPS (type)
9349 && (((TREE_CODE (arg0) == PLUS_EXPR
9350 || TREE_CODE (arg0) == MINUS_EXPR)
9351 && TREE_CODE (arg1) == MULT_EXPR)
9352 || ((TREE_CODE (arg1) == PLUS_EXPR
9353 || TREE_CODE (arg1) == MINUS_EXPR)
9354 && TREE_CODE (arg0) == MULT_EXPR)))
9356 tree parg0, parg1, parg, marg;
9357 enum tree_code pcode;
9359 if (TREE_CODE (arg1) == MULT_EXPR)
9360 parg = arg0, marg = arg1;
9361 else
9362 parg = arg1, marg = arg0;
9363 pcode = TREE_CODE (parg);
9364 parg0 = TREE_OPERAND (parg, 0);
9365 parg1 = TREE_OPERAND (parg, 1);
9366 STRIP_NOPS (parg0);
9367 STRIP_NOPS (parg1);
9369 if (TREE_CODE (parg0) == MULT_EXPR
9370 && TREE_CODE (parg1) != MULT_EXPR)
9371 return fold_build2_loc (loc, pcode, type,
9372 fold_build2_loc (loc, PLUS_EXPR, type,
9373 fold_convert_loc (loc, type,
9374 parg0),
9375 fold_convert_loc (loc, type,
9376 marg)),
9377 fold_convert_loc (loc, type, parg1));
9378 if (TREE_CODE (parg0) != MULT_EXPR
9379 && TREE_CODE (parg1) == MULT_EXPR)
9380 return
9381 fold_build2_loc (loc, PLUS_EXPR, type,
9382 fold_convert_loc (loc, type, parg0),
9383 fold_build2_loc (loc, pcode, type,
9384 fold_convert_loc (loc, type, marg),
9385 fold_convert_loc (loc, type,
9386 parg1)));
9389 else
9391 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9392 to __complex__ ( x, y ). This is not the same for SNaNs or
9393 if signed zeros are involved. */
9394 if (!HONOR_SNANS (element_mode (arg0))
9395 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9396 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9398 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9399 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9400 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9401 bool arg0rz = false, arg0iz = false;
9402 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9403 || (arg0i && (arg0iz = real_zerop (arg0i))))
9405 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9406 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9407 if (arg0rz && arg1i && real_zerop (arg1i))
9409 tree rp = arg1r ? arg1r
9410 : build1 (REALPART_EXPR, rtype, arg1);
9411 tree ip = arg0i ? arg0i
9412 : build1 (IMAGPART_EXPR, rtype, arg0);
9413 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9415 else if (arg0iz && arg1r && real_zerop (arg1r))
9417 tree rp = arg0r ? arg0r
9418 : build1 (REALPART_EXPR, rtype, arg0);
9419 tree ip = arg1i ? arg1i
9420 : build1 (IMAGPART_EXPR, rtype, arg1);
9421 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9426 if (flag_unsafe_math_optimizations
9427 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9428 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9429 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9430 return tem;
9432 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9433 We associate floats only if the user has specified
9434 -fassociative-math. */
9435 if (flag_associative_math
9436 && TREE_CODE (arg1) == PLUS_EXPR
9437 && TREE_CODE (arg0) != MULT_EXPR)
9439 tree tree10 = TREE_OPERAND (arg1, 0);
9440 tree tree11 = TREE_OPERAND (arg1, 1);
9441 if (TREE_CODE (tree11) == MULT_EXPR
9442 && TREE_CODE (tree10) == MULT_EXPR)
9444 tree tree0;
9445 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9446 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9449 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9450 We associate floats only if the user has specified
9451 -fassociative-math. */
9452 if (flag_associative_math
9453 && TREE_CODE (arg0) == PLUS_EXPR
9454 && TREE_CODE (arg1) != MULT_EXPR)
9456 tree tree00 = TREE_OPERAND (arg0, 0);
9457 tree tree01 = TREE_OPERAND (arg0, 1);
9458 if (TREE_CODE (tree01) == MULT_EXPR
9459 && TREE_CODE (tree00) == MULT_EXPR)
9461 tree tree0;
9462 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9463 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9468 bit_rotate:
9469 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9470 is a rotate of A by C1 bits. */
9471 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9472 is a rotate of A by B bits. */
9474 enum tree_code code0, code1;
9475 tree rtype;
9476 code0 = TREE_CODE (arg0);
9477 code1 = TREE_CODE (arg1);
9478 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9479 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9480 && operand_equal_p (TREE_OPERAND (arg0, 0),
9481 TREE_OPERAND (arg1, 0), 0)
9482 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9483 TYPE_UNSIGNED (rtype))
9484 /* Only create rotates in complete modes. Other cases are not
9485 expanded properly. */
9486 && (element_precision (rtype)
9487 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9489 tree tree01, tree11;
9490 enum tree_code code01, code11;
9492 tree01 = TREE_OPERAND (arg0, 1);
9493 tree11 = TREE_OPERAND (arg1, 1);
9494 STRIP_NOPS (tree01);
9495 STRIP_NOPS (tree11);
9496 code01 = TREE_CODE (tree01);
9497 code11 = TREE_CODE (tree11);
9498 if (code01 == INTEGER_CST
9499 && code11 == INTEGER_CST
9500 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9501 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9503 tem = build2_loc (loc, LROTATE_EXPR,
9504 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9505 TREE_OPERAND (arg0, 0),
9506 code0 == LSHIFT_EXPR
9507 ? TREE_OPERAND (arg0, 1)
9508 : TREE_OPERAND (arg1, 1));
9509 return fold_convert_loc (loc, type, tem);
9511 else if (code11 == MINUS_EXPR)
9513 tree tree110, tree111;
9514 tree110 = TREE_OPERAND (tree11, 0);
9515 tree111 = TREE_OPERAND (tree11, 1);
9516 STRIP_NOPS (tree110);
9517 STRIP_NOPS (tree111);
9518 if (TREE_CODE (tree110) == INTEGER_CST
9519 && 0 == compare_tree_int (tree110,
9520 element_precision
9521 (TREE_TYPE (TREE_OPERAND
9522 (arg0, 0))))
9523 && operand_equal_p (tree01, tree111, 0))
9524 return
9525 fold_convert_loc (loc, type,
9526 build2 ((code0 == LSHIFT_EXPR
9527 ? LROTATE_EXPR
9528 : RROTATE_EXPR),
9529 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9530 TREE_OPERAND (arg0, 0),
9531 TREE_OPERAND (arg0, 1)));
9533 else if (code01 == MINUS_EXPR)
9535 tree tree010, tree011;
9536 tree010 = TREE_OPERAND (tree01, 0);
9537 tree011 = TREE_OPERAND (tree01, 1);
9538 STRIP_NOPS (tree010);
9539 STRIP_NOPS (tree011);
9540 if (TREE_CODE (tree010) == INTEGER_CST
9541 && 0 == compare_tree_int (tree010,
9542 element_precision
9543 (TREE_TYPE (TREE_OPERAND
9544 (arg0, 0))))
9545 && operand_equal_p (tree11, tree011, 0))
9546 return fold_convert_loc
9547 (loc, type,
9548 build2 ((code0 != LSHIFT_EXPR
9549 ? LROTATE_EXPR
9550 : RROTATE_EXPR),
9551 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9552 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9557 associate:
9558 /* In most languages, can't associate operations on floats through
9559 parentheses. Rather than remember where the parentheses were, we
9560 don't associate floats at all, unless the user has specified
9561 -fassociative-math.
9562 And, we need to make sure type is not saturating. */
9564 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9565 && !TYPE_SATURATING (type))
9567 tree var0, con0, lit0, minus_lit0;
9568 tree var1, con1, lit1, minus_lit1;
9569 tree atype = type;
9570 bool ok = true;
9572 /* Split both trees into variables, constants, and literals. Then
9573 associate each group together, the constants with literals,
9574 then the result with variables. This increases the chances of
9575 literals being recombined later and of generating relocatable
9576 expressions for the sum of a constant and literal. */
9577 var0 = split_tree (loc, arg0, type, code,
9578 &con0, &lit0, &minus_lit0, 0);
9579 var1 = split_tree (loc, arg1, type, code,
9580 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9582 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9583 if (code == MINUS_EXPR)
9584 code = PLUS_EXPR;
9586 /* With undefined overflow prefer doing association in a type
9587 which wraps on overflow, if that is one of the operand types. */
9588 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9589 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9591 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9592 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9593 atype = TREE_TYPE (arg0);
9594 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9595 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9596 atype = TREE_TYPE (arg1);
9597 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9600 /* With undefined overflow we can only associate constants with one
9601 variable, and constants whose association doesn't overflow. */
9602 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9603 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9605 if (var0 && var1)
9607 tree tmp0 = var0;
9608 tree tmp1 = var1;
9609 bool one_neg = false;
9611 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9613 tmp0 = TREE_OPERAND (tmp0, 0);
9614 one_neg = !one_neg;
9616 if (CONVERT_EXPR_P (tmp0)
9617 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9618 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9619 <= TYPE_PRECISION (atype)))
9620 tmp0 = TREE_OPERAND (tmp0, 0);
9621 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9623 tmp1 = TREE_OPERAND (tmp1, 0);
9624 one_neg = !one_neg;
9626 if (CONVERT_EXPR_P (tmp1)
9627 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9628 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9629 <= TYPE_PRECISION (atype)))
9630 tmp1 = TREE_OPERAND (tmp1, 0);
9631 /* The only case we can still associate with two variables
9632 is if they cancel out. */
9633 if (!one_neg
9634 || !operand_equal_p (tmp0, tmp1, 0))
9635 ok = false;
9639 /* Only do something if we found more than two objects. Otherwise,
9640 nothing has changed and we risk infinite recursion. */
9641 if (ok
9642 && (2 < ((var0 != 0) + (var1 != 0)
9643 + (con0 != 0) + (con1 != 0)
9644 + (lit0 != 0) + (lit1 != 0)
9645 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9647 bool any_overflows = false;
9648 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9649 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9650 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9651 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9652 var0 = associate_trees (loc, var0, var1, code, atype);
9653 con0 = associate_trees (loc, con0, con1, code, atype);
9654 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9655 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9656 code, atype);
9658 /* Preserve the MINUS_EXPR if the negative part of the literal is
9659 greater than the positive part. Otherwise, the multiplicative
9660 folding code (i.e extract_muldiv) may be fooled in case
9661 unsigned constants are subtracted, like in the following
9662 example: ((X*2 + 4) - 8U)/2. */
9663 if (minus_lit0 && lit0)
9665 if (TREE_CODE (lit0) == INTEGER_CST
9666 && TREE_CODE (minus_lit0) == INTEGER_CST
9667 && tree_int_cst_lt (lit0, minus_lit0))
9669 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9670 MINUS_EXPR, atype);
9671 lit0 = 0;
9673 else
9675 lit0 = associate_trees (loc, lit0, minus_lit0,
9676 MINUS_EXPR, atype);
9677 minus_lit0 = 0;
9681 /* Don't introduce overflows through reassociation. */
9682 if (!any_overflows
9683 && ((lit0 && TREE_OVERFLOW_P (lit0))
9684 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9685 return NULL_TREE;
9687 if (minus_lit0)
9689 if (con0 == 0)
9690 return
9691 fold_convert_loc (loc, type,
9692 associate_trees (loc, var0, minus_lit0,
9693 MINUS_EXPR, atype));
9694 else
9696 con0 = associate_trees (loc, con0, minus_lit0,
9697 MINUS_EXPR, atype);
9698 return
9699 fold_convert_loc (loc, type,
9700 associate_trees (loc, var0, con0,
9701 PLUS_EXPR, atype));
9705 con0 = associate_trees (loc, con0, lit0, code, atype);
9706 return
9707 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9708 code, atype));
9712 return NULL_TREE;
9714 case MINUS_EXPR:
9715 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9716 if (TREE_CODE (arg0) == NEGATE_EXPR
9717 && negate_expr_p (op1)
9718 && reorder_operands_p (arg0, arg1))
9719 return fold_build2_loc (loc, MINUS_EXPR, type,
9720 negate_expr (op1),
9721 fold_convert_loc (loc, type,
9722 TREE_OPERAND (arg0, 0)));
9724 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9725 __complex__ ( x, -y ). This is not the same for SNaNs or if
9726 signed zeros are involved. */
9727 if (!HONOR_SNANS (element_mode (arg0))
9728 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9729 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9731 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9732 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9733 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9734 bool arg0rz = false, arg0iz = false;
9735 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9736 || (arg0i && (arg0iz = real_zerop (arg0i))))
9738 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9739 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9740 if (arg0rz && arg1i && real_zerop (arg1i))
9742 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9743 arg1r ? arg1r
9744 : build1 (REALPART_EXPR, rtype, arg1));
9745 tree ip = arg0i ? arg0i
9746 : build1 (IMAGPART_EXPR, rtype, arg0);
9747 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9749 else if (arg0iz && arg1r && real_zerop (arg1r))
9751 tree rp = arg0r ? arg0r
9752 : build1 (REALPART_EXPR, rtype, arg0);
9753 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9754 arg1i ? arg1i
9755 : build1 (IMAGPART_EXPR, rtype, arg1));
9756 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9761 /* A - B -> A + (-B) if B is easily negatable. */
9762 if (negate_expr_p (op1)
9763 && ! TYPE_OVERFLOW_SANITIZED (type)
9764 && ((FLOAT_TYPE_P (type)
9765 /* Avoid this transformation if B is a positive REAL_CST. */
9766 && (TREE_CODE (op1) != REAL_CST
9767 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9768 || INTEGRAL_TYPE_P (type)))
9769 return fold_build2_loc (loc, PLUS_EXPR, type,
9770 fold_convert_loc (loc, type, arg0),
9771 negate_expr (op1));
9773 /* Fold &a[i] - &a[j] to i-j. */
9774 if (TREE_CODE (arg0) == ADDR_EXPR
9775 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9776 && TREE_CODE (arg1) == ADDR_EXPR
9777 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9779 tree tem = fold_addr_of_array_ref_difference (loc, type,
9780 TREE_OPERAND (arg0, 0),
9781 TREE_OPERAND (arg1, 0));
9782 if (tem)
9783 return tem;
9786 if (FLOAT_TYPE_P (type)
9787 && flag_unsafe_math_optimizations
9788 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9789 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9790 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9791 return tem;
9793 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9794 one. Make sure the type is not saturating and has the signedness of
9795 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9796 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9797 if ((TREE_CODE (arg0) == MULT_EXPR
9798 || TREE_CODE (arg1) == MULT_EXPR)
9799 && !TYPE_SATURATING (type)
9800 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9801 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9802 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9804 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9805 if (tem)
9806 return tem;
9809 goto associate;
9811 case MULT_EXPR:
9812 if (! FLOAT_TYPE_P (type))
9814 /* Transform x * -C into -x * C if x is easily negatable. */
9815 if (TREE_CODE (op1) == INTEGER_CST
9816 && tree_int_cst_sgn (op1) == -1
9817 && negate_expr_p (op0)
9818 && (tem = negate_expr (op1)) != op1
9819 && ! TREE_OVERFLOW (tem))
9820 return fold_build2_loc (loc, MULT_EXPR, type,
9821 fold_convert_loc (loc, type,
9822 negate_expr (op0)), tem);
9824 strict_overflow_p = false;
9825 if (TREE_CODE (arg1) == INTEGER_CST
9826 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9827 &strict_overflow_p)))
9829 if (strict_overflow_p)
9830 fold_overflow_warning (("assuming signed overflow does not "
9831 "occur when simplifying "
9832 "multiplication"),
9833 WARN_STRICT_OVERFLOW_MISC);
9834 return fold_convert_loc (loc, type, tem);
9837 /* Optimize z * conj(z) for integer complex numbers. */
9838 if (TREE_CODE (arg0) == CONJ_EXPR
9839 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9840 return fold_mult_zconjz (loc, type, arg1);
9841 if (TREE_CODE (arg1) == CONJ_EXPR
9842 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9843 return fold_mult_zconjz (loc, type, arg0);
9845 else
9847 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9848 This is not the same for NaNs or if signed zeros are
9849 involved. */
9850 if (!HONOR_NANS (arg0)
9851 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9852 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9853 && TREE_CODE (arg1) == COMPLEX_CST
9854 && real_zerop (TREE_REALPART (arg1)))
9856 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9857 if (real_onep (TREE_IMAGPART (arg1)))
9858 return
9859 fold_build2_loc (loc, COMPLEX_EXPR, type,
9860 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9861 rtype, arg0)),
9862 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9863 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9864 return
9865 fold_build2_loc (loc, COMPLEX_EXPR, type,
9866 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9867 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9868 rtype, arg0)));
9871 /* Optimize z * conj(z) for floating point complex numbers.
9872 Guarded by flag_unsafe_math_optimizations as non-finite
9873 imaginary components don't produce scalar results. */
9874 if (flag_unsafe_math_optimizations
9875 && TREE_CODE (arg0) == CONJ_EXPR
9876 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9877 return fold_mult_zconjz (loc, type, arg1);
9878 if (flag_unsafe_math_optimizations
9879 && TREE_CODE (arg1) == CONJ_EXPR
9880 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9881 return fold_mult_zconjz (loc, type, arg0);
9883 goto associate;
9885 case BIT_IOR_EXPR:
9886 /* Canonicalize (X & C1) | C2. */
9887 if (TREE_CODE (arg0) == BIT_AND_EXPR
9888 && TREE_CODE (arg1) == INTEGER_CST
9889 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9891 int width = TYPE_PRECISION (type), w;
9892 wide_int c1 = TREE_OPERAND (arg0, 1);
9893 wide_int c2 = arg1;
9895 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9896 if ((c1 & c2) == c1)
9897 return omit_one_operand_loc (loc, type, arg1,
9898 TREE_OPERAND (arg0, 0));
9900 wide_int msk = wi::mask (width, false,
9901 TYPE_PRECISION (TREE_TYPE (arg1)));
9903 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9904 if (msk.and_not (c1 | c2) == 0)
9905 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9906 TREE_OPERAND (arg0, 0), arg1);
9908 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9909 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9910 mode which allows further optimizations. */
9911 c1 &= msk;
9912 c2 &= msk;
9913 wide_int c3 = c1.and_not (c2);
9914 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9916 wide_int mask = wi::mask (w, false,
9917 TYPE_PRECISION (type));
9918 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9920 c3 = mask;
9921 break;
9925 if (c3 != c1)
9926 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9927 fold_build2_loc (loc, BIT_AND_EXPR, type,
9928 TREE_OPERAND (arg0, 0),
9929 wide_int_to_tree (type,
9930 c3)),
9931 arg1);
9934 /* See if this can be simplified into a rotate first. If that
9935 is unsuccessful continue in the association code. */
9936 goto bit_rotate;
9938 case BIT_XOR_EXPR:
9939 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9940 if (TREE_CODE (arg0) == BIT_AND_EXPR
9941 && INTEGRAL_TYPE_P (type)
9942 && integer_onep (TREE_OPERAND (arg0, 1))
9943 && integer_onep (arg1))
9944 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9945 build_zero_cst (TREE_TYPE (arg0)));
9947 /* See if this can be simplified into a rotate first. If that
9948 is unsuccessful continue in the association code. */
9949 goto bit_rotate;
9951 case BIT_AND_EXPR:
9952 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9953 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9954 && INTEGRAL_TYPE_P (type)
9955 && integer_onep (TREE_OPERAND (arg0, 1))
9956 && integer_onep (arg1))
9958 tree tem2;
9959 tem = TREE_OPERAND (arg0, 0);
9960 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9961 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9962 tem, tem2);
9963 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9964 build_zero_cst (TREE_TYPE (tem)));
9966 /* Fold ~X & 1 as (X & 1) == 0. */
9967 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9968 && INTEGRAL_TYPE_P (type)
9969 && integer_onep (arg1))
9971 tree tem2;
9972 tem = TREE_OPERAND (arg0, 0);
9973 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9974 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9975 tem, tem2);
9976 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9977 build_zero_cst (TREE_TYPE (tem)));
9979 /* Fold !X & 1 as X == 0. */
9980 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9981 && integer_onep (arg1))
9983 tem = TREE_OPERAND (arg0, 0);
9984 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9985 build_zero_cst (TREE_TYPE (tem)));
9988 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9989 multiple of 1 << CST. */
9990 if (TREE_CODE (arg1) == INTEGER_CST)
9992 wide_int cst1 = arg1;
9993 wide_int ncst1 = -cst1;
9994 if ((cst1 & ncst1) == ncst1
9995 && multiple_of_p (type, arg0,
9996 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
9997 return fold_convert_loc (loc, type, arg0);
10000 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10001 bits from CST2. */
10002 if (TREE_CODE (arg1) == INTEGER_CST
10003 && TREE_CODE (arg0) == MULT_EXPR
10004 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10006 wide_int warg1 = arg1;
10007 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10009 if (masked == 0)
10010 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10011 arg0, arg1);
10012 else if (masked != warg1)
10014 /* Avoid the transform if arg1 is a mask of some
10015 mode which allows further optimizations. */
10016 int pop = wi::popcount (warg1);
10017 if (!(pop >= BITS_PER_UNIT
10018 && pow2p_hwi (pop)
10019 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10020 return fold_build2_loc (loc, code, type, op0,
10021 wide_int_to_tree (type, masked));
10025 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10026 ((A & N) + B) & M -> (A + B) & M
10027 Similarly if (N & M) == 0,
10028 ((A | N) + B) & M -> (A + B) & M
10029 and for - instead of + (or unary - instead of +)
10030 and/or ^ instead of |.
10031 If B is constant and (B & M) == 0, fold into A & M. */
10032 if (TREE_CODE (arg1) == INTEGER_CST)
10034 wide_int cst1 = arg1;
10035 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10036 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10037 && (TREE_CODE (arg0) == PLUS_EXPR
10038 || TREE_CODE (arg0) == MINUS_EXPR
10039 || TREE_CODE (arg0) == NEGATE_EXPR)
10040 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10041 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10043 tree pmop[2];
10044 int which = 0;
10045 wide_int cst0;
10047 /* Now we know that arg0 is (C + D) or (C - D) or
10048 -C and arg1 (M) is == (1LL << cst) - 1.
10049 Store C into PMOP[0] and D into PMOP[1]. */
10050 pmop[0] = TREE_OPERAND (arg0, 0);
10051 pmop[1] = NULL;
10052 if (TREE_CODE (arg0) != NEGATE_EXPR)
10054 pmop[1] = TREE_OPERAND (arg0, 1);
10055 which = 1;
10058 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10059 which = -1;
10061 for (; which >= 0; which--)
10062 switch (TREE_CODE (pmop[which]))
10064 case BIT_AND_EXPR:
10065 case BIT_IOR_EXPR:
10066 case BIT_XOR_EXPR:
10067 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10068 != INTEGER_CST)
10069 break;
10070 cst0 = TREE_OPERAND (pmop[which], 1);
10071 cst0 &= cst1;
10072 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10074 if (cst0 != cst1)
10075 break;
10077 else if (cst0 != 0)
10078 break;
10079 /* If C or D is of the form (A & N) where
10080 (N & M) == M, or of the form (A | N) or
10081 (A ^ N) where (N & M) == 0, replace it with A. */
10082 pmop[which] = TREE_OPERAND (pmop[which], 0);
10083 break;
10084 case INTEGER_CST:
10085 /* If C or D is a N where (N & M) == 0, it can be
10086 omitted (assumed 0). */
10087 if ((TREE_CODE (arg0) == PLUS_EXPR
10088 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10089 && (cst1 & pmop[which]) == 0)
10090 pmop[which] = NULL;
10091 break;
10092 default:
10093 break;
10096 /* Only build anything new if we optimized one or both arguments
10097 above. */
10098 if (pmop[0] != TREE_OPERAND (arg0, 0)
10099 || (TREE_CODE (arg0) != NEGATE_EXPR
10100 && pmop[1] != TREE_OPERAND (arg0, 1)))
10102 tree utype = TREE_TYPE (arg0);
10103 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10105 /* Perform the operations in a type that has defined
10106 overflow behavior. */
10107 utype = unsigned_type_for (TREE_TYPE (arg0));
10108 if (pmop[0] != NULL)
10109 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10110 if (pmop[1] != NULL)
10111 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10114 if (TREE_CODE (arg0) == NEGATE_EXPR)
10115 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10116 else if (TREE_CODE (arg0) == PLUS_EXPR)
10118 if (pmop[0] != NULL && pmop[1] != NULL)
10119 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10120 pmop[0], pmop[1]);
10121 else if (pmop[0] != NULL)
10122 tem = pmop[0];
10123 else if (pmop[1] != NULL)
10124 tem = pmop[1];
10125 else
10126 return build_int_cst (type, 0);
10128 else if (pmop[0] == NULL)
10129 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10130 else
10131 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10132 pmop[0], pmop[1]);
10133 /* TEM is now the new binary +, - or unary - replacement. */
10134 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10135 fold_convert_loc (loc, utype, arg1));
10136 return fold_convert_loc (loc, type, tem);
10141 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10142 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10143 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10145 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10147 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10148 if (mask == -1)
10149 return
10150 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10153 goto associate;
10155 case RDIV_EXPR:
10156 /* Don't touch a floating-point divide by zero unless the mode
10157 of the constant can represent infinity. */
10158 if (TREE_CODE (arg1) == REAL_CST
10159 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10160 && real_zerop (arg1))
10161 return NULL_TREE;
10163 /* (-A) / (-B) -> A / B */
10164 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10165 return fold_build2_loc (loc, RDIV_EXPR, type,
10166 TREE_OPERAND (arg0, 0),
10167 negate_expr (arg1));
10168 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10169 return fold_build2_loc (loc, RDIV_EXPR, type,
10170 negate_expr (arg0),
10171 TREE_OPERAND (arg1, 0));
10172 return NULL_TREE;
10174 case TRUNC_DIV_EXPR:
10175 /* Fall through */
10177 case FLOOR_DIV_EXPR:
10178 /* Simplify A / (B << N) where A and B are positive and B is
10179 a power of 2, to A >> (N + log2(B)). */
10180 strict_overflow_p = false;
10181 if (TREE_CODE (arg1) == LSHIFT_EXPR
10182 && (TYPE_UNSIGNED (type)
10183 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10185 tree sval = TREE_OPERAND (arg1, 0);
10186 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10188 tree sh_cnt = TREE_OPERAND (arg1, 1);
10189 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10190 wi::exact_log2 (sval));
10192 if (strict_overflow_p)
10193 fold_overflow_warning (("assuming signed overflow does not "
10194 "occur when simplifying A / (B << N)"),
10195 WARN_STRICT_OVERFLOW_MISC);
10197 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10198 sh_cnt, pow2);
10199 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10200 fold_convert_loc (loc, type, arg0), sh_cnt);
10204 /* Fall through */
10206 case ROUND_DIV_EXPR:
10207 case CEIL_DIV_EXPR:
10208 case EXACT_DIV_EXPR:
10209 if (integer_zerop (arg1))
10210 return NULL_TREE;
10212 /* Convert -A / -B to A / B when the type is signed and overflow is
10213 undefined. */
10214 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10215 && TREE_CODE (arg0) == NEGATE_EXPR
10216 && negate_expr_p (op1))
10218 if (INTEGRAL_TYPE_P (type))
10219 fold_overflow_warning (("assuming signed overflow does not occur "
10220 "when distributing negation across "
10221 "division"),
10222 WARN_STRICT_OVERFLOW_MISC);
10223 return fold_build2_loc (loc, code, type,
10224 fold_convert_loc (loc, type,
10225 TREE_OPERAND (arg0, 0)),
10226 negate_expr (op1));
10228 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10229 && TREE_CODE (arg1) == NEGATE_EXPR
10230 && negate_expr_p (op0))
10232 if (INTEGRAL_TYPE_P (type))
10233 fold_overflow_warning (("assuming signed overflow does not occur "
10234 "when distributing negation across "
10235 "division"),
10236 WARN_STRICT_OVERFLOW_MISC);
10237 return fold_build2_loc (loc, code, type,
10238 negate_expr (op0),
10239 fold_convert_loc (loc, type,
10240 TREE_OPERAND (arg1, 0)));
10243 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10244 operation, EXACT_DIV_EXPR.
10246 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10247 At one time others generated faster code, it's not clear if they do
10248 after the last round to changes to the DIV code in expmed.c. */
10249 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10250 && multiple_of_p (type, arg0, arg1))
10251 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10252 fold_convert (type, arg0),
10253 fold_convert (type, arg1));
10255 strict_overflow_p = false;
10256 if (TREE_CODE (arg1) == INTEGER_CST
10257 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10258 &strict_overflow_p)))
10260 if (strict_overflow_p)
10261 fold_overflow_warning (("assuming signed overflow does not occur "
10262 "when simplifying division"),
10263 WARN_STRICT_OVERFLOW_MISC);
10264 return fold_convert_loc (loc, type, tem);
10267 return NULL_TREE;
10269 case CEIL_MOD_EXPR:
10270 case FLOOR_MOD_EXPR:
10271 case ROUND_MOD_EXPR:
10272 case TRUNC_MOD_EXPR:
10273 strict_overflow_p = false;
10274 if (TREE_CODE (arg1) == INTEGER_CST
10275 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10276 &strict_overflow_p)))
10278 if (strict_overflow_p)
10279 fold_overflow_warning (("assuming signed overflow does not occur "
10280 "when simplifying modulus"),
10281 WARN_STRICT_OVERFLOW_MISC);
10282 return fold_convert_loc (loc, type, tem);
10285 return NULL_TREE;
10287 case LROTATE_EXPR:
10288 case RROTATE_EXPR:
10289 case RSHIFT_EXPR:
10290 case LSHIFT_EXPR:
10291 /* Since negative shift count is not well-defined,
10292 don't try to compute it in the compiler. */
10293 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10294 return NULL_TREE;
10296 prec = element_precision (type);
10298 /* If we have a rotate of a bit operation with the rotate count and
10299 the second operand of the bit operation both constant,
10300 permute the two operations. */
10301 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10302 && (TREE_CODE (arg0) == BIT_AND_EXPR
10303 || TREE_CODE (arg0) == BIT_IOR_EXPR
10304 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10305 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10307 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10308 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10309 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10310 fold_build2_loc (loc, code, type,
10311 arg00, arg1),
10312 fold_build2_loc (loc, code, type,
10313 arg01, arg1));
10316 /* Two consecutive rotates adding up to the some integer
10317 multiple of the precision of the type can be ignored. */
10318 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10319 && TREE_CODE (arg0) == RROTATE_EXPR
10320 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10321 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10322 prec) == 0)
10323 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10325 return NULL_TREE;
10327 case MIN_EXPR:
10328 case MAX_EXPR:
10329 goto associate;
10331 case TRUTH_ANDIF_EXPR:
10332 /* Note that the operands of this must be ints
10333 and their values must be 0 or 1.
10334 ("true" is a fixed value perhaps depending on the language.) */
10335 /* If first arg is constant zero, return it. */
10336 if (integer_zerop (arg0))
10337 return fold_convert_loc (loc, type, arg0);
10338 /* FALLTHRU */
10339 case TRUTH_AND_EXPR:
10340 /* If either arg is constant true, drop it. */
10341 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10342 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10343 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10344 /* Preserve sequence points. */
10345 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10346 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10347 /* If second arg is constant zero, result is zero, but first arg
10348 must be evaluated. */
10349 if (integer_zerop (arg1))
10350 return omit_one_operand_loc (loc, type, arg1, arg0);
10351 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10352 case will be handled here. */
10353 if (integer_zerop (arg0))
10354 return omit_one_operand_loc (loc, type, arg0, arg1);
10356 /* !X && X is always false. */
10357 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10358 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10359 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10360 /* X && !X is always false. */
10361 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10362 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10363 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10365 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10366 means A >= Y && A != MAX, but in this case we know that
10367 A < X <= MAX. */
10369 if (!TREE_SIDE_EFFECTS (arg0)
10370 && !TREE_SIDE_EFFECTS (arg1))
10372 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10373 if (tem && !operand_equal_p (tem, arg0, 0))
10374 return fold_build2_loc (loc, code, type, tem, arg1);
10376 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10377 if (tem && !operand_equal_p (tem, arg1, 0))
10378 return fold_build2_loc (loc, code, type, arg0, tem);
10381 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10382 != NULL_TREE)
10383 return tem;
10385 return NULL_TREE;
10387 case TRUTH_ORIF_EXPR:
10388 /* Note that the operands of this must be ints
10389 and their values must be 0 or true.
10390 ("true" is a fixed value perhaps depending on the language.) */
10391 /* If first arg is constant true, return it. */
10392 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10393 return fold_convert_loc (loc, type, arg0);
10394 /* FALLTHRU */
10395 case TRUTH_OR_EXPR:
10396 /* If either arg is constant zero, drop it. */
10397 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10398 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10399 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10400 /* Preserve sequence points. */
10401 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10402 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10403 /* If second arg is constant true, result is true, but we must
10404 evaluate first arg. */
10405 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10406 return omit_one_operand_loc (loc, type, arg1, arg0);
10407 /* Likewise for first arg, but note this only occurs here for
10408 TRUTH_OR_EXPR. */
10409 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10410 return omit_one_operand_loc (loc, type, arg0, arg1);
10412 /* !X || X is always true. */
10413 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10414 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10415 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10416 /* X || !X is always true. */
10417 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10418 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10419 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10421 /* (X && !Y) || (!X && Y) is X ^ Y */
10422 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10423 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10425 tree a0, a1, l0, l1, n0, n1;
10427 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10428 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10430 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10431 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10433 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10434 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10436 if ((operand_equal_p (n0, a0, 0)
10437 && operand_equal_p (n1, a1, 0))
10438 || (operand_equal_p (n0, a1, 0)
10439 && operand_equal_p (n1, a0, 0)))
10440 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10443 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10444 != NULL_TREE)
10445 return tem;
10447 return NULL_TREE;
10449 case TRUTH_XOR_EXPR:
10450 /* If the second arg is constant zero, drop it. */
10451 if (integer_zerop (arg1))
10452 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10453 /* If the second arg is constant true, this is a logical inversion. */
10454 if (integer_onep (arg1))
10456 tem = invert_truthvalue_loc (loc, arg0);
10457 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10459 /* Identical arguments cancel to zero. */
10460 if (operand_equal_p (arg0, arg1, 0))
10461 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10463 /* !X ^ X is always true. */
10464 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10465 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10466 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10468 /* X ^ !X is always true. */
10469 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10470 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10471 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10473 return NULL_TREE;
10475 case EQ_EXPR:
10476 case NE_EXPR:
10477 STRIP_NOPS (arg0);
10478 STRIP_NOPS (arg1);
10480 tem = fold_comparison (loc, code, type, op0, op1);
10481 if (tem != NULL_TREE)
10482 return tem;
10484 /* bool_var != 1 becomes !bool_var. */
10485 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10486 && code == NE_EXPR)
10487 return fold_convert_loc (loc, type,
10488 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10489 TREE_TYPE (arg0), arg0));
10491 /* bool_var == 0 becomes !bool_var. */
10492 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10493 && code == EQ_EXPR)
10494 return fold_convert_loc (loc, type,
10495 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10496 TREE_TYPE (arg0), arg0));
10498 /* !exp != 0 becomes !exp */
10499 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10500 && code == NE_EXPR)
10501 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10503 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10504 if ((TREE_CODE (arg0) == PLUS_EXPR
10505 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10506 || TREE_CODE (arg0) == MINUS_EXPR)
10507 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10508 0)),
10509 arg1, 0)
10510 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10511 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10513 tree val = TREE_OPERAND (arg0, 1);
10514 val = fold_build2_loc (loc, code, type, val,
10515 build_int_cst (TREE_TYPE (val), 0));
10516 return omit_two_operands_loc (loc, type, val,
10517 TREE_OPERAND (arg0, 0), arg1);
10520 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10521 if ((TREE_CODE (arg1) == PLUS_EXPR
10522 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10523 || TREE_CODE (arg1) == MINUS_EXPR)
10524 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10525 0)),
10526 arg0, 0)
10527 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10528 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10530 tree val = TREE_OPERAND (arg1, 1);
10531 val = fold_build2_loc (loc, code, type, val,
10532 build_int_cst (TREE_TYPE (val), 0));
10533 return omit_two_operands_loc (loc, type, val,
10534 TREE_OPERAND (arg1, 0), arg0);
10537 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10538 if (TREE_CODE (arg0) == MINUS_EXPR
10539 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10540 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10541 1)),
10542 arg1, 0)
10543 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10544 return omit_two_operands_loc (loc, type,
10545 code == NE_EXPR
10546 ? boolean_true_node : boolean_false_node,
10547 TREE_OPERAND (arg0, 1), arg1);
10549 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10550 if (TREE_CODE (arg1) == MINUS_EXPR
10551 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10552 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10553 1)),
10554 arg0, 0)
10555 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10556 return omit_two_operands_loc (loc, type,
10557 code == NE_EXPR
10558 ? boolean_true_node : boolean_false_node,
10559 TREE_OPERAND (arg1, 1), arg0);
10561 /* If this is an EQ or NE comparison with zero and ARG0 is
10562 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10563 two operations, but the latter can be done in one less insn
10564 on machines that have only two-operand insns or on which a
10565 constant cannot be the first operand. */
10566 if (TREE_CODE (arg0) == BIT_AND_EXPR
10567 && integer_zerop (arg1))
10569 tree arg00 = TREE_OPERAND (arg0, 0);
10570 tree arg01 = TREE_OPERAND (arg0, 1);
10571 if (TREE_CODE (arg00) == LSHIFT_EXPR
10572 && integer_onep (TREE_OPERAND (arg00, 0)))
10574 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10575 arg01, TREE_OPERAND (arg00, 1));
10576 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10577 build_int_cst (TREE_TYPE (arg0), 1));
10578 return fold_build2_loc (loc, code, type,
10579 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10580 arg1);
10582 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10583 && integer_onep (TREE_OPERAND (arg01, 0)))
10585 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10586 arg00, TREE_OPERAND (arg01, 1));
10587 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10588 build_int_cst (TREE_TYPE (arg0), 1));
10589 return fold_build2_loc (loc, code, type,
10590 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10591 arg1);
10595 /* If this is an NE or EQ comparison of zero against the result of a
10596 signed MOD operation whose second operand is a power of 2, make
10597 the MOD operation unsigned since it is simpler and equivalent. */
10598 if (integer_zerop (arg1)
10599 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10600 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10601 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10602 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10603 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10604 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10606 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10607 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10608 fold_convert_loc (loc, newtype,
10609 TREE_OPERAND (arg0, 0)),
10610 fold_convert_loc (loc, newtype,
10611 TREE_OPERAND (arg0, 1)));
10613 return fold_build2_loc (loc, code, type, newmod,
10614 fold_convert_loc (loc, newtype, arg1));
10617 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10618 C1 is a valid shift constant, and C2 is a power of two, i.e.
10619 a single bit. */
10620 if (TREE_CODE (arg0) == BIT_AND_EXPR
10621 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10622 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10623 == INTEGER_CST
10624 && integer_pow2p (TREE_OPERAND (arg0, 1))
10625 && integer_zerop (arg1))
10627 tree itype = TREE_TYPE (arg0);
10628 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10629 prec = TYPE_PRECISION (itype);
10631 /* Check for a valid shift count. */
10632 if (wi::ltu_p (arg001, prec))
10634 tree arg01 = TREE_OPERAND (arg0, 1);
10635 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10636 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10637 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10638 can be rewritten as (X & (C2 << C1)) != 0. */
10639 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10641 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10642 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10643 return fold_build2_loc (loc, code, type, tem,
10644 fold_convert_loc (loc, itype, arg1));
10646 /* Otherwise, for signed (arithmetic) shifts,
10647 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10648 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10649 else if (!TYPE_UNSIGNED (itype))
10650 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10651 arg000, build_int_cst (itype, 0));
10652 /* Otherwise, of unsigned (logical) shifts,
10653 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10654 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10655 else
10656 return omit_one_operand_loc (loc, type,
10657 code == EQ_EXPR ? integer_one_node
10658 : integer_zero_node,
10659 arg000);
10663 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10664 Similarly for NE_EXPR. */
10665 if (TREE_CODE (arg0) == BIT_AND_EXPR
10666 && TREE_CODE (arg1) == INTEGER_CST
10667 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10669 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10670 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10671 TREE_OPERAND (arg0, 1));
10672 tree dandnotc
10673 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10674 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10675 notc);
10676 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10677 if (integer_nonzerop (dandnotc))
10678 return omit_one_operand_loc (loc, type, rslt, arg0);
10681 /* If this is a comparison of a field, we may be able to simplify it. */
10682 if ((TREE_CODE (arg0) == COMPONENT_REF
10683 || TREE_CODE (arg0) == BIT_FIELD_REF)
10684 /* Handle the constant case even without -O
10685 to make sure the warnings are given. */
10686 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10688 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10689 if (t1)
10690 return t1;
10693 /* Optimize comparisons of strlen vs zero to a compare of the
10694 first character of the string vs zero. To wit,
10695 strlen(ptr) == 0 => *ptr == 0
10696 strlen(ptr) != 0 => *ptr != 0
10697 Other cases should reduce to one of these two (or a constant)
10698 due to the return value of strlen being unsigned. */
10699 if (TREE_CODE (arg0) == CALL_EXPR
10700 && integer_zerop (arg1))
10702 tree fndecl = get_callee_fndecl (arg0);
10704 if (fndecl
10705 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10706 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10707 && call_expr_nargs (arg0) == 1
10708 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10710 tree iref = build_fold_indirect_ref_loc (loc,
10711 CALL_EXPR_ARG (arg0, 0));
10712 return fold_build2_loc (loc, code, type, iref,
10713 build_int_cst (TREE_TYPE (iref), 0));
10717 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10718 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10719 if (TREE_CODE (arg0) == RSHIFT_EXPR
10720 && integer_zerop (arg1)
10721 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10723 tree arg00 = TREE_OPERAND (arg0, 0);
10724 tree arg01 = TREE_OPERAND (arg0, 1);
10725 tree itype = TREE_TYPE (arg00);
10726 if (wi::eq_p (arg01, element_precision (itype) - 1))
10728 if (TYPE_UNSIGNED (itype))
10730 itype = signed_type_for (itype);
10731 arg00 = fold_convert_loc (loc, itype, arg00);
10733 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10734 type, arg00, build_zero_cst (itype));
10738 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10739 (X & C) == 0 when C is a single bit. */
10740 if (TREE_CODE (arg0) == BIT_AND_EXPR
10741 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10742 && integer_zerop (arg1)
10743 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10745 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10746 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10747 TREE_OPERAND (arg0, 1));
10748 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10749 type, tem,
10750 fold_convert_loc (loc, TREE_TYPE (arg0),
10751 arg1));
10754 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10755 constant C is a power of two, i.e. a single bit. */
10756 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10757 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10758 && integer_zerop (arg1)
10759 && integer_pow2p (TREE_OPERAND (arg0, 1))
10760 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10761 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10763 tree arg00 = TREE_OPERAND (arg0, 0);
10764 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10765 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10768 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10769 when is C is a power of two, i.e. a single bit. */
10770 if (TREE_CODE (arg0) == BIT_AND_EXPR
10771 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10772 && integer_zerop (arg1)
10773 && integer_pow2p (TREE_OPERAND (arg0, 1))
10774 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10775 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10777 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10778 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10779 arg000, TREE_OPERAND (arg0, 1));
10780 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10781 tem, build_int_cst (TREE_TYPE (tem), 0));
10784 if (integer_zerop (arg1)
10785 && tree_expr_nonzero_p (arg0))
10787 tree res = constant_boolean_node (code==NE_EXPR, type);
10788 return omit_one_operand_loc (loc, type, res, arg0);
10791 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10792 if (TREE_CODE (arg0) == BIT_AND_EXPR
10793 && TREE_CODE (arg1) == BIT_AND_EXPR)
10795 tree arg00 = TREE_OPERAND (arg0, 0);
10796 tree arg01 = TREE_OPERAND (arg0, 1);
10797 tree arg10 = TREE_OPERAND (arg1, 0);
10798 tree arg11 = TREE_OPERAND (arg1, 1);
10799 tree itype = TREE_TYPE (arg0);
10801 if (operand_equal_p (arg01, arg11, 0))
10802 return fold_build2_loc (loc, code, type,
10803 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10804 fold_build2_loc (loc,
10805 BIT_XOR_EXPR, itype,
10806 arg00, arg10),
10807 arg01),
10808 build_zero_cst (itype));
10810 if (operand_equal_p (arg01, arg10, 0))
10811 return fold_build2_loc (loc, code, type,
10812 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10813 fold_build2_loc (loc,
10814 BIT_XOR_EXPR, itype,
10815 arg00, arg11),
10816 arg01),
10817 build_zero_cst (itype));
10819 if (operand_equal_p (arg00, arg11, 0))
10820 return fold_build2_loc (loc, code, type,
10821 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10822 fold_build2_loc (loc,
10823 BIT_XOR_EXPR, itype,
10824 arg01, arg10),
10825 arg00),
10826 build_zero_cst (itype));
10828 if (operand_equal_p (arg00, arg10, 0))
10829 return fold_build2_loc (loc, code, type,
10830 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10831 fold_build2_loc (loc,
10832 BIT_XOR_EXPR, itype,
10833 arg01, arg11),
10834 arg00),
10835 build_zero_cst (itype));
10838 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10839 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10841 tree arg00 = TREE_OPERAND (arg0, 0);
10842 tree arg01 = TREE_OPERAND (arg0, 1);
10843 tree arg10 = TREE_OPERAND (arg1, 0);
10844 tree arg11 = TREE_OPERAND (arg1, 1);
10845 tree itype = TREE_TYPE (arg0);
10847 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10848 operand_equal_p guarantees no side-effects so we don't need
10849 to use omit_one_operand on Z. */
10850 if (operand_equal_p (arg01, arg11, 0))
10851 return fold_build2_loc (loc, code, type, arg00,
10852 fold_convert_loc (loc, TREE_TYPE (arg00),
10853 arg10));
10854 if (operand_equal_p (arg01, arg10, 0))
10855 return fold_build2_loc (loc, code, type, arg00,
10856 fold_convert_loc (loc, TREE_TYPE (arg00),
10857 arg11));
10858 if (operand_equal_p (arg00, arg11, 0))
10859 return fold_build2_loc (loc, code, type, arg01,
10860 fold_convert_loc (loc, TREE_TYPE (arg01),
10861 arg10));
10862 if (operand_equal_p (arg00, arg10, 0))
10863 return fold_build2_loc (loc, code, type, arg01,
10864 fold_convert_loc (loc, TREE_TYPE (arg01),
10865 arg11));
10867 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10868 if (TREE_CODE (arg01) == INTEGER_CST
10869 && TREE_CODE (arg11) == INTEGER_CST)
10871 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10872 fold_convert_loc (loc, itype, arg11));
10873 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10874 return fold_build2_loc (loc, code, type, tem,
10875 fold_convert_loc (loc, itype, arg10));
10879 /* Attempt to simplify equality/inequality comparisons of complex
10880 values. Only lower the comparison if the result is known or
10881 can be simplified to a single scalar comparison. */
10882 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10883 || TREE_CODE (arg0) == COMPLEX_CST)
10884 && (TREE_CODE (arg1) == COMPLEX_EXPR
10885 || TREE_CODE (arg1) == COMPLEX_CST))
10887 tree real0, imag0, real1, imag1;
10888 tree rcond, icond;
10890 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10892 real0 = TREE_OPERAND (arg0, 0);
10893 imag0 = TREE_OPERAND (arg0, 1);
10895 else
10897 real0 = TREE_REALPART (arg0);
10898 imag0 = TREE_IMAGPART (arg0);
10901 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10903 real1 = TREE_OPERAND (arg1, 0);
10904 imag1 = TREE_OPERAND (arg1, 1);
10906 else
10908 real1 = TREE_REALPART (arg1);
10909 imag1 = TREE_IMAGPART (arg1);
10912 rcond = fold_binary_loc (loc, code, type, real0, real1);
10913 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10915 if (integer_zerop (rcond))
10917 if (code == EQ_EXPR)
10918 return omit_two_operands_loc (loc, type, boolean_false_node,
10919 imag0, imag1);
10920 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10922 else
10924 if (code == NE_EXPR)
10925 return omit_two_operands_loc (loc, type, boolean_true_node,
10926 imag0, imag1);
10927 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10931 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10932 if (icond && TREE_CODE (icond) == INTEGER_CST)
10934 if (integer_zerop (icond))
10936 if (code == EQ_EXPR)
10937 return omit_two_operands_loc (loc, type, boolean_false_node,
10938 real0, real1);
10939 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10941 else
10943 if (code == NE_EXPR)
10944 return omit_two_operands_loc (loc, type, boolean_true_node,
10945 real0, real1);
10946 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10951 return NULL_TREE;
10953 case LT_EXPR:
10954 case GT_EXPR:
10955 case LE_EXPR:
10956 case GE_EXPR:
10957 tem = fold_comparison (loc, code, type, op0, op1);
10958 if (tem != NULL_TREE)
10959 return tem;
10961 /* Transform comparisons of the form X +- C CMP X. */
10962 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10963 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10964 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10965 && !HONOR_SNANS (arg0))
10966 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10967 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10969 tree arg01 = TREE_OPERAND (arg0, 1);
10970 enum tree_code code0 = TREE_CODE (arg0);
10971 int is_positive;
10973 if (TREE_CODE (arg01) == REAL_CST)
10974 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10975 else
10976 is_positive = tree_int_cst_sgn (arg01);
10978 /* (X - c) > X becomes false. */
10979 if (code == GT_EXPR
10980 && ((code0 == MINUS_EXPR && is_positive >= 0)
10981 || (code0 == PLUS_EXPR && is_positive <= 0)))
10983 if (TREE_CODE (arg01) == INTEGER_CST
10984 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10985 fold_overflow_warning (("assuming signed overflow does not "
10986 "occur when assuming that (X - c) > X "
10987 "is always false"),
10988 WARN_STRICT_OVERFLOW_ALL);
10989 return constant_boolean_node (0, type);
10992 /* Likewise (X + c) < X becomes false. */
10993 if (code == LT_EXPR
10994 && ((code0 == PLUS_EXPR && is_positive >= 0)
10995 || (code0 == MINUS_EXPR && is_positive <= 0)))
10997 if (TREE_CODE (arg01) == INTEGER_CST
10998 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10999 fold_overflow_warning (("assuming signed overflow does not "
11000 "occur when assuming that "
11001 "(X + c) < X is always false"),
11002 WARN_STRICT_OVERFLOW_ALL);
11003 return constant_boolean_node (0, type);
11006 /* Convert (X - c) <= X to true. */
11007 if (!HONOR_NANS (arg1)
11008 && code == LE_EXPR
11009 && ((code0 == MINUS_EXPR && is_positive >= 0)
11010 || (code0 == PLUS_EXPR && is_positive <= 0)))
11012 if (TREE_CODE (arg01) == INTEGER_CST
11013 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11014 fold_overflow_warning (("assuming signed overflow does not "
11015 "occur when assuming that "
11016 "(X - c) <= X is always true"),
11017 WARN_STRICT_OVERFLOW_ALL);
11018 return constant_boolean_node (1, type);
11021 /* Convert (X + c) >= X to true. */
11022 if (!HONOR_NANS (arg1)
11023 && code == GE_EXPR
11024 && ((code0 == PLUS_EXPR && is_positive >= 0)
11025 || (code0 == MINUS_EXPR && is_positive <= 0)))
11027 if (TREE_CODE (arg01) == INTEGER_CST
11028 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11029 fold_overflow_warning (("assuming signed overflow does not "
11030 "occur when assuming that "
11031 "(X + c) >= X is always true"),
11032 WARN_STRICT_OVERFLOW_ALL);
11033 return constant_boolean_node (1, type);
11036 if (TREE_CODE (arg01) == INTEGER_CST)
11038 /* Convert X + c > X and X - c < X to true for integers. */
11039 if (code == GT_EXPR
11040 && ((code0 == PLUS_EXPR && is_positive > 0)
11041 || (code0 == MINUS_EXPR && is_positive < 0)))
11043 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11044 fold_overflow_warning (("assuming signed overflow does "
11045 "not occur when assuming that "
11046 "(X + c) > X is always true"),
11047 WARN_STRICT_OVERFLOW_ALL);
11048 return constant_boolean_node (1, type);
11051 if (code == LT_EXPR
11052 && ((code0 == MINUS_EXPR && is_positive > 0)
11053 || (code0 == PLUS_EXPR && is_positive < 0)))
11055 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11056 fold_overflow_warning (("assuming signed overflow does "
11057 "not occur when assuming that "
11058 "(X - c) < X is always true"),
11059 WARN_STRICT_OVERFLOW_ALL);
11060 return constant_boolean_node (1, type);
11063 /* Convert X + c <= X and X - c >= X to false for integers. */
11064 if (code == LE_EXPR
11065 && ((code0 == PLUS_EXPR && is_positive > 0)
11066 || (code0 == MINUS_EXPR && is_positive < 0)))
11068 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11069 fold_overflow_warning (("assuming signed overflow does "
11070 "not occur when assuming that "
11071 "(X + c) <= X is always false"),
11072 WARN_STRICT_OVERFLOW_ALL);
11073 return constant_boolean_node (0, type);
11076 if (code == GE_EXPR
11077 && ((code0 == MINUS_EXPR && is_positive > 0)
11078 || (code0 == PLUS_EXPR && is_positive < 0)))
11080 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11081 fold_overflow_warning (("assuming signed overflow does "
11082 "not occur when assuming that "
11083 "(X - c) >= X is always false"),
11084 WARN_STRICT_OVERFLOW_ALL);
11085 return constant_boolean_node (0, type);
11090 /* If we are comparing an ABS_EXPR with a constant, we can
11091 convert all the cases into explicit comparisons, but they may
11092 well not be faster than doing the ABS and one comparison.
11093 But ABS (X) <= C is a range comparison, which becomes a subtraction
11094 and a comparison, and is probably faster. */
11095 if (code == LE_EXPR
11096 && TREE_CODE (arg1) == INTEGER_CST
11097 && TREE_CODE (arg0) == ABS_EXPR
11098 && ! TREE_SIDE_EFFECTS (arg0)
11099 && (0 != (tem = negate_expr (arg1)))
11100 && TREE_CODE (tem) == INTEGER_CST
11101 && !TREE_OVERFLOW (tem))
11102 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11103 build2 (GE_EXPR, type,
11104 TREE_OPERAND (arg0, 0), tem),
11105 build2 (LE_EXPR, type,
11106 TREE_OPERAND (arg0, 0), arg1));
11108 /* Convert ABS_EXPR<x> >= 0 to true. */
11109 strict_overflow_p = false;
11110 if (code == GE_EXPR
11111 && (integer_zerop (arg1)
11112 || (! HONOR_NANS (arg0)
11113 && real_zerop (arg1)))
11114 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11116 if (strict_overflow_p)
11117 fold_overflow_warning (("assuming signed overflow does not occur "
11118 "when simplifying comparison of "
11119 "absolute value and zero"),
11120 WARN_STRICT_OVERFLOW_CONDITIONAL);
11121 return omit_one_operand_loc (loc, type,
11122 constant_boolean_node (true, type),
11123 arg0);
11126 /* Convert ABS_EXPR<x> < 0 to false. */
11127 strict_overflow_p = false;
11128 if (code == LT_EXPR
11129 && (integer_zerop (arg1) || real_zerop (arg1))
11130 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11132 if (strict_overflow_p)
11133 fold_overflow_warning (("assuming signed overflow does not occur "
11134 "when simplifying comparison of "
11135 "absolute value and zero"),
11136 WARN_STRICT_OVERFLOW_CONDITIONAL);
11137 return omit_one_operand_loc (loc, type,
11138 constant_boolean_node (false, type),
11139 arg0);
11142 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11143 and similarly for >= into !=. */
11144 if ((code == LT_EXPR || code == GE_EXPR)
11145 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11146 && TREE_CODE (arg1) == LSHIFT_EXPR
11147 && integer_onep (TREE_OPERAND (arg1, 0)))
11148 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11149 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11150 TREE_OPERAND (arg1, 1)),
11151 build_zero_cst (TREE_TYPE (arg0)));
11153 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11154 otherwise Y might be >= # of bits in X's type and thus e.g.
11155 (unsigned char) (1 << Y) for Y 15 might be 0.
11156 If the cast is widening, then 1 << Y should have unsigned type,
11157 otherwise if Y is number of bits in the signed shift type minus 1,
11158 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11159 31 might be 0xffffffff80000000. */
11160 if ((code == LT_EXPR || code == GE_EXPR)
11161 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11162 && CONVERT_EXPR_P (arg1)
11163 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11164 && (element_precision (TREE_TYPE (arg1))
11165 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11166 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11167 || (element_precision (TREE_TYPE (arg1))
11168 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11169 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11171 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11172 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11173 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11174 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11175 build_zero_cst (TREE_TYPE (arg0)));
11178 return NULL_TREE;
11180 case UNORDERED_EXPR:
11181 case ORDERED_EXPR:
11182 case UNLT_EXPR:
11183 case UNLE_EXPR:
11184 case UNGT_EXPR:
11185 case UNGE_EXPR:
11186 case UNEQ_EXPR:
11187 case LTGT_EXPR:
11188 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11190 tree targ0 = strip_float_extensions (arg0);
11191 tree targ1 = strip_float_extensions (arg1);
11192 tree newtype = TREE_TYPE (targ0);
11194 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11195 newtype = TREE_TYPE (targ1);
11197 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11198 return fold_build2_loc (loc, code, type,
11199 fold_convert_loc (loc, newtype, targ0),
11200 fold_convert_loc (loc, newtype, targ1));
11203 return NULL_TREE;
11205 case COMPOUND_EXPR:
11206 /* When pedantic, a compound expression can be neither an lvalue
11207 nor an integer constant expression. */
11208 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11209 return NULL_TREE;
11210 /* Don't let (0, 0) be null pointer constant. */
11211 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11212 : fold_convert_loc (loc, type, arg1);
11213 return pedantic_non_lvalue_loc (loc, tem);
11215 case ASSERT_EXPR:
11216 /* An ASSERT_EXPR should never be passed to fold_binary. */
11217 gcc_unreachable ();
11219 default:
11220 return NULL_TREE;
11221 } /* switch (code) */
11224 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11225 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11226 of GOTO_EXPR. */
11228 static tree
11229 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11231 switch (TREE_CODE (*tp))
11233 case LABEL_EXPR:
11234 return *tp;
11236 case GOTO_EXPR:
11237 *walk_subtrees = 0;
11239 /* fall through */
11241 default:
11242 return NULL_TREE;
11246 /* Return whether the sub-tree ST contains a label which is accessible from
11247 outside the sub-tree. */
11249 static bool
11250 contains_label_p (tree st)
11252 return
11253 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11256 /* Fold a ternary expression of code CODE and type TYPE with operands
11257 OP0, OP1, and OP2. Return the folded expression if folding is
11258 successful. Otherwise, return NULL_TREE. */
11260 tree
11261 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11262 tree op0, tree op1, tree op2)
11264 tree tem;
11265 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11266 enum tree_code_class kind = TREE_CODE_CLASS (code);
11268 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11269 && TREE_CODE_LENGTH (code) == 3);
11271 /* If this is a commutative operation, and OP0 is a constant, move it
11272 to OP1 to reduce the number of tests below. */
11273 if (commutative_ternary_tree_code (code)
11274 && tree_swap_operands_p (op0, op1, true))
11275 return fold_build3_loc (loc, code, type, op1, op0, op2);
11277 tem = generic_simplify (loc, code, type, op0, op1, op2);
11278 if (tem)
11279 return tem;
11281 /* Strip any conversions that don't change the mode. This is safe
11282 for every expression, except for a comparison expression because
11283 its signedness is derived from its operands. So, in the latter
11284 case, only strip conversions that don't change the signedness.
11286 Note that this is done as an internal manipulation within the
11287 constant folder, in order to find the simplest representation of
11288 the arguments so that their form can be studied. In any cases,
11289 the appropriate type conversions should be put back in the tree
11290 that will get out of the constant folder. */
11291 if (op0)
11293 arg0 = op0;
11294 STRIP_NOPS (arg0);
11297 if (op1)
11299 arg1 = op1;
11300 STRIP_NOPS (arg1);
11303 if (op2)
11305 arg2 = op2;
11306 STRIP_NOPS (arg2);
11309 switch (code)
11311 case COMPONENT_REF:
11312 if (TREE_CODE (arg0) == CONSTRUCTOR
11313 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11315 unsigned HOST_WIDE_INT idx;
11316 tree field, value;
11317 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11318 if (field == arg1)
11319 return value;
11321 return NULL_TREE;
11323 case COND_EXPR:
11324 case VEC_COND_EXPR:
11325 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11326 so all simple results must be passed through pedantic_non_lvalue. */
11327 if (TREE_CODE (arg0) == INTEGER_CST)
11329 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11330 tem = integer_zerop (arg0) ? op2 : op1;
11331 /* Only optimize constant conditions when the selected branch
11332 has the same type as the COND_EXPR. This avoids optimizing
11333 away "c ? x : throw", where the throw has a void type.
11334 Avoid throwing away that operand which contains label. */
11335 if ((!TREE_SIDE_EFFECTS (unused_op)
11336 || !contains_label_p (unused_op))
11337 && (! VOID_TYPE_P (TREE_TYPE (tem))
11338 || VOID_TYPE_P (type)))
11339 return pedantic_non_lvalue_loc (loc, tem);
11340 return NULL_TREE;
11342 else if (TREE_CODE (arg0) == VECTOR_CST)
11344 if ((TREE_CODE (arg1) == VECTOR_CST
11345 || TREE_CODE (arg1) == CONSTRUCTOR)
11346 && (TREE_CODE (arg2) == VECTOR_CST
11347 || TREE_CODE (arg2) == CONSTRUCTOR))
11349 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11350 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11351 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11352 for (i = 0; i < nelts; i++)
11354 tree val = VECTOR_CST_ELT (arg0, i);
11355 if (integer_all_onesp (val))
11356 sel[i] = i;
11357 else if (integer_zerop (val))
11358 sel[i] = nelts + i;
11359 else /* Currently unreachable. */
11360 return NULL_TREE;
11362 tree t = fold_vec_perm (type, arg1, arg2, sel);
11363 if (t != NULL_TREE)
11364 return t;
11368 /* If we have A op B ? A : C, we may be able to convert this to a
11369 simpler expression, depending on the operation and the values
11370 of B and C. Signed zeros prevent all of these transformations,
11371 for reasons given above each one.
11373 Also try swapping the arguments and inverting the conditional. */
11374 if (COMPARISON_CLASS_P (arg0)
11375 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11376 arg1, TREE_OPERAND (arg0, 1))
11377 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11379 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11380 if (tem)
11381 return tem;
11384 if (COMPARISON_CLASS_P (arg0)
11385 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11386 op2,
11387 TREE_OPERAND (arg0, 1))
11388 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11390 location_t loc0 = expr_location_or (arg0, loc);
11391 tem = fold_invert_truthvalue (loc0, arg0);
11392 if (tem && COMPARISON_CLASS_P (tem))
11394 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11395 if (tem)
11396 return tem;
11400 /* If the second operand is simpler than the third, swap them
11401 since that produces better jump optimization results. */
11402 if (truth_value_p (TREE_CODE (arg0))
11403 && tree_swap_operands_p (op1, op2, false))
11405 location_t loc0 = expr_location_or (arg0, loc);
11406 /* See if this can be inverted. If it can't, possibly because
11407 it was a floating-point inequality comparison, don't do
11408 anything. */
11409 tem = fold_invert_truthvalue (loc0, arg0);
11410 if (tem)
11411 return fold_build3_loc (loc, code, type, tem, op2, op1);
11414 /* Convert A ? 1 : 0 to simply A. */
11415 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11416 : (integer_onep (op1)
11417 && !VECTOR_TYPE_P (type)))
11418 && integer_zerop (op2)
11419 /* If we try to convert OP0 to our type, the
11420 call to fold will try to move the conversion inside
11421 a COND, which will recurse. In that case, the COND_EXPR
11422 is probably the best choice, so leave it alone. */
11423 && type == TREE_TYPE (arg0))
11424 return pedantic_non_lvalue_loc (loc, arg0);
11426 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11427 over COND_EXPR in cases such as floating point comparisons. */
11428 if (integer_zerop (op1)
11429 && code == COND_EXPR
11430 && integer_onep (op2)
11431 && !VECTOR_TYPE_P (type)
11432 && truth_value_p (TREE_CODE (arg0)))
11433 return pedantic_non_lvalue_loc (loc,
11434 fold_convert_loc (loc, type,
11435 invert_truthvalue_loc (loc,
11436 arg0)));
11438 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11439 if (TREE_CODE (arg0) == LT_EXPR
11440 && integer_zerop (TREE_OPERAND (arg0, 1))
11441 && integer_zerop (op2)
11442 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11444 /* sign_bit_p looks through both zero and sign extensions,
11445 but for this optimization only sign extensions are
11446 usable. */
11447 tree tem2 = TREE_OPERAND (arg0, 0);
11448 while (tem != tem2)
11450 if (TREE_CODE (tem2) != NOP_EXPR
11451 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11453 tem = NULL_TREE;
11454 break;
11456 tem2 = TREE_OPERAND (tem2, 0);
11458 /* sign_bit_p only checks ARG1 bits within A's precision.
11459 If <sign bit of A> has wider type than A, bits outside
11460 of A's precision in <sign bit of A> need to be checked.
11461 If they are all 0, this optimization needs to be done
11462 in unsigned A's type, if they are all 1 in signed A's type,
11463 otherwise this can't be done. */
11464 if (tem
11465 && TYPE_PRECISION (TREE_TYPE (tem))
11466 < TYPE_PRECISION (TREE_TYPE (arg1))
11467 && TYPE_PRECISION (TREE_TYPE (tem))
11468 < TYPE_PRECISION (type))
11470 int inner_width, outer_width;
11471 tree tem_type;
11473 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11474 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11475 if (outer_width > TYPE_PRECISION (type))
11476 outer_width = TYPE_PRECISION (type);
11478 wide_int mask = wi::shifted_mask
11479 (inner_width, outer_width - inner_width, false,
11480 TYPE_PRECISION (TREE_TYPE (arg1)));
11482 wide_int common = mask & arg1;
11483 if (common == mask)
11485 tem_type = signed_type_for (TREE_TYPE (tem));
11486 tem = fold_convert_loc (loc, tem_type, tem);
11488 else if (common == 0)
11490 tem_type = unsigned_type_for (TREE_TYPE (tem));
11491 tem = fold_convert_loc (loc, tem_type, tem);
11493 else
11494 tem = NULL;
11497 if (tem)
11498 return
11499 fold_convert_loc (loc, type,
11500 fold_build2_loc (loc, BIT_AND_EXPR,
11501 TREE_TYPE (tem), tem,
11502 fold_convert_loc (loc,
11503 TREE_TYPE (tem),
11504 arg1)));
11507 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11508 already handled above. */
11509 if (TREE_CODE (arg0) == BIT_AND_EXPR
11510 && integer_onep (TREE_OPERAND (arg0, 1))
11511 && integer_zerop (op2)
11512 && integer_pow2p (arg1))
11514 tree tem = TREE_OPERAND (arg0, 0);
11515 STRIP_NOPS (tem);
11516 if (TREE_CODE (tem) == RSHIFT_EXPR
11517 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11518 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11519 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11520 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11521 TREE_OPERAND (tem, 0), arg1);
11524 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11525 is probably obsolete because the first operand should be a
11526 truth value (that's why we have the two cases above), but let's
11527 leave it in until we can confirm this for all front-ends. */
11528 if (integer_zerop (op2)
11529 && TREE_CODE (arg0) == NE_EXPR
11530 && integer_zerop (TREE_OPERAND (arg0, 1))
11531 && integer_pow2p (arg1)
11532 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11533 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11534 arg1, OEP_ONLY_CONST))
11535 return pedantic_non_lvalue_loc (loc,
11536 fold_convert_loc (loc, type,
11537 TREE_OPERAND (arg0, 0)));
11539 /* Disable the transformations below for vectors, since
11540 fold_binary_op_with_conditional_arg may undo them immediately,
11541 yielding an infinite loop. */
11542 if (code == VEC_COND_EXPR)
11543 return NULL_TREE;
11545 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11546 if (integer_zerop (op2)
11547 && truth_value_p (TREE_CODE (arg0))
11548 && truth_value_p (TREE_CODE (arg1))
11549 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11550 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11551 : TRUTH_ANDIF_EXPR,
11552 type, fold_convert_loc (loc, type, arg0), arg1);
11554 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11555 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11556 && truth_value_p (TREE_CODE (arg0))
11557 && truth_value_p (TREE_CODE (arg1))
11558 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11560 location_t loc0 = expr_location_or (arg0, loc);
11561 /* Only perform transformation if ARG0 is easily inverted. */
11562 tem = fold_invert_truthvalue (loc0, arg0);
11563 if (tem)
11564 return fold_build2_loc (loc, code == VEC_COND_EXPR
11565 ? BIT_IOR_EXPR
11566 : TRUTH_ORIF_EXPR,
11567 type, fold_convert_loc (loc, type, tem),
11568 arg1);
11571 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11572 if (integer_zerop (arg1)
11573 && truth_value_p (TREE_CODE (arg0))
11574 && truth_value_p (TREE_CODE (op2))
11575 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11577 location_t loc0 = expr_location_or (arg0, loc);
11578 /* Only perform transformation if ARG0 is easily inverted. */
11579 tem = fold_invert_truthvalue (loc0, arg0);
11580 if (tem)
11581 return fold_build2_loc (loc, code == VEC_COND_EXPR
11582 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11583 type, fold_convert_loc (loc, type, tem),
11584 op2);
11587 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11588 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11589 && truth_value_p (TREE_CODE (arg0))
11590 && truth_value_p (TREE_CODE (op2))
11591 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11592 return fold_build2_loc (loc, code == VEC_COND_EXPR
11593 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11594 type, fold_convert_loc (loc, type, arg0), op2);
11596 return NULL_TREE;
11598 case CALL_EXPR:
11599 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11600 of fold_ternary on them. */
11601 gcc_unreachable ();
11603 case BIT_FIELD_REF:
11604 if (TREE_CODE (arg0) == VECTOR_CST
11605 && (type == TREE_TYPE (TREE_TYPE (arg0))
11606 || (TREE_CODE (type) == VECTOR_TYPE
11607 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11609 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11610 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11611 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11612 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11614 if (n != 0
11615 && (idx % width) == 0
11616 && (n % width) == 0
11617 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11619 idx = idx / width;
11620 n = n / width;
11622 if (TREE_CODE (arg0) == VECTOR_CST)
11624 if (n == 1)
11625 return VECTOR_CST_ELT (arg0, idx);
11627 tree *vals = XALLOCAVEC (tree, n);
11628 for (unsigned i = 0; i < n; ++i)
11629 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11630 return build_vector (type, vals);
11635 /* On constants we can use native encode/interpret to constant
11636 fold (nearly) all BIT_FIELD_REFs. */
11637 if (CONSTANT_CLASS_P (arg0)
11638 && can_native_interpret_type_p (type)
11639 && BITS_PER_UNIT == 8)
11641 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11642 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11643 /* Limit us to a reasonable amount of work. To relax the
11644 other limitations we need bit-shifting of the buffer
11645 and rounding up the size. */
11646 if (bitpos % BITS_PER_UNIT == 0
11647 && bitsize % BITS_PER_UNIT == 0
11648 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11650 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11651 unsigned HOST_WIDE_INT len
11652 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11653 bitpos / BITS_PER_UNIT);
11654 if (len > 0
11655 && len * BITS_PER_UNIT >= bitsize)
11657 tree v = native_interpret_expr (type, b,
11658 bitsize / BITS_PER_UNIT);
11659 if (v)
11660 return v;
11665 return NULL_TREE;
11667 case FMA_EXPR:
11668 /* For integers we can decompose the FMA if possible. */
11669 if (TREE_CODE (arg0) == INTEGER_CST
11670 && TREE_CODE (arg1) == INTEGER_CST)
11671 return fold_build2_loc (loc, PLUS_EXPR, type,
11672 const_binop (MULT_EXPR, arg0, arg1), arg2);
11673 if (integer_zerop (arg2))
11674 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11676 return fold_fma (loc, type, arg0, arg1, arg2);
11678 case VEC_PERM_EXPR:
11679 if (TREE_CODE (arg2) == VECTOR_CST)
11681 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11682 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11683 unsigned char *sel2 = sel + nelts;
11684 bool need_mask_canon = false;
11685 bool need_mask_canon2 = false;
11686 bool all_in_vec0 = true;
11687 bool all_in_vec1 = true;
11688 bool maybe_identity = true;
11689 bool single_arg = (op0 == op1);
11690 bool changed = false;
11692 mask2 = 2 * nelts - 1;
11693 mask = single_arg ? (nelts - 1) : mask2;
11694 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11695 for (i = 0; i < nelts; i++)
11697 tree val = VECTOR_CST_ELT (arg2, i);
11698 if (TREE_CODE (val) != INTEGER_CST)
11699 return NULL_TREE;
11701 /* Make sure that the perm value is in an acceptable
11702 range. */
11703 wide_int t = val;
11704 need_mask_canon |= wi::gtu_p (t, mask);
11705 need_mask_canon2 |= wi::gtu_p (t, mask2);
11706 sel[i] = t.to_uhwi () & mask;
11707 sel2[i] = t.to_uhwi () & mask2;
11709 if (sel[i] < nelts)
11710 all_in_vec1 = false;
11711 else
11712 all_in_vec0 = false;
11714 if ((sel[i] & (nelts-1)) != i)
11715 maybe_identity = false;
11718 if (maybe_identity)
11720 if (all_in_vec0)
11721 return op0;
11722 if (all_in_vec1)
11723 return op1;
11726 if (all_in_vec0)
11727 op1 = op0;
11728 else if (all_in_vec1)
11730 op0 = op1;
11731 for (i = 0; i < nelts; i++)
11732 sel[i] -= nelts;
11733 need_mask_canon = true;
11736 if ((TREE_CODE (op0) == VECTOR_CST
11737 || TREE_CODE (op0) == CONSTRUCTOR)
11738 && (TREE_CODE (op1) == VECTOR_CST
11739 || TREE_CODE (op1) == CONSTRUCTOR))
11741 tree t = fold_vec_perm (type, op0, op1, sel);
11742 if (t != NULL_TREE)
11743 return t;
11746 if (op0 == op1 && !single_arg)
11747 changed = true;
11749 /* Some targets are deficient and fail to expand a single
11750 argument permutation while still allowing an equivalent
11751 2-argument version. */
11752 if (need_mask_canon && arg2 == op2
11753 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11754 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11756 need_mask_canon = need_mask_canon2;
11757 sel = sel2;
11760 if (need_mask_canon && arg2 == op2)
11762 tree *tsel = XALLOCAVEC (tree, nelts);
11763 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11764 for (i = 0; i < nelts; i++)
11765 tsel[i] = build_int_cst (eltype, sel[i]);
11766 op2 = build_vector (TREE_TYPE (arg2), tsel);
11767 changed = true;
11770 if (changed)
11771 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11773 return NULL_TREE;
11775 case BIT_INSERT_EXPR:
11776 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11777 if (TREE_CODE (arg0) == INTEGER_CST
11778 && TREE_CODE (arg1) == INTEGER_CST)
11780 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11781 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11782 wide_int tem = wi::bit_and (arg0,
11783 wi::shifted_mask (bitpos, bitsize, true,
11784 TYPE_PRECISION (type)));
11785 wide_int tem2
11786 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11787 bitsize), bitpos);
11788 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11790 else if (TREE_CODE (arg0) == VECTOR_CST
11791 && CONSTANT_CLASS_P (arg1)
11792 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11793 TREE_TYPE (arg1)))
11795 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11796 unsigned HOST_WIDE_INT elsize
11797 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11798 if (bitpos % elsize == 0)
11800 unsigned k = bitpos / elsize;
11801 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11802 return arg0;
11803 else
11805 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11806 memcpy (elts, VECTOR_CST_ELTS (arg0),
11807 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11808 elts[k] = arg1;
11809 return build_vector (type, elts);
11813 return NULL_TREE;
11815 default:
11816 return NULL_TREE;
11817 } /* switch (code) */
11820 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11821 of an array (or vector). */
11823 tree
11824 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11826 tree index_type = NULL_TREE;
11827 offset_int low_bound = 0;
11829 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11831 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11832 if (domain_type && TYPE_MIN_VALUE (domain_type))
11834 /* Static constructors for variably sized objects makes no sense. */
11835 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11836 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11837 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11841 if (index_type)
11842 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11843 TYPE_SIGN (index_type));
11845 offset_int index = low_bound - 1;
11846 if (index_type)
11847 index = wi::ext (index, TYPE_PRECISION (index_type),
11848 TYPE_SIGN (index_type));
11850 offset_int max_index;
11851 unsigned HOST_WIDE_INT cnt;
11852 tree cfield, cval;
11854 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11856 /* Array constructor might explicitly set index, or specify a range,
11857 or leave index NULL meaning that it is next index after previous
11858 one. */
11859 if (cfield)
11861 if (TREE_CODE (cfield) == INTEGER_CST)
11862 max_index = index = wi::to_offset (cfield);
11863 else
11865 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11866 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11867 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11870 else
11872 index += 1;
11873 if (index_type)
11874 index = wi::ext (index, TYPE_PRECISION (index_type),
11875 TYPE_SIGN (index_type));
11876 max_index = index;
11879 /* Do we have match? */
11880 if (wi::cmpu (access_index, index) >= 0
11881 && wi::cmpu (access_index, max_index) <= 0)
11882 return cval;
11884 return NULL_TREE;
11887 /* Perform constant folding and related simplification of EXPR.
11888 The related simplifications include x*1 => x, x*0 => 0, etc.,
11889 and application of the associative law.
11890 NOP_EXPR conversions may be removed freely (as long as we
11891 are careful not to change the type of the overall expression).
11892 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11893 but we can constant-fold them if they have constant operands. */
11895 #ifdef ENABLE_FOLD_CHECKING
11896 # define fold(x) fold_1 (x)
11897 static tree fold_1 (tree);
11898 static
11899 #endif
11900 tree
11901 fold (tree expr)
11903 const tree t = expr;
11904 enum tree_code code = TREE_CODE (t);
11905 enum tree_code_class kind = TREE_CODE_CLASS (code);
11906 tree tem;
11907 location_t loc = EXPR_LOCATION (expr);
11909 /* Return right away if a constant. */
11910 if (kind == tcc_constant)
11911 return t;
11913 /* CALL_EXPR-like objects with variable numbers of operands are
11914 treated specially. */
11915 if (kind == tcc_vl_exp)
11917 if (code == CALL_EXPR)
11919 tem = fold_call_expr (loc, expr, false);
11920 return tem ? tem : expr;
11922 return expr;
11925 if (IS_EXPR_CODE_CLASS (kind))
11927 tree type = TREE_TYPE (t);
11928 tree op0, op1, op2;
11930 switch (TREE_CODE_LENGTH (code))
11932 case 1:
11933 op0 = TREE_OPERAND (t, 0);
11934 tem = fold_unary_loc (loc, code, type, op0);
11935 return tem ? tem : expr;
11936 case 2:
11937 op0 = TREE_OPERAND (t, 0);
11938 op1 = TREE_OPERAND (t, 1);
11939 tem = fold_binary_loc (loc, code, type, op0, op1);
11940 return tem ? tem : expr;
11941 case 3:
11942 op0 = TREE_OPERAND (t, 0);
11943 op1 = TREE_OPERAND (t, 1);
11944 op2 = TREE_OPERAND (t, 2);
11945 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11946 return tem ? tem : expr;
11947 default:
11948 break;
11952 switch (code)
11954 case ARRAY_REF:
11956 tree op0 = TREE_OPERAND (t, 0);
11957 tree op1 = TREE_OPERAND (t, 1);
11959 if (TREE_CODE (op1) == INTEGER_CST
11960 && TREE_CODE (op0) == CONSTRUCTOR
11961 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11963 tree val = get_array_ctor_element_at_index (op0,
11964 wi::to_offset (op1));
11965 if (val)
11966 return val;
11969 return t;
11972 /* Return a VECTOR_CST if possible. */
11973 case CONSTRUCTOR:
11975 tree type = TREE_TYPE (t);
11976 if (TREE_CODE (type) != VECTOR_TYPE)
11977 return t;
11979 unsigned i;
11980 tree val;
11981 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11982 if (! CONSTANT_CLASS_P (val))
11983 return t;
11985 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11988 case CONST_DECL:
11989 return fold (DECL_INITIAL (t));
11991 default:
11992 return t;
11993 } /* switch (code) */
11996 #ifdef ENABLE_FOLD_CHECKING
11997 #undef fold
11999 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12000 hash_table<nofree_ptr_hash<const tree_node> > *);
12001 static void fold_check_failed (const_tree, const_tree);
12002 void print_fold_checksum (const_tree);
12004 /* When --enable-checking=fold, compute a digest of expr before
12005 and after actual fold call to see if fold did not accidentally
12006 change original expr. */
12008 tree
12009 fold (tree expr)
12011 tree ret;
12012 struct md5_ctx ctx;
12013 unsigned char checksum_before[16], checksum_after[16];
12014 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12016 md5_init_ctx (&ctx);
12017 fold_checksum_tree (expr, &ctx, &ht);
12018 md5_finish_ctx (&ctx, checksum_before);
12019 ht.empty ();
12021 ret = fold_1 (expr);
12023 md5_init_ctx (&ctx);
12024 fold_checksum_tree (expr, &ctx, &ht);
12025 md5_finish_ctx (&ctx, checksum_after);
12027 if (memcmp (checksum_before, checksum_after, 16))
12028 fold_check_failed (expr, ret);
12030 return ret;
12033 void
12034 print_fold_checksum (const_tree expr)
12036 struct md5_ctx ctx;
12037 unsigned char checksum[16], cnt;
12038 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12040 md5_init_ctx (&ctx);
12041 fold_checksum_tree (expr, &ctx, &ht);
12042 md5_finish_ctx (&ctx, checksum);
12043 for (cnt = 0; cnt < 16; ++cnt)
12044 fprintf (stderr, "%02x", checksum[cnt]);
12045 putc ('\n', stderr);
12048 static void
12049 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12051 internal_error ("fold check: original tree changed by fold");
12054 static void
12055 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12056 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12058 const tree_node **slot;
12059 enum tree_code code;
12060 union tree_node buf;
12061 int i, len;
12063 recursive_label:
12064 if (expr == NULL)
12065 return;
12066 slot = ht->find_slot (expr, INSERT);
12067 if (*slot != NULL)
12068 return;
12069 *slot = expr;
12070 code = TREE_CODE (expr);
12071 if (TREE_CODE_CLASS (code) == tcc_declaration
12072 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12074 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12075 memcpy ((char *) &buf, expr, tree_size (expr));
12076 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12077 buf.decl_with_vis.symtab_node = NULL;
12078 expr = (tree) &buf;
12080 else if (TREE_CODE_CLASS (code) == tcc_type
12081 && (TYPE_POINTER_TO (expr)
12082 || TYPE_REFERENCE_TO (expr)
12083 || TYPE_CACHED_VALUES_P (expr)
12084 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12085 || TYPE_NEXT_VARIANT (expr)
12086 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12088 /* Allow these fields to be modified. */
12089 tree tmp;
12090 memcpy ((char *) &buf, expr, tree_size (expr));
12091 expr = tmp = (tree) &buf;
12092 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12093 TYPE_POINTER_TO (tmp) = NULL;
12094 TYPE_REFERENCE_TO (tmp) = NULL;
12095 TYPE_NEXT_VARIANT (tmp) = NULL;
12096 TYPE_ALIAS_SET (tmp) = -1;
12097 if (TYPE_CACHED_VALUES_P (tmp))
12099 TYPE_CACHED_VALUES_P (tmp) = 0;
12100 TYPE_CACHED_VALUES (tmp) = NULL;
12103 md5_process_bytes (expr, tree_size (expr), ctx);
12104 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12105 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12106 if (TREE_CODE_CLASS (code) != tcc_type
12107 && TREE_CODE_CLASS (code) != tcc_declaration
12108 && code != TREE_LIST
12109 && code != SSA_NAME
12110 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12111 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12112 switch (TREE_CODE_CLASS (code))
12114 case tcc_constant:
12115 switch (code)
12117 case STRING_CST:
12118 md5_process_bytes (TREE_STRING_POINTER (expr),
12119 TREE_STRING_LENGTH (expr), ctx);
12120 break;
12121 case COMPLEX_CST:
12122 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12123 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12124 break;
12125 case VECTOR_CST:
12126 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12127 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12128 break;
12129 default:
12130 break;
12132 break;
12133 case tcc_exceptional:
12134 switch (code)
12136 case TREE_LIST:
12137 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12138 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12139 expr = TREE_CHAIN (expr);
12140 goto recursive_label;
12141 break;
12142 case TREE_VEC:
12143 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12144 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12145 break;
12146 default:
12147 break;
12149 break;
12150 case tcc_expression:
12151 case tcc_reference:
12152 case tcc_comparison:
12153 case tcc_unary:
12154 case tcc_binary:
12155 case tcc_statement:
12156 case tcc_vl_exp:
12157 len = TREE_OPERAND_LENGTH (expr);
12158 for (i = 0; i < len; ++i)
12159 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12160 break;
12161 case tcc_declaration:
12162 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12163 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12164 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12166 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12167 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12168 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12169 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12170 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12173 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12175 if (TREE_CODE (expr) == FUNCTION_DECL)
12177 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12178 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12180 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12182 break;
12183 case tcc_type:
12184 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12185 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12186 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12187 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12188 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12189 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12190 if (INTEGRAL_TYPE_P (expr)
12191 || SCALAR_FLOAT_TYPE_P (expr))
12193 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12194 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12196 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12197 if (TREE_CODE (expr) == RECORD_TYPE
12198 || TREE_CODE (expr) == UNION_TYPE
12199 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12200 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12201 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12202 break;
12203 default:
12204 break;
12208 /* Helper function for outputting the checksum of a tree T. When
12209 debugging with gdb, you can "define mynext" to be "next" followed
12210 by "call debug_fold_checksum (op0)", then just trace down till the
12211 outputs differ. */
12213 DEBUG_FUNCTION void
12214 debug_fold_checksum (const_tree t)
12216 int i;
12217 unsigned char checksum[16];
12218 struct md5_ctx ctx;
12219 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12221 md5_init_ctx (&ctx);
12222 fold_checksum_tree (t, &ctx, &ht);
12223 md5_finish_ctx (&ctx, checksum);
12224 ht.empty ();
12226 for (i = 0; i < 16; i++)
12227 fprintf (stderr, "%d ", checksum[i]);
12229 fprintf (stderr, "\n");
12232 #endif
12234 /* Fold a unary tree expression with code CODE of type TYPE with an
12235 operand OP0. LOC is the location of the resulting expression.
12236 Return a folded expression if successful. Otherwise, return a tree
12237 expression with code CODE of type TYPE with an operand OP0. */
12239 tree
12240 fold_build1_stat_loc (location_t loc,
12241 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12243 tree tem;
12244 #ifdef ENABLE_FOLD_CHECKING
12245 unsigned char checksum_before[16], checksum_after[16];
12246 struct md5_ctx ctx;
12247 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12249 md5_init_ctx (&ctx);
12250 fold_checksum_tree (op0, &ctx, &ht);
12251 md5_finish_ctx (&ctx, checksum_before);
12252 ht.empty ();
12253 #endif
12255 tem = fold_unary_loc (loc, code, type, op0);
12256 if (!tem)
12257 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12259 #ifdef ENABLE_FOLD_CHECKING
12260 md5_init_ctx (&ctx);
12261 fold_checksum_tree (op0, &ctx, &ht);
12262 md5_finish_ctx (&ctx, checksum_after);
12264 if (memcmp (checksum_before, checksum_after, 16))
12265 fold_check_failed (op0, tem);
12266 #endif
12267 return tem;
12270 /* Fold a binary tree expression with code CODE of type TYPE with
12271 operands OP0 and OP1. LOC is the location of the resulting
12272 expression. Return a folded expression if successful. Otherwise,
12273 return a tree expression with code CODE of type TYPE with operands
12274 OP0 and OP1. */
12276 tree
12277 fold_build2_stat_loc (location_t loc,
12278 enum tree_code code, tree type, tree op0, tree op1
12279 MEM_STAT_DECL)
12281 tree tem;
12282 #ifdef ENABLE_FOLD_CHECKING
12283 unsigned char checksum_before_op0[16],
12284 checksum_before_op1[16],
12285 checksum_after_op0[16],
12286 checksum_after_op1[16];
12287 struct md5_ctx ctx;
12288 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12290 md5_init_ctx (&ctx);
12291 fold_checksum_tree (op0, &ctx, &ht);
12292 md5_finish_ctx (&ctx, checksum_before_op0);
12293 ht.empty ();
12295 md5_init_ctx (&ctx);
12296 fold_checksum_tree (op1, &ctx, &ht);
12297 md5_finish_ctx (&ctx, checksum_before_op1);
12298 ht.empty ();
12299 #endif
12301 tem = fold_binary_loc (loc, code, type, op0, op1);
12302 if (!tem)
12303 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12305 #ifdef ENABLE_FOLD_CHECKING
12306 md5_init_ctx (&ctx);
12307 fold_checksum_tree (op0, &ctx, &ht);
12308 md5_finish_ctx (&ctx, checksum_after_op0);
12309 ht.empty ();
12311 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12312 fold_check_failed (op0, tem);
12314 md5_init_ctx (&ctx);
12315 fold_checksum_tree (op1, &ctx, &ht);
12316 md5_finish_ctx (&ctx, checksum_after_op1);
12318 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12319 fold_check_failed (op1, tem);
12320 #endif
12321 return tem;
12324 /* Fold a ternary tree expression with code CODE of type TYPE with
12325 operands OP0, OP1, and OP2. Return a folded expression if
12326 successful. Otherwise, return a tree expression with code CODE of
12327 type TYPE with operands OP0, OP1, and OP2. */
12329 tree
12330 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12331 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12333 tree tem;
12334 #ifdef ENABLE_FOLD_CHECKING
12335 unsigned char checksum_before_op0[16],
12336 checksum_before_op1[16],
12337 checksum_before_op2[16],
12338 checksum_after_op0[16],
12339 checksum_after_op1[16],
12340 checksum_after_op2[16];
12341 struct md5_ctx ctx;
12342 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12344 md5_init_ctx (&ctx);
12345 fold_checksum_tree (op0, &ctx, &ht);
12346 md5_finish_ctx (&ctx, checksum_before_op0);
12347 ht.empty ();
12349 md5_init_ctx (&ctx);
12350 fold_checksum_tree (op1, &ctx, &ht);
12351 md5_finish_ctx (&ctx, checksum_before_op1);
12352 ht.empty ();
12354 md5_init_ctx (&ctx);
12355 fold_checksum_tree (op2, &ctx, &ht);
12356 md5_finish_ctx (&ctx, checksum_before_op2);
12357 ht.empty ();
12358 #endif
12360 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12361 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12362 if (!tem)
12363 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12365 #ifdef ENABLE_FOLD_CHECKING
12366 md5_init_ctx (&ctx);
12367 fold_checksum_tree (op0, &ctx, &ht);
12368 md5_finish_ctx (&ctx, checksum_after_op0);
12369 ht.empty ();
12371 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12372 fold_check_failed (op0, tem);
12374 md5_init_ctx (&ctx);
12375 fold_checksum_tree (op1, &ctx, &ht);
12376 md5_finish_ctx (&ctx, checksum_after_op1);
12377 ht.empty ();
12379 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12380 fold_check_failed (op1, tem);
12382 md5_init_ctx (&ctx);
12383 fold_checksum_tree (op2, &ctx, &ht);
12384 md5_finish_ctx (&ctx, checksum_after_op2);
12386 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12387 fold_check_failed (op2, tem);
12388 #endif
12389 return tem;
12392 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12393 arguments in ARGARRAY, and a null static chain.
12394 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12395 of type TYPE from the given operands as constructed by build_call_array. */
12397 tree
12398 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12399 int nargs, tree *argarray)
12401 tree tem;
12402 #ifdef ENABLE_FOLD_CHECKING
12403 unsigned char checksum_before_fn[16],
12404 checksum_before_arglist[16],
12405 checksum_after_fn[16],
12406 checksum_after_arglist[16];
12407 struct md5_ctx ctx;
12408 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12409 int i;
12411 md5_init_ctx (&ctx);
12412 fold_checksum_tree (fn, &ctx, &ht);
12413 md5_finish_ctx (&ctx, checksum_before_fn);
12414 ht.empty ();
12416 md5_init_ctx (&ctx);
12417 for (i = 0; i < nargs; i++)
12418 fold_checksum_tree (argarray[i], &ctx, &ht);
12419 md5_finish_ctx (&ctx, checksum_before_arglist);
12420 ht.empty ();
12421 #endif
12423 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12424 if (!tem)
12425 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12427 #ifdef ENABLE_FOLD_CHECKING
12428 md5_init_ctx (&ctx);
12429 fold_checksum_tree (fn, &ctx, &ht);
12430 md5_finish_ctx (&ctx, checksum_after_fn);
12431 ht.empty ();
12433 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12434 fold_check_failed (fn, tem);
12436 md5_init_ctx (&ctx);
12437 for (i = 0; i < nargs; i++)
12438 fold_checksum_tree (argarray[i], &ctx, &ht);
12439 md5_finish_ctx (&ctx, checksum_after_arglist);
12441 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12442 fold_check_failed (NULL_TREE, tem);
12443 #endif
12444 return tem;
12447 /* Perform constant folding and related simplification of initializer
12448 expression EXPR. These behave identically to "fold_buildN" but ignore
12449 potential run-time traps and exceptions that fold must preserve. */
12451 #define START_FOLD_INIT \
12452 int saved_signaling_nans = flag_signaling_nans;\
12453 int saved_trapping_math = flag_trapping_math;\
12454 int saved_rounding_math = flag_rounding_math;\
12455 int saved_trapv = flag_trapv;\
12456 int saved_folding_initializer = folding_initializer;\
12457 flag_signaling_nans = 0;\
12458 flag_trapping_math = 0;\
12459 flag_rounding_math = 0;\
12460 flag_trapv = 0;\
12461 folding_initializer = 1;
12463 #define END_FOLD_INIT \
12464 flag_signaling_nans = saved_signaling_nans;\
12465 flag_trapping_math = saved_trapping_math;\
12466 flag_rounding_math = saved_rounding_math;\
12467 flag_trapv = saved_trapv;\
12468 folding_initializer = saved_folding_initializer;
12470 tree
12471 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12472 tree type, tree op)
12474 tree result;
12475 START_FOLD_INIT;
12477 result = fold_build1_loc (loc, code, type, op);
12479 END_FOLD_INIT;
12480 return result;
12483 tree
12484 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12485 tree type, tree op0, tree op1)
12487 tree result;
12488 START_FOLD_INIT;
12490 result = fold_build2_loc (loc, code, type, op0, op1);
12492 END_FOLD_INIT;
12493 return result;
12496 tree
12497 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12498 int nargs, tree *argarray)
12500 tree result;
12501 START_FOLD_INIT;
12503 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12505 END_FOLD_INIT;
12506 return result;
12509 #undef START_FOLD_INIT
12510 #undef END_FOLD_INIT
12512 /* Determine if first argument is a multiple of second argument. Return 0 if
12513 it is not, or we cannot easily determined it to be.
12515 An example of the sort of thing we care about (at this point; this routine
12516 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12517 fold cases do now) is discovering that
12519 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12521 is a multiple of
12523 SAVE_EXPR (J * 8)
12525 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12527 This code also handles discovering that
12529 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12531 is a multiple of 8 so we don't have to worry about dealing with a
12532 possible remainder.
12534 Note that we *look* inside a SAVE_EXPR only to determine how it was
12535 calculated; it is not safe for fold to do much of anything else with the
12536 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12537 at run time. For example, the latter example above *cannot* be implemented
12538 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12539 evaluation time of the original SAVE_EXPR is not necessarily the same at
12540 the time the new expression is evaluated. The only optimization of this
12541 sort that would be valid is changing
12543 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12545 divided by 8 to
12547 SAVE_EXPR (I) * SAVE_EXPR (J)
12549 (where the same SAVE_EXPR (J) is used in the original and the
12550 transformed version). */
12553 multiple_of_p (tree type, const_tree top, const_tree bottom)
12555 gimple *stmt;
12556 tree t1, op1, op2;
12558 if (operand_equal_p (top, bottom, 0))
12559 return 1;
12561 if (TREE_CODE (type) != INTEGER_TYPE)
12562 return 0;
12564 switch (TREE_CODE (top))
12566 case BIT_AND_EXPR:
12567 /* Bitwise and provides a power of two multiple. If the mask is
12568 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12569 if (!integer_pow2p (bottom))
12570 return 0;
12571 /* FALLTHRU */
12573 case MULT_EXPR:
12574 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12575 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12577 case MINUS_EXPR:
12578 /* It is impossible to prove if op0 - op1 is multiple of bottom
12579 precisely, so be conservative here checking if both op0 and op1
12580 are multiple of bottom. Note we check the second operand first
12581 since it's usually simpler. */
12582 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12583 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12585 case PLUS_EXPR:
12586 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12587 as op0 - 3 if the expression has unsigned type. For example,
12588 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12589 op1 = TREE_OPERAND (top, 1);
12590 if (TYPE_UNSIGNED (type)
12591 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12592 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12593 return (multiple_of_p (type, op1, bottom)
12594 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12596 case LSHIFT_EXPR:
12597 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12599 op1 = TREE_OPERAND (top, 1);
12600 /* const_binop may not detect overflow correctly,
12601 so check for it explicitly here. */
12602 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12603 && 0 != (t1 = fold_convert (type,
12604 const_binop (LSHIFT_EXPR,
12605 size_one_node,
12606 op1)))
12607 && !TREE_OVERFLOW (t1))
12608 return multiple_of_p (type, t1, bottom);
12610 return 0;
12612 case NOP_EXPR:
12613 /* Can't handle conversions from non-integral or wider integral type. */
12614 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12615 || (TYPE_PRECISION (type)
12616 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12617 return 0;
12619 /* fall through */
12621 case SAVE_EXPR:
12622 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12624 case COND_EXPR:
12625 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12626 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12628 case INTEGER_CST:
12629 if (TREE_CODE (bottom) != INTEGER_CST
12630 || integer_zerop (bottom)
12631 || (TYPE_UNSIGNED (type)
12632 && (tree_int_cst_sgn (top) < 0
12633 || tree_int_cst_sgn (bottom) < 0)))
12634 return 0;
12635 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12636 SIGNED);
12638 case SSA_NAME:
12639 if (TREE_CODE (bottom) == INTEGER_CST
12640 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12641 && gimple_code (stmt) == GIMPLE_ASSIGN)
12643 enum tree_code code = gimple_assign_rhs_code (stmt);
12645 /* Check for special cases to see if top is defined as multiple
12646 of bottom:
12648 top = (X & ~(bottom - 1) ; bottom is power of 2
12652 Y = X % bottom
12653 top = X - Y. */
12654 if (code == BIT_AND_EXPR
12655 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12656 && TREE_CODE (op2) == INTEGER_CST
12657 && integer_pow2p (bottom)
12658 && wi::multiple_of_p (wi::to_widest (op2),
12659 wi::to_widest (bottom), UNSIGNED))
12660 return 1;
12662 op1 = gimple_assign_rhs1 (stmt);
12663 if (code == MINUS_EXPR
12664 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12665 && TREE_CODE (op2) == SSA_NAME
12666 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12667 && gimple_code (stmt) == GIMPLE_ASSIGN
12668 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12669 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12670 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12671 return 1;
12674 /* fall through */
12676 default:
12677 return 0;
12681 #define tree_expr_nonnegative_warnv_p(X, Y) \
12682 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12684 #define RECURSE(X) \
12685 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12687 /* Return true if CODE or TYPE is known to be non-negative. */
12689 static bool
12690 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12692 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12693 && truth_value_p (code))
12694 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12695 have a signed:1 type (where the value is -1 and 0). */
12696 return true;
12697 return false;
12700 /* Return true if (CODE OP0) is known to be non-negative. If the return
12701 value is based on the assumption that signed overflow is undefined,
12702 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12703 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12705 bool
12706 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12707 bool *strict_overflow_p, int depth)
12709 if (TYPE_UNSIGNED (type))
12710 return true;
12712 switch (code)
12714 case ABS_EXPR:
12715 /* We can't return 1 if flag_wrapv is set because
12716 ABS_EXPR<INT_MIN> = INT_MIN. */
12717 if (!ANY_INTEGRAL_TYPE_P (type))
12718 return true;
12719 if (TYPE_OVERFLOW_UNDEFINED (type))
12721 *strict_overflow_p = true;
12722 return true;
12724 break;
12726 case NON_LVALUE_EXPR:
12727 case FLOAT_EXPR:
12728 case FIX_TRUNC_EXPR:
12729 return RECURSE (op0);
12731 CASE_CONVERT:
12733 tree inner_type = TREE_TYPE (op0);
12734 tree outer_type = type;
12736 if (TREE_CODE (outer_type) == REAL_TYPE)
12738 if (TREE_CODE (inner_type) == REAL_TYPE)
12739 return RECURSE (op0);
12740 if (INTEGRAL_TYPE_P (inner_type))
12742 if (TYPE_UNSIGNED (inner_type))
12743 return true;
12744 return RECURSE (op0);
12747 else if (INTEGRAL_TYPE_P (outer_type))
12749 if (TREE_CODE (inner_type) == REAL_TYPE)
12750 return RECURSE (op0);
12751 if (INTEGRAL_TYPE_P (inner_type))
12752 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12753 && TYPE_UNSIGNED (inner_type);
12756 break;
12758 default:
12759 return tree_simple_nonnegative_warnv_p (code, type);
12762 /* We don't know sign of `t', so be conservative and return false. */
12763 return false;
12766 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12767 value is based on the assumption that signed overflow is undefined,
12768 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12769 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12771 bool
12772 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12773 tree op1, bool *strict_overflow_p,
12774 int depth)
12776 if (TYPE_UNSIGNED (type))
12777 return true;
12779 switch (code)
12781 case POINTER_PLUS_EXPR:
12782 case PLUS_EXPR:
12783 if (FLOAT_TYPE_P (type))
12784 return RECURSE (op0) && RECURSE (op1);
12786 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12787 both unsigned and at least 2 bits shorter than the result. */
12788 if (TREE_CODE (type) == INTEGER_TYPE
12789 && TREE_CODE (op0) == NOP_EXPR
12790 && TREE_CODE (op1) == NOP_EXPR)
12792 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12793 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12794 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12795 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12797 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12798 TYPE_PRECISION (inner2)) + 1;
12799 return prec < TYPE_PRECISION (type);
12802 break;
12804 case MULT_EXPR:
12805 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12807 /* x * x is always non-negative for floating point x
12808 or without overflow. */
12809 if (operand_equal_p (op0, op1, 0)
12810 || (RECURSE (op0) && RECURSE (op1)))
12812 if (ANY_INTEGRAL_TYPE_P (type)
12813 && TYPE_OVERFLOW_UNDEFINED (type))
12814 *strict_overflow_p = true;
12815 return true;
12819 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12820 both unsigned and their total bits is shorter than the result. */
12821 if (TREE_CODE (type) == INTEGER_TYPE
12822 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12823 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12825 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12826 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12827 : TREE_TYPE (op0);
12828 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12829 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12830 : TREE_TYPE (op1);
12832 bool unsigned0 = TYPE_UNSIGNED (inner0);
12833 bool unsigned1 = TYPE_UNSIGNED (inner1);
12835 if (TREE_CODE (op0) == INTEGER_CST)
12836 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12838 if (TREE_CODE (op1) == INTEGER_CST)
12839 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12841 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12842 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12844 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12845 ? tree_int_cst_min_precision (op0, UNSIGNED)
12846 : TYPE_PRECISION (inner0);
12848 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12849 ? tree_int_cst_min_precision (op1, UNSIGNED)
12850 : TYPE_PRECISION (inner1);
12852 return precision0 + precision1 < TYPE_PRECISION (type);
12855 return false;
12857 case BIT_AND_EXPR:
12858 case MAX_EXPR:
12859 return RECURSE (op0) || RECURSE (op1);
12861 case BIT_IOR_EXPR:
12862 case BIT_XOR_EXPR:
12863 case MIN_EXPR:
12864 case RDIV_EXPR:
12865 case TRUNC_DIV_EXPR:
12866 case CEIL_DIV_EXPR:
12867 case FLOOR_DIV_EXPR:
12868 case ROUND_DIV_EXPR:
12869 return RECURSE (op0) && RECURSE (op1);
12871 case TRUNC_MOD_EXPR:
12872 return RECURSE (op0);
12874 case FLOOR_MOD_EXPR:
12875 return RECURSE (op1);
12877 case CEIL_MOD_EXPR:
12878 case ROUND_MOD_EXPR:
12879 default:
12880 return tree_simple_nonnegative_warnv_p (code, type);
12883 /* We don't know sign of `t', so be conservative and return false. */
12884 return false;
12887 /* Return true if T is known to be non-negative. If the return
12888 value is based on the assumption that signed overflow is undefined,
12889 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12890 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12892 bool
12893 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12895 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12896 return true;
12898 switch (TREE_CODE (t))
12900 case INTEGER_CST:
12901 return tree_int_cst_sgn (t) >= 0;
12903 case REAL_CST:
12904 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12906 case FIXED_CST:
12907 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12909 case COND_EXPR:
12910 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12912 case SSA_NAME:
12913 /* Limit the depth of recursion to avoid quadratic behavior.
12914 This is expected to catch almost all occurrences in practice.
12915 If this code misses important cases that unbounded recursion
12916 would not, passes that need this information could be revised
12917 to provide it through dataflow propagation. */
12918 return (!name_registered_for_update_p (t)
12919 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12920 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12921 strict_overflow_p, depth));
12923 default:
12924 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12928 /* Return true if T is known to be non-negative. If the return
12929 value is based on the assumption that signed overflow is undefined,
12930 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12931 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12933 bool
12934 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12935 bool *strict_overflow_p, int depth)
12937 switch (fn)
12939 CASE_CFN_ACOS:
12940 CASE_CFN_ACOSH:
12941 CASE_CFN_CABS:
12942 CASE_CFN_COSH:
12943 CASE_CFN_ERFC:
12944 CASE_CFN_EXP:
12945 CASE_CFN_EXP10:
12946 CASE_CFN_EXP2:
12947 CASE_CFN_FABS:
12948 CASE_CFN_FDIM:
12949 CASE_CFN_HYPOT:
12950 CASE_CFN_POW10:
12951 CASE_CFN_FFS:
12952 CASE_CFN_PARITY:
12953 CASE_CFN_POPCOUNT:
12954 CASE_CFN_CLZ:
12955 CASE_CFN_CLRSB:
12956 case CFN_BUILT_IN_BSWAP32:
12957 case CFN_BUILT_IN_BSWAP64:
12958 /* Always true. */
12959 return true;
12961 CASE_CFN_SQRT:
12962 /* sqrt(-0.0) is -0.0. */
12963 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12964 return true;
12965 return RECURSE (arg0);
12967 CASE_CFN_ASINH:
12968 CASE_CFN_ATAN:
12969 CASE_CFN_ATANH:
12970 CASE_CFN_CBRT:
12971 CASE_CFN_CEIL:
12972 CASE_CFN_ERF:
12973 CASE_CFN_EXPM1:
12974 CASE_CFN_FLOOR:
12975 CASE_CFN_FMOD:
12976 CASE_CFN_FREXP:
12977 CASE_CFN_ICEIL:
12978 CASE_CFN_IFLOOR:
12979 CASE_CFN_IRINT:
12980 CASE_CFN_IROUND:
12981 CASE_CFN_LCEIL:
12982 CASE_CFN_LDEXP:
12983 CASE_CFN_LFLOOR:
12984 CASE_CFN_LLCEIL:
12985 CASE_CFN_LLFLOOR:
12986 CASE_CFN_LLRINT:
12987 CASE_CFN_LLROUND:
12988 CASE_CFN_LRINT:
12989 CASE_CFN_LROUND:
12990 CASE_CFN_MODF:
12991 CASE_CFN_NEARBYINT:
12992 CASE_CFN_RINT:
12993 CASE_CFN_ROUND:
12994 CASE_CFN_SCALB:
12995 CASE_CFN_SCALBLN:
12996 CASE_CFN_SCALBN:
12997 CASE_CFN_SIGNBIT:
12998 CASE_CFN_SIGNIFICAND:
12999 CASE_CFN_SINH:
13000 CASE_CFN_TANH:
13001 CASE_CFN_TRUNC:
13002 /* True if the 1st argument is nonnegative. */
13003 return RECURSE (arg0);
13005 CASE_CFN_FMAX:
13006 /* True if the 1st OR 2nd arguments are nonnegative. */
13007 return RECURSE (arg0) || RECURSE (arg1);
13009 CASE_CFN_FMIN:
13010 /* True if the 1st AND 2nd arguments are nonnegative. */
13011 return RECURSE (arg0) && RECURSE (arg1);
13013 CASE_CFN_COPYSIGN:
13014 /* True if the 2nd argument is nonnegative. */
13015 return RECURSE (arg1);
13017 CASE_CFN_POWI:
13018 /* True if the 1st argument is nonnegative or the second
13019 argument is an even integer. */
13020 if (TREE_CODE (arg1) == INTEGER_CST
13021 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13022 return true;
13023 return RECURSE (arg0);
13025 CASE_CFN_POW:
13026 /* True if the 1st argument is nonnegative or the second
13027 argument is an even integer valued real. */
13028 if (TREE_CODE (arg1) == REAL_CST)
13030 REAL_VALUE_TYPE c;
13031 HOST_WIDE_INT n;
13033 c = TREE_REAL_CST (arg1);
13034 n = real_to_integer (&c);
13035 if ((n & 1) == 0)
13037 REAL_VALUE_TYPE cint;
13038 real_from_integer (&cint, VOIDmode, n, SIGNED);
13039 if (real_identical (&c, &cint))
13040 return true;
13043 return RECURSE (arg0);
13045 default:
13046 break;
13048 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13051 /* Return true if T is known to be non-negative. If the return
13052 value is based on the assumption that signed overflow is undefined,
13053 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13054 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13056 static bool
13057 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13059 enum tree_code code = TREE_CODE (t);
13060 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13061 return true;
13063 switch (code)
13065 case TARGET_EXPR:
13067 tree temp = TARGET_EXPR_SLOT (t);
13068 t = TARGET_EXPR_INITIAL (t);
13070 /* If the initializer is non-void, then it's a normal expression
13071 that will be assigned to the slot. */
13072 if (!VOID_TYPE_P (t))
13073 return RECURSE (t);
13075 /* Otherwise, the initializer sets the slot in some way. One common
13076 way is an assignment statement at the end of the initializer. */
13077 while (1)
13079 if (TREE_CODE (t) == BIND_EXPR)
13080 t = expr_last (BIND_EXPR_BODY (t));
13081 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13082 || TREE_CODE (t) == TRY_CATCH_EXPR)
13083 t = expr_last (TREE_OPERAND (t, 0));
13084 else if (TREE_CODE (t) == STATEMENT_LIST)
13085 t = expr_last (t);
13086 else
13087 break;
13089 if (TREE_CODE (t) == MODIFY_EXPR
13090 && TREE_OPERAND (t, 0) == temp)
13091 return RECURSE (TREE_OPERAND (t, 1));
13093 return false;
13096 case CALL_EXPR:
13098 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13099 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13101 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13102 get_call_combined_fn (t),
13103 arg0,
13104 arg1,
13105 strict_overflow_p, depth);
13107 case COMPOUND_EXPR:
13108 case MODIFY_EXPR:
13109 return RECURSE (TREE_OPERAND (t, 1));
13111 case BIND_EXPR:
13112 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13114 case SAVE_EXPR:
13115 return RECURSE (TREE_OPERAND (t, 0));
13117 default:
13118 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13122 #undef RECURSE
13123 #undef tree_expr_nonnegative_warnv_p
13125 /* Return true if T is known to be non-negative. If the return
13126 value is based on the assumption that signed overflow is undefined,
13127 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13128 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13130 bool
13131 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13133 enum tree_code code;
13134 if (t == error_mark_node)
13135 return false;
13137 code = TREE_CODE (t);
13138 switch (TREE_CODE_CLASS (code))
13140 case tcc_binary:
13141 case tcc_comparison:
13142 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13143 TREE_TYPE (t),
13144 TREE_OPERAND (t, 0),
13145 TREE_OPERAND (t, 1),
13146 strict_overflow_p, depth);
13148 case tcc_unary:
13149 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13150 TREE_TYPE (t),
13151 TREE_OPERAND (t, 0),
13152 strict_overflow_p, depth);
13154 case tcc_constant:
13155 case tcc_declaration:
13156 case tcc_reference:
13157 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13159 default:
13160 break;
13163 switch (code)
13165 case TRUTH_AND_EXPR:
13166 case TRUTH_OR_EXPR:
13167 case TRUTH_XOR_EXPR:
13168 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13169 TREE_TYPE (t),
13170 TREE_OPERAND (t, 0),
13171 TREE_OPERAND (t, 1),
13172 strict_overflow_p, depth);
13173 case TRUTH_NOT_EXPR:
13174 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13175 TREE_TYPE (t),
13176 TREE_OPERAND (t, 0),
13177 strict_overflow_p, depth);
13179 case COND_EXPR:
13180 case CONSTRUCTOR:
13181 case OBJ_TYPE_REF:
13182 case ASSERT_EXPR:
13183 case ADDR_EXPR:
13184 case WITH_SIZE_EXPR:
13185 case SSA_NAME:
13186 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13188 default:
13189 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13193 /* Return true if `t' is known to be non-negative. Handle warnings
13194 about undefined signed overflow. */
13196 bool
13197 tree_expr_nonnegative_p (tree t)
13199 bool ret, strict_overflow_p;
13201 strict_overflow_p = false;
13202 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13203 if (strict_overflow_p)
13204 fold_overflow_warning (("assuming signed overflow does not occur when "
13205 "determining that expression is always "
13206 "non-negative"),
13207 WARN_STRICT_OVERFLOW_MISC);
13208 return ret;
13212 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13213 For floating point we further ensure that T is not denormal.
13214 Similar logic is present in nonzero_address in rtlanal.h.
13216 If the return value is based on the assumption that signed overflow
13217 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13218 change *STRICT_OVERFLOW_P. */
13220 bool
13221 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13222 bool *strict_overflow_p)
13224 switch (code)
13226 case ABS_EXPR:
13227 return tree_expr_nonzero_warnv_p (op0,
13228 strict_overflow_p);
13230 case NOP_EXPR:
13232 tree inner_type = TREE_TYPE (op0);
13233 tree outer_type = type;
13235 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13236 && tree_expr_nonzero_warnv_p (op0,
13237 strict_overflow_p));
13239 break;
13241 case NON_LVALUE_EXPR:
13242 return tree_expr_nonzero_warnv_p (op0,
13243 strict_overflow_p);
13245 default:
13246 break;
13249 return false;
13252 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13253 For floating point we further ensure that T is not denormal.
13254 Similar logic is present in nonzero_address in rtlanal.h.
13256 If the return value is based on the assumption that signed overflow
13257 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13258 change *STRICT_OVERFLOW_P. */
13260 bool
13261 tree_binary_nonzero_warnv_p (enum tree_code code,
13262 tree type,
13263 tree op0,
13264 tree op1, bool *strict_overflow_p)
13266 bool sub_strict_overflow_p;
13267 switch (code)
13269 case POINTER_PLUS_EXPR:
13270 case PLUS_EXPR:
13271 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13273 /* With the presence of negative values it is hard
13274 to say something. */
13275 sub_strict_overflow_p = false;
13276 if (!tree_expr_nonnegative_warnv_p (op0,
13277 &sub_strict_overflow_p)
13278 || !tree_expr_nonnegative_warnv_p (op1,
13279 &sub_strict_overflow_p))
13280 return false;
13281 /* One of operands must be positive and the other non-negative. */
13282 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13283 overflows, on a twos-complement machine the sum of two
13284 nonnegative numbers can never be zero. */
13285 return (tree_expr_nonzero_warnv_p (op0,
13286 strict_overflow_p)
13287 || tree_expr_nonzero_warnv_p (op1,
13288 strict_overflow_p));
13290 break;
13292 case MULT_EXPR:
13293 if (TYPE_OVERFLOW_UNDEFINED (type))
13295 if (tree_expr_nonzero_warnv_p (op0,
13296 strict_overflow_p)
13297 && tree_expr_nonzero_warnv_p (op1,
13298 strict_overflow_p))
13300 *strict_overflow_p = true;
13301 return true;
13304 break;
13306 case MIN_EXPR:
13307 sub_strict_overflow_p = false;
13308 if (tree_expr_nonzero_warnv_p (op0,
13309 &sub_strict_overflow_p)
13310 && tree_expr_nonzero_warnv_p (op1,
13311 &sub_strict_overflow_p))
13313 if (sub_strict_overflow_p)
13314 *strict_overflow_p = true;
13316 break;
13318 case MAX_EXPR:
13319 sub_strict_overflow_p = false;
13320 if (tree_expr_nonzero_warnv_p (op0,
13321 &sub_strict_overflow_p))
13323 if (sub_strict_overflow_p)
13324 *strict_overflow_p = true;
13326 /* When both operands are nonzero, then MAX must be too. */
13327 if (tree_expr_nonzero_warnv_p (op1,
13328 strict_overflow_p))
13329 return true;
13331 /* MAX where operand 0 is positive is positive. */
13332 return tree_expr_nonnegative_warnv_p (op0,
13333 strict_overflow_p);
13335 /* MAX where operand 1 is positive is positive. */
13336 else if (tree_expr_nonzero_warnv_p (op1,
13337 &sub_strict_overflow_p)
13338 && tree_expr_nonnegative_warnv_p (op1,
13339 &sub_strict_overflow_p))
13341 if (sub_strict_overflow_p)
13342 *strict_overflow_p = true;
13343 return true;
13345 break;
13347 case BIT_IOR_EXPR:
13348 return (tree_expr_nonzero_warnv_p (op1,
13349 strict_overflow_p)
13350 || tree_expr_nonzero_warnv_p (op0,
13351 strict_overflow_p));
13353 default:
13354 break;
13357 return false;
13360 /* Return true when T is an address and is known to be nonzero.
13361 For floating point we further ensure that T is not denormal.
13362 Similar logic is present in nonzero_address in rtlanal.h.
13364 If the return value is based on the assumption that signed overflow
13365 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13366 change *STRICT_OVERFLOW_P. */
13368 bool
13369 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13371 bool sub_strict_overflow_p;
13372 switch (TREE_CODE (t))
13374 case INTEGER_CST:
13375 return !integer_zerop (t);
13377 case ADDR_EXPR:
13379 tree base = TREE_OPERAND (t, 0);
13381 if (!DECL_P (base))
13382 base = get_base_address (base);
13384 if (base && TREE_CODE (base) == TARGET_EXPR)
13385 base = TARGET_EXPR_SLOT (base);
13387 if (!base)
13388 return false;
13390 /* For objects in symbol table check if we know they are non-zero.
13391 Don't do anything for variables and functions before symtab is built;
13392 it is quite possible that they will be declared weak later. */
13393 int nonzero_addr = maybe_nonzero_address (base);
13394 if (nonzero_addr >= 0)
13395 return nonzero_addr;
13397 /* Function local objects are never NULL. */
13398 if (DECL_P (base)
13399 && (DECL_CONTEXT (base)
13400 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13401 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13402 return true;
13404 /* Constants are never weak. */
13405 if (CONSTANT_CLASS_P (base))
13406 return true;
13408 return false;
13411 case COND_EXPR:
13412 sub_strict_overflow_p = false;
13413 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13414 &sub_strict_overflow_p)
13415 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13416 &sub_strict_overflow_p))
13418 if (sub_strict_overflow_p)
13419 *strict_overflow_p = true;
13420 return true;
13422 break;
13424 default:
13425 break;
13427 return false;
13430 #define integer_valued_real_p(X) \
13431 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13433 #define RECURSE(X) \
13434 ((integer_valued_real_p) (X, depth + 1))
13436 /* Return true if the floating point result of (CODE OP0) has an
13437 integer value. We also allow +Inf, -Inf and NaN to be considered
13438 integer values. Return false for signaling NaN.
13440 DEPTH is the current nesting depth of the query. */
13442 bool
13443 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13445 switch (code)
13447 case FLOAT_EXPR:
13448 return true;
13450 case ABS_EXPR:
13451 return RECURSE (op0);
13453 CASE_CONVERT:
13455 tree type = TREE_TYPE (op0);
13456 if (TREE_CODE (type) == INTEGER_TYPE)
13457 return true;
13458 if (TREE_CODE (type) == REAL_TYPE)
13459 return RECURSE (op0);
13460 break;
13463 default:
13464 break;
13466 return false;
13469 /* Return true if the floating point result of (CODE OP0 OP1) has an
13470 integer value. We also allow +Inf, -Inf and NaN to be considered
13471 integer values. Return false for signaling NaN.
13473 DEPTH is the current nesting depth of the query. */
13475 bool
13476 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13478 switch (code)
13480 case PLUS_EXPR:
13481 case MINUS_EXPR:
13482 case MULT_EXPR:
13483 case MIN_EXPR:
13484 case MAX_EXPR:
13485 return RECURSE (op0) && RECURSE (op1);
13487 default:
13488 break;
13490 return false;
13493 /* Return true if the floating point result of calling FNDECL with arguments
13494 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13495 considered integer values. Return false for signaling NaN. If FNDECL
13496 takes fewer than 2 arguments, the remaining ARGn are null.
13498 DEPTH is the current nesting depth of the query. */
13500 bool
13501 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13503 switch (fn)
13505 CASE_CFN_CEIL:
13506 CASE_CFN_FLOOR:
13507 CASE_CFN_NEARBYINT:
13508 CASE_CFN_RINT:
13509 CASE_CFN_ROUND:
13510 CASE_CFN_TRUNC:
13511 return true;
13513 CASE_CFN_FMIN:
13514 CASE_CFN_FMAX:
13515 return RECURSE (arg0) && RECURSE (arg1);
13517 default:
13518 break;
13520 return false;
13523 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13524 has an integer value. We also allow +Inf, -Inf and NaN to be
13525 considered integer values. Return false for signaling NaN.
13527 DEPTH is the current nesting depth of the query. */
13529 bool
13530 integer_valued_real_single_p (tree t, int depth)
13532 switch (TREE_CODE (t))
13534 case REAL_CST:
13535 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13537 case COND_EXPR:
13538 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13540 case SSA_NAME:
13541 /* Limit the depth of recursion to avoid quadratic behavior.
13542 This is expected to catch almost all occurrences in practice.
13543 If this code misses important cases that unbounded recursion
13544 would not, passes that need this information could be revised
13545 to provide it through dataflow propagation. */
13546 return (!name_registered_for_update_p (t)
13547 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13548 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13549 depth));
13551 default:
13552 break;
13554 return false;
13557 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13558 has an integer value. We also allow +Inf, -Inf and NaN to be
13559 considered integer values. Return false for signaling NaN.
13561 DEPTH is the current nesting depth of the query. */
13563 static bool
13564 integer_valued_real_invalid_p (tree t, int depth)
13566 switch (TREE_CODE (t))
13568 case COMPOUND_EXPR:
13569 case MODIFY_EXPR:
13570 case BIND_EXPR:
13571 return RECURSE (TREE_OPERAND (t, 1));
13573 case SAVE_EXPR:
13574 return RECURSE (TREE_OPERAND (t, 0));
13576 default:
13577 break;
13579 return false;
13582 #undef RECURSE
13583 #undef integer_valued_real_p
13585 /* Return true if the floating point expression T has an integer value.
13586 We also allow +Inf, -Inf and NaN to be considered integer values.
13587 Return false for signaling NaN.
13589 DEPTH is the current nesting depth of the query. */
13591 bool
13592 integer_valued_real_p (tree t, int depth)
13594 if (t == error_mark_node)
13595 return false;
13597 tree_code code = TREE_CODE (t);
13598 switch (TREE_CODE_CLASS (code))
13600 case tcc_binary:
13601 case tcc_comparison:
13602 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13603 TREE_OPERAND (t, 1), depth);
13605 case tcc_unary:
13606 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13608 case tcc_constant:
13609 case tcc_declaration:
13610 case tcc_reference:
13611 return integer_valued_real_single_p (t, depth);
13613 default:
13614 break;
13617 switch (code)
13619 case COND_EXPR:
13620 case SSA_NAME:
13621 return integer_valued_real_single_p (t, depth);
13623 case CALL_EXPR:
13625 tree arg0 = (call_expr_nargs (t) > 0
13626 ? CALL_EXPR_ARG (t, 0)
13627 : NULL_TREE);
13628 tree arg1 = (call_expr_nargs (t) > 1
13629 ? CALL_EXPR_ARG (t, 1)
13630 : NULL_TREE);
13631 return integer_valued_real_call_p (get_call_combined_fn (t),
13632 arg0, arg1, depth);
13635 default:
13636 return integer_valued_real_invalid_p (t, depth);
13640 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13641 attempt to fold the expression to a constant without modifying TYPE,
13642 OP0 or OP1.
13644 If the expression could be simplified to a constant, then return
13645 the constant. If the expression would not be simplified to a
13646 constant, then return NULL_TREE. */
13648 tree
13649 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13651 tree tem = fold_binary (code, type, op0, op1);
13652 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13655 /* Given the components of a unary expression CODE, TYPE and OP0,
13656 attempt to fold the expression to a constant without modifying
13657 TYPE or OP0.
13659 If the expression could be simplified to a constant, then return
13660 the constant. If the expression would not be simplified to a
13661 constant, then return NULL_TREE. */
13663 tree
13664 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13666 tree tem = fold_unary (code, type, op0);
13667 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13670 /* If EXP represents referencing an element in a constant string
13671 (either via pointer arithmetic or array indexing), return the
13672 tree representing the value accessed, otherwise return NULL. */
13674 tree
13675 fold_read_from_constant_string (tree exp)
13677 if ((TREE_CODE (exp) == INDIRECT_REF
13678 || TREE_CODE (exp) == ARRAY_REF)
13679 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13681 tree exp1 = TREE_OPERAND (exp, 0);
13682 tree index;
13683 tree string;
13684 location_t loc = EXPR_LOCATION (exp);
13686 if (TREE_CODE (exp) == INDIRECT_REF)
13687 string = string_constant (exp1, &index);
13688 else
13690 tree low_bound = array_ref_low_bound (exp);
13691 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13693 /* Optimize the special-case of a zero lower bound.
13695 We convert the low_bound to sizetype to avoid some problems
13696 with constant folding. (E.g. suppose the lower bound is 1,
13697 and its mode is QI. Without the conversion,l (ARRAY
13698 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13699 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13700 if (! integer_zerop (low_bound))
13701 index = size_diffop_loc (loc, index,
13702 fold_convert_loc (loc, sizetype, low_bound));
13704 string = exp1;
13707 if (string
13708 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13709 && TREE_CODE (string) == STRING_CST
13710 && TREE_CODE (index) == INTEGER_CST
13711 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13712 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13713 == MODE_INT)
13714 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13715 return build_int_cst_type (TREE_TYPE (exp),
13716 (TREE_STRING_POINTER (string)
13717 [TREE_INT_CST_LOW (index)]));
13719 return NULL;
13722 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13723 an integer constant, real, or fixed-point constant.
13725 TYPE is the type of the result. */
13727 static tree
13728 fold_negate_const (tree arg0, tree type)
13730 tree t = NULL_TREE;
13732 switch (TREE_CODE (arg0))
13734 case INTEGER_CST:
13736 bool overflow;
13737 wide_int val = wi::neg (arg0, &overflow);
13738 t = force_fit_type (type, val, 1,
13739 (overflow | TREE_OVERFLOW (arg0))
13740 && !TYPE_UNSIGNED (type));
13741 break;
13744 case REAL_CST:
13745 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13746 break;
13748 case FIXED_CST:
13750 FIXED_VALUE_TYPE f;
13751 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13752 &(TREE_FIXED_CST (arg0)), NULL,
13753 TYPE_SATURATING (type));
13754 t = build_fixed (type, f);
13755 /* Propagate overflow flags. */
13756 if (overflow_p | TREE_OVERFLOW (arg0))
13757 TREE_OVERFLOW (t) = 1;
13758 break;
13761 default:
13762 gcc_unreachable ();
13765 return t;
13768 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13769 an integer constant or real constant.
13771 TYPE is the type of the result. */
13773 tree
13774 fold_abs_const (tree arg0, tree type)
13776 tree t = NULL_TREE;
13778 switch (TREE_CODE (arg0))
13780 case INTEGER_CST:
13782 /* If the value is unsigned or non-negative, then the absolute value
13783 is the same as the ordinary value. */
13784 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13785 t = arg0;
13787 /* If the value is negative, then the absolute value is
13788 its negation. */
13789 else
13791 bool overflow;
13792 wide_int val = wi::neg (arg0, &overflow);
13793 t = force_fit_type (type, val, -1,
13794 overflow | TREE_OVERFLOW (arg0));
13797 break;
13799 case REAL_CST:
13800 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13801 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13802 else
13803 t = arg0;
13804 break;
13806 default:
13807 gcc_unreachable ();
13810 return t;
13813 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13814 constant. TYPE is the type of the result. */
13816 static tree
13817 fold_not_const (const_tree arg0, tree type)
13819 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13821 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13824 /* Given CODE, a relational operator, the target type, TYPE and two
13825 constant operands OP0 and OP1, return the result of the
13826 relational operation. If the result is not a compile time
13827 constant, then return NULL_TREE. */
13829 static tree
13830 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13832 int result, invert;
13834 /* From here on, the only cases we handle are when the result is
13835 known to be a constant. */
13837 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13839 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13840 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13842 /* Handle the cases where either operand is a NaN. */
13843 if (real_isnan (c0) || real_isnan (c1))
13845 switch (code)
13847 case EQ_EXPR:
13848 case ORDERED_EXPR:
13849 result = 0;
13850 break;
13852 case NE_EXPR:
13853 case UNORDERED_EXPR:
13854 case UNLT_EXPR:
13855 case UNLE_EXPR:
13856 case UNGT_EXPR:
13857 case UNGE_EXPR:
13858 case UNEQ_EXPR:
13859 result = 1;
13860 break;
13862 case LT_EXPR:
13863 case LE_EXPR:
13864 case GT_EXPR:
13865 case GE_EXPR:
13866 case LTGT_EXPR:
13867 if (flag_trapping_math)
13868 return NULL_TREE;
13869 result = 0;
13870 break;
13872 default:
13873 gcc_unreachable ();
13876 return constant_boolean_node (result, type);
13879 return constant_boolean_node (real_compare (code, c0, c1), type);
13882 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13884 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13885 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13886 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13889 /* Handle equality/inequality of complex constants. */
13890 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13892 tree rcond = fold_relational_const (code, type,
13893 TREE_REALPART (op0),
13894 TREE_REALPART (op1));
13895 tree icond = fold_relational_const (code, type,
13896 TREE_IMAGPART (op0),
13897 TREE_IMAGPART (op1));
13898 if (code == EQ_EXPR)
13899 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13900 else if (code == NE_EXPR)
13901 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13902 else
13903 return NULL_TREE;
13906 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13908 if (!VECTOR_TYPE_P (type))
13910 /* Have vector comparison with scalar boolean result. */
13911 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13912 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13913 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13915 tree elem0 = VECTOR_CST_ELT (op0, i);
13916 tree elem1 = VECTOR_CST_ELT (op1, i);
13917 tree tmp = fold_relational_const (code, type, elem0, elem1);
13918 if (tmp == NULL_TREE)
13919 return NULL_TREE;
13920 if (integer_zerop (tmp))
13921 return constant_boolean_node (false, type);
13923 return constant_boolean_node (true, type);
13925 unsigned count = VECTOR_CST_NELTS (op0);
13926 tree *elts = XALLOCAVEC (tree, count);
13927 gcc_assert (VECTOR_CST_NELTS (op1) == count
13928 && TYPE_VECTOR_SUBPARTS (type) == count);
13930 for (unsigned i = 0; i < count; i++)
13932 tree elem_type = TREE_TYPE (type);
13933 tree elem0 = VECTOR_CST_ELT (op0, i);
13934 tree elem1 = VECTOR_CST_ELT (op1, i);
13936 tree tem = fold_relational_const (code, elem_type,
13937 elem0, elem1);
13939 if (tem == NULL_TREE)
13940 return NULL_TREE;
13942 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13945 return build_vector (type, elts);
13948 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13950 To compute GT, swap the arguments and do LT.
13951 To compute GE, do LT and invert the result.
13952 To compute LE, swap the arguments, do LT and invert the result.
13953 To compute NE, do EQ and invert the result.
13955 Therefore, the code below must handle only EQ and LT. */
13957 if (code == LE_EXPR || code == GT_EXPR)
13959 std::swap (op0, op1);
13960 code = swap_tree_comparison (code);
13963 /* Note that it is safe to invert for real values here because we
13964 have already handled the one case that it matters. */
13966 invert = 0;
13967 if (code == NE_EXPR || code == GE_EXPR)
13969 invert = 1;
13970 code = invert_tree_comparison (code, false);
13973 /* Compute a result for LT or EQ if args permit;
13974 Otherwise return T. */
13975 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13977 if (code == EQ_EXPR)
13978 result = tree_int_cst_equal (op0, op1);
13979 else
13980 result = tree_int_cst_lt (op0, op1);
13982 else
13983 return NULL_TREE;
13985 if (invert)
13986 result ^= 1;
13987 return constant_boolean_node (result, type);
13990 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13991 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13992 itself. */
13994 tree
13995 fold_build_cleanup_point_expr (tree type, tree expr)
13997 /* If the expression does not have side effects then we don't have to wrap
13998 it with a cleanup point expression. */
13999 if (!TREE_SIDE_EFFECTS (expr))
14000 return expr;
14002 /* If the expression is a return, check to see if the expression inside the
14003 return has no side effects or the right hand side of the modify expression
14004 inside the return. If either don't have side effects set we don't need to
14005 wrap the expression in a cleanup point expression. Note we don't check the
14006 left hand side of the modify because it should always be a return decl. */
14007 if (TREE_CODE (expr) == RETURN_EXPR)
14009 tree op = TREE_OPERAND (expr, 0);
14010 if (!op || !TREE_SIDE_EFFECTS (op))
14011 return expr;
14012 op = TREE_OPERAND (op, 1);
14013 if (!TREE_SIDE_EFFECTS (op))
14014 return expr;
14017 return build1 (CLEANUP_POINT_EXPR, type, expr);
14020 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14021 of an indirection through OP0, or NULL_TREE if no simplification is
14022 possible. */
14024 tree
14025 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14027 tree sub = op0;
14028 tree subtype;
14030 STRIP_NOPS (sub);
14031 subtype = TREE_TYPE (sub);
14032 if (!POINTER_TYPE_P (subtype))
14033 return NULL_TREE;
14035 if (TREE_CODE (sub) == ADDR_EXPR)
14037 tree op = TREE_OPERAND (sub, 0);
14038 tree optype = TREE_TYPE (op);
14039 /* *&CONST_DECL -> to the value of the const decl. */
14040 if (TREE_CODE (op) == CONST_DECL)
14041 return DECL_INITIAL (op);
14042 /* *&p => p; make sure to handle *&"str"[cst] here. */
14043 if (type == optype)
14045 tree fop = fold_read_from_constant_string (op);
14046 if (fop)
14047 return fop;
14048 else
14049 return op;
14051 /* *(foo *)&fooarray => fooarray[0] */
14052 else if (TREE_CODE (optype) == ARRAY_TYPE
14053 && type == TREE_TYPE (optype)
14054 && (!in_gimple_form
14055 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14057 tree type_domain = TYPE_DOMAIN (optype);
14058 tree min_val = size_zero_node;
14059 if (type_domain && TYPE_MIN_VALUE (type_domain))
14060 min_val = TYPE_MIN_VALUE (type_domain);
14061 if (in_gimple_form
14062 && TREE_CODE (min_val) != INTEGER_CST)
14063 return NULL_TREE;
14064 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14065 NULL_TREE, NULL_TREE);
14067 /* *(foo *)&complexfoo => __real__ complexfoo */
14068 else if (TREE_CODE (optype) == COMPLEX_TYPE
14069 && type == TREE_TYPE (optype))
14070 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14071 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14072 else if (TREE_CODE (optype) == VECTOR_TYPE
14073 && type == TREE_TYPE (optype))
14075 tree part_width = TYPE_SIZE (type);
14076 tree index = bitsize_int (0);
14077 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14081 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14082 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14084 tree op00 = TREE_OPERAND (sub, 0);
14085 tree op01 = TREE_OPERAND (sub, 1);
14087 STRIP_NOPS (op00);
14088 if (TREE_CODE (op00) == ADDR_EXPR)
14090 tree op00type;
14091 op00 = TREE_OPERAND (op00, 0);
14092 op00type = TREE_TYPE (op00);
14094 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14095 if (TREE_CODE (op00type) == VECTOR_TYPE
14096 && type == TREE_TYPE (op00type))
14098 tree part_width = TYPE_SIZE (type);
14099 unsigned HOST_WIDE_INT max_offset
14100 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14101 * TYPE_VECTOR_SUBPARTS (op00type));
14102 if (tree_int_cst_sign_bit (op01) == 0
14103 && compare_tree_int (op01, max_offset) == -1)
14105 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14106 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14107 tree index = bitsize_int (indexi);
14108 return fold_build3_loc (loc,
14109 BIT_FIELD_REF, type, op00,
14110 part_width, index);
14113 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14114 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14115 && type == TREE_TYPE (op00type))
14117 tree size = TYPE_SIZE_UNIT (type);
14118 if (tree_int_cst_equal (size, op01))
14119 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14121 /* ((foo *)&fooarray)[1] => fooarray[1] */
14122 else if (TREE_CODE (op00type) == ARRAY_TYPE
14123 && type == TREE_TYPE (op00type))
14125 tree type_domain = TYPE_DOMAIN (op00type);
14126 tree min_val = size_zero_node;
14127 if (type_domain && TYPE_MIN_VALUE (type_domain))
14128 min_val = TYPE_MIN_VALUE (type_domain);
14129 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14130 TYPE_SIZE_UNIT (type));
14131 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14132 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14133 NULL_TREE, NULL_TREE);
14138 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14139 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14140 && type == TREE_TYPE (TREE_TYPE (subtype))
14141 && (!in_gimple_form
14142 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14144 tree type_domain;
14145 tree min_val = size_zero_node;
14146 sub = build_fold_indirect_ref_loc (loc, sub);
14147 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14148 if (type_domain && TYPE_MIN_VALUE (type_domain))
14149 min_val = TYPE_MIN_VALUE (type_domain);
14150 if (in_gimple_form
14151 && TREE_CODE (min_val) != INTEGER_CST)
14152 return NULL_TREE;
14153 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14154 NULL_TREE);
14157 return NULL_TREE;
14160 /* Builds an expression for an indirection through T, simplifying some
14161 cases. */
14163 tree
14164 build_fold_indirect_ref_loc (location_t loc, tree t)
14166 tree type = TREE_TYPE (TREE_TYPE (t));
14167 tree sub = fold_indirect_ref_1 (loc, type, t);
14169 if (sub)
14170 return sub;
14172 return build1_loc (loc, INDIRECT_REF, type, t);
14175 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14177 tree
14178 fold_indirect_ref_loc (location_t loc, tree t)
14180 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14182 if (sub)
14183 return sub;
14184 else
14185 return t;
14188 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14189 whose result is ignored. The type of the returned tree need not be
14190 the same as the original expression. */
14192 tree
14193 fold_ignored_result (tree t)
14195 if (!TREE_SIDE_EFFECTS (t))
14196 return integer_zero_node;
14198 for (;;)
14199 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14201 case tcc_unary:
14202 t = TREE_OPERAND (t, 0);
14203 break;
14205 case tcc_binary:
14206 case tcc_comparison:
14207 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14208 t = TREE_OPERAND (t, 0);
14209 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14210 t = TREE_OPERAND (t, 1);
14211 else
14212 return t;
14213 break;
14215 case tcc_expression:
14216 switch (TREE_CODE (t))
14218 case COMPOUND_EXPR:
14219 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14220 return t;
14221 t = TREE_OPERAND (t, 0);
14222 break;
14224 case COND_EXPR:
14225 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14226 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14227 return t;
14228 t = TREE_OPERAND (t, 0);
14229 break;
14231 default:
14232 return t;
14234 break;
14236 default:
14237 return t;
14241 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14243 tree
14244 round_up_loc (location_t loc, tree value, unsigned int divisor)
14246 tree div = NULL_TREE;
14248 if (divisor == 1)
14249 return value;
14251 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14252 have to do anything. Only do this when we are not given a const,
14253 because in that case, this check is more expensive than just
14254 doing it. */
14255 if (TREE_CODE (value) != INTEGER_CST)
14257 div = build_int_cst (TREE_TYPE (value), divisor);
14259 if (multiple_of_p (TREE_TYPE (value), value, div))
14260 return value;
14263 /* If divisor is a power of two, simplify this to bit manipulation. */
14264 if (pow2_or_zerop (divisor))
14266 if (TREE_CODE (value) == INTEGER_CST)
14268 wide_int val = value;
14269 bool overflow_p;
14271 if ((val & (divisor - 1)) == 0)
14272 return value;
14274 overflow_p = TREE_OVERFLOW (value);
14275 val += divisor - 1;
14276 val &= - (int) divisor;
14277 if (val == 0)
14278 overflow_p = true;
14280 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14282 else
14284 tree t;
14286 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14287 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14288 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14289 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14292 else
14294 if (!div)
14295 div = build_int_cst (TREE_TYPE (value), divisor);
14296 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14297 value = size_binop_loc (loc, MULT_EXPR, value, div);
14300 return value;
14303 /* Likewise, but round down. */
14305 tree
14306 round_down_loc (location_t loc, tree value, int divisor)
14308 tree div = NULL_TREE;
14310 gcc_assert (divisor > 0);
14311 if (divisor == 1)
14312 return value;
14314 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14315 have to do anything. Only do this when we are not given a const,
14316 because in that case, this check is more expensive than just
14317 doing it. */
14318 if (TREE_CODE (value) != INTEGER_CST)
14320 div = build_int_cst (TREE_TYPE (value), divisor);
14322 if (multiple_of_p (TREE_TYPE (value), value, div))
14323 return value;
14326 /* If divisor is a power of two, simplify this to bit manipulation. */
14327 if (pow2_or_zerop (divisor))
14329 tree t;
14331 t = build_int_cst (TREE_TYPE (value), -divisor);
14332 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14334 else
14336 if (!div)
14337 div = build_int_cst (TREE_TYPE (value), divisor);
14338 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14339 value = size_binop_loc (loc, MULT_EXPR, value, div);
14342 return value;
14345 /* Returns the pointer to the base of the object addressed by EXP and
14346 extracts the information about the offset of the access, storing it
14347 to PBITPOS and POFFSET. */
14349 static tree
14350 split_address_to_core_and_offset (tree exp,
14351 HOST_WIDE_INT *pbitpos, tree *poffset)
14353 tree core;
14354 machine_mode mode;
14355 int unsignedp, reversep, volatilep;
14356 HOST_WIDE_INT bitsize;
14357 location_t loc = EXPR_LOCATION (exp);
14359 if (TREE_CODE (exp) == ADDR_EXPR)
14361 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14362 poffset, &mode, &unsignedp, &reversep,
14363 &volatilep);
14364 core = build_fold_addr_expr_loc (loc, core);
14366 else
14368 core = exp;
14369 *pbitpos = 0;
14370 *poffset = NULL_TREE;
14373 return core;
14376 /* Returns true if addresses of E1 and E2 differ by a constant, false
14377 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14379 bool
14380 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14382 tree core1, core2;
14383 HOST_WIDE_INT bitpos1, bitpos2;
14384 tree toffset1, toffset2, tdiff, type;
14386 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14387 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14389 if (bitpos1 % BITS_PER_UNIT != 0
14390 || bitpos2 % BITS_PER_UNIT != 0
14391 || !operand_equal_p (core1, core2, 0))
14392 return false;
14394 if (toffset1 && toffset2)
14396 type = TREE_TYPE (toffset1);
14397 if (type != TREE_TYPE (toffset2))
14398 toffset2 = fold_convert (type, toffset2);
14400 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14401 if (!cst_and_fits_in_hwi (tdiff))
14402 return false;
14404 *diff = int_cst_value (tdiff);
14406 else if (toffset1 || toffset2)
14408 /* If only one of the offsets is non-constant, the difference cannot
14409 be a constant. */
14410 return false;
14412 else
14413 *diff = 0;
14415 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14416 return true;
14419 /* Return OFF converted to a pointer offset type suitable as offset for
14420 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14421 tree
14422 convert_to_ptrofftype_loc (location_t loc, tree off)
14424 return fold_convert_loc (loc, sizetype, off);
14427 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14428 tree
14429 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14431 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14432 ptr, convert_to_ptrofftype_loc (loc, off));
14435 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14436 tree
14437 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14439 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14440 ptr, size_int (off));
14443 /* Return a char pointer for a C string if it is a string constant
14444 or sum of string constant and integer constant. We only support
14445 string constants properly terminated with '\0' character.
14446 If STRLEN is a valid pointer, length (including terminating character)
14447 of returned string is stored to the argument. */
14449 const char *
14450 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14452 tree offset_node;
14454 if (strlen)
14455 *strlen = 0;
14457 src = string_constant (src, &offset_node);
14458 if (src == 0)
14459 return NULL;
14461 unsigned HOST_WIDE_INT offset = 0;
14462 if (offset_node != NULL_TREE)
14464 if (!tree_fits_uhwi_p (offset_node))
14465 return NULL;
14466 else
14467 offset = tree_to_uhwi (offset_node);
14470 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14471 const char *string = TREE_STRING_POINTER (src);
14473 /* Support only properly null-terminated strings. */
14474 if (string_length == 0
14475 || string[string_length - 1] != '\0'
14476 || offset >= string_length)
14477 return NULL;
14479 if (strlen)
14480 *strlen = string_length - offset;
14481 return string + offset;
14484 #if CHECKING_P
14486 namespace selftest {
14488 /* Helper functions for writing tests of folding trees. */
14490 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14492 static void
14493 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14494 tree constant)
14496 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14499 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14500 wrapping WRAPPED_EXPR. */
14502 static void
14503 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14504 tree wrapped_expr)
14506 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14507 ASSERT_NE (wrapped_expr, result);
14508 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14509 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14512 /* Verify that various arithmetic binary operations are folded
14513 correctly. */
14515 static void
14516 test_arithmetic_folding ()
14518 tree type = integer_type_node;
14519 tree x = create_tmp_var_raw (type, "x");
14520 tree zero = build_zero_cst (type);
14521 tree one = build_int_cst (type, 1);
14523 /* Addition. */
14524 /* 1 <-- (0 + 1) */
14525 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14526 one);
14527 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14528 one);
14530 /* (nonlvalue)x <-- (x + 0) */
14531 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14534 /* Subtraction. */
14535 /* 0 <-- (x - x) */
14536 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14537 zero);
14538 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14541 /* Multiplication. */
14542 /* 0 <-- (x * 0) */
14543 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14544 zero);
14546 /* (nonlvalue)x <-- (x * 1) */
14547 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14551 /* Verify that various binary operations on vectors are folded
14552 correctly. */
14554 static void
14555 test_vector_folding ()
14557 tree inner_type = integer_type_node;
14558 tree type = build_vector_type (inner_type, 4);
14559 tree zero = build_zero_cst (type);
14560 tree one = build_one_cst (type);
14562 /* Verify equality tests that return a scalar boolean result. */
14563 tree res_type = boolean_type_node;
14564 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14565 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14566 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14567 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14570 /* Run all of the selftests within this file. */
14572 void
14573 fold_const_c_tests ()
14575 test_arithmetic_folding ();
14576 test_vector_folding ();
14579 } // namespace selftest
14581 #endif /* CHECKING_P */