rs6000-c.c (altivec_resolve_overloaded_builtin): Reformat two multi-line strings.
[official-gcc.git] / gcc / fold-const.c
blobc5d9a79ed28c8d346645eb2f4ca5d482a7f7e1d7
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "tree-ssa-operands.h"
54 #include "optabs-query.h"
55 #include "cgraph.h"
56 #include "diagnostic-core.h"
57 #include "flags.h"
58 #include "alias.h"
59 #include "fold-const.h"
60 #include "fold-const-call.h"
61 #include "stor-layout.h"
62 #include "calls.h"
63 #include "tree-iterator.h"
64 #include "expr.h"
65 #include "intl.h"
66 #include "langhooks.h"
67 #include "tree-eh.h"
68 #include "gimplify.h"
69 #include "tree-dfa.h"
70 #include "builtins.h"
71 #include "generic-match.h"
72 #include "gimple-fold.h"
73 #include "params.h"
74 #include "tree-into-ssa.h"
75 #include "md5.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
78 #include "tree-ssanames.h"
79 #include "selftest.h"
81 #ifndef LOAD_EXTEND_OP
82 #define LOAD_EXTEND_OP(M) UNKNOWN
83 #endif
85 /* Nonzero if we are folding constants inside an initializer; zero
86 otherwise. */
87 int folding_initializer = 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code {
93 COMPCODE_FALSE = 0,
94 COMPCODE_LT = 1,
95 COMPCODE_EQ = 2,
96 COMPCODE_LE = 3,
97 COMPCODE_GT = 4,
98 COMPCODE_LTGT = 5,
99 COMPCODE_GE = 6,
100 COMPCODE_ORD = 7,
101 COMPCODE_UNORD = 8,
102 COMPCODE_UNLT = 9,
103 COMPCODE_UNEQ = 10,
104 COMPCODE_UNLE = 11,
105 COMPCODE_UNGT = 12,
106 COMPCODE_NE = 13,
107 COMPCODE_UNGE = 14,
108 COMPCODE_TRUE = 15
111 static bool negate_expr_p (tree);
112 static tree negate_expr (tree);
113 static tree split_tree (location_t, tree, tree, enum tree_code,
114 tree *, tree *, tree *, int);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int operand_equal_for_comparison_p (tree, tree, tree);
119 static int twoval_comparison_p (tree, tree *, tree *, int *);
120 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
121 static tree optimize_bit_field_compare (location_t, enum tree_code,
122 tree, tree, tree);
123 static int simple_operand_p (const_tree);
124 static bool simple_operand_p_2 (tree);
125 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
126 static tree range_predecessor (tree);
127 static tree range_successor (tree);
128 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
133 static tree fold_binary_op_with_conditional_arg (location_t,
134 enum tree_code, tree,
135 tree, tree,
136 tree, tree, int);
137 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
138 static bool reorder_operands_p (const_tree, const_tree);
139 static tree fold_negate_const (tree, tree);
140 static tree fold_not_const (const_tree, tree);
141 static tree fold_relational_const (enum tree_code, tree, tree, tree);
142 static tree fold_convert_const (enum tree_code, tree, tree);
143 static tree fold_view_convert_expr (tree, tree);
144 static bool vec_cst_ctor_to_array (tree, tree *);
147 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
148 Otherwise, return LOC. */
150 static location_t
151 expr_location_or (tree t, location_t loc)
153 location_t tloc = EXPR_LOCATION (t);
154 return tloc == UNKNOWN_LOCATION ? loc : tloc;
157 /* Similar to protected_set_expr_location, but never modify x in place,
158 if location can and needs to be set, unshare it. */
160 static inline tree
161 protected_set_expr_location_unshare (tree x, location_t loc)
163 if (CAN_HAVE_LOCATION_P (x)
164 && EXPR_LOCATION (x) != loc
165 && !(TREE_CODE (x) == SAVE_EXPR
166 || TREE_CODE (x) == TARGET_EXPR
167 || TREE_CODE (x) == BIND_EXPR))
169 x = copy_node (x);
170 SET_EXPR_LOCATION (x, loc);
172 return x;
175 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
176 division and returns the quotient. Otherwise returns
177 NULL_TREE. */
179 tree
180 div_if_zero_remainder (const_tree arg1, const_tree arg2)
182 widest_int quo;
184 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
185 SIGNED, &quo))
186 return wide_int_to_tree (TREE_TYPE (arg1), quo);
188 return NULL_TREE;
191 /* This is nonzero if we should defer warnings about undefined
192 overflow. This facility exists because these warnings are a
193 special case. The code to estimate loop iterations does not want
194 to issue any warnings, since it works with expressions which do not
195 occur in user code. Various bits of cleanup code call fold(), but
196 only use the result if it has certain characteristics (e.g., is a
197 constant); that code only wants to issue a warning if the result is
198 used. */
200 static int fold_deferring_overflow_warnings;
202 /* If a warning about undefined overflow is deferred, this is the
203 warning. Note that this may cause us to turn two warnings into
204 one, but that is fine since it is sufficient to only give one
205 warning per expression. */
207 static const char* fold_deferred_overflow_warning;
209 /* If a warning about undefined overflow is deferred, this is the
210 level at which the warning should be emitted. */
212 static enum warn_strict_overflow_code fold_deferred_overflow_code;
214 /* Start deferring overflow warnings. We could use a stack here to
215 permit nested calls, but at present it is not necessary. */
217 void
218 fold_defer_overflow_warnings (void)
220 ++fold_deferring_overflow_warnings;
223 /* Stop deferring overflow warnings. If there is a pending warning,
224 and ISSUE is true, then issue the warning if appropriate. STMT is
225 the statement with which the warning should be associated (used for
226 location information); STMT may be NULL. CODE is the level of the
227 warning--a warn_strict_overflow_code value. This function will use
228 the smaller of CODE and the deferred code when deciding whether to
229 issue the warning. CODE may be zero to mean to always use the
230 deferred code. */
232 void
233 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
235 const char *warnmsg;
236 location_t locus;
238 gcc_assert (fold_deferring_overflow_warnings > 0);
239 --fold_deferring_overflow_warnings;
240 if (fold_deferring_overflow_warnings > 0)
242 if (fold_deferred_overflow_warning != NULL
243 && code != 0
244 && code < (int) fold_deferred_overflow_code)
245 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
246 return;
249 warnmsg = fold_deferred_overflow_warning;
250 fold_deferred_overflow_warning = NULL;
252 if (!issue || warnmsg == NULL)
253 return;
255 if (gimple_no_warning_p (stmt))
256 return;
258 /* Use the smallest code level when deciding to issue the
259 warning. */
260 if (code == 0 || code > (int) fold_deferred_overflow_code)
261 code = fold_deferred_overflow_code;
263 if (!issue_strict_overflow_warning (code))
264 return;
266 if (stmt == NULL)
267 locus = input_location;
268 else
269 locus = gimple_location (stmt);
270 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
273 /* Stop deferring overflow warnings, ignoring any deferred
274 warnings. */
276 void
277 fold_undefer_and_ignore_overflow_warnings (void)
279 fold_undefer_overflow_warnings (false, NULL, 0);
282 /* Whether we are deferring overflow warnings. */
284 bool
285 fold_deferring_overflow_warnings_p (void)
287 return fold_deferring_overflow_warnings > 0;
290 /* This is called when we fold something based on the fact that signed
291 overflow is undefined. */
293 void
294 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
296 if (fold_deferring_overflow_warnings > 0)
298 if (fold_deferred_overflow_warning == NULL
299 || wc < fold_deferred_overflow_code)
301 fold_deferred_overflow_warning = gmsgid;
302 fold_deferred_overflow_code = wc;
305 else if (issue_strict_overflow_warning (wc))
306 warning (OPT_Wstrict_overflow, gmsgid);
309 /* Return true if the built-in mathematical function specified by CODE
310 is odd, i.e. -f(x) == f(-x). */
312 bool
313 negate_mathfn_p (combined_fn fn)
315 switch (fn)
317 CASE_CFN_ASIN:
318 CASE_CFN_ASINH:
319 CASE_CFN_ATAN:
320 CASE_CFN_ATANH:
321 CASE_CFN_CASIN:
322 CASE_CFN_CASINH:
323 CASE_CFN_CATAN:
324 CASE_CFN_CATANH:
325 CASE_CFN_CBRT:
326 CASE_CFN_CPROJ:
327 CASE_CFN_CSIN:
328 CASE_CFN_CSINH:
329 CASE_CFN_CTAN:
330 CASE_CFN_CTANH:
331 CASE_CFN_ERF:
332 CASE_CFN_LLROUND:
333 CASE_CFN_LROUND:
334 CASE_CFN_ROUND:
335 CASE_CFN_SIN:
336 CASE_CFN_SINH:
337 CASE_CFN_TAN:
338 CASE_CFN_TANH:
339 CASE_CFN_TRUNC:
340 return true;
342 CASE_CFN_LLRINT:
343 CASE_CFN_LRINT:
344 CASE_CFN_NEARBYINT:
345 CASE_CFN_RINT:
346 return !flag_rounding_math;
348 default:
349 break;
351 return false;
354 /* Check whether we may negate an integer constant T without causing
355 overflow. */
357 bool
358 may_negate_without_overflow_p (const_tree t)
360 tree type;
362 gcc_assert (TREE_CODE (t) == INTEGER_CST);
364 type = TREE_TYPE (t);
365 if (TYPE_UNSIGNED (type))
366 return false;
368 return !wi::only_sign_bit_p (t);
371 /* Determine whether an expression T can be cheaply negated using
372 the function negate_expr without introducing undefined overflow. */
374 static bool
375 negate_expr_p (tree t)
377 tree type;
379 if (t == 0)
380 return false;
382 type = TREE_TYPE (t);
384 STRIP_SIGN_NOPS (t);
385 switch (TREE_CODE (t))
387 case INTEGER_CST:
388 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
389 return true;
391 /* Check that -CST will not overflow type. */
392 return may_negate_without_overflow_p (t);
393 case BIT_NOT_EXPR:
394 return (INTEGRAL_TYPE_P (type)
395 && TYPE_OVERFLOW_WRAPS (type));
397 case FIXED_CST:
398 return true;
400 case NEGATE_EXPR:
401 return !TYPE_OVERFLOW_SANITIZED (type);
403 case REAL_CST:
404 /* We want to canonicalize to positive real constants. Pretend
405 that only negative ones can be easily negated. */
406 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
408 case COMPLEX_CST:
409 return negate_expr_p (TREE_REALPART (t))
410 && negate_expr_p (TREE_IMAGPART (t));
412 case VECTOR_CST:
414 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
415 return true;
417 int count = TYPE_VECTOR_SUBPARTS (type), i;
419 for (i = 0; i < count; i++)
420 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
421 return false;
423 return true;
426 case COMPLEX_EXPR:
427 return negate_expr_p (TREE_OPERAND (t, 0))
428 && negate_expr_p (TREE_OPERAND (t, 1));
430 case CONJ_EXPR:
431 return negate_expr_p (TREE_OPERAND (t, 0));
433 case PLUS_EXPR:
434 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
435 || HONOR_SIGNED_ZEROS (element_mode (type))
436 || (INTEGRAL_TYPE_P (type)
437 && ! TYPE_OVERFLOW_WRAPS (type)))
438 return false;
439 /* -(A + B) -> (-B) - A. */
440 if (negate_expr_p (TREE_OPERAND (t, 1))
441 && reorder_operands_p (TREE_OPERAND (t, 0),
442 TREE_OPERAND (t, 1)))
443 return true;
444 /* -(A + B) -> (-A) - B. */
445 return negate_expr_p (TREE_OPERAND (t, 0));
447 case MINUS_EXPR:
448 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
449 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
450 && !HONOR_SIGNED_ZEROS (element_mode (type))
451 && (! INTEGRAL_TYPE_P (type)
452 || TYPE_OVERFLOW_WRAPS (type))
453 && reorder_operands_p (TREE_OPERAND (t, 0),
454 TREE_OPERAND (t, 1));
456 case MULT_EXPR:
457 if (TYPE_UNSIGNED (type))
458 break;
459 /* INT_MIN/n * n doesn't overflow while negating one operand it does
460 if n is a power of two. */
461 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
462 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
463 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
464 && ! integer_pow2p (TREE_OPERAND (t, 0)))
465 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
466 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
467 break;
469 /* Fall through. */
471 case RDIV_EXPR:
472 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
473 return negate_expr_p (TREE_OPERAND (t, 1))
474 || negate_expr_p (TREE_OPERAND (t, 0));
475 break;
477 case TRUNC_DIV_EXPR:
478 case ROUND_DIV_EXPR:
479 case EXACT_DIV_EXPR:
480 if (TYPE_UNSIGNED (type))
481 break;
482 if (negate_expr_p (TREE_OPERAND (t, 0)))
483 return true;
484 /* In general we can't negate B in A / B, because if A is INT_MIN and
485 B is 1, we may turn this into INT_MIN / -1 which is undefined
486 and actually traps on some architectures. */
487 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
488 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
489 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
490 && ! integer_onep (TREE_OPERAND (t, 1))))
491 return negate_expr_p (TREE_OPERAND (t, 1));
492 break;
494 case NOP_EXPR:
495 /* Negate -((double)float) as (double)(-float). */
496 if (TREE_CODE (type) == REAL_TYPE)
498 tree tem = strip_float_extensions (t);
499 if (tem != t)
500 return negate_expr_p (tem);
502 break;
504 case CALL_EXPR:
505 /* Negate -f(x) as f(-x). */
506 if (negate_mathfn_p (get_call_combined_fn (t)))
507 return negate_expr_p (CALL_EXPR_ARG (t, 0));
508 break;
510 case RSHIFT_EXPR:
511 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
512 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
514 tree op1 = TREE_OPERAND (t, 1);
515 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
516 return true;
518 break;
520 default:
521 break;
523 return false;
526 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
527 simplification is possible.
528 If negate_expr_p would return true for T, NULL_TREE will never be
529 returned. */
531 static tree
532 fold_negate_expr (location_t loc, tree t)
534 tree type = TREE_TYPE (t);
535 tree tem;
537 switch (TREE_CODE (t))
539 /* Convert - (~A) to A + 1. */
540 case BIT_NOT_EXPR:
541 if (INTEGRAL_TYPE_P (type))
542 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
543 build_one_cst (type));
544 break;
546 case INTEGER_CST:
547 tem = fold_negate_const (t, type);
548 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
549 || (ANY_INTEGRAL_TYPE_P (type)
550 && !TYPE_OVERFLOW_TRAPS (type)
551 && TYPE_OVERFLOW_WRAPS (type))
552 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
553 return tem;
554 break;
556 case REAL_CST:
557 tem = fold_negate_const (t, type);
558 return tem;
560 case FIXED_CST:
561 tem = fold_negate_const (t, type);
562 return tem;
564 case COMPLEX_CST:
566 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
567 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
568 if (rpart && ipart)
569 return build_complex (type, rpart, ipart);
571 break;
573 case VECTOR_CST:
575 int count = TYPE_VECTOR_SUBPARTS (type), i;
576 tree *elts = XALLOCAVEC (tree, count);
578 for (i = 0; i < count; i++)
580 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
581 if (elts[i] == NULL_TREE)
582 return NULL_TREE;
585 return build_vector (type, elts);
588 case COMPLEX_EXPR:
589 if (negate_expr_p (t))
590 return fold_build2_loc (loc, COMPLEX_EXPR, type,
591 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
592 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
593 break;
595 case CONJ_EXPR:
596 if (negate_expr_p (t))
597 return fold_build1_loc (loc, CONJ_EXPR, type,
598 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
599 break;
601 case NEGATE_EXPR:
602 if (!TYPE_OVERFLOW_SANITIZED (type))
603 return TREE_OPERAND (t, 0);
604 break;
606 case PLUS_EXPR:
607 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
608 && !HONOR_SIGNED_ZEROS (element_mode (type)))
610 /* -(A + B) -> (-B) - A. */
611 if (negate_expr_p (TREE_OPERAND (t, 1))
612 && reorder_operands_p (TREE_OPERAND (t, 0),
613 TREE_OPERAND (t, 1)))
615 tem = negate_expr (TREE_OPERAND (t, 1));
616 return fold_build2_loc (loc, MINUS_EXPR, type,
617 tem, TREE_OPERAND (t, 0));
620 /* -(A + B) -> (-A) - B. */
621 if (negate_expr_p (TREE_OPERAND (t, 0)))
623 tem = negate_expr (TREE_OPERAND (t, 0));
624 return fold_build2_loc (loc, MINUS_EXPR, type,
625 tem, TREE_OPERAND (t, 1));
628 break;
630 case MINUS_EXPR:
631 /* - (A - B) -> B - A */
632 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
633 && !HONOR_SIGNED_ZEROS (element_mode (type))
634 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
635 return fold_build2_loc (loc, MINUS_EXPR, type,
636 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
637 break;
639 case MULT_EXPR:
640 if (TYPE_UNSIGNED (type))
641 break;
643 /* Fall through. */
645 case RDIV_EXPR:
646 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
648 tem = TREE_OPERAND (t, 1);
649 if (negate_expr_p (tem))
650 return fold_build2_loc (loc, TREE_CODE (t), type,
651 TREE_OPERAND (t, 0), negate_expr (tem));
652 tem = TREE_OPERAND (t, 0);
653 if (negate_expr_p (tem))
654 return fold_build2_loc (loc, TREE_CODE (t), type,
655 negate_expr (tem), TREE_OPERAND (t, 1));
657 break;
659 case TRUNC_DIV_EXPR:
660 case ROUND_DIV_EXPR:
661 case EXACT_DIV_EXPR:
662 if (TYPE_UNSIGNED (type))
663 break;
664 if (negate_expr_p (TREE_OPERAND (t, 0)))
665 return fold_build2_loc (loc, TREE_CODE (t), type,
666 negate_expr (TREE_OPERAND (t, 0)),
667 TREE_OPERAND (t, 1));
668 /* In general we can't negate B in A / B, because if A is INT_MIN and
669 B is 1, we may turn this into INT_MIN / -1 which is undefined
670 and actually traps on some architectures. */
671 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
672 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
673 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
674 && ! integer_onep (TREE_OPERAND (t, 1))))
675 && negate_expr_p (TREE_OPERAND (t, 1)))
676 return fold_build2_loc (loc, TREE_CODE (t), type,
677 TREE_OPERAND (t, 0),
678 negate_expr (TREE_OPERAND (t, 1)));
679 break;
681 case NOP_EXPR:
682 /* Convert -((double)float) into (double)(-float). */
683 if (TREE_CODE (type) == REAL_TYPE)
685 tem = strip_float_extensions (t);
686 if (tem != t && negate_expr_p (tem))
687 return fold_convert_loc (loc, type, negate_expr (tem));
689 break;
691 case CALL_EXPR:
692 /* Negate -f(x) as f(-x). */
693 if (negate_mathfn_p (get_call_combined_fn (t))
694 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
696 tree fndecl, arg;
698 fndecl = get_callee_fndecl (t);
699 arg = negate_expr (CALL_EXPR_ARG (t, 0));
700 return build_call_expr_loc (loc, fndecl, 1, arg);
702 break;
704 case RSHIFT_EXPR:
705 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
706 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
708 tree op1 = TREE_OPERAND (t, 1);
709 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
711 tree ntype = TYPE_UNSIGNED (type)
712 ? signed_type_for (type)
713 : unsigned_type_for (type);
714 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
715 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
716 return fold_convert_loc (loc, type, temp);
719 break;
721 default:
722 break;
725 return NULL_TREE;
728 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
729 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
730 return NULL_TREE. */
732 static tree
733 negate_expr (tree t)
735 tree type, tem;
736 location_t loc;
738 if (t == NULL_TREE)
739 return NULL_TREE;
741 loc = EXPR_LOCATION (t);
742 type = TREE_TYPE (t);
743 STRIP_SIGN_NOPS (t);
745 tem = fold_negate_expr (loc, t);
746 if (!tem)
747 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
748 return fold_convert_loc (loc, type, tem);
751 /* Split a tree IN into a constant, literal and variable parts that could be
752 combined with CODE to make IN. "constant" means an expression with
753 TREE_CONSTANT but that isn't an actual constant. CODE must be a
754 commutative arithmetic operation. Store the constant part into *CONP,
755 the literal in *LITP and return the variable part. If a part isn't
756 present, set it to null. If the tree does not decompose in this way,
757 return the entire tree as the variable part and the other parts as null.
759 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
760 case, we negate an operand that was subtracted. Except if it is a
761 literal for which we use *MINUS_LITP instead.
763 If NEGATE_P is true, we are negating all of IN, again except a literal
764 for which we use *MINUS_LITP instead. If a variable part is of pointer
765 type, it is negated after converting to TYPE. This prevents us from
766 generating illegal MINUS pointer expression. LOC is the location of
767 the converted variable part.
769 If IN is itself a literal or constant, return it as appropriate.
771 Note that we do not guarantee that any of the three values will be the
772 same type as IN, but they will have the same signedness and mode. */
774 static tree
775 split_tree (location_t loc, tree in, tree type, enum tree_code code,
776 tree *conp, tree *litp, tree *minus_litp, int negate_p)
778 tree var = 0;
780 *conp = 0;
781 *litp = 0;
782 *minus_litp = 0;
784 /* Strip any conversions that don't change the machine mode or signedness. */
785 STRIP_SIGN_NOPS (in);
787 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
788 || TREE_CODE (in) == FIXED_CST)
789 *litp = in;
790 else if (TREE_CODE (in) == code
791 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
792 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
793 /* We can associate addition and subtraction together (even
794 though the C standard doesn't say so) for integers because
795 the value is not affected. For reals, the value might be
796 affected, so we can't. */
797 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
798 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
800 tree op0 = TREE_OPERAND (in, 0);
801 tree op1 = TREE_OPERAND (in, 1);
802 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
803 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
805 /* First see if either of the operands is a literal, then a constant. */
806 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
807 || TREE_CODE (op0) == FIXED_CST)
808 *litp = op0, op0 = 0;
809 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
810 || TREE_CODE (op1) == FIXED_CST)
811 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
813 if (op0 != 0 && TREE_CONSTANT (op0))
814 *conp = op0, op0 = 0;
815 else if (op1 != 0 && TREE_CONSTANT (op1))
816 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
818 /* If we haven't dealt with either operand, this is not a case we can
819 decompose. Otherwise, VAR is either of the ones remaining, if any. */
820 if (op0 != 0 && op1 != 0)
821 var = in;
822 else if (op0 != 0)
823 var = op0;
824 else
825 var = op1, neg_var_p = neg1_p;
827 /* Now do any needed negations. */
828 if (neg_litp_p)
829 *minus_litp = *litp, *litp = 0;
830 if (neg_conp_p)
831 *conp = negate_expr (*conp);
832 if (neg_var_p && var)
834 /* Convert to TYPE before negating. */
835 var = fold_convert_loc (loc, type, var);
836 var = negate_expr (var);
839 else if (TREE_CODE (in) == BIT_NOT_EXPR
840 && code == PLUS_EXPR)
842 /* -X - 1 is folded to ~X, undo that here. */
843 *minus_litp = build_one_cst (TREE_TYPE (in));
844 var = negate_expr (TREE_OPERAND (in, 0));
846 else if (TREE_CONSTANT (in))
847 *conp = in;
848 else
849 var = in;
851 if (negate_p)
853 if (*litp)
854 *minus_litp = *litp, *litp = 0;
855 else if (*minus_litp)
856 *litp = *minus_litp, *minus_litp = 0;
857 *conp = negate_expr (*conp);
858 if (var)
860 /* Convert to TYPE before negating. */
861 var = fold_convert_loc (loc, type, var);
862 var = negate_expr (var);
866 return var;
869 /* Re-associate trees split by the above function. T1 and T2 are
870 either expressions to associate or null. Return the new
871 expression, if any. LOC is the location of the new expression. If
872 we build an operation, do it in TYPE and with CODE. */
874 static tree
875 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
877 if (t1 == 0)
878 return t2;
879 else if (t2 == 0)
880 return t1;
882 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
883 try to fold this since we will have infinite recursion. But do
884 deal with any NEGATE_EXPRs. */
885 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
886 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
888 if (code == PLUS_EXPR)
890 if (TREE_CODE (t1) == NEGATE_EXPR)
891 return build2_loc (loc, MINUS_EXPR, type,
892 fold_convert_loc (loc, type, t2),
893 fold_convert_loc (loc, type,
894 TREE_OPERAND (t1, 0)));
895 else if (TREE_CODE (t2) == NEGATE_EXPR)
896 return build2_loc (loc, MINUS_EXPR, type,
897 fold_convert_loc (loc, type, t1),
898 fold_convert_loc (loc, type,
899 TREE_OPERAND (t2, 0)));
900 else if (integer_zerop (t2))
901 return fold_convert_loc (loc, type, t1);
903 else if (code == MINUS_EXPR)
905 if (integer_zerop (t2))
906 return fold_convert_loc (loc, type, t1);
909 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
910 fold_convert_loc (loc, type, t2));
913 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
914 fold_convert_loc (loc, type, t2));
917 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
918 for use in int_const_binop, size_binop and size_diffop. */
920 static bool
921 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
923 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
924 return false;
925 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
926 return false;
928 switch (code)
930 case LSHIFT_EXPR:
931 case RSHIFT_EXPR:
932 case LROTATE_EXPR:
933 case RROTATE_EXPR:
934 return true;
936 default:
937 break;
940 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
941 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
942 && TYPE_MODE (type1) == TYPE_MODE (type2);
946 /* Combine two integer constants ARG1 and ARG2 under operation CODE
947 to produce a new constant. Return NULL_TREE if we don't know how
948 to evaluate CODE at compile-time. */
950 static tree
951 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
952 int overflowable)
954 wide_int res;
955 tree t;
956 tree type = TREE_TYPE (arg1);
957 signop sign = TYPE_SIGN (type);
958 bool overflow = false;
960 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
962 switch (code)
964 case BIT_IOR_EXPR:
965 res = wi::bit_or (arg1, arg2);
966 break;
968 case BIT_XOR_EXPR:
969 res = wi::bit_xor (arg1, arg2);
970 break;
972 case BIT_AND_EXPR:
973 res = wi::bit_and (arg1, arg2);
974 break;
976 case RSHIFT_EXPR:
977 case LSHIFT_EXPR:
978 if (wi::neg_p (arg2))
980 arg2 = -arg2;
981 if (code == RSHIFT_EXPR)
982 code = LSHIFT_EXPR;
983 else
984 code = RSHIFT_EXPR;
987 if (code == RSHIFT_EXPR)
988 /* It's unclear from the C standard whether shifts can overflow.
989 The following code ignores overflow; perhaps a C standard
990 interpretation ruling is needed. */
991 res = wi::rshift (arg1, arg2, sign);
992 else
993 res = wi::lshift (arg1, arg2);
994 break;
996 case RROTATE_EXPR:
997 case LROTATE_EXPR:
998 if (wi::neg_p (arg2))
1000 arg2 = -arg2;
1001 if (code == RROTATE_EXPR)
1002 code = LROTATE_EXPR;
1003 else
1004 code = RROTATE_EXPR;
1007 if (code == RROTATE_EXPR)
1008 res = wi::rrotate (arg1, arg2);
1009 else
1010 res = wi::lrotate (arg1, arg2);
1011 break;
1013 case PLUS_EXPR:
1014 res = wi::add (arg1, arg2, sign, &overflow);
1015 break;
1017 case MINUS_EXPR:
1018 res = wi::sub (arg1, arg2, sign, &overflow);
1019 break;
1021 case MULT_EXPR:
1022 res = wi::mul (arg1, arg2, sign, &overflow);
1023 break;
1025 case MULT_HIGHPART_EXPR:
1026 res = wi::mul_high (arg1, arg2, sign);
1027 break;
1029 case TRUNC_DIV_EXPR:
1030 case EXACT_DIV_EXPR:
1031 if (arg2 == 0)
1032 return NULL_TREE;
1033 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1034 break;
1036 case FLOOR_DIV_EXPR:
1037 if (arg2 == 0)
1038 return NULL_TREE;
1039 res = wi::div_floor (arg1, arg2, sign, &overflow);
1040 break;
1042 case CEIL_DIV_EXPR:
1043 if (arg2 == 0)
1044 return NULL_TREE;
1045 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1046 break;
1048 case ROUND_DIV_EXPR:
1049 if (arg2 == 0)
1050 return NULL_TREE;
1051 res = wi::div_round (arg1, arg2, sign, &overflow);
1052 break;
1054 case TRUNC_MOD_EXPR:
1055 if (arg2 == 0)
1056 return NULL_TREE;
1057 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1058 break;
1060 case FLOOR_MOD_EXPR:
1061 if (arg2 == 0)
1062 return NULL_TREE;
1063 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1064 break;
1066 case CEIL_MOD_EXPR:
1067 if (arg2 == 0)
1068 return NULL_TREE;
1069 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1070 break;
1072 case ROUND_MOD_EXPR:
1073 if (arg2 == 0)
1074 return NULL_TREE;
1075 res = wi::mod_round (arg1, arg2, sign, &overflow);
1076 break;
1078 case MIN_EXPR:
1079 res = wi::min (arg1, arg2, sign);
1080 break;
1082 case MAX_EXPR:
1083 res = wi::max (arg1, arg2, sign);
1084 break;
1086 default:
1087 return NULL_TREE;
1090 t = force_fit_type (type, res, overflowable,
1091 (((sign == SIGNED || overflowable == -1)
1092 && overflow)
1093 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1095 return t;
1098 tree
1099 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1101 return int_const_binop_1 (code, arg1, arg2, 1);
1104 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1105 constant. We assume ARG1 and ARG2 have the same data type, or at least
1106 are the same kind of constant and the same machine mode. Return zero if
1107 combining the constants is not allowed in the current operating mode. */
1109 static tree
1110 const_binop (enum tree_code code, tree arg1, tree arg2)
1112 /* Sanity check for the recursive cases. */
1113 if (!arg1 || !arg2)
1114 return NULL_TREE;
1116 STRIP_NOPS (arg1);
1117 STRIP_NOPS (arg2);
1119 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1121 if (code == POINTER_PLUS_EXPR)
1122 return int_const_binop (PLUS_EXPR,
1123 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1125 return int_const_binop (code, arg1, arg2);
1128 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1130 machine_mode mode;
1131 REAL_VALUE_TYPE d1;
1132 REAL_VALUE_TYPE d2;
1133 REAL_VALUE_TYPE value;
1134 REAL_VALUE_TYPE result;
1135 bool inexact;
1136 tree t, type;
1138 /* The following codes are handled by real_arithmetic. */
1139 switch (code)
1141 case PLUS_EXPR:
1142 case MINUS_EXPR:
1143 case MULT_EXPR:
1144 case RDIV_EXPR:
1145 case MIN_EXPR:
1146 case MAX_EXPR:
1147 break;
1149 default:
1150 return NULL_TREE;
1153 d1 = TREE_REAL_CST (arg1);
1154 d2 = TREE_REAL_CST (arg2);
1156 type = TREE_TYPE (arg1);
1157 mode = TYPE_MODE (type);
1159 /* Don't perform operation if we honor signaling NaNs and
1160 either operand is a signaling NaN. */
1161 if (HONOR_SNANS (mode)
1162 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1163 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1164 return NULL_TREE;
1166 /* Don't perform operation if it would raise a division
1167 by zero exception. */
1168 if (code == RDIV_EXPR
1169 && real_equal (&d2, &dconst0)
1170 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1171 return NULL_TREE;
1173 /* If either operand is a NaN, just return it. Otherwise, set up
1174 for floating-point trap; we return an overflow. */
1175 if (REAL_VALUE_ISNAN (d1))
1177 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1178 is off. */
1179 d1.signalling = 0;
1180 t = build_real (type, d1);
1181 return t;
1183 else if (REAL_VALUE_ISNAN (d2))
1185 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1186 is off. */
1187 d2.signalling = 0;
1188 t = build_real (type, d2);
1189 return t;
1192 inexact = real_arithmetic (&value, code, &d1, &d2);
1193 real_convert (&result, mode, &value);
1195 /* Don't constant fold this floating point operation if
1196 the result has overflowed and flag_trapping_math. */
1197 if (flag_trapping_math
1198 && MODE_HAS_INFINITIES (mode)
1199 && REAL_VALUE_ISINF (result)
1200 && !REAL_VALUE_ISINF (d1)
1201 && !REAL_VALUE_ISINF (d2))
1202 return NULL_TREE;
1204 /* Don't constant fold this floating point operation if the
1205 result may dependent upon the run-time rounding mode and
1206 flag_rounding_math is set, or if GCC's software emulation
1207 is unable to accurately represent the result. */
1208 if ((flag_rounding_math
1209 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1210 && (inexact || !real_identical (&result, &value)))
1211 return NULL_TREE;
1213 t = build_real (type, result);
1215 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1216 return t;
1219 if (TREE_CODE (arg1) == FIXED_CST)
1221 FIXED_VALUE_TYPE f1;
1222 FIXED_VALUE_TYPE f2;
1223 FIXED_VALUE_TYPE result;
1224 tree t, type;
1225 int sat_p;
1226 bool overflow_p;
1228 /* The following codes are handled by fixed_arithmetic. */
1229 switch (code)
1231 case PLUS_EXPR:
1232 case MINUS_EXPR:
1233 case MULT_EXPR:
1234 case TRUNC_DIV_EXPR:
1235 if (TREE_CODE (arg2) != FIXED_CST)
1236 return NULL_TREE;
1237 f2 = TREE_FIXED_CST (arg2);
1238 break;
1240 case LSHIFT_EXPR:
1241 case RSHIFT_EXPR:
1243 if (TREE_CODE (arg2) != INTEGER_CST)
1244 return NULL_TREE;
1245 wide_int w2 = arg2;
1246 f2.data.high = w2.elt (1);
1247 f2.data.low = w2.elt (0);
1248 f2.mode = SImode;
1250 break;
1252 default:
1253 return NULL_TREE;
1256 f1 = TREE_FIXED_CST (arg1);
1257 type = TREE_TYPE (arg1);
1258 sat_p = TYPE_SATURATING (type);
1259 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1260 t = build_fixed (type, result);
1261 /* Propagate overflow flags. */
1262 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1263 TREE_OVERFLOW (t) = 1;
1264 return t;
1267 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1269 tree type = TREE_TYPE (arg1);
1270 tree r1 = TREE_REALPART (arg1);
1271 tree i1 = TREE_IMAGPART (arg1);
1272 tree r2 = TREE_REALPART (arg2);
1273 tree i2 = TREE_IMAGPART (arg2);
1274 tree real, imag;
1276 switch (code)
1278 case PLUS_EXPR:
1279 case MINUS_EXPR:
1280 real = const_binop (code, r1, r2);
1281 imag = const_binop (code, i1, i2);
1282 break;
1284 case MULT_EXPR:
1285 if (COMPLEX_FLOAT_TYPE_P (type))
1286 return do_mpc_arg2 (arg1, arg2, type,
1287 /* do_nonfinite= */ folding_initializer,
1288 mpc_mul);
1290 real = const_binop (MINUS_EXPR,
1291 const_binop (MULT_EXPR, r1, r2),
1292 const_binop (MULT_EXPR, i1, i2));
1293 imag = const_binop (PLUS_EXPR,
1294 const_binop (MULT_EXPR, r1, i2),
1295 const_binop (MULT_EXPR, i1, r2));
1296 break;
1298 case RDIV_EXPR:
1299 if (COMPLEX_FLOAT_TYPE_P (type))
1300 return do_mpc_arg2 (arg1, arg2, type,
1301 /* do_nonfinite= */ folding_initializer,
1302 mpc_div);
1303 /* Fallthru ... */
1304 case TRUNC_DIV_EXPR:
1305 case CEIL_DIV_EXPR:
1306 case FLOOR_DIV_EXPR:
1307 case ROUND_DIV_EXPR:
1308 if (flag_complex_method == 0)
1310 /* Keep this algorithm in sync with
1311 tree-complex.c:expand_complex_div_straight().
1313 Expand complex division to scalars, straightforward algorithm.
1314 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1315 t = br*br + bi*bi
1317 tree magsquared
1318 = const_binop (PLUS_EXPR,
1319 const_binop (MULT_EXPR, r2, r2),
1320 const_binop (MULT_EXPR, i2, i2));
1321 tree t1
1322 = const_binop (PLUS_EXPR,
1323 const_binop (MULT_EXPR, r1, r2),
1324 const_binop (MULT_EXPR, i1, i2));
1325 tree t2
1326 = const_binop (MINUS_EXPR,
1327 const_binop (MULT_EXPR, i1, r2),
1328 const_binop (MULT_EXPR, r1, i2));
1330 real = const_binop (code, t1, magsquared);
1331 imag = const_binop (code, t2, magsquared);
1333 else
1335 /* Keep this algorithm in sync with
1336 tree-complex.c:expand_complex_div_wide().
1338 Expand complex division to scalars, modified algorithm to minimize
1339 overflow with wide input ranges. */
1340 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1341 fold_abs_const (r2, TREE_TYPE (type)),
1342 fold_abs_const (i2, TREE_TYPE (type)));
1344 if (integer_nonzerop (compare))
1346 /* In the TRUE branch, we compute
1347 ratio = br/bi;
1348 div = (br * ratio) + bi;
1349 tr = (ar * ratio) + ai;
1350 ti = (ai * ratio) - ar;
1351 tr = tr / div;
1352 ti = ti / div; */
1353 tree ratio = const_binop (code, r2, i2);
1354 tree div = const_binop (PLUS_EXPR, i2,
1355 const_binop (MULT_EXPR, r2, ratio));
1356 real = const_binop (MULT_EXPR, r1, ratio);
1357 real = const_binop (PLUS_EXPR, real, i1);
1358 real = const_binop (code, real, div);
1360 imag = const_binop (MULT_EXPR, i1, ratio);
1361 imag = const_binop (MINUS_EXPR, imag, r1);
1362 imag = const_binop (code, imag, div);
1364 else
1366 /* In the FALSE branch, we compute
1367 ratio = d/c;
1368 divisor = (d * ratio) + c;
1369 tr = (b * ratio) + a;
1370 ti = b - (a * ratio);
1371 tr = tr / div;
1372 ti = ti / div; */
1373 tree ratio = const_binop (code, i2, r2);
1374 tree div = const_binop (PLUS_EXPR, r2,
1375 const_binop (MULT_EXPR, i2, ratio));
1377 real = const_binop (MULT_EXPR, i1, ratio);
1378 real = const_binop (PLUS_EXPR, real, r1);
1379 real = const_binop (code, real, div);
1381 imag = const_binop (MULT_EXPR, r1, ratio);
1382 imag = const_binop (MINUS_EXPR, i1, imag);
1383 imag = const_binop (code, imag, div);
1386 break;
1388 default:
1389 return NULL_TREE;
1392 if (real && imag)
1393 return build_complex (type, real, imag);
1396 if (TREE_CODE (arg1) == VECTOR_CST
1397 && TREE_CODE (arg2) == VECTOR_CST)
1399 tree type = TREE_TYPE (arg1);
1400 int count = TYPE_VECTOR_SUBPARTS (type), i;
1401 tree *elts = XALLOCAVEC (tree, count);
1403 for (i = 0; i < count; i++)
1405 tree elem1 = VECTOR_CST_ELT (arg1, i);
1406 tree elem2 = VECTOR_CST_ELT (arg2, i);
1408 elts[i] = const_binop (code, elem1, elem2);
1410 /* It is possible that const_binop cannot handle the given
1411 code and return NULL_TREE */
1412 if (elts[i] == NULL_TREE)
1413 return NULL_TREE;
1416 return build_vector (type, elts);
1419 /* Shifts allow a scalar offset for a vector. */
1420 if (TREE_CODE (arg1) == VECTOR_CST
1421 && TREE_CODE (arg2) == INTEGER_CST)
1423 tree type = TREE_TYPE (arg1);
1424 int count = TYPE_VECTOR_SUBPARTS (type), i;
1425 tree *elts = XALLOCAVEC (tree, count);
1427 for (i = 0; i < count; i++)
1429 tree elem1 = VECTOR_CST_ELT (arg1, i);
1431 elts[i] = const_binop (code, elem1, arg2);
1433 /* It is possible that const_binop cannot handle the given
1434 code and return NULL_TREE. */
1435 if (elts[i] == NULL_TREE)
1436 return NULL_TREE;
1439 return build_vector (type, elts);
1441 return NULL_TREE;
1444 /* Overload that adds a TYPE parameter to be able to dispatch
1445 to fold_relational_const. */
1447 tree
1448 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1450 if (TREE_CODE_CLASS (code) == tcc_comparison)
1451 return fold_relational_const (code, type, arg1, arg2);
1453 /* ??? Until we make the const_binop worker take the type of the
1454 result as argument put those cases that need it here. */
1455 switch (code)
1457 case COMPLEX_EXPR:
1458 if ((TREE_CODE (arg1) == REAL_CST
1459 && TREE_CODE (arg2) == REAL_CST)
1460 || (TREE_CODE (arg1) == INTEGER_CST
1461 && TREE_CODE (arg2) == INTEGER_CST))
1462 return build_complex (type, arg1, arg2);
1463 return NULL_TREE;
1465 case VEC_PACK_TRUNC_EXPR:
1466 case VEC_PACK_FIX_TRUNC_EXPR:
1468 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1469 tree *elts;
1471 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1472 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1473 if (TREE_CODE (arg1) != VECTOR_CST
1474 || TREE_CODE (arg2) != VECTOR_CST)
1475 return NULL_TREE;
1477 elts = XALLOCAVEC (tree, nelts);
1478 if (!vec_cst_ctor_to_array (arg1, elts)
1479 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1480 return NULL_TREE;
1482 for (i = 0; i < nelts; i++)
1484 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1485 ? NOP_EXPR : FIX_TRUNC_EXPR,
1486 TREE_TYPE (type), elts[i]);
1487 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1488 return NULL_TREE;
1491 return build_vector (type, elts);
1494 case VEC_WIDEN_MULT_LO_EXPR:
1495 case VEC_WIDEN_MULT_HI_EXPR:
1496 case VEC_WIDEN_MULT_EVEN_EXPR:
1497 case VEC_WIDEN_MULT_ODD_EXPR:
1499 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1500 unsigned int out, ofs, scale;
1501 tree *elts;
1503 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1504 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1505 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1506 return NULL_TREE;
1508 elts = XALLOCAVEC (tree, nelts * 4);
1509 if (!vec_cst_ctor_to_array (arg1, elts)
1510 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1511 return NULL_TREE;
1513 if (code == VEC_WIDEN_MULT_LO_EXPR)
1514 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1515 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1516 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1517 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1518 scale = 1, ofs = 0;
1519 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1520 scale = 1, ofs = 1;
1522 for (out = 0; out < nelts; out++)
1524 unsigned int in1 = (out << scale) + ofs;
1525 unsigned int in2 = in1 + nelts * 2;
1526 tree t1, t2;
1528 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1529 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1531 if (t1 == NULL_TREE || t2 == NULL_TREE)
1532 return NULL_TREE;
1533 elts[out] = const_binop (MULT_EXPR, t1, t2);
1534 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1535 return NULL_TREE;
1538 return build_vector (type, elts);
1541 default:;
1544 if (TREE_CODE_CLASS (code) != tcc_binary)
1545 return NULL_TREE;
1547 /* Make sure type and arg0 have the same saturating flag. */
1548 gcc_checking_assert (TYPE_SATURATING (type)
1549 == TYPE_SATURATING (TREE_TYPE (arg1)));
1551 return const_binop (code, arg1, arg2);
1554 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1555 Return zero if computing the constants is not possible. */
1557 tree
1558 const_unop (enum tree_code code, tree type, tree arg0)
1560 /* Don't perform the operation, other than NEGATE and ABS, if
1561 flag_signaling_nans is on and the operand is a signaling NaN. */
1562 if (TREE_CODE (arg0) == REAL_CST
1563 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1564 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1565 && code != NEGATE_EXPR
1566 && code != ABS_EXPR)
1567 return NULL_TREE;
1569 switch (code)
1571 CASE_CONVERT:
1572 case FLOAT_EXPR:
1573 case FIX_TRUNC_EXPR:
1574 case FIXED_CONVERT_EXPR:
1575 return fold_convert_const (code, type, arg0);
1577 case ADDR_SPACE_CONVERT_EXPR:
1578 /* If the source address is 0, and the source address space
1579 cannot have a valid object at 0, fold to dest type null. */
1580 if (integer_zerop (arg0)
1581 && !(targetm.addr_space.zero_address_valid
1582 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1583 return fold_convert_const (code, type, arg0);
1584 break;
1586 case VIEW_CONVERT_EXPR:
1587 return fold_view_convert_expr (type, arg0);
1589 case NEGATE_EXPR:
1591 /* Can't call fold_negate_const directly here as that doesn't
1592 handle all cases and we might not be able to negate some
1593 constants. */
1594 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1595 if (tem && CONSTANT_CLASS_P (tem))
1596 return tem;
1597 break;
1600 case ABS_EXPR:
1601 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1602 return fold_abs_const (arg0, type);
1603 break;
1605 case CONJ_EXPR:
1606 if (TREE_CODE (arg0) == COMPLEX_CST)
1608 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1609 TREE_TYPE (type));
1610 return build_complex (type, TREE_REALPART (arg0), ipart);
1612 break;
1614 case BIT_NOT_EXPR:
1615 if (TREE_CODE (arg0) == INTEGER_CST)
1616 return fold_not_const (arg0, type);
1617 /* Perform BIT_NOT_EXPR on each element individually. */
1618 else if (TREE_CODE (arg0) == VECTOR_CST)
1620 tree *elements;
1621 tree elem;
1622 unsigned count = VECTOR_CST_NELTS (arg0), i;
1624 elements = XALLOCAVEC (tree, count);
1625 for (i = 0; i < count; i++)
1627 elem = VECTOR_CST_ELT (arg0, i);
1628 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1629 if (elem == NULL_TREE)
1630 break;
1631 elements[i] = elem;
1633 if (i == count)
1634 return build_vector (type, elements);
1636 break;
1638 case TRUTH_NOT_EXPR:
1639 if (TREE_CODE (arg0) == INTEGER_CST)
1640 return constant_boolean_node (integer_zerop (arg0), type);
1641 break;
1643 case REALPART_EXPR:
1644 if (TREE_CODE (arg0) == COMPLEX_CST)
1645 return fold_convert (type, TREE_REALPART (arg0));
1646 break;
1648 case IMAGPART_EXPR:
1649 if (TREE_CODE (arg0) == COMPLEX_CST)
1650 return fold_convert (type, TREE_IMAGPART (arg0));
1651 break;
1653 case VEC_UNPACK_LO_EXPR:
1654 case VEC_UNPACK_HI_EXPR:
1655 case VEC_UNPACK_FLOAT_LO_EXPR:
1656 case VEC_UNPACK_FLOAT_HI_EXPR:
1658 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1659 tree *elts;
1660 enum tree_code subcode;
1662 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1663 if (TREE_CODE (arg0) != VECTOR_CST)
1664 return NULL_TREE;
1666 elts = XALLOCAVEC (tree, nelts * 2);
1667 if (!vec_cst_ctor_to_array (arg0, elts))
1668 return NULL_TREE;
1670 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1671 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1672 elts += nelts;
1674 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1675 subcode = NOP_EXPR;
1676 else
1677 subcode = FLOAT_EXPR;
1679 for (i = 0; i < nelts; i++)
1681 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1682 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1683 return NULL_TREE;
1686 return build_vector (type, elts);
1689 case REDUC_MIN_EXPR:
1690 case REDUC_MAX_EXPR:
1691 case REDUC_PLUS_EXPR:
1693 unsigned int nelts, i;
1694 tree *elts;
1695 enum tree_code subcode;
1697 if (TREE_CODE (arg0) != VECTOR_CST)
1698 return NULL_TREE;
1699 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1701 elts = XALLOCAVEC (tree, nelts);
1702 if (!vec_cst_ctor_to_array (arg0, elts))
1703 return NULL_TREE;
1705 switch (code)
1707 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1708 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1709 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1710 default: gcc_unreachable ();
1713 for (i = 1; i < nelts; i++)
1715 elts[0] = const_binop (subcode, elts[0], elts[i]);
1716 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1717 return NULL_TREE;
1720 return elts[0];
1723 default:
1724 break;
1727 return NULL_TREE;
1730 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1731 indicates which particular sizetype to create. */
1733 tree
1734 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1736 return build_int_cst (sizetype_tab[(int) kind], number);
1739 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1740 is a tree code. The type of the result is taken from the operands.
1741 Both must be equivalent integer types, ala int_binop_types_match_p.
1742 If the operands are constant, so is the result. */
1744 tree
1745 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1747 tree type = TREE_TYPE (arg0);
1749 if (arg0 == error_mark_node || arg1 == error_mark_node)
1750 return error_mark_node;
1752 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1753 TREE_TYPE (arg1)));
1755 /* Handle the special case of two integer constants faster. */
1756 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1758 /* And some specific cases even faster than that. */
1759 if (code == PLUS_EXPR)
1761 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1762 return arg1;
1763 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1764 return arg0;
1766 else if (code == MINUS_EXPR)
1768 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1769 return arg0;
1771 else if (code == MULT_EXPR)
1773 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1774 return arg1;
1777 /* Handle general case of two integer constants. For sizetype
1778 constant calculations we always want to know about overflow,
1779 even in the unsigned case. */
1780 return int_const_binop_1 (code, arg0, arg1, -1);
1783 return fold_build2_loc (loc, code, type, arg0, arg1);
1786 /* Given two values, either both of sizetype or both of bitsizetype,
1787 compute the difference between the two values. Return the value
1788 in signed type corresponding to the type of the operands. */
1790 tree
1791 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1793 tree type = TREE_TYPE (arg0);
1794 tree ctype;
1796 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1797 TREE_TYPE (arg1)));
1799 /* If the type is already signed, just do the simple thing. */
1800 if (!TYPE_UNSIGNED (type))
1801 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1803 if (type == sizetype)
1804 ctype = ssizetype;
1805 else if (type == bitsizetype)
1806 ctype = sbitsizetype;
1807 else
1808 ctype = signed_type_for (type);
1810 /* If either operand is not a constant, do the conversions to the signed
1811 type and subtract. The hardware will do the right thing with any
1812 overflow in the subtraction. */
1813 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1814 return size_binop_loc (loc, MINUS_EXPR,
1815 fold_convert_loc (loc, ctype, arg0),
1816 fold_convert_loc (loc, ctype, arg1));
1818 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1819 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1820 overflow) and negate (which can't either). Special-case a result
1821 of zero while we're here. */
1822 if (tree_int_cst_equal (arg0, arg1))
1823 return build_int_cst (ctype, 0);
1824 else if (tree_int_cst_lt (arg1, arg0))
1825 return fold_convert_loc (loc, ctype,
1826 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1827 else
1828 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1829 fold_convert_loc (loc, ctype,
1830 size_binop_loc (loc,
1831 MINUS_EXPR,
1832 arg1, arg0)));
1835 /* A subroutine of fold_convert_const handling conversions of an
1836 INTEGER_CST to another integer type. */
1838 static tree
1839 fold_convert_const_int_from_int (tree type, const_tree arg1)
1841 /* Given an integer constant, make new constant with new type,
1842 appropriately sign-extended or truncated. Use widest_int
1843 so that any extension is done according ARG1's type. */
1844 return force_fit_type (type, wi::to_widest (arg1),
1845 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1846 TREE_OVERFLOW (arg1));
1849 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1850 to an integer type. */
1852 static tree
1853 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1855 bool overflow = false;
1856 tree t;
1858 /* The following code implements the floating point to integer
1859 conversion rules required by the Java Language Specification,
1860 that IEEE NaNs are mapped to zero and values that overflow
1861 the target precision saturate, i.e. values greater than
1862 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1863 are mapped to INT_MIN. These semantics are allowed by the
1864 C and C++ standards that simply state that the behavior of
1865 FP-to-integer conversion is unspecified upon overflow. */
1867 wide_int val;
1868 REAL_VALUE_TYPE r;
1869 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1871 switch (code)
1873 case FIX_TRUNC_EXPR:
1874 real_trunc (&r, VOIDmode, &x);
1875 break;
1877 default:
1878 gcc_unreachable ();
1881 /* If R is NaN, return zero and show we have an overflow. */
1882 if (REAL_VALUE_ISNAN (r))
1884 overflow = true;
1885 val = wi::zero (TYPE_PRECISION (type));
1888 /* See if R is less than the lower bound or greater than the
1889 upper bound. */
1891 if (! overflow)
1893 tree lt = TYPE_MIN_VALUE (type);
1894 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1895 if (real_less (&r, &l))
1897 overflow = true;
1898 val = lt;
1902 if (! overflow)
1904 tree ut = TYPE_MAX_VALUE (type);
1905 if (ut)
1907 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1908 if (real_less (&u, &r))
1910 overflow = true;
1911 val = ut;
1916 if (! overflow)
1917 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1919 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1920 return t;
1923 /* A subroutine of fold_convert_const handling conversions of a
1924 FIXED_CST to an integer type. */
1926 static tree
1927 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1929 tree t;
1930 double_int temp, temp_trunc;
1931 unsigned int mode;
1933 /* Right shift FIXED_CST to temp by fbit. */
1934 temp = TREE_FIXED_CST (arg1).data;
1935 mode = TREE_FIXED_CST (arg1).mode;
1936 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1938 temp = temp.rshift (GET_MODE_FBIT (mode),
1939 HOST_BITS_PER_DOUBLE_INT,
1940 SIGNED_FIXED_POINT_MODE_P (mode));
1942 /* Left shift temp to temp_trunc by fbit. */
1943 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1944 HOST_BITS_PER_DOUBLE_INT,
1945 SIGNED_FIXED_POINT_MODE_P (mode));
1947 else
1949 temp = double_int_zero;
1950 temp_trunc = double_int_zero;
1953 /* If FIXED_CST is negative, we need to round the value toward 0.
1954 By checking if the fractional bits are not zero to add 1 to temp. */
1955 if (SIGNED_FIXED_POINT_MODE_P (mode)
1956 && temp_trunc.is_negative ()
1957 && TREE_FIXED_CST (arg1).data != temp_trunc)
1958 temp += double_int_one;
1960 /* Given a fixed-point constant, make new constant with new type,
1961 appropriately sign-extended or truncated. */
1962 t = force_fit_type (type, temp, -1,
1963 (temp.is_negative ()
1964 && (TYPE_UNSIGNED (type)
1965 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1966 | TREE_OVERFLOW (arg1));
1968 return t;
1971 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1972 to another floating point type. */
1974 static tree
1975 fold_convert_const_real_from_real (tree type, const_tree arg1)
1977 REAL_VALUE_TYPE value;
1978 tree t;
1980 /* Don't perform the operation if flag_signaling_nans is on
1981 and the operand is a signaling NaN. */
1982 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1983 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1984 return NULL_TREE;
1986 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1987 t = build_real (type, value);
1989 /* If converting an infinity or NAN to a representation that doesn't
1990 have one, set the overflow bit so that we can produce some kind of
1991 error message at the appropriate point if necessary. It's not the
1992 most user-friendly message, but it's better than nothing. */
1993 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1994 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1995 TREE_OVERFLOW (t) = 1;
1996 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1997 && !MODE_HAS_NANS (TYPE_MODE (type)))
1998 TREE_OVERFLOW (t) = 1;
1999 /* Regular overflow, conversion produced an infinity in a mode that
2000 can't represent them. */
2001 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2002 && REAL_VALUE_ISINF (value)
2003 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2004 TREE_OVERFLOW (t) = 1;
2005 else
2006 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2007 return t;
2010 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2011 to a floating point type. */
2013 static tree
2014 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2016 REAL_VALUE_TYPE value;
2017 tree t;
2019 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2020 t = build_real (type, value);
2022 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2023 return t;
2026 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2027 to another fixed-point type. */
2029 static tree
2030 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2032 FIXED_VALUE_TYPE value;
2033 tree t;
2034 bool overflow_p;
2036 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2037 TYPE_SATURATING (type));
2038 t = build_fixed (type, value);
2040 /* Propagate overflow flags. */
2041 if (overflow_p | TREE_OVERFLOW (arg1))
2042 TREE_OVERFLOW (t) = 1;
2043 return t;
2046 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2047 to a fixed-point type. */
2049 static tree
2050 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2052 FIXED_VALUE_TYPE value;
2053 tree t;
2054 bool overflow_p;
2055 double_int di;
2057 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2059 di.low = TREE_INT_CST_ELT (arg1, 0);
2060 if (TREE_INT_CST_NUNITS (arg1) == 1)
2061 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2062 else
2063 di.high = TREE_INT_CST_ELT (arg1, 1);
2065 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2066 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2067 TYPE_SATURATING (type));
2068 t = build_fixed (type, value);
2070 /* Propagate overflow flags. */
2071 if (overflow_p | TREE_OVERFLOW (arg1))
2072 TREE_OVERFLOW (t) = 1;
2073 return t;
2076 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2077 to a fixed-point type. */
2079 static tree
2080 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2082 FIXED_VALUE_TYPE value;
2083 tree t;
2084 bool overflow_p;
2086 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2087 &TREE_REAL_CST (arg1),
2088 TYPE_SATURATING (type));
2089 t = build_fixed (type, value);
2091 /* Propagate overflow flags. */
2092 if (overflow_p | TREE_OVERFLOW (arg1))
2093 TREE_OVERFLOW (t) = 1;
2094 return t;
2097 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2098 type TYPE. If no simplification can be done return NULL_TREE. */
2100 static tree
2101 fold_convert_const (enum tree_code code, tree type, tree arg1)
2103 if (TREE_TYPE (arg1) == type)
2104 return arg1;
2106 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2107 || TREE_CODE (type) == OFFSET_TYPE)
2109 if (TREE_CODE (arg1) == INTEGER_CST)
2110 return fold_convert_const_int_from_int (type, arg1);
2111 else if (TREE_CODE (arg1) == REAL_CST)
2112 return fold_convert_const_int_from_real (code, type, arg1);
2113 else if (TREE_CODE (arg1) == FIXED_CST)
2114 return fold_convert_const_int_from_fixed (type, arg1);
2116 else if (TREE_CODE (type) == REAL_TYPE)
2118 if (TREE_CODE (arg1) == INTEGER_CST)
2119 return build_real_from_int_cst (type, arg1);
2120 else if (TREE_CODE (arg1) == REAL_CST)
2121 return fold_convert_const_real_from_real (type, arg1);
2122 else if (TREE_CODE (arg1) == FIXED_CST)
2123 return fold_convert_const_real_from_fixed (type, arg1);
2125 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2127 if (TREE_CODE (arg1) == FIXED_CST)
2128 return fold_convert_const_fixed_from_fixed (type, arg1);
2129 else if (TREE_CODE (arg1) == INTEGER_CST)
2130 return fold_convert_const_fixed_from_int (type, arg1);
2131 else if (TREE_CODE (arg1) == REAL_CST)
2132 return fold_convert_const_fixed_from_real (type, arg1);
2134 else if (TREE_CODE (type) == VECTOR_TYPE)
2136 if (TREE_CODE (arg1) == VECTOR_CST
2137 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2139 int len = TYPE_VECTOR_SUBPARTS (type);
2140 tree elttype = TREE_TYPE (type);
2141 tree *v = XALLOCAVEC (tree, len);
2142 for (int i = 0; i < len; ++i)
2144 tree elt = VECTOR_CST_ELT (arg1, i);
2145 tree cvt = fold_convert_const (code, elttype, elt);
2146 if (cvt == NULL_TREE)
2147 return NULL_TREE;
2148 v[i] = cvt;
2150 return build_vector (type, v);
2153 return NULL_TREE;
2156 /* Construct a vector of zero elements of vector type TYPE. */
2158 static tree
2159 build_zero_vector (tree type)
2161 tree t;
2163 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2164 return build_vector_from_val (type, t);
2167 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2169 bool
2170 fold_convertible_p (const_tree type, const_tree arg)
2172 tree orig = TREE_TYPE (arg);
2174 if (type == orig)
2175 return true;
2177 if (TREE_CODE (arg) == ERROR_MARK
2178 || TREE_CODE (type) == ERROR_MARK
2179 || TREE_CODE (orig) == ERROR_MARK)
2180 return false;
2182 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2183 return true;
2185 switch (TREE_CODE (type))
2187 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2188 case POINTER_TYPE: case REFERENCE_TYPE:
2189 case OFFSET_TYPE:
2190 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2191 || TREE_CODE (orig) == OFFSET_TYPE);
2193 case REAL_TYPE:
2194 case FIXED_POINT_TYPE:
2195 case VECTOR_TYPE:
2196 case VOID_TYPE:
2197 return TREE_CODE (type) == TREE_CODE (orig);
2199 default:
2200 return false;
2204 /* Convert expression ARG to type TYPE. Used by the middle-end for
2205 simple conversions in preference to calling the front-end's convert. */
2207 tree
2208 fold_convert_loc (location_t loc, tree type, tree arg)
2210 tree orig = TREE_TYPE (arg);
2211 tree tem;
2213 if (type == orig)
2214 return arg;
2216 if (TREE_CODE (arg) == ERROR_MARK
2217 || TREE_CODE (type) == ERROR_MARK
2218 || TREE_CODE (orig) == ERROR_MARK)
2219 return error_mark_node;
2221 switch (TREE_CODE (type))
2223 case POINTER_TYPE:
2224 case REFERENCE_TYPE:
2225 /* Handle conversions between pointers to different address spaces. */
2226 if (POINTER_TYPE_P (orig)
2227 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2228 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2229 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2230 /* fall through */
2232 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2233 case OFFSET_TYPE:
2234 if (TREE_CODE (arg) == INTEGER_CST)
2236 tem = fold_convert_const (NOP_EXPR, type, arg);
2237 if (tem != NULL_TREE)
2238 return tem;
2240 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2241 || TREE_CODE (orig) == OFFSET_TYPE)
2242 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2243 if (TREE_CODE (orig) == COMPLEX_TYPE)
2244 return fold_convert_loc (loc, type,
2245 fold_build1_loc (loc, REALPART_EXPR,
2246 TREE_TYPE (orig), arg));
2247 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2248 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2249 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2251 case REAL_TYPE:
2252 if (TREE_CODE (arg) == INTEGER_CST)
2254 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2255 if (tem != NULL_TREE)
2256 return tem;
2258 else if (TREE_CODE (arg) == REAL_CST)
2260 tem = fold_convert_const (NOP_EXPR, type, arg);
2261 if (tem != NULL_TREE)
2262 return tem;
2264 else if (TREE_CODE (arg) == FIXED_CST)
2266 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2267 if (tem != NULL_TREE)
2268 return tem;
2271 switch (TREE_CODE (orig))
2273 case INTEGER_TYPE:
2274 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2275 case POINTER_TYPE: case REFERENCE_TYPE:
2276 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2278 case REAL_TYPE:
2279 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2281 case FIXED_POINT_TYPE:
2282 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2284 case COMPLEX_TYPE:
2285 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2286 return fold_convert_loc (loc, type, tem);
2288 default:
2289 gcc_unreachable ();
2292 case FIXED_POINT_TYPE:
2293 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2294 || TREE_CODE (arg) == REAL_CST)
2296 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2297 if (tem != NULL_TREE)
2298 goto fold_convert_exit;
2301 switch (TREE_CODE (orig))
2303 case FIXED_POINT_TYPE:
2304 case INTEGER_TYPE:
2305 case ENUMERAL_TYPE:
2306 case BOOLEAN_TYPE:
2307 case REAL_TYPE:
2308 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2310 case COMPLEX_TYPE:
2311 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2312 return fold_convert_loc (loc, type, tem);
2314 default:
2315 gcc_unreachable ();
2318 case COMPLEX_TYPE:
2319 switch (TREE_CODE (orig))
2321 case INTEGER_TYPE:
2322 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2323 case POINTER_TYPE: case REFERENCE_TYPE:
2324 case REAL_TYPE:
2325 case FIXED_POINT_TYPE:
2326 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2327 fold_convert_loc (loc, TREE_TYPE (type), arg),
2328 fold_convert_loc (loc, TREE_TYPE (type),
2329 integer_zero_node));
2330 case COMPLEX_TYPE:
2332 tree rpart, ipart;
2334 if (TREE_CODE (arg) == COMPLEX_EXPR)
2336 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2337 TREE_OPERAND (arg, 0));
2338 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2339 TREE_OPERAND (arg, 1));
2340 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2343 arg = save_expr (arg);
2344 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2345 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2346 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2347 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2348 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2351 default:
2352 gcc_unreachable ();
2355 case VECTOR_TYPE:
2356 if (integer_zerop (arg))
2357 return build_zero_vector (type);
2358 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2359 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2360 || TREE_CODE (orig) == VECTOR_TYPE);
2361 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2363 case VOID_TYPE:
2364 tem = fold_ignored_result (arg);
2365 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2367 default:
2368 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2369 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2370 gcc_unreachable ();
2372 fold_convert_exit:
2373 protected_set_expr_location_unshare (tem, loc);
2374 return tem;
2377 /* Return false if expr can be assumed not to be an lvalue, true
2378 otherwise. */
2380 static bool
2381 maybe_lvalue_p (const_tree x)
2383 /* We only need to wrap lvalue tree codes. */
2384 switch (TREE_CODE (x))
2386 case VAR_DECL:
2387 case PARM_DECL:
2388 case RESULT_DECL:
2389 case LABEL_DECL:
2390 case FUNCTION_DECL:
2391 case SSA_NAME:
2393 case COMPONENT_REF:
2394 case MEM_REF:
2395 case INDIRECT_REF:
2396 case ARRAY_REF:
2397 case ARRAY_RANGE_REF:
2398 case BIT_FIELD_REF:
2399 case OBJ_TYPE_REF:
2401 case REALPART_EXPR:
2402 case IMAGPART_EXPR:
2403 case PREINCREMENT_EXPR:
2404 case PREDECREMENT_EXPR:
2405 case SAVE_EXPR:
2406 case TRY_CATCH_EXPR:
2407 case WITH_CLEANUP_EXPR:
2408 case COMPOUND_EXPR:
2409 case MODIFY_EXPR:
2410 case TARGET_EXPR:
2411 case COND_EXPR:
2412 case BIND_EXPR:
2413 break;
2415 default:
2416 /* Assume the worst for front-end tree codes. */
2417 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2418 break;
2419 return false;
2422 return true;
2425 /* Return an expr equal to X but certainly not valid as an lvalue. */
2427 tree
2428 non_lvalue_loc (location_t loc, tree x)
2430 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2431 us. */
2432 if (in_gimple_form)
2433 return x;
2435 if (! maybe_lvalue_p (x))
2436 return x;
2437 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2440 /* When pedantic, return an expr equal to X but certainly not valid as a
2441 pedantic lvalue. Otherwise, return X. */
2443 static tree
2444 pedantic_non_lvalue_loc (location_t loc, tree x)
2446 return protected_set_expr_location_unshare (x, loc);
2449 /* Given a tree comparison code, return the code that is the logical inverse.
2450 It is generally not safe to do this for floating-point comparisons, except
2451 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2452 ERROR_MARK in this case. */
2454 enum tree_code
2455 invert_tree_comparison (enum tree_code code, bool honor_nans)
2457 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2458 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2459 return ERROR_MARK;
2461 switch (code)
2463 case EQ_EXPR:
2464 return NE_EXPR;
2465 case NE_EXPR:
2466 return EQ_EXPR;
2467 case GT_EXPR:
2468 return honor_nans ? UNLE_EXPR : LE_EXPR;
2469 case GE_EXPR:
2470 return honor_nans ? UNLT_EXPR : LT_EXPR;
2471 case LT_EXPR:
2472 return honor_nans ? UNGE_EXPR : GE_EXPR;
2473 case LE_EXPR:
2474 return honor_nans ? UNGT_EXPR : GT_EXPR;
2475 case LTGT_EXPR:
2476 return UNEQ_EXPR;
2477 case UNEQ_EXPR:
2478 return LTGT_EXPR;
2479 case UNGT_EXPR:
2480 return LE_EXPR;
2481 case UNGE_EXPR:
2482 return LT_EXPR;
2483 case UNLT_EXPR:
2484 return GE_EXPR;
2485 case UNLE_EXPR:
2486 return GT_EXPR;
2487 case ORDERED_EXPR:
2488 return UNORDERED_EXPR;
2489 case UNORDERED_EXPR:
2490 return ORDERED_EXPR;
2491 default:
2492 gcc_unreachable ();
2496 /* Similar, but return the comparison that results if the operands are
2497 swapped. This is safe for floating-point. */
2499 enum tree_code
2500 swap_tree_comparison (enum tree_code code)
2502 switch (code)
2504 case EQ_EXPR:
2505 case NE_EXPR:
2506 case ORDERED_EXPR:
2507 case UNORDERED_EXPR:
2508 case LTGT_EXPR:
2509 case UNEQ_EXPR:
2510 return code;
2511 case GT_EXPR:
2512 return LT_EXPR;
2513 case GE_EXPR:
2514 return LE_EXPR;
2515 case LT_EXPR:
2516 return GT_EXPR;
2517 case LE_EXPR:
2518 return GE_EXPR;
2519 case UNGT_EXPR:
2520 return UNLT_EXPR;
2521 case UNGE_EXPR:
2522 return UNLE_EXPR;
2523 case UNLT_EXPR:
2524 return UNGT_EXPR;
2525 case UNLE_EXPR:
2526 return UNGE_EXPR;
2527 default:
2528 gcc_unreachable ();
2533 /* Convert a comparison tree code from an enum tree_code representation
2534 into a compcode bit-based encoding. This function is the inverse of
2535 compcode_to_comparison. */
2537 static enum comparison_code
2538 comparison_to_compcode (enum tree_code code)
2540 switch (code)
2542 case LT_EXPR:
2543 return COMPCODE_LT;
2544 case EQ_EXPR:
2545 return COMPCODE_EQ;
2546 case LE_EXPR:
2547 return COMPCODE_LE;
2548 case GT_EXPR:
2549 return COMPCODE_GT;
2550 case NE_EXPR:
2551 return COMPCODE_NE;
2552 case GE_EXPR:
2553 return COMPCODE_GE;
2554 case ORDERED_EXPR:
2555 return COMPCODE_ORD;
2556 case UNORDERED_EXPR:
2557 return COMPCODE_UNORD;
2558 case UNLT_EXPR:
2559 return COMPCODE_UNLT;
2560 case UNEQ_EXPR:
2561 return COMPCODE_UNEQ;
2562 case UNLE_EXPR:
2563 return COMPCODE_UNLE;
2564 case UNGT_EXPR:
2565 return COMPCODE_UNGT;
2566 case LTGT_EXPR:
2567 return COMPCODE_LTGT;
2568 case UNGE_EXPR:
2569 return COMPCODE_UNGE;
2570 default:
2571 gcc_unreachable ();
2575 /* Convert a compcode bit-based encoding of a comparison operator back
2576 to GCC's enum tree_code representation. This function is the
2577 inverse of comparison_to_compcode. */
2579 static enum tree_code
2580 compcode_to_comparison (enum comparison_code code)
2582 switch (code)
2584 case COMPCODE_LT:
2585 return LT_EXPR;
2586 case COMPCODE_EQ:
2587 return EQ_EXPR;
2588 case COMPCODE_LE:
2589 return LE_EXPR;
2590 case COMPCODE_GT:
2591 return GT_EXPR;
2592 case COMPCODE_NE:
2593 return NE_EXPR;
2594 case COMPCODE_GE:
2595 return GE_EXPR;
2596 case COMPCODE_ORD:
2597 return ORDERED_EXPR;
2598 case COMPCODE_UNORD:
2599 return UNORDERED_EXPR;
2600 case COMPCODE_UNLT:
2601 return UNLT_EXPR;
2602 case COMPCODE_UNEQ:
2603 return UNEQ_EXPR;
2604 case COMPCODE_UNLE:
2605 return UNLE_EXPR;
2606 case COMPCODE_UNGT:
2607 return UNGT_EXPR;
2608 case COMPCODE_LTGT:
2609 return LTGT_EXPR;
2610 case COMPCODE_UNGE:
2611 return UNGE_EXPR;
2612 default:
2613 gcc_unreachable ();
2617 /* Return a tree for the comparison which is the combination of
2618 doing the AND or OR (depending on CODE) of the two operations LCODE
2619 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2620 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2621 if this makes the transformation invalid. */
2623 tree
2624 combine_comparisons (location_t loc,
2625 enum tree_code code, enum tree_code lcode,
2626 enum tree_code rcode, tree truth_type,
2627 tree ll_arg, tree lr_arg)
2629 bool honor_nans = HONOR_NANS (ll_arg);
2630 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2631 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2632 int compcode;
2634 switch (code)
2636 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2637 compcode = lcompcode & rcompcode;
2638 break;
2640 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2641 compcode = lcompcode | rcompcode;
2642 break;
2644 default:
2645 return NULL_TREE;
2648 if (!honor_nans)
2650 /* Eliminate unordered comparisons, as well as LTGT and ORD
2651 which are not used unless the mode has NaNs. */
2652 compcode &= ~COMPCODE_UNORD;
2653 if (compcode == COMPCODE_LTGT)
2654 compcode = COMPCODE_NE;
2655 else if (compcode == COMPCODE_ORD)
2656 compcode = COMPCODE_TRUE;
2658 else if (flag_trapping_math)
2660 /* Check that the original operation and the optimized ones will trap
2661 under the same condition. */
2662 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2663 && (lcompcode != COMPCODE_EQ)
2664 && (lcompcode != COMPCODE_ORD);
2665 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2666 && (rcompcode != COMPCODE_EQ)
2667 && (rcompcode != COMPCODE_ORD);
2668 bool trap = (compcode & COMPCODE_UNORD) == 0
2669 && (compcode != COMPCODE_EQ)
2670 && (compcode != COMPCODE_ORD);
2672 /* In a short-circuited boolean expression the LHS might be
2673 such that the RHS, if evaluated, will never trap. For
2674 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2675 if neither x nor y is NaN. (This is a mixed blessing: for
2676 example, the expression above will never trap, hence
2677 optimizing it to x < y would be invalid). */
2678 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2679 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2680 rtrap = false;
2682 /* If the comparison was short-circuited, and only the RHS
2683 trapped, we may now generate a spurious trap. */
2684 if (rtrap && !ltrap
2685 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2686 return NULL_TREE;
2688 /* If we changed the conditions that cause a trap, we lose. */
2689 if ((ltrap || rtrap) != trap)
2690 return NULL_TREE;
2693 if (compcode == COMPCODE_TRUE)
2694 return constant_boolean_node (true, truth_type);
2695 else if (compcode == COMPCODE_FALSE)
2696 return constant_boolean_node (false, truth_type);
2697 else
2699 enum tree_code tcode;
2701 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2702 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2706 /* Return nonzero if two operands (typically of the same tree node)
2707 are necessarily equal. FLAGS modifies behavior as follows:
2709 If OEP_ONLY_CONST is set, only return nonzero for constants.
2710 This function tests whether the operands are indistinguishable;
2711 it does not test whether they are equal using C's == operation.
2712 The distinction is important for IEEE floating point, because
2713 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2714 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2716 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2717 even though it may hold multiple values during a function.
2718 This is because a GCC tree node guarantees that nothing else is
2719 executed between the evaluation of its "operands" (which may often
2720 be evaluated in arbitrary order). Hence if the operands themselves
2721 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2722 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2723 unset means assuming isochronic (or instantaneous) tree equivalence.
2724 Unless comparing arbitrary expression trees, such as from different
2725 statements, this flag can usually be left unset.
2727 If OEP_PURE_SAME is set, then pure functions with identical arguments
2728 are considered the same. It is used when the caller has other ways
2729 to ensure that global memory is unchanged in between.
2731 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2732 not values of expressions.
2734 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2735 any operand with side effect. This is unnecesarily conservative in the
2736 case we know that arg0 and arg1 are in disjoint code paths (such as in
2737 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2738 addresses with TREE_CONSTANT flag set so we know that &var == &var
2739 even if var is volatile. */
2742 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2744 /* When checking, verify at the outermost operand_equal_p call that
2745 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2746 hash value. */
2747 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2749 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2751 if (arg0 != arg1)
2753 inchash::hash hstate0 (0), hstate1 (0);
2754 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2755 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2756 hashval_t h0 = hstate0.end ();
2757 hashval_t h1 = hstate1.end ();
2758 gcc_assert (h0 == h1);
2760 return 1;
2762 else
2763 return 0;
2766 /* If either is ERROR_MARK, they aren't equal. */
2767 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2768 || TREE_TYPE (arg0) == error_mark_node
2769 || TREE_TYPE (arg1) == error_mark_node)
2770 return 0;
2772 /* Similar, if either does not have a type (like a released SSA name),
2773 they aren't equal. */
2774 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2775 return 0;
2777 /* We cannot consider pointers to different address space equal. */
2778 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2779 && POINTER_TYPE_P (TREE_TYPE (arg1))
2780 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2781 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2782 return 0;
2784 /* Check equality of integer constants before bailing out due to
2785 precision differences. */
2786 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2788 /* Address of INTEGER_CST is not defined; check that we did not forget
2789 to drop the OEP_ADDRESS_OF flags. */
2790 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2791 return tree_int_cst_equal (arg0, arg1);
2794 if (!(flags & OEP_ADDRESS_OF))
2796 /* If both types don't have the same signedness, then we can't consider
2797 them equal. We must check this before the STRIP_NOPS calls
2798 because they may change the signedness of the arguments. As pointers
2799 strictly don't have a signedness, require either two pointers or
2800 two non-pointers as well. */
2801 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2802 || POINTER_TYPE_P (TREE_TYPE (arg0))
2803 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2804 return 0;
2806 /* If both types don't have the same precision, then it is not safe
2807 to strip NOPs. */
2808 if (element_precision (TREE_TYPE (arg0))
2809 != element_precision (TREE_TYPE (arg1)))
2810 return 0;
2812 STRIP_NOPS (arg0);
2813 STRIP_NOPS (arg1);
2815 #if 0
2816 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2817 sanity check once the issue is solved. */
2818 else
2819 /* Addresses of conversions and SSA_NAMEs (and many other things)
2820 are not defined. Check that we did not forget to drop the
2821 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2822 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2823 && TREE_CODE (arg0) != SSA_NAME);
2824 #endif
2826 /* In case both args are comparisons but with different comparison
2827 code, try to swap the comparison operands of one arg to produce
2828 a match and compare that variant. */
2829 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2830 && COMPARISON_CLASS_P (arg0)
2831 && COMPARISON_CLASS_P (arg1))
2833 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2835 if (TREE_CODE (arg0) == swap_code)
2836 return operand_equal_p (TREE_OPERAND (arg0, 0),
2837 TREE_OPERAND (arg1, 1), flags)
2838 && operand_equal_p (TREE_OPERAND (arg0, 1),
2839 TREE_OPERAND (arg1, 0), flags);
2842 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2844 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2845 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2847 else if (flags & OEP_ADDRESS_OF)
2849 /* If we are interested in comparing addresses ignore
2850 MEM_REF wrappings of the base that can appear just for
2851 TBAA reasons. */
2852 if (TREE_CODE (arg0) == MEM_REF
2853 && DECL_P (arg1)
2854 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2855 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2856 && integer_zerop (TREE_OPERAND (arg0, 1)))
2857 return 1;
2858 else if (TREE_CODE (arg1) == MEM_REF
2859 && DECL_P (arg0)
2860 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2861 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2862 && integer_zerop (TREE_OPERAND (arg1, 1)))
2863 return 1;
2864 return 0;
2866 else
2867 return 0;
2870 /* When not checking adddresses, this is needed for conversions and for
2871 COMPONENT_REF. Might as well play it safe and always test this. */
2872 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2873 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2874 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2875 && !(flags & OEP_ADDRESS_OF)))
2876 return 0;
2878 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2879 We don't care about side effects in that case because the SAVE_EXPR
2880 takes care of that for us. In all other cases, two expressions are
2881 equal if they have no side effects. If we have two identical
2882 expressions with side effects that should be treated the same due
2883 to the only side effects being identical SAVE_EXPR's, that will
2884 be detected in the recursive calls below.
2885 If we are taking an invariant address of two identical objects
2886 they are necessarily equal as well. */
2887 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2888 && (TREE_CODE (arg0) == SAVE_EXPR
2889 || (flags & OEP_MATCH_SIDE_EFFECTS)
2890 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2891 return 1;
2893 /* Next handle constant cases, those for which we can return 1 even
2894 if ONLY_CONST is set. */
2895 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2896 switch (TREE_CODE (arg0))
2898 case INTEGER_CST:
2899 return tree_int_cst_equal (arg0, arg1);
2901 case FIXED_CST:
2902 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2903 TREE_FIXED_CST (arg1));
2905 case REAL_CST:
2906 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2907 return 1;
2910 if (!HONOR_SIGNED_ZEROS (arg0))
2912 /* If we do not distinguish between signed and unsigned zero,
2913 consider them equal. */
2914 if (real_zerop (arg0) && real_zerop (arg1))
2915 return 1;
2917 return 0;
2919 case VECTOR_CST:
2921 unsigned i;
2923 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2924 return 0;
2926 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2928 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2929 VECTOR_CST_ELT (arg1, i), flags))
2930 return 0;
2932 return 1;
2935 case COMPLEX_CST:
2936 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2937 flags)
2938 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2939 flags));
2941 case STRING_CST:
2942 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2943 && ! memcmp (TREE_STRING_POINTER (arg0),
2944 TREE_STRING_POINTER (arg1),
2945 TREE_STRING_LENGTH (arg0)));
2947 case ADDR_EXPR:
2948 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2949 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2950 flags | OEP_ADDRESS_OF
2951 | OEP_MATCH_SIDE_EFFECTS);
2952 case CONSTRUCTOR:
2953 /* In GIMPLE empty constructors are allowed in initializers of
2954 aggregates. */
2955 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0))
2956 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1)));
2957 default:
2958 break;
2961 if (flags & OEP_ONLY_CONST)
2962 return 0;
2964 /* Define macros to test an operand from arg0 and arg1 for equality and a
2965 variant that allows null and views null as being different from any
2966 non-null value. In the latter case, if either is null, the both
2967 must be; otherwise, do the normal comparison. */
2968 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2969 TREE_OPERAND (arg1, N), flags)
2971 #define OP_SAME_WITH_NULL(N) \
2972 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2973 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2975 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2977 case tcc_unary:
2978 /* Two conversions are equal only if signedness and modes match. */
2979 switch (TREE_CODE (arg0))
2981 CASE_CONVERT:
2982 case FIX_TRUNC_EXPR:
2983 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2984 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2985 return 0;
2986 break;
2987 default:
2988 break;
2991 return OP_SAME (0);
2994 case tcc_comparison:
2995 case tcc_binary:
2996 if (OP_SAME (0) && OP_SAME (1))
2997 return 1;
2999 /* For commutative ops, allow the other order. */
3000 return (commutative_tree_code (TREE_CODE (arg0))
3001 && operand_equal_p (TREE_OPERAND (arg0, 0),
3002 TREE_OPERAND (arg1, 1), flags)
3003 && operand_equal_p (TREE_OPERAND (arg0, 1),
3004 TREE_OPERAND (arg1, 0), flags));
3006 case tcc_reference:
3007 /* If either of the pointer (or reference) expressions we are
3008 dereferencing contain a side effect, these cannot be equal,
3009 but their addresses can be. */
3010 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3011 && (TREE_SIDE_EFFECTS (arg0)
3012 || TREE_SIDE_EFFECTS (arg1)))
3013 return 0;
3015 switch (TREE_CODE (arg0))
3017 case INDIRECT_REF:
3018 if (!(flags & OEP_ADDRESS_OF)
3019 && (TYPE_ALIGN (TREE_TYPE (arg0))
3020 != TYPE_ALIGN (TREE_TYPE (arg1))))
3021 return 0;
3022 flags &= ~OEP_ADDRESS_OF;
3023 return OP_SAME (0);
3025 case IMAGPART_EXPR:
3026 /* Require the same offset. */
3027 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3028 TYPE_SIZE (TREE_TYPE (arg1)),
3029 flags & ~OEP_ADDRESS_OF))
3030 return 0;
3032 /* Fallthru. */
3033 case REALPART_EXPR:
3034 case VIEW_CONVERT_EXPR:
3035 return OP_SAME (0);
3037 case TARGET_MEM_REF:
3038 case MEM_REF:
3039 if (!(flags & OEP_ADDRESS_OF))
3041 /* Require equal access sizes */
3042 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3043 && (!TYPE_SIZE (TREE_TYPE (arg0))
3044 || !TYPE_SIZE (TREE_TYPE (arg1))
3045 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3046 TYPE_SIZE (TREE_TYPE (arg1)),
3047 flags)))
3048 return 0;
3049 /* Verify that access happens in similar types. */
3050 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3051 return 0;
3052 /* Verify that accesses are TBAA compatible. */
3053 if (!alias_ptr_types_compatible_p
3054 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3055 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3056 || (MR_DEPENDENCE_CLIQUE (arg0)
3057 != MR_DEPENDENCE_CLIQUE (arg1))
3058 || (MR_DEPENDENCE_BASE (arg0)
3059 != MR_DEPENDENCE_BASE (arg1)))
3060 return 0;
3061 /* Verify that alignment is compatible. */
3062 if (TYPE_ALIGN (TREE_TYPE (arg0))
3063 != TYPE_ALIGN (TREE_TYPE (arg1)))
3064 return 0;
3066 flags &= ~OEP_ADDRESS_OF;
3067 return (OP_SAME (0) && OP_SAME (1)
3068 /* TARGET_MEM_REF require equal extra operands. */
3069 && (TREE_CODE (arg0) != TARGET_MEM_REF
3070 || (OP_SAME_WITH_NULL (2)
3071 && OP_SAME_WITH_NULL (3)
3072 && OP_SAME_WITH_NULL (4))));
3074 case ARRAY_REF:
3075 case ARRAY_RANGE_REF:
3076 if (!OP_SAME (0))
3077 return 0;
3078 flags &= ~OEP_ADDRESS_OF;
3079 /* Compare the array index by value if it is constant first as we
3080 may have different types but same value here. */
3081 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3082 TREE_OPERAND (arg1, 1))
3083 || OP_SAME (1))
3084 && OP_SAME_WITH_NULL (2)
3085 && OP_SAME_WITH_NULL (3)
3086 /* Compare low bound and element size as with OEP_ADDRESS_OF
3087 we have to account for the offset of the ref. */
3088 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3089 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3090 || (operand_equal_p (array_ref_low_bound
3091 (CONST_CAST_TREE (arg0)),
3092 array_ref_low_bound
3093 (CONST_CAST_TREE (arg1)), flags)
3094 && operand_equal_p (array_ref_element_size
3095 (CONST_CAST_TREE (arg0)),
3096 array_ref_element_size
3097 (CONST_CAST_TREE (arg1)),
3098 flags))));
3100 case COMPONENT_REF:
3101 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3102 may be NULL when we're called to compare MEM_EXPRs. */
3103 if (!OP_SAME_WITH_NULL (0)
3104 || !OP_SAME (1))
3105 return 0;
3106 flags &= ~OEP_ADDRESS_OF;
3107 return OP_SAME_WITH_NULL (2);
3109 case BIT_FIELD_REF:
3110 if (!OP_SAME (0))
3111 return 0;
3112 flags &= ~OEP_ADDRESS_OF;
3113 return OP_SAME (1) && OP_SAME (2);
3115 default:
3116 return 0;
3119 case tcc_expression:
3120 switch (TREE_CODE (arg0))
3122 case ADDR_EXPR:
3123 /* Be sure we pass right ADDRESS_OF flag. */
3124 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3125 return operand_equal_p (TREE_OPERAND (arg0, 0),
3126 TREE_OPERAND (arg1, 0),
3127 flags | OEP_ADDRESS_OF);
3129 case TRUTH_NOT_EXPR:
3130 return OP_SAME (0);
3132 case TRUTH_ANDIF_EXPR:
3133 case TRUTH_ORIF_EXPR:
3134 return OP_SAME (0) && OP_SAME (1);
3136 case FMA_EXPR:
3137 case WIDEN_MULT_PLUS_EXPR:
3138 case WIDEN_MULT_MINUS_EXPR:
3139 if (!OP_SAME (2))
3140 return 0;
3141 /* The multiplcation operands are commutative. */
3142 /* FALLTHRU */
3144 case TRUTH_AND_EXPR:
3145 case TRUTH_OR_EXPR:
3146 case TRUTH_XOR_EXPR:
3147 if (OP_SAME (0) && OP_SAME (1))
3148 return 1;
3150 /* Otherwise take into account this is a commutative operation. */
3151 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3152 TREE_OPERAND (arg1, 1), flags)
3153 && operand_equal_p (TREE_OPERAND (arg0, 1),
3154 TREE_OPERAND (arg1, 0), flags));
3156 case COND_EXPR:
3157 if (! OP_SAME (1) || ! OP_SAME (2))
3158 return 0;
3159 flags &= ~OEP_ADDRESS_OF;
3160 return OP_SAME (0);
3162 case VEC_COND_EXPR:
3163 case DOT_PROD_EXPR:
3164 case BIT_INSERT_EXPR:
3165 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3167 default:
3168 return 0;
3171 case tcc_vl_exp:
3172 switch (TREE_CODE (arg0))
3174 case CALL_EXPR:
3175 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3176 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3177 /* If not both CALL_EXPRs are either internal or normal function
3178 functions, then they are not equal. */
3179 return 0;
3180 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3182 /* If the CALL_EXPRs call different internal functions, then they
3183 are not equal. */
3184 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3185 return 0;
3187 else
3189 /* If the CALL_EXPRs call different functions, then they are not
3190 equal. */
3191 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3192 flags))
3193 return 0;
3196 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3198 unsigned int cef = call_expr_flags (arg0);
3199 if (flags & OEP_PURE_SAME)
3200 cef &= ECF_CONST | ECF_PURE;
3201 else
3202 cef &= ECF_CONST;
3203 if (!cef)
3204 return 0;
3207 /* Now see if all the arguments are the same. */
3209 const_call_expr_arg_iterator iter0, iter1;
3210 const_tree a0, a1;
3211 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3212 a1 = first_const_call_expr_arg (arg1, &iter1);
3213 a0 && a1;
3214 a0 = next_const_call_expr_arg (&iter0),
3215 a1 = next_const_call_expr_arg (&iter1))
3216 if (! operand_equal_p (a0, a1, flags))
3217 return 0;
3219 /* If we get here and both argument lists are exhausted
3220 then the CALL_EXPRs are equal. */
3221 return ! (a0 || a1);
3223 default:
3224 return 0;
3227 case tcc_declaration:
3228 /* Consider __builtin_sqrt equal to sqrt. */
3229 return (TREE_CODE (arg0) == FUNCTION_DECL
3230 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3231 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3232 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3234 case tcc_exceptional:
3235 if (TREE_CODE (arg0) == CONSTRUCTOR)
3237 /* In GIMPLE constructors are used only to build vectors from
3238 elements. Individual elements in the constructor must be
3239 indexed in increasing order and form an initial sequence.
3241 We make no effort to compare constructors in generic.
3242 (see sem_variable::equals in ipa-icf which can do so for
3243 constants). */
3244 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3245 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3246 return 0;
3248 /* Be sure that vectors constructed have the same representation.
3249 We only tested element precision and modes to match.
3250 Vectors may be BLKmode and thus also check that the number of
3251 parts match. */
3252 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3253 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3254 return 0;
3256 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3257 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3258 unsigned int len = vec_safe_length (v0);
3260 if (len != vec_safe_length (v1))
3261 return 0;
3263 for (unsigned int i = 0; i < len; i++)
3265 constructor_elt *c0 = &(*v0)[i];
3266 constructor_elt *c1 = &(*v1)[i];
3268 if (!operand_equal_p (c0->value, c1->value, flags)
3269 /* In GIMPLE the indexes can be either NULL or matching i.
3270 Double check this so we won't get false
3271 positives for GENERIC. */
3272 || (c0->index
3273 && (TREE_CODE (c0->index) != INTEGER_CST
3274 || !compare_tree_int (c0->index, i)))
3275 || (c1->index
3276 && (TREE_CODE (c1->index) != INTEGER_CST
3277 || !compare_tree_int (c1->index, i))))
3278 return 0;
3280 return 1;
3282 return 0;
3284 default:
3285 return 0;
3288 #undef OP_SAME
3289 #undef OP_SAME_WITH_NULL
3292 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3293 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3295 When in doubt, return 0. */
3297 static int
3298 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3300 int unsignedp1, unsignedpo;
3301 tree primarg0, primarg1, primother;
3302 unsigned int correct_width;
3304 if (operand_equal_p (arg0, arg1, 0))
3305 return 1;
3307 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3308 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3309 return 0;
3311 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3312 and see if the inner values are the same. This removes any
3313 signedness comparison, which doesn't matter here. */
3314 primarg0 = arg0, primarg1 = arg1;
3315 STRIP_NOPS (primarg0);
3316 STRIP_NOPS (primarg1);
3317 if (operand_equal_p (primarg0, primarg1, 0))
3318 return 1;
3320 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3321 actual comparison operand, ARG0.
3323 First throw away any conversions to wider types
3324 already present in the operands. */
3326 primarg1 = get_narrower (arg1, &unsignedp1);
3327 primother = get_narrower (other, &unsignedpo);
3329 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3330 if (unsignedp1 == unsignedpo
3331 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3332 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3334 tree type = TREE_TYPE (arg0);
3336 /* Make sure shorter operand is extended the right way
3337 to match the longer operand. */
3338 primarg1 = fold_convert (signed_or_unsigned_type_for
3339 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3341 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3342 return 1;
3345 return 0;
3348 /* See if ARG is an expression that is either a comparison or is performing
3349 arithmetic on comparisons. The comparisons must only be comparing
3350 two different values, which will be stored in *CVAL1 and *CVAL2; if
3351 they are nonzero it means that some operands have already been found.
3352 No variables may be used anywhere else in the expression except in the
3353 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3354 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3356 If this is true, return 1. Otherwise, return zero. */
3358 static int
3359 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3361 enum tree_code code = TREE_CODE (arg);
3362 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3364 /* We can handle some of the tcc_expression cases here. */
3365 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3366 tclass = tcc_unary;
3367 else if (tclass == tcc_expression
3368 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3369 || code == COMPOUND_EXPR))
3370 tclass = tcc_binary;
3372 else if (tclass == tcc_expression && code == SAVE_EXPR
3373 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3375 /* If we've already found a CVAL1 or CVAL2, this expression is
3376 two complex to handle. */
3377 if (*cval1 || *cval2)
3378 return 0;
3380 tclass = tcc_unary;
3381 *save_p = 1;
3384 switch (tclass)
3386 case tcc_unary:
3387 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3389 case tcc_binary:
3390 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3391 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3392 cval1, cval2, save_p));
3394 case tcc_constant:
3395 return 1;
3397 case tcc_expression:
3398 if (code == COND_EXPR)
3399 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3400 cval1, cval2, save_p)
3401 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3402 cval1, cval2, save_p)
3403 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3404 cval1, cval2, save_p));
3405 return 0;
3407 case tcc_comparison:
3408 /* First see if we can handle the first operand, then the second. For
3409 the second operand, we know *CVAL1 can't be zero. It must be that
3410 one side of the comparison is each of the values; test for the
3411 case where this isn't true by failing if the two operands
3412 are the same. */
3414 if (operand_equal_p (TREE_OPERAND (arg, 0),
3415 TREE_OPERAND (arg, 1), 0))
3416 return 0;
3418 if (*cval1 == 0)
3419 *cval1 = TREE_OPERAND (arg, 0);
3420 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3422 else if (*cval2 == 0)
3423 *cval2 = TREE_OPERAND (arg, 0);
3424 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3426 else
3427 return 0;
3429 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3431 else if (*cval2 == 0)
3432 *cval2 = TREE_OPERAND (arg, 1);
3433 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3435 else
3436 return 0;
3438 return 1;
3440 default:
3441 return 0;
3445 /* ARG is a tree that is known to contain just arithmetic operations and
3446 comparisons. Evaluate the operations in the tree substituting NEW0 for
3447 any occurrence of OLD0 as an operand of a comparison and likewise for
3448 NEW1 and OLD1. */
3450 static tree
3451 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3452 tree old1, tree new1)
3454 tree type = TREE_TYPE (arg);
3455 enum tree_code code = TREE_CODE (arg);
3456 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3458 /* We can handle some of the tcc_expression cases here. */
3459 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3460 tclass = tcc_unary;
3461 else if (tclass == tcc_expression
3462 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3463 tclass = tcc_binary;
3465 switch (tclass)
3467 case tcc_unary:
3468 return fold_build1_loc (loc, code, type,
3469 eval_subst (loc, TREE_OPERAND (arg, 0),
3470 old0, new0, old1, new1));
3472 case tcc_binary:
3473 return fold_build2_loc (loc, code, type,
3474 eval_subst (loc, TREE_OPERAND (arg, 0),
3475 old0, new0, old1, new1),
3476 eval_subst (loc, TREE_OPERAND (arg, 1),
3477 old0, new0, old1, new1));
3479 case tcc_expression:
3480 switch (code)
3482 case SAVE_EXPR:
3483 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3484 old1, new1);
3486 case COMPOUND_EXPR:
3487 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3488 old1, new1);
3490 case COND_EXPR:
3491 return fold_build3_loc (loc, code, type,
3492 eval_subst (loc, TREE_OPERAND (arg, 0),
3493 old0, new0, old1, new1),
3494 eval_subst (loc, TREE_OPERAND (arg, 1),
3495 old0, new0, old1, new1),
3496 eval_subst (loc, TREE_OPERAND (arg, 2),
3497 old0, new0, old1, new1));
3498 default:
3499 break;
3501 /* Fall through - ??? */
3503 case tcc_comparison:
3505 tree arg0 = TREE_OPERAND (arg, 0);
3506 tree arg1 = TREE_OPERAND (arg, 1);
3508 /* We need to check both for exact equality and tree equality. The
3509 former will be true if the operand has a side-effect. In that
3510 case, we know the operand occurred exactly once. */
3512 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3513 arg0 = new0;
3514 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3515 arg0 = new1;
3517 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3518 arg1 = new0;
3519 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3520 arg1 = new1;
3522 return fold_build2_loc (loc, code, type, arg0, arg1);
3525 default:
3526 return arg;
3530 /* Return a tree for the case when the result of an expression is RESULT
3531 converted to TYPE and OMITTED was previously an operand of the expression
3532 but is now not needed (e.g., we folded OMITTED * 0).
3534 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3535 the conversion of RESULT to TYPE. */
3537 tree
3538 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3540 tree t = fold_convert_loc (loc, type, result);
3542 /* If the resulting operand is an empty statement, just return the omitted
3543 statement casted to void. */
3544 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3545 return build1_loc (loc, NOP_EXPR, void_type_node,
3546 fold_ignored_result (omitted));
3548 if (TREE_SIDE_EFFECTS (omitted))
3549 return build2_loc (loc, COMPOUND_EXPR, type,
3550 fold_ignored_result (omitted), t);
3552 return non_lvalue_loc (loc, t);
3555 /* Return a tree for the case when the result of an expression is RESULT
3556 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3557 of the expression but are now not needed.
3559 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3560 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3561 evaluated before OMITTED2. Otherwise, if neither has side effects,
3562 just do the conversion of RESULT to TYPE. */
3564 tree
3565 omit_two_operands_loc (location_t loc, tree type, tree result,
3566 tree omitted1, tree omitted2)
3568 tree t = fold_convert_loc (loc, type, result);
3570 if (TREE_SIDE_EFFECTS (omitted2))
3571 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3572 if (TREE_SIDE_EFFECTS (omitted1))
3573 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3575 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3579 /* Return a simplified tree node for the truth-negation of ARG. This
3580 never alters ARG itself. We assume that ARG is an operation that
3581 returns a truth value (0 or 1).
3583 FIXME: one would think we would fold the result, but it causes
3584 problems with the dominator optimizer. */
3586 static tree
3587 fold_truth_not_expr (location_t loc, tree arg)
3589 tree type = TREE_TYPE (arg);
3590 enum tree_code code = TREE_CODE (arg);
3591 location_t loc1, loc2;
3593 /* If this is a comparison, we can simply invert it, except for
3594 floating-point non-equality comparisons, in which case we just
3595 enclose a TRUTH_NOT_EXPR around what we have. */
3597 if (TREE_CODE_CLASS (code) == tcc_comparison)
3599 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3600 if (FLOAT_TYPE_P (op_type)
3601 && flag_trapping_math
3602 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3603 && code != NE_EXPR && code != EQ_EXPR)
3604 return NULL_TREE;
3606 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3607 if (code == ERROR_MARK)
3608 return NULL_TREE;
3610 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3611 TREE_OPERAND (arg, 1));
3612 if (TREE_NO_WARNING (arg))
3613 TREE_NO_WARNING (ret) = 1;
3614 return ret;
3617 switch (code)
3619 case INTEGER_CST:
3620 return constant_boolean_node (integer_zerop (arg), type);
3622 case TRUTH_AND_EXPR:
3623 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3624 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3625 return build2_loc (loc, TRUTH_OR_EXPR, type,
3626 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3627 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3629 case TRUTH_OR_EXPR:
3630 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3631 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3632 return build2_loc (loc, TRUTH_AND_EXPR, type,
3633 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3634 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3636 case TRUTH_XOR_EXPR:
3637 /* Here we can invert either operand. We invert the first operand
3638 unless the second operand is a TRUTH_NOT_EXPR in which case our
3639 result is the XOR of the first operand with the inside of the
3640 negation of the second operand. */
3642 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3643 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3644 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3645 else
3646 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3647 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3648 TREE_OPERAND (arg, 1));
3650 case TRUTH_ANDIF_EXPR:
3651 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3652 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3653 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3654 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3655 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3657 case TRUTH_ORIF_EXPR:
3658 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3659 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3660 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3661 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3662 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3664 case TRUTH_NOT_EXPR:
3665 return TREE_OPERAND (arg, 0);
3667 case COND_EXPR:
3669 tree arg1 = TREE_OPERAND (arg, 1);
3670 tree arg2 = TREE_OPERAND (arg, 2);
3672 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3673 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3675 /* A COND_EXPR may have a throw as one operand, which
3676 then has void type. Just leave void operands
3677 as they are. */
3678 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3679 VOID_TYPE_P (TREE_TYPE (arg1))
3680 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3681 VOID_TYPE_P (TREE_TYPE (arg2))
3682 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3685 case COMPOUND_EXPR:
3686 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3687 return build2_loc (loc, COMPOUND_EXPR, type,
3688 TREE_OPERAND (arg, 0),
3689 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3691 case NON_LVALUE_EXPR:
3692 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3693 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3695 CASE_CONVERT:
3696 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3697 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3699 /* ... fall through ... */
3701 case FLOAT_EXPR:
3702 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3703 return build1_loc (loc, TREE_CODE (arg), type,
3704 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3706 case BIT_AND_EXPR:
3707 if (!integer_onep (TREE_OPERAND (arg, 1)))
3708 return NULL_TREE;
3709 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3711 case SAVE_EXPR:
3712 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3714 case CLEANUP_POINT_EXPR:
3715 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3716 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3717 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3719 default:
3720 return NULL_TREE;
3724 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3725 assume that ARG is an operation that returns a truth value (0 or 1
3726 for scalars, 0 or -1 for vectors). Return the folded expression if
3727 folding is successful. Otherwise, return NULL_TREE. */
3729 static tree
3730 fold_invert_truthvalue (location_t loc, tree arg)
3732 tree type = TREE_TYPE (arg);
3733 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3734 ? BIT_NOT_EXPR
3735 : TRUTH_NOT_EXPR,
3736 type, arg);
3739 /* Return a simplified tree node for the truth-negation of ARG. This
3740 never alters ARG itself. We assume that ARG is an operation that
3741 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3743 tree
3744 invert_truthvalue_loc (location_t loc, tree arg)
3746 if (TREE_CODE (arg) == ERROR_MARK)
3747 return arg;
3749 tree type = TREE_TYPE (arg);
3750 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3751 ? BIT_NOT_EXPR
3752 : TRUTH_NOT_EXPR,
3753 type, arg);
3756 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3757 with code CODE. This optimization is unsafe. */
3758 static tree
3759 distribute_real_division (location_t loc, enum tree_code code, tree type,
3760 tree arg0, tree arg1)
3762 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3763 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3765 /* (A / C) +- (B / C) -> (A +- B) / C. */
3766 if (mul0 == mul1
3767 && operand_equal_p (TREE_OPERAND (arg0, 1),
3768 TREE_OPERAND (arg1, 1), 0))
3769 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3770 fold_build2_loc (loc, code, type,
3771 TREE_OPERAND (arg0, 0),
3772 TREE_OPERAND (arg1, 0)),
3773 TREE_OPERAND (arg0, 1));
3775 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3776 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3777 TREE_OPERAND (arg1, 0), 0)
3778 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3779 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3781 REAL_VALUE_TYPE r0, r1;
3782 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3783 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3784 if (!mul0)
3785 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3786 if (!mul1)
3787 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3788 real_arithmetic (&r0, code, &r0, &r1);
3789 return fold_build2_loc (loc, MULT_EXPR, type,
3790 TREE_OPERAND (arg0, 0),
3791 build_real (type, r0));
3794 return NULL_TREE;
3797 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3798 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3799 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3800 is the original memory reference used to preserve the alias set of
3801 the access. */
3803 static tree
3804 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3805 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3806 int unsignedp, int reversep)
3808 tree result, bftype;
3810 if (get_alias_set (inner) != get_alias_set (orig_inner))
3811 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3812 build_fold_addr_expr (inner),
3813 build_int_cst
3814 (reference_alias_ptr_type (orig_inner), 0));
3816 if (bitpos == 0 && !reversep)
3818 tree size = TYPE_SIZE (TREE_TYPE (inner));
3819 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3820 || POINTER_TYPE_P (TREE_TYPE (inner)))
3821 && tree_fits_shwi_p (size)
3822 && tree_to_shwi (size) == bitsize)
3823 return fold_convert_loc (loc, type, inner);
3826 bftype = type;
3827 if (TYPE_PRECISION (bftype) != bitsize
3828 || TYPE_UNSIGNED (bftype) == !unsignedp)
3829 bftype = build_nonstandard_integer_type (bitsize, 0);
3831 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3832 size_int (bitsize), bitsize_int (bitpos));
3833 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3835 if (bftype != type)
3836 result = fold_convert_loc (loc, type, result);
3838 return result;
3841 /* Optimize a bit-field compare.
3843 There are two cases: First is a compare against a constant and the
3844 second is a comparison of two items where the fields are at the same
3845 bit position relative to the start of a chunk (byte, halfword, word)
3846 large enough to contain it. In these cases we can avoid the shift
3847 implicit in bitfield extractions.
3849 For constants, we emit a compare of the shifted constant with the
3850 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3851 compared. For two fields at the same position, we do the ANDs with the
3852 similar mask and compare the result of the ANDs.
3854 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3855 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3856 are the left and right operands of the comparison, respectively.
3858 If the optimization described above can be done, we return the resulting
3859 tree. Otherwise we return zero. */
3861 static tree
3862 optimize_bit_field_compare (location_t loc, enum tree_code code,
3863 tree compare_type, tree lhs, tree rhs)
3865 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3866 tree type = TREE_TYPE (lhs);
3867 tree unsigned_type;
3868 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3869 machine_mode lmode, rmode, nmode;
3870 int lunsignedp, runsignedp;
3871 int lreversep, rreversep;
3872 int lvolatilep = 0, rvolatilep = 0;
3873 tree linner, rinner = NULL_TREE;
3874 tree mask;
3875 tree offset;
3877 /* Get all the information about the extractions being done. If the bit size
3878 if the same as the size of the underlying object, we aren't doing an
3879 extraction at all and so can do nothing. We also don't want to
3880 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3881 then will no longer be able to replace it. */
3882 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3883 &lunsignedp, &lreversep, &lvolatilep);
3884 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3885 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3886 return 0;
3888 if (const_p)
3889 rreversep = lreversep;
3890 else
3892 /* If this is not a constant, we can only do something if bit positions,
3893 sizes, signedness and storage order are the same. */
3894 rinner
3895 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3896 &runsignedp, &rreversep, &rvolatilep);
3898 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3899 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3900 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3901 return 0;
3904 /* Honor the C++ memory model and mimic what RTL expansion does. */
3905 unsigned HOST_WIDE_INT bitstart = 0;
3906 unsigned HOST_WIDE_INT bitend = 0;
3907 if (TREE_CODE (lhs) == COMPONENT_REF)
3909 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3910 if (offset != NULL_TREE)
3911 return 0;
3914 /* See if we can find a mode to refer to this field. We should be able to,
3915 but fail if we can't. */
3916 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3917 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3918 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3919 TYPE_ALIGN (TREE_TYPE (rinner))),
3920 word_mode, false);
3921 if (nmode == VOIDmode)
3922 return 0;
3924 /* Set signed and unsigned types of the precision of this mode for the
3925 shifts below. */
3926 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3928 /* Compute the bit position and size for the new reference and our offset
3929 within it. If the new reference is the same size as the original, we
3930 won't optimize anything, so return zero. */
3931 nbitsize = GET_MODE_BITSIZE (nmode);
3932 nbitpos = lbitpos & ~ (nbitsize - 1);
3933 lbitpos -= nbitpos;
3934 if (nbitsize == lbitsize)
3935 return 0;
3937 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3938 lbitpos = nbitsize - lbitsize - lbitpos;
3940 /* Make the mask to be used against the extracted field. */
3941 mask = build_int_cst_type (unsigned_type, -1);
3942 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3943 mask = const_binop (RSHIFT_EXPR, mask,
3944 size_int (nbitsize - lbitsize - lbitpos));
3946 if (! const_p)
3947 /* If not comparing with constant, just rework the comparison
3948 and return. */
3949 return fold_build2_loc (loc, code, compare_type,
3950 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3951 make_bit_field_ref (loc, linner, lhs,
3952 unsigned_type,
3953 nbitsize, nbitpos,
3954 1, lreversep),
3955 mask),
3956 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3957 make_bit_field_ref (loc, rinner, rhs,
3958 unsigned_type,
3959 nbitsize, nbitpos,
3960 1, rreversep),
3961 mask));
3963 /* Otherwise, we are handling the constant case. See if the constant is too
3964 big for the field. Warn and return a tree for 0 (false) if so. We do
3965 this not only for its own sake, but to avoid having to test for this
3966 error case below. If we didn't, we might generate wrong code.
3968 For unsigned fields, the constant shifted right by the field length should
3969 be all zero. For signed fields, the high-order bits should agree with
3970 the sign bit. */
3972 if (lunsignedp)
3974 if (wi::lrshift (rhs, lbitsize) != 0)
3976 warning (0, "comparison is always %d due to width of bit-field",
3977 code == NE_EXPR);
3978 return constant_boolean_node (code == NE_EXPR, compare_type);
3981 else
3983 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3984 if (tem != 0 && tem != -1)
3986 warning (0, "comparison is always %d due to width of bit-field",
3987 code == NE_EXPR);
3988 return constant_boolean_node (code == NE_EXPR, compare_type);
3992 /* Single-bit compares should always be against zero. */
3993 if (lbitsize == 1 && ! integer_zerop (rhs))
3995 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3996 rhs = build_int_cst (type, 0);
3999 /* Make a new bitfield reference, shift the constant over the
4000 appropriate number of bits and mask it with the computed mask
4001 (in case this was a signed field). If we changed it, make a new one. */
4002 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4003 nbitsize, nbitpos, 1, lreversep);
4005 rhs = const_binop (BIT_AND_EXPR,
4006 const_binop (LSHIFT_EXPR,
4007 fold_convert_loc (loc, unsigned_type, rhs),
4008 size_int (lbitpos)),
4009 mask);
4011 lhs = build2_loc (loc, code, compare_type,
4012 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4013 return lhs;
4016 /* Subroutine for fold_truth_andor_1: decode a field reference.
4018 If EXP is a comparison reference, we return the innermost reference.
4020 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4021 set to the starting bit number.
4023 If the innermost field can be completely contained in a mode-sized
4024 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4026 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4027 otherwise it is not changed.
4029 *PUNSIGNEDP is set to the signedness of the field.
4031 *PREVERSEP is set to the storage order of the field.
4033 *PMASK is set to the mask used. This is either contained in a
4034 BIT_AND_EXPR or derived from the width of the field.
4036 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4038 Return 0 if this is not a component reference or is one that we can't
4039 do anything with. */
4041 static tree
4042 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4043 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4044 int *punsignedp, int *preversep, int *pvolatilep,
4045 tree *pmask, tree *pand_mask)
4047 tree exp = *exp_;
4048 tree outer_type = 0;
4049 tree and_mask = 0;
4050 tree mask, inner, offset;
4051 tree unsigned_type;
4052 unsigned int precision;
4054 /* All the optimizations using this function assume integer fields.
4055 There are problems with FP fields since the type_for_size call
4056 below can fail for, e.g., XFmode. */
4057 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4058 return 0;
4060 /* We are interested in the bare arrangement of bits, so strip everything
4061 that doesn't affect the machine mode. However, record the type of the
4062 outermost expression if it may matter below. */
4063 if (CONVERT_EXPR_P (exp)
4064 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4065 outer_type = TREE_TYPE (exp);
4066 STRIP_NOPS (exp);
4068 if (TREE_CODE (exp) == BIT_AND_EXPR)
4070 and_mask = TREE_OPERAND (exp, 1);
4071 exp = TREE_OPERAND (exp, 0);
4072 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4073 if (TREE_CODE (and_mask) != INTEGER_CST)
4074 return 0;
4077 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4078 punsignedp, preversep, pvolatilep);
4079 if ((inner == exp && and_mask == 0)
4080 || *pbitsize < 0 || offset != 0
4081 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4082 return 0;
4084 *exp_ = exp;
4086 /* If the number of bits in the reference is the same as the bitsize of
4087 the outer type, then the outer type gives the signedness. Otherwise
4088 (in case of a small bitfield) the signedness is unchanged. */
4089 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4090 *punsignedp = TYPE_UNSIGNED (outer_type);
4092 /* Compute the mask to access the bitfield. */
4093 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4094 precision = TYPE_PRECISION (unsigned_type);
4096 mask = build_int_cst_type (unsigned_type, -1);
4098 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4099 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4101 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4102 if (and_mask != 0)
4103 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4104 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4106 *pmask = mask;
4107 *pand_mask = and_mask;
4108 return inner;
4111 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4112 bit positions and MASK is SIGNED. */
4114 static int
4115 all_ones_mask_p (const_tree mask, unsigned int size)
4117 tree type = TREE_TYPE (mask);
4118 unsigned int precision = TYPE_PRECISION (type);
4120 /* If this function returns true when the type of the mask is
4121 UNSIGNED, then there will be errors. In particular see
4122 gcc.c-torture/execute/990326-1.c. There does not appear to be
4123 any documentation paper trail as to why this is so. But the pre
4124 wide-int worked with that restriction and it has been preserved
4125 here. */
4126 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4127 return false;
4129 return wi::mask (size, false, precision) == mask;
4132 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4133 represents the sign bit of EXP's type. If EXP represents a sign
4134 or zero extension, also test VAL against the unextended type.
4135 The return value is the (sub)expression whose sign bit is VAL,
4136 or NULL_TREE otherwise. */
4138 tree
4139 sign_bit_p (tree exp, const_tree val)
4141 int width;
4142 tree t;
4144 /* Tree EXP must have an integral type. */
4145 t = TREE_TYPE (exp);
4146 if (! INTEGRAL_TYPE_P (t))
4147 return NULL_TREE;
4149 /* Tree VAL must be an integer constant. */
4150 if (TREE_CODE (val) != INTEGER_CST
4151 || TREE_OVERFLOW (val))
4152 return NULL_TREE;
4154 width = TYPE_PRECISION (t);
4155 if (wi::only_sign_bit_p (val, width))
4156 return exp;
4158 /* Handle extension from a narrower type. */
4159 if (TREE_CODE (exp) == NOP_EXPR
4160 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4161 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4163 return NULL_TREE;
4166 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4167 to be evaluated unconditionally. */
4169 static int
4170 simple_operand_p (const_tree exp)
4172 /* Strip any conversions that don't change the machine mode. */
4173 STRIP_NOPS (exp);
4175 return (CONSTANT_CLASS_P (exp)
4176 || TREE_CODE (exp) == SSA_NAME
4177 || (DECL_P (exp)
4178 && ! TREE_ADDRESSABLE (exp)
4179 && ! TREE_THIS_VOLATILE (exp)
4180 && ! DECL_NONLOCAL (exp)
4181 /* Don't regard global variables as simple. They may be
4182 allocated in ways unknown to the compiler (shared memory,
4183 #pragma weak, etc). */
4184 && ! TREE_PUBLIC (exp)
4185 && ! DECL_EXTERNAL (exp)
4186 /* Weakrefs are not safe to be read, since they can be NULL.
4187 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4188 have DECL_WEAK flag set. */
4189 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4190 /* Loading a static variable is unduly expensive, but global
4191 registers aren't expensive. */
4192 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4195 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4196 to be evaluated unconditionally.
4197 I addition to simple_operand_p, we assume that comparisons, conversions,
4198 and logic-not operations are simple, if their operands are simple, too. */
4200 static bool
4201 simple_operand_p_2 (tree exp)
4203 enum tree_code code;
4205 if (TREE_SIDE_EFFECTS (exp)
4206 || tree_could_trap_p (exp))
4207 return false;
4209 while (CONVERT_EXPR_P (exp))
4210 exp = TREE_OPERAND (exp, 0);
4212 code = TREE_CODE (exp);
4214 if (TREE_CODE_CLASS (code) == tcc_comparison)
4215 return (simple_operand_p (TREE_OPERAND (exp, 0))
4216 && simple_operand_p (TREE_OPERAND (exp, 1)));
4218 if (code == TRUTH_NOT_EXPR)
4219 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4221 return simple_operand_p (exp);
4225 /* The following functions are subroutines to fold_range_test and allow it to
4226 try to change a logical combination of comparisons into a range test.
4228 For example, both
4229 X == 2 || X == 3 || X == 4 || X == 5
4231 X >= 2 && X <= 5
4232 are converted to
4233 (unsigned) (X - 2) <= 3
4235 We describe each set of comparisons as being either inside or outside
4236 a range, using a variable named like IN_P, and then describe the
4237 range with a lower and upper bound. If one of the bounds is omitted,
4238 it represents either the highest or lowest value of the type.
4240 In the comments below, we represent a range by two numbers in brackets
4241 preceded by a "+" to designate being inside that range, or a "-" to
4242 designate being outside that range, so the condition can be inverted by
4243 flipping the prefix. An omitted bound is represented by a "-". For
4244 example, "- [-, 10]" means being outside the range starting at the lowest
4245 possible value and ending at 10, in other words, being greater than 10.
4246 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4247 always false.
4249 We set up things so that the missing bounds are handled in a consistent
4250 manner so neither a missing bound nor "true" and "false" need to be
4251 handled using a special case. */
4253 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4254 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4255 and UPPER1_P are nonzero if the respective argument is an upper bound
4256 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4257 must be specified for a comparison. ARG1 will be converted to ARG0's
4258 type if both are specified. */
4260 static tree
4261 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4262 tree arg1, int upper1_p)
4264 tree tem;
4265 int result;
4266 int sgn0, sgn1;
4268 /* If neither arg represents infinity, do the normal operation.
4269 Else, if not a comparison, return infinity. Else handle the special
4270 comparison rules. Note that most of the cases below won't occur, but
4271 are handled for consistency. */
4273 if (arg0 != 0 && arg1 != 0)
4275 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4276 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4277 STRIP_NOPS (tem);
4278 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4281 if (TREE_CODE_CLASS (code) != tcc_comparison)
4282 return 0;
4284 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4285 for neither. In real maths, we cannot assume open ended ranges are
4286 the same. But, this is computer arithmetic, where numbers are finite.
4287 We can therefore make the transformation of any unbounded range with
4288 the value Z, Z being greater than any representable number. This permits
4289 us to treat unbounded ranges as equal. */
4290 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4291 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4292 switch (code)
4294 case EQ_EXPR:
4295 result = sgn0 == sgn1;
4296 break;
4297 case NE_EXPR:
4298 result = sgn0 != sgn1;
4299 break;
4300 case LT_EXPR:
4301 result = sgn0 < sgn1;
4302 break;
4303 case LE_EXPR:
4304 result = sgn0 <= sgn1;
4305 break;
4306 case GT_EXPR:
4307 result = sgn0 > sgn1;
4308 break;
4309 case GE_EXPR:
4310 result = sgn0 >= sgn1;
4311 break;
4312 default:
4313 gcc_unreachable ();
4316 return constant_boolean_node (result, type);
4319 /* Helper routine for make_range. Perform one step for it, return
4320 new expression if the loop should continue or NULL_TREE if it should
4321 stop. */
4323 tree
4324 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4325 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4326 bool *strict_overflow_p)
4328 tree arg0_type = TREE_TYPE (arg0);
4329 tree n_low, n_high, low = *p_low, high = *p_high;
4330 int in_p = *p_in_p, n_in_p;
4332 switch (code)
4334 case TRUTH_NOT_EXPR:
4335 /* We can only do something if the range is testing for zero. */
4336 if (low == NULL_TREE || high == NULL_TREE
4337 || ! integer_zerop (low) || ! integer_zerop (high))
4338 return NULL_TREE;
4339 *p_in_p = ! in_p;
4340 return arg0;
4342 case EQ_EXPR: case NE_EXPR:
4343 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4344 /* We can only do something if the range is testing for zero
4345 and if the second operand is an integer constant. Note that
4346 saying something is "in" the range we make is done by
4347 complementing IN_P since it will set in the initial case of
4348 being not equal to zero; "out" is leaving it alone. */
4349 if (low == NULL_TREE || high == NULL_TREE
4350 || ! integer_zerop (low) || ! integer_zerop (high)
4351 || TREE_CODE (arg1) != INTEGER_CST)
4352 return NULL_TREE;
4354 switch (code)
4356 case NE_EXPR: /* - [c, c] */
4357 low = high = arg1;
4358 break;
4359 case EQ_EXPR: /* + [c, c] */
4360 in_p = ! in_p, low = high = arg1;
4361 break;
4362 case GT_EXPR: /* - [-, c] */
4363 low = 0, high = arg1;
4364 break;
4365 case GE_EXPR: /* + [c, -] */
4366 in_p = ! in_p, low = arg1, high = 0;
4367 break;
4368 case LT_EXPR: /* - [c, -] */
4369 low = arg1, high = 0;
4370 break;
4371 case LE_EXPR: /* + [-, c] */
4372 in_p = ! in_p, low = 0, high = arg1;
4373 break;
4374 default:
4375 gcc_unreachable ();
4378 /* If this is an unsigned comparison, we also know that EXP is
4379 greater than or equal to zero. We base the range tests we make
4380 on that fact, so we record it here so we can parse existing
4381 range tests. We test arg0_type since often the return type
4382 of, e.g. EQ_EXPR, is boolean. */
4383 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4385 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4386 in_p, low, high, 1,
4387 build_int_cst (arg0_type, 0),
4388 NULL_TREE))
4389 return NULL_TREE;
4391 in_p = n_in_p, low = n_low, high = n_high;
4393 /* If the high bound is missing, but we have a nonzero low
4394 bound, reverse the range so it goes from zero to the low bound
4395 minus 1. */
4396 if (high == 0 && low && ! integer_zerop (low))
4398 in_p = ! in_p;
4399 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4400 build_int_cst (TREE_TYPE (low), 1), 0);
4401 low = build_int_cst (arg0_type, 0);
4405 *p_low = low;
4406 *p_high = high;
4407 *p_in_p = in_p;
4408 return arg0;
4410 case NEGATE_EXPR:
4411 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4412 low and high are non-NULL, then normalize will DTRT. */
4413 if (!TYPE_UNSIGNED (arg0_type)
4414 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4416 if (low == NULL_TREE)
4417 low = TYPE_MIN_VALUE (arg0_type);
4418 if (high == NULL_TREE)
4419 high = TYPE_MAX_VALUE (arg0_type);
4422 /* (-x) IN [a,b] -> x in [-b, -a] */
4423 n_low = range_binop (MINUS_EXPR, exp_type,
4424 build_int_cst (exp_type, 0),
4425 0, high, 1);
4426 n_high = range_binop (MINUS_EXPR, exp_type,
4427 build_int_cst (exp_type, 0),
4428 0, low, 0);
4429 if (n_high != 0 && TREE_OVERFLOW (n_high))
4430 return NULL_TREE;
4431 goto normalize;
4433 case BIT_NOT_EXPR:
4434 /* ~ X -> -X - 1 */
4435 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4436 build_int_cst (exp_type, 1));
4438 case PLUS_EXPR:
4439 case MINUS_EXPR:
4440 if (TREE_CODE (arg1) != INTEGER_CST)
4441 return NULL_TREE;
4443 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4444 move a constant to the other side. */
4445 if (!TYPE_UNSIGNED (arg0_type)
4446 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4447 return NULL_TREE;
4449 /* If EXP is signed, any overflow in the computation is undefined,
4450 so we don't worry about it so long as our computations on
4451 the bounds don't overflow. For unsigned, overflow is defined
4452 and this is exactly the right thing. */
4453 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4454 arg0_type, low, 0, arg1, 0);
4455 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4456 arg0_type, high, 1, arg1, 0);
4457 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4458 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4459 return NULL_TREE;
4461 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4462 *strict_overflow_p = true;
4464 normalize:
4465 /* Check for an unsigned range which has wrapped around the maximum
4466 value thus making n_high < n_low, and normalize it. */
4467 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4469 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4470 build_int_cst (TREE_TYPE (n_high), 1), 0);
4471 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4472 build_int_cst (TREE_TYPE (n_low), 1), 0);
4474 /* If the range is of the form +/- [ x+1, x ], we won't
4475 be able to normalize it. But then, it represents the
4476 whole range or the empty set, so make it
4477 +/- [ -, - ]. */
4478 if (tree_int_cst_equal (n_low, low)
4479 && tree_int_cst_equal (n_high, high))
4480 low = high = 0;
4481 else
4482 in_p = ! in_p;
4484 else
4485 low = n_low, high = n_high;
4487 *p_low = low;
4488 *p_high = high;
4489 *p_in_p = in_p;
4490 return arg0;
4492 CASE_CONVERT:
4493 case NON_LVALUE_EXPR:
4494 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4495 return NULL_TREE;
4497 if (! INTEGRAL_TYPE_P (arg0_type)
4498 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4499 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4500 return NULL_TREE;
4502 n_low = low, n_high = high;
4504 if (n_low != 0)
4505 n_low = fold_convert_loc (loc, arg0_type, n_low);
4507 if (n_high != 0)
4508 n_high = fold_convert_loc (loc, arg0_type, n_high);
4510 /* If we're converting arg0 from an unsigned type, to exp,
4511 a signed type, we will be doing the comparison as unsigned.
4512 The tests above have already verified that LOW and HIGH
4513 are both positive.
4515 So we have to ensure that we will handle large unsigned
4516 values the same way that the current signed bounds treat
4517 negative values. */
4519 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4521 tree high_positive;
4522 tree equiv_type;
4523 /* For fixed-point modes, we need to pass the saturating flag
4524 as the 2nd parameter. */
4525 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4526 equiv_type
4527 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4528 TYPE_SATURATING (arg0_type));
4529 else
4530 equiv_type
4531 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4533 /* A range without an upper bound is, naturally, unbounded.
4534 Since convert would have cropped a very large value, use
4535 the max value for the destination type. */
4536 high_positive
4537 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4538 : TYPE_MAX_VALUE (arg0_type);
4540 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4541 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4542 fold_convert_loc (loc, arg0_type,
4543 high_positive),
4544 build_int_cst (arg0_type, 1));
4546 /* If the low bound is specified, "and" the range with the
4547 range for which the original unsigned value will be
4548 positive. */
4549 if (low != 0)
4551 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4552 1, fold_convert_loc (loc, arg0_type,
4553 integer_zero_node),
4554 high_positive))
4555 return NULL_TREE;
4557 in_p = (n_in_p == in_p);
4559 else
4561 /* Otherwise, "or" the range with the range of the input
4562 that will be interpreted as negative. */
4563 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4564 1, fold_convert_loc (loc, arg0_type,
4565 integer_zero_node),
4566 high_positive))
4567 return NULL_TREE;
4569 in_p = (in_p != n_in_p);
4573 *p_low = n_low;
4574 *p_high = n_high;
4575 *p_in_p = in_p;
4576 return arg0;
4578 default:
4579 return NULL_TREE;
4583 /* Given EXP, a logical expression, set the range it is testing into
4584 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4585 actually being tested. *PLOW and *PHIGH will be made of the same
4586 type as the returned expression. If EXP is not a comparison, we
4587 will most likely not be returning a useful value and range. Set
4588 *STRICT_OVERFLOW_P to true if the return value is only valid
4589 because signed overflow is undefined; otherwise, do not change
4590 *STRICT_OVERFLOW_P. */
4592 tree
4593 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4594 bool *strict_overflow_p)
4596 enum tree_code code;
4597 tree arg0, arg1 = NULL_TREE;
4598 tree exp_type, nexp;
4599 int in_p;
4600 tree low, high;
4601 location_t loc = EXPR_LOCATION (exp);
4603 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4604 and see if we can refine the range. Some of the cases below may not
4605 happen, but it doesn't seem worth worrying about this. We "continue"
4606 the outer loop when we've changed something; otherwise we "break"
4607 the switch, which will "break" the while. */
4609 in_p = 0;
4610 low = high = build_int_cst (TREE_TYPE (exp), 0);
4612 while (1)
4614 code = TREE_CODE (exp);
4615 exp_type = TREE_TYPE (exp);
4616 arg0 = NULL_TREE;
4618 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4620 if (TREE_OPERAND_LENGTH (exp) > 0)
4621 arg0 = TREE_OPERAND (exp, 0);
4622 if (TREE_CODE_CLASS (code) == tcc_binary
4623 || TREE_CODE_CLASS (code) == tcc_comparison
4624 || (TREE_CODE_CLASS (code) == tcc_expression
4625 && TREE_OPERAND_LENGTH (exp) > 1))
4626 arg1 = TREE_OPERAND (exp, 1);
4628 if (arg0 == NULL_TREE)
4629 break;
4631 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4632 &high, &in_p, strict_overflow_p);
4633 if (nexp == NULL_TREE)
4634 break;
4635 exp = nexp;
4638 /* If EXP is a constant, we can evaluate whether this is true or false. */
4639 if (TREE_CODE (exp) == INTEGER_CST)
4641 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4642 exp, 0, low, 0))
4643 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4644 exp, 1, high, 1)));
4645 low = high = 0;
4646 exp = 0;
4649 *pin_p = in_p, *plow = low, *phigh = high;
4650 return exp;
4653 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4654 type, TYPE, return an expression to test if EXP is in (or out of, depending
4655 on IN_P) the range. Return 0 if the test couldn't be created. */
4657 tree
4658 build_range_check (location_t loc, tree type, tree exp, int in_p,
4659 tree low, tree high)
4661 tree etype = TREE_TYPE (exp), value;
4663 /* Disable this optimization for function pointer expressions
4664 on targets that require function pointer canonicalization. */
4665 if (targetm.have_canonicalize_funcptr_for_compare ()
4666 && TREE_CODE (etype) == POINTER_TYPE
4667 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4668 return NULL_TREE;
4670 if (! in_p)
4672 value = build_range_check (loc, type, exp, 1, low, high);
4673 if (value != 0)
4674 return invert_truthvalue_loc (loc, value);
4676 return 0;
4679 if (low == 0 && high == 0)
4680 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4682 if (low == 0)
4683 return fold_build2_loc (loc, LE_EXPR, type, exp,
4684 fold_convert_loc (loc, etype, high));
4686 if (high == 0)
4687 return fold_build2_loc (loc, GE_EXPR, type, exp,
4688 fold_convert_loc (loc, etype, low));
4690 if (operand_equal_p (low, high, 0))
4691 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4692 fold_convert_loc (loc, etype, low));
4694 if (integer_zerop (low))
4696 if (! TYPE_UNSIGNED (etype))
4698 etype = unsigned_type_for (etype);
4699 high = fold_convert_loc (loc, etype, high);
4700 exp = fold_convert_loc (loc, etype, exp);
4702 return build_range_check (loc, type, exp, 1, 0, high);
4705 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4706 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4708 int prec = TYPE_PRECISION (etype);
4710 if (wi::mask (prec - 1, false, prec) == high)
4712 if (TYPE_UNSIGNED (etype))
4714 tree signed_etype = signed_type_for (etype);
4715 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4716 etype
4717 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4718 else
4719 etype = signed_etype;
4720 exp = fold_convert_loc (loc, etype, exp);
4722 return fold_build2_loc (loc, GT_EXPR, type, exp,
4723 build_int_cst (etype, 0));
4727 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4728 This requires wrap-around arithmetics for the type of the expression.
4729 First make sure that arithmetics in this type is valid, then make sure
4730 that it wraps around. */
4731 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4732 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4733 TYPE_UNSIGNED (etype));
4735 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4737 tree utype, minv, maxv;
4739 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4740 for the type in question, as we rely on this here. */
4741 utype = unsigned_type_for (etype);
4742 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4743 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4744 build_int_cst (TREE_TYPE (maxv), 1), 1);
4745 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4747 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4748 minv, 1, maxv, 1)))
4749 etype = utype;
4750 else
4751 return 0;
4754 high = fold_convert_loc (loc, etype, high);
4755 low = fold_convert_loc (loc, etype, low);
4756 exp = fold_convert_loc (loc, etype, exp);
4758 value = const_binop (MINUS_EXPR, high, low);
4761 if (POINTER_TYPE_P (etype))
4763 if (value != 0 && !TREE_OVERFLOW (value))
4765 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4766 return build_range_check (loc, type,
4767 fold_build_pointer_plus_loc (loc, exp, low),
4768 1, build_int_cst (etype, 0), value);
4770 return 0;
4773 if (value != 0 && !TREE_OVERFLOW (value))
4774 return build_range_check (loc, type,
4775 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4776 1, build_int_cst (etype, 0), value);
4778 return 0;
4781 /* Return the predecessor of VAL in its type, handling the infinite case. */
4783 static tree
4784 range_predecessor (tree val)
4786 tree type = TREE_TYPE (val);
4788 if (INTEGRAL_TYPE_P (type)
4789 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4790 return 0;
4791 else
4792 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4793 build_int_cst (TREE_TYPE (val), 1), 0);
4796 /* Return the successor of VAL in its type, handling the infinite case. */
4798 static tree
4799 range_successor (tree val)
4801 tree type = TREE_TYPE (val);
4803 if (INTEGRAL_TYPE_P (type)
4804 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4805 return 0;
4806 else
4807 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4808 build_int_cst (TREE_TYPE (val), 1), 0);
4811 /* Given two ranges, see if we can merge them into one. Return 1 if we
4812 can, 0 if we can't. Set the output range into the specified parameters. */
4814 bool
4815 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4816 tree high0, int in1_p, tree low1, tree high1)
4818 int no_overlap;
4819 int subset;
4820 int temp;
4821 tree tem;
4822 int in_p;
4823 tree low, high;
4824 int lowequal = ((low0 == 0 && low1 == 0)
4825 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4826 low0, 0, low1, 0)));
4827 int highequal = ((high0 == 0 && high1 == 0)
4828 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4829 high0, 1, high1, 1)));
4831 /* Make range 0 be the range that starts first, or ends last if they
4832 start at the same value. Swap them if it isn't. */
4833 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4834 low0, 0, low1, 0))
4835 || (lowequal
4836 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4837 high1, 1, high0, 1))))
4839 temp = in0_p, in0_p = in1_p, in1_p = temp;
4840 tem = low0, low0 = low1, low1 = tem;
4841 tem = high0, high0 = high1, high1 = tem;
4844 /* Now flag two cases, whether the ranges are disjoint or whether the
4845 second range is totally subsumed in the first. Note that the tests
4846 below are simplified by the ones above. */
4847 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4848 high0, 1, low1, 0));
4849 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4850 high1, 1, high0, 1));
4852 /* We now have four cases, depending on whether we are including or
4853 excluding the two ranges. */
4854 if (in0_p && in1_p)
4856 /* If they don't overlap, the result is false. If the second range
4857 is a subset it is the result. Otherwise, the range is from the start
4858 of the second to the end of the first. */
4859 if (no_overlap)
4860 in_p = 0, low = high = 0;
4861 else if (subset)
4862 in_p = 1, low = low1, high = high1;
4863 else
4864 in_p = 1, low = low1, high = high0;
4867 else if (in0_p && ! in1_p)
4869 /* If they don't overlap, the result is the first range. If they are
4870 equal, the result is false. If the second range is a subset of the
4871 first, and the ranges begin at the same place, we go from just after
4872 the end of the second range to the end of the first. If the second
4873 range is not a subset of the first, or if it is a subset and both
4874 ranges end at the same place, the range starts at the start of the
4875 first range and ends just before the second range.
4876 Otherwise, we can't describe this as a single range. */
4877 if (no_overlap)
4878 in_p = 1, low = low0, high = high0;
4879 else if (lowequal && highequal)
4880 in_p = 0, low = high = 0;
4881 else if (subset && lowequal)
4883 low = range_successor (high1);
4884 high = high0;
4885 in_p = 1;
4886 if (low == 0)
4888 /* We are in the weird situation where high0 > high1 but
4889 high1 has no successor. Punt. */
4890 return 0;
4893 else if (! subset || highequal)
4895 low = low0;
4896 high = range_predecessor (low1);
4897 in_p = 1;
4898 if (high == 0)
4900 /* low0 < low1 but low1 has no predecessor. Punt. */
4901 return 0;
4904 else
4905 return 0;
4908 else if (! in0_p && in1_p)
4910 /* If they don't overlap, the result is the second range. If the second
4911 is a subset of the first, the result is false. Otherwise,
4912 the range starts just after the first range and ends at the
4913 end of the second. */
4914 if (no_overlap)
4915 in_p = 1, low = low1, high = high1;
4916 else if (subset || highequal)
4917 in_p = 0, low = high = 0;
4918 else
4920 low = range_successor (high0);
4921 high = high1;
4922 in_p = 1;
4923 if (low == 0)
4925 /* high1 > high0 but high0 has no successor. Punt. */
4926 return 0;
4931 else
4933 /* The case where we are excluding both ranges. Here the complex case
4934 is if they don't overlap. In that case, the only time we have a
4935 range is if they are adjacent. If the second is a subset of the
4936 first, the result is the first. Otherwise, the range to exclude
4937 starts at the beginning of the first range and ends at the end of the
4938 second. */
4939 if (no_overlap)
4941 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4942 range_successor (high0),
4943 1, low1, 0)))
4944 in_p = 0, low = low0, high = high1;
4945 else
4947 /* Canonicalize - [min, x] into - [-, x]. */
4948 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4949 switch (TREE_CODE (TREE_TYPE (low0)))
4951 case ENUMERAL_TYPE:
4952 if (TYPE_PRECISION (TREE_TYPE (low0))
4953 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4954 break;
4955 /* FALLTHROUGH */
4956 case INTEGER_TYPE:
4957 if (tree_int_cst_equal (low0,
4958 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4959 low0 = 0;
4960 break;
4961 case POINTER_TYPE:
4962 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4963 && integer_zerop (low0))
4964 low0 = 0;
4965 break;
4966 default:
4967 break;
4970 /* Canonicalize - [x, max] into - [x, -]. */
4971 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4972 switch (TREE_CODE (TREE_TYPE (high1)))
4974 case ENUMERAL_TYPE:
4975 if (TYPE_PRECISION (TREE_TYPE (high1))
4976 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4977 break;
4978 /* FALLTHROUGH */
4979 case INTEGER_TYPE:
4980 if (tree_int_cst_equal (high1,
4981 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4982 high1 = 0;
4983 break;
4984 case POINTER_TYPE:
4985 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4986 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4987 high1, 1,
4988 build_int_cst (TREE_TYPE (high1), 1),
4989 1)))
4990 high1 = 0;
4991 break;
4992 default:
4993 break;
4996 /* The ranges might be also adjacent between the maximum and
4997 minimum values of the given type. For
4998 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4999 return + [x + 1, y - 1]. */
5000 if (low0 == 0 && high1 == 0)
5002 low = range_successor (high0);
5003 high = range_predecessor (low1);
5004 if (low == 0 || high == 0)
5005 return 0;
5007 in_p = 1;
5009 else
5010 return 0;
5013 else if (subset)
5014 in_p = 0, low = low0, high = high0;
5015 else
5016 in_p = 0, low = low0, high = high1;
5019 *pin_p = in_p, *plow = low, *phigh = high;
5020 return 1;
5024 /* Subroutine of fold, looking inside expressions of the form
5025 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5026 of the COND_EXPR. This function is being used also to optimize
5027 A op B ? C : A, by reversing the comparison first.
5029 Return a folded expression whose code is not a COND_EXPR
5030 anymore, or NULL_TREE if no folding opportunity is found. */
5032 static tree
5033 fold_cond_expr_with_comparison (location_t loc, tree type,
5034 tree arg0, tree arg1, tree arg2)
5036 enum tree_code comp_code = TREE_CODE (arg0);
5037 tree arg00 = TREE_OPERAND (arg0, 0);
5038 tree arg01 = TREE_OPERAND (arg0, 1);
5039 tree arg1_type = TREE_TYPE (arg1);
5040 tree tem;
5042 STRIP_NOPS (arg1);
5043 STRIP_NOPS (arg2);
5045 /* If we have A op 0 ? A : -A, consider applying the following
5046 transformations:
5048 A == 0? A : -A same as -A
5049 A != 0? A : -A same as A
5050 A >= 0? A : -A same as abs (A)
5051 A > 0? A : -A same as abs (A)
5052 A <= 0? A : -A same as -abs (A)
5053 A < 0? A : -A same as -abs (A)
5055 None of these transformations work for modes with signed
5056 zeros. If A is +/-0, the first two transformations will
5057 change the sign of the result (from +0 to -0, or vice
5058 versa). The last four will fix the sign of the result,
5059 even though the original expressions could be positive or
5060 negative, depending on the sign of A.
5062 Note that all these transformations are correct if A is
5063 NaN, since the two alternatives (A and -A) are also NaNs. */
5064 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5065 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5066 ? real_zerop (arg01)
5067 : integer_zerop (arg01))
5068 && ((TREE_CODE (arg2) == NEGATE_EXPR
5069 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5070 /* In the case that A is of the form X-Y, '-A' (arg2) may
5071 have already been folded to Y-X, check for that. */
5072 || (TREE_CODE (arg1) == MINUS_EXPR
5073 && TREE_CODE (arg2) == MINUS_EXPR
5074 && operand_equal_p (TREE_OPERAND (arg1, 0),
5075 TREE_OPERAND (arg2, 1), 0)
5076 && operand_equal_p (TREE_OPERAND (arg1, 1),
5077 TREE_OPERAND (arg2, 0), 0))))
5078 switch (comp_code)
5080 case EQ_EXPR:
5081 case UNEQ_EXPR:
5082 tem = fold_convert_loc (loc, arg1_type, arg1);
5083 return pedantic_non_lvalue_loc (loc,
5084 fold_convert_loc (loc, type,
5085 negate_expr (tem)));
5086 case NE_EXPR:
5087 case LTGT_EXPR:
5088 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5089 case UNGE_EXPR:
5090 case UNGT_EXPR:
5091 if (flag_trapping_math)
5092 break;
5093 /* Fall through. */
5094 case GE_EXPR:
5095 case GT_EXPR:
5096 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5097 break;
5098 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5099 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5100 case UNLE_EXPR:
5101 case UNLT_EXPR:
5102 if (flag_trapping_math)
5103 break;
5104 case LE_EXPR:
5105 case LT_EXPR:
5106 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5107 break;
5108 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5109 return negate_expr (fold_convert_loc (loc, type, tem));
5110 default:
5111 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5112 break;
5115 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5116 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5117 both transformations are correct when A is NaN: A != 0
5118 is then true, and A == 0 is false. */
5120 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5121 && integer_zerop (arg01) && integer_zerop (arg2))
5123 if (comp_code == NE_EXPR)
5124 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5125 else if (comp_code == EQ_EXPR)
5126 return build_zero_cst (type);
5129 /* Try some transformations of A op B ? A : B.
5131 A == B? A : B same as B
5132 A != B? A : B same as A
5133 A >= B? A : B same as max (A, B)
5134 A > B? A : B same as max (B, A)
5135 A <= B? A : B same as min (A, B)
5136 A < B? A : B same as min (B, A)
5138 As above, these transformations don't work in the presence
5139 of signed zeros. For example, if A and B are zeros of
5140 opposite sign, the first two transformations will change
5141 the sign of the result. In the last four, the original
5142 expressions give different results for (A=+0, B=-0) and
5143 (A=-0, B=+0), but the transformed expressions do not.
5145 The first two transformations are correct if either A or B
5146 is a NaN. In the first transformation, the condition will
5147 be false, and B will indeed be chosen. In the case of the
5148 second transformation, the condition A != B will be true,
5149 and A will be chosen.
5151 The conversions to max() and min() are not correct if B is
5152 a number and A is not. The conditions in the original
5153 expressions will be false, so all four give B. The min()
5154 and max() versions would give a NaN instead. */
5155 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5156 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5157 /* Avoid these transformations if the COND_EXPR may be used
5158 as an lvalue in the C++ front-end. PR c++/19199. */
5159 && (in_gimple_form
5160 || VECTOR_TYPE_P (type)
5161 || (! lang_GNU_CXX ()
5162 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5163 || ! maybe_lvalue_p (arg1)
5164 || ! maybe_lvalue_p (arg2)))
5166 tree comp_op0 = arg00;
5167 tree comp_op1 = arg01;
5168 tree comp_type = TREE_TYPE (comp_op0);
5170 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5171 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5173 comp_type = type;
5174 comp_op0 = arg1;
5175 comp_op1 = arg2;
5178 switch (comp_code)
5180 case EQ_EXPR:
5181 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5182 case NE_EXPR:
5183 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5184 case LE_EXPR:
5185 case LT_EXPR:
5186 case UNLE_EXPR:
5187 case UNLT_EXPR:
5188 /* In C++ a ?: expression can be an lvalue, so put the
5189 operand which will be used if they are equal first
5190 so that we can convert this back to the
5191 corresponding COND_EXPR. */
5192 if (!HONOR_NANS (arg1))
5194 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5195 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5196 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5197 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5198 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5199 comp_op1, comp_op0);
5200 return pedantic_non_lvalue_loc (loc,
5201 fold_convert_loc (loc, type, tem));
5203 break;
5204 case GE_EXPR:
5205 case GT_EXPR:
5206 case UNGE_EXPR:
5207 case UNGT_EXPR:
5208 if (!HONOR_NANS (arg1))
5210 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5211 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5212 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5213 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5214 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5215 comp_op1, comp_op0);
5216 return pedantic_non_lvalue_loc (loc,
5217 fold_convert_loc (loc, type, tem));
5219 break;
5220 case UNEQ_EXPR:
5221 if (!HONOR_NANS (arg1))
5222 return pedantic_non_lvalue_loc (loc,
5223 fold_convert_loc (loc, type, arg2));
5224 break;
5225 case LTGT_EXPR:
5226 if (!HONOR_NANS (arg1))
5227 return pedantic_non_lvalue_loc (loc,
5228 fold_convert_loc (loc, type, arg1));
5229 break;
5230 default:
5231 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5232 break;
5236 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5237 we might still be able to simplify this. For example,
5238 if C1 is one less or one more than C2, this might have started
5239 out as a MIN or MAX and been transformed by this function.
5240 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5242 if (INTEGRAL_TYPE_P (type)
5243 && TREE_CODE (arg01) == INTEGER_CST
5244 && TREE_CODE (arg2) == INTEGER_CST)
5245 switch (comp_code)
5247 case EQ_EXPR:
5248 if (TREE_CODE (arg1) == INTEGER_CST)
5249 break;
5250 /* We can replace A with C1 in this case. */
5251 arg1 = fold_convert_loc (loc, type, arg01);
5252 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5254 case LT_EXPR:
5255 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5256 MIN_EXPR, to preserve the signedness of the comparison. */
5257 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5258 OEP_ONLY_CONST)
5259 && operand_equal_p (arg01,
5260 const_binop (PLUS_EXPR, arg2,
5261 build_int_cst (type, 1)),
5262 OEP_ONLY_CONST))
5264 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5265 fold_convert_loc (loc, TREE_TYPE (arg00),
5266 arg2));
5267 return pedantic_non_lvalue_loc (loc,
5268 fold_convert_loc (loc, type, tem));
5270 break;
5272 case LE_EXPR:
5273 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5274 as above. */
5275 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5276 OEP_ONLY_CONST)
5277 && operand_equal_p (arg01,
5278 const_binop (MINUS_EXPR, arg2,
5279 build_int_cst (type, 1)),
5280 OEP_ONLY_CONST))
5282 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5283 fold_convert_loc (loc, TREE_TYPE (arg00),
5284 arg2));
5285 return pedantic_non_lvalue_loc (loc,
5286 fold_convert_loc (loc, type, tem));
5288 break;
5290 case GT_EXPR:
5291 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5292 MAX_EXPR, to preserve the signedness of the comparison. */
5293 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5294 OEP_ONLY_CONST)
5295 && operand_equal_p (arg01,
5296 const_binop (MINUS_EXPR, arg2,
5297 build_int_cst (type, 1)),
5298 OEP_ONLY_CONST))
5300 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5301 fold_convert_loc (loc, TREE_TYPE (arg00),
5302 arg2));
5303 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5305 break;
5307 case GE_EXPR:
5308 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5309 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5310 OEP_ONLY_CONST)
5311 && operand_equal_p (arg01,
5312 const_binop (PLUS_EXPR, arg2,
5313 build_int_cst (type, 1)),
5314 OEP_ONLY_CONST))
5316 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5317 fold_convert_loc (loc, TREE_TYPE (arg00),
5318 arg2));
5319 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5321 break;
5322 case NE_EXPR:
5323 break;
5324 default:
5325 gcc_unreachable ();
5328 return NULL_TREE;
5333 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5334 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5335 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5336 false) >= 2)
5337 #endif
5339 /* EXP is some logical combination of boolean tests. See if we can
5340 merge it into some range test. Return the new tree if so. */
5342 static tree
5343 fold_range_test (location_t loc, enum tree_code code, tree type,
5344 tree op0, tree op1)
5346 int or_op = (code == TRUTH_ORIF_EXPR
5347 || code == TRUTH_OR_EXPR);
5348 int in0_p, in1_p, in_p;
5349 tree low0, low1, low, high0, high1, high;
5350 bool strict_overflow_p = false;
5351 tree tem, lhs, rhs;
5352 const char * const warnmsg = G_("assuming signed overflow does not occur "
5353 "when simplifying range test");
5355 if (!INTEGRAL_TYPE_P (type))
5356 return 0;
5358 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5359 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5361 /* If this is an OR operation, invert both sides; we will invert
5362 again at the end. */
5363 if (or_op)
5364 in0_p = ! in0_p, in1_p = ! in1_p;
5366 /* If both expressions are the same, if we can merge the ranges, and we
5367 can build the range test, return it or it inverted. If one of the
5368 ranges is always true or always false, consider it to be the same
5369 expression as the other. */
5370 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5371 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5372 in1_p, low1, high1)
5373 && 0 != (tem = (build_range_check (loc, type,
5374 lhs != 0 ? lhs
5375 : rhs != 0 ? rhs : integer_zero_node,
5376 in_p, low, high))))
5378 if (strict_overflow_p)
5379 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5380 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5383 /* On machines where the branch cost is expensive, if this is a
5384 short-circuited branch and the underlying object on both sides
5385 is the same, make a non-short-circuit operation. */
5386 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5387 && lhs != 0 && rhs != 0
5388 && (code == TRUTH_ANDIF_EXPR
5389 || code == TRUTH_ORIF_EXPR)
5390 && operand_equal_p (lhs, rhs, 0))
5392 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5393 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5394 which cases we can't do this. */
5395 if (simple_operand_p (lhs))
5396 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5397 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5398 type, op0, op1);
5400 else if (!lang_hooks.decls.global_bindings_p ()
5401 && !CONTAINS_PLACEHOLDER_P (lhs))
5403 tree common = save_expr (lhs);
5405 if (0 != (lhs = build_range_check (loc, type, common,
5406 or_op ? ! in0_p : in0_p,
5407 low0, high0))
5408 && (0 != (rhs = build_range_check (loc, type, common,
5409 or_op ? ! in1_p : in1_p,
5410 low1, high1))))
5412 if (strict_overflow_p)
5413 fold_overflow_warning (warnmsg,
5414 WARN_STRICT_OVERFLOW_COMPARISON);
5415 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5416 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5417 type, lhs, rhs);
5422 return 0;
5425 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5426 bit value. Arrange things so the extra bits will be set to zero if and
5427 only if C is signed-extended to its full width. If MASK is nonzero,
5428 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5430 static tree
5431 unextend (tree c, int p, int unsignedp, tree mask)
5433 tree type = TREE_TYPE (c);
5434 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5435 tree temp;
5437 if (p == modesize || unsignedp)
5438 return c;
5440 /* We work by getting just the sign bit into the low-order bit, then
5441 into the high-order bit, then sign-extend. We then XOR that value
5442 with C. */
5443 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5445 /* We must use a signed type in order to get an arithmetic right shift.
5446 However, we must also avoid introducing accidental overflows, so that
5447 a subsequent call to integer_zerop will work. Hence we must
5448 do the type conversion here. At this point, the constant is either
5449 zero or one, and the conversion to a signed type can never overflow.
5450 We could get an overflow if this conversion is done anywhere else. */
5451 if (TYPE_UNSIGNED (type))
5452 temp = fold_convert (signed_type_for (type), temp);
5454 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5455 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5456 if (mask != 0)
5457 temp = const_binop (BIT_AND_EXPR, temp,
5458 fold_convert (TREE_TYPE (c), mask));
5459 /* If necessary, convert the type back to match the type of C. */
5460 if (TYPE_UNSIGNED (type))
5461 temp = fold_convert (type, temp);
5463 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5466 /* For an expression that has the form
5467 (A && B) || ~B
5469 (A || B) && ~B,
5470 we can drop one of the inner expressions and simplify to
5471 A || ~B
5473 A && ~B
5474 LOC is the location of the resulting expression. OP is the inner
5475 logical operation; the left-hand side in the examples above, while CMPOP
5476 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5477 removing a condition that guards another, as in
5478 (A != NULL && A->...) || A == NULL
5479 which we must not transform. If RHS_ONLY is true, only eliminate the
5480 right-most operand of the inner logical operation. */
5482 static tree
5483 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5484 bool rhs_only)
5486 tree type = TREE_TYPE (cmpop);
5487 enum tree_code code = TREE_CODE (cmpop);
5488 enum tree_code truthop_code = TREE_CODE (op);
5489 tree lhs = TREE_OPERAND (op, 0);
5490 tree rhs = TREE_OPERAND (op, 1);
5491 tree orig_lhs = lhs, orig_rhs = rhs;
5492 enum tree_code rhs_code = TREE_CODE (rhs);
5493 enum tree_code lhs_code = TREE_CODE (lhs);
5494 enum tree_code inv_code;
5496 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5497 return NULL_TREE;
5499 if (TREE_CODE_CLASS (code) != tcc_comparison)
5500 return NULL_TREE;
5502 if (rhs_code == truthop_code)
5504 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5505 if (newrhs != NULL_TREE)
5507 rhs = newrhs;
5508 rhs_code = TREE_CODE (rhs);
5511 if (lhs_code == truthop_code && !rhs_only)
5513 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5514 if (newlhs != NULL_TREE)
5516 lhs = newlhs;
5517 lhs_code = TREE_CODE (lhs);
5521 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5522 if (inv_code == rhs_code
5523 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5524 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5525 return lhs;
5526 if (!rhs_only && inv_code == lhs_code
5527 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5528 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5529 return rhs;
5530 if (rhs != orig_rhs || lhs != orig_lhs)
5531 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5532 lhs, rhs);
5533 return NULL_TREE;
5536 /* Find ways of folding logical expressions of LHS and RHS:
5537 Try to merge two comparisons to the same innermost item.
5538 Look for range tests like "ch >= '0' && ch <= '9'".
5539 Look for combinations of simple terms on machines with expensive branches
5540 and evaluate the RHS unconditionally.
5542 For example, if we have p->a == 2 && p->b == 4 and we can make an
5543 object large enough to span both A and B, we can do this with a comparison
5544 against the object ANDed with the a mask.
5546 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5547 operations to do this with one comparison.
5549 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5550 function and the one above.
5552 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5553 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5555 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5556 two operands.
5558 We return the simplified tree or 0 if no optimization is possible. */
5560 static tree
5561 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5562 tree lhs, tree rhs)
5564 /* If this is the "or" of two comparisons, we can do something if
5565 the comparisons are NE_EXPR. If this is the "and", we can do something
5566 if the comparisons are EQ_EXPR. I.e.,
5567 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5569 WANTED_CODE is this operation code. For single bit fields, we can
5570 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5571 comparison for one-bit fields. */
5573 enum tree_code wanted_code;
5574 enum tree_code lcode, rcode;
5575 tree ll_arg, lr_arg, rl_arg, rr_arg;
5576 tree ll_inner, lr_inner, rl_inner, rr_inner;
5577 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5578 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5579 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5580 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5581 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5582 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5583 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5584 machine_mode lnmode, rnmode;
5585 tree ll_mask, lr_mask, rl_mask, rr_mask;
5586 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5587 tree l_const, r_const;
5588 tree lntype, rntype, result;
5589 HOST_WIDE_INT first_bit, end_bit;
5590 int volatilep;
5592 /* Start by getting the comparison codes. Fail if anything is volatile.
5593 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5594 it were surrounded with a NE_EXPR. */
5596 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5597 return 0;
5599 lcode = TREE_CODE (lhs);
5600 rcode = TREE_CODE (rhs);
5602 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5604 lhs = build2 (NE_EXPR, truth_type, lhs,
5605 build_int_cst (TREE_TYPE (lhs), 0));
5606 lcode = NE_EXPR;
5609 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5611 rhs = build2 (NE_EXPR, truth_type, rhs,
5612 build_int_cst (TREE_TYPE (rhs), 0));
5613 rcode = NE_EXPR;
5616 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5617 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5618 return 0;
5620 ll_arg = TREE_OPERAND (lhs, 0);
5621 lr_arg = TREE_OPERAND (lhs, 1);
5622 rl_arg = TREE_OPERAND (rhs, 0);
5623 rr_arg = TREE_OPERAND (rhs, 1);
5625 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5626 if (simple_operand_p (ll_arg)
5627 && simple_operand_p (lr_arg))
5629 if (operand_equal_p (ll_arg, rl_arg, 0)
5630 && operand_equal_p (lr_arg, rr_arg, 0))
5632 result = combine_comparisons (loc, code, lcode, rcode,
5633 truth_type, ll_arg, lr_arg);
5634 if (result)
5635 return result;
5637 else if (operand_equal_p (ll_arg, rr_arg, 0)
5638 && operand_equal_p (lr_arg, rl_arg, 0))
5640 result = combine_comparisons (loc, code, lcode,
5641 swap_tree_comparison (rcode),
5642 truth_type, ll_arg, lr_arg);
5643 if (result)
5644 return result;
5648 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5649 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5651 /* If the RHS can be evaluated unconditionally and its operands are
5652 simple, it wins to evaluate the RHS unconditionally on machines
5653 with expensive branches. In this case, this isn't a comparison
5654 that can be merged. */
5656 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5657 false) >= 2
5658 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5659 && simple_operand_p (rl_arg)
5660 && simple_operand_p (rr_arg))
5662 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5663 if (code == TRUTH_OR_EXPR
5664 && lcode == NE_EXPR && integer_zerop (lr_arg)
5665 && rcode == NE_EXPR && integer_zerop (rr_arg)
5666 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5667 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5668 return build2_loc (loc, NE_EXPR, truth_type,
5669 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5670 ll_arg, rl_arg),
5671 build_int_cst (TREE_TYPE (ll_arg), 0));
5673 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5674 if (code == TRUTH_AND_EXPR
5675 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5676 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5677 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5678 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5679 return build2_loc (loc, EQ_EXPR, truth_type,
5680 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5681 ll_arg, rl_arg),
5682 build_int_cst (TREE_TYPE (ll_arg), 0));
5685 /* See if the comparisons can be merged. Then get all the parameters for
5686 each side. */
5688 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5689 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5690 return 0;
5692 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5693 volatilep = 0;
5694 ll_inner = decode_field_reference (loc, &ll_arg,
5695 &ll_bitsize, &ll_bitpos, &ll_mode,
5696 &ll_unsignedp, &ll_reversep, &volatilep,
5697 &ll_mask, &ll_and_mask);
5698 lr_inner = decode_field_reference (loc, &lr_arg,
5699 &lr_bitsize, &lr_bitpos, &lr_mode,
5700 &lr_unsignedp, &lr_reversep, &volatilep,
5701 &lr_mask, &lr_and_mask);
5702 rl_inner = decode_field_reference (loc, &rl_arg,
5703 &rl_bitsize, &rl_bitpos, &rl_mode,
5704 &rl_unsignedp, &rl_reversep, &volatilep,
5705 &rl_mask, &rl_and_mask);
5706 rr_inner = decode_field_reference (loc, &rr_arg,
5707 &rr_bitsize, &rr_bitpos, &rr_mode,
5708 &rr_unsignedp, &rr_reversep, &volatilep,
5709 &rr_mask, &rr_and_mask);
5711 /* It must be true that the inner operation on the lhs of each
5712 comparison must be the same if we are to be able to do anything.
5713 Then see if we have constants. If not, the same must be true for
5714 the rhs's. */
5715 if (volatilep
5716 || ll_reversep != rl_reversep
5717 || ll_inner == 0 || rl_inner == 0
5718 || ! operand_equal_p (ll_inner, rl_inner, 0))
5719 return 0;
5721 if (TREE_CODE (lr_arg) == INTEGER_CST
5722 && TREE_CODE (rr_arg) == INTEGER_CST)
5724 l_const = lr_arg, r_const = rr_arg;
5725 lr_reversep = ll_reversep;
5727 else if (lr_reversep != rr_reversep
5728 || lr_inner == 0 || rr_inner == 0
5729 || ! operand_equal_p (lr_inner, rr_inner, 0))
5730 return 0;
5731 else
5732 l_const = r_const = 0;
5734 /* If either comparison code is not correct for our logical operation,
5735 fail. However, we can convert a one-bit comparison against zero into
5736 the opposite comparison against that bit being set in the field. */
5738 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5739 if (lcode != wanted_code)
5741 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5743 /* Make the left operand unsigned, since we are only interested
5744 in the value of one bit. Otherwise we are doing the wrong
5745 thing below. */
5746 ll_unsignedp = 1;
5747 l_const = ll_mask;
5749 else
5750 return 0;
5753 /* This is analogous to the code for l_const above. */
5754 if (rcode != wanted_code)
5756 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5758 rl_unsignedp = 1;
5759 r_const = rl_mask;
5761 else
5762 return 0;
5765 /* See if we can find a mode that contains both fields being compared on
5766 the left. If we can't, fail. Otherwise, update all constants and masks
5767 to be relative to a field of that size. */
5768 first_bit = MIN (ll_bitpos, rl_bitpos);
5769 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5770 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5771 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5772 volatilep);
5773 if (lnmode == VOIDmode)
5774 return 0;
5776 lnbitsize = GET_MODE_BITSIZE (lnmode);
5777 lnbitpos = first_bit & ~ (lnbitsize - 1);
5778 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5779 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5781 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5783 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5784 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5787 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5788 size_int (xll_bitpos));
5789 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5790 size_int (xrl_bitpos));
5792 if (l_const)
5794 l_const = fold_convert_loc (loc, lntype, l_const);
5795 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5796 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5797 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5798 fold_build1_loc (loc, BIT_NOT_EXPR,
5799 lntype, ll_mask))))
5801 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5803 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5806 if (r_const)
5808 r_const = fold_convert_loc (loc, lntype, r_const);
5809 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5810 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5811 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5812 fold_build1_loc (loc, BIT_NOT_EXPR,
5813 lntype, rl_mask))))
5815 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5817 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5821 /* If the right sides are not constant, do the same for it. Also,
5822 disallow this optimization if a size or signedness mismatch occurs
5823 between the left and right sides. */
5824 if (l_const == 0)
5826 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5827 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5828 /* Make sure the two fields on the right
5829 correspond to the left without being swapped. */
5830 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5831 return 0;
5833 first_bit = MIN (lr_bitpos, rr_bitpos);
5834 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5835 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5836 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5837 volatilep);
5838 if (rnmode == VOIDmode)
5839 return 0;
5841 rnbitsize = GET_MODE_BITSIZE (rnmode);
5842 rnbitpos = first_bit & ~ (rnbitsize - 1);
5843 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5844 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5846 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5848 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5849 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5852 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5853 rntype, lr_mask),
5854 size_int (xlr_bitpos));
5855 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5856 rntype, rr_mask),
5857 size_int (xrr_bitpos));
5859 /* Make a mask that corresponds to both fields being compared.
5860 Do this for both items being compared. If the operands are the
5861 same size and the bits being compared are in the same position
5862 then we can do this by masking both and comparing the masked
5863 results. */
5864 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5865 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5866 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5868 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5869 lntype, lnbitsize, lnbitpos,
5870 ll_unsignedp || rl_unsignedp, ll_reversep);
5871 if (! all_ones_mask_p (ll_mask, lnbitsize))
5872 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5874 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5875 rntype, rnbitsize, rnbitpos,
5876 lr_unsignedp || rr_unsignedp, lr_reversep);
5877 if (! all_ones_mask_p (lr_mask, rnbitsize))
5878 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5880 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5883 /* There is still another way we can do something: If both pairs of
5884 fields being compared are adjacent, we may be able to make a wider
5885 field containing them both.
5887 Note that we still must mask the lhs/rhs expressions. Furthermore,
5888 the mask must be shifted to account for the shift done by
5889 make_bit_field_ref. */
5890 if ((ll_bitsize + ll_bitpos == rl_bitpos
5891 && lr_bitsize + lr_bitpos == rr_bitpos)
5892 || (ll_bitpos == rl_bitpos + rl_bitsize
5893 && lr_bitpos == rr_bitpos + rr_bitsize))
5895 tree type;
5897 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5898 ll_bitsize + rl_bitsize,
5899 MIN (ll_bitpos, rl_bitpos),
5900 ll_unsignedp, ll_reversep);
5901 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5902 lr_bitsize + rr_bitsize,
5903 MIN (lr_bitpos, rr_bitpos),
5904 lr_unsignedp, lr_reversep);
5906 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5907 size_int (MIN (xll_bitpos, xrl_bitpos)));
5908 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5909 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5911 /* Convert to the smaller type before masking out unwanted bits. */
5912 type = lntype;
5913 if (lntype != rntype)
5915 if (lnbitsize > rnbitsize)
5917 lhs = fold_convert_loc (loc, rntype, lhs);
5918 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5919 type = rntype;
5921 else if (lnbitsize < rnbitsize)
5923 rhs = fold_convert_loc (loc, lntype, rhs);
5924 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5925 type = lntype;
5929 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5930 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5932 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5933 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5935 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5938 return 0;
5941 /* Handle the case of comparisons with constants. If there is something in
5942 common between the masks, those bits of the constants must be the same.
5943 If not, the condition is always false. Test for this to avoid generating
5944 incorrect code below. */
5945 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5946 if (! integer_zerop (result)
5947 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5948 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5950 if (wanted_code == NE_EXPR)
5952 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5953 return constant_boolean_node (true, truth_type);
5955 else
5957 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5958 return constant_boolean_node (false, truth_type);
5962 /* Construct the expression we will return. First get the component
5963 reference we will make. Unless the mask is all ones the width of
5964 that field, perform the mask operation. Then compare with the
5965 merged constant. */
5966 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5967 lntype, lnbitsize, lnbitpos,
5968 ll_unsignedp || rl_unsignedp, ll_reversep);
5970 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5971 if (! all_ones_mask_p (ll_mask, lnbitsize))
5972 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5974 return build2_loc (loc, wanted_code, truth_type, result,
5975 const_binop (BIT_IOR_EXPR, l_const, r_const));
5978 /* T is an integer expression that is being multiplied, divided, or taken a
5979 modulus (CODE says which and what kind of divide or modulus) by a
5980 constant C. See if we can eliminate that operation by folding it with
5981 other operations already in T. WIDE_TYPE, if non-null, is a type that
5982 should be used for the computation if wider than our type.
5984 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5985 (X * 2) + (Y * 4). We must, however, be assured that either the original
5986 expression would not overflow or that overflow is undefined for the type
5987 in the language in question.
5989 If we return a non-null expression, it is an equivalent form of the
5990 original computation, but need not be in the original type.
5992 We set *STRICT_OVERFLOW_P to true if the return values depends on
5993 signed overflow being undefined. Otherwise we do not change
5994 *STRICT_OVERFLOW_P. */
5996 static tree
5997 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5998 bool *strict_overflow_p)
6000 /* To avoid exponential search depth, refuse to allow recursion past
6001 three levels. Beyond that (1) it's highly unlikely that we'll find
6002 something interesting and (2) we've probably processed it before
6003 when we built the inner expression. */
6005 static int depth;
6006 tree ret;
6008 if (depth > 3)
6009 return NULL;
6011 depth++;
6012 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6013 depth--;
6015 return ret;
6018 static tree
6019 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6020 bool *strict_overflow_p)
6022 tree type = TREE_TYPE (t);
6023 enum tree_code tcode = TREE_CODE (t);
6024 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6025 > GET_MODE_SIZE (TYPE_MODE (type)))
6026 ? wide_type : type);
6027 tree t1, t2;
6028 int same_p = tcode == code;
6029 tree op0 = NULL_TREE, op1 = NULL_TREE;
6030 bool sub_strict_overflow_p;
6032 /* Don't deal with constants of zero here; they confuse the code below. */
6033 if (integer_zerop (c))
6034 return NULL_TREE;
6036 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6037 op0 = TREE_OPERAND (t, 0);
6039 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6040 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6042 /* Note that we need not handle conditional operations here since fold
6043 already handles those cases. So just do arithmetic here. */
6044 switch (tcode)
6046 case INTEGER_CST:
6047 /* For a constant, we can always simplify if we are a multiply
6048 or (for divide and modulus) if it is a multiple of our constant. */
6049 if (code == MULT_EXPR
6050 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6052 tree tem = const_binop (code, fold_convert (ctype, t),
6053 fold_convert (ctype, c));
6054 /* If the multiplication overflowed, we lost information on it.
6055 See PR68142 and PR69845. */
6056 if (TREE_OVERFLOW (tem))
6057 return NULL_TREE;
6058 return tem;
6060 break;
6062 CASE_CONVERT: case NON_LVALUE_EXPR:
6063 /* If op0 is an expression ... */
6064 if ((COMPARISON_CLASS_P (op0)
6065 || UNARY_CLASS_P (op0)
6066 || BINARY_CLASS_P (op0)
6067 || VL_EXP_CLASS_P (op0)
6068 || EXPRESSION_CLASS_P (op0))
6069 /* ... and has wrapping overflow, and its type is smaller
6070 than ctype, then we cannot pass through as widening. */
6071 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6072 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6073 && (TYPE_PRECISION (ctype)
6074 > TYPE_PRECISION (TREE_TYPE (op0))))
6075 /* ... or this is a truncation (t is narrower than op0),
6076 then we cannot pass through this narrowing. */
6077 || (TYPE_PRECISION (type)
6078 < TYPE_PRECISION (TREE_TYPE (op0)))
6079 /* ... or signedness changes for division or modulus,
6080 then we cannot pass through this conversion. */
6081 || (code != MULT_EXPR
6082 && (TYPE_UNSIGNED (ctype)
6083 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6084 /* ... or has undefined overflow while the converted to
6085 type has not, we cannot do the operation in the inner type
6086 as that would introduce undefined overflow. */
6087 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6088 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6089 && !TYPE_OVERFLOW_UNDEFINED (type))))
6090 break;
6092 /* Pass the constant down and see if we can make a simplification. If
6093 we can, replace this expression with the inner simplification for
6094 possible later conversion to our or some other type. */
6095 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6096 && TREE_CODE (t2) == INTEGER_CST
6097 && !TREE_OVERFLOW (t2)
6098 && (0 != (t1 = extract_muldiv (op0, t2, code,
6099 code == MULT_EXPR
6100 ? ctype : NULL_TREE,
6101 strict_overflow_p))))
6102 return t1;
6103 break;
6105 case ABS_EXPR:
6106 /* If widening the type changes it from signed to unsigned, then we
6107 must avoid building ABS_EXPR itself as unsigned. */
6108 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6110 tree cstype = (*signed_type_for) (ctype);
6111 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6112 != 0)
6114 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6115 return fold_convert (ctype, t1);
6117 break;
6119 /* If the constant is negative, we cannot simplify this. */
6120 if (tree_int_cst_sgn (c) == -1)
6121 break;
6122 /* FALLTHROUGH */
6123 case NEGATE_EXPR:
6124 /* For division and modulus, type can't be unsigned, as e.g.
6125 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6126 For signed types, even with wrapping overflow, this is fine. */
6127 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6128 break;
6129 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6130 != 0)
6131 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6132 break;
6134 case MIN_EXPR: case MAX_EXPR:
6135 /* If widening the type changes the signedness, then we can't perform
6136 this optimization as that changes the result. */
6137 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6138 break;
6140 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6141 sub_strict_overflow_p = false;
6142 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6143 &sub_strict_overflow_p)) != 0
6144 && (t2 = extract_muldiv (op1, c, code, wide_type,
6145 &sub_strict_overflow_p)) != 0)
6147 if (tree_int_cst_sgn (c) < 0)
6148 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6149 if (sub_strict_overflow_p)
6150 *strict_overflow_p = true;
6151 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6152 fold_convert (ctype, t2));
6154 break;
6156 case LSHIFT_EXPR: case RSHIFT_EXPR:
6157 /* If the second operand is constant, this is a multiplication
6158 or floor division, by a power of two, so we can treat it that
6159 way unless the multiplier or divisor overflows. Signed
6160 left-shift overflow is implementation-defined rather than
6161 undefined in C90, so do not convert signed left shift into
6162 multiplication. */
6163 if (TREE_CODE (op1) == INTEGER_CST
6164 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6165 /* const_binop may not detect overflow correctly,
6166 so check for it explicitly here. */
6167 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6168 && 0 != (t1 = fold_convert (ctype,
6169 const_binop (LSHIFT_EXPR,
6170 size_one_node,
6171 op1)))
6172 && !TREE_OVERFLOW (t1))
6173 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6174 ? MULT_EXPR : FLOOR_DIV_EXPR,
6175 ctype,
6176 fold_convert (ctype, op0),
6177 t1),
6178 c, code, wide_type, strict_overflow_p);
6179 break;
6181 case PLUS_EXPR: case MINUS_EXPR:
6182 /* See if we can eliminate the operation on both sides. If we can, we
6183 can return a new PLUS or MINUS. If we can't, the only remaining
6184 cases where we can do anything are if the second operand is a
6185 constant. */
6186 sub_strict_overflow_p = false;
6187 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6188 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6189 if (t1 != 0 && t2 != 0
6190 && (code == MULT_EXPR
6191 /* If not multiplication, we can only do this if both operands
6192 are divisible by c. */
6193 || (multiple_of_p (ctype, op0, c)
6194 && multiple_of_p (ctype, op1, c))))
6196 if (sub_strict_overflow_p)
6197 *strict_overflow_p = true;
6198 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6199 fold_convert (ctype, t2));
6202 /* If this was a subtraction, negate OP1 and set it to be an addition.
6203 This simplifies the logic below. */
6204 if (tcode == MINUS_EXPR)
6206 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6207 /* If OP1 was not easily negatable, the constant may be OP0. */
6208 if (TREE_CODE (op0) == INTEGER_CST)
6210 std::swap (op0, op1);
6211 std::swap (t1, t2);
6215 if (TREE_CODE (op1) != INTEGER_CST)
6216 break;
6218 /* If either OP1 or C are negative, this optimization is not safe for
6219 some of the division and remainder types while for others we need
6220 to change the code. */
6221 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6223 if (code == CEIL_DIV_EXPR)
6224 code = FLOOR_DIV_EXPR;
6225 else if (code == FLOOR_DIV_EXPR)
6226 code = CEIL_DIV_EXPR;
6227 else if (code != MULT_EXPR
6228 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6229 break;
6232 /* If it's a multiply or a division/modulus operation of a multiple
6233 of our constant, do the operation and verify it doesn't overflow. */
6234 if (code == MULT_EXPR
6235 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6237 op1 = const_binop (code, fold_convert (ctype, op1),
6238 fold_convert (ctype, c));
6239 /* We allow the constant to overflow with wrapping semantics. */
6240 if (op1 == 0
6241 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6242 break;
6244 else
6245 break;
6247 /* If we have an unsigned type, we cannot widen the operation since it
6248 will change the result if the original computation overflowed. */
6249 if (TYPE_UNSIGNED (ctype) && ctype != type)
6250 break;
6252 /* If we were able to eliminate our operation from the first side,
6253 apply our operation to the second side and reform the PLUS. */
6254 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6255 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6257 /* The last case is if we are a multiply. In that case, we can
6258 apply the distributive law to commute the multiply and addition
6259 if the multiplication of the constants doesn't overflow
6260 and overflow is defined. With undefined overflow
6261 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6262 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6263 return fold_build2 (tcode, ctype,
6264 fold_build2 (code, ctype,
6265 fold_convert (ctype, op0),
6266 fold_convert (ctype, c)),
6267 op1);
6269 break;
6271 case MULT_EXPR:
6272 /* We have a special case here if we are doing something like
6273 (C * 8) % 4 since we know that's zero. */
6274 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6275 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6276 /* If the multiplication can overflow we cannot optimize this. */
6277 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6278 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6279 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6281 *strict_overflow_p = true;
6282 return omit_one_operand (type, integer_zero_node, op0);
6285 /* ... fall through ... */
6287 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6288 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6289 /* If we can extract our operation from the LHS, do so and return a
6290 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6291 do something only if the second operand is a constant. */
6292 if (same_p
6293 && (t1 = extract_muldiv (op0, c, code, wide_type,
6294 strict_overflow_p)) != 0)
6295 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6296 fold_convert (ctype, op1));
6297 else if (tcode == MULT_EXPR && code == MULT_EXPR
6298 && (t1 = extract_muldiv (op1, c, code, wide_type,
6299 strict_overflow_p)) != 0)
6300 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6301 fold_convert (ctype, t1));
6302 else if (TREE_CODE (op1) != INTEGER_CST)
6303 return 0;
6305 /* If these are the same operation types, we can associate them
6306 assuming no overflow. */
6307 if (tcode == code)
6309 bool overflow_p = false;
6310 bool overflow_mul_p;
6311 signop sign = TYPE_SIGN (ctype);
6312 unsigned prec = TYPE_PRECISION (ctype);
6313 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6314 wi::to_wide (c, prec),
6315 sign, &overflow_mul_p);
6316 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6317 if (overflow_mul_p
6318 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6319 overflow_p = true;
6320 if (!overflow_p)
6321 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6322 wide_int_to_tree (ctype, mul));
6325 /* If these operations "cancel" each other, we have the main
6326 optimizations of this pass, which occur when either constant is a
6327 multiple of the other, in which case we replace this with either an
6328 operation or CODE or TCODE.
6330 If we have an unsigned type, we cannot do this since it will change
6331 the result if the original computation overflowed. */
6332 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6333 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6334 || (tcode == MULT_EXPR
6335 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6336 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6337 && code != MULT_EXPR)))
6339 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6341 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6342 *strict_overflow_p = true;
6343 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6344 fold_convert (ctype,
6345 const_binop (TRUNC_DIV_EXPR,
6346 op1, c)));
6348 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6350 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6351 *strict_overflow_p = true;
6352 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6353 fold_convert (ctype,
6354 const_binop (TRUNC_DIV_EXPR,
6355 c, op1)));
6358 break;
6360 default:
6361 break;
6364 return 0;
6367 /* Return a node which has the indicated constant VALUE (either 0 or
6368 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6369 and is of the indicated TYPE. */
6371 tree
6372 constant_boolean_node (bool value, tree type)
6374 if (type == integer_type_node)
6375 return value ? integer_one_node : integer_zero_node;
6376 else if (type == boolean_type_node)
6377 return value ? boolean_true_node : boolean_false_node;
6378 else if (TREE_CODE (type) == VECTOR_TYPE)
6379 return build_vector_from_val (type,
6380 build_int_cst (TREE_TYPE (type),
6381 value ? -1 : 0));
6382 else
6383 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6387 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6388 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6389 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6390 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6391 COND is the first argument to CODE; otherwise (as in the example
6392 given here), it is the second argument. TYPE is the type of the
6393 original expression. Return NULL_TREE if no simplification is
6394 possible. */
6396 static tree
6397 fold_binary_op_with_conditional_arg (location_t loc,
6398 enum tree_code code,
6399 tree type, tree op0, tree op1,
6400 tree cond, tree arg, int cond_first_p)
6402 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6403 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6404 tree test, true_value, false_value;
6405 tree lhs = NULL_TREE;
6406 tree rhs = NULL_TREE;
6407 enum tree_code cond_code = COND_EXPR;
6409 if (TREE_CODE (cond) == COND_EXPR
6410 || TREE_CODE (cond) == VEC_COND_EXPR)
6412 test = TREE_OPERAND (cond, 0);
6413 true_value = TREE_OPERAND (cond, 1);
6414 false_value = TREE_OPERAND (cond, 2);
6415 /* If this operand throws an expression, then it does not make
6416 sense to try to perform a logical or arithmetic operation
6417 involving it. */
6418 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6419 lhs = true_value;
6420 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6421 rhs = false_value;
6423 else if (!(TREE_CODE (type) != VECTOR_TYPE
6424 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6426 tree testtype = TREE_TYPE (cond);
6427 test = cond;
6428 true_value = constant_boolean_node (true, testtype);
6429 false_value = constant_boolean_node (false, testtype);
6431 else
6432 /* Detect the case of mixing vector and scalar types - bail out. */
6433 return NULL_TREE;
6435 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6436 cond_code = VEC_COND_EXPR;
6438 /* This transformation is only worthwhile if we don't have to wrap ARG
6439 in a SAVE_EXPR and the operation can be simplified without recursing
6440 on at least one of the branches once its pushed inside the COND_EXPR. */
6441 if (!TREE_CONSTANT (arg)
6442 && (TREE_SIDE_EFFECTS (arg)
6443 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6444 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6445 return NULL_TREE;
6447 arg = fold_convert_loc (loc, arg_type, arg);
6448 if (lhs == 0)
6450 true_value = fold_convert_loc (loc, cond_type, true_value);
6451 if (cond_first_p)
6452 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6453 else
6454 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6456 if (rhs == 0)
6458 false_value = fold_convert_loc (loc, cond_type, false_value);
6459 if (cond_first_p)
6460 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6461 else
6462 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6465 /* Check that we have simplified at least one of the branches. */
6466 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6467 return NULL_TREE;
6469 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6473 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6475 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6476 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6477 ADDEND is the same as X.
6479 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6480 and finite. The problematic cases are when X is zero, and its mode
6481 has signed zeros. In the case of rounding towards -infinity,
6482 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6483 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6485 bool
6486 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6488 if (!real_zerop (addend))
6489 return false;
6491 /* Don't allow the fold with -fsignaling-nans. */
6492 if (HONOR_SNANS (element_mode (type)))
6493 return false;
6495 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6496 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6497 return true;
6499 /* In a vector or complex, we would need to check the sign of all zeros. */
6500 if (TREE_CODE (addend) != REAL_CST)
6501 return false;
6503 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6504 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6505 negate = !negate;
6507 /* The mode has signed zeros, and we have to honor their sign.
6508 In this situation, there is only one case we can return true for.
6509 X - 0 is the same as X unless rounding towards -infinity is
6510 supported. */
6511 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6514 /* Subroutine of fold() that optimizes comparisons of a division by
6515 a nonzero integer constant against an integer constant, i.e.
6516 X/C1 op C2.
6518 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6519 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6520 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6522 The function returns the constant folded tree if a simplification
6523 can be made, and NULL_TREE otherwise. */
6525 static tree
6526 fold_div_compare (location_t loc,
6527 enum tree_code code, tree type, tree arg0, tree arg1)
6529 tree prod, tmp, hi, lo;
6530 tree arg00 = TREE_OPERAND (arg0, 0);
6531 tree arg01 = TREE_OPERAND (arg0, 1);
6532 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6533 bool neg_overflow = false;
6534 bool overflow;
6536 /* We have to do this the hard way to detect unsigned overflow.
6537 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6538 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6539 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6540 neg_overflow = false;
6542 if (sign == UNSIGNED)
6544 tmp = int_const_binop (MINUS_EXPR, arg01,
6545 build_int_cst (TREE_TYPE (arg01), 1));
6546 lo = prod;
6548 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6549 val = wi::add (prod, tmp, sign, &overflow);
6550 hi = force_fit_type (TREE_TYPE (arg00), val,
6551 -1, overflow | TREE_OVERFLOW (prod));
6553 else if (tree_int_cst_sgn (arg01) >= 0)
6555 tmp = int_const_binop (MINUS_EXPR, arg01,
6556 build_int_cst (TREE_TYPE (arg01), 1));
6557 switch (tree_int_cst_sgn (arg1))
6559 case -1:
6560 neg_overflow = true;
6561 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6562 hi = prod;
6563 break;
6565 case 0:
6566 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6567 hi = tmp;
6568 break;
6570 case 1:
6571 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6572 lo = prod;
6573 break;
6575 default:
6576 gcc_unreachable ();
6579 else
6581 /* A negative divisor reverses the relational operators. */
6582 code = swap_tree_comparison (code);
6584 tmp = int_const_binop (PLUS_EXPR, arg01,
6585 build_int_cst (TREE_TYPE (arg01), 1));
6586 switch (tree_int_cst_sgn (arg1))
6588 case -1:
6589 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6590 lo = prod;
6591 break;
6593 case 0:
6594 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6595 lo = tmp;
6596 break;
6598 case 1:
6599 neg_overflow = true;
6600 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6601 hi = prod;
6602 break;
6604 default:
6605 gcc_unreachable ();
6609 switch (code)
6611 case EQ_EXPR:
6612 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6613 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6614 if (TREE_OVERFLOW (hi))
6615 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6616 if (TREE_OVERFLOW (lo))
6617 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6618 return build_range_check (loc, type, arg00, 1, lo, hi);
6620 case NE_EXPR:
6621 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6622 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6623 if (TREE_OVERFLOW (hi))
6624 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6625 if (TREE_OVERFLOW (lo))
6626 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6627 return build_range_check (loc, type, arg00, 0, lo, hi);
6629 case LT_EXPR:
6630 if (TREE_OVERFLOW (lo))
6632 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6633 return omit_one_operand_loc (loc, type, tmp, arg00);
6635 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6637 case LE_EXPR:
6638 if (TREE_OVERFLOW (hi))
6640 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6641 return omit_one_operand_loc (loc, type, tmp, arg00);
6643 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6645 case GT_EXPR:
6646 if (TREE_OVERFLOW (hi))
6648 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6649 return omit_one_operand_loc (loc, type, tmp, arg00);
6651 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6653 case GE_EXPR:
6654 if (TREE_OVERFLOW (lo))
6656 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6657 return omit_one_operand_loc (loc, type, tmp, arg00);
6659 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6661 default:
6662 break;
6665 return NULL_TREE;
6669 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6670 equality/inequality test, then return a simplified form of the test
6671 using a sign testing. Otherwise return NULL. TYPE is the desired
6672 result type. */
6674 static tree
6675 fold_single_bit_test_into_sign_test (location_t loc,
6676 enum tree_code code, tree arg0, tree arg1,
6677 tree result_type)
6679 /* If this is testing a single bit, we can optimize the test. */
6680 if ((code == NE_EXPR || code == EQ_EXPR)
6681 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6682 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6684 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6685 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6686 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6688 if (arg00 != NULL_TREE
6689 /* This is only a win if casting to a signed type is cheap,
6690 i.e. when arg00's type is not a partial mode. */
6691 && TYPE_PRECISION (TREE_TYPE (arg00))
6692 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6694 tree stype = signed_type_for (TREE_TYPE (arg00));
6695 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6696 result_type,
6697 fold_convert_loc (loc, stype, arg00),
6698 build_int_cst (stype, 0));
6702 return NULL_TREE;
6705 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6706 equality/inequality test, then return a simplified form of
6707 the test using shifts and logical operations. Otherwise return
6708 NULL. TYPE is the desired result type. */
6710 tree
6711 fold_single_bit_test (location_t loc, enum tree_code code,
6712 tree arg0, tree arg1, tree result_type)
6714 /* If this is testing a single bit, we can optimize the test. */
6715 if ((code == NE_EXPR || code == EQ_EXPR)
6716 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6717 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6719 tree inner = TREE_OPERAND (arg0, 0);
6720 tree type = TREE_TYPE (arg0);
6721 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6722 machine_mode operand_mode = TYPE_MODE (type);
6723 int ops_unsigned;
6724 tree signed_type, unsigned_type, intermediate_type;
6725 tree tem, one;
6727 /* First, see if we can fold the single bit test into a sign-bit
6728 test. */
6729 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6730 result_type);
6731 if (tem)
6732 return tem;
6734 /* Otherwise we have (A & C) != 0 where C is a single bit,
6735 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6736 Similarly for (A & C) == 0. */
6738 /* If INNER is a right shift of a constant and it plus BITNUM does
6739 not overflow, adjust BITNUM and INNER. */
6740 if (TREE_CODE (inner) == RSHIFT_EXPR
6741 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6742 && bitnum < TYPE_PRECISION (type)
6743 && wi::ltu_p (TREE_OPERAND (inner, 1),
6744 TYPE_PRECISION (type) - bitnum))
6746 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6747 inner = TREE_OPERAND (inner, 0);
6750 /* If we are going to be able to omit the AND below, we must do our
6751 operations as unsigned. If we must use the AND, we have a choice.
6752 Normally unsigned is faster, but for some machines signed is. */
6753 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6754 && !flag_syntax_only) ? 0 : 1;
6756 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6757 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6758 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6759 inner = fold_convert_loc (loc, intermediate_type, inner);
6761 if (bitnum != 0)
6762 inner = build2 (RSHIFT_EXPR, intermediate_type,
6763 inner, size_int (bitnum));
6765 one = build_int_cst (intermediate_type, 1);
6767 if (code == EQ_EXPR)
6768 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6770 /* Put the AND last so it can combine with more things. */
6771 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6773 /* Make sure to return the proper type. */
6774 inner = fold_convert_loc (loc, result_type, inner);
6776 return inner;
6778 return NULL_TREE;
6781 /* Check whether we are allowed to reorder operands arg0 and arg1,
6782 such that the evaluation of arg1 occurs before arg0. */
6784 static bool
6785 reorder_operands_p (const_tree arg0, const_tree arg1)
6787 if (! flag_evaluation_order)
6788 return true;
6789 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6790 return true;
6791 return ! TREE_SIDE_EFFECTS (arg0)
6792 && ! TREE_SIDE_EFFECTS (arg1);
6795 /* Test whether it is preferable two swap two operands, ARG0 and
6796 ARG1, for example because ARG0 is an integer constant and ARG1
6797 isn't. If REORDER is true, only recommend swapping if we can
6798 evaluate the operands in reverse order. */
6800 bool
6801 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6803 if (CONSTANT_CLASS_P (arg1))
6804 return 0;
6805 if (CONSTANT_CLASS_P (arg0))
6806 return 1;
6808 STRIP_NOPS (arg0);
6809 STRIP_NOPS (arg1);
6811 if (TREE_CONSTANT (arg1))
6812 return 0;
6813 if (TREE_CONSTANT (arg0))
6814 return 1;
6816 if (reorder && flag_evaluation_order
6817 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6818 return 0;
6820 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6821 for commutative and comparison operators. Ensuring a canonical
6822 form allows the optimizers to find additional redundancies without
6823 having to explicitly check for both orderings. */
6824 if (TREE_CODE (arg0) == SSA_NAME
6825 && TREE_CODE (arg1) == SSA_NAME
6826 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6827 return 1;
6829 /* Put SSA_NAMEs last. */
6830 if (TREE_CODE (arg1) == SSA_NAME)
6831 return 0;
6832 if (TREE_CODE (arg0) == SSA_NAME)
6833 return 1;
6835 /* Put variables last. */
6836 if (DECL_P (arg1))
6837 return 0;
6838 if (DECL_P (arg0))
6839 return 1;
6841 return 0;
6845 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6846 means A >= Y && A != MAX, but in this case we know that
6847 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6849 static tree
6850 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6852 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6854 if (TREE_CODE (bound) == LT_EXPR)
6855 a = TREE_OPERAND (bound, 0);
6856 else if (TREE_CODE (bound) == GT_EXPR)
6857 a = TREE_OPERAND (bound, 1);
6858 else
6859 return NULL_TREE;
6861 typea = TREE_TYPE (a);
6862 if (!INTEGRAL_TYPE_P (typea)
6863 && !POINTER_TYPE_P (typea))
6864 return NULL_TREE;
6866 if (TREE_CODE (ineq) == LT_EXPR)
6868 a1 = TREE_OPERAND (ineq, 1);
6869 y = TREE_OPERAND (ineq, 0);
6871 else if (TREE_CODE (ineq) == GT_EXPR)
6873 a1 = TREE_OPERAND (ineq, 0);
6874 y = TREE_OPERAND (ineq, 1);
6876 else
6877 return NULL_TREE;
6879 if (TREE_TYPE (a1) != typea)
6880 return NULL_TREE;
6882 if (POINTER_TYPE_P (typea))
6884 /* Convert the pointer types into integer before taking the difference. */
6885 tree ta = fold_convert_loc (loc, ssizetype, a);
6886 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6887 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6889 else
6890 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6892 if (!diff || !integer_onep (diff))
6893 return NULL_TREE;
6895 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6898 /* Fold a sum or difference of at least one multiplication.
6899 Returns the folded tree or NULL if no simplification could be made. */
6901 static tree
6902 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6903 tree arg0, tree arg1)
6905 tree arg00, arg01, arg10, arg11;
6906 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6908 /* (A * C) +- (B * C) -> (A+-B) * C.
6909 (A * C) +- A -> A * (C+-1).
6910 We are most concerned about the case where C is a constant,
6911 but other combinations show up during loop reduction. Since
6912 it is not difficult, try all four possibilities. */
6914 if (TREE_CODE (arg0) == MULT_EXPR)
6916 arg00 = TREE_OPERAND (arg0, 0);
6917 arg01 = TREE_OPERAND (arg0, 1);
6919 else if (TREE_CODE (arg0) == INTEGER_CST)
6921 arg00 = build_one_cst (type);
6922 arg01 = arg0;
6924 else
6926 /* We cannot generate constant 1 for fract. */
6927 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6928 return NULL_TREE;
6929 arg00 = arg0;
6930 arg01 = build_one_cst (type);
6932 if (TREE_CODE (arg1) == MULT_EXPR)
6934 arg10 = TREE_OPERAND (arg1, 0);
6935 arg11 = TREE_OPERAND (arg1, 1);
6937 else if (TREE_CODE (arg1) == INTEGER_CST)
6939 arg10 = build_one_cst (type);
6940 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6941 the purpose of this canonicalization. */
6942 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6943 && negate_expr_p (arg1)
6944 && code == PLUS_EXPR)
6946 arg11 = negate_expr (arg1);
6947 code = MINUS_EXPR;
6949 else
6950 arg11 = arg1;
6952 else
6954 /* We cannot generate constant 1 for fract. */
6955 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6956 return NULL_TREE;
6957 arg10 = arg1;
6958 arg11 = build_one_cst (type);
6960 same = NULL_TREE;
6962 if (operand_equal_p (arg01, arg11, 0))
6963 same = arg01, alt0 = arg00, alt1 = arg10;
6964 else if (operand_equal_p (arg00, arg10, 0))
6965 same = arg00, alt0 = arg01, alt1 = arg11;
6966 else if (operand_equal_p (arg00, arg11, 0))
6967 same = arg00, alt0 = arg01, alt1 = arg10;
6968 else if (operand_equal_p (arg01, arg10, 0))
6969 same = arg01, alt0 = arg00, alt1 = arg11;
6971 /* No identical multiplicands; see if we can find a common
6972 power-of-two factor in non-power-of-two multiplies. This
6973 can help in multi-dimensional array access. */
6974 else if (tree_fits_shwi_p (arg01)
6975 && tree_fits_shwi_p (arg11))
6977 HOST_WIDE_INT int01, int11, tmp;
6978 bool swap = false;
6979 tree maybe_same;
6980 int01 = tree_to_shwi (arg01);
6981 int11 = tree_to_shwi (arg11);
6983 /* Move min of absolute values to int11. */
6984 if (absu_hwi (int01) < absu_hwi (int11))
6986 tmp = int01, int01 = int11, int11 = tmp;
6987 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6988 maybe_same = arg01;
6989 swap = true;
6991 else
6992 maybe_same = arg11;
6994 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6995 /* The remainder should not be a constant, otherwise we
6996 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6997 increased the number of multiplications necessary. */
6998 && TREE_CODE (arg10) != INTEGER_CST)
7000 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7001 build_int_cst (TREE_TYPE (arg00),
7002 int01 / int11));
7003 alt1 = arg10;
7004 same = maybe_same;
7005 if (swap)
7006 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7010 if (same)
7011 return fold_build2_loc (loc, MULT_EXPR, type,
7012 fold_build2_loc (loc, code, type,
7013 fold_convert_loc (loc, type, alt0),
7014 fold_convert_loc (loc, type, alt1)),
7015 fold_convert_loc (loc, type, same));
7017 return NULL_TREE;
7020 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7021 specified by EXPR into the buffer PTR of length LEN bytes.
7022 Return the number of bytes placed in the buffer, or zero
7023 upon failure. */
7025 static int
7026 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7028 tree type = TREE_TYPE (expr);
7029 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7030 int byte, offset, word, words;
7031 unsigned char value;
7033 if ((off == -1 && total_bytes > len)
7034 || off >= total_bytes)
7035 return 0;
7036 if (off == -1)
7037 off = 0;
7038 words = total_bytes / UNITS_PER_WORD;
7040 for (byte = 0; byte < total_bytes; byte++)
7042 int bitpos = byte * BITS_PER_UNIT;
7043 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7044 number of bytes. */
7045 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7047 if (total_bytes > UNITS_PER_WORD)
7049 word = byte / UNITS_PER_WORD;
7050 if (WORDS_BIG_ENDIAN)
7051 word = (words - 1) - word;
7052 offset = word * UNITS_PER_WORD;
7053 if (BYTES_BIG_ENDIAN)
7054 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7055 else
7056 offset += byte % UNITS_PER_WORD;
7058 else
7059 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7060 if (offset >= off
7061 && offset - off < len)
7062 ptr[offset - off] = value;
7064 return MIN (len, total_bytes - off);
7068 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7069 specified by EXPR into the buffer PTR of length LEN bytes.
7070 Return the number of bytes placed in the buffer, or zero
7071 upon failure. */
7073 static int
7074 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7076 tree type = TREE_TYPE (expr);
7077 machine_mode mode = TYPE_MODE (type);
7078 int total_bytes = GET_MODE_SIZE (mode);
7079 FIXED_VALUE_TYPE value;
7080 tree i_value, i_type;
7082 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7083 return 0;
7085 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7087 if (NULL_TREE == i_type
7088 || TYPE_PRECISION (i_type) != total_bytes)
7089 return 0;
7091 value = TREE_FIXED_CST (expr);
7092 i_value = double_int_to_tree (i_type, value.data);
7094 return native_encode_int (i_value, ptr, len, off);
7098 /* Subroutine of native_encode_expr. Encode the REAL_CST
7099 specified by EXPR into the buffer PTR of length LEN bytes.
7100 Return the number of bytes placed in the buffer, or zero
7101 upon failure. */
7103 static int
7104 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7106 tree type = TREE_TYPE (expr);
7107 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7108 int byte, offset, word, words, bitpos;
7109 unsigned char value;
7111 /* There are always 32 bits in each long, no matter the size of
7112 the hosts long. We handle floating point representations with
7113 up to 192 bits. */
7114 long tmp[6];
7116 if ((off == -1 && total_bytes > len)
7117 || off >= total_bytes)
7118 return 0;
7119 if (off == -1)
7120 off = 0;
7121 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7123 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7125 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7126 bitpos += BITS_PER_UNIT)
7128 byte = (bitpos / BITS_PER_UNIT) & 3;
7129 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7131 if (UNITS_PER_WORD < 4)
7133 word = byte / UNITS_PER_WORD;
7134 if (WORDS_BIG_ENDIAN)
7135 word = (words - 1) - word;
7136 offset = word * UNITS_PER_WORD;
7137 if (BYTES_BIG_ENDIAN)
7138 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7139 else
7140 offset += byte % UNITS_PER_WORD;
7142 else
7143 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7144 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7145 if (offset >= off
7146 && offset - off < len)
7147 ptr[offset - off] = value;
7149 return MIN (len, total_bytes - off);
7152 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7153 specified by EXPR into the buffer PTR of length LEN bytes.
7154 Return the number of bytes placed in the buffer, or zero
7155 upon failure. */
7157 static int
7158 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7160 int rsize, isize;
7161 tree part;
7163 part = TREE_REALPART (expr);
7164 rsize = native_encode_expr (part, ptr, len, off);
7165 if (off == -1
7166 && rsize == 0)
7167 return 0;
7168 part = TREE_IMAGPART (expr);
7169 if (off != -1)
7170 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7171 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7172 if (off == -1
7173 && isize != rsize)
7174 return 0;
7175 return rsize + isize;
7179 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7180 specified by EXPR into the buffer PTR of length LEN bytes.
7181 Return the number of bytes placed in the buffer, or zero
7182 upon failure. */
7184 static int
7185 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7187 unsigned i, count;
7188 int size, offset;
7189 tree itype, elem;
7191 offset = 0;
7192 count = VECTOR_CST_NELTS (expr);
7193 itype = TREE_TYPE (TREE_TYPE (expr));
7194 size = GET_MODE_SIZE (TYPE_MODE (itype));
7195 for (i = 0; i < count; i++)
7197 if (off >= size)
7199 off -= size;
7200 continue;
7202 elem = VECTOR_CST_ELT (expr, i);
7203 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7204 if ((off == -1 && res != size)
7205 || res == 0)
7206 return 0;
7207 offset += res;
7208 if (offset >= len)
7209 return offset;
7210 if (off != -1)
7211 off = 0;
7213 return offset;
7217 /* Subroutine of native_encode_expr. Encode the STRING_CST
7218 specified by EXPR into the buffer PTR of length LEN bytes.
7219 Return the number of bytes placed in the buffer, or zero
7220 upon failure. */
7222 static int
7223 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7225 tree type = TREE_TYPE (expr);
7226 HOST_WIDE_INT total_bytes;
7228 if (TREE_CODE (type) != ARRAY_TYPE
7229 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7230 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7231 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7232 return 0;
7233 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7234 if ((off == -1 && total_bytes > len)
7235 || off >= total_bytes)
7236 return 0;
7237 if (off == -1)
7238 off = 0;
7239 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7241 int written = 0;
7242 if (off < TREE_STRING_LENGTH (expr))
7244 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7245 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7247 memset (ptr + written, 0,
7248 MIN (total_bytes - written, len - written));
7250 else
7251 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7252 return MIN (total_bytes - off, len);
7256 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7257 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7258 buffer PTR of length LEN bytes. If OFF is not -1 then start
7259 the encoding at byte offset OFF and encode at most LEN bytes.
7260 Return the number of bytes placed in the buffer, or zero upon failure. */
7263 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7265 /* We don't support starting at negative offset and -1 is special. */
7266 if (off < -1)
7267 return 0;
7269 switch (TREE_CODE (expr))
7271 case INTEGER_CST:
7272 return native_encode_int (expr, ptr, len, off);
7274 case REAL_CST:
7275 return native_encode_real (expr, ptr, len, off);
7277 case FIXED_CST:
7278 return native_encode_fixed (expr, ptr, len, off);
7280 case COMPLEX_CST:
7281 return native_encode_complex (expr, ptr, len, off);
7283 case VECTOR_CST:
7284 return native_encode_vector (expr, ptr, len, off);
7286 case STRING_CST:
7287 return native_encode_string (expr, ptr, len, off);
7289 default:
7290 return 0;
7295 /* Subroutine of native_interpret_expr. Interpret the contents of
7296 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7297 If the buffer cannot be interpreted, return NULL_TREE. */
7299 static tree
7300 native_interpret_int (tree type, const unsigned char *ptr, int len)
7302 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7304 if (total_bytes > len
7305 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7306 return NULL_TREE;
7308 wide_int result = wi::from_buffer (ptr, total_bytes);
7310 return wide_int_to_tree (type, result);
7314 /* Subroutine of native_interpret_expr. Interpret the contents of
7315 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7316 If the buffer cannot be interpreted, return NULL_TREE. */
7318 static tree
7319 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7321 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7322 double_int result;
7323 FIXED_VALUE_TYPE fixed_value;
7325 if (total_bytes > len
7326 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7327 return NULL_TREE;
7329 result = double_int::from_buffer (ptr, total_bytes);
7330 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7332 return build_fixed (type, fixed_value);
7336 /* Subroutine of native_interpret_expr. Interpret the contents of
7337 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7338 If the buffer cannot be interpreted, return NULL_TREE. */
7340 static tree
7341 native_interpret_real (tree type, const unsigned char *ptr, int len)
7343 machine_mode mode = TYPE_MODE (type);
7344 int total_bytes = GET_MODE_SIZE (mode);
7345 unsigned char value;
7346 /* There are always 32 bits in each long, no matter the size of
7347 the hosts long. We handle floating point representations with
7348 up to 192 bits. */
7349 REAL_VALUE_TYPE r;
7350 long tmp[6];
7352 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7353 if (total_bytes > len || total_bytes > 24)
7354 return NULL_TREE;
7355 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7357 memset (tmp, 0, sizeof (tmp));
7358 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7359 bitpos += BITS_PER_UNIT)
7361 /* Both OFFSET and BYTE index within a long;
7362 bitpos indexes the whole float. */
7363 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7364 if (UNITS_PER_WORD < 4)
7366 int word = byte / UNITS_PER_WORD;
7367 if (WORDS_BIG_ENDIAN)
7368 word = (words - 1) - word;
7369 offset = word * UNITS_PER_WORD;
7370 if (BYTES_BIG_ENDIAN)
7371 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7372 else
7373 offset += byte % UNITS_PER_WORD;
7375 else
7377 offset = byte;
7378 if (BYTES_BIG_ENDIAN)
7380 /* Reverse bytes within each long, or within the entire float
7381 if it's smaller than a long (for HFmode). */
7382 offset = MIN (3, total_bytes - 1) - offset;
7383 gcc_assert (offset >= 0);
7386 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7388 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7391 real_from_target (&r, tmp, mode);
7392 return build_real (type, r);
7396 /* Subroutine of native_interpret_expr. Interpret the contents of
7397 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7398 If the buffer cannot be interpreted, return NULL_TREE. */
7400 static tree
7401 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7403 tree etype, rpart, ipart;
7404 int size;
7406 etype = TREE_TYPE (type);
7407 size = GET_MODE_SIZE (TYPE_MODE (etype));
7408 if (size * 2 > len)
7409 return NULL_TREE;
7410 rpart = native_interpret_expr (etype, ptr, size);
7411 if (!rpart)
7412 return NULL_TREE;
7413 ipart = native_interpret_expr (etype, ptr+size, size);
7414 if (!ipart)
7415 return NULL_TREE;
7416 return build_complex (type, rpart, ipart);
7420 /* Subroutine of native_interpret_expr. Interpret the contents of
7421 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7422 If the buffer cannot be interpreted, return NULL_TREE. */
7424 static tree
7425 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7427 tree etype, elem;
7428 int i, size, count;
7429 tree *elements;
7431 etype = TREE_TYPE (type);
7432 size = GET_MODE_SIZE (TYPE_MODE (etype));
7433 count = TYPE_VECTOR_SUBPARTS (type);
7434 if (size * count > len)
7435 return NULL_TREE;
7437 elements = XALLOCAVEC (tree, count);
7438 for (i = count - 1; i >= 0; i--)
7440 elem = native_interpret_expr (etype, ptr+(i*size), size);
7441 if (!elem)
7442 return NULL_TREE;
7443 elements[i] = elem;
7445 return build_vector (type, elements);
7449 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7450 the buffer PTR of length LEN as a constant of type TYPE. For
7451 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7452 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7453 return NULL_TREE. */
7455 tree
7456 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7458 switch (TREE_CODE (type))
7460 case INTEGER_TYPE:
7461 case ENUMERAL_TYPE:
7462 case BOOLEAN_TYPE:
7463 case POINTER_TYPE:
7464 case REFERENCE_TYPE:
7465 return native_interpret_int (type, ptr, len);
7467 case REAL_TYPE:
7468 return native_interpret_real (type, ptr, len);
7470 case FIXED_POINT_TYPE:
7471 return native_interpret_fixed (type, ptr, len);
7473 case COMPLEX_TYPE:
7474 return native_interpret_complex (type, ptr, len);
7476 case VECTOR_TYPE:
7477 return native_interpret_vector (type, ptr, len);
7479 default:
7480 return NULL_TREE;
7484 /* Returns true if we can interpret the contents of a native encoding
7485 as TYPE. */
7487 static bool
7488 can_native_interpret_type_p (tree type)
7490 switch (TREE_CODE (type))
7492 case INTEGER_TYPE:
7493 case ENUMERAL_TYPE:
7494 case BOOLEAN_TYPE:
7495 case POINTER_TYPE:
7496 case REFERENCE_TYPE:
7497 case FIXED_POINT_TYPE:
7498 case REAL_TYPE:
7499 case COMPLEX_TYPE:
7500 case VECTOR_TYPE:
7501 return true;
7502 default:
7503 return false;
7507 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7508 TYPE at compile-time. If we're unable to perform the conversion
7509 return NULL_TREE. */
7511 static tree
7512 fold_view_convert_expr (tree type, tree expr)
7514 /* We support up to 512-bit values (for V8DFmode). */
7515 unsigned char buffer[64];
7516 int len;
7518 /* Check that the host and target are sane. */
7519 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7520 return NULL_TREE;
7522 len = native_encode_expr (expr, buffer, sizeof (buffer));
7523 if (len == 0)
7524 return NULL_TREE;
7526 return native_interpret_expr (type, buffer, len);
7529 /* Build an expression for the address of T. Folds away INDIRECT_REF
7530 to avoid confusing the gimplify process. */
7532 tree
7533 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7535 /* The size of the object is not relevant when talking about its address. */
7536 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7537 t = TREE_OPERAND (t, 0);
7539 if (TREE_CODE (t) == INDIRECT_REF)
7541 t = TREE_OPERAND (t, 0);
7543 if (TREE_TYPE (t) != ptrtype)
7544 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7546 else if (TREE_CODE (t) == MEM_REF
7547 && integer_zerop (TREE_OPERAND (t, 1)))
7548 return TREE_OPERAND (t, 0);
7549 else if (TREE_CODE (t) == MEM_REF
7550 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7551 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7552 TREE_OPERAND (t, 0),
7553 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7554 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7556 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7558 if (TREE_TYPE (t) != ptrtype)
7559 t = fold_convert_loc (loc, ptrtype, t);
7561 else
7562 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7564 return t;
7567 /* Build an expression for the address of T. */
7569 tree
7570 build_fold_addr_expr_loc (location_t loc, tree t)
7572 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7574 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7577 /* Fold a unary expression of code CODE and type TYPE with operand
7578 OP0. Return the folded expression if folding is successful.
7579 Otherwise, return NULL_TREE. */
7581 tree
7582 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7584 tree tem;
7585 tree arg0;
7586 enum tree_code_class kind = TREE_CODE_CLASS (code);
7588 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7589 && TREE_CODE_LENGTH (code) == 1);
7591 arg0 = op0;
7592 if (arg0)
7594 if (CONVERT_EXPR_CODE_P (code)
7595 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7597 /* Don't use STRIP_NOPS, because signedness of argument type
7598 matters. */
7599 STRIP_SIGN_NOPS (arg0);
7601 else
7603 /* Strip any conversions that don't change the mode. This
7604 is safe for every expression, except for a comparison
7605 expression because its signedness is derived from its
7606 operands.
7608 Note that this is done as an internal manipulation within
7609 the constant folder, in order to find the simplest
7610 representation of the arguments so that their form can be
7611 studied. In any cases, the appropriate type conversions
7612 should be put back in the tree that will get out of the
7613 constant folder. */
7614 STRIP_NOPS (arg0);
7617 if (CONSTANT_CLASS_P (arg0))
7619 tree tem = const_unop (code, type, arg0);
7620 if (tem)
7622 if (TREE_TYPE (tem) != type)
7623 tem = fold_convert_loc (loc, type, tem);
7624 return tem;
7629 tem = generic_simplify (loc, code, type, op0);
7630 if (tem)
7631 return tem;
7633 if (TREE_CODE_CLASS (code) == tcc_unary)
7635 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7636 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7637 fold_build1_loc (loc, code, type,
7638 fold_convert_loc (loc, TREE_TYPE (op0),
7639 TREE_OPERAND (arg0, 1))));
7640 else if (TREE_CODE (arg0) == COND_EXPR)
7642 tree arg01 = TREE_OPERAND (arg0, 1);
7643 tree arg02 = TREE_OPERAND (arg0, 2);
7644 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7645 arg01 = fold_build1_loc (loc, code, type,
7646 fold_convert_loc (loc,
7647 TREE_TYPE (op0), arg01));
7648 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7649 arg02 = fold_build1_loc (loc, code, type,
7650 fold_convert_loc (loc,
7651 TREE_TYPE (op0), arg02));
7652 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7653 arg01, arg02);
7655 /* If this was a conversion, and all we did was to move into
7656 inside the COND_EXPR, bring it back out. But leave it if
7657 it is a conversion from integer to integer and the
7658 result precision is no wider than a word since such a
7659 conversion is cheap and may be optimized away by combine,
7660 while it couldn't if it were outside the COND_EXPR. Then return
7661 so we don't get into an infinite recursion loop taking the
7662 conversion out and then back in. */
7664 if ((CONVERT_EXPR_CODE_P (code)
7665 || code == NON_LVALUE_EXPR)
7666 && TREE_CODE (tem) == COND_EXPR
7667 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7668 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7669 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7670 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7671 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7672 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7673 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7674 && (INTEGRAL_TYPE_P
7675 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7676 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7677 || flag_syntax_only))
7678 tem = build1_loc (loc, code, type,
7679 build3 (COND_EXPR,
7680 TREE_TYPE (TREE_OPERAND
7681 (TREE_OPERAND (tem, 1), 0)),
7682 TREE_OPERAND (tem, 0),
7683 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7684 TREE_OPERAND (TREE_OPERAND (tem, 2),
7685 0)));
7686 return tem;
7690 switch (code)
7692 case NON_LVALUE_EXPR:
7693 if (!maybe_lvalue_p (op0))
7694 return fold_convert_loc (loc, type, op0);
7695 return NULL_TREE;
7697 CASE_CONVERT:
7698 case FLOAT_EXPR:
7699 case FIX_TRUNC_EXPR:
7700 if (COMPARISON_CLASS_P (op0))
7702 /* If we have (type) (a CMP b) and type is an integral type, return
7703 new expression involving the new type. Canonicalize
7704 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7705 non-integral type.
7706 Do not fold the result as that would not simplify further, also
7707 folding again results in recursions. */
7708 if (TREE_CODE (type) == BOOLEAN_TYPE)
7709 return build2_loc (loc, TREE_CODE (op0), type,
7710 TREE_OPERAND (op0, 0),
7711 TREE_OPERAND (op0, 1));
7712 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7713 && TREE_CODE (type) != VECTOR_TYPE)
7714 return build3_loc (loc, COND_EXPR, type, op0,
7715 constant_boolean_node (true, type),
7716 constant_boolean_node (false, type));
7719 /* Handle (T *)&A.B.C for A being of type T and B and C
7720 living at offset zero. This occurs frequently in
7721 C++ upcasting and then accessing the base. */
7722 if (TREE_CODE (op0) == ADDR_EXPR
7723 && POINTER_TYPE_P (type)
7724 && handled_component_p (TREE_OPERAND (op0, 0)))
7726 HOST_WIDE_INT bitsize, bitpos;
7727 tree offset;
7728 machine_mode mode;
7729 int unsignedp, reversep, volatilep;
7730 tree base
7731 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7732 &offset, &mode, &unsignedp, &reversep,
7733 &volatilep);
7734 /* If the reference was to a (constant) zero offset, we can use
7735 the address of the base if it has the same base type
7736 as the result type and the pointer type is unqualified. */
7737 if (! offset && bitpos == 0
7738 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7739 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7740 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7741 return fold_convert_loc (loc, type,
7742 build_fold_addr_expr_loc (loc, base));
7745 if (TREE_CODE (op0) == MODIFY_EXPR
7746 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7747 /* Detect assigning a bitfield. */
7748 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7749 && DECL_BIT_FIELD
7750 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7752 /* Don't leave an assignment inside a conversion
7753 unless assigning a bitfield. */
7754 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7755 /* First do the assignment, then return converted constant. */
7756 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7757 TREE_NO_WARNING (tem) = 1;
7758 TREE_USED (tem) = 1;
7759 return tem;
7762 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7763 constants (if x has signed type, the sign bit cannot be set
7764 in c). This folds extension into the BIT_AND_EXPR.
7765 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7766 very likely don't have maximal range for their precision and this
7767 transformation effectively doesn't preserve non-maximal ranges. */
7768 if (TREE_CODE (type) == INTEGER_TYPE
7769 && TREE_CODE (op0) == BIT_AND_EXPR
7770 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7772 tree and_expr = op0;
7773 tree and0 = TREE_OPERAND (and_expr, 0);
7774 tree and1 = TREE_OPERAND (and_expr, 1);
7775 int change = 0;
7777 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7778 || (TYPE_PRECISION (type)
7779 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7780 change = 1;
7781 else if (TYPE_PRECISION (TREE_TYPE (and1))
7782 <= HOST_BITS_PER_WIDE_INT
7783 && tree_fits_uhwi_p (and1))
7785 unsigned HOST_WIDE_INT cst;
7787 cst = tree_to_uhwi (and1);
7788 cst &= HOST_WIDE_INT_M1U
7789 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7790 change = (cst == 0);
7791 if (change
7792 && !flag_syntax_only
7793 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7794 == ZERO_EXTEND))
7796 tree uns = unsigned_type_for (TREE_TYPE (and0));
7797 and0 = fold_convert_loc (loc, uns, and0);
7798 and1 = fold_convert_loc (loc, uns, and1);
7801 if (change)
7803 tem = force_fit_type (type, wi::to_widest (and1), 0,
7804 TREE_OVERFLOW (and1));
7805 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7806 fold_convert_loc (loc, type, and0), tem);
7810 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7811 cast (T1)X will fold away. We assume that this happens when X itself
7812 is a cast. */
7813 if (POINTER_TYPE_P (type)
7814 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7815 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7817 tree arg00 = TREE_OPERAND (arg0, 0);
7818 tree arg01 = TREE_OPERAND (arg0, 1);
7820 return fold_build_pointer_plus_loc
7821 (loc, fold_convert_loc (loc, type, arg00), arg01);
7824 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7825 of the same precision, and X is an integer type not narrower than
7826 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7827 if (INTEGRAL_TYPE_P (type)
7828 && TREE_CODE (op0) == BIT_NOT_EXPR
7829 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7830 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7831 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7833 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7834 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7835 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7836 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7837 fold_convert_loc (loc, type, tem));
7840 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7841 type of X and Y (integer types only). */
7842 if (INTEGRAL_TYPE_P (type)
7843 && TREE_CODE (op0) == MULT_EXPR
7844 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7845 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7847 /* Be careful not to introduce new overflows. */
7848 tree mult_type;
7849 if (TYPE_OVERFLOW_WRAPS (type))
7850 mult_type = type;
7851 else
7852 mult_type = unsigned_type_for (type);
7854 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7856 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7857 fold_convert_loc (loc, mult_type,
7858 TREE_OPERAND (op0, 0)),
7859 fold_convert_loc (loc, mult_type,
7860 TREE_OPERAND (op0, 1)));
7861 return fold_convert_loc (loc, type, tem);
7865 return NULL_TREE;
7867 case VIEW_CONVERT_EXPR:
7868 if (TREE_CODE (op0) == MEM_REF)
7870 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7871 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7872 tem = fold_build2_loc (loc, MEM_REF, type,
7873 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7874 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7875 return tem;
7878 return NULL_TREE;
7880 case NEGATE_EXPR:
7881 tem = fold_negate_expr (loc, arg0);
7882 if (tem)
7883 return fold_convert_loc (loc, type, tem);
7884 return NULL_TREE;
7886 case ABS_EXPR:
7887 /* Convert fabs((double)float) into (double)fabsf(float). */
7888 if (TREE_CODE (arg0) == NOP_EXPR
7889 && TREE_CODE (type) == REAL_TYPE)
7891 tree targ0 = strip_float_extensions (arg0);
7892 if (targ0 != arg0)
7893 return fold_convert_loc (loc, type,
7894 fold_build1_loc (loc, ABS_EXPR,
7895 TREE_TYPE (targ0),
7896 targ0));
7898 return NULL_TREE;
7900 case BIT_NOT_EXPR:
7901 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7902 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7903 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7904 fold_convert_loc (loc, type,
7905 TREE_OPERAND (arg0, 0)))))
7906 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7907 fold_convert_loc (loc, type,
7908 TREE_OPERAND (arg0, 1)));
7909 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7910 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7911 fold_convert_loc (loc, type,
7912 TREE_OPERAND (arg0, 1)))))
7913 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7914 fold_convert_loc (loc, type,
7915 TREE_OPERAND (arg0, 0)), tem);
7917 return NULL_TREE;
7919 case TRUTH_NOT_EXPR:
7920 /* Note that the operand of this must be an int
7921 and its values must be 0 or 1.
7922 ("true" is a fixed value perhaps depending on the language,
7923 but we don't handle values other than 1 correctly yet.) */
7924 tem = fold_truth_not_expr (loc, arg0);
7925 if (!tem)
7926 return NULL_TREE;
7927 return fold_convert_loc (loc, type, tem);
7929 case INDIRECT_REF:
7930 /* Fold *&X to X if X is an lvalue. */
7931 if (TREE_CODE (op0) == ADDR_EXPR)
7933 tree op00 = TREE_OPERAND (op0, 0);
7934 if ((TREE_CODE (op00) == VAR_DECL
7935 || TREE_CODE (op00) == PARM_DECL
7936 || TREE_CODE (op00) == RESULT_DECL)
7937 && !TREE_READONLY (op00))
7938 return op00;
7940 return NULL_TREE;
7942 default:
7943 return NULL_TREE;
7944 } /* switch (code) */
7948 /* If the operation was a conversion do _not_ mark a resulting constant
7949 with TREE_OVERFLOW if the original constant was not. These conversions
7950 have implementation defined behavior and retaining the TREE_OVERFLOW
7951 flag here would confuse later passes such as VRP. */
7952 tree
7953 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7954 tree type, tree op0)
7956 tree res = fold_unary_loc (loc, code, type, op0);
7957 if (res
7958 && TREE_CODE (res) == INTEGER_CST
7959 && TREE_CODE (op0) == INTEGER_CST
7960 && CONVERT_EXPR_CODE_P (code))
7961 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7963 return res;
7966 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7967 operands OP0 and OP1. LOC is the location of the resulting expression.
7968 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7969 Return the folded expression if folding is successful. Otherwise,
7970 return NULL_TREE. */
7971 static tree
7972 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7973 tree arg0, tree arg1, tree op0, tree op1)
7975 tree tem;
7977 /* We only do these simplifications if we are optimizing. */
7978 if (!optimize)
7979 return NULL_TREE;
7981 /* Check for things like (A || B) && (A || C). We can convert this
7982 to A || (B && C). Note that either operator can be any of the four
7983 truth and/or operations and the transformation will still be
7984 valid. Also note that we only care about order for the
7985 ANDIF and ORIF operators. If B contains side effects, this
7986 might change the truth-value of A. */
7987 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7988 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7989 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7990 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7991 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7992 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7994 tree a00 = TREE_OPERAND (arg0, 0);
7995 tree a01 = TREE_OPERAND (arg0, 1);
7996 tree a10 = TREE_OPERAND (arg1, 0);
7997 tree a11 = TREE_OPERAND (arg1, 1);
7998 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7999 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8000 && (code == TRUTH_AND_EXPR
8001 || code == TRUTH_OR_EXPR));
8003 if (operand_equal_p (a00, a10, 0))
8004 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8005 fold_build2_loc (loc, code, type, a01, a11));
8006 else if (commutative && operand_equal_p (a00, a11, 0))
8007 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8008 fold_build2_loc (loc, code, type, a01, a10));
8009 else if (commutative && operand_equal_p (a01, a10, 0))
8010 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8011 fold_build2_loc (loc, code, type, a00, a11));
8013 /* This case if tricky because we must either have commutative
8014 operators or else A10 must not have side-effects. */
8016 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8017 && operand_equal_p (a01, a11, 0))
8018 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8019 fold_build2_loc (loc, code, type, a00, a10),
8020 a01);
8023 /* See if we can build a range comparison. */
8024 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8025 return tem;
8027 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8028 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8030 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8031 if (tem)
8032 return fold_build2_loc (loc, code, type, tem, arg1);
8035 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8036 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8038 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8039 if (tem)
8040 return fold_build2_loc (loc, code, type, arg0, tem);
8043 /* Check for the possibility of merging component references. If our
8044 lhs is another similar operation, try to merge its rhs with our
8045 rhs. Then try to merge our lhs and rhs. */
8046 if (TREE_CODE (arg0) == code
8047 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8048 TREE_OPERAND (arg0, 1), arg1)))
8049 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8051 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8052 return tem;
8054 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8055 && (code == TRUTH_AND_EXPR
8056 || code == TRUTH_ANDIF_EXPR
8057 || code == TRUTH_OR_EXPR
8058 || code == TRUTH_ORIF_EXPR))
8060 enum tree_code ncode, icode;
8062 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8063 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8064 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8066 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8067 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8068 We don't want to pack more than two leafs to a non-IF AND/OR
8069 expression.
8070 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8071 equal to IF-CODE, then we don't want to add right-hand operand.
8072 If the inner right-hand side of left-hand operand has
8073 side-effects, or isn't simple, then we can't add to it,
8074 as otherwise we might destroy if-sequence. */
8075 if (TREE_CODE (arg0) == icode
8076 && simple_operand_p_2 (arg1)
8077 /* Needed for sequence points to handle trappings, and
8078 side-effects. */
8079 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8081 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8082 arg1);
8083 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8084 tem);
8086 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8087 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8088 else if (TREE_CODE (arg1) == icode
8089 && simple_operand_p_2 (arg0)
8090 /* Needed for sequence points to handle trappings, and
8091 side-effects. */
8092 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8094 tem = fold_build2_loc (loc, ncode, type,
8095 arg0, TREE_OPERAND (arg1, 0));
8096 return fold_build2_loc (loc, icode, type, tem,
8097 TREE_OPERAND (arg1, 1));
8099 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8100 into (A OR B).
8101 For sequence point consistancy, we need to check for trapping,
8102 and side-effects. */
8103 else if (code == icode && simple_operand_p_2 (arg0)
8104 && simple_operand_p_2 (arg1))
8105 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8108 return NULL_TREE;
8111 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8112 by changing CODE to reduce the magnitude of constants involved in
8113 ARG0 of the comparison.
8114 Returns a canonicalized comparison tree if a simplification was
8115 possible, otherwise returns NULL_TREE.
8116 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8117 valid if signed overflow is undefined. */
8119 static tree
8120 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8121 tree arg0, tree arg1,
8122 bool *strict_overflow_p)
8124 enum tree_code code0 = TREE_CODE (arg0);
8125 tree t, cst0 = NULL_TREE;
8126 int sgn0;
8128 /* Match A +- CST code arg1. We can change this only if overflow
8129 is undefined. */
8130 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8131 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8132 /* In principle pointers also have undefined overflow behavior,
8133 but that causes problems elsewhere. */
8134 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8135 && (code0 == MINUS_EXPR
8136 || code0 == PLUS_EXPR)
8137 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8138 return NULL_TREE;
8140 /* Identify the constant in arg0 and its sign. */
8141 cst0 = TREE_OPERAND (arg0, 1);
8142 sgn0 = tree_int_cst_sgn (cst0);
8144 /* Overflowed constants and zero will cause problems. */
8145 if (integer_zerop (cst0)
8146 || TREE_OVERFLOW (cst0))
8147 return NULL_TREE;
8149 /* See if we can reduce the magnitude of the constant in
8150 arg0 by changing the comparison code. */
8151 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8152 if (code == LT_EXPR
8153 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8154 code = LE_EXPR;
8155 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8156 else if (code == GT_EXPR
8157 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8158 code = GE_EXPR;
8159 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8160 else if (code == LE_EXPR
8161 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8162 code = LT_EXPR;
8163 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8164 else if (code == GE_EXPR
8165 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8166 code = GT_EXPR;
8167 else
8168 return NULL_TREE;
8169 *strict_overflow_p = true;
8171 /* Now build the constant reduced in magnitude. But not if that
8172 would produce one outside of its types range. */
8173 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8174 && ((sgn0 == 1
8175 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8176 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8177 || (sgn0 == -1
8178 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8179 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8180 return NULL_TREE;
8182 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8183 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8184 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8185 t = fold_convert (TREE_TYPE (arg1), t);
8187 return fold_build2_loc (loc, code, type, t, arg1);
8190 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8191 overflow further. Try to decrease the magnitude of constants involved
8192 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8193 and put sole constants at the second argument position.
8194 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8196 static tree
8197 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8198 tree arg0, tree arg1)
8200 tree t;
8201 bool strict_overflow_p;
8202 const char * const warnmsg = G_("assuming signed overflow does not occur "
8203 "when reducing constant in comparison");
8205 /* Try canonicalization by simplifying arg0. */
8206 strict_overflow_p = false;
8207 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8208 &strict_overflow_p);
8209 if (t)
8211 if (strict_overflow_p)
8212 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8213 return t;
8216 /* Try canonicalization by simplifying arg1 using the swapped
8217 comparison. */
8218 code = swap_tree_comparison (code);
8219 strict_overflow_p = false;
8220 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8221 &strict_overflow_p);
8222 if (t && strict_overflow_p)
8223 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8224 return t;
8227 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8228 space. This is used to avoid issuing overflow warnings for
8229 expressions like &p->x which can not wrap. */
8231 static bool
8232 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8234 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8235 return true;
8237 if (bitpos < 0)
8238 return true;
8240 wide_int wi_offset;
8241 int precision = TYPE_PRECISION (TREE_TYPE (base));
8242 if (offset == NULL_TREE)
8243 wi_offset = wi::zero (precision);
8244 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8245 return true;
8246 else
8247 wi_offset = offset;
8249 bool overflow;
8250 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8251 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8252 if (overflow)
8253 return true;
8255 if (!wi::fits_uhwi_p (total))
8256 return true;
8258 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8259 if (size <= 0)
8260 return true;
8262 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8263 array. */
8264 if (TREE_CODE (base) == ADDR_EXPR)
8266 HOST_WIDE_INT base_size;
8268 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8269 if (base_size > 0 && size < base_size)
8270 size = base_size;
8273 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8276 /* Return a positive integer when the symbol DECL is known to have
8277 a nonzero address, zero when it's known not to (e.g., it's a weak
8278 symbol), and a negative integer when the symbol is not yet in the
8279 symbol table and so whether or not its address is zero is unknown. */
8280 static int
8281 maybe_nonzero_address (tree decl)
8283 if (DECL_P (decl) && decl_in_symtab_p (decl))
8284 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8285 return symbol->nonzero_address ();
8287 return -1;
8290 /* Subroutine of fold_binary. This routine performs all of the
8291 transformations that are common to the equality/inequality
8292 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8293 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8294 fold_binary should call fold_binary. Fold a comparison with
8295 tree code CODE and type TYPE with operands OP0 and OP1. Return
8296 the folded comparison or NULL_TREE. */
8298 static tree
8299 fold_comparison (location_t loc, enum tree_code code, tree type,
8300 tree op0, tree op1)
8302 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8303 tree arg0, arg1, tem;
8305 arg0 = op0;
8306 arg1 = op1;
8308 STRIP_SIGN_NOPS (arg0);
8309 STRIP_SIGN_NOPS (arg1);
8311 /* For comparisons of pointers we can decompose it to a compile time
8312 comparison of the base objects and the offsets into the object.
8313 This requires at least one operand being an ADDR_EXPR or a
8314 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8315 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8316 && (TREE_CODE (arg0) == ADDR_EXPR
8317 || TREE_CODE (arg1) == ADDR_EXPR
8318 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8319 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8321 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8322 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8323 machine_mode mode;
8324 int volatilep, reversep, unsignedp;
8325 bool indirect_base0 = false, indirect_base1 = false;
8327 /* Get base and offset for the access. Strip ADDR_EXPR for
8328 get_inner_reference, but put it back by stripping INDIRECT_REF
8329 off the base object if possible. indirect_baseN will be true
8330 if baseN is not an address but refers to the object itself. */
8331 base0 = arg0;
8332 if (TREE_CODE (arg0) == ADDR_EXPR)
8334 base0
8335 = get_inner_reference (TREE_OPERAND (arg0, 0),
8336 &bitsize, &bitpos0, &offset0, &mode,
8337 &unsignedp, &reversep, &volatilep);
8338 if (TREE_CODE (base0) == INDIRECT_REF)
8339 base0 = TREE_OPERAND (base0, 0);
8340 else
8341 indirect_base0 = true;
8343 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8345 base0 = TREE_OPERAND (arg0, 0);
8346 STRIP_SIGN_NOPS (base0);
8347 if (TREE_CODE (base0) == ADDR_EXPR)
8349 base0
8350 = get_inner_reference (TREE_OPERAND (base0, 0),
8351 &bitsize, &bitpos0, &offset0, &mode,
8352 &unsignedp, &reversep, &volatilep);
8353 if (TREE_CODE (base0) == INDIRECT_REF)
8354 base0 = TREE_OPERAND (base0, 0);
8355 else
8356 indirect_base0 = true;
8358 if (offset0 == NULL_TREE || integer_zerop (offset0))
8359 offset0 = TREE_OPERAND (arg0, 1);
8360 else
8361 offset0 = size_binop (PLUS_EXPR, offset0,
8362 TREE_OPERAND (arg0, 1));
8363 if (TREE_CODE (offset0) == INTEGER_CST)
8365 offset_int tem = wi::sext (wi::to_offset (offset0),
8366 TYPE_PRECISION (sizetype));
8367 tem <<= LOG2_BITS_PER_UNIT;
8368 tem += bitpos0;
8369 if (wi::fits_shwi_p (tem))
8371 bitpos0 = tem.to_shwi ();
8372 offset0 = NULL_TREE;
8377 base1 = arg1;
8378 if (TREE_CODE (arg1) == ADDR_EXPR)
8380 base1
8381 = get_inner_reference (TREE_OPERAND (arg1, 0),
8382 &bitsize, &bitpos1, &offset1, &mode,
8383 &unsignedp, &reversep, &volatilep);
8384 if (TREE_CODE (base1) == INDIRECT_REF)
8385 base1 = TREE_OPERAND (base1, 0);
8386 else
8387 indirect_base1 = true;
8389 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8391 base1 = TREE_OPERAND (arg1, 0);
8392 STRIP_SIGN_NOPS (base1);
8393 if (TREE_CODE (base1) == ADDR_EXPR)
8395 base1
8396 = get_inner_reference (TREE_OPERAND (base1, 0),
8397 &bitsize, &bitpos1, &offset1, &mode,
8398 &unsignedp, &reversep, &volatilep);
8399 if (TREE_CODE (base1) == INDIRECT_REF)
8400 base1 = TREE_OPERAND (base1, 0);
8401 else
8402 indirect_base1 = true;
8404 if (offset1 == NULL_TREE || integer_zerop (offset1))
8405 offset1 = TREE_OPERAND (arg1, 1);
8406 else
8407 offset1 = size_binop (PLUS_EXPR, offset1,
8408 TREE_OPERAND (arg1, 1));
8409 if (TREE_CODE (offset1) == INTEGER_CST)
8411 offset_int tem = wi::sext (wi::to_offset (offset1),
8412 TYPE_PRECISION (sizetype));
8413 tem <<= LOG2_BITS_PER_UNIT;
8414 tem += bitpos1;
8415 if (wi::fits_shwi_p (tem))
8417 bitpos1 = tem.to_shwi ();
8418 offset1 = NULL_TREE;
8423 /* If we have equivalent bases we might be able to simplify. */
8424 if (indirect_base0 == indirect_base1
8425 && operand_equal_p (base0, base1,
8426 indirect_base0 ? OEP_ADDRESS_OF : 0))
8428 /* We can fold this expression to a constant if the non-constant
8429 offset parts are equal. */
8430 if ((offset0 == offset1
8431 || (offset0 && offset1
8432 && operand_equal_p (offset0, offset1, 0)))
8433 && (equality_code
8434 || (indirect_base0
8435 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8436 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8439 if (!equality_code
8440 && bitpos0 != bitpos1
8441 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8442 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8443 fold_overflow_warning (("assuming pointer wraparound does not "
8444 "occur when comparing P +- C1 with "
8445 "P +- C2"),
8446 WARN_STRICT_OVERFLOW_CONDITIONAL);
8448 switch (code)
8450 case EQ_EXPR:
8451 return constant_boolean_node (bitpos0 == bitpos1, type);
8452 case NE_EXPR:
8453 return constant_boolean_node (bitpos0 != bitpos1, type);
8454 case LT_EXPR:
8455 return constant_boolean_node (bitpos0 < bitpos1, type);
8456 case LE_EXPR:
8457 return constant_boolean_node (bitpos0 <= bitpos1, type);
8458 case GE_EXPR:
8459 return constant_boolean_node (bitpos0 >= bitpos1, type);
8460 case GT_EXPR:
8461 return constant_boolean_node (bitpos0 > bitpos1, type);
8462 default:;
8465 /* We can simplify the comparison to a comparison of the variable
8466 offset parts if the constant offset parts are equal.
8467 Be careful to use signed sizetype here because otherwise we
8468 mess with array offsets in the wrong way. This is possible
8469 because pointer arithmetic is restricted to retain within an
8470 object and overflow on pointer differences is undefined as of
8471 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8472 else if (bitpos0 == bitpos1
8473 && (equality_code
8474 || (indirect_base0
8475 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8476 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8478 /* By converting to signed sizetype we cover middle-end pointer
8479 arithmetic which operates on unsigned pointer types of size
8480 type size and ARRAY_REF offsets which are properly sign or
8481 zero extended from their type in case it is narrower than
8482 sizetype. */
8483 if (offset0 == NULL_TREE)
8484 offset0 = build_int_cst (ssizetype, 0);
8485 else
8486 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8487 if (offset1 == NULL_TREE)
8488 offset1 = build_int_cst (ssizetype, 0);
8489 else
8490 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8492 if (!equality_code
8493 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8494 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8495 fold_overflow_warning (("assuming pointer wraparound does not "
8496 "occur when comparing P +- C1 with "
8497 "P +- C2"),
8498 WARN_STRICT_OVERFLOW_COMPARISON);
8500 return fold_build2_loc (loc, code, type, offset0, offset1);
8503 /* For equal offsets we can simplify to a comparison of the
8504 base addresses. */
8505 else if (bitpos0 == bitpos1
8506 && (indirect_base0
8507 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8508 && (indirect_base1
8509 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8510 && ((offset0 == offset1)
8511 || (offset0 && offset1
8512 && operand_equal_p (offset0, offset1, 0))))
8514 if (indirect_base0)
8515 base0 = build_fold_addr_expr_loc (loc, base0);
8516 if (indirect_base1)
8517 base1 = build_fold_addr_expr_loc (loc, base1);
8518 return fold_build2_loc (loc, code, type, base0, base1);
8520 /* Comparison between an ordinary (non-weak) symbol and a null
8521 pointer can be eliminated since such symbols must have a non
8522 null address. In C, relational expressions between pointers
8523 to objects and null pointers are undefined. The results
8524 below follow the C++ rules with the additional property that
8525 every object pointer compares greater than a null pointer.
8527 else if (DECL_P (base0)
8528 && maybe_nonzero_address (base0) > 0
8529 /* Avoid folding references to struct members at offset 0 to
8530 prevent tests like '&ptr->firstmember == 0' from getting
8531 eliminated. When ptr is null, although the -> expression
8532 is strictly speaking invalid, GCC retains it as a matter
8533 of QoI. See PR c/44555. */
8534 && (offset0 == NULL_TREE && bitpos0 != 0)
8535 /* The caller guarantees that when one of the arguments is
8536 constant (i.e., null in this case) it is second. */
8537 && integer_zerop (arg1))
8539 switch (code)
8541 case EQ_EXPR:
8542 case LE_EXPR:
8543 case LT_EXPR:
8544 return constant_boolean_node (false, type);
8545 case GE_EXPR:
8546 case GT_EXPR:
8547 case NE_EXPR:
8548 return constant_boolean_node (true, type);
8549 default:
8550 gcc_unreachable ();
8555 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8556 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8557 the resulting offset is smaller in absolute value than the
8558 original one and has the same sign. */
8559 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8560 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8561 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8562 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8563 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8564 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8565 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8566 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8568 tree const1 = TREE_OPERAND (arg0, 1);
8569 tree const2 = TREE_OPERAND (arg1, 1);
8570 tree variable1 = TREE_OPERAND (arg0, 0);
8571 tree variable2 = TREE_OPERAND (arg1, 0);
8572 tree cst;
8573 const char * const warnmsg = G_("assuming signed overflow does not "
8574 "occur when combining constants around "
8575 "a comparison");
8577 /* Put the constant on the side where it doesn't overflow and is
8578 of lower absolute value and of same sign than before. */
8579 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8580 ? MINUS_EXPR : PLUS_EXPR,
8581 const2, const1);
8582 if (!TREE_OVERFLOW (cst)
8583 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8584 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8586 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8587 return fold_build2_loc (loc, code, type,
8588 variable1,
8589 fold_build2_loc (loc, TREE_CODE (arg1),
8590 TREE_TYPE (arg1),
8591 variable2, cst));
8594 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8595 ? MINUS_EXPR : PLUS_EXPR,
8596 const1, const2);
8597 if (!TREE_OVERFLOW (cst)
8598 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8599 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8601 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8602 return fold_build2_loc (loc, code, type,
8603 fold_build2_loc (loc, TREE_CODE (arg0),
8604 TREE_TYPE (arg0),
8605 variable1, cst),
8606 variable2);
8610 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8611 if (tem)
8612 return tem;
8614 /* If we are comparing an expression that just has comparisons
8615 of two integer values, arithmetic expressions of those comparisons,
8616 and constants, we can simplify it. There are only three cases
8617 to check: the two values can either be equal, the first can be
8618 greater, or the second can be greater. Fold the expression for
8619 those three values. Since each value must be 0 or 1, we have
8620 eight possibilities, each of which corresponds to the constant 0
8621 or 1 or one of the six possible comparisons.
8623 This handles common cases like (a > b) == 0 but also handles
8624 expressions like ((x > y) - (y > x)) > 0, which supposedly
8625 occur in macroized code. */
8627 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8629 tree cval1 = 0, cval2 = 0;
8630 int save_p = 0;
8632 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8633 /* Don't handle degenerate cases here; they should already
8634 have been handled anyway. */
8635 && cval1 != 0 && cval2 != 0
8636 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8637 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8638 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8639 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8640 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8641 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8642 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8644 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8645 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8647 /* We can't just pass T to eval_subst in case cval1 or cval2
8648 was the same as ARG1. */
8650 tree high_result
8651 = fold_build2_loc (loc, code, type,
8652 eval_subst (loc, arg0, cval1, maxval,
8653 cval2, minval),
8654 arg1);
8655 tree equal_result
8656 = fold_build2_loc (loc, code, type,
8657 eval_subst (loc, arg0, cval1, maxval,
8658 cval2, maxval),
8659 arg1);
8660 tree low_result
8661 = fold_build2_loc (loc, code, type,
8662 eval_subst (loc, arg0, cval1, minval,
8663 cval2, maxval),
8664 arg1);
8666 /* All three of these results should be 0 or 1. Confirm they are.
8667 Then use those values to select the proper code to use. */
8669 if (TREE_CODE (high_result) == INTEGER_CST
8670 && TREE_CODE (equal_result) == INTEGER_CST
8671 && TREE_CODE (low_result) == INTEGER_CST)
8673 /* Make a 3-bit mask with the high-order bit being the
8674 value for `>', the next for '=', and the low for '<'. */
8675 switch ((integer_onep (high_result) * 4)
8676 + (integer_onep (equal_result) * 2)
8677 + integer_onep (low_result))
8679 case 0:
8680 /* Always false. */
8681 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8682 case 1:
8683 code = LT_EXPR;
8684 break;
8685 case 2:
8686 code = EQ_EXPR;
8687 break;
8688 case 3:
8689 code = LE_EXPR;
8690 break;
8691 case 4:
8692 code = GT_EXPR;
8693 break;
8694 case 5:
8695 code = NE_EXPR;
8696 break;
8697 case 6:
8698 code = GE_EXPR;
8699 break;
8700 case 7:
8701 /* Always true. */
8702 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8705 if (save_p)
8707 tem = save_expr (build2 (code, type, cval1, cval2));
8708 SET_EXPR_LOCATION (tem, loc);
8709 return tem;
8711 return fold_build2_loc (loc, code, type, cval1, cval2);
8716 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8717 into a single range test. */
8718 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8719 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8720 && TREE_CODE (arg1) == INTEGER_CST
8721 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8722 && !integer_zerop (TREE_OPERAND (arg0, 1))
8723 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8724 && !TREE_OVERFLOW (arg1))
8726 tem = fold_div_compare (loc, code, type, arg0, arg1);
8727 if (tem != NULL_TREE)
8728 return tem;
8731 return NULL_TREE;
8735 /* Subroutine of fold_binary. Optimize complex multiplications of the
8736 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8737 argument EXPR represents the expression "z" of type TYPE. */
8739 static tree
8740 fold_mult_zconjz (location_t loc, tree type, tree expr)
8742 tree itype = TREE_TYPE (type);
8743 tree rpart, ipart, tem;
8745 if (TREE_CODE (expr) == COMPLEX_EXPR)
8747 rpart = TREE_OPERAND (expr, 0);
8748 ipart = TREE_OPERAND (expr, 1);
8750 else if (TREE_CODE (expr) == COMPLEX_CST)
8752 rpart = TREE_REALPART (expr);
8753 ipart = TREE_IMAGPART (expr);
8755 else
8757 expr = save_expr (expr);
8758 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8759 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8762 rpart = save_expr (rpart);
8763 ipart = save_expr (ipart);
8764 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8765 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8766 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8767 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8768 build_zero_cst (itype));
8772 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8773 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8775 static bool
8776 vec_cst_ctor_to_array (tree arg, tree *elts)
8778 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8780 if (TREE_CODE (arg) == VECTOR_CST)
8782 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8783 elts[i] = VECTOR_CST_ELT (arg, i);
8785 else if (TREE_CODE (arg) == CONSTRUCTOR)
8787 constructor_elt *elt;
8789 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8790 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8791 return false;
8792 else
8793 elts[i] = elt->value;
8795 else
8796 return false;
8797 for (; i < nelts; i++)
8798 elts[i]
8799 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8800 return true;
8803 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8804 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8805 NULL_TREE otherwise. */
8807 static tree
8808 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8810 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8811 tree *elts;
8812 bool need_ctor = false;
8814 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8815 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8816 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8817 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8818 return NULL_TREE;
8820 elts = XALLOCAVEC (tree, nelts * 3);
8821 if (!vec_cst_ctor_to_array (arg0, elts)
8822 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8823 return NULL_TREE;
8825 for (i = 0; i < nelts; i++)
8827 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8828 need_ctor = true;
8829 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8832 if (need_ctor)
8834 vec<constructor_elt, va_gc> *v;
8835 vec_alloc (v, nelts);
8836 for (i = 0; i < nelts; i++)
8837 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8838 return build_constructor (type, v);
8840 else
8841 return build_vector (type, &elts[2 * nelts]);
8844 /* Try to fold a pointer difference of type TYPE two address expressions of
8845 array references AREF0 and AREF1 using location LOC. Return a
8846 simplified expression for the difference or NULL_TREE. */
8848 static tree
8849 fold_addr_of_array_ref_difference (location_t loc, tree type,
8850 tree aref0, tree aref1)
8852 tree base0 = TREE_OPERAND (aref0, 0);
8853 tree base1 = TREE_OPERAND (aref1, 0);
8854 tree base_offset = build_int_cst (type, 0);
8856 /* If the bases are array references as well, recurse. If the bases
8857 are pointer indirections compute the difference of the pointers.
8858 If the bases are equal, we are set. */
8859 if ((TREE_CODE (base0) == ARRAY_REF
8860 && TREE_CODE (base1) == ARRAY_REF
8861 && (base_offset
8862 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8863 || (INDIRECT_REF_P (base0)
8864 && INDIRECT_REF_P (base1)
8865 && (base_offset
8866 = fold_binary_loc (loc, MINUS_EXPR, type,
8867 fold_convert (type, TREE_OPERAND (base0, 0)),
8868 fold_convert (type,
8869 TREE_OPERAND (base1, 0)))))
8870 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8872 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8873 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8874 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8875 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8876 return fold_build2_loc (loc, PLUS_EXPR, type,
8877 base_offset,
8878 fold_build2_loc (loc, MULT_EXPR, type,
8879 diff, esz));
8881 return NULL_TREE;
8884 /* If the real or vector real constant CST of type TYPE has an exact
8885 inverse, return it, else return NULL. */
8887 tree
8888 exact_inverse (tree type, tree cst)
8890 REAL_VALUE_TYPE r;
8891 tree unit_type, *elts;
8892 machine_mode mode;
8893 unsigned vec_nelts, i;
8895 switch (TREE_CODE (cst))
8897 case REAL_CST:
8898 r = TREE_REAL_CST (cst);
8900 if (exact_real_inverse (TYPE_MODE (type), &r))
8901 return build_real (type, r);
8903 return NULL_TREE;
8905 case VECTOR_CST:
8906 vec_nelts = VECTOR_CST_NELTS (cst);
8907 elts = XALLOCAVEC (tree, vec_nelts);
8908 unit_type = TREE_TYPE (type);
8909 mode = TYPE_MODE (unit_type);
8911 for (i = 0; i < vec_nelts; i++)
8913 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8914 if (!exact_real_inverse (mode, &r))
8915 return NULL_TREE;
8916 elts[i] = build_real (unit_type, r);
8919 return build_vector (type, elts);
8921 default:
8922 return NULL_TREE;
8926 /* Mask out the tz least significant bits of X of type TYPE where
8927 tz is the number of trailing zeroes in Y. */
8928 static wide_int
8929 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8931 int tz = wi::ctz (y);
8932 if (tz > 0)
8933 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8934 return x;
8937 /* Return true when T is an address and is known to be nonzero.
8938 For floating point we further ensure that T is not denormal.
8939 Similar logic is present in nonzero_address in rtlanal.h.
8941 If the return value is based on the assumption that signed overflow
8942 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8943 change *STRICT_OVERFLOW_P. */
8945 static bool
8946 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8948 tree type = TREE_TYPE (t);
8949 enum tree_code code;
8951 /* Doing something useful for floating point would need more work. */
8952 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8953 return false;
8955 code = TREE_CODE (t);
8956 switch (TREE_CODE_CLASS (code))
8958 case tcc_unary:
8959 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8960 strict_overflow_p);
8961 case tcc_binary:
8962 case tcc_comparison:
8963 return tree_binary_nonzero_warnv_p (code, type,
8964 TREE_OPERAND (t, 0),
8965 TREE_OPERAND (t, 1),
8966 strict_overflow_p);
8967 case tcc_constant:
8968 case tcc_declaration:
8969 case tcc_reference:
8970 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8972 default:
8973 break;
8976 switch (code)
8978 case TRUTH_NOT_EXPR:
8979 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8980 strict_overflow_p);
8982 case TRUTH_AND_EXPR:
8983 case TRUTH_OR_EXPR:
8984 case TRUTH_XOR_EXPR:
8985 return tree_binary_nonzero_warnv_p (code, type,
8986 TREE_OPERAND (t, 0),
8987 TREE_OPERAND (t, 1),
8988 strict_overflow_p);
8990 case COND_EXPR:
8991 case CONSTRUCTOR:
8992 case OBJ_TYPE_REF:
8993 case ASSERT_EXPR:
8994 case ADDR_EXPR:
8995 case WITH_SIZE_EXPR:
8996 case SSA_NAME:
8997 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8999 case COMPOUND_EXPR:
9000 case MODIFY_EXPR:
9001 case BIND_EXPR:
9002 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9003 strict_overflow_p);
9005 case SAVE_EXPR:
9006 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9007 strict_overflow_p);
9009 case CALL_EXPR:
9011 tree fndecl = get_callee_fndecl (t);
9012 if (!fndecl) return false;
9013 if (flag_delete_null_pointer_checks && !flag_check_new
9014 && DECL_IS_OPERATOR_NEW (fndecl)
9015 && !TREE_NOTHROW (fndecl))
9016 return true;
9017 if (flag_delete_null_pointer_checks
9018 && lookup_attribute ("returns_nonnull",
9019 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9020 return true;
9021 return alloca_call_p (t);
9024 default:
9025 break;
9027 return false;
9030 /* Return true when T is an address and is known to be nonzero.
9031 Handle warnings about undefined signed overflow. */
9033 static bool
9034 tree_expr_nonzero_p (tree t)
9036 bool ret, strict_overflow_p;
9038 strict_overflow_p = false;
9039 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9040 if (strict_overflow_p)
9041 fold_overflow_warning (("assuming signed overflow does not occur when "
9042 "determining that expression is always "
9043 "non-zero"),
9044 WARN_STRICT_OVERFLOW_MISC);
9045 return ret;
9048 /* Return true if T is known not to be equal to an integer W. */
9050 bool
9051 expr_not_equal_to (tree t, const wide_int &w)
9053 wide_int min, max, nz;
9054 value_range_type rtype;
9055 switch (TREE_CODE (t))
9057 case INTEGER_CST:
9058 return wi::ne_p (t, w);
9060 case SSA_NAME:
9061 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9062 return false;
9063 rtype = get_range_info (t, &min, &max);
9064 if (rtype == VR_RANGE)
9066 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9067 return true;
9068 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9069 return true;
9071 else if (rtype == VR_ANTI_RANGE
9072 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9073 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9074 return true;
9075 /* If T has some known zero bits and W has any of those bits set,
9076 then T is known not to be equal to W. */
9077 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9078 TYPE_PRECISION (TREE_TYPE (t))), 0))
9079 return true;
9080 return false;
9082 default:
9083 return false;
9087 /* Fold a binary expression of code CODE and type TYPE with operands
9088 OP0 and OP1. LOC is the location of the resulting expression.
9089 Return the folded expression if folding is successful. Otherwise,
9090 return NULL_TREE. */
9092 tree
9093 fold_binary_loc (location_t loc,
9094 enum tree_code code, tree type, tree op0, tree op1)
9096 enum tree_code_class kind = TREE_CODE_CLASS (code);
9097 tree arg0, arg1, tem;
9098 tree t1 = NULL_TREE;
9099 bool strict_overflow_p;
9100 unsigned int prec;
9102 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9103 && TREE_CODE_LENGTH (code) == 2
9104 && op0 != NULL_TREE
9105 && op1 != NULL_TREE);
9107 arg0 = op0;
9108 arg1 = op1;
9110 /* Strip any conversions that don't change the mode. This is
9111 safe for every expression, except for a comparison expression
9112 because its signedness is derived from its operands. So, in
9113 the latter case, only strip conversions that don't change the
9114 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9115 preserved.
9117 Note that this is done as an internal manipulation within the
9118 constant folder, in order to find the simplest representation
9119 of the arguments so that their form can be studied. In any
9120 cases, the appropriate type conversions should be put back in
9121 the tree that will get out of the constant folder. */
9123 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9125 STRIP_SIGN_NOPS (arg0);
9126 STRIP_SIGN_NOPS (arg1);
9128 else
9130 STRIP_NOPS (arg0);
9131 STRIP_NOPS (arg1);
9134 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9135 constant but we can't do arithmetic on them. */
9136 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9138 tem = const_binop (code, type, arg0, arg1);
9139 if (tem != NULL_TREE)
9141 if (TREE_TYPE (tem) != type)
9142 tem = fold_convert_loc (loc, type, tem);
9143 return tem;
9147 /* If this is a commutative operation, and ARG0 is a constant, move it
9148 to ARG1 to reduce the number of tests below. */
9149 if (commutative_tree_code (code)
9150 && tree_swap_operands_p (arg0, arg1, true))
9151 return fold_build2_loc (loc, code, type, op1, op0);
9153 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9154 to ARG1 to reduce the number of tests below. */
9155 if (kind == tcc_comparison
9156 && tree_swap_operands_p (arg0, arg1, true))
9157 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9159 tem = generic_simplify (loc, code, type, op0, op1);
9160 if (tem)
9161 return tem;
9163 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9165 First check for cases where an arithmetic operation is applied to a
9166 compound, conditional, or comparison operation. Push the arithmetic
9167 operation inside the compound or conditional to see if any folding
9168 can then be done. Convert comparison to conditional for this purpose.
9169 The also optimizes non-constant cases that used to be done in
9170 expand_expr.
9172 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9173 one of the operands is a comparison and the other is a comparison, a
9174 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9175 code below would make the expression more complex. Change it to a
9176 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9177 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9179 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9180 || code == EQ_EXPR || code == NE_EXPR)
9181 && TREE_CODE (type) != VECTOR_TYPE
9182 && ((truth_value_p (TREE_CODE (arg0))
9183 && (truth_value_p (TREE_CODE (arg1))
9184 || (TREE_CODE (arg1) == BIT_AND_EXPR
9185 && integer_onep (TREE_OPERAND (arg1, 1)))))
9186 || (truth_value_p (TREE_CODE (arg1))
9187 && (truth_value_p (TREE_CODE (arg0))
9188 || (TREE_CODE (arg0) == BIT_AND_EXPR
9189 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9191 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9192 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9193 : TRUTH_XOR_EXPR,
9194 boolean_type_node,
9195 fold_convert_loc (loc, boolean_type_node, arg0),
9196 fold_convert_loc (loc, boolean_type_node, arg1));
9198 if (code == EQ_EXPR)
9199 tem = invert_truthvalue_loc (loc, tem);
9201 return fold_convert_loc (loc, type, tem);
9204 if (TREE_CODE_CLASS (code) == tcc_binary
9205 || TREE_CODE_CLASS (code) == tcc_comparison)
9207 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9209 tem = fold_build2_loc (loc, code, type,
9210 fold_convert_loc (loc, TREE_TYPE (op0),
9211 TREE_OPERAND (arg0, 1)), op1);
9212 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9213 tem);
9215 if (TREE_CODE (arg1) == COMPOUND_EXPR
9216 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9218 tem = fold_build2_loc (loc, code, type, op0,
9219 fold_convert_loc (loc, TREE_TYPE (op1),
9220 TREE_OPERAND (arg1, 1)));
9221 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9222 tem);
9225 if (TREE_CODE (arg0) == COND_EXPR
9226 || TREE_CODE (arg0) == VEC_COND_EXPR
9227 || COMPARISON_CLASS_P (arg0))
9229 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9230 arg0, arg1,
9231 /*cond_first_p=*/1);
9232 if (tem != NULL_TREE)
9233 return tem;
9236 if (TREE_CODE (arg1) == COND_EXPR
9237 || TREE_CODE (arg1) == VEC_COND_EXPR
9238 || COMPARISON_CLASS_P (arg1))
9240 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9241 arg1, arg0,
9242 /*cond_first_p=*/0);
9243 if (tem != NULL_TREE)
9244 return tem;
9248 switch (code)
9250 case MEM_REF:
9251 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9252 if (TREE_CODE (arg0) == ADDR_EXPR
9253 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9255 tree iref = TREE_OPERAND (arg0, 0);
9256 return fold_build2 (MEM_REF, type,
9257 TREE_OPERAND (iref, 0),
9258 int_const_binop (PLUS_EXPR, arg1,
9259 TREE_OPERAND (iref, 1)));
9262 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9263 if (TREE_CODE (arg0) == ADDR_EXPR
9264 && handled_component_p (TREE_OPERAND (arg0, 0)))
9266 tree base;
9267 HOST_WIDE_INT coffset;
9268 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9269 &coffset);
9270 if (!base)
9271 return NULL_TREE;
9272 return fold_build2 (MEM_REF, type,
9273 build_fold_addr_expr (base),
9274 int_const_binop (PLUS_EXPR, arg1,
9275 size_int (coffset)));
9278 return NULL_TREE;
9280 case POINTER_PLUS_EXPR:
9281 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9282 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9283 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9284 return fold_convert_loc (loc, type,
9285 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9286 fold_convert_loc (loc, sizetype,
9287 arg1),
9288 fold_convert_loc (loc, sizetype,
9289 arg0)));
9291 return NULL_TREE;
9293 case PLUS_EXPR:
9294 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9296 /* X + (X / CST) * -CST is X % CST. */
9297 if (TREE_CODE (arg1) == MULT_EXPR
9298 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9299 && operand_equal_p (arg0,
9300 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9302 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9303 tree cst1 = TREE_OPERAND (arg1, 1);
9304 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9305 cst1, cst0);
9306 if (sum && integer_zerop (sum))
9307 return fold_convert_loc (loc, type,
9308 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9309 TREE_TYPE (arg0), arg0,
9310 cst0));
9314 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9315 one. Make sure the type is not saturating and has the signedness of
9316 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9317 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9318 if ((TREE_CODE (arg0) == MULT_EXPR
9319 || TREE_CODE (arg1) == MULT_EXPR)
9320 && !TYPE_SATURATING (type)
9321 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9322 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9323 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9325 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9326 if (tem)
9327 return tem;
9330 if (! FLOAT_TYPE_P (type))
9332 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9333 (plus (plus (mult) (mult)) (foo)) so that we can
9334 take advantage of the factoring cases below. */
9335 if (ANY_INTEGRAL_TYPE_P (type)
9336 && TYPE_OVERFLOW_WRAPS (type)
9337 && (((TREE_CODE (arg0) == PLUS_EXPR
9338 || TREE_CODE (arg0) == MINUS_EXPR)
9339 && TREE_CODE (arg1) == MULT_EXPR)
9340 || ((TREE_CODE (arg1) == PLUS_EXPR
9341 || TREE_CODE (arg1) == MINUS_EXPR)
9342 && TREE_CODE (arg0) == MULT_EXPR)))
9344 tree parg0, parg1, parg, marg;
9345 enum tree_code pcode;
9347 if (TREE_CODE (arg1) == MULT_EXPR)
9348 parg = arg0, marg = arg1;
9349 else
9350 parg = arg1, marg = arg0;
9351 pcode = TREE_CODE (parg);
9352 parg0 = TREE_OPERAND (parg, 0);
9353 parg1 = TREE_OPERAND (parg, 1);
9354 STRIP_NOPS (parg0);
9355 STRIP_NOPS (parg1);
9357 if (TREE_CODE (parg0) == MULT_EXPR
9358 && TREE_CODE (parg1) != MULT_EXPR)
9359 return fold_build2_loc (loc, pcode, type,
9360 fold_build2_loc (loc, PLUS_EXPR, type,
9361 fold_convert_loc (loc, type,
9362 parg0),
9363 fold_convert_loc (loc, type,
9364 marg)),
9365 fold_convert_loc (loc, type, parg1));
9366 if (TREE_CODE (parg0) != MULT_EXPR
9367 && TREE_CODE (parg1) == MULT_EXPR)
9368 return
9369 fold_build2_loc (loc, PLUS_EXPR, type,
9370 fold_convert_loc (loc, type, parg0),
9371 fold_build2_loc (loc, pcode, type,
9372 fold_convert_loc (loc, type, marg),
9373 fold_convert_loc (loc, type,
9374 parg1)));
9377 else
9379 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9380 to __complex__ ( x, y ). This is not the same for SNaNs or
9381 if signed zeros are involved. */
9382 if (!HONOR_SNANS (element_mode (arg0))
9383 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9384 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9386 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9387 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9388 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9389 bool arg0rz = false, arg0iz = false;
9390 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9391 || (arg0i && (arg0iz = real_zerop (arg0i))))
9393 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9394 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9395 if (arg0rz && arg1i && real_zerop (arg1i))
9397 tree rp = arg1r ? arg1r
9398 : build1 (REALPART_EXPR, rtype, arg1);
9399 tree ip = arg0i ? arg0i
9400 : build1 (IMAGPART_EXPR, rtype, arg0);
9401 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9403 else if (arg0iz && arg1r && real_zerop (arg1r))
9405 tree rp = arg0r ? arg0r
9406 : build1 (REALPART_EXPR, rtype, arg0);
9407 tree ip = arg1i ? arg1i
9408 : build1 (IMAGPART_EXPR, rtype, arg1);
9409 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9414 if (flag_unsafe_math_optimizations
9415 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9416 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9417 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9418 return tem;
9420 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9421 We associate floats only if the user has specified
9422 -fassociative-math. */
9423 if (flag_associative_math
9424 && TREE_CODE (arg1) == PLUS_EXPR
9425 && TREE_CODE (arg0) != MULT_EXPR)
9427 tree tree10 = TREE_OPERAND (arg1, 0);
9428 tree tree11 = TREE_OPERAND (arg1, 1);
9429 if (TREE_CODE (tree11) == MULT_EXPR
9430 && TREE_CODE (tree10) == MULT_EXPR)
9432 tree tree0;
9433 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9434 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9437 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9438 We associate floats only if the user has specified
9439 -fassociative-math. */
9440 if (flag_associative_math
9441 && TREE_CODE (arg0) == PLUS_EXPR
9442 && TREE_CODE (arg1) != MULT_EXPR)
9444 tree tree00 = TREE_OPERAND (arg0, 0);
9445 tree tree01 = TREE_OPERAND (arg0, 1);
9446 if (TREE_CODE (tree01) == MULT_EXPR
9447 && TREE_CODE (tree00) == MULT_EXPR)
9449 tree tree0;
9450 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9451 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9456 bit_rotate:
9457 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9458 is a rotate of A by C1 bits. */
9459 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9460 is a rotate of A by B bits. */
9462 enum tree_code code0, code1;
9463 tree rtype;
9464 code0 = TREE_CODE (arg0);
9465 code1 = TREE_CODE (arg1);
9466 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9467 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9468 && operand_equal_p (TREE_OPERAND (arg0, 0),
9469 TREE_OPERAND (arg1, 0), 0)
9470 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9471 TYPE_UNSIGNED (rtype))
9472 /* Only create rotates in complete modes. Other cases are not
9473 expanded properly. */
9474 && (element_precision (rtype)
9475 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9477 tree tree01, tree11;
9478 enum tree_code code01, code11;
9480 tree01 = TREE_OPERAND (arg0, 1);
9481 tree11 = TREE_OPERAND (arg1, 1);
9482 STRIP_NOPS (tree01);
9483 STRIP_NOPS (tree11);
9484 code01 = TREE_CODE (tree01);
9485 code11 = TREE_CODE (tree11);
9486 if (code01 == INTEGER_CST
9487 && code11 == INTEGER_CST
9488 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9489 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9491 tem = build2_loc (loc, LROTATE_EXPR,
9492 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9493 TREE_OPERAND (arg0, 0),
9494 code0 == LSHIFT_EXPR
9495 ? TREE_OPERAND (arg0, 1)
9496 : TREE_OPERAND (arg1, 1));
9497 return fold_convert_loc (loc, type, tem);
9499 else if (code11 == MINUS_EXPR)
9501 tree tree110, tree111;
9502 tree110 = TREE_OPERAND (tree11, 0);
9503 tree111 = TREE_OPERAND (tree11, 1);
9504 STRIP_NOPS (tree110);
9505 STRIP_NOPS (tree111);
9506 if (TREE_CODE (tree110) == INTEGER_CST
9507 && 0 == compare_tree_int (tree110,
9508 element_precision
9509 (TREE_TYPE (TREE_OPERAND
9510 (arg0, 0))))
9511 && operand_equal_p (tree01, tree111, 0))
9512 return
9513 fold_convert_loc (loc, type,
9514 build2 ((code0 == LSHIFT_EXPR
9515 ? LROTATE_EXPR
9516 : RROTATE_EXPR),
9517 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9518 TREE_OPERAND (arg0, 0),
9519 TREE_OPERAND (arg0, 1)));
9521 else if (code01 == MINUS_EXPR)
9523 tree tree010, tree011;
9524 tree010 = TREE_OPERAND (tree01, 0);
9525 tree011 = TREE_OPERAND (tree01, 1);
9526 STRIP_NOPS (tree010);
9527 STRIP_NOPS (tree011);
9528 if (TREE_CODE (tree010) == INTEGER_CST
9529 && 0 == compare_tree_int (tree010,
9530 element_precision
9531 (TREE_TYPE (TREE_OPERAND
9532 (arg0, 0))))
9533 && operand_equal_p (tree11, tree011, 0))
9534 return fold_convert_loc
9535 (loc, type,
9536 build2 ((code0 != LSHIFT_EXPR
9537 ? LROTATE_EXPR
9538 : RROTATE_EXPR),
9539 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9540 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9545 associate:
9546 /* In most languages, can't associate operations on floats through
9547 parentheses. Rather than remember where the parentheses were, we
9548 don't associate floats at all, unless the user has specified
9549 -fassociative-math.
9550 And, we need to make sure type is not saturating. */
9552 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9553 && !TYPE_SATURATING (type))
9555 tree var0, con0, lit0, minus_lit0;
9556 tree var1, con1, lit1, minus_lit1;
9557 tree atype = type;
9558 bool ok = true;
9560 /* Split both trees into variables, constants, and literals. Then
9561 associate each group together, the constants with literals,
9562 then the result with variables. This increases the chances of
9563 literals being recombined later and of generating relocatable
9564 expressions for the sum of a constant and literal. */
9565 var0 = split_tree (loc, arg0, type, code,
9566 &con0, &lit0, &minus_lit0, 0);
9567 var1 = split_tree (loc, arg1, type, code,
9568 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9570 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9571 if (code == MINUS_EXPR)
9572 code = PLUS_EXPR;
9574 /* With undefined overflow prefer doing association in a type
9575 which wraps on overflow, if that is one of the operand types. */
9576 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9577 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9579 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9580 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9581 atype = TREE_TYPE (arg0);
9582 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9583 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9584 atype = TREE_TYPE (arg1);
9585 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9588 /* With undefined overflow we can only associate constants with one
9589 variable, and constants whose association doesn't overflow. */
9590 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9591 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9593 if (var0 && var1)
9595 tree tmp0 = var0;
9596 tree tmp1 = var1;
9597 bool one_neg = false;
9599 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9601 tmp0 = TREE_OPERAND (tmp0, 0);
9602 one_neg = !one_neg;
9604 if (CONVERT_EXPR_P (tmp0)
9605 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9606 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9607 <= TYPE_PRECISION (atype)))
9608 tmp0 = TREE_OPERAND (tmp0, 0);
9609 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9611 tmp1 = TREE_OPERAND (tmp1, 0);
9612 one_neg = !one_neg;
9614 if (CONVERT_EXPR_P (tmp1)
9615 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9616 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9617 <= TYPE_PRECISION (atype)))
9618 tmp1 = TREE_OPERAND (tmp1, 0);
9619 /* The only case we can still associate with two variables
9620 is if they cancel out. */
9621 if (!one_neg
9622 || !operand_equal_p (tmp0, tmp1, 0))
9623 ok = false;
9627 /* Only do something if we found more than two objects. Otherwise,
9628 nothing has changed and we risk infinite recursion. */
9629 if (ok
9630 && (2 < ((var0 != 0) + (var1 != 0)
9631 + (con0 != 0) + (con1 != 0)
9632 + (lit0 != 0) + (lit1 != 0)
9633 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9635 bool any_overflows = false;
9636 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9637 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9638 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9639 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9640 var0 = associate_trees (loc, var0, var1, code, atype);
9641 con0 = associate_trees (loc, con0, con1, code, atype);
9642 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9643 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9644 code, atype);
9646 /* Preserve the MINUS_EXPR if the negative part of the literal is
9647 greater than the positive part. Otherwise, the multiplicative
9648 folding code (i.e extract_muldiv) may be fooled in case
9649 unsigned constants are subtracted, like in the following
9650 example: ((X*2 + 4) - 8U)/2. */
9651 if (minus_lit0 && lit0)
9653 if (TREE_CODE (lit0) == INTEGER_CST
9654 && TREE_CODE (minus_lit0) == INTEGER_CST
9655 && tree_int_cst_lt (lit0, minus_lit0))
9657 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9658 MINUS_EXPR, atype);
9659 lit0 = 0;
9661 else
9663 lit0 = associate_trees (loc, lit0, minus_lit0,
9664 MINUS_EXPR, atype);
9665 minus_lit0 = 0;
9669 /* Don't introduce overflows through reassociation. */
9670 if (!any_overflows
9671 && ((lit0 && TREE_OVERFLOW_P (lit0))
9672 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9673 return NULL_TREE;
9675 if (minus_lit0)
9677 if (con0 == 0)
9678 return
9679 fold_convert_loc (loc, type,
9680 associate_trees (loc, var0, minus_lit0,
9681 MINUS_EXPR, atype));
9682 else
9684 con0 = associate_trees (loc, con0, minus_lit0,
9685 MINUS_EXPR, atype);
9686 return
9687 fold_convert_loc (loc, type,
9688 associate_trees (loc, var0, con0,
9689 PLUS_EXPR, atype));
9693 con0 = associate_trees (loc, con0, lit0, code, atype);
9694 return
9695 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9696 code, atype));
9700 return NULL_TREE;
9702 case MINUS_EXPR:
9703 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9704 if (TREE_CODE (arg0) == NEGATE_EXPR
9705 && negate_expr_p (op1)
9706 && reorder_operands_p (arg0, arg1))
9707 return fold_build2_loc (loc, MINUS_EXPR, type,
9708 negate_expr (op1),
9709 fold_convert_loc (loc, type,
9710 TREE_OPERAND (arg0, 0)));
9712 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9713 __complex__ ( x, -y ). This is not the same for SNaNs or if
9714 signed zeros are involved. */
9715 if (!HONOR_SNANS (element_mode (arg0))
9716 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9717 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9719 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9720 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9721 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9722 bool arg0rz = false, arg0iz = false;
9723 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9724 || (arg0i && (arg0iz = real_zerop (arg0i))))
9726 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9727 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9728 if (arg0rz && arg1i && real_zerop (arg1i))
9730 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9731 arg1r ? arg1r
9732 : build1 (REALPART_EXPR, rtype, arg1));
9733 tree ip = arg0i ? arg0i
9734 : build1 (IMAGPART_EXPR, rtype, arg0);
9735 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9737 else if (arg0iz && arg1r && real_zerop (arg1r))
9739 tree rp = arg0r ? arg0r
9740 : build1 (REALPART_EXPR, rtype, arg0);
9741 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9742 arg1i ? arg1i
9743 : build1 (IMAGPART_EXPR, rtype, arg1));
9744 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9749 /* A - B -> A + (-B) if B is easily negatable. */
9750 if (negate_expr_p (op1)
9751 && ! TYPE_OVERFLOW_SANITIZED (type)
9752 && ((FLOAT_TYPE_P (type)
9753 /* Avoid this transformation if B is a positive REAL_CST. */
9754 && (TREE_CODE (op1) != REAL_CST
9755 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9756 || INTEGRAL_TYPE_P (type)))
9757 return fold_build2_loc (loc, PLUS_EXPR, type,
9758 fold_convert_loc (loc, type, arg0),
9759 negate_expr (op1));
9761 /* Fold &a[i] - &a[j] to i-j. */
9762 if (TREE_CODE (arg0) == ADDR_EXPR
9763 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9764 && TREE_CODE (arg1) == ADDR_EXPR
9765 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9767 tree tem = fold_addr_of_array_ref_difference (loc, type,
9768 TREE_OPERAND (arg0, 0),
9769 TREE_OPERAND (arg1, 0));
9770 if (tem)
9771 return tem;
9774 if (FLOAT_TYPE_P (type)
9775 && flag_unsafe_math_optimizations
9776 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9777 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9778 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9779 return tem;
9781 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9782 one. Make sure the type is not saturating and has the signedness of
9783 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9784 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9785 if ((TREE_CODE (arg0) == MULT_EXPR
9786 || TREE_CODE (arg1) == MULT_EXPR)
9787 && !TYPE_SATURATING (type)
9788 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9789 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9790 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9792 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9793 if (tem)
9794 return tem;
9797 goto associate;
9799 case MULT_EXPR:
9800 if (! FLOAT_TYPE_P (type))
9802 /* Transform x * -C into -x * C if x is easily negatable. */
9803 if (TREE_CODE (op1) == INTEGER_CST
9804 && tree_int_cst_sgn (op1) == -1
9805 && negate_expr_p (op0)
9806 && (tem = negate_expr (op1)) != op1
9807 && ! TREE_OVERFLOW (tem))
9808 return fold_build2_loc (loc, MULT_EXPR, type,
9809 fold_convert_loc (loc, type,
9810 negate_expr (op0)), tem);
9812 strict_overflow_p = false;
9813 if (TREE_CODE (arg1) == INTEGER_CST
9814 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9815 &strict_overflow_p)))
9817 if (strict_overflow_p)
9818 fold_overflow_warning (("assuming signed overflow does not "
9819 "occur when simplifying "
9820 "multiplication"),
9821 WARN_STRICT_OVERFLOW_MISC);
9822 return fold_convert_loc (loc, type, tem);
9825 /* Optimize z * conj(z) for integer complex numbers. */
9826 if (TREE_CODE (arg0) == CONJ_EXPR
9827 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9828 return fold_mult_zconjz (loc, type, arg1);
9829 if (TREE_CODE (arg1) == CONJ_EXPR
9830 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9831 return fold_mult_zconjz (loc, type, arg0);
9833 else
9835 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9836 This is not the same for NaNs or if signed zeros are
9837 involved. */
9838 if (!HONOR_NANS (arg0)
9839 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9840 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9841 && TREE_CODE (arg1) == COMPLEX_CST
9842 && real_zerop (TREE_REALPART (arg1)))
9844 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9845 if (real_onep (TREE_IMAGPART (arg1)))
9846 return
9847 fold_build2_loc (loc, COMPLEX_EXPR, type,
9848 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9849 rtype, arg0)),
9850 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9851 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9852 return
9853 fold_build2_loc (loc, COMPLEX_EXPR, type,
9854 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9855 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9856 rtype, arg0)));
9859 /* Optimize z * conj(z) for floating point complex numbers.
9860 Guarded by flag_unsafe_math_optimizations as non-finite
9861 imaginary components don't produce scalar results. */
9862 if (flag_unsafe_math_optimizations
9863 && TREE_CODE (arg0) == CONJ_EXPR
9864 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9865 return fold_mult_zconjz (loc, type, arg1);
9866 if (flag_unsafe_math_optimizations
9867 && TREE_CODE (arg1) == CONJ_EXPR
9868 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9869 return fold_mult_zconjz (loc, type, arg0);
9871 goto associate;
9873 case BIT_IOR_EXPR:
9874 /* Canonicalize (X & C1) | C2. */
9875 if (TREE_CODE (arg0) == BIT_AND_EXPR
9876 && TREE_CODE (arg1) == INTEGER_CST
9877 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9879 int width = TYPE_PRECISION (type), w;
9880 wide_int c1 = TREE_OPERAND (arg0, 1);
9881 wide_int c2 = arg1;
9883 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9884 if ((c1 & c2) == c1)
9885 return omit_one_operand_loc (loc, type, arg1,
9886 TREE_OPERAND (arg0, 0));
9888 wide_int msk = wi::mask (width, false,
9889 TYPE_PRECISION (TREE_TYPE (arg1)));
9891 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9892 if (msk.and_not (c1 | c2) == 0)
9893 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9894 TREE_OPERAND (arg0, 0), arg1);
9896 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9897 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9898 mode which allows further optimizations. */
9899 c1 &= msk;
9900 c2 &= msk;
9901 wide_int c3 = c1.and_not (c2);
9902 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9904 wide_int mask = wi::mask (w, false,
9905 TYPE_PRECISION (type));
9906 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9908 c3 = mask;
9909 break;
9913 if (c3 != c1)
9914 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9915 fold_build2_loc (loc, BIT_AND_EXPR, type,
9916 TREE_OPERAND (arg0, 0),
9917 wide_int_to_tree (type,
9918 c3)),
9919 arg1);
9922 /* See if this can be simplified into a rotate first. If that
9923 is unsuccessful continue in the association code. */
9924 goto bit_rotate;
9926 case BIT_XOR_EXPR:
9927 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9928 if (TREE_CODE (arg0) == BIT_AND_EXPR
9929 && INTEGRAL_TYPE_P (type)
9930 && integer_onep (TREE_OPERAND (arg0, 1))
9931 && integer_onep (arg1))
9932 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9933 build_zero_cst (TREE_TYPE (arg0)));
9935 /* See if this can be simplified into a rotate first. If that
9936 is unsuccessful continue in the association code. */
9937 goto bit_rotate;
9939 case BIT_AND_EXPR:
9940 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9941 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9942 && INTEGRAL_TYPE_P (type)
9943 && integer_onep (TREE_OPERAND (arg0, 1))
9944 && integer_onep (arg1))
9946 tree tem2;
9947 tem = TREE_OPERAND (arg0, 0);
9948 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9949 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9950 tem, tem2);
9951 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9952 build_zero_cst (TREE_TYPE (tem)));
9954 /* Fold ~X & 1 as (X & 1) == 0. */
9955 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9956 && INTEGRAL_TYPE_P (type)
9957 && integer_onep (arg1))
9959 tree tem2;
9960 tem = TREE_OPERAND (arg0, 0);
9961 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9962 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9963 tem, tem2);
9964 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9965 build_zero_cst (TREE_TYPE (tem)));
9967 /* Fold !X & 1 as X == 0. */
9968 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9969 && integer_onep (arg1))
9971 tem = TREE_OPERAND (arg0, 0);
9972 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9973 build_zero_cst (TREE_TYPE (tem)));
9976 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9977 multiple of 1 << CST. */
9978 if (TREE_CODE (arg1) == INTEGER_CST)
9980 wide_int cst1 = arg1;
9981 wide_int ncst1 = -cst1;
9982 if ((cst1 & ncst1) == ncst1
9983 && multiple_of_p (type, arg0,
9984 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
9985 return fold_convert_loc (loc, type, arg0);
9988 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9989 bits from CST2. */
9990 if (TREE_CODE (arg1) == INTEGER_CST
9991 && TREE_CODE (arg0) == MULT_EXPR
9992 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9994 wide_int warg1 = arg1;
9995 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
9997 if (masked == 0)
9998 return omit_two_operands_loc (loc, type, build_zero_cst (type),
9999 arg0, arg1);
10000 else if (masked != warg1)
10002 /* Avoid the transform if arg1 is a mask of some
10003 mode which allows further optimizations. */
10004 int pop = wi::popcount (warg1);
10005 if (!(pop >= BITS_PER_UNIT
10006 && exact_log2 (pop) != -1
10007 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10008 return fold_build2_loc (loc, code, type, op0,
10009 wide_int_to_tree (type, masked));
10013 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10014 ((A & N) + B) & M -> (A + B) & M
10015 Similarly if (N & M) == 0,
10016 ((A | N) + B) & M -> (A + B) & M
10017 and for - instead of + (or unary - instead of +)
10018 and/or ^ instead of |.
10019 If B is constant and (B & M) == 0, fold into A & M. */
10020 if (TREE_CODE (arg1) == INTEGER_CST)
10022 wide_int cst1 = arg1;
10023 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10024 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10025 && (TREE_CODE (arg0) == PLUS_EXPR
10026 || TREE_CODE (arg0) == MINUS_EXPR
10027 || TREE_CODE (arg0) == NEGATE_EXPR)
10028 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10029 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10031 tree pmop[2];
10032 int which = 0;
10033 wide_int cst0;
10035 /* Now we know that arg0 is (C + D) or (C - D) or
10036 -C and arg1 (M) is == (1LL << cst) - 1.
10037 Store C into PMOP[0] and D into PMOP[1]. */
10038 pmop[0] = TREE_OPERAND (arg0, 0);
10039 pmop[1] = NULL;
10040 if (TREE_CODE (arg0) != NEGATE_EXPR)
10042 pmop[1] = TREE_OPERAND (arg0, 1);
10043 which = 1;
10046 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10047 which = -1;
10049 for (; which >= 0; which--)
10050 switch (TREE_CODE (pmop[which]))
10052 case BIT_AND_EXPR:
10053 case BIT_IOR_EXPR:
10054 case BIT_XOR_EXPR:
10055 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10056 != INTEGER_CST)
10057 break;
10058 cst0 = TREE_OPERAND (pmop[which], 1);
10059 cst0 &= cst1;
10060 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10062 if (cst0 != cst1)
10063 break;
10065 else if (cst0 != 0)
10066 break;
10067 /* If C or D is of the form (A & N) where
10068 (N & M) == M, or of the form (A | N) or
10069 (A ^ N) where (N & M) == 0, replace it with A. */
10070 pmop[which] = TREE_OPERAND (pmop[which], 0);
10071 break;
10072 case INTEGER_CST:
10073 /* If C or D is a N where (N & M) == 0, it can be
10074 omitted (assumed 0). */
10075 if ((TREE_CODE (arg0) == PLUS_EXPR
10076 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10077 && (cst1 & pmop[which]) == 0)
10078 pmop[which] = NULL;
10079 break;
10080 default:
10081 break;
10084 /* Only build anything new if we optimized one or both arguments
10085 above. */
10086 if (pmop[0] != TREE_OPERAND (arg0, 0)
10087 || (TREE_CODE (arg0) != NEGATE_EXPR
10088 && pmop[1] != TREE_OPERAND (arg0, 1)))
10090 tree utype = TREE_TYPE (arg0);
10091 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10093 /* Perform the operations in a type that has defined
10094 overflow behavior. */
10095 utype = unsigned_type_for (TREE_TYPE (arg0));
10096 if (pmop[0] != NULL)
10097 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10098 if (pmop[1] != NULL)
10099 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10102 if (TREE_CODE (arg0) == NEGATE_EXPR)
10103 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10104 else if (TREE_CODE (arg0) == PLUS_EXPR)
10106 if (pmop[0] != NULL && pmop[1] != NULL)
10107 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10108 pmop[0], pmop[1]);
10109 else if (pmop[0] != NULL)
10110 tem = pmop[0];
10111 else if (pmop[1] != NULL)
10112 tem = pmop[1];
10113 else
10114 return build_int_cst (type, 0);
10116 else if (pmop[0] == NULL)
10117 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10118 else
10119 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10120 pmop[0], pmop[1]);
10121 /* TEM is now the new binary +, - or unary - replacement. */
10122 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10123 fold_convert_loc (loc, utype, arg1));
10124 return fold_convert_loc (loc, type, tem);
10129 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10130 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10131 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10133 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10135 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10136 if (mask == -1)
10137 return
10138 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10141 goto associate;
10143 case RDIV_EXPR:
10144 /* Don't touch a floating-point divide by zero unless the mode
10145 of the constant can represent infinity. */
10146 if (TREE_CODE (arg1) == REAL_CST
10147 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10148 && real_zerop (arg1))
10149 return NULL_TREE;
10151 /* (-A) / (-B) -> A / B */
10152 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10153 return fold_build2_loc (loc, RDIV_EXPR, type,
10154 TREE_OPERAND (arg0, 0),
10155 negate_expr (arg1));
10156 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10157 return fold_build2_loc (loc, RDIV_EXPR, type,
10158 negate_expr (arg0),
10159 TREE_OPERAND (arg1, 0));
10160 return NULL_TREE;
10162 case TRUNC_DIV_EXPR:
10163 /* Fall through */
10165 case FLOOR_DIV_EXPR:
10166 /* Simplify A / (B << N) where A and B are positive and B is
10167 a power of 2, to A >> (N + log2(B)). */
10168 strict_overflow_p = false;
10169 if (TREE_CODE (arg1) == LSHIFT_EXPR
10170 && (TYPE_UNSIGNED (type)
10171 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10173 tree sval = TREE_OPERAND (arg1, 0);
10174 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10176 tree sh_cnt = TREE_OPERAND (arg1, 1);
10177 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10178 wi::exact_log2 (sval));
10180 if (strict_overflow_p)
10181 fold_overflow_warning (("assuming signed overflow does not "
10182 "occur when simplifying A / (B << N)"),
10183 WARN_STRICT_OVERFLOW_MISC);
10185 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10186 sh_cnt, pow2);
10187 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10188 fold_convert_loc (loc, type, arg0), sh_cnt);
10192 /* Fall through */
10194 case ROUND_DIV_EXPR:
10195 case CEIL_DIV_EXPR:
10196 case EXACT_DIV_EXPR:
10197 if (integer_zerop (arg1))
10198 return NULL_TREE;
10200 /* Convert -A / -B to A / B when the type is signed and overflow is
10201 undefined. */
10202 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10203 && TREE_CODE (arg0) == NEGATE_EXPR
10204 && negate_expr_p (op1))
10206 if (INTEGRAL_TYPE_P (type))
10207 fold_overflow_warning (("assuming signed overflow does not occur "
10208 "when distributing negation across "
10209 "division"),
10210 WARN_STRICT_OVERFLOW_MISC);
10211 return fold_build2_loc (loc, code, type,
10212 fold_convert_loc (loc, type,
10213 TREE_OPERAND (arg0, 0)),
10214 negate_expr (op1));
10216 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10217 && TREE_CODE (arg1) == NEGATE_EXPR
10218 && negate_expr_p (op0))
10220 if (INTEGRAL_TYPE_P (type))
10221 fold_overflow_warning (("assuming signed overflow does not occur "
10222 "when distributing negation across "
10223 "division"),
10224 WARN_STRICT_OVERFLOW_MISC);
10225 return fold_build2_loc (loc, code, type,
10226 negate_expr (op0),
10227 fold_convert_loc (loc, type,
10228 TREE_OPERAND (arg1, 0)));
10231 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10232 operation, EXACT_DIV_EXPR.
10234 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10235 At one time others generated faster code, it's not clear if they do
10236 after the last round to changes to the DIV code in expmed.c. */
10237 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10238 && multiple_of_p (type, arg0, arg1))
10239 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10240 fold_convert (type, arg0),
10241 fold_convert (type, arg1));
10243 strict_overflow_p = false;
10244 if (TREE_CODE (arg1) == INTEGER_CST
10245 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10246 &strict_overflow_p)))
10248 if (strict_overflow_p)
10249 fold_overflow_warning (("assuming signed overflow does not occur "
10250 "when simplifying division"),
10251 WARN_STRICT_OVERFLOW_MISC);
10252 return fold_convert_loc (loc, type, tem);
10255 return NULL_TREE;
10257 case CEIL_MOD_EXPR:
10258 case FLOOR_MOD_EXPR:
10259 case ROUND_MOD_EXPR:
10260 case TRUNC_MOD_EXPR:
10261 strict_overflow_p = false;
10262 if (TREE_CODE (arg1) == INTEGER_CST
10263 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10264 &strict_overflow_p)))
10266 if (strict_overflow_p)
10267 fold_overflow_warning (("assuming signed overflow does not occur "
10268 "when simplifying modulus"),
10269 WARN_STRICT_OVERFLOW_MISC);
10270 return fold_convert_loc (loc, type, tem);
10273 return NULL_TREE;
10275 case LROTATE_EXPR:
10276 case RROTATE_EXPR:
10277 case RSHIFT_EXPR:
10278 case LSHIFT_EXPR:
10279 /* Since negative shift count is not well-defined,
10280 don't try to compute it in the compiler. */
10281 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10282 return NULL_TREE;
10284 prec = element_precision (type);
10286 /* If we have a rotate of a bit operation with the rotate count and
10287 the second operand of the bit operation both constant,
10288 permute the two operations. */
10289 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10290 && (TREE_CODE (arg0) == BIT_AND_EXPR
10291 || TREE_CODE (arg0) == BIT_IOR_EXPR
10292 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10293 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10295 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10296 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10297 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10298 fold_build2_loc (loc, code, type,
10299 arg00, arg1),
10300 fold_build2_loc (loc, code, type,
10301 arg01, arg1));
10304 /* Two consecutive rotates adding up to the some integer
10305 multiple of the precision of the type can be ignored. */
10306 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10307 && TREE_CODE (arg0) == RROTATE_EXPR
10308 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10309 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10310 prec) == 0)
10311 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10313 return NULL_TREE;
10315 case MIN_EXPR:
10316 case MAX_EXPR:
10317 goto associate;
10319 case TRUTH_ANDIF_EXPR:
10320 /* Note that the operands of this must be ints
10321 and their values must be 0 or 1.
10322 ("true" is a fixed value perhaps depending on the language.) */
10323 /* If first arg is constant zero, return it. */
10324 if (integer_zerop (arg0))
10325 return fold_convert_loc (loc, type, arg0);
10326 case TRUTH_AND_EXPR:
10327 /* If either arg is constant true, drop it. */
10328 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10329 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10330 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10331 /* Preserve sequence points. */
10332 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10333 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10334 /* If second arg is constant zero, result is zero, but first arg
10335 must be evaluated. */
10336 if (integer_zerop (arg1))
10337 return omit_one_operand_loc (loc, type, arg1, arg0);
10338 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10339 case will be handled here. */
10340 if (integer_zerop (arg0))
10341 return omit_one_operand_loc (loc, type, arg0, arg1);
10343 /* !X && X is always false. */
10344 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10345 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10346 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10347 /* X && !X is always false. */
10348 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10349 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10350 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10352 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10353 means A >= Y && A != MAX, but in this case we know that
10354 A < X <= MAX. */
10356 if (!TREE_SIDE_EFFECTS (arg0)
10357 && !TREE_SIDE_EFFECTS (arg1))
10359 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10360 if (tem && !operand_equal_p (tem, arg0, 0))
10361 return fold_build2_loc (loc, code, type, tem, arg1);
10363 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10364 if (tem && !operand_equal_p (tem, arg1, 0))
10365 return fold_build2_loc (loc, code, type, arg0, tem);
10368 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10369 != NULL_TREE)
10370 return tem;
10372 return NULL_TREE;
10374 case TRUTH_ORIF_EXPR:
10375 /* Note that the operands of this must be ints
10376 and their values must be 0 or true.
10377 ("true" is a fixed value perhaps depending on the language.) */
10378 /* If first arg is constant true, return it. */
10379 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10380 return fold_convert_loc (loc, type, arg0);
10381 case TRUTH_OR_EXPR:
10382 /* If either arg is constant zero, drop it. */
10383 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10384 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10385 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10386 /* Preserve sequence points. */
10387 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10388 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10389 /* If second arg is constant true, result is true, but we must
10390 evaluate first arg. */
10391 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10392 return omit_one_operand_loc (loc, type, arg1, arg0);
10393 /* Likewise for first arg, but note this only occurs here for
10394 TRUTH_OR_EXPR. */
10395 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10396 return omit_one_operand_loc (loc, type, arg0, arg1);
10398 /* !X || X is always true. */
10399 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10400 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10401 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10402 /* X || !X is always true. */
10403 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10404 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10405 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10407 /* (X && !Y) || (!X && Y) is X ^ Y */
10408 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10409 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10411 tree a0, a1, l0, l1, n0, n1;
10413 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10414 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10416 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10417 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10419 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10420 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10422 if ((operand_equal_p (n0, a0, 0)
10423 && operand_equal_p (n1, a1, 0))
10424 || (operand_equal_p (n0, a1, 0)
10425 && operand_equal_p (n1, a0, 0)))
10426 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10429 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10430 != NULL_TREE)
10431 return tem;
10433 return NULL_TREE;
10435 case TRUTH_XOR_EXPR:
10436 /* If the second arg is constant zero, drop it. */
10437 if (integer_zerop (arg1))
10438 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10439 /* If the second arg is constant true, this is a logical inversion. */
10440 if (integer_onep (arg1))
10442 tem = invert_truthvalue_loc (loc, arg0);
10443 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10445 /* Identical arguments cancel to zero. */
10446 if (operand_equal_p (arg0, arg1, 0))
10447 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10449 /* !X ^ X is always true. */
10450 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10451 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10452 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10454 /* X ^ !X is always true. */
10455 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10456 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10457 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10459 return NULL_TREE;
10461 case EQ_EXPR:
10462 case NE_EXPR:
10463 STRIP_NOPS (arg0);
10464 STRIP_NOPS (arg1);
10466 tem = fold_comparison (loc, code, type, op0, op1);
10467 if (tem != NULL_TREE)
10468 return tem;
10470 /* bool_var != 1 becomes !bool_var. */
10471 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10472 && code == NE_EXPR)
10473 return fold_convert_loc (loc, type,
10474 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10475 TREE_TYPE (arg0), arg0));
10477 /* bool_var == 0 becomes !bool_var. */
10478 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10479 && code == EQ_EXPR)
10480 return fold_convert_loc (loc, type,
10481 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10482 TREE_TYPE (arg0), arg0));
10484 /* !exp != 0 becomes !exp */
10485 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10486 && code == NE_EXPR)
10487 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10489 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10490 if ((TREE_CODE (arg0) == PLUS_EXPR
10491 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10492 || TREE_CODE (arg0) == MINUS_EXPR)
10493 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10494 0)),
10495 arg1, 0)
10496 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10497 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10499 tree val = TREE_OPERAND (arg0, 1);
10500 val = fold_build2_loc (loc, code, type, val,
10501 build_int_cst (TREE_TYPE (val), 0));
10502 return omit_two_operands_loc (loc, type, val,
10503 TREE_OPERAND (arg0, 0), arg1);
10506 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10507 if ((TREE_CODE (arg1) == PLUS_EXPR
10508 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10509 || TREE_CODE (arg1) == MINUS_EXPR)
10510 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10511 0)),
10512 arg0, 0)
10513 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10514 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10516 tree val = TREE_OPERAND (arg1, 1);
10517 val = fold_build2_loc (loc, code, type, val,
10518 build_int_cst (TREE_TYPE (val), 0));
10519 return omit_two_operands_loc (loc, type, val,
10520 TREE_OPERAND (arg1, 0), arg0);
10523 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10524 if (TREE_CODE (arg0) == MINUS_EXPR
10525 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10526 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10527 1)),
10528 arg1, 0)
10529 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10530 return omit_two_operands_loc (loc, type,
10531 code == NE_EXPR
10532 ? boolean_true_node : boolean_false_node,
10533 TREE_OPERAND (arg0, 1), arg1);
10535 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10536 if (TREE_CODE (arg1) == MINUS_EXPR
10537 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10538 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10539 1)),
10540 arg0, 0)
10541 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10542 return omit_two_operands_loc (loc, type,
10543 code == NE_EXPR
10544 ? boolean_true_node : boolean_false_node,
10545 TREE_OPERAND (arg1, 1), arg0);
10547 /* If this is an EQ or NE comparison with zero and ARG0 is
10548 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10549 two operations, but the latter can be done in one less insn
10550 on machines that have only two-operand insns or on which a
10551 constant cannot be the first operand. */
10552 if (TREE_CODE (arg0) == BIT_AND_EXPR
10553 && integer_zerop (arg1))
10555 tree arg00 = TREE_OPERAND (arg0, 0);
10556 tree arg01 = TREE_OPERAND (arg0, 1);
10557 if (TREE_CODE (arg00) == LSHIFT_EXPR
10558 && integer_onep (TREE_OPERAND (arg00, 0)))
10560 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10561 arg01, TREE_OPERAND (arg00, 1));
10562 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10563 build_int_cst (TREE_TYPE (arg0), 1));
10564 return fold_build2_loc (loc, code, type,
10565 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10566 arg1);
10568 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10569 && integer_onep (TREE_OPERAND (arg01, 0)))
10571 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10572 arg00, TREE_OPERAND (arg01, 1));
10573 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10574 build_int_cst (TREE_TYPE (arg0), 1));
10575 return fold_build2_loc (loc, code, type,
10576 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10577 arg1);
10581 /* If this is an NE or EQ comparison of zero against the result of a
10582 signed MOD operation whose second operand is a power of 2, make
10583 the MOD operation unsigned since it is simpler and equivalent. */
10584 if (integer_zerop (arg1)
10585 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10586 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10587 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10588 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10589 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10590 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10592 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10593 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10594 fold_convert_loc (loc, newtype,
10595 TREE_OPERAND (arg0, 0)),
10596 fold_convert_loc (loc, newtype,
10597 TREE_OPERAND (arg0, 1)));
10599 return fold_build2_loc (loc, code, type, newmod,
10600 fold_convert_loc (loc, newtype, arg1));
10603 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10604 C1 is a valid shift constant, and C2 is a power of two, i.e.
10605 a single bit. */
10606 if (TREE_CODE (arg0) == BIT_AND_EXPR
10607 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10608 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10609 == INTEGER_CST
10610 && integer_pow2p (TREE_OPERAND (arg0, 1))
10611 && integer_zerop (arg1))
10613 tree itype = TREE_TYPE (arg0);
10614 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10615 prec = TYPE_PRECISION (itype);
10617 /* Check for a valid shift count. */
10618 if (wi::ltu_p (arg001, prec))
10620 tree arg01 = TREE_OPERAND (arg0, 1);
10621 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10622 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10623 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10624 can be rewritten as (X & (C2 << C1)) != 0. */
10625 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10627 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10628 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10629 return fold_build2_loc (loc, code, type, tem,
10630 fold_convert_loc (loc, itype, arg1));
10632 /* Otherwise, for signed (arithmetic) shifts,
10633 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10634 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10635 else if (!TYPE_UNSIGNED (itype))
10636 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10637 arg000, build_int_cst (itype, 0));
10638 /* Otherwise, of unsigned (logical) shifts,
10639 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10640 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10641 else
10642 return omit_one_operand_loc (loc, type,
10643 code == EQ_EXPR ? integer_one_node
10644 : integer_zero_node,
10645 arg000);
10649 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10650 Similarly for NE_EXPR. */
10651 if (TREE_CODE (arg0) == BIT_AND_EXPR
10652 && TREE_CODE (arg1) == INTEGER_CST
10653 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10655 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10656 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10657 TREE_OPERAND (arg0, 1));
10658 tree dandnotc
10659 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10660 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10661 notc);
10662 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10663 if (integer_nonzerop (dandnotc))
10664 return omit_one_operand_loc (loc, type, rslt, arg0);
10667 /* If this is a comparison of a field, we may be able to simplify it. */
10668 if ((TREE_CODE (arg0) == COMPONENT_REF
10669 || TREE_CODE (arg0) == BIT_FIELD_REF)
10670 /* Handle the constant case even without -O
10671 to make sure the warnings are given. */
10672 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10674 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10675 if (t1)
10676 return t1;
10679 /* Optimize comparisons of strlen vs zero to a compare of the
10680 first character of the string vs zero. To wit,
10681 strlen(ptr) == 0 => *ptr == 0
10682 strlen(ptr) != 0 => *ptr != 0
10683 Other cases should reduce to one of these two (or a constant)
10684 due to the return value of strlen being unsigned. */
10685 if (TREE_CODE (arg0) == CALL_EXPR
10686 && integer_zerop (arg1))
10688 tree fndecl = get_callee_fndecl (arg0);
10690 if (fndecl
10691 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10692 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10693 && call_expr_nargs (arg0) == 1
10694 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10696 tree iref = build_fold_indirect_ref_loc (loc,
10697 CALL_EXPR_ARG (arg0, 0));
10698 return fold_build2_loc (loc, code, type, iref,
10699 build_int_cst (TREE_TYPE (iref), 0));
10703 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10704 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10705 if (TREE_CODE (arg0) == RSHIFT_EXPR
10706 && integer_zerop (arg1)
10707 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10709 tree arg00 = TREE_OPERAND (arg0, 0);
10710 tree arg01 = TREE_OPERAND (arg0, 1);
10711 tree itype = TREE_TYPE (arg00);
10712 if (wi::eq_p (arg01, element_precision (itype) - 1))
10714 if (TYPE_UNSIGNED (itype))
10716 itype = signed_type_for (itype);
10717 arg00 = fold_convert_loc (loc, itype, arg00);
10719 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10720 type, arg00, build_zero_cst (itype));
10724 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10725 (X & C) == 0 when C is a single bit. */
10726 if (TREE_CODE (arg0) == BIT_AND_EXPR
10727 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10728 && integer_zerop (arg1)
10729 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10731 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10732 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10733 TREE_OPERAND (arg0, 1));
10734 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10735 type, tem,
10736 fold_convert_loc (loc, TREE_TYPE (arg0),
10737 arg1));
10740 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10741 constant C is a power of two, i.e. a single bit. */
10742 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10743 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10744 && integer_zerop (arg1)
10745 && integer_pow2p (TREE_OPERAND (arg0, 1))
10746 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10747 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10749 tree arg00 = TREE_OPERAND (arg0, 0);
10750 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10751 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10754 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10755 when is C is a power of two, i.e. a single bit. */
10756 if (TREE_CODE (arg0) == BIT_AND_EXPR
10757 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10758 && integer_zerop (arg1)
10759 && integer_pow2p (TREE_OPERAND (arg0, 1))
10760 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10761 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10763 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10764 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10765 arg000, TREE_OPERAND (arg0, 1));
10766 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10767 tem, build_int_cst (TREE_TYPE (tem), 0));
10770 if (integer_zerop (arg1)
10771 && tree_expr_nonzero_p (arg0))
10773 tree res = constant_boolean_node (code==NE_EXPR, type);
10774 return omit_one_operand_loc (loc, type, res, arg0);
10777 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10778 if (TREE_CODE (arg0) == BIT_AND_EXPR
10779 && TREE_CODE (arg1) == BIT_AND_EXPR)
10781 tree arg00 = TREE_OPERAND (arg0, 0);
10782 tree arg01 = TREE_OPERAND (arg0, 1);
10783 tree arg10 = TREE_OPERAND (arg1, 0);
10784 tree arg11 = TREE_OPERAND (arg1, 1);
10785 tree itype = TREE_TYPE (arg0);
10787 if (operand_equal_p (arg01, arg11, 0))
10788 return fold_build2_loc (loc, code, type,
10789 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10790 fold_build2_loc (loc,
10791 BIT_XOR_EXPR, itype,
10792 arg00, arg10),
10793 arg01),
10794 build_zero_cst (itype));
10796 if (operand_equal_p (arg01, arg10, 0))
10797 return fold_build2_loc (loc, code, type,
10798 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10799 fold_build2_loc (loc,
10800 BIT_XOR_EXPR, itype,
10801 arg00, arg11),
10802 arg01),
10803 build_zero_cst (itype));
10805 if (operand_equal_p (arg00, arg11, 0))
10806 return fold_build2_loc (loc, code, type,
10807 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10808 fold_build2_loc (loc,
10809 BIT_XOR_EXPR, itype,
10810 arg01, arg10),
10811 arg00),
10812 build_zero_cst (itype));
10814 if (operand_equal_p (arg00, arg10, 0))
10815 return fold_build2_loc (loc, code, type,
10816 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10817 fold_build2_loc (loc,
10818 BIT_XOR_EXPR, itype,
10819 arg01, arg11),
10820 arg00),
10821 build_zero_cst (itype));
10824 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10825 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10827 tree arg00 = TREE_OPERAND (arg0, 0);
10828 tree arg01 = TREE_OPERAND (arg0, 1);
10829 tree arg10 = TREE_OPERAND (arg1, 0);
10830 tree arg11 = TREE_OPERAND (arg1, 1);
10831 tree itype = TREE_TYPE (arg0);
10833 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10834 operand_equal_p guarantees no side-effects so we don't need
10835 to use omit_one_operand on Z. */
10836 if (operand_equal_p (arg01, arg11, 0))
10837 return fold_build2_loc (loc, code, type, arg00,
10838 fold_convert_loc (loc, TREE_TYPE (arg00),
10839 arg10));
10840 if (operand_equal_p (arg01, arg10, 0))
10841 return fold_build2_loc (loc, code, type, arg00,
10842 fold_convert_loc (loc, TREE_TYPE (arg00),
10843 arg11));
10844 if (operand_equal_p (arg00, arg11, 0))
10845 return fold_build2_loc (loc, code, type, arg01,
10846 fold_convert_loc (loc, TREE_TYPE (arg01),
10847 arg10));
10848 if (operand_equal_p (arg00, arg10, 0))
10849 return fold_build2_loc (loc, code, type, arg01,
10850 fold_convert_loc (loc, TREE_TYPE (arg01),
10851 arg11));
10853 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10854 if (TREE_CODE (arg01) == INTEGER_CST
10855 && TREE_CODE (arg11) == INTEGER_CST)
10857 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10858 fold_convert_loc (loc, itype, arg11));
10859 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10860 return fold_build2_loc (loc, code, type, tem,
10861 fold_convert_loc (loc, itype, arg10));
10865 /* Attempt to simplify equality/inequality comparisons of complex
10866 values. Only lower the comparison if the result is known or
10867 can be simplified to a single scalar comparison. */
10868 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10869 || TREE_CODE (arg0) == COMPLEX_CST)
10870 && (TREE_CODE (arg1) == COMPLEX_EXPR
10871 || TREE_CODE (arg1) == COMPLEX_CST))
10873 tree real0, imag0, real1, imag1;
10874 tree rcond, icond;
10876 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10878 real0 = TREE_OPERAND (arg0, 0);
10879 imag0 = TREE_OPERAND (arg0, 1);
10881 else
10883 real0 = TREE_REALPART (arg0);
10884 imag0 = TREE_IMAGPART (arg0);
10887 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10889 real1 = TREE_OPERAND (arg1, 0);
10890 imag1 = TREE_OPERAND (arg1, 1);
10892 else
10894 real1 = TREE_REALPART (arg1);
10895 imag1 = TREE_IMAGPART (arg1);
10898 rcond = fold_binary_loc (loc, code, type, real0, real1);
10899 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10901 if (integer_zerop (rcond))
10903 if (code == EQ_EXPR)
10904 return omit_two_operands_loc (loc, type, boolean_false_node,
10905 imag0, imag1);
10906 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10908 else
10910 if (code == NE_EXPR)
10911 return omit_two_operands_loc (loc, type, boolean_true_node,
10912 imag0, imag1);
10913 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10917 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10918 if (icond && TREE_CODE (icond) == INTEGER_CST)
10920 if (integer_zerop (icond))
10922 if (code == EQ_EXPR)
10923 return omit_two_operands_loc (loc, type, boolean_false_node,
10924 real0, real1);
10925 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10927 else
10929 if (code == NE_EXPR)
10930 return omit_two_operands_loc (loc, type, boolean_true_node,
10931 real0, real1);
10932 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10937 return NULL_TREE;
10939 case LT_EXPR:
10940 case GT_EXPR:
10941 case LE_EXPR:
10942 case GE_EXPR:
10943 tem = fold_comparison (loc, code, type, op0, op1);
10944 if (tem != NULL_TREE)
10945 return tem;
10947 /* Transform comparisons of the form X +- C CMP X. */
10948 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10949 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10950 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10951 && !HONOR_SNANS (arg0))
10952 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10953 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10955 tree arg01 = TREE_OPERAND (arg0, 1);
10956 enum tree_code code0 = TREE_CODE (arg0);
10957 int is_positive;
10959 if (TREE_CODE (arg01) == REAL_CST)
10960 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10961 else
10962 is_positive = tree_int_cst_sgn (arg01);
10964 /* (X - c) > X becomes false. */
10965 if (code == GT_EXPR
10966 && ((code0 == MINUS_EXPR && is_positive >= 0)
10967 || (code0 == PLUS_EXPR && is_positive <= 0)))
10969 if (TREE_CODE (arg01) == INTEGER_CST
10970 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10971 fold_overflow_warning (("assuming signed overflow does not "
10972 "occur when assuming that (X - c) > X "
10973 "is always false"),
10974 WARN_STRICT_OVERFLOW_ALL);
10975 return constant_boolean_node (0, type);
10978 /* Likewise (X + c) < X becomes false. */
10979 if (code == LT_EXPR
10980 && ((code0 == PLUS_EXPR && is_positive >= 0)
10981 || (code0 == MINUS_EXPR && is_positive <= 0)))
10983 if (TREE_CODE (arg01) == INTEGER_CST
10984 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
10985 fold_overflow_warning (("assuming signed overflow does not "
10986 "occur when assuming that "
10987 "(X + c) < X is always false"),
10988 WARN_STRICT_OVERFLOW_ALL);
10989 return constant_boolean_node (0, type);
10992 /* Convert (X - c) <= X to true. */
10993 if (!HONOR_NANS (arg1)
10994 && code == LE_EXPR
10995 && ((code0 == MINUS_EXPR && is_positive >= 0)
10996 || (code0 == PLUS_EXPR && is_positive <= 0)))
10998 if (TREE_CODE (arg01) == INTEGER_CST
10999 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11000 fold_overflow_warning (("assuming signed overflow does not "
11001 "occur when assuming that "
11002 "(X - c) <= X is always true"),
11003 WARN_STRICT_OVERFLOW_ALL);
11004 return constant_boolean_node (1, type);
11007 /* Convert (X + c) >= X to true. */
11008 if (!HONOR_NANS (arg1)
11009 && code == GE_EXPR
11010 && ((code0 == PLUS_EXPR && is_positive >= 0)
11011 || (code0 == MINUS_EXPR && is_positive <= 0)))
11013 if (TREE_CODE (arg01) == INTEGER_CST
11014 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11015 fold_overflow_warning (("assuming signed overflow does not "
11016 "occur when assuming that "
11017 "(X + c) >= X is always true"),
11018 WARN_STRICT_OVERFLOW_ALL);
11019 return constant_boolean_node (1, type);
11022 if (TREE_CODE (arg01) == INTEGER_CST)
11024 /* Convert X + c > X and X - c < X to true for integers. */
11025 if (code == GT_EXPR
11026 && ((code0 == PLUS_EXPR && is_positive > 0)
11027 || (code0 == MINUS_EXPR && is_positive < 0)))
11029 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11030 fold_overflow_warning (("assuming signed overflow does "
11031 "not occur when assuming that "
11032 "(X + c) > X is always true"),
11033 WARN_STRICT_OVERFLOW_ALL);
11034 return constant_boolean_node (1, type);
11037 if (code == LT_EXPR
11038 && ((code0 == MINUS_EXPR && is_positive > 0)
11039 || (code0 == PLUS_EXPR && is_positive < 0)))
11041 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11042 fold_overflow_warning (("assuming signed overflow does "
11043 "not occur when assuming that "
11044 "(X - c) < X is always true"),
11045 WARN_STRICT_OVERFLOW_ALL);
11046 return constant_boolean_node (1, type);
11049 /* Convert X + c <= X and X - c >= X to false for integers. */
11050 if (code == LE_EXPR
11051 && ((code0 == PLUS_EXPR && is_positive > 0)
11052 || (code0 == MINUS_EXPR && is_positive < 0)))
11054 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11055 fold_overflow_warning (("assuming signed overflow does "
11056 "not occur when assuming that "
11057 "(X + c) <= X is always false"),
11058 WARN_STRICT_OVERFLOW_ALL);
11059 return constant_boolean_node (0, type);
11062 if (code == GE_EXPR
11063 && ((code0 == MINUS_EXPR && is_positive > 0)
11064 || (code0 == PLUS_EXPR && is_positive < 0)))
11066 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11067 fold_overflow_warning (("assuming signed overflow does "
11068 "not occur when assuming that "
11069 "(X - c) >= X is always false"),
11070 WARN_STRICT_OVERFLOW_ALL);
11071 return constant_boolean_node (0, type);
11076 /* If we are comparing an ABS_EXPR with a constant, we can
11077 convert all the cases into explicit comparisons, but they may
11078 well not be faster than doing the ABS and one comparison.
11079 But ABS (X) <= C is a range comparison, which becomes a subtraction
11080 and a comparison, and is probably faster. */
11081 if (code == LE_EXPR
11082 && TREE_CODE (arg1) == INTEGER_CST
11083 && TREE_CODE (arg0) == ABS_EXPR
11084 && ! TREE_SIDE_EFFECTS (arg0)
11085 && (0 != (tem = negate_expr (arg1)))
11086 && TREE_CODE (tem) == INTEGER_CST
11087 && !TREE_OVERFLOW (tem))
11088 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11089 build2 (GE_EXPR, type,
11090 TREE_OPERAND (arg0, 0), tem),
11091 build2 (LE_EXPR, type,
11092 TREE_OPERAND (arg0, 0), arg1));
11094 /* Convert ABS_EXPR<x> >= 0 to true. */
11095 strict_overflow_p = false;
11096 if (code == GE_EXPR
11097 && (integer_zerop (arg1)
11098 || (! HONOR_NANS (arg0)
11099 && real_zerop (arg1)))
11100 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11102 if (strict_overflow_p)
11103 fold_overflow_warning (("assuming signed overflow does not occur "
11104 "when simplifying comparison of "
11105 "absolute value and zero"),
11106 WARN_STRICT_OVERFLOW_CONDITIONAL);
11107 return omit_one_operand_loc (loc, type,
11108 constant_boolean_node (true, type),
11109 arg0);
11112 /* Convert ABS_EXPR<x> < 0 to false. */
11113 strict_overflow_p = false;
11114 if (code == LT_EXPR
11115 && (integer_zerop (arg1) || real_zerop (arg1))
11116 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11118 if (strict_overflow_p)
11119 fold_overflow_warning (("assuming signed overflow does not occur "
11120 "when simplifying comparison of "
11121 "absolute value and zero"),
11122 WARN_STRICT_OVERFLOW_CONDITIONAL);
11123 return omit_one_operand_loc (loc, type,
11124 constant_boolean_node (false, type),
11125 arg0);
11128 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11129 and similarly for >= into !=. */
11130 if ((code == LT_EXPR || code == GE_EXPR)
11131 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11132 && TREE_CODE (arg1) == LSHIFT_EXPR
11133 && integer_onep (TREE_OPERAND (arg1, 0)))
11134 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11135 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11136 TREE_OPERAND (arg1, 1)),
11137 build_zero_cst (TREE_TYPE (arg0)));
11139 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11140 otherwise Y might be >= # of bits in X's type and thus e.g.
11141 (unsigned char) (1 << Y) for Y 15 might be 0.
11142 If the cast is widening, then 1 << Y should have unsigned type,
11143 otherwise if Y is number of bits in the signed shift type minus 1,
11144 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11145 31 might be 0xffffffff80000000. */
11146 if ((code == LT_EXPR || code == GE_EXPR)
11147 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11148 && CONVERT_EXPR_P (arg1)
11149 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11150 && (element_precision (TREE_TYPE (arg1))
11151 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11152 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11153 || (element_precision (TREE_TYPE (arg1))
11154 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11155 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11157 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11158 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11159 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11160 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11161 build_zero_cst (TREE_TYPE (arg0)));
11164 return NULL_TREE;
11166 case UNORDERED_EXPR:
11167 case ORDERED_EXPR:
11168 case UNLT_EXPR:
11169 case UNLE_EXPR:
11170 case UNGT_EXPR:
11171 case UNGE_EXPR:
11172 case UNEQ_EXPR:
11173 case LTGT_EXPR:
11174 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11176 tree targ0 = strip_float_extensions (arg0);
11177 tree targ1 = strip_float_extensions (arg1);
11178 tree newtype = TREE_TYPE (targ0);
11180 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11181 newtype = TREE_TYPE (targ1);
11183 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11184 return fold_build2_loc (loc, code, type,
11185 fold_convert_loc (loc, newtype, targ0),
11186 fold_convert_loc (loc, newtype, targ1));
11189 return NULL_TREE;
11191 case COMPOUND_EXPR:
11192 /* When pedantic, a compound expression can be neither an lvalue
11193 nor an integer constant expression. */
11194 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11195 return NULL_TREE;
11196 /* Don't let (0, 0) be null pointer constant. */
11197 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11198 : fold_convert_loc (loc, type, arg1);
11199 return pedantic_non_lvalue_loc (loc, tem);
11201 case ASSERT_EXPR:
11202 /* An ASSERT_EXPR should never be passed to fold_binary. */
11203 gcc_unreachable ();
11205 default:
11206 return NULL_TREE;
11207 } /* switch (code) */
11210 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11211 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11212 of GOTO_EXPR. */
11214 static tree
11215 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11217 switch (TREE_CODE (*tp))
11219 case LABEL_EXPR:
11220 return *tp;
11222 case GOTO_EXPR:
11223 *walk_subtrees = 0;
11225 /* ... fall through ... */
11227 default:
11228 return NULL_TREE;
11232 /* Return whether the sub-tree ST contains a label which is accessible from
11233 outside the sub-tree. */
11235 static bool
11236 contains_label_p (tree st)
11238 return
11239 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11242 /* Fold a ternary expression of code CODE and type TYPE with operands
11243 OP0, OP1, and OP2. Return the folded expression if folding is
11244 successful. Otherwise, return NULL_TREE. */
11246 tree
11247 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11248 tree op0, tree op1, tree op2)
11250 tree tem;
11251 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11252 enum tree_code_class kind = TREE_CODE_CLASS (code);
11254 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11255 && TREE_CODE_LENGTH (code) == 3);
11257 /* If this is a commutative operation, and OP0 is a constant, move it
11258 to OP1 to reduce the number of tests below. */
11259 if (commutative_ternary_tree_code (code)
11260 && tree_swap_operands_p (op0, op1, true))
11261 return fold_build3_loc (loc, code, type, op1, op0, op2);
11263 tem = generic_simplify (loc, code, type, op0, op1, op2);
11264 if (tem)
11265 return tem;
11267 /* Strip any conversions that don't change the mode. This is safe
11268 for every expression, except for a comparison expression because
11269 its signedness is derived from its operands. So, in the latter
11270 case, only strip conversions that don't change the signedness.
11272 Note that this is done as an internal manipulation within the
11273 constant folder, in order to find the simplest representation of
11274 the arguments so that their form can be studied. In any cases,
11275 the appropriate type conversions should be put back in the tree
11276 that will get out of the constant folder. */
11277 if (op0)
11279 arg0 = op0;
11280 STRIP_NOPS (arg0);
11283 if (op1)
11285 arg1 = op1;
11286 STRIP_NOPS (arg1);
11289 if (op2)
11291 arg2 = op2;
11292 STRIP_NOPS (arg2);
11295 switch (code)
11297 case COMPONENT_REF:
11298 if (TREE_CODE (arg0) == CONSTRUCTOR
11299 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11301 unsigned HOST_WIDE_INT idx;
11302 tree field, value;
11303 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11304 if (field == arg1)
11305 return value;
11307 return NULL_TREE;
11309 case COND_EXPR:
11310 case VEC_COND_EXPR:
11311 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11312 so all simple results must be passed through pedantic_non_lvalue. */
11313 if (TREE_CODE (arg0) == INTEGER_CST)
11315 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11316 tem = integer_zerop (arg0) ? op2 : op1;
11317 /* Only optimize constant conditions when the selected branch
11318 has the same type as the COND_EXPR. This avoids optimizing
11319 away "c ? x : throw", where the throw has a void type.
11320 Avoid throwing away that operand which contains label. */
11321 if ((!TREE_SIDE_EFFECTS (unused_op)
11322 || !contains_label_p (unused_op))
11323 && (! VOID_TYPE_P (TREE_TYPE (tem))
11324 || VOID_TYPE_P (type)))
11325 return pedantic_non_lvalue_loc (loc, tem);
11326 return NULL_TREE;
11328 else if (TREE_CODE (arg0) == VECTOR_CST)
11330 if ((TREE_CODE (arg1) == VECTOR_CST
11331 || TREE_CODE (arg1) == CONSTRUCTOR)
11332 && (TREE_CODE (arg2) == VECTOR_CST
11333 || TREE_CODE (arg2) == CONSTRUCTOR))
11335 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11336 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11337 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11338 for (i = 0; i < nelts; i++)
11340 tree val = VECTOR_CST_ELT (arg0, i);
11341 if (integer_all_onesp (val))
11342 sel[i] = i;
11343 else if (integer_zerop (val))
11344 sel[i] = nelts + i;
11345 else /* Currently unreachable. */
11346 return NULL_TREE;
11348 tree t = fold_vec_perm (type, arg1, arg2, sel);
11349 if (t != NULL_TREE)
11350 return t;
11354 /* If we have A op B ? A : C, we may be able to convert this to a
11355 simpler expression, depending on the operation and the values
11356 of B and C. Signed zeros prevent all of these transformations,
11357 for reasons given above each one.
11359 Also try swapping the arguments and inverting the conditional. */
11360 if (COMPARISON_CLASS_P (arg0)
11361 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11362 arg1, TREE_OPERAND (arg0, 1))
11363 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11365 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11366 if (tem)
11367 return tem;
11370 if (COMPARISON_CLASS_P (arg0)
11371 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11372 op2,
11373 TREE_OPERAND (arg0, 1))
11374 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11376 location_t loc0 = expr_location_or (arg0, loc);
11377 tem = fold_invert_truthvalue (loc0, arg0);
11378 if (tem && COMPARISON_CLASS_P (tem))
11380 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11381 if (tem)
11382 return tem;
11386 /* If the second operand is simpler than the third, swap them
11387 since that produces better jump optimization results. */
11388 if (truth_value_p (TREE_CODE (arg0))
11389 && tree_swap_operands_p (op1, op2, false))
11391 location_t loc0 = expr_location_or (arg0, loc);
11392 /* See if this can be inverted. If it can't, possibly because
11393 it was a floating-point inequality comparison, don't do
11394 anything. */
11395 tem = fold_invert_truthvalue (loc0, arg0);
11396 if (tem)
11397 return fold_build3_loc (loc, code, type, tem, op2, op1);
11400 /* Convert A ? 1 : 0 to simply A. */
11401 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11402 : (integer_onep (op1)
11403 && !VECTOR_TYPE_P (type)))
11404 && integer_zerop (op2)
11405 /* If we try to convert OP0 to our type, the
11406 call to fold will try to move the conversion inside
11407 a COND, which will recurse. In that case, the COND_EXPR
11408 is probably the best choice, so leave it alone. */
11409 && type == TREE_TYPE (arg0))
11410 return pedantic_non_lvalue_loc (loc, arg0);
11412 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11413 over COND_EXPR in cases such as floating point comparisons. */
11414 if (integer_zerop (op1)
11415 && code == COND_EXPR
11416 && integer_onep (op2)
11417 && !VECTOR_TYPE_P (type)
11418 && truth_value_p (TREE_CODE (arg0)))
11419 return pedantic_non_lvalue_loc (loc,
11420 fold_convert_loc (loc, type,
11421 invert_truthvalue_loc (loc,
11422 arg0)));
11424 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11425 if (TREE_CODE (arg0) == LT_EXPR
11426 && integer_zerop (TREE_OPERAND (arg0, 1))
11427 && integer_zerop (op2)
11428 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11430 /* sign_bit_p looks through both zero and sign extensions,
11431 but for this optimization only sign extensions are
11432 usable. */
11433 tree tem2 = TREE_OPERAND (arg0, 0);
11434 while (tem != tem2)
11436 if (TREE_CODE (tem2) != NOP_EXPR
11437 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11439 tem = NULL_TREE;
11440 break;
11442 tem2 = TREE_OPERAND (tem2, 0);
11444 /* sign_bit_p only checks ARG1 bits within A's precision.
11445 If <sign bit of A> has wider type than A, bits outside
11446 of A's precision in <sign bit of A> need to be checked.
11447 If they are all 0, this optimization needs to be done
11448 in unsigned A's type, if they are all 1 in signed A's type,
11449 otherwise this can't be done. */
11450 if (tem
11451 && TYPE_PRECISION (TREE_TYPE (tem))
11452 < TYPE_PRECISION (TREE_TYPE (arg1))
11453 && TYPE_PRECISION (TREE_TYPE (tem))
11454 < TYPE_PRECISION (type))
11456 int inner_width, outer_width;
11457 tree tem_type;
11459 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11460 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11461 if (outer_width > TYPE_PRECISION (type))
11462 outer_width = TYPE_PRECISION (type);
11464 wide_int mask = wi::shifted_mask
11465 (inner_width, outer_width - inner_width, false,
11466 TYPE_PRECISION (TREE_TYPE (arg1)));
11468 wide_int common = mask & arg1;
11469 if (common == mask)
11471 tem_type = signed_type_for (TREE_TYPE (tem));
11472 tem = fold_convert_loc (loc, tem_type, tem);
11474 else if (common == 0)
11476 tem_type = unsigned_type_for (TREE_TYPE (tem));
11477 tem = fold_convert_loc (loc, tem_type, tem);
11479 else
11480 tem = NULL;
11483 if (tem)
11484 return
11485 fold_convert_loc (loc, type,
11486 fold_build2_loc (loc, BIT_AND_EXPR,
11487 TREE_TYPE (tem), tem,
11488 fold_convert_loc (loc,
11489 TREE_TYPE (tem),
11490 arg1)));
11493 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11494 already handled above. */
11495 if (TREE_CODE (arg0) == BIT_AND_EXPR
11496 && integer_onep (TREE_OPERAND (arg0, 1))
11497 && integer_zerop (op2)
11498 && integer_pow2p (arg1))
11500 tree tem = TREE_OPERAND (arg0, 0);
11501 STRIP_NOPS (tem);
11502 if (TREE_CODE (tem) == RSHIFT_EXPR
11503 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11504 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11505 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11506 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11507 TREE_OPERAND (tem, 0), arg1);
11510 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11511 is probably obsolete because the first operand should be a
11512 truth value (that's why we have the two cases above), but let's
11513 leave it in until we can confirm this for all front-ends. */
11514 if (integer_zerop (op2)
11515 && TREE_CODE (arg0) == NE_EXPR
11516 && integer_zerop (TREE_OPERAND (arg0, 1))
11517 && integer_pow2p (arg1)
11518 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11519 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11520 arg1, OEP_ONLY_CONST))
11521 return pedantic_non_lvalue_loc (loc,
11522 fold_convert_loc (loc, type,
11523 TREE_OPERAND (arg0, 0)));
11525 /* Disable the transformations below for vectors, since
11526 fold_binary_op_with_conditional_arg may undo them immediately,
11527 yielding an infinite loop. */
11528 if (code == VEC_COND_EXPR)
11529 return NULL_TREE;
11531 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11532 if (integer_zerop (op2)
11533 && truth_value_p (TREE_CODE (arg0))
11534 && truth_value_p (TREE_CODE (arg1))
11535 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11536 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11537 : TRUTH_ANDIF_EXPR,
11538 type, fold_convert_loc (loc, type, arg0), arg1);
11540 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11541 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11542 && truth_value_p (TREE_CODE (arg0))
11543 && truth_value_p (TREE_CODE (arg1))
11544 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11546 location_t loc0 = expr_location_or (arg0, loc);
11547 /* Only perform transformation if ARG0 is easily inverted. */
11548 tem = fold_invert_truthvalue (loc0, arg0);
11549 if (tem)
11550 return fold_build2_loc (loc, code == VEC_COND_EXPR
11551 ? BIT_IOR_EXPR
11552 : TRUTH_ORIF_EXPR,
11553 type, fold_convert_loc (loc, type, tem),
11554 arg1);
11557 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11558 if (integer_zerop (arg1)
11559 && truth_value_p (TREE_CODE (arg0))
11560 && truth_value_p (TREE_CODE (op2))
11561 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11563 location_t loc0 = expr_location_or (arg0, loc);
11564 /* Only perform transformation if ARG0 is easily inverted. */
11565 tem = fold_invert_truthvalue (loc0, arg0);
11566 if (tem)
11567 return fold_build2_loc (loc, code == VEC_COND_EXPR
11568 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11569 type, fold_convert_loc (loc, type, tem),
11570 op2);
11573 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11574 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11575 && truth_value_p (TREE_CODE (arg0))
11576 && truth_value_p (TREE_CODE (op2))
11577 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11578 return fold_build2_loc (loc, code == VEC_COND_EXPR
11579 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11580 type, fold_convert_loc (loc, type, arg0), op2);
11582 return NULL_TREE;
11584 case CALL_EXPR:
11585 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11586 of fold_ternary on them. */
11587 gcc_unreachable ();
11589 case BIT_FIELD_REF:
11590 if (TREE_CODE (arg0) == VECTOR_CST
11591 && (type == TREE_TYPE (TREE_TYPE (arg0))
11592 || (TREE_CODE (type) == VECTOR_TYPE
11593 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11595 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11596 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11597 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11598 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11600 if (n != 0
11601 && (idx % width) == 0
11602 && (n % width) == 0
11603 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11605 idx = idx / width;
11606 n = n / width;
11608 if (TREE_CODE (arg0) == VECTOR_CST)
11610 if (n == 1)
11611 return VECTOR_CST_ELT (arg0, idx);
11613 tree *vals = XALLOCAVEC (tree, n);
11614 for (unsigned i = 0; i < n; ++i)
11615 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11616 return build_vector (type, vals);
11621 /* On constants we can use native encode/interpret to constant
11622 fold (nearly) all BIT_FIELD_REFs. */
11623 if (CONSTANT_CLASS_P (arg0)
11624 && can_native_interpret_type_p (type)
11625 && BITS_PER_UNIT == 8)
11627 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11628 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11629 /* Limit us to a reasonable amount of work. To relax the
11630 other limitations we need bit-shifting of the buffer
11631 and rounding up the size. */
11632 if (bitpos % BITS_PER_UNIT == 0
11633 && bitsize % BITS_PER_UNIT == 0
11634 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11636 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11637 unsigned HOST_WIDE_INT len
11638 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11639 bitpos / BITS_PER_UNIT);
11640 if (len > 0
11641 && len * BITS_PER_UNIT >= bitsize)
11643 tree v = native_interpret_expr (type, b,
11644 bitsize / BITS_PER_UNIT);
11645 if (v)
11646 return v;
11651 return NULL_TREE;
11653 case FMA_EXPR:
11654 /* For integers we can decompose the FMA if possible. */
11655 if (TREE_CODE (arg0) == INTEGER_CST
11656 && TREE_CODE (arg1) == INTEGER_CST)
11657 return fold_build2_loc (loc, PLUS_EXPR, type,
11658 const_binop (MULT_EXPR, arg0, arg1), arg2);
11659 if (integer_zerop (arg2))
11660 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11662 return fold_fma (loc, type, arg0, arg1, arg2);
11664 case VEC_PERM_EXPR:
11665 if (TREE_CODE (arg2) == VECTOR_CST)
11667 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11668 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11669 unsigned char *sel2 = sel + nelts;
11670 bool need_mask_canon = false;
11671 bool need_mask_canon2 = false;
11672 bool all_in_vec0 = true;
11673 bool all_in_vec1 = true;
11674 bool maybe_identity = true;
11675 bool single_arg = (op0 == op1);
11676 bool changed = false;
11678 mask2 = 2 * nelts - 1;
11679 mask = single_arg ? (nelts - 1) : mask2;
11680 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11681 for (i = 0; i < nelts; i++)
11683 tree val = VECTOR_CST_ELT (arg2, i);
11684 if (TREE_CODE (val) != INTEGER_CST)
11685 return NULL_TREE;
11687 /* Make sure that the perm value is in an acceptable
11688 range. */
11689 wide_int t = val;
11690 need_mask_canon |= wi::gtu_p (t, mask);
11691 need_mask_canon2 |= wi::gtu_p (t, mask2);
11692 sel[i] = t.to_uhwi () & mask;
11693 sel2[i] = t.to_uhwi () & mask2;
11695 if (sel[i] < nelts)
11696 all_in_vec1 = false;
11697 else
11698 all_in_vec0 = false;
11700 if ((sel[i] & (nelts-1)) != i)
11701 maybe_identity = false;
11704 if (maybe_identity)
11706 if (all_in_vec0)
11707 return op0;
11708 if (all_in_vec1)
11709 return op1;
11712 if (all_in_vec0)
11713 op1 = op0;
11714 else if (all_in_vec1)
11716 op0 = op1;
11717 for (i = 0; i < nelts; i++)
11718 sel[i] -= nelts;
11719 need_mask_canon = true;
11722 if ((TREE_CODE (op0) == VECTOR_CST
11723 || TREE_CODE (op0) == CONSTRUCTOR)
11724 && (TREE_CODE (op1) == VECTOR_CST
11725 || TREE_CODE (op1) == CONSTRUCTOR))
11727 tree t = fold_vec_perm (type, op0, op1, sel);
11728 if (t != NULL_TREE)
11729 return t;
11732 if (op0 == op1 && !single_arg)
11733 changed = true;
11735 /* Some targets are deficient and fail to expand a single
11736 argument permutation while still allowing an equivalent
11737 2-argument version. */
11738 if (need_mask_canon && arg2 == op2
11739 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11740 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11742 need_mask_canon = need_mask_canon2;
11743 sel = sel2;
11746 if (need_mask_canon && arg2 == op2)
11748 tree *tsel = XALLOCAVEC (tree, nelts);
11749 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11750 for (i = 0; i < nelts; i++)
11751 tsel[i] = build_int_cst (eltype, sel[i]);
11752 op2 = build_vector (TREE_TYPE (arg2), tsel);
11753 changed = true;
11756 if (changed)
11757 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11759 return NULL_TREE;
11761 case BIT_INSERT_EXPR:
11762 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11763 if (TREE_CODE (arg0) == INTEGER_CST
11764 && TREE_CODE (arg1) == INTEGER_CST)
11766 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11767 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11768 wide_int tem = wi::bit_and (arg0,
11769 wi::shifted_mask (bitpos, bitsize, true,
11770 TYPE_PRECISION (type)));
11771 wide_int tem2
11772 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11773 bitsize), bitpos);
11774 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11776 else if (TREE_CODE (arg0) == VECTOR_CST
11777 && CONSTANT_CLASS_P (arg1)
11778 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11779 TREE_TYPE (arg1)))
11781 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11782 unsigned HOST_WIDE_INT elsize
11783 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11784 if (bitpos % elsize == 0)
11786 unsigned k = bitpos / elsize;
11787 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11788 return arg0;
11789 else
11791 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11792 memcpy (elts, VECTOR_CST_ELTS (arg0),
11793 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11794 elts[k] = arg1;
11795 return build_vector (type, elts);
11799 return NULL_TREE;
11801 default:
11802 return NULL_TREE;
11803 } /* switch (code) */
11806 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11807 of an array (or vector). */
11809 tree
11810 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11812 tree index_type = NULL_TREE;
11813 offset_int low_bound = 0;
11815 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11817 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11818 if (domain_type && TYPE_MIN_VALUE (domain_type))
11820 /* Static constructors for variably sized objects makes no sense. */
11821 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11822 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11823 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11827 if (index_type)
11828 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11829 TYPE_SIGN (index_type));
11831 offset_int index = low_bound - 1;
11832 if (index_type)
11833 index = wi::ext (index, TYPE_PRECISION (index_type),
11834 TYPE_SIGN (index_type));
11836 offset_int max_index;
11837 unsigned HOST_WIDE_INT cnt;
11838 tree cfield, cval;
11840 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11842 /* Array constructor might explicitly set index, or specify a range,
11843 or leave index NULL meaning that it is next index after previous
11844 one. */
11845 if (cfield)
11847 if (TREE_CODE (cfield) == INTEGER_CST)
11848 max_index = index = wi::to_offset (cfield);
11849 else
11851 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11852 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11853 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11856 else
11858 index += 1;
11859 if (index_type)
11860 index = wi::ext (index, TYPE_PRECISION (index_type),
11861 TYPE_SIGN (index_type));
11862 max_index = index;
11865 /* Do we have match? */
11866 if (wi::cmpu (access_index, index) >= 0
11867 && wi::cmpu (access_index, max_index) <= 0)
11868 return cval;
11870 return NULL_TREE;
11873 /* Perform constant folding and related simplification of EXPR.
11874 The related simplifications include x*1 => x, x*0 => 0, etc.,
11875 and application of the associative law.
11876 NOP_EXPR conversions may be removed freely (as long as we
11877 are careful not to change the type of the overall expression).
11878 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11879 but we can constant-fold them if they have constant operands. */
11881 #ifdef ENABLE_FOLD_CHECKING
11882 # define fold(x) fold_1 (x)
11883 static tree fold_1 (tree);
11884 static
11885 #endif
11886 tree
11887 fold (tree expr)
11889 const tree t = expr;
11890 enum tree_code code = TREE_CODE (t);
11891 enum tree_code_class kind = TREE_CODE_CLASS (code);
11892 tree tem;
11893 location_t loc = EXPR_LOCATION (expr);
11895 /* Return right away if a constant. */
11896 if (kind == tcc_constant)
11897 return t;
11899 /* CALL_EXPR-like objects with variable numbers of operands are
11900 treated specially. */
11901 if (kind == tcc_vl_exp)
11903 if (code == CALL_EXPR)
11905 tem = fold_call_expr (loc, expr, false);
11906 return tem ? tem : expr;
11908 return expr;
11911 if (IS_EXPR_CODE_CLASS (kind))
11913 tree type = TREE_TYPE (t);
11914 tree op0, op1, op2;
11916 switch (TREE_CODE_LENGTH (code))
11918 case 1:
11919 op0 = TREE_OPERAND (t, 0);
11920 tem = fold_unary_loc (loc, code, type, op0);
11921 return tem ? tem : expr;
11922 case 2:
11923 op0 = TREE_OPERAND (t, 0);
11924 op1 = TREE_OPERAND (t, 1);
11925 tem = fold_binary_loc (loc, code, type, op0, op1);
11926 return tem ? tem : expr;
11927 case 3:
11928 op0 = TREE_OPERAND (t, 0);
11929 op1 = TREE_OPERAND (t, 1);
11930 op2 = TREE_OPERAND (t, 2);
11931 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11932 return tem ? tem : expr;
11933 default:
11934 break;
11938 switch (code)
11940 case ARRAY_REF:
11942 tree op0 = TREE_OPERAND (t, 0);
11943 tree op1 = TREE_OPERAND (t, 1);
11945 if (TREE_CODE (op1) == INTEGER_CST
11946 && TREE_CODE (op0) == CONSTRUCTOR
11947 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11949 tree val = get_array_ctor_element_at_index (op0,
11950 wi::to_offset (op1));
11951 if (val)
11952 return val;
11955 return t;
11958 /* Return a VECTOR_CST if possible. */
11959 case CONSTRUCTOR:
11961 tree type = TREE_TYPE (t);
11962 if (TREE_CODE (type) != VECTOR_TYPE)
11963 return t;
11965 unsigned i;
11966 tree val;
11967 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11968 if (! CONSTANT_CLASS_P (val))
11969 return t;
11971 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11974 case CONST_DECL:
11975 return fold (DECL_INITIAL (t));
11977 default:
11978 return t;
11979 } /* switch (code) */
11982 #ifdef ENABLE_FOLD_CHECKING
11983 #undef fold
11985 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11986 hash_table<nofree_ptr_hash<const tree_node> > *);
11987 static void fold_check_failed (const_tree, const_tree);
11988 void print_fold_checksum (const_tree);
11990 /* When --enable-checking=fold, compute a digest of expr before
11991 and after actual fold call to see if fold did not accidentally
11992 change original expr. */
11994 tree
11995 fold (tree expr)
11997 tree ret;
11998 struct md5_ctx ctx;
11999 unsigned char checksum_before[16], checksum_after[16];
12000 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12002 md5_init_ctx (&ctx);
12003 fold_checksum_tree (expr, &ctx, &ht);
12004 md5_finish_ctx (&ctx, checksum_before);
12005 ht.empty ();
12007 ret = fold_1 (expr);
12009 md5_init_ctx (&ctx);
12010 fold_checksum_tree (expr, &ctx, &ht);
12011 md5_finish_ctx (&ctx, checksum_after);
12013 if (memcmp (checksum_before, checksum_after, 16))
12014 fold_check_failed (expr, ret);
12016 return ret;
12019 void
12020 print_fold_checksum (const_tree expr)
12022 struct md5_ctx ctx;
12023 unsigned char checksum[16], cnt;
12024 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12026 md5_init_ctx (&ctx);
12027 fold_checksum_tree (expr, &ctx, &ht);
12028 md5_finish_ctx (&ctx, checksum);
12029 for (cnt = 0; cnt < 16; ++cnt)
12030 fprintf (stderr, "%02x", checksum[cnt]);
12031 putc ('\n', stderr);
12034 static void
12035 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12037 internal_error ("fold check: original tree changed by fold");
12040 static void
12041 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12042 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12044 const tree_node **slot;
12045 enum tree_code code;
12046 union tree_node buf;
12047 int i, len;
12049 recursive_label:
12050 if (expr == NULL)
12051 return;
12052 slot = ht->find_slot (expr, INSERT);
12053 if (*slot != NULL)
12054 return;
12055 *slot = expr;
12056 code = TREE_CODE (expr);
12057 if (TREE_CODE_CLASS (code) == tcc_declaration
12058 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12060 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12061 memcpy ((char *) &buf, expr, tree_size (expr));
12062 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12063 buf.decl_with_vis.symtab_node = NULL;
12064 expr = (tree) &buf;
12066 else if (TREE_CODE_CLASS (code) == tcc_type
12067 && (TYPE_POINTER_TO (expr)
12068 || TYPE_REFERENCE_TO (expr)
12069 || TYPE_CACHED_VALUES_P (expr)
12070 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12071 || TYPE_NEXT_VARIANT (expr)
12072 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12074 /* Allow these fields to be modified. */
12075 tree tmp;
12076 memcpy ((char *) &buf, expr, tree_size (expr));
12077 expr = tmp = (tree) &buf;
12078 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12079 TYPE_POINTER_TO (tmp) = NULL;
12080 TYPE_REFERENCE_TO (tmp) = NULL;
12081 TYPE_NEXT_VARIANT (tmp) = NULL;
12082 TYPE_ALIAS_SET (tmp) = -1;
12083 if (TYPE_CACHED_VALUES_P (tmp))
12085 TYPE_CACHED_VALUES_P (tmp) = 0;
12086 TYPE_CACHED_VALUES (tmp) = NULL;
12089 md5_process_bytes (expr, tree_size (expr), ctx);
12090 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12091 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12092 if (TREE_CODE_CLASS (code) != tcc_type
12093 && TREE_CODE_CLASS (code) != tcc_declaration
12094 && code != TREE_LIST
12095 && code != SSA_NAME
12096 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12097 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12098 switch (TREE_CODE_CLASS (code))
12100 case tcc_constant:
12101 switch (code)
12103 case STRING_CST:
12104 md5_process_bytes (TREE_STRING_POINTER (expr),
12105 TREE_STRING_LENGTH (expr), ctx);
12106 break;
12107 case COMPLEX_CST:
12108 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12109 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12110 break;
12111 case VECTOR_CST:
12112 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12113 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12114 break;
12115 default:
12116 break;
12118 break;
12119 case tcc_exceptional:
12120 switch (code)
12122 case TREE_LIST:
12123 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12124 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12125 expr = TREE_CHAIN (expr);
12126 goto recursive_label;
12127 break;
12128 case TREE_VEC:
12129 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12130 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12131 break;
12132 default:
12133 break;
12135 break;
12136 case tcc_expression:
12137 case tcc_reference:
12138 case tcc_comparison:
12139 case tcc_unary:
12140 case tcc_binary:
12141 case tcc_statement:
12142 case tcc_vl_exp:
12143 len = TREE_OPERAND_LENGTH (expr);
12144 for (i = 0; i < len; ++i)
12145 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12146 break;
12147 case tcc_declaration:
12148 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12149 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12150 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12152 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12153 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12154 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12155 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12156 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12159 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12161 if (TREE_CODE (expr) == FUNCTION_DECL)
12163 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12164 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12166 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12168 break;
12169 case tcc_type:
12170 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12171 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12172 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12173 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12174 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12175 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12176 if (INTEGRAL_TYPE_P (expr)
12177 || SCALAR_FLOAT_TYPE_P (expr))
12179 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12180 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12182 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12183 if (TREE_CODE (expr) == RECORD_TYPE
12184 || TREE_CODE (expr) == UNION_TYPE
12185 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12186 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12187 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12188 break;
12189 default:
12190 break;
12194 /* Helper function for outputting the checksum of a tree T. When
12195 debugging with gdb, you can "define mynext" to be "next" followed
12196 by "call debug_fold_checksum (op0)", then just trace down till the
12197 outputs differ. */
12199 DEBUG_FUNCTION void
12200 debug_fold_checksum (const_tree t)
12202 int i;
12203 unsigned char checksum[16];
12204 struct md5_ctx ctx;
12205 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12207 md5_init_ctx (&ctx);
12208 fold_checksum_tree (t, &ctx, &ht);
12209 md5_finish_ctx (&ctx, checksum);
12210 ht.empty ();
12212 for (i = 0; i < 16; i++)
12213 fprintf (stderr, "%d ", checksum[i]);
12215 fprintf (stderr, "\n");
12218 #endif
12220 /* Fold a unary tree expression with code CODE of type TYPE with an
12221 operand OP0. LOC is the location of the resulting expression.
12222 Return a folded expression if successful. Otherwise, return a tree
12223 expression with code CODE of type TYPE with an operand OP0. */
12225 tree
12226 fold_build1_stat_loc (location_t loc,
12227 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12229 tree tem;
12230 #ifdef ENABLE_FOLD_CHECKING
12231 unsigned char checksum_before[16], checksum_after[16];
12232 struct md5_ctx ctx;
12233 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12235 md5_init_ctx (&ctx);
12236 fold_checksum_tree (op0, &ctx, &ht);
12237 md5_finish_ctx (&ctx, checksum_before);
12238 ht.empty ();
12239 #endif
12241 tem = fold_unary_loc (loc, code, type, op0);
12242 if (!tem)
12243 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12245 #ifdef ENABLE_FOLD_CHECKING
12246 md5_init_ctx (&ctx);
12247 fold_checksum_tree (op0, &ctx, &ht);
12248 md5_finish_ctx (&ctx, checksum_after);
12250 if (memcmp (checksum_before, checksum_after, 16))
12251 fold_check_failed (op0, tem);
12252 #endif
12253 return tem;
12256 /* Fold a binary tree expression with code CODE of type TYPE with
12257 operands OP0 and OP1. LOC is the location of the resulting
12258 expression. Return a folded expression if successful. Otherwise,
12259 return a tree expression with code CODE of type TYPE with operands
12260 OP0 and OP1. */
12262 tree
12263 fold_build2_stat_loc (location_t loc,
12264 enum tree_code code, tree type, tree op0, tree op1
12265 MEM_STAT_DECL)
12267 tree tem;
12268 #ifdef ENABLE_FOLD_CHECKING
12269 unsigned char checksum_before_op0[16],
12270 checksum_before_op1[16],
12271 checksum_after_op0[16],
12272 checksum_after_op1[16];
12273 struct md5_ctx ctx;
12274 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12276 md5_init_ctx (&ctx);
12277 fold_checksum_tree (op0, &ctx, &ht);
12278 md5_finish_ctx (&ctx, checksum_before_op0);
12279 ht.empty ();
12281 md5_init_ctx (&ctx);
12282 fold_checksum_tree (op1, &ctx, &ht);
12283 md5_finish_ctx (&ctx, checksum_before_op1);
12284 ht.empty ();
12285 #endif
12287 tem = fold_binary_loc (loc, code, type, op0, op1);
12288 if (!tem)
12289 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12291 #ifdef ENABLE_FOLD_CHECKING
12292 md5_init_ctx (&ctx);
12293 fold_checksum_tree (op0, &ctx, &ht);
12294 md5_finish_ctx (&ctx, checksum_after_op0);
12295 ht.empty ();
12297 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12298 fold_check_failed (op0, tem);
12300 md5_init_ctx (&ctx);
12301 fold_checksum_tree (op1, &ctx, &ht);
12302 md5_finish_ctx (&ctx, checksum_after_op1);
12304 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12305 fold_check_failed (op1, tem);
12306 #endif
12307 return tem;
12310 /* Fold a ternary tree expression with code CODE of type TYPE with
12311 operands OP0, OP1, and OP2. Return a folded expression if
12312 successful. Otherwise, return a tree expression with code CODE of
12313 type TYPE with operands OP0, OP1, and OP2. */
12315 tree
12316 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12317 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12319 tree tem;
12320 #ifdef ENABLE_FOLD_CHECKING
12321 unsigned char checksum_before_op0[16],
12322 checksum_before_op1[16],
12323 checksum_before_op2[16],
12324 checksum_after_op0[16],
12325 checksum_after_op1[16],
12326 checksum_after_op2[16];
12327 struct md5_ctx ctx;
12328 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12330 md5_init_ctx (&ctx);
12331 fold_checksum_tree (op0, &ctx, &ht);
12332 md5_finish_ctx (&ctx, checksum_before_op0);
12333 ht.empty ();
12335 md5_init_ctx (&ctx);
12336 fold_checksum_tree (op1, &ctx, &ht);
12337 md5_finish_ctx (&ctx, checksum_before_op1);
12338 ht.empty ();
12340 md5_init_ctx (&ctx);
12341 fold_checksum_tree (op2, &ctx, &ht);
12342 md5_finish_ctx (&ctx, checksum_before_op2);
12343 ht.empty ();
12344 #endif
12346 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12347 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12348 if (!tem)
12349 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12351 #ifdef ENABLE_FOLD_CHECKING
12352 md5_init_ctx (&ctx);
12353 fold_checksum_tree (op0, &ctx, &ht);
12354 md5_finish_ctx (&ctx, checksum_after_op0);
12355 ht.empty ();
12357 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12358 fold_check_failed (op0, tem);
12360 md5_init_ctx (&ctx);
12361 fold_checksum_tree (op1, &ctx, &ht);
12362 md5_finish_ctx (&ctx, checksum_after_op1);
12363 ht.empty ();
12365 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12366 fold_check_failed (op1, tem);
12368 md5_init_ctx (&ctx);
12369 fold_checksum_tree (op2, &ctx, &ht);
12370 md5_finish_ctx (&ctx, checksum_after_op2);
12372 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12373 fold_check_failed (op2, tem);
12374 #endif
12375 return tem;
12378 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12379 arguments in ARGARRAY, and a null static chain.
12380 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12381 of type TYPE from the given operands as constructed by build_call_array. */
12383 tree
12384 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12385 int nargs, tree *argarray)
12387 tree tem;
12388 #ifdef ENABLE_FOLD_CHECKING
12389 unsigned char checksum_before_fn[16],
12390 checksum_before_arglist[16],
12391 checksum_after_fn[16],
12392 checksum_after_arglist[16];
12393 struct md5_ctx ctx;
12394 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12395 int i;
12397 md5_init_ctx (&ctx);
12398 fold_checksum_tree (fn, &ctx, &ht);
12399 md5_finish_ctx (&ctx, checksum_before_fn);
12400 ht.empty ();
12402 md5_init_ctx (&ctx);
12403 for (i = 0; i < nargs; i++)
12404 fold_checksum_tree (argarray[i], &ctx, &ht);
12405 md5_finish_ctx (&ctx, checksum_before_arglist);
12406 ht.empty ();
12407 #endif
12409 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12410 if (!tem)
12411 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12413 #ifdef ENABLE_FOLD_CHECKING
12414 md5_init_ctx (&ctx);
12415 fold_checksum_tree (fn, &ctx, &ht);
12416 md5_finish_ctx (&ctx, checksum_after_fn);
12417 ht.empty ();
12419 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12420 fold_check_failed (fn, tem);
12422 md5_init_ctx (&ctx);
12423 for (i = 0; i < nargs; i++)
12424 fold_checksum_tree (argarray[i], &ctx, &ht);
12425 md5_finish_ctx (&ctx, checksum_after_arglist);
12427 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12428 fold_check_failed (NULL_TREE, tem);
12429 #endif
12430 return tem;
12433 /* Perform constant folding and related simplification of initializer
12434 expression EXPR. These behave identically to "fold_buildN" but ignore
12435 potential run-time traps and exceptions that fold must preserve. */
12437 #define START_FOLD_INIT \
12438 int saved_signaling_nans = flag_signaling_nans;\
12439 int saved_trapping_math = flag_trapping_math;\
12440 int saved_rounding_math = flag_rounding_math;\
12441 int saved_trapv = flag_trapv;\
12442 int saved_folding_initializer = folding_initializer;\
12443 flag_signaling_nans = 0;\
12444 flag_trapping_math = 0;\
12445 flag_rounding_math = 0;\
12446 flag_trapv = 0;\
12447 folding_initializer = 1;
12449 #define END_FOLD_INIT \
12450 flag_signaling_nans = saved_signaling_nans;\
12451 flag_trapping_math = saved_trapping_math;\
12452 flag_rounding_math = saved_rounding_math;\
12453 flag_trapv = saved_trapv;\
12454 folding_initializer = saved_folding_initializer;
12456 tree
12457 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12458 tree type, tree op)
12460 tree result;
12461 START_FOLD_INIT;
12463 result = fold_build1_loc (loc, code, type, op);
12465 END_FOLD_INIT;
12466 return result;
12469 tree
12470 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12471 tree type, tree op0, tree op1)
12473 tree result;
12474 START_FOLD_INIT;
12476 result = fold_build2_loc (loc, code, type, op0, op1);
12478 END_FOLD_INIT;
12479 return result;
12482 tree
12483 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12484 int nargs, tree *argarray)
12486 tree result;
12487 START_FOLD_INIT;
12489 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12491 END_FOLD_INIT;
12492 return result;
12495 #undef START_FOLD_INIT
12496 #undef END_FOLD_INIT
12498 /* Determine if first argument is a multiple of second argument. Return 0 if
12499 it is not, or we cannot easily determined it to be.
12501 An example of the sort of thing we care about (at this point; this routine
12502 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12503 fold cases do now) is discovering that
12505 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12507 is a multiple of
12509 SAVE_EXPR (J * 8)
12511 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12513 This code also handles discovering that
12515 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12517 is a multiple of 8 so we don't have to worry about dealing with a
12518 possible remainder.
12520 Note that we *look* inside a SAVE_EXPR only to determine how it was
12521 calculated; it is not safe for fold to do much of anything else with the
12522 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12523 at run time. For example, the latter example above *cannot* be implemented
12524 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12525 evaluation time of the original SAVE_EXPR is not necessarily the same at
12526 the time the new expression is evaluated. The only optimization of this
12527 sort that would be valid is changing
12529 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12531 divided by 8 to
12533 SAVE_EXPR (I) * SAVE_EXPR (J)
12535 (where the same SAVE_EXPR (J) is used in the original and the
12536 transformed version). */
12539 multiple_of_p (tree type, const_tree top, const_tree bottom)
12541 if (operand_equal_p (top, bottom, 0))
12542 return 1;
12544 if (TREE_CODE (type) != INTEGER_TYPE)
12545 return 0;
12547 switch (TREE_CODE (top))
12549 case BIT_AND_EXPR:
12550 /* Bitwise and provides a power of two multiple. If the mask is
12551 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12552 if (!integer_pow2p (bottom))
12553 return 0;
12554 /* FALLTHRU */
12556 case MULT_EXPR:
12557 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12558 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12560 case PLUS_EXPR:
12561 case MINUS_EXPR:
12562 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12563 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12565 case LSHIFT_EXPR:
12566 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12568 tree op1, t1;
12570 op1 = TREE_OPERAND (top, 1);
12571 /* const_binop may not detect overflow correctly,
12572 so check for it explicitly here. */
12573 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12574 && 0 != (t1 = fold_convert (type,
12575 const_binop (LSHIFT_EXPR,
12576 size_one_node,
12577 op1)))
12578 && !TREE_OVERFLOW (t1))
12579 return multiple_of_p (type, t1, bottom);
12581 return 0;
12583 case NOP_EXPR:
12584 /* Can't handle conversions from non-integral or wider integral type. */
12585 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12586 || (TYPE_PRECISION (type)
12587 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12588 return 0;
12590 /* .. fall through ... */
12592 case SAVE_EXPR:
12593 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12595 case COND_EXPR:
12596 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12597 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12599 case INTEGER_CST:
12600 if (TREE_CODE (bottom) != INTEGER_CST
12601 || integer_zerop (bottom)
12602 || (TYPE_UNSIGNED (type)
12603 && (tree_int_cst_sgn (top) < 0
12604 || tree_int_cst_sgn (bottom) < 0)))
12605 return 0;
12606 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12607 SIGNED);
12609 default:
12610 return 0;
12614 #define tree_expr_nonnegative_warnv_p(X, Y) \
12615 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12617 #define RECURSE(X) \
12618 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12620 /* Return true if CODE or TYPE is known to be non-negative. */
12622 static bool
12623 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12625 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12626 && truth_value_p (code))
12627 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12628 have a signed:1 type (where the value is -1 and 0). */
12629 return true;
12630 return false;
12633 /* Return true if (CODE OP0) is known to be non-negative. If the return
12634 value is based on the assumption that signed overflow is undefined,
12635 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12636 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12638 bool
12639 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12640 bool *strict_overflow_p, int depth)
12642 if (TYPE_UNSIGNED (type))
12643 return true;
12645 switch (code)
12647 case ABS_EXPR:
12648 /* We can't return 1 if flag_wrapv is set because
12649 ABS_EXPR<INT_MIN> = INT_MIN. */
12650 if (!ANY_INTEGRAL_TYPE_P (type))
12651 return true;
12652 if (TYPE_OVERFLOW_UNDEFINED (type))
12654 *strict_overflow_p = true;
12655 return true;
12657 break;
12659 case NON_LVALUE_EXPR:
12660 case FLOAT_EXPR:
12661 case FIX_TRUNC_EXPR:
12662 return RECURSE (op0);
12664 CASE_CONVERT:
12666 tree inner_type = TREE_TYPE (op0);
12667 tree outer_type = type;
12669 if (TREE_CODE (outer_type) == REAL_TYPE)
12671 if (TREE_CODE (inner_type) == REAL_TYPE)
12672 return RECURSE (op0);
12673 if (INTEGRAL_TYPE_P (inner_type))
12675 if (TYPE_UNSIGNED (inner_type))
12676 return true;
12677 return RECURSE (op0);
12680 else if (INTEGRAL_TYPE_P (outer_type))
12682 if (TREE_CODE (inner_type) == REAL_TYPE)
12683 return RECURSE (op0);
12684 if (INTEGRAL_TYPE_P (inner_type))
12685 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12686 && TYPE_UNSIGNED (inner_type);
12689 break;
12691 default:
12692 return tree_simple_nonnegative_warnv_p (code, type);
12695 /* We don't know sign of `t', so be conservative and return false. */
12696 return false;
12699 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12700 value is based on the assumption that signed overflow is undefined,
12701 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12702 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12704 bool
12705 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12706 tree op1, bool *strict_overflow_p,
12707 int depth)
12709 if (TYPE_UNSIGNED (type))
12710 return true;
12712 switch (code)
12714 case POINTER_PLUS_EXPR:
12715 case PLUS_EXPR:
12716 if (FLOAT_TYPE_P (type))
12717 return RECURSE (op0) && RECURSE (op1);
12719 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12720 both unsigned and at least 2 bits shorter than the result. */
12721 if (TREE_CODE (type) == INTEGER_TYPE
12722 && TREE_CODE (op0) == NOP_EXPR
12723 && TREE_CODE (op1) == NOP_EXPR)
12725 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12726 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12727 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12728 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12730 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12731 TYPE_PRECISION (inner2)) + 1;
12732 return prec < TYPE_PRECISION (type);
12735 break;
12737 case MULT_EXPR:
12738 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12740 /* x * x is always non-negative for floating point x
12741 or without overflow. */
12742 if (operand_equal_p (op0, op1, 0)
12743 || (RECURSE (op0) && RECURSE (op1)))
12745 if (ANY_INTEGRAL_TYPE_P (type)
12746 && TYPE_OVERFLOW_UNDEFINED (type))
12747 *strict_overflow_p = true;
12748 return true;
12752 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12753 both unsigned and their total bits is shorter than the result. */
12754 if (TREE_CODE (type) == INTEGER_TYPE
12755 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12756 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12758 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12759 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12760 : TREE_TYPE (op0);
12761 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12762 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12763 : TREE_TYPE (op1);
12765 bool unsigned0 = TYPE_UNSIGNED (inner0);
12766 bool unsigned1 = TYPE_UNSIGNED (inner1);
12768 if (TREE_CODE (op0) == INTEGER_CST)
12769 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12771 if (TREE_CODE (op1) == INTEGER_CST)
12772 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12774 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12775 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12777 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12778 ? tree_int_cst_min_precision (op0, UNSIGNED)
12779 : TYPE_PRECISION (inner0);
12781 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12782 ? tree_int_cst_min_precision (op1, UNSIGNED)
12783 : TYPE_PRECISION (inner1);
12785 return precision0 + precision1 < TYPE_PRECISION (type);
12788 return false;
12790 case BIT_AND_EXPR:
12791 case MAX_EXPR:
12792 return RECURSE (op0) || RECURSE (op1);
12794 case BIT_IOR_EXPR:
12795 case BIT_XOR_EXPR:
12796 case MIN_EXPR:
12797 case RDIV_EXPR:
12798 case TRUNC_DIV_EXPR:
12799 case CEIL_DIV_EXPR:
12800 case FLOOR_DIV_EXPR:
12801 case ROUND_DIV_EXPR:
12802 return RECURSE (op0) && RECURSE (op1);
12804 case TRUNC_MOD_EXPR:
12805 return RECURSE (op0);
12807 case FLOOR_MOD_EXPR:
12808 return RECURSE (op1);
12810 case CEIL_MOD_EXPR:
12811 case ROUND_MOD_EXPR:
12812 default:
12813 return tree_simple_nonnegative_warnv_p (code, type);
12816 /* We don't know sign of `t', so be conservative and return false. */
12817 return false;
12820 /* Return true if T is known to be non-negative. If the return
12821 value is based on the assumption that signed overflow is undefined,
12822 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12823 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12825 bool
12826 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12828 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12829 return true;
12831 switch (TREE_CODE (t))
12833 case INTEGER_CST:
12834 return tree_int_cst_sgn (t) >= 0;
12836 case REAL_CST:
12837 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12839 case FIXED_CST:
12840 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12842 case COND_EXPR:
12843 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12845 case SSA_NAME:
12846 /* Limit the depth of recursion to avoid quadratic behavior.
12847 This is expected to catch almost all occurrences in practice.
12848 If this code misses important cases that unbounded recursion
12849 would not, passes that need this information could be revised
12850 to provide it through dataflow propagation. */
12851 return (!name_registered_for_update_p (t)
12852 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12853 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12854 strict_overflow_p, depth));
12856 default:
12857 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12861 /* Return true if T is known to be non-negative. If the return
12862 value is based on the assumption that signed overflow is undefined,
12863 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12864 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12866 bool
12867 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12868 bool *strict_overflow_p, int depth)
12870 switch (fn)
12872 CASE_CFN_ACOS:
12873 CASE_CFN_ACOSH:
12874 CASE_CFN_CABS:
12875 CASE_CFN_COSH:
12876 CASE_CFN_ERFC:
12877 CASE_CFN_EXP:
12878 CASE_CFN_EXP10:
12879 CASE_CFN_EXP2:
12880 CASE_CFN_FABS:
12881 CASE_CFN_FDIM:
12882 CASE_CFN_HYPOT:
12883 CASE_CFN_POW10:
12884 CASE_CFN_FFS:
12885 CASE_CFN_PARITY:
12886 CASE_CFN_POPCOUNT:
12887 CASE_CFN_CLZ:
12888 CASE_CFN_CLRSB:
12889 case CFN_BUILT_IN_BSWAP32:
12890 case CFN_BUILT_IN_BSWAP64:
12891 /* Always true. */
12892 return true;
12894 CASE_CFN_SQRT:
12895 /* sqrt(-0.0) is -0.0. */
12896 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12897 return true;
12898 return RECURSE (arg0);
12900 CASE_CFN_ASINH:
12901 CASE_CFN_ATAN:
12902 CASE_CFN_ATANH:
12903 CASE_CFN_CBRT:
12904 CASE_CFN_CEIL:
12905 CASE_CFN_ERF:
12906 CASE_CFN_EXPM1:
12907 CASE_CFN_FLOOR:
12908 CASE_CFN_FMOD:
12909 CASE_CFN_FREXP:
12910 CASE_CFN_ICEIL:
12911 CASE_CFN_IFLOOR:
12912 CASE_CFN_IRINT:
12913 CASE_CFN_IROUND:
12914 CASE_CFN_LCEIL:
12915 CASE_CFN_LDEXP:
12916 CASE_CFN_LFLOOR:
12917 CASE_CFN_LLCEIL:
12918 CASE_CFN_LLFLOOR:
12919 CASE_CFN_LLRINT:
12920 CASE_CFN_LLROUND:
12921 CASE_CFN_LRINT:
12922 CASE_CFN_LROUND:
12923 CASE_CFN_MODF:
12924 CASE_CFN_NEARBYINT:
12925 CASE_CFN_RINT:
12926 CASE_CFN_ROUND:
12927 CASE_CFN_SCALB:
12928 CASE_CFN_SCALBLN:
12929 CASE_CFN_SCALBN:
12930 CASE_CFN_SIGNBIT:
12931 CASE_CFN_SIGNIFICAND:
12932 CASE_CFN_SINH:
12933 CASE_CFN_TANH:
12934 CASE_CFN_TRUNC:
12935 /* True if the 1st argument is nonnegative. */
12936 return RECURSE (arg0);
12938 CASE_CFN_FMAX:
12939 /* True if the 1st OR 2nd arguments are nonnegative. */
12940 return RECURSE (arg0) || RECURSE (arg1);
12942 CASE_CFN_FMIN:
12943 /* True if the 1st AND 2nd arguments are nonnegative. */
12944 return RECURSE (arg0) && RECURSE (arg1);
12946 CASE_CFN_COPYSIGN:
12947 /* True if the 2nd argument is nonnegative. */
12948 return RECURSE (arg1);
12950 CASE_CFN_POWI:
12951 /* True if the 1st argument is nonnegative or the second
12952 argument is an even integer. */
12953 if (TREE_CODE (arg1) == INTEGER_CST
12954 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12955 return true;
12956 return RECURSE (arg0);
12958 CASE_CFN_POW:
12959 /* True if the 1st argument is nonnegative or the second
12960 argument is an even integer valued real. */
12961 if (TREE_CODE (arg1) == REAL_CST)
12963 REAL_VALUE_TYPE c;
12964 HOST_WIDE_INT n;
12966 c = TREE_REAL_CST (arg1);
12967 n = real_to_integer (&c);
12968 if ((n & 1) == 0)
12970 REAL_VALUE_TYPE cint;
12971 real_from_integer (&cint, VOIDmode, n, SIGNED);
12972 if (real_identical (&c, &cint))
12973 return true;
12976 return RECURSE (arg0);
12978 default:
12979 break;
12981 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
12984 /* Return true if T is known to be non-negative. If the return
12985 value is based on the assumption that signed overflow is undefined,
12986 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12987 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12989 static bool
12990 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12992 enum tree_code code = TREE_CODE (t);
12993 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12994 return true;
12996 switch (code)
12998 case TARGET_EXPR:
13000 tree temp = TARGET_EXPR_SLOT (t);
13001 t = TARGET_EXPR_INITIAL (t);
13003 /* If the initializer is non-void, then it's a normal expression
13004 that will be assigned to the slot. */
13005 if (!VOID_TYPE_P (t))
13006 return RECURSE (t);
13008 /* Otherwise, the initializer sets the slot in some way. One common
13009 way is an assignment statement at the end of the initializer. */
13010 while (1)
13012 if (TREE_CODE (t) == BIND_EXPR)
13013 t = expr_last (BIND_EXPR_BODY (t));
13014 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13015 || TREE_CODE (t) == TRY_CATCH_EXPR)
13016 t = expr_last (TREE_OPERAND (t, 0));
13017 else if (TREE_CODE (t) == STATEMENT_LIST)
13018 t = expr_last (t);
13019 else
13020 break;
13022 if (TREE_CODE (t) == MODIFY_EXPR
13023 && TREE_OPERAND (t, 0) == temp)
13024 return RECURSE (TREE_OPERAND (t, 1));
13026 return false;
13029 case CALL_EXPR:
13031 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13032 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13034 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13035 get_call_combined_fn (t),
13036 arg0,
13037 arg1,
13038 strict_overflow_p, depth);
13040 case COMPOUND_EXPR:
13041 case MODIFY_EXPR:
13042 return RECURSE (TREE_OPERAND (t, 1));
13044 case BIND_EXPR:
13045 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13047 case SAVE_EXPR:
13048 return RECURSE (TREE_OPERAND (t, 0));
13050 default:
13051 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13055 #undef RECURSE
13056 #undef tree_expr_nonnegative_warnv_p
13058 /* Return true if T is known to be non-negative. If the return
13059 value is based on the assumption that signed overflow is undefined,
13060 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13061 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13063 bool
13064 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13066 enum tree_code code;
13067 if (t == error_mark_node)
13068 return false;
13070 code = TREE_CODE (t);
13071 switch (TREE_CODE_CLASS (code))
13073 case tcc_binary:
13074 case tcc_comparison:
13075 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13076 TREE_TYPE (t),
13077 TREE_OPERAND (t, 0),
13078 TREE_OPERAND (t, 1),
13079 strict_overflow_p, depth);
13081 case tcc_unary:
13082 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13083 TREE_TYPE (t),
13084 TREE_OPERAND (t, 0),
13085 strict_overflow_p, depth);
13087 case tcc_constant:
13088 case tcc_declaration:
13089 case tcc_reference:
13090 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13092 default:
13093 break;
13096 switch (code)
13098 case TRUTH_AND_EXPR:
13099 case TRUTH_OR_EXPR:
13100 case TRUTH_XOR_EXPR:
13101 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13102 TREE_TYPE (t),
13103 TREE_OPERAND (t, 0),
13104 TREE_OPERAND (t, 1),
13105 strict_overflow_p, depth);
13106 case TRUTH_NOT_EXPR:
13107 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13108 TREE_TYPE (t),
13109 TREE_OPERAND (t, 0),
13110 strict_overflow_p, depth);
13112 case COND_EXPR:
13113 case CONSTRUCTOR:
13114 case OBJ_TYPE_REF:
13115 case ASSERT_EXPR:
13116 case ADDR_EXPR:
13117 case WITH_SIZE_EXPR:
13118 case SSA_NAME:
13119 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13121 default:
13122 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13126 /* Return true if `t' is known to be non-negative. Handle warnings
13127 about undefined signed overflow. */
13129 bool
13130 tree_expr_nonnegative_p (tree t)
13132 bool ret, strict_overflow_p;
13134 strict_overflow_p = false;
13135 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13136 if (strict_overflow_p)
13137 fold_overflow_warning (("assuming signed overflow does not occur when "
13138 "determining that expression is always "
13139 "non-negative"),
13140 WARN_STRICT_OVERFLOW_MISC);
13141 return ret;
13145 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13146 For floating point we further ensure that T is not denormal.
13147 Similar logic is present in nonzero_address in rtlanal.h.
13149 If the return value is based on the assumption that signed overflow
13150 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13151 change *STRICT_OVERFLOW_P. */
13153 bool
13154 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13155 bool *strict_overflow_p)
13157 switch (code)
13159 case ABS_EXPR:
13160 return tree_expr_nonzero_warnv_p (op0,
13161 strict_overflow_p);
13163 case NOP_EXPR:
13165 tree inner_type = TREE_TYPE (op0);
13166 tree outer_type = type;
13168 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13169 && tree_expr_nonzero_warnv_p (op0,
13170 strict_overflow_p));
13172 break;
13174 case NON_LVALUE_EXPR:
13175 return tree_expr_nonzero_warnv_p (op0,
13176 strict_overflow_p);
13178 default:
13179 break;
13182 return false;
13185 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13186 For floating point we further ensure that T is not denormal.
13187 Similar logic is present in nonzero_address in rtlanal.h.
13189 If the return value is based on the assumption that signed overflow
13190 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13191 change *STRICT_OVERFLOW_P. */
13193 bool
13194 tree_binary_nonzero_warnv_p (enum tree_code code,
13195 tree type,
13196 tree op0,
13197 tree op1, bool *strict_overflow_p)
13199 bool sub_strict_overflow_p;
13200 switch (code)
13202 case POINTER_PLUS_EXPR:
13203 case PLUS_EXPR:
13204 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13206 /* With the presence of negative values it is hard
13207 to say something. */
13208 sub_strict_overflow_p = false;
13209 if (!tree_expr_nonnegative_warnv_p (op0,
13210 &sub_strict_overflow_p)
13211 || !tree_expr_nonnegative_warnv_p (op1,
13212 &sub_strict_overflow_p))
13213 return false;
13214 /* One of operands must be positive and the other non-negative. */
13215 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13216 overflows, on a twos-complement machine the sum of two
13217 nonnegative numbers can never be zero. */
13218 return (tree_expr_nonzero_warnv_p (op0,
13219 strict_overflow_p)
13220 || tree_expr_nonzero_warnv_p (op1,
13221 strict_overflow_p));
13223 break;
13225 case MULT_EXPR:
13226 if (TYPE_OVERFLOW_UNDEFINED (type))
13228 if (tree_expr_nonzero_warnv_p (op0,
13229 strict_overflow_p)
13230 && tree_expr_nonzero_warnv_p (op1,
13231 strict_overflow_p))
13233 *strict_overflow_p = true;
13234 return true;
13237 break;
13239 case MIN_EXPR:
13240 sub_strict_overflow_p = false;
13241 if (tree_expr_nonzero_warnv_p (op0,
13242 &sub_strict_overflow_p)
13243 && tree_expr_nonzero_warnv_p (op1,
13244 &sub_strict_overflow_p))
13246 if (sub_strict_overflow_p)
13247 *strict_overflow_p = true;
13249 break;
13251 case MAX_EXPR:
13252 sub_strict_overflow_p = false;
13253 if (tree_expr_nonzero_warnv_p (op0,
13254 &sub_strict_overflow_p))
13256 if (sub_strict_overflow_p)
13257 *strict_overflow_p = true;
13259 /* When both operands are nonzero, then MAX must be too. */
13260 if (tree_expr_nonzero_warnv_p (op1,
13261 strict_overflow_p))
13262 return true;
13264 /* MAX where operand 0 is positive is positive. */
13265 return tree_expr_nonnegative_warnv_p (op0,
13266 strict_overflow_p);
13268 /* MAX where operand 1 is positive is positive. */
13269 else if (tree_expr_nonzero_warnv_p (op1,
13270 &sub_strict_overflow_p)
13271 && tree_expr_nonnegative_warnv_p (op1,
13272 &sub_strict_overflow_p))
13274 if (sub_strict_overflow_p)
13275 *strict_overflow_p = true;
13276 return true;
13278 break;
13280 case BIT_IOR_EXPR:
13281 return (tree_expr_nonzero_warnv_p (op1,
13282 strict_overflow_p)
13283 || tree_expr_nonzero_warnv_p (op0,
13284 strict_overflow_p));
13286 default:
13287 break;
13290 return false;
13293 /* Return true when T is an address and is known to be nonzero.
13294 For floating point we further ensure that T is not denormal.
13295 Similar logic is present in nonzero_address in rtlanal.h.
13297 If the return value is based on the assumption that signed overflow
13298 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13299 change *STRICT_OVERFLOW_P. */
13301 bool
13302 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13304 bool sub_strict_overflow_p;
13305 switch (TREE_CODE (t))
13307 case INTEGER_CST:
13308 return !integer_zerop (t);
13310 case ADDR_EXPR:
13312 tree base = TREE_OPERAND (t, 0);
13314 if (!DECL_P (base))
13315 base = get_base_address (base);
13317 if (base && TREE_CODE (base) == TARGET_EXPR)
13318 base = TARGET_EXPR_SLOT (base);
13320 if (!base)
13321 return false;
13323 /* For objects in symbol table check if we know they are non-zero.
13324 Don't do anything for variables and functions before symtab is built;
13325 it is quite possible that they will be declared weak later. */
13326 int nonzero_addr = maybe_nonzero_address (base);
13327 if (nonzero_addr >= 0)
13328 return nonzero_addr;
13330 /* Function local objects are never NULL. */
13331 if (DECL_P (base)
13332 && (DECL_CONTEXT (base)
13333 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13334 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13335 return true;
13337 /* Constants are never weak. */
13338 if (CONSTANT_CLASS_P (base))
13339 return true;
13341 return false;
13344 case COND_EXPR:
13345 sub_strict_overflow_p = false;
13346 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13347 &sub_strict_overflow_p)
13348 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13349 &sub_strict_overflow_p))
13351 if (sub_strict_overflow_p)
13352 *strict_overflow_p = true;
13353 return true;
13355 break;
13357 default:
13358 break;
13360 return false;
13363 #define integer_valued_real_p(X) \
13364 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13366 #define RECURSE(X) \
13367 ((integer_valued_real_p) (X, depth + 1))
13369 /* Return true if the floating point result of (CODE OP0) has an
13370 integer value. We also allow +Inf, -Inf and NaN to be considered
13371 integer values. Return false for signaling NaN.
13373 DEPTH is the current nesting depth of the query. */
13375 bool
13376 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13378 switch (code)
13380 case FLOAT_EXPR:
13381 return true;
13383 case ABS_EXPR:
13384 return RECURSE (op0);
13386 CASE_CONVERT:
13388 tree type = TREE_TYPE (op0);
13389 if (TREE_CODE (type) == INTEGER_TYPE)
13390 return true;
13391 if (TREE_CODE (type) == REAL_TYPE)
13392 return RECURSE (op0);
13393 break;
13396 default:
13397 break;
13399 return false;
13402 /* Return true if the floating point result of (CODE OP0 OP1) has an
13403 integer value. We also allow +Inf, -Inf and NaN to be considered
13404 integer values. Return false for signaling NaN.
13406 DEPTH is the current nesting depth of the query. */
13408 bool
13409 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13411 switch (code)
13413 case PLUS_EXPR:
13414 case MINUS_EXPR:
13415 case MULT_EXPR:
13416 case MIN_EXPR:
13417 case MAX_EXPR:
13418 return RECURSE (op0) && RECURSE (op1);
13420 default:
13421 break;
13423 return false;
13426 /* Return true if the floating point result of calling FNDECL with arguments
13427 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13428 considered integer values. Return false for signaling NaN. If FNDECL
13429 takes fewer than 2 arguments, the remaining ARGn are null.
13431 DEPTH is the current nesting depth of the query. */
13433 bool
13434 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13436 switch (fn)
13438 CASE_CFN_CEIL:
13439 CASE_CFN_FLOOR:
13440 CASE_CFN_NEARBYINT:
13441 CASE_CFN_RINT:
13442 CASE_CFN_ROUND:
13443 CASE_CFN_TRUNC:
13444 return true;
13446 CASE_CFN_FMIN:
13447 CASE_CFN_FMAX:
13448 return RECURSE (arg0) && RECURSE (arg1);
13450 default:
13451 break;
13453 return false;
13456 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13457 has an integer value. We also allow +Inf, -Inf and NaN to be
13458 considered integer values. Return false for signaling NaN.
13460 DEPTH is the current nesting depth of the query. */
13462 bool
13463 integer_valued_real_single_p (tree t, int depth)
13465 switch (TREE_CODE (t))
13467 case REAL_CST:
13468 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13470 case COND_EXPR:
13471 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13473 case SSA_NAME:
13474 /* Limit the depth of recursion to avoid quadratic behavior.
13475 This is expected to catch almost all occurrences in practice.
13476 If this code misses important cases that unbounded recursion
13477 would not, passes that need this information could be revised
13478 to provide it through dataflow propagation. */
13479 return (!name_registered_for_update_p (t)
13480 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13481 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13482 depth));
13484 default:
13485 break;
13487 return false;
13490 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13491 has an integer value. We also allow +Inf, -Inf and NaN to be
13492 considered integer values. Return false for signaling NaN.
13494 DEPTH is the current nesting depth of the query. */
13496 static bool
13497 integer_valued_real_invalid_p (tree t, int depth)
13499 switch (TREE_CODE (t))
13501 case COMPOUND_EXPR:
13502 case MODIFY_EXPR:
13503 case BIND_EXPR:
13504 return RECURSE (TREE_OPERAND (t, 1));
13506 case SAVE_EXPR:
13507 return RECURSE (TREE_OPERAND (t, 0));
13509 default:
13510 break;
13512 return false;
13515 #undef RECURSE
13516 #undef integer_valued_real_p
13518 /* Return true if the floating point expression T has an integer value.
13519 We also allow +Inf, -Inf and NaN to be considered integer values.
13520 Return false for signaling NaN.
13522 DEPTH is the current nesting depth of the query. */
13524 bool
13525 integer_valued_real_p (tree t, int depth)
13527 if (t == error_mark_node)
13528 return false;
13530 tree_code code = TREE_CODE (t);
13531 switch (TREE_CODE_CLASS (code))
13533 case tcc_binary:
13534 case tcc_comparison:
13535 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13536 TREE_OPERAND (t, 1), depth);
13538 case tcc_unary:
13539 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13541 case tcc_constant:
13542 case tcc_declaration:
13543 case tcc_reference:
13544 return integer_valued_real_single_p (t, depth);
13546 default:
13547 break;
13550 switch (code)
13552 case COND_EXPR:
13553 case SSA_NAME:
13554 return integer_valued_real_single_p (t, depth);
13556 case CALL_EXPR:
13558 tree arg0 = (call_expr_nargs (t) > 0
13559 ? CALL_EXPR_ARG (t, 0)
13560 : NULL_TREE);
13561 tree arg1 = (call_expr_nargs (t) > 1
13562 ? CALL_EXPR_ARG (t, 1)
13563 : NULL_TREE);
13564 return integer_valued_real_call_p (get_call_combined_fn (t),
13565 arg0, arg1, depth);
13568 default:
13569 return integer_valued_real_invalid_p (t, depth);
13573 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13574 attempt to fold the expression to a constant without modifying TYPE,
13575 OP0 or OP1.
13577 If the expression could be simplified to a constant, then return
13578 the constant. If the expression would not be simplified to a
13579 constant, then return NULL_TREE. */
13581 tree
13582 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13584 tree tem = fold_binary (code, type, op0, op1);
13585 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13588 /* Given the components of a unary expression CODE, TYPE and OP0,
13589 attempt to fold the expression to a constant without modifying
13590 TYPE or OP0.
13592 If the expression could be simplified to a constant, then return
13593 the constant. If the expression would not be simplified to a
13594 constant, then return NULL_TREE. */
13596 tree
13597 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13599 tree tem = fold_unary (code, type, op0);
13600 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13603 /* If EXP represents referencing an element in a constant string
13604 (either via pointer arithmetic or array indexing), return the
13605 tree representing the value accessed, otherwise return NULL. */
13607 tree
13608 fold_read_from_constant_string (tree exp)
13610 if ((TREE_CODE (exp) == INDIRECT_REF
13611 || TREE_CODE (exp) == ARRAY_REF)
13612 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13614 tree exp1 = TREE_OPERAND (exp, 0);
13615 tree index;
13616 tree string;
13617 location_t loc = EXPR_LOCATION (exp);
13619 if (TREE_CODE (exp) == INDIRECT_REF)
13620 string = string_constant (exp1, &index);
13621 else
13623 tree low_bound = array_ref_low_bound (exp);
13624 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13626 /* Optimize the special-case of a zero lower bound.
13628 We convert the low_bound to sizetype to avoid some problems
13629 with constant folding. (E.g. suppose the lower bound is 1,
13630 and its mode is QI. Without the conversion,l (ARRAY
13631 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13632 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13633 if (! integer_zerop (low_bound))
13634 index = size_diffop_loc (loc, index,
13635 fold_convert_loc (loc, sizetype, low_bound));
13637 string = exp1;
13640 if (string
13641 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13642 && TREE_CODE (string) == STRING_CST
13643 && TREE_CODE (index) == INTEGER_CST
13644 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13645 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13646 == MODE_INT)
13647 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13648 return build_int_cst_type (TREE_TYPE (exp),
13649 (TREE_STRING_POINTER (string)
13650 [TREE_INT_CST_LOW (index)]));
13652 return NULL;
13655 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13656 an integer constant, real, or fixed-point constant.
13658 TYPE is the type of the result. */
13660 static tree
13661 fold_negate_const (tree arg0, tree type)
13663 tree t = NULL_TREE;
13665 switch (TREE_CODE (arg0))
13667 case INTEGER_CST:
13669 bool overflow;
13670 wide_int val = wi::neg (arg0, &overflow);
13671 t = force_fit_type (type, val, 1,
13672 (overflow | TREE_OVERFLOW (arg0))
13673 && !TYPE_UNSIGNED (type));
13674 break;
13677 case REAL_CST:
13678 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13679 break;
13681 case FIXED_CST:
13683 FIXED_VALUE_TYPE f;
13684 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13685 &(TREE_FIXED_CST (arg0)), NULL,
13686 TYPE_SATURATING (type));
13687 t = build_fixed (type, f);
13688 /* Propagate overflow flags. */
13689 if (overflow_p | TREE_OVERFLOW (arg0))
13690 TREE_OVERFLOW (t) = 1;
13691 break;
13694 default:
13695 gcc_unreachable ();
13698 return t;
13701 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13702 an integer constant or real constant.
13704 TYPE is the type of the result. */
13706 tree
13707 fold_abs_const (tree arg0, tree type)
13709 tree t = NULL_TREE;
13711 switch (TREE_CODE (arg0))
13713 case INTEGER_CST:
13715 /* If the value is unsigned or non-negative, then the absolute value
13716 is the same as the ordinary value. */
13717 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13718 t = arg0;
13720 /* If the value is negative, then the absolute value is
13721 its negation. */
13722 else
13724 bool overflow;
13725 wide_int val = wi::neg (arg0, &overflow);
13726 t = force_fit_type (type, val, -1,
13727 overflow | TREE_OVERFLOW (arg0));
13730 break;
13732 case REAL_CST:
13733 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13734 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13735 else
13736 t = arg0;
13737 break;
13739 default:
13740 gcc_unreachable ();
13743 return t;
13746 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13747 constant. TYPE is the type of the result. */
13749 static tree
13750 fold_not_const (const_tree arg0, tree type)
13752 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13754 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13757 /* Given CODE, a relational operator, the target type, TYPE and two
13758 constant operands OP0 and OP1, return the result of the
13759 relational operation. If the result is not a compile time
13760 constant, then return NULL_TREE. */
13762 static tree
13763 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13765 int result, invert;
13767 /* From here on, the only cases we handle are when the result is
13768 known to be a constant. */
13770 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13772 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13773 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13775 /* Handle the cases where either operand is a NaN. */
13776 if (real_isnan (c0) || real_isnan (c1))
13778 switch (code)
13780 case EQ_EXPR:
13781 case ORDERED_EXPR:
13782 result = 0;
13783 break;
13785 case NE_EXPR:
13786 case UNORDERED_EXPR:
13787 case UNLT_EXPR:
13788 case UNLE_EXPR:
13789 case UNGT_EXPR:
13790 case UNGE_EXPR:
13791 case UNEQ_EXPR:
13792 result = 1;
13793 break;
13795 case LT_EXPR:
13796 case LE_EXPR:
13797 case GT_EXPR:
13798 case GE_EXPR:
13799 case LTGT_EXPR:
13800 if (flag_trapping_math)
13801 return NULL_TREE;
13802 result = 0;
13803 break;
13805 default:
13806 gcc_unreachable ();
13809 return constant_boolean_node (result, type);
13812 return constant_boolean_node (real_compare (code, c0, c1), type);
13815 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13817 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13818 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13819 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13822 /* Handle equality/inequality of complex constants. */
13823 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13825 tree rcond = fold_relational_const (code, type,
13826 TREE_REALPART (op0),
13827 TREE_REALPART (op1));
13828 tree icond = fold_relational_const (code, type,
13829 TREE_IMAGPART (op0),
13830 TREE_IMAGPART (op1));
13831 if (code == EQ_EXPR)
13832 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13833 else if (code == NE_EXPR)
13834 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13835 else
13836 return NULL_TREE;
13839 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13841 if (!VECTOR_TYPE_P (type))
13843 /* Have vector comparison with scalar boolean result. */
13844 bool result = true;
13845 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13846 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13847 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13849 tree elem0 = VECTOR_CST_ELT (op0, i);
13850 tree elem1 = VECTOR_CST_ELT (op1, i);
13851 tree tmp = fold_relational_const (code, type, elem0, elem1);
13852 result &= integer_onep (tmp);
13854 if (code == NE_EXPR)
13855 result = !result;
13856 return constant_boolean_node (result, type);
13858 unsigned count = VECTOR_CST_NELTS (op0);
13859 tree *elts = XALLOCAVEC (tree, count);
13860 gcc_assert (VECTOR_CST_NELTS (op1) == count
13861 && TYPE_VECTOR_SUBPARTS (type) == count);
13863 for (unsigned i = 0; i < count; i++)
13865 tree elem_type = TREE_TYPE (type);
13866 tree elem0 = VECTOR_CST_ELT (op0, i);
13867 tree elem1 = VECTOR_CST_ELT (op1, i);
13869 tree tem = fold_relational_const (code, elem_type,
13870 elem0, elem1);
13872 if (tem == NULL_TREE)
13873 return NULL_TREE;
13875 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13878 return build_vector (type, elts);
13881 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13883 To compute GT, swap the arguments and do LT.
13884 To compute GE, do LT and invert the result.
13885 To compute LE, swap the arguments, do LT and invert the result.
13886 To compute NE, do EQ and invert the result.
13888 Therefore, the code below must handle only EQ and LT. */
13890 if (code == LE_EXPR || code == GT_EXPR)
13892 std::swap (op0, op1);
13893 code = swap_tree_comparison (code);
13896 /* Note that it is safe to invert for real values here because we
13897 have already handled the one case that it matters. */
13899 invert = 0;
13900 if (code == NE_EXPR || code == GE_EXPR)
13902 invert = 1;
13903 code = invert_tree_comparison (code, false);
13906 /* Compute a result for LT or EQ if args permit;
13907 Otherwise return T. */
13908 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13910 if (code == EQ_EXPR)
13911 result = tree_int_cst_equal (op0, op1);
13912 else
13913 result = tree_int_cst_lt (op0, op1);
13915 else
13916 return NULL_TREE;
13918 if (invert)
13919 result ^= 1;
13920 return constant_boolean_node (result, type);
13923 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13924 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13925 itself. */
13927 tree
13928 fold_build_cleanup_point_expr (tree type, tree expr)
13930 /* If the expression does not have side effects then we don't have to wrap
13931 it with a cleanup point expression. */
13932 if (!TREE_SIDE_EFFECTS (expr))
13933 return expr;
13935 /* If the expression is a return, check to see if the expression inside the
13936 return has no side effects or the right hand side of the modify expression
13937 inside the return. If either don't have side effects set we don't need to
13938 wrap the expression in a cleanup point expression. Note we don't check the
13939 left hand side of the modify because it should always be a return decl. */
13940 if (TREE_CODE (expr) == RETURN_EXPR)
13942 tree op = TREE_OPERAND (expr, 0);
13943 if (!op || !TREE_SIDE_EFFECTS (op))
13944 return expr;
13945 op = TREE_OPERAND (op, 1);
13946 if (!TREE_SIDE_EFFECTS (op))
13947 return expr;
13950 return build1 (CLEANUP_POINT_EXPR, type, expr);
13953 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13954 of an indirection through OP0, or NULL_TREE if no simplification is
13955 possible. */
13957 tree
13958 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13960 tree sub = op0;
13961 tree subtype;
13963 STRIP_NOPS (sub);
13964 subtype = TREE_TYPE (sub);
13965 if (!POINTER_TYPE_P (subtype))
13966 return NULL_TREE;
13968 if (TREE_CODE (sub) == ADDR_EXPR)
13970 tree op = TREE_OPERAND (sub, 0);
13971 tree optype = TREE_TYPE (op);
13972 /* *&CONST_DECL -> to the value of the const decl. */
13973 if (TREE_CODE (op) == CONST_DECL)
13974 return DECL_INITIAL (op);
13975 /* *&p => p; make sure to handle *&"str"[cst] here. */
13976 if (type == optype)
13978 tree fop = fold_read_from_constant_string (op);
13979 if (fop)
13980 return fop;
13981 else
13982 return op;
13984 /* *(foo *)&fooarray => fooarray[0] */
13985 else if (TREE_CODE (optype) == ARRAY_TYPE
13986 && type == TREE_TYPE (optype)
13987 && (!in_gimple_form
13988 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13990 tree type_domain = TYPE_DOMAIN (optype);
13991 tree min_val = size_zero_node;
13992 if (type_domain && TYPE_MIN_VALUE (type_domain))
13993 min_val = TYPE_MIN_VALUE (type_domain);
13994 if (in_gimple_form
13995 && TREE_CODE (min_val) != INTEGER_CST)
13996 return NULL_TREE;
13997 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13998 NULL_TREE, NULL_TREE);
14000 /* *(foo *)&complexfoo => __real__ complexfoo */
14001 else if (TREE_CODE (optype) == COMPLEX_TYPE
14002 && type == TREE_TYPE (optype))
14003 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14004 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14005 else if (TREE_CODE (optype) == VECTOR_TYPE
14006 && type == TREE_TYPE (optype))
14008 tree part_width = TYPE_SIZE (type);
14009 tree index = bitsize_int (0);
14010 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14014 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14015 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14017 tree op00 = TREE_OPERAND (sub, 0);
14018 tree op01 = TREE_OPERAND (sub, 1);
14020 STRIP_NOPS (op00);
14021 if (TREE_CODE (op00) == ADDR_EXPR)
14023 tree op00type;
14024 op00 = TREE_OPERAND (op00, 0);
14025 op00type = TREE_TYPE (op00);
14027 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14028 if (TREE_CODE (op00type) == VECTOR_TYPE
14029 && type == TREE_TYPE (op00type))
14031 tree part_width = TYPE_SIZE (type);
14032 unsigned HOST_WIDE_INT max_offset
14033 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14034 * TYPE_VECTOR_SUBPARTS (op00type));
14035 if (tree_int_cst_sign_bit (op01) == 0
14036 && compare_tree_int (op01, max_offset) == -1)
14038 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14039 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14040 tree index = bitsize_int (indexi);
14041 return fold_build3_loc (loc,
14042 BIT_FIELD_REF, type, op00,
14043 part_width, index);
14046 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14047 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14048 && type == TREE_TYPE (op00type))
14050 tree size = TYPE_SIZE_UNIT (type);
14051 if (tree_int_cst_equal (size, op01))
14052 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14054 /* ((foo *)&fooarray)[1] => fooarray[1] */
14055 else if (TREE_CODE (op00type) == ARRAY_TYPE
14056 && type == TREE_TYPE (op00type))
14058 tree type_domain = TYPE_DOMAIN (op00type);
14059 tree min_val = size_zero_node;
14060 if (type_domain && TYPE_MIN_VALUE (type_domain))
14061 min_val = TYPE_MIN_VALUE (type_domain);
14062 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14063 TYPE_SIZE_UNIT (type));
14064 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14065 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14066 NULL_TREE, NULL_TREE);
14071 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14072 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14073 && type == TREE_TYPE (TREE_TYPE (subtype))
14074 && (!in_gimple_form
14075 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14077 tree type_domain;
14078 tree min_val = size_zero_node;
14079 sub = build_fold_indirect_ref_loc (loc, sub);
14080 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14081 if (type_domain && TYPE_MIN_VALUE (type_domain))
14082 min_val = TYPE_MIN_VALUE (type_domain);
14083 if (in_gimple_form
14084 && TREE_CODE (min_val) != INTEGER_CST)
14085 return NULL_TREE;
14086 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14087 NULL_TREE);
14090 return NULL_TREE;
14093 /* Builds an expression for an indirection through T, simplifying some
14094 cases. */
14096 tree
14097 build_fold_indirect_ref_loc (location_t loc, tree t)
14099 tree type = TREE_TYPE (TREE_TYPE (t));
14100 tree sub = fold_indirect_ref_1 (loc, type, t);
14102 if (sub)
14103 return sub;
14105 return build1_loc (loc, INDIRECT_REF, type, t);
14108 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14110 tree
14111 fold_indirect_ref_loc (location_t loc, tree t)
14113 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14115 if (sub)
14116 return sub;
14117 else
14118 return t;
14121 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14122 whose result is ignored. The type of the returned tree need not be
14123 the same as the original expression. */
14125 tree
14126 fold_ignored_result (tree t)
14128 if (!TREE_SIDE_EFFECTS (t))
14129 return integer_zero_node;
14131 for (;;)
14132 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14134 case tcc_unary:
14135 t = TREE_OPERAND (t, 0);
14136 break;
14138 case tcc_binary:
14139 case tcc_comparison:
14140 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14141 t = TREE_OPERAND (t, 0);
14142 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14143 t = TREE_OPERAND (t, 1);
14144 else
14145 return t;
14146 break;
14148 case tcc_expression:
14149 switch (TREE_CODE (t))
14151 case COMPOUND_EXPR:
14152 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14153 return t;
14154 t = TREE_OPERAND (t, 0);
14155 break;
14157 case COND_EXPR:
14158 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14159 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14160 return t;
14161 t = TREE_OPERAND (t, 0);
14162 break;
14164 default:
14165 return t;
14167 break;
14169 default:
14170 return t;
14174 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14176 tree
14177 round_up_loc (location_t loc, tree value, unsigned int divisor)
14179 tree div = NULL_TREE;
14181 if (divisor == 1)
14182 return value;
14184 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14185 have to do anything. Only do this when we are not given a const,
14186 because in that case, this check is more expensive than just
14187 doing it. */
14188 if (TREE_CODE (value) != INTEGER_CST)
14190 div = build_int_cst (TREE_TYPE (value), divisor);
14192 if (multiple_of_p (TREE_TYPE (value), value, div))
14193 return value;
14196 /* If divisor is a power of two, simplify this to bit manipulation. */
14197 if (divisor == (divisor & -divisor))
14199 if (TREE_CODE (value) == INTEGER_CST)
14201 wide_int val = value;
14202 bool overflow_p;
14204 if ((val & (divisor - 1)) == 0)
14205 return value;
14207 overflow_p = TREE_OVERFLOW (value);
14208 val += divisor - 1;
14209 val &= - (int) divisor;
14210 if (val == 0)
14211 overflow_p = true;
14213 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14215 else
14217 tree t;
14219 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14220 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14221 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14222 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14225 else
14227 if (!div)
14228 div = build_int_cst (TREE_TYPE (value), divisor);
14229 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14230 value = size_binop_loc (loc, MULT_EXPR, value, div);
14233 return value;
14236 /* Likewise, but round down. */
14238 tree
14239 round_down_loc (location_t loc, tree value, int divisor)
14241 tree div = NULL_TREE;
14243 gcc_assert (divisor > 0);
14244 if (divisor == 1)
14245 return value;
14247 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14248 have to do anything. Only do this when we are not given a const,
14249 because in that case, this check is more expensive than just
14250 doing it. */
14251 if (TREE_CODE (value) != INTEGER_CST)
14253 div = build_int_cst (TREE_TYPE (value), divisor);
14255 if (multiple_of_p (TREE_TYPE (value), value, div))
14256 return value;
14259 /* If divisor is a power of two, simplify this to bit manipulation. */
14260 if (divisor == (divisor & -divisor))
14262 tree t;
14264 t = build_int_cst (TREE_TYPE (value), -divisor);
14265 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14267 else
14269 if (!div)
14270 div = build_int_cst (TREE_TYPE (value), divisor);
14271 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14272 value = size_binop_loc (loc, MULT_EXPR, value, div);
14275 return value;
14278 /* Returns the pointer to the base of the object addressed by EXP and
14279 extracts the information about the offset of the access, storing it
14280 to PBITPOS and POFFSET. */
14282 static tree
14283 split_address_to_core_and_offset (tree exp,
14284 HOST_WIDE_INT *pbitpos, tree *poffset)
14286 tree core;
14287 machine_mode mode;
14288 int unsignedp, reversep, volatilep;
14289 HOST_WIDE_INT bitsize;
14290 location_t loc = EXPR_LOCATION (exp);
14292 if (TREE_CODE (exp) == ADDR_EXPR)
14294 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14295 poffset, &mode, &unsignedp, &reversep,
14296 &volatilep);
14297 core = build_fold_addr_expr_loc (loc, core);
14299 else
14301 core = exp;
14302 *pbitpos = 0;
14303 *poffset = NULL_TREE;
14306 return core;
14309 /* Returns true if addresses of E1 and E2 differ by a constant, false
14310 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14312 bool
14313 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14315 tree core1, core2;
14316 HOST_WIDE_INT bitpos1, bitpos2;
14317 tree toffset1, toffset2, tdiff, type;
14319 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14320 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14322 if (bitpos1 % BITS_PER_UNIT != 0
14323 || bitpos2 % BITS_PER_UNIT != 0
14324 || !operand_equal_p (core1, core2, 0))
14325 return false;
14327 if (toffset1 && toffset2)
14329 type = TREE_TYPE (toffset1);
14330 if (type != TREE_TYPE (toffset2))
14331 toffset2 = fold_convert (type, toffset2);
14333 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14334 if (!cst_and_fits_in_hwi (tdiff))
14335 return false;
14337 *diff = int_cst_value (tdiff);
14339 else if (toffset1 || toffset2)
14341 /* If only one of the offsets is non-constant, the difference cannot
14342 be a constant. */
14343 return false;
14345 else
14346 *diff = 0;
14348 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14349 return true;
14352 /* Return OFF converted to a pointer offset type suitable as offset for
14353 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14354 tree
14355 convert_to_ptrofftype_loc (location_t loc, tree off)
14357 return fold_convert_loc (loc, sizetype, off);
14360 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14361 tree
14362 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14364 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14365 ptr, convert_to_ptrofftype_loc (loc, off));
14368 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14369 tree
14370 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14372 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14373 ptr, size_int (off));
14376 /* Return a char pointer for a C string if it is a string constant
14377 or sum of string constant and integer constant. */
14379 const char *
14380 c_getstr (tree src)
14382 tree offset_node;
14384 src = string_constant (src, &offset_node);
14385 if (src == 0)
14386 return 0;
14388 if (offset_node == 0)
14389 return TREE_STRING_POINTER (src);
14390 else if (!tree_fits_uhwi_p (offset_node)
14391 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
14392 return 0;
14394 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
14397 #if CHECKING_P
14399 namespace selftest {
14401 /* Helper functions for writing tests of folding trees. */
14403 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14405 static void
14406 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14407 tree constant)
14409 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14412 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14413 wrapping WRAPPED_EXPR. */
14415 static void
14416 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14417 tree wrapped_expr)
14419 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14420 ASSERT_NE (wrapped_expr, result);
14421 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14422 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14425 /* Verify that various arithmetic binary operations are folded
14426 correctly. */
14428 static void
14429 test_arithmetic_folding ()
14431 tree type = integer_type_node;
14432 tree x = create_tmp_var_raw (type, "x");
14433 tree zero = build_zero_cst (type);
14434 tree one = build_int_cst (type, 1);
14436 /* Addition. */
14437 /* 1 <-- (0 + 1) */
14438 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14439 one);
14440 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14441 one);
14443 /* (nonlvalue)x <-- (x + 0) */
14444 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14447 /* Subtraction. */
14448 /* 0 <-- (x - x) */
14449 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14450 zero);
14451 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14454 /* Multiplication. */
14455 /* 0 <-- (x * 0) */
14456 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14457 zero);
14459 /* (nonlvalue)x <-- (x * 1) */
14460 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14464 /* Run all of the selftests within this file. */
14466 void
14467 fold_const_c_tests ()
14469 test_arithmetic_folding ();
14472 } // namespace selftest
14474 #endif /* CHECKING_P */