PR target/82524
[official-gcc.git] / gcc / fold-const.c
blobd1e61918e0b70ef350fd24c0a70220f03bbfa18b
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.
9433 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9434 though in this case CODE must be | and not + or ^, otherwise
9435 it doesn't return A when B is 0. */
9437 enum tree_code code0, code1;
9438 tree rtype;
9439 code0 = TREE_CODE (arg0);
9440 code1 = TREE_CODE (arg1);
9441 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9442 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9443 && operand_equal_p (TREE_OPERAND (arg0, 0),
9444 TREE_OPERAND (arg1, 0), 0)
9445 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9446 TYPE_UNSIGNED (rtype))
9447 /* Only create rotates in complete modes. Other cases are not
9448 expanded properly. */
9449 && (element_precision (rtype)
9450 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9452 tree tree01, tree11;
9453 tree orig_tree01, orig_tree11;
9454 enum tree_code code01, code11;
9456 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9457 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9458 STRIP_NOPS (tree01);
9459 STRIP_NOPS (tree11);
9460 code01 = TREE_CODE (tree01);
9461 code11 = TREE_CODE (tree11);
9462 if (code11 != MINUS_EXPR
9463 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9465 std::swap (code0, code1);
9466 std::swap (code01, code11);
9467 std::swap (tree01, tree11);
9468 std::swap (orig_tree01, orig_tree11);
9470 if (code01 == INTEGER_CST
9471 && code11 == INTEGER_CST
9472 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9473 == element_precision (rtype)))
9475 tem = build2_loc (loc, LROTATE_EXPR,
9476 rtype, TREE_OPERAND (arg0, 0),
9477 code0 == LSHIFT_EXPR
9478 ? orig_tree01 : orig_tree11);
9479 return fold_convert_loc (loc, type, tem);
9481 else if (code11 == MINUS_EXPR)
9483 tree tree110, tree111;
9484 tree110 = TREE_OPERAND (tree11, 0);
9485 tree111 = TREE_OPERAND (tree11, 1);
9486 STRIP_NOPS (tree110);
9487 STRIP_NOPS (tree111);
9488 if (TREE_CODE (tree110) == INTEGER_CST
9489 && 0 == compare_tree_int (tree110,
9490 element_precision (rtype))
9491 && operand_equal_p (tree01, tree111, 0))
9493 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9494 ? LROTATE_EXPR : RROTATE_EXPR),
9495 rtype, TREE_OPERAND (arg0, 0),
9496 orig_tree01);
9497 return fold_convert_loc (loc, type, tem);
9500 else if (code == BIT_IOR_EXPR
9501 && code11 == BIT_AND_EXPR
9502 && pow2p_hwi (element_precision (rtype)))
9504 tree tree110, tree111;
9505 tree110 = TREE_OPERAND (tree11, 0);
9506 tree111 = TREE_OPERAND (tree11, 1);
9507 STRIP_NOPS (tree110);
9508 STRIP_NOPS (tree111);
9509 if (TREE_CODE (tree110) == NEGATE_EXPR
9510 && TREE_CODE (tree111) == INTEGER_CST
9511 && 0 == compare_tree_int (tree111,
9512 element_precision (rtype) - 1)
9513 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9515 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9516 ? LROTATE_EXPR : RROTATE_EXPR),
9517 rtype, TREE_OPERAND (arg0, 0),
9518 orig_tree01);
9519 return fold_convert_loc (loc, type, tem);
9525 associate:
9526 /* In most languages, can't associate operations on floats through
9527 parentheses. Rather than remember where the parentheses were, we
9528 don't associate floats at all, unless the user has specified
9529 -fassociative-math.
9530 And, we need to make sure type is not saturating. */
9532 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9533 && !TYPE_SATURATING (type))
9535 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9536 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9537 tree atype = type;
9538 bool ok = true;
9540 /* Split both trees into variables, constants, and literals. Then
9541 associate each group together, the constants with literals,
9542 then the result with variables. This increases the chances of
9543 literals being recombined later and of generating relocatable
9544 expressions for the sum of a constant and literal. */
9545 var0 = split_tree (arg0, type, code,
9546 &minus_var0, &con0, &minus_con0,
9547 &lit0, &minus_lit0, 0);
9548 var1 = split_tree (arg1, type, code,
9549 &minus_var1, &con1, &minus_con1,
9550 &lit1, &minus_lit1, code == MINUS_EXPR);
9552 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9553 if (code == MINUS_EXPR)
9554 code = PLUS_EXPR;
9556 /* With undefined overflow prefer doing association in a type
9557 which wraps on overflow, if that is one of the operand types. */
9558 if (POINTER_TYPE_P (type)
9559 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9561 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9562 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9563 atype = TREE_TYPE (arg0);
9564 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9565 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9566 atype = TREE_TYPE (arg1);
9567 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9570 /* With undefined overflow we can only associate constants with one
9571 variable, and constants whose association doesn't overflow. */
9572 if (POINTER_TYPE_P (atype)
9573 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9575 if ((var0 && var1) || (minus_var0 && minus_var1))
9577 /* ??? If split_tree would handle NEGATE_EXPR we could
9578 simply reject these cases and the allowed cases would
9579 be the var0/minus_var1 ones. */
9580 tree tmp0 = var0 ? var0 : minus_var0;
9581 tree tmp1 = var1 ? var1 : minus_var1;
9582 bool one_neg = false;
9584 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9586 tmp0 = TREE_OPERAND (tmp0, 0);
9587 one_neg = !one_neg;
9589 if (CONVERT_EXPR_P (tmp0)
9590 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9591 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9592 <= TYPE_PRECISION (atype)))
9593 tmp0 = TREE_OPERAND (tmp0, 0);
9594 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9596 tmp1 = TREE_OPERAND (tmp1, 0);
9597 one_neg = !one_neg;
9599 if (CONVERT_EXPR_P (tmp1)
9600 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9601 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9602 <= TYPE_PRECISION (atype)))
9603 tmp1 = TREE_OPERAND (tmp1, 0);
9604 /* The only case we can still associate with two variables
9605 is if they cancel out. */
9606 if (!one_neg
9607 || !operand_equal_p (tmp0, tmp1, 0))
9608 ok = false;
9610 else if ((var0 && minus_var1
9611 && ! operand_equal_p (var0, minus_var1, 0))
9612 || (minus_var0 && var1
9613 && ! operand_equal_p (minus_var0, var1, 0)))
9614 ok = false;
9617 /* Only do something if we found more than two objects. Otherwise,
9618 nothing has changed and we risk infinite recursion. */
9619 if (ok
9620 && (2 < ((var0 != 0) + (var1 != 0)
9621 + (minus_var0 != 0) + (minus_var1 != 0)
9622 + (con0 != 0) + (con1 != 0)
9623 + (minus_con0 != 0) + (minus_con1 != 0)
9624 + (lit0 != 0) + (lit1 != 0)
9625 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9627 var0 = associate_trees (loc, var0, var1, code, atype);
9628 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9629 code, atype);
9630 con0 = associate_trees (loc, con0, con1, code, atype);
9631 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9632 code, atype);
9633 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9634 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9635 code, atype);
9637 if (minus_var0 && var0)
9639 var0 = associate_trees (loc, var0, minus_var0,
9640 MINUS_EXPR, atype);
9641 minus_var0 = 0;
9643 if (minus_con0 && con0)
9645 con0 = associate_trees (loc, con0, minus_con0,
9646 MINUS_EXPR, atype);
9647 minus_con0 = 0;
9650 /* Preserve the MINUS_EXPR if the negative part of the literal is
9651 greater than the positive part. Otherwise, the multiplicative
9652 folding code (i.e extract_muldiv) may be fooled in case
9653 unsigned constants are subtracted, like in the following
9654 example: ((X*2 + 4) - 8U)/2. */
9655 if (minus_lit0 && lit0)
9657 if (TREE_CODE (lit0) == INTEGER_CST
9658 && TREE_CODE (minus_lit0) == INTEGER_CST
9659 && tree_int_cst_lt (lit0, minus_lit0)
9660 /* But avoid ending up with only negated parts. */
9661 && (var0 || con0))
9663 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9664 MINUS_EXPR, atype);
9665 lit0 = 0;
9667 else
9669 lit0 = associate_trees (loc, lit0, minus_lit0,
9670 MINUS_EXPR, atype);
9671 minus_lit0 = 0;
9675 /* Don't introduce overflows through reassociation. */
9676 if ((lit0 && TREE_OVERFLOW_P (lit0))
9677 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9678 return NULL_TREE;
9680 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9681 con0 = associate_trees (loc, con0, lit0, code, atype);
9682 lit0 = 0;
9683 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9684 code, atype);
9685 minus_lit0 = 0;
9687 /* Eliminate minus_con0. */
9688 if (minus_con0)
9690 if (con0)
9691 con0 = associate_trees (loc, con0, minus_con0,
9692 MINUS_EXPR, atype);
9693 else if (var0)
9694 var0 = associate_trees (loc, var0, minus_con0,
9695 MINUS_EXPR, atype);
9696 else
9697 gcc_unreachable ();
9698 minus_con0 = 0;
9701 /* Eliminate minus_var0. */
9702 if (minus_var0)
9704 if (con0)
9705 con0 = associate_trees (loc, con0, minus_var0,
9706 MINUS_EXPR, atype);
9707 else
9708 gcc_unreachable ();
9709 minus_var0 = 0;
9712 return
9713 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9714 code, atype));
9718 return NULL_TREE;
9720 case MINUS_EXPR:
9721 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9722 if (TREE_CODE (arg0) == NEGATE_EXPR
9723 && negate_expr_p (op1))
9724 return fold_build2_loc (loc, MINUS_EXPR, type,
9725 negate_expr (op1),
9726 fold_convert_loc (loc, type,
9727 TREE_OPERAND (arg0, 0)));
9729 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9730 __complex__ ( x, -y ). This is not the same for SNaNs or if
9731 signed zeros are involved. */
9732 if (!HONOR_SNANS (element_mode (arg0))
9733 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9734 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9736 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9737 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9738 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9739 bool arg0rz = false, arg0iz = false;
9740 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9741 || (arg0i && (arg0iz = real_zerop (arg0i))))
9743 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9744 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9745 if (arg0rz && arg1i && real_zerop (arg1i))
9747 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9748 arg1r ? arg1r
9749 : build1 (REALPART_EXPR, rtype, arg1));
9750 tree ip = arg0i ? arg0i
9751 : build1 (IMAGPART_EXPR, rtype, arg0);
9752 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9754 else if (arg0iz && arg1r && real_zerop (arg1r))
9756 tree rp = arg0r ? arg0r
9757 : build1 (REALPART_EXPR, rtype, arg0);
9758 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9759 arg1i ? arg1i
9760 : build1 (IMAGPART_EXPR, rtype, arg1));
9761 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9766 /* A - B -> A + (-B) if B is easily negatable. */
9767 if (negate_expr_p (op1)
9768 && ! TYPE_OVERFLOW_SANITIZED (type)
9769 && ((FLOAT_TYPE_P (type)
9770 /* Avoid this transformation if B is a positive REAL_CST. */
9771 && (TREE_CODE (op1) != REAL_CST
9772 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9773 || INTEGRAL_TYPE_P (type)))
9774 return fold_build2_loc (loc, PLUS_EXPR, type,
9775 fold_convert_loc (loc, type, arg0),
9776 negate_expr (op1));
9778 /* Fold &a[i] - &a[j] to i-j. */
9779 if (TREE_CODE (arg0) == ADDR_EXPR
9780 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9781 && TREE_CODE (arg1) == ADDR_EXPR
9782 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9784 tree tem = fold_addr_of_array_ref_difference (loc, type,
9785 TREE_OPERAND (arg0, 0),
9786 TREE_OPERAND (arg1, 0));
9787 if (tem)
9788 return tem;
9791 if (FLOAT_TYPE_P (type)
9792 && flag_unsafe_math_optimizations
9793 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9794 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9795 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9796 return tem;
9798 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9799 one. Make sure the type is not saturating and has the signedness of
9800 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9801 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9802 if ((TREE_CODE (arg0) == MULT_EXPR
9803 || TREE_CODE (arg1) == MULT_EXPR)
9804 && !TYPE_SATURATING (type)
9805 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9806 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9807 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9809 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9810 if (tem)
9811 return tem;
9814 goto associate;
9816 case MULT_EXPR:
9817 if (! FLOAT_TYPE_P (type))
9819 /* Transform x * -C into -x * C if x is easily negatable. */
9820 if (TREE_CODE (op1) == INTEGER_CST
9821 && tree_int_cst_sgn (op1) == -1
9822 && negate_expr_p (op0)
9823 && negate_expr_p (op1)
9824 && (tem = negate_expr (op1)) != op1
9825 && ! TREE_OVERFLOW (tem))
9826 return fold_build2_loc (loc, MULT_EXPR, type,
9827 fold_convert_loc (loc, type,
9828 negate_expr (op0)), tem);
9830 strict_overflow_p = false;
9831 if (TREE_CODE (arg1) == INTEGER_CST
9832 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9833 &strict_overflow_p)))
9835 if (strict_overflow_p)
9836 fold_overflow_warning (("assuming signed overflow does not "
9837 "occur when simplifying "
9838 "multiplication"),
9839 WARN_STRICT_OVERFLOW_MISC);
9840 return fold_convert_loc (loc, type, tem);
9843 /* Optimize z * conj(z) for integer complex numbers. */
9844 if (TREE_CODE (arg0) == CONJ_EXPR
9845 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9846 return fold_mult_zconjz (loc, type, arg1);
9847 if (TREE_CODE (arg1) == CONJ_EXPR
9848 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9849 return fold_mult_zconjz (loc, type, arg0);
9851 else
9853 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9854 This is not the same for NaNs or if signed zeros are
9855 involved. */
9856 if (!HONOR_NANS (arg0)
9857 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9858 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9859 && TREE_CODE (arg1) == COMPLEX_CST
9860 && real_zerop (TREE_REALPART (arg1)))
9862 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9863 if (real_onep (TREE_IMAGPART (arg1)))
9864 return
9865 fold_build2_loc (loc, COMPLEX_EXPR, type,
9866 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9867 rtype, arg0)),
9868 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9869 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9870 return
9871 fold_build2_loc (loc, COMPLEX_EXPR, type,
9872 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9873 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9874 rtype, arg0)));
9877 /* Optimize z * conj(z) for floating point complex numbers.
9878 Guarded by flag_unsafe_math_optimizations as non-finite
9879 imaginary components don't produce scalar results. */
9880 if (flag_unsafe_math_optimizations
9881 && TREE_CODE (arg0) == CONJ_EXPR
9882 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9883 return fold_mult_zconjz (loc, type, arg1);
9884 if (flag_unsafe_math_optimizations
9885 && TREE_CODE (arg1) == CONJ_EXPR
9886 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9887 return fold_mult_zconjz (loc, type, arg0);
9889 goto associate;
9891 case BIT_IOR_EXPR:
9892 /* Canonicalize (X & C1) | C2. */
9893 if (TREE_CODE (arg0) == BIT_AND_EXPR
9894 && TREE_CODE (arg1) == INTEGER_CST
9895 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9897 int width = TYPE_PRECISION (type), w;
9898 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
9899 wide_int c2 = wi::to_wide (arg1);
9901 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9902 if ((c1 & c2) == c1)
9903 return omit_one_operand_loc (loc, type, arg1,
9904 TREE_OPERAND (arg0, 0));
9906 wide_int msk = wi::mask (width, false,
9907 TYPE_PRECISION (TREE_TYPE (arg1)));
9909 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9910 if (wi::bit_and_not (msk, c1 | c2) == 0)
9912 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9913 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9916 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9917 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9918 mode which allows further optimizations. */
9919 c1 &= msk;
9920 c2 &= msk;
9921 wide_int c3 = wi::bit_and_not (c1, c2);
9922 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9924 wide_int mask = wi::mask (w, false,
9925 TYPE_PRECISION (type));
9926 if (((c1 | c2) & mask) == mask
9927 && wi::bit_and_not (c1, mask) == 0)
9929 c3 = mask;
9930 break;
9934 if (c3 != c1)
9936 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9937 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
9938 wide_int_to_tree (type, c3));
9939 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9943 /* See if this can be simplified into a rotate first. If that
9944 is unsuccessful continue in the association code. */
9945 goto bit_rotate;
9947 case BIT_XOR_EXPR:
9948 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9949 if (TREE_CODE (arg0) == BIT_AND_EXPR
9950 && INTEGRAL_TYPE_P (type)
9951 && integer_onep (TREE_OPERAND (arg0, 1))
9952 && integer_onep (arg1))
9953 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9954 build_zero_cst (TREE_TYPE (arg0)));
9956 /* See if this can be simplified into a rotate first. If that
9957 is unsuccessful continue in the association code. */
9958 goto bit_rotate;
9960 case BIT_AND_EXPR:
9961 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9962 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9963 && INTEGRAL_TYPE_P (type)
9964 && integer_onep (TREE_OPERAND (arg0, 1))
9965 && integer_onep (arg1))
9967 tree tem2;
9968 tem = TREE_OPERAND (arg0, 0);
9969 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9970 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9971 tem, tem2);
9972 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9973 build_zero_cst (TREE_TYPE (tem)));
9975 /* Fold ~X & 1 as (X & 1) == 0. */
9976 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9977 && INTEGRAL_TYPE_P (type)
9978 && integer_onep (arg1))
9980 tree tem2;
9981 tem = TREE_OPERAND (arg0, 0);
9982 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9983 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
9984 tem, tem2);
9985 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
9986 build_zero_cst (TREE_TYPE (tem)));
9988 /* Fold !X & 1 as X == 0. */
9989 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9990 && integer_onep (arg1))
9992 tem = TREE_OPERAND (arg0, 0);
9993 return fold_build2_loc (loc, EQ_EXPR, type, tem,
9994 build_zero_cst (TREE_TYPE (tem)));
9997 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9998 multiple of 1 << CST. */
9999 if (TREE_CODE (arg1) == INTEGER_CST)
10001 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10002 wide_int ncst1 = -cst1;
10003 if ((cst1 & ncst1) == ncst1
10004 && multiple_of_p (type, arg0,
10005 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10006 return fold_convert_loc (loc, type, arg0);
10009 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10010 bits from CST2. */
10011 if (TREE_CODE (arg1) == INTEGER_CST
10012 && TREE_CODE (arg0) == MULT_EXPR
10013 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10015 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10016 wide_int masked
10017 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10019 if (masked == 0)
10020 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10021 arg0, arg1);
10022 else if (masked != warg1)
10024 /* Avoid the transform if arg1 is a mask of some
10025 mode which allows further optimizations. */
10026 int pop = wi::popcount (warg1);
10027 if (!(pop >= BITS_PER_UNIT
10028 && pow2p_hwi (pop)
10029 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10030 return fold_build2_loc (loc, code, type, op0,
10031 wide_int_to_tree (type, masked));
10035 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10036 ((A & N) + B) & M -> (A + B) & M
10037 Similarly if (N & M) == 0,
10038 ((A | N) + B) & M -> (A + B) & M
10039 and for - instead of + (or unary - instead of +)
10040 and/or ^ instead of |.
10041 If B is constant and (B & M) == 0, fold into A & M. */
10042 if (TREE_CODE (arg1) == INTEGER_CST)
10044 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10045 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10046 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10047 && (TREE_CODE (arg0) == PLUS_EXPR
10048 || TREE_CODE (arg0) == MINUS_EXPR
10049 || TREE_CODE (arg0) == NEGATE_EXPR)
10050 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10051 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10053 tree pmop[2];
10054 int which = 0;
10055 wide_int cst0;
10057 /* Now we know that arg0 is (C + D) or (C - D) or
10058 -C and arg1 (M) is == (1LL << cst) - 1.
10059 Store C into PMOP[0] and D into PMOP[1]. */
10060 pmop[0] = TREE_OPERAND (arg0, 0);
10061 pmop[1] = NULL;
10062 if (TREE_CODE (arg0) != NEGATE_EXPR)
10064 pmop[1] = TREE_OPERAND (arg0, 1);
10065 which = 1;
10068 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10069 which = -1;
10071 for (; which >= 0; which--)
10072 switch (TREE_CODE (pmop[which]))
10074 case BIT_AND_EXPR:
10075 case BIT_IOR_EXPR:
10076 case BIT_XOR_EXPR:
10077 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10078 != INTEGER_CST)
10079 break;
10080 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10081 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10083 if (cst0 != cst1)
10084 break;
10086 else if (cst0 != 0)
10087 break;
10088 /* If C or D is of the form (A & N) where
10089 (N & M) == M, or of the form (A | N) or
10090 (A ^ N) where (N & M) == 0, replace it with A. */
10091 pmop[which] = TREE_OPERAND (pmop[which], 0);
10092 break;
10093 case INTEGER_CST:
10094 /* If C or D is a N where (N & M) == 0, it can be
10095 omitted (assumed 0). */
10096 if ((TREE_CODE (arg0) == PLUS_EXPR
10097 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10098 && (cst1 & wi::to_wide (pmop[which])) == 0)
10099 pmop[which] = NULL;
10100 break;
10101 default:
10102 break;
10105 /* Only build anything new if we optimized one or both arguments
10106 above. */
10107 if (pmop[0] != TREE_OPERAND (arg0, 0)
10108 || (TREE_CODE (arg0) != NEGATE_EXPR
10109 && pmop[1] != TREE_OPERAND (arg0, 1)))
10111 tree utype = TREE_TYPE (arg0);
10112 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10114 /* Perform the operations in a type that has defined
10115 overflow behavior. */
10116 utype = unsigned_type_for (TREE_TYPE (arg0));
10117 if (pmop[0] != NULL)
10118 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10119 if (pmop[1] != NULL)
10120 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10123 if (TREE_CODE (arg0) == NEGATE_EXPR)
10124 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10125 else if (TREE_CODE (arg0) == PLUS_EXPR)
10127 if (pmop[0] != NULL && pmop[1] != NULL)
10128 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10129 pmop[0], pmop[1]);
10130 else if (pmop[0] != NULL)
10131 tem = pmop[0];
10132 else if (pmop[1] != NULL)
10133 tem = pmop[1];
10134 else
10135 return build_int_cst (type, 0);
10137 else if (pmop[0] == NULL)
10138 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10139 else
10140 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10141 pmop[0], pmop[1]);
10142 /* TEM is now the new binary +, - or unary - replacement. */
10143 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10144 fold_convert_loc (loc, utype, arg1));
10145 return fold_convert_loc (loc, type, tem);
10150 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10151 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10152 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10154 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10156 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10157 if (mask == -1)
10158 return
10159 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10162 goto associate;
10164 case RDIV_EXPR:
10165 /* Don't touch a floating-point divide by zero unless the mode
10166 of the constant can represent infinity. */
10167 if (TREE_CODE (arg1) == REAL_CST
10168 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10169 && real_zerop (arg1))
10170 return NULL_TREE;
10172 /* (-A) / (-B) -> A / B */
10173 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10174 return fold_build2_loc (loc, RDIV_EXPR, type,
10175 TREE_OPERAND (arg0, 0),
10176 negate_expr (arg1));
10177 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10178 return fold_build2_loc (loc, RDIV_EXPR, type,
10179 negate_expr (arg0),
10180 TREE_OPERAND (arg1, 0));
10181 return NULL_TREE;
10183 case TRUNC_DIV_EXPR:
10184 /* Fall through */
10186 case FLOOR_DIV_EXPR:
10187 /* Simplify A / (B << N) where A and B are positive and B is
10188 a power of 2, to A >> (N + log2(B)). */
10189 strict_overflow_p = false;
10190 if (TREE_CODE (arg1) == LSHIFT_EXPR
10191 && (TYPE_UNSIGNED (type)
10192 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10194 tree sval = TREE_OPERAND (arg1, 0);
10195 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10197 tree sh_cnt = TREE_OPERAND (arg1, 1);
10198 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10199 wi::exact_log2 (wi::to_wide (sval)));
10201 if (strict_overflow_p)
10202 fold_overflow_warning (("assuming signed overflow does not "
10203 "occur when simplifying A / (B << N)"),
10204 WARN_STRICT_OVERFLOW_MISC);
10206 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10207 sh_cnt, pow2);
10208 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10209 fold_convert_loc (loc, type, arg0), sh_cnt);
10213 /* Fall through */
10215 case ROUND_DIV_EXPR:
10216 case CEIL_DIV_EXPR:
10217 case EXACT_DIV_EXPR:
10218 if (integer_zerop (arg1))
10219 return NULL_TREE;
10221 /* Convert -A / -B to A / B when the type is signed and overflow is
10222 undefined. */
10223 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10224 && TREE_CODE (op0) == NEGATE_EXPR
10225 && negate_expr_p (op1))
10227 if (INTEGRAL_TYPE_P (type))
10228 fold_overflow_warning (("assuming signed overflow does not occur "
10229 "when distributing negation across "
10230 "division"),
10231 WARN_STRICT_OVERFLOW_MISC);
10232 return fold_build2_loc (loc, code, type,
10233 fold_convert_loc (loc, type,
10234 TREE_OPERAND (arg0, 0)),
10235 negate_expr (op1));
10237 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10238 && TREE_CODE (arg1) == NEGATE_EXPR
10239 && negate_expr_p (op0))
10241 if (INTEGRAL_TYPE_P (type))
10242 fold_overflow_warning (("assuming signed overflow does not occur "
10243 "when distributing negation across "
10244 "division"),
10245 WARN_STRICT_OVERFLOW_MISC);
10246 return fold_build2_loc (loc, code, type,
10247 negate_expr (op0),
10248 fold_convert_loc (loc, type,
10249 TREE_OPERAND (arg1, 0)));
10252 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10253 operation, EXACT_DIV_EXPR.
10255 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10256 At one time others generated faster code, it's not clear if they do
10257 after the last round to changes to the DIV code in expmed.c. */
10258 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10259 && multiple_of_p (type, arg0, arg1))
10260 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10261 fold_convert (type, arg0),
10262 fold_convert (type, arg1));
10264 strict_overflow_p = false;
10265 if (TREE_CODE (arg1) == INTEGER_CST
10266 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10267 &strict_overflow_p)))
10269 if (strict_overflow_p)
10270 fold_overflow_warning (("assuming signed overflow does not occur "
10271 "when simplifying division"),
10272 WARN_STRICT_OVERFLOW_MISC);
10273 return fold_convert_loc (loc, type, tem);
10276 return NULL_TREE;
10278 case CEIL_MOD_EXPR:
10279 case FLOOR_MOD_EXPR:
10280 case ROUND_MOD_EXPR:
10281 case TRUNC_MOD_EXPR:
10282 strict_overflow_p = false;
10283 if (TREE_CODE (arg1) == INTEGER_CST
10284 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10285 &strict_overflow_p)))
10287 if (strict_overflow_p)
10288 fold_overflow_warning (("assuming signed overflow does not occur "
10289 "when simplifying modulus"),
10290 WARN_STRICT_OVERFLOW_MISC);
10291 return fold_convert_loc (loc, type, tem);
10294 return NULL_TREE;
10296 case LROTATE_EXPR:
10297 case RROTATE_EXPR:
10298 case RSHIFT_EXPR:
10299 case LSHIFT_EXPR:
10300 /* Since negative shift count is not well-defined,
10301 don't try to compute it in the compiler. */
10302 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10303 return NULL_TREE;
10305 prec = element_precision (type);
10307 /* If we have a rotate of a bit operation with the rotate count and
10308 the second operand of the bit operation both constant,
10309 permute the two operations. */
10310 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10311 && (TREE_CODE (arg0) == BIT_AND_EXPR
10312 || TREE_CODE (arg0) == BIT_IOR_EXPR
10313 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10314 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10316 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10317 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10318 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10319 fold_build2_loc (loc, code, type,
10320 arg00, arg1),
10321 fold_build2_loc (loc, code, type,
10322 arg01, arg1));
10325 /* Two consecutive rotates adding up to the some integer
10326 multiple of the precision of the type can be ignored. */
10327 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10328 && TREE_CODE (arg0) == RROTATE_EXPR
10329 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10330 && wi::umod_trunc (wi::to_wide (arg1)
10331 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10332 prec) == 0)
10333 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10335 return NULL_TREE;
10337 case MIN_EXPR:
10338 case MAX_EXPR:
10339 goto associate;
10341 case TRUTH_ANDIF_EXPR:
10342 /* Note that the operands of this must be ints
10343 and their values must be 0 or 1.
10344 ("true" is a fixed value perhaps depending on the language.) */
10345 /* If first arg is constant zero, return it. */
10346 if (integer_zerop (arg0))
10347 return fold_convert_loc (loc, type, arg0);
10348 /* FALLTHRU */
10349 case TRUTH_AND_EXPR:
10350 /* If either arg is constant true, drop it. */
10351 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10352 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10353 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10354 /* Preserve sequence points. */
10355 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10356 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10357 /* If second arg is constant zero, result is zero, but first arg
10358 must be evaluated. */
10359 if (integer_zerop (arg1))
10360 return omit_one_operand_loc (loc, type, arg1, arg0);
10361 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10362 case will be handled here. */
10363 if (integer_zerop (arg0))
10364 return omit_one_operand_loc (loc, type, arg0, arg1);
10366 /* !X && X is always false. */
10367 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10368 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10369 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10370 /* X && !X is always false. */
10371 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10372 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10373 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10375 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10376 means A >= Y && A != MAX, but in this case we know that
10377 A < X <= MAX. */
10379 if (!TREE_SIDE_EFFECTS (arg0)
10380 && !TREE_SIDE_EFFECTS (arg1))
10382 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10383 if (tem && !operand_equal_p (tem, arg0, 0))
10384 return fold_build2_loc (loc, code, type, tem, arg1);
10386 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10387 if (tem && !operand_equal_p (tem, arg1, 0))
10388 return fold_build2_loc (loc, code, type, arg0, tem);
10391 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10392 != NULL_TREE)
10393 return tem;
10395 return NULL_TREE;
10397 case TRUTH_ORIF_EXPR:
10398 /* Note that the operands of this must be ints
10399 and their values must be 0 or true.
10400 ("true" is a fixed value perhaps depending on the language.) */
10401 /* If first arg is constant true, return it. */
10402 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10403 return fold_convert_loc (loc, type, arg0);
10404 /* FALLTHRU */
10405 case TRUTH_OR_EXPR:
10406 /* If either arg is constant zero, drop it. */
10407 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10408 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10409 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10410 /* Preserve sequence points. */
10411 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10412 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10413 /* If second arg is constant true, result is true, but we must
10414 evaluate first arg. */
10415 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10416 return omit_one_operand_loc (loc, type, arg1, arg0);
10417 /* Likewise for first arg, but note this only occurs here for
10418 TRUTH_OR_EXPR. */
10419 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10420 return omit_one_operand_loc (loc, type, arg0, arg1);
10422 /* !X || X is always true. */
10423 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10424 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10425 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10426 /* X || !X is always true. */
10427 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10428 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10429 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10431 /* (X && !Y) || (!X && Y) is X ^ Y */
10432 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10433 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10435 tree a0, a1, l0, l1, n0, n1;
10437 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10438 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10440 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10441 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10443 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10444 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10446 if ((operand_equal_p (n0, a0, 0)
10447 && operand_equal_p (n1, a1, 0))
10448 || (operand_equal_p (n0, a1, 0)
10449 && operand_equal_p (n1, a0, 0)))
10450 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10453 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10454 != NULL_TREE)
10455 return tem;
10457 return NULL_TREE;
10459 case TRUTH_XOR_EXPR:
10460 /* If the second arg is constant zero, drop it. */
10461 if (integer_zerop (arg1))
10462 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10463 /* If the second arg is constant true, this is a logical inversion. */
10464 if (integer_onep (arg1))
10466 tem = invert_truthvalue_loc (loc, arg0);
10467 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10469 /* Identical arguments cancel to zero. */
10470 if (operand_equal_p (arg0, arg1, 0))
10471 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10473 /* !X ^ X is always true. */
10474 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10475 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10476 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10478 /* X ^ !X is always true. */
10479 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10480 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10481 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10483 return NULL_TREE;
10485 case EQ_EXPR:
10486 case NE_EXPR:
10487 STRIP_NOPS (arg0);
10488 STRIP_NOPS (arg1);
10490 tem = fold_comparison (loc, code, type, op0, op1);
10491 if (tem != NULL_TREE)
10492 return tem;
10494 /* bool_var != 1 becomes !bool_var. */
10495 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10496 && code == NE_EXPR)
10497 return fold_convert_loc (loc, type,
10498 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10499 TREE_TYPE (arg0), arg0));
10501 /* bool_var == 0 becomes !bool_var. */
10502 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10503 && code == EQ_EXPR)
10504 return fold_convert_loc (loc, type,
10505 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10506 TREE_TYPE (arg0), arg0));
10508 /* !exp != 0 becomes !exp */
10509 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10510 && code == NE_EXPR)
10511 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10513 /* If this is an EQ or NE comparison with zero and ARG0 is
10514 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10515 two operations, but the latter can be done in one less insn
10516 on machines that have only two-operand insns or on which a
10517 constant cannot be the first operand. */
10518 if (TREE_CODE (arg0) == BIT_AND_EXPR
10519 && integer_zerop (arg1))
10521 tree arg00 = TREE_OPERAND (arg0, 0);
10522 tree arg01 = TREE_OPERAND (arg0, 1);
10523 if (TREE_CODE (arg00) == LSHIFT_EXPR
10524 && integer_onep (TREE_OPERAND (arg00, 0)))
10526 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10527 arg01, TREE_OPERAND (arg00, 1));
10528 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10529 build_int_cst (TREE_TYPE (arg0), 1));
10530 return fold_build2_loc (loc, code, type,
10531 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10532 arg1);
10534 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10535 && integer_onep (TREE_OPERAND (arg01, 0)))
10537 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10538 arg00, TREE_OPERAND (arg01, 1));
10539 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10540 build_int_cst (TREE_TYPE (arg0), 1));
10541 return fold_build2_loc (loc, code, type,
10542 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10543 arg1);
10547 /* If this is an NE or EQ comparison of zero against the result of a
10548 signed MOD operation whose second operand is a power of 2, make
10549 the MOD operation unsigned since it is simpler and equivalent. */
10550 if (integer_zerop (arg1)
10551 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10552 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10553 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10554 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10555 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10556 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10558 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10559 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10560 fold_convert_loc (loc, newtype,
10561 TREE_OPERAND (arg0, 0)),
10562 fold_convert_loc (loc, newtype,
10563 TREE_OPERAND (arg0, 1)));
10565 return fold_build2_loc (loc, code, type, newmod,
10566 fold_convert_loc (loc, newtype, arg1));
10569 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10570 C1 is a valid shift constant, and C2 is a power of two, i.e.
10571 a single bit. */
10572 if (TREE_CODE (arg0) == BIT_AND_EXPR
10573 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10574 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10575 == INTEGER_CST
10576 && integer_pow2p (TREE_OPERAND (arg0, 1))
10577 && integer_zerop (arg1))
10579 tree itype = TREE_TYPE (arg0);
10580 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10581 prec = TYPE_PRECISION (itype);
10583 /* Check for a valid shift count. */
10584 if (wi::ltu_p (wi::to_wide (arg001), prec))
10586 tree arg01 = TREE_OPERAND (arg0, 1);
10587 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10588 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10589 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10590 can be rewritten as (X & (C2 << C1)) != 0. */
10591 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10593 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10594 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10595 return fold_build2_loc (loc, code, type, tem,
10596 fold_convert_loc (loc, itype, arg1));
10598 /* Otherwise, for signed (arithmetic) shifts,
10599 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10600 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10601 else if (!TYPE_UNSIGNED (itype))
10602 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10603 arg000, build_int_cst (itype, 0));
10604 /* Otherwise, of unsigned (logical) shifts,
10605 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10606 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10607 else
10608 return omit_one_operand_loc (loc, type,
10609 code == EQ_EXPR ? integer_one_node
10610 : integer_zero_node,
10611 arg000);
10615 /* If this is a comparison of a field, we may be able to simplify it. */
10616 if ((TREE_CODE (arg0) == COMPONENT_REF
10617 || TREE_CODE (arg0) == BIT_FIELD_REF)
10618 /* Handle the constant case even without -O
10619 to make sure the warnings are given. */
10620 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10622 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10623 if (t1)
10624 return t1;
10627 /* Optimize comparisons of strlen vs zero to a compare of the
10628 first character of the string vs zero. To wit,
10629 strlen(ptr) == 0 => *ptr == 0
10630 strlen(ptr) != 0 => *ptr != 0
10631 Other cases should reduce to one of these two (or a constant)
10632 due to the return value of strlen being unsigned. */
10633 if (TREE_CODE (arg0) == CALL_EXPR
10634 && integer_zerop (arg1))
10636 tree fndecl = get_callee_fndecl (arg0);
10638 if (fndecl
10639 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10640 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10641 && call_expr_nargs (arg0) == 1
10642 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10644 tree iref = build_fold_indirect_ref_loc (loc,
10645 CALL_EXPR_ARG (arg0, 0));
10646 return fold_build2_loc (loc, code, type, iref,
10647 build_int_cst (TREE_TYPE (iref), 0));
10651 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10652 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10653 if (TREE_CODE (arg0) == RSHIFT_EXPR
10654 && integer_zerop (arg1)
10655 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10657 tree arg00 = TREE_OPERAND (arg0, 0);
10658 tree arg01 = TREE_OPERAND (arg0, 1);
10659 tree itype = TREE_TYPE (arg00);
10660 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10662 if (TYPE_UNSIGNED (itype))
10664 itype = signed_type_for (itype);
10665 arg00 = fold_convert_loc (loc, itype, arg00);
10667 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10668 type, arg00, build_zero_cst (itype));
10672 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10673 (X & C) == 0 when C is a single bit. */
10674 if (TREE_CODE (arg0) == BIT_AND_EXPR
10675 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10676 && integer_zerop (arg1)
10677 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10679 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10680 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10681 TREE_OPERAND (arg0, 1));
10682 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10683 type, tem,
10684 fold_convert_loc (loc, TREE_TYPE (arg0),
10685 arg1));
10688 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10689 constant C is a power of two, i.e. a single bit. */
10690 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10691 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10692 && integer_zerop (arg1)
10693 && integer_pow2p (TREE_OPERAND (arg0, 1))
10694 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10695 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10697 tree arg00 = TREE_OPERAND (arg0, 0);
10698 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10699 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10702 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10703 when is C is a power of two, i.e. a single bit. */
10704 if (TREE_CODE (arg0) == BIT_AND_EXPR
10705 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10706 && integer_zerop (arg1)
10707 && integer_pow2p (TREE_OPERAND (arg0, 1))
10708 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10709 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10711 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10712 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10713 arg000, TREE_OPERAND (arg0, 1));
10714 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10715 tem, build_int_cst (TREE_TYPE (tem), 0));
10718 if (integer_zerop (arg1)
10719 && tree_expr_nonzero_p (arg0))
10721 tree res = constant_boolean_node (code==NE_EXPR, type);
10722 return omit_one_operand_loc (loc, type, res, arg0);
10725 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10726 if (TREE_CODE (arg0) == BIT_AND_EXPR
10727 && TREE_CODE (arg1) == BIT_AND_EXPR)
10729 tree arg00 = TREE_OPERAND (arg0, 0);
10730 tree arg01 = TREE_OPERAND (arg0, 1);
10731 tree arg10 = TREE_OPERAND (arg1, 0);
10732 tree arg11 = TREE_OPERAND (arg1, 1);
10733 tree itype = TREE_TYPE (arg0);
10735 if (operand_equal_p (arg01, arg11, 0))
10737 tem = fold_convert_loc (loc, itype, arg10);
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 (arg01, arg10, 0))
10745 tem = fold_convert_loc (loc, itype, arg11);
10746 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10747 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10748 return fold_build2_loc (loc, code, type, tem,
10749 build_zero_cst (itype));
10751 if (operand_equal_p (arg00, arg11, 0))
10753 tem = fold_convert_loc (loc, itype, arg10);
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));
10759 if (operand_equal_p (arg00, arg10, 0))
10761 tem = fold_convert_loc (loc, itype, arg11);
10762 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10763 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10764 return fold_build2_loc (loc, code, type, tem,
10765 build_zero_cst (itype));
10769 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10770 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10772 tree arg00 = TREE_OPERAND (arg0, 0);
10773 tree arg01 = TREE_OPERAND (arg0, 1);
10774 tree arg10 = TREE_OPERAND (arg1, 0);
10775 tree arg11 = TREE_OPERAND (arg1, 1);
10776 tree itype = TREE_TYPE (arg0);
10778 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10779 operand_equal_p guarantees no side-effects so we don't need
10780 to use omit_one_operand on Z. */
10781 if (operand_equal_p (arg01, arg11, 0))
10782 return fold_build2_loc (loc, code, type, arg00,
10783 fold_convert_loc (loc, TREE_TYPE (arg00),
10784 arg10));
10785 if (operand_equal_p (arg01, arg10, 0))
10786 return fold_build2_loc (loc, code, type, arg00,
10787 fold_convert_loc (loc, TREE_TYPE (arg00),
10788 arg11));
10789 if (operand_equal_p (arg00, arg11, 0))
10790 return fold_build2_loc (loc, code, type, arg01,
10791 fold_convert_loc (loc, TREE_TYPE (arg01),
10792 arg10));
10793 if (operand_equal_p (arg00, arg10, 0))
10794 return fold_build2_loc (loc, code, type, arg01,
10795 fold_convert_loc (loc, TREE_TYPE (arg01),
10796 arg11));
10798 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10799 if (TREE_CODE (arg01) == INTEGER_CST
10800 && TREE_CODE (arg11) == INTEGER_CST)
10802 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10803 fold_convert_loc (loc, itype, arg11));
10804 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10805 return fold_build2_loc (loc, code, type, tem,
10806 fold_convert_loc (loc, itype, arg10));
10810 /* Attempt to simplify equality/inequality comparisons of complex
10811 values. Only lower the comparison if the result is known or
10812 can be simplified to a single scalar comparison. */
10813 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10814 || TREE_CODE (arg0) == COMPLEX_CST)
10815 && (TREE_CODE (arg1) == COMPLEX_EXPR
10816 || TREE_CODE (arg1) == COMPLEX_CST))
10818 tree real0, imag0, real1, imag1;
10819 tree rcond, icond;
10821 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10823 real0 = TREE_OPERAND (arg0, 0);
10824 imag0 = TREE_OPERAND (arg0, 1);
10826 else
10828 real0 = TREE_REALPART (arg0);
10829 imag0 = TREE_IMAGPART (arg0);
10832 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10834 real1 = TREE_OPERAND (arg1, 0);
10835 imag1 = TREE_OPERAND (arg1, 1);
10837 else
10839 real1 = TREE_REALPART (arg1);
10840 imag1 = TREE_IMAGPART (arg1);
10843 rcond = fold_binary_loc (loc, code, type, real0, real1);
10844 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10846 if (integer_zerop (rcond))
10848 if (code == EQ_EXPR)
10849 return omit_two_operands_loc (loc, type, boolean_false_node,
10850 imag0, imag1);
10851 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10853 else
10855 if (code == NE_EXPR)
10856 return omit_two_operands_loc (loc, type, boolean_true_node,
10857 imag0, imag1);
10858 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10862 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10863 if (icond && TREE_CODE (icond) == INTEGER_CST)
10865 if (integer_zerop (icond))
10867 if (code == EQ_EXPR)
10868 return omit_two_operands_loc (loc, type, boolean_false_node,
10869 real0, real1);
10870 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10872 else
10874 if (code == NE_EXPR)
10875 return omit_two_operands_loc (loc, type, boolean_true_node,
10876 real0, real1);
10877 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10882 return NULL_TREE;
10884 case LT_EXPR:
10885 case GT_EXPR:
10886 case LE_EXPR:
10887 case GE_EXPR:
10888 tem = fold_comparison (loc, code, type, op0, op1);
10889 if (tem != NULL_TREE)
10890 return tem;
10892 /* Transform comparisons of the form X +- C CMP X. */
10893 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10894 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10895 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10896 && !HONOR_SNANS (arg0))
10898 tree arg01 = TREE_OPERAND (arg0, 1);
10899 enum tree_code code0 = TREE_CODE (arg0);
10900 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10902 /* (X - c) > X becomes false. */
10903 if (code == GT_EXPR
10904 && ((code0 == MINUS_EXPR && is_positive >= 0)
10905 || (code0 == PLUS_EXPR && is_positive <= 0)))
10906 return constant_boolean_node (0, type);
10908 /* Likewise (X + c) < X becomes false. */
10909 if (code == LT_EXPR
10910 && ((code0 == PLUS_EXPR && is_positive >= 0)
10911 || (code0 == MINUS_EXPR && is_positive <= 0)))
10912 return constant_boolean_node (0, type);
10914 /* Convert (X - c) <= X to true. */
10915 if (!HONOR_NANS (arg1)
10916 && code == LE_EXPR
10917 && ((code0 == MINUS_EXPR && is_positive >= 0)
10918 || (code0 == PLUS_EXPR && is_positive <= 0)))
10919 return constant_boolean_node (1, type);
10921 /* Convert (X + c) >= X to true. */
10922 if (!HONOR_NANS (arg1)
10923 && code == GE_EXPR
10924 && ((code0 == PLUS_EXPR && is_positive >= 0)
10925 || (code0 == MINUS_EXPR && is_positive <= 0)))
10926 return constant_boolean_node (1, type);
10929 /* If we are comparing an ABS_EXPR with a constant, we can
10930 convert all the cases into explicit comparisons, but they may
10931 well not be faster than doing the ABS and one comparison.
10932 But ABS (X) <= C is a range comparison, which becomes a subtraction
10933 and a comparison, and is probably faster. */
10934 if (code == LE_EXPR
10935 && TREE_CODE (arg1) == INTEGER_CST
10936 && TREE_CODE (arg0) == ABS_EXPR
10937 && ! TREE_SIDE_EFFECTS (arg0)
10938 && (0 != (tem = negate_expr (arg1)))
10939 && TREE_CODE (tem) == INTEGER_CST
10940 && !TREE_OVERFLOW (tem))
10941 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
10942 build2 (GE_EXPR, type,
10943 TREE_OPERAND (arg0, 0), tem),
10944 build2 (LE_EXPR, type,
10945 TREE_OPERAND (arg0, 0), arg1));
10947 /* Convert ABS_EXPR<x> >= 0 to true. */
10948 strict_overflow_p = false;
10949 if (code == GE_EXPR
10950 && (integer_zerop (arg1)
10951 || (! HONOR_NANS (arg0)
10952 && real_zerop (arg1)))
10953 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
10955 if (strict_overflow_p)
10956 fold_overflow_warning (("assuming signed overflow does not occur "
10957 "when simplifying comparison of "
10958 "absolute value and zero"),
10959 WARN_STRICT_OVERFLOW_CONDITIONAL);
10960 return omit_one_operand_loc (loc, type,
10961 constant_boolean_node (true, type),
10962 arg0);
10965 /* Convert ABS_EXPR<x> < 0 to false. */
10966 strict_overflow_p = false;
10967 if (code == LT_EXPR
10968 && (integer_zerop (arg1) || real_zerop (arg1))
10969 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
10971 if (strict_overflow_p)
10972 fold_overflow_warning (("assuming signed overflow does not occur "
10973 "when simplifying comparison of "
10974 "absolute value and zero"),
10975 WARN_STRICT_OVERFLOW_CONDITIONAL);
10976 return omit_one_operand_loc (loc, type,
10977 constant_boolean_node (false, type),
10978 arg0);
10981 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
10982 and similarly for >= into !=. */
10983 if ((code == LT_EXPR || code == GE_EXPR)
10984 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10985 && TREE_CODE (arg1) == LSHIFT_EXPR
10986 && integer_onep (TREE_OPERAND (arg1, 0)))
10987 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10988 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10989 TREE_OPERAND (arg1, 1)),
10990 build_zero_cst (TREE_TYPE (arg0)));
10992 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
10993 otherwise Y might be >= # of bits in X's type and thus e.g.
10994 (unsigned char) (1 << Y) for Y 15 might be 0.
10995 If the cast is widening, then 1 << Y should have unsigned type,
10996 otherwise if Y is number of bits in the signed shift type minus 1,
10997 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
10998 31 might be 0xffffffff80000000. */
10999 if ((code == LT_EXPR || code == GE_EXPR)
11000 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11001 && CONVERT_EXPR_P (arg1)
11002 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11003 && (element_precision (TREE_TYPE (arg1))
11004 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11005 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11006 || (element_precision (TREE_TYPE (arg1))
11007 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11008 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11010 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11011 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11012 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11013 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11014 build_zero_cst (TREE_TYPE (arg0)));
11017 return NULL_TREE;
11019 case UNORDERED_EXPR:
11020 case ORDERED_EXPR:
11021 case UNLT_EXPR:
11022 case UNLE_EXPR:
11023 case UNGT_EXPR:
11024 case UNGE_EXPR:
11025 case UNEQ_EXPR:
11026 case LTGT_EXPR:
11027 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11029 tree targ0 = strip_float_extensions (arg0);
11030 tree targ1 = strip_float_extensions (arg1);
11031 tree newtype = TREE_TYPE (targ0);
11033 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11034 newtype = TREE_TYPE (targ1);
11036 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11037 return fold_build2_loc (loc, code, type,
11038 fold_convert_loc (loc, newtype, targ0),
11039 fold_convert_loc (loc, newtype, targ1));
11042 return NULL_TREE;
11044 case COMPOUND_EXPR:
11045 /* When pedantic, a compound expression can be neither an lvalue
11046 nor an integer constant expression. */
11047 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11048 return NULL_TREE;
11049 /* Don't let (0, 0) be null pointer constant. */
11050 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11051 : fold_convert_loc (loc, type, arg1);
11052 return pedantic_non_lvalue_loc (loc, tem);
11054 case ASSERT_EXPR:
11055 /* An ASSERT_EXPR should never be passed to fold_binary. */
11056 gcc_unreachable ();
11058 default:
11059 return NULL_TREE;
11060 } /* switch (code) */
11063 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11064 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11065 of GOTO_EXPR. */
11067 static tree
11068 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11070 switch (TREE_CODE (*tp))
11072 case LABEL_EXPR:
11073 return *tp;
11075 case GOTO_EXPR:
11076 *walk_subtrees = 0;
11078 /* fall through */
11080 default:
11081 return NULL_TREE;
11085 /* Return whether the sub-tree ST contains a label which is accessible from
11086 outside the sub-tree. */
11088 static bool
11089 contains_label_p (tree st)
11091 return
11092 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11095 /* Fold a ternary expression of code CODE and type TYPE with operands
11096 OP0, OP1, and OP2. Return the folded expression if folding is
11097 successful. Otherwise, return NULL_TREE. */
11099 tree
11100 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11101 tree op0, tree op1, tree op2)
11103 tree tem;
11104 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11105 enum tree_code_class kind = TREE_CODE_CLASS (code);
11107 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11108 && TREE_CODE_LENGTH (code) == 3);
11110 /* If this is a commutative operation, and OP0 is a constant, move it
11111 to OP1 to reduce the number of tests below. */
11112 if (commutative_ternary_tree_code (code)
11113 && tree_swap_operands_p (op0, op1))
11114 return fold_build3_loc (loc, code, type, op1, op0, op2);
11116 tem = generic_simplify (loc, code, type, op0, op1, op2);
11117 if (tem)
11118 return tem;
11120 /* Strip any conversions that don't change the mode. This is safe
11121 for every expression, except for a comparison expression because
11122 its signedness is derived from its operands. So, in the latter
11123 case, only strip conversions that don't change the signedness.
11125 Note that this is done as an internal manipulation within the
11126 constant folder, in order to find the simplest representation of
11127 the arguments so that their form can be studied. In any cases,
11128 the appropriate type conversions should be put back in the tree
11129 that will get out of the constant folder. */
11130 if (op0)
11132 arg0 = op0;
11133 STRIP_NOPS (arg0);
11136 if (op1)
11138 arg1 = op1;
11139 STRIP_NOPS (arg1);
11142 if (op2)
11144 arg2 = op2;
11145 STRIP_NOPS (arg2);
11148 switch (code)
11150 case COMPONENT_REF:
11151 if (TREE_CODE (arg0) == CONSTRUCTOR
11152 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11154 unsigned HOST_WIDE_INT idx;
11155 tree field, value;
11156 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11157 if (field == arg1)
11158 return value;
11160 return NULL_TREE;
11162 case COND_EXPR:
11163 case VEC_COND_EXPR:
11164 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11165 so all simple results must be passed through pedantic_non_lvalue. */
11166 if (TREE_CODE (arg0) == INTEGER_CST)
11168 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11169 tem = integer_zerop (arg0) ? op2 : op1;
11170 /* Only optimize constant conditions when the selected branch
11171 has the same type as the COND_EXPR. This avoids optimizing
11172 away "c ? x : throw", where the throw has a void type.
11173 Avoid throwing away that operand which contains label. */
11174 if ((!TREE_SIDE_EFFECTS (unused_op)
11175 || !contains_label_p (unused_op))
11176 && (! VOID_TYPE_P (TREE_TYPE (tem))
11177 || VOID_TYPE_P (type)))
11178 return pedantic_non_lvalue_loc (loc, tem);
11179 return NULL_TREE;
11181 else if (TREE_CODE (arg0) == VECTOR_CST)
11183 if ((TREE_CODE (arg1) == VECTOR_CST
11184 || TREE_CODE (arg1) == CONSTRUCTOR)
11185 && (TREE_CODE (arg2) == VECTOR_CST
11186 || TREE_CODE (arg2) == CONSTRUCTOR))
11188 unsigned int nelts = VECTOR_CST_NELTS (arg0), i;
11189 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11190 auto_vec_perm_indices sel (nelts);
11191 for (i = 0; i < nelts; i++)
11193 tree val = VECTOR_CST_ELT (arg0, i);
11194 if (integer_all_onesp (val))
11195 sel.quick_push (i);
11196 else if (integer_zerop (val))
11197 sel.quick_push (nelts + i);
11198 else /* Currently unreachable. */
11199 return NULL_TREE;
11201 tree t = fold_vec_perm (type, arg1, arg2, sel);
11202 if (t != NULL_TREE)
11203 return t;
11207 /* If we have A op B ? A : C, we may be able to convert this to a
11208 simpler expression, depending on the operation and the values
11209 of B and C. Signed zeros prevent all of these transformations,
11210 for reasons given above each one.
11212 Also try swapping the arguments and inverting the conditional. */
11213 if (COMPARISON_CLASS_P (arg0)
11214 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), arg1)
11215 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11217 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11218 if (tem)
11219 return tem;
11222 if (COMPARISON_CLASS_P (arg0)
11223 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11224 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11226 location_t loc0 = expr_location_or (arg0, loc);
11227 tem = fold_invert_truthvalue (loc0, arg0);
11228 if (tem && COMPARISON_CLASS_P (tem))
11230 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11231 if (tem)
11232 return tem;
11236 /* If the second operand is simpler than the third, swap them
11237 since that produces better jump optimization results. */
11238 if (truth_value_p (TREE_CODE (arg0))
11239 && tree_swap_operands_p (op1, op2))
11241 location_t loc0 = expr_location_or (arg0, loc);
11242 /* See if this can be inverted. If it can't, possibly because
11243 it was a floating-point inequality comparison, don't do
11244 anything. */
11245 tem = fold_invert_truthvalue (loc0, arg0);
11246 if (tem)
11247 return fold_build3_loc (loc, code, type, tem, op2, op1);
11250 /* Convert A ? 1 : 0 to simply A. */
11251 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11252 : (integer_onep (op1)
11253 && !VECTOR_TYPE_P (type)))
11254 && integer_zerop (op2)
11255 /* If we try to convert OP0 to our type, the
11256 call to fold will try to move the conversion inside
11257 a COND, which will recurse. In that case, the COND_EXPR
11258 is probably the best choice, so leave it alone. */
11259 && type == TREE_TYPE (arg0))
11260 return pedantic_non_lvalue_loc (loc, arg0);
11262 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11263 over COND_EXPR in cases such as floating point comparisons. */
11264 if (integer_zerop (op1)
11265 && code == COND_EXPR
11266 && integer_onep (op2)
11267 && !VECTOR_TYPE_P (type)
11268 && truth_value_p (TREE_CODE (arg0)))
11269 return pedantic_non_lvalue_loc (loc,
11270 fold_convert_loc (loc, type,
11271 invert_truthvalue_loc (loc,
11272 arg0)));
11274 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11275 if (TREE_CODE (arg0) == LT_EXPR
11276 && integer_zerop (TREE_OPERAND (arg0, 1))
11277 && integer_zerop (op2)
11278 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11280 /* sign_bit_p looks through both zero and sign extensions,
11281 but for this optimization only sign extensions are
11282 usable. */
11283 tree tem2 = TREE_OPERAND (arg0, 0);
11284 while (tem != tem2)
11286 if (TREE_CODE (tem2) != NOP_EXPR
11287 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11289 tem = NULL_TREE;
11290 break;
11292 tem2 = TREE_OPERAND (tem2, 0);
11294 /* sign_bit_p only checks ARG1 bits within A's precision.
11295 If <sign bit of A> has wider type than A, bits outside
11296 of A's precision in <sign bit of A> need to be checked.
11297 If they are all 0, this optimization needs to be done
11298 in unsigned A's type, if they are all 1 in signed A's type,
11299 otherwise this can't be done. */
11300 if (tem
11301 && TYPE_PRECISION (TREE_TYPE (tem))
11302 < TYPE_PRECISION (TREE_TYPE (arg1))
11303 && TYPE_PRECISION (TREE_TYPE (tem))
11304 < TYPE_PRECISION (type))
11306 int inner_width, outer_width;
11307 tree tem_type;
11309 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11310 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11311 if (outer_width > TYPE_PRECISION (type))
11312 outer_width = TYPE_PRECISION (type);
11314 wide_int mask = wi::shifted_mask
11315 (inner_width, outer_width - inner_width, false,
11316 TYPE_PRECISION (TREE_TYPE (arg1)));
11318 wide_int common = mask & wi::to_wide (arg1);
11319 if (common == mask)
11321 tem_type = signed_type_for (TREE_TYPE (tem));
11322 tem = fold_convert_loc (loc, tem_type, tem);
11324 else if (common == 0)
11326 tem_type = unsigned_type_for (TREE_TYPE (tem));
11327 tem = fold_convert_loc (loc, tem_type, tem);
11329 else
11330 tem = NULL;
11333 if (tem)
11334 return
11335 fold_convert_loc (loc, type,
11336 fold_build2_loc (loc, BIT_AND_EXPR,
11337 TREE_TYPE (tem), tem,
11338 fold_convert_loc (loc,
11339 TREE_TYPE (tem),
11340 arg1)));
11343 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11344 already handled above. */
11345 if (TREE_CODE (arg0) == BIT_AND_EXPR
11346 && integer_onep (TREE_OPERAND (arg0, 1))
11347 && integer_zerop (op2)
11348 && integer_pow2p (arg1))
11350 tree tem = TREE_OPERAND (arg0, 0);
11351 STRIP_NOPS (tem);
11352 if (TREE_CODE (tem) == RSHIFT_EXPR
11353 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11354 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11355 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11356 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11357 fold_convert_loc (loc, type,
11358 TREE_OPERAND (tem, 0)),
11359 op1);
11362 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11363 is probably obsolete because the first operand should be a
11364 truth value (that's why we have the two cases above), but let's
11365 leave it in until we can confirm this for all front-ends. */
11366 if (integer_zerop (op2)
11367 && TREE_CODE (arg0) == NE_EXPR
11368 && integer_zerop (TREE_OPERAND (arg0, 1))
11369 && integer_pow2p (arg1)
11370 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11371 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11372 arg1, OEP_ONLY_CONST))
11373 return pedantic_non_lvalue_loc (loc,
11374 fold_convert_loc (loc, type,
11375 TREE_OPERAND (arg0, 0)));
11377 /* Disable the transformations below for vectors, since
11378 fold_binary_op_with_conditional_arg may undo them immediately,
11379 yielding an infinite loop. */
11380 if (code == VEC_COND_EXPR)
11381 return NULL_TREE;
11383 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11384 if (integer_zerop (op2)
11385 && truth_value_p (TREE_CODE (arg0))
11386 && truth_value_p (TREE_CODE (arg1))
11387 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11388 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11389 : TRUTH_ANDIF_EXPR,
11390 type, fold_convert_loc (loc, type, arg0), op1);
11392 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11393 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11394 && truth_value_p (TREE_CODE (arg0))
11395 && truth_value_p (TREE_CODE (arg1))
11396 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11398 location_t loc0 = expr_location_or (arg0, loc);
11399 /* Only perform transformation if ARG0 is easily inverted. */
11400 tem = fold_invert_truthvalue (loc0, arg0);
11401 if (tem)
11402 return fold_build2_loc (loc, code == VEC_COND_EXPR
11403 ? BIT_IOR_EXPR
11404 : TRUTH_ORIF_EXPR,
11405 type, fold_convert_loc (loc, type, tem),
11406 op1);
11409 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11410 if (integer_zerop (arg1)
11411 && truth_value_p (TREE_CODE (arg0))
11412 && truth_value_p (TREE_CODE (op2))
11413 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11415 location_t loc0 = expr_location_or (arg0, loc);
11416 /* Only perform transformation if ARG0 is easily inverted. */
11417 tem = fold_invert_truthvalue (loc0, arg0);
11418 if (tem)
11419 return fold_build2_loc (loc, code == VEC_COND_EXPR
11420 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11421 type, fold_convert_loc (loc, type, tem),
11422 op2);
11425 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11426 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11427 && truth_value_p (TREE_CODE (arg0))
11428 && truth_value_p (TREE_CODE (op2))
11429 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11430 return fold_build2_loc (loc, code == VEC_COND_EXPR
11431 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11432 type, fold_convert_loc (loc, type, arg0), op2);
11434 return NULL_TREE;
11436 case CALL_EXPR:
11437 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11438 of fold_ternary on them. */
11439 gcc_unreachable ();
11441 case BIT_FIELD_REF:
11442 if (TREE_CODE (arg0) == VECTOR_CST
11443 && (type == TREE_TYPE (TREE_TYPE (arg0))
11444 || (TREE_CODE (type) == VECTOR_TYPE
11445 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11447 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11448 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11449 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11450 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11452 if (n != 0
11453 && (idx % width) == 0
11454 && (n % width) == 0
11455 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11457 idx = idx / width;
11458 n = n / width;
11460 if (TREE_CODE (arg0) == VECTOR_CST)
11462 if (n == 1)
11463 return VECTOR_CST_ELT (arg0, idx);
11465 auto_vec<tree, 32> vals (n);
11466 for (unsigned i = 0; i < n; ++i)
11467 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11468 return build_vector (type, vals);
11473 /* On constants we can use native encode/interpret to constant
11474 fold (nearly) all BIT_FIELD_REFs. */
11475 if (CONSTANT_CLASS_P (arg0)
11476 && can_native_interpret_type_p (type)
11477 && BITS_PER_UNIT == 8)
11479 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11480 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11481 /* Limit us to a reasonable amount of work. To relax the
11482 other limitations we need bit-shifting of the buffer
11483 and rounding up the size. */
11484 if (bitpos % BITS_PER_UNIT == 0
11485 && bitsize % BITS_PER_UNIT == 0
11486 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11488 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11489 unsigned HOST_WIDE_INT len
11490 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11491 bitpos / BITS_PER_UNIT);
11492 if (len > 0
11493 && len * BITS_PER_UNIT >= bitsize)
11495 tree v = native_interpret_expr (type, b,
11496 bitsize / BITS_PER_UNIT);
11497 if (v)
11498 return v;
11503 return NULL_TREE;
11505 case FMA_EXPR:
11506 /* For integers we can decompose the FMA if possible. */
11507 if (TREE_CODE (arg0) == INTEGER_CST
11508 && TREE_CODE (arg1) == INTEGER_CST)
11509 return fold_build2_loc (loc, PLUS_EXPR, type,
11510 const_binop (MULT_EXPR, arg0, arg1), arg2);
11511 if (integer_zerop (arg2))
11512 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11514 return fold_fma (loc, type, arg0, arg1, arg2);
11516 case VEC_PERM_EXPR:
11517 if (TREE_CODE (arg2) == VECTOR_CST)
11519 unsigned int nelts = VECTOR_CST_NELTS (arg2), i, mask, mask2;
11520 bool need_mask_canon = false;
11521 bool need_mask_canon2 = false;
11522 bool all_in_vec0 = true;
11523 bool all_in_vec1 = true;
11524 bool maybe_identity = true;
11525 bool single_arg = (op0 == op1);
11526 bool changed = false;
11528 mask2 = 2 * nelts - 1;
11529 mask = single_arg ? (nelts - 1) : mask2;
11530 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11531 auto_vec_perm_indices sel (nelts);
11532 auto_vec_perm_indices sel2 (nelts);
11533 for (i = 0; i < nelts; i++)
11535 tree val = VECTOR_CST_ELT (arg2, i);
11536 if (TREE_CODE (val) != INTEGER_CST)
11537 return NULL_TREE;
11539 /* Make sure that the perm value is in an acceptable
11540 range. */
11541 wi::tree_to_wide_ref t = wi::to_wide (val);
11542 need_mask_canon |= wi::gtu_p (t, mask);
11543 need_mask_canon2 |= wi::gtu_p (t, mask2);
11544 unsigned int elt = t.to_uhwi () & mask;
11545 unsigned int elt2 = t.to_uhwi () & mask2;
11547 if (elt < nelts)
11548 all_in_vec1 = false;
11549 else
11550 all_in_vec0 = false;
11552 if ((elt & (nelts - 1)) != i)
11553 maybe_identity = false;
11555 sel.quick_push (elt);
11556 sel2.quick_push (elt2);
11559 if (maybe_identity)
11561 if (all_in_vec0)
11562 return op0;
11563 if (all_in_vec1)
11564 return op1;
11567 if (all_in_vec0)
11568 op1 = op0;
11569 else if (all_in_vec1)
11571 op0 = op1;
11572 for (i = 0; i < nelts; i++)
11573 sel[i] -= nelts;
11574 need_mask_canon = true;
11577 if ((TREE_CODE (op0) == VECTOR_CST
11578 || TREE_CODE (op0) == CONSTRUCTOR)
11579 && (TREE_CODE (op1) == VECTOR_CST
11580 || TREE_CODE (op1) == CONSTRUCTOR))
11582 tree t = fold_vec_perm (type, op0, op1, sel);
11583 if (t != NULL_TREE)
11584 return t;
11587 if (op0 == op1 && !single_arg)
11588 changed = true;
11590 /* Some targets are deficient and fail to expand a single
11591 argument permutation while still allowing an equivalent
11592 2-argument version. */
11593 if (need_mask_canon && arg2 == op2
11594 && !can_vec_perm_p (TYPE_MODE (type), false, &sel)
11595 && can_vec_perm_p (TYPE_MODE (type), false, &sel2))
11597 need_mask_canon = need_mask_canon2;
11598 sel = sel2;
11601 if (need_mask_canon && arg2 == op2)
11603 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11604 auto_vec<tree, 32> tsel (nelts);
11605 for (i = 0; i < nelts; i++)
11606 tsel.quick_push (build_int_cst (eltype, sel[i]));
11607 op2 = build_vector (TREE_TYPE (arg2), tsel);
11608 changed = true;
11611 if (changed)
11612 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11614 return NULL_TREE;
11616 case BIT_INSERT_EXPR:
11617 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11618 if (TREE_CODE (arg0) == INTEGER_CST
11619 && TREE_CODE (arg1) == INTEGER_CST)
11621 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11622 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11623 wide_int tem = (wi::to_wide (arg0)
11624 & wi::shifted_mask (bitpos, bitsize, true,
11625 TYPE_PRECISION (type)));
11626 wide_int tem2
11627 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11628 bitsize), bitpos);
11629 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11631 else if (TREE_CODE (arg0) == VECTOR_CST
11632 && CONSTANT_CLASS_P (arg1)
11633 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11634 TREE_TYPE (arg1)))
11636 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11637 unsigned HOST_WIDE_INT elsize
11638 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11639 if (bitpos % elsize == 0)
11641 unsigned k = bitpos / elsize;
11642 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11643 return arg0;
11644 else
11646 unsigned int nelts = VECTOR_CST_NELTS (arg0);
11647 auto_vec<tree, 32> elts (nelts);
11648 elts.quick_grow (nelts);
11649 memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
11650 sizeof (tree) * nelts);
11651 elts[k] = arg1;
11652 return build_vector (type, elts);
11656 return NULL_TREE;
11658 default:
11659 return NULL_TREE;
11660 } /* switch (code) */
11663 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11664 of an array (or vector). */
11666 tree
11667 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11669 tree index_type = NULL_TREE;
11670 offset_int low_bound = 0;
11672 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11674 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11675 if (domain_type && TYPE_MIN_VALUE (domain_type))
11677 /* Static constructors for variably sized objects makes no sense. */
11678 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11679 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11680 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11684 if (index_type)
11685 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11686 TYPE_SIGN (index_type));
11688 offset_int index = low_bound - 1;
11689 if (index_type)
11690 index = wi::ext (index, TYPE_PRECISION (index_type),
11691 TYPE_SIGN (index_type));
11693 offset_int max_index;
11694 unsigned HOST_WIDE_INT cnt;
11695 tree cfield, cval;
11697 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11699 /* Array constructor might explicitly set index, or specify a range,
11700 or leave index NULL meaning that it is next index after previous
11701 one. */
11702 if (cfield)
11704 if (TREE_CODE (cfield) == INTEGER_CST)
11705 max_index = index = wi::to_offset (cfield);
11706 else
11708 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11709 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11710 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11713 else
11715 index += 1;
11716 if (index_type)
11717 index = wi::ext (index, TYPE_PRECISION (index_type),
11718 TYPE_SIGN (index_type));
11719 max_index = index;
11722 /* Do we have match? */
11723 if (wi::cmpu (access_index, index) >= 0
11724 && wi::cmpu (access_index, max_index) <= 0)
11725 return cval;
11727 return NULL_TREE;
11730 /* Perform constant folding and related simplification of EXPR.
11731 The related simplifications include x*1 => x, x*0 => 0, etc.,
11732 and application of the associative law.
11733 NOP_EXPR conversions may be removed freely (as long as we
11734 are careful not to change the type of the overall expression).
11735 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11736 but we can constant-fold them if they have constant operands. */
11738 #ifdef ENABLE_FOLD_CHECKING
11739 # define fold(x) fold_1 (x)
11740 static tree fold_1 (tree);
11741 static
11742 #endif
11743 tree
11744 fold (tree expr)
11746 const tree t = expr;
11747 enum tree_code code = TREE_CODE (t);
11748 enum tree_code_class kind = TREE_CODE_CLASS (code);
11749 tree tem;
11750 location_t loc = EXPR_LOCATION (expr);
11752 /* Return right away if a constant. */
11753 if (kind == tcc_constant)
11754 return t;
11756 /* CALL_EXPR-like objects with variable numbers of operands are
11757 treated specially. */
11758 if (kind == tcc_vl_exp)
11760 if (code == CALL_EXPR)
11762 tem = fold_call_expr (loc, expr, false);
11763 return tem ? tem : expr;
11765 return expr;
11768 if (IS_EXPR_CODE_CLASS (kind))
11770 tree type = TREE_TYPE (t);
11771 tree op0, op1, op2;
11773 switch (TREE_CODE_LENGTH (code))
11775 case 1:
11776 op0 = TREE_OPERAND (t, 0);
11777 tem = fold_unary_loc (loc, code, type, op0);
11778 return tem ? tem : expr;
11779 case 2:
11780 op0 = TREE_OPERAND (t, 0);
11781 op1 = TREE_OPERAND (t, 1);
11782 tem = fold_binary_loc (loc, code, type, op0, op1);
11783 return tem ? tem : expr;
11784 case 3:
11785 op0 = TREE_OPERAND (t, 0);
11786 op1 = TREE_OPERAND (t, 1);
11787 op2 = TREE_OPERAND (t, 2);
11788 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11789 return tem ? tem : expr;
11790 default:
11791 break;
11795 switch (code)
11797 case ARRAY_REF:
11799 tree op0 = TREE_OPERAND (t, 0);
11800 tree op1 = TREE_OPERAND (t, 1);
11802 if (TREE_CODE (op1) == INTEGER_CST
11803 && TREE_CODE (op0) == CONSTRUCTOR
11804 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11806 tree val = get_array_ctor_element_at_index (op0,
11807 wi::to_offset (op1));
11808 if (val)
11809 return val;
11812 return t;
11815 /* Return a VECTOR_CST if possible. */
11816 case CONSTRUCTOR:
11818 tree type = TREE_TYPE (t);
11819 if (TREE_CODE (type) != VECTOR_TYPE)
11820 return t;
11822 unsigned i;
11823 tree val;
11824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11825 if (! CONSTANT_CLASS_P (val))
11826 return t;
11828 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11831 case CONST_DECL:
11832 return fold (DECL_INITIAL (t));
11834 default:
11835 return t;
11836 } /* switch (code) */
11839 #ifdef ENABLE_FOLD_CHECKING
11840 #undef fold
11842 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11843 hash_table<nofree_ptr_hash<const tree_node> > *);
11844 static void fold_check_failed (const_tree, const_tree);
11845 void print_fold_checksum (const_tree);
11847 /* When --enable-checking=fold, compute a digest of expr before
11848 and after actual fold call to see if fold did not accidentally
11849 change original expr. */
11851 tree
11852 fold (tree expr)
11854 tree ret;
11855 struct md5_ctx ctx;
11856 unsigned char checksum_before[16], checksum_after[16];
11857 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11859 md5_init_ctx (&ctx);
11860 fold_checksum_tree (expr, &ctx, &ht);
11861 md5_finish_ctx (&ctx, checksum_before);
11862 ht.empty ();
11864 ret = fold_1 (expr);
11866 md5_init_ctx (&ctx);
11867 fold_checksum_tree (expr, &ctx, &ht);
11868 md5_finish_ctx (&ctx, checksum_after);
11870 if (memcmp (checksum_before, checksum_after, 16))
11871 fold_check_failed (expr, ret);
11873 return ret;
11876 void
11877 print_fold_checksum (const_tree expr)
11879 struct md5_ctx ctx;
11880 unsigned char checksum[16], cnt;
11881 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11883 md5_init_ctx (&ctx);
11884 fold_checksum_tree (expr, &ctx, &ht);
11885 md5_finish_ctx (&ctx, checksum);
11886 for (cnt = 0; cnt < 16; ++cnt)
11887 fprintf (stderr, "%02x", checksum[cnt]);
11888 putc ('\n', stderr);
11891 static void
11892 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
11894 internal_error ("fold check: original tree changed by fold");
11897 static void
11898 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
11899 hash_table<nofree_ptr_hash <const tree_node> > *ht)
11901 const tree_node **slot;
11902 enum tree_code code;
11903 union tree_node buf;
11904 int i, len;
11906 recursive_label:
11907 if (expr == NULL)
11908 return;
11909 slot = ht->find_slot (expr, INSERT);
11910 if (*slot != NULL)
11911 return;
11912 *slot = expr;
11913 code = TREE_CODE (expr);
11914 if (TREE_CODE_CLASS (code) == tcc_declaration
11915 && HAS_DECL_ASSEMBLER_NAME_P (expr))
11917 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
11918 memcpy ((char *) &buf, expr, tree_size (expr));
11919 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
11920 buf.decl_with_vis.symtab_node = NULL;
11921 expr = (tree) &buf;
11923 else if (TREE_CODE_CLASS (code) == tcc_type
11924 && (TYPE_POINTER_TO (expr)
11925 || TYPE_REFERENCE_TO (expr)
11926 || TYPE_CACHED_VALUES_P (expr)
11927 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
11928 || TYPE_NEXT_VARIANT (expr)
11929 || TYPE_ALIAS_SET_KNOWN_P (expr)))
11931 /* Allow these fields to be modified. */
11932 tree tmp;
11933 memcpy ((char *) &buf, expr, tree_size (expr));
11934 expr = tmp = (tree) &buf;
11935 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
11936 TYPE_POINTER_TO (tmp) = NULL;
11937 TYPE_REFERENCE_TO (tmp) = NULL;
11938 TYPE_NEXT_VARIANT (tmp) = NULL;
11939 TYPE_ALIAS_SET (tmp) = -1;
11940 if (TYPE_CACHED_VALUES_P (tmp))
11942 TYPE_CACHED_VALUES_P (tmp) = 0;
11943 TYPE_CACHED_VALUES (tmp) = NULL;
11946 md5_process_bytes (expr, tree_size (expr), ctx);
11947 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
11948 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
11949 if (TREE_CODE_CLASS (code) != tcc_type
11950 && TREE_CODE_CLASS (code) != tcc_declaration
11951 && code != TREE_LIST
11952 && code != SSA_NAME
11953 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
11954 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
11955 switch (TREE_CODE_CLASS (code))
11957 case tcc_constant:
11958 switch (code)
11960 case STRING_CST:
11961 md5_process_bytes (TREE_STRING_POINTER (expr),
11962 TREE_STRING_LENGTH (expr), ctx);
11963 break;
11964 case COMPLEX_CST:
11965 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
11966 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
11967 break;
11968 case VECTOR_CST:
11969 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
11970 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
11971 break;
11972 default:
11973 break;
11975 break;
11976 case tcc_exceptional:
11977 switch (code)
11979 case TREE_LIST:
11980 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
11981 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
11982 expr = TREE_CHAIN (expr);
11983 goto recursive_label;
11984 break;
11985 case TREE_VEC:
11986 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
11987 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
11988 break;
11989 default:
11990 break;
11992 break;
11993 case tcc_expression:
11994 case tcc_reference:
11995 case tcc_comparison:
11996 case tcc_unary:
11997 case tcc_binary:
11998 case tcc_statement:
11999 case tcc_vl_exp:
12000 len = TREE_OPERAND_LENGTH (expr);
12001 for (i = 0; i < len; ++i)
12002 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12003 break;
12004 case tcc_declaration:
12005 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12006 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12007 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12009 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12010 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12011 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12012 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12013 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12016 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12018 if (TREE_CODE (expr) == FUNCTION_DECL)
12020 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12021 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12023 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12025 break;
12026 case tcc_type:
12027 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12028 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12029 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12030 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12031 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12032 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12033 if (INTEGRAL_TYPE_P (expr)
12034 || SCALAR_FLOAT_TYPE_P (expr))
12036 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12037 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12039 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12040 if (TREE_CODE (expr) == RECORD_TYPE
12041 || TREE_CODE (expr) == UNION_TYPE
12042 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12043 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12044 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12045 break;
12046 default:
12047 break;
12051 /* Helper function for outputting the checksum of a tree T. When
12052 debugging with gdb, you can "define mynext" to be "next" followed
12053 by "call debug_fold_checksum (op0)", then just trace down till the
12054 outputs differ. */
12056 DEBUG_FUNCTION void
12057 debug_fold_checksum (const_tree t)
12059 int i;
12060 unsigned char checksum[16];
12061 struct md5_ctx ctx;
12062 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12064 md5_init_ctx (&ctx);
12065 fold_checksum_tree (t, &ctx, &ht);
12066 md5_finish_ctx (&ctx, checksum);
12067 ht.empty ();
12069 for (i = 0; i < 16; i++)
12070 fprintf (stderr, "%d ", checksum[i]);
12072 fprintf (stderr, "\n");
12075 #endif
12077 /* Fold a unary tree expression with code CODE of type TYPE with an
12078 operand OP0. LOC is the location of the resulting expression.
12079 Return a folded expression if successful. Otherwise, return a tree
12080 expression with code CODE of type TYPE with an operand OP0. */
12082 tree
12083 fold_build1_loc (location_t loc,
12084 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12086 tree tem;
12087 #ifdef ENABLE_FOLD_CHECKING
12088 unsigned char checksum_before[16], checksum_after[16];
12089 struct md5_ctx ctx;
12090 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12092 md5_init_ctx (&ctx);
12093 fold_checksum_tree (op0, &ctx, &ht);
12094 md5_finish_ctx (&ctx, checksum_before);
12095 ht.empty ();
12096 #endif
12098 tem = fold_unary_loc (loc, code, type, op0);
12099 if (!tem)
12100 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12102 #ifdef ENABLE_FOLD_CHECKING
12103 md5_init_ctx (&ctx);
12104 fold_checksum_tree (op0, &ctx, &ht);
12105 md5_finish_ctx (&ctx, checksum_after);
12107 if (memcmp (checksum_before, checksum_after, 16))
12108 fold_check_failed (op0, tem);
12109 #endif
12110 return tem;
12113 /* Fold a binary tree expression with code CODE of type TYPE with
12114 operands OP0 and OP1. LOC is the location of the resulting
12115 expression. Return a folded expression if successful. Otherwise,
12116 return a tree expression with code CODE of type TYPE with operands
12117 OP0 and OP1. */
12119 tree
12120 fold_build2_loc (location_t loc,
12121 enum tree_code code, tree type, tree op0, tree op1
12122 MEM_STAT_DECL)
12124 tree tem;
12125 #ifdef ENABLE_FOLD_CHECKING
12126 unsigned char checksum_before_op0[16],
12127 checksum_before_op1[16],
12128 checksum_after_op0[16],
12129 checksum_after_op1[16];
12130 struct md5_ctx ctx;
12131 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12133 md5_init_ctx (&ctx);
12134 fold_checksum_tree (op0, &ctx, &ht);
12135 md5_finish_ctx (&ctx, checksum_before_op0);
12136 ht.empty ();
12138 md5_init_ctx (&ctx);
12139 fold_checksum_tree (op1, &ctx, &ht);
12140 md5_finish_ctx (&ctx, checksum_before_op1);
12141 ht.empty ();
12142 #endif
12144 tem = fold_binary_loc (loc, code, type, op0, op1);
12145 if (!tem)
12146 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12148 #ifdef ENABLE_FOLD_CHECKING
12149 md5_init_ctx (&ctx);
12150 fold_checksum_tree (op0, &ctx, &ht);
12151 md5_finish_ctx (&ctx, checksum_after_op0);
12152 ht.empty ();
12154 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12155 fold_check_failed (op0, tem);
12157 md5_init_ctx (&ctx);
12158 fold_checksum_tree (op1, &ctx, &ht);
12159 md5_finish_ctx (&ctx, checksum_after_op1);
12161 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12162 fold_check_failed (op1, tem);
12163 #endif
12164 return tem;
12167 /* Fold a ternary tree expression with code CODE of type TYPE with
12168 operands OP0, OP1, and OP2. Return a folded expression if
12169 successful. Otherwise, return a tree expression with code CODE of
12170 type TYPE with operands OP0, OP1, and OP2. */
12172 tree
12173 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12174 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12176 tree tem;
12177 #ifdef ENABLE_FOLD_CHECKING
12178 unsigned char checksum_before_op0[16],
12179 checksum_before_op1[16],
12180 checksum_before_op2[16],
12181 checksum_after_op0[16],
12182 checksum_after_op1[16],
12183 checksum_after_op2[16];
12184 struct md5_ctx ctx;
12185 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12187 md5_init_ctx (&ctx);
12188 fold_checksum_tree (op0, &ctx, &ht);
12189 md5_finish_ctx (&ctx, checksum_before_op0);
12190 ht.empty ();
12192 md5_init_ctx (&ctx);
12193 fold_checksum_tree (op1, &ctx, &ht);
12194 md5_finish_ctx (&ctx, checksum_before_op1);
12195 ht.empty ();
12197 md5_init_ctx (&ctx);
12198 fold_checksum_tree (op2, &ctx, &ht);
12199 md5_finish_ctx (&ctx, checksum_before_op2);
12200 ht.empty ();
12201 #endif
12203 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12204 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12205 if (!tem)
12206 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12208 #ifdef ENABLE_FOLD_CHECKING
12209 md5_init_ctx (&ctx);
12210 fold_checksum_tree (op0, &ctx, &ht);
12211 md5_finish_ctx (&ctx, checksum_after_op0);
12212 ht.empty ();
12214 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12215 fold_check_failed (op0, tem);
12217 md5_init_ctx (&ctx);
12218 fold_checksum_tree (op1, &ctx, &ht);
12219 md5_finish_ctx (&ctx, checksum_after_op1);
12220 ht.empty ();
12222 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12223 fold_check_failed (op1, tem);
12225 md5_init_ctx (&ctx);
12226 fold_checksum_tree (op2, &ctx, &ht);
12227 md5_finish_ctx (&ctx, checksum_after_op2);
12229 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12230 fold_check_failed (op2, tem);
12231 #endif
12232 return tem;
12235 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12236 arguments in ARGARRAY, and a null static chain.
12237 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12238 of type TYPE from the given operands as constructed by build_call_array. */
12240 tree
12241 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12242 int nargs, tree *argarray)
12244 tree tem;
12245 #ifdef ENABLE_FOLD_CHECKING
12246 unsigned char checksum_before_fn[16],
12247 checksum_before_arglist[16],
12248 checksum_after_fn[16],
12249 checksum_after_arglist[16];
12250 struct md5_ctx ctx;
12251 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12252 int i;
12254 md5_init_ctx (&ctx);
12255 fold_checksum_tree (fn, &ctx, &ht);
12256 md5_finish_ctx (&ctx, checksum_before_fn);
12257 ht.empty ();
12259 md5_init_ctx (&ctx);
12260 for (i = 0; i < nargs; i++)
12261 fold_checksum_tree (argarray[i], &ctx, &ht);
12262 md5_finish_ctx (&ctx, checksum_before_arglist);
12263 ht.empty ();
12264 #endif
12266 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12267 if (!tem)
12268 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12270 #ifdef ENABLE_FOLD_CHECKING
12271 md5_init_ctx (&ctx);
12272 fold_checksum_tree (fn, &ctx, &ht);
12273 md5_finish_ctx (&ctx, checksum_after_fn);
12274 ht.empty ();
12276 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12277 fold_check_failed (fn, tem);
12279 md5_init_ctx (&ctx);
12280 for (i = 0; i < nargs; i++)
12281 fold_checksum_tree (argarray[i], &ctx, &ht);
12282 md5_finish_ctx (&ctx, checksum_after_arglist);
12284 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12285 fold_check_failed (NULL_TREE, tem);
12286 #endif
12287 return tem;
12290 /* Perform constant folding and related simplification of initializer
12291 expression EXPR. These behave identically to "fold_buildN" but ignore
12292 potential run-time traps and exceptions that fold must preserve. */
12294 #define START_FOLD_INIT \
12295 int saved_signaling_nans = flag_signaling_nans;\
12296 int saved_trapping_math = flag_trapping_math;\
12297 int saved_rounding_math = flag_rounding_math;\
12298 int saved_trapv = flag_trapv;\
12299 int saved_folding_initializer = folding_initializer;\
12300 flag_signaling_nans = 0;\
12301 flag_trapping_math = 0;\
12302 flag_rounding_math = 0;\
12303 flag_trapv = 0;\
12304 folding_initializer = 1;
12306 #define END_FOLD_INIT \
12307 flag_signaling_nans = saved_signaling_nans;\
12308 flag_trapping_math = saved_trapping_math;\
12309 flag_rounding_math = saved_rounding_math;\
12310 flag_trapv = saved_trapv;\
12311 folding_initializer = saved_folding_initializer;
12313 tree
12314 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12315 tree type, tree op)
12317 tree result;
12318 START_FOLD_INIT;
12320 result = fold_build1_loc (loc, code, type, op);
12322 END_FOLD_INIT;
12323 return result;
12326 tree
12327 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12328 tree type, tree op0, tree op1)
12330 tree result;
12331 START_FOLD_INIT;
12333 result = fold_build2_loc (loc, code, type, op0, op1);
12335 END_FOLD_INIT;
12336 return result;
12339 tree
12340 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12341 int nargs, tree *argarray)
12343 tree result;
12344 START_FOLD_INIT;
12346 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12348 END_FOLD_INIT;
12349 return result;
12352 #undef START_FOLD_INIT
12353 #undef END_FOLD_INIT
12355 /* Determine if first argument is a multiple of second argument. Return 0 if
12356 it is not, or we cannot easily determined it to be.
12358 An example of the sort of thing we care about (at this point; this routine
12359 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12360 fold cases do now) is discovering that
12362 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12364 is a multiple of
12366 SAVE_EXPR (J * 8)
12368 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12370 This code also handles discovering that
12372 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12374 is a multiple of 8 so we don't have to worry about dealing with a
12375 possible remainder.
12377 Note that we *look* inside a SAVE_EXPR only to determine how it was
12378 calculated; it is not safe for fold to do much of anything else with the
12379 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12380 at run time. For example, the latter example above *cannot* be implemented
12381 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12382 evaluation time of the original SAVE_EXPR is not necessarily the same at
12383 the time the new expression is evaluated. The only optimization of this
12384 sort that would be valid is changing
12386 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12388 divided by 8 to
12390 SAVE_EXPR (I) * SAVE_EXPR (J)
12392 (where the same SAVE_EXPR (J) is used in the original and the
12393 transformed version). */
12396 multiple_of_p (tree type, const_tree top, const_tree bottom)
12398 gimple *stmt;
12399 tree t1, op1, op2;
12401 if (operand_equal_p (top, bottom, 0))
12402 return 1;
12404 if (TREE_CODE (type) != INTEGER_TYPE)
12405 return 0;
12407 switch (TREE_CODE (top))
12409 case BIT_AND_EXPR:
12410 /* Bitwise and provides a power of two multiple. If the mask is
12411 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12412 if (!integer_pow2p (bottom))
12413 return 0;
12414 /* FALLTHRU */
12416 case MULT_EXPR:
12417 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12418 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12420 case MINUS_EXPR:
12421 /* It is impossible to prove if op0 - op1 is multiple of bottom
12422 precisely, so be conservative here checking if both op0 and op1
12423 are multiple of bottom. Note we check the second operand first
12424 since it's usually simpler. */
12425 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12426 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12428 case PLUS_EXPR:
12429 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12430 as op0 - 3 if the expression has unsigned type. For example,
12431 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12432 op1 = TREE_OPERAND (top, 1);
12433 if (TYPE_UNSIGNED (type)
12434 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12435 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12436 return (multiple_of_p (type, op1, bottom)
12437 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12439 case LSHIFT_EXPR:
12440 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12442 op1 = TREE_OPERAND (top, 1);
12443 /* const_binop may not detect overflow correctly,
12444 so check for it explicitly here. */
12445 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12446 wi::to_wide (op1))
12447 && 0 != (t1 = fold_convert (type,
12448 const_binop (LSHIFT_EXPR,
12449 size_one_node,
12450 op1)))
12451 && !TREE_OVERFLOW (t1))
12452 return multiple_of_p (type, t1, bottom);
12454 return 0;
12456 case NOP_EXPR:
12457 /* Can't handle conversions from non-integral or wider integral type. */
12458 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12459 || (TYPE_PRECISION (type)
12460 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12461 return 0;
12463 /* fall through */
12465 case SAVE_EXPR:
12466 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12468 case COND_EXPR:
12469 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12470 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12472 case INTEGER_CST:
12473 if (TREE_CODE (bottom) != INTEGER_CST
12474 || integer_zerop (bottom)
12475 || (TYPE_UNSIGNED (type)
12476 && (tree_int_cst_sgn (top) < 0
12477 || tree_int_cst_sgn (bottom) < 0)))
12478 return 0;
12479 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12480 SIGNED);
12482 case SSA_NAME:
12483 if (TREE_CODE (bottom) == INTEGER_CST
12484 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12485 && gimple_code (stmt) == GIMPLE_ASSIGN)
12487 enum tree_code code = gimple_assign_rhs_code (stmt);
12489 /* Check for special cases to see if top is defined as multiple
12490 of bottom:
12492 top = (X & ~(bottom - 1) ; bottom is power of 2
12496 Y = X % bottom
12497 top = X - Y. */
12498 if (code == BIT_AND_EXPR
12499 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12500 && TREE_CODE (op2) == INTEGER_CST
12501 && integer_pow2p (bottom)
12502 && wi::multiple_of_p (wi::to_widest (op2),
12503 wi::to_widest (bottom), UNSIGNED))
12504 return 1;
12506 op1 = gimple_assign_rhs1 (stmt);
12507 if (code == MINUS_EXPR
12508 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12509 && TREE_CODE (op2) == SSA_NAME
12510 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12511 && gimple_code (stmt) == GIMPLE_ASSIGN
12512 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12513 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12514 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12515 return 1;
12518 /* fall through */
12520 default:
12521 return 0;
12525 #define tree_expr_nonnegative_warnv_p(X, Y) \
12526 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12528 #define RECURSE(X) \
12529 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12531 /* Return true if CODE or TYPE is known to be non-negative. */
12533 static bool
12534 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12536 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12537 && truth_value_p (code))
12538 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12539 have a signed:1 type (where the value is -1 and 0). */
12540 return true;
12541 return false;
12544 /* Return true if (CODE OP0) is known to be non-negative. If the return
12545 value is based on the assumption that signed overflow is undefined,
12546 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12547 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12549 bool
12550 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12551 bool *strict_overflow_p, int depth)
12553 if (TYPE_UNSIGNED (type))
12554 return true;
12556 switch (code)
12558 case ABS_EXPR:
12559 /* We can't return 1 if flag_wrapv is set because
12560 ABS_EXPR<INT_MIN> = INT_MIN. */
12561 if (!ANY_INTEGRAL_TYPE_P (type))
12562 return true;
12563 if (TYPE_OVERFLOW_UNDEFINED (type))
12565 *strict_overflow_p = true;
12566 return true;
12568 break;
12570 case NON_LVALUE_EXPR:
12571 case FLOAT_EXPR:
12572 case FIX_TRUNC_EXPR:
12573 return RECURSE (op0);
12575 CASE_CONVERT:
12577 tree inner_type = TREE_TYPE (op0);
12578 tree outer_type = type;
12580 if (TREE_CODE (outer_type) == REAL_TYPE)
12582 if (TREE_CODE (inner_type) == REAL_TYPE)
12583 return RECURSE (op0);
12584 if (INTEGRAL_TYPE_P (inner_type))
12586 if (TYPE_UNSIGNED (inner_type))
12587 return true;
12588 return RECURSE (op0);
12591 else if (INTEGRAL_TYPE_P (outer_type))
12593 if (TREE_CODE (inner_type) == REAL_TYPE)
12594 return RECURSE (op0);
12595 if (INTEGRAL_TYPE_P (inner_type))
12596 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12597 && TYPE_UNSIGNED (inner_type);
12600 break;
12602 default:
12603 return tree_simple_nonnegative_warnv_p (code, type);
12606 /* We don't know sign of `t', so be conservative and return false. */
12607 return false;
12610 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12611 value is based on the assumption that signed overflow is undefined,
12612 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12613 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12615 bool
12616 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12617 tree op1, bool *strict_overflow_p,
12618 int depth)
12620 if (TYPE_UNSIGNED (type))
12621 return true;
12623 switch (code)
12625 case POINTER_PLUS_EXPR:
12626 case PLUS_EXPR:
12627 if (FLOAT_TYPE_P (type))
12628 return RECURSE (op0) && RECURSE (op1);
12630 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12631 both unsigned and at least 2 bits shorter than the result. */
12632 if (TREE_CODE (type) == INTEGER_TYPE
12633 && TREE_CODE (op0) == NOP_EXPR
12634 && TREE_CODE (op1) == NOP_EXPR)
12636 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12637 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12638 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12639 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12641 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12642 TYPE_PRECISION (inner2)) + 1;
12643 return prec < TYPE_PRECISION (type);
12646 break;
12648 case MULT_EXPR:
12649 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12651 /* x * x is always non-negative for floating point x
12652 or without overflow. */
12653 if (operand_equal_p (op0, op1, 0)
12654 || (RECURSE (op0) && RECURSE (op1)))
12656 if (ANY_INTEGRAL_TYPE_P (type)
12657 && TYPE_OVERFLOW_UNDEFINED (type))
12658 *strict_overflow_p = true;
12659 return true;
12663 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12664 both unsigned and their total bits is shorter than the result. */
12665 if (TREE_CODE (type) == INTEGER_TYPE
12666 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12667 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12669 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12670 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12671 : TREE_TYPE (op0);
12672 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12673 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12674 : TREE_TYPE (op1);
12676 bool unsigned0 = TYPE_UNSIGNED (inner0);
12677 bool unsigned1 = TYPE_UNSIGNED (inner1);
12679 if (TREE_CODE (op0) == INTEGER_CST)
12680 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12682 if (TREE_CODE (op1) == INTEGER_CST)
12683 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12685 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12686 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12688 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12689 ? tree_int_cst_min_precision (op0, UNSIGNED)
12690 : TYPE_PRECISION (inner0);
12692 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12693 ? tree_int_cst_min_precision (op1, UNSIGNED)
12694 : TYPE_PRECISION (inner1);
12696 return precision0 + precision1 < TYPE_PRECISION (type);
12699 return false;
12701 case BIT_AND_EXPR:
12702 case MAX_EXPR:
12703 return RECURSE (op0) || RECURSE (op1);
12705 case BIT_IOR_EXPR:
12706 case BIT_XOR_EXPR:
12707 case MIN_EXPR:
12708 case RDIV_EXPR:
12709 case TRUNC_DIV_EXPR:
12710 case CEIL_DIV_EXPR:
12711 case FLOOR_DIV_EXPR:
12712 case ROUND_DIV_EXPR:
12713 return RECURSE (op0) && RECURSE (op1);
12715 case TRUNC_MOD_EXPR:
12716 return RECURSE (op0);
12718 case FLOOR_MOD_EXPR:
12719 return RECURSE (op1);
12721 case CEIL_MOD_EXPR:
12722 case ROUND_MOD_EXPR:
12723 default:
12724 return tree_simple_nonnegative_warnv_p (code, type);
12727 /* We don't know sign of `t', so be conservative and return false. */
12728 return false;
12731 /* Return true if T is known to be non-negative. If the return
12732 value is based on the assumption that signed overflow is undefined,
12733 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12734 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12736 bool
12737 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12739 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12740 return true;
12742 switch (TREE_CODE (t))
12744 case INTEGER_CST:
12745 return tree_int_cst_sgn (t) >= 0;
12747 case REAL_CST:
12748 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12750 case FIXED_CST:
12751 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12753 case COND_EXPR:
12754 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12756 case SSA_NAME:
12757 /* Limit the depth of recursion to avoid quadratic behavior.
12758 This is expected to catch almost all occurrences in practice.
12759 If this code misses important cases that unbounded recursion
12760 would not, passes that need this information could be revised
12761 to provide it through dataflow propagation. */
12762 return (!name_registered_for_update_p (t)
12763 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12764 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12765 strict_overflow_p, depth));
12767 default:
12768 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12772 /* Return true if T is known to be non-negative. If the return
12773 value is based on the assumption that signed overflow is undefined,
12774 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12775 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12777 bool
12778 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12779 bool *strict_overflow_p, int depth)
12781 switch (fn)
12783 CASE_CFN_ACOS:
12784 CASE_CFN_ACOSH:
12785 CASE_CFN_CABS:
12786 CASE_CFN_COSH:
12787 CASE_CFN_ERFC:
12788 CASE_CFN_EXP:
12789 CASE_CFN_EXP10:
12790 CASE_CFN_EXP2:
12791 CASE_CFN_FABS:
12792 CASE_CFN_FDIM:
12793 CASE_CFN_HYPOT:
12794 CASE_CFN_POW10:
12795 CASE_CFN_FFS:
12796 CASE_CFN_PARITY:
12797 CASE_CFN_POPCOUNT:
12798 CASE_CFN_CLZ:
12799 CASE_CFN_CLRSB:
12800 case CFN_BUILT_IN_BSWAP32:
12801 case CFN_BUILT_IN_BSWAP64:
12802 /* Always true. */
12803 return true;
12805 CASE_CFN_SQRT:
12806 /* sqrt(-0.0) is -0.0. */
12807 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12808 return true;
12809 return RECURSE (arg0);
12811 CASE_CFN_ASINH:
12812 CASE_CFN_ATAN:
12813 CASE_CFN_ATANH:
12814 CASE_CFN_CBRT:
12815 CASE_CFN_CEIL:
12816 CASE_CFN_ERF:
12817 CASE_CFN_EXPM1:
12818 CASE_CFN_FLOOR:
12819 CASE_CFN_FMOD:
12820 CASE_CFN_FREXP:
12821 CASE_CFN_ICEIL:
12822 CASE_CFN_IFLOOR:
12823 CASE_CFN_IRINT:
12824 CASE_CFN_IROUND:
12825 CASE_CFN_LCEIL:
12826 CASE_CFN_LDEXP:
12827 CASE_CFN_LFLOOR:
12828 CASE_CFN_LLCEIL:
12829 CASE_CFN_LLFLOOR:
12830 CASE_CFN_LLRINT:
12831 CASE_CFN_LLROUND:
12832 CASE_CFN_LRINT:
12833 CASE_CFN_LROUND:
12834 CASE_CFN_MODF:
12835 CASE_CFN_NEARBYINT:
12836 CASE_CFN_RINT:
12837 CASE_CFN_ROUND:
12838 CASE_CFN_SCALB:
12839 CASE_CFN_SCALBLN:
12840 CASE_CFN_SCALBN:
12841 CASE_CFN_SIGNBIT:
12842 CASE_CFN_SIGNIFICAND:
12843 CASE_CFN_SINH:
12844 CASE_CFN_TANH:
12845 CASE_CFN_TRUNC:
12846 /* True if the 1st argument is nonnegative. */
12847 return RECURSE (arg0);
12849 CASE_CFN_FMAX:
12850 /* True if the 1st OR 2nd arguments are nonnegative. */
12851 return RECURSE (arg0) || RECURSE (arg1);
12853 CASE_CFN_FMIN:
12854 /* True if the 1st AND 2nd arguments are nonnegative. */
12855 return RECURSE (arg0) && RECURSE (arg1);
12857 CASE_CFN_COPYSIGN:
12858 /* True if the 2nd argument is nonnegative. */
12859 return RECURSE (arg1);
12861 CASE_CFN_POWI:
12862 /* True if the 1st argument is nonnegative or the second
12863 argument is an even integer. */
12864 if (TREE_CODE (arg1) == INTEGER_CST
12865 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12866 return true;
12867 return RECURSE (arg0);
12869 CASE_CFN_POW:
12870 /* True if the 1st argument is nonnegative or the second
12871 argument is an even integer valued real. */
12872 if (TREE_CODE (arg1) == REAL_CST)
12874 REAL_VALUE_TYPE c;
12875 HOST_WIDE_INT n;
12877 c = TREE_REAL_CST (arg1);
12878 n = real_to_integer (&c);
12879 if ((n & 1) == 0)
12881 REAL_VALUE_TYPE cint;
12882 real_from_integer (&cint, VOIDmode, n, SIGNED);
12883 if (real_identical (&c, &cint))
12884 return true;
12887 return RECURSE (arg0);
12889 default:
12890 break;
12892 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
12895 /* Return true if T is known to be non-negative. If the return
12896 value is based on the assumption that signed overflow is undefined,
12897 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12898 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12900 static bool
12901 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12903 enum tree_code code = TREE_CODE (t);
12904 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12905 return true;
12907 switch (code)
12909 case TARGET_EXPR:
12911 tree temp = TARGET_EXPR_SLOT (t);
12912 t = TARGET_EXPR_INITIAL (t);
12914 /* If the initializer is non-void, then it's a normal expression
12915 that will be assigned to the slot. */
12916 if (!VOID_TYPE_P (t))
12917 return RECURSE (t);
12919 /* Otherwise, the initializer sets the slot in some way. One common
12920 way is an assignment statement at the end of the initializer. */
12921 while (1)
12923 if (TREE_CODE (t) == BIND_EXPR)
12924 t = expr_last (BIND_EXPR_BODY (t));
12925 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
12926 || TREE_CODE (t) == TRY_CATCH_EXPR)
12927 t = expr_last (TREE_OPERAND (t, 0));
12928 else if (TREE_CODE (t) == STATEMENT_LIST)
12929 t = expr_last (t);
12930 else
12931 break;
12933 if (TREE_CODE (t) == MODIFY_EXPR
12934 && TREE_OPERAND (t, 0) == temp)
12935 return RECURSE (TREE_OPERAND (t, 1));
12937 return false;
12940 case CALL_EXPR:
12942 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
12943 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
12945 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
12946 get_call_combined_fn (t),
12947 arg0,
12948 arg1,
12949 strict_overflow_p, depth);
12951 case COMPOUND_EXPR:
12952 case MODIFY_EXPR:
12953 return RECURSE (TREE_OPERAND (t, 1));
12955 case BIND_EXPR:
12956 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
12958 case SAVE_EXPR:
12959 return RECURSE (TREE_OPERAND (t, 0));
12961 default:
12962 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12966 #undef RECURSE
12967 #undef tree_expr_nonnegative_warnv_p
12969 /* Return true if T is known to be non-negative. If the return
12970 value is based on the assumption that signed overflow is undefined,
12971 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12972 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12974 bool
12975 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12977 enum tree_code code;
12978 if (t == error_mark_node)
12979 return false;
12981 code = TREE_CODE (t);
12982 switch (TREE_CODE_CLASS (code))
12984 case tcc_binary:
12985 case tcc_comparison:
12986 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
12987 TREE_TYPE (t),
12988 TREE_OPERAND (t, 0),
12989 TREE_OPERAND (t, 1),
12990 strict_overflow_p, depth);
12992 case tcc_unary:
12993 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
12994 TREE_TYPE (t),
12995 TREE_OPERAND (t, 0),
12996 strict_overflow_p, depth);
12998 case tcc_constant:
12999 case tcc_declaration:
13000 case tcc_reference:
13001 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13003 default:
13004 break;
13007 switch (code)
13009 case TRUTH_AND_EXPR:
13010 case TRUTH_OR_EXPR:
13011 case TRUTH_XOR_EXPR:
13012 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13013 TREE_TYPE (t),
13014 TREE_OPERAND (t, 0),
13015 TREE_OPERAND (t, 1),
13016 strict_overflow_p, depth);
13017 case TRUTH_NOT_EXPR:
13018 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13019 TREE_TYPE (t),
13020 TREE_OPERAND (t, 0),
13021 strict_overflow_p, depth);
13023 case COND_EXPR:
13024 case CONSTRUCTOR:
13025 case OBJ_TYPE_REF:
13026 case ASSERT_EXPR:
13027 case ADDR_EXPR:
13028 case WITH_SIZE_EXPR:
13029 case SSA_NAME:
13030 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13032 default:
13033 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13037 /* Return true if `t' is known to be non-negative. Handle warnings
13038 about undefined signed overflow. */
13040 bool
13041 tree_expr_nonnegative_p (tree t)
13043 bool ret, strict_overflow_p;
13045 strict_overflow_p = false;
13046 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13047 if (strict_overflow_p)
13048 fold_overflow_warning (("assuming signed overflow does not occur when "
13049 "determining that expression is always "
13050 "non-negative"),
13051 WARN_STRICT_OVERFLOW_MISC);
13052 return ret;
13056 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13057 For floating point we further ensure that T is not denormal.
13058 Similar logic is present in nonzero_address in rtlanal.h.
13060 If the return value is based on the assumption that signed overflow
13061 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13062 change *STRICT_OVERFLOW_P. */
13064 bool
13065 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13066 bool *strict_overflow_p)
13068 switch (code)
13070 case ABS_EXPR:
13071 return tree_expr_nonzero_warnv_p (op0,
13072 strict_overflow_p);
13074 case NOP_EXPR:
13076 tree inner_type = TREE_TYPE (op0);
13077 tree outer_type = type;
13079 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13080 && tree_expr_nonzero_warnv_p (op0,
13081 strict_overflow_p));
13083 break;
13085 case NON_LVALUE_EXPR:
13086 return tree_expr_nonzero_warnv_p (op0,
13087 strict_overflow_p);
13089 default:
13090 break;
13093 return false;
13096 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13097 For floating point we further ensure that T is not denormal.
13098 Similar logic is present in nonzero_address in rtlanal.h.
13100 If the return value is based on the assumption that signed overflow
13101 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13102 change *STRICT_OVERFLOW_P. */
13104 bool
13105 tree_binary_nonzero_warnv_p (enum tree_code code,
13106 tree type,
13107 tree op0,
13108 tree op1, bool *strict_overflow_p)
13110 bool sub_strict_overflow_p;
13111 switch (code)
13113 case POINTER_PLUS_EXPR:
13114 case PLUS_EXPR:
13115 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13117 /* With the presence of negative values it is hard
13118 to say something. */
13119 sub_strict_overflow_p = false;
13120 if (!tree_expr_nonnegative_warnv_p (op0,
13121 &sub_strict_overflow_p)
13122 || !tree_expr_nonnegative_warnv_p (op1,
13123 &sub_strict_overflow_p))
13124 return false;
13125 /* One of operands must be positive and the other non-negative. */
13126 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13127 overflows, on a twos-complement machine the sum of two
13128 nonnegative numbers can never be zero. */
13129 return (tree_expr_nonzero_warnv_p (op0,
13130 strict_overflow_p)
13131 || tree_expr_nonzero_warnv_p (op1,
13132 strict_overflow_p));
13134 break;
13136 case MULT_EXPR:
13137 if (TYPE_OVERFLOW_UNDEFINED (type))
13139 if (tree_expr_nonzero_warnv_p (op0,
13140 strict_overflow_p)
13141 && tree_expr_nonzero_warnv_p (op1,
13142 strict_overflow_p))
13144 *strict_overflow_p = true;
13145 return true;
13148 break;
13150 case MIN_EXPR:
13151 sub_strict_overflow_p = false;
13152 if (tree_expr_nonzero_warnv_p (op0,
13153 &sub_strict_overflow_p)
13154 && tree_expr_nonzero_warnv_p (op1,
13155 &sub_strict_overflow_p))
13157 if (sub_strict_overflow_p)
13158 *strict_overflow_p = true;
13160 break;
13162 case MAX_EXPR:
13163 sub_strict_overflow_p = false;
13164 if (tree_expr_nonzero_warnv_p (op0,
13165 &sub_strict_overflow_p))
13167 if (sub_strict_overflow_p)
13168 *strict_overflow_p = true;
13170 /* When both operands are nonzero, then MAX must be too. */
13171 if (tree_expr_nonzero_warnv_p (op1,
13172 strict_overflow_p))
13173 return true;
13175 /* MAX where operand 0 is positive is positive. */
13176 return tree_expr_nonnegative_warnv_p (op0,
13177 strict_overflow_p);
13179 /* MAX where operand 1 is positive is positive. */
13180 else if (tree_expr_nonzero_warnv_p (op1,
13181 &sub_strict_overflow_p)
13182 && tree_expr_nonnegative_warnv_p (op1,
13183 &sub_strict_overflow_p))
13185 if (sub_strict_overflow_p)
13186 *strict_overflow_p = true;
13187 return true;
13189 break;
13191 case BIT_IOR_EXPR:
13192 return (tree_expr_nonzero_warnv_p (op1,
13193 strict_overflow_p)
13194 || tree_expr_nonzero_warnv_p (op0,
13195 strict_overflow_p));
13197 default:
13198 break;
13201 return false;
13204 /* Return true when T is an address and is known to be nonzero.
13205 For floating point we further ensure that T is not denormal.
13206 Similar logic is present in nonzero_address in rtlanal.h.
13208 If the return value is based on the assumption that signed overflow
13209 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13210 change *STRICT_OVERFLOW_P. */
13212 bool
13213 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13215 bool sub_strict_overflow_p;
13216 switch (TREE_CODE (t))
13218 case INTEGER_CST:
13219 return !integer_zerop (t);
13221 case ADDR_EXPR:
13223 tree base = TREE_OPERAND (t, 0);
13225 if (!DECL_P (base))
13226 base = get_base_address (base);
13228 if (base && TREE_CODE (base) == TARGET_EXPR)
13229 base = TARGET_EXPR_SLOT (base);
13231 if (!base)
13232 return false;
13234 /* For objects in symbol table check if we know they are non-zero.
13235 Don't do anything for variables and functions before symtab is built;
13236 it is quite possible that they will be declared weak later. */
13237 int nonzero_addr = maybe_nonzero_address (base);
13238 if (nonzero_addr >= 0)
13239 return nonzero_addr;
13241 /* Constants are never weak. */
13242 if (CONSTANT_CLASS_P (base))
13243 return true;
13245 return false;
13248 case COND_EXPR:
13249 sub_strict_overflow_p = false;
13250 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13251 &sub_strict_overflow_p)
13252 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13253 &sub_strict_overflow_p))
13255 if (sub_strict_overflow_p)
13256 *strict_overflow_p = true;
13257 return true;
13259 break;
13261 case SSA_NAME:
13262 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13263 break;
13264 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13266 default:
13267 break;
13269 return false;
13272 #define integer_valued_real_p(X) \
13273 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13275 #define RECURSE(X) \
13276 ((integer_valued_real_p) (X, depth + 1))
13278 /* Return true if the floating point result of (CODE OP0) has an
13279 integer value. We also allow +Inf, -Inf and NaN to be considered
13280 integer values. Return false for signaling NaN.
13282 DEPTH is the current nesting depth of the query. */
13284 bool
13285 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13287 switch (code)
13289 case FLOAT_EXPR:
13290 return true;
13292 case ABS_EXPR:
13293 return RECURSE (op0);
13295 CASE_CONVERT:
13297 tree type = TREE_TYPE (op0);
13298 if (TREE_CODE (type) == INTEGER_TYPE)
13299 return true;
13300 if (TREE_CODE (type) == REAL_TYPE)
13301 return RECURSE (op0);
13302 break;
13305 default:
13306 break;
13308 return false;
13311 /* Return true if the floating point result of (CODE OP0 OP1) has an
13312 integer value. We also allow +Inf, -Inf and NaN to be considered
13313 integer values. Return false for signaling NaN.
13315 DEPTH is the current nesting depth of the query. */
13317 bool
13318 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13320 switch (code)
13322 case PLUS_EXPR:
13323 case MINUS_EXPR:
13324 case MULT_EXPR:
13325 case MIN_EXPR:
13326 case MAX_EXPR:
13327 return RECURSE (op0) && RECURSE (op1);
13329 default:
13330 break;
13332 return false;
13335 /* Return true if the floating point result of calling FNDECL with arguments
13336 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13337 considered integer values. Return false for signaling NaN. If FNDECL
13338 takes fewer than 2 arguments, the remaining ARGn are null.
13340 DEPTH is the current nesting depth of the query. */
13342 bool
13343 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13345 switch (fn)
13347 CASE_CFN_CEIL:
13348 CASE_CFN_FLOOR:
13349 CASE_CFN_NEARBYINT:
13350 CASE_CFN_RINT:
13351 CASE_CFN_ROUND:
13352 CASE_CFN_TRUNC:
13353 return true;
13355 CASE_CFN_FMIN:
13356 CASE_CFN_FMAX:
13357 return RECURSE (arg0) && RECURSE (arg1);
13359 default:
13360 break;
13362 return false;
13365 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13366 has an integer value. We also allow +Inf, -Inf and NaN to be
13367 considered integer values. Return false for signaling NaN.
13369 DEPTH is the current nesting depth of the query. */
13371 bool
13372 integer_valued_real_single_p (tree t, int depth)
13374 switch (TREE_CODE (t))
13376 case REAL_CST:
13377 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13379 case COND_EXPR:
13380 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13382 case SSA_NAME:
13383 /* Limit the depth of recursion to avoid quadratic behavior.
13384 This is expected to catch almost all occurrences in practice.
13385 If this code misses important cases that unbounded recursion
13386 would not, passes that need this information could be revised
13387 to provide it through dataflow propagation. */
13388 return (!name_registered_for_update_p (t)
13389 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13390 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13391 depth));
13393 default:
13394 break;
13396 return false;
13399 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13400 has an integer value. We also allow +Inf, -Inf and NaN to be
13401 considered integer values. Return false for signaling NaN.
13403 DEPTH is the current nesting depth of the query. */
13405 static bool
13406 integer_valued_real_invalid_p (tree t, int depth)
13408 switch (TREE_CODE (t))
13410 case COMPOUND_EXPR:
13411 case MODIFY_EXPR:
13412 case BIND_EXPR:
13413 return RECURSE (TREE_OPERAND (t, 1));
13415 case SAVE_EXPR:
13416 return RECURSE (TREE_OPERAND (t, 0));
13418 default:
13419 break;
13421 return false;
13424 #undef RECURSE
13425 #undef integer_valued_real_p
13427 /* Return true if the floating point expression T has an integer value.
13428 We also allow +Inf, -Inf and NaN to be considered integer values.
13429 Return false for signaling NaN.
13431 DEPTH is the current nesting depth of the query. */
13433 bool
13434 integer_valued_real_p (tree t, int depth)
13436 if (t == error_mark_node)
13437 return false;
13439 tree_code code = TREE_CODE (t);
13440 switch (TREE_CODE_CLASS (code))
13442 case tcc_binary:
13443 case tcc_comparison:
13444 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13445 TREE_OPERAND (t, 1), depth);
13447 case tcc_unary:
13448 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13450 case tcc_constant:
13451 case tcc_declaration:
13452 case tcc_reference:
13453 return integer_valued_real_single_p (t, depth);
13455 default:
13456 break;
13459 switch (code)
13461 case COND_EXPR:
13462 case SSA_NAME:
13463 return integer_valued_real_single_p (t, depth);
13465 case CALL_EXPR:
13467 tree arg0 = (call_expr_nargs (t) > 0
13468 ? CALL_EXPR_ARG (t, 0)
13469 : NULL_TREE);
13470 tree arg1 = (call_expr_nargs (t) > 1
13471 ? CALL_EXPR_ARG (t, 1)
13472 : NULL_TREE);
13473 return integer_valued_real_call_p (get_call_combined_fn (t),
13474 arg0, arg1, depth);
13477 default:
13478 return integer_valued_real_invalid_p (t, depth);
13482 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13483 attempt to fold the expression to a constant without modifying TYPE,
13484 OP0 or OP1.
13486 If the expression could be simplified to a constant, then return
13487 the constant. If the expression would not be simplified to a
13488 constant, then return NULL_TREE. */
13490 tree
13491 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13493 tree tem = fold_binary (code, type, op0, op1);
13494 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13497 /* Given the components of a unary expression CODE, TYPE and OP0,
13498 attempt to fold the expression to a constant without modifying
13499 TYPE or OP0.
13501 If the expression could be simplified to a constant, then return
13502 the constant. If the expression would not be simplified to a
13503 constant, then return NULL_TREE. */
13505 tree
13506 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13508 tree tem = fold_unary (code, type, op0);
13509 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13512 /* If EXP represents referencing an element in a constant string
13513 (either via pointer arithmetic or array indexing), return the
13514 tree representing the value accessed, otherwise return NULL. */
13516 tree
13517 fold_read_from_constant_string (tree exp)
13519 if ((TREE_CODE (exp) == INDIRECT_REF
13520 || TREE_CODE (exp) == ARRAY_REF)
13521 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13523 tree exp1 = TREE_OPERAND (exp, 0);
13524 tree index;
13525 tree string;
13526 location_t loc = EXPR_LOCATION (exp);
13528 if (TREE_CODE (exp) == INDIRECT_REF)
13529 string = string_constant (exp1, &index);
13530 else
13532 tree low_bound = array_ref_low_bound (exp);
13533 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13535 /* Optimize the special-case of a zero lower bound.
13537 We convert the low_bound to sizetype to avoid some problems
13538 with constant folding. (E.g. suppose the lower bound is 1,
13539 and its mode is QI. Without the conversion,l (ARRAY
13540 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13541 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13542 if (! integer_zerop (low_bound))
13543 index = size_diffop_loc (loc, index,
13544 fold_convert_loc (loc, sizetype, low_bound));
13546 string = exp1;
13549 scalar_int_mode char_mode;
13550 if (string
13551 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13552 && TREE_CODE (string) == STRING_CST
13553 && TREE_CODE (index) == INTEGER_CST
13554 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13555 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13556 &char_mode)
13557 && GET_MODE_SIZE (char_mode) == 1)
13558 return build_int_cst_type (TREE_TYPE (exp),
13559 (TREE_STRING_POINTER (string)
13560 [TREE_INT_CST_LOW (index)]));
13562 return NULL;
13565 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13566 an integer constant, real, or fixed-point constant.
13568 TYPE is the type of the result. */
13570 static tree
13571 fold_negate_const (tree arg0, tree type)
13573 tree t = NULL_TREE;
13575 switch (TREE_CODE (arg0))
13577 case INTEGER_CST:
13579 bool overflow;
13580 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13581 t = force_fit_type (type, val, 1,
13582 (overflow && ! TYPE_UNSIGNED (type))
13583 || TREE_OVERFLOW (arg0));
13584 break;
13587 case REAL_CST:
13588 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13589 break;
13591 case FIXED_CST:
13593 FIXED_VALUE_TYPE f;
13594 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13595 &(TREE_FIXED_CST (arg0)), NULL,
13596 TYPE_SATURATING (type));
13597 t = build_fixed (type, f);
13598 /* Propagate overflow flags. */
13599 if (overflow_p | TREE_OVERFLOW (arg0))
13600 TREE_OVERFLOW (t) = 1;
13601 break;
13604 default:
13605 gcc_unreachable ();
13608 return t;
13611 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13612 an integer constant or real constant.
13614 TYPE is the type of the result. */
13616 tree
13617 fold_abs_const (tree arg0, tree type)
13619 tree t = NULL_TREE;
13621 switch (TREE_CODE (arg0))
13623 case INTEGER_CST:
13625 /* If the value is unsigned or non-negative, then the absolute value
13626 is the same as the ordinary value. */
13627 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13628 t = arg0;
13630 /* If the value is negative, then the absolute value is
13631 its negation. */
13632 else
13634 bool overflow;
13635 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13636 t = force_fit_type (type, val, -1,
13637 overflow | TREE_OVERFLOW (arg0));
13640 break;
13642 case REAL_CST:
13643 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13644 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13645 else
13646 t = arg0;
13647 break;
13649 default:
13650 gcc_unreachable ();
13653 return t;
13656 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13657 constant. TYPE is the type of the result. */
13659 static tree
13660 fold_not_const (const_tree arg0, tree type)
13662 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13664 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13667 /* Given CODE, a relational operator, the target type, TYPE and two
13668 constant operands OP0 and OP1, return the result of the
13669 relational operation. If the result is not a compile time
13670 constant, then return NULL_TREE. */
13672 static tree
13673 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13675 int result, invert;
13677 /* From here on, the only cases we handle are when the result is
13678 known to be a constant. */
13680 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13682 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13683 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13685 /* Handle the cases where either operand is a NaN. */
13686 if (real_isnan (c0) || real_isnan (c1))
13688 switch (code)
13690 case EQ_EXPR:
13691 case ORDERED_EXPR:
13692 result = 0;
13693 break;
13695 case NE_EXPR:
13696 case UNORDERED_EXPR:
13697 case UNLT_EXPR:
13698 case UNLE_EXPR:
13699 case UNGT_EXPR:
13700 case UNGE_EXPR:
13701 case UNEQ_EXPR:
13702 result = 1;
13703 break;
13705 case LT_EXPR:
13706 case LE_EXPR:
13707 case GT_EXPR:
13708 case GE_EXPR:
13709 case LTGT_EXPR:
13710 if (flag_trapping_math)
13711 return NULL_TREE;
13712 result = 0;
13713 break;
13715 default:
13716 gcc_unreachable ();
13719 return constant_boolean_node (result, type);
13722 return constant_boolean_node (real_compare (code, c0, c1), type);
13725 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13727 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13728 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13729 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13732 /* Handle equality/inequality of complex constants. */
13733 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13735 tree rcond = fold_relational_const (code, type,
13736 TREE_REALPART (op0),
13737 TREE_REALPART (op1));
13738 tree icond = fold_relational_const (code, type,
13739 TREE_IMAGPART (op0),
13740 TREE_IMAGPART (op1));
13741 if (code == EQ_EXPR)
13742 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13743 else if (code == NE_EXPR)
13744 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13745 else
13746 return NULL_TREE;
13749 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13751 if (!VECTOR_TYPE_P (type))
13753 /* Have vector comparison with scalar boolean result. */
13754 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13755 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13756 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13758 tree elem0 = VECTOR_CST_ELT (op0, i);
13759 tree elem1 = VECTOR_CST_ELT (op1, i);
13760 tree tmp = fold_relational_const (code, type, elem0, elem1);
13761 if (tmp == NULL_TREE)
13762 return NULL_TREE;
13763 if (integer_zerop (tmp))
13764 return constant_boolean_node (false, type);
13766 return constant_boolean_node (true, type);
13768 unsigned count = VECTOR_CST_NELTS (op0);
13769 gcc_assert (VECTOR_CST_NELTS (op1) == count
13770 && TYPE_VECTOR_SUBPARTS (type) == count);
13772 auto_vec<tree, 32> elts (count);
13773 for (unsigned i = 0; i < count; i++)
13775 tree elem_type = TREE_TYPE (type);
13776 tree elem0 = VECTOR_CST_ELT (op0, i);
13777 tree elem1 = VECTOR_CST_ELT (op1, i);
13779 tree tem = fold_relational_const (code, elem_type,
13780 elem0, elem1);
13782 if (tem == NULL_TREE)
13783 return NULL_TREE;
13785 elts.quick_push (build_int_cst (elem_type,
13786 integer_zerop (tem) ? 0 : -1));
13789 return build_vector (type, elts);
13792 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13794 To compute GT, swap the arguments and do LT.
13795 To compute GE, do LT and invert the result.
13796 To compute LE, swap the arguments, do LT and invert the result.
13797 To compute NE, do EQ and invert the result.
13799 Therefore, the code below must handle only EQ and LT. */
13801 if (code == LE_EXPR || code == GT_EXPR)
13803 std::swap (op0, op1);
13804 code = swap_tree_comparison (code);
13807 /* Note that it is safe to invert for real values here because we
13808 have already handled the one case that it matters. */
13810 invert = 0;
13811 if (code == NE_EXPR || code == GE_EXPR)
13813 invert = 1;
13814 code = invert_tree_comparison (code, false);
13817 /* Compute a result for LT or EQ if args permit;
13818 Otherwise return T. */
13819 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13821 if (code == EQ_EXPR)
13822 result = tree_int_cst_equal (op0, op1);
13823 else
13824 result = tree_int_cst_lt (op0, op1);
13826 else
13827 return NULL_TREE;
13829 if (invert)
13830 result ^= 1;
13831 return constant_boolean_node (result, type);
13834 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13835 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13836 itself. */
13838 tree
13839 fold_build_cleanup_point_expr (tree type, tree expr)
13841 /* If the expression does not have side effects then we don't have to wrap
13842 it with a cleanup point expression. */
13843 if (!TREE_SIDE_EFFECTS (expr))
13844 return expr;
13846 /* If the expression is a return, check to see if the expression inside the
13847 return has no side effects or the right hand side of the modify expression
13848 inside the return. If either don't have side effects set we don't need to
13849 wrap the expression in a cleanup point expression. Note we don't check the
13850 left hand side of the modify because it should always be a return decl. */
13851 if (TREE_CODE (expr) == RETURN_EXPR)
13853 tree op = TREE_OPERAND (expr, 0);
13854 if (!op || !TREE_SIDE_EFFECTS (op))
13855 return expr;
13856 op = TREE_OPERAND (op, 1);
13857 if (!TREE_SIDE_EFFECTS (op))
13858 return expr;
13861 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
13864 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13865 of an indirection through OP0, or NULL_TREE if no simplification is
13866 possible. */
13868 tree
13869 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13871 tree sub = op0;
13872 tree subtype;
13874 STRIP_NOPS (sub);
13875 subtype = TREE_TYPE (sub);
13876 if (!POINTER_TYPE_P (subtype)
13877 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
13878 return NULL_TREE;
13880 if (TREE_CODE (sub) == ADDR_EXPR)
13882 tree op = TREE_OPERAND (sub, 0);
13883 tree optype = TREE_TYPE (op);
13884 /* *&CONST_DECL -> to the value of the const decl. */
13885 if (TREE_CODE (op) == CONST_DECL)
13886 return DECL_INITIAL (op);
13887 /* *&p => p; make sure to handle *&"str"[cst] here. */
13888 if (type == optype)
13890 tree fop = fold_read_from_constant_string (op);
13891 if (fop)
13892 return fop;
13893 else
13894 return op;
13896 /* *(foo *)&fooarray => fooarray[0] */
13897 else if (TREE_CODE (optype) == ARRAY_TYPE
13898 && type == TREE_TYPE (optype)
13899 && (!in_gimple_form
13900 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13902 tree type_domain = TYPE_DOMAIN (optype);
13903 tree min_val = size_zero_node;
13904 if (type_domain && TYPE_MIN_VALUE (type_domain))
13905 min_val = TYPE_MIN_VALUE (type_domain);
13906 if (in_gimple_form
13907 && TREE_CODE (min_val) != INTEGER_CST)
13908 return NULL_TREE;
13909 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13910 NULL_TREE, NULL_TREE);
13912 /* *(foo *)&complexfoo => __real__ complexfoo */
13913 else if (TREE_CODE (optype) == COMPLEX_TYPE
13914 && type == TREE_TYPE (optype))
13915 return fold_build1_loc (loc, REALPART_EXPR, type, op);
13916 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
13917 else if (TREE_CODE (optype) == VECTOR_TYPE
13918 && type == TREE_TYPE (optype))
13920 tree part_width = TYPE_SIZE (type);
13921 tree index = bitsize_int (0);
13922 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
13926 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
13927 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
13929 tree op00 = TREE_OPERAND (sub, 0);
13930 tree op01 = TREE_OPERAND (sub, 1);
13932 STRIP_NOPS (op00);
13933 if (TREE_CODE (op00) == ADDR_EXPR)
13935 tree op00type;
13936 op00 = TREE_OPERAND (op00, 0);
13937 op00type = TREE_TYPE (op00);
13939 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
13940 if (TREE_CODE (op00type) == VECTOR_TYPE
13941 && type == TREE_TYPE (op00type))
13943 tree part_width = TYPE_SIZE (type);
13944 unsigned HOST_WIDE_INT max_offset
13945 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
13946 * TYPE_VECTOR_SUBPARTS (op00type));
13947 if (tree_int_cst_sign_bit (op01) == 0
13948 && compare_tree_int (op01, max_offset) == -1)
13950 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
13951 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
13952 tree index = bitsize_int (indexi);
13953 return fold_build3_loc (loc,
13954 BIT_FIELD_REF, type, op00,
13955 part_width, index);
13958 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
13959 else if (TREE_CODE (op00type) == COMPLEX_TYPE
13960 && type == TREE_TYPE (op00type))
13962 tree size = TYPE_SIZE_UNIT (type);
13963 if (tree_int_cst_equal (size, op01))
13964 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
13966 /* ((foo *)&fooarray)[1] => fooarray[1] */
13967 else if (TREE_CODE (op00type) == ARRAY_TYPE
13968 && type == TREE_TYPE (op00type))
13970 tree type_domain = TYPE_DOMAIN (op00type);
13971 tree min = size_zero_node;
13972 if (type_domain && TYPE_MIN_VALUE (type_domain))
13973 min = TYPE_MIN_VALUE (type_domain);
13974 offset_int off = wi::to_offset (op01);
13975 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
13976 offset_int remainder;
13977 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
13978 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
13980 off = off + wi::to_offset (min);
13981 op01 = wide_int_to_tree (sizetype, off);
13982 return build4_loc (loc, ARRAY_REF, type, op00, op01,
13983 NULL_TREE, NULL_TREE);
13989 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
13990 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
13991 && type == TREE_TYPE (TREE_TYPE (subtype))
13992 && (!in_gimple_form
13993 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13995 tree type_domain;
13996 tree min_val = size_zero_node;
13997 sub = build_fold_indirect_ref_loc (loc, sub);
13998 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
13999 if (type_domain && TYPE_MIN_VALUE (type_domain))
14000 min_val = TYPE_MIN_VALUE (type_domain);
14001 if (in_gimple_form
14002 && TREE_CODE (min_val) != INTEGER_CST)
14003 return NULL_TREE;
14004 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14005 NULL_TREE);
14008 return NULL_TREE;
14011 /* Builds an expression for an indirection through T, simplifying some
14012 cases. */
14014 tree
14015 build_fold_indirect_ref_loc (location_t loc, tree t)
14017 tree type = TREE_TYPE (TREE_TYPE (t));
14018 tree sub = fold_indirect_ref_1 (loc, type, t);
14020 if (sub)
14021 return sub;
14023 return build1_loc (loc, INDIRECT_REF, type, t);
14026 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14028 tree
14029 fold_indirect_ref_loc (location_t loc, tree t)
14031 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14033 if (sub)
14034 return sub;
14035 else
14036 return t;
14039 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14040 whose result is ignored. The type of the returned tree need not be
14041 the same as the original expression. */
14043 tree
14044 fold_ignored_result (tree t)
14046 if (!TREE_SIDE_EFFECTS (t))
14047 return integer_zero_node;
14049 for (;;)
14050 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14052 case tcc_unary:
14053 t = TREE_OPERAND (t, 0);
14054 break;
14056 case tcc_binary:
14057 case tcc_comparison:
14058 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14059 t = TREE_OPERAND (t, 0);
14060 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14061 t = TREE_OPERAND (t, 1);
14062 else
14063 return t;
14064 break;
14066 case tcc_expression:
14067 switch (TREE_CODE (t))
14069 case COMPOUND_EXPR:
14070 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14071 return t;
14072 t = TREE_OPERAND (t, 0);
14073 break;
14075 case COND_EXPR:
14076 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14077 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14078 return t;
14079 t = TREE_OPERAND (t, 0);
14080 break;
14082 default:
14083 return t;
14085 break;
14087 default:
14088 return t;
14092 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14094 tree
14095 round_up_loc (location_t loc, tree value, unsigned int divisor)
14097 tree div = NULL_TREE;
14099 if (divisor == 1)
14100 return value;
14102 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14103 have to do anything. Only do this when we are not given a const,
14104 because in that case, this check is more expensive than just
14105 doing it. */
14106 if (TREE_CODE (value) != INTEGER_CST)
14108 div = build_int_cst (TREE_TYPE (value), divisor);
14110 if (multiple_of_p (TREE_TYPE (value), value, div))
14111 return value;
14114 /* If divisor is a power of two, simplify this to bit manipulation. */
14115 if (pow2_or_zerop (divisor))
14117 if (TREE_CODE (value) == INTEGER_CST)
14119 wide_int val = wi::to_wide (value);
14120 bool overflow_p;
14122 if ((val & (divisor - 1)) == 0)
14123 return value;
14125 overflow_p = TREE_OVERFLOW (value);
14126 val += divisor - 1;
14127 val &= (int) -divisor;
14128 if (val == 0)
14129 overflow_p = true;
14131 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14133 else
14135 tree t;
14137 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14138 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14139 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14140 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14143 else
14145 if (!div)
14146 div = build_int_cst (TREE_TYPE (value), divisor);
14147 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14148 value = size_binop_loc (loc, MULT_EXPR, value, div);
14151 return value;
14154 /* Likewise, but round down. */
14156 tree
14157 round_down_loc (location_t loc, tree value, int divisor)
14159 tree div = NULL_TREE;
14161 gcc_assert (divisor > 0);
14162 if (divisor == 1)
14163 return value;
14165 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14166 have to do anything. Only do this when we are not given a const,
14167 because in that case, this check is more expensive than just
14168 doing it. */
14169 if (TREE_CODE (value) != INTEGER_CST)
14171 div = build_int_cst (TREE_TYPE (value), divisor);
14173 if (multiple_of_p (TREE_TYPE (value), value, div))
14174 return value;
14177 /* If divisor is a power of two, simplify this to bit manipulation. */
14178 if (pow2_or_zerop (divisor))
14180 tree t;
14182 t = build_int_cst (TREE_TYPE (value), -divisor);
14183 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14185 else
14187 if (!div)
14188 div = build_int_cst (TREE_TYPE (value), divisor);
14189 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14190 value = size_binop_loc (loc, MULT_EXPR, value, div);
14193 return value;
14196 /* Returns the pointer to the base of the object addressed by EXP and
14197 extracts the information about the offset of the access, storing it
14198 to PBITPOS and POFFSET. */
14200 static tree
14201 split_address_to_core_and_offset (tree exp,
14202 HOST_WIDE_INT *pbitpos, tree *poffset)
14204 tree core;
14205 machine_mode mode;
14206 int unsignedp, reversep, volatilep;
14207 HOST_WIDE_INT bitsize;
14208 location_t loc = EXPR_LOCATION (exp);
14210 if (TREE_CODE (exp) == ADDR_EXPR)
14212 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14213 poffset, &mode, &unsignedp, &reversep,
14214 &volatilep);
14215 core = build_fold_addr_expr_loc (loc, core);
14217 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14219 core = TREE_OPERAND (exp, 0);
14220 STRIP_NOPS (core);
14221 *pbitpos = 0;
14222 *poffset = TREE_OPERAND (exp, 1);
14223 if (TREE_CODE (*poffset) == INTEGER_CST)
14225 offset_int tem = wi::sext (wi::to_offset (*poffset),
14226 TYPE_PRECISION (TREE_TYPE (*poffset)));
14227 tem <<= LOG2_BITS_PER_UNIT;
14228 if (wi::fits_shwi_p (tem))
14230 *pbitpos = tem.to_shwi ();
14231 *poffset = NULL_TREE;
14235 else
14237 core = exp;
14238 *pbitpos = 0;
14239 *poffset = NULL_TREE;
14242 return core;
14245 /* Returns true if addresses of E1 and E2 differ by a constant, false
14246 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14248 bool
14249 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14251 tree core1, core2;
14252 HOST_WIDE_INT bitpos1, bitpos2;
14253 tree toffset1, toffset2, tdiff, type;
14255 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14256 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14258 if (bitpos1 % BITS_PER_UNIT != 0
14259 || bitpos2 % BITS_PER_UNIT != 0
14260 || !operand_equal_p (core1, core2, 0))
14261 return false;
14263 if (toffset1 && toffset2)
14265 type = TREE_TYPE (toffset1);
14266 if (type != TREE_TYPE (toffset2))
14267 toffset2 = fold_convert (type, toffset2);
14269 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14270 if (!cst_and_fits_in_hwi (tdiff))
14271 return false;
14273 *diff = int_cst_value (tdiff);
14275 else if (toffset1 || toffset2)
14277 /* If only one of the offsets is non-constant, the difference cannot
14278 be a constant. */
14279 return false;
14281 else
14282 *diff = 0;
14284 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14285 return true;
14288 /* Return OFF converted to a pointer offset type suitable as offset for
14289 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14290 tree
14291 convert_to_ptrofftype_loc (location_t loc, tree off)
14293 return fold_convert_loc (loc, sizetype, off);
14296 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14297 tree
14298 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14300 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14301 ptr, convert_to_ptrofftype_loc (loc, off));
14304 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14305 tree
14306 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14308 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14309 ptr, size_int (off));
14312 /* Return a char pointer for a C string if it is a string constant
14313 or sum of string constant and integer constant. We only support
14314 string constants properly terminated with '\0' character.
14315 If STRLEN is a valid pointer, length (including terminating character)
14316 of returned string is stored to the argument. */
14318 const char *
14319 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14321 tree offset_node;
14323 if (strlen)
14324 *strlen = 0;
14326 src = string_constant (src, &offset_node);
14327 if (src == 0)
14328 return NULL;
14330 unsigned HOST_WIDE_INT offset = 0;
14331 if (offset_node != NULL_TREE)
14333 if (!tree_fits_uhwi_p (offset_node))
14334 return NULL;
14335 else
14336 offset = tree_to_uhwi (offset_node);
14339 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14340 const char *string = TREE_STRING_POINTER (src);
14342 /* Support only properly null-terminated strings. */
14343 if (string_length == 0
14344 || string[string_length - 1] != '\0'
14345 || offset >= string_length)
14346 return NULL;
14348 if (strlen)
14349 *strlen = string_length - offset;
14350 return string + offset;
14353 #if CHECKING_P
14355 namespace selftest {
14357 /* Helper functions for writing tests of folding trees. */
14359 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14361 static void
14362 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14363 tree constant)
14365 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14368 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14369 wrapping WRAPPED_EXPR. */
14371 static void
14372 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14373 tree wrapped_expr)
14375 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14376 ASSERT_NE (wrapped_expr, result);
14377 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14378 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14381 /* Verify that various arithmetic binary operations are folded
14382 correctly. */
14384 static void
14385 test_arithmetic_folding ()
14387 tree type = integer_type_node;
14388 tree x = create_tmp_var_raw (type, "x");
14389 tree zero = build_zero_cst (type);
14390 tree one = build_int_cst (type, 1);
14392 /* Addition. */
14393 /* 1 <-- (0 + 1) */
14394 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14395 one);
14396 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14397 one);
14399 /* (nonlvalue)x <-- (x + 0) */
14400 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14403 /* Subtraction. */
14404 /* 0 <-- (x - x) */
14405 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14406 zero);
14407 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14410 /* Multiplication. */
14411 /* 0 <-- (x * 0) */
14412 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14413 zero);
14415 /* (nonlvalue)x <-- (x * 1) */
14416 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14420 /* Verify that various binary operations on vectors are folded
14421 correctly. */
14423 static void
14424 test_vector_folding ()
14426 tree inner_type = integer_type_node;
14427 tree type = build_vector_type (inner_type, 4);
14428 tree zero = build_zero_cst (type);
14429 tree one = build_one_cst (type);
14431 /* Verify equality tests that return a scalar boolean result. */
14432 tree res_type = boolean_type_node;
14433 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14434 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14435 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14436 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14439 /* Run all of the selftests within this file. */
14441 void
14442 fold_const_c_tests ()
14444 test_arithmetic_folding ();
14445 test_vector_folding ();
14448 } // namespace selftest
14450 #endif /* CHECKING_P */