re PR bootstrap/77993 (bootstrap failure on PowerPC/Linux)
[official-gcc.git] / gcc / fold-const.c
blobfb6e45dd393cd752b181e208647bea3b90d154f4
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
83 #ifndef LOAD_EXTEND_OP
84 #define LOAD_EXTEND_OP(M) UNKNOWN
85 #endif
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
113 static bool negate_expr_p (tree);
114 static tree negate_expr (tree);
115 static tree split_tree (location_t, tree, tree, enum tree_code,
116 tree *, tree *, tree *, int);
117 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
118 static enum comparison_code comparison_to_compcode (enum tree_code);
119 static enum tree_code compcode_to_comparison (enum comparison_code);
120 static int operand_equal_for_comparison_p (tree, tree, tree);
121 static int twoval_comparison_p (tree, tree *, tree *, int *);
122 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
123 static tree optimize_bit_field_compare (location_t, enum tree_code,
124 tree, tree, tree);
125 static int simple_operand_p (const_tree);
126 static bool simple_operand_p_2 (tree);
127 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
128 static tree range_predecessor (tree);
129 static tree range_successor (tree);
130 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
131 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
132 static tree unextend (tree, int, int, tree);
133 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
134 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
135 static tree fold_binary_op_with_conditional_arg (location_t,
136 enum tree_code, tree,
137 tree, tree,
138 tree, tree, int);
139 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
140 static bool reorder_operands_p (const_tree, const_tree);
141 static tree fold_negate_const (tree, tree);
142 static tree fold_not_const (const_tree, tree);
143 static tree fold_relational_const (enum tree_code, tree, tree, tree);
144 static tree fold_convert_const (enum tree_code, tree, tree);
145 static tree fold_view_convert_expr (tree, tree);
146 static bool vec_cst_ctor_to_array (tree, tree *);
149 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
150 Otherwise, return LOC. */
152 static location_t
153 expr_location_or (tree t, location_t loc)
155 location_t tloc = EXPR_LOCATION (t);
156 return tloc == UNKNOWN_LOCATION ? loc : tloc;
159 /* Similar to protected_set_expr_location, but never modify x in place,
160 if location can and needs to be set, unshare it. */
162 static inline tree
163 protected_set_expr_location_unshare (tree x, location_t loc)
165 if (CAN_HAVE_LOCATION_P (x)
166 && EXPR_LOCATION (x) != loc
167 && !(TREE_CODE (x) == SAVE_EXPR
168 || TREE_CODE (x) == TARGET_EXPR
169 || TREE_CODE (x) == BIND_EXPR))
171 x = copy_node (x);
172 SET_EXPR_LOCATION (x, loc);
174 return x;
177 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
178 division and returns the quotient. Otherwise returns
179 NULL_TREE. */
181 tree
182 div_if_zero_remainder (const_tree arg1, const_tree arg2)
184 widest_int quo;
186 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
187 SIGNED, &quo))
188 return wide_int_to_tree (TREE_TYPE (arg1), quo);
190 return NULL_TREE;
193 /* This is nonzero if we should defer warnings about undefined
194 overflow. This facility exists because these warnings are a
195 special case. The code to estimate loop iterations does not want
196 to issue any warnings, since it works with expressions which do not
197 occur in user code. Various bits of cleanup code call fold(), but
198 only use the result if it has certain characteristics (e.g., is a
199 constant); that code only wants to issue a warning if the result is
200 used. */
202 static int fold_deferring_overflow_warnings;
204 /* If a warning about undefined overflow is deferred, this is the
205 warning. Note that this may cause us to turn two warnings into
206 one, but that is fine since it is sufficient to only give one
207 warning per expression. */
209 static const char* fold_deferred_overflow_warning;
211 /* If a warning about undefined overflow is deferred, this is the
212 level at which the warning should be emitted. */
214 static enum warn_strict_overflow_code fold_deferred_overflow_code;
216 /* Start deferring overflow warnings. We could use a stack here to
217 permit nested calls, but at present it is not necessary. */
219 void
220 fold_defer_overflow_warnings (void)
222 ++fold_deferring_overflow_warnings;
225 /* Stop deferring overflow warnings. If there is a pending warning,
226 and ISSUE is true, then issue the warning if appropriate. STMT is
227 the statement with which the warning should be associated (used for
228 location information); STMT may be NULL. CODE is the level of the
229 warning--a warn_strict_overflow_code value. This function will use
230 the smaller of CODE and the deferred code when deciding whether to
231 issue the warning. CODE may be zero to mean to always use the
232 deferred code. */
234 void
235 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
237 const char *warnmsg;
238 location_t locus;
240 gcc_assert (fold_deferring_overflow_warnings > 0);
241 --fold_deferring_overflow_warnings;
242 if (fold_deferring_overflow_warnings > 0)
244 if (fold_deferred_overflow_warning != NULL
245 && code != 0
246 && code < (int) fold_deferred_overflow_code)
247 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
248 return;
251 warnmsg = fold_deferred_overflow_warning;
252 fold_deferred_overflow_warning = NULL;
254 if (!issue || warnmsg == NULL)
255 return;
257 if (gimple_no_warning_p (stmt))
258 return;
260 /* Use the smallest code level when deciding to issue the
261 warning. */
262 if (code == 0 || code > (int) fold_deferred_overflow_code)
263 code = fold_deferred_overflow_code;
265 if (!issue_strict_overflow_warning (code))
266 return;
268 if (stmt == NULL)
269 locus = input_location;
270 else
271 locus = gimple_location (stmt);
272 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
275 /* Stop deferring overflow warnings, ignoring any deferred
276 warnings. */
278 void
279 fold_undefer_and_ignore_overflow_warnings (void)
281 fold_undefer_overflow_warnings (false, NULL, 0);
284 /* Whether we are deferring overflow warnings. */
286 bool
287 fold_deferring_overflow_warnings_p (void)
289 return fold_deferring_overflow_warnings > 0;
292 /* This is called when we fold something based on the fact that signed
293 overflow is undefined. */
295 void
296 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
298 if (fold_deferring_overflow_warnings > 0)
300 if (fold_deferred_overflow_warning == NULL
301 || wc < fold_deferred_overflow_code)
303 fold_deferred_overflow_warning = gmsgid;
304 fold_deferred_overflow_code = wc;
307 else if (issue_strict_overflow_warning (wc))
308 warning (OPT_Wstrict_overflow, gmsgid);
311 /* Return true if the built-in mathematical function specified by CODE
312 is odd, i.e. -f(x) == f(-x). */
314 bool
315 negate_mathfn_p (combined_fn fn)
317 switch (fn)
319 CASE_CFN_ASIN:
320 CASE_CFN_ASINH:
321 CASE_CFN_ATAN:
322 CASE_CFN_ATANH:
323 CASE_CFN_CASIN:
324 CASE_CFN_CASINH:
325 CASE_CFN_CATAN:
326 CASE_CFN_CATANH:
327 CASE_CFN_CBRT:
328 CASE_CFN_CPROJ:
329 CASE_CFN_CSIN:
330 CASE_CFN_CSINH:
331 CASE_CFN_CTAN:
332 CASE_CFN_CTANH:
333 CASE_CFN_ERF:
334 CASE_CFN_LLROUND:
335 CASE_CFN_LROUND:
336 CASE_CFN_ROUND:
337 CASE_CFN_SIN:
338 CASE_CFN_SINH:
339 CASE_CFN_TAN:
340 CASE_CFN_TANH:
341 CASE_CFN_TRUNC:
342 return true;
344 CASE_CFN_LLRINT:
345 CASE_CFN_LRINT:
346 CASE_CFN_NEARBYINT:
347 CASE_CFN_RINT:
348 return !flag_rounding_math;
350 default:
351 break;
353 return false;
356 /* Check whether we may negate an integer constant T without causing
357 overflow. */
359 bool
360 may_negate_without_overflow_p (const_tree t)
362 tree type;
364 gcc_assert (TREE_CODE (t) == INTEGER_CST);
366 type = TREE_TYPE (t);
367 if (TYPE_UNSIGNED (type))
368 return false;
370 return !wi::only_sign_bit_p (t);
373 /* Determine whether an expression T can be cheaply negated using
374 the function negate_expr without introducing undefined overflow. */
376 static bool
377 negate_expr_p (tree t)
379 tree type;
381 if (t == 0)
382 return false;
384 type = TREE_TYPE (t);
386 STRIP_SIGN_NOPS (t);
387 switch (TREE_CODE (t))
389 case INTEGER_CST:
390 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
391 return true;
393 /* Check that -CST will not overflow type. */
394 return may_negate_without_overflow_p (t);
395 case BIT_NOT_EXPR:
396 return (INTEGRAL_TYPE_P (type)
397 && TYPE_OVERFLOW_WRAPS (type));
399 case FIXED_CST:
400 return true;
402 case NEGATE_EXPR:
403 return !TYPE_OVERFLOW_SANITIZED (type);
405 case REAL_CST:
406 /* We want to canonicalize to positive real constants. Pretend
407 that only negative ones can be easily negated. */
408 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
410 case COMPLEX_CST:
411 return negate_expr_p (TREE_REALPART (t))
412 && negate_expr_p (TREE_IMAGPART (t));
414 case VECTOR_CST:
416 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
417 return true;
419 int count = TYPE_VECTOR_SUBPARTS (type), i;
421 for (i = 0; i < count; i++)
422 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
423 return false;
425 return true;
428 case COMPLEX_EXPR:
429 return negate_expr_p (TREE_OPERAND (t, 0))
430 && negate_expr_p (TREE_OPERAND (t, 1));
432 case CONJ_EXPR:
433 return negate_expr_p (TREE_OPERAND (t, 0));
435 case PLUS_EXPR:
436 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
437 || HONOR_SIGNED_ZEROS (element_mode (type))
438 || (INTEGRAL_TYPE_P (type)
439 && ! TYPE_OVERFLOW_WRAPS (type)))
440 return false;
441 /* -(A + B) -> (-B) - A. */
442 if (negate_expr_p (TREE_OPERAND (t, 1))
443 && reorder_operands_p (TREE_OPERAND (t, 0),
444 TREE_OPERAND (t, 1)))
445 return true;
446 /* -(A + B) -> (-A) - B. */
447 return negate_expr_p (TREE_OPERAND (t, 0));
449 case MINUS_EXPR:
450 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
451 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
452 && !HONOR_SIGNED_ZEROS (element_mode (type))
453 && (! INTEGRAL_TYPE_P (type)
454 || TYPE_OVERFLOW_WRAPS (type))
455 && reorder_operands_p (TREE_OPERAND (t, 0),
456 TREE_OPERAND (t, 1));
458 case MULT_EXPR:
459 if (TYPE_UNSIGNED (type))
460 break;
461 /* INT_MIN/n * n doesn't overflow while negating one operand it does
462 if n is a power of two. */
463 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
464 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
465 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
466 && ! integer_pow2p (TREE_OPERAND (t, 0)))
467 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
468 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
469 break;
471 /* Fall through. */
473 case RDIV_EXPR:
474 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
475 return negate_expr_p (TREE_OPERAND (t, 1))
476 || negate_expr_p (TREE_OPERAND (t, 0));
477 break;
479 case TRUNC_DIV_EXPR:
480 case ROUND_DIV_EXPR:
481 case EXACT_DIV_EXPR:
482 if (TYPE_UNSIGNED (type))
483 break;
484 if (negate_expr_p (TREE_OPERAND (t, 0)))
485 return true;
486 /* In general we can't negate B in A / B, because if A is INT_MIN and
487 B is 1, we may turn this into INT_MIN / -1 which is undefined
488 and actually traps on some architectures. */
489 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
490 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
491 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
492 && ! integer_onep (TREE_OPERAND (t, 1))))
493 return negate_expr_p (TREE_OPERAND (t, 1));
494 break;
496 case NOP_EXPR:
497 /* Negate -((double)float) as (double)(-float). */
498 if (TREE_CODE (type) == REAL_TYPE)
500 tree tem = strip_float_extensions (t);
501 if (tem != t)
502 return negate_expr_p (tem);
504 break;
506 case CALL_EXPR:
507 /* Negate -f(x) as f(-x). */
508 if (negate_mathfn_p (get_call_combined_fn (t)))
509 return negate_expr_p (CALL_EXPR_ARG (t, 0));
510 break;
512 case RSHIFT_EXPR:
513 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
514 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
516 tree op1 = TREE_OPERAND (t, 1);
517 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
518 return true;
520 break;
522 default:
523 break;
525 return false;
528 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
529 simplification is possible.
530 If negate_expr_p would return true for T, NULL_TREE will never be
531 returned. */
533 static tree
534 fold_negate_expr (location_t loc, tree t)
536 tree type = TREE_TYPE (t);
537 tree tem;
539 switch (TREE_CODE (t))
541 /* Convert - (~A) to A + 1. */
542 case BIT_NOT_EXPR:
543 if (INTEGRAL_TYPE_P (type))
544 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
545 build_one_cst (type));
546 break;
548 case INTEGER_CST:
549 tem = fold_negate_const (t, type);
550 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
551 || (ANY_INTEGRAL_TYPE_P (type)
552 && !TYPE_OVERFLOW_TRAPS (type)
553 && TYPE_OVERFLOW_WRAPS (type))
554 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
555 return tem;
556 break;
558 case REAL_CST:
559 tem = fold_negate_const (t, type);
560 return tem;
562 case FIXED_CST:
563 tem = fold_negate_const (t, type);
564 return tem;
566 case COMPLEX_CST:
568 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
569 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
570 if (rpart && ipart)
571 return build_complex (type, rpart, ipart);
573 break;
575 case VECTOR_CST:
577 int count = TYPE_VECTOR_SUBPARTS (type), i;
578 tree *elts = XALLOCAVEC (tree, count);
580 for (i = 0; i < count; i++)
582 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
583 if (elts[i] == NULL_TREE)
584 return NULL_TREE;
587 return build_vector (type, elts);
590 case COMPLEX_EXPR:
591 if (negate_expr_p (t))
592 return fold_build2_loc (loc, COMPLEX_EXPR, type,
593 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
594 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
595 break;
597 case CONJ_EXPR:
598 if (negate_expr_p (t))
599 return fold_build1_loc (loc, CONJ_EXPR, type,
600 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
601 break;
603 case NEGATE_EXPR:
604 if (!TYPE_OVERFLOW_SANITIZED (type))
605 return TREE_OPERAND (t, 0);
606 break;
608 case PLUS_EXPR:
609 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
610 && !HONOR_SIGNED_ZEROS (element_mode (type)))
612 /* -(A + B) -> (-B) - A. */
613 if (negate_expr_p (TREE_OPERAND (t, 1))
614 && reorder_operands_p (TREE_OPERAND (t, 0),
615 TREE_OPERAND (t, 1)))
617 tem = negate_expr (TREE_OPERAND (t, 1));
618 return fold_build2_loc (loc, MINUS_EXPR, type,
619 tem, TREE_OPERAND (t, 0));
622 /* -(A + B) -> (-A) - B. */
623 if (negate_expr_p (TREE_OPERAND (t, 0)))
625 tem = negate_expr (TREE_OPERAND (t, 0));
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 tem, TREE_OPERAND (t, 1));
630 break;
632 case MINUS_EXPR:
633 /* - (A - B) -> B - A */
634 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
635 && !HONOR_SIGNED_ZEROS (element_mode (type))
636 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
637 return fold_build2_loc (loc, MINUS_EXPR, type,
638 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
639 break;
641 case MULT_EXPR:
642 if (TYPE_UNSIGNED (type))
643 break;
645 /* Fall through. */
647 case RDIV_EXPR:
648 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
650 tem = TREE_OPERAND (t, 1);
651 if (negate_expr_p (tem))
652 return fold_build2_loc (loc, TREE_CODE (t), type,
653 TREE_OPERAND (t, 0), negate_expr (tem));
654 tem = TREE_OPERAND (t, 0);
655 if (negate_expr_p (tem))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (tem), TREE_OPERAND (t, 1));
659 break;
661 case TRUNC_DIV_EXPR:
662 case ROUND_DIV_EXPR:
663 case EXACT_DIV_EXPR:
664 if (TYPE_UNSIGNED (type))
665 break;
666 if (negate_expr_p (TREE_OPERAND (t, 0)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 negate_expr (TREE_OPERAND (t, 0)),
669 TREE_OPERAND (t, 1));
670 /* In general we can't negate B in A / B, because if A is INT_MIN and
671 B is 1, we may turn this into INT_MIN / -1 which is undefined
672 and actually traps on some architectures. */
673 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
674 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
675 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
676 && ! integer_onep (TREE_OPERAND (t, 1))))
677 && negate_expr_p (TREE_OPERAND (t, 1)))
678 return fold_build2_loc (loc, TREE_CODE (t), type,
679 TREE_OPERAND (t, 0),
680 negate_expr (TREE_OPERAND (t, 1)));
681 break;
683 case NOP_EXPR:
684 /* Convert -((double)float) into (double)(-float). */
685 if (TREE_CODE (type) == REAL_TYPE)
687 tem = strip_float_extensions (t);
688 if (tem != t && negate_expr_p (tem))
689 return fold_convert_loc (loc, type, negate_expr (tem));
691 break;
693 case CALL_EXPR:
694 /* Negate -f(x) as f(-x). */
695 if (negate_mathfn_p (get_call_combined_fn (t))
696 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
698 tree fndecl, arg;
700 fndecl = get_callee_fndecl (t);
701 arg = negate_expr (CALL_EXPR_ARG (t, 0));
702 return build_call_expr_loc (loc, fndecl, 1, arg);
704 break;
706 case RSHIFT_EXPR:
707 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
708 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
710 tree op1 = TREE_OPERAND (t, 1);
711 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
713 tree ntype = TYPE_UNSIGNED (type)
714 ? signed_type_for (type)
715 : unsigned_type_for (type);
716 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
717 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
718 return fold_convert_loc (loc, type, temp);
721 break;
723 default:
724 break;
727 return NULL_TREE;
730 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
731 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
732 return NULL_TREE. */
734 static tree
735 negate_expr (tree t)
737 tree type, tem;
738 location_t loc;
740 if (t == NULL_TREE)
741 return NULL_TREE;
743 loc = EXPR_LOCATION (t);
744 type = TREE_TYPE (t);
745 STRIP_SIGN_NOPS (t);
747 tem = fold_negate_expr (loc, t);
748 if (!tem)
749 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
750 return fold_convert_loc (loc, type, tem);
753 /* Split a tree IN into a constant, literal and variable parts that could be
754 combined with CODE to make IN. "constant" means an expression with
755 TREE_CONSTANT but that isn't an actual constant. CODE must be a
756 commutative arithmetic operation. Store the constant part into *CONP,
757 the literal in *LITP and return the variable part. If a part isn't
758 present, set it to null. If the tree does not decompose in this way,
759 return the entire tree as the variable part and the other parts as null.
761 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
762 case, we negate an operand that was subtracted. Except if it is a
763 literal for which we use *MINUS_LITP instead.
765 If NEGATE_P is true, we are negating all of IN, again except a literal
766 for which we use *MINUS_LITP instead. If a variable part is of pointer
767 type, it is negated after converting to TYPE. This prevents us from
768 generating illegal MINUS pointer expression. LOC is the location of
769 the converted variable part.
771 If IN is itself a literal or constant, return it as appropriate.
773 Note that we do not guarantee that any of the three values will be the
774 same type as IN, but they will have the same signedness and mode. */
776 static tree
777 split_tree (location_t loc, tree in, tree type, enum tree_code code,
778 tree *conp, tree *litp, tree *minus_litp, int negate_p)
780 tree var = 0;
782 *conp = 0;
783 *litp = 0;
784 *minus_litp = 0;
786 /* Strip any conversions that don't change the machine mode or signedness. */
787 STRIP_SIGN_NOPS (in);
789 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
790 || TREE_CODE (in) == FIXED_CST)
791 *litp = in;
792 else if (TREE_CODE (in) == code
793 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
794 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
795 /* We can associate addition and subtraction together (even
796 though the C standard doesn't say so) for integers because
797 the value is not affected. For reals, the value might be
798 affected, so we can't. */
799 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
800 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
802 tree op0 = TREE_OPERAND (in, 0);
803 tree op1 = TREE_OPERAND (in, 1);
804 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
805 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
807 /* First see if either of the operands is a literal, then a constant. */
808 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
809 || TREE_CODE (op0) == FIXED_CST)
810 *litp = op0, op0 = 0;
811 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
812 || TREE_CODE (op1) == FIXED_CST)
813 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
815 if (op0 != 0 && TREE_CONSTANT (op0))
816 *conp = op0, op0 = 0;
817 else if (op1 != 0 && TREE_CONSTANT (op1))
818 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
820 /* If we haven't dealt with either operand, this is not a case we can
821 decompose. Otherwise, VAR is either of the ones remaining, if any. */
822 if (op0 != 0 && op1 != 0)
823 var = in;
824 else if (op0 != 0)
825 var = op0;
826 else
827 var = op1, neg_var_p = neg1_p;
829 /* Now do any needed negations. */
830 if (neg_litp_p)
831 *minus_litp = *litp, *litp = 0;
832 if (neg_conp_p)
833 *conp = negate_expr (*conp);
834 if (neg_var_p && var)
836 /* Convert to TYPE before negating. */
837 var = fold_convert_loc (loc, type, var);
838 var = negate_expr (var);
841 else if (TREE_CONSTANT (in))
842 *conp = in;
843 else if (TREE_CODE (in) == BIT_NOT_EXPR
844 && code == PLUS_EXPR)
846 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
847 when IN is constant. */
848 *minus_litp = build_one_cst (TREE_TYPE (in));
849 var = negate_expr (TREE_OPERAND (in, 0));
851 else
852 var = in;
854 if (negate_p)
856 if (*litp)
857 *minus_litp = *litp, *litp = 0;
858 else if (*minus_litp)
859 *litp = *minus_litp, *minus_litp = 0;
860 *conp = negate_expr (*conp);
861 if (var)
863 /* Convert to TYPE before negating. */
864 var = fold_convert_loc (loc, type, var);
865 var = negate_expr (var);
869 return var;
872 /* Re-associate trees split by the above function. T1 and T2 are
873 either expressions to associate or null. Return the new
874 expression, if any. LOC is the location of the new expression. If
875 we build an operation, do it in TYPE and with CODE. */
877 static tree
878 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
880 if (t1 == 0)
881 return t2;
882 else if (t2 == 0)
883 return t1;
885 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
886 try to fold this since we will have infinite recursion. But do
887 deal with any NEGATE_EXPRs. */
888 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
889 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
891 if (code == PLUS_EXPR)
893 if (TREE_CODE (t1) == NEGATE_EXPR)
894 return build2_loc (loc, MINUS_EXPR, type,
895 fold_convert_loc (loc, type, t2),
896 fold_convert_loc (loc, type,
897 TREE_OPERAND (t1, 0)));
898 else if (TREE_CODE (t2) == NEGATE_EXPR)
899 return build2_loc (loc, MINUS_EXPR, type,
900 fold_convert_loc (loc, type, t1),
901 fold_convert_loc (loc, type,
902 TREE_OPERAND (t2, 0)));
903 else if (integer_zerop (t2))
904 return fold_convert_loc (loc, type, t1);
906 else if (code == MINUS_EXPR)
908 if (integer_zerop (t2))
909 return fold_convert_loc (loc, type, t1);
912 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
913 fold_convert_loc (loc, type, t2));
916 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
917 fold_convert_loc (loc, type, t2));
920 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
921 for use in int_const_binop, size_binop and size_diffop. */
923 static bool
924 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
926 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
927 return false;
928 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
929 return false;
931 switch (code)
933 case LSHIFT_EXPR:
934 case RSHIFT_EXPR:
935 case LROTATE_EXPR:
936 case RROTATE_EXPR:
937 return true;
939 default:
940 break;
943 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
944 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
945 && TYPE_MODE (type1) == TYPE_MODE (type2);
949 /* Combine two integer constants ARG1 and ARG2 under operation CODE
950 to produce a new constant. Return NULL_TREE if we don't know how
951 to evaluate CODE at compile-time. */
953 static tree
954 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
955 int overflowable)
957 wide_int res;
958 tree t;
959 tree type = TREE_TYPE (arg1);
960 signop sign = TYPE_SIGN (type);
961 bool overflow = false;
963 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
965 switch (code)
967 case BIT_IOR_EXPR:
968 res = wi::bit_or (arg1, arg2);
969 break;
971 case BIT_XOR_EXPR:
972 res = wi::bit_xor (arg1, arg2);
973 break;
975 case BIT_AND_EXPR:
976 res = wi::bit_and (arg1, arg2);
977 break;
979 case RSHIFT_EXPR:
980 case LSHIFT_EXPR:
981 if (wi::neg_p (arg2))
983 arg2 = -arg2;
984 if (code == RSHIFT_EXPR)
985 code = LSHIFT_EXPR;
986 else
987 code = RSHIFT_EXPR;
990 if (code == RSHIFT_EXPR)
991 /* It's unclear from the C standard whether shifts can overflow.
992 The following code ignores overflow; perhaps a C standard
993 interpretation ruling is needed. */
994 res = wi::rshift (arg1, arg2, sign);
995 else
996 res = wi::lshift (arg1, arg2);
997 break;
999 case RROTATE_EXPR:
1000 case LROTATE_EXPR:
1001 if (wi::neg_p (arg2))
1003 arg2 = -arg2;
1004 if (code == RROTATE_EXPR)
1005 code = LROTATE_EXPR;
1006 else
1007 code = RROTATE_EXPR;
1010 if (code == RROTATE_EXPR)
1011 res = wi::rrotate (arg1, arg2);
1012 else
1013 res = wi::lrotate (arg1, arg2);
1014 break;
1016 case PLUS_EXPR:
1017 res = wi::add (arg1, arg2, sign, &overflow);
1018 break;
1020 case MINUS_EXPR:
1021 res = wi::sub (arg1, arg2, sign, &overflow);
1022 break;
1024 case MULT_EXPR:
1025 res = wi::mul (arg1, arg2, sign, &overflow);
1026 break;
1028 case MULT_HIGHPART_EXPR:
1029 res = wi::mul_high (arg1, arg2, sign);
1030 break;
1032 case TRUNC_DIV_EXPR:
1033 case EXACT_DIV_EXPR:
1034 if (arg2 == 0)
1035 return NULL_TREE;
1036 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1037 break;
1039 case FLOOR_DIV_EXPR:
1040 if (arg2 == 0)
1041 return NULL_TREE;
1042 res = wi::div_floor (arg1, arg2, sign, &overflow);
1043 break;
1045 case CEIL_DIV_EXPR:
1046 if (arg2 == 0)
1047 return NULL_TREE;
1048 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1049 break;
1051 case ROUND_DIV_EXPR:
1052 if (arg2 == 0)
1053 return NULL_TREE;
1054 res = wi::div_round (arg1, arg2, sign, &overflow);
1055 break;
1057 case TRUNC_MOD_EXPR:
1058 if (arg2 == 0)
1059 return NULL_TREE;
1060 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1061 break;
1063 case FLOOR_MOD_EXPR:
1064 if (arg2 == 0)
1065 return NULL_TREE;
1066 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1067 break;
1069 case CEIL_MOD_EXPR:
1070 if (arg2 == 0)
1071 return NULL_TREE;
1072 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1073 break;
1075 case ROUND_MOD_EXPR:
1076 if (arg2 == 0)
1077 return NULL_TREE;
1078 res = wi::mod_round (arg1, arg2, sign, &overflow);
1079 break;
1081 case MIN_EXPR:
1082 res = wi::min (arg1, arg2, sign);
1083 break;
1085 case MAX_EXPR:
1086 res = wi::max (arg1, arg2, sign);
1087 break;
1089 default:
1090 return NULL_TREE;
1093 t = force_fit_type (type, res, overflowable,
1094 (((sign == SIGNED || overflowable == -1)
1095 && overflow)
1096 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1098 return t;
1101 tree
1102 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1104 return int_const_binop_1 (code, arg1, arg2, 1);
1107 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1108 constant. We assume ARG1 and ARG2 have the same data type, or at least
1109 are the same kind of constant and the same machine mode. Return zero if
1110 combining the constants is not allowed in the current operating mode. */
1112 static tree
1113 const_binop (enum tree_code code, tree arg1, tree arg2)
1115 /* Sanity check for the recursive cases. */
1116 if (!arg1 || !arg2)
1117 return NULL_TREE;
1119 STRIP_NOPS (arg1);
1120 STRIP_NOPS (arg2);
1122 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1124 if (code == POINTER_PLUS_EXPR)
1125 return int_const_binop (PLUS_EXPR,
1126 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1128 return int_const_binop (code, arg1, arg2);
1131 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1133 machine_mode mode;
1134 REAL_VALUE_TYPE d1;
1135 REAL_VALUE_TYPE d2;
1136 REAL_VALUE_TYPE value;
1137 REAL_VALUE_TYPE result;
1138 bool inexact;
1139 tree t, type;
1141 /* The following codes are handled by real_arithmetic. */
1142 switch (code)
1144 case PLUS_EXPR:
1145 case MINUS_EXPR:
1146 case MULT_EXPR:
1147 case RDIV_EXPR:
1148 case MIN_EXPR:
1149 case MAX_EXPR:
1150 break;
1152 default:
1153 return NULL_TREE;
1156 d1 = TREE_REAL_CST (arg1);
1157 d2 = TREE_REAL_CST (arg2);
1159 type = TREE_TYPE (arg1);
1160 mode = TYPE_MODE (type);
1162 /* Don't perform operation if we honor signaling NaNs and
1163 either operand is a signaling NaN. */
1164 if (HONOR_SNANS (mode)
1165 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1166 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1167 return NULL_TREE;
1169 /* Don't perform operation if it would raise a division
1170 by zero exception. */
1171 if (code == RDIV_EXPR
1172 && real_equal (&d2, &dconst0)
1173 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1174 return NULL_TREE;
1176 /* If either operand is a NaN, just return it. Otherwise, set up
1177 for floating-point trap; we return an overflow. */
1178 if (REAL_VALUE_ISNAN (d1))
1180 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1181 is off. */
1182 d1.signalling = 0;
1183 t = build_real (type, d1);
1184 return t;
1186 else if (REAL_VALUE_ISNAN (d2))
1188 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1189 is off. */
1190 d2.signalling = 0;
1191 t = build_real (type, d2);
1192 return t;
1195 inexact = real_arithmetic (&value, code, &d1, &d2);
1196 real_convert (&result, mode, &value);
1198 /* Don't constant fold this floating point operation if
1199 the result has overflowed and flag_trapping_math. */
1200 if (flag_trapping_math
1201 && MODE_HAS_INFINITIES (mode)
1202 && REAL_VALUE_ISINF (result)
1203 && !REAL_VALUE_ISINF (d1)
1204 && !REAL_VALUE_ISINF (d2))
1205 return NULL_TREE;
1207 /* Don't constant fold this floating point operation if the
1208 result may dependent upon the run-time rounding mode and
1209 flag_rounding_math is set, or if GCC's software emulation
1210 is unable to accurately represent the result. */
1211 if ((flag_rounding_math
1212 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1213 && (inexact || !real_identical (&result, &value)))
1214 return NULL_TREE;
1216 t = build_real (type, result);
1218 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1219 return t;
1222 if (TREE_CODE (arg1) == FIXED_CST)
1224 FIXED_VALUE_TYPE f1;
1225 FIXED_VALUE_TYPE f2;
1226 FIXED_VALUE_TYPE result;
1227 tree t, type;
1228 int sat_p;
1229 bool overflow_p;
1231 /* The following codes are handled by fixed_arithmetic. */
1232 switch (code)
1234 case PLUS_EXPR:
1235 case MINUS_EXPR:
1236 case MULT_EXPR:
1237 case TRUNC_DIV_EXPR:
1238 if (TREE_CODE (arg2) != FIXED_CST)
1239 return NULL_TREE;
1240 f2 = TREE_FIXED_CST (arg2);
1241 break;
1243 case LSHIFT_EXPR:
1244 case RSHIFT_EXPR:
1246 if (TREE_CODE (arg2) != INTEGER_CST)
1247 return NULL_TREE;
1248 wide_int w2 = arg2;
1249 f2.data.high = w2.elt (1);
1250 f2.data.low = w2.elt (0);
1251 f2.mode = SImode;
1253 break;
1255 default:
1256 return NULL_TREE;
1259 f1 = TREE_FIXED_CST (arg1);
1260 type = TREE_TYPE (arg1);
1261 sat_p = TYPE_SATURATING (type);
1262 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1263 t = build_fixed (type, result);
1264 /* Propagate overflow flags. */
1265 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1266 TREE_OVERFLOW (t) = 1;
1267 return t;
1270 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1272 tree type = TREE_TYPE (arg1);
1273 tree r1 = TREE_REALPART (arg1);
1274 tree i1 = TREE_IMAGPART (arg1);
1275 tree r2 = TREE_REALPART (arg2);
1276 tree i2 = TREE_IMAGPART (arg2);
1277 tree real, imag;
1279 switch (code)
1281 case PLUS_EXPR:
1282 case MINUS_EXPR:
1283 real = const_binop (code, r1, r2);
1284 imag = const_binop (code, i1, i2);
1285 break;
1287 case MULT_EXPR:
1288 if (COMPLEX_FLOAT_TYPE_P (type))
1289 return do_mpc_arg2 (arg1, arg2, type,
1290 /* do_nonfinite= */ folding_initializer,
1291 mpc_mul);
1293 real = const_binop (MINUS_EXPR,
1294 const_binop (MULT_EXPR, r1, r2),
1295 const_binop (MULT_EXPR, i1, i2));
1296 imag = const_binop (PLUS_EXPR,
1297 const_binop (MULT_EXPR, r1, i2),
1298 const_binop (MULT_EXPR, i1, r2));
1299 break;
1301 case RDIV_EXPR:
1302 if (COMPLEX_FLOAT_TYPE_P (type))
1303 return do_mpc_arg2 (arg1, arg2, type,
1304 /* do_nonfinite= */ folding_initializer,
1305 mpc_div);
1306 /* Fallthru. */
1307 case TRUNC_DIV_EXPR:
1308 case CEIL_DIV_EXPR:
1309 case FLOOR_DIV_EXPR:
1310 case ROUND_DIV_EXPR:
1311 if (flag_complex_method == 0)
1313 /* Keep this algorithm in sync with
1314 tree-complex.c:expand_complex_div_straight().
1316 Expand complex division to scalars, straightforward algorithm.
1317 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1318 t = br*br + bi*bi
1320 tree magsquared
1321 = const_binop (PLUS_EXPR,
1322 const_binop (MULT_EXPR, r2, r2),
1323 const_binop (MULT_EXPR, i2, i2));
1324 tree t1
1325 = const_binop (PLUS_EXPR,
1326 const_binop (MULT_EXPR, r1, r2),
1327 const_binop (MULT_EXPR, i1, i2));
1328 tree t2
1329 = const_binop (MINUS_EXPR,
1330 const_binop (MULT_EXPR, i1, r2),
1331 const_binop (MULT_EXPR, r1, i2));
1333 real = const_binop (code, t1, magsquared);
1334 imag = const_binop (code, t2, magsquared);
1336 else
1338 /* Keep this algorithm in sync with
1339 tree-complex.c:expand_complex_div_wide().
1341 Expand complex division to scalars, modified algorithm to minimize
1342 overflow with wide input ranges. */
1343 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1344 fold_abs_const (r2, TREE_TYPE (type)),
1345 fold_abs_const (i2, TREE_TYPE (type)));
1347 if (integer_nonzerop (compare))
1349 /* In the TRUE branch, we compute
1350 ratio = br/bi;
1351 div = (br * ratio) + bi;
1352 tr = (ar * ratio) + ai;
1353 ti = (ai * ratio) - ar;
1354 tr = tr / div;
1355 ti = ti / div; */
1356 tree ratio = const_binop (code, r2, i2);
1357 tree div = const_binop (PLUS_EXPR, i2,
1358 const_binop (MULT_EXPR, r2, ratio));
1359 real = const_binop (MULT_EXPR, r1, ratio);
1360 real = const_binop (PLUS_EXPR, real, i1);
1361 real = const_binop (code, real, div);
1363 imag = const_binop (MULT_EXPR, i1, ratio);
1364 imag = const_binop (MINUS_EXPR, imag, r1);
1365 imag = const_binop (code, imag, div);
1367 else
1369 /* In the FALSE branch, we compute
1370 ratio = d/c;
1371 divisor = (d * ratio) + c;
1372 tr = (b * ratio) + a;
1373 ti = b - (a * ratio);
1374 tr = tr / div;
1375 ti = ti / div; */
1376 tree ratio = const_binop (code, i2, r2);
1377 tree div = const_binop (PLUS_EXPR, r2,
1378 const_binop (MULT_EXPR, i2, ratio));
1380 real = const_binop (MULT_EXPR, i1, ratio);
1381 real = const_binop (PLUS_EXPR, real, r1);
1382 real = const_binop (code, real, div);
1384 imag = const_binop (MULT_EXPR, r1, ratio);
1385 imag = const_binop (MINUS_EXPR, i1, imag);
1386 imag = const_binop (code, imag, div);
1389 break;
1391 default:
1392 return NULL_TREE;
1395 if (real && imag)
1396 return build_complex (type, real, imag);
1399 if (TREE_CODE (arg1) == VECTOR_CST
1400 && TREE_CODE (arg2) == VECTOR_CST)
1402 tree type = TREE_TYPE (arg1);
1403 int count = TYPE_VECTOR_SUBPARTS (type), i;
1404 tree *elts = XALLOCAVEC (tree, count);
1406 for (i = 0; i < count; i++)
1408 tree elem1 = VECTOR_CST_ELT (arg1, i);
1409 tree elem2 = VECTOR_CST_ELT (arg2, i);
1411 elts[i] = const_binop (code, elem1, elem2);
1413 /* It is possible that const_binop cannot handle the given
1414 code and return NULL_TREE */
1415 if (elts[i] == NULL_TREE)
1416 return NULL_TREE;
1419 return build_vector (type, elts);
1422 /* Shifts allow a scalar offset for a vector. */
1423 if (TREE_CODE (arg1) == VECTOR_CST
1424 && TREE_CODE (arg2) == INTEGER_CST)
1426 tree type = TREE_TYPE (arg1);
1427 int count = TYPE_VECTOR_SUBPARTS (type), i;
1428 tree *elts = XALLOCAVEC (tree, count);
1430 for (i = 0; i < count; i++)
1432 tree elem1 = VECTOR_CST_ELT (arg1, i);
1434 elts[i] = const_binop (code, elem1, arg2);
1436 /* It is possible that const_binop cannot handle the given
1437 code and return NULL_TREE. */
1438 if (elts[i] == NULL_TREE)
1439 return NULL_TREE;
1442 return build_vector (type, elts);
1444 return NULL_TREE;
1447 /* Overload that adds a TYPE parameter to be able to dispatch
1448 to fold_relational_const. */
1450 tree
1451 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1453 if (TREE_CODE_CLASS (code) == tcc_comparison)
1454 return fold_relational_const (code, type, arg1, arg2);
1456 /* ??? Until we make the const_binop worker take the type of the
1457 result as argument put those cases that need it here. */
1458 switch (code)
1460 case COMPLEX_EXPR:
1461 if ((TREE_CODE (arg1) == REAL_CST
1462 && TREE_CODE (arg2) == REAL_CST)
1463 || (TREE_CODE (arg1) == INTEGER_CST
1464 && TREE_CODE (arg2) == INTEGER_CST))
1465 return build_complex (type, arg1, arg2);
1466 return NULL_TREE;
1468 case VEC_PACK_TRUNC_EXPR:
1469 case VEC_PACK_FIX_TRUNC_EXPR:
1471 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1472 tree *elts;
1474 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1475 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1476 if (TREE_CODE (arg1) != VECTOR_CST
1477 || TREE_CODE (arg2) != VECTOR_CST)
1478 return NULL_TREE;
1480 elts = XALLOCAVEC (tree, nelts);
1481 if (!vec_cst_ctor_to_array (arg1, elts)
1482 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1483 return NULL_TREE;
1485 for (i = 0; i < nelts; i++)
1487 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1488 ? NOP_EXPR : FIX_TRUNC_EXPR,
1489 TREE_TYPE (type), elts[i]);
1490 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1491 return NULL_TREE;
1494 return build_vector (type, elts);
1497 case VEC_WIDEN_MULT_LO_EXPR:
1498 case VEC_WIDEN_MULT_HI_EXPR:
1499 case VEC_WIDEN_MULT_EVEN_EXPR:
1500 case VEC_WIDEN_MULT_ODD_EXPR:
1502 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1503 unsigned int out, ofs, scale;
1504 tree *elts;
1506 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1507 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1508 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1509 return NULL_TREE;
1511 elts = XALLOCAVEC (tree, nelts * 4);
1512 if (!vec_cst_ctor_to_array (arg1, elts)
1513 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1514 return NULL_TREE;
1516 if (code == VEC_WIDEN_MULT_LO_EXPR)
1517 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1518 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1519 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1520 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1521 scale = 1, ofs = 0;
1522 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1523 scale = 1, ofs = 1;
1525 for (out = 0; out < nelts; out++)
1527 unsigned int in1 = (out << scale) + ofs;
1528 unsigned int in2 = in1 + nelts * 2;
1529 tree t1, t2;
1531 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1532 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1534 if (t1 == NULL_TREE || t2 == NULL_TREE)
1535 return NULL_TREE;
1536 elts[out] = const_binop (MULT_EXPR, t1, t2);
1537 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1538 return NULL_TREE;
1541 return build_vector (type, elts);
1544 default:;
1547 if (TREE_CODE_CLASS (code) != tcc_binary)
1548 return NULL_TREE;
1550 /* Make sure type and arg0 have the same saturating flag. */
1551 gcc_checking_assert (TYPE_SATURATING (type)
1552 == TYPE_SATURATING (TREE_TYPE (arg1)));
1554 return const_binop (code, arg1, arg2);
1557 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1558 Return zero if computing the constants is not possible. */
1560 tree
1561 const_unop (enum tree_code code, tree type, tree arg0)
1563 /* Don't perform the operation, other than NEGATE and ABS, if
1564 flag_signaling_nans is on and the operand is a signaling NaN. */
1565 if (TREE_CODE (arg0) == REAL_CST
1566 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1567 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1568 && code != NEGATE_EXPR
1569 && code != ABS_EXPR)
1570 return NULL_TREE;
1572 switch (code)
1574 CASE_CONVERT:
1575 case FLOAT_EXPR:
1576 case FIX_TRUNC_EXPR:
1577 case FIXED_CONVERT_EXPR:
1578 return fold_convert_const (code, type, arg0);
1580 case ADDR_SPACE_CONVERT_EXPR:
1581 /* If the source address is 0, and the source address space
1582 cannot have a valid object at 0, fold to dest type null. */
1583 if (integer_zerop (arg0)
1584 && !(targetm.addr_space.zero_address_valid
1585 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1586 return fold_convert_const (code, type, arg0);
1587 break;
1589 case VIEW_CONVERT_EXPR:
1590 return fold_view_convert_expr (type, arg0);
1592 case NEGATE_EXPR:
1594 /* Can't call fold_negate_const directly here as that doesn't
1595 handle all cases and we might not be able to negate some
1596 constants. */
1597 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1598 if (tem && CONSTANT_CLASS_P (tem))
1599 return tem;
1600 break;
1603 case ABS_EXPR:
1604 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1605 return fold_abs_const (arg0, type);
1606 break;
1608 case CONJ_EXPR:
1609 if (TREE_CODE (arg0) == COMPLEX_CST)
1611 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1612 TREE_TYPE (type));
1613 return build_complex (type, TREE_REALPART (arg0), ipart);
1615 break;
1617 case BIT_NOT_EXPR:
1618 if (TREE_CODE (arg0) == INTEGER_CST)
1619 return fold_not_const (arg0, type);
1620 /* Perform BIT_NOT_EXPR on each element individually. */
1621 else if (TREE_CODE (arg0) == VECTOR_CST)
1623 tree *elements;
1624 tree elem;
1625 unsigned count = VECTOR_CST_NELTS (arg0), i;
1627 elements = XALLOCAVEC (tree, count);
1628 for (i = 0; i < count; i++)
1630 elem = VECTOR_CST_ELT (arg0, i);
1631 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1632 if (elem == NULL_TREE)
1633 break;
1634 elements[i] = elem;
1636 if (i == count)
1637 return build_vector (type, elements);
1639 break;
1641 case TRUTH_NOT_EXPR:
1642 if (TREE_CODE (arg0) == INTEGER_CST)
1643 return constant_boolean_node (integer_zerop (arg0), type);
1644 break;
1646 case REALPART_EXPR:
1647 if (TREE_CODE (arg0) == COMPLEX_CST)
1648 return fold_convert (type, TREE_REALPART (arg0));
1649 break;
1651 case IMAGPART_EXPR:
1652 if (TREE_CODE (arg0) == COMPLEX_CST)
1653 return fold_convert (type, TREE_IMAGPART (arg0));
1654 break;
1656 case VEC_UNPACK_LO_EXPR:
1657 case VEC_UNPACK_HI_EXPR:
1658 case VEC_UNPACK_FLOAT_LO_EXPR:
1659 case VEC_UNPACK_FLOAT_HI_EXPR:
1661 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1662 tree *elts;
1663 enum tree_code subcode;
1665 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1666 if (TREE_CODE (arg0) != VECTOR_CST)
1667 return NULL_TREE;
1669 elts = XALLOCAVEC (tree, nelts * 2);
1670 if (!vec_cst_ctor_to_array (arg0, elts))
1671 return NULL_TREE;
1673 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1674 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1675 elts += nelts;
1677 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1678 subcode = NOP_EXPR;
1679 else
1680 subcode = FLOAT_EXPR;
1682 for (i = 0; i < nelts; i++)
1684 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1685 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1686 return NULL_TREE;
1689 return build_vector (type, elts);
1692 case REDUC_MIN_EXPR:
1693 case REDUC_MAX_EXPR:
1694 case REDUC_PLUS_EXPR:
1696 unsigned int nelts, i;
1697 tree *elts;
1698 enum tree_code subcode;
1700 if (TREE_CODE (arg0) != VECTOR_CST)
1701 return NULL_TREE;
1702 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1704 elts = XALLOCAVEC (tree, nelts);
1705 if (!vec_cst_ctor_to_array (arg0, elts))
1706 return NULL_TREE;
1708 switch (code)
1710 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1711 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1712 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1713 default: gcc_unreachable ();
1716 for (i = 1; i < nelts; i++)
1718 elts[0] = const_binop (subcode, elts[0], elts[i]);
1719 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1720 return NULL_TREE;
1723 return elts[0];
1726 default:
1727 break;
1730 return NULL_TREE;
1733 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1734 indicates which particular sizetype to create. */
1736 tree
1737 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1739 return build_int_cst (sizetype_tab[(int) kind], number);
1742 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1743 is a tree code. The type of the result is taken from the operands.
1744 Both must be equivalent integer types, ala int_binop_types_match_p.
1745 If the operands are constant, so is the result. */
1747 tree
1748 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1750 tree type = TREE_TYPE (arg0);
1752 if (arg0 == error_mark_node || arg1 == error_mark_node)
1753 return error_mark_node;
1755 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1756 TREE_TYPE (arg1)));
1758 /* Handle the special case of two integer constants faster. */
1759 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1761 /* And some specific cases even faster than that. */
1762 if (code == PLUS_EXPR)
1764 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1765 return arg1;
1766 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1767 return arg0;
1769 else if (code == MINUS_EXPR)
1771 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1772 return arg0;
1774 else if (code == MULT_EXPR)
1776 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1777 return arg1;
1780 /* Handle general case of two integer constants. For sizetype
1781 constant calculations we always want to know about overflow,
1782 even in the unsigned case. */
1783 return int_const_binop_1 (code, arg0, arg1, -1);
1786 return fold_build2_loc (loc, code, type, arg0, arg1);
1789 /* Given two values, either both of sizetype or both of bitsizetype,
1790 compute the difference between the two values. Return the value
1791 in signed type corresponding to the type of the operands. */
1793 tree
1794 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1796 tree type = TREE_TYPE (arg0);
1797 tree ctype;
1799 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1800 TREE_TYPE (arg1)));
1802 /* If the type is already signed, just do the simple thing. */
1803 if (!TYPE_UNSIGNED (type))
1804 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1806 if (type == sizetype)
1807 ctype = ssizetype;
1808 else if (type == bitsizetype)
1809 ctype = sbitsizetype;
1810 else
1811 ctype = signed_type_for (type);
1813 /* If either operand is not a constant, do the conversions to the signed
1814 type and subtract. The hardware will do the right thing with any
1815 overflow in the subtraction. */
1816 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1817 return size_binop_loc (loc, MINUS_EXPR,
1818 fold_convert_loc (loc, ctype, arg0),
1819 fold_convert_loc (loc, ctype, arg1));
1821 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1822 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1823 overflow) and negate (which can't either). Special-case a result
1824 of zero while we're here. */
1825 if (tree_int_cst_equal (arg0, arg1))
1826 return build_int_cst (ctype, 0);
1827 else if (tree_int_cst_lt (arg1, arg0))
1828 return fold_convert_loc (loc, ctype,
1829 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1830 else
1831 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1832 fold_convert_loc (loc, ctype,
1833 size_binop_loc (loc,
1834 MINUS_EXPR,
1835 arg1, arg0)));
1838 /* A subroutine of fold_convert_const handling conversions of an
1839 INTEGER_CST to another integer type. */
1841 static tree
1842 fold_convert_const_int_from_int (tree type, const_tree arg1)
1844 /* Given an integer constant, make new constant with new type,
1845 appropriately sign-extended or truncated. Use widest_int
1846 so that any extension is done according ARG1's type. */
1847 return force_fit_type (type, wi::to_widest (arg1),
1848 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1849 TREE_OVERFLOW (arg1));
1852 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1853 to an integer type. */
1855 static tree
1856 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1858 bool overflow = false;
1859 tree t;
1861 /* The following code implements the floating point to integer
1862 conversion rules required by the Java Language Specification,
1863 that IEEE NaNs are mapped to zero and values that overflow
1864 the target precision saturate, i.e. values greater than
1865 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1866 are mapped to INT_MIN. These semantics are allowed by the
1867 C and C++ standards that simply state that the behavior of
1868 FP-to-integer conversion is unspecified upon overflow. */
1870 wide_int val;
1871 REAL_VALUE_TYPE r;
1872 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1874 switch (code)
1876 case FIX_TRUNC_EXPR:
1877 real_trunc (&r, VOIDmode, &x);
1878 break;
1880 default:
1881 gcc_unreachable ();
1884 /* If R is NaN, return zero and show we have an overflow. */
1885 if (REAL_VALUE_ISNAN (r))
1887 overflow = true;
1888 val = wi::zero (TYPE_PRECISION (type));
1891 /* See if R is less than the lower bound or greater than the
1892 upper bound. */
1894 if (! overflow)
1896 tree lt = TYPE_MIN_VALUE (type);
1897 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1898 if (real_less (&r, &l))
1900 overflow = true;
1901 val = lt;
1905 if (! overflow)
1907 tree ut = TYPE_MAX_VALUE (type);
1908 if (ut)
1910 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1911 if (real_less (&u, &r))
1913 overflow = true;
1914 val = ut;
1919 if (! overflow)
1920 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1922 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1923 return t;
1926 /* A subroutine of fold_convert_const handling conversions of a
1927 FIXED_CST to an integer type. */
1929 static tree
1930 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1932 tree t;
1933 double_int temp, temp_trunc;
1934 unsigned int mode;
1936 /* Right shift FIXED_CST to temp by fbit. */
1937 temp = TREE_FIXED_CST (arg1).data;
1938 mode = TREE_FIXED_CST (arg1).mode;
1939 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1941 temp = temp.rshift (GET_MODE_FBIT (mode),
1942 HOST_BITS_PER_DOUBLE_INT,
1943 SIGNED_FIXED_POINT_MODE_P (mode));
1945 /* Left shift temp to temp_trunc by fbit. */
1946 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1947 HOST_BITS_PER_DOUBLE_INT,
1948 SIGNED_FIXED_POINT_MODE_P (mode));
1950 else
1952 temp = double_int_zero;
1953 temp_trunc = double_int_zero;
1956 /* If FIXED_CST is negative, we need to round the value toward 0.
1957 By checking if the fractional bits are not zero to add 1 to temp. */
1958 if (SIGNED_FIXED_POINT_MODE_P (mode)
1959 && temp_trunc.is_negative ()
1960 && TREE_FIXED_CST (arg1).data != temp_trunc)
1961 temp += double_int_one;
1963 /* Given a fixed-point constant, make new constant with new type,
1964 appropriately sign-extended or truncated. */
1965 t = force_fit_type (type, temp, -1,
1966 (temp.is_negative ()
1967 && (TYPE_UNSIGNED (type)
1968 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1969 | TREE_OVERFLOW (arg1));
1971 return t;
1974 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1975 to another floating point type. */
1977 static tree
1978 fold_convert_const_real_from_real (tree type, const_tree arg1)
1980 REAL_VALUE_TYPE value;
1981 tree t;
1983 /* Don't perform the operation if flag_signaling_nans is on
1984 and the operand is a signaling NaN. */
1985 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1986 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1987 return NULL_TREE;
1989 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1990 t = build_real (type, value);
1992 /* If converting an infinity or NAN to a representation that doesn't
1993 have one, set the overflow bit so that we can produce some kind of
1994 error message at the appropriate point if necessary. It's not the
1995 most user-friendly message, but it's better than nothing. */
1996 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1997 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1998 TREE_OVERFLOW (t) = 1;
1999 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2000 && !MODE_HAS_NANS (TYPE_MODE (type)))
2001 TREE_OVERFLOW (t) = 1;
2002 /* Regular overflow, conversion produced an infinity in a mode that
2003 can't represent them. */
2004 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2005 && REAL_VALUE_ISINF (value)
2006 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2007 TREE_OVERFLOW (t) = 1;
2008 else
2009 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2010 return t;
2013 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2014 to a floating point type. */
2016 static tree
2017 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2019 REAL_VALUE_TYPE value;
2020 tree t;
2022 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2023 t = build_real (type, value);
2025 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2026 return t;
2029 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2030 to another fixed-point type. */
2032 static tree
2033 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2035 FIXED_VALUE_TYPE value;
2036 tree t;
2037 bool overflow_p;
2039 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2040 TYPE_SATURATING (type));
2041 t = build_fixed (type, value);
2043 /* Propagate overflow flags. */
2044 if (overflow_p | TREE_OVERFLOW (arg1))
2045 TREE_OVERFLOW (t) = 1;
2046 return t;
2049 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2050 to a fixed-point type. */
2052 static tree
2053 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2055 FIXED_VALUE_TYPE value;
2056 tree t;
2057 bool overflow_p;
2058 double_int di;
2060 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2062 di.low = TREE_INT_CST_ELT (arg1, 0);
2063 if (TREE_INT_CST_NUNITS (arg1) == 1)
2064 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2065 else
2066 di.high = TREE_INT_CST_ELT (arg1, 1);
2068 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2069 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2070 TYPE_SATURATING (type));
2071 t = build_fixed (type, value);
2073 /* Propagate overflow flags. */
2074 if (overflow_p | TREE_OVERFLOW (arg1))
2075 TREE_OVERFLOW (t) = 1;
2076 return t;
2079 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2080 to a fixed-point type. */
2082 static tree
2083 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2085 FIXED_VALUE_TYPE value;
2086 tree t;
2087 bool overflow_p;
2089 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2090 &TREE_REAL_CST (arg1),
2091 TYPE_SATURATING (type));
2092 t = build_fixed (type, value);
2094 /* Propagate overflow flags. */
2095 if (overflow_p | TREE_OVERFLOW (arg1))
2096 TREE_OVERFLOW (t) = 1;
2097 return t;
2100 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2101 type TYPE. If no simplification can be done return NULL_TREE. */
2103 static tree
2104 fold_convert_const (enum tree_code code, tree type, tree arg1)
2106 if (TREE_TYPE (arg1) == type)
2107 return arg1;
2109 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2110 || TREE_CODE (type) == OFFSET_TYPE)
2112 if (TREE_CODE (arg1) == INTEGER_CST)
2113 return fold_convert_const_int_from_int (type, arg1);
2114 else if (TREE_CODE (arg1) == REAL_CST)
2115 return fold_convert_const_int_from_real (code, type, arg1);
2116 else if (TREE_CODE (arg1) == FIXED_CST)
2117 return fold_convert_const_int_from_fixed (type, arg1);
2119 else if (TREE_CODE (type) == REAL_TYPE)
2121 if (TREE_CODE (arg1) == INTEGER_CST)
2122 return build_real_from_int_cst (type, arg1);
2123 else if (TREE_CODE (arg1) == REAL_CST)
2124 return fold_convert_const_real_from_real (type, arg1);
2125 else if (TREE_CODE (arg1) == FIXED_CST)
2126 return fold_convert_const_real_from_fixed (type, arg1);
2128 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2130 if (TREE_CODE (arg1) == FIXED_CST)
2131 return fold_convert_const_fixed_from_fixed (type, arg1);
2132 else if (TREE_CODE (arg1) == INTEGER_CST)
2133 return fold_convert_const_fixed_from_int (type, arg1);
2134 else if (TREE_CODE (arg1) == REAL_CST)
2135 return fold_convert_const_fixed_from_real (type, arg1);
2137 else if (TREE_CODE (type) == VECTOR_TYPE)
2139 if (TREE_CODE (arg1) == VECTOR_CST
2140 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2142 int len = TYPE_VECTOR_SUBPARTS (type);
2143 tree elttype = TREE_TYPE (type);
2144 tree *v = XALLOCAVEC (tree, len);
2145 for (int i = 0; i < len; ++i)
2147 tree elt = VECTOR_CST_ELT (arg1, i);
2148 tree cvt = fold_convert_const (code, elttype, elt);
2149 if (cvt == NULL_TREE)
2150 return NULL_TREE;
2151 v[i] = cvt;
2153 return build_vector (type, v);
2156 return NULL_TREE;
2159 /* Construct a vector of zero elements of vector type TYPE. */
2161 static tree
2162 build_zero_vector (tree type)
2164 tree t;
2166 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2167 return build_vector_from_val (type, t);
2170 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2172 bool
2173 fold_convertible_p (const_tree type, const_tree arg)
2175 tree orig = TREE_TYPE (arg);
2177 if (type == orig)
2178 return true;
2180 if (TREE_CODE (arg) == ERROR_MARK
2181 || TREE_CODE (type) == ERROR_MARK
2182 || TREE_CODE (orig) == ERROR_MARK)
2183 return false;
2185 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2186 return true;
2188 switch (TREE_CODE (type))
2190 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2191 case POINTER_TYPE: case REFERENCE_TYPE:
2192 case OFFSET_TYPE:
2193 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2194 || TREE_CODE (orig) == OFFSET_TYPE);
2196 case REAL_TYPE:
2197 case FIXED_POINT_TYPE:
2198 case VECTOR_TYPE:
2199 case VOID_TYPE:
2200 return TREE_CODE (type) == TREE_CODE (orig);
2202 default:
2203 return false;
2207 /* Convert expression ARG to type TYPE. Used by the middle-end for
2208 simple conversions in preference to calling the front-end's convert. */
2210 tree
2211 fold_convert_loc (location_t loc, tree type, tree arg)
2213 tree orig = TREE_TYPE (arg);
2214 tree tem;
2216 if (type == orig)
2217 return arg;
2219 if (TREE_CODE (arg) == ERROR_MARK
2220 || TREE_CODE (type) == ERROR_MARK
2221 || TREE_CODE (orig) == ERROR_MARK)
2222 return error_mark_node;
2224 switch (TREE_CODE (type))
2226 case POINTER_TYPE:
2227 case REFERENCE_TYPE:
2228 /* Handle conversions between pointers to different address spaces. */
2229 if (POINTER_TYPE_P (orig)
2230 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2231 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2232 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2233 /* fall through */
2235 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2236 case OFFSET_TYPE:
2237 if (TREE_CODE (arg) == INTEGER_CST)
2239 tem = fold_convert_const (NOP_EXPR, type, arg);
2240 if (tem != NULL_TREE)
2241 return tem;
2243 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2244 || TREE_CODE (orig) == OFFSET_TYPE)
2245 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2246 if (TREE_CODE (orig) == COMPLEX_TYPE)
2247 return fold_convert_loc (loc, type,
2248 fold_build1_loc (loc, REALPART_EXPR,
2249 TREE_TYPE (orig), arg));
2250 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2251 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2252 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2254 case REAL_TYPE:
2255 if (TREE_CODE (arg) == INTEGER_CST)
2257 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2258 if (tem != NULL_TREE)
2259 return tem;
2261 else if (TREE_CODE (arg) == REAL_CST)
2263 tem = fold_convert_const (NOP_EXPR, type, arg);
2264 if (tem != NULL_TREE)
2265 return tem;
2267 else if (TREE_CODE (arg) == FIXED_CST)
2269 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2270 if (tem != NULL_TREE)
2271 return tem;
2274 switch (TREE_CODE (orig))
2276 case INTEGER_TYPE:
2277 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2278 case POINTER_TYPE: case REFERENCE_TYPE:
2279 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2281 case REAL_TYPE:
2282 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2284 case FIXED_POINT_TYPE:
2285 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2287 case COMPLEX_TYPE:
2288 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2289 return fold_convert_loc (loc, type, tem);
2291 default:
2292 gcc_unreachable ();
2295 case FIXED_POINT_TYPE:
2296 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2297 || TREE_CODE (arg) == REAL_CST)
2299 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2300 if (tem != NULL_TREE)
2301 goto fold_convert_exit;
2304 switch (TREE_CODE (orig))
2306 case FIXED_POINT_TYPE:
2307 case INTEGER_TYPE:
2308 case ENUMERAL_TYPE:
2309 case BOOLEAN_TYPE:
2310 case REAL_TYPE:
2311 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2313 case COMPLEX_TYPE:
2314 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2315 return fold_convert_loc (loc, type, tem);
2317 default:
2318 gcc_unreachable ();
2321 case COMPLEX_TYPE:
2322 switch (TREE_CODE (orig))
2324 case INTEGER_TYPE:
2325 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2326 case POINTER_TYPE: case REFERENCE_TYPE:
2327 case REAL_TYPE:
2328 case FIXED_POINT_TYPE:
2329 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2330 fold_convert_loc (loc, TREE_TYPE (type), arg),
2331 fold_convert_loc (loc, TREE_TYPE (type),
2332 integer_zero_node));
2333 case COMPLEX_TYPE:
2335 tree rpart, ipart;
2337 if (TREE_CODE (arg) == COMPLEX_EXPR)
2339 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2340 TREE_OPERAND (arg, 0));
2341 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2342 TREE_OPERAND (arg, 1));
2343 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2346 arg = save_expr (arg);
2347 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2348 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2349 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2350 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2351 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2354 default:
2355 gcc_unreachable ();
2358 case VECTOR_TYPE:
2359 if (integer_zerop (arg))
2360 return build_zero_vector (type);
2361 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2362 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2363 || TREE_CODE (orig) == VECTOR_TYPE);
2364 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2366 case VOID_TYPE:
2367 tem = fold_ignored_result (arg);
2368 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2370 default:
2371 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2372 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2373 gcc_unreachable ();
2375 fold_convert_exit:
2376 protected_set_expr_location_unshare (tem, loc);
2377 return tem;
2380 /* Return false if expr can be assumed not to be an lvalue, true
2381 otherwise. */
2383 static bool
2384 maybe_lvalue_p (const_tree x)
2386 /* We only need to wrap lvalue tree codes. */
2387 switch (TREE_CODE (x))
2389 case VAR_DECL:
2390 case PARM_DECL:
2391 case RESULT_DECL:
2392 case LABEL_DECL:
2393 case FUNCTION_DECL:
2394 case SSA_NAME:
2396 case COMPONENT_REF:
2397 case MEM_REF:
2398 case INDIRECT_REF:
2399 case ARRAY_REF:
2400 case ARRAY_RANGE_REF:
2401 case BIT_FIELD_REF:
2402 case OBJ_TYPE_REF:
2404 case REALPART_EXPR:
2405 case IMAGPART_EXPR:
2406 case PREINCREMENT_EXPR:
2407 case PREDECREMENT_EXPR:
2408 case SAVE_EXPR:
2409 case TRY_CATCH_EXPR:
2410 case WITH_CLEANUP_EXPR:
2411 case COMPOUND_EXPR:
2412 case MODIFY_EXPR:
2413 case TARGET_EXPR:
2414 case COND_EXPR:
2415 case BIND_EXPR:
2416 break;
2418 default:
2419 /* Assume the worst for front-end tree codes. */
2420 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2421 break;
2422 return false;
2425 return true;
2428 /* Return an expr equal to X but certainly not valid as an lvalue. */
2430 tree
2431 non_lvalue_loc (location_t loc, tree x)
2433 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2434 us. */
2435 if (in_gimple_form)
2436 return x;
2438 if (! maybe_lvalue_p (x))
2439 return x;
2440 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2443 /* When pedantic, return an expr equal to X but certainly not valid as a
2444 pedantic lvalue. Otherwise, return X. */
2446 static tree
2447 pedantic_non_lvalue_loc (location_t loc, tree x)
2449 return protected_set_expr_location_unshare (x, loc);
2452 /* Given a tree comparison code, return the code that is the logical inverse.
2453 It is generally not safe to do this for floating-point comparisons, except
2454 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2455 ERROR_MARK in this case. */
2457 enum tree_code
2458 invert_tree_comparison (enum tree_code code, bool honor_nans)
2460 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2461 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2462 return ERROR_MARK;
2464 switch (code)
2466 case EQ_EXPR:
2467 return NE_EXPR;
2468 case NE_EXPR:
2469 return EQ_EXPR;
2470 case GT_EXPR:
2471 return honor_nans ? UNLE_EXPR : LE_EXPR;
2472 case GE_EXPR:
2473 return honor_nans ? UNLT_EXPR : LT_EXPR;
2474 case LT_EXPR:
2475 return honor_nans ? UNGE_EXPR : GE_EXPR;
2476 case LE_EXPR:
2477 return honor_nans ? UNGT_EXPR : GT_EXPR;
2478 case LTGT_EXPR:
2479 return UNEQ_EXPR;
2480 case UNEQ_EXPR:
2481 return LTGT_EXPR;
2482 case UNGT_EXPR:
2483 return LE_EXPR;
2484 case UNGE_EXPR:
2485 return LT_EXPR;
2486 case UNLT_EXPR:
2487 return GE_EXPR;
2488 case UNLE_EXPR:
2489 return GT_EXPR;
2490 case ORDERED_EXPR:
2491 return UNORDERED_EXPR;
2492 case UNORDERED_EXPR:
2493 return ORDERED_EXPR;
2494 default:
2495 gcc_unreachable ();
2499 /* Similar, but return the comparison that results if the operands are
2500 swapped. This is safe for floating-point. */
2502 enum tree_code
2503 swap_tree_comparison (enum tree_code code)
2505 switch (code)
2507 case EQ_EXPR:
2508 case NE_EXPR:
2509 case ORDERED_EXPR:
2510 case UNORDERED_EXPR:
2511 case LTGT_EXPR:
2512 case UNEQ_EXPR:
2513 return code;
2514 case GT_EXPR:
2515 return LT_EXPR;
2516 case GE_EXPR:
2517 return LE_EXPR;
2518 case LT_EXPR:
2519 return GT_EXPR;
2520 case LE_EXPR:
2521 return GE_EXPR;
2522 case UNGT_EXPR:
2523 return UNLT_EXPR;
2524 case UNGE_EXPR:
2525 return UNLE_EXPR;
2526 case UNLT_EXPR:
2527 return UNGT_EXPR;
2528 case UNLE_EXPR:
2529 return UNGE_EXPR;
2530 default:
2531 gcc_unreachable ();
2536 /* Convert a comparison tree code from an enum tree_code representation
2537 into a compcode bit-based encoding. This function is the inverse of
2538 compcode_to_comparison. */
2540 static enum comparison_code
2541 comparison_to_compcode (enum tree_code code)
2543 switch (code)
2545 case LT_EXPR:
2546 return COMPCODE_LT;
2547 case EQ_EXPR:
2548 return COMPCODE_EQ;
2549 case LE_EXPR:
2550 return COMPCODE_LE;
2551 case GT_EXPR:
2552 return COMPCODE_GT;
2553 case NE_EXPR:
2554 return COMPCODE_NE;
2555 case GE_EXPR:
2556 return COMPCODE_GE;
2557 case ORDERED_EXPR:
2558 return COMPCODE_ORD;
2559 case UNORDERED_EXPR:
2560 return COMPCODE_UNORD;
2561 case UNLT_EXPR:
2562 return COMPCODE_UNLT;
2563 case UNEQ_EXPR:
2564 return COMPCODE_UNEQ;
2565 case UNLE_EXPR:
2566 return COMPCODE_UNLE;
2567 case UNGT_EXPR:
2568 return COMPCODE_UNGT;
2569 case LTGT_EXPR:
2570 return COMPCODE_LTGT;
2571 case UNGE_EXPR:
2572 return COMPCODE_UNGE;
2573 default:
2574 gcc_unreachable ();
2578 /* Convert a compcode bit-based encoding of a comparison operator back
2579 to GCC's enum tree_code representation. This function is the
2580 inverse of comparison_to_compcode. */
2582 static enum tree_code
2583 compcode_to_comparison (enum comparison_code code)
2585 switch (code)
2587 case COMPCODE_LT:
2588 return LT_EXPR;
2589 case COMPCODE_EQ:
2590 return EQ_EXPR;
2591 case COMPCODE_LE:
2592 return LE_EXPR;
2593 case COMPCODE_GT:
2594 return GT_EXPR;
2595 case COMPCODE_NE:
2596 return NE_EXPR;
2597 case COMPCODE_GE:
2598 return GE_EXPR;
2599 case COMPCODE_ORD:
2600 return ORDERED_EXPR;
2601 case COMPCODE_UNORD:
2602 return UNORDERED_EXPR;
2603 case COMPCODE_UNLT:
2604 return UNLT_EXPR;
2605 case COMPCODE_UNEQ:
2606 return UNEQ_EXPR;
2607 case COMPCODE_UNLE:
2608 return UNLE_EXPR;
2609 case COMPCODE_UNGT:
2610 return UNGT_EXPR;
2611 case COMPCODE_LTGT:
2612 return LTGT_EXPR;
2613 case COMPCODE_UNGE:
2614 return UNGE_EXPR;
2615 default:
2616 gcc_unreachable ();
2620 /* Return a tree for the comparison which is the combination of
2621 doing the AND or OR (depending on CODE) of the two operations LCODE
2622 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2623 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2624 if this makes the transformation invalid. */
2626 tree
2627 combine_comparisons (location_t loc,
2628 enum tree_code code, enum tree_code lcode,
2629 enum tree_code rcode, tree truth_type,
2630 tree ll_arg, tree lr_arg)
2632 bool honor_nans = HONOR_NANS (ll_arg);
2633 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2634 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2635 int compcode;
2637 switch (code)
2639 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2640 compcode = lcompcode & rcompcode;
2641 break;
2643 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2644 compcode = lcompcode | rcompcode;
2645 break;
2647 default:
2648 return NULL_TREE;
2651 if (!honor_nans)
2653 /* Eliminate unordered comparisons, as well as LTGT and ORD
2654 which are not used unless the mode has NaNs. */
2655 compcode &= ~COMPCODE_UNORD;
2656 if (compcode == COMPCODE_LTGT)
2657 compcode = COMPCODE_NE;
2658 else if (compcode == COMPCODE_ORD)
2659 compcode = COMPCODE_TRUE;
2661 else if (flag_trapping_math)
2663 /* Check that the original operation and the optimized ones will trap
2664 under the same condition. */
2665 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2666 && (lcompcode != COMPCODE_EQ)
2667 && (lcompcode != COMPCODE_ORD);
2668 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2669 && (rcompcode != COMPCODE_EQ)
2670 && (rcompcode != COMPCODE_ORD);
2671 bool trap = (compcode & COMPCODE_UNORD) == 0
2672 && (compcode != COMPCODE_EQ)
2673 && (compcode != COMPCODE_ORD);
2675 /* In a short-circuited boolean expression the LHS might be
2676 such that the RHS, if evaluated, will never trap. For
2677 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2678 if neither x nor y is NaN. (This is a mixed blessing: for
2679 example, the expression above will never trap, hence
2680 optimizing it to x < y would be invalid). */
2681 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2682 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2683 rtrap = false;
2685 /* If the comparison was short-circuited, and only the RHS
2686 trapped, we may now generate a spurious trap. */
2687 if (rtrap && !ltrap
2688 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2689 return NULL_TREE;
2691 /* If we changed the conditions that cause a trap, we lose. */
2692 if ((ltrap || rtrap) != trap)
2693 return NULL_TREE;
2696 if (compcode == COMPCODE_TRUE)
2697 return constant_boolean_node (true, truth_type);
2698 else if (compcode == COMPCODE_FALSE)
2699 return constant_boolean_node (false, truth_type);
2700 else
2702 enum tree_code tcode;
2704 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2705 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2709 /* Return nonzero if two operands (typically of the same tree node)
2710 are necessarily equal. FLAGS modifies behavior as follows:
2712 If OEP_ONLY_CONST is set, only return nonzero for constants.
2713 This function tests whether the operands are indistinguishable;
2714 it does not test whether they are equal using C's == operation.
2715 The distinction is important for IEEE floating point, because
2716 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2717 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2719 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2720 even though it may hold multiple values during a function.
2721 This is because a GCC tree node guarantees that nothing else is
2722 executed between the evaluation of its "operands" (which may often
2723 be evaluated in arbitrary order). Hence if the operands themselves
2724 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2725 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2726 unset means assuming isochronic (or instantaneous) tree equivalence.
2727 Unless comparing arbitrary expression trees, such as from different
2728 statements, this flag can usually be left unset.
2730 If OEP_PURE_SAME is set, then pure functions with identical arguments
2731 are considered the same. It is used when the caller has other ways
2732 to ensure that global memory is unchanged in between.
2734 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2735 not values of expressions.
2737 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2738 any operand with side effect. This is unnecesarily conservative in the
2739 case we know that arg0 and arg1 are in disjoint code paths (such as in
2740 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2741 addresses with TREE_CONSTANT flag set so we know that &var == &var
2742 even if var is volatile. */
2745 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2747 /* When checking, verify at the outermost operand_equal_p call that
2748 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2749 hash value. */
2750 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2752 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2754 if (arg0 != arg1)
2756 inchash::hash hstate0 (0), hstate1 (0);
2757 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2758 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2759 hashval_t h0 = hstate0.end ();
2760 hashval_t h1 = hstate1.end ();
2761 gcc_assert (h0 == h1);
2763 return 1;
2765 else
2766 return 0;
2769 /* If either is ERROR_MARK, they aren't equal. */
2770 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2771 || TREE_TYPE (arg0) == error_mark_node
2772 || TREE_TYPE (arg1) == error_mark_node)
2773 return 0;
2775 /* Similar, if either does not have a type (like a released SSA name),
2776 they aren't equal. */
2777 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2778 return 0;
2780 /* We cannot consider pointers to different address space equal. */
2781 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2782 && POINTER_TYPE_P (TREE_TYPE (arg1))
2783 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2784 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2785 return 0;
2787 /* Check equality of integer constants before bailing out due to
2788 precision differences. */
2789 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2791 /* Address of INTEGER_CST is not defined; check that we did not forget
2792 to drop the OEP_ADDRESS_OF flags. */
2793 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2794 return tree_int_cst_equal (arg0, arg1);
2797 if (!(flags & OEP_ADDRESS_OF))
2799 /* If both types don't have the same signedness, then we can't consider
2800 them equal. We must check this before the STRIP_NOPS calls
2801 because they may change the signedness of the arguments. As pointers
2802 strictly don't have a signedness, require either two pointers or
2803 two non-pointers as well. */
2804 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2805 || POINTER_TYPE_P (TREE_TYPE (arg0))
2806 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2807 return 0;
2809 /* If both types don't have the same precision, then it is not safe
2810 to strip NOPs. */
2811 if (element_precision (TREE_TYPE (arg0))
2812 != element_precision (TREE_TYPE (arg1)))
2813 return 0;
2815 STRIP_NOPS (arg0);
2816 STRIP_NOPS (arg1);
2818 #if 0
2819 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2820 sanity check once the issue is solved. */
2821 else
2822 /* Addresses of conversions and SSA_NAMEs (and many other things)
2823 are not defined. Check that we did not forget to drop the
2824 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2825 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2826 && TREE_CODE (arg0) != SSA_NAME);
2827 #endif
2829 /* In case both args are comparisons but with different comparison
2830 code, try to swap the comparison operands of one arg to produce
2831 a match and compare that variant. */
2832 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2833 && COMPARISON_CLASS_P (arg0)
2834 && COMPARISON_CLASS_P (arg1))
2836 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2838 if (TREE_CODE (arg0) == swap_code)
2839 return operand_equal_p (TREE_OPERAND (arg0, 0),
2840 TREE_OPERAND (arg1, 1), flags)
2841 && operand_equal_p (TREE_OPERAND (arg0, 1),
2842 TREE_OPERAND (arg1, 0), flags);
2845 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2847 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2848 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2850 else if (flags & OEP_ADDRESS_OF)
2852 /* If we are interested in comparing addresses ignore
2853 MEM_REF wrappings of the base that can appear just for
2854 TBAA reasons. */
2855 if (TREE_CODE (arg0) == MEM_REF
2856 && DECL_P (arg1)
2857 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2858 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2859 && integer_zerop (TREE_OPERAND (arg0, 1)))
2860 return 1;
2861 else if (TREE_CODE (arg1) == MEM_REF
2862 && DECL_P (arg0)
2863 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2864 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2865 && integer_zerop (TREE_OPERAND (arg1, 1)))
2866 return 1;
2867 return 0;
2869 else
2870 return 0;
2873 /* When not checking adddresses, this is needed for conversions and for
2874 COMPONENT_REF. Might as well play it safe and always test this. */
2875 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2876 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2877 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2878 && !(flags & OEP_ADDRESS_OF)))
2879 return 0;
2881 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2882 We don't care about side effects in that case because the SAVE_EXPR
2883 takes care of that for us. In all other cases, two expressions are
2884 equal if they have no side effects. If we have two identical
2885 expressions with side effects that should be treated the same due
2886 to the only side effects being identical SAVE_EXPR's, that will
2887 be detected in the recursive calls below.
2888 If we are taking an invariant address of two identical objects
2889 they are necessarily equal as well. */
2890 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2891 && (TREE_CODE (arg0) == SAVE_EXPR
2892 || (flags & OEP_MATCH_SIDE_EFFECTS)
2893 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2894 return 1;
2896 /* Next handle constant cases, those for which we can return 1 even
2897 if ONLY_CONST is set. */
2898 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2899 switch (TREE_CODE (arg0))
2901 case INTEGER_CST:
2902 return tree_int_cst_equal (arg0, arg1);
2904 case FIXED_CST:
2905 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2906 TREE_FIXED_CST (arg1));
2908 case REAL_CST:
2909 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2910 return 1;
2913 if (!HONOR_SIGNED_ZEROS (arg0))
2915 /* If we do not distinguish between signed and unsigned zero,
2916 consider them equal. */
2917 if (real_zerop (arg0) && real_zerop (arg1))
2918 return 1;
2920 return 0;
2922 case VECTOR_CST:
2924 unsigned i;
2926 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2927 return 0;
2929 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2931 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2932 VECTOR_CST_ELT (arg1, i), flags))
2933 return 0;
2935 return 1;
2938 case COMPLEX_CST:
2939 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2940 flags)
2941 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2942 flags));
2944 case STRING_CST:
2945 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2946 && ! memcmp (TREE_STRING_POINTER (arg0),
2947 TREE_STRING_POINTER (arg1),
2948 TREE_STRING_LENGTH (arg0)));
2950 case ADDR_EXPR:
2951 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2952 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2953 flags | OEP_ADDRESS_OF
2954 | OEP_MATCH_SIDE_EFFECTS);
2955 case CONSTRUCTOR:
2956 /* In GIMPLE empty constructors are allowed in initializers of
2957 aggregates. */
2958 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2959 default:
2960 break;
2963 if (flags & OEP_ONLY_CONST)
2964 return 0;
2966 /* Define macros to test an operand from arg0 and arg1 for equality and a
2967 variant that allows null and views null as being different from any
2968 non-null value. In the latter case, if either is null, the both
2969 must be; otherwise, do the normal comparison. */
2970 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2971 TREE_OPERAND (arg1, N), flags)
2973 #define OP_SAME_WITH_NULL(N) \
2974 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2975 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2977 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2979 case tcc_unary:
2980 /* Two conversions are equal only if signedness and modes match. */
2981 switch (TREE_CODE (arg0))
2983 CASE_CONVERT:
2984 case FIX_TRUNC_EXPR:
2985 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2986 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2987 return 0;
2988 break;
2989 default:
2990 break;
2993 return OP_SAME (0);
2996 case tcc_comparison:
2997 case tcc_binary:
2998 if (OP_SAME (0) && OP_SAME (1))
2999 return 1;
3001 /* For commutative ops, allow the other order. */
3002 return (commutative_tree_code (TREE_CODE (arg0))
3003 && operand_equal_p (TREE_OPERAND (arg0, 0),
3004 TREE_OPERAND (arg1, 1), flags)
3005 && operand_equal_p (TREE_OPERAND (arg0, 1),
3006 TREE_OPERAND (arg1, 0), flags));
3008 case tcc_reference:
3009 /* If either of the pointer (or reference) expressions we are
3010 dereferencing contain a side effect, these cannot be equal,
3011 but their addresses can be. */
3012 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3013 && (TREE_SIDE_EFFECTS (arg0)
3014 || TREE_SIDE_EFFECTS (arg1)))
3015 return 0;
3017 switch (TREE_CODE (arg0))
3019 case INDIRECT_REF:
3020 if (!(flags & OEP_ADDRESS_OF)
3021 && (TYPE_ALIGN (TREE_TYPE (arg0))
3022 != TYPE_ALIGN (TREE_TYPE (arg1))))
3023 return 0;
3024 flags &= ~OEP_ADDRESS_OF;
3025 return OP_SAME (0);
3027 case IMAGPART_EXPR:
3028 /* Require the same offset. */
3029 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3030 TYPE_SIZE (TREE_TYPE (arg1)),
3031 flags & ~OEP_ADDRESS_OF))
3032 return 0;
3034 /* Fallthru. */
3035 case REALPART_EXPR:
3036 case VIEW_CONVERT_EXPR:
3037 return OP_SAME (0);
3039 case TARGET_MEM_REF:
3040 case MEM_REF:
3041 if (!(flags & OEP_ADDRESS_OF))
3043 /* Require equal access sizes */
3044 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3045 && (!TYPE_SIZE (TREE_TYPE (arg0))
3046 || !TYPE_SIZE (TREE_TYPE (arg1))
3047 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3048 TYPE_SIZE (TREE_TYPE (arg1)),
3049 flags)))
3050 return 0;
3051 /* Verify that access happens in similar types. */
3052 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3053 return 0;
3054 /* Verify that accesses are TBAA compatible. */
3055 if (!alias_ptr_types_compatible_p
3056 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3057 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3058 || (MR_DEPENDENCE_CLIQUE (arg0)
3059 != MR_DEPENDENCE_CLIQUE (arg1))
3060 || (MR_DEPENDENCE_BASE (arg0)
3061 != MR_DEPENDENCE_BASE (arg1)))
3062 return 0;
3063 /* Verify that alignment is compatible. */
3064 if (TYPE_ALIGN (TREE_TYPE (arg0))
3065 != TYPE_ALIGN (TREE_TYPE (arg1)))
3066 return 0;
3068 flags &= ~OEP_ADDRESS_OF;
3069 return (OP_SAME (0) && OP_SAME (1)
3070 /* TARGET_MEM_REF require equal extra operands. */
3071 && (TREE_CODE (arg0) != TARGET_MEM_REF
3072 || (OP_SAME_WITH_NULL (2)
3073 && OP_SAME_WITH_NULL (3)
3074 && OP_SAME_WITH_NULL (4))));
3076 case ARRAY_REF:
3077 case ARRAY_RANGE_REF:
3078 if (!OP_SAME (0))
3079 return 0;
3080 flags &= ~OEP_ADDRESS_OF;
3081 /* Compare the array index by value if it is constant first as we
3082 may have different types but same value here. */
3083 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3084 TREE_OPERAND (arg1, 1))
3085 || OP_SAME (1))
3086 && OP_SAME_WITH_NULL (2)
3087 && OP_SAME_WITH_NULL (3)
3088 /* Compare low bound and element size as with OEP_ADDRESS_OF
3089 we have to account for the offset of the ref. */
3090 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3091 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3092 || (operand_equal_p (array_ref_low_bound
3093 (CONST_CAST_TREE (arg0)),
3094 array_ref_low_bound
3095 (CONST_CAST_TREE (arg1)), flags)
3096 && operand_equal_p (array_ref_element_size
3097 (CONST_CAST_TREE (arg0)),
3098 array_ref_element_size
3099 (CONST_CAST_TREE (arg1)),
3100 flags))));
3102 case COMPONENT_REF:
3103 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3104 may be NULL when we're called to compare MEM_EXPRs. */
3105 if (!OP_SAME_WITH_NULL (0)
3106 || !OP_SAME (1))
3107 return 0;
3108 flags &= ~OEP_ADDRESS_OF;
3109 return OP_SAME_WITH_NULL (2);
3111 case BIT_FIELD_REF:
3112 if (!OP_SAME (0))
3113 return 0;
3114 flags &= ~OEP_ADDRESS_OF;
3115 return OP_SAME (1) && OP_SAME (2);
3117 default:
3118 return 0;
3121 case tcc_expression:
3122 switch (TREE_CODE (arg0))
3124 case ADDR_EXPR:
3125 /* Be sure we pass right ADDRESS_OF flag. */
3126 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3127 return operand_equal_p (TREE_OPERAND (arg0, 0),
3128 TREE_OPERAND (arg1, 0),
3129 flags | OEP_ADDRESS_OF);
3131 case TRUTH_NOT_EXPR:
3132 return OP_SAME (0);
3134 case TRUTH_ANDIF_EXPR:
3135 case TRUTH_ORIF_EXPR:
3136 return OP_SAME (0) && OP_SAME (1);
3138 case FMA_EXPR:
3139 case WIDEN_MULT_PLUS_EXPR:
3140 case WIDEN_MULT_MINUS_EXPR:
3141 if (!OP_SAME (2))
3142 return 0;
3143 /* The multiplcation operands are commutative. */
3144 /* FALLTHRU */
3146 case TRUTH_AND_EXPR:
3147 case TRUTH_OR_EXPR:
3148 case TRUTH_XOR_EXPR:
3149 if (OP_SAME (0) && OP_SAME (1))
3150 return 1;
3152 /* Otherwise take into account this is a commutative operation. */
3153 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3154 TREE_OPERAND (arg1, 1), flags)
3155 && operand_equal_p (TREE_OPERAND (arg0, 1),
3156 TREE_OPERAND (arg1, 0), flags));
3158 case COND_EXPR:
3159 if (! OP_SAME (1) || ! OP_SAME (2))
3160 return 0;
3161 flags &= ~OEP_ADDRESS_OF;
3162 return OP_SAME (0);
3164 case VEC_COND_EXPR:
3165 case DOT_PROD_EXPR:
3166 case BIT_INSERT_EXPR:
3167 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3169 default:
3170 return 0;
3173 case tcc_vl_exp:
3174 switch (TREE_CODE (arg0))
3176 case CALL_EXPR:
3177 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3178 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3179 /* If not both CALL_EXPRs are either internal or normal function
3180 functions, then they are not equal. */
3181 return 0;
3182 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3184 /* If the CALL_EXPRs call different internal functions, then they
3185 are not equal. */
3186 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3187 return 0;
3189 else
3191 /* If the CALL_EXPRs call different functions, then they are not
3192 equal. */
3193 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3194 flags))
3195 return 0;
3198 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3200 unsigned int cef = call_expr_flags (arg0);
3201 if (flags & OEP_PURE_SAME)
3202 cef &= ECF_CONST | ECF_PURE;
3203 else
3204 cef &= ECF_CONST;
3205 if (!cef)
3206 return 0;
3209 /* Now see if all the arguments are the same. */
3211 const_call_expr_arg_iterator iter0, iter1;
3212 const_tree a0, a1;
3213 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3214 a1 = first_const_call_expr_arg (arg1, &iter1);
3215 a0 && a1;
3216 a0 = next_const_call_expr_arg (&iter0),
3217 a1 = next_const_call_expr_arg (&iter1))
3218 if (! operand_equal_p (a0, a1, flags))
3219 return 0;
3221 /* If we get here and both argument lists are exhausted
3222 then the CALL_EXPRs are equal. */
3223 return ! (a0 || a1);
3225 default:
3226 return 0;
3229 case tcc_declaration:
3230 /* Consider __builtin_sqrt equal to sqrt. */
3231 return (TREE_CODE (arg0) == FUNCTION_DECL
3232 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3233 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3234 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3236 case tcc_exceptional:
3237 if (TREE_CODE (arg0) == CONSTRUCTOR)
3239 /* In GIMPLE constructors are used only to build vectors from
3240 elements. Individual elements in the constructor must be
3241 indexed in increasing order and form an initial sequence.
3243 We make no effort to compare constructors in generic.
3244 (see sem_variable::equals in ipa-icf which can do so for
3245 constants). */
3246 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3247 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3248 return 0;
3250 /* Be sure that vectors constructed have the same representation.
3251 We only tested element precision and modes to match.
3252 Vectors may be BLKmode and thus also check that the number of
3253 parts match. */
3254 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3255 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3256 return 0;
3258 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3259 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3260 unsigned int len = vec_safe_length (v0);
3262 if (len != vec_safe_length (v1))
3263 return 0;
3265 for (unsigned int i = 0; i < len; i++)
3267 constructor_elt *c0 = &(*v0)[i];
3268 constructor_elt *c1 = &(*v1)[i];
3270 if (!operand_equal_p (c0->value, c1->value, flags)
3271 /* In GIMPLE the indexes can be either NULL or matching i.
3272 Double check this so we won't get false
3273 positives for GENERIC. */
3274 || (c0->index
3275 && (TREE_CODE (c0->index) != INTEGER_CST
3276 || !compare_tree_int (c0->index, i)))
3277 || (c1->index
3278 && (TREE_CODE (c1->index) != INTEGER_CST
3279 || !compare_tree_int (c1->index, i))))
3280 return 0;
3282 return 1;
3284 return 0;
3286 default:
3287 return 0;
3290 #undef OP_SAME
3291 #undef OP_SAME_WITH_NULL
3294 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3295 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3297 When in doubt, return 0. */
3299 static int
3300 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3302 int unsignedp1, unsignedpo;
3303 tree primarg0, primarg1, primother;
3304 unsigned int correct_width;
3306 if (operand_equal_p (arg0, arg1, 0))
3307 return 1;
3309 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3310 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3311 return 0;
3313 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3314 and see if the inner values are the same. This removes any
3315 signedness comparison, which doesn't matter here. */
3316 primarg0 = arg0, primarg1 = arg1;
3317 STRIP_NOPS (primarg0);
3318 STRIP_NOPS (primarg1);
3319 if (operand_equal_p (primarg0, primarg1, 0))
3320 return 1;
3322 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3323 actual comparison operand, ARG0.
3325 First throw away any conversions to wider types
3326 already present in the operands. */
3328 primarg1 = get_narrower (arg1, &unsignedp1);
3329 primother = get_narrower (other, &unsignedpo);
3331 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3332 if (unsignedp1 == unsignedpo
3333 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3334 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3336 tree type = TREE_TYPE (arg0);
3338 /* Make sure shorter operand is extended the right way
3339 to match the longer operand. */
3340 primarg1 = fold_convert (signed_or_unsigned_type_for
3341 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3343 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3344 return 1;
3347 return 0;
3350 /* See if ARG is an expression that is either a comparison or is performing
3351 arithmetic on comparisons. The comparisons must only be comparing
3352 two different values, which will be stored in *CVAL1 and *CVAL2; if
3353 they are nonzero it means that some operands have already been found.
3354 No variables may be used anywhere else in the expression except in the
3355 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3356 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3358 If this is true, return 1. Otherwise, return zero. */
3360 static int
3361 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3363 enum tree_code code = TREE_CODE (arg);
3364 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3366 /* We can handle some of the tcc_expression cases here. */
3367 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3368 tclass = tcc_unary;
3369 else if (tclass == tcc_expression
3370 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3371 || code == COMPOUND_EXPR))
3372 tclass = tcc_binary;
3374 else if (tclass == tcc_expression && code == SAVE_EXPR
3375 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3377 /* If we've already found a CVAL1 or CVAL2, this expression is
3378 two complex to handle. */
3379 if (*cval1 || *cval2)
3380 return 0;
3382 tclass = tcc_unary;
3383 *save_p = 1;
3386 switch (tclass)
3388 case tcc_unary:
3389 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3391 case tcc_binary:
3392 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3393 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3394 cval1, cval2, save_p));
3396 case tcc_constant:
3397 return 1;
3399 case tcc_expression:
3400 if (code == COND_EXPR)
3401 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3402 cval1, cval2, save_p)
3403 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3404 cval1, cval2, save_p)
3405 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3406 cval1, cval2, save_p));
3407 return 0;
3409 case tcc_comparison:
3410 /* First see if we can handle the first operand, then the second. For
3411 the second operand, we know *CVAL1 can't be zero. It must be that
3412 one side of the comparison is each of the values; test for the
3413 case where this isn't true by failing if the two operands
3414 are the same. */
3416 if (operand_equal_p (TREE_OPERAND (arg, 0),
3417 TREE_OPERAND (arg, 1), 0))
3418 return 0;
3420 if (*cval1 == 0)
3421 *cval1 = TREE_OPERAND (arg, 0);
3422 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3424 else if (*cval2 == 0)
3425 *cval2 = TREE_OPERAND (arg, 0);
3426 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3428 else
3429 return 0;
3431 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3433 else if (*cval2 == 0)
3434 *cval2 = TREE_OPERAND (arg, 1);
3435 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3437 else
3438 return 0;
3440 return 1;
3442 default:
3443 return 0;
3447 /* ARG is a tree that is known to contain just arithmetic operations and
3448 comparisons. Evaluate the operations in the tree substituting NEW0 for
3449 any occurrence of OLD0 as an operand of a comparison and likewise for
3450 NEW1 and OLD1. */
3452 static tree
3453 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3454 tree old1, tree new1)
3456 tree type = TREE_TYPE (arg);
3457 enum tree_code code = TREE_CODE (arg);
3458 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3460 /* We can handle some of the tcc_expression cases here. */
3461 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3462 tclass = tcc_unary;
3463 else if (tclass == tcc_expression
3464 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3465 tclass = tcc_binary;
3467 switch (tclass)
3469 case tcc_unary:
3470 return fold_build1_loc (loc, code, type,
3471 eval_subst (loc, TREE_OPERAND (arg, 0),
3472 old0, new0, old1, new1));
3474 case tcc_binary:
3475 return fold_build2_loc (loc, code, type,
3476 eval_subst (loc, TREE_OPERAND (arg, 0),
3477 old0, new0, old1, new1),
3478 eval_subst (loc, TREE_OPERAND (arg, 1),
3479 old0, new0, old1, new1));
3481 case tcc_expression:
3482 switch (code)
3484 case SAVE_EXPR:
3485 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3486 old1, new1);
3488 case COMPOUND_EXPR:
3489 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3490 old1, new1);
3492 case COND_EXPR:
3493 return fold_build3_loc (loc, code, type,
3494 eval_subst (loc, TREE_OPERAND (arg, 0),
3495 old0, new0, old1, new1),
3496 eval_subst (loc, TREE_OPERAND (arg, 1),
3497 old0, new0, old1, new1),
3498 eval_subst (loc, TREE_OPERAND (arg, 2),
3499 old0, new0, old1, new1));
3500 default:
3501 break;
3503 /* Fall through - ??? */
3505 case tcc_comparison:
3507 tree arg0 = TREE_OPERAND (arg, 0);
3508 tree arg1 = TREE_OPERAND (arg, 1);
3510 /* We need to check both for exact equality and tree equality. The
3511 former will be true if the operand has a side-effect. In that
3512 case, we know the operand occurred exactly once. */
3514 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3515 arg0 = new0;
3516 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3517 arg0 = new1;
3519 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3520 arg1 = new0;
3521 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3522 arg1 = new1;
3524 return fold_build2_loc (loc, code, type, arg0, arg1);
3527 default:
3528 return arg;
3532 /* Return a tree for the case when the result of an expression is RESULT
3533 converted to TYPE and OMITTED was previously an operand of the expression
3534 but is now not needed (e.g., we folded OMITTED * 0).
3536 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3537 the conversion of RESULT to TYPE. */
3539 tree
3540 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3542 tree t = fold_convert_loc (loc, type, result);
3544 /* If the resulting operand is an empty statement, just return the omitted
3545 statement casted to void. */
3546 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3547 return build1_loc (loc, NOP_EXPR, void_type_node,
3548 fold_ignored_result (omitted));
3550 if (TREE_SIDE_EFFECTS (omitted))
3551 return build2_loc (loc, COMPOUND_EXPR, type,
3552 fold_ignored_result (omitted), t);
3554 return non_lvalue_loc (loc, t);
3557 /* Return a tree for the case when the result of an expression is RESULT
3558 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3559 of the expression but are now not needed.
3561 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3562 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3563 evaluated before OMITTED2. Otherwise, if neither has side effects,
3564 just do the conversion of RESULT to TYPE. */
3566 tree
3567 omit_two_operands_loc (location_t loc, tree type, tree result,
3568 tree omitted1, tree omitted2)
3570 tree t = fold_convert_loc (loc, type, result);
3572 if (TREE_SIDE_EFFECTS (omitted2))
3573 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3574 if (TREE_SIDE_EFFECTS (omitted1))
3575 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3577 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3581 /* Return a simplified tree node for the truth-negation of ARG. This
3582 never alters ARG itself. We assume that ARG is an operation that
3583 returns a truth value (0 or 1).
3585 FIXME: one would think we would fold the result, but it causes
3586 problems with the dominator optimizer. */
3588 static tree
3589 fold_truth_not_expr (location_t loc, tree arg)
3591 tree type = TREE_TYPE (arg);
3592 enum tree_code code = TREE_CODE (arg);
3593 location_t loc1, loc2;
3595 /* If this is a comparison, we can simply invert it, except for
3596 floating-point non-equality comparisons, in which case we just
3597 enclose a TRUTH_NOT_EXPR around what we have. */
3599 if (TREE_CODE_CLASS (code) == tcc_comparison)
3601 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3602 if (FLOAT_TYPE_P (op_type)
3603 && flag_trapping_math
3604 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3605 && code != NE_EXPR && code != EQ_EXPR)
3606 return NULL_TREE;
3608 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3609 if (code == ERROR_MARK)
3610 return NULL_TREE;
3612 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3613 TREE_OPERAND (arg, 1));
3614 if (TREE_NO_WARNING (arg))
3615 TREE_NO_WARNING (ret) = 1;
3616 return ret;
3619 switch (code)
3621 case INTEGER_CST:
3622 return constant_boolean_node (integer_zerop (arg), type);
3624 case TRUTH_AND_EXPR:
3625 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3626 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3627 return build2_loc (loc, TRUTH_OR_EXPR, type,
3628 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3629 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3631 case TRUTH_OR_EXPR:
3632 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3633 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3634 return build2_loc (loc, TRUTH_AND_EXPR, type,
3635 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3636 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3638 case TRUTH_XOR_EXPR:
3639 /* Here we can invert either operand. We invert the first operand
3640 unless the second operand is a TRUTH_NOT_EXPR in which case our
3641 result is the XOR of the first operand with the inside of the
3642 negation of the second operand. */
3644 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3645 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3646 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3647 else
3648 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3649 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3650 TREE_OPERAND (arg, 1));
3652 case TRUTH_ANDIF_EXPR:
3653 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3654 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3655 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3656 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3657 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3659 case TRUTH_ORIF_EXPR:
3660 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3661 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3662 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3663 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3664 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3666 case TRUTH_NOT_EXPR:
3667 return TREE_OPERAND (arg, 0);
3669 case COND_EXPR:
3671 tree arg1 = TREE_OPERAND (arg, 1);
3672 tree arg2 = TREE_OPERAND (arg, 2);
3674 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3675 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3677 /* A COND_EXPR may have a throw as one operand, which
3678 then has void type. Just leave void operands
3679 as they are. */
3680 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3681 VOID_TYPE_P (TREE_TYPE (arg1))
3682 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3683 VOID_TYPE_P (TREE_TYPE (arg2))
3684 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3687 case COMPOUND_EXPR:
3688 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3689 return build2_loc (loc, COMPOUND_EXPR, type,
3690 TREE_OPERAND (arg, 0),
3691 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3693 case NON_LVALUE_EXPR:
3694 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3695 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3697 CASE_CONVERT:
3698 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3699 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3701 /* fall through */
3703 case FLOAT_EXPR:
3704 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3705 return build1_loc (loc, TREE_CODE (arg), type,
3706 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3708 case BIT_AND_EXPR:
3709 if (!integer_onep (TREE_OPERAND (arg, 1)))
3710 return NULL_TREE;
3711 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3713 case SAVE_EXPR:
3714 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3716 case CLEANUP_POINT_EXPR:
3717 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3718 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3719 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3721 default:
3722 return NULL_TREE;
3726 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3727 assume that ARG is an operation that returns a truth value (0 or 1
3728 for scalars, 0 or -1 for vectors). Return the folded expression if
3729 folding is successful. Otherwise, return NULL_TREE. */
3731 static tree
3732 fold_invert_truthvalue (location_t loc, tree arg)
3734 tree type = TREE_TYPE (arg);
3735 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3736 ? BIT_NOT_EXPR
3737 : TRUTH_NOT_EXPR,
3738 type, arg);
3741 /* Return a simplified tree node for the truth-negation of ARG. This
3742 never alters ARG itself. We assume that ARG is an operation that
3743 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3745 tree
3746 invert_truthvalue_loc (location_t loc, tree arg)
3748 if (TREE_CODE (arg) == ERROR_MARK)
3749 return arg;
3751 tree type = TREE_TYPE (arg);
3752 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3753 ? BIT_NOT_EXPR
3754 : TRUTH_NOT_EXPR,
3755 type, arg);
3758 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3759 with code CODE. This optimization is unsafe. */
3760 static tree
3761 distribute_real_division (location_t loc, enum tree_code code, tree type,
3762 tree arg0, tree arg1)
3764 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3765 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3767 /* (A / C) +- (B / C) -> (A +- B) / C. */
3768 if (mul0 == mul1
3769 && operand_equal_p (TREE_OPERAND (arg0, 1),
3770 TREE_OPERAND (arg1, 1), 0))
3771 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3772 fold_build2_loc (loc, code, type,
3773 TREE_OPERAND (arg0, 0),
3774 TREE_OPERAND (arg1, 0)),
3775 TREE_OPERAND (arg0, 1));
3777 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3778 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3779 TREE_OPERAND (arg1, 0), 0)
3780 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3781 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3783 REAL_VALUE_TYPE r0, r1;
3784 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3785 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3786 if (!mul0)
3787 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3788 if (!mul1)
3789 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3790 real_arithmetic (&r0, code, &r0, &r1);
3791 return fold_build2_loc (loc, MULT_EXPR, type,
3792 TREE_OPERAND (arg0, 0),
3793 build_real (type, r0));
3796 return NULL_TREE;
3799 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3800 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3801 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3802 is the original memory reference used to preserve the alias set of
3803 the access. */
3805 static tree
3806 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3807 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3808 int unsignedp, int reversep)
3810 tree result, bftype;
3812 alias_set_type iset = get_alias_set (orig_inner);
3813 if (iset == 0 && get_alias_set (inner) != iset)
3814 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3815 build_fold_addr_expr (inner),
3816 build_int_cst (ptr_type_node, 0));
3818 if (bitpos == 0 && !reversep)
3820 tree size = TYPE_SIZE (TREE_TYPE (inner));
3821 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3822 || POINTER_TYPE_P (TREE_TYPE (inner)))
3823 && tree_fits_shwi_p (size)
3824 && tree_to_shwi (size) == bitsize)
3825 return fold_convert_loc (loc, type, inner);
3828 bftype = type;
3829 if (TYPE_PRECISION (bftype) != bitsize
3830 || TYPE_UNSIGNED (bftype) == !unsignedp)
3831 bftype = build_nonstandard_integer_type (bitsize, 0);
3833 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3834 size_int (bitsize), bitsize_int (bitpos));
3835 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3837 if (bftype != type)
3838 result = fold_convert_loc (loc, type, result);
3840 return result;
3843 /* Optimize a bit-field compare.
3845 There are two cases: First is a compare against a constant and the
3846 second is a comparison of two items where the fields are at the same
3847 bit position relative to the start of a chunk (byte, halfword, word)
3848 large enough to contain it. In these cases we can avoid the shift
3849 implicit in bitfield extractions.
3851 For constants, we emit a compare of the shifted constant with the
3852 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3853 compared. For two fields at the same position, we do the ANDs with the
3854 similar mask and compare the result of the ANDs.
3856 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3857 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3858 are the left and right operands of the comparison, respectively.
3860 If the optimization described above can be done, we return the resulting
3861 tree. Otherwise we return zero. */
3863 static tree
3864 optimize_bit_field_compare (location_t loc, enum tree_code code,
3865 tree compare_type, tree lhs, tree rhs)
3867 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3868 tree type = TREE_TYPE (lhs);
3869 tree unsigned_type;
3870 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3871 machine_mode lmode, rmode, nmode;
3872 int lunsignedp, runsignedp;
3873 int lreversep, rreversep;
3874 int lvolatilep = 0, rvolatilep = 0;
3875 tree linner, rinner = NULL_TREE;
3876 tree mask;
3877 tree offset;
3879 /* Get all the information about the extractions being done. If the bit size
3880 if the same as the size of the underlying object, we aren't doing an
3881 extraction at all and so can do nothing. We also don't want to
3882 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3883 then will no longer be able to replace it. */
3884 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3885 &lunsignedp, &lreversep, &lvolatilep);
3886 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3887 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3888 return 0;
3890 if (const_p)
3891 rreversep = lreversep;
3892 else
3894 /* If this is not a constant, we can only do something if bit positions,
3895 sizes, signedness and storage order are the same. */
3896 rinner
3897 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3898 &runsignedp, &rreversep, &rvolatilep);
3900 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3901 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3902 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3903 return 0;
3906 /* Honor the C++ memory model and mimic what RTL expansion does. */
3907 unsigned HOST_WIDE_INT bitstart = 0;
3908 unsigned HOST_WIDE_INT bitend = 0;
3909 if (TREE_CODE (lhs) == COMPONENT_REF)
3911 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3912 if (offset != NULL_TREE)
3913 return 0;
3916 /* See if we can find a mode to refer to this field. We should be able to,
3917 but fail if we can't. */
3918 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3919 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3920 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3921 TYPE_ALIGN (TREE_TYPE (rinner))),
3922 word_mode, false);
3923 if (nmode == VOIDmode)
3924 return 0;
3926 /* Set signed and unsigned types of the precision of this mode for the
3927 shifts below. */
3928 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3930 /* Compute the bit position and size for the new reference and our offset
3931 within it. If the new reference is the same size as the original, we
3932 won't optimize anything, so return zero. */
3933 nbitsize = GET_MODE_BITSIZE (nmode);
3934 nbitpos = lbitpos & ~ (nbitsize - 1);
3935 lbitpos -= nbitpos;
3936 if (nbitsize == lbitsize)
3937 return 0;
3939 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3940 lbitpos = nbitsize - lbitsize - lbitpos;
3942 /* Make the mask to be used against the extracted field. */
3943 mask = build_int_cst_type (unsigned_type, -1);
3944 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3945 mask = const_binop (RSHIFT_EXPR, mask,
3946 size_int (nbitsize - lbitsize - lbitpos));
3948 if (! const_p)
3949 /* If not comparing with constant, just rework the comparison
3950 and return. */
3951 return fold_build2_loc (loc, code, compare_type,
3952 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3953 make_bit_field_ref (loc, linner, lhs,
3954 unsigned_type,
3955 nbitsize, nbitpos,
3956 1, lreversep),
3957 mask),
3958 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3959 make_bit_field_ref (loc, rinner, rhs,
3960 unsigned_type,
3961 nbitsize, nbitpos,
3962 1, rreversep),
3963 mask));
3965 /* Otherwise, we are handling the constant case. See if the constant is too
3966 big for the field. Warn and return a tree for 0 (false) if so. We do
3967 this not only for its own sake, but to avoid having to test for this
3968 error case below. If we didn't, we might generate wrong code.
3970 For unsigned fields, the constant shifted right by the field length should
3971 be all zero. For signed fields, the high-order bits should agree with
3972 the sign bit. */
3974 if (lunsignedp)
3976 if (wi::lrshift (rhs, lbitsize) != 0)
3978 warning (0, "comparison is always %d due to width of bit-field",
3979 code == NE_EXPR);
3980 return constant_boolean_node (code == NE_EXPR, compare_type);
3983 else
3985 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3986 if (tem != 0 && tem != -1)
3988 warning (0, "comparison is always %d due to width of bit-field",
3989 code == NE_EXPR);
3990 return constant_boolean_node (code == NE_EXPR, compare_type);
3994 /* Single-bit compares should always be against zero. */
3995 if (lbitsize == 1 && ! integer_zerop (rhs))
3997 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3998 rhs = build_int_cst (type, 0);
4001 /* Make a new bitfield reference, shift the constant over the
4002 appropriate number of bits and mask it with the computed mask
4003 (in case this was a signed field). If we changed it, make a new one. */
4004 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4005 nbitsize, nbitpos, 1, lreversep);
4007 rhs = const_binop (BIT_AND_EXPR,
4008 const_binop (LSHIFT_EXPR,
4009 fold_convert_loc (loc, unsigned_type, rhs),
4010 size_int (lbitpos)),
4011 mask);
4013 lhs = build2_loc (loc, code, compare_type,
4014 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4015 return lhs;
4018 /* Subroutine for fold_truth_andor_1: decode a field reference.
4020 If EXP is a comparison reference, we return the innermost reference.
4022 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4023 set to the starting bit number.
4025 If the innermost field can be completely contained in a mode-sized
4026 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4028 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4029 otherwise it is not changed.
4031 *PUNSIGNEDP is set to the signedness of the field.
4033 *PREVERSEP is set to the storage order of the field.
4035 *PMASK is set to the mask used. This is either contained in a
4036 BIT_AND_EXPR or derived from the width of the field.
4038 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4040 Return 0 if this is not a component reference or is one that we can't
4041 do anything with. */
4043 static tree
4044 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4045 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4046 int *punsignedp, int *preversep, int *pvolatilep,
4047 tree *pmask, tree *pand_mask)
4049 tree exp = *exp_;
4050 tree outer_type = 0;
4051 tree and_mask = 0;
4052 tree mask, inner, offset;
4053 tree unsigned_type;
4054 unsigned int precision;
4056 /* All the optimizations using this function assume integer fields.
4057 There are problems with FP fields since the type_for_size call
4058 below can fail for, e.g., XFmode. */
4059 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4060 return 0;
4062 /* We are interested in the bare arrangement of bits, so strip everything
4063 that doesn't affect the machine mode. However, record the type of the
4064 outermost expression if it may matter below. */
4065 if (CONVERT_EXPR_P (exp)
4066 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4067 outer_type = TREE_TYPE (exp);
4068 STRIP_NOPS (exp);
4070 if (TREE_CODE (exp) == BIT_AND_EXPR)
4072 and_mask = TREE_OPERAND (exp, 1);
4073 exp = TREE_OPERAND (exp, 0);
4074 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4075 if (TREE_CODE (and_mask) != INTEGER_CST)
4076 return 0;
4079 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4080 punsignedp, preversep, pvolatilep);
4081 if ((inner == exp && and_mask == 0)
4082 || *pbitsize < 0 || offset != 0
4083 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4084 return 0;
4086 *exp_ = exp;
4088 /* If the number of bits in the reference is the same as the bitsize of
4089 the outer type, then the outer type gives the signedness. Otherwise
4090 (in case of a small bitfield) the signedness is unchanged. */
4091 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4092 *punsignedp = TYPE_UNSIGNED (outer_type);
4094 /* Compute the mask to access the bitfield. */
4095 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4096 precision = TYPE_PRECISION (unsigned_type);
4098 mask = build_int_cst_type (unsigned_type, -1);
4100 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4101 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4103 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4104 if (and_mask != 0)
4105 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4106 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4108 *pmask = mask;
4109 *pand_mask = and_mask;
4110 return inner;
4113 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4114 bit positions and MASK is SIGNED. */
4116 static int
4117 all_ones_mask_p (const_tree mask, unsigned int size)
4119 tree type = TREE_TYPE (mask);
4120 unsigned int precision = TYPE_PRECISION (type);
4122 /* If this function returns true when the type of the mask is
4123 UNSIGNED, then there will be errors. In particular see
4124 gcc.c-torture/execute/990326-1.c. There does not appear to be
4125 any documentation paper trail as to why this is so. But the pre
4126 wide-int worked with that restriction and it has been preserved
4127 here. */
4128 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4129 return false;
4131 return wi::mask (size, false, precision) == mask;
4134 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4135 represents the sign bit of EXP's type. If EXP represents a sign
4136 or zero extension, also test VAL against the unextended type.
4137 The return value is the (sub)expression whose sign bit is VAL,
4138 or NULL_TREE otherwise. */
4140 tree
4141 sign_bit_p (tree exp, const_tree val)
4143 int width;
4144 tree t;
4146 /* Tree EXP must have an integral type. */
4147 t = TREE_TYPE (exp);
4148 if (! INTEGRAL_TYPE_P (t))
4149 return NULL_TREE;
4151 /* Tree VAL must be an integer constant. */
4152 if (TREE_CODE (val) != INTEGER_CST
4153 || TREE_OVERFLOW (val))
4154 return NULL_TREE;
4156 width = TYPE_PRECISION (t);
4157 if (wi::only_sign_bit_p (val, width))
4158 return exp;
4160 /* Handle extension from a narrower type. */
4161 if (TREE_CODE (exp) == NOP_EXPR
4162 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4163 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4165 return NULL_TREE;
4168 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4169 to be evaluated unconditionally. */
4171 static int
4172 simple_operand_p (const_tree exp)
4174 /* Strip any conversions that don't change the machine mode. */
4175 STRIP_NOPS (exp);
4177 return (CONSTANT_CLASS_P (exp)
4178 || TREE_CODE (exp) == SSA_NAME
4179 || (DECL_P (exp)
4180 && ! TREE_ADDRESSABLE (exp)
4181 && ! TREE_THIS_VOLATILE (exp)
4182 && ! DECL_NONLOCAL (exp)
4183 /* Don't regard global variables as simple. They may be
4184 allocated in ways unknown to the compiler (shared memory,
4185 #pragma weak, etc). */
4186 && ! TREE_PUBLIC (exp)
4187 && ! DECL_EXTERNAL (exp)
4188 /* Weakrefs are not safe to be read, since they can be NULL.
4189 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4190 have DECL_WEAK flag set. */
4191 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4192 /* Loading a static variable is unduly expensive, but global
4193 registers aren't expensive. */
4194 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4197 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4198 to be evaluated unconditionally.
4199 I addition to simple_operand_p, we assume that comparisons, conversions,
4200 and logic-not operations are simple, if their operands are simple, too. */
4202 static bool
4203 simple_operand_p_2 (tree exp)
4205 enum tree_code code;
4207 if (TREE_SIDE_EFFECTS (exp)
4208 || tree_could_trap_p (exp))
4209 return false;
4211 while (CONVERT_EXPR_P (exp))
4212 exp = TREE_OPERAND (exp, 0);
4214 code = TREE_CODE (exp);
4216 if (TREE_CODE_CLASS (code) == tcc_comparison)
4217 return (simple_operand_p (TREE_OPERAND (exp, 0))
4218 && simple_operand_p (TREE_OPERAND (exp, 1)));
4220 if (code == TRUTH_NOT_EXPR)
4221 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4223 return simple_operand_p (exp);
4227 /* The following functions are subroutines to fold_range_test and allow it to
4228 try to change a logical combination of comparisons into a range test.
4230 For example, both
4231 X == 2 || X == 3 || X == 4 || X == 5
4233 X >= 2 && X <= 5
4234 are converted to
4235 (unsigned) (X - 2) <= 3
4237 We describe each set of comparisons as being either inside or outside
4238 a range, using a variable named like IN_P, and then describe the
4239 range with a lower and upper bound. If one of the bounds is omitted,
4240 it represents either the highest or lowest value of the type.
4242 In the comments below, we represent a range by two numbers in brackets
4243 preceded by a "+" to designate being inside that range, or a "-" to
4244 designate being outside that range, so the condition can be inverted by
4245 flipping the prefix. An omitted bound is represented by a "-". For
4246 example, "- [-, 10]" means being outside the range starting at the lowest
4247 possible value and ending at 10, in other words, being greater than 10.
4248 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4249 always false.
4251 We set up things so that the missing bounds are handled in a consistent
4252 manner so neither a missing bound nor "true" and "false" need to be
4253 handled using a special case. */
4255 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4256 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4257 and UPPER1_P are nonzero if the respective argument is an upper bound
4258 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4259 must be specified for a comparison. ARG1 will be converted to ARG0's
4260 type if both are specified. */
4262 static tree
4263 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4264 tree arg1, int upper1_p)
4266 tree tem;
4267 int result;
4268 int sgn0, sgn1;
4270 /* If neither arg represents infinity, do the normal operation.
4271 Else, if not a comparison, return infinity. Else handle the special
4272 comparison rules. Note that most of the cases below won't occur, but
4273 are handled for consistency. */
4275 if (arg0 != 0 && arg1 != 0)
4277 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4278 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4279 STRIP_NOPS (tem);
4280 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4283 if (TREE_CODE_CLASS (code) != tcc_comparison)
4284 return 0;
4286 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4287 for neither. In real maths, we cannot assume open ended ranges are
4288 the same. But, this is computer arithmetic, where numbers are finite.
4289 We can therefore make the transformation of any unbounded range with
4290 the value Z, Z being greater than any representable number. This permits
4291 us to treat unbounded ranges as equal. */
4292 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4293 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4294 switch (code)
4296 case EQ_EXPR:
4297 result = sgn0 == sgn1;
4298 break;
4299 case NE_EXPR:
4300 result = sgn0 != sgn1;
4301 break;
4302 case LT_EXPR:
4303 result = sgn0 < sgn1;
4304 break;
4305 case LE_EXPR:
4306 result = sgn0 <= sgn1;
4307 break;
4308 case GT_EXPR:
4309 result = sgn0 > sgn1;
4310 break;
4311 case GE_EXPR:
4312 result = sgn0 >= sgn1;
4313 break;
4314 default:
4315 gcc_unreachable ();
4318 return constant_boolean_node (result, type);
4321 /* Helper routine for make_range. Perform one step for it, return
4322 new expression if the loop should continue or NULL_TREE if it should
4323 stop. */
4325 tree
4326 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4327 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4328 bool *strict_overflow_p)
4330 tree arg0_type = TREE_TYPE (arg0);
4331 tree n_low, n_high, low = *p_low, high = *p_high;
4332 int in_p = *p_in_p, n_in_p;
4334 switch (code)
4336 case TRUTH_NOT_EXPR:
4337 /* We can only do something if the range is testing for zero. */
4338 if (low == NULL_TREE || high == NULL_TREE
4339 || ! integer_zerop (low) || ! integer_zerop (high))
4340 return NULL_TREE;
4341 *p_in_p = ! in_p;
4342 return arg0;
4344 case EQ_EXPR: case NE_EXPR:
4345 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4346 /* We can only do something if the range is testing for zero
4347 and if the second operand is an integer constant. Note that
4348 saying something is "in" the range we make is done by
4349 complementing IN_P since it will set in the initial case of
4350 being not equal to zero; "out" is leaving it alone. */
4351 if (low == NULL_TREE || high == NULL_TREE
4352 || ! integer_zerop (low) || ! integer_zerop (high)
4353 || TREE_CODE (arg1) != INTEGER_CST)
4354 return NULL_TREE;
4356 switch (code)
4358 case NE_EXPR: /* - [c, c] */
4359 low = high = arg1;
4360 break;
4361 case EQ_EXPR: /* + [c, c] */
4362 in_p = ! in_p, low = high = arg1;
4363 break;
4364 case GT_EXPR: /* - [-, c] */
4365 low = 0, high = arg1;
4366 break;
4367 case GE_EXPR: /* + [c, -] */
4368 in_p = ! in_p, low = arg1, high = 0;
4369 break;
4370 case LT_EXPR: /* - [c, -] */
4371 low = arg1, high = 0;
4372 break;
4373 case LE_EXPR: /* + [-, c] */
4374 in_p = ! in_p, low = 0, high = arg1;
4375 break;
4376 default:
4377 gcc_unreachable ();
4380 /* If this is an unsigned comparison, we also know that EXP is
4381 greater than or equal to zero. We base the range tests we make
4382 on that fact, so we record it here so we can parse existing
4383 range tests. We test arg0_type since often the return type
4384 of, e.g. EQ_EXPR, is boolean. */
4385 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4387 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4388 in_p, low, high, 1,
4389 build_int_cst (arg0_type, 0),
4390 NULL_TREE))
4391 return NULL_TREE;
4393 in_p = n_in_p, low = n_low, high = n_high;
4395 /* If the high bound is missing, but we have a nonzero low
4396 bound, reverse the range so it goes from zero to the low bound
4397 minus 1. */
4398 if (high == 0 && low && ! integer_zerop (low))
4400 in_p = ! in_p;
4401 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4402 build_int_cst (TREE_TYPE (low), 1), 0);
4403 low = build_int_cst (arg0_type, 0);
4407 *p_low = low;
4408 *p_high = high;
4409 *p_in_p = in_p;
4410 return arg0;
4412 case NEGATE_EXPR:
4413 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4414 low and high are non-NULL, then normalize will DTRT. */
4415 if (!TYPE_UNSIGNED (arg0_type)
4416 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4418 if (low == NULL_TREE)
4419 low = TYPE_MIN_VALUE (arg0_type);
4420 if (high == NULL_TREE)
4421 high = TYPE_MAX_VALUE (arg0_type);
4424 /* (-x) IN [a,b] -> x in [-b, -a] */
4425 n_low = range_binop (MINUS_EXPR, exp_type,
4426 build_int_cst (exp_type, 0),
4427 0, high, 1);
4428 n_high = range_binop (MINUS_EXPR, exp_type,
4429 build_int_cst (exp_type, 0),
4430 0, low, 0);
4431 if (n_high != 0 && TREE_OVERFLOW (n_high))
4432 return NULL_TREE;
4433 goto normalize;
4435 case BIT_NOT_EXPR:
4436 /* ~ X -> -X - 1 */
4437 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4438 build_int_cst (exp_type, 1));
4440 case PLUS_EXPR:
4441 case MINUS_EXPR:
4442 if (TREE_CODE (arg1) != INTEGER_CST)
4443 return NULL_TREE;
4445 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4446 move a constant to the other side. */
4447 if (!TYPE_UNSIGNED (arg0_type)
4448 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4449 return NULL_TREE;
4451 /* If EXP is signed, any overflow in the computation is undefined,
4452 so we don't worry about it so long as our computations on
4453 the bounds don't overflow. For unsigned, overflow is defined
4454 and this is exactly the right thing. */
4455 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4456 arg0_type, low, 0, arg1, 0);
4457 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4458 arg0_type, high, 1, arg1, 0);
4459 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4460 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4461 return NULL_TREE;
4463 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4464 *strict_overflow_p = true;
4466 normalize:
4467 /* Check for an unsigned range which has wrapped around the maximum
4468 value thus making n_high < n_low, and normalize it. */
4469 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4471 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4472 build_int_cst (TREE_TYPE (n_high), 1), 0);
4473 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4474 build_int_cst (TREE_TYPE (n_low), 1), 0);
4476 /* If the range is of the form +/- [ x+1, x ], we won't
4477 be able to normalize it. But then, it represents the
4478 whole range or the empty set, so make it
4479 +/- [ -, - ]. */
4480 if (tree_int_cst_equal (n_low, low)
4481 && tree_int_cst_equal (n_high, high))
4482 low = high = 0;
4483 else
4484 in_p = ! in_p;
4486 else
4487 low = n_low, high = n_high;
4489 *p_low = low;
4490 *p_high = high;
4491 *p_in_p = in_p;
4492 return arg0;
4494 CASE_CONVERT:
4495 case NON_LVALUE_EXPR:
4496 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4497 return NULL_TREE;
4499 if (! INTEGRAL_TYPE_P (arg0_type)
4500 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4501 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4502 return NULL_TREE;
4504 n_low = low, n_high = high;
4506 if (n_low != 0)
4507 n_low = fold_convert_loc (loc, arg0_type, n_low);
4509 if (n_high != 0)
4510 n_high = fold_convert_loc (loc, arg0_type, n_high);
4512 /* If we're converting arg0 from an unsigned type, to exp,
4513 a signed type, we will be doing the comparison as unsigned.
4514 The tests above have already verified that LOW and HIGH
4515 are both positive.
4517 So we have to ensure that we will handle large unsigned
4518 values the same way that the current signed bounds treat
4519 negative values. */
4521 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4523 tree high_positive;
4524 tree equiv_type;
4525 /* For fixed-point modes, we need to pass the saturating flag
4526 as the 2nd parameter. */
4527 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4528 equiv_type
4529 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4530 TYPE_SATURATING (arg0_type));
4531 else
4532 equiv_type
4533 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4535 /* A range without an upper bound is, naturally, unbounded.
4536 Since convert would have cropped a very large value, use
4537 the max value for the destination type. */
4538 high_positive
4539 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4540 : TYPE_MAX_VALUE (arg0_type);
4542 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4543 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4544 fold_convert_loc (loc, arg0_type,
4545 high_positive),
4546 build_int_cst (arg0_type, 1));
4548 /* If the low bound is specified, "and" the range with the
4549 range for which the original unsigned value will be
4550 positive. */
4551 if (low != 0)
4553 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4554 1, fold_convert_loc (loc, arg0_type,
4555 integer_zero_node),
4556 high_positive))
4557 return NULL_TREE;
4559 in_p = (n_in_p == in_p);
4561 else
4563 /* Otherwise, "or" the range with the range of the input
4564 that will be interpreted as negative. */
4565 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4566 1, fold_convert_loc (loc, arg0_type,
4567 integer_zero_node),
4568 high_positive))
4569 return NULL_TREE;
4571 in_p = (in_p != n_in_p);
4575 *p_low = n_low;
4576 *p_high = n_high;
4577 *p_in_p = in_p;
4578 return arg0;
4580 default:
4581 return NULL_TREE;
4585 /* Given EXP, a logical expression, set the range it is testing into
4586 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4587 actually being tested. *PLOW and *PHIGH will be made of the same
4588 type as the returned expression. If EXP is not a comparison, we
4589 will most likely not be returning a useful value and range. Set
4590 *STRICT_OVERFLOW_P to true if the return value is only valid
4591 because signed overflow is undefined; otherwise, do not change
4592 *STRICT_OVERFLOW_P. */
4594 tree
4595 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4596 bool *strict_overflow_p)
4598 enum tree_code code;
4599 tree arg0, arg1 = NULL_TREE;
4600 tree exp_type, nexp;
4601 int in_p;
4602 tree low, high;
4603 location_t loc = EXPR_LOCATION (exp);
4605 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4606 and see if we can refine the range. Some of the cases below may not
4607 happen, but it doesn't seem worth worrying about this. We "continue"
4608 the outer loop when we've changed something; otherwise we "break"
4609 the switch, which will "break" the while. */
4611 in_p = 0;
4612 low = high = build_int_cst (TREE_TYPE (exp), 0);
4614 while (1)
4616 code = TREE_CODE (exp);
4617 exp_type = TREE_TYPE (exp);
4618 arg0 = NULL_TREE;
4620 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4622 if (TREE_OPERAND_LENGTH (exp) > 0)
4623 arg0 = TREE_OPERAND (exp, 0);
4624 if (TREE_CODE_CLASS (code) == tcc_binary
4625 || TREE_CODE_CLASS (code) == tcc_comparison
4626 || (TREE_CODE_CLASS (code) == tcc_expression
4627 && TREE_OPERAND_LENGTH (exp) > 1))
4628 arg1 = TREE_OPERAND (exp, 1);
4630 if (arg0 == NULL_TREE)
4631 break;
4633 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4634 &high, &in_p, strict_overflow_p);
4635 if (nexp == NULL_TREE)
4636 break;
4637 exp = nexp;
4640 /* If EXP is a constant, we can evaluate whether this is true or false. */
4641 if (TREE_CODE (exp) == INTEGER_CST)
4643 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4644 exp, 0, low, 0))
4645 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4646 exp, 1, high, 1)));
4647 low = high = 0;
4648 exp = 0;
4651 *pin_p = in_p, *plow = low, *phigh = high;
4652 return exp;
4655 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4656 type, TYPE, return an expression to test if EXP is in (or out of, depending
4657 on IN_P) the range. Return 0 if the test couldn't be created. */
4659 tree
4660 build_range_check (location_t loc, tree type, tree exp, int in_p,
4661 tree low, tree high)
4663 tree etype = TREE_TYPE (exp), value;
4665 /* Disable this optimization for function pointer expressions
4666 on targets that require function pointer canonicalization. */
4667 if (targetm.have_canonicalize_funcptr_for_compare ()
4668 && TREE_CODE (etype) == POINTER_TYPE
4669 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4670 return NULL_TREE;
4672 if (! in_p)
4674 value = build_range_check (loc, type, exp, 1, low, high);
4675 if (value != 0)
4676 return invert_truthvalue_loc (loc, value);
4678 return 0;
4681 if (low == 0 && high == 0)
4682 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4684 if (low == 0)
4685 return fold_build2_loc (loc, LE_EXPR, type, exp,
4686 fold_convert_loc (loc, etype, high));
4688 if (high == 0)
4689 return fold_build2_loc (loc, GE_EXPR, type, exp,
4690 fold_convert_loc (loc, etype, low));
4692 if (operand_equal_p (low, high, 0))
4693 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4694 fold_convert_loc (loc, etype, low));
4696 if (integer_zerop (low))
4698 if (! TYPE_UNSIGNED (etype))
4700 etype = unsigned_type_for (etype);
4701 high = fold_convert_loc (loc, etype, high);
4702 exp = fold_convert_loc (loc, etype, exp);
4704 return build_range_check (loc, type, exp, 1, 0, high);
4707 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4708 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4710 int prec = TYPE_PRECISION (etype);
4712 if (wi::mask (prec - 1, false, prec) == high)
4714 if (TYPE_UNSIGNED (etype))
4716 tree signed_etype = signed_type_for (etype);
4717 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4718 etype
4719 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4720 else
4721 etype = signed_etype;
4722 exp = fold_convert_loc (loc, etype, exp);
4724 return fold_build2_loc (loc, GT_EXPR, type, exp,
4725 build_int_cst (etype, 0));
4729 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4730 This requires wrap-around arithmetics for the type of the expression.
4731 First make sure that arithmetics in this type is valid, then make sure
4732 that it wraps around. */
4733 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4734 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4735 TYPE_UNSIGNED (etype));
4737 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4739 tree utype, minv, maxv;
4741 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4742 for the type in question, as we rely on this here. */
4743 utype = unsigned_type_for (etype);
4744 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4745 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4746 build_int_cst (TREE_TYPE (maxv), 1), 1);
4747 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4749 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4750 minv, 1, maxv, 1)))
4751 etype = utype;
4752 else
4753 return 0;
4756 high = fold_convert_loc (loc, etype, high);
4757 low = fold_convert_loc (loc, etype, low);
4758 exp = fold_convert_loc (loc, etype, exp);
4760 value = const_binop (MINUS_EXPR, high, low);
4763 if (POINTER_TYPE_P (etype))
4765 if (value != 0 && !TREE_OVERFLOW (value))
4767 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4768 return build_range_check (loc, type,
4769 fold_build_pointer_plus_loc (loc, exp, low),
4770 1, build_int_cst (etype, 0), value);
4772 return 0;
4775 if (value != 0 && !TREE_OVERFLOW (value))
4776 return build_range_check (loc, type,
4777 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4778 1, build_int_cst (etype, 0), value);
4780 return 0;
4783 /* Return the predecessor of VAL in its type, handling the infinite case. */
4785 static tree
4786 range_predecessor (tree val)
4788 tree type = TREE_TYPE (val);
4790 if (INTEGRAL_TYPE_P (type)
4791 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4792 return 0;
4793 else
4794 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4795 build_int_cst (TREE_TYPE (val), 1), 0);
4798 /* Return the successor of VAL in its type, handling the infinite case. */
4800 static tree
4801 range_successor (tree val)
4803 tree type = TREE_TYPE (val);
4805 if (INTEGRAL_TYPE_P (type)
4806 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4807 return 0;
4808 else
4809 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4810 build_int_cst (TREE_TYPE (val), 1), 0);
4813 /* Given two ranges, see if we can merge them into one. Return 1 if we
4814 can, 0 if we can't. Set the output range into the specified parameters. */
4816 bool
4817 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4818 tree high0, int in1_p, tree low1, tree high1)
4820 int no_overlap;
4821 int subset;
4822 int temp;
4823 tree tem;
4824 int in_p;
4825 tree low, high;
4826 int lowequal = ((low0 == 0 && low1 == 0)
4827 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4828 low0, 0, low1, 0)));
4829 int highequal = ((high0 == 0 && high1 == 0)
4830 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4831 high0, 1, high1, 1)));
4833 /* Make range 0 be the range that starts first, or ends last if they
4834 start at the same value. Swap them if it isn't. */
4835 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4836 low0, 0, low1, 0))
4837 || (lowequal
4838 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4839 high1, 1, high0, 1))))
4841 temp = in0_p, in0_p = in1_p, in1_p = temp;
4842 tem = low0, low0 = low1, low1 = tem;
4843 tem = high0, high0 = high1, high1 = tem;
4846 /* Now flag two cases, whether the ranges are disjoint or whether the
4847 second range is totally subsumed in the first. Note that the tests
4848 below are simplified by the ones above. */
4849 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4850 high0, 1, low1, 0));
4851 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4852 high1, 1, high0, 1));
4854 /* We now have four cases, depending on whether we are including or
4855 excluding the two ranges. */
4856 if (in0_p && in1_p)
4858 /* If they don't overlap, the result is false. If the second range
4859 is a subset it is the result. Otherwise, the range is from the start
4860 of the second to the end of the first. */
4861 if (no_overlap)
4862 in_p = 0, low = high = 0;
4863 else if (subset)
4864 in_p = 1, low = low1, high = high1;
4865 else
4866 in_p = 1, low = low1, high = high0;
4869 else if (in0_p && ! in1_p)
4871 /* If they don't overlap, the result is the first range. If they are
4872 equal, the result is false. If the second range is a subset of the
4873 first, and the ranges begin at the same place, we go from just after
4874 the end of the second range to the end of the first. If the second
4875 range is not a subset of the first, or if it is a subset and both
4876 ranges end at the same place, the range starts at the start of the
4877 first range and ends just before the second range.
4878 Otherwise, we can't describe this as a single range. */
4879 if (no_overlap)
4880 in_p = 1, low = low0, high = high0;
4881 else if (lowequal && highequal)
4882 in_p = 0, low = high = 0;
4883 else if (subset && lowequal)
4885 low = range_successor (high1);
4886 high = high0;
4887 in_p = 1;
4888 if (low == 0)
4890 /* We are in the weird situation where high0 > high1 but
4891 high1 has no successor. Punt. */
4892 return 0;
4895 else if (! subset || highequal)
4897 low = low0;
4898 high = range_predecessor (low1);
4899 in_p = 1;
4900 if (high == 0)
4902 /* low0 < low1 but low1 has no predecessor. Punt. */
4903 return 0;
4906 else
4907 return 0;
4910 else if (! in0_p && in1_p)
4912 /* If they don't overlap, the result is the second range. If the second
4913 is a subset of the first, the result is false. Otherwise,
4914 the range starts just after the first range and ends at the
4915 end of the second. */
4916 if (no_overlap)
4917 in_p = 1, low = low1, high = high1;
4918 else if (subset || highequal)
4919 in_p = 0, low = high = 0;
4920 else
4922 low = range_successor (high0);
4923 high = high1;
4924 in_p = 1;
4925 if (low == 0)
4927 /* high1 > high0 but high0 has no successor. Punt. */
4928 return 0;
4933 else
4935 /* The case where we are excluding both ranges. Here the complex case
4936 is if they don't overlap. In that case, the only time we have a
4937 range is if they are adjacent. If the second is a subset of the
4938 first, the result is the first. Otherwise, the range to exclude
4939 starts at the beginning of the first range and ends at the end of the
4940 second. */
4941 if (no_overlap)
4943 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4944 range_successor (high0),
4945 1, low1, 0)))
4946 in_p = 0, low = low0, high = high1;
4947 else
4949 /* Canonicalize - [min, x] into - [-, x]. */
4950 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4951 switch (TREE_CODE (TREE_TYPE (low0)))
4953 case ENUMERAL_TYPE:
4954 if (TYPE_PRECISION (TREE_TYPE (low0))
4955 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4956 break;
4957 /* FALLTHROUGH */
4958 case INTEGER_TYPE:
4959 if (tree_int_cst_equal (low0,
4960 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4961 low0 = 0;
4962 break;
4963 case POINTER_TYPE:
4964 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4965 && integer_zerop (low0))
4966 low0 = 0;
4967 break;
4968 default:
4969 break;
4972 /* Canonicalize - [x, max] into - [x, -]. */
4973 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4974 switch (TREE_CODE (TREE_TYPE (high1)))
4976 case ENUMERAL_TYPE:
4977 if (TYPE_PRECISION (TREE_TYPE (high1))
4978 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4979 break;
4980 /* FALLTHROUGH */
4981 case INTEGER_TYPE:
4982 if (tree_int_cst_equal (high1,
4983 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4984 high1 = 0;
4985 break;
4986 case POINTER_TYPE:
4987 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4988 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4989 high1, 1,
4990 build_int_cst (TREE_TYPE (high1), 1),
4991 1)))
4992 high1 = 0;
4993 break;
4994 default:
4995 break;
4998 /* The ranges might be also adjacent between the maximum and
4999 minimum values of the given type. For
5000 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5001 return + [x + 1, y - 1]. */
5002 if (low0 == 0 && high1 == 0)
5004 low = range_successor (high0);
5005 high = range_predecessor (low1);
5006 if (low == 0 || high == 0)
5007 return 0;
5009 in_p = 1;
5011 else
5012 return 0;
5015 else if (subset)
5016 in_p = 0, low = low0, high = high0;
5017 else
5018 in_p = 0, low = low0, high = high1;
5021 *pin_p = in_p, *plow = low, *phigh = high;
5022 return 1;
5026 /* Subroutine of fold, looking inside expressions of the form
5027 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5028 of the COND_EXPR. This function is being used also to optimize
5029 A op B ? C : A, by reversing the comparison first.
5031 Return a folded expression whose code is not a COND_EXPR
5032 anymore, or NULL_TREE if no folding opportunity is found. */
5034 static tree
5035 fold_cond_expr_with_comparison (location_t loc, tree type,
5036 tree arg0, tree arg1, tree arg2)
5038 enum tree_code comp_code = TREE_CODE (arg0);
5039 tree arg00 = TREE_OPERAND (arg0, 0);
5040 tree arg01 = TREE_OPERAND (arg0, 1);
5041 tree arg1_type = TREE_TYPE (arg1);
5042 tree tem;
5044 STRIP_NOPS (arg1);
5045 STRIP_NOPS (arg2);
5047 /* If we have A op 0 ? A : -A, consider applying the following
5048 transformations:
5050 A == 0? A : -A same as -A
5051 A != 0? A : -A same as 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)
5055 A < 0? A : -A same as -abs (A)
5057 None of these transformations work for modes with signed
5058 zeros. If A is +/-0, the first two transformations will
5059 change the sign of the result (from +0 to -0, or vice
5060 versa). The last four will fix the sign of the result,
5061 even though the original expressions could be positive or
5062 negative, depending on the sign of A.
5064 Note that all these transformations are correct if A is
5065 NaN, since the two alternatives (A and -A) are also NaNs. */
5066 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5067 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5068 ? real_zerop (arg01)
5069 : integer_zerop (arg01))
5070 && ((TREE_CODE (arg2) == NEGATE_EXPR
5071 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5072 /* In the case that A is of the form X-Y, '-A' (arg2) may
5073 have already been folded to Y-X, check for that. */
5074 || (TREE_CODE (arg1) == MINUS_EXPR
5075 && TREE_CODE (arg2) == MINUS_EXPR
5076 && operand_equal_p (TREE_OPERAND (arg1, 0),
5077 TREE_OPERAND (arg2, 1), 0)
5078 && operand_equal_p (TREE_OPERAND (arg1, 1),
5079 TREE_OPERAND (arg2, 0), 0))))
5080 switch (comp_code)
5082 case EQ_EXPR:
5083 case UNEQ_EXPR:
5084 tem = fold_convert_loc (loc, arg1_type, arg1);
5085 return pedantic_non_lvalue_loc (loc,
5086 fold_convert_loc (loc, type,
5087 negate_expr (tem)));
5088 case NE_EXPR:
5089 case LTGT_EXPR:
5090 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5091 case UNGE_EXPR:
5092 case UNGT_EXPR:
5093 if (flag_trapping_math)
5094 break;
5095 /* Fall through. */
5096 case GE_EXPR:
5097 case GT_EXPR:
5098 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5099 break;
5100 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5101 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5102 case UNLE_EXPR:
5103 case UNLT_EXPR:
5104 if (flag_trapping_math)
5105 break;
5106 /* FALLTHRU */
5107 case LE_EXPR:
5108 case LT_EXPR:
5109 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5110 break;
5111 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5112 return negate_expr (fold_convert_loc (loc, type, tem));
5113 default:
5114 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5115 break;
5118 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5119 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5120 both transformations are correct when A is NaN: A != 0
5121 is then true, and A == 0 is false. */
5123 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5124 && integer_zerop (arg01) && integer_zerop (arg2))
5126 if (comp_code == NE_EXPR)
5127 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5128 else if (comp_code == EQ_EXPR)
5129 return build_zero_cst (type);
5132 /* Try some transformations of A op B ? A : B.
5134 A == B? A : B same as B
5135 A != B? A : B same as A
5136 A >= B? A : B same as max (A, B)
5137 A > B? A : B same as max (B, A)
5138 A <= B? A : B same as min (A, B)
5139 A < B? A : B same as min (B, A)
5141 As above, these transformations don't work in the presence
5142 of signed zeros. For example, if A and B are zeros of
5143 opposite sign, the first two transformations will change
5144 the sign of the result. In the last four, the original
5145 expressions give different results for (A=+0, B=-0) and
5146 (A=-0, B=+0), but the transformed expressions do not.
5148 The first two transformations are correct if either A or B
5149 is a NaN. In the first transformation, the condition will
5150 be false, and B will indeed be chosen. In the case of the
5151 second transformation, the condition A != B will be true,
5152 and A will be chosen.
5154 The conversions to max() and min() are not correct if B is
5155 a number and A is not. The conditions in the original
5156 expressions will be false, so all four give B. The min()
5157 and max() versions would give a NaN instead. */
5158 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5159 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5160 /* Avoid these transformations if the COND_EXPR may be used
5161 as an lvalue in the C++ front-end. PR c++/19199. */
5162 && (in_gimple_form
5163 || VECTOR_TYPE_P (type)
5164 || (! lang_GNU_CXX ()
5165 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5166 || ! maybe_lvalue_p (arg1)
5167 || ! maybe_lvalue_p (arg2)))
5169 tree comp_op0 = arg00;
5170 tree comp_op1 = arg01;
5171 tree comp_type = TREE_TYPE (comp_op0);
5173 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5174 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5176 comp_type = type;
5177 comp_op0 = arg1;
5178 comp_op1 = arg2;
5181 switch (comp_code)
5183 case EQ_EXPR:
5184 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5185 case NE_EXPR:
5186 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5187 case LE_EXPR:
5188 case LT_EXPR:
5189 case UNLE_EXPR:
5190 case UNLT_EXPR:
5191 /* In C++ a ?: expression can be an lvalue, so put the
5192 operand which will be used if they are equal first
5193 so that we can convert this back to the
5194 corresponding COND_EXPR. */
5195 if (!HONOR_NANS (arg1))
5197 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5198 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5199 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5200 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5201 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5202 comp_op1, comp_op0);
5203 return pedantic_non_lvalue_loc (loc,
5204 fold_convert_loc (loc, type, tem));
5206 break;
5207 case GE_EXPR:
5208 case GT_EXPR:
5209 case UNGE_EXPR:
5210 case UNGT_EXPR:
5211 if (!HONOR_NANS (arg1))
5213 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5214 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5215 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5216 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5217 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5218 comp_op1, comp_op0);
5219 return pedantic_non_lvalue_loc (loc,
5220 fold_convert_loc (loc, type, tem));
5222 break;
5223 case UNEQ_EXPR:
5224 if (!HONOR_NANS (arg1))
5225 return pedantic_non_lvalue_loc (loc,
5226 fold_convert_loc (loc, type, arg2));
5227 break;
5228 case LTGT_EXPR:
5229 if (!HONOR_NANS (arg1))
5230 return pedantic_non_lvalue_loc (loc,
5231 fold_convert_loc (loc, type, arg1));
5232 break;
5233 default:
5234 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5235 break;
5239 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5240 we might still be able to simplify this. For example,
5241 if C1 is one less or one more than C2, this might have started
5242 out as a MIN or MAX and been transformed by this function.
5243 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5245 if (INTEGRAL_TYPE_P (type)
5246 && TREE_CODE (arg01) == INTEGER_CST
5247 && TREE_CODE (arg2) == INTEGER_CST)
5248 switch (comp_code)
5250 case EQ_EXPR:
5251 if (TREE_CODE (arg1) == INTEGER_CST)
5252 break;
5253 /* We can replace A with C1 in this case. */
5254 arg1 = fold_convert_loc (loc, type, arg01);
5255 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5257 case LT_EXPR:
5258 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5259 MIN_EXPR, to preserve the signedness of the comparison. */
5260 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5261 OEP_ONLY_CONST)
5262 && operand_equal_p (arg01,
5263 const_binop (PLUS_EXPR, arg2,
5264 build_int_cst (type, 1)),
5265 OEP_ONLY_CONST))
5267 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5268 fold_convert_loc (loc, TREE_TYPE (arg00),
5269 arg2));
5270 return pedantic_non_lvalue_loc (loc,
5271 fold_convert_loc (loc, type, tem));
5273 break;
5275 case LE_EXPR:
5276 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5277 as above. */
5278 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5279 OEP_ONLY_CONST)
5280 && operand_equal_p (arg01,
5281 const_binop (MINUS_EXPR, arg2,
5282 build_int_cst (type, 1)),
5283 OEP_ONLY_CONST))
5285 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5286 fold_convert_loc (loc, TREE_TYPE (arg00),
5287 arg2));
5288 return pedantic_non_lvalue_loc (loc,
5289 fold_convert_loc (loc, type, tem));
5291 break;
5293 case GT_EXPR:
5294 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5295 MAX_EXPR, to preserve the signedness of the comparison. */
5296 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5297 OEP_ONLY_CONST)
5298 && operand_equal_p (arg01,
5299 const_binop (MINUS_EXPR, arg2,
5300 build_int_cst (type, 1)),
5301 OEP_ONLY_CONST))
5303 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5304 fold_convert_loc (loc, TREE_TYPE (arg00),
5305 arg2));
5306 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5308 break;
5310 case GE_EXPR:
5311 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5312 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5313 OEP_ONLY_CONST)
5314 && operand_equal_p (arg01,
5315 const_binop (PLUS_EXPR, arg2,
5316 build_int_cst (type, 1)),
5317 OEP_ONLY_CONST))
5319 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5320 fold_convert_loc (loc, TREE_TYPE (arg00),
5321 arg2));
5322 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5324 break;
5325 case NE_EXPR:
5326 break;
5327 default:
5328 gcc_unreachable ();
5331 return NULL_TREE;
5336 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5337 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5338 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5339 false) >= 2)
5340 #endif
5342 /* EXP is some logical combination of boolean tests. See if we can
5343 merge it into some range test. Return the new tree if so. */
5345 static tree
5346 fold_range_test (location_t loc, enum tree_code code, tree type,
5347 tree op0, tree op1)
5349 int or_op = (code == TRUTH_ORIF_EXPR
5350 || code == TRUTH_OR_EXPR);
5351 int in0_p, in1_p, in_p;
5352 tree low0, low1, low, high0, high1, high;
5353 bool strict_overflow_p = false;
5354 tree tem, lhs, rhs;
5355 const char * const warnmsg = G_("assuming signed overflow does not occur "
5356 "when simplifying range test");
5358 if (!INTEGRAL_TYPE_P (type))
5359 return 0;
5361 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5362 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5364 /* If this is an OR operation, invert both sides; we will invert
5365 again at the end. */
5366 if (or_op)
5367 in0_p = ! in0_p, in1_p = ! in1_p;
5369 /* If both expressions are the same, if we can merge the ranges, and we
5370 can build the range test, return it or it inverted. If one of the
5371 ranges is always true or always false, consider it to be the same
5372 expression as the other. */
5373 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5374 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5375 in1_p, low1, high1)
5376 && 0 != (tem = (build_range_check (loc, type,
5377 lhs != 0 ? lhs
5378 : rhs != 0 ? rhs : integer_zero_node,
5379 in_p, low, high))))
5381 if (strict_overflow_p)
5382 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5383 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5386 /* On machines where the branch cost is expensive, if this is a
5387 short-circuited branch and the underlying object on both sides
5388 is the same, make a non-short-circuit operation. */
5389 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5390 && lhs != 0 && rhs != 0
5391 && (code == TRUTH_ANDIF_EXPR
5392 || code == TRUTH_ORIF_EXPR)
5393 && operand_equal_p (lhs, rhs, 0))
5395 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5396 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5397 which cases we can't do this. */
5398 if (simple_operand_p (lhs))
5399 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5400 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5401 type, op0, op1);
5403 else if (!lang_hooks.decls.global_bindings_p ()
5404 && !CONTAINS_PLACEHOLDER_P (lhs))
5406 tree common = save_expr (lhs);
5408 if (0 != (lhs = build_range_check (loc, type, common,
5409 or_op ? ! in0_p : in0_p,
5410 low0, high0))
5411 && (0 != (rhs = build_range_check (loc, type, common,
5412 or_op ? ! in1_p : in1_p,
5413 low1, high1))))
5415 if (strict_overflow_p)
5416 fold_overflow_warning (warnmsg,
5417 WARN_STRICT_OVERFLOW_COMPARISON);
5418 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5419 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5420 type, lhs, rhs);
5425 return 0;
5428 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5429 bit value. Arrange things so the extra bits will be set to zero if and
5430 only if C is signed-extended to its full width. If MASK is nonzero,
5431 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5433 static tree
5434 unextend (tree c, int p, int unsignedp, tree mask)
5436 tree type = TREE_TYPE (c);
5437 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5438 tree temp;
5440 if (p == modesize || unsignedp)
5441 return c;
5443 /* We work by getting just the sign bit into the low-order bit, then
5444 into the high-order bit, then sign-extend. We then XOR that value
5445 with C. */
5446 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5448 /* We must use a signed type in order to get an arithmetic right shift.
5449 However, we must also avoid introducing accidental overflows, so that
5450 a subsequent call to integer_zerop will work. Hence we must
5451 do the type conversion here. At this point, the constant is either
5452 zero or one, and the conversion to a signed type can never overflow.
5453 We could get an overflow if this conversion is done anywhere else. */
5454 if (TYPE_UNSIGNED (type))
5455 temp = fold_convert (signed_type_for (type), temp);
5457 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5458 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5459 if (mask != 0)
5460 temp = const_binop (BIT_AND_EXPR, temp,
5461 fold_convert (TREE_TYPE (c), mask));
5462 /* If necessary, convert the type back to match the type of C. */
5463 if (TYPE_UNSIGNED (type))
5464 temp = fold_convert (type, temp);
5466 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5469 /* For an expression that has the form
5470 (A && B) || ~B
5472 (A || B) && ~B,
5473 we can drop one of the inner expressions and simplify to
5474 A || ~B
5476 A && ~B
5477 LOC is the location of the resulting expression. OP is the inner
5478 logical operation; the left-hand side in the examples above, while CMPOP
5479 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5480 removing a condition that guards another, as in
5481 (A != NULL && A->...) || A == NULL
5482 which we must not transform. If RHS_ONLY is true, only eliminate the
5483 right-most operand of the inner logical operation. */
5485 static tree
5486 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5487 bool rhs_only)
5489 tree type = TREE_TYPE (cmpop);
5490 enum tree_code code = TREE_CODE (cmpop);
5491 enum tree_code truthop_code = TREE_CODE (op);
5492 tree lhs = TREE_OPERAND (op, 0);
5493 tree rhs = TREE_OPERAND (op, 1);
5494 tree orig_lhs = lhs, orig_rhs = rhs;
5495 enum tree_code rhs_code = TREE_CODE (rhs);
5496 enum tree_code lhs_code = TREE_CODE (lhs);
5497 enum tree_code inv_code;
5499 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5500 return NULL_TREE;
5502 if (TREE_CODE_CLASS (code) != tcc_comparison)
5503 return NULL_TREE;
5505 if (rhs_code == truthop_code)
5507 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5508 if (newrhs != NULL_TREE)
5510 rhs = newrhs;
5511 rhs_code = TREE_CODE (rhs);
5514 if (lhs_code == truthop_code && !rhs_only)
5516 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5517 if (newlhs != NULL_TREE)
5519 lhs = newlhs;
5520 lhs_code = TREE_CODE (lhs);
5524 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5525 if (inv_code == rhs_code
5526 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5527 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5528 return lhs;
5529 if (!rhs_only && inv_code == lhs_code
5530 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5531 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5532 return rhs;
5533 if (rhs != orig_rhs || lhs != orig_lhs)
5534 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5535 lhs, rhs);
5536 return NULL_TREE;
5539 /* Find ways of folding logical expressions of LHS and RHS:
5540 Try to merge two comparisons to the same innermost item.
5541 Look for range tests like "ch >= '0' && ch <= '9'".
5542 Look for combinations of simple terms on machines with expensive branches
5543 and evaluate the RHS unconditionally.
5545 For example, if we have p->a == 2 && p->b == 4 and we can make an
5546 object large enough to span both A and B, we can do this with a comparison
5547 against the object ANDed with the a mask.
5549 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5550 operations to do this with one comparison.
5552 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5553 function and the one above.
5555 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5556 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5558 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5559 two operands.
5561 We return the simplified tree or 0 if no optimization is possible. */
5563 static tree
5564 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5565 tree lhs, tree rhs)
5567 /* If this is the "or" of two comparisons, we can do something if
5568 the comparisons are NE_EXPR. If this is the "and", we can do something
5569 if the comparisons are EQ_EXPR. I.e.,
5570 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5572 WANTED_CODE is this operation code. For single bit fields, we can
5573 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5574 comparison for one-bit fields. */
5576 enum tree_code wanted_code;
5577 enum tree_code lcode, rcode;
5578 tree ll_arg, lr_arg, rl_arg, rr_arg;
5579 tree ll_inner, lr_inner, rl_inner, rr_inner;
5580 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5581 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5582 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5583 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5584 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5585 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5586 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5587 machine_mode lnmode, rnmode;
5588 tree ll_mask, lr_mask, rl_mask, rr_mask;
5589 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5590 tree l_const, r_const;
5591 tree lntype, rntype, result;
5592 HOST_WIDE_INT first_bit, end_bit;
5593 int volatilep;
5595 /* Start by getting the comparison codes. Fail if anything is volatile.
5596 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5597 it were surrounded with a NE_EXPR. */
5599 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5600 return 0;
5602 lcode = TREE_CODE (lhs);
5603 rcode = TREE_CODE (rhs);
5605 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5607 lhs = build2 (NE_EXPR, truth_type, lhs,
5608 build_int_cst (TREE_TYPE (lhs), 0));
5609 lcode = NE_EXPR;
5612 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5614 rhs = build2 (NE_EXPR, truth_type, rhs,
5615 build_int_cst (TREE_TYPE (rhs), 0));
5616 rcode = NE_EXPR;
5619 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5620 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5621 return 0;
5623 ll_arg = TREE_OPERAND (lhs, 0);
5624 lr_arg = TREE_OPERAND (lhs, 1);
5625 rl_arg = TREE_OPERAND (rhs, 0);
5626 rr_arg = TREE_OPERAND (rhs, 1);
5628 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5629 if (simple_operand_p (ll_arg)
5630 && simple_operand_p (lr_arg))
5632 if (operand_equal_p (ll_arg, rl_arg, 0)
5633 && operand_equal_p (lr_arg, rr_arg, 0))
5635 result = combine_comparisons (loc, code, lcode, rcode,
5636 truth_type, ll_arg, lr_arg);
5637 if (result)
5638 return result;
5640 else if (operand_equal_p (ll_arg, rr_arg, 0)
5641 && operand_equal_p (lr_arg, rl_arg, 0))
5643 result = combine_comparisons (loc, code, lcode,
5644 swap_tree_comparison (rcode),
5645 truth_type, ll_arg, lr_arg);
5646 if (result)
5647 return result;
5651 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5652 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5654 /* If the RHS can be evaluated unconditionally and its operands are
5655 simple, it wins to evaluate the RHS unconditionally on machines
5656 with expensive branches. In this case, this isn't a comparison
5657 that can be merged. */
5659 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5660 false) >= 2
5661 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5662 && simple_operand_p (rl_arg)
5663 && simple_operand_p (rr_arg))
5665 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5666 if (code == TRUTH_OR_EXPR
5667 && lcode == NE_EXPR && integer_zerop (lr_arg)
5668 && rcode == NE_EXPR && integer_zerop (rr_arg)
5669 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5670 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5671 return build2_loc (loc, NE_EXPR, truth_type,
5672 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5673 ll_arg, rl_arg),
5674 build_int_cst (TREE_TYPE (ll_arg), 0));
5676 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5677 if (code == TRUTH_AND_EXPR
5678 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5679 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5680 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5681 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5682 return build2_loc (loc, EQ_EXPR, truth_type,
5683 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5684 ll_arg, rl_arg),
5685 build_int_cst (TREE_TYPE (ll_arg), 0));
5688 /* See if the comparisons can be merged. Then get all the parameters for
5689 each side. */
5691 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5692 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5693 return 0;
5695 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5696 volatilep = 0;
5697 ll_inner = decode_field_reference (loc, &ll_arg,
5698 &ll_bitsize, &ll_bitpos, &ll_mode,
5699 &ll_unsignedp, &ll_reversep, &volatilep,
5700 &ll_mask, &ll_and_mask);
5701 lr_inner = decode_field_reference (loc, &lr_arg,
5702 &lr_bitsize, &lr_bitpos, &lr_mode,
5703 &lr_unsignedp, &lr_reversep, &volatilep,
5704 &lr_mask, &lr_and_mask);
5705 rl_inner = decode_field_reference (loc, &rl_arg,
5706 &rl_bitsize, &rl_bitpos, &rl_mode,
5707 &rl_unsignedp, &rl_reversep, &volatilep,
5708 &rl_mask, &rl_and_mask);
5709 rr_inner = decode_field_reference (loc, &rr_arg,
5710 &rr_bitsize, &rr_bitpos, &rr_mode,
5711 &rr_unsignedp, &rr_reversep, &volatilep,
5712 &rr_mask, &rr_and_mask);
5714 /* It must be true that the inner operation on the lhs of each
5715 comparison must be the same if we are to be able to do anything.
5716 Then see if we have constants. If not, the same must be true for
5717 the rhs's. */
5718 if (volatilep
5719 || ll_reversep != rl_reversep
5720 || ll_inner == 0 || rl_inner == 0
5721 || ! operand_equal_p (ll_inner, rl_inner, 0))
5722 return 0;
5724 if (TREE_CODE (lr_arg) == INTEGER_CST
5725 && TREE_CODE (rr_arg) == INTEGER_CST)
5727 l_const = lr_arg, r_const = rr_arg;
5728 lr_reversep = ll_reversep;
5730 else if (lr_reversep != rr_reversep
5731 || lr_inner == 0 || rr_inner == 0
5732 || ! operand_equal_p (lr_inner, rr_inner, 0))
5733 return 0;
5734 else
5735 l_const = r_const = 0;
5737 /* If either comparison code is not correct for our logical operation,
5738 fail. However, we can convert a one-bit comparison against zero into
5739 the opposite comparison against that bit being set in the field. */
5741 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5742 if (lcode != wanted_code)
5744 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5746 /* Make the left operand unsigned, since we are only interested
5747 in the value of one bit. Otherwise we are doing the wrong
5748 thing below. */
5749 ll_unsignedp = 1;
5750 l_const = ll_mask;
5752 else
5753 return 0;
5756 /* This is analogous to the code for l_const above. */
5757 if (rcode != wanted_code)
5759 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5761 rl_unsignedp = 1;
5762 r_const = rl_mask;
5764 else
5765 return 0;
5768 /* See if we can find a mode that contains both fields being compared on
5769 the left. If we can't, fail. Otherwise, update all constants and masks
5770 to be relative to a field of that size. */
5771 first_bit = MIN (ll_bitpos, rl_bitpos);
5772 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5773 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5774 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5775 volatilep);
5776 if (lnmode == VOIDmode)
5777 return 0;
5779 lnbitsize = GET_MODE_BITSIZE (lnmode);
5780 lnbitpos = first_bit & ~ (lnbitsize - 1);
5781 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5782 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5784 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5786 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5787 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5790 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5791 size_int (xll_bitpos));
5792 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5793 size_int (xrl_bitpos));
5795 if (l_const)
5797 l_const = fold_convert_loc (loc, lntype, l_const);
5798 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5799 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5800 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5801 fold_build1_loc (loc, BIT_NOT_EXPR,
5802 lntype, ll_mask))))
5804 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5806 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5809 if (r_const)
5811 r_const = fold_convert_loc (loc, lntype, r_const);
5812 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5813 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5814 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5815 fold_build1_loc (loc, BIT_NOT_EXPR,
5816 lntype, rl_mask))))
5818 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5820 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5824 /* If the right sides are not constant, do the same for it. Also,
5825 disallow this optimization if a size or signedness mismatch occurs
5826 between the left and right sides. */
5827 if (l_const == 0)
5829 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5830 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5831 /* Make sure the two fields on the right
5832 correspond to the left without being swapped. */
5833 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5834 return 0;
5836 first_bit = MIN (lr_bitpos, rr_bitpos);
5837 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5838 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5839 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5840 volatilep);
5841 if (rnmode == VOIDmode)
5842 return 0;
5844 rnbitsize = GET_MODE_BITSIZE (rnmode);
5845 rnbitpos = first_bit & ~ (rnbitsize - 1);
5846 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5847 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5849 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5851 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5852 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5855 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5856 rntype, lr_mask),
5857 size_int (xlr_bitpos));
5858 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5859 rntype, rr_mask),
5860 size_int (xrr_bitpos));
5862 /* Make a mask that corresponds to both fields being compared.
5863 Do this for both items being compared. If the operands are the
5864 same size and the bits being compared are in the same position
5865 then we can do this by masking both and comparing the masked
5866 results. */
5867 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5868 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5869 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5871 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5872 lntype, lnbitsize, lnbitpos,
5873 ll_unsignedp || rl_unsignedp, ll_reversep);
5874 if (! all_ones_mask_p (ll_mask, lnbitsize))
5875 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5877 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5878 rntype, rnbitsize, rnbitpos,
5879 lr_unsignedp || rr_unsignedp, lr_reversep);
5880 if (! all_ones_mask_p (lr_mask, rnbitsize))
5881 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5883 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5886 /* There is still another way we can do something: If both pairs of
5887 fields being compared are adjacent, we may be able to make a wider
5888 field containing them both.
5890 Note that we still must mask the lhs/rhs expressions. Furthermore,
5891 the mask must be shifted to account for the shift done by
5892 make_bit_field_ref. */
5893 if ((ll_bitsize + ll_bitpos == rl_bitpos
5894 && lr_bitsize + lr_bitpos == rr_bitpos)
5895 || (ll_bitpos == rl_bitpos + rl_bitsize
5896 && lr_bitpos == rr_bitpos + rr_bitsize))
5898 tree type;
5900 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5901 ll_bitsize + rl_bitsize,
5902 MIN (ll_bitpos, rl_bitpos),
5903 ll_unsignedp, ll_reversep);
5904 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5905 lr_bitsize + rr_bitsize,
5906 MIN (lr_bitpos, rr_bitpos),
5907 lr_unsignedp, lr_reversep);
5909 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5910 size_int (MIN (xll_bitpos, xrl_bitpos)));
5911 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5912 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5914 /* Convert to the smaller type before masking out unwanted bits. */
5915 type = lntype;
5916 if (lntype != rntype)
5918 if (lnbitsize > rnbitsize)
5920 lhs = fold_convert_loc (loc, rntype, lhs);
5921 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5922 type = rntype;
5924 else if (lnbitsize < rnbitsize)
5926 rhs = fold_convert_loc (loc, lntype, rhs);
5927 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5928 type = lntype;
5932 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5933 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5935 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5936 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5938 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5941 return 0;
5944 /* Handle the case of comparisons with constants. If there is something in
5945 common between the masks, those bits of the constants must be the same.
5946 If not, the condition is always false. Test for this to avoid generating
5947 incorrect code below. */
5948 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5949 if (! integer_zerop (result)
5950 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5951 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5953 if (wanted_code == NE_EXPR)
5955 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5956 return constant_boolean_node (true, truth_type);
5958 else
5960 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5961 return constant_boolean_node (false, truth_type);
5965 /* Construct the expression we will return. First get the component
5966 reference we will make. Unless the mask is all ones the width of
5967 that field, perform the mask operation. Then compare with the
5968 merged constant. */
5969 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5970 lntype, lnbitsize, lnbitpos,
5971 ll_unsignedp || rl_unsignedp, ll_reversep);
5973 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5974 if (! all_ones_mask_p (ll_mask, lnbitsize))
5975 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5977 return build2_loc (loc, wanted_code, truth_type, result,
5978 const_binop (BIT_IOR_EXPR, l_const, r_const));
5981 /* T is an integer expression that is being multiplied, divided, or taken a
5982 modulus (CODE says which and what kind of divide or modulus) by a
5983 constant C. See if we can eliminate that operation by folding it with
5984 other operations already in T. WIDE_TYPE, if non-null, is a type that
5985 should be used for the computation if wider than our type.
5987 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5988 (X * 2) + (Y * 4). We must, however, be assured that either the original
5989 expression would not overflow or that overflow is undefined for the type
5990 in the language in question.
5992 If we return a non-null expression, it is an equivalent form of the
5993 original computation, but need not be in the original type.
5995 We set *STRICT_OVERFLOW_P to true if the return values depends on
5996 signed overflow being undefined. Otherwise we do not change
5997 *STRICT_OVERFLOW_P. */
5999 static tree
6000 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6001 bool *strict_overflow_p)
6003 /* To avoid exponential search depth, refuse to allow recursion past
6004 three levels. Beyond that (1) it's highly unlikely that we'll find
6005 something interesting and (2) we've probably processed it before
6006 when we built the inner expression. */
6008 static int depth;
6009 tree ret;
6011 if (depth > 3)
6012 return NULL;
6014 depth++;
6015 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6016 depth--;
6018 return ret;
6021 static tree
6022 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6023 bool *strict_overflow_p)
6025 tree type = TREE_TYPE (t);
6026 enum tree_code tcode = TREE_CODE (t);
6027 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6028 > GET_MODE_SIZE (TYPE_MODE (type)))
6029 ? wide_type : type);
6030 tree t1, t2;
6031 int same_p = tcode == code;
6032 tree op0 = NULL_TREE, op1 = NULL_TREE;
6033 bool sub_strict_overflow_p;
6035 /* Don't deal with constants of zero here; they confuse the code below. */
6036 if (integer_zerop (c))
6037 return NULL_TREE;
6039 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6040 op0 = TREE_OPERAND (t, 0);
6042 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6043 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6045 /* Note that we need not handle conditional operations here since fold
6046 already handles those cases. So just do arithmetic here. */
6047 switch (tcode)
6049 case INTEGER_CST:
6050 /* For a constant, we can always simplify if we are a multiply
6051 or (for divide and modulus) if it is a multiple of our constant. */
6052 if (code == MULT_EXPR
6053 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6055 tree tem = const_binop (code, fold_convert (ctype, t),
6056 fold_convert (ctype, c));
6057 /* If the multiplication overflowed, we lost information on it.
6058 See PR68142 and PR69845. */
6059 if (TREE_OVERFLOW (tem))
6060 return NULL_TREE;
6061 return tem;
6063 break;
6065 CASE_CONVERT: case NON_LVALUE_EXPR:
6066 /* If op0 is an expression ... */
6067 if ((COMPARISON_CLASS_P (op0)
6068 || UNARY_CLASS_P (op0)
6069 || BINARY_CLASS_P (op0)
6070 || VL_EXP_CLASS_P (op0)
6071 || EXPRESSION_CLASS_P (op0))
6072 /* ... and has wrapping overflow, and its type is smaller
6073 than ctype, then we cannot pass through as widening. */
6074 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6075 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6076 && (TYPE_PRECISION (ctype)
6077 > TYPE_PRECISION (TREE_TYPE (op0))))
6078 /* ... or this is a truncation (t is narrower than op0),
6079 then we cannot pass through this narrowing. */
6080 || (TYPE_PRECISION (type)
6081 < TYPE_PRECISION (TREE_TYPE (op0)))
6082 /* ... or signedness changes for division or modulus,
6083 then we cannot pass through this conversion. */
6084 || (code != MULT_EXPR
6085 && (TYPE_UNSIGNED (ctype)
6086 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6087 /* ... or has undefined overflow while the converted to
6088 type has not, we cannot do the operation in the inner type
6089 as that would introduce undefined overflow. */
6090 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6091 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6092 && !TYPE_OVERFLOW_UNDEFINED (type))))
6093 break;
6095 /* Pass the constant down and see if we can make a simplification. If
6096 we can, replace this expression with the inner simplification for
6097 possible later conversion to our or some other type. */
6098 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6099 && TREE_CODE (t2) == INTEGER_CST
6100 && !TREE_OVERFLOW (t2)
6101 && (0 != (t1 = extract_muldiv (op0, t2, code,
6102 code == MULT_EXPR
6103 ? ctype : NULL_TREE,
6104 strict_overflow_p))))
6105 return t1;
6106 break;
6108 case ABS_EXPR:
6109 /* If widening the type changes it from signed to unsigned, then we
6110 must avoid building ABS_EXPR itself as unsigned. */
6111 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6113 tree cstype = (*signed_type_for) (ctype);
6114 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6115 != 0)
6117 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6118 return fold_convert (ctype, t1);
6120 break;
6122 /* If the constant is negative, we cannot simplify this. */
6123 if (tree_int_cst_sgn (c) == -1)
6124 break;
6125 /* FALLTHROUGH */
6126 case NEGATE_EXPR:
6127 /* For division and modulus, type can't be unsigned, as e.g.
6128 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6129 For signed types, even with wrapping overflow, this is fine. */
6130 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6131 break;
6132 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6133 != 0)
6134 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6135 break;
6137 case MIN_EXPR: case MAX_EXPR:
6138 /* If widening the type changes the signedness, then we can't perform
6139 this optimization as that changes the result. */
6140 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6141 break;
6143 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6144 sub_strict_overflow_p = false;
6145 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6146 &sub_strict_overflow_p)) != 0
6147 && (t2 = extract_muldiv (op1, c, code, wide_type,
6148 &sub_strict_overflow_p)) != 0)
6150 if (tree_int_cst_sgn (c) < 0)
6151 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6152 if (sub_strict_overflow_p)
6153 *strict_overflow_p = true;
6154 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6155 fold_convert (ctype, t2));
6157 break;
6159 case LSHIFT_EXPR: case RSHIFT_EXPR:
6160 /* If the second operand is constant, this is a multiplication
6161 or floor division, by a power of two, so we can treat it that
6162 way unless the multiplier or divisor overflows. Signed
6163 left-shift overflow is implementation-defined rather than
6164 undefined in C90, so do not convert signed left shift into
6165 multiplication. */
6166 if (TREE_CODE (op1) == INTEGER_CST
6167 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6168 /* const_binop may not detect overflow correctly,
6169 so check for it explicitly here. */
6170 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6171 && 0 != (t1 = fold_convert (ctype,
6172 const_binop (LSHIFT_EXPR,
6173 size_one_node,
6174 op1)))
6175 && !TREE_OVERFLOW (t1))
6176 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6177 ? MULT_EXPR : FLOOR_DIV_EXPR,
6178 ctype,
6179 fold_convert (ctype, op0),
6180 t1),
6181 c, code, wide_type, strict_overflow_p);
6182 break;
6184 case PLUS_EXPR: case MINUS_EXPR:
6185 /* See if we can eliminate the operation on both sides. If we can, we
6186 can return a new PLUS or MINUS. If we can't, the only remaining
6187 cases where we can do anything are if the second operand is a
6188 constant. */
6189 sub_strict_overflow_p = false;
6190 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6191 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6192 if (t1 != 0 && t2 != 0
6193 && (code == MULT_EXPR
6194 /* If not multiplication, we can only do this if both operands
6195 are divisible by c. */
6196 || (multiple_of_p (ctype, op0, c)
6197 && multiple_of_p (ctype, op1, c))))
6199 if (sub_strict_overflow_p)
6200 *strict_overflow_p = true;
6201 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6202 fold_convert (ctype, t2));
6205 /* If this was a subtraction, negate OP1 and set it to be an addition.
6206 This simplifies the logic below. */
6207 if (tcode == MINUS_EXPR)
6209 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6210 /* If OP1 was not easily negatable, the constant may be OP0. */
6211 if (TREE_CODE (op0) == INTEGER_CST)
6213 std::swap (op0, op1);
6214 std::swap (t1, t2);
6218 if (TREE_CODE (op1) != INTEGER_CST)
6219 break;
6221 /* If either OP1 or C are negative, this optimization is not safe for
6222 some of the division and remainder types while for others we need
6223 to change the code. */
6224 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6226 if (code == CEIL_DIV_EXPR)
6227 code = FLOOR_DIV_EXPR;
6228 else if (code == FLOOR_DIV_EXPR)
6229 code = CEIL_DIV_EXPR;
6230 else if (code != MULT_EXPR
6231 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6232 break;
6235 /* If it's a multiply or a division/modulus operation of a multiple
6236 of our constant, do the operation and verify it doesn't overflow. */
6237 if (code == MULT_EXPR
6238 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6240 op1 = const_binop (code, fold_convert (ctype, op1),
6241 fold_convert (ctype, c));
6242 /* We allow the constant to overflow with wrapping semantics. */
6243 if (op1 == 0
6244 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6245 break;
6247 else
6248 break;
6250 /* If we have an unsigned type, we cannot widen the operation since it
6251 will change the result if the original computation overflowed. */
6252 if (TYPE_UNSIGNED (ctype) && ctype != type)
6253 break;
6255 /* If we were able to eliminate our operation from the first side,
6256 apply our operation to the second side and reform the PLUS. */
6257 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6258 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6260 /* The last case is if we are a multiply. In that case, we can
6261 apply the distributive law to commute the multiply and addition
6262 if the multiplication of the constants doesn't overflow
6263 and overflow is defined. With undefined overflow
6264 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6265 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6266 return fold_build2 (tcode, ctype,
6267 fold_build2 (code, ctype,
6268 fold_convert (ctype, op0),
6269 fold_convert (ctype, c)),
6270 op1);
6272 break;
6274 case MULT_EXPR:
6275 /* We have a special case here if we are doing something like
6276 (C * 8) % 4 since we know that's zero. */
6277 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6278 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6279 /* If the multiplication can overflow we cannot optimize this. */
6280 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6281 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6282 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6284 *strict_overflow_p = true;
6285 return omit_one_operand (type, integer_zero_node, op0);
6288 /* ... fall through ... */
6290 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6291 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6292 /* If we can extract our operation from the LHS, do so and return a
6293 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6294 do something only if the second operand is a constant. */
6295 if (same_p
6296 && (t1 = extract_muldiv (op0, c, code, wide_type,
6297 strict_overflow_p)) != 0)
6298 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6299 fold_convert (ctype, op1));
6300 else if (tcode == MULT_EXPR && code == MULT_EXPR
6301 && (t1 = extract_muldiv (op1, c, code, wide_type,
6302 strict_overflow_p)) != 0)
6303 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6304 fold_convert (ctype, t1));
6305 else if (TREE_CODE (op1) != INTEGER_CST)
6306 return 0;
6308 /* If these are the same operation types, we can associate them
6309 assuming no overflow. */
6310 if (tcode == code)
6312 bool overflow_p = false;
6313 bool overflow_mul_p;
6314 signop sign = TYPE_SIGN (ctype);
6315 unsigned prec = TYPE_PRECISION (ctype);
6316 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6317 wi::to_wide (c, prec),
6318 sign, &overflow_mul_p);
6319 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6320 if (overflow_mul_p
6321 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6322 overflow_p = true;
6323 if (!overflow_p)
6324 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6325 wide_int_to_tree (ctype, mul));
6328 /* If these operations "cancel" each other, we have the main
6329 optimizations of this pass, which occur when either constant is a
6330 multiple of the other, in which case we replace this with either an
6331 operation or CODE or TCODE.
6333 If we have an unsigned type, we cannot do this since it will change
6334 the result if the original computation overflowed. */
6335 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6336 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6337 || (tcode == MULT_EXPR
6338 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6339 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6340 && code != MULT_EXPR)))
6342 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6344 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6345 *strict_overflow_p = true;
6346 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6347 fold_convert (ctype,
6348 const_binop (TRUNC_DIV_EXPR,
6349 op1, c)));
6351 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6353 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6354 *strict_overflow_p = true;
6355 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6356 fold_convert (ctype,
6357 const_binop (TRUNC_DIV_EXPR,
6358 c, op1)));
6361 break;
6363 default:
6364 break;
6367 return 0;
6370 /* Return a node which has the indicated constant VALUE (either 0 or
6371 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6372 and is of the indicated TYPE. */
6374 tree
6375 constant_boolean_node (bool value, tree type)
6377 if (type == integer_type_node)
6378 return value ? integer_one_node : integer_zero_node;
6379 else if (type == boolean_type_node)
6380 return value ? boolean_true_node : boolean_false_node;
6381 else if (TREE_CODE (type) == VECTOR_TYPE)
6382 return build_vector_from_val (type,
6383 build_int_cst (TREE_TYPE (type),
6384 value ? -1 : 0));
6385 else
6386 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6390 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6391 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6392 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6393 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6394 COND is the first argument to CODE; otherwise (as in the example
6395 given here), it is the second argument. TYPE is the type of the
6396 original expression. Return NULL_TREE if no simplification is
6397 possible. */
6399 static tree
6400 fold_binary_op_with_conditional_arg (location_t loc,
6401 enum tree_code code,
6402 tree type, tree op0, tree op1,
6403 tree cond, tree arg, int cond_first_p)
6405 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6406 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6407 tree test, true_value, false_value;
6408 tree lhs = NULL_TREE;
6409 tree rhs = NULL_TREE;
6410 enum tree_code cond_code = COND_EXPR;
6412 if (TREE_CODE (cond) == COND_EXPR
6413 || TREE_CODE (cond) == VEC_COND_EXPR)
6415 test = TREE_OPERAND (cond, 0);
6416 true_value = TREE_OPERAND (cond, 1);
6417 false_value = TREE_OPERAND (cond, 2);
6418 /* If this operand throws an expression, then it does not make
6419 sense to try to perform a logical or arithmetic operation
6420 involving it. */
6421 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6422 lhs = true_value;
6423 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6424 rhs = false_value;
6426 else if (!(TREE_CODE (type) != VECTOR_TYPE
6427 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6429 tree testtype = TREE_TYPE (cond);
6430 test = cond;
6431 true_value = constant_boolean_node (true, testtype);
6432 false_value = constant_boolean_node (false, testtype);
6434 else
6435 /* Detect the case of mixing vector and scalar types - bail out. */
6436 return NULL_TREE;
6438 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6439 cond_code = VEC_COND_EXPR;
6441 /* This transformation is only worthwhile if we don't have to wrap ARG
6442 in a SAVE_EXPR and the operation can be simplified without recursing
6443 on at least one of the branches once its pushed inside the COND_EXPR. */
6444 if (!TREE_CONSTANT (arg)
6445 && (TREE_SIDE_EFFECTS (arg)
6446 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6447 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6448 return NULL_TREE;
6450 arg = fold_convert_loc (loc, arg_type, arg);
6451 if (lhs == 0)
6453 true_value = fold_convert_loc (loc, cond_type, true_value);
6454 if (cond_first_p)
6455 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6456 else
6457 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6459 if (rhs == 0)
6461 false_value = fold_convert_loc (loc, cond_type, false_value);
6462 if (cond_first_p)
6463 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6464 else
6465 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6468 /* Check that we have simplified at least one of the branches. */
6469 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6470 return NULL_TREE;
6472 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6476 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6478 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6479 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6480 ADDEND is the same as X.
6482 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6483 and finite. The problematic cases are when X is zero, and its mode
6484 has signed zeros. In the case of rounding towards -infinity,
6485 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6486 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6488 bool
6489 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6491 if (!real_zerop (addend))
6492 return false;
6494 /* Don't allow the fold with -fsignaling-nans. */
6495 if (HONOR_SNANS (element_mode (type)))
6496 return false;
6498 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6499 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6500 return true;
6502 /* In a vector or complex, we would need to check the sign of all zeros. */
6503 if (TREE_CODE (addend) != REAL_CST)
6504 return false;
6506 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6507 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6508 negate = !negate;
6510 /* The mode has signed zeros, and we have to honor their sign.
6511 In this situation, there is only one case we can return true for.
6512 X - 0 is the same as X unless rounding towards -infinity is
6513 supported. */
6514 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6517 /* Subroutine of fold() that optimizes comparisons of a division by
6518 a nonzero integer constant against an integer constant, i.e.
6519 X/C1 op C2.
6521 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6522 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6523 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6525 The function returns the constant folded tree if a simplification
6526 can be made, and NULL_TREE otherwise. */
6528 static tree
6529 fold_div_compare (location_t loc,
6530 enum tree_code code, tree type, tree arg0, tree arg1)
6532 tree prod, tmp, hi, lo;
6533 tree arg00 = TREE_OPERAND (arg0, 0);
6534 tree arg01 = TREE_OPERAND (arg0, 1);
6535 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6536 bool neg_overflow = false;
6537 bool overflow;
6539 /* We have to do this the hard way to detect unsigned overflow.
6540 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6541 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6542 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6543 neg_overflow = false;
6545 if (sign == UNSIGNED)
6547 tmp = int_const_binop (MINUS_EXPR, arg01,
6548 build_int_cst (TREE_TYPE (arg01), 1));
6549 lo = prod;
6551 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6552 val = wi::add (prod, tmp, sign, &overflow);
6553 hi = force_fit_type (TREE_TYPE (arg00), val,
6554 -1, overflow | TREE_OVERFLOW (prod));
6556 else if (tree_int_cst_sgn (arg01) >= 0)
6558 tmp = int_const_binop (MINUS_EXPR, arg01,
6559 build_int_cst (TREE_TYPE (arg01), 1));
6560 switch (tree_int_cst_sgn (arg1))
6562 case -1:
6563 neg_overflow = true;
6564 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6565 hi = prod;
6566 break;
6568 case 0:
6569 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6570 hi = tmp;
6571 break;
6573 case 1:
6574 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6575 lo = prod;
6576 break;
6578 default:
6579 gcc_unreachable ();
6582 else
6584 /* A negative divisor reverses the relational operators. */
6585 code = swap_tree_comparison (code);
6587 tmp = int_const_binop (PLUS_EXPR, arg01,
6588 build_int_cst (TREE_TYPE (arg01), 1));
6589 switch (tree_int_cst_sgn (arg1))
6591 case -1:
6592 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6593 lo = prod;
6594 break;
6596 case 0:
6597 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6598 lo = tmp;
6599 break;
6601 case 1:
6602 neg_overflow = true;
6603 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6604 hi = prod;
6605 break;
6607 default:
6608 gcc_unreachable ();
6612 switch (code)
6614 case EQ_EXPR:
6615 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6616 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6617 if (TREE_OVERFLOW (hi))
6618 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6619 if (TREE_OVERFLOW (lo))
6620 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6621 return build_range_check (loc, type, arg00, 1, lo, hi);
6623 case NE_EXPR:
6624 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6625 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6626 if (TREE_OVERFLOW (hi))
6627 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6628 if (TREE_OVERFLOW (lo))
6629 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6630 return build_range_check (loc, type, arg00, 0, lo, hi);
6632 case LT_EXPR:
6633 if (TREE_OVERFLOW (lo))
6635 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6636 return omit_one_operand_loc (loc, type, tmp, arg00);
6638 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6640 case LE_EXPR:
6641 if (TREE_OVERFLOW (hi))
6643 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6644 return omit_one_operand_loc (loc, type, tmp, arg00);
6646 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6648 case GT_EXPR:
6649 if (TREE_OVERFLOW (hi))
6651 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6652 return omit_one_operand_loc (loc, type, tmp, arg00);
6654 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6656 case GE_EXPR:
6657 if (TREE_OVERFLOW (lo))
6659 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6660 return omit_one_operand_loc (loc, type, tmp, arg00);
6662 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6664 default:
6665 break;
6668 return NULL_TREE;
6672 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6673 equality/inequality test, then return a simplified form of the test
6674 using a sign testing. Otherwise return NULL. TYPE is the desired
6675 result type. */
6677 static tree
6678 fold_single_bit_test_into_sign_test (location_t loc,
6679 enum tree_code code, tree arg0, tree arg1,
6680 tree result_type)
6682 /* If this is testing a single bit, we can optimize the test. */
6683 if ((code == NE_EXPR || code == EQ_EXPR)
6684 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6685 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6687 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6688 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6689 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6691 if (arg00 != NULL_TREE
6692 /* This is only a win if casting to a signed type is cheap,
6693 i.e. when arg00's type is not a partial mode. */
6694 && TYPE_PRECISION (TREE_TYPE (arg00))
6695 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6697 tree stype = signed_type_for (TREE_TYPE (arg00));
6698 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6699 result_type,
6700 fold_convert_loc (loc, stype, arg00),
6701 build_int_cst (stype, 0));
6705 return NULL_TREE;
6708 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6709 equality/inequality test, then return a simplified form of
6710 the test using shifts and logical operations. Otherwise return
6711 NULL. TYPE is the desired result type. */
6713 tree
6714 fold_single_bit_test (location_t loc, enum tree_code code,
6715 tree arg0, tree arg1, tree result_type)
6717 /* If this is testing a single bit, we can optimize the test. */
6718 if ((code == NE_EXPR || code == EQ_EXPR)
6719 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6720 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6722 tree inner = TREE_OPERAND (arg0, 0);
6723 tree type = TREE_TYPE (arg0);
6724 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6725 machine_mode operand_mode = TYPE_MODE (type);
6726 int ops_unsigned;
6727 tree signed_type, unsigned_type, intermediate_type;
6728 tree tem, one;
6730 /* First, see if we can fold the single bit test into a sign-bit
6731 test. */
6732 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6733 result_type);
6734 if (tem)
6735 return tem;
6737 /* Otherwise we have (A & C) != 0 where C is a single bit,
6738 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6739 Similarly for (A & C) == 0. */
6741 /* If INNER is a right shift of a constant and it plus BITNUM does
6742 not overflow, adjust BITNUM and INNER. */
6743 if (TREE_CODE (inner) == RSHIFT_EXPR
6744 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6745 && bitnum < TYPE_PRECISION (type)
6746 && wi::ltu_p (TREE_OPERAND (inner, 1),
6747 TYPE_PRECISION (type) - bitnum))
6749 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6750 inner = TREE_OPERAND (inner, 0);
6753 /* If we are going to be able to omit the AND below, we must do our
6754 operations as unsigned. If we must use the AND, we have a choice.
6755 Normally unsigned is faster, but for some machines signed is. */
6756 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6757 && !flag_syntax_only) ? 0 : 1;
6759 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6760 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6761 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6762 inner = fold_convert_loc (loc, intermediate_type, inner);
6764 if (bitnum != 0)
6765 inner = build2 (RSHIFT_EXPR, intermediate_type,
6766 inner, size_int (bitnum));
6768 one = build_int_cst (intermediate_type, 1);
6770 if (code == EQ_EXPR)
6771 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6773 /* Put the AND last so it can combine with more things. */
6774 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6776 /* Make sure to return the proper type. */
6777 inner = fold_convert_loc (loc, result_type, inner);
6779 return inner;
6781 return NULL_TREE;
6784 /* Check whether we are allowed to reorder operands arg0 and arg1,
6785 such that the evaluation of arg1 occurs before arg0. */
6787 static bool
6788 reorder_operands_p (const_tree arg0, const_tree arg1)
6790 if (! flag_evaluation_order)
6791 return true;
6792 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6793 return true;
6794 return ! TREE_SIDE_EFFECTS (arg0)
6795 && ! TREE_SIDE_EFFECTS (arg1);
6798 /* Test whether it is preferable two swap two operands, ARG0 and
6799 ARG1, for example because ARG0 is an integer constant and ARG1
6800 isn't. If REORDER is true, only recommend swapping if we can
6801 evaluate the operands in reverse order. */
6803 bool
6804 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6806 if (CONSTANT_CLASS_P (arg1))
6807 return 0;
6808 if (CONSTANT_CLASS_P (arg0))
6809 return 1;
6811 STRIP_NOPS (arg0);
6812 STRIP_NOPS (arg1);
6814 if (TREE_CONSTANT (arg1))
6815 return 0;
6816 if (TREE_CONSTANT (arg0))
6817 return 1;
6819 if (reorder && flag_evaluation_order
6820 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6821 return 0;
6823 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6824 for commutative and comparison operators. Ensuring a canonical
6825 form allows the optimizers to find additional redundancies without
6826 having to explicitly check for both orderings. */
6827 if (TREE_CODE (arg0) == SSA_NAME
6828 && TREE_CODE (arg1) == SSA_NAME
6829 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6830 return 1;
6832 /* Put SSA_NAMEs last. */
6833 if (TREE_CODE (arg1) == SSA_NAME)
6834 return 0;
6835 if (TREE_CODE (arg0) == SSA_NAME)
6836 return 1;
6838 /* Put variables last. */
6839 if (DECL_P (arg1))
6840 return 0;
6841 if (DECL_P (arg0))
6842 return 1;
6844 return 0;
6848 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6849 means A >= Y && A != MAX, but in this case we know that
6850 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6852 static tree
6853 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6855 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6857 if (TREE_CODE (bound) == LT_EXPR)
6858 a = TREE_OPERAND (bound, 0);
6859 else if (TREE_CODE (bound) == GT_EXPR)
6860 a = TREE_OPERAND (bound, 1);
6861 else
6862 return NULL_TREE;
6864 typea = TREE_TYPE (a);
6865 if (!INTEGRAL_TYPE_P (typea)
6866 && !POINTER_TYPE_P (typea))
6867 return NULL_TREE;
6869 if (TREE_CODE (ineq) == LT_EXPR)
6871 a1 = TREE_OPERAND (ineq, 1);
6872 y = TREE_OPERAND (ineq, 0);
6874 else if (TREE_CODE (ineq) == GT_EXPR)
6876 a1 = TREE_OPERAND (ineq, 0);
6877 y = TREE_OPERAND (ineq, 1);
6879 else
6880 return NULL_TREE;
6882 if (TREE_TYPE (a1) != typea)
6883 return NULL_TREE;
6885 if (POINTER_TYPE_P (typea))
6887 /* Convert the pointer types into integer before taking the difference. */
6888 tree ta = fold_convert_loc (loc, ssizetype, a);
6889 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6890 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6892 else
6893 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6895 if (!diff || !integer_onep (diff))
6896 return NULL_TREE;
6898 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6901 /* Fold a sum or difference of at least one multiplication.
6902 Returns the folded tree or NULL if no simplification could be made. */
6904 static tree
6905 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6906 tree arg0, tree arg1)
6908 tree arg00, arg01, arg10, arg11;
6909 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6911 /* (A * C) +- (B * C) -> (A+-B) * C.
6912 (A * C) +- A -> A * (C+-1).
6913 We are most concerned about the case where C is a constant,
6914 but other combinations show up during loop reduction. Since
6915 it is not difficult, try all four possibilities. */
6917 if (TREE_CODE (arg0) == MULT_EXPR)
6919 arg00 = TREE_OPERAND (arg0, 0);
6920 arg01 = TREE_OPERAND (arg0, 1);
6922 else if (TREE_CODE (arg0) == INTEGER_CST)
6924 arg00 = build_one_cst (type);
6925 arg01 = arg0;
6927 else
6929 /* We cannot generate constant 1 for fract. */
6930 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6931 return NULL_TREE;
6932 arg00 = arg0;
6933 arg01 = build_one_cst (type);
6935 if (TREE_CODE (arg1) == MULT_EXPR)
6937 arg10 = TREE_OPERAND (arg1, 0);
6938 arg11 = TREE_OPERAND (arg1, 1);
6940 else if (TREE_CODE (arg1) == INTEGER_CST)
6942 arg10 = build_one_cst (type);
6943 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6944 the purpose of this canonicalization. */
6945 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6946 && negate_expr_p (arg1)
6947 && code == PLUS_EXPR)
6949 arg11 = negate_expr (arg1);
6950 code = MINUS_EXPR;
6952 else
6953 arg11 = arg1;
6955 else
6957 /* We cannot generate constant 1 for fract. */
6958 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6959 return NULL_TREE;
6960 arg10 = arg1;
6961 arg11 = build_one_cst (type);
6963 same = NULL_TREE;
6965 if (operand_equal_p (arg01, arg11, 0))
6966 same = arg01, alt0 = arg00, alt1 = arg10;
6967 else if (operand_equal_p (arg00, arg10, 0))
6968 same = arg00, alt0 = arg01, alt1 = arg11;
6969 else if (operand_equal_p (arg00, arg11, 0))
6970 same = arg00, alt0 = arg01, alt1 = arg10;
6971 else if (operand_equal_p (arg01, arg10, 0))
6972 same = arg01, alt0 = arg00, alt1 = arg11;
6974 /* No identical multiplicands; see if we can find a common
6975 power-of-two factor in non-power-of-two multiplies. This
6976 can help in multi-dimensional array access. */
6977 else if (tree_fits_shwi_p (arg01)
6978 && tree_fits_shwi_p (arg11))
6980 HOST_WIDE_INT int01, int11, tmp;
6981 bool swap = false;
6982 tree maybe_same;
6983 int01 = tree_to_shwi (arg01);
6984 int11 = tree_to_shwi (arg11);
6986 /* Move min of absolute values to int11. */
6987 if (absu_hwi (int01) < absu_hwi (int11))
6989 tmp = int01, int01 = int11, int11 = tmp;
6990 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6991 maybe_same = arg01;
6992 swap = true;
6994 else
6995 maybe_same = arg11;
6997 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6998 /* The remainder should not be a constant, otherwise we
6999 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7000 increased the number of multiplications necessary. */
7001 && TREE_CODE (arg10) != INTEGER_CST)
7003 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7004 build_int_cst (TREE_TYPE (arg00),
7005 int01 / int11));
7006 alt1 = arg10;
7007 same = maybe_same;
7008 if (swap)
7009 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7013 if (same)
7014 return fold_build2_loc (loc, MULT_EXPR, type,
7015 fold_build2_loc (loc, code, type,
7016 fold_convert_loc (loc, type, alt0),
7017 fold_convert_loc (loc, type, alt1)),
7018 fold_convert_loc (loc, type, same));
7020 return NULL_TREE;
7023 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7024 specified by EXPR into the buffer PTR of length LEN bytes.
7025 Return the number of bytes placed in the buffer, or zero
7026 upon failure. */
7028 static int
7029 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7031 tree type = TREE_TYPE (expr);
7032 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7033 int byte, offset, word, words;
7034 unsigned char value;
7036 if ((off == -1 && total_bytes > len)
7037 || off >= total_bytes)
7038 return 0;
7039 if (off == -1)
7040 off = 0;
7041 words = total_bytes / UNITS_PER_WORD;
7043 for (byte = 0; byte < total_bytes; byte++)
7045 int bitpos = byte * BITS_PER_UNIT;
7046 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7047 number of bytes. */
7048 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7050 if (total_bytes > UNITS_PER_WORD)
7052 word = byte / UNITS_PER_WORD;
7053 if (WORDS_BIG_ENDIAN)
7054 word = (words - 1) - word;
7055 offset = word * UNITS_PER_WORD;
7056 if (BYTES_BIG_ENDIAN)
7057 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7058 else
7059 offset += byte % UNITS_PER_WORD;
7061 else
7062 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7063 if (offset >= off
7064 && offset - off < len)
7065 ptr[offset - off] = value;
7067 return MIN (len, total_bytes - off);
7071 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7072 specified by EXPR into the buffer PTR of length LEN bytes.
7073 Return the number of bytes placed in the buffer, or zero
7074 upon failure. */
7076 static int
7077 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7079 tree type = TREE_TYPE (expr);
7080 machine_mode mode = TYPE_MODE (type);
7081 int total_bytes = GET_MODE_SIZE (mode);
7082 FIXED_VALUE_TYPE value;
7083 tree i_value, i_type;
7085 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7086 return 0;
7088 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7090 if (NULL_TREE == i_type
7091 || TYPE_PRECISION (i_type) != total_bytes)
7092 return 0;
7094 value = TREE_FIXED_CST (expr);
7095 i_value = double_int_to_tree (i_type, value.data);
7097 return native_encode_int (i_value, ptr, len, off);
7101 /* Subroutine of native_encode_expr. Encode the REAL_CST
7102 specified by EXPR into the buffer PTR of length LEN bytes.
7103 Return the number of bytes placed in the buffer, or zero
7104 upon failure. */
7106 static int
7107 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7109 tree type = TREE_TYPE (expr);
7110 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7111 int byte, offset, word, words, bitpos;
7112 unsigned char value;
7114 /* There are always 32 bits in each long, no matter the size of
7115 the hosts long. We handle floating point representations with
7116 up to 192 bits. */
7117 long tmp[6];
7119 if ((off == -1 && total_bytes > len)
7120 || off >= total_bytes)
7121 return 0;
7122 if (off == -1)
7123 off = 0;
7124 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7126 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7128 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7129 bitpos += BITS_PER_UNIT)
7131 byte = (bitpos / BITS_PER_UNIT) & 3;
7132 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7134 if (UNITS_PER_WORD < 4)
7136 word = byte / UNITS_PER_WORD;
7137 if (WORDS_BIG_ENDIAN)
7138 word = (words - 1) - word;
7139 offset = word * UNITS_PER_WORD;
7140 if (BYTES_BIG_ENDIAN)
7141 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7142 else
7143 offset += byte % UNITS_PER_WORD;
7145 else
7147 offset = byte;
7148 if (BYTES_BIG_ENDIAN)
7150 /* Reverse bytes within each long, or within the entire float
7151 if it's smaller than a long (for HFmode). */
7152 offset = MIN (3, total_bytes - 1) - offset;
7153 gcc_assert (offset >= 0);
7156 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7157 if (offset >= off
7158 && offset - off < len)
7159 ptr[offset - off] = value;
7161 return MIN (len, total_bytes - off);
7164 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7165 specified by EXPR into the buffer PTR of length LEN bytes.
7166 Return the number of bytes placed in the buffer, or zero
7167 upon failure. */
7169 static int
7170 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7172 int rsize, isize;
7173 tree part;
7175 part = TREE_REALPART (expr);
7176 rsize = native_encode_expr (part, ptr, len, off);
7177 if (off == -1
7178 && rsize == 0)
7179 return 0;
7180 part = TREE_IMAGPART (expr);
7181 if (off != -1)
7182 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7183 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7184 if (off == -1
7185 && isize != rsize)
7186 return 0;
7187 return rsize + isize;
7191 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7192 specified by EXPR into the buffer PTR of length LEN bytes.
7193 Return the number of bytes placed in the buffer, or zero
7194 upon failure. */
7196 static int
7197 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7199 unsigned i, count;
7200 int size, offset;
7201 tree itype, elem;
7203 offset = 0;
7204 count = VECTOR_CST_NELTS (expr);
7205 itype = TREE_TYPE (TREE_TYPE (expr));
7206 size = GET_MODE_SIZE (TYPE_MODE (itype));
7207 for (i = 0; i < count; i++)
7209 if (off >= size)
7211 off -= size;
7212 continue;
7214 elem = VECTOR_CST_ELT (expr, i);
7215 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7216 if ((off == -1 && res != size)
7217 || res == 0)
7218 return 0;
7219 offset += res;
7220 if (offset >= len)
7221 return offset;
7222 if (off != -1)
7223 off = 0;
7225 return offset;
7229 /* Subroutine of native_encode_expr. Encode the STRING_CST
7230 specified by EXPR into the buffer PTR of length LEN bytes.
7231 Return the number of bytes placed in the buffer, or zero
7232 upon failure. */
7234 static int
7235 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7237 tree type = TREE_TYPE (expr);
7238 HOST_WIDE_INT total_bytes;
7240 if (TREE_CODE (type) != ARRAY_TYPE
7241 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7242 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7243 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7244 return 0;
7245 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7246 if ((off == -1 && total_bytes > len)
7247 || off >= total_bytes)
7248 return 0;
7249 if (off == -1)
7250 off = 0;
7251 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7253 int written = 0;
7254 if (off < TREE_STRING_LENGTH (expr))
7256 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7257 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7259 memset (ptr + written, 0,
7260 MIN (total_bytes - written, len - written));
7262 else
7263 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7264 return MIN (total_bytes - off, len);
7268 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7269 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7270 buffer PTR of length LEN bytes. If OFF is not -1 then start
7271 the encoding at byte offset OFF and encode at most LEN bytes.
7272 Return the number of bytes placed in the buffer, or zero upon failure. */
7275 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7277 /* We don't support starting at negative offset and -1 is special. */
7278 if (off < -1)
7279 return 0;
7281 switch (TREE_CODE (expr))
7283 case INTEGER_CST:
7284 return native_encode_int (expr, ptr, len, off);
7286 case REAL_CST:
7287 return native_encode_real (expr, ptr, len, off);
7289 case FIXED_CST:
7290 return native_encode_fixed (expr, ptr, len, off);
7292 case COMPLEX_CST:
7293 return native_encode_complex (expr, ptr, len, off);
7295 case VECTOR_CST:
7296 return native_encode_vector (expr, ptr, len, off);
7298 case STRING_CST:
7299 return native_encode_string (expr, ptr, len, off);
7301 default:
7302 return 0;
7307 /* Subroutine of native_interpret_expr. Interpret the contents of
7308 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7309 If the buffer cannot be interpreted, return NULL_TREE. */
7311 static tree
7312 native_interpret_int (tree type, const unsigned char *ptr, int len)
7314 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7316 if (total_bytes > len
7317 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7318 return NULL_TREE;
7320 wide_int result = wi::from_buffer (ptr, total_bytes);
7322 return wide_int_to_tree (type, result);
7326 /* Subroutine of native_interpret_expr. Interpret the contents of
7327 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7328 If the buffer cannot be interpreted, return NULL_TREE. */
7330 static tree
7331 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7333 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7334 double_int result;
7335 FIXED_VALUE_TYPE fixed_value;
7337 if (total_bytes > len
7338 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7339 return NULL_TREE;
7341 result = double_int::from_buffer (ptr, total_bytes);
7342 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7344 return build_fixed (type, fixed_value);
7348 /* Subroutine of native_interpret_expr. Interpret the contents of
7349 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7350 If the buffer cannot be interpreted, return NULL_TREE. */
7352 static tree
7353 native_interpret_real (tree type, const unsigned char *ptr, int len)
7355 machine_mode mode = TYPE_MODE (type);
7356 int total_bytes = GET_MODE_SIZE (mode);
7357 unsigned char value;
7358 /* There are always 32 bits in each long, no matter the size of
7359 the hosts long. We handle floating point representations with
7360 up to 192 bits. */
7361 REAL_VALUE_TYPE r;
7362 long tmp[6];
7364 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7365 if (total_bytes > len || total_bytes > 24)
7366 return NULL_TREE;
7367 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7369 memset (tmp, 0, sizeof (tmp));
7370 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7371 bitpos += BITS_PER_UNIT)
7373 /* Both OFFSET and BYTE index within a long;
7374 bitpos indexes the whole float. */
7375 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7376 if (UNITS_PER_WORD < 4)
7378 int word = byte / UNITS_PER_WORD;
7379 if (WORDS_BIG_ENDIAN)
7380 word = (words - 1) - word;
7381 offset = word * UNITS_PER_WORD;
7382 if (BYTES_BIG_ENDIAN)
7383 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7384 else
7385 offset += byte % UNITS_PER_WORD;
7387 else
7389 offset = byte;
7390 if (BYTES_BIG_ENDIAN)
7392 /* Reverse bytes within each long, or within the entire float
7393 if it's smaller than a long (for HFmode). */
7394 offset = MIN (3, total_bytes - 1) - offset;
7395 gcc_assert (offset >= 0);
7398 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7400 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7403 real_from_target (&r, tmp, mode);
7404 return build_real (type, r);
7408 /* Subroutine of native_interpret_expr. Interpret the contents of
7409 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7410 If the buffer cannot be interpreted, return NULL_TREE. */
7412 static tree
7413 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7415 tree etype, rpart, ipart;
7416 int size;
7418 etype = TREE_TYPE (type);
7419 size = GET_MODE_SIZE (TYPE_MODE (etype));
7420 if (size * 2 > len)
7421 return NULL_TREE;
7422 rpart = native_interpret_expr (etype, ptr, size);
7423 if (!rpart)
7424 return NULL_TREE;
7425 ipart = native_interpret_expr (etype, ptr+size, size);
7426 if (!ipart)
7427 return NULL_TREE;
7428 return build_complex (type, rpart, ipart);
7432 /* Subroutine of native_interpret_expr. Interpret the contents of
7433 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7434 If the buffer cannot be interpreted, return NULL_TREE. */
7436 static tree
7437 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7439 tree etype, elem;
7440 int i, size, count;
7441 tree *elements;
7443 etype = TREE_TYPE (type);
7444 size = GET_MODE_SIZE (TYPE_MODE (etype));
7445 count = TYPE_VECTOR_SUBPARTS (type);
7446 if (size * count > len)
7447 return NULL_TREE;
7449 elements = XALLOCAVEC (tree, count);
7450 for (i = count - 1; i >= 0; i--)
7452 elem = native_interpret_expr (etype, ptr+(i*size), size);
7453 if (!elem)
7454 return NULL_TREE;
7455 elements[i] = elem;
7457 return build_vector (type, elements);
7461 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7462 the buffer PTR of length LEN as a constant of type TYPE. For
7463 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7464 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7465 return NULL_TREE. */
7467 tree
7468 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7470 switch (TREE_CODE (type))
7472 case INTEGER_TYPE:
7473 case ENUMERAL_TYPE:
7474 case BOOLEAN_TYPE:
7475 case POINTER_TYPE:
7476 case REFERENCE_TYPE:
7477 return native_interpret_int (type, ptr, len);
7479 case REAL_TYPE:
7480 return native_interpret_real (type, ptr, len);
7482 case FIXED_POINT_TYPE:
7483 return native_interpret_fixed (type, ptr, len);
7485 case COMPLEX_TYPE:
7486 return native_interpret_complex (type, ptr, len);
7488 case VECTOR_TYPE:
7489 return native_interpret_vector (type, ptr, len);
7491 default:
7492 return NULL_TREE;
7496 /* Returns true if we can interpret the contents of a native encoding
7497 as TYPE. */
7499 static bool
7500 can_native_interpret_type_p (tree type)
7502 switch (TREE_CODE (type))
7504 case INTEGER_TYPE:
7505 case ENUMERAL_TYPE:
7506 case BOOLEAN_TYPE:
7507 case POINTER_TYPE:
7508 case REFERENCE_TYPE:
7509 case FIXED_POINT_TYPE:
7510 case REAL_TYPE:
7511 case COMPLEX_TYPE:
7512 case VECTOR_TYPE:
7513 return true;
7514 default:
7515 return false;
7519 /* Return true iff a constant of type TYPE is accepted by
7520 native_encode_expr. */
7522 bool
7523 can_native_encode_type_p (tree type)
7525 switch (TREE_CODE (type))
7527 case INTEGER_TYPE:
7528 case REAL_TYPE:
7529 case FIXED_POINT_TYPE:
7530 case COMPLEX_TYPE:
7531 case VECTOR_TYPE:
7532 case POINTER_TYPE:
7533 return true;
7534 default:
7535 return false;
7539 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7540 TYPE at compile-time. If we're unable to perform the conversion
7541 return NULL_TREE. */
7543 static tree
7544 fold_view_convert_expr (tree type, tree expr)
7546 /* We support up to 512-bit values (for V8DFmode). */
7547 unsigned char buffer[64];
7548 int len;
7550 /* Check that the host and target are sane. */
7551 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7552 return NULL_TREE;
7554 len = native_encode_expr (expr, buffer, sizeof (buffer));
7555 if (len == 0)
7556 return NULL_TREE;
7558 return native_interpret_expr (type, buffer, len);
7561 /* Build an expression for the address of T. Folds away INDIRECT_REF
7562 to avoid confusing the gimplify process. */
7564 tree
7565 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7567 /* The size of the object is not relevant when talking about its address. */
7568 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7569 t = TREE_OPERAND (t, 0);
7571 if (TREE_CODE (t) == INDIRECT_REF)
7573 t = TREE_OPERAND (t, 0);
7575 if (TREE_TYPE (t) != ptrtype)
7576 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7578 else if (TREE_CODE (t) == MEM_REF
7579 && integer_zerop (TREE_OPERAND (t, 1)))
7580 return TREE_OPERAND (t, 0);
7581 else if (TREE_CODE (t) == MEM_REF
7582 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7583 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7584 TREE_OPERAND (t, 0),
7585 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7586 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7588 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7590 if (TREE_TYPE (t) != ptrtype)
7591 t = fold_convert_loc (loc, ptrtype, t);
7593 else
7594 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7596 return t;
7599 /* Build an expression for the address of T. */
7601 tree
7602 build_fold_addr_expr_loc (location_t loc, tree t)
7604 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7606 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7609 /* Fold a unary expression of code CODE and type TYPE with operand
7610 OP0. Return the folded expression if folding is successful.
7611 Otherwise, return NULL_TREE. */
7613 tree
7614 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7616 tree tem;
7617 tree arg0;
7618 enum tree_code_class kind = TREE_CODE_CLASS (code);
7620 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7621 && TREE_CODE_LENGTH (code) == 1);
7623 arg0 = op0;
7624 if (arg0)
7626 if (CONVERT_EXPR_CODE_P (code)
7627 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7629 /* Don't use STRIP_NOPS, because signedness of argument type
7630 matters. */
7631 STRIP_SIGN_NOPS (arg0);
7633 else
7635 /* Strip any conversions that don't change the mode. This
7636 is safe for every expression, except for a comparison
7637 expression because its signedness is derived from its
7638 operands.
7640 Note that this is done as an internal manipulation within
7641 the constant folder, in order to find the simplest
7642 representation of the arguments so that their form can be
7643 studied. In any cases, the appropriate type conversions
7644 should be put back in the tree that will get out of the
7645 constant folder. */
7646 STRIP_NOPS (arg0);
7649 if (CONSTANT_CLASS_P (arg0))
7651 tree tem = const_unop (code, type, arg0);
7652 if (tem)
7654 if (TREE_TYPE (tem) != type)
7655 tem = fold_convert_loc (loc, type, tem);
7656 return tem;
7661 tem = generic_simplify (loc, code, type, op0);
7662 if (tem)
7663 return tem;
7665 if (TREE_CODE_CLASS (code) == tcc_unary)
7667 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7668 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7669 fold_build1_loc (loc, code, type,
7670 fold_convert_loc (loc, TREE_TYPE (op0),
7671 TREE_OPERAND (arg0, 1))));
7672 else if (TREE_CODE (arg0) == COND_EXPR)
7674 tree arg01 = TREE_OPERAND (arg0, 1);
7675 tree arg02 = TREE_OPERAND (arg0, 2);
7676 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7677 arg01 = fold_build1_loc (loc, code, type,
7678 fold_convert_loc (loc,
7679 TREE_TYPE (op0), arg01));
7680 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7681 arg02 = fold_build1_loc (loc, code, type,
7682 fold_convert_loc (loc,
7683 TREE_TYPE (op0), arg02));
7684 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7685 arg01, arg02);
7687 /* If this was a conversion, and all we did was to move into
7688 inside the COND_EXPR, bring it back out. But leave it if
7689 it is a conversion from integer to integer and the
7690 result precision is no wider than a word since such a
7691 conversion is cheap and may be optimized away by combine,
7692 while it couldn't if it were outside the COND_EXPR. Then return
7693 so we don't get into an infinite recursion loop taking the
7694 conversion out and then back in. */
7696 if ((CONVERT_EXPR_CODE_P (code)
7697 || code == NON_LVALUE_EXPR)
7698 && TREE_CODE (tem) == COND_EXPR
7699 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7700 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7701 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7702 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7703 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7704 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7705 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7706 && (INTEGRAL_TYPE_P
7707 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7708 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7709 || flag_syntax_only))
7710 tem = build1_loc (loc, code, type,
7711 build3 (COND_EXPR,
7712 TREE_TYPE (TREE_OPERAND
7713 (TREE_OPERAND (tem, 1), 0)),
7714 TREE_OPERAND (tem, 0),
7715 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7716 TREE_OPERAND (TREE_OPERAND (tem, 2),
7717 0)));
7718 return tem;
7722 switch (code)
7724 case NON_LVALUE_EXPR:
7725 if (!maybe_lvalue_p (op0))
7726 return fold_convert_loc (loc, type, op0);
7727 return NULL_TREE;
7729 CASE_CONVERT:
7730 case FLOAT_EXPR:
7731 case FIX_TRUNC_EXPR:
7732 if (COMPARISON_CLASS_P (op0))
7734 /* If we have (type) (a CMP b) and type is an integral type, return
7735 new expression involving the new type. Canonicalize
7736 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7737 non-integral type.
7738 Do not fold the result as that would not simplify further, also
7739 folding again results in recursions. */
7740 if (TREE_CODE (type) == BOOLEAN_TYPE)
7741 return build2_loc (loc, TREE_CODE (op0), type,
7742 TREE_OPERAND (op0, 0),
7743 TREE_OPERAND (op0, 1));
7744 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7745 && TREE_CODE (type) != VECTOR_TYPE)
7746 return build3_loc (loc, COND_EXPR, type, op0,
7747 constant_boolean_node (true, type),
7748 constant_boolean_node (false, type));
7751 /* Handle (T *)&A.B.C for A being of type T and B and C
7752 living at offset zero. This occurs frequently in
7753 C++ upcasting and then accessing the base. */
7754 if (TREE_CODE (op0) == ADDR_EXPR
7755 && POINTER_TYPE_P (type)
7756 && handled_component_p (TREE_OPERAND (op0, 0)))
7758 HOST_WIDE_INT bitsize, bitpos;
7759 tree offset;
7760 machine_mode mode;
7761 int unsignedp, reversep, volatilep;
7762 tree base
7763 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7764 &offset, &mode, &unsignedp, &reversep,
7765 &volatilep);
7766 /* If the reference was to a (constant) zero offset, we can use
7767 the address of the base if it has the same base type
7768 as the result type and the pointer type is unqualified. */
7769 if (! offset && bitpos == 0
7770 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7771 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7772 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7773 return fold_convert_loc (loc, type,
7774 build_fold_addr_expr_loc (loc, base));
7777 if (TREE_CODE (op0) == MODIFY_EXPR
7778 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7779 /* Detect assigning a bitfield. */
7780 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7781 && DECL_BIT_FIELD
7782 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7784 /* Don't leave an assignment inside a conversion
7785 unless assigning a bitfield. */
7786 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7787 /* First do the assignment, then return converted constant. */
7788 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7789 TREE_NO_WARNING (tem) = 1;
7790 TREE_USED (tem) = 1;
7791 return tem;
7794 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7795 constants (if x has signed type, the sign bit cannot be set
7796 in c). This folds extension into the BIT_AND_EXPR.
7797 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7798 very likely don't have maximal range for their precision and this
7799 transformation effectively doesn't preserve non-maximal ranges. */
7800 if (TREE_CODE (type) == INTEGER_TYPE
7801 && TREE_CODE (op0) == BIT_AND_EXPR
7802 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7804 tree and_expr = op0;
7805 tree and0 = TREE_OPERAND (and_expr, 0);
7806 tree and1 = TREE_OPERAND (and_expr, 1);
7807 int change = 0;
7809 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7810 || (TYPE_PRECISION (type)
7811 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7812 change = 1;
7813 else if (TYPE_PRECISION (TREE_TYPE (and1))
7814 <= HOST_BITS_PER_WIDE_INT
7815 && tree_fits_uhwi_p (and1))
7817 unsigned HOST_WIDE_INT cst;
7819 cst = tree_to_uhwi (and1);
7820 cst &= HOST_WIDE_INT_M1U
7821 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7822 change = (cst == 0);
7823 if (change
7824 && !flag_syntax_only
7825 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7826 == ZERO_EXTEND))
7828 tree uns = unsigned_type_for (TREE_TYPE (and0));
7829 and0 = fold_convert_loc (loc, uns, and0);
7830 and1 = fold_convert_loc (loc, uns, and1);
7833 if (change)
7835 tem = force_fit_type (type, wi::to_widest (and1), 0,
7836 TREE_OVERFLOW (and1));
7837 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7838 fold_convert_loc (loc, type, and0), tem);
7842 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7843 cast (T1)X will fold away. We assume that this happens when X itself
7844 is a cast. */
7845 if (POINTER_TYPE_P (type)
7846 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7847 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7849 tree arg00 = TREE_OPERAND (arg0, 0);
7850 tree arg01 = TREE_OPERAND (arg0, 1);
7852 return fold_build_pointer_plus_loc
7853 (loc, fold_convert_loc (loc, type, arg00), arg01);
7856 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7857 of the same precision, and X is an integer type not narrower than
7858 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7859 if (INTEGRAL_TYPE_P (type)
7860 && TREE_CODE (op0) == BIT_NOT_EXPR
7861 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7862 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7863 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7865 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7866 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7867 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7868 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7869 fold_convert_loc (loc, type, tem));
7872 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7873 type of X and Y (integer types only). */
7874 if (INTEGRAL_TYPE_P (type)
7875 && TREE_CODE (op0) == MULT_EXPR
7876 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7877 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7879 /* Be careful not to introduce new overflows. */
7880 tree mult_type;
7881 if (TYPE_OVERFLOW_WRAPS (type))
7882 mult_type = type;
7883 else
7884 mult_type = unsigned_type_for (type);
7886 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7888 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7889 fold_convert_loc (loc, mult_type,
7890 TREE_OPERAND (op0, 0)),
7891 fold_convert_loc (loc, mult_type,
7892 TREE_OPERAND (op0, 1)));
7893 return fold_convert_loc (loc, type, tem);
7897 return NULL_TREE;
7899 case VIEW_CONVERT_EXPR:
7900 if (TREE_CODE (op0) == MEM_REF)
7902 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7903 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7904 tem = fold_build2_loc (loc, MEM_REF, type,
7905 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7906 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7907 return tem;
7910 return NULL_TREE;
7912 case NEGATE_EXPR:
7913 tem = fold_negate_expr (loc, arg0);
7914 if (tem)
7915 return fold_convert_loc (loc, type, tem);
7916 return NULL_TREE;
7918 case ABS_EXPR:
7919 /* Convert fabs((double)float) into (double)fabsf(float). */
7920 if (TREE_CODE (arg0) == NOP_EXPR
7921 && TREE_CODE (type) == REAL_TYPE)
7923 tree targ0 = strip_float_extensions (arg0);
7924 if (targ0 != arg0)
7925 return fold_convert_loc (loc, type,
7926 fold_build1_loc (loc, ABS_EXPR,
7927 TREE_TYPE (targ0),
7928 targ0));
7930 return NULL_TREE;
7932 case BIT_NOT_EXPR:
7933 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7934 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7935 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7936 fold_convert_loc (loc, type,
7937 TREE_OPERAND (arg0, 0)))))
7938 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7939 fold_convert_loc (loc, type,
7940 TREE_OPERAND (arg0, 1)));
7941 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7942 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7943 fold_convert_loc (loc, type,
7944 TREE_OPERAND (arg0, 1)))))
7945 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7946 fold_convert_loc (loc, type,
7947 TREE_OPERAND (arg0, 0)), tem);
7949 return NULL_TREE;
7951 case TRUTH_NOT_EXPR:
7952 /* Note that the operand of this must be an int
7953 and its values must be 0 or 1.
7954 ("true" is a fixed value perhaps depending on the language,
7955 but we don't handle values other than 1 correctly yet.) */
7956 tem = fold_truth_not_expr (loc, arg0);
7957 if (!tem)
7958 return NULL_TREE;
7959 return fold_convert_loc (loc, type, tem);
7961 case INDIRECT_REF:
7962 /* Fold *&X to X if X is an lvalue. */
7963 if (TREE_CODE (op0) == ADDR_EXPR)
7965 tree op00 = TREE_OPERAND (op0, 0);
7966 if ((VAR_P (op00)
7967 || TREE_CODE (op00) == PARM_DECL
7968 || TREE_CODE (op00) == RESULT_DECL)
7969 && !TREE_READONLY (op00))
7970 return op00;
7972 return NULL_TREE;
7974 default:
7975 return NULL_TREE;
7976 } /* switch (code) */
7980 /* If the operation was a conversion do _not_ mark a resulting constant
7981 with TREE_OVERFLOW if the original constant was not. These conversions
7982 have implementation defined behavior and retaining the TREE_OVERFLOW
7983 flag here would confuse later passes such as VRP. */
7984 tree
7985 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7986 tree type, tree op0)
7988 tree res = fold_unary_loc (loc, code, type, op0);
7989 if (res
7990 && TREE_CODE (res) == INTEGER_CST
7991 && TREE_CODE (op0) == INTEGER_CST
7992 && CONVERT_EXPR_CODE_P (code))
7993 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7995 return res;
7998 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7999 operands OP0 and OP1. LOC is the location of the resulting expression.
8000 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8001 Return the folded expression if folding is successful. Otherwise,
8002 return NULL_TREE. */
8003 static tree
8004 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8005 tree arg0, tree arg1, tree op0, tree op1)
8007 tree tem;
8009 /* We only do these simplifications if we are optimizing. */
8010 if (!optimize)
8011 return NULL_TREE;
8013 /* Check for things like (A || B) && (A || C). We can convert this
8014 to A || (B && C). Note that either operator can be any of the four
8015 truth and/or operations and the transformation will still be
8016 valid. Also note that we only care about order for the
8017 ANDIF and ORIF operators. If B contains side effects, this
8018 might change the truth-value of A. */
8019 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8020 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8021 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8022 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8023 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8024 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8026 tree a00 = TREE_OPERAND (arg0, 0);
8027 tree a01 = TREE_OPERAND (arg0, 1);
8028 tree a10 = TREE_OPERAND (arg1, 0);
8029 tree a11 = TREE_OPERAND (arg1, 1);
8030 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8031 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8032 && (code == TRUTH_AND_EXPR
8033 || code == TRUTH_OR_EXPR));
8035 if (operand_equal_p (a00, a10, 0))
8036 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8037 fold_build2_loc (loc, code, type, a01, a11));
8038 else if (commutative && operand_equal_p (a00, a11, 0))
8039 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8040 fold_build2_loc (loc, code, type, a01, a10));
8041 else if (commutative && operand_equal_p (a01, a10, 0))
8042 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8043 fold_build2_loc (loc, code, type, a00, a11));
8045 /* This case if tricky because we must either have commutative
8046 operators or else A10 must not have side-effects. */
8048 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8049 && operand_equal_p (a01, a11, 0))
8050 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8051 fold_build2_loc (loc, code, type, a00, a10),
8052 a01);
8055 /* See if we can build a range comparison. */
8056 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8057 return tem;
8059 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8060 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8062 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8063 if (tem)
8064 return fold_build2_loc (loc, code, type, tem, arg1);
8067 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8068 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8070 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8071 if (tem)
8072 return fold_build2_loc (loc, code, type, arg0, tem);
8075 /* Check for the possibility of merging component references. If our
8076 lhs is another similar operation, try to merge its rhs with our
8077 rhs. Then try to merge our lhs and rhs. */
8078 if (TREE_CODE (arg0) == code
8079 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8080 TREE_OPERAND (arg0, 1), arg1)))
8081 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8083 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8084 return tem;
8086 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8087 && (code == TRUTH_AND_EXPR
8088 || code == TRUTH_ANDIF_EXPR
8089 || code == TRUTH_OR_EXPR
8090 || code == TRUTH_ORIF_EXPR))
8092 enum tree_code ncode, icode;
8094 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8095 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8096 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8098 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8099 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8100 We don't want to pack more than two leafs to a non-IF AND/OR
8101 expression.
8102 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8103 equal to IF-CODE, then we don't want to add right-hand operand.
8104 If the inner right-hand side of left-hand operand has
8105 side-effects, or isn't simple, then we can't add to it,
8106 as otherwise we might destroy if-sequence. */
8107 if (TREE_CODE (arg0) == icode
8108 && simple_operand_p_2 (arg1)
8109 /* Needed for sequence points to handle trappings, and
8110 side-effects. */
8111 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8113 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8114 arg1);
8115 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8116 tem);
8118 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8119 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8120 else if (TREE_CODE (arg1) == icode
8121 && simple_operand_p_2 (arg0)
8122 /* Needed for sequence points to handle trappings, and
8123 side-effects. */
8124 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8126 tem = fold_build2_loc (loc, ncode, type,
8127 arg0, TREE_OPERAND (arg1, 0));
8128 return fold_build2_loc (loc, icode, type, tem,
8129 TREE_OPERAND (arg1, 1));
8131 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8132 into (A OR B).
8133 For sequence point consistancy, we need to check for trapping,
8134 and side-effects. */
8135 else if (code == icode && simple_operand_p_2 (arg0)
8136 && simple_operand_p_2 (arg1))
8137 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8140 return NULL_TREE;
8143 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8144 by changing CODE to reduce the magnitude of constants involved in
8145 ARG0 of the comparison.
8146 Returns a canonicalized comparison tree if a simplification was
8147 possible, otherwise returns NULL_TREE.
8148 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8149 valid if signed overflow is undefined. */
8151 static tree
8152 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8153 tree arg0, tree arg1,
8154 bool *strict_overflow_p)
8156 enum tree_code code0 = TREE_CODE (arg0);
8157 tree t, cst0 = NULL_TREE;
8158 int sgn0;
8160 /* Match A +- CST code arg1. We can change this only if overflow
8161 is undefined. */
8162 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8163 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8164 /* In principle pointers also have undefined overflow behavior,
8165 but that causes problems elsewhere. */
8166 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8167 && (code0 == MINUS_EXPR
8168 || code0 == PLUS_EXPR)
8169 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8170 return NULL_TREE;
8172 /* Identify the constant in arg0 and its sign. */
8173 cst0 = TREE_OPERAND (arg0, 1);
8174 sgn0 = tree_int_cst_sgn (cst0);
8176 /* Overflowed constants and zero will cause problems. */
8177 if (integer_zerop (cst0)
8178 || TREE_OVERFLOW (cst0))
8179 return NULL_TREE;
8181 /* See if we can reduce the magnitude of the constant in
8182 arg0 by changing the comparison code. */
8183 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8184 if (code == LT_EXPR
8185 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8186 code = LE_EXPR;
8187 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8188 else if (code == GT_EXPR
8189 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8190 code = GE_EXPR;
8191 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8192 else if (code == LE_EXPR
8193 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8194 code = LT_EXPR;
8195 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8196 else if (code == GE_EXPR
8197 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8198 code = GT_EXPR;
8199 else
8200 return NULL_TREE;
8201 *strict_overflow_p = true;
8203 /* Now build the constant reduced in magnitude. But not if that
8204 would produce one outside of its types range. */
8205 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8206 && ((sgn0 == 1
8207 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8208 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8209 || (sgn0 == -1
8210 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8211 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8212 return NULL_TREE;
8214 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8215 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8216 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8217 t = fold_convert (TREE_TYPE (arg1), t);
8219 return fold_build2_loc (loc, code, type, t, arg1);
8222 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8223 overflow further. Try to decrease the magnitude of constants involved
8224 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8225 and put sole constants at the second argument position.
8226 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8228 static tree
8229 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8230 tree arg0, tree arg1)
8232 tree t;
8233 bool strict_overflow_p;
8234 const char * const warnmsg = G_("assuming signed overflow does not occur "
8235 "when reducing constant in comparison");
8237 /* Try canonicalization by simplifying arg0. */
8238 strict_overflow_p = false;
8239 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8240 &strict_overflow_p);
8241 if (t)
8243 if (strict_overflow_p)
8244 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8245 return t;
8248 /* Try canonicalization by simplifying arg1 using the swapped
8249 comparison. */
8250 code = swap_tree_comparison (code);
8251 strict_overflow_p = false;
8252 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8253 &strict_overflow_p);
8254 if (t && strict_overflow_p)
8255 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8256 return t;
8259 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8260 space. This is used to avoid issuing overflow warnings for
8261 expressions like &p->x which can not wrap. */
8263 static bool
8264 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8266 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8267 return true;
8269 if (bitpos < 0)
8270 return true;
8272 wide_int wi_offset;
8273 int precision = TYPE_PRECISION (TREE_TYPE (base));
8274 if (offset == NULL_TREE)
8275 wi_offset = wi::zero (precision);
8276 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8277 return true;
8278 else
8279 wi_offset = offset;
8281 bool overflow;
8282 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8283 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8284 if (overflow)
8285 return true;
8287 if (!wi::fits_uhwi_p (total))
8288 return true;
8290 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8291 if (size <= 0)
8292 return true;
8294 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8295 array. */
8296 if (TREE_CODE (base) == ADDR_EXPR)
8298 HOST_WIDE_INT base_size;
8300 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8301 if (base_size > 0 && size < base_size)
8302 size = base_size;
8305 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8308 /* Return a positive integer when the symbol DECL is known to have
8309 a nonzero address, zero when it's known not to (e.g., it's a weak
8310 symbol), and a negative integer when the symbol is not yet in the
8311 symbol table and so whether or not its address is zero is unknown. */
8312 static int
8313 maybe_nonzero_address (tree decl)
8315 if (DECL_P (decl) && decl_in_symtab_p (decl))
8316 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8317 return symbol->nonzero_address ();
8319 return -1;
8322 /* Subroutine of fold_binary. This routine performs all of the
8323 transformations that are common to the equality/inequality
8324 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8325 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8326 fold_binary should call fold_binary. Fold a comparison with
8327 tree code CODE and type TYPE with operands OP0 and OP1. Return
8328 the folded comparison or NULL_TREE. */
8330 static tree
8331 fold_comparison (location_t loc, enum tree_code code, tree type,
8332 tree op0, tree op1)
8334 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8335 tree arg0, arg1, tem;
8337 arg0 = op0;
8338 arg1 = op1;
8340 STRIP_SIGN_NOPS (arg0);
8341 STRIP_SIGN_NOPS (arg1);
8343 /* For comparisons of pointers we can decompose it to a compile time
8344 comparison of the base objects and the offsets into the object.
8345 This requires at least one operand being an ADDR_EXPR or a
8346 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8347 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8348 && (TREE_CODE (arg0) == ADDR_EXPR
8349 || TREE_CODE (arg1) == ADDR_EXPR
8350 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8351 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8353 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8354 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8355 machine_mode mode;
8356 int volatilep, reversep, unsignedp;
8357 bool indirect_base0 = false, indirect_base1 = false;
8359 /* Get base and offset for the access. Strip ADDR_EXPR for
8360 get_inner_reference, but put it back by stripping INDIRECT_REF
8361 off the base object if possible. indirect_baseN will be true
8362 if baseN is not an address but refers to the object itself. */
8363 base0 = arg0;
8364 if (TREE_CODE (arg0) == ADDR_EXPR)
8366 base0
8367 = get_inner_reference (TREE_OPERAND (arg0, 0),
8368 &bitsize, &bitpos0, &offset0, &mode,
8369 &unsignedp, &reversep, &volatilep);
8370 if (TREE_CODE (base0) == INDIRECT_REF)
8371 base0 = TREE_OPERAND (base0, 0);
8372 else
8373 indirect_base0 = true;
8375 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8377 base0 = TREE_OPERAND (arg0, 0);
8378 STRIP_SIGN_NOPS (base0);
8379 if (TREE_CODE (base0) == ADDR_EXPR)
8381 base0
8382 = get_inner_reference (TREE_OPERAND (base0, 0),
8383 &bitsize, &bitpos0, &offset0, &mode,
8384 &unsignedp, &reversep, &volatilep);
8385 if (TREE_CODE (base0) == INDIRECT_REF)
8386 base0 = TREE_OPERAND (base0, 0);
8387 else
8388 indirect_base0 = true;
8390 if (offset0 == NULL_TREE || integer_zerop (offset0))
8391 offset0 = TREE_OPERAND (arg0, 1);
8392 else
8393 offset0 = size_binop (PLUS_EXPR, offset0,
8394 TREE_OPERAND (arg0, 1));
8395 if (TREE_CODE (offset0) == INTEGER_CST)
8397 offset_int tem = wi::sext (wi::to_offset (offset0),
8398 TYPE_PRECISION (sizetype));
8399 tem <<= LOG2_BITS_PER_UNIT;
8400 tem += bitpos0;
8401 if (wi::fits_shwi_p (tem))
8403 bitpos0 = tem.to_shwi ();
8404 offset0 = NULL_TREE;
8409 base1 = arg1;
8410 if (TREE_CODE (arg1) == ADDR_EXPR)
8412 base1
8413 = get_inner_reference (TREE_OPERAND (arg1, 0),
8414 &bitsize, &bitpos1, &offset1, &mode,
8415 &unsignedp, &reversep, &volatilep);
8416 if (TREE_CODE (base1) == INDIRECT_REF)
8417 base1 = TREE_OPERAND (base1, 0);
8418 else
8419 indirect_base1 = true;
8421 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8423 base1 = TREE_OPERAND (arg1, 0);
8424 STRIP_SIGN_NOPS (base1);
8425 if (TREE_CODE (base1) == ADDR_EXPR)
8427 base1
8428 = get_inner_reference (TREE_OPERAND (base1, 0),
8429 &bitsize, &bitpos1, &offset1, &mode,
8430 &unsignedp, &reversep, &volatilep);
8431 if (TREE_CODE (base1) == INDIRECT_REF)
8432 base1 = TREE_OPERAND (base1, 0);
8433 else
8434 indirect_base1 = true;
8436 if (offset1 == NULL_TREE || integer_zerop (offset1))
8437 offset1 = TREE_OPERAND (arg1, 1);
8438 else
8439 offset1 = size_binop (PLUS_EXPR, offset1,
8440 TREE_OPERAND (arg1, 1));
8441 if (TREE_CODE (offset1) == INTEGER_CST)
8443 offset_int tem = wi::sext (wi::to_offset (offset1),
8444 TYPE_PRECISION (sizetype));
8445 tem <<= LOG2_BITS_PER_UNIT;
8446 tem += bitpos1;
8447 if (wi::fits_shwi_p (tem))
8449 bitpos1 = tem.to_shwi ();
8450 offset1 = NULL_TREE;
8455 /* If we have equivalent bases we might be able to simplify. */
8456 if (indirect_base0 == indirect_base1
8457 && operand_equal_p (base0, base1,
8458 indirect_base0 ? OEP_ADDRESS_OF : 0))
8460 /* We can fold this expression to a constant if the non-constant
8461 offset parts are equal. */
8462 if ((offset0 == offset1
8463 || (offset0 && offset1
8464 && operand_equal_p (offset0, offset1, 0)))
8465 && (equality_code
8466 || (indirect_base0
8467 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8468 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8471 if (!equality_code
8472 && bitpos0 != bitpos1
8473 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8474 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8475 fold_overflow_warning (("assuming pointer wraparound does not "
8476 "occur when comparing P +- C1 with "
8477 "P +- C2"),
8478 WARN_STRICT_OVERFLOW_CONDITIONAL);
8480 switch (code)
8482 case EQ_EXPR:
8483 return constant_boolean_node (bitpos0 == bitpos1, type);
8484 case NE_EXPR:
8485 return constant_boolean_node (bitpos0 != bitpos1, type);
8486 case LT_EXPR:
8487 return constant_boolean_node (bitpos0 < bitpos1, type);
8488 case LE_EXPR:
8489 return constant_boolean_node (bitpos0 <= bitpos1, type);
8490 case GE_EXPR:
8491 return constant_boolean_node (bitpos0 >= bitpos1, type);
8492 case GT_EXPR:
8493 return constant_boolean_node (bitpos0 > bitpos1, type);
8494 default:;
8497 /* We can simplify the comparison to a comparison of the variable
8498 offset parts if the constant offset parts are equal.
8499 Be careful to use signed sizetype here because otherwise we
8500 mess with array offsets in the wrong way. This is possible
8501 because pointer arithmetic is restricted to retain within an
8502 object and overflow on pointer differences is undefined as of
8503 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8504 else if (bitpos0 == bitpos1
8505 && (equality_code
8506 || (indirect_base0
8507 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8508 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8510 /* By converting to signed sizetype we cover middle-end pointer
8511 arithmetic which operates on unsigned pointer types of size
8512 type size and ARRAY_REF offsets which are properly sign or
8513 zero extended from their type in case it is narrower than
8514 sizetype. */
8515 if (offset0 == NULL_TREE)
8516 offset0 = build_int_cst (ssizetype, 0);
8517 else
8518 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8519 if (offset1 == NULL_TREE)
8520 offset1 = build_int_cst (ssizetype, 0);
8521 else
8522 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8524 if (!equality_code
8525 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8526 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8527 fold_overflow_warning (("assuming pointer wraparound does not "
8528 "occur when comparing P +- C1 with "
8529 "P +- C2"),
8530 WARN_STRICT_OVERFLOW_COMPARISON);
8532 return fold_build2_loc (loc, code, type, offset0, offset1);
8535 /* For equal offsets we can simplify to a comparison of the
8536 base addresses. */
8537 else if (bitpos0 == bitpos1
8538 && (indirect_base0
8539 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8540 && (indirect_base1
8541 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8542 && ((offset0 == offset1)
8543 || (offset0 && offset1
8544 && operand_equal_p (offset0, offset1, 0))))
8546 if (indirect_base0)
8547 base0 = build_fold_addr_expr_loc (loc, base0);
8548 if (indirect_base1)
8549 base1 = build_fold_addr_expr_loc (loc, base1);
8550 return fold_build2_loc (loc, code, type, base0, base1);
8552 /* Comparison between an ordinary (non-weak) symbol and a null
8553 pointer can be eliminated since such symbols must have a non
8554 null address. In C, relational expressions between pointers
8555 to objects and null pointers are undefined. The results
8556 below follow the C++ rules with the additional property that
8557 every object pointer compares greater than a null pointer.
8559 else if (DECL_P (base0)
8560 && maybe_nonzero_address (base0) > 0
8561 /* Avoid folding references to struct members at offset 0 to
8562 prevent tests like '&ptr->firstmember == 0' from getting
8563 eliminated. When ptr is null, although the -> expression
8564 is strictly speaking invalid, GCC retains it as a matter
8565 of QoI. See PR c/44555. */
8566 && (offset0 == NULL_TREE && bitpos0 != 0)
8567 /* The caller guarantees that when one of the arguments is
8568 constant (i.e., null in this case) it is second. */
8569 && integer_zerop (arg1))
8571 switch (code)
8573 case EQ_EXPR:
8574 case LE_EXPR:
8575 case LT_EXPR:
8576 return constant_boolean_node (false, type);
8577 case GE_EXPR:
8578 case GT_EXPR:
8579 case NE_EXPR:
8580 return constant_boolean_node (true, type);
8581 default:
8582 gcc_unreachable ();
8587 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8588 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8589 the resulting offset is smaller in absolute value than the
8590 original one and has the same sign. */
8591 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8592 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8593 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8594 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8595 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8596 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8597 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8598 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8600 tree const1 = TREE_OPERAND (arg0, 1);
8601 tree const2 = TREE_OPERAND (arg1, 1);
8602 tree variable1 = TREE_OPERAND (arg0, 0);
8603 tree variable2 = TREE_OPERAND (arg1, 0);
8604 tree cst;
8605 const char * const warnmsg = G_("assuming signed overflow does not "
8606 "occur when combining constants around "
8607 "a comparison");
8609 /* Put the constant on the side where it doesn't overflow and is
8610 of lower absolute value and of same sign than before. */
8611 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8612 ? MINUS_EXPR : PLUS_EXPR,
8613 const2, const1);
8614 if (!TREE_OVERFLOW (cst)
8615 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8616 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8618 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8619 return fold_build2_loc (loc, code, type,
8620 variable1,
8621 fold_build2_loc (loc, TREE_CODE (arg1),
8622 TREE_TYPE (arg1),
8623 variable2, cst));
8626 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8627 ? MINUS_EXPR : PLUS_EXPR,
8628 const1, const2);
8629 if (!TREE_OVERFLOW (cst)
8630 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8631 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8633 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8634 return fold_build2_loc (loc, code, type,
8635 fold_build2_loc (loc, TREE_CODE (arg0),
8636 TREE_TYPE (arg0),
8637 variable1, cst),
8638 variable2);
8642 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8643 if (tem)
8644 return tem;
8646 /* If we are comparing an expression that just has comparisons
8647 of two integer values, arithmetic expressions of those comparisons,
8648 and constants, we can simplify it. There are only three cases
8649 to check: the two values can either be equal, the first can be
8650 greater, or the second can be greater. Fold the expression for
8651 those three values. Since each value must be 0 or 1, we have
8652 eight possibilities, each of which corresponds to the constant 0
8653 or 1 or one of the six possible comparisons.
8655 This handles common cases like (a > b) == 0 but also handles
8656 expressions like ((x > y) - (y > x)) > 0, which supposedly
8657 occur in macroized code. */
8659 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8661 tree cval1 = 0, cval2 = 0;
8662 int save_p = 0;
8664 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8665 /* Don't handle degenerate cases here; they should already
8666 have been handled anyway. */
8667 && cval1 != 0 && cval2 != 0
8668 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8669 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8670 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8671 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8672 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8673 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8674 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8676 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8677 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8679 /* We can't just pass T to eval_subst in case cval1 or cval2
8680 was the same as ARG1. */
8682 tree high_result
8683 = fold_build2_loc (loc, code, type,
8684 eval_subst (loc, arg0, cval1, maxval,
8685 cval2, minval),
8686 arg1);
8687 tree equal_result
8688 = fold_build2_loc (loc, code, type,
8689 eval_subst (loc, arg0, cval1, maxval,
8690 cval2, maxval),
8691 arg1);
8692 tree low_result
8693 = fold_build2_loc (loc, code, type,
8694 eval_subst (loc, arg0, cval1, minval,
8695 cval2, maxval),
8696 arg1);
8698 /* All three of these results should be 0 or 1. Confirm they are.
8699 Then use those values to select the proper code to use. */
8701 if (TREE_CODE (high_result) == INTEGER_CST
8702 && TREE_CODE (equal_result) == INTEGER_CST
8703 && TREE_CODE (low_result) == INTEGER_CST)
8705 /* Make a 3-bit mask with the high-order bit being the
8706 value for `>', the next for '=', and the low for '<'. */
8707 switch ((integer_onep (high_result) * 4)
8708 + (integer_onep (equal_result) * 2)
8709 + integer_onep (low_result))
8711 case 0:
8712 /* Always false. */
8713 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8714 case 1:
8715 code = LT_EXPR;
8716 break;
8717 case 2:
8718 code = EQ_EXPR;
8719 break;
8720 case 3:
8721 code = LE_EXPR;
8722 break;
8723 case 4:
8724 code = GT_EXPR;
8725 break;
8726 case 5:
8727 code = NE_EXPR;
8728 break;
8729 case 6:
8730 code = GE_EXPR;
8731 break;
8732 case 7:
8733 /* Always true. */
8734 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8737 if (save_p)
8739 tem = save_expr (build2 (code, type, cval1, cval2));
8740 SET_EXPR_LOCATION (tem, loc);
8741 return tem;
8743 return fold_build2_loc (loc, code, type, cval1, cval2);
8748 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8749 into a single range test. */
8750 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8751 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8752 && TREE_CODE (arg1) == INTEGER_CST
8753 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8754 && !integer_zerop (TREE_OPERAND (arg0, 1))
8755 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8756 && !TREE_OVERFLOW (arg1))
8758 tem = fold_div_compare (loc, code, type, arg0, arg1);
8759 if (tem != NULL_TREE)
8760 return tem;
8763 return NULL_TREE;
8767 /* Subroutine of fold_binary. Optimize complex multiplications of the
8768 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8769 argument EXPR represents the expression "z" of type TYPE. */
8771 static tree
8772 fold_mult_zconjz (location_t loc, tree type, tree expr)
8774 tree itype = TREE_TYPE (type);
8775 tree rpart, ipart, tem;
8777 if (TREE_CODE (expr) == COMPLEX_EXPR)
8779 rpart = TREE_OPERAND (expr, 0);
8780 ipart = TREE_OPERAND (expr, 1);
8782 else if (TREE_CODE (expr) == COMPLEX_CST)
8784 rpart = TREE_REALPART (expr);
8785 ipart = TREE_IMAGPART (expr);
8787 else
8789 expr = save_expr (expr);
8790 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8791 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8794 rpart = save_expr (rpart);
8795 ipart = save_expr (ipart);
8796 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8797 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8798 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8799 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8800 build_zero_cst (itype));
8804 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8805 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8807 static bool
8808 vec_cst_ctor_to_array (tree arg, tree *elts)
8810 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8812 if (TREE_CODE (arg) == VECTOR_CST)
8814 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8815 elts[i] = VECTOR_CST_ELT (arg, i);
8817 else if (TREE_CODE (arg) == CONSTRUCTOR)
8819 constructor_elt *elt;
8821 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8822 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8823 return false;
8824 else
8825 elts[i] = elt->value;
8827 else
8828 return false;
8829 for (; i < nelts; i++)
8830 elts[i]
8831 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8832 return true;
8835 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8836 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8837 NULL_TREE otherwise. */
8839 static tree
8840 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8842 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8843 tree *elts;
8844 bool need_ctor = false;
8846 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8847 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8848 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8849 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8850 return NULL_TREE;
8852 elts = XALLOCAVEC (tree, nelts * 3);
8853 if (!vec_cst_ctor_to_array (arg0, elts)
8854 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8855 return NULL_TREE;
8857 for (i = 0; i < nelts; i++)
8859 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8860 need_ctor = true;
8861 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8864 if (need_ctor)
8866 vec<constructor_elt, va_gc> *v;
8867 vec_alloc (v, nelts);
8868 for (i = 0; i < nelts; i++)
8869 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8870 return build_constructor (type, v);
8872 else
8873 return build_vector (type, &elts[2 * nelts]);
8876 /* Try to fold a pointer difference of type TYPE two address expressions of
8877 array references AREF0 and AREF1 using location LOC. Return a
8878 simplified expression for the difference or NULL_TREE. */
8880 static tree
8881 fold_addr_of_array_ref_difference (location_t loc, tree type,
8882 tree aref0, tree aref1)
8884 tree base0 = TREE_OPERAND (aref0, 0);
8885 tree base1 = TREE_OPERAND (aref1, 0);
8886 tree base_offset = build_int_cst (type, 0);
8888 /* If the bases are array references as well, recurse. If the bases
8889 are pointer indirections compute the difference of the pointers.
8890 If the bases are equal, we are set. */
8891 if ((TREE_CODE (base0) == ARRAY_REF
8892 && TREE_CODE (base1) == ARRAY_REF
8893 && (base_offset
8894 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8895 || (INDIRECT_REF_P (base0)
8896 && INDIRECT_REF_P (base1)
8897 && (base_offset
8898 = fold_binary_loc (loc, MINUS_EXPR, type,
8899 fold_convert (type, TREE_OPERAND (base0, 0)),
8900 fold_convert (type,
8901 TREE_OPERAND (base1, 0)))))
8902 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8904 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8905 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8906 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8907 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8908 return fold_build2_loc (loc, PLUS_EXPR, type,
8909 base_offset,
8910 fold_build2_loc (loc, MULT_EXPR, type,
8911 diff, esz));
8913 return NULL_TREE;
8916 /* If the real or vector real constant CST of type TYPE has an exact
8917 inverse, return it, else return NULL. */
8919 tree
8920 exact_inverse (tree type, tree cst)
8922 REAL_VALUE_TYPE r;
8923 tree unit_type, *elts;
8924 machine_mode mode;
8925 unsigned vec_nelts, i;
8927 switch (TREE_CODE (cst))
8929 case REAL_CST:
8930 r = TREE_REAL_CST (cst);
8932 if (exact_real_inverse (TYPE_MODE (type), &r))
8933 return build_real (type, r);
8935 return NULL_TREE;
8937 case VECTOR_CST:
8938 vec_nelts = VECTOR_CST_NELTS (cst);
8939 elts = XALLOCAVEC (tree, vec_nelts);
8940 unit_type = TREE_TYPE (type);
8941 mode = TYPE_MODE (unit_type);
8943 for (i = 0; i < vec_nelts; i++)
8945 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8946 if (!exact_real_inverse (mode, &r))
8947 return NULL_TREE;
8948 elts[i] = build_real (unit_type, r);
8951 return build_vector (type, elts);
8953 default:
8954 return NULL_TREE;
8958 /* Mask out the tz least significant bits of X of type TYPE where
8959 tz is the number of trailing zeroes in Y. */
8960 static wide_int
8961 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8963 int tz = wi::ctz (y);
8964 if (tz > 0)
8965 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8966 return x;
8969 /* Return true when T is an address and is known to be nonzero.
8970 For floating point we further ensure that T is not denormal.
8971 Similar logic is present in nonzero_address in rtlanal.h.
8973 If the return value is based on the assumption that signed overflow
8974 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8975 change *STRICT_OVERFLOW_P. */
8977 static bool
8978 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8980 tree type = TREE_TYPE (t);
8981 enum tree_code code;
8983 /* Doing something useful for floating point would need more work. */
8984 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8985 return false;
8987 code = TREE_CODE (t);
8988 switch (TREE_CODE_CLASS (code))
8990 case tcc_unary:
8991 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8992 strict_overflow_p);
8993 case tcc_binary:
8994 case tcc_comparison:
8995 return tree_binary_nonzero_warnv_p (code, type,
8996 TREE_OPERAND (t, 0),
8997 TREE_OPERAND (t, 1),
8998 strict_overflow_p);
8999 case tcc_constant:
9000 case tcc_declaration:
9001 case tcc_reference:
9002 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9004 default:
9005 break;
9008 switch (code)
9010 case TRUTH_NOT_EXPR:
9011 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9012 strict_overflow_p);
9014 case TRUTH_AND_EXPR:
9015 case TRUTH_OR_EXPR:
9016 case TRUTH_XOR_EXPR:
9017 return tree_binary_nonzero_warnv_p (code, type,
9018 TREE_OPERAND (t, 0),
9019 TREE_OPERAND (t, 1),
9020 strict_overflow_p);
9022 case COND_EXPR:
9023 case CONSTRUCTOR:
9024 case OBJ_TYPE_REF:
9025 case ASSERT_EXPR:
9026 case ADDR_EXPR:
9027 case WITH_SIZE_EXPR:
9028 case SSA_NAME:
9029 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9031 case COMPOUND_EXPR:
9032 case MODIFY_EXPR:
9033 case BIND_EXPR:
9034 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9035 strict_overflow_p);
9037 case SAVE_EXPR:
9038 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9039 strict_overflow_p);
9041 case CALL_EXPR:
9043 tree fndecl = get_callee_fndecl (t);
9044 if (!fndecl) return false;
9045 if (flag_delete_null_pointer_checks && !flag_check_new
9046 && DECL_IS_OPERATOR_NEW (fndecl)
9047 && !TREE_NOTHROW (fndecl))
9048 return true;
9049 if (flag_delete_null_pointer_checks
9050 && lookup_attribute ("returns_nonnull",
9051 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9052 return true;
9053 return alloca_call_p (t);
9056 default:
9057 break;
9059 return false;
9062 /* Return true when T is an address and is known to be nonzero.
9063 Handle warnings about undefined signed overflow. */
9065 static bool
9066 tree_expr_nonzero_p (tree t)
9068 bool ret, strict_overflow_p;
9070 strict_overflow_p = false;
9071 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9072 if (strict_overflow_p)
9073 fold_overflow_warning (("assuming signed overflow does not occur when "
9074 "determining that expression is always "
9075 "non-zero"),
9076 WARN_STRICT_OVERFLOW_MISC);
9077 return ret;
9080 /* Return true if T is known not to be equal to an integer W. */
9082 bool
9083 expr_not_equal_to (tree t, const wide_int &w)
9085 wide_int min, max, nz;
9086 value_range_type rtype;
9087 switch (TREE_CODE (t))
9089 case INTEGER_CST:
9090 return wi::ne_p (t, w);
9092 case SSA_NAME:
9093 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9094 return false;
9095 rtype = get_range_info (t, &min, &max);
9096 if (rtype == VR_RANGE)
9098 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9099 return true;
9100 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9101 return true;
9103 else if (rtype == VR_ANTI_RANGE
9104 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9105 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9106 return true;
9107 /* If T has some known zero bits and W has any of those bits set,
9108 then T is known not to be equal to W. */
9109 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9110 TYPE_PRECISION (TREE_TYPE (t))), 0))
9111 return true;
9112 return false;
9114 default:
9115 return false;
9119 /* Fold a binary expression of code CODE and type TYPE with operands
9120 OP0 and OP1. LOC is the location of the resulting expression.
9121 Return the folded expression if folding is successful. Otherwise,
9122 return NULL_TREE. */
9124 tree
9125 fold_binary_loc (location_t loc,
9126 enum tree_code code, tree type, tree op0, tree op1)
9128 enum tree_code_class kind = TREE_CODE_CLASS (code);
9129 tree arg0, arg1, tem;
9130 tree t1 = NULL_TREE;
9131 bool strict_overflow_p;
9132 unsigned int prec;
9134 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9135 && TREE_CODE_LENGTH (code) == 2
9136 && op0 != NULL_TREE
9137 && op1 != NULL_TREE);
9139 arg0 = op0;
9140 arg1 = op1;
9142 /* Strip any conversions that don't change the mode. This is
9143 safe for every expression, except for a comparison expression
9144 because its signedness is derived from its operands. So, in
9145 the latter case, only strip conversions that don't change the
9146 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9147 preserved.
9149 Note that this is done as an internal manipulation within the
9150 constant folder, in order to find the simplest representation
9151 of the arguments so that their form can be studied. In any
9152 cases, the appropriate type conversions should be put back in
9153 the tree that will get out of the constant folder. */
9155 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9157 STRIP_SIGN_NOPS (arg0);
9158 STRIP_SIGN_NOPS (arg1);
9160 else
9162 STRIP_NOPS (arg0);
9163 STRIP_NOPS (arg1);
9166 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9167 constant but we can't do arithmetic on them. */
9168 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9170 tem = const_binop (code, type, arg0, arg1);
9171 if (tem != NULL_TREE)
9173 if (TREE_TYPE (tem) != type)
9174 tem = fold_convert_loc (loc, type, tem);
9175 return tem;
9179 /* If this is a commutative operation, and ARG0 is a constant, move it
9180 to ARG1 to reduce the number of tests below. */
9181 if (commutative_tree_code (code)
9182 && tree_swap_operands_p (arg0, arg1, true))
9183 return fold_build2_loc (loc, code, type, op1, op0);
9185 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9186 to ARG1 to reduce the number of tests below. */
9187 if (kind == tcc_comparison
9188 && tree_swap_operands_p (arg0, arg1, true))
9189 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9191 tem = generic_simplify (loc, code, type, op0, op1);
9192 if (tem)
9193 return tem;
9195 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9197 First check for cases where an arithmetic operation is applied to a
9198 compound, conditional, or comparison operation. Push the arithmetic
9199 operation inside the compound or conditional to see if any folding
9200 can then be done. Convert comparison to conditional for this purpose.
9201 The also optimizes non-constant cases that used to be done in
9202 expand_expr.
9204 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9205 one of the operands is a comparison and the other is a comparison, a
9206 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9207 code below would make the expression more complex. Change it to a
9208 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9209 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9211 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9212 || code == EQ_EXPR || code == NE_EXPR)
9213 && TREE_CODE (type) != VECTOR_TYPE
9214 && ((truth_value_p (TREE_CODE (arg0))
9215 && (truth_value_p (TREE_CODE (arg1))
9216 || (TREE_CODE (arg1) == BIT_AND_EXPR
9217 && integer_onep (TREE_OPERAND (arg1, 1)))))
9218 || (truth_value_p (TREE_CODE (arg1))
9219 && (truth_value_p (TREE_CODE (arg0))
9220 || (TREE_CODE (arg0) == BIT_AND_EXPR
9221 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9223 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9224 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9225 : TRUTH_XOR_EXPR,
9226 boolean_type_node,
9227 fold_convert_loc (loc, boolean_type_node, arg0),
9228 fold_convert_loc (loc, boolean_type_node, arg1));
9230 if (code == EQ_EXPR)
9231 tem = invert_truthvalue_loc (loc, tem);
9233 return fold_convert_loc (loc, type, tem);
9236 if (TREE_CODE_CLASS (code) == tcc_binary
9237 || TREE_CODE_CLASS (code) == tcc_comparison)
9239 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9241 tem = fold_build2_loc (loc, code, type,
9242 fold_convert_loc (loc, TREE_TYPE (op0),
9243 TREE_OPERAND (arg0, 1)), op1);
9244 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9245 tem);
9247 if (TREE_CODE (arg1) == COMPOUND_EXPR
9248 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9250 tem = fold_build2_loc (loc, code, type, op0,
9251 fold_convert_loc (loc, TREE_TYPE (op1),
9252 TREE_OPERAND (arg1, 1)));
9253 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9254 tem);
9257 if (TREE_CODE (arg0) == COND_EXPR
9258 || TREE_CODE (arg0) == VEC_COND_EXPR
9259 || COMPARISON_CLASS_P (arg0))
9261 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9262 arg0, arg1,
9263 /*cond_first_p=*/1);
9264 if (tem != NULL_TREE)
9265 return tem;
9268 if (TREE_CODE (arg1) == COND_EXPR
9269 || TREE_CODE (arg1) == VEC_COND_EXPR
9270 || COMPARISON_CLASS_P (arg1))
9272 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9273 arg1, arg0,
9274 /*cond_first_p=*/0);
9275 if (tem != NULL_TREE)
9276 return tem;
9280 switch (code)
9282 case MEM_REF:
9283 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9284 if (TREE_CODE (arg0) == ADDR_EXPR
9285 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9287 tree iref = TREE_OPERAND (arg0, 0);
9288 return fold_build2 (MEM_REF, type,
9289 TREE_OPERAND (iref, 0),
9290 int_const_binop (PLUS_EXPR, arg1,
9291 TREE_OPERAND (iref, 1)));
9294 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9295 if (TREE_CODE (arg0) == ADDR_EXPR
9296 && handled_component_p (TREE_OPERAND (arg0, 0)))
9298 tree base;
9299 HOST_WIDE_INT coffset;
9300 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9301 &coffset);
9302 if (!base)
9303 return NULL_TREE;
9304 return fold_build2 (MEM_REF, type,
9305 build_fold_addr_expr (base),
9306 int_const_binop (PLUS_EXPR, arg1,
9307 size_int (coffset)));
9310 return NULL_TREE;
9312 case POINTER_PLUS_EXPR:
9313 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9314 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9315 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9316 return fold_convert_loc (loc, type,
9317 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9318 fold_convert_loc (loc, sizetype,
9319 arg1),
9320 fold_convert_loc (loc, sizetype,
9321 arg0)));
9323 return NULL_TREE;
9325 case PLUS_EXPR:
9326 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9328 /* X + (X / CST) * -CST is X % CST. */
9329 if (TREE_CODE (arg1) == MULT_EXPR
9330 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9331 && operand_equal_p (arg0,
9332 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9334 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9335 tree cst1 = TREE_OPERAND (arg1, 1);
9336 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9337 cst1, cst0);
9338 if (sum && integer_zerop (sum))
9339 return fold_convert_loc (loc, type,
9340 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9341 TREE_TYPE (arg0), arg0,
9342 cst0));
9346 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9347 one. Make sure the type is not saturating and has the signedness of
9348 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9349 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9350 if ((TREE_CODE (arg0) == MULT_EXPR
9351 || TREE_CODE (arg1) == MULT_EXPR)
9352 && !TYPE_SATURATING (type)
9353 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9354 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9355 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9357 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9358 if (tem)
9359 return tem;
9362 if (! FLOAT_TYPE_P (type))
9364 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9365 (plus (plus (mult) (mult)) (foo)) so that we can
9366 take advantage of the factoring cases below. */
9367 if (ANY_INTEGRAL_TYPE_P (type)
9368 && TYPE_OVERFLOW_WRAPS (type)
9369 && (((TREE_CODE (arg0) == PLUS_EXPR
9370 || TREE_CODE (arg0) == MINUS_EXPR)
9371 && TREE_CODE (arg1) == MULT_EXPR)
9372 || ((TREE_CODE (arg1) == PLUS_EXPR
9373 || TREE_CODE (arg1) == MINUS_EXPR)
9374 && TREE_CODE (arg0) == MULT_EXPR)))
9376 tree parg0, parg1, parg, marg;
9377 enum tree_code pcode;
9379 if (TREE_CODE (arg1) == MULT_EXPR)
9380 parg = arg0, marg = arg1;
9381 else
9382 parg = arg1, marg = arg0;
9383 pcode = TREE_CODE (parg);
9384 parg0 = TREE_OPERAND (parg, 0);
9385 parg1 = TREE_OPERAND (parg, 1);
9386 STRIP_NOPS (parg0);
9387 STRIP_NOPS (parg1);
9389 if (TREE_CODE (parg0) == MULT_EXPR
9390 && TREE_CODE (parg1) != MULT_EXPR)
9391 return fold_build2_loc (loc, pcode, type,
9392 fold_build2_loc (loc, PLUS_EXPR, type,
9393 fold_convert_loc (loc, type,
9394 parg0),
9395 fold_convert_loc (loc, type,
9396 marg)),
9397 fold_convert_loc (loc, type, parg1));
9398 if (TREE_CODE (parg0) != MULT_EXPR
9399 && TREE_CODE (parg1) == MULT_EXPR)
9400 return
9401 fold_build2_loc (loc, PLUS_EXPR, type,
9402 fold_convert_loc (loc, type, parg0),
9403 fold_build2_loc (loc, pcode, type,
9404 fold_convert_loc (loc, type, marg),
9405 fold_convert_loc (loc, type,
9406 parg1)));
9409 else
9411 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9412 to __complex__ ( x, y ). This is not the same for SNaNs or
9413 if signed zeros are involved. */
9414 if (!HONOR_SNANS (element_mode (arg0))
9415 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9416 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9418 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9419 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9420 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9421 bool arg0rz = false, arg0iz = false;
9422 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9423 || (arg0i && (arg0iz = real_zerop (arg0i))))
9425 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9426 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9427 if (arg0rz && arg1i && real_zerop (arg1i))
9429 tree rp = arg1r ? arg1r
9430 : build1 (REALPART_EXPR, rtype, arg1);
9431 tree ip = arg0i ? arg0i
9432 : build1 (IMAGPART_EXPR, rtype, arg0);
9433 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9435 else if (arg0iz && arg1r && real_zerop (arg1r))
9437 tree rp = arg0r ? arg0r
9438 : build1 (REALPART_EXPR, rtype, arg0);
9439 tree ip = arg1i ? arg1i
9440 : build1 (IMAGPART_EXPR, rtype, arg1);
9441 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9446 if (flag_unsafe_math_optimizations
9447 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9448 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9449 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9450 return tem;
9452 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9453 We associate floats only if the user has specified
9454 -fassociative-math. */
9455 if (flag_associative_math
9456 && TREE_CODE (arg1) == PLUS_EXPR
9457 && TREE_CODE (arg0) != MULT_EXPR)
9459 tree tree10 = TREE_OPERAND (arg1, 0);
9460 tree tree11 = TREE_OPERAND (arg1, 1);
9461 if (TREE_CODE (tree11) == MULT_EXPR
9462 && TREE_CODE (tree10) == MULT_EXPR)
9464 tree tree0;
9465 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9466 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9469 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9470 We associate floats only if the user has specified
9471 -fassociative-math. */
9472 if (flag_associative_math
9473 && TREE_CODE (arg0) == PLUS_EXPR
9474 && TREE_CODE (arg1) != MULT_EXPR)
9476 tree tree00 = TREE_OPERAND (arg0, 0);
9477 tree tree01 = TREE_OPERAND (arg0, 1);
9478 if (TREE_CODE (tree01) == MULT_EXPR
9479 && TREE_CODE (tree00) == MULT_EXPR)
9481 tree tree0;
9482 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9483 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9488 bit_rotate:
9489 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9490 is a rotate of A by C1 bits. */
9491 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9492 is a rotate of A by B bits. */
9494 enum tree_code code0, code1;
9495 tree rtype;
9496 code0 = TREE_CODE (arg0);
9497 code1 = TREE_CODE (arg1);
9498 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9499 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9500 && operand_equal_p (TREE_OPERAND (arg0, 0),
9501 TREE_OPERAND (arg1, 0), 0)
9502 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9503 TYPE_UNSIGNED (rtype))
9504 /* Only create rotates in complete modes. Other cases are not
9505 expanded properly. */
9506 && (element_precision (rtype)
9507 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9509 tree tree01, tree11;
9510 enum tree_code code01, code11;
9512 tree01 = TREE_OPERAND (arg0, 1);
9513 tree11 = TREE_OPERAND (arg1, 1);
9514 STRIP_NOPS (tree01);
9515 STRIP_NOPS (tree11);
9516 code01 = TREE_CODE (tree01);
9517 code11 = TREE_CODE (tree11);
9518 if (code01 == INTEGER_CST
9519 && code11 == INTEGER_CST
9520 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9521 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9523 tem = build2_loc (loc, LROTATE_EXPR,
9524 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9525 TREE_OPERAND (arg0, 0),
9526 code0 == LSHIFT_EXPR
9527 ? TREE_OPERAND (arg0, 1)
9528 : TREE_OPERAND (arg1, 1));
9529 return fold_convert_loc (loc, type, tem);
9531 else if (code11 == MINUS_EXPR)
9533 tree tree110, tree111;
9534 tree110 = TREE_OPERAND (tree11, 0);
9535 tree111 = TREE_OPERAND (tree11, 1);
9536 STRIP_NOPS (tree110);
9537 STRIP_NOPS (tree111);
9538 if (TREE_CODE (tree110) == INTEGER_CST
9539 && 0 == compare_tree_int (tree110,
9540 element_precision
9541 (TREE_TYPE (TREE_OPERAND
9542 (arg0, 0))))
9543 && operand_equal_p (tree01, tree111, 0))
9544 return
9545 fold_convert_loc (loc, type,
9546 build2 ((code0 == LSHIFT_EXPR
9547 ? LROTATE_EXPR
9548 : RROTATE_EXPR),
9549 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9550 TREE_OPERAND (arg0, 0),
9551 TREE_OPERAND (arg0, 1)));
9553 else if (code01 == MINUS_EXPR)
9555 tree tree010, tree011;
9556 tree010 = TREE_OPERAND (tree01, 0);
9557 tree011 = TREE_OPERAND (tree01, 1);
9558 STRIP_NOPS (tree010);
9559 STRIP_NOPS (tree011);
9560 if (TREE_CODE (tree010) == INTEGER_CST
9561 && 0 == compare_tree_int (tree010,
9562 element_precision
9563 (TREE_TYPE (TREE_OPERAND
9564 (arg0, 0))))
9565 && operand_equal_p (tree11, tree011, 0))
9566 return fold_convert_loc
9567 (loc, type,
9568 build2 ((code0 != LSHIFT_EXPR
9569 ? LROTATE_EXPR
9570 : RROTATE_EXPR),
9571 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9572 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9577 associate:
9578 /* In most languages, can't associate operations on floats through
9579 parentheses. Rather than remember where the parentheses were, we
9580 don't associate floats at all, unless the user has specified
9581 -fassociative-math.
9582 And, we need to make sure type is not saturating. */
9584 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9585 && !TYPE_SATURATING (type))
9587 tree var0, con0, lit0, minus_lit0;
9588 tree var1, con1, lit1, minus_lit1;
9589 tree atype = type;
9590 bool ok = true;
9592 /* Split both trees into variables, constants, and literals. Then
9593 associate each group together, the constants with literals,
9594 then the result with variables. This increases the chances of
9595 literals being recombined later and of generating relocatable
9596 expressions for the sum of a constant and literal. */
9597 var0 = split_tree (loc, arg0, type, code,
9598 &con0, &lit0, &minus_lit0, 0);
9599 var1 = split_tree (loc, arg1, type, code,
9600 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9602 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9603 if (code == MINUS_EXPR)
9604 code = PLUS_EXPR;
9606 /* With undefined overflow prefer doing association in a type
9607 which wraps on overflow, if that is one of the operand types. */
9608 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9609 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9611 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9612 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9613 atype = TREE_TYPE (arg0);
9614 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9615 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9616 atype = TREE_TYPE (arg1);
9617 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9620 /* With undefined overflow we can only associate constants with one
9621 variable, and constants whose association doesn't overflow. */
9622 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9623 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9625 if (var0 && var1)
9627 tree tmp0 = var0;
9628 tree tmp1 = var1;
9629 bool one_neg = false;
9631 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9633 tmp0 = TREE_OPERAND (tmp0, 0);
9634 one_neg = !one_neg;
9636 if (CONVERT_EXPR_P (tmp0)
9637 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9638 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9639 <= TYPE_PRECISION (atype)))
9640 tmp0 = TREE_OPERAND (tmp0, 0);
9641 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9643 tmp1 = TREE_OPERAND (tmp1, 0);
9644 one_neg = !one_neg;
9646 if (CONVERT_EXPR_P (tmp1)
9647 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9648 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9649 <= TYPE_PRECISION (atype)))
9650 tmp1 = TREE_OPERAND (tmp1, 0);
9651 /* The only case we can still associate with two variables
9652 is if they cancel out. */
9653 if (!one_neg
9654 || !operand_equal_p (tmp0, tmp1, 0))
9655 ok = false;
9659 /* Only do something if we found more than two objects. Otherwise,
9660 nothing has changed and we risk infinite recursion. */
9661 if (ok
9662 && (2 < ((var0 != 0) + (var1 != 0)
9663 + (con0 != 0) + (con1 != 0)
9664 + (lit0 != 0) + (lit1 != 0)
9665 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9667 bool any_overflows = false;
9668 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9669 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9670 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9671 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9672 var0 = associate_trees (loc, var0, var1, code, atype);
9673 con0 = associate_trees (loc, con0, con1, code, atype);
9674 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9675 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9676 code, atype);
9678 /* Preserve the MINUS_EXPR if the negative part of the literal is
9679 greater than the positive part. Otherwise, the multiplicative
9680 folding code (i.e extract_muldiv) may be fooled in case
9681 unsigned constants are subtracted, like in the following
9682 example: ((X*2 + 4) - 8U)/2. */
9683 if (minus_lit0 && lit0)
9685 if (TREE_CODE (lit0) == INTEGER_CST
9686 && TREE_CODE (minus_lit0) == INTEGER_CST
9687 && tree_int_cst_lt (lit0, minus_lit0))
9689 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9690 MINUS_EXPR, atype);
9691 lit0 = 0;
9693 else
9695 lit0 = associate_trees (loc, lit0, minus_lit0,
9696 MINUS_EXPR, atype);
9697 minus_lit0 = 0;
9701 /* Don't introduce overflows through reassociation. */
9702 if (!any_overflows
9703 && ((lit0 && TREE_OVERFLOW_P (lit0))
9704 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9705 return NULL_TREE;
9707 if (minus_lit0)
9709 if (con0 == 0)
9710 return
9711 fold_convert_loc (loc, type,
9712 associate_trees (loc, var0, minus_lit0,
9713 MINUS_EXPR, atype));
9714 else
9716 con0 = associate_trees (loc, con0, minus_lit0,
9717 MINUS_EXPR, atype);
9718 return
9719 fold_convert_loc (loc, type,
9720 associate_trees (loc, var0, con0,
9721 PLUS_EXPR, atype));
9725 con0 = associate_trees (loc, con0, lit0, code, atype);
9726 return
9727 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9728 code, atype));
9732 return NULL_TREE;
9734 case MINUS_EXPR:
9735 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9736 if (TREE_CODE (arg0) == NEGATE_EXPR
9737 && negate_expr_p (op1)
9738 && reorder_operands_p (arg0, arg1))
9739 return fold_build2_loc (loc, MINUS_EXPR, type,
9740 negate_expr (op1),
9741 fold_convert_loc (loc, type,
9742 TREE_OPERAND (arg0, 0)));
9744 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9745 __complex__ ( x, -y ). This is not the same for SNaNs or if
9746 signed zeros are involved. */
9747 if (!HONOR_SNANS (element_mode (arg0))
9748 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9749 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9751 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9752 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9753 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9754 bool arg0rz = false, arg0iz = false;
9755 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9756 || (arg0i && (arg0iz = real_zerop (arg0i))))
9758 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9759 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9760 if (arg0rz && arg1i && real_zerop (arg1i))
9762 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9763 arg1r ? arg1r
9764 : build1 (REALPART_EXPR, rtype, arg1));
9765 tree ip = arg0i ? arg0i
9766 : build1 (IMAGPART_EXPR, rtype, arg0);
9767 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9769 else if (arg0iz && arg1r && real_zerop (arg1r))
9771 tree rp = arg0r ? arg0r
9772 : build1 (REALPART_EXPR, rtype, arg0);
9773 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9774 arg1i ? arg1i
9775 : build1 (IMAGPART_EXPR, rtype, arg1));
9776 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9781 /* A - B -> A + (-B) if B is easily negatable. */
9782 if (negate_expr_p (op1)
9783 && ! TYPE_OVERFLOW_SANITIZED (type)
9784 && ((FLOAT_TYPE_P (type)
9785 /* Avoid this transformation if B is a positive REAL_CST. */
9786 && (TREE_CODE (op1) != REAL_CST
9787 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9788 || INTEGRAL_TYPE_P (type)))
9789 return fold_build2_loc (loc, PLUS_EXPR, type,
9790 fold_convert_loc (loc, type, arg0),
9791 negate_expr (op1));
9793 /* Fold &a[i] - &a[j] to i-j. */
9794 if (TREE_CODE (arg0) == ADDR_EXPR
9795 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9796 && TREE_CODE (arg1) == ADDR_EXPR
9797 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9799 tree tem = fold_addr_of_array_ref_difference (loc, type,
9800 TREE_OPERAND (arg0, 0),
9801 TREE_OPERAND (arg1, 0));
9802 if (tem)
9803 return tem;
9806 if (FLOAT_TYPE_P (type)
9807 && flag_unsafe_math_optimizations
9808 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9809 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9810 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9811 return tem;
9813 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9814 one. Make sure the type is not saturating and has the signedness of
9815 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9816 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9817 if ((TREE_CODE (arg0) == MULT_EXPR
9818 || TREE_CODE (arg1) == MULT_EXPR)
9819 && !TYPE_SATURATING (type)
9820 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9821 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9822 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9824 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9825 if (tem)
9826 return tem;
9829 goto associate;
9831 case MULT_EXPR:
9832 if (! FLOAT_TYPE_P (type))
9834 /* Transform x * -C into -x * C if x is easily negatable. */
9835 if (TREE_CODE (op1) == INTEGER_CST
9836 && tree_int_cst_sgn (op1) == -1
9837 && negate_expr_p (op0)
9838 && (tem = negate_expr (op1)) != op1
9839 && ! TREE_OVERFLOW (tem))
9840 return fold_build2_loc (loc, MULT_EXPR, type,
9841 fold_convert_loc (loc, type,
9842 negate_expr (op0)), tem);
9844 strict_overflow_p = false;
9845 if (TREE_CODE (arg1) == INTEGER_CST
9846 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9847 &strict_overflow_p)))
9849 if (strict_overflow_p)
9850 fold_overflow_warning (("assuming signed overflow does not "
9851 "occur when simplifying "
9852 "multiplication"),
9853 WARN_STRICT_OVERFLOW_MISC);
9854 return fold_convert_loc (loc, type, tem);
9857 /* Optimize z * conj(z) for integer complex numbers. */
9858 if (TREE_CODE (arg0) == CONJ_EXPR
9859 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9860 return fold_mult_zconjz (loc, type, arg1);
9861 if (TREE_CODE (arg1) == CONJ_EXPR
9862 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9863 return fold_mult_zconjz (loc, type, arg0);
9865 else
9867 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9868 This is not the same for NaNs or if signed zeros are
9869 involved. */
9870 if (!HONOR_NANS (arg0)
9871 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9872 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9873 && TREE_CODE (arg1) == COMPLEX_CST
9874 && real_zerop (TREE_REALPART (arg1)))
9876 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9877 if (real_onep (TREE_IMAGPART (arg1)))
9878 return
9879 fold_build2_loc (loc, COMPLEX_EXPR, type,
9880 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9881 rtype, arg0)),
9882 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9883 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9884 return
9885 fold_build2_loc (loc, COMPLEX_EXPR, type,
9886 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9887 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9888 rtype, arg0)));
9891 /* Optimize z * conj(z) for floating point complex numbers.
9892 Guarded by flag_unsafe_math_optimizations as non-finite
9893 imaginary components don't produce scalar results. */
9894 if (flag_unsafe_math_optimizations
9895 && TREE_CODE (arg0) == CONJ_EXPR
9896 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9897 return fold_mult_zconjz (loc, type, arg1);
9898 if (flag_unsafe_math_optimizations
9899 && TREE_CODE (arg1) == CONJ_EXPR
9900 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9901 return fold_mult_zconjz (loc, type, arg0);
9903 goto associate;
9905 case BIT_IOR_EXPR:
9906 /* Canonicalize (X & C1) | C2. */
9907 if (TREE_CODE (arg0) == BIT_AND_EXPR
9908 && TREE_CODE (arg1) == INTEGER_CST
9909 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9911 int width = TYPE_PRECISION (type), w;
9912 wide_int c1 = TREE_OPERAND (arg0, 1);
9913 wide_int c2 = arg1;
9915 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9916 if ((c1 & c2) == c1)
9917 return omit_one_operand_loc (loc, type, arg1,
9918 TREE_OPERAND (arg0, 0));
9920 wide_int msk = wi::mask (width, false,
9921 TYPE_PRECISION (TREE_TYPE (arg1)));
9923 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9924 if (msk.and_not (c1 | c2) == 0)
9925 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9926 TREE_OPERAND (arg0, 0), arg1);
9928 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9929 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9930 mode which allows further optimizations. */
9931 c1 &= msk;
9932 c2 &= msk;
9933 wide_int c3 = c1.and_not (c2);
9934 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9936 wide_int mask = wi::mask (w, false,
9937 TYPE_PRECISION (type));
9938 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9940 c3 = mask;
9941 break;
9945 if (c3 != c1)
9946 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9947 fold_build2_loc (loc, BIT_AND_EXPR, type,
9948 TREE_OPERAND (arg0, 0),
9949 wide_int_to_tree (type,
9950 c3)),
9951 arg1);
9954 /* See if this can be simplified into a rotate first. If that
9955 is unsuccessful continue in the association code. */
9956 goto bit_rotate;
9958 case BIT_XOR_EXPR:
9959 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9960 if (TREE_CODE (arg0) == BIT_AND_EXPR
9961 && INTEGRAL_TYPE_P (type)
9962 && integer_onep (TREE_OPERAND (arg0, 1))
9963 && integer_onep (arg1))
9964 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9965 build_zero_cst (TREE_TYPE (arg0)));
9967 /* See if this can be simplified into a rotate first. If that
9968 is unsuccessful continue in the association code. */
9969 goto bit_rotate;
9971 case BIT_AND_EXPR:
9972 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9973 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9974 && INTEGRAL_TYPE_P (type)
9975 && integer_onep (TREE_OPERAND (arg0, 1))
9976 && integer_onep (arg1))
9978 tree tem2;
9979 tem = TREE_OPERAND (arg0, 0);
9980 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9981 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9982 tem, tem2);
9983 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9984 build_zero_cst (TREE_TYPE (tem)));
9986 /* Fold ~X & 1 as (X & 1) == 0. */
9987 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9988 && INTEGRAL_TYPE_P (type)
9989 && integer_onep (arg1))
9991 tree tem2;
9992 tem = TREE_OPERAND (arg0, 0);
9993 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9994 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9995 tem, tem2);
9996 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9997 build_zero_cst (TREE_TYPE (tem)));
9999 /* Fold !X & 1 as X == 0. */
10000 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10001 && integer_onep (arg1))
10003 tem = TREE_OPERAND (arg0, 0);
10004 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10005 build_zero_cst (TREE_TYPE (tem)));
10008 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10009 multiple of 1 << CST. */
10010 if (TREE_CODE (arg1) == INTEGER_CST)
10012 wide_int cst1 = arg1;
10013 wide_int ncst1 = -cst1;
10014 if ((cst1 & ncst1) == ncst1
10015 && multiple_of_p (type, arg0,
10016 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10017 return fold_convert_loc (loc, type, arg0);
10020 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10021 bits from CST2. */
10022 if (TREE_CODE (arg1) == INTEGER_CST
10023 && TREE_CODE (arg0) == MULT_EXPR
10024 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10026 wide_int warg1 = arg1;
10027 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10029 if (masked == 0)
10030 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10031 arg0, arg1);
10032 else if (masked != warg1)
10034 /* Avoid the transform if arg1 is a mask of some
10035 mode which allows further optimizations. */
10036 int pop = wi::popcount (warg1);
10037 if (!(pop >= BITS_PER_UNIT
10038 && pow2p_hwi (pop)
10039 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10040 return fold_build2_loc (loc, code, type, op0,
10041 wide_int_to_tree (type, masked));
10045 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10046 ((A & N) + B) & M -> (A + B) & M
10047 Similarly if (N & M) == 0,
10048 ((A | N) + B) & M -> (A + B) & M
10049 and for - instead of + (or unary - instead of +)
10050 and/or ^ instead of |.
10051 If B is constant and (B & M) == 0, fold into A & M. */
10052 if (TREE_CODE (arg1) == INTEGER_CST)
10054 wide_int cst1 = arg1;
10055 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10056 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10057 && (TREE_CODE (arg0) == PLUS_EXPR
10058 || TREE_CODE (arg0) == MINUS_EXPR
10059 || TREE_CODE (arg0) == NEGATE_EXPR)
10060 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10061 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10063 tree pmop[2];
10064 int which = 0;
10065 wide_int cst0;
10067 /* Now we know that arg0 is (C + D) or (C - D) or
10068 -C and arg1 (M) is == (1LL << cst) - 1.
10069 Store C into PMOP[0] and D into PMOP[1]. */
10070 pmop[0] = TREE_OPERAND (arg0, 0);
10071 pmop[1] = NULL;
10072 if (TREE_CODE (arg0) != NEGATE_EXPR)
10074 pmop[1] = TREE_OPERAND (arg0, 1);
10075 which = 1;
10078 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10079 which = -1;
10081 for (; which >= 0; which--)
10082 switch (TREE_CODE (pmop[which]))
10084 case BIT_AND_EXPR:
10085 case BIT_IOR_EXPR:
10086 case BIT_XOR_EXPR:
10087 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10088 != INTEGER_CST)
10089 break;
10090 cst0 = TREE_OPERAND (pmop[which], 1);
10091 cst0 &= cst1;
10092 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10094 if (cst0 != cst1)
10095 break;
10097 else if (cst0 != 0)
10098 break;
10099 /* If C or D is of the form (A & N) where
10100 (N & M) == M, or of the form (A | N) or
10101 (A ^ N) where (N & M) == 0, replace it with A. */
10102 pmop[which] = TREE_OPERAND (pmop[which], 0);
10103 break;
10104 case INTEGER_CST:
10105 /* If C or D is a N where (N & M) == 0, it can be
10106 omitted (assumed 0). */
10107 if ((TREE_CODE (arg0) == PLUS_EXPR
10108 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10109 && (cst1 & pmop[which]) == 0)
10110 pmop[which] = NULL;
10111 break;
10112 default:
10113 break;
10116 /* Only build anything new if we optimized one or both arguments
10117 above. */
10118 if (pmop[0] != TREE_OPERAND (arg0, 0)
10119 || (TREE_CODE (arg0) != NEGATE_EXPR
10120 && pmop[1] != TREE_OPERAND (arg0, 1)))
10122 tree utype = TREE_TYPE (arg0);
10123 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10125 /* Perform the operations in a type that has defined
10126 overflow behavior. */
10127 utype = unsigned_type_for (TREE_TYPE (arg0));
10128 if (pmop[0] != NULL)
10129 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10130 if (pmop[1] != NULL)
10131 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10134 if (TREE_CODE (arg0) == NEGATE_EXPR)
10135 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10136 else if (TREE_CODE (arg0) == PLUS_EXPR)
10138 if (pmop[0] != NULL && pmop[1] != NULL)
10139 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10140 pmop[0], pmop[1]);
10141 else if (pmop[0] != NULL)
10142 tem = pmop[0];
10143 else if (pmop[1] != NULL)
10144 tem = pmop[1];
10145 else
10146 return build_int_cst (type, 0);
10148 else if (pmop[0] == NULL)
10149 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10150 else
10151 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10152 pmop[0], pmop[1]);
10153 /* TEM is now the new binary +, - or unary - replacement. */
10154 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10155 fold_convert_loc (loc, utype, arg1));
10156 return fold_convert_loc (loc, type, tem);
10161 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10162 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10163 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10165 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10167 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10168 if (mask == -1)
10169 return
10170 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10173 goto associate;
10175 case RDIV_EXPR:
10176 /* Don't touch a floating-point divide by zero unless the mode
10177 of the constant can represent infinity. */
10178 if (TREE_CODE (arg1) == REAL_CST
10179 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10180 && real_zerop (arg1))
10181 return NULL_TREE;
10183 /* (-A) / (-B) -> A / B */
10184 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10185 return fold_build2_loc (loc, RDIV_EXPR, type,
10186 TREE_OPERAND (arg0, 0),
10187 negate_expr (arg1));
10188 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10189 return fold_build2_loc (loc, RDIV_EXPR, type,
10190 negate_expr (arg0),
10191 TREE_OPERAND (arg1, 0));
10192 return NULL_TREE;
10194 case TRUNC_DIV_EXPR:
10195 /* Fall through */
10197 case FLOOR_DIV_EXPR:
10198 /* Simplify A / (B << N) where A and B are positive and B is
10199 a power of 2, to A >> (N + log2(B)). */
10200 strict_overflow_p = false;
10201 if (TREE_CODE (arg1) == LSHIFT_EXPR
10202 && (TYPE_UNSIGNED (type)
10203 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10205 tree sval = TREE_OPERAND (arg1, 0);
10206 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10208 tree sh_cnt = TREE_OPERAND (arg1, 1);
10209 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10210 wi::exact_log2 (sval));
10212 if (strict_overflow_p)
10213 fold_overflow_warning (("assuming signed overflow does not "
10214 "occur when simplifying A / (B << N)"),
10215 WARN_STRICT_OVERFLOW_MISC);
10217 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10218 sh_cnt, pow2);
10219 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10220 fold_convert_loc (loc, type, arg0), sh_cnt);
10224 /* Fall through */
10226 case ROUND_DIV_EXPR:
10227 case CEIL_DIV_EXPR:
10228 case EXACT_DIV_EXPR:
10229 if (integer_zerop (arg1))
10230 return NULL_TREE;
10232 /* Convert -A / -B to A / B when the type is signed and overflow is
10233 undefined. */
10234 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10235 && TREE_CODE (arg0) == NEGATE_EXPR
10236 && negate_expr_p (op1))
10238 if (INTEGRAL_TYPE_P (type))
10239 fold_overflow_warning (("assuming signed overflow does not occur "
10240 "when distributing negation across "
10241 "division"),
10242 WARN_STRICT_OVERFLOW_MISC);
10243 return fold_build2_loc (loc, code, type,
10244 fold_convert_loc (loc, type,
10245 TREE_OPERAND (arg0, 0)),
10246 negate_expr (op1));
10248 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10249 && TREE_CODE (arg1) == NEGATE_EXPR
10250 && negate_expr_p (op0))
10252 if (INTEGRAL_TYPE_P (type))
10253 fold_overflow_warning (("assuming signed overflow does not occur "
10254 "when distributing negation across "
10255 "division"),
10256 WARN_STRICT_OVERFLOW_MISC);
10257 return fold_build2_loc (loc, code, type,
10258 negate_expr (op0),
10259 fold_convert_loc (loc, type,
10260 TREE_OPERAND (arg1, 0)));
10263 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10264 operation, EXACT_DIV_EXPR.
10266 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10267 At one time others generated faster code, it's not clear if they do
10268 after the last round to changes to the DIV code in expmed.c. */
10269 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10270 && multiple_of_p (type, arg0, arg1))
10271 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10272 fold_convert (type, arg0),
10273 fold_convert (type, arg1));
10275 strict_overflow_p = false;
10276 if (TREE_CODE (arg1) == INTEGER_CST
10277 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10278 &strict_overflow_p)))
10280 if (strict_overflow_p)
10281 fold_overflow_warning (("assuming signed overflow does not occur "
10282 "when simplifying division"),
10283 WARN_STRICT_OVERFLOW_MISC);
10284 return fold_convert_loc (loc, type, tem);
10287 return NULL_TREE;
10289 case CEIL_MOD_EXPR:
10290 case FLOOR_MOD_EXPR:
10291 case ROUND_MOD_EXPR:
10292 case TRUNC_MOD_EXPR:
10293 strict_overflow_p = false;
10294 if (TREE_CODE (arg1) == INTEGER_CST
10295 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10296 &strict_overflow_p)))
10298 if (strict_overflow_p)
10299 fold_overflow_warning (("assuming signed overflow does not occur "
10300 "when simplifying modulus"),
10301 WARN_STRICT_OVERFLOW_MISC);
10302 return fold_convert_loc (loc, type, tem);
10305 return NULL_TREE;
10307 case LROTATE_EXPR:
10308 case RROTATE_EXPR:
10309 case RSHIFT_EXPR:
10310 case LSHIFT_EXPR:
10311 /* Since negative shift count is not well-defined,
10312 don't try to compute it in the compiler. */
10313 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10314 return NULL_TREE;
10316 prec = element_precision (type);
10318 /* If we have a rotate of a bit operation with the rotate count and
10319 the second operand of the bit operation both constant,
10320 permute the two operations. */
10321 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10322 && (TREE_CODE (arg0) == BIT_AND_EXPR
10323 || TREE_CODE (arg0) == BIT_IOR_EXPR
10324 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10325 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10327 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10328 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10329 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10330 fold_build2_loc (loc, code, type,
10331 arg00, arg1),
10332 fold_build2_loc (loc, code, type,
10333 arg01, arg1));
10336 /* Two consecutive rotates adding up to the some integer
10337 multiple of the precision of the type can be ignored. */
10338 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10339 && TREE_CODE (arg0) == RROTATE_EXPR
10340 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10341 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10342 prec) == 0)
10343 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10345 return NULL_TREE;
10347 case MIN_EXPR:
10348 case MAX_EXPR:
10349 goto associate;
10351 case TRUTH_ANDIF_EXPR:
10352 /* Note that the operands of this must be ints
10353 and their values must be 0 or 1.
10354 ("true" is a fixed value perhaps depending on the language.) */
10355 /* If first arg is constant zero, return it. */
10356 if (integer_zerop (arg0))
10357 return fold_convert_loc (loc, type, arg0);
10358 /* FALLTHRU */
10359 case TRUTH_AND_EXPR:
10360 /* If either arg is constant true, drop it. */
10361 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10362 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10363 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10364 /* Preserve sequence points. */
10365 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10366 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10367 /* If second arg is constant zero, result is zero, but first arg
10368 must be evaluated. */
10369 if (integer_zerop (arg1))
10370 return omit_one_operand_loc (loc, type, arg1, arg0);
10371 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10372 case will be handled here. */
10373 if (integer_zerop (arg0))
10374 return omit_one_operand_loc (loc, type, arg0, arg1);
10376 /* !X && X is always false. */
10377 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10378 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10379 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10380 /* X && !X is always false. */
10381 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10382 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10383 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10385 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10386 means A >= Y && A != MAX, but in this case we know that
10387 A < X <= MAX. */
10389 if (!TREE_SIDE_EFFECTS (arg0)
10390 && !TREE_SIDE_EFFECTS (arg1))
10392 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10393 if (tem && !operand_equal_p (tem, arg0, 0))
10394 return fold_build2_loc (loc, code, type, tem, arg1);
10396 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10397 if (tem && !operand_equal_p (tem, arg1, 0))
10398 return fold_build2_loc (loc, code, type, arg0, tem);
10401 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10402 != NULL_TREE)
10403 return tem;
10405 return NULL_TREE;
10407 case TRUTH_ORIF_EXPR:
10408 /* Note that the operands of this must be ints
10409 and their values must be 0 or true.
10410 ("true" is a fixed value perhaps depending on the language.) */
10411 /* If first arg is constant true, return it. */
10412 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10413 return fold_convert_loc (loc, type, arg0);
10414 /* FALLTHRU */
10415 case TRUTH_OR_EXPR:
10416 /* If either arg is constant zero, drop it. */
10417 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10418 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10419 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10420 /* Preserve sequence points. */
10421 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10422 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10423 /* If second arg is constant true, result is true, but we must
10424 evaluate first arg. */
10425 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10426 return omit_one_operand_loc (loc, type, arg1, arg0);
10427 /* Likewise for first arg, but note this only occurs here for
10428 TRUTH_OR_EXPR. */
10429 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10430 return omit_one_operand_loc (loc, type, arg0, arg1);
10432 /* !X || X is always true. */
10433 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10434 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10435 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10436 /* X || !X is always true. */
10437 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10438 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10439 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10441 /* (X && !Y) || (!X && Y) is X ^ Y */
10442 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10443 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10445 tree a0, a1, l0, l1, n0, n1;
10447 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10448 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10450 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10451 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10453 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10454 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10456 if ((operand_equal_p (n0, a0, 0)
10457 && operand_equal_p (n1, a1, 0))
10458 || (operand_equal_p (n0, a1, 0)
10459 && operand_equal_p (n1, a0, 0)))
10460 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10463 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10464 != NULL_TREE)
10465 return tem;
10467 return NULL_TREE;
10469 case TRUTH_XOR_EXPR:
10470 /* If the second arg is constant zero, drop it. */
10471 if (integer_zerop (arg1))
10472 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10473 /* If the second arg is constant true, this is a logical inversion. */
10474 if (integer_onep (arg1))
10476 tem = invert_truthvalue_loc (loc, arg0);
10477 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10479 /* Identical arguments cancel to zero. */
10480 if (operand_equal_p (arg0, arg1, 0))
10481 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10483 /* !X ^ X is always true. */
10484 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10485 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10486 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10488 /* X ^ !X is always true. */
10489 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10490 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10491 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10493 return NULL_TREE;
10495 case EQ_EXPR:
10496 case NE_EXPR:
10497 STRIP_NOPS (arg0);
10498 STRIP_NOPS (arg1);
10500 tem = fold_comparison (loc, code, type, op0, op1);
10501 if (tem != NULL_TREE)
10502 return tem;
10504 /* bool_var != 1 becomes !bool_var. */
10505 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10506 && code == NE_EXPR)
10507 return fold_convert_loc (loc, type,
10508 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10509 TREE_TYPE (arg0), arg0));
10511 /* bool_var == 0 becomes !bool_var. */
10512 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10513 && code == EQ_EXPR)
10514 return fold_convert_loc (loc, type,
10515 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10516 TREE_TYPE (arg0), arg0));
10518 /* !exp != 0 becomes !exp */
10519 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10520 && code == NE_EXPR)
10521 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10523 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10524 if ((TREE_CODE (arg0) == PLUS_EXPR
10525 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10526 || TREE_CODE (arg0) == MINUS_EXPR)
10527 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10528 0)),
10529 arg1, 0)
10530 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10531 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10533 tree val = TREE_OPERAND (arg0, 1);
10534 val = fold_build2_loc (loc, code, type, val,
10535 build_int_cst (TREE_TYPE (val), 0));
10536 return omit_two_operands_loc (loc, type, val,
10537 TREE_OPERAND (arg0, 0), arg1);
10540 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10541 if ((TREE_CODE (arg1) == PLUS_EXPR
10542 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10543 || TREE_CODE (arg1) == MINUS_EXPR)
10544 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10545 0)),
10546 arg0, 0)
10547 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10548 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10550 tree val = TREE_OPERAND (arg1, 1);
10551 val = fold_build2_loc (loc, code, type, val,
10552 build_int_cst (TREE_TYPE (val), 0));
10553 return omit_two_operands_loc (loc, type, val,
10554 TREE_OPERAND (arg1, 0), arg0);
10557 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10558 if (TREE_CODE (arg0) == MINUS_EXPR
10559 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10560 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10561 1)),
10562 arg1, 0)
10563 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10564 return omit_two_operands_loc (loc, type,
10565 code == NE_EXPR
10566 ? boolean_true_node : boolean_false_node,
10567 TREE_OPERAND (arg0, 1), arg1);
10569 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10570 if (TREE_CODE (arg1) == MINUS_EXPR
10571 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10572 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10573 1)),
10574 arg0, 0)
10575 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10576 return omit_two_operands_loc (loc, type,
10577 code == NE_EXPR
10578 ? boolean_true_node : boolean_false_node,
10579 TREE_OPERAND (arg1, 1), arg0);
10581 /* If this is an EQ or NE comparison with zero and ARG0 is
10582 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10583 two operations, but the latter can be done in one less insn
10584 on machines that have only two-operand insns or on which a
10585 constant cannot be the first operand. */
10586 if (TREE_CODE (arg0) == BIT_AND_EXPR
10587 && integer_zerop (arg1))
10589 tree arg00 = TREE_OPERAND (arg0, 0);
10590 tree arg01 = TREE_OPERAND (arg0, 1);
10591 if (TREE_CODE (arg00) == LSHIFT_EXPR
10592 && integer_onep (TREE_OPERAND (arg00, 0)))
10594 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10595 arg01, TREE_OPERAND (arg00, 1));
10596 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10597 build_int_cst (TREE_TYPE (arg0), 1));
10598 return fold_build2_loc (loc, code, type,
10599 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10600 arg1);
10602 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10603 && integer_onep (TREE_OPERAND (arg01, 0)))
10605 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10606 arg00, TREE_OPERAND (arg01, 1));
10607 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10608 build_int_cst (TREE_TYPE (arg0), 1));
10609 return fold_build2_loc (loc, code, type,
10610 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10611 arg1);
10615 /* If this is an NE or EQ comparison of zero against the result of a
10616 signed MOD operation whose second operand is a power of 2, make
10617 the MOD operation unsigned since it is simpler and equivalent. */
10618 if (integer_zerop (arg1)
10619 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10620 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10621 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10622 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10623 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10624 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10626 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10627 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10628 fold_convert_loc (loc, newtype,
10629 TREE_OPERAND (arg0, 0)),
10630 fold_convert_loc (loc, newtype,
10631 TREE_OPERAND (arg0, 1)));
10633 return fold_build2_loc (loc, code, type, newmod,
10634 fold_convert_loc (loc, newtype, arg1));
10637 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10638 C1 is a valid shift constant, and C2 is a power of two, i.e.
10639 a single bit. */
10640 if (TREE_CODE (arg0) == BIT_AND_EXPR
10641 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10642 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10643 == INTEGER_CST
10644 && integer_pow2p (TREE_OPERAND (arg0, 1))
10645 && integer_zerop (arg1))
10647 tree itype = TREE_TYPE (arg0);
10648 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10649 prec = TYPE_PRECISION (itype);
10651 /* Check for a valid shift count. */
10652 if (wi::ltu_p (arg001, prec))
10654 tree arg01 = TREE_OPERAND (arg0, 1);
10655 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10656 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10657 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10658 can be rewritten as (X & (C2 << C1)) != 0. */
10659 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10661 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10662 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10663 return fold_build2_loc (loc, code, type, tem,
10664 fold_convert_loc (loc, itype, arg1));
10666 /* Otherwise, for signed (arithmetic) shifts,
10667 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10668 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10669 else if (!TYPE_UNSIGNED (itype))
10670 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10671 arg000, build_int_cst (itype, 0));
10672 /* Otherwise, of unsigned (logical) shifts,
10673 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10674 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10675 else
10676 return omit_one_operand_loc (loc, type,
10677 code == EQ_EXPR ? integer_one_node
10678 : integer_zero_node,
10679 arg000);
10683 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10684 Similarly for NE_EXPR. */
10685 if (TREE_CODE (arg0) == BIT_AND_EXPR
10686 && TREE_CODE (arg1) == INTEGER_CST
10687 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10689 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10690 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10691 TREE_OPERAND (arg0, 1));
10692 tree dandnotc
10693 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10694 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10695 notc);
10696 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10697 if (integer_nonzerop (dandnotc))
10698 return omit_one_operand_loc (loc, type, rslt, arg0);
10701 /* If this is a comparison of a field, we may be able to simplify it. */
10702 if ((TREE_CODE (arg0) == COMPONENT_REF
10703 || TREE_CODE (arg0) == BIT_FIELD_REF)
10704 /* Handle the constant case even without -O
10705 to make sure the warnings are given. */
10706 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10708 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10709 if (t1)
10710 return t1;
10713 /* Optimize comparisons of strlen vs zero to a compare of the
10714 first character of the string vs zero. To wit,
10715 strlen(ptr) == 0 => *ptr == 0
10716 strlen(ptr) != 0 => *ptr != 0
10717 Other cases should reduce to one of these two (or a constant)
10718 due to the return value of strlen being unsigned. */
10719 if (TREE_CODE (arg0) == CALL_EXPR
10720 && integer_zerop (arg1))
10722 tree fndecl = get_callee_fndecl (arg0);
10724 if (fndecl
10725 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10726 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10727 && call_expr_nargs (arg0) == 1
10728 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10730 tree iref = build_fold_indirect_ref_loc (loc,
10731 CALL_EXPR_ARG (arg0, 0));
10732 return fold_build2_loc (loc, code, type, iref,
10733 build_int_cst (TREE_TYPE (iref), 0));
10737 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10738 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10739 if (TREE_CODE (arg0) == RSHIFT_EXPR
10740 && integer_zerop (arg1)
10741 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10743 tree arg00 = TREE_OPERAND (arg0, 0);
10744 tree arg01 = TREE_OPERAND (arg0, 1);
10745 tree itype = TREE_TYPE (arg00);
10746 if (wi::eq_p (arg01, element_precision (itype) - 1))
10748 if (TYPE_UNSIGNED (itype))
10750 itype = signed_type_for (itype);
10751 arg00 = fold_convert_loc (loc, itype, arg00);
10753 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10754 type, arg00, build_zero_cst (itype));
10758 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10759 (X & C) == 0 when C is a single bit. */
10760 if (TREE_CODE (arg0) == BIT_AND_EXPR
10761 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10762 && integer_zerop (arg1)
10763 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10765 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10766 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10767 TREE_OPERAND (arg0, 1));
10768 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10769 type, tem,
10770 fold_convert_loc (loc, TREE_TYPE (arg0),
10771 arg1));
10774 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10775 constant C is a power of two, i.e. a single bit. */
10776 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10777 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10778 && integer_zerop (arg1)
10779 && integer_pow2p (TREE_OPERAND (arg0, 1))
10780 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10781 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10783 tree arg00 = TREE_OPERAND (arg0, 0);
10784 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10785 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10788 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10789 when is C is a power of two, i.e. a single bit. */
10790 if (TREE_CODE (arg0) == BIT_AND_EXPR
10791 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10792 && integer_zerop (arg1)
10793 && integer_pow2p (TREE_OPERAND (arg0, 1))
10794 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10795 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10797 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10798 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10799 arg000, TREE_OPERAND (arg0, 1));
10800 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10801 tem, build_int_cst (TREE_TYPE (tem), 0));
10804 if (integer_zerop (arg1)
10805 && tree_expr_nonzero_p (arg0))
10807 tree res = constant_boolean_node (code==NE_EXPR, type);
10808 return omit_one_operand_loc (loc, type, res, arg0);
10811 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10812 if (TREE_CODE (arg0) == BIT_AND_EXPR
10813 && TREE_CODE (arg1) == BIT_AND_EXPR)
10815 tree arg00 = TREE_OPERAND (arg0, 0);
10816 tree arg01 = TREE_OPERAND (arg0, 1);
10817 tree arg10 = TREE_OPERAND (arg1, 0);
10818 tree arg11 = TREE_OPERAND (arg1, 1);
10819 tree itype = TREE_TYPE (arg0);
10821 if (operand_equal_p (arg01, arg11, 0))
10822 return fold_build2_loc (loc, code, type,
10823 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10824 fold_build2_loc (loc,
10825 BIT_XOR_EXPR, itype,
10826 arg00, arg10),
10827 arg01),
10828 build_zero_cst (itype));
10830 if (operand_equal_p (arg01, arg10, 0))
10831 return fold_build2_loc (loc, code, type,
10832 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10833 fold_build2_loc (loc,
10834 BIT_XOR_EXPR, itype,
10835 arg00, arg11),
10836 arg01),
10837 build_zero_cst (itype));
10839 if (operand_equal_p (arg00, arg11, 0))
10840 return fold_build2_loc (loc, code, type,
10841 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10842 fold_build2_loc (loc,
10843 BIT_XOR_EXPR, itype,
10844 arg01, arg10),
10845 arg00),
10846 build_zero_cst (itype));
10848 if (operand_equal_p (arg00, arg10, 0))
10849 return fold_build2_loc (loc, code, type,
10850 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10851 fold_build2_loc (loc,
10852 BIT_XOR_EXPR, itype,
10853 arg01, arg11),
10854 arg00),
10855 build_zero_cst (itype));
10858 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10859 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10861 tree arg00 = TREE_OPERAND (arg0, 0);
10862 tree arg01 = TREE_OPERAND (arg0, 1);
10863 tree arg10 = TREE_OPERAND (arg1, 0);
10864 tree arg11 = TREE_OPERAND (arg1, 1);
10865 tree itype = TREE_TYPE (arg0);
10867 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10868 operand_equal_p guarantees no side-effects so we don't need
10869 to use omit_one_operand on Z. */
10870 if (operand_equal_p (arg01, arg11, 0))
10871 return fold_build2_loc (loc, code, type, arg00,
10872 fold_convert_loc (loc, TREE_TYPE (arg00),
10873 arg10));
10874 if (operand_equal_p (arg01, arg10, 0))
10875 return fold_build2_loc (loc, code, type, arg00,
10876 fold_convert_loc (loc, TREE_TYPE (arg00),
10877 arg11));
10878 if (operand_equal_p (arg00, arg11, 0))
10879 return fold_build2_loc (loc, code, type, arg01,
10880 fold_convert_loc (loc, TREE_TYPE (arg01),
10881 arg10));
10882 if (operand_equal_p (arg00, arg10, 0))
10883 return fold_build2_loc (loc, code, type, arg01,
10884 fold_convert_loc (loc, TREE_TYPE (arg01),
10885 arg11));
10887 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10888 if (TREE_CODE (arg01) == INTEGER_CST
10889 && TREE_CODE (arg11) == INTEGER_CST)
10891 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10892 fold_convert_loc (loc, itype, arg11));
10893 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10894 return fold_build2_loc (loc, code, type, tem,
10895 fold_convert_loc (loc, itype, arg10));
10899 /* Attempt to simplify equality/inequality comparisons of complex
10900 values. Only lower the comparison if the result is known or
10901 can be simplified to a single scalar comparison. */
10902 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10903 || TREE_CODE (arg0) == COMPLEX_CST)
10904 && (TREE_CODE (arg1) == COMPLEX_EXPR
10905 || TREE_CODE (arg1) == COMPLEX_CST))
10907 tree real0, imag0, real1, imag1;
10908 tree rcond, icond;
10910 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10912 real0 = TREE_OPERAND (arg0, 0);
10913 imag0 = TREE_OPERAND (arg0, 1);
10915 else
10917 real0 = TREE_REALPART (arg0);
10918 imag0 = TREE_IMAGPART (arg0);
10921 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10923 real1 = TREE_OPERAND (arg1, 0);
10924 imag1 = TREE_OPERAND (arg1, 1);
10926 else
10928 real1 = TREE_REALPART (arg1);
10929 imag1 = TREE_IMAGPART (arg1);
10932 rcond = fold_binary_loc (loc, code, type, real0, real1);
10933 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10935 if (integer_zerop (rcond))
10937 if (code == EQ_EXPR)
10938 return omit_two_operands_loc (loc, type, boolean_false_node,
10939 imag0, imag1);
10940 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10942 else
10944 if (code == NE_EXPR)
10945 return omit_two_operands_loc (loc, type, boolean_true_node,
10946 imag0, imag1);
10947 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10951 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10952 if (icond && TREE_CODE (icond) == INTEGER_CST)
10954 if (integer_zerop (icond))
10956 if (code == EQ_EXPR)
10957 return omit_two_operands_loc (loc, type, boolean_false_node,
10958 real0, real1);
10959 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10961 else
10963 if (code == NE_EXPR)
10964 return omit_two_operands_loc (loc, type, boolean_true_node,
10965 real0, real1);
10966 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10971 return NULL_TREE;
10973 case LT_EXPR:
10974 case GT_EXPR:
10975 case LE_EXPR:
10976 case GE_EXPR:
10977 tem = fold_comparison (loc, code, type, op0, op1);
10978 if (tem != NULL_TREE)
10979 return tem;
10981 /* Transform comparisons of the form X +- C CMP X. */
10982 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10983 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10984 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10985 && !HONOR_SNANS (arg0))
10986 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10987 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10989 tree arg01 = TREE_OPERAND (arg0, 1);
10990 enum tree_code code0 = TREE_CODE (arg0);
10991 int is_positive;
10993 if (TREE_CODE (arg01) == REAL_CST)
10994 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10995 else
10996 is_positive = tree_int_cst_sgn (arg01);
10998 /* (X - c) > X becomes false. */
10999 if (code == GT_EXPR
11000 && ((code0 == MINUS_EXPR && is_positive >= 0)
11001 || (code0 == PLUS_EXPR && is_positive <= 0)))
11003 if (TREE_CODE (arg01) == INTEGER_CST
11004 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11005 fold_overflow_warning (("assuming signed overflow does not "
11006 "occur when assuming that (X - c) > X "
11007 "is always false"),
11008 WARN_STRICT_OVERFLOW_ALL);
11009 return constant_boolean_node (0, type);
11012 /* Likewise (X + c) < X becomes false. */
11013 if (code == LT_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 false"),
11022 WARN_STRICT_OVERFLOW_ALL);
11023 return constant_boolean_node (0, type);
11026 /* Convert (X - c) <= X to true. */
11027 if (!HONOR_NANS (arg1)
11028 && code == LE_EXPR
11029 && ((code0 == MINUS_EXPR && is_positive >= 0)
11030 || (code0 == PLUS_EXPR && is_positive <= 0)))
11032 if (TREE_CODE (arg01) == INTEGER_CST
11033 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11034 fold_overflow_warning (("assuming signed overflow does not "
11035 "occur when assuming that "
11036 "(X - c) <= X is always true"),
11037 WARN_STRICT_OVERFLOW_ALL);
11038 return constant_boolean_node (1, type);
11041 /* Convert (X + c) >= X to true. */
11042 if (!HONOR_NANS (arg1)
11043 && code == GE_EXPR
11044 && ((code0 == PLUS_EXPR && is_positive >= 0)
11045 || (code0 == MINUS_EXPR && is_positive <= 0)))
11047 if (TREE_CODE (arg01) == INTEGER_CST
11048 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11049 fold_overflow_warning (("assuming signed overflow does not "
11050 "occur when assuming that "
11051 "(X + c) >= X is always true"),
11052 WARN_STRICT_OVERFLOW_ALL);
11053 return constant_boolean_node (1, type);
11056 if (TREE_CODE (arg01) == INTEGER_CST)
11058 /* Convert X + c > X and X - c < X to true for integers. */
11059 if (code == GT_EXPR
11060 && ((code0 == PLUS_EXPR && is_positive > 0)
11061 || (code0 == MINUS_EXPR && is_positive < 0)))
11063 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11064 fold_overflow_warning (("assuming signed overflow does "
11065 "not occur when assuming that "
11066 "(X + c) > X is always true"),
11067 WARN_STRICT_OVERFLOW_ALL);
11068 return constant_boolean_node (1, type);
11071 if (code == LT_EXPR
11072 && ((code0 == MINUS_EXPR && is_positive > 0)
11073 || (code0 == PLUS_EXPR && is_positive < 0)))
11075 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11076 fold_overflow_warning (("assuming signed overflow does "
11077 "not occur when assuming that "
11078 "(X - c) < X is always true"),
11079 WARN_STRICT_OVERFLOW_ALL);
11080 return constant_boolean_node (1, type);
11083 /* Convert X + c <= X and X - c >= X to false for integers. */
11084 if (code == LE_EXPR
11085 && ((code0 == PLUS_EXPR && is_positive > 0)
11086 || (code0 == MINUS_EXPR && is_positive < 0)))
11088 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11089 fold_overflow_warning (("assuming signed overflow does "
11090 "not occur when assuming that "
11091 "(X + c) <= X is always false"),
11092 WARN_STRICT_OVERFLOW_ALL);
11093 return constant_boolean_node (0, type);
11096 if (code == GE_EXPR
11097 && ((code0 == MINUS_EXPR && is_positive > 0)
11098 || (code0 == PLUS_EXPR && is_positive < 0)))
11100 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11101 fold_overflow_warning (("assuming signed overflow does "
11102 "not occur when assuming that "
11103 "(X - c) >= X is always false"),
11104 WARN_STRICT_OVERFLOW_ALL);
11105 return constant_boolean_node (0, type);
11110 /* If we are comparing an ABS_EXPR with a constant, we can
11111 convert all the cases into explicit comparisons, but they may
11112 well not be faster than doing the ABS and one comparison.
11113 But ABS (X) <= C is a range comparison, which becomes a subtraction
11114 and a comparison, and is probably faster. */
11115 if (code == LE_EXPR
11116 && TREE_CODE (arg1) == INTEGER_CST
11117 && TREE_CODE (arg0) == ABS_EXPR
11118 && ! TREE_SIDE_EFFECTS (arg0)
11119 && (0 != (tem = negate_expr (arg1)))
11120 && TREE_CODE (tem) == INTEGER_CST
11121 && !TREE_OVERFLOW (tem))
11122 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11123 build2 (GE_EXPR, type,
11124 TREE_OPERAND (arg0, 0), tem),
11125 build2 (LE_EXPR, type,
11126 TREE_OPERAND (arg0, 0), arg1));
11128 /* Convert ABS_EXPR<x> >= 0 to true. */
11129 strict_overflow_p = false;
11130 if (code == GE_EXPR
11131 && (integer_zerop (arg1)
11132 || (! HONOR_NANS (arg0)
11133 && real_zerop (arg1)))
11134 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11136 if (strict_overflow_p)
11137 fold_overflow_warning (("assuming signed overflow does not occur "
11138 "when simplifying comparison of "
11139 "absolute value and zero"),
11140 WARN_STRICT_OVERFLOW_CONDITIONAL);
11141 return omit_one_operand_loc (loc, type,
11142 constant_boolean_node (true, type),
11143 arg0);
11146 /* Convert ABS_EXPR<x> < 0 to false. */
11147 strict_overflow_p = false;
11148 if (code == LT_EXPR
11149 && (integer_zerop (arg1) || real_zerop (arg1))
11150 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11152 if (strict_overflow_p)
11153 fold_overflow_warning (("assuming signed overflow does not occur "
11154 "when simplifying comparison of "
11155 "absolute value and zero"),
11156 WARN_STRICT_OVERFLOW_CONDITIONAL);
11157 return omit_one_operand_loc (loc, type,
11158 constant_boolean_node (false, type),
11159 arg0);
11162 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11163 and similarly for >= into !=. */
11164 if ((code == LT_EXPR || code == GE_EXPR)
11165 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11166 && TREE_CODE (arg1) == LSHIFT_EXPR
11167 && integer_onep (TREE_OPERAND (arg1, 0)))
11168 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11169 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11170 TREE_OPERAND (arg1, 1)),
11171 build_zero_cst (TREE_TYPE (arg0)));
11173 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11174 otherwise Y might be >= # of bits in X's type and thus e.g.
11175 (unsigned char) (1 << Y) for Y 15 might be 0.
11176 If the cast is widening, then 1 << Y should have unsigned type,
11177 otherwise if Y is number of bits in the signed shift type minus 1,
11178 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11179 31 might be 0xffffffff80000000. */
11180 if ((code == LT_EXPR || code == GE_EXPR)
11181 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11182 && CONVERT_EXPR_P (arg1)
11183 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11184 && (element_precision (TREE_TYPE (arg1))
11185 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11186 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11187 || (element_precision (TREE_TYPE (arg1))
11188 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11189 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11191 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11192 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11193 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11194 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11195 build_zero_cst (TREE_TYPE (arg0)));
11198 return NULL_TREE;
11200 case UNORDERED_EXPR:
11201 case ORDERED_EXPR:
11202 case UNLT_EXPR:
11203 case UNLE_EXPR:
11204 case UNGT_EXPR:
11205 case UNGE_EXPR:
11206 case UNEQ_EXPR:
11207 case LTGT_EXPR:
11208 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11210 tree targ0 = strip_float_extensions (arg0);
11211 tree targ1 = strip_float_extensions (arg1);
11212 tree newtype = TREE_TYPE (targ0);
11214 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11215 newtype = TREE_TYPE (targ1);
11217 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11218 return fold_build2_loc (loc, code, type,
11219 fold_convert_loc (loc, newtype, targ0),
11220 fold_convert_loc (loc, newtype, targ1));
11223 return NULL_TREE;
11225 case COMPOUND_EXPR:
11226 /* When pedantic, a compound expression can be neither an lvalue
11227 nor an integer constant expression. */
11228 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11229 return NULL_TREE;
11230 /* Don't let (0, 0) be null pointer constant. */
11231 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11232 : fold_convert_loc (loc, type, arg1);
11233 return pedantic_non_lvalue_loc (loc, tem);
11235 case ASSERT_EXPR:
11236 /* An ASSERT_EXPR should never be passed to fold_binary. */
11237 gcc_unreachable ();
11239 default:
11240 return NULL_TREE;
11241 } /* switch (code) */
11244 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11245 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11246 of GOTO_EXPR. */
11248 static tree
11249 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11251 switch (TREE_CODE (*tp))
11253 case LABEL_EXPR:
11254 return *tp;
11256 case GOTO_EXPR:
11257 *walk_subtrees = 0;
11259 /* fall through */
11261 default:
11262 return NULL_TREE;
11266 /* Return whether the sub-tree ST contains a label which is accessible from
11267 outside the sub-tree. */
11269 static bool
11270 contains_label_p (tree st)
11272 return
11273 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11276 /* Fold a ternary expression of code CODE and type TYPE with operands
11277 OP0, OP1, and OP2. Return the folded expression if folding is
11278 successful. Otherwise, return NULL_TREE. */
11280 tree
11281 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11282 tree op0, tree op1, tree op2)
11284 tree tem;
11285 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11286 enum tree_code_class kind = TREE_CODE_CLASS (code);
11288 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11289 && TREE_CODE_LENGTH (code) == 3);
11291 /* If this is a commutative operation, and OP0 is a constant, move it
11292 to OP1 to reduce the number of tests below. */
11293 if (commutative_ternary_tree_code (code)
11294 && tree_swap_operands_p (op0, op1, true))
11295 return fold_build3_loc (loc, code, type, op1, op0, op2);
11297 tem = generic_simplify (loc, code, type, op0, op1, op2);
11298 if (tem)
11299 return tem;
11301 /* Strip any conversions that don't change the mode. This is safe
11302 for every expression, except for a comparison expression because
11303 its signedness is derived from its operands. So, in the latter
11304 case, only strip conversions that don't change the signedness.
11306 Note that this is done as an internal manipulation within the
11307 constant folder, in order to find the simplest representation of
11308 the arguments so that their form can be studied. In any cases,
11309 the appropriate type conversions should be put back in the tree
11310 that will get out of the constant folder. */
11311 if (op0)
11313 arg0 = op0;
11314 STRIP_NOPS (arg0);
11317 if (op1)
11319 arg1 = op1;
11320 STRIP_NOPS (arg1);
11323 if (op2)
11325 arg2 = op2;
11326 STRIP_NOPS (arg2);
11329 switch (code)
11331 case COMPONENT_REF:
11332 if (TREE_CODE (arg0) == CONSTRUCTOR
11333 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11335 unsigned HOST_WIDE_INT idx;
11336 tree field, value;
11337 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11338 if (field == arg1)
11339 return value;
11341 return NULL_TREE;
11343 case COND_EXPR:
11344 case VEC_COND_EXPR:
11345 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11346 so all simple results must be passed through pedantic_non_lvalue. */
11347 if (TREE_CODE (arg0) == INTEGER_CST)
11349 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11350 tem = integer_zerop (arg0) ? op2 : op1;
11351 /* Only optimize constant conditions when the selected branch
11352 has the same type as the COND_EXPR. This avoids optimizing
11353 away "c ? x : throw", where the throw has a void type.
11354 Avoid throwing away that operand which contains label. */
11355 if ((!TREE_SIDE_EFFECTS (unused_op)
11356 || !contains_label_p (unused_op))
11357 && (! VOID_TYPE_P (TREE_TYPE (tem))
11358 || VOID_TYPE_P (type)))
11359 return pedantic_non_lvalue_loc (loc, tem);
11360 return NULL_TREE;
11362 else if (TREE_CODE (arg0) == VECTOR_CST)
11364 if ((TREE_CODE (arg1) == VECTOR_CST
11365 || TREE_CODE (arg1) == CONSTRUCTOR)
11366 && (TREE_CODE (arg2) == VECTOR_CST
11367 || TREE_CODE (arg2) == CONSTRUCTOR))
11369 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11370 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11371 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11372 for (i = 0; i < nelts; i++)
11374 tree val = VECTOR_CST_ELT (arg0, i);
11375 if (integer_all_onesp (val))
11376 sel[i] = i;
11377 else if (integer_zerop (val))
11378 sel[i] = nelts + i;
11379 else /* Currently unreachable. */
11380 return NULL_TREE;
11382 tree t = fold_vec_perm (type, arg1, arg2, sel);
11383 if (t != NULL_TREE)
11384 return t;
11388 /* If we have A op B ? A : C, we may be able to convert this to a
11389 simpler expression, depending on the operation and the values
11390 of B and C. Signed zeros prevent all of these transformations,
11391 for reasons given above each one.
11393 Also try swapping the arguments and inverting the conditional. */
11394 if (COMPARISON_CLASS_P (arg0)
11395 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11396 arg1, TREE_OPERAND (arg0, 1))
11397 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11399 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11400 if (tem)
11401 return tem;
11404 if (COMPARISON_CLASS_P (arg0)
11405 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11406 op2,
11407 TREE_OPERAND (arg0, 1))
11408 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11410 location_t loc0 = expr_location_or (arg0, loc);
11411 tem = fold_invert_truthvalue (loc0, arg0);
11412 if (tem && COMPARISON_CLASS_P (tem))
11414 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11415 if (tem)
11416 return tem;
11420 /* If the second operand is simpler than the third, swap them
11421 since that produces better jump optimization results. */
11422 if (truth_value_p (TREE_CODE (arg0))
11423 && tree_swap_operands_p (op1, op2, false))
11425 location_t loc0 = expr_location_or (arg0, loc);
11426 /* See if this can be inverted. If it can't, possibly because
11427 it was a floating-point inequality comparison, don't do
11428 anything. */
11429 tem = fold_invert_truthvalue (loc0, arg0);
11430 if (tem)
11431 return fold_build3_loc (loc, code, type, tem, op2, op1);
11434 /* Convert A ? 1 : 0 to simply A. */
11435 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11436 : (integer_onep (op1)
11437 && !VECTOR_TYPE_P (type)))
11438 && integer_zerop (op2)
11439 /* If we try to convert OP0 to our type, the
11440 call to fold will try to move the conversion inside
11441 a COND, which will recurse. In that case, the COND_EXPR
11442 is probably the best choice, so leave it alone. */
11443 && type == TREE_TYPE (arg0))
11444 return pedantic_non_lvalue_loc (loc, arg0);
11446 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11447 over COND_EXPR in cases such as floating point comparisons. */
11448 if (integer_zerop (op1)
11449 && code == COND_EXPR
11450 && integer_onep (op2)
11451 && !VECTOR_TYPE_P (type)
11452 && truth_value_p (TREE_CODE (arg0)))
11453 return pedantic_non_lvalue_loc (loc,
11454 fold_convert_loc (loc, type,
11455 invert_truthvalue_loc (loc,
11456 arg0)));
11458 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11459 if (TREE_CODE (arg0) == LT_EXPR
11460 && integer_zerop (TREE_OPERAND (arg0, 1))
11461 && integer_zerop (op2)
11462 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11464 /* sign_bit_p looks through both zero and sign extensions,
11465 but for this optimization only sign extensions are
11466 usable. */
11467 tree tem2 = TREE_OPERAND (arg0, 0);
11468 while (tem != tem2)
11470 if (TREE_CODE (tem2) != NOP_EXPR
11471 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11473 tem = NULL_TREE;
11474 break;
11476 tem2 = TREE_OPERAND (tem2, 0);
11478 /* sign_bit_p only checks ARG1 bits within A's precision.
11479 If <sign bit of A> has wider type than A, bits outside
11480 of A's precision in <sign bit of A> need to be checked.
11481 If they are all 0, this optimization needs to be done
11482 in unsigned A's type, if they are all 1 in signed A's type,
11483 otherwise this can't be done. */
11484 if (tem
11485 && TYPE_PRECISION (TREE_TYPE (tem))
11486 < TYPE_PRECISION (TREE_TYPE (arg1))
11487 && TYPE_PRECISION (TREE_TYPE (tem))
11488 < TYPE_PRECISION (type))
11490 int inner_width, outer_width;
11491 tree tem_type;
11493 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11494 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11495 if (outer_width > TYPE_PRECISION (type))
11496 outer_width = TYPE_PRECISION (type);
11498 wide_int mask = wi::shifted_mask
11499 (inner_width, outer_width - inner_width, false,
11500 TYPE_PRECISION (TREE_TYPE (arg1)));
11502 wide_int common = mask & arg1;
11503 if (common == mask)
11505 tem_type = signed_type_for (TREE_TYPE (tem));
11506 tem = fold_convert_loc (loc, tem_type, tem);
11508 else if (common == 0)
11510 tem_type = unsigned_type_for (TREE_TYPE (tem));
11511 tem = fold_convert_loc (loc, tem_type, tem);
11513 else
11514 tem = NULL;
11517 if (tem)
11518 return
11519 fold_convert_loc (loc, type,
11520 fold_build2_loc (loc, BIT_AND_EXPR,
11521 TREE_TYPE (tem), tem,
11522 fold_convert_loc (loc,
11523 TREE_TYPE (tem),
11524 arg1)));
11527 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11528 already handled above. */
11529 if (TREE_CODE (arg0) == BIT_AND_EXPR
11530 && integer_onep (TREE_OPERAND (arg0, 1))
11531 && integer_zerop (op2)
11532 && integer_pow2p (arg1))
11534 tree tem = TREE_OPERAND (arg0, 0);
11535 STRIP_NOPS (tem);
11536 if (TREE_CODE (tem) == RSHIFT_EXPR
11537 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11538 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11539 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11540 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11541 TREE_OPERAND (tem, 0), arg1);
11544 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11545 is probably obsolete because the first operand should be a
11546 truth value (that's why we have the two cases above), but let's
11547 leave it in until we can confirm this for all front-ends. */
11548 if (integer_zerop (op2)
11549 && TREE_CODE (arg0) == NE_EXPR
11550 && integer_zerop (TREE_OPERAND (arg0, 1))
11551 && integer_pow2p (arg1)
11552 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11553 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11554 arg1, OEP_ONLY_CONST))
11555 return pedantic_non_lvalue_loc (loc,
11556 fold_convert_loc (loc, type,
11557 TREE_OPERAND (arg0, 0)));
11559 /* Disable the transformations below for vectors, since
11560 fold_binary_op_with_conditional_arg may undo them immediately,
11561 yielding an infinite loop. */
11562 if (code == VEC_COND_EXPR)
11563 return NULL_TREE;
11565 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11566 if (integer_zerop (op2)
11567 && truth_value_p (TREE_CODE (arg0))
11568 && truth_value_p (TREE_CODE (arg1))
11569 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11570 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11571 : TRUTH_ANDIF_EXPR,
11572 type, fold_convert_loc (loc, type, arg0), arg1);
11574 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11575 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11576 && truth_value_p (TREE_CODE (arg0))
11577 && truth_value_p (TREE_CODE (arg1))
11578 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11580 location_t loc0 = expr_location_or (arg0, loc);
11581 /* Only perform transformation if ARG0 is easily inverted. */
11582 tem = fold_invert_truthvalue (loc0, arg0);
11583 if (tem)
11584 return fold_build2_loc (loc, code == VEC_COND_EXPR
11585 ? BIT_IOR_EXPR
11586 : TRUTH_ORIF_EXPR,
11587 type, fold_convert_loc (loc, type, tem),
11588 arg1);
11591 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11592 if (integer_zerop (arg1)
11593 && truth_value_p (TREE_CODE (arg0))
11594 && truth_value_p (TREE_CODE (op2))
11595 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11597 location_t loc0 = expr_location_or (arg0, loc);
11598 /* Only perform transformation if ARG0 is easily inverted. */
11599 tem = fold_invert_truthvalue (loc0, arg0);
11600 if (tem)
11601 return fold_build2_loc (loc, code == VEC_COND_EXPR
11602 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11603 type, fold_convert_loc (loc, type, tem),
11604 op2);
11607 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11608 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11609 && truth_value_p (TREE_CODE (arg0))
11610 && truth_value_p (TREE_CODE (op2))
11611 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11612 return fold_build2_loc (loc, code == VEC_COND_EXPR
11613 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11614 type, fold_convert_loc (loc, type, arg0), op2);
11616 return NULL_TREE;
11618 case CALL_EXPR:
11619 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11620 of fold_ternary on them. */
11621 gcc_unreachable ();
11623 case BIT_FIELD_REF:
11624 if (TREE_CODE (arg0) == VECTOR_CST
11625 && (type == TREE_TYPE (TREE_TYPE (arg0))
11626 || (TREE_CODE (type) == VECTOR_TYPE
11627 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11629 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11630 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11631 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11632 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11634 if (n != 0
11635 && (idx % width) == 0
11636 && (n % width) == 0
11637 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11639 idx = idx / width;
11640 n = n / width;
11642 if (TREE_CODE (arg0) == VECTOR_CST)
11644 if (n == 1)
11645 return VECTOR_CST_ELT (arg0, idx);
11647 tree *vals = XALLOCAVEC (tree, n);
11648 for (unsigned i = 0; i < n; ++i)
11649 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11650 return build_vector (type, vals);
11655 /* On constants we can use native encode/interpret to constant
11656 fold (nearly) all BIT_FIELD_REFs. */
11657 if (CONSTANT_CLASS_P (arg0)
11658 && can_native_interpret_type_p (type)
11659 && BITS_PER_UNIT == 8)
11661 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11662 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11663 /* Limit us to a reasonable amount of work. To relax the
11664 other limitations we need bit-shifting of the buffer
11665 and rounding up the size. */
11666 if (bitpos % BITS_PER_UNIT == 0
11667 && bitsize % BITS_PER_UNIT == 0
11668 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11670 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11671 unsigned HOST_WIDE_INT len
11672 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11673 bitpos / BITS_PER_UNIT);
11674 if (len > 0
11675 && len * BITS_PER_UNIT >= bitsize)
11677 tree v = native_interpret_expr (type, b,
11678 bitsize / BITS_PER_UNIT);
11679 if (v)
11680 return v;
11685 return NULL_TREE;
11687 case FMA_EXPR:
11688 /* For integers we can decompose the FMA if possible. */
11689 if (TREE_CODE (arg0) == INTEGER_CST
11690 && TREE_CODE (arg1) == INTEGER_CST)
11691 return fold_build2_loc (loc, PLUS_EXPR, type,
11692 const_binop (MULT_EXPR, arg0, arg1), arg2);
11693 if (integer_zerop (arg2))
11694 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11696 return fold_fma (loc, type, arg0, arg1, arg2);
11698 case VEC_PERM_EXPR:
11699 if (TREE_CODE (arg2) == VECTOR_CST)
11701 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11702 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11703 unsigned char *sel2 = sel + nelts;
11704 bool need_mask_canon = false;
11705 bool need_mask_canon2 = false;
11706 bool all_in_vec0 = true;
11707 bool all_in_vec1 = true;
11708 bool maybe_identity = true;
11709 bool single_arg = (op0 == op1);
11710 bool changed = false;
11712 mask2 = 2 * nelts - 1;
11713 mask = single_arg ? (nelts - 1) : mask2;
11714 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11715 for (i = 0; i < nelts; i++)
11717 tree val = VECTOR_CST_ELT (arg2, i);
11718 if (TREE_CODE (val) != INTEGER_CST)
11719 return NULL_TREE;
11721 /* Make sure that the perm value is in an acceptable
11722 range. */
11723 wide_int t = val;
11724 need_mask_canon |= wi::gtu_p (t, mask);
11725 need_mask_canon2 |= wi::gtu_p (t, mask2);
11726 sel[i] = t.to_uhwi () & mask;
11727 sel2[i] = t.to_uhwi () & mask2;
11729 if (sel[i] < nelts)
11730 all_in_vec1 = false;
11731 else
11732 all_in_vec0 = false;
11734 if ((sel[i] & (nelts-1)) != i)
11735 maybe_identity = false;
11738 if (maybe_identity)
11740 if (all_in_vec0)
11741 return op0;
11742 if (all_in_vec1)
11743 return op1;
11746 if (all_in_vec0)
11747 op1 = op0;
11748 else if (all_in_vec1)
11750 op0 = op1;
11751 for (i = 0; i < nelts; i++)
11752 sel[i] -= nelts;
11753 need_mask_canon = true;
11756 if ((TREE_CODE (op0) == VECTOR_CST
11757 || TREE_CODE (op0) == CONSTRUCTOR)
11758 && (TREE_CODE (op1) == VECTOR_CST
11759 || TREE_CODE (op1) == CONSTRUCTOR))
11761 tree t = fold_vec_perm (type, op0, op1, sel);
11762 if (t != NULL_TREE)
11763 return t;
11766 if (op0 == op1 && !single_arg)
11767 changed = true;
11769 /* Some targets are deficient and fail to expand a single
11770 argument permutation while still allowing an equivalent
11771 2-argument version. */
11772 if (need_mask_canon && arg2 == op2
11773 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11774 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11776 need_mask_canon = need_mask_canon2;
11777 sel = sel2;
11780 if (need_mask_canon && arg2 == op2)
11782 tree *tsel = XALLOCAVEC (tree, nelts);
11783 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11784 for (i = 0; i < nelts; i++)
11785 tsel[i] = build_int_cst (eltype, sel[i]);
11786 op2 = build_vector (TREE_TYPE (arg2), tsel);
11787 changed = true;
11790 if (changed)
11791 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11793 return NULL_TREE;
11795 case BIT_INSERT_EXPR:
11796 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11797 if (TREE_CODE (arg0) == INTEGER_CST
11798 && TREE_CODE (arg1) == INTEGER_CST)
11800 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11801 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11802 wide_int tem = wi::bit_and (arg0,
11803 wi::shifted_mask (bitpos, bitsize, true,
11804 TYPE_PRECISION (type)));
11805 wide_int tem2
11806 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11807 bitsize), bitpos);
11808 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11810 else if (TREE_CODE (arg0) == VECTOR_CST
11811 && CONSTANT_CLASS_P (arg1)
11812 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11813 TREE_TYPE (arg1)))
11815 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11816 unsigned HOST_WIDE_INT elsize
11817 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11818 if (bitpos % elsize == 0)
11820 unsigned k = bitpos / elsize;
11821 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11822 return arg0;
11823 else
11825 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11826 memcpy (elts, VECTOR_CST_ELTS (arg0),
11827 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11828 elts[k] = arg1;
11829 return build_vector (type, elts);
11833 return NULL_TREE;
11835 default:
11836 return NULL_TREE;
11837 } /* switch (code) */
11840 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11841 of an array (or vector). */
11843 tree
11844 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11846 tree index_type = NULL_TREE;
11847 offset_int low_bound = 0;
11849 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11851 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11852 if (domain_type && TYPE_MIN_VALUE (domain_type))
11854 /* Static constructors for variably sized objects makes no sense. */
11855 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11856 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11857 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11861 if (index_type)
11862 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11863 TYPE_SIGN (index_type));
11865 offset_int index = low_bound - 1;
11866 if (index_type)
11867 index = wi::ext (index, TYPE_PRECISION (index_type),
11868 TYPE_SIGN (index_type));
11870 offset_int max_index;
11871 unsigned HOST_WIDE_INT cnt;
11872 tree cfield, cval;
11874 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11876 /* Array constructor might explicitly set index, or specify a range,
11877 or leave index NULL meaning that it is next index after previous
11878 one. */
11879 if (cfield)
11881 if (TREE_CODE (cfield) == INTEGER_CST)
11882 max_index = index = wi::to_offset (cfield);
11883 else
11885 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11886 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11887 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11890 else
11892 index += 1;
11893 if (index_type)
11894 index = wi::ext (index, TYPE_PRECISION (index_type),
11895 TYPE_SIGN (index_type));
11896 max_index = index;
11899 /* Do we have match? */
11900 if (wi::cmpu (access_index, index) >= 0
11901 && wi::cmpu (access_index, max_index) <= 0)
11902 return cval;
11904 return NULL_TREE;
11907 /* Perform constant folding and related simplification of EXPR.
11908 The related simplifications include x*1 => x, x*0 => 0, etc.,
11909 and application of the associative law.
11910 NOP_EXPR conversions may be removed freely (as long as we
11911 are careful not to change the type of the overall expression).
11912 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11913 but we can constant-fold them if they have constant operands. */
11915 #ifdef ENABLE_FOLD_CHECKING
11916 # define fold(x) fold_1 (x)
11917 static tree fold_1 (tree);
11918 static
11919 #endif
11920 tree
11921 fold (tree expr)
11923 const tree t = expr;
11924 enum tree_code code = TREE_CODE (t);
11925 enum tree_code_class kind = TREE_CODE_CLASS (code);
11926 tree tem;
11927 location_t loc = EXPR_LOCATION (expr);
11929 /* Return right away if a constant. */
11930 if (kind == tcc_constant)
11931 return t;
11933 /* CALL_EXPR-like objects with variable numbers of operands are
11934 treated specially. */
11935 if (kind == tcc_vl_exp)
11937 if (code == CALL_EXPR)
11939 tem = fold_call_expr (loc, expr, false);
11940 return tem ? tem : expr;
11942 return expr;
11945 if (IS_EXPR_CODE_CLASS (kind))
11947 tree type = TREE_TYPE (t);
11948 tree op0, op1, op2;
11950 switch (TREE_CODE_LENGTH (code))
11952 case 1:
11953 op0 = TREE_OPERAND (t, 0);
11954 tem = fold_unary_loc (loc, code, type, op0);
11955 return tem ? tem : expr;
11956 case 2:
11957 op0 = TREE_OPERAND (t, 0);
11958 op1 = TREE_OPERAND (t, 1);
11959 tem = fold_binary_loc (loc, code, type, op0, op1);
11960 return tem ? tem : expr;
11961 case 3:
11962 op0 = TREE_OPERAND (t, 0);
11963 op1 = TREE_OPERAND (t, 1);
11964 op2 = TREE_OPERAND (t, 2);
11965 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11966 return tem ? tem : expr;
11967 default:
11968 break;
11972 switch (code)
11974 case ARRAY_REF:
11976 tree op0 = TREE_OPERAND (t, 0);
11977 tree op1 = TREE_OPERAND (t, 1);
11979 if (TREE_CODE (op1) == INTEGER_CST
11980 && TREE_CODE (op0) == CONSTRUCTOR
11981 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11983 tree val = get_array_ctor_element_at_index (op0,
11984 wi::to_offset (op1));
11985 if (val)
11986 return val;
11989 return t;
11992 /* Return a VECTOR_CST if possible. */
11993 case CONSTRUCTOR:
11995 tree type = TREE_TYPE (t);
11996 if (TREE_CODE (type) != VECTOR_TYPE)
11997 return t;
11999 unsigned i;
12000 tree val;
12001 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12002 if (! CONSTANT_CLASS_P (val))
12003 return t;
12005 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12008 case CONST_DECL:
12009 return fold (DECL_INITIAL (t));
12011 default:
12012 return t;
12013 } /* switch (code) */
12016 #ifdef ENABLE_FOLD_CHECKING
12017 #undef fold
12019 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12020 hash_table<nofree_ptr_hash<const tree_node> > *);
12021 static void fold_check_failed (const_tree, const_tree);
12022 void print_fold_checksum (const_tree);
12024 /* When --enable-checking=fold, compute a digest of expr before
12025 and after actual fold call to see if fold did not accidentally
12026 change original expr. */
12028 tree
12029 fold (tree expr)
12031 tree ret;
12032 struct md5_ctx ctx;
12033 unsigned char checksum_before[16], checksum_after[16];
12034 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12036 md5_init_ctx (&ctx);
12037 fold_checksum_tree (expr, &ctx, &ht);
12038 md5_finish_ctx (&ctx, checksum_before);
12039 ht.empty ();
12041 ret = fold_1 (expr);
12043 md5_init_ctx (&ctx);
12044 fold_checksum_tree (expr, &ctx, &ht);
12045 md5_finish_ctx (&ctx, checksum_after);
12047 if (memcmp (checksum_before, checksum_after, 16))
12048 fold_check_failed (expr, ret);
12050 return ret;
12053 void
12054 print_fold_checksum (const_tree expr)
12056 struct md5_ctx ctx;
12057 unsigned char checksum[16], cnt;
12058 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12060 md5_init_ctx (&ctx);
12061 fold_checksum_tree (expr, &ctx, &ht);
12062 md5_finish_ctx (&ctx, checksum);
12063 for (cnt = 0; cnt < 16; ++cnt)
12064 fprintf (stderr, "%02x", checksum[cnt]);
12065 putc ('\n', stderr);
12068 static void
12069 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12071 internal_error ("fold check: original tree changed by fold");
12074 static void
12075 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12076 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12078 const tree_node **slot;
12079 enum tree_code code;
12080 union tree_node buf;
12081 int i, len;
12083 recursive_label:
12084 if (expr == NULL)
12085 return;
12086 slot = ht->find_slot (expr, INSERT);
12087 if (*slot != NULL)
12088 return;
12089 *slot = expr;
12090 code = TREE_CODE (expr);
12091 if (TREE_CODE_CLASS (code) == tcc_declaration
12092 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12094 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12095 memcpy ((char *) &buf, expr, tree_size (expr));
12096 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12097 buf.decl_with_vis.symtab_node = NULL;
12098 expr = (tree) &buf;
12100 else if (TREE_CODE_CLASS (code) == tcc_type
12101 && (TYPE_POINTER_TO (expr)
12102 || TYPE_REFERENCE_TO (expr)
12103 || TYPE_CACHED_VALUES_P (expr)
12104 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12105 || TYPE_NEXT_VARIANT (expr)
12106 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12108 /* Allow these fields to be modified. */
12109 tree tmp;
12110 memcpy ((char *) &buf, expr, tree_size (expr));
12111 expr = tmp = (tree) &buf;
12112 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12113 TYPE_POINTER_TO (tmp) = NULL;
12114 TYPE_REFERENCE_TO (tmp) = NULL;
12115 TYPE_NEXT_VARIANT (tmp) = NULL;
12116 TYPE_ALIAS_SET (tmp) = -1;
12117 if (TYPE_CACHED_VALUES_P (tmp))
12119 TYPE_CACHED_VALUES_P (tmp) = 0;
12120 TYPE_CACHED_VALUES (tmp) = NULL;
12123 md5_process_bytes (expr, tree_size (expr), ctx);
12124 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12125 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12126 if (TREE_CODE_CLASS (code) != tcc_type
12127 && TREE_CODE_CLASS (code) != tcc_declaration
12128 && code != TREE_LIST
12129 && code != SSA_NAME
12130 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12131 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12132 switch (TREE_CODE_CLASS (code))
12134 case tcc_constant:
12135 switch (code)
12137 case STRING_CST:
12138 md5_process_bytes (TREE_STRING_POINTER (expr),
12139 TREE_STRING_LENGTH (expr), ctx);
12140 break;
12141 case COMPLEX_CST:
12142 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12143 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12144 break;
12145 case VECTOR_CST:
12146 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12147 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12148 break;
12149 default:
12150 break;
12152 break;
12153 case tcc_exceptional:
12154 switch (code)
12156 case TREE_LIST:
12157 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12158 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12159 expr = TREE_CHAIN (expr);
12160 goto recursive_label;
12161 break;
12162 case TREE_VEC:
12163 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12164 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12165 break;
12166 default:
12167 break;
12169 break;
12170 case tcc_expression:
12171 case tcc_reference:
12172 case tcc_comparison:
12173 case tcc_unary:
12174 case tcc_binary:
12175 case tcc_statement:
12176 case tcc_vl_exp:
12177 len = TREE_OPERAND_LENGTH (expr);
12178 for (i = 0; i < len; ++i)
12179 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12180 break;
12181 case tcc_declaration:
12182 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12183 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12184 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12186 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12187 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12188 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12189 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12190 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12193 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12195 if (TREE_CODE (expr) == FUNCTION_DECL)
12197 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12198 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12200 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12202 break;
12203 case tcc_type:
12204 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12205 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12206 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12207 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12208 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12209 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12210 if (INTEGRAL_TYPE_P (expr)
12211 || SCALAR_FLOAT_TYPE_P (expr))
12213 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12214 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12216 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12217 if (TREE_CODE (expr) == RECORD_TYPE
12218 || TREE_CODE (expr) == UNION_TYPE
12219 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12220 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12221 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12222 break;
12223 default:
12224 break;
12228 /* Helper function for outputting the checksum of a tree T. When
12229 debugging with gdb, you can "define mynext" to be "next" followed
12230 by "call debug_fold_checksum (op0)", then just trace down till the
12231 outputs differ. */
12233 DEBUG_FUNCTION void
12234 debug_fold_checksum (const_tree t)
12236 int i;
12237 unsigned char checksum[16];
12238 struct md5_ctx ctx;
12239 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12241 md5_init_ctx (&ctx);
12242 fold_checksum_tree (t, &ctx, &ht);
12243 md5_finish_ctx (&ctx, checksum);
12244 ht.empty ();
12246 for (i = 0; i < 16; i++)
12247 fprintf (stderr, "%d ", checksum[i]);
12249 fprintf (stderr, "\n");
12252 #endif
12254 /* Fold a unary tree expression with code CODE of type TYPE with an
12255 operand OP0. LOC is the location of the resulting expression.
12256 Return a folded expression if successful. Otherwise, return a tree
12257 expression with code CODE of type TYPE with an operand OP0. */
12259 tree
12260 fold_build1_stat_loc (location_t loc,
12261 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12263 tree tem;
12264 #ifdef ENABLE_FOLD_CHECKING
12265 unsigned char checksum_before[16], checksum_after[16];
12266 struct md5_ctx ctx;
12267 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12269 md5_init_ctx (&ctx);
12270 fold_checksum_tree (op0, &ctx, &ht);
12271 md5_finish_ctx (&ctx, checksum_before);
12272 ht.empty ();
12273 #endif
12275 tem = fold_unary_loc (loc, code, type, op0);
12276 if (!tem)
12277 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12279 #ifdef ENABLE_FOLD_CHECKING
12280 md5_init_ctx (&ctx);
12281 fold_checksum_tree (op0, &ctx, &ht);
12282 md5_finish_ctx (&ctx, checksum_after);
12284 if (memcmp (checksum_before, checksum_after, 16))
12285 fold_check_failed (op0, tem);
12286 #endif
12287 return tem;
12290 /* Fold a binary tree expression with code CODE of type TYPE with
12291 operands OP0 and OP1. LOC is the location of the resulting
12292 expression. Return a folded expression if successful. Otherwise,
12293 return a tree expression with code CODE of type TYPE with operands
12294 OP0 and OP1. */
12296 tree
12297 fold_build2_stat_loc (location_t loc,
12298 enum tree_code code, tree type, tree op0, tree op1
12299 MEM_STAT_DECL)
12301 tree tem;
12302 #ifdef ENABLE_FOLD_CHECKING
12303 unsigned char checksum_before_op0[16],
12304 checksum_before_op1[16],
12305 checksum_after_op0[16],
12306 checksum_after_op1[16];
12307 struct md5_ctx ctx;
12308 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12310 md5_init_ctx (&ctx);
12311 fold_checksum_tree (op0, &ctx, &ht);
12312 md5_finish_ctx (&ctx, checksum_before_op0);
12313 ht.empty ();
12315 md5_init_ctx (&ctx);
12316 fold_checksum_tree (op1, &ctx, &ht);
12317 md5_finish_ctx (&ctx, checksum_before_op1);
12318 ht.empty ();
12319 #endif
12321 tem = fold_binary_loc (loc, code, type, op0, op1);
12322 if (!tem)
12323 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12325 #ifdef ENABLE_FOLD_CHECKING
12326 md5_init_ctx (&ctx);
12327 fold_checksum_tree (op0, &ctx, &ht);
12328 md5_finish_ctx (&ctx, checksum_after_op0);
12329 ht.empty ();
12331 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12332 fold_check_failed (op0, tem);
12334 md5_init_ctx (&ctx);
12335 fold_checksum_tree (op1, &ctx, &ht);
12336 md5_finish_ctx (&ctx, checksum_after_op1);
12338 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12339 fold_check_failed (op1, tem);
12340 #endif
12341 return tem;
12344 /* Fold a ternary tree expression with code CODE of type TYPE with
12345 operands OP0, OP1, and OP2. Return a folded expression if
12346 successful. Otherwise, return a tree expression with code CODE of
12347 type TYPE with operands OP0, OP1, and OP2. */
12349 tree
12350 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12351 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12353 tree tem;
12354 #ifdef ENABLE_FOLD_CHECKING
12355 unsigned char checksum_before_op0[16],
12356 checksum_before_op1[16],
12357 checksum_before_op2[16],
12358 checksum_after_op0[16],
12359 checksum_after_op1[16],
12360 checksum_after_op2[16];
12361 struct md5_ctx ctx;
12362 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12364 md5_init_ctx (&ctx);
12365 fold_checksum_tree (op0, &ctx, &ht);
12366 md5_finish_ctx (&ctx, checksum_before_op0);
12367 ht.empty ();
12369 md5_init_ctx (&ctx);
12370 fold_checksum_tree (op1, &ctx, &ht);
12371 md5_finish_ctx (&ctx, checksum_before_op1);
12372 ht.empty ();
12374 md5_init_ctx (&ctx);
12375 fold_checksum_tree (op2, &ctx, &ht);
12376 md5_finish_ctx (&ctx, checksum_before_op2);
12377 ht.empty ();
12378 #endif
12380 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12381 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12382 if (!tem)
12383 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12385 #ifdef ENABLE_FOLD_CHECKING
12386 md5_init_ctx (&ctx);
12387 fold_checksum_tree (op0, &ctx, &ht);
12388 md5_finish_ctx (&ctx, checksum_after_op0);
12389 ht.empty ();
12391 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12392 fold_check_failed (op0, tem);
12394 md5_init_ctx (&ctx);
12395 fold_checksum_tree (op1, &ctx, &ht);
12396 md5_finish_ctx (&ctx, checksum_after_op1);
12397 ht.empty ();
12399 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12400 fold_check_failed (op1, tem);
12402 md5_init_ctx (&ctx);
12403 fold_checksum_tree (op2, &ctx, &ht);
12404 md5_finish_ctx (&ctx, checksum_after_op2);
12406 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12407 fold_check_failed (op2, tem);
12408 #endif
12409 return tem;
12412 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12413 arguments in ARGARRAY, and a null static chain.
12414 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12415 of type TYPE from the given operands as constructed by build_call_array. */
12417 tree
12418 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12419 int nargs, tree *argarray)
12421 tree tem;
12422 #ifdef ENABLE_FOLD_CHECKING
12423 unsigned char checksum_before_fn[16],
12424 checksum_before_arglist[16],
12425 checksum_after_fn[16],
12426 checksum_after_arglist[16];
12427 struct md5_ctx ctx;
12428 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12429 int i;
12431 md5_init_ctx (&ctx);
12432 fold_checksum_tree (fn, &ctx, &ht);
12433 md5_finish_ctx (&ctx, checksum_before_fn);
12434 ht.empty ();
12436 md5_init_ctx (&ctx);
12437 for (i = 0; i < nargs; i++)
12438 fold_checksum_tree (argarray[i], &ctx, &ht);
12439 md5_finish_ctx (&ctx, checksum_before_arglist);
12440 ht.empty ();
12441 #endif
12443 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12444 if (!tem)
12445 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12447 #ifdef ENABLE_FOLD_CHECKING
12448 md5_init_ctx (&ctx);
12449 fold_checksum_tree (fn, &ctx, &ht);
12450 md5_finish_ctx (&ctx, checksum_after_fn);
12451 ht.empty ();
12453 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12454 fold_check_failed (fn, tem);
12456 md5_init_ctx (&ctx);
12457 for (i = 0; i < nargs; i++)
12458 fold_checksum_tree (argarray[i], &ctx, &ht);
12459 md5_finish_ctx (&ctx, checksum_after_arglist);
12461 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12462 fold_check_failed (NULL_TREE, tem);
12463 #endif
12464 return tem;
12467 /* Perform constant folding and related simplification of initializer
12468 expression EXPR. These behave identically to "fold_buildN" but ignore
12469 potential run-time traps and exceptions that fold must preserve. */
12471 #define START_FOLD_INIT \
12472 int saved_signaling_nans = flag_signaling_nans;\
12473 int saved_trapping_math = flag_trapping_math;\
12474 int saved_rounding_math = flag_rounding_math;\
12475 int saved_trapv = flag_trapv;\
12476 int saved_folding_initializer = folding_initializer;\
12477 flag_signaling_nans = 0;\
12478 flag_trapping_math = 0;\
12479 flag_rounding_math = 0;\
12480 flag_trapv = 0;\
12481 folding_initializer = 1;
12483 #define END_FOLD_INIT \
12484 flag_signaling_nans = saved_signaling_nans;\
12485 flag_trapping_math = saved_trapping_math;\
12486 flag_rounding_math = saved_rounding_math;\
12487 flag_trapv = saved_trapv;\
12488 folding_initializer = saved_folding_initializer;
12490 tree
12491 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12492 tree type, tree op)
12494 tree result;
12495 START_FOLD_INIT;
12497 result = fold_build1_loc (loc, code, type, op);
12499 END_FOLD_INIT;
12500 return result;
12503 tree
12504 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12505 tree type, tree op0, tree op1)
12507 tree result;
12508 START_FOLD_INIT;
12510 result = fold_build2_loc (loc, code, type, op0, op1);
12512 END_FOLD_INIT;
12513 return result;
12516 tree
12517 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12518 int nargs, tree *argarray)
12520 tree result;
12521 START_FOLD_INIT;
12523 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12525 END_FOLD_INIT;
12526 return result;
12529 #undef START_FOLD_INIT
12530 #undef END_FOLD_INIT
12532 /* Determine if first argument is a multiple of second argument. Return 0 if
12533 it is not, or we cannot easily determined it to be.
12535 An example of the sort of thing we care about (at this point; this routine
12536 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12537 fold cases do now) is discovering that
12539 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12541 is a multiple of
12543 SAVE_EXPR (J * 8)
12545 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12547 This code also handles discovering that
12549 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12551 is a multiple of 8 so we don't have to worry about dealing with a
12552 possible remainder.
12554 Note that we *look* inside a SAVE_EXPR only to determine how it was
12555 calculated; it is not safe for fold to do much of anything else with the
12556 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12557 at run time. For example, the latter example above *cannot* be implemented
12558 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12559 evaluation time of the original SAVE_EXPR is not necessarily the same at
12560 the time the new expression is evaluated. The only optimization of this
12561 sort that would be valid is changing
12563 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12565 divided by 8 to
12567 SAVE_EXPR (I) * SAVE_EXPR (J)
12569 (where the same SAVE_EXPR (J) is used in the original and the
12570 transformed version). */
12573 multiple_of_p (tree type, const_tree top, const_tree bottom)
12575 gimple *stmt;
12576 tree t1, op1, op2;
12578 if (operand_equal_p (top, bottom, 0))
12579 return 1;
12581 if (TREE_CODE (type) != INTEGER_TYPE)
12582 return 0;
12584 switch (TREE_CODE (top))
12586 case BIT_AND_EXPR:
12587 /* Bitwise and provides a power of two multiple. If the mask is
12588 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12589 if (!integer_pow2p (bottom))
12590 return 0;
12591 /* FALLTHRU */
12593 case MULT_EXPR:
12594 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12595 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12597 case MINUS_EXPR:
12598 /* It is impossible to prove if op0 - op1 is multiple of bottom
12599 precisely, so be conservative here checking if both op0 and op1
12600 are multiple of bottom. Note we check the second operand first
12601 since it's usually simpler. */
12602 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12603 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12605 case PLUS_EXPR:
12606 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12607 as op0 - 3 if the expression has unsigned type. For example,
12608 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12609 op1 = TREE_OPERAND (top, 1);
12610 if (TYPE_UNSIGNED (type)
12611 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12612 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12613 return (multiple_of_p (type, op1, bottom)
12614 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12616 case LSHIFT_EXPR:
12617 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12619 op1 = TREE_OPERAND (top, 1);
12620 /* const_binop may not detect overflow correctly,
12621 so check for it explicitly here. */
12622 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12623 && 0 != (t1 = fold_convert (type,
12624 const_binop (LSHIFT_EXPR,
12625 size_one_node,
12626 op1)))
12627 && !TREE_OVERFLOW (t1))
12628 return multiple_of_p (type, t1, bottom);
12630 return 0;
12632 case NOP_EXPR:
12633 /* Can't handle conversions from non-integral or wider integral type. */
12634 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12635 || (TYPE_PRECISION (type)
12636 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12637 return 0;
12639 /* fall through */
12641 case SAVE_EXPR:
12642 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12644 case COND_EXPR:
12645 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12646 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12648 case INTEGER_CST:
12649 if (TREE_CODE (bottom) != INTEGER_CST
12650 || integer_zerop (bottom)
12651 || (TYPE_UNSIGNED (type)
12652 && (tree_int_cst_sgn (top) < 0
12653 || tree_int_cst_sgn (bottom) < 0)))
12654 return 0;
12655 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12656 SIGNED);
12658 case SSA_NAME:
12659 if (TREE_CODE (bottom) == INTEGER_CST
12660 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12661 && gimple_code (stmt) == GIMPLE_ASSIGN)
12663 enum tree_code code = gimple_assign_rhs_code (stmt);
12665 /* Check for special cases to see if top is defined as multiple
12666 of bottom:
12668 top = (X & ~(bottom - 1) ; bottom is power of 2
12672 Y = X % bottom
12673 top = X - Y. */
12674 if (code == BIT_AND_EXPR
12675 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12676 && TREE_CODE (op2) == INTEGER_CST
12677 && integer_pow2p (bottom)
12678 && wi::multiple_of_p (wi::to_widest (op2),
12679 wi::to_widest (bottom), UNSIGNED))
12680 return 1;
12682 op1 = gimple_assign_rhs1 (stmt);
12683 if (code == MINUS_EXPR
12684 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12685 && TREE_CODE (op2) == SSA_NAME
12686 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12687 && gimple_code (stmt) == GIMPLE_ASSIGN
12688 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12689 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12690 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12691 return 1;
12694 /* fall through */
12696 default:
12697 return 0;
12701 #define tree_expr_nonnegative_warnv_p(X, Y) \
12702 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12704 #define RECURSE(X) \
12705 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12707 /* Return true if CODE or TYPE is known to be non-negative. */
12709 static bool
12710 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12712 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12713 && truth_value_p (code))
12714 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12715 have a signed:1 type (where the value is -1 and 0). */
12716 return true;
12717 return false;
12720 /* Return true if (CODE OP0) is known to be non-negative. If the return
12721 value is based on the assumption that signed overflow is undefined,
12722 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12723 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12725 bool
12726 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12727 bool *strict_overflow_p, int depth)
12729 if (TYPE_UNSIGNED (type))
12730 return true;
12732 switch (code)
12734 case ABS_EXPR:
12735 /* We can't return 1 if flag_wrapv is set because
12736 ABS_EXPR<INT_MIN> = INT_MIN. */
12737 if (!ANY_INTEGRAL_TYPE_P (type))
12738 return true;
12739 if (TYPE_OVERFLOW_UNDEFINED (type))
12741 *strict_overflow_p = true;
12742 return true;
12744 break;
12746 case NON_LVALUE_EXPR:
12747 case FLOAT_EXPR:
12748 case FIX_TRUNC_EXPR:
12749 return RECURSE (op0);
12751 CASE_CONVERT:
12753 tree inner_type = TREE_TYPE (op0);
12754 tree outer_type = type;
12756 if (TREE_CODE (outer_type) == REAL_TYPE)
12758 if (TREE_CODE (inner_type) == REAL_TYPE)
12759 return RECURSE (op0);
12760 if (INTEGRAL_TYPE_P (inner_type))
12762 if (TYPE_UNSIGNED (inner_type))
12763 return true;
12764 return RECURSE (op0);
12767 else if (INTEGRAL_TYPE_P (outer_type))
12769 if (TREE_CODE (inner_type) == REAL_TYPE)
12770 return RECURSE (op0);
12771 if (INTEGRAL_TYPE_P (inner_type))
12772 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12773 && TYPE_UNSIGNED (inner_type);
12776 break;
12778 default:
12779 return tree_simple_nonnegative_warnv_p (code, type);
12782 /* We don't know sign of `t', so be conservative and return false. */
12783 return false;
12786 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12787 value is based on the assumption that signed overflow is undefined,
12788 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12789 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12791 bool
12792 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12793 tree op1, bool *strict_overflow_p,
12794 int depth)
12796 if (TYPE_UNSIGNED (type))
12797 return true;
12799 switch (code)
12801 case POINTER_PLUS_EXPR:
12802 case PLUS_EXPR:
12803 if (FLOAT_TYPE_P (type))
12804 return RECURSE (op0) && RECURSE (op1);
12806 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12807 both unsigned and at least 2 bits shorter than the result. */
12808 if (TREE_CODE (type) == INTEGER_TYPE
12809 && TREE_CODE (op0) == NOP_EXPR
12810 && TREE_CODE (op1) == NOP_EXPR)
12812 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12813 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12814 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12815 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12817 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12818 TYPE_PRECISION (inner2)) + 1;
12819 return prec < TYPE_PRECISION (type);
12822 break;
12824 case MULT_EXPR:
12825 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12827 /* x * x is always non-negative for floating point x
12828 or without overflow. */
12829 if (operand_equal_p (op0, op1, 0)
12830 || (RECURSE (op0) && RECURSE (op1)))
12832 if (ANY_INTEGRAL_TYPE_P (type)
12833 && TYPE_OVERFLOW_UNDEFINED (type))
12834 *strict_overflow_p = true;
12835 return true;
12839 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12840 both unsigned and their total bits is shorter than the result. */
12841 if (TREE_CODE (type) == INTEGER_TYPE
12842 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12843 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12845 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12846 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12847 : TREE_TYPE (op0);
12848 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12849 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12850 : TREE_TYPE (op1);
12852 bool unsigned0 = TYPE_UNSIGNED (inner0);
12853 bool unsigned1 = TYPE_UNSIGNED (inner1);
12855 if (TREE_CODE (op0) == INTEGER_CST)
12856 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12858 if (TREE_CODE (op1) == INTEGER_CST)
12859 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12861 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12862 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12864 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12865 ? tree_int_cst_min_precision (op0, UNSIGNED)
12866 : TYPE_PRECISION (inner0);
12868 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12869 ? tree_int_cst_min_precision (op1, UNSIGNED)
12870 : TYPE_PRECISION (inner1);
12872 return precision0 + precision1 < TYPE_PRECISION (type);
12875 return false;
12877 case BIT_AND_EXPR:
12878 case MAX_EXPR:
12879 return RECURSE (op0) || RECURSE (op1);
12881 case BIT_IOR_EXPR:
12882 case BIT_XOR_EXPR:
12883 case MIN_EXPR:
12884 case RDIV_EXPR:
12885 case TRUNC_DIV_EXPR:
12886 case CEIL_DIV_EXPR:
12887 case FLOOR_DIV_EXPR:
12888 case ROUND_DIV_EXPR:
12889 return RECURSE (op0) && RECURSE (op1);
12891 case TRUNC_MOD_EXPR:
12892 return RECURSE (op0);
12894 case FLOOR_MOD_EXPR:
12895 return RECURSE (op1);
12897 case CEIL_MOD_EXPR:
12898 case ROUND_MOD_EXPR:
12899 default:
12900 return tree_simple_nonnegative_warnv_p (code, type);
12903 /* We don't know sign of `t', so be conservative and return false. */
12904 return false;
12907 /* Return true if T is known to be non-negative. If the return
12908 value is based on the assumption that signed overflow is undefined,
12909 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12910 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12912 bool
12913 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12915 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12916 return true;
12918 switch (TREE_CODE (t))
12920 case INTEGER_CST:
12921 return tree_int_cst_sgn (t) >= 0;
12923 case REAL_CST:
12924 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12926 case FIXED_CST:
12927 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12929 case COND_EXPR:
12930 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12932 case SSA_NAME:
12933 /* Limit the depth of recursion to avoid quadratic behavior.
12934 This is expected to catch almost all occurrences in practice.
12935 If this code misses important cases that unbounded recursion
12936 would not, passes that need this information could be revised
12937 to provide it through dataflow propagation. */
12938 return (!name_registered_for_update_p (t)
12939 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12940 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12941 strict_overflow_p, depth));
12943 default:
12944 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12948 /* Return true if T is known to be non-negative. If the return
12949 value is based on the assumption that signed overflow is undefined,
12950 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12951 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12953 bool
12954 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12955 bool *strict_overflow_p, int depth)
12957 switch (fn)
12959 CASE_CFN_ACOS:
12960 CASE_CFN_ACOSH:
12961 CASE_CFN_CABS:
12962 CASE_CFN_COSH:
12963 CASE_CFN_ERFC:
12964 CASE_CFN_EXP:
12965 CASE_CFN_EXP10:
12966 CASE_CFN_EXP2:
12967 CASE_CFN_FABS:
12968 CASE_CFN_FDIM:
12969 CASE_CFN_HYPOT:
12970 CASE_CFN_POW10:
12971 CASE_CFN_FFS:
12972 CASE_CFN_PARITY:
12973 CASE_CFN_POPCOUNT:
12974 CASE_CFN_CLZ:
12975 CASE_CFN_CLRSB:
12976 case CFN_BUILT_IN_BSWAP32:
12977 case CFN_BUILT_IN_BSWAP64:
12978 /* Always true. */
12979 return true;
12981 CASE_CFN_SQRT:
12982 /* sqrt(-0.0) is -0.0. */
12983 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12984 return true;
12985 return RECURSE (arg0);
12987 CASE_CFN_ASINH:
12988 CASE_CFN_ATAN:
12989 CASE_CFN_ATANH:
12990 CASE_CFN_CBRT:
12991 CASE_CFN_CEIL:
12992 CASE_CFN_ERF:
12993 CASE_CFN_EXPM1:
12994 CASE_CFN_FLOOR:
12995 CASE_CFN_FMOD:
12996 CASE_CFN_FREXP:
12997 CASE_CFN_ICEIL:
12998 CASE_CFN_IFLOOR:
12999 CASE_CFN_IRINT:
13000 CASE_CFN_IROUND:
13001 CASE_CFN_LCEIL:
13002 CASE_CFN_LDEXP:
13003 CASE_CFN_LFLOOR:
13004 CASE_CFN_LLCEIL:
13005 CASE_CFN_LLFLOOR:
13006 CASE_CFN_LLRINT:
13007 CASE_CFN_LLROUND:
13008 CASE_CFN_LRINT:
13009 CASE_CFN_LROUND:
13010 CASE_CFN_MODF:
13011 CASE_CFN_NEARBYINT:
13012 CASE_CFN_RINT:
13013 CASE_CFN_ROUND:
13014 CASE_CFN_SCALB:
13015 CASE_CFN_SCALBLN:
13016 CASE_CFN_SCALBN:
13017 CASE_CFN_SIGNBIT:
13018 CASE_CFN_SIGNIFICAND:
13019 CASE_CFN_SINH:
13020 CASE_CFN_TANH:
13021 CASE_CFN_TRUNC:
13022 /* True if the 1st argument is nonnegative. */
13023 return RECURSE (arg0);
13025 CASE_CFN_FMAX:
13026 /* True if the 1st OR 2nd arguments are nonnegative. */
13027 return RECURSE (arg0) || RECURSE (arg1);
13029 CASE_CFN_FMIN:
13030 /* True if the 1st AND 2nd arguments are nonnegative. */
13031 return RECURSE (arg0) && RECURSE (arg1);
13033 CASE_CFN_COPYSIGN:
13034 /* True if the 2nd argument is nonnegative. */
13035 return RECURSE (arg1);
13037 CASE_CFN_POWI:
13038 /* True if the 1st argument is nonnegative or the second
13039 argument is an even integer. */
13040 if (TREE_CODE (arg1) == INTEGER_CST
13041 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13042 return true;
13043 return RECURSE (arg0);
13045 CASE_CFN_POW:
13046 /* True if the 1st argument is nonnegative or the second
13047 argument is an even integer valued real. */
13048 if (TREE_CODE (arg1) == REAL_CST)
13050 REAL_VALUE_TYPE c;
13051 HOST_WIDE_INT n;
13053 c = TREE_REAL_CST (arg1);
13054 n = real_to_integer (&c);
13055 if ((n & 1) == 0)
13057 REAL_VALUE_TYPE cint;
13058 real_from_integer (&cint, VOIDmode, n, SIGNED);
13059 if (real_identical (&c, &cint))
13060 return true;
13063 return RECURSE (arg0);
13065 default:
13066 break;
13068 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13071 /* Return true if T is known to be non-negative. If the return
13072 value is based on the assumption that signed overflow is undefined,
13073 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13074 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13076 static bool
13077 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13079 enum tree_code code = TREE_CODE (t);
13080 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13081 return true;
13083 switch (code)
13085 case TARGET_EXPR:
13087 tree temp = TARGET_EXPR_SLOT (t);
13088 t = TARGET_EXPR_INITIAL (t);
13090 /* If the initializer is non-void, then it's a normal expression
13091 that will be assigned to the slot. */
13092 if (!VOID_TYPE_P (t))
13093 return RECURSE (t);
13095 /* Otherwise, the initializer sets the slot in some way. One common
13096 way is an assignment statement at the end of the initializer. */
13097 while (1)
13099 if (TREE_CODE (t) == BIND_EXPR)
13100 t = expr_last (BIND_EXPR_BODY (t));
13101 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13102 || TREE_CODE (t) == TRY_CATCH_EXPR)
13103 t = expr_last (TREE_OPERAND (t, 0));
13104 else if (TREE_CODE (t) == STATEMENT_LIST)
13105 t = expr_last (t);
13106 else
13107 break;
13109 if (TREE_CODE (t) == MODIFY_EXPR
13110 && TREE_OPERAND (t, 0) == temp)
13111 return RECURSE (TREE_OPERAND (t, 1));
13113 return false;
13116 case CALL_EXPR:
13118 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13119 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13121 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13122 get_call_combined_fn (t),
13123 arg0,
13124 arg1,
13125 strict_overflow_p, depth);
13127 case COMPOUND_EXPR:
13128 case MODIFY_EXPR:
13129 return RECURSE (TREE_OPERAND (t, 1));
13131 case BIND_EXPR:
13132 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13134 case SAVE_EXPR:
13135 return RECURSE (TREE_OPERAND (t, 0));
13137 default:
13138 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13142 #undef RECURSE
13143 #undef tree_expr_nonnegative_warnv_p
13145 /* Return true if T is known to be non-negative. If the return
13146 value is based on the assumption that signed overflow is undefined,
13147 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13148 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13150 bool
13151 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13153 enum tree_code code;
13154 if (t == error_mark_node)
13155 return false;
13157 code = TREE_CODE (t);
13158 switch (TREE_CODE_CLASS (code))
13160 case tcc_binary:
13161 case tcc_comparison:
13162 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13163 TREE_TYPE (t),
13164 TREE_OPERAND (t, 0),
13165 TREE_OPERAND (t, 1),
13166 strict_overflow_p, depth);
13168 case tcc_unary:
13169 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13170 TREE_TYPE (t),
13171 TREE_OPERAND (t, 0),
13172 strict_overflow_p, depth);
13174 case tcc_constant:
13175 case tcc_declaration:
13176 case tcc_reference:
13177 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13179 default:
13180 break;
13183 switch (code)
13185 case TRUTH_AND_EXPR:
13186 case TRUTH_OR_EXPR:
13187 case TRUTH_XOR_EXPR:
13188 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13189 TREE_TYPE (t),
13190 TREE_OPERAND (t, 0),
13191 TREE_OPERAND (t, 1),
13192 strict_overflow_p, depth);
13193 case TRUTH_NOT_EXPR:
13194 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13195 TREE_TYPE (t),
13196 TREE_OPERAND (t, 0),
13197 strict_overflow_p, depth);
13199 case COND_EXPR:
13200 case CONSTRUCTOR:
13201 case OBJ_TYPE_REF:
13202 case ASSERT_EXPR:
13203 case ADDR_EXPR:
13204 case WITH_SIZE_EXPR:
13205 case SSA_NAME:
13206 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13208 default:
13209 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13213 /* Return true if `t' is known to be non-negative. Handle warnings
13214 about undefined signed overflow. */
13216 bool
13217 tree_expr_nonnegative_p (tree t)
13219 bool ret, strict_overflow_p;
13221 strict_overflow_p = false;
13222 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13223 if (strict_overflow_p)
13224 fold_overflow_warning (("assuming signed overflow does not occur when "
13225 "determining that expression is always "
13226 "non-negative"),
13227 WARN_STRICT_OVERFLOW_MISC);
13228 return ret;
13232 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13233 For floating point we further ensure that T is not denormal.
13234 Similar logic is present in nonzero_address in rtlanal.h.
13236 If the return value is based on the assumption that signed overflow
13237 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13238 change *STRICT_OVERFLOW_P. */
13240 bool
13241 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13242 bool *strict_overflow_p)
13244 switch (code)
13246 case ABS_EXPR:
13247 return tree_expr_nonzero_warnv_p (op0,
13248 strict_overflow_p);
13250 case NOP_EXPR:
13252 tree inner_type = TREE_TYPE (op0);
13253 tree outer_type = type;
13255 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13256 && tree_expr_nonzero_warnv_p (op0,
13257 strict_overflow_p));
13259 break;
13261 case NON_LVALUE_EXPR:
13262 return tree_expr_nonzero_warnv_p (op0,
13263 strict_overflow_p);
13265 default:
13266 break;
13269 return false;
13272 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13273 For floating point we further ensure that T is not denormal.
13274 Similar logic is present in nonzero_address in rtlanal.h.
13276 If the return value is based on the assumption that signed overflow
13277 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13278 change *STRICT_OVERFLOW_P. */
13280 bool
13281 tree_binary_nonzero_warnv_p (enum tree_code code,
13282 tree type,
13283 tree op0,
13284 tree op1, bool *strict_overflow_p)
13286 bool sub_strict_overflow_p;
13287 switch (code)
13289 case POINTER_PLUS_EXPR:
13290 case PLUS_EXPR:
13291 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13293 /* With the presence of negative values it is hard
13294 to say something. */
13295 sub_strict_overflow_p = false;
13296 if (!tree_expr_nonnegative_warnv_p (op0,
13297 &sub_strict_overflow_p)
13298 || !tree_expr_nonnegative_warnv_p (op1,
13299 &sub_strict_overflow_p))
13300 return false;
13301 /* One of operands must be positive and the other non-negative. */
13302 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13303 overflows, on a twos-complement machine the sum of two
13304 nonnegative numbers can never be zero. */
13305 return (tree_expr_nonzero_warnv_p (op0,
13306 strict_overflow_p)
13307 || tree_expr_nonzero_warnv_p (op1,
13308 strict_overflow_p));
13310 break;
13312 case MULT_EXPR:
13313 if (TYPE_OVERFLOW_UNDEFINED (type))
13315 if (tree_expr_nonzero_warnv_p (op0,
13316 strict_overflow_p)
13317 && tree_expr_nonzero_warnv_p (op1,
13318 strict_overflow_p))
13320 *strict_overflow_p = true;
13321 return true;
13324 break;
13326 case MIN_EXPR:
13327 sub_strict_overflow_p = false;
13328 if (tree_expr_nonzero_warnv_p (op0,
13329 &sub_strict_overflow_p)
13330 && tree_expr_nonzero_warnv_p (op1,
13331 &sub_strict_overflow_p))
13333 if (sub_strict_overflow_p)
13334 *strict_overflow_p = true;
13336 break;
13338 case MAX_EXPR:
13339 sub_strict_overflow_p = false;
13340 if (tree_expr_nonzero_warnv_p (op0,
13341 &sub_strict_overflow_p))
13343 if (sub_strict_overflow_p)
13344 *strict_overflow_p = true;
13346 /* When both operands are nonzero, then MAX must be too. */
13347 if (tree_expr_nonzero_warnv_p (op1,
13348 strict_overflow_p))
13349 return true;
13351 /* MAX where operand 0 is positive is positive. */
13352 return tree_expr_nonnegative_warnv_p (op0,
13353 strict_overflow_p);
13355 /* MAX where operand 1 is positive is positive. */
13356 else if (tree_expr_nonzero_warnv_p (op1,
13357 &sub_strict_overflow_p)
13358 && tree_expr_nonnegative_warnv_p (op1,
13359 &sub_strict_overflow_p))
13361 if (sub_strict_overflow_p)
13362 *strict_overflow_p = true;
13363 return true;
13365 break;
13367 case BIT_IOR_EXPR:
13368 return (tree_expr_nonzero_warnv_p (op1,
13369 strict_overflow_p)
13370 || tree_expr_nonzero_warnv_p (op0,
13371 strict_overflow_p));
13373 default:
13374 break;
13377 return false;
13380 /* Return true when T is an address and is known to be nonzero.
13381 For floating point we further ensure that T is not denormal.
13382 Similar logic is present in nonzero_address in rtlanal.h.
13384 If the return value is based on the assumption that signed overflow
13385 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13386 change *STRICT_OVERFLOW_P. */
13388 bool
13389 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13391 bool sub_strict_overflow_p;
13392 switch (TREE_CODE (t))
13394 case INTEGER_CST:
13395 return !integer_zerop (t);
13397 case ADDR_EXPR:
13399 tree base = TREE_OPERAND (t, 0);
13401 if (!DECL_P (base))
13402 base = get_base_address (base);
13404 if (base && TREE_CODE (base) == TARGET_EXPR)
13405 base = TARGET_EXPR_SLOT (base);
13407 if (!base)
13408 return false;
13410 /* For objects in symbol table check if we know they are non-zero.
13411 Don't do anything for variables and functions before symtab is built;
13412 it is quite possible that they will be declared weak later. */
13413 int nonzero_addr = maybe_nonzero_address (base);
13414 if (nonzero_addr >= 0)
13415 return nonzero_addr;
13417 /* Function local objects are never NULL. */
13418 if (DECL_P (base)
13419 && (DECL_CONTEXT (base)
13420 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13421 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13422 return true;
13424 /* Constants are never weak. */
13425 if (CONSTANT_CLASS_P (base))
13426 return true;
13428 return false;
13431 case COND_EXPR:
13432 sub_strict_overflow_p = false;
13433 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13434 &sub_strict_overflow_p)
13435 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13436 &sub_strict_overflow_p))
13438 if (sub_strict_overflow_p)
13439 *strict_overflow_p = true;
13440 return true;
13442 break;
13444 default:
13445 break;
13447 return false;
13450 #define integer_valued_real_p(X) \
13451 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13453 #define RECURSE(X) \
13454 ((integer_valued_real_p) (X, depth + 1))
13456 /* Return true if the floating point result of (CODE OP0) has an
13457 integer value. We also allow +Inf, -Inf and NaN to be considered
13458 integer values. Return false for signaling NaN.
13460 DEPTH is the current nesting depth of the query. */
13462 bool
13463 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13465 switch (code)
13467 case FLOAT_EXPR:
13468 return true;
13470 case ABS_EXPR:
13471 return RECURSE (op0);
13473 CASE_CONVERT:
13475 tree type = TREE_TYPE (op0);
13476 if (TREE_CODE (type) == INTEGER_TYPE)
13477 return true;
13478 if (TREE_CODE (type) == REAL_TYPE)
13479 return RECURSE (op0);
13480 break;
13483 default:
13484 break;
13486 return false;
13489 /* Return true if the floating point result of (CODE OP0 OP1) has an
13490 integer value. We also allow +Inf, -Inf and NaN to be considered
13491 integer values. Return false for signaling NaN.
13493 DEPTH is the current nesting depth of the query. */
13495 bool
13496 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13498 switch (code)
13500 case PLUS_EXPR:
13501 case MINUS_EXPR:
13502 case MULT_EXPR:
13503 case MIN_EXPR:
13504 case MAX_EXPR:
13505 return RECURSE (op0) && RECURSE (op1);
13507 default:
13508 break;
13510 return false;
13513 /* Return true if the floating point result of calling FNDECL with arguments
13514 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13515 considered integer values. Return false for signaling NaN. If FNDECL
13516 takes fewer than 2 arguments, the remaining ARGn are null.
13518 DEPTH is the current nesting depth of the query. */
13520 bool
13521 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13523 switch (fn)
13525 CASE_CFN_CEIL:
13526 CASE_CFN_FLOOR:
13527 CASE_CFN_NEARBYINT:
13528 CASE_CFN_RINT:
13529 CASE_CFN_ROUND:
13530 CASE_CFN_TRUNC:
13531 return true;
13533 CASE_CFN_FMIN:
13534 CASE_CFN_FMAX:
13535 return RECURSE (arg0) && RECURSE (arg1);
13537 default:
13538 break;
13540 return false;
13543 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13544 has an integer value. We also allow +Inf, -Inf and NaN to be
13545 considered integer values. Return false for signaling NaN.
13547 DEPTH is the current nesting depth of the query. */
13549 bool
13550 integer_valued_real_single_p (tree t, int depth)
13552 switch (TREE_CODE (t))
13554 case REAL_CST:
13555 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13557 case COND_EXPR:
13558 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13560 case SSA_NAME:
13561 /* Limit the depth of recursion to avoid quadratic behavior.
13562 This is expected to catch almost all occurrences in practice.
13563 If this code misses important cases that unbounded recursion
13564 would not, passes that need this information could be revised
13565 to provide it through dataflow propagation. */
13566 return (!name_registered_for_update_p (t)
13567 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13568 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13569 depth));
13571 default:
13572 break;
13574 return false;
13577 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13578 has an integer value. We also allow +Inf, -Inf and NaN to be
13579 considered integer values. Return false for signaling NaN.
13581 DEPTH is the current nesting depth of the query. */
13583 static bool
13584 integer_valued_real_invalid_p (tree t, int depth)
13586 switch (TREE_CODE (t))
13588 case COMPOUND_EXPR:
13589 case MODIFY_EXPR:
13590 case BIND_EXPR:
13591 return RECURSE (TREE_OPERAND (t, 1));
13593 case SAVE_EXPR:
13594 return RECURSE (TREE_OPERAND (t, 0));
13596 default:
13597 break;
13599 return false;
13602 #undef RECURSE
13603 #undef integer_valued_real_p
13605 /* Return true if the floating point expression T has an integer value.
13606 We also allow +Inf, -Inf and NaN to be considered integer values.
13607 Return false for signaling NaN.
13609 DEPTH is the current nesting depth of the query. */
13611 bool
13612 integer_valued_real_p (tree t, int depth)
13614 if (t == error_mark_node)
13615 return false;
13617 tree_code code = TREE_CODE (t);
13618 switch (TREE_CODE_CLASS (code))
13620 case tcc_binary:
13621 case tcc_comparison:
13622 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13623 TREE_OPERAND (t, 1), depth);
13625 case tcc_unary:
13626 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13628 case tcc_constant:
13629 case tcc_declaration:
13630 case tcc_reference:
13631 return integer_valued_real_single_p (t, depth);
13633 default:
13634 break;
13637 switch (code)
13639 case COND_EXPR:
13640 case SSA_NAME:
13641 return integer_valued_real_single_p (t, depth);
13643 case CALL_EXPR:
13645 tree arg0 = (call_expr_nargs (t) > 0
13646 ? CALL_EXPR_ARG (t, 0)
13647 : NULL_TREE);
13648 tree arg1 = (call_expr_nargs (t) > 1
13649 ? CALL_EXPR_ARG (t, 1)
13650 : NULL_TREE);
13651 return integer_valued_real_call_p (get_call_combined_fn (t),
13652 arg0, arg1, depth);
13655 default:
13656 return integer_valued_real_invalid_p (t, depth);
13660 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13661 attempt to fold the expression to a constant without modifying TYPE,
13662 OP0 or OP1.
13664 If the expression could be simplified to a constant, then return
13665 the constant. If the expression would not be simplified to a
13666 constant, then return NULL_TREE. */
13668 tree
13669 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13671 tree tem = fold_binary (code, type, op0, op1);
13672 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13675 /* Given the components of a unary expression CODE, TYPE and OP0,
13676 attempt to fold the expression to a constant without modifying
13677 TYPE or OP0.
13679 If the expression could be simplified to a constant, then return
13680 the constant. If the expression would not be simplified to a
13681 constant, then return NULL_TREE. */
13683 tree
13684 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13686 tree tem = fold_unary (code, type, op0);
13687 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13690 /* If EXP represents referencing an element in a constant string
13691 (either via pointer arithmetic or array indexing), return the
13692 tree representing the value accessed, otherwise return NULL. */
13694 tree
13695 fold_read_from_constant_string (tree exp)
13697 if ((TREE_CODE (exp) == INDIRECT_REF
13698 || TREE_CODE (exp) == ARRAY_REF)
13699 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13701 tree exp1 = TREE_OPERAND (exp, 0);
13702 tree index;
13703 tree string;
13704 location_t loc = EXPR_LOCATION (exp);
13706 if (TREE_CODE (exp) == INDIRECT_REF)
13707 string = string_constant (exp1, &index);
13708 else
13710 tree low_bound = array_ref_low_bound (exp);
13711 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13713 /* Optimize the special-case of a zero lower bound.
13715 We convert the low_bound to sizetype to avoid some problems
13716 with constant folding. (E.g. suppose the lower bound is 1,
13717 and its mode is QI. Without the conversion,l (ARRAY
13718 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13719 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13720 if (! integer_zerop (low_bound))
13721 index = size_diffop_loc (loc, index,
13722 fold_convert_loc (loc, sizetype, low_bound));
13724 string = exp1;
13727 if (string
13728 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13729 && TREE_CODE (string) == STRING_CST
13730 && TREE_CODE (index) == INTEGER_CST
13731 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13732 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13733 == MODE_INT)
13734 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13735 return build_int_cst_type (TREE_TYPE (exp),
13736 (TREE_STRING_POINTER (string)
13737 [TREE_INT_CST_LOW (index)]));
13739 return NULL;
13742 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13743 an integer constant, real, or fixed-point constant.
13745 TYPE is the type of the result. */
13747 static tree
13748 fold_negate_const (tree arg0, tree type)
13750 tree t = NULL_TREE;
13752 switch (TREE_CODE (arg0))
13754 case INTEGER_CST:
13756 bool overflow;
13757 wide_int val = wi::neg (arg0, &overflow);
13758 t = force_fit_type (type, val, 1,
13759 (overflow | TREE_OVERFLOW (arg0))
13760 && !TYPE_UNSIGNED (type));
13761 break;
13764 case REAL_CST:
13765 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13766 break;
13768 case FIXED_CST:
13770 FIXED_VALUE_TYPE f;
13771 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13772 &(TREE_FIXED_CST (arg0)), NULL,
13773 TYPE_SATURATING (type));
13774 t = build_fixed (type, f);
13775 /* Propagate overflow flags. */
13776 if (overflow_p | TREE_OVERFLOW (arg0))
13777 TREE_OVERFLOW (t) = 1;
13778 break;
13781 default:
13782 gcc_unreachable ();
13785 return t;
13788 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13789 an integer constant or real constant.
13791 TYPE is the type of the result. */
13793 tree
13794 fold_abs_const (tree arg0, tree type)
13796 tree t = NULL_TREE;
13798 switch (TREE_CODE (arg0))
13800 case INTEGER_CST:
13802 /* If the value is unsigned or non-negative, then the absolute value
13803 is the same as the ordinary value. */
13804 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13805 t = arg0;
13807 /* If the value is negative, then the absolute value is
13808 its negation. */
13809 else
13811 bool overflow;
13812 wide_int val = wi::neg (arg0, &overflow);
13813 t = force_fit_type (type, val, -1,
13814 overflow | TREE_OVERFLOW (arg0));
13817 break;
13819 case REAL_CST:
13820 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13821 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13822 else
13823 t = arg0;
13824 break;
13826 default:
13827 gcc_unreachable ();
13830 return t;
13833 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13834 constant. TYPE is the type of the result. */
13836 static tree
13837 fold_not_const (const_tree arg0, tree type)
13839 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13841 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13844 /* Given CODE, a relational operator, the target type, TYPE and two
13845 constant operands OP0 and OP1, return the result of the
13846 relational operation. If the result is not a compile time
13847 constant, then return NULL_TREE. */
13849 static tree
13850 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13852 int result, invert;
13854 /* From here on, the only cases we handle are when the result is
13855 known to be a constant. */
13857 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13859 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13860 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13862 /* Handle the cases where either operand is a NaN. */
13863 if (real_isnan (c0) || real_isnan (c1))
13865 switch (code)
13867 case EQ_EXPR:
13868 case ORDERED_EXPR:
13869 result = 0;
13870 break;
13872 case NE_EXPR:
13873 case UNORDERED_EXPR:
13874 case UNLT_EXPR:
13875 case UNLE_EXPR:
13876 case UNGT_EXPR:
13877 case UNGE_EXPR:
13878 case UNEQ_EXPR:
13879 result = 1;
13880 break;
13882 case LT_EXPR:
13883 case LE_EXPR:
13884 case GT_EXPR:
13885 case GE_EXPR:
13886 case LTGT_EXPR:
13887 if (flag_trapping_math)
13888 return NULL_TREE;
13889 result = 0;
13890 break;
13892 default:
13893 gcc_unreachable ();
13896 return constant_boolean_node (result, type);
13899 return constant_boolean_node (real_compare (code, c0, c1), type);
13902 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13904 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13905 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13906 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13909 /* Handle equality/inequality of complex constants. */
13910 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13912 tree rcond = fold_relational_const (code, type,
13913 TREE_REALPART (op0),
13914 TREE_REALPART (op1));
13915 tree icond = fold_relational_const (code, type,
13916 TREE_IMAGPART (op0),
13917 TREE_IMAGPART (op1));
13918 if (code == EQ_EXPR)
13919 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13920 else if (code == NE_EXPR)
13921 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13922 else
13923 return NULL_TREE;
13926 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13928 if (!VECTOR_TYPE_P (type))
13930 /* Have vector comparison with scalar boolean result. */
13931 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13932 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13933 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13935 tree elem0 = VECTOR_CST_ELT (op0, i);
13936 tree elem1 = VECTOR_CST_ELT (op1, i);
13937 tree tmp = fold_relational_const (code, type, elem0, elem1);
13938 if (tmp == NULL_TREE)
13939 return NULL_TREE;
13940 if (integer_zerop (tmp))
13941 return constant_boolean_node (false, type);
13943 return constant_boolean_node (true, type);
13945 unsigned count = VECTOR_CST_NELTS (op0);
13946 tree *elts = XALLOCAVEC (tree, count);
13947 gcc_assert (VECTOR_CST_NELTS (op1) == count
13948 && TYPE_VECTOR_SUBPARTS (type) == count);
13950 for (unsigned i = 0; i < count; i++)
13952 tree elem_type = TREE_TYPE (type);
13953 tree elem0 = VECTOR_CST_ELT (op0, i);
13954 tree elem1 = VECTOR_CST_ELT (op1, i);
13956 tree tem = fold_relational_const (code, elem_type,
13957 elem0, elem1);
13959 if (tem == NULL_TREE)
13960 return NULL_TREE;
13962 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13965 return build_vector (type, elts);
13968 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13970 To compute GT, swap the arguments and do LT.
13971 To compute GE, do LT and invert the result.
13972 To compute LE, swap the arguments, do LT and invert the result.
13973 To compute NE, do EQ and invert the result.
13975 Therefore, the code below must handle only EQ and LT. */
13977 if (code == LE_EXPR || code == GT_EXPR)
13979 std::swap (op0, op1);
13980 code = swap_tree_comparison (code);
13983 /* Note that it is safe to invert for real values here because we
13984 have already handled the one case that it matters. */
13986 invert = 0;
13987 if (code == NE_EXPR || code == GE_EXPR)
13989 invert = 1;
13990 code = invert_tree_comparison (code, false);
13993 /* Compute a result for LT or EQ if args permit;
13994 Otherwise return T. */
13995 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13997 if (code == EQ_EXPR)
13998 result = tree_int_cst_equal (op0, op1);
13999 else
14000 result = tree_int_cst_lt (op0, op1);
14002 else
14003 return NULL_TREE;
14005 if (invert)
14006 result ^= 1;
14007 return constant_boolean_node (result, type);
14010 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14011 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14012 itself. */
14014 tree
14015 fold_build_cleanup_point_expr (tree type, tree expr)
14017 /* If the expression does not have side effects then we don't have to wrap
14018 it with a cleanup point expression. */
14019 if (!TREE_SIDE_EFFECTS (expr))
14020 return expr;
14022 /* If the expression is a return, check to see if the expression inside the
14023 return has no side effects or the right hand side of the modify expression
14024 inside the return. If either don't have side effects set we don't need to
14025 wrap the expression in a cleanup point expression. Note we don't check the
14026 left hand side of the modify because it should always be a return decl. */
14027 if (TREE_CODE (expr) == RETURN_EXPR)
14029 tree op = TREE_OPERAND (expr, 0);
14030 if (!op || !TREE_SIDE_EFFECTS (op))
14031 return expr;
14032 op = TREE_OPERAND (op, 1);
14033 if (!TREE_SIDE_EFFECTS (op))
14034 return expr;
14037 return build1 (CLEANUP_POINT_EXPR, type, expr);
14040 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14041 of an indirection through OP0, or NULL_TREE if no simplification is
14042 possible. */
14044 tree
14045 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14047 tree sub = op0;
14048 tree subtype;
14050 STRIP_NOPS (sub);
14051 subtype = TREE_TYPE (sub);
14052 if (!POINTER_TYPE_P (subtype))
14053 return NULL_TREE;
14055 if (TREE_CODE (sub) == ADDR_EXPR)
14057 tree op = TREE_OPERAND (sub, 0);
14058 tree optype = TREE_TYPE (op);
14059 /* *&CONST_DECL -> to the value of the const decl. */
14060 if (TREE_CODE (op) == CONST_DECL)
14061 return DECL_INITIAL (op);
14062 /* *&p => p; make sure to handle *&"str"[cst] here. */
14063 if (type == optype)
14065 tree fop = fold_read_from_constant_string (op);
14066 if (fop)
14067 return fop;
14068 else
14069 return op;
14071 /* *(foo *)&fooarray => fooarray[0] */
14072 else if (TREE_CODE (optype) == ARRAY_TYPE
14073 && type == TREE_TYPE (optype)
14074 && (!in_gimple_form
14075 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14077 tree type_domain = TYPE_DOMAIN (optype);
14078 tree min_val = size_zero_node;
14079 if (type_domain && TYPE_MIN_VALUE (type_domain))
14080 min_val = TYPE_MIN_VALUE (type_domain);
14081 if (in_gimple_form
14082 && TREE_CODE (min_val) != INTEGER_CST)
14083 return NULL_TREE;
14084 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14085 NULL_TREE, NULL_TREE);
14087 /* *(foo *)&complexfoo => __real__ complexfoo */
14088 else if (TREE_CODE (optype) == COMPLEX_TYPE
14089 && type == TREE_TYPE (optype))
14090 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14091 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14092 else if (TREE_CODE (optype) == VECTOR_TYPE
14093 && type == TREE_TYPE (optype))
14095 tree part_width = TYPE_SIZE (type);
14096 tree index = bitsize_int (0);
14097 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14101 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14102 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14104 tree op00 = TREE_OPERAND (sub, 0);
14105 tree op01 = TREE_OPERAND (sub, 1);
14107 STRIP_NOPS (op00);
14108 if (TREE_CODE (op00) == ADDR_EXPR)
14110 tree op00type;
14111 op00 = TREE_OPERAND (op00, 0);
14112 op00type = TREE_TYPE (op00);
14114 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14115 if (TREE_CODE (op00type) == VECTOR_TYPE
14116 && type == TREE_TYPE (op00type))
14118 tree part_width = TYPE_SIZE (type);
14119 unsigned HOST_WIDE_INT max_offset
14120 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14121 * TYPE_VECTOR_SUBPARTS (op00type));
14122 if (tree_int_cst_sign_bit (op01) == 0
14123 && compare_tree_int (op01, max_offset) == -1)
14125 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14126 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14127 tree index = bitsize_int (indexi);
14128 return fold_build3_loc (loc,
14129 BIT_FIELD_REF, type, op00,
14130 part_width, index);
14133 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14134 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14135 && type == TREE_TYPE (op00type))
14137 tree size = TYPE_SIZE_UNIT (type);
14138 if (tree_int_cst_equal (size, op01))
14139 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14141 /* ((foo *)&fooarray)[1] => fooarray[1] */
14142 else if (TREE_CODE (op00type) == ARRAY_TYPE
14143 && type == TREE_TYPE (op00type))
14145 tree type_domain = TYPE_DOMAIN (op00type);
14146 tree min_val = size_zero_node;
14147 if (type_domain && TYPE_MIN_VALUE (type_domain))
14148 min_val = TYPE_MIN_VALUE (type_domain);
14149 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14150 TYPE_SIZE_UNIT (type));
14151 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14152 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14153 NULL_TREE, NULL_TREE);
14158 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14159 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14160 && type == TREE_TYPE (TREE_TYPE (subtype))
14161 && (!in_gimple_form
14162 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14164 tree type_domain;
14165 tree min_val = size_zero_node;
14166 sub = build_fold_indirect_ref_loc (loc, sub);
14167 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14168 if (type_domain && TYPE_MIN_VALUE (type_domain))
14169 min_val = TYPE_MIN_VALUE (type_domain);
14170 if (in_gimple_form
14171 && TREE_CODE (min_val) != INTEGER_CST)
14172 return NULL_TREE;
14173 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14174 NULL_TREE);
14177 return NULL_TREE;
14180 /* Builds an expression for an indirection through T, simplifying some
14181 cases. */
14183 tree
14184 build_fold_indirect_ref_loc (location_t loc, tree t)
14186 tree type = TREE_TYPE (TREE_TYPE (t));
14187 tree sub = fold_indirect_ref_1 (loc, type, t);
14189 if (sub)
14190 return sub;
14192 return build1_loc (loc, INDIRECT_REF, type, t);
14195 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14197 tree
14198 fold_indirect_ref_loc (location_t loc, tree t)
14200 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14202 if (sub)
14203 return sub;
14204 else
14205 return t;
14208 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14209 whose result is ignored. The type of the returned tree need not be
14210 the same as the original expression. */
14212 tree
14213 fold_ignored_result (tree t)
14215 if (!TREE_SIDE_EFFECTS (t))
14216 return integer_zero_node;
14218 for (;;)
14219 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14221 case tcc_unary:
14222 t = TREE_OPERAND (t, 0);
14223 break;
14225 case tcc_binary:
14226 case tcc_comparison:
14227 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14228 t = TREE_OPERAND (t, 0);
14229 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14230 t = TREE_OPERAND (t, 1);
14231 else
14232 return t;
14233 break;
14235 case tcc_expression:
14236 switch (TREE_CODE (t))
14238 case COMPOUND_EXPR:
14239 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14240 return t;
14241 t = TREE_OPERAND (t, 0);
14242 break;
14244 case COND_EXPR:
14245 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14246 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14247 return t;
14248 t = TREE_OPERAND (t, 0);
14249 break;
14251 default:
14252 return t;
14254 break;
14256 default:
14257 return t;
14261 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14263 tree
14264 round_up_loc (location_t loc, tree value, unsigned int divisor)
14266 tree div = NULL_TREE;
14268 if (divisor == 1)
14269 return value;
14271 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14272 have to do anything. Only do this when we are not given a const,
14273 because in that case, this check is more expensive than just
14274 doing it. */
14275 if (TREE_CODE (value) != INTEGER_CST)
14277 div = build_int_cst (TREE_TYPE (value), divisor);
14279 if (multiple_of_p (TREE_TYPE (value), value, div))
14280 return value;
14283 /* If divisor is a power of two, simplify this to bit manipulation. */
14284 if (pow2_or_zerop (divisor))
14286 if (TREE_CODE (value) == INTEGER_CST)
14288 wide_int val = value;
14289 bool overflow_p;
14291 if ((val & (divisor - 1)) == 0)
14292 return value;
14294 overflow_p = TREE_OVERFLOW (value);
14295 val += divisor - 1;
14296 val &= - (int) divisor;
14297 if (val == 0)
14298 overflow_p = true;
14300 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14302 else
14304 tree t;
14306 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14307 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14308 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14309 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14312 else
14314 if (!div)
14315 div = build_int_cst (TREE_TYPE (value), divisor);
14316 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14317 value = size_binop_loc (loc, MULT_EXPR, value, div);
14320 return value;
14323 /* Likewise, but round down. */
14325 tree
14326 round_down_loc (location_t loc, tree value, int divisor)
14328 tree div = NULL_TREE;
14330 gcc_assert (divisor > 0);
14331 if (divisor == 1)
14332 return value;
14334 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14335 have to do anything. Only do this when we are not given a const,
14336 because in that case, this check is more expensive than just
14337 doing it. */
14338 if (TREE_CODE (value) != INTEGER_CST)
14340 div = build_int_cst (TREE_TYPE (value), divisor);
14342 if (multiple_of_p (TREE_TYPE (value), value, div))
14343 return value;
14346 /* If divisor is a power of two, simplify this to bit manipulation. */
14347 if (pow2_or_zerop (divisor))
14349 tree t;
14351 t = build_int_cst (TREE_TYPE (value), -divisor);
14352 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14354 else
14356 if (!div)
14357 div = build_int_cst (TREE_TYPE (value), divisor);
14358 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14359 value = size_binop_loc (loc, MULT_EXPR, value, div);
14362 return value;
14365 /* Returns the pointer to the base of the object addressed by EXP and
14366 extracts the information about the offset of the access, storing it
14367 to PBITPOS and POFFSET. */
14369 static tree
14370 split_address_to_core_and_offset (tree exp,
14371 HOST_WIDE_INT *pbitpos, tree *poffset)
14373 tree core;
14374 machine_mode mode;
14375 int unsignedp, reversep, volatilep;
14376 HOST_WIDE_INT bitsize;
14377 location_t loc = EXPR_LOCATION (exp);
14379 if (TREE_CODE (exp) == ADDR_EXPR)
14381 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14382 poffset, &mode, &unsignedp, &reversep,
14383 &volatilep);
14384 core = build_fold_addr_expr_loc (loc, core);
14386 else
14388 core = exp;
14389 *pbitpos = 0;
14390 *poffset = NULL_TREE;
14393 return core;
14396 /* Returns true if addresses of E1 and E2 differ by a constant, false
14397 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14399 bool
14400 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14402 tree core1, core2;
14403 HOST_WIDE_INT bitpos1, bitpos2;
14404 tree toffset1, toffset2, tdiff, type;
14406 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14407 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14409 if (bitpos1 % BITS_PER_UNIT != 0
14410 || bitpos2 % BITS_PER_UNIT != 0
14411 || !operand_equal_p (core1, core2, 0))
14412 return false;
14414 if (toffset1 && toffset2)
14416 type = TREE_TYPE (toffset1);
14417 if (type != TREE_TYPE (toffset2))
14418 toffset2 = fold_convert (type, toffset2);
14420 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14421 if (!cst_and_fits_in_hwi (tdiff))
14422 return false;
14424 *diff = int_cst_value (tdiff);
14426 else if (toffset1 || toffset2)
14428 /* If only one of the offsets is non-constant, the difference cannot
14429 be a constant. */
14430 return false;
14432 else
14433 *diff = 0;
14435 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14436 return true;
14439 /* Return OFF converted to a pointer offset type suitable as offset for
14440 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14441 tree
14442 convert_to_ptrofftype_loc (location_t loc, tree off)
14444 return fold_convert_loc (loc, sizetype, off);
14447 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14448 tree
14449 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14451 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14452 ptr, convert_to_ptrofftype_loc (loc, off));
14455 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14456 tree
14457 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14459 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14460 ptr, size_int (off));
14463 /* Return a char pointer for a C string if it is a string constant
14464 or sum of string constant and integer constant. We only support
14465 string constants properly terminated with '\0' character.
14466 If STRLEN is a valid pointer, length (including terminating character)
14467 of returned string is stored to the argument. */
14469 const char *
14470 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14472 tree offset_node;
14474 if (strlen)
14475 *strlen = 0;
14477 src = string_constant (src, &offset_node);
14478 if (src == 0)
14479 return NULL;
14481 unsigned HOST_WIDE_INT offset = 0;
14482 if (offset_node != NULL_TREE)
14484 if (!tree_fits_uhwi_p (offset_node))
14485 return NULL;
14486 else
14487 offset = tree_to_uhwi (offset_node);
14490 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14491 const char *string = TREE_STRING_POINTER (src);
14493 /* Support only properly null-terminated strings. */
14494 if (string_length == 0
14495 || string[string_length - 1] != '\0'
14496 || offset >= string_length)
14497 return NULL;
14499 if (strlen)
14500 *strlen = string_length - offset;
14501 return string + offset;
14504 #if CHECKING_P
14506 namespace selftest {
14508 /* Helper functions for writing tests of folding trees. */
14510 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14512 static void
14513 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14514 tree constant)
14516 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14519 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14520 wrapping WRAPPED_EXPR. */
14522 static void
14523 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14524 tree wrapped_expr)
14526 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14527 ASSERT_NE (wrapped_expr, result);
14528 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14529 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14532 /* Verify that various arithmetic binary operations are folded
14533 correctly. */
14535 static void
14536 test_arithmetic_folding ()
14538 tree type = integer_type_node;
14539 tree x = create_tmp_var_raw (type, "x");
14540 tree zero = build_zero_cst (type);
14541 tree one = build_int_cst (type, 1);
14543 /* Addition. */
14544 /* 1 <-- (0 + 1) */
14545 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14546 one);
14547 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14548 one);
14550 /* (nonlvalue)x <-- (x + 0) */
14551 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14554 /* Subtraction. */
14555 /* 0 <-- (x - x) */
14556 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14557 zero);
14558 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14561 /* Multiplication. */
14562 /* 0 <-- (x * 0) */
14563 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14564 zero);
14566 /* (nonlvalue)x <-- (x * 1) */
14567 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14571 /* Verify that various binary operations on vectors are folded
14572 correctly. */
14574 static void
14575 test_vector_folding ()
14577 tree inner_type = integer_type_node;
14578 tree type = build_vector_type (inner_type, 4);
14579 tree zero = build_zero_cst (type);
14580 tree one = build_one_cst (type);
14582 /* Verify equality tests that return a scalar boolean result. */
14583 tree res_type = boolean_type_node;
14584 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14585 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14586 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14587 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14590 /* Run all of the selftests within this file. */
14592 void
14593 fold_const_c_tests ()
14595 test_arithmetic_folding ();
14596 test_vector_folding ();
14599 } // namespace selftest
14601 #endif /* CHECKING_P */