/gcc
[official-gcc.git] / gcc / fold-const.c
blobe22b02f13120713ba98b81ac60c34e56d3482a99
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
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 associate_trees (location_t, tree, tree, enum tree_code, tree);
114 static enum comparison_code comparison_to_compcode (enum tree_code);
115 static enum tree_code compcode_to_comparison (enum comparison_code);
116 static int twoval_comparison_p (tree, tree *, tree *, int *);
117 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
118 static tree optimize_bit_field_compare (location_t, enum tree_code,
119 tree, tree, tree);
120 static int simple_operand_p (const_tree);
121 static bool simple_operand_p_2 (tree);
122 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
123 static tree range_predecessor (tree);
124 static tree range_successor (tree);
125 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
126 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
127 static tree unextend (tree, int, int, tree);
128 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
129 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
130 static tree fold_binary_op_with_conditional_arg (location_t,
131 enum tree_code, tree,
132 tree, tree,
133 tree, tree, int);
134 static tree fold_negate_const (tree, tree);
135 static tree fold_not_const (const_tree, tree);
136 static tree fold_relational_const (enum tree_code, tree, tree, tree);
137 static tree fold_convert_const (enum tree_code, tree, tree);
138 static tree fold_view_convert_expr (tree, tree);
139 static tree fold_negate_expr (location_t, tree);
142 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
143 Otherwise, return LOC. */
145 static location_t
146 expr_location_or (tree t, location_t loc)
148 location_t tloc = EXPR_LOCATION (t);
149 return tloc == UNKNOWN_LOCATION ? loc : tloc;
152 /* Similar to protected_set_expr_location, but never modify x in place,
153 if location can and needs to be set, unshare it. */
155 static inline tree
156 protected_set_expr_location_unshare (tree x, location_t loc)
158 if (CAN_HAVE_LOCATION_P (x)
159 && EXPR_LOCATION (x) != loc
160 && !(TREE_CODE (x) == SAVE_EXPR
161 || TREE_CODE (x) == TARGET_EXPR
162 || TREE_CODE (x) == BIND_EXPR))
164 x = copy_node (x);
165 SET_EXPR_LOCATION (x, loc);
167 return x;
170 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
171 division and returns the quotient. Otherwise returns
172 NULL_TREE. */
174 tree
175 div_if_zero_remainder (const_tree arg1, const_tree arg2)
177 widest_int quo;
179 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
180 SIGNED, &quo))
181 return wide_int_to_tree (TREE_TYPE (arg1), quo);
183 return NULL_TREE;
186 /* This is nonzero if we should defer warnings about undefined
187 overflow. This facility exists because these warnings are a
188 special case. The code to estimate loop iterations does not want
189 to issue any warnings, since it works with expressions which do not
190 occur in user code. Various bits of cleanup code call fold(), but
191 only use the result if it has certain characteristics (e.g., is a
192 constant); that code only wants to issue a warning if the result is
193 used. */
195 static int fold_deferring_overflow_warnings;
197 /* If a warning about undefined overflow is deferred, this is the
198 warning. Note that this may cause us to turn two warnings into
199 one, but that is fine since it is sufficient to only give one
200 warning per expression. */
202 static const char* fold_deferred_overflow_warning;
204 /* If a warning about undefined overflow is deferred, this is the
205 level at which the warning should be emitted. */
207 static enum warn_strict_overflow_code fold_deferred_overflow_code;
209 /* Start deferring overflow warnings. We could use a stack here to
210 permit nested calls, but at present it is not necessary. */
212 void
213 fold_defer_overflow_warnings (void)
215 ++fold_deferring_overflow_warnings;
218 /* Stop deferring overflow warnings. If there is a pending warning,
219 and ISSUE is true, then issue the warning if appropriate. STMT is
220 the statement with which the warning should be associated (used for
221 location information); STMT may be NULL. CODE is the level of the
222 warning--a warn_strict_overflow_code value. This function will use
223 the smaller of CODE and the deferred code when deciding whether to
224 issue the warning. CODE may be zero to mean to always use the
225 deferred code. */
227 void
228 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
230 const char *warnmsg;
231 location_t locus;
233 gcc_assert (fold_deferring_overflow_warnings > 0);
234 --fold_deferring_overflow_warnings;
235 if (fold_deferring_overflow_warnings > 0)
237 if (fold_deferred_overflow_warning != NULL
238 && code != 0
239 && code < (int) fold_deferred_overflow_code)
240 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
241 return;
244 warnmsg = fold_deferred_overflow_warning;
245 fold_deferred_overflow_warning = NULL;
247 if (!issue || warnmsg == NULL)
248 return;
250 if (gimple_no_warning_p (stmt))
251 return;
253 /* Use the smallest code level when deciding to issue the
254 warning. */
255 if (code == 0 || code > (int) fold_deferred_overflow_code)
256 code = fold_deferred_overflow_code;
258 if (!issue_strict_overflow_warning (code))
259 return;
261 if (stmt == NULL)
262 locus = input_location;
263 else
264 locus = gimple_location (stmt);
265 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
268 /* Stop deferring overflow warnings, ignoring any deferred
269 warnings. */
271 void
272 fold_undefer_and_ignore_overflow_warnings (void)
274 fold_undefer_overflow_warnings (false, NULL, 0);
277 /* Whether we are deferring overflow warnings. */
279 bool
280 fold_deferring_overflow_warnings_p (void)
282 return fold_deferring_overflow_warnings > 0;
285 /* This is called when we fold something based on the fact that signed
286 overflow is undefined. */
288 void
289 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
291 if (fold_deferring_overflow_warnings > 0)
293 if (fold_deferred_overflow_warning == NULL
294 || wc < fold_deferred_overflow_code)
296 fold_deferred_overflow_warning = gmsgid;
297 fold_deferred_overflow_code = wc;
300 else if (issue_strict_overflow_warning (wc))
301 warning (OPT_Wstrict_overflow, gmsgid);
304 /* Return true if the built-in mathematical function specified by CODE
305 is odd, i.e. -f(x) == f(-x). */
307 bool
308 negate_mathfn_p (combined_fn fn)
310 switch (fn)
312 CASE_CFN_ASIN:
313 CASE_CFN_ASINH:
314 CASE_CFN_ATAN:
315 CASE_CFN_ATANH:
316 CASE_CFN_CASIN:
317 CASE_CFN_CASINH:
318 CASE_CFN_CATAN:
319 CASE_CFN_CATANH:
320 CASE_CFN_CBRT:
321 CASE_CFN_CPROJ:
322 CASE_CFN_CSIN:
323 CASE_CFN_CSINH:
324 CASE_CFN_CTAN:
325 CASE_CFN_CTANH:
326 CASE_CFN_ERF:
327 CASE_CFN_LLROUND:
328 CASE_CFN_LROUND:
329 CASE_CFN_ROUND:
330 CASE_CFN_SIN:
331 CASE_CFN_SINH:
332 CASE_CFN_TAN:
333 CASE_CFN_TANH:
334 CASE_CFN_TRUNC:
335 return true;
337 CASE_CFN_LLRINT:
338 CASE_CFN_LRINT:
339 CASE_CFN_NEARBYINT:
340 CASE_CFN_RINT:
341 return !flag_rounding_math;
343 default:
344 break;
346 return false;
349 /* Check whether we may negate an integer constant T without causing
350 overflow. */
352 bool
353 may_negate_without_overflow_p (const_tree t)
355 tree type;
357 gcc_assert (TREE_CODE (t) == INTEGER_CST);
359 type = TREE_TYPE (t);
360 if (TYPE_UNSIGNED (type))
361 return false;
363 return !wi::only_sign_bit_p (wi::to_wide (t));
366 /* Determine whether an expression T can be cheaply negated using
367 the function negate_expr without introducing undefined overflow. */
369 static bool
370 negate_expr_p (tree t)
372 tree type;
374 if (t == 0)
375 return false;
377 type = TREE_TYPE (t);
379 STRIP_SIGN_NOPS (t);
380 switch (TREE_CODE (t))
382 case INTEGER_CST:
383 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
384 return true;
386 /* Check that -CST will not overflow type. */
387 return may_negate_without_overflow_p (t);
388 case BIT_NOT_EXPR:
389 return (INTEGRAL_TYPE_P (type)
390 && TYPE_OVERFLOW_WRAPS (type));
392 case FIXED_CST:
393 return true;
395 case NEGATE_EXPR:
396 return !TYPE_OVERFLOW_SANITIZED (type);
398 case REAL_CST:
399 /* We want to canonicalize to positive real constants. Pretend
400 that only negative ones can be easily negated. */
401 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
403 case COMPLEX_CST:
404 return negate_expr_p (TREE_REALPART (t))
405 && negate_expr_p (TREE_IMAGPART (t));
407 case VECTOR_CST:
409 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
410 return true;
412 int count = VECTOR_CST_NELTS (t), i;
414 for (i = 0; i < count; i++)
415 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
416 return false;
418 return true;
421 case COMPLEX_EXPR:
422 return negate_expr_p (TREE_OPERAND (t, 0))
423 && negate_expr_p (TREE_OPERAND (t, 1));
425 case CONJ_EXPR:
426 return negate_expr_p (TREE_OPERAND (t, 0));
428 case PLUS_EXPR:
429 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
430 || HONOR_SIGNED_ZEROS (element_mode (type))
431 || (INTEGRAL_TYPE_P (type)
432 && ! TYPE_OVERFLOW_WRAPS (type)))
433 return false;
434 /* -(A + B) -> (-B) - A. */
435 if (negate_expr_p (TREE_OPERAND (t, 1)))
436 return true;
437 /* -(A + B) -> (-A) - B. */
438 return negate_expr_p (TREE_OPERAND (t, 0));
440 case MINUS_EXPR:
441 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
443 && !HONOR_SIGNED_ZEROS (element_mode (type))
444 && (! INTEGRAL_TYPE_P (type)
445 || TYPE_OVERFLOW_WRAPS (type));
447 case MULT_EXPR:
448 if (TYPE_UNSIGNED (type))
449 break;
450 /* INT_MIN/n * n doesn't overflow while negating one operand it does
451 if n is a (negative) power of two. */
452 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
453 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
454 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
455 && (wi::popcount
456 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
457 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
458 && (wi::popcount
459 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
460 break;
462 /* Fall through. */
464 case RDIV_EXPR:
465 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
466 return negate_expr_p (TREE_OPERAND (t, 1))
467 || negate_expr_p (TREE_OPERAND (t, 0));
468 break;
470 case TRUNC_DIV_EXPR:
471 case ROUND_DIV_EXPR:
472 case EXACT_DIV_EXPR:
473 if (TYPE_UNSIGNED (type))
474 break;
475 if (negate_expr_p (TREE_OPERAND (t, 0)))
476 return true;
477 /* In general we can't negate B in A / B, because if A is INT_MIN and
478 B is 1, we may turn this into INT_MIN / -1 which is undefined
479 and actually traps on some architectures. */
480 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
481 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
482 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
483 && ! integer_onep (TREE_OPERAND (t, 1))))
484 return negate_expr_p (TREE_OPERAND (t, 1));
485 break;
487 case NOP_EXPR:
488 /* Negate -((double)float) as (double)(-float). */
489 if (TREE_CODE (type) == REAL_TYPE)
491 tree tem = strip_float_extensions (t);
492 if (tem != t)
493 return negate_expr_p (tem);
495 break;
497 case CALL_EXPR:
498 /* Negate -f(x) as f(-x). */
499 if (negate_mathfn_p (get_call_combined_fn (t)))
500 return negate_expr_p (CALL_EXPR_ARG (t, 0));
501 break;
503 case RSHIFT_EXPR:
504 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
505 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
507 tree op1 = TREE_OPERAND (t, 1);
508 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
509 return true;
511 break;
513 default:
514 break;
516 return false;
519 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
520 simplification is possible.
521 If negate_expr_p would return true for T, NULL_TREE will never be
522 returned. */
524 static tree
525 fold_negate_expr_1 (location_t loc, tree t)
527 tree type = TREE_TYPE (t);
528 tree tem;
530 switch (TREE_CODE (t))
532 /* Convert - (~A) to A + 1. */
533 case BIT_NOT_EXPR:
534 if (INTEGRAL_TYPE_P (type))
535 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
536 build_one_cst (type));
537 break;
539 case INTEGER_CST:
540 tem = fold_negate_const (t, type);
541 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
542 || (ANY_INTEGRAL_TYPE_P (type)
543 && !TYPE_OVERFLOW_TRAPS (type)
544 && TYPE_OVERFLOW_WRAPS (type))
545 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
546 return tem;
547 break;
549 case REAL_CST:
550 tem = fold_negate_const (t, type);
551 return tem;
553 case FIXED_CST:
554 tem = fold_negate_const (t, type);
555 return tem;
557 case COMPLEX_CST:
559 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
560 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
561 if (rpart && ipart)
562 return build_complex (type, rpart, ipart);
564 break;
566 case VECTOR_CST:
568 int count = VECTOR_CST_NELTS (t), i;
570 auto_vec<tree, 32> elts (count);
571 for (i = 0; i < count; i++)
573 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
574 if (elt == NULL_TREE)
575 return NULL_TREE;
576 elts.quick_push (elt);
579 return build_vector (type, elts);
582 case COMPLEX_EXPR:
583 if (negate_expr_p (t))
584 return fold_build2_loc (loc, COMPLEX_EXPR, type,
585 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
586 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
587 break;
589 case CONJ_EXPR:
590 if (negate_expr_p (t))
591 return fold_build1_loc (loc, CONJ_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
593 break;
595 case NEGATE_EXPR:
596 if (!TYPE_OVERFLOW_SANITIZED (type))
597 return TREE_OPERAND (t, 0);
598 break;
600 case PLUS_EXPR:
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
602 && !HONOR_SIGNED_ZEROS (element_mode (type)))
604 /* -(A + B) -> (-B) - A. */
605 if (negate_expr_p (TREE_OPERAND (t, 1)))
607 tem = negate_expr (TREE_OPERAND (t, 1));
608 return fold_build2_loc (loc, MINUS_EXPR, type,
609 tem, TREE_OPERAND (t, 0));
612 /* -(A + B) -> (-A) - B. */
613 if (negate_expr_p (TREE_OPERAND (t, 0)))
615 tem = negate_expr (TREE_OPERAND (t, 0));
616 return fold_build2_loc (loc, MINUS_EXPR, type,
617 tem, TREE_OPERAND (t, 1));
620 break;
622 case MINUS_EXPR:
623 /* - (A - B) -> B - A */
624 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
625 && !HONOR_SIGNED_ZEROS (element_mode (type)))
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
628 break;
630 case MULT_EXPR:
631 if (TYPE_UNSIGNED (type))
632 break;
634 /* Fall through. */
636 case RDIV_EXPR:
637 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
639 tem = TREE_OPERAND (t, 1);
640 if (negate_expr_p (tem))
641 return fold_build2_loc (loc, TREE_CODE (t), type,
642 TREE_OPERAND (t, 0), negate_expr (tem));
643 tem = TREE_OPERAND (t, 0);
644 if (negate_expr_p (tem))
645 return fold_build2_loc (loc, TREE_CODE (t), type,
646 negate_expr (tem), TREE_OPERAND (t, 1));
648 break;
650 case TRUNC_DIV_EXPR:
651 case ROUND_DIV_EXPR:
652 case EXACT_DIV_EXPR:
653 if (TYPE_UNSIGNED (type))
654 break;
655 if (negate_expr_p (TREE_OPERAND (t, 0)))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (TREE_OPERAND (t, 0)),
658 TREE_OPERAND (t, 1));
659 /* In general we can't negate B in A / B, because if A is INT_MIN and
660 B is 1, we may turn this into INT_MIN / -1 which is undefined
661 and actually traps on some architectures. */
662 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
663 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
664 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
665 && ! integer_onep (TREE_OPERAND (t, 1))))
666 && negate_expr_p (TREE_OPERAND (t, 1)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 TREE_OPERAND (t, 0),
669 negate_expr (TREE_OPERAND (t, 1)));
670 break;
672 case NOP_EXPR:
673 /* Convert -((double)float) into (double)(-float). */
674 if (TREE_CODE (type) == REAL_TYPE)
676 tem = strip_float_extensions (t);
677 if (tem != t && negate_expr_p (tem))
678 return fold_convert_loc (loc, type, negate_expr (tem));
680 break;
682 case CALL_EXPR:
683 /* Negate -f(x) as f(-x). */
684 if (negate_mathfn_p (get_call_combined_fn (t))
685 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
687 tree fndecl, arg;
689 fndecl = get_callee_fndecl (t);
690 arg = negate_expr (CALL_EXPR_ARG (t, 0));
691 return build_call_expr_loc (loc, fndecl, 1, arg);
693 break;
695 case RSHIFT_EXPR:
696 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
697 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
699 tree op1 = TREE_OPERAND (t, 1);
700 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
702 tree ntype = TYPE_UNSIGNED (type)
703 ? signed_type_for (type)
704 : unsigned_type_for (type);
705 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
706 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
707 return fold_convert_loc (loc, type, temp);
710 break;
712 default:
713 break;
716 return NULL_TREE;
719 /* A wrapper for fold_negate_expr_1. */
721 static tree
722 fold_negate_expr (location_t loc, tree t)
724 tree type = TREE_TYPE (t);
725 STRIP_SIGN_NOPS (t);
726 tree tem = fold_negate_expr_1 (loc, t);
727 if (tem == NULL_TREE)
728 return NULL_TREE;
729 return fold_convert_loc (loc, type, tem);
732 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
733 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
734 return NULL_TREE. */
736 static tree
737 negate_expr (tree t)
739 tree type, tem;
740 location_t loc;
742 if (t == NULL_TREE)
743 return NULL_TREE;
745 loc = EXPR_LOCATION (t);
746 type = TREE_TYPE (t);
747 STRIP_SIGN_NOPS (t);
749 tem = fold_negate_expr (loc, t);
750 if (!tem)
751 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
752 return fold_convert_loc (loc, type, tem);
755 /* Split a tree IN into a constant, literal and variable parts that could be
756 combined with CODE to make IN. "constant" means an expression with
757 TREE_CONSTANT but that isn't an actual constant. CODE must be a
758 commutative arithmetic operation. Store the constant part into *CONP,
759 the literal in *LITP and return the variable part. If a part isn't
760 present, set it to null. If the tree does not decompose in this way,
761 return the entire tree as the variable part and the other parts as null.
763 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
764 case, we negate an operand that was subtracted. Except if it is a
765 literal for which we use *MINUS_LITP instead.
767 If NEGATE_P is true, we are negating all of IN, again except a literal
768 for which we use *MINUS_LITP instead. If a variable part is of pointer
769 type, it is negated after converting to TYPE. This prevents us from
770 generating illegal MINUS pointer expression. LOC is the location of
771 the converted variable part.
773 If IN is itself a literal or constant, return it as appropriate.
775 Note that we do not guarantee that any of the three values will be the
776 same type as IN, but they will have the same signedness and mode. */
778 static tree
779 split_tree (tree in, tree type, enum tree_code code,
780 tree *minus_varp, tree *conp, tree *minus_conp,
781 tree *litp, tree *minus_litp, int negate_p)
783 tree var = 0;
784 *minus_varp = 0;
785 *conp = 0;
786 *minus_conp = 0;
787 *litp = 0;
788 *minus_litp = 0;
790 /* Strip any conversions that don't change the machine mode or signedness. */
791 STRIP_SIGN_NOPS (in);
793 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
794 || TREE_CODE (in) == FIXED_CST)
795 *litp = in;
796 else if (TREE_CODE (in) == code
797 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
798 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
799 /* We can associate addition and subtraction together (even
800 though the C standard doesn't say so) for integers because
801 the value is not affected. For reals, the value might be
802 affected, so we can't. */
803 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
804 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805 || (code == MINUS_EXPR
806 && (TREE_CODE (in) == PLUS_EXPR
807 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
809 tree op0 = TREE_OPERAND (in, 0);
810 tree op1 = TREE_OPERAND (in, 1);
811 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
812 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
814 /* First see if either of the operands is a literal, then a constant. */
815 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
816 || TREE_CODE (op0) == FIXED_CST)
817 *litp = op0, op0 = 0;
818 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
819 || TREE_CODE (op1) == FIXED_CST)
820 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
822 if (op0 != 0 && TREE_CONSTANT (op0))
823 *conp = op0, op0 = 0;
824 else if (op1 != 0 && TREE_CONSTANT (op1))
825 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
827 /* If we haven't dealt with either operand, this is not a case we can
828 decompose. Otherwise, VAR is either of the ones remaining, if any. */
829 if (op0 != 0 && op1 != 0)
830 var = in;
831 else if (op0 != 0)
832 var = op0;
833 else
834 var = op1, neg_var_p = neg1_p;
836 /* Now do any needed negations. */
837 if (neg_litp_p)
838 *minus_litp = *litp, *litp = 0;
839 if (neg_conp_p && *conp)
840 *minus_conp = *conp, *conp = 0;
841 if (neg_var_p && var)
842 *minus_varp = var, var = 0;
844 else if (TREE_CONSTANT (in))
845 *conp = in;
846 else if (TREE_CODE (in) == BIT_NOT_EXPR
847 && code == PLUS_EXPR)
849 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
850 when IN is constant. */
851 *litp = build_minus_one_cst (type);
852 *minus_varp = TREE_OPERAND (in, 0);
854 else
855 var = in;
857 if (negate_p)
859 if (*litp)
860 *minus_litp = *litp, *litp = 0;
861 else if (*minus_litp)
862 *litp = *minus_litp, *minus_litp = 0;
863 if (*conp)
864 *minus_conp = *conp, *conp = 0;
865 else if (*minus_conp)
866 *conp = *minus_conp, *minus_conp = 0;
867 if (var)
868 *minus_varp = var, var = 0;
869 else if (*minus_varp)
870 var = *minus_varp, *minus_varp = 0;
873 if (*litp
874 && TREE_OVERFLOW_P (*litp))
875 *litp = drop_tree_overflow (*litp);
876 if (*minus_litp
877 && TREE_OVERFLOW_P (*minus_litp))
878 *minus_litp = drop_tree_overflow (*minus_litp);
880 return var;
883 /* Re-associate trees split by the above function. T1 and T2 are
884 either expressions to associate or null. Return the new
885 expression, if any. LOC is the location of the new expression. If
886 we build an operation, do it in TYPE and with CODE. */
888 static tree
889 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
891 if (t1 == 0)
893 gcc_assert (t2 == 0 || code != MINUS_EXPR);
894 return t2;
896 else if (t2 == 0)
897 return t1;
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
903 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
904 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
906 if (code == PLUS_EXPR)
908 if (TREE_CODE (t1) == NEGATE_EXPR)
909 return build2_loc (loc, MINUS_EXPR, type,
910 fold_convert_loc (loc, type, t2),
911 fold_convert_loc (loc, type,
912 TREE_OPERAND (t1, 0)));
913 else if (TREE_CODE (t2) == NEGATE_EXPR)
914 return build2_loc (loc, MINUS_EXPR, type,
915 fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type,
917 TREE_OPERAND (t2, 0)));
918 else if (integer_zerop (t2))
919 return fold_convert_loc (loc, type, t1);
921 else if (code == MINUS_EXPR)
923 if (integer_zerop (t2))
924 return fold_convert_loc (loc, type, t1);
927 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
928 fold_convert_loc (loc, type, t2));
931 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 fold_convert_loc (loc, type, t2));
935 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
936 for use in int_const_binop, size_binop and size_diffop. */
938 static bool
939 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
941 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
942 return false;
943 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
944 return false;
946 switch (code)
948 case LSHIFT_EXPR:
949 case RSHIFT_EXPR:
950 case LROTATE_EXPR:
951 case RROTATE_EXPR:
952 return true;
954 default:
955 break;
958 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
959 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
960 && TYPE_MODE (type1) == TYPE_MODE (type2);
964 /* Combine two integer constants PARG1 and PARG2 under operation CODE
965 to produce a new constant. Return NULL_TREE if we don't know how
966 to evaluate CODE at compile-time. */
968 static tree
969 int_const_binop_1 (enum tree_code code, const_tree parg1, const_tree parg2,
970 int overflowable)
972 wide_int res;
973 tree t;
974 tree type = TREE_TYPE (parg1);
975 signop sign = TYPE_SIGN (type);
976 bool overflow = false;
978 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
979 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
981 switch (code)
983 case BIT_IOR_EXPR:
984 res = wi::bit_or (arg1, arg2);
985 break;
987 case BIT_XOR_EXPR:
988 res = wi::bit_xor (arg1, arg2);
989 break;
991 case BIT_AND_EXPR:
992 res = wi::bit_and (arg1, arg2);
993 break;
995 case RSHIFT_EXPR:
996 case LSHIFT_EXPR:
997 if (wi::neg_p (arg2))
999 arg2 = -arg2;
1000 if (code == RSHIFT_EXPR)
1001 code = LSHIFT_EXPR;
1002 else
1003 code = RSHIFT_EXPR;
1006 if (code == RSHIFT_EXPR)
1007 /* It's unclear from the C standard whether shifts can overflow.
1008 The following code ignores overflow; perhaps a C standard
1009 interpretation ruling is needed. */
1010 res = wi::rshift (arg1, arg2, sign);
1011 else
1012 res = wi::lshift (arg1, arg2);
1013 break;
1015 case RROTATE_EXPR:
1016 case LROTATE_EXPR:
1017 if (wi::neg_p (arg2))
1019 arg2 = -arg2;
1020 if (code == RROTATE_EXPR)
1021 code = LROTATE_EXPR;
1022 else
1023 code = RROTATE_EXPR;
1026 if (code == RROTATE_EXPR)
1027 res = wi::rrotate (arg1, arg2);
1028 else
1029 res = wi::lrotate (arg1, arg2);
1030 break;
1032 case PLUS_EXPR:
1033 res = wi::add (arg1, arg2, sign, &overflow);
1034 break;
1036 case MINUS_EXPR:
1037 res = wi::sub (arg1, arg2, sign, &overflow);
1038 break;
1040 case MULT_EXPR:
1041 res = wi::mul (arg1, arg2, sign, &overflow);
1042 break;
1044 case MULT_HIGHPART_EXPR:
1045 res = wi::mul_high (arg1, arg2, sign);
1046 break;
1048 case TRUNC_DIV_EXPR:
1049 case EXACT_DIV_EXPR:
1050 if (arg2 == 0)
1051 return NULL_TREE;
1052 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1053 break;
1055 case FLOOR_DIV_EXPR:
1056 if (arg2 == 0)
1057 return NULL_TREE;
1058 res = wi::div_floor (arg1, arg2, sign, &overflow);
1059 break;
1061 case CEIL_DIV_EXPR:
1062 if (arg2 == 0)
1063 return NULL_TREE;
1064 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1065 break;
1067 case ROUND_DIV_EXPR:
1068 if (arg2 == 0)
1069 return NULL_TREE;
1070 res = wi::div_round (arg1, arg2, sign, &overflow);
1071 break;
1073 case TRUNC_MOD_EXPR:
1074 if (arg2 == 0)
1075 return NULL_TREE;
1076 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1077 break;
1079 case FLOOR_MOD_EXPR:
1080 if (arg2 == 0)
1081 return NULL_TREE;
1082 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1083 break;
1085 case CEIL_MOD_EXPR:
1086 if (arg2 == 0)
1087 return NULL_TREE;
1088 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1089 break;
1091 case ROUND_MOD_EXPR:
1092 if (arg2 == 0)
1093 return NULL_TREE;
1094 res = wi::mod_round (arg1, arg2, sign, &overflow);
1095 break;
1097 case MIN_EXPR:
1098 res = wi::min (arg1, arg2, sign);
1099 break;
1101 case MAX_EXPR:
1102 res = wi::max (arg1, arg2, sign);
1103 break;
1105 default:
1106 return NULL_TREE;
1109 t = force_fit_type (type, res, overflowable,
1110 (((sign == SIGNED || overflowable == -1)
1111 && overflow)
1112 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1114 return t;
1117 tree
1118 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1120 return int_const_binop_1 (code, arg1, arg2, 1);
1123 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1124 constant. We assume ARG1 and ARG2 have the same data type, or at least
1125 are the same kind of constant and the same machine mode. Return zero if
1126 combining the constants is not allowed in the current operating mode. */
1128 static tree
1129 const_binop (enum tree_code code, tree arg1, tree arg2)
1131 /* Sanity check for the recursive cases. */
1132 if (!arg1 || !arg2)
1133 return NULL_TREE;
1135 STRIP_NOPS (arg1);
1136 STRIP_NOPS (arg2);
1138 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1140 if (code == POINTER_PLUS_EXPR)
1141 return int_const_binop (PLUS_EXPR,
1142 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1144 return int_const_binop (code, arg1, arg2);
1147 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1149 machine_mode mode;
1150 REAL_VALUE_TYPE d1;
1151 REAL_VALUE_TYPE d2;
1152 REAL_VALUE_TYPE value;
1153 REAL_VALUE_TYPE result;
1154 bool inexact;
1155 tree t, type;
1157 /* The following codes are handled by real_arithmetic. */
1158 switch (code)
1160 case PLUS_EXPR:
1161 case MINUS_EXPR:
1162 case MULT_EXPR:
1163 case RDIV_EXPR:
1164 case MIN_EXPR:
1165 case MAX_EXPR:
1166 break;
1168 default:
1169 return NULL_TREE;
1172 d1 = TREE_REAL_CST (arg1);
1173 d2 = TREE_REAL_CST (arg2);
1175 type = TREE_TYPE (arg1);
1176 mode = TYPE_MODE (type);
1178 /* Don't perform operation if we honor signaling NaNs and
1179 either operand is a signaling NaN. */
1180 if (HONOR_SNANS (mode)
1181 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1182 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1183 return NULL_TREE;
1185 /* Don't perform operation if it would raise a division
1186 by zero exception. */
1187 if (code == RDIV_EXPR
1188 && real_equal (&d2, &dconst0)
1189 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1190 return NULL_TREE;
1192 /* If either operand is a NaN, just return it. Otherwise, set up
1193 for floating-point trap; we return an overflow. */
1194 if (REAL_VALUE_ISNAN (d1))
1196 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1197 is off. */
1198 d1.signalling = 0;
1199 t = build_real (type, d1);
1200 return t;
1202 else if (REAL_VALUE_ISNAN (d2))
1204 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1205 is off. */
1206 d2.signalling = 0;
1207 t = build_real (type, d2);
1208 return t;
1211 inexact = real_arithmetic (&value, code, &d1, &d2);
1212 real_convert (&result, mode, &value);
1214 /* Don't constant fold this floating point operation if
1215 the result has overflowed and flag_trapping_math. */
1216 if (flag_trapping_math
1217 && MODE_HAS_INFINITIES (mode)
1218 && REAL_VALUE_ISINF (result)
1219 && !REAL_VALUE_ISINF (d1)
1220 && !REAL_VALUE_ISINF (d2))
1221 return NULL_TREE;
1223 /* Don't constant fold this floating point operation if the
1224 result may dependent upon the run-time rounding mode and
1225 flag_rounding_math is set, or if GCC's software emulation
1226 is unable to accurately represent the result. */
1227 if ((flag_rounding_math
1228 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1229 && (inexact || !real_identical (&result, &value)))
1230 return NULL_TREE;
1232 t = build_real (type, result);
1234 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1235 return t;
1238 if (TREE_CODE (arg1) == FIXED_CST)
1240 FIXED_VALUE_TYPE f1;
1241 FIXED_VALUE_TYPE f2;
1242 FIXED_VALUE_TYPE result;
1243 tree t, type;
1244 int sat_p;
1245 bool overflow_p;
1247 /* The following codes are handled by fixed_arithmetic. */
1248 switch (code)
1250 case PLUS_EXPR:
1251 case MINUS_EXPR:
1252 case MULT_EXPR:
1253 case TRUNC_DIV_EXPR:
1254 if (TREE_CODE (arg2) != FIXED_CST)
1255 return NULL_TREE;
1256 f2 = TREE_FIXED_CST (arg2);
1257 break;
1259 case LSHIFT_EXPR:
1260 case RSHIFT_EXPR:
1262 if (TREE_CODE (arg2) != INTEGER_CST)
1263 return NULL_TREE;
1264 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1265 f2.data.high = w2.elt (1);
1266 f2.data.low = w2.ulow ();
1267 f2.mode = SImode;
1269 break;
1271 default:
1272 return NULL_TREE;
1275 f1 = TREE_FIXED_CST (arg1);
1276 type = TREE_TYPE (arg1);
1277 sat_p = TYPE_SATURATING (type);
1278 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1279 t = build_fixed (type, result);
1280 /* Propagate overflow flags. */
1281 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1282 TREE_OVERFLOW (t) = 1;
1283 return t;
1286 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1288 tree type = TREE_TYPE (arg1);
1289 tree r1 = TREE_REALPART (arg1);
1290 tree i1 = TREE_IMAGPART (arg1);
1291 tree r2 = TREE_REALPART (arg2);
1292 tree i2 = TREE_IMAGPART (arg2);
1293 tree real, imag;
1295 switch (code)
1297 case PLUS_EXPR:
1298 case MINUS_EXPR:
1299 real = const_binop (code, r1, r2);
1300 imag = const_binop (code, i1, i2);
1301 break;
1303 case MULT_EXPR:
1304 if (COMPLEX_FLOAT_TYPE_P (type))
1305 return do_mpc_arg2 (arg1, arg2, type,
1306 /* do_nonfinite= */ folding_initializer,
1307 mpc_mul);
1309 real = const_binop (MINUS_EXPR,
1310 const_binop (MULT_EXPR, r1, r2),
1311 const_binop (MULT_EXPR, i1, i2));
1312 imag = const_binop (PLUS_EXPR,
1313 const_binop (MULT_EXPR, r1, i2),
1314 const_binop (MULT_EXPR, i1, r2));
1315 break;
1317 case RDIV_EXPR:
1318 if (COMPLEX_FLOAT_TYPE_P (type))
1319 return do_mpc_arg2 (arg1, arg2, type,
1320 /* do_nonfinite= */ folding_initializer,
1321 mpc_div);
1322 /* Fallthru. */
1323 case TRUNC_DIV_EXPR:
1324 case CEIL_DIV_EXPR:
1325 case FLOOR_DIV_EXPR:
1326 case ROUND_DIV_EXPR:
1327 if (flag_complex_method == 0)
1329 /* Keep this algorithm in sync with
1330 tree-complex.c:expand_complex_div_straight().
1332 Expand complex division to scalars, straightforward algorithm.
1333 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1334 t = br*br + bi*bi
1336 tree magsquared
1337 = const_binop (PLUS_EXPR,
1338 const_binop (MULT_EXPR, r2, r2),
1339 const_binop (MULT_EXPR, i2, i2));
1340 tree t1
1341 = const_binop (PLUS_EXPR,
1342 const_binop (MULT_EXPR, r1, r2),
1343 const_binop (MULT_EXPR, i1, i2));
1344 tree t2
1345 = const_binop (MINUS_EXPR,
1346 const_binop (MULT_EXPR, i1, r2),
1347 const_binop (MULT_EXPR, r1, i2));
1349 real = const_binop (code, t1, magsquared);
1350 imag = const_binop (code, t2, magsquared);
1352 else
1354 /* Keep this algorithm in sync with
1355 tree-complex.c:expand_complex_div_wide().
1357 Expand complex division to scalars, modified algorithm to minimize
1358 overflow with wide input ranges. */
1359 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1360 fold_abs_const (r2, TREE_TYPE (type)),
1361 fold_abs_const (i2, TREE_TYPE (type)));
1363 if (integer_nonzerop (compare))
1365 /* In the TRUE branch, we compute
1366 ratio = br/bi;
1367 div = (br * ratio) + bi;
1368 tr = (ar * ratio) + ai;
1369 ti = (ai * ratio) - ar;
1370 tr = tr / div;
1371 ti = ti / div; */
1372 tree ratio = const_binop (code, r2, i2);
1373 tree div = const_binop (PLUS_EXPR, i2,
1374 const_binop (MULT_EXPR, r2, ratio));
1375 real = const_binop (MULT_EXPR, r1, ratio);
1376 real = const_binop (PLUS_EXPR, real, i1);
1377 real = const_binop (code, real, div);
1379 imag = const_binop (MULT_EXPR, i1, ratio);
1380 imag = const_binop (MINUS_EXPR, imag, r1);
1381 imag = const_binop (code, imag, div);
1383 else
1385 /* In the FALSE branch, we compute
1386 ratio = d/c;
1387 divisor = (d * ratio) + c;
1388 tr = (b * ratio) + a;
1389 ti = b - (a * ratio);
1390 tr = tr / div;
1391 ti = ti / div; */
1392 tree ratio = const_binop (code, i2, r2);
1393 tree div = const_binop (PLUS_EXPR, r2,
1394 const_binop (MULT_EXPR, i2, ratio));
1396 real = const_binop (MULT_EXPR, i1, ratio);
1397 real = const_binop (PLUS_EXPR, real, r1);
1398 real = const_binop (code, real, div);
1400 imag = const_binop (MULT_EXPR, r1, ratio);
1401 imag = const_binop (MINUS_EXPR, i1, imag);
1402 imag = const_binop (code, imag, div);
1405 break;
1407 default:
1408 return NULL_TREE;
1411 if (real && imag)
1412 return build_complex (type, real, imag);
1415 if (TREE_CODE (arg1) == VECTOR_CST
1416 && TREE_CODE (arg2) == VECTOR_CST)
1418 tree type = TREE_TYPE (arg1);
1419 int count = VECTOR_CST_NELTS (arg1), i;
1421 auto_vec<tree, 32> elts (count);
1422 for (i = 0; i < count; i++)
1424 tree elem1 = VECTOR_CST_ELT (arg1, i);
1425 tree elem2 = VECTOR_CST_ELT (arg2, i);
1427 tree elt = const_binop (code, elem1, elem2);
1429 /* It is possible that const_binop cannot handle the given
1430 code and return NULL_TREE */
1431 if (elt == NULL_TREE)
1432 return NULL_TREE;
1433 elts.quick_push (elt);
1436 return build_vector (type, elts);
1439 /* Shifts allow a scalar offset for a vector. */
1440 if (TREE_CODE (arg1) == VECTOR_CST
1441 && TREE_CODE (arg2) == INTEGER_CST)
1443 tree type = TREE_TYPE (arg1);
1444 int count = VECTOR_CST_NELTS (arg1), i;
1446 auto_vec<tree, 32> elts (count);
1447 for (i = 0; i < count; i++)
1449 tree elem1 = VECTOR_CST_ELT (arg1, i);
1451 tree elt = const_binop (code, elem1, arg2);
1453 /* It is possible that const_binop cannot handle the given
1454 code and return NULL_TREE. */
1455 if (elt == NULL_TREE)
1456 return NULL_TREE;
1457 elts.quick_push (elt);
1460 return build_vector (type, elts);
1462 return NULL_TREE;
1465 /* Overload that adds a TYPE parameter to be able to dispatch
1466 to fold_relational_const. */
1468 tree
1469 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1471 if (TREE_CODE_CLASS (code) == tcc_comparison)
1472 return fold_relational_const (code, type, arg1, arg2);
1474 /* ??? Until we make the const_binop worker take the type of the
1475 result as argument put those cases that need it here. */
1476 switch (code)
1478 case COMPLEX_EXPR:
1479 if ((TREE_CODE (arg1) == REAL_CST
1480 && TREE_CODE (arg2) == REAL_CST)
1481 || (TREE_CODE (arg1) == INTEGER_CST
1482 && TREE_CODE (arg2) == INTEGER_CST))
1483 return build_complex (type, arg1, arg2);
1484 return NULL_TREE;
1486 case VEC_PACK_TRUNC_EXPR:
1487 case VEC_PACK_FIX_TRUNC_EXPR:
1489 unsigned int out_nelts, in_nelts, i;
1491 if (TREE_CODE (arg1) != VECTOR_CST
1492 || TREE_CODE (arg2) != VECTOR_CST)
1493 return NULL_TREE;
1495 in_nelts = VECTOR_CST_NELTS (arg1);
1496 out_nelts = in_nelts * 2;
1497 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1498 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1500 auto_vec<tree, 32> elts (out_nelts);
1501 for (i = 0; i < out_nelts; i++)
1503 tree elt = (i < in_nelts
1504 ? VECTOR_CST_ELT (arg1, i)
1505 : VECTOR_CST_ELT (arg2, i - in_nelts));
1506 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1507 ? NOP_EXPR : FIX_TRUNC_EXPR,
1508 TREE_TYPE (type), elt);
1509 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1510 return NULL_TREE;
1511 elts.quick_push (elt);
1514 return build_vector (type, elts);
1517 case VEC_WIDEN_MULT_LO_EXPR:
1518 case VEC_WIDEN_MULT_HI_EXPR:
1519 case VEC_WIDEN_MULT_EVEN_EXPR:
1520 case VEC_WIDEN_MULT_ODD_EXPR:
1522 unsigned int out_nelts, in_nelts, out, ofs, scale;
1524 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1525 return NULL_TREE;
1527 in_nelts = VECTOR_CST_NELTS (arg1);
1528 out_nelts = in_nelts / 2;
1529 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1530 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1532 if (code == VEC_WIDEN_MULT_LO_EXPR)
1533 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1534 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1535 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1536 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1537 scale = 1, ofs = 0;
1538 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1539 scale = 1, ofs = 1;
1541 auto_vec<tree, 32> elts (out_nelts);
1542 for (out = 0; out < out_nelts; out++)
1544 unsigned int in = (out << scale) + ofs;
1545 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1546 VECTOR_CST_ELT (arg1, in));
1547 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1548 VECTOR_CST_ELT (arg2, in));
1550 if (t1 == NULL_TREE || t2 == NULL_TREE)
1551 return NULL_TREE;
1552 tree elt = const_binop (MULT_EXPR, t1, t2);
1553 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1554 return NULL_TREE;
1555 elts.quick_push (elt);
1558 return build_vector (type, elts);
1561 default:;
1564 if (TREE_CODE_CLASS (code) != tcc_binary)
1565 return NULL_TREE;
1567 /* Make sure type and arg0 have the same saturating flag. */
1568 gcc_checking_assert (TYPE_SATURATING (type)
1569 == TYPE_SATURATING (TREE_TYPE (arg1)));
1571 return const_binop (code, arg1, arg2);
1574 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1575 Return zero if computing the constants is not possible. */
1577 tree
1578 const_unop (enum tree_code code, tree type, tree arg0)
1580 /* Don't perform the operation, other than NEGATE and ABS, if
1581 flag_signaling_nans is on and the operand is a signaling NaN. */
1582 if (TREE_CODE (arg0) == REAL_CST
1583 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1584 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1585 && code != NEGATE_EXPR
1586 && code != ABS_EXPR)
1587 return NULL_TREE;
1589 switch (code)
1591 CASE_CONVERT:
1592 case FLOAT_EXPR:
1593 case FIX_TRUNC_EXPR:
1594 case FIXED_CONVERT_EXPR:
1595 return fold_convert_const (code, type, arg0);
1597 case ADDR_SPACE_CONVERT_EXPR:
1598 /* If the source address is 0, and the source address space
1599 cannot have a valid object at 0, fold to dest type null. */
1600 if (integer_zerop (arg0)
1601 && !(targetm.addr_space.zero_address_valid
1602 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1603 return fold_convert_const (code, type, arg0);
1604 break;
1606 case VIEW_CONVERT_EXPR:
1607 return fold_view_convert_expr (type, arg0);
1609 case NEGATE_EXPR:
1611 /* Can't call fold_negate_const directly here as that doesn't
1612 handle all cases and we might not be able to negate some
1613 constants. */
1614 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1615 if (tem && CONSTANT_CLASS_P (tem))
1616 return tem;
1617 break;
1620 case ABS_EXPR:
1621 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1622 return fold_abs_const (arg0, type);
1623 break;
1625 case CONJ_EXPR:
1626 if (TREE_CODE (arg0) == COMPLEX_CST)
1628 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1629 TREE_TYPE (type));
1630 return build_complex (type, TREE_REALPART (arg0), ipart);
1632 break;
1634 case BIT_NOT_EXPR:
1635 if (TREE_CODE (arg0) == INTEGER_CST)
1636 return fold_not_const (arg0, type);
1637 /* Perform BIT_NOT_EXPR on each element individually. */
1638 else if (TREE_CODE (arg0) == VECTOR_CST)
1640 tree elem;
1641 unsigned count = VECTOR_CST_NELTS (arg0), i;
1643 auto_vec<tree, 32> elements (count);
1644 for (i = 0; i < count; i++)
1646 elem = VECTOR_CST_ELT (arg0, i);
1647 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1648 if (elem == NULL_TREE)
1649 break;
1650 elements.quick_push (elem);
1652 if (i == count)
1653 return build_vector (type, elements);
1655 break;
1657 case TRUTH_NOT_EXPR:
1658 if (TREE_CODE (arg0) == INTEGER_CST)
1659 return constant_boolean_node (integer_zerop (arg0), type);
1660 break;
1662 case REALPART_EXPR:
1663 if (TREE_CODE (arg0) == COMPLEX_CST)
1664 return fold_convert (type, TREE_REALPART (arg0));
1665 break;
1667 case IMAGPART_EXPR:
1668 if (TREE_CODE (arg0) == COMPLEX_CST)
1669 return fold_convert (type, TREE_IMAGPART (arg0));
1670 break;
1672 case VEC_UNPACK_LO_EXPR:
1673 case VEC_UNPACK_HI_EXPR:
1674 case VEC_UNPACK_FLOAT_LO_EXPR:
1675 case VEC_UNPACK_FLOAT_HI_EXPR:
1677 unsigned int out_nelts, in_nelts, i;
1678 enum tree_code subcode;
1680 if (TREE_CODE (arg0) != VECTOR_CST)
1681 return NULL_TREE;
1683 in_nelts = VECTOR_CST_NELTS (arg0);
1684 out_nelts = in_nelts / 2;
1685 gcc_assert (out_nelts == TYPE_VECTOR_SUBPARTS (type));
1687 unsigned int offset = 0;
1688 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1689 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1690 offset = out_nelts;
1692 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1693 subcode = NOP_EXPR;
1694 else
1695 subcode = FLOAT_EXPR;
1697 auto_vec<tree, 32> elts (out_nelts);
1698 for (i = 0; i < out_nelts; i++)
1700 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1701 VECTOR_CST_ELT (arg0, i + offset));
1702 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1703 return NULL_TREE;
1704 elts.quick_push (elt);
1707 return build_vector (type, elts);
1710 case REDUC_MIN_EXPR:
1711 case REDUC_MAX_EXPR:
1712 case REDUC_PLUS_EXPR:
1714 unsigned int nelts, i;
1715 enum tree_code subcode;
1717 if (TREE_CODE (arg0) != VECTOR_CST)
1718 return NULL_TREE;
1719 nelts = VECTOR_CST_NELTS (arg0);
1721 switch (code)
1723 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1724 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1725 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1726 default: gcc_unreachable ();
1729 tree res = VECTOR_CST_ELT (arg0, 0);
1730 for (i = 1; i < nelts; i++)
1732 res = const_binop (subcode, res, VECTOR_CST_ELT (arg0, i));
1733 if (res == NULL_TREE || !CONSTANT_CLASS_P (res))
1734 return NULL_TREE;
1737 return res;
1740 default:
1741 break;
1744 return NULL_TREE;
1747 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1748 indicates which particular sizetype to create. */
1750 tree
1751 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1753 return build_int_cst (sizetype_tab[(int) kind], number);
1756 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1757 is a tree code. The type of the result is taken from the operands.
1758 Both must be equivalent integer types, ala int_binop_types_match_p.
1759 If the operands are constant, so is the result. */
1761 tree
1762 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1764 tree type = TREE_TYPE (arg0);
1766 if (arg0 == error_mark_node || arg1 == error_mark_node)
1767 return error_mark_node;
1769 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1770 TREE_TYPE (arg1)));
1772 /* Handle the special case of two integer constants faster. */
1773 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1775 /* And some specific cases even faster than that. */
1776 if (code == PLUS_EXPR)
1778 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1779 return arg1;
1780 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1781 return arg0;
1783 else if (code == MINUS_EXPR)
1785 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1786 return arg0;
1788 else if (code == MULT_EXPR)
1790 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1791 return arg1;
1794 /* Handle general case of two integer constants. For sizetype
1795 constant calculations we always want to know about overflow,
1796 even in the unsigned case. */
1797 return int_const_binop_1 (code, arg0, arg1, -1);
1800 return fold_build2_loc (loc, code, type, arg0, arg1);
1803 /* Given two values, either both of sizetype or both of bitsizetype,
1804 compute the difference between the two values. Return the value
1805 in signed type corresponding to the type of the operands. */
1807 tree
1808 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1810 tree type = TREE_TYPE (arg0);
1811 tree ctype;
1813 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1814 TREE_TYPE (arg1)));
1816 /* If the type is already signed, just do the simple thing. */
1817 if (!TYPE_UNSIGNED (type))
1818 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1820 if (type == sizetype)
1821 ctype = ssizetype;
1822 else if (type == bitsizetype)
1823 ctype = sbitsizetype;
1824 else
1825 ctype = signed_type_for (type);
1827 /* If either operand is not a constant, do the conversions to the signed
1828 type and subtract. The hardware will do the right thing with any
1829 overflow in the subtraction. */
1830 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1831 return size_binop_loc (loc, MINUS_EXPR,
1832 fold_convert_loc (loc, ctype, arg0),
1833 fold_convert_loc (loc, ctype, arg1));
1835 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1836 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1837 overflow) and negate (which can't either). Special-case a result
1838 of zero while we're here. */
1839 if (tree_int_cst_equal (arg0, arg1))
1840 return build_int_cst (ctype, 0);
1841 else if (tree_int_cst_lt (arg1, arg0))
1842 return fold_convert_loc (loc, ctype,
1843 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1844 else
1845 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1846 fold_convert_loc (loc, ctype,
1847 size_binop_loc (loc,
1848 MINUS_EXPR,
1849 arg1, arg0)));
1852 /* A subroutine of fold_convert_const handling conversions of an
1853 INTEGER_CST to another integer type. */
1855 static tree
1856 fold_convert_const_int_from_int (tree type, const_tree arg1)
1858 /* Given an integer constant, make new constant with new type,
1859 appropriately sign-extended or truncated. Use widest_int
1860 so that any extension is done according ARG1's type. */
1861 return force_fit_type (type, wi::to_widest (arg1),
1862 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1863 TREE_OVERFLOW (arg1));
1866 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1867 to an integer type. */
1869 static tree
1870 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1872 bool overflow = false;
1873 tree t;
1875 /* The following code implements the floating point to integer
1876 conversion rules required by the Java Language Specification,
1877 that IEEE NaNs are mapped to zero and values that overflow
1878 the target precision saturate, i.e. values greater than
1879 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1880 are mapped to INT_MIN. These semantics are allowed by the
1881 C and C++ standards that simply state that the behavior of
1882 FP-to-integer conversion is unspecified upon overflow. */
1884 wide_int val;
1885 REAL_VALUE_TYPE r;
1886 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1888 switch (code)
1890 case FIX_TRUNC_EXPR:
1891 real_trunc (&r, VOIDmode, &x);
1892 break;
1894 default:
1895 gcc_unreachable ();
1898 /* If R is NaN, return zero and show we have an overflow. */
1899 if (REAL_VALUE_ISNAN (r))
1901 overflow = true;
1902 val = wi::zero (TYPE_PRECISION (type));
1905 /* See if R is less than the lower bound or greater than the
1906 upper bound. */
1908 if (! overflow)
1910 tree lt = TYPE_MIN_VALUE (type);
1911 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1912 if (real_less (&r, &l))
1914 overflow = true;
1915 val = wi::to_wide (lt);
1919 if (! overflow)
1921 tree ut = TYPE_MAX_VALUE (type);
1922 if (ut)
1924 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1925 if (real_less (&u, &r))
1927 overflow = true;
1928 val = wi::to_wide (ut);
1933 if (! overflow)
1934 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1936 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1937 return t;
1940 /* A subroutine of fold_convert_const handling conversions of a
1941 FIXED_CST to an integer type. */
1943 static tree
1944 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1946 tree t;
1947 double_int temp, temp_trunc;
1948 scalar_mode mode;
1950 /* Right shift FIXED_CST to temp by fbit. */
1951 temp = TREE_FIXED_CST (arg1).data;
1952 mode = TREE_FIXED_CST (arg1).mode;
1953 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1955 temp = temp.rshift (GET_MODE_FBIT (mode),
1956 HOST_BITS_PER_DOUBLE_INT,
1957 SIGNED_FIXED_POINT_MODE_P (mode));
1959 /* Left shift temp to temp_trunc by fbit. */
1960 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1961 HOST_BITS_PER_DOUBLE_INT,
1962 SIGNED_FIXED_POINT_MODE_P (mode));
1964 else
1966 temp = double_int_zero;
1967 temp_trunc = double_int_zero;
1970 /* If FIXED_CST is negative, we need to round the value toward 0.
1971 By checking if the fractional bits are not zero to add 1 to temp. */
1972 if (SIGNED_FIXED_POINT_MODE_P (mode)
1973 && temp_trunc.is_negative ()
1974 && TREE_FIXED_CST (arg1).data != temp_trunc)
1975 temp += double_int_one;
1977 /* Given a fixed-point constant, make new constant with new type,
1978 appropriately sign-extended or truncated. */
1979 t = force_fit_type (type, temp, -1,
1980 (temp.is_negative ()
1981 && (TYPE_UNSIGNED (type)
1982 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1983 | TREE_OVERFLOW (arg1));
1985 return t;
1988 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1989 to another floating point type. */
1991 static tree
1992 fold_convert_const_real_from_real (tree type, const_tree arg1)
1994 REAL_VALUE_TYPE value;
1995 tree t;
1997 /* Don't perform the operation if flag_signaling_nans is on
1998 and the operand is a signaling NaN. */
1999 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2000 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2001 return NULL_TREE;
2003 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2004 t = build_real (type, value);
2006 /* If converting an infinity or NAN to a representation that doesn't
2007 have one, set the overflow bit so that we can produce some kind of
2008 error message at the appropriate point if necessary. It's not the
2009 most user-friendly message, but it's better than nothing. */
2010 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2011 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2012 TREE_OVERFLOW (t) = 1;
2013 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2014 && !MODE_HAS_NANS (TYPE_MODE (type)))
2015 TREE_OVERFLOW (t) = 1;
2016 /* Regular overflow, conversion produced an infinity in a mode that
2017 can't represent them. */
2018 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2019 && REAL_VALUE_ISINF (value)
2020 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2021 TREE_OVERFLOW (t) = 1;
2022 else
2023 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2024 return t;
2027 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2028 to a floating point type. */
2030 static tree
2031 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2033 REAL_VALUE_TYPE value;
2034 tree t;
2036 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2037 &TREE_FIXED_CST (arg1));
2038 t = build_real (type, value);
2040 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2041 return t;
2044 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2045 to another fixed-point type. */
2047 static tree
2048 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2050 FIXED_VALUE_TYPE value;
2051 tree t;
2052 bool overflow_p;
2054 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2055 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2056 t = build_fixed (type, value);
2058 /* Propagate overflow flags. */
2059 if (overflow_p | TREE_OVERFLOW (arg1))
2060 TREE_OVERFLOW (t) = 1;
2061 return t;
2064 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2065 to a fixed-point type. */
2067 static tree
2068 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2070 FIXED_VALUE_TYPE value;
2071 tree t;
2072 bool overflow_p;
2073 double_int di;
2075 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2077 di.low = TREE_INT_CST_ELT (arg1, 0);
2078 if (TREE_INT_CST_NUNITS (arg1) == 1)
2079 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2080 else
2081 di.high = TREE_INT_CST_ELT (arg1, 1);
2083 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2084 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2085 TYPE_SATURATING (type));
2086 t = build_fixed (type, value);
2088 /* Propagate overflow flags. */
2089 if (overflow_p | TREE_OVERFLOW (arg1))
2090 TREE_OVERFLOW (t) = 1;
2091 return t;
2094 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2095 to a fixed-point type. */
2097 static tree
2098 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2100 FIXED_VALUE_TYPE value;
2101 tree t;
2102 bool overflow_p;
2104 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2105 &TREE_REAL_CST (arg1),
2106 TYPE_SATURATING (type));
2107 t = build_fixed (type, value);
2109 /* Propagate overflow flags. */
2110 if (overflow_p | TREE_OVERFLOW (arg1))
2111 TREE_OVERFLOW (t) = 1;
2112 return t;
2115 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2116 type TYPE. If no simplification can be done return NULL_TREE. */
2118 static tree
2119 fold_convert_const (enum tree_code code, tree type, tree arg1)
2121 if (TREE_TYPE (arg1) == type)
2122 return arg1;
2124 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2125 || TREE_CODE (type) == OFFSET_TYPE)
2127 if (TREE_CODE (arg1) == INTEGER_CST)
2128 return fold_convert_const_int_from_int (type, arg1);
2129 else if (TREE_CODE (arg1) == REAL_CST)
2130 return fold_convert_const_int_from_real (code, type, arg1);
2131 else if (TREE_CODE (arg1) == FIXED_CST)
2132 return fold_convert_const_int_from_fixed (type, arg1);
2134 else if (TREE_CODE (type) == REAL_TYPE)
2136 if (TREE_CODE (arg1) == INTEGER_CST)
2137 return build_real_from_int_cst (type, arg1);
2138 else if (TREE_CODE (arg1) == REAL_CST)
2139 return fold_convert_const_real_from_real (type, arg1);
2140 else if (TREE_CODE (arg1) == FIXED_CST)
2141 return fold_convert_const_real_from_fixed (type, arg1);
2143 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2145 if (TREE_CODE (arg1) == FIXED_CST)
2146 return fold_convert_const_fixed_from_fixed (type, arg1);
2147 else if (TREE_CODE (arg1) == INTEGER_CST)
2148 return fold_convert_const_fixed_from_int (type, arg1);
2149 else if (TREE_CODE (arg1) == REAL_CST)
2150 return fold_convert_const_fixed_from_real (type, arg1);
2152 else if (TREE_CODE (type) == VECTOR_TYPE)
2154 if (TREE_CODE (arg1) == VECTOR_CST
2155 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2157 int len = VECTOR_CST_NELTS (arg1);
2158 tree elttype = TREE_TYPE (type);
2159 auto_vec<tree, 32> v (len);
2160 for (int i = 0; i < len; ++i)
2162 tree elt = VECTOR_CST_ELT (arg1, i);
2163 tree cvt = fold_convert_const (code, elttype, elt);
2164 if (cvt == NULL_TREE)
2165 return NULL_TREE;
2166 v.quick_push (cvt);
2168 return build_vector (type, v);
2171 return NULL_TREE;
2174 /* Construct a vector of zero elements of vector type TYPE. */
2176 static tree
2177 build_zero_vector (tree type)
2179 tree t;
2181 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2182 return build_vector_from_val (type, t);
2185 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2187 bool
2188 fold_convertible_p (const_tree type, const_tree arg)
2190 tree orig = TREE_TYPE (arg);
2192 if (type == orig)
2193 return true;
2195 if (TREE_CODE (arg) == ERROR_MARK
2196 || TREE_CODE (type) == ERROR_MARK
2197 || TREE_CODE (orig) == ERROR_MARK)
2198 return false;
2200 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2201 return true;
2203 switch (TREE_CODE (type))
2205 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2206 case POINTER_TYPE: case REFERENCE_TYPE:
2207 case OFFSET_TYPE:
2208 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2209 || TREE_CODE (orig) == OFFSET_TYPE);
2211 case REAL_TYPE:
2212 case FIXED_POINT_TYPE:
2213 case VECTOR_TYPE:
2214 case VOID_TYPE:
2215 return TREE_CODE (type) == TREE_CODE (orig);
2217 default:
2218 return false;
2222 /* Convert expression ARG to type TYPE. Used by the middle-end for
2223 simple conversions in preference to calling the front-end's convert. */
2225 tree
2226 fold_convert_loc (location_t loc, tree type, tree arg)
2228 tree orig = TREE_TYPE (arg);
2229 tree tem;
2231 if (type == orig)
2232 return arg;
2234 if (TREE_CODE (arg) == ERROR_MARK
2235 || TREE_CODE (type) == ERROR_MARK
2236 || TREE_CODE (orig) == ERROR_MARK)
2237 return error_mark_node;
2239 switch (TREE_CODE (type))
2241 case POINTER_TYPE:
2242 case REFERENCE_TYPE:
2243 /* Handle conversions between pointers to different address spaces. */
2244 if (POINTER_TYPE_P (orig)
2245 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2246 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2247 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2248 /* fall through */
2250 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2251 case OFFSET_TYPE:
2252 if (TREE_CODE (arg) == INTEGER_CST)
2254 tem = fold_convert_const (NOP_EXPR, type, arg);
2255 if (tem != NULL_TREE)
2256 return tem;
2258 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2259 || TREE_CODE (orig) == OFFSET_TYPE)
2260 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2261 if (TREE_CODE (orig) == COMPLEX_TYPE)
2262 return fold_convert_loc (loc, type,
2263 fold_build1_loc (loc, REALPART_EXPR,
2264 TREE_TYPE (orig), arg));
2265 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2266 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2267 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2269 case REAL_TYPE:
2270 if (TREE_CODE (arg) == INTEGER_CST)
2272 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2273 if (tem != NULL_TREE)
2274 return tem;
2276 else if (TREE_CODE (arg) == REAL_CST)
2278 tem = fold_convert_const (NOP_EXPR, type, arg);
2279 if (tem != NULL_TREE)
2280 return tem;
2282 else if (TREE_CODE (arg) == FIXED_CST)
2284 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2285 if (tem != NULL_TREE)
2286 return tem;
2289 switch (TREE_CODE (orig))
2291 case INTEGER_TYPE:
2292 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2293 case POINTER_TYPE: case REFERENCE_TYPE:
2294 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2296 case REAL_TYPE:
2297 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2299 case FIXED_POINT_TYPE:
2300 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2302 case COMPLEX_TYPE:
2303 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2304 return fold_convert_loc (loc, type, tem);
2306 default:
2307 gcc_unreachable ();
2310 case FIXED_POINT_TYPE:
2311 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2312 || TREE_CODE (arg) == REAL_CST)
2314 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2315 if (tem != NULL_TREE)
2316 goto fold_convert_exit;
2319 switch (TREE_CODE (orig))
2321 case FIXED_POINT_TYPE:
2322 case INTEGER_TYPE:
2323 case ENUMERAL_TYPE:
2324 case BOOLEAN_TYPE:
2325 case REAL_TYPE:
2326 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2328 case COMPLEX_TYPE:
2329 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2330 return fold_convert_loc (loc, type, tem);
2332 default:
2333 gcc_unreachable ();
2336 case COMPLEX_TYPE:
2337 switch (TREE_CODE (orig))
2339 case INTEGER_TYPE:
2340 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2341 case POINTER_TYPE: case REFERENCE_TYPE:
2342 case REAL_TYPE:
2343 case FIXED_POINT_TYPE:
2344 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2345 fold_convert_loc (loc, TREE_TYPE (type), arg),
2346 fold_convert_loc (loc, TREE_TYPE (type),
2347 integer_zero_node));
2348 case COMPLEX_TYPE:
2350 tree rpart, ipart;
2352 if (TREE_CODE (arg) == COMPLEX_EXPR)
2354 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2355 TREE_OPERAND (arg, 0));
2356 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2357 TREE_OPERAND (arg, 1));
2358 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2361 arg = save_expr (arg);
2362 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2363 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2364 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2365 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2366 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2369 default:
2370 gcc_unreachable ();
2373 case VECTOR_TYPE:
2374 if (integer_zerop (arg))
2375 return build_zero_vector (type);
2376 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2377 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2378 || TREE_CODE (orig) == VECTOR_TYPE);
2379 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2381 case VOID_TYPE:
2382 tem = fold_ignored_result (arg);
2383 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2385 default:
2386 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2387 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2388 gcc_unreachable ();
2390 fold_convert_exit:
2391 protected_set_expr_location_unshare (tem, loc);
2392 return tem;
2395 /* Return false if expr can be assumed not to be an lvalue, true
2396 otherwise. */
2398 static bool
2399 maybe_lvalue_p (const_tree x)
2401 /* We only need to wrap lvalue tree codes. */
2402 switch (TREE_CODE (x))
2404 case VAR_DECL:
2405 case PARM_DECL:
2406 case RESULT_DECL:
2407 case LABEL_DECL:
2408 case FUNCTION_DECL:
2409 case SSA_NAME:
2411 case COMPONENT_REF:
2412 case MEM_REF:
2413 case INDIRECT_REF:
2414 case ARRAY_REF:
2415 case ARRAY_RANGE_REF:
2416 case BIT_FIELD_REF:
2417 case OBJ_TYPE_REF:
2419 case REALPART_EXPR:
2420 case IMAGPART_EXPR:
2421 case PREINCREMENT_EXPR:
2422 case PREDECREMENT_EXPR:
2423 case SAVE_EXPR:
2424 case TRY_CATCH_EXPR:
2425 case WITH_CLEANUP_EXPR:
2426 case COMPOUND_EXPR:
2427 case MODIFY_EXPR:
2428 case TARGET_EXPR:
2429 case COND_EXPR:
2430 case BIND_EXPR:
2431 break;
2433 default:
2434 /* Assume the worst for front-end tree codes. */
2435 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2436 break;
2437 return false;
2440 return true;
2443 /* Return an expr equal to X but certainly not valid as an lvalue. */
2445 tree
2446 non_lvalue_loc (location_t loc, tree x)
2448 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2449 us. */
2450 if (in_gimple_form)
2451 return x;
2453 if (! maybe_lvalue_p (x))
2454 return x;
2455 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2458 /* When pedantic, return an expr equal to X but certainly not valid as a
2459 pedantic lvalue. Otherwise, return X. */
2461 static tree
2462 pedantic_non_lvalue_loc (location_t loc, tree x)
2464 return protected_set_expr_location_unshare (x, loc);
2467 /* Given a tree comparison code, return the code that is the logical inverse.
2468 It is generally not safe to do this for floating-point comparisons, except
2469 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2470 ERROR_MARK in this case. */
2472 enum tree_code
2473 invert_tree_comparison (enum tree_code code, bool honor_nans)
2475 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2476 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2477 return ERROR_MARK;
2479 switch (code)
2481 case EQ_EXPR:
2482 return NE_EXPR;
2483 case NE_EXPR:
2484 return EQ_EXPR;
2485 case GT_EXPR:
2486 return honor_nans ? UNLE_EXPR : LE_EXPR;
2487 case GE_EXPR:
2488 return honor_nans ? UNLT_EXPR : LT_EXPR;
2489 case LT_EXPR:
2490 return honor_nans ? UNGE_EXPR : GE_EXPR;
2491 case LE_EXPR:
2492 return honor_nans ? UNGT_EXPR : GT_EXPR;
2493 case LTGT_EXPR:
2494 return UNEQ_EXPR;
2495 case UNEQ_EXPR:
2496 return LTGT_EXPR;
2497 case UNGT_EXPR:
2498 return LE_EXPR;
2499 case UNGE_EXPR:
2500 return LT_EXPR;
2501 case UNLT_EXPR:
2502 return GE_EXPR;
2503 case UNLE_EXPR:
2504 return GT_EXPR;
2505 case ORDERED_EXPR:
2506 return UNORDERED_EXPR;
2507 case UNORDERED_EXPR:
2508 return ORDERED_EXPR;
2509 default:
2510 gcc_unreachable ();
2514 /* Similar, but return the comparison that results if the operands are
2515 swapped. This is safe for floating-point. */
2517 enum tree_code
2518 swap_tree_comparison (enum tree_code code)
2520 switch (code)
2522 case EQ_EXPR:
2523 case NE_EXPR:
2524 case ORDERED_EXPR:
2525 case UNORDERED_EXPR:
2526 case LTGT_EXPR:
2527 case UNEQ_EXPR:
2528 return code;
2529 case GT_EXPR:
2530 return LT_EXPR;
2531 case GE_EXPR:
2532 return LE_EXPR;
2533 case LT_EXPR:
2534 return GT_EXPR;
2535 case LE_EXPR:
2536 return GE_EXPR;
2537 case UNGT_EXPR:
2538 return UNLT_EXPR;
2539 case UNGE_EXPR:
2540 return UNLE_EXPR;
2541 case UNLT_EXPR:
2542 return UNGT_EXPR;
2543 case UNLE_EXPR:
2544 return UNGE_EXPR;
2545 default:
2546 gcc_unreachable ();
2551 /* Convert a comparison tree code from an enum tree_code representation
2552 into a compcode bit-based encoding. This function is the inverse of
2553 compcode_to_comparison. */
2555 static enum comparison_code
2556 comparison_to_compcode (enum tree_code code)
2558 switch (code)
2560 case LT_EXPR:
2561 return COMPCODE_LT;
2562 case EQ_EXPR:
2563 return COMPCODE_EQ;
2564 case LE_EXPR:
2565 return COMPCODE_LE;
2566 case GT_EXPR:
2567 return COMPCODE_GT;
2568 case NE_EXPR:
2569 return COMPCODE_NE;
2570 case GE_EXPR:
2571 return COMPCODE_GE;
2572 case ORDERED_EXPR:
2573 return COMPCODE_ORD;
2574 case UNORDERED_EXPR:
2575 return COMPCODE_UNORD;
2576 case UNLT_EXPR:
2577 return COMPCODE_UNLT;
2578 case UNEQ_EXPR:
2579 return COMPCODE_UNEQ;
2580 case UNLE_EXPR:
2581 return COMPCODE_UNLE;
2582 case UNGT_EXPR:
2583 return COMPCODE_UNGT;
2584 case LTGT_EXPR:
2585 return COMPCODE_LTGT;
2586 case UNGE_EXPR:
2587 return COMPCODE_UNGE;
2588 default:
2589 gcc_unreachable ();
2593 /* Convert a compcode bit-based encoding of a comparison operator back
2594 to GCC's enum tree_code representation. This function is the
2595 inverse of comparison_to_compcode. */
2597 static enum tree_code
2598 compcode_to_comparison (enum comparison_code code)
2600 switch (code)
2602 case COMPCODE_LT:
2603 return LT_EXPR;
2604 case COMPCODE_EQ:
2605 return EQ_EXPR;
2606 case COMPCODE_LE:
2607 return LE_EXPR;
2608 case COMPCODE_GT:
2609 return GT_EXPR;
2610 case COMPCODE_NE:
2611 return NE_EXPR;
2612 case COMPCODE_GE:
2613 return GE_EXPR;
2614 case COMPCODE_ORD:
2615 return ORDERED_EXPR;
2616 case COMPCODE_UNORD:
2617 return UNORDERED_EXPR;
2618 case COMPCODE_UNLT:
2619 return UNLT_EXPR;
2620 case COMPCODE_UNEQ:
2621 return UNEQ_EXPR;
2622 case COMPCODE_UNLE:
2623 return UNLE_EXPR;
2624 case COMPCODE_UNGT:
2625 return UNGT_EXPR;
2626 case COMPCODE_LTGT:
2627 return LTGT_EXPR;
2628 case COMPCODE_UNGE:
2629 return UNGE_EXPR;
2630 default:
2631 gcc_unreachable ();
2635 /* Return a tree for the comparison which is the combination of
2636 doing the AND or OR (depending on CODE) of the two operations LCODE
2637 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2638 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2639 if this makes the transformation invalid. */
2641 tree
2642 combine_comparisons (location_t loc,
2643 enum tree_code code, enum tree_code lcode,
2644 enum tree_code rcode, tree truth_type,
2645 tree ll_arg, tree lr_arg)
2647 bool honor_nans = HONOR_NANS (ll_arg);
2648 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2649 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2650 int compcode;
2652 switch (code)
2654 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2655 compcode = lcompcode & rcompcode;
2656 break;
2658 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2659 compcode = lcompcode | rcompcode;
2660 break;
2662 default:
2663 return NULL_TREE;
2666 if (!honor_nans)
2668 /* Eliminate unordered comparisons, as well as LTGT and ORD
2669 which are not used unless the mode has NaNs. */
2670 compcode &= ~COMPCODE_UNORD;
2671 if (compcode == COMPCODE_LTGT)
2672 compcode = COMPCODE_NE;
2673 else if (compcode == COMPCODE_ORD)
2674 compcode = COMPCODE_TRUE;
2676 else if (flag_trapping_math)
2678 /* Check that the original operation and the optimized ones will trap
2679 under the same condition. */
2680 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2681 && (lcompcode != COMPCODE_EQ)
2682 && (lcompcode != COMPCODE_ORD);
2683 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2684 && (rcompcode != COMPCODE_EQ)
2685 && (rcompcode != COMPCODE_ORD);
2686 bool trap = (compcode & COMPCODE_UNORD) == 0
2687 && (compcode != COMPCODE_EQ)
2688 && (compcode != COMPCODE_ORD);
2690 /* In a short-circuited boolean expression the LHS might be
2691 such that the RHS, if evaluated, will never trap. For
2692 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2693 if neither x nor y is NaN. (This is a mixed blessing: for
2694 example, the expression above will never trap, hence
2695 optimizing it to x < y would be invalid). */
2696 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2697 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2698 rtrap = false;
2700 /* If the comparison was short-circuited, and only the RHS
2701 trapped, we may now generate a spurious trap. */
2702 if (rtrap && !ltrap
2703 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2704 return NULL_TREE;
2706 /* If we changed the conditions that cause a trap, we lose. */
2707 if ((ltrap || rtrap) != trap)
2708 return NULL_TREE;
2711 if (compcode == COMPCODE_TRUE)
2712 return constant_boolean_node (true, truth_type);
2713 else if (compcode == COMPCODE_FALSE)
2714 return constant_boolean_node (false, truth_type);
2715 else
2717 enum tree_code tcode;
2719 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2720 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2724 /* Return nonzero if two operands (typically of the same tree node)
2725 are necessarily equal. FLAGS modifies behavior as follows:
2727 If OEP_ONLY_CONST is set, only return nonzero for constants.
2728 This function tests whether the operands are indistinguishable;
2729 it does not test whether they are equal using C's == operation.
2730 The distinction is important for IEEE floating point, because
2731 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2732 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2734 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2735 even though it may hold multiple values during a function.
2736 This is because a GCC tree node guarantees that nothing else is
2737 executed between the evaluation of its "operands" (which may often
2738 be evaluated in arbitrary order). Hence if the operands themselves
2739 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2740 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2741 unset means assuming isochronic (or instantaneous) tree equivalence.
2742 Unless comparing arbitrary expression trees, such as from different
2743 statements, this flag can usually be left unset.
2745 If OEP_PURE_SAME is set, then pure functions with identical arguments
2746 are considered the same. It is used when the caller has other ways
2747 to ensure that global memory is unchanged in between.
2749 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2750 not values of expressions.
2752 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2753 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2755 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2756 any operand with side effect. This is unnecesarily conservative in the
2757 case we know that arg0 and arg1 are in disjoint code paths (such as in
2758 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2759 addresses with TREE_CONSTANT flag set so we know that &var == &var
2760 even if var is volatile. */
2763 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2765 /* When checking, verify at the outermost operand_equal_p call that
2766 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2767 hash value. */
2768 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2770 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2772 if (arg0 != arg1)
2774 inchash::hash hstate0 (0), hstate1 (0);
2775 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2776 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2777 hashval_t h0 = hstate0.end ();
2778 hashval_t h1 = hstate1.end ();
2779 gcc_assert (h0 == h1);
2781 return 1;
2783 else
2784 return 0;
2787 /* If either is ERROR_MARK, they aren't equal. */
2788 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2789 || TREE_TYPE (arg0) == error_mark_node
2790 || TREE_TYPE (arg1) == error_mark_node)
2791 return 0;
2793 /* Similar, if either does not have a type (like a released SSA name),
2794 they aren't equal. */
2795 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2796 return 0;
2798 /* We cannot consider pointers to different address space equal. */
2799 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2800 && POINTER_TYPE_P (TREE_TYPE (arg1))
2801 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2802 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2803 return 0;
2805 /* Check equality of integer constants before bailing out due to
2806 precision differences. */
2807 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2809 /* Address of INTEGER_CST is not defined; check that we did not forget
2810 to drop the OEP_ADDRESS_OF flags. */
2811 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2812 return tree_int_cst_equal (arg0, arg1);
2815 if (!(flags & OEP_ADDRESS_OF))
2817 /* If both types don't have the same signedness, then we can't consider
2818 them equal. We must check this before the STRIP_NOPS calls
2819 because they may change the signedness of the arguments. As pointers
2820 strictly don't have a signedness, require either two pointers or
2821 two non-pointers as well. */
2822 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2823 || POINTER_TYPE_P (TREE_TYPE (arg0))
2824 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2825 return 0;
2827 /* If both types don't have the same precision, then it is not safe
2828 to strip NOPs. */
2829 if (element_precision (TREE_TYPE (arg0))
2830 != element_precision (TREE_TYPE (arg1)))
2831 return 0;
2833 STRIP_NOPS (arg0);
2834 STRIP_NOPS (arg1);
2836 #if 0
2837 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2838 sanity check once the issue is solved. */
2839 else
2840 /* Addresses of conversions and SSA_NAMEs (and many other things)
2841 are not defined. Check that we did not forget to drop the
2842 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2843 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2844 && TREE_CODE (arg0) != SSA_NAME);
2845 #endif
2847 /* In case both args are comparisons but with different comparison
2848 code, try to swap the comparison operands of one arg to produce
2849 a match and compare that variant. */
2850 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2851 && COMPARISON_CLASS_P (arg0)
2852 && COMPARISON_CLASS_P (arg1))
2854 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2856 if (TREE_CODE (arg0) == swap_code)
2857 return operand_equal_p (TREE_OPERAND (arg0, 0),
2858 TREE_OPERAND (arg1, 1), flags)
2859 && operand_equal_p (TREE_OPERAND (arg0, 1),
2860 TREE_OPERAND (arg1, 0), flags);
2863 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2865 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2866 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2868 else if (flags & OEP_ADDRESS_OF)
2870 /* If we are interested in comparing addresses ignore
2871 MEM_REF wrappings of the base that can appear just for
2872 TBAA reasons. */
2873 if (TREE_CODE (arg0) == MEM_REF
2874 && DECL_P (arg1)
2875 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2876 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2877 && integer_zerop (TREE_OPERAND (arg0, 1)))
2878 return 1;
2879 else if (TREE_CODE (arg1) == MEM_REF
2880 && DECL_P (arg0)
2881 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2882 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2883 && integer_zerop (TREE_OPERAND (arg1, 1)))
2884 return 1;
2885 return 0;
2887 else
2888 return 0;
2891 /* When not checking adddresses, this is needed for conversions and for
2892 COMPONENT_REF. Might as well play it safe and always test this. */
2893 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2894 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2895 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2896 && !(flags & OEP_ADDRESS_OF)))
2897 return 0;
2899 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2900 We don't care about side effects in that case because the SAVE_EXPR
2901 takes care of that for us. In all other cases, two expressions are
2902 equal if they have no side effects. If we have two identical
2903 expressions with side effects that should be treated the same due
2904 to the only side effects being identical SAVE_EXPR's, that will
2905 be detected in the recursive calls below.
2906 If we are taking an invariant address of two identical objects
2907 they are necessarily equal as well. */
2908 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2909 && (TREE_CODE (arg0) == SAVE_EXPR
2910 || (flags & OEP_MATCH_SIDE_EFFECTS)
2911 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2912 return 1;
2914 /* Next handle constant cases, those for which we can return 1 even
2915 if ONLY_CONST is set. */
2916 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2917 switch (TREE_CODE (arg0))
2919 case INTEGER_CST:
2920 return tree_int_cst_equal (arg0, arg1);
2922 case FIXED_CST:
2923 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2924 TREE_FIXED_CST (arg1));
2926 case REAL_CST:
2927 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2928 return 1;
2931 if (!HONOR_SIGNED_ZEROS (arg0))
2933 /* If we do not distinguish between signed and unsigned zero,
2934 consider them equal. */
2935 if (real_zerop (arg0) && real_zerop (arg1))
2936 return 1;
2938 return 0;
2940 case VECTOR_CST:
2942 unsigned i;
2944 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2945 return 0;
2947 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2949 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2950 VECTOR_CST_ELT (arg1, i), flags))
2951 return 0;
2953 return 1;
2956 case COMPLEX_CST:
2957 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2958 flags)
2959 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2960 flags));
2962 case STRING_CST:
2963 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2964 && ! memcmp (TREE_STRING_POINTER (arg0),
2965 TREE_STRING_POINTER (arg1),
2966 TREE_STRING_LENGTH (arg0)));
2968 case ADDR_EXPR:
2969 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2970 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2971 flags | OEP_ADDRESS_OF
2972 | OEP_MATCH_SIDE_EFFECTS);
2973 case CONSTRUCTOR:
2974 /* In GIMPLE empty constructors are allowed in initializers of
2975 aggregates. */
2976 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2977 default:
2978 break;
2981 if (flags & OEP_ONLY_CONST)
2982 return 0;
2984 /* Define macros to test an operand from arg0 and arg1 for equality and a
2985 variant that allows null and views null as being different from any
2986 non-null value. In the latter case, if either is null, the both
2987 must be; otherwise, do the normal comparison. */
2988 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2989 TREE_OPERAND (arg1, N), flags)
2991 #define OP_SAME_WITH_NULL(N) \
2992 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2993 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2995 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2997 case tcc_unary:
2998 /* Two conversions are equal only if signedness and modes match. */
2999 switch (TREE_CODE (arg0))
3001 CASE_CONVERT:
3002 case FIX_TRUNC_EXPR:
3003 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3004 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3005 return 0;
3006 break;
3007 default:
3008 break;
3011 return OP_SAME (0);
3014 case tcc_comparison:
3015 case tcc_binary:
3016 if (OP_SAME (0) && OP_SAME (1))
3017 return 1;
3019 /* For commutative ops, allow the other order. */
3020 return (commutative_tree_code (TREE_CODE (arg0))
3021 && operand_equal_p (TREE_OPERAND (arg0, 0),
3022 TREE_OPERAND (arg1, 1), flags)
3023 && operand_equal_p (TREE_OPERAND (arg0, 1),
3024 TREE_OPERAND (arg1, 0), flags));
3026 case tcc_reference:
3027 /* If either of the pointer (or reference) expressions we are
3028 dereferencing contain a side effect, these cannot be equal,
3029 but their addresses can be. */
3030 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3031 && (TREE_SIDE_EFFECTS (arg0)
3032 || TREE_SIDE_EFFECTS (arg1)))
3033 return 0;
3035 switch (TREE_CODE (arg0))
3037 case INDIRECT_REF:
3038 if (!(flags & OEP_ADDRESS_OF)
3039 && (TYPE_ALIGN (TREE_TYPE (arg0))
3040 != TYPE_ALIGN (TREE_TYPE (arg1))))
3041 return 0;
3042 flags &= ~OEP_ADDRESS_OF;
3043 return OP_SAME (0);
3045 case IMAGPART_EXPR:
3046 /* Require the same offset. */
3047 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3048 TYPE_SIZE (TREE_TYPE (arg1)),
3049 flags & ~OEP_ADDRESS_OF))
3050 return 0;
3052 /* Fallthru. */
3053 case REALPART_EXPR:
3054 case VIEW_CONVERT_EXPR:
3055 return OP_SAME (0);
3057 case TARGET_MEM_REF:
3058 case MEM_REF:
3059 if (!(flags & OEP_ADDRESS_OF))
3061 /* Require equal access sizes */
3062 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3063 && (!TYPE_SIZE (TREE_TYPE (arg0))
3064 || !TYPE_SIZE (TREE_TYPE (arg1))
3065 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3066 TYPE_SIZE (TREE_TYPE (arg1)),
3067 flags)))
3068 return 0;
3069 /* Verify that access happens in similar types. */
3070 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3071 return 0;
3072 /* Verify that accesses are TBAA compatible. */
3073 if (!alias_ptr_types_compatible_p
3074 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3075 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3076 || (MR_DEPENDENCE_CLIQUE (arg0)
3077 != MR_DEPENDENCE_CLIQUE (arg1))
3078 || (MR_DEPENDENCE_BASE (arg0)
3079 != MR_DEPENDENCE_BASE (arg1)))
3080 return 0;
3081 /* Verify that alignment is compatible. */
3082 if (TYPE_ALIGN (TREE_TYPE (arg0))
3083 != TYPE_ALIGN (TREE_TYPE (arg1)))
3084 return 0;
3086 flags &= ~OEP_ADDRESS_OF;
3087 return (OP_SAME (0) && OP_SAME (1)
3088 /* TARGET_MEM_REF require equal extra operands. */
3089 && (TREE_CODE (arg0) != TARGET_MEM_REF
3090 || (OP_SAME_WITH_NULL (2)
3091 && OP_SAME_WITH_NULL (3)
3092 && OP_SAME_WITH_NULL (4))));
3094 case ARRAY_REF:
3095 case ARRAY_RANGE_REF:
3096 if (!OP_SAME (0))
3097 return 0;
3098 flags &= ~OEP_ADDRESS_OF;
3099 /* Compare the array index by value if it is constant first as we
3100 may have different types but same value here. */
3101 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3102 TREE_OPERAND (arg1, 1))
3103 || OP_SAME (1))
3104 && OP_SAME_WITH_NULL (2)
3105 && OP_SAME_WITH_NULL (3)
3106 /* Compare low bound and element size as with OEP_ADDRESS_OF
3107 we have to account for the offset of the ref. */
3108 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3109 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3110 || (operand_equal_p (array_ref_low_bound
3111 (CONST_CAST_TREE (arg0)),
3112 array_ref_low_bound
3113 (CONST_CAST_TREE (arg1)), flags)
3114 && operand_equal_p (array_ref_element_size
3115 (CONST_CAST_TREE (arg0)),
3116 array_ref_element_size
3117 (CONST_CAST_TREE (arg1)),
3118 flags))));
3120 case COMPONENT_REF:
3121 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3122 may be NULL when we're called to compare MEM_EXPRs. */
3123 if (!OP_SAME_WITH_NULL (0)
3124 || !OP_SAME (1))
3125 return 0;
3126 flags &= ~OEP_ADDRESS_OF;
3127 return OP_SAME_WITH_NULL (2);
3129 case BIT_FIELD_REF:
3130 if (!OP_SAME (0))
3131 return 0;
3132 flags &= ~OEP_ADDRESS_OF;
3133 return OP_SAME (1) && OP_SAME (2);
3135 default:
3136 return 0;
3139 case tcc_expression:
3140 switch (TREE_CODE (arg0))
3142 case ADDR_EXPR:
3143 /* Be sure we pass right ADDRESS_OF flag. */
3144 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3145 return operand_equal_p (TREE_OPERAND (arg0, 0),
3146 TREE_OPERAND (arg1, 0),
3147 flags | OEP_ADDRESS_OF);
3149 case TRUTH_NOT_EXPR:
3150 return OP_SAME (0);
3152 case TRUTH_ANDIF_EXPR:
3153 case TRUTH_ORIF_EXPR:
3154 return OP_SAME (0) && OP_SAME (1);
3156 case FMA_EXPR:
3157 case WIDEN_MULT_PLUS_EXPR:
3158 case WIDEN_MULT_MINUS_EXPR:
3159 if (!OP_SAME (2))
3160 return 0;
3161 /* The multiplcation operands are commutative. */
3162 /* FALLTHRU */
3164 case TRUTH_AND_EXPR:
3165 case TRUTH_OR_EXPR:
3166 case TRUTH_XOR_EXPR:
3167 if (OP_SAME (0) && OP_SAME (1))
3168 return 1;
3170 /* Otherwise take into account this is a commutative operation. */
3171 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3172 TREE_OPERAND (arg1, 1), flags)
3173 && operand_equal_p (TREE_OPERAND (arg0, 1),
3174 TREE_OPERAND (arg1, 0), flags));
3176 case COND_EXPR:
3177 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3178 return 0;
3179 flags &= ~OEP_ADDRESS_OF;
3180 return OP_SAME (0);
3182 case BIT_INSERT_EXPR:
3183 /* BIT_INSERT_EXPR has an implict operand as the type precision
3184 of op1. Need to check to make sure they are the same. */
3185 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3186 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3187 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3188 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3189 return false;
3190 /* FALLTHRU */
3192 case VEC_COND_EXPR:
3193 case DOT_PROD_EXPR:
3194 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3196 case MODIFY_EXPR:
3197 case INIT_EXPR:
3198 case COMPOUND_EXPR:
3199 case PREDECREMENT_EXPR:
3200 case PREINCREMENT_EXPR:
3201 case POSTDECREMENT_EXPR:
3202 case POSTINCREMENT_EXPR:
3203 if (flags & OEP_LEXICOGRAPHIC)
3204 return OP_SAME (0) && OP_SAME (1);
3205 return 0;
3207 case CLEANUP_POINT_EXPR:
3208 case EXPR_STMT:
3209 if (flags & OEP_LEXICOGRAPHIC)
3210 return OP_SAME (0);
3211 return 0;
3213 default:
3214 return 0;
3217 case tcc_vl_exp:
3218 switch (TREE_CODE (arg0))
3220 case CALL_EXPR:
3221 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3222 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3223 /* If not both CALL_EXPRs are either internal or normal function
3224 functions, then they are not equal. */
3225 return 0;
3226 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3228 /* If the CALL_EXPRs call different internal functions, then they
3229 are not equal. */
3230 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3231 return 0;
3233 else
3235 /* If the CALL_EXPRs call different functions, then they are not
3236 equal. */
3237 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3238 flags))
3239 return 0;
3242 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3244 unsigned int cef = call_expr_flags (arg0);
3245 if (flags & OEP_PURE_SAME)
3246 cef &= ECF_CONST | ECF_PURE;
3247 else
3248 cef &= ECF_CONST;
3249 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3250 return 0;
3253 /* Now see if all the arguments are the same. */
3255 const_call_expr_arg_iterator iter0, iter1;
3256 const_tree a0, a1;
3257 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3258 a1 = first_const_call_expr_arg (arg1, &iter1);
3259 a0 && a1;
3260 a0 = next_const_call_expr_arg (&iter0),
3261 a1 = next_const_call_expr_arg (&iter1))
3262 if (! operand_equal_p (a0, a1, flags))
3263 return 0;
3265 /* If we get here and both argument lists are exhausted
3266 then the CALL_EXPRs are equal. */
3267 return ! (a0 || a1);
3269 default:
3270 return 0;
3273 case tcc_declaration:
3274 /* Consider __builtin_sqrt equal to sqrt. */
3275 return (TREE_CODE (arg0) == FUNCTION_DECL
3276 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3277 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3278 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3280 case tcc_exceptional:
3281 if (TREE_CODE (arg0) == CONSTRUCTOR)
3283 /* In GIMPLE constructors are used only to build vectors from
3284 elements. Individual elements in the constructor must be
3285 indexed in increasing order and form an initial sequence.
3287 We make no effort to compare constructors in generic.
3288 (see sem_variable::equals in ipa-icf which can do so for
3289 constants). */
3290 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3291 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3292 return 0;
3294 /* Be sure that vectors constructed have the same representation.
3295 We only tested element precision and modes to match.
3296 Vectors may be BLKmode and thus also check that the number of
3297 parts match. */
3298 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3299 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3300 return 0;
3302 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3303 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3304 unsigned int len = vec_safe_length (v0);
3306 if (len != vec_safe_length (v1))
3307 return 0;
3309 for (unsigned int i = 0; i < len; i++)
3311 constructor_elt *c0 = &(*v0)[i];
3312 constructor_elt *c1 = &(*v1)[i];
3314 if (!operand_equal_p (c0->value, c1->value, flags)
3315 /* In GIMPLE the indexes can be either NULL or matching i.
3316 Double check this so we won't get false
3317 positives for GENERIC. */
3318 || (c0->index
3319 && (TREE_CODE (c0->index) != INTEGER_CST
3320 || !compare_tree_int (c0->index, i)))
3321 || (c1->index
3322 && (TREE_CODE (c1->index) != INTEGER_CST
3323 || !compare_tree_int (c1->index, i))))
3324 return 0;
3326 return 1;
3328 else if (TREE_CODE (arg0) == STATEMENT_LIST
3329 && (flags & OEP_LEXICOGRAPHIC))
3331 /* Compare the STATEMENT_LISTs. */
3332 tree_stmt_iterator tsi1, tsi2;
3333 tree body1 = CONST_CAST_TREE (arg0);
3334 tree body2 = CONST_CAST_TREE (arg1);
3335 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3336 tsi_next (&tsi1), tsi_next (&tsi2))
3338 /* The lists don't have the same number of statements. */
3339 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3340 return 0;
3341 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3342 return 1;
3343 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3344 OEP_LEXICOGRAPHIC))
3345 return 0;
3348 return 0;
3350 case tcc_statement:
3351 switch (TREE_CODE (arg0))
3353 case RETURN_EXPR:
3354 if (flags & OEP_LEXICOGRAPHIC)
3355 return OP_SAME_WITH_NULL (0);
3356 return 0;
3357 default:
3358 return 0;
3361 default:
3362 return 0;
3365 #undef OP_SAME
3366 #undef OP_SAME_WITH_NULL
3369 /* Similar to operand_equal_p, but strip nops first. */
3371 static bool
3372 operand_equal_for_comparison_p (tree arg0, tree arg1)
3374 if (operand_equal_p (arg0, arg1, 0))
3375 return true;
3377 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3378 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3379 return false;
3381 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3382 and see if the inner values are the same. This removes any
3383 signedness comparison, which doesn't matter here. */
3384 STRIP_NOPS (arg0);
3385 STRIP_NOPS (arg1);
3386 if (operand_equal_p (arg0, arg1, 0))
3387 return true;
3389 return false;
3392 /* See if ARG is an expression that is either a comparison or is performing
3393 arithmetic on comparisons. The comparisons must only be comparing
3394 two different values, which will be stored in *CVAL1 and *CVAL2; if
3395 they are nonzero it means that some operands have already been found.
3396 No variables may be used anywhere else in the expression except in the
3397 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3398 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3400 If this is true, return 1. Otherwise, return zero. */
3402 static int
3403 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3405 enum tree_code code = TREE_CODE (arg);
3406 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3408 /* We can handle some of the tcc_expression cases here. */
3409 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3410 tclass = tcc_unary;
3411 else if (tclass == tcc_expression
3412 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3413 || code == COMPOUND_EXPR))
3414 tclass = tcc_binary;
3416 else if (tclass == tcc_expression && code == SAVE_EXPR
3417 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3419 /* If we've already found a CVAL1 or CVAL2, this expression is
3420 two complex to handle. */
3421 if (*cval1 || *cval2)
3422 return 0;
3424 tclass = tcc_unary;
3425 *save_p = 1;
3428 switch (tclass)
3430 case tcc_unary:
3431 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3433 case tcc_binary:
3434 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3435 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3436 cval1, cval2, save_p));
3438 case tcc_constant:
3439 return 1;
3441 case tcc_expression:
3442 if (code == COND_EXPR)
3443 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3444 cval1, cval2, save_p)
3445 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3446 cval1, cval2, save_p)
3447 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3448 cval1, cval2, save_p));
3449 return 0;
3451 case tcc_comparison:
3452 /* First see if we can handle the first operand, then the second. For
3453 the second operand, we know *CVAL1 can't be zero. It must be that
3454 one side of the comparison is each of the values; test for the
3455 case where this isn't true by failing if the two operands
3456 are the same. */
3458 if (operand_equal_p (TREE_OPERAND (arg, 0),
3459 TREE_OPERAND (arg, 1), 0))
3460 return 0;
3462 if (*cval1 == 0)
3463 *cval1 = TREE_OPERAND (arg, 0);
3464 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3466 else if (*cval2 == 0)
3467 *cval2 = TREE_OPERAND (arg, 0);
3468 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3470 else
3471 return 0;
3473 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3475 else if (*cval2 == 0)
3476 *cval2 = TREE_OPERAND (arg, 1);
3477 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3479 else
3480 return 0;
3482 return 1;
3484 default:
3485 return 0;
3489 /* ARG is a tree that is known to contain just arithmetic operations and
3490 comparisons. Evaluate the operations in the tree substituting NEW0 for
3491 any occurrence of OLD0 as an operand of a comparison and likewise for
3492 NEW1 and OLD1. */
3494 static tree
3495 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3496 tree old1, tree new1)
3498 tree type = TREE_TYPE (arg);
3499 enum tree_code code = TREE_CODE (arg);
3500 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3502 /* We can handle some of the tcc_expression cases here. */
3503 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3504 tclass = tcc_unary;
3505 else if (tclass == tcc_expression
3506 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3507 tclass = tcc_binary;
3509 switch (tclass)
3511 case tcc_unary:
3512 return fold_build1_loc (loc, code, type,
3513 eval_subst (loc, TREE_OPERAND (arg, 0),
3514 old0, new0, old1, new1));
3516 case tcc_binary:
3517 return fold_build2_loc (loc, code, type,
3518 eval_subst (loc, TREE_OPERAND (arg, 0),
3519 old0, new0, old1, new1),
3520 eval_subst (loc, TREE_OPERAND (arg, 1),
3521 old0, new0, old1, new1));
3523 case tcc_expression:
3524 switch (code)
3526 case SAVE_EXPR:
3527 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3528 old1, new1);
3530 case COMPOUND_EXPR:
3531 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3532 old1, new1);
3534 case COND_EXPR:
3535 return fold_build3_loc (loc, code, type,
3536 eval_subst (loc, TREE_OPERAND (arg, 0),
3537 old0, new0, old1, new1),
3538 eval_subst (loc, TREE_OPERAND (arg, 1),
3539 old0, new0, old1, new1),
3540 eval_subst (loc, TREE_OPERAND (arg, 2),
3541 old0, new0, old1, new1));
3542 default:
3543 break;
3545 /* Fall through - ??? */
3547 case tcc_comparison:
3549 tree arg0 = TREE_OPERAND (arg, 0);
3550 tree arg1 = TREE_OPERAND (arg, 1);
3552 /* We need to check both for exact equality and tree equality. The
3553 former will be true if the operand has a side-effect. In that
3554 case, we know the operand occurred exactly once. */
3556 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3557 arg0 = new0;
3558 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3559 arg0 = new1;
3561 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3562 arg1 = new0;
3563 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3564 arg1 = new1;
3566 return fold_build2_loc (loc, code, type, arg0, arg1);
3569 default:
3570 return arg;
3574 /* Return a tree for the case when the result of an expression is RESULT
3575 converted to TYPE and OMITTED was previously an operand of the expression
3576 but is now not needed (e.g., we folded OMITTED * 0).
3578 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3579 the conversion of RESULT to TYPE. */
3581 tree
3582 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3584 tree t = fold_convert_loc (loc, type, result);
3586 /* If the resulting operand is an empty statement, just return the omitted
3587 statement casted to void. */
3588 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3589 return build1_loc (loc, NOP_EXPR, void_type_node,
3590 fold_ignored_result (omitted));
3592 if (TREE_SIDE_EFFECTS (omitted))
3593 return build2_loc (loc, COMPOUND_EXPR, type,
3594 fold_ignored_result (omitted), t);
3596 return non_lvalue_loc (loc, t);
3599 /* Return a tree for the case when the result of an expression is RESULT
3600 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3601 of the expression but are now not needed.
3603 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3604 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3605 evaluated before OMITTED2. Otherwise, if neither has side effects,
3606 just do the conversion of RESULT to TYPE. */
3608 tree
3609 omit_two_operands_loc (location_t loc, tree type, tree result,
3610 tree omitted1, tree omitted2)
3612 tree t = fold_convert_loc (loc, type, result);
3614 if (TREE_SIDE_EFFECTS (omitted2))
3615 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3616 if (TREE_SIDE_EFFECTS (omitted1))
3617 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3619 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3623 /* Return a simplified tree node for the truth-negation of ARG. This
3624 never alters ARG itself. We assume that ARG is an operation that
3625 returns a truth value (0 or 1).
3627 FIXME: one would think we would fold the result, but it causes
3628 problems with the dominator optimizer. */
3630 static tree
3631 fold_truth_not_expr (location_t loc, tree arg)
3633 tree type = TREE_TYPE (arg);
3634 enum tree_code code = TREE_CODE (arg);
3635 location_t loc1, loc2;
3637 /* If this is a comparison, we can simply invert it, except for
3638 floating-point non-equality comparisons, in which case we just
3639 enclose a TRUTH_NOT_EXPR around what we have. */
3641 if (TREE_CODE_CLASS (code) == tcc_comparison)
3643 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3644 if (FLOAT_TYPE_P (op_type)
3645 && flag_trapping_math
3646 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3647 && code != NE_EXPR && code != EQ_EXPR)
3648 return NULL_TREE;
3650 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3651 if (code == ERROR_MARK)
3652 return NULL_TREE;
3654 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3655 TREE_OPERAND (arg, 1));
3656 if (TREE_NO_WARNING (arg))
3657 TREE_NO_WARNING (ret) = 1;
3658 return ret;
3661 switch (code)
3663 case INTEGER_CST:
3664 return constant_boolean_node (integer_zerop (arg), type);
3666 case TRUTH_AND_EXPR:
3667 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3668 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3669 return build2_loc (loc, TRUTH_OR_EXPR, type,
3670 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3671 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3673 case TRUTH_OR_EXPR:
3674 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3675 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3676 return build2_loc (loc, TRUTH_AND_EXPR, type,
3677 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3678 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3680 case TRUTH_XOR_EXPR:
3681 /* Here we can invert either operand. We invert the first operand
3682 unless the second operand is a TRUTH_NOT_EXPR in which case our
3683 result is the XOR of the first operand with the inside of the
3684 negation of the second operand. */
3686 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3687 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3688 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3689 else
3690 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3691 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3692 TREE_OPERAND (arg, 1));
3694 case TRUTH_ANDIF_EXPR:
3695 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3696 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3697 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3698 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3699 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3701 case TRUTH_ORIF_EXPR:
3702 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3703 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3704 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3705 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3706 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3708 case TRUTH_NOT_EXPR:
3709 return TREE_OPERAND (arg, 0);
3711 case COND_EXPR:
3713 tree arg1 = TREE_OPERAND (arg, 1);
3714 tree arg2 = TREE_OPERAND (arg, 2);
3716 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3717 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3719 /* A COND_EXPR may have a throw as one operand, which
3720 then has void type. Just leave void operands
3721 as they are. */
3722 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3723 VOID_TYPE_P (TREE_TYPE (arg1))
3724 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3725 VOID_TYPE_P (TREE_TYPE (arg2))
3726 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3729 case COMPOUND_EXPR:
3730 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3731 return build2_loc (loc, COMPOUND_EXPR, type,
3732 TREE_OPERAND (arg, 0),
3733 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3735 case NON_LVALUE_EXPR:
3736 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3737 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3739 CASE_CONVERT:
3740 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3741 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3743 /* fall through */
3745 case FLOAT_EXPR:
3746 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3747 return build1_loc (loc, TREE_CODE (arg), type,
3748 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3750 case BIT_AND_EXPR:
3751 if (!integer_onep (TREE_OPERAND (arg, 1)))
3752 return NULL_TREE;
3753 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3755 case SAVE_EXPR:
3756 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3758 case CLEANUP_POINT_EXPR:
3759 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3760 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3761 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3763 default:
3764 return NULL_TREE;
3768 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3769 assume that ARG is an operation that returns a truth value (0 or 1
3770 for scalars, 0 or -1 for vectors). Return the folded expression if
3771 folding is successful. Otherwise, return NULL_TREE. */
3773 static tree
3774 fold_invert_truthvalue (location_t loc, tree arg)
3776 tree type = TREE_TYPE (arg);
3777 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3778 ? BIT_NOT_EXPR
3779 : TRUTH_NOT_EXPR,
3780 type, arg);
3783 /* Return a simplified tree node for the truth-negation of ARG. This
3784 never alters ARG itself. We assume that ARG is an operation that
3785 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3787 tree
3788 invert_truthvalue_loc (location_t loc, tree arg)
3790 if (TREE_CODE (arg) == ERROR_MARK)
3791 return arg;
3793 tree type = TREE_TYPE (arg);
3794 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3795 ? BIT_NOT_EXPR
3796 : TRUTH_NOT_EXPR,
3797 type, arg);
3800 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3801 with code CODE. This optimization is unsafe. */
3802 static tree
3803 distribute_real_division (location_t loc, enum tree_code code, tree type,
3804 tree arg0, tree arg1)
3806 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3807 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3809 /* (A / C) +- (B / C) -> (A +- B) / C. */
3810 if (mul0 == mul1
3811 && operand_equal_p (TREE_OPERAND (arg0, 1),
3812 TREE_OPERAND (arg1, 1), 0))
3813 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3814 fold_build2_loc (loc, code, type,
3815 TREE_OPERAND (arg0, 0),
3816 TREE_OPERAND (arg1, 0)),
3817 TREE_OPERAND (arg0, 1));
3819 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3820 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3821 TREE_OPERAND (arg1, 0), 0)
3822 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3823 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3825 REAL_VALUE_TYPE r0, r1;
3826 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3827 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3828 if (!mul0)
3829 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3830 if (!mul1)
3831 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3832 real_arithmetic (&r0, code, &r0, &r1);
3833 return fold_build2_loc (loc, MULT_EXPR, type,
3834 TREE_OPERAND (arg0, 0),
3835 build_real (type, r0));
3838 return NULL_TREE;
3841 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3842 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3843 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3844 is the original memory reference used to preserve the alias set of
3845 the access. */
3847 static tree
3848 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3849 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3850 int unsignedp, int reversep)
3852 tree result, bftype;
3854 /* Attempt not to lose the access path if possible. */
3855 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3857 tree ninner = TREE_OPERAND (orig_inner, 0);
3858 machine_mode nmode;
3859 HOST_WIDE_INT nbitsize, nbitpos;
3860 tree noffset;
3861 int nunsignedp, nreversep, nvolatilep = 0;
3862 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3863 &noffset, &nmode, &nunsignedp,
3864 &nreversep, &nvolatilep);
3865 if (base == inner
3866 && noffset == NULL_TREE
3867 && nbitsize >= bitsize
3868 && nbitpos <= bitpos
3869 && bitpos + bitsize <= nbitpos + nbitsize
3870 && !reversep
3871 && !nreversep
3872 && !nvolatilep)
3874 inner = ninner;
3875 bitpos -= nbitpos;
3879 alias_set_type iset = get_alias_set (orig_inner);
3880 if (iset == 0 && get_alias_set (inner) != iset)
3881 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3882 build_fold_addr_expr (inner),
3883 build_int_cst (ptr_type_node, 0));
3885 if (bitpos == 0 && !reversep)
3887 tree size = TYPE_SIZE (TREE_TYPE (inner));
3888 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3889 || POINTER_TYPE_P (TREE_TYPE (inner)))
3890 && tree_fits_shwi_p (size)
3891 && tree_to_shwi (size) == bitsize)
3892 return fold_convert_loc (loc, type, inner);
3895 bftype = type;
3896 if (TYPE_PRECISION (bftype) != bitsize
3897 || TYPE_UNSIGNED (bftype) == !unsignedp)
3898 bftype = build_nonstandard_integer_type (bitsize, 0);
3900 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3901 bitsize_int (bitsize), bitsize_int (bitpos));
3902 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3904 if (bftype != type)
3905 result = fold_convert_loc (loc, type, result);
3907 return result;
3910 /* Optimize a bit-field compare.
3912 There are two cases: First is a compare against a constant and the
3913 second is a comparison of two items where the fields are at the same
3914 bit position relative to the start of a chunk (byte, halfword, word)
3915 large enough to contain it. In these cases we can avoid the shift
3916 implicit in bitfield extractions.
3918 For constants, we emit a compare of the shifted constant with the
3919 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3920 compared. For two fields at the same position, we do the ANDs with the
3921 similar mask and compare the result of the ANDs.
3923 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3924 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3925 are the left and right operands of the comparison, respectively.
3927 If the optimization described above can be done, we return the resulting
3928 tree. Otherwise we return zero. */
3930 static tree
3931 optimize_bit_field_compare (location_t loc, enum tree_code code,
3932 tree compare_type, tree lhs, tree rhs)
3934 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3935 tree type = TREE_TYPE (lhs);
3936 tree unsigned_type;
3937 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3938 machine_mode lmode, rmode;
3939 scalar_int_mode nmode;
3940 int lunsignedp, runsignedp;
3941 int lreversep, rreversep;
3942 int lvolatilep = 0, rvolatilep = 0;
3943 tree linner, rinner = NULL_TREE;
3944 tree mask;
3945 tree offset;
3947 /* Get all the information about the extractions being done. If the bit size
3948 if the same as the size of the underlying object, we aren't doing an
3949 extraction at all and so can do nothing. We also don't want to
3950 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3951 then will no longer be able to replace it. */
3952 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3953 &lunsignedp, &lreversep, &lvolatilep);
3954 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3955 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3956 return 0;
3958 if (const_p)
3959 rreversep = lreversep;
3960 else
3962 /* If this is not a constant, we can only do something if bit positions,
3963 sizes, signedness and storage order are the same. */
3964 rinner
3965 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3966 &runsignedp, &rreversep, &rvolatilep);
3968 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3969 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3970 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3971 return 0;
3974 /* Honor the C++ memory model and mimic what RTL expansion does. */
3975 unsigned HOST_WIDE_INT bitstart = 0;
3976 unsigned HOST_WIDE_INT bitend = 0;
3977 if (TREE_CODE (lhs) == COMPONENT_REF)
3979 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3980 if (offset != NULL_TREE)
3981 return 0;
3984 /* See if we can find a mode to refer to this field. We should be able to,
3985 but fail if we can't. */
3986 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
3987 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3988 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3989 TYPE_ALIGN (TREE_TYPE (rinner))),
3990 BITS_PER_WORD, false, &nmode))
3991 return 0;
3993 /* Set signed and unsigned types of the precision of this mode for the
3994 shifts below. */
3995 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3997 /* Compute the bit position and size for the new reference and our offset
3998 within it. If the new reference is the same size as the original, we
3999 won't optimize anything, so return zero. */
4000 nbitsize = GET_MODE_BITSIZE (nmode);
4001 nbitpos = lbitpos & ~ (nbitsize - 1);
4002 lbitpos -= nbitpos;
4003 if (nbitsize == lbitsize)
4004 return 0;
4006 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4007 lbitpos = nbitsize - lbitsize - lbitpos;
4009 /* Make the mask to be used against the extracted field. */
4010 mask = build_int_cst_type (unsigned_type, -1);
4011 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4012 mask = const_binop (RSHIFT_EXPR, mask,
4013 size_int (nbitsize - lbitsize - lbitpos));
4015 if (! const_p)
4016 /* If not comparing with constant, just rework the comparison
4017 and return. */
4018 return fold_build2_loc (loc, code, compare_type,
4019 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4020 make_bit_field_ref (loc, linner, lhs,
4021 unsigned_type,
4022 nbitsize, nbitpos,
4023 1, lreversep),
4024 mask),
4025 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4026 make_bit_field_ref (loc, rinner, rhs,
4027 unsigned_type,
4028 nbitsize, nbitpos,
4029 1, rreversep),
4030 mask));
4032 /* Otherwise, we are handling the constant case. See if the constant is too
4033 big for the field. Warn and return a tree for 0 (false) if so. We do
4034 this not only for its own sake, but to avoid having to test for this
4035 error case below. If we didn't, we might generate wrong code.
4037 For unsigned fields, the constant shifted right by the field length should
4038 be all zero. For signed fields, the high-order bits should agree with
4039 the sign bit. */
4041 if (lunsignedp)
4043 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4045 warning (0, "comparison is always %d due to width of bit-field",
4046 code == NE_EXPR);
4047 return constant_boolean_node (code == NE_EXPR, compare_type);
4050 else
4052 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4053 if (tem != 0 && tem != -1)
4055 warning (0, "comparison is always %d due to width of bit-field",
4056 code == NE_EXPR);
4057 return constant_boolean_node (code == NE_EXPR, compare_type);
4061 /* Single-bit compares should always be against zero. */
4062 if (lbitsize == 1 && ! integer_zerop (rhs))
4064 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4065 rhs = build_int_cst (type, 0);
4068 /* Make a new bitfield reference, shift the constant over the
4069 appropriate number of bits and mask it with the computed mask
4070 (in case this was a signed field). If we changed it, make a new one. */
4071 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4072 nbitsize, nbitpos, 1, lreversep);
4074 rhs = const_binop (BIT_AND_EXPR,
4075 const_binop (LSHIFT_EXPR,
4076 fold_convert_loc (loc, unsigned_type, rhs),
4077 size_int (lbitpos)),
4078 mask);
4080 lhs = build2_loc (loc, code, compare_type,
4081 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4082 return lhs;
4085 /* Subroutine for fold_truth_andor_1: decode a field reference.
4087 If EXP is a comparison reference, we return the innermost reference.
4089 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4090 set to the starting bit number.
4092 If the innermost field can be completely contained in a mode-sized
4093 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4095 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4096 otherwise it is not changed.
4098 *PUNSIGNEDP is set to the signedness of the field.
4100 *PREVERSEP is set to the storage order of the field.
4102 *PMASK is set to the mask used. This is either contained in a
4103 BIT_AND_EXPR or derived from the width of the field.
4105 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4107 Return 0 if this is not a component reference or is one that we can't
4108 do anything with. */
4110 static tree
4111 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4112 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4113 int *punsignedp, int *preversep, int *pvolatilep,
4114 tree *pmask, tree *pand_mask)
4116 tree exp = *exp_;
4117 tree outer_type = 0;
4118 tree and_mask = 0;
4119 tree mask, inner, offset;
4120 tree unsigned_type;
4121 unsigned int precision;
4123 /* All the optimizations using this function assume integer fields.
4124 There are problems with FP fields since the type_for_size call
4125 below can fail for, e.g., XFmode. */
4126 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4127 return 0;
4129 /* We are interested in the bare arrangement of bits, so strip everything
4130 that doesn't affect the machine mode. However, record the type of the
4131 outermost expression if it may matter below. */
4132 if (CONVERT_EXPR_P (exp)
4133 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4134 outer_type = TREE_TYPE (exp);
4135 STRIP_NOPS (exp);
4137 if (TREE_CODE (exp) == BIT_AND_EXPR)
4139 and_mask = TREE_OPERAND (exp, 1);
4140 exp = TREE_OPERAND (exp, 0);
4141 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4142 if (TREE_CODE (and_mask) != INTEGER_CST)
4143 return 0;
4146 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4147 punsignedp, preversep, pvolatilep);
4148 if ((inner == exp && and_mask == 0)
4149 || *pbitsize < 0 || offset != 0
4150 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4151 /* Reject out-of-bound accesses (PR79731). */
4152 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4153 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4154 *pbitpos + *pbitsize) < 0))
4155 return 0;
4157 *exp_ = exp;
4159 /* If the number of bits in the reference is the same as the bitsize of
4160 the outer type, then the outer type gives the signedness. Otherwise
4161 (in case of a small bitfield) the signedness is unchanged. */
4162 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4163 *punsignedp = TYPE_UNSIGNED (outer_type);
4165 /* Compute the mask to access the bitfield. */
4166 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4167 precision = TYPE_PRECISION (unsigned_type);
4169 mask = build_int_cst_type (unsigned_type, -1);
4171 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4172 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4174 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4175 if (and_mask != 0)
4176 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4177 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4179 *pmask = mask;
4180 *pand_mask = and_mask;
4181 return inner;
4184 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4185 bit positions and MASK is SIGNED. */
4187 static int
4188 all_ones_mask_p (const_tree mask, unsigned int size)
4190 tree type = TREE_TYPE (mask);
4191 unsigned int precision = TYPE_PRECISION (type);
4193 /* If this function returns true when the type of the mask is
4194 UNSIGNED, then there will be errors. In particular see
4195 gcc.c-torture/execute/990326-1.c. There does not appear to be
4196 any documentation paper trail as to why this is so. But the pre
4197 wide-int worked with that restriction and it has been preserved
4198 here. */
4199 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4200 return false;
4202 return wi::mask (size, false, precision) == wi::to_wide (mask);
4205 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4206 represents the sign bit of EXP's type. If EXP represents a sign
4207 or zero extension, also test VAL against the unextended type.
4208 The return value is the (sub)expression whose sign bit is VAL,
4209 or NULL_TREE otherwise. */
4211 tree
4212 sign_bit_p (tree exp, const_tree val)
4214 int width;
4215 tree t;
4217 /* Tree EXP must have an integral type. */
4218 t = TREE_TYPE (exp);
4219 if (! INTEGRAL_TYPE_P (t))
4220 return NULL_TREE;
4222 /* Tree VAL must be an integer constant. */
4223 if (TREE_CODE (val) != INTEGER_CST
4224 || TREE_OVERFLOW (val))
4225 return NULL_TREE;
4227 width = TYPE_PRECISION (t);
4228 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4229 return exp;
4231 /* Handle extension from a narrower type. */
4232 if (TREE_CODE (exp) == NOP_EXPR
4233 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4234 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4236 return NULL_TREE;
4239 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4240 to be evaluated unconditionally. */
4242 static int
4243 simple_operand_p (const_tree exp)
4245 /* Strip any conversions that don't change the machine mode. */
4246 STRIP_NOPS (exp);
4248 return (CONSTANT_CLASS_P (exp)
4249 || TREE_CODE (exp) == SSA_NAME
4250 || (DECL_P (exp)
4251 && ! TREE_ADDRESSABLE (exp)
4252 && ! TREE_THIS_VOLATILE (exp)
4253 && ! DECL_NONLOCAL (exp)
4254 /* Don't regard global variables as simple. They may be
4255 allocated in ways unknown to the compiler (shared memory,
4256 #pragma weak, etc). */
4257 && ! TREE_PUBLIC (exp)
4258 && ! DECL_EXTERNAL (exp)
4259 /* Weakrefs are not safe to be read, since they can be NULL.
4260 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4261 have DECL_WEAK flag set. */
4262 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4263 /* Loading a static variable is unduly expensive, but global
4264 registers aren't expensive. */
4265 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4268 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4269 to be evaluated unconditionally.
4270 I addition to simple_operand_p, we assume that comparisons, conversions,
4271 and logic-not operations are simple, if their operands are simple, too. */
4273 static bool
4274 simple_operand_p_2 (tree exp)
4276 enum tree_code code;
4278 if (TREE_SIDE_EFFECTS (exp)
4279 || tree_could_trap_p (exp))
4280 return false;
4282 while (CONVERT_EXPR_P (exp))
4283 exp = TREE_OPERAND (exp, 0);
4285 code = TREE_CODE (exp);
4287 if (TREE_CODE_CLASS (code) == tcc_comparison)
4288 return (simple_operand_p (TREE_OPERAND (exp, 0))
4289 && simple_operand_p (TREE_OPERAND (exp, 1)));
4291 if (code == TRUTH_NOT_EXPR)
4292 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4294 return simple_operand_p (exp);
4298 /* The following functions are subroutines to fold_range_test and allow it to
4299 try to change a logical combination of comparisons into a range test.
4301 For example, both
4302 X == 2 || X == 3 || X == 4 || X == 5
4304 X >= 2 && X <= 5
4305 are converted to
4306 (unsigned) (X - 2) <= 3
4308 We describe each set of comparisons as being either inside or outside
4309 a range, using a variable named like IN_P, and then describe the
4310 range with a lower and upper bound. If one of the bounds is omitted,
4311 it represents either the highest or lowest value of the type.
4313 In the comments below, we represent a range by two numbers in brackets
4314 preceded by a "+" to designate being inside that range, or a "-" to
4315 designate being outside that range, so the condition can be inverted by
4316 flipping the prefix. An omitted bound is represented by a "-". For
4317 example, "- [-, 10]" means being outside the range starting at the lowest
4318 possible value and ending at 10, in other words, being greater than 10.
4319 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4320 always false.
4322 We set up things so that the missing bounds are handled in a consistent
4323 manner so neither a missing bound nor "true" and "false" need to be
4324 handled using a special case. */
4326 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4327 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4328 and UPPER1_P are nonzero if the respective argument is an upper bound
4329 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4330 must be specified for a comparison. ARG1 will be converted to ARG0's
4331 type if both are specified. */
4333 static tree
4334 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4335 tree arg1, int upper1_p)
4337 tree tem;
4338 int result;
4339 int sgn0, sgn1;
4341 /* If neither arg represents infinity, do the normal operation.
4342 Else, if not a comparison, return infinity. Else handle the special
4343 comparison rules. Note that most of the cases below won't occur, but
4344 are handled for consistency. */
4346 if (arg0 != 0 && arg1 != 0)
4348 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4349 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4350 STRIP_NOPS (tem);
4351 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4354 if (TREE_CODE_CLASS (code) != tcc_comparison)
4355 return 0;
4357 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4358 for neither. In real maths, we cannot assume open ended ranges are
4359 the same. But, this is computer arithmetic, where numbers are finite.
4360 We can therefore make the transformation of any unbounded range with
4361 the value Z, Z being greater than any representable number. This permits
4362 us to treat unbounded ranges as equal. */
4363 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4364 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4365 switch (code)
4367 case EQ_EXPR:
4368 result = sgn0 == sgn1;
4369 break;
4370 case NE_EXPR:
4371 result = sgn0 != sgn1;
4372 break;
4373 case LT_EXPR:
4374 result = sgn0 < sgn1;
4375 break;
4376 case LE_EXPR:
4377 result = sgn0 <= sgn1;
4378 break;
4379 case GT_EXPR:
4380 result = sgn0 > sgn1;
4381 break;
4382 case GE_EXPR:
4383 result = sgn0 >= sgn1;
4384 break;
4385 default:
4386 gcc_unreachable ();
4389 return constant_boolean_node (result, type);
4392 /* Helper routine for make_range. Perform one step for it, return
4393 new expression if the loop should continue or NULL_TREE if it should
4394 stop. */
4396 tree
4397 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4398 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4399 bool *strict_overflow_p)
4401 tree arg0_type = TREE_TYPE (arg0);
4402 tree n_low, n_high, low = *p_low, high = *p_high;
4403 int in_p = *p_in_p, n_in_p;
4405 switch (code)
4407 case TRUTH_NOT_EXPR:
4408 /* We can only do something if the range is testing for zero. */
4409 if (low == NULL_TREE || high == NULL_TREE
4410 || ! integer_zerop (low) || ! integer_zerop (high))
4411 return NULL_TREE;
4412 *p_in_p = ! in_p;
4413 return arg0;
4415 case EQ_EXPR: case NE_EXPR:
4416 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4417 /* We can only do something if the range is testing for zero
4418 and if the second operand is an integer constant. Note that
4419 saying something is "in" the range we make is done by
4420 complementing IN_P since it will set in the initial case of
4421 being not equal to zero; "out" is leaving it alone. */
4422 if (low == NULL_TREE || high == NULL_TREE
4423 || ! integer_zerop (low) || ! integer_zerop (high)
4424 || TREE_CODE (arg1) != INTEGER_CST)
4425 return NULL_TREE;
4427 switch (code)
4429 case NE_EXPR: /* - [c, c] */
4430 low = high = arg1;
4431 break;
4432 case EQ_EXPR: /* + [c, c] */
4433 in_p = ! in_p, low = high = arg1;
4434 break;
4435 case GT_EXPR: /* - [-, c] */
4436 low = 0, high = arg1;
4437 break;
4438 case GE_EXPR: /* + [c, -] */
4439 in_p = ! in_p, low = arg1, high = 0;
4440 break;
4441 case LT_EXPR: /* - [c, -] */
4442 low = arg1, high = 0;
4443 break;
4444 case LE_EXPR: /* + [-, c] */
4445 in_p = ! in_p, low = 0, high = arg1;
4446 break;
4447 default:
4448 gcc_unreachable ();
4451 /* If this is an unsigned comparison, we also know that EXP is
4452 greater than or equal to zero. We base the range tests we make
4453 on that fact, so we record it here so we can parse existing
4454 range tests. We test arg0_type since often the return type
4455 of, e.g. EQ_EXPR, is boolean. */
4456 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4458 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4459 in_p, low, high, 1,
4460 build_int_cst (arg0_type, 0),
4461 NULL_TREE))
4462 return NULL_TREE;
4464 in_p = n_in_p, low = n_low, high = n_high;
4466 /* If the high bound is missing, but we have a nonzero low
4467 bound, reverse the range so it goes from zero to the low bound
4468 minus 1. */
4469 if (high == 0 && low && ! integer_zerop (low))
4471 in_p = ! in_p;
4472 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4473 build_int_cst (TREE_TYPE (low), 1), 0);
4474 low = build_int_cst (arg0_type, 0);
4478 *p_low = low;
4479 *p_high = high;
4480 *p_in_p = in_p;
4481 return arg0;
4483 case NEGATE_EXPR:
4484 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4485 low and high are non-NULL, then normalize will DTRT. */
4486 if (!TYPE_UNSIGNED (arg0_type)
4487 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4489 if (low == NULL_TREE)
4490 low = TYPE_MIN_VALUE (arg0_type);
4491 if (high == NULL_TREE)
4492 high = TYPE_MAX_VALUE (arg0_type);
4495 /* (-x) IN [a,b] -> x in [-b, -a] */
4496 n_low = range_binop (MINUS_EXPR, exp_type,
4497 build_int_cst (exp_type, 0),
4498 0, high, 1);
4499 n_high = range_binop (MINUS_EXPR, exp_type,
4500 build_int_cst (exp_type, 0),
4501 0, low, 0);
4502 if (n_high != 0 && TREE_OVERFLOW (n_high))
4503 return NULL_TREE;
4504 goto normalize;
4506 case BIT_NOT_EXPR:
4507 /* ~ X -> -X - 1 */
4508 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4509 build_int_cst (exp_type, 1));
4511 case PLUS_EXPR:
4512 case MINUS_EXPR:
4513 if (TREE_CODE (arg1) != INTEGER_CST)
4514 return NULL_TREE;
4516 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4517 move a constant to the other side. */
4518 if (!TYPE_UNSIGNED (arg0_type)
4519 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4520 return NULL_TREE;
4522 /* If EXP is signed, any overflow in the computation is undefined,
4523 so we don't worry about it so long as our computations on
4524 the bounds don't overflow. For unsigned, overflow is defined
4525 and this is exactly the right thing. */
4526 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4527 arg0_type, low, 0, arg1, 0);
4528 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4529 arg0_type, high, 1, arg1, 0);
4530 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4531 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4532 return NULL_TREE;
4534 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4535 *strict_overflow_p = true;
4537 normalize:
4538 /* Check for an unsigned range which has wrapped around the maximum
4539 value thus making n_high < n_low, and normalize it. */
4540 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4542 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4543 build_int_cst (TREE_TYPE (n_high), 1), 0);
4544 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4545 build_int_cst (TREE_TYPE (n_low), 1), 0);
4547 /* If the range is of the form +/- [ x+1, x ], we won't
4548 be able to normalize it. But then, it represents the
4549 whole range or the empty set, so make it
4550 +/- [ -, - ]. */
4551 if (tree_int_cst_equal (n_low, low)
4552 && tree_int_cst_equal (n_high, high))
4553 low = high = 0;
4554 else
4555 in_p = ! in_p;
4557 else
4558 low = n_low, high = n_high;
4560 *p_low = low;
4561 *p_high = high;
4562 *p_in_p = in_p;
4563 return arg0;
4565 CASE_CONVERT:
4566 case NON_LVALUE_EXPR:
4567 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4568 return NULL_TREE;
4570 if (! INTEGRAL_TYPE_P (arg0_type)
4571 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4572 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4573 return NULL_TREE;
4575 n_low = low, n_high = high;
4577 if (n_low != 0)
4578 n_low = fold_convert_loc (loc, arg0_type, n_low);
4580 if (n_high != 0)
4581 n_high = fold_convert_loc (loc, arg0_type, n_high);
4583 /* If we're converting arg0 from an unsigned type, to exp,
4584 a signed type, we will be doing the comparison as unsigned.
4585 The tests above have already verified that LOW and HIGH
4586 are both positive.
4588 So we have to ensure that we will handle large unsigned
4589 values the same way that the current signed bounds treat
4590 negative values. */
4592 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4594 tree high_positive;
4595 tree equiv_type;
4596 /* For fixed-point modes, we need to pass the saturating flag
4597 as the 2nd parameter. */
4598 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4599 equiv_type
4600 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4601 TYPE_SATURATING (arg0_type));
4602 else
4603 equiv_type
4604 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4606 /* A range without an upper bound is, naturally, unbounded.
4607 Since convert would have cropped a very large value, use
4608 the max value for the destination type. */
4609 high_positive
4610 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4611 : TYPE_MAX_VALUE (arg0_type);
4613 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4614 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4615 fold_convert_loc (loc, arg0_type,
4616 high_positive),
4617 build_int_cst (arg0_type, 1));
4619 /* If the low bound is specified, "and" the range with the
4620 range for which the original unsigned value will be
4621 positive. */
4622 if (low != 0)
4624 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4625 1, fold_convert_loc (loc, arg0_type,
4626 integer_zero_node),
4627 high_positive))
4628 return NULL_TREE;
4630 in_p = (n_in_p == in_p);
4632 else
4634 /* Otherwise, "or" the range with the range of the input
4635 that will be interpreted as negative. */
4636 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4637 1, fold_convert_loc (loc, arg0_type,
4638 integer_zero_node),
4639 high_positive))
4640 return NULL_TREE;
4642 in_p = (in_p != n_in_p);
4646 *p_low = n_low;
4647 *p_high = n_high;
4648 *p_in_p = in_p;
4649 return arg0;
4651 default:
4652 return NULL_TREE;
4656 /* Given EXP, a logical expression, set the range it is testing into
4657 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4658 actually being tested. *PLOW and *PHIGH will be made of the same
4659 type as the returned expression. If EXP is not a comparison, we
4660 will most likely not be returning a useful value and range. Set
4661 *STRICT_OVERFLOW_P to true if the return value is only valid
4662 because signed overflow is undefined; otherwise, do not change
4663 *STRICT_OVERFLOW_P. */
4665 tree
4666 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4667 bool *strict_overflow_p)
4669 enum tree_code code;
4670 tree arg0, arg1 = NULL_TREE;
4671 tree exp_type, nexp;
4672 int in_p;
4673 tree low, high;
4674 location_t loc = EXPR_LOCATION (exp);
4676 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4677 and see if we can refine the range. Some of the cases below may not
4678 happen, but it doesn't seem worth worrying about this. We "continue"
4679 the outer loop when we've changed something; otherwise we "break"
4680 the switch, which will "break" the while. */
4682 in_p = 0;
4683 low = high = build_int_cst (TREE_TYPE (exp), 0);
4685 while (1)
4687 code = TREE_CODE (exp);
4688 exp_type = TREE_TYPE (exp);
4689 arg0 = NULL_TREE;
4691 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4693 if (TREE_OPERAND_LENGTH (exp) > 0)
4694 arg0 = TREE_OPERAND (exp, 0);
4695 if (TREE_CODE_CLASS (code) == tcc_binary
4696 || TREE_CODE_CLASS (code) == tcc_comparison
4697 || (TREE_CODE_CLASS (code) == tcc_expression
4698 && TREE_OPERAND_LENGTH (exp) > 1))
4699 arg1 = TREE_OPERAND (exp, 1);
4701 if (arg0 == NULL_TREE)
4702 break;
4704 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4705 &high, &in_p, strict_overflow_p);
4706 if (nexp == NULL_TREE)
4707 break;
4708 exp = nexp;
4711 /* If EXP is a constant, we can evaluate whether this is true or false. */
4712 if (TREE_CODE (exp) == INTEGER_CST)
4714 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4715 exp, 0, low, 0))
4716 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4717 exp, 1, high, 1)));
4718 low = high = 0;
4719 exp = 0;
4722 *pin_p = in_p, *plow = low, *phigh = high;
4723 return exp;
4726 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4727 a bitwise check i.e. when
4728 LOW == 0xXX...X00...0
4729 HIGH == 0xXX...X11...1
4730 Return corresponding mask in MASK and stem in VALUE. */
4732 static bool
4733 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4734 tree *value)
4736 if (TREE_CODE (low) != INTEGER_CST
4737 || TREE_CODE (high) != INTEGER_CST)
4738 return false;
4740 unsigned prec = TYPE_PRECISION (type);
4741 wide_int lo = wi::to_wide (low, prec);
4742 wide_int hi = wi::to_wide (high, prec);
4744 wide_int end_mask = lo ^ hi;
4745 if ((end_mask & (end_mask + 1)) != 0
4746 || (lo & end_mask) != 0)
4747 return false;
4749 wide_int stem_mask = ~end_mask;
4750 wide_int stem = lo & stem_mask;
4751 if (stem != (hi & stem_mask))
4752 return false;
4754 *mask = wide_int_to_tree (type, stem_mask);
4755 *value = wide_int_to_tree (type, stem);
4757 return true;
4760 /* Helper routine for build_range_check and match.pd. Return the type to
4761 perform the check or NULL if it shouldn't be optimized. */
4763 tree
4764 range_check_type (tree etype)
4766 /* First make sure that arithmetics in this type is valid, then make sure
4767 that it wraps around. */
4768 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4769 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4770 TYPE_UNSIGNED (etype));
4772 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4774 tree utype, minv, maxv;
4776 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4777 for the type in question, as we rely on this here. */
4778 utype = unsigned_type_for (etype);
4779 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4780 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4781 build_int_cst (TREE_TYPE (maxv), 1), 1);
4782 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4784 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4785 minv, 1, maxv, 1)))
4786 etype = utype;
4787 else
4788 return NULL_TREE;
4790 return etype;
4793 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4794 type, TYPE, return an expression to test if EXP is in (or out of, depending
4795 on IN_P) the range. Return 0 if the test couldn't be created. */
4797 tree
4798 build_range_check (location_t loc, tree type, tree exp, int in_p,
4799 tree low, tree high)
4801 tree etype = TREE_TYPE (exp), mask, value;
4803 /* Disable this optimization for function pointer expressions
4804 on targets that require function pointer canonicalization. */
4805 if (targetm.have_canonicalize_funcptr_for_compare ()
4806 && TREE_CODE (etype) == POINTER_TYPE
4807 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4808 return NULL_TREE;
4810 if (! in_p)
4812 value = build_range_check (loc, type, exp, 1, low, high);
4813 if (value != 0)
4814 return invert_truthvalue_loc (loc, value);
4816 return 0;
4819 if (low == 0 && high == 0)
4820 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4822 if (low == 0)
4823 return fold_build2_loc (loc, LE_EXPR, type, exp,
4824 fold_convert_loc (loc, etype, high));
4826 if (high == 0)
4827 return fold_build2_loc (loc, GE_EXPR, type, exp,
4828 fold_convert_loc (loc, etype, low));
4830 if (operand_equal_p (low, high, 0))
4831 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4832 fold_convert_loc (loc, etype, low));
4834 if (TREE_CODE (exp) == BIT_AND_EXPR
4835 && maskable_range_p (low, high, etype, &mask, &value))
4836 return fold_build2_loc (loc, EQ_EXPR, type,
4837 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4838 exp, mask),
4839 value);
4841 if (integer_zerop (low))
4843 if (! TYPE_UNSIGNED (etype))
4845 etype = unsigned_type_for (etype);
4846 high = fold_convert_loc (loc, etype, high);
4847 exp = fold_convert_loc (loc, etype, exp);
4849 return build_range_check (loc, type, exp, 1, 0, high);
4852 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4853 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4855 int prec = TYPE_PRECISION (etype);
4857 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4859 if (TYPE_UNSIGNED (etype))
4861 tree signed_etype = signed_type_for (etype);
4862 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4863 etype
4864 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4865 else
4866 etype = signed_etype;
4867 exp = fold_convert_loc (loc, etype, exp);
4869 return fold_build2_loc (loc, GT_EXPR, type, exp,
4870 build_int_cst (etype, 0));
4874 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4875 This requires wrap-around arithmetics for the type of the expression. */
4876 etype = range_check_type (etype);
4877 if (etype == NULL_TREE)
4878 return NULL_TREE;
4880 if (POINTER_TYPE_P (etype))
4881 etype = unsigned_type_for (etype);
4883 high = fold_convert_loc (loc, etype, high);
4884 low = fold_convert_loc (loc, etype, low);
4885 exp = fold_convert_loc (loc, etype, exp);
4887 value = const_binop (MINUS_EXPR, high, low);
4889 if (value != 0 && !TREE_OVERFLOW (value))
4890 return build_range_check (loc, type,
4891 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4892 1, build_int_cst (etype, 0), value);
4894 return 0;
4897 /* Return the predecessor of VAL in its type, handling the infinite case. */
4899 static tree
4900 range_predecessor (tree val)
4902 tree type = TREE_TYPE (val);
4904 if (INTEGRAL_TYPE_P (type)
4905 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4906 return 0;
4907 else
4908 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4909 build_int_cst (TREE_TYPE (val), 1), 0);
4912 /* Return the successor of VAL in its type, handling the infinite case. */
4914 static tree
4915 range_successor (tree val)
4917 tree type = TREE_TYPE (val);
4919 if (INTEGRAL_TYPE_P (type)
4920 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4921 return 0;
4922 else
4923 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4924 build_int_cst (TREE_TYPE (val), 1), 0);
4927 /* Given two ranges, see if we can merge them into one. Return 1 if we
4928 can, 0 if we can't. Set the output range into the specified parameters. */
4930 bool
4931 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4932 tree high0, int in1_p, tree low1, tree high1)
4934 int no_overlap;
4935 int subset;
4936 int temp;
4937 tree tem;
4938 int in_p;
4939 tree low, high;
4940 int lowequal = ((low0 == 0 && low1 == 0)
4941 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4942 low0, 0, low1, 0)));
4943 int highequal = ((high0 == 0 && high1 == 0)
4944 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4945 high0, 1, high1, 1)));
4947 /* Make range 0 be the range that starts first, or ends last if they
4948 start at the same value. Swap them if it isn't. */
4949 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4950 low0, 0, low1, 0))
4951 || (lowequal
4952 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4953 high1, 1, high0, 1))))
4955 temp = in0_p, in0_p = in1_p, in1_p = temp;
4956 tem = low0, low0 = low1, low1 = tem;
4957 tem = high0, high0 = high1, high1 = tem;
4960 /* Now flag two cases, whether the ranges are disjoint or whether the
4961 second range is totally subsumed in the first. Note that the tests
4962 below are simplified by the ones above. */
4963 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4964 high0, 1, low1, 0));
4965 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4966 high1, 1, high0, 1));
4968 /* We now have four cases, depending on whether we are including or
4969 excluding the two ranges. */
4970 if (in0_p && in1_p)
4972 /* If they don't overlap, the result is false. If the second range
4973 is a subset it is the result. Otherwise, the range is from the start
4974 of the second to the end of the first. */
4975 if (no_overlap)
4976 in_p = 0, low = high = 0;
4977 else if (subset)
4978 in_p = 1, low = low1, high = high1;
4979 else
4980 in_p = 1, low = low1, high = high0;
4983 else if (in0_p && ! in1_p)
4985 /* If they don't overlap, the result is the first range. If they are
4986 equal, the result is false. If the second range is a subset of the
4987 first, and the ranges begin at the same place, we go from just after
4988 the end of the second range to the end of the first. If the second
4989 range is not a subset of the first, or if it is a subset and both
4990 ranges end at the same place, the range starts at the start of the
4991 first range and ends just before the second range.
4992 Otherwise, we can't describe this as a single range. */
4993 if (no_overlap)
4994 in_p = 1, low = low0, high = high0;
4995 else if (lowequal && highequal)
4996 in_p = 0, low = high = 0;
4997 else if (subset && lowequal)
4999 low = range_successor (high1);
5000 high = high0;
5001 in_p = 1;
5002 if (low == 0)
5004 /* We are in the weird situation where high0 > high1 but
5005 high1 has no successor. Punt. */
5006 return 0;
5009 else if (! subset || highequal)
5011 low = low0;
5012 high = range_predecessor (low1);
5013 in_p = 1;
5014 if (high == 0)
5016 /* low0 < low1 but low1 has no predecessor. Punt. */
5017 return 0;
5020 else
5021 return 0;
5024 else if (! in0_p && in1_p)
5026 /* If they don't overlap, the result is the second range. If the second
5027 is a subset of the first, the result is false. Otherwise,
5028 the range starts just after the first range and ends at the
5029 end of the second. */
5030 if (no_overlap)
5031 in_p = 1, low = low1, high = high1;
5032 else if (subset || highequal)
5033 in_p = 0, low = high = 0;
5034 else
5036 low = range_successor (high0);
5037 high = high1;
5038 in_p = 1;
5039 if (low == 0)
5041 /* high1 > high0 but high0 has no successor. Punt. */
5042 return 0;
5047 else
5049 /* The case where we are excluding both ranges. Here the complex case
5050 is if they don't overlap. In that case, the only time we have a
5051 range is if they are adjacent. If the second is a subset of the
5052 first, the result is the first. Otherwise, the range to exclude
5053 starts at the beginning of the first range and ends at the end of the
5054 second. */
5055 if (no_overlap)
5057 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5058 range_successor (high0),
5059 1, low1, 0)))
5060 in_p = 0, low = low0, high = high1;
5061 else
5063 /* Canonicalize - [min, x] into - [-, x]. */
5064 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5065 switch (TREE_CODE (TREE_TYPE (low0)))
5067 case ENUMERAL_TYPE:
5068 if (TYPE_PRECISION (TREE_TYPE (low0))
5069 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5070 break;
5071 /* FALLTHROUGH */
5072 case INTEGER_TYPE:
5073 if (tree_int_cst_equal (low0,
5074 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5075 low0 = 0;
5076 break;
5077 case POINTER_TYPE:
5078 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5079 && integer_zerop (low0))
5080 low0 = 0;
5081 break;
5082 default:
5083 break;
5086 /* Canonicalize - [x, max] into - [x, -]. */
5087 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5088 switch (TREE_CODE (TREE_TYPE (high1)))
5090 case ENUMERAL_TYPE:
5091 if (TYPE_PRECISION (TREE_TYPE (high1))
5092 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5093 break;
5094 /* FALLTHROUGH */
5095 case INTEGER_TYPE:
5096 if (tree_int_cst_equal (high1,
5097 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5098 high1 = 0;
5099 break;
5100 case POINTER_TYPE:
5101 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5102 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5103 high1, 1,
5104 build_int_cst (TREE_TYPE (high1), 1),
5105 1)))
5106 high1 = 0;
5107 break;
5108 default:
5109 break;
5112 /* The ranges might be also adjacent between the maximum and
5113 minimum values of the given type. For
5114 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5115 return + [x + 1, y - 1]. */
5116 if (low0 == 0 && high1 == 0)
5118 low = range_successor (high0);
5119 high = range_predecessor (low1);
5120 if (low == 0 || high == 0)
5121 return 0;
5123 in_p = 1;
5125 else
5126 return 0;
5129 else if (subset)
5130 in_p = 0, low = low0, high = high0;
5131 else
5132 in_p = 0, low = low0, high = high1;
5135 *pin_p = in_p, *plow = low, *phigh = high;
5136 return 1;
5140 /* Subroutine of fold, looking inside expressions of the form
5141 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5142 of the COND_EXPR. This function is being used also to optimize
5143 A op B ? C : A, by reversing the comparison first.
5145 Return a folded expression whose code is not a COND_EXPR
5146 anymore, or NULL_TREE if no folding opportunity is found. */
5148 static tree
5149 fold_cond_expr_with_comparison (location_t loc, tree type,
5150 tree arg0, tree arg1, tree arg2)
5152 enum tree_code comp_code = TREE_CODE (arg0);
5153 tree arg00 = TREE_OPERAND (arg0, 0);
5154 tree arg01 = TREE_OPERAND (arg0, 1);
5155 tree arg1_type = TREE_TYPE (arg1);
5156 tree tem;
5158 STRIP_NOPS (arg1);
5159 STRIP_NOPS (arg2);
5161 /* If we have A op 0 ? A : -A, consider applying the following
5162 transformations:
5164 A == 0? A : -A same as -A
5165 A != 0? A : -A same as A
5166 A >= 0? A : -A same as abs (A)
5167 A > 0? A : -A same as abs (A)
5168 A <= 0? A : -A same as -abs (A)
5169 A < 0? A : -A same as -abs (A)
5171 None of these transformations work for modes with signed
5172 zeros. If A is +/-0, the first two transformations will
5173 change the sign of the result (from +0 to -0, or vice
5174 versa). The last four will fix the sign of the result,
5175 even though the original expressions could be positive or
5176 negative, depending on the sign of A.
5178 Note that all these transformations are correct if A is
5179 NaN, since the two alternatives (A and -A) are also NaNs. */
5180 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5181 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5182 ? real_zerop (arg01)
5183 : integer_zerop (arg01))
5184 && ((TREE_CODE (arg2) == NEGATE_EXPR
5185 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5186 /* In the case that A is of the form X-Y, '-A' (arg2) may
5187 have already been folded to Y-X, check for that. */
5188 || (TREE_CODE (arg1) == MINUS_EXPR
5189 && TREE_CODE (arg2) == MINUS_EXPR
5190 && operand_equal_p (TREE_OPERAND (arg1, 0),
5191 TREE_OPERAND (arg2, 1), 0)
5192 && operand_equal_p (TREE_OPERAND (arg1, 1),
5193 TREE_OPERAND (arg2, 0), 0))))
5194 switch (comp_code)
5196 case EQ_EXPR:
5197 case UNEQ_EXPR:
5198 tem = fold_convert_loc (loc, arg1_type, arg1);
5199 return fold_convert_loc (loc, type, negate_expr (tem));
5200 case NE_EXPR:
5201 case LTGT_EXPR:
5202 return fold_convert_loc (loc, type, arg1);
5203 case UNGE_EXPR:
5204 case UNGT_EXPR:
5205 if (flag_trapping_math)
5206 break;
5207 /* Fall through. */
5208 case GE_EXPR:
5209 case GT_EXPR:
5210 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5211 break;
5212 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5213 return fold_convert_loc (loc, type, tem);
5214 case UNLE_EXPR:
5215 case UNLT_EXPR:
5216 if (flag_trapping_math)
5217 break;
5218 /* FALLTHRU */
5219 case LE_EXPR:
5220 case LT_EXPR:
5221 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5222 break;
5223 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5224 return negate_expr (fold_convert_loc (loc, type, tem));
5225 default:
5226 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5227 break;
5230 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5231 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5232 both transformations are correct when A is NaN: A != 0
5233 is then true, and A == 0 is false. */
5235 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5236 && integer_zerop (arg01) && integer_zerop (arg2))
5238 if (comp_code == NE_EXPR)
5239 return fold_convert_loc (loc, type, arg1);
5240 else if (comp_code == EQ_EXPR)
5241 return build_zero_cst (type);
5244 /* Try some transformations of A op B ? A : B.
5246 A == B? A : B same as B
5247 A != B? A : B same as A
5248 A >= B? A : B same as max (A, B)
5249 A > B? A : B same as max (B, A)
5250 A <= B? A : B same as min (A, B)
5251 A < B? A : B same as min (B, A)
5253 As above, these transformations don't work in the presence
5254 of signed zeros. For example, if A and B are zeros of
5255 opposite sign, the first two transformations will change
5256 the sign of the result. In the last four, the original
5257 expressions give different results for (A=+0, B=-0) and
5258 (A=-0, B=+0), but the transformed expressions do not.
5260 The first two transformations are correct if either A or B
5261 is a NaN. In the first transformation, the condition will
5262 be false, and B will indeed be chosen. In the case of the
5263 second transformation, the condition A != B will be true,
5264 and A will be chosen.
5266 The conversions to max() and min() are not correct if B is
5267 a number and A is not. The conditions in the original
5268 expressions will be false, so all four give B. The min()
5269 and max() versions would give a NaN instead. */
5270 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5271 && operand_equal_for_comparison_p (arg01, arg2)
5272 /* Avoid these transformations if the COND_EXPR may be used
5273 as an lvalue in the C++ front-end. PR c++/19199. */
5274 && (in_gimple_form
5275 || VECTOR_TYPE_P (type)
5276 || (! lang_GNU_CXX ()
5277 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5278 || ! maybe_lvalue_p (arg1)
5279 || ! maybe_lvalue_p (arg2)))
5281 tree comp_op0 = arg00;
5282 tree comp_op1 = arg01;
5283 tree comp_type = TREE_TYPE (comp_op0);
5285 switch (comp_code)
5287 case EQ_EXPR:
5288 return fold_convert_loc (loc, type, arg2);
5289 case NE_EXPR:
5290 return fold_convert_loc (loc, type, arg1);
5291 case LE_EXPR:
5292 case LT_EXPR:
5293 case UNLE_EXPR:
5294 case UNLT_EXPR:
5295 /* In C++ a ?: expression can be an lvalue, so put the
5296 operand which will be used if they are equal first
5297 so that we can convert this back to the
5298 corresponding COND_EXPR. */
5299 if (!HONOR_NANS (arg1))
5301 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5302 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5303 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5304 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5305 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5306 comp_op1, comp_op0);
5307 return fold_convert_loc (loc, type, tem);
5309 break;
5310 case GE_EXPR:
5311 case GT_EXPR:
5312 case UNGE_EXPR:
5313 case UNGT_EXPR:
5314 if (!HONOR_NANS (arg1))
5316 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5317 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5318 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5319 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5320 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5321 comp_op1, comp_op0);
5322 return fold_convert_loc (loc, type, tem);
5324 break;
5325 case UNEQ_EXPR:
5326 if (!HONOR_NANS (arg1))
5327 return fold_convert_loc (loc, type, arg2);
5328 break;
5329 case LTGT_EXPR:
5330 if (!HONOR_NANS (arg1))
5331 return fold_convert_loc (loc, type, arg1);
5332 break;
5333 default:
5334 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5335 break;
5339 return NULL_TREE;
5344 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5345 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5346 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5347 false) >= 2)
5348 #endif
5350 /* EXP is some logical combination of boolean tests. See if we can
5351 merge it into some range test. Return the new tree if so. */
5353 static tree
5354 fold_range_test (location_t loc, enum tree_code code, tree type,
5355 tree op0, tree op1)
5357 int or_op = (code == TRUTH_ORIF_EXPR
5358 || code == TRUTH_OR_EXPR);
5359 int in0_p, in1_p, in_p;
5360 tree low0, low1, low, high0, high1, high;
5361 bool strict_overflow_p = false;
5362 tree tem, lhs, rhs;
5363 const char * const warnmsg = G_("assuming signed overflow does not occur "
5364 "when simplifying range test");
5366 if (!INTEGRAL_TYPE_P (type))
5367 return 0;
5369 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5370 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5372 /* If this is an OR operation, invert both sides; we will invert
5373 again at the end. */
5374 if (or_op)
5375 in0_p = ! in0_p, in1_p = ! in1_p;
5377 /* If both expressions are the same, if we can merge the ranges, and we
5378 can build the range test, return it or it inverted. If one of the
5379 ranges is always true or always false, consider it to be the same
5380 expression as the other. */
5381 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5382 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5383 in1_p, low1, high1)
5384 && 0 != (tem = (build_range_check (loc, type,
5385 lhs != 0 ? lhs
5386 : rhs != 0 ? rhs : integer_zero_node,
5387 in_p, low, high))))
5389 if (strict_overflow_p)
5390 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5391 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5394 /* On machines where the branch cost is expensive, if this is a
5395 short-circuited branch and the underlying object on both sides
5396 is the same, make a non-short-circuit operation. */
5397 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5398 && !flag_sanitize_coverage
5399 && lhs != 0 && rhs != 0
5400 && (code == TRUTH_ANDIF_EXPR
5401 || code == TRUTH_ORIF_EXPR)
5402 && operand_equal_p (lhs, rhs, 0))
5404 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5405 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5406 which cases we can't do this. */
5407 if (simple_operand_p (lhs))
5408 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5409 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5410 type, op0, op1);
5412 else if (!lang_hooks.decls.global_bindings_p ()
5413 && !CONTAINS_PLACEHOLDER_P (lhs))
5415 tree common = save_expr (lhs);
5417 if (0 != (lhs = build_range_check (loc, type, common,
5418 or_op ? ! in0_p : in0_p,
5419 low0, high0))
5420 && (0 != (rhs = build_range_check (loc, type, common,
5421 or_op ? ! in1_p : in1_p,
5422 low1, high1))))
5424 if (strict_overflow_p)
5425 fold_overflow_warning (warnmsg,
5426 WARN_STRICT_OVERFLOW_COMPARISON);
5427 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5428 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5429 type, lhs, rhs);
5434 return 0;
5437 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5438 bit value. Arrange things so the extra bits will be set to zero if and
5439 only if C is signed-extended to its full width. If MASK is nonzero,
5440 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5442 static tree
5443 unextend (tree c, int p, int unsignedp, tree mask)
5445 tree type = TREE_TYPE (c);
5446 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5447 tree temp;
5449 if (p == modesize || unsignedp)
5450 return c;
5452 /* We work by getting just the sign bit into the low-order bit, then
5453 into the high-order bit, then sign-extend. We then XOR that value
5454 with C. */
5455 temp = build_int_cst (TREE_TYPE (c),
5456 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5458 /* We must use a signed type in order to get an arithmetic right shift.
5459 However, we must also avoid introducing accidental overflows, so that
5460 a subsequent call to integer_zerop will work. Hence we must
5461 do the type conversion here. At this point, the constant is either
5462 zero or one, and the conversion to a signed type can never overflow.
5463 We could get an overflow if this conversion is done anywhere else. */
5464 if (TYPE_UNSIGNED (type))
5465 temp = fold_convert (signed_type_for (type), temp);
5467 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5468 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5469 if (mask != 0)
5470 temp = const_binop (BIT_AND_EXPR, temp,
5471 fold_convert (TREE_TYPE (c), mask));
5472 /* If necessary, convert the type back to match the type of C. */
5473 if (TYPE_UNSIGNED (type))
5474 temp = fold_convert (type, temp);
5476 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5479 /* For an expression that has the form
5480 (A && B) || ~B
5482 (A || B) && ~B,
5483 we can drop one of the inner expressions and simplify to
5484 A || ~B
5486 A && ~B
5487 LOC is the location of the resulting expression. OP is the inner
5488 logical operation; the left-hand side in the examples above, while CMPOP
5489 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5490 removing a condition that guards another, as in
5491 (A != NULL && A->...) || A == NULL
5492 which we must not transform. If RHS_ONLY is true, only eliminate the
5493 right-most operand of the inner logical operation. */
5495 static tree
5496 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5497 bool rhs_only)
5499 tree type = TREE_TYPE (cmpop);
5500 enum tree_code code = TREE_CODE (cmpop);
5501 enum tree_code truthop_code = TREE_CODE (op);
5502 tree lhs = TREE_OPERAND (op, 0);
5503 tree rhs = TREE_OPERAND (op, 1);
5504 tree orig_lhs = lhs, orig_rhs = rhs;
5505 enum tree_code rhs_code = TREE_CODE (rhs);
5506 enum tree_code lhs_code = TREE_CODE (lhs);
5507 enum tree_code inv_code;
5509 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5510 return NULL_TREE;
5512 if (TREE_CODE_CLASS (code) != tcc_comparison)
5513 return NULL_TREE;
5515 if (rhs_code == truthop_code)
5517 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5518 if (newrhs != NULL_TREE)
5520 rhs = newrhs;
5521 rhs_code = TREE_CODE (rhs);
5524 if (lhs_code == truthop_code && !rhs_only)
5526 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5527 if (newlhs != NULL_TREE)
5529 lhs = newlhs;
5530 lhs_code = TREE_CODE (lhs);
5534 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5535 if (inv_code == rhs_code
5536 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5537 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5538 return lhs;
5539 if (!rhs_only && inv_code == lhs_code
5540 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5541 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5542 return rhs;
5543 if (rhs != orig_rhs || lhs != orig_lhs)
5544 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5545 lhs, rhs);
5546 return NULL_TREE;
5549 /* Find ways of folding logical expressions of LHS and RHS:
5550 Try to merge two comparisons to the same innermost item.
5551 Look for range tests like "ch >= '0' && ch <= '9'".
5552 Look for combinations of simple terms on machines with expensive branches
5553 and evaluate the RHS unconditionally.
5555 For example, if we have p->a == 2 && p->b == 4 and we can make an
5556 object large enough to span both A and B, we can do this with a comparison
5557 against the object ANDed with the a mask.
5559 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5560 operations to do this with one comparison.
5562 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5563 function and the one above.
5565 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5566 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5568 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5569 two operands.
5571 We return the simplified tree or 0 if no optimization is possible. */
5573 static tree
5574 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5575 tree lhs, tree rhs)
5577 /* If this is the "or" of two comparisons, we can do something if
5578 the comparisons are NE_EXPR. If this is the "and", we can do something
5579 if the comparisons are EQ_EXPR. I.e.,
5580 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5582 WANTED_CODE is this operation code. For single bit fields, we can
5583 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5584 comparison for one-bit fields. */
5586 enum tree_code wanted_code;
5587 enum tree_code lcode, rcode;
5588 tree ll_arg, lr_arg, rl_arg, rr_arg;
5589 tree ll_inner, lr_inner, rl_inner, rr_inner;
5590 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5591 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5592 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5593 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5594 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5595 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5596 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5597 scalar_int_mode lnmode, rnmode;
5598 tree ll_mask, lr_mask, rl_mask, rr_mask;
5599 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5600 tree l_const, r_const;
5601 tree lntype, rntype, result;
5602 HOST_WIDE_INT first_bit, end_bit;
5603 int volatilep;
5605 /* Start by getting the comparison codes. Fail if anything is volatile.
5606 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5607 it were surrounded with a NE_EXPR. */
5609 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5610 return 0;
5612 lcode = TREE_CODE (lhs);
5613 rcode = TREE_CODE (rhs);
5615 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5617 lhs = build2 (NE_EXPR, truth_type, lhs,
5618 build_int_cst (TREE_TYPE (lhs), 0));
5619 lcode = NE_EXPR;
5622 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5624 rhs = build2 (NE_EXPR, truth_type, rhs,
5625 build_int_cst (TREE_TYPE (rhs), 0));
5626 rcode = NE_EXPR;
5629 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5630 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5631 return 0;
5633 ll_arg = TREE_OPERAND (lhs, 0);
5634 lr_arg = TREE_OPERAND (lhs, 1);
5635 rl_arg = TREE_OPERAND (rhs, 0);
5636 rr_arg = TREE_OPERAND (rhs, 1);
5638 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5639 if (simple_operand_p (ll_arg)
5640 && simple_operand_p (lr_arg))
5642 if (operand_equal_p (ll_arg, rl_arg, 0)
5643 && operand_equal_p (lr_arg, rr_arg, 0))
5645 result = combine_comparisons (loc, code, lcode, rcode,
5646 truth_type, ll_arg, lr_arg);
5647 if (result)
5648 return result;
5650 else if (operand_equal_p (ll_arg, rr_arg, 0)
5651 && operand_equal_p (lr_arg, rl_arg, 0))
5653 result = combine_comparisons (loc, code, lcode,
5654 swap_tree_comparison (rcode),
5655 truth_type, ll_arg, lr_arg);
5656 if (result)
5657 return result;
5661 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5662 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5664 /* If the RHS can be evaluated unconditionally and its operands are
5665 simple, it wins to evaluate the RHS unconditionally on machines
5666 with expensive branches. In this case, this isn't a comparison
5667 that can be merged. */
5669 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5670 false) >= 2
5671 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5672 && simple_operand_p (rl_arg)
5673 && simple_operand_p (rr_arg))
5675 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5676 if (code == TRUTH_OR_EXPR
5677 && lcode == NE_EXPR && integer_zerop (lr_arg)
5678 && rcode == NE_EXPR && integer_zerop (rr_arg)
5679 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5680 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5681 return build2_loc (loc, NE_EXPR, truth_type,
5682 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5683 ll_arg, rl_arg),
5684 build_int_cst (TREE_TYPE (ll_arg), 0));
5686 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5687 if (code == TRUTH_AND_EXPR
5688 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5689 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5690 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5691 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5692 return build2_loc (loc, EQ_EXPR, truth_type,
5693 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5694 ll_arg, rl_arg),
5695 build_int_cst (TREE_TYPE (ll_arg), 0));
5698 /* See if the comparisons can be merged. Then get all the parameters for
5699 each side. */
5701 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5702 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5703 return 0;
5705 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5706 volatilep = 0;
5707 ll_inner = decode_field_reference (loc, &ll_arg,
5708 &ll_bitsize, &ll_bitpos, &ll_mode,
5709 &ll_unsignedp, &ll_reversep, &volatilep,
5710 &ll_mask, &ll_and_mask);
5711 lr_inner = decode_field_reference (loc, &lr_arg,
5712 &lr_bitsize, &lr_bitpos, &lr_mode,
5713 &lr_unsignedp, &lr_reversep, &volatilep,
5714 &lr_mask, &lr_and_mask);
5715 rl_inner = decode_field_reference (loc, &rl_arg,
5716 &rl_bitsize, &rl_bitpos, &rl_mode,
5717 &rl_unsignedp, &rl_reversep, &volatilep,
5718 &rl_mask, &rl_and_mask);
5719 rr_inner = decode_field_reference (loc, &rr_arg,
5720 &rr_bitsize, &rr_bitpos, &rr_mode,
5721 &rr_unsignedp, &rr_reversep, &volatilep,
5722 &rr_mask, &rr_and_mask);
5724 /* It must be true that the inner operation on the lhs of each
5725 comparison must be the same if we are to be able to do anything.
5726 Then see if we have constants. If not, the same must be true for
5727 the rhs's. */
5728 if (volatilep
5729 || ll_reversep != rl_reversep
5730 || ll_inner == 0 || rl_inner == 0
5731 || ! operand_equal_p (ll_inner, rl_inner, 0))
5732 return 0;
5734 if (TREE_CODE (lr_arg) == INTEGER_CST
5735 && TREE_CODE (rr_arg) == INTEGER_CST)
5737 l_const = lr_arg, r_const = rr_arg;
5738 lr_reversep = ll_reversep;
5740 else if (lr_reversep != rr_reversep
5741 || lr_inner == 0 || rr_inner == 0
5742 || ! operand_equal_p (lr_inner, rr_inner, 0))
5743 return 0;
5744 else
5745 l_const = r_const = 0;
5747 /* If either comparison code is not correct for our logical operation,
5748 fail. However, we can convert a one-bit comparison against zero into
5749 the opposite comparison against that bit being set in the field. */
5751 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5752 if (lcode != wanted_code)
5754 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5756 /* Make the left operand unsigned, since we are only interested
5757 in the value of one bit. Otherwise we are doing the wrong
5758 thing below. */
5759 ll_unsignedp = 1;
5760 l_const = ll_mask;
5762 else
5763 return 0;
5766 /* This is analogous to the code for l_const above. */
5767 if (rcode != wanted_code)
5769 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5771 rl_unsignedp = 1;
5772 r_const = rl_mask;
5774 else
5775 return 0;
5778 /* See if we can find a mode that contains both fields being compared on
5779 the left. If we can't, fail. Otherwise, update all constants and masks
5780 to be relative to a field of that size. */
5781 first_bit = MIN (ll_bitpos, rl_bitpos);
5782 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5783 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5784 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5785 volatilep, &lnmode))
5786 return 0;
5788 lnbitsize = GET_MODE_BITSIZE (lnmode);
5789 lnbitpos = first_bit & ~ (lnbitsize - 1);
5790 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5791 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5793 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5795 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5796 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5799 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5800 size_int (xll_bitpos));
5801 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5802 size_int (xrl_bitpos));
5804 if (l_const)
5806 l_const = fold_convert_loc (loc, lntype, l_const);
5807 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5808 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5809 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5810 fold_build1_loc (loc, BIT_NOT_EXPR,
5811 lntype, ll_mask))))
5813 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5815 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5818 if (r_const)
5820 r_const = fold_convert_loc (loc, lntype, r_const);
5821 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5822 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5823 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5824 fold_build1_loc (loc, BIT_NOT_EXPR,
5825 lntype, rl_mask))))
5827 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5829 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5833 /* If the right sides are not constant, do the same for it. Also,
5834 disallow this optimization if a size or signedness mismatch occurs
5835 between the left and right sides. */
5836 if (l_const == 0)
5838 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5839 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5840 /* Make sure the two fields on the right
5841 correspond to the left without being swapped. */
5842 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5843 return 0;
5845 first_bit = MIN (lr_bitpos, rr_bitpos);
5846 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5847 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5848 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5849 volatilep, &rnmode))
5850 return 0;
5852 rnbitsize = GET_MODE_BITSIZE (rnmode);
5853 rnbitpos = first_bit & ~ (rnbitsize - 1);
5854 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5855 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5857 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5859 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5860 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5863 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5864 rntype, lr_mask),
5865 size_int (xlr_bitpos));
5866 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5867 rntype, rr_mask),
5868 size_int (xrr_bitpos));
5870 /* Make a mask that corresponds to both fields being compared.
5871 Do this for both items being compared. If the operands are the
5872 same size and the bits being compared are in the same position
5873 then we can do this by masking both and comparing the masked
5874 results. */
5875 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5876 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5877 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5879 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5880 lntype, lnbitsize, lnbitpos,
5881 ll_unsignedp || rl_unsignedp, ll_reversep);
5882 if (! all_ones_mask_p (ll_mask, lnbitsize))
5883 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5885 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5886 rntype, rnbitsize, rnbitpos,
5887 lr_unsignedp || rr_unsignedp, lr_reversep);
5888 if (! all_ones_mask_p (lr_mask, rnbitsize))
5889 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5891 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5894 /* There is still another way we can do something: If both pairs of
5895 fields being compared are adjacent, we may be able to make a wider
5896 field containing them both.
5898 Note that we still must mask the lhs/rhs expressions. Furthermore,
5899 the mask must be shifted to account for the shift done by
5900 make_bit_field_ref. */
5901 if ((ll_bitsize + ll_bitpos == rl_bitpos
5902 && lr_bitsize + lr_bitpos == rr_bitpos)
5903 || (ll_bitpos == rl_bitpos + rl_bitsize
5904 && lr_bitpos == rr_bitpos + rr_bitsize))
5906 tree type;
5908 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5909 ll_bitsize + rl_bitsize,
5910 MIN (ll_bitpos, rl_bitpos),
5911 ll_unsignedp, ll_reversep);
5912 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5913 lr_bitsize + rr_bitsize,
5914 MIN (lr_bitpos, rr_bitpos),
5915 lr_unsignedp, lr_reversep);
5917 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5918 size_int (MIN (xll_bitpos, xrl_bitpos)));
5919 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5920 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5922 /* Convert to the smaller type before masking out unwanted bits. */
5923 type = lntype;
5924 if (lntype != rntype)
5926 if (lnbitsize > rnbitsize)
5928 lhs = fold_convert_loc (loc, rntype, lhs);
5929 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5930 type = rntype;
5932 else if (lnbitsize < rnbitsize)
5934 rhs = fold_convert_loc (loc, lntype, rhs);
5935 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5936 type = lntype;
5940 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5941 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5943 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5944 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5946 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5949 return 0;
5952 /* Handle the case of comparisons with constants. If there is something in
5953 common between the masks, those bits of the constants must be the same.
5954 If not, the condition is always false. Test for this to avoid generating
5955 incorrect code below. */
5956 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5957 if (! integer_zerop (result)
5958 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5959 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5961 if (wanted_code == NE_EXPR)
5963 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5964 return constant_boolean_node (true, truth_type);
5966 else
5968 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5969 return constant_boolean_node (false, truth_type);
5973 /* Construct the expression we will return. First get the component
5974 reference we will make. Unless the mask is all ones the width of
5975 that field, perform the mask operation. Then compare with the
5976 merged constant. */
5977 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5978 lntype, lnbitsize, lnbitpos,
5979 ll_unsignedp || rl_unsignedp, ll_reversep);
5981 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5982 if (! all_ones_mask_p (ll_mask, lnbitsize))
5983 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5985 return build2_loc (loc, wanted_code, truth_type, result,
5986 const_binop (BIT_IOR_EXPR, l_const, r_const));
5989 /* T is an integer expression that is being multiplied, divided, or taken a
5990 modulus (CODE says which and what kind of divide or modulus) by a
5991 constant C. See if we can eliminate that operation by folding it with
5992 other operations already in T. WIDE_TYPE, if non-null, is a type that
5993 should be used for the computation if wider than our type.
5995 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5996 (X * 2) + (Y * 4). We must, however, be assured that either the original
5997 expression would not overflow or that overflow is undefined for the type
5998 in the language in question.
6000 If we return a non-null expression, it is an equivalent form of the
6001 original computation, but need not be in the original type.
6003 We set *STRICT_OVERFLOW_P to true if the return values depends on
6004 signed overflow being undefined. Otherwise we do not change
6005 *STRICT_OVERFLOW_P. */
6007 static tree
6008 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6009 bool *strict_overflow_p)
6011 /* To avoid exponential search depth, refuse to allow recursion past
6012 three levels. Beyond that (1) it's highly unlikely that we'll find
6013 something interesting and (2) we've probably processed it before
6014 when we built the inner expression. */
6016 static int depth;
6017 tree ret;
6019 if (depth > 3)
6020 return NULL;
6022 depth++;
6023 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6024 depth--;
6026 return ret;
6029 static tree
6030 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6031 bool *strict_overflow_p)
6033 tree type = TREE_TYPE (t);
6034 enum tree_code tcode = TREE_CODE (t);
6035 tree ctype = (wide_type != 0
6036 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6037 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6038 ? wide_type : type);
6039 tree t1, t2;
6040 int same_p = tcode == code;
6041 tree op0 = NULL_TREE, op1 = NULL_TREE;
6042 bool sub_strict_overflow_p;
6044 /* Don't deal with constants of zero here; they confuse the code below. */
6045 if (integer_zerop (c))
6046 return NULL_TREE;
6048 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6049 op0 = TREE_OPERAND (t, 0);
6051 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6052 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6054 /* Note that we need not handle conditional operations here since fold
6055 already handles those cases. So just do arithmetic here. */
6056 switch (tcode)
6058 case INTEGER_CST:
6059 /* For a constant, we can always simplify if we are a multiply
6060 or (for divide and modulus) if it is a multiple of our constant. */
6061 if (code == MULT_EXPR
6062 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6063 TYPE_SIGN (type)))
6065 tree tem = const_binop (code, fold_convert (ctype, t),
6066 fold_convert (ctype, c));
6067 /* If the multiplication overflowed, we lost information on it.
6068 See PR68142 and PR69845. */
6069 if (TREE_OVERFLOW (tem))
6070 return NULL_TREE;
6071 return tem;
6073 break;
6075 CASE_CONVERT: case NON_LVALUE_EXPR:
6076 /* If op0 is an expression ... */
6077 if ((COMPARISON_CLASS_P (op0)
6078 || UNARY_CLASS_P (op0)
6079 || BINARY_CLASS_P (op0)
6080 || VL_EXP_CLASS_P (op0)
6081 || EXPRESSION_CLASS_P (op0))
6082 /* ... and has wrapping overflow, and its type is smaller
6083 than ctype, then we cannot pass through as widening. */
6084 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6085 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6086 && (TYPE_PRECISION (ctype)
6087 > TYPE_PRECISION (TREE_TYPE (op0))))
6088 /* ... or this is a truncation (t is narrower than op0),
6089 then we cannot pass through this narrowing. */
6090 || (TYPE_PRECISION (type)
6091 < TYPE_PRECISION (TREE_TYPE (op0)))
6092 /* ... or signedness changes for division or modulus,
6093 then we cannot pass through this conversion. */
6094 || (code != MULT_EXPR
6095 && (TYPE_UNSIGNED (ctype)
6096 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6097 /* ... or has undefined overflow while the converted to
6098 type has not, we cannot do the operation in the inner type
6099 as that would introduce undefined overflow. */
6100 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6101 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6102 && !TYPE_OVERFLOW_UNDEFINED (type))))
6103 break;
6105 /* Pass the constant down and see if we can make a simplification. If
6106 we can, replace this expression with the inner simplification for
6107 possible later conversion to our or some other type. */
6108 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6109 && TREE_CODE (t2) == INTEGER_CST
6110 && !TREE_OVERFLOW (t2)
6111 && (0 != (t1 = extract_muldiv (op0, t2, code,
6112 code == MULT_EXPR
6113 ? ctype : NULL_TREE,
6114 strict_overflow_p))))
6115 return t1;
6116 break;
6118 case ABS_EXPR:
6119 /* If widening the type changes it from signed to unsigned, then we
6120 must avoid building ABS_EXPR itself as unsigned. */
6121 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6123 tree cstype = (*signed_type_for) (ctype);
6124 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6125 != 0)
6127 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6128 return fold_convert (ctype, t1);
6130 break;
6132 /* If the constant is negative, we cannot simplify this. */
6133 if (tree_int_cst_sgn (c) == -1)
6134 break;
6135 /* FALLTHROUGH */
6136 case NEGATE_EXPR:
6137 /* For division and modulus, type can't be unsigned, as e.g.
6138 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6139 For signed types, even with wrapping overflow, this is fine. */
6140 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6141 break;
6142 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6143 != 0)
6144 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6145 break;
6147 case MIN_EXPR: case MAX_EXPR:
6148 /* If widening the type changes the signedness, then we can't perform
6149 this optimization as that changes the result. */
6150 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6151 break;
6153 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6154 sub_strict_overflow_p = false;
6155 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6156 &sub_strict_overflow_p)) != 0
6157 && (t2 = extract_muldiv (op1, c, code, wide_type,
6158 &sub_strict_overflow_p)) != 0)
6160 if (tree_int_cst_sgn (c) < 0)
6161 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6162 if (sub_strict_overflow_p)
6163 *strict_overflow_p = true;
6164 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6165 fold_convert (ctype, t2));
6167 break;
6169 case LSHIFT_EXPR: case RSHIFT_EXPR:
6170 /* If the second operand is constant, this is a multiplication
6171 or floor division, by a power of two, so we can treat it that
6172 way unless the multiplier or divisor overflows. Signed
6173 left-shift overflow is implementation-defined rather than
6174 undefined in C90, so do not convert signed left shift into
6175 multiplication. */
6176 if (TREE_CODE (op1) == INTEGER_CST
6177 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6178 /* const_binop may not detect overflow correctly,
6179 so check for it explicitly here. */
6180 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6181 wi::to_wide (op1))
6182 && 0 != (t1 = fold_convert (ctype,
6183 const_binop (LSHIFT_EXPR,
6184 size_one_node,
6185 op1)))
6186 && !TREE_OVERFLOW (t1))
6187 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6188 ? MULT_EXPR : FLOOR_DIV_EXPR,
6189 ctype,
6190 fold_convert (ctype, op0),
6191 t1),
6192 c, code, wide_type, strict_overflow_p);
6193 break;
6195 case PLUS_EXPR: case MINUS_EXPR:
6196 /* See if we can eliminate the operation on both sides. If we can, we
6197 can return a new PLUS or MINUS. If we can't, the only remaining
6198 cases where we can do anything are if the second operand is a
6199 constant. */
6200 sub_strict_overflow_p = false;
6201 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6202 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6203 if (t1 != 0 && t2 != 0
6204 && TYPE_OVERFLOW_WRAPS (ctype)
6205 && (code == MULT_EXPR
6206 /* If not multiplication, we can only do this if both operands
6207 are divisible by c. */
6208 || (multiple_of_p (ctype, op0, c)
6209 && multiple_of_p (ctype, op1, c))))
6211 if (sub_strict_overflow_p)
6212 *strict_overflow_p = true;
6213 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6214 fold_convert (ctype, t2));
6217 /* If this was a subtraction, negate OP1 and set it to be an addition.
6218 This simplifies the logic below. */
6219 if (tcode == MINUS_EXPR)
6221 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6222 /* If OP1 was not easily negatable, the constant may be OP0. */
6223 if (TREE_CODE (op0) == INTEGER_CST)
6225 std::swap (op0, op1);
6226 std::swap (t1, t2);
6230 if (TREE_CODE (op1) != INTEGER_CST)
6231 break;
6233 /* If either OP1 or C are negative, this optimization is not safe for
6234 some of the division and remainder types while for others we need
6235 to change the code. */
6236 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6238 if (code == CEIL_DIV_EXPR)
6239 code = FLOOR_DIV_EXPR;
6240 else if (code == FLOOR_DIV_EXPR)
6241 code = CEIL_DIV_EXPR;
6242 else if (code != MULT_EXPR
6243 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6244 break;
6247 /* If it's a multiply or a division/modulus operation of a multiple
6248 of our constant, do the operation and verify it doesn't overflow. */
6249 if (code == MULT_EXPR
6250 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6251 TYPE_SIGN (type)))
6253 op1 = const_binop (code, fold_convert (ctype, op1),
6254 fold_convert (ctype, c));
6255 /* We allow the constant to overflow with wrapping semantics. */
6256 if (op1 == 0
6257 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6258 break;
6260 else
6261 break;
6263 /* If we have an unsigned type, we cannot widen the operation since it
6264 will change the result if the original computation overflowed. */
6265 if (TYPE_UNSIGNED (ctype) && ctype != type)
6266 break;
6268 /* The last case is if we are a multiply. In that case, we can
6269 apply the distributive law to commute the multiply and addition
6270 if the multiplication of the constants doesn't overflow
6271 and overflow is defined. With undefined overflow
6272 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6273 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6274 return fold_build2 (tcode, ctype,
6275 fold_build2 (code, ctype,
6276 fold_convert (ctype, op0),
6277 fold_convert (ctype, c)),
6278 op1);
6280 break;
6282 case MULT_EXPR:
6283 /* We have a special case here if we are doing something like
6284 (C * 8) % 4 since we know that's zero. */
6285 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6286 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6287 /* If the multiplication can overflow we cannot optimize this. */
6288 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6289 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6290 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6291 TYPE_SIGN (type)))
6293 *strict_overflow_p = true;
6294 return omit_one_operand (type, integer_zero_node, op0);
6297 /* ... fall through ... */
6299 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6300 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6301 /* If we can extract our operation from the LHS, do so and return a
6302 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6303 do something only if the second operand is a constant. */
6304 if (same_p
6305 && TYPE_OVERFLOW_WRAPS (ctype)
6306 && (t1 = extract_muldiv (op0, c, code, wide_type,
6307 strict_overflow_p)) != 0)
6308 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6309 fold_convert (ctype, op1));
6310 else if (tcode == MULT_EXPR && code == MULT_EXPR
6311 && TYPE_OVERFLOW_WRAPS (ctype)
6312 && (t1 = extract_muldiv (op1, c, code, wide_type,
6313 strict_overflow_p)) != 0)
6314 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6315 fold_convert (ctype, t1));
6316 else if (TREE_CODE (op1) != INTEGER_CST)
6317 return 0;
6319 /* If these are the same operation types, we can associate them
6320 assuming no overflow. */
6321 if (tcode == code)
6323 bool overflow_p = false;
6324 bool overflow_mul_p;
6325 signop sign = TYPE_SIGN (ctype);
6326 unsigned prec = TYPE_PRECISION (ctype);
6327 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6328 wi::to_wide (c, prec),
6329 sign, &overflow_mul_p);
6330 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6331 if (overflow_mul_p
6332 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6333 overflow_p = true;
6334 if (!overflow_p)
6335 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6336 wide_int_to_tree (ctype, mul));
6339 /* If these operations "cancel" each other, we have the main
6340 optimizations of this pass, which occur when either constant is a
6341 multiple of the other, in which case we replace this with either an
6342 operation or CODE or TCODE.
6344 If we have an unsigned type, we cannot do this since it will change
6345 the result if the original computation overflowed. */
6346 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6347 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6348 || (tcode == MULT_EXPR
6349 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6350 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6351 && code != MULT_EXPR)))
6353 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6354 TYPE_SIGN (type)))
6356 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6357 *strict_overflow_p = true;
6358 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6359 fold_convert (ctype,
6360 const_binop (TRUNC_DIV_EXPR,
6361 op1, c)));
6363 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6364 TYPE_SIGN (type)))
6366 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6367 *strict_overflow_p = true;
6368 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6369 fold_convert (ctype,
6370 const_binop (TRUNC_DIV_EXPR,
6371 c, op1)));
6374 break;
6376 default:
6377 break;
6380 return 0;
6383 /* Return a node which has the indicated constant VALUE (either 0 or
6384 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6385 and is of the indicated TYPE. */
6387 tree
6388 constant_boolean_node (bool value, tree type)
6390 if (type == integer_type_node)
6391 return value ? integer_one_node : integer_zero_node;
6392 else if (type == boolean_type_node)
6393 return value ? boolean_true_node : boolean_false_node;
6394 else if (TREE_CODE (type) == VECTOR_TYPE)
6395 return build_vector_from_val (type,
6396 build_int_cst (TREE_TYPE (type),
6397 value ? -1 : 0));
6398 else
6399 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6403 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6404 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6405 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6406 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6407 COND is the first argument to CODE; otherwise (as in the example
6408 given here), it is the second argument. TYPE is the type of the
6409 original expression. Return NULL_TREE if no simplification is
6410 possible. */
6412 static tree
6413 fold_binary_op_with_conditional_arg (location_t loc,
6414 enum tree_code code,
6415 tree type, tree op0, tree op1,
6416 tree cond, tree arg, int cond_first_p)
6418 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6419 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6420 tree test, true_value, false_value;
6421 tree lhs = NULL_TREE;
6422 tree rhs = NULL_TREE;
6423 enum tree_code cond_code = COND_EXPR;
6425 if (TREE_CODE (cond) == COND_EXPR
6426 || TREE_CODE (cond) == VEC_COND_EXPR)
6428 test = TREE_OPERAND (cond, 0);
6429 true_value = TREE_OPERAND (cond, 1);
6430 false_value = TREE_OPERAND (cond, 2);
6431 /* If this operand throws an expression, then it does not make
6432 sense to try to perform a logical or arithmetic operation
6433 involving it. */
6434 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6435 lhs = true_value;
6436 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6437 rhs = false_value;
6439 else if (!(TREE_CODE (type) != VECTOR_TYPE
6440 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6442 tree testtype = TREE_TYPE (cond);
6443 test = cond;
6444 true_value = constant_boolean_node (true, testtype);
6445 false_value = constant_boolean_node (false, testtype);
6447 else
6448 /* Detect the case of mixing vector and scalar types - bail out. */
6449 return NULL_TREE;
6451 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6452 cond_code = VEC_COND_EXPR;
6454 /* This transformation is only worthwhile if we don't have to wrap ARG
6455 in a SAVE_EXPR and the operation can be simplified without recursing
6456 on at least one of the branches once its pushed inside the COND_EXPR. */
6457 if (!TREE_CONSTANT (arg)
6458 && (TREE_SIDE_EFFECTS (arg)
6459 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6460 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6461 return NULL_TREE;
6463 arg = fold_convert_loc (loc, arg_type, arg);
6464 if (lhs == 0)
6466 true_value = fold_convert_loc (loc, cond_type, true_value);
6467 if (cond_first_p)
6468 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6469 else
6470 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6472 if (rhs == 0)
6474 false_value = fold_convert_loc (loc, cond_type, false_value);
6475 if (cond_first_p)
6476 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6477 else
6478 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6481 /* Check that we have simplified at least one of the branches. */
6482 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6483 return NULL_TREE;
6485 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6489 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6491 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6492 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6493 ADDEND is the same as X.
6495 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6496 and finite. The problematic cases are when X is zero, and its mode
6497 has signed zeros. In the case of rounding towards -infinity,
6498 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6499 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6501 bool
6502 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6504 if (!real_zerop (addend))
6505 return false;
6507 /* Don't allow the fold with -fsignaling-nans. */
6508 if (HONOR_SNANS (element_mode (type)))
6509 return false;
6511 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6512 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6513 return true;
6515 /* In a vector or complex, we would need to check the sign of all zeros. */
6516 if (TREE_CODE (addend) != REAL_CST)
6517 return false;
6519 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6520 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6521 negate = !negate;
6523 /* The mode has signed zeros, and we have to honor their sign.
6524 In this situation, there is only one case we can return true for.
6525 X - 0 is the same as X unless rounding towards -infinity is
6526 supported. */
6527 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6530 /* Subroutine of match.pd that optimizes comparisons of a division by
6531 a nonzero integer constant against an integer constant, i.e.
6532 X/C1 op C2.
6534 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6535 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6537 enum tree_code
6538 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6539 tree *hi, bool *neg_overflow)
6541 tree prod, tmp, type = TREE_TYPE (c1);
6542 signop sign = TYPE_SIGN (type);
6543 bool overflow;
6545 /* We have to do this the hard way to detect unsigned overflow.
6546 prod = int_const_binop (MULT_EXPR, c1, c2); */
6547 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6548 prod = force_fit_type (type, val, -1, overflow);
6549 *neg_overflow = false;
6551 if (sign == UNSIGNED)
6553 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6554 *lo = prod;
6556 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6557 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6558 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6560 else if (tree_int_cst_sgn (c1) >= 0)
6562 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6563 switch (tree_int_cst_sgn (c2))
6565 case -1:
6566 *neg_overflow = true;
6567 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6568 *hi = prod;
6569 break;
6571 case 0:
6572 *lo = fold_negate_const (tmp, type);
6573 *hi = tmp;
6574 break;
6576 case 1:
6577 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6578 *lo = prod;
6579 break;
6581 default:
6582 gcc_unreachable ();
6585 else
6587 /* A negative divisor reverses the relational operators. */
6588 code = swap_tree_comparison (code);
6590 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6591 switch (tree_int_cst_sgn (c2))
6593 case -1:
6594 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6595 *lo = prod;
6596 break;
6598 case 0:
6599 *hi = fold_negate_const (tmp, type);
6600 *lo = tmp;
6601 break;
6603 case 1:
6604 *neg_overflow = true;
6605 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6606 *hi = prod;
6607 break;
6609 default:
6610 gcc_unreachable ();
6614 if (code != EQ_EXPR && code != NE_EXPR)
6615 return code;
6617 if (TREE_OVERFLOW (*lo)
6618 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6619 *lo = NULL_TREE;
6620 if (TREE_OVERFLOW (*hi)
6621 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6622 *hi = NULL_TREE;
6624 return code;
6628 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6629 equality/inequality test, then return a simplified form of the test
6630 using a sign testing. Otherwise return NULL. TYPE is the desired
6631 result type. */
6633 static tree
6634 fold_single_bit_test_into_sign_test (location_t loc,
6635 enum tree_code code, tree arg0, tree arg1,
6636 tree result_type)
6638 /* If this is testing a single bit, we can optimize the test. */
6639 if ((code == NE_EXPR || code == EQ_EXPR)
6640 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6641 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6643 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6644 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6645 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6647 if (arg00 != NULL_TREE
6648 /* This is only a win if casting to a signed type is cheap,
6649 i.e. when arg00's type is not a partial mode. */
6650 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6652 tree stype = signed_type_for (TREE_TYPE (arg00));
6653 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6654 result_type,
6655 fold_convert_loc (loc, stype, arg00),
6656 build_int_cst (stype, 0));
6660 return NULL_TREE;
6663 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6664 equality/inequality test, then return a simplified form of
6665 the test using shifts and logical operations. Otherwise return
6666 NULL. TYPE is the desired result type. */
6668 tree
6669 fold_single_bit_test (location_t loc, enum tree_code code,
6670 tree arg0, tree arg1, tree result_type)
6672 /* If this is testing a single bit, we can optimize the test. */
6673 if ((code == NE_EXPR || code == EQ_EXPR)
6674 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6675 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6677 tree inner = TREE_OPERAND (arg0, 0);
6678 tree type = TREE_TYPE (arg0);
6679 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6680 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6681 int ops_unsigned;
6682 tree signed_type, unsigned_type, intermediate_type;
6683 tree tem, one;
6685 /* First, see if we can fold the single bit test into a sign-bit
6686 test. */
6687 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6688 result_type);
6689 if (tem)
6690 return tem;
6692 /* Otherwise we have (A & C) != 0 where C is a single bit,
6693 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6694 Similarly for (A & C) == 0. */
6696 /* If INNER is a right shift of a constant and it plus BITNUM does
6697 not overflow, adjust BITNUM and INNER. */
6698 if (TREE_CODE (inner) == RSHIFT_EXPR
6699 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6700 && bitnum < TYPE_PRECISION (type)
6701 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6702 TYPE_PRECISION (type) - bitnum))
6704 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6705 inner = TREE_OPERAND (inner, 0);
6708 /* If we are going to be able to omit the AND below, we must do our
6709 operations as unsigned. If we must use the AND, we have a choice.
6710 Normally unsigned is faster, but for some machines signed is. */
6711 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6712 && !flag_syntax_only) ? 0 : 1;
6714 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6715 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6716 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6717 inner = fold_convert_loc (loc, intermediate_type, inner);
6719 if (bitnum != 0)
6720 inner = build2 (RSHIFT_EXPR, intermediate_type,
6721 inner, size_int (bitnum));
6723 one = build_int_cst (intermediate_type, 1);
6725 if (code == EQ_EXPR)
6726 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6728 /* Put the AND last so it can combine with more things. */
6729 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6731 /* Make sure to return the proper type. */
6732 inner = fold_convert_loc (loc, result_type, inner);
6734 return inner;
6736 return NULL_TREE;
6739 /* Test whether it is preferable two swap two operands, ARG0 and
6740 ARG1, for example because ARG0 is an integer constant and ARG1
6741 isn't. */
6743 bool
6744 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6746 if (CONSTANT_CLASS_P (arg1))
6747 return 0;
6748 if (CONSTANT_CLASS_P (arg0))
6749 return 1;
6751 STRIP_NOPS (arg0);
6752 STRIP_NOPS (arg1);
6754 if (TREE_CONSTANT (arg1))
6755 return 0;
6756 if (TREE_CONSTANT (arg0))
6757 return 1;
6759 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6760 for commutative and comparison operators. Ensuring a canonical
6761 form allows the optimizers to find additional redundancies without
6762 having to explicitly check for both orderings. */
6763 if (TREE_CODE (arg0) == SSA_NAME
6764 && TREE_CODE (arg1) == SSA_NAME
6765 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6766 return 1;
6768 /* Put SSA_NAMEs last. */
6769 if (TREE_CODE (arg1) == SSA_NAME)
6770 return 0;
6771 if (TREE_CODE (arg0) == SSA_NAME)
6772 return 1;
6774 /* Put variables last. */
6775 if (DECL_P (arg1))
6776 return 0;
6777 if (DECL_P (arg0))
6778 return 1;
6780 return 0;
6784 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6785 means A >= Y && A != MAX, but in this case we know that
6786 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6788 static tree
6789 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6791 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6793 if (TREE_CODE (bound) == LT_EXPR)
6794 a = TREE_OPERAND (bound, 0);
6795 else if (TREE_CODE (bound) == GT_EXPR)
6796 a = TREE_OPERAND (bound, 1);
6797 else
6798 return NULL_TREE;
6800 typea = TREE_TYPE (a);
6801 if (!INTEGRAL_TYPE_P (typea)
6802 && !POINTER_TYPE_P (typea))
6803 return NULL_TREE;
6805 if (TREE_CODE (ineq) == LT_EXPR)
6807 a1 = TREE_OPERAND (ineq, 1);
6808 y = TREE_OPERAND (ineq, 0);
6810 else if (TREE_CODE (ineq) == GT_EXPR)
6812 a1 = TREE_OPERAND (ineq, 0);
6813 y = TREE_OPERAND (ineq, 1);
6815 else
6816 return NULL_TREE;
6818 if (TREE_TYPE (a1) != typea)
6819 return NULL_TREE;
6821 if (POINTER_TYPE_P (typea))
6823 /* Convert the pointer types into integer before taking the difference. */
6824 tree ta = fold_convert_loc (loc, ssizetype, a);
6825 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6826 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6828 else
6829 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6831 if (!diff || !integer_onep (diff))
6832 return NULL_TREE;
6834 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6837 /* Fold a sum or difference of at least one multiplication.
6838 Returns the folded tree or NULL if no simplification could be made. */
6840 static tree
6841 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6842 tree arg0, tree arg1)
6844 tree arg00, arg01, arg10, arg11;
6845 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6847 /* (A * C) +- (B * C) -> (A+-B) * C.
6848 (A * C) +- A -> A * (C+-1).
6849 We are most concerned about the case where C is a constant,
6850 but other combinations show up during loop reduction. Since
6851 it is not difficult, try all four possibilities. */
6853 if (TREE_CODE (arg0) == MULT_EXPR)
6855 arg00 = TREE_OPERAND (arg0, 0);
6856 arg01 = TREE_OPERAND (arg0, 1);
6858 else if (TREE_CODE (arg0) == INTEGER_CST)
6860 arg00 = build_one_cst (type);
6861 arg01 = arg0;
6863 else
6865 /* We cannot generate constant 1 for fract. */
6866 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6867 return NULL_TREE;
6868 arg00 = arg0;
6869 arg01 = build_one_cst (type);
6871 if (TREE_CODE (arg1) == MULT_EXPR)
6873 arg10 = TREE_OPERAND (arg1, 0);
6874 arg11 = TREE_OPERAND (arg1, 1);
6876 else if (TREE_CODE (arg1) == INTEGER_CST)
6878 arg10 = build_one_cst (type);
6879 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6880 the purpose of this canonicalization. */
6881 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
6882 && negate_expr_p (arg1)
6883 && code == PLUS_EXPR)
6885 arg11 = negate_expr (arg1);
6886 code = MINUS_EXPR;
6888 else
6889 arg11 = arg1;
6891 else
6893 /* We cannot generate constant 1 for fract. */
6894 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6895 return NULL_TREE;
6896 arg10 = arg1;
6897 arg11 = build_one_cst (type);
6899 same = NULL_TREE;
6901 /* Prefer factoring a common non-constant. */
6902 if (operand_equal_p (arg00, arg10, 0))
6903 same = arg00, alt0 = arg01, alt1 = arg11;
6904 else if (operand_equal_p (arg01, arg11, 0))
6905 same = arg01, alt0 = arg00, alt1 = arg10;
6906 else if (operand_equal_p (arg00, arg11, 0))
6907 same = arg00, alt0 = arg01, alt1 = arg10;
6908 else if (operand_equal_p (arg01, arg10, 0))
6909 same = arg01, alt0 = arg00, alt1 = arg11;
6911 /* No identical multiplicands; see if we can find a common
6912 power-of-two factor in non-power-of-two multiplies. This
6913 can help in multi-dimensional array access. */
6914 else if (tree_fits_shwi_p (arg01)
6915 && tree_fits_shwi_p (arg11))
6917 HOST_WIDE_INT int01, int11, tmp;
6918 bool swap = false;
6919 tree maybe_same;
6920 int01 = tree_to_shwi (arg01);
6921 int11 = tree_to_shwi (arg11);
6923 /* Move min of absolute values to int11. */
6924 if (absu_hwi (int01) < absu_hwi (int11))
6926 tmp = int01, int01 = int11, int11 = tmp;
6927 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6928 maybe_same = arg01;
6929 swap = true;
6931 else
6932 maybe_same = arg11;
6934 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6935 /* The remainder should not be a constant, otherwise we
6936 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6937 increased the number of multiplications necessary. */
6938 && TREE_CODE (arg10) != INTEGER_CST)
6940 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6941 build_int_cst (TREE_TYPE (arg00),
6942 int01 / int11));
6943 alt1 = arg10;
6944 same = maybe_same;
6945 if (swap)
6946 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6950 if (!same)
6951 return NULL_TREE;
6953 if (! INTEGRAL_TYPE_P (type)
6954 || TYPE_OVERFLOW_WRAPS (type)
6955 /* We are neither factoring zero nor minus one. */
6956 || TREE_CODE (same) == INTEGER_CST)
6957 return fold_build2_loc (loc, MULT_EXPR, type,
6958 fold_build2_loc (loc, code, type,
6959 fold_convert_loc (loc, type, alt0),
6960 fold_convert_loc (loc, type, alt1)),
6961 fold_convert_loc (loc, type, same));
6963 /* Same may be zero and thus the operation 'code' may overflow. Likewise
6964 same may be minus one and thus the multiplication may overflow. Perform
6965 the operations in an unsigned type. */
6966 tree utype = unsigned_type_for (type);
6967 tree tem = fold_build2_loc (loc, code, utype,
6968 fold_convert_loc (loc, utype, alt0),
6969 fold_convert_loc (loc, utype, alt1));
6970 /* If the sum evaluated to a constant that is not -INF the multiplication
6971 cannot overflow. */
6972 if (TREE_CODE (tem) == INTEGER_CST
6973 && (wi::to_wide (tem)
6974 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
6975 return fold_build2_loc (loc, MULT_EXPR, type,
6976 fold_convert (type, tem), same);
6978 return fold_convert_loc (loc, type,
6979 fold_build2_loc (loc, MULT_EXPR, utype, tem,
6980 fold_convert_loc (loc, utype, same)));
6983 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6984 specified by EXPR into the buffer PTR of length LEN bytes.
6985 Return the number of bytes placed in the buffer, or zero
6986 upon failure. */
6988 static int
6989 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6991 tree type = TREE_TYPE (expr);
6992 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
6993 int byte, offset, word, words;
6994 unsigned char value;
6996 if ((off == -1 && total_bytes > len) || off >= total_bytes)
6997 return 0;
6998 if (off == -1)
6999 off = 0;
7001 if (ptr == NULL)
7002 /* Dry run. */
7003 return MIN (len, total_bytes - off);
7005 words = total_bytes / UNITS_PER_WORD;
7007 for (byte = 0; byte < total_bytes; byte++)
7009 int bitpos = byte * BITS_PER_UNIT;
7010 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7011 number of bytes. */
7012 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7014 if (total_bytes > UNITS_PER_WORD)
7016 word = byte / UNITS_PER_WORD;
7017 if (WORDS_BIG_ENDIAN)
7018 word = (words - 1) - word;
7019 offset = word * UNITS_PER_WORD;
7020 if (BYTES_BIG_ENDIAN)
7021 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7022 else
7023 offset += byte % UNITS_PER_WORD;
7025 else
7026 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7027 if (offset >= off && offset - off < len)
7028 ptr[offset - off] = value;
7030 return MIN (len, total_bytes - off);
7034 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7035 specified by EXPR into the buffer PTR of length LEN bytes.
7036 Return the number of bytes placed in the buffer, or zero
7037 upon failure. */
7039 static int
7040 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7042 tree type = TREE_TYPE (expr);
7043 scalar_mode mode = SCALAR_TYPE_MODE (type);
7044 int total_bytes = GET_MODE_SIZE (mode);
7045 FIXED_VALUE_TYPE value;
7046 tree i_value, i_type;
7048 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7049 return 0;
7051 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7053 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7054 return 0;
7056 value = TREE_FIXED_CST (expr);
7057 i_value = double_int_to_tree (i_type, value.data);
7059 return native_encode_int (i_value, ptr, len, off);
7063 /* Subroutine of native_encode_expr. Encode the REAL_CST
7064 specified by EXPR into the buffer PTR of length LEN bytes.
7065 Return the number of bytes placed in the buffer, or zero
7066 upon failure. */
7068 static int
7069 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7071 tree type = TREE_TYPE (expr);
7072 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7073 int byte, offset, word, words, bitpos;
7074 unsigned char value;
7076 /* There are always 32 bits in each long, no matter the size of
7077 the hosts long. We handle floating point representations with
7078 up to 192 bits. */
7079 long tmp[6];
7081 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7082 return 0;
7083 if (off == -1)
7084 off = 0;
7086 if (ptr == NULL)
7087 /* Dry run. */
7088 return MIN (len, total_bytes - off);
7090 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7092 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7094 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7095 bitpos += BITS_PER_UNIT)
7097 byte = (bitpos / BITS_PER_UNIT) & 3;
7098 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7100 if (UNITS_PER_WORD < 4)
7102 word = byte / UNITS_PER_WORD;
7103 if (WORDS_BIG_ENDIAN)
7104 word = (words - 1) - word;
7105 offset = word * UNITS_PER_WORD;
7106 if (BYTES_BIG_ENDIAN)
7107 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7108 else
7109 offset += byte % UNITS_PER_WORD;
7111 else
7113 offset = byte;
7114 if (BYTES_BIG_ENDIAN)
7116 /* Reverse bytes within each long, or within the entire float
7117 if it's smaller than a long (for HFmode). */
7118 offset = MIN (3, total_bytes - 1) - offset;
7119 gcc_assert (offset >= 0);
7122 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7123 if (offset >= off
7124 && offset - off < len)
7125 ptr[offset - off] = value;
7127 return MIN (len, total_bytes - off);
7130 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7131 specified by EXPR into the buffer PTR of length LEN bytes.
7132 Return the number of bytes placed in the buffer, or zero
7133 upon failure. */
7135 static int
7136 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7138 int rsize, isize;
7139 tree part;
7141 part = TREE_REALPART (expr);
7142 rsize = native_encode_expr (part, ptr, len, off);
7143 if (off == -1 && rsize == 0)
7144 return 0;
7145 part = TREE_IMAGPART (expr);
7146 if (off != -1)
7147 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7148 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7149 len - rsize, off);
7150 if (off == -1 && isize != rsize)
7151 return 0;
7152 return rsize + isize;
7156 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7157 specified by EXPR into the buffer PTR of length LEN bytes.
7158 Return the number of bytes placed in the buffer, or zero
7159 upon failure. */
7161 static int
7162 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7164 unsigned i, count;
7165 int size, offset;
7166 tree itype, elem;
7168 offset = 0;
7169 count = VECTOR_CST_NELTS (expr);
7170 itype = TREE_TYPE (TREE_TYPE (expr));
7171 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7172 for (i = 0; i < count; i++)
7174 if (off >= size)
7176 off -= size;
7177 continue;
7179 elem = VECTOR_CST_ELT (expr, i);
7180 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7181 len - offset, off);
7182 if ((off == -1 && res != size) || res == 0)
7183 return 0;
7184 offset += res;
7185 if (offset >= len)
7186 return offset;
7187 if (off != -1)
7188 off = 0;
7190 return offset;
7194 /* Subroutine of native_encode_expr. Encode the STRING_CST
7195 specified by EXPR into the buffer PTR of length LEN bytes.
7196 Return the number of bytes placed in the buffer, or zero
7197 upon failure. */
7199 static int
7200 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7202 tree type = TREE_TYPE (expr);
7204 /* Wide-char strings are encoded in target byte-order so native
7205 encoding them is trivial. */
7206 if (BITS_PER_UNIT != CHAR_BIT
7207 || TREE_CODE (type) != ARRAY_TYPE
7208 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7209 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7210 return 0;
7212 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7213 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7214 return 0;
7215 if (off == -1)
7216 off = 0;
7217 if (ptr == NULL)
7218 /* Dry run. */;
7219 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7221 int written = 0;
7222 if (off < TREE_STRING_LENGTH (expr))
7224 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7225 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7227 memset (ptr + written, 0,
7228 MIN (total_bytes - written, len - written));
7230 else
7231 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7232 return MIN (total_bytes - off, len);
7236 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7237 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7238 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7239 anything, just do a dry run. If OFF is not -1 then start
7240 the encoding at byte offset OFF and encode at most LEN bytes.
7241 Return the number of bytes placed in the buffer, or zero upon failure. */
7244 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7246 /* We don't support starting at negative offset and -1 is special. */
7247 if (off < -1)
7248 return 0;
7250 switch (TREE_CODE (expr))
7252 case INTEGER_CST:
7253 return native_encode_int (expr, ptr, len, off);
7255 case REAL_CST:
7256 return native_encode_real (expr, ptr, len, off);
7258 case FIXED_CST:
7259 return native_encode_fixed (expr, ptr, len, off);
7261 case COMPLEX_CST:
7262 return native_encode_complex (expr, ptr, len, off);
7264 case VECTOR_CST:
7265 return native_encode_vector (expr, ptr, len, off);
7267 case STRING_CST:
7268 return native_encode_string (expr, ptr, len, off);
7270 default:
7271 return 0;
7276 /* Subroutine of native_interpret_expr. Interpret the contents of
7277 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7278 If the buffer cannot be interpreted, return NULL_TREE. */
7280 static tree
7281 native_interpret_int (tree type, const unsigned char *ptr, int len)
7283 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7285 if (total_bytes > len
7286 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7287 return NULL_TREE;
7289 wide_int result = wi::from_buffer (ptr, total_bytes);
7291 return wide_int_to_tree (type, result);
7295 /* Subroutine of native_interpret_expr. Interpret the contents of
7296 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7297 If the buffer cannot be interpreted, return NULL_TREE. */
7299 static tree
7300 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7302 scalar_mode mode = SCALAR_TYPE_MODE (type);
7303 int total_bytes = GET_MODE_SIZE (mode);
7304 double_int result;
7305 FIXED_VALUE_TYPE fixed_value;
7307 if (total_bytes > len
7308 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7309 return NULL_TREE;
7311 result = double_int::from_buffer (ptr, total_bytes);
7312 fixed_value = fixed_from_double_int (result, mode);
7314 return build_fixed (type, fixed_value);
7318 /* Subroutine of native_interpret_expr. Interpret the contents of
7319 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7320 If the buffer cannot be interpreted, return NULL_TREE. */
7322 static tree
7323 native_interpret_real (tree type, const unsigned char *ptr, int len)
7325 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7326 int total_bytes = GET_MODE_SIZE (mode);
7327 unsigned char value;
7328 /* There are always 32 bits in each long, no matter the size of
7329 the hosts long. We handle floating point representations with
7330 up to 192 bits. */
7331 REAL_VALUE_TYPE r;
7332 long tmp[6];
7334 if (total_bytes > len || total_bytes > 24)
7335 return NULL_TREE;
7336 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7338 memset (tmp, 0, sizeof (tmp));
7339 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7340 bitpos += BITS_PER_UNIT)
7342 /* Both OFFSET and BYTE index within a long;
7343 bitpos indexes the whole float. */
7344 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7345 if (UNITS_PER_WORD < 4)
7347 int word = byte / UNITS_PER_WORD;
7348 if (WORDS_BIG_ENDIAN)
7349 word = (words - 1) - word;
7350 offset = word * UNITS_PER_WORD;
7351 if (BYTES_BIG_ENDIAN)
7352 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7353 else
7354 offset += byte % UNITS_PER_WORD;
7356 else
7358 offset = byte;
7359 if (BYTES_BIG_ENDIAN)
7361 /* Reverse bytes within each long, or within the entire float
7362 if it's smaller than a long (for HFmode). */
7363 offset = MIN (3, total_bytes - 1) - offset;
7364 gcc_assert (offset >= 0);
7367 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7369 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7372 real_from_target (&r, tmp, mode);
7373 return build_real (type, r);
7377 /* Subroutine of native_interpret_expr. Interpret the contents of
7378 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7379 If the buffer cannot be interpreted, return NULL_TREE. */
7381 static tree
7382 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7384 tree etype, rpart, ipart;
7385 int size;
7387 etype = TREE_TYPE (type);
7388 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7389 if (size * 2 > len)
7390 return NULL_TREE;
7391 rpart = native_interpret_expr (etype, ptr, size);
7392 if (!rpart)
7393 return NULL_TREE;
7394 ipart = native_interpret_expr (etype, ptr+size, size);
7395 if (!ipart)
7396 return NULL_TREE;
7397 return build_complex (type, rpart, ipart);
7401 /* Subroutine of native_interpret_expr. Interpret the contents of
7402 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7403 If the buffer cannot be interpreted, return NULL_TREE. */
7405 static tree
7406 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7408 tree etype, elem;
7409 int i, size, count;
7411 etype = TREE_TYPE (type);
7412 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7413 count = TYPE_VECTOR_SUBPARTS (type);
7414 if (size * count > len)
7415 return NULL_TREE;
7417 auto_vec<tree, 32> elements (count);
7418 for (i = 0; i < count; ++i)
7420 elem = native_interpret_expr (etype, ptr+(i*size), size);
7421 if (!elem)
7422 return NULL_TREE;
7423 elements.quick_push (elem);
7425 return build_vector (type, elements);
7429 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7430 the buffer PTR of length LEN as a constant of type TYPE. For
7431 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7432 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7433 return NULL_TREE. */
7435 tree
7436 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7438 switch (TREE_CODE (type))
7440 case INTEGER_TYPE:
7441 case ENUMERAL_TYPE:
7442 case BOOLEAN_TYPE:
7443 case POINTER_TYPE:
7444 case REFERENCE_TYPE:
7445 return native_interpret_int (type, ptr, len);
7447 case REAL_TYPE:
7448 return native_interpret_real (type, ptr, len);
7450 case FIXED_POINT_TYPE:
7451 return native_interpret_fixed (type, ptr, len);
7453 case COMPLEX_TYPE:
7454 return native_interpret_complex (type, ptr, len);
7456 case VECTOR_TYPE:
7457 return native_interpret_vector (type, ptr, len);
7459 default:
7460 return NULL_TREE;
7464 /* Returns true if we can interpret the contents of a native encoding
7465 as TYPE. */
7467 static bool
7468 can_native_interpret_type_p (tree type)
7470 switch (TREE_CODE (type))
7472 case INTEGER_TYPE:
7473 case ENUMERAL_TYPE:
7474 case BOOLEAN_TYPE:
7475 case POINTER_TYPE:
7476 case REFERENCE_TYPE:
7477 case FIXED_POINT_TYPE:
7478 case REAL_TYPE:
7479 case COMPLEX_TYPE:
7480 case VECTOR_TYPE:
7481 return true;
7482 default:
7483 return false;
7488 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7489 TYPE at compile-time. If we're unable to perform the conversion
7490 return NULL_TREE. */
7492 static tree
7493 fold_view_convert_expr (tree type, tree expr)
7495 /* We support up to 512-bit values (for V8DFmode). */
7496 unsigned char buffer[64];
7497 int len;
7499 /* Check that the host and target are sane. */
7500 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7501 return NULL_TREE;
7503 len = native_encode_expr (expr, buffer, sizeof (buffer));
7504 if (len == 0)
7505 return NULL_TREE;
7507 return native_interpret_expr (type, buffer, len);
7510 /* Build an expression for the address of T. Folds away INDIRECT_REF
7511 to avoid confusing the gimplify process. */
7513 tree
7514 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7516 /* The size of the object is not relevant when talking about its address. */
7517 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7518 t = TREE_OPERAND (t, 0);
7520 if (TREE_CODE (t) == INDIRECT_REF)
7522 t = TREE_OPERAND (t, 0);
7524 if (TREE_TYPE (t) != ptrtype)
7525 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7527 else if (TREE_CODE (t) == MEM_REF
7528 && integer_zerop (TREE_OPERAND (t, 1)))
7529 return TREE_OPERAND (t, 0);
7530 else if (TREE_CODE (t) == MEM_REF
7531 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7532 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7533 TREE_OPERAND (t, 0),
7534 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7535 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7537 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7539 if (TREE_TYPE (t) != ptrtype)
7540 t = fold_convert_loc (loc, ptrtype, t);
7542 else
7543 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7545 return t;
7548 /* Build an expression for the address of T. */
7550 tree
7551 build_fold_addr_expr_loc (location_t loc, tree t)
7553 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7555 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7558 /* Fold a unary expression of code CODE and type TYPE with operand
7559 OP0. Return the folded expression if folding is successful.
7560 Otherwise, return NULL_TREE. */
7562 tree
7563 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7565 tree tem;
7566 tree arg0;
7567 enum tree_code_class kind = TREE_CODE_CLASS (code);
7569 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7570 && TREE_CODE_LENGTH (code) == 1);
7572 arg0 = op0;
7573 if (arg0)
7575 if (CONVERT_EXPR_CODE_P (code)
7576 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7578 /* Don't use STRIP_NOPS, because signedness of argument type
7579 matters. */
7580 STRIP_SIGN_NOPS (arg0);
7582 else
7584 /* Strip any conversions that don't change the mode. This
7585 is safe for every expression, except for a comparison
7586 expression because its signedness is derived from its
7587 operands.
7589 Note that this is done as an internal manipulation within
7590 the constant folder, in order to find the simplest
7591 representation of the arguments so that their form can be
7592 studied. In any cases, the appropriate type conversions
7593 should be put back in the tree that will get out of the
7594 constant folder. */
7595 STRIP_NOPS (arg0);
7598 if (CONSTANT_CLASS_P (arg0))
7600 tree tem = const_unop (code, type, arg0);
7601 if (tem)
7603 if (TREE_TYPE (tem) != type)
7604 tem = fold_convert_loc (loc, type, tem);
7605 return tem;
7610 tem = generic_simplify (loc, code, type, op0);
7611 if (tem)
7612 return tem;
7614 if (TREE_CODE_CLASS (code) == tcc_unary)
7616 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7617 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7618 fold_build1_loc (loc, code, type,
7619 fold_convert_loc (loc, TREE_TYPE (op0),
7620 TREE_OPERAND (arg0, 1))));
7621 else if (TREE_CODE (arg0) == COND_EXPR)
7623 tree arg01 = TREE_OPERAND (arg0, 1);
7624 tree arg02 = TREE_OPERAND (arg0, 2);
7625 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7626 arg01 = fold_build1_loc (loc, code, type,
7627 fold_convert_loc (loc,
7628 TREE_TYPE (op0), arg01));
7629 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7630 arg02 = fold_build1_loc (loc, code, type,
7631 fold_convert_loc (loc,
7632 TREE_TYPE (op0), arg02));
7633 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7634 arg01, arg02);
7636 /* If this was a conversion, and all we did was to move into
7637 inside the COND_EXPR, bring it back out. But leave it if
7638 it is a conversion from integer to integer and the
7639 result precision is no wider than a word since such a
7640 conversion is cheap and may be optimized away by combine,
7641 while it couldn't if it were outside the COND_EXPR. Then return
7642 so we don't get into an infinite recursion loop taking the
7643 conversion out and then back in. */
7645 if ((CONVERT_EXPR_CODE_P (code)
7646 || code == NON_LVALUE_EXPR)
7647 && TREE_CODE (tem) == COND_EXPR
7648 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7649 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7650 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7651 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7652 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7653 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7654 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7655 && (INTEGRAL_TYPE_P
7656 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7657 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7658 || flag_syntax_only))
7659 tem = build1_loc (loc, code, type,
7660 build3 (COND_EXPR,
7661 TREE_TYPE (TREE_OPERAND
7662 (TREE_OPERAND (tem, 1), 0)),
7663 TREE_OPERAND (tem, 0),
7664 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7665 TREE_OPERAND (TREE_OPERAND (tem, 2),
7666 0)));
7667 return tem;
7671 switch (code)
7673 case NON_LVALUE_EXPR:
7674 if (!maybe_lvalue_p (op0))
7675 return fold_convert_loc (loc, type, op0);
7676 return NULL_TREE;
7678 CASE_CONVERT:
7679 case FLOAT_EXPR:
7680 case FIX_TRUNC_EXPR:
7681 if (COMPARISON_CLASS_P (op0))
7683 /* If we have (type) (a CMP b) and type is an integral type, return
7684 new expression involving the new type. Canonicalize
7685 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7686 non-integral type.
7687 Do not fold the result as that would not simplify further, also
7688 folding again results in recursions. */
7689 if (TREE_CODE (type) == BOOLEAN_TYPE)
7690 return build2_loc (loc, TREE_CODE (op0), type,
7691 TREE_OPERAND (op0, 0),
7692 TREE_OPERAND (op0, 1));
7693 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7694 && TREE_CODE (type) != VECTOR_TYPE)
7695 return build3_loc (loc, COND_EXPR, type, op0,
7696 constant_boolean_node (true, type),
7697 constant_boolean_node (false, type));
7700 /* Handle (T *)&A.B.C for A being of type T and B and C
7701 living at offset zero. This occurs frequently in
7702 C++ upcasting and then accessing the base. */
7703 if (TREE_CODE (op0) == ADDR_EXPR
7704 && POINTER_TYPE_P (type)
7705 && handled_component_p (TREE_OPERAND (op0, 0)))
7707 HOST_WIDE_INT bitsize, bitpos;
7708 tree offset;
7709 machine_mode mode;
7710 int unsignedp, reversep, volatilep;
7711 tree base
7712 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7713 &offset, &mode, &unsignedp, &reversep,
7714 &volatilep);
7715 /* If the reference was to a (constant) zero offset, we can use
7716 the address of the base if it has the same base type
7717 as the result type and the pointer type is unqualified. */
7718 if (! offset && bitpos == 0
7719 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7720 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7721 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7722 return fold_convert_loc (loc, type,
7723 build_fold_addr_expr_loc (loc, base));
7726 if (TREE_CODE (op0) == MODIFY_EXPR
7727 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7728 /* Detect assigning a bitfield. */
7729 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7730 && DECL_BIT_FIELD
7731 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7733 /* Don't leave an assignment inside a conversion
7734 unless assigning a bitfield. */
7735 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7736 /* First do the assignment, then return converted constant. */
7737 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7738 TREE_NO_WARNING (tem) = 1;
7739 TREE_USED (tem) = 1;
7740 return tem;
7743 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7744 constants (if x has signed type, the sign bit cannot be set
7745 in c). This folds extension into the BIT_AND_EXPR.
7746 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7747 very likely don't have maximal range for their precision and this
7748 transformation effectively doesn't preserve non-maximal ranges. */
7749 if (TREE_CODE (type) == INTEGER_TYPE
7750 && TREE_CODE (op0) == BIT_AND_EXPR
7751 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7753 tree and_expr = op0;
7754 tree and0 = TREE_OPERAND (and_expr, 0);
7755 tree and1 = TREE_OPERAND (and_expr, 1);
7756 int change = 0;
7758 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7759 || (TYPE_PRECISION (type)
7760 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7761 change = 1;
7762 else if (TYPE_PRECISION (TREE_TYPE (and1))
7763 <= HOST_BITS_PER_WIDE_INT
7764 && tree_fits_uhwi_p (and1))
7766 unsigned HOST_WIDE_INT cst;
7768 cst = tree_to_uhwi (and1);
7769 cst &= HOST_WIDE_INT_M1U
7770 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7771 change = (cst == 0);
7772 if (change
7773 && !flag_syntax_only
7774 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7775 == ZERO_EXTEND))
7777 tree uns = unsigned_type_for (TREE_TYPE (and0));
7778 and0 = fold_convert_loc (loc, uns, and0);
7779 and1 = fold_convert_loc (loc, uns, and1);
7782 if (change)
7784 tem = force_fit_type (type, wi::to_widest (and1), 0,
7785 TREE_OVERFLOW (and1));
7786 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7787 fold_convert_loc (loc, type, and0), tem);
7791 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7792 cast (T1)X will fold away. We assume that this happens when X itself
7793 is a cast. */
7794 if (POINTER_TYPE_P (type)
7795 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7796 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7798 tree arg00 = TREE_OPERAND (arg0, 0);
7799 tree arg01 = TREE_OPERAND (arg0, 1);
7801 return fold_build_pointer_plus_loc
7802 (loc, fold_convert_loc (loc, type, arg00), arg01);
7805 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7806 of the same precision, and X is an integer type not narrower than
7807 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7808 if (INTEGRAL_TYPE_P (type)
7809 && TREE_CODE (op0) == BIT_NOT_EXPR
7810 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7811 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7812 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7814 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7815 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7816 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7817 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7818 fold_convert_loc (loc, type, tem));
7821 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7822 type of X and Y (integer types only). */
7823 if (INTEGRAL_TYPE_P (type)
7824 && TREE_CODE (op0) == MULT_EXPR
7825 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7826 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7828 /* Be careful not to introduce new overflows. */
7829 tree mult_type;
7830 if (TYPE_OVERFLOW_WRAPS (type))
7831 mult_type = type;
7832 else
7833 mult_type = unsigned_type_for (type);
7835 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7837 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7838 fold_convert_loc (loc, mult_type,
7839 TREE_OPERAND (op0, 0)),
7840 fold_convert_loc (loc, mult_type,
7841 TREE_OPERAND (op0, 1)));
7842 return fold_convert_loc (loc, type, tem);
7846 return NULL_TREE;
7848 case VIEW_CONVERT_EXPR:
7849 if (TREE_CODE (op0) == MEM_REF)
7851 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7852 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7853 tem = fold_build2_loc (loc, MEM_REF, type,
7854 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7855 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7856 return tem;
7859 return NULL_TREE;
7861 case NEGATE_EXPR:
7862 tem = fold_negate_expr (loc, arg0);
7863 if (tem)
7864 return fold_convert_loc (loc, type, tem);
7865 return NULL_TREE;
7867 case ABS_EXPR:
7868 /* Convert fabs((double)float) into (double)fabsf(float). */
7869 if (TREE_CODE (arg0) == NOP_EXPR
7870 && TREE_CODE (type) == REAL_TYPE)
7872 tree targ0 = strip_float_extensions (arg0);
7873 if (targ0 != arg0)
7874 return fold_convert_loc (loc, type,
7875 fold_build1_loc (loc, ABS_EXPR,
7876 TREE_TYPE (targ0),
7877 targ0));
7879 return NULL_TREE;
7881 case BIT_NOT_EXPR:
7882 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7883 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7884 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7885 fold_convert_loc (loc, type,
7886 TREE_OPERAND (arg0, 0)))))
7887 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7888 fold_convert_loc (loc, type,
7889 TREE_OPERAND (arg0, 1)));
7890 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7891 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7892 fold_convert_loc (loc, type,
7893 TREE_OPERAND (arg0, 1)))))
7894 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7895 fold_convert_loc (loc, type,
7896 TREE_OPERAND (arg0, 0)), tem);
7898 return NULL_TREE;
7900 case TRUTH_NOT_EXPR:
7901 /* Note that the operand of this must be an int
7902 and its values must be 0 or 1.
7903 ("true" is a fixed value perhaps depending on the language,
7904 but we don't handle values other than 1 correctly yet.) */
7905 tem = fold_truth_not_expr (loc, arg0);
7906 if (!tem)
7907 return NULL_TREE;
7908 return fold_convert_loc (loc, type, tem);
7910 case INDIRECT_REF:
7911 /* Fold *&X to X if X is an lvalue. */
7912 if (TREE_CODE (op0) == ADDR_EXPR)
7914 tree op00 = TREE_OPERAND (op0, 0);
7915 if ((VAR_P (op00)
7916 || TREE_CODE (op00) == PARM_DECL
7917 || TREE_CODE (op00) == RESULT_DECL)
7918 && !TREE_READONLY (op00))
7919 return op00;
7921 return NULL_TREE;
7923 default:
7924 return NULL_TREE;
7925 } /* switch (code) */
7929 /* If the operation was a conversion do _not_ mark a resulting constant
7930 with TREE_OVERFLOW if the original constant was not. These conversions
7931 have implementation defined behavior and retaining the TREE_OVERFLOW
7932 flag here would confuse later passes such as VRP. */
7933 tree
7934 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7935 tree type, tree op0)
7937 tree res = fold_unary_loc (loc, code, type, op0);
7938 if (res
7939 && TREE_CODE (res) == INTEGER_CST
7940 && TREE_CODE (op0) == INTEGER_CST
7941 && CONVERT_EXPR_CODE_P (code))
7942 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7944 return res;
7947 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7948 operands OP0 and OP1. LOC is the location of the resulting expression.
7949 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7950 Return the folded expression if folding is successful. Otherwise,
7951 return NULL_TREE. */
7952 static tree
7953 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7954 tree arg0, tree arg1, tree op0, tree op1)
7956 tree tem;
7958 /* We only do these simplifications if we are optimizing. */
7959 if (!optimize)
7960 return NULL_TREE;
7962 /* Check for things like (A || B) && (A || C). We can convert this
7963 to A || (B && C). Note that either operator can be any of the four
7964 truth and/or operations and the transformation will still be
7965 valid. Also note that we only care about order for the
7966 ANDIF and ORIF operators. If B contains side effects, this
7967 might change the truth-value of A. */
7968 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7969 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7970 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7971 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7972 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7973 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7975 tree a00 = TREE_OPERAND (arg0, 0);
7976 tree a01 = TREE_OPERAND (arg0, 1);
7977 tree a10 = TREE_OPERAND (arg1, 0);
7978 tree a11 = TREE_OPERAND (arg1, 1);
7979 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7980 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7981 && (code == TRUTH_AND_EXPR
7982 || code == TRUTH_OR_EXPR));
7984 if (operand_equal_p (a00, a10, 0))
7985 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7986 fold_build2_loc (loc, code, type, a01, a11));
7987 else if (commutative && operand_equal_p (a00, a11, 0))
7988 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7989 fold_build2_loc (loc, code, type, a01, a10));
7990 else if (commutative && operand_equal_p (a01, a10, 0))
7991 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
7992 fold_build2_loc (loc, code, type, a00, a11));
7994 /* This case if tricky because we must either have commutative
7995 operators or else A10 must not have side-effects. */
7997 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7998 && operand_equal_p (a01, a11, 0))
7999 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8000 fold_build2_loc (loc, code, type, a00, a10),
8001 a01);
8004 /* See if we can build a range comparison. */
8005 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8006 return tem;
8008 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8009 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8011 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8012 if (tem)
8013 return fold_build2_loc (loc, code, type, tem, arg1);
8016 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8017 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8019 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8020 if (tem)
8021 return fold_build2_loc (loc, code, type, arg0, tem);
8024 /* Check for the possibility of merging component references. If our
8025 lhs is another similar operation, try to merge its rhs with our
8026 rhs. Then try to merge our lhs and rhs. */
8027 if (TREE_CODE (arg0) == code
8028 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8029 TREE_OPERAND (arg0, 1), arg1)))
8030 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8032 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8033 return tem;
8035 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8036 && !flag_sanitize_coverage
8037 && (code == TRUTH_AND_EXPR
8038 || code == TRUTH_ANDIF_EXPR
8039 || code == TRUTH_OR_EXPR
8040 || code == TRUTH_ORIF_EXPR))
8042 enum tree_code ncode, icode;
8044 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8045 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8046 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8048 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8049 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8050 We don't want to pack more than two leafs to a non-IF AND/OR
8051 expression.
8052 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8053 equal to IF-CODE, then we don't want to add right-hand operand.
8054 If the inner right-hand side of left-hand operand has
8055 side-effects, or isn't simple, then we can't add to it,
8056 as otherwise we might destroy if-sequence. */
8057 if (TREE_CODE (arg0) == icode
8058 && simple_operand_p_2 (arg1)
8059 /* Needed for sequence points to handle trappings, and
8060 side-effects. */
8061 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8063 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8064 arg1);
8065 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8066 tem);
8068 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8069 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8070 else if (TREE_CODE (arg1) == icode
8071 && simple_operand_p_2 (arg0)
8072 /* Needed for sequence points to handle trappings, and
8073 side-effects. */
8074 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8076 tem = fold_build2_loc (loc, ncode, type,
8077 arg0, TREE_OPERAND (arg1, 0));
8078 return fold_build2_loc (loc, icode, type, tem,
8079 TREE_OPERAND (arg1, 1));
8081 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8082 into (A OR B).
8083 For sequence point consistancy, we need to check for trapping,
8084 and side-effects. */
8085 else if (code == icode && simple_operand_p_2 (arg0)
8086 && simple_operand_p_2 (arg1))
8087 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8090 return NULL_TREE;
8093 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8094 by changing CODE to reduce the magnitude of constants involved in
8095 ARG0 of the comparison.
8096 Returns a canonicalized comparison tree if a simplification was
8097 possible, otherwise returns NULL_TREE.
8098 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8099 valid if signed overflow is undefined. */
8101 static tree
8102 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8103 tree arg0, tree arg1,
8104 bool *strict_overflow_p)
8106 enum tree_code code0 = TREE_CODE (arg0);
8107 tree t, cst0 = NULL_TREE;
8108 int sgn0;
8110 /* Match A +- CST code arg1. We can change this only if overflow
8111 is undefined. */
8112 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8113 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8114 /* In principle pointers also have undefined overflow behavior,
8115 but that causes problems elsewhere. */
8116 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8117 && (code0 == MINUS_EXPR
8118 || code0 == PLUS_EXPR)
8119 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8120 return NULL_TREE;
8122 /* Identify the constant in arg0 and its sign. */
8123 cst0 = TREE_OPERAND (arg0, 1);
8124 sgn0 = tree_int_cst_sgn (cst0);
8126 /* Overflowed constants and zero will cause problems. */
8127 if (integer_zerop (cst0)
8128 || TREE_OVERFLOW (cst0))
8129 return NULL_TREE;
8131 /* See if we can reduce the magnitude of the constant in
8132 arg0 by changing the comparison code. */
8133 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8134 if (code == LT_EXPR
8135 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8136 code = LE_EXPR;
8137 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8138 else if (code == GT_EXPR
8139 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8140 code = GE_EXPR;
8141 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8142 else if (code == LE_EXPR
8143 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8144 code = LT_EXPR;
8145 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8146 else if (code == GE_EXPR
8147 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8148 code = GT_EXPR;
8149 else
8150 return NULL_TREE;
8151 *strict_overflow_p = true;
8153 /* Now build the constant reduced in magnitude. But not if that
8154 would produce one outside of its types range. */
8155 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8156 && ((sgn0 == 1
8157 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8158 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8159 || (sgn0 == -1
8160 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8161 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8162 return NULL_TREE;
8164 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8165 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8166 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8167 t = fold_convert (TREE_TYPE (arg1), t);
8169 return fold_build2_loc (loc, code, type, t, arg1);
8172 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8173 overflow further. Try to decrease the magnitude of constants involved
8174 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8175 and put sole constants at the second argument position.
8176 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8178 static tree
8179 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8180 tree arg0, tree arg1)
8182 tree t;
8183 bool strict_overflow_p;
8184 const char * const warnmsg = G_("assuming signed overflow does not occur "
8185 "when reducing constant in comparison");
8187 /* Try canonicalization by simplifying arg0. */
8188 strict_overflow_p = false;
8189 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8190 &strict_overflow_p);
8191 if (t)
8193 if (strict_overflow_p)
8194 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8195 return t;
8198 /* Try canonicalization by simplifying arg1 using the swapped
8199 comparison. */
8200 code = swap_tree_comparison (code);
8201 strict_overflow_p = false;
8202 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8203 &strict_overflow_p);
8204 if (t && strict_overflow_p)
8205 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8206 return t;
8209 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8210 space. This is used to avoid issuing overflow warnings for
8211 expressions like &p->x which can not wrap. */
8213 static bool
8214 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8216 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8217 return true;
8219 if (bitpos < 0)
8220 return true;
8222 wide_int wi_offset;
8223 int precision = TYPE_PRECISION (TREE_TYPE (base));
8224 if (offset == NULL_TREE)
8225 wi_offset = wi::zero (precision);
8226 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8227 return true;
8228 else
8229 wi_offset = wi::to_wide (offset);
8231 bool overflow;
8232 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8233 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8234 if (overflow)
8235 return true;
8237 if (!wi::fits_uhwi_p (total))
8238 return true;
8240 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8241 if (size <= 0)
8242 return true;
8244 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8245 array. */
8246 if (TREE_CODE (base) == ADDR_EXPR)
8248 HOST_WIDE_INT base_size;
8250 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8251 if (base_size > 0 && size < base_size)
8252 size = base_size;
8255 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8258 /* Return a positive integer when the symbol DECL is known to have
8259 a nonzero address, zero when it's known not to (e.g., it's a weak
8260 symbol), and a negative integer when the symbol is not yet in the
8261 symbol table and so whether or not its address is zero is unknown.
8262 For function local objects always return positive integer. */
8263 static int
8264 maybe_nonzero_address (tree decl)
8266 if (DECL_P (decl) && decl_in_symtab_p (decl))
8267 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8268 return symbol->nonzero_address ();
8270 /* Function local objects are never NULL. */
8271 if (DECL_P (decl)
8272 && (DECL_CONTEXT (decl)
8273 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8274 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8275 return 1;
8277 return -1;
8280 /* Subroutine of fold_binary. This routine performs all of the
8281 transformations that are common to the equality/inequality
8282 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8283 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8284 fold_binary should call fold_binary. Fold a comparison with
8285 tree code CODE and type TYPE with operands OP0 and OP1. Return
8286 the folded comparison or NULL_TREE. */
8288 static tree
8289 fold_comparison (location_t loc, enum tree_code code, tree type,
8290 tree op0, tree op1)
8292 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8293 tree arg0, arg1, tem;
8295 arg0 = op0;
8296 arg1 = op1;
8298 STRIP_SIGN_NOPS (arg0);
8299 STRIP_SIGN_NOPS (arg1);
8301 /* For comparisons of pointers we can decompose it to a compile time
8302 comparison of the base objects and the offsets into the object.
8303 This requires at least one operand being an ADDR_EXPR or a
8304 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8305 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8306 && (TREE_CODE (arg0) == ADDR_EXPR
8307 || TREE_CODE (arg1) == ADDR_EXPR
8308 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8309 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8311 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8312 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8313 machine_mode mode;
8314 int volatilep, reversep, unsignedp;
8315 bool indirect_base0 = false, indirect_base1 = false;
8317 /* Get base and offset for the access. Strip ADDR_EXPR for
8318 get_inner_reference, but put it back by stripping INDIRECT_REF
8319 off the base object if possible. indirect_baseN will be true
8320 if baseN is not an address but refers to the object itself. */
8321 base0 = arg0;
8322 if (TREE_CODE (arg0) == ADDR_EXPR)
8324 base0
8325 = get_inner_reference (TREE_OPERAND (arg0, 0),
8326 &bitsize, &bitpos0, &offset0, &mode,
8327 &unsignedp, &reversep, &volatilep);
8328 if (TREE_CODE (base0) == INDIRECT_REF)
8329 base0 = TREE_OPERAND (base0, 0);
8330 else
8331 indirect_base0 = true;
8333 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8335 base0 = TREE_OPERAND (arg0, 0);
8336 STRIP_SIGN_NOPS (base0);
8337 if (TREE_CODE (base0) == ADDR_EXPR)
8339 base0
8340 = get_inner_reference (TREE_OPERAND (base0, 0),
8341 &bitsize, &bitpos0, &offset0, &mode,
8342 &unsignedp, &reversep, &volatilep);
8343 if (TREE_CODE (base0) == INDIRECT_REF)
8344 base0 = TREE_OPERAND (base0, 0);
8345 else
8346 indirect_base0 = true;
8348 if (offset0 == NULL_TREE || integer_zerop (offset0))
8349 offset0 = TREE_OPERAND (arg0, 1);
8350 else
8351 offset0 = size_binop (PLUS_EXPR, offset0,
8352 TREE_OPERAND (arg0, 1));
8353 if (TREE_CODE (offset0) == INTEGER_CST)
8355 offset_int tem = wi::sext (wi::to_offset (offset0),
8356 TYPE_PRECISION (sizetype));
8357 tem <<= LOG2_BITS_PER_UNIT;
8358 tem += bitpos0;
8359 if (wi::fits_shwi_p (tem))
8361 bitpos0 = tem.to_shwi ();
8362 offset0 = NULL_TREE;
8367 base1 = arg1;
8368 if (TREE_CODE (arg1) == ADDR_EXPR)
8370 base1
8371 = get_inner_reference (TREE_OPERAND (arg1, 0),
8372 &bitsize, &bitpos1, &offset1, &mode,
8373 &unsignedp, &reversep, &volatilep);
8374 if (TREE_CODE (base1) == INDIRECT_REF)
8375 base1 = TREE_OPERAND (base1, 0);
8376 else
8377 indirect_base1 = true;
8379 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8381 base1 = TREE_OPERAND (arg1, 0);
8382 STRIP_SIGN_NOPS (base1);
8383 if (TREE_CODE (base1) == ADDR_EXPR)
8385 base1
8386 = get_inner_reference (TREE_OPERAND (base1, 0),
8387 &bitsize, &bitpos1, &offset1, &mode,
8388 &unsignedp, &reversep, &volatilep);
8389 if (TREE_CODE (base1) == INDIRECT_REF)
8390 base1 = TREE_OPERAND (base1, 0);
8391 else
8392 indirect_base1 = true;
8394 if (offset1 == NULL_TREE || integer_zerop (offset1))
8395 offset1 = TREE_OPERAND (arg1, 1);
8396 else
8397 offset1 = size_binop (PLUS_EXPR, offset1,
8398 TREE_OPERAND (arg1, 1));
8399 if (TREE_CODE (offset1) == INTEGER_CST)
8401 offset_int tem = wi::sext (wi::to_offset (offset1),
8402 TYPE_PRECISION (sizetype));
8403 tem <<= LOG2_BITS_PER_UNIT;
8404 tem += bitpos1;
8405 if (wi::fits_shwi_p (tem))
8407 bitpos1 = tem.to_shwi ();
8408 offset1 = NULL_TREE;
8413 /* If we have equivalent bases we might be able to simplify. */
8414 if (indirect_base0 == indirect_base1
8415 && operand_equal_p (base0, base1,
8416 indirect_base0 ? OEP_ADDRESS_OF : 0))
8418 /* We can fold this expression to a constant if the non-constant
8419 offset parts are equal. */
8420 if (offset0 == offset1
8421 || (offset0 && offset1
8422 && operand_equal_p (offset0, offset1, 0)))
8424 if (!equality_code
8425 && bitpos0 != bitpos1
8426 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8427 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8428 fold_overflow_warning (("assuming pointer wraparound does not "
8429 "occur when comparing P +- C1 with "
8430 "P +- C2"),
8431 WARN_STRICT_OVERFLOW_CONDITIONAL);
8433 switch (code)
8435 case EQ_EXPR:
8436 return constant_boolean_node (bitpos0 == bitpos1, type);
8437 case NE_EXPR:
8438 return constant_boolean_node (bitpos0 != bitpos1, type);
8439 case LT_EXPR:
8440 return constant_boolean_node (bitpos0 < bitpos1, type);
8441 case LE_EXPR:
8442 return constant_boolean_node (bitpos0 <= bitpos1, type);
8443 case GE_EXPR:
8444 return constant_boolean_node (bitpos0 >= bitpos1, type);
8445 case GT_EXPR:
8446 return constant_boolean_node (bitpos0 > bitpos1, type);
8447 default:;
8450 /* We can simplify the comparison to a comparison of the variable
8451 offset parts if the constant offset parts are equal.
8452 Be careful to use signed sizetype here because otherwise we
8453 mess with array offsets in the wrong way. This is possible
8454 because pointer arithmetic is restricted to retain within an
8455 object and overflow on pointer differences is undefined as of
8456 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8457 else if (bitpos0 == bitpos1)
8459 /* By converting to signed sizetype we cover middle-end pointer
8460 arithmetic which operates on unsigned pointer types of size
8461 type size and ARRAY_REF offsets which are properly sign or
8462 zero extended from their type in case it is narrower than
8463 sizetype. */
8464 if (offset0 == NULL_TREE)
8465 offset0 = build_int_cst (ssizetype, 0);
8466 else
8467 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8468 if (offset1 == NULL_TREE)
8469 offset1 = build_int_cst (ssizetype, 0);
8470 else
8471 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8473 if (!equality_code
8474 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8475 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8476 fold_overflow_warning (("assuming pointer wraparound does not "
8477 "occur when comparing P +- C1 with "
8478 "P +- C2"),
8479 WARN_STRICT_OVERFLOW_COMPARISON);
8481 return fold_build2_loc (loc, code, type, offset0, offset1);
8484 /* For equal offsets we can simplify to a comparison of the
8485 base addresses. */
8486 else if (bitpos0 == bitpos1
8487 && (indirect_base0
8488 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8489 && (indirect_base1
8490 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8491 && ((offset0 == offset1)
8492 || (offset0 && offset1
8493 && operand_equal_p (offset0, offset1, 0))))
8495 if (indirect_base0)
8496 base0 = build_fold_addr_expr_loc (loc, base0);
8497 if (indirect_base1)
8498 base1 = build_fold_addr_expr_loc (loc, base1);
8499 return fold_build2_loc (loc, code, type, base0, base1);
8501 /* Comparison between an ordinary (non-weak) symbol and a null
8502 pointer can be eliminated since such symbols must have a non
8503 null address. In C, relational expressions between pointers
8504 to objects and null pointers are undefined. The results
8505 below follow the C++ rules with the additional property that
8506 every object pointer compares greater than a null pointer.
8508 else if (((DECL_P (base0)
8509 && maybe_nonzero_address (base0) > 0
8510 /* Avoid folding references to struct members at offset 0 to
8511 prevent tests like '&ptr->firstmember == 0' from getting
8512 eliminated. When ptr is null, although the -> expression
8513 is strictly speaking invalid, GCC retains it as a matter
8514 of QoI. See PR c/44555. */
8515 && (offset0 == NULL_TREE && bitpos0 != 0))
8516 || CONSTANT_CLASS_P (base0))
8517 && indirect_base0
8518 /* The caller guarantees that when one of the arguments is
8519 constant (i.e., null in this case) it is second. */
8520 && integer_zerop (arg1))
8522 switch (code)
8524 case EQ_EXPR:
8525 case LE_EXPR:
8526 case LT_EXPR:
8527 return constant_boolean_node (false, type);
8528 case GE_EXPR:
8529 case GT_EXPR:
8530 case NE_EXPR:
8531 return constant_boolean_node (true, type);
8532 default:
8533 gcc_unreachable ();
8538 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8539 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8540 the resulting offset is smaller in absolute value than the
8541 original one and has the same sign. */
8542 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8543 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8544 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8545 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8546 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8547 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8548 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8549 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8551 tree const1 = TREE_OPERAND (arg0, 1);
8552 tree const2 = TREE_OPERAND (arg1, 1);
8553 tree variable1 = TREE_OPERAND (arg0, 0);
8554 tree variable2 = TREE_OPERAND (arg1, 0);
8555 tree cst;
8556 const char * const warnmsg = G_("assuming signed overflow does not "
8557 "occur when combining constants around "
8558 "a comparison");
8560 /* Put the constant on the side where it doesn't overflow and is
8561 of lower absolute value and of same sign than before. */
8562 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8563 ? MINUS_EXPR : PLUS_EXPR,
8564 const2, const1);
8565 if (!TREE_OVERFLOW (cst)
8566 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8567 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8569 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8570 return fold_build2_loc (loc, code, type,
8571 variable1,
8572 fold_build2_loc (loc, TREE_CODE (arg1),
8573 TREE_TYPE (arg1),
8574 variable2, cst));
8577 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8578 ? MINUS_EXPR : PLUS_EXPR,
8579 const1, const2);
8580 if (!TREE_OVERFLOW (cst)
8581 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8582 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8584 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8585 return fold_build2_loc (loc, code, type,
8586 fold_build2_loc (loc, TREE_CODE (arg0),
8587 TREE_TYPE (arg0),
8588 variable1, cst),
8589 variable2);
8593 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8594 if (tem)
8595 return tem;
8597 /* If we are comparing an expression that just has comparisons
8598 of two integer values, arithmetic expressions of those comparisons,
8599 and constants, we can simplify it. There are only three cases
8600 to check: the two values can either be equal, the first can be
8601 greater, or the second can be greater. Fold the expression for
8602 those three values. Since each value must be 0 or 1, we have
8603 eight possibilities, each of which corresponds to the constant 0
8604 or 1 or one of the six possible comparisons.
8606 This handles common cases like (a > b) == 0 but also handles
8607 expressions like ((x > y) - (y > x)) > 0, which supposedly
8608 occur in macroized code. */
8610 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8612 tree cval1 = 0, cval2 = 0;
8613 int save_p = 0;
8615 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8616 /* Don't handle degenerate cases here; they should already
8617 have been handled anyway. */
8618 && cval1 != 0 && cval2 != 0
8619 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8620 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8621 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8622 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8623 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8624 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8625 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8627 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8628 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8630 /* We can't just pass T to eval_subst in case cval1 or cval2
8631 was the same as ARG1. */
8633 tree high_result
8634 = fold_build2_loc (loc, code, type,
8635 eval_subst (loc, arg0, cval1, maxval,
8636 cval2, minval),
8637 arg1);
8638 tree equal_result
8639 = fold_build2_loc (loc, code, type,
8640 eval_subst (loc, arg0, cval1, maxval,
8641 cval2, maxval),
8642 arg1);
8643 tree low_result
8644 = fold_build2_loc (loc, code, type,
8645 eval_subst (loc, arg0, cval1, minval,
8646 cval2, maxval),
8647 arg1);
8649 /* All three of these results should be 0 or 1. Confirm they are.
8650 Then use those values to select the proper code to use. */
8652 if (TREE_CODE (high_result) == INTEGER_CST
8653 && TREE_CODE (equal_result) == INTEGER_CST
8654 && TREE_CODE (low_result) == INTEGER_CST)
8656 /* Make a 3-bit mask with the high-order bit being the
8657 value for `>', the next for '=', and the low for '<'. */
8658 switch ((integer_onep (high_result) * 4)
8659 + (integer_onep (equal_result) * 2)
8660 + integer_onep (low_result))
8662 case 0:
8663 /* Always false. */
8664 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8665 case 1:
8666 code = LT_EXPR;
8667 break;
8668 case 2:
8669 code = EQ_EXPR;
8670 break;
8671 case 3:
8672 code = LE_EXPR;
8673 break;
8674 case 4:
8675 code = GT_EXPR;
8676 break;
8677 case 5:
8678 code = NE_EXPR;
8679 break;
8680 case 6:
8681 code = GE_EXPR;
8682 break;
8683 case 7:
8684 /* Always true. */
8685 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8688 if (save_p)
8690 tem = save_expr (build2 (code, type, cval1, cval2));
8691 protected_set_expr_location (tem, loc);
8692 return tem;
8694 return fold_build2_loc (loc, code, type, cval1, cval2);
8699 return NULL_TREE;
8703 /* Subroutine of fold_binary. Optimize complex multiplications of the
8704 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8705 argument EXPR represents the expression "z" of type TYPE. */
8707 static tree
8708 fold_mult_zconjz (location_t loc, tree type, tree expr)
8710 tree itype = TREE_TYPE (type);
8711 tree rpart, ipart, tem;
8713 if (TREE_CODE (expr) == COMPLEX_EXPR)
8715 rpart = TREE_OPERAND (expr, 0);
8716 ipart = TREE_OPERAND (expr, 1);
8718 else if (TREE_CODE (expr) == COMPLEX_CST)
8720 rpart = TREE_REALPART (expr);
8721 ipart = TREE_IMAGPART (expr);
8723 else
8725 expr = save_expr (expr);
8726 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8727 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8730 rpart = save_expr (rpart);
8731 ipart = save_expr (ipart);
8732 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8733 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8734 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8735 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8736 build_zero_cst (itype));
8740 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8741 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8742 true if successful. */
8744 static bool
8745 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8747 unsigned int i;
8749 if (TREE_CODE (arg) == VECTOR_CST)
8751 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8752 elts[i] = VECTOR_CST_ELT (arg, i);
8754 else if (TREE_CODE (arg) == CONSTRUCTOR)
8756 constructor_elt *elt;
8758 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8759 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8760 return false;
8761 else
8762 elts[i] = elt->value;
8764 else
8765 return false;
8766 for (; i < nelts; i++)
8767 elts[i]
8768 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8769 return true;
8772 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8773 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8774 NULL_TREE otherwise. */
8776 static tree
8777 fold_vec_perm (tree type, tree arg0, tree arg1, vec_perm_indices sel)
8779 unsigned int i;
8780 bool need_ctor = false;
8782 unsigned int nelts = sel.length ();
8783 gcc_assert (TYPE_VECTOR_SUBPARTS (type) == nelts
8784 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8785 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8786 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8787 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8788 return NULL_TREE;
8790 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8791 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8792 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8793 return NULL_TREE;
8795 auto_vec<tree, 32> out_elts (nelts);
8796 for (i = 0; i < nelts; i++)
8798 if (!CONSTANT_CLASS_P (in_elts[sel[i]]))
8799 need_ctor = true;
8800 out_elts.quick_push (unshare_expr (in_elts[sel[i]]));
8803 if (need_ctor)
8805 vec<constructor_elt, va_gc> *v;
8806 vec_alloc (v, nelts);
8807 for (i = 0; i < nelts; i++)
8808 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8809 return build_constructor (type, v);
8811 else
8812 return build_vector (type, out_elts);
8815 /* Try to fold a pointer difference of type TYPE two address expressions of
8816 array references AREF0 and AREF1 using location LOC. Return a
8817 simplified expression for the difference or NULL_TREE. */
8819 static tree
8820 fold_addr_of_array_ref_difference (location_t loc, tree type,
8821 tree aref0, tree aref1)
8823 tree base0 = TREE_OPERAND (aref0, 0);
8824 tree base1 = TREE_OPERAND (aref1, 0);
8825 tree base_offset = build_int_cst (type, 0);
8827 /* If the bases are array references as well, recurse. If the bases
8828 are pointer indirections compute the difference of the pointers.
8829 If the bases are equal, we are set. */
8830 if ((TREE_CODE (base0) == ARRAY_REF
8831 && TREE_CODE (base1) == ARRAY_REF
8832 && (base_offset
8833 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8834 || (INDIRECT_REF_P (base0)
8835 && INDIRECT_REF_P (base1)
8836 && (base_offset
8837 = fold_binary_loc (loc, MINUS_EXPR, type,
8838 fold_convert (type, TREE_OPERAND (base0, 0)),
8839 fold_convert (type,
8840 TREE_OPERAND (base1, 0)))))
8841 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8843 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8844 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8845 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8846 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
8847 return fold_build2_loc (loc, PLUS_EXPR, type,
8848 base_offset,
8849 fold_build2_loc (loc, MULT_EXPR, type,
8850 diff, esz));
8852 return NULL_TREE;
8855 /* If the real or vector real constant CST of type TYPE has an exact
8856 inverse, return it, else return NULL. */
8858 tree
8859 exact_inverse (tree type, tree cst)
8861 REAL_VALUE_TYPE r;
8862 tree unit_type;
8863 machine_mode mode;
8864 unsigned vec_nelts, i;
8866 switch (TREE_CODE (cst))
8868 case REAL_CST:
8869 r = TREE_REAL_CST (cst);
8871 if (exact_real_inverse (TYPE_MODE (type), &r))
8872 return build_real (type, r);
8874 return NULL_TREE;
8876 case VECTOR_CST:
8878 vec_nelts = VECTOR_CST_NELTS (cst);
8879 unit_type = TREE_TYPE (type);
8880 mode = TYPE_MODE (unit_type);
8882 auto_vec<tree, 32> elts (vec_nelts);
8883 for (i = 0; i < vec_nelts; i++)
8885 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8886 if (!exact_real_inverse (mode, &r))
8887 return NULL_TREE;
8888 elts.quick_push (build_real (unit_type, r));
8891 return build_vector (type, elts);
8894 default:
8895 return NULL_TREE;
8899 /* Mask out the tz least significant bits of X of type TYPE where
8900 tz is the number of trailing zeroes in Y. */
8901 static wide_int
8902 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8904 int tz = wi::ctz (y);
8905 if (tz > 0)
8906 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8907 return x;
8910 /* Return true when T is an address and is known to be nonzero.
8911 For floating point we further ensure that T is not denormal.
8912 Similar logic is present in nonzero_address in rtlanal.h.
8914 If the return value is based on the assumption that signed overflow
8915 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8916 change *STRICT_OVERFLOW_P. */
8918 static bool
8919 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8921 tree type = TREE_TYPE (t);
8922 enum tree_code code;
8924 /* Doing something useful for floating point would need more work. */
8925 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8926 return false;
8928 code = TREE_CODE (t);
8929 switch (TREE_CODE_CLASS (code))
8931 case tcc_unary:
8932 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8933 strict_overflow_p);
8934 case tcc_binary:
8935 case tcc_comparison:
8936 return tree_binary_nonzero_warnv_p (code, type,
8937 TREE_OPERAND (t, 0),
8938 TREE_OPERAND (t, 1),
8939 strict_overflow_p);
8940 case tcc_constant:
8941 case tcc_declaration:
8942 case tcc_reference:
8943 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8945 default:
8946 break;
8949 switch (code)
8951 case TRUTH_NOT_EXPR:
8952 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8953 strict_overflow_p);
8955 case TRUTH_AND_EXPR:
8956 case TRUTH_OR_EXPR:
8957 case TRUTH_XOR_EXPR:
8958 return tree_binary_nonzero_warnv_p (code, type,
8959 TREE_OPERAND (t, 0),
8960 TREE_OPERAND (t, 1),
8961 strict_overflow_p);
8963 case COND_EXPR:
8964 case CONSTRUCTOR:
8965 case OBJ_TYPE_REF:
8966 case ASSERT_EXPR:
8967 case ADDR_EXPR:
8968 case WITH_SIZE_EXPR:
8969 case SSA_NAME:
8970 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8972 case COMPOUND_EXPR:
8973 case MODIFY_EXPR:
8974 case BIND_EXPR:
8975 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
8976 strict_overflow_p);
8978 case SAVE_EXPR:
8979 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
8980 strict_overflow_p);
8982 case CALL_EXPR:
8984 tree fndecl = get_callee_fndecl (t);
8985 if (!fndecl) return false;
8986 if (flag_delete_null_pointer_checks && !flag_check_new
8987 && DECL_IS_OPERATOR_NEW (fndecl)
8988 && !TREE_NOTHROW (fndecl))
8989 return true;
8990 if (flag_delete_null_pointer_checks
8991 && lookup_attribute ("returns_nonnull",
8992 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
8993 return true;
8994 return alloca_call_p (t);
8997 default:
8998 break;
9000 return false;
9003 /* Return true when T is an address and is known to be nonzero.
9004 Handle warnings about undefined signed overflow. */
9006 bool
9007 tree_expr_nonzero_p (tree t)
9009 bool ret, strict_overflow_p;
9011 strict_overflow_p = false;
9012 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9013 if (strict_overflow_p)
9014 fold_overflow_warning (("assuming signed overflow does not occur when "
9015 "determining that expression is always "
9016 "non-zero"),
9017 WARN_STRICT_OVERFLOW_MISC);
9018 return ret;
9021 /* Return true if T is known not to be equal to an integer W. */
9023 bool
9024 expr_not_equal_to (tree t, const wide_int &w)
9026 wide_int min, max, nz;
9027 value_range_type rtype;
9028 switch (TREE_CODE (t))
9030 case INTEGER_CST:
9031 return wi::to_wide (t) != w;
9033 case SSA_NAME:
9034 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9035 return false;
9036 rtype = get_range_info (t, &min, &max);
9037 if (rtype == VR_RANGE)
9039 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9040 return true;
9041 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9042 return true;
9044 else if (rtype == VR_ANTI_RANGE
9045 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9046 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9047 return true;
9048 /* If T has some known zero bits and W has any of those bits set,
9049 then T is known not to be equal to W. */
9050 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9051 TYPE_PRECISION (TREE_TYPE (t))), 0))
9052 return true;
9053 return false;
9055 default:
9056 return false;
9060 /* Fold a binary expression of code CODE and type TYPE with operands
9061 OP0 and OP1. LOC is the location of the resulting expression.
9062 Return the folded expression if folding is successful. Otherwise,
9063 return NULL_TREE. */
9065 tree
9066 fold_binary_loc (location_t loc,
9067 enum tree_code code, tree type, tree op0, tree op1)
9069 enum tree_code_class kind = TREE_CODE_CLASS (code);
9070 tree arg0, arg1, tem;
9071 tree t1 = NULL_TREE;
9072 bool strict_overflow_p;
9073 unsigned int prec;
9075 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9076 && TREE_CODE_LENGTH (code) == 2
9077 && op0 != NULL_TREE
9078 && op1 != NULL_TREE);
9080 arg0 = op0;
9081 arg1 = op1;
9083 /* Strip any conversions that don't change the mode. This is
9084 safe for every expression, except for a comparison expression
9085 because its signedness is derived from its operands. So, in
9086 the latter case, only strip conversions that don't change the
9087 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9088 preserved.
9090 Note that this is done as an internal manipulation within the
9091 constant folder, in order to find the simplest representation
9092 of the arguments so that their form can be studied. In any
9093 cases, the appropriate type conversions should be put back in
9094 the tree that will get out of the constant folder. */
9096 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9098 STRIP_SIGN_NOPS (arg0);
9099 STRIP_SIGN_NOPS (arg1);
9101 else
9103 STRIP_NOPS (arg0);
9104 STRIP_NOPS (arg1);
9107 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9108 constant but we can't do arithmetic on them. */
9109 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9111 tem = const_binop (code, type, arg0, arg1);
9112 if (tem != NULL_TREE)
9114 if (TREE_TYPE (tem) != type)
9115 tem = fold_convert_loc (loc, type, tem);
9116 return tem;
9120 /* If this is a commutative operation, and ARG0 is a constant, move it
9121 to ARG1 to reduce the number of tests below. */
9122 if (commutative_tree_code (code)
9123 && tree_swap_operands_p (arg0, arg1))
9124 return fold_build2_loc (loc, code, type, op1, op0);
9126 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9127 to ARG1 to reduce the number of tests below. */
9128 if (kind == tcc_comparison
9129 && tree_swap_operands_p (arg0, arg1))
9130 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9132 tem = generic_simplify (loc, code, type, op0, op1);
9133 if (tem)
9134 return tem;
9136 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9138 First check for cases where an arithmetic operation is applied to a
9139 compound, conditional, or comparison operation. Push the arithmetic
9140 operation inside the compound or conditional to see if any folding
9141 can then be done. Convert comparison to conditional for this purpose.
9142 The also optimizes non-constant cases that used to be done in
9143 expand_expr.
9145 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9146 one of the operands is a comparison and the other is a comparison, a
9147 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9148 code below would make the expression more complex. Change it to a
9149 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9150 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9152 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9153 || code == EQ_EXPR || code == NE_EXPR)
9154 && TREE_CODE (type) != VECTOR_TYPE
9155 && ((truth_value_p (TREE_CODE (arg0))
9156 && (truth_value_p (TREE_CODE (arg1))
9157 || (TREE_CODE (arg1) == BIT_AND_EXPR
9158 && integer_onep (TREE_OPERAND (arg1, 1)))))
9159 || (truth_value_p (TREE_CODE (arg1))
9160 && (truth_value_p (TREE_CODE (arg0))
9161 || (TREE_CODE (arg0) == BIT_AND_EXPR
9162 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9164 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9165 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9166 : TRUTH_XOR_EXPR,
9167 boolean_type_node,
9168 fold_convert_loc (loc, boolean_type_node, arg0),
9169 fold_convert_loc (loc, boolean_type_node, arg1));
9171 if (code == EQ_EXPR)
9172 tem = invert_truthvalue_loc (loc, tem);
9174 return fold_convert_loc (loc, type, tem);
9177 if (TREE_CODE_CLASS (code) == tcc_binary
9178 || TREE_CODE_CLASS (code) == tcc_comparison)
9180 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9182 tem = fold_build2_loc (loc, code, type,
9183 fold_convert_loc (loc, TREE_TYPE (op0),
9184 TREE_OPERAND (arg0, 1)), op1);
9185 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9186 tem);
9188 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9190 tem = fold_build2_loc (loc, code, type, op0,
9191 fold_convert_loc (loc, TREE_TYPE (op1),
9192 TREE_OPERAND (arg1, 1)));
9193 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9194 tem);
9197 if (TREE_CODE (arg0) == COND_EXPR
9198 || TREE_CODE (arg0) == VEC_COND_EXPR
9199 || COMPARISON_CLASS_P (arg0))
9201 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9202 arg0, arg1,
9203 /*cond_first_p=*/1);
9204 if (tem != NULL_TREE)
9205 return tem;
9208 if (TREE_CODE (arg1) == COND_EXPR
9209 || TREE_CODE (arg1) == VEC_COND_EXPR
9210 || COMPARISON_CLASS_P (arg1))
9212 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9213 arg1, arg0,
9214 /*cond_first_p=*/0);
9215 if (tem != NULL_TREE)
9216 return tem;
9220 switch (code)
9222 case MEM_REF:
9223 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9224 if (TREE_CODE (arg0) == ADDR_EXPR
9225 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9227 tree iref = TREE_OPERAND (arg0, 0);
9228 return fold_build2 (MEM_REF, type,
9229 TREE_OPERAND (iref, 0),
9230 int_const_binop (PLUS_EXPR, arg1,
9231 TREE_OPERAND (iref, 1)));
9234 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9235 if (TREE_CODE (arg0) == ADDR_EXPR
9236 && handled_component_p (TREE_OPERAND (arg0, 0)))
9238 tree base;
9239 HOST_WIDE_INT coffset;
9240 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9241 &coffset);
9242 if (!base)
9243 return NULL_TREE;
9244 return fold_build2 (MEM_REF, type,
9245 build_fold_addr_expr (base),
9246 int_const_binop (PLUS_EXPR, arg1,
9247 size_int (coffset)));
9250 return NULL_TREE;
9252 case POINTER_PLUS_EXPR:
9253 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9254 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9255 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9256 return fold_convert_loc (loc, type,
9257 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9258 fold_convert_loc (loc, sizetype,
9259 arg1),
9260 fold_convert_loc (loc, sizetype,
9261 arg0)));
9263 return NULL_TREE;
9265 case PLUS_EXPR:
9266 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9268 /* X + (X / CST) * -CST is X % CST. */
9269 if (TREE_CODE (arg1) == MULT_EXPR
9270 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9271 && operand_equal_p (arg0,
9272 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9274 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9275 tree cst1 = TREE_OPERAND (arg1, 1);
9276 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9277 cst1, cst0);
9278 if (sum && integer_zerop (sum))
9279 return fold_convert_loc (loc, type,
9280 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9281 TREE_TYPE (arg0), arg0,
9282 cst0));
9286 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9287 one. Make sure the type is not saturating and has the signedness of
9288 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9289 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9290 if ((TREE_CODE (arg0) == MULT_EXPR
9291 || TREE_CODE (arg1) == MULT_EXPR)
9292 && !TYPE_SATURATING (type)
9293 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9294 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9295 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9297 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9298 if (tem)
9299 return tem;
9302 if (! FLOAT_TYPE_P (type))
9304 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9305 (plus (plus (mult) (mult)) (foo)) so that we can
9306 take advantage of the factoring cases below. */
9307 if (ANY_INTEGRAL_TYPE_P (type)
9308 && TYPE_OVERFLOW_WRAPS (type)
9309 && (((TREE_CODE (arg0) == PLUS_EXPR
9310 || TREE_CODE (arg0) == MINUS_EXPR)
9311 && TREE_CODE (arg1) == MULT_EXPR)
9312 || ((TREE_CODE (arg1) == PLUS_EXPR
9313 || TREE_CODE (arg1) == MINUS_EXPR)
9314 && TREE_CODE (arg0) == MULT_EXPR)))
9316 tree parg0, parg1, parg, marg;
9317 enum tree_code pcode;
9319 if (TREE_CODE (arg1) == MULT_EXPR)
9320 parg = arg0, marg = arg1;
9321 else
9322 parg = arg1, marg = arg0;
9323 pcode = TREE_CODE (parg);
9324 parg0 = TREE_OPERAND (parg, 0);
9325 parg1 = TREE_OPERAND (parg, 1);
9326 STRIP_NOPS (parg0);
9327 STRIP_NOPS (parg1);
9329 if (TREE_CODE (parg0) == MULT_EXPR
9330 && TREE_CODE (parg1) != MULT_EXPR)
9331 return fold_build2_loc (loc, pcode, type,
9332 fold_build2_loc (loc, PLUS_EXPR, type,
9333 fold_convert_loc (loc, type,
9334 parg0),
9335 fold_convert_loc (loc, type,
9336 marg)),
9337 fold_convert_loc (loc, type, parg1));
9338 if (TREE_CODE (parg0) != MULT_EXPR
9339 && TREE_CODE (parg1) == MULT_EXPR)
9340 return
9341 fold_build2_loc (loc, PLUS_EXPR, type,
9342 fold_convert_loc (loc, type, parg0),
9343 fold_build2_loc (loc, pcode, type,
9344 fold_convert_loc (loc, type, marg),
9345 fold_convert_loc (loc, type,
9346 parg1)));
9349 else
9351 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9352 to __complex__ ( x, y ). This is not the same for SNaNs or
9353 if signed zeros are involved. */
9354 if (!HONOR_SNANS (element_mode (arg0))
9355 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9356 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9358 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9359 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9360 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9361 bool arg0rz = false, arg0iz = false;
9362 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9363 || (arg0i && (arg0iz = real_zerop (arg0i))))
9365 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9366 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9367 if (arg0rz && arg1i && real_zerop (arg1i))
9369 tree rp = arg1r ? arg1r
9370 : build1 (REALPART_EXPR, rtype, arg1);
9371 tree ip = arg0i ? arg0i
9372 : build1 (IMAGPART_EXPR, rtype, arg0);
9373 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9375 else if (arg0iz && arg1r && real_zerop (arg1r))
9377 tree rp = arg0r ? arg0r
9378 : build1 (REALPART_EXPR, rtype, arg0);
9379 tree ip = arg1i ? arg1i
9380 : build1 (IMAGPART_EXPR, rtype, arg1);
9381 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9386 if (flag_unsafe_math_optimizations
9387 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9388 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9389 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9390 return tem;
9392 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9393 We associate floats only if the user has specified
9394 -fassociative-math. */
9395 if (flag_associative_math
9396 && TREE_CODE (arg1) == PLUS_EXPR
9397 && TREE_CODE (arg0) != MULT_EXPR)
9399 tree tree10 = TREE_OPERAND (arg1, 0);
9400 tree tree11 = TREE_OPERAND (arg1, 1);
9401 if (TREE_CODE (tree11) == MULT_EXPR
9402 && TREE_CODE (tree10) == MULT_EXPR)
9404 tree tree0;
9405 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9406 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9409 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9410 We associate floats only if the user has specified
9411 -fassociative-math. */
9412 if (flag_associative_math
9413 && TREE_CODE (arg0) == PLUS_EXPR
9414 && TREE_CODE (arg1) != MULT_EXPR)
9416 tree tree00 = TREE_OPERAND (arg0, 0);
9417 tree tree01 = TREE_OPERAND (arg0, 1);
9418 if (TREE_CODE (tree01) == MULT_EXPR
9419 && TREE_CODE (tree00) == MULT_EXPR)
9421 tree tree0;
9422 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9423 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9428 bit_rotate:
9429 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9430 is a rotate of A by C1 bits. */
9431 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9432 is a rotate of A by B bits. */
9434 enum tree_code code0, code1;
9435 tree rtype;
9436 code0 = TREE_CODE (arg0);
9437 code1 = TREE_CODE (arg1);
9438 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9439 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9440 && operand_equal_p (TREE_OPERAND (arg0, 0),
9441 TREE_OPERAND (arg1, 0), 0)
9442 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9443 TYPE_UNSIGNED (rtype))
9444 /* Only create rotates in complete modes. Other cases are not
9445 expanded properly. */
9446 && (element_precision (rtype)
9447 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9449 tree tree01, tree11;
9450 enum tree_code code01, code11;
9452 tree01 = TREE_OPERAND (arg0, 1);
9453 tree11 = TREE_OPERAND (arg1, 1);
9454 STRIP_NOPS (tree01);
9455 STRIP_NOPS (tree11);
9456 code01 = TREE_CODE (tree01);
9457 code11 = TREE_CODE (tree11);
9458 if (code01 == INTEGER_CST
9459 && code11 == INTEGER_CST
9460 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9461 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9463 tem = build2_loc (loc, LROTATE_EXPR,
9464 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9465 TREE_OPERAND (arg0, 0),
9466 code0 == LSHIFT_EXPR
9467 ? TREE_OPERAND (arg0, 1)
9468 : TREE_OPERAND (arg1, 1));
9469 return fold_convert_loc (loc, type, tem);
9471 else if (code11 == MINUS_EXPR)
9473 tree tree110, tree111;
9474 tree110 = TREE_OPERAND (tree11, 0);
9475 tree111 = TREE_OPERAND (tree11, 1);
9476 STRIP_NOPS (tree110);
9477 STRIP_NOPS (tree111);
9478 if (TREE_CODE (tree110) == INTEGER_CST
9479 && 0 == compare_tree_int (tree110,
9480 element_precision
9481 (TREE_TYPE (TREE_OPERAND
9482 (arg0, 0))))
9483 && operand_equal_p (tree01, tree111, 0))
9484 return
9485 fold_convert_loc (loc, type,
9486 build2 ((code0 == LSHIFT_EXPR
9487 ? LROTATE_EXPR
9488 : RROTATE_EXPR),
9489 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9490 TREE_OPERAND (arg0, 0),
9491 TREE_OPERAND (arg0, 1)));
9493 else if (code01 == MINUS_EXPR)
9495 tree tree010, tree011;
9496 tree010 = TREE_OPERAND (tree01, 0);
9497 tree011 = TREE_OPERAND (tree01, 1);
9498 STRIP_NOPS (tree010);
9499 STRIP_NOPS (tree011);
9500 if (TREE_CODE (tree010) == INTEGER_CST
9501 && 0 == compare_tree_int (tree010,
9502 element_precision
9503 (TREE_TYPE (TREE_OPERAND
9504 (arg0, 0))))
9505 && operand_equal_p (tree11, tree011, 0))
9506 return fold_convert_loc
9507 (loc, type,
9508 build2 ((code0 != LSHIFT_EXPR
9509 ? LROTATE_EXPR
9510 : RROTATE_EXPR),
9511 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9512 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9517 associate:
9518 /* In most languages, can't associate operations on floats through
9519 parentheses. Rather than remember where the parentheses were, we
9520 don't associate floats at all, unless the user has specified
9521 -fassociative-math.
9522 And, we need to make sure type is not saturating. */
9524 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9525 && !TYPE_SATURATING (type))
9527 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9528 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9529 tree atype = type;
9530 bool ok = true;
9532 /* Split both trees into variables, constants, and literals. Then
9533 associate each group together, the constants with literals,
9534 then the result with variables. This increases the chances of
9535 literals being recombined later and of generating relocatable
9536 expressions for the sum of a constant and literal. */
9537 var0 = split_tree (arg0, type, code,
9538 &minus_var0, &con0, &minus_con0,
9539 &lit0, &minus_lit0, 0);
9540 var1 = split_tree (arg1, type, code,
9541 &minus_var1, &con1, &minus_con1,
9542 &lit1, &minus_lit1, code == MINUS_EXPR);
9544 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9545 if (code == MINUS_EXPR)
9546 code = PLUS_EXPR;
9548 /* With undefined overflow prefer doing association in a type
9549 which wraps on overflow, if that is one of the operand types. */
9550 if (POINTER_TYPE_P (type)
9551 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9553 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9554 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9555 atype = TREE_TYPE (arg0);
9556 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9557 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9558 atype = TREE_TYPE (arg1);
9559 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9562 /* With undefined overflow we can only associate constants with one
9563 variable, and constants whose association doesn't overflow. */
9564 if (POINTER_TYPE_P (atype)
9565 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9567 if ((var0 && var1) || (minus_var0 && minus_var1))
9569 /* ??? If split_tree would handle NEGATE_EXPR we could
9570 simply reject these cases and the allowed cases would
9571 be the var0/minus_var1 ones. */
9572 tree tmp0 = var0 ? var0 : minus_var0;
9573 tree tmp1 = var1 ? var1 : minus_var1;
9574 bool one_neg = false;
9576 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9578 tmp0 = TREE_OPERAND (tmp0, 0);
9579 one_neg = !one_neg;
9581 if (CONVERT_EXPR_P (tmp0)
9582 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9583 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9584 <= TYPE_PRECISION (atype)))
9585 tmp0 = TREE_OPERAND (tmp0, 0);
9586 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9588 tmp1 = TREE_OPERAND (tmp1, 0);
9589 one_neg = !one_neg;
9591 if (CONVERT_EXPR_P (tmp1)
9592 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9593 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9594 <= TYPE_PRECISION (atype)))
9595 tmp1 = TREE_OPERAND (tmp1, 0);
9596 /* The only case we can still associate with two variables
9597 is if they cancel out. */
9598 if (!one_neg
9599 || !operand_equal_p (tmp0, tmp1, 0))
9600 ok = false;
9602 else if ((var0 && minus_var1
9603 && ! operand_equal_p (var0, minus_var1, 0))
9604 || (minus_var0 && var1
9605 && ! operand_equal_p (minus_var0, var1, 0)))
9606 ok = false;
9609 /* Only do something if we found more than two objects. Otherwise,
9610 nothing has changed and we risk infinite recursion. */
9611 if (ok
9612 && (2 < ((var0 != 0) + (var1 != 0)
9613 + (minus_var0 != 0) + (minus_var1 != 0)
9614 + (con0 != 0) + (con1 != 0)
9615 + (minus_con0 != 0) + (minus_con1 != 0)
9616 + (lit0 != 0) + (lit1 != 0)
9617 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9619 var0 = associate_trees (loc, var0, var1, code, atype);
9620 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9621 code, atype);
9622 con0 = associate_trees (loc, con0, con1, code, atype);
9623 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9624 code, atype);
9625 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9626 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9627 code, atype);
9629 if (minus_var0 && var0)
9631 var0 = associate_trees (loc, var0, minus_var0,
9632 MINUS_EXPR, atype);
9633 minus_var0 = 0;
9635 if (minus_con0 && con0)
9637 con0 = associate_trees (loc, con0, minus_con0,
9638 MINUS_EXPR, atype);
9639 minus_con0 = 0;
9642 /* Preserve the MINUS_EXPR if the negative part of the literal is
9643 greater than the positive part. Otherwise, the multiplicative
9644 folding code (i.e extract_muldiv) may be fooled in case
9645 unsigned constants are subtracted, like in the following
9646 example: ((X*2 + 4) - 8U)/2. */
9647 if (minus_lit0 && lit0)
9649 if (TREE_CODE (lit0) == INTEGER_CST
9650 && TREE_CODE (minus_lit0) == INTEGER_CST
9651 && tree_int_cst_lt (lit0, minus_lit0)
9652 /* But avoid ending up with only negated parts. */
9653 && (var0 || con0))
9655 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9656 MINUS_EXPR, atype);
9657 lit0 = 0;
9659 else
9661 lit0 = associate_trees (loc, lit0, minus_lit0,
9662 MINUS_EXPR, atype);
9663 minus_lit0 = 0;
9667 /* Don't introduce overflows through reassociation. */
9668 if ((lit0 && TREE_OVERFLOW_P (lit0))
9669 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9670 return NULL_TREE;
9672 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9673 con0 = associate_trees (loc, con0, lit0, code, atype);
9674 lit0 = 0;
9675 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9676 code, atype);
9677 minus_lit0 = 0;
9679 /* Eliminate minus_con0. */
9680 if (minus_con0)
9682 if (con0)
9683 con0 = associate_trees (loc, con0, minus_con0,
9684 MINUS_EXPR, atype);
9685 else if (var0)
9686 var0 = associate_trees (loc, var0, minus_con0,
9687 MINUS_EXPR, atype);
9688 else
9689 gcc_unreachable ();
9690 minus_con0 = 0;
9693 /* Eliminate minus_var0. */
9694 if (minus_var0)
9696 if (con0)
9697 con0 = associate_trees (loc, con0, minus_var0,
9698 MINUS_EXPR, atype);
9699 else
9700 gcc_unreachable ();
9701 minus_var0 = 0;
9704 return
9705 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9706 code, atype));
9710 return NULL_TREE;
9712 case MINUS_EXPR:
9713 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9714 if (TREE_CODE (arg0) == NEGATE_EXPR
9715 && negate_expr_p (op1))
9716 return fold_build2_loc (loc, MINUS_EXPR, type,
9717 negate_expr (op1),
9718 fold_convert_loc (loc, type,
9719 TREE_OPERAND (arg0, 0)));
9721 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9722 __complex__ ( x, -y ). This is not the same for SNaNs or if
9723 signed zeros are involved. */
9724 if (!HONOR_SNANS (element_mode (arg0))
9725 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9726 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9728 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9729 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9730 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9731 bool arg0rz = false, arg0iz = false;
9732 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9733 || (arg0i && (arg0iz = real_zerop (arg0i))))
9735 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9736 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9737 if (arg0rz && arg1i && real_zerop (arg1i))
9739 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9740 arg1r ? arg1r
9741 : build1 (REALPART_EXPR, rtype, arg1));
9742 tree ip = arg0i ? arg0i
9743 : build1 (IMAGPART_EXPR, rtype, arg0);
9744 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9746 else if (arg0iz && arg1r && real_zerop (arg1r))
9748 tree rp = arg0r ? arg0r
9749 : build1 (REALPART_EXPR, rtype, arg0);
9750 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9751 arg1i ? arg1i
9752 : build1 (IMAGPART_EXPR, rtype, arg1));
9753 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9758 /* A - B -> A + (-B) if B is easily negatable. */
9759 if (negate_expr_p (op1)
9760 && ! TYPE_OVERFLOW_SANITIZED (type)
9761 && ((FLOAT_TYPE_P (type)
9762 /* Avoid this transformation if B is a positive REAL_CST. */
9763 && (TREE_CODE (op1) != REAL_CST
9764 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9765 || INTEGRAL_TYPE_P (type)))
9766 return fold_build2_loc (loc, PLUS_EXPR, type,
9767 fold_convert_loc (loc, type, arg0),
9768 negate_expr (op1));
9770 /* Fold &a[i] - &a[j] to i-j. */
9771 if (TREE_CODE (arg0) == ADDR_EXPR
9772 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9773 && TREE_CODE (arg1) == ADDR_EXPR
9774 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9776 tree tem = fold_addr_of_array_ref_difference (loc, type,
9777 TREE_OPERAND (arg0, 0),
9778 TREE_OPERAND (arg1, 0));
9779 if (tem)
9780 return tem;
9783 if (FLOAT_TYPE_P (type)
9784 && flag_unsafe_math_optimizations
9785 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9786 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9787 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9788 return tem;
9790 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9791 one. Make sure the type is not saturating and has the signedness of
9792 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9793 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9794 if ((TREE_CODE (arg0) == MULT_EXPR
9795 || TREE_CODE (arg1) == MULT_EXPR)
9796 && !TYPE_SATURATING (type)
9797 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9798 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9799 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9801 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9802 if (tem)
9803 return tem;
9806 goto associate;
9808 case MULT_EXPR:
9809 if (! FLOAT_TYPE_P (type))
9811 /* Transform x * -C into -x * C if x is easily negatable. */
9812 if (TREE_CODE (op1) == INTEGER_CST
9813 && tree_int_cst_sgn (op1) == -1
9814 && negate_expr_p (op0)
9815 && negate_expr_p (op1)
9816 && (tem = negate_expr (op1)) != op1
9817 && ! TREE_OVERFLOW (tem))
9818 return fold_build2_loc (loc, MULT_EXPR, type,
9819 fold_convert_loc (loc, type,
9820 negate_expr (op0)), tem);
9822 strict_overflow_p = false;
9823 if (TREE_CODE (arg1) == INTEGER_CST
9824 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9825 &strict_overflow_p)))
9827 if (strict_overflow_p)
9828 fold_overflow_warning (("assuming signed overflow does not "
9829 "occur when simplifying "
9830 "multiplication"),
9831 WARN_STRICT_OVERFLOW_MISC);
9832 return fold_convert_loc (loc, type, tem);
9835 /* Optimize z * conj(z) for integer complex numbers. */
9836 if (TREE_CODE (arg0) == CONJ_EXPR
9837 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9838 return fold_mult_zconjz (loc, type, arg1);
9839 if (TREE_CODE (arg1) == CONJ_EXPR
9840 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9841 return fold_mult_zconjz (loc, type, arg0);
9843 else
9845 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9846 This is not the same for NaNs or if signed zeros are
9847 involved. */
9848 if (!HONOR_NANS (arg0)
9849 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9850 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9851 && TREE_CODE (arg1) == COMPLEX_CST
9852 && real_zerop (TREE_REALPART (arg1)))
9854 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9855 if (real_onep (TREE_IMAGPART (arg1)))
9856 return
9857 fold_build2_loc (loc, COMPLEX_EXPR, type,
9858 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9859 rtype, arg0)),
9860 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9861 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9862 return
9863 fold_build2_loc (loc, COMPLEX_EXPR, type,
9864 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9865 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9866 rtype, arg0)));
9869 /* Optimize z * conj(z) for floating point complex numbers.
9870 Guarded by flag_unsafe_math_optimizations as non-finite
9871 imaginary components don't produce scalar results. */
9872 if (flag_unsafe_math_optimizations
9873 && TREE_CODE (arg0) == CONJ_EXPR
9874 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9875 return fold_mult_zconjz (loc, type, arg1);
9876 if (flag_unsafe_math_optimizations
9877 && TREE_CODE (arg1) == CONJ_EXPR
9878 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9879 return fold_mult_zconjz (loc, type, arg0);
9881 goto associate;
9883 case BIT_IOR_EXPR:
9884 /* Canonicalize (X & C1) | C2. */
9885 if (TREE_CODE (arg0) == BIT_AND_EXPR
9886 && TREE_CODE (arg1) == INTEGER_CST
9887 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9889 int width = TYPE_PRECISION (type), w;
9890 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
9891 wide_int c2 = wi::to_wide (arg1);
9893 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9894 if ((c1 & c2) == c1)
9895 return omit_one_operand_loc (loc, type, arg1,
9896 TREE_OPERAND (arg0, 0));
9898 wide_int msk = wi::mask (width, false,
9899 TYPE_PRECISION (TREE_TYPE (arg1)));
9901 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9902 if (wi::bit_and_not (msk, c1 | c2) == 0)
9904 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9905 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9908 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9909 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9910 mode which allows further optimizations. */
9911 c1 &= msk;
9912 c2 &= msk;
9913 wide_int c3 = wi::bit_and_not (c1, c2);
9914 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9916 wide_int mask = wi::mask (w, false,
9917 TYPE_PRECISION (type));
9918 if (((c1 | c2) & mask) == mask
9919 && wi::bit_and_not (c1, mask) == 0)
9921 c3 = mask;
9922 break;
9926 if (c3 != c1)
9928 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9929 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
9930 wide_int_to_tree (type, c3));
9931 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
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_XOR_EXPR:
9940 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9941 if (TREE_CODE (arg0) == BIT_AND_EXPR
9942 && INTEGRAL_TYPE_P (type)
9943 && integer_onep (TREE_OPERAND (arg0, 1))
9944 && integer_onep (arg1))
9945 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9946 build_zero_cst (TREE_TYPE (arg0)));
9948 /* See if this can be simplified into a rotate first. If that
9949 is unsuccessful continue in the association code. */
9950 goto bit_rotate;
9952 case BIT_AND_EXPR:
9953 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9954 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9955 && INTEGRAL_TYPE_P (type)
9956 && integer_onep (TREE_OPERAND (arg0, 1))
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 & 1) == 0. */
9968 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9969 && INTEGRAL_TYPE_P (type)
9970 && integer_onep (arg1))
9972 tree tem2;
9973 tem = TREE_OPERAND (arg0, 0);
9974 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9975 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9976 tem, tem2);
9977 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9978 build_zero_cst (TREE_TYPE (tem)));
9980 /* Fold !X & 1 as X == 0. */
9981 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9982 && integer_onep (arg1))
9984 tem = TREE_OPERAND (arg0, 0);
9985 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9986 build_zero_cst (TREE_TYPE (tem)));
9989 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9990 multiple of 1 << CST. */
9991 if (TREE_CODE (arg1) == INTEGER_CST)
9993 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
9994 wide_int ncst1 = -cst1;
9995 if ((cst1 & ncst1) == ncst1
9996 && multiple_of_p (type, arg0,
9997 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
9998 return fold_convert_loc (loc, type, arg0);
10001 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10002 bits from CST2. */
10003 if (TREE_CODE (arg1) == INTEGER_CST
10004 && TREE_CODE (arg0) == MULT_EXPR
10005 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10007 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10008 wide_int masked
10009 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10011 if (masked == 0)
10012 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10013 arg0, arg1);
10014 else if (masked != warg1)
10016 /* Avoid the transform if arg1 is a mask of some
10017 mode which allows further optimizations. */
10018 int pop = wi::popcount (warg1);
10019 if (!(pop >= BITS_PER_UNIT
10020 && pow2p_hwi (pop)
10021 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10022 return fold_build2_loc (loc, code, type, op0,
10023 wide_int_to_tree (type, masked));
10027 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10028 ((A & N) + B) & M -> (A + B) & M
10029 Similarly if (N & M) == 0,
10030 ((A | N) + B) & M -> (A + B) & M
10031 and for - instead of + (or unary - instead of +)
10032 and/or ^ instead of |.
10033 If B is constant and (B & M) == 0, fold into A & M. */
10034 if (TREE_CODE (arg1) == INTEGER_CST)
10036 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10037 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10038 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10039 && (TREE_CODE (arg0) == PLUS_EXPR
10040 || TREE_CODE (arg0) == MINUS_EXPR
10041 || TREE_CODE (arg0) == NEGATE_EXPR)
10042 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10043 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10045 tree pmop[2];
10046 int which = 0;
10047 wide_int cst0;
10049 /* Now we know that arg0 is (C + D) or (C - D) or
10050 -C and arg1 (M) is == (1LL << cst) - 1.
10051 Store C into PMOP[0] and D into PMOP[1]. */
10052 pmop[0] = TREE_OPERAND (arg0, 0);
10053 pmop[1] = NULL;
10054 if (TREE_CODE (arg0) != NEGATE_EXPR)
10056 pmop[1] = TREE_OPERAND (arg0, 1);
10057 which = 1;
10060 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10061 which = -1;
10063 for (; which >= 0; which--)
10064 switch (TREE_CODE (pmop[which]))
10066 case BIT_AND_EXPR:
10067 case BIT_IOR_EXPR:
10068 case BIT_XOR_EXPR:
10069 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10070 != INTEGER_CST)
10071 break;
10072 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10073 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10075 if (cst0 != cst1)
10076 break;
10078 else if (cst0 != 0)
10079 break;
10080 /* If C or D is of the form (A & N) where
10081 (N & M) == M, or of the form (A | N) or
10082 (A ^ N) where (N & M) == 0, replace it with A. */
10083 pmop[which] = TREE_OPERAND (pmop[which], 0);
10084 break;
10085 case INTEGER_CST:
10086 /* If C or D is a N where (N & M) == 0, it can be
10087 omitted (assumed 0). */
10088 if ((TREE_CODE (arg0) == PLUS_EXPR
10089 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10090 && (cst1 & wi::to_wide (pmop[which])) == 0)
10091 pmop[which] = NULL;
10092 break;
10093 default:
10094 break;
10097 /* Only build anything new if we optimized one or both arguments
10098 above. */
10099 if (pmop[0] != TREE_OPERAND (arg0, 0)
10100 || (TREE_CODE (arg0) != NEGATE_EXPR
10101 && pmop[1] != TREE_OPERAND (arg0, 1)))
10103 tree utype = TREE_TYPE (arg0);
10104 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10106 /* Perform the operations in a type that has defined
10107 overflow behavior. */
10108 utype = unsigned_type_for (TREE_TYPE (arg0));
10109 if (pmop[0] != NULL)
10110 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10111 if (pmop[1] != NULL)
10112 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10115 if (TREE_CODE (arg0) == NEGATE_EXPR)
10116 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10117 else if (TREE_CODE (arg0) == PLUS_EXPR)
10119 if (pmop[0] != NULL && pmop[1] != NULL)
10120 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10121 pmop[0], pmop[1]);
10122 else if (pmop[0] != NULL)
10123 tem = pmop[0];
10124 else if (pmop[1] != NULL)
10125 tem = pmop[1];
10126 else
10127 return build_int_cst (type, 0);
10129 else if (pmop[0] == NULL)
10130 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10131 else
10132 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10133 pmop[0], pmop[1]);
10134 /* TEM is now the new binary +, - or unary - replacement. */
10135 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10136 fold_convert_loc (loc, utype, arg1));
10137 return fold_convert_loc (loc, type, tem);
10142 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10143 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10144 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10146 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10148 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10149 if (mask == -1)
10150 return
10151 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10154 goto associate;
10156 case RDIV_EXPR:
10157 /* Don't touch a floating-point divide by zero unless the mode
10158 of the constant can represent infinity. */
10159 if (TREE_CODE (arg1) == REAL_CST
10160 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10161 && real_zerop (arg1))
10162 return NULL_TREE;
10164 /* (-A) / (-B) -> A / B */
10165 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10166 return fold_build2_loc (loc, RDIV_EXPR, type,
10167 TREE_OPERAND (arg0, 0),
10168 negate_expr (arg1));
10169 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10170 return fold_build2_loc (loc, RDIV_EXPR, type,
10171 negate_expr (arg0),
10172 TREE_OPERAND (arg1, 0));
10173 return NULL_TREE;
10175 case TRUNC_DIV_EXPR:
10176 /* Fall through */
10178 case FLOOR_DIV_EXPR:
10179 /* Simplify A / (B << N) where A and B are positive and B is
10180 a power of 2, to A >> (N + log2(B)). */
10181 strict_overflow_p = false;
10182 if (TREE_CODE (arg1) == LSHIFT_EXPR
10183 && (TYPE_UNSIGNED (type)
10184 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10186 tree sval = TREE_OPERAND (arg1, 0);
10187 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10189 tree sh_cnt = TREE_OPERAND (arg1, 1);
10190 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10191 wi::exact_log2 (wi::to_wide (sval)));
10193 if (strict_overflow_p)
10194 fold_overflow_warning (("assuming signed overflow does not "
10195 "occur when simplifying A / (B << N)"),
10196 WARN_STRICT_OVERFLOW_MISC);
10198 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10199 sh_cnt, pow2);
10200 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10201 fold_convert_loc (loc, type, arg0), sh_cnt);
10205 /* Fall through */
10207 case ROUND_DIV_EXPR:
10208 case CEIL_DIV_EXPR:
10209 case EXACT_DIV_EXPR:
10210 if (integer_zerop (arg1))
10211 return NULL_TREE;
10213 /* Convert -A / -B to A / B when the type is signed and overflow is
10214 undefined. */
10215 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10216 && TREE_CODE (op0) == NEGATE_EXPR
10217 && negate_expr_p (op1))
10219 if (INTEGRAL_TYPE_P (type))
10220 fold_overflow_warning (("assuming signed overflow does not occur "
10221 "when distributing negation across "
10222 "division"),
10223 WARN_STRICT_OVERFLOW_MISC);
10224 return fold_build2_loc (loc, code, type,
10225 fold_convert_loc (loc, type,
10226 TREE_OPERAND (arg0, 0)),
10227 negate_expr (op1));
10229 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10230 && TREE_CODE (arg1) == NEGATE_EXPR
10231 && negate_expr_p (op0))
10233 if (INTEGRAL_TYPE_P (type))
10234 fold_overflow_warning (("assuming signed overflow does not occur "
10235 "when distributing negation across "
10236 "division"),
10237 WARN_STRICT_OVERFLOW_MISC);
10238 return fold_build2_loc (loc, code, type,
10239 negate_expr (op0),
10240 fold_convert_loc (loc, type,
10241 TREE_OPERAND (arg1, 0)));
10244 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10245 operation, EXACT_DIV_EXPR.
10247 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10248 At one time others generated faster code, it's not clear if they do
10249 after the last round to changes to the DIV code in expmed.c. */
10250 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10251 && multiple_of_p (type, arg0, arg1))
10252 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10253 fold_convert (type, arg0),
10254 fold_convert (type, arg1));
10256 strict_overflow_p = false;
10257 if (TREE_CODE (arg1) == INTEGER_CST
10258 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10259 &strict_overflow_p)))
10261 if (strict_overflow_p)
10262 fold_overflow_warning (("assuming signed overflow does not occur "
10263 "when simplifying division"),
10264 WARN_STRICT_OVERFLOW_MISC);
10265 return fold_convert_loc (loc, type, tem);
10268 return NULL_TREE;
10270 case CEIL_MOD_EXPR:
10271 case FLOOR_MOD_EXPR:
10272 case ROUND_MOD_EXPR:
10273 case TRUNC_MOD_EXPR:
10274 strict_overflow_p = false;
10275 if (TREE_CODE (arg1) == INTEGER_CST
10276 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10277 &strict_overflow_p)))
10279 if (strict_overflow_p)
10280 fold_overflow_warning (("assuming signed overflow does not occur "
10281 "when simplifying modulus"),
10282 WARN_STRICT_OVERFLOW_MISC);
10283 return fold_convert_loc (loc, type, tem);
10286 return NULL_TREE;
10288 case LROTATE_EXPR:
10289 case RROTATE_EXPR:
10290 case RSHIFT_EXPR:
10291 case LSHIFT_EXPR:
10292 /* Since negative shift count is not well-defined,
10293 don't try to compute it in the compiler. */
10294 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10295 return NULL_TREE;
10297 prec = element_precision (type);
10299 /* If we have a rotate of a bit operation with the rotate count and
10300 the second operand of the bit operation both constant,
10301 permute the two operations. */
10302 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10303 && (TREE_CODE (arg0) == BIT_AND_EXPR
10304 || TREE_CODE (arg0) == BIT_IOR_EXPR
10305 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10306 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10308 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10309 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10310 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10311 fold_build2_loc (loc, code, type,
10312 arg00, arg1),
10313 fold_build2_loc (loc, code, type,
10314 arg01, arg1));
10317 /* Two consecutive rotates adding up to the some integer
10318 multiple of the precision of the type can be ignored. */
10319 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10320 && TREE_CODE (arg0) == RROTATE_EXPR
10321 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10322 && wi::umod_trunc (wi::to_wide (arg1)
10323 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10324 prec) == 0)
10325 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10327 return NULL_TREE;
10329 case MIN_EXPR:
10330 case MAX_EXPR:
10331 goto associate;
10333 case TRUTH_ANDIF_EXPR:
10334 /* Note that the operands of this must be ints
10335 and their values must be 0 or 1.
10336 ("true" is a fixed value perhaps depending on the language.) */
10337 /* If first arg is constant zero, return it. */
10338 if (integer_zerop (arg0))
10339 return fold_convert_loc (loc, type, arg0);
10340 /* FALLTHRU */
10341 case TRUTH_AND_EXPR:
10342 /* If either arg is constant true, drop it. */
10343 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10344 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10345 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10346 /* Preserve sequence points. */
10347 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10348 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10349 /* If second arg is constant zero, result is zero, but first arg
10350 must be evaluated. */
10351 if (integer_zerop (arg1))
10352 return omit_one_operand_loc (loc, type, arg1, arg0);
10353 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10354 case will be handled here. */
10355 if (integer_zerop (arg0))
10356 return omit_one_operand_loc (loc, type, arg0, arg1);
10358 /* !X && X is always false. */
10359 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10360 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10361 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10362 /* X && !X is always false. */
10363 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10364 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10365 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10367 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10368 means A >= Y && A != MAX, but in this case we know that
10369 A < X <= MAX. */
10371 if (!TREE_SIDE_EFFECTS (arg0)
10372 && !TREE_SIDE_EFFECTS (arg1))
10374 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10375 if (tem && !operand_equal_p (tem, arg0, 0))
10376 return fold_build2_loc (loc, code, type, tem, arg1);
10378 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10379 if (tem && !operand_equal_p (tem, arg1, 0))
10380 return fold_build2_loc (loc, code, type, arg0, tem);
10383 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10384 != NULL_TREE)
10385 return tem;
10387 return NULL_TREE;
10389 case TRUTH_ORIF_EXPR:
10390 /* Note that the operands of this must be ints
10391 and their values must be 0 or true.
10392 ("true" is a fixed value perhaps depending on the language.) */
10393 /* If first arg is constant true, return it. */
10394 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10395 return fold_convert_loc (loc, type, arg0);
10396 /* FALLTHRU */
10397 case TRUTH_OR_EXPR:
10398 /* If either arg is constant zero, drop it. */
10399 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10400 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10401 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10402 /* Preserve sequence points. */
10403 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10404 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10405 /* If second arg is constant true, result is true, but we must
10406 evaluate first arg. */
10407 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10408 return omit_one_operand_loc (loc, type, arg1, arg0);
10409 /* Likewise for first arg, but note this only occurs here for
10410 TRUTH_OR_EXPR. */
10411 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10412 return omit_one_operand_loc (loc, type, arg0, arg1);
10414 /* !X || X is always true. */
10415 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10416 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10417 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10418 /* X || !X is always true. */
10419 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10420 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10421 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10423 /* (X && !Y) || (!X && Y) is X ^ Y */
10424 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10425 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10427 tree a0, a1, l0, l1, n0, n1;
10429 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10430 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10432 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10433 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10435 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10436 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10438 if ((operand_equal_p (n0, a0, 0)
10439 && operand_equal_p (n1, a1, 0))
10440 || (operand_equal_p (n0, a1, 0)
10441 && operand_equal_p (n1, a0, 0)))
10442 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10445 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10446 != NULL_TREE)
10447 return tem;
10449 return NULL_TREE;
10451 case TRUTH_XOR_EXPR:
10452 /* If the second arg is constant zero, drop it. */
10453 if (integer_zerop (arg1))
10454 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10455 /* If the second arg is constant true, this is a logical inversion. */
10456 if (integer_onep (arg1))
10458 tem = invert_truthvalue_loc (loc, arg0);
10459 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10461 /* Identical arguments cancel to zero. */
10462 if (operand_equal_p (arg0, arg1, 0))
10463 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10465 /* !X ^ X is always true. */
10466 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10467 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10468 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10470 /* X ^ !X is always true. */
10471 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10472 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10473 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10475 return NULL_TREE;
10477 case EQ_EXPR:
10478 case NE_EXPR:
10479 STRIP_NOPS (arg0);
10480 STRIP_NOPS (arg1);
10482 tem = fold_comparison (loc, code, type, op0, op1);
10483 if (tem != NULL_TREE)
10484 return tem;
10486 /* bool_var != 1 becomes !bool_var. */
10487 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10488 && code == NE_EXPR)
10489 return fold_convert_loc (loc, type,
10490 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10491 TREE_TYPE (arg0), arg0));
10493 /* bool_var == 0 becomes !bool_var. */
10494 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10495 && code == EQ_EXPR)
10496 return fold_convert_loc (loc, type,
10497 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10498 TREE_TYPE (arg0), arg0));
10500 /* !exp != 0 becomes !exp */
10501 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10502 && code == NE_EXPR)
10503 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10505 /* If this is an EQ or NE comparison with zero and ARG0 is
10506 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10507 two operations, but the latter can be done in one less insn
10508 on machines that have only two-operand insns or on which a
10509 constant cannot be the first operand. */
10510 if (TREE_CODE (arg0) == BIT_AND_EXPR
10511 && integer_zerop (arg1))
10513 tree arg00 = TREE_OPERAND (arg0, 0);
10514 tree arg01 = TREE_OPERAND (arg0, 1);
10515 if (TREE_CODE (arg00) == LSHIFT_EXPR
10516 && integer_onep (TREE_OPERAND (arg00, 0)))
10518 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10519 arg01, TREE_OPERAND (arg00, 1));
10520 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10521 build_int_cst (TREE_TYPE (arg0), 1));
10522 return fold_build2_loc (loc, code, type,
10523 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10524 arg1);
10526 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10527 && integer_onep (TREE_OPERAND (arg01, 0)))
10529 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10530 arg00, TREE_OPERAND (arg01, 1));
10531 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10532 build_int_cst (TREE_TYPE (arg0), 1));
10533 return fold_build2_loc (loc, code, type,
10534 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10535 arg1);
10539 /* If this is an NE or EQ comparison of zero against the result of a
10540 signed MOD operation whose second operand is a power of 2, make
10541 the MOD operation unsigned since it is simpler and equivalent. */
10542 if (integer_zerop (arg1)
10543 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10544 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10545 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10546 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10547 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10548 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10550 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10551 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10552 fold_convert_loc (loc, newtype,
10553 TREE_OPERAND (arg0, 0)),
10554 fold_convert_loc (loc, newtype,
10555 TREE_OPERAND (arg0, 1)));
10557 return fold_build2_loc (loc, code, type, newmod,
10558 fold_convert_loc (loc, newtype, arg1));
10561 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10562 C1 is a valid shift constant, and C2 is a power of two, i.e.
10563 a single bit. */
10564 if (TREE_CODE (arg0) == BIT_AND_EXPR
10565 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10566 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10567 == INTEGER_CST
10568 && integer_pow2p (TREE_OPERAND (arg0, 1))
10569 && integer_zerop (arg1))
10571 tree itype = TREE_TYPE (arg0);
10572 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10573 prec = TYPE_PRECISION (itype);
10575 /* Check for a valid shift count. */
10576 if (wi::ltu_p (wi::to_wide (arg001), prec))
10578 tree arg01 = TREE_OPERAND (arg0, 1);
10579 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10580 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10581 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10582 can be rewritten as (X & (C2 << C1)) != 0. */
10583 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10585 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10586 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10587 return fold_build2_loc (loc, code, type, tem,
10588 fold_convert_loc (loc, itype, arg1));
10590 /* Otherwise, for signed (arithmetic) shifts,
10591 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10592 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10593 else if (!TYPE_UNSIGNED (itype))
10594 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10595 arg000, build_int_cst (itype, 0));
10596 /* Otherwise, of unsigned (logical) shifts,
10597 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10598 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10599 else
10600 return omit_one_operand_loc (loc, type,
10601 code == EQ_EXPR ? integer_one_node
10602 : integer_zero_node,
10603 arg000);
10607 /* If this is a comparison of a field, we may be able to simplify it. */
10608 if ((TREE_CODE (arg0) == COMPONENT_REF
10609 || TREE_CODE (arg0) == BIT_FIELD_REF)
10610 /* Handle the constant case even without -O
10611 to make sure the warnings are given. */
10612 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10614 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10615 if (t1)
10616 return t1;
10619 /* Optimize comparisons of strlen vs zero to a compare of the
10620 first character of the string vs zero. To wit,
10621 strlen(ptr) == 0 => *ptr == 0
10622 strlen(ptr) != 0 => *ptr != 0
10623 Other cases should reduce to one of these two (or a constant)
10624 due to the return value of strlen being unsigned. */
10625 if (TREE_CODE (arg0) == CALL_EXPR
10626 && integer_zerop (arg1))
10628 tree fndecl = get_callee_fndecl (arg0);
10630 if (fndecl
10631 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10632 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10633 && call_expr_nargs (arg0) == 1
10634 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10636 tree iref = build_fold_indirect_ref_loc (loc,
10637 CALL_EXPR_ARG (arg0, 0));
10638 return fold_build2_loc (loc, code, type, iref,
10639 build_int_cst (TREE_TYPE (iref), 0));
10643 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10644 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10645 if (TREE_CODE (arg0) == RSHIFT_EXPR
10646 && integer_zerop (arg1)
10647 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10649 tree arg00 = TREE_OPERAND (arg0, 0);
10650 tree arg01 = TREE_OPERAND (arg0, 1);
10651 tree itype = TREE_TYPE (arg00);
10652 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10654 if (TYPE_UNSIGNED (itype))
10656 itype = signed_type_for (itype);
10657 arg00 = fold_convert_loc (loc, itype, arg00);
10659 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10660 type, arg00, build_zero_cst (itype));
10664 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10665 (X & C) == 0 when C is a single bit. */
10666 if (TREE_CODE (arg0) == BIT_AND_EXPR
10667 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10668 && integer_zerop (arg1)
10669 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10671 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10672 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10673 TREE_OPERAND (arg0, 1));
10674 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10675 type, tem,
10676 fold_convert_loc (loc, TREE_TYPE (arg0),
10677 arg1));
10680 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10681 constant C is a power of two, i.e. a single bit. */
10682 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10683 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10684 && integer_zerop (arg1)
10685 && integer_pow2p (TREE_OPERAND (arg0, 1))
10686 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10687 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10689 tree arg00 = TREE_OPERAND (arg0, 0);
10690 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10691 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10694 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10695 when is C is a power of two, i.e. a single bit. */
10696 if (TREE_CODE (arg0) == BIT_AND_EXPR
10697 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10698 && integer_zerop (arg1)
10699 && integer_pow2p (TREE_OPERAND (arg0, 1))
10700 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10701 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10703 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10704 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10705 arg000, TREE_OPERAND (arg0, 1));
10706 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10707 tem, build_int_cst (TREE_TYPE (tem), 0));
10710 if (integer_zerop (arg1)
10711 && tree_expr_nonzero_p (arg0))
10713 tree res = constant_boolean_node (code==NE_EXPR, type);
10714 return omit_one_operand_loc (loc, type, res, arg0);
10717 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10718 if (TREE_CODE (arg0) == BIT_AND_EXPR
10719 && TREE_CODE (arg1) == BIT_AND_EXPR)
10721 tree arg00 = TREE_OPERAND (arg0, 0);
10722 tree arg01 = TREE_OPERAND (arg0, 1);
10723 tree arg10 = TREE_OPERAND (arg1, 0);
10724 tree arg11 = TREE_OPERAND (arg1, 1);
10725 tree itype = TREE_TYPE (arg0);
10727 if (operand_equal_p (arg01, arg11, 0))
10729 tem = fold_convert_loc (loc, itype, arg10);
10730 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10731 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10732 return fold_build2_loc (loc, code, type, tem,
10733 build_zero_cst (itype));
10735 if (operand_equal_p (arg01, arg10, 0))
10737 tem = fold_convert_loc (loc, itype, arg11);
10738 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10739 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10740 return fold_build2_loc (loc, code, type, tem,
10741 build_zero_cst (itype));
10743 if (operand_equal_p (arg00, arg11, 0))
10745 tem = fold_convert_loc (loc, itype, arg10);
10746 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10747 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10748 return fold_build2_loc (loc, code, type, tem,
10749 build_zero_cst (itype));
10751 if (operand_equal_p (arg00, arg10, 0))
10753 tem = fold_convert_loc (loc, itype, arg11);
10754 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10755 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10756 return fold_build2_loc (loc, code, type, tem,
10757 build_zero_cst (itype));
10761 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10762 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10764 tree arg00 = TREE_OPERAND (arg0, 0);
10765 tree arg01 = TREE_OPERAND (arg0, 1);
10766 tree arg10 = TREE_OPERAND (arg1, 0);
10767 tree arg11 = TREE_OPERAND (arg1, 1);
10768 tree itype = TREE_TYPE (arg0);
10770 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10771 operand_equal_p guarantees no side-effects so we don't need
10772 to use omit_one_operand on Z. */
10773 if (operand_equal_p (arg01, arg11, 0))
10774 return fold_build2_loc (loc, code, type, arg00,
10775 fold_convert_loc (loc, TREE_TYPE (arg00),
10776 arg10));
10777 if (operand_equal_p (arg01, arg10, 0))
10778 return fold_build2_loc (loc, code, type, arg00,
10779 fold_convert_loc (loc, TREE_TYPE (arg00),
10780 arg11));
10781 if (operand_equal_p (arg00, arg11, 0))
10782 return fold_build2_loc (loc, code, type, arg01,
10783 fold_convert_loc (loc, TREE_TYPE (arg01),
10784 arg10));
10785 if (operand_equal_p (arg00, arg10, 0))
10786 return fold_build2_loc (loc, code, type, arg01,
10787 fold_convert_loc (loc, TREE_TYPE (arg01),
10788 arg11));
10790 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10791 if (TREE_CODE (arg01) == INTEGER_CST
10792 && TREE_CODE (arg11) == INTEGER_CST)
10794 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10795 fold_convert_loc (loc, itype, arg11));
10796 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10797 return fold_build2_loc (loc, code, type, tem,
10798 fold_convert_loc (loc, itype, arg10));
10802 /* Attempt to simplify equality/inequality comparisons of complex
10803 values. Only lower the comparison if the result is known or
10804 can be simplified to a single scalar comparison. */
10805 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10806 || TREE_CODE (arg0) == COMPLEX_CST)
10807 && (TREE_CODE (arg1) == COMPLEX_EXPR
10808 || TREE_CODE (arg1) == COMPLEX_CST))
10810 tree real0, imag0, real1, imag1;
10811 tree rcond, icond;
10813 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10815 real0 = TREE_OPERAND (arg0, 0);
10816 imag0 = TREE_OPERAND (arg0, 1);
10818 else
10820 real0 = TREE_REALPART (arg0);
10821 imag0 = TREE_IMAGPART (arg0);
10824 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10826 real1 = TREE_OPERAND (arg1, 0);
10827 imag1 = TREE_OPERAND (arg1, 1);
10829 else
10831 real1 = TREE_REALPART (arg1);
10832 imag1 = TREE_IMAGPART (arg1);
10835 rcond = fold_binary_loc (loc, code, type, real0, real1);
10836 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10838 if (integer_zerop (rcond))
10840 if (code == EQ_EXPR)
10841 return omit_two_operands_loc (loc, type, boolean_false_node,
10842 imag0, imag1);
10843 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10845 else
10847 if (code == NE_EXPR)
10848 return omit_two_operands_loc (loc, type, boolean_true_node,
10849 imag0, imag1);
10850 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10854 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10855 if (icond && TREE_CODE (icond) == INTEGER_CST)
10857 if (integer_zerop (icond))
10859 if (code == EQ_EXPR)
10860 return omit_two_operands_loc (loc, type, boolean_false_node,
10861 real0, real1);
10862 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10864 else
10866 if (code == NE_EXPR)
10867 return omit_two_operands_loc (loc, type, boolean_true_node,
10868 real0, real1);
10869 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10874 return NULL_TREE;
10876 case LT_EXPR:
10877 case GT_EXPR:
10878 case LE_EXPR:
10879 case GE_EXPR:
10880 tem = fold_comparison (loc, code, type, op0, op1);
10881 if (tem != NULL_TREE)
10882 return tem;
10884 /* Transform comparisons of the form X +- C CMP X. */
10885 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10886 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10887 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10888 && !HONOR_SNANS (arg0))
10890 tree arg01 = TREE_OPERAND (arg0, 1);
10891 enum tree_code code0 = TREE_CODE (arg0);
10892 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10894 /* (X - c) > X becomes false. */
10895 if (code == GT_EXPR
10896 && ((code0 == MINUS_EXPR && is_positive >= 0)
10897 || (code0 == PLUS_EXPR && is_positive <= 0)))
10898 return constant_boolean_node (0, type);
10900 /* Likewise (X + c) < X becomes false. */
10901 if (code == LT_EXPR
10902 && ((code0 == PLUS_EXPR && is_positive >= 0)
10903 || (code0 == MINUS_EXPR && is_positive <= 0)))
10904 return constant_boolean_node (0, type);
10906 /* Convert (X - c) <= X to true. */
10907 if (!HONOR_NANS (arg1)
10908 && code == LE_EXPR
10909 && ((code0 == MINUS_EXPR && is_positive >= 0)
10910 || (code0 == PLUS_EXPR && is_positive <= 0)))
10911 return constant_boolean_node (1, type);
10913 /* Convert (X + c) >= X to true. */
10914 if (!HONOR_NANS (arg1)
10915 && code == GE_EXPR
10916 && ((code0 == PLUS_EXPR && is_positive >= 0)
10917 || (code0 == MINUS_EXPR && is_positive <= 0)))
10918 return constant_boolean_node (1, type);
10921 /* If we are comparing an ABS_EXPR with a constant, we can
10922 convert all the cases into explicit comparisons, but they may
10923 well not be faster than doing the ABS and one comparison.
10924 But ABS (X) <= C is a range comparison, which becomes a subtraction
10925 and a comparison, and is probably faster. */
10926 if (code == LE_EXPR
10927 && TREE_CODE (arg1) == INTEGER_CST
10928 && TREE_CODE (arg0) == ABS_EXPR
10929 && ! TREE_SIDE_EFFECTS (arg0)
10930 && (0 != (tem = negate_expr (arg1)))
10931 && TREE_CODE (tem) == INTEGER_CST
10932 && !TREE_OVERFLOW (tem))
10933 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
10934 build2 (GE_EXPR, type,
10935 TREE_OPERAND (arg0, 0), tem),
10936 build2 (LE_EXPR, type,
10937 TREE_OPERAND (arg0, 0), arg1));
10939 /* Convert ABS_EXPR<x> >= 0 to true. */
10940 strict_overflow_p = false;
10941 if (code == GE_EXPR
10942 && (integer_zerop (arg1)
10943 || (! HONOR_NANS (arg0)
10944 && real_zerop (arg1)))
10945 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
10947 if (strict_overflow_p)
10948 fold_overflow_warning (("assuming signed overflow does not occur "
10949 "when simplifying comparison of "
10950 "absolute value and zero"),
10951 WARN_STRICT_OVERFLOW_CONDITIONAL);
10952 return omit_one_operand_loc (loc, type,
10953 constant_boolean_node (true, type),
10954 arg0);
10957 /* Convert ABS_EXPR<x> < 0 to false. */
10958 strict_overflow_p = false;
10959 if (code == LT_EXPR
10960 && (integer_zerop (arg1) || real_zerop (arg1))
10961 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
10963 if (strict_overflow_p)
10964 fold_overflow_warning (("assuming signed overflow does not occur "
10965 "when simplifying comparison of "
10966 "absolute value and zero"),
10967 WARN_STRICT_OVERFLOW_CONDITIONAL);
10968 return omit_one_operand_loc (loc, type,
10969 constant_boolean_node (false, type),
10970 arg0);
10973 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
10974 and similarly for >= into !=. */
10975 if ((code == LT_EXPR || code == GE_EXPR)
10976 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10977 && TREE_CODE (arg1) == LSHIFT_EXPR
10978 && integer_onep (TREE_OPERAND (arg1, 0)))
10979 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10980 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10981 TREE_OPERAND (arg1, 1)),
10982 build_zero_cst (TREE_TYPE (arg0)));
10984 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
10985 otherwise Y might be >= # of bits in X's type and thus e.g.
10986 (unsigned char) (1 << Y) for Y 15 might be 0.
10987 If the cast is widening, then 1 << Y should have unsigned type,
10988 otherwise if Y is number of bits in the signed shift type minus 1,
10989 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
10990 31 might be 0xffffffff80000000. */
10991 if ((code == LT_EXPR || code == GE_EXPR)
10992 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10993 && CONVERT_EXPR_P (arg1)
10994 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
10995 && (element_precision (TREE_TYPE (arg1))
10996 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
10997 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
10998 || (element_precision (TREE_TYPE (arg1))
10999 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11000 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11002 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11003 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11004 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11005 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11006 build_zero_cst (TREE_TYPE (arg0)));
11009 return NULL_TREE;
11011 case UNORDERED_EXPR:
11012 case ORDERED_EXPR:
11013 case UNLT_EXPR:
11014 case UNLE_EXPR:
11015 case UNGT_EXPR:
11016 case UNGE_EXPR:
11017 case UNEQ_EXPR:
11018 case LTGT_EXPR:
11019 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11021 tree targ0 = strip_float_extensions (arg0);
11022 tree targ1 = strip_float_extensions (arg1);
11023 tree newtype = TREE_TYPE (targ0);
11025 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11026 newtype = TREE_TYPE (targ1);
11028 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11029 return fold_build2_loc (loc, code, type,
11030 fold_convert_loc (loc, newtype, targ0),
11031 fold_convert_loc (loc, newtype, targ1));
11034 return NULL_TREE;
11036 case COMPOUND_EXPR:
11037 /* When pedantic, a compound expression can be neither an lvalue
11038 nor an integer constant expression. */
11039 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11040 return NULL_TREE;
11041 /* Don't let (0, 0) be null pointer constant. */
11042 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11043 : fold_convert_loc (loc, type, arg1);
11044 return pedantic_non_lvalue_loc (loc, tem);
11046 case ASSERT_EXPR:
11047 /* An ASSERT_EXPR should never be passed to fold_binary. */
11048 gcc_unreachable ();
11050 default:
11051 return NULL_TREE;
11052 } /* switch (code) */
11055 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11056 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11057 of GOTO_EXPR. */
11059 static tree
11060 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11062 switch (TREE_CODE (*tp))
11064 case LABEL_EXPR:
11065 return *tp;
11067 case GOTO_EXPR:
11068 *walk_subtrees = 0;
11070 /* fall through */
11072 default:
11073 return NULL_TREE;
11077 /* Return whether the sub-tree ST contains a label which is accessible from
11078 outside the sub-tree. */
11080 static bool
11081 contains_label_p (tree st)
11083 return
11084 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11087 /* Fold a ternary expression of code CODE and type TYPE with operands
11088 OP0, OP1, and OP2. Return the folded expression if folding is
11089 successful. Otherwise, return NULL_TREE. */
11091 tree
11092 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11093 tree op0, tree op1, tree op2)
11095 tree tem;
11096 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11097 enum tree_code_class kind = TREE_CODE_CLASS (code);
11099 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11100 && TREE_CODE_LENGTH (code) == 3);
11102 /* If this is a commutative operation, and OP0 is a constant, move it
11103 to OP1 to reduce the number of tests below. */
11104 if (commutative_ternary_tree_code (code)
11105 && tree_swap_operands_p (op0, op1))
11106 return fold_build3_loc (loc, code, type, op1, op0, op2);
11108 tem = generic_simplify (loc, code, type, op0, op1, op2);
11109 if (tem)
11110 return tem;
11112 /* Strip any conversions that don't change the mode. This is safe
11113 for every expression, except for a comparison expression because
11114 its signedness is derived from its operands. So, in the latter
11115 case, only strip conversions that don't change the signedness.
11117 Note that this is done as an internal manipulation within the
11118 constant folder, in order to find the simplest representation of
11119 the arguments so that their form can be studied. In any cases,
11120 the appropriate type conversions should be put back in the tree
11121 that will get out of the constant folder. */
11122 if (op0)
11124 arg0 = op0;
11125 STRIP_NOPS (arg0);
11128 if (op1)
11130 arg1 = op1;
11131 STRIP_NOPS (arg1);
11134 if (op2)
11136 arg2 = op2;
11137 STRIP_NOPS (arg2);
11140 switch (code)
11142 case COMPONENT_REF:
11143 if (TREE_CODE (arg0) == CONSTRUCTOR
11144 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11146 unsigned HOST_WIDE_INT idx;
11147 tree field, value;
11148 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11149 if (field == arg1)
11150 return value;
11152 return NULL_TREE;
11154 case COND_EXPR:
11155 case VEC_COND_EXPR:
11156 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11157 so all simple results must be passed through pedantic_non_lvalue. */
11158 if (TREE_CODE (arg0) == INTEGER_CST)
11160 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11161 tem = integer_zerop (arg0) ? op2 : op1;
11162 /* Only optimize constant conditions when the selected branch
11163 has the same type as the COND_EXPR. This avoids optimizing
11164 away "c ? x : throw", where the throw has a void type.
11165 Avoid throwing away that operand which contains label. */
11166 if ((!TREE_SIDE_EFFECTS (unused_op)
11167 || !contains_label_p (unused_op))
11168 && (! VOID_TYPE_P (TREE_TYPE (tem))
11169 || VOID_TYPE_P (type)))
11170 return pedantic_non_lvalue_loc (loc, tem);
11171 return NULL_TREE;
11173 else if (TREE_CODE (arg0) == VECTOR_CST)
11175 if ((TREE_CODE (arg1) == VECTOR_CST
11176 || TREE_CODE (arg1) == CONSTRUCTOR)
11177 && (TREE_CODE (arg2) == VECTOR_CST
11178 || TREE_CODE (arg2) == CONSTRUCTOR))
11180 unsigned int nelts = VECTOR_CST_NELTS (arg0), i;
11181 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11182 auto_vec_perm_indices sel (nelts);
11183 for (i = 0; i < nelts; i++)
11185 tree val = VECTOR_CST_ELT (arg0, i);
11186 if (integer_all_onesp (val))
11187 sel.quick_push (i);
11188 else if (integer_zerop (val))
11189 sel.quick_push (nelts + i);
11190 else /* Currently unreachable. */
11191 return NULL_TREE;
11193 tree t = fold_vec_perm (type, arg1, arg2, sel);
11194 if (t != NULL_TREE)
11195 return t;
11199 /* If we have A op B ? A : C, we may be able to convert this to a
11200 simpler expression, depending on the operation and the values
11201 of B and C. Signed zeros prevent all of these transformations,
11202 for reasons given above each one.
11204 Also try swapping the arguments and inverting the conditional. */
11205 if (COMPARISON_CLASS_P (arg0)
11206 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), arg1)
11207 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11209 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11210 if (tem)
11211 return tem;
11214 if (COMPARISON_CLASS_P (arg0)
11215 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11216 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11218 location_t loc0 = expr_location_or (arg0, loc);
11219 tem = fold_invert_truthvalue (loc0, arg0);
11220 if (tem && COMPARISON_CLASS_P (tem))
11222 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11223 if (tem)
11224 return tem;
11228 /* If the second operand is simpler than the third, swap them
11229 since that produces better jump optimization results. */
11230 if (truth_value_p (TREE_CODE (arg0))
11231 && tree_swap_operands_p (op1, op2))
11233 location_t loc0 = expr_location_or (arg0, loc);
11234 /* See if this can be inverted. If it can't, possibly because
11235 it was a floating-point inequality comparison, don't do
11236 anything. */
11237 tem = fold_invert_truthvalue (loc0, arg0);
11238 if (tem)
11239 return fold_build3_loc (loc, code, type, tem, op2, op1);
11242 /* Convert A ? 1 : 0 to simply A. */
11243 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11244 : (integer_onep (op1)
11245 && !VECTOR_TYPE_P (type)))
11246 && integer_zerop (op2)
11247 /* If we try to convert OP0 to our type, the
11248 call to fold will try to move the conversion inside
11249 a COND, which will recurse. In that case, the COND_EXPR
11250 is probably the best choice, so leave it alone. */
11251 && type == TREE_TYPE (arg0))
11252 return pedantic_non_lvalue_loc (loc, arg0);
11254 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11255 over COND_EXPR in cases such as floating point comparisons. */
11256 if (integer_zerop (op1)
11257 && code == COND_EXPR
11258 && integer_onep (op2)
11259 && !VECTOR_TYPE_P (type)
11260 && truth_value_p (TREE_CODE (arg0)))
11261 return pedantic_non_lvalue_loc (loc,
11262 fold_convert_loc (loc, type,
11263 invert_truthvalue_loc (loc,
11264 arg0)));
11266 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11267 if (TREE_CODE (arg0) == LT_EXPR
11268 && integer_zerop (TREE_OPERAND (arg0, 1))
11269 && integer_zerop (op2)
11270 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11272 /* sign_bit_p looks through both zero and sign extensions,
11273 but for this optimization only sign extensions are
11274 usable. */
11275 tree tem2 = TREE_OPERAND (arg0, 0);
11276 while (tem != tem2)
11278 if (TREE_CODE (tem2) != NOP_EXPR
11279 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11281 tem = NULL_TREE;
11282 break;
11284 tem2 = TREE_OPERAND (tem2, 0);
11286 /* sign_bit_p only checks ARG1 bits within A's precision.
11287 If <sign bit of A> has wider type than A, bits outside
11288 of A's precision in <sign bit of A> need to be checked.
11289 If they are all 0, this optimization needs to be done
11290 in unsigned A's type, if they are all 1 in signed A's type,
11291 otherwise this can't be done. */
11292 if (tem
11293 && TYPE_PRECISION (TREE_TYPE (tem))
11294 < TYPE_PRECISION (TREE_TYPE (arg1))
11295 && TYPE_PRECISION (TREE_TYPE (tem))
11296 < TYPE_PRECISION (type))
11298 int inner_width, outer_width;
11299 tree tem_type;
11301 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11302 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11303 if (outer_width > TYPE_PRECISION (type))
11304 outer_width = TYPE_PRECISION (type);
11306 wide_int mask = wi::shifted_mask
11307 (inner_width, outer_width - inner_width, false,
11308 TYPE_PRECISION (TREE_TYPE (arg1)));
11310 wide_int common = mask & wi::to_wide (arg1);
11311 if (common == mask)
11313 tem_type = signed_type_for (TREE_TYPE (tem));
11314 tem = fold_convert_loc (loc, tem_type, tem);
11316 else if (common == 0)
11318 tem_type = unsigned_type_for (TREE_TYPE (tem));
11319 tem = fold_convert_loc (loc, tem_type, tem);
11321 else
11322 tem = NULL;
11325 if (tem)
11326 return
11327 fold_convert_loc (loc, type,
11328 fold_build2_loc (loc, BIT_AND_EXPR,
11329 TREE_TYPE (tem), tem,
11330 fold_convert_loc (loc,
11331 TREE_TYPE (tem),
11332 arg1)));
11335 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11336 already handled above. */
11337 if (TREE_CODE (arg0) == BIT_AND_EXPR
11338 && integer_onep (TREE_OPERAND (arg0, 1))
11339 && integer_zerop (op2)
11340 && integer_pow2p (arg1))
11342 tree tem = TREE_OPERAND (arg0, 0);
11343 STRIP_NOPS (tem);
11344 if (TREE_CODE (tem) == RSHIFT_EXPR
11345 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11346 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11347 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11348 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11349 fold_convert_loc (loc, type,
11350 TREE_OPERAND (tem, 0)),
11351 op1);
11354 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11355 is probably obsolete because the first operand should be a
11356 truth value (that's why we have the two cases above), but let's
11357 leave it in until we can confirm this for all front-ends. */
11358 if (integer_zerop (op2)
11359 && TREE_CODE (arg0) == NE_EXPR
11360 && integer_zerop (TREE_OPERAND (arg0, 1))
11361 && integer_pow2p (arg1)
11362 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11363 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11364 arg1, OEP_ONLY_CONST))
11365 return pedantic_non_lvalue_loc (loc,
11366 fold_convert_loc (loc, type,
11367 TREE_OPERAND (arg0, 0)));
11369 /* Disable the transformations below for vectors, since
11370 fold_binary_op_with_conditional_arg may undo them immediately,
11371 yielding an infinite loop. */
11372 if (code == VEC_COND_EXPR)
11373 return NULL_TREE;
11375 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11376 if (integer_zerop (op2)
11377 && truth_value_p (TREE_CODE (arg0))
11378 && truth_value_p (TREE_CODE (arg1))
11379 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11380 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11381 : TRUTH_ANDIF_EXPR,
11382 type, fold_convert_loc (loc, type, arg0), op1);
11384 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11385 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11386 && truth_value_p (TREE_CODE (arg0))
11387 && truth_value_p (TREE_CODE (arg1))
11388 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11390 location_t loc0 = expr_location_or (arg0, loc);
11391 /* Only perform transformation if ARG0 is easily inverted. */
11392 tem = fold_invert_truthvalue (loc0, arg0);
11393 if (tem)
11394 return fold_build2_loc (loc, code == VEC_COND_EXPR
11395 ? BIT_IOR_EXPR
11396 : TRUTH_ORIF_EXPR,
11397 type, fold_convert_loc (loc, type, tem),
11398 op1);
11401 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11402 if (integer_zerop (arg1)
11403 && truth_value_p (TREE_CODE (arg0))
11404 && truth_value_p (TREE_CODE (op2))
11405 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11407 location_t loc0 = expr_location_or (arg0, loc);
11408 /* Only perform transformation if ARG0 is easily inverted. */
11409 tem = fold_invert_truthvalue (loc0, arg0);
11410 if (tem)
11411 return fold_build2_loc (loc, code == VEC_COND_EXPR
11412 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11413 type, fold_convert_loc (loc, type, tem),
11414 op2);
11417 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11418 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11419 && truth_value_p (TREE_CODE (arg0))
11420 && truth_value_p (TREE_CODE (op2))
11421 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11422 return fold_build2_loc (loc, code == VEC_COND_EXPR
11423 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11424 type, fold_convert_loc (loc, type, arg0), op2);
11426 return NULL_TREE;
11428 case CALL_EXPR:
11429 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11430 of fold_ternary on them. */
11431 gcc_unreachable ();
11433 case BIT_FIELD_REF:
11434 if (TREE_CODE (arg0) == VECTOR_CST
11435 && (type == TREE_TYPE (TREE_TYPE (arg0))
11436 || (TREE_CODE (type) == VECTOR_TYPE
11437 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11439 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11440 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11441 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11442 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11444 if (n != 0
11445 && (idx % width) == 0
11446 && (n % width) == 0
11447 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11449 idx = idx / width;
11450 n = n / width;
11452 if (TREE_CODE (arg0) == VECTOR_CST)
11454 if (n == 1)
11455 return VECTOR_CST_ELT (arg0, idx);
11457 auto_vec<tree, 32> vals (n);
11458 for (unsigned i = 0; i < n; ++i)
11459 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11460 return build_vector (type, vals);
11465 /* On constants we can use native encode/interpret to constant
11466 fold (nearly) all BIT_FIELD_REFs. */
11467 if (CONSTANT_CLASS_P (arg0)
11468 && can_native_interpret_type_p (type)
11469 && BITS_PER_UNIT == 8)
11471 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11472 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11473 /* Limit us to a reasonable amount of work. To relax the
11474 other limitations we need bit-shifting of the buffer
11475 and rounding up the size. */
11476 if (bitpos % BITS_PER_UNIT == 0
11477 && bitsize % BITS_PER_UNIT == 0
11478 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11480 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11481 unsigned HOST_WIDE_INT len
11482 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11483 bitpos / BITS_PER_UNIT);
11484 if (len > 0
11485 && len * BITS_PER_UNIT >= bitsize)
11487 tree v = native_interpret_expr (type, b,
11488 bitsize / BITS_PER_UNIT);
11489 if (v)
11490 return v;
11495 return NULL_TREE;
11497 case FMA_EXPR:
11498 /* For integers we can decompose the FMA if possible. */
11499 if (TREE_CODE (arg0) == INTEGER_CST
11500 && TREE_CODE (arg1) == INTEGER_CST)
11501 return fold_build2_loc (loc, PLUS_EXPR, type,
11502 const_binop (MULT_EXPR, arg0, arg1), arg2);
11503 if (integer_zerop (arg2))
11504 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11506 return fold_fma (loc, type, arg0, arg1, arg2);
11508 case VEC_PERM_EXPR:
11509 if (TREE_CODE (arg2) == VECTOR_CST)
11511 unsigned int nelts = VECTOR_CST_NELTS (arg2), i, mask, mask2;
11512 bool need_mask_canon = false;
11513 bool need_mask_canon2 = false;
11514 bool all_in_vec0 = true;
11515 bool all_in_vec1 = true;
11516 bool maybe_identity = true;
11517 bool single_arg = (op0 == op1);
11518 bool changed = false;
11520 mask2 = 2 * nelts - 1;
11521 mask = single_arg ? (nelts - 1) : mask2;
11522 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11523 auto_vec_perm_indices sel (nelts);
11524 auto_vec_perm_indices sel2 (nelts);
11525 for (i = 0; i < nelts; i++)
11527 tree val = VECTOR_CST_ELT (arg2, i);
11528 if (TREE_CODE (val) != INTEGER_CST)
11529 return NULL_TREE;
11531 /* Make sure that the perm value is in an acceptable
11532 range. */
11533 wi::tree_to_wide_ref t = wi::to_wide (val);
11534 need_mask_canon |= wi::gtu_p (t, mask);
11535 need_mask_canon2 |= wi::gtu_p (t, mask2);
11536 unsigned int elt = t.to_uhwi () & mask;
11537 unsigned int elt2 = t.to_uhwi () & mask2;
11539 if (elt < nelts)
11540 all_in_vec1 = false;
11541 else
11542 all_in_vec0 = false;
11544 if ((elt & (nelts - 1)) != i)
11545 maybe_identity = false;
11547 sel.quick_push (elt);
11548 sel2.quick_push (elt2);
11551 if (maybe_identity)
11553 if (all_in_vec0)
11554 return op0;
11555 if (all_in_vec1)
11556 return op1;
11559 if (all_in_vec0)
11560 op1 = op0;
11561 else if (all_in_vec1)
11563 op0 = op1;
11564 for (i = 0; i < nelts; i++)
11565 sel[i] -= nelts;
11566 need_mask_canon = true;
11569 if ((TREE_CODE (op0) == VECTOR_CST
11570 || TREE_CODE (op0) == CONSTRUCTOR)
11571 && (TREE_CODE (op1) == VECTOR_CST
11572 || TREE_CODE (op1) == CONSTRUCTOR))
11574 tree t = fold_vec_perm (type, op0, op1, sel);
11575 if (t != NULL_TREE)
11576 return t;
11579 if (op0 == op1 && !single_arg)
11580 changed = true;
11582 /* Some targets are deficient and fail to expand a single
11583 argument permutation while still allowing an equivalent
11584 2-argument version. */
11585 if (need_mask_canon && arg2 == op2
11586 && !can_vec_perm_p (TYPE_MODE (type), false, &sel)
11587 && can_vec_perm_p (TYPE_MODE (type), false, &sel2))
11589 need_mask_canon = need_mask_canon2;
11590 sel = sel2;
11593 if (need_mask_canon && arg2 == op2)
11595 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11596 auto_vec<tree, 32> tsel (nelts);
11597 for (i = 0; i < nelts; i++)
11598 tsel.quick_push (build_int_cst (eltype, sel[i]));
11599 op2 = build_vector (TREE_TYPE (arg2), tsel);
11600 changed = true;
11603 if (changed)
11604 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11606 return NULL_TREE;
11608 case BIT_INSERT_EXPR:
11609 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11610 if (TREE_CODE (arg0) == INTEGER_CST
11611 && TREE_CODE (arg1) == INTEGER_CST)
11613 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11614 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11615 wide_int tem = (wi::to_wide (arg0)
11616 & wi::shifted_mask (bitpos, bitsize, true,
11617 TYPE_PRECISION (type)));
11618 wide_int tem2
11619 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11620 bitsize), bitpos);
11621 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11623 else if (TREE_CODE (arg0) == VECTOR_CST
11624 && CONSTANT_CLASS_P (arg1)
11625 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11626 TREE_TYPE (arg1)))
11628 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11629 unsigned HOST_WIDE_INT elsize
11630 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11631 if (bitpos % elsize == 0)
11633 unsigned k = bitpos / elsize;
11634 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11635 return arg0;
11636 else
11638 unsigned int nelts = VECTOR_CST_NELTS (arg0);
11639 auto_vec<tree, 32> elts (nelts);
11640 elts.quick_grow (nelts);
11641 memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
11642 sizeof (tree) * nelts);
11643 elts[k] = arg1;
11644 return build_vector (type, elts);
11648 return NULL_TREE;
11650 default:
11651 return NULL_TREE;
11652 } /* switch (code) */
11655 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11656 of an array (or vector). */
11658 tree
11659 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11661 tree index_type = NULL_TREE;
11662 offset_int low_bound = 0;
11664 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11666 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11667 if (domain_type && TYPE_MIN_VALUE (domain_type))
11669 /* Static constructors for variably sized objects makes no sense. */
11670 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11671 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11672 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11676 if (index_type)
11677 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11678 TYPE_SIGN (index_type));
11680 offset_int index = low_bound - 1;
11681 if (index_type)
11682 index = wi::ext (index, TYPE_PRECISION (index_type),
11683 TYPE_SIGN (index_type));
11685 offset_int max_index;
11686 unsigned HOST_WIDE_INT cnt;
11687 tree cfield, cval;
11689 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11691 /* Array constructor might explicitly set index, or specify a range,
11692 or leave index NULL meaning that it is next index after previous
11693 one. */
11694 if (cfield)
11696 if (TREE_CODE (cfield) == INTEGER_CST)
11697 max_index = index = wi::to_offset (cfield);
11698 else
11700 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11701 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11702 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11705 else
11707 index += 1;
11708 if (index_type)
11709 index = wi::ext (index, TYPE_PRECISION (index_type),
11710 TYPE_SIGN (index_type));
11711 max_index = index;
11714 /* Do we have match? */
11715 if (wi::cmpu (access_index, index) >= 0
11716 && wi::cmpu (access_index, max_index) <= 0)
11717 return cval;
11719 return NULL_TREE;
11722 /* Perform constant folding and related simplification of EXPR.
11723 The related simplifications include x*1 => x, x*0 => 0, etc.,
11724 and application of the associative law.
11725 NOP_EXPR conversions may be removed freely (as long as we
11726 are careful not to change the type of the overall expression).
11727 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11728 but we can constant-fold them if they have constant operands. */
11730 #ifdef ENABLE_FOLD_CHECKING
11731 # define fold(x) fold_1 (x)
11732 static tree fold_1 (tree);
11733 static
11734 #endif
11735 tree
11736 fold (tree expr)
11738 const tree t = expr;
11739 enum tree_code code = TREE_CODE (t);
11740 enum tree_code_class kind = TREE_CODE_CLASS (code);
11741 tree tem;
11742 location_t loc = EXPR_LOCATION (expr);
11744 /* Return right away if a constant. */
11745 if (kind == tcc_constant)
11746 return t;
11748 /* CALL_EXPR-like objects with variable numbers of operands are
11749 treated specially. */
11750 if (kind == tcc_vl_exp)
11752 if (code == CALL_EXPR)
11754 tem = fold_call_expr (loc, expr, false);
11755 return tem ? tem : expr;
11757 return expr;
11760 if (IS_EXPR_CODE_CLASS (kind))
11762 tree type = TREE_TYPE (t);
11763 tree op0, op1, op2;
11765 switch (TREE_CODE_LENGTH (code))
11767 case 1:
11768 op0 = TREE_OPERAND (t, 0);
11769 tem = fold_unary_loc (loc, code, type, op0);
11770 return tem ? tem : expr;
11771 case 2:
11772 op0 = TREE_OPERAND (t, 0);
11773 op1 = TREE_OPERAND (t, 1);
11774 tem = fold_binary_loc (loc, code, type, op0, op1);
11775 return tem ? tem : expr;
11776 case 3:
11777 op0 = TREE_OPERAND (t, 0);
11778 op1 = TREE_OPERAND (t, 1);
11779 op2 = TREE_OPERAND (t, 2);
11780 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11781 return tem ? tem : expr;
11782 default:
11783 break;
11787 switch (code)
11789 case ARRAY_REF:
11791 tree op0 = TREE_OPERAND (t, 0);
11792 tree op1 = TREE_OPERAND (t, 1);
11794 if (TREE_CODE (op1) == INTEGER_CST
11795 && TREE_CODE (op0) == CONSTRUCTOR
11796 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11798 tree val = get_array_ctor_element_at_index (op0,
11799 wi::to_offset (op1));
11800 if (val)
11801 return val;
11804 return t;
11807 /* Return a VECTOR_CST if possible. */
11808 case CONSTRUCTOR:
11810 tree type = TREE_TYPE (t);
11811 if (TREE_CODE (type) != VECTOR_TYPE)
11812 return t;
11814 unsigned i;
11815 tree val;
11816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11817 if (! CONSTANT_CLASS_P (val))
11818 return t;
11820 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11823 case CONST_DECL:
11824 return fold (DECL_INITIAL (t));
11826 default:
11827 return t;
11828 } /* switch (code) */
11831 #ifdef ENABLE_FOLD_CHECKING
11832 #undef fold
11834 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11835 hash_table<nofree_ptr_hash<const tree_node> > *);
11836 static void fold_check_failed (const_tree, const_tree);
11837 void print_fold_checksum (const_tree);
11839 /* When --enable-checking=fold, compute a digest of expr before
11840 and after actual fold call to see if fold did not accidentally
11841 change original expr. */
11843 tree
11844 fold (tree expr)
11846 tree ret;
11847 struct md5_ctx ctx;
11848 unsigned char checksum_before[16], checksum_after[16];
11849 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11851 md5_init_ctx (&ctx);
11852 fold_checksum_tree (expr, &ctx, &ht);
11853 md5_finish_ctx (&ctx, checksum_before);
11854 ht.empty ();
11856 ret = fold_1 (expr);
11858 md5_init_ctx (&ctx);
11859 fold_checksum_tree (expr, &ctx, &ht);
11860 md5_finish_ctx (&ctx, checksum_after);
11862 if (memcmp (checksum_before, checksum_after, 16))
11863 fold_check_failed (expr, ret);
11865 return ret;
11868 void
11869 print_fold_checksum (const_tree expr)
11871 struct md5_ctx ctx;
11872 unsigned char checksum[16], cnt;
11873 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11875 md5_init_ctx (&ctx);
11876 fold_checksum_tree (expr, &ctx, &ht);
11877 md5_finish_ctx (&ctx, checksum);
11878 for (cnt = 0; cnt < 16; ++cnt)
11879 fprintf (stderr, "%02x", checksum[cnt]);
11880 putc ('\n', stderr);
11883 static void
11884 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
11886 internal_error ("fold check: original tree changed by fold");
11889 static void
11890 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
11891 hash_table<nofree_ptr_hash <const tree_node> > *ht)
11893 const tree_node **slot;
11894 enum tree_code code;
11895 union tree_node buf;
11896 int i, len;
11898 recursive_label:
11899 if (expr == NULL)
11900 return;
11901 slot = ht->find_slot (expr, INSERT);
11902 if (*slot != NULL)
11903 return;
11904 *slot = expr;
11905 code = TREE_CODE (expr);
11906 if (TREE_CODE_CLASS (code) == tcc_declaration
11907 && HAS_DECL_ASSEMBLER_NAME_P (expr))
11909 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
11910 memcpy ((char *) &buf, expr, tree_size (expr));
11911 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
11912 buf.decl_with_vis.symtab_node = NULL;
11913 expr = (tree) &buf;
11915 else if (TREE_CODE_CLASS (code) == tcc_type
11916 && (TYPE_POINTER_TO (expr)
11917 || TYPE_REFERENCE_TO (expr)
11918 || TYPE_CACHED_VALUES_P (expr)
11919 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
11920 || TYPE_NEXT_VARIANT (expr)
11921 || TYPE_ALIAS_SET_KNOWN_P (expr)))
11923 /* Allow these fields to be modified. */
11924 tree tmp;
11925 memcpy ((char *) &buf, expr, tree_size (expr));
11926 expr = tmp = (tree) &buf;
11927 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
11928 TYPE_POINTER_TO (tmp) = NULL;
11929 TYPE_REFERENCE_TO (tmp) = NULL;
11930 TYPE_NEXT_VARIANT (tmp) = NULL;
11931 TYPE_ALIAS_SET (tmp) = -1;
11932 if (TYPE_CACHED_VALUES_P (tmp))
11934 TYPE_CACHED_VALUES_P (tmp) = 0;
11935 TYPE_CACHED_VALUES (tmp) = NULL;
11938 md5_process_bytes (expr, tree_size (expr), ctx);
11939 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
11940 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
11941 if (TREE_CODE_CLASS (code) != tcc_type
11942 && TREE_CODE_CLASS (code) != tcc_declaration
11943 && code != TREE_LIST
11944 && code != SSA_NAME
11945 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
11946 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
11947 switch (TREE_CODE_CLASS (code))
11949 case tcc_constant:
11950 switch (code)
11952 case STRING_CST:
11953 md5_process_bytes (TREE_STRING_POINTER (expr),
11954 TREE_STRING_LENGTH (expr), ctx);
11955 break;
11956 case COMPLEX_CST:
11957 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
11958 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
11959 break;
11960 case VECTOR_CST:
11961 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
11962 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
11963 break;
11964 default:
11965 break;
11967 break;
11968 case tcc_exceptional:
11969 switch (code)
11971 case TREE_LIST:
11972 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
11973 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
11974 expr = TREE_CHAIN (expr);
11975 goto recursive_label;
11976 break;
11977 case TREE_VEC:
11978 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
11979 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
11980 break;
11981 default:
11982 break;
11984 break;
11985 case tcc_expression:
11986 case tcc_reference:
11987 case tcc_comparison:
11988 case tcc_unary:
11989 case tcc_binary:
11990 case tcc_statement:
11991 case tcc_vl_exp:
11992 len = TREE_OPERAND_LENGTH (expr);
11993 for (i = 0; i < len; ++i)
11994 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
11995 break;
11996 case tcc_declaration:
11997 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
11998 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
11999 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12001 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12002 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12003 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12004 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12005 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12008 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12010 if (TREE_CODE (expr) == FUNCTION_DECL)
12012 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12013 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12015 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12017 break;
12018 case tcc_type:
12019 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12020 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12021 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12022 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12023 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12024 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12025 if (INTEGRAL_TYPE_P (expr)
12026 || SCALAR_FLOAT_TYPE_P (expr))
12028 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12029 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12031 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12032 if (TREE_CODE (expr) == RECORD_TYPE
12033 || TREE_CODE (expr) == UNION_TYPE
12034 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12035 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12036 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12037 break;
12038 default:
12039 break;
12043 /* Helper function for outputting the checksum of a tree T. When
12044 debugging with gdb, you can "define mynext" to be "next" followed
12045 by "call debug_fold_checksum (op0)", then just trace down till the
12046 outputs differ. */
12048 DEBUG_FUNCTION void
12049 debug_fold_checksum (const_tree t)
12051 int i;
12052 unsigned char checksum[16];
12053 struct md5_ctx ctx;
12054 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12056 md5_init_ctx (&ctx);
12057 fold_checksum_tree (t, &ctx, &ht);
12058 md5_finish_ctx (&ctx, checksum);
12059 ht.empty ();
12061 for (i = 0; i < 16; i++)
12062 fprintf (stderr, "%d ", checksum[i]);
12064 fprintf (stderr, "\n");
12067 #endif
12069 /* Fold a unary tree expression with code CODE of type TYPE with an
12070 operand OP0. LOC is the location of the resulting expression.
12071 Return a folded expression if successful. Otherwise, return a tree
12072 expression with code CODE of type TYPE with an operand OP0. */
12074 tree
12075 fold_build1_loc (location_t loc,
12076 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12078 tree tem;
12079 #ifdef ENABLE_FOLD_CHECKING
12080 unsigned char checksum_before[16], checksum_after[16];
12081 struct md5_ctx ctx;
12082 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12084 md5_init_ctx (&ctx);
12085 fold_checksum_tree (op0, &ctx, &ht);
12086 md5_finish_ctx (&ctx, checksum_before);
12087 ht.empty ();
12088 #endif
12090 tem = fold_unary_loc (loc, code, type, op0);
12091 if (!tem)
12092 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12094 #ifdef ENABLE_FOLD_CHECKING
12095 md5_init_ctx (&ctx);
12096 fold_checksum_tree (op0, &ctx, &ht);
12097 md5_finish_ctx (&ctx, checksum_after);
12099 if (memcmp (checksum_before, checksum_after, 16))
12100 fold_check_failed (op0, tem);
12101 #endif
12102 return tem;
12105 /* Fold a binary tree expression with code CODE of type TYPE with
12106 operands OP0 and OP1. LOC is the location of the resulting
12107 expression. Return a folded expression if successful. Otherwise,
12108 return a tree expression with code CODE of type TYPE with operands
12109 OP0 and OP1. */
12111 tree
12112 fold_build2_loc (location_t loc,
12113 enum tree_code code, tree type, tree op0, tree op1
12114 MEM_STAT_DECL)
12116 tree tem;
12117 #ifdef ENABLE_FOLD_CHECKING
12118 unsigned char checksum_before_op0[16],
12119 checksum_before_op1[16],
12120 checksum_after_op0[16],
12121 checksum_after_op1[16];
12122 struct md5_ctx ctx;
12123 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12125 md5_init_ctx (&ctx);
12126 fold_checksum_tree (op0, &ctx, &ht);
12127 md5_finish_ctx (&ctx, checksum_before_op0);
12128 ht.empty ();
12130 md5_init_ctx (&ctx);
12131 fold_checksum_tree (op1, &ctx, &ht);
12132 md5_finish_ctx (&ctx, checksum_before_op1);
12133 ht.empty ();
12134 #endif
12136 tem = fold_binary_loc (loc, code, type, op0, op1);
12137 if (!tem)
12138 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12140 #ifdef ENABLE_FOLD_CHECKING
12141 md5_init_ctx (&ctx);
12142 fold_checksum_tree (op0, &ctx, &ht);
12143 md5_finish_ctx (&ctx, checksum_after_op0);
12144 ht.empty ();
12146 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12147 fold_check_failed (op0, tem);
12149 md5_init_ctx (&ctx);
12150 fold_checksum_tree (op1, &ctx, &ht);
12151 md5_finish_ctx (&ctx, checksum_after_op1);
12153 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12154 fold_check_failed (op1, tem);
12155 #endif
12156 return tem;
12159 /* Fold a ternary tree expression with code CODE of type TYPE with
12160 operands OP0, OP1, and OP2. Return a folded expression if
12161 successful. Otherwise, return a tree expression with code CODE of
12162 type TYPE with operands OP0, OP1, and OP2. */
12164 tree
12165 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12166 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12168 tree tem;
12169 #ifdef ENABLE_FOLD_CHECKING
12170 unsigned char checksum_before_op0[16],
12171 checksum_before_op1[16],
12172 checksum_before_op2[16],
12173 checksum_after_op0[16],
12174 checksum_after_op1[16],
12175 checksum_after_op2[16];
12176 struct md5_ctx ctx;
12177 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12179 md5_init_ctx (&ctx);
12180 fold_checksum_tree (op0, &ctx, &ht);
12181 md5_finish_ctx (&ctx, checksum_before_op0);
12182 ht.empty ();
12184 md5_init_ctx (&ctx);
12185 fold_checksum_tree (op1, &ctx, &ht);
12186 md5_finish_ctx (&ctx, checksum_before_op1);
12187 ht.empty ();
12189 md5_init_ctx (&ctx);
12190 fold_checksum_tree (op2, &ctx, &ht);
12191 md5_finish_ctx (&ctx, checksum_before_op2);
12192 ht.empty ();
12193 #endif
12195 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12196 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12197 if (!tem)
12198 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12200 #ifdef ENABLE_FOLD_CHECKING
12201 md5_init_ctx (&ctx);
12202 fold_checksum_tree (op0, &ctx, &ht);
12203 md5_finish_ctx (&ctx, checksum_after_op0);
12204 ht.empty ();
12206 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12207 fold_check_failed (op0, tem);
12209 md5_init_ctx (&ctx);
12210 fold_checksum_tree (op1, &ctx, &ht);
12211 md5_finish_ctx (&ctx, checksum_after_op1);
12212 ht.empty ();
12214 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12215 fold_check_failed (op1, tem);
12217 md5_init_ctx (&ctx);
12218 fold_checksum_tree (op2, &ctx, &ht);
12219 md5_finish_ctx (&ctx, checksum_after_op2);
12221 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12222 fold_check_failed (op2, tem);
12223 #endif
12224 return tem;
12227 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12228 arguments in ARGARRAY, and a null static chain.
12229 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12230 of type TYPE from the given operands as constructed by build_call_array. */
12232 tree
12233 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12234 int nargs, tree *argarray)
12236 tree tem;
12237 #ifdef ENABLE_FOLD_CHECKING
12238 unsigned char checksum_before_fn[16],
12239 checksum_before_arglist[16],
12240 checksum_after_fn[16],
12241 checksum_after_arglist[16];
12242 struct md5_ctx ctx;
12243 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12244 int i;
12246 md5_init_ctx (&ctx);
12247 fold_checksum_tree (fn, &ctx, &ht);
12248 md5_finish_ctx (&ctx, checksum_before_fn);
12249 ht.empty ();
12251 md5_init_ctx (&ctx);
12252 for (i = 0; i < nargs; i++)
12253 fold_checksum_tree (argarray[i], &ctx, &ht);
12254 md5_finish_ctx (&ctx, checksum_before_arglist);
12255 ht.empty ();
12256 #endif
12258 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12259 if (!tem)
12260 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12262 #ifdef ENABLE_FOLD_CHECKING
12263 md5_init_ctx (&ctx);
12264 fold_checksum_tree (fn, &ctx, &ht);
12265 md5_finish_ctx (&ctx, checksum_after_fn);
12266 ht.empty ();
12268 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12269 fold_check_failed (fn, tem);
12271 md5_init_ctx (&ctx);
12272 for (i = 0; i < nargs; i++)
12273 fold_checksum_tree (argarray[i], &ctx, &ht);
12274 md5_finish_ctx (&ctx, checksum_after_arglist);
12276 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12277 fold_check_failed (NULL_TREE, tem);
12278 #endif
12279 return tem;
12282 /* Perform constant folding and related simplification of initializer
12283 expression EXPR. These behave identically to "fold_buildN" but ignore
12284 potential run-time traps and exceptions that fold must preserve. */
12286 #define START_FOLD_INIT \
12287 int saved_signaling_nans = flag_signaling_nans;\
12288 int saved_trapping_math = flag_trapping_math;\
12289 int saved_rounding_math = flag_rounding_math;\
12290 int saved_trapv = flag_trapv;\
12291 int saved_folding_initializer = folding_initializer;\
12292 flag_signaling_nans = 0;\
12293 flag_trapping_math = 0;\
12294 flag_rounding_math = 0;\
12295 flag_trapv = 0;\
12296 folding_initializer = 1;
12298 #define END_FOLD_INIT \
12299 flag_signaling_nans = saved_signaling_nans;\
12300 flag_trapping_math = saved_trapping_math;\
12301 flag_rounding_math = saved_rounding_math;\
12302 flag_trapv = saved_trapv;\
12303 folding_initializer = saved_folding_initializer;
12305 tree
12306 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12307 tree type, tree op)
12309 tree result;
12310 START_FOLD_INIT;
12312 result = fold_build1_loc (loc, code, type, op);
12314 END_FOLD_INIT;
12315 return result;
12318 tree
12319 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12320 tree type, tree op0, tree op1)
12322 tree result;
12323 START_FOLD_INIT;
12325 result = fold_build2_loc (loc, code, type, op0, op1);
12327 END_FOLD_INIT;
12328 return result;
12331 tree
12332 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12333 int nargs, tree *argarray)
12335 tree result;
12336 START_FOLD_INIT;
12338 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12340 END_FOLD_INIT;
12341 return result;
12344 #undef START_FOLD_INIT
12345 #undef END_FOLD_INIT
12347 /* Determine if first argument is a multiple of second argument. Return 0 if
12348 it is not, or we cannot easily determined it to be.
12350 An example of the sort of thing we care about (at this point; this routine
12351 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12352 fold cases do now) is discovering that
12354 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12356 is a multiple of
12358 SAVE_EXPR (J * 8)
12360 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12362 This code also handles discovering that
12364 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12366 is a multiple of 8 so we don't have to worry about dealing with a
12367 possible remainder.
12369 Note that we *look* inside a SAVE_EXPR only to determine how it was
12370 calculated; it is not safe for fold to do much of anything else with the
12371 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12372 at run time. For example, the latter example above *cannot* be implemented
12373 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12374 evaluation time of the original SAVE_EXPR is not necessarily the same at
12375 the time the new expression is evaluated. The only optimization of this
12376 sort that would be valid is changing
12378 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12380 divided by 8 to
12382 SAVE_EXPR (I) * SAVE_EXPR (J)
12384 (where the same SAVE_EXPR (J) is used in the original and the
12385 transformed version). */
12388 multiple_of_p (tree type, const_tree top, const_tree bottom)
12390 gimple *stmt;
12391 tree t1, op1, op2;
12393 if (operand_equal_p (top, bottom, 0))
12394 return 1;
12396 if (TREE_CODE (type) != INTEGER_TYPE)
12397 return 0;
12399 switch (TREE_CODE (top))
12401 case BIT_AND_EXPR:
12402 /* Bitwise and provides a power of two multiple. If the mask is
12403 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12404 if (!integer_pow2p (bottom))
12405 return 0;
12406 /* FALLTHRU */
12408 case MULT_EXPR:
12409 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12410 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12412 case MINUS_EXPR:
12413 /* It is impossible to prove if op0 - op1 is multiple of bottom
12414 precisely, so be conservative here checking if both op0 and op1
12415 are multiple of bottom. Note we check the second operand first
12416 since it's usually simpler. */
12417 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12418 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12420 case PLUS_EXPR:
12421 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12422 as op0 - 3 if the expression has unsigned type. For example,
12423 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12424 op1 = TREE_OPERAND (top, 1);
12425 if (TYPE_UNSIGNED (type)
12426 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12427 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12428 return (multiple_of_p (type, op1, bottom)
12429 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12431 case LSHIFT_EXPR:
12432 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12434 op1 = TREE_OPERAND (top, 1);
12435 /* const_binop may not detect overflow correctly,
12436 so check for it explicitly here. */
12437 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12438 wi::to_wide (op1))
12439 && 0 != (t1 = fold_convert (type,
12440 const_binop (LSHIFT_EXPR,
12441 size_one_node,
12442 op1)))
12443 && !TREE_OVERFLOW (t1))
12444 return multiple_of_p (type, t1, bottom);
12446 return 0;
12448 case NOP_EXPR:
12449 /* Can't handle conversions from non-integral or wider integral type. */
12450 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12451 || (TYPE_PRECISION (type)
12452 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12453 return 0;
12455 /* fall through */
12457 case SAVE_EXPR:
12458 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12460 case COND_EXPR:
12461 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12462 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12464 case INTEGER_CST:
12465 if (TREE_CODE (bottom) != INTEGER_CST
12466 || integer_zerop (bottom)
12467 || (TYPE_UNSIGNED (type)
12468 && (tree_int_cst_sgn (top) < 0
12469 || tree_int_cst_sgn (bottom) < 0)))
12470 return 0;
12471 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12472 SIGNED);
12474 case SSA_NAME:
12475 if (TREE_CODE (bottom) == INTEGER_CST
12476 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12477 && gimple_code (stmt) == GIMPLE_ASSIGN)
12479 enum tree_code code = gimple_assign_rhs_code (stmt);
12481 /* Check for special cases to see if top is defined as multiple
12482 of bottom:
12484 top = (X & ~(bottom - 1) ; bottom is power of 2
12488 Y = X % bottom
12489 top = X - Y. */
12490 if (code == BIT_AND_EXPR
12491 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12492 && TREE_CODE (op2) == INTEGER_CST
12493 && integer_pow2p (bottom)
12494 && wi::multiple_of_p (wi::to_widest (op2),
12495 wi::to_widest (bottom), UNSIGNED))
12496 return 1;
12498 op1 = gimple_assign_rhs1 (stmt);
12499 if (code == MINUS_EXPR
12500 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12501 && TREE_CODE (op2) == SSA_NAME
12502 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12503 && gimple_code (stmt) == GIMPLE_ASSIGN
12504 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12505 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12506 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12507 return 1;
12510 /* fall through */
12512 default:
12513 return 0;
12517 #define tree_expr_nonnegative_warnv_p(X, Y) \
12518 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12520 #define RECURSE(X) \
12521 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12523 /* Return true if CODE or TYPE is known to be non-negative. */
12525 static bool
12526 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12528 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12529 && truth_value_p (code))
12530 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12531 have a signed:1 type (where the value is -1 and 0). */
12532 return true;
12533 return false;
12536 /* Return true if (CODE OP0) is known to be non-negative. If the return
12537 value is based on the assumption that signed overflow is undefined,
12538 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12539 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12541 bool
12542 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12543 bool *strict_overflow_p, int depth)
12545 if (TYPE_UNSIGNED (type))
12546 return true;
12548 switch (code)
12550 case ABS_EXPR:
12551 /* We can't return 1 if flag_wrapv is set because
12552 ABS_EXPR<INT_MIN> = INT_MIN. */
12553 if (!ANY_INTEGRAL_TYPE_P (type))
12554 return true;
12555 if (TYPE_OVERFLOW_UNDEFINED (type))
12557 *strict_overflow_p = true;
12558 return true;
12560 break;
12562 case NON_LVALUE_EXPR:
12563 case FLOAT_EXPR:
12564 case FIX_TRUNC_EXPR:
12565 return RECURSE (op0);
12567 CASE_CONVERT:
12569 tree inner_type = TREE_TYPE (op0);
12570 tree outer_type = type;
12572 if (TREE_CODE (outer_type) == REAL_TYPE)
12574 if (TREE_CODE (inner_type) == REAL_TYPE)
12575 return RECURSE (op0);
12576 if (INTEGRAL_TYPE_P (inner_type))
12578 if (TYPE_UNSIGNED (inner_type))
12579 return true;
12580 return RECURSE (op0);
12583 else if (INTEGRAL_TYPE_P (outer_type))
12585 if (TREE_CODE (inner_type) == REAL_TYPE)
12586 return RECURSE (op0);
12587 if (INTEGRAL_TYPE_P (inner_type))
12588 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12589 && TYPE_UNSIGNED (inner_type);
12592 break;
12594 default:
12595 return tree_simple_nonnegative_warnv_p (code, type);
12598 /* We don't know sign of `t', so be conservative and return false. */
12599 return false;
12602 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12603 value is based on the assumption that signed overflow is undefined,
12604 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12605 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12607 bool
12608 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12609 tree op1, bool *strict_overflow_p,
12610 int depth)
12612 if (TYPE_UNSIGNED (type))
12613 return true;
12615 switch (code)
12617 case POINTER_PLUS_EXPR:
12618 case PLUS_EXPR:
12619 if (FLOAT_TYPE_P (type))
12620 return RECURSE (op0) && RECURSE (op1);
12622 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12623 both unsigned and at least 2 bits shorter than the result. */
12624 if (TREE_CODE (type) == INTEGER_TYPE
12625 && TREE_CODE (op0) == NOP_EXPR
12626 && TREE_CODE (op1) == NOP_EXPR)
12628 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12629 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12630 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12631 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12633 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12634 TYPE_PRECISION (inner2)) + 1;
12635 return prec < TYPE_PRECISION (type);
12638 break;
12640 case MULT_EXPR:
12641 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12643 /* x * x is always non-negative for floating point x
12644 or without overflow. */
12645 if (operand_equal_p (op0, op1, 0)
12646 || (RECURSE (op0) && RECURSE (op1)))
12648 if (ANY_INTEGRAL_TYPE_P (type)
12649 && TYPE_OVERFLOW_UNDEFINED (type))
12650 *strict_overflow_p = true;
12651 return true;
12655 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12656 both unsigned and their total bits is shorter than the result. */
12657 if (TREE_CODE (type) == INTEGER_TYPE
12658 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12659 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12661 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12662 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12663 : TREE_TYPE (op0);
12664 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12665 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12666 : TREE_TYPE (op1);
12668 bool unsigned0 = TYPE_UNSIGNED (inner0);
12669 bool unsigned1 = TYPE_UNSIGNED (inner1);
12671 if (TREE_CODE (op0) == INTEGER_CST)
12672 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12674 if (TREE_CODE (op1) == INTEGER_CST)
12675 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12677 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12678 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12680 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12681 ? tree_int_cst_min_precision (op0, UNSIGNED)
12682 : TYPE_PRECISION (inner0);
12684 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12685 ? tree_int_cst_min_precision (op1, UNSIGNED)
12686 : TYPE_PRECISION (inner1);
12688 return precision0 + precision1 < TYPE_PRECISION (type);
12691 return false;
12693 case BIT_AND_EXPR:
12694 case MAX_EXPR:
12695 return RECURSE (op0) || RECURSE (op1);
12697 case BIT_IOR_EXPR:
12698 case BIT_XOR_EXPR:
12699 case MIN_EXPR:
12700 case RDIV_EXPR:
12701 case TRUNC_DIV_EXPR:
12702 case CEIL_DIV_EXPR:
12703 case FLOOR_DIV_EXPR:
12704 case ROUND_DIV_EXPR:
12705 return RECURSE (op0) && RECURSE (op1);
12707 case TRUNC_MOD_EXPR:
12708 return RECURSE (op0);
12710 case FLOOR_MOD_EXPR:
12711 return RECURSE (op1);
12713 case CEIL_MOD_EXPR:
12714 case ROUND_MOD_EXPR:
12715 default:
12716 return tree_simple_nonnegative_warnv_p (code, type);
12719 /* We don't know sign of `t', so be conservative and return false. */
12720 return false;
12723 /* Return true if T is known to be non-negative. If the return
12724 value is based on the assumption that signed overflow is undefined,
12725 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12726 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12728 bool
12729 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12731 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12732 return true;
12734 switch (TREE_CODE (t))
12736 case INTEGER_CST:
12737 return tree_int_cst_sgn (t) >= 0;
12739 case REAL_CST:
12740 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12742 case FIXED_CST:
12743 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12745 case COND_EXPR:
12746 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12748 case SSA_NAME:
12749 /* Limit the depth of recursion to avoid quadratic behavior.
12750 This is expected to catch almost all occurrences in practice.
12751 If this code misses important cases that unbounded recursion
12752 would not, passes that need this information could be revised
12753 to provide it through dataflow propagation. */
12754 return (!name_registered_for_update_p (t)
12755 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12756 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12757 strict_overflow_p, depth));
12759 default:
12760 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12764 /* Return true if T is known to be non-negative. If the return
12765 value is based on the assumption that signed overflow is undefined,
12766 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12767 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12769 bool
12770 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12771 bool *strict_overflow_p, int depth)
12773 switch (fn)
12775 CASE_CFN_ACOS:
12776 CASE_CFN_ACOSH:
12777 CASE_CFN_CABS:
12778 CASE_CFN_COSH:
12779 CASE_CFN_ERFC:
12780 CASE_CFN_EXP:
12781 CASE_CFN_EXP10:
12782 CASE_CFN_EXP2:
12783 CASE_CFN_FABS:
12784 CASE_CFN_FDIM:
12785 CASE_CFN_HYPOT:
12786 CASE_CFN_POW10:
12787 CASE_CFN_FFS:
12788 CASE_CFN_PARITY:
12789 CASE_CFN_POPCOUNT:
12790 CASE_CFN_CLZ:
12791 CASE_CFN_CLRSB:
12792 case CFN_BUILT_IN_BSWAP32:
12793 case CFN_BUILT_IN_BSWAP64:
12794 /* Always true. */
12795 return true;
12797 CASE_CFN_SQRT:
12798 /* sqrt(-0.0) is -0.0. */
12799 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12800 return true;
12801 return RECURSE (arg0);
12803 CASE_CFN_ASINH:
12804 CASE_CFN_ATAN:
12805 CASE_CFN_ATANH:
12806 CASE_CFN_CBRT:
12807 CASE_CFN_CEIL:
12808 CASE_CFN_ERF:
12809 CASE_CFN_EXPM1:
12810 CASE_CFN_FLOOR:
12811 CASE_CFN_FMOD:
12812 CASE_CFN_FREXP:
12813 CASE_CFN_ICEIL:
12814 CASE_CFN_IFLOOR:
12815 CASE_CFN_IRINT:
12816 CASE_CFN_IROUND:
12817 CASE_CFN_LCEIL:
12818 CASE_CFN_LDEXP:
12819 CASE_CFN_LFLOOR:
12820 CASE_CFN_LLCEIL:
12821 CASE_CFN_LLFLOOR:
12822 CASE_CFN_LLRINT:
12823 CASE_CFN_LLROUND:
12824 CASE_CFN_LRINT:
12825 CASE_CFN_LROUND:
12826 CASE_CFN_MODF:
12827 CASE_CFN_NEARBYINT:
12828 CASE_CFN_RINT:
12829 CASE_CFN_ROUND:
12830 CASE_CFN_SCALB:
12831 CASE_CFN_SCALBLN:
12832 CASE_CFN_SCALBN:
12833 CASE_CFN_SIGNBIT:
12834 CASE_CFN_SIGNIFICAND:
12835 CASE_CFN_SINH:
12836 CASE_CFN_TANH:
12837 CASE_CFN_TRUNC:
12838 /* True if the 1st argument is nonnegative. */
12839 return RECURSE (arg0);
12841 CASE_CFN_FMAX:
12842 /* True if the 1st OR 2nd arguments are nonnegative. */
12843 return RECURSE (arg0) || RECURSE (arg1);
12845 CASE_CFN_FMIN:
12846 /* True if the 1st AND 2nd arguments are nonnegative. */
12847 return RECURSE (arg0) && RECURSE (arg1);
12849 CASE_CFN_COPYSIGN:
12850 /* True if the 2nd argument is nonnegative. */
12851 return RECURSE (arg1);
12853 CASE_CFN_POWI:
12854 /* True if the 1st argument is nonnegative or the second
12855 argument is an even integer. */
12856 if (TREE_CODE (arg1) == INTEGER_CST
12857 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12858 return true;
12859 return RECURSE (arg0);
12861 CASE_CFN_POW:
12862 /* True if the 1st argument is nonnegative or the second
12863 argument is an even integer valued real. */
12864 if (TREE_CODE (arg1) == REAL_CST)
12866 REAL_VALUE_TYPE c;
12867 HOST_WIDE_INT n;
12869 c = TREE_REAL_CST (arg1);
12870 n = real_to_integer (&c);
12871 if ((n & 1) == 0)
12873 REAL_VALUE_TYPE cint;
12874 real_from_integer (&cint, VOIDmode, n, SIGNED);
12875 if (real_identical (&c, &cint))
12876 return true;
12879 return RECURSE (arg0);
12881 default:
12882 break;
12884 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
12887 /* Return true if T is known to be non-negative. If the return
12888 value is based on the assumption that signed overflow is undefined,
12889 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12890 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12892 static bool
12893 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12895 enum tree_code code = TREE_CODE (t);
12896 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12897 return true;
12899 switch (code)
12901 case TARGET_EXPR:
12903 tree temp = TARGET_EXPR_SLOT (t);
12904 t = TARGET_EXPR_INITIAL (t);
12906 /* If the initializer is non-void, then it's a normal expression
12907 that will be assigned to the slot. */
12908 if (!VOID_TYPE_P (t))
12909 return RECURSE (t);
12911 /* Otherwise, the initializer sets the slot in some way. One common
12912 way is an assignment statement at the end of the initializer. */
12913 while (1)
12915 if (TREE_CODE (t) == BIND_EXPR)
12916 t = expr_last (BIND_EXPR_BODY (t));
12917 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
12918 || TREE_CODE (t) == TRY_CATCH_EXPR)
12919 t = expr_last (TREE_OPERAND (t, 0));
12920 else if (TREE_CODE (t) == STATEMENT_LIST)
12921 t = expr_last (t);
12922 else
12923 break;
12925 if (TREE_CODE (t) == MODIFY_EXPR
12926 && TREE_OPERAND (t, 0) == temp)
12927 return RECURSE (TREE_OPERAND (t, 1));
12929 return false;
12932 case CALL_EXPR:
12934 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
12935 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
12937 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
12938 get_call_combined_fn (t),
12939 arg0,
12940 arg1,
12941 strict_overflow_p, depth);
12943 case COMPOUND_EXPR:
12944 case MODIFY_EXPR:
12945 return RECURSE (TREE_OPERAND (t, 1));
12947 case BIND_EXPR:
12948 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
12950 case SAVE_EXPR:
12951 return RECURSE (TREE_OPERAND (t, 0));
12953 default:
12954 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12958 #undef RECURSE
12959 #undef tree_expr_nonnegative_warnv_p
12961 /* Return true if T is known to be non-negative. If the return
12962 value is based on the assumption that signed overflow is undefined,
12963 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12964 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12966 bool
12967 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12969 enum tree_code code;
12970 if (t == error_mark_node)
12971 return false;
12973 code = TREE_CODE (t);
12974 switch (TREE_CODE_CLASS (code))
12976 case tcc_binary:
12977 case tcc_comparison:
12978 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
12979 TREE_TYPE (t),
12980 TREE_OPERAND (t, 0),
12981 TREE_OPERAND (t, 1),
12982 strict_overflow_p, depth);
12984 case tcc_unary:
12985 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
12986 TREE_TYPE (t),
12987 TREE_OPERAND (t, 0),
12988 strict_overflow_p, depth);
12990 case tcc_constant:
12991 case tcc_declaration:
12992 case tcc_reference:
12993 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
12995 default:
12996 break;
12999 switch (code)
13001 case TRUTH_AND_EXPR:
13002 case TRUTH_OR_EXPR:
13003 case TRUTH_XOR_EXPR:
13004 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13005 TREE_TYPE (t),
13006 TREE_OPERAND (t, 0),
13007 TREE_OPERAND (t, 1),
13008 strict_overflow_p, depth);
13009 case TRUTH_NOT_EXPR:
13010 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13011 TREE_TYPE (t),
13012 TREE_OPERAND (t, 0),
13013 strict_overflow_p, depth);
13015 case COND_EXPR:
13016 case CONSTRUCTOR:
13017 case OBJ_TYPE_REF:
13018 case ASSERT_EXPR:
13019 case ADDR_EXPR:
13020 case WITH_SIZE_EXPR:
13021 case SSA_NAME:
13022 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13024 default:
13025 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13029 /* Return true if `t' is known to be non-negative. Handle warnings
13030 about undefined signed overflow. */
13032 bool
13033 tree_expr_nonnegative_p (tree t)
13035 bool ret, strict_overflow_p;
13037 strict_overflow_p = false;
13038 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13039 if (strict_overflow_p)
13040 fold_overflow_warning (("assuming signed overflow does not occur when "
13041 "determining that expression is always "
13042 "non-negative"),
13043 WARN_STRICT_OVERFLOW_MISC);
13044 return ret;
13048 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13049 For floating point we further ensure that T is not denormal.
13050 Similar logic is present in nonzero_address in rtlanal.h.
13052 If the return value is based on the assumption that signed overflow
13053 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13054 change *STRICT_OVERFLOW_P. */
13056 bool
13057 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13058 bool *strict_overflow_p)
13060 switch (code)
13062 case ABS_EXPR:
13063 return tree_expr_nonzero_warnv_p (op0,
13064 strict_overflow_p);
13066 case NOP_EXPR:
13068 tree inner_type = TREE_TYPE (op0);
13069 tree outer_type = type;
13071 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13072 && tree_expr_nonzero_warnv_p (op0,
13073 strict_overflow_p));
13075 break;
13077 case NON_LVALUE_EXPR:
13078 return tree_expr_nonzero_warnv_p (op0,
13079 strict_overflow_p);
13081 default:
13082 break;
13085 return false;
13088 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13089 For floating point we further ensure that T is not denormal.
13090 Similar logic is present in nonzero_address in rtlanal.h.
13092 If the return value is based on the assumption that signed overflow
13093 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13094 change *STRICT_OVERFLOW_P. */
13096 bool
13097 tree_binary_nonzero_warnv_p (enum tree_code code,
13098 tree type,
13099 tree op0,
13100 tree op1, bool *strict_overflow_p)
13102 bool sub_strict_overflow_p;
13103 switch (code)
13105 case POINTER_PLUS_EXPR:
13106 case PLUS_EXPR:
13107 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13109 /* With the presence of negative values it is hard
13110 to say something. */
13111 sub_strict_overflow_p = false;
13112 if (!tree_expr_nonnegative_warnv_p (op0,
13113 &sub_strict_overflow_p)
13114 || !tree_expr_nonnegative_warnv_p (op1,
13115 &sub_strict_overflow_p))
13116 return false;
13117 /* One of operands must be positive and the other non-negative. */
13118 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13119 overflows, on a twos-complement machine the sum of two
13120 nonnegative numbers can never be zero. */
13121 return (tree_expr_nonzero_warnv_p (op0,
13122 strict_overflow_p)
13123 || tree_expr_nonzero_warnv_p (op1,
13124 strict_overflow_p));
13126 break;
13128 case MULT_EXPR:
13129 if (TYPE_OVERFLOW_UNDEFINED (type))
13131 if (tree_expr_nonzero_warnv_p (op0,
13132 strict_overflow_p)
13133 && tree_expr_nonzero_warnv_p (op1,
13134 strict_overflow_p))
13136 *strict_overflow_p = true;
13137 return true;
13140 break;
13142 case MIN_EXPR:
13143 sub_strict_overflow_p = false;
13144 if (tree_expr_nonzero_warnv_p (op0,
13145 &sub_strict_overflow_p)
13146 && tree_expr_nonzero_warnv_p (op1,
13147 &sub_strict_overflow_p))
13149 if (sub_strict_overflow_p)
13150 *strict_overflow_p = true;
13152 break;
13154 case MAX_EXPR:
13155 sub_strict_overflow_p = false;
13156 if (tree_expr_nonzero_warnv_p (op0,
13157 &sub_strict_overflow_p))
13159 if (sub_strict_overflow_p)
13160 *strict_overflow_p = true;
13162 /* When both operands are nonzero, then MAX must be too. */
13163 if (tree_expr_nonzero_warnv_p (op1,
13164 strict_overflow_p))
13165 return true;
13167 /* MAX where operand 0 is positive is positive. */
13168 return tree_expr_nonnegative_warnv_p (op0,
13169 strict_overflow_p);
13171 /* MAX where operand 1 is positive is positive. */
13172 else if (tree_expr_nonzero_warnv_p (op1,
13173 &sub_strict_overflow_p)
13174 && tree_expr_nonnegative_warnv_p (op1,
13175 &sub_strict_overflow_p))
13177 if (sub_strict_overflow_p)
13178 *strict_overflow_p = true;
13179 return true;
13181 break;
13183 case BIT_IOR_EXPR:
13184 return (tree_expr_nonzero_warnv_p (op1,
13185 strict_overflow_p)
13186 || tree_expr_nonzero_warnv_p (op0,
13187 strict_overflow_p));
13189 default:
13190 break;
13193 return false;
13196 /* Return true when T is an address and is known to be nonzero.
13197 For floating point we further ensure that T is not denormal.
13198 Similar logic is present in nonzero_address in rtlanal.h.
13200 If the return value is based on the assumption that signed overflow
13201 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13202 change *STRICT_OVERFLOW_P. */
13204 bool
13205 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13207 bool sub_strict_overflow_p;
13208 switch (TREE_CODE (t))
13210 case INTEGER_CST:
13211 return !integer_zerop (t);
13213 case ADDR_EXPR:
13215 tree base = TREE_OPERAND (t, 0);
13217 if (!DECL_P (base))
13218 base = get_base_address (base);
13220 if (base && TREE_CODE (base) == TARGET_EXPR)
13221 base = TARGET_EXPR_SLOT (base);
13223 if (!base)
13224 return false;
13226 /* For objects in symbol table check if we know they are non-zero.
13227 Don't do anything for variables and functions before symtab is built;
13228 it is quite possible that they will be declared weak later. */
13229 int nonzero_addr = maybe_nonzero_address (base);
13230 if (nonzero_addr >= 0)
13231 return nonzero_addr;
13233 /* Constants are never weak. */
13234 if (CONSTANT_CLASS_P (base))
13235 return true;
13237 return false;
13240 case COND_EXPR:
13241 sub_strict_overflow_p = false;
13242 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13243 &sub_strict_overflow_p)
13244 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13245 &sub_strict_overflow_p))
13247 if (sub_strict_overflow_p)
13248 *strict_overflow_p = true;
13249 return true;
13251 break;
13253 case SSA_NAME:
13254 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13255 break;
13256 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13258 default:
13259 break;
13261 return false;
13264 #define integer_valued_real_p(X) \
13265 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13267 #define RECURSE(X) \
13268 ((integer_valued_real_p) (X, depth + 1))
13270 /* Return true if the floating point result of (CODE OP0) has an
13271 integer value. We also allow +Inf, -Inf and NaN to be considered
13272 integer values. Return false for signaling NaN.
13274 DEPTH is the current nesting depth of the query. */
13276 bool
13277 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13279 switch (code)
13281 case FLOAT_EXPR:
13282 return true;
13284 case ABS_EXPR:
13285 return RECURSE (op0);
13287 CASE_CONVERT:
13289 tree type = TREE_TYPE (op0);
13290 if (TREE_CODE (type) == INTEGER_TYPE)
13291 return true;
13292 if (TREE_CODE (type) == REAL_TYPE)
13293 return RECURSE (op0);
13294 break;
13297 default:
13298 break;
13300 return false;
13303 /* Return true if the floating point result of (CODE OP0 OP1) has an
13304 integer value. We also allow +Inf, -Inf and NaN to be considered
13305 integer values. Return false for signaling NaN.
13307 DEPTH is the current nesting depth of the query. */
13309 bool
13310 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13312 switch (code)
13314 case PLUS_EXPR:
13315 case MINUS_EXPR:
13316 case MULT_EXPR:
13317 case MIN_EXPR:
13318 case MAX_EXPR:
13319 return RECURSE (op0) && RECURSE (op1);
13321 default:
13322 break;
13324 return false;
13327 /* Return true if the floating point result of calling FNDECL with arguments
13328 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13329 considered integer values. Return false for signaling NaN. If FNDECL
13330 takes fewer than 2 arguments, the remaining ARGn are null.
13332 DEPTH is the current nesting depth of the query. */
13334 bool
13335 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13337 switch (fn)
13339 CASE_CFN_CEIL:
13340 CASE_CFN_FLOOR:
13341 CASE_CFN_NEARBYINT:
13342 CASE_CFN_RINT:
13343 CASE_CFN_ROUND:
13344 CASE_CFN_TRUNC:
13345 return true;
13347 CASE_CFN_FMIN:
13348 CASE_CFN_FMAX:
13349 return RECURSE (arg0) && RECURSE (arg1);
13351 default:
13352 break;
13354 return false;
13357 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13358 has an integer value. We also allow +Inf, -Inf and NaN to be
13359 considered integer values. Return false for signaling NaN.
13361 DEPTH is the current nesting depth of the query. */
13363 bool
13364 integer_valued_real_single_p (tree t, int depth)
13366 switch (TREE_CODE (t))
13368 case REAL_CST:
13369 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13371 case COND_EXPR:
13372 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13374 case SSA_NAME:
13375 /* Limit the depth of recursion to avoid quadratic behavior.
13376 This is expected to catch almost all occurrences in practice.
13377 If this code misses important cases that unbounded recursion
13378 would not, passes that need this information could be revised
13379 to provide it through dataflow propagation. */
13380 return (!name_registered_for_update_p (t)
13381 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13382 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13383 depth));
13385 default:
13386 break;
13388 return false;
13391 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13392 has an integer value. We also allow +Inf, -Inf and NaN to be
13393 considered integer values. Return false for signaling NaN.
13395 DEPTH is the current nesting depth of the query. */
13397 static bool
13398 integer_valued_real_invalid_p (tree t, int depth)
13400 switch (TREE_CODE (t))
13402 case COMPOUND_EXPR:
13403 case MODIFY_EXPR:
13404 case BIND_EXPR:
13405 return RECURSE (TREE_OPERAND (t, 1));
13407 case SAVE_EXPR:
13408 return RECURSE (TREE_OPERAND (t, 0));
13410 default:
13411 break;
13413 return false;
13416 #undef RECURSE
13417 #undef integer_valued_real_p
13419 /* Return true if the floating point expression T has an integer value.
13420 We also allow +Inf, -Inf and NaN to be considered integer values.
13421 Return false for signaling NaN.
13423 DEPTH is the current nesting depth of the query. */
13425 bool
13426 integer_valued_real_p (tree t, int depth)
13428 if (t == error_mark_node)
13429 return false;
13431 tree_code code = TREE_CODE (t);
13432 switch (TREE_CODE_CLASS (code))
13434 case tcc_binary:
13435 case tcc_comparison:
13436 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13437 TREE_OPERAND (t, 1), depth);
13439 case tcc_unary:
13440 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13442 case tcc_constant:
13443 case tcc_declaration:
13444 case tcc_reference:
13445 return integer_valued_real_single_p (t, depth);
13447 default:
13448 break;
13451 switch (code)
13453 case COND_EXPR:
13454 case SSA_NAME:
13455 return integer_valued_real_single_p (t, depth);
13457 case CALL_EXPR:
13459 tree arg0 = (call_expr_nargs (t) > 0
13460 ? CALL_EXPR_ARG (t, 0)
13461 : NULL_TREE);
13462 tree arg1 = (call_expr_nargs (t) > 1
13463 ? CALL_EXPR_ARG (t, 1)
13464 : NULL_TREE);
13465 return integer_valued_real_call_p (get_call_combined_fn (t),
13466 arg0, arg1, depth);
13469 default:
13470 return integer_valued_real_invalid_p (t, depth);
13474 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13475 attempt to fold the expression to a constant without modifying TYPE,
13476 OP0 or OP1.
13478 If the expression could be simplified to a constant, then return
13479 the constant. If the expression would not be simplified to a
13480 constant, then return NULL_TREE. */
13482 tree
13483 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13485 tree tem = fold_binary (code, type, op0, op1);
13486 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13489 /* Given the components of a unary expression CODE, TYPE and OP0,
13490 attempt to fold the expression to a constant without modifying
13491 TYPE or OP0.
13493 If the expression could be simplified to a constant, then return
13494 the constant. If the expression would not be simplified to a
13495 constant, then return NULL_TREE. */
13497 tree
13498 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13500 tree tem = fold_unary (code, type, op0);
13501 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13504 /* If EXP represents referencing an element in a constant string
13505 (either via pointer arithmetic or array indexing), return the
13506 tree representing the value accessed, otherwise return NULL. */
13508 tree
13509 fold_read_from_constant_string (tree exp)
13511 if ((TREE_CODE (exp) == INDIRECT_REF
13512 || TREE_CODE (exp) == ARRAY_REF)
13513 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13515 tree exp1 = TREE_OPERAND (exp, 0);
13516 tree index;
13517 tree string;
13518 location_t loc = EXPR_LOCATION (exp);
13520 if (TREE_CODE (exp) == INDIRECT_REF)
13521 string = string_constant (exp1, &index);
13522 else
13524 tree low_bound = array_ref_low_bound (exp);
13525 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13527 /* Optimize the special-case of a zero lower bound.
13529 We convert the low_bound to sizetype to avoid some problems
13530 with constant folding. (E.g. suppose the lower bound is 1,
13531 and its mode is QI. Without the conversion,l (ARRAY
13532 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13533 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13534 if (! integer_zerop (low_bound))
13535 index = size_diffop_loc (loc, index,
13536 fold_convert_loc (loc, sizetype, low_bound));
13538 string = exp1;
13541 scalar_int_mode char_mode;
13542 if (string
13543 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13544 && TREE_CODE (string) == STRING_CST
13545 && TREE_CODE (index) == INTEGER_CST
13546 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13547 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13548 &char_mode)
13549 && GET_MODE_SIZE (char_mode) == 1)
13550 return build_int_cst_type (TREE_TYPE (exp),
13551 (TREE_STRING_POINTER (string)
13552 [TREE_INT_CST_LOW (index)]));
13554 return NULL;
13557 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13558 an integer constant, real, or fixed-point constant.
13560 TYPE is the type of the result. */
13562 static tree
13563 fold_negate_const (tree arg0, tree type)
13565 tree t = NULL_TREE;
13567 switch (TREE_CODE (arg0))
13569 case INTEGER_CST:
13571 bool overflow;
13572 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13573 t = force_fit_type (type, val, 1,
13574 (overflow && ! TYPE_UNSIGNED (type))
13575 || TREE_OVERFLOW (arg0));
13576 break;
13579 case REAL_CST:
13580 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13581 break;
13583 case FIXED_CST:
13585 FIXED_VALUE_TYPE f;
13586 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13587 &(TREE_FIXED_CST (arg0)), NULL,
13588 TYPE_SATURATING (type));
13589 t = build_fixed (type, f);
13590 /* Propagate overflow flags. */
13591 if (overflow_p | TREE_OVERFLOW (arg0))
13592 TREE_OVERFLOW (t) = 1;
13593 break;
13596 default:
13597 gcc_unreachable ();
13600 return t;
13603 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13604 an integer constant or real constant.
13606 TYPE is the type of the result. */
13608 tree
13609 fold_abs_const (tree arg0, tree type)
13611 tree t = NULL_TREE;
13613 switch (TREE_CODE (arg0))
13615 case INTEGER_CST:
13617 /* If the value is unsigned or non-negative, then the absolute value
13618 is the same as the ordinary value. */
13619 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13620 t = arg0;
13622 /* If the value is negative, then the absolute value is
13623 its negation. */
13624 else
13626 bool overflow;
13627 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13628 t = force_fit_type (type, val, -1,
13629 overflow | TREE_OVERFLOW (arg0));
13632 break;
13634 case REAL_CST:
13635 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13636 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13637 else
13638 t = arg0;
13639 break;
13641 default:
13642 gcc_unreachable ();
13645 return t;
13648 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13649 constant. TYPE is the type of the result. */
13651 static tree
13652 fold_not_const (const_tree arg0, tree type)
13654 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13656 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13659 /* Given CODE, a relational operator, the target type, TYPE and two
13660 constant operands OP0 and OP1, return the result of the
13661 relational operation. If the result is not a compile time
13662 constant, then return NULL_TREE. */
13664 static tree
13665 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13667 int result, invert;
13669 /* From here on, the only cases we handle are when the result is
13670 known to be a constant. */
13672 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13674 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13675 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13677 /* Handle the cases where either operand is a NaN. */
13678 if (real_isnan (c0) || real_isnan (c1))
13680 switch (code)
13682 case EQ_EXPR:
13683 case ORDERED_EXPR:
13684 result = 0;
13685 break;
13687 case NE_EXPR:
13688 case UNORDERED_EXPR:
13689 case UNLT_EXPR:
13690 case UNLE_EXPR:
13691 case UNGT_EXPR:
13692 case UNGE_EXPR:
13693 case UNEQ_EXPR:
13694 result = 1;
13695 break;
13697 case LT_EXPR:
13698 case LE_EXPR:
13699 case GT_EXPR:
13700 case GE_EXPR:
13701 case LTGT_EXPR:
13702 if (flag_trapping_math)
13703 return NULL_TREE;
13704 result = 0;
13705 break;
13707 default:
13708 gcc_unreachable ();
13711 return constant_boolean_node (result, type);
13714 return constant_boolean_node (real_compare (code, c0, c1), type);
13717 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13719 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13720 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13721 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13724 /* Handle equality/inequality of complex constants. */
13725 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13727 tree rcond = fold_relational_const (code, type,
13728 TREE_REALPART (op0),
13729 TREE_REALPART (op1));
13730 tree icond = fold_relational_const (code, type,
13731 TREE_IMAGPART (op0),
13732 TREE_IMAGPART (op1));
13733 if (code == EQ_EXPR)
13734 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13735 else if (code == NE_EXPR)
13736 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13737 else
13738 return NULL_TREE;
13741 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13743 if (!VECTOR_TYPE_P (type))
13745 /* Have vector comparison with scalar boolean result. */
13746 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13747 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13748 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13750 tree elem0 = VECTOR_CST_ELT (op0, i);
13751 tree elem1 = VECTOR_CST_ELT (op1, i);
13752 tree tmp = fold_relational_const (code, type, elem0, elem1);
13753 if (tmp == NULL_TREE)
13754 return NULL_TREE;
13755 if (integer_zerop (tmp))
13756 return constant_boolean_node (false, type);
13758 return constant_boolean_node (true, type);
13760 unsigned count = VECTOR_CST_NELTS (op0);
13761 gcc_assert (VECTOR_CST_NELTS (op1) == count
13762 && TYPE_VECTOR_SUBPARTS (type) == count);
13764 auto_vec<tree, 32> elts (count);
13765 for (unsigned i = 0; i < count; i++)
13767 tree elem_type = TREE_TYPE (type);
13768 tree elem0 = VECTOR_CST_ELT (op0, i);
13769 tree elem1 = VECTOR_CST_ELT (op1, i);
13771 tree tem = fold_relational_const (code, elem_type,
13772 elem0, elem1);
13774 if (tem == NULL_TREE)
13775 return NULL_TREE;
13777 elts.quick_push (build_int_cst (elem_type,
13778 integer_zerop (tem) ? 0 : -1));
13781 return build_vector (type, elts);
13784 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13786 To compute GT, swap the arguments and do LT.
13787 To compute GE, do LT and invert the result.
13788 To compute LE, swap the arguments, do LT and invert the result.
13789 To compute NE, do EQ and invert the result.
13791 Therefore, the code below must handle only EQ and LT. */
13793 if (code == LE_EXPR || code == GT_EXPR)
13795 std::swap (op0, op1);
13796 code = swap_tree_comparison (code);
13799 /* Note that it is safe to invert for real values here because we
13800 have already handled the one case that it matters. */
13802 invert = 0;
13803 if (code == NE_EXPR || code == GE_EXPR)
13805 invert = 1;
13806 code = invert_tree_comparison (code, false);
13809 /* Compute a result for LT or EQ if args permit;
13810 Otherwise return T. */
13811 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13813 if (code == EQ_EXPR)
13814 result = tree_int_cst_equal (op0, op1);
13815 else
13816 result = tree_int_cst_lt (op0, op1);
13818 else
13819 return NULL_TREE;
13821 if (invert)
13822 result ^= 1;
13823 return constant_boolean_node (result, type);
13826 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13827 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13828 itself. */
13830 tree
13831 fold_build_cleanup_point_expr (tree type, tree expr)
13833 /* If the expression does not have side effects then we don't have to wrap
13834 it with a cleanup point expression. */
13835 if (!TREE_SIDE_EFFECTS (expr))
13836 return expr;
13838 /* If the expression is a return, check to see if the expression inside the
13839 return has no side effects or the right hand side of the modify expression
13840 inside the return. If either don't have side effects set we don't need to
13841 wrap the expression in a cleanup point expression. Note we don't check the
13842 left hand side of the modify because it should always be a return decl. */
13843 if (TREE_CODE (expr) == RETURN_EXPR)
13845 tree op = TREE_OPERAND (expr, 0);
13846 if (!op || !TREE_SIDE_EFFECTS (op))
13847 return expr;
13848 op = TREE_OPERAND (op, 1);
13849 if (!TREE_SIDE_EFFECTS (op))
13850 return expr;
13853 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
13856 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13857 of an indirection through OP0, or NULL_TREE if no simplification is
13858 possible. */
13860 tree
13861 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13863 tree sub = op0;
13864 tree subtype;
13866 STRIP_NOPS (sub);
13867 subtype = TREE_TYPE (sub);
13868 if (!POINTER_TYPE_P (subtype)
13869 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
13870 return NULL_TREE;
13872 if (TREE_CODE (sub) == ADDR_EXPR)
13874 tree op = TREE_OPERAND (sub, 0);
13875 tree optype = TREE_TYPE (op);
13876 /* *&CONST_DECL -> to the value of the const decl. */
13877 if (TREE_CODE (op) == CONST_DECL)
13878 return DECL_INITIAL (op);
13879 /* *&p => p; make sure to handle *&"str"[cst] here. */
13880 if (type == optype)
13882 tree fop = fold_read_from_constant_string (op);
13883 if (fop)
13884 return fop;
13885 else
13886 return op;
13888 /* *(foo *)&fooarray => fooarray[0] */
13889 else if (TREE_CODE (optype) == ARRAY_TYPE
13890 && type == TREE_TYPE (optype)
13891 && (!in_gimple_form
13892 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13894 tree type_domain = TYPE_DOMAIN (optype);
13895 tree min_val = size_zero_node;
13896 if (type_domain && TYPE_MIN_VALUE (type_domain))
13897 min_val = TYPE_MIN_VALUE (type_domain);
13898 if (in_gimple_form
13899 && TREE_CODE (min_val) != INTEGER_CST)
13900 return NULL_TREE;
13901 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13902 NULL_TREE, NULL_TREE);
13904 /* *(foo *)&complexfoo => __real__ complexfoo */
13905 else if (TREE_CODE (optype) == COMPLEX_TYPE
13906 && type == TREE_TYPE (optype))
13907 return fold_build1_loc (loc, REALPART_EXPR, type, op);
13908 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
13909 else if (TREE_CODE (optype) == VECTOR_TYPE
13910 && type == TREE_TYPE (optype))
13912 tree part_width = TYPE_SIZE (type);
13913 tree index = bitsize_int (0);
13914 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
13918 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
13919 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
13921 tree op00 = TREE_OPERAND (sub, 0);
13922 tree op01 = TREE_OPERAND (sub, 1);
13924 STRIP_NOPS (op00);
13925 if (TREE_CODE (op00) == ADDR_EXPR)
13927 tree op00type;
13928 op00 = TREE_OPERAND (op00, 0);
13929 op00type = TREE_TYPE (op00);
13931 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
13932 if (TREE_CODE (op00type) == VECTOR_TYPE
13933 && type == TREE_TYPE (op00type))
13935 tree part_width = TYPE_SIZE (type);
13936 unsigned HOST_WIDE_INT max_offset
13937 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
13938 * TYPE_VECTOR_SUBPARTS (op00type));
13939 if (tree_int_cst_sign_bit (op01) == 0
13940 && compare_tree_int (op01, max_offset) == -1)
13942 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
13943 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
13944 tree index = bitsize_int (indexi);
13945 return fold_build3_loc (loc,
13946 BIT_FIELD_REF, type, op00,
13947 part_width, index);
13950 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
13951 else if (TREE_CODE (op00type) == COMPLEX_TYPE
13952 && type == TREE_TYPE (op00type))
13954 tree size = TYPE_SIZE_UNIT (type);
13955 if (tree_int_cst_equal (size, op01))
13956 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
13958 /* ((foo *)&fooarray)[1] => fooarray[1] */
13959 else if (TREE_CODE (op00type) == ARRAY_TYPE
13960 && type == TREE_TYPE (op00type))
13962 tree type_domain = TYPE_DOMAIN (op00type);
13963 tree min = size_zero_node;
13964 if (type_domain && TYPE_MIN_VALUE (type_domain))
13965 min = TYPE_MIN_VALUE (type_domain);
13966 offset_int off = wi::to_offset (op01);
13967 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
13968 offset_int remainder;
13969 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
13970 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
13972 off = off + wi::to_offset (min);
13973 op01 = wide_int_to_tree (sizetype, off);
13974 return build4_loc (loc, ARRAY_REF, type, op00, op01,
13975 NULL_TREE, NULL_TREE);
13981 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
13982 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
13983 && type == TREE_TYPE (TREE_TYPE (subtype))
13984 && (!in_gimple_form
13985 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13987 tree type_domain;
13988 tree min_val = size_zero_node;
13989 sub = build_fold_indirect_ref_loc (loc, sub);
13990 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
13991 if (type_domain && TYPE_MIN_VALUE (type_domain))
13992 min_val = TYPE_MIN_VALUE (type_domain);
13993 if (in_gimple_form
13994 && TREE_CODE (min_val) != INTEGER_CST)
13995 return NULL_TREE;
13996 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
13997 NULL_TREE);
14000 return NULL_TREE;
14003 /* Builds an expression for an indirection through T, simplifying some
14004 cases. */
14006 tree
14007 build_fold_indirect_ref_loc (location_t loc, tree t)
14009 tree type = TREE_TYPE (TREE_TYPE (t));
14010 tree sub = fold_indirect_ref_1 (loc, type, t);
14012 if (sub)
14013 return sub;
14015 return build1_loc (loc, INDIRECT_REF, type, t);
14018 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14020 tree
14021 fold_indirect_ref_loc (location_t loc, tree t)
14023 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14025 if (sub)
14026 return sub;
14027 else
14028 return t;
14031 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14032 whose result is ignored. The type of the returned tree need not be
14033 the same as the original expression. */
14035 tree
14036 fold_ignored_result (tree t)
14038 if (!TREE_SIDE_EFFECTS (t))
14039 return integer_zero_node;
14041 for (;;)
14042 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14044 case tcc_unary:
14045 t = TREE_OPERAND (t, 0);
14046 break;
14048 case tcc_binary:
14049 case tcc_comparison:
14050 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14051 t = TREE_OPERAND (t, 0);
14052 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14053 t = TREE_OPERAND (t, 1);
14054 else
14055 return t;
14056 break;
14058 case tcc_expression:
14059 switch (TREE_CODE (t))
14061 case COMPOUND_EXPR:
14062 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14063 return t;
14064 t = TREE_OPERAND (t, 0);
14065 break;
14067 case COND_EXPR:
14068 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14069 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14070 return t;
14071 t = TREE_OPERAND (t, 0);
14072 break;
14074 default:
14075 return t;
14077 break;
14079 default:
14080 return t;
14084 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14086 tree
14087 round_up_loc (location_t loc, tree value, unsigned int divisor)
14089 tree div = NULL_TREE;
14091 if (divisor == 1)
14092 return value;
14094 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14095 have to do anything. Only do this when we are not given a const,
14096 because in that case, this check is more expensive than just
14097 doing it. */
14098 if (TREE_CODE (value) != INTEGER_CST)
14100 div = build_int_cst (TREE_TYPE (value), divisor);
14102 if (multiple_of_p (TREE_TYPE (value), value, div))
14103 return value;
14106 /* If divisor is a power of two, simplify this to bit manipulation. */
14107 if (pow2_or_zerop (divisor))
14109 if (TREE_CODE (value) == INTEGER_CST)
14111 wide_int val = wi::to_wide (value);
14112 bool overflow_p;
14114 if ((val & (divisor - 1)) == 0)
14115 return value;
14117 overflow_p = TREE_OVERFLOW (value);
14118 val += divisor - 1;
14119 val &= (int) -divisor;
14120 if (val == 0)
14121 overflow_p = true;
14123 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14125 else
14127 tree t;
14129 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14130 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14131 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14132 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14135 else
14137 if (!div)
14138 div = build_int_cst (TREE_TYPE (value), divisor);
14139 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14140 value = size_binop_loc (loc, MULT_EXPR, value, div);
14143 return value;
14146 /* Likewise, but round down. */
14148 tree
14149 round_down_loc (location_t loc, tree value, int divisor)
14151 tree div = NULL_TREE;
14153 gcc_assert (divisor > 0);
14154 if (divisor == 1)
14155 return value;
14157 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14158 have to do anything. Only do this when we are not given a const,
14159 because in that case, this check is more expensive than just
14160 doing it. */
14161 if (TREE_CODE (value) != INTEGER_CST)
14163 div = build_int_cst (TREE_TYPE (value), divisor);
14165 if (multiple_of_p (TREE_TYPE (value), value, div))
14166 return value;
14169 /* If divisor is a power of two, simplify this to bit manipulation. */
14170 if (pow2_or_zerop (divisor))
14172 tree t;
14174 t = build_int_cst (TREE_TYPE (value), -divisor);
14175 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14177 else
14179 if (!div)
14180 div = build_int_cst (TREE_TYPE (value), divisor);
14181 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14182 value = size_binop_loc (loc, MULT_EXPR, value, div);
14185 return value;
14188 /* Returns the pointer to the base of the object addressed by EXP and
14189 extracts the information about the offset of the access, storing it
14190 to PBITPOS and POFFSET. */
14192 static tree
14193 split_address_to_core_and_offset (tree exp,
14194 HOST_WIDE_INT *pbitpos, tree *poffset)
14196 tree core;
14197 machine_mode mode;
14198 int unsignedp, reversep, volatilep;
14199 HOST_WIDE_INT bitsize;
14200 location_t loc = EXPR_LOCATION (exp);
14202 if (TREE_CODE (exp) == ADDR_EXPR)
14204 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14205 poffset, &mode, &unsignedp, &reversep,
14206 &volatilep);
14207 core = build_fold_addr_expr_loc (loc, core);
14209 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14211 core = TREE_OPERAND (exp, 0);
14212 STRIP_NOPS (core);
14213 *pbitpos = 0;
14214 *poffset = TREE_OPERAND (exp, 1);
14215 if (TREE_CODE (*poffset) == INTEGER_CST)
14217 offset_int tem = wi::sext (wi::to_offset (*poffset),
14218 TYPE_PRECISION (TREE_TYPE (*poffset)));
14219 tem <<= LOG2_BITS_PER_UNIT;
14220 if (wi::fits_shwi_p (tem))
14222 *pbitpos = tem.to_shwi ();
14223 *poffset = NULL_TREE;
14227 else
14229 core = exp;
14230 *pbitpos = 0;
14231 *poffset = NULL_TREE;
14234 return core;
14237 /* Returns true if addresses of E1 and E2 differ by a constant, false
14238 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14240 bool
14241 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14243 tree core1, core2;
14244 HOST_WIDE_INT bitpos1, bitpos2;
14245 tree toffset1, toffset2, tdiff, type;
14247 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14248 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14250 if (bitpos1 % BITS_PER_UNIT != 0
14251 || bitpos2 % BITS_PER_UNIT != 0
14252 || !operand_equal_p (core1, core2, 0))
14253 return false;
14255 if (toffset1 && toffset2)
14257 type = TREE_TYPE (toffset1);
14258 if (type != TREE_TYPE (toffset2))
14259 toffset2 = fold_convert (type, toffset2);
14261 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14262 if (!cst_and_fits_in_hwi (tdiff))
14263 return false;
14265 *diff = int_cst_value (tdiff);
14267 else if (toffset1 || toffset2)
14269 /* If only one of the offsets is non-constant, the difference cannot
14270 be a constant. */
14271 return false;
14273 else
14274 *diff = 0;
14276 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14277 return true;
14280 /* Return OFF converted to a pointer offset type suitable as offset for
14281 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14282 tree
14283 convert_to_ptrofftype_loc (location_t loc, tree off)
14285 return fold_convert_loc (loc, sizetype, off);
14288 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14289 tree
14290 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14292 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14293 ptr, convert_to_ptrofftype_loc (loc, off));
14296 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14297 tree
14298 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14300 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14301 ptr, size_int (off));
14304 /* Return a char pointer for a C string if it is a string constant
14305 or sum of string constant and integer constant. We only support
14306 string constants properly terminated with '\0' character.
14307 If STRLEN is a valid pointer, length (including terminating character)
14308 of returned string is stored to the argument. */
14310 const char *
14311 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14313 tree offset_node;
14315 if (strlen)
14316 *strlen = 0;
14318 src = string_constant (src, &offset_node);
14319 if (src == 0)
14320 return NULL;
14322 unsigned HOST_WIDE_INT offset = 0;
14323 if (offset_node != NULL_TREE)
14325 if (!tree_fits_uhwi_p (offset_node))
14326 return NULL;
14327 else
14328 offset = tree_to_uhwi (offset_node);
14331 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14332 const char *string = TREE_STRING_POINTER (src);
14334 /* Support only properly null-terminated strings. */
14335 if (string_length == 0
14336 || string[string_length - 1] != '\0'
14337 || offset >= string_length)
14338 return NULL;
14340 if (strlen)
14341 *strlen = string_length - offset;
14342 return string + offset;
14345 #if CHECKING_P
14347 namespace selftest {
14349 /* Helper functions for writing tests of folding trees. */
14351 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14353 static void
14354 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14355 tree constant)
14357 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14360 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14361 wrapping WRAPPED_EXPR. */
14363 static void
14364 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14365 tree wrapped_expr)
14367 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14368 ASSERT_NE (wrapped_expr, result);
14369 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14370 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14373 /* Verify that various arithmetic binary operations are folded
14374 correctly. */
14376 static void
14377 test_arithmetic_folding ()
14379 tree type = integer_type_node;
14380 tree x = create_tmp_var_raw (type, "x");
14381 tree zero = build_zero_cst (type);
14382 tree one = build_int_cst (type, 1);
14384 /* Addition. */
14385 /* 1 <-- (0 + 1) */
14386 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14387 one);
14388 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14389 one);
14391 /* (nonlvalue)x <-- (x + 0) */
14392 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14395 /* Subtraction. */
14396 /* 0 <-- (x - x) */
14397 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14398 zero);
14399 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14402 /* Multiplication. */
14403 /* 0 <-- (x * 0) */
14404 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14405 zero);
14407 /* (nonlvalue)x <-- (x * 1) */
14408 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14412 /* Verify that various binary operations on vectors are folded
14413 correctly. */
14415 static void
14416 test_vector_folding ()
14418 tree inner_type = integer_type_node;
14419 tree type = build_vector_type (inner_type, 4);
14420 tree zero = build_zero_cst (type);
14421 tree one = build_one_cst (type);
14423 /* Verify equality tests that return a scalar boolean result. */
14424 tree res_type = boolean_type_node;
14425 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14426 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14427 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14428 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14431 /* Run all of the selftests within this file. */
14433 void
14434 fold_const_c_tests ()
14436 test_arithmetic_folding ();
14437 test_vector_folding ();
14440 } // namespace selftest
14442 #endif /* CHECKING_P */