Update ChangeLog and version files for release
[official-gcc.git] / gcc / fold-const.c
blobbff39c7b3a5901534cbe181a455b470a156fe41e
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 "tm_p.h"
53 #include "tree-ssa-operands.h"
54 #include "optabs-query.h"
55 #include "cgraph.h"
56 #include "diagnostic-core.h"
57 #include "flags.h"
58 #include "alias.h"
59 #include "fold-const.h"
60 #include "fold-const-call.h"
61 #include "stor-layout.h"
62 #include "calls.h"
63 #include "tree-iterator.h"
64 #include "expr.h"
65 #include "intl.h"
66 #include "langhooks.h"
67 #include "tree-eh.h"
68 #include "gimplify.h"
69 #include "tree-dfa.h"
70 #include "builtins.h"
71 #include "generic-match.h"
72 #include "gimple-fold.h"
73 #include "params.h"
74 #include "tree-into-ssa.h"
75 #include "md5.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
78 #include "tree-ssanames.h"
80 #ifndef LOAD_EXTEND_OP
81 #define LOAD_EXTEND_OP(M) UNKNOWN
82 #endif
84 /* Nonzero if we are folding constants inside an initializer; zero
85 otherwise. */
86 int folding_initializer = 0;
88 /* The following constants represent a bit based encoding of GCC's
89 comparison operators. This encoding simplifies transformations
90 on relational comparison operators, such as AND and OR. */
91 enum comparison_code {
92 COMPCODE_FALSE = 0,
93 COMPCODE_LT = 1,
94 COMPCODE_EQ = 2,
95 COMPCODE_LE = 3,
96 COMPCODE_GT = 4,
97 COMPCODE_LTGT = 5,
98 COMPCODE_GE = 6,
99 COMPCODE_ORD = 7,
100 COMPCODE_UNORD = 8,
101 COMPCODE_UNLT = 9,
102 COMPCODE_UNEQ = 10,
103 COMPCODE_UNLE = 11,
104 COMPCODE_UNGT = 12,
105 COMPCODE_NE = 13,
106 COMPCODE_UNGE = 14,
107 COMPCODE_TRUE = 15
110 static bool negate_expr_p (tree);
111 static tree negate_expr (tree);
112 static tree split_tree (location_t, tree, tree, enum tree_code,
113 tree *, tree *, tree *, int);
114 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
115 static enum comparison_code comparison_to_compcode (enum tree_code);
116 static enum tree_code compcode_to_comparison (enum comparison_code);
117 static int operand_equal_for_comparison_p (tree, tree, tree);
118 static int twoval_comparison_p (tree, tree *, tree *, int *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree optimize_bit_field_compare (location_t, enum tree_code,
121 tree, tree, tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree optimize_minmax_comparison (location_t, enum tree_code,
131 tree, tree, tree);
132 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
134 static tree fold_binary_op_with_conditional_arg (location_t,
135 enum tree_code, tree,
136 tree, tree,
137 tree, tree, int);
138 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
139 static bool reorder_operands_p (const_tree, const_tree);
140 static tree fold_negate_const (tree, tree);
141 static tree fold_not_const (const_tree, tree);
142 static tree fold_relational_const (enum tree_code, tree, tree, tree);
143 static tree fold_convert_const (enum tree_code, tree, tree);
144 static tree fold_view_convert_expr (tree, tree);
145 static bool vec_cst_ctor_to_array (tree, tree *);
148 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
149 Otherwise, return LOC. */
151 static location_t
152 expr_location_or (tree t, location_t loc)
154 location_t tloc = EXPR_LOCATION (t);
155 return tloc == UNKNOWN_LOCATION ? loc : tloc;
158 /* Similar to protected_set_expr_location, but never modify x in place,
159 if location can and needs to be set, unshare it. */
161 static inline tree
162 protected_set_expr_location_unshare (tree x, location_t loc)
164 if (CAN_HAVE_LOCATION_P (x)
165 && EXPR_LOCATION (x) != loc
166 && !(TREE_CODE (x) == SAVE_EXPR
167 || TREE_CODE (x) == TARGET_EXPR
168 || TREE_CODE (x) == BIND_EXPR))
170 x = copy_node (x);
171 SET_EXPR_LOCATION (x, loc);
173 return x;
176 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
177 division and returns the quotient. Otherwise returns
178 NULL_TREE. */
180 tree
181 div_if_zero_remainder (const_tree arg1, const_tree arg2)
183 widest_int quo;
185 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
186 SIGNED, &quo))
187 return wide_int_to_tree (TREE_TYPE (arg1), quo);
189 return NULL_TREE;
192 /* This is nonzero if we should defer warnings about undefined
193 overflow. This facility exists because these warnings are a
194 special case. The code to estimate loop iterations does not want
195 to issue any warnings, since it works with expressions which do not
196 occur in user code. Various bits of cleanup code call fold(), but
197 only use the result if it has certain characteristics (e.g., is a
198 constant); that code only wants to issue a warning if the result is
199 used. */
201 static int fold_deferring_overflow_warnings;
203 /* If a warning about undefined overflow is deferred, this is the
204 warning. Note that this may cause us to turn two warnings into
205 one, but that is fine since it is sufficient to only give one
206 warning per expression. */
208 static const char* fold_deferred_overflow_warning;
210 /* If a warning about undefined overflow is deferred, this is the
211 level at which the warning should be emitted. */
213 static enum warn_strict_overflow_code fold_deferred_overflow_code;
215 /* Start deferring overflow warnings. We could use a stack here to
216 permit nested calls, but at present it is not necessary. */
218 void
219 fold_defer_overflow_warnings (void)
221 ++fold_deferring_overflow_warnings;
224 /* Stop deferring overflow warnings. If there is a pending warning,
225 and ISSUE is true, then issue the warning if appropriate. STMT is
226 the statement with which the warning should be associated (used for
227 location information); STMT may be NULL. CODE is the level of the
228 warning--a warn_strict_overflow_code value. This function will use
229 the smaller of CODE and the deferred code when deciding whether to
230 issue the warning. CODE may be zero to mean to always use the
231 deferred code. */
233 void
234 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
236 const char *warnmsg;
237 location_t locus;
239 gcc_assert (fold_deferring_overflow_warnings > 0);
240 --fold_deferring_overflow_warnings;
241 if (fold_deferring_overflow_warnings > 0)
243 if (fold_deferred_overflow_warning != NULL
244 && code != 0
245 && code < (int) fold_deferred_overflow_code)
246 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
247 return;
250 warnmsg = fold_deferred_overflow_warning;
251 fold_deferred_overflow_warning = NULL;
253 if (!issue || warnmsg == NULL)
254 return;
256 if (gimple_no_warning_p (stmt))
257 return;
259 /* Use the smallest code level when deciding to issue the
260 warning. */
261 if (code == 0 || code > (int) fold_deferred_overflow_code)
262 code = fold_deferred_overflow_code;
264 if (!issue_strict_overflow_warning (code))
265 return;
267 if (stmt == NULL)
268 locus = input_location;
269 else
270 locus = gimple_location (stmt);
271 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
274 /* Stop deferring overflow warnings, ignoring any deferred
275 warnings. */
277 void
278 fold_undefer_and_ignore_overflow_warnings (void)
280 fold_undefer_overflow_warnings (false, NULL, 0);
283 /* Whether we are deferring overflow warnings. */
285 bool
286 fold_deferring_overflow_warnings_p (void)
288 return fold_deferring_overflow_warnings > 0;
291 /* This is called when we fold something based on the fact that signed
292 overflow is undefined. */
294 static void
295 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
297 if (fold_deferring_overflow_warnings > 0)
299 if (fold_deferred_overflow_warning == NULL
300 || wc < fold_deferred_overflow_code)
302 fold_deferred_overflow_warning = gmsgid;
303 fold_deferred_overflow_code = wc;
306 else if (issue_strict_overflow_warning (wc))
307 warning (OPT_Wstrict_overflow, gmsgid);
310 /* Return true if the built-in mathematical function specified by CODE
311 is odd, i.e. -f(x) == f(-x). */
313 bool
314 negate_mathfn_p (combined_fn fn)
316 switch (fn)
318 CASE_CFN_ASIN:
319 CASE_CFN_ASINH:
320 CASE_CFN_ATAN:
321 CASE_CFN_ATANH:
322 CASE_CFN_CASIN:
323 CASE_CFN_CASINH:
324 CASE_CFN_CATAN:
325 CASE_CFN_CATANH:
326 CASE_CFN_CBRT:
327 CASE_CFN_CPROJ:
328 CASE_CFN_CSIN:
329 CASE_CFN_CSINH:
330 CASE_CFN_CTAN:
331 CASE_CFN_CTANH:
332 CASE_CFN_ERF:
333 CASE_CFN_LLROUND:
334 CASE_CFN_LROUND:
335 CASE_CFN_ROUND:
336 CASE_CFN_SIN:
337 CASE_CFN_SINH:
338 CASE_CFN_TAN:
339 CASE_CFN_TANH:
340 CASE_CFN_TRUNC:
341 return true;
343 CASE_CFN_LLRINT:
344 CASE_CFN_LRINT:
345 CASE_CFN_NEARBYINT:
346 CASE_CFN_RINT:
347 return !flag_rounding_math;
349 default:
350 break;
352 return false;
355 /* Check whether we may negate an integer constant T without causing
356 overflow. */
358 bool
359 may_negate_without_overflow_p (const_tree t)
361 tree type;
363 gcc_assert (TREE_CODE (t) == INTEGER_CST);
365 type = TREE_TYPE (t);
366 if (TYPE_UNSIGNED (type))
367 return false;
369 return !wi::only_sign_bit_p (t);
372 /* Determine whether an expression T can be cheaply negated using
373 the function negate_expr without introducing undefined overflow. */
375 static bool
376 negate_expr_p (tree t)
378 tree type;
380 if (t == 0)
381 return false;
383 type = TREE_TYPE (t);
385 STRIP_SIGN_NOPS (t);
386 switch (TREE_CODE (t))
388 case INTEGER_CST:
389 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
390 return true;
392 /* Check that -CST will not overflow type. */
393 return may_negate_without_overflow_p (t);
394 case BIT_NOT_EXPR:
395 return (INTEGRAL_TYPE_P (type)
396 && TYPE_OVERFLOW_WRAPS (type));
398 case FIXED_CST:
399 return true;
401 case NEGATE_EXPR:
402 return !TYPE_OVERFLOW_SANITIZED (type);
404 case REAL_CST:
405 /* We want to canonicalize to positive real constants. Pretend
406 that only negative ones can be easily negated. */
407 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
409 case COMPLEX_CST:
410 return negate_expr_p (TREE_REALPART (t))
411 && negate_expr_p (TREE_IMAGPART (t));
413 case VECTOR_CST:
415 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
416 return true;
418 int count = TYPE_VECTOR_SUBPARTS (type), i;
420 for (i = 0; i < count; i++)
421 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
422 return false;
424 return true;
427 case COMPLEX_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0))
429 && negate_expr_p (TREE_OPERAND (t, 1));
431 case CONJ_EXPR:
432 return negate_expr_p (TREE_OPERAND (t, 0));
434 case PLUS_EXPR:
435 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
436 || HONOR_SIGNED_ZEROS (element_mode (type))
437 || (INTEGRAL_TYPE_P (type)
438 && ! TYPE_OVERFLOW_WRAPS (type)))
439 return false;
440 /* -(A + B) -> (-B) - A. */
441 if (negate_expr_p (TREE_OPERAND (t, 1))
442 && reorder_operands_p (TREE_OPERAND (t, 0),
443 TREE_OPERAND (t, 1)))
444 return true;
445 /* -(A + B) -> (-A) - B. */
446 return negate_expr_p (TREE_OPERAND (t, 0));
448 case MINUS_EXPR:
449 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
450 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
451 && !HONOR_SIGNED_ZEROS (element_mode (type))
452 && (! INTEGRAL_TYPE_P (type)
453 || TYPE_OVERFLOW_WRAPS (type))
454 && reorder_operands_p (TREE_OPERAND (t, 0),
455 TREE_OPERAND (t, 1));
457 case MULT_EXPR:
458 if (TYPE_UNSIGNED (type))
459 break;
460 /* INT_MIN/n * n doesn't overflow while negating one operand it does
461 if n is a (negative) power of two. */
462 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
463 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
464 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
465 && wi::popcount (wi::abs (TREE_OPERAND (t, 0))) != 1)
466 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
467 && wi::popcount (wi::abs (TREE_OPERAND (t, 1))) != 1)))
468 break;
470 /* Fall through. */
472 case RDIV_EXPR:
473 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
474 return negate_expr_p (TREE_OPERAND (t, 1))
475 || negate_expr_p (TREE_OPERAND (t, 0));
476 break;
478 case TRUNC_DIV_EXPR:
479 case ROUND_DIV_EXPR:
480 case EXACT_DIV_EXPR:
481 if (TYPE_UNSIGNED (type))
482 break;
483 if (negate_expr_p (TREE_OPERAND (t, 0)))
484 return true;
485 /* In general we can't negate B in A / B, because if A is INT_MIN and
486 B is 1, we may turn this into INT_MIN / -1 which is undefined
487 and actually traps on some architectures. */
488 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
489 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
490 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
491 && ! integer_onep (TREE_OPERAND (t, 1))))
492 return negate_expr_p (TREE_OPERAND (t, 1));
493 break;
495 case NOP_EXPR:
496 /* Negate -((double)float) as (double)(-float). */
497 if (TREE_CODE (type) == REAL_TYPE)
499 tree tem = strip_float_extensions (t);
500 if (tem != t)
501 return negate_expr_p (tem);
503 break;
505 case CALL_EXPR:
506 /* Negate -f(x) as f(-x). */
507 if (negate_mathfn_p (get_call_combined_fn (t)))
508 return negate_expr_p (CALL_EXPR_ARG (t, 0));
509 break;
511 case RSHIFT_EXPR:
512 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
513 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
515 tree op1 = TREE_OPERAND (t, 1);
516 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
517 return true;
519 break;
521 default:
522 break;
524 return false;
527 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
528 simplification is possible.
529 If negate_expr_p would return true for T, NULL_TREE will never be
530 returned. */
532 static tree
533 fold_negate_expr (location_t loc, tree t)
535 tree type = TREE_TYPE (t);
536 tree tem;
538 switch (TREE_CODE (t))
540 /* Convert - (~A) to A + 1. */
541 case BIT_NOT_EXPR:
542 if (INTEGRAL_TYPE_P (type))
543 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
544 build_one_cst (type));
545 break;
547 case INTEGER_CST:
548 tem = fold_negate_const (t, type);
549 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
550 || (ANY_INTEGRAL_TYPE_P (type)
551 && !TYPE_OVERFLOW_TRAPS (type)
552 && TYPE_OVERFLOW_WRAPS (type))
553 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
554 return tem;
555 break;
557 case REAL_CST:
558 tem = fold_negate_const (t, type);
559 return tem;
561 case FIXED_CST:
562 tem = fold_negate_const (t, type);
563 return tem;
565 case COMPLEX_CST:
567 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
568 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
569 if (rpart && ipart)
570 return build_complex (type, rpart, ipart);
572 break;
574 case VECTOR_CST:
576 int count = TYPE_VECTOR_SUBPARTS (type), i;
577 tree *elts = XALLOCAVEC (tree, count);
579 for (i = 0; i < count; i++)
581 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
582 if (elts[i] == NULL_TREE)
583 return NULL_TREE;
586 return build_vector (type, elts);
589 case COMPLEX_EXPR:
590 if (negate_expr_p (t))
591 return fold_build2_loc (loc, COMPLEX_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
593 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
594 break;
596 case CONJ_EXPR:
597 if (negate_expr_p (t))
598 return fold_build1_loc (loc, CONJ_EXPR, type,
599 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
600 break;
602 case NEGATE_EXPR:
603 if (!TYPE_OVERFLOW_SANITIZED (type))
604 return TREE_OPERAND (t, 0);
605 break;
607 case PLUS_EXPR:
608 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
609 && !HONOR_SIGNED_ZEROS (element_mode (type)))
611 /* -(A + B) -> (-B) - A. */
612 if (negate_expr_p (TREE_OPERAND (t, 1))
613 && reorder_operands_p (TREE_OPERAND (t, 0),
614 TREE_OPERAND (t, 1)))
616 tem = negate_expr (TREE_OPERAND (t, 1));
617 return fold_build2_loc (loc, MINUS_EXPR, type,
618 tem, TREE_OPERAND (t, 0));
621 /* -(A + B) -> (-A) - B. */
622 if (negate_expr_p (TREE_OPERAND (t, 0)))
624 tem = negate_expr (TREE_OPERAND (t, 0));
625 return fold_build2_loc (loc, MINUS_EXPR, type,
626 tem, TREE_OPERAND (t, 1));
629 break;
631 case MINUS_EXPR:
632 /* - (A - B) -> B - A */
633 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
634 && !HONOR_SIGNED_ZEROS (element_mode (type))
635 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
636 return fold_build2_loc (loc, MINUS_EXPR, type,
637 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
638 break;
640 case MULT_EXPR:
641 if (TYPE_UNSIGNED (type))
642 break;
644 /* Fall through. */
646 case RDIV_EXPR:
647 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
649 tem = TREE_OPERAND (t, 1);
650 if (negate_expr_p (tem))
651 return fold_build2_loc (loc, TREE_CODE (t), type,
652 TREE_OPERAND (t, 0), negate_expr (tem));
653 tem = TREE_OPERAND (t, 0);
654 if (negate_expr_p (tem))
655 return fold_build2_loc (loc, TREE_CODE (t), type,
656 negate_expr (tem), TREE_OPERAND (t, 1));
658 break;
660 case TRUNC_DIV_EXPR:
661 case ROUND_DIV_EXPR:
662 case EXACT_DIV_EXPR:
663 if (TYPE_UNSIGNED (type))
664 break;
665 if (negate_expr_p (TREE_OPERAND (t, 0)))
666 return fold_build2_loc (loc, TREE_CODE (t), type,
667 negate_expr (TREE_OPERAND (t, 0)),
668 TREE_OPERAND (t, 1));
669 /* In general we can't negate B in A / B, because if A is INT_MIN and
670 B is 1, we may turn this into INT_MIN / -1 which is undefined
671 and actually traps on some architectures. */
672 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
673 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
674 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
675 && ! integer_onep (TREE_OPERAND (t, 1))))
676 && negate_expr_p (TREE_OPERAND (t, 1)))
677 return fold_build2_loc (loc, TREE_CODE (t), type,
678 TREE_OPERAND (t, 0),
679 negate_expr (TREE_OPERAND (t, 1)));
680 break;
682 case NOP_EXPR:
683 /* Convert -((double)float) into (double)(-float). */
684 if (TREE_CODE (type) == REAL_TYPE)
686 tem = strip_float_extensions (t);
687 if (tem != t && negate_expr_p (tem))
688 return fold_convert_loc (loc, type, negate_expr (tem));
690 break;
692 case CALL_EXPR:
693 /* Negate -f(x) as f(-x). */
694 if (negate_mathfn_p (get_call_combined_fn (t))
695 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
697 tree fndecl, arg;
699 fndecl = get_callee_fndecl (t);
700 arg = negate_expr (CALL_EXPR_ARG (t, 0));
701 return build_call_expr_loc (loc, fndecl, 1, arg);
703 break;
705 case RSHIFT_EXPR:
706 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
707 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
709 tree op1 = TREE_OPERAND (t, 1);
710 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
712 tree ntype = TYPE_UNSIGNED (type)
713 ? signed_type_for (type)
714 : unsigned_type_for (type);
715 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
716 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
717 return fold_convert_loc (loc, type, temp);
720 break;
722 default:
723 break;
726 return NULL_TREE;
729 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
730 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
731 return NULL_TREE. */
733 static tree
734 negate_expr (tree t)
736 tree type, tem;
737 location_t loc;
739 if (t == NULL_TREE)
740 return NULL_TREE;
742 loc = EXPR_LOCATION (t);
743 type = TREE_TYPE (t);
744 STRIP_SIGN_NOPS (t);
746 tem = fold_negate_expr (loc, t);
747 if (!tem)
748 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
749 return fold_convert_loc (loc, type, tem);
752 /* Split a tree IN into a constant, literal and variable parts that could be
753 combined with CODE to make IN. "constant" means an expression with
754 TREE_CONSTANT but that isn't an actual constant. CODE must be a
755 commutative arithmetic operation. Store the constant part into *CONP,
756 the literal in *LITP and return the variable part. If a part isn't
757 present, set it to null. If the tree does not decompose in this way,
758 return the entire tree as the variable part and the other parts as null.
760 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
761 case, we negate an operand that was subtracted. Except if it is a
762 literal for which we use *MINUS_LITP instead.
764 If NEGATE_P is true, we are negating all of IN, again except a literal
765 for which we use *MINUS_LITP instead. If a variable part is of pointer
766 type, it is negated after converting to TYPE. This prevents us from
767 generating illegal MINUS pointer expression. LOC is the location of
768 the converted variable part.
770 If IN is itself a literal or constant, return it as appropriate.
772 Note that we do not guarantee that any of the three values will be the
773 same type as IN, but they will have the same signedness and mode. */
775 static tree
776 split_tree (location_t loc, tree in, tree type, enum tree_code code,
777 tree *conp, tree *litp, tree *minus_litp, int negate_p)
779 tree var = 0;
781 *conp = 0;
782 *litp = 0;
783 *minus_litp = 0;
785 /* Strip any conversions that don't change the machine mode or signedness. */
786 STRIP_SIGN_NOPS (in);
788 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
789 || TREE_CODE (in) == FIXED_CST)
790 *litp = in;
791 else if (TREE_CODE (in) == code
792 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
793 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
794 /* We can associate addition and subtraction together (even
795 though the C standard doesn't say so) for integers because
796 the value is not affected. For reals, the value might be
797 affected, so we can't. */
798 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
799 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
801 tree op0 = TREE_OPERAND (in, 0);
802 tree op1 = TREE_OPERAND (in, 1);
803 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
804 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
806 /* First see if either of the operands is a literal, then a constant. */
807 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
808 || TREE_CODE (op0) == FIXED_CST)
809 *litp = op0, op0 = 0;
810 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
811 || TREE_CODE (op1) == FIXED_CST)
812 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
814 if (op0 != 0 && TREE_CONSTANT (op0))
815 *conp = op0, op0 = 0;
816 else if (op1 != 0 && TREE_CONSTANT (op1))
817 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
819 /* If we haven't dealt with either operand, this is not a case we can
820 decompose. Otherwise, VAR is either of the ones remaining, if any. */
821 if (op0 != 0 && op1 != 0)
822 var = in;
823 else if (op0 != 0)
824 var = op0;
825 else
826 var = op1, neg_var_p = neg1_p;
828 /* Now do any needed negations. */
829 if (neg_litp_p)
830 *minus_litp = *litp, *litp = 0;
831 if (neg_conp_p)
832 *conp = negate_expr (*conp);
833 if (neg_var_p && var)
835 /* Convert to TYPE before negating. */
836 var = fold_convert_loc (loc, type, var);
837 var = negate_expr (var);
840 else if (TREE_CONSTANT (in))
841 *conp = in;
842 else if (TREE_CODE (in) == BIT_NOT_EXPR
843 && code == PLUS_EXPR)
845 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
846 when IN is constant. */
847 *minus_litp = build_one_cst (TREE_TYPE (in));
848 var = negate_expr (TREE_OPERAND (in, 0));
850 else
851 var = in;
853 if (negate_p)
855 if (*litp)
856 *minus_litp = *litp, *litp = 0;
857 else if (*minus_litp)
858 *litp = *minus_litp, *minus_litp = 0;
859 *conp = negate_expr (*conp);
860 if (var)
862 /* Convert to TYPE before negating. */
863 var = fold_convert_loc (loc, type, var);
864 var = negate_expr (var);
868 return var;
871 /* Re-associate trees split by the above function. T1 and T2 are
872 either expressions to associate or null. Return the new
873 expression, if any. LOC is the location of the new expression. If
874 we build an operation, do it in TYPE and with CODE. */
876 static tree
877 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
879 if (t1 == 0)
880 return t2;
881 else if (t2 == 0)
882 return t1;
884 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
885 try to fold this since we will have infinite recursion. But do
886 deal with any NEGATE_EXPRs. */
887 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
888 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
890 if (code == PLUS_EXPR)
892 if (TREE_CODE (t1) == NEGATE_EXPR)
893 return build2_loc (loc, MINUS_EXPR, type,
894 fold_convert_loc (loc, type, t2),
895 fold_convert_loc (loc, type,
896 TREE_OPERAND (t1, 0)));
897 else if (TREE_CODE (t2) == NEGATE_EXPR)
898 return build2_loc (loc, MINUS_EXPR, type,
899 fold_convert_loc (loc, type, t1),
900 fold_convert_loc (loc, type,
901 TREE_OPERAND (t2, 0)));
902 else if (integer_zerop (t2))
903 return fold_convert_loc (loc, type, t1);
905 else if (code == MINUS_EXPR)
907 if (integer_zerop (t2))
908 return fold_convert_loc (loc, type, t1);
911 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
912 fold_convert_loc (loc, type, t2));
915 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type, t2));
919 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
920 for use in int_const_binop, size_binop and size_diffop. */
922 static bool
923 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
925 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
926 return false;
927 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
928 return false;
930 switch (code)
932 case LSHIFT_EXPR:
933 case RSHIFT_EXPR:
934 case LROTATE_EXPR:
935 case RROTATE_EXPR:
936 return true;
938 default:
939 break;
942 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
943 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
944 && TYPE_MODE (type1) == TYPE_MODE (type2);
948 /* Combine two integer constants ARG1 and ARG2 under operation CODE
949 to produce a new constant. Return NULL_TREE if we don't know how
950 to evaluate CODE at compile-time. */
952 static tree
953 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
954 int overflowable)
956 wide_int res;
957 tree t;
958 tree type = TREE_TYPE (arg1);
959 signop sign = TYPE_SIGN (type);
960 bool overflow = false;
962 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
963 TYPE_SIGN (TREE_TYPE (parg2)));
965 switch (code)
967 case BIT_IOR_EXPR:
968 res = wi::bit_or (arg1, arg2);
969 break;
971 case BIT_XOR_EXPR:
972 res = wi::bit_xor (arg1, arg2);
973 break;
975 case BIT_AND_EXPR:
976 res = wi::bit_and (arg1, arg2);
977 break;
979 case RSHIFT_EXPR:
980 case LSHIFT_EXPR:
981 if (wi::neg_p (arg2))
983 arg2 = -arg2;
984 if (code == RSHIFT_EXPR)
985 code = LSHIFT_EXPR;
986 else
987 code = RSHIFT_EXPR;
990 if (code == RSHIFT_EXPR)
991 /* It's unclear from the C standard whether shifts can overflow.
992 The following code ignores overflow; perhaps a C standard
993 interpretation ruling is needed. */
994 res = wi::rshift (arg1, arg2, sign);
995 else
996 res = wi::lshift (arg1, arg2);
997 break;
999 case RROTATE_EXPR:
1000 case LROTATE_EXPR:
1001 if (wi::neg_p (arg2))
1003 arg2 = -arg2;
1004 if (code == RROTATE_EXPR)
1005 code = LROTATE_EXPR;
1006 else
1007 code = RROTATE_EXPR;
1010 if (code == RROTATE_EXPR)
1011 res = wi::rrotate (arg1, arg2);
1012 else
1013 res = wi::lrotate (arg1, arg2);
1014 break;
1016 case PLUS_EXPR:
1017 res = wi::add (arg1, arg2, sign, &overflow);
1018 break;
1020 case MINUS_EXPR:
1021 res = wi::sub (arg1, arg2, sign, &overflow);
1022 break;
1024 case MULT_EXPR:
1025 res = wi::mul (arg1, arg2, sign, &overflow);
1026 break;
1028 case MULT_HIGHPART_EXPR:
1029 res = wi::mul_high (arg1, arg2, sign);
1030 break;
1032 case TRUNC_DIV_EXPR:
1033 case EXACT_DIV_EXPR:
1034 if (arg2 == 0)
1035 return NULL_TREE;
1036 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1037 break;
1039 case FLOOR_DIV_EXPR:
1040 if (arg2 == 0)
1041 return NULL_TREE;
1042 res = wi::div_floor (arg1, arg2, sign, &overflow);
1043 break;
1045 case CEIL_DIV_EXPR:
1046 if (arg2 == 0)
1047 return NULL_TREE;
1048 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1049 break;
1051 case ROUND_DIV_EXPR:
1052 if (arg2 == 0)
1053 return NULL_TREE;
1054 res = wi::div_round (arg1, arg2, sign, &overflow);
1055 break;
1057 case TRUNC_MOD_EXPR:
1058 if (arg2 == 0)
1059 return NULL_TREE;
1060 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1061 break;
1063 case FLOOR_MOD_EXPR:
1064 if (arg2 == 0)
1065 return NULL_TREE;
1066 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1067 break;
1069 case CEIL_MOD_EXPR:
1070 if (arg2 == 0)
1071 return NULL_TREE;
1072 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1073 break;
1075 case ROUND_MOD_EXPR:
1076 if (arg2 == 0)
1077 return NULL_TREE;
1078 res = wi::mod_round (arg1, arg2, sign, &overflow);
1079 break;
1081 case MIN_EXPR:
1082 res = wi::min (arg1, arg2, sign);
1083 break;
1085 case MAX_EXPR:
1086 res = wi::max (arg1, arg2, sign);
1087 break;
1089 default:
1090 return NULL_TREE;
1093 t = force_fit_type (type, res, overflowable,
1094 (((sign == SIGNED || overflowable == -1)
1095 && overflow)
1096 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1098 return t;
1101 tree
1102 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1104 return int_const_binop_1 (code, arg1, arg2, 1);
1107 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1108 constant. We assume ARG1 and ARG2 have the same data type, or at least
1109 are the same kind of constant and the same machine mode. Return zero if
1110 combining the constants is not allowed in the current operating mode. */
1112 static tree
1113 const_binop (enum tree_code code, tree arg1, tree arg2)
1115 /* Sanity check for the recursive cases. */
1116 if (!arg1 || !arg2)
1117 return NULL_TREE;
1119 STRIP_NOPS (arg1);
1120 STRIP_NOPS (arg2);
1122 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1124 if (code == POINTER_PLUS_EXPR)
1125 return int_const_binop (PLUS_EXPR,
1126 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1128 return int_const_binop (code, arg1, arg2);
1131 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1133 machine_mode mode;
1134 REAL_VALUE_TYPE d1;
1135 REAL_VALUE_TYPE d2;
1136 REAL_VALUE_TYPE value;
1137 REAL_VALUE_TYPE result;
1138 bool inexact;
1139 tree t, type;
1141 /* The following codes are handled by real_arithmetic. */
1142 switch (code)
1144 case PLUS_EXPR:
1145 case MINUS_EXPR:
1146 case MULT_EXPR:
1147 case RDIV_EXPR:
1148 case MIN_EXPR:
1149 case MAX_EXPR:
1150 break;
1152 default:
1153 return NULL_TREE;
1156 d1 = TREE_REAL_CST (arg1);
1157 d2 = TREE_REAL_CST (arg2);
1159 type = TREE_TYPE (arg1);
1160 mode = TYPE_MODE (type);
1162 /* Don't perform operation if we honor signaling NaNs and
1163 either operand is a signaling NaN. */
1164 if (HONOR_SNANS (mode)
1165 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1166 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1167 return NULL_TREE;
1169 /* Don't perform operation if it would raise a division
1170 by zero exception. */
1171 if (code == RDIV_EXPR
1172 && real_equal (&d2, &dconst0)
1173 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1174 return NULL_TREE;
1176 /* If either operand is a NaN, just return it. Otherwise, set up
1177 for floating-point trap; we return an overflow. */
1178 if (REAL_VALUE_ISNAN (d1))
1180 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1181 is off. */
1182 d1.signalling = 0;
1183 t = build_real (type, d1);
1184 return t;
1186 else if (REAL_VALUE_ISNAN (d2))
1188 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1189 is off. */
1190 d2.signalling = 0;
1191 t = build_real (type, d2);
1192 return t;
1195 inexact = real_arithmetic (&value, code, &d1, &d2);
1196 real_convert (&result, mode, &value);
1198 /* Don't constant fold this floating point operation if
1199 the result has overflowed and flag_trapping_math. */
1200 if (flag_trapping_math
1201 && MODE_HAS_INFINITIES (mode)
1202 && REAL_VALUE_ISINF (result)
1203 && !REAL_VALUE_ISINF (d1)
1204 && !REAL_VALUE_ISINF (d2))
1205 return NULL_TREE;
1207 /* Don't constant fold this floating point operation if the
1208 result may dependent upon the run-time rounding mode and
1209 flag_rounding_math is set, or if GCC's software emulation
1210 is unable to accurately represent the result. */
1211 if ((flag_rounding_math
1212 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1213 && (inexact || !real_identical (&result, &value)))
1214 return NULL_TREE;
1216 t = build_real (type, result);
1218 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1219 return t;
1222 if (TREE_CODE (arg1) == FIXED_CST)
1224 FIXED_VALUE_TYPE f1;
1225 FIXED_VALUE_TYPE f2;
1226 FIXED_VALUE_TYPE result;
1227 tree t, type;
1228 int sat_p;
1229 bool overflow_p;
1231 /* The following codes are handled by fixed_arithmetic. */
1232 switch (code)
1234 case PLUS_EXPR:
1235 case MINUS_EXPR:
1236 case MULT_EXPR:
1237 case TRUNC_DIV_EXPR:
1238 if (TREE_CODE (arg2) != FIXED_CST)
1239 return NULL_TREE;
1240 f2 = TREE_FIXED_CST (arg2);
1241 break;
1243 case LSHIFT_EXPR:
1244 case RSHIFT_EXPR:
1246 if (TREE_CODE (arg2) != INTEGER_CST)
1247 return NULL_TREE;
1248 wide_int w2 = arg2;
1249 f2.data.high = w2.elt (1);
1250 f2.data.low = w2.elt (0);
1251 f2.mode = SImode;
1253 break;
1255 default:
1256 return NULL_TREE;
1259 f1 = TREE_FIXED_CST (arg1);
1260 type = TREE_TYPE (arg1);
1261 sat_p = TYPE_SATURATING (type);
1262 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1263 t = build_fixed (type, result);
1264 /* Propagate overflow flags. */
1265 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1266 TREE_OVERFLOW (t) = 1;
1267 return t;
1270 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1272 tree type = TREE_TYPE (arg1);
1273 tree r1 = TREE_REALPART (arg1);
1274 tree i1 = TREE_IMAGPART (arg1);
1275 tree r2 = TREE_REALPART (arg2);
1276 tree i2 = TREE_IMAGPART (arg2);
1277 tree real, imag;
1279 switch (code)
1281 case PLUS_EXPR:
1282 case MINUS_EXPR:
1283 real = const_binop (code, r1, r2);
1284 imag = const_binop (code, i1, i2);
1285 break;
1287 case MULT_EXPR:
1288 if (COMPLEX_FLOAT_TYPE_P (type))
1289 return do_mpc_arg2 (arg1, arg2, type,
1290 /* do_nonfinite= */ folding_initializer,
1291 mpc_mul);
1293 real = const_binop (MINUS_EXPR,
1294 const_binop (MULT_EXPR, r1, r2),
1295 const_binop (MULT_EXPR, i1, i2));
1296 imag = const_binop (PLUS_EXPR,
1297 const_binop (MULT_EXPR, r1, i2),
1298 const_binop (MULT_EXPR, i1, r2));
1299 break;
1301 case RDIV_EXPR:
1302 if (COMPLEX_FLOAT_TYPE_P (type))
1303 return do_mpc_arg2 (arg1, arg2, type,
1304 /* do_nonfinite= */ folding_initializer,
1305 mpc_div);
1306 /* Fallthru ... */
1307 case TRUNC_DIV_EXPR:
1308 case CEIL_DIV_EXPR:
1309 case FLOOR_DIV_EXPR:
1310 case ROUND_DIV_EXPR:
1311 if (flag_complex_method == 0)
1313 /* Keep this algorithm in sync with
1314 tree-complex.c:expand_complex_div_straight().
1316 Expand complex division to scalars, straightforward algorithm.
1317 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1318 t = br*br + bi*bi
1320 tree magsquared
1321 = const_binop (PLUS_EXPR,
1322 const_binop (MULT_EXPR, r2, r2),
1323 const_binop (MULT_EXPR, i2, i2));
1324 tree t1
1325 = const_binop (PLUS_EXPR,
1326 const_binop (MULT_EXPR, r1, r2),
1327 const_binop (MULT_EXPR, i1, i2));
1328 tree t2
1329 = const_binop (MINUS_EXPR,
1330 const_binop (MULT_EXPR, i1, r2),
1331 const_binop (MULT_EXPR, r1, i2));
1333 real = const_binop (code, t1, magsquared);
1334 imag = const_binop (code, t2, magsquared);
1336 else
1338 /* Keep this algorithm in sync with
1339 tree-complex.c:expand_complex_div_wide().
1341 Expand complex division to scalars, modified algorithm to minimize
1342 overflow with wide input ranges. */
1343 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1344 fold_abs_const (r2, TREE_TYPE (type)),
1345 fold_abs_const (i2, TREE_TYPE (type)));
1347 if (integer_nonzerop (compare))
1349 /* In the TRUE branch, we compute
1350 ratio = br/bi;
1351 div = (br * ratio) + bi;
1352 tr = (ar * ratio) + ai;
1353 ti = (ai * ratio) - ar;
1354 tr = tr / div;
1355 ti = ti / div; */
1356 tree ratio = const_binop (code, r2, i2);
1357 tree div = const_binop (PLUS_EXPR, i2,
1358 const_binop (MULT_EXPR, r2, ratio));
1359 real = const_binop (MULT_EXPR, r1, ratio);
1360 real = const_binop (PLUS_EXPR, real, i1);
1361 real = const_binop (code, real, div);
1363 imag = const_binop (MULT_EXPR, i1, ratio);
1364 imag = const_binop (MINUS_EXPR, imag, r1);
1365 imag = const_binop (code, imag, div);
1367 else
1369 /* In the FALSE branch, we compute
1370 ratio = d/c;
1371 divisor = (d * ratio) + c;
1372 tr = (b * ratio) + a;
1373 ti = b - (a * ratio);
1374 tr = tr / div;
1375 ti = ti / div; */
1376 tree ratio = const_binop (code, i2, r2);
1377 tree div = const_binop (PLUS_EXPR, r2,
1378 const_binop (MULT_EXPR, i2, ratio));
1380 real = const_binop (MULT_EXPR, i1, ratio);
1381 real = const_binop (PLUS_EXPR, real, r1);
1382 real = const_binop (code, real, div);
1384 imag = const_binop (MULT_EXPR, r1, ratio);
1385 imag = const_binop (MINUS_EXPR, i1, imag);
1386 imag = const_binop (code, imag, div);
1389 break;
1391 default:
1392 return NULL_TREE;
1395 if (real && imag)
1396 return build_complex (type, real, imag);
1399 if (TREE_CODE (arg1) == VECTOR_CST
1400 && TREE_CODE (arg2) == VECTOR_CST)
1402 tree type = TREE_TYPE (arg1);
1403 int count = TYPE_VECTOR_SUBPARTS (type), i;
1404 tree *elts = XALLOCAVEC (tree, count);
1406 for (i = 0; i < count; i++)
1408 tree elem1 = VECTOR_CST_ELT (arg1, i);
1409 tree elem2 = VECTOR_CST_ELT (arg2, i);
1411 elts[i] = const_binop (code, elem1, elem2);
1413 /* It is possible that const_binop cannot handle the given
1414 code and return NULL_TREE */
1415 if (elts[i] == NULL_TREE)
1416 return NULL_TREE;
1419 return build_vector (type, elts);
1422 /* Shifts allow a scalar offset for a vector. */
1423 if (TREE_CODE (arg1) == VECTOR_CST
1424 && TREE_CODE (arg2) == INTEGER_CST)
1426 tree type = TREE_TYPE (arg1);
1427 int count = TYPE_VECTOR_SUBPARTS (type), i;
1428 tree *elts = XALLOCAVEC (tree, count);
1430 for (i = 0; i < count; i++)
1432 tree elem1 = VECTOR_CST_ELT (arg1, i);
1434 elts[i] = const_binop (code, elem1, arg2);
1436 /* It is possible that const_binop cannot handle the given
1437 code and return NULL_TREE. */
1438 if (elts[i] == NULL_TREE)
1439 return NULL_TREE;
1442 return build_vector (type, elts);
1444 return NULL_TREE;
1447 /* Overload that adds a TYPE parameter to be able to dispatch
1448 to fold_relational_const. */
1450 tree
1451 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1453 if (TREE_CODE_CLASS (code) == tcc_comparison)
1454 return fold_relational_const (code, type, arg1, arg2);
1456 /* ??? Until we make the const_binop worker take the type of the
1457 result as argument put those cases that need it here. */
1458 switch (code)
1460 case COMPLEX_EXPR:
1461 if ((TREE_CODE (arg1) == REAL_CST
1462 && TREE_CODE (arg2) == REAL_CST)
1463 || (TREE_CODE (arg1) == INTEGER_CST
1464 && TREE_CODE (arg2) == INTEGER_CST))
1465 return build_complex (type, arg1, arg2);
1466 return NULL_TREE;
1468 case VEC_PACK_TRUNC_EXPR:
1469 case VEC_PACK_FIX_TRUNC_EXPR:
1471 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1472 tree *elts;
1474 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1475 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1476 if (TREE_CODE (arg1) != VECTOR_CST
1477 || TREE_CODE (arg2) != VECTOR_CST)
1478 return NULL_TREE;
1480 elts = XALLOCAVEC (tree, nelts);
1481 if (!vec_cst_ctor_to_array (arg1, elts)
1482 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1483 return NULL_TREE;
1485 for (i = 0; i < nelts; i++)
1487 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1488 ? NOP_EXPR : FIX_TRUNC_EXPR,
1489 TREE_TYPE (type), elts[i]);
1490 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1491 return NULL_TREE;
1494 return build_vector (type, elts);
1497 case VEC_WIDEN_MULT_LO_EXPR:
1498 case VEC_WIDEN_MULT_HI_EXPR:
1499 case VEC_WIDEN_MULT_EVEN_EXPR:
1500 case VEC_WIDEN_MULT_ODD_EXPR:
1502 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1503 unsigned int out, ofs, scale;
1504 tree *elts;
1506 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1507 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1508 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1509 return NULL_TREE;
1511 elts = XALLOCAVEC (tree, nelts * 4);
1512 if (!vec_cst_ctor_to_array (arg1, elts)
1513 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1514 return NULL_TREE;
1516 if (code == VEC_WIDEN_MULT_LO_EXPR)
1517 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1518 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1519 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1520 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1521 scale = 1, ofs = 0;
1522 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1523 scale = 1, ofs = 1;
1525 for (out = 0; out < nelts; out++)
1527 unsigned int in1 = (out << scale) + ofs;
1528 unsigned int in2 = in1 + nelts * 2;
1529 tree t1, t2;
1531 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1532 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1534 if (t1 == NULL_TREE || t2 == NULL_TREE)
1535 return NULL_TREE;
1536 elts[out] = const_binop (MULT_EXPR, t1, t2);
1537 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1538 return NULL_TREE;
1541 return build_vector (type, elts);
1544 default:;
1547 if (TREE_CODE_CLASS (code) != tcc_binary)
1548 return NULL_TREE;
1550 /* Make sure type and arg0 have the same saturating flag. */
1551 gcc_checking_assert (TYPE_SATURATING (type)
1552 == TYPE_SATURATING (TREE_TYPE (arg1)));
1554 return const_binop (code, arg1, arg2);
1557 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1558 Return zero if computing the constants is not possible. */
1560 tree
1561 const_unop (enum tree_code code, tree type, tree arg0)
1563 /* Don't perform the operation, other than NEGATE and ABS, if
1564 flag_signaling_nans is on and the operand is a signaling NaN. */
1565 if (TREE_CODE (arg0) == REAL_CST
1566 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1567 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1568 && code != NEGATE_EXPR
1569 && code != ABS_EXPR)
1570 return NULL_TREE;
1572 switch (code)
1574 CASE_CONVERT:
1575 case FLOAT_EXPR:
1576 case FIX_TRUNC_EXPR:
1577 case FIXED_CONVERT_EXPR:
1578 return fold_convert_const (code, type, arg0);
1580 case ADDR_SPACE_CONVERT_EXPR:
1581 /* If the source address is 0, and the source address space
1582 cannot have a valid object at 0, fold to dest type null. */
1583 if (integer_zerop (arg0)
1584 && !(targetm.addr_space.zero_address_valid
1585 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1586 return fold_convert_const (code, type, arg0);
1587 break;
1589 case VIEW_CONVERT_EXPR:
1590 return fold_view_convert_expr (type, arg0);
1592 case NEGATE_EXPR:
1594 /* Can't call fold_negate_const directly here as that doesn't
1595 handle all cases and we might not be able to negate some
1596 constants. */
1597 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1598 if (tem && CONSTANT_CLASS_P (tem))
1599 return tem;
1600 break;
1603 case ABS_EXPR:
1604 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1605 return fold_abs_const (arg0, type);
1606 break;
1608 case CONJ_EXPR:
1609 if (TREE_CODE (arg0) == COMPLEX_CST)
1611 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1612 TREE_TYPE (type));
1613 return build_complex (type, TREE_REALPART (arg0), ipart);
1615 break;
1617 case BIT_NOT_EXPR:
1618 if (TREE_CODE (arg0) == INTEGER_CST)
1619 return fold_not_const (arg0, type);
1620 /* Perform BIT_NOT_EXPR on each element individually. */
1621 else if (TREE_CODE (arg0) == VECTOR_CST)
1623 tree *elements;
1624 tree elem;
1625 unsigned count = VECTOR_CST_NELTS (arg0), i;
1627 elements = XALLOCAVEC (tree, count);
1628 for (i = 0; i < count; i++)
1630 elem = VECTOR_CST_ELT (arg0, i);
1631 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1632 if (elem == NULL_TREE)
1633 break;
1634 elements[i] = elem;
1636 if (i == count)
1637 return build_vector (type, elements);
1639 break;
1641 case TRUTH_NOT_EXPR:
1642 if (TREE_CODE (arg0) == INTEGER_CST)
1643 return constant_boolean_node (integer_zerop (arg0), type);
1644 break;
1646 case REALPART_EXPR:
1647 if (TREE_CODE (arg0) == COMPLEX_CST)
1648 return fold_convert (type, TREE_REALPART (arg0));
1649 break;
1651 case IMAGPART_EXPR:
1652 if (TREE_CODE (arg0) == COMPLEX_CST)
1653 return fold_convert (type, TREE_IMAGPART (arg0));
1654 break;
1656 case VEC_UNPACK_LO_EXPR:
1657 case VEC_UNPACK_HI_EXPR:
1658 case VEC_UNPACK_FLOAT_LO_EXPR:
1659 case VEC_UNPACK_FLOAT_HI_EXPR:
1661 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1662 tree *elts;
1663 enum tree_code subcode;
1665 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1666 if (TREE_CODE (arg0) != VECTOR_CST)
1667 return NULL_TREE;
1669 elts = XALLOCAVEC (tree, nelts * 2);
1670 if (!vec_cst_ctor_to_array (arg0, elts))
1671 return NULL_TREE;
1673 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1674 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1675 elts += nelts;
1677 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1678 subcode = NOP_EXPR;
1679 else
1680 subcode = FLOAT_EXPR;
1682 for (i = 0; i < nelts; i++)
1684 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1685 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1686 return NULL_TREE;
1689 return build_vector (type, elts);
1692 case REDUC_MIN_EXPR:
1693 case REDUC_MAX_EXPR:
1694 case REDUC_PLUS_EXPR:
1696 unsigned int nelts, i;
1697 tree *elts;
1698 enum tree_code subcode;
1700 if (TREE_CODE (arg0) != VECTOR_CST)
1701 return NULL_TREE;
1702 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1704 elts = XALLOCAVEC (tree, nelts);
1705 if (!vec_cst_ctor_to_array (arg0, elts))
1706 return NULL_TREE;
1708 switch (code)
1710 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1711 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1712 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1713 default: gcc_unreachable ();
1716 for (i = 1; i < nelts; i++)
1718 elts[0] = const_binop (subcode, elts[0], elts[i]);
1719 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1720 return NULL_TREE;
1723 return elts[0];
1726 default:
1727 break;
1730 return NULL_TREE;
1733 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1734 indicates which particular sizetype to create. */
1736 tree
1737 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1739 return build_int_cst (sizetype_tab[(int) kind], number);
1742 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1743 is a tree code. The type of the result is taken from the operands.
1744 Both must be equivalent integer types, ala int_binop_types_match_p.
1745 If the operands are constant, so is the result. */
1747 tree
1748 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1750 tree type = TREE_TYPE (arg0);
1752 if (arg0 == error_mark_node || arg1 == error_mark_node)
1753 return error_mark_node;
1755 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1756 TREE_TYPE (arg1)));
1758 /* Handle the special case of two integer constants faster. */
1759 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1761 /* And some specific cases even faster than that. */
1762 if (code == PLUS_EXPR)
1764 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1765 return arg1;
1766 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1767 return arg0;
1769 else if (code == MINUS_EXPR)
1771 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1772 return arg0;
1774 else if (code == MULT_EXPR)
1776 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1777 return arg1;
1780 /* Handle general case of two integer constants. For sizetype
1781 constant calculations we always want to know about overflow,
1782 even in the unsigned case. */
1783 return int_const_binop_1 (code, arg0, arg1, -1);
1786 return fold_build2_loc (loc, code, type, arg0, arg1);
1789 /* Given two values, either both of sizetype or both of bitsizetype,
1790 compute the difference between the two values. Return the value
1791 in signed type corresponding to the type of the operands. */
1793 tree
1794 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1796 tree type = TREE_TYPE (arg0);
1797 tree ctype;
1799 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1800 TREE_TYPE (arg1)));
1802 /* If the type is already signed, just do the simple thing. */
1803 if (!TYPE_UNSIGNED (type))
1804 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1806 if (type == sizetype)
1807 ctype = ssizetype;
1808 else if (type == bitsizetype)
1809 ctype = sbitsizetype;
1810 else
1811 ctype = signed_type_for (type);
1813 /* If either operand is not a constant, do the conversions to the signed
1814 type and subtract. The hardware will do the right thing with any
1815 overflow in the subtraction. */
1816 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1817 return size_binop_loc (loc, MINUS_EXPR,
1818 fold_convert_loc (loc, ctype, arg0),
1819 fold_convert_loc (loc, ctype, arg1));
1821 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1822 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1823 overflow) and negate (which can't either). Special-case a result
1824 of zero while we're here. */
1825 if (tree_int_cst_equal (arg0, arg1))
1826 return build_int_cst (ctype, 0);
1827 else if (tree_int_cst_lt (arg1, arg0))
1828 return fold_convert_loc (loc, ctype,
1829 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1830 else
1831 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1832 fold_convert_loc (loc, ctype,
1833 size_binop_loc (loc,
1834 MINUS_EXPR,
1835 arg1, arg0)));
1838 /* A subroutine of fold_convert_const handling conversions of an
1839 INTEGER_CST to another integer type. */
1841 static tree
1842 fold_convert_const_int_from_int (tree type, const_tree arg1)
1844 /* Given an integer constant, make new constant with new type,
1845 appropriately sign-extended or truncated. Use widest_int
1846 so that any extension is done according ARG1's type. */
1847 return force_fit_type (type, wi::to_widest (arg1),
1848 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1849 TREE_OVERFLOW (arg1));
1852 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1853 to an integer type. */
1855 static tree
1856 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1858 bool overflow = false;
1859 tree t;
1861 /* The following code implements the floating point to integer
1862 conversion rules required by the Java Language Specification,
1863 that IEEE NaNs are mapped to zero and values that overflow
1864 the target precision saturate, i.e. values greater than
1865 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1866 are mapped to INT_MIN. These semantics are allowed by the
1867 C and C++ standards that simply state that the behavior of
1868 FP-to-integer conversion is unspecified upon overflow. */
1870 wide_int val;
1871 REAL_VALUE_TYPE r;
1872 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1874 switch (code)
1876 case FIX_TRUNC_EXPR:
1877 real_trunc (&r, VOIDmode, &x);
1878 break;
1880 default:
1881 gcc_unreachable ();
1884 /* If R is NaN, return zero and show we have an overflow. */
1885 if (REAL_VALUE_ISNAN (r))
1887 overflow = true;
1888 val = wi::zero (TYPE_PRECISION (type));
1891 /* See if R is less than the lower bound or greater than the
1892 upper bound. */
1894 if (! overflow)
1896 tree lt = TYPE_MIN_VALUE (type);
1897 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1898 if (real_less (&r, &l))
1900 overflow = true;
1901 val = lt;
1905 if (! overflow)
1907 tree ut = TYPE_MAX_VALUE (type);
1908 if (ut)
1910 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1911 if (real_less (&u, &r))
1913 overflow = true;
1914 val = ut;
1919 if (! overflow)
1920 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1922 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1923 return t;
1926 /* A subroutine of fold_convert_const handling conversions of a
1927 FIXED_CST to an integer type. */
1929 static tree
1930 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1932 tree t;
1933 double_int temp, temp_trunc;
1934 unsigned int mode;
1936 /* Right shift FIXED_CST to temp by fbit. */
1937 temp = TREE_FIXED_CST (arg1).data;
1938 mode = TREE_FIXED_CST (arg1).mode;
1939 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1941 temp = temp.rshift (GET_MODE_FBIT (mode),
1942 HOST_BITS_PER_DOUBLE_INT,
1943 SIGNED_FIXED_POINT_MODE_P (mode));
1945 /* Left shift temp to temp_trunc by fbit. */
1946 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1947 HOST_BITS_PER_DOUBLE_INT,
1948 SIGNED_FIXED_POINT_MODE_P (mode));
1950 else
1952 temp = double_int_zero;
1953 temp_trunc = double_int_zero;
1956 /* If FIXED_CST is negative, we need to round the value toward 0.
1957 By checking if the fractional bits are not zero to add 1 to temp. */
1958 if (SIGNED_FIXED_POINT_MODE_P (mode)
1959 && temp_trunc.is_negative ()
1960 && TREE_FIXED_CST (arg1).data != temp_trunc)
1961 temp += double_int_one;
1963 /* Given a fixed-point constant, make new constant with new type,
1964 appropriately sign-extended or truncated. */
1965 t = force_fit_type (type, temp, -1,
1966 (temp.is_negative ()
1967 && (TYPE_UNSIGNED (type)
1968 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1969 | TREE_OVERFLOW (arg1));
1971 return t;
1974 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1975 to another floating point type. */
1977 static tree
1978 fold_convert_const_real_from_real (tree type, const_tree arg1)
1980 REAL_VALUE_TYPE value;
1981 tree t;
1983 /* Don't perform the operation if flag_signaling_nans is on
1984 and the operand is a signaling NaN. */
1985 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1986 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1987 return NULL_TREE;
1989 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1990 t = build_real (type, value);
1992 /* If converting an infinity or NAN to a representation that doesn't
1993 have one, set the overflow bit so that we can produce some kind of
1994 error message at the appropriate point if necessary. It's not the
1995 most user-friendly message, but it's better than nothing. */
1996 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1997 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1998 TREE_OVERFLOW (t) = 1;
1999 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2000 && !MODE_HAS_NANS (TYPE_MODE (type)))
2001 TREE_OVERFLOW (t) = 1;
2002 /* Regular overflow, conversion produced an infinity in a mode that
2003 can't represent them. */
2004 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2005 && REAL_VALUE_ISINF (value)
2006 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2007 TREE_OVERFLOW (t) = 1;
2008 else
2009 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2010 return t;
2013 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2014 to a floating point type. */
2016 static tree
2017 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2019 REAL_VALUE_TYPE value;
2020 tree t;
2022 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2023 t = build_real (type, value);
2025 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2026 return t;
2029 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2030 to another fixed-point type. */
2032 static tree
2033 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2035 FIXED_VALUE_TYPE value;
2036 tree t;
2037 bool overflow_p;
2039 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2040 TYPE_SATURATING (type));
2041 t = build_fixed (type, value);
2043 /* Propagate overflow flags. */
2044 if (overflow_p | TREE_OVERFLOW (arg1))
2045 TREE_OVERFLOW (t) = 1;
2046 return t;
2049 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2050 to a fixed-point type. */
2052 static tree
2053 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2055 FIXED_VALUE_TYPE value;
2056 tree t;
2057 bool overflow_p;
2058 double_int di;
2060 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2062 di.low = TREE_INT_CST_ELT (arg1, 0);
2063 if (TREE_INT_CST_NUNITS (arg1) == 1)
2064 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2065 else
2066 di.high = TREE_INT_CST_ELT (arg1, 1);
2068 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2069 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2070 TYPE_SATURATING (type));
2071 t = build_fixed (type, value);
2073 /* Propagate overflow flags. */
2074 if (overflow_p | TREE_OVERFLOW (arg1))
2075 TREE_OVERFLOW (t) = 1;
2076 return t;
2079 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2080 to a fixed-point type. */
2082 static tree
2083 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2085 FIXED_VALUE_TYPE value;
2086 tree t;
2087 bool overflow_p;
2089 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2090 &TREE_REAL_CST (arg1),
2091 TYPE_SATURATING (type));
2092 t = build_fixed (type, value);
2094 /* Propagate overflow flags. */
2095 if (overflow_p | TREE_OVERFLOW (arg1))
2096 TREE_OVERFLOW (t) = 1;
2097 return t;
2100 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2101 type TYPE. If no simplification can be done return NULL_TREE. */
2103 static tree
2104 fold_convert_const (enum tree_code code, tree type, tree arg1)
2106 if (TREE_TYPE (arg1) == type)
2107 return arg1;
2109 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2110 || TREE_CODE (type) == OFFSET_TYPE)
2112 if (TREE_CODE (arg1) == INTEGER_CST)
2113 return fold_convert_const_int_from_int (type, arg1);
2114 else if (TREE_CODE (arg1) == REAL_CST)
2115 return fold_convert_const_int_from_real (code, type, arg1);
2116 else if (TREE_CODE (arg1) == FIXED_CST)
2117 return fold_convert_const_int_from_fixed (type, arg1);
2119 else if (TREE_CODE (type) == REAL_TYPE)
2121 if (TREE_CODE (arg1) == INTEGER_CST)
2122 return build_real_from_int_cst (type, arg1);
2123 else if (TREE_CODE (arg1) == REAL_CST)
2124 return fold_convert_const_real_from_real (type, arg1);
2125 else if (TREE_CODE (arg1) == FIXED_CST)
2126 return fold_convert_const_real_from_fixed (type, arg1);
2128 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2130 if (TREE_CODE (arg1) == FIXED_CST)
2131 return fold_convert_const_fixed_from_fixed (type, arg1);
2132 else if (TREE_CODE (arg1) == INTEGER_CST)
2133 return fold_convert_const_fixed_from_int (type, arg1);
2134 else if (TREE_CODE (arg1) == REAL_CST)
2135 return fold_convert_const_fixed_from_real (type, arg1);
2137 else if (TREE_CODE (type) == VECTOR_TYPE)
2139 if (TREE_CODE (arg1) == VECTOR_CST
2140 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2142 int len = TYPE_VECTOR_SUBPARTS (type);
2143 tree elttype = TREE_TYPE (type);
2144 tree *v = XALLOCAVEC (tree, len);
2145 for (int i = 0; i < len; ++i)
2147 tree elt = VECTOR_CST_ELT (arg1, i);
2148 tree cvt = fold_convert_const (code, elttype, elt);
2149 if (cvt == NULL_TREE)
2150 return NULL_TREE;
2151 v[i] = cvt;
2153 return build_vector (type, v);
2156 return NULL_TREE;
2159 /* Construct a vector of zero elements of vector type TYPE. */
2161 static tree
2162 build_zero_vector (tree type)
2164 tree t;
2166 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2167 return build_vector_from_val (type, t);
2170 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2172 bool
2173 fold_convertible_p (const_tree type, const_tree arg)
2175 tree orig = TREE_TYPE (arg);
2177 if (type == orig)
2178 return true;
2180 if (TREE_CODE (arg) == ERROR_MARK
2181 || TREE_CODE (type) == ERROR_MARK
2182 || TREE_CODE (orig) == ERROR_MARK)
2183 return false;
2185 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2186 return true;
2188 switch (TREE_CODE (type))
2190 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2191 case POINTER_TYPE: case REFERENCE_TYPE:
2192 case OFFSET_TYPE:
2193 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2194 || TREE_CODE (orig) == OFFSET_TYPE);
2196 case REAL_TYPE:
2197 case FIXED_POINT_TYPE:
2198 case VECTOR_TYPE:
2199 case VOID_TYPE:
2200 return TREE_CODE (type) == TREE_CODE (orig);
2202 default:
2203 return false;
2207 /* Convert expression ARG to type TYPE. Used by the middle-end for
2208 simple conversions in preference to calling the front-end's convert. */
2210 tree
2211 fold_convert_loc (location_t loc, tree type, tree arg)
2213 tree orig = TREE_TYPE (arg);
2214 tree tem;
2216 if (type == orig)
2217 return arg;
2219 if (TREE_CODE (arg) == ERROR_MARK
2220 || TREE_CODE (type) == ERROR_MARK
2221 || TREE_CODE (orig) == ERROR_MARK)
2222 return error_mark_node;
2224 switch (TREE_CODE (type))
2226 case POINTER_TYPE:
2227 case REFERENCE_TYPE:
2228 /* Handle conversions between pointers to different address spaces. */
2229 if (POINTER_TYPE_P (orig)
2230 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2231 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2232 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2233 /* fall through */
2235 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2236 case OFFSET_TYPE:
2237 if (TREE_CODE (arg) == INTEGER_CST)
2239 tem = fold_convert_const (NOP_EXPR, type, arg);
2240 if (tem != NULL_TREE)
2241 return tem;
2243 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2244 || TREE_CODE (orig) == OFFSET_TYPE)
2245 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2246 if (TREE_CODE (orig) == COMPLEX_TYPE)
2247 return fold_convert_loc (loc, type,
2248 fold_build1_loc (loc, REALPART_EXPR,
2249 TREE_TYPE (orig), arg));
2250 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2251 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2252 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2254 case REAL_TYPE:
2255 if (TREE_CODE (arg) == INTEGER_CST)
2257 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2258 if (tem != NULL_TREE)
2259 return tem;
2261 else if (TREE_CODE (arg) == REAL_CST)
2263 tem = fold_convert_const (NOP_EXPR, type, arg);
2264 if (tem != NULL_TREE)
2265 return tem;
2267 else if (TREE_CODE (arg) == FIXED_CST)
2269 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2270 if (tem != NULL_TREE)
2271 return tem;
2274 switch (TREE_CODE (orig))
2276 case INTEGER_TYPE:
2277 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2278 case POINTER_TYPE: case REFERENCE_TYPE:
2279 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2281 case REAL_TYPE:
2282 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2284 case FIXED_POINT_TYPE:
2285 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2287 case COMPLEX_TYPE:
2288 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2289 return fold_convert_loc (loc, type, tem);
2291 default:
2292 gcc_unreachable ();
2295 case FIXED_POINT_TYPE:
2296 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2297 || TREE_CODE (arg) == REAL_CST)
2299 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2300 if (tem != NULL_TREE)
2301 goto fold_convert_exit;
2304 switch (TREE_CODE (orig))
2306 case FIXED_POINT_TYPE:
2307 case INTEGER_TYPE:
2308 case ENUMERAL_TYPE:
2309 case BOOLEAN_TYPE:
2310 case REAL_TYPE:
2311 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2313 case COMPLEX_TYPE:
2314 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2315 return fold_convert_loc (loc, type, tem);
2317 default:
2318 gcc_unreachable ();
2321 case COMPLEX_TYPE:
2322 switch (TREE_CODE (orig))
2324 case INTEGER_TYPE:
2325 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2326 case POINTER_TYPE: case REFERENCE_TYPE:
2327 case REAL_TYPE:
2328 case FIXED_POINT_TYPE:
2329 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2330 fold_convert_loc (loc, TREE_TYPE (type), arg),
2331 fold_convert_loc (loc, TREE_TYPE (type),
2332 integer_zero_node));
2333 case COMPLEX_TYPE:
2335 tree rpart, ipart;
2337 if (TREE_CODE (arg) == COMPLEX_EXPR)
2339 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2340 TREE_OPERAND (arg, 0));
2341 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2342 TREE_OPERAND (arg, 1));
2343 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2346 arg = save_expr (arg);
2347 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2348 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2349 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2350 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2351 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2354 default:
2355 gcc_unreachable ();
2358 case VECTOR_TYPE:
2359 if (integer_zerop (arg))
2360 return build_zero_vector (type);
2361 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2362 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2363 || TREE_CODE (orig) == VECTOR_TYPE);
2364 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2366 case VOID_TYPE:
2367 tem = fold_ignored_result (arg);
2368 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2370 default:
2371 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2372 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2373 gcc_unreachable ();
2375 fold_convert_exit:
2376 protected_set_expr_location_unshare (tem, loc);
2377 return tem;
2380 /* Return false if expr can be assumed not to be an lvalue, true
2381 otherwise. */
2383 static bool
2384 maybe_lvalue_p (const_tree x)
2386 /* We only need to wrap lvalue tree codes. */
2387 switch (TREE_CODE (x))
2389 case VAR_DECL:
2390 case PARM_DECL:
2391 case RESULT_DECL:
2392 case LABEL_DECL:
2393 case FUNCTION_DECL:
2394 case SSA_NAME:
2396 case COMPONENT_REF:
2397 case MEM_REF:
2398 case INDIRECT_REF:
2399 case ARRAY_REF:
2400 case ARRAY_RANGE_REF:
2401 case BIT_FIELD_REF:
2402 case OBJ_TYPE_REF:
2404 case REALPART_EXPR:
2405 case IMAGPART_EXPR:
2406 case PREINCREMENT_EXPR:
2407 case PREDECREMENT_EXPR:
2408 case SAVE_EXPR:
2409 case TRY_CATCH_EXPR:
2410 case WITH_CLEANUP_EXPR:
2411 case COMPOUND_EXPR:
2412 case MODIFY_EXPR:
2413 case TARGET_EXPR:
2414 case COND_EXPR:
2415 case BIND_EXPR:
2416 break;
2418 default:
2419 /* Assume the worst for front-end tree codes. */
2420 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2421 break;
2422 return false;
2425 return true;
2428 /* Return an expr equal to X but certainly not valid as an lvalue. */
2430 tree
2431 non_lvalue_loc (location_t loc, tree x)
2433 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2434 us. */
2435 if (in_gimple_form)
2436 return x;
2438 if (! maybe_lvalue_p (x))
2439 return x;
2440 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2443 /* When pedantic, return an expr equal to X but certainly not valid as a
2444 pedantic lvalue. Otherwise, return X. */
2446 static tree
2447 pedantic_non_lvalue_loc (location_t loc, tree x)
2449 return protected_set_expr_location_unshare (x, loc);
2452 /* Given a tree comparison code, return the code that is the logical inverse.
2453 It is generally not safe to do this for floating-point comparisons, except
2454 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2455 ERROR_MARK in this case. */
2457 enum tree_code
2458 invert_tree_comparison (enum tree_code code, bool honor_nans)
2460 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2461 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2462 return ERROR_MARK;
2464 switch (code)
2466 case EQ_EXPR:
2467 return NE_EXPR;
2468 case NE_EXPR:
2469 return EQ_EXPR;
2470 case GT_EXPR:
2471 return honor_nans ? UNLE_EXPR : LE_EXPR;
2472 case GE_EXPR:
2473 return honor_nans ? UNLT_EXPR : LT_EXPR;
2474 case LT_EXPR:
2475 return honor_nans ? UNGE_EXPR : GE_EXPR;
2476 case LE_EXPR:
2477 return honor_nans ? UNGT_EXPR : GT_EXPR;
2478 case LTGT_EXPR:
2479 return UNEQ_EXPR;
2480 case UNEQ_EXPR:
2481 return LTGT_EXPR;
2482 case UNGT_EXPR:
2483 return LE_EXPR;
2484 case UNGE_EXPR:
2485 return LT_EXPR;
2486 case UNLT_EXPR:
2487 return GE_EXPR;
2488 case UNLE_EXPR:
2489 return GT_EXPR;
2490 case ORDERED_EXPR:
2491 return UNORDERED_EXPR;
2492 case UNORDERED_EXPR:
2493 return ORDERED_EXPR;
2494 default:
2495 gcc_unreachable ();
2499 /* Similar, but return the comparison that results if the operands are
2500 swapped. This is safe for floating-point. */
2502 enum tree_code
2503 swap_tree_comparison (enum tree_code code)
2505 switch (code)
2507 case EQ_EXPR:
2508 case NE_EXPR:
2509 case ORDERED_EXPR:
2510 case UNORDERED_EXPR:
2511 case LTGT_EXPR:
2512 case UNEQ_EXPR:
2513 return code;
2514 case GT_EXPR:
2515 return LT_EXPR;
2516 case GE_EXPR:
2517 return LE_EXPR;
2518 case LT_EXPR:
2519 return GT_EXPR;
2520 case LE_EXPR:
2521 return GE_EXPR;
2522 case UNGT_EXPR:
2523 return UNLT_EXPR;
2524 case UNGE_EXPR:
2525 return UNLE_EXPR;
2526 case UNLT_EXPR:
2527 return UNGT_EXPR;
2528 case UNLE_EXPR:
2529 return UNGE_EXPR;
2530 default:
2531 gcc_unreachable ();
2536 /* Convert a comparison tree code from an enum tree_code representation
2537 into a compcode bit-based encoding. This function is the inverse of
2538 compcode_to_comparison. */
2540 static enum comparison_code
2541 comparison_to_compcode (enum tree_code code)
2543 switch (code)
2545 case LT_EXPR:
2546 return COMPCODE_LT;
2547 case EQ_EXPR:
2548 return COMPCODE_EQ;
2549 case LE_EXPR:
2550 return COMPCODE_LE;
2551 case GT_EXPR:
2552 return COMPCODE_GT;
2553 case NE_EXPR:
2554 return COMPCODE_NE;
2555 case GE_EXPR:
2556 return COMPCODE_GE;
2557 case ORDERED_EXPR:
2558 return COMPCODE_ORD;
2559 case UNORDERED_EXPR:
2560 return COMPCODE_UNORD;
2561 case UNLT_EXPR:
2562 return COMPCODE_UNLT;
2563 case UNEQ_EXPR:
2564 return COMPCODE_UNEQ;
2565 case UNLE_EXPR:
2566 return COMPCODE_UNLE;
2567 case UNGT_EXPR:
2568 return COMPCODE_UNGT;
2569 case LTGT_EXPR:
2570 return COMPCODE_LTGT;
2571 case UNGE_EXPR:
2572 return COMPCODE_UNGE;
2573 default:
2574 gcc_unreachable ();
2578 /* Convert a compcode bit-based encoding of a comparison operator back
2579 to GCC's enum tree_code representation. This function is the
2580 inverse of comparison_to_compcode. */
2582 static enum tree_code
2583 compcode_to_comparison (enum comparison_code code)
2585 switch (code)
2587 case COMPCODE_LT:
2588 return LT_EXPR;
2589 case COMPCODE_EQ:
2590 return EQ_EXPR;
2591 case COMPCODE_LE:
2592 return LE_EXPR;
2593 case COMPCODE_GT:
2594 return GT_EXPR;
2595 case COMPCODE_NE:
2596 return NE_EXPR;
2597 case COMPCODE_GE:
2598 return GE_EXPR;
2599 case COMPCODE_ORD:
2600 return ORDERED_EXPR;
2601 case COMPCODE_UNORD:
2602 return UNORDERED_EXPR;
2603 case COMPCODE_UNLT:
2604 return UNLT_EXPR;
2605 case COMPCODE_UNEQ:
2606 return UNEQ_EXPR;
2607 case COMPCODE_UNLE:
2608 return UNLE_EXPR;
2609 case COMPCODE_UNGT:
2610 return UNGT_EXPR;
2611 case COMPCODE_LTGT:
2612 return LTGT_EXPR;
2613 case COMPCODE_UNGE:
2614 return UNGE_EXPR;
2615 default:
2616 gcc_unreachable ();
2620 /* Return a tree for the comparison which is the combination of
2621 doing the AND or OR (depending on CODE) of the two operations LCODE
2622 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2623 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2624 if this makes the transformation invalid. */
2626 tree
2627 combine_comparisons (location_t loc,
2628 enum tree_code code, enum tree_code lcode,
2629 enum tree_code rcode, tree truth_type,
2630 tree ll_arg, tree lr_arg)
2632 bool honor_nans = HONOR_NANS (ll_arg);
2633 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2634 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2635 int compcode;
2637 switch (code)
2639 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2640 compcode = lcompcode & rcompcode;
2641 break;
2643 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2644 compcode = lcompcode | rcompcode;
2645 break;
2647 default:
2648 return NULL_TREE;
2651 if (!honor_nans)
2653 /* Eliminate unordered comparisons, as well as LTGT and ORD
2654 which are not used unless the mode has NaNs. */
2655 compcode &= ~COMPCODE_UNORD;
2656 if (compcode == COMPCODE_LTGT)
2657 compcode = COMPCODE_NE;
2658 else if (compcode == COMPCODE_ORD)
2659 compcode = COMPCODE_TRUE;
2661 else if (flag_trapping_math)
2663 /* Check that the original operation and the optimized ones will trap
2664 under the same condition. */
2665 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2666 && (lcompcode != COMPCODE_EQ)
2667 && (lcompcode != COMPCODE_ORD);
2668 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2669 && (rcompcode != COMPCODE_EQ)
2670 && (rcompcode != COMPCODE_ORD);
2671 bool trap = (compcode & COMPCODE_UNORD) == 0
2672 && (compcode != COMPCODE_EQ)
2673 && (compcode != COMPCODE_ORD);
2675 /* In a short-circuited boolean expression the LHS might be
2676 such that the RHS, if evaluated, will never trap. For
2677 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2678 if neither x nor y is NaN. (This is a mixed blessing: for
2679 example, the expression above will never trap, hence
2680 optimizing it to x < y would be invalid). */
2681 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2682 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2683 rtrap = false;
2685 /* If the comparison was short-circuited, and only the RHS
2686 trapped, we may now generate a spurious trap. */
2687 if (rtrap && !ltrap
2688 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2689 return NULL_TREE;
2691 /* If we changed the conditions that cause a trap, we lose. */
2692 if ((ltrap || rtrap) != trap)
2693 return NULL_TREE;
2696 if (compcode == COMPCODE_TRUE)
2697 return constant_boolean_node (true, truth_type);
2698 else if (compcode == COMPCODE_FALSE)
2699 return constant_boolean_node (false, truth_type);
2700 else
2702 enum tree_code tcode;
2704 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2705 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2709 /* Return nonzero if two operands (typically of the same tree node)
2710 are necessarily equal. FLAGS modifies behavior as follows:
2712 If OEP_ONLY_CONST is set, only return nonzero for constants.
2713 This function tests whether the operands are indistinguishable;
2714 it does not test whether they are equal using C's == operation.
2715 The distinction is important for IEEE floating point, because
2716 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2717 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2719 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2720 even though it may hold multiple values during a function.
2721 This is because a GCC tree node guarantees that nothing else is
2722 executed between the evaluation of its "operands" (which may often
2723 be evaluated in arbitrary order). Hence if the operands themselves
2724 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2725 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2726 unset means assuming isochronic (or instantaneous) tree equivalence.
2727 Unless comparing arbitrary expression trees, such as from different
2728 statements, this flag can usually be left unset.
2730 If OEP_PURE_SAME is set, then pure functions with identical arguments
2731 are considered the same. It is used when the caller has other ways
2732 to ensure that global memory is unchanged in between.
2734 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2735 not values of expressions.
2737 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2738 any operand with side effect. This is unnecesarily conservative in the
2739 case we know that arg0 and arg1 are in disjoint code paths (such as in
2740 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2741 addresses with TREE_CONSTANT flag set so we know that &var == &var
2742 even if var is volatile. */
2745 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2747 /* If either is ERROR_MARK, they aren't equal. */
2748 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2749 || TREE_TYPE (arg0) == error_mark_node
2750 || TREE_TYPE (arg1) == error_mark_node)
2751 return 0;
2753 /* Similar, if either does not have a type (like a released SSA name),
2754 they aren't equal. */
2755 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2756 return 0;
2758 /* We cannot consider pointers to different address space equal. */
2759 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2760 && POINTER_TYPE_P (TREE_TYPE (arg1))
2761 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2762 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2763 return 0;
2765 /* Check equality of integer constants before bailing out due to
2766 precision differences. */
2767 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2769 /* Address of INTEGER_CST is not defined; check that we did not forget
2770 to drop the OEP_ADDRESS_OF flags. */
2771 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2772 return tree_int_cst_equal (arg0, arg1);
2775 if (!(flags & OEP_ADDRESS_OF))
2777 /* If both types don't have the same signedness, then we can't consider
2778 them equal. We must check this before the STRIP_NOPS calls
2779 because they may change the signedness of the arguments. As pointers
2780 strictly don't have a signedness, require either two pointers or
2781 two non-pointers as well. */
2782 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2783 || POINTER_TYPE_P (TREE_TYPE (arg0))
2784 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2785 return 0;
2787 /* If both types don't have the same precision, then it is not safe
2788 to strip NOPs. */
2789 if (element_precision (TREE_TYPE (arg0))
2790 != element_precision (TREE_TYPE (arg1)))
2791 return 0;
2793 STRIP_NOPS (arg0);
2794 STRIP_NOPS (arg1);
2796 #if 0
2797 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2798 sanity check once the issue is solved. */
2799 else
2800 /* Addresses of conversions and SSA_NAMEs (and many other things)
2801 are not defined. Check that we did not forget to drop the
2802 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2803 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2804 && TREE_CODE (arg0) != SSA_NAME);
2805 #endif
2807 /* In case both args are comparisons but with different comparison
2808 code, try to swap the comparison operands of one arg to produce
2809 a match and compare that variant. */
2810 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2811 && COMPARISON_CLASS_P (arg0)
2812 && COMPARISON_CLASS_P (arg1))
2814 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2816 if (TREE_CODE (arg0) == swap_code)
2817 return operand_equal_p (TREE_OPERAND (arg0, 0),
2818 TREE_OPERAND (arg1, 1), flags)
2819 && operand_equal_p (TREE_OPERAND (arg0, 1),
2820 TREE_OPERAND (arg1, 0), flags);
2823 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2825 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2826 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2828 else if (flags & OEP_ADDRESS_OF)
2830 /* If we are interested in comparing addresses ignore
2831 MEM_REF wrappings of the base that can appear just for
2832 TBAA reasons. */
2833 if (TREE_CODE (arg0) == MEM_REF
2834 && DECL_P (arg1)
2835 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2836 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2837 && integer_zerop (TREE_OPERAND (arg0, 1)))
2838 return 1;
2839 else if (TREE_CODE (arg1) == MEM_REF
2840 && DECL_P (arg0)
2841 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2842 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2843 && integer_zerop (TREE_OPERAND (arg1, 1)))
2844 return 1;
2845 return 0;
2847 else
2848 return 0;
2851 /* When not checking adddresses, this is needed for conversions and for
2852 COMPONENT_REF. Might as well play it safe and always test this. */
2853 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2854 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2855 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2856 && !(flags & OEP_ADDRESS_OF)))
2857 return 0;
2859 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2860 We don't care about side effects in that case because the SAVE_EXPR
2861 takes care of that for us. In all other cases, two expressions are
2862 equal if they have no side effects. If we have two identical
2863 expressions with side effects that should be treated the same due
2864 to the only side effects being identical SAVE_EXPR's, that will
2865 be detected in the recursive calls below.
2866 If we are taking an invariant address of two identical objects
2867 they are necessarily equal as well. */
2868 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2869 && (TREE_CODE (arg0) == SAVE_EXPR
2870 || (flags & OEP_MATCH_SIDE_EFFECTS)
2871 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2872 return 1;
2874 /* Next handle constant cases, those for which we can return 1 even
2875 if ONLY_CONST is set. */
2876 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2877 switch (TREE_CODE (arg0))
2879 case INTEGER_CST:
2880 return tree_int_cst_equal (arg0, arg1);
2882 case FIXED_CST:
2883 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2884 TREE_FIXED_CST (arg1));
2886 case REAL_CST:
2887 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2888 return 1;
2891 if (!HONOR_SIGNED_ZEROS (arg0))
2893 /* If we do not distinguish between signed and unsigned zero,
2894 consider them equal. */
2895 if (real_zerop (arg0) && real_zerop (arg1))
2896 return 1;
2898 return 0;
2900 case VECTOR_CST:
2902 unsigned i;
2904 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2905 return 0;
2907 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2909 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2910 VECTOR_CST_ELT (arg1, i), flags))
2911 return 0;
2913 return 1;
2916 case COMPLEX_CST:
2917 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2918 flags)
2919 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2920 flags));
2922 case STRING_CST:
2923 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2924 && ! memcmp (TREE_STRING_POINTER (arg0),
2925 TREE_STRING_POINTER (arg1),
2926 TREE_STRING_LENGTH (arg0)));
2928 case ADDR_EXPR:
2929 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2930 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2931 flags | OEP_ADDRESS_OF
2932 | OEP_MATCH_SIDE_EFFECTS);
2933 case CONSTRUCTOR:
2934 /* In GIMPLE empty constructors are allowed in initializers of
2935 aggregates. */
2936 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0))
2937 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1)));
2938 default:
2939 break;
2942 if (flags & OEP_ONLY_CONST)
2943 return 0;
2945 /* Define macros to test an operand from arg0 and arg1 for equality and a
2946 variant that allows null and views null as being different from any
2947 non-null value. In the latter case, if either is null, the both
2948 must be; otherwise, do the normal comparison. */
2949 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2950 TREE_OPERAND (arg1, N), flags)
2952 #define OP_SAME_WITH_NULL(N) \
2953 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2954 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2956 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2958 case tcc_unary:
2959 /* Two conversions are equal only if signedness and modes match. */
2960 switch (TREE_CODE (arg0))
2962 CASE_CONVERT:
2963 case FIX_TRUNC_EXPR:
2964 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2965 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2966 return 0;
2967 break;
2968 default:
2969 break;
2972 return OP_SAME (0);
2975 case tcc_comparison:
2976 case tcc_binary:
2977 if (OP_SAME (0) && OP_SAME (1))
2978 return 1;
2980 /* For commutative ops, allow the other order. */
2981 return (commutative_tree_code (TREE_CODE (arg0))
2982 && operand_equal_p (TREE_OPERAND (arg0, 0),
2983 TREE_OPERAND (arg1, 1), flags)
2984 && operand_equal_p (TREE_OPERAND (arg0, 1),
2985 TREE_OPERAND (arg1, 0), flags));
2987 case tcc_reference:
2988 /* If either of the pointer (or reference) expressions we are
2989 dereferencing contain a side effect, these cannot be equal,
2990 but their addresses can be. */
2991 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
2992 && (TREE_SIDE_EFFECTS (arg0)
2993 || TREE_SIDE_EFFECTS (arg1)))
2994 return 0;
2996 switch (TREE_CODE (arg0))
2998 case INDIRECT_REF:
2999 if (!(flags & OEP_ADDRESS_OF)
3000 && (TYPE_ALIGN (TREE_TYPE (arg0))
3001 != TYPE_ALIGN (TREE_TYPE (arg1))))
3002 return 0;
3003 flags &= ~OEP_ADDRESS_OF;
3004 return OP_SAME (0);
3006 case IMAGPART_EXPR:
3007 /* Require the same offset. */
3008 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3009 TYPE_SIZE (TREE_TYPE (arg1)),
3010 flags & ~OEP_ADDRESS_OF))
3011 return 0;
3013 /* Fallthru. */
3014 case REALPART_EXPR:
3015 case VIEW_CONVERT_EXPR:
3016 return OP_SAME (0);
3018 case TARGET_MEM_REF:
3019 case MEM_REF:
3020 if (!(flags & OEP_ADDRESS_OF))
3022 /* Require equal access sizes */
3023 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3024 && (!TYPE_SIZE (TREE_TYPE (arg0))
3025 || !TYPE_SIZE (TREE_TYPE (arg1))
3026 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3027 TYPE_SIZE (TREE_TYPE (arg1)),
3028 flags)))
3029 return 0;
3030 /* Verify that access happens in similar types. */
3031 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3032 return 0;
3033 /* Verify that accesses are TBAA compatible. */
3034 if (!alias_ptr_types_compatible_p
3035 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3036 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3037 || (MR_DEPENDENCE_CLIQUE (arg0)
3038 != MR_DEPENDENCE_CLIQUE (arg1))
3039 || (MR_DEPENDENCE_BASE (arg0)
3040 != MR_DEPENDENCE_BASE (arg1)))
3041 return 0;
3042 /* Verify that alignment is compatible. */
3043 if (TYPE_ALIGN (TREE_TYPE (arg0))
3044 != TYPE_ALIGN (TREE_TYPE (arg1)))
3045 return 0;
3047 flags &= ~OEP_ADDRESS_OF;
3048 return (OP_SAME (0) && OP_SAME (1)
3049 /* TARGET_MEM_REF require equal extra operands. */
3050 && (TREE_CODE (arg0) != TARGET_MEM_REF
3051 || (OP_SAME_WITH_NULL (2)
3052 && OP_SAME_WITH_NULL (3)
3053 && OP_SAME_WITH_NULL (4))));
3055 case ARRAY_REF:
3056 case ARRAY_RANGE_REF:
3057 if (!OP_SAME (0))
3058 return 0;
3059 flags &= ~OEP_ADDRESS_OF;
3060 /* Compare the array index by value if it is constant first as we
3061 may have different types but same value here. */
3062 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3063 TREE_OPERAND (arg1, 1))
3064 || OP_SAME (1))
3065 && OP_SAME_WITH_NULL (2)
3066 && OP_SAME_WITH_NULL (3)
3067 /* Compare low bound and element size as with OEP_ADDRESS_OF
3068 we have to account for the offset of the ref. */
3069 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3070 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3071 || (operand_equal_p (array_ref_low_bound
3072 (CONST_CAST_TREE (arg0)),
3073 array_ref_low_bound
3074 (CONST_CAST_TREE (arg1)), flags)
3075 && operand_equal_p (array_ref_element_size
3076 (CONST_CAST_TREE (arg0)),
3077 array_ref_element_size
3078 (CONST_CAST_TREE (arg1)),
3079 flags))));
3081 case COMPONENT_REF:
3082 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3083 may be NULL when we're called to compare MEM_EXPRs. */
3084 if (!OP_SAME_WITH_NULL (0)
3085 || !OP_SAME (1))
3086 return 0;
3087 flags &= ~OEP_ADDRESS_OF;
3088 return OP_SAME_WITH_NULL (2);
3090 case BIT_FIELD_REF:
3091 if (!OP_SAME (0))
3092 return 0;
3093 flags &= ~OEP_ADDRESS_OF;
3094 return OP_SAME (1) && OP_SAME (2);
3096 default:
3097 return 0;
3100 case tcc_expression:
3101 switch (TREE_CODE (arg0))
3103 case ADDR_EXPR:
3104 /* Be sure we pass right ADDRESS_OF flag. */
3105 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3106 return operand_equal_p (TREE_OPERAND (arg0, 0),
3107 TREE_OPERAND (arg1, 0),
3108 flags | OEP_ADDRESS_OF);
3110 case TRUTH_NOT_EXPR:
3111 return OP_SAME (0);
3113 case TRUTH_ANDIF_EXPR:
3114 case TRUTH_ORIF_EXPR:
3115 return OP_SAME (0) && OP_SAME (1);
3117 case FMA_EXPR:
3118 case WIDEN_MULT_PLUS_EXPR:
3119 case WIDEN_MULT_MINUS_EXPR:
3120 if (!OP_SAME (2))
3121 return 0;
3122 /* The multiplcation operands are commutative. */
3123 /* FALLTHRU */
3125 case TRUTH_AND_EXPR:
3126 case TRUTH_OR_EXPR:
3127 case TRUTH_XOR_EXPR:
3128 if (OP_SAME (0) && OP_SAME (1))
3129 return 1;
3131 /* Otherwise take into account this is a commutative operation. */
3132 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3133 TREE_OPERAND (arg1, 1), flags)
3134 && operand_equal_p (TREE_OPERAND (arg0, 1),
3135 TREE_OPERAND (arg1, 0), flags));
3137 case COND_EXPR:
3138 if (! OP_SAME (1) || ! OP_SAME (2))
3139 return 0;
3140 flags &= ~OEP_ADDRESS_OF;
3141 return OP_SAME (0);
3143 case VEC_COND_EXPR:
3144 case DOT_PROD_EXPR:
3145 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3147 default:
3148 return 0;
3151 case tcc_vl_exp:
3152 switch (TREE_CODE (arg0))
3154 case CALL_EXPR:
3155 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3156 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3157 /* If not both CALL_EXPRs are either internal or normal function
3158 functions, then they are not equal. */
3159 return 0;
3160 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3162 /* If the CALL_EXPRs call different internal functions, then they
3163 are not equal. */
3164 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3165 return 0;
3167 else
3169 /* If the CALL_EXPRs call different functions, then they are not
3170 equal. */
3171 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3172 flags))
3173 return 0;
3176 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3178 unsigned int cef = call_expr_flags (arg0);
3179 if (flags & OEP_PURE_SAME)
3180 cef &= ECF_CONST | ECF_PURE;
3181 else
3182 cef &= ECF_CONST;
3183 if (!cef)
3184 return 0;
3187 /* Now see if all the arguments are the same. */
3189 const_call_expr_arg_iterator iter0, iter1;
3190 const_tree a0, a1;
3191 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3192 a1 = first_const_call_expr_arg (arg1, &iter1);
3193 a0 && a1;
3194 a0 = next_const_call_expr_arg (&iter0),
3195 a1 = next_const_call_expr_arg (&iter1))
3196 if (! operand_equal_p (a0, a1, flags))
3197 return 0;
3199 /* If we get here and both argument lists are exhausted
3200 then the CALL_EXPRs are equal. */
3201 return ! (a0 || a1);
3203 default:
3204 return 0;
3207 case tcc_declaration:
3208 /* Consider __builtin_sqrt equal to sqrt. */
3209 return (TREE_CODE (arg0) == FUNCTION_DECL
3210 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3211 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3212 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3214 case tcc_exceptional:
3215 if (TREE_CODE (arg0) == CONSTRUCTOR)
3217 /* In GIMPLE constructors are used only to build vectors from
3218 elements. Individual elements in the constructor must be
3219 indexed in increasing order and form an initial sequence.
3221 We make no effort to compare constructors in generic.
3222 (see sem_variable::equals in ipa-icf which can do so for
3223 constants). */
3224 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3225 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3226 return 0;
3228 /* Be sure that vectors constructed have the same representation.
3229 We only tested element precision and modes to match.
3230 Vectors may be BLKmode and thus also check that the number of
3231 parts match. */
3232 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3233 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3234 return 0;
3236 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3237 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3238 unsigned int len = vec_safe_length (v0);
3240 if (len != vec_safe_length (v1))
3241 return 0;
3243 for (unsigned int i = 0; i < len; i++)
3245 constructor_elt *c0 = &(*v0)[i];
3246 constructor_elt *c1 = &(*v1)[i];
3248 if (!operand_equal_p (c0->value, c1->value, flags)
3249 /* In GIMPLE the indexes can be either NULL or matching i.
3250 Double check this so we won't get false
3251 positives for GENERIC. */
3252 || (c0->index
3253 && (TREE_CODE (c0->index) != INTEGER_CST
3254 || !compare_tree_int (c0->index, i)))
3255 || (c1->index
3256 && (TREE_CODE (c1->index) != INTEGER_CST
3257 || !compare_tree_int (c1->index, i))))
3258 return 0;
3260 return 1;
3262 return 0;
3264 default:
3265 return 0;
3268 #undef OP_SAME
3269 #undef OP_SAME_WITH_NULL
3272 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3273 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3275 When in doubt, return 0. */
3277 static int
3278 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3280 int unsignedp1, unsignedpo;
3281 tree primarg0, primarg1, primother;
3282 unsigned int correct_width;
3284 if (operand_equal_p (arg0, arg1, 0))
3285 return 1;
3287 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3288 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3289 return 0;
3291 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3292 and see if the inner values are the same. This removes any
3293 signedness comparison, which doesn't matter here. */
3294 primarg0 = arg0, primarg1 = arg1;
3295 STRIP_NOPS (primarg0);
3296 STRIP_NOPS (primarg1);
3297 if (operand_equal_p (primarg0, primarg1, 0))
3298 return 1;
3300 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3301 actual comparison operand, ARG0.
3303 First throw away any conversions to wider types
3304 already present in the operands. */
3306 primarg1 = get_narrower (arg1, &unsignedp1);
3307 primother = get_narrower (other, &unsignedpo);
3309 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3310 if (unsignedp1 == unsignedpo
3311 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3312 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3314 tree type = TREE_TYPE (arg0);
3316 /* Make sure shorter operand is extended the right way
3317 to match the longer operand. */
3318 primarg1 = fold_convert (signed_or_unsigned_type_for
3319 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3321 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3322 return 1;
3325 return 0;
3328 /* See if ARG is an expression that is either a comparison or is performing
3329 arithmetic on comparisons. The comparisons must only be comparing
3330 two different values, which will be stored in *CVAL1 and *CVAL2; if
3331 they are nonzero it means that some operands have already been found.
3332 No variables may be used anywhere else in the expression except in the
3333 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3334 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3336 If this is true, return 1. Otherwise, return zero. */
3338 static int
3339 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3341 enum tree_code code = TREE_CODE (arg);
3342 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3344 /* We can handle some of the tcc_expression cases here. */
3345 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3346 tclass = tcc_unary;
3347 else if (tclass == tcc_expression
3348 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3349 || code == COMPOUND_EXPR))
3350 tclass = tcc_binary;
3352 else if (tclass == tcc_expression && code == SAVE_EXPR
3353 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3355 /* If we've already found a CVAL1 or CVAL2, this expression is
3356 two complex to handle. */
3357 if (*cval1 || *cval2)
3358 return 0;
3360 tclass = tcc_unary;
3361 *save_p = 1;
3364 switch (tclass)
3366 case tcc_unary:
3367 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3369 case tcc_binary:
3370 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3371 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3372 cval1, cval2, save_p));
3374 case tcc_constant:
3375 return 1;
3377 case tcc_expression:
3378 if (code == COND_EXPR)
3379 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3380 cval1, cval2, save_p)
3381 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3382 cval1, cval2, save_p)
3383 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3384 cval1, cval2, save_p));
3385 return 0;
3387 case tcc_comparison:
3388 /* First see if we can handle the first operand, then the second. For
3389 the second operand, we know *CVAL1 can't be zero. It must be that
3390 one side of the comparison is each of the values; test for the
3391 case where this isn't true by failing if the two operands
3392 are the same. */
3394 if (operand_equal_p (TREE_OPERAND (arg, 0),
3395 TREE_OPERAND (arg, 1), 0))
3396 return 0;
3398 if (*cval1 == 0)
3399 *cval1 = TREE_OPERAND (arg, 0);
3400 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3402 else if (*cval2 == 0)
3403 *cval2 = TREE_OPERAND (arg, 0);
3404 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3406 else
3407 return 0;
3409 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3411 else if (*cval2 == 0)
3412 *cval2 = TREE_OPERAND (arg, 1);
3413 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3415 else
3416 return 0;
3418 return 1;
3420 default:
3421 return 0;
3425 /* ARG is a tree that is known to contain just arithmetic operations and
3426 comparisons. Evaluate the operations in the tree substituting NEW0 for
3427 any occurrence of OLD0 as an operand of a comparison and likewise for
3428 NEW1 and OLD1. */
3430 static tree
3431 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3432 tree old1, tree new1)
3434 tree type = TREE_TYPE (arg);
3435 enum tree_code code = TREE_CODE (arg);
3436 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3438 /* We can handle some of the tcc_expression cases here. */
3439 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3440 tclass = tcc_unary;
3441 else if (tclass == tcc_expression
3442 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3443 tclass = tcc_binary;
3445 switch (tclass)
3447 case tcc_unary:
3448 return fold_build1_loc (loc, code, type,
3449 eval_subst (loc, TREE_OPERAND (arg, 0),
3450 old0, new0, old1, new1));
3452 case tcc_binary:
3453 return fold_build2_loc (loc, code, type,
3454 eval_subst (loc, TREE_OPERAND (arg, 0),
3455 old0, new0, old1, new1),
3456 eval_subst (loc, TREE_OPERAND (arg, 1),
3457 old0, new0, old1, new1));
3459 case tcc_expression:
3460 switch (code)
3462 case SAVE_EXPR:
3463 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3464 old1, new1);
3466 case COMPOUND_EXPR:
3467 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3468 old1, new1);
3470 case COND_EXPR:
3471 return fold_build3_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),
3476 eval_subst (loc, TREE_OPERAND (arg, 2),
3477 old0, new0, old1, new1));
3478 default:
3479 break;
3481 /* Fall through - ??? */
3483 case tcc_comparison:
3485 tree arg0 = TREE_OPERAND (arg, 0);
3486 tree arg1 = TREE_OPERAND (arg, 1);
3488 /* We need to check both for exact equality and tree equality. The
3489 former will be true if the operand has a side-effect. In that
3490 case, we know the operand occurred exactly once. */
3492 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3493 arg0 = new0;
3494 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3495 arg0 = new1;
3497 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3498 arg1 = new0;
3499 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3500 arg1 = new1;
3502 return fold_build2_loc (loc, code, type, arg0, arg1);
3505 default:
3506 return arg;
3510 /* Return a tree for the case when the result of an expression is RESULT
3511 converted to TYPE and OMITTED was previously an operand of the expression
3512 but is now not needed (e.g., we folded OMITTED * 0).
3514 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3515 the conversion of RESULT to TYPE. */
3517 tree
3518 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3520 tree t = fold_convert_loc (loc, type, result);
3522 /* If the resulting operand is an empty statement, just return the omitted
3523 statement casted to void. */
3524 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3525 return build1_loc (loc, NOP_EXPR, void_type_node,
3526 fold_ignored_result (omitted));
3528 if (TREE_SIDE_EFFECTS (omitted))
3529 return build2_loc (loc, COMPOUND_EXPR, type,
3530 fold_ignored_result (omitted), t);
3532 return non_lvalue_loc (loc, t);
3535 /* Return a tree for the case when the result of an expression is RESULT
3536 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3537 of the expression but are now not needed.
3539 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3540 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3541 evaluated before OMITTED2. Otherwise, if neither has side effects,
3542 just do the conversion of RESULT to TYPE. */
3544 tree
3545 omit_two_operands_loc (location_t loc, tree type, tree result,
3546 tree omitted1, tree omitted2)
3548 tree t = fold_convert_loc (loc, type, result);
3550 if (TREE_SIDE_EFFECTS (omitted2))
3551 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3552 if (TREE_SIDE_EFFECTS (omitted1))
3553 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3555 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3559 /* Return a simplified tree node for the truth-negation of ARG. This
3560 never alters ARG itself. We assume that ARG is an operation that
3561 returns a truth value (0 or 1).
3563 FIXME: one would think we would fold the result, but it causes
3564 problems with the dominator optimizer. */
3566 static tree
3567 fold_truth_not_expr (location_t loc, tree arg)
3569 tree type = TREE_TYPE (arg);
3570 enum tree_code code = TREE_CODE (arg);
3571 location_t loc1, loc2;
3573 /* If this is a comparison, we can simply invert it, except for
3574 floating-point non-equality comparisons, in which case we just
3575 enclose a TRUTH_NOT_EXPR around what we have. */
3577 if (TREE_CODE_CLASS (code) == tcc_comparison)
3579 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3580 if (FLOAT_TYPE_P (op_type)
3581 && flag_trapping_math
3582 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3583 && code != NE_EXPR && code != EQ_EXPR)
3584 return NULL_TREE;
3586 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3587 if (code == ERROR_MARK)
3588 return NULL_TREE;
3590 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3591 TREE_OPERAND (arg, 1));
3592 if (TREE_NO_WARNING (arg))
3593 TREE_NO_WARNING (ret) = 1;
3594 return ret;
3597 switch (code)
3599 case INTEGER_CST:
3600 return constant_boolean_node (integer_zerop (arg), type);
3602 case TRUTH_AND_EXPR:
3603 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3604 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3605 return build2_loc (loc, TRUTH_OR_EXPR, type,
3606 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3607 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3609 case TRUTH_OR_EXPR:
3610 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3611 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3612 return build2_loc (loc, TRUTH_AND_EXPR, type,
3613 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3614 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3616 case TRUTH_XOR_EXPR:
3617 /* Here we can invert either operand. We invert the first operand
3618 unless the second operand is a TRUTH_NOT_EXPR in which case our
3619 result is the XOR of the first operand with the inside of the
3620 negation of the second operand. */
3622 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3623 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3624 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3625 else
3626 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3627 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3628 TREE_OPERAND (arg, 1));
3630 case TRUTH_ANDIF_EXPR:
3631 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3632 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3633 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3634 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3635 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3637 case TRUTH_ORIF_EXPR:
3638 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3639 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3640 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3641 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3642 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3644 case TRUTH_NOT_EXPR:
3645 return TREE_OPERAND (arg, 0);
3647 case COND_EXPR:
3649 tree arg1 = TREE_OPERAND (arg, 1);
3650 tree arg2 = TREE_OPERAND (arg, 2);
3652 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3653 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3655 /* A COND_EXPR may have a throw as one operand, which
3656 then has void type. Just leave void operands
3657 as they are. */
3658 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3659 VOID_TYPE_P (TREE_TYPE (arg1))
3660 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3661 VOID_TYPE_P (TREE_TYPE (arg2))
3662 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3665 case COMPOUND_EXPR:
3666 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3667 return build2_loc (loc, COMPOUND_EXPR, type,
3668 TREE_OPERAND (arg, 0),
3669 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3671 case NON_LVALUE_EXPR:
3672 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3673 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3675 CASE_CONVERT:
3676 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3677 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3679 /* ... fall through ... */
3681 case FLOAT_EXPR:
3682 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3683 return build1_loc (loc, TREE_CODE (arg), type,
3684 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3686 case BIT_AND_EXPR:
3687 if (!integer_onep (TREE_OPERAND (arg, 1)))
3688 return NULL_TREE;
3689 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3691 case SAVE_EXPR:
3692 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3694 case CLEANUP_POINT_EXPR:
3695 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3696 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3697 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3699 default:
3700 return NULL_TREE;
3704 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3705 assume that ARG is an operation that returns a truth value (0 or 1
3706 for scalars, 0 or -1 for vectors). Return the folded expression if
3707 folding is successful. Otherwise, return NULL_TREE. */
3709 static tree
3710 fold_invert_truthvalue (location_t loc, tree arg)
3712 tree type = TREE_TYPE (arg);
3713 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3714 ? BIT_NOT_EXPR
3715 : TRUTH_NOT_EXPR,
3716 type, arg);
3719 /* Return a simplified tree node for the truth-negation of ARG. This
3720 never alters ARG itself. We assume that ARG is an operation that
3721 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3723 tree
3724 invert_truthvalue_loc (location_t loc, tree arg)
3726 if (TREE_CODE (arg) == ERROR_MARK)
3727 return arg;
3729 tree type = TREE_TYPE (arg);
3730 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3731 ? BIT_NOT_EXPR
3732 : TRUTH_NOT_EXPR,
3733 type, arg);
3736 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3737 with code CODE. This optimization is unsafe. */
3738 static tree
3739 distribute_real_division (location_t loc, enum tree_code code, tree type,
3740 tree arg0, tree arg1)
3742 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3743 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3745 /* (A / C) +- (B / C) -> (A +- B) / C. */
3746 if (mul0 == mul1
3747 && operand_equal_p (TREE_OPERAND (arg0, 1),
3748 TREE_OPERAND (arg1, 1), 0))
3749 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3750 fold_build2_loc (loc, code, type,
3751 TREE_OPERAND (arg0, 0),
3752 TREE_OPERAND (arg1, 0)),
3753 TREE_OPERAND (arg0, 1));
3755 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3756 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3757 TREE_OPERAND (arg1, 0), 0)
3758 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3759 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3761 REAL_VALUE_TYPE r0, r1;
3762 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3763 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3764 if (!mul0)
3765 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3766 if (!mul1)
3767 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3768 real_arithmetic (&r0, code, &r0, &r1);
3769 return fold_build2_loc (loc, MULT_EXPR, type,
3770 TREE_OPERAND (arg0, 0),
3771 build_real (type, r0));
3774 return NULL_TREE;
3777 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3778 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3779 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3780 is the original memory reference used to preserve the alias set of
3781 the access. */
3783 static tree
3784 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3785 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3786 int unsignedp, int reversep)
3788 tree result, bftype;
3790 alias_set_type iset = get_alias_set (orig_inner);
3791 if (iset == 0 && get_alias_set (inner) != iset)
3792 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3793 build_fold_addr_expr (inner),
3794 build_int_cst (ptr_type_node, 0));
3796 if (bitpos == 0 && !reversep)
3798 tree size = TYPE_SIZE (TREE_TYPE (inner));
3799 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3800 || POINTER_TYPE_P (TREE_TYPE (inner)))
3801 && tree_fits_shwi_p (size)
3802 && tree_to_shwi (size) == bitsize)
3803 return fold_convert_loc (loc, type, inner);
3806 bftype = type;
3807 if (TYPE_PRECISION (bftype) != bitsize
3808 || TYPE_UNSIGNED (bftype) == !unsignedp)
3809 bftype = build_nonstandard_integer_type (bitsize, 0);
3811 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3812 size_int (bitsize), bitsize_int (bitpos));
3813 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3815 if (bftype != type)
3816 result = fold_convert_loc (loc, type, result);
3818 return result;
3821 /* Optimize a bit-field compare.
3823 There are two cases: First is a compare against a constant and the
3824 second is a comparison of two items where the fields are at the same
3825 bit position relative to the start of a chunk (byte, halfword, word)
3826 large enough to contain it. In these cases we can avoid the shift
3827 implicit in bitfield extractions.
3829 For constants, we emit a compare of the shifted constant with the
3830 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3831 compared. For two fields at the same position, we do the ANDs with the
3832 similar mask and compare the result of the ANDs.
3834 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3835 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3836 are the left and right operands of the comparison, respectively.
3838 If the optimization described above can be done, we return the resulting
3839 tree. Otherwise we return zero. */
3841 static tree
3842 optimize_bit_field_compare (location_t loc, enum tree_code code,
3843 tree compare_type, tree lhs, tree rhs)
3845 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3846 tree type = TREE_TYPE (lhs);
3847 tree unsigned_type;
3848 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3849 machine_mode lmode, rmode, nmode;
3850 int lunsignedp, runsignedp;
3851 int lreversep, rreversep;
3852 int lvolatilep = 0, rvolatilep = 0;
3853 tree linner, rinner = NULL_TREE;
3854 tree mask;
3855 tree offset;
3857 /* Get all the information about the extractions being done. If the bit size
3858 if the same as the size of the underlying object, we aren't doing an
3859 extraction at all and so can do nothing. We also don't want to
3860 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3861 then will no longer be able to replace it. */
3862 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3863 &lunsignedp, &lreversep, &lvolatilep, false);
3864 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3865 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3866 return 0;
3868 if (const_p)
3869 rreversep = lreversep;
3870 else
3872 /* If this is not a constant, we can only do something if bit positions,
3873 sizes, signedness and storage order are the same. */
3874 rinner
3875 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3876 &runsignedp, &rreversep, &rvolatilep, false);
3878 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3879 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3880 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3881 return 0;
3884 /* See if we can find a mode to refer to this field. We should be able to,
3885 but fail if we can't. */
3886 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3887 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3888 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3889 TYPE_ALIGN (TREE_TYPE (rinner))),
3890 word_mode, false);
3891 if (nmode == VOIDmode)
3892 return 0;
3894 /* Set signed and unsigned types of the precision of this mode for the
3895 shifts below. */
3896 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3898 /* Compute the bit position and size for the new reference and our offset
3899 within it. If the new reference is the same size as the original, we
3900 won't optimize anything, so return zero. */
3901 nbitsize = GET_MODE_BITSIZE (nmode);
3902 nbitpos = lbitpos & ~ (nbitsize - 1);
3903 lbitpos -= nbitpos;
3904 if (nbitsize == lbitsize)
3905 return 0;
3907 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3908 lbitpos = nbitsize - lbitsize - lbitpos;
3910 /* Make the mask to be used against the extracted field. */
3911 mask = build_int_cst_type (unsigned_type, -1);
3912 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3913 mask = const_binop (RSHIFT_EXPR, mask,
3914 size_int (nbitsize - lbitsize - lbitpos));
3916 if (! const_p)
3917 /* If not comparing with constant, just rework the comparison
3918 and return. */
3919 return fold_build2_loc (loc, code, compare_type,
3920 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3921 make_bit_field_ref (loc, linner, lhs,
3922 unsigned_type,
3923 nbitsize, nbitpos,
3924 1, lreversep),
3925 mask),
3926 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3927 make_bit_field_ref (loc, rinner, rhs,
3928 unsigned_type,
3929 nbitsize, nbitpos,
3930 1, rreversep),
3931 mask));
3933 /* Otherwise, we are handling the constant case. See if the constant is too
3934 big for the field. Warn and return a tree for 0 (false) if so. We do
3935 this not only for its own sake, but to avoid having to test for this
3936 error case below. If we didn't, we might generate wrong code.
3938 For unsigned fields, the constant shifted right by the field length should
3939 be all zero. For signed fields, the high-order bits should agree with
3940 the sign bit. */
3942 if (lunsignedp)
3944 if (wi::lrshift (rhs, lbitsize) != 0)
3946 warning (0, "comparison is always %d due to width of bit-field",
3947 code == NE_EXPR);
3948 return constant_boolean_node (code == NE_EXPR, compare_type);
3951 else
3953 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3954 if (tem != 0 && tem != -1)
3956 warning (0, "comparison is always %d due to width of bit-field",
3957 code == NE_EXPR);
3958 return constant_boolean_node (code == NE_EXPR, compare_type);
3962 /* Single-bit compares should always be against zero. */
3963 if (lbitsize == 1 && ! integer_zerop (rhs))
3965 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3966 rhs = build_int_cst (type, 0);
3969 /* Make a new bitfield reference, shift the constant over the
3970 appropriate number of bits and mask it with the computed mask
3971 (in case this was a signed field). If we changed it, make a new one. */
3972 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
3973 nbitsize, nbitpos, 1, lreversep);
3975 rhs = const_binop (BIT_AND_EXPR,
3976 const_binop (LSHIFT_EXPR,
3977 fold_convert_loc (loc, unsigned_type, rhs),
3978 size_int (lbitpos)),
3979 mask);
3981 lhs = build2_loc (loc, code, compare_type,
3982 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3983 return lhs;
3986 /* Subroutine for fold_truth_andor_1: decode a field reference.
3988 If EXP is a comparison reference, we return the innermost reference.
3990 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3991 set to the starting bit number.
3993 If the innermost field can be completely contained in a mode-sized
3994 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3996 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3997 otherwise it is not changed.
3999 *PUNSIGNEDP is set to the signedness of the field.
4001 *PREVERSEP is set to the storage order of the field.
4003 *PMASK is set to the mask used. This is either contained in a
4004 BIT_AND_EXPR or derived from the width of the field.
4006 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4008 Return 0 if this is not a component reference or is one that we can't
4009 do anything with. */
4011 static tree
4012 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4013 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4014 int *punsignedp, int *preversep, int *pvolatilep,
4015 tree *pmask, tree *pand_mask)
4017 tree exp = *exp_;
4018 tree outer_type = 0;
4019 tree and_mask = 0;
4020 tree mask, inner, offset;
4021 tree unsigned_type;
4022 unsigned int precision;
4024 /* All the optimizations using this function assume integer fields.
4025 There are problems with FP fields since the type_for_size call
4026 below can fail for, e.g., XFmode. */
4027 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4028 return 0;
4030 /* We are interested in the bare arrangement of bits, so strip everything
4031 that doesn't affect the machine mode. However, record the type of the
4032 outermost expression if it may matter below. */
4033 if (CONVERT_EXPR_P (exp)
4034 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4035 outer_type = TREE_TYPE (exp);
4036 STRIP_NOPS (exp);
4038 if (TREE_CODE (exp) == BIT_AND_EXPR)
4040 and_mask = TREE_OPERAND (exp, 1);
4041 exp = TREE_OPERAND (exp, 0);
4042 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4043 if (TREE_CODE (and_mask) != INTEGER_CST)
4044 return 0;
4047 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4048 punsignedp, preversep, pvolatilep, false);
4049 if ((inner == exp && and_mask == 0)
4050 || *pbitsize < 0 || offset != 0
4051 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4052 return 0;
4054 *exp_ = exp;
4056 /* If the number of bits in the reference is the same as the bitsize of
4057 the outer type, then the outer type gives the signedness. Otherwise
4058 (in case of a small bitfield) the signedness is unchanged. */
4059 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4060 *punsignedp = TYPE_UNSIGNED (outer_type);
4062 /* Compute the mask to access the bitfield. */
4063 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4064 precision = TYPE_PRECISION (unsigned_type);
4066 mask = build_int_cst_type (unsigned_type, -1);
4068 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4069 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4071 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4072 if (and_mask != 0)
4073 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4074 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4076 *pmask = mask;
4077 *pand_mask = and_mask;
4078 return inner;
4081 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4082 bit positions and MASK is SIGNED. */
4084 static int
4085 all_ones_mask_p (const_tree mask, unsigned int size)
4087 tree type = TREE_TYPE (mask);
4088 unsigned int precision = TYPE_PRECISION (type);
4090 /* If this function returns true when the type of the mask is
4091 UNSIGNED, then there will be errors. In particular see
4092 gcc.c-torture/execute/990326-1.c. There does not appear to be
4093 any documentation paper trail as to why this is so. But the pre
4094 wide-int worked with that restriction and it has been preserved
4095 here. */
4096 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4097 return false;
4099 return wi::mask (size, false, precision) == mask;
4102 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4103 represents the sign bit of EXP's type. If EXP represents a sign
4104 or zero extension, also test VAL against the unextended type.
4105 The return value is the (sub)expression whose sign bit is VAL,
4106 or NULL_TREE otherwise. */
4108 tree
4109 sign_bit_p (tree exp, const_tree val)
4111 int width;
4112 tree t;
4114 /* Tree EXP must have an integral type. */
4115 t = TREE_TYPE (exp);
4116 if (! INTEGRAL_TYPE_P (t))
4117 return NULL_TREE;
4119 /* Tree VAL must be an integer constant. */
4120 if (TREE_CODE (val) != INTEGER_CST
4121 || TREE_OVERFLOW (val))
4122 return NULL_TREE;
4124 width = TYPE_PRECISION (t);
4125 if (wi::only_sign_bit_p (val, width))
4126 return exp;
4128 /* Handle extension from a narrower type. */
4129 if (TREE_CODE (exp) == NOP_EXPR
4130 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4131 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4133 return NULL_TREE;
4136 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4137 to be evaluated unconditionally. */
4139 static int
4140 simple_operand_p (const_tree exp)
4142 /* Strip any conversions that don't change the machine mode. */
4143 STRIP_NOPS (exp);
4145 return (CONSTANT_CLASS_P (exp)
4146 || TREE_CODE (exp) == SSA_NAME
4147 || (DECL_P (exp)
4148 && ! TREE_ADDRESSABLE (exp)
4149 && ! TREE_THIS_VOLATILE (exp)
4150 && ! DECL_NONLOCAL (exp)
4151 /* Don't regard global variables as simple. They may be
4152 allocated in ways unknown to the compiler (shared memory,
4153 #pragma weak, etc). */
4154 && ! TREE_PUBLIC (exp)
4155 && ! DECL_EXTERNAL (exp)
4156 /* Weakrefs are not safe to be read, since they can be NULL.
4157 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4158 have DECL_WEAK flag set. */
4159 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4160 /* Loading a static variable is unduly expensive, but global
4161 registers aren't expensive. */
4162 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4165 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4166 to be evaluated unconditionally.
4167 I addition to simple_operand_p, we assume that comparisons, conversions,
4168 and logic-not operations are simple, if their operands are simple, too. */
4170 static bool
4171 simple_operand_p_2 (tree exp)
4173 enum tree_code code;
4175 if (TREE_SIDE_EFFECTS (exp)
4176 || tree_could_trap_p (exp))
4177 return false;
4179 while (CONVERT_EXPR_P (exp))
4180 exp = TREE_OPERAND (exp, 0);
4182 code = TREE_CODE (exp);
4184 if (TREE_CODE_CLASS (code) == tcc_comparison)
4185 return (simple_operand_p (TREE_OPERAND (exp, 0))
4186 && simple_operand_p (TREE_OPERAND (exp, 1)));
4188 if (code == TRUTH_NOT_EXPR)
4189 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4191 return simple_operand_p (exp);
4195 /* The following functions are subroutines to fold_range_test and allow it to
4196 try to change a logical combination of comparisons into a range test.
4198 For example, both
4199 X == 2 || X == 3 || X == 4 || X == 5
4201 X >= 2 && X <= 5
4202 are converted to
4203 (unsigned) (X - 2) <= 3
4205 We describe each set of comparisons as being either inside or outside
4206 a range, using a variable named like IN_P, and then describe the
4207 range with a lower and upper bound. If one of the bounds is omitted,
4208 it represents either the highest or lowest value of the type.
4210 In the comments below, we represent a range by two numbers in brackets
4211 preceded by a "+" to designate being inside that range, or a "-" to
4212 designate being outside that range, so the condition can be inverted by
4213 flipping the prefix. An omitted bound is represented by a "-". For
4214 example, "- [-, 10]" means being outside the range starting at the lowest
4215 possible value and ending at 10, in other words, being greater than 10.
4216 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4217 always false.
4219 We set up things so that the missing bounds are handled in a consistent
4220 manner so neither a missing bound nor "true" and "false" need to be
4221 handled using a special case. */
4223 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4224 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4225 and UPPER1_P are nonzero if the respective argument is an upper bound
4226 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4227 must be specified for a comparison. ARG1 will be converted to ARG0's
4228 type if both are specified. */
4230 static tree
4231 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4232 tree arg1, int upper1_p)
4234 tree tem;
4235 int result;
4236 int sgn0, sgn1;
4238 /* If neither arg represents infinity, do the normal operation.
4239 Else, if not a comparison, return infinity. Else handle the special
4240 comparison rules. Note that most of the cases below won't occur, but
4241 are handled for consistency. */
4243 if (arg0 != 0 && arg1 != 0)
4245 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4246 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4247 STRIP_NOPS (tem);
4248 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4251 if (TREE_CODE_CLASS (code) != tcc_comparison)
4252 return 0;
4254 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4255 for neither. In real maths, we cannot assume open ended ranges are
4256 the same. But, this is computer arithmetic, where numbers are finite.
4257 We can therefore make the transformation of any unbounded range with
4258 the value Z, Z being greater than any representable number. This permits
4259 us to treat unbounded ranges as equal. */
4260 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4261 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4262 switch (code)
4264 case EQ_EXPR:
4265 result = sgn0 == sgn1;
4266 break;
4267 case NE_EXPR:
4268 result = sgn0 != sgn1;
4269 break;
4270 case LT_EXPR:
4271 result = sgn0 < sgn1;
4272 break;
4273 case LE_EXPR:
4274 result = sgn0 <= sgn1;
4275 break;
4276 case GT_EXPR:
4277 result = sgn0 > sgn1;
4278 break;
4279 case GE_EXPR:
4280 result = sgn0 >= sgn1;
4281 break;
4282 default:
4283 gcc_unreachable ();
4286 return constant_boolean_node (result, type);
4289 /* Helper routine for make_range. Perform one step for it, return
4290 new expression if the loop should continue or NULL_TREE if it should
4291 stop. */
4293 tree
4294 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4295 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4296 bool *strict_overflow_p)
4298 tree arg0_type = TREE_TYPE (arg0);
4299 tree n_low, n_high, low = *p_low, high = *p_high;
4300 int in_p = *p_in_p, n_in_p;
4302 switch (code)
4304 case TRUTH_NOT_EXPR:
4305 /* We can only do something if the range is testing for zero. */
4306 if (low == NULL_TREE || high == NULL_TREE
4307 || ! integer_zerop (low) || ! integer_zerop (high))
4308 return NULL_TREE;
4309 *p_in_p = ! in_p;
4310 return arg0;
4312 case EQ_EXPR: case NE_EXPR:
4313 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4314 /* We can only do something if the range is testing for zero
4315 and if the second operand is an integer constant. Note that
4316 saying something is "in" the range we make is done by
4317 complementing IN_P since it will set in the initial case of
4318 being not equal to zero; "out" is leaving it alone. */
4319 if (low == NULL_TREE || high == NULL_TREE
4320 || ! integer_zerop (low) || ! integer_zerop (high)
4321 || TREE_CODE (arg1) != INTEGER_CST)
4322 return NULL_TREE;
4324 switch (code)
4326 case NE_EXPR: /* - [c, c] */
4327 low = high = arg1;
4328 break;
4329 case EQ_EXPR: /* + [c, c] */
4330 in_p = ! in_p, low = high = arg1;
4331 break;
4332 case GT_EXPR: /* - [-, c] */
4333 low = 0, high = arg1;
4334 break;
4335 case GE_EXPR: /* + [c, -] */
4336 in_p = ! in_p, low = arg1, high = 0;
4337 break;
4338 case LT_EXPR: /* - [c, -] */
4339 low = arg1, high = 0;
4340 break;
4341 case LE_EXPR: /* + [-, c] */
4342 in_p = ! in_p, low = 0, high = arg1;
4343 break;
4344 default:
4345 gcc_unreachable ();
4348 /* If this is an unsigned comparison, we also know that EXP is
4349 greater than or equal to zero. We base the range tests we make
4350 on that fact, so we record it here so we can parse existing
4351 range tests. We test arg0_type since often the return type
4352 of, e.g. EQ_EXPR, is boolean. */
4353 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4355 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4356 in_p, low, high, 1,
4357 build_int_cst (arg0_type, 0),
4358 NULL_TREE))
4359 return NULL_TREE;
4361 in_p = n_in_p, low = n_low, high = n_high;
4363 /* If the high bound is missing, but we have a nonzero low
4364 bound, reverse the range so it goes from zero to the low bound
4365 minus 1. */
4366 if (high == 0 && low && ! integer_zerop (low))
4368 in_p = ! in_p;
4369 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4370 build_int_cst (TREE_TYPE (low), 1), 0);
4371 low = build_int_cst (arg0_type, 0);
4375 *p_low = low;
4376 *p_high = high;
4377 *p_in_p = in_p;
4378 return arg0;
4380 case NEGATE_EXPR:
4381 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4382 low and high are non-NULL, then normalize will DTRT. */
4383 if (!TYPE_UNSIGNED (arg0_type)
4384 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4386 if (low == NULL_TREE)
4387 low = TYPE_MIN_VALUE (arg0_type);
4388 if (high == NULL_TREE)
4389 high = TYPE_MAX_VALUE (arg0_type);
4392 /* (-x) IN [a,b] -> x in [-b, -a] */
4393 n_low = range_binop (MINUS_EXPR, exp_type,
4394 build_int_cst (exp_type, 0),
4395 0, high, 1);
4396 n_high = range_binop (MINUS_EXPR, exp_type,
4397 build_int_cst (exp_type, 0),
4398 0, low, 0);
4399 if (n_high != 0 && TREE_OVERFLOW (n_high))
4400 return NULL_TREE;
4401 goto normalize;
4403 case BIT_NOT_EXPR:
4404 /* ~ X -> -X - 1 */
4405 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4406 build_int_cst (exp_type, 1));
4408 case PLUS_EXPR:
4409 case MINUS_EXPR:
4410 if (TREE_CODE (arg1) != INTEGER_CST)
4411 return NULL_TREE;
4413 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4414 move a constant to the other side. */
4415 if (!TYPE_UNSIGNED (arg0_type)
4416 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4417 return NULL_TREE;
4419 /* If EXP is signed, any overflow in the computation is undefined,
4420 so we don't worry about it so long as our computations on
4421 the bounds don't overflow. For unsigned, overflow is defined
4422 and this is exactly the right thing. */
4423 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4424 arg0_type, low, 0, arg1, 0);
4425 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4426 arg0_type, high, 1, arg1, 0);
4427 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4428 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4429 return NULL_TREE;
4431 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4432 *strict_overflow_p = true;
4434 normalize:
4435 /* Check for an unsigned range which has wrapped around the maximum
4436 value thus making n_high < n_low, and normalize it. */
4437 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4439 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4440 build_int_cst (TREE_TYPE (n_high), 1), 0);
4441 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4442 build_int_cst (TREE_TYPE (n_low), 1), 0);
4444 /* If the range is of the form +/- [ x+1, x ], we won't
4445 be able to normalize it. But then, it represents the
4446 whole range or the empty set, so make it
4447 +/- [ -, - ]. */
4448 if (tree_int_cst_equal (n_low, low)
4449 && tree_int_cst_equal (n_high, high))
4450 low = high = 0;
4451 else
4452 in_p = ! in_p;
4454 else
4455 low = n_low, high = n_high;
4457 *p_low = low;
4458 *p_high = high;
4459 *p_in_p = in_p;
4460 return arg0;
4462 CASE_CONVERT:
4463 case NON_LVALUE_EXPR:
4464 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4465 return NULL_TREE;
4467 if (! INTEGRAL_TYPE_P (arg0_type)
4468 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4469 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4470 return NULL_TREE;
4472 n_low = low, n_high = high;
4474 if (n_low != 0)
4475 n_low = fold_convert_loc (loc, arg0_type, n_low);
4477 if (n_high != 0)
4478 n_high = fold_convert_loc (loc, arg0_type, n_high);
4480 /* If we're converting arg0 from an unsigned type, to exp,
4481 a signed type, we will be doing the comparison as unsigned.
4482 The tests above have already verified that LOW and HIGH
4483 are both positive.
4485 So we have to ensure that we will handle large unsigned
4486 values the same way that the current signed bounds treat
4487 negative values. */
4489 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4491 tree high_positive;
4492 tree equiv_type;
4493 /* For fixed-point modes, we need to pass the saturating flag
4494 as the 2nd parameter. */
4495 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4496 equiv_type
4497 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4498 TYPE_SATURATING (arg0_type));
4499 else
4500 equiv_type
4501 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4503 /* A range without an upper bound is, naturally, unbounded.
4504 Since convert would have cropped a very large value, use
4505 the max value for the destination type. */
4506 high_positive
4507 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4508 : TYPE_MAX_VALUE (arg0_type);
4510 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4511 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4512 fold_convert_loc (loc, arg0_type,
4513 high_positive),
4514 build_int_cst (arg0_type, 1));
4516 /* If the low bound is specified, "and" the range with the
4517 range for which the original unsigned value will be
4518 positive. */
4519 if (low != 0)
4521 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4522 1, fold_convert_loc (loc, arg0_type,
4523 integer_zero_node),
4524 high_positive))
4525 return NULL_TREE;
4527 in_p = (n_in_p == in_p);
4529 else
4531 /* Otherwise, "or" the range with the range of the input
4532 that will be interpreted as negative. */
4533 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4534 1, fold_convert_loc (loc, arg0_type,
4535 integer_zero_node),
4536 high_positive))
4537 return NULL_TREE;
4539 in_p = (in_p != n_in_p);
4543 *p_low = n_low;
4544 *p_high = n_high;
4545 *p_in_p = in_p;
4546 return arg0;
4548 default:
4549 return NULL_TREE;
4553 /* Given EXP, a logical expression, set the range it is testing into
4554 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4555 actually being tested. *PLOW and *PHIGH will be made of the same
4556 type as the returned expression. If EXP is not a comparison, we
4557 will most likely not be returning a useful value and range. Set
4558 *STRICT_OVERFLOW_P to true if the return value is only valid
4559 because signed overflow is undefined; otherwise, do not change
4560 *STRICT_OVERFLOW_P. */
4562 tree
4563 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4564 bool *strict_overflow_p)
4566 enum tree_code code;
4567 tree arg0, arg1 = NULL_TREE;
4568 tree exp_type, nexp;
4569 int in_p;
4570 tree low, high;
4571 location_t loc = EXPR_LOCATION (exp);
4573 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4574 and see if we can refine the range. Some of the cases below may not
4575 happen, but it doesn't seem worth worrying about this. We "continue"
4576 the outer loop when we've changed something; otherwise we "break"
4577 the switch, which will "break" the while. */
4579 in_p = 0;
4580 low = high = build_int_cst (TREE_TYPE (exp), 0);
4582 while (1)
4584 code = TREE_CODE (exp);
4585 exp_type = TREE_TYPE (exp);
4586 arg0 = NULL_TREE;
4588 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4590 if (TREE_OPERAND_LENGTH (exp) > 0)
4591 arg0 = TREE_OPERAND (exp, 0);
4592 if (TREE_CODE_CLASS (code) == tcc_binary
4593 || TREE_CODE_CLASS (code) == tcc_comparison
4594 || (TREE_CODE_CLASS (code) == tcc_expression
4595 && TREE_OPERAND_LENGTH (exp) > 1))
4596 arg1 = TREE_OPERAND (exp, 1);
4598 if (arg0 == NULL_TREE)
4599 break;
4601 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4602 &high, &in_p, strict_overflow_p);
4603 if (nexp == NULL_TREE)
4604 break;
4605 exp = nexp;
4608 /* If EXP is a constant, we can evaluate whether this is true or false. */
4609 if (TREE_CODE (exp) == INTEGER_CST)
4611 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4612 exp, 0, low, 0))
4613 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4614 exp, 1, high, 1)));
4615 low = high = 0;
4616 exp = 0;
4619 *pin_p = in_p, *plow = low, *phigh = high;
4620 return exp;
4623 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4624 type, TYPE, return an expression to test if EXP is in (or out of, depending
4625 on IN_P) the range. Return 0 if the test couldn't be created. */
4627 tree
4628 build_range_check (location_t loc, tree type, tree exp, int in_p,
4629 tree low, tree high)
4631 tree etype = TREE_TYPE (exp), value;
4633 /* Disable this optimization for function pointer expressions
4634 on targets that require function pointer canonicalization. */
4635 if (targetm.have_canonicalize_funcptr_for_compare ()
4636 && TREE_CODE (etype) == POINTER_TYPE
4637 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4638 return NULL_TREE;
4640 if (! in_p)
4642 value = build_range_check (loc, type, exp, 1, low, high);
4643 if (value != 0)
4644 return invert_truthvalue_loc (loc, value);
4646 return 0;
4649 if (low == 0 && high == 0)
4650 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4652 if (low == 0)
4653 return fold_build2_loc (loc, LE_EXPR, type, exp,
4654 fold_convert_loc (loc, etype, high));
4656 if (high == 0)
4657 return fold_build2_loc (loc, GE_EXPR, type, exp,
4658 fold_convert_loc (loc, etype, low));
4660 if (operand_equal_p (low, high, 0))
4661 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4662 fold_convert_loc (loc, etype, low));
4664 if (integer_zerop (low))
4666 if (! TYPE_UNSIGNED (etype))
4668 etype = unsigned_type_for (etype);
4669 high = fold_convert_loc (loc, etype, high);
4670 exp = fold_convert_loc (loc, etype, exp);
4672 return build_range_check (loc, type, exp, 1, 0, high);
4675 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4676 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4678 int prec = TYPE_PRECISION (etype);
4680 if (wi::mask (prec - 1, false, prec) == high)
4682 if (TYPE_UNSIGNED (etype))
4684 tree signed_etype = signed_type_for (etype);
4685 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4686 etype
4687 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4688 else
4689 etype = signed_etype;
4690 exp = fold_convert_loc (loc, etype, exp);
4692 return fold_build2_loc (loc, GT_EXPR, type, exp,
4693 build_int_cst (etype, 0));
4697 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4698 This requires wrap-around arithmetics for the type of the expression.
4699 First make sure that arithmetics in this type is valid, then make sure
4700 that it wraps around. */
4701 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4702 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4703 TYPE_UNSIGNED (etype));
4705 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4707 tree utype, minv, maxv;
4709 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4710 for the type in question, as we rely on this here. */
4711 utype = unsigned_type_for (etype);
4712 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4713 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4714 build_int_cst (TREE_TYPE (maxv), 1), 1);
4715 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4717 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4718 minv, 1, maxv, 1)))
4719 etype = utype;
4720 else
4721 return 0;
4724 high = fold_convert_loc (loc, etype, high);
4725 low = fold_convert_loc (loc, etype, low);
4726 exp = fold_convert_loc (loc, etype, exp);
4728 value = const_binop (MINUS_EXPR, high, low);
4731 if (POINTER_TYPE_P (etype))
4733 if (value != 0 && !TREE_OVERFLOW (value))
4735 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4736 return build_range_check (loc, type,
4737 fold_build_pointer_plus_loc (loc, exp, low),
4738 1, build_int_cst (etype, 0), value);
4740 return 0;
4743 if (value != 0 && !TREE_OVERFLOW (value))
4744 return build_range_check (loc, type,
4745 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4746 1, build_int_cst (etype, 0), value);
4748 return 0;
4751 /* Return the predecessor of VAL in its type, handling the infinite case. */
4753 static tree
4754 range_predecessor (tree val)
4756 tree type = TREE_TYPE (val);
4758 if (INTEGRAL_TYPE_P (type)
4759 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4760 return 0;
4761 else
4762 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4763 build_int_cst (TREE_TYPE (val), 1), 0);
4766 /* Return the successor of VAL in its type, handling the infinite case. */
4768 static tree
4769 range_successor (tree val)
4771 tree type = TREE_TYPE (val);
4773 if (INTEGRAL_TYPE_P (type)
4774 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4775 return 0;
4776 else
4777 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4778 build_int_cst (TREE_TYPE (val), 1), 0);
4781 /* Given two ranges, see if we can merge them into one. Return 1 if we
4782 can, 0 if we can't. Set the output range into the specified parameters. */
4784 bool
4785 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4786 tree high0, int in1_p, tree low1, tree high1)
4788 int no_overlap;
4789 int subset;
4790 int temp;
4791 tree tem;
4792 int in_p;
4793 tree low, high;
4794 int lowequal = ((low0 == 0 && low1 == 0)
4795 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4796 low0, 0, low1, 0)));
4797 int highequal = ((high0 == 0 && high1 == 0)
4798 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4799 high0, 1, high1, 1)));
4801 /* Make range 0 be the range that starts first, or ends last if they
4802 start at the same value. Swap them if it isn't. */
4803 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4804 low0, 0, low1, 0))
4805 || (lowequal
4806 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4807 high1, 1, high0, 1))))
4809 temp = in0_p, in0_p = in1_p, in1_p = temp;
4810 tem = low0, low0 = low1, low1 = tem;
4811 tem = high0, high0 = high1, high1 = tem;
4814 /* Now flag two cases, whether the ranges are disjoint or whether the
4815 second range is totally subsumed in the first. Note that the tests
4816 below are simplified by the ones above. */
4817 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4818 high0, 1, low1, 0));
4819 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4820 high1, 1, high0, 1));
4822 /* We now have four cases, depending on whether we are including or
4823 excluding the two ranges. */
4824 if (in0_p && in1_p)
4826 /* If they don't overlap, the result is false. If the second range
4827 is a subset it is the result. Otherwise, the range is from the start
4828 of the second to the end of the first. */
4829 if (no_overlap)
4830 in_p = 0, low = high = 0;
4831 else if (subset)
4832 in_p = 1, low = low1, high = high1;
4833 else
4834 in_p = 1, low = low1, high = high0;
4837 else if (in0_p && ! in1_p)
4839 /* If they don't overlap, the result is the first range. If they are
4840 equal, the result is false. If the second range is a subset of the
4841 first, and the ranges begin at the same place, we go from just after
4842 the end of the second range to the end of the first. If the second
4843 range is not a subset of the first, or if it is a subset and both
4844 ranges end at the same place, the range starts at the start of the
4845 first range and ends just before the second range.
4846 Otherwise, we can't describe this as a single range. */
4847 if (no_overlap)
4848 in_p = 1, low = low0, high = high0;
4849 else if (lowequal && highequal)
4850 in_p = 0, low = high = 0;
4851 else if (subset && lowequal)
4853 low = range_successor (high1);
4854 high = high0;
4855 in_p = 1;
4856 if (low == 0)
4858 /* We are in the weird situation where high0 > high1 but
4859 high1 has no successor. Punt. */
4860 return 0;
4863 else if (! subset || highequal)
4865 low = low0;
4866 high = range_predecessor (low1);
4867 in_p = 1;
4868 if (high == 0)
4870 /* low0 < low1 but low1 has no predecessor. Punt. */
4871 return 0;
4874 else
4875 return 0;
4878 else if (! in0_p && in1_p)
4880 /* If they don't overlap, the result is the second range. If the second
4881 is a subset of the first, the result is false. Otherwise,
4882 the range starts just after the first range and ends at the
4883 end of the second. */
4884 if (no_overlap)
4885 in_p = 1, low = low1, high = high1;
4886 else if (subset || highequal)
4887 in_p = 0, low = high = 0;
4888 else
4890 low = range_successor (high0);
4891 high = high1;
4892 in_p = 1;
4893 if (low == 0)
4895 /* high1 > high0 but high0 has no successor. Punt. */
4896 return 0;
4901 else
4903 /* The case where we are excluding both ranges. Here the complex case
4904 is if they don't overlap. In that case, the only time we have a
4905 range is if they are adjacent. If the second is a subset of the
4906 first, the result is the first. Otherwise, the range to exclude
4907 starts at the beginning of the first range and ends at the end of the
4908 second. */
4909 if (no_overlap)
4911 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4912 range_successor (high0),
4913 1, low1, 0)))
4914 in_p = 0, low = low0, high = high1;
4915 else
4917 /* Canonicalize - [min, x] into - [-, x]. */
4918 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4919 switch (TREE_CODE (TREE_TYPE (low0)))
4921 case ENUMERAL_TYPE:
4922 if (TYPE_PRECISION (TREE_TYPE (low0))
4923 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4924 break;
4925 /* FALLTHROUGH */
4926 case INTEGER_TYPE:
4927 if (tree_int_cst_equal (low0,
4928 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4929 low0 = 0;
4930 break;
4931 case POINTER_TYPE:
4932 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4933 && integer_zerop (low0))
4934 low0 = 0;
4935 break;
4936 default:
4937 break;
4940 /* Canonicalize - [x, max] into - [x, -]. */
4941 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4942 switch (TREE_CODE (TREE_TYPE (high1)))
4944 case ENUMERAL_TYPE:
4945 if (TYPE_PRECISION (TREE_TYPE (high1))
4946 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4947 break;
4948 /* FALLTHROUGH */
4949 case INTEGER_TYPE:
4950 if (tree_int_cst_equal (high1,
4951 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4952 high1 = 0;
4953 break;
4954 case POINTER_TYPE:
4955 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4956 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4957 high1, 1,
4958 build_int_cst (TREE_TYPE (high1), 1),
4959 1)))
4960 high1 = 0;
4961 break;
4962 default:
4963 break;
4966 /* The ranges might be also adjacent between the maximum and
4967 minimum values of the given type. For
4968 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4969 return + [x + 1, y - 1]. */
4970 if (low0 == 0 && high1 == 0)
4972 low = range_successor (high0);
4973 high = range_predecessor (low1);
4974 if (low == 0 || high == 0)
4975 return 0;
4977 in_p = 1;
4979 else
4980 return 0;
4983 else if (subset)
4984 in_p = 0, low = low0, high = high0;
4985 else
4986 in_p = 0, low = low0, high = high1;
4989 *pin_p = in_p, *plow = low, *phigh = high;
4990 return 1;
4994 /* Subroutine of fold, looking inside expressions of the form
4995 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4996 of the COND_EXPR. This function is being used also to optimize
4997 A op B ? C : A, by reversing the comparison first.
4999 Return a folded expression whose code is not a COND_EXPR
5000 anymore, or NULL_TREE if no folding opportunity is found. */
5002 static tree
5003 fold_cond_expr_with_comparison (location_t loc, tree type,
5004 tree arg0, tree arg1, tree arg2)
5006 enum tree_code comp_code = TREE_CODE (arg0);
5007 tree arg00 = TREE_OPERAND (arg0, 0);
5008 tree arg01 = TREE_OPERAND (arg0, 1);
5009 tree arg1_type = TREE_TYPE (arg1);
5010 tree tem;
5012 STRIP_NOPS (arg1);
5013 STRIP_NOPS (arg2);
5015 /* If we have A op 0 ? A : -A, consider applying the following
5016 transformations:
5018 A == 0? A : -A same as -A
5019 A != 0? A : -A same as A
5020 A >= 0? A : -A same as abs (A)
5021 A > 0? A : -A same as abs (A)
5022 A <= 0? A : -A same as -abs (A)
5023 A < 0? A : -A same as -abs (A)
5025 None of these transformations work for modes with signed
5026 zeros. If A is +/-0, the first two transformations will
5027 change the sign of the result (from +0 to -0, or vice
5028 versa). The last four will fix the sign of the result,
5029 even though the original expressions could be positive or
5030 negative, depending on the sign of A.
5032 Note that all these transformations are correct if A is
5033 NaN, since the two alternatives (A and -A) are also NaNs. */
5034 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5035 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5036 ? real_zerop (arg01)
5037 : integer_zerop (arg01))
5038 && ((TREE_CODE (arg2) == NEGATE_EXPR
5039 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5040 /* In the case that A is of the form X-Y, '-A' (arg2) may
5041 have already been folded to Y-X, check for that. */
5042 || (TREE_CODE (arg1) == MINUS_EXPR
5043 && TREE_CODE (arg2) == MINUS_EXPR
5044 && operand_equal_p (TREE_OPERAND (arg1, 0),
5045 TREE_OPERAND (arg2, 1), 0)
5046 && operand_equal_p (TREE_OPERAND (arg1, 1),
5047 TREE_OPERAND (arg2, 0), 0))))
5048 switch (comp_code)
5050 case EQ_EXPR:
5051 case UNEQ_EXPR:
5052 tem = fold_convert_loc (loc, arg1_type, arg1);
5053 return pedantic_non_lvalue_loc (loc,
5054 fold_convert_loc (loc, type,
5055 negate_expr (tem)));
5056 case NE_EXPR:
5057 case LTGT_EXPR:
5058 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5059 case UNGE_EXPR:
5060 case UNGT_EXPR:
5061 if (flag_trapping_math)
5062 break;
5063 /* Fall through. */
5064 case GE_EXPR:
5065 case GT_EXPR:
5066 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5067 break;
5068 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5069 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5070 case UNLE_EXPR:
5071 case UNLT_EXPR:
5072 if (flag_trapping_math)
5073 break;
5074 case LE_EXPR:
5075 case LT_EXPR:
5076 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5077 break;
5078 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5079 return negate_expr (fold_convert_loc (loc, type, tem));
5080 default:
5081 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5082 break;
5085 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5086 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5087 both transformations are correct when A is NaN: A != 0
5088 is then true, and A == 0 is false. */
5090 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5091 && integer_zerop (arg01) && integer_zerop (arg2))
5093 if (comp_code == NE_EXPR)
5094 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5095 else if (comp_code == EQ_EXPR)
5096 return build_zero_cst (type);
5099 /* Try some transformations of A op B ? A : B.
5101 A == B? A : B same as B
5102 A != B? A : B same as A
5103 A >= B? A : B same as max (A, B)
5104 A > B? A : B same as max (B, A)
5105 A <= B? A : B same as min (A, B)
5106 A < B? A : B same as min (B, A)
5108 As above, these transformations don't work in the presence
5109 of signed zeros. For example, if A and B are zeros of
5110 opposite sign, the first two transformations will change
5111 the sign of the result. In the last four, the original
5112 expressions give different results for (A=+0, B=-0) and
5113 (A=-0, B=+0), but the transformed expressions do not.
5115 The first two transformations are correct if either A or B
5116 is a NaN. In the first transformation, the condition will
5117 be false, and B will indeed be chosen. In the case of the
5118 second transformation, the condition A != B will be true,
5119 and A will be chosen.
5121 The conversions to max() and min() are not correct if B is
5122 a number and A is not. The conditions in the original
5123 expressions will be false, so all four give B. The min()
5124 and max() versions would give a NaN instead. */
5125 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5126 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5127 /* Avoid these transformations if the COND_EXPR may be used
5128 as an lvalue in the C++ front-end. PR c++/19199. */
5129 && (in_gimple_form
5130 || VECTOR_TYPE_P (type)
5131 || (! lang_GNU_CXX ()
5132 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5133 || ! maybe_lvalue_p (arg1)
5134 || ! maybe_lvalue_p (arg2)))
5136 tree comp_op0 = arg00;
5137 tree comp_op1 = arg01;
5138 tree comp_type = TREE_TYPE (comp_op0);
5140 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5141 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5143 comp_type = type;
5144 comp_op0 = arg1;
5145 comp_op1 = arg2;
5148 switch (comp_code)
5150 case EQ_EXPR:
5151 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5152 case NE_EXPR:
5153 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5154 case LE_EXPR:
5155 case LT_EXPR:
5156 case UNLE_EXPR:
5157 case UNLT_EXPR:
5158 /* In C++ a ?: expression can be an lvalue, so put the
5159 operand which will be used if they are equal first
5160 so that we can convert this back to the
5161 corresponding COND_EXPR. */
5162 if (!HONOR_NANS (arg1))
5164 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5165 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5166 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5167 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5168 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5169 comp_op1, comp_op0);
5170 return pedantic_non_lvalue_loc (loc,
5171 fold_convert_loc (loc, type, tem));
5173 break;
5174 case GE_EXPR:
5175 case GT_EXPR:
5176 case UNGE_EXPR:
5177 case UNGT_EXPR:
5178 if (!HONOR_NANS (arg1))
5180 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5181 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5182 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5183 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5184 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5185 comp_op1, comp_op0);
5186 return pedantic_non_lvalue_loc (loc,
5187 fold_convert_loc (loc, type, tem));
5189 break;
5190 case UNEQ_EXPR:
5191 if (!HONOR_NANS (arg1))
5192 return pedantic_non_lvalue_loc (loc,
5193 fold_convert_loc (loc, type, arg2));
5194 break;
5195 case LTGT_EXPR:
5196 if (!HONOR_NANS (arg1))
5197 return pedantic_non_lvalue_loc (loc,
5198 fold_convert_loc (loc, type, arg1));
5199 break;
5200 default:
5201 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5202 break;
5206 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5207 we might still be able to simplify this. For example,
5208 if C1 is one less or one more than C2, this might have started
5209 out as a MIN or MAX and been transformed by this function.
5210 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5212 if (INTEGRAL_TYPE_P (type)
5213 && TREE_CODE (arg01) == INTEGER_CST
5214 && TREE_CODE (arg2) == INTEGER_CST)
5215 switch (comp_code)
5217 case EQ_EXPR:
5218 if (TREE_CODE (arg1) == INTEGER_CST)
5219 break;
5220 /* We can replace A with C1 in this case. */
5221 arg1 = fold_convert_loc (loc, type, arg01);
5222 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5224 case LT_EXPR:
5225 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5226 MIN_EXPR, to preserve the signedness of the comparison. */
5227 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5228 OEP_ONLY_CONST)
5229 && operand_equal_p (arg01,
5230 const_binop (PLUS_EXPR, arg2,
5231 build_int_cst (type, 1)),
5232 OEP_ONLY_CONST))
5234 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5235 fold_convert_loc (loc, TREE_TYPE (arg00),
5236 arg2));
5237 return pedantic_non_lvalue_loc (loc,
5238 fold_convert_loc (loc, type, tem));
5240 break;
5242 case LE_EXPR:
5243 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5244 as above. */
5245 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5246 OEP_ONLY_CONST)
5247 && operand_equal_p (arg01,
5248 const_binop (MINUS_EXPR, arg2,
5249 build_int_cst (type, 1)),
5250 OEP_ONLY_CONST))
5252 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5253 fold_convert_loc (loc, TREE_TYPE (arg00),
5254 arg2));
5255 return pedantic_non_lvalue_loc (loc,
5256 fold_convert_loc (loc, type, tem));
5258 break;
5260 case GT_EXPR:
5261 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5262 MAX_EXPR, to preserve the signedness of the comparison. */
5263 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5264 OEP_ONLY_CONST)
5265 && operand_equal_p (arg01,
5266 const_binop (MINUS_EXPR, arg2,
5267 build_int_cst (type, 1)),
5268 OEP_ONLY_CONST))
5270 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5271 fold_convert_loc (loc, TREE_TYPE (arg00),
5272 arg2));
5273 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5275 break;
5277 case GE_EXPR:
5278 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5279 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5280 OEP_ONLY_CONST)
5281 && operand_equal_p (arg01,
5282 const_binop (PLUS_EXPR, arg2,
5283 build_int_cst (type, 1)),
5284 OEP_ONLY_CONST))
5286 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5287 fold_convert_loc (loc, TREE_TYPE (arg00),
5288 arg2));
5289 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5291 break;
5292 case NE_EXPR:
5293 break;
5294 default:
5295 gcc_unreachable ();
5298 return NULL_TREE;
5303 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5304 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5305 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5306 false) >= 2)
5307 #endif
5309 /* EXP is some logical combination of boolean tests. See if we can
5310 merge it into some range test. Return the new tree if so. */
5312 static tree
5313 fold_range_test (location_t loc, enum tree_code code, tree type,
5314 tree op0, tree op1)
5316 int or_op = (code == TRUTH_ORIF_EXPR
5317 || code == TRUTH_OR_EXPR);
5318 int in0_p, in1_p, in_p;
5319 tree low0, low1, low, high0, high1, high;
5320 bool strict_overflow_p = false;
5321 tree tem, lhs, rhs;
5322 const char * const warnmsg = G_("assuming signed overflow does not occur "
5323 "when simplifying range test");
5325 if (!INTEGRAL_TYPE_P (type))
5326 return 0;
5328 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5329 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5331 /* If this is an OR operation, invert both sides; we will invert
5332 again at the end. */
5333 if (or_op)
5334 in0_p = ! in0_p, in1_p = ! in1_p;
5336 /* If both expressions are the same, if we can merge the ranges, and we
5337 can build the range test, return it or it inverted. If one of the
5338 ranges is always true or always false, consider it to be the same
5339 expression as the other. */
5340 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5341 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5342 in1_p, low1, high1)
5343 && 0 != (tem = (build_range_check (loc, type,
5344 lhs != 0 ? lhs
5345 : rhs != 0 ? rhs : integer_zero_node,
5346 in_p, low, high))))
5348 if (strict_overflow_p)
5349 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5350 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5353 /* On machines where the branch cost is expensive, if this is a
5354 short-circuited branch and the underlying object on both sides
5355 is the same, make a non-short-circuit operation. */
5356 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5357 && lhs != 0 && rhs != 0
5358 && (code == TRUTH_ANDIF_EXPR
5359 || code == TRUTH_ORIF_EXPR)
5360 && operand_equal_p (lhs, rhs, 0))
5362 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5363 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5364 which cases we can't do this. */
5365 if (simple_operand_p (lhs))
5366 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5367 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5368 type, op0, op1);
5370 else if (!lang_hooks.decls.global_bindings_p ()
5371 && !CONTAINS_PLACEHOLDER_P (lhs))
5373 tree common = save_expr (lhs);
5375 if (0 != (lhs = build_range_check (loc, type, common,
5376 or_op ? ! in0_p : in0_p,
5377 low0, high0))
5378 && (0 != (rhs = build_range_check (loc, type, common,
5379 or_op ? ! in1_p : in1_p,
5380 low1, high1))))
5382 if (strict_overflow_p)
5383 fold_overflow_warning (warnmsg,
5384 WARN_STRICT_OVERFLOW_COMPARISON);
5385 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5386 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5387 type, lhs, rhs);
5392 return 0;
5395 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5396 bit value. Arrange things so the extra bits will be set to zero if and
5397 only if C is signed-extended to its full width. If MASK is nonzero,
5398 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5400 static tree
5401 unextend (tree c, int p, int unsignedp, tree mask)
5403 tree type = TREE_TYPE (c);
5404 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5405 tree temp;
5407 if (p == modesize || unsignedp)
5408 return c;
5410 /* We work by getting just the sign bit into the low-order bit, then
5411 into the high-order bit, then sign-extend. We then XOR that value
5412 with C. */
5413 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5415 /* We must use a signed type in order to get an arithmetic right shift.
5416 However, we must also avoid introducing accidental overflows, so that
5417 a subsequent call to integer_zerop will work. Hence we must
5418 do the type conversion here. At this point, the constant is either
5419 zero or one, and the conversion to a signed type can never overflow.
5420 We could get an overflow if this conversion is done anywhere else. */
5421 if (TYPE_UNSIGNED (type))
5422 temp = fold_convert (signed_type_for (type), temp);
5424 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5425 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5426 if (mask != 0)
5427 temp = const_binop (BIT_AND_EXPR, temp,
5428 fold_convert (TREE_TYPE (c), mask));
5429 /* If necessary, convert the type back to match the type of C. */
5430 if (TYPE_UNSIGNED (type))
5431 temp = fold_convert (type, temp);
5433 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5436 /* For an expression that has the form
5437 (A && B) || ~B
5439 (A || B) && ~B,
5440 we can drop one of the inner expressions and simplify to
5441 A || ~B
5443 A && ~B
5444 LOC is the location of the resulting expression. OP is the inner
5445 logical operation; the left-hand side in the examples above, while CMPOP
5446 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5447 removing a condition that guards another, as in
5448 (A != NULL && A->...) || A == NULL
5449 which we must not transform. If RHS_ONLY is true, only eliminate the
5450 right-most operand of the inner logical operation. */
5452 static tree
5453 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5454 bool rhs_only)
5456 tree type = TREE_TYPE (cmpop);
5457 enum tree_code code = TREE_CODE (cmpop);
5458 enum tree_code truthop_code = TREE_CODE (op);
5459 tree lhs = TREE_OPERAND (op, 0);
5460 tree rhs = TREE_OPERAND (op, 1);
5461 tree orig_lhs = lhs, orig_rhs = rhs;
5462 enum tree_code rhs_code = TREE_CODE (rhs);
5463 enum tree_code lhs_code = TREE_CODE (lhs);
5464 enum tree_code inv_code;
5466 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5467 return NULL_TREE;
5469 if (TREE_CODE_CLASS (code) != tcc_comparison)
5470 return NULL_TREE;
5472 if (rhs_code == truthop_code)
5474 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5475 if (newrhs != NULL_TREE)
5477 rhs = newrhs;
5478 rhs_code = TREE_CODE (rhs);
5481 if (lhs_code == truthop_code && !rhs_only)
5483 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5484 if (newlhs != NULL_TREE)
5486 lhs = newlhs;
5487 lhs_code = TREE_CODE (lhs);
5491 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5492 if (inv_code == rhs_code
5493 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5494 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5495 return lhs;
5496 if (!rhs_only && inv_code == lhs_code
5497 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5498 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5499 return rhs;
5500 if (rhs != orig_rhs || lhs != orig_lhs)
5501 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5502 lhs, rhs);
5503 return NULL_TREE;
5506 /* Find ways of folding logical expressions of LHS and RHS:
5507 Try to merge two comparisons to the same innermost item.
5508 Look for range tests like "ch >= '0' && ch <= '9'".
5509 Look for combinations of simple terms on machines with expensive branches
5510 and evaluate the RHS unconditionally.
5512 For example, if we have p->a == 2 && p->b == 4 and we can make an
5513 object large enough to span both A and B, we can do this with a comparison
5514 against the object ANDed with the a mask.
5516 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5517 operations to do this with one comparison.
5519 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5520 function and the one above.
5522 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5523 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5525 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5526 two operands.
5528 We return the simplified tree or 0 if no optimization is possible. */
5530 static tree
5531 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5532 tree lhs, tree rhs)
5534 /* If this is the "or" of two comparisons, we can do something if
5535 the comparisons are NE_EXPR. If this is the "and", we can do something
5536 if the comparisons are EQ_EXPR. I.e.,
5537 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5539 WANTED_CODE is this operation code. For single bit fields, we can
5540 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5541 comparison for one-bit fields. */
5543 enum tree_code wanted_code;
5544 enum tree_code lcode, rcode;
5545 tree ll_arg, lr_arg, rl_arg, rr_arg;
5546 tree ll_inner, lr_inner, rl_inner, rr_inner;
5547 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5548 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5549 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5550 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5551 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5552 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5553 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5554 machine_mode lnmode, rnmode;
5555 tree ll_mask, lr_mask, rl_mask, rr_mask;
5556 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5557 tree l_const, r_const;
5558 tree lntype, rntype, result;
5559 HOST_WIDE_INT first_bit, end_bit;
5560 int volatilep;
5562 /* Start by getting the comparison codes. Fail if anything is volatile.
5563 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5564 it were surrounded with a NE_EXPR. */
5566 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5567 return 0;
5569 lcode = TREE_CODE (lhs);
5570 rcode = TREE_CODE (rhs);
5572 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5574 lhs = build2 (NE_EXPR, truth_type, lhs,
5575 build_int_cst (TREE_TYPE (lhs), 0));
5576 lcode = NE_EXPR;
5579 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5581 rhs = build2 (NE_EXPR, truth_type, rhs,
5582 build_int_cst (TREE_TYPE (rhs), 0));
5583 rcode = NE_EXPR;
5586 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5587 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5588 return 0;
5590 ll_arg = TREE_OPERAND (lhs, 0);
5591 lr_arg = TREE_OPERAND (lhs, 1);
5592 rl_arg = TREE_OPERAND (rhs, 0);
5593 rr_arg = TREE_OPERAND (rhs, 1);
5595 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5596 if (simple_operand_p (ll_arg)
5597 && simple_operand_p (lr_arg))
5599 if (operand_equal_p (ll_arg, rl_arg, 0)
5600 && operand_equal_p (lr_arg, rr_arg, 0))
5602 result = combine_comparisons (loc, code, lcode, rcode,
5603 truth_type, ll_arg, lr_arg);
5604 if (result)
5605 return result;
5607 else if (operand_equal_p (ll_arg, rr_arg, 0)
5608 && operand_equal_p (lr_arg, rl_arg, 0))
5610 result = combine_comparisons (loc, code, lcode,
5611 swap_tree_comparison (rcode),
5612 truth_type, ll_arg, lr_arg);
5613 if (result)
5614 return result;
5618 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5619 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5621 /* If the RHS can be evaluated unconditionally and its operands are
5622 simple, it wins to evaluate the RHS unconditionally on machines
5623 with expensive branches. In this case, this isn't a comparison
5624 that can be merged. */
5626 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5627 false) >= 2
5628 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5629 && simple_operand_p (rl_arg)
5630 && simple_operand_p (rr_arg))
5632 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5633 if (code == TRUTH_OR_EXPR
5634 && lcode == NE_EXPR && integer_zerop (lr_arg)
5635 && rcode == NE_EXPR && integer_zerop (rr_arg)
5636 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5637 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5638 return build2_loc (loc, NE_EXPR, truth_type,
5639 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5640 ll_arg, rl_arg),
5641 build_int_cst (TREE_TYPE (ll_arg), 0));
5643 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5644 if (code == TRUTH_AND_EXPR
5645 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5646 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5647 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5648 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5649 return build2_loc (loc, EQ_EXPR, truth_type,
5650 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5651 ll_arg, rl_arg),
5652 build_int_cst (TREE_TYPE (ll_arg), 0));
5655 /* See if the comparisons can be merged. Then get all the parameters for
5656 each side. */
5658 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5659 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5660 return 0;
5662 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5663 volatilep = 0;
5664 ll_inner = decode_field_reference (loc, &ll_arg,
5665 &ll_bitsize, &ll_bitpos, &ll_mode,
5666 &ll_unsignedp, &ll_reversep, &volatilep,
5667 &ll_mask, &ll_and_mask);
5668 lr_inner = decode_field_reference (loc, &lr_arg,
5669 &lr_bitsize, &lr_bitpos, &lr_mode,
5670 &lr_unsignedp, &lr_reversep, &volatilep,
5671 &lr_mask, &lr_and_mask);
5672 rl_inner = decode_field_reference (loc, &rl_arg,
5673 &rl_bitsize, &rl_bitpos, &rl_mode,
5674 &rl_unsignedp, &rl_reversep, &volatilep,
5675 &rl_mask, &rl_and_mask);
5676 rr_inner = decode_field_reference (loc, &rr_arg,
5677 &rr_bitsize, &rr_bitpos, &rr_mode,
5678 &rr_unsignedp, &rr_reversep, &volatilep,
5679 &rr_mask, &rr_and_mask);
5681 /* It must be true that the inner operation on the lhs of each
5682 comparison must be the same if we are to be able to do anything.
5683 Then see if we have constants. If not, the same must be true for
5684 the rhs's. */
5685 if (volatilep
5686 || ll_reversep != rl_reversep
5687 || ll_inner == 0 || rl_inner == 0
5688 || ! operand_equal_p (ll_inner, rl_inner, 0))
5689 return 0;
5691 if (TREE_CODE (lr_arg) == INTEGER_CST
5692 && TREE_CODE (rr_arg) == INTEGER_CST)
5694 l_const = lr_arg, r_const = rr_arg;
5695 lr_reversep = ll_reversep;
5697 else if (lr_reversep != rr_reversep
5698 || lr_inner == 0 || rr_inner == 0
5699 || ! operand_equal_p (lr_inner, rr_inner, 0))
5700 return 0;
5701 else
5702 l_const = r_const = 0;
5704 /* If either comparison code is not correct for our logical operation,
5705 fail. However, we can convert a one-bit comparison against zero into
5706 the opposite comparison against that bit being set in the field. */
5708 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5709 if (lcode != wanted_code)
5711 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5713 /* Make the left operand unsigned, since we are only interested
5714 in the value of one bit. Otherwise we are doing the wrong
5715 thing below. */
5716 ll_unsignedp = 1;
5717 l_const = ll_mask;
5719 else
5720 return 0;
5723 /* This is analogous to the code for l_const above. */
5724 if (rcode != wanted_code)
5726 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5728 rl_unsignedp = 1;
5729 r_const = rl_mask;
5731 else
5732 return 0;
5735 /* See if we can find a mode that contains both fields being compared on
5736 the left. If we can't, fail. Otherwise, update all constants and masks
5737 to be relative to a field of that size. */
5738 first_bit = MIN (ll_bitpos, rl_bitpos);
5739 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5740 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5741 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5742 volatilep);
5743 if (lnmode == VOIDmode)
5744 return 0;
5746 lnbitsize = GET_MODE_BITSIZE (lnmode);
5747 lnbitpos = first_bit & ~ (lnbitsize - 1);
5748 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5749 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5751 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5753 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5754 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5757 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5758 size_int (xll_bitpos));
5759 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5760 size_int (xrl_bitpos));
5762 if (l_const)
5764 l_const = fold_convert_loc (loc, lntype, l_const);
5765 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5766 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5767 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5768 fold_build1_loc (loc, BIT_NOT_EXPR,
5769 lntype, ll_mask))))
5771 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5773 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5776 if (r_const)
5778 r_const = fold_convert_loc (loc, lntype, r_const);
5779 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5780 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5781 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5782 fold_build1_loc (loc, BIT_NOT_EXPR,
5783 lntype, rl_mask))))
5785 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5787 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5791 /* If the right sides are not constant, do the same for it. Also,
5792 disallow this optimization if a size or signedness mismatch occurs
5793 between the left and right sides. */
5794 if (l_const == 0)
5796 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5797 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5798 /* Make sure the two fields on the right
5799 correspond to the left without being swapped. */
5800 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5801 return 0;
5803 first_bit = MIN (lr_bitpos, rr_bitpos);
5804 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5805 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5806 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5807 volatilep);
5808 if (rnmode == VOIDmode)
5809 return 0;
5811 rnbitsize = GET_MODE_BITSIZE (rnmode);
5812 rnbitpos = first_bit & ~ (rnbitsize - 1);
5813 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5814 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5816 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5818 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5819 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5822 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5823 rntype, lr_mask),
5824 size_int (xlr_bitpos));
5825 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5826 rntype, rr_mask),
5827 size_int (xrr_bitpos));
5829 /* Make a mask that corresponds to both fields being compared.
5830 Do this for both items being compared. If the operands are the
5831 same size and the bits being compared are in the same position
5832 then we can do this by masking both and comparing the masked
5833 results. */
5834 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5835 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5836 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5838 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5839 lntype, lnbitsize, lnbitpos,
5840 ll_unsignedp || rl_unsignedp, ll_reversep);
5841 if (! all_ones_mask_p (ll_mask, lnbitsize))
5842 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5844 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5845 rntype, rnbitsize, rnbitpos,
5846 lr_unsignedp || rr_unsignedp, lr_reversep);
5847 if (! all_ones_mask_p (lr_mask, rnbitsize))
5848 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5850 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5853 /* There is still another way we can do something: If both pairs of
5854 fields being compared are adjacent, we may be able to make a wider
5855 field containing them both.
5857 Note that we still must mask the lhs/rhs expressions. Furthermore,
5858 the mask must be shifted to account for the shift done by
5859 make_bit_field_ref. */
5860 if ((ll_bitsize + ll_bitpos == rl_bitpos
5861 && lr_bitsize + lr_bitpos == rr_bitpos)
5862 || (ll_bitpos == rl_bitpos + rl_bitsize
5863 && lr_bitpos == rr_bitpos + rr_bitsize))
5865 tree type;
5867 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5868 ll_bitsize + rl_bitsize,
5869 MIN (ll_bitpos, rl_bitpos),
5870 ll_unsignedp, ll_reversep);
5871 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5872 lr_bitsize + rr_bitsize,
5873 MIN (lr_bitpos, rr_bitpos),
5874 lr_unsignedp, lr_reversep);
5876 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5877 size_int (MIN (xll_bitpos, xrl_bitpos)));
5878 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5879 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5881 /* Convert to the smaller type before masking out unwanted bits. */
5882 type = lntype;
5883 if (lntype != rntype)
5885 if (lnbitsize > rnbitsize)
5887 lhs = fold_convert_loc (loc, rntype, lhs);
5888 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5889 type = rntype;
5891 else if (lnbitsize < rnbitsize)
5893 rhs = fold_convert_loc (loc, lntype, rhs);
5894 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5895 type = lntype;
5899 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5900 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5902 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5903 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5905 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5908 return 0;
5911 /* Handle the case of comparisons with constants. If there is something in
5912 common between the masks, those bits of the constants must be the same.
5913 If not, the condition is always false. Test for this to avoid generating
5914 incorrect code below. */
5915 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5916 if (! integer_zerop (result)
5917 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5918 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5920 if (wanted_code == NE_EXPR)
5922 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5923 return constant_boolean_node (true, truth_type);
5925 else
5927 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5928 return constant_boolean_node (false, truth_type);
5932 /* Construct the expression we will return. First get the component
5933 reference we will make. Unless the mask is all ones the width of
5934 that field, perform the mask operation. Then compare with the
5935 merged constant. */
5936 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5937 lntype, lnbitsize, lnbitpos,
5938 ll_unsignedp || rl_unsignedp, ll_reversep);
5940 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5941 if (! all_ones_mask_p (ll_mask, lnbitsize))
5942 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5944 return build2_loc (loc, wanted_code, truth_type, result,
5945 const_binop (BIT_IOR_EXPR, l_const, r_const));
5948 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5949 constant. */
5951 static tree
5952 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5953 tree op0, tree op1)
5955 tree arg0 = op0;
5956 enum tree_code op_code;
5957 tree comp_const;
5958 tree minmax_const;
5959 int consts_equal, consts_lt;
5960 tree inner;
5962 STRIP_SIGN_NOPS (arg0);
5964 op_code = TREE_CODE (arg0);
5965 minmax_const = TREE_OPERAND (arg0, 1);
5966 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5967 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5968 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5969 inner = TREE_OPERAND (arg0, 0);
5971 /* If something does not permit us to optimize, return the original tree. */
5972 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5973 || TREE_CODE (comp_const) != INTEGER_CST
5974 || TREE_OVERFLOW (comp_const)
5975 || TREE_CODE (minmax_const) != INTEGER_CST
5976 || TREE_OVERFLOW (minmax_const))
5977 return NULL_TREE;
5979 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5980 and GT_EXPR, doing the rest with recursive calls using logical
5981 simplifications. */
5982 switch (code)
5984 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5986 tree tem
5987 = optimize_minmax_comparison (loc,
5988 invert_tree_comparison (code, false),
5989 type, op0, op1);
5990 if (tem)
5991 return invert_truthvalue_loc (loc, tem);
5992 return NULL_TREE;
5995 case GE_EXPR:
5996 return
5997 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5998 optimize_minmax_comparison
5999 (loc, EQ_EXPR, type, arg0, comp_const),
6000 optimize_minmax_comparison
6001 (loc, GT_EXPR, type, arg0, comp_const));
6003 case EQ_EXPR:
6004 if (op_code == MAX_EXPR && consts_equal)
6005 /* MAX (X, 0) == 0 -> X <= 0 */
6006 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6008 else if (op_code == MAX_EXPR && consts_lt)
6009 /* MAX (X, 0) == 5 -> X == 5 */
6010 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6012 else if (op_code == MAX_EXPR)
6013 /* MAX (X, 0) == -1 -> false */
6014 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6016 else if (consts_equal)
6017 /* MIN (X, 0) == 0 -> X >= 0 */
6018 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6020 else if (consts_lt)
6021 /* MIN (X, 0) == 5 -> false */
6022 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6024 else
6025 /* MIN (X, 0) == -1 -> X == -1 */
6026 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6028 case GT_EXPR:
6029 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6030 /* MAX (X, 0) > 0 -> X > 0
6031 MAX (X, 0) > 5 -> X > 5 */
6032 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6034 else if (op_code == MAX_EXPR)
6035 /* MAX (X, 0) > -1 -> true */
6036 return omit_one_operand_loc (loc, type, integer_one_node, inner);
6038 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6039 /* MIN (X, 0) > 0 -> false
6040 MIN (X, 0) > 5 -> false */
6041 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6043 else
6044 /* MIN (X, 0) > -1 -> X > -1 */
6045 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6047 default:
6048 return NULL_TREE;
6052 /* T is an integer expression that is being multiplied, divided, or taken a
6053 modulus (CODE says which and what kind of divide or modulus) by a
6054 constant C. See if we can eliminate that operation by folding it with
6055 other operations already in T. WIDE_TYPE, if non-null, is a type that
6056 should be used for the computation if wider than our type.
6058 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6059 (X * 2) + (Y * 4). We must, however, be assured that either the original
6060 expression would not overflow or that overflow is undefined for the type
6061 in the language in question.
6063 If we return a non-null expression, it is an equivalent form of the
6064 original computation, but need not be in the original type.
6066 We set *STRICT_OVERFLOW_P to true if the return values depends on
6067 signed overflow being undefined. Otherwise we do not change
6068 *STRICT_OVERFLOW_P. */
6070 static tree
6071 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6072 bool *strict_overflow_p)
6074 /* To avoid exponential search depth, refuse to allow recursion past
6075 three levels. Beyond that (1) it's highly unlikely that we'll find
6076 something interesting and (2) we've probably processed it before
6077 when we built the inner expression. */
6079 static int depth;
6080 tree ret;
6082 if (depth > 3)
6083 return NULL;
6085 depth++;
6086 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6087 depth--;
6089 return ret;
6092 static tree
6093 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6094 bool *strict_overflow_p)
6096 tree type = TREE_TYPE (t);
6097 enum tree_code tcode = TREE_CODE (t);
6098 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6099 > GET_MODE_SIZE (TYPE_MODE (type)))
6100 ? wide_type : type);
6101 tree t1, t2;
6102 int same_p = tcode == code;
6103 tree op0 = NULL_TREE, op1 = NULL_TREE;
6104 bool sub_strict_overflow_p;
6106 /* Don't deal with constants of zero here; they confuse the code below. */
6107 if (integer_zerop (c))
6108 return NULL_TREE;
6110 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6111 op0 = TREE_OPERAND (t, 0);
6113 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6114 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6116 /* Note that we need not handle conditional operations here since fold
6117 already handles those cases. So just do arithmetic here. */
6118 switch (tcode)
6120 case INTEGER_CST:
6121 /* For a constant, we can always simplify if we are a multiply
6122 or (for divide and modulus) if it is a multiple of our constant. */
6123 if (code == MULT_EXPR
6124 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6126 tree tem = const_binop (code, fold_convert (ctype, t),
6127 fold_convert (ctype, c));
6128 /* If the multiplication overflowed, we lost information on it.
6129 See PR68142 and PR69845. */
6130 if (TREE_OVERFLOW (tem))
6131 return NULL_TREE;
6132 return tem;
6134 break;
6136 CASE_CONVERT: case NON_LVALUE_EXPR:
6137 /* If op0 is an expression ... */
6138 if ((COMPARISON_CLASS_P (op0)
6139 || UNARY_CLASS_P (op0)
6140 || BINARY_CLASS_P (op0)
6141 || VL_EXP_CLASS_P (op0)
6142 || EXPRESSION_CLASS_P (op0))
6143 /* ... and has wrapping overflow, and its type is smaller
6144 than ctype, then we cannot pass through as widening. */
6145 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6146 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6147 && (TYPE_PRECISION (ctype)
6148 > TYPE_PRECISION (TREE_TYPE (op0))))
6149 /* ... or this is a truncation (t is narrower than op0),
6150 then we cannot pass through this narrowing. */
6151 || (TYPE_PRECISION (type)
6152 < TYPE_PRECISION (TREE_TYPE (op0)))
6153 /* ... or signedness changes for division or modulus,
6154 then we cannot pass through this conversion. */
6155 || (code != MULT_EXPR
6156 && (TYPE_UNSIGNED (ctype)
6157 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6158 /* ... or has undefined overflow while the converted to
6159 type has not, we cannot do the operation in the inner type
6160 as that would introduce undefined overflow. */
6161 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6162 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6163 && !TYPE_OVERFLOW_UNDEFINED (type))))
6164 break;
6166 /* Pass the constant down and see if we can make a simplification. If
6167 we can, replace this expression with the inner simplification for
6168 possible later conversion to our or some other type. */
6169 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6170 && TREE_CODE (t2) == INTEGER_CST
6171 && !TREE_OVERFLOW (t2)
6172 && (0 != (t1 = extract_muldiv (op0, t2, code,
6173 code == MULT_EXPR
6174 ? ctype : NULL_TREE,
6175 strict_overflow_p))))
6176 return t1;
6177 break;
6179 case ABS_EXPR:
6180 /* If widening the type changes it from signed to unsigned, then we
6181 must avoid building ABS_EXPR itself as unsigned. */
6182 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6184 tree cstype = (*signed_type_for) (ctype);
6185 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6186 != 0)
6188 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6189 return fold_convert (ctype, t1);
6191 break;
6193 /* If the constant is negative, we cannot simplify this. */
6194 if (tree_int_cst_sgn (c) == -1)
6195 break;
6196 /* FALLTHROUGH */
6197 case NEGATE_EXPR:
6198 /* For division and modulus, type can't be unsigned, as e.g.
6199 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6200 For signed types, even with wrapping overflow, this is fine. */
6201 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6202 break;
6203 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6204 != 0)
6205 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6206 break;
6208 case MIN_EXPR: case MAX_EXPR:
6209 /* If widening the type changes the signedness, then we can't perform
6210 this optimization as that changes the result. */
6211 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6212 break;
6214 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6215 sub_strict_overflow_p = false;
6216 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6217 &sub_strict_overflow_p)) != 0
6218 && (t2 = extract_muldiv (op1, c, code, wide_type,
6219 &sub_strict_overflow_p)) != 0)
6221 if (tree_int_cst_sgn (c) < 0)
6222 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6223 if (sub_strict_overflow_p)
6224 *strict_overflow_p = true;
6225 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6226 fold_convert (ctype, t2));
6228 break;
6230 case LSHIFT_EXPR: case RSHIFT_EXPR:
6231 /* If the second operand is constant, this is a multiplication
6232 or floor division, by a power of two, so we can treat it that
6233 way unless the multiplier or divisor overflows. Signed
6234 left-shift overflow is implementation-defined rather than
6235 undefined in C90, so do not convert signed left shift into
6236 multiplication. */
6237 if (TREE_CODE (op1) == INTEGER_CST
6238 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6239 /* const_binop may not detect overflow correctly,
6240 so check for it explicitly here. */
6241 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6242 && 0 != (t1 = fold_convert (ctype,
6243 const_binop (LSHIFT_EXPR,
6244 size_one_node,
6245 op1)))
6246 && !TREE_OVERFLOW (t1))
6247 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6248 ? MULT_EXPR : FLOOR_DIV_EXPR,
6249 ctype,
6250 fold_convert (ctype, op0),
6251 t1),
6252 c, code, wide_type, strict_overflow_p);
6253 break;
6255 case PLUS_EXPR: case MINUS_EXPR:
6256 /* See if we can eliminate the operation on both sides. If we can, we
6257 can return a new PLUS or MINUS. If we can't, the only remaining
6258 cases where we can do anything are if the second operand is a
6259 constant. */
6260 sub_strict_overflow_p = false;
6261 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6262 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6263 if (t1 != 0 && t2 != 0
6264 && (code == MULT_EXPR
6265 /* If not multiplication, we can only do this if both operands
6266 are divisible by c. */
6267 || (multiple_of_p (ctype, op0, c)
6268 && multiple_of_p (ctype, op1, c))))
6270 if (sub_strict_overflow_p)
6271 *strict_overflow_p = true;
6272 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6273 fold_convert (ctype, t2));
6276 /* If this was a subtraction, negate OP1 and set it to be an addition.
6277 This simplifies the logic below. */
6278 if (tcode == MINUS_EXPR)
6280 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6281 /* If OP1 was not easily negatable, the constant may be OP0. */
6282 if (TREE_CODE (op0) == INTEGER_CST)
6284 std::swap (op0, op1);
6285 std::swap (t1, t2);
6289 if (TREE_CODE (op1) != INTEGER_CST)
6290 break;
6292 /* If either OP1 or C are negative, this optimization is not safe for
6293 some of the division and remainder types while for others we need
6294 to change the code. */
6295 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6297 if (code == CEIL_DIV_EXPR)
6298 code = FLOOR_DIV_EXPR;
6299 else if (code == FLOOR_DIV_EXPR)
6300 code = CEIL_DIV_EXPR;
6301 else if (code != MULT_EXPR
6302 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6303 break;
6306 /* If it's a multiply or a division/modulus operation of a multiple
6307 of our constant, do the operation and verify it doesn't overflow. */
6308 if (code == MULT_EXPR
6309 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6311 op1 = const_binop (code, fold_convert (ctype, op1),
6312 fold_convert (ctype, c));
6313 /* We allow the constant to overflow with wrapping semantics. */
6314 if (op1 == 0
6315 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6316 break;
6318 else
6319 break;
6321 /* If we have an unsigned type, we cannot widen the operation since it
6322 will change the result if the original computation overflowed. */
6323 if (TYPE_UNSIGNED (ctype) && ctype != type)
6324 break;
6326 /* If we were able to eliminate our operation from the first side,
6327 apply our operation to the second side and reform the PLUS. */
6328 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6329 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6331 /* The last case is if we are a multiply. In that case, we can
6332 apply the distributive law to commute the multiply and addition
6333 if the multiplication of the constants doesn't overflow
6334 and overflow is defined. With undefined overflow
6335 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6336 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6337 return fold_build2 (tcode, ctype,
6338 fold_build2 (code, ctype,
6339 fold_convert (ctype, op0),
6340 fold_convert (ctype, c)),
6341 op1);
6343 break;
6345 case MULT_EXPR:
6346 /* We have a special case here if we are doing something like
6347 (C * 8) % 4 since we know that's zero. */
6348 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6349 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6350 /* If the multiplication can overflow we cannot optimize this. */
6351 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6352 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6353 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6355 *strict_overflow_p = true;
6356 return omit_one_operand (type, integer_zero_node, op0);
6359 /* ... fall through ... */
6361 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6362 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6363 /* If we can extract our operation from the LHS, do so and return a
6364 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6365 do something only if the second operand is a constant. */
6366 if (same_p
6367 && (t1 = extract_muldiv (op0, c, code, wide_type,
6368 strict_overflow_p)) != 0)
6369 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6370 fold_convert (ctype, op1));
6371 else if (tcode == MULT_EXPR && code == MULT_EXPR
6372 && (t1 = extract_muldiv (op1, c, code, wide_type,
6373 strict_overflow_p)) != 0)
6374 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6375 fold_convert (ctype, t1));
6376 else if (TREE_CODE (op1) != INTEGER_CST)
6377 return 0;
6379 /* If these are the same operation types, we can associate them
6380 assuming no overflow. */
6381 if (tcode == code)
6383 bool overflow_p = false;
6384 bool overflow_mul_p;
6385 signop sign = TYPE_SIGN (ctype);
6386 unsigned prec = TYPE_PRECISION (ctype);
6387 wide_int mul = wi::mul (wide_int::from (op1, prec,
6388 TYPE_SIGN (TREE_TYPE (op1))),
6389 wide_int::from (c, prec,
6390 TYPE_SIGN (TREE_TYPE (c))),
6391 sign, &overflow_mul_p);
6392 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6393 if (overflow_mul_p
6394 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6395 overflow_p = true;
6396 if (!overflow_p)
6397 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6398 wide_int_to_tree (ctype, mul));
6401 /* If these operations "cancel" each other, we have the main
6402 optimizations of this pass, which occur when either constant is a
6403 multiple of the other, in which case we replace this with either an
6404 operation or CODE or TCODE.
6406 If we have an unsigned type, we cannot do this since it will change
6407 the result if the original computation overflowed. */
6408 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6409 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6410 || (tcode == MULT_EXPR
6411 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6412 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6413 && code != MULT_EXPR)))
6415 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6417 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6418 *strict_overflow_p = true;
6419 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6420 fold_convert (ctype,
6421 const_binop (TRUNC_DIV_EXPR,
6422 op1, c)));
6424 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6426 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6427 *strict_overflow_p = true;
6428 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6429 fold_convert (ctype,
6430 const_binop (TRUNC_DIV_EXPR,
6431 c, op1)));
6434 break;
6436 default:
6437 break;
6440 return 0;
6443 /* Return a node which has the indicated constant VALUE (either 0 or
6444 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6445 and is of the indicated TYPE. */
6447 tree
6448 constant_boolean_node (bool value, tree type)
6450 if (type == integer_type_node)
6451 return value ? integer_one_node : integer_zero_node;
6452 else if (type == boolean_type_node)
6453 return value ? boolean_true_node : boolean_false_node;
6454 else if (TREE_CODE (type) == VECTOR_TYPE)
6455 return build_vector_from_val (type,
6456 build_int_cst (TREE_TYPE (type),
6457 value ? -1 : 0));
6458 else
6459 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6463 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6464 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6465 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6466 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6467 COND is the first argument to CODE; otherwise (as in the example
6468 given here), it is the second argument. TYPE is the type of the
6469 original expression. Return NULL_TREE if no simplification is
6470 possible. */
6472 static tree
6473 fold_binary_op_with_conditional_arg (location_t loc,
6474 enum tree_code code,
6475 tree type, tree op0, tree op1,
6476 tree cond, tree arg, int cond_first_p)
6478 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6479 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6480 tree test, true_value, false_value;
6481 tree lhs = NULL_TREE;
6482 tree rhs = NULL_TREE;
6483 enum tree_code cond_code = COND_EXPR;
6485 if (TREE_CODE (cond) == COND_EXPR
6486 || TREE_CODE (cond) == VEC_COND_EXPR)
6488 test = TREE_OPERAND (cond, 0);
6489 true_value = TREE_OPERAND (cond, 1);
6490 false_value = TREE_OPERAND (cond, 2);
6491 /* If this operand throws an expression, then it does not make
6492 sense to try to perform a logical or arithmetic operation
6493 involving it. */
6494 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6495 lhs = true_value;
6496 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6497 rhs = false_value;
6499 else if (!(TREE_CODE (type) != VECTOR_TYPE
6500 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6502 tree testtype = TREE_TYPE (cond);
6503 test = cond;
6504 true_value = constant_boolean_node (true, testtype);
6505 false_value = constant_boolean_node (false, testtype);
6507 else
6508 /* Detect the case of mixing vector and scalar types - bail out. */
6509 return NULL_TREE;
6511 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6512 cond_code = VEC_COND_EXPR;
6514 /* This transformation is only worthwhile if we don't have to wrap ARG
6515 in a SAVE_EXPR and the operation can be simplified without recursing
6516 on at least one of the branches once its pushed inside the COND_EXPR. */
6517 if (!TREE_CONSTANT (arg)
6518 && (TREE_SIDE_EFFECTS (arg)
6519 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6520 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6521 return NULL_TREE;
6523 arg = fold_convert_loc (loc, arg_type, arg);
6524 if (lhs == 0)
6526 true_value = fold_convert_loc (loc, cond_type, true_value);
6527 if (cond_first_p)
6528 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6529 else
6530 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6532 if (rhs == 0)
6534 false_value = fold_convert_loc (loc, cond_type, false_value);
6535 if (cond_first_p)
6536 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6537 else
6538 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6541 /* Check that we have simplified at least one of the branches. */
6542 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6543 return NULL_TREE;
6545 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6549 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6551 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6552 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6553 ADDEND is the same as X.
6555 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6556 and finite. The problematic cases are when X is zero, and its mode
6557 has signed zeros. In the case of rounding towards -infinity,
6558 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6559 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6561 bool
6562 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6564 if (!real_zerop (addend))
6565 return false;
6567 /* Don't allow the fold with -fsignaling-nans. */
6568 if (HONOR_SNANS (element_mode (type)))
6569 return false;
6571 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6572 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6573 return true;
6575 /* In a vector or complex, we would need to check the sign of all zeros. */
6576 if (TREE_CODE (addend) != REAL_CST)
6577 return false;
6579 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6580 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6581 negate = !negate;
6583 /* The mode has signed zeros, and we have to honor their sign.
6584 In this situation, there is only one case we can return true for.
6585 X - 0 is the same as X unless rounding towards -infinity is
6586 supported. */
6587 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6590 /* Subroutine of fold() that optimizes comparisons of a division by
6591 a nonzero integer constant against an integer constant, i.e.
6592 X/C1 op C2.
6594 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6595 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6596 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6598 The function returns the constant folded tree if a simplification
6599 can be made, and NULL_TREE otherwise. */
6601 static tree
6602 fold_div_compare (location_t loc,
6603 enum tree_code code, tree type, tree arg0, tree arg1)
6605 tree prod, tmp, hi, lo;
6606 tree arg00 = TREE_OPERAND (arg0, 0);
6607 tree arg01 = TREE_OPERAND (arg0, 1);
6608 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6609 bool neg_overflow = false;
6610 bool overflow;
6612 /* We have to do this the hard way to detect unsigned overflow.
6613 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6614 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6615 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6616 neg_overflow = false;
6618 if (sign == UNSIGNED)
6620 tmp = int_const_binop (MINUS_EXPR, arg01,
6621 build_int_cst (TREE_TYPE (arg01), 1));
6622 lo = prod;
6624 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6625 val = wi::add (prod, tmp, sign, &overflow);
6626 hi = force_fit_type (TREE_TYPE (arg00), val,
6627 -1, overflow | TREE_OVERFLOW (prod));
6629 else if (tree_int_cst_sgn (arg01) >= 0)
6631 tmp = int_const_binop (MINUS_EXPR, arg01,
6632 build_int_cst (TREE_TYPE (arg01), 1));
6633 switch (tree_int_cst_sgn (arg1))
6635 case -1:
6636 neg_overflow = true;
6637 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6638 hi = prod;
6639 break;
6641 case 0:
6642 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6643 hi = tmp;
6644 break;
6646 case 1:
6647 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6648 lo = prod;
6649 break;
6651 default:
6652 gcc_unreachable ();
6655 else
6657 /* A negative divisor reverses the relational operators. */
6658 code = swap_tree_comparison (code);
6660 tmp = int_const_binop (PLUS_EXPR, arg01,
6661 build_int_cst (TREE_TYPE (arg01), 1));
6662 switch (tree_int_cst_sgn (arg1))
6664 case -1:
6665 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6666 lo = prod;
6667 break;
6669 case 0:
6670 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6671 lo = tmp;
6672 break;
6674 case 1:
6675 neg_overflow = true;
6676 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6677 hi = prod;
6678 break;
6680 default:
6681 gcc_unreachable ();
6685 switch (code)
6687 case EQ_EXPR:
6688 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6689 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6690 if (TREE_OVERFLOW (hi))
6691 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6692 if (TREE_OVERFLOW (lo))
6693 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6694 return build_range_check (loc, type, arg00, 1, lo, hi);
6696 case NE_EXPR:
6697 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6698 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6699 if (TREE_OVERFLOW (hi))
6700 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6701 if (TREE_OVERFLOW (lo))
6702 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6703 return build_range_check (loc, type, arg00, 0, lo, hi);
6705 case LT_EXPR:
6706 if (TREE_OVERFLOW (lo))
6708 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6709 return omit_one_operand_loc (loc, type, tmp, arg00);
6711 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6713 case LE_EXPR:
6714 if (TREE_OVERFLOW (hi))
6716 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6717 return omit_one_operand_loc (loc, type, tmp, arg00);
6719 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6721 case GT_EXPR:
6722 if (TREE_OVERFLOW (hi))
6724 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6725 return omit_one_operand_loc (loc, type, tmp, arg00);
6727 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6729 case GE_EXPR:
6730 if (TREE_OVERFLOW (lo))
6732 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6733 return omit_one_operand_loc (loc, type, tmp, arg00);
6735 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6737 default:
6738 break;
6741 return NULL_TREE;
6745 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6746 equality/inequality test, then return a simplified form of the test
6747 using a sign testing. Otherwise return NULL. TYPE is the desired
6748 result type. */
6750 static tree
6751 fold_single_bit_test_into_sign_test (location_t loc,
6752 enum tree_code code, tree arg0, tree arg1,
6753 tree result_type)
6755 /* If this is testing a single bit, we can optimize the test. */
6756 if ((code == NE_EXPR || code == EQ_EXPR)
6757 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6758 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6760 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6761 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6762 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6764 if (arg00 != NULL_TREE
6765 /* This is only a win if casting to a signed type is cheap,
6766 i.e. when arg00's type is not a partial mode. */
6767 && TYPE_PRECISION (TREE_TYPE (arg00))
6768 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6770 tree stype = signed_type_for (TREE_TYPE (arg00));
6771 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6772 result_type,
6773 fold_convert_loc (loc, stype, arg00),
6774 build_int_cst (stype, 0));
6778 return NULL_TREE;
6781 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6782 equality/inequality test, then return a simplified form of
6783 the test using shifts and logical operations. Otherwise return
6784 NULL. TYPE is the desired result type. */
6786 tree
6787 fold_single_bit_test (location_t loc, enum tree_code code,
6788 tree arg0, tree arg1, tree result_type)
6790 /* If this is testing a single bit, we can optimize the test. */
6791 if ((code == NE_EXPR || code == EQ_EXPR)
6792 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6793 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6795 tree inner = TREE_OPERAND (arg0, 0);
6796 tree type = TREE_TYPE (arg0);
6797 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6798 machine_mode operand_mode = TYPE_MODE (type);
6799 int ops_unsigned;
6800 tree signed_type, unsigned_type, intermediate_type;
6801 tree tem, one;
6803 /* First, see if we can fold the single bit test into a sign-bit
6804 test. */
6805 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6806 result_type);
6807 if (tem)
6808 return tem;
6810 /* Otherwise we have (A & C) != 0 where C is a single bit,
6811 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6812 Similarly for (A & C) == 0. */
6814 /* If INNER is a right shift of a constant and it plus BITNUM does
6815 not overflow, adjust BITNUM and INNER. */
6816 if (TREE_CODE (inner) == RSHIFT_EXPR
6817 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6818 && bitnum < TYPE_PRECISION (type)
6819 && wi::ltu_p (TREE_OPERAND (inner, 1),
6820 TYPE_PRECISION (type) - bitnum))
6822 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6823 inner = TREE_OPERAND (inner, 0);
6826 /* If we are going to be able to omit the AND below, we must do our
6827 operations as unsigned. If we must use the AND, we have a choice.
6828 Normally unsigned is faster, but for some machines signed is. */
6829 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6830 && !flag_syntax_only) ? 0 : 1;
6832 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6833 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6834 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6835 inner = fold_convert_loc (loc, intermediate_type, inner);
6837 if (bitnum != 0)
6838 inner = build2 (RSHIFT_EXPR, intermediate_type,
6839 inner, size_int (bitnum));
6841 one = build_int_cst (intermediate_type, 1);
6843 if (code == EQ_EXPR)
6844 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6846 /* Put the AND last so it can combine with more things. */
6847 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6849 /* Make sure to return the proper type. */
6850 inner = fold_convert_loc (loc, result_type, inner);
6852 return inner;
6854 return NULL_TREE;
6857 /* Check whether we are allowed to reorder operands arg0 and arg1,
6858 such that the evaluation of arg1 occurs before arg0. */
6860 static bool
6861 reorder_operands_p (const_tree arg0, const_tree arg1)
6863 if (! flag_evaluation_order)
6864 return true;
6865 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6866 return true;
6867 return ! TREE_SIDE_EFFECTS (arg0)
6868 && ! TREE_SIDE_EFFECTS (arg1);
6871 /* Test whether it is preferable two swap two operands, ARG0 and
6872 ARG1, for example because ARG0 is an integer constant and ARG1
6873 isn't. If REORDER is true, only recommend swapping if we can
6874 evaluate the operands in reverse order. */
6876 bool
6877 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6879 if (CONSTANT_CLASS_P (arg1))
6880 return 0;
6881 if (CONSTANT_CLASS_P (arg0))
6882 return 1;
6884 STRIP_NOPS (arg0);
6885 STRIP_NOPS (arg1);
6887 if (TREE_CONSTANT (arg1))
6888 return 0;
6889 if (TREE_CONSTANT (arg0))
6890 return 1;
6892 if (reorder && flag_evaluation_order
6893 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6894 return 0;
6896 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6897 for commutative and comparison operators. Ensuring a canonical
6898 form allows the optimizers to find additional redundancies without
6899 having to explicitly check for both orderings. */
6900 if (TREE_CODE (arg0) == SSA_NAME
6901 && TREE_CODE (arg1) == SSA_NAME
6902 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6903 return 1;
6905 /* Put SSA_NAMEs last. */
6906 if (TREE_CODE (arg1) == SSA_NAME)
6907 return 0;
6908 if (TREE_CODE (arg0) == SSA_NAME)
6909 return 1;
6911 /* Put variables last. */
6912 if (DECL_P (arg1))
6913 return 0;
6914 if (DECL_P (arg0))
6915 return 1;
6917 return 0;
6921 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6922 means A >= Y && A != MAX, but in this case we know that
6923 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6925 static tree
6926 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6928 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6930 if (TREE_CODE (bound) == LT_EXPR)
6931 a = TREE_OPERAND (bound, 0);
6932 else if (TREE_CODE (bound) == GT_EXPR)
6933 a = TREE_OPERAND (bound, 1);
6934 else
6935 return NULL_TREE;
6937 typea = TREE_TYPE (a);
6938 if (!INTEGRAL_TYPE_P (typea)
6939 && !POINTER_TYPE_P (typea))
6940 return NULL_TREE;
6942 if (TREE_CODE (ineq) == LT_EXPR)
6944 a1 = TREE_OPERAND (ineq, 1);
6945 y = TREE_OPERAND (ineq, 0);
6947 else if (TREE_CODE (ineq) == GT_EXPR)
6949 a1 = TREE_OPERAND (ineq, 0);
6950 y = TREE_OPERAND (ineq, 1);
6952 else
6953 return NULL_TREE;
6955 if (TREE_TYPE (a1) != typea)
6956 return NULL_TREE;
6958 if (POINTER_TYPE_P (typea))
6960 /* Convert the pointer types into integer before taking the difference. */
6961 tree ta = fold_convert_loc (loc, ssizetype, a);
6962 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6963 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6965 else
6966 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6968 if (!diff || !integer_onep (diff))
6969 return NULL_TREE;
6971 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6974 /* Fold a sum or difference of at least one multiplication.
6975 Returns the folded tree or NULL if no simplification could be made. */
6977 static tree
6978 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6979 tree arg0, tree arg1)
6981 tree arg00, arg01, arg10, arg11;
6982 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6984 /* (A * C) +- (B * C) -> (A+-B) * C.
6985 (A * C) +- A -> A * (C+-1).
6986 We are most concerned about the case where C is a constant,
6987 but other combinations show up during loop reduction. Since
6988 it is not difficult, try all four possibilities. */
6990 if (TREE_CODE (arg0) == MULT_EXPR)
6992 arg00 = TREE_OPERAND (arg0, 0);
6993 arg01 = TREE_OPERAND (arg0, 1);
6995 else if (TREE_CODE (arg0) == INTEGER_CST)
6997 arg00 = build_one_cst (type);
6998 arg01 = arg0;
7000 else
7002 /* We cannot generate constant 1 for fract. */
7003 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7004 return NULL_TREE;
7005 arg00 = arg0;
7006 arg01 = build_one_cst (type);
7008 if (TREE_CODE (arg1) == MULT_EXPR)
7010 arg10 = TREE_OPERAND (arg1, 0);
7011 arg11 = TREE_OPERAND (arg1, 1);
7013 else if (TREE_CODE (arg1) == INTEGER_CST)
7015 arg10 = build_one_cst (type);
7016 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7017 the purpose of this canonicalization. */
7018 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
7019 && negate_expr_p (arg1)
7020 && code == PLUS_EXPR)
7022 arg11 = negate_expr (arg1);
7023 code = MINUS_EXPR;
7025 else
7026 arg11 = arg1;
7028 else
7030 /* We cannot generate constant 1 for fract. */
7031 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7032 return NULL_TREE;
7033 arg10 = arg1;
7034 arg11 = build_one_cst (type);
7036 same = NULL_TREE;
7038 if (operand_equal_p (arg01, arg11, 0))
7039 same = arg01, alt0 = arg00, alt1 = arg10;
7040 else if (operand_equal_p (arg00, arg10, 0))
7041 same = arg00, alt0 = arg01, alt1 = arg11;
7042 else if (operand_equal_p (arg00, arg11, 0))
7043 same = arg00, alt0 = arg01, alt1 = arg10;
7044 else if (operand_equal_p (arg01, arg10, 0))
7045 same = arg01, alt0 = arg00, alt1 = arg11;
7047 /* No identical multiplicands; see if we can find a common
7048 power-of-two factor in non-power-of-two multiplies. This
7049 can help in multi-dimensional array access. */
7050 else if (tree_fits_shwi_p (arg01)
7051 && tree_fits_shwi_p (arg11))
7053 HOST_WIDE_INT int01, int11, tmp;
7054 bool swap = false;
7055 tree maybe_same;
7056 int01 = tree_to_shwi (arg01);
7057 int11 = tree_to_shwi (arg11);
7059 /* Move min of absolute values to int11. */
7060 if (absu_hwi (int01) < absu_hwi (int11))
7062 tmp = int01, int01 = int11, int11 = tmp;
7063 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7064 maybe_same = arg01;
7065 swap = true;
7067 else
7068 maybe_same = arg11;
7070 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7071 /* The remainder should not be a constant, otherwise we
7072 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7073 increased the number of multiplications necessary. */
7074 && TREE_CODE (arg10) != INTEGER_CST)
7076 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7077 build_int_cst (TREE_TYPE (arg00),
7078 int01 / int11));
7079 alt1 = arg10;
7080 same = maybe_same;
7081 if (swap)
7082 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7086 if (same)
7087 return fold_build2_loc (loc, MULT_EXPR, type,
7088 fold_build2_loc (loc, code, type,
7089 fold_convert_loc (loc, type, alt0),
7090 fold_convert_loc (loc, type, alt1)),
7091 fold_convert_loc (loc, type, same));
7093 return NULL_TREE;
7096 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7097 specified by EXPR into the buffer PTR of length LEN bytes.
7098 Return the number of bytes placed in the buffer, or zero
7099 upon failure. */
7101 static int
7102 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7104 tree type = TREE_TYPE (expr);
7105 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7106 int byte, offset, word, words;
7107 unsigned char value;
7109 if ((off == -1 && total_bytes > len)
7110 || off >= total_bytes)
7111 return 0;
7112 if (off == -1)
7113 off = 0;
7114 words = total_bytes / UNITS_PER_WORD;
7116 for (byte = 0; byte < total_bytes; byte++)
7118 int bitpos = byte * BITS_PER_UNIT;
7119 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7120 number of bytes. */
7121 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7123 if (total_bytes > UNITS_PER_WORD)
7125 word = byte / UNITS_PER_WORD;
7126 if (WORDS_BIG_ENDIAN)
7127 word = (words - 1) - word;
7128 offset = word * UNITS_PER_WORD;
7129 if (BYTES_BIG_ENDIAN)
7130 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7131 else
7132 offset += byte % UNITS_PER_WORD;
7134 else
7135 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7136 if (offset >= off
7137 && offset - off < len)
7138 ptr[offset - off] = value;
7140 return MIN (len, total_bytes - off);
7144 /* Subroutine of native_encode_expr. Encode the FIXED_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_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7152 tree type = TREE_TYPE (expr);
7153 machine_mode mode = TYPE_MODE (type);
7154 int total_bytes = GET_MODE_SIZE (mode);
7155 FIXED_VALUE_TYPE value;
7156 tree i_value, i_type;
7158 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7159 return 0;
7161 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7163 if (NULL_TREE == i_type
7164 || TYPE_PRECISION (i_type) != total_bytes)
7165 return 0;
7167 value = TREE_FIXED_CST (expr);
7168 i_value = double_int_to_tree (i_type, value.data);
7170 return native_encode_int (i_value, ptr, len, off);
7174 /* Subroutine of native_encode_expr. Encode the REAL_CST
7175 specified by EXPR into the buffer PTR of length LEN bytes.
7176 Return the number of bytes placed in the buffer, or zero
7177 upon failure. */
7179 static int
7180 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7182 tree type = TREE_TYPE (expr);
7183 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7184 int byte, offset, word, words, bitpos;
7185 unsigned char value;
7187 /* There are always 32 bits in each long, no matter the size of
7188 the hosts long. We handle floating point representations with
7189 up to 192 bits. */
7190 long tmp[6];
7192 if ((off == -1 && total_bytes > len)
7193 || off >= total_bytes)
7194 return 0;
7195 if (off == -1)
7196 off = 0;
7197 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7199 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7201 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7202 bitpos += BITS_PER_UNIT)
7204 byte = (bitpos / BITS_PER_UNIT) & 3;
7205 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7207 if (UNITS_PER_WORD < 4)
7209 word = byte / UNITS_PER_WORD;
7210 if (WORDS_BIG_ENDIAN)
7211 word = (words - 1) - word;
7212 offset = word * UNITS_PER_WORD;
7213 if (BYTES_BIG_ENDIAN)
7214 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7215 else
7216 offset += byte % UNITS_PER_WORD;
7218 else
7219 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7220 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7221 if (offset >= off
7222 && offset - off < len)
7223 ptr[offset - off] = value;
7225 return MIN (len, total_bytes - off);
7228 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7229 specified by EXPR into the buffer PTR of length LEN bytes.
7230 Return the number of bytes placed in the buffer, or zero
7231 upon failure. */
7233 static int
7234 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7236 int rsize, isize;
7237 tree part;
7239 part = TREE_REALPART (expr);
7240 rsize = native_encode_expr (part, ptr, len, off);
7241 if (off == -1
7242 && rsize == 0)
7243 return 0;
7244 part = TREE_IMAGPART (expr);
7245 if (off != -1)
7246 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7247 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7248 if (off == -1
7249 && isize != rsize)
7250 return 0;
7251 return rsize + isize;
7255 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7256 specified by EXPR into the buffer PTR of length LEN bytes.
7257 Return the number of bytes placed in the buffer, or zero
7258 upon failure. */
7260 static int
7261 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7263 unsigned i, count;
7264 int size, offset;
7265 tree itype, elem;
7267 offset = 0;
7268 count = VECTOR_CST_NELTS (expr);
7269 itype = TREE_TYPE (TREE_TYPE (expr));
7270 size = GET_MODE_SIZE (TYPE_MODE (itype));
7271 for (i = 0; i < count; i++)
7273 if (off >= size)
7275 off -= size;
7276 continue;
7278 elem = VECTOR_CST_ELT (expr, i);
7279 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7280 if ((off == -1 && res != size)
7281 || res == 0)
7282 return 0;
7283 offset += res;
7284 if (offset >= len)
7285 return offset;
7286 if (off != -1)
7287 off = 0;
7289 return offset;
7293 /* Subroutine of native_encode_expr. Encode the STRING_CST
7294 specified by EXPR into the buffer PTR of length LEN bytes.
7295 Return the number of bytes placed in the buffer, or zero
7296 upon failure. */
7298 static int
7299 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7301 tree type = TREE_TYPE (expr);
7302 HOST_WIDE_INT total_bytes;
7304 if (TREE_CODE (type) != ARRAY_TYPE
7305 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7306 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7307 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7308 return 0;
7309 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7310 if ((off == -1 && total_bytes > len)
7311 || off >= total_bytes)
7312 return 0;
7313 if (off == -1)
7314 off = 0;
7315 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7317 int written = 0;
7318 if (off < TREE_STRING_LENGTH (expr))
7320 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7321 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7323 memset (ptr + written, 0,
7324 MIN (total_bytes - written, len - written));
7326 else
7327 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7328 return MIN (total_bytes - off, len);
7332 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7333 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7334 buffer PTR of length LEN bytes. If OFF is not -1 then start
7335 the encoding at byte offset OFF and encode at most LEN bytes.
7336 Return the number of bytes placed in the buffer, or zero upon failure. */
7339 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7341 /* We don't support starting at negative offset and -1 is special. */
7342 if (off < -1)
7343 return 0;
7345 switch (TREE_CODE (expr))
7347 case INTEGER_CST:
7348 return native_encode_int (expr, ptr, len, off);
7350 case REAL_CST:
7351 return native_encode_real (expr, ptr, len, off);
7353 case FIXED_CST:
7354 return native_encode_fixed (expr, ptr, len, off);
7356 case COMPLEX_CST:
7357 return native_encode_complex (expr, ptr, len, off);
7359 case VECTOR_CST:
7360 return native_encode_vector (expr, ptr, len, off);
7362 case STRING_CST:
7363 return native_encode_string (expr, ptr, len, off);
7365 default:
7366 return 0;
7371 /* Subroutine of native_interpret_expr. Interpret the contents of
7372 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7373 If the buffer cannot be interpreted, return NULL_TREE. */
7375 static tree
7376 native_interpret_int (tree type, const unsigned char *ptr, int len)
7378 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7380 if (total_bytes > len
7381 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7382 return NULL_TREE;
7384 wide_int result = wi::from_buffer (ptr, total_bytes);
7386 return wide_int_to_tree (type, result);
7390 /* Subroutine of native_interpret_expr. Interpret the contents of
7391 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7392 If the buffer cannot be interpreted, return NULL_TREE. */
7394 static tree
7395 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7397 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7398 double_int result;
7399 FIXED_VALUE_TYPE fixed_value;
7401 if (total_bytes > len
7402 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7403 return NULL_TREE;
7405 result = double_int::from_buffer (ptr, total_bytes);
7406 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7408 return build_fixed (type, fixed_value);
7412 /* Subroutine of native_interpret_expr. Interpret the contents of
7413 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7414 If the buffer cannot be interpreted, return NULL_TREE. */
7416 static tree
7417 native_interpret_real (tree type, const unsigned char *ptr, int len)
7419 machine_mode mode = TYPE_MODE (type);
7420 int total_bytes = GET_MODE_SIZE (mode);
7421 unsigned char value;
7422 /* There are always 32 bits in each long, no matter the size of
7423 the hosts long. We handle floating point representations with
7424 up to 192 bits. */
7425 REAL_VALUE_TYPE r;
7426 long tmp[6];
7428 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7429 if (total_bytes > len || total_bytes > 24)
7430 return NULL_TREE;
7431 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7433 memset (tmp, 0, sizeof (tmp));
7434 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7435 bitpos += BITS_PER_UNIT)
7437 /* Both OFFSET and BYTE index within a long;
7438 bitpos indexes the whole float. */
7439 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7440 if (UNITS_PER_WORD < 4)
7442 int word = byte / UNITS_PER_WORD;
7443 if (WORDS_BIG_ENDIAN)
7444 word = (words - 1) - word;
7445 offset = word * UNITS_PER_WORD;
7446 if (BYTES_BIG_ENDIAN)
7447 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7448 else
7449 offset += byte % UNITS_PER_WORD;
7451 else
7453 offset = byte;
7454 if (BYTES_BIG_ENDIAN)
7456 /* Reverse bytes within each long, or within the entire float
7457 if it's smaller than a long (for HFmode). */
7458 offset = MIN (3, total_bytes - 1) - offset;
7459 gcc_assert (offset >= 0);
7462 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7464 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7467 real_from_target (&r, tmp, mode);
7468 return build_real (type, r);
7472 /* Subroutine of native_interpret_expr. Interpret the contents of
7473 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7474 If the buffer cannot be interpreted, return NULL_TREE. */
7476 static tree
7477 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7479 tree etype, rpart, ipart;
7480 int size;
7482 etype = TREE_TYPE (type);
7483 size = GET_MODE_SIZE (TYPE_MODE (etype));
7484 if (size * 2 > len)
7485 return NULL_TREE;
7486 rpart = native_interpret_expr (etype, ptr, size);
7487 if (!rpart)
7488 return NULL_TREE;
7489 ipart = native_interpret_expr (etype, ptr+size, size);
7490 if (!ipart)
7491 return NULL_TREE;
7492 return build_complex (type, rpart, ipart);
7496 /* Subroutine of native_interpret_expr. Interpret the contents of
7497 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7498 If the buffer cannot be interpreted, return NULL_TREE. */
7500 static tree
7501 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7503 tree etype, elem;
7504 int i, size, count;
7505 tree *elements;
7507 etype = TREE_TYPE (type);
7508 size = GET_MODE_SIZE (TYPE_MODE (etype));
7509 count = TYPE_VECTOR_SUBPARTS (type);
7510 if (size * count > len)
7511 return NULL_TREE;
7513 elements = XALLOCAVEC (tree, count);
7514 for (i = count - 1; i >= 0; i--)
7516 elem = native_interpret_expr (etype, ptr+(i*size), size);
7517 if (!elem)
7518 return NULL_TREE;
7519 elements[i] = elem;
7521 return build_vector (type, elements);
7525 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7526 the buffer PTR of length LEN as a constant of type TYPE. For
7527 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7528 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7529 return NULL_TREE. */
7531 tree
7532 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7534 switch (TREE_CODE (type))
7536 case INTEGER_TYPE:
7537 case ENUMERAL_TYPE:
7538 case BOOLEAN_TYPE:
7539 case POINTER_TYPE:
7540 case REFERENCE_TYPE:
7541 return native_interpret_int (type, ptr, len);
7543 case REAL_TYPE:
7544 return native_interpret_real (type, ptr, len);
7546 case FIXED_POINT_TYPE:
7547 return native_interpret_fixed (type, ptr, len);
7549 case COMPLEX_TYPE:
7550 return native_interpret_complex (type, ptr, len);
7552 case VECTOR_TYPE:
7553 return native_interpret_vector (type, ptr, len);
7555 default:
7556 return NULL_TREE;
7560 /* Returns true if we can interpret the contents of a native encoding
7561 as TYPE. */
7563 static bool
7564 can_native_interpret_type_p (tree type)
7566 switch (TREE_CODE (type))
7568 case INTEGER_TYPE:
7569 case ENUMERAL_TYPE:
7570 case BOOLEAN_TYPE:
7571 case POINTER_TYPE:
7572 case REFERENCE_TYPE:
7573 case FIXED_POINT_TYPE:
7574 case REAL_TYPE:
7575 case COMPLEX_TYPE:
7576 case VECTOR_TYPE:
7577 return true;
7578 default:
7579 return false;
7583 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7584 TYPE at compile-time. If we're unable to perform the conversion
7585 return NULL_TREE. */
7587 static tree
7588 fold_view_convert_expr (tree type, tree expr)
7590 /* We support up to 512-bit values (for V8DFmode). */
7591 unsigned char buffer[64];
7592 int len;
7594 /* Check that the host and target are sane. */
7595 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7596 return NULL_TREE;
7598 len = native_encode_expr (expr, buffer, sizeof (buffer));
7599 if (len == 0)
7600 return NULL_TREE;
7602 return native_interpret_expr (type, buffer, len);
7605 /* Build an expression for the address of T. Folds away INDIRECT_REF
7606 to avoid confusing the gimplify process. */
7608 tree
7609 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7611 /* The size of the object is not relevant when talking about its address. */
7612 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7613 t = TREE_OPERAND (t, 0);
7615 if (TREE_CODE (t) == INDIRECT_REF)
7617 t = TREE_OPERAND (t, 0);
7619 if (TREE_TYPE (t) != ptrtype)
7620 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7622 else if (TREE_CODE (t) == MEM_REF
7623 && integer_zerop (TREE_OPERAND (t, 1)))
7624 return TREE_OPERAND (t, 0);
7625 else if (TREE_CODE (t) == MEM_REF
7626 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7627 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7628 TREE_OPERAND (t, 0),
7629 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7630 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7632 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7634 if (TREE_TYPE (t) != ptrtype)
7635 t = fold_convert_loc (loc, ptrtype, t);
7637 else
7638 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7640 return t;
7643 /* Build an expression for the address of T. */
7645 tree
7646 build_fold_addr_expr_loc (location_t loc, tree t)
7648 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7650 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7653 /* Fold a unary expression of code CODE and type TYPE with operand
7654 OP0. Return the folded expression if folding is successful.
7655 Otherwise, return NULL_TREE. */
7657 tree
7658 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7660 tree tem;
7661 tree arg0;
7662 enum tree_code_class kind = TREE_CODE_CLASS (code);
7664 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7665 && TREE_CODE_LENGTH (code) == 1);
7667 arg0 = op0;
7668 if (arg0)
7670 if (CONVERT_EXPR_CODE_P (code)
7671 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7673 /* Don't use STRIP_NOPS, because signedness of argument type
7674 matters. */
7675 STRIP_SIGN_NOPS (arg0);
7677 else
7679 /* Strip any conversions that don't change the mode. This
7680 is safe for every expression, except for a comparison
7681 expression because its signedness is derived from its
7682 operands.
7684 Note that this is done as an internal manipulation within
7685 the constant folder, in order to find the simplest
7686 representation of the arguments so that their form can be
7687 studied. In any cases, the appropriate type conversions
7688 should be put back in the tree that will get out of the
7689 constant folder. */
7690 STRIP_NOPS (arg0);
7693 if (CONSTANT_CLASS_P (arg0))
7695 tree tem = const_unop (code, type, arg0);
7696 if (tem)
7698 if (TREE_TYPE (tem) != type)
7699 tem = fold_convert_loc (loc, type, tem);
7700 return tem;
7705 tem = generic_simplify (loc, code, type, op0);
7706 if (tem)
7707 return tem;
7709 if (TREE_CODE_CLASS (code) == tcc_unary)
7711 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7712 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7713 fold_build1_loc (loc, code, type,
7714 fold_convert_loc (loc, TREE_TYPE (op0),
7715 TREE_OPERAND (arg0, 1))));
7716 else if (TREE_CODE (arg0) == COND_EXPR)
7718 tree arg01 = TREE_OPERAND (arg0, 1);
7719 tree arg02 = TREE_OPERAND (arg0, 2);
7720 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7721 arg01 = fold_build1_loc (loc, code, type,
7722 fold_convert_loc (loc,
7723 TREE_TYPE (op0), arg01));
7724 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7725 arg02 = fold_build1_loc (loc, code, type,
7726 fold_convert_loc (loc,
7727 TREE_TYPE (op0), arg02));
7728 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7729 arg01, arg02);
7731 /* If this was a conversion, and all we did was to move into
7732 inside the COND_EXPR, bring it back out. But leave it if
7733 it is a conversion from integer to integer and the
7734 result precision is no wider than a word since such a
7735 conversion is cheap and may be optimized away by combine,
7736 while it couldn't if it were outside the COND_EXPR. Then return
7737 so we don't get into an infinite recursion loop taking the
7738 conversion out and then back in. */
7740 if ((CONVERT_EXPR_CODE_P (code)
7741 || code == NON_LVALUE_EXPR)
7742 && TREE_CODE (tem) == COND_EXPR
7743 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7744 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7745 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7746 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7747 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7748 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7749 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7750 && (INTEGRAL_TYPE_P
7751 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7752 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7753 || flag_syntax_only))
7754 tem = build1_loc (loc, code, type,
7755 build3 (COND_EXPR,
7756 TREE_TYPE (TREE_OPERAND
7757 (TREE_OPERAND (tem, 1), 0)),
7758 TREE_OPERAND (tem, 0),
7759 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7760 TREE_OPERAND (TREE_OPERAND (tem, 2),
7761 0)));
7762 return tem;
7766 switch (code)
7768 case NON_LVALUE_EXPR:
7769 if (!maybe_lvalue_p (op0))
7770 return fold_convert_loc (loc, type, op0);
7771 return NULL_TREE;
7773 CASE_CONVERT:
7774 case FLOAT_EXPR:
7775 case FIX_TRUNC_EXPR:
7776 if (COMPARISON_CLASS_P (op0))
7778 /* If we have (type) (a CMP b) and type is an integral type, return
7779 new expression involving the new type. Canonicalize
7780 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7781 non-integral type.
7782 Do not fold the result as that would not simplify further, also
7783 folding again results in recursions. */
7784 if (TREE_CODE (type) == BOOLEAN_TYPE)
7785 return build2_loc (loc, TREE_CODE (op0), type,
7786 TREE_OPERAND (op0, 0),
7787 TREE_OPERAND (op0, 1));
7788 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7789 && TREE_CODE (type) != VECTOR_TYPE)
7790 return build3_loc (loc, COND_EXPR, type, op0,
7791 constant_boolean_node (true, type),
7792 constant_boolean_node (false, type));
7795 /* Handle (T *)&A.B.C for A being of type T and B and C
7796 living at offset zero. This occurs frequently in
7797 C++ upcasting and then accessing the base. */
7798 if (TREE_CODE (op0) == ADDR_EXPR
7799 && POINTER_TYPE_P (type)
7800 && handled_component_p (TREE_OPERAND (op0, 0)))
7802 HOST_WIDE_INT bitsize, bitpos;
7803 tree offset;
7804 machine_mode mode;
7805 int unsignedp, reversep, volatilep;
7806 tree base
7807 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7808 &offset, &mode, &unsignedp, &reversep,
7809 &volatilep, false);
7810 /* If the reference was to a (constant) zero offset, we can use
7811 the address of the base if it has the same base type
7812 as the result type and the pointer type is unqualified. */
7813 if (! offset && bitpos == 0
7814 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7815 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7816 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7817 return fold_convert_loc (loc, type,
7818 build_fold_addr_expr_loc (loc, base));
7821 if (TREE_CODE (op0) == MODIFY_EXPR
7822 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7823 /* Detect assigning a bitfield. */
7824 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7825 && DECL_BIT_FIELD
7826 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7828 /* Don't leave an assignment inside a conversion
7829 unless assigning a bitfield. */
7830 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7831 /* First do the assignment, then return converted constant. */
7832 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7833 TREE_NO_WARNING (tem) = 1;
7834 TREE_USED (tem) = 1;
7835 return tem;
7838 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7839 constants (if x has signed type, the sign bit cannot be set
7840 in c). This folds extension into the BIT_AND_EXPR.
7841 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7842 very likely don't have maximal range for their precision and this
7843 transformation effectively doesn't preserve non-maximal ranges. */
7844 if (TREE_CODE (type) == INTEGER_TYPE
7845 && TREE_CODE (op0) == BIT_AND_EXPR
7846 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7848 tree and_expr = op0;
7849 tree and0 = TREE_OPERAND (and_expr, 0);
7850 tree and1 = TREE_OPERAND (and_expr, 1);
7851 int change = 0;
7853 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7854 || (TYPE_PRECISION (type)
7855 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7856 change = 1;
7857 else if (TYPE_PRECISION (TREE_TYPE (and1))
7858 <= HOST_BITS_PER_WIDE_INT
7859 && tree_fits_uhwi_p (and1))
7861 unsigned HOST_WIDE_INT cst;
7863 cst = tree_to_uhwi (and1);
7864 cst &= HOST_WIDE_INT_M1U
7865 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7866 change = (cst == 0);
7867 if (change
7868 && !flag_syntax_only
7869 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7870 == ZERO_EXTEND))
7872 tree uns = unsigned_type_for (TREE_TYPE (and0));
7873 and0 = fold_convert_loc (loc, uns, and0);
7874 and1 = fold_convert_loc (loc, uns, and1);
7877 if (change)
7879 tem = force_fit_type (type, wi::to_widest (and1), 0,
7880 TREE_OVERFLOW (and1));
7881 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7882 fold_convert_loc (loc, type, and0), tem);
7886 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7887 cast (T1)X will fold away. We assume that this happens when X itself
7888 is a cast. */
7889 if (POINTER_TYPE_P (type)
7890 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7891 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7893 tree arg00 = TREE_OPERAND (arg0, 0);
7894 tree arg01 = TREE_OPERAND (arg0, 1);
7896 return fold_build_pointer_plus_loc
7897 (loc, fold_convert_loc (loc, type, arg00), arg01);
7900 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7901 of the same precision, and X is an integer type not narrower than
7902 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7903 if (INTEGRAL_TYPE_P (type)
7904 && TREE_CODE (op0) == BIT_NOT_EXPR
7905 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7906 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7907 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7909 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7910 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7911 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7912 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7913 fold_convert_loc (loc, type, tem));
7916 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7917 type of X and Y (integer types only). */
7918 if (INTEGRAL_TYPE_P (type)
7919 && TREE_CODE (op0) == MULT_EXPR
7920 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7921 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7923 /* Be careful not to introduce new overflows. */
7924 tree mult_type;
7925 if (TYPE_OVERFLOW_WRAPS (type))
7926 mult_type = type;
7927 else
7928 mult_type = unsigned_type_for (type);
7930 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7932 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7933 fold_convert_loc (loc, mult_type,
7934 TREE_OPERAND (op0, 0)),
7935 fold_convert_loc (loc, mult_type,
7936 TREE_OPERAND (op0, 1)));
7937 return fold_convert_loc (loc, type, tem);
7941 return NULL_TREE;
7943 case VIEW_CONVERT_EXPR:
7944 if (TREE_CODE (op0) == MEM_REF)
7946 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7947 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7948 tem = fold_build2_loc (loc, MEM_REF, type,
7949 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7950 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7951 return tem;
7954 return NULL_TREE;
7956 case NEGATE_EXPR:
7957 tem = fold_negate_expr (loc, arg0);
7958 if (tem)
7959 return fold_convert_loc (loc, type, tem);
7960 return NULL_TREE;
7962 case ABS_EXPR:
7963 /* Convert fabs((double)float) into (double)fabsf(float). */
7964 if (TREE_CODE (arg0) == NOP_EXPR
7965 && TREE_CODE (type) == REAL_TYPE)
7967 tree targ0 = strip_float_extensions (arg0);
7968 if (targ0 != arg0)
7969 return fold_convert_loc (loc, type,
7970 fold_build1_loc (loc, ABS_EXPR,
7971 TREE_TYPE (targ0),
7972 targ0));
7974 return NULL_TREE;
7976 case BIT_NOT_EXPR:
7977 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7978 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7979 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7980 fold_convert_loc (loc, type,
7981 TREE_OPERAND (arg0, 0)))))
7982 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7983 fold_convert_loc (loc, type,
7984 TREE_OPERAND (arg0, 1)));
7985 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7986 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7987 fold_convert_loc (loc, type,
7988 TREE_OPERAND (arg0, 1)))))
7989 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7990 fold_convert_loc (loc, type,
7991 TREE_OPERAND (arg0, 0)), tem);
7993 return NULL_TREE;
7995 case TRUTH_NOT_EXPR:
7996 /* Note that the operand of this must be an int
7997 and its values must be 0 or 1.
7998 ("true" is a fixed value perhaps depending on the language,
7999 but we don't handle values other than 1 correctly yet.) */
8000 tem = fold_truth_not_expr (loc, arg0);
8001 if (!tem)
8002 return NULL_TREE;
8003 return fold_convert_loc (loc, type, tem);
8005 case INDIRECT_REF:
8006 /* Fold *&X to X if X is an lvalue. */
8007 if (TREE_CODE (op0) == ADDR_EXPR)
8009 tree op00 = TREE_OPERAND (op0, 0);
8010 if ((TREE_CODE (op00) == VAR_DECL
8011 || TREE_CODE (op00) == PARM_DECL
8012 || TREE_CODE (op00) == RESULT_DECL)
8013 && !TREE_READONLY (op00))
8014 return op00;
8016 return NULL_TREE;
8018 default:
8019 return NULL_TREE;
8020 } /* switch (code) */
8024 /* If the operation was a conversion do _not_ mark a resulting constant
8025 with TREE_OVERFLOW if the original constant was not. These conversions
8026 have implementation defined behavior and retaining the TREE_OVERFLOW
8027 flag here would confuse later passes such as VRP. */
8028 tree
8029 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8030 tree type, tree op0)
8032 tree res = fold_unary_loc (loc, code, type, op0);
8033 if (res
8034 && TREE_CODE (res) == INTEGER_CST
8035 && TREE_CODE (op0) == INTEGER_CST
8036 && CONVERT_EXPR_CODE_P (code))
8037 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8039 return res;
8042 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8043 operands OP0 and OP1. LOC is the location of the resulting expression.
8044 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8045 Return the folded expression if folding is successful. Otherwise,
8046 return NULL_TREE. */
8047 static tree
8048 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8049 tree arg0, tree arg1, tree op0, tree op1)
8051 tree tem;
8053 /* We only do these simplifications if we are optimizing. */
8054 if (!optimize)
8055 return NULL_TREE;
8057 /* Check for things like (A || B) && (A || C). We can convert this
8058 to A || (B && C). Note that either operator can be any of the four
8059 truth and/or operations and the transformation will still be
8060 valid. Also note that we only care about order for the
8061 ANDIF and ORIF operators. If B contains side effects, this
8062 might change the truth-value of A. */
8063 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8064 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8065 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8066 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8067 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8068 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8070 tree a00 = TREE_OPERAND (arg0, 0);
8071 tree a01 = TREE_OPERAND (arg0, 1);
8072 tree a10 = TREE_OPERAND (arg1, 0);
8073 tree a11 = TREE_OPERAND (arg1, 1);
8074 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8075 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8076 && (code == TRUTH_AND_EXPR
8077 || code == TRUTH_OR_EXPR));
8079 if (operand_equal_p (a00, a10, 0))
8080 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8081 fold_build2_loc (loc, code, type, a01, a11));
8082 else if (commutative && operand_equal_p (a00, a11, 0))
8083 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8084 fold_build2_loc (loc, code, type, a01, a10));
8085 else if (commutative && operand_equal_p (a01, a10, 0))
8086 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8087 fold_build2_loc (loc, code, type, a00, a11));
8089 /* This case if tricky because we must either have commutative
8090 operators or else A10 must not have side-effects. */
8092 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8093 && operand_equal_p (a01, a11, 0))
8094 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8095 fold_build2_loc (loc, code, type, a00, a10),
8096 a01);
8099 /* See if we can build a range comparison. */
8100 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8101 return tem;
8103 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8104 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8106 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8107 if (tem)
8108 return fold_build2_loc (loc, code, type, tem, arg1);
8111 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8112 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8114 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8115 if (tem)
8116 return fold_build2_loc (loc, code, type, arg0, tem);
8119 /* Check for the possibility of merging component references. If our
8120 lhs is another similar operation, try to merge its rhs with our
8121 rhs. Then try to merge our lhs and rhs. */
8122 if (TREE_CODE (arg0) == code
8123 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8124 TREE_OPERAND (arg0, 1), arg1)))
8125 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8127 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8128 return tem;
8130 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8131 && (code == TRUTH_AND_EXPR
8132 || code == TRUTH_ANDIF_EXPR
8133 || code == TRUTH_OR_EXPR
8134 || code == TRUTH_ORIF_EXPR))
8136 enum tree_code ncode, icode;
8138 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8139 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8140 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8142 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8143 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8144 We don't want to pack more than two leafs to a non-IF AND/OR
8145 expression.
8146 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8147 equal to IF-CODE, then we don't want to add right-hand operand.
8148 If the inner right-hand side of left-hand operand has
8149 side-effects, or isn't simple, then we can't add to it,
8150 as otherwise we might destroy if-sequence. */
8151 if (TREE_CODE (arg0) == icode
8152 && simple_operand_p_2 (arg1)
8153 /* Needed for sequence points to handle trappings, and
8154 side-effects. */
8155 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8157 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8158 arg1);
8159 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8160 tem);
8162 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8163 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8164 else if (TREE_CODE (arg1) == icode
8165 && simple_operand_p_2 (arg0)
8166 /* Needed for sequence points to handle trappings, and
8167 side-effects. */
8168 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8170 tem = fold_build2_loc (loc, ncode, type,
8171 arg0, TREE_OPERAND (arg1, 0));
8172 return fold_build2_loc (loc, icode, type, tem,
8173 TREE_OPERAND (arg1, 1));
8175 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8176 into (A OR B).
8177 For sequence point consistancy, we need to check for trapping,
8178 and side-effects. */
8179 else if (code == icode && simple_operand_p_2 (arg0)
8180 && simple_operand_p_2 (arg1))
8181 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8184 return NULL_TREE;
8187 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8188 by changing CODE to reduce the magnitude of constants involved in
8189 ARG0 of the comparison.
8190 Returns a canonicalized comparison tree if a simplification was
8191 possible, otherwise returns NULL_TREE.
8192 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8193 valid if signed overflow is undefined. */
8195 static tree
8196 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8197 tree arg0, tree arg1,
8198 bool *strict_overflow_p)
8200 enum tree_code code0 = TREE_CODE (arg0);
8201 tree t, cst0 = NULL_TREE;
8202 int sgn0;
8204 /* Match A +- CST code arg1. We can change this only if overflow
8205 is undefined. */
8206 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8207 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8208 /* In principle pointers also have undefined overflow behavior,
8209 but that causes problems elsewhere. */
8210 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8211 && (code0 == MINUS_EXPR
8212 || code0 == PLUS_EXPR)
8213 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8214 return NULL_TREE;
8216 /* Identify the constant in arg0 and its sign. */
8217 cst0 = TREE_OPERAND (arg0, 1);
8218 sgn0 = tree_int_cst_sgn (cst0);
8220 /* Overflowed constants and zero will cause problems. */
8221 if (integer_zerop (cst0)
8222 || TREE_OVERFLOW (cst0))
8223 return NULL_TREE;
8225 /* See if we can reduce the magnitude of the constant in
8226 arg0 by changing the comparison code. */
8227 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8228 if (code == LT_EXPR
8229 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8230 code = LE_EXPR;
8231 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8232 else if (code == GT_EXPR
8233 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8234 code = GE_EXPR;
8235 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8236 else if (code == LE_EXPR
8237 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8238 code = LT_EXPR;
8239 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8240 else if (code == GE_EXPR
8241 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8242 code = GT_EXPR;
8243 else
8244 return NULL_TREE;
8245 *strict_overflow_p = true;
8247 /* Now build the constant reduced in magnitude. But not if that
8248 would produce one outside of its types range. */
8249 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8250 && ((sgn0 == 1
8251 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8252 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8253 || (sgn0 == -1
8254 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8255 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8256 return NULL_TREE;
8258 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8259 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8260 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8261 t = fold_convert (TREE_TYPE (arg1), t);
8263 return fold_build2_loc (loc, code, type, t, arg1);
8266 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8267 overflow further. Try to decrease the magnitude of constants involved
8268 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8269 and put sole constants at the second argument position.
8270 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8272 static tree
8273 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8274 tree arg0, tree arg1)
8276 tree t;
8277 bool strict_overflow_p;
8278 const char * const warnmsg = G_("assuming signed overflow does not occur "
8279 "when reducing constant in comparison");
8281 /* Try canonicalization by simplifying arg0. */
8282 strict_overflow_p = false;
8283 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8284 &strict_overflow_p);
8285 if (t)
8287 if (strict_overflow_p)
8288 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8289 return t;
8292 /* Try canonicalization by simplifying arg1 using the swapped
8293 comparison. */
8294 code = swap_tree_comparison (code);
8295 strict_overflow_p = false;
8296 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8297 &strict_overflow_p);
8298 if (t && strict_overflow_p)
8299 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8300 return t;
8303 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8304 space. This is used to avoid issuing overflow warnings for
8305 expressions like &p->x which can not wrap. */
8307 static bool
8308 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8310 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8311 return true;
8313 if (bitpos < 0)
8314 return true;
8316 wide_int wi_offset;
8317 int precision = TYPE_PRECISION (TREE_TYPE (base));
8318 if (offset == NULL_TREE)
8319 wi_offset = wi::zero (precision);
8320 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8321 return true;
8322 else
8323 wi_offset = offset;
8325 bool overflow;
8326 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8327 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8328 if (overflow)
8329 return true;
8331 if (!wi::fits_uhwi_p (total))
8332 return true;
8334 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8335 if (size <= 0)
8336 return true;
8338 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8339 array. */
8340 if (TREE_CODE (base) == ADDR_EXPR)
8342 HOST_WIDE_INT base_size;
8344 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8345 if (base_size > 0 && size < base_size)
8346 size = base_size;
8349 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8352 /* Return a positive integer when the symbol DECL is known to have
8353 a nonzero address, zero when it's known not to (e.g., it's a weak
8354 symbol), and a negative integer when the symbol is not yet in the
8355 symbol table and so whether or not its address is zero is unknown. */
8356 static int
8357 maybe_nonzero_address (tree decl)
8359 if (DECL_P (decl) && decl_in_symtab_p (decl))
8360 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8361 return symbol->nonzero_address ();
8363 return -1;
8366 /* Subroutine of fold_binary. This routine performs all of the
8367 transformations that are common to the equality/inequality
8368 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8369 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8370 fold_binary should call fold_binary. Fold a comparison with
8371 tree code CODE and type TYPE with operands OP0 and OP1. Return
8372 the folded comparison or NULL_TREE. */
8374 static tree
8375 fold_comparison (location_t loc, enum tree_code code, tree type,
8376 tree op0, tree op1)
8378 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8379 tree arg0, arg1, tem;
8381 arg0 = op0;
8382 arg1 = op1;
8384 STRIP_SIGN_NOPS (arg0);
8385 STRIP_SIGN_NOPS (arg1);
8387 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8388 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8389 && (equality_code
8390 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8391 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8392 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8393 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8394 && TREE_CODE (arg1) == INTEGER_CST
8395 && !TREE_OVERFLOW (arg1))
8397 const enum tree_code
8398 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8399 tree const1 = TREE_OPERAND (arg0, 1);
8400 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8401 tree variable = TREE_OPERAND (arg0, 0);
8402 tree new_const = int_const_binop (reverse_op, const2, const1);
8404 /* If the constant operation overflowed this can be
8405 simplified as a comparison against INT_MAX/INT_MIN. */
8406 if (TREE_OVERFLOW (new_const)
8407 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8409 int const1_sgn = tree_int_cst_sgn (const1);
8410 enum tree_code code2 = code;
8412 /* Get the sign of the constant on the lhs if the
8413 operation were VARIABLE + CONST1. */
8414 if (TREE_CODE (arg0) == MINUS_EXPR)
8415 const1_sgn = -const1_sgn;
8417 /* The sign of the constant determines if we overflowed
8418 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8419 Canonicalize to the INT_MIN overflow by swapping the comparison
8420 if necessary. */
8421 if (const1_sgn == -1)
8422 code2 = swap_tree_comparison (code);
8424 /* We now can look at the canonicalized case
8425 VARIABLE + 1 CODE2 INT_MIN
8426 and decide on the result. */
8427 switch (code2)
8429 case EQ_EXPR:
8430 case LT_EXPR:
8431 case LE_EXPR:
8432 return
8433 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8435 case NE_EXPR:
8436 case GE_EXPR:
8437 case GT_EXPR:
8438 return
8439 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8441 default:
8442 gcc_unreachable ();
8445 else
8447 if (!equality_code)
8448 fold_overflow_warning ("assuming signed overflow does not occur "
8449 "when changing X +- C1 cmp C2 to "
8450 "X cmp C2 -+ C1",
8451 WARN_STRICT_OVERFLOW_COMPARISON);
8452 return fold_build2_loc (loc, code, type, variable, new_const);
8456 /* For comparisons of pointers we can decompose it to a compile time
8457 comparison of the base objects and the offsets into the object.
8458 This requires at least one operand being an ADDR_EXPR or a
8459 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8460 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8461 && (TREE_CODE (arg0) == ADDR_EXPR
8462 || TREE_CODE (arg1) == ADDR_EXPR
8463 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8464 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8466 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8467 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8468 machine_mode mode;
8469 int volatilep, reversep, unsignedp;
8470 bool indirect_base0 = false, indirect_base1 = false;
8472 /* Get base and offset for the access. Strip ADDR_EXPR for
8473 get_inner_reference, but put it back by stripping INDIRECT_REF
8474 off the base object if possible. indirect_baseN will be true
8475 if baseN is not an address but refers to the object itself. */
8476 base0 = arg0;
8477 if (TREE_CODE (arg0) == ADDR_EXPR)
8479 base0
8480 = get_inner_reference (TREE_OPERAND (arg0, 0),
8481 &bitsize, &bitpos0, &offset0, &mode,
8482 &unsignedp, &reversep, &volatilep, false);
8483 if (TREE_CODE (base0) == INDIRECT_REF)
8484 base0 = TREE_OPERAND (base0, 0);
8485 else
8486 indirect_base0 = true;
8488 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8490 base0 = TREE_OPERAND (arg0, 0);
8491 STRIP_SIGN_NOPS (base0);
8492 if (TREE_CODE (base0) == ADDR_EXPR)
8494 base0
8495 = get_inner_reference (TREE_OPERAND (base0, 0),
8496 &bitsize, &bitpos0, &offset0, &mode,
8497 &unsignedp, &reversep, &volatilep,
8498 false);
8499 if (TREE_CODE (base0) == INDIRECT_REF)
8500 base0 = TREE_OPERAND (base0, 0);
8501 else
8502 indirect_base0 = true;
8504 if (offset0 == NULL_TREE || integer_zerop (offset0))
8505 offset0 = TREE_OPERAND (arg0, 1);
8506 else
8507 offset0 = size_binop (PLUS_EXPR, offset0,
8508 TREE_OPERAND (arg0, 1));
8509 if (TREE_CODE (offset0) == INTEGER_CST)
8511 offset_int tem = wi::sext (wi::to_offset (offset0),
8512 TYPE_PRECISION (sizetype));
8513 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
8514 tem += bitpos0;
8515 if (wi::fits_shwi_p (tem))
8517 bitpos0 = tem.to_shwi ();
8518 offset0 = NULL_TREE;
8523 base1 = arg1;
8524 if (TREE_CODE (arg1) == ADDR_EXPR)
8526 base1
8527 = get_inner_reference (TREE_OPERAND (arg1, 0),
8528 &bitsize, &bitpos1, &offset1, &mode,
8529 &unsignedp, &reversep, &volatilep, false);
8530 if (TREE_CODE (base1) == INDIRECT_REF)
8531 base1 = TREE_OPERAND (base1, 0);
8532 else
8533 indirect_base1 = true;
8535 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8537 base1 = TREE_OPERAND (arg1, 0);
8538 STRIP_SIGN_NOPS (base1);
8539 if (TREE_CODE (base1) == ADDR_EXPR)
8541 base1
8542 = get_inner_reference (TREE_OPERAND (base1, 0),
8543 &bitsize, &bitpos1, &offset1, &mode,
8544 &unsignedp, &reversep, &volatilep,
8545 false);
8546 if (TREE_CODE (base1) == INDIRECT_REF)
8547 base1 = TREE_OPERAND (base1, 0);
8548 else
8549 indirect_base1 = true;
8551 if (offset1 == NULL_TREE || integer_zerop (offset1))
8552 offset1 = TREE_OPERAND (arg1, 1);
8553 else
8554 offset1 = size_binop (PLUS_EXPR, offset1,
8555 TREE_OPERAND (arg1, 1));
8556 if (TREE_CODE (offset1) == INTEGER_CST)
8558 offset_int tem = wi::sext (wi::to_offset (offset1),
8559 TYPE_PRECISION (sizetype));
8560 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
8561 tem += bitpos1;
8562 if (wi::fits_shwi_p (tem))
8564 bitpos1 = tem.to_shwi ();
8565 offset1 = NULL_TREE;
8570 /* If we have equivalent bases we might be able to simplify. */
8571 if (indirect_base0 == indirect_base1
8572 && operand_equal_p (base0, base1,
8573 indirect_base0 ? OEP_ADDRESS_OF : 0))
8575 /* We can fold this expression to a constant if the non-constant
8576 offset parts are equal. */
8577 if ((offset0 == offset1
8578 || (offset0 && offset1
8579 && operand_equal_p (offset0, offset1, 0)))
8580 && (equality_code
8581 || (indirect_base0
8582 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8583 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8586 if (!equality_code
8587 && bitpos0 != bitpos1
8588 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8589 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8590 fold_overflow_warning (("assuming pointer wraparound does not "
8591 "occur when comparing P +- C1 with "
8592 "P +- C2"),
8593 WARN_STRICT_OVERFLOW_CONDITIONAL);
8595 switch (code)
8597 case EQ_EXPR:
8598 return constant_boolean_node (bitpos0 == bitpos1, type);
8599 case NE_EXPR:
8600 return constant_boolean_node (bitpos0 != bitpos1, type);
8601 case LT_EXPR:
8602 return constant_boolean_node (bitpos0 < bitpos1, type);
8603 case LE_EXPR:
8604 return constant_boolean_node (bitpos0 <= bitpos1, type);
8605 case GE_EXPR:
8606 return constant_boolean_node (bitpos0 >= bitpos1, type);
8607 case GT_EXPR:
8608 return constant_boolean_node (bitpos0 > bitpos1, type);
8609 default:;
8612 /* We can simplify the comparison to a comparison of the variable
8613 offset parts if the constant offset parts are equal.
8614 Be careful to use signed sizetype here because otherwise we
8615 mess with array offsets in the wrong way. This is possible
8616 because pointer arithmetic is restricted to retain within an
8617 object and overflow on pointer differences is undefined as of
8618 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8619 else if (bitpos0 == bitpos1
8620 && (equality_code
8621 || (indirect_base0
8622 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8623 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8625 /* By converting to signed sizetype we cover middle-end pointer
8626 arithmetic which operates on unsigned pointer types of size
8627 type size and ARRAY_REF offsets which are properly sign or
8628 zero extended from their type in case it is narrower than
8629 sizetype. */
8630 if (offset0 == NULL_TREE)
8631 offset0 = build_int_cst (ssizetype, 0);
8632 else
8633 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8634 if (offset1 == NULL_TREE)
8635 offset1 = build_int_cst (ssizetype, 0);
8636 else
8637 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8639 if (!equality_code
8640 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8641 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8642 fold_overflow_warning (("assuming pointer wraparound does not "
8643 "occur when comparing P +- C1 with "
8644 "P +- C2"),
8645 WARN_STRICT_OVERFLOW_COMPARISON);
8647 return fold_build2_loc (loc, code, type, offset0, offset1);
8650 /* For equal offsets we can simplify to a comparison of the
8651 base addresses. */
8652 else if (bitpos0 == bitpos1
8653 && (indirect_base0
8654 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8655 && (indirect_base1
8656 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8657 && ((offset0 == offset1)
8658 || (offset0 && offset1
8659 && operand_equal_p (offset0, offset1, 0))))
8661 if (indirect_base0)
8662 base0 = build_fold_addr_expr_loc (loc, base0);
8663 if (indirect_base1)
8664 base1 = build_fold_addr_expr_loc (loc, base1);
8665 return fold_build2_loc (loc, code, type, base0, base1);
8667 /* Comparison between an ordinary (non-weak) symbol and a null
8668 pointer can be eliminated since such symbols must have a non
8669 null address. In C, relational expressions between pointers
8670 to objects and null pointers are undefined. The results
8671 below follow the C++ rules with the additional property that
8672 every object pointer compares greater than a null pointer.
8674 else if (DECL_P (base0)
8675 && maybe_nonzero_address (base0) > 0
8676 /* Avoid folding references to struct members at offset 0 to
8677 prevent tests like '&ptr->firstmember == 0' from getting
8678 eliminated. When ptr is null, although the -> expression
8679 is strictly speaking invalid, GCC retains it as a matter
8680 of QoI. See PR c/44555. */
8681 && (offset0 == NULL_TREE && bitpos0 != 0)
8682 /* The caller guarantees that when one of the arguments is
8683 constant (i.e., null in this case) it is second. */
8684 && integer_zerop (arg1))
8686 switch (code)
8688 case EQ_EXPR:
8689 case LE_EXPR:
8690 case LT_EXPR:
8691 return constant_boolean_node (false, type);
8692 case GE_EXPR:
8693 case GT_EXPR:
8694 case NE_EXPR:
8695 return constant_boolean_node (true, type);
8696 default:
8697 gcc_unreachable ();
8702 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8703 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8704 the resulting offset is smaller in absolute value than the
8705 original one and has the same sign. */
8706 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8707 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8708 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8709 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8710 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8711 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8712 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8713 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8715 tree const1 = TREE_OPERAND (arg0, 1);
8716 tree const2 = TREE_OPERAND (arg1, 1);
8717 tree variable1 = TREE_OPERAND (arg0, 0);
8718 tree variable2 = TREE_OPERAND (arg1, 0);
8719 tree cst;
8720 const char * const warnmsg = G_("assuming signed overflow does not "
8721 "occur when combining constants around "
8722 "a comparison");
8724 /* Put the constant on the side where it doesn't overflow and is
8725 of lower absolute value and of same sign than before. */
8726 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8727 ? MINUS_EXPR : PLUS_EXPR,
8728 const2, const1);
8729 if (!TREE_OVERFLOW (cst)
8730 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8731 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8733 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8734 return fold_build2_loc (loc, code, type,
8735 variable1,
8736 fold_build2_loc (loc, TREE_CODE (arg1),
8737 TREE_TYPE (arg1),
8738 variable2, cst));
8741 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8742 ? MINUS_EXPR : PLUS_EXPR,
8743 const1, const2);
8744 if (!TREE_OVERFLOW (cst)
8745 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8746 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8748 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8749 return fold_build2_loc (loc, code, type,
8750 fold_build2_loc (loc, TREE_CODE (arg0),
8751 TREE_TYPE (arg0),
8752 variable1, cst),
8753 variable2);
8757 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8758 if (tem)
8759 return tem;
8761 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8762 constant, we can simplify it. */
8763 if (TREE_CODE (arg1) == INTEGER_CST
8764 && (TREE_CODE (arg0) == MIN_EXPR
8765 || TREE_CODE (arg0) == MAX_EXPR)
8766 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8768 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8769 if (tem)
8770 return tem;
8773 /* If we are comparing an expression that just has comparisons
8774 of two integer values, arithmetic expressions of those comparisons,
8775 and constants, we can simplify it. There are only three cases
8776 to check: the two values can either be equal, the first can be
8777 greater, or the second can be greater. Fold the expression for
8778 those three values. Since each value must be 0 or 1, we have
8779 eight possibilities, each of which corresponds to the constant 0
8780 or 1 or one of the six possible comparisons.
8782 This handles common cases like (a > b) == 0 but also handles
8783 expressions like ((x > y) - (y > x)) > 0, which supposedly
8784 occur in macroized code. */
8786 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8788 tree cval1 = 0, cval2 = 0;
8789 int save_p = 0;
8791 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8792 /* Don't handle degenerate cases here; they should already
8793 have been handled anyway. */
8794 && cval1 != 0 && cval2 != 0
8795 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8796 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8797 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8798 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8799 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8800 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8801 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8803 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8804 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8806 /* We can't just pass T to eval_subst in case cval1 or cval2
8807 was the same as ARG1. */
8809 tree high_result
8810 = fold_build2_loc (loc, code, type,
8811 eval_subst (loc, arg0, cval1, maxval,
8812 cval2, minval),
8813 arg1);
8814 tree equal_result
8815 = fold_build2_loc (loc, code, type,
8816 eval_subst (loc, arg0, cval1, maxval,
8817 cval2, maxval),
8818 arg1);
8819 tree low_result
8820 = fold_build2_loc (loc, code, type,
8821 eval_subst (loc, arg0, cval1, minval,
8822 cval2, maxval),
8823 arg1);
8825 /* All three of these results should be 0 or 1. Confirm they are.
8826 Then use those values to select the proper code to use. */
8828 if (TREE_CODE (high_result) == INTEGER_CST
8829 && TREE_CODE (equal_result) == INTEGER_CST
8830 && TREE_CODE (low_result) == INTEGER_CST)
8832 /* Make a 3-bit mask with the high-order bit being the
8833 value for `>', the next for '=', and the low for '<'. */
8834 switch ((integer_onep (high_result) * 4)
8835 + (integer_onep (equal_result) * 2)
8836 + integer_onep (low_result))
8838 case 0:
8839 /* Always false. */
8840 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8841 case 1:
8842 code = LT_EXPR;
8843 break;
8844 case 2:
8845 code = EQ_EXPR;
8846 break;
8847 case 3:
8848 code = LE_EXPR;
8849 break;
8850 case 4:
8851 code = GT_EXPR;
8852 break;
8853 case 5:
8854 code = NE_EXPR;
8855 break;
8856 case 6:
8857 code = GE_EXPR;
8858 break;
8859 case 7:
8860 /* Always true. */
8861 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8864 if (save_p)
8866 tem = save_expr (build2 (code, type, cval1, cval2));
8867 SET_EXPR_LOCATION (tem, loc);
8868 return tem;
8870 return fold_build2_loc (loc, code, type, cval1, cval2);
8875 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8876 into a single range test. */
8877 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8878 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8879 && TREE_CODE (arg1) == INTEGER_CST
8880 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8881 && !integer_zerop (TREE_OPERAND (arg0, 1))
8882 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8883 && !TREE_OVERFLOW (arg1))
8885 tem = fold_div_compare (loc, code, type, arg0, arg1);
8886 if (tem != NULL_TREE)
8887 return tem;
8890 return NULL_TREE;
8894 /* Subroutine of fold_binary. Optimize complex multiplications of the
8895 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8896 argument EXPR represents the expression "z" of type TYPE. */
8898 static tree
8899 fold_mult_zconjz (location_t loc, tree type, tree expr)
8901 tree itype = TREE_TYPE (type);
8902 tree rpart, ipart, tem;
8904 if (TREE_CODE (expr) == COMPLEX_EXPR)
8906 rpart = TREE_OPERAND (expr, 0);
8907 ipart = TREE_OPERAND (expr, 1);
8909 else if (TREE_CODE (expr) == COMPLEX_CST)
8911 rpart = TREE_REALPART (expr);
8912 ipart = TREE_IMAGPART (expr);
8914 else
8916 expr = save_expr (expr);
8917 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8918 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8921 rpart = save_expr (rpart);
8922 ipart = save_expr (ipart);
8923 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8924 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8925 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8926 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8927 build_zero_cst (itype));
8931 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8932 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8934 static bool
8935 vec_cst_ctor_to_array (tree arg, tree *elts)
8937 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8939 if (TREE_CODE (arg) == VECTOR_CST)
8941 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8942 elts[i] = VECTOR_CST_ELT (arg, i);
8944 else if (TREE_CODE (arg) == CONSTRUCTOR)
8946 constructor_elt *elt;
8948 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8949 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8950 return false;
8951 else
8952 elts[i] = elt->value;
8954 else
8955 return false;
8956 for (; i < nelts; i++)
8957 elts[i]
8958 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8959 return true;
8962 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8963 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8964 NULL_TREE otherwise. */
8966 static tree
8967 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8969 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8970 tree *elts;
8971 bool need_ctor = false;
8973 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8974 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8975 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8976 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8977 return NULL_TREE;
8979 elts = XALLOCAVEC (tree, nelts * 3);
8980 if (!vec_cst_ctor_to_array (arg0, elts)
8981 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8982 return NULL_TREE;
8984 for (i = 0; i < nelts; i++)
8986 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8987 need_ctor = true;
8988 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8991 if (need_ctor)
8993 vec<constructor_elt, va_gc> *v;
8994 vec_alloc (v, nelts);
8995 for (i = 0; i < nelts; i++)
8996 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8997 return build_constructor (type, v);
8999 else
9000 return build_vector (type, &elts[2 * nelts]);
9003 /* Try to fold a pointer difference of type TYPE two address expressions of
9004 array references AREF0 and AREF1 using location LOC. Return a
9005 simplified expression for the difference or NULL_TREE. */
9007 static tree
9008 fold_addr_of_array_ref_difference (location_t loc, tree type,
9009 tree aref0, tree aref1)
9011 tree base0 = TREE_OPERAND (aref0, 0);
9012 tree base1 = TREE_OPERAND (aref1, 0);
9013 tree base_offset = build_int_cst (type, 0);
9015 /* If the bases are array references as well, recurse. If the bases
9016 are pointer indirections compute the difference of the pointers.
9017 If the bases are equal, we are set. */
9018 if ((TREE_CODE (base0) == ARRAY_REF
9019 && TREE_CODE (base1) == ARRAY_REF
9020 && (base_offset
9021 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9022 || (INDIRECT_REF_P (base0)
9023 && INDIRECT_REF_P (base1)
9024 && (base_offset
9025 = fold_binary_loc (loc, MINUS_EXPR, type,
9026 fold_convert (type, TREE_OPERAND (base0, 0)),
9027 fold_convert (type,
9028 TREE_OPERAND (base1, 0)))))
9029 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9031 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9032 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9033 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9034 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9035 return fold_build2_loc (loc, PLUS_EXPR, type,
9036 base_offset,
9037 fold_build2_loc (loc, MULT_EXPR, type,
9038 diff, esz));
9040 return NULL_TREE;
9043 /* If the real or vector real constant CST of type TYPE has an exact
9044 inverse, return it, else return NULL. */
9046 tree
9047 exact_inverse (tree type, tree cst)
9049 REAL_VALUE_TYPE r;
9050 tree unit_type, *elts;
9051 machine_mode mode;
9052 unsigned vec_nelts, i;
9054 switch (TREE_CODE (cst))
9056 case REAL_CST:
9057 r = TREE_REAL_CST (cst);
9059 if (exact_real_inverse (TYPE_MODE (type), &r))
9060 return build_real (type, r);
9062 return NULL_TREE;
9064 case VECTOR_CST:
9065 vec_nelts = VECTOR_CST_NELTS (cst);
9066 elts = XALLOCAVEC (tree, vec_nelts);
9067 unit_type = TREE_TYPE (type);
9068 mode = TYPE_MODE (unit_type);
9070 for (i = 0; i < vec_nelts; i++)
9072 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9073 if (!exact_real_inverse (mode, &r))
9074 return NULL_TREE;
9075 elts[i] = build_real (unit_type, r);
9078 return build_vector (type, elts);
9080 default:
9081 return NULL_TREE;
9085 /* Mask out the tz least significant bits of X of type TYPE where
9086 tz is the number of trailing zeroes in Y. */
9087 static wide_int
9088 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9090 int tz = wi::ctz (y);
9091 if (tz > 0)
9092 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9093 return x;
9096 /* Return true when T is an address and is known to be nonzero.
9097 For floating point we further ensure that T is not denormal.
9098 Similar logic is present in nonzero_address in rtlanal.h.
9100 If the return value is based on the assumption that signed overflow
9101 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9102 change *STRICT_OVERFLOW_P. */
9104 static bool
9105 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9107 tree type = TREE_TYPE (t);
9108 enum tree_code code;
9110 /* Doing something useful for floating point would need more work. */
9111 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9112 return false;
9114 code = TREE_CODE (t);
9115 switch (TREE_CODE_CLASS (code))
9117 case tcc_unary:
9118 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9119 strict_overflow_p);
9120 case tcc_binary:
9121 case tcc_comparison:
9122 return tree_binary_nonzero_warnv_p (code, type,
9123 TREE_OPERAND (t, 0),
9124 TREE_OPERAND (t, 1),
9125 strict_overflow_p);
9126 case tcc_constant:
9127 case tcc_declaration:
9128 case tcc_reference:
9129 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9131 default:
9132 break;
9135 switch (code)
9137 case TRUTH_NOT_EXPR:
9138 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9139 strict_overflow_p);
9141 case TRUTH_AND_EXPR:
9142 case TRUTH_OR_EXPR:
9143 case TRUTH_XOR_EXPR:
9144 return tree_binary_nonzero_warnv_p (code, type,
9145 TREE_OPERAND (t, 0),
9146 TREE_OPERAND (t, 1),
9147 strict_overflow_p);
9149 case COND_EXPR:
9150 case CONSTRUCTOR:
9151 case OBJ_TYPE_REF:
9152 case ASSERT_EXPR:
9153 case ADDR_EXPR:
9154 case WITH_SIZE_EXPR:
9155 case SSA_NAME:
9156 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9158 case COMPOUND_EXPR:
9159 case MODIFY_EXPR:
9160 case BIND_EXPR:
9161 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9162 strict_overflow_p);
9164 case SAVE_EXPR:
9165 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9166 strict_overflow_p);
9168 case CALL_EXPR:
9170 tree fndecl = get_callee_fndecl (t);
9171 if (!fndecl) return false;
9172 if (flag_delete_null_pointer_checks && !flag_check_new
9173 && DECL_IS_OPERATOR_NEW (fndecl)
9174 && !TREE_NOTHROW (fndecl))
9175 return true;
9176 if (flag_delete_null_pointer_checks
9177 && lookup_attribute ("returns_nonnull",
9178 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9179 return true;
9180 return alloca_call_p (t);
9183 default:
9184 break;
9186 return false;
9189 /* Return true when T is an address and is known to be nonzero.
9190 Handle warnings about undefined signed overflow. */
9192 static bool
9193 tree_expr_nonzero_p (tree t)
9195 bool ret, strict_overflow_p;
9197 strict_overflow_p = false;
9198 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9199 if (strict_overflow_p)
9200 fold_overflow_warning (("assuming signed overflow does not occur when "
9201 "determining that expression is always "
9202 "non-zero"),
9203 WARN_STRICT_OVERFLOW_MISC);
9204 return ret;
9207 /* Return true if T is known not to be equal to an integer W. */
9209 bool
9210 expr_not_equal_to (tree t, const wide_int &w)
9212 wide_int min, max, nz;
9213 value_range_type rtype;
9214 switch (TREE_CODE (t))
9216 case INTEGER_CST:
9217 return wi::ne_p (t, w);
9219 case SSA_NAME:
9220 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9221 return false;
9222 rtype = get_range_info (t, &min, &max);
9223 if (rtype == VR_RANGE)
9225 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9226 return true;
9227 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9228 return true;
9230 else if (rtype == VR_ANTI_RANGE
9231 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9232 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9233 return true;
9234 /* If T has some known zero bits and W has any of those bits set,
9235 then T is known not to be equal to W. */
9236 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9237 TYPE_PRECISION (TREE_TYPE (t))), 0))
9238 return true;
9239 return false;
9241 default:
9242 return false;
9246 /* Fold a binary expression of code CODE and type TYPE with operands
9247 OP0 and OP1. LOC is the location of the resulting expression.
9248 Return the folded expression if folding is successful. Otherwise,
9249 return NULL_TREE. */
9251 tree
9252 fold_binary_loc (location_t loc,
9253 enum tree_code code, tree type, tree op0, tree op1)
9255 enum tree_code_class kind = TREE_CODE_CLASS (code);
9256 tree arg0, arg1, tem;
9257 tree t1 = NULL_TREE;
9258 bool strict_overflow_p;
9259 unsigned int prec;
9261 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9262 && TREE_CODE_LENGTH (code) == 2
9263 && op0 != NULL_TREE
9264 && op1 != NULL_TREE);
9266 arg0 = op0;
9267 arg1 = op1;
9269 /* Strip any conversions that don't change the mode. This is
9270 safe for every expression, except for a comparison expression
9271 because its signedness is derived from its operands. So, in
9272 the latter case, only strip conversions that don't change the
9273 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9274 preserved.
9276 Note that this is done as an internal manipulation within the
9277 constant folder, in order to find the simplest representation
9278 of the arguments so that their form can be studied. In any
9279 cases, the appropriate type conversions should be put back in
9280 the tree that will get out of the constant folder. */
9282 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9284 STRIP_SIGN_NOPS (arg0);
9285 STRIP_SIGN_NOPS (arg1);
9287 else
9289 STRIP_NOPS (arg0);
9290 STRIP_NOPS (arg1);
9293 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9294 constant but we can't do arithmetic on them. */
9295 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9297 tem = const_binop (code, type, arg0, arg1);
9298 if (tem != NULL_TREE)
9300 if (TREE_TYPE (tem) != type)
9301 tem = fold_convert_loc (loc, type, tem);
9302 return tem;
9306 /* If this is a commutative operation, and ARG0 is a constant, move it
9307 to ARG1 to reduce the number of tests below. */
9308 if (commutative_tree_code (code)
9309 && tree_swap_operands_p (arg0, arg1, true))
9310 return fold_build2_loc (loc, code, type, op1, op0);
9312 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9313 to ARG1 to reduce the number of tests below. */
9314 if (kind == tcc_comparison
9315 && tree_swap_operands_p (arg0, arg1, true))
9316 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9318 tem = generic_simplify (loc, code, type, op0, op1);
9319 if (tem)
9320 return tem;
9322 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9324 First check for cases where an arithmetic operation is applied to a
9325 compound, conditional, or comparison operation. Push the arithmetic
9326 operation inside the compound or conditional to see if any folding
9327 can then be done. Convert comparison to conditional for this purpose.
9328 The also optimizes non-constant cases that used to be done in
9329 expand_expr.
9331 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9332 one of the operands is a comparison and the other is a comparison, a
9333 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9334 code below would make the expression more complex. Change it to a
9335 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9336 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9338 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9339 || code == EQ_EXPR || code == NE_EXPR)
9340 && TREE_CODE (type) != VECTOR_TYPE
9341 && ((truth_value_p (TREE_CODE (arg0))
9342 && (truth_value_p (TREE_CODE (arg1))
9343 || (TREE_CODE (arg1) == BIT_AND_EXPR
9344 && integer_onep (TREE_OPERAND (arg1, 1)))))
9345 || (truth_value_p (TREE_CODE (arg1))
9346 && (truth_value_p (TREE_CODE (arg0))
9347 || (TREE_CODE (arg0) == BIT_AND_EXPR
9348 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9350 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9351 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9352 : TRUTH_XOR_EXPR,
9353 boolean_type_node,
9354 fold_convert_loc (loc, boolean_type_node, arg0),
9355 fold_convert_loc (loc, boolean_type_node, arg1));
9357 if (code == EQ_EXPR)
9358 tem = invert_truthvalue_loc (loc, tem);
9360 return fold_convert_loc (loc, type, tem);
9363 if (TREE_CODE_CLASS (code) == tcc_binary
9364 || TREE_CODE_CLASS (code) == tcc_comparison)
9366 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9368 tem = fold_build2_loc (loc, code, type,
9369 fold_convert_loc (loc, TREE_TYPE (op0),
9370 TREE_OPERAND (arg0, 1)), op1);
9371 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9372 tem);
9374 if (TREE_CODE (arg1) == COMPOUND_EXPR
9375 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9377 tem = fold_build2_loc (loc, code, type, op0,
9378 fold_convert_loc (loc, TREE_TYPE (op1),
9379 TREE_OPERAND (arg1, 1)));
9380 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9381 tem);
9384 if (TREE_CODE (arg0) == COND_EXPR
9385 || TREE_CODE (arg0) == VEC_COND_EXPR
9386 || COMPARISON_CLASS_P (arg0))
9388 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9389 arg0, arg1,
9390 /*cond_first_p=*/1);
9391 if (tem != NULL_TREE)
9392 return tem;
9395 if (TREE_CODE (arg1) == COND_EXPR
9396 || TREE_CODE (arg1) == VEC_COND_EXPR
9397 || COMPARISON_CLASS_P (arg1))
9399 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9400 arg1, arg0,
9401 /*cond_first_p=*/0);
9402 if (tem != NULL_TREE)
9403 return tem;
9407 switch (code)
9409 case MEM_REF:
9410 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9411 if (TREE_CODE (arg0) == ADDR_EXPR
9412 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9414 tree iref = TREE_OPERAND (arg0, 0);
9415 return fold_build2 (MEM_REF, type,
9416 TREE_OPERAND (iref, 0),
9417 int_const_binop (PLUS_EXPR, arg1,
9418 TREE_OPERAND (iref, 1)));
9421 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9422 if (TREE_CODE (arg0) == ADDR_EXPR
9423 && handled_component_p (TREE_OPERAND (arg0, 0)))
9425 tree base;
9426 HOST_WIDE_INT coffset;
9427 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9428 &coffset);
9429 if (!base)
9430 return NULL_TREE;
9431 return fold_build2 (MEM_REF, type,
9432 build_fold_addr_expr (base),
9433 int_const_binop (PLUS_EXPR, arg1,
9434 size_int (coffset)));
9437 return NULL_TREE;
9439 case POINTER_PLUS_EXPR:
9440 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9441 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9442 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9443 return fold_convert_loc (loc, type,
9444 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9445 fold_convert_loc (loc, sizetype,
9446 arg1),
9447 fold_convert_loc (loc, sizetype,
9448 arg0)));
9450 return NULL_TREE;
9452 case PLUS_EXPR:
9453 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9455 /* X + (X / CST) * -CST is X % CST. */
9456 if (TREE_CODE (arg1) == MULT_EXPR
9457 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9458 && operand_equal_p (arg0,
9459 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9461 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9462 tree cst1 = TREE_OPERAND (arg1, 1);
9463 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9464 cst1, cst0);
9465 if (sum && integer_zerop (sum))
9466 return fold_convert_loc (loc, type,
9467 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9468 TREE_TYPE (arg0), arg0,
9469 cst0));
9473 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9474 one. Make sure the type is not saturating and has the signedness of
9475 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9476 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9477 if ((TREE_CODE (arg0) == MULT_EXPR
9478 || TREE_CODE (arg1) == MULT_EXPR)
9479 && !TYPE_SATURATING (type)
9480 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9481 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9482 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9484 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9485 if (tem)
9486 return tem;
9489 if (! FLOAT_TYPE_P (type))
9491 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9492 (plus (plus (mult) (mult)) (foo)) so that we can
9493 take advantage of the factoring cases below. */
9494 if (ANY_INTEGRAL_TYPE_P (type)
9495 && TYPE_OVERFLOW_WRAPS (type)
9496 && (((TREE_CODE (arg0) == PLUS_EXPR
9497 || TREE_CODE (arg0) == MINUS_EXPR)
9498 && TREE_CODE (arg1) == MULT_EXPR)
9499 || ((TREE_CODE (arg1) == PLUS_EXPR
9500 || TREE_CODE (arg1) == MINUS_EXPR)
9501 && TREE_CODE (arg0) == MULT_EXPR)))
9503 tree parg0, parg1, parg, marg;
9504 enum tree_code pcode;
9506 if (TREE_CODE (arg1) == MULT_EXPR)
9507 parg = arg0, marg = arg1;
9508 else
9509 parg = arg1, marg = arg0;
9510 pcode = TREE_CODE (parg);
9511 parg0 = TREE_OPERAND (parg, 0);
9512 parg1 = TREE_OPERAND (parg, 1);
9513 STRIP_NOPS (parg0);
9514 STRIP_NOPS (parg1);
9516 if (TREE_CODE (parg0) == MULT_EXPR
9517 && TREE_CODE (parg1) != MULT_EXPR)
9518 return fold_build2_loc (loc, pcode, type,
9519 fold_build2_loc (loc, PLUS_EXPR, type,
9520 fold_convert_loc (loc, type,
9521 parg0),
9522 fold_convert_loc (loc, type,
9523 marg)),
9524 fold_convert_loc (loc, type, parg1));
9525 if (TREE_CODE (parg0) != MULT_EXPR
9526 && TREE_CODE (parg1) == MULT_EXPR)
9527 return
9528 fold_build2_loc (loc, PLUS_EXPR, type,
9529 fold_convert_loc (loc, type, parg0),
9530 fold_build2_loc (loc, pcode, type,
9531 fold_convert_loc (loc, type, marg),
9532 fold_convert_loc (loc, type,
9533 parg1)));
9536 else
9538 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9539 to __complex__ ( x, y ). This is not the same for SNaNs or
9540 if signed zeros are involved. */
9541 if (!HONOR_SNANS (element_mode (arg0))
9542 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9543 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9545 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9546 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9547 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9548 bool arg0rz = false, arg0iz = false;
9549 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9550 || (arg0i && (arg0iz = real_zerop (arg0i))))
9552 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9553 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9554 if (arg0rz && arg1i && real_zerop (arg1i))
9556 tree rp = arg1r ? arg1r
9557 : build1 (REALPART_EXPR, rtype, arg1);
9558 tree ip = arg0i ? arg0i
9559 : build1 (IMAGPART_EXPR, rtype, arg0);
9560 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9562 else if (arg0iz && arg1r && real_zerop (arg1r))
9564 tree rp = arg0r ? arg0r
9565 : build1 (REALPART_EXPR, rtype, arg0);
9566 tree ip = arg1i ? arg1i
9567 : build1 (IMAGPART_EXPR, rtype, arg1);
9568 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9573 if (flag_unsafe_math_optimizations
9574 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9575 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9576 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9577 return tem;
9579 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9580 We associate floats only if the user has specified
9581 -fassociative-math. */
9582 if (flag_associative_math
9583 && TREE_CODE (arg1) == PLUS_EXPR
9584 && TREE_CODE (arg0) != MULT_EXPR)
9586 tree tree10 = TREE_OPERAND (arg1, 0);
9587 tree tree11 = TREE_OPERAND (arg1, 1);
9588 if (TREE_CODE (tree11) == MULT_EXPR
9589 && TREE_CODE (tree10) == MULT_EXPR)
9591 tree tree0;
9592 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9593 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9596 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9597 We associate floats only if the user has specified
9598 -fassociative-math. */
9599 if (flag_associative_math
9600 && TREE_CODE (arg0) == PLUS_EXPR
9601 && TREE_CODE (arg1) != MULT_EXPR)
9603 tree tree00 = TREE_OPERAND (arg0, 0);
9604 tree tree01 = TREE_OPERAND (arg0, 1);
9605 if (TREE_CODE (tree01) == MULT_EXPR
9606 && TREE_CODE (tree00) == MULT_EXPR)
9608 tree tree0;
9609 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9610 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9615 bit_rotate:
9616 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9617 is a rotate of A by C1 bits. */
9618 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9619 is a rotate of A by B bits. */
9621 enum tree_code code0, code1;
9622 tree rtype;
9623 code0 = TREE_CODE (arg0);
9624 code1 = TREE_CODE (arg1);
9625 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9626 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9627 && operand_equal_p (TREE_OPERAND (arg0, 0),
9628 TREE_OPERAND (arg1, 0), 0)
9629 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9630 TYPE_UNSIGNED (rtype))
9631 /* Only create rotates in complete modes. Other cases are not
9632 expanded properly. */
9633 && (element_precision (rtype)
9634 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9636 tree tree01, tree11;
9637 enum tree_code code01, code11;
9639 tree01 = TREE_OPERAND (arg0, 1);
9640 tree11 = TREE_OPERAND (arg1, 1);
9641 STRIP_NOPS (tree01);
9642 STRIP_NOPS (tree11);
9643 code01 = TREE_CODE (tree01);
9644 code11 = TREE_CODE (tree11);
9645 if (code01 == INTEGER_CST
9646 && code11 == INTEGER_CST
9647 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9648 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9650 tem = build2_loc (loc, LROTATE_EXPR,
9651 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9652 TREE_OPERAND (arg0, 0),
9653 code0 == LSHIFT_EXPR
9654 ? TREE_OPERAND (arg0, 1)
9655 : TREE_OPERAND (arg1, 1));
9656 return fold_convert_loc (loc, type, tem);
9658 else if (code11 == MINUS_EXPR)
9660 tree tree110, tree111;
9661 tree110 = TREE_OPERAND (tree11, 0);
9662 tree111 = TREE_OPERAND (tree11, 1);
9663 STRIP_NOPS (tree110);
9664 STRIP_NOPS (tree111);
9665 if (TREE_CODE (tree110) == INTEGER_CST
9666 && 0 == compare_tree_int (tree110,
9667 element_precision
9668 (TREE_TYPE (TREE_OPERAND
9669 (arg0, 0))))
9670 && operand_equal_p (tree01, tree111, 0))
9671 return
9672 fold_convert_loc (loc, type,
9673 build2 ((code0 == LSHIFT_EXPR
9674 ? LROTATE_EXPR
9675 : RROTATE_EXPR),
9676 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9677 TREE_OPERAND (arg0, 0),
9678 TREE_OPERAND (arg0, 1)));
9680 else if (code01 == MINUS_EXPR)
9682 tree tree010, tree011;
9683 tree010 = TREE_OPERAND (tree01, 0);
9684 tree011 = TREE_OPERAND (tree01, 1);
9685 STRIP_NOPS (tree010);
9686 STRIP_NOPS (tree011);
9687 if (TREE_CODE (tree010) == INTEGER_CST
9688 && 0 == compare_tree_int (tree010,
9689 element_precision
9690 (TREE_TYPE (TREE_OPERAND
9691 (arg0, 0))))
9692 && operand_equal_p (tree11, tree011, 0))
9693 return fold_convert_loc
9694 (loc, type,
9695 build2 ((code0 != LSHIFT_EXPR
9696 ? LROTATE_EXPR
9697 : RROTATE_EXPR),
9698 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9699 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9704 associate:
9705 /* In most languages, can't associate operations on floats through
9706 parentheses. Rather than remember where the parentheses were, we
9707 don't associate floats at all, unless the user has specified
9708 -fassociative-math.
9709 And, we need to make sure type is not saturating. */
9711 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9712 && !TYPE_SATURATING (type))
9714 tree var0, con0, lit0, minus_lit0;
9715 tree var1, con1, lit1, minus_lit1;
9716 tree atype = type;
9717 bool ok = true;
9719 /* Split both trees into variables, constants, and literals. Then
9720 associate each group together, the constants with literals,
9721 then the result with variables. This increases the chances of
9722 literals being recombined later and of generating relocatable
9723 expressions for the sum of a constant and literal. */
9724 var0 = split_tree (loc, arg0, type, code,
9725 &con0, &lit0, &minus_lit0, 0);
9726 var1 = split_tree (loc, arg1, type, code,
9727 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9729 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9730 if (code == MINUS_EXPR)
9731 code = PLUS_EXPR;
9733 /* With undefined overflow prefer doing association in a type
9734 which wraps on overflow, if that is one of the operand types. */
9735 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9736 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9738 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9739 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9740 atype = TREE_TYPE (arg0);
9741 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9742 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9743 atype = TREE_TYPE (arg1);
9744 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9747 /* With undefined overflow we can only associate constants with one
9748 variable, and constants whose association doesn't overflow. */
9749 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9750 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9752 if (var0 && var1)
9754 tree tmp0 = var0;
9755 tree tmp1 = var1;
9756 bool one_neg = false;
9758 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9760 tmp0 = TREE_OPERAND (tmp0, 0);
9761 one_neg = !one_neg;
9763 if (CONVERT_EXPR_P (tmp0)
9764 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9765 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9766 <= TYPE_PRECISION (atype)))
9767 tmp0 = TREE_OPERAND (tmp0, 0);
9768 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9770 tmp1 = TREE_OPERAND (tmp1, 0);
9771 one_neg = !one_neg;
9773 if (CONVERT_EXPR_P (tmp1)
9774 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9775 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9776 <= TYPE_PRECISION (atype)))
9777 tmp1 = TREE_OPERAND (tmp1, 0);
9778 /* The only case we can still associate with two variables
9779 is if they cancel out. */
9780 if (!one_neg
9781 || !operand_equal_p (tmp0, tmp1, 0))
9782 ok = false;
9786 /* Only do something if we found more than two objects. Otherwise,
9787 nothing has changed and we risk infinite recursion. */
9788 if (ok
9789 && (2 < ((var0 != 0) + (var1 != 0)
9790 + (con0 != 0) + (con1 != 0)
9791 + (lit0 != 0) + (lit1 != 0)
9792 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9794 bool any_overflows = false;
9795 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9796 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9797 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9798 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9799 var0 = associate_trees (loc, var0, var1, code, atype);
9800 con0 = associate_trees (loc, con0, con1, code, atype);
9801 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9802 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9803 code, atype);
9805 /* Preserve the MINUS_EXPR if the negative part of the literal is
9806 greater than the positive part. Otherwise, the multiplicative
9807 folding code (i.e extract_muldiv) may be fooled in case
9808 unsigned constants are subtracted, like in the following
9809 example: ((X*2 + 4) - 8U)/2. */
9810 if (minus_lit0 && lit0)
9812 if (TREE_CODE (lit0) == INTEGER_CST
9813 && TREE_CODE (minus_lit0) == INTEGER_CST
9814 && tree_int_cst_lt (lit0, minus_lit0))
9816 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9817 MINUS_EXPR, atype);
9818 lit0 = 0;
9820 else
9822 lit0 = associate_trees (loc, lit0, minus_lit0,
9823 MINUS_EXPR, atype);
9824 minus_lit0 = 0;
9828 /* Don't introduce overflows through reassociation. */
9829 if (!any_overflows
9830 && ((lit0 && TREE_OVERFLOW_P (lit0))
9831 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9832 return NULL_TREE;
9834 if (minus_lit0)
9836 if (con0 == 0)
9837 return
9838 fold_convert_loc (loc, type,
9839 associate_trees (loc, var0, minus_lit0,
9840 MINUS_EXPR, atype));
9841 else
9843 con0 = associate_trees (loc, con0, minus_lit0,
9844 MINUS_EXPR, atype);
9845 return
9846 fold_convert_loc (loc, type,
9847 associate_trees (loc, var0, con0,
9848 PLUS_EXPR, atype));
9852 con0 = associate_trees (loc, con0, lit0, code, atype);
9853 return
9854 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9855 code, atype));
9859 return NULL_TREE;
9861 case MINUS_EXPR:
9862 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9863 if (TREE_CODE (arg0) == NEGATE_EXPR
9864 && negate_expr_p (op1)
9865 && reorder_operands_p (arg0, arg1))
9866 return fold_build2_loc (loc, MINUS_EXPR, type,
9867 negate_expr (op1),
9868 fold_convert_loc (loc, type,
9869 TREE_OPERAND (arg0, 0)));
9871 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9872 __complex__ ( x, -y ). This is not the same for SNaNs or if
9873 signed zeros are involved. */
9874 if (!HONOR_SNANS (element_mode (arg0))
9875 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9876 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9878 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9879 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9880 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9881 bool arg0rz = false, arg0iz = false;
9882 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9883 || (arg0i && (arg0iz = real_zerop (arg0i))))
9885 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9886 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9887 if (arg0rz && arg1i && real_zerop (arg1i))
9889 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9890 arg1r ? arg1r
9891 : build1 (REALPART_EXPR, rtype, arg1));
9892 tree ip = arg0i ? arg0i
9893 : build1 (IMAGPART_EXPR, rtype, arg0);
9894 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9896 else if (arg0iz && arg1r && real_zerop (arg1r))
9898 tree rp = arg0r ? arg0r
9899 : build1 (REALPART_EXPR, rtype, arg0);
9900 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9901 arg1i ? arg1i
9902 : build1 (IMAGPART_EXPR, rtype, arg1));
9903 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9908 /* A - B -> A + (-B) if B is easily negatable. */
9909 if (negate_expr_p (op1)
9910 && ! TYPE_OVERFLOW_SANITIZED (type)
9911 && ((FLOAT_TYPE_P (type)
9912 /* Avoid this transformation if B is a positive REAL_CST. */
9913 && (TREE_CODE (op1) != REAL_CST
9914 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9915 || INTEGRAL_TYPE_P (type)))
9916 return fold_build2_loc (loc, PLUS_EXPR, type,
9917 fold_convert_loc (loc, type, arg0),
9918 negate_expr (op1));
9920 /* Fold &a[i] - &a[j] to i-j. */
9921 if (TREE_CODE (arg0) == ADDR_EXPR
9922 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9923 && TREE_CODE (arg1) == ADDR_EXPR
9924 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9926 tree tem = fold_addr_of_array_ref_difference (loc, type,
9927 TREE_OPERAND (arg0, 0),
9928 TREE_OPERAND (arg1, 0));
9929 if (tem)
9930 return tem;
9933 if (FLOAT_TYPE_P (type)
9934 && flag_unsafe_math_optimizations
9935 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9936 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9937 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9938 return tem;
9940 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9941 one. Make sure the type is not saturating and has the signedness of
9942 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9943 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9944 if ((TREE_CODE (arg0) == MULT_EXPR
9945 || TREE_CODE (arg1) == MULT_EXPR)
9946 && !TYPE_SATURATING (type)
9947 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9948 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9949 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9951 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9952 if (tem)
9953 return tem;
9956 goto associate;
9958 case MULT_EXPR:
9959 if (! FLOAT_TYPE_P (type))
9961 /* Transform x * -C into -x * C if x is easily negatable. */
9962 if (TREE_CODE (op1) == INTEGER_CST
9963 && tree_int_cst_sgn (op1) == -1
9964 && negate_expr_p (op0)
9965 && (tem = negate_expr (op1)) != op1
9966 && ! TREE_OVERFLOW (tem))
9967 return fold_build2_loc (loc, MULT_EXPR, type,
9968 fold_convert_loc (loc, type,
9969 negate_expr (op0)), tem);
9971 /* (A + A) * C -> A * 2 * C */
9972 if (TREE_CODE (arg0) == PLUS_EXPR
9973 && TREE_CODE (arg1) == INTEGER_CST
9974 && operand_equal_p (TREE_OPERAND (arg0, 0),
9975 TREE_OPERAND (arg0, 1), 0))
9976 return fold_build2_loc (loc, MULT_EXPR, type,
9977 omit_one_operand_loc (loc, type,
9978 TREE_OPERAND (arg0, 0),
9979 TREE_OPERAND (arg0, 1)),
9980 fold_build2_loc (loc, MULT_EXPR, type,
9981 build_int_cst (type, 2) , arg1));
9983 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9984 sign-changing only. */
9985 if (TREE_CODE (arg1) == INTEGER_CST
9986 && TREE_CODE (arg0) == EXACT_DIV_EXPR
9987 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
9988 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9990 strict_overflow_p = false;
9991 if (TREE_CODE (arg1) == INTEGER_CST
9992 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9993 &strict_overflow_p)))
9995 if (strict_overflow_p)
9996 fold_overflow_warning (("assuming signed overflow does not "
9997 "occur when simplifying "
9998 "multiplication"),
9999 WARN_STRICT_OVERFLOW_MISC);
10000 return fold_convert_loc (loc, type, tem);
10003 /* Optimize z * conj(z) for integer complex numbers. */
10004 if (TREE_CODE (arg0) == CONJ_EXPR
10005 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10006 return fold_mult_zconjz (loc, type, arg1);
10007 if (TREE_CODE (arg1) == CONJ_EXPR
10008 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10009 return fold_mult_zconjz (loc, type, arg0);
10011 else
10013 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10014 This is not the same for NaNs or if signed zeros are
10015 involved. */
10016 if (!HONOR_NANS (arg0)
10017 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10018 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10019 && TREE_CODE (arg1) == COMPLEX_CST
10020 && real_zerop (TREE_REALPART (arg1)))
10022 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10023 if (real_onep (TREE_IMAGPART (arg1)))
10024 return
10025 fold_build2_loc (loc, COMPLEX_EXPR, type,
10026 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10027 rtype, arg0)),
10028 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10029 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10030 return
10031 fold_build2_loc (loc, COMPLEX_EXPR, type,
10032 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10033 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10034 rtype, arg0)));
10037 /* Optimize z * conj(z) for floating point complex numbers.
10038 Guarded by flag_unsafe_math_optimizations as non-finite
10039 imaginary components don't produce scalar results. */
10040 if (flag_unsafe_math_optimizations
10041 && TREE_CODE (arg0) == CONJ_EXPR
10042 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10043 return fold_mult_zconjz (loc, type, arg1);
10044 if (flag_unsafe_math_optimizations
10045 && TREE_CODE (arg1) == CONJ_EXPR
10046 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10047 return fold_mult_zconjz (loc, type, arg0);
10049 if (flag_unsafe_math_optimizations)
10052 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
10053 if (!in_gimple_form
10054 && optimize
10055 && operand_equal_p (arg0, arg1, 0))
10057 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10059 if (powfn)
10061 tree arg = build_real (type, dconst2);
10062 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10067 goto associate;
10069 case BIT_IOR_EXPR:
10070 /* Canonicalize (X & C1) | C2. */
10071 if (TREE_CODE (arg0) == BIT_AND_EXPR
10072 && TREE_CODE (arg1) == INTEGER_CST
10073 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10075 int width = TYPE_PRECISION (type), w;
10076 wide_int c1 = TREE_OPERAND (arg0, 1);
10077 wide_int c2 = arg1;
10079 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10080 if ((c1 & c2) == c1)
10081 return omit_one_operand_loc (loc, type, arg1,
10082 TREE_OPERAND (arg0, 0));
10084 wide_int msk = wi::mask (width, false,
10085 TYPE_PRECISION (TREE_TYPE (arg1)));
10087 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10088 if (msk.and_not (c1 | c2) == 0)
10089 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10090 TREE_OPERAND (arg0, 0), arg1);
10092 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10093 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10094 mode which allows further optimizations. */
10095 c1 &= msk;
10096 c2 &= msk;
10097 wide_int c3 = c1.and_not (c2);
10098 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10100 wide_int mask = wi::mask (w, false,
10101 TYPE_PRECISION (type));
10102 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
10104 c3 = mask;
10105 break;
10109 if (c3 != c1)
10110 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10111 fold_build2_loc (loc, BIT_AND_EXPR, type,
10112 TREE_OPERAND (arg0, 0),
10113 wide_int_to_tree (type,
10114 c3)),
10115 arg1);
10118 /* See if this can be simplified into a rotate first. If that
10119 is unsuccessful continue in the association code. */
10120 goto bit_rotate;
10122 case BIT_XOR_EXPR:
10123 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10124 if (TREE_CODE (arg0) == BIT_AND_EXPR
10125 && INTEGRAL_TYPE_P (type)
10126 && integer_onep (TREE_OPERAND (arg0, 1))
10127 && integer_onep (arg1))
10128 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10129 build_zero_cst (TREE_TYPE (arg0)));
10131 /* See if this can be simplified into a rotate first. If that
10132 is unsuccessful continue in the association code. */
10133 goto bit_rotate;
10135 case BIT_AND_EXPR:
10136 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10137 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10138 && INTEGRAL_TYPE_P (type)
10139 && integer_onep (TREE_OPERAND (arg0, 1))
10140 && integer_onep (arg1))
10142 tree tem2;
10143 tem = TREE_OPERAND (arg0, 0);
10144 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10145 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10146 tem, tem2);
10147 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10148 build_zero_cst (TREE_TYPE (tem)));
10150 /* Fold ~X & 1 as (X & 1) == 0. */
10151 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10152 && INTEGRAL_TYPE_P (type)
10153 && integer_onep (arg1))
10155 tree tem2;
10156 tem = TREE_OPERAND (arg0, 0);
10157 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10158 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10159 tem, tem2);
10160 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10161 build_zero_cst (TREE_TYPE (tem)));
10163 /* Fold !X & 1 as X == 0. */
10164 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10165 && integer_onep (arg1))
10167 tem = TREE_OPERAND (arg0, 0);
10168 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10169 build_zero_cst (TREE_TYPE (tem)));
10172 /* Fold (X ^ Y) & Y as ~X & Y. */
10173 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10174 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10176 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10177 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10178 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10179 fold_convert_loc (loc, type, arg1));
10181 /* Fold (X ^ Y) & X as ~Y & X. */
10182 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10183 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10184 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10186 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10187 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10188 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10189 fold_convert_loc (loc, type, arg1));
10191 /* Fold X & (X ^ Y) as X & ~Y. */
10192 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10193 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10195 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10196 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10197 fold_convert_loc (loc, type, arg0),
10198 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10200 /* Fold X & (Y ^ X) as ~Y & X. */
10201 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10202 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10203 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10205 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10206 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10207 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10208 fold_convert_loc (loc, type, arg0));
10211 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10212 multiple of 1 << CST. */
10213 if (TREE_CODE (arg1) == INTEGER_CST)
10215 wide_int cst1 = arg1;
10216 wide_int ncst1 = -cst1;
10217 if ((cst1 & ncst1) == ncst1
10218 && multiple_of_p (type, arg0,
10219 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10220 return fold_convert_loc (loc, type, arg0);
10223 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10224 bits from CST2. */
10225 if (TREE_CODE (arg1) == INTEGER_CST
10226 && TREE_CODE (arg0) == MULT_EXPR
10227 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10229 wide_int warg1 = arg1;
10230 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10232 if (masked == 0)
10233 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10234 arg0, arg1);
10235 else if (masked != warg1)
10237 /* Avoid the transform if arg1 is a mask of some
10238 mode which allows further optimizations. */
10239 int pop = wi::popcount (warg1);
10240 if (!(pop >= BITS_PER_UNIT
10241 && exact_log2 (pop) != -1
10242 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10243 return fold_build2_loc (loc, code, type, op0,
10244 wide_int_to_tree (type, masked));
10248 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10249 ((A & N) + B) & M -> (A + B) & M
10250 Similarly if (N & M) == 0,
10251 ((A | N) + B) & M -> (A + B) & M
10252 and for - instead of + (or unary - instead of +)
10253 and/or ^ instead of |.
10254 If B is constant and (B & M) == 0, fold into A & M. */
10255 if (TREE_CODE (arg1) == INTEGER_CST)
10257 wide_int cst1 = arg1;
10258 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10259 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10260 && (TREE_CODE (arg0) == PLUS_EXPR
10261 || TREE_CODE (arg0) == MINUS_EXPR
10262 || TREE_CODE (arg0) == NEGATE_EXPR)
10263 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10264 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10266 tree pmop[2];
10267 int which = 0;
10268 wide_int cst0;
10270 /* Now we know that arg0 is (C + D) or (C - D) or
10271 -C and arg1 (M) is == (1LL << cst) - 1.
10272 Store C into PMOP[0] and D into PMOP[1]. */
10273 pmop[0] = TREE_OPERAND (arg0, 0);
10274 pmop[1] = NULL;
10275 if (TREE_CODE (arg0) != NEGATE_EXPR)
10277 pmop[1] = TREE_OPERAND (arg0, 1);
10278 which = 1;
10281 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10282 which = -1;
10284 for (; which >= 0; which--)
10285 switch (TREE_CODE (pmop[which]))
10287 case BIT_AND_EXPR:
10288 case BIT_IOR_EXPR:
10289 case BIT_XOR_EXPR:
10290 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10291 != INTEGER_CST)
10292 break;
10293 cst0 = TREE_OPERAND (pmop[which], 1);
10294 cst0 &= cst1;
10295 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10297 if (cst0 != cst1)
10298 break;
10300 else if (cst0 != 0)
10301 break;
10302 /* If C or D is of the form (A & N) where
10303 (N & M) == M, or of the form (A | N) or
10304 (A ^ N) where (N & M) == 0, replace it with A. */
10305 pmop[which] = TREE_OPERAND (pmop[which], 0);
10306 break;
10307 case INTEGER_CST:
10308 /* If C or D is a N where (N & M) == 0, it can be
10309 omitted (assumed 0). */
10310 if ((TREE_CODE (arg0) == PLUS_EXPR
10311 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10312 && (cst1 & pmop[which]) == 0)
10313 pmop[which] = NULL;
10314 break;
10315 default:
10316 break;
10319 /* Only build anything new if we optimized one or both arguments
10320 above. */
10321 if (pmop[0] != TREE_OPERAND (arg0, 0)
10322 || (TREE_CODE (arg0) != NEGATE_EXPR
10323 && pmop[1] != TREE_OPERAND (arg0, 1)))
10325 tree utype = TREE_TYPE (arg0);
10326 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10328 /* Perform the operations in a type that has defined
10329 overflow behavior. */
10330 utype = unsigned_type_for (TREE_TYPE (arg0));
10331 if (pmop[0] != NULL)
10332 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10333 if (pmop[1] != NULL)
10334 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10337 if (TREE_CODE (arg0) == NEGATE_EXPR)
10338 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10339 else if (TREE_CODE (arg0) == PLUS_EXPR)
10341 if (pmop[0] != NULL && pmop[1] != NULL)
10342 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10343 pmop[0], pmop[1]);
10344 else if (pmop[0] != NULL)
10345 tem = pmop[0];
10346 else if (pmop[1] != NULL)
10347 tem = pmop[1];
10348 else
10349 return build_int_cst (type, 0);
10351 else if (pmop[0] == NULL)
10352 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10353 else
10354 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10355 pmop[0], pmop[1]);
10356 /* TEM is now the new binary +, - or unary - replacement. */
10357 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10358 fold_convert_loc (loc, utype, arg1));
10359 return fold_convert_loc (loc, type, tem);
10364 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10365 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10366 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10368 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10370 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10371 if (mask == -1)
10372 return
10373 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10376 goto associate;
10378 case RDIV_EXPR:
10379 /* Don't touch a floating-point divide by zero unless the mode
10380 of the constant can represent infinity. */
10381 if (TREE_CODE (arg1) == REAL_CST
10382 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10383 && real_zerop (arg1))
10384 return NULL_TREE;
10386 /* (-A) / (-B) -> A / B */
10387 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10388 return fold_build2_loc (loc, RDIV_EXPR, type,
10389 TREE_OPERAND (arg0, 0),
10390 negate_expr (arg1));
10391 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10392 return fold_build2_loc (loc, RDIV_EXPR, type,
10393 negate_expr (arg0),
10394 TREE_OPERAND (arg1, 0));
10395 return NULL_TREE;
10397 case TRUNC_DIV_EXPR:
10398 /* Fall through */
10400 case FLOOR_DIV_EXPR:
10401 /* Simplify A / (B << N) where A and B are positive and B is
10402 a power of 2, to A >> (N + log2(B)). */
10403 strict_overflow_p = false;
10404 if (TREE_CODE (arg1) == LSHIFT_EXPR
10405 && (TYPE_UNSIGNED (type)
10406 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10408 tree sval = TREE_OPERAND (arg1, 0);
10409 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10411 tree sh_cnt = TREE_OPERAND (arg1, 1);
10412 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10413 wi::exact_log2 (sval));
10415 if (strict_overflow_p)
10416 fold_overflow_warning (("assuming signed overflow does not "
10417 "occur when simplifying A / (B << N)"),
10418 WARN_STRICT_OVERFLOW_MISC);
10420 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10421 sh_cnt, pow2);
10422 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10423 fold_convert_loc (loc, type, arg0), sh_cnt);
10427 /* Fall through */
10429 case ROUND_DIV_EXPR:
10430 case CEIL_DIV_EXPR:
10431 case EXACT_DIV_EXPR:
10432 if (integer_zerop (arg1))
10433 return NULL_TREE;
10435 /* Convert -A / -B to A / B when the type is signed and overflow is
10436 undefined. */
10437 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10438 && TREE_CODE (arg0) == NEGATE_EXPR
10439 && negate_expr_p (op1))
10441 if (INTEGRAL_TYPE_P (type))
10442 fold_overflow_warning (("assuming signed overflow does not occur "
10443 "when distributing negation across "
10444 "division"),
10445 WARN_STRICT_OVERFLOW_MISC);
10446 return fold_build2_loc (loc, code, type,
10447 fold_convert_loc (loc, type,
10448 TREE_OPERAND (arg0, 0)),
10449 negate_expr (op1));
10451 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10452 && TREE_CODE (arg1) == NEGATE_EXPR
10453 && negate_expr_p (op0))
10455 if (INTEGRAL_TYPE_P (type))
10456 fold_overflow_warning (("assuming signed overflow does not occur "
10457 "when distributing negation across "
10458 "division"),
10459 WARN_STRICT_OVERFLOW_MISC);
10460 return fold_build2_loc (loc, code, type,
10461 negate_expr (op0),
10462 fold_convert_loc (loc, type,
10463 TREE_OPERAND (arg1, 0)));
10466 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10467 operation, EXACT_DIV_EXPR.
10469 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10470 At one time others generated faster code, it's not clear if they do
10471 after the last round to changes to the DIV code in expmed.c. */
10472 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10473 && multiple_of_p (type, arg0, arg1))
10474 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10475 fold_convert (type, arg0),
10476 fold_convert (type, arg1));
10478 strict_overflow_p = false;
10479 if (TREE_CODE (arg1) == INTEGER_CST
10480 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10481 &strict_overflow_p)))
10483 if (strict_overflow_p)
10484 fold_overflow_warning (("assuming signed overflow does not occur "
10485 "when simplifying division"),
10486 WARN_STRICT_OVERFLOW_MISC);
10487 return fold_convert_loc (loc, type, tem);
10490 return NULL_TREE;
10492 case CEIL_MOD_EXPR:
10493 case FLOOR_MOD_EXPR:
10494 case ROUND_MOD_EXPR:
10495 case TRUNC_MOD_EXPR:
10496 strict_overflow_p = false;
10497 if (TREE_CODE (arg1) == INTEGER_CST
10498 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10499 &strict_overflow_p)))
10501 if (strict_overflow_p)
10502 fold_overflow_warning (("assuming signed overflow does not occur "
10503 "when simplifying modulus"),
10504 WARN_STRICT_OVERFLOW_MISC);
10505 return fold_convert_loc (loc, type, tem);
10508 return NULL_TREE;
10510 case LROTATE_EXPR:
10511 case RROTATE_EXPR:
10512 case RSHIFT_EXPR:
10513 case LSHIFT_EXPR:
10514 /* Since negative shift count is not well-defined,
10515 don't try to compute it in the compiler. */
10516 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10517 return NULL_TREE;
10519 prec = element_precision (type);
10521 /* If we have a rotate of a bit operation with the rotate count and
10522 the second operand of the bit operation both constant,
10523 permute the two operations. */
10524 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10525 && (TREE_CODE (arg0) == BIT_AND_EXPR
10526 || TREE_CODE (arg0) == BIT_IOR_EXPR
10527 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10528 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10530 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10531 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10532 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10533 fold_build2_loc (loc, code, type,
10534 arg00, arg1),
10535 fold_build2_loc (loc, code, type,
10536 arg01, arg1));
10539 /* Two consecutive rotates adding up to the some integer
10540 multiple of the precision of the type can be ignored. */
10541 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10542 && TREE_CODE (arg0) == RROTATE_EXPR
10543 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10544 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10545 prec) == 0)
10546 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10548 return NULL_TREE;
10550 case MIN_EXPR:
10551 case MAX_EXPR:
10552 goto associate;
10554 case TRUTH_ANDIF_EXPR:
10555 /* Note that the operands of this must be ints
10556 and their values must be 0 or 1.
10557 ("true" is a fixed value perhaps depending on the language.) */
10558 /* If first arg is constant zero, return it. */
10559 if (integer_zerop (arg0))
10560 return fold_convert_loc (loc, type, arg0);
10561 case TRUTH_AND_EXPR:
10562 /* If either arg is constant true, drop it. */
10563 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10564 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10565 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10566 /* Preserve sequence points. */
10567 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10568 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10569 /* If second arg is constant zero, result is zero, but first arg
10570 must be evaluated. */
10571 if (integer_zerop (arg1))
10572 return omit_one_operand_loc (loc, type, arg1, arg0);
10573 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10574 case will be handled here. */
10575 if (integer_zerop (arg0))
10576 return omit_one_operand_loc (loc, type, arg0, arg1);
10578 /* !X && X is always false. */
10579 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10580 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10581 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10582 /* X && !X is always false. */
10583 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10584 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10585 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10587 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10588 means A >= Y && A != MAX, but in this case we know that
10589 A < X <= MAX. */
10591 if (!TREE_SIDE_EFFECTS (arg0)
10592 && !TREE_SIDE_EFFECTS (arg1))
10594 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10595 if (tem && !operand_equal_p (tem, arg0, 0))
10596 return fold_build2_loc (loc, code, type, tem, arg1);
10598 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10599 if (tem && !operand_equal_p (tem, arg1, 0))
10600 return fold_build2_loc (loc, code, type, arg0, tem);
10603 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10604 != NULL_TREE)
10605 return tem;
10607 return NULL_TREE;
10609 case TRUTH_ORIF_EXPR:
10610 /* Note that the operands of this must be ints
10611 and their values must be 0 or true.
10612 ("true" is a fixed value perhaps depending on the language.) */
10613 /* If first arg is constant true, return it. */
10614 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10615 return fold_convert_loc (loc, type, arg0);
10616 case TRUTH_OR_EXPR:
10617 /* If either arg is constant zero, drop it. */
10618 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10619 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10620 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10621 /* Preserve sequence points. */
10622 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10623 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10624 /* If second arg is constant true, result is true, but we must
10625 evaluate first arg. */
10626 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10627 return omit_one_operand_loc (loc, type, arg1, arg0);
10628 /* Likewise for first arg, but note this only occurs here for
10629 TRUTH_OR_EXPR. */
10630 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10631 return omit_one_operand_loc (loc, type, arg0, arg1);
10633 /* !X || X is always true. */
10634 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10635 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10636 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10637 /* X || !X is always true. */
10638 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10639 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10640 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10642 /* (X && !Y) || (!X && Y) is X ^ Y */
10643 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10644 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10646 tree a0, a1, l0, l1, n0, n1;
10648 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10649 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10651 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10652 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10654 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10655 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10657 if ((operand_equal_p (n0, a0, 0)
10658 && operand_equal_p (n1, a1, 0))
10659 || (operand_equal_p (n0, a1, 0)
10660 && operand_equal_p (n1, a0, 0)))
10661 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10664 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10665 != NULL_TREE)
10666 return tem;
10668 return NULL_TREE;
10670 case TRUTH_XOR_EXPR:
10671 /* If the second arg is constant zero, drop it. */
10672 if (integer_zerop (arg1))
10673 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10674 /* If the second arg is constant true, this is a logical inversion. */
10675 if (integer_onep (arg1))
10677 tem = invert_truthvalue_loc (loc, arg0);
10678 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10680 /* Identical arguments cancel to zero. */
10681 if (operand_equal_p (arg0, arg1, 0))
10682 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10684 /* !X ^ X is always true. */
10685 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10686 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10687 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10689 /* X ^ !X is always true. */
10690 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10691 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10692 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10694 return NULL_TREE;
10696 case EQ_EXPR:
10697 case NE_EXPR:
10698 STRIP_NOPS (arg0);
10699 STRIP_NOPS (arg1);
10701 tem = fold_comparison (loc, code, type, op0, op1);
10702 if (tem != NULL_TREE)
10703 return tem;
10705 /* bool_var != 1 becomes !bool_var. */
10706 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10707 && code == NE_EXPR)
10708 return fold_convert_loc (loc, type,
10709 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10710 TREE_TYPE (arg0), arg0));
10712 /* bool_var == 0 becomes !bool_var. */
10713 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10714 && code == EQ_EXPR)
10715 return fold_convert_loc (loc, type,
10716 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10717 TREE_TYPE (arg0), arg0));
10719 /* !exp != 0 becomes !exp */
10720 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10721 && code == NE_EXPR)
10722 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10724 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10725 if ((TREE_CODE (arg0) == PLUS_EXPR
10726 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10727 || TREE_CODE (arg0) == MINUS_EXPR)
10728 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10729 0)),
10730 arg1, 0)
10731 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10732 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10734 tree val = TREE_OPERAND (arg0, 1);
10735 val = fold_build2_loc (loc, code, type, val,
10736 build_int_cst (TREE_TYPE (val), 0));
10737 return omit_two_operands_loc (loc, type, val,
10738 TREE_OPERAND (arg0, 0), arg1);
10741 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10742 if ((TREE_CODE (arg1) == PLUS_EXPR
10743 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10744 || TREE_CODE (arg1) == MINUS_EXPR)
10745 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10746 0)),
10747 arg0, 0)
10748 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10749 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10751 tree val = TREE_OPERAND (arg1, 1);
10752 val = fold_build2_loc (loc, code, type, val,
10753 build_int_cst (TREE_TYPE (val), 0));
10754 return omit_two_operands_loc (loc, type, val,
10755 TREE_OPERAND (arg1, 0), arg0);
10758 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10759 if (TREE_CODE (arg0) == MINUS_EXPR
10760 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10761 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10762 1)),
10763 arg1, 0)
10764 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10765 return omit_two_operands_loc (loc, type,
10766 code == NE_EXPR
10767 ? boolean_true_node : boolean_false_node,
10768 TREE_OPERAND (arg0, 1), arg1);
10770 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10771 if (TREE_CODE (arg1) == MINUS_EXPR
10772 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10773 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10774 1)),
10775 arg0, 0)
10776 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10777 return omit_two_operands_loc (loc, type,
10778 code == NE_EXPR
10779 ? boolean_true_node : boolean_false_node,
10780 TREE_OPERAND (arg1, 1), arg0);
10782 /* If this is an EQ or NE comparison with zero and ARG0 is
10783 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10784 two operations, but the latter can be done in one less insn
10785 on machines that have only two-operand insns or on which a
10786 constant cannot be the first operand. */
10787 if (TREE_CODE (arg0) == BIT_AND_EXPR
10788 && integer_zerop (arg1))
10790 tree arg00 = TREE_OPERAND (arg0, 0);
10791 tree arg01 = TREE_OPERAND (arg0, 1);
10792 if (TREE_CODE (arg00) == LSHIFT_EXPR
10793 && integer_onep (TREE_OPERAND (arg00, 0)))
10795 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10796 arg01, TREE_OPERAND (arg00, 1));
10797 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10798 build_int_cst (TREE_TYPE (arg0), 1));
10799 return fold_build2_loc (loc, code, type,
10800 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10801 arg1);
10803 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10804 && integer_onep (TREE_OPERAND (arg01, 0)))
10806 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10807 arg00, TREE_OPERAND (arg01, 1));
10808 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10809 build_int_cst (TREE_TYPE (arg0), 1));
10810 return fold_build2_loc (loc, code, type,
10811 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10812 arg1);
10816 /* If this is an NE or EQ comparison of zero against the result of a
10817 signed MOD operation whose second operand is a power of 2, make
10818 the MOD operation unsigned since it is simpler and equivalent. */
10819 if (integer_zerop (arg1)
10820 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10821 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10822 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10823 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10824 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10825 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10827 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10828 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10829 fold_convert_loc (loc, newtype,
10830 TREE_OPERAND (arg0, 0)),
10831 fold_convert_loc (loc, newtype,
10832 TREE_OPERAND (arg0, 1)));
10834 return fold_build2_loc (loc, code, type, newmod,
10835 fold_convert_loc (loc, newtype, arg1));
10838 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10839 C1 is a valid shift constant, and C2 is a power of two, i.e.
10840 a single bit. */
10841 if (TREE_CODE (arg0) == BIT_AND_EXPR
10842 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10843 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10844 == INTEGER_CST
10845 && integer_pow2p (TREE_OPERAND (arg0, 1))
10846 && integer_zerop (arg1))
10848 tree itype = TREE_TYPE (arg0);
10849 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10850 prec = TYPE_PRECISION (itype);
10852 /* Check for a valid shift count. */
10853 if (wi::ltu_p (arg001, prec))
10855 tree arg01 = TREE_OPERAND (arg0, 1);
10856 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10857 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10858 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10859 can be rewritten as (X & (C2 << C1)) != 0. */
10860 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10862 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10863 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10864 return fold_build2_loc (loc, code, type, tem,
10865 fold_convert_loc (loc, itype, arg1));
10867 /* Otherwise, for signed (arithmetic) shifts,
10868 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10869 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10870 else if (!TYPE_UNSIGNED (itype))
10871 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10872 arg000, build_int_cst (itype, 0));
10873 /* Otherwise, of unsigned (logical) shifts,
10874 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10875 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10876 else
10877 return omit_one_operand_loc (loc, type,
10878 code == EQ_EXPR ? integer_one_node
10879 : integer_zero_node,
10880 arg000);
10884 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10885 Similarly for NE_EXPR. */
10886 if (TREE_CODE (arg0) == BIT_AND_EXPR
10887 && TREE_CODE (arg1) == INTEGER_CST
10888 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10890 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10891 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10892 TREE_OPERAND (arg0, 1));
10893 tree dandnotc
10894 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10895 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10896 notc);
10897 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10898 if (integer_nonzerop (dandnotc))
10899 return omit_one_operand_loc (loc, type, rslt, arg0);
10902 /* If this is a comparison of a field, we may be able to simplify it. */
10903 if ((TREE_CODE (arg0) == COMPONENT_REF
10904 || TREE_CODE (arg0) == BIT_FIELD_REF)
10905 /* Handle the constant case even without -O
10906 to make sure the warnings are given. */
10907 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10909 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10910 if (t1)
10911 return t1;
10914 /* Optimize comparisons of strlen vs zero to a compare of the
10915 first character of the string vs zero. To wit,
10916 strlen(ptr) == 0 => *ptr == 0
10917 strlen(ptr) != 0 => *ptr != 0
10918 Other cases should reduce to one of these two (or a constant)
10919 due to the return value of strlen being unsigned. */
10920 if (TREE_CODE (arg0) == CALL_EXPR
10921 && integer_zerop (arg1))
10923 tree fndecl = get_callee_fndecl (arg0);
10925 if (fndecl
10926 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10927 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10928 && call_expr_nargs (arg0) == 1
10929 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10931 tree iref = build_fold_indirect_ref_loc (loc,
10932 CALL_EXPR_ARG (arg0, 0));
10933 return fold_build2_loc (loc, code, type, iref,
10934 build_int_cst (TREE_TYPE (iref), 0));
10938 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10939 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10940 if (TREE_CODE (arg0) == RSHIFT_EXPR
10941 && integer_zerop (arg1)
10942 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10944 tree arg00 = TREE_OPERAND (arg0, 0);
10945 tree arg01 = TREE_OPERAND (arg0, 1);
10946 tree itype = TREE_TYPE (arg00);
10947 if (wi::eq_p (arg01, element_precision (itype) - 1))
10949 if (TYPE_UNSIGNED (itype))
10951 itype = signed_type_for (itype);
10952 arg00 = fold_convert_loc (loc, itype, arg00);
10954 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10955 type, arg00, build_zero_cst (itype));
10959 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10960 (X & C) == 0 when C is a single bit. */
10961 if (TREE_CODE (arg0) == BIT_AND_EXPR
10962 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10963 && integer_zerop (arg1)
10964 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10966 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10967 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10968 TREE_OPERAND (arg0, 1));
10969 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10970 type, tem,
10971 fold_convert_loc (loc, TREE_TYPE (arg0),
10972 arg1));
10975 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10976 constant C is a power of two, i.e. a single bit. */
10977 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10978 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10979 && integer_zerop (arg1)
10980 && integer_pow2p (TREE_OPERAND (arg0, 1))
10981 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10982 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10984 tree arg00 = TREE_OPERAND (arg0, 0);
10985 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10986 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10989 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10990 when is C is a power of two, i.e. a single bit. */
10991 if (TREE_CODE (arg0) == BIT_AND_EXPR
10992 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10993 && integer_zerop (arg1)
10994 && integer_pow2p (TREE_OPERAND (arg0, 1))
10995 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10996 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10998 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10999 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
11000 arg000, TREE_OPERAND (arg0, 1));
11001 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11002 tem, build_int_cst (TREE_TYPE (tem), 0));
11005 if (integer_zerop (arg1)
11006 && tree_expr_nonzero_p (arg0))
11008 tree res = constant_boolean_node (code==NE_EXPR, type);
11009 return omit_one_operand_loc (loc, type, res, arg0);
11012 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
11013 if (TREE_CODE (arg0) == BIT_AND_EXPR
11014 && TREE_CODE (arg1) == BIT_AND_EXPR)
11016 tree arg00 = TREE_OPERAND (arg0, 0);
11017 tree arg01 = TREE_OPERAND (arg0, 1);
11018 tree arg10 = TREE_OPERAND (arg1, 0);
11019 tree arg11 = TREE_OPERAND (arg1, 1);
11020 tree itype = TREE_TYPE (arg0);
11022 if (operand_equal_p (arg01, arg11, 0))
11023 return fold_build2_loc (loc, code, type,
11024 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11025 fold_build2_loc (loc,
11026 BIT_XOR_EXPR, itype,
11027 arg00, arg10),
11028 arg01),
11029 build_zero_cst (itype));
11031 if (operand_equal_p (arg01, arg10, 0))
11032 return fold_build2_loc (loc, code, type,
11033 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11034 fold_build2_loc (loc,
11035 BIT_XOR_EXPR, itype,
11036 arg00, arg11),
11037 arg01),
11038 build_zero_cst (itype));
11040 if (operand_equal_p (arg00, arg11, 0))
11041 return fold_build2_loc (loc, code, type,
11042 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11043 fold_build2_loc (loc,
11044 BIT_XOR_EXPR, itype,
11045 arg01, arg10),
11046 arg00),
11047 build_zero_cst (itype));
11049 if (operand_equal_p (arg00, arg10, 0))
11050 return fold_build2_loc (loc, code, type,
11051 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11052 fold_build2_loc (loc,
11053 BIT_XOR_EXPR, itype,
11054 arg01, arg11),
11055 arg00),
11056 build_zero_cst (itype));
11059 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11060 && TREE_CODE (arg1) == BIT_XOR_EXPR)
11062 tree arg00 = TREE_OPERAND (arg0, 0);
11063 tree arg01 = TREE_OPERAND (arg0, 1);
11064 tree arg10 = TREE_OPERAND (arg1, 0);
11065 tree arg11 = TREE_OPERAND (arg1, 1);
11066 tree itype = TREE_TYPE (arg0);
11068 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
11069 operand_equal_p guarantees no side-effects so we don't need
11070 to use omit_one_operand on Z. */
11071 if (operand_equal_p (arg01, arg11, 0))
11072 return fold_build2_loc (loc, code, type, arg00,
11073 fold_convert_loc (loc, TREE_TYPE (arg00),
11074 arg10));
11075 if (operand_equal_p (arg01, arg10, 0))
11076 return fold_build2_loc (loc, code, type, arg00,
11077 fold_convert_loc (loc, TREE_TYPE (arg00),
11078 arg11));
11079 if (operand_equal_p (arg00, arg11, 0))
11080 return fold_build2_loc (loc, code, type, arg01,
11081 fold_convert_loc (loc, TREE_TYPE (arg01),
11082 arg10));
11083 if (operand_equal_p (arg00, arg10, 0))
11084 return fold_build2_loc (loc, code, type, arg01,
11085 fold_convert_loc (loc, TREE_TYPE (arg01),
11086 arg11));
11088 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
11089 if (TREE_CODE (arg01) == INTEGER_CST
11090 && TREE_CODE (arg11) == INTEGER_CST)
11092 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
11093 fold_convert_loc (loc, itype, arg11));
11094 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
11095 return fold_build2_loc (loc, code, type, tem,
11096 fold_convert_loc (loc, itype, arg10));
11100 /* Attempt to simplify equality/inequality comparisons of complex
11101 values. Only lower the comparison if the result is known or
11102 can be simplified to a single scalar comparison. */
11103 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11104 || TREE_CODE (arg0) == COMPLEX_CST)
11105 && (TREE_CODE (arg1) == COMPLEX_EXPR
11106 || TREE_CODE (arg1) == COMPLEX_CST))
11108 tree real0, imag0, real1, imag1;
11109 tree rcond, icond;
11111 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11113 real0 = TREE_OPERAND (arg0, 0);
11114 imag0 = TREE_OPERAND (arg0, 1);
11116 else
11118 real0 = TREE_REALPART (arg0);
11119 imag0 = TREE_IMAGPART (arg0);
11122 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11124 real1 = TREE_OPERAND (arg1, 0);
11125 imag1 = TREE_OPERAND (arg1, 1);
11127 else
11129 real1 = TREE_REALPART (arg1);
11130 imag1 = TREE_IMAGPART (arg1);
11133 rcond = fold_binary_loc (loc, code, type, real0, real1);
11134 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11136 if (integer_zerop (rcond))
11138 if (code == EQ_EXPR)
11139 return omit_two_operands_loc (loc, type, boolean_false_node,
11140 imag0, imag1);
11141 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11143 else
11145 if (code == NE_EXPR)
11146 return omit_two_operands_loc (loc, type, boolean_true_node,
11147 imag0, imag1);
11148 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11152 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11153 if (icond && TREE_CODE (icond) == INTEGER_CST)
11155 if (integer_zerop (icond))
11157 if (code == EQ_EXPR)
11158 return omit_two_operands_loc (loc, type, boolean_false_node,
11159 real0, real1);
11160 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11162 else
11164 if (code == NE_EXPR)
11165 return omit_two_operands_loc (loc, type, boolean_true_node,
11166 real0, real1);
11167 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11172 return NULL_TREE;
11174 case LT_EXPR:
11175 case GT_EXPR:
11176 case LE_EXPR:
11177 case GE_EXPR:
11178 tem = fold_comparison (loc, code, type, op0, op1);
11179 if (tem != NULL_TREE)
11180 return tem;
11182 /* Transform comparisons of the form X +- C CMP X. */
11183 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11184 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11185 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11186 && !HONOR_SNANS (arg0))
11187 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11188 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11190 tree arg01 = TREE_OPERAND (arg0, 1);
11191 enum tree_code code0 = TREE_CODE (arg0);
11192 int is_positive;
11194 if (TREE_CODE (arg01) == REAL_CST)
11195 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11196 else
11197 is_positive = tree_int_cst_sgn (arg01);
11199 /* (X - c) > X becomes false. */
11200 if (code == GT_EXPR
11201 && ((code0 == MINUS_EXPR && is_positive >= 0)
11202 || (code0 == PLUS_EXPR && is_positive <= 0)))
11204 if (TREE_CODE (arg01) == INTEGER_CST
11205 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11206 fold_overflow_warning (("assuming signed overflow does not "
11207 "occur when assuming that (X - c) > X "
11208 "is always false"),
11209 WARN_STRICT_OVERFLOW_ALL);
11210 return constant_boolean_node (0, type);
11213 /* Likewise (X + c) < X becomes false. */
11214 if (code == LT_EXPR
11215 && ((code0 == PLUS_EXPR && is_positive >= 0)
11216 || (code0 == MINUS_EXPR && is_positive <= 0)))
11218 if (TREE_CODE (arg01) == INTEGER_CST
11219 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11220 fold_overflow_warning (("assuming signed overflow does not "
11221 "occur when assuming that "
11222 "(X + c) < X is always false"),
11223 WARN_STRICT_OVERFLOW_ALL);
11224 return constant_boolean_node (0, type);
11227 /* Convert (X - c) <= X to true. */
11228 if (!HONOR_NANS (arg1)
11229 && code == LE_EXPR
11230 && ((code0 == MINUS_EXPR && is_positive >= 0)
11231 || (code0 == PLUS_EXPR && is_positive <= 0)))
11233 if (TREE_CODE (arg01) == INTEGER_CST
11234 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11235 fold_overflow_warning (("assuming signed overflow does not "
11236 "occur when assuming that "
11237 "(X - c) <= X is always true"),
11238 WARN_STRICT_OVERFLOW_ALL);
11239 return constant_boolean_node (1, type);
11242 /* Convert (X + c) >= X to true. */
11243 if (!HONOR_NANS (arg1)
11244 && code == GE_EXPR
11245 && ((code0 == PLUS_EXPR && is_positive >= 0)
11246 || (code0 == MINUS_EXPR && is_positive <= 0)))
11248 if (TREE_CODE (arg01) == INTEGER_CST
11249 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11250 fold_overflow_warning (("assuming signed overflow does not "
11251 "occur when assuming that "
11252 "(X + c) >= X is always true"),
11253 WARN_STRICT_OVERFLOW_ALL);
11254 return constant_boolean_node (1, type);
11257 if (TREE_CODE (arg01) == INTEGER_CST)
11259 /* Convert X + c > X and X - c < X to true for integers. */
11260 if (code == GT_EXPR
11261 && ((code0 == PLUS_EXPR && is_positive > 0)
11262 || (code0 == MINUS_EXPR && is_positive < 0)))
11264 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11265 fold_overflow_warning (("assuming signed overflow does "
11266 "not occur when assuming that "
11267 "(X + c) > X is always true"),
11268 WARN_STRICT_OVERFLOW_ALL);
11269 return constant_boolean_node (1, type);
11272 if (code == LT_EXPR
11273 && ((code0 == MINUS_EXPR && is_positive > 0)
11274 || (code0 == PLUS_EXPR && is_positive < 0)))
11276 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11277 fold_overflow_warning (("assuming signed overflow does "
11278 "not occur when assuming that "
11279 "(X - c) < X is always true"),
11280 WARN_STRICT_OVERFLOW_ALL);
11281 return constant_boolean_node (1, type);
11284 /* Convert X + c <= X and X - c >= X to false for integers. */
11285 if (code == LE_EXPR
11286 && ((code0 == PLUS_EXPR && is_positive > 0)
11287 || (code0 == MINUS_EXPR && is_positive < 0)))
11289 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11290 fold_overflow_warning (("assuming signed overflow does "
11291 "not occur when assuming that "
11292 "(X + c) <= X is always false"),
11293 WARN_STRICT_OVERFLOW_ALL);
11294 return constant_boolean_node (0, type);
11297 if (code == GE_EXPR
11298 && ((code0 == MINUS_EXPR && is_positive > 0)
11299 || (code0 == PLUS_EXPR && is_positive < 0)))
11301 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11302 fold_overflow_warning (("assuming signed overflow does "
11303 "not occur when assuming that "
11304 "(X - c) >= X is always false"),
11305 WARN_STRICT_OVERFLOW_ALL);
11306 return constant_boolean_node (0, type);
11311 /* If we are comparing an ABS_EXPR with a constant, we can
11312 convert all the cases into explicit comparisons, but they may
11313 well not be faster than doing the ABS and one comparison.
11314 But ABS (X) <= C is a range comparison, which becomes a subtraction
11315 and a comparison, and is probably faster. */
11316 if (code == LE_EXPR
11317 && TREE_CODE (arg1) == INTEGER_CST
11318 && TREE_CODE (arg0) == ABS_EXPR
11319 && ! TREE_SIDE_EFFECTS (arg0)
11320 && (0 != (tem = negate_expr (arg1)))
11321 && TREE_CODE (tem) == INTEGER_CST
11322 && !TREE_OVERFLOW (tem))
11323 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11324 build2 (GE_EXPR, type,
11325 TREE_OPERAND (arg0, 0), tem),
11326 build2 (LE_EXPR, type,
11327 TREE_OPERAND (arg0, 0), arg1));
11329 /* Convert ABS_EXPR<x> >= 0 to true. */
11330 strict_overflow_p = false;
11331 if (code == GE_EXPR
11332 && (integer_zerop (arg1)
11333 || (! HONOR_NANS (arg0)
11334 && real_zerop (arg1)))
11335 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11337 if (strict_overflow_p)
11338 fold_overflow_warning (("assuming signed overflow does not occur "
11339 "when simplifying comparison of "
11340 "absolute value and zero"),
11341 WARN_STRICT_OVERFLOW_CONDITIONAL);
11342 return omit_one_operand_loc (loc, type,
11343 constant_boolean_node (true, type),
11344 arg0);
11347 /* Convert ABS_EXPR<x> < 0 to false. */
11348 strict_overflow_p = false;
11349 if (code == LT_EXPR
11350 && (integer_zerop (arg1) || real_zerop (arg1))
11351 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11353 if (strict_overflow_p)
11354 fold_overflow_warning (("assuming signed overflow does not occur "
11355 "when simplifying comparison of "
11356 "absolute value and zero"),
11357 WARN_STRICT_OVERFLOW_CONDITIONAL);
11358 return omit_one_operand_loc (loc, type,
11359 constant_boolean_node (false, type),
11360 arg0);
11363 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11364 and similarly for >= into !=. */
11365 if ((code == LT_EXPR || code == GE_EXPR)
11366 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11367 && TREE_CODE (arg1) == LSHIFT_EXPR
11368 && integer_onep (TREE_OPERAND (arg1, 0)))
11369 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11370 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11371 TREE_OPERAND (arg1, 1)),
11372 build_zero_cst (TREE_TYPE (arg0)));
11374 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11375 otherwise Y might be >= # of bits in X's type and thus e.g.
11376 (unsigned char) (1 << Y) for Y 15 might be 0.
11377 If the cast is widening, then 1 << Y should have unsigned type,
11378 otherwise if Y is number of bits in the signed shift type minus 1,
11379 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11380 31 might be 0xffffffff80000000. */
11381 if ((code == LT_EXPR || code == GE_EXPR)
11382 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11383 && CONVERT_EXPR_P (arg1)
11384 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11385 && (element_precision (TREE_TYPE (arg1))
11386 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11387 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11388 || (element_precision (TREE_TYPE (arg1))
11389 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11390 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11392 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11393 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11394 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11395 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11396 build_zero_cst (TREE_TYPE (arg0)));
11399 return NULL_TREE;
11401 case UNORDERED_EXPR:
11402 case ORDERED_EXPR:
11403 case UNLT_EXPR:
11404 case UNLE_EXPR:
11405 case UNGT_EXPR:
11406 case UNGE_EXPR:
11407 case UNEQ_EXPR:
11408 case LTGT_EXPR:
11409 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11411 tree targ0 = strip_float_extensions (arg0);
11412 tree targ1 = strip_float_extensions (arg1);
11413 tree newtype = TREE_TYPE (targ0);
11415 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11416 newtype = TREE_TYPE (targ1);
11418 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11419 return fold_build2_loc (loc, code, type,
11420 fold_convert_loc (loc, newtype, targ0),
11421 fold_convert_loc (loc, newtype, targ1));
11424 return NULL_TREE;
11426 case COMPOUND_EXPR:
11427 /* When pedantic, a compound expression can be neither an lvalue
11428 nor an integer constant expression. */
11429 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11430 return NULL_TREE;
11431 /* Don't let (0, 0) be null pointer constant. */
11432 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11433 : fold_convert_loc (loc, type, arg1);
11434 return pedantic_non_lvalue_loc (loc, tem);
11436 case ASSERT_EXPR:
11437 /* An ASSERT_EXPR should never be passed to fold_binary. */
11438 gcc_unreachable ();
11440 default:
11441 return NULL_TREE;
11442 } /* switch (code) */
11445 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11446 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11447 of GOTO_EXPR. */
11449 static tree
11450 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11452 switch (TREE_CODE (*tp))
11454 case LABEL_EXPR:
11455 return *tp;
11457 case GOTO_EXPR:
11458 *walk_subtrees = 0;
11460 /* ... fall through ... */
11462 default:
11463 return NULL_TREE;
11467 /* Return whether the sub-tree ST contains a label which is accessible from
11468 outside the sub-tree. */
11470 static bool
11471 contains_label_p (tree st)
11473 return
11474 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11477 /* Fold a ternary expression of code CODE and type TYPE with operands
11478 OP0, OP1, and OP2. Return the folded expression if folding is
11479 successful. Otherwise, return NULL_TREE. */
11481 tree
11482 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11483 tree op0, tree op1, tree op2)
11485 tree tem;
11486 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11487 enum tree_code_class kind = TREE_CODE_CLASS (code);
11489 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11490 && TREE_CODE_LENGTH (code) == 3);
11492 /* If this is a commutative operation, and OP0 is a constant, move it
11493 to OP1 to reduce the number of tests below. */
11494 if (commutative_ternary_tree_code (code)
11495 && tree_swap_operands_p (op0, op1, true))
11496 return fold_build3_loc (loc, code, type, op1, op0, op2);
11498 tem = generic_simplify (loc, code, type, op0, op1, op2);
11499 if (tem)
11500 return tem;
11502 /* Strip any conversions that don't change the mode. This is safe
11503 for every expression, except for a comparison expression because
11504 its signedness is derived from its operands. So, in the latter
11505 case, only strip conversions that don't change the signedness.
11507 Note that this is done as an internal manipulation within the
11508 constant folder, in order to find the simplest representation of
11509 the arguments so that their form can be studied. In any cases,
11510 the appropriate type conversions should be put back in the tree
11511 that will get out of the constant folder. */
11512 if (op0)
11514 arg0 = op0;
11515 STRIP_NOPS (arg0);
11518 if (op1)
11520 arg1 = op1;
11521 STRIP_NOPS (arg1);
11524 if (op2)
11526 arg2 = op2;
11527 STRIP_NOPS (arg2);
11530 switch (code)
11532 case COMPONENT_REF:
11533 if (TREE_CODE (arg0) == CONSTRUCTOR
11534 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11536 unsigned HOST_WIDE_INT idx;
11537 tree field, value;
11538 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11539 if (field == arg1)
11540 return value;
11542 return NULL_TREE;
11544 case COND_EXPR:
11545 case VEC_COND_EXPR:
11546 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11547 so all simple results must be passed through pedantic_non_lvalue. */
11548 if (TREE_CODE (arg0) == INTEGER_CST)
11550 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11551 tem = integer_zerop (arg0) ? op2 : op1;
11552 /* Only optimize constant conditions when the selected branch
11553 has the same type as the COND_EXPR. This avoids optimizing
11554 away "c ? x : throw", where the throw has a void type.
11555 Avoid throwing away that operand which contains label. */
11556 if ((!TREE_SIDE_EFFECTS (unused_op)
11557 || !contains_label_p (unused_op))
11558 && (! VOID_TYPE_P (TREE_TYPE (tem))
11559 || VOID_TYPE_P (type)))
11560 return pedantic_non_lvalue_loc (loc, tem);
11561 return NULL_TREE;
11563 else if (TREE_CODE (arg0) == VECTOR_CST)
11565 if ((TREE_CODE (arg1) == VECTOR_CST
11566 || TREE_CODE (arg1) == CONSTRUCTOR)
11567 && (TREE_CODE (arg2) == VECTOR_CST
11568 || TREE_CODE (arg2) == CONSTRUCTOR))
11570 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11571 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11572 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11573 for (i = 0; i < nelts; i++)
11575 tree val = VECTOR_CST_ELT (arg0, i);
11576 if (integer_all_onesp (val))
11577 sel[i] = i;
11578 else if (integer_zerop (val))
11579 sel[i] = nelts + i;
11580 else /* Currently unreachable. */
11581 return NULL_TREE;
11583 tree t = fold_vec_perm (type, arg1, arg2, sel);
11584 if (t != NULL_TREE)
11585 return t;
11589 /* If we have A op B ? A : C, we may be able to convert this to a
11590 simpler expression, depending on the operation and the values
11591 of B and C. Signed zeros prevent all of these transformations,
11592 for reasons given above each one.
11594 Also try swapping the arguments and inverting the conditional. */
11595 if (COMPARISON_CLASS_P (arg0)
11596 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11597 arg1, TREE_OPERAND (arg0, 1))
11598 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11600 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11601 if (tem)
11602 return tem;
11605 if (COMPARISON_CLASS_P (arg0)
11606 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11607 op2,
11608 TREE_OPERAND (arg0, 1))
11609 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11611 location_t loc0 = expr_location_or (arg0, loc);
11612 tem = fold_invert_truthvalue (loc0, arg0);
11613 if (tem && COMPARISON_CLASS_P (tem))
11615 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11616 if (tem)
11617 return tem;
11621 /* If the second operand is simpler than the third, swap them
11622 since that produces better jump optimization results. */
11623 if (truth_value_p (TREE_CODE (arg0))
11624 && tree_swap_operands_p (op1, op2, false))
11626 location_t loc0 = expr_location_or (arg0, loc);
11627 /* See if this can be inverted. If it can't, possibly because
11628 it was a floating-point inequality comparison, don't do
11629 anything. */
11630 tem = fold_invert_truthvalue (loc0, arg0);
11631 if (tem)
11632 return fold_build3_loc (loc, code, type, tem, op2, op1);
11635 /* Convert A ? 1 : 0 to simply A. */
11636 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11637 : (integer_onep (op1)
11638 && !VECTOR_TYPE_P (type)))
11639 && integer_zerop (op2)
11640 /* If we try to convert OP0 to our type, the
11641 call to fold will try to move the conversion inside
11642 a COND, which will recurse. In that case, the COND_EXPR
11643 is probably the best choice, so leave it alone. */
11644 && type == TREE_TYPE (arg0))
11645 return pedantic_non_lvalue_loc (loc, arg0);
11647 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11648 over COND_EXPR in cases such as floating point comparisons. */
11649 if (integer_zerop (op1)
11650 && code == COND_EXPR
11651 && integer_onep (op2)
11652 && !VECTOR_TYPE_P (type)
11653 && truth_value_p (TREE_CODE (arg0)))
11654 return pedantic_non_lvalue_loc (loc,
11655 fold_convert_loc (loc, type,
11656 invert_truthvalue_loc (loc,
11657 arg0)));
11659 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11660 if (TREE_CODE (arg0) == LT_EXPR
11661 && integer_zerop (TREE_OPERAND (arg0, 1))
11662 && integer_zerop (op2)
11663 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11665 /* sign_bit_p looks through both zero and sign extensions,
11666 but for this optimization only sign extensions are
11667 usable. */
11668 tree tem2 = TREE_OPERAND (arg0, 0);
11669 while (tem != tem2)
11671 if (TREE_CODE (tem2) != NOP_EXPR
11672 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11674 tem = NULL_TREE;
11675 break;
11677 tem2 = TREE_OPERAND (tem2, 0);
11679 /* sign_bit_p only checks ARG1 bits within A's precision.
11680 If <sign bit of A> has wider type than A, bits outside
11681 of A's precision in <sign bit of A> need to be checked.
11682 If they are all 0, this optimization needs to be done
11683 in unsigned A's type, if they are all 1 in signed A's type,
11684 otherwise this can't be done. */
11685 if (tem
11686 && TYPE_PRECISION (TREE_TYPE (tem))
11687 < TYPE_PRECISION (TREE_TYPE (arg1))
11688 && TYPE_PRECISION (TREE_TYPE (tem))
11689 < TYPE_PRECISION (type))
11691 int inner_width, outer_width;
11692 tree tem_type;
11694 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11695 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11696 if (outer_width > TYPE_PRECISION (type))
11697 outer_width = TYPE_PRECISION (type);
11699 wide_int mask = wi::shifted_mask
11700 (inner_width, outer_width - inner_width, false,
11701 TYPE_PRECISION (TREE_TYPE (arg1)));
11703 wide_int common = mask & arg1;
11704 if (common == mask)
11706 tem_type = signed_type_for (TREE_TYPE (tem));
11707 tem = fold_convert_loc (loc, tem_type, tem);
11709 else if (common == 0)
11711 tem_type = unsigned_type_for (TREE_TYPE (tem));
11712 tem = fold_convert_loc (loc, tem_type, tem);
11714 else
11715 tem = NULL;
11718 if (tem)
11719 return
11720 fold_convert_loc (loc, type,
11721 fold_build2_loc (loc, BIT_AND_EXPR,
11722 TREE_TYPE (tem), tem,
11723 fold_convert_loc (loc,
11724 TREE_TYPE (tem),
11725 arg1)));
11728 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11729 already handled above. */
11730 if (TREE_CODE (arg0) == BIT_AND_EXPR
11731 && integer_onep (TREE_OPERAND (arg0, 1))
11732 && integer_zerop (op2)
11733 && integer_pow2p (arg1))
11735 tree tem = TREE_OPERAND (arg0, 0);
11736 STRIP_NOPS (tem);
11737 if (TREE_CODE (tem) == RSHIFT_EXPR
11738 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11739 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11740 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11741 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11742 TREE_OPERAND (tem, 0), arg1);
11745 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11746 is probably obsolete because the first operand should be a
11747 truth value (that's why we have the two cases above), but let's
11748 leave it in until we can confirm this for all front-ends. */
11749 if (integer_zerop (op2)
11750 && TREE_CODE (arg0) == NE_EXPR
11751 && integer_zerop (TREE_OPERAND (arg0, 1))
11752 && integer_pow2p (arg1)
11753 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11754 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11755 arg1, OEP_ONLY_CONST))
11756 return pedantic_non_lvalue_loc (loc,
11757 fold_convert_loc (loc, type,
11758 TREE_OPERAND (arg0, 0)));
11760 /* Disable the transformations below for vectors, since
11761 fold_binary_op_with_conditional_arg may undo them immediately,
11762 yielding an infinite loop. */
11763 if (code == VEC_COND_EXPR)
11764 return NULL_TREE;
11766 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11767 if (integer_zerop (op2)
11768 && truth_value_p (TREE_CODE (arg0))
11769 && truth_value_p (TREE_CODE (arg1))
11770 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11771 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11772 : TRUTH_ANDIF_EXPR,
11773 type, fold_convert_loc (loc, type, arg0), arg1);
11775 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11776 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11777 && truth_value_p (TREE_CODE (arg0))
11778 && truth_value_p (TREE_CODE (arg1))
11779 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11781 location_t loc0 = expr_location_or (arg0, loc);
11782 /* Only perform transformation if ARG0 is easily inverted. */
11783 tem = fold_invert_truthvalue (loc0, arg0);
11784 if (tem)
11785 return fold_build2_loc (loc, code == VEC_COND_EXPR
11786 ? BIT_IOR_EXPR
11787 : TRUTH_ORIF_EXPR,
11788 type, fold_convert_loc (loc, type, tem),
11789 arg1);
11792 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11793 if (integer_zerop (arg1)
11794 && truth_value_p (TREE_CODE (arg0))
11795 && truth_value_p (TREE_CODE (op2))
11796 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11798 location_t loc0 = expr_location_or (arg0, loc);
11799 /* Only perform transformation if ARG0 is easily inverted. */
11800 tem = fold_invert_truthvalue (loc0, arg0);
11801 if (tem)
11802 return fold_build2_loc (loc, code == VEC_COND_EXPR
11803 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11804 type, fold_convert_loc (loc, type, tem),
11805 op2);
11808 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11809 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11810 && truth_value_p (TREE_CODE (arg0))
11811 && truth_value_p (TREE_CODE (op2))
11812 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11813 return fold_build2_loc (loc, code == VEC_COND_EXPR
11814 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11815 type, fold_convert_loc (loc, type, arg0), op2);
11817 return NULL_TREE;
11819 case CALL_EXPR:
11820 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11821 of fold_ternary on them. */
11822 gcc_unreachable ();
11824 case BIT_FIELD_REF:
11825 if ((TREE_CODE (arg0) == VECTOR_CST
11826 || (TREE_CODE (arg0) == CONSTRUCTOR
11827 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
11828 && (type == TREE_TYPE (TREE_TYPE (arg0))
11829 || (TREE_CODE (type) == VECTOR_TYPE
11830 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11832 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11833 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11834 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11835 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11837 if (n != 0
11838 && (idx % width) == 0
11839 && (n % width) == 0
11840 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11842 idx = idx / width;
11843 n = n / width;
11845 if (TREE_CODE (arg0) == VECTOR_CST)
11847 if (n == 1)
11848 return VECTOR_CST_ELT (arg0, idx);
11850 tree *vals = XALLOCAVEC (tree, n);
11851 for (unsigned i = 0; i < n; ++i)
11852 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11853 return build_vector (type, vals);
11856 /* Constructor elements can be subvectors. */
11857 unsigned HOST_WIDE_INT k = 1;
11858 if (CONSTRUCTOR_NELTS (arg0) != 0)
11860 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
11861 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
11862 k = TYPE_VECTOR_SUBPARTS (cons_elem);
11865 /* We keep an exact subset of the constructor elements. */
11866 if ((idx % k) == 0 && (n % k) == 0)
11868 if (CONSTRUCTOR_NELTS (arg0) == 0)
11869 return build_constructor (type, NULL);
11870 idx /= k;
11871 n /= k;
11872 if (n == 1)
11874 if (idx < CONSTRUCTOR_NELTS (arg0))
11875 return CONSTRUCTOR_ELT (arg0, idx)->value;
11876 return build_zero_cst (type);
11879 vec<constructor_elt, va_gc> *vals;
11880 vec_alloc (vals, n);
11881 for (unsigned i = 0;
11882 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
11883 ++i)
11884 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
11885 CONSTRUCTOR_ELT
11886 (arg0, idx + i)->value);
11887 return build_constructor (type, vals);
11889 /* The bitfield references a single constructor element. */
11890 else if (idx + n <= (idx / k + 1) * k)
11892 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
11893 return build_zero_cst (type);
11894 else if (n == k)
11895 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
11896 else
11897 return fold_build3_loc (loc, code, type,
11898 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
11899 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
11904 /* A bit-field-ref that referenced the full argument can be stripped. */
11905 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11906 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
11907 && integer_zerop (op2))
11908 return fold_convert_loc (loc, type, arg0);
11910 /* On constants we can use native encode/interpret to constant
11911 fold (nearly) all BIT_FIELD_REFs. */
11912 if (CONSTANT_CLASS_P (arg0)
11913 && can_native_interpret_type_p (type)
11914 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
11915 /* This limitation should not be necessary, we just need to
11916 round this up to mode size. */
11917 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
11918 /* Need bit-shifting of the buffer to relax the following. */
11919 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
11921 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11922 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11923 unsigned HOST_WIDE_INT clen;
11924 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
11925 /* ??? We cannot tell native_encode_expr to start at
11926 some random byte only. So limit us to a reasonable amount
11927 of work. */
11928 if (clen <= 4096)
11930 unsigned char *b = XALLOCAVEC (unsigned char, clen);
11931 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
11932 if (len > 0
11933 && len * BITS_PER_UNIT >= bitpos + bitsize)
11935 tree v = native_interpret_expr (type,
11936 b + bitpos / BITS_PER_UNIT,
11937 bitsize / BITS_PER_UNIT);
11938 if (v)
11939 return v;
11944 return NULL_TREE;
11946 case FMA_EXPR:
11947 /* For integers we can decompose the FMA if possible. */
11948 if (TREE_CODE (arg0) == INTEGER_CST
11949 && TREE_CODE (arg1) == INTEGER_CST)
11950 return fold_build2_loc (loc, PLUS_EXPR, type,
11951 const_binop (MULT_EXPR, arg0, arg1), arg2);
11952 if (integer_zerop (arg2))
11953 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11955 return fold_fma (loc, type, arg0, arg1, arg2);
11957 case VEC_PERM_EXPR:
11958 if (TREE_CODE (arg2) == VECTOR_CST)
11960 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11961 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11962 unsigned char *sel2 = sel + nelts;
11963 bool need_mask_canon = false;
11964 bool need_mask_canon2 = false;
11965 bool all_in_vec0 = true;
11966 bool all_in_vec1 = true;
11967 bool maybe_identity = true;
11968 bool single_arg = (op0 == op1);
11969 bool changed = false;
11971 mask2 = 2 * nelts - 1;
11972 mask = single_arg ? (nelts - 1) : mask2;
11973 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11974 for (i = 0; i < nelts; i++)
11976 tree val = VECTOR_CST_ELT (arg2, i);
11977 if (TREE_CODE (val) != INTEGER_CST)
11978 return NULL_TREE;
11980 /* Make sure that the perm value is in an acceptable
11981 range. */
11982 wide_int t = val;
11983 need_mask_canon |= wi::gtu_p (t, mask);
11984 need_mask_canon2 |= wi::gtu_p (t, mask2);
11985 sel[i] = t.to_uhwi () & mask;
11986 sel2[i] = t.to_uhwi () & mask2;
11988 if (sel[i] < nelts)
11989 all_in_vec1 = false;
11990 else
11991 all_in_vec0 = false;
11993 if ((sel[i] & (nelts-1)) != i)
11994 maybe_identity = false;
11997 if (maybe_identity)
11999 if (all_in_vec0)
12000 return op0;
12001 if (all_in_vec1)
12002 return op1;
12005 if (all_in_vec0)
12006 op1 = op0;
12007 else if (all_in_vec1)
12009 op0 = op1;
12010 for (i = 0; i < nelts; i++)
12011 sel[i] -= nelts;
12012 need_mask_canon = true;
12015 if ((TREE_CODE (op0) == VECTOR_CST
12016 || TREE_CODE (op0) == CONSTRUCTOR)
12017 && (TREE_CODE (op1) == VECTOR_CST
12018 || TREE_CODE (op1) == CONSTRUCTOR))
12020 tree t = fold_vec_perm (type, op0, op1, sel);
12021 if (t != NULL_TREE)
12022 return t;
12025 if (op0 == op1 && !single_arg)
12026 changed = true;
12028 /* Some targets are deficient and fail to expand a single
12029 argument permutation while still allowing an equivalent
12030 2-argument version. */
12031 if (need_mask_canon && arg2 == op2
12032 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
12033 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
12035 need_mask_canon = need_mask_canon2;
12036 sel = sel2;
12039 if (need_mask_canon && arg2 == op2)
12041 tree *tsel = XALLOCAVEC (tree, nelts);
12042 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
12043 for (i = 0; i < nelts; i++)
12044 tsel[i] = build_int_cst (eltype, sel[i]);
12045 op2 = build_vector (TREE_TYPE (arg2), tsel);
12046 changed = true;
12049 if (changed)
12050 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
12052 return NULL_TREE;
12054 default:
12055 return NULL_TREE;
12056 } /* switch (code) */
12059 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
12060 of an array (or vector). */
12062 tree
12063 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
12065 tree index_type = NULL_TREE;
12066 offset_int low_bound = 0;
12068 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
12070 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
12071 if (domain_type && TYPE_MIN_VALUE (domain_type))
12073 /* Static constructors for variably sized objects makes no sense. */
12074 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
12075 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
12076 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
12080 if (index_type)
12081 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
12082 TYPE_SIGN (index_type));
12084 offset_int index = low_bound - 1;
12085 if (index_type)
12086 index = wi::ext (index, TYPE_PRECISION (index_type),
12087 TYPE_SIGN (index_type));
12089 offset_int max_index;
12090 unsigned HOST_WIDE_INT cnt;
12091 tree cfield, cval;
12093 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
12095 /* Array constructor might explicitly set index, or specify a range,
12096 or leave index NULL meaning that it is next index after previous
12097 one. */
12098 if (cfield)
12100 if (TREE_CODE (cfield) == INTEGER_CST)
12101 max_index = index = wi::to_offset (cfield);
12102 else
12104 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
12105 index = wi::to_offset (TREE_OPERAND (cfield, 0));
12106 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
12109 else
12111 index += 1;
12112 if (index_type)
12113 index = wi::ext (index, TYPE_PRECISION (index_type),
12114 TYPE_SIGN (index_type));
12115 max_index = index;
12118 /* Do we have match? */
12119 if (wi::cmpu (access_index, index) >= 0
12120 && wi::cmpu (access_index, max_index) <= 0)
12121 return cval;
12123 return NULL_TREE;
12126 /* Perform constant folding and related simplification of EXPR.
12127 The related simplifications include x*1 => x, x*0 => 0, etc.,
12128 and application of the associative law.
12129 NOP_EXPR conversions may be removed freely (as long as we
12130 are careful not to change the type of the overall expression).
12131 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
12132 but we can constant-fold them if they have constant operands. */
12134 #ifdef ENABLE_FOLD_CHECKING
12135 # define fold(x) fold_1 (x)
12136 static tree fold_1 (tree);
12137 static
12138 #endif
12139 tree
12140 fold (tree expr)
12142 const tree t = expr;
12143 enum tree_code code = TREE_CODE (t);
12144 enum tree_code_class kind = TREE_CODE_CLASS (code);
12145 tree tem;
12146 location_t loc = EXPR_LOCATION (expr);
12148 /* Return right away if a constant. */
12149 if (kind == tcc_constant)
12150 return t;
12152 /* CALL_EXPR-like objects with variable numbers of operands are
12153 treated specially. */
12154 if (kind == tcc_vl_exp)
12156 if (code == CALL_EXPR)
12158 tem = fold_call_expr (loc, expr, false);
12159 return tem ? tem : expr;
12161 return expr;
12164 if (IS_EXPR_CODE_CLASS (kind))
12166 tree type = TREE_TYPE (t);
12167 tree op0, op1, op2;
12169 switch (TREE_CODE_LENGTH (code))
12171 case 1:
12172 op0 = TREE_OPERAND (t, 0);
12173 tem = fold_unary_loc (loc, code, type, op0);
12174 return tem ? tem : expr;
12175 case 2:
12176 op0 = TREE_OPERAND (t, 0);
12177 op1 = TREE_OPERAND (t, 1);
12178 tem = fold_binary_loc (loc, code, type, op0, op1);
12179 return tem ? tem : expr;
12180 case 3:
12181 op0 = TREE_OPERAND (t, 0);
12182 op1 = TREE_OPERAND (t, 1);
12183 op2 = TREE_OPERAND (t, 2);
12184 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12185 return tem ? tem : expr;
12186 default:
12187 break;
12191 switch (code)
12193 case ARRAY_REF:
12195 tree op0 = TREE_OPERAND (t, 0);
12196 tree op1 = TREE_OPERAND (t, 1);
12198 if (TREE_CODE (op1) == INTEGER_CST
12199 && TREE_CODE (op0) == CONSTRUCTOR
12200 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12202 tree val = get_array_ctor_element_at_index (op0,
12203 wi::to_offset (op1));
12204 if (val)
12205 return val;
12208 return t;
12211 /* Return a VECTOR_CST if possible. */
12212 case CONSTRUCTOR:
12214 tree type = TREE_TYPE (t);
12215 if (TREE_CODE (type) != VECTOR_TYPE)
12216 return t;
12218 unsigned i;
12219 tree val;
12220 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12221 if (! CONSTANT_CLASS_P (val))
12222 return t;
12224 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12227 case CONST_DECL:
12228 return fold (DECL_INITIAL (t));
12230 default:
12231 return t;
12232 } /* switch (code) */
12235 #ifdef ENABLE_FOLD_CHECKING
12236 #undef fold
12238 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12239 hash_table<nofree_ptr_hash<const tree_node> > *);
12240 static void fold_check_failed (const_tree, const_tree);
12241 void print_fold_checksum (const_tree);
12243 /* When --enable-checking=fold, compute a digest of expr before
12244 and after actual fold call to see if fold did not accidentally
12245 change original expr. */
12247 tree
12248 fold (tree expr)
12250 tree ret;
12251 struct md5_ctx ctx;
12252 unsigned char checksum_before[16], checksum_after[16];
12253 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12255 md5_init_ctx (&ctx);
12256 fold_checksum_tree (expr, &ctx, &ht);
12257 md5_finish_ctx (&ctx, checksum_before);
12258 ht.empty ();
12260 ret = fold_1 (expr);
12262 md5_init_ctx (&ctx);
12263 fold_checksum_tree (expr, &ctx, &ht);
12264 md5_finish_ctx (&ctx, checksum_after);
12266 if (memcmp (checksum_before, checksum_after, 16))
12267 fold_check_failed (expr, ret);
12269 return ret;
12272 void
12273 print_fold_checksum (const_tree expr)
12275 struct md5_ctx ctx;
12276 unsigned char checksum[16], cnt;
12277 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12279 md5_init_ctx (&ctx);
12280 fold_checksum_tree (expr, &ctx, &ht);
12281 md5_finish_ctx (&ctx, checksum);
12282 for (cnt = 0; cnt < 16; ++cnt)
12283 fprintf (stderr, "%02x", checksum[cnt]);
12284 putc ('\n', stderr);
12287 static void
12288 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12290 internal_error ("fold check: original tree changed by fold");
12293 static void
12294 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12295 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12297 const tree_node **slot;
12298 enum tree_code code;
12299 union tree_node buf;
12300 int i, len;
12302 recursive_label:
12303 if (expr == NULL)
12304 return;
12305 slot = ht->find_slot (expr, INSERT);
12306 if (*slot != NULL)
12307 return;
12308 *slot = expr;
12309 code = TREE_CODE (expr);
12310 if (TREE_CODE_CLASS (code) == tcc_declaration
12311 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12313 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12314 memcpy ((char *) &buf, expr, tree_size (expr));
12315 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12316 buf.decl_with_vis.symtab_node = NULL;
12317 expr = (tree) &buf;
12319 else if (TREE_CODE_CLASS (code) == tcc_type
12320 && (TYPE_POINTER_TO (expr)
12321 || TYPE_REFERENCE_TO (expr)
12322 || TYPE_CACHED_VALUES_P (expr)
12323 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12324 || TYPE_NEXT_VARIANT (expr)
12325 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12327 /* Allow these fields to be modified. */
12328 tree tmp;
12329 memcpy ((char *) &buf, expr, tree_size (expr));
12330 expr = tmp = (tree) &buf;
12331 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12332 TYPE_POINTER_TO (tmp) = NULL;
12333 TYPE_REFERENCE_TO (tmp) = NULL;
12334 TYPE_NEXT_VARIANT (tmp) = NULL;
12335 TYPE_ALIAS_SET (tmp) = -1;
12336 if (TYPE_CACHED_VALUES_P (tmp))
12338 TYPE_CACHED_VALUES_P (tmp) = 0;
12339 TYPE_CACHED_VALUES (tmp) = NULL;
12342 md5_process_bytes (expr, tree_size (expr), ctx);
12343 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12344 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12345 if (TREE_CODE_CLASS (code) != tcc_type
12346 && TREE_CODE_CLASS (code) != tcc_declaration
12347 && code != TREE_LIST
12348 && code != SSA_NAME
12349 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12350 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12351 switch (TREE_CODE_CLASS (code))
12353 case tcc_constant:
12354 switch (code)
12356 case STRING_CST:
12357 md5_process_bytes (TREE_STRING_POINTER (expr),
12358 TREE_STRING_LENGTH (expr), ctx);
12359 break;
12360 case COMPLEX_CST:
12361 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12362 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12363 break;
12364 case VECTOR_CST:
12365 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12366 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12367 break;
12368 default:
12369 break;
12371 break;
12372 case tcc_exceptional:
12373 switch (code)
12375 case TREE_LIST:
12376 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12377 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12378 expr = TREE_CHAIN (expr);
12379 goto recursive_label;
12380 break;
12381 case TREE_VEC:
12382 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12383 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12384 break;
12385 default:
12386 break;
12388 break;
12389 case tcc_expression:
12390 case tcc_reference:
12391 case tcc_comparison:
12392 case tcc_unary:
12393 case tcc_binary:
12394 case tcc_statement:
12395 case tcc_vl_exp:
12396 len = TREE_OPERAND_LENGTH (expr);
12397 for (i = 0; i < len; ++i)
12398 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12399 break;
12400 case tcc_declaration:
12401 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12402 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12403 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12405 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12406 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12407 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12408 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12409 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12412 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12414 if (TREE_CODE (expr) == FUNCTION_DECL)
12416 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12417 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12419 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12421 break;
12422 case tcc_type:
12423 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12424 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12425 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12426 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12427 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12428 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12429 if (INTEGRAL_TYPE_P (expr)
12430 || SCALAR_FLOAT_TYPE_P (expr))
12432 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12433 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12435 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12436 if (TREE_CODE (expr) == RECORD_TYPE
12437 || TREE_CODE (expr) == UNION_TYPE
12438 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12439 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12440 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12441 break;
12442 default:
12443 break;
12447 /* Helper function for outputting the checksum of a tree T. When
12448 debugging with gdb, you can "define mynext" to be "next" followed
12449 by "call debug_fold_checksum (op0)", then just trace down till the
12450 outputs differ. */
12452 DEBUG_FUNCTION void
12453 debug_fold_checksum (const_tree t)
12455 int i;
12456 unsigned char checksum[16];
12457 struct md5_ctx ctx;
12458 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12460 md5_init_ctx (&ctx);
12461 fold_checksum_tree (t, &ctx, &ht);
12462 md5_finish_ctx (&ctx, checksum);
12463 ht.empty ();
12465 for (i = 0; i < 16; i++)
12466 fprintf (stderr, "%d ", checksum[i]);
12468 fprintf (stderr, "\n");
12471 #endif
12473 /* Fold a unary tree expression with code CODE of type TYPE with an
12474 operand OP0. LOC is the location of the resulting expression.
12475 Return a folded expression if successful. Otherwise, return a tree
12476 expression with code CODE of type TYPE with an operand OP0. */
12478 tree
12479 fold_build1_stat_loc (location_t loc,
12480 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12482 tree tem;
12483 #ifdef ENABLE_FOLD_CHECKING
12484 unsigned char checksum_before[16], checksum_after[16];
12485 struct md5_ctx ctx;
12486 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12488 md5_init_ctx (&ctx);
12489 fold_checksum_tree (op0, &ctx, &ht);
12490 md5_finish_ctx (&ctx, checksum_before);
12491 ht.empty ();
12492 #endif
12494 tem = fold_unary_loc (loc, code, type, op0);
12495 if (!tem)
12496 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12498 #ifdef ENABLE_FOLD_CHECKING
12499 md5_init_ctx (&ctx);
12500 fold_checksum_tree (op0, &ctx, &ht);
12501 md5_finish_ctx (&ctx, checksum_after);
12503 if (memcmp (checksum_before, checksum_after, 16))
12504 fold_check_failed (op0, tem);
12505 #endif
12506 return tem;
12509 /* Fold a binary tree expression with code CODE of type TYPE with
12510 operands OP0 and OP1. LOC is the location of the resulting
12511 expression. Return a folded expression if successful. Otherwise,
12512 return a tree expression with code CODE of type TYPE with operands
12513 OP0 and OP1. */
12515 tree
12516 fold_build2_stat_loc (location_t loc,
12517 enum tree_code code, tree type, tree op0, tree op1
12518 MEM_STAT_DECL)
12520 tree tem;
12521 #ifdef ENABLE_FOLD_CHECKING
12522 unsigned char checksum_before_op0[16],
12523 checksum_before_op1[16],
12524 checksum_after_op0[16],
12525 checksum_after_op1[16];
12526 struct md5_ctx ctx;
12527 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12529 md5_init_ctx (&ctx);
12530 fold_checksum_tree (op0, &ctx, &ht);
12531 md5_finish_ctx (&ctx, checksum_before_op0);
12532 ht.empty ();
12534 md5_init_ctx (&ctx);
12535 fold_checksum_tree (op1, &ctx, &ht);
12536 md5_finish_ctx (&ctx, checksum_before_op1);
12537 ht.empty ();
12538 #endif
12540 tem = fold_binary_loc (loc, code, type, op0, op1);
12541 if (!tem)
12542 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12544 #ifdef ENABLE_FOLD_CHECKING
12545 md5_init_ctx (&ctx);
12546 fold_checksum_tree (op0, &ctx, &ht);
12547 md5_finish_ctx (&ctx, checksum_after_op0);
12548 ht.empty ();
12550 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12551 fold_check_failed (op0, tem);
12553 md5_init_ctx (&ctx);
12554 fold_checksum_tree (op1, &ctx, &ht);
12555 md5_finish_ctx (&ctx, checksum_after_op1);
12557 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12558 fold_check_failed (op1, tem);
12559 #endif
12560 return tem;
12563 /* Fold a ternary tree expression with code CODE of type TYPE with
12564 operands OP0, OP1, and OP2. Return a folded expression if
12565 successful. Otherwise, return a tree expression with code CODE of
12566 type TYPE with operands OP0, OP1, and OP2. */
12568 tree
12569 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12570 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12572 tree tem;
12573 #ifdef ENABLE_FOLD_CHECKING
12574 unsigned char checksum_before_op0[16],
12575 checksum_before_op1[16],
12576 checksum_before_op2[16],
12577 checksum_after_op0[16],
12578 checksum_after_op1[16],
12579 checksum_after_op2[16];
12580 struct md5_ctx ctx;
12581 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12583 md5_init_ctx (&ctx);
12584 fold_checksum_tree (op0, &ctx, &ht);
12585 md5_finish_ctx (&ctx, checksum_before_op0);
12586 ht.empty ();
12588 md5_init_ctx (&ctx);
12589 fold_checksum_tree (op1, &ctx, &ht);
12590 md5_finish_ctx (&ctx, checksum_before_op1);
12591 ht.empty ();
12593 md5_init_ctx (&ctx);
12594 fold_checksum_tree (op2, &ctx, &ht);
12595 md5_finish_ctx (&ctx, checksum_before_op2);
12596 ht.empty ();
12597 #endif
12599 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12600 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12601 if (!tem)
12602 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12604 #ifdef ENABLE_FOLD_CHECKING
12605 md5_init_ctx (&ctx);
12606 fold_checksum_tree (op0, &ctx, &ht);
12607 md5_finish_ctx (&ctx, checksum_after_op0);
12608 ht.empty ();
12610 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12611 fold_check_failed (op0, tem);
12613 md5_init_ctx (&ctx);
12614 fold_checksum_tree (op1, &ctx, &ht);
12615 md5_finish_ctx (&ctx, checksum_after_op1);
12616 ht.empty ();
12618 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12619 fold_check_failed (op1, tem);
12621 md5_init_ctx (&ctx);
12622 fold_checksum_tree (op2, &ctx, &ht);
12623 md5_finish_ctx (&ctx, checksum_after_op2);
12625 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12626 fold_check_failed (op2, tem);
12627 #endif
12628 return tem;
12631 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12632 arguments in ARGARRAY, and a null static chain.
12633 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12634 of type TYPE from the given operands as constructed by build_call_array. */
12636 tree
12637 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12638 int nargs, tree *argarray)
12640 tree tem;
12641 #ifdef ENABLE_FOLD_CHECKING
12642 unsigned char checksum_before_fn[16],
12643 checksum_before_arglist[16],
12644 checksum_after_fn[16],
12645 checksum_after_arglist[16];
12646 struct md5_ctx ctx;
12647 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12648 int i;
12650 md5_init_ctx (&ctx);
12651 fold_checksum_tree (fn, &ctx, &ht);
12652 md5_finish_ctx (&ctx, checksum_before_fn);
12653 ht.empty ();
12655 md5_init_ctx (&ctx);
12656 for (i = 0; i < nargs; i++)
12657 fold_checksum_tree (argarray[i], &ctx, &ht);
12658 md5_finish_ctx (&ctx, checksum_before_arglist);
12659 ht.empty ();
12660 #endif
12662 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12663 if (!tem)
12664 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12666 #ifdef ENABLE_FOLD_CHECKING
12667 md5_init_ctx (&ctx);
12668 fold_checksum_tree (fn, &ctx, &ht);
12669 md5_finish_ctx (&ctx, checksum_after_fn);
12670 ht.empty ();
12672 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12673 fold_check_failed (fn, tem);
12675 md5_init_ctx (&ctx);
12676 for (i = 0; i < nargs; i++)
12677 fold_checksum_tree (argarray[i], &ctx, &ht);
12678 md5_finish_ctx (&ctx, checksum_after_arglist);
12680 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12681 fold_check_failed (NULL_TREE, tem);
12682 #endif
12683 return tem;
12686 /* Perform constant folding and related simplification of initializer
12687 expression EXPR. These behave identically to "fold_buildN" but ignore
12688 potential run-time traps and exceptions that fold must preserve. */
12690 #define START_FOLD_INIT \
12691 int saved_signaling_nans = flag_signaling_nans;\
12692 int saved_trapping_math = flag_trapping_math;\
12693 int saved_rounding_math = flag_rounding_math;\
12694 int saved_trapv = flag_trapv;\
12695 int saved_folding_initializer = folding_initializer;\
12696 flag_signaling_nans = 0;\
12697 flag_trapping_math = 0;\
12698 flag_rounding_math = 0;\
12699 flag_trapv = 0;\
12700 folding_initializer = 1;
12702 #define END_FOLD_INIT \
12703 flag_signaling_nans = saved_signaling_nans;\
12704 flag_trapping_math = saved_trapping_math;\
12705 flag_rounding_math = saved_rounding_math;\
12706 flag_trapv = saved_trapv;\
12707 folding_initializer = saved_folding_initializer;
12709 tree
12710 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12711 tree type, tree op)
12713 tree result;
12714 START_FOLD_INIT;
12716 result = fold_build1_loc (loc, code, type, op);
12718 END_FOLD_INIT;
12719 return result;
12722 tree
12723 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12724 tree type, tree op0, tree op1)
12726 tree result;
12727 START_FOLD_INIT;
12729 result = fold_build2_loc (loc, code, type, op0, op1);
12731 END_FOLD_INIT;
12732 return result;
12735 tree
12736 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12737 int nargs, tree *argarray)
12739 tree result;
12740 START_FOLD_INIT;
12742 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12744 END_FOLD_INIT;
12745 return result;
12748 #undef START_FOLD_INIT
12749 #undef END_FOLD_INIT
12751 /* Determine if first argument is a multiple of second argument. Return 0 if
12752 it is not, or we cannot easily determined it to be.
12754 An example of the sort of thing we care about (at this point; this routine
12755 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12756 fold cases do now) is discovering that
12758 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12760 is a multiple of
12762 SAVE_EXPR (J * 8)
12764 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12766 This code also handles discovering that
12768 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12770 is a multiple of 8 so we don't have to worry about dealing with a
12771 possible remainder.
12773 Note that we *look* inside a SAVE_EXPR only to determine how it was
12774 calculated; it is not safe for fold to do much of anything else with the
12775 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12776 at run time. For example, the latter example above *cannot* be implemented
12777 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12778 evaluation time of the original SAVE_EXPR is not necessarily the same at
12779 the time the new expression is evaluated. The only optimization of this
12780 sort that would be valid is changing
12782 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12784 divided by 8 to
12786 SAVE_EXPR (I) * SAVE_EXPR (J)
12788 (where the same SAVE_EXPR (J) is used in the original and the
12789 transformed version). */
12792 multiple_of_p (tree type, const_tree top, const_tree bottom)
12794 if (operand_equal_p (top, bottom, 0))
12795 return 1;
12797 if (TREE_CODE (type) != INTEGER_TYPE)
12798 return 0;
12800 switch (TREE_CODE (top))
12802 case BIT_AND_EXPR:
12803 /* Bitwise and provides a power of two multiple. If the mask is
12804 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12805 if (!integer_pow2p (bottom))
12806 return 0;
12807 /* FALLTHRU */
12809 case MULT_EXPR:
12810 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12811 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12813 case PLUS_EXPR:
12814 case MINUS_EXPR:
12815 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12816 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12818 case LSHIFT_EXPR:
12819 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12821 tree op1, t1;
12823 op1 = TREE_OPERAND (top, 1);
12824 /* const_binop may not detect overflow correctly,
12825 so check for it explicitly here. */
12826 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12827 && 0 != (t1 = fold_convert (type,
12828 const_binop (LSHIFT_EXPR,
12829 size_one_node,
12830 op1)))
12831 && !TREE_OVERFLOW (t1))
12832 return multiple_of_p (type, t1, bottom);
12834 return 0;
12836 case NOP_EXPR:
12837 /* Can't handle conversions from non-integral or wider integral type. */
12838 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12839 || (TYPE_PRECISION (type)
12840 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12841 return 0;
12843 /* .. fall through ... */
12845 case SAVE_EXPR:
12846 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12848 case COND_EXPR:
12849 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12850 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12852 case INTEGER_CST:
12853 if (TREE_CODE (bottom) != INTEGER_CST
12854 || integer_zerop (bottom)
12855 || (TYPE_UNSIGNED (type)
12856 && (tree_int_cst_sgn (top) < 0
12857 || tree_int_cst_sgn (bottom) < 0)))
12858 return 0;
12859 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12860 SIGNED);
12862 default:
12863 return 0;
12867 #define tree_expr_nonnegative_warnv_p(X, Y) \
12868 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12870 #define RECURSE(X) \
12871 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12873 /* Return true if CODE or TYPE is known to be non-negative. */
12875 static bool
12876 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12878 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12879 && truth_value_p (code))
12880 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12881 have a signed:1 type (where the value is -1 and 0). */
12882 return true;
12883 return false;
12886 /* Return true if (CODE OP0) is known to be non-negative. If the return
12887 value is based on the assumption that signed overflow is undefined,
12888 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12889 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12891 bool
12892 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12893 bool *strict_overflow_p, int depth)
12895 if (TYPE_UNSIGNED (type))
12896 return true;
12898 switch (code)
12900 case ABS_EXPR:
12901 /* We can't return 1 if flag_wrapv is set because
12902 ABS_EXPR<INT_MIN> = INT_MIN. */
12903 if (!ANY_INTEGRAL_TYPE_P (type))
12904 return true;
12905 if (TYPE_OVERFLOW_UNDEFINED (type))
12907 *strict_overflow_p = true;
12908 return true;
12910 break;
12912 case NON_LVALUE_EXPR:
12913 case FLOAT_EXPR:
12914 case FIX_TRUNC_EXPR:
12915 return RECURSE (op0);
12917 CASE_CONVERT:
12919 tree inner_type = TREE_TYPE (op0);
12920 tree outer_type = type;
12922 if (TREE_CODE (outer_type) == REAL_TYPE)
12924 if (TREE_CODE (inner_type) == REAL_TYPE)
12925 return RECURSE (op0);
12926 if (INTEGRAL_TYPE_P (inner_type))
12928 if (TYPE_UNSIGNED (inner_type))
12929 return true;
12930 return RECURSE (op0);
12933 else if (INTEGRAL_TYPE_P (outer_type))
12935 if (TREE_CODE (inner_type) == REAL_TYPE)
12936 return RECURSE (op0);
12937 if (INTEGRAL_TYPE_P (inner_type))
12938 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12939 && TYPE_UNSIGNED (inner_type);
12942 break;
12944 default:
12945 return tree_simple_nonnegative_warnv_p (code, type);
12948 /* We don't know sign of `t', so be conservative and return false. */
12949 return false;
12952 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12953 value is based on the assumption that signed overflow is undefined,
12954 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12955 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12957 bool
12958 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12959 tree op1, bool *strict_overflow_p,
12960 int depth)
12962 if (TYPE_UNSIGNED (type))
12963 return true;
12965 switch (code)
12967 case POINTER_PLUS_EXPR:
12968 case PLUS_EXPR:
12969 if (FLOAT_TYPE_P (type))
12970 return RECURSE (op0) && RECURSE (op1);
12972 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12973 both unsigned and at least 2 bits shorter than the result. */
12974 if (TREE_CODE (type) == INTEGER_TYPE
12975 && TREE_CODE (op0) == NOP_EXPR
12976 && TREE_CODE (op1) == NOP_EXPR)
12978 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12979 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12980 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12981 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12983 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12984 TYPE_PRECISION (inner2)) + 1;
12985 return prec < TYPE_PRECISION (type);
12988 break;
12990 case MULT_EXPR:
12991 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12993 /* x * x is always non-negative for floating point x
12994 or without overflow. */
12995 if (operand_equal_p (op0, op1, 0)
12996 || (RECURSE (op0) && RECURSE (op1)))
12998 if (ANY_INTEGRAL_TYPE_P (type)
12999 && TYPE_OVERFLOW_UNDEFINED (type))
13000 *strict_overflow_p = true;
13001 return true;
13005 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
13006 both unsigned and their total bits is shorter than the result. */
13007 if (TREE_CODE (type) == INTEGER_TYPE
13008 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
13009 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
13011 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
13012 ? TREE_TYPE (TREE_OPERAND (op0, 0))
13013 : TREE_TYPE (op0);
13014 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
13015 ? TREE_TYPE (TREE_OPERAND (op1, 0))
13016 : TREE_TYPE (op1);
13018 bool unsigned0 = TYPE_UNSIGNED (inner0);
13019 bool unsigned1 = TYPE_UNSIGNED (inner1);
13021 if (TREE_CODE (op0) == INTEGER_CST)
13022 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
13024 if (TREE_CODE (op1) == INTEGER_CST)
13025 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
13027 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
13028 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
13030 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
13031 ? tree_int_cst_min_precision (op0, UNSIGNED)
13032 : TYPE_PRECISION (inner0);
13034 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
13035 ? tree_int_cst_min_precision (op1, UNSIGNED)
13036 : TYPE_PRECISION (inner1);
13038 return precision0 + precision1 < TYPE_PRECISION (type);
13041 return false;
13043 case BIT_AND_EXPR:
13044 case MAX_EXPR:
13045 return RECURSE (op0) || RECURSE (op1);
13047 case BIT_IOR_EXPR:
13048 case BIT_XOR_EXPR:
13049 case MIN_EXPR:
13050 case RDIV_EXPR:
13051 case TRUNC_DIV_EXPR:
13052 case CEIL_DIV_EXPR:
13053 case FLOOR_DIV_EXPR:
13054 case ROUND_DIV_EXPR:
13055 return RECURSE (op0) && RECURSE (op1);
13057 case TRUNC_MOD_EXPR:
13058 return RECURSE (op0);
13060 case FLOOR_MOD_EXPR:
13061 return RECURSE (op1);
13063 case CEIL_MOD_EXPR:
13064 case ROUND_MOD_EXPR:
13065 default:
13066 return tree_simple_nonnegative_warnv_p (code, type);
13069 /* We don't know sign of `t', so be conservative and return false. */
13070 return false;
13073 /* Return true if T is known to be non-negative. If the return
13074 value is based on the assumption that signed overflow is undefined,
13075 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13076 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13078 bool
13079 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13081 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13082 return true;
13084 switch (TREE_CODE (t))
13086 case INTEGER_CST:
13087 return tree_int_cst_sgn (t) >= 0;
13089 case REAL_CST:
13090 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13092 case FIXED_CST:
13093 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13095 case COND_EXPR:
13096 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13098 case SSA_NAME:
13099 /* Limit the depth of recursion to avoid quadratic behavior.
13100 This is expected to catch almost all occurrences in practice.
13101 If this code misses important cases that unbounded recursion
13102 would not, passes that need this information could be revised
13103 to provide it through dataflow propagation. */
13104 return (!name_registered_for_update_p (t)
13105 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13106 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
13107 strict_overflow_p, depth));
13109 default:
13110 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13114 /* Return true if T is known to be non-negative. If the return
13115 value is based on the assumption that signed overflow is undefined,
13116 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13117 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13119 bool
13120 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13121 bool *strict_overflow_p, int depth)
13123 switch (fn)
13125 CASE_CFN_ACOS:
13126 CASE_CFN_ACOSH:
13127 CASE_CFN_CABS:
13128 CASE_CFN_COSH:
13129 CASE_CFN_ERFC:
13130 CASE_CFN_EXP:
13131 CASE_CFN_EXP10:
13132 CASE_CFN_EXP2:
13133 CASE_CFN_FABS:
13134 CASE_CFN_FDIM:
13135 CASE_CFN_HYPOT:
13136 CASE_CFN_POW10:
13137 CASE_CFN_FFS:
13138 CASE_CFN_PARITY:
13139 CASE_CFN_POPCOUNT:
13140 CASE_CFN_CLZ:
13141 CASE_CFN_CLRSB:
13142 case CFN_BUILT_IN_BSWAP32:
13143 case CFN_BUILT_IN_BSWAP64:
13144 /* Always true. */
13145 return true;
13147 CASE_CFN_SQRT:
13148 /* sqrt(-0.0) is -0.0. */
13149 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13150 return true;
13151 return RECURSE (arg0);
13153 CASE_CFN_ASINH:
13154 CASE_CFN_ATAN:
13155 CASE_CFN_ATANH:
13156 CASE_CFN_CBRT:
13157 CASE_CFN_CEIL:
13158 CASE_CFN_ERF:
13159 CASE_CFN_EXPM1:
13160 CASE_CFN_FLOOR:
13161 CASE_CFN_FMOD:
13162 CASE_CFN_FREXP:
13163 CASE_CFN_ICEIL:
13164 CASE_CFN_IFLOOR:
13165 CASE_CFN_IRINT:
13166 CASE_CFN_IROUND:
13167 CASE_CFN_LCEIL:
13168 CASE_CFN_LDEXP:
13169 CASE_CFN_LFLOOR:
13170 CASE_CFN_LLCEIL:
13171 CASE_CFN_LLFLOOR:
13172 CASE_CFN_LLRINT:
13173 CASE_CFN_LLROUND:
13174 CASE_CFN_LRINT:
13175 CASE_CFN_LROUND:
13176 CASE_CFN_MODF:
13177 CASE_CFN_NEARBYINT:
13178 CASE_CFN_RINT:
13179 CASE_CFN_ROUND:
13180 CASE_CFN_SCALB:
13181 CASE_CFN_SCALBLN:
13182 CASE_CFN_SCALBN:
13183 CASE_CFN_SIGNBIT:
13184 CASE_CFN_SIGNIFICAND:
13185 CASE_CFN_SINH:
13186 CASE_CFN_TANH:
13187 CASE_CFN_TRUNC:
13188 /* True if the 1st argument is nonnegative. */
13189 return RECURSE (arg0);
13191 CASE_CFN_FMAX:
13192 /* True if the 1st OR 2nd arguments are nonnegative. */
13193 return RECURSE (arg0) || RECURSE (arg1);
13195 CASE_CFN_FMIN:
13196 /* True if the 1st AND 2nd arguments are nonnegative. */
13197 return RECURSE (arg0) && RECURSE (arg1);
13199 CASE_CFN_COPYSIGN:
13200 /* True if the 2nd argument is nonnegative. */
13201 return RECURSE (arg1);
13203 CASE_CFN_POWI:
13204 /* True if the 1st argument is nonnegative or the second
13205 argument is an even integer. */
13206 if (TREE_CODE (arg1) == INTEGER_CST
13207 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13208 return true;
13209 return RECURSE (arg0);
13211 CASE_CFN_POW:
13212 /* True if the 1st argument is nonnegative or the second
13213 argument is an even integer valued real. */
13214 if (TREE_CODE (arg1) == REAL_CST)
13216 REAL_VALUE_TYPE c;
13217 HOST_WIDE_INT n;
13219 c = TREE_REAL_CST (arg1);
13220 n = real_to_integer (&c);
13221 if ((n & 1) == 0)
13223 REAL_VALUE_TYPE cint;
13224 real_from_integer (&cint, VOIDmode, n, SIGNED);
13225 if (real_identical (&c, &cint))
13226 return true;
13229 return RECURSE (arg0);
13231 default:
13232 break;
13234 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13237 /* Return true if T is known to be non-negative. If the return
13238 value is based on the assumption that signed overflow is undefined,
13239 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13240 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13242 static bool
13243 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13245 enum tree_code code = TREE_CODE (t);
13246 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13247 return true;
13249 switch (code)
13251 case TARGET_EXPR:
13253 tree temp = TARGET_EXPR_SLOT (t);
13254 t = TARGET_EXPR_INITIAL (t);
13256 /* If the initializer is non-void, then it's a normal expression
13257 that will be assigned to the slot. */
13258 if (!VOID_TYPE_P (t))
13259 return RECURSE (t);
13261 /* Otherwise, the initializer sets the slot in some way. One common
13262 way is an assignment statement at the end of the initializer. */
13263 while (1)
13265 if (TREE_CODE (t) == BIND_EXPR)
13266 t = expr_last (BIND_EXPR_BODY (t));
13267 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13268 || TREE_CODE (t) == TRY_CATCH_EXPR)
13269 t = expr_last (TREE_OPERAND (t, 0));
13270 else if (TREE_CODE (t) == STATEMENT_LIST)
13271 t = expr_last (t);
13272 else
13273 break;
13275 if (TREE_CODE (t) == MODIFY_EXPR
13276 && TREE_OPERAND (t, 0) == temp)
13277 return RECURSE (TREE_OPERAND (t, 1));
13279 return false;
13282 case CALL_EXPR:
13284 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13285 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13287 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13288 get_call_combined_fn (t),
13289 arg0,
13290 arg1,
13291 strict_overflow_p, depth);
13293 case COMPOUND_EXPR:
13294 case MODIFY_EXPR:
13295 return RECURSE (TREE_OPERAND (t, 1));
13297 case BIND_EXPR:
13298 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13300 case SAVE_EXPR:
13301 return RECURSE (TREE_OPERAND (t, 0));
13303 default:
13304 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13308 #undef RECURSE
13309 #undef tree_expr_nonnegative_warnv_p
13311 /* Return true if T is known to be non-negative. If the return
13312 value is based on the assumption that signed overflow is undefined,
13313 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13314 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13316 bool
13317 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13319 enum tree_code code;
13320 if (t == error_mark_node)
13321 return false;
13323 code = TREE_CODE (t);
13324 switch (TREE_CODE_CLASS (code))
13326 case tcc_binary:
13327 case tcc_comparison:
13328 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13329 TREE_TYPE (t),
13330 TREE_OPERAND (t, 0),
13331 TREE_OPERAND (t, 1),
13332 strict_overflow_p, depth);
13334 case tcc_unary:
13335 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13336 TREE_TYPE (t),
13337 TREE_OPERAND (t, 0),
13338 strict_overflow_p, depth);
13340 case tcc_constant:
13341 case tcc_declaration:
13342 case tcc_reference:
13343 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13345 default:
13346 break;
13349 switch (code)
13351 case TRUTH_AND_EXPR:
13352 case TRUTH_OR_EXPR:
13353 case TRUTH_XOR_EXPR:
13354 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13355 TREE_TYPE (t),
13356 TREE_OPERAND (t, 0),
13357 TREE_OPERAND (t, 1),
13358 strict_overflow_p, depth);
13359 case TRUTH_NOT_EXPR:
13360 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13361 TREE_TYPE (t),
13362 TREE_OPERAND (t, 0),
13363 strict_overflow_p, depth);
13365 case COND_EXPR:
13366 case CONSTRUCTOR:
13367 case OBJ_TYPE_REF:
13368 case ASSERT_EXPR:
13369 case ADDR_EXPR:
13370 case WITH_SIZE_EXPR:
13371 case SSA_NAME:
13372 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13374 default:
13375 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13379 /* Return true if `t' is known to be non-negative. Handle warnings
13380 about undefined signed overflow. */
13382 bool
13383 tree_expr_nonnegative_p (tree t)
13385 bool ret, strict_overflow_p;
13387 strict_overflow_p = false;
13388 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13389 if (strict_overflow_p)
13390 fold_overflow_warning (("assuming signed overflow does not occur when "
13391 "determining that expression is always "
13392 "non-negative"),
13393 WARN_STRICT_OVERFLOW_MISC);
13394 return ret;
13398 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13399 For floating point we further ensure that T is not denormal.
13400 Similar logic is present in nonzero_address in rtlanal.h.
13402 If the return value is based on the assumption that signed overflow
13403 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13404 change *STRICT_OVERFLOW_P. */
13406 bool
13407 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13408 bool *strict_overflow_p)
13410 switch (code)
13412 case ABS_EXPR:
13413 return tree_expr_nonzero_warnv_p (op0,
13414 strict_overflow_p);
13416 case NOP_EXPR:
13418 tree inner_type = TREE_TYPE (op0);
13419 tree outer_type = type;
13421 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13422 && tree_expr_nonzero_warnv_p (op0,
13423 strict_overflow_p));
13425 break;
13427 case NON_LVALUE_EXPR:
13428 return tree_expr_nonzero_warnv_p (op0,
13429 strict_overflow_p);
13431 default:
13432 break;
13435 return false;
13438 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13439 For floating point we further ensure that T is not denormal.
13440 Similar logic is present in nonzero_address in rtlanal.h.
13442 If the return value is based on the assumption that signed overflow
13443 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13444 change *STRICT_OVERFLOW_P. */
13446 bool
13447 tree_binary_nonzero_warnv_p (enum tree_code code,
13448 tree type,
13449 tree op0,
13450 tree op1, bool *strict_overflow_p)
13452 bool sub_strict_overflow_p;
13453 switch (code)
13455 case POINTER_PLUS_EXPR:
13456 case PLUS_EXPR:
13457 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13459 /* With the presence of negative values it is hard
13460 to say something. */
13461 sub_strict_overflow_p = false;
13462 if (!tree_expr_nonnegative_warnv_p (op0,
13463 &sub_strict_overflow_p)
13464 || !tree_expr_nonnegative_warnv_p (op1,
13465 &sub_strict_overflow_p))
13466 return false;
13467 /* One of operands must be positive and the other non-negative. */
13468 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13469 overflows, on a twos-complement machine the sum of two
13470 nonnegative numbers can never be zero. */
13471 return (tree_expr_nonzero_warnv_p (op0,
13472 strict_overflow_p)
13473 || tree_expr_nonzero_warnv_p (op1,
13474 strict_overflow_p));
13476 break;
13478 case MULT_EXPR:
13479 if (TYPE_OVERFLOW_UNDEFINED (type))
13481 if (tree_expr_nonzero_warnv_p (op0,
13482 strict_overflow_p)
13483 && tree_expr_nonzero_warnv_p (op1,
13484 strict_overflow_p))
13486 *strict_overflow_p = true;
13487 return true;
13490 break;
13492 case MIN_EXPR:
13493 sub_strict_overflow_p = false;
13494 if (tree_expr_nonzero_warnv_p (op0,
13495 &sub_strict_overflow_p)
13496 && tree_expr_nonzero_warnv_p (op1,
13497 &sub_strict_overflow_p))
13499 if (sub_strict_overflow_p)
13500 *strict_overflow_p = true;
13502 break;
13504 case MAX_EXPR:
13505 sub_strict_overflow_p = false;
13506 if (tree_expr_nonzero_warnv_p (op0,
13507 &sub_strict_overflow_p))
13509 if (sub_strict_overflow_p)
13510 *strict_overflow_p = true;
13512 /* When both operands are nonzero, then MAX must be too. */
13513 if (tree_expr_nonzero_warnv_p (op1,
13514 strict_overflow_p))
13515 return true;
13517 /* MAX where operand 0 is positive is positive. */
13518 return tree_expr_nonnegative_warnv_p (op0,
13519 strict_overflow_p);
13521 /* MAX where operand 1 is positive is positive. */
13522 else if (tree_expr_nonzero_warnv_p (op1,
13523 &sub_strict_overflow_p)
13524 && tree_expr_nonnegative_warnv_p (op1,
13525 &sub_strict_overflow_p))
13527 if (sub_strict_overflow_p)
13528 *strict_overflow_p = true;
13529 return true;
13531 break;
13533 case BIT_IOR_EXPR:
13534 return (tree_expr_nonzero_warnv_p (op1,
13535 strict_overflow_p)
13536 || tree_expr_nonzero_warnv_p (op0,
13537 strict_overflow_p));
13539 default:
13540 break;
13543 return false;
13546 /* Return true when T is an address and is known to be nonzero.
13547 For floating point we further ensure that T is not denormal.
13548 Similar logic is present in nonzero_address in rtlanal.h.
13550 If the return value is based on the assumption that signed overflow
13551 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13552 change *STRICT_OVERFLOW_P. */
13554 bool
13555 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13557 bool sub_strict_overflow_p;
13558 switch (TREE_CODE (t))
13560 case INTEGER_CST:
13561 return !integer_zerop (t);
13563 case ADDR_EXPR:
13565 tree base = TREE_OPERAND (t, 0);
13567 if (!DECL_P (base))
13568 base = get_base_address (base);
13570 if (base && TREE_CODE (base) == TARGET_EXPR)
13571 base = TARGET_EXPR_SLOT (base);
13573 if (!base)
13574 return false;
13576 /* For objects in symbol table check if we know they are non-zero.
13577 Don't do anything for variables and functions before symtab is built;
13578 it is quite possible that they will be declared weak later. */
13579 int nonzero_addr = maybe_nonzero_address (base);
13580 if (nonzero_addr >= 0)
13581 return nonzero_addr;
13583 /* Function local objects are never NULL. */
13584 if (DECL_P (base)
13585 && (DECL_CONTEXT (base)
13586 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13587 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13588 return true;
13590 /* Constants are never weak. */
13591 if (CONSTANT_CLASS_P (base))
13592 return true;
13594 return false;
13597 case COND_EXPR:
13598 sub_strict_overflow_p = false;
13599 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13600 &sub_strict_overflow_p)
13601 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13602 &sub_strict_overflow_p))
13604 if (sub_strict_overflow_p)
13605 *strict_overflow_p = true;
13606 return true;
13608 break;
13610 default:
13611 break;
13613 return false;
13616 #define integer_valued_real_p(X) \
13617 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13619 #define RECURSE(X) \
13620 ((integer_valued_real_p) (X, depth + 1))
13622 /* Return true if the floating point result of (CODE OP0) has an
13623 integer value. We also allow +Inf, -Inf and NaN to be considered
13624 integer values. Return false for signaling NaN.
13626 DEPTH is the current nesting depth of the query. */
13628 bool
13629 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13631 switch (code)
13633 case FLOAT_EXPR:
13634 return true;
13636 case ABS_EXPR:
13637 return RECURSE (op0);
13639 CASE_CONVERT:
13641 tree type = TREE_TYPE (op0);
13642 if (TREE_CODE (type) == INTEGER_TYPE)
13643 return true;
13644 if (TREE_CODE (type) == REAL_TYPE)
13645 return RECURSE (op0);
13646 break;
13649 default:
13650 break;
13652 return false;
13655 /* Return true if the floating point result of (CODE OP0 OP1) has an
13656 integer value. We also allow +Inf, -Inf and NaN to be considered
13657 integer values. Return false for signaling NaN.
13659 DEPTH is the current nesting depth of the query. */
13661 bool
13662 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13664 switch (code)
13666 case PLUS_EXPR:
13667 case MINUS_EXPR:
13668 case MULT_EXPR:
13669 case MIN_EXPR:
13670 case MAX_EXPR:
13671 return RECURSE (op0) && RECURSE (op1);
13673 default:
13674 break;
13676 return false;
13679 /* Return true if the floating point result of calling FNDECL with arguments
13680 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13681 considered integer values. Return false for signaling NaN. If FNDECL
13682 takes fewer than 2 arguments, the remaining ARGn are null.
13684 DEPTH is the current nesting depth of the query. */
13686 bool
13687 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13689 switch (fn)
13691 CASE_CFN_CEIL:
13692 CASE_CFN_FLOOR:
13693 CASE_CFN_NEARBYINT:
13694 CASE_CFN_RINT:
13695 CASE_CFN_ROUND:
13696 CASE_CFN_TRUNC:
13697 return true;
13699 CASE_CFN_FMIN:
13700 CASE_CFN_FMAX:
13701 return RECURSE (arg0) && RECURSE (arg1);
13703 default:
13704 break;
13706 return false;
13709 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13710 has an integer value. We also allow +Inf, -Inf and NaN to be
13711 considered integer values. Return false for signaling NaN.
13713 DEPTH is the current nesting depth of the query. */
13715 bool
13716 integer_valued_real_single_p (tree t, int depth)
13718 switch (TREE_CODE (t))
13720 case REAL_CST:
13721 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13723 case COND_EXPR:
13724 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13726 case SSA_NAME:
13727 /* Limit the depth of recursion to avoid quadratic behavior.
13728 This is expected to catch almost all occurrences in practice.
13729 If this code misses important cases that unbounded recursion
13730 would not, passes that need this information could be revised
13731 to provide it through dataflow propagation. */
13732 return (!name_registered_for_update_p (t)
13733 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13734 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13735 depth));
13737 default:
13738 break;
13740 return false;
13743 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13744 has an integer value. We also allow +Inf, -Inf and NaN to be
13745 considered integer values. Return false for signaling NaN.
13747 DEPTH is the current nesting depth of the query. */
13749 static bool
13750 integer_valued_real_invalid_p (tree t, int depth)
13752 switch (TREE_CODE (t))
13754 case COMPOUND_EXPR:
13755 case MODIFY_EXPR:
13756 case BIND_EXPR:
13757 return RECURSE (TREE_OPERAND (t, 1));
13759 case SAVE_EXPR:
13760 return RECURSE (TREE_OPERAND (t, 0));
13762 default:
13763 break;
13765 return false;
13768 #undef RECURSE
13769 #undef integer_valued_real_p
13771 /* Return true if the floating point expression T has an integer value.
13772 We also allow +Inf, -Inf and NaN to be considered integer values.
13773 Return false for signaling NaN.
13775 DEPTH is the current nesting depth of the query. */
13777 bool
13778 integer_valued_real_p (tree t, int depth)
13780 if (t == error_mark_node)
13781 return false;
13783 tree_code code = TREE_CODE (t);
13784 switch (TREE_CODE_CLASS (code))
13786 case tcc_binary:
13787 case tcc_comparison:
13788 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13789 TREE_OPERAND (t, 1), depth);
13791 case tcc_unary:
13792 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13794 case tcc_constant:
13795 case tcc_declaration:
13796 case tcc_reference:
13797 return integer_valued_real_single_p (t, depth);
13799 default:
13800 break;
13803 switch (code)
13805 case COND_EXPR:
13806 case SSA_NAME:
13807 return integer_valued_real_single_p (t, depth);
13809 case CALL_EXPR:
13811 tree arg0 = (call_expr_nargs (t) > 0
13812 ? CALL_EXPR_ARG (t, 0)
13813 : NULL_TREE);
13814 tree arg1 = (call_expr_nargs (t) > 1
13815 ? CALL_EXPR_ARG (t, 1)
13816 : NULL_TREE);
13817 return integer_valued_real_call_p (get_call_combined_fn (t),
13818 arg0, arg1, depth);
13821 default:
13822 return integer_valued_real_invalid_p (t, depth);
13826 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13827 attempt to fold the expression to a constant without modifying TYPE,
13828 OP0 or OP1.
13830 If the expression could be simplified to a constant, then return
13831 the constant. If the expression would not be simplified to a
13832 constant, then return NULL_TREE. */
13834 tree
13835 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13837 tree tem = fold_binary (code, type, op0, op1);
13838 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13841 /* Given the components of a unary expression CODE, TYPE and OP0,
13842 attempt to fold the expression to a constant without modifying
13843 TYPE or OP0.
13845 If the expression could be simplified to a constant, then return
13846 the constant. If the expression would not be simplified to a
13847 constant, then return NULL_TREE. */
13849 tree
13850 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13852 tree tem = fold_unary (code, type, op0);
13853 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13856 /* If EXP represents referencing an element in a constant string
13857 (either via pointer arithmetic or array indexing), return the
13858 tree representing the value accessed, otherwise return NULL. */
13860 tree
13861 fold_read_from_constant_string (tree exp)
13863 if ((TREE_CODE (exp) == INDIRECT_REF
13864 || TREE_CODE (exp) == ARRAY_REF)
13865 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13867 tree exp1 = TREE_OPERAND (exp, 0);
13868 tree index;
13869 tree string;
13870 location_t loc = EXPR_LOCATION (exp);
13872 if (TREE_CODE (exp) == INDIRECT_REF)
13873 string = string_constant (exp1, &index);
13874 else
13876 tree low_bound = array_ref_low_bound (exp);
13877 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13879 /* Optimize the special-case of a zero lower bound.
13881 We convert the low_bound to sizetype to avoid some problems
13882 with constant folding. (E.g. suppose the lower bound is 1,
13883 and its mode is QI. Without the conversion,l (ARRAY
13884 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13885 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13886 if (! integer_zerop (low_bound))
13887 index = size_diffop_loc (loc, index,
13888 fold_convert_loc (loc, sizetype, low_bound));
13890 string = exp1;
13893 if (string
13894 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13895 && TREE_CODE (string) == STRING_CST
13896 && TREE_CODE (index) == INTEGER_CST
13897 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13898 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13899 == MODE_INT)
13900 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13901 return build_int_cst_type (TREE_TYPE (exp),
13902 (TREE_STRING_POINTER (string)
13903 [TREE_INT_CST_LOW (index)]));
13905 return NULL;
13908 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13909 an integer constant, real, or fixed-point constant.
13911 TYPE is the type of the result. */
13913 static tree
13914 fold_negate_const (tree arg0, tree type)
13916 tree t = NULL_TREE;
13918 switch (TREE_CODE (arg0))
13920 case INTEGER_CST:
13922 bool overflow;
13923 wide_int val = wi::neg (arg0, &overflow);
13924 t = force_fit_type (type, val, 1,
13925 (overflow | TREE_OVERFLOW (arg0))
13926 && !TYPE_UNSIGNED (type));
13927 break;
13930 case REAL_CST:
13931 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13932 break;
13934 case FIXED_CST:
13936 FIXED_VALUE_TYPE f;
13937 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13938 &(TREE_FIXED_CST (arg0)), NULL,
13939 TYPE_SATURATING (type));
13940 t = build_fixed (type, f);
13941 /* Propagate overflow flags. */
13942 if (overflow_p | TREE_OVERFLOW (arg0))
13943 TREE_OVERFLOW (t) = 1;
13944 break;
13947 default:
13948 gcc_unreachable ();
13951 return t;
13954 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13955 an integer constant or real constant.
13957 TYPE is the type of the result. */
13959 tree
13960 fold_abs_const (tree arg0, tree type)
13962 tree t = NULL_TREE;
13964 switch (TREE_CODE (arg0))
13966 case INTEGER_CST:
13968 /* If the value is unsigned or non-negative, then the absolute value
13969 is the same as the ordinary value. */
13970 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13971 t = arg0;
13973 /* If the value is negative, then the absolute value is
13974 its negation. */
13975 else
13977 bool overflow;
13978 wide_int val = wi::neg (arg0, &overflow);
13979 t = force_fit_type (type, val, -1,
13980 overflow | TREE_OVERFLOW (arg0));
13983 break;
13985 case REAL_CST:
13986 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13987 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13988 else
13989 t = arg0;
13990 break;
13992 default:
13993 gcc_unreachable ();
13996 return t;
13999 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14000 constant. TYPE is the type of the result. */
14002 static tree
14003 fold_not_const (const_tree arg0, tree type)
14005 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14007 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
14010 /* Given CODE, a relational operator, the target type, TYPE and two
14011 constant operands OP0 and OP1, return the result of the
14012 relational operation. If the result is not a compile time
14013 constant, then return NULL_TREE. */
14015 static tree
14016 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14018 int result, invert;
14020 /* From here on, the only cases we handle are when the result is
14021 known to be a constant. */
14023 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14025 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14026 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14028 /* Handle the cases where either operand is a NaN. */
14029 if (real_isnan (c0) || real_isnan (c1))
14031 switch (code)
14033 case EQ_EXPR:
14034 case ORDERED_EXPR:
14035 result = 0;
14036 break;
14038 case NE_EXPR:
14039 case UNORDERED_EXPR:
14040 case UNLT_EXPR:
14041 case UNLE_EXPR:
14042 case UNGT_EXPR:
14043 case UNGE_EXPR:
14044 case UNEQ_EXPR:
14045 result = 1;
14046 break;
14048 case LT_EXPR:
14049 case LE_EXPR:
14050 case GT_EXPR:
14051 case GE_EXPR:
14052 case LTGT_EXPR:
14053 if (flag_trapping_math)
14054 return NULL_TREE;
14055 result = 0;
14056 break;
14058 default:
14059 gcc_unreachable ();
14062 return constant_boolean_node (result, type);
14065 return constant_boolean_node (real_compare (code, c0, c1), type);
14068 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14070 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14071 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14072 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14075 /* Handle equality/inequality of complex constants. */
14076 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14078 tree rcond = fold_relational_const (code, type,
14079 TREE_REALPART (op0),
14080 TREE_REALPART (op1));
14081 tree icond = fold_relational_const (code, type,
14082 TREE_IMAGPART (op0),
14083 TREE_IMAGPART (op1));
14084 if (code == EQ_EXPR)
14085 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14086 else if (code == NE_EXPR)
14087 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14088 else
14089 return NULL_TREE;
14092 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
14094 if (!VECTOR_TYPE_P (type))
14096 /* Have vector comparison with scalar boolean result. */
14097 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
14098 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
14099 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
14101 tree elem0 = VECTOR_CST_ELT (op0, i);
14102 tree elem1 = VECTOR_CST_ELT (op1, i);
14103 tree tmp = fold_relational_const (code, type, elem0, elem1);
14104 if (tmp == NULL_TREE)
14105 return NULL_TREE;
14106 if (integer_zerop (tmp))
14107 return constant_boolean_node (false, type);
14109 return constant_boolean_node (true, type);
14111 unsigned count = VECTOR_CST_NELTS (op0);
14112 tree *elts = XALLOCAVEC (tree, count);
14113 gcc_assert (VECTOR_CST_NELTS (op1) == count
14114 && TYPE_VECTOR_SUBPARTS (type) == count);
14116 for (unsigned i = 0; i < count; i++)
14118 tree elem_type = TREE_TYPE (type);
14119 tree elem0 = VECTOR_CST_ELT (op0, i);
14120 tree elem1 = VECTOR_CST_ELT (op1, i);
14122 tree tem = fold_relational_const (code, elem_type,
14123 elem0, elem1);
14125 if (tem == NULL_TREE)
14126 return NULL_TREE;
14128 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
14131 return build_vector (type, elts);
14134 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14136 To compute GT, swap the arguments and do LT.
14137 To compute GE, do LT and invert the result.
14138 To compute LE, swap the arguments, do LT and invert the result.
14139 To compute NE, do EQ and invert the result.
14141 Therefore, the code below must handle only EQ and LT. */
14143 if (code == LE_EXPR || code == GT_EXPR)
14145 std::swap (op0, op1);
14146 code = swap_tree_comparison (code);
14149 /* Note that it is safe to invert for real values here because we
14150 have already handled the one case that it matters. */
14152 invert = 0;
14153 if (code == NE_EXPR || code == GE_EXPR)
14155 invert = 1;
14156 code = invert_tree_comparison (code, false);
14159 /* Compute a result for LT or EQ if args permit;
14160 Otherwise return T. */
14161 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14163 if (code == EQ_EXPR)
14164 result = tree_int_cst_equal (op0, op1);
14165 else
14166 result = tree_int_cst_lt (op0, op1);
14168 else
14169 return NULL_TREE;
14171 if (invert)
14172 result ^= 1;
14173 return constant_boolean_node (result, type);
14176 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14177 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14178 itself. */
14180 tree
14181 fold_build_cleanup_point_expr (tree type, tree expr)
14183 /* If the expression does not have side effects then we don't have to wrap
14184 it with a cleanup point expression. */
14185 if (!TREE_SIDE_EFFECTS (expr))
14186 return expr;
14188 /* If the expression is a return, check to see if the expression inside the
14189 return has no side effects or the right hand side of the modify expression
14190 inside the return. If either don't have side effects set we don't need to
14191 wrap the expression in a cleanup point expression. Note we don't check the
14192 left hand side of the modify because it should always be a return decl. */
14193 if (TREE_CODE (expr) == RETURN_EXPR)
14195 tree op = TREE_OPERAND (expr, 0);
14196 if (!op || !TREE_SIDE_EFFECTS (op))
14197 return expr;
14198 op = TREE_OPERAND (op, 1);
14199 if (!TREE_SIDE_EFFECTS (op))
14200 return expr;
14203 return build1 (CLEANUP_POINT_EXPR, type, expr);
14206 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14207 of an indirection through OP0, or NULL_TREE if no simplification is
14208 possible. */
14210 tree
14211 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14213 tree sub = op0;
14214 tree subtype;
14216 STRIP_NOPS (sub);
14217 subtype = TREE_TYPE (sub);
14218 if (!POINTER_TYPE_P (subtype))
14219 return NULL_TREE;
14221 if (TREE_CODE (sub) == ADDR_EXPR)
14223 tree op = TREE_OPERAND (sub, 0);
14224 tree optype = TREE_TYPE (op);
14225 /* *&CONST_DECL -> to the value of the const decl. */
14226 if (TREE_CODE (op) == CONST_DECL)
14227 return DECL_INITIAL (op);
14228 /* *&p => p; make sure to handle *&"str"[cst] here. */
14229 if (type == optype)
14231 tree fop = fold_read_from_constant_string (op);
14232 if (fop)
14233 return fop;
14234 else
14235 return op;
14237 /* *(foo *)&fooarray => fooarray[0] */
14238 else if (TREE_CODE (optype) == ARRAY_TYPE
14239 && type == TREE_TYPE (optype)
14240 && (!in_gimple_form
14241 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14243 tree type_domain = TYPE_DOMAIN (optype);
14244 tree min_val = size_zero_node;
14245 if (type_domain && TYPE_MIN_VALUE (type_domain))
14246 min_val = TYPE_MIN_VALUE (type_domain);
14247 if (in_gimple_form
14248 && TREE_CODE (min_val) != INTEGER_CST)
14249 return NULL_TREE;
14250 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14251 NULL_TREE, NULL_TREE);
14253 /* *(foo *)&complexfoo => __real__ complexfoo */
14254 else if (TREE_CODE (optype) == COMPLEX_TYPE
14255 && type == TREE_TYPE (optype))
14256 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14257 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14258 else if (TREE_CODE (optype) == VECTOR_TYPE
14259 && type == TREE_TYPE (optype))
14261 tree part_width = TYPE_SIZE (type);
14262 tree index = bitsize_int (0);
14263 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14267 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14268 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14270 tree op00 = TREE_OPERAND (sub, 0);
14271 tree op01 = TREE_OPERAND (sub, 1);
14273 STRIP_NOPS (op00);
14274 if (TREE_CODE (op00) == ADDR_EXPR)
14276 tree op00type;
14277 op00 = TREE_OPERAND (op00, 0);
14278 op00type = TREE_TYPE (op00);
14280 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14281 if (TREE_CODE (op00type) == VECTOR_TYPE
14282 && type == TREE_TYPE (op00type))
14284 tree part_width = TYPE_SIZE (type);
14285 unsigned HOST_WIDE_INT max_offset
14286 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14287 * TYPE_VECTOR_SUBPARTS (op00type));
14288 if (tree_int_cst_sign_bit (op01) == 0
14289 && compare_tree_int (op01, max_offset) == -1)
14291 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14292 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14293 tree index = bitsize_int (indexi);
14294 return fold_build3_loc (loc,
14295 BIT_FIELD_REF, type, op00,
14296 part_width, index);
14299 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14300 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14301 && type == TREE_TYPE (op00type))
14303 tree size = TYPE_SIZE_UNIT (type);
14304 if (tree_int_cst_equal (size, op01))
14305 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14307 /* ((foo *)&fooarray)[1] => fooarray[1] */
14308 else if (TREE_CODE (op00type) == ARRAY_TYPE
14309 && type == TREE_TYPE (op00type))
14311 tree type_domain = TYPE_DOMAIN (op00type);
14312 tree min_val = size_zero_node;
14313 if (type_domain && TYPE_MIN_VALUE (type_domain))
14314 min_val = TYPE_MIN_VALUE (type_domain);
14315 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14316 TYPE_SIZE_UNIT (type));
14317 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14318 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14319 NULL_TREE, NULL_TREE);
14324 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14325 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14326 && type == TREE_TYPE (TREE_TYPE (subtype))
14327 && (!in_gimple_form
14328 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14330 tree type_domain;
14331 tree min_val = size_zero_node;
14332 sub = build_fold_indirect_ref_loc (loc, sub);
14333 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14334 if (type_domain && TYPE_MIN_VALUE (type_domain))
14335 min_val = TYPE_MIN_VALUE (type_domain);
14336 if (in_gimple_form
14337 && TREE_CODE (min_val) != INTEGER_CST)
14338 return NULL_TREE;
14339 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14340 NULL_TREE);
14343 return NULL_TREE;
14346 /* Builds an expression for an indirection through T, simplifying some
14347 cases. */
14349 tree
14350 build_fold_indirect_ref_loc (location_t loc, tree t)
14352 tree type = TREE_TYPE (TREE_TYPE (t));
14353 tree sub = fold_indirect_ref_1 (loc, type, t);
14355 if (sub)
14356 return sub;
14358 return build1_loc (loc, INDIRECT_REF, type, t);
14361 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14363 tree
14364 fold_indirect_ref_loc (location_t loc, tree t)
14366 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14368 if (sub)
14369 return sub;
14370 else
14371 return t;
14374 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14375 whose result is ignored. The type of the returned tree need not be
14376 the same as the original expression. */
14378 tree
14379 fold_ignored_result (tree t)
14381 if (!TREE_SIDE_EFFECTS (t))
14382 return integer_zero_node;
14384 for (;;)
14385 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14387 case tcc_unary:
14388 t = TREE_OPERAND (t, 0);
14389 break;
14391 case tcc_binary:
14392 case tcc_comparison:
14393 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14394 t = TREE_OPERAND (t, 0);
14395 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14396 t = TREE_OPERAND (t, 1);
14397 else
14398 return t;
14399 break;
14401 case tcc_expression:
14402 switch (TREE_CODE (t))
14404 case COMPOUND_EXPR:
14405 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14406 return t;
14407 t = TREE_OPERAND (t, 0);
14408 break;
14410 case COND_EXPR:
14411 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14412 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14413 return t;
14414 t = TREE_OPERAND (t, 0);
14415 break;
14417 default:
14418 return t;
14420 break;
14422 default:
14423 return t;
14427 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14429 tree
14430 round_up_loc (location_t loc, tree value, unsigned int divisor)
14432 tree div = NULL_TREE;
14434 if (divisor == 1)
14435 return value;
14437 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14438 have to do anything. Only do this when we are not given a const,
14439 because in that case, this check is more expensive than just
14440 doing it. */
14441 if (TREE_CODE (value) != INTEGER_CST)
14443 div = build_int_cst (TREE_TYPE (value), divisor);
14445 if (multiple_of_p (TREE_TYPE (value), value, div))
14446 return value;
14449 /* If divisor is a power of two, simplify this to bit manipulation. */
14450 if (divisor == (divisor & -divisor))
14452 if (TREE_CODE (value) == INTEGER_CST)
14454 wide_int val = value;
14455 bool overflow_p;
14457 if ((val & (divisor - 1)) == 0)
14458 return value;
14460 overflow_p = TREE_OVERFLOW (value);
14461 val += divisor - 1;
14462 val &= - (int) divisor;
14463 if (val == 0)
14464 overflow_p = true;
14466 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14468 else
14470 tree t;
14472 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14473 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14474 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14475 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14478 else
14480 if (!div)
14481 div = build_int_cst (TREE_TYPE (value), divisor);
14482 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14483 value = size_binop_loc (loc, MULT_EXPR, value, div);
14486 return value;
14489 /* Likewise, but round down. */
14491 tree
14492 round_down_loc (location_t loc, tree value, int divisor)
14494 tree div = NULL_TREE;
14496 gcc_assert (divisor > 0);
14497 if (divisor == 1)
14498 return value;
14500 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14501 have to do anything. Only do this when we are not given a const,
14502 because in that case, this check is more expensive than just
14503 doing it. */
14504 if (TREE_CODE (value) != INTEGER_CST)
14506 div = build_int_cst (TREE_TYPE (value), divisor);
14508 if (multiple_of_p (TREE_TYPE (value), value, div))
14509 return value;
14512 /* If divisor is a power of two, simplify this to bit manipulation. */
14513 if (divisor == (divisor & -divisor))
14515 tree t;
14517 t = build_int_cst (TREE_TYPE (value), -divisor);
14518 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14520 else
14522 if (!div)
14523 div = build_int_cst (TREE_TYPE (value), divisor);
14524 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14525 value = size_binop_loc (loc, MULT_EXPR, value, div);
14528 return value;
14531 /* Returns the pointer to the base of the object addressed by EXP and
14532 extracts the information about the offset of the access, storing it
14533 to PBITPOS and POFFSET. */
14535 static tree
14536 split_address_to_core_and_offset (tree exp,
14537 HOST_WIDE_INT *pbitpos, tree *poffset)
14539 tree core;
14540 machine_mode mode;
14541 int unsignedp, reversep, volatilep;
14542 HOST_WIDE_INT bitsize;
14543 location_t loc = EXPR_LOCATION (exp);
14545 if (TREE_CODE (exp) == ADDR_EXPR)
14547 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14548 poffset, &mode, &unsignedp, &reversep,
14549 &volatilep, false);
14550 core = build_fold_addr_expr_loc (loc, core);
14552 else
14554 core = exp;
14555 *pbitpos = 0;
14556 *poffset = NULL_TREE;
14559 return core;
14562 /* Returns true if addresses of E1 and E2 differ by a constant, false
14563 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14565 bool
14566 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14568 tree core1, core2;
14569 HOST_WIDE_INT bitpos1, bitpos2;
14570 tree toffset1, toffset2, tdiff, type;
14572 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14573 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14575 if (bitpos1 % BITS_PER_UNIT != 0
14576 || bitpos2 % BITS_PER_UNIT != 0
14577 || !operand_equal_p (core1, core2, 0))
14578 return false;
14580 if (toffset1 && toffset2)
14582 type = TREE_TYPE (toffset1);
14583 if (type != TREE_TYPE (toffset2))
14584 toffset2 = fold_convert (type, toffset2);
14586 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14587 if (!cst_and_fits_in_hwi (tdiff))
14588 return false;
14590 *diff = int_cst_value (tdiff);
14592 else if (toffset1 || toffset2)
14594 /* If only one of the offsets is non-constant, the difference cannot
14595 be a constant. */
14596 return false;
14598 else
14599 *diff = 0;
14601 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14602 return true;
14605 /* Return OFF converted to a pointer offset type suitable as offset for
14606 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14607 tree
14608 convert_to_ptrofftype_loc (location_t loc, tree off)
14610 return fold_convert_loc (loc, sizetype, off);
14613 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14614 tree
14615 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14617 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14618 ptr, convert_to_ptrofftype_loc (loc, off));
14621 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14622 tree
14623 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14625 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14626 ptr, size_int (off));
14629 /* Return a char pointer for a C string if it is a string constant
14630 or sum of string constant and integer constant. */
14632 const char *
14633 c_getstr (tree src)
14635 tree offset_node;
14637 src = string_constant (src, &offset_node);
14638 if (src == 0)
14639 return 0;
14641 if (offset_node == 0)
14642 return TREE_STRING_POINTER (src);
14643 else if (!tree_fits_uhwi_p (offset_node)
14644 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
14645 return 0;
14647 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);