* sv.po: Update.
[official-gcc.git] / gcc / fold-const.c
blobe34bc81eae8f1458730e9c68d465d9180dbc9285
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "tree-ssa-operands.h"
54 #include "optabs-query.h"
55 #include "cgraph.h"
56 #include "diagnostic-core.h"
57 #include "flags.h"
58 #include "alias.h"
59 #include "fold-const.h"
60 #include "fold-const-call.h"
61 #include "stor-layout.h"
62 #include "calls.h"
63 #include "tree-iterator.h"
64 #include "expr.h"
65 #include "intl.h"
66 #include "langhooks.h"
67 #include "tree-eh.h"
68 #include "gimplify.h"
69 #include "tree-dfa.h"
70 #include "builtins.h"
71 #include "generic-match.h"
72 #include "gimple-fold.h"
73 #include "params.h"
74 #include "tree-into-ssa.h"
75 #include "md5.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
78 #include "tree-ssanames.h"
80 #ifndef LOAD_EXTEND_OP
81 #define LOAD_EXTEND_OP(M) UNKNOWN
82 #endif
84 /* Nonzero if we are folding constants inside an initializer; zero
85 otherwise. */
86 int folding_initializer = 0;
88 /* The following constants represent a bit based encoding of GCC's
89 comparison operators. This encoding simplifies transformations
90 on relational comparison operators, such as AND and OR. */
91 enum comparison_code {
92 COMPCODE_FALSE = 0,
93 COMPCODE_LT = 1,
94 COMPCODE_EQ = 2,
95 COMPCODE_LE = 3,
96 COMPCODE_GT = 4,
97 COMPCODE_LTGT = 5,
98 COMPCODE_GE = 6,
99 COMPCODE_ORD = 7,
100 COMPCODE_UNORD = 8,
101 COMPCODE_UNLT = 9,
102 COMPCODE_UNEQ = 10,
103 COMPCODE_UNLE = 11,
104 COMPCODE_UNGT = 12,
105 COMPCODE_NE = 13,
106 COMPCODE_UNGE = 14,
107 COMPCODE_TRUE = 15
110 static bool negate_expr_p (tree);
111 static tree negate_expr (tree);
112 static tree split_tree (location_t, tree, tree, enum tree_code,
113 tree *, tree *, tree *, int);
114 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
115 static enum comparison_code comparison_to_compcode (enum tree_code);
116 static enum tree_code compcode_to_comparison (enum comparison_code);
117 static int operand_equal_for_comparison_p (tree, tree, tree);
118 static int twoval_comparison_p (tree, tree *, tree *, int *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree make_bit_field_ref (location_t, tree, tree,
121 HOST_WIDE_INT, HOST_WIDE_INT, int, int);
122 static tree optimize_bit_field_compare (location_t, enum tree_code,
123 tree, tree, tree);
124 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
125 HOST_WIDE_INT *,
126 machine_mode *, int *, int *, int *,
127 tree *, tree *);
128 static int simple_operand_p (const_tree);
129 static bool simple_operand_p_2 (tree);
130 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
131 static tree range_predecessor (tree);
132 static tree range_successor (tree);
133 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
134 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
135 static tree unextend (tree, int, int, tree);
136 static tree optimize_minmax_comparison (location_t, enum tree_code,
137 tree, tree, tree);
138 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
139 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
140 static tree fold_binary_op_with_conditional_arg (location_t,
141 enum tree_code, tree,
142 tree, tree,
143 tree, tree, int);
144 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
145 static bool reorder_operands_p (const_tree, const_tree);
146 static tree fold_negate_const (tree, tree);
147 static tree fold_not_const (const_tree, tree);
148 static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 static tree fold_convert_const (enum tree_code, tree, tree);
150 static tree fold_view_convert_expr (tree, tree);
151 static bool vec_cst_ctor_to_array (tree, tree *);
154 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
155 Otherwise, return LOC. */
157 static location_t
158 expr_location_or (tree t, location_t loc)
160 location_t tloc = EXPR_LOCATION (t);
161 return tloc == UNKNOWN_LOCATION ? loc : tloc;
164 /* Similar to protected_set_expr_location, but never modify x in place,
165 if location can and needs to be set, unshare it. */
167 static inline tree
168 protected_set_expr_location_unshare (tree x, location_t loc)
170 if (CAN_HAVE_LOCATION_P (x)
171 && EXPR_LOCATION (x) != loc
172 && !(TREE_CODE (x) == SAVE_EXPR
173 || TREE_CODE (x) == TARGET_EXPR
174 || TREE_CODE (x) == BIND_EXPR))
176 x = copy_node (x);
177 SET_EXPR_LOCATION (x, loc);
179 return x;
182 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
183 division and returns the quotient. Otherwise returns
184 NULL_TREE. */
186 tree
187 div_if_zero_remainder (const_tree arg1, const_tree arg2)
189 widest_int quo;
191 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
192 SIGNED, &quo))
193 return wide_int_to_tree (TREE_TYPE (arg1), quo);
195 return NULL_TREE;
198 /* This is nonzero if we should defer warnings about undefined
199 overflow. This facility exists because these warnings are a
200 special case. The code to estimate loop iterations does not want
201 to issue any warnings, since it works with expressions which do not
202 occur in user code. Various bits of cleanup code call fold(), but
203 only use the result if it has certain characteristics (e.g., is a
204 constant); that code only wants to issue a warning if the result is
205 used. */
207 static int fold_deferring_overflow_warnings;
209 /* If a warning about undefined overflow is deferred, this is the
210 warning. Note that this may cause us to turn two warnings into
211 one, but that is fine since it is sufficient to only give one
212 warning per expression. */
214 static const char* fold_deferred_overflow_warning;
216 /* If a warning about undefined overflow is deferred, this is the
217 level at which the warning should be emitted. */
219 static enum warn_strict_overflow_code fold_deferred_overflow_code;
221 /* Start deferring overflow warnings. We could use a stack here to
222 permit nested calls, but at present it is not necessary. */
224 void
225 fold_defer_overflow_warnings (void)
227 ++fold_deferring_overflow_warnings;
230 /* Stop deferring overflow warnings. If there is a pending warning,
231 and ISSUE is true, then issue the warning if appropriate. STMT is
232 the statement with which the warning should be associated (used for
233 location information); STMT may be NULL. CODE is the level of the
234 warning--a warn_strict_overflow_code value. This function will use
235 the smaller of CODE and the deferred code when deciding whether to
236 issue the warning. CODE may be zero to mean to always use the
237 deferred code. */
239 void
240 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
242 const char *warnmsg;
243 location_t locus;
245 gcc_assert (fold_deferring_overflow_warnings > 0);
246 --fold_deferring_overflow_warnings;
247 if (fold_deferring_overflow_warnings > 0)
249 if (fold_deferred_overflow_warning != NULL
250 && code != 0
251 && code < (int) fold_deferred_overflow_code)
252 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
253 return;
256 warnmsg = fold_deferred_overflow_warning;
257 fold_deferred_overflow_warning = NULL;
259 if (!issue || warnmsg == NULL)
260 return;
262 if (gimple_no_warning_p (stmt))
263 return;
265 /* Use the smallest code level when deciding to issue the
266 warning. */
267 if (code == 0 || code > (int) fold_deferred_overflow_code)
268 code = fold_deferred_overflow_code;
270 if (!issue_strict_overflow_warning (code))
271 return;
273 if (stmt == NULL)
274 locus = input_location;
275 else
276 locus = gimple_location (stmt);
277 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
280 /* Stop deferring overflow warnings, ignoring any deferred
281 warnings. */
283 void
284 fold_undefer_and_ignore_overflow_warnings (void)
286 fold_undefer_overflow_warnings (false, NULL, 0);
289 /* Whether we are deferring overflow warnings. */
291 bool
292 fold_deferring_overflow_warnings_p (void)
294 return fold_deferring_overflow_warnings > 0;
297 /* This is called when we fold something based on the fact that signed
298 overflow is undefined. */
300 static void
301 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
303 if (fold_deferring_overflow_warnings > 0)
305 if (fold_deferred_overflow_warning == NULL
306 || wc < fold_deferred_overflow_code)
308 fold_deferred_overflow_warning = gmsgid;
309 fold_deferred_overflow_code = wc;
312 else if (issue_strict_overflow_warning (wc))
313 warning (OPT_Wstrict_overflow, gmsgid);
316 /* Return true if the built-in mathematical function specified by CODE
317 is odd, i.e. -f(x) == f(-x). */
319 bool
320 negate_mathfn_p (combined_fn fn)
322 switch (fn)
324 CASE_CFN_ASIN:
325 CASE_CFN_ASINH:
326 CASE_CFN_ATAN:
327 CASE_CFN_ATANH:
328 CASE_CFN_CASIN:
329 CASE_CFN_CASINH:
330 CASE_CFN_CATAN:
331 CASE_CFN_CATANH:
332 CASE_CFN_CBRT:
333 CASE_CFN_CPROJ:
334 CASE_CFN_CSIN:
335 CASE_CFN_CSINH:
336 CASE_CFN_CTAN:
337 CASE_CFN_CTANH:
338 CASE_CFN_ERF:
339 CASE_CFN_LLROUND:
340 CASE_CFN_LROUND:
341 CASE_CFN_ROUND:
342 CASE_CFN_SIN:
343 CASE_CFN_SINH:
344 CASE_CFN_TAN:
345 CASE_CFN_TANH:
346 CASE_CFN_TRUNC:
347 return true;
349 CASE_CFN_LLRINT:
350 CASE_CFN_LRINT:
351 CASE_CFN_NEARBYINT:
352 CASE_CFN_RINT:
353 return !flag_rounding_math;
355 default:
356 break;
358 return false;
361 /* Check whether we may negate an integer constant T without causing
362 overflow. */
364 bool
365 may_negate_without_overflow_p (const_tree t)
367 tree type;
369 gcc_assert (TREE_CODE (t) == INTEGER_CST);
371 type = TREE_TYPE (t);
372 if (TYPE_UNSIGNED (type))
373 return false;
375 return !wi::only_sign_bit_p (t);
378 /* Determine whether an expression T can be cheaply negated using
379 the function negate_expr without introducing undefined overflow. */
381 static bool
382 negate_expr_p (tree t)
384 tree type;
386 if (t == 0)
387 return false;
389 type = TREE_TYPE (t);
391 STRIP_SIGN_NOPS (t);
392 switch (TREE_CODE (t))
394 case INTEGER_CST:
395 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
396 return true;
398 /* Check that -CST will not overflow type. */
399 return may_negate_without_overflow_p (t);
400 case BIT_NOT_EXPR:
401 return (INTEGRAL_TYPE_P (type)
402 && TYPE_OVERFLOW_WRAPS (type));
404 case FIXED_CST:
405 return true;
407 case NEGATE_EXPR:
408 return !TYPE_OVERFLOW_SANITIZED (type);
410 case REAL_CST:
411 /* We want to canonicalize to positive real constants. Pretend
412 that only negative ones can be easily negated. */
413 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
415 case COMPLEX_CST:
416 return negate_expr_p (TREE_REALPART (t))
417 && negate_expr_p (TREE_IMAGPART (t));
419 case VECTOR_CST:
421 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
422 return true;
424 int count = TYPE_VECTOR_SUBPARTS (type), i;
426 for (i = 0; i < count; i++)
427 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
428 return false;
430 return true;
433 case COMPLEX_EXPR:
434 return negate_expr_p (TREE_OPERAND (t, 0))
435 && negate_expr_p (TREE_OPERAND (t, 1));
437 case CONJ_EXPR:
438 return negate_expr_p (TREE_OPERAND (t, 0));
440 case PLUS_EXPR:
441 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
442 || HONOR_SIGNED_ZEROS (element_mode (type))
443 || (INTEGRAL_TYPE_P (type)
444 && ! TYPE_OVERFLOW_WRAPS (type)))
445 return false;
446 /* -(A + B) -> (-B) - A. */
447 if (negate_expr_p (TREE_OPERAND (t, 1))
448 && reorder_operands_p (TREE_OPERAND (t, 0),
449 TREE_OPERAND (t, 1)))
450 return true;
451 /* -(A + B) -> (-A) - B. */
452 return negate_expr_p (TREE_OPERAND (t, 0));
454 case MINUS_EXPR:
455 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
456 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
457 && !HONOR_SIGNED_ZEROS (element_mode (type))
458 && (! INTEGRAL_TYPE_P (type)
459 || TYPE_OVERFLOW_WRAPS (type))
460 && reorder_operands_p (TREE_OPERAND (t, 0),
461 TREE_OPERAND (t, 1));
463 case MULT_EXPR:
464 if (TYPE_UNSIGNED (type))
465 break;
466 /* INT_MIN/n * n doesn't overflow while negating one operand it does
467 if n is a power of two. */
468 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
469 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
470 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
471 && ! integer_pow2p (TREE_OPERAND (t, 0)))
472 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
473 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
474 break;
476 /* Fall through. */
478 case RDIV_EXPR:
479 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
480 return negate_expr_p (TREE_OPERAND (t, 1))
481 || negate_expr_p (TREE_OPERAND (t, 0));
482 break;
484 case TRUNC_DIV_EXPR:
485 case ROUND_DIV_EXPR:
486 case EXACT_DIV_EXPR:
487 if (TYPE_UNSIGNED (type))
488 break;
489 if (negate_expr_p (TREE_OPERAND (t, 0)))
490 return true;
491 /* In general we can't negate B in A / B, because if A is INT_MIN and
492 B is 1, we may turn this into INT_MIN / -1 which is undefined
493 and actually traps on some architectures. */
494 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
495 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
496 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
497 && ! integer_onep (TREE_OPERAND (t, 1))))
498 return negate_expr_p (TREE_OPERAND (t, 1));
499 break;
501 case NOP_EXPR:
502 /* Negate -((double)float) as (double)(-float). */
503 if (TREE_CODE (type) == REAL_TYPE)
505 tree tem = strip_float_extensions (t);
506 if (tem != t)
507 return negate_expr_p (tem);
509 break;
511 case CALL_EXPR:
512 /* Negate -f(x) as f(-x). */
513 if (negate_mathfn_p (get_call_combined_fn (t)))
514 return negate_expr_p (CALL_EXPR_ARG (t, 0));
515 break;
517 case RSHIFT_EXPR:
518 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
519 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
521 tree op1 = TREE_OPERAND (t, 1);
522 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
523 return true;
525 break;
527 default:
528 break;
530 return false;
533 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
534 simplification is possible.
535 If negate_expr_p would return true for T, NULL_TREE will never be
536 returned. */
538 static tree
539 fold_negate_expr (location_t loc, tree t)
541 tree type = TREE_TYPE (t);
542 tree tem;
544 switch (TREE_CODE (t))
546 /* Convert - (~A) to A + 1. */
547 case BIT_NOT_EXPR:
548 if (INTEGRAL_TYPE_P (type))
549 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
550 build_one_cst (type));
551 break;
553 case INTEGER_CST:
554 tem = fold_negate_const (t, type);
555 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
556 || (ANY_INTEGRAL_TYPE_P (type)
557 && !TYPE_OVERFLOW_TRAPS (type)
558 && TYPE_OVERFLOW_WRAPS (type))
559 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
560 return tem;
561 break;
563 case REAL_CST:
564 tem = fold_negate_const (t, type);
565 return tem;
567 case FIXED_CST:
568 tem = fold_negate_const (t, type);
569 return tem;
571 case COMPLEX_CST:
573 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
574 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
575 if (rpart && ipart)
576 return build_complex (type, rpart, ipart);
578 break;
580 case VECTOR_CST:
582 int count = TYPE_VECTOR_SUBPARTS (type), i;
583 tree *elts = XALLOCAVEC (tree, count);
585 for (i = 0; i < count; i++)
587 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
588 if (elts[i] == NULL_TREE)
589 return NULL_TREE;
592 return build_vector (type, elts);
595 case COMPLEX_EXPR:
596 if (negate_expr_p (t))
597 return fold_build2_loc (loc, COMPLEX_EXPR, type,
598 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
599 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
600 break;
602 case CONJ_EXPR:
603 if (negate_expr_p (t))
604 return fold_build1_loc (loc, CONJ_EXPR, type,
605 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
606 break;
608 case NEGATE_EXPR:
609 if (!TYPE_OVERFLOW_SANITIZED (type))
610 return TREE_OPERAND (t, 0);
611 break;
613 case PLUS_EXPR:
614 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
615 && !HONOR_SIGNED_ZEROS (element_mode (type)))
617 /* -(A + B) -> (-B) - A. */
618 if (negate_expr_p (TREE_OPERAND (t, 1))
619 && reorder_operands_p (TREE_OPERAND (t, 0),
620 TREE_OPERAND (t, 1)))
622 tem = negate_expr (TREE_OPERAND (t, 1));
623 return fold_build2_loc (loc, MINUS_EXPR, type,
624 tem, TREE_OPERAND (t, 0));
627 /* -(A + B) -> (-A) - B. */
628 if (negate_expr_p (TREE_OPERAND (t, 0)))
630 tem = negate_expr (TREE_OPERAND (t, 0));
631 return fold_build2_loc (loc, MINUS_EXPR, type,
632 tem, TREE_OPERAND (t, 1));
635 break;
637 case MINUS_EXPR:
638 /* - (A - B) -> B - A */
639 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
640 && !HONOR_SIGNED_ZEROS (element_mode (type))
641 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
642 return fold_build2_loc (loc, MINUS_EXPR, type,
643 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
644 break;
646 case MULT_EXPR:
647 if (TYPE_UNSIGNED (type))
648 break;
650 /* Fall through. */
652 case RDIV_EXPR:
653 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
655 tem = TREE_OPERAND (t, 1);
656 if (negate_expr_p (tem))
657 return fold_build2_loc (loc, TREE_CODE (t), type,
658 TREE_OPERAND (t, 0), negate_expr (tem));
659 tem = TREE_OPERAND (t, 0);
660 if (negate_expr_p (tem))
661 return fold_build2_loc (loc, TREE_CODE (t), type,
662 negate_expr (tem), TREE_OPERAND (t, 1));
664 break;
666 case TRUNC_DIV_EXPR:
667 case ROUND_DIV_EXPR:
668 case EXACT_DIV_EXPR:
669 if (TYPE_UNSIGNED (type))
670 break;
671 if (negate_expr_p (TREE_OPERAND (t, 0)))
672 return fold_build2_loc (loc, TREE_CODE (t), type,
673 negate_expr (TREE_OPERAND (t, 0)),
674 TREE_OPERAND (t, 1));
675 /* In general we can't negate B in A / B, because if A is INT_MIN and
676 B is 1, we may turn this into INT_MIN / -1 which is undefined
677 and actually traps on some architectures. */
678 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
679 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
680 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
681 && ! integer_onep (TREE_OPERAND (t, 1))))
682 && negate_expr_p (TREE_OPERAND (t, 1)))
683 return fold_build2_loc (loc, TREE_CODE (t), type,
684 TREE_OPERAND (t, 0),
685 negate_expr (TREE_OPERAND (t, 1)));
686 break;
688 case NOP_EXPR:
689 /* Convert -((double)float) into (double)(-float). */
690 if (TREE_CODE (type) == REAL_TYPE)
692 tem = strip_float_extensions (t);
693 if (tem != t && negate_expr_p (tem))
694 return fold_convert_loc (loc, type, negate_expr (tem));
696 break;
698 case CALL_EXPR:
699 /* Negate -f(x) as f(-x). */
700 if (negate_mathfn_p (get_call_combined_fn (t))
701 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
703 tree fndecl, arg;
705 fndecl = get_callee_fndecl (t);
706 arg = negate_expr (CALL_EXPR_ARG (t, 0));
707 return build_call_expr_loc (loc, fndecl, 1, arg);
709 break;
711 case RSHIFT_EXPR:
712 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
713 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
715 tree op1 = TREE_OPERAND (t, 1);
716 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
718 tree ntype = TYPE_UNSIGNED (type)
719 ? signed_type_for (type)
720 : unsigned_type_for (type);
721 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
722 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
723 return fold_convert_loc (loc, type, temp);
726 break;
728 default:
729 break;
732 return NULL_TREE;
735 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
736 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
737 return NULL_TREE. */
739 static tree
740 negate_expr (tree t)
742 tree type, tem;
743 location_t loc;
745 if (t == NULL_TREE)
746 return NULL_TREE;
748 loc = EXPR_LOCATION (t);
749 type = TREE_TYPE (t);
750 STRIP_SIGN_NOPS (t);
752 tem = fold_negate_expr (loc, t);
753 if (!tem)
754 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
755 return fold_convert_loc (loc, type, tem);
758 /* Split a tree IN into a constant, literal and variable parts that could be
759 combined with CODE to make IN. "constant" means an expression with
760 TREE_CONSTANT but that isn't an actual constant. CODE must be a
761 commutative arithmetic operation. Store the constant part into *CONP,
762 the literal in *LITP and return the variable part. If a part isn't
763 present, set it to null. If the tree does not decompose in this way,
764 return the entire tree as the variable part and the other parts as null.
766 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
767 case, we negate an operand that was subtracted. Except if it is a
768 literal for which we use *MINUS_LITP instead.
770 If NEGATE_P is true, we are negating all of IN, again except a literal
771 for which we use *MINUS_LITP instead. If a variable part is of pointer
772 type, it is negated after converting to TYPE. This prevents us from
773 generating illegal MINUS pointer expression. LOC is the location of
774 the converted variable part.
776 If IN is itself a literal or constant, return it as appropriate.
778 Note that we do not guarantee that any of the three values will be the
779 same type as IN, but they will have the same signedness and mode. */
781 static tree
782 split_tree (location_t loc, tree in, tree type, enum tree_code code,
783 tree *conp, tree *litp, tree *minus_litp, int negate_p)
785 tree var = 0;
787 *conp = 0;
788 *litp = 0;
789 *minus_litp = 0;
791 /* Strip any conversions that don't change the machine mode or signedness. */
792 STRIP_SIGN_NOPS (in);
794 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
795 || TREE_CODE (in) == FIXED_CST)
796 *litp = in;
797 else if (TREE_CODE (in) == code
798 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
799 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
800 /* We can associate addition and subtraction together (even
801 though the C standard doesn't say so) for integers because
802 the value is not affected. For reals, the value might be
803 affected, so we can't. */
804 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
807 tree op0 = TREE_OPERAND (in, 0);
808 tree op1 = TREE_OPERAND (in, 1);
809 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
810 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
812 /* First see if either of the operands is a literal, then a constant. */
813 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
814 || TREE_CODE (op0) == FIXED_CST)
815 *litp = op0, op0 = 0;
816 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
817 || TREE_CODE (op1) == FIXED_CST)
818 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
820 if (op0 != 0 && TREE_CONSTANT (op0))
821 *conp = op0, op0 = 0;
822 else if (op1 != 0 && TREE_CONSTANT (op1))
823 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
825 /* If we haven't dealt with either operand, this is not a case we can
826 decompose. Otherwise, VAR is either of the ones remaining, if any. */
827 if (op0 != 0 && op1 != 0)
828 var = in;
829 else if (op0 != 0)
830 var = op0;
831 else
832 var = op1, neg_var_p = neg1_p;
834 /* Now do any needed negations. */
835 if (neg_litp_p)
836 *minus_litp = *litp, *litp = 0;
837 if (neg_conp_p)
838 *conp = negate_expr (*conp);
839 if (neg_var_p)
841 /* Convert to TYPE before negating a pointer type expr. */
842 if (var && POINTER_TYPE_P (TREE_TYPE (var)))
843 var = fold_convert_loc (loc, type, var);
844 var = negate_expr (var);
847 else if (TREE_CODE (in) == BIT_NOT_EXPR
848 && code == PLUS_EXPR)
850 /* -X - 1 is folded to ~X, undo that here. */
851 *minus_litp = build_one_cst (TREE_TYPE (in));
852 var = negate_expr (TREE_OPERAND (in, 0));
854 else if (TREE_CONSTANT (in))
855 *conp = in;
856 else
857 var = in;
859 if (negate_p)
861 if (*litp)
862 *minus_litp = *litp, *litp = 0;
863 else if (*minus_litp)
864 *litp = *minus_litp, *minus_litp = 0;
865 *conp = negate_expr (*conp);
866 /* Convert to TYPE before negating a pointer type expr. */
867 if (var && POINTER_TYPE_P (TREE_TYPE (var)))
868 var = fold_convert_loc (loc, type, var);
869 var = negate_expr (var);
872 return var;
875 /* Re-associate trees split by the above function. T1 and T2 are
876 either expressions to associate or null. Return the new
877 expression, if any. LOC is the location of the new expression. If
878 we build an operation, do it in TYPE and with CODE. */
880 static tree
881 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
883 if (t1 == 0)
884 return t2;
885 else if (t2 == 0)
886 return t1;
888 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
889 try to fold this since we will have infinite recursion. But do
890 deal with any NEGATE_EXPRs. */
891 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
892 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
894 if (code == PLUS_EXPR)
896 if (TREE_CODE (t1) == NEGATE_EXPR)
897 return build2_loc (loc, MINUS_EXPR, type,
898 fold_convert_loc (loc, type, t2),
899 fold_convert_loc (loc, type,
900 TREE_OPERAND (t1, 0)));
901 else if (TREE_CODE (t2) == NEGATE_EXPR)
902 return build2_loc (loc, MINUS_EXPR, type,
903 fold_convert_loc (loc, type, t1),
904 fold_convert_loc (loc, type,
905 TREE_OPERAND (t2, 0)));
906 else if (integer_zerop (t2))
907 return fold_convert_loc (loc, type, t1);
909 else if (code == MINUS_EXPR)
911 if (integer_zerop (t2))
912 return fold_convert_loc (loc, type, t1);
915 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type, t2));
919 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
920 fold_convert_loc (loc, type, t2));
923 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
924 for use in int_const_binop, size_binop and size_diffop. */
926 static bool
927 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
929 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
930 return false;
931 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
932 return false;
934 switch (code)
936 case LSHIFT_EXPR:
937 case RSHIFT_EXPR:
938 case LROTATE_EXPR:
939 case RROTATE_EXPR:
940 return true;
942 default:
943 break;
946 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
947 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
948 && TYPE_MODE (type1) == TYPE_MODE (type2);
952 /* Combine two integer constants ARG1 and ARG2 under operation CODE
953 to produce a new constant. Return NULL_TREE if we don't know how
954 to evaluate CODE at compile-time. */
956 static tree
957 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
958 int overflowable)
960 wide_int res;
961 tree t;
962 tree type = TREE_TYPE (arg1);
963 signop sign = TYPE_SIGN (type);
964 bool overflow = false;
966 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
967 TYPE_SIGN (TREE_TYPE (parg2)));
969 switch (code)
971 case BIT_IOR_EXPR:
972 res = wi::bit_or (arg1, arg2);
973 break;
975 case BIT_XOR_EXPR:
976 res = wi::bit_xor (arg1, arg2);
977 break;
979 case BIT_AND_EXPR:
980 res = wi::bit_and (arg1, arg2);
981 break;
983 case RSHIFT_EXPR:
984 case LSHIFT_EXPR:
985 if (wi::neg_p (arg2))
987 arg2 = -arg2;
988 if (code == RSHIFT_EXPR)
989 code = LSHIFT_EXPR;
990 else
991 code = RSHIFT_EXPR;
994 if (code == RSHIFT_EXPR)
995 /* It's unclear from the C standard whether shifts can overflow.
996 The following code ignores overflow; perhaps a C standard
997 interpretation ruling is needed. */
998 res = wi::rshift (arg1, arg2, sign);
999 else
1000 res = wi::lshift (arg1, arg2);
1001 break;
1003 case RROTATE_EXPR:
1004 case LROTATE_EXPR:
1005 if (wi::neg_p (arg2))
1007 arg2 = -arg2;
1008 if (code == RROTATE_EXPR)
1009 code = LROTATE_EXPR;
1010 else
1011 code = RROTATE_EXPR;
1014 if (code == RROTATE_EXPR)
1015 res = wi::rrotate (arg1, arg2);
1016 else
1017 res = wi::lrotate (arg1, arg2);
1018 break;
1020 case PLUS_EXPR:
1021 res = wi::add (arg1, arg2, sign, &overflow);
1022 break;
1024 case MINUS_EXPR:
1025 res = wi::sub (arg1, arg2, sign, &overflow);
1026 break;
1028 case MULT_EXPR:
1029 res = wi::mul (arg1, arg2, sign, &overflow);
1030 break;
1032 case MULT_HIGHPART_EXPR:
1033 res = wi::mul_high (arg1, arg2, sign);
1034 break;
1036 case TRUNC_DIV_EXPR:
1037 case EXACT_DIV_EXPR:
1038 if (arg2 == 0)
1039 return NULL_TREE;
1040 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1041 break;
1043 case FLOOR_DIV_EXPR:
1044 if (arg2 == 0)
1045 return NULL_TREE;
1046 res = wi::div_floor (arg1, arg2, sign, &overflow);
1047 break;
1049 case CEIL_DIV_EXPR:
1050 if (arg2 == 0)
1051 return NULL_TREE;
1052 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1053 break;
1055 case ROUND_DIV_EXPR:
1056 if (arg2 == 0)
1057 return NULL_TREE;
1058 res = wi::div_round (arg1, arg2, sign, &overflow);
1059 break;
1061 case TRUNC_MOD_EXPR:
1062 if (arg2 == 0)
1063 return NULL_TREE;
1064 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1065 break;
1067 case FLOOR_MOD_EXPR:
1068 if (arg2 == 0)
1069 return NULL_TREE;
1070 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1071 break;
1073 case CEIL_MOD_EXPR:
1074 if (arg2 == 0)
1075 return NULL_TREE;
1076 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1077 break;
1079 case ROUND_MOD_EXPR:
1080 if (arg2 == 0)
1081 return NULL_TREE;
1082 res = wi::mod_round (arg1, arg2, sign, &overflow);
1083 break;
1085 case MIN_EXPR:
1086 res = wi::min (arg1, arg2, sign);
1087 break;
1089 case MAX_EXPR:
1090 res = wi::max (arg1, arg2, sign);
1091 break;
1093 default:
1094 return NULL_TREE;
1097 t = force_fit_type (type, res, overflowable,
1098 (((sign == SIGNED || overflowable == -1)
1099 && overflow)
1100 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1102 return t;
1105 tree
1106 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1108 return int_const_binop_1 (code, arg1, arg2, 1);
1111 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1112 constant. We assume ARG1 and ARG2 have the same data type, or at least
1113 are the same kind of constant and the same machine mode. Return zero if
1114 combining the constants is not allowed in the current operating mode. */
1116 static tree
1117 const_binop (enum tree_code code, tree arg1, tree arg2)
1119 /* Sanity check for the recursive cases. */
1120 if (!arg1 || !arg2)
1121 return NULL_TREE;
1123 STRIP_NOPS (arg1);
1124 STRIP_NOPS (arg2);
1126 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1128 if (code == POINTER_PLUS_EXPR)
1129 return int_const_binop (PLUS_EXPR,
1130 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1132 return int_const_binop (code, arg1, arg2);
1135 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1137 machine_mode mode;
1138 REAL_VALUE_TYPE d1;
1139 REAL_VALUE_TYPE d2;
1140 REAL_VALUE_TYPE value;
1141 REAL_VALUE_TYPE result;
1142 bool inexact;
1143 tree t, type;
1145 /* The following codes are handled by real_arithmetic. */
1146 switch (code)
1148 case PLUS_EXPR:
1149 case MINUS_EXPR:
1150 case MULT_EXPR:
1151 case RDIV_EXPR:
1152 case MIN_EXPR:
1153 case MAX_EXPR:
1154 break;
1156 default:
1157 return NULL_TREE;
1160 d1 = TREE_REAL_CST (arg1);
1161 d2 = TREE_REAL_CST (arg2);
1163 type = TREE_TYPE (arg1);
1164 mode = TYPE_MODE (type);
1166 /* Don't perform operation if we honor signaling NaNs and
1167 either operand is a signaling NaN. */
1168 if (HONOR_SNANS (mode)
1169 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1170 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1171 return NULL_TREE;
1173 /* Don't perform operation if it would raise a division
1174 by zero exception. */
1175 if (code == RDIV_EXPR
1176 && real_equal (&d2, &dconst0)
1177 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1178 return NULL_TREE;
1180 /* If either operand is a NaN, just return it. Otherwise, set up
1181 for floating-point trap; we return an overflow. */
1182 if (REAL_VALUE_ISNAN (d1))
1184 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1185 is off. */
1186 d1.signalling = 0;
1187 t = build_real (type, d1);
1188 return t;
1190 else if (REAL_VALUE_ISNAN (d2))
1192 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1193 is off. */
1194 d2.signalling = 0;
1195 t = build_real (type, d2);
1196 return t;
1199 inexact = real_arithmetic (&value, code, &d1, &d2);
1200 real_convert (&result, mode, &value);
1202 /* Don't constant fold this floating point operation if
1203 the result has overflowed and flag_trapping_math. */
1204 if (flag_trapping_math
1205 && MODE_HAS_INFINITIES (mode)
1206 && REAL_VALUE_ISINF (result)
1207 && !REAL_VALUE_ISINF (d1)
1208 && !REAL_VALUE_ISINF (d2))
1209 return NULL_TREE;
1211 /* Don't constant fold this floating point operation if the
1212 result may dependent upon the run-time rounding mode and
1213 flag_rounding_math is set, or if GCC's software emulation
1214 is unable to accurately represent the result. */
1215 if ((flag_rounding_math
1216 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1217 && (inexact || !real_identical (&result, &value)))
1218 return NULL_TREE;
1220 t = build_real (type, result);
1222 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1223 return t;
1226 if (TREE_CODE (arg1) == FIXED_CST)
1228 FIXED_VALUE_TYPE f1;
1229 FIXED_VALUE_TYPE f2;
1230 FIXED_VALUE_TYPE result;
1231 tree t, type;
1232 int sat_p;
1233 bool overflow_p;
1235 /* The following codes are handled by fixed_arithmetic. */
1236 switch (code)
1238 case PLUS_EXPR:
1239 case MINUS_EXPR:
1240 case MULT_EXPR:
1241 case TRUNC_DIV_EXPR:
1242 if (TREE_CODE (arg2) != FIXED_CST)
1243 return NULL_TREE;
1244 f2 = TREE_FIXED_CST (arg2);
1245 break;
1247 case LSHIFT_EXPR:
1248 case RSHIFT_EXPR:
1250 if (TREE_CODE (arg2) != INTEGER_CST)
1251 return NULL_TREE;
1252 wide_int w2 = arg2;
1253 f2.data.high = w2.elt (1);
1254 f2.data.low = w2.elt (0);
1255 f2.mode = SImode;
1257 break;
1259 default:
1260 return NULL_TREE;
1263 f1 = TREE_FIXED_CST (arg1);
1264 type = TREE_TYPE (arg1);
1265 sat_p = TYPE_SATURATING (type);
1266 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1267 t = build_fixed (type, result);
1268 /* Propagate overflow flags. */
1269 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1270 TREE_OVERFLOW (t) = 1;
1271 return t;
1274 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1276 tree type = TREE_TYPE (arg1);
1277 tree r1 = TREE_REALPART (arg1);
1278 tree i1 = TREE_IMAGPART (arg1);
1279 tree r2 = TREE_REALPART (arg2);
1280 tree i2 = TREE_IMAGPART (arg2);
1281 tree real, imag;
1283 switch (code)
1285 case PLUS_EXPR:
1286 case MINUS_EXPR:
1287 real = const_binop (code, r1, r2);
1288 imag = const_binop (code, i1, i2);
1289 break;
1291 case MULT_EXPR:
1292 if (COMPLEX_FLOAT_TYPE_P (type))
1293 return do_mpc_arg2 (arg1, arg2, type,
1294 /* do_nonfinite= */ folding_initializer,
1295 mpc_mul);
1297 real = const_binop (MINUS_EXPR,
1298 const_binop (MULT_EXPR, r1, r2),
1299 const_binop (MULT_EXPR, i1, i2));
1300 imag = const_binop (PLUS_EXPR,
1301 const_binop (MULT_EXPR, r1, i2),
1302 const_binop (MULT_EXPR, i1, r2));
1303 break;
1305 case RDIV_EXPR:
1306 if (COMPLEX_FLOAT_TYPE_P (type))
1307 return do_mpc_arg2 (arg1, arg2, type,
1308 /* do_nonfinite= */ folding_initializer,
1309 mpc_div);
1310 /* Fallthru ... */
1311 case TRUNC_DIV_EXPR:
1312 case CEIL_DIV_EXPR:
1313 case FLOOR_DIV_EXPR:
1314 case ROUND_DIV_EXPR:
1315 if (flag_complex_method == 0)
1317 /* Keep this algorithm in sync with
1318 tree-complex.c:expand_complex_div_straight().
1320 Expand complex division to scalars, straightforward algorithm.
1321 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1322 t = br*br + bi*bi
1324 tree magsquared
1325 = const_binop (PLUS_EXPR,
1326 const_binop (MULT_EXPR, r2, r2),
1327 const_binop (MULT_EXPR, i2, i2));
1328 tree t1
1329 = const_binop (PLUS_EXPR,
1330 const_binop (MULT_EXPR, r1, r2),
1331 const_binop (MULT_EXPR, i1, i2));
1332 tree t2
1333 = const_binop (MINUS_EXPR,
1334 const_binop (MULT_EXPR, i1, r2),
1335 const_binop (MULT_EXPR, r1, i2));
1337 real = const_binop (code, t1, magsquared);
1338 imag = const_binop (code, t2, magsquared);
1340 else
1342 /* Keep this algorithm in sync with
1343 tree-complex.c:expand_complex_div_wide().
1345 Expand complex division to scalars, modified algorithm to minimize
1346 overflow with wide input ranges. */
1347 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1348 fold_abs_const (r2, TREE_TYPE (type)),
1349 fold_abs_const (i2, TREE_TYPE (type)));
1351 if (integer_nonzerop (compare))
1353 /* In the TRUE branch, we compute
1354 ratio = br/bi;
1355 div = (br * ratio) + bi;
1356 tr = (ar * ratio) + ai;
1357 ti = (ai * ratio) - ar;
1358 tr = tr / div;
1359 ti = ti / div; */
1360 tree ratio = const_binop (code, r2, i2);
1361 tree div = const_binop (PLUS_EXPR, i2,
1362 const_binop (MULT_EXPR, r2, ratio));
1363 real = const_binop (MULT_EXPR, r1, ratio);
1364 real = const_binop (PLUS_EXPR, real, i1);
1365 real = const_binop (code, real, div);
1367 imag = const_binop (MULT_EXPR, i1, ratio);
1368 imag = const_binop (MINUS_EXPR, imag, r1);
1369 imag = const_binop (code, imag, div);
1371 else
1373 /* In the FALSE branch, we compute
1374 ratio = d/c;
1375 divisor = (d * ratio) + c;
1376 tr = (b * ratio) + a;
1377 ti = b - (a * ratio);
1378 tr = tr / div;
1379 ti = ti / div; */
1380 tree ratio = const_binop (code, i2, r2);
1381 tree div = const_binop (PLUS_EXPR, r2,
1382 const_binop (MULT_EXPR, i2, ratio));
1384 real = const_binop (MULT_EXPR, i1, ratio);
1385 real = const_binop (PLUS_EXPR, real, r1);
1386 real = const_binop (code, real, div);
1388 imag = const_binop (MULT_EXPR, r1, ratio);
1389 imag = const_binop (MINUS_EXPR, i1, imag);
1390 imag = const_binop (code, imag, div);
1393 break;
1395 default:
1396 return NULL_TREE;
1399 if (real && imag)
1400 return build_complex (type, real, imag);
1403 if (TREE_CODE (arg1) == VECTOR_CST
1404 && TREE_CODE (arg2) == VECTOR_CST)
1406 tree type = TREE_TYPE (arg1);
1407 int count = TYPE_VECTOR_SUBPARTS (type), i;
1408 tree *elts = XALLOCAVEC (tree, count);
1410 for (i = 0; i < count; i++)
1412 tree elem1 = VECTOR_CST_ELT (arg1, i);
1413 tree elem2 = VECTOR_CST_ELT (arg2, i);
1415 elts[i] = const_binop (code, elem1, elem2);
1417 /* It is possible that const_binop cannot handle the given
1418 code and return NULL_TREE */
1419 if (elts[i] == NULL_TREE)
1420 return NULL_TREE;
1423 return build_vector (type, elts);
1426 /* Shifts allow a scalar offset for a vector. */
1427 if (TREE_CODE (arg1) == VECTOR_CST
1428 && TREE_CODE (arg2) == INTEGER_CST)
1430 tree type = TREE_TYPE (arg1);
1431 int count = TYPE_VECTOR_SUBPARTS (type), i;
1432 tree *elts = XALLOCAVEC (tree, count);
1434 for (i = 0; i < count; i++)
1436 tree elem1 = VECTOR_CST_ELT (arg1, i);
1438 elts[i] = const_binop (code, elem1, arg2);
1440 /* It is possible that const_binop cannot handle the given
1441 code and return NULL_TREE. */
1442 if (elts[i] == NULL_TREE)
1443 return NULL_TREE;
1446 return build_vector (type, elts);
1448 return NULL_TREE;
1451 /* Overload that adds a TYPE parameter to be able to dispatch
1452 to fold_relational_const. */
1454 tree
1455 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1457 if (TREE_CODE_CLASS (code) == tcc_comparison)
1458 return fold_relational_const (code, type, arg1, arg2);
1460 /* ??? Until we make the const_binop worker take the type of the
1461 result as argument put those cases that need it here. */
1462 switch (code)
1464 case COMPLEX_EXPR:
1465 if ((TREE_CODE (arg1) == REAL_CST
1466 && TREE_CODE (arg2) == REAL_CST)
1467 || (TREE_CODE (arg1) == INTEGER_CST
1468 && TREE_CODE (arg2) == INTEGER_CST))
1469 return build_complex (type, arg1, arg2);
1470 return NULL_TREE;
1472 case VEC_PACK_TRUNC_EXPR:
1473 case VEC_PACK_FIX_TRUNC_EXPR:
1475 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1476 tree *elts;
1478 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1479 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1480 if (TREE_CODE (arg1) != VECTOR_CST
1481 || TREE_CODE (arg2) != VECTOR_CST)
1482 return NULL_TREE;
1484 elts = XALLOCAVEC (tree, nelts);
1485 if (!vec_cst_ctor_to_array (arg1, elts)
1486 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1487 return NULL_TREE;
1489 for (i = 0; i < nelts; i++)
1491 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1492 ? NOP_EXPR : FIX_TRUNC_EXPR,
1493 TREE_TYPE (type), elts[i]);
1494 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1495 return NULL_TREE;
1498 return build_vector (type, elts);
1501 case VEC_WIDEN_MULT_LO_EXPR:
1502 case VEC_WIDEN_MULT_HI_EXPR:
1503 case VEC_WIDEN_MULT_EVEN_EXPR:
1504 case VEC_WIDEN_MULT_ODD_EXPR:
1506 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1507 unsigned int out, ofs, scale;
1508 tree *elts;
1510 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1511 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1512 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1513 return NULL_TREE;
1515 elts = XALLOCAVEC (tree, nelts * 4);
1516 if (!vec_cst_ctor_to_array (arg1, elts)
1517 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1518 return NULL_TREE;
1520 if (code == VEC_WIDEN_MULT_LO_EXPR)
1521 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1522 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1523 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1524 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1525 scale = 1, ofs = 0;
1526 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1527 scale = 1, ofs = 1;
1529 for (out = 0; out < nelts; out++)
1531 unsigned int in1 = (out << scale) + ofs;
1532 unsigned int in2 = in1 + nelts * 2;
1533 tree t1, t2;
1535 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1536 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1538 if (t1 == NULL_TREE || t2 == NULL_TREE)
1539 return NULL_TREE;
1540 elts[out] = const_binop (MULT_EXPR, t1, t2);
1541 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1542 return NULL_TREE;
1545 return build_vector (type, elts);
1548 default:;
1551 if (TREE_CODE_CLASS (code) != tcc_binary)
1552 return NULL_TREE;
1554 /* Make sure type and arg0 have the same saturating flag. */
1555 gcc_checking_assert (TYPE_SATURATING (type)
1556 == TYPE_SATURATING (TREE_TYPE (arg1)));
1558 return const_binop (code, arg1, arg2);
1561 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1562 Return zero if computing the constants is not possible. */
1564 tree
1565 const_unop (enum tree_code code, tree type, tree arg0)
1567 /* Don't perform the operation, other than NEGATE and ABS, if
1568 flag_signaling_nans is on and the operand is a signaling NaN. */
1569 if (TREE_CODE (arg0) == REAL_CST
1570 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1571 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1572 && code != NEGATE_EXPR
1573 && code != ABS_EXPR)
1574 return NULL_TREE;
1576 switch (code)
1578 CASE_CONVERT:
1579 case FLOAT_EXPR:
1580 case FIX_TRUNC_EXPR:
1581 case FIXED_CONVERT_EXPR:
1582 return fold_convert_const (code, type, arg0);
1584 case ADDR_SPACE_CONVERT_EXPR:
1585 /* If the source address is 0, and the source address space
1586 cannot have a valid object at 0, fold to dest type null. */
1587 if (integer_zerop (arg0)
1588 && !(targetm.addr_space.zero_address_valid
1589 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1590 return fold_convert_const (code, type, arg0);
1591 break;
1593 case VIEW_CONVERT_EXPR:
1594 return fold_view_convert_expr (type, arg0);
1596 case NEGATE_EXPR:
1598 /* Can't call fold_negate_const directly here as that doesn't
1599 handle all cases and we might not be able to negate some
1600 constants. */
1601 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1602 if (tem && CONSTANT_CLASS_P (tem))
1603 return tem;
1604 break;
1607 case ABS_EXPR:
1608 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1609 return fold_abs_const (arg0, type);
1610 break;
1612 case CONJ_EXPR:
1613 if (TREE_CODE (arg0) == COMPLEX_CST)
1615 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1616 TREE_TYPE (type));
1617 return build_complex (type, TREE_REALPART (arg0), ipart);
1619 break;
1621 case BIT_NOT_EXPR:
1622 if (TREE_CODE (arg0) == INTEGER_CST)
1623 return fold_not_const (arg0, type);
1624 /* Perform BIT_NOT_EXPR on each element individually. */
1625 else if (TREE_CODE (arg0) == VECTOR_CST)
1627 tree *elements;
1628 tree elem;
1629 unsigned count = VECTOR_CST_NELTS (arg0), i;
1631 elements = XALLOCAVEC (tree, count);
1632 for (i = 0; i < count; i++)
1634 elem = VECTOR_CST_ELT (arg0, i);
1635 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1636 if (elem == NULL_TREE)
1637 break;
1638 elements[i] = elem;
1640 if (i == count)
1641 return build_vector (type, elements);
1643 break;
1645 case TRUTH_NOT_EXPR:
1646 if (TREE_CODE (arg0) == INTEGER_CST)
1647 return constant_boolean_node (integer_zerop (arg0), type);
1648 break;
1650 case REALPART_EXPR:
1651 if (TREE_CODE (arg0) == COMPLEX_CST)
1652 return fold_convert (type, TREE_REALPART (arg0));
1653 break;
1655 case IMAGPART_EXPR:
1656 if (TREE_CODE (arg0) == COMPLEX_CST)
1657 return fold_convert (type, TREE_IMAGPART (arg0));
1658 break;
1660 case VEC_UNPACK_LO_EXPR:
1661 case VEC_UNPACK_HI_EXPR:
1662 case VEC_UNPACK_FLOAT_LO_EXPR:
1663 case VEC_UNPACK_FLOAT_HI_EXPR:
1665 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1666 tree *elts;
1667 enum tree_code subcode;
1669 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1670 if (TREE_CODE (arg0) != VECTOR_CST)
1671 return NULL_TREE;
1673 elts = XALLOCAVEC (tree, nelts * 2);
1674 if (!vec_cst_ctor_to_array (arg0, elts))
1675 return NULL_TREE;
1677 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1678 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1679 elts += nelts;
1681 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1682 subcode = NOP_EXPR;
1683 else
1684 subcode = FLOAT_EXPR;
1686 for (i = 0; i < nelts; i++)
1688 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1689 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1690 return NULL_TREE;
1693 return build_vector (type, elts);
1696 case REDUC_MIN_EXPR:
1697 case REDUC_MAX_EXPR:
1698 case REDUC_PLUS_EXPR:
1700 unsigned int nelts, i;
1701 tree *elts;
1702 enum tree_code subcode;
1704 if (TREE_CODE (arg0) != VECTOR_CST)
1705 return NULL_TREE;
1706 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1708 elts = XALLOCAVEC (tree, nelts);
1709 if (!vec_cst_ctor_to_array (arg0, elts))
1710 return NULL_TREE;
1712 switch (code)
1714 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1715 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1716 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1717 default: gcc_unreachable ();
1720 for (i = 1; i < nelts; i++)
1722 elts[0] = const_binop (subcode, elts[0], elts[i]);
1723 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1724 return NULL_TREE;
1727 return elts[0];
1730 default:
1731 break;
1734 return NULL_TREE;
1737 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1738 indicates which particular sizetype to create. */
1740 tree
1741 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1743 return build_int_cst (sizetype_tab[(int) kind], number);
1746 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1747 is a tree code. The type of the result is taken from the operands.
1748 Both must be equivalent integer types, ala int_binop_types_match_p.
1749 If the operands are constant, so is the result. */
1751 tree
1752 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1754 tree type = TREE_TYPE (arg0);
1756 if (arg0 == error_mark_node || arg1 == error_mark_node)
1757 return error_mark_node;
1759 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1760 TREE_TYPE (arg1)));
1762 /* Handle the special case of two integer constants faster. */
1763 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1765 /* And some specific cases even faster than that. */
1766 if (code == PLUS_EXPR)
1768 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1769 return arg1;
1770 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1771 return arg0;
1773 else if (code == MINUS_EXPR)
1775 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1776 return arg0;
1778 else if (code == MULT_EXPR)
1780 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1781 return arg1;
1784 /* Handle general case of two integer constants. For sizetype
1785 constant calculations we always want to know about overflow,
1786 even in the unsigned case. */
1787 return int_const_binop_1 (code, arg0, arg1, -1);
1790 return fold_build2_loc (loc, code, type, arg0, arg1);
1793 /* Given two values, either both of sizetype or both of bitsizetype,
1794 compute the difference between the two values. Return the value
1795 in signed type corresponding to the type of the operands. */
1797 tree
1798 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1800 tree type = TREE_TYPE (arg0);
1801 tree ctype;
1803 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1804 TREE_TYPE (arg1)));
1806 /* If the type is already signed, just do the simple thing. */
1807 if (!TYPE_UNSIGNED (type))
1808 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1810 if (type == sizetype)
1811 ctype = ssizetype;
1812 else if (type == bitsizetype)
1813 ctype = sbitsizetype;
1814 else
1815 ctype = signed_type_for (type);
1817 /* If either operand is not a constant, do the conversions to the signed
1818 type and subtract. The hardware will do the right thing with any
1819 overflow in the subtraction. */
1820 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1821 return size_binop_loc (loc, MINUS_EXPR,
1822 fold_convert_loc (loc, ctype, arg0),
1823 fold_convert_loc (loc, ctype, arg1));
1825 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1826 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1827 overflow) and negate (which can't either). Special-case a result
1828 of zero while we're here. */
1829 if (tree_int_cst_equal (arg0, arg1))
1830 return build_int_cst (ctype, 0);
1831 else if (tree_int_cst_lt (arg1, arg0))
1832 return fold_convert_loc (loc, ctype,
1833 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1834 else
1835 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1836 fold_convert_loc (loc, ctype,
1837 size_binop_loc (loc,
1838 MINUS_EXPR,
1839 arg1, arg0)));
1842 /* A subroutine of fold_convert_const handling conversions of an
1843 INTEGER_CST to another integer type. */
1845 static tree
1846 fold_convert_const_int_from_int (tree type, const_tree arg1)
1848 /* Given an integer constant, make new constant with new type,
1849 appropriately sign-extended or truncated. Use widest_int
1850 so that any extension is done according ARG1's type. */
1851 return force_fit_type (type, wi::to_widest (arg1),
1852 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1853 TREE_OVERFLOW (arg1));
1856 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1857 to an integer type. */
1859 static tree
1860 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1862 bool overflow = false;
1863 tree t;
1865 /* The following code implements the floating point to integer
1866 conversion rules required by the Java Language Specification,
1867 that IEEE NaNs are mapped to zero and values that overflow
1868 the target precision saturate, i.e. values greater than
1869 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1870 are mapped to INT_MIN. These semantics are allowed by the
1871 C and C++ standards that simply state that the behavior of
1872 FP-to-integer conversion is unspecified upon overflow. */
1874 wide_int val;
1875 REAL_VALUE_TYPE r;
1876 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1878 switch (code)
1880 case FIX_TRUNC_EXPR:
1881 real_trunc (&r, VOIDmode, &x);
1882 break;
1884 default:
1885 gcc_unreachable ();
1888 /* If R is NaN, return zero and show we have an overflow. */
1889 if (REAL_VALUE_ISNAN (r))
1891 overflow = true;
1892 val = wi::zero (TYPE_PRECISION (type));
1895 /* See if R is less than the lower bound or greater than the
1896 upper bound. */
1898 if (! overflow)
1900 tree lt = TYPE_MIN_VALUE (type);
1901 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1902 if (real_less (&r, &l))
1904 overflow = true;
1905 val = lt;
1909 if (! overflow)
1911 tree ut = TYPE_MAX_VALUE (type);
1912 if (ut)
1914 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1915 if (real_less (&u, &r))
1917 overflow = true;
1918 val = ut;
1923 if (! overflow)
1924 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1926 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1927 return t;
1930 /* A subroutine of fold_convert_const handling conversions of a
1931 FIXED_CST to an integer type. */
1933 static tree
1934 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1936 tree t;
1937 double_int temp, temp_trunc;
1938 unsigned int mode;
1940 /* Right shift FIXED_CST to temp by fbit. */
1941 temp = TREE_FIXED_CST (arg1).data;
1942 mode = TREE_FIXED_CST (arg1).mode;
1943 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1945 temp = temp.rshift (GET_MODE_FBIT (mode),
1946 HOST_BITS_PER_DOUBLE_INT,
1947 SIGNED_FIXED_POINT_MODE_P (mode));
1949 /* Left shift temp to temp_trunc by fbit. */
1950 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1951 HOST_BITS_PER_DOUBLE_INT,
1952 SIGNED_FIXED_POINT_MODE_P (mode));
1954 else
1956 temp = double_int_zero;
1957 temp_trunc = double_int_zero;
1960 /* If FIXED_CST is negative, we need to round the value toward 0.
1961 By checking if the fractional bits are not zero to add 1 to temp. */
1962 if (SIGNED_FIXED_POINT_MODE_P (mode)
1963 && temp_trunc.is_negative ()
1964 && TREE_FIXED_CST (arg1).data != temp_trunc)
1965 temp += double_int_one;
1967 /* Given a fixed-point constant, make new constant with new type,
1968 appropriately sign-extended or truncated. */
1969 t = force_fit_type (type, temp, -1,
1970 (temp.is_negative ()
1971 && (TYPE_UNSIGNED (type)
1972 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1973 | TREE_OVERFLOW (arg1));
1975 return t;
1978 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1979 to another floating point type. */
1981 static tree
1982 fold_convert_const_real_from_real (tree type, const_tree arg1)
1984 REAL_VALUE_TYPE value;
1985 tree t;
1987 /* Don't perform the operation if flag_signaling_nans is on
1988 and the operand is a signaling NaN. */
1989 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1990 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1991 return NULL_TREE;
1993 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1994 t = build_real (type, value);
1996 /* If converting an infinity or NAN to a representation that doesn't
1997 have one, set the overflow bit so that we can produce some kind of
1998 error message at the appropriate point if necessary. It's not the
1999 most user-friendly message, but it's better than nothing. */
2000 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2001 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2002 TREE_OVERFLOW (t) = 1;
2003 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2004 && !MODE_HAS_NANS (TYPE_MODE (type)))
2005 TREE_OVERFLOW (t) = 1;
2006 /* Regular overflow, conversion produced an infinity in a mode that
2007 can't represent them. */
2008 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2009 && REAL_VALUE_ISINF (value)
2010 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2011 TREE_OVERFLOW (t) = 1;
2012 else
2013 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2014 return t;
2017 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2018 to a floating point type. */
2020 static tree
2021 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2023 REAL_VALUE_TYPE value;
2024 tree t;
2026 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2027 t = build_real (type, value);
2029 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2030 return t;
2033 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2034 to another fixed-point type. */
2036 static tree
2037 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2039 FIXED_VALUE_TYPE value;
2040 tree t;
2041 bool overflow_p;
2043 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2044 TYPE_SATURATING (type));
2045 t = build_fixed (type, value);
2047 /* Propagate overflow flags. */
2048 if (overflow_p | TREE_OVERFLOW (arg1))
2049 TREE_OVERFLOW (t) = 1;
2050 return t;
2053 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2054 to a fixed-point type. */
2056 static tree
2057 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2059 FIXED_VALUE_TYPE value;
2060 tree t;
2061 bool overflow_p;
2062 double_int di;
2064 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2066 di.low = TREE_INT_CST_ELT (arg1, 0);
2067 if (TREE_INT_CST_NUNITS (arg1) == 1)
2068 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2069 else
2070 di.high = TREE_INT_CST_ELT (arg1, 1);
2072 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2073 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2074 TYPE_SATURATING (type));
2075 t = build_fixed (type, value);
2077 /* Propagate overflow flags. */
2078 if (overflow_p | TREE_OVERFLOW (arg1))
2079 TREE_OVERFLOW (t) = 1;
2080 return t;
2083 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2084 to a fixed-point type. */
2086 static tree
2087 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2089 FIXED_VALUE_TYPE value;
2090 tree t;
2091 bool overflow_p;
2093 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2094 &TREE_REAL_CST (arg1),
2095 TYPE_SATURATING (type));
2096 t = build_fixed (type, value);
2098 /* Propagate overflow flags. */
2099 if (overflow_p | TREE_OVERFLOW (arg1))
2100 TREE_OVERFLOW (t) = 1;
2101 return t;
2104 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2105 type TYPE. If no simplification can be done return NULL_TREE. */
2107 static tree
2108 fold_convert_const (enum tree_code code, tree type, tree arg1)
2110 if (TREE_TYPE (arg1) == type)
2111 return arg1;
2113 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2114 || TREE_CODE (type) == OFFSET_TYPE)
2116 if (TREE_CODE (arg1) == INTEGER_CST)
2117 return fold_convert_const_int_from_int (type, arg1);
2118 else if (TREE_CODE (arg1) == REAL_CST)
2119 return fold_convert_const_int_from_real (code, type, arg1);
2120 else if (TREE_CODE (arg1) == FIXED_CST)
2121 return fold_convert_const_int_from_fixed (type, arg1);
2123 else if (TREE_CODE (type) == REAL_TYPE)
2125 if (TREE_CODE (arg1) == INTEGER_CST)
2126 return build_real_from_int_cst (type, arg1);
2127 else if (TREE_CODE (arg1) == REAL_CST)
2128 return fold_convert_const_real_from_real (type, arg1);
2129 else if (TREE_CODE (arg1) == FIXED_CST)
2130 return fold_convert_const_real_from_fixed (type, arg1);
2132 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2134 if (TREE_CODE (arg1) == FIXED_CST)
2135 return fold_convert_const_fixed_from_fixed (type, arg1);
2136 else if (TREE_CODE (arg1) == INTEGER_CST)
2137 return fold_convert_const_fixed_from_int (type, arg1);
2138 else if (TREE_CODE (arg1) == REAL_CST)
2139 return fold_convert_const_fixed_from_real (type, arg1);
2141 else if (TREE_CODE (type) == VECTOR_TYPE)
2143 if (TREE_CODE (arg1) == VECTOR_CST
2144 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2146 int len = TYPE_VECTOR_SUBPARTS (type);
2147 tree elttype = TREE_TYPE (type);
2148 tree *v = XALLOCAVEC (tree, len);
2149 for (int i = 0; i < len; ++i)
2151 tree elt = VECTOR_CST_ELT (arg1, i);
2152 tree cvt = fold_convert_const (code, elttype, elt);
2153 if (cvt == NULL_TREE)
2154 return NULL_TREE;
2155 v[i] = cvt;
2157 return build_vector (type, v);
2160 return NULL_TREE;
2163 /* Construct a vector of zero elements of vector type TYPE. */
2165 static tree
2166 build_zero_vector (tree type)
2168 tree t;
2170 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2171 return build_vector_from_val (type, t);
2174 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2176 bool
2177 fold_convertible_p (const_tree type, const_tree arg)
2179 tree orig = TREE_TYPE (arg);
2181 if (type == orig)
2182 return true;
2184 if (TREE_CODE (arg) == ERROR_MARK
2185 || TREE_CODE (type) == ERROR_MARK
2186 || TREE_CODE (orig) == ERROR_MARK)
2187 return false;
2189 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2190 return true;
2192 switch (TREE_CODE (type))
2194 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2195 case POINTER_TYPE: case REFERENCE_TYPE:
2196 case OFFSET_TYPE:
2197 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2198 || TREE_CODE (orig) == OFFSET_TYPE);
2200 case REAL_TYPE:
2201 case FIXED_POINT_TYPE:
2202 case COMPLEX_TYPE:
2203 case VECTOR_TYPE:
2204 case VOID_TYPE:
2205 return TREE_CODE (type) == TREE_CODE (orig);
2207 default:
2208 return false;
2212 /* Convert expression ARG to type TYPE. Used by the middle-end for
2213 simple conversions in preference to calling the front-end's convert. */
2215 tree
2216 fold_convert_loc (location_t loc, tree type, tree arg)
2218 tree orig = TREE_TYPE (arg);
2219 tree tem;
2221 if (type == orig)
2222 return arg;
2224 if (TREE_CODE (arg) == ERROR_MARK
2225 || TREE_CODE (type) == ERROR_MARK
2226 || TREE_CODE (orig) == ERROR_MARK)
2227 return error_mark_node;
2229 switch (TREE_CODE (type))
2231 case POINTER_TYPE:
2232 case REFERENCE_TYPE:
2233 /* Handle conversions between pointers to different address spaces. */
2234 if (POINTER_TYPE_P (orig)
2235 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2236 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2237 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2238 /* fall through */
2240 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2241 case OFFSET_TYPE:
2242 if (TREE_CODE (arg) == INTEGER_CST)
2244 tem = fold_convert_const (NOP_EXPR, type, arg);
2245 if (tem != NULL_TREE)
2246 return tem;
2248 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2249 || TREE_CODE (orig) == OFFSET_TYPE)
2250 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2251 if (TREE_CODE (orig) == COMPLEX_TYPE)
2252 return fold_convert_loc (loc, type,
2253 fold_build1_loc (loc, REALPART_EXPR,
2254 TREE_TYPE (orig), arg));
2255 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2256 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2257 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2259 case REAL_TYPE:
2260 if (TREE_CODE (arg) == INTEGER_CST)
2262 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2263 if (tem != NULL_TREE)
2264 return tem;
2266 else if (TREE_CODE (arg) == REAL_CST)
2268 tem = fold_convert_const (NOP_EXPR, type, arg);
2269 if (tem != NULL_TREE)
2270 return tem;
2272 else if (TREE_CODE (arg) == FIXED_CST)
2274 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2275 if (tem != NULL_TREE)
2276 return tem;
2279 switch (TREE_CODE (orig))
2281 case INTEGER_TYPE:
2282 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2283 case POINTER_TYPE: case REFERENCE_TYPE:
2284 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2286 case REAL_TYPE:
2287 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2289 case FIXED_POINT_TYPE:
2290 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2292 case COMPLEX_TYPE:
2293 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2294 return fold_convert_loc (loc, type, tem);
2296 default:
2297 gcc_unreachable ();
2300 case FIXED_POINT_TYPE:
2301 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2302 || TREE_CODE (arg) == REAL_CST)
2304 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2305 if (tem != NULL_TREE)
2306 goto fold_convert_exit;
2309 switch (TREE_CODE (orig))
2311 case FIXED_POINT_TYPE:
2312 case INTEGER_TYPE:
2313 case ENUMERAL_TYPE:
2314 case BOOLEAN_TYPE:
2315 case REAL_TYPE:
2316 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2318 case COMPLEX_TYPE:
2319 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2320 return fold_convert_loc (loc, type, tem);
2322 default:
2323 gcc_unreachable ();
2326 case COMPLEX_TYPE:
2327 switch (TREE_CODE (orig))
2329 case INTEGER_TYPE:
2330 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2331 case POINTER_TYPE: case REFERENCE_TYPE:
2332 case REAL_TYPE:
2333 case FIXED_POINT_TYPE:
2334 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2335 fold_convert_loc (loc, TREE_TYPE (type), arg),
2336 fold_convert_loc (loc, TREE_TYPE (type),
2337 integer_zero_node));
2338 case COMPLEX_TYPE:
2340 tree rpart, ipart;
2342 if (TREE_CODE (arg) == COMPLEX_EXPR)
2344 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2345 TREE_OPERAND (arg, 0));
2346 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2347 TREE_OPERAND (arg, 1));
2348 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2351 arg = save_expr (arg);
2352 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2353 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2354 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2355 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2356 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2359 default:
2360 gcc_unreachable ();
2363 case VECTOR_TYPE:
2364 if (integer_zerop (arg))
2365 return build_zero_vector (type);
2366 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2367 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2368 || TREE_CODE (orig) == VECTOR_TYPE);
2369 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2371 case VOID_TYPE:
2372 tem = fold_ignored_result (arg);
2373 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2375 default:
2376 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2377 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2378 gcc_unreachable ();
2380 fold_convert_exit:
2381 protected_set_expr_location_unshare (tem, loc);
2382 return tem;
2385 /* Return false if expr can be assumed not to be an lvalue, true
2386 otherwise. */
2388 static bool
2389 maybe_lvalue_p (const_tree x)
2391 /* We only need to wrap lvalue tree codes. */
2392 switch (TREE_CODE (x))
2394 case VAR_DECL:
2395 case PARM_DECL:
2396 case RESULT_DECL:
2397 case LABEL_DECL:
2398 case FUNCTION_DECL:
2399 case SSA_NAME:
2401 case COMPONENT_REF:
2402 case MEM_REF:
2403 case INDIRECT_REF:
2404 case ARRAY_REF:
2405 case ARRAY_RANGE_REF:
2406 case BIT_FIELD_REF:
2407 case OBJ_TYPE_REF:
2409 case REALPART_EXPR:
2410 case IMAGPART_EXPR:
2411 case PREINCREMENT_EXPR:
2412 case PREDECREMENT_EXPR:
2413 case SAVE_EXPR:
2414 case TRY_CATCH_EXPR:
2415 case WITH_CLEANUP_EXPR:
2416 case COMPOUND_EXPR:
2417 case MODIFY_EXPR:
2418 case TARGET_EXPR:
2419 case COND_EXPR:
2420 case BIND_EXPR:
2421 break;
2423 default:
2424 /* Assume the worst for front-end tree codes. */
2425 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2426 break;
2427 return false;
2430 return true;
2433 /* Return an expr equal to X but certainly not valid as an lvalue. */
2435 tree
2436 non_lvalue_loc (location_t loc, tree x)
2438 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2439 us. */
2440 if (in_gimple_form)
2441 return x;
2443 if (! maybe_lvalue_p (x))
2444 return x;
2445 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2448 /* When pedantic, return an expr equal to X but certainly not valid as a
2449 pedantic lvalue. Otherwise, return X. */
2451 static tree
2452 pedantic_non_lvalue_loc (location_t loc, tree x)
2454 return protected_set_expr_location_unshare (x, loc);
2457 /* Given a tree comparison code, return the code that is the logical inverse.
2458 It is generally not safe to do this for floating-point comparisons, except
2459 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2460 ERROR_MARK in this case. */
2462 enum tree_code
2463 invert_tree_comparison (enum tree_code code, bool honor_nans)
2465 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2466 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2467 return ERROR_MARK;
2469 switch (code)
2471 case EQ_EXPR:
2472 return NE_EXPR;
2473 case NE_EXPR:
2474 return EQ_EXPR;
2475 case GT_EXPR:
2476 return honor_nans ? UNLE_EXPR : LE_EXPR;
2477 case GE_EXPR:
2478 return honor_nans ? UNLT_EXPR : LT_EXPR;
2479 case LT_EXPR:
2480 return honor_nans ? UNGE_EXPR : GE_EXPR;
2481 case LE_EXPR:
2482 return honor_nans ? UNGT_EXPR : GT_EXPR;
2483 case LTGT_EXPR:
2484 return UNEQ_EXPR;
2485 case UNEQ_EXPR:
2486 return LTGT_EXPR;
2487 case UNGT_EXPR:
2488 return LE_EXPR;
2489 case UNGE_EXPR:
2490 return LT_EXPR;
2491 case UNLT_EXPR:
2492 return GE_EXPR;
2493 case UNLE_EXPR:
2494 return GT_EXPR;
2495 case ORDERED_EXPR:
2496 return UNORDERED_EXPR;
2497 case UNORDERED_EXPR:
2498 return ORDERED_EXPR;
2499 default:
2500 gcc_unreachable ();
2504 /* Similar, but return the comparison that results if the operands are
2505 swapped. This is safe for floating-point. */
2507 enum tree_code
2508 swap_tree_comparison (enum tree_code code)
2510 switch (code)
2512 case EQ_EXPR:
2513 case NE_EXPR:
2514 case ORDERED_EXPR:
2515 case UNORDERED_EXPR:
2516 case LTGT_EXPR:
2517 case UNEQ_EXPR:
2518 return code;
2519 case GT_EXPR:
2520 return LT_EXPR;
2521 case GE_EXPR:
2522 return LE_EXPR;
2523 case LT_EXPR:
2524 return GT_EXPR;
2525 case LE_EXPR:
2526 return GE_EXPR;
2527 case UNGT_EXPR:
2528 return UNLT_EXPR;
2529 case UNGE_EXPR:
2530 return UNLE_EXPR;
2531 case UNLT_EXPR:
2532 return UNGT_EXPR;
2533 case UNLE_EXPR:
2534 return UNGE_EXPR;
2535 default:
2536 gcc_unreachable ();
2541 /* Convert a comparison tree code from an enum tree_code representation
2542 into a compcode bit-based encoding. This function is the inverse of
2543 compcode_to_comparison. */
2545 static enum comparison_code
2546 comparison_to_compcode (enum tree_code code)
2548 switch (code)
2550 case LT_EXPR:
2551 return COMPCODE_LT;
2552 case EQ_EXPR:
2553 return COMPCODE_EQ;
2554 case LE_EXPR:
2555 return COMPCODE_LE;
2556 case GT_EXPR:
2557 return COMPCODE_GT;
2558 case NE_EXPR:
2559 return COMPCODE_NE;
2560 case GE_EXPR:
2561 return COMPCODE_GE;
2562 case ORDERED_EXPR:
2563 return COMPCODE_ORD;
2564 case UNORDERED_EXPR:
2565 return COMPCODE_UNORD;
2566 case UNLT_EXPR:
2567 return COMPCODE_UNLT;
2568 case UNEQ_EXPR:
2569 return COMPCODE_UNEQ;
2570 case UNLE_EXPR:
2571 return COMPCODE_UNLE;
2572 case UNGT_EXPR:
2573 return COMPCODE_UNGT;
2574 case LTGT_EXPR:
2575 return COMPCODE_LTGT;
2576 case UNGE_EXPR:
2577 return COMPCODE_UNGE;
2578 default:
2579 gcc_unreachable ();
2583 /* Convert a compcode bit-based encoding of a comparison operator back
2584 to GCC's enum tree_code representation. This function is the
2585 inverse of comparison_to_compcode. */
2587 static enum tree_code
2588 compcode_to_comparison (enum comparison_code code)
2590 switch (code)
2592 case COMPCODE_LT:
2593 return LT_EXPR;
2594 case COMPCODE_EQ:
2595 return EQ_EXPR;
2596 case COMPCODE_LE:
2597 return LE_EXPR;
2598 case COMPCODE_GT:
2599 return GT_EXPR;
2600 case COMPCODE_NE:
2601 return NE_EXPR;
2602 case COMPCODE_GE:
2603 return GE_EXPR;
2604 case COMPCODE_ORD:
2605 return ORDERED_EXPR;
2606 case COMPCODE_UNORD:
2607 return UNORDERED_EXPR;
2608 case COMPCODE_UNLT:
2609 return UNLT_EXPR;
2610 case COMPCODE_UNEQ:
2611 return UNEQ_EXPR;
2612 case COMPCODE_UNLE:
2613 return UNLE_EXPR;
2614 case COMPCODE_UNGT:
2615 return UNGT_EXPR;
2616 case COMPCODE_LTGT:
2617 return LTGT_EXPR;
2618 case COMPCODE_UNGE:
2619 return UNGE_EXPR;
2620 default:
2621 gcc_unreachable ();
2625 /* Return a tree for the comparison which is the combination of
2626 doing the AND or OR (depending on CODE) of the two operations LCODE
2627 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2628 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2629 if this makes the transformation invalid. */
2631 tree
2632 combine_comparisons (location_t loc,
2633 enum tree_code code, enum tree_code lcode,
2634 enum tree_code rcode, tree truth_type,
2635 tree ll_arg, tree lr_arg)
2637 bool honor_nans = HONOR_NANS (ll_arg);
2638 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2639 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2640 int compcode;
2642 switch (code)
2644 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2645 compcode = lcompcode & rcompcode;
2646 break;
2648 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2649 compcode = lcompcode | rcompcode;
2650 break;
2652 default:
2653 return NULL_TREE;
2656 if (!honor_nans)
2658 /* Eliminate unordered comparisons, as well as LTGT and ORD
2659 which are not used unless the mode has NaNs. */
2660 compcode &= ~COMPCODE_UNORD;
2661 if (compcode == COMPCODE_LTGT)
2662 compcode = COMPCODE_NE;
2663 else if (compcode == COMPCODE_ORD)
2664 compcode = COMPCODE_TRUE;
2666 else if (flag_trapping_math)
2668 /* Check that the original operation and the optimized ones will trap
2669 under the same condition. */
2670 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2671 && (lcompcode != COMPCODE_EQ)
2672 && (lcompcode != COMPCODE_ORD);
2673 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2674 && (rcompcode != COMPCODE_EQ)
2675 && (rcompcode != COMPCODE_ORD);
2676 bool trap = (compcode & COMPCODE_UNORD) == 0
2677 && (compcode != COMPCODE_EQ)
2678 && (compcode != COMPCODE_ORD);
2680 /* In a short-circuited boolean expression the LHS might be
2681 such that the RHS, if evaluated, will never trap. For
2682 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2683 if neither x nor y is NaN. (This is a mixed blessing: for
2684 example, the expression above will never trap, hence
2685 optimizing it to x < y would be invalid). */
2686 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2687 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2688 rtrap = false;
2690 /* If the comparison was short-circuited, and only the RHS
2691 trapped, we may now generate a spurious trap. */
2692 if (rtrap && !ltrap
2693 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2694 return NULL_TREE;
2696 /* If we changed the conditions that cause a trap, we lose. */
2697 if ((ltrap || rtrap) != trap)
2698 return NULL_TREE;
2701 if (compcode == COMPCODE_TRUE)
2702 return constant_boolean_node (true, truth_type);
2703 else if (compcode == COMPCODE_FALSE)
2704 return constant_boolean_node (false, truth_type);
2705 else
2707 enum tree_code tcode;
2709 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2710 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2714 /* Return nonzero if two operands (typically of the same tree node)
2715 are necessarily equal. FLAGS modifies behavior as follows:
2717 If OEP_ONLY_CONST is set, only return nonzero for constants.
2718 This function tests whether the operands are indistinguishable;
2719 it does not test whether they are equal using C's == operation.
2720 The distinction is important for IEEE floating point, because
2721 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2722 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2724 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2725 even though it may hold multiple values during a function.
2726 This is because a GCC tree node guarantees that nothing else is
2727 executed between the evaluation of its "operands" (which may often
2728 be evaluated in arbitrary order). Hence if the operands themselves
2729 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2730 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2731 unset means assuming isochronic (or instantaneous) tree equivalence.
2732 Unless comparing arbitrary expression trees, such as from different
2733 statements, this flag can usually be left unset.
2735 If OEP_PURE_SAME is set, then pure functions with identical arguments
2736 are considered the same. It is used when the caller has other ways
2737 to ensure that global memory is unchanged in between.
2739 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2740 not values of expressions.
2742 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2743 any operand with side effect. This is unnecesarily conservative in the
2744 case we know that arg0 and arg1 are in disjoint code paths (such as in
2745 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2746 addresses with TREE_CONSTANT flag set so we know that &var == &var
2747 even if var is volatile. */
2750 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2752 /* If either is ERROR_MARK, they aren't equal. */
2753 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2754 || TREE_TYPE (arg0) == error_mark_node
2755 || TREE_TYPE (arg1) == error_mark_node)
2756 return 0;
2758 /* Similar, if either does not have a type (like a released SSA name),
2759 they aren't equal. */
2760 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2761 return 0;
2763 /* We cannot consider pointers to different address space equal. */
2764 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2765 && POINTER_TYPE_P (TREE_TYPE (arg1))
2766 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2767 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2768 return 0;
2770 /* Check equality of integer constants before bailing out due to
2771 precision differences. */
2772 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2774 /* Address of INTEGER_CST is not defined; check that we did not forget
2775 to drop the OEP_ADDRESS_OF flags. */
2776 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2777 return tree_int_cst_equal (arg0, arg1);
2780 if (!(flags & OEP_ADDRESS_OF))
2782 /* If both types don't have the same signedness, then we can't consider
2783 them equal. We must check this before the STRIP_NOPS calls
2784 because they may change the signedness of the arguments. As pointers
2785 strictly don't have a signedness, require either two pointers or
2786 two non-pointers as well. */
2787 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2788 || POINTER_TYPE_P (TREE_TYPE (arg0))
2789 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2790 return 0;
2792 /* If both types don't have the same precision, then it is not safe
2793 to strip NOPs. */
2794 if (element_precision (TREE_TYPE (arg0))
2795 != element_precision (TREE_TYPE (arg1)))
2796 return 0;
2798 STRIP_NOPS (arg0);
2799 STRIP_NOPS (arg1);
2801 #if 0
2802 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2803 sanity check once the issue is solved. */
2804 else
2805 /* Addresses of conversions and SSA_NAMEs (and many other things)
2806 are not defined. Check that we did not forget to drop the
2807 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2808 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2809 && TREE_CODE (arg0) != SSA_NAME);
2810 #endif
2812 /* In case both args are comparisons but with different comparison
2813 code, try to swap the comparison operands of one arg to produce
2814 a match and compare that variant. */
2815 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2816 && COMPARISON_CLASS_P (arg0)
2817 && COMPARISON_CLASS_P (arg1))
2819 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2821 if (TREE_CODE (arg0) == swap_code)
2822 return operand_equal_p (TREE_OPERAND (arg0, 0),
2823 TREE_OPERAND (arg1, 1), flags)
2824 && operand_equal_p (TREE_OPERAND (arg0, 1),
2825 TREE_OPERAND (arg1, 0), flags);
2828 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2830 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2831 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2833 else if (flags & OEP_ADDRESS_OF)
2835 /* If we are interested in comparing addresses ignore
2836 MEM_REF wrappings of the base that can appear just for
2837 TBAA reasons. */
2838 if (TREE_CODE (arg0) == MEM_REF
2839 && DECL_P (arg1)
2840 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2841 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2842 && integer_zerop (TREE_OPERAND (arg0, 1)))
2843 return 1;
2844 else if (TREE_CODE (arg1) == MEM_REF
2845 && DECL_P (arg0)
2846 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2847 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2848 && integer_zerop (TREE_OPERAND (arg1, 1)))
2849 return 1;
2850 return 0;
2852 else
2853 return 0;
2856 /* When not checking adddresses, this is needed for conversions and for
2857 COMPONENT_REF. Might as well play it safe and always test this. */
2858 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2859 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2860 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2861 && !(flags & OEP_ADDRESS_OF)))
2862 return 0;
2864 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2865 We don't care about side effects in that case because the SAVE_EXPR
2866 takes care of that for us. In all other cases, two expressions are
2867 equal if they have no side effects. If we have two identical
2868 expressions with side effects that should be treated the same due
2869 to the only side effects being identical SAVE_EXPR's, that will
2870 be detected in the recursive calls below.
2871 If we are taking an invariant address of two identical objects
2872 they are necessarily equal as well. */
2873 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2874 && (TREE_CODE (arg0) == SAVE_EXPR
2875 || (flags & OEP_MATCH_SIDE_EFFECTS)
2876 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2877 return 1;
2879 /* Next handle constant cases, those for which we can return 1 even
2880 if ONLY_CONST is set. */
2881 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2882 switch (TREE_CODE (arg0))
2884 case INTEGER_CST:
2885 return tree_int_cst_equal (arg0, arg1);
2887 case FIXED_CST:
2888 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2889 TREE_FIXED_CST (arg1));
2891 case REAL_CST:
2892 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2893 return 1;
2896 if (!HONOR_SIGNED_ZEROS (arg0))
2898 /* If we do not distinguish between signed and unsigned zero,
2899 consider them equal. */
2900 if (real_zerop (arg0) && real_zerop (arg1))
2901 return 1;
2903 return 0;
2905 case VECTOR_CST:
2907 unsigned i;
2909 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2910 return 0;
2912 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2914 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2915 VECTOR_CST_ELT (arg1, i), flags))
2916 return 0;
2918 return 1;
2921 case COMPLEX_CST:
2922 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2923 flags)
2924 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2925 flags));
2927 case STRING_CST:
2928 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2929 && ! memcmp (TREE_STRING_POINTER (arg0),
2930 TREE_STRING_POINTER (arg1),
2931 TREE_STRING_LENGTH (arg0)));
2933 case ADDR_EXPR:
2934 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2935 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2936 flags | OEP_ADDRESS_OF
2937 | OEP_MATCH_SIDE_EFFECTS);
2938 case CONSTRUCTOR:
2939 /* In GIMPLE empty constructors are allowed in initializers of
2940 aggregates. */
2941 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0))
2942 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1)));
2943 default:
2944 break;
2947 if (flags & OEP_ONLY_CONST)
2948 return 0;
2950 /* Define macros to test an operand from arg0 and arg1 for equality and a
2951 variant that allows null and views null as being different from any
2952 non-null value. In the latter case, if either is null, the both
2953 must be; otherwise, do the normal comparison. */
2954 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2955 TREE_OPERAND (arg1, N), flags)
2957 #define OP_SAME_WITH_NULL(N) \
2958 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2959 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2961 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2963 case tcc_unary:
2964 /* Two conversions are equal only if signedness and modes match. */
2965 switch (TREE_CODE (arg0))
2967 CASE_CONVERT:
2968 case FIX_TRUNC_EXPR:
2969 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2970 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2971 return 0;
2972 break;
2973 default:
2974 break;
2977 return OP_SAME (0);
2980 case tcc_comparison:
2981 case tcc_binary:
2982 if (OP_SAME (0) && OP_SAME (1))
2983 return 1;
2985 /* For commutative ops, allow the other order. */
2986 return (commutative_tree_code (TREE_CODE (arg0))
2987 && operand_equal_p (TREE_OPERAND (arg0, 0),
2988 TREE_OPERAND (arg1, 1), flags)
2989 && operand_equal_p (TREE_OPERAND (arg0, 1),
2990 TREE_OPERAND (arg1, 0), flags));
2992 case tcc_reference:
2993 /* If either of the pointer (or reference) expressions we are
2994 dereferencing contain a side effect, these cannot be equal,
2995 but their addresses can be. */
2996 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
2997 && (TREE_SIDE_EFFECTS (arg0)
2998 || TREE_SIDE_EFFECTS (arg1)))
2999 return 0;
3001 switch (TREE_CODE (arg0))
3003 case INDIRECT_REF:
3004 if (!(flags & OEP_ADDRESS_OF)
3005 && (TYPE_ALIGN (TREE_TYPE (arg0))
3006 != TYPE_ALIGN (TREE_TYPE (arg1))))
3007 return 0;
3008 flags &= ~OEP_ADDRESS_OF;
3009 return OP_SAME (0);
3011 case REALPART_EXPR:
3012 case IMAGPART_EXPR:
3013 case VIEW_CONVERT_EXPR:
3014 return OP_SAME (0);
3016 case TARGET_MEM_REF:
3017 case MEM_REF:
3018 if (!(flags & OEP_ADDRESS_OF))
3020 /* Require equal access sizes */
3021 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3022 && (!TYPE_SIZE (TREE_TYPE (arg0))
3023 || !TYPE_SIZE (TREE_TYPE (arg1))
3024 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3025 TYPE_SIZE (TREE_TYPE (arg1)),
3026 flags)))
3027 return 0;
3028 /* Verify that accesses are TBAA compatible. */
3029 if (!alias_ptr_types_compatible_p
3030 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3031 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3032 || (MR_DEPENDENCE_CLIQUE (arg0)
3033 != MR_DEPENDENCE_CLIQUE (arg1))
3034 || (MR_DEPENDENCE_BASE (arg0)
3035 != MR_DEPENDENCE_BASE (arg1)))
3036 return 0;
3037 /* Verify that alignment is compatible. */
3038 if (TYPE_ALIGN (TREE_TYPE (arg0))
3039 != TYPE_ALIGN (TREE_TYPE (arg1)))
3040 return 0;
3042 flags &= ~OEP_ADDRESS_OF;
3043 return (OP_SAME (0) && OP_SAME (1)
3044 /* TARGET_MEM_REF require equal extra operands. */
3045 && (TREE_CODE (arg0) != TARGET_MEM_REF
3046 || (OP_SAME_WITH_NULL (2)
3047 && OP_SAME_WITH_NULL (3)
3048 && OP_SAME_WITH_NULL (4))));
3050 case ARRAY_REF:
3051 case ARRAY_RANGE_REF:
3052 /* Operands 2 and 3 may be null.
3053 Compare the array index by value if it is constant first as we
3054 may have different types but same value here. */
3055 if (!OP_SAME (0))
3056 return 0;
3057 flags &= ~OEP_ADDRESS_OF;
3058 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3059 TREE_OPERAND (arg1, 1))
3060 || OP_SAME (1))
3061 && OP_SAME_WITH_NULL (2)
3062 && OP_SAME_WITH_NULL (3));
3064 case COMPONENT_REF:
3065 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3066 may be NULL when we're called to compare MEM_EXPRs. */
3067 if (!OP_SAME_WITH_NULL (0)
3068 || !OP_SAME (1))
3069 return 0;
3070 flags &= ~OEP_ADDRESS_OF;
3071 return OP_SAME_WITH_NULL (2);
3073 case BIT_FIELD_REF:
3074 if (!OP_SAME (0))
3075 return 0;
3076 flags &= ~OEP_ADDRESS_OF;
3077 return OP_SAME (1) && OP_SAME (2);
3079 default:
3080 return 0;
3083 case tcc_expression:
3084 switch (TREE_CODE (arg0))
3086 case ADDR_EXPR:
3087 /* Be sure we pass right ADDRESS_OF flag. */
3088 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3089 return operand_equal_p (TREE_OPERAND (arg0, 0),
3090 TREE_OPERAND (arg1, 0),
3091 flags | OEP_ADDRESS_OF);
3093 case TRUTH_NOT_EXPR:
3094 return OP_SAME (0);
3096 case TRUTH_ANDIF_EXPR:
3097 case TRUTH_ORIF_EXPR:
3098 return OP_SAME (0) && OP_SAME (1);
3100 case FMA_EXPR:
3101 case WIDEN_MULT_PLUS_EXPR:
3102 case WIDEN_MULT_MINUS_EXPR:
3103 if (!OP_SAME (2))
3104 return 0;
3105 /* The multiplcation operands are commutative. */
3106 /* FALLTHRU */
3108 case TRUTH_AND_EXPR:
3109 case TRUTH_OR_EXPR:
3110 case TRUTH_XOR_EXPR:
3111 if (OP_SAME (0) && OP_SAME (1))
3112 return 1;
3114 /* Otherwise take into account this is a commutative operation. */
3115 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3116 TREE_OPERAND (arg1, 1), flags)
3117 && operand_equal_p (TREE_OPERAND (arg0, 1),
3118 TREE_OPERAND (arg1, 0), flags));
3120 case COND_EXPR:
3121 case VEC_COND_EXPR:
3122 case DOT_PROD_EXPR:
3123 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3125 default:
3126 return 0;
3129 case tcc_vl_exp:
3130 switch (TREE_CODE (arg0))
3132 case CALL_EXPR:
3133 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3134 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3135 /* If not both CALL_EXPRs are either internal or normal function
3136 functions, then they are not equal. */
3137 return 0;
3138 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3140 /* If the CALL_EXPRs call different internal functions, then they
3141 are not equal. */
3142 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3143 return 0;
3145 else
3147 /* If the CALL_EXPRs call different functions, then they are not
3148 equal. */
3149 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3150 flags))
3151 return 0;
3154 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3156 unsigned int cef = call_expr_flags (arg0);
3157 if (flags & OEP_PURE_SAME)
3158 cef &= ECF_CONST | ECF_PURE;
3159 else
3160 cef &= ECF_CONST;
3161 if (!cef)
3162 return 0;
3165 /* Now see if all the arguments are the same. */
3167 const_call_expr_arg_iterator iter0, iter1;
3168 const_tree a0, a1;
3169 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3170 a1 = first_const_call_expr_arg (arg1, &iter1);
3171 a0 && a1;
3172 a0 = next_const_call_expr_arg (&iter0),
3173 a1 = next_const_call_expr_arg (&iter1))
3174 if (! operand_equal_p (a0, a1, flags))
3175 return 0;
3177 /* If we get here and both argument lists are exhausted
3178 then the CALL_EXPRs are equal. */
3179 return ! (a0 || a1);
3181 default:
3182 return 0;
3185 case tcc_declaration:
3186 /* Consider __builtin_sqrt equal to sqrt. */
3187 return (TREE_CODE (arg0) == FUNCTION_DECL
3188 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3189 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3190 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3192 case tcc_exceptional:
3193 if (TREE_CODE (arg0) == CONSTRUCTOR)
3195 /* In GIMPLE constructors are used only to build vectors from
3196 elements. Individual elements in the constructor must be
3197 indexed in increasing order and form an initial sequence.
3199 We make no effort to compare constructors in generic.
3200 (see sem_variable::equals in ipa-icf which can do so for
3201 constants). */
3202 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3203 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3204 return 0;
3206 /* Be sure that vectors constructed have the same representation.
3207 We only tested element precision and modes to match.
3208 Vectors may be BLKmode and thus also check that the number of
3209 parts match. */
3210 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3211 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3212 return 0;
3214 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3215 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3216 unsigned int len = vec_safe_length (v0);
3218 if (len != vec_safe_length (v1))
3219 return 0;
3221 for (unsigned int i = 0; i < len; i++)
3223 constructor_elt *c0 = &(*v0)[i];
3224 constructor_elt *c1 = &(*v1)[i];
3226 if (!operand_equal_p (c0->value, c1->value, flags)
3227 /* In GIMPLE the indexes can be either NULL or matching i.
3228 Double check this so we won't get false
3229 positives for GENERIC. */
3230 || (c0->index
3231 && (TREE_CODE (c0->index) != INTEGER_CST
3232 || !compare_tree_int (c0->index, i)))
3233 || (c1->index
3234 && (TREE_CODE (c1->index) != INTEGER_CST
3235 || !compare_tree_int (c1->index, i))))
3236 return 0;
3238 return 1;
3240 return 0;
3242 default:
3243 return 0;
3246 #undef OP_SAME
3247 #undef OP_SAME_WITH_NULL
3250 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3251 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3253 When in doubt, return 0. */
3255 static int
3256 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3258 int unsignedp1, unsignedpo;
3259 tree primarg0, primarg1, primother;
3260 unsigned int correct_width;
3262 if (operand_equal_p (arg0, arg1, 0))
3263 return 1;
3265 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3266 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3267 return 0;
3269 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3270 and see if the inner values are the same. This removes any
3271 signedness comparison, which doesn't matter here. */
3272 primarg0 = arg0, primarg1 = arg1;
3273 STRIP_NOPS (primarg0);
3274 STRIP_NOPS (primarg1);
3275 if (operand_equal_p (primarg0, primarg1, 0))
3276 return 1;
3278 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3279 actual comparison operand, ARG0.
3281 First throw away any conversions to wider types
3282 already present in the operands. */
3284 primarg1 = get_narrower (arg1, &unsignedp1);
3285 primother = get_narrower (other, &unsignedpo);
3287 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3288 if (unsignedp1 == unsignedpo
3289 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3290 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3292 tree type = TREE_TYPE (arg0);
3294 /* Make sure shorter operand is extended the right way
3295 to match the longer operand. */
3296 primarg1 = fold_convert (signed_or_unsigned_type_for
3297 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3299 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3300 return 1;
3303 return 0;
3306 /* See if ARG is an expression that is either a comparison or is performing
3307 arithmetic on comparisons. The comparisons must only be comparing
3308 two different values, which will be stored in *CVAL1 and *CVAL2; if
3309 they are nonzero it means that some operands have already been found.
3310 No variables may be used anywhere else in the expression except in the
3311 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3312 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3314 If this is true, return 1. Otherwise, return zero. */
3316 static int
3317 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3319 enum tree_code code = TREE_CODE (arg);
3320 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3322 /* We can handle some of the tcc_expression cases here. */
3323 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3324 tclass = tcc_unary;
3325 else if (tclass == tcc_expression
3326 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3327 || code == COMPOUND_EXPR))
3328 tclass = tcc_binary;
3330 else if (tclass == tcc_expression && code == SAVE_EXPR
3331 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3333 /* If we've already found a CVAL1 or CVAL2, this expression is
3334 two complex to handle. */
3335 if (*cval1 || *cval2)
3336 return 0;
3338 tclass = tcc_unary;
3339 *save_p = 1;
3342 switch (tclass)
3344 case tcc_unary:
3345 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3347 case tcc_binary:
3348 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3349 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3350 cval1, cval2, save_p));
3352 case tcc_constant:
3353 return 1;
3355 case tcc_expression:
3356 if (code == COND_EXPR)
3357 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3358 cval1, cval2, save_p)
3359 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3360 cval1, cval2, save_p)
3361 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3362 cval1, cval2, save_p));
3363 return 0;
3365 case tcc_comparison:
3366 /* First see if we can handle the first operand, then the second. For
3367 the second operand, we know *CVAL1 can't be zero. It must be that
3368 one side of the comparison is each of the values; test for the
3369 case where this isn't true by failing if the two operands
3370 are the same. */
3372 if (operand_equal_p (TREE_OPERAND (arg, 0),
3373 TREE_OPERAND (arg, 1), 0))
3374 return 0;
3376 if (*cval1 == 0)
3377 *cval1 = TREE_OPERAND (arg, 0);
3378 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3380 else if (*cval2 == 0)
3381 *cval2 = TREE_OPERAND (arg, 0);
3382 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3384 else
3385 return 0;
3387 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3389 else if (*cval2 == 0)
3390 *cval2 = TREE_OPERAND (arg, 1);
3391 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3393 else
3394 return 0;
3396 return 1;
3398 default:
3399 return 0;
3403 /* ARG is a tree that is known to contain just arithmetic operations and
3404 comparisons. Evaluate the operations in the tree substituting NEW0 for
3405 any occurrence of OLD0 as an operand of a comparison and likewise for
3406 NEW1 and OLD1. */
3408 static tree
3409 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3410 tree old1, tree new1)
3412 tree type = TREE_TYPE (arg);
3413 enum tree_code code = TREE_CODE (arg);
3414 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3416 /* We can handle some of the tcc_expression cases here. */
3417 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3418 tclass = tcc_unary;
3419 else if (tclass == tcc_expression
3420 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3421 tclass = tcc_binary;
3423 switch (tclass)
3425 case tcc_unary:
3426 return fold_build1_loc (loc, code, type,
3427 eval_subst (loc, TREE_OPERAND (arg, 0),
3428 old0, new0, old1, new1));
3430 case tcc_binary:
3431 return fold_build2_loc (loc, code, type,
3432 eval_subst (loc, TREE_OPERAND (arg, 0),
3433 old0, new0, old1, new1),
3434 eval_subst (loc, TREE_OPERAND (arg, 1),
3435 old0, new0, old1, new1));
3437 case tcc_expression:
3438 switch (code)
3440 case SAVE_EXPR:
3441 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3442 old1, new1);
3444 case COMPOUND_EXPR:
3445 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3446 old1, new1);
3448 case COND_EXPR:
3449 return fold_build3_loc (loc, code, type,
3450 eval_subst (loc, TREE_OPERAND (arg, 0),
3451 old0, new0, old1, new1),
3452 eval_subst (loc, TREE_OPERAND (arg, 1),
3453 old0, new0, old1, new1),
3454 eval_subst (loc, TREE_OPERAND (arg, 2),
3455 old0, new0, old1, new1));
3456 default:
3457 break;
3459 /* Fall through - ??? */
3461 case tcc_comparison:
3463 tree arg0 = TREE_OPERAND (arg, 0);
3464 tree arg1 = TREE_OPERAND (arg, 1);
3466 /* We need to check both for exact equality and tree equality. The
3467 former will be true if the operand has a side-effect. In that
3468 case, we know the operand occurred exactly once. */
3470 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3471 arg0 = new0;
3472 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3473 arg0 = new1;
3475 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3476 arg1 = new0;
3477 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3478 arg1 = new1;
3480 return fold_build2_loc (loc, code, type, arg0, arg1);
3483 default:
3484 return arg;
3488 /* Return a tree for the case when the result of an expression is RESULT
3489 converted to TYPE and OMITTED was previously an operand of the expression
3490 but is now not needed (e.g., we folded OMITTED * 0).
3492 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3493 the conversion of RESULT to TYPE. */
3495 tree
3496 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3498 tree t = fold_convert_loc (loc, type, result);
3500 /* If the resulting operand is an empty statement, just return the omitted
3501 statement casted to void. */
3502 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3503 return build1_loc (loc, NOP_EXPR, void_type_node,
3504 fold_ignored_result (omitted));
3506 if (TREE_SIDE_EFFECTS (omitted))
3507 return build2_loc (loc, COMPOUND_EXPR, type,
3508 fold_ignored_result (omitted), t);
3510 return non_lvalue_loc (loc, t);
3513 /* Return a tree for the case when the result of an expression is RESULT
3514 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3515 of the expression but are now not needed.
3517 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3518 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3519 evaluated before OMITTED2. Otherwise, if neither has side effects,
3520 just do the conversion of RESULT to TYPE. */
3522 tree
3523 omit_two_operands_loc (location_t loc, tree type, tree result,
3524 tree omitted1, tree omitted2)
3526 tree t = fold_convert_loc (loc, type, result);
3528 if (TREE_SIDE_EFFECTS (omitted2))
3529 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3530 if (TREE_SIDE_EFFECTS (omitted1))
3531 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3533 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3537 /* Return a simplified tree node for the truth-negation of ARG. This
3538 never alters ARG itself. We assume that ARG is an operation that
3539 returns a truth value (0 or 1).
3541 FIXME: one would think we would fold the result, but it causes
3542 problems with the dominator optimizer. */
3544 static tree
3545 fold_truth_not_expr (location_t loc, tree arg)
3547 tree type = TREE_TYPE (arg);
3548 enum tree_code code = TREE_CODE (arg);
3549 location_t loc1, loc2;
3551 /* If this is a comparison, we can simply invert it, except for
3552 floating-point non-equality comparisons, in which case we just
3553 enclose a TRUTH_NOT_EXPR around what we have. */
3555 if (TREE_CODE_CLASS (code) == tcc_comparison)
3557 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3558 if (FLOAT_TYPE_P (op_type)
3559 && flag_trapping_math
3560 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3561 && code != NE_EXPR && code != EQ_EXPR)
3562 return NULL_TREE;
3564 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3565 if (code == ERROR_MARK)
3566 return NULL_TREE;
3568 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3569 TREE_OPERAND (arg, 1));
3572 switch (code)
3574 case INTEGER_CST:
3575 return constant_boolean_node (integer_zerop (arg), type);
3577 case TRUTH_AND_EXPR:
3578 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3579 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3580 return build2_loc (loc, TRUTH_OR_EXPR, type,
3581 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3582 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3584 case TRUTH_OR_EXPR:
3585 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3586 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3587 return build2_loc (loc, TRUTH_AND_EXPR, type,
3588 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3589 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3591 case TRUTH_XOR_EXPR:
3592 /* Here we can invert either operand. We invert the first operand
3593 unless the second operand is a TRUTH_NOT_EXPR in which case our
3594 result is the XOR of the first operand with the inside of the
3595 negation of the second operand. */
3597 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3598 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3599 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3600 else
3601 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3602 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3603 TREE_OPERAND (arg, 1));
3605 case TRUTH_ANDIF_EXPR:
3606 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3607 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3608 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3609 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3610 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3612 case TRUTH_ORIF_EXPR:
3613 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3614 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3615 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3616 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3617 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3619 case TRUTH_NOT_EXPR:
3620 return TREE_OPERAND (arg, 0);
3622 case COND_EXPR:
3624 tree arg1 = TREE_OPERAND (arg, 1);
3625 tree arg2 = TREE_OPERAND (arg, 2);
3627 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3628 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3630 /* A COND_EXPR may have a throw as one operand, which
3631 then has void type. Just leave void operands
3632 as they are. */
3633 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3634 VOID_TYPE_P (TREE_TYPE (arg1))
3635 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3636 VOID_TYPE_P (TREE_TYPE (arg2))
3637 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3640 case COMPOUND_EXPR:
3641 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3642 return build2_loc (loc, COMPOUND_EXPR, type,
3643 TREE_OPERAND (arg, 0),
3644 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3646 case NON_LVALUE_EXPR:
3647 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3648 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3650 CASE_CONVERT:
3651 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3652 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3654 /* ... fall through ... */
3656 case FLOAT_EXPR:
3657 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3658 return build1_loc (loc, TREE_CODE (arg), type,
3659 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3661 case BIT_AND_EXPR:
3662 if (!integer_onep (TREE_OPERAND (arg, 1)))
3663 return NULL_TREE;
3664 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3666 case SAVE_EXPR:
3667 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3669 case CLEANUP_POINT_EXPR:
3670 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3671 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3672 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3674 default:
3675 return NULL_TREE;
3679 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3680 assume that ARG is an operation that returns a truth value (0 or 1
3681 for scalars, 0 or -1 for vectors). Return the folded expression if
3682 folding is successful. Otherwise, return NULL_TREE. */
3684 static tree
3685 fold_invert_truthvalue (location_t loc, tree arg)
3687 tree type = TREE_TYPE (arg);
3688 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3689 ? BIT_NOT_EXPR
3690 : TRUTH_NOT_EXPR,
3691 type, arg);
3694 /* Return a simplified tree node for the truth-negation of ARG. This
3695 never alters ARG itself. We assume that ARG is an operation that
3696 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3698 tree
3699 invert_truthvalue_loc (location_t loc, tree arg)
3701 if (TREE_CODE (arg) == ERROR_MARK)
3702 return arg;
3704 tree type = TREE_TYPE (arg);
3705 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3706 ? BIT_NOT_EXPR
3707 : TRUTH_NOT_EXPR,
3708 type, arg);
3711 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3712 with code CODE. This optimization is unsafe. */
3713 static tree
3714 distribute_real_division (location_t loc, enum tree_code code, tree type,
3715 tree arg0, tree arg1)
3717 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3718 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3720 /* (A / C) +- (B / C) -> (A +- B) / C. */
3721 if (mul0 == mul1
3722 && operand_equal_p (TREE_OPERAND (arg0, 1),
3723 TREE_OPERAND (arg1, 1), 0))
3724 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3725 fold_build2_loc (loc, code, type,
3726 TREE_OPERAND (arg0, 0),
3727 TREE_OPERAND (arg1, 0)),
3728 TREE_OPERAND (arg0, 1));
3730 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3731 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3732 TREE_OPERAND (arg1, 0), 0)
3733 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3734 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3736 REAL_VALUE_TYPE r0, r1;
3737 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3738 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3739 if (!mul0)
3740 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3741 if (!mul1)
3742 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3743 real_arithmetic (&r0, code, &r0, &r1);
3744 return fold_build2_loc (loc, MULT_EXPR, type,
3745 TREE_OPERAND (arg0, 0),
3746 build_real (type, r0));
3749 return NULL_TREE;
3752 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3753 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3754 and uses reverse storage order if REVERSEP is nonzero. */
3756 static tree
3757 make_bit_field_ref (location_t loc, tree inner, tree type,
3758 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3759 int unsignedp, int reversep)
3761 tree result, bftype;
3763 if (bitpos == 0 && !reversep)
3765 tree size = TYPE_SIZE (TREE_TYPE (inner));
3766 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3767 || POINTER_TYPE_P (TREE_TYPE (inner)))
3768 && tree_fits_shwi_p (size)
3769 && tree_to_shwi (size) == bitsize)
3770 return fold_convert_loc (loc, type, inner);
3773 bftype = type;
3774 if (TYPE_PRECISION (bftype) != bitsize
3775 || TYPE_UNSIGNED (bftype) == !unsignedp)
3776 bftype = build_nonstandard_integer_type (bitsize, 0);
3778 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3779 size_int (bitsize), bitsize_int (bitpos));
3780 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3782 if (bftype != type)
3783 result = fold_convert_loc (loc, type, result);
3785 return result;
3788 /* Optimize a bit-field compare.
3790 There are two cases: First is a compare against a constant and the
3791 second is a comparison of two items where the fields are at the same
3792 bit position relative to the start of a chunk (byte, halfword, word)
3793 large enough to contain it. In these cases we can avoid the shift
3794 implicit in bitfield extractions.
3796 For constants, we emit a compare of the shifted constant with the
3797 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3798 compared. For two fields at the same position, we do the ANDs with the
3799 similar mask and compare the result of the ANDs.
3801 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3802 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3803 are the left and right operands of the comparison, respectively.
3805 If the optimization described above can be done, we return the resulting
3806 tree. Otherwise we return zero. */
3808 static tree
3809 optimize_bit_field_compare (location_t loc, enum tree_code code,
3810 tree compare_type, tree lhs, tree rhs)
3812 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3813 tree type = TREE_TYPE (lhs);
3814 tree unsigned_type;
3815 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3816 machine_mode lmode, rmode, nmode;
3817 int lunsignedp, runsignedp;
3818 int lreversep, rreversep;
3819 int lvolatilep = 0, rvolatilep = 0;
3820 tree linner, rinner = NULL_TREE;
3821 tree mask;
3822 tree offset;
3824 /* Get all the information about the extractions being done. If the bit size
3825 if the same as the size of the underlying object, we aren't doing an
3826 extraction at all and so can do nothing. We also don't want to
3827 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3828 then will no longer be able to replace it. */
3829 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3830 &lunsignedp, &lreversep, &lvolatilep, false);
3831 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3832 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3833 return 0;
3835 if (const_p)
3836 rreversep = lreversep;
3837 else
3839 /* If this is not a constant, we can only do something if bit positions,
3840 sizes, signedness and storage order are the same. */
3841 rinner
3842 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3843 &runsignedp, &rreversep, &rvolatilep, false);
3845 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3846 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3847 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3848 return 0;
3851 /* See if we can find a mode to refer to this field. We should be able to,
3852 but fail if we can't. */
3853 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3854 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3855 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3856 TYPE_ALIGN (TREE_TYPE (rinner))),
3857 word_mode, false);
3858 if (nmode == VOIDmode)
3859 return 0;
3861 /* Set signed and unsigned types of the precision of this mode for the
3862 shifts below. */
3863 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3865 /* Compute the bit position and size for the new reference and our offset
3866 within it. If the new reference is the same size as the original, we
3867 won't optimize anything, so return zero. */
3868 nbitsize = GET_MODE_BITSIZE (nmode);
3869 nbitpos = lbitpos & ~ (nbitsize - 1);
3870 lbitpos -= nbitpos;
3871 if (nbitsize == lbitsize)
3872 return 0;
3874 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3875 lbitpos = nbitsize - lbitsize - lbitpos;
3877 /* Make the mask to be used against the extracted field. */
3878 mask = build_int_cst_type (unsigned_type, -1);
3879 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3880 mask = const_binop (RSHIFT_EXPR, mask,
3881 size_int (nbitsize - lbitsize - lbitpos));
3883 if (! const_p)
3884 /* If not comparing with constant, just rework the comparison
3885 and return. */
3886 return fold_build2_loc (loc, code, compare_type,
3887 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3888 make_bit_field_ref (loc, linner,
3889 unsigned_type,
3890 nbitsize, nbitpos,
3891 1, lreversep),
3892 mask),
3893 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3894 make_bit_field_ref (loc, rinner,
3895 unsigned_type,
3896 nbitsize, nbitpos,
3897 1, rreversep),
3898 mask));
3900 /* Otherwise, we are handling the constant case. See if the constant is too
3901 big for the field. Warn and return a tree for 0 (false) if so. We do
3902 this not only for its own sake, but to avoid having to test for this
3903 error case below. If we didn't, we might generate wrong code.
3905 For unsigned fields, the constant shifted right by the field length should
3906 be all zero. For signed fields, the high-order bits should agree with
3907 the sign bit. */
3909 if (lunsignedp)
3911 if (wi::lrshift (rhs, lbitsize) != 0)
3913 warning (0, "comparison is always %d due to width of bit-field",
3914 code == NE_EXPR);
3915 return constant_boolean_node (code == NE_EXPR, compare_type);
3918 else
3920 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3921 if (tem != 0 && tem != -1)
3923 warning (0, "comparison is always %d due to width of bit-field",
3924 code == NE_EXPR);
3925 return constant_boolean_node (code == NE_EXPR, compare_type);
3929 /* Single-bit compares should always be against zero. */
3930 if (lbitsize == 1 && ! integer_zerop (rhs))
3932 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3933 rhs = build_int_cst (type, 0);
3936 /* Make a new bitfield reference, shift the constant over the
3937 appropriate number of bits and mask it with the computed mask
3938 (in case this was a signed field). If we changed it, make a new one. */
3939 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1,
3940 lreversep);
3942 rhs = const_binop (BIT_AND_EXPR,
3943 const_binop (LSHIFT_EXPR,
3944 fold_convert_loc (loc, unsigned_type, rhs),
3945 size_int (lbitpos)),
3946 mask);
3948 lhs = build2_loc (loc, code, compare_type,
3949 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3950 return lhs;
3953 /* Subroutine for fold_truth_andor_1: decode a field reference.
3955 If EXP is a comparison reference, we return the innermost reference.
3957 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3958 set to the starting bit number.
3960 If the innermost field can be completely contained in a mode-sized
3961 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3963 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3964 otherwise it is not changed.
3966 *PUNSIGNEDP is set to the signedness of the field.
3968 *PREVERSEP is set to the storage order of the field.
3970 *PMASK is set to the mask used. This is either contained in a
3971 BIT_AND_EXPR or derived from the width of the field.
3973 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3975 Return 0 if this is not a component reference or is one that we can't
3976 do anything with. */
3978 static tree
3979 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3980 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3981 int *punsignedp, int *preversep, int *pvolatilep,
3982 tree *pmask, tree *pand_mask)
3984 tree outer_type = 0;
3985 tree and_mask = 0;
3986 tree mask, inner, offset;
3987 tree unsigned_type;
3988 unsigned int precision;
3990 /* All the optimizations using this function assume integer fields.
3991 There are problems with FP fields since the type_for_size call
3992 below can fail for, e.g., XFmode. */
3993 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3994 return 0;
3996 /* We are interested in the bare arrangement of bits, so strip everything
3997 that doesn't affect the machine mode. However, record the type of the
3998 outermost expression if it may matter below. */
3999 if (CONVERT_EXPR_P (exp)
4000 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4001 outer_type = TREE_TYPE (exp);
4002 STRIP_NOPS (exp);
4004 if (TREE_CODE (exp) == BIT_AND_EXPR)
4006 and_mask = TREE_OPERAND (exp, 1);
4007 exp = TREE_OPERAND (exp, 0);
4008 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4009 if (TREE_CODE (and_mask) != INTEGER_CST)
4010 return 0;
4013 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4014 punsignedp, preversep, pvolatilep, false);
4015 if ((inner == exp && and_mask == 0)
4016 || *pbitsize < 0 || offset != 0
4017 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4018 return 0;
4020 /* If the number of bits in the reference is the same as the bitsize of
4021 the outer type, then the outer type gives the signedness. Otherwise
4022 (in case of a small bitfield) the signedness is unchanged. */
4023 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4024 *punsignedp = TYPE_UNSIGNED (outer_type);
4026 /* Compute the mask to access the bitfield. */
4027 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4028 precision = TYPE_PRECISION (unsigned_type);
4030 mask = build_int_cst_type (unsigned_type, -1);
4032 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4033 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4035 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4036 if (and_mask != 0)
4037 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4038 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4040 *pmask = mask;
4041 *pand_mask = and_mask;
4042 return inner;
4045 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4046 bit positions and MASK is SIGNED. */
4048 static int
4049 all_ones_mask_p (const_tree mask, unsigned int size)
4051 tree type = TREE_TYPE (mask);
4052 unsigned int precision = TYPE_PRECISION (type);
4054 /* If this function returns true when the type of the mask is
4055 UNSIGNED, then there will be errors. In particular see
4056 gcc.c-torture/execute/990326-1.c. There does not appear to be
4057 any documentation paper trail as to why this is so. But the pre
4058 wide-int worked with that restriction and it has been preserved
4059 here. */
4060 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4061 return false;
4063 return wi::mask (size, false, precision) == mask;
4066 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4067 represents the sign bit of EXP's type. If EXP represents a sign
4068 or zero extension, also test VAL against the unextended type.
4069 The return value is the (sub)expression whose sign bit is VAL,
4070 or NULL_TREE otherwise. */
4072 tree
4073 sign_bit_p (tree exp, const_tree val)
4075 int width;
4076 tree t;
4078 /* Tree EXP must have an integral type. */
4079 t = TREE_TYPE (exp);
4080 if (! INTEGRAL_TYPE_P (t))
4081 return NULL_TREE;
4083 /* Tree VAL must be an integer constant. */
4084 if (TREE_CODE (val) != INTEGER_CST
4085 || TREE_OVERFLOW (val))
4086 return NULL_TREE;
4088 width = TYPE_PRECISION (t);
4089 if (wi::only_sign_bit_p (val, width))
4090 return exp;
4092 /* Handle extension from a narrower type. */
4093 if (TREE_CODE (exp) == NOP_EXPR
4094 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4095 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4097 return NULL_TREE;
4100 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4101 to be evaluated unconditionally. */
4103 static int
4104 simple_operand_p (const_tree exp)
4106 /* Strip any conversions that don't change the machine mode. */
4107 STRIP_NOPS (exp);
4109 return (CONSTANT_CLASS_P (exp)
4110 || TREE_CODE (exp) == SSA_NAME
4111 || (DECL_P (exp)
4112 && ! TREE_ADDRESSABLE (exp)
4113 && ! TREE_THIS_VOLATILE (exp)
4114 && ! DECL_NONLOCAL (exp)
4115 /* Don't regard global variables as simple. They may be
4116 allocated in ways unknown to the compiler (shared memory,
4117 #pragma weak, etc). */
4118 && ! TREE_PUBLIC (exp)
4119 && ! DECL_EXTERNAL (exp)
4120 /* Weakrefs are not safe to be read, since they can be NULL.
4121 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4122 have DECL_WEAK flag set. */
4123 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4124 /* Loading a static variable is unduly expensive, but global
4125 registers aren't expensive. */
4126 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4129 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4130 to be evaluated unconditionally.
4131 I addition to simple_operand_p, we assume that comparisons, conversions,
4132 and logic-not operations are simple, if their operands are simple, too. */
4134 static bool
4135 simple_operand_p_2 (tree exp)
4137 enum tree_code code;
4139 if (TREE_SIDE_EFFECTS (exp)
4140 || tree_could_trap_p (exp))
4141 return false;
4143 while (CONVERT_EXPR_P (exp))
4144 exp = TREE_OPERAND (exp, 0);
4146 code = TREE_CODE (exp);
4148 if (TREE_CODE_CLASS (code) == tcc_comparison)
4149 return (simple_operand_p (TREE_OPERAND (exp, 0))
4150 && simple_operand_p (TREE_OPERAND (exp, 1)));
4152 if (code == TRUTH_NOT_EXPR)
4153 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4155 return simple_operand_p (exp);
4159 /* The following functions are subroutines to fold_range_test and allow it to
4160 try to change a logical combination of comparisons into a range test.
4162 For example, both
4163 X == 2 || X == 3 || X == 4 || X == 5
4165 X >= 2 && X <= 5
4166 are converted to
4167 (unsigned) (X - 2) <= 3
4169 We describe each set of comparisons as being either inside or outside
4170 a range, using a variable named like IN_P, and then describe the
4171 range with a lower and upper bound. If one of the bounds is omitted,
4172 it represents either the highest or lowest value of the type.
4174 In the comments below, we represent a range by two numbers in brackets
4175 preceded by a "+" to designate being inside that range, or a "-" to
4176 designate being outside that range, so the condition can be inverted by
4177 flipping the prefix. An omitted bound is represented by a "-". For
4178 example, "- [-, 10]" means being outside the range starting at the lowest
4179 possible value and ending at 10, in other words, being greater than 10.
4180 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4181 always false.
4183 We set up things so that the missing bounds are handled in a consistent
4184 manner so neither a missing bound nor "true" and "false" need to be
4185 handled using a special case. */
4187 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4188 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4189 and UPPER1_P are nonzero if the respective argument is an upper bound
4190 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4191 must be specified for a comparison. ARG1 will be converted to ARG0's
4192 type if both are specified. */
4194 static tree
4195 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4196 tree arg1, int upper1_p)
4198 tree tem;
4199 int result;
4200 int sgn0, sgn1;
4202 /* If neither arg represents infinity, do the normal operation.
4203 Else, if not a comparison, return infinity. Else handle the special
4204 comparison rules. Note that most of the cases below won't occur, but
4205 are handled for consistency. */
4207 if (arg0 != 0 && arg1 != 0)
4209 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4210 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4211 STRIP_NOPS (tem);
4212 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4215 if (TREE_CODE_CLASS (code) != tcc_comparison)
4216 return 0;
4218 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4219 for neither. In real maths, we cannot assume open ended ranges are
4220 the same. But, this is computer arithmetic, where numbers are finite.
4221 We can therefore make the transformation of any unbounded range with
4222 the value Z, Z being greater than any representable number. This permits
4223 us to treat unbounded ranges as equal. */
4224 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4225 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4226 switch (code)
4228 case EQ_EXPR:
4229 result = sgn0 == sgn1;
4230 break;
4231 case NE_EXPR:
4232 result = sgn0 != sgn1;
4233 break;
4234 case LT_EXPR:
4235 result = sgn0 < sgn1;
4236 break;
4237 case LE_EXPR:
4238 result = sgn0 <= sgn1;
4239 break;
4240 case GT_EXPR:
4241 result = sgn0 > sgn1;
4242 break;
4243 case GE_EXPR:
4244 result = sgn0 >= sgn1;
4245 break;
4246 default:
4247 gcc_unreachable ();
4250 return constant_boolean_node (result, type);
4253 /* Helper routine for make_range. Perform one step for it, return
4254 new expression if the loop should continue or NULL_TREE if it should
4255 stop. */
4257 tree
4258 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4259 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4260 bool *strict_overflow_p)
4262 tree arg0_type = TREE_TYPE (arg0);
4263 tree n_low, n_high, low = *p_low, high = *p_high;
4264 int in_p = *p_in_p, n_in_p;
4266 switch (code)
4268 case TRUTH_NOT_EXPR:
4269 /* We can only do something if the range is testing for zero. */
4270 if (low == NULL_TREE || high == NULL_TREE
4271 || ! integer_zerop (low) || ! integer_zerop (high))
4272 return NULL_TREE;
4273 *p_in_p = ! in_p;
4274 return arg0;
4276 case EQ_EXPR: case NE_EXPR:
4277 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4278 /* We can only do something if the range is testing for zero
4279 and if the second operand is an integer constant. Note that
4280 saying something is "in" the range we make is done by
4281 complementing IN_P since it will set in the initial case of
4282 being not equal to zero; "out" is leaving it alone. */
4283 if (low == NULL_TREE || high == NULL_TREE
4284 || ! integer_zerop (low) || ! integer_zerop (high)
4285 || TREE_CODE (arg1) != INTEGER_CST)
4286 return NULL_TREE;
4288 switch (code)
4290 case NE_EXPR: /* - [c, c] */
4291 low = high = arg1;
4292 break;
4293 case EQ_EXPR: /* + [c, c] */
4294 in_p = ! in_p, low = high = arg1;
4295 break;
4296 case GT_EXPR: /* - [-, c] */
4297 low = 0, high = arg1;
4298 break;
4299 case GE_EXPR: /* + [c, -] */
4300 in_p = ! in_p, low = arg1, high = 0;
4301 break;
4302 case LT_EXPR: /* - [c, -] */
4303 low = arg1, high = 0;
4304 break;
4305 case LE_EXPR: /* + [-, c] */
4306 in_p = ! in_p, low = 0, high = arg1;
4307 break;
4308 default:
4309 gcc_unreachable ();
4312 /* If this is an unsigned comparison, we also know that EXP is
4313 greater than or equal to zero. We base the range tests we make
4314 on that fact, so we record it here so we can parse existing
4315 range tests. We test arg0_type since often the return type
4316 of, e.g. EQ_EXPR, is boolean. */
4317 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4319 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4320 in_p, low, high, 1,
4321 build_int_cst (arg0_type, 0),
4322 NULL_TREE))
4323 return NULL_TREE;
4325 in_p = n_in_p, low = n_low, high = n_high;
4327 /* If the high bound is missing, but we have a nonzero low
4328 bound, reverse the range so it goes from zero to the low bound
4329 minus 1. */
4330 if (high == 0 && low && ! integer_zerop (low))
4332 in_p = ! in_p;
4333 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4334 build_int_cst (TREE_TYPE (low), 1), 0);
4335 low = build_int_cst (arg0_type, 0);
4339 *p_low = low;
4340 *p_high = high;
4341 *p_in_p = in_p;
4342 return arg0;
4344 case NEGATE_EXPR:
4345 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4346 low and high are non-NULL, then normalize will DTRT. */
4347 if (!TYPE_UNSIGNED (arg0_type)
4348 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4350 if (low == NULL_TREE)
4351 low = TYPE_MIN_VALUE (arg0_type);
4352 if (high == NULL_TREE)
4353 high = TYPE_MAX_VALUE (arg0_type);
4356 /* (-x) IN [a,b] -> x in [-b, -a] */
4357 n_low = range_binop (MINUS_EXPR, exp_type,
4358 build_int_cst (exp_type, 0),
4359 0, high, 1);
4360 n_high = range_binop (MINUS_EXPR, exp_type,
4361 build_int_cst (exp_type, 0),
4362 0, low, 0);
4363 if (n_high != 0 && TREE_OVERFLOW (n_high))
4364 return NULL_TREE;
4365 goto normalize;
4367 case BIT_NOT_EXPR:
4368 /* ~ X -> -X - 1 */
4369 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4370 build_int_cst (exp_type, 1));
4372 case PLUS_EXPR:
4373 case MINUS_EXPR:
4374 if (TREE_CODE (arg1) != INTEGER_CST)
4375 return NULL_TREE;
4377 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4378 move a constant to the other side. */
4379 if (!TYPE_UNSIGNED (arg0_type)
4380 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4381 return NULL_TREE;
4383 /* If EXP is signed, any overflow in the computation is undefined,
4384 so we don't worry about it so long as our computations on
4385 the bounds don't overflow. For unsigned, overflow is defined
4386 and this is exactly the right thing. */
4387 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4388 arg0_type, low, 0, arg1, 0);
4389 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4390 arg0_type, high, 1, arg1, 0);
4391 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4392 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4393 return NULL_TREE;
4395 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4396 *strict_overflow_p = true;
4398 normalize:
4399 /* Check for an unsigned range which has wrapped around the maximum
4400 value thus making n_high < n_low, and normalize it. */
4401 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4403 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4404 build_int_cst (TREE_TYPE (n_high), 1), 0);
4405 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4406 build_int_cst (TREE_TYPE (n_low), 1), 0);
4408 /* If the range is of the form +/- [ x+1, x ], we won't
4409 be able to normalize it. But then, it represents the
4410 whole range or the empty set, so make it
4411 +/- [ -, - ]. */
4412 if (tree_int_cst_equal (n_low, low)
4413 && tree_int_cst_equal (n_high, high))
4414 low = high = 0;
4415 else
4416 in_p = ! in_p;
4418 else
4419 low = n_low, high = n_high;
4421 *p_low = low;
4422 *p_high = high;
4423 *p_in_p = in_p;
4424 return arg0;
4426 CASE_CONVERT:
4427 case NON_LVALUE_EXPR:
4428 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4429 return NULL_TREE;
4431 if (! INTEGRAL_TYPE_P (arg0_type)
4432 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4433 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4434 return NULL_TREE;
4436 n_low = low, n_high = high;
4438 if (n_low != 0)
4439 n_low = fold_convert_loc (loc, arg0_type, n_low);
4441 if (n_high != 0)
4442 n_high = fold_convert_loc (loc, arg0_type, n_high);
4444 /* If we're converting arg0 from an unsigned type, to exp,
4445 a signed type, we will be doing the comparison as unsigned.
4446 The tests above have already verified that LOW and HIGH
4447 are both positive.
4449 So we have to ensure that we will handle large unsigned
4450 values the same way that the current signed bounds treat
4451 negative values. */
4453 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4455 tree high_positive;
4456 tree equiv_type;
4457 /* For fixed-point modes, we need to pass the saturating flag
4458 as the 2nd parameter. */
4459 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4460 equiv_type
4461 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4462 TYPE_SATURATING (arg0_type));
4463 else
4464 equiv_type
4465 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4467 /* A range without an upper bound is, naturally, unbounded.
4468 Since convert would have cropped a very large value, use
4469 the max value for the destination type. */
4470 high_positive
4471 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4472 : TYPE_MAX_VALUE (arg0_type);
4474 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4475 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4476 fold_convert_loc (loc, arg0_type,
4477 high_positive),
4478 build_int_cst (arg0_type, 1));
4480 /* If the low bound is specified, "and" the range with the
4481 range for which the original unsigned value will be
4482 positive. */
4483 if (low != 0)
4485 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4486 1, fold_convert_loc (loc, arg0_type,
4487 integer_zero_node),
4488 high_positive))
4489 return NULL_TREE;
4491 in_p = (n_in_p == in_p);
4493 else
4495 /* Otherwise, "or" the range with the range of the input
4496 that will be interpreted as negative. */
4497 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4498 1, fold_convert_loc (loc, arg0_type,
4499 integer_zero_node),
4500 high_positive))
4501 return NULL_TREE;
4503 in_p = (in_p != n_in_p);
4507 *p_low = n_low;
4508 *p_high = n_high;
4509 *p_in_p = in_p;
4510 return arg0;
4512 default:
4513 return NULL_TREE;
4517 /* Given EXP, a logical expression, set the range it is testing into
4518 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4519 actually being tested. *PLOW and *PHIGH will be made of the same
4520 type as the returned expression. If EXP is not a comparison, we
4521 will most likely not be returning a useful value and range. Set
4522 *STRICT_OVERFLOW_P to true if the return value is only valid
4523 because signed overflow is undefined; otherwise, do not change
4524 *STRICT_OVERFLOW_P. */
4526 tree
4527 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4528 bool *strict_overflow_p)
4530 enum tree_code code;
4531 tree arg0, arg1 = NULL_TREE;
4532 tree exp_type, nexp;
4533 int in_p;
4534 tree low, high;
4535 location_t loc = EXPR_LOCATION (exp);
4537 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4538 and see if we can refine the range. Some of the cases below may not
4539 happen, but it doesn't seem worth worrying about this. We "continue"
4540 the outer loop when we've changed something; otherwise we "break"
4541 the switch, which will "break" the while. */
4543 in_p = 0;
4544 low = high = build_int_cst (TREE_TYPE (exp), 0);
4546 while (1)
4548 code = TREE_CODE (exp);
4549 exp_type = TREE_TYPE (exp);
4550 arg0 = NULL_TREE;
4552 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4554 if (TREE_OPERAND_LENGTH (exp) > 0)
4555 arg0 = TREE_OPERAND (exp, 0);
4556 if (TREE_CODE_CLASS (code) == tcc_binary
4557 || TREE_CODE_CLASS (code) == tcc_comparison
4558 || (TREE_CODE_CLASS (code) == tcc_expression
4559 && TREE_OPERAND_LENGTH (exp) > 1))
4560 arg1 = TREE_OPERAND (exp, 1);
4562 if (arg0 == NULL_TREE)
4563 break;
4565 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4566 &high, &in_p, strict_overflow_p);
4567 if (nexp == NULL_TREE)
4568 break;
4569 exp = nexp;
4572 /* If EXP is a constant, we can evaluate whether this is true or false. */
4573 if (TREE_CODE (exp) == INTEGER_CST)
4575 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4576 exp, 0, low, 0))
4577 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4578 exp, 1, high, 1)));
4579 low = high = 0;
4580 exp = 0;
4583 *pin_p = in_p, *plow = low, *phigh = high;
4584 return exp;
4587 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4588 type, TYPE, return an expression to test if EXP is in (or out of, depending
4589 on IN_P) the range. Return 0 if the test couldn't be created. */
4591 tree
4592 build_range_check (location_t loc, tree type, tree exp, int in_p,
4593 tree low, tree high)
4595 tree etype = TREE_TYPE (exp), value;
4597 /* Disable this optimization for function pointer expressions
4598 on targets that require function pointer canonicalization. */
4599 if (targetm.have_canonicalize_funcptr_for_compare ()
4600 && TREE_CODE (etype) == POINTER_TYPE
4601 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4602 return NULL_TREE;
4604 if (! in_p)
4606 value = build_range_check (loc, type, exp, 1, low, high);
4607 if (value != 0)
4608 return invert_truthvalue_loc (loc, value);
4610 return 0;
4613 if (low == 0 && high == 0)
4614 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4616 if (low == 0)
4617 return fold_build2_loc (loc, LE_EXPR, type, exp,
4618 fold_convert_loc (loc, etype, high));
4620 if (high == 0)
4621 return fold_build2_loc (loc, GE_EXPR, type, exp,
4622 fold_convert_loc (loc, etype, low));
4624 if (operand_equal_p (low, high, 0))
4625 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4626 fold_convert_loc (loc, etype, low));
4628 if (integer_zerop (low))
4630 if (! TYPE_UNSIGNED (etype))
4632 etype = unsigned_type_for (etype);
4633 high = fold_convert_loc (loc, etype, high);
4634 exp = fold_convert_loc (loc, etype, exp);
4636 return build_range_check (loc, type, exp, 1, 0, high);
4639 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4640 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4642 int prec = TYPE_PRECISION (etype);
4644 if (wi::mask (prec - 1, false, prec) == high)
4646 if (TYPE_UNSIGNED (etype))
4648 tree signed_etype = signed_type_for (etype);
4649 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4650 etype
4651 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4652 else
4653 etype = signed_etype;
4654 exp = fold_convert_loc (loc, etype, exp);
4656 return fold_build2_loc (loc, GT_EXPR, type, exp,
4657 build_int_cst (etype, 0));
4661 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4662 This requires wrap-around arithmetics for the type of the expression.
4663 First make sure that arithmetics in this type is valid, then make sure
4664 that it wraps around. */
4665 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4666 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4667 TYPE_UNSIGNED (etype));
4669 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4671 tree utype, minv, maxv;
4673 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4674 for the type in question, as we rely on this here. */
4675 utype = unsigned_type_for (etype);
4676 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4677 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4678 build_int_cst (TREE_TYPE (maxv), 1), 1);
4679 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4681 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4682 minv, 1, maxv, 1)))
4683 etype = utype;
4684 else
4685 return 0;
4688 high = fold_convert_loc (loc, etype, high);
4689 low = fold_convert_loc (loc, etype, low);
4690 exp = fold_convert_loc (loc, etype, exp);
4692 value = const_binop (MINUS_EXPR, high, low);
4695 if (POINTER_TYPE_P (etype))
4697 if (value != 0 && !TREE_OVERFLOW (value))
4699 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4700 return build_range_check (loc, type,
4701 fold_build_pointer_plus_loc (loc, exp, low),
4702 1, build_int_cst (etype, 0), value);
4704 return 0;
4707 if (value != 0 && !TREE_OVERFLOW (value))
4708 return build_range_check (loc, type,
4709 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4710 1, build_int_cst (etype, 0), value);
4712 return 0;
4715 /* Return the predecessor of VAL in its type, handling the infinite case. */
4717 static tree
4718 range_predecessor (tree val)
4720 tree type = TREE_TYPE (val);
4722 if (INTEGRAL_TYPE_P (type)
4723 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4724 return 0;
4725 else
4726 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4727 build_int_cst (TREE_TYPE (val), 1), 0);
4730 /* Return the successor of VAL in its type, handling the infinite case. */
4732 static tree
4733 range_successor (tree val)
4735 tree type = TREE_TYPE (val);
4737 if (INTEGRAL_TYPE_P (type)
4738 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4739 return 0;
4740 else
4741 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4742 build_int_cst (TREE_TYPE (val), 1), 0);
4745 /* Given two ranges, see if we can merge them into one. Return 1 if we
4746 can, 0 if we can't. Set the output range into the specified parameters. */
4748 bool
4749 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4750 tree high0, int in1_p, tree low1, tree high1)
4752 int no_overlap;
4753 int subset;
4754 int temp;
4755 tree tem;
4756 int in_p;
4757 tree low, high;
4758 int lowequal = ((low0 == 0 && low1 == 0)
4759 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4760 low0, 0, low1, 0)));
4761 int highequal = ((high0 == 0 && high1 == 0)
4762 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4763 high0, 1, high1, 1)));
4765 /* Make range 0 be the range that starts first, or ends last if they
4766 start at the same value. Swap them if it isn't. */
4767 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4768 low0, 0, low1, 0))
4769 || (lowequal
4770 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4771 high1, 1, high0, 1))))
4773 temp = in0_p, in0_p = in1_p, in1_p = temp;
4774 tem = low0, low0 = low1, low1 = tem;
4775 tem = high0, high0 = high1, high1 = tem;
4778 /* Now flag two cases, whether the ranges are disjoint or whether the
4779 second range is totally subsumed in the first. Note that the tests
4780 below are simplified by the ones above. */
4781 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4782 high0, 1, low1, 0));
4783 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4784 high1, 1, high0, 1));
4786 /* We now have four cases, depending on whether we are including or
4787 excluding the two ranges. */
4788 if (in0_p && in1_p)
4790 /* If they don't overlap, the result is false. If the second range
4791 is a subset it is the result. Otherwise, the range is from the start
4792 of the second to the end of the first. */
4793 if (no_overlap)
4794 in_p = 0, low = high = 0;
4795 else if (subset)
4796 in_p = 1, low = low1, high = high1;
4797 else
4798 in_p = 1, low = low1, high = high0;
4801 else if (in0_p && ! in1_p)
4803 /* If they don't overlap, the result is the first range. If they are
4804 equal, the result is false. If the second range is a subset of the
4805 first, and the ranges begin at the same place, we go from just after
4806 the end of the second range to the end of the first. If the second
4807 range is not a subset of the first, or if it is a subset and both
4808 ranges end at the same place, the range starts at the start of the
4809 first range and ends just before the second range.
4810 Otherwise, we can't describe this as a single range. */
4811 if (no_overlap)
4812 in_p = 1, low = low0, high = high0;
4813 else if (lowequal && highequal)
4814 in_p = 0, low = high = 0;
4815 else if (subset && lowequal)
4817 low = range_successor (high1);
4818 high = high0;
4819 in_p = 1;
4820 if (low == 0)
4822 /* We are in the weird situation where high0 > high1 but
4823 high1 has no successor. Punt. */
4824 return 0;
4827 else if (! subset || highequal)
4829 low = low0;
4830 high = range_predecessor (low1);
4831 in_p = 1;
4832 if (high == 0)
4834 /* low0 < low1 but low1 has no predecessor. Punt. */
4835 return 0;
4838 else
4839 return 0;
4842 else if (! in0_p && in1_p)
4844 /* If they don't overlap, the result is the second range. If the second
4845 is a subset of the first, the result is false. Otherwise,
4846 the range starts just after the first range and ends at the
4847 end of the second. */
4848 if (no_overlap)
4849 in_p = 1, low = low1, high = high1;
4850 else if (subset || highequal)
4851 in_p = 0, low = high = 0;
4852 else
4854 low = range_successor (high0);
4855 high = high1;
4856 in_p = 1;
4857 if (low == 0)
4859 /* high1 > high0 but high0 has no successor. Punt. */
4860 return 0;
4865 else
4867 /* The case where we are excluding both ranges. Here the complex case
4868 is if they don't overlap. In that case, the only time we have a
4869 range is if they are adjacent. If the second is a subset of the
4870 first, the result is the first. Otherwise, the range to exclude
4871 starts at the beginning of the first range and ends at the end of the
4872 second. */
4873 if (no_overlap)
4875 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4876 range_successor (high0),
4877 1, low1, 0)))
4878 in_p = 0, low = low0, high = high1;
4879 else
4881 /* Canonicalize - [min, x] into - [-, x]. */
4882 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4883 switch (TREE_CODE (TREE_TYPE (low0)))
4885 case ENUMERAL_TYPE:
4886 if (TYPE_PRECISION (TREE_TYPE (low0))
4887 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4888 break;
4889 /* FALLTHROUGH */
4890 case INTEGER_TYPE:
4891 if (tree_int_cst_equal (low0,
4892 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4893 low0 = 0;
4894 break;
4895 case POINTER_TYPE:
4896 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4897 && integer_zerop (low0))
4898 low0 = 0;
4899 break;
4900 default:
4901 break;
4904 /* Canonicalize - [x, max] into - [x, -]. */
4905 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4906 switch (TREE_CODE (TREE_TYPE (high1)))
4908 case ENUMERAL_TYPE:
4909 if (TYPE_PRECISION (TREE_TYPE (high1))
4910 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4911 break;
4912 /* FALLTHROUGH */
4913 case INTEGER_TYPE:
4914 if (tree_int_cst_equal (high1,
4915 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4916 high1 = 0;
4917 break;
4918 case POINTER_TYPE:
4919 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4920 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4921 high1, 1,
4922 build_int_cst (TREE_TYPE (high1), 1),
4923 1)))
4924 high1 = 0;
4925 break;
4926 default:
4927 break;
4930 /* The ranges might be also adjacent between the maximum and
4931 minimum values of the given type. For
4932 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4933 return + [x + 1, y - 1]. */
4934 if (low0 == 0 && high1 == 0)
4936 low = range_successor (high0);
4937 high = range_predecessor (low1);
4938 if (low == 0 || high == 0)
4939 return 0;
4941 in_p = 1;
4943 else
4944 return 0;
4947 else if (subset)
4948 in_p = 0, low = low0, high = high0;
4949 else
4950 in_p = 0, low = low0, high = high1;
4953 *pin_p = in_p, *plow = low, *phigh = high;
4954 return 1;
4958 /* Subroutine of fold, looking inside expressions of the form
4959 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4960 of the COND_EXPR. This function is being used also to optimize
4961 A op B ? C : A, by reversing the comparison first.
4963 Return a folded expression whose code is not a COND_EXPR
4964 anymore, or NULL_TREE if no folding opportunity is found. */
4966 static tree
4967 fold_cond_expr_with_comparison (location_t loc, tree type,
4968 tree arg0, tree arg1, tree arg2)
4970 enum tree_code comp_code = TREE_CODE (arg0);
4971 tree arg00 = TREE_OPERAND (arg0, 0);
4972 tree arg01 = TREE_OPERAND (arg0, 1);
4973 tree arg1_type = TREE_TYPE (arg1);
4974 tree tem;
4976 STRIP_NOPS (arg1);
4977 STRIP_NOPS (arg2);
4979 /* If we have A op 0 ? A : -A, consider applying the following
4980 transformations:
4982 A == 0? A : -A same as -A
4983 A != 0? A : -A same as A
4984 A >= 0? A : -A same as abs (A)
4985 A > 0? A : -A same as abs (A)
4986 A <= 0? A : -A same as -abs (A)
4987 A < 0? A : -A same as -abs (A)
4989 None of these transformations work for modes with signed
4990 zeros. If A is +/-0, the first two transformations will
4991 change the sign of the result (from +0 to -0, or vice
4992 versa). The last four will fix the sign of the result,
4993 even though the original expressions could be positive or
4994 negative, depending on the sign of A.
4996 Note that all these transformations are correct if A is
4997 NaN, since the two alternatives (A and -A) are also NaNs. */
4998 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4999 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5000 ? real_zerop (arg01)
5001 : integer_zerop (arg01))
5002 && ((TREE_CODE (arg2) == NEGATE_EXPR
5003 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5004 /* In the case that A is of the form X-Y, '-A' (arg2) may
5005 have already been folded to Y-X, check for that. */
5006 || (TREE_CODE (arg1) == MINUS_EXPR
5007 && TREE_CODE (arg2) == MINUS_EXPR
5008 && operand_equal_p (TREE_OPERAND (arg1, 0),
5009 TREE_OPERAND (arg2, 1), 0)
5010 && operand_equal_p (TREE_OPERAND (arg1, 1),
5011 TREE_OPERAND (arg2, 0), 0))))
5012 switch (comp_code)
5014 case EQ_EXPR:
5015 case UNEQ_EXPR:
5016 tem = fold_convert_loc (loc, arg1_type, arg1);
5017 return pedantic_non_lvalue_loc (loc,
5018 fold_convert_loc (loc, type,
5019 negate_expr (tem)));
5020 case NE_EXPR:
5021 case LTGT_EXPR:
5022 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5023 case UNGE_EXPR:
5024 case UNGT_EXPR:
5025 if (flag_trapping_math)
5026 break;
5027 /* Fall through. */
5028 case GE_EXPR:
5029 case GT_EXPR:
5030 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5031 break;
5032 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5033 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5034 case UNLE_EXPR:
5035 case UNLT_EXPR:
5036 if (flag_trapping_math)
5037 break;
5038 case LE_EXPR:
5039 case LT_EXPR:
5040 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5041 break;
5042 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5043 return negate_expr (fold_convert_loc (loc, type, tem));
5044 default:
5045 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5046 break;
5049 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5050 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5051 both transformations are correct when A is NaN: A != 0
5052 is then true, and A == 0 is false. */
5054 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5055 && integer_zerop (arg01) && integer_zerop (arg2))
5057 if (comp_code == NE_EXPR)
5058 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5059 else if (comp_code == EQ_EXPR)
5060 return build_zero_cst (type);
5063 /* Try some transformations of A op B ? A : B.
5065 A == B? A : B same as B
5066 A != B? A : B same as A
5067 A >= B? A : B same as max (A, B)
5068 A > B? A : B same as max (B, A)
5069 A <= B? A : B same as min (A, B)
5070 A < B? A : B same as min (B, A)
5072 As above, these transformations don't work in the presence
5073 of signed zeros. For example, if A and B are zeros of
5074 opposite sign, the first two transformations will change
5075 the sign of the result. In the last four, the original
5076 expressions give different results for (A=+0, B=-0) and
5077 (A=-0, B=+0), but the transformed expressions do not.
5079 The first two transformations are correct if either A or B
5080 is a NaN. In the first transformation, the condition will
5081 be false, and B will indeed be chosen. In the case of the
5082 second transformation, the condition A != B will be true,
5083 and A will be chosen.
5085 The conversions to max() and min() are not correct if B is
5086 a number and A is not. The conditions in the original
5087 expressions will be false, so all four give B. The min()
5088 and max() versions would give a NaN instead. */
5089 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5090 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5091 /* Avoid these transformations if the COND_EXPR may be used
5092 as an lvalue in the C++ front-end. PR c++/19199. */
5093 && (in_gimple_form
5094 || VECTOR_TYPE_P (type)
5095 || (! lang_GNU_CXX ()
5096 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5097 || ! maybe_lvalue_p (arg1)
5098 || ! maybe_lvalue_p (arg2)))
5100 tree comp_op0 = arg00;
5101 tree comp_op1 = arg01;
5102 tree comp_type = TREE_TYPE (comp_op0);
5104 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5105 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5107 comp_type = type;
5108 comp_op0 = arg1;
5109 comp_op1 = arg2;
5112 switch (comp_code)
5114 case EQ_EXPR:
5115 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5116 case NE_EXPR:
5117 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5118 case LE_EXPR:
5119 case LT_EXPR:
5120 case UNLE_EXPR:
5121 case UNLT_EXPR:
5122 /* In C++ a ?: expression can be an lvalue, so put the
5123 operand which will be used if they are equal first
5124 so that we can convert this back to the
5125 corresponding COND_EXPR. */
5126 if (!HONOR_NANS (arg1))
5128 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5129 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5130 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5131 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5132 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5133 comp_op1, comp_op0);
5134 return pedantic_non_lvalue_loc (loc,
5135 fold_convert_loc (loc, type, tem));
5137 break;
5138 case GE_EXPR:
5139 case GT_EXPR:
5140 case UNGE_EXPR:
5141 case UNGT_EXPR:
5142 if (!HONOR_NANS (arg1))
5144 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5145 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5146 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5147 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5148 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5149 comp_op1, comp_op0);
5150 return pedantic_non_lvalue_loc (loc,
5151 fold_convert_loc (loc, type, tem));
5153 break;
5154 case UNEQ_EXPR:
5155 if (!HONOR_NANS (arg1))
5156 return pedantic_non_lvalue_loc (loc,
5157 fold_convert_loc (loc, type, arg2));
5158 break;
5159 case LTGT_EXPR:
5160 if (!HONOR_NANS (arg1))
5161 return pedantic_non_lvalue_loc (loc,
5162 fold_convert_loc (loc, type, arg1));
5163 break;
5164 default:
5165 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5166 break;
5170 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5171 we might still be able to simplify this. For example,
5172 if C1 is one less or one more than C2, this might have started
5173 out as a MIN or MAX and been transformed by this function.
5174 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5176 if (INTEGRAL_TYPE_P (type)
5177 && TREE_CODE (arg01) == INTEGER_CST
5178 && TREE_CODE (arg2) == INTEGER_CST)
5179 switch (comp_code)
5181 case EQ_EXPR:
5182 if (TREE_CODE (arg1) == INTEGER_CST)
5183 break;
5184 /* We can replace A with C1 in this case. */
5185 arg1 = fold_convert_loc (loc, type, arg01);
5186 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5188 case LT_EXPR:
5189 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5190 MIN_EXPR, to preserve the signedness of the comparison. */
5191 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5192 OEP_ONLY_CONST)
5193 && operand_equal_p (arg01,
5194 const_binop (PLUS_EXPR, arg2,
5195 build_int_cst (type, 1)),
5196 OEP_ONLY_CONST))
5198 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5199 fold_convert_loc (loc, TREE_TYPE (arg00),
5200 arg2));
5201 return pedantic_non_lvalue_loc (loc,
5202 fold_convert_loc (loc, type, tem));
5204 break;
5206 case LE_EXPR:
5207 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5208 as above. */
5209 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5210 OEP_ONLY_CONST)
5211 && operand_equal_p (arg01,
5212 const_binop (MINUS_EXPR, arg2,
5213 build_int_cst (type, 1)),
5214 OEP_ONLY_CONST))
5216 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5217 fold_convert_loc (loc, TREE_TYPE (arg00),
5218 arg2));
5219 return pedantic_non_lvalue_loc (loc,
5220 fold_convert_loc (loc, type, tem));
5222 break;
5224 case GT_EXPR:
5225 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5226 MAX_EXPR, to preserve the signedness of the comparison. */
5227 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5228 OEP_ONLY_CONST)
5229 && operand_equal_p (arg01,
5230 const_binop (MINUS_EXPR, arg2,
5231 build_int_cst (type, 1)),
5232 OEP_ONLY_CONST))
5234 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5235 fold_convert_loc (loc, TREE_TYPE (arg00),
5236 arg2));
5237 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5239 break;
5241 case GE_EXPR:
5242 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5243 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5244 OEP_ONLY_CONST)
5245 && operand_equal_p (arg01,
5246 const_binop (PLUS_EXPR, arg2,
5247 build_int_cst (type, 1)),
5248 OEP_ONLY_CONST))
5250 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5251 fold_convert_loc (loc, TREE_TYPE (arg00),
5252 arg2));
5253 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5255 break;
5256 case NE_EXPR:
5257 break;
5258 default:
5259 gcc_unreachable ();
5262 return NULL_TREE;
5267 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5268 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5269 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5270 false) >= 2)
5271 #endif
5273 /* EXP is some logical combination of boolean tests. See if we can
5274 merge it into some range test. Return the new tree if so. */
5276 static tree
5277 fold_range_test (location_t loc, enum tree_code code, tree type,
5278 tree op0, tree op1)
5280 int or_op = (code == TRUTH_ORIF_EXPR
5281 || code == TRUTH_OR_EXPR);
5282 int in0_p, in1_p, in_p;
5283 tree low0, low1, low, high0, high1, high;
5284 bool strict_overflow_p = false;
5285 tree tem, lhs, rhs;
5286 const char * const warnmsg = G_("assuming signed overflow does not occur "
5287 "when simplifying range test");
5289 if (!INTEGRAL_TYPE_P (type))
5290 return 0;
5292 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5293 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5295 /* If this is an OR operation, invert both sides; we will invert
5296 again at the end. */
5297 if (or_op)
5298 in0_p = ! in0_p, in1_p = ! in1_p;
5300 /* If both expressions are the same, if we can merge the ranges, and we
5301 can build the range test, return it or it inverted. If one of the
5302 ranges is always true or always false, consider it to be the same
5303 expression as the other. */
5304 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5305 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5306 in1_p, low1, high1)
5307 && 0 != (tem = (build_range_check (loc, type,
5308 lhs != 0 ? lhs
5309 : rhs != 0 ? rhs : integer_zero_node,
5310 in_p, low, high))))
5312 if (strict_overflow_p)
5313 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5314 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5317 /* On machines where the branch cost is expensive, if this is a
5318 short-circuited branch and the underlying object on both sides
5319 is the same, make a non-short-circuit operation. */
5320 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5321 && lhs != 0 && rhs != 0
5322 && (code == TRUTH_ANDIF_EXPR
5323 || code == TRUTH_ORIF_EXPR)
5324 && operand_equal_p (lhs, rhs, 0))
5326 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5327 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5328 which cases we can't do this. */
5329 if (simple_operand_p (lhs))
5330 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5331 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5332 type, op0, op1);
5334 else if (!lang_hooks.decls.global_bindings_p ()
5335 && !CONTAINS_PLACEHOLDER_P (lhs))
5337 tree common = save_expr (lhs);
5339 if (0 != (lhs = build_range_check (loc, type, common,
5340 or_op ? ! in0_p : in0_p,
5341 low0, high0))
5342 && (0 != (rhs = build_range_check (loc, type, common,
5343 or_op ? ! in1_p : in1_p,
5344 low1, high1))))
5346 if (strict_overflow_p)
5347 fold_overflow_warning (warnmsg,
5348 WARN_STRICT_OVERFLOW_COMPARISON);
5349 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5350 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5351 type, lhs, rhs);
5356 return 0;
5359 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5360 bit value. Arrange things so the extra bits will be set to zero if and
5361 only if C is signed-extended to its full width. If MASK is nonzero,
5362 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5364 static tree
5365 unextend (tree c, int p, int unsignedp, tree mask)
5367 tree type = TREE_TYPE (c);
5368 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5369 tree temp;
5371 if (p == modesize || unsignedp)
5372 return c;
5374 /* We work by getting just the sign bit into the low-order bit, then
5375 into the high-order bit, then sign-extend. We then XOR that value
5376 with C. */
5377 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5379 /* We must use a signed type in order to get an arithmetic right shift.
5380 However, we must also avoid introducing accidental overflows, so that
5381 a subsequent call to integer_zerop will work. Hence we must
5382 do the type conversion here. At this point, the constant is either
5383 zero or one, and the conversion to a signed type can never overflow.
5384 We could get an overflow if this conversion is done anywhere else. */
5385 if (TYPE_UNSIGNED (type))
5386 temp = fold_convert (signed_type_for (type), temp);
5388 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5389 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5390 if (mask != 0)
5391 temp = const_binop (BIT_AND_EXPR, temp,
5392 fold_convert (TREE_TYPE (c), mask));
5393 /* If necessary, convert the type back to match the type of C. */
5394 if (TYPE_UNSIGNED (type))
5395 temp = fold_convert (type, temp);
5397 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5400 /* For an expression that has the form
5401 (A && B) || ~B
5403 (A || B) && ~B,
5404 we can drop one of the inner expressions and simplify to
5405 A || ~B
5407 A && ~B
5408 LOC is the location of the resulting expression. OP is the inner
5409 logical operation; the left-hand side in the examples above, while CMPOP
5410 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5411 removing a condition that guards another, as in
5412 (A != NULL && A->...) || A == NULL
5413 which we must not transform. If RHS_ONLY is true, only eliminate the
5414 right-most operand of the inner logical operation. */
5416 static tree
5417 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5418 bool rhs_only)
5420 tree type = TREE_TYPE (cmpop);
5421 enum tree_code code = TREE_CODE (cmpop);
5422 enum tree_code truthop_code = TREE_CODE (op);
5423 tree lhs = TREE_OPERAND (op, 0);
5424 tree rhs = TREE_OPERAND (op, 1);
5425 tree orig_lhs = lhs, orig_rhs = rhs;
5426 enum tree_code rhs_code = TREE_CODE (rhs);
5427 enum tree_code lhs_code = TREE_CODE (lhs);
5428 enum tree_code inv_code;
5430 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5431 return NULL_TREE;
5433 if (TREE_CODE_CLASS (code) != tcc_comparison)
5434 return NULL_TREE;
5436 if (rhs_code == truthop_code)
5438 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5439 if (newrhs != NULL_TREE)
5441 rhs = newrhs;
5442 rhs_code = TREE_CODE (rhs);
5445 if (lhs_code == truthop_code && !rhs_only)
5447 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5448 if (newlhs != NULL_TREE)
5450 lhs = newlhs;
5451 lhs_code = TREE_CODE (lhs);
5455 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5456 if (inv_code == rhs_code
5457 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5458 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5459 return lhs;
5460 if (!rhs_only && inv_code == lhs_code
5461 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5462 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5463 return rhs;
5464 if (rhs != orig_rhs || lhs != orig_lhs)
5465 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5466 lhs, rhs);
5467 return NULL_TREE;
5470 /* Find ways of folding logical expressions of LHS and RHS:
5471 Try to merge two comparisons to the same innermost item.
5472 Look for range tests like "ch >= '0' && ch <= '9'".
5473 Look for combinations of simple terms on machines with expensive branches
5474 and evaluate the RHS unconditionally.
5476 For example, if we have p->a == 2 && p->b == 4 and we can make an
5477 object large enough to span both A and B, we can do this with a comparison
5478 against the object ANDed with the a mask.
5480 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5481 operations to do this with one comparison.
5483 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5484 function and the one above.
5486 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5487 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5489 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5490 two operands.
5492 We return the simplified tree or 0 if no optimization is possible. */
5494 static tree
5495 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5496 tree lhs, tree rhs)
5498 /* If this is the "or" of two comparisons, we can do something if
5499 the comparisons are NE_EXPR. If this is the "and", we can do something
5500 if the comparisons are EQ_EXPR. I.e.,
5501 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5503 WANTED_CODE is this operation code. For single bit fields, we can
5504 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5505 comparison for one-bit fields. */
5507 enum tree_code wanted_code;
5508 enum tree_code lcode, rcode;
5509 tree ll_arg, lr_arg, rl_arg, rr_arg;
5510 tree ll_inner, lr_inner, rl_inner, rr_inner;
5511 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5512 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5513 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5514 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5515 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5516 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5517 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5518 machine_mode lnmode, rnmode;
5519 tree ll_mask, lr_mask, rl_mask, rr_mask;
5520 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5521 tree l_const, r_const;
5522 tree lntype, rntype, result;
5523 HOST_WIDE_INT first_bit, end_bit;
5524 int volatilep;
5526 /* Start by getting the comparison codes. Fail if anything is volatile.
5527 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5528 it were surrounded with a NE_EXPR. */
5530 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5531 return 0;
5533 lcode = TREE_CODE (lhs);
5534 rcode = TREE_CODE (rhs);
5536 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5538 lhs = build2 (NE_EXPR, truth_type, lhs,
5539 build_int_cst (TREE_TYPE (lhs), 0));
5540 lcode = NE_EXPR;
5543 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5545 rhs = build2 (NE_EXPR, truth_type, rhs,
5546 build_int_cst (TREE_TYPE (rhs), 0));
5547 rcode = NE_EXPR;
5550 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5551 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5552 return 0;
5554 ll_arg = TREE_OPERAND (lhs, 0);
5555 lr_arg = TREE_OPERAND (lhs, 1);
5556 rl_arg = TREE_OPERAND (rhs, 0);
5557 rr_arg = TREE_OPERAND (rhs, 1);
5559 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5560 if (simple_operand_p (ll_arg)
5561 && simple_operand_p (lr_arg))
5563 if (operand_equal_p (ll_arg, rl_arg, 0)
5564 && operand_equal_p (lr_arg, rr_arg, 0))
5566 result = combine_comparisons (loc, code, lcode, rcode,
5567 truth_type, ll_arg, lr_arg);
5568 if (result)
5569 return result;
5571 else if (operand_equal_p (ll_arg, rr_arg, 0)
5572 && operand_equal_p (lr_arg, rl_arg, 0))
5574 result = combine_comparisons (loc, code, lcode,
5575 swap_tree_comparison (rcode),
5576 truth_type, ll_arg, lr_arg);
5577 if (result)
5578 return result;
5582 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5583 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5585 /* If the RHS can be evaluated unconditionally and its operands are
5586 simple, it wins to evaluate the RHS unconditionally on machines
5587 with expensive branches. In this case, this isn't a comparison
5588 that can be merged. */
5590 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5591 false) >= 2
5592 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5593 && simple_operand_p (rl_arg)
5594 && simple_operand_p (rr_arg))
5596 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5597 if (code == TRUTH_OR_EXPR
5598 && lcode == NE_EXPR && integer_zerop (lr_arg)
5599 && rcode == NE_EXPR && integer_zerop (rr_arg)
5600 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5601 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5602 return build2_loc (loc, NE_EXPR, truth_type,
5603 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5604 ll_arg, rl_arg),
5605 build_int_cst (TREE_TYPE (ll_arg), 0));
5607 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5608 if (code == TRUTH_AND_EXPR
5609 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5610 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5611 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5612 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5613 return build2_loc (loc, EQ_EXPR, truth_type,
5614 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5615 ll_arg, rl_arg),
5616 build_int_cst (TREE_TYPE (ll_arg), 0));
5619 /* See if the comparisons can be merged. Then get all the parameters for
5620 each side. */
5622 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5623 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5624 return 0;
5626 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5627 volatilep = 0;
5628 ll_inner = decode_field_reference (loc, ll_arg,
5629 &ll_bitsize, &ll_bitpos, &ll_mode,
5630 &ll_unsignedp, &ll_reversep, &volatilep,
5631 &ll_mask, &ll_and_mask);
5632 lr_inner = decode_field_reference (loc, lr_arg,
5633 &lr_bitsize, &lr_bitpos, &lr_mode,
5634 &lr_unsignedp, &lr_reversep, &volatilep,
5635 &lr_mask, &lr_and_mask);
5636 rl_inner = decode_field_reference (loc, rl_arg,
5637 &rl_bitsize, &rl_bitpos, &rl_mode,
5638 &rl_unsignedp, &rl_reversep, &volatilep,
5639 &rl_mask, &rl_and_mask);
5640 rr_inner = decode_field_reference (loc, rr_arg,
5641 &rr_bitsize, &rr_bitpos, &rr_mode,
5642 &rr_unsignedp, &rr_reversep, &volatilep,
5643 &rr_mask, &rr_and_mask);
5645 /* It must be true that the inner operation on the lhs of each
5646 comparison must be the same if we are to be able to do anything.
5647 Then see if we have constants. If not, the same must be true for
5648 the rhs's. */
5649 if (volatilep
5650 || ll_reversep != rl_reversep
5651 || ll_inner == 0 || rl_inner == 0
5652 || ! operand_equal_p (ll_inner, rl_inner, 0))
5653 return 0;
5655 if (TREE_CODE (lr_arg) == INTEGER_CST
5656 && TREE_CODE (rr_arg) == INTEGER_CST)
5658 l_const = lr_arg, r_const = rr_arg;
5659 lr_reversep = ll_reversep;
5661 else if (lr_reversep != rr_reversep
5662 || lr_inner == 0 || rr_inner == 0
5663 || ! operand_equal_p (lr_inner, rr_inner, 0))
5664 return 0;
5665 else
5666 l_const = r_const = 0;
5668 /* If either comparison code is not correct for our logical operation,
5669 fail. However, we can convert a one-bit comparison against zero into
5670 the opposite comparison against that bit being set in the field. */
5672 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5673 if (lcode != wanted_code)
5675 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5677 /* Make the left operand unsigned, since we are only interested
5678 in the value of one bit. Otherwise we are doing the wrong
5679 thing below. */
5680 ll_unsignedp = 1;
5681 l_const = ll_mask;
5683 else
5684 return 0;
5687 /* This is analogous to the code for l_const above. */
5688 if (rcode != wanted_code)
5690 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5692 rl_unsignedp = 1;
5693 r_const = rl_mask;
5695 else
5696 return 0;
5699 /* See if we can find a mode that contains both fields being compared on
5700 the left. If we can't, fail. Otherwise, update all constants and masks
5701 to be relative to a field of that size. */
5702 first_bit = MIN (ll_bitpos, rl_bitpos);
5703 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5704 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5705 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5706 volatilep);
5707 if (lnmode == VOIDmode)
5708 return 0;
5710 lnbitsize = GET_MODE_BITSIZE (lnmode);
5711 lnbitpos = first_bit & ~ (lnbitsize - 1);
5712 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5713 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5715 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5717 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5718 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5721 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5722 size_int (xll_bitpos));
5723 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5724 size_int (xrl_bitpos));
5726 if (l_const)
5728 l_const = fold_convert_loc (loc, lntype, l_const);
5729 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5730 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5731 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5732 fold_build1_loc (loc, BIT_NOT_EXPR,
5733 lntype, ll_mask))))
5735 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5737 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5740 if (r_const)
5742 r_const = fold_convert_loc (loc, lntype, r_const);
5743 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5744 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5745 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5746 fold_build1_loc (loc, BIT_NOT_EXPR,
5747 lntype, rl_mask))))
5749 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5751 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5755 /* If the right sides are not constant, do the same for it. Also,
5756 disallow this optimization if a size or signedness mismatch occurs
5757 between the left and right sides. */
5758 if (l_const == 0)
5760 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5761 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5762 /* Make sure the two fields on the right
5763 correspond to the left without being swapped. */
5764 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5765 return 0;
5767 first_bit = MIN (lr_bitpos, rr_bitpos);
5768 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5769 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5770 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5771 volatilep);
5772 if (rnmode == VOIDmode)
5773 return 0;
5775 rnbitsize = GET_MODE_BITSIZE (rnmode);
5776 rnbitpos = first_bit & ~ (rnbitsize - 1);
5777 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5778 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5780 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5782 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5783 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5786 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5787 rntype, lr_mask),
5788 size_int (xlr_bitpos));
5789 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5790 rntype, rr_mask),
5791 size_int (xrr_bitpos));
5793 /* Make a mask that corresponds to both fields being compared.
5794 Do this for both items being compared. If the operands are the
5795 same size and the bits being compared are in the same position
5796 then we can do this by masking both and comparing the masked
5797 results. */
5798 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5799 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5800 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5802 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5803 ll_unsignedp || rl_unsignedp, ll_reversep);
5804 if (! all_ones_mask_p (ll_mask, lnbitsize))
5805 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5807 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5808 lr_unsignedp || rr_unsignedp, lr_reversep);
5809 if (! all_ones_mask_p (lr_mask, rnbitsize))
5810 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5812 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5815 /* There is still another way we can do something: If both pairs of
5816 fields being compared are adjacent, we may be able to make a wider
5817 field containing them both.
5819 Note that we still must mask the lhs/rhs expressions. Furthermore,
5820 the mask must be shifted to account for the shift done by
5821 make_bit_field_ref. */
5822 if ((ll_bitsize + ll_bitpos == rl_bitpos
5823 && lr_bitsize + lr_bitpos == rr_bitpos)
5824 || (ll_bitpos == rl_bitpos + rl_bitsize
5825 && lr_bitpos == rr_bitpos + rr_bitsize))
5827 tree type;
5829 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5830 ll_bitsize + rl_bitsize,
5831 MIN (ll_bitpos, rl_bitpos),
5832 ll_unsignedp, ll_reversep);
5833 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5834 lr_bitsize + rr_bitsize,
5835 MIN (lr_bitpos, rr_bitpos),
5836 lr_unsignedp, lr_reversep);
5838 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5839 size_int (MIN (xll_bitpos, xrl_bitpos)));
5840 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5841 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5843 /* Convert to the smaller type before masking out unwanted bits. */
5844 type = lntype;
5845 if (lntype != rntype)
5847 if (lnbitsize > rnbitsize)
5849 lhs = fold_convert_loc (loc, rntype, lhs);
5850 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5851 type = rntype;
5853 else if (lnbitsize < rnbitsize)
5855 rhs = fold_convert_loc (loc, lntype, rhs);
5856 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5857 type = lntype;
5861 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5862 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5864 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5865 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5867 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5870 return 0;
5873 /* Handle the case of comparisons with constants. If there is something in
5874 common between the masks, those bits of the constants must be the same.
5875 If not, the condition is always false. Test for this to avoid generating
5876 incorrect code below. */
5877 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5878 if (! integer_zerop (result)
5879 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5880 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5882 if (wanted_code == NE_EXPR)
5884 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5885 return constant_boolean_node (true, truth_type);
5887 else
5889 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5890 return constant_boolean_node (false, truth_type);
5894 /* Construct the expression we will return. First get the component
5895 reference we will make. Unless the mask is all ones the width of
5896 that field, perform the mask operation. Then compare with the
5897 merged constant. */
5898 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5899 ll_unsignedp || rl_unsignedp, ll_reversep);
5901 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5902 if (! all_ones_mask_p (ll_mask, lnbitsize))
5903 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5905 return build2_loc (loc, wanted_code, truth_type, result,
5906 const_binop (BIT_IOR_EXPR, l_const, r_const));
5909 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5910 constant. */
5912 static tree
5913 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5914 tree op0, tree op1)
5916 tree arg0 = op0;
5917 enum tree_code op_code;
5918 tree comp_const;
5919 tree minmax_const;
5920 int consts_equal, consts_lt;
5921 tree inner;
5923 STRIP_SIGN_NOPS (arg0);
5925 op_code = TREE_CODE (arg0);
5926 minmax_const = TREE_OPERAND (arg0, 1);
5927 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5928 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5929 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5930 inner = TREE_OPERAND (arg0, 0);
5932 /* If something does not permit us to optimize, return the original tree. */
5933 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5934 || TREE_CODE (comp_const) != INTEGER_CST
5935 || TREE_OVERFLOW (comp_const)
5936 || TREE_CODE (minmax_const) != INTEGER_CST
5937 || TREE_OVERFLOW (minmax_const))
5938 return NULL_TREE;
5940 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5941 and GT_EXPR, doing the rest with recursive calls using logical
5942 simplifications. */
5943 switch (code)
5945 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5947 tree tem
5948 = optimize_minmax_comparison (loc,
5949 invert_tree_comparison (code, false),
5950 type, op0, op1);
5951 if (tem)
5952 return invert_truthvalue_loc (loc, tem);
5953 return NULL_TREE;
5956 case GE_EXPR:
5957 return
5958 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5959 optimize_minmax_comparison
5960 (loc, EQ_EXPR, type, arg0, comp_const),
5961 optimize_minmax_comparison
5962 (loc, GT_EXPR, type, arg0, comp_const));
5964 case EQ_EXPR:
5965 if (op_code == MAX_EXPR && consts_equal)
5966 /* MAX (X, 0) == 0 -> X <= 0 */
5967 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5969 else if (op_code == MAX_EXPR && consts_lt)
5970 /* MAX (X, 0) == 5 -> X == 5 */
5971 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5973 else if (op_code == MAX_EXPR)
5974 /* MAX (X, 0) == -1 -> false */
5975 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5977 else if (consts_equal)
5978 /* MIN (X, 0) == 0 -> X >= 0 */
5979 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5981 else if (consts_lt)
5982 /* MIN (X, 0) == 5 -> false */
5983 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5985 else
5986 /* MIN (X, 0) == -1 -> X == -1 */
5987 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5989 case GT_EXPR:
5990 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5991 /* MAX (X, 0) > 0 -> X > 0
5992 MAX (X, 0) > 5 -> X > 5 */
5993 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5995 else if (op_code == MAX_EXPR)
5996 /* MAX (X, 0) > -1 -> true */
5997 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5999 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6000 /* MIN (X, 0) > 0 -> false
6001 MIN (X, 0) > 5 -> false */
6002 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6004 else
6005 /* MIN (X, 0) > -1 -> X > -1 */
6006 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6008 default:
6009 return NULL_TREE;
6013 /* T is an integer expression that is being multiplied, divided, or taken a
6014 modulus (CODE says which and what kind of divide or modulus) by a
6015 constant C. See if we can eliminate that operation by folding it with
6016 other operations already in T. WIDE_TYPE, if non-null, is a type that
6017 should be used for the computation if wider than our type.
6019 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6020 (X * 2) + (Y * 4). We must, however, be assured that either the original
6021 expression would not overflow or that overflow is undefined for the type
6022 in the language in question.
6024 If we return a non-null expression, it is an equivalent form of the
6025 original computation, but need not be in the original type.
6027 We set *STRICT_OVERFLOW_P to true if the return values depends on
6028 signed overflow being undefined. Otherwise we do not change
6029 *STRICT_OVERFLOW_P. */
6031 static tree
6032 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6033 bool *strict_overflow_p)
6035 /* To avoid exponential search depth, refuse to allow recursion past
6036 three levels. Beyond that (1) it's highly unlikely that we'll find
6037 something interesting and (2) we've probably processed it before
6038 when we built the inner expression. */
6040 static int depth;
6041 tree ret;
6043 if (depth > 3)
6044 return NULL;
6046 depth++;
6047 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6048 depth--;
6050 return ret;
6053 static tree
6054 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6055 bool *strict_overflow_p)
6057 tree type = TREE_TYPE (t);
6058 enum tree_code tcode = TREE_CODE (t);
6059 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6060 > GET_MODE_SIZE (TYPE_MODE (type)))
6061 ? wide_type : type);
6062 tree t1, t2;
6063 int same_p = tcode == code;
6064 tree op0 = NULL_TREE, op1 = NULL_TREE;
6065 bool sub_strict_overflow_p;
6067 /* Don't deal with constants of zero here; they confuse the code below. */
6068 if (integer_zerop (c))
6069 return NULL_TREE;
6071 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6072 op0 = TREE_OPERAND (t, 0);
6074 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6075 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6077 /* Note that we need not handle conditional operations here since fold
6078 already handles those cases. So just do arithmetic here. */
6079 switch (tcode)
6081 case INTEGER_CST:
6082 /* For a constant, we can always simplify if we are a multiply
6083 or (for divide and modulus) if it is a multiple of our constant. */
6084 if (code == MULT_EXPR
6085 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6087 tree tem = const_binop (code, fold_convert (ctype, t),
6088 fold_convert (ctype, c));
6089 /* If the multiplication overflowed to INT_MIN then we lost sign
6090 information on it and a subsequent multiplication might
6091 spuriously overflow. See PR68142. */
6092 if (TREE_OVERFLOW (tem)
6093 && wi::eq_p (tem, wi::min_value (TYPE_PRECISION (ctype), SIGNED)))
6094 return NULL_TREE;
6095 return tem;
6097 break;
6099 CASE_CONVERT: case NON_LVALUE_EXPR:
6100 /* If op0 is an expression ... */
6101 if ((COMPARISON_CLASS_P (op0)
6102 || UNARY_CLASS_P (op0)
6103 || BINARY_CLASS_P (op0)
6104 || VL_EXP_CLASS_P (op0)
6105 || EXPRESSION_CLASS_P (op0))
6106 /* ... and has wrapping overflow, and its type is smaller
6107 than ctype, then we cannot pass through as widening. */
6108 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6109 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6110 && (TYPE_PRECISION (ctype)
6111 > TYPE_PRECISION (TREE_TYPE (op0))))
6112 /* ... or this is a truncation (t is narrower than op0),
6113 then we cannot pass through this narrowing. */
6114 || (TYPE_PRECISION (type)
6115 < TYPE_PRECISION (TREE_TYPE (op0)))
6116 /* ... or signedness changes for division or modulus,
6117 then we cannot pass through this conversion. */
6118 || (code != MULT_EXPR
6119 && (TYPE_UNSIGNED (ctype)
6120 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6121 /* ... or has undefined overflow while the converted to
6122 type has not, we cannot do the operation in the inner type
6123 as that would introduce undefined overflow. */
6124 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6125 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6126 && !TYPE_OVERFLOW_UNDEFINED (type))))
6127 break;
6129 /* Pass the constant down and see if we can make a simplification. If
6130 we can, replace this expression with the inner simplification for
6131 possible later conversion to our or some other type. */
6132 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6133 && TREE_CODE (t2) == INTEGER_CST
6134 && !TREE_OVERFLOW (t2)
6135 && (0 != (t1 = extract_muldiv (op0, t2, code,
6136 code == MULT_EXPR
6137 ? ctype : NULL_TREE,
6138 strict_overflow_p))))
6139 return t1;
6140 break;
6142 case ABS_EXPR:
6143 /* If widening the type changes it from signed to unsigned, then we
6144 must avoid building ABS_EXPR itself as unsigned. */
6145 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6147 tree cstype = (*signed_type_for) (ctype);
6148 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6149 != 0)
6151 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6152 return fold_convert (ctype, t1);
6154 break;
6156 /* If the constant is negative, we cannot simplify this. */
6157 if (tree_int_cst_sgn (c) == -1)
6158 break;
6159 /* FALLTHROUGH */
6160 case NEGATE_EXPR:
6161 /* For division and modulus, type can't be unsigned, as e.g.
6162 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6163 For signed types, even with wrapping overflow, this is fine. */
6164 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6165 break;
6166 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6167 != 0)
6168 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6169 break;
6171 case MIN_EXPR: case MAX_EXPR:
6172 /* If widening the type changes the signedness, then we can't perform
6173 this optimization as that changes the result. */
6174 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6175 break;
6177 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6178 sub_strict_overflow_p = false;
6179 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6180 &sub_strict_overflow_p)) != 0
6181 && (t2 = extract_muldiv (op1, c, code, wide_type,
6182 &sub_strict_overflow_p)) != 0)
6184 if (tree_int_cst_sgn (c) < 0)
6185 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6186 if (sub_strict_overflow_p)
6187 *strict_overflow_p = true;
6188 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6189 fold_convert (ctype, t2));
6191 break;
6193 case LSHIFT_EXPR: case RSHIFT_EXPR:
6194 /* If the second operand is constant, this is a multiplication
6195 or floor division, by a power of two, so we can treat it that
6196 way unless the multiplier or divisor overflows. Signed
6197 left-shift overflow is implementation-defined rather than
6198 undefined in C90, so do not convert signed left shift into
6199 multiplication. */
6200 if (TREE_CODE (op1) == INTEGER_CST
6201 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6202 /* const_binop may not detect overflow correctly,
6203 so check for it explicitly here. */
6204 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6205 && 0 != (t1 = fold_convert (ctype,
6206 const_binop (LSHIFT_EXPR,
6207 size_one_node,
6208 op1)))
6209 && !TREE_OVERFLOW (t1))
6210 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6211 ? MULT_EXPR : FLOOR_DIV_EXPR,
6212 ctype,
6213 fold_convert (ctype, op0),
6214 t1),
6215 c, code, wide_type, strict_overflow_p);
6216 break;
6218 case PLUS_EXPR: case MINUS_EXPR:
6219 /* See if we can eliminate the operation on both sides. If we can, we
6220 can return a new PLUS or MINUS. If we can't, the only remaining
6221 cases where we can do anything are if the second operand is a
6222 constant. */
6223 sub_strict_overflow_p = false;
6224 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6225 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6226 if (t1 != 0 && t2 != 0
6227 && (code == MULT_EXPR
6228 /* If not multiplication, we can only do this if both operands
6229 are divisible by c. */
6230 || (multiple_of_p (ctype, op0, c)
6231 && multiple_of_p (ctype, op1, c))))
6233 if (sub_strict_overflow_p)
6234 *strict_overflow_p = true;
6235 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6236 fold_convert (ctype, t2));
6239 /* If this was a subtraction, negate OP1 and set it to be an addition.
6240 This simplifies the logic below. */
6241 if (tcode == MINUS_EXPR)
6243 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6244 /* If OP1 was not easily negatable, the constant may be OP0. */
6245 if (TREE_CODE (op0) == INTEGER_CST)
6247 std::swap (op0, op1);
6248 std::swap (t1, t2);
6252 if (TREE_CODE (op1) != INTEGER_CST)
6253 break;
6255 /* If either OP1 or C are negative, this optimization is not safe for
6256 some of the division and remainder types while for others we need
6257 to change the code. */
6258 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6260 if (code == CEIL_DIV_EXPR)
6261 code = FLOOR_DIV_EXPR;
6262 else if (code == FLOOR_DIV_EXPR)
6263 code = CEIL_DIV_EXPR;
6264 else if (code != MULT_EXPR
6265 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6266 break;
6269 /* If it's a multiply or a division/modulus operation of a multiple
6270 of our constant, do the operation and verify it doesn't overflow. */
6271 if (code == MULT_EXPR
6272 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6274 op1 = const_binop (code, fold_convert (ctype, op1),
6275 fold_convert (ctype, c));
6276 /* We allow the constant to overflow with wrapping semantics. */
6277 if (op1 == 0
6278 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6279 break;
6281 else
6282 break;
6284 /* If we have an unsigned type, we cannot widen the operation since it
6285 will change the result if the original computation overflowed. */
6286 if (TYPE_UNSIGNED (ctype) && ctype != type)
6287 break;
6289 /* If we were able to eliminate our operation from the first side,
6290 apply our operation to the second side and reform the PLUS. */
6291 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6292 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6294 /* The last case is if we are a multiply. In that case, we can
6295 apply the distributive law to commute the multiply and addition
6296 if the multiplication of the constants doesn't overflow
6297 and overflow is defined. With undefined overflow
6298 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6299 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6300 return fold_build2 (tcode, ctype,
6301 fold_build2 (code, ctype,
6302 fold_convert (ctype, op0),
6303 fold_convert (ctype, c)),
6304 op1);
6306 break;
6308 case MULT_EXPR:
6309 /* We have a special case here if we are doing something like
6310 (C * 8) % 4 since we know that's zero. */
6311 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6312 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6313 /* If the multiplication can overflow we cannot optimize this. */
6314 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6315 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6316 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6318 *strict_overflow_p = true;
6319 return omit_one_operand (type, integer_zero_node, op0);
6322 /* ... fall through ... */
6324 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6325 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6326 /* If we can extract our operation from the LHS, do so and return a
6327 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6328 do something only if the second operand is a constant. */
6329 if (same_p
6330 && (t1 = extract_muldiv (op0, c, code, wide_type,
6331 strict_overflow_p)) != 0)
6332 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6333 fold_convert (ctype, op1));
6334 else if (tcode == MULT_EXPR && code == MULT_EXPR
6335 && (t1 = extract_muldiv (op1, c, code, wide_type,
6336 strict_overflow_p)) != 0)
6337 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6338 fold_convert (ctype, t1));
6339 else if (TREE_CODE (op1) != INTEGER_CST)
6340 return 0;
6342 /* If these are the same operation types, we can associate them
6343 assuming no overflow. */
6344 if (tcode == code)
6346 bool overflow_p = false;
6347 bool overflow_mul_p;
6348 signop sign = TYPE_SIGN (ctype);
6349 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6350 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6351 if (overflow_mul_p
6352 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6353 overflow_p = true;
6354 if (!overflow_p)
6356 mul = wide_int::from (mul, TYPE_PRECISION (ctype),
6357 TYPE_SIGN (TREE_TYPE (op1)));
6358 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6359 wide_int_to_tree (ctype, mul));
6363 /* If these operations "cancel" each other, we have the main
6364 optimizations of this pass, which occur when either constant is a
6365 multiple of the other, in which case we replace this with either an
6366 operation or CODE or TCODE.
6368 If we have an unsigned type, we cannot do this since it will change
6369 the result if the original computation overflowed. */
6370 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6371 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6372 || (tcode == MULT_EXPR
6373 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6374 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6375 && code != MULT_EXPR)))
6377 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6379 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6380 *strict_overflow_p = true;
6381 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6382 fold_convert (ctype,
6383 const_binop (TRUNC_DIV_EXPR,
6384 op1, c)));
6386 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6388 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6389 *strict_overflow_p = true;
6390 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6391 fold_convert (ctype,
6392 const_binop (TRUNC_DIV_EXPR,
6393 c, op1)));
6396 break;
6398 default:
6399 break;
6402 return 0;
6405 /* Return a node which has the indicated constant VALUE (either 0 or
6406 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6407 and is of the indicated TYPE. */
6409 tree
6410 constant_boolean_node (bool value, tree type)
6412 if (type == integer_type_node)
6413 return value ? integer_one_node : integer_zero_node;
6414 else if (type == boolean_type_node)
6415 return value ? boolean_true_node : boolean_false_node;
6416 else if (TREE_CODE (type) == VECTOR_TYPE)
6417 return build_vector_from_val (type,
6418 build_int_cst (TREE_TYPE (type),
6419 value ? -1 : 0));
6420 else
6421 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6425 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6426 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6427 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6428 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6429 COND is the first argument to CODE; otherwise (as in the example
6430 given here), it is the second argument. TYPE is the type of the
6431 original expression. Return NULL_TREE if no simplification is
6432 possible. */
6434 static tree
6435 fold_binary_op_with_conditional_arg (location_t loc,
6436 enum tree_code code,
6437 tree type, tree op0, tree op1,
6438 tree cond, tree arg, int cond_first_p)
6440 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6441 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6442 tree test, true_value, false_value;
6443 tree lhs = NULL_TREE;
6444 tree rhs = NULL_TREE;
6445 enum tree_code cond_code = COND_EXPR;
6447 if (TREE_CODE (cond) == COND_EXPR
6448 || TREE_CODE (cond) == VEC_COND_EXPR)
6450 test = TREE_OPERAND (cond, 0);
6451 true_value = TREE_OPERAND (cond, 1);
6452 false_value = TREE_OPERAND (cond, 2);
6453 /* If this operand throws an expression, then it does not make
6454 sense to try to perform a logical or arithmetic operation
6455 involving it. */
6456 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6457 lhs = true_value;
6458 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6459 rhs = false_value;
6461 else if (!(TREE_CODE (type) != VECTOR_TYPE
6462 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6464 tree testtype = TREE_TYPE (cond);
6465 test = cond;
6466 true_value = constant_boolean_node (true, testtype);
6467 false_value = constant_boolean_node (false, testtype);
6469 else
6470 /* Detect the case of mixing vector and scalar types - bail out. */
6471 return NULL_TREE;
6473 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6474 cond_code = VEC_COND_EXPR;
6476 /* This transformation is only worthwhile if we don't have to wrap ARG
6477 in a SAVE_EXPR and the operation can be simplified without recursing
6478 on at least one of the branches once its pushed inside the COND_EXPR. */
6479 if (!TREE_CONSTANT (arg)
6480 && (TREE_SIDE_EFFECTS (arg)
6481 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6482 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6483 return NULL_TREE;
6485 arg = fold_convert_loc (loc, arg_type, arg);
6486 if (lhs == 0)
6488 true_value = fold_convert_loc (loc, cond_type, true_value);
6489 if (cond_first_p)
6490 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6491 else
6492 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6494 if (rhs == 0)
6496 false_value = fold_convert_loc (loc, cond_type, false_value);
6497 if (cond_first_p)
6498 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6499 else
6500 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6503 /* Check that we have simplified at least one of the branches. */
6504 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6505 return NULL_TREE;
6507 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6511 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6513 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6514 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6515 ADDEND is the same as X.
6517 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6518 and finite. The problematic cases are when X is zero, and its mode
6519 has signed zeros. In the case of rounding towards -infinity,
6520 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6521 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6523 bool
6524 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6526 if (!real_zerop (addend))
6527 return false;
6529 /* Don't allow the fold with -fsignaling-nans. */
6530 if (HONOR_SNANS (element_mode (type)))
6531 return false;
6533 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6534 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6535 return true;
6537 /* In a vector or complex, we would need to check the sign of all zeros. */
6538 if (TREE_CODE (addend) != REAL_CST)
6539 return false;
6541 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6542 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6543 negate = !negate;
6545 /* The mode has signed zeros, and we have to honor their sign.
6546 In this situation, there is only one case we can return true for.
6547 X - 0 is the same as X unless rounding towards -infinity is
6548 supported. */
6549 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6552 /* Subroutine of fold() that optimizes comparisons of a division by
6553 a nonzero integer constant against an integer constant, i.e.
6554 X/C1 op C2.
6556 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6557 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6558 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6560 The function returns the constant folded tree if a simplification
6561 can be made, and NULL_TREE otherwise. */
6563 static tree
6564 fold_div_compare (location_t loc,
6565 enum tree_code code, tree type, tree arg0, tree arg1)
6567 tree prod, tmp, hi, lo;
6568 tree arg00 = TREE_OPERAND (arg0, 0);
6569 tree arg01 = TREE_OPERAND (arg0, 1);
6570 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6571 bool neg_overflow = false;
6572 bool overflow;
6574 /* We have to do this the hard way to detect unsigned overflow.
6575 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6576 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6577 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6578 neg_overflow = false;
6580 if (sign == UNSIGNED)
6582 tmp = int_const_binop (MINUS_EXPR, arg01,
6583 build_int_cst (TREE_TYPE (arg01), 1));
6584 lo = prod;
6586 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6587 val = wi::add (prod, tmp, sign, &overflow);
6588 hi = force_fit_type (TREE_TYPE (arg00), val,
6589 -1, overflow | TREE_OVERFLOW (prod));
6591 else if (tree_int_cst_sgn (arg01) >= 0)
6593 tmp = int_const_binop (MINUS_EXPR, arg01,
6594 build_int_cst (TREE_TYPE (arg01), 1));
6595 switch (tree_int_cst_sgn (arg1))
6597 case -1:
6598 neg_overflow = true;
6599 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6600 hi = prod;
6601 break;
6603 case 0:
6604 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6605 hi = tmp;
6606 break;
6608 case 1:
6609 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6610 lo = prod;
6611 break;
6613 default:
6614 gcc_unreachable ();
6617 else
6619 /* A negative divisor reverses the relational operators. */
6620 code = swap_tree_comparison (code);
6622 tmp = int_const_binop (PLUS_EXPR, arg01,
6623 build_int_cst (TREE_TYPE (arg01), 1));
6624 switch (tree_int_cst_sgn (arg1))
6626 case -1:
6627 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6628 lo = prod;
6629 break;
6631 case 0:
6632 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6633 lo = tmp;
6634 break;
6636 case 1:
6637 neg_overflow = true;
6638 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6639 hi = prod;
6640 break;
6642 default:
6643 gcc_unreachable ();
6647 switch (code)
6649 case EQ_EXPR:
6650 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6651 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6652 if (TREE_OVERFLOW (hi))
6653 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6654 if (TREE_OVERFLOW (lo))
6655 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6656 return build_range_check (loc, type, arg00, 1, lo, hi);
6658 case NE_EXPR:
6659 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6660 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6661 if (TREE_OVERFLOW (hi))
6662 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6663 if (TREE_OVERFLOW (lo))
6664 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6665 return build_range_check (loc, type, arg00, 0, lo, hi);
6667 case LT_EXPR:
6668 if (TREE_OVERFLOW (lo))
6670 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6671 return omit_one_operand_loc (loc, type, tmp, arg00);
6673 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6675 case LE_EXPR:
6676 if (TREE_OVERFLOW (hi))
6678 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6679 return omit_one_operand_loc (loc, type, tmp, arg00);
6681 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6683 case GT_EXPR:
6684 if (TREE_OVERFLOW (hi))
6686 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6687 return omit_one_operand_loc (loc, type, tmp, arg00);
6689 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6691 case GE_EXPR:
6692 if (TREE_OVERFLOW (lo))
6694 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6695 return omit_one_operand_loc (loc, type, tmp, arg00);
6697 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6699 default:
6700 break;
6703 return NULL_TREE;
6707 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6708 equality/inequality test, then return a simplified form of the test
6709 using a sign testing. Otherwise return NULL. TYPE is the desired
6710 result type. */
6712 static tree
6713 fold_single_bit_test_into_sign_test (location_t loc,
6714 enum tree_code code, tree arg0, tree arg1,
6715 tree result_type)
6717 /* If this is testing a single bit, we can optimize the test. */
6718 if ((code == NE_EXPR || code == EQ_EXPR)
6719 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6720 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6722 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6723 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6724 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6726 if (arg00 != NULL_TREE
6727 /* This is only a win if casting to a signed type is cheap,
6728 i.e. when arg00's type is not a partial mode. */
6729 && TYPE_PRECISION (TREE_TYPE (arg00))
6730 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6732 tree stype = signed_type_for (TREE_TYPE (arg00));
6733 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6734 result_type,
6735 fold_convert_loc (loc, stype, arg00),
6736 build_int_cst (stype, 0));
6740 return NULL_TREE;
6743 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6744 equality/inequality test, then return a simplified form of
6745 the test using shifts and logical operations. Otherwise return
6746 NULL. TYPE is the desired result type. */
6748 tree
6749 fold_single_bit_test (location_t loc, enum tree_code code,
6750 tree arg0, tree arg1, tree result_type)
6752 /* If this is testing a single bit, we can optimize the test. */
6753 if ((code == NE_EXPR || code == EQ_EXPR)
6754 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6755 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6757 tree inner = TREE_OPERAND (arg0, 0);
6758 tree type = TREE_TYPE (arg0);
6759 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6760 machine_mode operand_mode = TYPE_MODE (type);
6761 int ops_unsigned;
6762 tree signed_type, unsigned_type, intermediate_type;
6763 tree tem, one;
6765 /* First, see if we can fold the single bit test into a sign-bit
6766 test. */
6767 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6768 result_type);
6769 if (tem)
6770 return tem;
6772 /* Otherwise we have (A & C) != 0 where C is a single bit,
6773 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6774 Similarly for (A & C) == 0. */
6776 /* If INNER is a right shift of a constant and it plus BITNUM does
6777 not overflow, adjust BITNUM and INNER. */
6778 if (TREE_CODE (inner) == RSHIFT_EXPR
6779 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6780 && bitnum < TYPE_PRECISION (type)
6781 && wi::ltu_p (TREE_OPERAND (inner, 1),
6782 TYPE_PRECISION (type) - bitnum))
6784 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6785 inner = TREE_OPERAND (inner, 0);
6788 /* If we are going to be able to omit the AND below, we must do our
6789 operations as unsigned. If we must use the AND, we have a choice.
6790 Normally unsigned is faster, but for some machines signed is. */
6791 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6792 && !flag_syntax_only) ? 0 : 1;
6794 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6795 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6796 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6797 inner = fold_convert_loc (loc, intermediate_type, inner);
6799 if (bitnum != 0)
6800 inner = build2 (RSHIFT_EXPR, intermediate_type,
6801 inner, size_int (bitnum));
6803 one = build_int_cst (intermediate_type, 1);
6805 if (code == EQ_EXPR)
6806 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6808 /* Put the AND last so it can combine with more things. */
6809 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6811 /* Make sure to return the proper type. */
6812 inner = fold_convert_loc (loc, result_type, inner);
6814 return inner;
6816 return NULL_TREE;
6819 /* Check whether we are allowed to reorder operands arg0 and arg1,
6820 such that the evaluation of arg1 occurs before arg0. */
6822 static bool
6823 reorder_operands_p (const_tree arg0, const_tree arg1)
6825 if (! flag_evaluation_order)
6826 return true;
6827 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6828 return true;
6829 return ! TREE_SIDE_EFFECTS (arg0)
6830 && ! TREE_SIDE_EFFECTS (arg1);
6833 /* Test whether it is preferable two swap two operands, ARG0 and
6834 ARG1, for example because ARG0 is an integer constant and ARG1
6835 isn't. If REORDER is true, only recommend swapping if we can
6836 evaluate the operands in reverse order. */
6838 bool
6839 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6841 if (CONSTANT_CLASS_P (arg1))
6842 return 0;
6843 if (CONSTANT_CLASS_P (arg0))
6844 return 1;
6846 STRIP_NOPS (arg0);
6847 STRIP_NOPS (arg1);
6849 if (TREE_CONSTANT (arg1))
6850 return 0;
6851 if (TREE_CONSTANT (arg0))
6852 return 1;
6854 if (reorder && flag_evaluation_order
6855 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6856 return 0;
6858 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6859 for commutative and comparison operators. Ensuring a canonical
6860 form allows the optimizers to find additional redundancies without
6861 having to explicitly check for both orderings. */
6862 if (TREE_CODE (arg0) == SSA_NAME
6863 && TREE_CODE (arg1) == SSA_NAME
6864 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6865 return 1;
6867 /* Put SSA_NAMEs last. */
6868 if (TREE_CODE (arg1) == SSA_NAME)
6869 return 0;
6870 if (TREE_CODE (arg0) == SSA_NAME)
6871 return 1;
6873 /* Put variables last. */
6874 if (DECL_P (arg1))
6875 return 0;
6876 if (DECL_P (arg0))
6877 return 1;
6879 return 0;
6883 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6884 means A >= Y && A != MAX, but in this case we know that
6885 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6887 static tree
6888 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6890 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6892 if (TREE_CODE (bound) == LT_EXPR)
6893 a = TREE_OPERAND (bound, 0);
6894 else if (TREE_CODE (bound) == GT_EXPR)
6895 a = TREE_OPERAND (bound, 1);
6896 else
6897 return NULL_TREE;
6899 typea = TREE_TYPE (a);
6900 if (!INTEGRAL_TYPE_P (typea)
6901 && !POINTER_TYPE_P (typea))
6902 return NULL_TREE;
6904 if (TREE_CODE (ineq) == LT_EXPR)
6906 a1 = TREE_OPERAND (ineq, 1);
6907 y = TREE_OPERAND (ineq, 0);
6909 else if (TREE_CODE (ineq) == GT_EXPR)
6911 a1 = TREE_OPERAND (ineq, 0);
6912 y = TREE_OPERAND (ineq, 1);
6914 else
6915 return NULL_TREE;
6917 if (TREE_TYPE (a1) != typea)
6918 return NULL_TREE;
6920 if (POINTER_TYPE_P (typea))
6922 /* Convert the pointer types into integer before taking the difference. */
6923 tree ta = fold_convert_loc (loc, ssizetype, a);
6924 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6925 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6927 else
6928 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6930 if (!diff || !integer_onep (diff))
6931 return NULL_TREE;
6933 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6936 /* Fold a sum or difference of at least one multiplication.
6937 Returns the folded tree or NULL if no simplification could be made. */
6939 static tree
6940 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6941 tree arg0, tree arg1)
6943 tree arg00, arg01, arg10, arg11;
6944 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6946 /* (A * C) +- (B * C) -> (A+-B) * C.
6947 (A * C) +- A -> A * (C+-1).
6948 We are most concerned about the case where C is a constant,
6949 but other combinations show up during loop reduction. Since
6950 it is not difficult, try all four possibilities. */
6952 if (TREE_CODE (arg0) == MULT_EXPR)
6954 arg00 = TREE_OPERAND (arg0, 0);
6955 arg01 = TREE_OPERAND (arg0, 1);
6957 else if (TREE_CODE (arg0) == INTEGER_CST)
6959 arg00 = build_one_cst (type);
6960 arg01 = arg0;
6962 else
6964 /* We cannot generate constant 1 for fract. */
6965 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6966 return NULL_TREE;
6967 arg00 = arg0;
6968 arg01 = build_one_cst (type);
6970 if (TREE_CODE (arg1) == MULT_EXPR)
6972 arg10 = TREE_OPERAND (arg1, 0);
6973 arg11 = TREE_OPERAND (arg1, 1);
6975 else if (TREE_CODE (arg1) == INTEGER_CST)
6977 arg10 = build_one_cst (type);
6978 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6979 the purpose of this canonicalization. */
6980 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6981 && negate_expr_p (arg1)
6982 && code == PLUS_EXPR)
6984 arg11 = negate_expr (arg1);
6985 code = MINUS_EXPR;
6987 else
6988 arg11 = arg1;
6990 else
6992 /* We cannot generate constant 1 for fract. */
6993 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6994 return NULL_TREE;
6995 arg10 = arg1;
6996 arg11 = build_one_cst (type);
6998 same = NULL_TREE;
7000 if (operand_equal_p (arg01, arg11, 0))
7001 same = arg01, alt0 = arg00, alt1 = arg10;
7002 else if (operand_equal_p (arg00, arg10, 0))
7003 same = arg00, alt0 = arg01, alt1 = arg11;
7004 else if (operand_equal_p (arg00, arg11, 0))
7005 same = arg00, alt0 = arg01, alt1 = arg10;
7006 else if (operand_equal_p (arg01, arg10, 0))
7007 same = arg01, alt0 = arg00, alt1 = arg11;
7009 /* No identical multiplicands; see if we can find a common
7010 power-of-two factor in non-power-of-two multiplies. This
7011 can help in multi-dimensional array access. */
7012 else if (tree_fits_shwi_p (arg01)
7013 && tree_fits_shwi_p (arg11))
7015 HOST_WIDE_INT int01, int11, tmp;
7016 bool swap = false;
7017 tree maybe_same;
7018 int01 = tree_to_shwi (arg01);
7019 int11 = tree_to_shwi (arg11);
7021 /* Move min of absolute values to int11. */
7022 if (absu_hwi (int01) < absu_hwi (int11))
7024 tmp = int01, int01 = int11, int11 = tmp;
7025 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7026 maybe_same = arg01;
7027 swap = true;
7029 else
7030 maybe_same = arg11;
7032 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7033 /* The remainder should not be a constant, otherwise we
7034 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7035 increased the number of multiplications necessary. */
7036 && TREE_CODE (arg10) != INTEGER_CST)
7038 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7039 build_int_cst (TREE_TYPE (arg00),
7040 int01 / int11));
7041 alt1 = arg10;
7042 same = maybe_same;
7043 if (swap)
7044 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7048 if (same)
7049 return fold_build2_loc (loc, MULT_EXPR, type,
7050 fold_build2_loc (loc, code, type,
7051 fold_convert_loc (loc, type, alt0),
7052 fold_convert_loc (loc, type, alt1)),
7053 fold_convert_loc (loc, type, same));
7055 return NULL_TREE;
7058 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7059 specified by EXPR into the buffer PTR of length LEN bytes.
7060 Return the number of bytes placed in the buffer, or zero
7061 upon failure. */
7063 static int
7064 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7066 tree type = TREE_TYPE (expr);
7067 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7068 int byte, offset, word, words;
7069 unsigned char value;
7071 if ((off == -1 && total_bytes > len)
7072 || off >= total_bytes)
7073 return 0;
7074 if (off == -1)
7075 off = 0;
7076 words = total_bytes / UNITS_PER_WORD;
7078 for (byte = 0; byte < total_bytes; byte++)
7080 int bitpos = byte * BITS_PER_UNIT;
7081 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7082 number of bytes. */
7083 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7085 if (total_bytes > UNITS_PER_WORD)
7087 word = byte / UNITS_PER_WORD;
7088 if (WORDS_BIG_ENDIAN)
7089 word = (words - 1) - word;
7090 offset = word * UNITS_PER_WORD;
7091 if (BYTES_BIG_ENDIAN)
7092 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7093 else
7094 offset += byte % UNITS_PER_WORD;
7096 else
7097 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7098 if (offset >= off
7099 && offset - off < len)
7100 ptr[offset - off] = value;
7102 return MIN (len, total_bytes - off);
7106 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7107 specified by EXPR into the buffer PTR of length LEN bytes.
7108 Return the number of bytes placed in the buffer, or zero
7109 upon failure. */
7111 static int
7112 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7114 tree type = TREE_TYPE (expr);
7115 machine_mode mode = TYPE_MODE (type);
7116 int total_bytes = GET_MODE_SIZE (mode);
7117 FIXED_VALUE_TYPE value;
7118 tree i_value, i_type;
7120 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7121 return 0;
7123 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7125 if (NULL_TREE == i_type
7126 || TYPE_PRECISION (i_type) != total_bytes)
7127 return 0;
7129 value = TREE_FIXED_CST (expr);
7130 i_value = double_int_to_tree (i_type, value.data);
7132 return native_encode_int (i_value, ptr, len, off);
7136 /* Subroutine of native_encode_expr. Encode the REAL_CST
7137 specified by EXPR into the buffer PTR of length LEN bytes.
7138 Return the number of bytes placed in the buffer, or zero
7139 upon failure. */
7141 static int
7142 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7144 tree type = TREE_TYPE (expr);
7145 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7146 int byte, offset, word, words, bitpos;
7147 unsigned char value;
7149 /* There are always 32 bits in each long, no matter the size of
7150 the hosts long. We handle floating point representations with
7151 up to 192 bits. */
7152 long tmp[6];
7154 if ((off == -1 && total_bytes > len)
7155 || off >= total_bytes)
7156 return 0;
7157 if (off == -1)
7158 off = 0;
7159 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7161 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7163 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7164 bitpos += BITS_PER_UNIT)
7166 byte = (bitpos / BITS_PER_UNIT) & 3;
7167 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7169 if (UNITS_PER_WORD < 4)
7171 word = byte / UNITS_PER_WORD;
7172 if (WORDS_BIG_ENDIAN)
7173 word = (words - 1) - word;
7174 offset = word * UNITS_PER_WORD;
7175 if (BYTES_BIG_ENDIAN)
7176 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7177 else
7178 offset += byte % UNITS_PER_WORD;
7180 else
7181 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7182 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7183 if (offset >= off
7184 && offset - off < len)
7185 ptr[offset - off] = value;
7187 return MIN (len, total_bytes - off);
7190 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7191 specified by EXPR into the buffer PTR of length LEN bytes.
7192 Return the number of bytes placed in the buffer, or zero
7193 upon failure. */
7195 static int
7196 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7198 int rsize, isize;
7199 tree part;
7201 part = TREE_REALPART (expr);
7202 rsize = native_encode_expr (part, ptr, len, off);
7203 if (off == -1
7204 && rsize == 0)
7205 return 0;
7206 part = TREE_IMAGPART (expr);
7207 if (off != -1)
7208 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7209 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7210 if (off == -1
7211 && isize != rsize)
7212 return 0;
7213 return rsize + isize;
7217 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7218 specified by EXPR into the buffer PTR of length LEN bytes.
7219 Return the number of bytes placed in the buffer, or zero
7220 upon failure. */
7222 static int
7223 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7225 unsigned i, count;
7226 int size, offset;
7227 tree itype, elem;
7229 offset = 0;
7230 count = VECTOR_CST_NELTS (expr);
7231 itype = TREE_TYPE (TREE_TYPE (expr));
7232 size = GET_MODE_SIZE (TYPE_MODE (itype));
7233 for (i = 0; i < count; i++)
7235 if (off >= size)
7237 off -= size;
7238 continue;
7240 elem = VECTOR_CST_ELT (expr, i);
7241 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7242 if ((off == -1 && res != size)
7243 || res == 0)
7244 return 0;
7245 offset += res;
7246 if (offset >= len)
7247 return offset;
7248 if (off != -1)
7249 off = 0;
7251 return offset;
7255 /* Subroutine of native_encode_expr. Encode the STRING_CST
7256 specified by EXPR into the buffer PTR of length LEN bytes.
7257 Return the number of bytes placed in the buffer, or zero
7258 upon failure. */
7260 static int
7261 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7263 tree type = TREE_TYPE (expr);
7264 HOST_WIDE_INT total_bytes;
7266 if (TREE_CODE (type) != ARRAY_TYPE
7267 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7268 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7269 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7270 return 0;
7271 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7272 if ((off == -1 && total_bytes > len)
7273 || off >= total_bytes)
7274 return 0;
7275 if (off == -1)
7276 off = 0;
7277 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7279 int written = 0;
7280 if (off < TREE_STRING_LENGTH (expr))
7282 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7283 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7285 memset (ptr + written, 0,
7286 MIN (total_bytes - written, len - written));
7288 else
7289 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7290 return MIN (total_bytes - off, len);
7294 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7295 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7296 buffer PTR of length LEN bytes. If OFF is not -1 then start
7297 the encoding at byte offset OFF and encode at most LEN bytes.
7298 Return the number of bytes placed in the buffer, or zero upon failure. */
7301 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7303 /* We don't support starting at negative offset and -1 is special. */
7304 if (off < -1)
7305 return 0;
7307 switch (TREE_CODE (expr))
7309 case INTEGER_CST:
7310 return native_encode_int (expr, ptr, len, off);
7312 case REAL_CST:
7313 return native_encode_real (expr, ptr, len, off);
7315 case FIXED_CST:
7316 return native_encode_fixed (expr, ptr, len, off);
7318 case COMPLEX_CST:
7319 return native_encode_complex (expr, ptr, len, off);
7321 case VECTOR_CST:
7322 return native_encode_vector (expr, ptr, len, off);
7324 case STRING_CST:
7325 return native_encode_string (expr, ptr, len, off);
7327 default:
7328 return 0;
7333 /* Subroutine of native_interpret_expr. Interpret the contents of
7334 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7335 If the buffer cannot be interpreted, return NULL_TREE. */
7337 static tree
7338 native_interpret_int (tree type, const unsigned char *ptr, int len)
7340 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7342 if (total_bytes > len
7343 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7344 return NULL_TREE;
7346 wide_int result = wi::from_buffer (ptr, total_bytes);
7348 return wide_int_to_tree (type, result);
7352 /* Subroutine of native_interpret_expr. Interpret the contents of
7353 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7354 If the buffer cannot be interpreted, return NULL_TREE. */
7356 static tree
7357 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7359 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7360 double_int result;
7361 FIXED_VALUE_TYPE fixed_value;
7363 if (total_bytes > len
7364 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7365 return NULL_TREE;
7367 result = double_int::from_buffer (ptr, total_bytes);
7368 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7370 return build_fixed (type, fixed_value);
7374 /* Subroutine of native_interpret_expr. Interpret the contents of
7375 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7376 If the buffer cannot be interpreted, return NULL_TREE. */
7378 static tree
7379 native_interpret_real (tree type, const unsigned char *ptr, int len)
7381 machine_mode mode = TYPE_MODE (type);
7382 int total_bytes = GET_MODE_SIZE (mode);
7383 unsigned char value;
7384 /* There are always 32 bits in each long, no matter the size of
7385 the hosts long. We handle floating point representations with
7386 up to 192 bits. */
7387 REAL_VALUE_TYPE r;
7388 long tmp[6];
7390 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7391 if (total_bytes > len || total_bytes > 24)
7392 return NULL_TREE;
7393 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7395 memset (tmp, 0, sizeof (tmp));
7396 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7397 bitpos += BITS_PER_UNIT)
7399 /* Both OFFSET and BYTE index within a long;
7400 bitpos indexes the whole float. */
7401 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7402 if (UNITS_PER_WORD < 4)
7404 int word = byte / UNITS_PER_WORD;
7405 if (WORDS_BIG_ENDIAN)
7406 word = (words - 1) - word;
7407 offset = word * UNITS_PER_WORD;
7408 if (BYTES_BIG_ENDIAN)
7409 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7410 else
7411 offset += byte % UNITS_PER_WORD;
7413 else
7415 offset = byte;
7416 if (BYTES_BIG_ENDIAN)
7418 /* Reverse bytes within each long, or within the entire float
7419 if it's smaller than a long (for HFmode). */
7420 offset = MIN (3, total_bytes - 1) - offset;
7421 gcc_assert (offset >= 0);
7424 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7426 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7429 real_from_target (&r, tmp, mode);
7430 return build_real (type, r);
7434 /* Subroutine of native_interpret_expr. Interpret the contents of
7435 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7436 If the buffer cannot be interpreted, return NULL_TREE. */
7438 static tree
7439 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7441 tree etype, rpart, ipart;
7442 int size;
7444 etype = TREE_TYPE (type);
7445 size = GET_MODE_SIZE (TYPE_MODE (etype));
7446 if (size * 2 > len)
7447 return NULL_TREE;
7448 rpart = native_interpret_expr (etype, ptr, size);
7449 if (!rpart)
7450 return NULL_TREE;
7451 ipart = native_interpret_expr (etype, ptr+size, size);
7452 if (!ipart)
7453 return NULL_TREE;
7454 return build_complex (type, rpart, ipart);
7458 /* Subroutine of native_interpret_expr. Interpret the contents of
7459 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7460 If the buffer cannot be interpreted, return NULL_TREE. */
7462 static tree
7463 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7465 tree etype, elem;
7466 int i, size, count;
7467 tree *elements;
7469 etype = TREE_TYPE (type);
7470 size = GET_MODE_SIZE (TYPE_MODE (etype));
7471 count = TYPE_VECTOR_SUBPARTS (type);
7472 if (size * count > len)
7473 return NULL_TREE;
7475 elements = XALLOCAVEC (tree, count);
7476 for (i = count - 1; i >= 0; i--)
7478 elem = native_interpret_expr (etype, ptr+(i*size), size);
7479 if (!elem)
7480 return NULL_TREE;
7481 elements[i] = elem;
7483 return build_vector (type, elements);
7487 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7488 the buffer PTR of length LEN as a constant of type TYPE. For
7489 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7490 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7491 return NULL_TREE. */
7493 tree
7494 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7496 switch (TREE_CODE (type))
7498 case INTEGER_TYPE:
7499 case ENUMERAL_TYPE:
7500 case BOOLEAN_TYPE:
7501 case POINTER_TYPE:
7502 case REFERENCE_TYPE:
7503 return native_interpret_int (type, ptr, len);
7505 case REAL_TYPE:
7506 return native_interpret_real (type, ptr, len);
7508 case FIXED_POINT_TYPE:
7509 return native_interpret_fixed (type, ptr, len);
7511 case COMPLEX_TYPE:
7512 return native_interpret_complex (type, ptr, len);
7514 case VECTOR_TYPE:
7515 return native_interpret_vector (type, ptr, len);
7517 default:
7518 return NULL_TREE;
7522 /* Returns true if we can interpret the contents of a native encoding
7523 as TYPE. */
7525 static bool
7526 can_native_interpret_type_p (tree type)
7528 switch (TREE_CODE (type))
7530 case INTEGER_TYPE:
7531 case ENUMERAL_TYPE:
7532 case BOOLEAN_TYPE:
7533 case POINTER_TYPE:
7534 case REFERENCE_TYPE:
7535 case FIXED_POINT_TYPE:
7536 case REAL_TYPE:
7537 case COMPLEX_TYPE:
7538 case VECTOR_TYPE:
7539 return true;
7540 default:
7541 return false;
7545 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7546 TYPE at compile-time. If we're unable to perform the conversion
7547 return NULL_TREE. */
7549 static tree
7550 fold_view_convert_expr (tree type, tree expr)
7552 /* We support up to 512-bit values (for V8DFmode). */
7553 unsigned char buffer[64];
7554 int len;
7556 /* Check that the host and target are sane. */
7557 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7558 return NULL_TREE;
7560 len = native_encode_expr (expr, buffer, sizeof (buffer));
7561 if (len == 0)
7562 return NULL_TREE;
7564 return native_interpret_expr (type, buffer, len);
7567 /* Build an expression for the address of T. Folds away INDIRECT_REF
7568 to avoid confusing the gimplify process. */
7570 tree
7571 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7573 /* The size of the object is not relevant when talking about its address. */
7574 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7575 t = TREE_OPERAND (t, 0);
7577 if (TREE_CODE (t) == INDIRECT_REF)
7579 t = TREE_OPERAND (t, 0);
7581 if (TREE_TYPE (t) != ptrtype)
7582 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7584 else if (TREE_CODE (t) == MEM_REF
7585 && integer_zerop (TREE_OPERAND (t, 1)))
7586 return TREE_OPERAND (t, 0);
7587 else if (TREE_CODE (t) == MEM_REF
7588 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7589 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7590 TREE_OPERAND (t, 0),
7591 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7592 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7594 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7596 if (TREE_TYPE (t) != ptrtype)
7597 t = fold_convert_loc (loc, ptrtype, t);
7599 else
7600 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7602 return t;
7605 /* Build an expression for the address of T. */
7607 tree
7608 build_fold_addr_expr_loc (location_t loc, tree t)
7610 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7612 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7615 /* Fold a unary expression of code CODE and type TYPE with operand
7616 OP0. Return the folded expression if folding is successful.
7617 Otherwise, return NULL_TREE. */
7619 tree
7620 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7622 tree tem;
7623 tree arg0;
7624 enum tree_code_class kind = TREE_CODE_CLASS (code);
7626 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7627 && TREE_CODE_LENGTH (code) == 1);
7629 arg0 = op0;
7630 if (arg0)
7632 if (CONVERT_EXPR_CODE_P (code)
7633 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7635 /* Don't use STRIP_NOPS, because signedness of argument type
7636 matters. */
7637 STRIP_SIGN_NOPS (arg0);
7639 else
7641 /* Strip any conversions that don't change the mode. This
7642 is safe for every expression, except for a comparison
7643 expression because its signedness is derived from its
7644 operands.
7646 Note that this is done as an internal manipulation within
7647 the constant folder, in order to find the simplest
7648 representation of the arguments so that their form can be
7649 studied. In any cases, the appropriate type conversions
7650 should be put back in the tree that will get out of the
7651 constant folder. */
7652 STRIP_NOPS (arg0);
7655 if (CONSTANT_CLASS_P (arg0))
7657 tree tem = const_unop (code, type, arg0);
7658 if (tem)
7660 if (TREE_TYPE (tem) != type)
7661 tem = fold_convert_loc (loc, type, tem);
7662 return tem;
7667 tem = generic_simplify (loc, code, type, op0);
7668 if (tem)
7669 return tem;
7671 if (TREE_CODE_CLASS (code) == tcc_unary)
7673 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7674 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7675 fold_build1_loc (loc, code, type,
7676 fold_convert_loc (loc, TREE_TYPE (op0),
7677 TREE_OPERAND (arg0, 1))));
7678 else if (TREE_CODE (arg0) == COND_EXPR)
7680 tree arg01 = TREE_OPERAND (arg0, 1);
7681 tree arg02 = TREE_OPERAND (arg0, 2);
7682 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7683 arg01 = fold_build1_loc (loc, code, type,
7684 fold_convert_loc (loc,
7685 TREE_TYPE (op0), arg01));
7686 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7687 arg02 = fold_build1_loc (loc, code, type,
7688 fold_convert_loc (loc,
7689 TREE_TYPE (op0), arg02));
7690 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7691 arg01, arg02);
7693 /* If this was a conversion, and all we did was to move into
7694 inside the COND_EXPR, bring it back out. But leave it if
7695 it is a conversion from integer to integer and the
7696 result precision is no wider than a word since such a
7697 conversion is cheap and may be optimized away by combine,
7698 while it couldn't if it were outside the COND_EXPR. Then return
7699 so we don't get into an infinite recursion loop taking the
7700 conversion out and then back in. */
7702 if ((CONVERT_EXPR_CODE_P (code)
7703 || code == NON_LVALUE_EXPR)
7704 && TREE_CODE (tem) == COND_EXPR
7705 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7706 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7707 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7708 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7709 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7710 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7711 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7712 && (INTEGRAL_TYPE_P
7713 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7714 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7715 || flag_syntax_only))
7716 tem = build1_loc (loc, code, type,
7717 build3 (COND_EXPR,
7718 TREE_TYPE (TREE_OPERAND
7719 (TREE_OPERAND (tem, 1), 0)),
7720 TREE_OPERAND (tem, 0),
7721 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7722 TREE_OPERAND (TREE_OPERAND (tem, 2),
7723 0)));
7724 return tem;
7728 switch (code)
7730 case NON_LVALUE_EXPR:
7731 if (!maybe_lvalue_p (op0))
7732 return fold_convert_loc (loc, type, op0);
7733 return NULL_TREE;
7735 CASE_CONVERT:
7736 case FLOAT_EXPR:
7737 case FIX_TRUNC_EXPR:
7738 if (COMPARISON_CLASS_P (op0))
7740 /* If we have (type) (a CMP b) and type is an integral type, return
7741 new expression involving the new type. Canonicalize
7742 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7743 non-integral type.
7744 Do not fold the result as that would not simplify further, also
7745 folding again results in recursions. */
7746 if (TREE_CODE (type) == BOOLEAN_TYPE)
7747 return build2_loc (loc, TREE_CODE (op0), type,
7748 TREE_OPERAND (op0, 0),
7749 TREE_OPERAND (op0, 1));
7750 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7751 && TREE_CODE (type) != VECTOR_TYPE)
7752 return build3_loc (loc, COND_EXPR, type, op0,
7753 constant_boolean_node (true, type),
7754 constant_boolean_node (false, type));
7757 /* Handle (T *)&A.B.C for A being of type T and B and C
7758 living at offset zero. This occurs frequently in
7759 C++ upcasting and then accessing the base. */
7760 if (TREE_CODE (op0) == ADDR_EXPR
7761 && POINTER_TYPE_P (type)
7762 && handled_component_p (TREE_OPERAND (op0, 0)))
7764 HOST_WIDE_INT bitsize, bitpos;
7765 tree offset;
7766 machine_mode mode;
7767 int unsignedp, reversep, volatilep;
7768 tree base
7769 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7770 &offset, &mode, &unsignedp, &reversep,
7771 &volatilep, false);
7772 /* If the reference was to a (constant) zero offset, we can use
7773 the address of the base if it has the same base type
7774 as the result type and the pointer type is unqualified. */
7775 if (! offset && bitpos == 0
7776 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7777 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7778 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7779 return fold_convert_loc (loc, type,
7780 build_fold_addr_expr_loc (loc, base));
7783 if (TREE_CODE (op0) == MODIFY_EXPR
7784 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7785 /* Detect assigning a bitfield. */
7786 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7787 && DECL_BIT_FIELD
7788 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7790 /* Don't leave an assignment inside a conversion
7791 unless assigning a bitfield. */
7792 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7793 /* First do the assignment, then return converted constant. */
7794 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7795 TREE_NO_WARNING (tem) = 1;
7796 TREE_USED (tem) = 1;
7797 return tem;
7800 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7801 constants (if x has signed type, the sign bit cannot be set
7802 in c). This folds extension into the BIT_AND_EXPR.
7803 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7804 very likely don't have maximal range for their precision and this
7805 transformation effectively doesn't preserve non-maximal ranges. */
7806 if (TREE_CODE (type) == INTEGER_TYPE
7807 && TREE_CODE (op0) == BIT_AND_EXPR
7808 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7810 tree and_expr = op0;
7811 tree and0 = TREE_OPERAND (and_expr, 0);
7812 tree and1 = TREE_OPERAND (and_expr, 1);
7813 int change = 0;
7815 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7816 || (TYPE_PRECISION (type)
7817 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7818 change = 1;
7819 else if (TYPE_PRECISION (TREE_TYPE (and1))
7820 <= HOST_BITS_PER_WIDE_INT
7821 && tree_fits_uhwi_p (and1))
7823 unsigned HOST_WIDE_INT cst;
7825 cst = tree_to_uhwi (and1);
7826 cst &= HOST_WIDE_INT_M1U
7827 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7828 change = (cst == 0);
7829 if (change
7830 && !flag_syntax_only
7831 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7832 == ZERO_EXTEND))
7834 tree uns = unsigned_type_for (TREE_TYPE (and0));
7835 and0 = fold_convert_loc (loc, uns, and0);
7836 and1 = fold_convert_loc (loc, uns, and1);
7839 if (change)
7841 tem = force_fit_type (type, wi::to_widest (and1), 0,
7842 TREE_OVERFLOW (and1));
7843 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7844 fold_convert_loc (loc, type, and0), tem);
7848 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7849 cast (T1)X will fold away. We assume that this happens when X itself
7850 is a cast. */
7851 if (POINTER_TYPE_P (type)
7852 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7853 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7855 tree arg00 = TREE_OPERAND (arg0, 0);
7856 tree arg01 = TREE_OPERAND (arg0, 1);
7858 return fold_build_pointer_plus_loc
7859 (loc, fold_convert_loc (loc, type, arg00), arg01);
7862 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7863 of the same precision, and X is an integer type not narrower than
7864 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7865 if (INTEGRAL_TYPE_P (type)
7866 && TREE_CODE (op0) == BIT_NOT_EXPR
7867 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7868 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7869 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7871 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7872 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7873 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7874 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7875 fold_convert_loc (loc, type, tem));
7878 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7879 type of X and Y (integer types only). */
7880 if (INTEGRAL_TYPE_P (type)
7881 && TREE_CODE (op0) == MULT_EXPR
7882 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7883 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7885 /* Be careful not to introduce new overflows. */
7886 tree mult_type;
7887 if (TYPE_OVERFLOW_WRAPS (type))
7888 mult_type = type;
7889 else
7890 mult_type = unsigned_type_for (type);
7892 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7894 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7895 fold_convert_loc (loc, mult_type,
7896 TREE_OPERAND (op0, 0)),
7897 fold_convert_loc (loc, mult_type,
7898 TREE_OPERAND (op0, 1)));
7899 return fold_convert_loc (loc, type, tem);
7903 return NULL_TREE;
7905 case VIEW_CONVERT_EXPR:
7906 if (TREE_CODE (op0) == MEM_REF)
7908 tem = fold_build2_loc (loc, MEM_REF, type,
7909 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7910 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7911 return tem;
7914 return NULL_TREE;
7916 case NEGATE_EXPR:
7917 tem = fold_negate_expr (loc, arg0);
7918 if (tem)
7919 return fold_convert_loc (loc, type, tem);
7920 return NULL_TREE;
7922 case ABS_EXPR:
7923 /* Convert fabs((double)float) into (double)fabsf(float). */
7924 if (TREE_CODE (arg0) == NOP_EXPR
7925 && TREE_CODE (type) == REAL_TYPE)
7927 tree targ0 = strip_float_extensions (arg0);
7928 if (targ0 != arg0)
7929 return fold_convert_loc (loc, type,
7930 fold_build1_loc (loc, ABS_EXPR,
7931 TREE_TYPE (targ0),
7932 targ0));
7934 return NULL_TREE;
7936 case BIT_NOT_EXPR:
7937 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7938 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7939 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7940 fold_convert_loc (loc, type,
7941 TREE_OPERAND (arg0, 0)))))
7942 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7943 fold_convert_loc (loc, type,
7944 TREE_OPERAND (arg0, 1)));
7945 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7946 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7947 fold_convert_loc (loc, type,
7948 TREE_OPERAND (arg0, 1)))))
7949 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7950 fold_convert_loc (loc, type,
7951 TREE_OPERAND (arg0, 0)), tem);
7953 return NULL_TREE;
7955 case TRUTH_NOT_EXPR:
7956 /* Note that the operand of this must be an int
7957 and its values must be 0 or 1.
7958 ("true" is a fixed value perhaps depending on the language,
7959 but we don't handle values other than 1 correctly yet.) */
7960 tem = fold_truth_not_expr (loc, arg0);
7961 if (!tem)
7962 return NULL_TREE;
7963 return fold_convert_loc (loc, type, tem);
7965 case INDIRECT_REF:
7966 /* Fold *&X to X if X is an lvalue. */
7967 if (TREE_CODE (op0) == ADDR_EXPR)
7969 tree op00 = TREE_OPERAND (op0, 0);
7970 if ((TREE_CODE (op00) == VAR_DECL
7971 || TREE_CODE (op00) == PARM_DECL
7972 || TREE_CODE (op00) == RESULT_DECL)
7973 && !TREE_READONLY (op00))
7974 return op00;
7976 return NULL_TREE;
7978 default:
7979 return NULL_TREE;
7980 } /* switch (code) */
7984 /* If the operation was a conversion do _not_ mark a resulting constant
7985 with TREE_OVERFLOW if the original constant was not. These conversions
7986 have implementation defined behavior and retaining the TREE_OVERFLOW
7987 flag here would confuse later passes such as VRP. */
7988 tree
7989 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7990 tree type, tree op0)
7992 tree res = fold_unary_loc (loc, code, type, op0);
7993 if (res
7994 && TREE_CODE (res) == INTEGER_CST
7995 && TREE_CODE (op0) == INTEGER_CST
7996 && CONVERT_EXPR_CODE_P (code))
7997 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7999 return res;
8002 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8003 operands OP0 and OP1. LOC is the location of the resulting expression.
8004 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8005 Return the folded expression if folding is successful. Otherwise,
8006 return NULL_TREE. */
8007 static tree
8008 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8009 tree arg0, tree arg1, tree op0, tree op1)
8011 tree tem;
8013 /* We only do these simplifications if we are optimizing. */
8014 if (!optimize)
8015 return NULL_TREE;
8017 /* Check for things like (A || B) && (A || C). We can convert this
8018 to A || (B && C). Note that either operator can be any of the four
8019 truth and/or operations and the transformation will still be
8020 valid. Also note that we only care about order for the
8021 ANDIF and ORIF operators. If B contains side effects, this
8022 might change the truth-value of A. */
8023 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8024 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8025 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8026 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8027 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8028 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8030 tree a00 = TREE_OPERAND (arg0, 0);
8031 tree a01 = TREE_OPERAND (arg0, 1);
8032 tree a10 = TREE_OPERAND (arg1, 0);
8033 tree a11 = TREE_OPERAND (arg1, 1);
8034 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8035 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8036 && (code == TRUTH_AND_EXPR
8037 || code == TRUTH_OR_EXPR));
8039 if (operand_equal_p (a00, a10, 0))
8040 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8041 fold_build2_loc (loc, code, type, a01, a11));
8042 else if (commutative && operand_equal_p (a00, a11, 0))
8043 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8044 fold_build2_loc (loc, code, type, a01, a10));
8045 else if (commutative && operand_equal_p (a01, a10, 0))
8046 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8047 fold_build2_loc (loc, code, type, a00, a11));
8049 /* This case if tricky because we must either have commutative
8050 operators or else A10 must not have side-effects. */
8052 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8053 && operand_equal_p (a01, a11, 0))
8054 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8055 fold_build2_loc (loc, code, type, a00, a10),
8056 a01);
8059 /* See if we can build a range comparison. */
8060 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8061 return tem;
8063 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8064 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8066 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8067 if (tem)
8068 return fold_build2_loc (loc, code, type, tem, arg1);
8071 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8072 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8074 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8075 if (tem)
8076 return fold_build2_loc (loc, code, type, arg0, tem);
8079 /* Check for the possibility of merging component references. If our
8080 lhs is another similar operation, try to merge its rhs with our
8081 rhs. Then try to merge our lhs and rhs. */
8082 if (TREE_CODE (arg0) == code
8083 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8084 TREE_OPERAND (arg0, 1), arg1)))
8085 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8087 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8088 return tem;
8090 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8091 && (code == TRUTH_AND_EXPR
8092 || code == TRUTH_ANDIF_EXPR
8093 || code == TRUTH_OR_EXPR
8094 || code == TRUTH_ORIF_EXPR))
8096 enum tree_code ncode, icode;
8098 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8099 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8100 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8102 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8103 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8104 We don't want to pack more than two leafs to a non-IF AND/OR
8105 expression.
8106 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8107 equal to IF-CODE, then we don't want to add right-hand operand.
8108 If the inner right-hand side of left-hand operand has
8109 side-effects, or isn't simple, then we can't add to it,
8110 as otherwise we might destroy if-sequence. */
8111 if (TREE_CODE (arg0) == icode
8112 && simple_operand_p_2 (arg1)
8113 /* Needed for sequence points to handle trappings, and
8114 side-effects. */
8115 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8117 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8118 arg1);
8119 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8120 tem);
8122 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8123 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8124 else if (TREE_CODE (arg1) == icode
8125 && simple_operand_p_2 (arg0)
8126 /* Needed for sequence points to handle trappings, and
8127 side-effects. */
8128 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8130 tem = fold_build2_loc (loc, ncode, type,
8131 arg0, TREE_OPERAND (arg1, 0));
8132 return fold_build2_loc (loc, icode, type, tem,
8133 TREE_OPERAND (arg1, 1));
8135 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8136 into (A OR B).
8137 For sequence point consistancy, we need to check for trapping,
8138 and side-effects. */
8139 else if (code == icode && simple_operand_p_2 (arg0)
8140 && simple_operand_p_2 (arg1))
8141 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8144 return NULL_TREE;
8147 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8148 by changing CODE to reduce the magnitude of constants involved in
8149 ARG0 of the comparison.
8150 Returns a canonicalized comparison tree if a simplification was
8151 possible, otherwise returns NULL_TREE.
8152 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8153 valid if signed overflow is undefined. */
8155 static tree
8156 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8157 tree arg0, tree arg1,
8158 bool *strict_overflow_p)
8160 enum tree_code code0 = TREE_CODE (arg0);
8161 tree t, cst0 = NULL_TREE;
8162 int sgn0;
8164 /* Match A +- CST code arg1. We can change this only if overflow
8165 is undefined. */
8166 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8167 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8168 /* In principle pointers also have undefined overflow behavior,
8169 but that causes problems elsewhere. */
8170 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8171 && (code0 == MINUS_EXPR
8172 || code0 == PLUS_EXPR)
8173 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8174 return NULL_TREE;
8176 /* Identify the constant in arg0 and its sign. */
8177 cst0 = TREE_OPERAND (arg0, 1);
8178 sgn0 = tree_int_cst_sgn (cst0);
8180 /* Overflowed constants and zero will cause problems. */
8181 if (integer_zerop (cst0)
8182 || TREE_OVERFLOW (cst0))
8183 return NULL_TREE;
8185 /* See if we can reduce the magnitude of the constant in
8186 arg0 by changing the comparison code. */
8187 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8188 if (code == LT_EXPR
8189 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8190 code = LE_EXPR;
8191 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8192 else if (code == GT_EXPR
8193 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8194 code = GE_EXPR;
8195 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8196 else if (code == LE_EXPR
8197 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8198 code = LT_EXPR;
8199 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8200 else if (code == GE_EXPR
8201 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8202 code = GT_EXPR;
8203 else
8204 return NULL_TREE;
8205 *strict_overflow_p = true;
8207 /* Now build the constant reduced in magnitude. But not if that
8208 would produce one outside of its types range. */
8209 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8210 && ((sgn0 == 1
8211 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8212 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8213 || (sgn0 == -1
8214 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8215 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8216 return NULL_TREE;
8218 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8219 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8220 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8221 t = fold_convert (TREE_TYPE (arg1), t);
8223 return fold_build2_loc (loc, code, type, t, arg1);
8226 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8227 overflow further. Try to decrease the magnitude of constants involved
8228 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8229 and put sole constants at the second argument position.
8230 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8232 static tree
8233 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8234 tree arg0, tree arg1)
8236 tree t;
8237 bool strict_overflow_p;
8238 const char * const warnmsg = G_("assuming signed overflow does not occur "
8239 "when reducing constant in comparison");
8241 /* Try canonicalization by simplifying arg0. */
8242 strict_overflow_p = false;
8243 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8244 &strict_overflow_p);
8245 if (t)
8247 if (strict_overflow_p)
8248 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8249 return t;
8252 /* Try canonicalization by simplifying arg1 using the swapped
8253 comparison. */
8254 code = swap_tree_comparison (code);
8255 strict_overflow_p = false;
8256 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8257 &strict_overflow_p);
8258 if (t && strict_overflow_p)
8259 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8260 return t;
8263 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8264 space. This is used to avoid issuing overflow warnings for
8265 expressions like &p->x which can not wrap. */
8267 static bool
8268 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8270 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8271 return true;
8273 if (bitpos < 0)
8274 return true;
8276 wide_int wi_offset;
8277 int precision = TYPE_PRECISION (TREE_TYPE (base));
8278 if (offset == NULL_TREE)
8279 wi_offset = wi::zero (precision);
8280 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8281 return true;
8282 else
8283 wi_offset = offset;
8285 bool overflow;
8286 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8287 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8288 if (overflow)
8289 return true;
8291 if (!wi::fits_uhwi_p (total))
8292 return true;
8294 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8295 if (size <= 0)
8296 return true;
8298 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8299 array. */
8300 if (TREE_CODE (base) == ADDR_EXPR)
8302 HOST_WIDE_INT base_size;
8304 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8305 if (base_size > 0 && size < base_size)
8306 size = base_size;
8309 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8312 /* Subroutine of fold_binary. This routine performs all of the
8313 transformations that are common to the equality/inequality
8314 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8315 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8316 fold_binary should call fold_binary. Fold a comparison with
8317 tree code CODE and type TYPE with operands OP0 and OP1. Return
8318 the folded comparison or NULL_TREE. */
8320 static tree
8321 fold_comparison (location_t loc, enum tree_code code, tree type,
8322 tree op0, tree op1)
8324 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8325 tree arg0, arg1, tem;
8327 arg0 = op0;
8328 arg1 = op1;
8330 STRIP_SIGN_NOPS (arg0);
8331 STRIP_SIGN_NOPS (arg1);
8333 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8334 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8335 && (equality_code
8336 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8337 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8338 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8339 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8340 && TREE_CODE (arg1) == INTEGER_CST
8341 && !TREE_OVERFLOW (arg1))
8343 const enum tree_code
8344 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8345 tree const1 = TREE_OPERAND (arg0, 1);
8346 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8347 tree variable = TREE_OPERAND (arg0, 0);
8348 tree new_const = int_const_binop (reverse_op, const2, const1);
8350 /* If the constant operation overflowed this can be
8351 simplified as a comparison against INT_MAX/INT_MIN. */
8352 if (TREE_OVERFLOW (new_const)
8353 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8355 int const1_sgn = tree_int_cst_sgn (const1);
8356 enum tree_code code2 = code;
8358 /* Get the sign of the constant on the lhs if the
8359 operation were VARIABLE + CONST1. */
8360 if (TREE_CODE (arg0) == MINUS_EXPR)
8361 const1_sgn = -const1_sgn;
8363 /* The sign of the constant determines if we overflowed
8364 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8365 Canonicalize to the INT_MIN overflow by swapping the comparison
8366 if necessary. */
8367 if (const1_sgn == -1)
8368 code2 = swap_tree_comparison (code);
8370 /* We now can look at the canonicalized case
8371 VARIABLE + 1 CODE2 INT_MIN
8372 and decide on the result. */
8373 switch (code2)
8375 case EQ_EXPR:
8376 case LT_EXPR:
8377 case LE_EXPR:
8378 return
8379 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8381 case NE_EXPR:
8382 case GE_EXPR:
8383 case GT_EXPR:
8384 return
8385 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8387 default:
8388 gcc_unreachable ();
8391 else
8393 if (!equality_code)
8394 fold_overflow_warning ("assuming signed overflow does not occur "
8395 "when changing X +- C1 cmp C2 to "
8396 "X cmp C2 -+ C1",
8397 WARN_STRICT_OVERFLOW_COMPARISON);
8398 return fold_build2_loc (loc, code, type, variable, new_const);
8402 /* For comparisons of pointers we can decompose it to a compile time
8403 comparison of the base objects and the offsets into the object.
8404 This requires at least one operand being an ADDR_EXPR or a
8405 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8406 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8407 && (TREE_CODE (arg0) == ADDR_EXPR
8408 || TREE_CODE (arg1) == ADDR_EXPR
8409 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8410 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8412 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8413 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8414 machine_mode mode;
8415 int volatilep, reversep, unsignedp;
8416 bool indirect_base0 = false, indirect_base1 = false;
8418 /* Get base and offset for the access. Strip ADDR_EXPR for
8419 get_inner_reference, but put it back by stripping INDIRECT_REF
8420 off the base object if possible. indirect_baseN will be true
8421 if baseN is not an address but refers to the object itself. */
8422 base0 = arg0;
8423 if (TREE_CODE (arg0) == ADDR_EXPR)
8425 base0
8426 = get_inner_reference (TREE_OPERAND (arg0, 0),
8427 &bitsize, &bitpos0, &offset0, &mode,
8428 &unsignedp, &reversep, &volatilep, false);
8429 if (TREE_CODE (base0) == INDIRECT_REF)
8430 base0 = TREE_OPERAND (base0, 0);
8431 else
8432 indirect_base0 = true;
8434 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8436 base0 = TREE_OPERAND (arg0, 0);
8437 STRIP_SIGN_NOPS (base0);
8438 if (TREE_CODE (base0) == ADDR_EXPR)
8440 base0
8441 = get_inner_reference (TREE_OPERAND (base0, 0),
8442 &bitsize, &bitpos0, &offset0, &mode,
8443 &unsignedp, &reversep, &volatilep,
8444 false);
8445 if (TREE_CODE (base0) == INDIRECT_REF)
8446 base0 = TREE_OPERAND (base0, 0);
8447 else
8448 indirect_base0 = true;
8450 if (offset0 == NULL_TREE || integer_zerop (offset0))
8451 offset0 = TREE_OPERAND (arg0, 1);
8452 else
8453 offset0 = size_binop (PLUS_EXPR, offset0,
8454 TREE_OPERAND (arg0, 1));
8455 if (TREE_CODE (offset0) == INTEGER_CST)
8457 offset_int tem = wi::sext (wi::to_offset (offset0),
8458 TYPE_PRECISION (sizetype));
8459 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
8460 tem += bitpos0;
8461 if (wi::fits_shwi_p (tem))
8463 bitpos0 = tem.to_shwi ();
8464 offset0 = NULL_TREE;
8469 base1 = arg1;
8470 if (TREE_CODE (arg1) == ADDR_EXPR)
8472 base1
8473 = get_inner_reference (TREE_OPERAND (arg1, 0),
8474 &bitsize, &bitpos1, &offset1, &mode,
8475 &unsignedp, &reversep, &volatilep, false);
8476 if (TREE_CODE (base1) == INDIRECT_REF)
8477 base1 = TREE_OPERAND (base1, 0);
8478 else
8479 indirect_base1 = true;
8481 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8483 base1 = TREE_OPERAND (arg1, 0);
8484 STRIP_SIGN_NOPS (base1);
8485 if (TREE_CODE (base1) == ADDR_EXPR)
8487 base1
8488 = get_inner_reference (TREE_OPERAND (base1, 0),
8489 &bitsize, &bitpos1, &offset1, &mode,
8490 &unsignedp, &reversep, &volatilep,
8491 false);
8492 if (TREE_CODE (base1) == INDIRECT_REF)
8493 base1 = TREE_OPERAND (base1, 0);
8494 else
8495 indirect_base1 = true;
8497 if (offset1 == NULL_TREE || integer_zerop (offset1))
8498 offset1 = TREE_OPERAND (arg1, 1);
8499 else
8500 offset1 = size_binop (PLUS_EXPR, offset1,
8501 TREE_OPERAND (arg1, 1));
8502 if (TREE_CODE (offset1) == INTEGER_CST)
8504 offset_int tem = wi::sext (wi::to_offset (offset1),
8505 TYPE_PRECISION (sizetype));
8506 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
8507 tem += bitpos1;
8508 if (wi::fits_shwi_p (tem))
8510 bitpos1 = tem.to_shwi ();
8511 offset1 = NULL_TREE;
8516 /* If we have equivalent bases we might be able to simplify. */
8517 if (indirect_base0 == indirect_base1
8518 && operand_equal_p (base0, base1,
8519 indirect_base0 ? OEP_ADDRESS_OF : 0))
8521 /* We can fold this expression to a constant if the non-constant
8522 offset parts are equal. */
8523 if ((offset0 == offset1
8524 || (offset0 && offset1
8525 && operand_equal_p (offset0, offset1, 0)))
8526 && (code == EQ_EXPR
8527 || code == NE_EXPR
8528 || (indirect_base0 && DECL_P (base0))
8529 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8532 if (!equality_code
8533 && bitpos0 != bitpos1
8534 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8535 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8536 fold_overflow_warning (("assuming pointer wraparound does not "
8537 "occur when comparing P +- C1 with "
8538 "P +- C2"),
8539 WARN_STRICT_OVERFLOW_CONDITIONAL);
8541 switch (code)
8543 case EQ_EXPR:
8544 return constant_boolean_node (bitpos0 == bitpos1, type);
8545 case NE_EXPR:
8546 return constant_boolean_node (bitpos0 != bitpos1, type);
8547 case LT_EXPR:
8548 return constant_boolean_node (bitpos0 < bitpos1, type);
8549 case LE_EXPR:
8550 return constant_boolean_node (bitpos0 <= bitpos1, type);
8551 case GE_EXPR:
8552 return constant_boolean_node (bitpos0 >= bitpos1, type);
8553 case GT_EXPR:
8554 return constant_boolean_node (bitpos0 > bitpos1, type);
8555 default:;
8558 /* We can simplify the comparison to a comparison of the variable
8559 offset parts if the constant offset parts are equal.
8560 Be careful to use signed sizetype here because otherwise we
8561 mess with array offsets in the wrong way. This is possible
8562 because pointer arithmetic is restricted to retain within an
8563 object and overflow on pointer differences is undefined as of
8564 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8565 else if (bitpos0 == bitpos1
8566 && (equality_code
8567 || (indirect_base0 && DECL_P (base0))
8568 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8570 /* By converting to signed sizetype we cover middle-end pointer
8571 arithmetic which operates on unsigned pointer types of size
8572 type size and ARRAY_REF offsets which are properly sign or
8573 zero extended from their type in case it is narrower than
8574 sizetype. */
8575 if (offset0 == NULL_TREE)
8576 offset0 = build_int_cst (ssizetype, 0);
8577 else
8578 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8579 if (offset1 == NULL_TREE)
8580 offset1 = build_int_cst (ssizetype, 0);
8581 else
8582 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8584 if (!equality_code
8585 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8586 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8587 fold_overflow_warning (("assuming pointer wraparound does not "
8588 "occur when comparing P +- C1 with "
8589 "P +- C2"),
8590 WARN_STRICT_OVERFLOW_COMPARISON);
8592 return fold_build2_loc (loc, code, type, offset0, offset1);
8595 /* For equal offsets we can simplify to a comparison of the
8596 base addresses. */
8597 else if (bitpos0 == bitpos1
8598 && (indirect_base0
8599 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8600 && (indirect_base1
8601 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8602 && ((offset0 == offset1)
8603 || (offset0 && offset1
8604 && operand_equal_p (offset0, offset1, 0))))
8606 if (indirect_base0)
8607 base0 = build_fold_addr_expr_loc (loc, base0);
8608 if (indirect_base1)
8609 base1 = build_fold_addr_expr_loc (loc, base1);
8610 return fold_build2_loc (loc, code, type, base0, base1);
8614 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8615 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8616 the resulting offset is smaller in absolute value than the
8617 original one and has the same sign. */
8618 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8619 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8620 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8621 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8622 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8623 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8624 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8625 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8627 tree const1 = TREE_OPERAND (arg0, 1);
8628 tree const2 = TREE_OPERAND (arg1, 1);
8629 tree variable1 = TREE_OPERAND (arg0, 0);
8630 tree variable2 = TREE_OPERAND (arg1, 0);
8631 tree cst;
8632 const char * const warnmsg = G_("assuming signed overflow does not "
8633 "occur when combining constants around "
8634 "a comparison");
8636 /* Put the constant on the side where it doesn't overflow and is
8637 of lower absolute value and of same sign than before. */
8638 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8639 ? MINUS_EXPR : PLUS_EXPR,
8640 const2, const1);
8641 if (!TREE_OVERFLOW (cst)
8642 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8643 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8645 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8646 return fold_build2_loc (loc, code, type,
8647 variable1,
8648 fold_build2_loc (loc, TREE_CODE (arg1),
8649 TREE_TYPE (arg1),
8650 variable2, cst));
8653 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8654 ? MINUS_EXPR : PLUS_EXPR,
8655 const1, const2);
8656 if (!TREE_OVERFLOW (cst)
8657 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8658 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8660 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8661 return fold_build2_loc (loc, code, type,
8662 fold_build2_loc (loc, TREE_CODE (arg0),
8663 TREE_TYPE (arg0),
8664 variable1, cst),
8665 variable2);
8669 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8670 if (tem)
8671 return tem;
8673 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8674 constant, we can simplify it. */
8675 if (TREE_CODE (arg1) == INTEGER_CST
8676 && (TREE_CODE (arg0) == MIN_EXPR
8677 || TREE_CODE (arg0) == MAX_EXPR)
8678 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8680 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8681 if (tem)
8682 return tem;
8685 /* If we are comparing an expression that just has comparisons
8686 of two integer values, arithmetic expressions of those comparisons,
8687 and constants, we can simplify it. There are only three cases
8688 to check: the two values can either be equal, the first can be
8689 greater, or the second can be greater. Fold the expression for
8690 those three values. Since each value must be 0 or 1, we have
8691 eight possibilities, each of which corresponds to the constant 0
8692 or 1 or one of the six possible comparisons.
8694 This handles common cases like (a > b) == 0 but also handles
8695 expressions like ((x > y) - (y > x)) > 0, which supposedly
8696 occur in macroized code. */
8698 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8700 tree cval1 = 0, cval2 = 0;
8701 int save_p = 0;
8703 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8704 /* Don't handle degenerate cases here; they should already
8705 have been handled anyway. */
8706 && cval1 != 0 && cval2 != 0
8707 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8708 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8709 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8710 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8711 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8712 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8713 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8715 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8716 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8718 /* We can't just pass T to eval_subst in case cval1 or cval2
8719 was the same as ARG1. */
8721 tree high_result
8722 = fold_build2_loc (loc, code, type,
8723 eval_subst (loc, arg0, cval1, maxval,
8724 cval2, minval),
8725 arg1);
8726 tree equal_result
8727 = fold_build2_loc (loc, code, type,
8728 eval_subst (loc, arg0, cval1, maxval,
8729 cval2, maxval),
8730 arg1);
8731 tree low_result
8732 = fold_build2_loc (loc, code, type,
8733 eval_subst (loc, arg0, cval1, minval,
8734 cval2, maxval),
8735 arg1);
8737 /* All three of these results should be 0 or 1. Confirm they are.
8738 Then use those values to select the proper code to use. */
8740 if (TREE_CODE (high_result) == INTEGER_CST
8741 && TREE_CODE (equal_result) == INTEGER_CST
8742 && TREE_CODE (low_result) == INTEGER_CST)
8744 /* Make a 3-bit mask with the high-order bit being the
8745 value for `>', the next for '=', and the low for '<'. */
8746 switch ((integer_onep (high_result) * 4)
8747 + (integer_onep (equal_result) * 2)
8748 + integer_onep (low_result))
8750 case 0:
8751 /* Always false. */
8752 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8753 case 1:
8754 code = LT_EXPR;
8755 break;
8756 case 2:
8757 code = EQ_EXPR;
8758 break;
8759 case 3:
8760 code = LE_EXPR;
8761 break;
8762 case 4:
8763 code = GT_EXPR;
8764 break;
8765 case 5:
8766 code = NE_EXPR;
8767 break;
8768 case 6:
8769 code = GE_EXPR;
8770 break;
8771 case 7:
8772 /* Always true. */
8773 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8776 if (save_p)
8778 tem = save_expr (build2 (code, type, cval1, cval2));
8779 SET_EXPR_LOCATION (tem, loc);
8780 return tem;
8782 return fold_build2_loc (loc, code, type, cval1, cval2);
8787 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8788 into a single range test. */
8789 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8790 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8791 && TREE_CODE (arg1) == INTEGER_CST
8792 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8793 && !integer_zerop (TREE_OPERAND (arg0, 1))
8794 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8795 && !TREE_OVERFLOW (arg1))
8797 tem = fold_div_compare (loc, code, type, arg0, arg1);
8798 if (tem != NULL_TREE)
8799 return tem;
8802 return NULL_TREE;
8806 /* Subroutine of fold_binary. Optimize complex multiplications of the
8807 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8808 argument EXPR represents the expression "z" of type TYPE. */
8810 static tree
8811 fold_mult_zconjz (location_t loc, tree type, tree expr)
8813 tree itype = TREE_TYPE (type);
8814 tree rpart, ipart, tem;
8816 if (TREE_CODE (expr) == COMPLEX_EXPR)
8818 rpart = TREE_OPERAND (expr, 0);
8819 ipart = TREE_OPERAND (expr, 1);
8821 else if (TREE_CODE (expr) == COMPLEX_CST)
8823 rpart = TREE_REALPART (expr);
8824 ipart = TREE_IMAGPART (expr);
8826 else
8828 expr = save_expr (expr);
8829 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8830 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8833 rpart = save_expr (rpart);
8834 ipart = save_expr (ipart);
8835 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8836 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8837 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8838 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8839 build_zero_cst (itype));
8843 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8844 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8846 static bool
8847 vec_cst_ctor_to_array (tree arg, tree *elts)
8849 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8851 if (TREE_CODE (arg) == VECTOR_CST)
8853 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8854 elts[i] = VECTOR_CST_ELT (arg, i);
8856 else if (TREE_CODE (arg) == CONSTRUCTOR)
8858 constructor_elt *elt;
8860 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8861 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8862 return false;
8863 else
8864 elts[i] = elt->value;
8866 else
8867 return false;
8868 for (; i < nelts; i++)
8869 elts[i]
8870 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8871 return true;
8874 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8875 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8876 NULL_TREE otherwise. */
8878 static tree
8879 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8881 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8882 tree *elts;
8883 bool need_ctor = false;
8885 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8886 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8887 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8888 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8889 return NULL_TREE;
8891 elts = XALLOCAVEC (tree, nelts * 3);
8892 if (!vec_cst_ctor_to_array (arg0, elts)
8893 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8894 return NULL_TREE;
8896 for (i = 0; i < nelts; i++)
8898 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8899 need_ctor = true;
8900 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8903 if (need_ctor)
8905 vec<constructor_elt, va_gc> *v;
8906 vec_alloc (v, nelts);
8907 for (i = 0; i < nelts; i++)
8908 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8909 return build_constructor (type, v);
8911 else
8912 return build_vector (type, &elts[2 * nelts]);
8915 /* Try to fold a pointer difference of type TYPE two address expressions of
8916 array references AREF0 and AREF1 using location LOC. Return a
8917 simplified expression for the difference or NULL_TREE. */
8919 static tree
8920 fold_addr_of_array_ref_difference (location_t loc, tree type,
8921 tree aref0, tree aref1)
8923 tree base0 = TREE_OPERAND (aref0, 0);
8924 tree base1 = TREE_OPERAND (aref1, 0);
8925 tree base_offset = build_int_cst (type, 0);
8927 /* If the bases are array references as well, recurse. If the bases
8928 are pointer indirections compute the difference of the pointers.
8929 If the bases are equal, we are set. */
8930 if ((TREE_CODE (base0) == ARRAY_REF
8931 && TREE_CODE (base1) == ARRAY_REF
8932 && (base_offset
8933 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8934 || (INDIRECT_REF_P (base0)
8935 && INDIRECT_REF_P (base1)
8936 && (base_offset
8937 = fold_binary_loc (loc, MINUS_EXPR, type,
8938 fold_convert (type, TREE_OPERAND (base0, 0)),
8939 fold_convert (type,
8940 TREE_OPERAND (base1, 0)))))
8941 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8943 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8944 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8945 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8946 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8947 return fold_build2_loc (loc, PLUS_EXPR, type,
8948 base_offset,
8949 fold_build2_loc (loc, MULT_EXPR, type,
8950 diff, esz));
8952 return NULL_TREE;
8955 /* If the real or vector real constant CST of type TYPE has an exact
8956 inverse, return it, else return NULL. */
8958 tree
8959 exact_inverse (tree type, tree cst)
8961 REAL_VALUE_TYPE r;
8962 tree unit_type, *elts;
8963 machine_mode mode;
8964 unsigned vec_nelts, i;
8966 switch (TREE_CODE (cst))
8968 case REAL_CST:
8969 r = TREE_REAL_CST (cst);
8971 if (exact_real_inverse (TYPE_MODE (type), &r))
8972 return build_real (type, r);
8974 return NULL_TREE;
8976 case VECTOR_CST:
8977 vec_nelts = VECTOR_CST_NELTS (cst);
8978 elts = XALLOCAVEC (tree, vec_nelts);
8979 unit_type = TREE_TYPE (type);
8980 mode = TYPE_MODE (unit_type);
8982 for (i = 0; i < vec_nelts; i++)
8984 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8985 if (!exact_real_inverse (mode, &r))
8986 return NULL_TREE;
8987 elts[i] = build_real (unit_type, r);
8990 return build_vector (type, elts);
8992 default:
8993 return NULL_TREE;
8997 /* Mask out the tz least significant bits of X of type TYPE where
8998 tz is the number of trailing zeroes in Y. */
8999 static wide_int
9000 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9002 int tz = wi::ctz (y);
9003 if (tz > 0)
9004 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9005 return x;
9008 /* Return true when T is an address and is known to be nonzero.
9009 For floating point we further ensure that T is not denormal.
9010 Similar logic is present in nonzero_address in rtlanal.h.
9012 If the return value is based on the assumption that signed overflow
9013 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9014 change *STRICT_OVERFLOW_P. */
9016 static bool
9017 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9019 tree type = TREE_TYPE (t);
9020 enum tree_code code;
9022 /* Doing something useful for floating point would need more work. */
9023 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9024 return false;
9026 code = TREE_CODE (t);
9027 switch (TREE_CODE_CLASS (code))
9029 case tcc_unary:
9030 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9031 strict_overflow_p);
9032 case tcc_binary:
9033 case tcc_comparison:
9034 return tree_binary_nonzero_warnv_p (code, type,
9035 TREE_OPERAND (t, 0),
9036 TREE_OPERAND (t, 1),
9037 strict_overflow_p);
9038 case tcc_constant:
9039 case tcc_declaration:
9040 case tcc_reference:
9041 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9043 default:
9044 break;
9047 switch (code)
9049 case TRUTH_NOT_EXPR:
9050 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9051 strict_overflow_p);
9053 case TRUTH_AND_EXPR:
9054 case TRUTH_OR_EXPR:
9055 case TRUTH_XOR_EXPR:
9056 return tree_binary_nonzero_warnv_p (code, type,
9057 TREE_OPERAND (t, 0),
9058 TREE_OPERAND (t, 1),
9059 strict_overflow_p);
9061 case COND_EXPR:
9062 case CONSTRUCTOR:
9063 case OBJ_TYPE_REF:
9064 case ASSERT_EXPR:
9065 case ADDR_EXPR:
9066 case WITH_SIZE_EXPR:
9067 case SSA_NAME:
9068 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9070 case COMPOUND_EXPR:
9071 case MODIFY_EXPR:
9072 case BIND_EXPR:
9073 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9074 strict_overflow_p);
9076 case SAVE_EXPR:
9077 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9078 strict_overflow_p);
9080 case CALL_EXPR:
9082 tree fndecl = get_callee_fndecl (t);
9083 if (!fndecl) return false;
9084 if (flag_delete_null_pointer_checks && !flag_check_new
9085 && DECL_IS_OPERATOR_NEW (fndecl)
9086 && !TREE_NOTHROW (fndecl))
9087 return true;
9088 if (flag_delete_null_pointer_checks
9089 && lookup_attribute ("returns_nonnull",
9090 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9091 return true;
9092 return alloca_call_p (t);
9095 default:
9096 break;
9098 return false;
9101 /* Return true when T is an address and is known to be nonzero.
9102 Handle warnings about undefined signed overflow. */
9104 static bool
9105 tree_expr_nonzero_p (tree t)
9107 bool ret, strict_overflow_p;
9109 strict_overflow_p = false;
9110 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9111 if (strict_overflow_p)
9112 fold_overflow_warning (("assuming signed overflow does not occur when "
9113 "determining that expression is always "
9114 "non-zero"),
9115 WARN_STRICT_OVERFLOW_MISC);
9116 return ret;
9119 /* Return true if T is known not to be equal to an integer W. */
9121 bool
9122 expr_not_equal_to (tree t, const wide_int &w)
9124 wide_int min, max, nz;
9125 value_range_type rtype;
9126 switch (TREE_CODE (t))
9128 case INTEGER_CST:
9129 return wi::ne_p (t, w);
9131 case SSA_NAME:
9132 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9133 return false;
9134 rtype = get_range_info (t, &min, &max);
9135 if (rtype == VR_RANGE)
9137 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9138 return true;
9139 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9140 return true;
9142 else if (rtype == VR_ANTI_RANGE
9143 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9144 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9145 return true;
9146 /* If T has some known zero bits and W has any of those bits set,
9147 then T is known not to be equal to W. */
9148 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9149 TYPE_PRECISION (TREE_TYPE (t))), 0))
9150 return true;
9151 return false;
9153 default:
9154 return false;
9158 /* Fold a binary expression of code CODE and type TYPE with operands
9159 OP0 and OP1. LOC is the location of the resulting expression.
9160 Return the folded expression if folding is successful. Otherwise,
9161 return NULL_TREE. */
9163 tree
9164 fold_binary_loc (location_t loc,
9165 enum tree_code code, tree type, tree op0, tree op1)
9167 enum tree_code_class kind = TREE_CODE_CLASS (code);
9168 tree arg0, arg1, tem;
9169 tree t1 = NULL_TREE;
9170 bool strict_overflow_p;
9171 unsigned int prec;
9173 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9174 && TREE_CODE_LENGTH (code) == 2
9175 && op0 != NULL_TREE
9176 && op1 != NULL_TREE);
9178 arg0 = op0;
9179 arg1 = op1;
9181 /* Strip any conversions that don't change the mode. This is
9182 safe for every expression, except for a comparison expression
9183 because its signedness is derived from its operands. So, in
9184 the latter case, only strip conversions that don't change the
9185 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9186 preserved.
9188 Note that this is done as an internal manipulation within the
9189 constant folder, in order to find the simplest representation
9190 of the arguments so that their form can be studied. In any
9191 cases, the appropriate type conversions should be put back in
9192 the tree that will get out of the constant folder. */
9194 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9196 STRIP_SIGN_NOPS (arg0);
9197 STRIP_SIGN_NOPS (arg1);
9199 else
9201 STRIP_NOPS (arg0);
9202 STRIP_NOPS (arg1);
9205 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9206 constant but we can't do arithmetic on them. */
9207 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9209 tem = const_binop (code, type, arg0, arg1);
9210 if (tem != NULL_TREE)
9212 if (TREE_TYPE (tem) != type)
9213 tem = fold_convert_loc (loc, type, tem);
9214 return tem;
9218 /* If this is a commutative operation, and ARG0 is a constant, move it
9219 to ARG1 to reduce the number of tests below. */
9220 if (commutative_tree_code (code)
9221 && tree_swap_operands_p (arg0, arg1, true))
9222 return fold_build2_loc (loc, code, type, op1, op0);
9224 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9225 to ARG1 to reduce the number of tests below. */
9226 if (kind == tcc_comparison
9227 && tree_swap_operands_p (arg0, arg1, true))
9228 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9230 tem = generic_simplify (loc, code, type, op0, op1);
9231 if (tem)
9232 return tem;
9234 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9236 First check for cases where an arithmetic operation is applied to a
9237 compound, conditional, or comparison operation. Push the arithmetic
9238 operation inside the compound or conditional to see if any folding
9239 can then be done. Convert comparison to conditional for this purpose.
9240 The also optimizes non-constant cases that used to be done in
9241 expand_expr.
9243 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9244 one of the operands is a comparison and the other is a comparison, a
9245 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9246 code below would make the expression more complex. Change it to a
9247 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9248 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9250 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9251 || code == EQ_EXPR || code == NE_EXPR)
9252 && TREE_CODE (type) != VECTOR_TYPE
9253 && ((truth_value_p (TREE_CODE (arg0))
9254 && (truth_value_p (TREE_CODE (arg1))
9255 || (TREE_CODE (arg1) == BIT_AND_EXPR
9256 && integer_onep (TREE_OPERAND (arg1, 1)))))
9257 || (truth_value_p (TREE_CODE (arg1))
9258 && (truth_value_p (TREE_CODE (arg0))
9259 || (TREE_CODE (arg0) == BIT_AND_EXPR
9260 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9262 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9263 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9264 : TRUTH_XOR_EXPR,
9265 boolean_type_node,
9266 fold_convert_loc (loc, boolean_type_node, arg0),
9267 fold_convert_loc (loc, boolean_type_node, arg1));
9269 if (code == EQ_EXPR)
9270 tem = invert_truthvalue_loc (loc, tem);
9272 return fold_convert_loc (loc, type, tem);
9275 if (TREE_CODE_CLASS (code) == tcc_binary
9276 || TREE_CODE_CLASS (code) == tcc_comparison)
9278 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9280 tem = fold_build2_loc (loc, code, type,
9281 fold_convert_loc (loc, TREE_TYPE (op0),
9282 TREE_OPERAND (arg0, 1)), op1);
9283 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9284 tem);
9286 if (TREE_CODE (arg1) == COMPOUND_EXPR
9287 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9289 tem = fold_build2_loc (loc, code, type, op0,
9290 fold_convert_loc (loc, TREE_TYPE (op1),
9291 TREE_OPERAND (arg1, 1)));
9292 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9293 tem);
9296 if (TREE_CODE (arg0) == COND_EXPR
9297 || TREE_CODE (arg0) == VEC_COND_EXPR
9298 || COMPARISON_CLASS_P (arg0))
9300 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9301 arg0, arg1,
9302 /*cond_first_p=*/1);
9303 if (tem != NULL_TREE)
9304 return tem;
9307 if (TREE_CODE (arg1) == COND_EXPR
9308 || TREE_CODE (arg1) == VEC_COND_EXPR
9309 || COMPARISON_CLASS_P (arg1))
9311 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9312 arg1, arg0,
9313 /*cond_first_p=*/0);
9314 if (tem != NULL_TREE)
9315 return tem;
9319 switch (code)
9321 case MEM_REF:
9322 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9323 if (TREE_CODE (arg0) == ADDR_EXPR
9324 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9326 tree iref = TREE_OPERAND (arg0, 0);
9327 return fold_build2 (MEM_REF, type,
9328 TREE_OPERAND (iref, 0),
9329 int_const_binop (PLUS_EXPR, arg1,
9330 TREE_OPERAND (iref, 1)));
9333 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9334 if (TREE_CODE (arg0) == ADDR_EXPR
9335 && handled_component_p (TREE_OPERAND (arg0, 0)))
9337 tree base;
9338 HOST_WIDE_INT coffset;
9339 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9340 &coffset);
9341 if (!base)
9342 return NULL_TREE;
9343 return fold_build2 (MEM_REF, type,
9344 build_fold_addr_expr (base),
9345 int_const_binop (PLUS_EXPR, arg1,
9346 size_int (coffset)));
9349 return NULL_TREE;
9351 case POINTER_PLUS_EXPR:
9352 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9353 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9354 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9355 return fold_convert_loc (loc, type,
9356 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9357 fold_convert_loc (loc, sizetype,
9358 arg1),
9359 fold_convert_loc (loc, sizetype,
9360 arg0)));
9362 return NULL_TREE;
9364 case PLUS_EXPR:
9365 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9367 /* X + (X / CST) * -CST is X % CST. */
9368 if (TREE_CODE (arg1) == MULT_EXPR
9369 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9370 && operand_equal_p (arg0,
9371 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9373 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9374 tree cst1 = TREE_OPERAND (arg1, 1);
9375 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9376 cst1, cst0);
9377 if (sum && integer_zerop (sum))
9378 return fold_convert_loc (loc, type,
9379 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9380 TREE_TYPE (arg0), arg0,
9381 cst0));
9385 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9386 one. Make sure the type is not saturating and has the signedness of
9387 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9388 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9389 if ((TREE_CODE (arg0) == MULT_EXPR
9390 || TREE_CODE (arg1) == MULT_EXPR)
9391 && !TYPE_SATURATING (type)
9392 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9393 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9394 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9396 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9397 if (tem)
9398 return tem;
9401 if (! FLOAT_TYPE_P (type))
9403 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9404 (plus (plus (mult) (mult)) (foo)) so that we can
9405 take advantage of the factoring cases below. */
9406 if (ANY_INTEGRAL_TYPE_P (type)
9407 && TYPE_OVERFLOW_WRAPS (type)
9408 && (((TREE_CODE (arg0) == PLUS_EXPR
9409 || TREE_CODE (arg0) == MINUS_EXPR)
9410 && TREE_CODE (arg1) == MULT_EXPR)
9411 || ((TREE_CODE (arg1) == PLUS_EXPR
9412 || TREE_CODE (arg1) == MINUS_EXPR)
9413 && TREE_CODE (arg0) == MULT_EXPR)))
9415 tree parg0, parg1, parg, marg;
9416 enum tree_code pcode;
9418 if (TREE_CODE (arg1) == MULT_EXPR)
9419 parg = arg0, marg = arg1;
9420 else
9421 parg = arg1, marg = arg0;
9422 pcode = TREE_CODE (parg);
9423 parg0 = TREE_OPERAND (parg, 0);
9424 parg1 = TREE_OPERAND (parg, 1);
9425 STRIP_NOPS (parg0);
9426 STRIP_NOPS (parg1);
9428 if (TREE_CODE (parg0) == MULT_EXPR
9429 && TREE_CODE (parg1) != MULT_EXPR)
9430 return fold_build2_loc (loc, pcode, type,
9431 fold_build2_loc (loc, PLUS_EXPR, type,
9432 fold_convert_loc (loc, type,
9433 parg0),
9434 fold_convert_loc (loc, type,
9435 marg)),
9436 fold_convert_loc (loc, type, parg1));
9437 if (TREE_CODE (parg0) != MULT_EXPR
9438 && TREE_CODE (parg1) == MULT_EXPR)
9439 return
9440 fold_build2_loc (loc, PLUS_EXPR, type,
9441 fold_convert_loc (loc, type, parg0),
9442 fold_build2_loc (loc, pcode, type,
9443 fold_convert_loc (loc, type, marg),
9444 fold_convert_loc (loc, type,
9445 parg1)));
9448 else
9450 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9451 to __complex__ ( x, y ). This is not the same for SNaNs or
9452 if signed zeros are involved. */
9453 if (!HONOR_SNANS (element_mode (arg0))
9454 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9455 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9457 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9458 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9459 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9460 bool arg0rz = false, arg0iz = false;
9461 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9462 || (arg0i && (arg0iz = real_zerop (arg0i))))
9464 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9465 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9466 if (arg0rz && arg1i && real_zerop (arg1i))
9468 tree rp = arg1r ? arg1r
9469 : build1 (REALPART_EXPR, rtype, arg1);
9470 tree ip = arg0i ? arg0i
9471 : build1 (IMAGPART_EXPR, rtype, arg0);
9472 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9474 else if (arg0iz && arg1r && real_zerop (arg1r))
9476 tree rp = arg0r ? arg0r
9477 : build1 (REALPART_EXPR, rtype, arg0);
9478 tree ip = arg1i ? arg1i
9479 : build1 (IMAGPART_EXPR, rtype, arg1);
9480 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9485 if (flag_unsafe_math_optimizations
9486 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9487 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9488 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9489 return tem;
9491 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9492 We associate floats only if the user has specified
9493 -fassociative-math. */
9494 if (flag_associative_math
9495 && TREE_CODE (arg1) == PLUS_EXPR
9496 && TREE_CODE (arg0) != MULT_EXPR)
9498 tree tree10 = TREE_OPERAND (arg1, 0);
9499 tree tree11 = TREE_OPERAND (arg1, 1);
9500 if (TREE_CODE (tree11) == MULT_EXPR
9501 && TREE_CODE (tree10) == MULT_EXPR)
9503 tree tree0;
9504 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9505 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9508 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9509 We associate floats only if the user has specified
9510 -fassociative-math. */
9511 if (flag_associative_math
9512 && TREE_CODE (arg0) == PLUS_EXPR
9513 && TREE_CODE (arg1) != MULT_EXPR)
9515 tree tree00 = TREE_OPERAND (arg0, 0);
9516 tree tree01 = TREE_OPERAND (arg0, 1);
9517 if (TREE_CODE (tree01) == MULT_EXPR
9518 && TREE_CODE (tree00) == MULT_EXPR)
9520 tree tree0;
9521 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9522 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9527 bit_rotate:
9528 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9529 is a rotate of A by C1 bits. */
9530 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9531 is a rotate of A by B bits. */
9533 enum tree_code code0, code1;
9534 tree rtype;
9535 code0 = TREE_CODE (arg0);
9536 code1 = TREE_CODE (arg1);
9537 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9538 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9539 && operand_equal_p (TREE_OPERAND (arg0, 0),
9540 TREE_OPERAND (arg1, 0), 0)
9541 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9542 TYPE_UNSIGNED (rtype))
9543 /* Only create rotates in complete modes. Other cases are not
9544 expanded properly. */
9545 && (element_precision (rtype)
9546 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9548 tree tree01, tree11;
9549 enum tree_code code01, code11;
9551 tree01 = TREE_OPERAND (arg0, 1);
9552 tree11 = TREE_OPERAND (arg1, 1);
9553 STRIP_NOPS (tree01);
9554 STRIP_NOPS (tree11);
9555 code01 = TREE_CODE (tree01);
9556 code11 = TREE_CODE (tree11);
9557 if (code01 == INTEGER_CST
9558 && code11 == INTEGER_CST
9559 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9560 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9562 tem = build2_loc (loc, LROTATE_EXPR,
9563 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9564 TREE_OPERAND (arg0, 0),
9565 code0 == LSHIFT_EXPR
9566 ? TREE_OPERAND (arg0, 1)
9567 : TREE_OPERAND (arg1, 1));
9568 return fold_convert_loc (loc, type, tem);
9570 else if (code11 == MINUS_EXPR)
9572 tree tree110, tree111;
9573 tree110 = TREE_OPERAND (tree11, 0);
9574 tree111 = TREE_OPERAND (tree11, 1);
9575 STRIP_NOPS (tree110);
9576 STRIP_NOPS (tree111);
9577 if (TREE_CODE (tree110) == INTEGER_CST
9578 && 0 == compare_tree_int (tree110,
9579 element_precision
9580 (TREE_TYPE (TREE_OPERAND
9581 (arg0, 0))))
9582 && operand_equal_p (tree01, tree111, 0))
9583 return
9584 fold_convert_loc (loc, type,
9585 build2 ((code0 == LSHIFT_EXPR
9586 ? LROTATE_EXPR
9587 : RROTATE_EXPR),
9588 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9589 TREE_OPERAND (arg0, 0),
9590 TREE_OPERAND (arg0, 1)));
9592 else if (code01 == MINUS_EXPR)
9594 tree tree010, tree011;
9595 tree010 = TREE_OPERAND (tree01, 0);
9596 tree011 = TREE_OPERAND (tree01, 1);
9597 STRIP_NOPS (tree010);
9598 STRIP_NOPS (tree011);
9599 if (TREE_CODE (tree010) == INTEGER_CST
9600 && 0 == compare_tree_int (tree010,
9601 element_precision
9602 (TREE_TYPE (TREE_OPERAND
9603 (arg0, 0))))
9604 && operand_equal_p (tree11, tree011, 0))
9605 return fold_convert_loc
9606 (loc, type,
9607 build2 ((code0 != LSHIFT_EXPR
9608 ? LROTATE_EXPR
9609 : RROTATE_EXPR),
9610 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9611 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9616 associate:
9617 /* In most languages, can't associate operations on floats through
9618 parentheses. Rather than remember where the parentheses were, we
9619 don't associate floats at all, unless the user has specified
9620 -fassociative-math.
9621 And, we need to make sure type is not saturating. */
9623 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9624 && !TYPE_SATURATING (type))
9626 tree var0, con0, lit0, minus_lit0;
9627 tree var1, con1, lit1, minus_lit1;
9628 tree atype = type;
9629 bool ok = true;
9631 /* Split both trees into variables, constants, and literals. Then
9632 associate each group together, the constants with literals,
9633 then the result with variables. This increases the chances of
9634 literals being recombined later and of generating relocatable
9635 expressions for the sum of a constant and literal. */
9636 var0 = split_tree (loc, arg0, type, code,
9637 &con0, &lit0, &minus_lit0, 0);
9638 var1 = split_tree (loc, arg1, type, code,
9639 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9641 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9642 if (code == MINUS_EXPR)
9643 code = PLUS_EXPR;
9645 /* With undefined overflow prefer doing association in a type
9646 which wraps on overflow, if that is one of the operand types. */
9647 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9648 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9650 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9651 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9652 atype = TREE_TYPE (arg0);
9653 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9654 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9655 atype = TREE_TYPE (arg1);
9656 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9659 /* With undefined overflow we can only associate constants with one
9660 variable, and constants whose association doesn't overflow. */
9661 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9662 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9664 if (var0 && var1)
9666 tree tmp0 = var0;
9667 tree tmp1 = var1;
9668 bool one_neg = false;
9670 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9672 tmp0 = TREE_OPERAND (tmp0, 0);
9673 one_neg = !one_neg;
9675 if (CONVERT_EXPR_P (tmp0)
9676 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9677 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9678 <= TYPE_PRECISION (atype)))
9679 tmp0 = TREE_OPERAND (tmp0, 0);
9680 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9682 tmp1 = TREE_OPERAND (tmp1, 0);
9683 one_neg = !one_neg;
9685 if (CONVERT_EXPR_P (tmp1)
9686 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9687 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9688 <= TYPE_PRECISION (atype)))
9689 tmp1 = TREE_OPERAND (tmp1, 0);
9690 /* The only case we can still associate with two variables
9691 is if they cancel out. */
9692 if (!one_neg
9693 || !operand_equal_p (tmp0, tmp1, 0))
9694 ok = false;
9698 /* Only do something if we found more than two objects. Otherwise,
9699 nothing has changed and we risk infinite recursion. */
9700 if (ok
9701 && (2 < ((var0 != 0) + (var1 != 0)
9702 + (con0 != 0) + (con1 != 0)
9703 + (lit0 != 0) + (lit1 != 0)
9704 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9706 bool any_overflows = false;
9707 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9708 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9709 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9710 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9711 var0 = associate_trees (loc, var0, var1, code, atype);
9712 con0 = associate_trees (loc, con0, con1, code, atype);
9713 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9714 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9715 code, atype);
9717 /* Preserve the MINUS_EXPR if the negative part of the literal is
9718 greater than the positive part. Otherwise, the multiplicative
9719 folding code (i.e extract_muldiv) may be fooled in case
9720 unsigned constants are subtracted, like in the following
9721 example: ((X*2 + 4) - 8U)/2. */
9722 if (minus_lit0 && lit0)
9724 if (TREE_CODE (lit0) == INTEGER_CST
9725 && TREE_CODE (minus_lit0) == INTEGER_CST
9726 && tree_int_cst_lt (lit0, minus_lit0))
9728 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9729 MINUS_EXPR, atype);
9730 lit0 = 0;
9732 else
9734 lit0 = associate_trees (loc, lit0, minus_lit0,
9735 MINUS_EXPR, atype);
9736 minus_lit0 = 0;
9740 /* Don't introduce overflows through reassociation. */
9741 if (!any_overflows
9742 && ((lit0 && TREE_OVERFLOW_P (lit0))
9743 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9744 return NULL_TREE;
9746 if (minus_lit0)
9748 if (con0 == 0)
9749 return
9750 fold_convert_loc (loc, type,
9751 associate_trees (loc, var0, minus_lit0,
9752 MINUS_EXPR, atype));
9753 else
9755 con0 = associate_trees (loc, con0, minus_lit0,
9756 MINUS_EXPR, atype);
9757 return
9758 fold_convert_loc (loc, type,
9759 associate_trees (loc, var0, con0,
9760 PLUS_EXPR, atype));
9764 con0 = associate_trees (loc, con0, lit0, code, atype);
9765 return
9766 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9767 code, atype));
9771 return NULL_TREE;
9773 case MINUS_EXPR:
9774 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9775 if (TREE_CODE (arg0) == NEGATE_EXPR
9776 && negate_expr_p (op1)
9777 && reorder_operands_p (arg0, arg1))
9778 return fold_build2_loc (loc, MINUS_EXPR, type,
9779 negate_expr (op1),
9780 fold_convert_loc (loc, type,
9781 TREE_OPERAND (arg0, 0)));
9783 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9784 __complex__ ( x, -y ). This is not the same for SNaNs or if
9785 signed zeros are involved. */
9786 if (!HONOR_SNANS (element_mode (arg0))
9787 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9788 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9790 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9791 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9792 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9793 bool arg0rz = false, arg0iz = false;
9794 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9795 || (arg0i && (arg0iz = real_zerop (arg0i))))
9797 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9798 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9799 if (arg0rz && arg1i && real_zerop (arg1i))
9801 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9802 arg1r ? arg1r
9803 : build1 (REALPART_EXPR, rtype, arg1));
9804 tree ip = arg0i ? arg0i
9805 : build1 (IMAGPART_EXPR, rtype, arg0);
9806 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9808 else if (arg0iz && arg1r && real_zerop (arg1r))
9810 tree rp = arg0r ? arg0r
9811 : build1 (REALPART_EXPR, rtype, arg0);
9812 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9813 arg1i ? arg1i
9814 : build1 (IMAGPART_EXPR, rtype, arg1));
9815 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9820 /* A - B -> A + (-B) if B is easily negatable. */
9821 if (negate_expr_p (op1)
9822 && ! TYPE_OVERFLOW_SANITIZED (type)
9823 && ((FLOAT_TYPE_P (type)
9824 /* Avoid this transformation if B is a positive REAL_CST. */
9825 && (TREE_CODE (op1) != REAL_CST
9826 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9827 || INTEGRAL_TYPE_P (type)))
9828 return fold_build2_loc (loc, PLUS_EXPR, type,
9829 fold_convert_loc (loc, type, arg0),
9830 negate_expr (op1));
9832 /* Fold &a[i] - &a[j] to i-j. */
9833 if (TREE_CODE (arg0) == ADDR_EXPR
9834 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9835 && TREE_CODE (arg1) == ADDR_EXPR
9836 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9838 tree tem = fold_addr_of_array_ref_difference (loc, type,
9839 TREE_OPERAND (arg0, 0),
9840 TREE_OPERAND (arg1, 0));
9841 if (tem)
9842 return tem;
9845 if (FLOAT_TYPE_P (type)
9846 && flag_unsafe_math_optimizations
9847 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9848 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9849 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9850 return tem;
9852 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9853 one. Make sure the type is not saturating and has the signedness of
9854 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9855 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9856 if ((TREE_CODE (arg0) == MULT_EXPR
9857 || TREE_CODE (arg1) == MULT_EXPR)
9858 && !TYPE_SATURATING (type)
9859 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9860 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9861 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9863 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9864 if (tem)
9865 return tem;
9868 goto associate;
9870 case MULT_EXPR:
9871 if (! FLOAT_TYPE_P (type))
9873 /* Transform x * -C into -x * C if x is easily negatable. */
9874 if (TREE_CODE (op1) == INTEGER_CST
9875 && tree_int_cst_sgn (op1) == -1
9876 && negate_expr_p (op0)
9877 && (tem = negate_expr (op1)) != op1
9878 && ! TREE_OVERFLOW (tem))
9879 return fold_build2_loc (loc, MULT_EXPR, type,
9880 fold_convert_loc (loc, type,
9881 negate_expr (op0)), tem);
9883 /* (A + A) * C -> A * 2 * C */
9884 if (TREE_CODE (arg0) == PLUS_EXPR
9885 && TREE_CODE (arg1) == INTEGER_CST
9886 && operand_equal_p (TREE_OPERAND (arg0, 0),
9887 TREE_OPERAND (arg0, 1), 0))
9888 return fold_build2_loc (loc, MULT_EXPR, type,
9889 omit_one_operand_loc (loc, type,
9890 TREE_OPERAND (arg0, 0),
9891 TREE_OPERAND (arg0, 1)),
9892 fold_build2_loc (loc, MULT_EXPR, type,
9893 build_int_cst (type, 2) , arg1));
9895 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9896 sign-changing only. */
9897 if (TREE_CODE (arg1) == INTEGER_CST
9898 && TREE_CODE (arg0) == EXACT_DIV_EXPR
9899 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
9900 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9902 strict_overflow_p = false;
9903 if (TREE_CODE (arg1) == INTEGER_CST
9904 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9905 &strict_overflow_p)))
9907 if (strict_overflow_p)
9908 fold_overflow_warning (("assuming signed overflow does not "
9909 "occur when simplifying "
9910 "multiplication"),
9911 WARN_STRICT_OVERFLOW_MISC);
9912 return fold_convert_loc (loc, type, tem);
9915 /* Optimize z * conj(z) for integer complex numbers. */
9916 if (TREE_CODE (arg0) == CONJ_EXPR
9917 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9918 return fold_mult_zconjz (loc, type, arg1);
9919 if (TREE_CODE (arg1) == CONJ_EXPR
9920 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9921 return fold_mult_zconjz (loc, type, arg0);
9923 else
9925 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9926 This is not the same for NaNs or if signed zeros are
9927 involved. */
9928 if (!HONOR_NANS (arg0)
9929 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9930 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9931 && TREE_CODE (arg1) == COMPLEX_CST
9932 && real_zerop (TREE_REALPART (arg1)))
9934 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9935 if (real_onep (TREE_IMAGPART (arg1)))
9936 return
9937 fold_build2_loc (loc, COMPLEX_EXPR, type,
9938 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9939 rtype, arg0)),
9940 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9941 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9942 return
9943 fold_build2_loc (loc, COMPLEX_EXPR, type,
9944 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9945 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9946 rtype, arg0)));
9949 /* Optimize z * conj(z) for floating point complex numbers.
9950 Guarded by flag_unsafe_math_optimizations as non-finite
9951 imaginary components don't produce scalar results. */
9952 if (flag_unsafe_math_optimizations
9953 && TREE_CODE (arg0) == CONJ_EXPR
9954 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9955 return fold_mult_zconjz (loc, type, arg1);
9956 if (flag_unsafe_math_optimizations
9957 && TREE_CODE (arg1) == CONJ_EXPR
9958 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9959 return fold_mult_zconjz (loc, type, arg0);
9961 if (flag_unsafe_math_optimizations)
9964 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
9965 if (!in_gimple_form
9966 && optimize
9967 && operand_equal_p (arg0, arg1, 0))
9969 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
9971 if (powfn)
9973 tree arg = build_real (type, dconst2);
9974 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
9979 goto associate;
9981 case BIT_IOR_EXPR:
9982 /* Canonicalize (X & C1) | C2. */
9983 if (TREE_CODE (arg0) == BIT_AND_EXPR
9984 && TREE_CODE (arg1) == INTEGER_CST
9985 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9987 int width = TYPE_PRECISION (type), w;
9988 wide_int c1 = TREE_OPERAND (arg0, 1);
9989 wide_int c2 = arg1;
9991 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9992 if ((c1 & c2) == c1)
9993 return omit_one_operand_loc (loc, type, arg1,
9994 TREE_OPERAND (arg0, 0));
9996 wide_int msk = wi::mask (width, false,
9997 TYPE_PRECISION (TREE_TYPE (arg1)));
9999 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10000 if (msk.and_not (c1 | c2) == 0)
10001 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10002 TREE_OPERAND (arg0, 0), arg1);
10004 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10005 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10006 mode which allows further optimizations. */
10007 c1 &= msk;
10008 c2 &= msk;
10009 wide_int c3 = c1.and_not (c2);
10010 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10012 wide_int mask = wi::mask (w, false,
10013 TYPE_PRECISION (type));
10014 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
10016 c3 = mask;
10017 break;
10021 if (c3 != c1)
10022 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10023 fold_build2_loc (loc, BIT_AND_EXPR, type,
10024 TREE_OPERAND (arg0, 0),
10025 wide_int_to_tree (type,
10026 c3)),
10027 arg1);
10030 /* See if this can be simplified into a rotate first. If that
10031 is unsuccessful continue in the association code. */
10032 goto bit_rotate;
10034 case BIT_XOR_EXPR:
10035 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10036 if (TREE_CODE (arg0) == BIT_AND_EXPR
10037 && INTEGRAL_TYPE_P (type)
10038 && integer_onep (TREE_OPERAND (arg0, 1))
10039 && integer_onep (arg1))
10040 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10041 build_zero_cst (TREE_TYPE (arg0)));
10043 /* See if this can be simplified into a rotate first. If that
10044 is unsuccessful continue in the association code. */
10045 goto bit_rotate;
10047 case BIT_AND_EXPR:
10048 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10049 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10050 && INTEGRAL_TYPE_P (type)
10051 && integer_onep (TREE_OPERAND (arg0, 1))
10052 && integer_onep (arg1))
10054 tree tem2;
10055 tem = TREE_OPERAND (arg0, 0);
10056 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10057 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10058 tem, tem2);
10059 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10060 build_zero_cst (TREE_TYPE (tem)));
10062 /* Fold ~X & 1 as (X & 1) == 0. */
10063 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10064 && INTEGRAL_TYPE_P (type)
10065 && integer_onep (arg1))
10067 tree tem2;
10068 tem = TREE_OPERAND (arg0, 0);
10069 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10070 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10071 tem, tem2);
10072 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10073 build_zero_cst (TREE_TYPE (tem)));
10075 /* Fold !X & 1 as X == 0. */
10076 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10077 && integer_onep (arg1))
10079 tem = TREE_OPERAND (arg0, 0);
10080 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10081 build_zero_cst (TREE_TYPE (tem)));
10084 /* Fold (X ^ Y) & Y as ~X & Y. */
10085 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10086 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10088 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10089 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10090 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10091 fold_convert_loc (loc, type, arg1));
10093 /* Fold (X ^ Y) & X as ~Y & X. */
10094 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10095 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10096 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10098 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10099 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10100 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10101 fold_convert_loc (loc, type, arg1));
10103 /* Fold X & (X ^ Y) as X & ~Y. */
10104 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10105 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10107 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10108 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10109 fold_convert_loc (loc, type, arg0),
10110 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10112 /* Fold X & (Y ^ X) as ~Y & X. */
10113 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10114 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10115 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10117 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10118 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10119 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10120 fold_convert_loc (loc, type, arg0));
10123 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10124 multiple of 1 << CST. */
10125 if (TREE_CODE (arg1) == INTEGER_CST)
10127 wide_int cst1 = arg1;
10128 wide_int ncst1 = -cst1;
10129 if ((cst1 & ncst1) == ncst1
10130 && multiple_of_p (type, arg0,
10131 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10132 return fold_convert_loc (loc, type, arg0);
10135 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10136 bits from CST2. */
10137 if (TREE_CODE (arg1) == INTEGER_CST
10138 && TREE_CODE (arg0) == MULT_EXPR
10139 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10141 wide_int warg1 = arg1;
10142 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10144 if (masked == 0)
10145 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10146 arg0, arg1);
10147 else if (masked != warg1)
10149 /* Avoid the transform if arg1 is a mask of some
10150 mode which allows further optimizations. */
10151 int pop = wi::popcount (warg1);
10152 if (!(pop >= BITS_PER_UNIT
10153 && exact_log2 (pop) != -1
10154 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10155 return fold_build2_loc (loc, code, type, op0,
10156 wide_int_to_tree (type, masked));
10160 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10161 ((A & N) + B) & M -> (A + B) & M
10162 Similarly if (N & M) == 0,
10163 ((A | N) + B) & M -> (A + B) & M
10164 and for - instead of + (or unary - instead of +)
10165 and/or ^ instead of |.
10166 If B is constant and (B & M) == 0, fold into A & M. */
10167 if (TREE_CODE (arg1) == INTEGER_CST)
10169 wide_int cst1 = arg1;
10170 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10171 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10172 && (TREE_CODE (arg0) == PLUS_EXPR
10173 || TREE_CODE (arg0) == MINUS_EXPR
10174 || TREE_CODE (arg0) == NEGATE_EXPR)
10175 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10176 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10178 tree pmop[2];
10179 int which = 0;
10180 wide_int cst0;
10182 /* Now we know that arg0 is (C + D) or (C - D) or
10183 -C and arg1 (M) is == (1LL << cst) - 1.
10184 Store C into PMOP[0] and D into PMOP[1]. */
10185 pmop[0] = TREE_OPERAND (arg0, 0);
10186 pmop[1] = NULL;
10187 if (TREE_CODE (arg0) != NEGATE_EXPR)
10189 pmop[1] = TREE_OPERAND (arg0, 1);
10190 which = 1;
10193 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10194 which = -1;
10196 for (; which >= 0; which--)
10197 switch (TREE_CODE (pmop[which]))
10199 case BIT_AND_EXPR:
10200 case BIT_IOR_EXPR:
10201 case BIT_XOR_EXPR:
10202 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10203 != INTEGER_CST)
10204 break;
10205 cst0 = TREE_OPERAND (pmop[which], 1);
10206 cst0 &= cst1;
10207 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10209 if (cst0 != cst1)
10210 break;
10212 else if (cst0 != 0)
10213 break;
10214 /* If C or D is of the form (A & N) where
10215 (N & M) == M, or of the form (A | N) or
10216 (A ^ N) where (N & M) == 0, replace it with A. */
10217 pmop[which] = TREE_OPERAND (pmop[which], 0);
10218 break;
10219 case INTEGER_CST:
10220 /* If C or D is a N where (N & M) == 0, it can be
10221 omitted (assumed 0). */
10222 if ((TREE_CODE (arg0) == PLUS_EXPR
10223 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10224 && (cst1 & pmop[which]) == 0)
10225 pmop[which] = NULL;
10226 break;
10227 default:
10228 break;
10231 /* Only build anything new if we optimized one or both arguments
10232 above. */
10233 if (pmop[0] != TREE_OPERAND (arg0, 0)
10234 || (TREE_CODE (arg0) != NEGATE_EXPR
10235 && pmop[1] != TREE_OPERAND (arg0, 1)))
10237 tree utype = TREE_TYPE (arg0);
10238 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10240 /* Perform the operations in a type that has defined
10241 overflow behavior. */
10242 utype = unsigned_type_for (TREE_TYPE (arg0));
10243 if (pmop[0] != NULL)
10244 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10245 if (pmop[1] != NULL)
10246 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10249 if (TREE_CODE (arg0) == NEGATE_EXPR)
10250 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10251 else if (TREE_CODE (arg0) == PLUS_EXPR)
10253 if (pmop[0] != NULL && pmop[1] != NULL)
10254 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10255 pmop[0], pmop[1]);
10256 else if (pmop[0] != NULL)
10257 tem = pmop[0];
10258 else if (pmop[1] != NULL)
10259 tem = pmop[1];
10260 else
10261 return build_int_cst (type, 0);
10263 else if (pmop[0] == NULL)
10264 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10265 else
10266 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10267 pmop[0], pmop[1]);
10268 /* TEM is now the new binary +, - or unary - replacement. */
10269 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10270 fold_convert_loc (loc, utype, arg1));
10271 return fold_convert_loc (loc, type, tem);
10276 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10277 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10278 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10280 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10282 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10283 if (mask == -1)
10284 return
10285 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10288 goto associate;
10290 case RDIV_EXPR:
10291 /* Don't touch a floating-point divide by zero unless the mode
10292 of the constant can represent infinity. */
10293 if (TREE_CODE (arg1) == REAL_CST
10294 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10295 && real_zerop (arg1))
10296 return NULL_TREE;
10298 /* (-A) / (-B) -> A / B */
10299 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10300 return fold_build2_loc (loc, RDIV_EXPR, type,
10301 TREE_OPERAND (arg0, 0),
10302 negate_expr (arg1));
10303 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10304 return fold_build2_loc (loc, RDIV_EXPR, type,
10305 negate_expr (arg0),
10306 TREE_OPERAND (arg1, 0));
10307 return NULL_TREE;
10309 case TRUNC_DIV_EXPR:
10310 /* Fall through */
10312 case FLOOR_DIV_EXPR:
10313 /* Simplify A / (B << N) where A and B are positive and B is
10314 a power of 2, to A >> (N + log2(B)). */
10315 strict_overflow_p = false;
10316 if (TREE_CODE (arg1) == LSHIFT_EXPR
10317 && (TYPE_UNSIGNED (type)
10318 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10320 tree sval = TREE_OPERAND (arg1, 0);
10321 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10323 tree sh_cnt = TREE_OPERAND (arg1, 1);
10324 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10325 wi::exact_log2 (sval));
10327 if (strict_overflow_p)
10328 fold_overflow_warning (("assuming signed overflow does not "
10329 "occur when simplifying A / (B << N)"),
10330 WARN_STRICT_OVERFLOW_MISC);
10332 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10333 sh_cnt, pow2);
10334 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10335 fold_convert_loc (loc, type, arg0), sh_cnt);
10339 /* Fall through */
10341 case ROUND_DIV_EXPR:
10342 case CEIL_DIV_EXPR:
10343 case EXACT_DIV_EXPR:
10344 if (integer_zerop (arg1))
10345 return NULL_TREE;
10347 /* Convert -A / -B to A / B when the type is signed and overflow is
10348 undefined. */
10349 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10350 && TREE_CODE (arg0) == NEGATE_EXPR
10351 && negate_expr_p (op1))
10353 if (INTEGRAL_TYPE_P (type))
10354 fold_overflow_warning (("assuming signed overflow does not occur "
10355 "when distributing negation across "
10356 "division"),
10357 WARN_STRICT_OVERFLOW_MISC);
10358 return fold_build2_loc (loc, code, type,
10359 fold_convert_loc (loc, type,
10360 TREE_OPERAND (arg0, 0)),
10361 negate_expr (op1));
10363 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10364 && TREE_CODE (arg1) == NEGATE_EXPR
10365 && negate_expr_p (op0))
10367 if (INTEGRAL_TYPE_P (type))
10368 fold_overflow_warning (("assuming signed overflow does not occur "
10369 "when distributing negation across "
10370 "division"),
10371 WARN_STRICT_OVERFLOW_MISC);
10372 return fold_build2_loc (loc, code, type,
10373 negate_expr (op0),
10374 fold_convert_loc (loc, type,
10375 TREE_OPERAND (arg1, 0)));
10378 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10379 operation, EXACT_DIV_EXPR.
10381 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10382 At one time others generated faster code, it's not clear if they do
10383 after the last round to changes to the DIV code in expmed.c. */
10384 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10385 && multiple_of_p (type, arg0, arg1))
10386 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10387 fold_convert (type, arg0),
10388 fold_convert (type, arg1));
10390 strict_overflow_p = false;
10391 if (TREE_CODE (arg1) == INTEGER_CST
10392 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10393 &strict_overflow_p)))
10395 if (strict_overflow_p)
10396 fold_overflow_warning (("assuming signed overflow does not occur "
10397 "when simplifying division"),
10398 WARN_STRICT_OVERFLOW_MISC);
10399 return fold_convert_loc (loc, type, tem);
10402 return NULL_TREE;
10404 case CEIL_MOD_EXPR:
10405 case FLOOR_MOD_EXPR:
10406 case ROUND_MOD_EXPR:
10407 case TRUNC_MOD_EXPR:
10408 strict_overflow_p = false;
10409 if (TREE_CODE (arg1) == INTEGER_CST
10410 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10411 &strict_overflow_p)))
10413 if (strict_overflow_p)
10414 fold_overflow_warning (("assuming signed overflow does not occur "
10415 "when simplifying modulus"),
10416 WARN_STRICT_OVERFLOW_MISC);
10417 return fold_convert_loc (loc, type, tem);
10420 return NULL_TREE;
10422 case LROTATE_EXPR:
10423 case RROTATE_EXPR:
10424 case RSHIFT_EXPR:
10425 case LSHIFT_EXPR:
10426 /* Since negative shift count is not well-defined,
10427 don't try to compute it in the compiler. */
10428 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10429 return NULL_TREE;
10431 prec = element_precision (type);
10433 /* If we have a rotate of a bit operation with the rotate count and
10434 the second operand of the bit operation both constant,
10435 permute the two operations. */
10436 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10437 && (TREE_CODE (arg0) == BIT_AND_EXPR
10438 || TREE_CODE (arg0) == BIT_IOR_EXPR
10439 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10440 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10441 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10442 fold_build2_loc (loc, code, type,
10443 TREE_OPERAND (arg0, 0), arg1),
10444 fold_build2_loc (loc, code, type,
10445 TREE_OPERAND (arg0, 1), arg1));
10447 /* Two consecutive rotates adding up to the some integer
10448 multiple of the precision of the type can be ignored. */
10449 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10450 && TREE_CODE (arg0) == RROTATE_EXPR
10451 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10452 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10453 prec) == 0)
10454 return TREE_OPERAND (arg0, 0);
10456 return NULL_TREE;
10458 case MIN_EXPR:
10459 case MAX_EXPR:
10460 goto associate;
10462 case TRUTH_ANDIF_EXPR:
10463 /* Note that the operands of this must be ints
10464 and their values must be 0 or 1.
10465 ("true" is a fixed value perhaps depending on the language.) */
10466 /* If first arg is constant zero, return it. */
10467 if (integer_zerop (arg0))
10468 return fold_convert_loc (loc, type, arg0);
10469 case TRUTH_AND_EXPR:
10470 /* If either arg is constant true, drop it. */
10471 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10472 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10473 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10474 /* Preserve sequence points. */
10475 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10476 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10477 /* If second arg is constant zero, result is zero, but first arg
10478 must be evaluated. */
10479 if (integer_zerop (arg1))
10480 return omit_one_operand_loc (loc, type, arg1, arg0);
10481 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10482 case will be handled here. */
10483 if (integer_zerop (arg0))
10484 return omit_one_operand_loc (loc, type, arg0, arg1);
10486 /* !X && X is always false. */
10487 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10488 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10489 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10490 /* X && !X is always false. */
10491 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10492 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10493 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10495 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10496 means A >= Y && A != MAX, but in this case we know that
10497 A < X <= MAX. */
10499 if (!TREE_SIDE_EFFECTS (arg0)
10500 && !TREE_SIDE_EFFECTS (arg1))
10502 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10503 if (tem && !operand_equal_p (tem, arg0, 0))
10504 return fold_build2_loc (loc, code, type, tem, arg1);
10506 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10507 if (tem && !operand_equal_p (tem, arg1, 0))
10508 return fold_build2_loc (loc, code, type, arg0, tem);
10511 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10512 != NULL_TREE)
10513 return tem;
10515 return NULL_TREE;
10517 case TRUTH_ORIF_EXPR:
10518 /* Note that the operands of this must be ints
10519 and their values must be 0 or true.
10520 ("true" is a fixed value perhaps depending on the language.) */
10521 /* If first arg is constant true, return it. */
10522 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10523 return fold_convert_loc (loc, type, arg0);
10524 case TRUTH_OR_EXPR:
10525 /* If either arg is constant zero, drop it. */
10526 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10527 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10528 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10529 /* Preserve sequence points. */
10530 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10531 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10532 /* If second arg is constant true, result is true, but we must
10533 evaluate first arg. */
10534 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10535 return omit_one_operand_loc (loc, type, arg1, arg0);
10536 /* Likewise for first arg, but note this only occurs here for
10537 TRUTH_OR_EXPR. */
10538 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10539 return omit_one_operand_loc (loc, type, arg0, arg1);
10541 /* !X || X is always true. */
10542 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10543 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10544 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10545 /* X || !X is always true. */
10546 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10547 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10548 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10550 /* (X && !Y) || (!X && Y) is X ^ Y */
10551 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10552 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10554 tree a0, a1, l0, l1, n0, n1;
10556 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10557 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10559 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10560 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10562 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10563 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10565 if ((operand_equal_p (n0, a0, 0)
10566 && operand_equal_p (n1, a1, 0))
10567 || (operand_equal_p (n0, a1, 0)
10568 && operand_equal_p (n1, a0, 0)))
10569 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10572 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10573 != NULL_TREE)
10574 return tem;
10576 return NULL_TREE;
10578 case TRUTH_XOR_EXPR:
10579 /* If the second arg is constant zero, drop it. */
10580 if (integer_zerop (arg1))
10581 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10582 /* If the second arg is constant true, this is a logical inversion. */
10583 if (integer_onep (arg1))
10585 tem = invert_truthvalue_loc (loc, arg0);
10586 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10588 /* Identical arguments cancel to zero. */
10589 if (operand_equal_p (arg0, arg1, 0))
10590 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10592 /* !X ^ X is always true. */
10593 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10594 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10595 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10597 /* X ^ !X is always true. */
10598 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10599 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10600 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10602 return NULL_TREE;
10604 case EQ_EXPR:
10605 case NE_EXPR:
10606 STRIP_NOPS (arg0);
10607 STRIP_NOPS (arg1);
10609 tem = fold_comparison (loc, code, type, op0, op1);
10610 if (tem != NULL_TREE)
10611 return tem;
10613 /* bool_var != 1 becomes !bool_var. */
10614 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10615 && code == NE_EXPR)
10616 return fold_convert_loc (loc, type,
10617 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10618 TREE_TYPE (arg0), arg0));
10620 /* bool_var == 0 becomes !bool_var. */
10621 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10622 && code == EQ_EXPR)
10623 return fold_convert_loc (loc, type,
10624 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10625 TREE_TYPE (arg0), arg0));
10627 /* !exp != 0 becomes !exp */
10628 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10629 && code == NE_EXPR)
10630 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10632 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10633 if ((TREE_CODE (arg0) == PLUS_EXPR
10634 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10635 || TREE_CODE (arg0) == MINUS_EXPR)
10636 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10637 0)),
10638 arg1, 0)
10639 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10640 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10642 tree val = TREE_OPERAND (arg0, 1);
10643 val = fold_build2_loc (loc, code, type, val,
10644 build_int_cst (TREE_TYPE (val), 0));
10645 return omit_two_operands_loc (loc, type, val,
10646 TREE_OPERAND (arg0, 0), arg1);
10649 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10650 if ((TREE_CODE (arg1) == PLUS_EXPR
10651 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10652 || TREE_CODE (arg1) == MINUS_EXPR)
10653 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10654 0)),
10655 arg0, 0)
10656 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10657 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10659 tree val = TREE_OPERAND (arg1, 1);
10660 val = fold_build2_loc (loc, code, type, val,
10661 build_int_cst (TREE_TYPE (val), 0));
10662 return omit_two_operands_loc (loc, type, val,
10663 TREE_OPERAND (arg1, 0), arg0);
10666 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10667 if (TREE_CODE (arg0) == MINUS_EXPR
10668 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10669 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10670 1)),
10671 arg1, 0)
10672 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10673 return omit_two_operands_loc (loc, type,
10674 code == NE_EXPR
10675 ? boolean_true_node : boolean_false_node,
10676 TREE_OPERAND (arg0, 1), arg1);
10678 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10679 if (TREE_CODE (arg1) == MINUS_EXPR
10680 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10681 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10682 1)),
10683 arg0, 0)
10684 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10685 return omit_two_operands_loc (loc, type,
10686 code == NE_EXPR
10687 ? boolean_true_node : boolean_false_node,
10688 TREE_OPERAND (arg1, 1), arg0);
10690 /* If this is an EQ or NE comparison with zero and ARG0 is
10691 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10692 two operations, but the latter can be done in one less insn
10693 on machines that have only two-operand insns or on which a
10694 constant cannot be the first operand. */
10695 if (TREE_CODE (arg0) == BIT_AND_EXPR
10696 && integer_zerop (arg1))
10698 tree arg00 = TREE_OPERAND (arg0, 0);
10699 tree arg01 = TREE_OPERAND (arg0, 1);
10700 if (TREE_CODE (arg00) == LSHIFT_EXPR
10701 && integer_onep (TREE_OPERAND (arg00, 0)))
10703 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10704 arg01, TREE_OPERAND (arg00, 1));
10705 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10706 build_int_cst (TREE_TYPE (arg0), 1));
10707 return fold_build2_loc (loc, code, type,
10708 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10709 arg1);
10711 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10712 && integer_onep (TREE_OPERAND (arg01, 0)))
10714 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10715 arg00, TREE_OPERAND (arg01, 1));
10716 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10717 build_int_cst (TREE_TYPE (arg0), 1));
10718 return fold_build2_loc (loc, code, type,
10719 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10720 arg1);
10724 /* If this is an NE or EQ comparison of zero against the result of a
10725 signed MOD operation whose second operand is a power of 2, make
10726 the MOD operation unsigned since it is simpler and equivalent. */
10727 if (integer_zerop (arg1)
10728 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10729 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10730 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10731 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10732 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10733 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10735 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10736 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10737 fold_convert_loc (loc, newtype,
10738 TREE_OPERAND (arg0, 0)),
10739 fold_convert_loc (loc, newtype,
10740 TREE_OPERAND (arg0, 1)));
10742 return fold_build2_loc (loc, code, type, newmod,
10743 fold_convert_loc (loc, newtype, arg1));
10746 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10747 C1 is a valid shift constant, and C2 is a power of two, i.e.
10748 a single bit. */
10749 if (TREE_CODE (arg0) == BIT_AND_EXPR
10750 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10751 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10752 == INTEGER_CST
10753 && integer_pow2p (TREE_OPERAND (arg0, 1))
10754 && integer_zerop (arg1))
10756 tree itype = TREE_TYPE (arg0);
10757 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10758 prec = TYPE_PRECISION (itype);
10760 /* Check for a valid shift count. */
10761 if (wi::ltu_p (arg001, prec))
10763 tree arg01 = TREE_OPERAND (arg0, 1);
10764 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10765 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10766 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10767 can be rewritten as (X & (C2 << C1)) != 0. */
10768 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10770 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10771 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10772 return fold_build2_loc (loc, code, type, tem,
10773 fold_convert_loc (loc, itype, arg1));
10775 /* Otherwise, for signed (arithmetic) shifts,
10776 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10777 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10778 else if (!TYPE_UNSIGNED (itype))
10779 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10780 arg000, build_int_cst (itype, 0));
10781 /* Otherwise, of unsigned (logical) shifts,
10782 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10783 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10784 else
10785 return omit_one_operand_loc (loc, type,
10786 code == EQ_EXPR ? integer_one_node
10787 : integer_zero_node,
10788 arg000);
10792 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10793 Similarly for NE_EXPR. */
10794 if (TREE_CODE (arg0) == BIT_AND_EXPR
10795 && TREE_CODE (arg1) == INTEGER_CST
10796 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10798 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10799 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10800 TREE_OPERAND (arg0, 1));
10801 tree dandnotc
10802 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10803 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10804 notc);
10805 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10806 if (integer_nonzerop (dandnotc))
10807 return omit_one_operand_loc (loc, type, rslt, arg0);
10810 /* If this is a comparison of a field, we may be able to simplify it. */
10811 if ((TREE_CODE (arg0) == COMPONENT_REF
10812 || TREE_CODE (arg0) == BIT_FIELD_REF)
10813 /* Handle the constant case even without -O
10814 to make sure the warnings are given. */
10815 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10817 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10818 if (t1)
10819 return t1;
10822 /* Optimize comparisons of strlen vs zero to a compare of the
10823 first character of the string vs zero. To wit,
10824 strlen(ptr) == 0 => *ptr == 0
10825 strlen(ptr) != 0 => *ptr != 0
10826 Other cases should reduce to one of these two (or a constant)
10827 due to the return value of strlen being unsigned. */
10828 if (TREE_CODE (arg0) == CALL_EXPR
10829 && integer_zerop (arg1))
10831 tree fndecl = get_callee_fndecl (arg0);
10833 if (fndecl
10834 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10835 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10836 && call_expr_nargs (arg0) == 1
10837 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10839 tree iref = build_fold_indirect_ref_loc (loc,
10840 CALL_EXPR_ARG (arg0, 0));
10841 return fold_build2_loc (loc, code, type, iref,
10842 build_int_cst (TREE_TYPE (iref), 0));
10846 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10847 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10848 if (TREE_CODE (arg0) == RSHIFT_EXPR
10849 && integer_zerop (arg1)
10850 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10852 tree arg00 = TREE_OPERAND (arg0, 0);
10853 tree arg01 = TREE_OPERAND (arg0, 1);
10854 tree itype = TREE_TYPE (arg00);
10855 if (wi::eq_p (arg01, element_precision (itype) - 1))
10857 if (TYPE_UNSIGNED (itype))
10859 itype = signed_type_for (itype);
10860 arg00 = fold_convert_loc (loc, itype, arg00);
10862 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10863 type, arg00, build_zero_cst (itype));
10867 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10868 (X & C) == 0 when C is a single bit. */
10869 if (TREE_CODE (arg0) == BIT_AND_EXPR
10870 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10871 && integer_zerop (arg1)
10872 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10874 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10875 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10876 TREE_OPERAND (arg0, 1));
10877 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10878 type, tem,
10879 fold_convert_loc (loc, TREE_TYPE (arg0),
10880 arg1));
10883 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10884 constant C is a power of two, i.e. a single bit. */
10885 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10886 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10887 && integer_zerop (arg1)
10888 && integer_pow2p (TREE_OPERAND (arg0, 1))
10889 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10890 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10892 tree arg00 = TREE_OPERAND (arg0, 0);
10893 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10894 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10897 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10898 when is C is a power of two, i.e. a single bit. */
10899 if (TREE_CODE (arg0) == BIT_AND_EXPR
10900 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10901 && integer_zerop (arg1)
10902 && integer_pow2p (TREE_OPERAND (arg0, 1))
10903 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10904 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10906 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10907 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10908 arg000, TREE_OPERAND (arg0, 1));
10909 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10910 tem, build_int_cst (TREE_TYPE (tem), 0));
10913 if (integer_zerop (arg1)
10914 && tree_expr_nonzero_p (arg0))
10916 tree res = constant_boolean_node (code==NE_EXPR, type);
10917 return omit_one_operand_loc (loc, type, res, arg0);
10920 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10921 if (TREE_CODE (arg0) == BIT_AND_EXPR
10922 && TREE_CODE (arg1) == BIT_AND_EXPR)
10924 tree arg00 = TREE_OPERAND (arg0, 0);
10925 tree arg01 = TREE_OPERAND (arg0, 1);
10926 tree arg10 = TREE_OPERAND (arg1, 0);
10927 tree arg11 = TREE_OPERAND (arg1, 1);
10928 tree itype = TREE_TYPE (arg0);
10930 if (operand_equal_p (arg01, arg11, 0))
10931 return fold_build2_loc (loc, code, type,
10932 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10933 fold_build2_loc (loc,
10934 BIT_XOR_EXPR, itype,
10935 arg00, arg10),
10936 arg01),
10937 build_zero_cst (itype));
10939 if (operand_equal_p (arg01, arg10, 0))
10940 return fold_build2_loc (loc, code, type,
10941 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10942 fold_build2_loc (loc,
10943 BIT_XOR_EXPR, itype,
10944 arg00, arg11),
10945 arg01),
10946 build_zero_cst (itype));
10948 if (operand_equal_p (arg00, arg11, 0))
10949 return fold_build2_loc (loc, code, type,
10950 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10951 fold_build2_loc (loc,
10952 BIT_XOR_EXPR, itype,
10953 arg01, arg10),
10954 arg00),
10955 build_zero_cst (itype));
10957 if (operand_equal_p (arg00, arg10, 0))
10958 return fold_build2_loc (loc, code, type,
10959 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10960 fold_build2_loc (loc,
10961 BIT_XOR_EXPR, itype,
10962 arg01, arg11),
10963 arg00),
10964 build_zero_cst (itype));
10967 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10968 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10970 tree arg00 = TREE_OPERAND (arg0, 0);
10971 tree arg01 = TREE_OPERAND (arg0, 1);
10972 tree arg10 = TREE_OPERAND (arg1, 0);
10973 tree arg11 = TREE_OPERAND (arg1, 1);
10974 tree itype = TREE_TYPE (arg0);
10976 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10977 operand_equal_p guarantees no side-effects so we don't need
10978 to use omit_one_operand on Z. */
10979 if (operand_equal_p (arg01, arg11, 0))
10980 return fold_build2_loc (loc, code, type, arg00,
10981 fold_convert_loc (loc, TREE_TYPE (arg00),
10982 arg10));
10983 if (operand_equal_p (arg01, arg10, 0))
10984 return fold_build2_loc (loc, code, type, arg00,
10985 fold_convert_loc (loc, TREE_TYPE (arg00),
10986 arg11));
10987 if (operand_equal_p (arg00, arg11, 0))
10988 return fold_build2_loc (loc, code, type, arg01,
10989 fold_convert_loc (loc, TREE_TYPE (arg01),
10990 arg10));
10991 if (operand_equal_p (arg00, arg10, 0))
10992 return fold_build2_loc (loc, code, type, arg01,
10993 fold_convert_loc (loc, TREE_TYPE (arg01),
10994 arg11));
10996 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10997 if (TREE_CODE (arg01) == INTEGER_CST
10998 && TREE_CODE (arg11) == INTEGER_CST)
11000 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
11001 fold_convert_loc (loc, itype, arg11));
11002 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
11003 return fold_build2_loc (loc, code, type, tem,
11004 fold_convert_loc (loc, itype, arg10));
11008 /* Attempt to simplify equality/inequality comparisons of complex
11009 values. Only lower the comparison if the result is known or
11010 can be simplified to a single scalar comparison. */
11011 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11012 || TREE_CODE (arg0) == COMPLEX_CST)
11013 && (TREE_CODE (arg1) == COMPLEX_EXPR
11014 || TREE_CODE (arg1) == COMPLEX_CST))
11016 tree real0, imag0, real1, imag1;
11017 tree rcond, icond;
11019 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11021 real0 = TREE_OPERAND (arg0, 0);
11022 imag0 = TREE_OPERAND (arg0, 1);
11024 else
11026 real0 = TREE_REALPART (arg0);
11027 imag0 = TREE_IMAGPART (arg0);
11030 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11032 real1 = TREE_OPERAND (arg1, 0);
11033 imag1 = TREE_OPERAND (arg1, 1);
11035 else
11037 real1 = TREE_REALPART (arg1);
11038 imag1 = TREE_IMAGPART (arg1);
11041 rcond = fold_binary_loc (loc, code, type, real0, real1);
11042 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11044 if (integer_zerop (rcond))
11046 if (code == EQ_EXPR)
11047 return omit_two_operands_loc (loc, type, boolean_false_node,
11048 imag0, imag1);
11049 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11051 else
11053 if (code == NE_EXPR)
11054 return omit_two_operands_loc (loc, type, boolean_true_node,
11055 imag0, imag1);
11056 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11060 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11061 if (icond && TREE_CODE (icond) == INTEGER_CST)
11063 if (integer_zerop (icond))
11065 if (code == EQ_EXPR)
11066 return omit_two_operands_loc (loc, type, boolean_false_node,
11067 real0, real1);
11068 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11070 else
11072 if (code == NE_EXPR)
11073 return omit_two_operands_loc (loc, type, boolean_true_node,
11074 real0, real1);
11075 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11080 return NULL_TREE;
11082 case LT_EXPR:
11083 case GT_EXPR:
11084 case LE_EXPR:
11085 case GE_EXPR:
11086 tem = fold_comparison (loc, code, type, op0, op1);
11087 if (tem != NULL_TREE)
11088 return tem;
11090 /* Transform comparisons of the form X +- C CMP X. */
11091 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11092 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11093 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11094 && !HONOR_SNANS (arg0))
11095 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11096 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11098 tree arg01 = TREE_OPERAND (arg0, 1);
11099 enum tree_code code0 = TREE_CODE (arg0);
11100 int is_positive;
11102 if (TREE_CODE (arg01) == REAL_CST)
11103 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11104 else
11105 is_positive = tree_int_cst_sgn (arg01);
11107 /* (X - c) > X becomes false. */
11108 if (code == GT_EXPR
11109 && ((code0 == MINUS_EXPR && is_positive >= 0)
11110 || (code0 == PLUS_EXPR && is_positive <= 0)))
11112 if (TREE_CODE (arg01) == INTEGER_CST
11113 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11114 fold_overflow_warning (("assuming signed overflow does not "
11115 "occur when assuming that (X - c) > X "
11116 "is always false"),
11117 WARN_STRICT_OVERFLOW_ALL);
11118 return constant_boolean_node (0, type);
11121 /* Likewise (X + c) < X becomes false. */
11122 if (code == LT_EXPR
11123 && ((code0 == PLUS_EXPR && is_positive >= 0)
11124 || (code0 == MINUS_EXPR && is_positive <= 0)))
11126 if (TREE_CODE (arg01) == INTEGER_CST
11127 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11128 fold_overflow_warning (("assuming signed overflow does not "
11129 "occur when assuming that "
11130 "(X + c) < X is always false"),
11131 WARN_STRICT_OVERFLOW_ALL);
11132 return constant_boolean_node (0, type);
11135 /* Convert (X - c) <= X to true. */
11136 if (!HONOR_NANS (arg1)
11137 && code == LE_EXPR
11138 && ((code0 == MINUS_EXPR && is_positive >= 0)
11139 || (code0 == PLUS_EXPR && is_positive <= 0)))
11141 if (TREE_CODE (arg01) == INTEGER_CST
11142 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11143 fold_overflow_warning (("assuming signed overflow does not "
11144 "occur when assuming that "
11145 "(X - c) <= X is always true"),
11146 WARN_STRICT_OVERFLOW_ALL);
11147 return constant_boolean_node (1, type);
11150 /* Convert (X + c) >= X to true. */
11151 if (!HONOR_NANS (arg1)
11152 && code == GE_EXPR
11153 && ((code0 == PLUS_EXPR && is_positive >= 0)
11154 || (code0 == MINUS_EXPR && is_positive <= 0)))
11156 if (TREE_CODE (arg01) == INTEGER_CST
11157 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11158 fold_overflow_warning (("assuming signed overflow does not "
11159 "occur when assuming that "
11160 "(X + c) >= X is always true"),
11161 WARN_STRICT_OVERFLOW_ALL);
11162 return constant_boolean_node (1, type);
11165 if (TREE_CODE (arg01) == INTEGER_CST)
11167 /* Convert X + c > X and X - c < X to true for integers. */
11168 if (code == GT_EXPR
11169 && ((code0 == PLUS_EXPR && is_positive > 0)
11170 || (code0 == MINUS_EXPR && is_positive < 0)))
11172 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11173 fold_overflow_warning (("assuming signed overflow does "
11174 "not occur when assuming that "
11175 "(X + c) > X is always true"),
11176 WARN_STRICT_OVERFLOW_ALL);
11177 return constant_boolean_node (1, type);
11180 if (code == LT_EXPR
11181 && ((code0 == MINUS_EXPR && is_positive > 0)
11182 || (code0 == PLUS_EXPR && is_positive < 0)))
11184 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11185 fold_overflow_warning (("assuming signed overflow does "
11186 "not occur when assuming that "
11187 "(X - c) < X is always true"),
11188 WARN_STRICT_OVERFLOW_ALL);
11189 return constant_boolean_node (1, type);
11192 /* Convert X + c <= X and X - c >= X to false for integers. */
11193 if (code == LE_EXPR
11194 && ((code0 == PLUS_EXPR && is_positive > 0)
11195 || (code0 == MINUS_EXPR && is_positive < 0)))
11197 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11198 fold_overflow_warning (("assuming signed overflow does "
11199 "not occur when assuming that "
11200 "(X + c) <= X is always false"),
11201 WARN_STRICT_OVERFLOW_ALL);
11202 return constant_boolean_node (0, type);
11205 if (code == GE_EXPR
11206 && ((code0 == MINUS_EXPR && is_positive > 0)
11207 || (code0 == PLUS_EXPR && is_positive < 0)))
11209 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11210 fold_overflow_warning (("assuming signed overflow does "
11211 "not occur when assuming that "
11212 "(X - c) >= X is always false"),
11213 WARN_STRICT_OVERFLOW_ALL);
11214 return constant_boolean_node (0, type);
11219 /* If we are comparing an ABS_EXPR with a constant, we can
11220 convert all the cases into explicit comparisons, but they may
11221 well not be faster than doing the ABS and one comparison.
11222 But ABS (X) <= C is a range comparison, which becomes a subtraction
11223 and a comparison, and is probably faster. */
11224 if (code == LE_EXPR
11225 && TREE_CODE (arg1) == INTEGER_CST
11226 && TREE_CODE (arg0) == ABS_EXPR
11227 && ! TREE_SIDE_EFFECTS (arg0)
11228 && (0 != (tem = negate_expr (arg1)))
11229 && TREE_CODE (tem) == INTEGER_CST
11230 && !TREE_OVERFLOW (tem))
11231 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11232 build2 (GE_EXPR, type,
11233 TREE_OPERAND (arg0, 0), tem),
11234 build2 (LE_EXPR, type,
11235 TREE_OPERAND (arg0, 0), arg1));
11237 /* Convert ABS_EXPR<x> >= 0 to true. */
11238 strict_overflow_p = false;
11239 if (code == GE_EXPR
11240 && (integer_zerop (arg1)
11241 || (! HONOR_NANS (arg0)
11242 && real_zerop (arg1)))
11243 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11245 if (strict_overflow_p)
11246 fold_overflow_warning (("assuming signed overflow does not occur "
11247 "when simplifying comparison of "
11248 "absolute value and zero"),
11249 WARN_STRICT_OVERFLOW_CONDITIONAL);
11250 return omit_one_operand_loc (loc, type,
11251 constant_boolean_node (true, type),
11252 arg0);
11255 /* Convert ABS_EXPR<x> < 0 to false. */
11256 strict_overflow_p = false;
11257 if (code == LT_EXPR
11258 && (integer_zerop (arg1) || real_zerop (arg1))
11259 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11261 if (strict_overflow_p)
11262 fold_overflow_warning (("assuming signed overflow does not occur "
11263 "when simplifying comparison of "
11264 "absolute value and zero"),
11265 WARN_STRICT_OVERFLOW_CONDITIONAL);
11266 return omit_one_operand_loc (loc, type,
11267 constant_boolean_node (false, type),
11268 arg0);
11271 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11272 and similarly for >= into !=. */
11273 if ((code == LT_EXPR || code == GE_EXPR)
11274 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11275 && TREE_CODE (arg1) == LSHIFT_EXPR
11276 && integer_onep (TREE_OPERAND (arg1, 0)))
11277 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11278 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11279 TREE_OPERAND (arg1, 1)),
11280 build_zero_cst (TREE_TYPE (arg0)));
11282 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11283 otherwise Y might be >= # of bits in X's type and thus e.g.
11284 (unsigned char) (1 << Y) for Y 15 might be 0.
11285 If the cast is widening, then 1 << Y should have unsigned type,
11286 otherwise if Y is number of bits in the signed shift type minus 1,
11287 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11288 31 might be 0xffffffff80000000. */
11289 if ((code == LT_EXPR || code == GE_EXPR)
11290 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11291 && CONVERT_EXPR_P (arg1)
11292 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11293 && (element_precision (TREE_TYPE (arg1))
11294 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11295 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11296 || (element_precision (TREE_TYPE (arg1))
11297 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11298 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11300 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11301 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11302 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11303 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11304 build_zero_cst (TREE_TYPE (arg0)));
11307 return NULL_TREE;
11309 case UNORDERED_EXPR:
11310 case ORDERED_EXPR:
11311 case UNLT_EXPR:
11312 case UNLE_EXPR:
11313 case UNGT_EXPR:
11314 case UNGE_EXPR:
11315 case UNEQ_EXPR:
11316 case LTGT_EXPR:
11317 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11319 tree targ0 = strip_float_extensions (arg0);
11320 tree targ1 = strip_float_extensions (arg1);
11321 tree newtype = TREE_TYPE (targ0);
11323 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11324 newtype = TREE_TYPE (targ1);
11326 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11327 return fold_build2_loc (loc, code, type,
11328 fold_convert_loc (loc, newtype, targ0),
11329 fold_convert_loc (loc, newtype, targ1));
11332 return NULL_TREE;
11334 case COMPOUND_EXPR:
11335 /* When pedantic, a compound expression can be neither an lvalue
11336 nor an integer constant expression. */
11337 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11338 return NULL_TREE;
11339 /* Don't let (0, 0) be null pointer constant. */
11340 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11341 : fold_convert_loc (loc, type, arg1);
11342 return pedantic_non_lvalue_loc (loc, tem);
11344 case ASSERT_EXPR:
11345 /* An ASSERT_EXPR should never be passed to fold_binary. */
11346 gcc_unreachable ();
11348 default:
11349 return NULL_TREE;
11350 } /* switch (code) */
11353 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11354 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11355 of GOTO_EXPR. */
11357 static tree
11358 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11360 switch (TREE_CODE (*tp))
11362 case LABEL_EXPR:
11363 return *tp;
11365 case GOTO_EXPR:
11366 *walk_subtrees = 0;
11368 /* ... fall through ... */
11370 default:
11371 return NULL_TREE;
11375 /* Return whether the sub-tree ST contains a label which is accessible from
11376 outside the sub-tree. */
11378 static bool
11379 contains_label_p (tree st)
11381 return
11382 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11385 /* Fold a ternary expression of code CODE and type TYPE with operands
11386 OP0, OP1, and OP2. Return the folded expression if folding is
11387 successful. Otherwise, return NULL_TREE. */
11389 tree
11390 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11391 tree op0, tree op1, tree op2)
11393 tree tem;
11394 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11395 enum tree_code_class kind = TREE_CODE_CLASS (code);
11397 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11398 && TREE_CODE_LENGTH (code) == 3);
11400 /* If this is a commutative operation, and OP0 is a constant, move it
11401 to OP1 to reduce the number of tests below. */
11402 if (commutative_ternary_tree_code (code)
11403 && tree_swap_operands_p (op0, op1, true))
11404 return fold_build3_loc (loc, code, type, op1, op0, op2);
11406 tem = generic_simplify (loc, code, type, op0, op1, op2);
11407 if (tem)
11408 return tem;
11410 /* Strip any conversions that don't change the mode. This is safe
11411 for every expression, except for a comparison expression because
11412 its signedness is derived from its operands. So, in the latter
11413 case, only strip conversions that don't change the signedness.
11415 Note that this is done as an internal manipulation within the
11416 constant folder, in order to find the simplest representation of
11417 the arguments so that their form can be studied. In any cases,
11418 the appropriate type conversions should be put back in the tree
11419 that will get out of the constant folder. */
11420 if (op0)
11422 arg0 = op0;
11423 STRIP_NOPS (arg0);
11426 if (op1)
11428 arg1 = op1;
11429 STRIP_NOPS (arg1);
11432 if (op2)
11434 arg2 = op2;
11435 STRIP_NOPS (arg2);
11438 switch (code)
11440 case COMPONENT_REF:
11441 if (TREE_CODE (arg0) == CONSTRUCTOR
11442 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11444 unsigned HOST_WIDE_INT idx;
11445 tree field, value;
11446 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11447 if (field == arg1)
11448 return value;
11450 return NULL_TREE;
11452 case COND_EXPR:
11453 case VEC_COND_EXPR:
11454 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11455 so all simple results must be passed through pedantic_non_lvalue. */
11456 if (TREE_CODE (arg0) == INTEGER_CST)
11458 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11459 tem = integer_zerop (arg0) ? op2 : op1;
11460 /* Only optimize constant conditions when the selected branch
11461 has the same type as the COND_EXPR. This avoids optimizing
11462 away "c ? x : throw", where the throw has a void type.
11463 Avoid throwing away that operand which contains label. */
11464 if ((!TREE_SIDE_EFFECTS (unused_op)
11465 || !contains_label_p (unused_op))
11466 && (! VOID_TYPE_P (TREE_TYPE (tem))
11467 || VOID_TYPE_P (type)))
11468 return pedantic_non_lvalue_loc (loc, tem);
11469 return NULL_TREE;
11471 else if (TREE_CODE (arg0) == VECTOR_CST)
11473 if ((TREE_CODE (arg1) == VECTOR_CST
11474 || TREE_CODE (arg1) == CONSTRUCTOR)
11475 && (TREE_CODE (arg2) == VECTOR_CST
11476 || TREE_CODE (arg2) == CONSTRUCTOR))
11478 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11479 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11480 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11481 for (i = 0; i < nelts; i++)
11483 tree val = VECTOR_CST_ELT (arg0, i);
11484 if (integer_all_onesp (val))
11485 sel[i] = i;
11486 else if (integer_zerop (val))
11487 sel[i] = nelts + i;
11488 else /* Currently unreachable. */
11489 return NULL_TREE;
11491 tree t = fold_vec_perm (type, arg1, arg2, sel);
11492 if (t != NULL_TREE)
11493 return t;
11497 /* If we have A op B ? A : C, we may be able to convert this to a
11498 simpler expression, depending on the operation and the values
11499 of B and C. Signed zeros prevent all of these transformations,
11500 for reasons given above each one.
11502 Also try swapping the arguments and inverting the conditional. */
11503 if (COMPARISON_CLASS_P (arg0)
11504 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11505 arg1, TREE_OPERAND (arg0, 1))
11506 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11508 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11509 if (tem)
11510 return tem;
11513 if (COMPARISON_CLASS_P (arg0)
11514 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11515 op2,
11516 TREE_OPERAND (arg0, 1))
11517 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11519 location_t loc0 = expr_location_or (arg0, loc);
11520 tem = fold_invert_truthvalue (loc0, arg0);
11521 if (tem && COMPARISON_CLASS_P (tem))
11523 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11524 if (tem)
11525 return tem;
11529 /* If the second operand is simpler than the third, swap them
11530 since that produces better jump optimization results. */
11531 if (truth_value_p (TREE_CODE (arg0))
11532 && tree_swap_operands_p (op1, op2, false))
11534 location_t loc0 = expr_location_or (arg0, loc);
11535 /* See if this can be inverted. If it can't, possibly because
11536 it was a floating-point inequality comparison, don't do
11537 anything. */
11538 tem = fold_invert_truthvalue (loc0, arg0);
11539 if (tem)
11540 return fold_build3_loc (loc, code, type, tem, op2, op1);
11543 /* Convert A ? 1 : 0 to simply A. */
11544 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11545 : (integer_onep (op1)
11546 && !VECTOR_TYPE_P (type)))
11547 && integer_zerop (op2)
11548 /* If we try to convert OP0 to our type, the
11549 call to fold will try to move the conversion inside
11550 a COND, which will recurse. In that case, the COND_EXPR
11551 is probably the best choice, so leave it alone. */
11552 && type == TREE_TYPE (arg0))
11553 return pedantic_non_lvalue_loc (loc, arg0);
11555 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11556 over COND_EXPR in cases such as floating point comparisons. */
11557 if (integer_zerop (op1)
11558 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
11559 : (integer_onep (op2)
11560 && !VECTOR_TYPE_P (type)))
11561 && truth_value_p (TREE_CODE (arg0)))
11562 return pedantic_non_lvalue_loc (loc,
11563 fold_convert_loc (loc, type,
11564 invert_truthvalue_loc (loc,
11565 arg0)));
11567 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11568 if (TREE_CODE (arg0) == LT_EXPR
11569 && integer_zerop (TREE_OPERAND (arg0, 1))
11570 && integer_zerop (op2)
11571 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11573 /* sign_bit_p looks through both zero and sign extensions,
11574 but for this optimization only sign extensions are
11575 usable. */
11576 tree tem2 = TREE_OPERAND (arg0, 0);
11577 while (tem != tem2)
11579 if (TREE_CODE (tem2) != NOP_EXPR
11580 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11582 tem = NULL_TREE;
11583 break;
11585 tem2 = TREE_OPERAND (tem2, 0);
11587 /* sign_bit_p only checks ARG1 bits within A's precision.
11588 If <sign bit of A> has wider type than A, bits outside
11589 of A's precision in <sign bit of A> need to be checked.
11590 If they are all 0, this optimization needs to be done
11591 in unsigned A's type, if they are all 1 in signed A's type,
11592 otherwise this can't be done. */
11593 if (tem
11594 && TYPE_PRECISION (TREE_TYPE (tem))
11595 < TYPE_PRECISION (TREE_TYPE (arg1))
11596 && TYPE_PRECISION (TREE_TYPE (tem))
11597 < TYPE_PRECISION (type))
11599 int inner_width, outer_width;
11600 tree tem_type;
11602 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11603 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11604 if (outer_width > TYPE_PRECISION (type))
11605 outer_width = TYPE_PRECISION (type);
11607 wide_int mask = wi::shifted_mask
11608 (inner_width, outer_width - inner_width, false,
11609 TYPE_PRECISION (TREE_TYPE (arg1)));
11611 wide_int common = mask & arg1;
11612 if (common == mask)
11614 tem_type = signed_type_for (TREE_TYPE (tem));
11615 tem = fold_convert_loc (loc, tem_type, tem);
11617 else if (common == 0)
11619 tem_type = unsigned_type_for (TREE_TYPE (tem));
11620 tem = fold_convert_loc (loc, tem_type, tem);
11622 else
11623 tem = NULL;
11626 if (tem)
11627 return
11628 fold_convert_loc (loc, type,
11629 fold_build2_loc (loc, BIT_AND_EXPR,
11630 TREE_TYPE (tem), tem,
11631 fold_convert_loc (loc,
11632 TREE_TYPE (tem),
11633 arg1)));
11636 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11637 already handled above. */
11638 if (TREE_CODE (arg0) == BIT_AND_EXPR
11639 && integer_onep (TREE_OPERAND (arg0, 1))
11640 && integer_zerop (op2)
11641 && integer_pow2p (arg1))
11643 tree tem = TREE_OPERAND (arg0, 0);
11644 STRIP_NOPS (tem);
11645 if (TREE_CODE (tem) == RSHIFT_EXPR
11646 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11647 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11648 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11649 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11650 TREE_OPERAND (tem, 0), arg1);
11653 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11654 is probably obsolete because the first operand should be a
11655 truth value (that's why we have the two cases above), but let's
11656 leave it in until we can confirm this for all front-ends. */
11657 if (integer_zerop (op2)
11658 && TREE_CODE (arg0) == NE_EXPR
11659 && integer_zerop (TREE_OPERAND (arg0, 1))
11660 && integer_pow2p (arg1)
11661 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11662 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11663 arg1, OEP_ONLY_CONST))
11664 return pedantic_non_lvalue_loc (loc,
11665 fold_convert_loc (loc, type,
11666 TREE_OPERAND (arg0, 0)));
11668 /* Disable the transformations below for vectors, since
11669 fold_binary_op_with_conditional_arg may undo them immediately,
11670 yielding an infinite loop. */
11671 if (code == VEC_COND_EXPR)
11672 return NULL_TREE;
11674 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11675 if (integer_zerop (op2)
11676 && truth_value_p (TREE_CODE (arg0))
11677 && truth_value_p (TREE_CODE (arg1))
11678 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11679 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11680 : TRUTH_ANDIF_EXPR,
11681 type, fold_convert_loc (loc, type, arg0), arg1);
11683 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11684 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11685 && truth_value_p (TREE_CODE (arg0))
11686 && truth_value_p (TREE_CODE (arg1))
11687 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11689 location_t loc0 = expr_location_or (arg0, loc);
11690 /* Only perform transformation if ARG0 is easily inverted. */
11691 tem = fold_invert_truthvalue (loc0, arg0);
11692 if (tem)
11693 return fold_build2_loc (loc, code == VEC_COND_EXPR
11694 ? BIT_IOR_EXPR
11695 : TRUTH_ORIF_EXPR,
11696 type, fold_convert_loc (loc, type, tem),
11697 arg1);
11700 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11701 if (integer_zerop (arg1)
11702 && truth_value_p (TREE_CODE (arg0))
11703 && truth_value_p (TREE_CODE (op2))
11704 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11706 location_t loc0 = expr_location_or (arg0, loc);
11707 /* Only perform transformation if ARG0 is easily inverted. */
11708 tem = fold_invert_truthvalue (loc0, arg0);
11709 if (tem)
11710 return fold_build2_loc (loc, code == VEC_COND_EXPR
11711 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11712 type, fold_convert_loc (loc, type, tem),
11713 op2);
11716 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11717 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11718 && truth_value_p (TREE_CODE (arg0))
11719 && truth_value_p (TREE_CODE (op2))
11720 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11721 return fold_build2_loc (loc, code == VEC_COND_EXPR
11722 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11723 type, fold_convert_loc (loc, type, arg0), op2);
11725 return NULL_TREE;
11727 case CALL_EXPR:
11728 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11729 of fold_ternary on them. */
11730 gcc_unreachable ();
11732 case BIT_FIELD_REF:
11733 if ((TREE_CODE (arg0) == VECTOR_CST
11734 || (TREE_CODE (arg0) == CONSTRUCTOR
11735 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
11736 && (type == TREE_TYPE (TREE_TYPE (arg0))
11737 || (TREE_CODE (type) == VECTOR_TYPE
11738 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11740 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11741 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11742 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11743 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11745 if (n != 0
11746 && (idx % width) == 0
11747 && (n % width) == 0
11748 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11750 idx = idx / width;
11751 n = n / width;
11753 if (TREE_CODE (arg0) == VECTOR_CST)
11755 if (n == 1)
11756 return VECTOR_CST_ELT (arg0, idx);
11758 tree *vals = XALLOCAVEC (tree, n);
11759 for (unsigned i = 0; i < n; ++i)
11760 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11761 return build_vector (type, vals);
11764 /* Constructor elements can be subvectors. */
11765 unsigned HOST_WIDE_INT k = 1;
11766 if (CONSTRUCTOR_NELTS (arg0) != 0)
11768 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
11769 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
11770 k = TYPE_VECTOR_SUBPARTS (cons_elem);
11773 /* We keep an exact subset of the constructor elements. */
11774 if ((idx % k) == 0 && (n % k) == 0)
11776 if (CONSTRUCTOR_NELTS (arg0) == 0)
11777 return build_constructor (type, NULL);
11778 idx /= k;
11779 n /= k;
11780 if (n == 1)
11782 if (idx < CONSTRUCTOR_NELTS (arg0))
11783 return CONSTRUCTOR_ELT (arg0, idx)->value;
11784 return build_zero_cst (type);
11787 vec<constructor_elt, va_gc> *vals;
11788 vec_alloc (vals, n);
11789 for (unsigned i = 0;
11790 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
11791 ++i)
11792 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
11793 CONSTRUCTOR_ELT
11794 (arg0, idx + i)->value);
11795 return build_constructor (type, vals);
11797 /* The bitfield references a single constructor element. */
11798 else if (idx + n <= (idx / k + 1) * k)
11800 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
11801 return build_zero_cst (type);
11802 else if (n == k)
11803 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
11804 else
11805 return fold_build3_loc (loc, code, type,
11806 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
11807 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
11812 /* A bit-field-ref that referenced the full argument can be stripped. */
11813 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11814 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
11815 && integer_zerop (op2))
11816 return fold_convert_loc (loc, type, arg0);
11818 /* On constants we can use native encode/interpret to constant
11819 fold (nearly) all BIT_FIELD_REFs. */
11820 if (CONSTANT_CLASS_P (arg0)
11821 && can_native_interpret_type_p (type)
11822 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
11823 /* This limitation should not be necessary, we just need to
11824 round this up to mode size. */
11825 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
11826 /* Need bit-shifting of the buffer to relax the following. */
11827 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
11829 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11830 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11831 unsigned HOST_WIDE_INT clen;
11832 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
11833 /* ??? We cannot tell native_encode_expr to start at
11834 some random byte only. So limit us to a reasonable amount
11835 of work. */
11836 if (clen <= 4096)
11838 unsigned char *b = XALLOCAVEC (unsigned char, clen);
11839 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
11840 if (len > 0
11841 && len * BITS_PER_UNIT >= bitpos + bitsize)
11843 tree v = native_interpret_expr (type,
11844 b + bitpos / BITS_PER_UNIT,
11845 bitsize / BITS_PER_UNIT);
11846 if (v)
11847 return v;
11852 return NULL_TREE;
11854 case FMA_EXPR:
11855 /* For integers we can decompose the FMA if possible. */
11856 if (TREE_CODE (arg0) == INTEGER_CST
11857 && TREE_CODE (arg1) == INTEGER_CST)
11858 return fold_build2_loc (loc, PLUS_EXPR, type,
11859 const_binop (MULT_EXPR, arg0, arg1), arg2);
11860 if (integer_zerop (arg2))
11861 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11863 return fold_fma (loc, type, arg0, arg1, arg2);
11865 case VEC_PERM_EXPR:
11866 if (TREE_CODE (arg2) == VECTOR_CST)
11868 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11869 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11870 unsigned char *sel2 = sel + nelts;
11871 bool need_mask_canon = false;
11872 bool need_mask_canon2 = false;
11873 bool all_in_vec0 = true;
11874 bool all_in_vec1 = true;
11875 bool maybe_identity = true;
11876 bool single_arg = (op0 == op1);
11877 bool changed = false;
11879 mask2 = 2 * nelts - 1;
11880 mask = single_arg ? (nelts - 1) : mask2;
11881 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11882 for (i = 0; i < nelts; i++)
11884 tree val = VECTOR_CST_ELT (arg2, i);
11885 if (TREE_CODE (val) != INTEGER_CST)
11886 return NULL_TREE;
11888 /* Make sure that the perm value is in an acceptable
11889 range. */
11890 wide_int t = val;
11891 need_mask_canon |= wi::gtu_p (t, mask);
11892 need_mask_canon2 |= wi::gtu_p (t, mask2);
11893 sel[i] = t.to_uhwi () & mask;
11894 sel2[i] = t.to_uhwi () & mask2;
11896 if (sel[i] < nelts)
11897 all_in_vec1 = false;
11898 else
11899 all_in_vec0 = false;
11901 if ((sel[i] & (nelts-1)) != i)
11902 maybe_identity = false;
11905 if (maybe_identity)
11907 if (all_in_vec0)
11908 return op0;
11909 if (all_in_vec1)
11910 return op1;
11913 if (all_in_vec0)
11914 op1 = op0;
11915 else if (all_in_vec1)
11917 op0 = op1;
11918 for (i = 0; i < nelts; i++)
11919 sel[i] -= nelts;
11920 need_mask_canon = true;
11923 if ((TREE_CODE (op0) == VECTOR_CST
11924 || TREE_CODE (op0) == CONSTRUCTOR)
11925 && (TREE_CODE (op1) == VECTOR_CST
11926 || TREE_CODE (op1) == CONSTRUCTOR))
11928 tree t = fold_vec_perm (type, op0, op1, sel);
11929 if (t != NULL_TREE)
11930 return t;
11933 if (op0 == op1 && !single_arg)
11934 changed = true;
11936 /* Some targets are deficient and fail to expand a single
11937 argument permutation while still allowing an equivalent
11938 2-argument version. */
11939 if (need_mask_canon && arg2 == op2
11940 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11941 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11943 need_mask_canon = need_mask_canon2;
11944 sel = sel2;
11947 if (need_mask_canon && arg2 == op2)
11949 tree *tsel = XALLOCAVEC (tree, nelts);
11950 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11951 for (i = 0; i < nelts; i++)
11952 tsel[i] = build_int_cst (eltype, sel[i]);
11953 op2 = build_vector (TREE_TYPE (arg2), tsel);
11954 changed = true;
11957 if (changed)
11958 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11960 return NULL_TREE;
11962 default:
11963 return NULL_TREE;
11964 } /* switch (code) */
11967 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11968 of an array (or vector). */
11970 tree
11971 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11973 tree index_type = NULL_TREE;
11974 offset_int low_bound = 0;
11976 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11978 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11979 if (domain_type && TYPE_MIN_VALUE (domain_type))
11981 /* Static constructors for variably sized objects makes no sense. */
11982 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11983 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11984 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11988 if (index_type)
11989 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11990 TYPE_SIGN (index_type));
11992 offset_int index = low_bound - 1;
11993 if (index_type)
11994 index = wi::ext (index, TYPE_PRECISION (index_type),
11995 TYPE_SIGN (index_type));
11997 offset_int max_index;
11998 unsigned HOST_WIDE_INT cnt;
11999 tree cfield, cval;
12001 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
12003 /* Array constructor might explicitly set index, or specify a range,
12004 or leave index NULL meaning that it is next index after previous
12005 one. */
12006 if (cfield)
12008 if (TREE_CODE (cfield) == INTEGER_CST)
12009 max_index = index = wi::to_offset (cfield);
12010 else
12012 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
12013 index = wi::to_offset (TREE_OPERAND (cfield, 0));
12014 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
12017 else
12019 index += 1;
12020 if (index_type)
12021 index = wi::ext (index, TYPE_PRECISION (index_type),
12022 TYPE_SIGN (index_type));
12023 max_index = index;
12026 /* Do we have match? */
12027 if (wi::cmpu (access_index, index) >= 0
12028 && wi::cmpu (access_index, max_index) <= 0)
12029 return cval;
12031 return NULL_TREE;
12034 /* Perform constant folding and related simplification of EXPR.
12035 The related simplifications include x*1 => x, x*0 => 0, etc.,
12036 and application of the associative law.
12037 NOP_EXPR conversions may be removed freely (as long as we
12038 are careful not to change the type of the overall expression).
12039 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
12040 but we can constant-fold them if they have constant operands. */
12042 #ifdef ENABLE_FOLD_CHECKING
12043 # define fold(x) fold_1 (x)
12044 static tree fold_1 (tree);
12045 static
12046 #endif
12047 tree
12048 fold (tree expr)
12050 const tree t = expr;
12051 enum tree_code code = TREE_CODE (t);
12052 enum tree_code_class kind = TREE_CODE_CLASS (code);
12053 tree tem;
12054 location_t loc = EXPR_LOCATION (expr);
12056 /* Return right away if a constant. */
12057 if (kind == tcc_constant)
12058 return t;
12060 /* CALL_EXPR-like objects with variable numbers of operands are
12061 treated specially. */
12062 if (kind == tcc_vl_exp)
12064 if (code == CALL_EXPR)
12066 tem = fold_call_expr (loc, expr, false);
12067 return tem ? tem : expr;
12069 return expr;
12072 if (IS_EXPR_CODE_CLASS (kind))
12074 tree type = TREE_TYPE (t);
12075 tree op0, op1, op2;
12077 switch (TREE_CODE_LENGTH (code))
12079 case 1:
12080 op0 = TREE_OPERAND (t, 0);
12081 tem = fold_unary_loc (loc, code, type, op0);
12082 return tem ? tem : expr;
12083 case 2:
12084 op0 = TREE_OPERAND (t, 0);
12085 op1 = TREE_OPERAND (t, 1);
12086 tem = fold_binary_loc (loc, code, type, op0, op1);
12087 return tem ? tem : expr;
12088 case 3:
12089 op0 = TREE_OPERAND (t, 0);
12090 op1 = TREE_OPERAND (t, 1);
12091 op2 = TREE_OPERAND (t, 2);
12092 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12093 return tem ? tem : expr;
12094 default:
12095 break;
12099 switch (code)
12101 case ARRAY_REF:
12103 tree op0 = TREE_OPERAND (t, 0);
12104 tree op1 = TREE_OPERAND (t, 1);
12106 if (TREE_CODE (op1) == INTEGER_CST
12107 && TREE_CODE (op0) == CONSTRUCTOR
12108 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12110 tree val = get_array_ctor_element_at_index (op0,
12111 wi::to_offset (op1));
12112 if (val)
12113 return val;
12116 return t;
12119 /* Return a VECTOR_CST if possible. */
12120 case CONSTRUCTOR:
12122 tree type = TREE_TYPE (t);
12123 if (TREE_CODE (type) != VECTOR_TYPE)
12124 return t;
12126 unsigned i;
12127 tree val;
12128 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12129 if (! CONSTANT_CLASS_P (val))
12130 return t;
12132 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12135 case CONST_DECL:
12136 return fold (DECL_INITIAL (t));
12138 default:
12139 return t;
12140 } /* switch (code) */
12143 #ifdef ENABLE_FOLD_CHECKING
12144 #undef fold
12146 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12147 hash_table<nofree_ptr_hash<const tree_node> > *);
12148 static void fold_check_failed (const_tree, const_tree);
12149 void print_fold_checksum (const_tree);
12151 /* When --enable-checking=fold, compute a digest of expr before
12152 and after actual fold call to see if fold did not accidentally
12153 change original expr. */
12155 tree
12156 fold (tree expr)
12158 tree ret;
12159 struct md5_ctx ctx;
12160 unsigned char checksum_before[16], checksum_after[16];
12161 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12163 md5_init_ctx (&ctx);
12164 fold_checksum_tree (expr, &ctx, &ht);
12165 md5_finish_ctx (&ctx, checksum_before);
12166 ht.empty ();
12168 ret = fold_1 (expr);
12170 md5_init_ctx (&ctx);
12171 fold_checksum_tree (expr, &ctx, &ht);
12172 md5_finish_ctx (&ctx, checksum_after);
12174 if (memcmp (checksum_before, checksum_after, 16))
12175 fold_check_failed (expr, ret);
12177 return ret;
12180 void
12181 print_fold_checksum (const_tree expr)
12183 struct md5_ctx ctx;
12184 unsigned char checksum[16], cnt;
12185 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12187 md5_init_ctx (&ctx);
12188 fold_checksum_tree (expr, &ctx, &ht);
12189 md5_finish_ctx (&ctx, checksum);
12190 for (cnt = 0; cnt < 16; ++cnt)
12191 fprintf (stderr, "%02x", checksum[cnt]);
12192 putc ('\n', stderr);
12195 static void
12196 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12198 internal_error ("fold check: original tree changed by fold");
12201 static void
12202 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12203 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12205 const tree_node **slot;
12206 enum tree_code code;
12207 union tree_node buf;
12208 int i, len;
12210 recursive_label:
12211 if (expr == NULL)
12212 return;
12213 slot = ht->find_slot (expr, INSERT);
12214 if (*slot != NULL)
12215 return;
12216 *slot = expr;
12217 code = TREE_CODE (expr);
12218 if (TREE_CODE_CLASS (code) == tcc_declaration
12219 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12221 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12222 memcpy ((char *) &buf, expr, tree_size (expr));
12223 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12224 buf.decl_with_vis.symtab_node = NULL;
12225 expr = (tree) &buf;
12227 else if (TREE_CODE_CLASS (code) == tcc_type
12228 && (TYPE_POINTER_TO (expr)
12229 || TYPE_REFERENCE_TO (expr)
12230 || TYPE_CACHED_VALUES_P (expr)
12231 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12232 || TYPE_NEXT_VARIANT (expr)))
12234 /* Allow these fields to be modified. */
12235 tree tmp;
12236 memcpy ((char *) &buf, expr, tree_size (expr));
12237 expr = tmp = (tree) &buf;
12238 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12239 TYPE_POINTER_TO (tmp) = NULL;
12240 TYPE_REFERENCE_TO (tmp) = NULL;
12241 TYPE_NEXT_VARIANT (tmp) = NULL;
12242 if (TYPE_CACHED_VALUES_P (tmp))
12244 TYPE_CACHED_VALUES_P (tmp) = 0;
12245 TYPE_CACHED_VALUES (tmp) = NULL;
12248 md5_process_bytes (expr, tree_size (expr), ctx);
12249 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12250 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12251 if (TREE_CODE_CLASS (code) != tcc_type
12252 && TREE_CODE_CLASS (code) != tcc_declaration
12253 && code != TREE_LIST
12254 && code != SSA_NAME
12255 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12256 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12257 switch (TREE_CODE_CLASS (code))
12259 case tcc_constant:
12260 switch (code)
12262 case STRING_CST:
12263 md5_process_bytes (TREE_STRING_POINTER (expr),
12264 TREE_STRING_LENGTH (expr), ctx);
12265 break;
12266 case COMPLEX_CST:
12267 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12268 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12269 break;
12270 case VECTOR_CST:
12271 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12272 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12273 break;
12274 default:
12275 break;
12277 break;
12278 case tcc_exceptional:
12279 switch (code)
12281 case TREE_LIST:
12282 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12283 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12284 expr = TREE_CHAIN (expr);
12285 goto recursive_label;
12286 break;
12287 case TREE_VEC:
12288 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12289 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12290 break;
12291 default:
12292 break;
12294 break;
12295 case tcc_expression:
12296 case tcc_reference:
12297 case tcc_comparison:
12298 case tcc_unary:
12299 case tcc_binary:
12300 case tcc_statement:
12301 case tcc_vl_exp:
12302 len = TREE_OPERAND_LENGTH (expr);
12303 for (i = 0; i < len; ++i)
12304 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12305 break;
12306 case tcc_declaration:
12307 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12308 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12309 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12311 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12312 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12313 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12314 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12315 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12318 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12320 if (TREE_CODE (expr) == FUNCTION_DECL)
12322 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12323 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12325 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12327 break;
12328 case tcc_type:
12329 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12330 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12331 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12332 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12333 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12334 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12335 if (INTEGRAL_TYPE_P (expr)
12336 || SCALAR_FLOAT_TYPE_P (expr))
12338 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12339 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12341 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12342 if (TREE_CODE (expr) == RECORD_TYPE
12343 || TREE_CODE (expr) == UNION_TYPE
12344 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12345 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12346 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12347 break;
12348 default:
12349 break;
12353 /* Helper function for outputting the checksum of a tree T. When
12354 debugging with gdb, you can "define mynext" to be "next" followed
12355 by "call debug_fold_checksum (op0)", then just trace down till the
12356 outputs differ. */
12358 DEBUG_FUNCTION void
12359 debug_fold_checksum (const_tree t)
12361 int i;
12362 unsigned char checksum[16];
12363 struct md5_ctx ctx;
12364 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12366 md5_init_ctx (&ctx);
12367 fold_checksum_tree (t, &ctx, &ht);
12368 md5_finish_ctx (&ctx, checksum);
12369 ht.empty ();
12371 for (i = 0; i < 16; i++)
12372 fprintf (stderr, "%d ", checksum[i]);
12374 fprintf (stderr, "\n");
12377 #endif
12379 /* Fold a unary tree expression with code CODE of type TYPE with an
12380 operand OP0. LOC is the location of the resulting expression.
12381 Return a folded expression if successful. Otherwise, return a tree
12382 expression with code CODE of type TYPE with an operand OP0. */
12384 tree
12385 fold_build1_stat_loc (location_t loc,
12386 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12388 tree tem;
12389 #ifdef ENABLE_FOLD_CHECKING
12390 unsigned char checksum_before[16], checksum_after[16];
12391 struct md5_ctx ctx;
12392 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12394 md5_init_ctx (&ctx);
12395 fold_checksum_tree (op0, &ctx, &ht);
12396 md5_finish_ctx (&ctx, checksum_before);
12397 ht.empty ();
12398 #endif
12400 tem = fold_unary_loc (loc, code, type, op0);
12401 if (!tem)
12402 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12404 #ifdef ENABLE_FOLD_CHECKING
12405 md5_init_ctx (&ctx);
12406 fold_checksum_tree (op0, &ctx, &ht);
12407 md5_finish_ctx (&ctx, checksum_after);
12409 if (memcmp (checksum_before, checksum_after, 16))
12410 fold_check_failed (op0, tem);
12411 #endif
12412 return tem;
12415 /* Fold a binary tree expression with code CODE of type TYPE with
12416 operands OP0 and OP1. LOC is the location of the resulting
12417 expression. Return a folded expression if successful. Otherwise,
12418 return a tree expression with code CODE of type TYPE with operands
12419 OP0 and OP1. */
12421 tree
12422 fold_build2_stat_loc (location_t loc,
12423 enum tree_code code, tree type, tree op0, tree op1
12424 MEM_STAT_DECL)
12426 tree tem;
12427 #ifdef ENABLE_FOLD_CHECKING
12428 unsigned char checksum_before_op0[16],
12429 checksum_before_op1[16],
12430 checksum_after_op0[16],
12431 checksum_after_op1[16];
12432 struct md5_ctx ctx;
12433 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12435 md5_init_ctx (&ctx);
12436 fold_checksum_tree (op0, &ctx, &ht);
12437 md5_finish_ctx (&ctx, checksum_before_op0);
12438 ht.empty ();
12440 md5_init_ctx (&ctx);
12441 fold_checksum_tree (op1, &ctx, &ht);
12442 md5_finish_ctx (&ctx, checksum_before_op1);
12443 ht.empty ();
12444 #endif
12446 tem = fold_binary_loc (loc, code, type, op0, op1);
12447 if (!tem)
12448 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12450 #ifdef ENABLE_FOLD_CHECKING
12451 md5_init_ctx (&ctx);
12452 fold_checksum_tree (op0, &ctx, &ht);
12453 md5_finish_ctx (&ctx, checksum_after_op0);
12454 ht.empty ();
12456 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12457 fold_check_failed (op0, tem);
12459 md5_init_ctx (&ctx);
12460 fold_checksum_tree (op1, &ctx, &ht);
12461 md5_finish_ctx (&ctx, checksum_after_op1);
12463 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12464 fold_check_failed (op1, tem);
12465 #endif
12466 return tem;
12469 /* Fold a ternary tree expression with code CODE of type TYPE with
12470 operands OP0, OP1, and OP2. Return a folded expression if
12471 successful. Otherwise, return a tree expression with code CODE of
12472 type TYPE with operands OP0, OP1, and OP2. */
12474 tree
12475 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12476 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12478 tree tem;
12479 #ifdef ENABLE_FOLD_CHECKING
12480 unsigned char checksum_before_op0[16],
12481 checksum_before_op1[16],
12482 checksum_before_op2[16],
12483 checksum_after_op0[16],
12484 checksum_after_op1[16],
12485 checksum_after_op2[16];
12486 struct md5_ctx ctx;
12487 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12489 md5_init_ctx (&ctx);
12490 fold_checksum_tree (op0, &ctx, &ht);
12491 md5_finish_ctx (&ctx, checksum_before_op0);
12492 ht.empty ();
12494 md5_init_ctx (&ctx);
12495 fold_checksum_tree (op1, &ctx, &ht);
12496 md5_finish_ctx (&ctx, checksum_before_op1);
12497 ht.empty ();
12499 md5_init_ctx (&ctx);
12500 fold_checksum_tree (op2, &ctx, &ht);
12501 md5_finish_ctx (&ctx, checksum_before_op2);
12502 ht.empty ();
12503 #endif
12505 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12506 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12507 if (!tem)
12508 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12510 #ifdef ENABLE_FOLD_CHECKING
12511 md5_init_ctx (&ctx);
12512 fold_checksum_tree (op0, &ctx, &ht);
12513 md5_finish_ctx (&ctx, checksum_after_op0);
12514 ht.empty ();
12516 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12517 fold_check_failed (op0, tem);
12519 md5_init_ctx (&ctx);
12520 fold_checksum_tree (op1, &ctx, &ht);
12521 md5_finish_ctx (&ctx, checksum_after_op1);
12522 ht.empty ();
12524 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12525 fold_check_failed (op1, tem);
12527 md5_init_ctx (&ctx);
12528 fold_checksum_tree (op2, &ctx, &ht);
12529 md5_finish_ctx (&ctx, checksum_after_op2);
12531 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12532 fold_check_failed (op2, tem);
12533 #endif
12534 return tem;
12537 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12538 arguments in ARGARRAY, and a null static chain.
12539 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12540 of type TYPE from the given operands as constructed by build_call_array. */
12542 tree
12543 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12544 int nargs, tree *argarray)
12546 tree tem;
12547 #ifdef ENABLE_FOLD_CHECKING
12548 unsigned char checksum_before_fn[16],
12549 checksum_before_arglist[16],
12550 checksum_after_fn[16],
12551 checksum_after_arglist[16];
12552 struct md5_ctx ctx;
12553 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12554 int i;
12556 md5_init_ctx (&ctx);
12557 fold_checksum_tree (fn, &ctx, &ht);
12558 md5_finish_ctx (&ctx, checksum_before_fn);
12559 ht.empty ();
12561 md5_init_ctx (&ctx);
12562 for (i = 0; i < nargs; i++)
12563 fold_checksum_tree (argarray[i], &ctx, &ht);
12564 md5_finish_ctx (&ctx, checksum_before_arglist);
12565 ht.empty ();
12566 #endif
12568 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12569 if (!tem)
12570 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12572 #ifdef ENABLE_FOLD_CHECKING
12573 md5_init_ctx (&ctx);
12574 fold_checksum_tree (fn, &ctx, &ht);
12575 md5_finish_ctx (&ctx, checksum_after_fn);
12576 ht.empty ();
12578 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12579 fold_check_failed (fn, tem);
12581 md5_init_ctx (&ctx);
12582 for (i = 0; i < nargs; i++)
12583 fold_checksum_tree (argarray[i], &ctx, &ht);
12584 md5_finish_ctx (&ctx, checksum_after_arglist);
12586 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12587 fold_check_failed (NULL_TREE, tem);
12588 #endif
12589 return tem;
12592 /* Perform constant folding and related simplification of initializer
12593 expression EXPR. These behave identically to "fold_buildN" but ignore
12594 potential run-time traps and exceptions that fold must preserve. */
12596 #define START_FOLD_INIT \
12597 int saved_signaling_nans = flag_signaling_nans;\
12598 int saved_trapping_math = flag_trapping_math;\
12599 int saved_rounding_math = flag_rounding_math;\
12600 int saved_trapv = flag_trapv;\
12601 int saved_folding_initializer = folding_initializer;\
12602 flag_signaling_nans = 0;\
12603 flag_trapping_math = 0;\
12604 flag_rounding_math = 0;\
12605 flag_trapv = 0;\
12606 folding_initializer = 1;
12608 #define END_FOLD_INIT \
12609 flag_signaling_nans = saved_signaling_nans;\
12610 flag_trapping_math = saved_trapping_math;\
12611 flag_rounding_math = saved_rounding_math;\
12612 flag_trapv = saved_trapv;\
12613 folding_initializer = saved_folding_initializer;
12615 tree
12616 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12617 tree type, tree op)
12619 tree result;
12620 START_FOLD_INIT;
12622 result = fold_build1_loc (loc, code, type, op);
12624 END_FOLD_INIT;
12625 return result;
12628 tree
12629 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12630 tree type, tree op0, tree op1)
12632 tree result;
12633 START_FOLD_INIT;
12635 result = fold_build2_loc (loc, code, type, op0, op1);
12637 END_FOLD_INIT;
12638 return result;
12641 tree
12642 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12643 int nargs, tree *argarray)
12645 tree result;
12646 START_FOLD_INIT;
12648 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12650 END_FOLD_INIT;
12651 return result;
12654 #undef START_FOLD_INIT
12655 #undef END_FOLD_INIT
12657 /* Determine if first argument is a multiple of second argument. Return 0 if
12658 it is not, or we cannot easily determined it to be.
12660 An example of the sort of thing we care about (at this point; this routine
12661 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12662 fold cases do now) is discovering that
12664 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12666 is a multiple of
12668 SAVE_EXPR (J * 8)
12670 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12672 This code also handles discovering that
12674 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12676 is a multiple of 8 so we don't have to worry about dealing with a
12677 possible remainder.
12679 Note that we *look* inside a SAVE_EXPR only to determine how it was
12680 calculated; it is not safe for fold to do much of anything else with the
12681 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12682 at run time. For example, the latter example above *cannot* be implemented
12683 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12684 evaluation time of the original SAVE_EXPR is not necessarily the same at
12685 the time the new expression is evaluated. The only optimization of this
12686 sort that would be valid is changing
12688 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12690 divided by 8 to
12692 SAVE_EXPR (I) * SAVE_EXPR (J)
12694 (where the same SAVE_EXPR (J) is used in the original and the
12695 transformed version). */
12698 multiple_of_p (tree type, const_tree top, const_tree bottom)
12700 if (operand_equal_p (top, bottom, 0))
12701 return 1;
12703 if (TREE_CODE (type) != INTEGER_TYPE)
12704 return 0;
12706 switch (TREE_CODE (top))
12708 case BIT_AND_EXPR:
12709 /* Bitwise and provides a power of two multiple. If the mask is
12710 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12711 if (!integer_pow2p (bottom))
12712 return 0;
12713 /* FALLTHRU */
12715 case MULT_EXPR:
12716 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12717 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12719 case PLUS_EXPR:
12720 case MINUS_EXPR:
12721 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12722 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12724 case LSHIFT_EXPR:
12725 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12727 tree op1, t1;
12729 op1 = TREE_OPERAND (top, 1);
12730 /* const_binop may not detect overflow correctly,
12731 so check for it explicitly here. */
12732 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12733 && 0 != (t1 = fold_convert (type,
12734 const_binop (LSHIFT_EXPR,
12735 size_one_node,
12736 op1)))
12737 && !TREE_OVERFLOW (t1))
12738 return multiple_of_p (type, t1, bottom);
12740 return 0;
12742 case NOP_EXPR:
12743 /* Can't handle conversions from non-integral or wider integral type. */
12744 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12745 || (TYPE_PRECISION (type)
12746 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12747 return 0;
12749 /* .. fall through ... */
12751 case SAVE_EXPR:
12752 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12754 case COND_EXPR:
12755 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12756 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12758 case INTEGER_CST:
12759 if (TREE_CODE (bottom) != INTEGER_CST
12760 || integer_zerop (bottom)
12761 || (TYPE_UNSIGNED (type)
12762 && (tree_int_cst_sgn (top) < 0
12763 || tree_int_cst_sgn (bottom) < 0)))
12764 return 0;
12765 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12766 SIGNED);
12768 default:
12769 return 0;
12773 #define tree_expr_nonnegative_warnv_p(X, Y) \
12774 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12776 #define RECURSE(X) \
12777 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12779 /* Return true if CODE or TYPE is known to be non-negative. */
12781 static bool
12782 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12784 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12785 && truth_value_p (code))
12786 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12787 have a signed:1 type (where the value is -1 and 0). */
12788 return true;
12789 return false;
12792 /* Return true if (CODE OP0) is known to be non-negative. If the return
12793 value is based on the assumption that signed overflow is undefined,
12794 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12795 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12797 bool
12798 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12799 bool *strict_overflow_p, int depth)
12801 if (TYPE_UNSIGNED (type))
12802 return true;
12804 switch (code)
12806 case ABS_EXPR:
12807 /* We can't return 1 if flag_wrapv is set because
12808 ABS_EXPR<INT_MIN> = INT_MIN. */
12809 if (!ANY_INTEGRAL_TYPE_P (type))
12810 return true;
12811 if (TYPE_OVERFLOW_UNDEFINED (type))
12813 *strict_overflow_p = true;
12814 return true;
12816 break;
12818 case NON_LVALUE_EXPR:
12819 case FLOAT_EXPR:
12820 case FIX_TRUNC_EXPR:
12821 return RECURSE (op0);
12823 CASE_CONVERT:
12825 tree inner_type = TREE_TYPE (op0);
12826 tree outer_type = type;
12828 if (TREE_CODE (outer_type) == REAL_TYPE)
12830 if (TREE_CODE (inner_type) == REAL_TYPE)
12831 return RECURSE (op0);
12832 if (INTEGRAL_TYPE_P (inner_type))
12834 if (TYPE_UNSIGNED (inner_type))
12835 return true;
12836 return RECURSE (op0);
12839 else if (INTEGRAL_TYPE_P (outer_type))
12841 if (TREE_CODE (inner_type) == REAL_TYPE)
12842 return RECURSE (op0);
12843 if (INTEGRAL_TYPE_P (inner_type))
12844 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12845 && TYPE_UNSIGNED (inner_type);
12848 break;
12850 default:
12851 return tree_simple_nonnegative_warnv_p (code, type);
12854 /* We don't know sign of `t', so be conservative and return false. */
12855 return false;
12858 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12859 value is based on the assumption that signed overflow is undefined,
12860 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12861 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12863 bool
12864 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12865 tree op1, bool *strict_overflow_p,
12866 int depth)
12868 if (TYPE_UNSIGNED (type))
12869 return true;
12871 switch (code)
12873 case POINTER_PLUS_EXPR:
12874 case PLUS_EXPR:
12875 if (FLOAT_TYPE_P (type))
12876 return RECURSE (op0) && RECURSE (op1);
12878 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12879 both unsigned and at least 2 bits shorter than the result. */
12880 if (TREE_CODE (type) == INTEGER_TYPE
12881 && TREE_CODE (op0) == NOP_EXPR
12882 && TREE_CODE (op1) == NOP_EXPR)
12884 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12885 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12886 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12887 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12889 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12890 TYPE_PRECISION (inner2)) + 1;
12891 return prec < TYPE_PRECISION (type);
12894 break;
12896 case MULT_EXPR:
12897 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12899 /* x * x is always non-negative for floating point x
12900 or without overflow. */
12901 if (operand_equal_p (op0, op1, 0)
12902 || (RECURSE (op0) && RECURSE (op1)))
12904 if (ANY_INTEGRAL_TYPE_P (type)
12905 && TYPE_OVERFLOW_UNDEFINED (type))
12906 *strict_overflow_p = true;
12907 return true;
12911 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12912 both unsigned and their total bits is shorter than the result. */
12913 if (TREE_CODE (type) == INTEGER_TYPE
12914 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12915 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12917 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12918 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12919 : TREE_TYPE (op0);
12920 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12921 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12922 : TREE_TYPE (op1);
12924 bool unsigned0 = TYPE_UNSIGNED (inner0);
12925 bool unsigned1 = TYPE_UNSIGNED (inner1);
12927 if (TREE_CODE (op0) == INTEGER_CST)
12928 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12930 if (TREE_CODE (op1) == INTEGER_CST)
12931 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12933 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12934 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12936 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12937 ? tree_int_cst_min_precision (op0, UNSIGNED)
12938 : TYPE_PRECISION (inner0);
12940 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12941 ? tree_int_cst_min_precision (op1, UNSIGNED)
12942 : TYPE_PRECISION (inner1);
12944 return precision0 + precision1 < TYPE_PRECISION (type);
12947 return false;
12949 case BIT_AND_EXPR:
12950 case MAX_EXPR:
12951 return RECURSE (op0) || RECURSE (op1);
12953 case BIT_IOR_EXPR:
12954 case BIT_XOR_EXPR:
12955 case MIN_EXPR:
12956 case RDIV_EXPR:
12957 case TRUNC_DIV_EXPR:
12958 case CEIL_DIV_EXPR:
12959 case FLOOR_DIV_EXPR:
12960 case ROUND_DIV_EXPR:
12961 return RECURSE (op0) && RECURSE (op1);
12963 case TRUNC_MOD_EXPR:
12964 return RECURSE (op0);
12966 case FLOOR_MOD_EXPR:
12967 return RECURSE (op1);
12969 case CEIL_MOD_EXPR:
12970 case ROUND_MOD_EXPR:
12971 default:
12972 return tree_simple_nonnegative_warnv_p (code, type);
12975 /* We don't know sign of `t', so be conservative and return false. */
12976 return false;
12979 /* Return true if T is known to be non-negative. If the return
12980 value is based on the assumption that signed overflow is undefined,
12981 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12982 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12984 bool
12985 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12987 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12988 return true;
12990 switch (TREE_CODE (t))
12992 case INTEGER_CST:
12993 return tree_int_cst_sgn (t) >= 0;
12995 case REAL_CST:
12996 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12998 case FIXED_CST:
12999 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13001 case COND_EXPR:
13002 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13004 case SSA_NAME:
13005 /* Limit the depth of recursion to avoid quadratic behavior.
13006 This is expected to catch almost all occurrences in practice.
13007 If this code misses important cases that unbounded recursion
13008 would not, passes that need this information could be revised
13009 to provide it through dataflow propagation. */
13010 return (!name_registered_for_update_p (t)
13011 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13012 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
13013 strict_overflow_p, depth));
13015 default:
13016 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13020 /* Return true if T is known to be non-negative. If the return
13021 value is based on the assumption that signed overflow is undefined,
13022 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13023 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13025 bool
13026 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13027 bool *strict_overflow_p, int depth)
13029 switch (fn)
13031 CASE_CFN_ACOS:
13032 CASE_CFN_ACOSH:
13033 CASE_CFN_CABS:
13034 CASE_CFN_COSH:
13035 CASE_CFN_ERFC:
13036 CASE_CFN_EXP:
13037 CASE_CFN_EXP10:
13038 CASE_CFN_EXP2:
13039 CASE_CFN_FABS:
13040 CASE_CFN_FDIM:
13041 CASE_CFN_HYPOT:
13042 CASE_CFN_POW10:
13043 CASE_CFN_FFS:
13044 CASE_CFN_PARITY:
13045 CASE_CFN_POPCOUNT:
13046 CASE_CFN_CLZ:
13047 CASE_CFN_CLRSB:
13048 case CFN_BUILT_IN_BSWAP32:
13049 case CFN_BUILT_IN_BSWAP64:
13050 /* Always true. */
13051 return true;
13053 CASE_CFN_SQRT:
13054 /* sqrt(-0.0) is -0.0. */
13055 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13056 return true;
13057 return RECURSE (arg0);
13059 CASE_CFN_ASINH:
13060 CASE_CFN_ATAN:
13061 CASE_CFN_ATANH:
13062 CASE_CFN_CBRT:
13063 CASE_CFN_CEIL:
13064 CASE_CFN_ERF:
13065 CASE_CFN_EXPM1:
13066 CASE_CFN_FLOOR:
13067 CASE_CFN_FMOD:
13068 CASE_CFN_FREXP:
13069 CASE_CFN_ICEIL:
13070 CASE_CFN_IFLOOR:
13071 CASE_CFN_IRINT:
13072 CASE_CFN_IROUND:
13073 CASE_CFN_LCEIL:
13074 CASE_CFN_LDEXP:
13075 CASE_CFN_LFLOOR:
13076 CASE_CFN_LLCEIL:
13077 CASE_CFN_LLFLOOR:
13078 CASE_CFN_LLRINT:
13079 CASE_CFN_LLROUND:
13080 CASE_CFN_LRINT:
13081 CASE_CFN_LROUND:
13082 CASE_CFN_MODF:
13083 CASE_CFN_NEARBYINT:
13084 CASE_CFN_RINT:
13085 CASE_CFN_ROUND:
13086 CASE_CFN_SCALB:
13087 CASE_CFN_SCALBLN:
13088 CASE_CFN_SCALBN:
13089 CASE_CFN_SIGNBIT:
13090 CASE_CFN_SIGNIFICAND:
13091 CASE_CFN_SINH:
13092 CASE_CFN_TANH:
13093 CASE_CFN_TRUNC:
13094 /* True if the 1st argument is nonnegative. */
13095 return RECURSE (arg0);
13097 CASE_CFN_FMAX:
13098 /* True if the 1st OR 2nd arguments are nonnegative. */
13099 return RECURSE (arg0) || RECURSE (arg1);
13101 CASE_CFN_FMIN:
13102 /* True if the 1st AND 2nd arguments are nonnegative. */
13103 return RECURSE (arg0) && RECURSE (arg1);
13105 CASE_CFN_COPYSIGN:
13106 /* True if the 2nd argument is nonnegative. */
13107 return RECURSE (arg1);
13109 CASE_CFN_POWI:
13110 /* True if the 1st argument is nonnegative or the second
13111 argument is an even integer. */
13112 if (TREE_CODE (arg1) == INTEGER_CST
13113 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13114 return true;
13115 return RECURSE (arg0);
13117 CASE_CFN_POW:
13118 /* True if the 1st argument is nonnegative or the second
13119 argument is an even integer valued real. */
13120 if (TREE_CODE (arg1) == REAL_CST)
13122 REAL_VALUE_TYPE c;
13123 HOST_WIDE_INT n;
13125 c = TREE_REAL_CST (arg1);
13126 n = real_to_integer (&c);
13127 if ((n & 1) == 0)
13129 REAL_VALUE_TYPE cint;
13130 real_from_integer (&cint, VOIDmode, n, SIGNED);
13131 if (real_identical (&c, &cint))
13132 return true;
13135 return RECURSE (arg0);
13137 default:
13138 break;
13140 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13143 /* Return true if T is known to be non-negative. If the return
13144 value is based on the assumption that signed overflow is undefined,
13145 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13146 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13148 static bool
13149 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13151 enum tree_code code = TREE_CODE (t);
13152 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13153 return true;
13155 switch (code)
13157 case TARGET_EXPR:
13159 tree temp = TARGET_EXPR_SLOT (t);
13160 t = TARGET_EXPR_INITIAL (t);
13162 /* If the initializer is non-void, then it's a normal expression
13163 that will be assigned to the slot. */
13164 if (!VOID_TYPE_P (t))
13165 return RECURSE (t);
13167 /* Otherwise, the initializer sets the slot in some way. One common
13168 way is an assignment statement at the end of the initializer. */
13169 while (1)
13171 if (TREE_CODE (t) == BIND_EXPR)
13172 t = expr_last (BIND_EXPR_BODY (t));
13173 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13174 || TREE_CODE (t) == TRY_CATCH_EXPR)
13175 t = expr_last (TREE_OPERAND (t, 0));
13176 else if (TREE_CODE (t) == STATEMENT_LIST)
13177 t = expr_last (t);
13178 else
13179 break;
13181 if (TREE_CODE (t) == MODIFY_EXPR
13182 && TREE_OPERAND (t, 0) == temp)
13183 return RECURSE (TREE_OPERAND (t, 1));
13185 return false;
13188 case CALL_EXPR:
13190 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13191 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13193 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13194 get_call_combined_fn (t),
13195 arg0,
13196 arg1,
13197 strict_overflow_p, depth);
13199 case COMPOUND_EXPR:
13200 case MODIFY_EXPR:
13201 return RECURSE (TREE_OPERAND (t, 1));
13203 case BIND_EXPR:
13204 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13206 case SAVE_EXPR:
13207 return RECURSE (TREE_OPERAND (t, 0));
13209 default:
13210 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13214 #undef RECURSE
13215 #undef tree_expr_nonnegative_warnv_p
13217 /* Return true if T is known to be non-negative. If the return
13218 value is based on the assumption that signed overflow is undefined,
13219 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13220 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13222 bool
13223 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13225 enum tree_code code;
13226 if (t == error_mark_node)
13227 return false;
13229 code = TREE_CODE (t);
13230 switch (TREE_CODE_CLASS (code))
13232 case tcc_binary:
13233 case tcc_comparison:
13234 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13235 TREE_TYPE (t),
13236 TREE_OPERAND (t, 0),
13237 TREE_OPERAND (t, 1),
13238 strict_overflow_p, depth);
13240 case tcc_unary:
13241 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13242 TREE_TYPE (t),
13243 TREE_OPERAND (t, 0),
13244 strict_overflow_p, depth);
13246 case tcc_constant:
13247 case tcc_declaration:
13248 case tcc_reference:
13249 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13251 default:
13252 break;
13255 switch (code)
13257 case TRUTH_AND_EXPR:
13258 case TRUTH_OR_EXPR:
13259 case TRUTH_XOR_EXPR:
13260 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13261 TREE_TYPE (t),
13262 TREE_OPERAND (t, 0),
13263 TREE_OPERAND (t, 1),
13264 strict_overflow_p, depth);
13265 case TRUTH_NOT_EXPR:
13266 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13267 TREE_TYPE (t),
13268 TREE_OPERAND (t, 0),
13269 strict_overflow_p, depth);
13271 case COND_EXPR:
13272 case CONSTRUCTOR:
13273 case OBJ_TYPE_REF:
13274 case ASSERT_EXPR:
13275 case ADDR_EXPR:
13276 case WITH_SIZE_EXPR:
13277 case SSA_NAME:
13278 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13280 default:
13281 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13285 /* Return true if `t' is known to be non-negative. Handle warnings
13286 about undefined signed overflow. */
13288 bool
13289 tree_expr_nonnegative_p (tree t)
13291 bool ret, strict_overflow_p;
13293 strict_overflow_p = false;
13294 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13295 if (strict_overflow_p)
13296 fold_overflow_warning (("assuming signed overflow does not occur when "
13297 "determining that expression is always "
13298 "non-negative"),
13299 WARN_STRICT_OVERFLOW_MISC);
13300 return ret;
13304 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13305 For floating point we further ensure that T is not denormal.
13306 Similar logic is present in nonzero_address in rtlanal.h.
13308 If the return value is based on the assumption that signed overflow
13309 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13310 change *STRICT_OVERFLOW_P. */
13312 bool
13313 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13314 bool *strict_overflow_p)
13316 switch (code)
13318 case ABS_EXPR:
13319 return tree_expr_nonzero_warnv_p (op0,
13320 strict_overflow_p);
13322 case NOP_EXPR:
13324 tree inner_type = TREE_TYPE (op0);
13325 tree outer_type = type;
13327 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13328 && tree_expr_nonzero_warnv_p (op0,
13329 strict_overflow_p));
13331 break;
13333 case NON_LVALUE_EXPR:
13334 return tree_expr_nonzero_warnv_p (op0,
13335 strict_overflow_p);
13337 default:
13338 break;
13341 return false;
13344 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13345 For floating point we further ensure that T is not denormal.
13346 Similar logic is present in nonzero_address in rtlanal.h.
13348 If the return value is based on the assumption that signed overflow
13349 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13350 change *STRICT_OVERFLOW_P. */
13352 bool
13353 tree_binary_nonzero_warnv_p (enum tree_code code,
13354 tree type,
13355 tree op0,
13356 tree op1, bool *strict_overflow_p)
13358 bool sub_strict_overflow_p;
13359 switch (code)
13361 case POINTER_PLUS_EXPR:
13362 case PLUS_EXPR:
13363 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13365 /* With the presence of negative values it is hard
13366 to say something. */
13367 sub_strict_overflow_p = false;
13368 if (!tree_expr_nonnegative_warnv_p (op0,
13369 &sub_strict_overflow_p)
13370 || !tree_expr_nonnegative_warnv_p (op1,
13371 &sub_strict_overflow_p))
13372 return false;
13373 /* One of operands must be positive and the other non-negative. */
13374 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13375 overflows, on a twos-complement machine the sum of two
13376 nonnegative numbers can never be zero. */
13377 return (tree_expr_nonzero_warnv_p (op0,
13378 strict_overflow_p)
13379 || tree_expr_nonzero_warnv_p (op1,
13380 strict_overflow_p));
13382 break;
13384 case MULT_EXPR:
13385 if (TYPE_OVERFLOW_UNDEFINED (type))
13387 if (tree_expr_nonzero_warnv_p (op0,
13388 strict_overflow_p)
13389 && tree_expr_nonzero_warnv_p (op1,
13390 strict_overflow_p))
13392 *strict_overflow_p = true;
13393 return true;
13396 break;
13398 case MIN_EXPR:
13399 sub_strict_overflow_p = false;
13400 if (tree_expr_nonzero_warnv_p (op0,
13401 &sub_strict_overflow_p)
13402 && tree_expr_nonzero_warnv_p (op1,
13403 &sub_strict_overflow_p))
13405 if (sub_strict_overflow_p)
13406 *strict_overflow_p = true;
13408 break;
13410 case MAX_EXPR:
13411 sub_strict_overflow_p = false;
13412 if (tree_expr_nonzero_warnv_p (op0,
13413 &sub_strict_overflow_p))
13415 if (sub_strict_overflow_p)
13416 *strict_overflow_p = true;
13418 /* When both operands are nonzero, then MAX must be too. */
13419 if (tree_expr_nonzero_warnv_p (op1,
13420 strict_overflow_p))
13421 return true;
13423 /* MAX where operand 0 is positive is positive. */
13424 return tree_expr_nonnegative_warnv_p (op0,
13425 strict_overflow_p);
13427 /* MAX where operand 1 is positive is positive. */
13428 else if (tree_expr_nonzero_warnv_p (op1,
13429 &sub_strict_overflow_p)
13430 && tree_expr_nonnegative_warnv_p (op1,
13431 &sub_strict_overflow_p))
13433 if (sub_strict_overflow_p)
13434 *strict_overflow_p = true;
13435 return true;
13437 break;
13439 case BIT_IOR_EXPR:
13440 return (tree_expr_nonzero_warnv_p (op1,
13441 strict_overflow_p)
13442 || tree_expr_nonzero_warnv_p (op0,
13443 strict_overflow_p));
13445 default:
13446 break;
13449 return false;
13452 /* Return true when T is an address and is known to be nonzero.
13453 For floating point we further ensure that T is not denormal.
13454 Similar logic is present in nonzero_address in rtlanal.h.
13456 If the return value is based on the assumption that signed overflow
13457 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13458 change *STRICT_OVERFLOW_P. */
13460 bool
13461 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13463 bool sub_strict_overflow_p;
13464 switch (TREE_CODE (t))
13466 case INTEGER_CST:
13467 return !integer_zerop (t);
13469 case ADDR_EXPR:
13471 tree base = TREE_OPERAND (t, 0);
13473 if (!DECL_P (base))
13474 base = get_base_address (base);
13476 if (!base)
13477 return false;
13479 /* For objects in symbol table check if we know they are non-zero.
13480 Don't do anything for variables and functions before symtab is built;
13481 it is quite possible that they will be declared weak later. */
13482 if (DECL_P (base) && decl_in_symtab_p (base))
13484 struct symtab_node *symbol;
13486 symbol = symtab_node::get_create (base);
13487 if (symbol)
13488 return symbol->nonzero_address ();
13489 else
13490 return false;
13493 /* Function local objects are never NULL. */
13494 if (DECL_P (base)
13495 && (DECL_CONTEXT (base)
13496 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13497 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13498 return true;
13500 /* Constants are never weak. */
13501 if (CONSTANT_CLASS_P (base))
13502 return true;
13504 return false;
13507 case COND_EXPR:
13508 sub_strict_overflow_p = false;
13509 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13510 &sub_strict_overflow_p)
13511 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13512 &sub_strict_overflow_p))
13514 if (sub_strict_overflow_p)
13515 *strict_overflow_p = true;
13516 return true;
13518 break;
13520 default:
13521 break;
13523 return false;
13526 #define integer_valued_real_p(X) \
13527 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13529 #define RECURSE(X) \
13530 ((integer_valued_real_p) (X, depth + 1))
13532 /* Return true if the floating point result of (CODE OP0) has an
13533 integer value. We also allow +Inf, -Inf and NaN to be considered
13534 integer values. Return false for signaling NaN.
13536 DEPTH is the current nesting depth of the query. */
13538 bool
13539 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13541 switch (code)
13543 case FLOAT_EXPR:
13544 return true;
13546 case ABS_EXPR:
13547 return RECURSE (op0);
13549 CASE_CONVERT:
13551 tree type = TREE_TYPE (op0);
13552 if (TREE_CODE (type) == INTEGER_TYPE)
13553 return true;
13554 if (TREE_CODE (type) == REAL_TYPE)
13555 return RECURSE (op0);
13556 break;
13559 default:
13560 break;
13562 return false;
13565 /* Return true if the floating point result of (CODE OP0 OP1) has an
13566 integer value. We also allow +Inf, -Inf and NaN to be considered
13567 integer values. Return false for signaling NaN.
13569 DEPTH is the current nesting depth of the query. */
13571 bool
13572 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13574 switch (code)
13576 case PLUS_EXPR:
13577 case MINUS_EXPR:
13578 case MULT_EXPR:
13579 case MIN_EXPR:
13580 case MAX_EXPR:
13581 return RECURSE (op0) && RECURSE (op1);
13583 default:
13584 break;
13586 return false;
13589 /* Return true if the floating point result of calling FNDECL with arguments
13590 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13591 considered integer values. Return false for signaling NaN. If FNDECL
13592 takes fewer than 2 arguments, the remaining ARGn are null.
13594 DEPTH is the current nesting depth of the query. */
13596 bool
13597 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13599 switch (fn)
13601 CASE_CFN_CEIL:
13602 CASE_CFN_FLOOR:
13603 CASE_CFN_NEARBYINT:
13604 CASE_CFN_RINT:
13605 CASE_CFN_ROUND:
13606 CASE_CFN_TRUNC:
13607 return true;
13609 CASE_CFN_FMIN:
13610 CASE_CFN_FMAX:
13611 return RECURSE (arg0) && RECURSE (arg1);
13613 default:
13614 break;
13616 return false;
13619 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13620 has an integer value. We also allow +Inf, -Inf and NaN to be
13621 considered integer values. Return false for signaling NaN.
13623 DEPTH is the current nesting depth of the query. */
13625 bool
13626 integer_valued_real_single_p (tree t, int depth)
13628 switch (TREE_CODE (t))
13630 case REAL_CST:
13631 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13633 case COND_EXPR:
13634 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13636 case SSA_NAME:
13637 /* Limit the depth of recursion to avoid quadratic behavior.
13638 This is expected to catch almost all occurrences in practice.
13639 If this code misses important cases that unbounded recursion
13640 would not, passes that need this information could be revised
13641 to provide it through dataflow propagation. */
13642 return (!name_registered_for_update_p (t)
13643 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13644 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13645 depth));
13647 default:
13648 break;
13650 return false;
13653 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13654 has an integer value. We also allow +Inf, -Inf and NaN to be
13655 considered integer values. Return false for signaling NaN.
13657 DEPTH is the current nesting depth of the query. */
13659 static bool
13660 integer_valued_real_invalid_p (tree t, int depth)
13662 switch (TREE_CODE (t))
13664 case COMPOUND_EXPR:
13665 case MODIFY_EXPR:
13666 case BIND_EXPR:
13667 return RECURSE (TREE_OPERAND (t, 1));
13669 case SAVE_EXPR:
13670 return RECURSE (TREE_OPERAND (t, 0));
13672 default:
13673 break;
13675 return false;
13678 #undef RECURSE
13679 #undef integer_valued_real_p
13681 /* Return true if the floating point expression T has an integer value.
13682 We also allow +Inf, -Inf and NaN to be considered integer values.
13683 Return false for signaling NaN.
13685 DEPTH is the current nesting depth of the query. */
13687 bool
13688 integer_valued_real_p (tree t, int depth)
13690 if (t == error_mark_node)
13691 return false;
13693 tree_code code = TREE_CODE (t);
13694 switch (TREE_CODE_CLASS (code))
13696 case tcc_binary:
13697 case tcc_comparison:
13698 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13699 TREE_OPERAND (t, 1), depth);
13701 case tcc_unary:
13702 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13704 case tcc_constant:
13705 case tcc_declaration:
13706 case tcc_reference:
13707 return integer_valued_real_single_p (t, depth);
13709 default:
13710 break;
13713 switch (code)
13715 case COND_EXPR:
13716 case SSA_NAME:
13717 return integer_valued_real_single_p (t, depth);
13719 case CALL_EXPR:
13721 tree arg0 = (call_expr_nargs (t) > 0
13722 ? CALL_EXPR_ARG (t, 0)
13723 : NULL_TREE);
13724 tree arg1 = (call_expr_nargs (t) > 1
13725 ? CALL_EXPR_ARG (t, 1)
13726 : NULL_TREE);
13727 return integer_valued_real_call_p (get_call_combined_fn (t),
13728 arg0, arg1, depth);
13731 default:
13732 return integer_valued_real_invalid_p (t, depth);
13736 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13737 attempt to fold the expression to a constant without modifying TYPE,
13738 OP0 or OP1.
13740 If the expression could be simplified to a constant, then return
13741 the constant. If the expression would not be simplified to a
13742 constant, then return NULL_TREE. */
13744 tree
13745 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13747 tree tem = fold_binary (code, type, op0, op1);
13748 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13751 /* Given the components of a unary expression CODE, TYPE and OP0,
13752 attempt to fold the expression to a constant without modifying
13753 TYPE or OP0.
13755 If the expression could be simplified to a constant, then return
13756 the constant. If the expression would not be simplified to a
13757 constant, then return NULL_TREE. */
13759 tree
13760 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13762 tree tem = fold_unary (code, type, op0);
13763 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13766 /* If EXP represents referencing an element in a constant string
13767 (either via pointer arithmetic or array indexing), return the
13768 tree representing the value accessed, otherwise return NULL. */
13770 tree
13771 fold_read_from_constant_string (tree exp)
13773 if ((TREE_CODE (exp) == INDIRECT_REF
13774 || TREE_CODE (exp) == ARRAY_REF)
13775 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13777 tree exp1 = TREE_OPERAND (exp, 0);
13778 tree index;
13779 tree string;
13780 location_t loc = EXPR_LOCATION (exp);
13782 if (TREE_CODE (exp) == INDIRECT_REF)
13783 string = string_constant (exp1, &index);
13784 else
13786 tree low_bound = array_ref_low_bound (exp);
13787 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13789 /* Optimize the special-case of a zero lower bound.
13791 We convert the low_bound to sizetype to avoid some problems
13792 with constant folding. (E.g. suppose the lower bound is 1,
13793 and its mode is QI. Without the conversion,l (ARRAY
13794 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13795 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13796 if (! integer_zerop (low_bound))
13797 index = size_diffop_loc (loc, index,
13798 fold_convert_loc (loc, sizetype, low_bound));
13800 string = exp1;
13803 if (string
13804 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13805 && TREE_CODE (string) == STRING_CST
13806 && TREE_CODE (index) == INTEGER_CST
13807 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13808 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13809 == MODE_INT)
13810 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13811 return build_int_cst_type (TREE_TYPE (exp),
13812 (TREE_STRING_POINTER (string)
13813 [TREE_INT_CST_LOW (index)]));
13815 return NULL;
13818 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13819 an integer constant, real, or fixed-point constant.
13821 TYPE is the type of the result. */
13823 static tree
13824 fold_negate_const (tree arg0, tree type)
13826 tree t = NULL_TREE;
13828 switch (TREE_CODE (arg0))
13830 case INTEGER_CST:
13832 bool overflow;
13833 wide_int val = wi::neg (arg0, &overflow);
13834 t = force_fit_type (type, val, 1,
13835 (overflow | TREE_OVERFLOW (arg0))
13836 && !TYPE_UNSIGNED (type));
13837 break;
13840 case REAL_CST:
13841 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13842 break;
13844 case FIXED_CST:
13846 FIXED_VALUE_TYPE f;
13847 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13848 &(TREE_FIXED_CST (arg0)), NULL,
13849 TYPE_SATURATING (type));
13850 t = build_fixed (type, f);
13851 /* Propagate overflow flags. */
13852 if (overflow_p | TREE_OVERFLOW (arg0))
13853 TREE_OVERFLOW (t) = 1;
13854 break;
13857 default:
13858 gcc_unreachable ();
13861 return t;
13864 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13865 an integer constant or real constant.
13867 TYPE is the type of the result. */
13869 tree
13870 fold_abs_const (tree arg0, tree type)
13872 tree t = NULL_TREE;
13874 switch (TREE_CODE (arg0))
13876 case INTEGER_CST:
13878 /* If the value is unsigned or non-negative, then the absolute value
13879 is the same as the ordinary value. */
13880 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13881 t = arg0;
13883 /* If the value is negative, then the absolute value is
13884 its negation. */
13885 else
13887 bool overflow;
13888 wide_int val = wi::neg (arg0, &overflow);
13889 t = force_fit_type (type, val, -1,
13890 overflow | TREE_OVERFLOW (arg0));
13893 break;
13895 case REAL_CST:
13896 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13897 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13898 else
13899 t = arg0;
13900 break;
13902 default:
13903 gcc_unreachable ();
13906 return t;
13909 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13910 constant. TYPE is the type of the result. */
13912 static tree
13913 fold_not_const (const_tree arg0, tree type)
13915 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13917 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13920 /* Given CODE, a relational operator, the target type, TYPE and two
13921 constant operands OP0 and OP1, return the result of the
13922 relational operation. If the result is not a compile time
13923 constant, then return NULL_TREE. */
13925 static tree
13926 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13928 int result, invert;
13930 /* From here on, the only cases we handle are when the result is
13931 known to be a constant. */
13933 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13935 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13936 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13938 /* Handle the cases where either operand is a NaN. */
13939 if (real_isnan (c0) || real_isnan (c1))
13941 switch (code)
13943 case EQ_EXPR:
13944 case ORDERED_EXPR:
13945 result = 0;
13946 break;
13948 case NE_EXPR:
13949 case UNORDERED_EXPR:
13950 case UNLT_EXPR:
13951 case UNLE_EXPR:
13952 case UNGT_EXPR:
13953 case UNGE_EXPR:
13954 case UNEQ_EXPR:
13955 result = 1;
13956 break;
13958 case LT_EXPR:
13959 case LE_EXPR:
13960 case GT_EXPR:
13961 case GE_EXPR:
13962 case LTGT_EXPR:
13963 if (flag_trapping_math)
13964 return NULL_TREE;
13965 result = 0;
13966 break;
13968 default:
13969 gcc_unreachable ();
13972 return constant_boolean_node (result, type);
13975 return constant_boolean_node (real_compare (code, c0, c1), type);
13978 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13980 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13981 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13982 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13985 /* Handle equality/inequality of complex constants. */
13986 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13988 tree rcond = fold_relational_const (code, type,
13989 TREE_REALPART (op0),
13990 TREE_REALPART (op1));
13991 tree icond = fold_relational_const (code, type,
13992 TREE_IMAGPART (op0),
13993 TREE_IMAGPART (op1));
13994 if (code == EQ_EXPR)
13995 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13996 else if (code == NE_EXPR)
13997 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13998 else
13999 return NULL_TREE;
14002 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
14004 if (!VECTOR_TYPE_P (type))
14006 /* Have vector comparison with scalar boolean result. */
14007 bool result = true;
14008 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
14009 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
14010 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
14012 tree elem0 = VECTOR_CST_ELT (op0, i);
14013 tree elem1 = VECTOR_CST_ELT (op1, i);
14014 tree tmp = fold_relational_const (code, type, elem0, elem1);
14015 result &= integer_onep (tmp);
14017 if (code == NE_EXPR)
14018 result = !result;
14019 return constant_boolean_node (result, type);
14021 unsigned count = VECTOR_CST_NELTS (op0);
14022 tree *elts = XALLOCAVEC (tree, count);
14023 gcc_assert (VECTOR_CST_NELTS (op1) == count
14024 && TYPE_VECTOR_SUBPARTS (type) == count);
14026 for (unsigned i = 0; i < count; i++)
14028 tree elem_type = TREE_TYPE (type);
14029 tree elem0 = VECTOR_CST_ELT (op0, i);
14030 tree elem1 = VECTOR_CST_ELT (op1, i);
14032 tree tem = fold_relational_const (code, elem_type,
14033 elem0, elem1);
14035 if (tem == NULL_TREE)
14036 return NULL_TREE;
14038 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
14041 return build_vector (type, elts);
14044 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14046 To compute GT, swap the arguments and do LT.
14047 To compute GE, do LT and invert the result.
14048 To compute LE, swap the arguments, do LT and invert the result.
14049 To compute NE, do EQ and invert the result.
14051 Therefore, the code below must handle only EQ and LT. */
14053 if (code == LE_EXPR || code == GT_EXPR)
14055 std::swap (op0, op1);
14056 code = swap_tree_comparison (code);
14059 /* Note that it is safe to invert for real values here because we
14060 have already handled the one case that it matters. */
14062 invert = 0;
14063 if (code == NE_EXPR || code == GE_EXPR)
14065 invert = 1;
14066 code = invert_tree_comparison (code, false);
14069 /* Compute a result for LT or EQ if args permit;
14070 Otherwise return T. */
14071 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14073 if (code == EQ_EXPR)
14074 result = tree_int_cst_equal (op0, op1);
14075 else
14076 result = tree_int_cst_lt (op0, op1);
14078 else
14079 return NULL_TREE;
14081 if (invert)
14082 result ^= 1;
14083 return constant_boolean_node (result, type);
14086 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14087 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14088 itself. */
14090 tree
14091 fold_build_cleanup_point_expr (tree type, tree expr)
14093 /* If the expression does not have side effects then we don't have to wrap
14094 it with a cleanup point expression. */
14095 if (!TREE_SIDE_EFFECTS (expr))
14096 return expr;
14098 /* If the expression is a return, check to see if the expression inside the
14099 return has no side effects or the right hand side of the modify expression
14100 inside the return. If either don't have side effects set we don't need to
14101 wrap the expression in a cleanup point expression. Note we don't check the
14102 left hand side of the modify because it should always be a return decl. */
14103 if (TREE_CODE (expr) == RETURN_EXPR)
14105 tree op = TREE_OPERAND (expr, 0);
14106 if (!op || !TREE_SIDE_EFFECTS (op))
14107 return expr;
14108 op = TREE_OPERAND (op, 1);
14109 if (!TREE_SIDE_EFFECTS (op))
14110 return expr;
14113 return build1 (CLEANUP_POINT_EXPR, type, expr);
14116 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14117 of an indirection through OP0, or NULL_TREE if no simplification is
14118 possible. */
14120 tree
14121 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14123 tree sub = op0;
14124 tree subtype;
14126 STRIP_NOPS (sub);
14127 subtype = TREE_TYPE (sub);
14128 if (!POINTER_TYPE_P (subtype))
14129 return NULL_TREE;
14131 if (TREE_CODE (sub) == ADDR_EXPR)
14133 tree op = TREE_OPERAND (sub, 0);
14134 tree optype = TREE_TYPE (op);
14135 /* *&CONST_DECL -> to the value of the const decl. */
14136 if (TREE_CODE (op) == CONST_DECL)
14137 return DECL_INITIAL (op);
14138 /* *&p => p; make sure to handle *&"str"[cst] here. */
14139 if (type == optype)
14141 tree fop = fold_read_from_constant_string (op);
14142 if (fop)
14143 return fop;
14144 else
14145 return op;
14147 /* *(foo *)&fooarray => fooarray[0] */
14148 else if (TREE_CODE (optype) == ARRAY_TYPE
14149 && type == TREE_TYPE (optype)
14150 && (!in_gimple_form
14151 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14153 tree type_domain = TYPE_DOMAIN (optype);
14154 tree min_val = size_zero_node;
14155 if (type_domain && TYPE_MIN_VALUE (type_domain))
14156 min_val = TYPE_MIN_VALUE (type_domain);
14157 if (in_gimple_form
14158 && TREE_CODE (min_val) != INTEGER_CST)
14159 return NULL_TREE;
14160 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14161 NULL_TREE, NULL_TREE);
14163 /* *(foo *)&complexfoo => __real__ complexfoo */
14164 else if (TREE_CODE (optype) == COMPLEX_TYPE
14165 && type == TREE_TYPE (optype))
14166 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14167 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14168 else if (TREE_CODE (optype) == VECTOR_TYPE
14169 && type == TREE_TYPE (optype))
14171 tree part_width = TYPE_SIZE (type);
14172 tree index = bitsize_int (0);
14173 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14177 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14178 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14180 tree op00 = TREE_OPERAND (sub, 0);
14181 tree op01 = TREE_OPERAND (sub, 1);
14183 STRIP_NOPS (op00);
14184 if (TREE_CODE (op00) == ADDR_EXPR)
14186 tree op00type;
14187 op00 = TREE_OPERAND (op00, 0);
14188 op00type = TREE_TYPE (op00);
14190 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14191 if (TREE_CODE (op00type) == VECTOR_TYPE
14192 && type == TREE_TYPE (op00type))
14194 HOST_WIDE_INT offset = tree_to_shwi (op01);
14195 tree part_width = TYPE_SIZE (type);
14196 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
14197 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14198 tree index = bitsize_int (indexi);
14200 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
14201 return fold_build3_loc (loc,
14202 BIT_FIELD_REF, type, op00,
14203 part_width, index);
14206 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14207 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14208 && type == TREE_TYPE (op00type))
14210 tree size = TYPE_SIZE_UNIT (type);
14211 if (tree_int_cst_equal (size, op01))
14212 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14214 /* ((foo *)&fooarray)[1] => fooarray[1] */
14215 else if (TREE_CODE (op00type) == ARRAY_TYPE
14216 && type == TREE_TYPE (op00type))
14218 tree type_domain = TYPE_DOMAIN (op00type);
14219 tree min_val = size_zero_node;
14220 if (type_domain && TYPE_MIN_VALUE (type_domain))
14221 min_val = TYPE_MIN_VALUE (type_domain);
14222 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14223 TYPE_SIZE_UNIT (type));
14224 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14225 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14226 NULL_TREE, NULL_TREE);
14231 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14232 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14233 && type == TREE_TYPE (TREE_TYPE (subtype))
14234 && (!in_gimple_form
14235 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14237 tree type_domain;
14238 tree min_val = size_zero_node;
14239 sub = build_fold_indirect_ref_loc (loc, sub);
14240 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14241 if (type_domain && TYPE_MIN_VALUE (type_domain))
14242 min_val = TYPE_MIN_VALUE (type_domain);
14243 if (in_gimple_form
14244 && TREE_CODE (min_val) != INTEGER_CST)
14245 return NULL_TREE;
14246 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14247 NULL_TREE);
14250 return NULL_TREE;
14253 /* Builds an expression for an indirection through T, simplifying some
14254 cases. */
14256 tree
14257 build_fold_indirect_ref_loc (location_t loc, tree t)
14259 tree type = TREE_TYPE (TREE_TYPE (t));
14260 tree sub = fold_indirect_ref_1 (loc, type, t);
14262 if (sub)
14263 return sub;
14265 return build1_loc (loc, INDIRECT_REF, type, t);
14268 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14270 tree
14271 fold_indirect_ref_loc (location_t loc, tree t)
14273 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14275 if (sub)
14276 return sub;
14277 else
14278 return t;
14281 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14282 whose result is ignored. The type of the returned tree need not be
14283 the same as the original expression. */
14285 tree
14286 fold_ignored_result (tree t)
14288 if (!TREE_SIDE_EFFECTS (t))
14289 return integer_zero_node;
14291 for (;;)
14292 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14294 case tcc_unary:
14295 t = TREE_OPERAND (t, 0);
14296 break;
14298 case tcc_binary:
14299 case tcc_comparison:
14300 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14301 t = TREE_OPERAND (t, 0);
14302 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14303 t = TREE_OPERAND (t, 1);
14304 else
14305 return t;
14306 break;
14308 case tcc_expression:
14309 switch (TREE_CODE (t))
14311 case COMPOUND_EXPR:
14312 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14313 return t;
14314 t = TREE_OPERAND (t, 0);
14315 break;
14317 case COND_EXPR:
14318 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14319 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14320 return t;
14321 t = TREE_OPERAND (t, 0);
14322 break;
14324 default:
14325 return t;
14327 break;
14329 default:
14330 return t;
14334 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14336 tree
14337 round_up_loc (location_t loc, tree value, unsigned int divisor)
14339 tree div = NULL_TREE;
14341 if (divisor == 1)
14342 return value;
14344 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14345 have to do anything. Only do this when we are not given a const,
14346 because in that case, this check is more expensive than just
14347 doing it. */
14348 if (TREE_CODE (value) != INTEGER_CST)
14350 div = build_int_cst (TREE_TYPE (value), divisor);
14352 if (multiple_of_p (TREE_TYPE (value), value, div))
14353 return value;
14356 /* If divisor is a power of two, simplify this to bit manipulation. */
14357 if (divisor == (divisor & -divisor))
14359 if (TREE_CODE (value) == INTEGER_CST)
14361 wide_int val = value;
14362 bool overflow_p;
14364 if ((val & (divisor - 1)) == 0)
14365 return value;
14367 overflow_p = TREE_OVERFLOW (value);
14368 val += divisor - 1;
14369 val &= - (int) divisor;
14370 if (val == 0)
14371 overflow_p = true;
14373 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14375 else
14377 tree t;
14379 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14380 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14381 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14382 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14385 else
14387 if (!div)
14388 div = build_int_cst (TREE_TYPE (value), divisor);
14389 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14390 value = size_binop_loc (loc, MULT_EXPR, value, div);
14393 return value;
14396 /* Likewise, but round down. */
14398 tree
14399 round_down_loc (location_t loc, tree value, int divisor)
14401 tree div = NULL_TREE;
14403 gcc_assert (divisor > 0);
14404 if (divisor == 1)
14405 return value;
14407 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14408 have to do anything. Only do this when we are not given a const,
14409 because in that case, this check is more expensive than just
14410 doing it. */
14411 if (TREE_CODE (value) != INTEGER_CST)
14413 div = build_int_cst (TREE_TYPE (value), divisor);
14415 if (multiple_of_p (TREE_TYPE (value), value, div))
14416 return value;
14419 /* If divisor is a power of two, simplify this to bit manipulation. */
14420 if (divisor == (divisor & -divisor))
14422 tree t;
14424 t = build_int_cst (TREE_TYPE (value), -divisor);
14425 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14427 else
14429 if (!div)
14430 div = build_int_cst (TREE_TYPE (value), divisor);
14431 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14432 value = size_binop_loc (loc, MULT_EXPR, value, div);
14435 return value;
14438 /* Returns the pointer to the base of the object addressed by EXP and
14439 extracts the information about the offset of the access, storing it
14440 to PBITPOS and POFFSET. */
14442 static tree
14443 split_address_to_core_and_offset (tree exp,
14444 HOST_WIDE_INT *pbitpos, tree *poffset)
14446 tree core;
14447 machine_mode mode;
14448 int unsignedp, reversep, volatilep;
14449 HOST_WIDE_INT bitsize;
14450 location_t loc = EXPR_LOCATION (exp);
14452 if (TREE_CODE (exp) == ADDR_EXPR)
14454 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14455 poffset, &mode, &unsignedp, &reversep,
14456 &volatilep, false);
14457 core = build_fold_addr_expr_loc (loc, core);
14459 else
14461 core = exp;
14462 *pbitpos = 0;
14463 *poffset = NULL_TREE;
14466 return core;
14469 /* Returns true if addresses of E1 and E2 differ by a constant, false
14470 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14472 bool
14473 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14475 tree core1, core2;
14476 HOST_WIDE_INT bitpos1, bitpos2;
14477 tree toffset1, toffset2, tdiff, type;
14479 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14480 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14482 if (bitpos1 % BITS_PER_UNIT != 0
14483 || bitpos2 % BITS_PER_UNIT != 0
14484 || !operand_equal_p (core1, core2, 0))
14485 return false;
14487 if (toffset1 && toffset2)
14489 type = TREE_TYPE (toffset1);
14490 if (type != TREE_TYPE (toffset2))
14491 toffset2 = fold_convert (type, toffset2);
14493 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14494 if (!cst_and_fits_in_hwi (tdiff))
14495 return false;
14497 *diff = int_cst_value (tdiff);
14499 else if (toffset1 || toffset2)
14501 /* If only one of the offsets is non-constant, the difference cannot
14502 be a constant. */
14503 return false;
14505 else
14506 *diff = 0;
14508 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14509 return true;
14512 /* Return OFF converted to a pointer offset type suitable as offset for
14513 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14514 tree
14515 convert_to_ptrofftype_loc (location_t loc, tree off)
14517 return fold_convert_loc (loc, sizetype, off);
14520 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14521 tree
14522 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14524 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14525 ptr, convert_to_ptrofftype_loc (loc, off));
14528 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14529 tree
14530 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14532 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14533 ptr, size_int (off));
14536 /* Return a char pointer for a C string if it is a string constant
14537 or sum of string constant and integer constant. */
14539 const char *
14540 c_getstr (tree src)
14542 tree offset_node;
14544 src = string_constant (src, &offset_node);
14545 if (src == 0)
14546 return 0;
14548 if (offset_node == 0)
14549 return TREE_STRING_POINTER (src);
14550 else if (!tree_fits_uhwi_p (offset_node)
14551 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
14552 return 0;
14554 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);