2016-09-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / fold-const.c
bloba6b7ec7dfeced0a019ce2b7d4c2ee6abde25e84b
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-vrp.h"
79 #include "tree-ssanames.h"
80 #include "selftest.h"
82 #ifndef LOAD_EXTEND_OP
83 #define LOAD_EXTEND_OP(M) UNKNOWN
84 #endif
86 /* Nonzero if we are folding constants inside an initializer; zero
87 otherwise. */
88 int folding_initializer = 0;
90 /* The following constants represent a bit based encoding of GCC's
91 comparison operators. This encoding simplifies transformations
92 on relational comparison operators, such as AND and OR. */
93 enum comparison_code {
94 COMPCODE_FALSE = 0,
95 COMPCODE_LT = 1,
96 COMPCODE_EQ = 2,
97 COMPCODE_LE = 3,
98 COMPCODE_GT = 4,
99 COMPCODE_LTGT = 5,
100 COMPCODE_GE = 6,
101 COMPCODE_ORD = 7,
102 COMPCODE_UNORD = 8,
103 COMPCODE_UNLT = 9,
104 COMPCODE_UNEQ = 10,
105 COMPCODE_UNLE = 11,
106 COMPCODE_UNGT = 12,
107 COMPCODE_NE = 13,
108 COMPCODE_UNGE = 14,
109 COMPCODE_TRUE = 15
112 static bool negate_expr_p (tree);
113 static tree negate_expr (tree);
114 static tree split_tree (location_t, tree, tree, enum tree_code,
115 tree *, tree *, tree *, int);
116 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
117 static enum comparison_code comparison_to_compcode (enum tree_code);
118 static enum tree_code compcode_to_comparison (enum comparison_code);
119 static int operand_equal_for_comparison_p (tree, tree, tree);
120 static int twoval_comparison_p (tree, tree *, tree *, int *);
121 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
122 static tree optimize_bit_field_compare (location_t, enum tree_code,
123 tree, tree, tree);
124 static int simple_operand_p (const_tree);
125 static bool simple_operand_p_2 (tree);
126 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
127 static tree range_predecessor (tree);
128 static tree range_successor (tree);
129 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
130 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
131 static tree unextend (tree, int, int, 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 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 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 && ! integer_pow2p (TREE_OPERAND (t, 0)))
466 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
467 && ! integer_pow2p (TREE_OPERAND (t, 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 = wi::to_wide (parg2, TYPE_PRECISION (type));
964 switch (code)
966 case BIT_IOR_EXPR:
967 res = wi::bit_or (arg1, arg2);
968 break;
970 case BIT_XOR_EXPR:
971 res = wi::bit_xor (arg1, arg2);
972 break;
974 case BIT_AND_EXPR:
975 res = wi::bit_and (arg1, arg2);
976 break;
978 case RSHIFT_EXPR:
979 case LSHIFT_EXPR:
980 if (wi::neg_p (arg2))
982 arg2 = -arg2;
983 if (code == RSHIFT_EXPR)
984 code = LSHIFT_EXPR;
985 else
986 code = RSHIFT_EXPR;
989 if (code == RSHIFT_EXPR)
990 /* It's unclear from the C standard whether shifts can overflow.
991 The following code ignores overflow; perhaps a C standard
992 interpretation ruling is needed. */
993 res = wi::rshift (arg1, arg2, sign);
994 else
995 res = wi::lshift (arg1, arg2);
996 break;
998 case RROTATE_EXPR:
999 case LROTATE_EXPR:
1000 if (wi::neg_p (arg2))
1002 arg2 = -arg2;
1003 if (code == RROTATE_EXPR)
1004 code = LROTATE_EXPR;
1005 else
1006 code = RROTATE_EXPR;
1009 if (code == RROTATE_EXPR)
1010 res = wi::rrotate (arg1, arg2);
1011 else
1012 res = wi::lrotate (arg1, arg2);
1013 break;
1015 case PLUS_EXPR:
1016 res = wi::add (arg1, arg2, sign, &overflow);
1017 break;
1019 case MINUS_EXPR:
1020 res = wi::sub (arg1, arg2, sign, &overflow);
1021 break;
1023 case MULT_EXPR:
1024 res = wi::mul (arg1, arg2, sign, &overflow);
1025 break;
1027 case MULT_HIGHPART_EXPR:
1028 res = wi::mul_high (arg1, arg2, sign);
1029 break;
1031 case TRUNC_DIV_EXPR:
1032 case EXACT_DIV_EXPR:
1033 if (arg2 == 0)
1034 return NULL_TREE;
1035 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1036 break;
1038 case FLOOR_DIV_EXPR:
1039 if (arg2 == 0)
1040 return NULL_TREE;
1041 res = wi::div_floor (arg1, arg2, sign, &overflow);
1042 break;
1044 case CEIL_DIV_EXPR:
1045 if (arg2 == 0)
1046 return NULL_TREE;
1047 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1048 break;
1050 case ROUND_DIV_EXPR:
1051 if (arg2 == 0)
1052 return NULL_TREE;
1053 res = wi::div_round (arg1, arg2, sign, &overflow);
1054 break;
1056 case TRUNC_MOD_EXPR:
1057 if (arg2 == 0)
1058 return NULL_TREE;
1059 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1060 break;
1062 case FLOOR_MOD_EXPR:
1063 if (arg2 == 0)
1064 return NULL_TREE;
1065 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1066 break;
1068 case CEIL_MOD_EXPR:
1069 if (arg2 == 0)
1070 return NULL_TREE;
1071 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1072 break;
1074 case ROUND_MOD_EXPR:
1075 if (arg2 == 0)
1076 return NULL_TREE;
1077 res = wi::mod_round (arg1, arg2, sign, &overflow);
1078 break;
1080 case MIN_EXPR:
1081 res = wi::min (arg1, arg2, sign);
1082 break;
1084 case MAX_EXPR:
1085 res = wi::max (arg1, arg2, sign);
1086 break;
1088 default:
1089 return NULL_TREE;
1092 t = force_fit_type (type, res, overflowable,
1093 (((sign == SIGNED || overflowable == -1)
1094 && overflow)
1095 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1097 return t;
1100 tree
1101 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1103 return int_const_binop_1 (code, arg1, arg2, 1);
1106 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1107 constant. We assume ARG1 and ARG2 have the same data type, or at least
1108 are the same kind of constant and the same machine mode. Return zero if
1109 combining the constants is not allowed in the current operating mode. */
1111 static tree
1112 const_binop (enum tree_code code, tree arg1, tree arg2)
1114 /* Sanity check for the recursive cases. */
1115 if (!arg1 || !arg2)
1116 return NULL_TREE;
1118 STRIP_NOPS (arg1);
1119 STRIP_NOPS (arg2);
1121 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1123 if (code == POINTER_PLUS_EXPR)
1124 return int_const_binop (PLUS_EXPR,
1125 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1127 return int_const_binop (code, arg1, arg2);
1130 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1132 machine_mode mode;
1133 REAL_VALUE_TYPE d1;
1134 REAL_VALUE_TYPE d2;
1135 REAL_VALUE_TYPE value;
1136 REAL_VALUE_TYPE result;
1137 bool inexact;
1138 tree t, type;
1140 /* The following codes are handled by real_arithmetic. */
1141 switch (code)
1143 case PLUS_EXPR:
1144 case MINUS_EXPR:
1145 case MULT_EXPR:
1146 case RDIV_EXPR:
1147 case MIN_EXPR:
1148 case MAX_EXPR:
1149 break;
1151 default:
1152 return NULL_TREE;
1155 d1 = TREE_REAL_CST (arg1);
1156 d2 = TREE_REAL_CST (arg2);
1158 type = TREE_TYPE (arg1);
1159 mode = TYPE_MODE (type);
1161 /* Don't perform operation if we honor signaling NaNs and
1162 either operand is a signaling NaN. */
1163 if (HONOR_SNANS (mode)
1164 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1165 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1166 return NULL_TREE;
1168 /* Don't perform operation if it would raise a division
1169 by zero exception. */
1170 if (code == RDIV_EXPR
1171 && real_equal (&d2, &dconst0)
1172 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1173 return NULL_TREE;
1175 /* If either operand is a NaN, just return it. Otherwise, set up
1176 for floating-point trap; we return an overflow. */
1177 if (REAL_VALUE_ISNAN (d1))
1179 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1180 is off. */
1181 d1.signalling = 0;
1182 t = build_real (type, d1);
1183 return t;
1185 else if (REAL_VALUE_ISNAN (d2))
1187 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1188 is off. */
1189 d2.signalling = 0;
1190 t = build_real (type, d2);
1191 return t;
1194 inexact = real_arithmetic (&value, code, &d1, &d2);
1195 real_convert (&result, mode, &value);
1197 /* Don't constant fold this floating point operation if
1198 the result has overflowed and flag_trapping_math. */
1199 if (flag_trapping_math
1200 && MODE_HAS_INFINITIES (mode)
1201 && REAL_VALUE_ISINF (result)
1202 && !REAL_VALUE_ISINF (d1)
1203 && !REAL_VALUE_ISINF (d2))
1204 return NULL_TREE;
1206 /* Don't constant fold this floating point operation if the
1207 result may dependent upon the run-time rounding mode and
1208 flag_rounding_math is set, or if GCC's software emulation
1209 is unable to accurately represent the result. */
1210 if ((flag_rounding_math
1211 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1212 && (inexact || !real_identical (&result, &value)))
1213 return NULL_TREE;
1215 t = build_real (type, result);
1217 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1218 return t;
1221 if (TREE_CODE (arg1) == FIXED_CST)
1223 FIXED_VALUE_TYPE f1;
1224 FIXED_VALUE_TYPE f2;
1225 FIXED_VALUE_TYPE result;
1226 tree t, type;
1227 int sat_p;
1228 bool overflow_p;
1230 /* The following codes are handled by fixed_arithmetic. */
1231 switch (code)
1233 case PLUS_EXPR:
1234 case MINUS_EXPR:
1235 case MULT_EXPR:
1236 case TRUNC_DIV_EXPR:
1237 if (TREE_CODE (arg2) != FIXED_CST)
1238 return NULL_TREE;
1239 f2 = TREE_FIXED_CST (arg2);
1240 break;
1242 case LSHIFT_EXPR:
1243 case RSHIFT_EXPR:
1245 if (TREE_CODE (arg2) != INTEGER_CST)
1246 return NULL_TREE;
1247 wide_int w2 = arg2;
1248 f2.data.high = w2.elt (1);
1249 f2.data.low = w2.elt (0);
1250 f2.mode = SImode;
1252 break;
1254 default:
1255 return NULL_TREE;
1258 f1 = TREE_FIXED_CST (arg1);
1259 type = TREE_TYPE (arg1);
1260 sat_p = TYPE_SATURATING (type);
1261 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1262 t = build_fixed (type, result);
1263 /* Propagate overflow flags. */
1264 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1265 TREE_OVERFLOW (t) = 1;
1266 return t;
1269 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1271 tree type = TREE_TYPE (arg1);
1272 tree r1 = TREE_REALPART (arg1);
1273 tree i1 = TREE_IMAGPART (arg1);
1274 tree r2 = TREE_REALPART (arg2);
1275 tree i2 = TREE_IMAGPART (arg2);
1276 tree real, imag;
1278 switch (code)
1280 case PLUS_EXPR:
1281 case MINUS_EXPR:
1282 real = const_binop (code, r1, r2);
1283 imag = const_binop (code, i1, i2);
1284 break;
1286 case MULT_EXPR:
1287 if (COMPLEX_FLOAT_TYPE_P (type))
1288 return do_mpc_arg2 (arg1, arg2, type,
1289 /* do_nonfinite= */ folding_initializer,
1290 mpc_mul);
1292 real = const_binop (MINUS_EXPR,
1293 const_binop (MULT_EXPR, r1, r2),
1294 const_binop (MULT_EXPR, i1, i2));
1295 imag = const_binop (PLUS_EXPR,
1296 const_binop (MULT_EXPR, r1, i2),
1297 const_binop (MULT_EXPR, i1, r2));
1298 break;
1300 case RDIV_EXPR:
1301 if (COMPLEX_FLOAT_TYPE_P (type))
1302 return do_mpc_arg2 (arg1, arg2, type,
1303 /* do_nonfinite= */ folding_initializer,
1304 mpc_div);
1305 /* Fallthru. */
1306 case TRUNC_DIV_EXPR:
1307 case CEIL_DIV_EXPR:
1308 case FLOOR_DIV_EXPR:
1309 case ROUND_DIV_EXPR:
1310 if (flag_complex_method == 0)
1312 /* Keep this algorithm in sync with
1313 tree-complex.c:expand_complex_div_straight().
1315 Expand complex division to scalars, straightforward algorithm.
1316 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1317 t = br*br + bi*bi
1319 tree magsquared
1320 = const_binop (PLUS_EXPR,
1321 const_binop (MULT_EXPR, r2, r2),
1322 const_binop (MULT_EXPR, i2, i2));
1323 tree t1
1324 = const_binop (PLUS_EXPR,
1325 const_binop (MULT_EXPR, r1, r2),
1326 const_binop (MULT_EXPR, i1, i2));
1327 tree t2
1328 = const_binop (MINUS_EXPR,
1329 const_binop (MULT_EXPR, i1, r2),
1330 const_binop (MULT_EXPR, r1, i2));
1332 real = const_binop (code, t1, magsquared);
1333 imag = const_binop (code, t2, magsquared);
1335 else
1337 /* Keep this algorithm in sync with
1338 tree-complex.c:expand_complex_div_wide().
1340 Expand complex division to scalars, modified algorithm to minimize
1341 overflow with wide input ranges. */
1342 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1343 fold_abs_const (r2, TREE_TYPE (type)),
1344 fold_abs_const (i2, TREE_TYPE (type)));
1346 if (integer_nonzerop (compare))
1348 /* In the TRUE branch, we compute
1349 ratio = br/bi;
1350 div = (br * ratio) + bi;
1351 tr = (ar * ratio) + ai;
1352 ti = (ai * ratio) - ar;
1353 tr = tr / div;
1354 ti = ti / div; */
1355 tree ratio = const_binop (code, r2, i2);
1356 tree div = const_binop (PLUS_EXPR, i2,
1357 const_binop (MULT_EXPR, r2, ratio));
1358 real = const_binop (MULT_EXPR, r1, ratio);
1359 real = const_binop (PLUS_EXPR, real, i1);
1360 real = const_binop (code, real, div);
1362 imag = const_binop (MULT_EXPR, i1, ratio);
1363 imag = const_binop (MINUS_EXPR, imag, r1);
1364 imag = const_binop (code, imag, div);
1366 else
1368 /* In the FALSE branch, we compute
1369 ratio = d/c;
1370 divisor = (d * ratio) + c;
1371 tr = (b * ratio) + a;
1372 ti = b - (a * ratio);
1373 tr = tr / div;
1374 ti = ti / div; */
1375 tree ratio = const_binop (code, i2, r2);
1376 tree div = const_binop (PLUS_EXPR, r2,
1377 const_binop (MULT_EXPR, i2, ratio));
1379 real = const_binop (MULT_EXPR, i1, ratio);
1380 real = const_binop (PLUS_EXPR, real, r1);
1381 real = const_binop (code, real, div);
1383 imag = const_binop (MULT_EXPR, r1, ratio);
1384 imag = const_binop (MINUS_EXPR, i1, imag);
1385 imag = const_binop (code, imag, div);
1388 break;
1390 default:
1391 return NULL_TREE;
1394 if (real && imag)
1395 return build_complex (type, real, imag);
1398 if (TREE_CODE (arg1) == VECTOR_CST
1399 && TREE_CODE (arg2) == VECTOR_CST)
1401 tree type = TREE_TYPE (arg1);
1402 int count = TYPE_VECTOR_SUBPARTS (type), i;
1403 tree *elts = XALLOCAVEC (tree, count);
1405 for (i = 0; i < count; i++)
1407 tree elem1 = VECTOR_CST_ELT (arg1, i);
1408 tree elem2 = VECTOR_CST_ELT (arg2, i);
1410 elts[i] = const_binop (code, elem1, elem2);
1412 /* It is possible that const_binop cannot handle the given
1413 code and return NULL_TREE */
1414 if (elts[i] == NULL_TREE)
1415 return NULL_TREE;
1418 return build_vector (type, elts);
1421 /* Shifts allow a scalar offset for a vector. */
1422 if (TREE_CODE (arg1) == VECTOR_CST
1423 && TREE_CODE (arg2) == INTEGER_CST)
1425 tree type = TREE_TYPE (arg1);
1426 int count = TYPE_VECTOR_SUBPARTS (type), i;
1427 tree *elts = XALLOCAVEC (tree, count);
1429 for (i = 0; i < count; i++)
1431 tree elem1 = VECTOR_CST_ELT (arg1, i);
1433 elts[i] = const_binop (code, elem1, arg2);
1435 /* It is possible that const_binop cannot handle the given
1436 code and return NULL_TREE. */
1437 if (elts[i] == NULL_TREE)
1438 return NULL_TREE;
1441 return build_vector (type, elts);
1443 return NULL_TREE;
1446 /* Overload that adds a TYPE parameter to be able to dispatch
1447 to fold_relational_const. */
1449 tree
1450 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1452 if (TREE_CODE_CLASS (code) == tcc_comparison)
1453 return fold_relational_const (code, type, arg1, arg2);
1455 /* ??? Until we make the const_binop worker take the type of the
1456 result as argument put those cases that need it here. */
1457 switch (code)
1459 case COMPLEX_EXPR:
1460 if ((TREE_CODE (arg1) == REAL_CST
1461 && TREE_CODE (arg2) == REAL_CST)
1462 || (TREE_CODE (arg1) == INTEGER_CST
1463 && TREE_CODE (arg2) == INTEGER_CST))
1464 return build_complex (type, arg1, arg2);
1465 return NULL_TREE;
1467 case VEC_PACK_TRUNC_EXPR:
1468 case VEC_PACK_FIX_TRUNC_EXPR:
1470 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1471 tree *elts;
1473 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1474 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1475 if (TREE_CODE (arg1) != VECTOR_CST
1476 || TREE_CODE (arg2) != VECTOR_CST)
1477 return NULL_TREE;
1479 elts = XALLOCAVEC (tree, nelts);
1480 if (!vec_cst_ctor_to_array (arg1, elts)
1481 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1482 return NULL_TREE;
1484 for (i = 0; i < nelts; i++)
1486 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1487 ? NOP_EXPR : FIX_TRUNC_EXPR,
1488 TREE_TYPE (type), elts[i]);
1489 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1490 return NULL_TREE;
1493 return build_vector (type, elts);
1496 case VEC_WIDEN_MULT_LO_EXPR:
1497 case VEC_WIDEN_MULT_HI_EXPR:
1498 case VEC_WIDEN_MULT_EVEN_EXPR:
1499 case VEC_WIDEN_MULT_ODD_EXPR:
1501 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1502 unsigned int out, ofs, scale;
1503 tree *elts;
1505 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1506 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1507 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1508 return NULL_TREE;
1510 elts = XALLOCAVEC (tree, nelts * 4);
1511 if (!vec_cst_ctor_to_array (arg1, elts)
1512 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1513 return NULL_TREE;
1515 if (code == VEC_WIDEN_MULT_LO_EXPR)
1516 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1517 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1518 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1519 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1520 scale = 1, ofs = 0;
1521 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1522 scale = 1, ofs = 1;
1524 for (out = 0; out < nelts; out++)
1526 unsigned int in1 = (out << scale) + ofs;
1527 unsigned int in2 = in1 + nelts * 2;
1528 tree t1, t2;
1530 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1531 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1533 if (t1 == NULL_TREE || t2 == NULL_TREE)
1534 return NULL_TREE;
1535 elts[out] = const_binop (MULT_EXPR, t1, t2);
1536 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1537 return NULL_TREE;
1540 return build_vector (type, elts);
1543 default:;
1546 if (TREE_CODE_CLASS (code) != tcc_binary)
1547 return NULL_TREE;
1549 /* Make sure type and arg0 have the same saturating flag. */
1550 gcc_checking_assert (TYPE_SATURATING (type)
1551 == TYPE_SATURATING (TREE_TYPE (arg1)));
1553 return const_binop (code, arg1, arg2);
1556 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1557 Return zero if computing the constants is not possible. */
1559 tree
1560 const_unop (enum tree_code code, tree type, tree arg0)
1562 /* Don't perform the operation, other than NEGATE and ABS, if
1563 flag_signaling_nans is on and the operand is a signaling NaN. */
1564 if (TREE_CODE (arg0) == REAL_CST
1565 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1566 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1567 && code != NEGATE_EXPR
1568 && code != ABS_EXPR)
1569 return NULL_TREE;
1571 switch (code)
1573 CASE_CONVERT:
1574 case FLOAT_EXPR:
1575 case FIX_TRUNC_EXPR:
1576 case FIXED_CONVERT_EXPR:
1577 return fold_convert_const (code, type, arg0);
1579 case ADDR_SPACE_CONVERT_EXPR:
1580 /* If the source address is 0, and the source address space
1581 cannot have a valid object at 0, fold to dest type null. */
1582 if (integer_zerop (arg0)
1583 && !(targetm.addr_space.zero_address_valid
1584 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1585 return fold_convert_const (code, type, arg0);
1586 break;
1588 case VIEW_CONVERT_EXPR:
1589 return fold_view_convert_expr (type, arg0);
1591 case NEGATE_EXPR:
1593 /* Can't call fold_negate_const directly here as that doesn't
1594 handle all cases and we might not be able to negate some
1595 constants. */
1596 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1597 if (tem && CONSTANT_CLASS_P (tem))
1598 return tem;
1599 break;
1602 case ABS_EXPR:
1603 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1604 return fold_abs_const (arg0, type);
1605 break;
1607 case CONJ_EXPR:
1608 if (TREE_CODE (arg0) == COMPLEX_CST)
1610 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1611 TREE_TYPE (type));
1612 return build_complex (type, TREE_REALPART (arg0), ipart);
1614 break;
1616 case BIT_NOT_EXPR:
1617 if (TREE_CODE (arg0) == INTEGER_CST)
1618 return fold_not_const (arg0, type);
1619 /* Perform BIT_NOT_EXPR on each element individually. */
1620 else if (TREE_CODE (arg0) == VECTOR_CST)
1622 tree *elements;
1623 tree elem;
1624 unsigned count = VECTOR_CST_NELTS (arg0), i;
1626 elements = XALLOCAVEC (tree, count);
1627 for (i = 0; i < count; i++)
1629 elem = VECTOR_CST_ELT (arg0, i);
1630 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1631 if (elem == NULL_TREE)
1632 break;
1633 elements[i] = elem;
1635 if (i == count)
1636 return build_vector (type, elements);
1638 break;
1640 case TRUTH_NOT_EXPR:
1641 if (TREE_CODE (arg0) == INTEGER_CST)
1642 return constant_boolean_node (integer_zerop (arg0), type);
1643 break;
1645 case REALPART_EXPR:
1646 if (TREE_CODE (arg0) == COMPLEX_CST)
1647 return fold_convert (type, TREE_REALPART (arg0));
1648 break;
1650 case IMAGPART_EXPR:
1651 if (TREE_CODE (arg0) == COMPLEX_CST)
1652 return fold_convert (type, TREE_IMAGPART (arg0));
1653 break;
1655 case VEC_UNPACK_LO_EXPR:
1656 case VEC_UNPACK_HI_EXPR:
1657 case VEC_UNPACK_FLOAT_LO_EXPR:
1658 case VEC_UNPACK_FLOAT_HI_EXPR:
1660 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1661 tree *elts;
1662 enum tree_code subcode;
1664 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1665 if (TREE_CODE (arg0) != VECTOR_CST)
1666 return NULL_TREE;
1668 elts = XALLOCAVEC (tree, nelts * 2);
1669 if (!vec_cst_ctor_to_array (arg0, elts))
1670 return NULL_TREE;
1672 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1673 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1674 elts += nelts;
1676 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1677 subcode = NOP_EXPR;
1678 else
1679 subcode = FLOAT_EXPR;
1681 for (i = 0; i < nelts; i++)
1683 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1684 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1685 return NULL_TREE;
1688 return build_vector (type, elts);
1691 case REDUC_MIN_EXPR:
1692 case REDUC_MAX_EXPR:
1693 case REDUC_PLUS_EXPR:
1695 unsigned int nelts, i;
1696 tree *elts;
1697 enum tree_code subcode;
1699 if (TREE_CODE (arg0) != VECTOR_CST)
1700 return NULL_TREE;
1701 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1703 elts = XALLOCAVEC (tree, nelts);
1704 if (!vec_cst_ctor_to_array (arg0, elts))
1705 return NULL_TREE;
1707 switch (code)
1709 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1710 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1711 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1712 default: gcc_unreachable ();
1715 for (i = 1; i < nelts; i++)
1717 elts[0] = const_binop (subcode, elts[0], elts[i]);
1718 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1719 return NULL_TREE;
1722 return elts[0];
1725 default:
1726 break;
1729 return NULL_TREE;
1732 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1733 indicates which particular sizetype to create. */
1735 tree
1736 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1738 return build_int_cst (sizetype_tab[(int) kind], number);
1741 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1742 is a tree code. The type of the result is taken from the operands.
1743 Both must be equivalent integer types, ala int_binop_types_match_p.
1744 If the operands are constant, so is the result. */
1746 tree
1747 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1749 tree type = TREE_TYPE (arg0);
1751 if (arg0 == error_mark_node || arg1 == error_mark_node)
1752 return error_mark_node;
1754 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1755 TREE_TYPE (arg1)));
1757 /* Handle the special case of two integer constants faster. */
1758 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1760 /* And some specific cases even faster than that. */
1761 if (code == PLUS_EXPR)
1763 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1764 return arg1;
1765 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1766 return arg0;
1768 else if (code == MINUS_EXPR)
1770 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1771 return arg0;
1773 else if (code == MULT_EXPR)
1775 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1776 return arg1;
1779 /* Handle general case of two integer constants. For sizetype
1780 constant calculations we always want to know about overflow,
1781 even in the unsigned case. */
1782 return int_const_binop_1 (code, arg0, arg1, -1);
1785 return fold_build2_loc (loc, code, type, arg0, arg1);
1788 /* Given two values, either both of sizetype or both of bitsizetype,
1789 compute the difference between the two values. Return the value
1790 in signed type corresponding to the type of the operands. */
1792 tree
1793 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1795 tree type = TREE_TYPE (arg0);
1796 tree ctype;
1798 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1799 TREE_TYPE (arg1)));
1801 /* If the type is already signed, just do the simple thing. */
1802 if (!TYPE_UNSIGNED (type))
1803 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1805 if (type == sizetype)
1806 ctype = ssizetype;
1807 else if (type == bitsizetype)
1808 ctype = sbitsizetype;
1809 else
1810 ctype = signed_type_for (type);
1812 /* If either operand is not a constant, do the conversions to the signed
1813 type and subtract. The hardware will do the right thing with any
1814 overflow in the subtraction. */
1815 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1816 return size_binop_loc (loc, MINUS_EXPR,
1817 fold_convert_loc (loc, ctype, arg0),
1818 fold_convert_loc (loc, ctype, arg1));
1820 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1821 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1822 overflow) and negate (which can't either). Special-case a result
1823 of zero while we're here. */
1824 if (tree_int_cst_equal (arg0, arg1))
1825 return build_int_cst (ctype, 0);
1826 else if (tree_int_cst_lt (arg1, arg0))
1827 return fold_convert_loc (loc, ctype,
1828 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1829 else
1830 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1831 fold_convert_loc (loc, ctype,
1832 size_binop_loc (loc,
1833 MINUS_EXPR,
1834 arg1, arg0)));
1837 /* A subroutine of fold_convert_const handling conversions of an
1838 INTEGER_CST to another integer type. */
1840 static tree
1841 fold_convert_const_int_from_int (tree type, const_tree arg1)
1843 /* Given an integer constant, make new constant with new type,
1844 appropriately sign-extended or truncated. Use widest_int
1845 so that any extension is done according ARG1's type. */
1846 return force_fit_type (type, wi::to_widest (arg1),
1847 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1848 TREE_OVERFLOW (arg1));
1851 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1852 to an integer type. */
1854 static tree
1855 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1857 bool overflow = false;
1858 tree t;
1860 /* The following code implements the floating point to integer
1861 conversion rules required by the Java Language Specification,
1862 that IEEE NaNs are mapped to zero and values that overflow
1863 the target precision saturate, i.e. values greater than
1864 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1865 are mapped to INT_MIN. These semantics are allowed by the
1866 C and C++ standards that simply state that the behavior of
1867 FP-to-integer conversion is unspecified upon overflow. */
1869 wide_int val;
1870 REAL_VALUE_TYPE r;
1871 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1873 switch (code)
1875 case FIX_TRUNC_EXPR:
1876 real_trunc (&r, VOIDmode, &x);
1877 break;
1879 default:
1880 gcc_unreachable ();
1883 /* If R is NaN, return zero and show we have an overflow. */
1884 if (REAL_VALUE_ISNAN (r))
1886 overflow = true;
1887 val = wi::zero (TYPE_PRECISION (type));
1890 /* See if R is less than the lower bound or greater than the
1891 upper bound. */
1893 if (! overflow)
1895 tree lt = TYPE_MIN_VALUE (type);
1896 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1897 if (real_less (&r, &l))
1899 overflow = true;
1900 val = lt;
1904 if (! overflow)
1906 tree ut = TYPE_MAX_VALUE (type);
1907 if (ut)
1909 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1910 if (real_less (&u, &r))
1912 overflow = true;
1913 val = ut;
1918 if (! overflow)
1919 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1921 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1922 return t;
1925 /* A subroutine of fold_convert_const handling conversions of a
1926 FIXED_CST to an integer type. */
1928 static tree
1929 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1931 tree t;
1932 double_int temp, temp_trunc;
1933 unsigned int mode;
1935 /* Right shift FIXED_CST to temp by fbit. */
1936 temp = TREE_FIXED_CST (arg1).data;
1937 mode = TREE_FIXED_CST (arg1).mode;
1938 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1940 temp = temp.rshift (GET_MODE_FBIT (mode),
1941 HOST_BITS_PER_DOUBLE_INT,
1942 SIGNED_FIXED_POINT_MODE_P (mode));
1944 /* Left shift temp to temp_trunc by fbit. */
1945 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1946 HOST_BITS_PER_DOUBLE_INT,
1947 SIGNED_FIXED_POINT_MODE_P (mode));
1949 else
1951 temp = double_int_zero;
1952 temp_trunc = double_int_zero;
1955 /* If FIXED_CST is negative, we need to round the value toward 0.
1956 By checking if the fractional bits are not zero to add 1 to temp. */
1957 if (SIGNED_FIXED_POINT_MODE_P (mode)
1958 && temp_trunc.is_negative ()
1959 && TREE_FIXED_CST (arg1).data != temp_trunc)
1960 temp += double_int_one;
1962 /* Given a fixed-point constant, make new constant with new type,
1963 appropriately sign-extended or truncated. */
1964 t = force_fit_type (type, temp, -1,
1965 (temp.is_negative ()
1966 && (TYPE_UNSIGNED (type)
1967 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1968 | TREE_OVERFLOW (arg1));
1970 return t;
1973 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1974 to another floating point type. */
1976 static tree
1977 fold_convert_const_real_from_real (tree type, const_tree arg1)
1979 REAL_VALUE_TYPE value;
1980 tree t;
1982 /* Don't perform the operation if flag_signaling_nans is on
1983 and the operand is a signaling NaN. */
1984 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1985 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1986 return NULL_TREE;
1988 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1989 t = build_real (type, value);
1991 /* If converting an infinity or NAN to a representation that doesn't
1992 have one, set the overflow bit so that we can produce some kind of
1993 error message at the appropriate point if necessary. It's not the
1994 most user-friendly message, but it's better than nothing. */
1995 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1996 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1997 TREE_OVERFLOW (t) = 1;
1998 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1999 && !MODE_HAS_NANS (TYPE_MODE (type)))
2000 TREE_OVERFLOW (t) = 1;
2001 /* Regular overflow, conversion produced an infinity in a mode that
2002 can't represent them. */
2003 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2004 && REAL_VALUE_ISINF (value)
2005 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2006 TREE_OVERFLOW (t) = 1;
2007 else
2008 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2009 return t;
2012 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2013 to a floating point type. */
2015 static tree
2016 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2018 REAL_VALUE_TYPE value;
2019 tree t;
2021 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2022 t = build_real (type, value);
2024 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2025 return t;
2028 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2029 to another fixed-point type. */
2031 static tree
2032 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2034 FIXED_VALUE_TYPE value;
2035 tree t;
2036 bool overflow_p;
2038 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2039 TYPE_SATURATING (type));
2040 t = build_fixed (type, value);
2042 /* Propagate overflow flags. */
2043 if (overflow_p | TREE_OVERFLOW (arg1))
2044 TREE_OVERFLOW (t) = 1;
2045 return t;
2048 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2049 to a fixed-point type. */
2051 static tree
2052 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2054 FIXED_VALUE_TYPE value;
2055 tree t;
2056 bool overflow_p;
2057 double_int di;
2059 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2061 di.low = TREE_INT_CST_ELT (arg1, 0);
2062 if (TREE_INT_CST_NUNITS (arg1) == 1)
2063 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2064 else
2065 di.high = TREE_INT_CST_ELT (arg1, 1);
2067 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2068 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2069 TYPE_SATURATING (type));
2070 t = build_fixed (type, value);
2072 /* Propagate overflow flags. */
2073 if (overflow_p | TREE_OVERFLOW (arg1))
2074 TREE_OVERFLOW (t) = 1;
2075 return t;
2078 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2079 to a fixed-point type. */
2081 static tree
2082 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2084 FIXED_VALUE_TYPE value;
2085 tree t;
2086 bool overflow_p;
2088 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2089 &TREE_REAL_CST (arg1),
2090 TYPE_SATURATING (type));
2091 t = build_fixed (type, value);
2093 /* Propagate overflow flags. */
2094 if (overflow_p | TREE_OVERFLOW (arg1))
2095 TREE_OVERFLOW (t) = 1;
2096 return t;
2099 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2100 type TYPE. If no simplification can be done return NULL_TREE. */
2102 static tree
2103 fold_convert_const (enum tree_code code, tree type, tree arg1)
2105 if (TREE_TYPE (arg1) == type)
2106 return arg1;
2108 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2109 || TREE_CODE (type) == OFFSET_TYPE)
2111 if (TREE_CODE (arg1) == INTEGER_CST)
2112 return fold_convert_const_int_from_int (type, arg1);
2113 else if (TREE_CODE (arg1) == REAL_CST)
2114 return fold_convert_const_int_from_real (code, type, arg1);
2115 else if (TREE_CODE (arg1) == FIXED_CST)
2116 return fold_convert_const_int_from_fixed (type, arg1);
2118 else if (TREE_CODE (type) == REAL_TYPE)
2120 if (TREE_CODE (arg1) == INTEGER_CST)
2121 return build_real_from_int_cst (type, arg1);
2122 else if (TREE_CODE (arg1) == REAL_CST)
2123 return fold_convert_const_real_from_real (type, arg1);
2124 else if (TREE_CODE (arg1) == FIXED_CST)
2125 return fold_convert_const_real_from_fixed (type, arg1);
2127 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2129 if (TREE_CODE (arg1) == FIXED_CST)
2130 return fold_convert_const_fixed_from_fixed (type, arg1);
2131 else if (TREE_CODE (arg1) == INTEGER_CST)
2132 return fold_convert_const_fixed_from_int (type, arg1);
2133 else if (TREE_CODE (arg1) == REAL_CST)
2134 return fold_convert_const_fixed_from_real (type, arg1);
2136 else if (TREE_CODE (type) == VECTOR_TYPE)
2138 if (TREE_CODE (arg1) == VECTOR_CST
2139 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2141 int len = TYPE_VECTOR_SUBPARTS (type);
2142 tree elttype = TREE_TYPE (type);
2143 tree *v = XALLOCAVEC (tree, len);
2144 for (int i = 0; i < len; ++i)
2146 tree elt = VECTOR_CST_ELT (arg1, i);
2147 tree cvt = fold_convert_const (code, elttype, elt);
2148 if (cvt == NULL_TREE)
2149 return NULL_TREE;
2150 v[i] = cvt;
2152 return build_vector (type, v);
2155 return NULL_TREE;
2158 /* Construct a vector of zero elements of vector type TYPE. */
2160 static tree
2161 build_zero_vector (tree type)
2163 tree t;
2165 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2166 return build_vector_from_val (type, t);
2169 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2171 bool
2172 fold_convertible_p (const_tree type, const_tree arg)
2174 tree orig = TREE_TYPE (arg);
2176 if (type == orig)
2177 return true;
2179 if (TREE_CODE (arg) == ERROR_MARK
2180 || TREE_CODE (type) == ERROR_MARK
2181 || TREE_CODE (orig) == ERROR_MARK)
2182 return false;
2184 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2185 return true;
2187 switch (TREE_CODE (type))
2189 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2190 case POINTER_TYPE: case REFERENCE_TYPE:
2191 case OFFSET_TYPE:
2192 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2193 || TREE_CODE (orig) == OFFSET_TYPE);
2195 case REAL_TYPE:
2196 case FIXED_POINT_TYPE:
2197 case VECTOR_TYPE:
2198 case VOID_TYPE:
2199 return TREE_CODE (type) == TREE_CODE (orig);
2201 default:
2202 return false;
2206 /* Convert expression ARG to type TYPE. Used by the middle-end for
2207 simple conversions in preference to calling the front-end's convert. */
2209 tree
2210 fold_convert_loc (location_t loc, tree type, tree arg)
2212 tree orig = TREE_TYPE (arg);
2213 tree tem;
2215 if (type == orig)
2216 return arg;
2218 if (TREE_CODE (arg) == ERROR_MARK
2219 || TREE_CODE (type) == ERROR_MARK
2220 || TREE_CODE (orig) == ERROR_MARK)
2221 return error_mark_node;
2223 switch (TREE_CODE (type))
2225 case POINTER_TYPE:
2226 case REFERENCE_TYPE:
2227 /* Handle conversions between pointers to different address spaces. */
2228 if (POINTER_TYPE_P (orig)
2229 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2230 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2231 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2232 /* fall through */
2234 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2235 case OFFSET_TYPE:
2236 if (TREE_CODE (arg) == INTEGER_CST)
2238 tem = fold_convert_const (NOP_EXPR, type, arg);
2239 if (tem != NULL_TREE)
2240 return tem;
2242 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2243 || TREE_CODE (orig) == OFFSET_TYPE)
2244 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2245 if (TREE_CODE (orig) == COMPLEX_TYPE)
2246 return fold_convert_loc (loc, type,
2247 fold_build1_loc (loc, REALPART_EXPR,
2248 TREE_TYPE (orig), arg));
2249 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2250 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2251 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2253 case REAL_TYPE:
2254 if (TREE_CODE (arg) == INTEGER_CST)
2256 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2257 if (tem != NULL_TREE)
2258 return tem;
2260 else if (TREE_CODE (arg) == REAL_CST)
2262 tem = fold_convert_const (NOP_EXPR, type, arg);
2263 if (tem != NULL_TREE)
2264 return tem;
2266 else if (TREE_CODE (arg) == FIXED_CST)
2268 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2269 if (tem != NULL_TREE)
2270 return tem;
2273 switch (TREE_CODE (orig))
2275 case INTEGER_TYPE:
2276 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2277 case POINTER_TYPE: case REFERENCE_TYPE:
2278 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2280 case REAL_TYPE:
2281 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2283 case FIXED_POINT_TYPE:
2284 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2286 case COMPLEX_TYPE:
2287 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2288 return fold_convert_loc (loc, type, tem);
2290 default:
2291 gcc_unreachable ();
2294 case FIXED_POINT_TYPE:
2295 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2296 || TREE_CODE (arg) == REAL_CST)
2298 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2299 if (tem != NULL_TREE)
2300 goto fold_convert_exit;
2303 switch (TREE_CODE (orig))
2305 case FIXED_POINT_TYPE:
2306 case INTEGER_TYPE:
2307 case ENUMERAL_TYPE:
2308 case BOOLEAN_TYPE:
2309 case REAL_TYPE:
2310 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2312 case COMPLEX_TYPE:
2313 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2314 return fold_convert_loc (loc, type, tem);
2316 default:
2317 gcc_unreachable ();
2320 case COMPLEX_TYPE:
2321 switch (TREE_CODE (orig))
2323 case INTEGER_TYPE:
2324 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2325 case POINTER_TYPE: case REFERENCE_TYPE:
2326 case REAL_TYPE:
2327 case FIXED_POINT_TYPE:
2328 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2329 fold_convert_loc (loc, TREE_TYPE (type), arg),
2330 fold_convert_loc (loc, TREE_TYPE (type),
2331 integer_zero_node));
2332 case COMPLEX_TYPE:
2334 tree rpart, ipart;
2336 if (TREE_CODE (arg) == COMPLEX_EXPR)
2338 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2339 TREE_OPERAND (arg, 0));
2340 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2341 TREE_OPERAND (arg, 1));
2342 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2345 arg = save_expr (arg);
2346 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2347 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2348 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2349 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2350 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2353 default:
2354 gcc_unreachable ();
2357 case VECTOR_TYPE:
2358 if (integer_zerop (arg))
2359 return build_zero_vector (type);
2360 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2361 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2362 || TREE_CODE (orig) == VECTOR_TYPE);
2363 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2365 case VOID_TYPE:
2366 tem = fold_ignored_result (arg);
2367 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2369 default:
2370 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2371 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2372 gcc_unreachable ();
2374 fold_convert_exit:
2375 protected_set_expr_location_unshare (tem, loc);
2376 return tem;
2379 /* Return false if expr can be assumed not to be an lvalue, true
2380 otherwise. */
2382 static bool
2383 maybe_lvalue_p (const_tree x)
2385 /* We only need to wrap lvalue tree codes. */
2386 switch (TREE_CODE (x))
2388 case VAR_DECL:
2389 case PARM_DECL:
2390 case RESULT_DECL:
2391 case LABEL_DECL:
2392 case FUNCTION_DECL:
2393 case SSA_NAME:
2395 case COMPONENT_REF:
2396 case MEM_REF:
2397 case INDIRECT_REF:
2398 case ARRAY_REF:
2399 case ARRAY_RANGE_REF:
2400 case BIT_FIELD_REF:
2401 case OBJ_TYPE_REF:
2403 case REALPART_EXPR:
2404 case IMAGPART_EXPR:
2405 case PREINCREMENT_EXPR:
2406 case PREDECREMENT_EXPR:
2407 case SAVE_EXPR:
2408 case TRY_CATCH_EXPR:
2409 case WITH_CLEANUP_EXPR:
2410 case COMPOUND_EXPR:
2411 case MODIFY_EXPR:
2412 case TARGET_EXPR:
2413 case COND_EXPR:
2414 case BIND_EXPR:
2415 break;
2417 default:
2418 /* Assume the worst for front-end tree codes. */
2419 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2420 break;
2421 return false;
2424 return true;
2427 /* Return an expr equal to X but certainly not valid as an lvalue. */
2429 tree
2430 non_lvalue_loc (location_t loc, tree x)
2432 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2433 us. */
2434 if (in_gimple_form)
2435 return x;
2437 if (! maybe_lvalue_p (x))
2438 return x;
2439 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2442 /* When pedantic, return an expr equal to X but certainly not valid as a
2443 pedantic lvalue. Otherwise, return X. */
2445 static tree
2446 pedantic_non_lvalue_loc (location_t loc, tree x)
2448 return protected_set_expr_location_unshare (x, loc);
2451 /* Given a tree comparison code, return the code that is the logical inverse.
2452 It is generally not safe to do this for floating-point comparisons, except
2453 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2454 ERROR_MARK in this case. */
2456 enum tree_code
2457 invert_tree_comparison (enum tree_code code, bool honor_nans)
2459 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2460 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2461 return ERROR_MARK;
2463 switch (code)
2465 case EQ_EXPR:
2466 return NE_EXPR;
2467 case NE_EXPR:
2468 return EQ_EXPR;
2469 case GT_EXPR:
2470 return honor_nans ? UNLE_EXPR : LE_EXPR;
2471 case GE_EXPR:
2472 return honor_nans ? UNLT_EXPR : LT_EXPR;
2473 case LT_EXPR:
2474 return honor_nans ? UNGE_EXPR : GE_EXPR;
2475 case LE_EXPR:
2476 return honor_nans ? UNGT_EXPR : GT_EXPR;
2477 case LTGT_EXPR:
2478 return UNEQ_EXPR;
2479 case UNEQ_EXPR:
2480 return LTGT_EXPR;
2481 case UNGT_EXPR:
2482 return LE_EXPR;
2483 case UNGE_EXPR:
2484 return LT_EXPR;
2485 case UNLT_EXPR:
2486 return GE_EXPR;
2487 case UNLE_EXPR:
2488 return GT_EXPR;
2489 case ORDERED_EXPR:
2490 return UNORDERED_EXPR;
2491 case UNORDERED_EXPR:
2492 return ORDERED_EXPR;
2493 default:
2494 gcc_unreachable ();
2498 /* Similar, but return the comparison that results if the operands are
2499 swapped. This is safe for floating-point. */
2501 enum tree_code
2502 swap_tree_comparison (enum tree_code code)
2504 switch (code)
2506 case EQ_EXPR:
2507 case NE_EXPR:
2508 case ORDERED_EXPR:
2509 case UNORDERED_EXPR:
2510 case LTGT_EXPR:
2511 case UNEQ_EXPR:
2512 return code;
2513 case GT_EXPR:
2514 return LT_EXPR;
2515 case GE_EXPR:
2516 return LE_EXPR;
2517 case LT_EXPR:
2518 return GT_EXPR;
2519 case LE_EXPR:
2520 return GE_EXPR;
2521 case UNGT_EXPR:
2522 return UNLT_EXPR;
2523 case UNGE_EXPR:
2524 return UNLE_EXPR;
2525 case UNLT_EXPR:
2526 return UNGT_EXPR;
2527 case UNLE_EXPR:
2528 return UNGE_EXPR;
2529 default:
2530 gcc_unreachable ();
2535 /* Convert a comparison tree code from an enum tree_code representation
2536 into a compcode bit-based encoding. This function is the inverse of
2537 compcode_to_comparison. */
2539 static enum comparison_code
2540 comparison_to_compcode (enum tree_code code)
2542 switch (code)
2544 case LT_EXPR:
2545 return COMPCODE_LT;
2546 case EQ_EXPR:
2547 return COMPCODE_EQ;
2548 case LE_EXPR:
2549 return COMPCODE_LE;
2550 case GT_EXPR:
2551 return COMPCODE_GT;
2552 case NE_EXPR:
2553 return COMPCODE_NE;
2554 case GE_EXPR:
2555 return COMPCODE_GE;
2556 case ORDERED_EXPR:
2557 return COMPCODE_ORD;
2558 case UNORDERED_EXPR:
2559 return COMPCODE_UNORD;
2560 case UNLT_EXPR:
2561 return COMPCODE_UNLT;
2562 case UNEQ_EXPR:
2563 return COMPCODE_UNEQ;
2564 case UNLE_EXPR:
2565 return COMPCODE_UNLE;
2566 case UNGT_EXPR:
2567 return COMPCODE_UNGT;
2568 case LTGT_EXPR:
2569 return COMPCODE_LTGT;
2570 case UNGE_EXPR:
2571 return COMPCODE_UNGE;
2572 default:
2573 gcc_unreachable ();
2577 /* Convert a compcode bit-based encoding of a comparison operator back
2578 to GCC's enum tree_code representation. This function is the
2579 inverse of comparison_to_compcode. */
2581 static enum tree_code
2582 compcode_to_comparison (enum comparison_code code)
2584 switch (code)
2586 case COMPCODE_LT:
2587 return LT_EXPR;
2588 case COMPCODE_EQ:
2589 return EQ_EXPR;
2590 case COMPCODE_LE:
2591 return LE_EXPR;
2592 case COMPCODE_GT:
2593 return GT_EXPR;
2594 case COMPCODE_NE:
2595 return NE_EXPR;
2596 case COMPCODE_GE:
2597 return GE_EXPR;
2598 case COMPCODE_ORD:
2599 return ORDERED_EXPR;
2600 case COMPCODE_UNORD:
2601 return UNORDERED_EXPR;
2602 case COMPCODE_UNLT:
2603 return UNLT_EXPR;
2604 case COMPCODE_UNEQ:
2605 return UNEQ_EXPR;
2606 case COMPCODE_UNLE:
2607 return UNLE_EXPR;
2608 case COMPCODE_UNGT:
2609 return UNGT_EXPR;
2610 case COMPCODE_LTGT:
2611 return LTGT_EXPR;
2612 case COMPCODE_UNGE:
2613 return UNGE_EXPR;
2614 default:
2615 gcc_unreachable ();
2619 /* Return a tree for the comparison which is the combination of
2620 doing the AND or OR (depending on CODE) of the two operations LCODE
2621 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2622 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2623 if this makes the transformation invalid. */
2625 tree
2626 combine_comparisons (location_t loc,
2627 enum tree_code code, enum tree_code lcode,
2628 enum tree_code rcode, tree truth_type,
2629 tree ll_arg, tree lr_arg)
2631 bool honor_nans = HONOR_NANS (ll_arg);
2632 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2633 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2634 int compcode;
2636 switch (code)
2638 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2639 compcode = lcompcode & rcompcode;
2640 break;
2642 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2643 compcode = lcompcode | rcompcode;
2644 break;
2646 default:
2647 return NULL_TREE;
2650 if (!honor_nans)
2652 /* Eliminate unordered comparisons, as well as LTGT and ORD
2653 which are not used unless the mode has NaNs. */
2654 compcode &= ~COMPCODE_UNORD;
2655 if (compcode == COMPCODE_LTGT)
2656 compcode = COMPCODE_NE;
2657 else if (compcode == COMPCODE_ORD)
2658 compcode = COMPCODE_TRUE;
2660 else if (flag_trapping_math)
2662 /* Check that the original operation and the optimized ones will trap
2663 under the same condition. */
2664 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2665 && (lcompcode != COMPCODE_EQ)
2666 && (lcompcode != COMPCODE_ORD);
2667 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2668 && (rcompcode != COMPCODE_EQ)
2669 && (rcompcode != COMPCODE_ORD);
2670 bool trap = (compcode & COMPCODE_UNORD) == 0
2671 && (compcode != COMPCODE_EQ)
2672 && (compcode != COMPCODE_ORD);
2674 /* In a short-circuited boolean expression the LHS might be
2675 such that the RHS, if evaluated, will never trap. For
2676 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2677 if neither x nor y is NaN. (This is a mixed blessing: for
2678 example, the expression above will never trap, hence
2679 optimizing it to x < y would be invalid). */
2680 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2681 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2682 rtrap = false;
2684 /* If the comparison was short-circuited, and only the RHS
2685 trapped, we may now generate a spurious trap. */
2686 if (rtrap && !ltrap
2687 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2688 return NULL_TREE;
2690 /* If we changed the conditions that cause a trap, we lose. */
2691 if ((ltrap || rtrap) != trap)
2692 return NULL_TREE;
2695 if (compcode == COMPCODE_TRUE)
2696 return constant_boolean_node (true, truth_type);
2697 else if (compcode == COMPCODE_FALSE)
2698 return constant_boolean_node (false, truth_type);
2699 else
2701 enum tree_code tcode;
2703 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2704 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2708 /* Return nonzero if two operands (typically of the same tree node)
2709 are necessarily equal. FLAGS modifies behavior as follows:
2711 If OEP_ONLY_CONST is set, only return nonzero for constants.
2712 This function tests whether the operands are indistinguishable;
2713 it does not test whether they are equal using C's == operation.
2714 The distinction is important for IEEE floating point, because
2715 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2716 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2718 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2719 even though it may hold multiple values during a function.
2720 This is because a GCC tree node guarantees that nothing else is
2721 executed between the evaluation of its "operands" (which may often
2722 be evaluated in arbitrary order). Hence if the operands themselves
2723 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2724 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2725 unset means assuming isochronic (or instantaneous) tree equivalence.
2726 Unless comparing arbitrary expression trees, such as from different
2727 statements, this flag can usually be left unset.
2729 If OEP_PURE_SAME is set, then pure functions with identical arguments
2730 are considered the same. It is used when the caller has other ways
2731 to ensure that global memory is unchanged in between.
2733 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2734 not values of expressions.
2736 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2737 any operand with side effect. This is unnecesarily conservative in the
2738 case we know that arg0 and arg1 are in disjoint code paths (such as in
2739 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2740 addresses with TREE_CONSTANT flag set so we know that &var == &var
2741 even if var is volatile. */
2744 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2746 /* When checking, verify at the outermost operand_equal_p call that
2747 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2748 hash value. */
2749 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2751 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2753 if (arg0 != arg1)
2755 inchash::hash hstate0 (0), hstate1 (0);
2756 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2757 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2758 hashval_t h0 = hstate0.end ();
2759 hashval_t h1 = hstate1.end ();
2760 gcc_assert (h0 == h1);
2762 return 1;
2764 else
2765 return 0;
2768 /* If either is ERROR_MARK, they aren't equal. */
2769 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2770 || TREE_TYPE (arg0) == error_mark_node
2771 || TREE_TYPE (arg1) == error_mark_node)
2772 return 0;
2774 /* Similar, if either does not have a type (like a released SSA name),
2775 they aren't equal. */
2776 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2777 return 0;
2779 /* We cannot consider pointers to different address space equal. */
2780 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2781 && POINTER_TYPE_P (TREE_TYPE (arg1))
2782 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2783 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2784 return 0;
2786 /* Check equality of integer constants before bailing out due to
2787 precision differences. */
2788 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2790 /* Address of INTEGER_CST is not defined; check that we did not forget
2791 to drop the OEP_ADDRESS_OF flags. */
2792 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2793 return tree_int_cst_equal (arg0, arg1);
2796 if (!(flags & OEP_ADDRESS_OF))
2798 /* If both types don't have the same signedness, then we can't consider
2799 them equal. We must check this before the STRIP_NOPS calls
2800 because they may change the signedness of the arguments. As pointers
2801 strictly don't have a signedness, require either two pointers or
2802 two non-pointers as well. */
2803 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2804 || POINTER_TYPE_P (TREE_TYPE (arg0))
2805 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2806 return 0;
2808 /* If both types don't have the same precision, then it is not safe
2809 to strip NOPs. */
2810 if (element_precision (TREE_TYPE (arg0))
2811 != element_precision (TREE_TYPE (arg1)))
2812 return 0;
2814 STRIP_NOPS (arg0);
2815 STRIP_NOPS (arg1);
2817 #if 0
2818 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2819 sanity check once the issue is solved. */
2820 else
2821 /* Addresses of conversions and SSA_NAMEs (and many other things)
2822 are not defined. Check that we did not forget to drop the
2823 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2824 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2825 && TREE_CODE (arg0) != SSA_NAME);
2826 #endif
2828 /* In case both args are comparisons but with different comparison
2829 code, try to swap the comparison operands of one arg to produce
2830 a match and compare that variant. */
2831 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2832 && COMPARISON_CLASS_P (arg0)
2833 && COMPARISON_CLASS_P (arg1))
2835 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2837 if (TREE_CODE (arg0) == swap_code)
2838 return operand_equal_p (TREE_OPERAND (arg0, 0),
2839 TREE_OPERAND (arg1, 1), flags)
2840 && operand_equal_p (TREE_OPERAND (arg0, 1),
2841 TREE_OPERAND (arg1, 0), flags);
2844 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2846 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2847 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2849 else if (flags & OEP_ADDRESS_OF)
2851 /* If we are interested in comparing addresses ignore
2852 MEM_REF wrappings of the base that can appear just for
2853 TBAA reasons. */
2854 if (TREE_CODE (arg0) == MEM_REF
2855 && DECL_P (arg1)
2856 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2857 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2858 && integer_zerop (TREE_OPERAND (arg0, 1)))
2859 return 1;
2860 else if (TREE_CODE (arg1) == MEM_REF
2861 && DECL_P (arg0)
2862 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2863 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2864 && integer_zerop (TREE_OPERAND (arg1, 1)))
2865 return 1;
2866 return 0;
2868 else
2869 return 0;
2872 /* When not checking adddresses, this is needed for conversions and for
2873 COMPONENT_REF. Might as well play it safe and always test this. */
2874 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2875 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2876 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2877 && !(flags & OEP_ADDRESS_OF)))
2878 return 0;
2880 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2881 We don't care about side effects in that case because the SAVE_EXPR
2882 takes care of that for us. In all other cases, two expressions are
2883 equal if they have no side effects. If we have two identical
2884 expressions with side effects that should be treated the same due
2885 to the only side effects being identical SAVE_EXPR's, that will
2886 be detected in the recursive calls below.
2887 If we are taking an invariant address of two identical objects
2888 they are necessarily equal as well. */
2889 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2890 && (TREE_CODE (arg0) == SAVE_EXPR
2891 || (flags & OEP_MATCH_SIDE_EFFECTS)
2892 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2893 return 1;
2895 /* Next handle constant cases, those for which we can return 1 even
2896 if ONLY_CONST is set. */
2897 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2898 switch (TREE_CODE (arg0))
2900 case INTEGER_CST:
2901 return tree_int_cst_equal (arg0, arg1);
2903 case FIXED_CST:
2904 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2905 TREE_FIXED_CST (arg1));
2907 case REAL_CST:
2908 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2909 return 1;
2912 if (!HONOR_SIGNED_ZEROS (arg0))
2914 /* If we do not distinguish between signed and unsigned zero,
2915 consider them equal. */
2916 if (real_zerop (arg0) && real_zerop (arg1))
2917 return 1;
2919 return 0;
2921 case VECTOR_CST:
2923 unsigned i;
2925 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2926 return 0;
2928 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2930 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2931 VECTOR_CST_ELT (arg1, i), flags))
2932 return 0;
2934 return 1;
2937 case COMPLEX_CST:
2938 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2939 flags)
2940 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2941 flags));
2943 case STRING_CST:
2944 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2945 && ! memcmp (TREE_STRING_POINTER (arg0),
2946 TREE_STRING_POINTER (arg1),
2947 TREE_STRING_LENGTH (arg0)));
2949 case ADDR_EXPR:
2950 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2951 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2952 flags | OEP_ADDRESS_OF
2953 | OEP_MATCH_SIDE_EFFECTS);
2954 case CONSTRUCTOR:
2955 /* In GIMPLE empty constructors are allowed in initializers of
2956 aggregates. */
2957 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2958 default:
2959 break;
2962 if (flags & OEP_ONLY_CONST)
2963 return 0;
2965 /* Define macros to test an operand from arg0 and arg1 for equality and a
2966 variant that allows null and views null as being different from any
2967 non-null value. In the latter case, if either is null, the both
2968 must be; otherwise, do the normal comparison. */
2969 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2970 TREE_OPERAND (arg1, N), flags)
2972 #define OP_SAME_WITH_NULL(N) \
2973 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2974 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2976 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2978 case tcc_unary:
2979 /* Two conversions are equal only if signedness and modes match. */
2980 switch (TREE_CODE (arg0))
2982 CASE_CONVERT:
2983 case FIX_TRUNC_EXPR:
2984 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2985 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2986 return 0;
2987 break;
2988 default:
2989 break;
2992 return OP_SAME (0);
2995 case tcc_comparison:
2996 case tcc_binary:
2997 if (OP_SAME (0) && OP_SAME (1))
2998 return 1;
3000 /* For commutative ops, allow the other order. */
3001 return (commutative_tree_code (TREE_CODE (arg0))
3002 && operand_equal_p (TREE_OPERAND (arg0, 0),
3003 TREE_OPERAND (arg1, 1), flags)
3004 && operand_equal_p (TREE_OPERAND (arg0, 1),
3005 TREE_OPERAND (arg1, 0), flags));
3007 case tcc_reference:
3008 /* If either of the pointer (or reference) expressions we are
3009 dereferencing contain a side effect, these cannot be equal,
3010 but their addresses can be. */
3011 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3012 && (TREE_SIDE_EFFECTS (arg0)
3013 || TREE_SIDE_EFFECTS (arg1)))
3014 return 0;
3016 switch (TREE_CODE (arg0))
3018 case INDIRECT_REF:
3019 if (!(flags & OEP_ADDRESS_OF)
3020 && (TYPE_ALIGN (TREE_TYPE (arg0))
3021 != TYPE_ALIGN (TREE_TYPE (arg1))))
3022 return 0;
3023 flags &= ~OEP_ADDRESS_OF;
3024 return OP_SAME (0);
3026 case IMAGPART_EXPR:
3027 /* Require the same offset. */
3028 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3029 TYPE_SIZE (TREE_TYPE (arg1)),
3030 flags & ~OEP_ADDRESS_OF))
3031 return 0;
3033 /* Fallthru. */
3034 case REALPART_EXPR:
3035 case VIEW_CONVERT_EXPR:
3036 return OP_SAME (0);
3038 case TARGET_MEM_REF:
3039 case MEM_REF:
3040 if (!(flags & OEP_ADDRESS_OF))
3042 /* Require equal access sizes */
3043 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3044 && (!TYPE_SIZE (TREE_TYPE (arg0))
3045 || !TYPE_SIZE (TREE_TYPE (arg1))
3046 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3047 TYPE_SIZE (TREE_TYPE (arg1)),
3048 flags)))
3049 return 0;
3050 /* Verify that access happens in similar types. */
3051 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3052 return 0;
3053 /* Verify that accesses are TBAA compatible. */
3054 if (!alias_ptr_types_compatible_p
3055 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3056 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3057 || (MR_DEPENDENCE_CLIQUE (arg0)
3058 != MR_DEPENDENCE_CLIQUE (arg1))
3059 || (MR_DEPENDENCE_BASE (arg0)
3060 != MR_DEPENDENCE_BASE (arg1)))
3061 return 0;
3062 /* Verify that alignment is compatible. */
3063 if (TYPE_ALIGN (TREE_TYPE (arg0))
3064 != TYPE_ALIGN (TREE_TYPE (arg1)))
3065 return 0;
3067 flags &= ~OEP_ADDRESS_OF;
3068 return (OP_SAME (0) && OP_SAME (1)
3069 /* TARGET_MEM_REF require equal extra operands. */
3070 && (TREE_CODE (arg0) != TARGET_MEM_REF
3071 || (OP_SAME_WITH_NULL (2)
3072 && OP_SAME_WITH_NULL (3)
3073 && OP_SAME_WITH_NULL (4))));
3075 case ARRAY_REF:
3076 case ARRAY_RANGE_REF:
3077 if (!OP_SAME (0))
3078 return 0;
3079 flags &= ~OEP_ADDRESS_OF;
3080 /* Compare the array index by value if it is constant first as we
3081 may have different types but same value here. */
3082 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3083 TREE_OPERAND (arg1, 1))
3084 || OP_SAME (1))
3085 && OP_SAME_WITH_NULL (2)
3086 && OP_SAME_WITH_NULL (3)
3087 /* Compare low bound and element size as with OEP_ADDRESS_OF
3088 we have to account for the offset of the ref. */
3089 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3090 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3091 || (operand_equal_p (array_ref_low_bound
3092 (CONST_CAST_TREE (arg0)),
3093 array_ref_low_bound
3094 (CONST_CAST_TREE (arg1)), flags)
3095 && operand_equal_p (array_ref_element_size
3096 (CONST_CAST_TREE (arg0)),
3097 array_ref_element_size
3098 (CONST_CAST_TREE (arg1)),
3099 flags))));
3101 case COMPONENT_REF:
3102 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3103 may be NULL when we're called to compare MEM_EXPRs. */
3104 if (!OP_SAME_WITH_NULL (0)
3105 || !OP_SAME (1))
3106 return 0;
3107 flags &= ~OEP_ADDRESS_OF;
3108 return OP_SAME_WITH_NULL (2);
3110 case BIT_FIELD_REF:
3111 if (!OP_SAME (0))
3112 return 0;
3113 flags &= ~OEP_ADDRESS_OF;
3114 return OP_SAME (1) && OP_SAME (2);
3116 default:
3117 return 0;
3120 case tcc_expression:
3121 switch (TREE_CODE (arg0))
3123 case ADDR_EXPR:
3124 /* Be sure we pass right ADDRESS_OF flag. */
3125 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3126 return operand_equal_p (TREE_OPERAND (arg0, 0),
3127 TREE_OPERAND (arg1, 0),
3128 flags | OEP_ADDRESS_OF);
3130 case TRUTH_NOT_EXPR:
3131 return OP_SAME (0);
3133 case TRUTH_ANDIF_EXPR:
3134 case TRUTH_ORIF_EXPR:
3135 return OP_SAME (0) && OP_SAME (1);
3137 case FMA_EXPR:
3138 case WIDEN_MULT_PLUS_EXPR:
3139 case WIDEN_MULT_MINUS_EXPR:
3140 if (!OP_SAME (2))
3141 return 0;
3142 /* The multiplcation operands are commutative. */
3143 /* FALLTHRU */
3145 case TRUTH_AND_EXPR:
3146 case TRUTH_OR_EXPR:
3147 case TRUTH_XOR_EXPR:
3148 if (OP_SAME (0) && OP_SAME (1))
3149 return 1;
3151 /* Otherwise take into account this is a commutative operation. */
3152 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3153 TREE_OPERAND (arg1, 1), flags)
3154 && operand_equal_p (TREE_OPERAND (arg0, 1),
3155 TREE_OPERAND (arg1, 0), flags));
3157 case COND_EXPR:
3158 if (! OP_SAME (1) || ! OP_SAME (2))
3159 return 0;
3160 flags &= ~OEP_ADDRESS_OF;
3161 return OP_SAME (0);
3163 case VEC_COND_EXPR:
3164 case DOT_PROD_EXPR:
3165 case BIT_INSERT_EXPR:
3166 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3168 default:
3169 return 0;
3172 case tcc_vl_exp:
3173 switch (TREE_CODE (arg0))
3175 case CALL_EXPR:
3176 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3177 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3178 /* If not both CALL_EXPRs are either internal or normal function
3179 functions, then they are not equal. */
3180 return 0;
3181 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3183 /* If the CALL_EXPRs call different internal functions, then they
3184 are not equal. */
3185 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3186 return 0;
3188 else
3190 /* If the CALL_EXPRs call different functions, then they are not
3191 equal. */
3192 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3193 flags))
3194 return 0;
3197 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3199 unsigned int cef = call_expr_flags (arg0);
3200 if (flags & OEP_PURE_SAME)
3201 cef &= ECF_CONST | ECF_PURE;
3202 else
3203 cef &= ECF_CONST;
3204 if (!cef)
3205 return 0;
3208 /* Now see if all the arguments are the same. */
3210 const_call_expr_arg_iterator iter0, iter1;
3211 const_tree a0, a1;
3212 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3213 a1 = first_const_call_expr_arg (arg1, &iter1);
3214 a0 && a1;
3215 a0 = next_const_call_expr_arg (&iter0),
3216 a1 = next_const_call_expr_arg (&iter1))
3217 if (! operand_equal_p (a0, a1, flags))
3218 return 0;
3220 /* If we get here and both argument lists are exhausted
3221 then the CALL_EXPRs are equal. */
3222 return ! (a0 || a1);
3224 default:
3225 return 0;
3228 case tcc_declaration:
3229 /* Consider __builtin_sqrt equal to sqrt. */
3230 return (TREE_CODE (arg0) == FUNCTION_DECL
3231 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3232 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3233 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3235 case tcc_exceptional:
3236 if (TREE_CODE (arg0) == CONSTRUCTOR)
3238 /* In GIMPLE constructors are used only to build vectors from
3239 elements. Individual elements in the constructor must be
3240 indexed in increasing order and form an initial sequence.
3242 We make no effort to compare constructors in generic.
3243 (see sem_variable::equals in ipa-icf which can do so for
3244 constants). */
3245 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3246 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3247 return 0;
3249 /* Be sure that vectors constructed have the same representation.
3250 We only tested element precision and modes to match.
3251 Vectors may be BLKmode and thus also check that the number of
3252 parts match. */
3253 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3254 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3255 return 0;
3257 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3258 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3259 unsigned int len = vec_safe_length (v0);
3261 if (len != vec_safe_length (v1))
3262 return 0;
3264 for (unsigned int i = 0; i < len; i++)
3266 constructor_elt *c0 = &(*v0)[i];
3267 constructor_elt *c1 = &(*v1)[i];
3269 if (!operand_equal_p (c0->value, c1->value, flags)
3270 /* In GIMPLE the indexes can be either NULL or matching i.
3271 Double check this so we won't get false
3272 positives for GENERIC. */
3273 || (c0->index
3274 && (TREE_CODE (c0->index) != INTEGER_CST
3275 || !compare_tree_int (c0->index, i)))
3276 || (c1->index
3277 && (TREE_CODE (c1->index) != INTEGER_CST
3278 || !compare_tree_int (c1->index, i))))
3279 return 0;
3281 return 1;
3283 return 0;
3285 default:
3286 return 0;
3289 #undef OP_SAME
3290 #undef OP_SAME_WITH_NULL
3293 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3294 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3296 When in doubt, return 0. */
3298 static int
3299 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3301 int unsignedp1, unsignedpo;
3302 tree primarg0, primarg1, primother;
3303 unsigned int correct_width;
3305 if (operand_equal_p (arg0, arg1, 0))
3306 return 1;
3308 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3309 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3310 return 0;
3312 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3313 and see if the inner values are the same. This removes any
3314 signedness comparison, which doesn't matter here. */
3315 primarg0 = arg0, primarg1 = arg1;
3316 STRIP_NOPS (primarg0);
3317 STRIP_NOPS (primarg1);
3318 if (operand_equal_p (primarg0, primarg1, 0))
3319 return 1;
3321 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3322 actual comparison operand, ARG0.
3324 First throw away any conversions to wider types
3325 already present in the operands. */
3327 primarg1 = get_narrower (arg1, &unsignedp1);
3328 primother = get_narrower (other, &unsignedpo);
3330 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3331 if (unsignedp1 == unsignedpo
3332 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3333 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3335 tree type = TREE_TYPE (arg0);
3337 /* Make sure shorter operand is extended the right way
3338 to match the longer operand. */
3339 primarg1 = fold_convert (signed_or_unsigned_type_for
3340 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3342 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3343 return 1;
3346 return 0;
3349 /* See if ARG is an expression that is either a comparison or is performing
3350 arithmetic on comparisons. The comparisons must only be comparing
3351 two different values, which will be stored in *CVAL1 and *CVAL2; if
3352 they are nonzero it means that some operands have already been found.
3353 No variables may be used anywhere else in the expression except in the
3354 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3355 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3357 If this is true, return 1. Otherwise, return zero. */
3359 static int
3360 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3362 enum tree_code code = TREE_CODE (arg);
3363 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3365 /* We can handle some of the tcc_expression cases here. */
3366 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3367 tclass = tcc_unary;
3368 else if (tclass == tcc_expression
3369 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3370 || code == COMPOUND_EXPR))
3371 tclass = tcc_binary;
3373 else if (tclass == tcc_expression && code == SAVE_EXPR
3374 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3376 /* If we've already found a CVAL1 or CVAL2, this expression is
3377 two complex to handle. */
3378 if (*cval1 || *cval2)
3379 return 0;
3381 tclass = tcc_unary;
3382 *save_p = 1;
3385 switch (tclass)
3387 case tcc_unary:
3388 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3390 case tcc_binary:
3391 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3392 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3393 cval1, cval2, save_p));
3395 case tcc_constant:
3396 return 1;
3398 case tcc_expression:
3399 if (code == COND_EXPR)
3400 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3401 cval1, cval2, save_p)
3402 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3403 cval1, cval2, save_p)
3404 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3405 cval1, cval2, save_p));
3406 return 0;
3408 case tcc_comparison:
3409 /* First see if we can handle the first operand, then the second. For
3410 the second operand, we know *CVAL1 can't be zero. It must be that
3411 one side of the comparison is each of the values; test for the
3412 case where this isn't true by failing if the two operands
3413 are the same. */
3415 if (operand_equal_p (TREE_OPERAND (arg, 0),
3416 TREE_OPERAND (arg, 1), 0))
3417 return 0;
3419 if (*cval1 == 0)
3420 *cval1 = TREE_OPERAND (arg, 0);
3421 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3423 else if (*cval2 == 0)
3424 *cval2 = TREE_OPERAND (arg, 0);
3425 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3427 else
3428 return 0;
3430 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3432 else if (*cval2 == 0)
3433 *cval2 = TREE_OPERAND (arg, 1);
3434 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3436 else
3437 return 0;
3439 return 1;
3441 default:
3442 return 0;
3446 /* ARG is a tree that is known to contain just arithmetic operations and
3447 comparisons. Evaluate the operations in the tree substituting NEW0 for
3448 any occurrence of OLD0 as an operand of a comparison and likewise for
3449 NEW1 and OLD1. */
3451 static tree
3452 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3453 tree old1, tree new1)
3455 tree type = TREE_TYPE (arg);
3456 enum tree_code code = TREE_CODE (arg);
3457 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3459 /* We can handle some of the tcc_expression cases here. */
3460 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3461 tclass = tcc_unary;
3462 else if (tclass == tcc_expression
3463 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3464 tclass = tcc_binary;
3466 switch (tclass)
3468 case tcc_unary:
3469 return fold_build1_loc (loc, code, type,
3470 eval_subst (loc, TREE_OPERAND (arg, 0),
3471 old0, new0, old1, new1));
3473 case tcc_binary:
3474 return fold_build2_loc (loc, code, type,
3475 eval_subst (loc, TREE_OPERAND (arg, 0),
3476 old0, new0, old1, new1),
3477 eval_subst (loc, TREE_OPERAND (arg, 1),
3478 old0, new0, old1, new1));
3480 case tcc_expression:
3481 switch (code)
3483 case SAVE_EXPR:
3484 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3485 old1, new1);
3487 case COMPOUND_EXPR:
3488 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3489 old1, new1);
3491 case COND_EXPR:
3492 return fold_build3_loc (loc, code, type,
3493 eval_subst (loc, TREE_OPERAND (arg, 0),
3494 old0, new0, old1, new1),
3495 eval_subst (loc, TREE_OPERAND (arg, 1),
3496 old0, new0, old1, new1),
3497 eval_subst (loc, TREE_OPERAND (arg, 2),
3498 old0, new0, old1, new1));
3499 default:
3500 break;
3502 /* Fall through - ??? */
3504 case tcc_comparison:
3506 tree arg0 = TREE_OPERAND (arg, 0);
3507 tree arg1 = TREE_OPERAND (arg, 1);
3509 /* We need to check both for exact equality and tree equality. The
3510 former will be true if the operand has a side-effect. In that
3511 case, we know the operand occurred exactly once. */
3513 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3514 arg0 = new0;
3515 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3516 arg0 = new1;
3518 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3519 arg1 = new0;
3520 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3521 arg1 = new1;
3523 return fold_build2_loc (loc, code, type, arg0, arg1);
3526 default:
3527 return arg;
3531 /* Return a tree for the case when the result of an expression is RESULT
3532 converted to TYPE and OMITTED was previously an operand of the expression
3533 but is now not needed (e.g., we folded OMITTED * 0).
3535 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3536 the conversion of RESULT to TYPE. */
3538 tree
3539 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3541 tree t = fold_convert_loc (loc, type, result);
3543 /* If the resulting operand is an empty statement, just return the omitted
3544 statement casted to void. */
3545 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3546 return build1_loc (loc, NOP_EXPR, void_type_node,
3547 fold_ignored_result (omitted));
3549 if (TREE_SIDE_EFFECTS (omitted))
3550 return build2_loc (loc, COMPOUND_EXPR, type,
3551 fold_ignored_result (omitted), t);
3553 return non_lvalue_loc (loc, t);
3556 /* Return a tree for the case when the result of an expression is RESULT
3557 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3558 of the expression but are now not needed.
3560 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3561 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3562 evaluated before OMITTED2. Otherwise, if neither has side effects,
3563 just do the conversion of RESULT to TYPE. */
3565 tree
3566 omit_two_operands_loc (location_t loc, tree type, tree result,
3567 tree omitted1, tree omitted2)
3569 tree t = fold_convert_loc (loc, type, result);
3571 if (TREE_SIDE_EFFECTS (omitted2))
3572 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3573 if (TREE_SIDE_EFFECTS (omitted1))
3574 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3576 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3580 /* Return a simplified tree node for the truth-negation of ARG. This
3581 never alters ARG itself. We assume that ARG is an operation that
3582 returns a truth value (0 or 1).
3584 FIXME: one would think we would fold the result, but it causes
3585 problems with the dominator optimizer. */
3587 static tree
3588 fold_truth_not_expr (location_t loc, tree arg)
3590 tree type = TREE_TYPE (arg);
3591 enum tree_code code = TREE_CODE (arg);
3592 location_t loc1, loc2;
3594 /* If this is a comparison, we can simply invert it, except for
3595 floating-point non-equality comparisons, in which case we just
3596 enclose a TRUTH_NOT_EXPR around what we have. */
3598 if (TREE_CODE_CLASS (code) == tcc_comparison)
3600 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3601 if (FLOAT_TYPE_P (op_type)
3602 && flag_trapping_math
3603 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3604 && code != NE_EXPR && code != EQ_EXPR)
3605 return NULL_TREE;
3607 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3608 if (code == ERROR_MARK)
3609 return NULL_TREE;
3611 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3612 TREE_OPERAND (arg, 1));
3613 if (TREE_NO_WARNING (arg))
3614 TREE_NO_WARNING (ret) = 1;
3615 return ret;
3618 switch (code)
3620 case INTEGER_CST:
3621 return constant_boolean_node (integer_zerop (arg), type);
3623 case TRUTH_AND_EXPR:
3624 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3625 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3626 return build2_loc (loc, TRUTH_OR_EXPR, type,
3627 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3628 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3630 case TRUTH_OR_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_AND_EXPR, type,
3634 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3635 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3637 case TRUTH_XOR_EXPR:
3638 /* Here we can invert either operand. We invert the first operand
3639 unless the second operand is a TRUTH_NOT_EXPR in which case our
3640 result is the XOR of the first operand with the inside of the
3641 negation of the second operand. */
3643 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3644 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3645 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3646 else
3647 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3648 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3649 TREE_OPERAND (arg, 1));
3651 case TRUTH_ANDIF_EXPR:
3652 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3653 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3654 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3655 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3656 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3658 case TRUTH_ORIF_EXPR:
3659 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3660 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3661 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3662 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3663 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3665 case TRUTH_NOT_EXPR:
3666 return TREE_OPERAND (arg, 0);
3668 case COND_EXPR:
3670 tree arg1 = TREE_OPERAND (arg, 1);
3671 tree arg2 = TREE_OPERAND (arg, 2);
3673 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3674 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3676 /* A COND_EXPR may have a throw as one operand, which
3677 then has void type. Just leave void operands
3678 as they are. */
3679 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3680 VOID_TYPE_P (TREE_TYPE (arg1))
3681 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3682 VOID_TYPE_P (TREE_TYPE (arg2))
3683 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3686 case COMPOUND_EXPR:
3687 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3688 return build2_loc (loc, COMPOUND_EXPR, type,
3689 TREE_OPERAND (arg, 0),
3690 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3692 case NON_LVALUE_EXPR:
3693 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3694 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3696 CASE_CONVERT:
3697 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3698 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3700 /* fall through */
3702 case FLOAT_EXPR:
3703 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3704 return build1_loc (loc, TREE_CODE (arg), type,
3705 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3707 case BIT_AND_EXPR:
3708 if (!integer_onep (TREE_OPERAND (arg, 1)))
3709 return NULL_TREE;
3710 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3712 case SAVE_EXPR:
3713 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3715 case CLEANUP_POINT_EXPR:
3716 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3717 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3718 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3720 default:
3721 return NULL_TREE;
3725 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3726 assume that ARG is an operation that returns a truth value (0 or 1
3727 for scalars, 0 or -1 for vectors). Return the folded expression if
3728 folding is successful. Otherwise, return NULL_TREE. */
3730 static tree
3731 fold_invert_truthvalue (location_t loc, tree arg)
3733 tree type = TREE_TYPE (arg);
3734 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3735 ? BIT_NOT_EXPR
3736 : TRUTH_NOT_EXPR,
3737 type, arg);
3740 /* Return a simplified tree node for the truth-negation of ARG. This
3741 never alters ARG itself. We assume that ARG is an operation that
3742 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3744 tree
3745 invert_truthvalue_loc (location_t loc, tree arg)
3747 if (TREE_CODE (arg) == ERROR_MARK)
3748 return arg;
3750 tree type = TREE_TYPE (arg);
3751 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3752 ? BIT_NOT_EXPR
3753 : TRUTH_NOT_EXPR,
3754 type, arg);
3757 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3758 with code CODE. This optimization is unsafe. */
3759 static tree
3760 distribute_real_division (location_t loc, enum tree_code code, tree type,
3761 tree arg0, tree arg1)
3763 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3764 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3766 /* (A / C) +- (B / C) -> (A +- B) / C. */
3767 if (mul0 == mul1
3768 && operand_equal_p (TREE_OPERAND (arg0, 1),
3769 TREE_OPERAND (arg1, 1), 0))
3770 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3771 fold_build2_loc (loc, code, type,
3772 TREE_OPERAND (arg0, 0),
3773 TREE_OPERAND (arg1, 0)),
3774 TREE_OPERAND (arg0, 1));
3776 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3777 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3778 TREE_OPERAND (arg1, 0), 0)
3779 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3780 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3782 REAL_VALUE_TYPE r0, r1;
3783 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3784 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3785 if (!mul0)
3786 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3787 if (!mul1)
3788 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3789 real_arithmetic (&r0, code, &r0, &r1);
3790 return fold_build2_loc (loc, MULT_EXPR, type,
3791 TREE_OPERAND (arg0, 0),
3792 build_real (type, r0));
3795 return NULL_TREE;
3798 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3799 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3800 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3801 is the original memory reference used to preserve the alias set of
3802 the access. */
3804 static tree
3805 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3806 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3807 int unsignedp, int reversep)
3809 tree result, bftype;
3811 if (get_alias_set (inner) != get_alias_set (orig_inner))
3812 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3813 build_fold_addr_expr (inner),
3814 build_int_cst
3815 (reference_alias_ptr_type (orig_inner), 0));
3817 if (bitpos == 0 && !reversep)
3819 tree size = TYPE_SIZE (TREE_TYPE (inner));
3820 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3821 || POINTER_TYPE_P (TREE_TYPE (inner)))
3822 && tree_fits_shwi_p (size)
3823 && tree_to_shwi (size) == bitsize)
3824 return fold_convert_loc (loc, type, inner);
3827 bftype = type;
3828 if (TYPE_PRECISION (bftype) != bitsize
3829 || TYPE_UNSIGNED (bftype) == !unsignedp)
3830 bftype = build_nonstandard_integer_type (bitsize, 0);
3832 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3833 size_int (bitsize), bitsize_int (bitpos));
3834 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3836 if (bftype != type)
3837 result = fold_convert_loc (loc, type, result);
3839 return result;
3842 /* Optimize a bit-field compare.
3844 There are two cases: First is a compare against a constant and the
3845 second is a comparison of two items where the fields are at the same
3846 bit position relative to the start of a chunk (byte, halfword, word)
3847 large enough to contain it. In these cases we can avoid the shift
3848 implicit in bitfield extractions.
3850 For constants, we emit a compare of the shifted constant with the
3851 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3852 compared. For two fields at the same position, we do the ANDs with the
3853 similar mask and compare the result of the ANDs.
3855 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3856 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3857 are the left and right operands of the comparison, respectively.
3859 If the optimization described above can be done, we return the resulting
3860 tree. Otherwise we return zero. */
3862 static tree
3863 optimize_bit_field_compare (location_t loc, enum tree_code code,
3864 tree compare_type, tree lhs, tree rhs)
3866 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3867 tree type = TREE_TYPE (lhs);
3868 tree unsigned_type;
3869 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3870 machine_mode lmode, rmode, nmode;
3871 int lunsignedp, runsignedp;
3872 int lreversep, rreversep;
3873 int lvolatilep = 0, rvolatilep = 0;
3874 tree linner, rinner = NULL_TREE;
3875 tree mask;
3876 tree offset;
3878 /* Get all the information about the extractions being done. If the bit size
3879 if the same as the size of the underlying object, we aren't doing an
3880 extraction at all and so can do nothing. We also don't want to
3881 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3882 then will no longer be able to replace it. */
3883 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3884 &lunsignedp, &lreversep, &lvolatilep);
3885 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3886 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3887 return 0;
3889 if (const_p)
3890 rreversep = lreversep;
3891 else
3893 /* If this is not a constant, we can only do something if bit positions,
3894 sizes, signedness and storage order are the same. */
3895 rinner
3896 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3897 &runsignedp, &rreversep, &rvolatilep);
3899 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3900 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3901 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3902 return 0;
3905 /* Honor the C++ memory model and mimic what RTL expansion does. */
3906 unsigned HOST_WIDE_INT bitstart = 0;
3907 unsigned HOST_WIDE_INT bitend = 0;
3908 if (TREE_CODE (lhs) == COMPONENT_REF)
3910 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3911 if (offset != NULL_TREE)
3912 return 0;
3915 /* See if we can find a mode to refer to this field. We should be able to,
3916 but fail if we can't. */
3917 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3918 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3919 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3920 TYPE_ALIGN (TREE_TYPE (rinner))),
3921 word_mode, false);
3922 if (nmode == VOIDmode)
3923 return 0;
3925 /* Set signed and unsigned types of the precision of this mode for the
3926 shifts below. */
3927 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3929 /* Compute the bit position and size for the new reference and our offset
3930 within it. If the new reference is the same size as the original, we
3931 won't optimize anything, so return zero. */
3932 nbitsize = GET_MODE_BITSIZE (nmode);
3933 nbitpos = lbitpos & ~ (nbitsize - 1);
3934 lbitpos -= nbitpos;
3935 if (nbitsize == lbitsize)
3936 return 0;
3938 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3939 lbitpos = nbitsize - lbitsize - lbitpos;
3941 /* Make the mask to be used against the extracted field. */
3942 mask = build_int_cst_type (unsigned_type, -1);
3943 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3944 mask = const_binop (RSHIFT_EXPR, mask,
3945 size_int (nbitsize - lbitsize - lbitpos));
3947 if (! const_p)
3948 /* If not comparing with constant, just rework the comparison
3949 and return. */
3950 return fold_build2_loc (loc, code, compare_type,
3951 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3952 make_bit_field_ref (loc, linner, lhs,
3953 unsigned_type,
3954 nbitsize, nbitpos,
3955 1, lreversep),
3956 mask),
3957 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3958 make_bit_field_ref (loc, rinner, rhs,
3959 unsigned_type,
3960 nbitsize, nbitpos,
3961 1, rreversep),
3962 mask));
3964 /* Otherwise, we are handling the constant case. See if the constant is too
3965 big for the field. Warn and return a tree for 0 (false) if so. We do
3966 this not only for its own sake, but to avoid having to test for this
3967 error case below. If we didn't, we might generate wrong code.
3969 For unsigned fields, the constant shifted right by the field length should
3970 be all zero. For signed fields, the high-order bits should agree with
3971 the sign bit. */
3973 if (lunsignedp)
3975 if (wi::lrshift (rhs, lbitsize) != 0)
3977 warning (0, "comparison is always %d due to width of bit-field",
3978 code == NE_EXPR);
3979 return constant_boolean_node (code == NE_EXPR, compare_type);
3982 else
3984 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3985 if (tem != 0 && tem != -1)
3987 warning (0, "comparison is always %d due to width of bit-field",
3988 code == NE_EXPR);
3989 return constant_boolean_node (code == NE_EXPR, compare_type);
3993 /* Single-bit compares should always be against zero. */
3994 if (lbitsize == 1 && ! integer_zerop (rhs))
3996 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3997 rhs = build_int_cst (type, 0);
4000 /* Make a new bitfield reference, shift the constant over the
4001 appropriate number of bits and mask it with the computed mask
4002 (in case this was a signed field). If we changed it, make a new one. */
4003 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4004 nbitsize, nbitpos, 1, lreversep);
4006 rhs = const_binop (BIT_AND_EXPR,
4007 const_binop (LSHIFT_EXPR,
4008 fold_convert_loc (loc, unsigned_type, rhs),
4009 size_int (lbitpos)),
4010 mask);
4012 lhs = build2_loc (loc, code, compare_type,
4013 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4014 return lhs;
4017 /* Subroutine for fold_truth_andor_1: decode a field reference.
4019 If EXP is a comparison reference, we return the innermost reference.
4021 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4022 set to the starting bit number.
4024 If the innermost field can be completely contained in a mode-sized
4025 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4027 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4028 otherwise it is not changed.
4030 *PUNSIGNEDP is set to the signedness of the field.
4032 *PREVERSEP is set to the storage order of the field.
4034 *PMASK is set to the mask used. This is either contained in a
4035 BIT_AND_EXPR or derived from the width of the field.
4037 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4039 Return 0 if this is not a component reference or is one that we can't
4040 do anything with. */
4042 static tree
4043 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4044 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4045 int *punsignedp, int *preversep, int *pvolatilep,
4046 tree *pmask, tree *pand_mask)
4048 tree exp = *exp_;
4049 tree outer_type = 0;
4050 tree and_mask = 0;
4051 tree mask, inner, offset;
4052 tree unsigned_type;
4053 unsigned int precision;
4055 /* All the optimizations using this function assume integer fields.
4056 There are problems with FP fields since the type_for_size call
4057 below can fail for, e.g., XFmode. */
4058 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4059 return 0;
4061 /* We are interested in the bare arrangement of bits, so strip everything
4062 that doesn't affect the machine mode. However, record the type of the
4063 outermost expression if it may matter below. */
4064 if (CONVERT_EXPR_P (exp)
4065 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4066 outer_type = TREE_TYPE (exp);
4067 STRIP_NOPS (exp);
4069 if (TREE_CODE (exp) == BIT_AND_EXPR)
4071 and_mask = TREE_OPERAND (exp, 1);
4072 exp = TREE_OPERAND (exp, 0);
4073 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4074 if (TREE_CODE (and_mask) != INTEGER_CST)
4075 return 0;
4078 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4079 punsignedp, preversep, pvolatilep);
4080 if ((inner == exp && and_mask == 0)
4081 || *pbitsize < 0 || offset != 0
4082 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4083 return 0;
4085 *exp_ = exp;
4087 /* If the number of bits in the reference is the same as the bitsize of
4088 the outer type, then the outer type gives the signedness. Otherwise
4089 (in case of a small bitfield) the signedness is unchanged. */
4090 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4091 *punsignedp = TYPE_UNSIGNED (outer_type);
4093 /* Compute the mask to access the bitfield. */
4094 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4095 precision = TYPE_PRECISION (unsigned_type);
4097 mask = build_int_cst_type (unsigned_type, -1);
4099 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4100 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4102 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4103 if (and_mask != 0)
4104 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4105 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4107 *pmask = mask;
4108 *pand_mask = and_mask;
4109 return inner;
4112 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4113 bit positions and MASK is SIGNED. */
4115 static int
4116 all_ones_mask_p (const_tree mask, unsigned int size)
4118 tree type = TREE_TYPE (mask);
4119 unsigned int precision = TYPE_PRECISION (type);
4121 /* If this function returns true when the type of the mask is
4122 UNSIGNED, then there will be errors. In particular see
4123 gcc.c-torture/execute/990326-1.c. There does not appear to be
4124 any documentation paper trail as to why this is so. But the pre
4125 wide-int worked with that restriction and it has been preserved
4126 here. */
4127 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4128 return false;
4130 return wi::mask (size, false, precision) == mask;
4133 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4134 represents the sign bit of EXP's type. If EXP represents a sign
4135 or zero extension, also test VAL against the unextended type.
4136 The return value is the (sub)expression whose sign bit is VAL,
4137 or NULL_TREE otherwise. */
4139 tree
4140 sign_bit_p (tree exp, const_tree val)
4142 int width;
4143 tree t;
4145 /* Tree EXP must have an integral type. */
4146 t = TREE_TYPE (exp);
4147 if (! INTEGRAL_TYPE_P (t))
4148 return NULL_TREE;
4150 /* Tree VAL must be an integer constant. */
4151 if (TREE_CODE (val) != INTEGER_CST
4152 || TREE_OVERFLOW (val))
4153 return NULL_TREE;
4155 width = TYPE_PRECISION (t);
4156 if (wi::only_sign_bit_p (val, width))
4157 return exp;
4159 /* Handle extension from a narrower type. */
4160 if (TREE_CODE (exp) == NOP_EXPR
4161 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4162 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4164 return NULL_TREE;
4167 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4168 to be evaluated unconditionally. */
4170 static int
4171 simple_operand_p (const_tree exp)
4173 /* Strip any conversions that don't change the machine mode. */
4174 STRIP_NOPS (exp);
4176 return (CONSTANT_CLASS_P (exp)
4177 || TREE_CODE (exp) == SSA_NAME
4178 || (DECL_P (exp)
4179 && ! TREE_ADDRESSABLE (exp)
4180 && ! TREE_THIS_VOLATILE (exp)
4181 && ! DECL_NONLOCAL (exp)
4182 /* Don't regard global variables as simple. They may be
4183 allocated in ways unknown to the compiler (shared memory,
4184 #pragma weak, etc). */
4185 && ! TREE_PUBLIC (exp)
4186 && ! DECL_EXTERNAL (exp)
4187 /* Weakrefs are not safe to be read, since they can be NULL.
4188 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4189 have DECL_WEAK flag set. */
4190 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4191 /* Loading a static variable is unduly expensive, but global
4192 registers aren't expensive. */
4193 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4196 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4197 to be evaluated unconditionally.
4198 I addition to simple_operand_p, we assume that comparisons, conversions,
4199 and logic-not operations are simple, if their operands are simple, too. */
4201 static bool
4202 simple_operand_p_2 (tree exp)
4204 enum tree_code code;
4206 if (TREE_SIDE_EFFECTS (exp)
4207 || tree_could_trap_p (exp))
4208 return false;
4210 while (CONVERT_EXPR_P (exp))
4211 exp = TREE_OPERAND (exp, 0);
4213 code = TREE_CODE (exp);
4215 if (TREE_CODE_CLASS (code) == tcc_comparison)
4216 return (simple_operand_p (TREE_OPERAND (exp, 0))
4217 && simple_operand_p (TREE_OPERAND (exp, 1)));
4219 if (code == TRUTH_NOT_EXPR)
4220 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4222 return simple_operand_p (exp);
4226 /* The following functions are subroutines to fold_range_test and allow it to
4227 try to change a logical combination of comparisons into a range test.
4229 For example, both
4230 X == 2 || X == 3 || X == 4 || X == 5
4232 X >= 2 && X <= 5
4233 are converted to
4234 (unsigned) (X - 2) <= 3
4236 We describe each set of comparisons as being either inside or outside
4237 a range, using a variable named like IN_P, and then describe the
4238 range with a lower and upper bound. If one of the bounds is omitted,
4239 it represents either the highest or lowest value of the type.
4241 In the comments below, we represent a range by two numbers in brackets
4242 preceded by a "+" to designate being inside that range, or a "-" to
4243 designate being outside that range, so the condition can be inverted by
4244 flipping the prefix. An omitted bound is represented by a "-". For
4245 example, "- [-, 10]" means being outside the range starting at the lowest
4246 possible value and ending at 10, in other words, being greater than 10.
4247 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4248 always false.
4250 We set up things so that the missing bounds are handled in a consistent
4251 manner so neither a missing bound nor "true" and "false" need to be
4252 handled using a special case. */
4254 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4255 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4256 and UPPER1_P are nonzero if the respective argument is an upper bound
4257 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4258 must be specified for a comparison. ARG1 will be converted to ARG0's
4259 type if both are specified. */
4261 static tree
4262 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4263 tree arg1, int upper1_p)
4265 tree tem;
4266 int result;
4267 int sgn0, sgn1;
4269 /* If neither arg represents infinity, do the normal operation.
4270 Else, if not a comparison, return infinity. Else handle the special
4271 comparison rules. Note that most of the cases below won't occur, but
4272 are handled for consistency. */
4274 if (arg0 != 0 && arg1 != 0)
4276 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4277 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4278 STRIP_NOPS (tem);
4279 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4282 if (TREE_CODE_CLASS (code) != tcc_comparison)
4283 return 0;
4285 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4286 for neither. In real maths, we cannot assume open ended ranges are
4287 the same. But, this is computer arithmetic, where numbers are finite.
4288 We can therefore make the transformation of any unbounded range with
4289 the value Z, Z being greater than any representable number. This permits
4290 us to treat unbounded ranges as equal. */
4291 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4292 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4293 switch (code)
4295 case EQ_EXPR:
4296 result = sgn0 == sgn1;
4297 break;
4298 case NE_EXPR:
4299 result = sgn0 != sgn1;
4300 break;
4301 case LT_EXPR:
4302 result = sgn0 < sgn1;
4303 break;
4304 case LE_EXPR:
4305 result = sgn0 <= sgn1;
4306 break;
4307 case GT_EXPR:
4308 result = sgn0 > sgn1;
4309 break;
4310 case GE_EXPR:
4311 result = sgn0 >= sgn1;
4312 break;
4313 default:
4314 gcc_unreachable ();
4317 return constant_boolean_node (result, type);
4320 /* Helper routine for make_range. Perform one step for it, return
4321 new expression if the loop should continue or NULL_TREE if it should
4322 stop. */
4324 tree
4325 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4326 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4327 bool *strict_overflow_p)
4329 tree arg0_type = TREE_TYPE (arg0);
4330 tree n_low, n_high, low = *p_low, high = *p_high;
4331 int in_p = *p_in_p, n_in_p;
4333 switch (code)
4335 case TRUTH_NOT_EXPR:
4336 /* We can only do something if the range is testing for zero. */
4337 if (low == NULL_TREE || high == NULL_TREE
4338 || ! integer_zerop (low) || ! integer_zerop (high))
4339 return NULL_TREE;
4340 *p_in_p = ! in_p;
4341 return arg0;
4343 case EQ_EXPR: case NE_EXPR:
4344 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4345 /* We can only do something if the range is testing for zero
4346 and if the second operand is an integer constant. Note that
4347 saying something is "in" the range we make is done by
4348 complementing IN_P since it will set in the initial case of
4349 being not equal to zero; "out" is leaving it alone. */
4350 if (low == NULL_TREE || high == NULL_TREE
4351 || ! integer_zerop (low) || ! integer_zerop (high)
4352 || TREE_CODE (arg1) != INTEGER_CST)
4353 return NULL_TREE;
4355 switch (code)
4357 case NE_EXPR: /* - [c, c] */
4358 low = high = arg1;
4359 break;
4360 case EQ_EXPR: /* + [c, c] */
4361 in_p = ! in_p, low = high = arg1;
4362 break;
4363 case GT_EXPR: /* - [-, c] */
4364 low = 0, high = arg1;
4365 break;
4366 case GE_EXPR: /* + [c, -] */
4367 in_p = ! in_p, low = arg1, high = 0;
4368 break;
4369 case LT_EXPR: /* - [c, -] */
4370 low = arg1, high = 0;
4371 break;
4372 case LE_EXPR: /* + [-, c] */
4373 in_p = ! in_p, low = 0, high = arg1;
4374 break;
4375 default:
4376 gcc_unreachable ();
4379 /* If this is an unsigned comparison, we also know that EXP is
4380 greater than or equal to zero. We base the range tests we make
4381 on that fact, so we record it here so we can parse existing
4382 range tests. We test arg0_type since often the return type
4383 of, e.g. EQ_EXPR, is boolean. */
4384 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4386 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4387 in_p, low, high, 1,
4388 build_int_cst (arg0_type, 0),
4389 NULL_TREE))
4390 return NULL_TREE;
4392 in_p = n_in_p, low = n_low, high = n_high;
4394 /* If the high bound is missing, but we have a nonzero low
4395 bound, reverse the range so it goes from zero to the low bound
4396 minus 1. */
4397 if (high == 0 && low && ! integer_zerop (low))
4399 in_p = ! in_p;
4400 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4401 build_int_cst (TREE_TYPE (low), 1), 0);
4402 low = build_int_cst (arg0_type, 0);
4406 *p_low = low;
4407 *p_high = high;
4408 *p_in_p = in_p;
4409 return arg0;
4411 case NEGATE_EXPR:
4412 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4413 low and high are non-NULL, then normalize will DTRT. */
4414 if (!TYPE_UNSIGNED (arg0_type)
4415 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4417 if (low == NULL_TREE)
4418 low = TYPE_MIN_VALUE (arg0_type);
4419 if (high == NULL_TREE)
4420 high = TYPE_MAX_VALUE (arg0_type);
4423 /* (-x) IN [a,b] -> x in [-b, -a] */
4424 n_low = range_binop (MINUS_EXPR, exp_type,
4425 build_int_cst (exp_type, 0),
4426 0, high, 1);
4427 n_high = range_binop (MINUS_EXPR, exp_type,
4428 build_int_cst (exp_type, 0),
4429 0, low, 0);
4430 if (n_high != 0 && TREE_OVERFLOW (n_high))
4431 return NULL_TREE;
4432 goto normalize;
4434 case BIT_NOT_EXPR:
4435 /* ~ X -> -X - 1 */
4436 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4437 build_int_cst (exp_type, 1));
4439 case PLUS_EXPR:
4440 case MINUS_EXPR:
4441 if (TREE_CODE (arg1) != INTEGER_CST)
4442 return NULL_TREE;
4444 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4445 move a constant to the other side. */
4446 if (!TYPE_UNSIGNED (arg0_type)
4447 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4448 return NULL_TREE;
4450 /* If EXP is signed, any overflow in the computation is undefined,
4451 so we don't worry about it so long as our computations on
4452 the bounds don't overflow. For unsigned, overflow is defined
4453 and this is exactly the right thing. */
4454 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4455 arg0_type, low, 0, arg1, 0);
4456 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4457 arg0_type, high, 1, arg1, 0);
4458 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4459 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4460 return NULL_TREE;
4462 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4463 *strict_overflow_p = true;
4465 normalize:
4466 /* Check for an unsigned range which has wrapped around the maximum
4467 value thus making n_high < n_low, and normalize it. */
4468 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4470 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4471 build_int_cst (TREE_TYPE (n_high), 1), 0);
4472 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4473 build_int_cst (TREE_TYPE (n_low), 1), 0);
4475 /* If the range is of the form +/- [ x+1, x ], we won't
4476 be able to normalize it. But then, it represents the
4477 whole range or the empty set, so make it
4478 +/- [ -, - ]. */
4479 if (tree_int_cst_equal (n_low, low)
4480 && tree_int_cst_equal (n_high, high))
4481 low = high = 0;
4482 else
4483 in_p = ! in_p;
4485 else
4486 low = n_low, high = n_high;
4488 *p_low = low;
4489 *p_high = high;
4490 *p_in_p = in_p;
4491 return arg0;
4493 CASE_CONVERT:
4494 case NON_LVALUE_EXPR:
4495 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4496 return NULL_TREE;
4498 if (! INTEGRAL_TYPE_P (arg0_type)
4499 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4500 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4501 return NULL_TREE;
4503 n_low = low, n_high = high;
4505 if (n_low != 0)
4506 n_low = fold_convert_loc (loc, arg0_type, n_low);
4508 if (n_high != 0)
4509 n_high = fold_convert_loc (loc, arg0_type, n_high);
4511 /* If we're converting arg0 from an unsigned type, to exp,
4512 a signed type, we will be doing the comparison as unsigned.
4513 The tests above have already verified that LOW and HIGH
4514 are both positive.
4516 So we have to ensure that we will handle large unsigned
4517 values the same way that the current signed bounds treat
4518 negative values. */
4520 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4522 tree high_positive;
4523 tree equiv_type;
4524 /* For fixed-point modes, we need to pass the saturating flag
4525 as the 2nd parameter. */
4526 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4527 equiv_type
4528 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4529 TYPE_SATURATING (arg0_type));
4530 else
4531 equiv_type
4532 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4534 /* A range without an upper bound is, naturally, unbounded.
4535 Since convert would have cropped a very large value, use
4536 the max value for the destination type. */
4537 high_positive
4538 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4539 : TYPE_MAX_VALUE (arg0_type);
4541 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4542 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4543 fold_convert_loc (loc, arg0_type,
4544 high_positive),
4545 build_int_cst (arg0_type, 1));
4547 /* If the low bound is specified, "and" the range with the
4548 range for which the original unsigned value will be
4549 positive. */
4550 if (low != 0)
4552 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4553 1, fold_convert_loc (loc, arg0_type,
4554 integer_zero_node),
4555 high_positive))
4556 return NULL_TREE;
4558 in_p = (n_in_p == in_p);
4560 else
4562 /* Otherwise, "or" the range with the range of the input
4563 that will be interpreted as negative. */
4564 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4565 1, fold_convert_loc (loc, arg0_type,
4566 integer_zero_node),
4567 high_positive))
4568 return NULL_TREE;
4570 in_p = (in_p != n_in_p);
4574 *p_low = n_low;
4575 *p_high = n_high;
4576 *p_in_p = in_p;
4577 return arg0;
4579 default:
4580 return NULL_TREE;
4584 /* Given EXP, a logical expression, set the range it is testing into
4585 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4586 actually being tested. *PLOW and *PHIGH will be made of the same
4587 type as the returned expression. If EXP is not a comparison, we
4588 will most likely not be returning a useful value and range. Set
4589 *STRICT_OVERFLOW_P to true if the return value is only valid
4590 because signed overflow is undefined; otherwise, do not change
4591 *STRICT_OVERFLOW_P. */
4593 tree
4594 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4595 bool *strict_overflow_p)
4597 enum tree_code code;
4598 tree arg0, arg1 = NULL_TREE;
4599 tree exp_type, nexp;
4600 int in_p;
4601 tree low, high;
4602 location_t loc = EXPR_LOCATION (exp);
4604 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4605 and see if we can refine the range. Some of the cases below may not
4606 happen, but it doesn't seem worth worrying about this. We "continue"
4607 the outer loop when we've changed something; otherwise we "break"
4608 the switch, which will "break" the while. */
4610 in_p = 0;
4611 low = high = build_int_cst (TREE_TYPE (exp), 0);
4613 while (1)
4615 code = TREE_CODE (exp);
4616 exp_type = TREE_TYPE (exp);
4617 arg0 = NULL_TREE;
4619 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4621 if (TREE_OPERAND_LENGTH (exp) > 0)
4622 arg0 = TREE_OPERAND (exp, 0);
4623 if (TREE_CODE_CLASS (code) == tcc_binary
4624 || TREE_CODE_CLASS (code) == tcc_comparison
4625 || (TREE_CODE_CLASS (code) == tcc_expression
4626 && TREE_OPERAND_LENGTH (exp) > 1))
4627 arg1 = TREE_OPERAND (exp, 1);
4629 if (arg0 == NULL_TREE)
4630 break;
4632 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4633 &high, &in_p, strict_overflow_p);
4634 if (nexp == NULL_TREE)
4635 break;
4636 exp = nexp;
4639 /* If EXP is a constant, we can evaluate whether this is true or false. */
4640 if (TREE_CODE (exp) == INTEGER_CST)
4642 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4643 exp, 0, low, 0))
4644 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4645 exp, 1, high, 1)));
4646 low = high = 0;
4647 exp = 0;
4650 *pin_p = in_p, *plow = low, *phigh = high;
4651 return exp;
4654 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4655 type, TYPE, return an expression to test if EXP is in (or out of, depending
4656 on IN_P) the range. Return 0 if the test couldn't be created. */
4658 tree
4659 build_range_check (location_t loc, tree type, tree exp, int in_p,
4660 tree low, tree high)
4662 tree etype = TREE_TYPE (exp), value;
4664 /* Disable this optimization for function pointer expressions
4665 on targets that require function pointer canonicalization. */
4666 if (targetm.have_canonicalize_funcptr_for_compare ()
4667 && TREE_CODE (etype) == POINTER_TYPE
4668 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4669 return NULL_TREE;
4671 if (! in_p)
4673 value = build_range_check (loc, type, exp, 1, low, high);
4674 if (value != 0)
4675 return invert_truthvalue_loc (loc, value);
4677 return 0;
4680 if (low == 0 && high == 0)
4681 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4683 if (low == 0)
4684 return fold_build2_loc (loc, LE_EXPR, type, exp,
4685 fold_convert_loc (loc, etype, high));
4687 if (high == 0)
4688 return fold_build2_loc (loc, GE_EXPR, type, exp,
4689 fold_convert_loc (loc, etype, low));
4691 if (operand_equal_p (low, high, 0))
4692 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4693 fold_convert_loc (loc, etype, low));
4695 if (integer_zerop (low))
4697 if (! TYPE_UNSIGNED (etype))
4699 etype = unsigned_type_for (etype);
4700 high = fold_convert_loc (loc, etype, high);
4701 exp = fold_convert_loc (loc, etype, exp);
4703 return build_range_check (loc, type, exp, 1, 0, high);
4706 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4707 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4709 int prec = TYPE_PRECISION (etype);
4711 if (wi::mask (prec - 1, false, prec) == high)
4713 if (TYPE_UNSIGNED (etype))
4715 tree signed_etype = signed_type_for (etype);
4716 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4717 etype
4718 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4719 else
4720 etype = signed_etype;
4721 exp = fold_convert_loc (loc, etype, exp);
4723 return fold_build2_loc (loc, GT_EXPR, type, exp,
4724 build_int_cst (etype, 0));
4728 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4729 This requires wrap-around arithmetics for the type of the expression.
4730 First make sure that arithmetics in this type is valid, then make sure
4731 that it wraps around. */
4732 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4733 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4734 TYPE_UNSIGNED (etype));
4736 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4738 tree utype, minv, maxv;
4740 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4741 for the type in question, as we rely on this here. */
4742 utype = unsigned_type_for (etype);
4743 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4744 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4745 build_int_cst (TREE_TYPE (maxv), 1), 1);
4746 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4748 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4749 minv, 1, maxv, 1)))
4750 etype = utype;
4751 else
4752 return 0;
4755 high = fold_convert_loc (loc, etype, high);
4756 low = fold_convert_loc (loc, etype, low);
4757 exp = fold_convert_loc (loc, etype, exp);
4759 value = const_binop (MINUS_EXPR, high, low);
4762 if (POINTER_TYPE_P (etype))
4764 if (value != 0 && !TREE_OVERFLOW (value))
4766 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4767 return build_range_check (loc, type,
4768 fold_build_pointer_plus_loc (loc, exp, low),
4769 1, build_int_cst (etype, 0), value);
4771 return 0;
4774 if (value != 0 && !TREE_OVERFLOW (value))
4775 return build_range_check (loc, type,
4776 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4777 1, build_int_cst (etype, 0), value);
4779 return 0;
4782 /* Return the predecessor of VAL in its type, handling the infinite case. */
4784 static tree
4785 range_predecessor (tree val)
4787 tree type = TREE_TYPE (val);
4789 if (INTEGRAL_TYPE_P (type)
4790 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4791 return 0;
4792 else
4793 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4794 build_int_cst (TREE_TYPE (val), 1), 0);
4797 /* Return the successor of VAL in its type, handling the infinite case. */
4799 static tree
4800 range_successor (tree val)
4802 tree type = TREE_TYPE (val);
4804 if (INTEGRAL_TYPE_P (type)
4805 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4806 return 0;
4807 else
4808 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4809 build_int_cst (TREE_TYPE (val), 1), 0);
4812 /* Given two ranges, see if we can merge them into one. Return 1 if we
4813 can, 0 if we can't. Set the output range into the specified parameters. */
4815 bool
4816 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4817 tree high0, int in1_p, tree low1, tree high1)
4819 int no_overlap;
4820 int subset;
4821 int temp;
4822 tree tem;
4823 int in_p;
4824 tree low, high;
4825 int lowequal = ((low0 == 0 && low1 == 0)
4826 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4827 low0, 0, low1, 0)));
4828 int highequal = ((high0 == 0 && high1 == 0)
4829 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4830 high0, 1, high1, 1)));
4832 /* Make range 0 be the range that starts first, or ends last if they
4833 start at the same value. Swap them if it isn't. */
4834 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4835 low0, 0, low1, 0))
4836 || (lowequal
4837 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4838 high1, 1, high0, 1))))
4840 temp = in0_p, in0_p = in1_p, in1_p = temp;
4841 tem = low0, low0 = low1, low1 = tem;
4842 tem = high0, high0 = high1, high1 = tem;
4845 /* Now flag two cases, whether the ranges are disjoint or whether the
4846 second range is totally subsumed in the first. Note that the tests
4847 below are simplified by the ones above. */
4848 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4849 high0, 1, low1, 0));
4850 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4851 high1, 1, high0, 1));
4853 /* We now have four cases, depending on whether we are including or
4854 excluding the two ranges. */
4855 if (in0_p && in1_p)
4857 /* If they don't overlap, the result is false. If the second range
4858 is a subset it is the result. Otherwise, the range is from the start
4859 of the second to the end of the first. */
4860 if (no_overlap)
4861 in_p = 0, low = high = 0;
4862 else if (subset)
4863 in_p = 1, low = low1, high = high1;
4864 else
4865 in_p = 1, low = low1, high = high0;
4868 else if (in0_p && ! in1_p)
4870 /* If they don't overlap, the result is the first range. If they are
4871 equal, the result is false. If the second range is a subset of the
4872 first, and the ranges begin at the same place, we go from just after
4873 the end of the second range to the end of the first. If the second
4874 range is not a subset of the first, or if it is a subset and both
4875 ranges end at the same place, the range starts at the start of the
4876 first range and ends just before the second range.
4877 Otherwise, we can't describe this as a single range. */
4878 if (no_overlap)
4879 in_p = 1, low = low0, high = high0;
4880 else if (lowequal && highequal)
4881 in_p = 0, low = high = 0;
4882 else if (subset && lowequal)
4884 low = range_successor (high1);
4885 high = high0;
4886 in_p = 1;
4887 if (low == 0)
4889 /* We are in the weird situation where high0 > high1 but
4890 high1 has no successor. Punt. */
4891 return 0;
4894 else if (! subset || highequal)
4896 low = low0;
4897 high = range_predecessor (low1);
4898 in_p = 1;
4899 if (high == 0)
4901 /* low0 < low1 but low1 has no predecessor. Punt. */
4902 return 0;
4905 else
4906 return 0;
4909 else if (! in0_p && in1_p)
4911 /* If they don't overlap, the result is the second range. If the second
4912 is a subset of the first, the result is false. Otherwise,
4913 the range starts just after the first range and ends at the
4914 end of the second. */
4915 if (no_overlap)
4916 in_p = 1, low = low1, high = high1;
4917 else if (subset || highequal)
4918 in_p = 0, low = high = 0;
4919 else
4921 low = range_successor (high0);
4922 high = high1;
4923 in_p = 1;
4924 if (low == 0)
4926 /* high1 > high0 but high0 has no successor. Punt. */
4927 return 0;
4932 else
4934 /* The case where we are excluding both ranges. Here the complex case
4935 is if they don't overlap. In that case, the only time we have a
4936 range is if they are adjacent. If the second is a subset of the
4937 first, the result is the first. Otherwise, the range to exclude
4938 starts at the beginning of the first range and ends at the end of the
4939 second. */
4940 if (no_overlap)
4942 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4943 range_successor (high0),
4944 1, low1, 0)))
4945 in_p = 0, low = low0, high = high1;
4946 else
4948 /* Canonicalize - [min, x] into - [-, x]. */
4949 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4950 switch (TREE_CODE (TREE_TYPE (low0)))
4952 case ENUMERAL_TYPE:
4953 if (TYPE_PRECISION (TREE_TYPE (low0))
4954 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4955 break;
4956 /* FALLTHROUGH */
4957 case INTEGER_TYPE:
4958 if (tree_int_cst_equal (low0,
4959 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4960 low0 = 0;
4961 break;
4962 case POINTER_TYPE:
4963 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4964 && integer_zerop (low0))
4965 low0 = 0;
4966 break;
4967 default:
4968 break;
4971 /* Canonicalize - [x, max] into - [x, -]. */
4972 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4973 switch (TREE_CODE (TREE_TYPE (high1)))
4975 case ENUMERAL_TYPE:
4976 if (TYPE_PRECISION (TREE_TYPE (high1))
4977 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4978 break;
4979 /* FALLTHROUGH */
4980 case INTEGER_TYPE:
4981 if (tree_int_cst_equal (high1,
4982 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4983 high1 = 0;
4984 break;
4985 case POINTER_TYPE:
4986 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4987 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4988 high1, 1,
4989 build_int_cst (TREE_TYPE (high1), 1),
4990 1)))
4991 high1 = 0;
4992 break;
4993 default:
4994 break;
4997 /* The ranges might be also adjacent between the maximum and
4998 minimum values of the given type. For
4999 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5000 return + [x + 1, y - 1]. */
5001 if (low0 == 0 && high1 == 0)
5003 low = range_successor (high0);
5004 high = range_predecessor (low1);
5005 if (low == 0 || high == 0)
5006 return 0;
5008 in_p = 1;
5010 else
5011 return 0;
5014 else if (subset)
5015 in_p = 0, low = low0, high = high0;
5016 else
5017 in_p = 0, low = low0, high = high1;
5020 *pin_p = in_p, *plow = low, *phigh = high;
5021 return 1;
5025 /* Subroutine of fold, looking inside expressions of the form
5026 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5027 of the COND_EXPR. This function is being used also to optimize
5028 A op B ? C : A, by reversing the comparison first.
5030 Return a folded expression whose code is not a COND_EXPR
5031 anymore, or NULL_TREE if no folding opportunity is found. */
5033 static tree
5034 fold_cond_expr_with_comparison (location_t loc, tree type,
5035 tree arg0, tree arg1, tree arg2)
5037 enum tree_code comp_code = TREE_CODE (arg0);
5038 tree arg00 = TREE_OPERAND (arg0, 0);
5039 tree arg01 = TREE_OPERAND (arg0, 1);
5040 tree arg1_type = TREE_TYPE (arg1);
5041 tree tem;
5043 STRIP_NOPS (arg1);
5044 STRIP_NOPS (arg2);
5046 /* If we have A op 0 ? A : -A, consider applying the following
5047 transformations:
5049 A == 0? A : -A same as -A
5050 A != 0? A : -A same as A
5051 A >= 0? A : -A same as abs (A)
5052 A > 0? A : -A same as abs (A)
5053 A <= 0? A : -A same as -abs (A)
5054 A < 0? A : -A same as -abs (A)
5056 None of these transformations work for modes with signed
5057 zeros. If A is +/-0, the first two transformations will
5058 change the sign of the result (from +0 to -0, or vice
5059 versa). The last four will fix the sign of the result,
5060 even though the original expressions could be positive or
5061 negative, depending on the sign of A.
5063 Note that all these transformations are correct if A is
5064 NaN, since the two alternatives (A and -A) are also NaNs. */
5065 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5066 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5067 ? real_zerop (arg01)
5068 : integer_zerop (arg01))
5069 && ((TREE_CODE (arg2) == NEGATE_EXPR
5070 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5071 /* In the case that A is of the form X-Y, '-A' (arg2) may
5072 have already been folded to Y-X, check for that. */
5073 || (TREE_CODE (arg1) == MINUS_EXPR
5074 && TREE_CODE (arg2) == MINUS_EXPR
5075 && operand_equal_p (TREE_OPERAND (arg1, 0),
5076 TREE_OPERAND (arg2, 1), 0)
5077 && operand_equal_p (TREE_OPERAND (arg1, 1),
5078 TREE_OPERAND (arg2, 0), 0))))
5079 switch (comp_code)
5081 case EQ_EXPR:
5082 case UNEQ_EXPR:
5083 tem = fold_convert_loc (loc, arg1_type, arg1);
5084 return pedantic_non_lvalue_loc (loc,
5085 fold_convert_loc (loc, type,
5086 negate_expr (tem)));
5087 case NE_EXPR:
5088 case LTGT_EXPR:
5089 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5090 case UNGE_EXPR:
5091 case UNGT_EXPR:
5092 if (flag_trapping_math)
5093 break;
5094 /* Fall through. */
5095 case GE_EXPR:
5096 case GT_EXPR:
5097 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5098 break;
5099 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5100 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5101 case UNLE_EXPR:
5102 case UNLT_EXPR:
5103 if (flag_trapping_math)
5104 break;
5105 /* FALLTHRU */
5106 case LE_EXPR:
5107 case LT_EXPR:
5108 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5109 break;
5110 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5111 return negate_expr (fold_convert_loc (loc, type, tem));
5112 default:
5113 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5114 break;
5117 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5118 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5119 both transformations are correct when A is NaN: A != 0
5120 is then true, and A == 0 is false. */
5122 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5123 && integer_zerop (arg01) && integer_zerop (arg2))
5125 if (comp_code == NE_EXPR)
5126 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5127 else if (comp_code == EQ_EXPR)
5128 return build_zero_cst (type);
5131 /* Try some transformations of A op B ? A : B.
5133 A == B? A : B same as B
5134 A != B? A : B same as A
5135 A >= B? A : B same as max (A, B)
5136 A > B? A : B same as max (B, A)
5137 A <= B? A : B same as min (A, B)
5138 A < B? A : B same as min (B, A)
5140 As above, these transformations don't work in the presence
5141 of signed zeros. For example, if A and B are zeros of
5142 opposite sign, the first two transformations will change
5143 the sign of the result. In the last four, the original
5144 expressions give different results for (A=+0, B=-0) and
5145 (A=-0, B=+0), but the transformed expressions do not.
5147 The first two transformations are correct if either A or B
5148 is a NaN. In the first transformation, the condition will
5149 be false, and B will indeed be chosen. In the case of the
5150 second transformation, the condition A != B will be true,
5151 and A will be chosen.
5153 The conversions to max() and min() are not correct if B is
5154 a number and A is not. The conditions in the original
5155 expressions will be false, so all four give B. The min()
5156 and max() versions would give a NaN instead. */
5157 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5158 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5159 /* Avoid these transformations if the COND_EXPR may be used
5160 as an lvalue in the C++ front-end. PR c++/19199. */
5161 && (in_gimple_form
5162 || VECTOR_TYPE_P (type)
5163 || (! lang_GNU_CXX ()
5164 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5165 || ! maybe_lvalue_p (arg1)
5166 || ! maybe_lvalue_p (arg2)))
5168 tree comp_op0 = arg00;
5169 tree comp_op1 = arg01;
5170 tree comp_type = TREE_TYPE (comp_op0);
5172 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5173 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5175 comp_type = type;
5176 comp_op0 = arg1;
5177 comp_op1 = arg2;
5180 switch (comp_code)
5182 case EQ_EXPR:
5183 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5184 case NE_EXPR:
5185 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5186 case LE_EXPR:
5187 case LT_EXPR:
5188 case UNLE_EXPR:
5189 case UNLT_EXPR:
5190 /* In C++ a ?: expression can be an lvalue, so put the
5191 operand which will be used if they are equal first
5192 so that we can convert this back to the
5193 corresponding COND_EXPR. */
5194 if (!HONOR_NANS (arg1))
5196 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5197 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5198 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5199 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5200 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5201 comp_op1, comp_op0);
5202 return pedantic_non_lvalue_loc (loc,
5203 fold_convert_loc (loc, type, tem));
5205 break;
5206 case GE_EXPR:
5207 case GT_EXPR:
5208 case UNGE_EXPR:
5209 case UNGT_EXPR:
5210 if (!HONOR_NANS (arg1))
5212 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5213 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5214 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5215 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5216 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5217 comp_op1, comp_op0);
5218 return pedantic_non_lvalue_loc (loc,
5219 fold_convert_loc (loc, type, tem));
5221 break;
5222 case UNEQ_EXPR:
5223 if (!HONOR_NANS (arg1))
5224 return pedantic_non_lvalue_loc (loc,
5225 fold_convert_loc (loc, type, arg2));
5226 break;
5227 case LTGT_EXPR:
5228 if (!HONOR_NANS (arg1))
5229 return pedantic_non_lvalue_loc (loc,
5230 fold_convert_loc (loc, type, arg1));
5231 break;
5232 default:
5233 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5234 break;
5238 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5239 we might still be able to simplify this. For example,
5240 if C1 is one less or one more than C2, this might have started
5241 out as a MIN or MAX and been transformed by this function.
5242 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5244 if (INTEGRAL_TYPE_P (type)
5245 && TREE_CODE (arg01) == INTEGER_CST
5246 && TREE_CODE (arg2) == INTEGER_CST)
5247 switch (comp_code)
5249 case EQ_EXPR:
5250 if (TREE_CODE (arg1) == INTEGER_CST)
5251 break;
5252 /* We can replace A with C1 in this case. */
5253 arg1 = fold_convert_loc (loc, type, arg01);
5254 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5256 case LT_EXPR:
5257 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5258 MIN_EXPR, to preserve the signedness of the comparison. */
5259 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5260 OEP_ONLY_CONST)
5261 && operand_equal_p (arg01,
5262 const_binop (PLUS_EXPR, arg2,
5263 build_int_cst (type, 1)),
5264 OEP_ONLY_CONST))
5266 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5267 fold_convert_loc (loc, TREE_TYPE (arg00),
5268 arg2));
5269 return pedantic_non_lvalue_loc (loc,
5270 fold_convert_loc (loc, type, tem));
5272 break;
5274 case LE_EXPR:
5275 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5276 as above. */
5277 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5278 OEP_ONLY_CONST)
5279 && operand_equal_p (arg01,
5280 const_binop (MINUS_EXPR, arg2,
5281 build_int_cst (type, 1)),
5282 OEP_ONLY_CONST))
5284 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5285 fold_convert_loc (loc, TREE_TYPE (arg00),
5286 arg2));
5287 return pedantic_non_lvalue_loc (loc,
5288 fold_convert_loc (loc, type, tem));
5290 break;
5292 case GT_EXPR:
5293 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5294 MAX_EXPR, to preserve the signedness of the comparison. */
5295 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5296 OEP_ONLY_CONST)
5297 && operand_equal_p (arg01,
5298 const_binop (MINUS_EXPR, arg2,
5299 build_int_cst (type, 1)),
5300 OEP_ONLY_CONST))
5302 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5303 fold_convert_loc (loc, TREE_TYPE (arg00),
5304 arg2));
5305 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5307 break;
5309 case GE_EXPR:
5310 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5311 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5312 OEP_ONLY_CONST)
5313 && operand_equal_p (arg01,
5314 const_binop (PLUS_EXPR, arg2,
5315 build_int_cst (type, 1)),
5316 OEP_ONLY_CONST))
5318 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5319 fold_convert_loc (loc, TREE_TYPE (arg00),
5320 arg2));
5321 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5323 break;
5324 case NE_EXPR:
5325 break;
5326 default:
5327 gcc_unreachable ();
5330 return NULL_TREE;
5335 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5336 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5337 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5338 false) >= 2)
5339 #endif
5341 /* EXP is some logical combination of boolean tests. See if we can
5342 merge it into some range test. Return the new tree if so. */
5344 static tree
5345 fold_range_test (location_t loc, enum tree_code code, tree type,
5346 tree op0, tree op1)
5348 int or_op = (code == TRUTH_ORIF_EXPR
5349 || code == TRUTH_OR_EXPR);
5350 int in0_p, in1_p, in_p;
5351 tree low0, low1, low, high0, high1, high;
5352 bool strict_overflow_p = false;
5353 tree tem, lhs, rhs;
5354 const char * const warnmsg = G_("assuming signed overflow does not occur "
5355 "when simplifying range test");
5357 if (!INTEGRAL_TYPE_P (type))
5358 return 0;
5360 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5361 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5363 /* If this is an OR operation, invert both sides; we will invert
5364 again at the end. */
5365 if (or_op)
5366 in0_p = ! in0_p, in1_p = ! in1_p;
5368 /* If both expressions are the same, if we can merge the ranges, and we
5369 can build the range test, return it or it inverted. If one of the
5370 ranges is always true or always false, consider it to be the same
5371 expression as the other. */
5372 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5373 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5374 in1_p, low1, high1)
5375 && 0 != (tem = (build_range_check (loc, type,
5376 lhs != 0 ? lhs
5377 : rhs != 0 ? rhs : integer_zero_node,
5378 in_p, low, high))))
5380 if (strict_overflow_p)
5381 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5382 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5385 /* On machines where the branch cost is expensive, if this is a
5386 short-circuited branch and the underlying object on both sides
5387 is the same, make a non-short-circuit operation. */
5388 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5389 && lhs != 0 && rhs != 0
5390 && (code == TRUTH_ANDIF_EXPR
5391 || code == TRUTH_ORIF_EXPR)
5392 && operand_equal_p (lhs, rhs, 0))
5394 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5395 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5396 which cases we can't do this. */
5397 if (simple_operand_p (lhs))
5398 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5399 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5400 type, op0, op1);
5402 else if (!lang_hooks.decls.global_bindings_p ()
5403 && !CONTAINS_PLACEHOLDER_P (lhs))
5405 tree common = save_expr (lhs);
5407 if (0 != (lhs = build_range_check (loc, type, common,
5408 or_op ? ! in0_p : in0_p,
5409 low0, high0))
5410 && (0 != (rhs = build_range_check (loc, type, common,
5411 or_op ? ! in1_p : in1_p,
5412 low1, high1))))
5414 if (strict_overflow_p)
5415 fold_overflow_warning (warnmsg,
5416 WARN_STRICT_OVERFLOW_COMPARISON);
5417 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5418 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5419 type, lhs, rhs);
5424 return 0;
5427 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5428 bit value. Arrange things so the extra bits will be set to zero if and
5429 only if C is signed-extended to its full width. If MASK is nonzero,
5430 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5432 static tree
5433 unextend (tree c, int p, int unsignedp, tree mask)
5435 tree type = TREE_TYPE (c);
5436 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5437 tree temp;
5439 if (p == modesize || unsignedp)
5440 return c;
5442 /* We work by getting just the sign bit into the low-order bit, then
5443 into the high-order bit, then sign-extend. We then XOR that value
5444 with C. */
5445 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5447 /* We must use a signed type in order to get an arithmetic right shift.
5448 However, we must also avoid introducing accidental overflows, so that
5449 a subsequent call to integer_zerop will work. Hence we must
5450 do the type conversion here. At this point, the constant is either
5451 zero or one, and the conversion to a signed type can never overflow.
5452 We could get an overflow if this conversion is done anywhere else. */
5453 if (TYPE_UNSIGNED (type))
5454 temp = fold_convert (signed_type_for (type), temp);
5456 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5457 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5458 if (mask != 0)
5459 temp = const_binop (BIT_AND_EXPR, temp,
5460 fold_convert (TREE_TYPE (c), mask));
5461 /* If necessary, convert the type back to match the type of C. */
5462 if (TYPE_UNSIGNED (type))
5463 temp = fold_convert (type, temp);
5465 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5468 /* For an expression that has the form
5469 (A && B) || ~B
5471 (A || B) && ~B,
5472 we can drop one of the inner expressions and simplify to
5473 A || ~B
5475 A && ~B
5476 LOC is the location of the resulting expression. OP is the inner
5477 logical operation; the left-hand side in the examples above, while CMPOP
5478 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5479 removing a condition that guards another, as in
5480 (A != NULL && A->...) || A == NULL
5481 which we must not transform. If RHS_ONLY is true, only eliminate the
5482 right-most operand of the inner logical operation. */
5484 static tree
5485 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5486 bool rhs_only)
5488 tree type = TREE_TYPE (cmpop);
5489 enum tree_code code = TREE_CODE (cmpop);
5490 enum tree_code truthop_code = TREE_CODE (op);
5491 tree lhs = TREE_OPERAND (op, 0);
5492 tree rhs = TREE_OPERAND (op, 1);
5493 tree orig_lhs = lhs, orig_rhs = rhs;
5494 enum tree_code rhs_code = TREE_CODE (rhs);
5495 enum tree_code lhs_code = TREE_CODE (lhs);
5496 enum tree_code inv_code;
5498 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5499 return NULL_TREE;
5501 if (TREE_CODE_CLASS (code) != tcc_comparison)
5502 return NULL_TREE;
5504 if (rhs_code == truthop_code)
5506 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5507 if (newrhs != NULL_TREE)
5509 rhs = newrhs;
5510 rhs_code = TREE_CODE (rhs);
5513 if (lhs_code == truthop_code && !rhs_only)
5515 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5516 if (newlhs != NULL_TREE)
5518 lhs = newlhs;
5519 lhs_code = TREE_CODE (lhs);
5523 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5524 if (inv_code == rhs_code
5525 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5526 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5527 return lhs;
5528 if (!rhs_only && inv_code == lhs_code
5529 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5530 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5531 return rhs;
5532 if (rhs != orig_rhs || lhs != orig_lhs)
5533 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5534 lhs, rhs);
5535 return NULL_TREE;
5538 /* Find ways of folding logical expressions of LHS and RHS:
5539 Try to merge two comparisons to the same innermost item.
5540 Look for range tests like "ch >= '0' && ch <= '9'".
5541 Look for combinations of simple terms on machines with expensive branches
5542 and evaluate the RHS unconditionally.
5544 For example, if we have p->a == 2 && p->b == 4 and we can make an
5545 object large enough to span both A and B, we can do this with a comparison
5546 against the object ANDed with the a mask.
5548 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5549 operations to do this with one comparison.
5551 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5552 function and the one above.
5554 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5555 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5557 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5558 two operands.
5560 We return the simplified tree or 0 if no optimization is possible. */
5562 static tree
5563 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5564 tree lhs, tree rhs)
5566 /* If this is the "or" of two comparisons, we can do something if
5567 the comparisons are NE_EXPR. If this is the "and", we can do something
5568 if the comparisons are EQ_EXPR. I.e.,
5569 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5571 WANTED_CODE is this operation code. For single bit fields, we can
5572 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5573 comparison for one-bit fields. */
5575 enum tree_code wanted_code;
5576 enum tree_code lcode, rcode;
5577 tree ll_arg, lr_arg, rl_arg, rr_arg;
5578 tree ll_inner, lr_inner, rl_inner, rr_inner;
5579 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5580 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5581 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5582 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5583 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5584 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5585 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5586 machine_mode lnmode, rnmode;
5587 tree ll_mask, lr_mask, rl_mask, rr_mask;
5588 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5589 tree l_const, r_const;
5590 tree lntype, rntype, result;
5591 HOST_WIDE_INT first_bit, end_bit;
5592 int volatilep;
5594 /* Start by getting the comparison codes. Fail if anything is volatile.
5595 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5596 it were surrounded with a NE_EXPR. */
5598 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5599 return 0;
5601 lcode = TREE_CODE (lhs);
5602 rcode = TREE_CODE (rhs);
5604 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5606 lhs = build2 (NE_EXPR, truth_type, lhs,
5607 build_int_cst (TREE_TYPE (lhs), 0));
5608 lcode = NE_EXPR;
5611 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5613 rhs = build2 (NE_EXPR, truth_type, rhs,
5614 build_int_cst (TREE_TYPE (rhs), 0));
5615 rcode = NE_EXPR;
5618 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5619 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5620 return 0;
5622 ll_arg = TREE_OPERAND (lhs, 0);
5623 lr_arg = TREE_OPERAND (lhs, 1);
5624 rl_arg = TREE_OPERAND (rhs, 0);
5625 rr_arg = TREE_OPERAND (rhs, 1);
5627 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5628 if (simple_operand_p (ll_arg)
5629 && simple_operand_p (lr_arg))
5631 if (operand_equal_p (ll_arg, rl_arg, 0)
5632 && operand_equal_p (lr_arg, rr_arg, 0))
5634 result = combine_comparisons (loc, code, lcode, rcode,
5635 truth_type, ll_arg, lr_arg);
5636 if (result)
5637 return result;
5639 else if (operand_equal_p (ll_arg, rr_arg, 0)
5640 && operand_equal_p (lr_arg, rl_arg, 0))
5642 result = combine_comparisons (loc, code, lcode,
5643 swap_tree_comparison (rcode),
5644 truth_type, ll_arg, lr_arg);
5645 if (result)
5646 return result;
5650 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5651 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5653 /* If the RHS can be evaluated unconditionally and its operands are
5654 simple, it wins to evaluate the RHS unconditionally on machines
5655 with expensive branches. In this case, this isn't a comparison
5656 that can be merged. */
5658 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5659 false) >= 2
5660 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5661 && simple_operand_p (rl_arg)
5662 && simple_operand_p (rr_arg))
5664 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5665 if (code == TRUTH_OR_EXPR
5666 && lcode == NE_EXPR && integer_zerop (lr_arg)
5667 && rcode == NE_EXPR && integer_zerop (rr_arg)
5668 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5669 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5670 return build2_loc (loc, NE_EXPR, truth_type,
5671 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5672 ll_arg, rl_arg),
5673 build_int_cst (TREE_TYPE (ll_arg), 0));
5675 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5676 if (code == TRUTH_AND_EXPR
5677 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5678 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5679 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5680 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5681 return build2_loc (loc, EQ_EXPR, truth_type,
5682 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5683 ll_arg, rl_arg),
5684 build_int_cst (TREE_TYPE (ll_arg), 0));
5687 /* See if the comparisons can be merged. Then get all the parameters for
5688 each side. */
5690 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5691 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5692 return 0;
5694 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5695 volatilep = 0;
5696 ll_inner = decode_field_reference (loc, &ll_arg,
5697 &ll_bitsize, &ll_bitpos, &ll_mode,
5698 &ll_unsignedp, &ll_reversep, &volatilep,
5699 &ll_mask, &ll_and_mask);
5700 lr_inner = decode_field_reference (loc, &lr_arg,
5701 &lr_bitsize, &lr_bitpos, &lr_mode,
5702 &lr_unsignedp, &lr_reversep, &volatilep,
5703 &lr_mask, &lr_and_mask);
5704 rl_inner = decode_field_reference (loc, &rl_arg,
5705 &rl_bitsize, &rl_bitpos, &rl_mode,
5706 &rl_unsignedp, &rl_reversep, &volatilep,
5707 &rl_mask, &rl_and_mask);
5708 rr_inner = decode_field_reference (loc, &rr_arg,
5709 &rr_bitsize, &rr_bitpos, &rr_mode,
5710 &rr_unsignedp, &rr_reversep, &volatilep,
5711 &rr_mask, &rr_and_mask);
5713 /* It must be true that the inner operation on the lhs of each
5714 comparison must be the same if we are to be able to do anything.
5715 Then see if we have constants. If not, the same must be true for
5716 the rhs's. */
5717 if (volatilep
5718 || ll_reversep != rl_reversep
5719 || ll_inner == 0 || rl_inner == 0
5720 || ! operand_equal_p (ll_inner, rl_inner, 0))
5721 return 0;
5723 if (TREE_CODE (lr_arg) == INTEGER_CST
5724 && TREE_CODE (rr_arg) == INTEGER_CST)
5726 l_const = lr_arg, r_const = rr_arg;
5727 lr_reversep = ll_reversep;
5729 else if (lr_reversep != rr_reversep
5730 || lr_inner == 0 || rr_inner == 0
5731 || ! operand_equal_p (lr_inner, rr_inner, 0))
5732 return 0;
5733 else
5734 l_const = r_const = 0;
5736 /* If either comparison code is not correct for our logical operation,
5737 fail. However, we can convert a one-bit comparison against zero into
5738 the opposite comparison against that bit being set in the field. */
5740 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5741 if (lcode != wanted_code)
5743 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5745 /* Make the left operand unsigned, since we are only interested
5746 in the value of one bit. Otherwise we are doing the wrong
5747 thing below. */
5748 ll_unsignedp = 1;
5749 l_const = ll_mask;
5751 else
5752 return 0;
5755 /* This is analogous to the code for l_const above. */
5756 if (rcode != wanted_code)
5758 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5760 rl_unsignedp = 1;
5761 r_const = rl_mask;
5763 else
5764 return 0;
5767 /* See if we can find a mode that contains both fields being compared on
5768 the left. If we can't, fail. Otherwise, update all constants and masks
5769 to be relative to a field of that size. */
5770 first_bit = MIN (ll_bitpos, rl_bitpos);
5771 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5772 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5773 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5774 volatilep);
5775 if (lnmode == VOIDmode)
5776 return 0;
5778 lnbitsize = GET_MODE_BITSIZE (lnmode);
5779 lnbitpos = first_bit & ~ (lnbitsize - 1);
5780 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5781 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5783 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5785 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5786 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5789 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5790 size_int (xll_bitpos));
5791 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5792 size_int (xrl_bitpos));
5794 if (l_const)
5796 l_const = fold_convert_loc (loc, lntype, l_const);
5797 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5798 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5799 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5800 fold_build1_loc (loc, BIT_NOT_EXPR,
5801 lntype, ll_mask))))
5803 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5805 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5808 if (r_const)
5810 r_const = fold_convert_loc (loc, lntype, r_const);
5811 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5812 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5813 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5814 fold_build1_loc (loc, BIT_NOT_EXPR,
5815 lntype, rl_mask))))
5817 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5819 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5823 /* If the right sides are not constant, do the same for it. Also,
5824 disallow this optimization if a size or signedness mismatch occurs
5825 between the left and right sides. */
5826 if (l_const == 0)
5828 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5829 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5830 /* Make sure the two fields on the right
5831 correspond to the left without being swapped. */
5832 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5833 return 0;
5835 first_bit = MIN (lr_bitpos, rr_bitpos);
5836 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5837 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5838 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5839 volatilep);
5840 if (rnmode == VOIDmode)
5841 return 0;
5843 rnbitsize = GET_MODE_BITSIZE (rnmode);
5844 rnbitpos = first_bit & ~ (rnbitsize - 1);
5845 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5846 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5848 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5850 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5851 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5854 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5855 rntype, lr_mask),
5856 size_int (xlr_bitpos));
5857 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5858 rntype, rr_mask),
5859 size_int (xrr_bitpos));
5861 /* Make a mask that corresponds to both fields being compared.
5862 Do this for both items being compared. If the operands are the
5863 same size and the bits being compared are in the same position
5864 then we can do this by masking both and comparing the masked
5865 results. */
5866 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5867 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5868 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5870 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5871 lntype, lnbitsize, lnbitpos,
5872 ll_unsignedp || rl_unsignedp, ll_reversep);
5873 if (! all_ones_mask_p (ll_mask, lnbitsize))
5874 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5876 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5877 rntype, rnbitsize, rnbitpos,
5878 lr_unsignedp || rr_unsignedp, lr_reversep);
5879 if (! all_ones_mask_p (lr_mask, rnbitsize))
5880 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5882 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5885 /* There is still another way we can do something: If both pairs of
5886 fields being compared are adjacent, we may be able to make a wider
5887 field containing them both.
5889 Note that we still must mask the lhs/rhs expressions. Furthermore,
5890 the mask must be shifted to account for the shift done by
5891 make_bit_field_ref. */
5892 if ((ll_bitsize + ll_bitpos == rl_bitpos
5893 && lr_bitsize + lr_bitpos == rr_bitpos)
5894 || (ll_bitpos == rl_bitpos + rl_bitsize
5895 && lr_bitpos == rr_bitpos + rr_bitsize))
5897 tree type;
5899 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5900 ll_bitsize + rl_bitsize,
5901 MIN (ll_bitpos, rl_bitpos),
5902 ll_unsignedp, ll_reversep);
5903 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5904 lr_bitsize + rr_bitsize,
5905 MIN (lr_bitpos, rr_bitpos),
5906 lr_unsignedp, lr_reversep);
5908 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5909 size_int (MIN (xll_bitpos, xrl_bitpos)));
5910 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5911 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5913 /* Convert to the smaller type before masking out unwanted bits. */
5914 type = lntype;
5915 if (lntype != rntype)
5917 if (lnbitsize > rnbitsize)
5919 lhs = fold_convert_loc (loc, rntype, lhs);
5920 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5921 type = rntype;
5923 else if (lnbitsize < rnbitsize)
5925 rhs = fold_convert_loc (loc, lntype, rhs);
5926 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5927 type = lntype;
5931 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5932 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5934 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5935 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5937 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5940 return 0;
5943 /* Handle the case of comparisons with constants. If there is something in
5944 common between the masks, those bits of the constants must be the same.
5945 If not, the condition is always false. Test for this to avoid generating
5946 incorrect code below. */
5947 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5948 if (! integer_zerop (result)
5949 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5950 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5952 if (wanted_code == NE_EXPR)
5954 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5955 return constant_boolean_node (true, truth_type);
5957 else
5959 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5960 return constant_boolean_node (false, truth_type);
5964 /* Construct the expression we will return. First get the component
5965 reference we will make. Unless the mask is all ones the width of
5966 that field, perform the mask operation. Then compare with the
5967 merged constant. */
5968 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5969 lntype, lnbitsize, lnbitpos,
5970 ll_unsignedp || rl_unsignedp, ll_reversep);
5972 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5973 if (! all_ones_mask_p (ll_mask, lnbitsize))
5974 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5976 return build2_loc (loc, wanted_code, truth_type, result,
5977 const_binop (BIT_IOR_EXPR, l_const, r_const));
5980 /* T is an integer expression that is being multiplied, divided, or taken a
5981 modulus (CODE says which and what kind of divide or modulus) by a
5982 constant C. See if we can eliminate that operation by folding it with
5983 other operations already in T. WIDE_TYPE, if non-null, is a type that
5984 should be used for the computation if wider than our type.
5986 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5987 (X * 2) + (Y * 4). We must, however, be assured that either the original
5988 expression would not overflow or that overflow is undefined for the type
5989 in the language in question.
5991 If we return a non-null expression, it is an equivalent form of the
5992 original computation, but need not be in the original type.
5994 We set *STRICT_OVERFLOW_P to true if the return values depends on
5995 signed overflow being undefined. Otherwise we do not change
5996 *STRICT_OVERFLOW_P. */
5998 static tree
5999 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6000 bool *strict_overflow_p)
6002 /* To avoid exponential search depth, refuse to allow recursion past
6003 three levels. Beyond that (1) it's highly unlikely that we'll find
6004 something interesting and (2) we've probably processed it before
6005 when we built the inner expression. */
6007 static int depth;
6008 tree ret;
6010 if (depth > 3)
6011 return NULL;
6013 depth++;
6014 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6015 depth--;
6017 return ret;
6020 static tree
6021 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6022 bool *strict_overflow_p)
6024 tree type = TREE_TYPE (t);
6025 enum tree_code tcode = TREE_CODE (t);
6026 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6027 > GET_MODE_SIZE (TYPE_MODE (type)))
6028 ? wide_type : type);
6029 tree t1, t2;
6030 int same_p = tcode == code;
6031 tree op0 = NULL_TREE, op1 = NULL_TREE;
6032 bool sub_strict_overflow_p;
6034 /* Don't deal with constants of zero here; they confuse the code below. */
6035 if (integer_zerop (c))
6036 return NULL_TREE;
6038 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6039 op0 = TREE_OPERAND (t, 0);
6041 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6042 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6044 /* Note that we need not handle conditional operations here since fold
6045 already handles those cases. So just do arithmetic here. */
6046 switch (tcode)
6048 case INTEGER_CST:
6049 /* For a constant, we can always simplify if we are a multiply
6050 or (for divide and modulus) if it is a multiple of our constant. */
6051 if (code == MULT_EXPR
6052 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6054 tree tem = const_binop (code, fold_convert (ctype, t),
6055 fold_convert (ctype, c));
6056 /* If the multiplication overflowed, we lost information on it.
6057 See PR68142 and PR69845. */
6058 if (TREE_OVERFLOW (tem))
6059 return NULL_TREE;
6060 return tem;
6062 break;
6064 CASE_CONVERT: case NON_LVALUE_EXPR:
6065 /* If op0 is an expression ... */
6066 if ((COMPARISON_CLASS_P (op0)
6067 || UNARY_CLASS_P (op0)
6068 || BINARY_CLASS_P (op0)
6069 || VL_EXP_CLASS_P (op0)
6070 || EXPRESSION_CLASS_P (op0))
6071 /* ... and has wrapping overflow, and its type is smaller
6072 than ctype, then we cannot pass through as widening. */
6073 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6074 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6075 && (TYPE_PRECISION (ctype)
6076 > TYPE_PRECISION (TREE_TYPE (op0))))
6077 /* ... or this is a truncation (t is narrower than op0),
6078 then we cannot pass through this narrowing. */
6079 || (TYPE_PRECISION (type)
6080 < TYPE_PRECISION (TREE_TYPE (op0)))
6081 /* ... or signedness changes for division or modulus,
6082 then we cannot pass through this conversion. */
6083 || (code != MULT_EXPR
6084 && (TYPE_UNSIGNED (ctype)
6085 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6086 /* ... or has undefined overflow while the converted to
6087 type has not, we cannot do the operation in the inner type
6088 as that would introduce undefined overflow. */
6089 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6090 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6091 && !TYPE_OVERFLOW_UNDEFINED (type))))
6092 break;
6094 /* Pass the constant down and see if we can make a simplification. If
6095 we can, replace this expression with the inner simplification for
6096 possible later conversion to our or some other type. */
6097 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6098 && TREE_CODE (t2) == INTEGER_CST
6099 && !TREE_OVERFLOW (t2)
6100 && (0 != (t1 = extract_muldiv (op0, t2, code,
6101 code == MULT_EXPR
6102 ? ctype : NULL_TREE,
6103 strict_overflow_p))))
6104 return t1;
6105 break;
6107 case ABS_EXPR:
6108 /* If widening the type changes it from signed to unsigned, then we
6109 must avoid building ABS_EXPR itself as unsigned. */
6110 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6112 tree cstype = (*signed_type_for) (ctype);
6113 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6114 != 0)
6116 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6117 return fold_convert (ctype, t1);
6119 break;
6121 /* If the constant is negative, we cannot simplify this. */
6122 if (tree_int_cst_sgn (c) == -1)
6123 break;
6124 /* FALLTHROUGH */
6125 case NEGATE_EXPR:
6126 /* For division and modulus, type can't be unsigned, as e.g.
6127 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6128 For signed types, even with wrapping overflow, this is fine. */
6129 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6130 break;
6131 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6132 != 0)
6133 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6134 break;
6136 case MIN_EXPR: case MAX_EXPR:
6137 /* If widening the type changes the signedness, then we can't perform
6138 this optimization as that changes the result. */
6139 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6140 break;
6142 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6143 sub_strict_overflow_p = false;
6144 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6145 &sub_strict_overflow_p)) != 0
6146 && (t2 = extract_muldiv (op1, c, code, wide_type,
6147 &sub_strict_overflow_p)) != 0)
6149 if (tree_int_cst_sgn (c) < 0)
6150 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6151 if (sub_strict_overflow_p)
6152 *strict_overflow_p = true;
6153 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6154 fold_convert (ctype, t2));
6156 break;
6158 case LSHIFT_EXPR: case RSHIFT_EXPR:
6159 /* If the second operand is constant, this is a multiplication
6160 or floor division, by a power of two, so we can treat it that
6161 way unless the multiplier or divisor overflows. Signed
6162 left-shift overflow is implementation-defined rather than
6163 undefined in C90, so do not convert signed left shift into
6164 multiplication. */
6165 if (TREE_CODE (op1) == INTEGER_CST
6166 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6167 /* const_binop may not detect overflow correctly,
6168 so check for it explicitly here. */
6169 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6170 && 0 != (t1 = fold_convert (ctype,
6171 const_binop (LSHIFT_EXPR,
6172 size_one_node,
6173 op1)))
6174 && !TREE_OVERFLOW (t1))
6175 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6176 ? MULT_EXPR : FLOOR_DIV_EXPR,
6177 ctype,
6178 fold_convert (ctype, op0),
6179 t1),
6180 c, code, wide_type, strict_overflow_p);
6181 break;
6183 case PLUS_EXPR: case MINUS_EXPR:
6184 /* See if we can eliminate the operation on both sides. If we can, we
6185 can return a new PLUS or MINUS. If we can't, the only remaining
6186 cases where we can do anything are if the second operand is a
6187 constant. */
6188 sub_strict_overflow_p = false;
6189 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6190 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6191 if (t1 != 0 && t2 != 0
6192 && (code == MULT_EXPR
6193 /* If not multiplication, we can only do this if both operands
6194 are divisible by c. */
6195 || (multiple_of_p (ctype, op0, c)
6196 && multiple_of_p (ctype, op1, c))))
6198 if (sub_strict_overflow_p)
6199 *strict_overflow_p = true;
6200 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6201 fold_convert (ctype, t2));
6204 /* If this was a subtraction, negate OP1 and set it to be an addition.
6205 This simplifies the logic below. */
6206 if (tcode == MINUS_EXPR)
6208 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6209 /* If OP1 was not easily negatable, the constant may be OP0. */
6210 if (TREE_CODE (op0) == INTEGER_CST)
6212 std::swap (op0, op1);
6213 std::swap (t1, t2);
6217 if (TREE_CODE (op1) != INTEGER_CST)
6218 break;
6220 /* If either OP1 or C are negative, this optimization is not safe for
6221 some of the division and remainder types while for others we need
6222 to change the code. */
6223 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6225 if (code == CEIL_DIV_EXPR)
6226 code = FLOOR_DIV_EXPR;
6227 else if (code == FLOOR_DIV_EXPR)
6228 code = CEIL_DIV_EXPR;
6229 else if (code != MULT_EXPR
6230 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6231 break;
6234 /* If it's a multiply or a division/modulus operation of a multiple
6235 of our constant, do the operation and verify it doesn't overflow. */
6236 if (code == MULT_EXPR
6237 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6239 op1 = const_binop (code, fold_convert (ctype, op1),
6240 fold_convert (ctype, c));
6241 /* We allow the constant to overflow with wrapping semantics. */
6242 if (op1 == 0
6243 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6244 break;
6246 else
6247 break;
6249 /* If we have an unsigned type, we cannot widen the operation since it
6250 will change the result if the original computation overflowed. */
6251 if (TYPE_UNSIGNED (ctype) && ctype != type)
6252 break;
6254 /* If we were able to eliminate our operation from the first side,
6255 apply our operation to the second side and reform the PLUS. */
6256 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6257 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6259 /* The last case is if we are a multiply. In that case, we can
6260 apply the distributive law to commute the multiply and addition
6261 if the multiplication of the constants doesn't overflow
6262 and overflow is defined. With undefined overflow
6263 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6264 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6265 return fold_build2 (tcode, ctype,
6266 fold_build2 (code, ctype,
6267 fold_convert (ctype, op0),
6268 fold_convert (ctype, c)),
6269 op1);
6271 break;
6273 case MULT_EXPR:
6274 /* We have a special case here if we are doing something like
6275 (C * 8) % 4 since we know that's zero. */
6276 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6277 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6278 /* If the multiplication can overflow we cannot optimize this. */
6279 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6280 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6281 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6283 *strict_overflow_p = true;
6284 return omit_one_operand (type, integer_zero_node, op0);
6287 /* ... fall through ... */
6289 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6290 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6291 /* If we can extract our operation from the LHS, do so and return a
6292 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6293 do something only if the second operand is a constant. */
6294 if (same_p
6295 && (t1 = extract_muldiv (op0, c, code, wide_type,
6296 strict_overflow_p)) != 0)
6297 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6298 fold_convert (ctype, op1));
6299 else if (tcode == MULT_EXPR && code == MULT_EXPR
6300 && (t1 = extract_muldiv (op1, c, code, wide_type,
6301 strict_overflow_p)) != 0)
6302 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6303 fold_convert (ctype, t1));
6304 else if (TREE_CODE (op1) != INTEGER_CST)
6305 return 0;
6307 /* If these are the same operation types, we can associate them
6308 assuming no overflow. */
6309 if (tcode == code)
6311 bool overflow_p = false;
6312 bool overflow_mul_p;
6313 signop sign = TYPE_SIGN (ctype);
6314 unsigned prec = TYPE_PRECISION (ctype);
6315 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6316 wi::to_wide (c, prec),
6317 sign, &overflow_mul_p);
6318 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6319 if (overflow_mul_p
6320 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6321 overflow_p = true;
6322 if (!overflow_p)
6323 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6324 wide_int_to_tree (ctype, mul));
6327 /* If these operations "cancel" each other, we have the main
6328 optimizations of this pass, which occur when either constant is a
6329 multiple of the other, in which case we replace this with either an
6330 operation or CODE or TCODE.
6332 If we have an unsigned type, we cannot do this since it will change
6333 the result if the original computation overflowed. */
6334 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6335 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6336 || (tcode == MULT_EXPR
6337 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6338 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6339 && code != MULT_EXPR)))
6341 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6343 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6344 *strict_overflow_p = true;
6345 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6346 fold_convert (ctype,
6347 const_binop (TRUNC_DIV_EXPR,
6348 op1, c)));
6350 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6352 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6353 *strict_overflow_p = true;
6354 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6355 fold_convert (ctype,
6356 const_binop (TRUNC_DIV_EXPR,
6357 c, op1)));
6360 break;
6362 default:
6363 break;
6366 return 0;
6369 /* Return a node which has the indicated constant VALUE (either 0 or
6370 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6371 and is of the indicated TYPE. */
6373 tree
6374 constant_boolean_node (bool value, tree type)
6376 if (type == integer_type_node)
6377 return value ? integer_one_node : integer_zero_node;
6378 else if (type == boolean_type_node)
6379 return value ? boolean_true_node : boolean_false_node;
6380 else if (TREE_CODE (type) == VECTOR_TYPE)
6381 return build_vector_from_val (type,
6382 build_int_cst (TREE_TYPE (type),
6383 value ? -1 : 0));
6384 else
6385 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6389 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6390 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6391 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6392 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6393 COND is the first argument to CODE; otherwise (as in the example
6394 given here), it is the second argument. TYPE is the type of the
6395 original expression. Return NULL_TREE if no simplification is
6396 possible. */
6398 static tree
6399 fold_binary_op_with_conditional_arg (location_t loc,
6400 enum tree_code code,
6401 tree type, tree op0, tree op1,
6402 tree cond, tree arg, int cond_first_p)
6404 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6405 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6406 tree test, true_value, false_value;
6407 tree lhs = NULL_TREE;
6408 tree rhs = NULL_TREE;
6409 enum tree_code cond_code = COND_EXPR;
6411 if (TREE_CODE (cond) == COND_EXPR
6412 || TREE_CODE (cond) == VEC_COND_EXPR)
6414 test = TREE_OPERAND (cond, 0);
6415 true_value = TREE_OPERAND (cond, 1);
6416 false_value = TREE_OPERAND (cond, 2);
6417 /* If this operand throws an expression, then it does not make
6418 sense to try to perform a logical or arithmetic operation
6419 involving it. */
6420 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6421 lhs = true_value;
6422 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6423 rhs = false_value;
6425 else if (!(TREE_CODE (type) != VECTOR_TYPE
6426 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6428 tree testtype = TREE_TYPE (cond);
6429 test = cond;
6430 true_value = constant_boolean_node (true, testtype);
6431 false_value = constant_boolean_node (false, testtype);
6433 else
6434 /* Detect the case of mixing vector and scalar types - bail out. */
6435 return NULL_TREE;
6437 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6438 cond_code = VEC_COND_EXPR;
6440 /* This transformation is only worthwhile if we don't have to wrap ARG
6441 in a SAVE_EXPR and the operation can be simplified without recursing
6442 on at least one of the branches once its pushed inside the COND_EXPR. */
6443 if (!TREE_CONSTANT (arg)
6444 && (TREE_SIDE_EFFECTS (arg)
6445 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6446 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6447 return NULL_TREE;
6449 arg = fold_convert_loc (loc, arg_type, arg);
6450 if (lhs == 0)
6452 true_value = fold_convert_loc (loc, cond_type, true_value);
6453 if (cond_first_p)
6454 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6455 else
6456 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6458 if (rhs == 0)
6460 false_value = fold_convert_loc (loc, cond_type, false_value);
6461 if (cond_first_p)
6462 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6463 else
6464 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6467 /* Check that we have simplified at least one of the branches. */
6468 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6469 return NULL_TREE;
6471 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6475 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6477 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6478 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6479 ADDEND is the same as X.
6481 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6482 and finite. The problematic cases are when X is zero, and its mode
6483 has signed zeros. In the case of rounding towards -infinity,
6484 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6485 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6487 bool
6488 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6490 if (!real_zerop (addend))
6491 return false;
6493 /* Don't allow the fold with -fsignaling-nans. */
6494 if (HONOR_SNANS (element_mode (type)))
6495 return false;
6497 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6498 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6499 return true;
6501 /* In a vector or complex, we would need to check the sign of all zeros. */
6502 if (TREE_CODE (addend) != REAL_CST)
6503 return false;
6505 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6506 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6507 negate = !negate;
6509 /* The mode has signed zeros, and we have to honor their sign.
6510 In this situation, there is only one case we can return true for.
6511 X - 0 is the same as X unless rounding towards -infinity is
6512 supported. */
6513 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6516 /* Subroutine of fold() that optimizes comparisons of a division by
6517 a nonzero integer constant against an integer constant, i.e.
6518 X/C1 op C2.
6520 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6521 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6522 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6524 The function returns the constant folded tree if a simplification
6525 can be made, and NULL_TREE otherwise. */
6527 static tree
6528 fold_div_compare (location_t loc,
6529 enum tree_code code, tree type, tree arg0, tree arg1)
6531 tree prod, tmp, hi, lo;
6532 tree arg00 = TREE_OPERAND (arg0, 0);
6533 tree arg01 = TREE_OPERAND (arg0, 1);
6534 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6535 bool neg_overflow = false;
6536 bool overflow;
6538 /* We have to do this the hard way to detect unsigned overflow.
6539 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6540 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6541 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6542 neg_overflow = false;
6544 if (sign == UNSIGNED)
6546 tmp = int_const_binop (MINUS_EXPR, arg01,
6547 build_int_cst (TREE_TYPE (arg01), 1));
6548 lo = prod;
6550 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6551 val = wi::add (prod, tmp, sign, &overflow);
6552 hi = force_fit_type (TREE_TYPE (arg00), val,
6553 -1, overflow | TREE_OVERFLOW (prod));
6555 else if (tree_int_cst_sgn (arg01) >= 0)
6557 tmp = int_const_binop (MINUS_EXPR, arg01,
6558 build_int_cst (TREE_TYPE (arg01), 1));
6559 switch (tree_int_cst_sgn (arg1))
6561 case -1:
6562 neg_overflow = true;
6563 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6564 hi = prod;
6565 break;
6567 case 0:
6568 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6569 hi = tmp;
6570 break;
6572 case 1:
6573 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6574 lo = prod;
6575 break;
6577 default:
6578 gcc_unreachable ();
6581 else
6583 /* A negative divisor reverses the relational operators. */
6584 code = swap_tree_comparison (code);
6586 tmp = int_const_binop (PLUS_EXPR, arg01,
6587 build_int_cst (TREE_TYPE (arg01), 1));
6588 switch (tree_int_cst_sgn (arg1))
6590 case -1:
6591 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6592 lo = prod;
6593 break;
6595 case 0:
6596 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6597 lo = tmp;
6598 break;
6600 case 1:
6601 neg_overflow = true;
6602 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6603 hi = prod;
6604 break;
6606 default:
6607 gcc_unreachable ();
6611 switch (code)
6613 case EQ_EXPR:
6614 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6615 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6616 if (TREE_OVERFLOW (hi))
6617 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6618 if (TREE_OVERFLOW (lo))
6619 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6620 return build_range_check (loc, type, arg00, 1, lo, hi);
6622 case NE_EXPR:
6623 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6624 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6625 if (TREE_OVERFLOW (hi))
6626 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6627 if (TREE_OVERFLOW (lo))
6628 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6629 return build_range_check (loc, type, arg00, 0, lo, hi);
6631 case LT_EXPR:
6632 if (TREE_OVERFLOW (lo))
6634 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6635 return omit_one_operand_loc (loc, type, tmp, arg00);
6637 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6639 case LE_EXPR:
6640 if (TREE_OVERFLOW (hi))
6642 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6643 return omit_one_operand_loc (loc, type, tmp, arg00);
6645 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6647 case GT_EXPR:
6648 if (TREE_OVERFLOW (hi))
6650 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6651 return omit_one_operand_loc (loc, type, tmp, arg00);
6653 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6655 case GE_EXPR:
6656 if (TREE_OVERFLOW (lo))
6658 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6659 return omit_one_operand_loc (loc, type, tmp, arg00);
6661 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6663 default:
6664 break;
6667 return NULL_TREE;
6671 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6672 equality/inequality test, then return a simplified form of the test
6673 using a sign testing. Otherwise return NULL. TYPE is the desired
6674 result type. */
6676 static tree
6677 fold_single_bit_test_into_sign_test (location_t loc,
6678 enum tree_code code, tree arg0, tree arg1,
6679 tree result_type)
6681 /* If this is testing a single bit, we can optimize the test. */
6682 if ((code == NE_EXPR || code == EQ_EXPR)
6683 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6684 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6686 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6687 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6688 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6690 if (arg00 != NULL_TREE
6691 /* This is only a win if casting to a signed type is cheap,
6692 i.e. when arg00's type is not a partial mode. */
6693 && TYPE_PRECISION (TREE_TYPE (arg00))
6694 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6696 tree stype = signed_type_for (TREE_TYPE (arg00));
6697 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6698 result_type,
6699 fold_convert_loc (loc, stype, arg00),
6700 build_int_cst (stype, 0));
6704 return NULL_TREE;
6707 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6708 equality/inequality test, then return a simplified form of
6709 the test using shifts and logical operations. Otherwise return
6710 NULL. TYPE is the desired result type. */
6712 tree
6713 fold_single_bit_test (location_t loc, enum tree_code code,
6714 tree arg0, tree arg1, tree result_type)
6716 /* If this is testing a single bit, we can optimize the test. */
6717 if ((code == NE_EXPR || code == EQ_EXPR)
6718 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6719 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6721 tree inner = TREE_OPERAND (arg0, 0);
6722 tree type = TREE_TYPE (arg0);
6723 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6724 machine_mode operand_mode = TYPE_MODE (type);
6725 int ops_unsigned;
6726 tree signed_type, unsigned_type, intermediate_type;
6727 tree tem, one;
6729 /* First, see if we can fold the single bit test into a sign-bit
6730 test. */
6731 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6732 result_type);
6733 if (tem)
6734 return tem;
6736 /* Otherwise we have (A & C) != 0 where C is a single bit,
6737 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6738 Similarly for (A & C) == 0. */
6740 /* If INNER is a right shift of a constant and it plus BITNUM does
6741 not overflow, adjust BITNUM and INNER. */
6742 if (TREE_CODE (inner) == RSHIFT_EXPR
6743 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6744 && bitnum < TYPE_PRECISION (type)
6745 && wi::ltu_p (TREE_OPERAND (inner, 1),
6746 TYPE_PRECISION (type) - bitnum))
6748 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6749 inner = TREE_OPERAND (inner, 0);
6752 /* If we are going to be able to omit the AND below, we must do our
6753 operations as unsigned. If we must use the AND, we have a choice.
6754 Normally unsigned is faster, but for some machines signed is. */
6755 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6756 && !flag_syntax_only) ? 0 : 1;
6758 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6759 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6760 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6761 inner = fold_convert_loc (loc, intermediate_type, inner);
6763 if (bitnum != 0)
6764 inner = build2 (RSHIFT_EXPR, intermediate_type,
6765 inner, size_int (bitnum));
6767 one = build_int_cst (intermediate_type, 1);
6769 if (code == EQ_EXPR)
6770 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6772 /* Put the AND last so it can combine with more things. */
6773 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6775 /* Make sure to return the proper type. */
6776 inner = fold_convert_loc (loc, result_type, inner);
6778 return inner;
6780 return NULL_TREE;
6783 /* Check whether we are allowed to reorder operands arg0 and arg1,
6784 such that the evaluation of arg1 occurs before arg0. */
6786 static bool
6787 reorder_operands_p (const_tree arg0, const_tree arg1)
6789 if (! flag_evaluation_order)
6790 return true;
6791 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6792 return true;
6793 return ! TREE_SIDE_EFFECTS (arg0)
6794 && ! TREE_SIDE_EFFECTS (arg1);
6797 /* Test whether it is preferable two swap two operands, ARG0 and
6798 ARG1, for example because ARG0 is an integer constant and ARG1
6799 isn't. If REORDER is true, only recommend swapping if we can
6800 evaluate the operands in reverse order. */
6802 bool
6803 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6805 if (CONSTANT_CLASS_P (arg1))
6806 return 0;
6807 if (CONSTANT_CLASS_P (arg0))
6808 return 1;
6810 STRIP_NOPS (arg0);
6811 STRIP_NOPS (arg1);
6813 if (TREE_CONSTANT (arg1))
6814 return 0;
6815 if (TREE_CONSTANT (arg0))
6816 return 1;
6818 if (reorder && flag_evaluation_order
6819 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6820 return 0;
6822 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6823 for commutative and comparison operators. Ensuring a canonical
6824 form allows the optimizers to find additional redundancies without
6825 having to explicitly check for both orderings. */
6826 if (TREE_CODE (arg0) == SSA_NAME
6827 && TREE_CODE (arg1) == SSA_NAME
6828 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6829 return 1;
6831 /* Put SSA_NAMEs last. */
6832 if (TREE_CODE (arg1) == SSA_NAME)
6833 return 0;
6834 if (TREE_CODE (arg0) == SSA_NAME)
6835 return 1;
6837 /* Put variables last. */
6838 if (DECL_P (arg1))
6839 return 0;
6840 if (DECL_P (arg0))
6841 return 1;
6843 return 0;
6847 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6848 means A >= Y && A != MAX, but in this case we know that
6849 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6851 static tree
6852 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6854 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6856 if (TREE_CODE (bound) == LT_EXPR)
6857 a = TREE_OPERAND (bound, 0);
6858 else if (TREE_CODE (bound) == GT_EXPR)
6859 a = TREE_OPERAND (bound, 1);
6860 else
6861 return NULL_TREE;
6863 typea = TREE_TYPE (a);
6864 if (!INTEGRAL_TYPE_P (typea)
6865 && !POINTER_TYPE_P (typea))
6866 return NULL_TREE;
6868 if (TREE_CODE (ineq) == LT_EXPR)
6870 a1 = TREE_OPERAND (ineq, 1);
6871 y = TREE_OPERAND (ineq, 0);
6873 else if (TREE_CODE (ineq) == GT_EXPR)
6875 a1 = TREE_OPERAND (ineq, 0);
6876 y = TREE_OPERAND (ineq, 1);
6878 else
6879 return NULL_TREE;
6881 if (TREE_TYPE (a1) != typea)
6882 return NULL_TREE;
6884 if (POINTER_TYPE_P (typea))
6886 /* Convert the pointer types into integer before taking the difference. */
6887 tree ta = fold_convert_loc (loc, ssizetype, a);
6888 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6889 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6891 else
6892 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6894 if (!diff || !integer_onep (diff))
6895 return NULL_TREE;
6897 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6900 /* Fold a sum or difference of at least one multiplication.
6901 Returns the folded tree or NULL if no simplification could be made. */
6903 static tree
6904 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6905 tree arg0, tree arg1)
6907 tree arg00, arg01, arg10, arg11;
6908 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6910 /* (A * C) +- (B * C) -> (A+-B) * C.
6911 (A * C) +- A -> A * (C+-1).
6912 We are most concerned about the case where C is a constant,
6913 but other combinations show up during loop reduction. Since
6914 it is not difficult, try all four possibilities. */
6916 if (TREE_CODE (arg0) == MULT_EXPR)
6918 arg00 = TREE_OPERAND (arg0, 0);
6919 arg01 = TREE_OPERAND (arg0, 1);
6921 else if (TREE_CODE (arg0) == INTEGER_CST)
6923 arg00 = build_one_cst (type);
6924 arg01 = arg0;
6926 else
6928 /* We cannot generate constant 1 for fract. */
6929 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6930 return NULL_TREE;
6931 arg00 = arg0;
6932 arg01 = build_one_cst (type);
6934 if (TREE_CODE (arg1) == MULT_EXPR)
6936 arg10 = TREE_OPERAND (arg1, 0);
6937 arg11 = TREE_OPERAND (arg1, 1);
6939 else if (TREE_CODE (arg1) == INTEGER_CST)
6941 arg10 = build_one_cst (type);
6942 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6943 the purpose of this canonicalization. */
6944 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6945 && negate_expr_p (arg1)
6946 && code == PLUS_EXPR)
6948 arg11 = negate_expr (arg1);
6949 code = MINUS_EXPR;
6951 else
6952 arg11 = arg1;
6954 else
6956 /* We cannot generate constant 1 for fract. */
6957 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6958 return NULL_TREE;
6959 arg10 = arg1;
6960 arg11 = build_one_cst (type);
6962 same = NULL_TREE;
6964 if (operand_equal_p (arg01, arg11, 0))
6965 same = arg01, alt0 = arg00, alt1 = arg10;
6966 else if (operand_equal_p (arg00, arg10, 0))
6967 same = arg00, alt0 = arg01, alt1 = arg11;
6968 else if (operand_equal_p (arg00, arg11, 0))
6969 same = arg00, alt0 = arg01, alt1 = arg10;
6970 else if (operand_equal_p (arg01, arg10, 0))
6971 same = arg01, alt0 = arg00, alt1 = arg11;
6973 /* No identical multiplicands; see if we can find a common
6974 power-of-two factor in non-power-of-two multiplies. This
6975 can help in multi-dimensional array access. */
6976 else if (tree_fits_shwi_p (arg01)
6977 && tree_fits_shwi_p (arg11))
6979 HOST_WIDE_INT int01, int11, tmp;
6980 bool swap = false;
6981 tree maybe_same;
6982 int01 = tree_to_shwi (arg01);
6983 int11 = tree_to_shwi (arg11);
6985 /* Move min of absolute values to int11. */
6986 if (absu_hwi (int01) < absu_hwi (int11))
6988 tmp = int01, int01 = int11, int11 = tmp;
6989 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6990 maybe_same = arg01;
6991 swap = true;
6993 else
6994 maybe_same = arg11;
6996 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6997 /* The remainder should not be a constant, otherwise we
6998 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6999 increased the number of multiplications necessary. */
7000 && TREE_CODE (arg10) != INTEGER_CST)
7002 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7003 build_int_cst (TREE_TYPE (arg00),
7004 int01 / int11));
7005 alt1 = arg10;
7006 same = maybe_same;
7007 if (swap)
7008 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7012 if (same)
7013 return fold_build2_loc (loc, MULT_EXPR, type,
7014 fold_build2_loc (loc, code, type,
7015 fold_convert_loc (loc, type, alt0),
7016 fold_convert_loc (loc, type, alt1)),
7017 fold_convert_loc (loc, type, same));
7019 return NULL_TREE;
7022 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7023 specified by EXPR into the buffer PTR of length LEN bytes.
7024 Return the number of bytes placed in the buffer, or zero
7025 upon failure. */
7027 static int
7028 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7030 tree type = TREE_TYPE (expr);
7031 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7032 int byte, offset, word, words;
7033 unsigned char value;
7035 if ((off == -1 && total_bytes > len)
7036 || off >= total_bytes)
7037 return 0;
7038 if (off == -1)
7039 off = 0;
7040 words = total_bytes / UNITS_PER_WORD;
7042 for (byte = 0; byte < total_bytes; byte++)
7044 int bitpos = byte * BITS_PER_UNIT;
7045 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7046 number of bytes. */
7047 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7049 if (total_bytes > UNITS_PER_WORD)
7051 word = byte / UNITS_PER_WORD;
7052 if (WORDS_BIG_ENDIAN)
7053 word = (words - 1) - word;
7054 offset = word * UNITS_PER_WORD;
7055 if (BYTES_BIG_ENDIAN)
7056 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7057 else
7058 offset += byte % UNITS_PER_WORD;
7060 else
7061 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7062 if (offset >= off
7063 && offset - off < len)
7064 ptr[offset - off] = value;
7066 return MIN (len, total_bytes - off);
7070 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7071 specified by EXPR into the buffer PTR of length LEN bytes.
7072 Return the number of bytes placed in the buffer, or zero
7073 upon failure. */
7075 static int
7076 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7078 tree type = TREE_TYPE (expr);
7079 machine_mode mode = TYPE_MODE (type);
7080 int total_bytes = GET_MODE_SIZE (mode);
7081 FIXED_VALUE_TYPE value;
7082 tree i_value, i_type;
7084 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7085 return 0;
7087 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7089 if (NULL_TREE == i_type
7090 || TYPE_PRECISION (i_type) != total_bytes)
7091 return 0;
7093 value = TREE_FIXED_CST (expr);
7094 i_value = double_int_to_tree (i_type, value.data);
7096 return native_encode_int (i_value, ptr, len, off);
7100 /* Subroutine of native_encode_expr. Encode the REAL_CST
7101 specified by EXPR into the buffer PTR of length LEN bytes.
7102 Return the number of bytes placed in the buffer, or zero
7103 upon failure. */
7105 static int
7106 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7108 tree type = TREE_TYPE (expr);
7109 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7110 int byte, offset, word, words, bitpos;
7111 unsigned char value;
7113 /* There are always 32 bits in each long, no matter the size of
7114 the hosts long. We handle floating point representations with
7115 up to 192 bits. */
7116 long tmp[6];
7118 if ((off == -1 && total_bytes > len)
7119 || off >= total_bytes)
7120 return 0;
7121 if (off == -1)
7122 off = 0;
7123 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7125 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7127 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7128 bitpos += BITS_PER_UNIT)
7130 byte = (bitpos / BITS_PER_UNIT) & 3;
7131 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7133 if (UNITS_PER_WORD < 4)
7135 word = byte / UNITS_PER_WORD;
7136 if (WORDS_BIG_ENDIAN)
7137 word = (words - 1) - word;
7138 offset = word * UNITS_PER_WORD;
7139 if (BYTES_BIG_ENDIAN)
7140 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7141 else
7142 offset += byte % UNITS_PER_WORD;
7144 else
7145 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7146 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7147 if (offset >= off
7148 && offset - off < len)
7149 ptr[offset - off] = value;
7151 return MIN (len, total_bytes - off);
7154 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7155 specified by EXPR into the buffer PTR of length LEN bytes.
7156 Return the number of bytes placed in the buffer, or zero
7157 upon failure. */
7159 static int
7160 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7162 int rsize, isize;
7163 tree part;
7165 part = TREE_REALPART (expr);
7166 rsize = native_encode_expr (part, ptr, len, off);
7167 if (off == -1
7168 && rsize == 0)
7169 return 0;
7170 part = TREE_IMAGPART (expr);
7171 if (off != -1)
7172 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7173 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7174 if (off == -1
7175 && isize != rsize)
7176 return 0;
7177 return rsize + isize;
7181 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7182 specified by EXPR into the buffer PTR of length LEN bytes.
7183 Return the number of bytes placed in the buffer, or zero
7184 upon failure. */
7186 static int
7187 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7189 unsigned i, count;
7190 int size, offset;
7191 tree itype, elem;
7193 offset = 0;
7194 count = VECTOR_CST_NELTS (expr);
7195 itype = TREE_TYPE (TREE_TYPE (expr));
7196 size = GET_MODE_SIZE (TYPE_MODE (itype));
7197 for (i = 0; i < count; i++)
7199 if (off >= size)
7201 off -= size;
7202 continue;
7204 elem = VECTOR_CST_ELT (expr, i);
7205 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7206 if ((off == -1 && res != size)
7207 || res == 0)
7208 return 0;
7209 offset += res;
7210 if (offset >= len)
7211 return offset;
7212 if (off != -1)
7213 off = 0;
7215 return offset;
7219 /* Subroutine of native_encode_expr. Encode the STRING_CST
7220 specified by EXPR into the buffer PTR of length LEN bytes.
7221 Return the number of bytes placed in the buffer, or zero
7222 upon failure. */
7224 static int
7225 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7227 tree type = TREE_TYPE (expr);
7228 HOST_WIDE_INT total_bytes;
7230 if (TREE_CODE (type) != ARRAY_TYPE
7231 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7232 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7233 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7234 return 0;
7235 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7236 if ((off == -1 && total_bytes > len)
7237 || off >= total_bytes)
7238 return 0;
7239 if (off == -1)
7240 off = 0;
7241 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7243 int written = 0;
7244 if (off < TREE_STRING_LENGTH (expr))
7246 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7247 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7249 memset (ptr + written, 0,
7250 MIN (total_bytes - written, len - written));
7252 else
7253 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7254 return MIN (total_bytes - off, len);
7258 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7259 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7260 buffer PTR of length LEN bytes. If OFF is not -1 then start
7261 the encoding at byte offset OFF and encode at most LEN bytes.
7262 Return the number of bytes placed in the buffer, or zero upon failure. */
7265 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7267 /* We don't support starting at negative offset and -1 is special. */
7268 if (off < -1)
7269 return 0;
7271 switch (TREE_CODE (expr))
7273 case INTEGER_CST:
7274 return native_encode_int (expr, ptr, len, off);
7276 case REAL_CST:
7277 return native_encode_real (expr, ptr, len, off);
7279 case FIXED_CST:
7280 return native_encode_fixed (expr, ptr, len, off);
7282 case COMPLEX_CST:
7283 return native_encode_complex (expr, ptr, len, off);
7285 case VECTOR_CST:
7286 return native_encode_vector (expr, ptr, len, off);
7288 case STRING_CST:
7289 return native_encode_string (expr, ptr, len, off);
7291 default:
7292 return 0;
7297 /* Subroutine of native_interpret_expr. Interpret the contents of
7298 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7299 If the buffer cannot be interpreted, return NULL_TREE. */
7301 static tree
7302 native_interpret_int (tree type, const unsigned char *ptr, int len)
7304 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7306 if (total_bytes > len
7307 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7308 return NULL_TREE;
7310 wide_int result = wi::from_buffer (ptr, total_bytes);
7312 return wide_int_to_tree (type, result);
7316 /* Subroutine of native_interpret_expr. Interpret the contents of
7317 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7318 If the buffer cannot be interpreted, return NULL_TREE. */
7320 static tree
7321 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7323 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7324 double_int result;
7325 FIXED_VALUE_TYPE fixed_value;
7327 if (total_bytes > len
7328 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7329 return NULL_TREE;
7331 result = double_int::from_buffer (ptr, total_bytes);
7332 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7334 return build_fixed (type, fixed_value);
7338 /* Subroutine of native_interpret_expr. Interpret the contents of
7339 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7340 If the buffer cannot be interpreted, return NULL_TREE. */
7342 static tree
7343 native_interpret_real (tree type, const unsigned char *ptr, int len)
7345 machine_mode mode = TYPE_MODE (type);
7346 int total_bytes = GET_MODE_SIZE (mode);
7347 unsigned char value;
7348 /* There are always 32 bits in each long, no matter the size of
7349 the hosts long. We handle floating point representations with
7350 up to 192 bits. */
7351 REAL_VALUE_TYPE r;
7352 long tmp[6];
7354 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7355 if (total_bytes > len || total_bytes > 24)
7356 return NULL_TREE;
7357 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7359 memset (tmp, 0, sizeof (tmp));
7360 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7361 bitpos += BITS_PER_UNIT)
7363 /* Both OFFSET and BYTE index within a long;
7364 bitpos indexes the whole float. */
7365 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7366 if (UNITS_PER_WORD < 4)
7368 int word = byte / UNITS_PER_WORD;
7369 if (WORDS_BIG_ENDIAN)
7370 word = (words - 1) - word;
7371 offset = word * UNITS_PER_WORD;
7372 if (BYTES_BIG_ENDIAN)
7373 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7374 else
7375 offset += byte % UNITS_PER_WORD;
7377 else
7379 offset = byte;
7380 if (BYTES_BIG_ENDIAN)
7382 /* Reverse bytes within each long, or within the entire float
7383 if it's smaller than a long (for HFmode). */
7384 offset = MIN (3, total_bytes - 1) - offset;
7385 gcc_assert (offset >= 0);
7388 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7390 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7393 real_from_target (&r, tmp, mode);
7394 return build_real (type, r);
7398 /* Subroutine of native_interpret_expr. Interpret the contents of
7399 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7400 If the buffer cannot be interpreted, return NULL_TREE. */
7402 static tree
7403 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7405 tree etype, rpart, ipart;
7406 int size;
7408 etype = TREE_TYPE (type);
7409 size = GET_MODE_SIZE (TYPE_MODE (etype));
7410 if (size * 2 > len)
7411 return NULL_TREE;
7412 rpart = native_interpret_expr (etype, ptr, size);
7413 if (!rpart)
7414 return NULL_TREE;
7415 ipart = native_interpret_expr (etype, ptr+size, size);
7416 if (!ipart)
7417 return NULL_TREE;
7418 return build_complex (type, rpart, ipart);
7422 /* Subroutine of native_interpret_expr. Interpret the contents of
7423 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7424 If the buffer cannot be interpreted, return NULL_TREE. */
7426 static tree
7427 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7429 tree etype, elem;
7430 int i, size, count;
7431 tree *elements;
7433 etype = TREE_TYPE (type);
7434 size = GET_MODE_SIZE (TYPE_MODE (etype));
7435 count = TYPE_VECTOR_SUBPARTS (type);
7436 if (size * count > len)
7437 return NULL_TREE;
7439 elements = XALLOCAVEC (tree, count);
7440 for (i = count - 1; i >= 0; i--)
7442 elem = native_interpret_expr (etype, ptr+(i*size), size);
7443 if (!elem)
7444 return NULL_TREE;
7445 elements[i] = elem;
7447 return build_vector (type, elements);
7451 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7452 the buffer PTR of length LEN as a constant of type TYPE. For
7453 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7454 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7455 return NULL_TREE. */
7457 tree
7458 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7460 switch (TREE_CODE (type))
7462 case INTEGER_TYPE:
7463 case ENUMERAL_TYPE:
7464 case BOOLEAN_TYPE:
7465 case POINTER_TYPE:
7466 case REFERENCE_TYPE:
7467 return native_interpret_int (type, ptr, len);
7469 case REAL_TYPE:
7470 return native_interpret_real (type, ptr, len);
7472 case FIXED_POINT_TYPE:
7473 return native_interpret_fixed (type, ptr, len);
7475 case COMPLEX_TYPE:
7476 return native_interpret_complex (type, ptr, len);
7478 case VECTOR_TYPE:
7479 return native_interpret_vector (type, ptr, len);
7481 default:
7482 return NULL_TREE;
7486 /* Returns true if we can interpret the contents of a native encoding
7487 as TYPE. */
7489 static bool
7490 can_native_interpret_type_p (tree type)
7492 switch (TREE_CODE (type))
7494 case INTEGER_TYPE:
7495 case ENUMERAL_TYPE:
7496 case BOOLEAN_TYPE:
7497 case POINTER_TYPE:
7498 case REFERENCE_TYPE:
7499 case FIXED_POINT_TYPE:
7500 case REAL_TYPE:
7501 case COMPLEX_TYPE:
7502 case VECTOR_TYPE:
7503 return true;
7504 default:
7505 return false;
7509 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7510 TYPE at compile-time. If we're unable to perform the conversion
7511 return NULL_TREE. */
7513 static tree
7514 fold_view_convert_expr (tree type, tree expr)
7516 /* We support up to 512-bit values (for V8DFmode). */
7517 unsigned char buffer[64];
7518 int len;
7520 /* Check that the host and target are sane. */
7521 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7522 return NULL_TREE;
7524 len = native_encode_expr (expr, buffer, sizeof (buffer));
7525 if (len == 0)
7526 return NULL_TREE;
7528 return native_interpret_expr (type, buffer, len);
7531 /* Build an expression for the address of T. Folds away INDIRECT_REF
7532 to avoid confusing the gimplify process. */
7534 tree
7535 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7537 /* The size of the object is not relevant when talking about its address. */
7538 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7539 t = TREE_OPERAND (t, 0);
7541 if (TREE_CODE (t) == INDIRECT_REF)
7543 t = TREE_OPERAND (t, 0);
7545 if (TREE_TYPE (t) != ptrtype)
7546 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7548 else if (TREE_CODE (t) == MEM_REF
7549 && integer_zerop (TREE_OPERAND (t, 1)))
7550 return TREE_OPERAND (t, 0);
7551 else if (TREE_CODE (t) == MEM_REF
7552 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7553 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7554 TREE_OPERAND (t, 0),
7555 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7556 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7558 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7560 if (TREE_TYPE (t) != ptrtype)
7561 t = fold_convert_loc (loc, ptrtype, t);
7563 else
7564 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7566 return t;
7569 /* Build an expression for the address of T. */
7571 tree
7572 build_fold_addr_expr_loc (location_t loc, tree t)
7574 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7576 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7579 /* Fold a unary expression of code CODE and type TYPE with operand
7580 OP0. Return the folded expression if folding is successful.
7581 Otherwise, return NULL_TREE. */
7583 tree
7584 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7586 tree tem;
7587 tree arg0;
7588 enum tree_code_class kind = TREE_CODE_CLASS (code);
7590 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7591 && TREE_CODE_LENGTH (code) == 1);
7593 arg0 = op0;
7594 if (arg0)
7596 if (CONVERT_EXPR_CODE_P (code)
7597 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7599 /* Don't use STRIP_NOPS, because signedness of argument type
7600 matters. */
7601 STRIP_SIGN_NOPS (arg0);
7603 else
7605 /* Strip any conversions that don't change the mode. This
7606 is safe for every expression, except for a comparison
7607 expression because its signedness is derived from its
7608 operands.
7610 Note that this is done as an internal manipulation within
7611 the constant folder, in order to find the simplest
7612 representation of the arguments so that their form can be
7613 studied. In any cases, the appropriate type conversions
7614 should be put back in the tree that will get out of the
7615 constant folder. */
7616 STRIP_NOPS (arg0);
7619 if (CONSTANT_CLASS_P (arg0))
7621 tree tem = const_unop (code, type, arg0);
7622 if (tem)
7624 if (TREE_TYPE (tem) != type)
7625 tem = fold_convert_loc (loc, type, tem);
7626 return tem;
7631 tem = generic_simplify (loc, code, type, op0);
7632 if (tem)
7633 return tem;
7635 if (TREE_CODE_CLASS (code) == tcc_unary)
7637 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7638 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7639 fold_build1_loc (loc, code, type,
7640 fold_convert_loc (loc, TREE_TYPE (op0),
7641 TREE_OPERAND (arg0, 1))));
7642 else if (TREE_CODE (arg0) == COND_EXPR)
7644 tree arg01 = TREE_OPERAND (arg0, 1);
7645 tree arg02 = TREE_OPERAND (arg0, 2);
7646 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7647 arg01 = fold_build1_loc (loc, code, type,
7648 fold_convert_loc (loc,
7649 TREE_TYPE (op0), arg01));
7650 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7651 arg02 = fold_build1_loc (loc, code, type,
7652 fold_convert_loc (loc,
7653 TREE_TYPE (op0), arg02));
7654 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7655 arg01, arg02);
7657 /* If this was a conversion, and all we did was to move into
7658 inside the COND_EXPR, bring it back out. But leave it if
7659 it is a conversion from integer to integer and the
7660 result precision is no wider than a word since such a
7661 conversion is cheap and may be optimized away by combine,
7662 while it couldn't if it were outside the COND_EXPR. Then return
7663 so we don't get into an infinite recursion loop taking the
7664 conversion out and then back in. */
7666 if ((CONVERT_EXPR_CODE_P (code)
7667 || code == NON_LVALUE_EXPR)
7668 && TREE_CODE (tem) == COND_EXPR
7669 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7670 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7671 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7672 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7673 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7674 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7675 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7676 && (INTEGRAL_TYPE_P
7677 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7678 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7679 || flag_syntax_only))
7680 tem = build1_loc (loc, code, type,
7681 build3 (COND_EXPR,
7682 TREE_TYPE (TREE_OPERAND
7683 (TREE_OPERAND (tem, 1), 0)),
7684 TREE_OPERAND (tem, 0),
7685 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7686 TREE_OPERAND (TREE_OPERAND (tem, 2),
7687 0)));
7688 return tem;
7692 switch (code)
7694 case NON_LVALUE_EXPR:
7695 if (!maybe_lvalue_p (op0))
7696 return fold_convert_loc (loc, type, op0);
7697 return NULL_TREE;
7699 CASE_CONVERT:
7700 case FLOAT_EXPR:
7701 case FIX_TRUNC_EXPR:
7702 if (COMPARISON_CLASS_P (op0))
7704 /* If we have (type) (a CMP b) and type is an integral type, return
7705 new expression involving the new type. Canonicalize
7706 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7707 non-integral type.
7708 Do not fold the result as that would not simplify further, also
7709 folding again results in recursions. */
7710 if (TREE_CODE (type) == BOOLEAN_TYPE)
7711 return build2_loc (loc, TREE_CODE (op0), type,
7712 TREE_OPERAND (op0, 0),
7713 TREE_OPERAND (op0, 1));
7714 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7715 && TREE_CODE (type) != VECTOR_TYPE)
7716 return build3_loc (loc, COND_EXPR, type, op0,
7717 constant_boolean_node (true, type),
7718 constant_boolean_node (false, type));
7721 /* Handle (T *)&A.B.C for A being of type T and B and C
7722 living at offset zero. This occurs frequently in
7723 C++ upcasting and then accessing the base. */
7724 if (TREE_CODE (op0) == ADDR_EXPR
7725 && POINTER_TYPE_P (type)
7726 && handled_component_p (TREE_OPERAND (op0, 0)))
7728 HOST_WIDE_INT bitsize, bitpos;
7729 tree offset;
7730 machine_mode mode;
7731 int unsignedp, reversep, volatilep;
7732 tree base
7733 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7734 &offset, &mode, &unsignedp, &reversep,
7735 &volatilep);
7736 /* If the reference was to a (constant) zero offset, we can use
7737 the address of the base if it has the same base type
7738 as the result type and the pointer type is unqualified. */
7739 if (! offset && bitpos == 0
7740 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7741 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7742 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7743 return fold_convert_loc (loc, type,
7744 build_fold_addr_expr_loc (loc, base));
7747 if (TREE_CODE (op0) == MODIFY_EXPR
7748 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7749 /* Detect assigning a bitfield. */
7750 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7751 && DECL_BIT_FIELD
7752 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7754 /* Don't leave an assignment inside a conversion
7755 unless assigning a bitfield. */
7756 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7757 /* First do the assignment, then return converted constant. */
7758 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7759 TREE_NO_WARNING (tem) = 1;
7760 TREE_USED (tem) = 1;
7761 return tem;
7764 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7765 constants (if x has signed type, the sign bit cannot be set
7766 in c). This folds extension into the BIT_AND_EXPR.
7767 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7768 very likely don't have maximal range for their precision and this
7769 transformation effectively doesn't preserve non-maximal ranges. */
7770 if (TREE_CODE (type) == INTEGER_TYPE
7771 && TREE_CODE (op0) == BIT_AND_EXPR
7772 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7774 tree and_expr = op0;
7775 tree and0 = TREE_OPERAND (and_expr, 0);
7776 tree and1 = TREE_OPERAND (and_expr, 1);
7777 int change = 0;
7779 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7780 || (TYPE_PRECISION (type)
7781 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7782 change = 1;
7783 else if (TYPE_PRECISION (TREE_TYPE (and1))
7784 <= HOST_BITS_PER_WIDE_INT
7785 && tree_fits_uhwi_p (and1))
7787 unsigned HOST_WIDE_INT cst;
7789 cst = tree_to_uhwi (and1);
7790 cst &= HOST_WIDE_INT_M1U
7791 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7792 change = (cst == 0);
7793 if (change
7794 && !flag_syntax_only
7795 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7796 == ZERO_EXTEND))
7798 tree uns = unsigned_type_for (TREE_TYPE (and0));
7799 and0 = fold_convert_loc (loc, uns, and0);
7800 and1 = fold_convert_loc (loc, uns, and1);
7803 if (change)
7805 tem = force_fit_type (type, wi::to_widest (and1), 0,
7806 TREE_OVERFLOW (and1));
7807 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7808 fold_convert_loc (loc, type, and0), tem);
7812 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7813 cast (T1)X will fold away. We assume that this happens when X itself
7814 is a cast. */
7815 if (POINTER_TYPE_P (type)
7816 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7817 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7819 tree arg00 = TREE_OPERAND (arg0, 0);
7820 tree arg01 = TREE_OPERAND (arg0, 1);
7822 return fold_build_pointer_plus_loc
7823 (loc, fold_convert_loc (loc, type, arg00), arg01);
7826 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7827 of the same precision, and X is an integer type not narrower than
7828 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7829 if (INTEGRAL_TYPE_P (type)
7830 && TREE_CODE (op0) == BIT_NOT_EXPR
7831 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7832 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7833 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7835 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7836 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7837 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7838 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7839 fold_convert_loc (loc, type, tem));
7842 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7843 type of X and Y (integer types only). */
7844 if (INTEGRAL_TYPE_P (type)
7845 && TREE_CODE (op0) == MULT_EXPR
7846 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7847 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7849 /* Be careful not to introduce new overflows. */
7850 tree mult_type;
7851 if (TYPE_OVERFLOW_WRAPS (type))
7852 mult_type = type;
7853 else
7854 mult_type = unsigned_type_for (type);
7856 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7858 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7859 fold_convert_loc (loc, mult_type,
7860 TREE_OPERAND (op0, 0)),
7861 fold_convert_loc (loc, mult_type,
7862 TREE_OPERAND (op0, 1)));
7863 return fold_convert_loc (loc, type, tem);
7867 return NULL_TREE;
7869 case VIEW_CONVERT_EXPR:
7870 if (TREE_CODE (op0) == MEM_REF)
7872 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7873 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7874 tem = fold_build2_loc (loc, MEM_REF, type,
7875 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7876 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7877 return tem;
7880 return NULL_TREE;
7882 case NEGATE_EXPR:
7883 tem = fold_negate_expr (loc, arg0);
7884 if (tem)
7885 return fold_convert_loc (loc, type, tem);
7886 return NULL_TREE;
7888 case ABS_EXPR:
7889 /* Convert fabs((double)float) into (double)fabsf(float). */
7890 if (TREE_CODE (arg0) == NOP_EXPR
7891 && TREE_CODE (type) == REAL_TYPE)
7893 tree targ0 = strip_float_extensions (arg0);
7894 if (targ0 != arg0)
7895 return fold_convert_loc (loc, type,
7896 fold_build1_loc (loc, ABS_EXPR,
7897 TREE_TYPE (targ0),
7898 targ0));
7900 return NULL_TREE;
7902 case BIT_NOT_EXPR:
7903 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7904 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7905 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7906 fold_convert_loc (loc, type,
7907 TREE_OPERAND (arg0, 0)))))
7908 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7909 fold_convert_loc (loc, type,
7910 TREE_OPERAND (arg0, 1)));
7911 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7912 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7913 fold_convert_loc (loc, type,
7914 TREE_OPERAND (arg0, 1)))))
7915 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7916 fold_convert_loc (loc, type,
7917 TREE_OPERAND (arg0, 0)), tem);
7919 return NULL_TREE;
7921 case TRUTH_NOT_EXPR:
7922 /* Note that the operand of this must be an int
7923 and its values must be 0 or 1.
7924 ("true" is a fixed value perhaps depending on the language,
7925 but we don't handle values other than 1 correctly yet.) */
7926 tem = fold_truth_not_expr (loc, arg0);
7927 if (!tem)
7928 return NULL_TREE;
7929 return fold_convert_loc (loc, type, tem);
7931 case INDIRECT_REF:
7932 /* Fold *&X to X if X is an lvalue. */
7933 if (TREE_CODE (op0) == ADDR_EXPR)
7935 tree op00 = TREE_OPERAND (op0, 0);
7936 if ((TREE_CODE (op00) == VAR_DECL
7937 || TREE_CODE (op00) == PARM_DECL
7938 || TREE_CODE (op00) == RESULT_DECL)
7939 && !TREE_READONLY (op00))
7940 return op00;
7942 return NULL_TREE;
7944 default:
7945 return NULL_TREE;
7946 } /* switch (code) */
7950 /* If the operation was a conversion do _not_ mark a resulting constant
7951 with TREE_OVERFLOW if the original constant was not. These conversions
7952 have implementation defined behavior and retaining the TREE_OVERFLOW
7953 flag here would confuse later passes such as VRP. */
7954 tree
7955 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7956 tree type, tree op0)
7958 tree res = fold_unary_loc (loc, code, type, op0);
7959 if (res
7960 && TREE_CODE (res) == INTEGER_CST
7961 && TREE_CODE (op0) == INTEGER_CST
7962 && CONVERT_EXPR_CODE_P (code))
7963 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7965 return res;
7968 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7969 operands OP0 and OP1. LOC is the location of the resulting expression.
7970 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7971 Return the folded expression if folding is successful. Otherwise,
7972 return NULL_TREE. */
7973 static tree
7974 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7975 tree arg0, tree arg1, tree op0, tree op1)
7977 tree tem;
7979 /* We only do these simplifications if we are optimizing. */
7980 if (!optimize)
7981 return NULL_TREE;
7983 /* Check for things like (A || B) && (A || C). We can convert this
7984 to A || (B && C). Note that either operator can be any of the four
7985 truth and/or operations and the transformation will still be
7986 valid. Also note that we only care about order for the
7987 ANDIF and ORIF operators. If B contains side effects, this
7988 might change the truth-value of A. */
7989 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7990 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7991 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7992 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7993 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7994 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7996 tree a00 = TREE_OPERAND (arg0, 0);
7997 tree a01 = TREE_OPERAND (arg0, 1);
7998 tree a10 = TREE_OPERAND (arg1, 0);
7999 tree a11 = TREE_OPERAND (arg1, 1);
8000 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8001 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8002 && (code == TRUTH_AND_EXPR
8003 || code == TRUTH_OR_EXPR));
8005 if (operand_equal_p (a00, a10, 0))
8006 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8007 fold_build2_loc (loc, code, type, a01, a11));
8008 else if (commutative && operand_equal_p (a00, a11, 0))
8009 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8010 fold_build2_loc (loc, code, type, a01, a10));
8011 else if (commutative && operand_equal_p (a01, a10, 0))
8012 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8013 fold_build2_loc (loc, code, type, a00, a11));
8015 /* This case if tricky because we must either have commutative
8016 operators or else A10 must not have side-effects. */
8018 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8019 && operand_equal_p (a01, a11, 0))
8020 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8021 fold_build2_loc (loc, code, type, a00, a10),
8022 a01);
8025 /* See if we can build a range comparison. */
8026 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8027 return tem;
8029 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8030 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8032 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8033 if (tem)
8034 return fold_build2_loc (loc, code, type, tem, arg1);
8037 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8038 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8040 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8041 if (tem)
8042 return fold_build2_loc (loc, code, type, arg0, tem);
8045 /* Check for the possibility of merging component references. If our
8046 lhs is another similar operation, try to merge its rhs with our
8047 rhs. Then try to merge our lhs and rhs. */
8048 if (TREE_CODE (arg0) == code
8049 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8050 TREE_OPERAND (arg0, 1), arg1)))
8051 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8053 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8054 return tem;
8056 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8057 && (code == TRUTH_AND_EXPR
8058 || code == TRUTH_ANDIF_EXPR
8059 || code == TRUTH_OR_EXPR
8060 || code == TRUTH_ORIF_EXPR))
8062 enum tree_code ncode, icode;
8064 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8065 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8066 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8068 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8069 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8070 We don't want to pack more than two leafs to a non-IF AND/OR
8071 expression.
8072 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8073 equal to IF-CODE, then we don't want to add right-hand operand.
8074 If the inner right-hand side of left-hand operand has
8075 side-effects, or isn't simple, then we can't add to it,
8076 as otherwise we might destroy if-sequence. */
8077 if (TREE_CODE (arg0) == icode
8078 && simple_operand_p_2 (arg1)
8079 /* Needed for sequence points to handle trappings, and
8080 side-effects. */
8081 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8083 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8084 arg1);
8085 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8086 tem);
8088 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8089 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8090 else if (TREE_CODE (arg1) == icode
8091 && simple_operand_p_2 (arg0)
8092 /* Needed for sequence points to handle trappings, and
8093 side-effects. */
8094 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8096 tem = fold_build2_loc (loc, ncode, type,
8097 arg0, TREE_OPERAND (arg1, 0));
8098 return fold_build2_loc (loc, icode, type, tem,
8099 TREE_OPERAND (arg1, 1));
8101 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8102 into (A OR B).
8103 For sequence point consistancy, we need to check for trapping,
8104 and side-effects. */
8105 else if (code == icode && simple_operand_p_2 (arg0)
8106 && simple_operand_p_2 (arg1))
8107 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8110 return NULL_TREE;
8113 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8114 by changing CODE to reduce the magnitude of constants involved in
8115 ARG0 of the comparison.
8116 Returns a canonicalized comparison tree if a simplification was
8117 possible, otherwise returns NULL_TREE.
8118 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8119 valid if signed overflow is undefined. */
8121 static tree
8122 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8123 tree arg0, tree arg1,
8124 bool *strict_overflow_p)
8126 enum tree_code code0 = TREE_CODE (arg0);
8127 tree t, cst0 = NULL_TREE;
8128 int sgn0;
8130 /* Match A +- CST code arg1. We can change this only if overflow
8131 is undefined. */
8132 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8133 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8134 /* In principle pointers also have undefined overflow behavior,
8135 but that causes problems elsewhere. */
8136 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8137 && (code0 == MINUS_EXPR
8138 || code0 == PLUS_EXPR)
8139 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8140 return NULL_TREE;
8142 /* Identify the constant in arg0 and its sign. */
8143 cst0 = TREE_OPERAND (arg0, 1);
8144 sgn0 = tree_int_cst_sgn (cst0);
8146 /* Overflowed constants and zero will cause problems. */
8147 if (integer_zerop (cst0)
8148 || TREE_OVERFLOW (cst0))
8149 return NULL_TREE;
8151 /* See if we can reduce the magnitude of the constant in
8152 arg0 by changing the comparison code. */
8153 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8154 if (code == LT_EXPR
8155 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8156 code = LE_EXPR;
8157 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8158 else if (code == GT_EXPR
8159 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8160 code = GE_EXPR;
8161 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8162 else if (code == LE_EXPR
8163 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8164 code = LT_EXPR;
8165 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8166 else if (code == GE_EXPR
8167 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8168 code = GT_EXPR;
8169 else
8170 return NULL_TREE;
8171 *strict_overflow_p = true;
8173 /* Now build the constant reduced in magnitude. But not if that
8174 would produce one outside of its types range. */
8175 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8176 && ((sgn0 == 1
8177 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8178 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8179 || (sgn0 == -1
8180 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8181 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8182 return NULL_TREE;
8184 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8185 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8186 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8187 t = fold_convert (TREE_TYPE (arg1), t);
8189 return fold_build2_loc (loc, code, type, t, arg1);
8192 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8193 overflow further. Try to decrease the magnitude of constants involved
8194 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8195 and put sole constants at the second argument position.
8196 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8198 static tree
8199 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8200 tree arg0, tree arg1)
8202 tree t;
8203 bool strict_overflow_p;
8204 const char * const warnmsg = G_("assuming signed overflow does not occur "
8205 "when reducing constant in comparison");
8207 /* Try canonicalization by simplifying arg0. */
8208 strict_overflow_p = false;
8209 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8210 &strict_overflow_p);
8211 if (t)
8213 if (strict_overflow_p)
8214 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8215 return t;
8218 /* Try canonicalization by simplifying arg1 using the swapped
8219 comparison. */
8220 code = swap_tree_comparison (code);
8221 strict_overflow_p = false;
8222 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8223 &strict_overflow_p);
8224 if (t && strict_overflow_p)
8225 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8226 return t;
8229 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8230 space. This is used to avoid issuing overflow warnings for
8231 expressions like &p->x which can not wrap. */
8233 static bool
8234 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8236 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8237 return true;
8239 if (bitpos < 0)
8240 return true;
8242 wide_int wi_offset;
8243 int precision = TYPE_PRECISION (TREE_TYPE (base));
8244 if (offset == NULL_TREE)
8245 wi_offset = wi::zero (precision);
8246 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8247 return true;
8248 else
8249 wi_offset = offset;
8251 bool overflow;
8252 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8253 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8254 if (overflow)
8255 return true;
8257 if (!wi::fits_uhwi_p (total))
8258 return true;
8260 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8261 if (size <= 0)
8262 return true;
8264 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8265 array. */
8266 if (TREE_CODE (base) == ADDR_EXPR)
8268 HOST_WIDE_INT base_size;
8270 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8271 if (base_size > 0 && size < base_size)
8272 size = base_size;
8275 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8278 /* Return a positive integer when the symbol DECL is known to have
8279 a nonzero address, zero when it's known not to (e.g., it's a weak
8280 symbol), and a negative integer when the symbol is not yet in the
8281 symbol table and so whether or not its address is zero is unknown. */
8282 static int
8283 maybe_nonzero_address (tree decl)
8285 if (DECL_P (decl) && decl_in_symtab_p (decl))
8286 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8287 return symbol->nonzero_address ();
8289 return -1;
8292 /* Subroutine of fold_binary. This routine performs all of the
8293 transformations that are common to the equality/inequality
8294 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8295 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8296 fold_binary should call fold_binary. Fold a comparison with
8297 tree code CODE and type TYPE with operands OP0 and OP1. Return
8298 the folded comparison or NULL_TREE. */
8300 static tree
8301 fold_comparison (location_t loc, enum tree_code code, tree type,
8302 tree op0, tree op1)
8304 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8305 tree arg0, arg1, tem;
8307 arg0 = op0;
8308 arg1 = op1;
8310 STRIP_SIGN_NOPS (arg0);
8311 STRIP_SIGN_NOPS (arg1);
8313 /* For comparisons of pointers we can decompose it to a compile time
8314 comparison of the base objects and the offsets into the object.
8315 This requires at least one operand being an ADDR_EXPR or a
8316 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8317 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8318 && (TREE_CODE (arg0) == ADDR_EXPR
8319 || TREE_CODE (arg1) == ADDR_EXPR
8320 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8321 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8323 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8324 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8325 machine_mode mode;
8326 int volatilep, reversep, unsignedp;
8327 bool indirect_base0 = false, indirect_base1 = false;
8329 /* Get base and offset for the access. Strip ADDR_EXPR for
8330 get_inner_reference, but put it back by stripping INDIRECT_REF
8331 off the base object if possible. indirect_baseN will be true
8332 if baseN is not an address but refers to the object itself. */
8333 base0 = arg0;
8334 if (TREE_CODE (arg0) == ADDR_EXPR)
8336 base0
8337 = get_inner_reference (TREE_OPERAND (arg0, 0),
8338 &bitsize, &bitpos0, &offset0, &mode,
8339 &unsignedp, &reversep, &volatilep);
8340 if (TREE_CODE (base0) == INDIRECT_REF)
8341 base0 = TREE_OPERAND (base0, 0);
8342 else
8343 indirect_base0 = true;
8345 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8347 base0 = TREE_OPERAND (arg0, 0);
8348 STRIP_SIGN_NOPS (base0);
8349 if (TREE_CODE (base0) == ADDR_EXPR)
8351 base0
8352 = get_inner_reference (TREE_OPERAND (base0, 0),
8353 &bitsize, &bitpos0, &offset0, &mode,
8354 &unsignedp, &reversep, &volatilep);
8355 if (TREE_CODE (base0) == INDIRECT_REF)
8356 base0 = TREE_OPERAND (base0, 0);
8357 else
8358 indirect_base0 = true;
8360 if (offset0 == NULL_TREE || integer_zerop (offset0))
8361 offset0 = TREE_OPERAND (arg0, 1);
8362 else
8363 offset0 = size_binop (PLUS_EXPR, offset0,
8364 TREE_OPERAND (arg0, 1));
8365 if (TREE_CODE (offset0) == INTEGER_CST)
8367 offset_int tem = wi::sext (wi::to_offset (offset0),
8368 TYPE_PRECISION (sizetype));
8369 tem <<= LOG2_BITS_PER_UNIT;
8370 tem += bitpos0;
8371 if (wi::fits_shwi_p (tem))
8373 bitpos0 = tem.to_shwi ();
8374 offset0 = NULL_TREE;
8379 base1 = arg1;
8380 if (TREE_CODE (arg1) == ADDR_EXPR)
8382 base1
8383 = get_inner_reference (TREE_OPERAND (arg1, 0),
8384 &bitsize, &bitpos1, &offset1, &mode,
8385 &unsignedp, &reversep, &volatilep);
8386 if (TREE_CODE (base1) == INDIRECT_REF)
8387 base1 = TREE_OPERAND (base1, 0);
8388 else
8389 indirect_base1 = true;
8391 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8393 base1 = TREE_OPERAND (arg1, 0);
8394 STRIP_SIGN_NOPS (base1);
8395 if (TREE_CODE (base1) == ADDR_EXPR)
8397 base1
8398 = get_inner_reference (TREE_OPERAND (base1, 0),
8399 &bitsize, &bitpos1, &offset1, &mode,
8400 &unsignedp, &reversep, &volatilep);
8401 if (TREE_CODE (base1) == INDIRECT_REF)
8402 base1 = TREE_OPERAND (base1, 0);
8403 else
8404 indirect_base1 = true;
8406 if (offset1 == NULL_TREE || integer_zerop (offset1))
8407 offset1 = TREE_OPERAND (arg1, 1);
8408 else
8409 offset1 = size_binop (PLUS_EXPR, offset1,
8410 TREE_OPERAND (arg1, 1));
8411 if (TREE_CODE (offset1) == INTEGER_CST)
8413 offset_int tem = wi::sext (wi::to_offset (offset1),
8414 TYPE_PRECISION (sizetype));
8415 tem <<= LOG2_BITS_PER_UNIT;
8416 tem += bitpos1;
8417 if (wi::fits_shwi_p (tem))
8419 bitpos1 = tem.to_shwi ();
8420 offset1 = NULL_TREE;
8425 /* If we have equivalent bases we might be able to simplify. */
8426 if (indirect_base0 == indirect_base1
8427 && operand_equal_p (base0, base1,
8428 indirect_base0 ? OEP_ADDRESS_OF : 0))
8430 /* We can fold this expression to a constant if the non-constant
8431 offset parts are equal. */
8432 if ((offset0 == offset1
8433 || (offset0 && offset1
8434 && operand_equal_p (offset0, offset1, 0)))
8435 && (equality_code
8436 || (indirect_base0
8437 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8438 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8441 if (!equality_code
8442 && bitpos0 != bitpos1
8443 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8444 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8445 fold_overflow_warning (("assuming pointer wraparound does not "
8446 "occur when comparing P +- C1 with "
8447 "P +- C2"),
8448 WARN_STRICT_OVERFLOW_CONDITIONAL);
8450 switch (code)
8452 case EQ_EXPR:
8453 return constant_boolean_node (bitpos0 == bitpos1, type);
8454 case NE_EXPR:
8455 return constant_boolean_node (bitpos0 != bitpos1, type);
8456 case LT_EXPR:
8457 return constant_boolean_node (bitpos0 < bitpos1, type);
8458 case LE_EXPR:
8459 return constant_boolean_node (bitpos0 <= bitpos1, type);
8460 case GE_EXPR:
8461 return constant_boolean_node (bitpos0 >= bitpos1, type);
8462 case GT_EXPR:
8463 return constant_boolean_node (bitpos0 > bitpos1, type);
8464 default:;
8467 /* We can simplify the comparison to a comparison of the variable
8468 offset parts if the constant offset parts are equal.
8469 Be careful to use signed sizetype here because otherwise we
8470 mess with array offsets in the wrong way. This is possible
8471 because pointer arithmetic is restricted to retain within an
8472 object and overflow on pointer differences is undefined as of
8473 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8474 else if (bitpos0 == bitpos1
8475 && (equality_code
8476 || (indirect_base0
8477 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8478 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8480 /* By converting to signed sizetype we cover middle-end pointer
8481 arithmetic which operates on unsigned pointer types of size
8482 type size and ARRAY_REF offsets which are properly sign or
8483 zero extended from their type in case it is narrower than
8484 sizetype. */
8485 if (offset0 == NULL_TREE)
8486 offset0 = build_int_cst (ssizetype, 0);
8487 else
8488 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8489 if (offset1 == NULL_TREE)
8490 offset1 = build_int_cst (ssizetype, 0);
8491 else
8492 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8494 if (!equality_code
8495 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8496 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8497 fold_overflow_warning (("assuming pointer wraparound does not "
8498 "occur when comparing P +- C1 with "
8499 "P +- C2"),
8500 WARN_STRICT_OVERFLOW_COMPARISON);
8502 return fold_build2_loc (loc, code, type, offset0, offset1);
8505 /* For equal offsets we can simplify to a comparison of the
8506 base addresses. */
8507 else if (bitpos0 == bitpos1
8508 && (indirect_base0
8509 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8510 && (indirect_base1
8511 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8512 && ((offset0 == offset1)
8513 || (offset0 && offset1
8514 && operand_equal_p (offset0, offset1, 0))))
8516 if (indirect_base0)
8517 base0 = build_fold_addr_expr_loc (loc, base0);
8518 if (indirect_base1)
8519 base1 = build_fold_addr_expr_loc (loc, base1);
8520 return fold_build2_loc (loc, code, type, base0, base1);
8522 /* Comparison between an ordinary (non-weak) symbol and a null
8523 pointer can be eliminated since such symbols must have a non
8524 null address. In C, relational expressions between pointers
8525 to objects and null pointers are undefined. The results
8526 below follow the C++ rules with the additional property that
8527 every object pointer compares greater than a null pointer.
8529 else if (DECL_P (base0)
8530 && maybe_nonzero_address (base0) > 0
8531 /* Avoid folding references to struct members at offset 0 to
8532 prevent tests like '&ptr->firstmember == 0' from getting
8533 eliminated. When ptr is null, although the -> expression
8534 is strictly speaking invalid, GCC retains it as a matter
8535 of QoI. See PR c/44555. */
8536 && (offset0 == NULL_TREE && bitpos0 != 0)
8537 /* The caller guarantees that when one of the arguments is
8538 constant (i.e., null in this case) it is second. */
8539 && integer_zerop (arg1))
8541 switch (code)
8543 case EQ_EXPR:
8544 case LE_EXPR:
8545 case LT_EXPR:
8546 return constant_boolean_node (false, type);
8547 case GE_EXPR:
8548 case GT_EXPR:
8549 case NE_EXPR:
8550 return constant_boolean_node (true, type);
8551 default:
8552 gcc_unreachable ();
8557 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8558 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8559 the resulting offset is smaller in absolute value than the
8560 original one and has the same sign. */
8561 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8562 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8563 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8564 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8565 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8566 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8567 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8568 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8570 tree const1 = TREE_OPERAND (arg0, 1);
8571 tree const2 = TREE_OPERAND (arg1, 1);
8572 tree variable1 = TREE_OPERAND (arg0, 0);
8573 tree variable2 = TREE_OPERAND (arg1, 0);
8574 tree cst;
8575 const char * const warnmsg = G_("assuming signed overflow does not "
8576 "occur when combining constants around "
8577 "a comparison");
8579 /* Put the constant on the side where it doesn't overflow and is
8580 of lower absolute value and of same sign than before. */
8581 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8582 ? MINUS_EXPR : PLUS_EXPR,
8583 const2, const1);
8584 if (!TREE_OVERFLOW (cst)
8585 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8586 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8588 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8589 return fold_build2_loc (loc, code, type,
8590 variable1,
8591 fold_build2_loc (loc, TREE_CODE (arg1),
8592 TREE_TYPE (arg1),
8593 variable2, cst));
8596 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8597 ? MINUS_EXPR : PLUS_EXPR,
8598 const1, const2);
8599 if (!TREE_OVERFLOW (cst)
8600 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8601 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8603 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8604 return fold_build2_loc (loc, code, type,
8605 fold_build2_loc (loc, TREE_CODE (arg0),
8606 TREE_TYPE (arg0),
8607 variable1, cst),
8608 variable2);
8612 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8613 if (tem)
8614 return tem;
8616 /* If we are comparing an expression that just has comparisons
8617 of two integer values, arithmetic expressions of those comparisons,
8618 and constants, we can simplify it. There are only three cases
8619 to check: the two values can either be equal, the first can be
8620 greater, or the second can be greater. Fold the expression for
8621 those three values. Since each value must be 0 or 1, we have
8622 eight possibilities, each of which corresponds to the constant 0
8623 or 1 or one of the six possible comparisons.
8625 This handles common cases like (a > b) == 0 but also handles
8626 expressions like ((x > y) - (y > x)) > 0, which supposedly
8627 occur in macroized code. */
8629 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8631 tree cval1 = 0, cval2 = 0;
8632 int save_p = 0;
8634 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8635 /* Don't handle degenerate cases here; they should already
8636 have been handled anyway. */
8637 && cval1 != 0 && cval2 != 0
8638 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8639 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8640 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8641 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8642 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8643 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8644 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8646 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8647 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8649 /* We can't just pass T to eval_subst in case cval1 or cval2
8650 was the same as ARG1. */
8652 tree high_result
8653 = fold_build2_loc (loc, code, type,
8654 eval_subst (loc, arg0, cval1, maxval,
8655 cval2, minval),
8656 arg1);
8657 tree equal_result
8658 = fold_build2_loc (loc, code, type,
8659 eval_subst (loc, arg0, cval1, maxval,
8660 cval2, maxval),
8661 arg1);
8662 tree low_result
8663 = fold_build2_loc (loc, code, type,
8664 eval_subst (loc, arg0, cval1, minval,
8665 cval2, maxval),
8666 arg1);
8668 /* All three of these results should be 0 or 1. Confirm they are.
8669 Then use those values to select the proper code to use. */
8671 if (TREE_CODE (high_result) == INTEGER_CST
8672 && TREE_CODE (equal_result) == INTEGER_CST
8673 && TREE_CODE (low_result) == INTEGER_CST)
8675 /* Make a 3-bit mask with the high-order bit being the
8676 value for `>', the next for '=', and the low for '<'. */
8677 switch ((integer_onep (high_result) * 4)
8678 + (integer_onep (equal_result) * 2)
8679 + integer_onep (low_result))
8681 case 0:
8682 /* Always false. */
8683 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8684 case 1:
8685 code = LT_EXPR;
8686 break;
8687 case 2:
8688 code = EQ_EXPR;
8689 break;
8690 case 3:
8691 code = LE_EXPR;
8692 break;
8693 case 4:
8694 code = GT_EXPR;
8695 break;
8696 case 5:
8697 code = NE_EXPR;
8698 break;
8699 case 6:
8700 code = GE_EXPR;
8701 break;
8702 case 7:
8703 /* Always true. */
8704 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8707 if (save_p)
8709 tem = save_expr (build2 (code, type, cval1, cval2));
8710 SET_EXPR_LOCATION (tem, loc);
8711 return tem;
8713 return fold_build2_loc (loc, code, type, cval1, cval2);
8718 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8719 into a single range test. */
8720 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8721 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8722 && TREE_CODE (arg1) == INTEGER_CST
8723 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8724 && !integer_zerop (TREE_OPERAND (arg0, 1))
8725 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8726 && !TREE_OVERFLOW (arg1))
8728 tem = fold_div_compare (loc, code, type, arg0, arg1);
8729 if (tem != NULL_TREE)
8730 return tem;
8733 return NULL_TREE;
8737 /* Subroutine of fold_binary. Optimize complex multiplications of the
8738 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8739 argument EXPR represents the expression "z" of type TYPE. */
8741 static tree
8742 fold_mult_zconjz (location_t loc, tree type, tree expr)
8744 tree itype = TREE_TYPE (type);
8745 tree rpart, ipart, tem;
8747 if (TREE_CODE (expr) == COMPLEX_EXPR)
8749 rpart = TREE_OPERAND (expr, 0);
8750 ipart = TREE_OPERAND (expr, 1);
8752 else if (TREE_CODE (expr) == COMPLEX_CST)
8754 rpart = TREE_REALPART (expr);
8755 ipart = TREE_IMAGPART (expr);
8757 else
8759 expr = save_expr (expr);
8760 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8761 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8764 rpart = save_expr (rpart);
8765 ipart = save_expr (ipart);
8766 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8767 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8768 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8769 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8770 build_zero_cst (itype));
8774 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8775 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8777 static bool
8778 vec_cst_ctor_to_array (tree arg, tree *elts)
8780 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8782 if (TREE_CODE (arg) == VECTOR_CST)
8784 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8785 elts[i] = VECTOR_CST_ELT (arg, i);
8787 else if (TREE_CODE (arg) == CONSTRUCTOR)
8789 constructor_elt *elt;
8791 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8792 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8793 return false;
8794 else
8795 elts[i] = elt->value;
8797 else
8798 return false;
8799 for (; i < nelts; i++)
8800 elts[i]
8801 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8802 return true;
8805 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8806 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8807 NULL_TREE otherwise. */
8809 static tree
8810 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8812 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8813 tree *elts;
8814 bool need_ctor = false;
8816 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8817 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8818 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8819 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8820 return NULL_TREE;
8822 elts = XALLOCAVEC (tree, nelts * 3);
8823 if (!vec_cst_ctor_to_array (arg0, elts)
8824 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8825 return NULL_TREE;
8827 for (i = 0; i < nelts; i++)
8829 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8830 need_ctor = true;
8831 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8834 if (need_ctor)
8836 vec<constructor_elt, va_gc> *v;
8837 vec_alloc (v, nelts);
8838 for (i = 0; i < nelts; i++)
8839 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8840 return build_constructor (type, v);
8842 else
8843 return build_vector (type, &elts[2 * nelts]);
8846 /* Try to fold a pointer difference of type TYPE two address expressions of
8847 array references AREF0 and AREF1 using location LOC. Return a
8848 simplified expression for the difference or NULL_TREE. */
8850 static tree
8851 fold_addr_of_array_ref_difference (location_t loc, tree type,
8852 tree aref0, tree aref1)
8854 tree base0 = TREE_OPERAND (aref0, 0);
8855 tree base1 = TREE_OPERAND (aref1, 0);
8856 tree base_offset = build_int_cst (type, 0);
8858 /* If the bases are array references as well, recurse. If the bases
8859 are pointer indirections compute the difference of the pointers.
8860 If the bases are equal, we are set. */
8861 if ((TREE_CODE (base0) == ARRAY_REF
8862 && TREE_CODE (base1) == ARRAY_REF
8863 && (base_offset
8864 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8865 || (INDIRECT_REF_P (base0)
8866 && INDIRECT_REF_P (base1)
8867 && (base_offset
8868 = fold_binary_loc (loc, MINUS_EXPR, type,
8869 fold_convert (type, TREE_OPERAND (base0, 0)),
8870 fold_convert (type,
8871 TREE_OPERAND (base1, 0)))))
8872 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8874 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8875 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8876 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8877 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8878 return fold_build2_loc (loc, PLUS_EXPR, type,
8879 base_offset,
8880 fold_build2_loc (loc, MULT_EXPR, type,
8881 diff, esz));
8883 return NULL_TREE;
8886 /* If the real or vector real constant CST of type TYPE has an exact
8887 inverse, return it, else return NULL. */
8889 tree
8890 exact_inverse (tree type, tree cst)
8892 REAL_VALUE_TYPE r;
8893 tree unit_type, *elts;
8894 machine_mode mode;
8895 unsigned vec_nelts, i;
8897 switch (TREE_CODE (cst))
8899 case REAL_CST:
8900 r = TREE_REAL_CST (cst);
8902 if (exact_real_inverse (TYPE_MODE (type), &r))
8903 return build_real (type, r);
8905 return NULL_TREE;
8907 case VECTOR_CST:
8908 vec_nelts = VECTOR_CST_NELTS (cst);
8909 elts = XALLOCAVEC (tree, vec_nelts);
8910 unit_type = TREE_TYPE (type);
8911 mode = TYPE_MODE (unit_type);
8913 for (i = 0; i < vec_nelts; i++)
8915 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8916 if (!exact_real_inverse (mode, &r))
8917 return NULL_TREE;
8918 elts[i] = build_real (unit_type, r);
8921 return build_vector (type, elts);
8923 default:
8924 return NULL_TREE;
8928 /* Mask out the tz least significant bits of X of type TYPE where
8929 tz is the number of trailing zeroes in Y. */
8930 static wide_int
8931 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8933 int tz = wi::ctz (y);
8934 if (tz > 0)
8935 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8936 return x;
8939 /* Return true when T is an address and is known to be nonzero.
8940 For floating point we further ensure that T is not denormal.
8941 Similar logic is present in nonzero_address in rtlanal.h.
8943 If the return value is based on the assumption that signed overflow
8944 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8945 change *STRICT_OVERFLOW_P. */
8947 static bool
8948 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8950 tree type = TREE_TYPE (t);
8951 enum tree_code code;
8953 /* Doing something useful for floating point would need more work. */
8954 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8955 return false;
8957 code = TREE_CODE (t);
8958 switch (TREE_CODE_CLASS (code))
8960 case tcc_unary:
8961 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8962 strict_overflow_p);
8963 case tcc_binary:
8964 case tcc_comparison:
8965 return tree_binary_nonzero_warnv_p (code, type,
8966 TREE_OPERAND (t, 0),
8967 TREE_OPERAND (t, 1),
8968 strict_overflow_p);
8969 case tcc_constant:
8970 case tcc_declaration:
8971 case tcc_reference:
8972 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8974 default:
8975 break;
8978 switch (code)
8980 case TRUTH_NOT_EXPR:
8981 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8982 strict_overflow_p);
8984 case TRUTH_AND_EXPR:
8985 case TRUTH_OR_EXPR:
8986 case TRUTH_XOR_EXPR:
8987 return tree_binary_nonzero_warnv_p (code, type,
8988 TREE_OPERAND (t, 0),
8989 TREE_OPERAND (t, 1),
8990 strict_overflow_p);
8992 case COND_EXPR:
8993 case CONSTRUCTOR:
8994 case OBJ_TYPE_REF:
8995 case ASSERT_EXPR:
8996 case ADDR_EXPR:
8997 case WITH_SIZE_EXPR:
8998 case SSA_NAME:
8999 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9001 case COMPOUND_EXPR:
9002 case MODIFY_EXPR:
9003 case BIND_EXPR:
9004 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9005 strict_overflow_p);
9007 case SAVE_EXPR:
9008 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9009 strict_overflow_p);
9011 case CALL_EXPR:
9013 tree fndecl = get_callee_fndecl (t);
9014 if (!fndecl) return false;
9015 if (flag_delete_null_pointer_checks && !flag_check_new
9016 && DECL_IS_OPERATOR_NEW (fndecl)
9017 && !TREE_NOTHROW (fndecl))
9018 return true;
9019 if (flag_delete_null_pointer_checks
9020 && lookup_attribute ("returns_nonnull",
9021 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9022 return true;
9023 return alloca_call_p (t);
9026 default:
9027 break;
9029 return false;
9032 /* Return true when T is an address and is known to be nonzero.
9033 Handle warnings about undefined signed overflow. */
9035 static bool
9036 tree_expr_nonzero_p (tree t)
9038 bool ret, strict_overflow_p;
9040 strict_overflow_p = false;
9041 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9042 if (strict_overflow_p)
9043 fold_overflow_warning (("assuming signed overflow does not occur when "
9044 "determining that expression is always "
9045 "non-zero"),
9046 WARN_STRICT_OVERFLOW_MISC);
9047 return ret;
9050 /* Return true if T is known not to be equal to an integer W. */
9052 bool
9053 expr_not_equal_to (tree t, const wide_int &w)
9055 wide_int min, max, nz;
9056 value_range_type rtype;
9057 switch (TREE_CODE (t))
9059 case INTEGER_CST:
9060 return wi::ne_p (t, w);
9062 case SSA_NAME:
9063 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9064 return false;
9065 rtype = get_range_info (t, &min, &max);
9066 if (rtype == VR_RANGE)
9068 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9069 return true;
9070 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9071 return true;
9073 else if (rtype == VR_ANTI_RANGE
9074 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9075 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9076 return true;
9077 /* If T has some known zero bits and W has any of those bits set,
9078 then T is known not to be equal to W. */
9079 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9080 TYPE_PRECISION (TREE_TYPE (t))), 0))
9081 return true;
9082 return false;
9084 default:
9085 return false;
9089 /* Fold a binary expression of code CODE and type TYPE with operands
9090 OP0 and OP1. LOC is the location of the resulting expression.
9091 Return the folded expression if folding is successful. Otherwise,
9092 return NULL_TREE. */
9094 tree
9095 fold_binary_loc (location_t loc,
9096 enum tree_code code, tree type, tree op0, tree op1)
9098 enum tree_code_class kind = TREE_CODE_CLASS (code);
9099 tree arg0, arg1, tem;
9100 tree t1 = NULL_TREE;
9101 bool strict_overflow_p;
9102 unsigned int prec;
9104 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9105 && TREE_CODE_LENGTH (code) == 2
9106 && op0 != NULL_TREE
9107 && op1 != NULL_TREE);
9109 arg0 = op0;
9110 arg1 = op1;
9112 /* Strip any conversions that don't change the mode. This is
9113 safe for every expression, except for a comparison expression
9114 because its signedness is derived from its operands. So, in
9115 the latter case, only strip conversions that don't change the
9116 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9117 preserved.
9119 Note that this is done as an internal manipulation within the
9120 constant folder, in order to find the simplest representation
9121 of the arguments so that their form can be studied. In any
9122 cases, the appropriate type conversions should be put back in
9123 the tree that will get out of the constant folder. */
9125 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9127 STRIP_SIGN_NOPS (arg0);
9128 STRIP_SIGN_NOPS (arg1);
9130 else
9132 STRIP_NOPS (arg0);
9133 STRIP_NOPS (arg1);
9136 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9137 constant but we can't do arithmetic on them. */
9138 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9140 tem = const_binop (code, type, arg0, arg1);
9141 if (tem != NULL_TREE)
9143 if (TREE_TYPE (tem) != type)
9144 tem = fold_convert_loc (loc, type, tem);
9145 return tem;
9149 /* If this is a commutative operation, and ARG0 is a constant, move it
9150 to ARG1 to reduce the number of tests below. */
9151 if (commutative_tree_code (code)
9152 && tree_swap_operands_p (arg0, arg1, true))
9153 return fold_build2_loc (loc, code, type, op1, op0);
9155 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9156 to ARG1 to reduce the number of tests below. */
9157 if (kind == tcc_comparison
9158 && tree_swap_operands_p (arg0, arg1, true))
9159 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9161 tem = generic_simplify (loc, code, type, op0, op1);
9162 if (tem)
9163 return tem;
9165 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9167 First check for cases where an arithmetic operation is applied to a
9168 compound, conditional, or comparison operation. Push the arithmetic
9169 operation inside the compound or conditional to see if any folding
9170 can then be done. Convert comparison to conditional for this purpose.
9171 The also optimizes non-constant cases that used to be done in
9172 expand_expr.
9174 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9175 one of the operands is a comparison and the other is a comparison, a
9176 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9177 code below would make the expression more complex. Change it to a
9178 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9179 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9181 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9182 || code == EQ_EXPR || code == NE_EXPR)
9183 && TREE_CODE (type) != VECTOR_TYPE
9184 && ((truth_value_p (TREE_CODE (arg0))
9185 && (truth_value_p (TREE_CODE (arg1))
9186 || (TREE_CODE (arg1) == BIT_AND_EXPR
9187 && integer_onep (TREE_OPERAND (arg1, 1)))))
9188 || (truth_value_p (TREE_CODE (arg1))
9189 && (truth_value_p (TREE_CODE (arg0))
9190 || (TREE_CODE (arg0) == BIT_AND_EXPR
9191 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9193 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9194 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9195 : TRUTH_XOR_EXPR,
9196 boolean_type_node,
9197 fold_convert_loc (loc, boolean_type_node, arg0),
9198 fold_convert_loc (loc, boolean_type_node, arg1));
9200 if (code == EQ_EXPR)
9201 tem = invert_truthvalue_loc (loc, tem);
9203 return fold_convert_loc (loc, type, tem);
9206 if (TREE_CODE_CLASS (code) == tcc_binary
9207 || TREE_CODE_CLASS (code) == tcc_comparison)
9209 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9211 tem = fold_build2_loc (loc, code, type,
9212 fold_convert_loc (loc, TREE_TYPE (op0),
9213 TREE_OPERAND (arg0, 1)), op1);
9214 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9215 tem);
9217 if (TREE_CODE (arg1) == COMPOUND_EXPR
9218 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9220 tem = fold_build2_loc (loc, code, type, op0,
9221 fold_convert_loc (loc, TREE_TYPE (op1),
9222 TREE_OPERAND (arg1, 1)));
9223 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9224 tem);
9227 if (TREE_CODE (arg0) == COND_EXPR
9228 || TREE_CODE (arg0) == VEC_COND_EXPR
9229 || COMPARISON_CLASS_P (arg0))
9231 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9232 arg0, arg1,
9233 /*cond_first_p=*/1);
9234 if (tem != NULL_TREE)
9235 return tem;
9238 if (TREE_CODE (arg1) == COND_EXPR
9239 || TREE_CODE (arg1) == VEC_COND_EXPR
9240 || COMPARISON_CLASS_P (arg1))
9242 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9243 arg1, arg0,
9244 /*cond_first_p=*/0);
9245 if (tem != NULL_TREE)
9246 return tem;
9250 switch (code)
9252 case MEM_REF:
9253 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9254 if (TREE_CODE (arg0) == ADDR_EXPR
9255 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9257 tree iref = TREE_OPERAND (arg0, 0);
9258 return fold_build2 (MEM_REF, type,
9259 TREE_OPERAND (iref, 0),
9260 int_const_binop (PLUS_EXPR, arg1,
9261 TREE_OPERAND (iref, 1)));
9264 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9265 if (TREE_CODE (arg0) == ADDR_EXPR
9266 && handled_component_p (TREE_OPERAND (arg0, 0)))
9268 tree base;
9269 HOST_WIDE_INT coffset;
9270 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9271 &coffset);
9272 if (!base)
9273 return NULL_TREE;
9274 return fold_build2 (MEM_REF, type,
9275 build_fold_addr_expr (base),
9276 int_const_binop (PLUS_EXPR, arg1,
9277 size_int (coffset)));
9280 return NULL_TREE;
9282 case POINTER_PLUS_EXPR:
9283 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9284 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9285 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9286 return fold_convert_loc (loc, type,
9287 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9288 fold_convert_loc (loc, sizetype,
9289 arg1),
9290 fold_convert_loc (loc, sizetype,
9291 arg0)));
9293 return NULL_TREE;
9295 case PLUS_EXPR:
9296 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9298 /* X + (X / CST) * -CST is X % CST. */
9299 if (TREE_CODE (arg1) == MULT_EXPR
9300 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9301 && operand_equal_p (arg0,
9302 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9304 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9305 tree cst1 = TREE_OPERAND (arg1, 1);
9306 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9307 cst1, cst0);
9308 if (sum && integer_zerop (sum))
9309 return fold_convert_loc (loc, type,
9310 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9311 TREE_TYPE (arg0), arg0,
9312 cst0));
9316 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9317 one. Make sure the type is not saturating and has the signedness of
9318 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9319 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9320 if ((TREE_CODE (arg0) == MULT_EXPR
9321 || TREE_CODE (arg1) == MULT_EXPR)
9322 && !TYPE_SATURATING (type)
9323 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9324 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9325 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9327 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9328 if (tem)
9329 return tem;
9332 if (! FLOAT_TYPE_P (type))
9334 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9335 (plus (plus (mult) (mult)) (foo)) so that we can
9336 take advantage of the factoring cases below. */
9337 if (ANY_INTEGRAL_TYPE_P (type)
9338 && TYPE_OVERFLOW_WRAPS (type)
9339 && (((TREE_CODE (arg0) == PLUS_EXPR
9340 || TREE_CODE (arg0) == MINUS_EXPR)
9341 && TREE_CODE (arg1) == MULT_EXPR)
9342 || ((TREE_CODE (arg1) == PLUS_EXPR
9343 || TREE_CODE (arg1) == MINUS_EXPR)
9344 && TREE_CODE (arg0) == MULT_EXPR)))
9346 tree parg0, parg1, parg, marg;
9347 enum tree_code pcode;
9349 if (TREE_CODE (arg1) == MULT_EXPR)
9350 parg = arg0, marg = arg1;
9351 else
9352 parg = arg1, marg = arg0;
9353 pcode = TREE_CODE (parg);
9354 parg0 = TREE_OPERAND (parg, 0);
9355 parg1 = TREE_OPERAND (parg, 1);
9356 STRIP_NOPS (parg0);
9357 STRIP_NOPS (parg1);
9359 if (TREE_CODE (parg0) == MULT_EXPR
9360 && TREE_CODE (parg1) != MULT_EXPR)
9361 return fold_build2_loc (loc, pcode, type,
9362 fold_build2_loc (loc, PLUS_EXPR, type,
9363 fold_convert_loc (loc, type,
9364 parg0),
9365 fold_convert_loc (loc, type,
9366 marg)),
9367 fold_convert_loc (loc, type, parg1));
9368 if (TREE_CODE (parg0) != MULT_EXPR
9369 && TREE_CODE (parg1) == MULT_EXPR)
9370 return
9371 fold_build2_loc (loc, PLUS_EXPR, type,
9372 fold_convert_loc (loc, type, parg0),
9373 fold_build2_loc (loc, pcode, type,
9374 fold_convert_loc (loc, type, marg),
9375 fold_convert_loc (loc, type,
9376 parg1)));
9379 else
9381 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9382 to __complex__ ( x, y ). This is not the same for SNaNs or
9383 if signed zeros are involved. */
9384 if (!HONOR_SNANS (element_mode (arg0))
9385 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9386 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9388 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9389 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9390 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9391 bool arg0rz = false, arg0iz = false;
9392 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9393 || (arg0i && (arg0iz = real_zerop (arg0i))))
9395 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9396 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9397 if (arg0rz && arg1i && real_zerop (arg1i))
9399 tree rp = arg1r ? arg1r
9400 : build1 (REALPART_EXPR, rtype, arg1);
9401 tree ip = arg0i ? arg0i
9402 : build1 (IMAGPART_EXPR, rtype, arg0);
9403 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9405 else if (arg0iz && arg1r && real_zerop (arg1r))
9407 tree rp = arg0r ? arg0r
9408 : build1 (REALPART_EXPR, rtype, arg0);
9409 tree ip = arg1i ? arg1i
9410 : build1 (IMAGPART_EXPR, rtype, arg1);
9411 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9416 if (flag_unsafe_math_optimizations
9417 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9418 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9419 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9420 return tem;
9422 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9423 We associate floats only if the user has specified
9424 -fassociative-math. */
9425 if (flag_associative_math
9426 && TREE_CODE (arg1) == PLUS_EXPR
9427 && TREE_CODE (arg0) != MULT_EXPR)
9429 tree tree10 = TREE_OPERAND (arg1, 0);
9430 tree tree11 = TREE_OPERAND (arg1, 1);
9431 if (TREE_CODE (tree11) == MULT_EXPR
9432 && TREE_CODE (tree10) == MULT_EXPR)
9434 tree tree0;
9435 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9436 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9439 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9440 We associate floats only if the user has specified
9441 -fassociative-math. */
9442 if (flag_associative_math
9443 && TREE_CODE (arg0) == PLUS_EXPR
9444 && TREE_CODE (arg1) != MULT_EXPR)
9446 tree tree00 = TREE_OPERAND (arg0, 0);
9447 tree tree01 = TREE_OPERAND (arg0, 1);
9448 if (TREE_CODE (tree01) == MULT_EXPR
9449 && TREE_CODE (tree00) == MULT_EXPR)
9451 tree tree0;
9452 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9453 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9458 bit_rotate:
9459 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9460 is a rotate of A by C1 bits. */
9461 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9462 is a rotate of A by B bits. */
9464 enum tree_code code0, code1;
9465 tree rtype;
9466 code0 = TREE_CODE (arg0);
9467 code1 = TREE_CODE (arg1);
9468 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9469 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9470 && operand_equal_p (TREE_OPERAND (arg0, 0),
9471 TREE_OPERAND (arg1, 0), 0)
9472 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9473 TYPE_UNSIGNED (rtype))
9474 /* Only create rotates in complete modes. Other cases are not
9475 expanded properly. */
9476 && (element_precision (rtype)
9477 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9479 tree tree01, tree11;
9480 enum tree_code code01, code11;
9482 tree01 = TREE_OPERAND (arg0, 1);
9483 tree11 = TREE_OPERAND (arg1, 1);
9484 STRIP_NOPS (tree01);
9485 STRIP_NOPS (tree11);
9486 code01 = TREE_CODE (tree01);
9487 code11 = TREE_CODE (tree11);
9488 if (code01 == INTEGER_CST
9489 && code11 == INTEGER_CST
9490 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9491 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9493 tem = build2_loc (loc, LROTATE_EXPR,
9494 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9495 TREE_OPERAND (arg0, 0),
9496 code0 == LSHIFT_EXPR
9497 ? TREE_OPERAND (arg0, 1)
9498 : TREE_OPERAND (arg1, 1));
9499 return fold_convert_loc (loc, type, tem);
9501 else if (code11 == MINUS_EXPR)
9503 tree tree110, tree111;
9504 tree110 = TREE_OPERAND (tree11, 0);
9505 tree111 = TREE_OPERAND (tree11, 1);
9506 STRIP_NOPS (tree110);
9507 STRIP_NOPS (tree111);
9508 if (TREE_CODE (tree110) == INTEGER_CST
9509 && 0 == compare_tree_int (tree110,
9510 element_precision
9511 (TREE_TYPE (TREE_OPERAND
9512 (arg0, 0))))
9513 && operand_equal_p (tree01, tree111, 0))
9514 return
9515 fold_convert_loc (loc, type,
9516 build2 ((code0 == LSHIFT_EXPR
9517 ? LROTATE_EXPR
9518 : RROTATE_EXPR),
9519 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9520 TREE_OPERAND (arg0, 0),
9521 TREE_OPERAND (arg0, 1)));
9523 else if (code01 == MINUS_EXPR)
9525 tree tree010, tree011;
9526 tree010 = TREE_OPERAND (tree01, 0);
9527 tree011 = TREE_OPERAND (tree01, 1);
9528 STRIP_NOPS (tree010);
9529 STRIP_NOPS (tree011);
9530 if (TREE_CODE (tree010) == INTEGER_CST
9531 && 0 == compare_tree_int (tree010,
9532 element_precision
9533 (TREE_TYPE (TREE_OPERAND
9534 (arg0, 0))))
9535 && operand_equal_p (tree11, tree011, 0))
9536 return fold_convert_loc
9537 (loc, type,
9538 build2 ((code0 != LSHIFT_EXPR
9539 ? LROTATE_EXPR
9540 : RROTATE_EXPR),
9541 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9542 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9547 associate:
9548 /* In most languages, can't associate operations on floats through
9549 parentheses. Rather than remember where the parentheses were, we
9550 don't associate floats at all, unless the user has specified
9551 -fassociative-math.
9552 And, we need to make sure type is not saturating. */
9554 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9555 && !TYPE_SATURATING (type))
9557 tree var0, con0, lit0, minus_lit0;
9558 tree var1, con1, lit1, minus_lit1;
9559 tree atype = type;
9560 bool ok = true;
9562 /* Split both trees into variables, constants, and literals. Then
9563 associate each group together, the constants with literals,
9564 then the result with variables. This increases the chances of
9565 literals being recombined later and of generating relocatable
9566 expressions for the sum of a constant and literal. */
9567 var0 = split_tree (loc, arg0, type, code,
9568 &con0, &lit0, &minus_lit0, 0);
9569 var1 = split_tree (loc, arg1, type, code,
9570 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9572 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9573 if (code == MINUS_EXPR)
9574 code = PLUS_EXPR;
9576 /* With undefined overflow prefer doing association in a type
9577 which wraps on overflow, if that is one of the operand types. */
9578 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9579 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9581 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9582 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9583 atype = TREE_TYPE (arg0);
9584 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9585 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9586 atype = TREE_TYPE (arg1);
9587 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9590 /* With undefined overflow we can only associate constants with one
9591 variable, and constants whose association doesn't overflow. */
9592 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9593 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9595 if (var0 && var1)
9597 tree tmp0 = var0;
9598 tree tmp1 = var1;
9599 bool one_neg = false;
9601 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9603 tmp0 = TREE_OPERAND (tmp0, 0);
9604 one_neg = !one_neg;
9606 if (CONVERT_EXPR_P (tmp0)
9607 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9608 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9609 <= TYPE_PRECISION (atype)))
9610 tmp0 = TREE_OPERAND (tmp0, 0);
9611 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9613 tmp1 = TREE_OPERAND (tmp1, 0);
9614 one_neg = !one_neg;
9616 if (CONVERT_EXPR_P (tmp1)
9617 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9618 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9619 <= TYPE_PRECISION (atype)))
9620 tmp1 = TREE_OPERAND (tmp1, 0);
9621 /* The only case we can still associate with two variables
9622 is if they cancel out. */
9623 if (!one_neg
9624 || !operand_equal_p (tmp0, tmp1, 0))
9625 ok = false;
9629 /* Only do something if we found more than two objects. Otherwise,
9630 nothing has changed and we risk infinite recursion. */
9631 if (ok
9632 && (2 < ((var0 != 0) + (var1 != 0)
9633 + (con0 != 0) + (con1 != 0)
9634 + (lit0 != 0) + (lit1 != 0)
9635 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9637 bool any_overflows = false;
9638 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9639 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9640 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9641 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9642 var0 = associate_trees (loc, var0, var1, code, atype);
9643 con0 = associate_trees (loc, con0, con1, code, atype);
9644 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9645 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9646 code, atype);
9648 /* Preserve the MINUS_EXPR if the negative part of the literal is
9649 greater than the positive part. Otherwise, the multiplicative
9650 folding code (i.e extract_muldiv) may be fooled in case
9651 unsigned constants are subtracted, like in the following
9652 example: ((X*2 + 4) - 8U)/2. */
9653 if (minus_lit0 && lit0)
9655 if (TREE_CODE (lit0) == INTEGER_CST
9656 && TREE_CODE (minus_lit0) == INTEGER_CST
9657 && tree_int_cst_lt (lit0, minus_lit0))
9659 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9660 MINUS_EXPR, atype);
9661 lit0 = 0;
9663 else
9665 lit0 = associate_trees (loc, lit0, minus_lit0,
9666 MINUS_EXPR, atype);
9667 minus_lit0 = 0;
9671 /* Don't introduce overflows through reassociation. */
9672 if (!any_overflows
9673 && ((lit0 && TREE_OVERFLOW_P (lit0))
9674 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9675 return NULL_TREE;
9677 if (minus_lit0)
9679 if (con0 == 0)
9680 return
9681 fold_convert_loc (loc, type,
9682 associate_trees (loc, var0, minus_lit0,
9683 MINUS_EXPR, atype));
9684 else
9686 con0 = associate_trees (loc, con0, minus_lit0,
9687 MINUS_EXPR, atype);
9688 return
9689 fold_convert_loc (loc, type,
9690 associate_trees (loc, var0, con0,
9691 PLUS_EXPR, atype));
9695 con0 = associate_trees (loc, con0, lit0, code, atype);
9696 return
9697 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9698 code, atype));
9702 return NULL_TREE;
9704 case MINUS_EXPR:
9705 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9706 if (TREE_CODE (arg0) == NEGATE_EXPR
9707 && negate_expr_p (op1)
9708 && reorder_operands_p (arg0, arg1))
9709 return fold_build2_loc (loc, MINUS_EXPR, type,
9710 negate_expr (op1),
9711 fold_convert_loc (loc, type,
9712 TREE_OPERAND (arg0, 0)));
9714 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9715 __complex__ ( x, -y ). This is not the same for SNaNs or if
9716 signed zeros are involved. */
9717 if (!HONOR_SNANS (element_mode (arg0))
9718 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9719 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9721 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9722 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9723 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9724 bool arg0rz = false, arg0iz = false;
9725 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9726 || (arg0i && (arg0iz = real_zerop (arg0i))))
9728 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9729 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9730 if (arg0rz && arg1i && real_zerop (arg1i))
9732 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9733 arg1r ? arg1r
9734 : build1 (REALPART_EXPR, rtype, arg1));
9735 tree ip = arg0i ? arg0i
9736 : build1 (IMAGPART_EXPR, rtype, arg0);
9737 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9739 else if (arg0iz && arg1r && real_zerop (arg1r))
9741 tree rp = arg0r ? arg0r
9742 : build1 (REALPART_EXPR, rtype, arg0);
9743 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9744 arg1i ? arg1i
9745 : build1 (IMAGPART_EXPR, rtype, arg1));
9746 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9751 /* A - B -> A + (-B) if B is easily negatable. */
9752 if (negate_expr_p (op1)
9753 && ! TYPE_OVERFLOW_SANITIZED (type)
9754 && ((FLOAT_TYPE_P (type)
9755 /* Avoid this transformation if B is a positive REAL_CST. */
9756 && (TREE_CODE (op1) != REAL_CST
9757 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9758 || INTEGRAL_TYPE_P (type)))
9759 return fold_build2_loc (loc, PLUS_EXPR, type,
9760 fold_convert_loc (loc, type, arg0),
9761 negate_expr (op1));
9763 /* Fold &a[i] - &a[j] to i-j. */
9764 if (TREE_CODE (arg0) == ADDR_EXPR
9765 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9766 && TREE_CODE (arg1) == ADDR_EXPR
9767 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9769 tree tem = fold_addr_of_array_ref_difference (loc, type,
9770 TREE_OPERAND (arg0, 0),
9771 TREE_OPERAND (arg1, 0));
9772 if (tem)
9773 return tem;
9776 if (FLOAT_TYPE_P (type)
9777 && flag_unsafe_math_optimizations
9778 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9779 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9780 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9781 return tem;
9783 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9784 one. Make sure the type is not saturating and has the signedness of
9785 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9786 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9787 if ((TREE_CODE (arg0) == MULT_EXPR
9788 || TREE_CODE (arg1) == MULT_EXPR)
9789 && !TYPE_SATURATING (type)
9790 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9791 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9792 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9794 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9795 if (tem)
9796 return tem;
9799 goto associate;
9801 case MULT_EXPR:
9802 if (! FLOAT_TYPE_P (type))
9804 /* Transform x * -C into -x * C if x is easily negatable. */
9805 if (TREE_CODE (op1) == INTEGER_CST
9806 && tree_int_cst_sgn (op1) == -1
9807 && negate_expr_p (op0)
9808 && (tem = negate_expr (op1)) != op1
9809 && ! TREE_OVERFLOW (tem))
9810 return fold_build2_loc (loc, MULT_EXPR, type,
9811 fold_convert_loc (loc, type,
9812 negate_expr (op0)), tem);
9814 strict_overflow_p = false;
9815 if (TREE_CODE (arg1) == INTEGER_CST
9816 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9817 &strict_overflow_p)))
9819 if (strict_overflow_p)
9820 fold_overflow_warning (("assuming signed overflow does not "
9821 "occur when simplifying "
9822 "multiplication"),
9823 WARN_STRICT_OVERFLOW_MISC);
9824 return fold_convert_loc (loc, type, tem);
9827 /* Optimize z * conj(z) for integer complex numbers. */
9828 if (TREE_CODE (arg0) == CONJ_EXPR
9829 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9830 return fold_mult_zconjz (loc, type, arg1);
9831 if (TREE_CODE (arg1) == CONJ_EXPR
9832 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9833 return fold_mult_zconjz (loc, type, arg0);
9835 else
9837 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9838 This is not the same for NaNs or if signed zeros are
9839 involved. */
9840 if (!HONOR_NANS (arg0)
9841 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9842 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9843 && TREE_CODE (arg1) == COMPLEX_CST
9844 && real_zerop (TREE_REALPART (arg1)))
9846 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9847 if (real_onep (TREE_IMAGPART (arg1)))
9848 return
9849 fold_build2_loc (loc, COMPLEX_EXPR, type,
9850 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9851 rtype, arg0)),
9852 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9853 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9854 return
9855 fold_build2_loc (loc, COMPLEX_EXPR, type,
9856 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9857 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9858 rtype, arg0)));
9861 /* Optimize z * conj(z) for floating point complex numbers.
9862 Guarded by flag_unsafe_math_optimizations as non-finite
9863 imaginary components don't produce scalar results. */
9864 if (flag_unsafe_math_optimizations
9865 && TREE_CODE (arg0) == CONJ_EXPR
9866 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9867 return fold_mult_zconjz (loc, type, arg1);
9868 if (flag_unsafe_math_optimizations
9869 && TREE_CODE (arg1) == CONJ_EXPR
9870 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9871 return fold_mult_zconjz (loc, type, arg0);
9873 goto associate;
9875 case BIT_IOR_EXPR:
9876 /* Canonicalize (X & C1) | C2. */
9877 if (TREE_CODE (arg0) == BIT_AND_EXPR
9878 && TREE_CODE (arg1) == INTEGER_CST
9879 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9881 int width = TYPE_PRECISION (type), w;
9882 wide_int c1 = TREE_OPERAND (arg0, 1);
9883 wide_int c2 = arg1;
9885 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9886 if ((c1 & c2) == c1)
9887 return omit_one_operand_loc (loc, type, arg1,
9888 TREE_OPERAND (arg0, 0));
9890 wide_int msk = wi::mask (width, false,
9891 TYPE_PRECISION (TREE_TYPE (arg1)));
9893 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9894 if (msk.and_not (c1 | c2) == 0)
9895 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9896 TREE_OPERAND (arg0, 0), arg1);
9898 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9899 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9900 mode which allows further optimizations. */
9901 c1 &= msk;
9902 c2 &= msk;
9903 wide_int c3 = c1.and_not (c2);
9904 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9906 wide_int mask = wi::mask (w, false,
9907 TYPE_PRECISION (type));
9908 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9910 c3 = mask;
9911 break;
9915 if (c3 != c1)
9916 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9917 fold_build2_loc (loc, BIT_AND_EXPR, type,
9918 TREE_OPERAND (arg0, 0),
9919 wide_int_to_tree (type,
9920 c3)),
9921 arg1);
9924 /* See if this can be simplified into a rotate first. If that
9925 is unsuccessful continue in the association code. */
9926 goto bit_rotate;
9928 case BIT_XOR_EXPR:
9929 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9930 if (TREE_CODE (arg0) == BIT_AND_EXPR
9931 && INTEGRAL_TYPE_P (type)
9932 && integer_onep (TREE_OPERAND (arg0, 1))
9933 && integer_onep (arg1))
9934 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9935 build_zero_cst (TREE_TYPE (arg0)));
9937 /* See if this can be simplified into a rotate first. If that
9938 is unsuccessful continue in the association code. */
9939 goto bit_rotate;
9941 case BIT_AND_EXPR:
9942 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9943 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9944 && INTEGRAL_TYPE_P (type)
9945 && integer_onep (TREE_OPERAND (arg0, 1))
9946 && integer_onep (arg1))
9948 tree tem2;
9949 tem = TREE_OPERAND (arg0, 0);
9950 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9951 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9952 tem, tem2);
9953 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9954 build_zero_cst (TREE_TYPE (tem)));
9956 /* Fold ~X & 1 as (X & 1) == 0. */
9957 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9958 && INTEGRAL_TYPE_P (type)
9959 && integer_onep (arg1))
9961 tree tem2;
9962 tem = TREE_OPERAND (arg0, 0);
9963 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9964 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9965 tem, tem2);
9966 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9967 build_zero_cst (TREE_TYPE (tem)));
9969 /* Fold !X & 1 as X == 0. */
9970 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9971 && integer_onep (arg1))
9973 tem = TREE_OPERAND (arg0, 0);
9974 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9975 build_zero_cst (TREE_TYPE (tem)));
9978 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9979 multiple of 1 << CST. */
9980 if (TREE_CODE (arg1) == INTEGER_CST)
9982 wide_int cst1 = arg1;
9983 wide_int ncst1 = -cst1;
9984 if ((cst1 & ncst1) == ncst1
9985 && multiple_of_p (type, arg0,
9986 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
9987 return fold_convert_loc (loc, type, arg0);
9990 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9991 bits from CST2. */
9992 if (TREE_CODE (arg1) == INTEGER_CST
9993 && TREE_CODE (arg0) == MULT_EXPR
9994 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9996 wide_int warg1 = arg1;
9997 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
9999 if (masked == 0)
10000 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10001 arg0, arg1);
10002 else if (masked != warg1)
10004 /* Avoid the transform if arg1 is a mask of some
10005 mode which allows further optimizations. */
10006 int pop = wi::popcount (warg1);
10007 if (!(pop >= BITS_PER_UNIT
10008 && pow2p_hwi (pop)
10009 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10010 return fold_build2_loc (loc, code, type, op0,
10011 wide_int_to_tree (type, masked));
10015 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10016 ((A & N) + B) & M -> (A + B) & M
10017 Similarly if (N & M) == 0,
10018 ((A | N) + B) & M -> (A + B) & M
10019 and for - instead of + (or unary - instead of +)
10020 and/or ^ instead of |.
10021 If B is constant and (B & M) == 0, fold into A & M. */
10022 if (TREE_CODE (arg1) == INTEGER_CST)
10024 wide_int cst1 = arg1;
10025 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10026 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10027 && (TREE_CODE (arg0) == PLUS_EXPR
10028 || TREE_CODE (arg0) == MINUS_EXPR
10029 || TREE_CODE (arg0) == NEGATE_EXPR)
10030 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10031 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10033 tree pmop[2];
10034 int which = 0;
10035 wide_int cst0;
10037 /* Now we know that arg0 is (C + D) or (C - D) or
10038 -C and arg1 (M) is == (1LL << cst) - 1.
10039 Store C into PMOP[0] and D into PMOP[1]. */
10040 pmop[0] = TREE_OPERAND (arg0, 0);
10041 pmop[1] = NULL;
10042 if (TREE_CODE (arg0) != NEGATE_EXPR)
10044 pmop[1] = TREE_OPERAND (arg0, 1);
10045 which = 1;
10048 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10049 which = -1;
10051 for (; which >= 0; which--)
10052 switch (TREE_CODE (pmop[which]))
10054 case BIT_AND_EXPR:
10055 case BIT_IOR_EXPR:
10056 case BIT_XOR_EXPR:
10057 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10058 != INTEGER_CST)
10059 break;
10060 cst0 = TREE_OPERAND (pmop[which], 1);
10061 cst0 &= cst1;
10062 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10064 if (cst0 != cst1)
10065 break;
10067 else if (cst0 != 0)
10068 break;
10069 /* If C or D is of the form (A & N) where
10070 (N & M) == M, or of the form (A | N) or
10071 (A ^ N) where (N & M) == 0, replace it with A. */
10072 pmop[which] = TREE_OPERAND (pmop[which], 0);
10073 break;
10074 case INTEGER_CST:
10075 /* If C or D is a N where (N & M) == 0, it can be
10076 omitted (assumed 0). */
10077 if ((TREE_CODE (arg0) == PLUS_EXPR
10078 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10079 && (cst1 & pmop[which]) == 0)
10080 pmop[which] = NULL;
10081 break;
10082 default:
10083 break;
10086 /* Only build anything new if we optimized one or both arguments
10087 above. */
10088 if (pmop[0] != TREE_OPERAND (arg0, 0)
10089 || (TREE_CODE (arg0) != NEGATE_EXPR
10090 && pmop[1] != TREE_OPERAND (arg0, 1)))
10092 tree utype = TREE_TYPE (arg0);
10093 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10095 /* Perform the operations in a type that has defined
10096 overflow behavior. */
10097 utype = unsigned_type_for (TREE_TYPE (arg0));
10098 if (pmop[0] != NULL)
10099 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10100 if (pmop[1] != NULL)
10101 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10104 if (TREE_CODE (arg0) == NEGATE_EXPR)
10105 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10106 else if (TREE_CODE (arg0) == PLUS_EXPR)
10108 if (pmop[0] != NULL && pmop[1] != NULL)
10109 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10110 pmop[0], pmop[1]);
10111 else if (pmop[0] != NULL)
10112 tem = pmop[0];
10113 else if (pmop[1] != NULL)
10114 tem = pmop[1];
10115 else
10116 return build_int_cst (type, 0);
10118 else if (pmop[0] == NULL)
10119 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10120 else
10121 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10122 pmop[0], pmop[1]);
10123 /* TEM is now the new binary +, - or unary - replacement. */
10124 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10125 fold_convert_loc (loc, utype, arg1));
10126 return fold_convert_loc (loc, type, tem);
10131 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10132 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10133 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10135 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10137 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10138 if (mask == -1)
10139 return
10140 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10143 goto associate;
10145 case RDIV_EXPR:
10146 /* Don't touch a floating-point divide by zero unless the mode
10147 of the constant can represent infinity. */
10148 if (TREE_CODE (arg1) == REAL_CST
10149 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10150 && real_zerop (arg1))
10151 return NULL_TREE;
10153 /* (-A) / (-B) -> A / B */
10154 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10155 return fold_build2_loc (loc, RDIV_EXPR, type,
10156 TREE_OPERAND (arg0, 0),
10157 negate_expr (arg1));
10158 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10159 return fold_build2_loc (loc, RDIV_EXPR, type,
10160 negate_expr (arg0),
10161 TREE_OPERAND (arg1, 0));
10162 return NULL_TREE;
10164 case TRUNC_DIV_EXPR:
10165 /* Fall through */
10167 case FLOOR_DIV_EXPR:
10168 /* Simplify A / (B << N) where A and B are positive and B is
10169 a power of 2, to A >> (N + log2(B)). */
10170 strict_overflow_p = false;
10171 if (TREE_CODE (arg1) == LSHIFT_EXPR
10172 && (TYPE_UNSIGNED (type)
10173 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10175 tree sval = TREE_OPERAND (arg1, 0);
10176 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10178 tree sh_cnt = TREE_OPERAND (arg1, 1);
10179 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10180 wi::exact_log2 (sval));
10182 if (strict_overflow_p)
10183 fold_overflow_warning (("assuming signed overflow does not "
10184 "occur when simplifying A / (B << N)"),
10185 WARN_STRICT_OVERFLOW_MISC);
10187 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10188 sh_cnt, pow2);
10189 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10190 fold_convert_loc (loc, type, arg0), sh_cnt);
10194 /* Fall through */
10196 case ROUND_DIV_EXPR:
10197 case CEIL_DIV_EXPR:
10198 case EXACT_DIV_EXPR:
10199 if (integer_zerop (arg1))
10200 return NULL_TREE;
10202 /* Convert -A / -B to A / B when the type is signed and overflow is
10203 undefined. */
10204 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10205 && TREE_CODE (arg0) == NEGATE_EXPR
10206 && negate_expr_p (op1))
10208 if (INTEGRAL_TYPE_P (type))
10209 fold_overflow_warning (("assuming signed overflow does not occur "
10210 "when distributing negation across "
10211 "division"),
10212 WARN_STRICT_OVERFLOW_MISC);
10213 return fold_build2_loc (loc, code, type,
10214 fold_convert_loc (loc, type,
10215 TREE_OPERAND (arg0, 0)),
10216 negate_expr (op1));
10218 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10219 && TREE_CODE (arg1) == NEGATE_EXPR
10220 && negate_expr_p (op0))
10222 if (INTEGRAL_TYPE_P (type))
10223 fold_overflow_warning (("assuming signed overflow does not occur "
10224 "when distributing negation across "
10225 "division"),
10226 WARN_STRICT_OVERFLOW_MISC);
10227 return fold_build2_loc (loc, code, type,
10228 negate_expr (op0),
10229 fold_convert_loc (loc, type,
10230 TREE_OPERAND (arg1, 0)));
10233 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10234 operation, EXACT_DIV_EXPR.
10236 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10237 At one time others generated faster code, it's not clear if they do
10238 after the last round to changes to the DIV code in expmed.c. */
10239 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10240 && multiple_of_p (type, arg0, arg1))
10241 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10242 fold_convert (type, arg0),
10243 fold_convert (type, arg1));
10245 strict_overflow_p = false;
10246 if (TREE_CODE (arg1) == INTEGER_CST
10247 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10248 &strict_overflow_p)))
10250 if (strict_overflow_p)
10251 fold_overflow_warning (("assuming signed overflow does not occur "
10252 "when simplifying division"),
10253 WARN_STRICT_OVERFLOW_MISC);
10254 return fold_convert_loc (loc, type, tem);
10257 return NULL_TREE;
10259 case CEIL_MOD_EXPR:
10260 case FLOOR_MOD_EXPR:
10261 case ROUND_MOD_EXPR:
10262 case TRUNC_MOD_EXPR:
10263 strict_overflow_p = false;
10264 if (TREE_CODE (arg1) == INTEGER_CST
10265 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10266 &strict_overflow_p)))
10268 if (strict_overflow_p)
10269 fold_overflow_warning (("assuming signed overflow does not occur "
10270 "when simplifying modulus"),
10271 WARN_STRICT_OVERFLOW_MISC);
10272 return fold_convert_loc (loc, type, tem);
10275 return NULL_TREE;
10277 case LROTATE_EXPR:
10278 case RROTATE_EXPR:
10279 case RSHIFT_EXPR:
10280 case LSHIFT_EXPR:
10281 /* Since negative shift count is not well-defined,
10282 don't try to compute it in the compiler. */
10283 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10284 return NULL_TREE;
10286 prec = element_precision (type);
10288 /* If we have a rotate of a bit operation with the rotate count and
10289 the second operand of the bit operation both constant,
10290 permute the two operations. */
10291 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10292 && (TREE_CODE (arg0) == BIT_AND_EXPR
10293 || TREE_CODE (arg0) == BIT_IOR_EXPR
10294 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10295 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10297 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10298 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10299 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10300 fold_build2_loc (loc, code, type,
10301 arg00, arg1),
10302 fold_build2_loc (loc, code, type,
10303 arg01, arg1));
10306 /* Two consecutive rotates adding up to the some integer
10307 multiple of the precision of the type can be ignored. */
10308 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10309 && TREE_CODE (arg0) == RROTATE_EXPR
10310 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10311 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10312 prec) == 0)
10313 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10315 return NULL_TREE;
10317 case MIN_EXPR:
10318 case MAX_EXPR:
10319 goto associate;
10321 case TRUTH_ANDIF_EXPR:
10322 /* Note that the operands of this must be ints
10323 and their values must be 0 or 1.
10324 ("true" is a fixed value perhaps depending on the language.) */
10325 /* If first arg is constant zero, return it. */
10326 if (integer_zerop (arg0))
10327 return fold_convert_loc (loc, type, arg0);
10328 /* FALLTHRU */
10329 case TRUTH_AND_EXPR:
10330 /* If either arg is constant true, drop it. */
10331 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10332 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10333 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10334 /* Preserve sequence points. */
10335 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10336 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10337 /* If second arg is constant zero, result is zero, but first arg
10338 must be evaluated. */
10339 if (integer_zerop (arg1))
10340 return omit_one_operand_loc (loc, type, arg1, arg0);
10341 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10342 case will be handled here. */
10343 if (integer_zerop (arg0))
10344 return omit_one_operand_loc (loc, type, arg0, arg1);
10346 /* !X && X is always false. */
10347 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10348 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10349 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10350 /* X && !X is always false. */
10351 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10352 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10353 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10355 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10356 means A >= Y && A != MAX, but in this case we know that
10357 A < X <= MAX. */
10359 if (!TREE_SIDE_EFFECTS (arg0)
10360 && !TREE_SIDE_EFFECTS (arg1))
10362 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10363 if (tem && !operand_equal_p (tem, arg0, 0))
10364 return fold_build2_loc (loc, code, type, tem, arg1);
10366 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10367 if (tem && !operand_equal_p (tem, arg1, 0))
10368 return fold_build2_loc (loc, code, type, arg0, tem);
10371 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10372 != NULL_TREE)
10373 return tem;
10375 return NULL_TREE;
10377 case TRUTH_ORIF_EXPR:
10378 /* Note that the operands of this must be ints
10379 and their values must be 0 or true.
10380 ("true" is a fixed value perhaps depending on the language.) */
10381 /* If first arg is constant true, return it. */
10382 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10383 return fold_convert_loc (loc, type, arg0);
10384 /* FALLTHRU */
10385 case TRUTH_OR_EXPR:
10386 /* If either arg is constant zero, drop it. */
10387 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10388 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10389 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10390 /* Preserve sequence points. */
10391 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10392 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10393 /* If second arg is constant true, result is true, but we must
10394 evaluate first arg. */
10395 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10396 return omit_one_operand_loc (loc, type, arg1, arg0);
10397 /* Likewise for first arg, but note this only occurs here for
10398 TRUTH_OR_EXPR. */
10399 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10400 return omit_one_operand_loc (loc, type, arg0, arg1);
10402 /* !X || X is always true. */
10403 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10404 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10405 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10406 /* X || !X is always true. */
10407 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10408 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10409 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10411 /* (X && !Y) || (!X && Y) is X ^ Y */
10412 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10413 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10415 tree a0, a1, l0, l1, n0, n1;
10417 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10418 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10420 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10421 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10423 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10424 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10426 if ((operand_equal_p (n0, a0, 0)
10427 && operand_equal_p (n1, a1, 0))
10428 || (operand_equal_p (n0, a1, 0)
10429 && operand_equal_p (n1, a0, 0)))
10430 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10433 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10434 != NULL_TREE)
10435 return tem;
10437 return NULL_TREE;
10439 case TRUTH_XOR_EXPR:
10440 /* If the second arg is constant zero, drop it. */
10441 if (integer_zerop (arg1))
10442 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10443 /* If the second arg is constant true, this is a logical inversion. */
10444 if (integer_onep (arg1))
10446 tem = invert_truthvalue_loc (loc, arg0);
10447 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10449 /* Identical arguments cancel to zero. */
10450 if (operand_equal_p (arg0, arg1, 0))
10451 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10453 /* !X ^ X is always true. */
10454 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10455 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10456 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10458 /* X ^ !X is always true. */
10459 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10460 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10461 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10463 return NULL_TREE;
10465 case EQ_EXPR:
10466 case NE_EXPR:
10467 STRIP_NOPS (arg0);
10468 STRIP_NOPS (arg1);
10470 tem = fold_comparison (loc, code, type, op0, op1);
10471 if (tem != NULL_TREE)
10472 return tem;
10474 /* bool_var != 1 becomes !bool_var. */
10475 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10476 && code == NE_EXPR)
10477 return fold_convert_loc (loc, type,
10478 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10479 TREE_TYPE (arg0), arg0));
10481 /* bool_var == 0 becomes !bool_var. */
10482 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10483 && code == EQ_EXPR)
10484 return fold_convert_loc (loc, type,
10485 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10486 TREE_TYPE (arg0), arg0));
10488 /* !exp != 0 becomes !exp */
10489 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10490 && code == NE_EXPR)
10491 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10493 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10494 if ((TREE_CODE (arg0) == PLUS_EXPR
10495 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10496 || TREE_CODE (arg0) == MINUS_EXPR)
10497 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10498 0)),
10499 arg1, 0)
10500 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10501 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10503 tree val = TREE_OPERAND (arg0, 1);
10504 val = fold_build2_loc (loc, code, type, val,
10505 build_int_cst (TREE_TYPE (val), 0));
10506 return omit_two_operands_loc (loc, type, val,
10507 TREE_OPERAND (arg0, 0), arg1);
10510 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10511 if ((TREE_CODE (arg1) == PLUS_EXPR
10512 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10513 || TREE_CODE (arg1) == MINUS_EXPR)
10514 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10515 0)),
10516 arg0, 0)
10517 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10518 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10520 tree val = TREE_OPERAND (arg1, 1);
10521 val = fold_build2_loc (loc, code, type, val,
10522 build_int_cst (TREE_TYPE (val), 0));
10523 return omit_two_operands_loc (loc, type, val,
10524 TREE_OPERAND (arg1, 0), arg0);
10527 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10528 if (TREE_CODE (arg0) == MINUS_EXPR
10529 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10530 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10531 1)),
10532 arg1, 0)
10533 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10534 return omit_two_operands_loc (loc, type,
10535 code == NE_EXPR
10536 ? boolean_true_node : boolean_false_node,
10537 TREE_OPERAND (arg0, 1), arg1);
10539 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10540 if (TREE_CODE (arg1) == MINUS_EXPR
10541 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10542 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10543 1)),
10544 arg0, 0)
10545 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10546 return omit_two_operands_loc (loc, type,
10547 code == NE_EXPR
10548 ? boolean_true_node : boolean_false_node,
10549 TREE_OPERAND (arg1, 1), arg0);
10551 /* If this is an EQ or NE comparison with zero and ARG0 is
10552 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10553 two operations, but the latter can be done in one less insn
10554 on machines that have only two-operand insns or on which a
10555 constant cannot be the first operand. */
10556 if (TREE_CODE (arg0) == BIT_AND_EXPR
10557 && integer_zerop (arg1))
10559 tree arg00 = TREE_OPERAND (arg0, 0);
10560 tree arg01 = TREE_OPERAND (arg0, 1);
10561 if (TREE_CODE (arg00) == LSHIFT_EXPR
10562 && integer_onep (TREE_OPERAND (arg00, 0)))
10564 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10565 arg01, TREE_OPERAND (arg00, 1));
10566 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10567 build_int_cst (TREE_TYPE (arg0), 1));
10568 return fold_build2_loc (loc, code, type,
10569 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10570 arg1);
10572 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10573 && integer_onep (TREE_OPERAND (arg01, 0)))
10575 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10576 arg00, TREE_OPERAND (arg01, 1));
10577 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10578 build_int_cst (TREE_TYPE (arg0), 1));
10579 return fold_build2_loc (loc, code, type,
10580 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10581 arg1);
10585 /* If this is an NE or EQ comparison of zero against the result of a
10586 signed MOD operation whose second operand is a power of 2, make
10587 the MOD operation unsigned since it is simpler and equivalent. */
10588 if (integer_zerop (arg1)
10589 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10590 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10591 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10592 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10593 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10594 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10596 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10597 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10598 fold_convert_loc (loc, newtype,
10599 TREE_OPERAND (arg0, 0)),
10600 fold_convert_loc (loc, newtype,
10601 TREE_OPERAND (arg0, 1)));
10603 return fold_build2_loc (loc, code, type, newmod,
10604 fold_convert_loc (loc, newtype, arg1));
10607 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10608 C1 is a valid shift constant, and C2 is a power of two, i.e.
10609 a single bit. */
10610 if (TREE_CODE (arg0) == BIT_AND_EXPR
10611 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10612 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10613 == INTEGER_CST
10614 && integer_pow2p (TREE_OPERAND (arg0, 1))
10615 && integer_zerop (arg1))
10617 tree itype = TREE_TYPE (arg0);
10618 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10619 prec = TYPE_PRECISION (itype);
10621 /* Check for a valid shift count. */
10622 if (wi::ltu_p (arg001, prec))
10624 tree arg01 = TREE_OPERAND (arg0, 1);
10625 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10626 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10627 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10628 can be rewritten as (X & (C2 << C1)) != 0. */
10629 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10631 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10632 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10633 return fold_build2_loc (loc, code, type, tem,
10634 fold_convert_loc (loc, itype, arg1));
10636 /* Otherwise, for signed (arithmetic) shifts,
10637 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10638 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10639 else if (!TYPE_UNSIGNED (itype))
10640 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10641 arg000, build_int_cst (itype, 0));
10642 /* Otherwise, of unsigned (logical) shifts,
10643 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10644 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10645 else
10646 return omit_one_operand_loc (loc, type,
10647 code == EQ_EXPR ? integer_one_node
10648 : integer_zero_node,
10649 arg000);
10653 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10654 Similarly for NE_EXPR. */
10655 if (TREE_CODE (arg0) == BIT_AND_EXPR
10656 && TREE_CODE (arg1) == INTEGER_CST
10657 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10659 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10660 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10661 TREE_OPERAND (arg0, 1));
10662 tree dandnotc
10663 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10664 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10665 notc);
10666 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10667 if (integer_nonzerop (dandnotc))
10668 return omit_one_operand_loc (loc, type, rslt, arg0);
10671 /* If this is a comparison of a field, we may be able to simplify it. */
10672 if ((TREE_CODE (arg0) == COMPONENT_REF
10673 || TREE_CODE (arg0) == BIT_FIELD_REF)
10674 /* Handle the constant case even without -O
10675 to make sure the warnings are given. */
10676 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10678 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10679 if (t1)
10680 return t1;
10683 /* Optimize comparisons of strlen vs zero to a compare of the
10684 first character of the string vs zero. To wit,
10685 strlen(ptr) == 0 => *ptr == 0
10686 strlen(ptr) != 0 => *ptr != 0
10687 Other cases should reduce to one of these two (or a constant)
10688 due to the return value of strlen being unsigned. */
10689 if (TREE_CODE (arg0) == CALL_EXPR
10690 && integer_zerop (arg1))
10692 tree fndecl = get_callee_fndecl (arg0);
10694 if (fndecl
10695 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10696 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10697 && call_expr_nargs (arg0) == 1
10698 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10700 tree iref = build_fold_indirect_ref_loc (loc,
10701 CALL_EXPR_ARG (arg0, 0));
10702 return fold_build2_loc (loc, code, type, iref,
10703 build_int_cst (TREE_TYPE (iref), 0));
10707 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10708 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10709 if (TREE_CODE (arg0) == RSHIFT_EXPR
10710 && integer_zerop (arg1)
10711 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10713 tree arg00 = TREE_OPERAND (arg0, 0);
10714 tree arg01 = TREE_OPERAND (arg0, 1);
10715 tree itype = TREE_TYPE (arg00);
10716 if (wi::eq_p (arg01, element_precision (itype) - 1))
10718 if (TYPE_UNSIGNED (itype))
10720 itype = signed_type_for (itype);
10721 arg00 = fold_convert_loc (loc, itype, arg00);
10723 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10724 type, arg00, build_zero_cst (itype));
10728 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10729 (X & C) == 0 when C is a single bit. */
10730 if (TREE_CODE (arg0) == BIT_AND_EXPR
10731 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10732 && integer_zerop (arg1)
10733 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10735 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10736 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10737 TREE_OPERAND (arg0, 1));
10738 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10739 type, tem,
10740 fold_convert_loc (loc, TREE_TYPE (arg0),
10741 arg1));
10744 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10745 constant C is a power of two, i.e. a single bit. */
10746 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10747 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10748 && integer_zerop (arg1)
10749 && integer_pow2p (TREE_OPERAND (arg0, 1))
10750 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10751 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10753 tree arg00 = TREE_OPERAND (arg0, 0);
10754 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10755 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10758 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10759 when is C is a power of two, i.e. a single bit. */
10760 if (TREE_CODE (arg0) == BIT_AND_EXPR
10761 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10762 && integer_zerop (arg1)
10763 && integer_pow2p (TREE_OPERAND (arg0, 1))
10764 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10765 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10767 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10768 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10769 arg000, TREE_OPERAND (arg0, 1));
10770 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10771 tem, build_int_cst (TREE_TYPE (tem), 0));
10774 if (integer_zerop (arg1)
10775 && tree_expr_nonzero_p (arg0))
10777 tree res = constant_boolean_node (code==NE_EXPR, type);
10778 return omit_one_operand_loc (loc, type, res, arg0);
10781 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10782 if (TREE_CODE (arg0) == BIT_AND_EXPR
10783 && TREE_CODE (arg1) == BIT_AND_EXPR)
10785 tree arg00 = TREE_OPERAND (arg0, 0);
10786 tree arg01 = TREE_OPERAND (arg0, 1);
10787 tree arg10 = TREE_OPERAND (arg1, 0);
10788 tree arg11 = TREE_OPERAND (arg1, 1);
10789 tree itype = TREE_TYPE (arg0);
10791 if (operand_equal_p (arg01, arg11, 0))
10792 return fold_build2_loc (loc, code, type,
10793 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10794 fold_build2_loc (loc,
10795 BIT_XOR_EXPR, itype,
10796 arg00, arg10),
10797 arg01),
10798 build_zero_cst (itype));
10800 if (operand_equal_p (arg01, arg10, 0))
10801 return fold_build2_loc (loc, code, type,
10802 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10803 fold_build2_loc (loc,
10804 BIT_XOR_EXPR, itype,
10805 arg00, arg11),
10806 arg01),
10807 build_zero_cst (itype));
10809 if (operand_equal_p (arg00, arg11, 0))
10810 return fold_build2_loc (loc, code, type,
10811 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10812 fold_build2_loc (loc,
10813 BIT_XOR_EXPR, itype,
10814 arg01, arg10),
10815 arg00),
10816 build_zero_cst (itype));
10818 if (operand_equal_p (arg00, arg10, 0))
10819 return fold_build2_loc (loc, code, type,
10820 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10821 fold_build2_loc (loc,
10822 BIT_XOR_EXPR, itype,
10823 arg01, arg11),
10824 arg00),
10825 build_zero_cst (itype));
10828 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10829 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10831 tree arg00 = TREE_OPERAND (arg0, 0);
10832 tree arg01 = TREE_OPERAND (arg0, 1);
10833 tree arg10 = TREE_OPERAND (arg1, 0);
10834 tree arg11 = TREE_OPERAND (arg1, 1);
10835 tree itype = TREE_TYPE (arg0);
10837 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10838 operand_equal_p guarantees no side-effects so we don't need
10839 to use omit_one_operand on Z. */
10840 if (operand_equal_p (arg01, arg11, 0))
10841 return fold_build2_loc (loc, code, type, arg00,
10842 fold_convert_loc (loc, TREE_TYPE (arg00),
10843 arg10));
10844 if (operand_equal_p (arg01, arg10, 0))
10845 return fold_build2_loc (loc, code, type, arg00,
10846 fold_convert_loc (loc, TREE_TYPE (arg00),
10847 arg11));
10848 if (operand_equal_p (arg00, arg11, 0))
10849 return fold_build2_loc (loc, code, type, arg01,
10850 fold_convert_loc (loc, TREE_TYPE (arg01),
10851 arg10));
10852 if (operand_equal_p (arg00, arg10, 0))
10853 return fold_build2_loc (loc, code, type, arg01,
10854 fold_convert_loc (loc, TREE_TYPE (arg01),
10855 arg11));
10857 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10858 if (TREE_CODE (arg01) == INTEGER_CST
10859 && TREE_CODE (arg11) == INTEGER_CST)
10861 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10862 fold_convert_loc (loc, itype, arg11));
10863 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10864 return fold_build2_loc (loc, code, type, tem,
10865 fold_convert_loc (loc, itype, arg10));
10869 /* Attempt to simplify equality/inequality comparisons of complex
10870 values. Only lower the comparison if the result is known or
10871 can be simplified to a single scalar comparison. */
10872 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10873 || TREE_CODE (arg0) == COMPLEX_CST)
10874 && (TREE_CODE (arg1) == COMPLEX_EXPR
10875 || TREE_CODE (arg1) == COMPLEX_CST))
10877 tree real0, imag0, real1, imag1;
10878 tree rcond, icond;
10880 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10882 real0 = TREE_OPERAND (arg0, 0);
10883 imag0 = TREE_OPERAND (arg0, 1);
10885 else
10887 real0 = TREE_REALPART (arg0);
10888 imag0 = TREE_IMAGPART (arg0);
10891 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10893 real1 = TREE_OPERAND (arg1, 0);
10894 imag1 = TREE_OPERAND (arg1, 1);
10896 else
10898 real1 = TREE_REALPART (arg1);
10899 imag1 = TREE_IMAGPART (arg1);
10902 rcond = fold_binary_loc (loc, code, type, real0, real1);
10903 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10905 if (integer_zerop (rcond))
10907 if (code == EQ_EXPR)
10908 return omit_two_operands_loc (loc, type, boolean_false_node,
10909 imag0, imag1);
10910 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10912 else
10914 if (code == NE_EXPR)
10915 return omit_two_operands_loc (loc, type, boolean_true_node,
10916 imag0, imag1);
10917 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10921 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10922 if (icond && TREE_CODE (icond) == INTEGER_CST)
10924 if (integer_zerop (icond))
10926 if (code == EQ_EXPR)
10927 return omit_two_operands_loc (loc, type, boolean_false_node,
10928 real0, real1);
10929 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10931 else
10933 if (code == NE_EXPR)
10934 return omit_two_operands_loc (loc, type, boolean_true_node,
10935 real0, real1);
10936 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10941 return NULL_TREE;
10943 case LT_EXPR:
10944 case GT_EXPR:
10945 case LE_EXPR:
10946 case GE_EXPR:
10947 tem = fold_comparison (loc, code, type, op0, op1);
10948 if (tem != NULL_TREE)
10949 return tem;
10951 /* Transform comparisons of the form X +- C CMP X. */
10952 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10953 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10954 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10955 && !HONOR_SNANS (arg0))
10956 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10957 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10959 tree arg01 = TREE_OPERAND (arg0, 1);
10960 enum tree_code code0 = TREE_CODE (arg0);
10961 int is_positive;
10963 if (TREE_CODE (arg01) == REAL_CST)
10964 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10965 else
10966 is_positive = tree_int_cst_sgn (arg01);
10968 /* (X - c) > X becomes false. */
10969 if (code == GT_EXPR
10970 && ((code0 == MINUS_EXPR && is_positive >= 0)
10971 || (code0 == PLUS_EXPR && is_positive <= 0)))
10973 if (TREE_CODE (arg01) == INTEGER_CST
10974 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10975 fold_overflow_warning (("assuming signed overflow does not "
10976 "occur when assuming that (X - c) > X "
10977 "is always false"),
10978 WARN_STRICT_OVERFLOW_ALL);
10979 return constant_boolean_node (0, type);
10982 /* Likewise (X + c) < X becomes false. */
10983 if (code == LT_EXPR
10984 && ((code0 == PLUS_EXPR && is_positive >= 0)
10985 || (code0 == MINUS_EXPR && is_positive <= 0)))
10987 if (TREE_CODE (arg01) == INTEGER_CST
10988 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10989 fold_overflow_warning (("assuming signed overflow does not "
10990 "occur when assuming that "
10991 "(X + c) < X is always false"),
10992 WARN_STRICT_OVERFLOW_ALL);
10993 return constant_boolean_node (0, type);
10996 /* Convert (X - c) <= X to true. */
10997 if (!HONOR_NANS (arg1)
10998 && code == LE_EXPR
10999 && ((code0 == MINUS_EXPR && is_positive >= 0)
11000 || (code0 == PLUS_EXPR && is_positive <= 0)))
11002 if (TREE_CODE (arg01) == INTEGER_CST
11003 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11004 fold_overflow_warning (("assuming signed overflow does not "
11005 "occur when assuming that "
11006 "(X - c) <= X is always true"),
11007 WARN_STRICT_OVERFLOW_ALL);
11008 return constant_boolean_node (1, type);
11011 /* Convert (X + c) >= X to true. */
11012 if (!HONOR_NANS (arg1)
11013 && code == GE_EXPR
11014 && ((code0 == PLUS_EXPR && is_positive >= 0)
11015 || (code0 == MINUS_EXPR && is_positive <= 0)))
11017 if (TREE_CODE (arg01) == INTEGER_CST
11018 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11019 fold_overflow_warning (("assuming signed overflow does not "
11020 "occur when assuming that "
11021 "(X + c) >= X is always true"),
11022 WARN_STRICT_OVERFLOW_ALL);
11023 return constant_boolean_node (1, type);
11026 if (TREE_CODE (arg01) == INTEGER_CST)
11028 /* Convert X + c > X and X - c < X to true for integers. */
11029 if (code == GT_EXPR
11030 && ((code0 == PLUS_EXPR && is_positive > 0)
11031 || (code0 == MINUS_EXPR && is_positive < 0)))
11033 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11034 fold_overflow_warning (("assuming signed overflow does "
11035 "not occur when assuming that "
11036 "(X + c) > X is always true"),
11037 WARN_STRICT_OVERFLOW_ALL);
11038 return constant_boolean_node (1, type);
11041 if (code == LT_EXPR
11042 && ((code0 == MINUS_EXPR && is_positive > 0)
11043 || (code0 == PLUS_EXPR && is_positive < 0)))
11045 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11046 fold_overflow_warning (("assuming signed overflow does "
11047 "not occur when assuming that "
11048 "(X - c) < X is always true"),
11049 WARN_STRICT_OVERFLOW_ALL);
11050 return constant_boolean_node (1, type);
11053 /* Convert X + c <= X and X - c >= X to false for integers. */
11054 if (code == LE_EXPR
11055 && ((code0 == PLUS_EXPR && is_positive > 0)
11056 || (code0 == MINUS_EXPR && is_positive < 0)))
11058 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11059 fold_overflow_warning (("assuming signed overflow does "
11060 "not occur when assuming that "
11061 "(X + c) <= X is always false"),
11062 WARN_STRICT_OVERFLOW_ALL);
11063 return constant_boolean_node (0, type);
11066 if (code == GE_EXPR
11067 && ((code0 == MINUS_EXPR && is_positive > 0)
11068 || (code0 == PLUS_EXPR && is_positive < 0)))
11070 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11071 fold_overflow_warning (("assuming signed overflow does "
11072 "not occur when assuming that "
11073 "(X - c) >= X is always false"),
11074 WARN_STRICT_OVERFLOW_ALL);
11075 return constant_boolean_node (0, type);
11080 /* If we are comparing an ABS_EXPR with a constant, we can
11081 convert all the cases into explicit comparisons, but they may
11082 well not be faster than doing the ABS and one comparison.
11083 But ABS (X) <= C is a range comparison, which becomes a subtraction
11084 and a comparison, and is probably faster. */
11085 if (code == LE_EXPR
11086 && TREE_CODE (arg1) == INTEGER_CST
11087 && TREE_CODE (arg0) == ABS_EXPR
11088 && ! TREE_SIDE_EFFECTS (arg0)
11089 && (0 != (tem = negate_expr (arg1)))
11090 && TREE_CODE (tem) == INTEGER_CST
11091 && !TREE_OVERFLOW (tem))
11092 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11093 build2 (GE_EXPR, type,
11094 TREE_OPERAND (arg0, 0), tem),
11095 build2 (LE_EXPR, type,
11096 TREE_OPERAND (arg0, 0), arg1));
11098 /* Convert ABS_EXPR<x> >= 0 to true. */
11099 strict_overflow_p = false;
11100 if (code == GE_EXPR
11101 && (integer_zerop (arg1)
11102 || (! HONOR_NANS (arg0)
11103 && real_zerop (arg1)))
11104 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11106 if (strict_overflow_p)
11107 fold_overflow_warning (("assuming signed overflow does not occur "
11108 "when simplifying comparison of "
11109 "absolute value and zero"),
11110 WARN_STRICT_OVERFLOW_CONDITIONAL);
11111 return omit_one_operand_loc (loc, type,
11112 constant_boolean_node (true, type),
11113 arg0);
11116 /* Convert ABS_EXPR<x> < 0 to false. */
11117 strict_overflow_p = false;
11118 if (code == LT_EXPR
11119 && (integer_zerop (arg1) || real_zerop (arg1))
11120 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11122 if (strict_overflow_p)
11123 fold_overflow_warning (("assuming signed overflow does not occur "
11124 "when simplifying comparison of "
11125 "absolute value and zero"),
11126 WARN_STRICT_OVERFLOW_CONDITIONAL);
11127 return omit_one_operand_loc (loc, type,
11128 constant_boolean_node (false, type),
11129 arg0);
11132 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11133 and similarly for >= into !=. */
11134 if ((code == LT_EXPR || code == GE_EXPR)
11135 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11136 && TREE_CODE (arg1) == LSHIFT_EXPR
11137 && integer_onep (TREE_OPERAND (arg1, 0)))
11138 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11139 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11140 TREE_OPERAND (arg1, 1)),
11141 build_zero_cst (TREE_TYPE (arg0)));
11143 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11144 otherwise Y might be >= # of bits in X's type and thus e.g.
11145 (unsigned char) (1 << Y) for Y 15 might be 0.
11146 If the cast is widening, then 1 << Y should have unsigned type,
11147 otherwise if Y is number of bits in the signed shift type minus 1,
11148 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11149 31 might be 0xffffffff80000000. */
11150 if ((code == LT_EXPR || code == GE_EXPR)
11151 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11152 && CONVERT_EXPR_P (arg1)
11153 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11154 && (element_precision (TREE_TYPE (arg1))
11155 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11156 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11157 || (element_precision (TREE_TYPE (arg1))
11158 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11159 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11161 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11162 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11163 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11164 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11165 build_zero_cst (TREE_TYPE (arg0)));
11168 return NULL_TREE;
11170 case UNORDERED_EXPR:
11171 case ORDERED_EXPR:
11172 case UNLT_EXPR:
11173 case UNLE_EXPR:
11174 case UNGT_EXPR:
11175 case UNGE_EXPR:
11176 case UNEQ_EXPR:
11177 case LTGT_EXPR:
11178 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11180 tree targ0 = strip_float_extensions (arg0);
11181 tree targ1 = strip_float_extensions (arg1);
11182 tree newtype = TREE_TYPE (targ0);
11184 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11185 newtype = TREE_TYPE (targ1);
11187 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11188 return fold_build2_loc (loc, code, type,
11189 fold_convert_loc (loc, newtype, targ0),
11190 fold_convert_loc (loc, newtype, targ1));
11193 return NULL_TREE;
11195 case COMPOUND_EXPR:
11196 /* When pedantic, a compound expression can be neither an lvalue
11197 nor an integer constant expression. */
11198 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11199 return NULL_TREE;
11200 /* Don't let (0, 0) be null pointer constant. */
11201 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11202 : fold_convert_loc (loc, type, arg1);
11203 return pedantic_non_lvalue_loc (loc, tem);
11205 case ASSERT_EXPR:
11206 /* An ASSERT_EXPR should never be passed to fold_binary. */
11207 gcc_unreachable ();
11209 default:
11210 return NULL_TREE;
11211 } /* switch (code) */
11214 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11215 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11216 of GOTO_EXPR. */
11218 static tree
11219 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11221 switch (TREE_CODE (*tp))
11223 case LABEL_EXPR:
11224 return *tp;
11226 case GOTO_EXPR:
11227 *walk_subtrees = 0;
11229 /* fall through */
11231 default:
11232 return NULL_TREE;
11236 /* Return whether the sub-tree ST contains a label which is accessible from
11237 outside the sub-tree. */
11239 static bool
11240 contains_label_p (tree st)
11242 return
11243 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11246 /* Fold a ternary expression of code CODE and type TYPE with operands
11247 OP0, OP1, and OP2. Return the folded expression if folding is
11248 successful. Otherwise, return NULL_TREE. */
11250 tree
11251 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11252 tree op0, tree op1, tree op2)
11254 tree tem;
11255 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11256 enum tree_code_class kind = TREE_CODE_CLASS (code);
11258 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11259 && TREE_CODE_LENGTH (code) == 3);
11261 /* If this is a commutative operation, and OP0 is a constant, move it
11262 to OP1 to reduce the number of tests below. */
11263 if (commutative_ternary_tree_code (code)
11264 && tree_swap_operands_p (op0, op1, true))
11265 return fold_build3_loc (loc, code, type, op1, op0, op2);
11267 tem = generic_simplify (loc, code, type, op0, op1, op2);
11268 if (tem)
11269 return tem;
11271 /* Strip any conversions that don't change the mode. This is safe
11272 for every expression, except for a comparison expression because
11273 its signedness is derived from its operands. So, in the latter
11274 case, only strip conversions that don't change the signedness.
11276 Note that this is done as an internal manipulation within the
11277 constant folder, in order to find the simplest representation of
11278 the arguments so that their form can be studied. In any cases,
11279 the appropriate type conversions should be put back in the tree
11280 that will get out of the constant folder. */
11281 if (op0)
11283 arg0 = op0;
11284 STRIP_NOPS (arg0);
11287 if (op1)
11289 arg1 = op1;
11290 STRIP_NOPS (arg1);
11293 if (op2)
11295 arg2 = op2;
11296 STRIP_NOPS (arg2);
11299 switch (code)
11301 case COMPONENT_REF:
11302 if (TREE_CODE (arg0) == CONSTRUCTOR
11303 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11305 unsigned HOST_WIDE_INT idx;
11306 tree field, value;
11307 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11308 if (field == arg1)
11309 return value;
11311 return NULL_TREE;
11313 case COND_EXPR:
11314 case VEC_COND_EXPR:
11315 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11316 so all simple results must be passed through pedantic_non_lvalue. */
11317 if (TREE_CODE (arg0) == INTEGER_CST)
11319 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11320 tem = integer_zerop (arg0) ? op2 : op1;
11321 /* Only optimize constant conditions when the selected branch
11322 has the same type as the COND_EXPR. This avoids optimizing
11323 away "c ? x : throw", where the throw has a void type.
11324 Avoid throwing away that operand which contains label. */
11325 if ((!TREE_SIDE_EFFECTS (unused_op)
11326 || !contains_label_p (unused_op))
11327 && (! VOID_TYPE_P (TREE_TYPE (tem))
11328 || VOID_TYPE_P (type)))
11329 return pedantic_non_lvalue_loc (loc, tem);
11330 return NULL_TREE;
11332 else if (TREE_CODE (arg0) == VECTOR_CST)
11334 if ((TREE_CODE (arg1) == VECTOR_CST
11335 || TREE_CODE (arg1) == CONSTRUCTOR)
11336 && (TREE_CODE (arg2) == VECTOR_CST
11337 || TREE_CODE (arg2) == CONSTRUCTOR))
11339 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11340 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11341 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11342 for (i = 0; i < nelts; i++)
11344 tree val = VECTOR_CST_ELT (arg0, i);
11345 if (integer_all_onesp (val))
11346 sel[i] = i;
11347 else if (integer_zerop (val))
11348 sel[i] = nelts + i;
11349 else /* Currently unreachable. */
11350 return NULL_TREE;
11352 tree t = fold_vec_perm (type, arg1, arg2, sel);
11353 if (t != NULL_TREE)
11354 return t;
11358 /* If we have A op B ? A : C, we may be able to convert this to a
11359 simpler expression, depending on the operation and the values
11360 of B and C. Signed zeros prevent all of these transformations,
11361 for reasons given above each one.
11363 Also try swapping the arguments and inverting the conditional. */
11364 if (COMPARISON_CLASS_P (arg0)
11365 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11366 arg1, TREE_OPERAND (arg0, 1))
11367 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11369 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11370 if (tem)
11371 return tem;
11374 if (COMPARISON_CLASS_P (arg0)
11375 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11376 op2,
11377 TREE_OPERAND (arg0, 1))
11378 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11380 location_t loc0 = expr_location_or (arg0, loc);
11381 tem = fold_invert_truthvalue (loc0, arg0);
11382 if (tem && COMPARISON_CLASS_P (tem))
11384 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11385 if (tem)
11386 return tem;
11390 /* If the second operand is simpler than the third, swap them
11391 since that produces better jump optimization results. */
11392 if (truth_value_p (TREE_CODE (arg0))
11393 && tree_swap_operands_p (op1, op2, false))
11395 location_t loc0 = expr_location_or (arg0, loc);
11396 /* See if this can be inverted. If it can't, possibly because
11397 it was a floating-point inequality comparison, don't do
11398 anything. */
11399 tem = fold_invert_truthvalue (loc0, arg0);
11400 if (tem)
11401 return fold_build3_loc (loc, code, type, tem, op2, op1);
11404 /* Convert A ? 1 : 0 to simply A. */
11405 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11406 : (integer_onep (op1)
11407 && !VECTOR_TYPE_P (type)))
11408 && integer_zerop (op2)
11409 /* If we try to convert OP0 to our type, the
11410 call to fold will try to move the conversion inside
11411 a COND, which will recurse. In that case, the COND_EXPR
11412 is probably the best choice, so leave it alone. */
11413 && type == TREE_TYPE (arg0))
11414 return pedantic_non_lvalue_loc (loc, arg0);
11416 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11417 over COND_EXPR in cases such as floating point comparisons. */
11418 if (integer_zerop (op1)
11419 && code == COND_EXPR
11420 && integer_onep (op2)
11421 && !VECTOR_TYPE_P (type)
11422 && truth_value_p (TREE_CODE (arg0)))
11423 return pedantic_non_lvalue_loc (loc,
11424 fold_convert_loc (loc, type,
11425 invert_truthvalue_loc (loc,
11426 arg0)));
11428 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11429 if (TREE_CODE (arg0) == LT_EXPR
11430 && integer_zerop (TREE_OPERAND (arg0, 1))
11431 && integer_zerop (op2)
11432 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11434 /* sign_bit_p looks through both zero and sign extensions,
11435 but for this optimization only sign extensions are
11436 usable. */
11437 tree tem2 = TREE_OPERAND (arg0, 0);
11438 while (tem != tem2)
11440 if (TREE_CODE (tem2) != NOP_EXPR
11441 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11443 tem = NULL_TREE;
11444 break;
11446 tem2 = TREE_OPERAND (tem2, 0);
11448 /* sign_bit_p only checks ARG1 bits within A's precision.
11449 If <sign bit of A> has wider type than A, bits outside
11450 of A's precision in <sign bit of A> need to be checked.
11451 If they are all 0, this optimization needs to be done
11452 in unsigned A's type, if they are all 1 in signed A's type,
11453 otherwise this can't be done. */
11454 if (tem
11455 && TYPE_PRECISION (TREE_TYPE (tem))
11456 < TYPE_PRECISION (TREE_TYPE (arg1))
11457 && TYPE_PRECISION (TREE_TYPE (tem))
11458 < TYPE_PRECISION (type))
11460 int inner_width, outer_width;
11461 tree tem_type;
11463 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11464 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11465 if (outer_width > TYPE_PRECISION (type))
11466 outer_width = TYPE_PRECISION (type);
11468 wide_int mask = wi::shifted_mask
11469 (inner_width, outer_width - inner_width, false,
11470 TYPE_PRECISION (TREE_TYPE (arg1)));
11472 wide_int common = mask & arg1;
11473 if (common == mask)
11475 tem_type = signed_type_for (TREE_TYPE (tem));
11476 tem = fold_convert_loc (loc, tem_type, tem);
11478 else if (common == 0)
11480 tem_type = unsigned_type_for (TREE_TYPE (tem));
11481 tem = fold_convert_loc (loc, tem_type, tem);
11483 else
11484 tem = NULL;
11487 if (tem)
11488 return
11489 fold_convert_loc (loc, type,
11490 fold_build2_loc (loc, BIT_AND_EXPR,
11491 TREE_TYPE (tem), tem,
11492 fold_convert_loc (loc,
11493 TREE_TYPE (tem),
11494 arg1)));
11497 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11498 already handled above. */
11499 if (TREE_CODE (arg0) == BIT_AND_EXPR
11500 && integer_onep (TREE_OPERAND (arg0, 1))
11501 && integer_zerop (op2)
11502 && integer_pow2p (arg1))
11504 tree tem = TREE_OPERAND (arg0, 0);
11505 STRIP_NOPS (tem);
11506 if (TREE_CODE (tem) == RSHIFT_EXPR
11507 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11508 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11509 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11510 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11511 TREE_OPERAND (tem, 0), arg1);
11514 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11515 is probably obsolete because the first operand should be a
11516 truth value (that's why we have the two cases above), but let's
11517 leave it in until we can confirm this for all front-ends. */
11518 if (integer_zerop (op2)
11519 && TREE_CODE (arg0) == NE_EXPR
11520 && integer_zerop (TREE_OPERAND (arg0, 1))
11521 && integer_pow2p (arg1)
11522 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11523 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11524 arg1, OEP_ONLY_CONST))
11525 return pedantic_non_lvalue_loc (loc,
11526 fold_convert_loc (loc, type,
11527 TREE_OPERAND (arg0, 0)));
11529 /* Disable the transformations below for vectors, since
11530 fold_binary_op_with_conditional_arg may undo them immediately,
11531 yielding an infinite loop. */
11532 if (code == VEC_COND_EXPR)
11533 return NULL_TREE;
11535 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11536 if (integer_zerop (op2)
11537 && truth_value_p (TREE_CODE (arg0))
11538 && truth_value_p (TREE_CODE (arg1))
11539 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11540 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11541 : TRUTH_ANDIF_EXPR,
11542 type, fold_convert_loc (loc, type, arg0), arg1);
11544 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11545 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11546 && truth_value_p (TREE_CODE (arg0))
11547 && truth_value_p (TREE_CODE (arg1))
11548 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11550 location_t loc0 = expr_location_or (arg0, loc);
11551 /* Only perform transformation if ARG0 is easily inverted. */
11552 tem = fold_invert_truthvalue (loc0, arg0);
11553 if (tem)
11554 return fold_build2_loc (loc, code == VEC_COND_EXPR
11555 ? BIT_IOR_EXPR
11556 : TRUTH_ORIF_EXPR,
11557 type, fold_convert_loc (loc, type, tem),
11558 arg1);
11561 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11562 if (integer_zerop (arg1)
11563 && truth_value_p (TREE_CODE (arg0))
11564 && truth_value_p (TREE_CODE (op2))
11565 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11567 location_t loc0 = expr_location_or (arg0, loc);
11568 /* Only perform transformation if ARG0 is easily inverted. */
11569 tem = fold_invert_truthvalue (loc0, arg0);
11570 if (tem)
11571 return fold_build2_loc (loc, code == VEC_COND_EXPR
11572 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11573 type, fold_convert_loc (loc, type, tem),
11574 op2);
11577 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11578 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11579 && truth_value_p (TREE_CODE (arg0))
11580 && truth_value_p (TREE_CODE (op2))
11581 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11582 return fold_build2_loc (loc, code == VEC_COND_EXPR
11583 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11584 type, fold_convert_loc (loc, type, arg0), op2);
11586 return NULL_TREE;
11588 case CALL_EXPR:
11589 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11590 of fold_ternary on them. */
11591 gcc_unreachable ();
11593 case BIT_FIELD_REF:
11594 if (TREE_CODE (arg0) == VECTOR_CST
11595 && (type == TREE_TYPE (TREE_TYPE (arg0))
11596 || (TREE_CODE (type) == VECTOR_TYPE
11597 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11599 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11600 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11601 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11602 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11604 if (n != 0
11605 && (idx % width) == 0
11606 && (n % width) == 0
11607 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11609 idx = idx / width;
11610 n = n / width;
11612 if (TREE_CODE (arg0) == VECTOR_CST)
11614 if (n == 1)
11615 return VECTOR_CST_ELT (arg0, idx);
11617 tree *vals = XALLOCAVEC (tree, n);
11618 for (unsigned i = 0; i < n; ++i)
11619 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11620 return build_vector (type, vals);
11625 /* On constants we can use native encode/interpret to constant
11626 fold (nearly) all BIT_FIELD_REFs. */
11627 if (CONSTANT_CLASS_P (arg0)
11628 && can_native_interpret_type_p (type)
11629 && BITS_PER_UNIT == 8)
11631 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11632 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11633 /* Limit us to a reasonable amount of work. To relax the
11634 other limitations we need bit-shifting of the buffer
11635 and rounding up the size. */
11636 if (bitpos % BITS_PER_UNIT == 0
11637 && bitsize % BITS_PER_UNIT == 0
11638 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11640 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11641 unsigned HOST_WIDE_INT len
11642 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11643 bitpos / BITS_PER_UNIT);
11644 if (len > 0
11645 && len * BITS_PER_UNIT >= bitsize)
11647 tree v = native_interpret_expr (type, b,
11648 bitsize / BITS_PER_UNIT);
11649 if (v)
11650 return v;
11655 return NULL_TREE;
11657 case FMA_EXPR:
11658 /* For integers we can decompose the FMA if possible. */
11659 if (TREE_CODE (arg0) == INTEGER_CST
11660 && TREE_CODE (arg1) == INTEGER_CST)
11661 return fold_build2_loc (loc, PLUS_EXPR, type,
11662 const_binop (MULT_EXPR, arg0, arg1), arg2);
11663 if (integer_zerop (arg2))
11664 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11666 return fold_fma (loc, type, arg0, arg1, arg2);
11668 case VEC_PERM_EXPR:
11669 if (TREE_CODE (arg2) == VECTOR_CST)
11671 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11672 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11673 unsigned char *sel2 = sel + nelts;
11674 bool need_mask_canon = false;
11675 bool need_mask_canon2 = false;
11676 bool all_in_vec0 = true;
11677 bool all_in_vec1 = true;
11678 bool maybe_identity = true;
11679 bool single_arg = (op0 == op1);
11680 bool changed = false;
11682 mask2 = 2 * nelts - 1;
11683 mask = single_arg ? (nelts - 1) : mask2;
11684 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11685 for (i = 0; i < nelts; i++)
11687 tree val = VECTOR_CST_ELT (arg2, i);
11688 if (TREE_CODE (val) != INTEGER_CST)
11689 return NULL_TREE;
11691 /* Make sure that the perm value is in an acceptable
11692 range. */
11693 wide_int t = val;
11694 need_mask_canon |= wi::gtu_p (t, mask);
11695 need_mask_canon2 |= wi::gtu_p (t, mask2);
11696 sel[i] = t.to_uhwi () & mask;
11697 sel2[i] = t.to_uhwi () & mask2;
11699 if (sel[i] < nelts)
11700 all_in_vec1 = false;
11701 else
11702 all_in_vec0 = false;
11704 if ((sel[i] & (nelts-1)) != i)
11705 maybe_identity = false;
11708 if (maybe_identity)
11710 if (all_in_vec0)
11711 return op0;
11712 if (all_in_vec1)
11713 return op1;
11716 if (all_in_vec0)
11717 op1 = op0;
11718 else if (all_in_vec1)
11720 op0 = op1;
11721 for (i = 0; i < nelts; i++)
11722 sel[i] -= nelts;
11723 need_mask_canon = true;
11726 if ((TREE_CODE (op0) == VECTOR_CST
11727 || TREE_CODE (op0) == CONSTRUCTOR)
11728 && (TREE_CODE (op1) == VECTOR_CST
11729 || TREE_CODE (op1) == CONSTRUCTOR))
11731 tree t = fold_vec_perm (type, op0, op1, sel);
11732 if (t != NULL_TREE)
11733 return t;
11736 if (op0 == op1 && !single_arg)
11737 changed = true;
11739 /* Some targets are deficient and fail to expand a single
11740 argument permutation while still allowing an equivalent
11741 2-argument version. */
11742 if (need_mask_canon && arg2 == op2
11743 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11744 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11746 need_mask_canon = need_mask_canon2;
11747 sel = sel2;
11750 if (need_mask_canon && arg2 == op2)
11752 tree *tsel = XALLOCAVEC (tree, nelts);
11753 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11754 for (i = 0; i < nelts; i++)
11755 tsel[i] = build_int_cst (eltype, sel[i]);
11756 op2 = build_vector (TREE_TYPE (arg2), tsel);
11757 changed = true;
11760 if (changed)
11761 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11763 return NULL_TREE;
11765 case BIT_INSERT_EXPR:
11766 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11767 if (TREE_CODE (arg0) == INTEGER_CST
11768 && TREE_CODE (arg1) == INTEGER_CST)
11770 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11771 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11772 wide_int tem = wi::bit_and (arg0,
11773 wi::shifted_mask (bitpos, bitsize, true,
11774 TYPE_PRECISION (type)));
11775 wide_int tem2
11776 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11777 bitsize), bitpos);
11778 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11780 else if (TREE_CODE (arg0) == VECTOR_CST
11781 && CONSTANT_CLASS_P (arg1)
11782 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11783 TREE_TYPE (arg1)))
11785 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11786 unsigned HOST_WIDE_INT elsize
11787 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11788 if (bitpos % elsize == 0)
11790 unsigned k = bitpos / elsize;
11791 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11792 return arg0;
11793 else
11795 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11796 memcpy (elts, VECTOR_CST_ELTS (arg0),
11797 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11798 elts[k] = arg1;
11799 return build_vector (type, elts);
11803 return NULL_TREE;
11805 default:
11806 return NULL_TREE;
11807 } /* switch (code) */
11810 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11811 of an array (or vector). */
11813 tree
11814 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11816 tree index_type = NULL_TREE;
11817 offset_int low_bound = 0;
11819 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11821 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11822 if (domain_type && TYPE_MIN_VALUE (domain_type))
11824 /* Static constructors for variably sized objects makes no sense. */
11825 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11826 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11827 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11831 if (index_type)
11832 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11833 TYPE_SIGN (index_type));
11835 offset_int index = low_bound - 1;
11836 if (index_type)
11837 index = wi::ext (index, TYPE_PRECISION (index_type),
11838 TYPE_SIGN (index_type));
11840 offset_int max_index;
11841 unsigned HOST_WIDE_INT cnt;
11842 tree cfield, cval;
11844 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11846 /* Array constructor might explicitly set index, or specify a range,
11847 or leave index NULL meaning that it is next index after previous
11848 one. */
11849 if (cfield)
11851 if (TREE_CODE (cfield) == INTEGER_CST)
11852 max_index = index = wi::to_offset (cfield);
11853 else
11855 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11856 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11857 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11860 else
11862 index += 1;
11863 if (index_type)
11864 index = wi::ext (index, TYPE_PRECISION (index_type),
11865 TYPE_SIGN (index_type));
11866 max_index = index;
11869 /* Do we have match? */
11870 if (wi::cmpu (access_index, index) >= 0
11871 && wi::cmpu (access_index, max_index) <= 0)
11872 return cval;
11874 return NULL_TREE;
11877 /* Perform constant folding and related simplification of EXPR.
11878 The related simplifications include x*1 => x, x*0 => 0, etc.,
11879 and application of the associative law.
11880 NOP_EXPR conversions may be removed freely (as long as we
11881 are careful not to change the type of the overall expression).
11882 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11883 but we can constant-fold them if they have constant operands. */
11885 #ifdef ENABLE_FOLD_CHECKING
11886 # define fold(x) fold_1 (x)
11887 static tree fold_1 (tree);
11888 static
11889 #endif
11890 tree
11891 fold (tree expr)
11893 const tree t = expr;
11894 enum tree_code code = TREE_CODE (t);
11895 enum tree_code_class kind = TREE_CODE_CLASS (code);
11896 tree tem;
11897 location_t loc = EXPR_LOCATION (expr);
11899 /* Return right away if a constant. */
11900 if (kind == tcc_constant)
11901 return t;
11903 /* CALL_EXPR-like objects with variable numbers of operands are
11904 treated specially. */
11905 if (kind == tcc_vl_exp)
11907 if (code == CALL_EXPR)
11909 tem = fold_call_expr (loc, expr, false);
11910 return tem ? tem : expr;
11912 return expr;
11915 if (IS_EXPR_CODE_CLASS (kind))
11917 tree type = TREE_TYPE (t);
11918 tree op0, op1, op2;
11920 switch (TREE_CODE_LENGTH (code))
11922 case 1:
11923 op0 = TREE_OPERAND (t, 0);
11924 tem = fold_unary_loc (loc, code, type, op0);
11925 return tem ? tem : expr;
11926 case 2:
11927 op0 = TREE_OPERAND (t, 0);
11928 op1 = TREE_OPERAND (t, 1);
11929 tem = fold_binary_loc (loc, code, type, op0, op1);
11930 return tem ? tem : expr;
11931 case 3:
11932 op0 = TREE_OPERAND (t, 0);
11933 op1 = TREE_OPERAND (t, 1);
11934 op2 = TREE_OPERAND (t, 2);
11935 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11936 return tem ? tem : expr;
11937 default:
11938 break;
11942 switch (code)
11944 case ARRAY_REF:
11946 tree op0 = TREE_OPERAND (t, 0);
11947 tree op1 = TREE_OPERAND (t, 1);
11949 if (TREE_CODE (op1) == INTEGER_CST
11950 && TREE_CODE (op0) == CONSTRUCTOR
11951 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11953 tree val = get_array_ctor_element_at_index (op0,
11954 wi::to_offset (op1));
11955 if (val)
11956 return val;
11959 return t;
11962 /* Return a VECTOR_CST if possible. */
11963 case CONSTRUCTOR:
11965 tree type = TREE_TYPE (t);
11966 if (TREE_CODE (type) != VECTOR_TYPE)
11967 return t;
11969 unsigned i;
11970 tree val;
11971 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11972 if (! CONSTANT_CLASS_P (val))
11973 return t;
11975 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11978 case CONST_DECL:
11979 return fold (DECL_INITIAL (t));
11981 default:
11982 return t;
11983 } /* switch (code) */
11986 #ifdef ENABLE_FOLD_CHECKING
11987 #undef fold
11989 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11990 hash_table<nofree_ptr_hash<const tree_node> > *);
11991 static void fold_check_failed (const_tree, const_tree);
11992 void print_fold_checksum (const_tree);
11994 /* When --enable-checking=fold, compute a digest of expr before
11995 and after actual fold call to see if fold did not accidentally
11996 change original expr. */
11998 tree
11999 fold (tree expr)
12001 tree ret;
12002 struct md5_ctx ctx;
12003 unsigned char checksum_before[16], checksum_after[16];
12004 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12006 md5_init_ctx (&ctx);
12007 fold_checksum_tree (expr, &ctx, &ht);
12008 md5_finish_ctx (&ctx, checksum_before);
12009 ht.empty ();
12011 ret = fold_1 (expr);
12013 md5_init_ctx (&ctx);
12014 fold_checksum_tree (expr, &ctx, &ht);
12015 md5_finish_ctx (&ctx, checksum_after);
12017 if (memcmp (checksum_before, checksum_after, 16))
12018 fold_check_failed (expr, ret);
12020 return ret;
12023 void
12024 print_fold_checksum (const_tree expr)
12026 struct md5_ctx ctx;
12027 unsigned char checksum[16], cnt;
12028 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12030 md5_init_ctx (&ctx);
12031 fold_checksum_tree (expr, &ctx, &ht);
12032 md5_finish_ctx (&ctx, checksum);
12033 for (cnt = 0; cnt < 16; ++cnt)
12034 fprintf (stderr, "%02x", checksum[cnt]);
12035 putc ('\n', stderr);
12038 static void
12039 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12041 internal_error ("fold check: original tree changed by fold");
12044 static void
12045 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12046 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12048 const tree_node **slot;
12049 enum tree_code code;
12050 union tree_node buf;
12051 int i, len;
12053 recursive_label:
12054 if (expr == NULL)
12055 return;
12056 slot = ht->find_slot (expr, INSERT);
12057 if (*slot != NULL)
12058 return;
12059 *slot = expr;
12060 code = TREE_CODE (expr);
12061 if (TREE_CODE_CLASS (code) == tcc_declaration
12062 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12064 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12065 memcpy ((char *) &buf, expr, tree_size (expr));
12066 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12067 buf.decl_with_vis.symtab_node = NULL;
12068 expr = (tree) &buf;
12070 else if (TREE_CODE_CLASS (code) == tcc_type
12071 && (TYPE_POINTER_TO (expr)
12072 || TYPE_REFERENCE_TO (expr)
12073 || TYPE_CACHED_VALUES_P (expr)
12074 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12075 || TYPE_NEXT_VARIANT (expr)
12076 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12078 /* Allow these fields to be modified. */
12079 tree tmp;
12080 memcpy ((char *) &buf, expr, tree_size (expr));
12081 expr = tmp = (tree) &buf;
12082 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12083 TYPE_POINTER_TO (tmp) = NULL;
12084 TYPE_REFERENCE_TO (tmp) = NULL;
12085 TYPE_NEXT_VARIANT (tmp) = NULL;
12086 TYPE_ALIAS_SET (tmp) = -1;
12087 if (TYPE_CACHED_VALUES_P (tmp))
12089 TYPE_CACHED_VALUES_P (tmp) = 0;
12090 TYPE_CACHED_VALUES (tmp) = NULL;
12093 md5_process_bytes (expr, tree_size (expr), ctx);
12094 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12095 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12096 if (TREE_CODE_CLASS (code) != tcc_type
12097 && TREE_CODE_CLASS (code) != tcc_declaration
12098 && code != TREE_LIST
12099 && code != SSA_NAME
12100 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12101 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12102 switch (TREE_CODE_CLASS (code))
12104 case tcc_constant:
12105 switch (code)
12107 case STRING_CST:
12108 md5_process_bytes (TREE_STRING_POINTER (expr),
12109 TREE_STRING_LENGTH (expr), ctx);
12110 break;
12111 case COMPLEX_CST:
12112 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12113 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12114 break;
12115 case VECTOR_CST:
12116 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12117 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12118 break;
12119 default:
12120 break;
12122 break;
12123 case tcc_exceptional:
12124 switch (code)
12126 case TREE_LIST:
12127 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12128 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12129 expr = TREE_CHAIN (expr);
12130 goto recursive_label;
12131 break;
12132 case TREE_VEC:
12133 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12134 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12135 break;
12136 default:
12137 break;
12139 break;
12140 case tcc_expression:
12141 case tcc_reference:
12142 case tcc_comparison:
12143 case tcc_unary:
12144 case tcc_binary:
12145 case tcc_statement:
12146 case tcc_vl_exp:
12147 len = TREE_OPERAND_LENGTH (expr);
12148 for (i = 0; i < len; ++i)
12149 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12150 break;
12151 case tcc_declaration:
12152 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12153 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12154 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12156 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12157 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12158 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12159 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12160 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12163 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12165 if (TREE_CODE (expr) == FUNCTION_DECL)
12167 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12168 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12170 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12172 break;
12173 case tcc_type:
12174 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12175 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12176 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12177 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12178 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12179 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12180 if (INTEGRAL_TYPE_P (expr)
12181 || SCALAR_FLOAT_TYPE_P (expr))
12183 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12184 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12186 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12187 if (TREE_CODE (expr) == RECORD_TYPE
12188 || TREE_CODE (expr) == UNION_TYPE
12189 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12190 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12191 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12192 break;
12193 default:
12194 break;
12198 /* Helper function for outputting the checksum of a tree T. When
12199 debugging with gdb, you can "define mynext" to be "next" followed
12200 by "call debug_fold_checksum (op0)", then just trace down till the
12201 outputs differ. */
12203 DEBUG_FUNCTION void
12204 debug_fold_checksum (const_tree t)
12206 int i;
12207 unsigned char checksum[16];
12208 struct md5_ctx ctx;
12209 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12211 md5_init_ctx (&ctx);
12212 fold_checksum_tree (t, &ctx, &ht);
12213 md5_finish_ctx (&ctx, checksum);
12214 ht.empty ();
12216 for (i = 0; i < 16; i++)
12217 fprintf (stderr, "%d ", checksum[i]);
12219 fprintf (stderr, "\n");
12222 #endif
12224 /* Fold a unary tree expression with code CODE of type TYPE with an
12225 operand OP0. LOC is the location of the resulting expression.
12226 Return a folded expression if successful. Otherwise, return a tree
12227 expression with code CODE of type TYPE with an operand OP0. */
12229 tree
12230 fold_build1_stat_loc (location_t loc,
12231 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12233 tree tem;
12234 #ifdef ENABLE_FOLD_CHECKING
12235 unsigned char checksum_before[16], checksum_after[16];
12236 struct md5_ctx ctx;
12237 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12239 md5_init_ctx (&ctx);
12240 fold_checksum_tree (op0, &ctx, &ht);
12241 md5_finish_ctx (&ctx, checksum_before);
12242 ht.empty ();
12243 #endif
12245 tem = fold_unary_loc (loc, code, type, op0);
12246 if (!tem)
12247 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12249 #ifdef ENABLE_FOLD_CHECKING
12250 md5_init_ctx (&ctx);
12251 fold_checksum_tree (op0, &ctx, &ht);
12252 md5_finish_ctx (&ctx, checksum_after);
12254 if (memcmp (checksum_before, checksum_after, 16))
12255 fold_check_failed (op0, tem);
12256 #endif
12257 return tem;
12260 /* Fold a binary tree expression with code CODE of type TYPE with
12261 operands OP0 and OP1. LOC is the location of the resulting
12262 expression. Return a folded expression if successful. Otherwise,
12263 return a tree expression with code CODE of type TYPE with operands
12264 OP0 and OP1. */
12266 tree
12267 fold_build2_stat_loc (location_t loc,
12268 enum tree_code code, tree type, tree op0, tree op1
12269 MEM_STAT_DECL)
12271 tree tem;
12272 #ifdef ENABLE_FOLD_CHECKING
12273 unsigned char checksum_before_op0[16],
12274 checksum_before_op1[16],
12275 checksum_after_op0[16],
12276 checksum_after_op1[16];
12277 struct md5_ctx ctx;
12278 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12280 md5_init_ctx (&ctx);
12281 fold_checksum_tree (op0, &ctx, &ht);
12282 md5_finish_ctx (&ctx, checksum_before_op0);
12283 ht.empty ();
12285 md5_init_ctx (&ctx);
12286 fold_checksum_tree (op1, &ctx, &ht);
12287 md5_finish_ctx (&ctx, checksum_before_op1);
12288 ht.empty ();
12289 #endif
12291 tem = fold_binary_loc (loc, code, type, op0, op1);
12292 if (!tem)
12293 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12295 #ifdef ENABLE_FOLD_CHECKING
12296 md5_init_ctx (&ctx);
12297 fold_checksum_tree (op0, &ctx, &ht);
12298 md5_finish_ctx (&ctx, checksum_after_op0);
12299 ht.empty ();
12301 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12302 fold_check_failed (op0, tem);
12304 md5_init_ctx (&ctx);
12305 fold_checksum_tree (op1, &ctx, &ht);
12306 md5_finish_ctx (&ctx, checksum_after_op1);
12308 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12309 fold_check_failed (op1, tem);
12310 #endif
12311 return tem;
12314 /* Fold a ternary tree expression with code CODE of type TYPE with
12315 operands OP0, OP1, and OP2. Return a folded expression if
12316 successful. Otherwise, return a tree expression with code CODE of
12317 type TYPE with operands OP0, OP1, and OP2. */
12319 tree
12320 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12321 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12323 tree tem;
12324 #ifdef ENABLE_FOLD_CHECKING
12325 unsigned char checksum_before_op0[16],
12326 checksum_before_op1[16],
12327 checksum_before_op2[16],
12328 checksum_after_op0[16],
12329 checksum_after_op1[16],
12330 checksum_after_op2[16];
12331 struct md5_ctx ctx;
12332 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12334 md5_init_ctx (&ctx);
12335 fold_checksum_tree (op0, &ctx, &ht);
12336 md5_finish_ctx (&ctx, checksum_before_op0);
12337 ht.empty ();
12339 md5_init_ctx (&ctx);
12340 fold_checksum_tree (op1, &ctx, &ht);
12341 md5_finish_ctx (&ctx, checksum_before_op1);
12342 ht.empty ();
12344 md5_init_ctx (&ctx);
12345 fold_checksum_tree (op2, &ctx, &ht);
12346 md5_finish_ctx (&ctx, checksum_before_op2);
12347 ht.empty ();
12348 #endif
12350 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12351 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12352 if (!tem)
12353 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12355 #ifdef ENABLE_FOLD_CHECKING
12356 md5_init_ctx (&ctx);
12357 fold_checksum_tree (op0, &ctx, &ht);
12358 md5_finish_ctx (&ctx, checksum_after_op0);
12359 ht.empty ();
12361 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12362 fold_check_failed (op0, tem);
12364 md5_init_ctx (&ctx);
12365 fold_checksum_tree (op1, &ctx, &ht);
12366 md5_finish_ctx (&ctx, checksum_after_op1);
12367 ht.empty ();
12369 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12370 fold_check_failed (op1, tem);
12372 md5_init_ctx (&ctx);
12373 fold_checksum_tree (op2, &ctx, &ht);
12374 md5_finish_ctx (&ctx, checksum_after_op2);
12376 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12377 fold_check_failed (op2, tem);
12378 #endif
12379 return tem;
12382 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12383 arguments in ARGARRAY, and a null static chain.
12384 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12385 of type TYPE from the given operands as constructed by build_call_array. */
12387 tree
12388 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12389 int nargs, tree *argarray)
12391 tree tem;
12392 #ifdef ENABLE_FOLD_CHECKING
12393 unsigned char checksum_before_fn[16],
12394 checksum_before_arglist[16],
12395 checksum_after_fn[16],
12396 checksum_after_arglist[16];
12397 struct md5_ctx ctx;
12398 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12399 int i;
12401 md5_init_ctx (&ctx);
12402 fold_checksum_tree (fn, &ctx, &ht);
12403 md5_finish_ctx (&ctx, checksum_before_fn);
12404 ht.empty ();
12406 md5_init_ctx (&ctx);
12407 for (i = 0; i < nargs; i++)
12408 fold_checksum_tree (argarray[i], &ctx, &ht);
12409 md5_finish_ctx (&ctx, checksum_before_arglist);
12410 ht.empty ();
12411 #endif
12413 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12414 if (!tem)
12415 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12417 #ifdef ENABLE_FOLD_CHECKING
12418 md5_init_ctx (&ctx);
12419 fold_checksum_tree (fn, &ctx, &ht);
12420 md5_finish_ctx (&ctx, checksum_after_fn);
12421 ht.empty ();
12423 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12424 fold_check_failed (fn, tem);
12426 md5_init_ctx (&ctx);
12427 for (i = 0; i < nargs; i++)
12428 fold_checksum_tree (argarray[i], &ctx, &ht);
12429 md5_finish_ctx (&ctx, checksum_after_arglist);
12431 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12432 fold_check_failed (NULL_TREE, tem);
12433 #endif
12434 return tem;
12437 /* Perform constant folding and related simplification of initializer
12438 expression EXPR. These behave identically to "fold_buildN" but ignore
12439 potential run-time traps and exceptions that fold must preserve. */
12441 #define START_FOLD_INIT \
12442 int saved_signaling_nans = flag_signaling_nans;\
12443 int saved_trapping_math = flag_trapping_math;\
12444 int saved_rounding_math = flag_rounding_math;\
12445 int saved_trapv = flag_trapv;\
12446 int saved_folding_initializer = folding_initializer;\
12447 flag_signaling_nans = 0;\
12448 flag_trapping_math = 0;\
12449 flag_rounding_math = 0;\
12450 flag_trapv = 0;\
12451 folding_initializer = 1;
12453 #define END_FOLD_INIT \
12454 flag_signaling_nans = saved_signaling_nans;\
12455 flag_trapping_math = saved_trapping_math;\
12456 flag_rounding_math = saved_rounding_math;\
12457 flag_trapv = saved_trapv;\
12458 folding_initializer = saved_folding_initializer;
12460 tree
12461 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12462 tree type, tree op)
12464 tree result;
12465 START_FOLD_INIT;
12467 result = fold_build1_loc (loc, code, type, op);
12469 END_FOLD_INIT;
12470 return result;
12473 tree
12474 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12475 tree type, tree op0, tree op1)
12477 tree result;
12478 START_FOLD_INIT;
12480 result = fold_build2_loc (loc, code, type, op0, op1);
12482 END_FOLD_INIT;
12483 return result;
12486 tree
12487 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12488 int nargs, tree *argarray)
12490 tree result;
12491 START_FOLD_INIT;
12493 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12495 END_FOLD_INIT;
12496 return result;
12499 #undef START_FOLD_INIT
12500 #undef END_FOLD_INIT
12502 /* Determine if first argument is a multiple of second argument. Return 0 if
12503 it is not, or we cannot easily determined it to be.
12505 An example of the sort of thing we care about (at this point; this routine
12506 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12507 fold cases do now) is discovering that
12509 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12511 is a multiple of
12513 SAVE_EXPR (J * 8)
12515 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12517 This code also handles discovering that
12519 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12521 is a multiple of 8 so we don't have to worry about dealing with a
12522 possible remainder.
12524 Note that we *look* inside a SAVE_EXPR only to determine how it was
12525 calculated; it is not safe for fold to do much of anything else with the
12526 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12527 at run time. For example, the latter example above *cannot* be implemented
12528 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12529 evaluation time of the original SAVE_EXPR is not necessarily the same at
12530 the time the new expression is evaluated. The only optimization of this
12531 sort that would be valid is changing
12533 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12535 divided by 8 to
12537 SAVE_EXPR (I) * SAVE_EXPR (J)
12539 (where the same SAVE_EXPR (J) is used in the original and the
12540 transformed version). */
12543 multiple_of_p (tree type, const_tree top, const_tree bottom)
12545 gimple *stmt;
12546 tree t1, op1, op2;
12548 if (operand_equal_p (top, bottom, 0))
12549 return 1;
12551 if (TREE_CODE (type) != INTEGER_TYPE)
12552 return 0;
12554 switch (TREE_CODE (top))
12556 case BIT_AND_EXPR:
12557 /* Bitwise and provides a power of two multiple. If the mask is
12558 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12559 if (!integer_pow2p (bottom))
12560 return 0;
12561 /* FALLTHRU */
12563 case MULT_EXPR:
12564 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12565 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12567 case MINUS_EXPR:
12568 /* It is impossible to prove if op0 - op1 is multiple of bottom
12569 precisely, so be conservative here checking if both op0 and op1
12570 are multiple of bottom. Note we check the second operand first
12571 since it's usually simpler. */
12572 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12573 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12575 case PLUS_EXPR:
12576 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12577 as op0 - 3 if the expression has unsigned type. For example,
12578 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12579 op1 = TREE_OPERAND (top, 1);
12580 if (TYPE_UNSIGNED (type)
12581 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12582 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12583 return (multiple_of_p (type, op1, bottom)
12584 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12586 case LSHIFT_EXPR:
12587 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12589 op1 = TREE_OPERAND (top, 1);
12590 /* const_binop may not detect overflow correctly,
12591 so check for it explicitly here. */
12592 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12593 && 0 != (t1 = fold_convert (type,
12594 const_binop (LSHIFT_EXPR,
12595 size_one_node,
12596 op1)))
12597 && !TREE_OVERFLOW (t1))
12598 return multiple_of_p (type, t1, bottom);
12600 return 0;
12602 case NOP_EXPR:
12603 /* Can't handle conversions from non-integral or wider integral type. */
12604 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12605 || (TYPE_PRECISION (type)
12606 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12607 return 0;
12609 /* fall through */
12611 case SAVE_EXPR:
12612 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12614 case COND_EXPR:
12615 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12616 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12618 case INTEGER_CST:
12619 if (TREE_CODE (bottom) != INTEGER_CST
12620 || integer_zerop (bottom)
12621 || (TYPE_UNSIGNED (type)
12622 && (tree_int_cst_sgn (top) < 0
12623 || tree_int_cst_sgn (bottom) < 0)))
12624 return 0;
12625 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12626 SIGNED);
12628 case SSA_NAME:
12629 if (TREE_CODE (bottom) == INTEGER_CST
12630 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12631 && gimple_code (stmt) == GIMPLE_ASSIGN)
12633 enum tree_code code = gimple_assign_rhs_code (stmt);
12635 /* Check for special cases to see if top is defined as multiple
12636 of bottom:
12638 top = (X & ~(bottom - 1) ; bottom is power of 2
12642 Y = X % bottom
12643 top = X - Y. */
12644 if (code == BIT_AND_EXPR
12645 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12646 && TREE_CODE (op2) == INTEGER_CST
12647 && integer_pow2p (bottom)
12648 && wi::multiple_of_p (wi::to_widest (op2),
12649 wi::to_widest (bottom), UNSIGNED))
12650 return 1;
12652 op1 = gimple_assign_rhs1 (stmt);
12653 if (code == MINUS_EXPR
12654 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12655 && TREE_CODE (op2) == SSA_NAME
12656 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12657 && gimple_code (stmt) == GIMPLE_ASSIGN
12658 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12659 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12660 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12661 return 1;
12664 /* fall through */
12666 default:
12667 return 0;
12671 #define tree_expr_nonnegative_warnv_p(X, Y) \
12672 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12674 #define RECURSE(X) \
12675 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12677 /* Return true if CODE or TYPE is known to be non-negative. */
12679 static bool
12680 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12682 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12683 && truth_value_p (code))
12684 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12685 have a signed:1 type (where the value is -1 and 0). */
12686 return true;
12687 return false;
12690 /* Return true if (CODE OP0) is known to be non-negative. If the return
12691 value is based on the assumption that signed overflow is undefined,
12692 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12693 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12695 bool
12696 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12697 bool *strict_overflow_p, int depth)
12699 if (TYPE_UNSIGNED (type))
12700 return true;
12702 switch (code)
12704 case ABS_EXPR:
12705 /* We can't return 1 if flag_wrapv is set because
12706 ABS_EXPR<INT_MIN> = INT_MIN. */
12707 if (!ANY_INTEGRAL_TYPE_P (type))
12708 return true;
12709 if (TYPE_OVERFLOW_UNDEFINED (type))
12711 *strict_overflow_p = true;
12712 return true;
12714 break;
12716 case NON_LVALUE_EXPR:
12717 case FLOAT_EXPR:
12718 case FIX_TRUNC_EXPR:
12719 return RECURSE (op0);
12721 CASE_CONVERT:
12723 tree inner_type = TREE_TYPE (op0);
12724 tree outer_type = type;
12726 if (TREE_CODE (outer_type) == REAL_TYPE)
12728 if (TREE_CODE (inner_type) == REAL_TYPE)
12729 return RECURSE (op0);
12730 if (INTEGRAL_TYPE_P (inner_type))
12732 if (TYPE_UNSIGNED (inner_type))
12733 return true;
12734 return RECURSE (op0);
12737 else if (INTEGRAL_TYPE_P (outer_type))
12739 if (TREE_CODE (inner_type) == REAL_TYPE)
12740 return RECURSE (op0);
12741 if (INTEGRAL_TYPE_P (inner_type))
12742 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12743 && TYPE_UNSIGNED (inner_type);
12746 break;
12748 default:
12749 return tree_simple_nonnegative_warnv_p (code, type);
12752 /* We don't know sign of `t', so be conservative and return false. */
12753 return false;
12756 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12757 value is based on the assumption that signed overflow is undefined,
12758 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12759 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12761 bool
12762 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12763 tree op1, bool *strict_overflow_p,
12764 int depth)
12766 if (TYPE_UNSIGNED (type))
12767 return true;
12769 switch (code)
12771 case POINTER_PLUS_EXPR:
12772 case PLUS_EXPR:
12773 if (FLOAT_TYPE_P (type))
12774 return RECURSE (op0) && RECURSE (op1);
12776 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12777 both unsigned and at least 2 bits shorter than the result. */
12778 if (TREE_CODE (type) == INTEGER_TYPE
12779 && TREE_CODE (op0) == NOP_EXPR
12780 && TREE_CODE (op1) == NOP_EXPR)
12782 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12783 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12784 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12785 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12787 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12788 TYPE_PRECISION (inner2)) + 1;
12789 return prec < TYPE_PRECISION (type);
12792 break;
12794 case MULT_EXPR:
12795 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12797 /* x * x is always non-negative for floating point x
12798 or without overflow. */
12799 if (operand_equal_p (op0, op1, 0)
12800 || (RECURSE (op0) && RECURSE (op1)))
12802 if (ANY_INTEGRAL_TYPE_P (type)
12803 && TYPE_OVERFLOW_UNDEFINED (type))
12804 *strict_overflow_p = true;
12805 return true;
12809 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12810 both unsigned and their total bits is shorter than the result. */
12811 if (TREE_CODE (type) == INTEGER_TYPE
12812 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12813 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12815 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12816 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12817 : TREE_TYPE (op0);
12818 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12819 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12820 : TREE_TYPE (op1);
12822 bool unsigned0 = TYPE_UNSIGNED (inner0);
12823 bool unsigned1 = TYPE_UNSIGNED (inner1);
12825 if (TREE_CODE (op0) == INTEGER_CST)
12826 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12828 if (TREE_CODE (op1) == INTEGER_CST)
12829 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12831 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12832 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12834 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12835 ? tree_int_cst_min_precision (op0, UNSIGNED)
12836 : TYPE_PRECISION (inner0);
12838 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12839 ? tree_int_cst_min_precision (op1, UNSIGNED)
12840 : TYPE_PRECISION (inner1);
12842 return precision0 + precision1 < TYPE_PRECISION (type);
12845 return false;
12847 case BIT_AND_EXPR:
12848 case MAX_EXPR:
12849 return RECURSE (op0) || RECURSE (op1);
12851 case BIT_IOR_EXPR:
12852 case BIT_XOR_EXPR:
12853 case MIN_EXPR:
12854 case RDIV_EXPR:
12855 case TRUNC_DIV_EXPR:
12856 case CEIL_DIV_EXPR:
12857 case FLOOR_DIV_EXPR:
12858 case ROUND_DIV_EXPR:
12859 return RECURSE (op0) && RECURSE (op1);
12861 case TRUNC_MOD_EXPR:
12862 return RECURSE (op0);
12864 case FLOOR_MOD_EXPR:
12865 return RECURSE (op1);
12867 case CEIL_MOD_EXPR:
12868 case ROUND_MOD_EXPR:
12869 default:
12870 return tree_simple_nonnegative_warnv_p (code, type);
12873 /* We don't know sign of `t', so be conservative and return false. */
12874 return false;
12877 /* Return true if T is known to be non-negative. If the return
12878 value is based on the assumption that signed overflow is undefined,
12879 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12880 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12882 bool
12883 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12885 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12886 return true;
12888 switch (TREE_CODE (t))
12890 case INTEGER_CST:
12891 return tree_int_cst_sgn (t) >= 0;
12893 case REAL_CST:
12894 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12896 case FIXED_CST:
12897 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12899 case COND_EXPR:
12900 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12902 case SSA_NAME:
12903 /* Limit the depth of recursion to avoid quadratic behavior.
12904 This is expected to catch almost all occurrences in practice.
12905 If this code misses important cases that unbounded recursion
12906 would not, passes that need this information could be revised
12907 to provide it through dataflow propagation. */
12908 return (!name_registered_for_update_p (t)
12909 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12910 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12911 strict_overflow_p, depth));
12913 default:
12914 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12918 /* Return true if T is known to be non-negative. If the return
12919 value is based on the assumption that signed overflow is undefined,
12920 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12921 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12923 bool
12924 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12925 bool *strict_overflow_p, int depth)
12927 switch (fn)
12929 CASE_CFN_ACOS:
12930 CASE_CFN_ACOSH:
12931 CASE_CFN_CABS:
12932 CASE_CFN_COSH:
12933 CASE_CFN_ERFC:
12934 CASE_CFN_EXP:
12935 CASE_CFN_EXP10:
12936 CASE_CFN_EXP2:
12937 CASE_CFN_FABS:
12938 CASE_CFN_FDIM:
12939 CASE_CFN_HYPOT:
12940 CASE_CFN_POW10:
12941 CASE_CFN_FFS:
12942 CASE_CFN_PARITY:
12943 CASE_CFN_POPCOUNT:
12944 CASE_CFN_CLZ:
12945 CASE_CFN_CLRSB:
12946 case CFN_BUILT_IN_BSWAP32:
12947 case CFN_BUILT_IN_BSWAP64:
12948 /* Always true. */
12949 return true;
12951 CASE_CFN_SQRT:
12952 /* sqrt(-0.0) is -0.0. */
12953 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12954 return true;
12955 return RECURSE (arg0);
12957 CASE_CFN_ASINH:
12958 CASE_CFN_ATAN:
12959 CASE_CFN_ATANH:
12960 CASE_CFN_CBRT:
12961 CASE_CFN_CEIL:
12962 CASE_CFN_ERF:
12963 CASE_CFN_EXPM1:
12964 CASE_CFN_FLOOR:
12965 CASE_CFN_FMOD:
12966 CASE_CFN_FREXP:
12967 CASE_CFN_ICEIL:
12968 CASE_CFN_IFLOOR:
12969 CASE_CFN_IRINT:
12970 CASE_CFN_IROUND:
12971 CASE_CFN_LCEIL:
12972 CASE_CFN_LDEXP:
12973 CASE_CFN_LFLOOR:
12974 CASE_CFN_LLCEIL:
12975 CASE_CFN_LLFLOOR:
12976 CASE_CFN_LLRINT:
12977 CASE_CFN_LLROUND:
12978 CASE_CFN_LRINT:
12979 CASE_CFN_LROUND:
12980 CASE_CFN_MODF:
12981 CASE_CFN_NEARBYINT:
12982 CASE_CFN_RINT:
12983 CASE_CFN_ROUND:
12984 CASE_CFN_SCALB:
12985 CASE_CFN_SCALBLN:
12986 CASE_CFN_SCALBN:
12987 CASE_CFN_SIGNBIT:
12988 CASE_CFN_SIGNIFICAND:
12989 CASE_CFN_SINH:
12990 CASE_CFN_TANH:
12991 CASE_CFN_TRUNC:
12992 /* True if the 1st argument is nonnegative. */
12993 return RECURSE (arg0);
12995 CASE_CFN_FMAX:
12996 /* True if the 1st OR 2nd arguments are nonnegative. */
12997 return RECURSE (arg0) || RECURSE (arg1);
12999 CASE_CFN_FMIN:
13000 /* True if the 1st AND 2nd arguments are nonnegative. */
13001 return RECURSE (arg0) && RECURSE (arg1);
13003 CASE_CFN_COPYSIGN:
13004 /* True if the 2nd argument is nonnegative. */
13005 return RECURSE (arg1);
13007 CASE_CFN_POWI:
13008 /* True if the 1st argument is nonnegative or the second
13009 argument is an even integer. */
13010 if (TREE_CODE (arg1) == INTEGER_CST
13011 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13012 return true;
13013 return RECURSE (arg0);
13015 CASE_CFN_POW:
13016 /* True if the 1st argument is nonnegative or the second
13017 argument is an even integer valued real. */
13018 if (TREE_CODE (arg1) == REAL_CST)
13020 REAL_VALUE_TYPE c;
13021 HOST_WIDE_INT n;
13023 c = TREE_REAL_CST (arg1);
13024 n = real_to_integer (&c);
13025 if ((n & 1) == 0)
13027 REAL_VALUE_TYPE cint;
13028 real_from_integer (&cint, VOIDmode, n, SIGNED);
13029 if (real_identical (&c, &cint))
13030 return true;
13033 return RECURSE (arg0);
13035 default:
13036 break;
13038 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13041 /* Return true if T is known to be non-negative. If the return
13042 value is based on the assumption that signed overflow is undefined,
13043 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13044 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13046 static bool
13047 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13049 enum tree_code code = TREE_CODE (t);
13050 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13051 return true;
13053 switch (code)
13055 case TARGET_EXPR:
13057 tree temp = TARGET_EXPR_SLOT (t);
13058 t = TARGET_EXPR_INITIAL (t);
13060 /* If the initializer is non-void, then it's a normal expression
13061 that will be assigned to the slot. */
13062 if (!VOID_TYPE_P (t))
13063 return RECURSE (t);
13065 /* Otherwise, the initializer sets the slot in some way. One common
13066 way is an assignment statement at the end of the initializer. */
13067 while (1)
13069 if (TREE_CODE (t) == BIND_EXPR)
13070 t = expr_last (BIND_EXPR_BODY (t));
13071 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13072 || TREE_CODE (t) == TRY_CATCH_EXPR)
13073 t = expr_last (TREE_OPERAND (t, 0));
13074 else if (TREE_CODE (t) == STATEMENT_LIST)
13075 t = expr_last (t);
13076 else
13077 break;
13079 if (TREE_CODE (t) == MODIFY_EXPR
13080 && TREE_OPERAND (t, 0) == temp)
13081 return RECURSE (TREE_OPERAND (t, 1));
13083 return false;
13086 case CALL_EXPR:
13088 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13089 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13091 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13092 get_call_combined_fn (t),
13093 arg0,
13094 arg1,
13095 strict_overflow_p, depth);
13097 case COMPOUND_EXPR:
13098 case MODIFY_EXPR:
13099 return RECURSE (TREE_OPERAND (t, 1));
13101 case BIND_EXPR:
13102 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13104 case SAVE_EXPR:
13105 return RECURSE (TREE_OPERAND (t, 0));
13107 default:
13108 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13112 #undef RECURSE
13113 #undef tree_expr_nonnegative_warnv_p
13115 /* Return true if T is known to be non-negative. If the return
13116 value is based on the assumption that signed overflow is undefined,
13117 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13118 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13120 bool
13121 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13123 enum tree_code code;
13124 if (t == error_mark_node)
13125 return false;
13127 code = TREE_CODE (t);
13128 switch (TREE_CODE_CLASS (code))
13130 case tcc_binary:
13131 case tcc_comparison:
13132 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13133 TREE_TYPE (t),
13134 TREE_OPERAND (t, 0),
13135 TREE_OPERAND (t, 1),
13136 strict_overflow_p, depth);
13138 case tcc_unary:
13139 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13140 TREE_TYPE (t),
13141 TREE_OPERAND (t, 0),
13142 strict_overflow_p, depth);
13144 case tcc_constant:
13145 case tcc_declaration:
13146 case tcc_reference:
13147 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13149 default:
13150 break;
13153 switch (code)
13155 case TRUTH_AND_EXPR:
13156 case TRUTH_OR_EXPR:
13157 case TRUTH_XOR_EXPR:
13158 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13159 TREE_TYPE (t),
13160 TREE_OPERAND (t, 0),
13161 TREE_OPERAND (t, 1),
13162 strict_overflow_p, depth);
13163 case TRUTH_NOT_EXPR:
13164 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13165 TREE_TYPE (t),
13166 TREE_OPERAND (t, 0),
13167 strict_overflow_p, depth);
13169 case COND_EXPR:
13170 case CONSTRUCTOR:
13171 case OBJ_TYPE_REF:
13172 case ASSERT_EXPR:
13173 case ADDR_EXPR:
13174 case WITH_SIZE_EXPR:
13175 case SSA_NAME:
13176 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13178 default:
13179 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13183 /* Return true if `t' is known to be non-negative. Handle warnings
13184 about undefined signed overflow. */
13186 bool
13187 tree_expr_nonnegative_p (tree t)
13189 bool ret, strict_overflow_p;
13191 strict_overflow_p = false;
13192 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13193 if (strict_overflow_p)
13194 fold_overflow_warning (("assuming signed overflow does not occur when "
13195 "determining that expression is always "
13196 "non-negative"),
13197 WARN_STRICT_OVERFLOW_MISC);
13198 return ret;
13202 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13203 For floating point we further ensure that T is not denormal.
13204 Similar logic is present in nonzero_address in rtlanal.h.
13206 If the return value is based on the assumption that signed overflow
13207 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13208 change *STRICT_OVERFLOW_P. */
13210 bool
13211 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13212 bool *strict_overflow_p)
13214 switch (code)
13216 case ABS_EXPR:
13217 return tree_expr_nonzero_warnv_p (op0,
13218 strict_overflow_p);
13220 case NOP_EXPR:
13222 tree inner_type = TREE_TYPE (op0);
13223 tree outer_type = type;
13225 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13226 && tree_expr_nonzero_warnv_p (op0,
13227 strict_overflow_p));
13229 break;
13231 case NON_LVALUE_EXPR:
13232 return tree_expr_nonzero_warnv_p (op0,
13233 strict_overflow_p);
13235 default:
13236 break;
13239 return false;
13242 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13243 For floating point we further ensure that T is not denormal.
13244 Similar logic is present in nonzero_address in rtlanal.h.
13246 If the return value is based on the assumption that signed overflow
13247 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13248 change *STRICT_OVERFLOW_P. */
13250 bool
13251 tree_binary_nonzero_warnv_p (enum tree_code code,
13252 tree type,
13253 tree op0,
13254 tree op1, bool *strict_overflow_p)
13256 bool sub_strict_overflow_p;
13257 switch (code)
13259 case POINTER_PLUS_EXPR:
13260 case PLUS_EXPR:
13261 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13263 /* With the presence of negative values it is hard
13264 to say something. */
13265 sub_strict_overflow_p = false;
13266 if (!tree_expr_nonnegative_warnv_p (op0,
13267 &sub_strict_overflow_p)
13268 || !tree_expr_nonnegative_warnv_p (op1,
13269 &sub_strict_overflow_p))
13270 return false;
13271 /* One of operands must be positive and the other non-negative. */
13272 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13273 overflows, on a twos-complement machine the sum of two
13274 nonnegative numbers can never be zero. */
13275 return (tree_expr_nonzero_warnv_p (op0,
13276 strict_overflow_p)
13277 || tree_expr_nonzero_warnv_p (op1,
13278 strict_overflow_p));
13280 break;
13282 case MULT_EXPR:
13283 if (TYPE_OVERFLOW_UNDEFINED (type))
13285 if (tree_expr_nonzero_warnv_p (op0,
13286 strict_overflow_p)
13287 && tree_expr_nonzero_warnv_p (op1,
13288 strict_overflow_p))
13290 *strict_overflow_p = true;
13291 return true;
13294 break;
13296 case MIN_EXPR:
13297 sub_strict_overflow_p = false;
13298 if (tree_expr_nonzero_warnv_p (op0,
13299 &sub_strict_overflow_p)
13300 && tree_expr_nonzero_warnv_p (op1,
13301 &sub_strict_overflow_p))
13303 if (sub_strict_overflow_p)
13304 *strict_overflow_p = true;
13306 break;
13308 case MAX_EXPR:
13309 sub_strict_overflow_p = false;
13310 if (tree_expr_nonzero_warnv_p (op0,
13311 &sub_strict_overflow_p))
13313 if (sub_strict_overflow_p)
13314 *strict_overflow_p = true;
13316 /* When both operands are nonzero, then MAX must be too. */
13317 if (tree_expr_nonzero_warnv_p (op1,
13318 strict_overflow_p))
13319 return true;
13321 /* MAX where operand 0 is positive is positive. */
13322 return tree_expr_nonnegative_warnv_p (op0,
13323 strict_overflow_p);
13325 /* MAX where operand 1 is positive is positive. */
13326 else if (tree_expr_nonzero_warnv_p (op1,
13327 &sub_strict_overflow_p)
13328 && tree_expr_nonnegative_warnv_p (op1,
13329 &sub_strict_overflow_p))
13331 if (sub_strict_overflow_p)
13332 *strict_overflow_p = true;
13333 return true;
13335 break;
13337 case BIT_IOR_EXPR:
13338 return (tree_expr_nonzero_warnv_p (op1,
13339 strict_overflow_p)
13340 || tree_expr_nonzero_warnv_p (op0,
13341 strict_overflow_p));
13343 default:
13344 break;
13347 return false;
13350 /* Return true when T is an address and is known to be nonzero.
13351 For floating point we further ensure that T is not denormal.
13352 Similar logic is present in nonzero_address in rtlanal.h.
13354 If the return value is based on the assumption that signed overflow
13355 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13356 change *STRICT_OVERFLOW_P. */
13358 bool
13359 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13361 bool sub_strict_overflow_p;
13362 switch (TREE_CODE (t))
13364 case INTEGER_CST:
13365 return !integer_zerop (t);
13367 case ADDR_EXPR:
13369 tree base = TREE_OPERAND (t, 0);
13371 if (!DECL_P (base))
13372 base = get_base_address (base);
13374 if (base && TREE_CODE (base) == TARGET_EXPR)
13375 base = TARGET_EXPR_SLOT (base);
13377 if (!base)
13378 return false;
13380 /* For objects in symbol table check if we know they are non-zero.
13381 Don't do anything for variables and functions before symtab is built;
13382 it is quite possible that they will be declared weak later. */
13383 int nonzero_addr = maybe_nonzero_address (base);
13384 if (nonzero_addr >= 0)
13385 return nonzero_addr;
13387 /* Function local objects are never NULL. */
13388 if (DECL_P (base)
13389 && (DECL_CONTEXT (base)
13390 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13391 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13392 return true;
13394 /* Constants are never weak. */
13395 if (CONSTANT_CLASS_P (base))
13396 return true;
13398 return false;
13401 case COND_EXPR:
13402 sub_strict_overflow_p = false;
13403 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13404 &sub_strict_overflow_p)
13405 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13406 &sub_strict_overflow_p))
13408 if (sub_strict_overflow_p)
13409 *strict_overflow_p = true;
13410 return true;
13412 break;
13414 default:
13415 break;
13417 return false;
13420 #define integer_valued_real_p(X) \
13421 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13423 #define RECURSE(X) \
13424 ((integer_valued_real_p) (X, depth + 1))
13426 /* Return true if the floating point result of (CODE OP0) has an
13427 integer value. We also allow +Inf, -Inf and NaN to be considered
13428 integer values. Return false for signaling NaN.
13430 DEPTH is the current nesting depth of the query. */
13432 bool
13433 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13435 switch (code)
13437 case FLOAT_EXPR:
13438 return true;
13440 case ABS_EXPR:
13441 return RECURSE (op0);
13443 CASE_CONVERT:
13445 tree type = TREE_TYPE (op0);
13446 if (TREE_CODE (type) == INTEGER_TYPE)
13447 return true;
13448 if (TREE_CODE (type) == REAL_TYPE)
13449 return RECURSE (op0);
13450 break;
13453 default:
13454 break;
13456 return false;
13459 /* Return true if the floating point result of (CODE OP0 OP1) has an
13460 integer value. We also allow +Inf, -Inf and NaN to be considered
13461 integer values. Return false for signaling NaN.
13463 DEPTH is the current nesting depth of the query. */
13465 bool
13466 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13468 switch (code)
13470 case PLUS_EXPR:
13471 case MINUS_EXPR:
13472 case MULT_EXPR:
13473 case MIN_EXPR:
13474 case MAX_EXPR:
13475 return RECURSE (op0) && RECURSE (op1);
13477 default:
13478 break;
13480 return false;
13483 /* Return true if the floating point result of calling FNDECL with arguments
13484 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13485 considered integer values. Return false for signaling NaN. If FNDECL
13486 takes fewer than 2 arguments, the remaining ARGn are null.
13488 DEPTH is the current nesting depth of the query. */
13490 bool
13491 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13493 switch (fn)
13495 CASE_CFN_CEIL:
13496 CASE_CFN_FLOOR:
13497 CASE_CFN_NEARBYINT:
13498 CASE_CFN_RINT:
13499 CASE_CFN_ROUND:
13500 CASE_CFN_TRUNC:
13501 return true;
13503 CASE_CFN_FMIN:
13504 CASE_CFN_FMAX:
13505 return RECURSE (arg0) && RECURSE (arg1);
13507 default:
13508 break;
13510 return false;
13513 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13514 has an integer value. We also allow +Inf, -Inf and NaN to be
13515 considered integer values. Return false for signaling NaN.
13517 DEPTH is the current nesting depth of the query. */
13519 bool
13520 integer_valued_real_single_p (tree t, int depth)
13522 switch (TREE_CODE (t))
13524 case REAL_CST:
13525 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13527 case COND_EXPR:
13528 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13530 case SSA_NAME:
13531 /* Limit the depth of recursion to avoid quadratic behavior.
13532 This is expected to catch almost all occurrences in practice.
13533 If this code misses important cases that unbounded recursion
13534 would not, passes that need this information could be revised
13535 to provide it through dataflow propagation. */
13536 return (!name_registered_for_update_p (t)
13537 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13538 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13539 depth));
13541 default:
13542 break;
13544 return false;
13547 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13548 has an integer value. We also allow +Inf, -Inf and NaN to be
13549 considered integer values. Return false for signaling NaN.
13551 DEPTH is the current nesting depth of the query. */
13553 static bool
13554 integer_valued_real_invalid_p (tree t, int depth)
13556 switch (TREE_CODE (t))
13558 case COMPOUND_EXPR:
13559 case MODIFY_EXPR:
13560 case BIND_EXPR:
13561 return RECURSE (TREE_OPERAND (t, 1));
13563 case SAVE_EXPR:
13564 return RECURSE (TREE_OPERAND (t, 0));
13566 default:
13567 break;
13569 return false;
13572 #undef RECURSE
13573 #undef integer_valued_real_p
13575 /* Return true if the floating point expression T has an integer value.
13576 We also allow +Inf, -Inf and NaN to be considered integer values.
13577 Return false for signaling NaN.
13579 DEPTH is the current nesting depth of the query. */
13581 bool
13582 integer_valued_real_p (tree t, int depth)
13584 if (t == error_mark_node)
13585 return false;
13587 tree_code code = TREE_CODE (t);
13588 switch (TREE_CODE_CLASS (code))
13590 case tcc_binary:
13591 case tcc_comparison:
13592 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13593 TREE_OPERAND (t, 1), depth);
13595 case tcc_unary:
13596 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13598 case tcc_constant:
13599 case tcc_declaration:
13600 case tcc_reference:
13601 return integer_valued_real_single_p (t, depth);
13603 default:
13604 break;
13607 switch (code)
13609 case COND_EXPR:
13610 case SSA_NAME:
13611 return integer_valued_real_single_p (t, depth);
13613 case CALL_EXPR:
13615 tree arg0 = (call_expr_nargs (t) > 0
13616 ? CALL_EXPR_ARG (t, 0)
13617 : NULL_TREE);
13618 tree arg1 = (call_expr_nargs (t) > 1
13619 ? CALL_EXPR_ARG (t, 1)
13620 : NULL_TREE);
13621 return integer_valued_real_call_p (get_call_combined_fn (t),
13622 arg0, arg1, depth);
13625 default:
13626 return integer_valued_real_invalid_p (t, depth);
13630 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13631 attempt to fold the expression to a constant without modifying TYPE,
13632 OP0 or OP1.
13634 If the expression could be simplified to a constant, then return
13635 the constant. If the expression would not be simplified to a
13636 constant, then return NULL_TREE. */
13638 tree
13639 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13641 tree tem = fold_binary (code, type, op0, op1);
13642 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13645 /* Given the components of a unary expression CODE, TYPE and OP0,
13646 attempt to fold the expression to a constant without modifying
13647 TYPE or OP0.
13649 If the expression could be simplified to a constant, then return
13650 the constant. If the expression would not be simplified to a
13651 constant, then return NULL_TREE. */
13653 tree
13654 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13656 tree tem = fold_unary (code, type, op0);
13657 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13660 /* If EXP represents referencing an element in a constant string
13661 (either via pointer arithmetic or array indexing), return the
13662 tree representing the value accessed, otherwise return NULL. */
13664 tree
13665 fold_read_from_constant_string (tree exp)
13667 if ((TREE_CODE (exp) == INDIRECT_REF
13668 || TREE_CODE (exp) == ARRAY_REF)
13669 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13671 tree exp1 = TREE_OPERAND (exp, 0);
13672 tree index;
13673 tree string;
13674 location_t loc = EXPR_LOCATION (exp);
13676 if (TREE_CODE (exp) == INDIRECT_REF)
13677 string = string_constant (exp1, &index);
13678 else
13680 tree low_bound = array_ref_low_bound (exp);
13681 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13683 /* Optimize the special-case of a zero lower bound.
13685 We convert the low_bound to sizetype to avoid some problems
13686 with constant folding. (E.g. suppose the lower bound is 1,
13687 and its mode is QI. Without the conversion,l (ARRAY
13688 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13689 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13690 if (! integer_zerop (low_bound))
13691 index = size_diffop_loc (loc, index,
13692 fold_convert_loc (loc, sizetype, low_bound));
13694 string = exp1;
13697 if (string
13698 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13699 && TREE_CODE (string) == STRING_CST
13700 && TREE_CODE (index) == INTEGER_CST
13701 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13702 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13703 == MODE_INT)
13704 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13705 return build_int_cst_type (TREE_TYPE (exp),
13706 (TREE_STRING_POINTER (string)
13707 [TREE_INT_CST_LOW (index)]));
13709 return NULL;
13712 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13713 an integer constant, real, or fixed-point constant.
13715 TYPE is the type of the result. */
13717 static tree
13718 fold_negate_const (tree arg0, tree type)
13720 tree t = NULL_TREE;
13722 switch (TREE_CODE (arg0))
13724 case INTEGER_CST:
13726 bool overflow;
13727 wide_int val = wi::neg (arg0, &overflow);
13728 t = force_fit_type (type, val, 1,
13729 (overflow | TREE_OVERFLOW (arg0))
13730 && !TYPE_UNSIGNED (type));
13731 break;
13734 case REAL_CST:
13735 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13736 break;
13738 case FIXED_CST:
13740 FIXED_VALUE_TYPE f;
13741 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13742 &(TREE_FIXED_CST (arg0)), NULL,
13743 TYPE_SATURATING (type));
13744 t = build_fixed (type, f);
13745 /* Propagate overflow flags. */
13746 if (overflow_p | TREE_OVERFLOW (arg0))
13747 TREE_OVERFLOW (t) = 1;
13748 break;
13751 default:
13752 gcc_unreachable ();
13755 return t;
13758 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13759 an integer constant or real constant.
13761 TYPE is the type of the result. */
13763 tree
13764 fold_abs_const (tree arg0, tree type)
13766 tree t = NULL_TREE;
13768 switch (TREE_CODE (arg0))
13770 case INTEGER_CST:
13772 /* If the value is unsigned or non-negative, then the absolute value
13773 is the same as the ordinary value. */
13774 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13775 t = arg0;
13777 /* If the value is negative, then the absolute value is
13778 its negation. */
13779 else
13781 bool overflow;
13782 wide_int val = wi::neg (arg0, &overflow);
13783 t = force_fit_type (type, val, -1,
13784 overflow | TREE_OVERFLOW (arg0));
13787 break;
13789 case REAL_CST:
13790 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13791 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13792 else
13793 t = arg0;
13794 break;
13796 default:
13797 gcc_unreachable ();
13800 return t;
13803 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13804 constant. TYPE is the type of the result. */
13806 static tree
13807 fold_not_const (const_tree arg0, tree type)
13809 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13811 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13814 /* Given CODE, a relational operator, the target type, TYPE and two
13815 constant operands OP0 and OP1, return the result of the
13816 relational operation. If the result is not a compile time
13817 constant, then return NULL_TREE. */
13819 static tree
13820 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13822 int result, invert;
13824 /* From here on, the only cases we handle are when the result is
13825 known to be a constant. */
13827 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13829 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13830 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13832 /* Handle the cases where either operand is a NaN. */
13833 if (real_isnan (c0) || real_isnan (c1))
13835 switch (code)
13837 case EQ_EXPR:
13838 case ORDERED_EXPR:
13839 result = 0;
13840 break;
13842 case NE_EXPR:
13843 case UNORDERED_EXPR:
13844 case UNLT_EXPR:
13845 case UNLE_EXPR:
13846 case UNGT_EXPR:
13847 case UNGE_EXPR:
13848 case UNEQ_EXPR:
13849 result = 1;
13850 break;
13852 case LT_EXPR:
13853 case LE_EXPR:
13854 case GT_EXPR:
13855 case GE_EXPR:
13856 case LTGT_EXPR:
13857 if (flag_trapping_math)
13858 return NULL_TREE;
13859 result = 0;
13860 break;
13862 default:
13863 gcc_unreachable ();
13866 return constant_boolean_node (result, type);
13869 return constant_boolean_node (real_compare (code, c0, c1), type);
13872 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13874 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13875 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13876 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13879 /* Handle equality/inequality of complex constants. */
13880 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13882 tree rcond = fold_relational_const (code, type,
13883 TREE_REALPART (op0),
13884 TREE_REALPART (op1));
13885 tree icond = fold_relational_const (code, type,
13886 TREE_IMAGPART (op0),
13887 TREE_IMAGPART (op1));
13888 if (code == EQ_EXPR)
13889 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13890 else if (code == NE_EXPR)
13891 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13892 else
13893 return NULL_TREE;
13896 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13898 if (!VECTOR_TYPE_P (type))
13900 /* Have vector comparison with scalar boolean result. */
13901 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13902 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13903 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13905 tree elem0 = VECTOR_CST_ELT (op0, i);
13906 tree elem1 = VECTOR_CST_ELT (op1, i);
13907 tree tmp = fold_relational_const (code, type, elem0, elem1);
13908 if (tmp == NULL_TREE)
13909 return NULL_TREE;
13910 if (integer_zerop (tmp))
13911 return constant_boolean_node (false, type);
13913 return constant_boolean_node (true, type);
13915 unsigned count = VECTOR_CST_NELTS (op0);
13916 tree *elts = XALLOCAVEC (tree, count);
13917 gcc_assert (VECTOR_CST_NELTS (op1) == count
13918 && TYPE_VECTOR_SUBPARTS (type) == count);
13920 for (unsigned i = 0; i < count; i++)
13922 tree elem_type = TREE_TYPE (type);
13923 tree elem0 = VECTOR_CST_ELT (op0, i);
13924 tree elem1 = VECTOR_CST_ELT (op1, i);
13926 tree tem = fold_relational_const (code, elem_type,
13927 elem0, elem1);
13929 if (tem == NULL_TREE)
13930 return NULL_TREE;
13932 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13935 return build_vector (type, elts);
13938 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13940 To compute GT, swap the arguments and do LT.
13941 To compute GE, do LT and invert the result.
13942 To compute LE, swap the arguments, do LT and invert the result.
13943 To compute NE, do EQ and invert the result.
13945 Therefore, the code below must handle only EQ and LT. */
13947 if (code == LE_EXPR || code == GT_EXPR)
13949 std::swap (op0, op1);
13950 code = swap_tree_comparison (code);
13953 /* Note that it is safe to invert for real values here because we
13954 have already handled the one case that it matters. */
13956 invert = 0;
13957 if (code == NE_EXPR || code == GE_EXPR)
13959 invert = 1;
13960 code = invert_tree_comparison (code, false);
13963 /* Compute a result for LT or EQ if args permit;
13964 Otherwise return T. */
13965 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13967 if (code == EQ_EXPR)
13968 result = tree_int_cst_equal (op0, op1);
13969 else
13970 result = tree_int_cst_lt (op0, op1);
13972 else
13973 return NULL_TREE;
13975 if (invert)
13976 result ^= 1;
13977 return constant_boolean_node (result, type);
13980 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13981 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13982 itself. */
13984 tree
13985 fold_build_cleanup_point_expr (tree type, tree expr)
13987 /* If the expression does not have side effects then we don't have to wrap
13988 it with a cleanup point expression. */
13989 if (!TREE_SIDE_EFFECTS (expr))
13990 return expr;
13992 /* If the expression is a return, check to see if the expression inside the
13993 return has no side effects or the right hand side of the modify expression
13994 inside the return. If either don't have side effects set we don't need to
13995 wrap the expression in a cleanup point expression. Note we don't check the
13996 left hand side of the modify because it should always be a return decl. */
13997 if (TREE_CODE (expr) == RETURN_EXPR)
13999 tree op = TREE_OPERAND (expr, 0);
14000 if (!op || !TREE_SIDE_EFFECTS (op))
14001 return expr;
14002 op = TREE_OPERAND (op, 1);
14003 if (!TREE_SIDE_EFFECTS (op))
14004 return expr;
14007 return build1 (CLEANUP_POINT_EXPR, type, expr);
14010 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14011 of an indirection through OP0, or NULL_TREE if no simplification is
14012 possible. */
14014 tree
14015 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14017 tree sub = op0;
14018 tree subtype;
14020 STRIP_NOPS (sub);
14021 subtype = TREE_TYPE (sub);
14022 if (!POINTER_TYPE_P (subtype))
14023 return NULL_TREE;
14025 if (TREE_CODE (sub) == ADDR_EXPR)
14027 tree op = TREE_OPERAND (sub, 0);
14028 tree optype = TREE_TYPE (op);
14029 /* *&CONST_DECL -> to the value of the const decl. */
14030 if (TREE_CODE (op) == CONST_DECL)
14031 return DECL_INITIAL (op);
14032 /* *&p => p; make sure to handle *&"str"[cst] here. */
14033 if (type == optype)
14035 tree fop = fold_read_from_constant_string (op);
14036 if (fop)
14037 return fop;
14038 else
14039 return op;
14041 /* *(foo *)&fooarray => fooarray[0] */
14042 else if (TREE_CODE (optype) == ARRAY_TYPE
14043 && type == TREE_TYPE (optype)
14044 && (!in_gimple_form
14045 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14047 tree type_domain = TYPE_DOMAIN (optype);
14048 tree min_val = size_zero_node;
14049 if (type_domain && TYPE_MIN_VALUE (type_domain))
14050 min_val = TYPE_MIN_VALUE (type_domain);
14051 if (in_gimple_form
14052 && TREE_CODE (min_val) != INTEGER_CST)
14053 return NULL_TREE;
14054 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14055 NULL_TREE, NULL_TREE);
14057 /* *(foo *)&complexfoo => __real__ complexfoo */
14058 else if (TREE_CODE (optype) == COMPLEX_TYPE
14059 && type == TREE_TYPE (optype))
14060 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14061 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14062 else if (TREE_CODE (optype) == VECTOR_TYPE
14063 && type == TREE_TYPE (optype))
14065 tree part_width = TYPE_SIZE (type);
14066 tree index = bitsize_int (0);
14067 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14071 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14072 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14074 tree op00 = TREE_OPERAND (sub, 0);
14075 tree op01 = TREE_OPERAND (sub, 1);
14077 STRIP_NOPS (op00);
14078 if (TREE_CODE (op00) == ADDR_EXPR)
14080 tree op00type;
14081 op00 = TREE_OPERAND (op00, 0);
14082 op00type = TREE_TYPE (op00);
14084 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14085 if (TREE_CODE (op00type) == VECTOR_TYPE
14086 && type == TREE_TYPE (op00type))
14088 tree part_width = TYPE_SIZE (type);
14089 unsigned HOST_WIDE_INT max_offset
14090 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14091 * TYPE_VECTOR_SUBPARTS (op00type));
14092 if (tree_int_cst_sign_bit (op01) == 0
14093 && compare_tree_int (op01, max_offset) == -1)
14095 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14096 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14097 tree index = bitsize_int (indexi);
14098 return fold_build3_loc (loc,
14099 BIT_FIELD_REF, type, op00,
14100 part_width, index);
14103 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14104 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14105 && type == TREE_TYPE (op00type))
14107 tree size = TYPE_SIZE_UNIT (type);
14108 if (tree_int_cst_equal (size, op01))
14109 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14111 /* ((foo *)&fooarray)[1] => fooarray[1] */
14112 else if (TREE_CODE (op00type) == ARRAY_TYPE
14113 && type == TREE_TYPE (op00type))
14115 tree type_domain = TYPE_DOMAIN (op00type);
14116 tree min_val = size_zero_node;
14117 if (type_domain && TYPE_MIN_VALUE (type_domain))
14118 min_val = TYPE_MIN_VALUE (type_domain);
14119 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14120 TYPE_SIZE_UNIT (type));
14121 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14122 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14123 NULL_TREE, NULL_TREE);
14128 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14129 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14130 && type == TREE_TYPE (TREE_TYPE (subtype))
14131 && (!in_gimple_form
14132 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14134 tree type_domain;
14135 tree min_val = size_zero_node;
14136 sub = build_fold_indirect_ref_loc (loc, sub);
14137 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14138 if (type_domain && TYPE_MIN_VALUE (type_domain))
14139 min_val = TYPE_MIN_VALUE (type_domain);
14140 if (in_gimple_form
14141 && TREE_CODE (min_val) != INTEGER_CST)
14142 return NULL_TREE;
14143 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14144 NULL_TREE);
14147 return NULL_TREE;
14150 /* Builds an expression for an indirection through T, simplifying some
14151 cases. */
14153 tree
14154 build_fold_indirect_ref_loc (location_t loc, tree t)
14156 tree type = TREE_TYPE (TREE_TYPE (t));
14157 tree sub = fold_indirect_ref_1 (loc, type, t);
14159 if (sub)
14160 return sub;
14162 return build1_loc (loc, INDIRECT_REF, type, t);
14165 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14167 tree
14168 fold_indirect_ref_loc (location_t loc, tree t)
14170 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14172 if (sub)
14173 return sub;
14174 else
14175 return t;
14178 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14179 whose result is ignored. The type of the returned tree need not be
14180 the same as the original expression. */
14182 tree
14183 fold_ignored_result (tree t)
14185 if (!TREE_SIDE_EFFECTS (t))
14186 return integer_zero_node;
14188 for (;;)
14189 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14191 case tcc_unary:
14192 t = TREE_OPERAND (t, 0);
14193 break;
14195 case tcc_binary:
14196 case tcc_comparison:
14197 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14198 t = TREE_OPERAND (t, 0);
14199 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14200 t = TREE_OPERAND (t, 1);
14201 else
14202 return t;
14203 break;
14205 case tcc_expression:
14206 switch (TREE_CODE (t))
14208 case COMPOUND_EXPR:
14209 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14210 return t;
14211 t = TREE_OPERAND (t, 0);
14212 break;
14214 case COND_EXPR:
14215 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14216 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14217 return t;
14218 t = TREE_OPERAND (t, 0);
14219 break;
14221 default:
14222 return t;
14224 break;
14226 default:
14227 return t;
14231 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14233 tree
14234 round_up_loc (location_t loc, tree value, unsigned int divisor)
14236 tree div = NULL_TREE;
14238 if (divisor == 1)
14239 return value;
14241 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14242 have to do anything. Only do this when we are not given a const,
14243 because in that case, this check is more expensive than just
14244 doing it. */
14245 if (TREE_CODE (value) != INTEGER_CST)
14247 div = build_int_cst (TREE_TYPE (value), divisor);
14249 if (multiple_of_p (TREE_TYPE (value), value, div))
14250 return value;
14253 /* If divisor is a power of two, simplify this to bit manipulation. */
14254 if (pow2_or_zerop (divisor))
14256 if (TREE_CODE (value) == INTEGER_CST)
14258 wide_int val = value;
14259 bool overflow_p;
14261 if ((val & (divisor - 1)) == 0)
14262 return value;
14264 overflow_p = TREE_OVERFLOW (value);
14265 val += divisor - 1;
14266 val &= - (int) divisor;
14267 if (val == 0)
14268 overflow_p = true;
14270 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14272 else
14274 tree t;
14276 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14277 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14278 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14279 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14282 else
14284 if (!div)
14285 div = build_int_cst (TREE_TYPE (value), divisor);
14286 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14287 value = size_binop_loc (loc, MULT_EXPR, value, div);
14290 return value;
14293 /* Likewise, but round down. */
14295 tree
14296 round_down_loc (location_t loc, tree value, int divisor)
14298 tree div = NULL_TREE;
14300 gcc_assert (divisor > 0);
14301 if (divisor == 1)
14302 return value;
14304 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14305 have to do anything. Only do this when we are not given a const,
14306 because in that case, this check is more expensive than just
14307 doing it. */
14308 if (TREE_CODE (value) != INTEGER_CST)
14310 div = build_int_cst (TREE_TYPE (value), divisor);
14312 if (multiple_of_p (TREE_TYPE (value), value, div))
14313 return value;
14316 /* If divisor is a power of two, simplify this to bit manipulation. */
14317 if (pow2_or_zerop (divisor))
14319 tree t;
14321 t = build_int_cst (TREE_TYPE (value), -divisor);
14322 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14324 else
14326 if (!div)
14327 div = build_int_cst (TREE_TYPE (value), divisor);
14328 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14329 value = size_binop_loc (loc, MULT_EXPR, value, div);
14332 return value;
14335 /* Returns the pointer to the base of the object addressed by EXP and
14336 extracts the information about the offset of the access, storing it
14337 to PBITPOS and POFFSET. */
14339 static tree
14340 split_address_to_core_and_offset (tree exp,
14341 HOST_WIDE_INT *pbitpos, tree *poffset)
14343 tree core;
14344 machine_mode mode;
14345 int unsignedp, reversep, volatilep;
14346 HOST_WIDE_INT bitsize;
14347 location_t loc = EXPR_LOCATION (exp);
14349 if (TREE_CODE (exp) == ADDR_EXPR)
14351 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14352 poffset, &mode, &unsignedp, &reversep,
14353 &volatilep);
14354 core = build_fold_addr_expr_loc (loc, core);
14356 else
14358 core = exp;
14359 *pbitpos = 0;
14360 *poffset = NULL_TREE;
14363 return core;
14366 /* Returns true if addresses of E1 and E2 differ by a constant, false
14367 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14369 bool
14370 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14372 tree core1, core2;
14373 HOST_WIDE_INT bitpos1, bitpos2;
14374 tree toffset1, toffset2, tdiff, type;
14376 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14377 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14379 if (bitpos1 % BITS_PER_UNIT != 0
14380 || bitpos2 % BITS_PER_UNIT != 0
14381 || !operand_equal_p (core1, core2, 0))
14382 return false;
14384 if (toffset1 && toffset2)
14386 type = TREE_TYPE (toffset1);
14387 if (type != TREE_TYPE (toffset2))
14388 toffset2 = fold_convert (type, toffset2);
14390 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14391 if (!cst_and_fits_in_hwi (tdiff))
14392 return false;
14394 *diff = int_cst_value (tdiff);
14396 else if (toffset1 || toffset2)
14398 /* If only one of the offsets is non-constant, the difference cannot
14399 be a constant. */
14400 return false;
14402 else
14403 *diff = 0;
14405 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14406 return true;
14409 /* Return OFF converted to a pointer offset type suitable as offset for
14410 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14411 tree
14412 convert_to_ptrofftype_loc (location_t loc, tree off)
14414 return fold_convert_loc (loc, sizetype, off);
14417 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14418 tree
14419 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14421 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14422 ptr, convert_to_ptrofftype_loc (loc, off));
14425 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14426 tree
14427 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14429 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14430 ptr, size_int (off));
14433 /* Return a char pointer for a C string if it is a string constant
14434 or sum of string constant and integer constant. */
14436 const char *
14437 c_getstr (tree src)
14439 tree offset_node;
14441 src = string_constant (src, &offset_node);
14442 if (src == 0)
14443 return 0;
14445 if (offset_node == 0)
14446 return TREE_STRING_POINTER (src);
14447 else if (!tree_fits_uhwi_p (offset_node)
14448 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
14449 return 0;
14451 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
14454 #if CHECKING_P
14456 namespace selftest {
14458 /* Helper functions for writing tests of folding trees. */
14460 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14462 static void
14463 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14464 tree constant)
14466 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14469 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14470 wrapping WRAPPED_EXPR. */
14472 static void
14473 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14474 tree wrapped_expr)
14476 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14477 ASSERT_NE (wrapped_expr, result);
14478 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14479 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14482 /* Verify that various arithmetic binary operations are folded
14483 correctly. */
14485 static void
14486 test_arithmetic_folding ()
14488 tree type = integer_type_node;
14489 tree x = create_tmp_var_raw (type, "x");
14490 tree zero = build_zero_cst (type);
14491 tree one = build_int_cst (type, 1);
14493 /* Addition. */
14494 /* 1 <-- (0 + 1) */
14495 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14496 one);
14497 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14498 one);
14500 /* (nonlvalue)x <-- (x + 0) */
14501 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14504 /* Subtraction. */
14505 /* 0 <-- (x - x) */
14506 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14507 zero);
14508 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14511 /* Multiplication. */
14512 /* 0 <-- (x * 0) */
14513 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14514 zero);
14516 /* (nonlvalue)x <-- (x * 1) */
14517 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14521 /* Verify that various binary operations on vectors are folded
14522 correctly. */
14524 static void
14525 test_vector_folding ()
14527 tree inner_type = integer_type_node;
14528 tree type = build_vector_type (inner_type, 4);
14529 tree zero = build_zero_cst (type);
14530 tree one = build_one_cst (type);
14532 /* Verify equality tests that return a scalar boolean result. */
14533 tree res_type = boolean_type_node;
14534 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14535 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14536 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14537 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14540 /* Run all of the selftests within this file. */
14542 void
14543 fold_const_c_tests ()
14545 test_arithmetic_folding ();
14546 test_vector_folding ();
14549 } // namespace selftest
14551 #endif /* CHECKING_P */