Selftest framework
[official-gcc.git] / gcc / fold-const.c
blob0efa9d5b0b1dd338a324c5a75ddc5b7a8545a013
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "tree-ssa-operands.h"
54 #include "optabs-query.h"
55 #include "cgraph.h"
56 #include "diagnostic-core.h"
57 #include "flags.h"
58 #include "alias.h"
59 #include "fold-const.h"
60 #include "fold-const-call.h"
61 #include "stor-layout.h"
62 #include "calls.h"
63 #include "tree-iterator.h"
64 #include "expr.h"
65 #include "intl.h"
66 #include "langhooks.h"
67 #include "tree-eh.h"
68 #include "gimplify.h"
69 #include "tree-dfa.h"
70 #include "builtins.h"
71 #include "generic-match.h"
72 #include "gimple-fold.h"
73 #include "params.h"
74 #include "tree-into-ssa.h"
75 #include "md5.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
78 #include "tree-ssanames.h"
79 #include "selftest.h"
81 #ifndef LOAD_EXTEND_OP
82 #define LOAD_EXTEND_OP(M) UNKNOWN
83 #endif
85 /* Nonzero if we are folding constants inside an initializer; zero
86 otherwise. */
87 int folding_initializer = 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code {
93 COMPCODE_FALSE = 0,
94 COMPCODE_LT = 1,
95 COMPCODE_EQ = 2,
96 COMPCODE_LE = 3,
97 COMPCODE_GT = 4,
98 COMPCODE_LTGT = 5,
99 COMPCODE_GE = 6,
100 COMPCODE_ORD = 7,
101 COMPCODE_UNORD = 8,
102 COMPCODE_UNLT = 9,
103 COMPCODE_UNEQ = 10,
104 COMPCODE_UNLE = 11,
105 COMPCODE_UNGT = 12,
106 COMPCODE_NE = 13,
107 COMPCODE_UNGE = 14,
108 COMPCODE_TRUE = 15
111 static bool negate_expr_p (tree);
112 static tree negate_expr (tree);
113 static tree split_tree (location_t, tree, tree, enum tree_code,
114 tree *, tree *, tree *, int);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int operand_equal_for_comparison_p (tree, tree, tree);
119 static int twoval_comparison_p (tree, tree *, tree *, int *);
120 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
121 static tree optimize_bit_field_compare (location_t, enum tree_code,
122 tree, tree, tree);
123 static int simple_operand_p (const_tree);
124 static bool simple_operand_p_2 (tree);
125 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
126 static tree range_predecessor (tree);
127 static tree range_successor (tree);
128 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree optimize_minmax_comparison (location_t, enum tree_code,
132 tree, tree, tree);
133 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
134 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
135 static tree fold_binary_op_with_conditional_arg (location_t,
136 enum tree_code, tree,
137 tree, tree,
138 tree, tree, int);
139 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
140 static bool reorder_operands_p (const_tree, const_tree);
141 static tree fold_negate_const (tree, tree);
142 static tree fold_not_const (const_tree, tree);
143 static tree fold_relational_const (enum tree_code, tree, tree, tree);
144 static tree fold_convert_const (enum tree_code, tree, tree);
145 static tree fold_view_convert_expr (tree, tree);
146 static bool vec_cst_ctor_to_array (tree, tree *);
149 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
150 Otherwise, return LOC. */
152 static location_t
153 expr_location_or (tree t, location_t loc)
155 location_t tloc = EXPR_LOCATION (t);
156 return tloc == UNKNOWN_LOCATION ? loc : tloc;
159 /* Similar to protected_set_expr_location, but never modify x in place,
160 if location can and needs to be set, unshare it. */
162 static inline tree
163 protected_set_expr_location_unshare (tree x, location_t loc)
165 if (CAN_HAVE_LOCATION_P (x)
166 && EXPR_LOCATION (x) != loc
167 && !(TREE_CODE (x) == SAVE_EXPR
168 || TREE_CODE (x) == TARGET_EXPR
169 || TREE_CODE (x) == BIND_EXPR))
171 x = copy_node (x);
172 SET_EXPR_LOCATION (x, loc);
174 return x;
177 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
178 division and returns the quotient. Otherwise returns
179 NULL_TREE. */
181 tree
182 div_if_zero_remainder (const_tree arg1, const_tree arg2)
184 widest_int quo;
186 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
187 SIGNED, &quo))
188 return wide_int_to_tree (TREE_TYPE (arg1), quo);
190 return NULL_TREE;
193 /* This is nonzero if we should defer warnings about undefined
194 overflow. This facility exists because these warnings are a
195 special case. The code to estimate loop iterations does not want
196 to issue any warnings, since it works with expressions which do not
197 occur in user code. Various bits of cleanup code call fold(), but
198 only use the result if it has certain characteristics (e.g., is a
199 constant); that code only wants to issue a warning if the result is
200 used. */
202 static int fold_deferring_overflow_warnings;
204 /* If a warning about undefined overflow is deferred, this is the
205 warning. Note that this may cause us to turn two warnings into
206 one, but that is fine since it is sufficient to only give one
207 warning per expression. */
209 static const char* fold_deferred_overflow_warning;
211 /* If a warning about undefined overflow is deferred, this is the
212 level at which the warning should be emitted. */
214 static enum warn_strict_overflow_code fold_deferred_overflow_code;
216 /* Start deferring overflow warnings. We could use a stack here to
217 permit nested calls, but at present it is not necessary. */
219 void
220 fold_defer_overflow_warnings (void)
222 ++fold_deferring_overflow_warnings;
225 /* Stop deferring overflow warnings. If there is a pending warning,
226 and ISSUE is true, then issue the warning if appropriate. STMT is
227 the statement with which the warning should be associated (used for
228 location information); STMT may be NULL. CODE is the level of the
229 warning--a warn_strict_overflow_code value. This function will use
230 the smaller of CODE and the deferred code when deciding whether to
231 issue the warning. CODE may be zero to mean to always use the
232 deferred code. */
234 void
235 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
237 const char *warnmsg;
238 location_t locus;
240 gcc_assert (fold_deferring_overflow_warnings > 0);
241 --fold_deferring_overflow_warnings;
242 if (fold_deferring_overflow_warnings > 0)
244 if (fold_deferred_overflow_warning != NULL
245 && code != 0
246 && code < (int) fold_deferred_overflow_code)
247 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
248 return;
251 warnmsg = fold_deferred_overflow_warning;
252 fold_deferred_overflow_warning = NULL;
254 if (!issue || warnmsg == NULL)
255 return;
257 if (gimple_no_warning_p (stmt))
258 return;
260 /* Use the smallest code level when deciding to issue the
261 warning. */
262 if (code == 0 || code > (int) fold_deferred_overflow_code)
263 code = fold_deferred_overflow_code;
265 if (!issue_strict_overflow_warning (code))
266 return;
268 if (stmt == NULL)
269 locus = input_location;
270 else
271 locus = gimple_location (stmt);
272 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
275 /* Stop deferring overflow warnings, ignoring any deferred
276 warnings. */
278 void
279 fold_undefer_and_ignore_overflow_warnings (void)
281 fold_undefer_overflow_warnings (false, NULL, 0);
284 /* Whether we are deferring overflow warnings. */
286 bool
287 fold_deferring_overflow_warnings_p (void)
289 return fold_deferring_overflow_warnings > 0;
292 /* This is called when we fold something based on the fact that signed
293 overflow is undefined. */
295 void
296 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
298 if (fold_deferring_overflow_warnings > 0)
300 if (fold_deferred_overflow_warning == NULL
301 || wc < fold_deferred_overflow_code)
303 fold_deferred_overflow_warning = gmsgid;
304 fold_deferred_overflow_code = wc;
307 else if (issue_strict_overflow_warning (wc))
308 warning (OPT_Wstrict_overflow, gmsgid);
311 /* Return true if the built-in mathematical function specified by CODE
312 is odd, i.e. -f(x) == f(-x). */
314 bool
315 negate_mathfn_p (combined_fn fn)
317 switch (fn)
319 CASE_CFN_ASIN:
320 CASE_CFN_ASINH:
321 CASE_CFN_ATAN:
322 CASE_CFN_ATANH:
323 CASE_CFN_CASIN:
324 CASE_CFN_CASINH:
325 CASE_CFN_CATAN:
326 CASE_CFN_CATANH:
327 CASE_CFN_CBRT:
328 CASE_CFN_CPROJ:
329 CASE_CFN_CSIN:
330 CASE_CFN_CSINH:
331 CASE_CFN_CTAN:
332 CASE_CFN_CTANH:
333 CASE_CFN_ERF:
334 CASE_CFN_LLROUND:
335 CASE_CFN_LROUND:
336 CASE_CFN_ROUND:
337 CASE_CFN_SIN:
338 CASE_CFN_SINH:
339 CASE_CFN_TAN:
340 CASE_CFN_TANH:
341 CASE_CFN_TRUNC:
342 return true;
344 CASE_CFN_LLRINT:
345 CASE_CFN_LRINT:
346 CASE_CFN_NEARBYINT:
347 CASE_CFN_RINT:
348 return !flag_rounding_math;
350 default:
351 break;
353 return false;
356 /* Check whether we may negate an integer constant T without causing
357 overflow. */
359 bool
360 may_negate_without_overflow_p (const_tree t)
362 tree type;
364 gcc_assert (TREE_CODE (t) == INTEGER_CST);
366 type = TREE_TYPE (t);
367 if (TYPE_UNSIGNED (type))
368 return false;
370 return !wi::only_sign_bit_p (t);
373 /* Determine whether an expression T can be cheaply negated using
374 the function negate_expr without introducing undefined overflow. */
376 static bool
377 negate_expr_p (tree t)
379 tree type;
381 if (t == 0)
382 return false;
384 type = TREE_TYPE (t);
386 STRIP_SIGN_NOPS (t);
387 switch (TREE_CODE (t))
389 case INTEGER_CST:
390 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
391 return true;
393 /* Check that -CST will not overflow type. */
394 return may_negate_without_overflow_p (t);
395 case BIT_NOT_EXPR:
396 return (INTEGRAL_TYPE_P (type)
397 && TYPE_OVERFLOW_WRAPS (type));
399 case FIXED_CST:
400 return true;
402 case NEGATE_EXPR:
403 return !TYPE_OVERFLOW_SANITIZED (type);
405 case REAL_CST:
406 /* We want to canonicalize to positive real constants. Pretend
407 that only negative ones can be easily negated. */
408 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
410 case COMPLEX_CST:
411 return negate_expr_p (TREE_REALPART (t))
412 && negate_expr_p (TREE_IMAGPART (t));
414 case VECTOR_CST:
416 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
417 return true;
419 int count = TYPE_VECTOR_SUBPARTS (type), i;
421 for (i = 0; i < count; i++)
422 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
423 return false;
425 return true;
428 case COMPLEX_EXPR:
429 return negate_expr_p (TREE_OPERAND (t, 0))
430 && negate_expr_p (TREE_OPERAND (t, 1));
432 case CONJ_EXPR:
433 return negate_expr_p (TREE_OPERAND (t, 0));
435 case PLUS_EXPR:
436 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
437 || HONOR_SIGNED_ZEROS (element_mode (type))
438 || (INTEGRAL_TYPE_P (type)
439 && ! TYPE_OVERFLOW_WRAPS (type)))
440 return false;
441 /* -(A + B) -> (-B) - A. */
442 if (negate_expr_p (TREE_OPERAND (t, 1))
443 && reorder_operands_p (TREE_OPERAND (t, 0),
444 TREE_OPERAND (t, 1)))
445 return true;
446 /* -(A + B) -> (-A) - B. */
447 return negate_expr_p (TREE_OPERAND (t, 0));
449 case MINUS_EXPR:
450 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
451 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
452 && !HONOR_SIGNED_ZEROS (element_mode (type))
453 && (! INTEGRAL_TYPE_P (type)
454 || TYPE_OVERFLOW_WRAPS (type))
455 && reorder_operands_p (TREE_OPERAND (t, 0),
456 TREE_OPERAND (t, 1));
458 case MULT_EXPR:
459 if (TYPE_UNSIGNED (type))
460 break;
461 /* INT_MIN/n * n doesn't overflow while negating one operand it does
462 if n is a power of two. */
463 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
464 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
465 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
466 && ! integer_pow2p (TREE_OPERAND (t, 0)))
467 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
468 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
469 break;
471 /* Fall through. */
473 case RDIV_EXPR:
474 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
475 return negate_expr_p (TREE_OPERAND (t, 1))
476 || negate_expr_p (TREE_OPERAND (t, 0));
477 break;
479 case TRUNC_DIV_EXPR:
480 case ROUND_DIV_EXPR:
481 case EXACT_DIV_EXPR:
482 if (TYPE_UNSIGNED (type))
483 break;
484 if (negate_expr_p (TREE_OPERAND (t, 0)))
485 return true;
486 /* In general we can't negate B in A / B, because if A is INT_MIN and
487 B is 1, we may turn this into INT_MIN / -1 which is undefined
488 and actually traps on some architectures. */
489 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
490 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
491 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
492 && ! integer_onep (TREE_OPERAND (t, 1))))
493 return negate_expr_p (TREE_OPERAND (t, 1));
494 break;
496 case NOP_EXPR:
497 /* Negate -((double)float) as (double)(-float). */
498 if (TREE_CODE (type) == REAL_TYPE)
500 tree tem = strip_float_extensions (t);
501 if (tem != t)
502 return negate_expr_p (tem);
504 break;
506 case CALL_EXPR:
507 /* Negate -f(x) as f(-x). */
508 if (negate_mathfn_p (get_call_combined_fn (t)))
509 return negate_expr_p (CALL_EXPR_ARG (t, 0));
510 break;
512 case RSHIFT_EXPR:
513 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
514 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
516 tree op1 = TREE_OPERAND (t, 1);
517 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
518 return true;
520 break;
522 default:
523 break;
525 return false;
528 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
529 simplification is possible.
530 If negate_expr_p would return true for T, NULL_TREE will never be
531 returned. */
533 static tree
534 fold_negate_expr (location_t loc, tree t)
536 tree type = TREE_TYPE (t);
537 tree tem;
539 switch (TREE_CODE (t))
541 /* Convert - (~A) to A + 1. */
542 case BIT_NOT_EXPR:
543 if (INTEGRAL_TYPE_P (type))
544 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
545 build_one_cst (type));
546 break;
548 case INTEGER_CST:
549 tem = fold_negate_const (t, type);
550 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
551 || (ANY_INTEGRAL_TYPE_P (type)
552 && !TYPE_OVERFLOW_TRAPS (type)
553 && TYPE_OVERFLOW_WRAPS (type))
554 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
555 return tem;
556 break;
558 case REAL_CST:
559 tem = fold_negate_const (t, type);
560 return tem;
562 case FIXED_CST:
563 tem = fold_negate_const (t, type);
564 return tem;
566 case COMPLEX_CST:
568 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
569 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
570 if (rpart && ipart)
571 return build_complex (type, rpart, ipart);
573 break;
575 case VECTOR_CST:
577 int count = TYPE_VECTOR_SUBPARTS (type), i;
578 tree *elts = XALLOCAVEC (tree, count);
580 for (i = 0; i < count; i++)
582 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
583 if (elts[i] == NULL_TREE)
584 return NULL_TREE;
587 return build_vector (type, elts);
590 case COMPLEX_EXPR:
591 if (negate_expr_p (t))
592 return fold_build2_loc (loc, COMPLEX_EXPR, type,
593 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
594 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
595 break;
597 case CONJ_EXPR:
598 if (negate_expr_p (t))
599 return fold_build1_loc (loc, CONJ_EXPR, type,
600 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
601 break;
603 case NEGATE_EXPR:
604 if (!TYPE_OVERFLOW_SANITIZED (type))
605 return TREE_OPERAND (t, 0);
606 break;
608 case PLUS_EXPR:
609 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
610 && !HONOR_SIGNED_ZEROS (element_mode (type)))
612 /* -(A + B) -> (-B) - A. */
613 if (negate_expr_p (TREE_OPERAND (t, 1))
614 && reorder_operands_p (TREE_OPERAND (t, 0),
615 TREE_OPERAND (t, 1)))
617 tem = negate_expr (TREE_OPERAND (t, 1));
618 return fold_build2_loc (loc, MINUS_EXPR, type,
619 tem, TREE_OPERAND (t, 0));
622 /* -(A + B) -> (-A) - B. */
623 if (negate_expr_p (TREE_OPERAND (t, 0)))
625 tem = negate_expr (TREE_OPERAND (t, 0));
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 tem, TREE_OPERAND (t, 1));
630 break;
632 case MINUS_EXPR:
633 /* - (A - B) -> B - A */
634 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
635 && !HONOR_SIGNED_ZEROS (element_mode (type))
636 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
637 return fold_build2_loc (loc, MINUS_EXPR, type,
638 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
639 break;
641 case MULT_EXPR:
642 if (TYPE_UNSIGNED (type))
643 break;
645 /* Fall through. */
647 case RDIV_EXPR:
648 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
650 tem = TREE_OPERAND (t, 1);
651 if (negate_expr_p (tem))
652 return fold_build2_loc (loc, TREE_CODE (t), type,
653 TREE_OPERAND (t, 0), negate_expr (tem));
654 tem = TREE_OPERAND (t, 0);
655 if (negate_expr_p (tem))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (tem), TREE_OPERAND (t, 1));
659 break;
661 case TRUNC_DIV_EXPR:
662 case ROUND_DIV_EXPR:
663 case EXACT_DIV_EXPR:
664 if (TYPE_UNSIGNED (type))
665 break;
666 if (negate_expr_p (TREE_OPERAND (t, 0)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 negate_expr (TREE_OPERAND (t, 0)),
669 TREE_OPERAND (t, 1));
670 /* In general we can't negate B in A / B, because if A is INT_MIN and
671 B is 1, we may turn this into INT_MIN / -1 which is undefined
672 and actually traps on some architectures. */
673 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
674 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
675 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
676 && ! integer_onep (TREE_OPERAND (t, 1))))
677 && negate_expr_p (TREE_OPERAND (t, 1)))
678 return fold_build2_loc (loc, TREE_CODE (t), type,
679 TREE_OPERAND (t, 0),
680 negate_expr (TREE_OPERAND (t, 1)));
681 break;
683 case NOP_EXPR:
684 /* Convert -((double)float) into (double)(-float). */
685 if (TREE_CODE (type) == REAL_TYPE)
687 tem = strip_float_extensions (t);
688 if (tem != t && negate_expr_p (tem))
689 return fold_convert_loc (loc, type, negate_expr (tem));
691 break;
693 case CALL_EXPR:
694 /* Negate -f(x) as f(-x). */
695 if (negate_mathfn_p (get_call_combined_fn (t))
696 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
698 tree fndecl, arg;
700 fndecl = get_callee_fndecl (t);
701 arg = negate_expr (CALL_EXPR_ARG (t, 0));
702 return build_call_expr_loc (loc, fndecl, 1, arg);
704 break;
706 case RSHIFT_EXPR:
707 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
708 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
710 tree op1 = TREE_OPERAND (t, 1);
711 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
713 tree ntype = TYPE_UNSIGNED (type)
714 ? signed_type_for (type)
715 : unsigned_type_for (type);
716 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
717 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
718 return fold_convert_loc (loc, type, temp);
721 break;
723 default:
724 break;
727 return NULL_TREE;
730 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
731 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
732 return NULL_TREE. */
734 static tree
735 negate_expr (tree t)
737 tree type, tem;
738 location_t loc;
740 if (t == NULL_TREE)
741 return NULL_TREE;
743 loc = EXPR_LOCATION (t);
744 type = TREE_TYPE (t);
745 STRIP_SIGN_NOPS (t);
747 tem = fold_negate_expr (loc, t);
748 if (!tem)
749 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
750 return fold_convert_loc (loc, type, tem);
753 /* Split a tree IN into a constant, literal and variable parts that could be
754 combined with CODE to make IN. "constant" means an expression with
755 TREE_CONSTANT but that isn't an actual constant. CODE must be a
756 commutative arithmetic operation. Store the constant part into *CONP,
757 the literal in *LITP and return the variable part. If a part isn't
758 present, set it to null. If the tree does not decompose in this way,
759 return the entire tree as the variable part and the other parts as null.
761 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
762 case, we negate an operand that was subtracted. Except if it is a
763 literal for which we use *MINUS_LITP instead.
765 If NEGATE_P is true, we are negating all of IN, again except a literal
766 for which we use *MINUS_LITP instead. If a variable part is of pointer
767 type, it is negated after converting to TYPE. This prevents us from
768 generating illegal MINUS pointer expression. LOC is the location of
769 the converted variable part.
771 If IN is itself a literal or constant, return it as appropriate.
773 Note that we do not guarantee that any of the three values will be the
774 same type as IN, but they will have the same signedness and mode. */
776 static tree
777 split_tree (location_t loc, tree in, tree type, enum tree_code code,
778 tree *conp, tree *litp, tree *minus_litp, int negate_p)
780 tree var = 0;
782 *conp = 0;
783 *litp = 0;
784 *minus_litp = 0;
786 /* Strip any conversions that don't change the machine mode or signedness. */
787 STRIP_SIGN_NOPS (in);
789 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
790 || TREE_CODE (in) == FIXED_CST)
791 *litp = in;
792 else if (TREE_CODE (in) == code
793 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
794 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
795 /* We can associate addition and subtraction together (even
796 though the C standard doesn't say so) for integers because
797 the value is not affected. For reals, the value might be
798 affected, so we can't. */
799 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
800 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
802 tree op0 = TREE_OPERAND (in, 0);
803 tree op1 = TREE_OPERAND (in, 1);
804 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
805 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
807 /* First see if either of the operands is a literal, then a constant. */
808 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
809 || TREE_CODE (op0) == FIXED_CST)
810 *litp = op0, op0 = 0;
811 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
812 || TREE_CODE (op1) == FIXED_CST)
813 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
815 if (op0 != 0 && TREE_CONSTANT (op0))
816 *conp = op0, op0 = 0;
817 else if (op1 != 0 && TREE_CONSTANT (op1))
818 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
820 /* If we haven't dealt with either operand, this is not a case we can
821 decompose. Otherwise, VAR is either of the ones remaining, if any. */
822 if (op0 != 0 && op1 != 0)
823 var = in;
824 else if (op0 != 0)
825 var = op0;
826 else
827 var = op1, neg_var_p = neg1_p;
829 /* Now do any needed negations. */
830 if (neg_litp_p)
831 *minus_litp = *litp, *litp = 0;
832 if (neg_conp_p)
833 *conp = negate_expr (*conp);
834 if (neg_var_p && var)
836 /* Convert to TYPE before negating. */
837 var = fold_convert_loc (loc, type, var);
838 var = negate_expr (var);
841 else if (TREE_CODE (in) == BIT_NOT_EXPR
842 && code == PLUS_EXPR)
844 /* -X - 1 is folded to ~X, undo that here. */
845 *minus_litp = build_one_cst (TREE_TYPE (in));
846 var = negate_expr (TREE_OPERAND (in, 0));
848 else if (TREE_CONSTANT (in))
849 *conp = in;
850 else
851 var = in;
853 if (negate_p)
855 if (*litp)
856 *minus_litp = *litp, *litp = 0;
857 else if (*minus_litp)
858 *litp = *minus_litp, *minus_litp = 0;
859 *conp = negate_expr (*conp);
860 if (var)
862 /* Convert to TYPE before negating. */
863 var = fold_convert_loc (loc, type, var);
864 var = negate_expr (var);
868 return var;
871 /* Re-associate trees split by the above function. T1 and T2 are
872 either expressions to associate or null. Return the new
873 expression, if any. LOC is the location of the new expression. If
874 we build an operation, do it in TYPE and with CODE. */
876 static tree
877 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
879 if (t1 == 0)
880 return t2;
881 else if (t2 == 0)
882 return t1;
884 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
885 try to fold this since we will have infinite recursion. But do
886 deal with any NEGATE_EXPRs. */
887 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
888 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
890 if (code == PLUS_EXPR)
892 if (TREE_CODE (t1) == NEGATE_EXPR)
893 return build2_loc (loc, MINUS_EXPR, type,
894 fold_convert_loc (loc, type, t2),
895 fold_convert_loc (loc, type,
896 TREE_OPERAND (t1, 0)));
897 else if (TREE_CODE (t2) == NEGATE_EXPR)
898 return build2_loc (loc, MINUS_EXPR, type,
899 fold_convert_loc (loc, type, t1),
900 fold_convert_loc (loc, type,
901 TREE_OPERAND (t2, 0)));
902 else if (integer_zerop (t2))
903 return fold_convert_loc (loc, type, t1);
905 else if (code == MINUS_EXPR)
907 if (integer_zerop (t2))
908 return fold_convert_loc (loc, type, t1);
911 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
912 fold_convert_loc (loc, type, t2));
915 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type, t2));
919 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
920 for use in int_const_binop, size_binop and size_diffop. */
922 static bool
923 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
925 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
926 return false;
927 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
928 return false;
930 switch (code)
932 case LSHIFT_EXPR:
933 case RSHIFT_EXPR:
934 case LROTATE_EXPR:
935 case RROTATE_EXPR:
936 return true;
938 default:
939 break;
942 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
943 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
944 && TYPE_MODE (type1) == TYPE_MODE (type2);
948 /* Combine two integer constants ARG1 and ARG2 under operation CODE
949 to produce a new constant. Return NULL_TREE if we don't know how
950 to evaluate CODE at compile-time. */
952 static tree
953 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
954 int overflowable)
956 wide_int res;
957 tree t;
958 tree type = TREE_TYPE (arg1);
959 signop sign = TYPE_SIGN (type);
960 bool overflow = false;
962 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
964 switch (code)
966 case BIT_IOR_EXPR:
967 res = wi::bit_or (arg1, arg2);
968 break;
970 case BIT_XOR_EXPR:
971 res = wi::bit_xor (arg1, arg2);
972 break;
974 case BIT_AND_EXPR:
975 res = wi::bit_and (arg1, arg2);
976 break;
978 case RSHIFT_EXPR:
979 case LSHIFT_EXPR:
980 if (wi::neg_p (arg2))
982 arg2 = -arg2;
983 if (code == RSHIFT_EXPR)
984 code = LSHIFT_EXPR;
985 else
986 code = RSHIFT_EXPR;
989 if (code == RSHIFT_EXPR)
990 /* It's unclear from the C standard whether shifts can overflow.
991 The following code ignores overflow; perhaps a C standard
992 interpretation ruling is needed. */
993 res = wi::rshift (arg1, arg2, sign);
994 else
995 res = wi::lshift (arg1, arg2);
996 break;
998 case RROTATE_EXPR:
999 case LROTATE_EXPR:
1000 if (wi::neg_p (arg2))
1002 arg2 = -arg2;
1003 if (code == RROTATE_EXPR)
1004 code = LROTATE_EXPR;
1005 else
1006 code = RROTATE_EXPR;
1009 if (code == RROTATE_EXPR)
1010 res = wi::rrotate (arg1, arg2);
1011 else
1012 res = wi::lrotate (arg1, arg2);
1013 break;
1015 case PLUS_EXPR:
1016 res = wi::add (arg1, arg2, sign, &overflow);
1017 break;
1019 case MINUS_EXPR:
1020 res = wi::sub (arg1, arg2, sign, &overflow);
1021 break;
1023 case MULT_EXPR:
1024 res = wi::mul (arg1, arg2, sign, &overflow);
1025 break;
1027 case MULT_HIGHPART_EXPR:
1028 res = wi::mul_high (arg1, arg2, sign);
1029 break;
1031 case TRUNC_DIV_EXPR:
1032 case EXACT_DIV_EXPR:
1033 if (arg2 == 0)
1034 return NULL_TREE;
1035 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1036 break;
1038 case FLOOR_DIV_EXPR:
1039 if (arg2 == 0)
1040 return NULL_TREE;
1041 res = wi::div_floor (arg1, arg2, sign, &overflow);
1042 break;
1044 case CEIL_DIV_EXPR:
1045 if (arg2 == 0)
1046 return NULL_TREE;
1047 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1048 break;
1050 case ROUND_DIV_EXPR:
1051 if (arg2 == 0)
1052 return NULL_TREE;
1053 res = wi::div_round (arg1, arg2, sign, &overflow);
1054 break;
1056 case TRUNC_MOD_EXPR:
1057 if (arg2 == 0)
1058 return NULL_TREE;
1059 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1060 break;
1062 case FLOOR_MOD_EXPR:
1063 if (arg2 == 0)
1064 return NULL_TREE;
1065 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1066 break;
1068 case CEIL_MOD_EXPR:
1069 if (arg2 == 0)
1070 return NULL_TREE;
1071 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1072 break;
1074 case ROUND_MOD_EXPR:
1075 if (arg2 == 0)
1076 return NULL_TREE;
1077 res = wi::mod_round (arg1, arg2, sign, &overflow);
1078 break;
1080 case MIN_EXPR:
1081 res = wi::min (arg1, arg2, sign);
1082 break;
1084 case MAX_EXPR:
1085 res = wi::max (arg1, arg2, sign);
1086 break;
1088 default:
1089 return NULL_TREE;
1092 t = force_fit_type (type, res, overflowable,
1093 (((sign == SIGNED || overflowable == -1)
1094 && overflow)
1095 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1097 return t;
1100 tree
1101 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1103 return int_const_binop_1 (code, arg1, arg2, 1);
1106 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1107 constant. We assume ARG1 and ARG2 have the same data type, or at least
1108 are the same kind of constant and the same machine mode. Return zero if
1109 combining the constants is not allowed in the current operating mode. */
1111 static tree
1112 const_binop (enum tree_code code, tree arg1, tree arg2)
1114 /* Sanity check for the recursive cases. */
1115 if (!arg1 || !arg2)
1116 return NULL_TREE;
1118 STRIP_NOPS (arg1);
1119 STRIP_NOPS (arg2);
1121 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1123 if (code == POINTER_PLUS_EXPR)
1124 return int_const_binop (PLUS_EXPR,
1125 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1127 return int_const_binop (code, arg1, arg2);
1130 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1132 machine_mode mode;
1133 REAL_VALUE_TYPE d1;
1134 REAL_VALUE_TYPE d2;
1135 REAL_VALUE_TYPE value;
1136 REAL_VALUE_TYPE result;
1137 bool inexact;
1138 tree t, type;
1140 /* The following codes are handled by real_arithmetic. */
1141 switch (code)
1143 case PLUS_EXPR:
1144 case MINUS_EXPR:
1145 case MULT_EXPR:
1146 case RDIV_EXPR:
1147 case MIN_EXPR:
1148 case MAX_EXPR:
1149 break;
1151 default:
1152 return NULL_TREE;
1155 d1 = TREE_REAL_CST (arg1);
1156 d2 = TREE_REAL_CST (arg2);
1158 type = TREE_TYPE (arg1);
1159 mode = TYPE_MODE (type);
1161 /* Don't perform operation if we honor signaling NaNs and
1162 either operand is a signaling NaN. */
1163 if (HONOR_SNANS (mode)
1164 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1165 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1166 return NULL_TREE;
1168 /* Don't perform operation if it would raise a division
1169 by zero exception. */
1170 if (code == RDIV_EXPR
1171 && real_equal (&d2, &dconst0)
1172 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1173 return NULL_TREE;
1175 /* If either operand is a NaN, just return it. Otherwise, set up
1176 for floating-point trap; we return an overflow. */
1177 if (REAL_VALUE_ISNAN (d1))
1179 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1180 is off. */
1181 d1.signalling = 0;
1182 t = build_real (type, d1);
1183 return t;
1185 else if (REAL_VALUE_ISNAN (d2))
1187 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1188 is off. */
1189 d2.signalling = 0;
1190 t = build_real (type, d2);
1191 return t;
1194 inexact = real_arithmetic (&value, code, &d1, &d2);
1195 real_convert (&result, mode, &value);
1197 /* Don't constant fold this floating point operation if
1198 the result has overflowed and flag_trapping_math. */
1199 if (flag_trapping_math
1200 && MODE_HAS_INFINITIES (mode)
1201 && REAL_VALUE_ISINF (result)
1202 && !REAL_VALUE_ISINF (d1)
1203 && !REAL_VALUE_ISINF (d2))
1204 return NULL_TREE;
1206 /* Don't constant fold this floating point operation if the
1207 result may dependent upon the run-time rounding mode and
1208 flag_rounding_math is set, or if GCC's software emulation
1209 is unable to accurately represent the result. */
1210 if ((flag_rounding_math
1211 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1212 && (inexact || !real_identical (&result, &value)))
1213 return NULL_TREE;
1215 t = build_real (type, result);
1217 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1218 return t;
1221 if (TREE_CODE (arg1) == FIXED_CST)
1223 FIXED_VALUE_TYPE f1;
1224 FIXED_VALUE_TYPE f2;
1225 FIXED_VALUE_TYPE result;
1226 tree t, type;
1227 int sat_p;
1228 bool overflow_p;
1230 /* The following codes are handled by fixed_arithmetic. */
1231 switch (code)
1233 case PLUS_EXPR:
1234 case MINUS_EXPR:
1235 case MULT_EXPR:
1236 case TRUNC_DIV_EXPR:
1237 if (TREE_CODE (arg2) != FIXED_CST)
1238 return NULL_TREE;
1239 f2 = TREE_FIXED_CST (arg2);
1240 break;
1242 case LSHIFT_EXPR:
1243 case RSHIFT_EXPR:
1245 if (TREE_CODE (arg2) != INTEGER_CST)
1246 return NULL_TREE;
1247 wide_int w2 = arg2;
1248 f2.data.high = w2.elt (1);
1249 f2.data.low = w2.elt (0);
1250 f2.mode = SImode;
1252 break;
1254 default:
1255 return NULL_TREE;
1258 f1 = TREE_FIXED_CST (arg1);
1259 type = TREE_TYPE (arg1);
1260 sat_p = TYPE_SATURATING (type);
1261 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1262 t = build_fixed (type, result);
1263 /* Propagate overflow flags. */
1264 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1265 TREE_OVERFLOW (t) = 1;
1266 return t;
1269 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1271 tree type = TREE_TYPE (arg1);
1272 tree r1 = TREE_REALPART (arg1);
1273 tree i1 = TREE_IMAGPART (arg1);
1274 tree r2 = TREE_REALPART (arg2);
1275 tree i2 = TREE_IMAGPART (arg2);
1276 tree real, imag;
1278 switch (code)
1280 case PLUS_EXPR:
1281 case MINUS_EXPR:
1282 real = const_binop (code, r1, r2);
1283 imag = const_binop (code, i1, i2);
1284 break;
1286 case MULT_EXPR:
1287 if (COMPLEX_FLOAT_TYPE_P (type))
1288 return do_mpc_arg2 (arg1, arg2, type,
1289 /* do_nonfinite= */ folding_initializer,
1290 mpc_mul);
1292 real = const_binop (MINUS_EXPR,
1293 const_binop (MULT_EXPR, r1, r2),
1294 const_binop (MULT_EXPR, i1, i2));
1295 imag = const_binop (PLUS_EXPR,
1296 const_binop (MULT_EXPR, r1, i2),
1297 const_binop (MULT_EXPR, i1, r2));
1298 break;
1300 case RDIV_EXPR:
1301 if (COMPLEX_FLOAT_TYPE_P (type))
1302 return do_mpc_arg2 (arg1, arg2, type,
1303 /* do_nonfinite= */ folding_initializer,
1304 mpc_div);
1305 /* Fallthru ... */
1306 case TRUNC_DIV_EXPR:
1307 case CEIL_DIV_EXPR:
1308 case FLOOR_DIV_EXPR:
1309 case ROUND_DIV_EXPR:
1310 if (flag_complex_method == 0)
1312 /* Keep this algorithm in sync with
1313 tree-complex.c:expand_complex_div_straight().
1315 Expand complex division to scalars, straightforward algorithm.
1316 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1317 t = br*br + bi*bi
1319 tree magsquared
1320 = const_binop (PLUS_EXPR,
1321 const_binop (MULT_EXPR, r2, r2),
1322 const_binop (MULT_EXPR, i2, i2));
1323 tree t1
1324 = const_binop (PLUS_EXPR,
1325 const_binop (MULT_EXPR, r1, r2),
1326 const_binop (MULT_EXPR, i1, i2));
1327 tree t2
1328 = const_binop (MINUS_EXPR,
1329 const_binop (MULT_EXPR, i1, r2),
1330 const_binop (MULT_EXPR, r1, i2));
1332 real = const_binop (code, t1, magsquared);
1333 imag = const_binop (code, t2, magsquared);
1335 else
1337 /* Keep this algorithm in sync with
1338 tree-complex.c:expand_complex_div_wide().
1340 Expand complex division to scalars, modified algorithm to minimize
1341 overflow with wide input ranges. */
1342 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1343 fold_abs_const (r2, TREE_TYPE (type)),
1344 fold_abs_const (i2, TREE_TYPE (type)));
1346 if (integer_nonzerop (compare))
1348 /* In the TRUE branch, we compute
1349 ratio = br/bi;
1350 div = (br * ratio) + bi;
1351 tr = (ar * ratio) + ai;
1352 ti = (ai * ratio) - ar;
1353 tr = tr / div;
1354 ti = ti / div; */
1355 tree ratio = const_binop (code, r2, i2);
1356 tree div = const_binop (PLUS_EXPR, i2,
1357 const_binop (MULT_EXPR, r2, ratio));
1358 real = const_binop (MULT_EXPR, r1, ratio);
1359 real = const_binop (PLUS_EXPR, real, i1);
1360 real = const_binop (code, real, div);
1362 imag = const_binop (MULT_EXPR, i1, ratio);
1363 imag = const_binop (MINUS_EXPR, imag, r1);
1364 imag = const_binop (code, imag, div);
1366 else
1368 /* In the FALSE branch, we compute
1369 ratio = d/c;
1370 divisor = (d * ratio) + c;
1371 tr = (b * ratio) + a;
1372 ti = b - (a * ratio);
1373 tr = tr / div;
1374 ti = ti / div; */
1375 tree ratio = const_binop (code, i2, r2);
1376 tree div = const_binop (PLUS_EXPR, r2,
1377 const_binop (MULT_EXPR, i2, ratio));
1379 real = const_binop (MULT_EXPR, i1, ratio);
1380 real = const_binop (PLUS_EXPR, real, r1);
1381 real = const_binop (code, real, div);
1383 imag = const_binop (MULT_EXPR, r1, ratio);
1384 imag = const_binop (MINUS_EXPR, i1, imag);
1385 imag = const_binop (code, imag, div);
1388 break;
1390 default:
1391 return NULL_TREE;
1394 if (real && imag)
1395 return build_complex (type, real, imag);
1398 if (TREE_CODE (arg1) == VECTOR_CST
1399 && TREE_CODE (arg2) == VECTOR_CST)
1401 tree type = TREE_TYPE (arg1);
1402 int count = TYPE_VECTOR_SUBPARTS (type), i;
1403 tree *elts = XALLOCAVEC (tree, count);
1405 for (i = 0; i < count; i++)
1407 tree elem1 = VECTOR_CST_ELT (arg1, i);
1408 tree elem2 = VECTOR_CST_ELT (arg2, i);
1410 elts[i] = const_binop (code, elem1, elem2);
1412 /* It is possible that const_binop cannot handle the given
1413 code and return NULL_TREE */
1414 if (elts[i] == NULL_TREE)
1415 return NULL_TREE;
1418 return build_vector (type, elts);
1421 /* Shifts allow a scalar offset for a vector. */
1422 if (TREE_CODE (arg1) == VECTOR_CST
1423 && TREE_CODE (arg2) == INTEGER_CST)
1425 tree type = TREE_TYPE (arg1);
1426 int count = TYPE_VECTOR_SUBPARTS (type), i;
1427 tree *elts = XALLOCAVEC (tree, count);
1429 for (i = 0; i < count; i++)
1431 tree elem1 = VECTOR_CST_ELT (arg1, i);
1433 elts[i] = const_binop (code, elem1, arg2);
1435 /* It is possible that const_binop cannot handle the given
1436 code and return NULL_TREE. */
1437 if (elts[i] == NULL_TREE)
1438 return NULL_TREE;
1441 return build_vector (type, elts);
1443 return NULL_TREE;
1446 /* Overload that adds a TYPE parameter to be able to dispatch
1447 to fold_relational_const. */
1449 tree
1450 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1452 if (TREE_CODE_CLASS (code) == tcc_comparison)
1453 return fold_relational_const (code, type, arg1, arg2);
1455 /* ??? Until we make the const_binop worker take the type of the
1456 result as argument put those cases that need it here. */
1457 switch (code)
1459 case COMPLEX_EXPR:
1460 if ((TREE_CODE (arg1) == REAL_CST
1461 && TREE_CODE (arg2) == REAL_CST)
1462 || (TREE_CODE (arg1) == INTEGER_CST
1463 && TREE_CODE (arg2) == INTEGER_CST))
1464 return build_complex (type, arg1, arg2);
1465 return NULL_TREE;
1467 case VEC_PACK_TRUNC_EXPR:
1468 case VEC_PACK_FIX_TRUNC_EXPR:
1470 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1471 tree *elts;
1473 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1474 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1475 if (TREE_CODE (arg1) != VECTOR_CST
1476 || TREE_CODE (arg2) != VECTOR_CST)
1477 return NULL_TREE;
1479 elts = XALLOCAVEC (tree, nelts);
1480 if (!vec_cst_ctor_to_array (arg1, elts)
1481 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1482 return NULL_TREE;
1484 for (i = 0; i < nelts; i++)
1486 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1487 ? NOP_EXPR : FIX_TRUNC_EXPR,
1488 TREE_TYPE (type), elts[i]);
1489 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1490 return NULL_TREE;
1493 return build_vector (type, elts);
1496 case VEC_WIDEN_MULT_LO_EXPR:
1497 case VEC_WIDEN_MULT_HI_EXPR:
1498 case VEC_WIDEN_MULT_EVEN_EXPR:
1499 case VEC_WIDEN_MULT_ODD_EXPR:
1501 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1502 unsigned int out, ofs, scale;
1503 tree *elts;
1505 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1506 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1507 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1508 return NULL_TREE;
1510 elts = XALLOCAVEC (tree, nelts * 4);
1511 if (!vec_cst_ctor_to_array (arg1, elts)
1512 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1513 return NULL_TREE;
1515 if (code == VEC_WIDEN_MULT_LO_EXPR)
1516 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1517 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1518 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1519 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1520 scale = 1, ofs = 0;
1521 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1522 scale = 1, ofs = 1;
1524 for (out = 0; out < nelts; out++)
1526 unsigned int in1 = (out << scale) + ofs;
1527 unsigned int in2 = in1 + nelts * 2;
1528 tree t1, t2;
1530 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1531 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1533 if (t1 == NULL_TREE || t2 == NULL_TREE)
1534 return NULL_TREE;
1535 elts[out] = const_binop (MULT_EXPR, t1, t2);
1536 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1537 return NULL_TREE;
1540 return build_vector (type, elts);
1543 default:;
1546 if (TREE_CODE_CLASS (code) != tcc_binary)
1547 return NULL_TREE;
1549 /* Make sure type and arg0 have the same saturating flag. */
1550 gcc_checking_assert (TYPE_SATURATING (type)
1551 == TYPE_SATURATING (TREE_TYPE (arg1)));
1553 return const_binop (code, arg1, arg2);
1556 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1557 Return zero if computing the constants is not possible. */
1559 tree
1560 const_unop (enum tree_code code, tree type, tree arg0)
1562 /* Don't perform the operation, other than NEGATE and ABS, if
1563 flag_signaling_nans is on and the operand is a signaling NaN. */
1564 if (TREE_CODE (arg0) == REAL_CST
1565 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1566 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1567 && code != NEGATE_EXPR
1568 && code != ABS_EXPR)
1569 return NULL_TREE;
1571 switch (code)
1573 CASE_CONVERT:
1574 case FLOAT_EXPR:
1575 case FIX_TRUNC_EXPR:
1576 case FIXED_CONVERT_EXPR:
1577 return fold_convert_const (code, type, arg0);
1579 case ADDR_SPACE_CONVERT_EXPR:
1580 /* If the source address is 0, and the source address space
1581 cannot have a valid object at 0, fold to dest type null. */
1582 if (integer_zerop (arg0)
1583 && !(targetm.addr_space.zero_address_valid
1584 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1585 return fold_convert_const (code, type, arg0);
1586 break;
1588 case VIEW_CONVERT_EXPR:
1589 return fold_view_convert_expr (type, arg0);
1591 case NEGATE_EXPR:
1593 /* Can't call fold_negate_const directly here as that doesn't
1594 handle all cases and we might not be able to negate some
1595 constants. */
1596 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1597 if (tem && CONSTANT_CLASS_P (tem))
1598 return tem;
1599 break;
1602 case ABS_EXPR:
1603 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1604 return fold_abs_const (arg0, type);
1605 break;
1607 case CONJ_EXPR:
1608 if (TREE_CODE (arg0) == COMPLEX_CST)
1610 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1611 TREE_TYPE (type));
1612 return build_complex (type, TREE_REALPART (arg0), ipart);
1614 break;
1616 case BIT_NOT_EXPR:
1617 if (TREE_CODE (arg0) == INTEGER_CST)
1618 return fold_not_const (arg0, type);
1619 /* Perform BIT_NOT_EXPR on each element individually. */
1620 else if (TREE_CODE (arg0) == VECTOR_CST)
1622 tree *elements;
1623 tree elem;
1624 unsigned count = VECTOR_CST_NELTS (arg0), i;
1626 elements = XALLOCAVEC (tree, count);
1627 for (i = 0; i < count; i++)
1629 elem = VECTOR_CST_ELT (arg0, i);
1630 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1631 if (elem == NULL_TREE)
1632 break;
1633 elements[i] = elem;
1635 if (i == count)
1636 return build_vector (type, elements);
1638 break;
1640 case TRUTH_NOT_EXPR:
1641 if (TREE_CODE (arg0) == INTEGER_CST)
1642 return constant_boolean_node (integer_zerop (arg0), type);
1643 break;
1645 case REALPART_EXPR:
1646 if (TREE_CODE (arg0) == COMPLEX_CST)
1647 return fold_convert (type, TREE_REALPART (arg0));
1648 break;
1650 case IMAGPART_EXPR:
1651 if (TREE_CODE (arg0) == COMPLEX_CST)
1652 return fold_convert (type, TREE_IMAGPART (arg0));
1653 break;
1655 case VEC_UNPACK_LO_EXPR:
1656 case VEC_UNPACK_HI_EXPR:
1657 case VEC_UNPACK_FLOAT_LO_EXPR:
1658 case VEC_UNPACK_FLOAT_HI_EXPR:
1660 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1661 tree *elts;
1662 enum tree_code subcode;
1664 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1665 if (TREE_CODE (arg0) != VECTOR_CST)
1666 return NULL_TREE;
1668 elts = XALLOCAVEC (tree, nelts * 2);
1669 if (!vec_cst_ctor_to_array (arg0, elts))
1670 return NULL_TREE;
1672 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1673 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1674 elts += nelts;
1676 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1677 subcode = NOP_EXPR;
1678 else
1679 subcode = FLOAT_EXPR;
1681 for (i = 0; i < nelts; i++)
1683 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1684 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1685 return NULL_TREE;
1688 return build_vector (type, elts);
1691 case REDUC_MIN_EXPR:
1692 case REDUC_MAX_EXPR:
1693 case REDUC_PLUS_EXPR:
1695 unsigned int nelts, i;
1696 tree *elts;
1697 enum tree_code subcode;
1699 if (TREE_CODE (arg0) != VECTOR_CST)
1700 return NULL_TREE;
1701 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1703 elts = XALLOCAVEC (tree, nelts);
1704 if (!vec_cst_ctor_to_array (arg0, elts))
1705 return NULL_TREE;
1707 switch (code)
1709 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1710 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1711 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1712 default: gcc_unreachable ();
1715 for (i = 1; i < nelts; i++)
1717 elts[0] = const_binop (subcode, elts[0], elts[i]);
1718 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1719 return NULL_TREE;
1722 return elts[0];
1725 default:
1726 break;
1729 return NULL_TREE;
1732 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1733 indicates which particular sizetype to create. */
1735 tree
1736 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1738 return build_int_cst (sizetype_tab[(int) kind], number);
1741 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1742 is a tree code. The type of the result is taken from the operands.
1743 Both must be equivalent integer types, ala int_binop_types_match_p.
1744 If the operands are constant, so is the result. */
1746 tree
1747 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1749 tree type = TREE_TYPE (arg0);
1751 if (arg0 == error_mark_node || arg1 == error_mark_node)
1752 return error_mark_node;
1754 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1755 TREE_TYPE (arg1)));
1757 /* Handle the special case of two integer constants faster. */
1758 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1760 /* And some specific cases even faster than that. */
1761 if (code == PLUS_EXPR)
1763 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1764 return arg1;
1765 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1766 return arg0;
1768 else if (code == MINUS_EXPR)
1770 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1771 return arg0;
1773 else if (code == MULT_EXPR)
1775 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1776 return arg1;
1779 /* Handle general case of two integer constants. For sizetype
1780 constant calculations we always want to know about overflow,
1781 even in the unsigned case. */
1782 return int_const_binop_1 (code, arg0, arg1, -1);
1785 return fold_build2_loc (loc, code, type, arg0, arg1);
1788 /* Given two values, either both of sizetype or both of bitsizetype,
1789 compute the difference between the two values. Return the value
1790 in signed type corresponding to the type of the operands. */
1792 tree
1793 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1795 tree type = TREE_TYPE (arg0);
1796 tree ctype;
1798 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1799 TREE_TYPE (arg1)));
1801 /* If the type is already signed, just do the simple thing. */
1802 if (!TYPE_UNSIGNED (type))
1803 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1805 if (type == sizetype)
1806 ctype = ssizetype;
1807 else if (type == bitsizetype)
1808 ctype = sbitsizetype;
1809 else
1810 ctype = signed_type_for (type);
1812 /* If either operand is not a constant, do the conversions to the signed
1813 type and subtract. The hardware will do the right thing with any
1814 overflow in the subtraction. */
1815 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1816 return size_binop_loc (loc, MINUS_EXPR,
1817 fold_convert_loc (loc, ctype, arg0),
1818 fold_convert_loc (loc, ctype, arg1));
1820 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1821 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1822 overflow) and negate (which can't either). Special-case a result
1823 of zero while we're here. */
1824 if (tree_int_cst_equal (arg0, arg1))
1825 return build_int_cst (ctype, 0);
1826 else if (tree_int_cst_lt (arg1, arg0))
1827 return fold_convert_loc (loc, ctype,
1828 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1829 else
1830 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1831 fold_convert_loc (loc, ctype,
1832 size_binop_loc (loc,
1833 MINUS_EXPR,
1834 arg1, arg0)));
1837 /* A subroutine of fold_convert_const handling conversions of an
1838 INTEGER_CST to another integer type. */
1840 static tree
1841 fold_convert_const_int_from_int (tree type, const_tree arg1)
1843 /* Given an integer constant, make new constant with new type,
1844 appropriately sign-extended or truncated. Use widest_int
1845 so that any extension is done according ARG1's type. */
1846 return force_fit_type (type, wi::to_widest (arg1),
1847 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1848 TREE_OVERFLOW (arg1));
1851 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1852 to an integer type. */
1854 static tree
1855 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1857 bool overflow = false;
1858 tree t;
1860 /* The following code implements the floating point to integer
1861 conversion rules required by the Java Language Specification,
1862 that IEEE NaNs are mapped to zero and values that overflow
1863 the target precision saturate, i.e. values greater than
1864 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1865 are mapped to INT_MIN. These semantics are allowed by the
1866 C and C++ standards that simply state that the behavior of
1867 FP-to-integer conversion is unspecified upon overflow. */
1869 wide_int val;
1870 REAL_VALUE_TYPE r;
1871 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1873 switch (code)
1875 case FIX_TRUNC_EXPR:
1876 real_trunc (&r, VOIDmode, &x);
1877 break;
1879 default:
1880 gcc_unreachable ();
1883 /* If R is NaN, return zero and show we have an overflow. */
1884 if (REAL_VALUE_ISNAN (r))
1886 overflow = true;
1887 val = wi::zero (TYPE_PRECISION (type));
1890 /* See if R is less than the lower bound or greater than the
1891 upper bound. */
1893 if (! overflow)
1895 tree lt = TYPE_MIN_VALUE (type);
1896 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1897 if (real_less (&r, &l))
1899 overflow = true;
1900 val = lt;
1904 if (! overflow)
1906 tree ut = TYPE_MAX_VALUE (type);
1907 if (ut)
1909 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1910 if (real_less (&u, &r))
1912 overflow = true;
1913 val = ut;
1918 if (! overflow)
1919 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1921 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1922 return t;
1925 /* A subroutine of fold_convert_const handling conversions of a
1926 FIXED_CST to an integer type. */
1928 static tree
1929 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1931 tree t;
1932 double_int temp, temp_trunc;
1933 unsigned int mode;
1935 /* Right shift FIXED_CST to temp by fbit. */
1936 temp = TREE_FIXED_CST (arg1).data;
1937 mode = TREE_FIXED_CST (arg1).mode;
1938 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1940 temp = temp.rshift (GET_MODE_FBIT (mode),
1941 HOST_BITS_PER_DOUBLE_INT,
1942 SIGNED_FIXED_POINT_MODE_P (mode));
1944 /* Left shift temp to temp_trunc by fbit. */
1945 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1946 HOST_BITS_PER_DOUBLE_INT,
1947 SIGNED_FIXED_POINT_MODE_P (mode));
1949 else
1951 temp = double_int_zero;
1952 temp_trunc = double_int_zero;
1955 /* If FIXED_CST is negative, we need to round the value toward 0.
1956 By checking if the fractional bits are not zero to add 1 to temp. */
1957 if (SIGNED_FIXED_POINT_MODE_P (mode)
1958 && temp_trunc.is_negative ()
1959 && TREE_FIXED_CST (arg1).data != temp_trunc)
1960 temp += double_int_one;
1962 /* Given a fixed-point constant, make new constant with new type,
1963 appropriately sign-extended or truncated. */
1964 t = force_fit_type (type, temp, -1,
1965 (temp.is_negative ()
1966 && (TYPE_UNSIGNED (type)
1967 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1968 | TREE_OVERFLOW (arg1));
1970 return t;
1973 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1974 to another floating point type. */
1976 static tree
1977 fold_convert_const_real_from_real (tree type, const_tree arg1)
1979 REAL_VALUE_TYPE value;
1980 tree t;
1982 /* Don't perform the operation if flag_signaling_nans is on
1983 and the operand is a signaling NaN. */
1984 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1985 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1986 return NULL_TREE;
1988 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1989 t = build_real (type, value);
1991 /* If converting an infinity or NAN to a representation that doesn't
1992 have one, set the overflow bit so that we can produce some kind of
1993 error message at the appropriate point if necessary. It's not the
1994 most user-friendly message, but it's better than nothing. */
1995 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1996 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1997 TREE_OVERFLOW (t) = 1;
1998 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1999 && !MODE_HAS_NANS (TYPE_MODE (type)))
2000 TREE_OVERFLOW (t) = 1;
2001 /* Regular overflow, conversion produced an infinity in a mode that
2002 can't represent them. */
2003 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2004 && REAL_VALUE_ISINF (value)
2005 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2006 TREE_OVERFLOW (t) = 1;
2007 else
2008 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2009 return t;
2012 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2013 to a floating point type. */
2015 static tree
2016 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2018 REAL_VALUE_TYPE value;
2019 tree t;
2021 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2022 t = build_real (type, value);
2024 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2025 return t;
2028 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2029 to another fixed-point type. */
2031 static tree
2032 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2034 FIXED_VALUE_TYPE value;
2035 tree t;
2036 bool overflow_p;
2038 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2039 TYPE_SATURATING (type));
2040 t = build_fixed (type, value);
2042 /* Propagate overflow flags. */
2043 if (overflow_p | TREE_OVERFLOW (arg1))
2044 TREE_OVERFLOW (t) = 1;
2045 return t;
2048 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2049 to a fixed-point type. */
2051 static tree
2052 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2054 FIXED_VALUE_TYPE value;
2055 tree t;
2056 bool overflow_p;
2057 double_int di;
2059 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2061 di.low = TREE_INT_CST_ELT (arg1, 0);
2062 if (TREE_INT_CST_NUNITS (arg1) == 1)
2063 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2064 else
2065 di.high = TREE_INT_CST_ELT (arg1, 1);
2067 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2068 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2069 TYPE_SATURATING (type));
2070 t = build_fixed (type, value);
2072 /* Propagate overflow flags. */
2073 if (overflow_p | TREE_OVERFLOW (arg1))
2074 TREE_OVERFLOW (t) = 1;
2075 return t;
2078 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2079 to a fixed-point type. */
2081 static tree
2082 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2084 FIXED_VALUE_TYPE value;
2085 tree t;
2086 bool overflow_p;
2088 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2089 &TREE_REAL_CST (arg1),
2090 TYPE_SATURATING (type));
2091 t = build_fixed (type, value);
2093 /* Propagate overflow flags. */
2094 if (overflow_p | TREE_OVERFLOW (arg1))
2095 TREE_OVERFLOW (t) = 1;
2096 return t;
2099 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2100 type TYPE. If no simplification can be done return NULL_TREE. */
2102 static tree
2103 fold_convert_const (enum tree_code code, tree type, tree arg1)
2105 if (TREE_TYPE (arg1) == type)
2106 return arg1;
2108 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2109 || TREE_CODE (type) == OFFSET_TYPE)
2111 if (TREE_CODE (arg1) == INTEGER_CST)
2112 return fold_convert_const_int_from_int (type, arg1);
2113 else if (TREE_CODE (arg1) == REAL_CST)
2114 return fold_convert_const_int_from_real (code, type, arg1);
2115 else if (TREE_CODE (arg1) == FIXED_CST)
2116 return fold_convert_const_int_from_fixed (type, arg1);
2118 else if (TREE_CODE (type) == REAL_TYPE)
2120 if (TREE_CODE (arg1) == INTEGER_CST)
2121 return build_real_from_int_cst (type, arg1);
2122 else if (TREE_CODE (arg1) == REAL_CST)
2123 return fold_convert_const_real_from_real (type, arg1);
2124 else if (TREE_CODE (arg1) == FIXED_CST)
2125 return fold_convert_const_real_from_fixed (type, arg1);
2127 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2129 if (TREE_CODE (arg1) == FIXED_CST)
2130 return fold_convert_const_fixed_from_fixed (type, arg1);
2131 else if (TREE_CODE (arg1) == INTEGER_CST)
2132 return fold_convert_const_fixed_from_int (type, arg1);
2133 else if (TREE_CODE (arg1) == REAL_CST)
2134 return fold_convert_const_fixed_from_real (type, arg1);
2136 else if (TREE_CODE (type) == VECTOR_TYPE)
2138 if (TREE_CODE (arg1) == VECTOR_CST
2139 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2141 int len = TYPE_VECTOR_SUBPARTS (type);
2142 tree elttype = TREE_TYPE (type);
2143 tree *v = XALLOCAVEC (tree, len);
2144 for (int i = 0; i < len; ++i)
2146 tree elt = VECTOR_CST_ELT (arg1, i);
2147 tree cvt = fold_convert_const (code, elttype, elt);
2148 if (cvt == NULL_TREE)
2149 return NULL_TREE;
2150 v[i] = cvt;
2152 return build_vector (type, v);
2155 return NULL_TREE;
2158 /* Construct a vector of zero elements of vector type TYPE. */
2160 static tree
2161 build_zero_vector (tree type)
2163 tree t;
2165 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2166 return build_vector_from_val (type, t);
2169 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2171 bool
2172 fold_convertible_p (const_tree type, const_tree arg)
2174 tree orig = TREE_TYPE (arg);
2176 if (type == orig)
2177 return true;
2179 if (TREE_CODE (arg) == ERROR_MARK
2180 || TREE_CODE (type) == ERROR_MARK
2181 || TREE_CODE (orig) == ERROR_MARK)
2182 return false;
2184 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2185 return true;
2187 switch (TREE_CODE (type))
2189 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2190 case POINTER_TYPE: case REFERENCE_TYPE:
2191 case OFFSET_TYPE:
2192 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2193 || TREE_CODE (orig) == OFFSET_TYPE);
2195 case REAL_TYPE:
2196 case FIXED_POINT_TYPE:
2197 case COMPLEX_TYPE:
2198 case VECTOR_TYPE:
2199 case VOID_TYPE:
2200 return TREE_CODE (type) == TREE_CODE (orig);
2202 default:
2203 return false;
2207 /* Convert expression ARG to type TYPE. Used by the middle-end for
2208 simple conversions in preference to calling the front-end's convert. */
2210 tree
2211 fold_convert_loc (location_t loc, tree type, tree arg)
2213 tree orig = TREE_TYPE (arg);
2214 tree tem;
2216 if (type == orig)
2217 return arg;
2219 if (TREE_CODE (arg) == ERROR_MARK
2220 || TREE_CODE (type) == ERROR_MARK
2221 || TREE_CODE (orig) == ERROR_MARK)
2222 return error_mark_node;
2224 switch (TREE_CODE (type))
2226 case POINTER_TYPE:
2227 case REFERENCE_TYPE:
2228 /* Handle conversions between pointers to different address spaces. */
2229 if (POINTER_TYPE_P (orig)
2230 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2231 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2232 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2233 /* fall through */
2235 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2236 case OFFSET_TYPE:
2237 if (TREE_CODE (arg) == INTEGER_CST)
2239 tem = fold_convert_const (NOP_EXPR, type, arg);
2240 if (tem != NULL_TREE)
2241 return tem;
2243 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2244 || TREE_CODE (orig) == OFFSET_TYPE)
2245 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2246 if (TREE_CODE (orig) == COMPLEX_TYPE)
2247 return fold_convert_loc (loc, type,
2248 fold_build1_loc (loc, REALPART_EXPR,
2249 TREE_TYPE (orig), arg));
2250 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2251 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2252 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2254 case REAL_TYPE:
2255 if (TREE_CODE (arg) == INTEGER_CST)
2257 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2258 if (tem != NULL_TREE)
2259 return tem;
2261 else if (TREE_CODE (arg) == REAL_CST)
2263 tem = fold_convert_const (NOP_EXPR, type, arg);
2264 if (tem != NULL_TREE)
2265 return tem;
2267 else if (TREE_CODE (arg) == FIXED_CST)
2269 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2270 if (tem != NULL_TREE)
2271 return tem;
2274 switch (TREE_CODE (orig))
2276 case INTEGER_TYPE:
2277 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2278 case POINTER_TYPE: case REFERENCE_TYPE:
2279 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2281 case REAL_TYPE:
2282 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2284 case FIXED_POINT_TYPE:
2285 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2287 case COMPLEX_TYPE:
2288 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2289 return fold_convert_loc (loc, type, tem);
2291 default:
2292 gcc_unreachable ();
2295 case FIXED_POINT_TYPE:
2296 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2297 || TREE_CODE (arg) == REAL_CST)
2299 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2300 if (tem != NULL_TREE)
2301 goto fold_convert_exit;
2304 switch (TREE_CODE (orig))
2306 case FIXED_POINT_TYPE:
2307 case INTEGER_TYPE:
2308 case ENUMERAL_TYPE:
2309 case BOOLEAN_TYPE:
2310 case REAL_TYPE:
2311 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2313 case COMPLEX_TYPE:
2314 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2315 return fold_convert_loc (loc, type, tem);
2317 default:
2318 gcc_unreachable ();
2321 case COMPLEX_TYPE:
2322 switch (TREE_CODE (orig))
2324 case INTEGER_TYPE:
2325 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2326 case POINTER_TYPE: case REFERENCE_TYPE:
2327 case REAL_TYPE:
2328 case FIXED_POINT_TYPE:
2329 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2330 fold_convert_loc (loc, TREE_TYPE (type), arg),
2331 fold_convert_loc (loc, TREE_TYPE (type),
2332 integer_zero_node));
2333 case COMPLEX_TYPE:
2335 tree rpart, ipart;
2337 if (TREE_CODE (arg) == COMPLEX_EXPR)
2339 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2340 TREE_OPERAND (arg, 0));
2341 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2342 TREE_OPERAND (arg, 1));
2343 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2346 arg = save_expr (arg);
2347 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2348 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2349 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2350 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2351 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2354 default:
2355 gcc_unreachable ();
2358 case VECTOR_TYPE:
2359 if (integer_zerop (arg))
2360 return build_zero_vector (type);
2361 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2362 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2363 || TREE_CODE (orig) == VECTOR_TYPE);
2364 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2366 case VOID_TYPE:
2367 tem = fold_ignored_result (arg);
2368 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2370 default:
2371 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2372 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2373 gcc_unreachable ();
2375 fold_convert_exit:
2376 protected_set_expr_location_unshare (tem, loc);
2377 return tem;
2380 /* Return false if expr can be assumed not to be an lvalue, true
2381 otherwise. */
2383 static bool
2384 maybe_lvalue_p (const_tree x)
2386 /* We only need to wrap lvalue tree codes. */
2387 switch (TREE_CODE (x))
2389 case VAR_DECL:
2390 case PARM_DECL:
2391 case RESULT_DECL:
2392 case LABEL_DECL:
2393 case FUNCTION_DECL:
2394 case SSA_NAME:
2396 case COMPONENT_REF:
2397 case MEM_REF:
2398 case INDIRECT_REF:
2399 case ARRAY_REF:
2400 case ARRAY_RANGE_REF:
2401 case BIT_FIELD_REF:
2402 case OBJ_TYPE_REF:
2404 case REALPART_EXPR:
2405 case IMAGPART_EXPR:
2406 case PREINCREMENT_EXPR:
2407 case PREDECREMENT_EXPR:
2408 case SAVE_EXPR:
2409 case TRY_CATCH_EXPR:
2410 case WITH_CLEANUP_EXPR:
2411 case COMPOUND_EXPR:
2412 case MODIFY_EXPR:
2413 case TARGET_EXPR:
2414 case COND_EXPR:
2415 case BIND_EXPR:
2416 break;
2418 default:
2419 /* Assume the worst for front-end tree codes. */
2420 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2421 break;
2422 return false;
2425 return true;
2428 /* Return an expr equal to X but certainly not valid as an lvalue. */
2430 tree
2431 non_lvalue_loc (location_t loc, tree x)
2433 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2434 us. */
2435 if (in_gimple_form)
2436 return x;
2438 if (! maybe_lvalue_p (x))
2439 return x;
2440 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2443 /* When pedantic, return an expr equal to X but certainly not valid as a
2444 pedantic lvalue. Otherwise, return X. */
2446 static tree
2447 pedantic_non_lvalue_loc (location_t loc, tree x)
2449 return protected_set_expr_location_unshare (x, loc);
2452 /* Given a tree comparison code, return the code that is the logical inverse.
2453 It is generally not safe to do this for floating-point comparisons, except
2454 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2455 ERROR_MARK in this case. */
2457 enum tree_code
2458 invert_tree_comparison (enum tree_code code, bool honor_nans)
2460 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2461 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2462 return ERROR_MARK;
2464 switch (code)
2466 case EQ_EXPR:
2467 return NE_EXPR;
2468 case NE_EXPR:
2469 return EQ_EXPR;
2470 case GT_EXPR:
2471 return honor_nans ? UNLE_EXPR : LE_EXPR;
2472 case GE_EXPR:
2473 return honor_nans ? UNLT_EXPR : LT_EXPR;
2474 case LT_EXPR:
2475 return honor_nans ? UNGE_EXPR : GE_EXPR;
2476 case LE_EXPR:
2477 return honor_nans ? UNGT_EXPR : GT_EXPR;
2478 case LTGT_EXPR:
2479 return UNEQ_EXPR;
2480 case UNEQ_EXPR:
2481 return LTGT_EXPR;
2482 case UNGT_EXPR:
2483 return LE_EXPR;
2484 case UNGE_EXPR:
2485 return LT_EXPR;
2486 case UNLT_EXPR:
2487 return GE_EXPR;
2488 case UNLE_EXPR:
2489 return GT_EXPR;
2490 case ORDERED_EXPR:
2491 return UNORDERED_EXPR;
2492 case UNORDERED_EXPR:
2493 return ORDERED_EXPR;
2494 default:
2495 gcc_unreachable ();
2499 /* Similar, but return the comparison that results if the operands are
2500 swapped. This is safe for floating-point. */
2502 enum tree_code
2503 swap_tree_comparison (enum tree_code code)
2505 switch (code)
2507 case EQ_EXPR:
2508 case NE_EXPR:
2509 case ORDERED_EXPR:
2510 case UNORDERED_EXPR:
2511 case LTGT_EXPR:
2512 case UNEQ_EXPR:
2513 return code;
2514 case GT_EXPR:
2515 return LT_EXPR;
2516 case GE_EXPR:
2517 return LE_EXPR;
2518 case LT_EXPR:
2519 return GT_EXPR;
2520 case LE_EXPR:
2521 return GE_EXPR;
2522 case UNGT_EXPR:
2523 return UNLT_EXPR;
2524 case UNGE_EXPR:
2525 return UNLE_EXPR;
2526 case UNLT_EXPR:
2527 return UNGT_EXPR;
2528 case UNLE_EXPR:
2529 return UNGE_EXPR;
2530 default:
2531 gcc_unreachable ();
2536 /* Convert a comparison tree code from an enum tree_code representation
2537 into a compcode bit-based encoding. This function is the inverse of
2538 compcode_to_comparison. */
2540 static enum comparison_code
2541 comparison_to_compcode (enum tree_code code)
2543 switch (code)
2545 case LT_EXPR:
2546 return COMPCODE_LT;
2547 case EQ_EXPR:
2548 return COMPCODE_EQ;
2549 case LE_EXPR:
2550 return COMPCODE_LE;
2551 case GT_EXPR:
2552 return COMPCODE_GT;
2553 case NE_EXPR:
2554 return COMPCODE_NE;
2555 case GE_EXPR:
2556 return COMPCODE_GE;
2557 case ORDERED_EXPR:
2558 return COMPCODE_ORD;
2559 case UNORDERED_EXPR:
2560 return COMPCODE_UNORD;
2561 case UNLT_EXPR:
2562 return COMPCODE_UNLT;
2563 case UNEQ_EXPR:
2564 return COMPCODE_UNEQ;
2565 case UNLE_EXPR:
2566 return COMPCODE_UNLE;
2567 case UNGT_EXPR:
2568 return COMPCODE_UNGT;
2569 case LTGT_EXPR:
2570 return COMPCODE_LTGT;
2571 case UNGE_EXPR:
2572 return COMPCODE_UNGE;
2573 default:
2574 gcc_unreachable ();
2578 /* Convert a compcode bit-based encoding of a comparison operator back
2579 to GCC's enum tree_code representation. This function is the
2580 inverse of comparison_to_compcode. */
2582 static enum tree_code
2583 compcode_to_comparison (enum comparison_code code)
2585 switch (code)
2587 case COMPCODE_LT:
2588 return LT_EXPR;
2589 case COMPCODE_EQ:
2590 return EQ_EXPR;
2591 case COMPCODE_LE:
2592 return LE_EXPR;
2593 case COMPCODE_GT:
2594 return GT_EXPR;
2595 case COMPCODE_NE:
2596 return NE_EXPR;
2597 case COMPCODE_GE:
2598 return GE_EXPR;
2599 case COMPCODE_ORD:
2600 return ORDERED_EXPR;
2601 case COMPCODE_UNORD:
2602 return UNORDERED_EXPR;
2603 case COMPCODE_UNLT:
2604 return UNLT_EXPR;
2605 case COMPCODE_UNEQ:
2606 return UNEQ_EXPR;
2607 case COMPCODE_UNLE:
2608 return UNLE_EXPR;
2609 case COMPCODE_UNGT:
2610 return UNGT_EXPR;
2611 case COMPCODE_LTGT:
2612 return LTGT_EXPR;
2613 case COMPCODE_UNGE:
2614 return UNGE_EXPR;
2615 default:
2616 gcc_unreachable ();
2620 /* Return a tree for the comparison which is the combination of
2621 doing the AND or OR (depending on CODE) of the two operations LCODE
2622 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2623 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2624 if this makes the transformation invalid. */
2626 tree
2627 combine_comparisons (location_t loc,
2628 enum tree_code code, enum tree_code lcode,
2629 enum tree_code rcode, tree truth_type,
2630 tree ll_arg, tree lr_arg)
2632 bool honor_nans = HONOR_NANS (ll_arg);
2633 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2634 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2635 int compcode;
2637 switch (code)
2639 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2640 compcode = lcompcode & rcompcode;
2641 break;
2643 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2644 compcode = lcompcode | rcompcode;
2645 break;
2647 default:
2648 return NULL_TREE;
2651 if (!honor_nans)
2653 /* Eliminate unordered comparisons, as well as LTGT and ORD
2654 which are not used unless the mode has NaNs. */
2655 compcode &= ~COMPCODE_UNORD;
2656 if (compcode == COMPCODE_LTGT)
2657 compcode = COMPCODE_NE;
2658 else if (compcode == COMPCODE_ORD)
2659 compcode = COMPCODE_TRUE;
2661 else if (flag_trapping_math)
2663 /* Check that the original operation and the optimized ones will trap
2664 under the same condition. */
2665 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2666 && (lcompcode != COMPCODE_EQ)
2667 && (lcompcode != COMPCODE_ORD);
2668 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2669 && (rcompcode != COMPCODE_EQ)
2670 && (rcompcode != COMPCODE_ORD);
2671 bool trap = (compcode & COMPCODE_UNORD) == 0
2672 && (compcode != COMPCODE_EQ)
2673 && (compcode != COMPCODE_ORD);
2675 /* In a short-circuited boolean expression the LHS might be
2676 such that the RHS, if evaluated, will never trap. For
2677 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2678 if neither x nor y is NaN. (This is a mixed blessing: for
2679 example, the expression above will never trap, hence
2680 optimizing it to x < y would be invalid). */
2681 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2682 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2683 rtrap = false;
2685 /* If the comparison was short-circuited, and only the RHS
2686 trapped, we may now generate a spurious trap. */
2687 if (rtrap && !ltrap
2688 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2689 return NULL_TREE;
2691 /* If we changed the conditions that cause a trap, we lose. */
2692 if ((ltrap || rtrap) != trap)
2693 return NULL_TREE;
2696 if (compcode == COMPCODE_TRUE)
2697 return constant_boolean_node (true, truth_type);
2698 else if (compcode == COMPCODE_FALSE)
2699 return constant_boolean_node (false, truth_type);
2700 else
2702 enum tree_code tcode;
2704 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2705 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2709 /* Return nonzero if two operands (typically of the same tree node)
2710 are necessarily equal. FLAGS modifies behavior as follows:
2712 If OEP_ONLY_CONST is set, only return nonzero for constants.
2713 This function tests whether the operands are indistinguishable;
2714 it does not test whether they are equal using C's == operation.
2715 The distinction is important for IEEE floating point, because
2716 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2717 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2719 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2720 even though it may hold multiple values during a function.
2721 This is because a GCC tree node guarantees that nothing else is
2722 executed between the evaluation of its "operands" (which may often
2723 be evaluated in arbitrary order). Hence if the operands themselves
2724 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2725 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2726 unset means assuming isochronic (or instantaneous) tree equivalence.
2727 Unless comparing arbitrary expression trees, such as from different
2728 statements, this flag can usually be left unset.
2730 If OEP_PURE_SAME is set, then pure functions with identical arguments
2731 are considered the same. It is used when the caller has other ways
2732 to ensure that global memory is unchanged in between.
2734 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2735 not values of expressions.
2737 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2738 any operand with side effect. This is unnecesarily conservative in the
2739 case we know that arg0 and arg1 are in disjoint code paths (such as in
2740 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2741 addresses with TREE_CONSTANT flag set so we know that &var == &var
2742 even if var is volatile. */
2745 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2747 /* When checking, verify at the outermost operand_equal_p call that
2748 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2749 hash value. */
2750 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2752 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2754 if (arg0 != arg1)
2756 inchash::hash hstate0 (0), hstate1 (0);
2757 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2758 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2759 hashval_t h0 = hstate0.end ();
2760 hashval_t h1 = hstate1.end ();
2761 gcc_assert (h0 == h1);
2763 return 1;
2765 else
2766 return 0;
2769 /* If either is ERROR_MARK, they aren't equal. */
2770 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2771 || TREE_TYPE (arg0) == error_mark_node
2772 || TREE_TYPE (arg1) == error_mark_node)
2773 return 0;
2775 /* Similar, if either does not have a type (like a released SSA name),
2776 they aren't equal. */
2777 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2778 return 0;
2780 /* We cannot consider pointers to different address space equal. */
2781 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2782 && POINTER_TYPE_P (TREE_TYPE (arg1))
2783 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2784 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2785 return 0;
2787 /* Check equality of integer constants before bailing out due to
2788 precision differences. */
2789 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2791 /* Address of INTEGER_CST is not defined; check that we did not forget
2792 to drop the OEP_ADDRESS_OF flags. */
2793 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2794 return tree_int_cst_equal (arg0, arg1);
2797 if (!(flags & OEP_ADDRESS_OF))
2799 /* If both types don't have the same signedness, then we can't consider
2800 them equal. We must check this before the STRIP_NOPS calls
2801 because they may change the signedness of the arguments. As pointers
2802 strictly don't have a signedness, require either two pointers or
2803 two non-pointers as well. */
2804 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2805 || POINTER_TYPE_P (TREE_TYPE (arg0))
2806 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2807 return 0;
2809 /* If both types don't have the same precision, then it is not safe
2810 to strip NOPs. */
2811 if (element_precision (TREE_TYPE (arg0))
2812 != element_precision (TREE_TYPE (arg1)))
2813 return 0;
2815 STRIP_NOPS (arg0);
2816 STRIP_NOPS (arg1);
2818 #if 0
2819 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2820 sanity check once the issue is solved. */
2821 else
2822 /* Addresses of conversions and SSA_NAMEs (and many other things)
2823 are not defined. Check that we did not forget to drop the
2824 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2825 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2826 && TREE_CODE (arg0) != SSA_NAME);
2827 #endif
2829 /* In case both args are comparisons but with different comparison
2830 code, try to swap the comparison operands of one arg to produce
2831 a match and compare that variant. */
2832 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2833 && COMPARISON_CLASS_P (arg0)
2834 && COMPARISON_CLASS_P (arg1))
2836 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2838 if (TREE_CODE (arg0) == swap_code)
2839 return operand_equal_p (TREE_OPERAND (arg0, 0),
2840 TREE_OPERAND (arg1, 1), flags)
2841 && operand_equal_p (TREE_OPERAND (arg0, 1),
2842 TREE_OPERAND (arg1, 0), flags);
2845 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2847 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2848 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2850 else if (flags & OEP_ADDRESS_OF)
2852 /* If we are interested in comparing addresses ignore
2853 MEM_REF wrappings of the base that can appear just for
2854 TBAA reasons. */
2855 if (TREE_CODE (arg0) == MEM_REF
2856 && DECL_P (arg1)
2857 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2858 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2859 && integer_zerop (TREE_OPERAND (arg0, 1)))
2860 return 1;
2861 else if (TREE_CODE (arg1) == MEM_REF
2862 && DECL_P (arg0)
2863 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2864 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2865 && integer_zerop (TREE_OPERAND (arg1, 1)))
2866 return 1;
2867 return 0;
2869 else
2870 return 0;
2873 /* When not checking adddresses, this is needed for conversions and for
2874 COMPONENT_REF. Might as well play it safe and always test this. */
2875 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2876 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2877 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2878 && !(flags & OEP_ADDRESS_OF)))
2879 return 0;
2881 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2882 We don't care about side effects in that case because the SAVE_EXPR
2883 takes care of that for us. In all other cases, two expressions are
2884 equal if they have no side effects. If we have two identical
2885 expressions with side effects that should be treated the same due
2886 to the only side effects being identical SAVE_EXPR's, that will
2887 be detected in the recursive calls below.
2888 If we are taking an invariant address of two identical objects
2889 they are necessarily equal as well. */
2890 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2891 && (TREE_CODE (arg0) == SAVE_EXPR
2892 || (flags & OEP_MATCH_SIDE_EFFECTS)
2893 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2894 return 1;
2896 /* Next handle constant cases, those for which we can return 1 even
2897 if ONLY_CONST is set. */
2898 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2899 switch (TREE_CODE (arg0))
2901 case INTEGER_CST:
2902 return tree_int_cst_equal (arg0, arg1);
2904 case FIXED_CST:
2905 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2906 TREE_FIXED_CST (arg1));
2908 case REAL_CST:
2909 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2910 return 1;
2913 if (!HONOR_SIGNED_ZEROS (arg0))
2915 /* If we do not distinguish between signed and unsigned zero,
2916 consider them equal. */
2917 if (real_zerop (arg0) && real_zerop (arg1))
2918 return 1;
2920 return 0;
2922 case VECTOR_CST:
2924 unsigned i;
2926 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2927 return 0;
2929 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2931 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2932 VECTOR_CST_ELT (arg1, i), flags))
2933 return 0;
2935 return 1;
2938 case COMPLEX_CST:
2939 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2940 flags)
2941 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2942 flags));
2944 case STRING_CST:
2945 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2946 && ! memcmp (TREE_STRING_POINTER (arg0),
2947 TREE_STRING_POINTER (arg1),
2948 TREE_STRING_LENGTH (arg0)));
2950 case ADDR_EXPR:
2951 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2952 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2953 flags | OEP_ADDRESS_OF
2954 | OEP_MATCH_SIDE_EFFECTS);
2955 case CONSTRUCTOR:
2956 /* In GIMPLE empty constructors are allowed in initializers of
2957 aggregates. */
2958 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0))
2959 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1)));
2960 default:
2961 break;
2964 if (flags & OEP_ONLY_CONST)
2965 return 0;
2967 /* Define macros to test an operand from arg0 and arg1 for equality and a
2968 variant that allows null and views null as being different from any
2969 non-null value. In the latter case, if either is null, the both
2970 must be; otherwise, do the normal comparison. */
2971 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2972 TREE_OPERAND (arg1, N), flags)
2974 #define OP_SAME_WITH_NULL(N) \
2975 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2976 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2978 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2980 case tcc_unary:
2981 /* Two conversions are equal only if signedness and modes match. */
2982 switch (TREE_CODE (arg0))
2984 CASE_CONVERT:
2985 case FIX_TRUNC_EXPR:
2986 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2987 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2988 return 0;
2989 break;
2990 default:
2991 break;
2994 return OP_SAME (0);
2997 case tcc_comparison:
2998 case tcc_binary:
2999 if (OP_SAME (0) && OP_SAME (1))
3000 return 1;
3002 /* For commutative ops, allow the other order. */
3003 return (commutative_tree_code (TREE_CODE (arg0))
3004 && operand_equal_p (TREE_OPERAND (arg0, 0),
3005 TREE_OPERAND (arg1, 1), flags)
3006 && operand_equal_p (TREE_OPERAND (arg0, 1),
3007 TREE_OPERAND (arg1, 0), flags));
3009 case tcc_reference:
3010 /* If either of the pointer (or reference) expressions we are
3011 dereferencing contain a side effect, these cannot be equal,
3012 but their addresses can be. */
3013 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3014 && (TREE_SIDE_EFFECTS (arg0)
3015 || TREE_SIDE_EFFECTS (arg1)))
3016 return 0;
3018 switch (TREE_CODE (arg0))
3020 case INDIRECT_REF:
3021 if (!(flags & OEP_ADDRESS_OF)
3022 && (TYPE_ALIGN (TREE_TYPE (arg0))
3023 != TYPE_ALIGN (TREE_TYPE (arg1))))
3024 return 0;
3025 flags &= ~OEP_ADDRESS_OF;
3026 return OP_SAME (0);
3028 case IMAGPART_EXPR:
3029 /* Require the same offset. */
3030 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3031 TYPE_SIZE (TREE_TYPE (arg1)),
3032 flags & ~OEP_ADDRESS_OF))
3033 return 0;
3035 /* Fallthru. */
3036 case REALPART_EXPR:
3037 case VIEW_CONVERT_EXPR:
3038 return OP_SAME (0);
3040 case TARGET_MEM_REF:
3041 case MEM_REF:
3042 if (!(flags & OEP_ADDRESS_OF))
3044 /* Require equal access sizes */
3045 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3046 && (!TYPE_SIZE (TREE_TYPE (arg0))
3047 || !TYPE_SIZE (TREE_TYPE (arg1))
3048 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3049 TYPE_SIZE (TREE_TYPE (arg1)),
3050 flags)))
3051 return 0;
3052 /* Verify that access happens in similar types. */
3053 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3054 return 0;
3055 /* Verify that accesses are TBAA compatible. */
3056 if (!alias_ptr_types_compatible_p
3057 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3058 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3059 || (MR_DEPENDENCE_CLIQUE (arg0)
3060 != MR_DEPENDENCE_CLIQUE (arg1))
3061 || (MR_DEPENDENCE_BASE (arg0)
3062 != MR_DEPENDENCE_BASE (arg1)))
3063 return 0;
3064 /* Verify that alignment is compatible. */
3065 if (TYPE_ALIGN (TREE_TYPE (arg0))
3066 != TYPE_ALIGN (TREE_TYPE (arg1)))
3067 return 0;
3069 flags &= ~OEP_ADDRESS_OF;
3070 return (OP_SAME (0) && OP_SAME (1)
3071 /* TARGET_MEM_REF require equal extra operands. */
3072 && (TREE_CODE (arg0) != TARGET_MEM_REF
3073 || (OP_SAME_WITH_NULL (2)
3074 && OP_SAME_WITH_NULL (3)
3075 && OP_SAME_WITH_NULL (4))));
3077 case ARRAY_REF:
3078 case ARRAY_RANGE_REF:
3079 if (!OP_SAME (0))
3080 return 0;
3081 flags &= ~OEP_ADDRESS_OF;
3082 /* Compare the array index by value if it is constant first as we
3083 may have different types but same value here. */
3084 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3085 TREE_OPERAND (arg1, 1))
3086 || OP_SAME (1))
3087 && OP_SAME_WITH_NULL (2)
3088 && OP_SAME_WITH_NULL (3)
3089 /* Compare low bound and element size as with OEP_ADDRESS_OF
3090 we have to account for the offset of the ref. */
3091 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3092 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3093 || (operand_equal_p (array_ref_low_bound
3094 (CONST_CAST_TREE (arg0)),
3095 array_ref_low_bound
3096 (CONST_CAST_TREE (arg1)), flags)
3097 && operand_equal_p (array_ref_element_size
3098 (CONST_CAST_TREE (arg0)),
3099 array_ref_element_size
3100 (CONST_CAST_TREE (arg1)),
3101 flags))));
3103 case COMPONENT_REF:
3104 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3105 may be NULL when we're called to compare MEM_EXPRs. */
3106 if (!OP_SAME_WITH_NULL (0)
3107 || !OP_SAME (1))
3108 return 0;
3109 flags &= ~OEP_ADDRESS_OF;
3110 return OP_SAME_WITH_NULL (2);
3112 case BIT_FIELD_REF:
3113 if (!OP_SAME (0))
3114 return 0;
3115 flags &= ~OEP_ADDRESS_OF;
3116 return OP_SAME (1) && OP_SAME (2);
3118 default:
3119 return 0;
3122 case tcc_expression:
3123 switch (TREE_CODE (arg0))
3125 case ADDR_EXPR:
3126 /* Be sure we pass right ADDRESS_OF flag. */
3127 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3128 return operand_equal_p (TREE_OPERAND (arg0, 0),
3129 TREE_OPERAND (arg1, 0),
3130 flags | OEP_ADDRESS_OF);
3132 case TRUTH_NOT_EXPR:
3133 return OP_SAME (0);
3135 case TRUTH_ANDIF_EXPR:
3136 case TRUTH_ORIF_EXPR:
3137 return OP_SAME (0) && OP_SAME (1);
3139 case FMA_EXPR:
3140 case WIDEN_MULT_PLUS_EXPR:
3141 case WIDEN_MULT_MINUS_EXPR:
3142 if (!OP_SAME (2))
3143 return 0;
3144 /* The multiplcation operands are commutative. */
3145 /* FALLTHRU */
3147 case TRUTH_AND_EXPR:
3148 case TRUTH_OR_EXPR:
3149 case TRUTH_XOR_EXPR:
3150 if (OP_SAME (0) && OP_SAME (1))
3151 return 1;
3153 /* Otherwise take into account this is a commutative operation. */
3154 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3155 TREE_OPERAND (arg1, 1), flags)
3156 && operand_equal_p (TREE_OPERAND (arg0, 1),
3157 TREE_OPERAND (arg1, 0), flags));
3159 case COND_EXPR:
3160 if (! OP_SAME (1) || ! OP_SAME (2))
3161 return 0;
3162 flags &= ~OEP_ADDRESS_OF;
3163 return OP_SAME (0);
3165 case VEC_COND_EXPR:
3166 case DOT_PROD_EXPR:
3167 case BIT_INSERT_EXPR:
3168 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3170 default:
3171 return 0;
3174 case tcc_vl_exp:
3175 switch (TREE_CODE (arg0))
3177 case CALL_EXPR:
3178 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3179 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3180 /* If not both CALL_EXPRs are either internal or normal function
3181 functions, then they are not equal. */
3182 return 0;
3183 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3185 /* If the CALL_EXPRs call different internal functions, then they
3186 are not equal. */
3187 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3188 return 0;
3190 else
3192 /* If the CALL_EXPRs call different functions, then they are not
3193 equal. */
3194 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3195 flags))
3196 return 0;
3199 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3201 unsigned int cef = call_expr_flags (arg0);
3202 if (flags & OEP_PURE_SAME)
3203 cef &= ECF_CONST | ECF_PURE;
3204 else
3205 cef &= ECF_CONST;
3206 if (!cef)
3207 return 0;
3210 /* Now see if all the arguments are the same. */
3212 const_call_expr_arg_iterator iter0, iter1;
3213 const_tree a0, a1;
3214 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3215 a1 = first_const_call_expr_arg (arg1, &iter1);
3216 a0 && a1;
3217 a0 = next_const_call_expr_arg (&iter0),
3218 a1 = next_const_call_expr_arg (&iter1))
3219 if (! operand_equal_p (a0, a1, flags))
3220 return 0;
3222 /* If we get here and both argument lists are exhausted
3223 then the CALL_EXPRs are equal. */
3224 return ! (a0 || a1);
3226 default:
3227 return 0;
3230 case tcc_declaration:
3231 /* Consider __builtin_sqrt equal to sqrt. */
3232 return (TREE_CODE (arg0) == FUNCTION_DECL
3233 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3234 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3235 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3237 case tcc_exceptional:
3238 if (TREE_CODE (arg0) == CONSTRUCTOR)
3240 /* In GIMPLE constructors are used only to build vectors from
3241 elements. Individual elements in the constructor must be
3242 indexed in increasing order and form an initial sequence.
3244 We make no effort to compare constructors in generic.
3245 (see sem_variable::equals in ipa-icf which can do so for
3246 constants). */
3247 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3248 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3249 return 0;
3251 /* Be sure that vectors constructed have the same representation.
3252 We only tested element precision and modes to match.
3253 Vectors may be BLKmode and thus also check that the number of
3254 parts match. */
3255 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3256 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3257 return 0;
3259 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3260 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3261 unsigned int len = vec_safe_length (v0);
3263 if (len != vec_safe_length (v1))
3264 return 0;
3266 for (unsigned int i = 0; i < len; i++)
3268 constructor_elt *c0 = &(*v0)[i];
3269 constructor_elt *c1 = &(*v1)[i];
3271 if (!operand_equal_p (c0->value, c1->value, flags)
3272 /* In GIMPLE the indexes can be either NULL or matching i.
3273 Double check this so we won't get false
3274 positives for GENERIC. */
3275 || (c0->index
3276 && (TREE_CODE (c0->index) != INTEGER_CST
3277 || !compare_tree_int (c0->index, i)))
3278 || (c1->index
3279 && (TREE_CODE (c1->index) != INTEGER_CST
3280 || !compare_tree_int (c1->index, i))))
3281 return 0;
3283 return 1;
3285 return 0;
3287 default:
3288 return 0;
3291 #undef OP_SAME
3292 #undef OP_SAME_WITH_NULL
3295 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3296 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3298 When in doubt, return 0. */
3300 static int
3301 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3303 int unsignedp1, unsignedpo;
3304 tree primarg0, primarg1, primother;
3305 unsigned int correct_width;
3307 if (operand_equal_p (arg0, arg1, 0))
3308 return 1;
3310 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3311 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3312 return 0;
3314 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3315 and see if the inner values are the same. This removes any
3316 signedness comparison, which doesn't matter here. */
3317 primarg0 = arg0, primarg1 = arg1;
3318 STRIP_NOPS (primarg0);
3319 STRIP_NOPS (primarg1);
3320 if (operand_equal_p (primarg0, primarg1, 0))
3321 return 1;
3323 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3324 actual comparison operand, ARG0.
3326 First throw away any conversions to wider types
3327 already present in the operands. */
3329 primarg1 = get_narrower (arg1, &unsignedp1);
3330 primother = get_narrower (other, &unsignedpo);
3332 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3333 if (unsignedp1 == unsignedpo
3334 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3335 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3337 tree type = TREE_TYPE (arg0);
3339 /* Make sure shorter operand is extended the right way
3340 to match the longer operand. */
3341 primarg1 = fold_convert (signed_or_unsigned_type_for
3342 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3344 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3345 return 1;
3348 return 0;
3351 /* See if ARG is an expression that is either a comparison or is performing
3352 arithmetic on comparisons. The comparisons must only be comparing
3353 two different values, which will be stored in *CVAL1 and *CVAL2; if
3354 they are nonzero it means that some operands have already been found.
3355 No variables may be used anywhere else in the expression except in the
3356 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3357 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3359 If this is true, return 1. Otherwise, return zero. */
3361 static int
3362 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3364 enum tree_code code = TREE_CODE (arg);
3365 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3367 /* We can handle some of the tcc_expression cases here. */
3368 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3369 tclass = tcc_unary;
3370 else if (tclass == tcc_expression
3371 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3372 || code == COMPOUND_EXPR))
3373 tclass = tcc_binary;
3375 else if (tclass == tcc_expression && code == SAVE_EXPR
3376 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3378 /* If we've already found a CVAL1 or CVAL2, this expression is
3379 two complex to handle. */
3380 if (*cval1 || *cval2)
3381 return 0;
3383 tclass = tcc_unary;
3384 *save_p = 1;
3387 switch (tclass)
3389 case tcc_unary:
3390 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3392 case tcc_binary:
3393 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3394 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3395 cval1, cval2, save_p));
3397 case tcc_constant:
3398 return 1;
3400 case tcc_expression:
3401 if (code == COND_EXPR)
3402 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3403 cval1, cval2, save_p)
3404 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3405 cval1, cval2, save_p)
3406 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3407 cval1, cval2, save_p));
3408 return 0;
3410 case tcc_comparison:
3411 /* First see if we can handle the first operand, then the second. For
3412 the second operand, we know *CVAL1 can't be zero. It must be that
3413 one side of the comparison is each of the values; test for the
3414 case where this isn't true by failing if the two operands
3415 are the same. */
3417 if (operand_equal_p (TREE_OPERAND (arg, 0),
3418 TREE_OPERAND (arg, 1), 0))
3419 return 0;
3421 if (*cval1 == 0)
3422 *cval1 = TREE_OPERAND (arg, 0);
3423 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3425 else if (*cval2 == 0)
3426 *cval2 = TREE_OPERAND (arg, 0);
3427 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3429 else
3430 return 0;
3432 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3434 else if (*cval2 == 0)
3435 *cval2 = TREE_OPERAND (arg, 1);
3436 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3438 else
3439 return 0;
3441 return 1;
3443 default:
3444 return 0;
3448 /* ARG is a tree that is known to contain just arithmetic operations and
3449 comparisons. Evaluate the operations in the tree substituting NEW0 for
3450 any occurrence of OLD0 as an operand of a comparison and likewise for
3451 NEW1 and OLD1. */
3453 static tree
3454 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3455 tree old1, tree new1)
3457 tree type = TREE_TYPE (arg);
3458 enum tree_code code = TREE_CODE (arg);
3459 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3461 /* We can handle some of the tcc_expression cases here. */
3462 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3463 tclass = tcc_unary;
3464 else if (tclass == tcc_expression
3465 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3466 tclass = tcc_binary;
3468 switch (tclass)
3470 case tcc_unary:
3471 return fold_build1_loc (loc, code, type,
3472 eval_subst (loc, TREE_OPERAND (arg, 0),
3473 old0, new0, old1, new1));
3475 case tcc_binary:
3476 return fold_build2_loc (loc, code, type,
3477 eval_subst (loc, TREE_OPERAND (arg, 0),
3478 old0, new0, old1, new1),
3479 eval_subst (loc, TREE_OPERAND (arg, 1),
3480 old0, new0, old1, new1));
3482 case tcc_expression:
3483 switch (code)
3485 case SAVE_EXPR:
3486 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3487 old1, new1);
3489 case COMPOUND_EXPR:
3490 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3491 old1, new1);
3493 case COND_EXPR:
3494 return fold_build3_loc (loc, code, type,
3495 eval_subst (loc, TREE_OPERAND (arg, 0),
3496 old0, new0, old1, new1),
3497 eval_subst (loc, TREE_OPERAND (arg, 1),
3498 old0, new0, old1, new1),
3499 eval_subst (loc, TREE_OPERAND (arg, 2),
3500 old0, new0, old1, new1));
3501 default:
3502 break;
3504 /* Fall through - ??? */
3506 case tcc_comparison:
3508 tree arg0 = TREE_OPERAND (arg, 0);
3509 tree arg1 = TREE_OPERAND (arg, 1);
3511 /* We need to check both for exact equality and tree equality. The
3512 former will be true if the operand has a side-effect. In that
3513 case, we know the operand occurred exactly once. */
3515 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3516 arg0 = new0;
3517 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3518 arg0 = new1;
3520 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3521 arg1 = new0;
3522 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3523 arg1 = new1;
3525 return fold_build2_loc (loc, code, type, arg0, arg1);
3528 default:
3529 return arg;
3533 /* Return a tree for the case when the result of an expression is RESULT
3534 converted to TYPE and OMITTED was previously an operand of the expression
3535 but is now not needed (e.g., we folded OMITTED * 0).
3537 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3538 the conversion of RESULT to TYPE. */
3540 tree
3541 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3543 tree t = fold_convert_loc (loc, type, result);
3545 /* If the resulting operand is an empty statement, just return the omitted
3546 statement casted to void. */
3547 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3548 return build1_loc (loc, NOP_EXPR, void_type_node,
3549 fold_ignored_result (omitted));
3551 if (TREE_SIDE_EFFECTS (omitted))
3552 return build2_loc (loc, COMPOUND_EXPR, type,
3553 fold_ignored_result (omitted), t);
3555 return non_lvalue_loc (loc, t);
3558 /* Return a tree for the case when the result of an expression is RESULT
3559 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3560 of the expression but are now not needed.
3562 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3563 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3564 evaluated before OMITTED2. Otherwise, if neither has side effects,
3565 just do the conversion of RESULT to TYPE. */
3567 tree
3568 omit_two_operands_loc (location_t loc, tree type, tree result,
3569 tree omitted1, tree omitted2)
3571 tree t = fold_convert_loc (loc, type, result);
3573 if (TREE_SIDE_EFFECTS (omitted2))
3574 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3575 if (TREE_SIDE_EFFECTS (omitted1))
3576 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3578 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3582 /* Return a simplified tree node for the truth-negation of ARG. This
3583 never alters ARG itself. We assume that ARG is an operation that
3584 returns a truth value (0 or 1).
3586 FIXME: one would think we would fold the result, but it causes
3587 problems with the dominator optimizer. */
3589 static tree
3590 fold_truth_not_expr (location_t loc, tree arg)
3592 tree type = TREE_TYPE (arg);
3593 enum tree_code code = TREE_CODE (arg);
3594 location_t loc1, loc2;
3596 /* If this is a comparison, we can simply invert it, except for
3597 floating-point non-equality comparisons, in which case we just
3598 enclose a TRUTH_NOT_EXPR around what we have. */
3600 if (TREE_CODE_CLASS (code) == tcc_comparison)
3602 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3603 if (FLOAT_TYPE_P (op_type)
3604 && flag_trapping_math
3605 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3606 && code != NE_EXPR && code != EQ_EXPR)
3607 return NULL_TREE;
3609 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3610 if (code == ERROR_MARK)
3611 return NULL_TREE;
3613 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3614 TREE_OPERAND (arg, 1));
3615 if (TREE_NO_WARNING (arg))
3616 TREE_NO_WARNING (ret) = 1;
3617 return ret;
3620 switch (code)
3622 case INTEGER_CST:
3623 return constant_boolean_node (integer_zerop (arg), type);
3625 case TRUTH_AND_EXPR:
3626 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3627 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3628 return build2_loc (loc, TRUTH_OR_EXPR, type,
3629 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3630 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3632 case TRUTH_OR_EXPR:
3633 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3634 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3635 return build2_loc (loc, TRUTH_AND_EXPR, type,
3636 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3637 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3639 case TRUTH_XOR_EXPR:
3640 /* Here we can invert either operand. We invert the first operand
3641 unless the second operand is a TRUTH_NOT_EXPR in which case our
3642 result is the XOR of the first operand with the inside of the
3643 negation of the second operand. */
3645 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3646 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3647 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3648 else
3649 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3650 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3651 TREE_OPERAND (arg, 1));
3653 case TRUTH_ANDIF_EXPR:
3654 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3655 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3656 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3657 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3658 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3660 case TRUTH_ORIF_EXPR:
3661 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3662 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3663 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3664 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3665 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3667 case TRUTH_NOT_EXPR:
3668 return TREE_OPERAND (arg, 0);
3670 case COND_EXPR:
3672 tree arg1 = TREE_OPERAND (arg, 1);
3673 tree arg2 = TREE_OPERAND (arg, 2);
3675 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3676 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3678 /* A COND_EXPR may have a throw as one operand, which
3679 then has void type. Just leave void operands
3680 as they are. */
3681 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3682 VOID_TYPE_P (TREE_TYPE (arg1))
3683 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3684 VOID_TYPE_P (TREE_TYPE (arg2))
3685 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3688 case COMPOUND_EXPR:
3689 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3690 return build2_loc (loc, COMPOUND_EXPR, type,
3691 TREE_OPERAND (arg, 0),
3692 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3694 case NON_LVALUE_EXPR:
3695 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3696 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3698 CASE_CONVERT:
3699 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3700 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3702 /* ... fall through ... */
3704 case FLOAT_EXPR:
3705 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3706 return build1_loc (loc, TREE_CODE (arg), type,
3707 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3709 case BIT_AND_EXPR:
3710 if (!integer_onep (TREE_OPERAND (arg, 1)))
3711 return NULL_TREE;
3712 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3714 case SAVE_EXPR:
3715 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3717 case CLEANUP_POINT_EXPR:
3718 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3719 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3720 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3722 default:
3723 return NULL_TREE;
3727 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3728 assume that ARG is an operation that returns a truth value (0 or 1
3729 for scalars, 0 or -1 for vectors). Return the folded expression if
3730 folding is successful. Otherwise, return NULL_TREE. */
3732 static tree
3733 fold_invert_truthvalue (location_t loc, tree arg)
3735 tree type = TREE_TYPE (arg);
3736 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3737 ? BIT_NOT_EXPR
3738 : TRUTH_NOT_EXPR,
3739 type, arg);
3742 /* Return a simplified tree node for the truth-negation of ARG. This
3743 never alters ARG itself. We assume that ARG is an operation that
3744 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3746 tree
3747 invert_truthvalue_loc (location_t loc, tree arg)
3749 if (TREE_CODE (arg) == ERROR_MARK)
3750 return arg;
3752 tree type = TREE_TYPE (arg);
3753 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3754 ? BIT_NOT_EXPR
3755 : TRUTH_NOT_EXPR,
3756 type, arg);
3759 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3760 with code CODE. This optimization is unsafe. */
3761 static tree
3762 distribute_real_division (location_t loc, enum tree_code code, tree type,
3763 tree arg0, tree arg1)
3765 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3766 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3768 /* (A / C) +- (B / C) -> (A +- B) / C. */
3769 if (mul0 == mul1
3770 && operand_equal_p (TREE_OPERAND (arg0, 1),
3771 TREE_OPERAND (arg1, 1), 0))
3772 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3773 fold_build2_loc (loc, code, type,
3774 TREE_OPERAND (arg0, 0),
3775 TREE_OPERAND (arg1, 0)),
3776 TREE_OPERAND (arg0, 1));
3778 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3779 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3780 TREE_OPERAND (arg1, 0), 0)
3781 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3782 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3784 REAL_VALUE_TYPE r0, r1;
3785 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3786 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3787 if (!mul0)
3788 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3789 if (!mul1)
3790 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3791 real_arithmetic (&r0, code, &r0, &r1);
3792 return fold_build2_loc (loc, MULT_EXPR, type,
3793 TREE_OPERAND (arg0, 0),
3794 build_real (type, r0));
3797 return NULL_TREE;
3800 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3801 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3802 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3803 is the original memory reference used to preserve the alias set of
3804 the access. */
3806 static tree
3807 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3808 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3809 int unsignedp, int reversep)
3811 tree result, bftype;
3813 if (get_alias_set (inner) != get_alias_set (orig_inner))
3814 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3815 build_fold_addr_expr (inner),
3816 build_int_cst
3817 (reference_alias_ptr_type (orig_inner), 0));
3819 if (bitpos == 0 && !reversep)
3821 tree size = TYPE_SIZE (TREE_TYPE (inner));
3822 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3823 || POINTER_TYPE_P (TREE_TYPE (inner)))
3824 && tree_fits_shwi_p (size)
3825 && tree_to_shwi (size) == bitsize)
3826 return fold_convert_loc (loc, type, inner);
3829 bftype = type;
3830 if (TYPE_PRECISION (bftype) != bitsize
3831 || TYPE_UNSIGNED (bftype) == !unsignedp)
3832 bftype = build_nonstandard_integer_type (bitsize, 0);
3834 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3835 size_int (bitsize), bitsize_int (bitpos));
3836 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3838 if (bftype != type)
3839 result = fold_convert_loc (loc, type, result);
3841 return result;
3844 /* Optimize a bit-field compare.
3846 There are two cases: First is a compare against a constant and the
3847 second is a comparison of two items where the fields are at the same
3848 bit position relative to the start of a chunk (byte, halfword, word)
3849 large enough to contain it. In these cases we can avoid the shift
3850 implicit in bitfield extractions.
3852 For constants, we emit a compare of the shifted constant with the
3853 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3854 compared. For two fields at the same position, we do the ANDs with the
3855 similar mask and compare the result of the ANDs.
3857 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3858 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3859 are the left and right operands of the comparison, respectively.
3861 If the optimization described above can be done, we return the resulting
3862 tree. Otherwise we return zero. */
3864 static tree
3865 optimize_bit_field_compare (location_t loc, enum tree_code code,
3866 tree compare_type, tree lhs, tree rhs)
3868 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3869 tree type = TREE_TYPE (lhs);
3870 tree unsigned_type;
3871 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3872 machine_mode lmode, rmode, nmode;
3873 int lunsignedp, runsignedp;
3874 int lreversep, rreversep;
3875 int lvolatilep = 0, rvolatilep = 0;
3876 tree linner, rinner = NULL_TREE;
3877 tree mask;
3878 tree offset;
3880 /* Get all the information about the extractions being done. If the bit size
3881 if the same as the size of the underlying object, we aren't doing an
3882 extraction at all and so can do nothing. We also don't want to
3883 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3884 then will no longer be able to replace it. */
3885 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3886 &lunsignedp, &lreversep, &lvolatilep, false);
3887 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3888 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3889 return 0;
3891 if (const_p)
3892 rreversep = lreversep;
3893 else
3895 /* If this is not a constant, we can only do something if bit positions,
3896 sizes, signedness and storage order are the same. */
3897 rinner
3898 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3899 &runsignedp, &rreversep, &rvolatilep, false);
3901 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3902 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3903 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3904 return 0;
3907 /* See if we can find a mode to refer to this field. We should be able to,
3908 but fail if we can't. */
3909 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3910 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3911 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3912 TYPE_ALIGN (TREE_TYPE (rinner))),
3913 word_mode, false);
3914 if (nmode == VOIDmode)
3915 return 0;
3917 /* Set signed and unsigned types of the precision of this mode for the
3918 shifts below. */
3919 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3921 /* Compute the bit position and size for the new reference and our offset
3922 within it. If the new reference is the same size as the original, we
3923 won't optimize anything, so return zero. */
3924 nbitsize = GET_MODE_BITSIZE (nmode);
3925 nbitpos = lbitpos & ~ (nbitsize - 1);
3926 lbitpos -= nbitpos;
3927 if (nbitsize == lbitsize)
3928 return 0;
3930 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3931 lbitpos = nbitsize - lbitsize - lbitpos;
3933 /* Make the mask to be used against the extracted field. */
3934 mask = build_int_cst_type (unsigned_type, -1);
3935 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3936 mask = const_binop (RSHIFT_EXPR, mask,
3937 size_int (nbitsize - lbitsize - lbitpos));
3939 if (! const_p)
3940 /* If not comparing with constant, just rework the comparison
3941 and return. */
3942 return fold_build2_loc (loc, code, compare_type,
3943 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3944 make_bit_field_ref (loc, linner, lhs,
3945 unsigned_type,
3946 nbitsize, nbitpos,
3947 1, lreversep),
3948 mask),
3949 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3950 make_bit_field_ref (loc, rinner, rhs,
3951 unsigned_type,
3952 nbitsize, nbitpos,
3953 1, rreversep),
3954 mask));
3956 /* Otherwise, we are handling the constant case. See if the constant is too
3957 big for the field. Warn and return a tree for 0 (false) if so. We do
3958 this not only for its own sake, but to avoid having to test for this
3959 error case below. If we didn't, we might generate wrong code.
3961 For unsigned fields, the constant shifted right by the field length should
3962 be all zero. For signed fields, the high-order bits should agree with
3963 the sign bit. */
3965 if (lunsignedp)
3967 if (wi::lrshift (rhs, lbitsize) != 0)
3969 warning (0, "comparison is always %d due to width of bit-field",
3970 code == NE_EXPR);
3971 return constant_boolean_node (code == NE_EXPR, compare_type);
3974 else
3976 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3977 if (tem != 0 && tem != -1)
3979 warning (0, "comparison is always %d due to width of bit-field",
3980 code == NE_EXPR);
3981 return constant_boolean_node (code == NE_EXPR, compare_type);
3985 /* Single-bit compares should always be against zero. */
3986 if (lbitsize == 1 && ! integer_zerop (rhs))
3988 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3989 rhs = build_int_cst (type, 0);
3992 /* Make a new bitfield reference, shift the constant over the
3993 appropriate number of bits and mask it with the computed mask
3994 (in case this was a signed field). If we changed it, make a new one. */
3995 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
3996 nbitsize, nbitpos, 1, lreversep);
3998 rhs = const_binop (BIT_AND_EXPR,
3999 const_binop (LSHIFT_EXPR,
4000 fold_convert_loc (loc, unsigned_type, rhs),
4001 size_int (lbitpos)),
4002 mask);
4004 lhs = build2_loc (loc, code, compare_type,
4005 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4006 return lhs;
4009 /* Subroutine for fold_truth_andor_1: decode a field reference.
4011 If EXP is a comparison reference, we return the innermost reference.
4013 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4014 set to the starting bit number.
4016 If the innermost field can be completely contained in a mode-sized
4017 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4019 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4020 otherwise it is not changed.
4022 *PUNSIGNEDP is set to the signedness of the field.
4024 *PREVERSEP is set to the storage order of the field.
4026 *PMASK is set to the mask used. This is either contained in a
4027 BIT_AND_EXPR or derived from the width of the field.
4029 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4031 Return 0 if this is not a component reference or is one that we can't
4032 do anything with. */
4034 static tree
4035 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4036 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4037 int *punsignedp, int *preversep, int *pvolatilep,
4038 tree *pmask, tree *pand_mask)
4040 tree exp = *exp_;
4041 tree outer_type = 0;
4042 tree and_mask = 0;
4043 tree mask, inner, offset;
4044 tree unsigned_type;
4045 unsigned int precision;
4047 /* All the optimizations using this function assume integer fields.
4048 There are problems with FP fields since the type_for_size call
4049 below can fail for, e.g., XFmode. */
4050 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4051 return 0;
4053 /* We are interested in the bare arrangement of bits, so strip everything
4054 that doesn't affect the machine mode. However, record the type of the
4055 outermost expression if it may matter below. */
4056 if (CONVERT_EXPR_P (exp)
4057 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4058 outer_type = TREE_TYPE (exp);
4059 STRIP_NOPS (exp);
4061 if (TREE_CODE (exp) == BIT_AND_EXPR)
4063 and_mask = TREE_OPERAND (exp, 1);
4064 exp = TREE_OPERAND (exp, 0);
4065 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4066 if (TREE_CODE (and_mask) != INTEGER_CST)
4067 return 0;
4070 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4071 punsignedp, preversep, pvolatilep, false);
4072 if ((inner == exp && and_mask == 0)
4073 || *pbitsize < 0 || offset != 0
4074 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4075 return 0;
4077 *exp_ = exp;
4079 /* If the number of bits in the reference is the same as the bitsize of
4080 the outer type, then the outer type gives the signedness. Otherwise
4081 (in case of a small bitfield) the signedness is unchanged. */
4082 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4083 *punsignedp = TYPE_UNSIGNED (outer_type);
4085 /* Compute the mask to access the bitfield. */
4086 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4087 precision = TYPE_PRECISION (unsigned_type);
4089 mask = build_int_cst_type (unsigned_type, -1);
4091 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4092 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4094 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4095 if (and_mask != 0)
4096 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4097 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4099 *pmask = mask;
4100 *pand_mask = and_mask;
4101 return inner;
4104 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4105 bit positions and MASK is SIGNED. */
4107 static int
4108 all_ones_mask_p (const_tree mask, unsigned int size)
4110 tree type = TREE_TYPE (mask);
4111 unsigned int precision = TYPE_PRECISION (type);
4113 /* If this function returns true when the type of the mask is
4114 UNSIGNED, then there will be errors. In particular see
4115 gcc.c-torture/execute/990326-1.c. There does not appear to be
4116 any documentation paper trail as to why this is so. But the pre
4117 wide-int worked with that restriction and it has been preserved
4118 here. */
4119 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4120 return false;
4122 return wi::mask (size, false, precision) == mask;
4125 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4126 represents the sign bit of EXP's type. If EXP represents a sign
4127 or zero extension, also test VAL against the unextended type.
4128 The return value is the (sub)expression whose sign bit is VAL,
4129 or NULL_TREE otherwise. */
4131 tree
4132 sign_bit_p (tree exp, const_tree val)
4134 int width;
4135 tree t;
4137 /* Tree EXP must have an integral type. */
4138 t = TREE_TYPE (exp);
4139 if (! INTEGRAL_TYPE_P (t))
4140 return NULL_TREE;
4142 /* Tree VAL must be an integer constant. */
4143 if (TREE_CODE (val) != INTEGER_CST
4144 || TREE_OVERFLOW (val))
4145 return NULL_TREE;
4147 width = TYPE_PRECISION (t);
4148 if (wi::only_sign_bit_p (val, width))
4149 return exp;
4151 /* Handle extension from a narrower type. */
4152 if (TREE_CODE (exp) == NOP_EXPR
4153 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4154 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4156 return NULL_TREE;
4159 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4160 to be evaluated unconditionally. */
4162 static int
4163 simple_operand_p (const_tree exp)
4165 /* Strip any conversions that don't change the machine mode. */
4166 STRIP_NOPS (exp);
4168 return (CONSTANT_CLASS_P (exp)
4169 || TREE_CODE (exp) == SSA_NAME
4170 || (DECL_P (exp)
4171 && ! TREE_ADDRESSABLE (exp)
4172 && ! TREE_THIS_VOLATILE (exp)
4173 && ! DECL_NONLOCAL (exp)
4174 /* Don't regard global variables as simple. They may be
4175 allocated in ways unknown to the compiler (shared memory,
4176 #pragma weak, etc). */
4177 && ! TREE_PUBLIC (exp)
4178 && ! DECL_EXTERNAL (exp)
4179 /* Weakrefs are not safe to be read, since they can be NULL.
4180 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4181 have DECL_WEAK flag set. */
4182 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4183 /* Loading a static variable is unduly expensive, but global
4184 registers aren't expensive. */
4185 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4188 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4189 to be evaluated unconditionally.
4190 I addition to simple_operand_p, we assume that comparisons, conversions,
4191 and logic-not operations are simple, if their operands are simple, too. */
4193 static bool
4194 simple_operand_p_2 (tree exp)
4196 enum tree_code code;
4198 if (TREE_SIDE_EFFECTS (exp)
4199 || tree_could_trap_p (exp))
4200 return false;
4202 while (CONVERT_EXPR_P (exp))
4203 exp = TREE_OPERAND (exp, 0);
4205 code = TREE_CODE (exp);
4207 if (TREE_CODE_CLASS (code) == tcc_comparison)
4208 return (simple_operand_p (TREE_OPERAND (exp, 0))
4209 && simple_operand_p (TREE_OPERAND (exp, 1)));
4211 if (code == TRUTH_NOT_EXPR)
4212 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4214 return simple_operand_p (exp);
4218 /* The following functions are subroutines to fold_range_test and allow it to
4219 try to change a logical combination of comparisons into a range test.
4221 For example, both
4222 X == 2 || X == 3 || X == 4 || X == 5
4224 X >= 2 && X <= 5
4225 are converted to
4226 (unsigned) (X - 2) <= 3
4228 We describe each set of comparisons as being either inside or outside
4229 a range, using a variable named like IN_P, and then describe the
4230 range with a lower and upper bound. If one of the bounds is omitted,
4231 it represents either the highest or lowest value of the type.
4233 In the comments below, we represent a range by two numbers in brackets
4234 preceded by a "+" to designate being inside that range, or a "-" to
4235 designate being outside that range, so the condition can be inverted by
4236 flipping the prefix. An omitted bound is represented by a "-". For
4237 example, "- [-, 10]" means being outside the range starting at the lowest
4238 possible value and ending at 10, in other words, being greater than 10.
4239 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4240 always false.
4242 We set up things so that the missing bounds are handled in a consistent
4243 manner so neither a missing bound nor "true" and "false" need to be
4244 handled using a special case. */
4246 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4247 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4248 and UPPER1_P are nonzero if the respective argument is an upper bound
4249 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4250 must be specified for a comparison. ARG1 will be converted to ARG0's
4251 type if both are specified. */
4253 static tree
4254 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4255 tree arg1, int upper1_p)
4257 tree tem;
4258 int result;
4259 int sgn0, sgn1;
4261 /* If neither arg represents infinity, do the normal operation.
4262 Else, if not a comparison, return infinity. Else handle the special
4263 comparison rules. Note that most of the cases below won't occur, but
4264 are handled for consistency. */
4266 if (arg0 != 0 && arg1 != 0)
4268 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4269 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4270 STRIP_NOPS (tem);
4271 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4274 if (TREE_CODE_CLASS (code) != tcc_comparison)
4275 return 0;
4277 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4278 for neither. In real maths, we cannot assume open ended ranges are
4279 the same. But, this is computer arithmetic, where numbers are finite.
4280 We can therefore make the transformation of any unbounded range with
4281 the value Z, Z being greater than any representable number. This permits
4282 us to treat unbounded ranges as equal. */
4283 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4284 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4285 switch (code)
4287 case EQ_EXPR:
4288 result = sgn0 == sgn1;
4289 break;
4290 case NE_EXPR:
4291 result = sgn0 != sgn1;
4292 break;
4293 case LT_EXPR:
4294 result = sgn0 < sgn1;
4295 break;
4296 case LE_EXPR:
4297 result = sgn0 <= sgn1;
4298 break;
4299 case GT_EXPR:
4300 result = sgn0 > sgn1;
4301 break;
4302 case GE_EXPR:
4303 result = sgn0 >= sgn1;
4304 break;
4305 default:
4306 gcc_unreachable ();
4309 return constant_boolean_node (result, type);
4312 /* Helper routine for make_range. Perform one step for it, return
4313 new expression if the loop should continue or NULL_TREE if it should
4314 stop. */
4316 tree
4317 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4318 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4319 bool *strict_overflow_p)
4321 tree arg0_type = TREE_TYPE (arg0);
4322 tree n_low, n_high, low = *p_low, high = *p_high;
4323 int in_p = *p_in_p, n_in_p;
4325 switch (code)
4327 case TRUTH_NOT_EXPR:
4328 /* We can only do something if the range is testing for zero. */
4329 if (low == NULL_TREE || high == NULL_TREE
4330 || ! integer_zerop (low) || ! integer_zerop (high))
4331 return NULL_TREE;
4332 *p_in_p = ! in_p;
4333 return arg0;
4335 case EQ_EXPR: case NE_EXPR:
4336 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4337 /* We can only do something if the range is testing for zero
4338 and if the second operand is an integer constant. Note that
4339 saying something is "in" the range we make is done by
4340 complementing IN_P since it will set in the initial case of
4341 being not equal to zero; "out" is leaving it alone. */
4342 if (low == NULL_TREE || high == NULL_TREE
4343 || ! integer_zerop (low) || ! integer_zerop (high)
4344 || TREE_CODE (arg1) != INTEGER_CST)
4345 return NULL_TREE;
4347 switch (code)
4349 case NE_EXPR: /* - [c, c] */
4350 low = high = arg1;
4351 break;
4352 case EQ_EXPR: /* + [c, c] */
4353 in_p = ! in_p, low = high = arg1;
4354 break;
4355 case GT_EXPR: /* - [-, c] */
4356 low = 0, high = arg1;
4357 break;
4358 case GE_EXPR: /* + [c, -] */
4359 in_p = ! in_p, low = arg1, high = 0;
4360 break;
4361 case LT_EXPR: /* - [c, -] */
4362 low = arg1, high = 0;
4363 break;
4364 case LE_EXPR: /* + [-, c] */
4365 in_p = ! in_p, low = 0, high = arg1;
4366 break;
4367 default:
4368 gcc_unreachable ();
4371 /* If this is an unsigned comparison, we also know that EXP is
4372 greater than or equal to zero. We base the range tests we make
4373 on that fact, so we record it here so we can parse existing
4374 range tests. We test arg0_type since often the return type
4375 of, e.g. EQ_EXPR, is boolean. */
4376 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4378 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4379 in_p, low, high, 1,
4380 build_int_cst (arg0_type, 0),
4381 NULL_TREE))
4382 return NULL_TREE;
4384 in_p = n_in_p, low = n_low, high = n_high;
4386 /* If the high bound is missing, but we have a nonzero low
4387 bound, reverse the range so it goes from zero to the low bound
4388 minus 1. */
4389 if (high == 0 && low && ! integer_zerop (low))
4391 in_p = ! in_p;
4392 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4393 build_int_cst (TREE_TYPE (low), 1), 0);
4394 low = build_int_cst (arg0_type, 0);
4398 *p_low = low;
4399 *p_high = high;
4400 *p_in_p = in_p;
4401 return arg0;
4403 case NEGATE_EXPR:
4404 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4405 low and high are non-NULL, then normalize will DTRT. */
4406 if (!TYPE_UNSIGNED (arg0_type)
4407 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4409 if (low == NULL_TREE)
4410 low = TYPE_MIN_VALUE (arg0_type);
4411 if (high == NULL_TREE)
4412 high = TYPE_MAX_VALUE (arg0_type);
4415 /* (-x) IN [a,b] -> x in [-b, -a] */
4416 n_low = range_binop (MINUS_EXPR, exp_type,
4417 build_int_cst (exp_type, 0),
4418 0, high, 1);
4419 n_high = range_binop (MINUS_EXPR, exp_type,
4420 build_int_cst (exp_type, 0),
4421 0, low, 0);
4422 if (n_high != 0 && TREE_OVERFLOW (n_high))
4423 return NULL_TREE;
4424 goto normalize;
4426 case BIT_NOT_EXPR:
4427 /* ~ X -> -X - 1 */
4428 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4429 build_int_cst (exp_type, 1));
4431 case PLUS_EXPR:
4432 case MINUS_EXPR:
4433 if (TREE_CODE (arg1) != INTEGER_CST)
4434 return NULL_TREE;
4436 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4437 move a constant to the other side. */
4438 if (!TYPE_UNSIGNED (arg0_type)
4439 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4440 return NULL_TREE;
4442 /* If EXP is signed, any overflow in the computation is undefined,
4443 so we don't worry about it so long as our computations on
4444 the bounds don't overflow. For unsigned, overflow is defined
4445 and this is exactly the right thing. */
4446 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4447 arg0_type, low, 0, arg1, 0);
4448 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4449 arg0_type, high, 1, arg1, 0);
4450 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4451 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4452 return NULL_TREE;
4454 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4455 *strict_overflow_p = true;
4457 normalize:
4458 /* Check for an unsigned range which has wrapped around the maximum
4459 value thus making n_high < n_low, and normalize it. */
4460 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4462 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4463 build_int_cst (TREE_TYPE (n_high), 1), 0);
4464 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4465 build_int_cst (TREE_TYPE (n_low), 1), 0);
4467 /* If the range is of the form +/- [ x+1, x ], we won't
4468 be able to normalize it. But then, it represents the
4469 whole range or the empty set, so make it
4470 +/- [ -, - ]. */
4471 if (tree_int_cst_equal (n_low, low)
4472 && tree_int_cst_equal (n_high, high))
4473 low = high = 0;
4474 else
4475 in_p = ! in_p;
4477 else
4478 low = n_low, high = n_high;
4480 *p_low = low;
4481 *p_high = high;
4482 *p_in_p = in_p;
4483 return arg0;
4485 CASE_CONVERT:
4486 case NON_LVALUE_EXPR:
4487 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4488 return NULL_TREE;
4490 if (! INTEGRAL_TYPE_P (arg0_type)
4491 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4492 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4493 return NULL_TREE;
4495 n_low = low, n_high = high;
4497 if (n_low != 0)
4498 n_low = fold_convert_loc (loc, arg0_type, n_low);
4500 if (n_high != 0)
4501 n_high = fold_convert_loc (loc, arg0_type, n_high);
4503 /* If we're converting arg0 from an unsigned type, to exp,
4504 a signed type, we will be doing the comparison as unsigned.
4505 The tests above have already verified that LOW and HIGH
4506 are both positive.
4508 So we have to ensure that we will handle large unsigned
4509 values the same way that the current signed bounds treat
4510 negative values. */
4512 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4514 tree high_positive;
4515 tree equiv_type;
4516 /* For fixed-point modes, we need to pass the saturating flag
4517 as the 2nd parameter. */
4518 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4519 equiv_type
4520 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4521 TYPE_SATURATING (arg0_type));
4522 else
4523 equiv_type
4524 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4526 /* A range without an upper bound is, naturally, unbounded.
4527 Since convert would have cropped a very large value, use
4528 the max value for the destination type. */
4529 high_positive
4530 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4531 : TYPE_MAX_VALUE (arg0_type);
4533 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4534 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4535 fold_convert_loc (loc, arg0_type,
4536 high_positive),
4537 build_int_cst (arg0_type, 1));
4539 /* If the low bound is specified, "and" the range with the
4540 range for which the original unsigned value will be
4541 positive. */
4542 if (low != 0)
4544 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4545 1, fold_convert_loc (loc, arg0_type,
4546 integer_zero_node),
4547 high_positive))
4548 return NULL_TREE;
4550 in_p = (n_in_p == in_p);
4552 else
4554 /* Otherwise, "or" the range with the range of the input
4555 that will be interpreted as negative. */
4556 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4557 1, fold_convert_loc (loc, arg0_type,
4558 integer_zero_node),
4559 high_positive))
4560 return NULL_TREE;
4562 in_p = (in_p != n_in_p);
4566 *p_low = n_low;
4567 *p_high = n_high;
4568 *p_in_p = in_p;
4569 return arg0;
4571 default:
4572 return NULL_TREE;
4576 /* Given EXP, a logical expression, set the range it is testing into
4577 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4578 actually being tested. *PLOW and *PHIGH will be made of the same
4579 type as the returned expression. If EXP is not a comparison, we
4580 will most likely not be returning a useful value and range. Set
4581 *STRICT_OVERFLOW_P to true if the return value is only valid
4582 because signed overflow is undefined; otherwise, do not change
4583 *STRICT_OVERFLOW_P. */
4585 tree
4586 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4587 bool *strict_overflow_p)
4589 enum tree_code code;
4590 tree arg0, arg1 = NULL_TREE;
4591 tree exp_type, nexp;
4592 int in_p;
4593 tree low, high;
4594 location_t loc = EXPR_LOCATION (exp);
4596 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4597 and see if we can refine the range. Some of the cases below may not
4598 happen, but it doesn't seem worth worrying about this. We "continue"
4599 the outer loop when we've changed something; otherwise we "break"
4600 the switch, which will "break" the while. */
4602 in_p = 0;
4603 low = high = build_int_cst (TREE_TYPE (exp), 0);
4605 while (1)
4607 code = TREE_CODE (exp);
4608 exp_type = TREE_TYPE (exp);
4609 arg0 = NULL_TREE;
4611 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4613 if (TREE_OPERAND_LENGTH (exp) > 0)
4614 arg0 = TREE_OPERAND (exp, 0);
4615 if (TREE_CODE_CLASS (code) == tcc_binary
4616 || TREE_CODE_CLASS (code) == tcc_comparison
4617 || (TREE_CODE_CLASS (code) == tcc_expression
4618 && TREE_OPERAND_LENGTH (exp) > 1))
4619 arg1 = TREE_OPERAND (exp, 1);
4621 if (arg0 == NULL_TREE)
4622 break;
4624 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4625 &high, &in_p, strict_overflow_p);
4626 if (nexp == NULL_TREE)
4627 break;
4628 exp = nexp;
4631 /* If EXP is a constant, we can evaluate whether this is true or false. */
4632 if (TREE_CODE (exp) == INTEGER_CST)
4634 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4635 exp, 0, low, 0))
4636 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4637 exp, 1, high, 1)));
4638 low = high = 0;
4639 exp = 0;
4642 *pin_p = in_p, *plow = low, *phigh = high;
4643 return exp;
4646 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4647 type, TYPE, return an expression to test if EXP is in (or out of, depending
4648 on IN_P) the range. Return 0 if the test couldn't be created. */
4650 tree
4651 build_range_check (location_t loc, tree type, tree exp, int in_p,
4652 tree low, tree high)
4654 tree etype = TREE_TYPE (exp), value;
4656 /* Disable this optimization for function pointer expressions
4657 on targets that require function pointer canonicalization. */
4658 if (targetm.have_canonicalize_funcptr_for_compare ()
4659 && TREE_CODE (etype) == POINTER_TYPE
4660 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4661 return NULL_TREE;
4663 if (! in_p)
4665 value = build_range_check (loc, type, exp, 1, low, high);
4666 if (value != 0)
4667 return invert_truthvalue_loc (loc, value);
4669 return 0;
4672 if (low == 0 && high == 0)
4673 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4675 if (low == 0)
4676 return fold_build2_loc (loc, LE_EXPR, type, exp,
4677 fold_convert_loc (loc, etype, high));
4679 if (high == 0)
4680 return fold_build2_loc (loc, GE_EXPR, type, exp,
4681 fold_convert_loc (loc, etype, low));
4683 if (operand_equal_p (low, high, 0))
4684 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4685 fold_convert_loc (loc, etype, low));
4687 if (integer_zerop (low))
4689 if (! TYPE_UNSIGNED (etype))
4691 etype = unsigned_type_for (etype);
4692 high = fold_convert_loc (loc, etype, high);
4693 exp = fold_convert_loc (loc, etype, exp);
4695 return build_range_check (loc, type, exp, 1, 0, high);
4698 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4699 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4701 int prec = TYPE_PRECISION (etype);
4703 if (wi::mask (prec - 1, false, prec) == high)
4705 if (TYPE_UNSIGNED (etype))
4707 tree signed_etype = signed_type_for (etype);
4708 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4709 etype
4710 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4711 else
4712 etype = signed_etype;
4713 exp = fold_convert_loc (loc, etype, exp);
4715 return fold_build2_loc (loc, GT_EXPR, type, exp,
4716 build_int_cst (etype, 0));
4720 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4721 This requires wrap-around arithmetics for the type of the expression.
4722 First make sure that arithmetics in this type is valid, then make sure
4723 that it wraps around. */
4724 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4725 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4726 TYPE_UNSIGNED (etype));
4728 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4730 tree utype, minv, maxv;
4732 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4733 for the type in question, as we rely on this here. */
4734 utype = unsigned_type_for (etype);
4735 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4736 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4737 build_int_cst (TREE_TYPE (maxv), 1), 1);
4738 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4740 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4741 minv, 1, maxv, 1)))
4742 etype = utype;
4743 else
4744 return 0;
4747 high = fold_convert_loc (loc, etype, high);
4748 low = fold_convert_loc (loc, etype, low);
4749 exp = fold_convert_loc (loc, etype, exp);
4751 value = const_binop (MINUS_EXPR, high, low);
4754 if (POINTER_TYPE_P (etype))
4756 if (value != 0 && !TREE_OVERFLOW (value))
4758 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4759 return build_range_check (loc, type,
4760 fold_build_pointer_plus_loc (loc, exp, low),
4761 1, build_int_cst (etype, 0), value);
4763 return 0;
4766 if (value != 0 && !TREE_OVERFLOW (value))
4767 return build_range_check (loc, type,
4768 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4769 1, build_int_cst (etype, 0), value);
4771 return 0;
4774 /* Return the predecessor of VAL in its type, handling the infinite case. */
4776 static tree
4777 range_predecessor (tree val)
4779 tree type = TREE_TYPE (val);
4781 if (INTEGRAL_TYPE_P (type)
4782 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4783 return 0;
4784 else
4785 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4786 build_int_cst (TREE_TYPE (val), 1), 0);
4789 /* Return the successor of VAL in its type, handling the infinite case. */
4791 static tree
4792 range_successor (tree val)
4794 tree type = TREE_TYPE (val);
4796 if (INTEGRAL_TYPE_P (type)
4797 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4798 return 0;
4799 else
4800 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4801 build_int_cst (TREE_TYPE (val), 1), 0);
4804 /* Given two ranges, see if we can merge them into one. Return 1 if we
4805 can, 0 if we can't. Set the output range into the specified parameters. */
4807 bool
4808 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4809 tree high0, int in1_p, tree low1, tree high1)
4811 int no_overlap;
4812 int subset;
4813 int temp;
4814 tree tem;
4815 int in_p;
4816 tree low, high;
4817 int lowequal = ((low0 == 0 && low1 == 0)
4818 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4819 low0, 0, low1, 0)));
4820 int highequal = ((high0 == 0 && high1 == 0)
4821 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4822 high0, 1, high1, 1)));
4824 /* Make range 0 be the range that starts first, or ends last if they
4825 start at the same value. Swap them if it isn't. */
4826 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4827 low0, 0, low1, 0))
4828 || (lowequal
4829 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4830 high1, 1, high0, 1))))
4832 temp = in0_p, in0_p = in1_p, in1_p = temp;
4833 tem = low0, low0 = low1, low1 = tem;
4834 tem = high0, high0 = high1, high1 = tem;
4837 /* Now flag two cases, whether the ranges are disjoint or whether the
4838 second range is totally subsumed in the first. Note that the tests
4839 below are simplified by the ones above. */
4840 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4841 high0, 1, low1, 0));
4842 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4843 high1, 1, high0, 1));
4845 /* We now have four cases, depending on whether we are including or
4846 excluding the two ranges. */
4847 if (in0_p && in1_p)
4849 /* If they don't overlap, the result is false. If the second range
4850 is a subset it is the result. Otherwise, the range is from the start
4851 of the second to the end of the first. */
4852 if (no_overlap)
4853 in_p = 0, low = high = 0;
4854 else if (subset)
4855 in_p = 1, low = low1, high = high1;
4856 else
4857 in_p = 1, low = low1, high = high0;
4860 else if (in0_p && ! in1_p)
4862 /* If they don't overlap, the result is the first range. If they are
4863 equal, the result is false. If the second range is a subset of the
4864 first, and the ranges begin at the same place, we go from just after
4865 the end of the second range to the end of the first. If the second
4866 range is not a subset of the first, or if it is a subset and both
4867 ranges end at the same place, the range starts at the start of the
4868 first range and ends just before the second range.
4869 Otherwise, we can't describe this as a single range. */
4870 if (no_overlap)
4871 in_p = 1, low = low0, high = high0;
4872 else if (lowequal && highequal)
4873 in_p = 0, low = high = 0;
4874 else if (subset && lowequal)
4876 low = range_successor (high1);
4877 high = high0;
4878 in_p = 1;
4879 if (low == 0)
4881 /* We are in the weird situation where high0 > high1 but
4882 high1 has no successor. Punt. */
4883 return 0;
4886 else if (! subset || highequal)
4888 low = low0;
4889 high = range_predecessor (low1);
4890 in_p = 1;
4891 if (high == 0)
4893 /* low0 < low1 but low1 has no predecessor. Punt. */
4894 return 0;
4897 else
4898 return 0;
4901 else if (! in0_p && in1_p)
4903 /* If they don't overlap, the result is the second range. If the second
4904 is a subset of the first, the result is false. Otherwise,
4905 the range starts just after the first range and ends at the
4906 end of the second. */
4907 if (no_overlap)
4908 in_p = 1, low = low1, high = high1;
4909 else if (subset || highequal)
4910 in_p = 0, low = high = 0;
4911 else
4913 low = range_successor (high0);
4914 high = high1;
4915 in_p = 1;
4916 if (low == 0)
4918 /* high1 > high0 but high0 has no successor. Punt. */
4919 return 0;
4924 else
4926 /* The case where we are excluding both ranges. Here the complex case
4927 is if they don't overlap. In that case, the only time we have a
4928 range is if they are adjacent. If the second is a subset of the
4929 first, the result is the first. Otherwise, the range to exclude
4930 starts at the beginning of the first range and ends at the end of the
4931 second. */
4932 if (no_overlap)
4934 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4935 range_successor (high0),
4936 1, low1, 0)))
4937 in_p = 0, low = low0, high = high1;
4938 else
4940 /* Canonicalize - [min, x] into - [-, x]. */
4941 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4942 switch (TREE_CODE (TREE_TYPE (low0)))
4944 case ENUMERAL_TYPE:
4945 if (TYPE_PRECISION (TREE_TYPE (low0))
4946 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4947 break;
4948 /* FALLTHROUGH */
4949 case INTEGER_TYPE:
4950 if (tree_int_cst_equal (low0,
4951 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4952 low0 = 0;
4953 break;
4954 case POINTER_TYPE:
4955 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4956 && integer_zerop (low0))
4957 low0 = 0;
4958 break;
4959 default:
4960 break;
4963 /* Canonicalize - [x, max] into - [x, -]. */
4964 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4965 switch (TREE_CODE (TREE_TYPE (high1)))
4967 case ENUMERAL_TYPE:
4968 if (TYPE_PRECISION (TREE_TYPE (high1))
4969 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4970 break;
4971 /* FALLTHROUGH */
4972 case INTEGER_TYPE:
4973 if (tree_int_cst_equal (high1,
4974 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4975 high1 = 0;
4976 break;
4977 case POINTER_TYPE:
4978 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4979 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4980 high1, 1,
4981 build_int_cst (TREE_TYPE (high1), 1),
4982 1)))
4983 high1 = 0;
4984 break;
4985 default:
4986 break;
4989 /* The ranges might be also adjacent between the maximum and
4990 minimum values of the given type. For
4991 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4992 return + [x + 1, y - 1]. */
4993 if (low0 == 0 && high1 == 0)
4995 low = range_successor (high0);
4996 high = range_predecessor (low1);
4997 if (low == 0 || high == 0)
4998 return 0;
5000 in_p = 1;
5002 else
5003 return 0;
5006 else if (subset)
5007 in_p = 0, low = low0, high = high0;
5008 else
5009 in_p = 0, low = low0, high = high1;
5012 *pin_p = in_p, *plow = low, *phigh = high;
5013 return 1;
5017 /* Subroutine of fold, looking inside expressions of the form
5018 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5019 of the COND_EXPR. This function is being used also to optimize
5020 A op B ? C : A, by reversing the comparison first.
5022 Return a folded expression whose code is not a COND_EXPR
5023 anymore, or NULL_TREE if no folding opportunity is found. */
5025 static tree
5026 fold_cond_expr_with_comparison (location_t loc, tree type,
5027 tree arg0, tree arg1, tree arg2)
5029 enum tree_code comp_code = TREE_CODE (arg0);
5030 tree arg00 = TREE_OPERAND (arg0, 0);
5031 tree arg01 = TREE_OPERAND (arg0, 1);
5032 tree arg1_type = TREE_TYPE (arg1);
5033 tree tem;
5035 STRIP_NOPS (arg1);
5036 STRIP_NOPS (arg2);
5038 /* If we have A op 0 ? A : -A, consider applying the following
5039 transformations:
5041 A == 0? A : -A same as -A
5042 A != 0? A : -A same as A
5043 A >= 0? A : -A same as abs (A)
5044 A > 0? A : -A same as abs (A)
5045 A <= 0? A : -A same as -abs (A)
5046 A < 0? A : -A same as -abs (A)
5048 None of these transformations work for modes with signed
5049 zeros. If A is +/-0, the first two transformations will
5050 change the sign of the result (from +0 to -0, or vice
5051 versa). The last four will fix the sign of the result,
5052 even though the original expressions could be positive or
5053 negative, depending on the sign of A.
5055 Note that all these transformations are correct if A is
5056 NaN, since the two alternatives (A and -A) are also NaNs. */
5057 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5058 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5059 ? real_zerop (arg01)
5060 : integer_zerop (arg01))
5061 && ((TREE_CODE (arg2) == NEGATE_EXPR
5062 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5063 /* In the case that A is of the form X-Y, '-A' (arg2) may
5064 have already been folded to Y-X, check for that. */
5065 || (TREE_CODE (arg1) == MINUS_EXPR
5066 && TREE_CODE (arg2) == MINUS_EXPR
5067 && operand_equal_p (TREE_OPERAND (arg1, 0),
5068 TREE_OPERAND (arg2, 1), 0)
5069 && operand_equal_p (TREE_OPERAND (arg1, 1),
5070 TREE_OPERAND (arg2, 0), 0))))
5071 switch (comp_code)
5073 case EQ_EXPR:
5074 case UNEQ_EXPR:
5075 tem = fold_convert_loc (loc, arg1_type, arg1);
5076 return pedantic_non_lvalue_loc (loc,
5077 fold_convert_loc (loc, type,
5078 negate_expr (tem)));
5079 case NE_EXPR:
5080 case LTGT_EXPR:
5081 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5082 case UNGE_EXPR:
5083 case UNGT_EXPR:
5084 if (flag_trapping_math)
5085 break;
5086 /* Fall through. */
5087 case GE_EXPR:
5088 case GT_EXPR:
5089 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5090 break;
5091 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5092 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5093 case UNLE_EXPR:
5094 case UNLT_EXPR:
5095 if (flag_trapping_math)
5096 break;
5097 case LE_EXPR:
5098 case LT_EXPR:
5099 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5100 break;
5101 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5102 return negate_expr (fold_convert_loc (loc, type, tem));
5103 default:
5104 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5105 break;
5108 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5109 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5110 both transformations are correct when A is NaN: A != 0
5111 is then true, and A == 0 is false. */
5113 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5114 && integer_zerop (arg01) && integer_zerop (arg2))
5116 if (comp_code == NE_EXPR)
5117 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5118 else if (comp_code == EQ_EXPR)
5119 return build_zero_cst (type);
5122 /* Try some transformations of A op B ? A : B.
5124 A == B? A : B same as B
5125 A != B? A : B same as A
5126 A >= B? A : B same as max (A, B)
5127 A > B? A : B same as max (B, A)
5128 A <= B? A : B same as min (A, B)
5129 A < B? A : B same as min (B, A)
5131 As above, these transformations don't work in the presence
5132 of signed zeros. For example, if A and B are zeros of
5133 opposite sign, the first two transformations will change
5134 the sign of the result. In the last four, the original
5135 expressions give different results for (A=+0, B=-0) and
5136 (A=-0, B=+0), but the transformed expressions do not.
5138 The first two transformations are correct if either A or B
5139 is a NaN. In the first transformation, the condition will
5140 be false, and B will indeed be chosen. In the case of the
5141 second transformation, the condition A != B will be true,
5142 and A will be chosen.
5144 The conversions to max() and min() are not correct if B is
5145 a number and A is not. The conditions in the original
5146 expressions will be false, so all four give B. The min()
5147 and max() versions would give a NaN instead. */
5148 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5149 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5150 /* Avoid these transformations if the COND_EXPR may be used
5151 as an lvalue in the C++ front-end. PR c++/19199. */
5152 && (in_gimple_form
5153 || VECTOR_TYPE_P (type)
5154 || (! lang_GNU_CXX ()
5155 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5156 || ! maybe_lvalue_p (arg1)
5157 || ! maybe_lvalue_p (arg2)))
5159 tree comp_op0 = arg00;
5160 tree comp_op1 = arg01;
5161 tree comp_type = TREE_TYPE (comp_op0);
5163 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5164 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5166 comp_type = type;
5167 comp_op0 = arg1;
5168 comp_op1 = arg2;
5171 switch (comp_code)
5173 case EQ_EXPR:
5174 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5175 case NE_EXPR:
5176 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5177 case LE_EXPR:
5178 case LT_EXPR:
5179 case UNLE_EXPR:
5180 case UNLT_EXPR:
5181 /* In C++ a ?: expression can be an lvalue, so put the
5182 operand which will be used if they are equal first
5183 so that we can convert this back to the
5184 corresponding COND_EXPR. */
5185 if (!HONOR_NANS (arg1))
5187 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5188 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5189 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5190 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5191 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5192 comp_op1, comp_op0);
5193 return pedantic_non_lvalue_loc (loc,
5194 fold_convert_loc (loc, type, tem));
5196 break;
5197 case GE_EXPR:
5198 case GT_EXPR:
5199 case UNGE_EXPR:
5200 case UNGT_EXPR:
5201 if (!HONOR_NANS (arg1))
5203 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5204 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5205 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5206 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5207 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5208 comp_op1, comp_op0);
5209 return pedantic_non_lvalue_loc (loc,
5210 fold_convert_loc (loc, type, tem));
5212 break;
5213 case UNEQ_EXPR:
5214 if (!HONOR_NANS (arg1))
5215 return pedantic_non_lvalue_loc (loc,
5216 fold_convert_loc (loc, type, arg2));
5217 break;
5218 case LTGT_EXPR:
5219 if (!HONOR_NANS (arg1))
5220 return pedantic_non_lvalue_loc (loc,
5221 fold_convert_loc (loc, type, arg1));
5222 break;
5223 default:
5224 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5225 break;
5229 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5230 we might still be able to simplify this. For example,
5231 if C1 is one less or one more than C2, this might have started
5232 out as a MIN or MAX and been transformed by this function.
5233 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5235 if (INTEGRAL_TYPE_P (type)
5236 && TREE_CODE (arg01) == INTEGER_CST
5237 && TREE_CODE (arg2) == INTEGER_CST)
5238 switch (comp_code)
5240 case EQ_EXPR:
5241 if (TREE_CODE (arg1) == INTEGER_CST)
5242 break;
5243 /* We can replace A with C1 in this case. */
5244 arg1 = fold_convert_loc (loc, type, arg01);
5245 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5247 case LT_EXPR:
5248 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5249 MIN_EXPR, to preserve the signedness of the comparison. */
5250 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5251 OEP_ONLY_CONST)
5252 && operand_equal_p (arg01,
5253 const_binop (PLUS_EXPR, arg2,
5254 build_int_cst (type, 1)),
5255 OEP_ONLY_CONST))
5257 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5258 fold_convert_loc (loc, TREE_TYPE (arg00),
5259 arg2));
5260 return pedantic_non_lvalue_loc (loc,
5261 fold_convert_loc (loc, type, tem));
5263 break;
5265 case LE_EXPR:
5266 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5267 as above. */
5268 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5269 OEP_ONLY_CONST)
5270 && operand_equal_p (arg01,
5271 const_binop (MINUS_EXPR, arg2,
5272 build_int_cst (type, 1)),
5273 OEP_ONLY_CONST))
5275 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5276 fold_convert_loc (loc, TREE_TYPE (arg00),
5277 arg2));
5278 return pedantic_non_lvalue_loc (loc,
5279 fold_convert_loc (loc, type, tem));
5281 break;
5283 case GT_EXPR:
5284 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5285 MAX_EXPR, to preserve the signedness of the comparison. */
5286 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5287 OEP_ONLY_CONST)
5288 && operand_equal_p (arg01,
5289 const_binop (MINUS_EXPR, arg2,
5290 build_int_cst (type, 1)),
5291 OEP_ONLY_CONST))
5293 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5294 fold_convert_loc (loc, TREE_TYPE (arg00),
5295 arg2));
5296 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5298 break;
5300 case GE_EXPR:
5301 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5302 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5303 OEP_ONLY_CONST)
5304 && operand_equal_p (arg01,
5305 const_binop (PLUS_EXPR, arg2,
5306 build_int_cst (type, 1)),
5307 OEP_ONLY_CONST))
5309 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5310 fold_convert_loc (loc, TREE_TYPE (arg00),
5311 arg2));
5312 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5314 break;
5315 case NE_EXPR:
5316 break;
5317 default:
5318 gcc_unreachable ();
5321 return NULL_TREE;
5326 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5327 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5328 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5329 false) >= 2)
5330 #endif
5332 /* EXP is some logical combination of boolean tests. See if we can
5333 merge it into some range test. Return the new tree if so. */
5335 static tree
5336 fold_range_test (location_t loc, enum tree_code code, tree type,
5337 tree op0, tree op1)
5339 int or_op = (code == TRUTH_ORIF_EXPR
5340 || code == TRUTH_OR_EXPR);
5341 int in0_p, in1_p, in_p;
5342 tree low0, low1, low, high0, high1, high;
5343 bool strict_overflow_p = false;
5344 tree tem, lhs, rhs;
5345 const char * const warnmsg = G_("assuming signed overflow does not occur "
5346 "when simplifying range test");
5348 if (!INTEGRAL_TYPE_P (type))
5349 return 0;
5351 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5352 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5354 /* If this is an OR operation, invert both sides; we will invert
5355 again at the end. */
5356 if (or_op)
5357 in0_p = ! in0_p, in1_p = ! in1_p;
5359 /* If both expressions are the same, if we can merge the ranges, and we
5360 can build the range test, return it or it inverted. If one of the
5361 ranges is always true or always false, consider it to be the same
5362 expression as the other. */
5363 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5364 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5365 in1_p, low1, high1)
5366 && 0 != (tem = (build_range_check (loc, type,
5367 lhs != 0 ? lhs
5368 : rhs != 0 ? rhs : integer_zero_node,
5369 in_p, low, high))))
5371 if (strict_overflow_p)
5372 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5373 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5376 /* On machines where the branch cost is expensive, if this is a
5377 short-circuited branch and the underlying object on both sides
5378 is the same, make a non-short-circuit operation. */
5379 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5380 && lhs != 0 && rhs != 0
5381 && (code == TRUTH_ANDIF_EXPR
5382 || code == TRUTH_ORIF_EXPR)
5383 && operand_equal_p (lhs, rhs, 0))
5385 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5386 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5387 which cases we can't do this. */
5388 if (simple_operand_p (lhs))
5389 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5390 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5391 type, op0, op1);
5393 else if (!lang_hooks.decls.global_bindings_p ()
5394 && !CONTAINS_PLACEHOLDER_P (lhs))
5396 tree common = save_expr (lhs);
5398 if (0 != (lhs = build_range_check (loc, type, common,
5399 or_op ? ! in0_p : in0_p,
5400 low0, high0))
5401 && (0 != (rhs = build_range_check (loc, type, common,
5402 or_op ? ! in1_p : in1_p,
5403 low1, high1))))
5405 if (strict_overflow_p)
5406 fold_overflow_warning (warnmsg,
5407 WARN_STRICT_OVERFLOW_COMPARISON);
5408 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5409 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5410 type, lhs, rhs);
5415 return 0;
5418 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5419 bit value. Arrange things so the extra bits will be set to zero if and
5420 only if C is signed-extended to its full width. If MASK is nonzero,
5421 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5423 static tree
5424 unextend (tree c, int p, int unsignedp, tree mask)
5426 tree type = TREE_TYPE (c);
5427 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5428 tree temp;
5430 if (p == modesize || unsignedp)
5431 return c;
5433 /* We work by getting just the sign bit into the low-order bit, then
5434 into the high-order bit, then sign-extend. We then XOR that value
5435 with C. */
5436 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5438 /* We must use a signed type in order to get an arithmetic right shift.
5439 However, we must also avoid introducing accidental overflows, so that
5440 a subsequent call to integer_zerop will work. Hence we must
5441 do the type conversion here. At this point, the constant is either
5442 zero or one, and the conversion to a signed type can never overflow.
5443 We could get an overflow if this conversion is done anywhere else. */
5444 if (TYPE_UNSIGNED (type))
5445 temp = fold_convert (signed_type_for (type), temp);
5447 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5448 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5449 if (mask != 0)
5450 temp = const_binop (BIT_AND_EXPR, temp,
5451 fold_convert (TREE_TYPE (c), mask));
5452 /* If necessary, convert the type back to match the type of C. */
5453 if (TYPE_UNSIGNED (type))
5454 temp = fold_convert (type, temp);
5456 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5459 /* For an expression that has the form
5460 (A && B) || ~B
5462 (A || B) && ~B,
5463 we can drop one of the inner expressions and simplify to
5464 A || ~B
5466 A && ~B
5467 LOC is the location of the resulting expression. OP is the inner
5468 logical operation; the left-hand side in the examples above, while CMPOP
5469 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5470 removing a condition that guards another, as in
5471 (A != NULL && A->...) || A == NULL
5472 which we must not transform. If RHS_ONLY is true, only eliminate the
5473 right-most operand of the inner logical operation. */
5475 static tree
5476 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5477 bool rhs_only)
5479 tree type = TREE_TYPE (cmpop);
5480 enum tree_code code = TREE_CODE (cmpop);
5481 enum tree_code truthop_code = TREE_CODE (op);
5482 tree lhs = TREE_OPERAND (op, 0);
5483 tree rhs = TREE_OPERAND (op, 1);
5484 tree orig_lhs = lhs, orig_rhs = rhs;
5485 enum tree_code rhs_code = TREE_CODE (rhs);
5486 enum tree_code lhs_code = TREE_CODE (lhs);
5487 enum tree_code inv_code;
5489 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5490 return NULL_TREE;
5492 if (TREE_CODE_CLASS (code) != tcc_comparison)
5493 return NULL_TREE;
5495 if (rhs_code == truthop_code)
5497 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5498 if (newrhs != NULL_TREE)
5500 rhs = newrhs;
5501 rhs_code = TREE_CODE (rhs);
5504 if (lhs_code == truthop_code && !rhs_only)
5506 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5507 if (newlhs != NULL_TREE)
5509 lhs = newlhs;
5510 lhs_code = TREE_CODE (lhs);
5514 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5515 if (inv_code == rhs_code
5516 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5517 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5518 return lhs;
5519 if (!rhs_only && inv_code == lhs_code
5520 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5521 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5522 return rhs;
5523 if (rhs != orig_rhs || lhs != orig_lhs)
5524 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5525 lhs, rhs);
5526 return NULL_TREE;
5529 /* Find ways of folding logical expressions of LHS and RHS:
5530 Try to merge two comparisons to the same innermost item.
5531 Look for range tests like "ch >= '0' && ch <= '9'".
5532 Look for combinations of simple terms on machines with expensive branches
5533 and evaluate the RHS unconditionally.
5535 For example, if we have p->a == 2 && p->b == 4 and we can make an
5536 object large enough to span both A and B, we can do this with a comparison
5537 against the object ANDed with the a mask.
5539 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5540 operations to do this with one comparison.
5542 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5543 function and the one above.
5545 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5546 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5548 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5549 two operands.
5551 We return the simplified tree or 0 if no optimization is possible. */
5553 static tree
5554 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5555 tree lhs, tree rhs)
5557 /* If this is the "or" of two comparisons, we can do something if
5558 the comparisons are NE_EXPR. If this is the "and", we can do something
5559 if the comparisons are EQ_EXPR. I.e.,
5560 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5562 WANTED_CODE is this operation code. For single bit fields, we can
5563 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5564 comparison for one-bit fields. */
5566 enum tree_code wanted_code;
5567 enum tree_code lcode, rcode;
5568 tree ll_arg, lr_arg, rl_arg, rr_arg;
5569 tree ll_inner, lr_inner, rl_inner, rr_inner;
5570 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5571 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5572 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5573 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5574 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5575 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5576 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5577 machine_mode lnmode, rnmode;
5578 tree ll_mask, lr_mask, rl_mask, rr_mask;
5579 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5580 tree l_const, r_const;
5581 tree lntype, rntype, result;
5582 HOST_WIDE_INT first_bit, end_bit;
5583 int volatilep;
5585 /* Start by getting the comparison codes. Fail if anything is volatile.
5586 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5587 it were surrounded with a NE_EXPR. */
5589 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5590 return 0;
5592 lcode = TREE_CODE (lhs);
5593 rcode = TREE_CODE (rhs);
5595 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5597 lhs = build2 (NE_EXPR, truth_type, lhs,
5598 build_int_cst (TREE_TYPE (lhs), 0));
5599 lcode = NE_EXPR;
5602 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5604 rhs = build2 (NE_EXPR, truth_type, rhs,
5605 build_int_cst (TREE_TYPE (rhs), 0));
5606 rcode = NE_EXPR;
5609 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5610 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5611 return 0;
5613 ll_arg = TREE_OPERAND (lhs, 0);
5614 lr_arg = TREE_OPERAND (lhs, 1);
5615 rl_arg = TREE_OPERAND (rhs, 0);
5616 rr_arg = TREE_OPERAND (rhs, 1);
5618 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5619 if (simple_operand_p (ll_arg)
5620 && simple_operand_p (lr_arg))
5622 if (operand_equal_p (ll_arg, rl_arg, 0)
5623 && operand_equal_p (lr_arg, rr_arg, 0))
5625 result = combine_comparisons (loc, code, lcode, rcode,
5626 truth_type, ll_arg, lr_arg);
5627 if (result)
5628 return result;
5630 else if (operand_equal_p (ll_arg, rr_arg, 0)
5631 && operand_equal_p (lr_arg, rl_arg, 0))
5633 result = combine_comparisons (loc, code, lcode,
5634 swap_tree_comparison (rcode),
5635 truth_type, ll_arg, lr_arg);
5636 if (result)
5637 return result;
5641 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5642 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5644 /* If the RHS can be evaluated unconditionally and its operands are
5645 simple, it wins to evaluate the RHS unconditionally on machines
5646 with expensive branches. In this case, this isn't a comparison
5647 that can be merged. */
5649 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5650 false) >= 2
5651 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5652 && simple_operand_p (rl_arg)
5653 && simple_operand_p (rr_arg))
5655 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5656 if (code == TRUTH_OR_EXPR
5657 && lcode == NE_EXPR && integer_zerop (lr_arg)
5658 && rcode == NE_EXPR && integer_zerop (rr_arg)
5659 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5660 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5661 return build2_loc (loc, NE_EXPR, truth_type,
5662 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5663 ll_arg, rl_arg),
5664 build_int_cst (TREE_TYPE (ll_arg), 0));
5666 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5667 if (code == TRUTH_AND_EXPR
5668 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5669 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5670 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5671 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5672 return build2_loc (loc, EQ_EXPR, truth_type,
5673 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5674 ll_arg, rl_arg),
5675 build_int_cst (TREE_TYPE (ll_arg), 0));
5678 /* See if the comparisons can be merged. Then get all the parameters for
5679 each side. */
5681 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5682 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5683 return 0;
5685 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5686 volatilep = 0;
5687 ll_inner = decode_field_reference (loc, &ll_arg,
5688 &ll_bitsize, &ll_bitpos, &ll_mode,
5689 &ll_unsignedp, &ll_reversep, &volatilep,
5690 &ll_mask, &ll_and_mask);
5691 lr_inner = decode_field_reference (loc, &lr_arg,
5692 &lr_bitsize, &lr_bitpos, &lr_mode,
5693 &lr_unsignedp, &lr_reversep, &volatilep,
5694 &lr_mask, &lr_and_mask);
5695 rl_inner = decode_field_reference (loc, &rl_arg,
5696 &rl_bitsize, &rl_bitpos, &rl_mode,
5697 &rl_unsignedp, &rl_reversep, &volatilep,
5698 &rl_mask, &rl_and_mask);
5699 rr_inner = decode_field_reference (loc, &rr_arg,
5700 &rr_bitsize, &rr_bitpos, &rr_mode,
5701 &rr_unsignedp, &rr_reversep, &volatilep,
5702 &rr_mask, &rr_and_mask);
5704 /* It must be true that the inner operation on the lhs of each
5705 comparison must be the same if we are to be able to do anything.
5706 Then see if we have constants. If not, the same must be true for
5707 the rhs's. */
5708 if (volatilep
5709 || ll_reversep != rl_reversep
5710 || ll_inner == 0 || rl_inner == 0
5711 || ! operand_equal_p (ll_inner, rl_inner, 0))
5712 return 0;
5714 if (TREE_CODE (lr_arg) == INTEGER_CST
5715 && TREE_CODE (rr_arg) == INTEGER_CST)
5717 l_const = lr_arg, r_const = rr_arg;
5718 lr_reversep = ll_reversep;
5720 else if (lr_reversep != rr_reversep
5721 || lr_inner == 0 || rr_inner == 0
5722 || ! operand_equal_p (lr_inner, rr_inner, 0))
5723 return 0;
5724 else
5725 l_const = r_const = 0;
5727 /* If either comparison code is not correct for our logical operation,
5728 fail. However, we can convert a one-bit comparison against zero into
5729 the opposite comparison against that bit being set in the field. */
5731 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5732 if (lcode != wanted_code)
5734 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5736 /* Make the left operand unsigned, since we are only interested
5737 in the value of one bit. Otherwise we are doing the wrong
5738 thing below. */
5739 ll_unsignedp = 1;
5740 l_const = ll_mask;
5742 else
5743 return 0;
5746 /* This is analogous to the code for l_const above. */
5747 if (rcode != wanted_code)
5749 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5751 rl_unsignedp = 1;
5752 r_const = rl_mask;
5754 else
5755 return 0;
5758 /* See if we can find a mode that contains both fields being compared on
5759 the left. If we can't, fail. Otherwise, update all constants and masks
5760 to be relative to a field of that size. */
5761 first_bit = MIN (ll_bitpos, rl_bitpos);
5762 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5763 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5764 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5765 volatilep);
5766 if (lnmode == VOIDmode)
5767 return 0;
5769 lnbitsize = GET_MODE_BITSIZE (lnmode);
5770 lnbitpos = first_bit & ~ (lnbitsize - 1);
5771 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5772 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5774 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5776 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5777 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5780 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5781 size_int (xll_bitpos));
5782 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5783 size_int (xrl_bitpos));
5785 if (l_const)
5787 l_const = fold_convert_loc (loc, lntype, l_const);
5788 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5789 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5790 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5791 fold_build1_loc (loc, BIT_NOT_EXPR,
5792 lntype, ll_mask))))
5794 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5796 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5799 if (r_const)
5801 r_const = fold_convert_loc (loc, lntype, r_const);
5802 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5803 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5804 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5805 fold_build1_loc (loc, BIT_NOT_EXPR,
5806 lntype, rl_mask))))
5808 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5810 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5814 /* If the right sides are not constant, do the same for it. Also,
5815 disallow this optimization if a size or signedness mismatch occurs
5816 between the left and right sides. */
5817 if (l_const == 0)
5819 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5820 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5821 /* Make sure the two fields on the right
5822 correspond to the left without being swapped. */
5823 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5824 return 0;
5826 first_bit = MIN (lr_bitpos, rr_bitpos);
5827 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5828 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5829 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5830 volatilep);
5831 if (rnmode == VOIDmode)
5832 return 0;
5834 rnbitsize = GET_MODE_BITSIZE (rnmode);
5835 rnbitpos = first_bit & ~ (rnbitsize - 1);
5836 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5837 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5839 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5841 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5842 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5845 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5846 rntype, lr_mask),
5847 size_int (xlr_bitpos));
5848 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5849 rntype, rr_mask),
5850 size_int (xrr_bitpos));
5852 /* Make a mask that corresponds to both fields being compared.
5853 Do this for both items being compared. If the operands are the
5854 same size and the bits being compared are in the same position
5855 then we can do this by masking both and comparing the masked
5856 results. */
5857 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5858 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5859 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5861 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5862 lntype, lnbitsize, lnbitpos,
5863 ll_unsignedp || rl_unsignedp, ll_reversep);
5864 if (! all_ones_mask_p (ll_mask, lnbitsize))
5865 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5867 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5868 rntype, rnbitsize, rnbitpos,
5869 lr_unsignedp || rr_unsignedp, lr_reversep);
5870 if (! all_ones_mask_p (lr_mask, rnbitsize))
5871 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5873 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5876 /* There is still another way we can do something: If both pairs of
5877 fields being compared are adjacent, we may be able to make a wider
5878 field containing them both.
5880 Note that we still must mask the lhs/rhs expressions. Furthermore,
5881 the mask must be shifted to account for the shift done by
5882 make_bit_field_ref. */
5883 if ((ll_bitsize + ll_bitpos == rl_bitpos
5884 && lr_bitsize + lr_bitpos == rr_bitpos)
5885 || (ll_bitpos == rl_bitpos + rl_bitsize
5886 && lr_bitpos == rr_bitpos + rr_bitsize))
5888 tree type;
5890 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5891 ll_bitsize + rl_bitsize,
5892 MIN (ll_bitpos, rl_bitpos),
5893 ll_unsignedp, ll_reversep);
5894 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5895 lr_bitsize + rr_bitsize,
5896 MIN (lr_bitpos, rr_bitpos),
5897 lr_unsignedp, lr_reversep);
5899 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5900 size_int (MIN (xll_bitpos, xrl_bitpos)));
5901 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5902 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5904 /* Convert to the smaller type before masking out unwanted bits. */
5905 type = lntype;
5906 if (lntype != rntype)
5908 if (lnbitsize > rnbitsize)
5910 lhs = fold_convert_loc (loc, rntype, lhs);
5911 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5912 type = rntype;
5914 else if (lnbitsize < rnbitsize)
5916 rhs = fold_convert_loc (loc, lntype, rhs);
5917 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5918 type = lntype;
5922 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5923 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5925 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5926 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5928 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5931 return 0;
5934 /* Handle the case of comparisons with constants. If there is something in
5935 common between the masks, those bits of the constants must be the same.
5936 If not, the condition is always false. Test for this to avoid generating
5937 incorrect code below. */
5938 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5939 if (! integer_zerop (result)
5940 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5941 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5943 if (wanted_code == NE_EXPR)
5945 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5946 return constant_boolean_node (true, truth_type);
5948 else
5950 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5951 return constant_boolean_node (false, truth_type);
5955 /* Construct the expression we will return. First get the component
5956 reference we will make. Unless the mask is all ones the width of
5957 that field, perform the mask operation. Then compare with the
5958 merged constant. */
5959 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5960 lntype, lnbitsize, lnbitpos,
5961 ll_unsignedp || rl_unsignedp, ll_reversep);
5963 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5964 if (! all_ones_mask_p (ll_mask, lnbitsize))
5965 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5967 return build2_loc (loc, wanted_code, truth_type, result,
5968 const_binop (BIT_IOR_EXPR, l_const, r_const));
5971 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5972 constant. */
5974 static tree
5975 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5976 tree op0, tree op1)
5978 tree arg0 = op0;
5979 enum tree_code op_code;
5980 tree comp_const;
5981 tree minmax_const;
5982 int consts_equal, consts_lt;
5983 tree inner;
5985 STRIP_SIGN_NOPS (arg0);
5987 op_code = TREE_CODE (arg0);
5988 minmax_const = TREE_OPERAND (arg0, 1);
5989 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5990 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5991 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5992 inner = TREE_OPERAND (arg0, 0);
5994 /* If something does not permit us to optimize, return the original tree. */
5995 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5996 || TREE_CODE (comp_const) != INTEGER_CST
5997 || TREE_OVERFLOW (comp_const)
5998 || TREE_CODE (minmax_const) != INTEGER_CST
5999 || TREE_OVERFLOW (minmax_const))
6000 return NULL_TREE;
6002 /* Now handle all the various comparison codes. We only handle EQ_EXPR
6003 and GT_EXPR, doing the rest with recursive calls using logical
6004 simplifications. */
6005 switch (code)
6007 case NE_EXPR: case LT_EXPR: case LE_EXPR:
6009 tree tem
6010 = optimize_minmax_comparison (loc,
6011 invert_tree_comparison (code, false),
6012 type, op0, op1);
6013 if (tem)
6014 return invert_truthvalue_loc (loc, tem);
6015 return NULL_TREE;
6018 case GE_EXPR:
6019 return
6020 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
6021 optimize_minmax_comparison
6022 (loc, EQ_EXPR, type, arg0, comp_const),
6023 optimize_minmax_comparison
6024 (loc, GT_EXPR, type, arg0, comp_const));
6026 case EQ_EXPR:
6027 if (op_code == MAX_EXPR && consts_equal)
6028 /* MAX (X, 0) == 0 -> X <= 0 */
6029 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6031 else if (op_code == MAX_EXPR && consts_lt)
6032 /* MAX (X, 0) == 5 -> X == 5 */
6033 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6035 else if (op_code == MAX_EXPR)
6036 /* MAX (X, 0) == -1 -> false */
6037 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6039 else if (consts_equal)
6040 /* MIN (X, 0) == 0 -> X >= 0 */
6041 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6043 else if (consts_lt)
6044 /* MIN (X, 0) == 5 -> false */
6045 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6047 else
6048 /* MIN (X, 0) == -1 -> X == -1 */
6049 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6051 case GT_EXPR:
6052 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6053 /* MAX (X, 0) > 0 -> X > 0
6054 MAX (X, 0) > 5 -> X > 5 */
6055 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6057 else if (op_code == MAX_EXPR)
6058 /* MAX (X, 0) > -1 -> true */
6059 return omit_one_operand_loc (loc, type, integer_one_node, inner);
6061 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6062 /* MIN (X, 0) > 0 -> false
6063 MIN (X, 0) > 5 -> false */
6064 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6066 else
6067 /* MIN (X, 0) > -1 -> X > -1 */
6068 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6070 default:
6071 return NULL_TREE;
6075 /* T is an integer expression that is being multiplied, divided, or taken a
6076 modulus (CODE says which and what kind of divide or modulus) by a
6077 constant C. See if we can eliminate that operation by folding it with
6078 other operations already in T. WIDE_TYPE, if non-null, is a type that
6079 should be used for the computation if wider than our type.
6081 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6082 (X * 2) + (Y * 4). We must, however, be assured that either the original
6083 expression would not overflow or that overflow is undefined for the type
6084 in the language in question.
6086 If we return a non-null expression, it is an equivalent form of the
6087 original computation, but need not be in the original type.
6089 We set *STRICT_OVERFLOW_P to true if the return values depends on
6090 signed overflow being undefined. Otherwise we do not change
6091 *STRICT_OVERFLOW_P. */
6093 static tree
6094 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6095 bool *strict_overflow_p)
6097 /* To avoid exponential search depth, refuse to allow recursion past
6098 three levels. Beyond that (1) it's highly unlikely that we'll find
6099 something interesting and (2) we've probably processed it before
6100 when we built the inner expression. */
6102 static int depth;
6103 tree ret;
6105 if (depth > 3)
6106 return NULL;
6108 depth++;
6109 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6110 depth--;
6112 return ret;
6115 static tree
6116 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6117 bool *strict_overflow_p)
6119 tree type = TREE_TYPE (t);
6120 enum tree_code tcode = TREE_CODE (t);
6121 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6122 > GET_MODE_SIZE (TYPE_MODE (type)))
6123 ? wide_type : type);
6124 tree t1, t2;
6125 int same_p = tcode == code;
6126 tree op0 = NULL_TREE, op1 = NULL_TREE;
6127 bool sub_strict_overflow_p;
6129 /* Don't deal with constants of zero here; they confuse the code below. */
6130 if (integer_zerop (c))
6131 return NULL_TREE;
6133 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6134 op0 = TREE_OPERAND (t, 0);
6136 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6137 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6139 /* Note that we need not handle conditional operations here since fold
6140 already handles those cases. So just do arithmetic here. */
6141 switch (tcode)
6143 case INTEGER_CST:
6144 /* For a constant, we can always simplify if we are a multiply
6145 or (for divide and modulus) if it is a multiple of our constant. */
6146 if (code == MULT_EXPR
6147 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6149 tree tem = const_binop (code, fold_convert (ctype, t),
6150 fold_convert (ctype, c));
6151 /* If the multiplication overflowed, we lost information on it.
6152 See PR68142 and PR69845. */
6153 if (TREE_OVERFLOW (tem))
6154 return NULL_TREE;
6155 return tem;
6157 break;
6159 CASE_CONVERT: case NON_LVALUE_EXPR:
6160 /* If op0 is an expression ... */
6161 if ((COMPARISON_CLASS_P (op0)
6162 || UNARY_CLASS_P (op0)
6163 || BINARY_CLASS_P (op0)
6164 || VL_EXP_CLASS_P (op0)
6165 || EXPRESSION_CLASS_P (op0))
6166 /* ... and has wrapping overflow, and its type is smaller
6167 than ctype, then we cannot pass through as widening. */
6168 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6169 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6170 && (TYPE_PRECISION (ctype)
6171 > TYPE_PRECISION (TREE_TYPE (op0))))
6172 /* ... or this is a truncation (t is narrower than op0),
6173 then we cannot pass through this narrowing. */
6174 || (TYPE_PRECISION (type)
6175 < TYPE_PRECISION (TREE_TYPE (op0)))
6176 /* ... or signedness changes for division or modulus,
6177 then we cannot pass through this conversion. */
6178 || (code != MULT_EXPR
6179 && (TYPE_UNSIGNED (ctype)
6180 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6181 /* ... or has undefined overflow while the converted to
6182 type has not, we cannot do the operation in the inner type
6183 as that would introduce undefined overflow. */
6184 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6185 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6186 && !TYPE_OVERFLOW_UNDEFINED (type))))
6187 break;
6189 /* Pass the constant down and see if we can make a simplification. If
6190 we can, replace this expression with the inner simplification for
6191 possible later conversion to our or some other type. */
6192 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6193 && TREE_CODE (t2) == INTEGER_CST
6194 && !TREE_OVERFLOW (t2)
6195 && (0 != (t1 = extract_muldiv (op0, t2, code,
6196 code == MULT_EXPR
6197 ? ctype : NULL_TREE,
6198 strict_overflow_p))))
6199 return t1;
6200 break;
6202 case ABS_EXPR:
6203 /* If widening the type changes it from signed to unsigned, then we
6204 must avoid building ABS_EXPR itself as unsigned. */
6205 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6207 tree cstype = (*signed_type_for) (ctype);
6208 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6209 != 0)
6211 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6212 return fold_convert (ctype, t1);
6214 break;
6216 /* If the constant is negative, we cannot simplify this. */
6217 if (tree_int_cst_sgn (c) == -1)
6218 break;
6219 /* FALLTHROUGH */
6220 case NEGATE_EXPR:
6221 /* For division and modulus, type can't be unsigned, as e.g.
6222 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6223 For signed types, even with wrapping overflow, this is fine. */
6224 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6225 break;
6226 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6227 != 0)
6228 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6229 break;
6231 case MIN_EXPR: case MAX_EXPR:
6232 /* If widening the type changes the signedness, then we can't perform
6233 this optimization as that changes the result. */
6234 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6235 break;
6237 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6238 sub_strict_overflow_p = false;
6239 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6240 &sub_strict_overflow_p)) != 0
6241 && (t2 = extract_muldiv (op1, c, code, wide_type,
6242 &sub_strict_overflow_p)) != 0)
6244 if (tree_int_cst_sgn (c) < 0)
6245 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6246 if (sub_strict_overflow_p)
6247 *strict_overflow_p = true;
6248 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6249 fold_convert (ctype, t2));
6251 break;
6253 case LSHIFT_EXPR: case RSHIFT_EXPR:
6254 /* If the second operand is constant, this is a multiplication
6255 or floor division, by a power of two, so we can treat it that
6256 way unless the multiplier or divisor overflows. Signed
6257 left-shift overflow is implementation-defined rather than
6258 undefined in C90, so do not convert signed left shift into
6259 multiplication. */
6260 if (TREE_CODE (op1) == INTEGER_CST
6261 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6262 /* const_binop may not detect overflow correctly,
6263 so check for it explicitly here. */
6264 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6265 && 0 != (t1 = fold_convert (ctype,
6266 const_binop (LSHIFT_EXPR,
6267 size_one_node,
6268 op1)))
6269 && !TREE_OVERFLOW (t1))
6270 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6271 ? MULT_EXPR : FLOOR_DIV_EXPR,
6272 ctype,
6273 fold_convert (ctype, op0),
6274 t1),
6275 c, code, wide_type, strict_overflow_p);
6276 break;
6278 case PLUS_EXPR: case MINUS_EXPR:
6279 /* See if we can eliminate the operation on both sides. If we can, we
6280 can return a new PLUS or MINUS. If we can't, the only remaining
6281 cases where we can do anything are if the second operand is a
6282 constant. */
6283 sub_strict_overflow_p = false;
6284 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6285 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6286 if (t1 != 0 && t2 != 0
6287 && (code == MULT_EXPR
6288 /* If not multiplication, we can only do this if both operands
6289 are divisible by c. */
6290 || (multiple_of_p (ctype, op0, c)
6291 && multiple_of_p (ctype, op1, c))))
6293 if (sub_strict_overflow_p)
6294 *strict_overflow_p = true;
6295 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6296 fold_convert (ctype, t2));
6299 /* If this was a subtraction, negate OP1 and set it to be an addition.
6300 This simplifies the logic below. */
6301 if (tcode == MINUS_EXPR)
6303 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6304 /* If OP1 was not easily negatable, the constant may be OP0. */
6305 if (TREE_CODE (op0) == INTEGER_CST)
6307 std::swap (op0, op1);
6308 std::swap (t1, t2);
6312 if (TREE_CODE (op1) != INTEGER_CST)
6313 break;
6315 /* If either OP1 or C are negative, this optimization is not safe for
6316 some of the division and remainder types while for others we need
6317 to change the code. */
6318 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6320 if (code == CEIL_DIV_EXPR)
6321 code = FLOOR_DIV_EXPR;
6322 else if (code == FLOOR_DIV_EXPR)
6323 code = CEIL_DIV_EXPR;
6324 else if (code != MULT_EXPR
6325 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6326 break;
6329 /* If it's a multiply or a division/modulus operation of a multiple
6330 of our constant, do the operation and verify it doesn't overflow. */
6331 if (code == MULT_EXPR
6332 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6334 op1 = const_binop (code, fold_convert (ctype, op1),
6335 fold_convert (ctype, c));
6336 /* We allow the constant to overflow with wrapping semantics. */
6337 if (op1 == 0
6338 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6339 break;
6341 else
6342 break;
6344 /* If we have an unsigned type, we cannot widen the operation since it
6345 will change the result if the original computation overflowed. */
6346 if (TYPE_UNSIGNED (ctype) && ctype != type)
6347 break;
6349 /* If we were able to eliminate our operation from the first side,
6350 apply our operation to the second side and reform the PLUS. */
6351 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6352 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6354 /* The last case is if we are a multiply. In that case, we can
6355 apply the distributive law to commute the multiply and addition
6356 if the multiplication of the constants doesn't overflow
6357 and overflow is defined. With undefined overflow
6358 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6359 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6360 return fold_build2 (tcode, ctype,
6361 fold_build2 (code, ctype,
6362 fold_convert (ctype, op0),
6363 fold_convert (ctype, c)),
6364 op1);
6366 break;
6368 case MULT_EXPR:
6369 /* We have a special case here if we are doing something like
6370 (C * 8) % 4 since we know that's zero. */
6371 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6372 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6373 /* If the multiplication can overflow we cannot optimize this. */
6374 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6375 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6376 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6378 *strict_overflow_p = true;
6379 return omit_one_operand (type, integer_zero_node, op0);
6382 /* ... fall through ... */
6384 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6385 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6386 /* If we can extract our operation from the LHS, do so and return a
6387 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6388 do something only if the second operand is a constant. */
6389 if (same_p
6390 && (t1 = extract_muldiv (op0, c, code, wide_type,
6391 strict_overflow_p)) != 0)
6392 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6393 fold_convert (ctype, op1));
6394 else if (tcode == MULT_EXPR && code == MULT_EXPR
6395 && (t1 = extract_muldiv (op1, c, code, wide_type,
6396 strict_overflow_p)) != 0)
6397 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6398 fold_convert (ctype, t1));
6399 else if (TREE_CODE (op1) != INTEGER_CST)
6400 return 0;
6402 /* If these are the same operation types, we can associate them
6403 assuming no overflow. */
6404 if (tcode == code)
6406 bool overflow_p = false;
6407 bool overflow_mul_p;
6408 signop sign = TYPE_SIGN (ctype);
6409 unsigned prec = TYPE_PRECISION (ctype);
6410 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6411 wi::to_wide (c, prec),
6412 sign, &overflow_mul_p);
6413 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6414 if (overflow_mul_p
6415 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6416 overflow_p = true;
6417 if (!overflow_p)
6418 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6419 wide_int_to_tree (ctype, mul));
6422 /* If these operations "cancel" each other, we have the main
6423 optimizations of this pass, which occur when either constant is a
6424 multiple of the other, in which case we replace this with either an
6425 operation or CODE or TCODE.
6427 If we have an unsigned type, we cannot do this since it will change
6428 the result if the original computation overflowed. */
6429 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6430 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6431 || (tcode == MULT_EXPR
6432 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6433 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6434 && code != MULT_EXPR)))
6436 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6438 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6439 *strict_overflow_p = true;
6440 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6441 fold_convert (ctype,
6442 const_binop (TRUNC_DIV_EXPR,
6443 op1, c)));
6445 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6447 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6448 *strict_overflow_p = true;
6449 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6450 fold_convert (ctype,
6451 const_binop (TRUNC_DIV_EXPR,
6452 c, op1)));
6455 break;
6457 default:
6458 break;
6461 return 0;
6464 /* Return a node which has the indicated constant VALUE (either 0 or
6465 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6466 and is of the indicated TYPE. */
6468 tree
6469 constant_boolean_node (bool value, tree type)
6471 if (type == integer_type_node)
6472 return value ? integer_one_node : integer_zero_node;
6473 else if (type == boolean_type_node)
6474 return value ? boolean_true_node : boolean_false_node;
6475 else if (TREE_CODE (type) == VECTOR_TYPE)
6476 return build_vector_from_val (type,
6477 build_int_cst (TREE_TYPE (type),
6478 value ? -1 : 0));
6479 else
6480 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6484 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6485 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6486 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6487 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6488 COND is the first argument to CODE; otherwise (as in the example
6489 given here), it is the second argument. TYPE is the type of the
6490 original expression. Return NULL_TREE if no simplification is
6491 possible. */
6493 static tree
6494 fold_binary_op_with_conditional_arg (location_t loc,
6495 enum tree_code code,
6496 tree type, tree op0, tree op1,
6497 tree cond, tree arg, int cond_first_p)
6499 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6500 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6501 tree test, true_value, false_value;
6502 tree lhs = NULL_TREE;
6503 tree rhs = NULL_TREE;
6504 enum tree_code cond_code = COND_EXPR;
6506 if (TREE_CODE (cond) == COND_EXPR
6507 || TREE_CODE (cond) == VEC_COND_EXPR)
6509 test = TREE_OPERAND (cond, 0);
6510 true_value = TREE_OPERAND (cond, 1);
6511 false_value = TREE_OPERAND (cond, 2);
6512 /* If this operand throws an expression, then it does not make
6513 sense to try to perform a logical or arithmetic operation
6514 involving it. */
6515 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6516 lhs = true_value;
6517 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6518 rhs = false_value;
6520 else if (!(TREE_CODE (type) != VECTOR_TYPE
6521 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6523 tree testtype = TREE_TYPE (cond);
6524 test = cond;
6525 true_value = constant_boolean_node (true, testtype);
6526 false_value = constant_boolean_node (false, testtype);
6528 else
6529 /* Detect the case of mixing vector and scalar types - bail out. */
6530 return NULL_TREE;
6532 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6533 cond_code = VEC_COND_EXPR;
6535 /* This transformation is only worthwhile if we don't have to wrap ARG
6536 in a SAVE_EXPR and the operation can be simplified without recursing
6537 on at least one of the branches once its pushed inside the COND_EXPR. */
6538 if (!TREE_CONSTANT (arg)
6539 && (TREE_SIDE_EFFECTS (arg)
6540 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6541 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6542 return NULL_TREE;
6544 arg = fold_convert_loc (loc, arg_type, arg);
6545 if (lhs == 0)
6547 true_value = fold_convert_loc (loc, cond_type, true_value);
6548 if (cond_first_p)
6549 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6550 else
6551 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6553 if (rhs == 0)
6555 false_value = fold_convert_loc (loc, cond_type, false_value);
6556 if (cond_first_p)
6557 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6558 else
6559 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6562 /* Check that we have simplified at least one of the branches. */
6563 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6564 return NULL_TREE;
6566 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6570 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6572 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6573 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6574 ADDEND is the same as X.
6576 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6577 and finite. The problematic cases are when X is zero, and its mode
6578 has signed zeros. In the case of rounding towards -infinity,
6579 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6580 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6582 bool
6583 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6585 if (!real_zerop (addend))
6586 return false;
6588 /* Don't allow the fold with -fsignaling-nans. */
6589 if (HONOR_SNANS (element_mode (type)))
6590 return false;
6592 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6593 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6594 return true;
6596 /* In a vector or complex, we would need to check the sign of all zeros. */
6597 if (TREE_CODE (addend) != REAL_CST)
6598 return false;
6600 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6601 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6602 negate = !negate;
6604 /* The mode has signed zeros, and we have to honor their sign.
6605 In this situation, there is only one case we can return true for.
6606 X - 0 is the same as X unless rounding towards -infinity is
6607 supported. */
6608 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6611 /* Subroutine of fold() that optimizes comparisons of a division by
6612 a nonzero integer constant against an integer constant, i.e.
6613 X/C1 op C2.
6615 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6616 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6617 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6619 The function returns the constant folded tree if a simplification
6620 can be made, and NULL_TREE otherwise. */
6622 static tree
6623 fold_div_compare (location_t loc,
6624 enum tree_code code, tree type, tree arg0, tree arg1)
6626 tree prod, tmp, hi, lo;
6627 tree arg00 = TREE_OPERAND (arg0, 0);
6628 tree arg01 = TREE_OPERAND (arg0, 1);
6629 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6630 bool neg_overflow = false;
6631 bool overflow;
6633 /* We have to do this the hard way to detect unsigned overflow.
6634 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6635 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6636 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6637 neg_overflow = false;
6639 if (sign == UNSIGNED)
6641 tmp = int_const_binop (MINUS_EXPR, arg01,
6642 build_int_cst (TREE_TYPE (arg01), 1));
6643 lo = prod;
6645 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6646 val = wi::add (prod, tmp, sign, &overflow);
6647 hi = force_fit_type (TREE_TYPE (arg00), val,
6648 -1, overflow | TREE_OVERFLOW (prod));
6650 else if (tree_int_cst_sgn (arg01) >= 0)
6652 tmp = int_const_binop (MINUS_EXPR, arg01,
6653 build_int_cst (TREE_TYPE (arg01), 1));
6654 switch (tree_int_cst_sgn (arg1))
6656 case -1:
6657 neg_overflow = true;
6658 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6659 hi = prod;
6660 break;
6662 case 0:
6663 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6664 hi = tmp;
6665 break;
6667 case 1:
6668 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6669 lo = prod;
6670 break;
6672 default:
6673 gcc_unreachable ();
6676 else
6678 /* A negative divisor reverses the relational operators. */
6679 code = swap_tree_comparison (code);
6681 tmp = int_const_binop (PLUS_EXPR, arg01,
6682 build_int_cst (TREE_TYPE (arg01), 1));
6683 switch (tree_int_cst_sgn (arg1))
6685 case -1:
6686 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6687 lo = prod;
6688 break;
6690 case 0:
6691 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6692 lo = tmp;
6693 break;
6695 case 1:
6696 neg_overflow = true;
6697 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6698 hi = prod;
6699 break;
6701 default:
6702 gcc_unreachable ();
6706 switch (code)
6708 case EQ_EXPR:
6709 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6710 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6711 if (TREE_OVERFLOW (hi))
6712 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6713 if (TREE_OVERFLOW (lo))
6714 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6715 return build_range_check (loc, type, arg00, 1, lo, hi);
6717 case NE_EXPR:
6718 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6719 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6720 if (TREE_OVERFLOW (hi))
6721 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6722 if (TREE_OVERFLOW (lo))
6723 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6724 return build_range_check (loc, type, arg00, 0, lo, hi);
6726 case LT_EXPR:
6727 if (TREE_OVERFLOW (lo))
6729 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6730 return omit_one_operand_loc (loc, type, tmp, arg00);
6732 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6734 case LE_EXPR:
6735 if (TREE_OVERFLOW (hi))
6737 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6738 return omit_one_operand_loc (loc, type, tmp, arg00);
6740 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6742 case GT_EXPR:
6743 if (TREE_OVERFLOW (hi))
6745 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6746 return omit_one_operand_loc (loc, type, tmp, arg00);
6748 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6750 case GE_EXPR:
6751 if (TREE_OVERFLOW (lo))
6753 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6754 return omit_one_operand_loc (loc, type, tmp, arg00);
6756 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6758 default:
6759 break;
6762 return NULL_TREE;
6766 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6767 equality/inequality test, then return a simplified form of the test
6768 using a sign testing. Otherwise return NULL. TYPE is the desired
6769 result type. */
6771 static tree
6772 fold_single_bit_test_into_sign_test (location_t loc,
6773 enum tree_code code, tree arg0, tree arg1,
6774 tree result_type)
6776 /* If this is testing a single bit, we can optimize the test. */
6777 if ((code == NE_EXPR || code == EQ_EXPR)
6778 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6779 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6781 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6782 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6783 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6785 if (arg00 != NULL_TREE
6786 /* This is only a win if casting to a signed type is cheap,
6787 i.e. when arg00's type is not a partial mode. */
6788 && TYPE_PRECISION (TREE_TYPE (arg00))
6789 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6791 tree stype = signed_type_for (TREE_TYPE (arg00));
6792 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6793 result_type,
6794 fold_convert_loc (loc, stype, arg00),
6795 build_int_cst (stype, 0));
6799 return NULL_TREE;
6802 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6803 equality/inequality test, then return a simplified form of
6804 the test using shifts and logical operations. Otherwise return
6805 NULL. TYPE is the desired result type. */
6807 tree
6808 fold_single_bit_test (location_t loc, enum tree_code code,
6809 tree arg0, tree arg1, tree result_type)
6811 /* If this is testing a single bit, we can optimize the test. */
6812 if ((code == NE_EXPR || code == EQ_EXPR)
6813 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6814 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6816 tree inner = TREE_OPERAND (arg0, 0);
6817 tree type = TREE_TYPE (arg0);
6818 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6819 machine_mode operand_mode = TYPE_MODE (type);
6820 int ops_unsigned;
6821 tree signed_type, unsigned_type, intermediate_type;
6822 tree tem, one;
6824 /* First, see if we can fold the single bit test into a sign-bit
6825 test. */
6826 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6827 result_type);
6828 if (tem)
6829 return tem;
6831 /* Otherwise we have (A & C) != 0 where C is a single bit,
6832 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6833 Similarly for (A & C) == 0. */
6835 /* If INNER is a right shift of a constant and it plus BITNUM does
6836 not overflow, adjust BITNUM and INNER. */
6837 if (TREE_CODE (inner) == RSHIFT_EXPR
6838 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6839 && bitnum < TYPE_PRECISION (type)
6840 && wi::ltu_p (TREE_OPERAND (inner, 1),
6841 TYPE_PRECISION (type) - bitnum))
6843 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6844 inner = TREE_OPERAND (inner, 0);
6847 /* If we are going to be able to omit the AND below, we must do our
6848 operations as unsigned. If we must use the AND, we have a choice.
6849 Normally unsigned is faster, but for some machines signed is. */
6850 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6851 && !flag_syntax_only) ? 0 : 1;
6853 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6854 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6855 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6856 inner = fold_convert_loc (loc, intermediate_type, inner);
6858 if (bitnum != 0)
6859 inner = build2 (RSHIFT_EXPR, intermediate_type,
6860 inner, size_int (bitnum));
6862 one = build_int_cst (intermediate_type, 1);
6864 if (code == EQ_EXPR)
6865 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6867 /* Put the AND last so it can combine with more things. */
6868 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6870 /* Make sure to return the proper type. */
6871 inner = fold_convert_loc (loc, result_type, inner);
6873 return inner;
6875 return NULL_TREE;
6878 /* Check whether we are allowed to reorder operands arg0 and arg1,
6879 such that the evaluation of arg1 occurs before arg0. */
6881 static bool
6882 reorder_operands_p (const_tree arg0, const_tree arg1)
6884 if (! flag_evaluation_order)
6885 return true;
6886 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6887 return true;
6888 return ! TREE_SIDE_EFFECTS (arg0)
6889 && ! TREE_SIDE_EFFECTS (arg1);
6892 /* Test whether it is preferable two swap two operands, ARG0 and
6893 ARG1, for example because ARG0 is an integer constant and ARG1
6894 isn't. If REORDER is true, only recommend swapping if we can
6895 evaluate the operands in reverse order. */
6897 bool
6898 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6900 if (CONSTANT_CLASS_P (arg1))
6901 return 0;
6902 if (CONSTANT_CLASS_P (arg0))
6903 return 1;
6905 STRIP_NOPS (arg0);
6906 STRIP_NOPS (arg1);
6908 if (TREE_CONSTANT (arg1))
6909 return 0;
6910 if (TREE_CONSTANT (arg0))
6911 return 1;
6913 if (reorder && flag_evaluation_order
6914 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6915 return 0;
6917 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6918 for commutative and comparison operators. Ensuring a canonical
6919 form allows the optimizers to find additional redundancies without
6920 having to explicitly check for both orderings. */
6921 if (TREE_CODE (arg0) == SSA_NAME
6922 && TREE_CODE (arg1) == SSA_NAME
6923 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6924 return 1;
6926 /* Put SSA_NAMEs last. */
6927 if (TREE_CODE (arg1) == SSA_NAME)
6928 return 0;
6929 if (TREE_CODE (arg0) == SSA_NAME)
6930 return 1;
6932 /* Put variables last. */
6933 if (DECL_P (arg1))
6934 return 0;
6935 if (DECL_P (arg0))
6936 return 1;
6938 return 0;
6942 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6943 means A >= Y && A != MAX, but in this case we know that
6944 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6946 static tree
6947 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6949 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6951 if (TREE_CODE (bound) == LT_EXPR)
6952 a = TREE_OPERAND (bound, 0);
6953 else if (TREE_CODE (bound) == GT_EXPR)
6954 a = TREE_OPERAND (bound, 1);
6955 else
6956 return NULL_TREE;
6958 typea = TREE_TYPE (a);
6959 if (!INTEGRAL_TYPE_P (typea)
6960 && !POINTER_TYPE_P (typea))
6961 return NULL_TREE;
6963 if (TREE_CODE (ineq) == LT_EXPR)
6965 a1 = TREE_OPERAND (ineq, 1);
6966 y = TREE_OPERAND (ineq, 0);
6968 else if (TREE_CODE (ineq) == GT_EXPR)
6970 a1 = TREE_OPERAND (ineq, 0);
6971 y = TREE_OPERAND (ineq, 1);
6973 else
6974 return NULL_TREE;
6976 if (TREE_TYPE (a1) != typea)
6977 return NULL_TREE;
6979 if (POINTER_TYPE_P (typea))
6981 /* Convert the pointer types into integer before taking the difference. */
6982 tree ta = fold_convert_loc (loc, ssizetype, a);
6983 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6984 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6986 else
6987 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6989 if (!diff || !integer_onep (diff))
6990 return NULL_TREE;
6992 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6995 /* Fold a sum or difference of at least one multiplication.
6996 Returns the folded tree or NULL if no simplification could be made. */
6998 static tree
6999 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7000 tree arg0, tree arg1)
7002 tree arg00, arg01, arg10, arg11;
7003 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7005 /* (A * C) +- (B * C) -> (A+-B) * C.
7006 (A * C) +- A -> A * (C+-1).
7007 We are most concerned about the case where C is a constant,
7008 but other combinations show up during loop reduction. Since
7009 it is not difficult, try all four possibilities. */
7011 if (TREE_CODE (arg0) == MULT_EXPR)
7013 arg00 = TREE_OPERAND (arg0, 0);
7014 arg01 = TREE_OPERAND (arg0, 1);
7016 else if (TREE_CODE (arg0) == INTEGER_CST)
7018 arg00 = build_one_cst (type);
7019 arg01 = arg0;
7021 else
7023 /* We cannot generate constant 1 for fract. */
7024 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7025 return NULL_TREE;
7026 arg00 = arg0;
7027 arg01 = build_one_cst (type);
7029 if (TREE_CODE (arg1) == MULT_EXPR)
7031 arg10 = TREE_OPERAND (arg1, 0);
7032 arg11 = TREE_OPERAND (arg1, 1);
7034 else if (TREE_CODE (arg1) == INTEGER_CST)
7036 arg10 = build_one_cst (type);
7037 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7038 the purpose of this canonicalization. */
7039 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
7040 && negate_expr_p (arg1)
7041 && code == PLUS_EXPR)
7043 arg11 = negate_expr (arg1);
7044 code = MINUS_EXPR;
7046 else
7047 arg11 = arg1;
7049 else
7051 /* We cannot generate constant 1 for fract. */
7052 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7053 return NULL_TREE;
7054 arg10 = arg1;
7055 arg11 = build_one_cst (type);
7057 same = NULL_TREE;
7059 if (operand_equal_p (arg01, arg11, 0))
7060 same = arg01, alt0 = arg00, alt1 = arg10;
7061 else if (operand_equal_p (arg00, arg10, 0))
7062 same = arg00, alt0 = arg01, alt1 = arg11;
7063 else if (operand_equal_p (arg00, arg11, 0))
7064 same = arg00, alt0 = arg01, alt1 = arg10;
7065 else if (operand_equal_p (arg01, arg10, 0))
7066 same = arg01, alt0 = arg00, alt1 = arg11;
7068 /* No identical multiplicands; see if we can find a common
7069 power-of-two factor in non-power-of-two multiplies. This
7070 can help in multi-dimensional array access. */
7071 else if (tree_fits_shwi_p (arg01)
7072 && tree_fits_shwi_p (arg11))
7074 HOST_WIDE_INT int01, int11, tmp;
7075 bool swap = false;
7076 tree maybe_same;
7077 int01 = tree_to_shwi (arg01);
7078 int11 = tree_to_shwi (arg11);
7080 /* Move min of absolute values to int11. */
7081 if (absu_hwi (int01) < absu_hwi (int11))
7083 tmp = int01, int01 = int11, int11 = tmp;
7084 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7085 maybe_same = arg01;
7086 swap = true;
7088 else
7089 maybe_same = arg11;
7091 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7092 /* The remainder should not be a constant, otherwise we
7093 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7094 increased the number of multiplications necessary. */
7095 && TREE_CODE (arg10) != INTEGER_CST)
7097 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7098 build_int_cst (TREE_TYPE (arg00),
7099 int01 / int11));
7100 alt1 = arg10;
7101 same = maybe_same;
7102 if (swap)
7103 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7107 if (same)
7108 return fold_build2_loc (loc, MULT_EXPR, type,
7109 fold_build2_loc (loc, code, type,
7110 fold_convert_loc (loc, type, alt0),
7111 fold_convert_loc (loc, type, alt1)),
7112 fold_convert_loc (loc, type, same));
7114 return NULL_TREE;
7117 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7118 specified by EXPR into the buffer PTR of length LEN bytes.
7119 Return the number of bytes placed in the buffer, or zero
7120 upon failure. */
7122 static int
7123 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7125 tree type = TREE_TYPE (expr);
7126 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7127 int byte, offset, word, words;
7128 unsigned char value;
7130 if ((off == -1 && total_bytes > len)
7131 || off >= total_bytes)
7132 return 0;
7133 if (off == -1)
7134 off = 0;
7135 words = total_bytes / UNITS_PER_WORD;
7137 for (byte = 0; byte < total_bytes; byte++)
7139 int bitpos = byte * BITS_PER_UNIT;
7140 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7141 number of bytes. */
7142 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7144 if (total_bytes > UNITS_PER_WORD)
7146 word = byte / UNITS_PER_WORD;
7147 if (WORDS_BIG_ENDIAN)
7148 word = (words - 1) - word;
7149 offset = word * UNITS_PER_WORD;
7150 if (BYTES_BIG_ENDIAN)
7151 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7152 else
7153 offset += byte % UNITS_PER_WORD;
7155 else
7156 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7157 if (offset >= off
7158 && offset - off < len)
7159 ptr[offset - off] = value;
7161 return MIN (len, total_bytes - off);
7165 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7166 specified by EXPR into the buffer PTR of length LEN bytes.
7167 Return the number of bytes placed in the buffer, or zero
7168 upon failure. */
7170 static int
7171 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7173 tree type = TREE_TYPE (expr);
7174 machine_mode mode = TYPE_MODE (type);
7175 int total_bytes = GET_MODE_SIZE (mode);
7176 FIXED_VALUE_TYPE value;
7177 tree i_value, i_type;
7179 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7180 return 0;
7182 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7184 if (NULL_TREE == i_type
7185 || TYPE_PRECISION (i_type) != total_bytes)
7186 return 0;
7188 value = TREE_FIXED_CST (expr);
7189 i_value = double_int_to_tree (i_type, value.data);
7191 return native_encode_int (i_value, ptr, len, off);
7195 /* Subroutine of native_encode_expr. Encode the REAL_CST
7196 specified by EXPR into the buffer PTR of length LEN bytes.
7197 Return the number of bytes placed in the buffer, or zero
7198 upon failure. */
7200 static int
7201 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7203 tree type = TREE_TYPE (expr);
7204 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7205 int byte, offset, word, words, bitpos;
7206 unsigned char value;
7208 /* There are always 32 bits in each long, no matter the size of
7209 the hosts long. We handle floating point representations with
7210 up to 192 bits. */
7211 long tmp[6];
7213 if ((off == -1 && total_bytes > len)
7214 || off >= total_bytes)
7215 return 0;
7216 if (off == -1)
7217 off = 0;
7218 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7220 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7222 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7223 bitpos += BITS_PER_UNIT)
7225 byte = (bitpos / BITS_PER_UNIT) & 3;
7226 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7228 if (UNITS_PER_WORD < 4)
7230 word = byte / UNITS_PER_WORD;
7231 if (WORDS_BIG_ENDIAN)
7232 word = (words - 1) - word;
7233 offset = word * UNITS_PER_WORD;
7234 if (BYTES_BIG_ENDIAN)
7235 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7236 else
7237 offset += byte % UNITS_PER_WORD;
7239 else
7240 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7241 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7242 if (offset >= off
7243 && offset - off < len)
7244 ptr[offset - off] = value;
7246 return MIN (len, total_bytes - off);
7249 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7250 specified by EXPR into the buffer PTR of length LEN bytes.
7251 Return the number of bytes placed in the buffer, or zero
7252 upon failure. */
7254 static int
7255 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7257 int rsize, isize;
7258 tree part;
7260 part = TREE_REALPART (expr);
7261 rsize = native_encode_expr (part, ptr, len, off);
7262 if (off == -1
7263 && rsize == 0)
7264 return 0;
7265 part = TREE_IMAGPART (expr);
7266 if (off != -1)
7267 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7268 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7269 if (off == -1
7270 && isize != rsize)
7271 return 0;
7272 return rsize + isize;
7276 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7277 specified by EXPR into the buffer PTR of length LEN bytes.
7278 Return the number of bytes placed in the buffer, or zero
7279 upon failure. */
7281 static int
7282 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7284 unsigned i, count;
7285 int size, offset;
7286 tree itype, elem;
7288 offset = 0;
7289 count = VECTOR_CST_NELTS (expr);
7290 itype = TREE_TYPE (TREE_TYPE (expr));
7291 size = GET_MODE_SIZE (TYPE_MODE (itype));
7292 for (i = 0; i < count; i++)
7294 if (off >= size)
7296 off -= size;
7297 continue;
7299 elem = VECTOR_CST_ELT (expr, i);
7300 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7301 if ((off == -1 && res != size)
7302 || res == 0)
7303 return 0;
7304 offset += res;
7305 if (offset >= len)
7306 return offset;
7307 if (off != -1)
7308 off = 0;
7310 return offset;
7314 /* Subroutine of native_encode_expr. Encode the STRING_CST
7315 specified by EXPR into the buffer PTR of length LEN bytes.
7316 Return the number of bytes placed in the buffer, or zero
7317 upon failure. */
7319 static int
7320 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7322 tree type = TREE_TYPE (expr);
7323 HOST_WIDE_INT total_bytes;
7325 if (TREE_CODE (type) != ARRAY_TYPE
7326 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7327 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7328 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7329 return 0;
7330 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7331 if ((off == -1 && total_bytes > len)
7332 || off >= total_bytes)
7333 return 0;
7334 if (off == -1)
7335 off = 0;
7336 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7338 int written = 0;
7339 if (off < TREE_STRING_LENGTH (expr))
7341 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7342 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7344 memset (ptr + written, 0,
7345 MIN (total_bytes - written, len - written));
7347 else
7348 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7349 return MIN (total_bytes - off, len);
7353 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7354 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7355 buffer PTR of length LEN bytes. If OFF is not -1 then start
7356 the encoding at byte offset OFF and encode at most LEN bytes.
7357 Return the number of bytes placed in the buffer, or zero upon failure. */
7360 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7362 /* We don't support starting at negative offset and -1 is special. */
7363 if (off < -1)
7364 return 0;
7366 switch (TREE_CODE (expr))
7368 case INTEGER_CST:
7369 return native_encode_int (expr, ptr, len, off);
7371 case REAL_CST:
7372 return native_encode_real (expr, ptr, len, off);
7374 case FIXED_CST:
7375 return native_encode_fixed (expr, ptr, len, off);
7377 case COMPLEX_CST:
7378 return native_encode_complex (expr, ptr, len, off);
7380 case VECTOR_CST:
7381 return native_encode_vector (expr, ptr, len, off);
7383 case STRING_CST:
7384 return native_encode_string (expr, ptr, len, off);
7386 default:
7387 return 0;
7392 /* Subroutine of native_interpret_expr. Interpret the contents of
7393 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7394 If the buffer cannot be interpreted, return NULL_TREE. */
7396 static tree
7397 native_interpret_int (tree type, const unsigned char *ptr, int len)
7399 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7401 if (total_bytes > len
7402 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7403 return NULL_TREE;
7405 wide_int result = wi::from_buffer (ptr, total_bytes);
7407 return wide_int_to_tree (type, result);
7411 /* Subroutine of native_interpret_expr. Interpret the contents of
7412 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7413 If the buffer cannot be interpreted, return NULL_TREE. */
7415 static tree
7416 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7418 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7419 double_int result;
7420 FIXED_VALUE_TYPE fixed_value;
7422 if (total_bytes > len
7423 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7424 return NULL_TREE;
7426 result = double_int::from_buffer (ptr, total_bytes);
7427 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7429 return build_fixed (type, fixed_value);
7433 /* Subroutine of native_interpret_expr. Interpret the contents of
7434 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7435 If the buffer cannot be interpreted, return NULL_TREE. */
7437 static tree
7438 native_interpret_real (tree type, const unsigned char *ptr, int len)
7440 machine_mode mode = TYPE_MODE (type);
7441 int total_bytes = GET_MODE_SIZE (mode);
7442 unsigned char value;
7443 /* There are always 32 bits in each long, no matter the size of
7444 the hosts long. We handle floating point representations with
7445 up to 192 bits. */
7446 REAL_VALUE_TYPE r;
7447 long tmp[6];
7449 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7450 if (total_bytes > len || total_bytes > 24)
7451 return NULL_TREE;
7452 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7454 memset (tmp, 0, sizeof (tmp));
7455 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7456 bitpos += BITS_PER_UNIT)
7458 /* Both OFFSET and BYTE index within a long;
7459 bitpos indexes the whole float. */
7460 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7461 if (UNITS_PER_WORD < 4)
7463 int word = byte / UNITS_PER_WORD;
7464 if (WORDS_BIG_ENDIAN)
7465 word = (words - 1) - word;
7466 offset = word * UNITS_PER_WORD;
7467 if (BYTES_BIG_ENDIAN)
7468 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7469 else
7470 offset += byte % UNITS_PER_WORD;
7472 else
7474 offset = byte;
7475 if (BYTES_BIG_ENDIAN)
7477 /* Reverse bytes within each long, or within the entire float
7478 if it's smaller than a long (for HFmode). */
7479 offset = MIN (3, total_bytes - 1) - offset;
7480 gcc_assert (offset >= 0);
7483 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7485 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7488 real_from_target (&r, tmp, mode);
7489 return build_real (type, r);
7493 /* Subroutine of native_interpret_expr. Interpret the contents of
7494 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7495 If the buffer cannot be interpreted, return NULL_TREE. */
7497 static tree
7498 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7500 tree etype, rpart, ipart;
7501 int size;
7503 etype = TREE_TYPE (type);
7504 size = GET_MODE_SIZE (TYPE_MODE (etype));
7505 if (size * 2 > len)
7506 return NULL_TREE;
7507 rpart = native_interpret_expr (etype, ptr, size);
7508 if (!rpart)
7509 return NULL_TREE;
7510 ipart = native_interpret_expr (etype, ptr+size, size);
7511 if (!ipart)
7512 return NULL_TREE;
7513 return build_complex (type, rpart, ipart);
7517 /* Subroutine of native_interpret_expr. Interpret the contents of
7518 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7519 If the buffer cannot be interpreted, return NULL_TREE. */
7521 static tree
7522 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7524 tree etype, elem;
7525 int i, size, count;
7526 tree *elements;
7528 etype = TREE_TYPE (type);
7529 size = GET_MODE_SIZE (TYPE_MODE (etype));
7530 count = TYPE_VECTOR_SUBPARTS (type);
7531 if (size * count > len)
7532 return NULL_TREE;
7534 elements = XALLOCAVEC (tree, count);
7535 for (i = count - 1; i >= 0; i--)
7537 elem = native_interpret_expr (etype, ptr+(i*size), size);
7538 if (!elem)
7539 return NULL_TREE;
7540 elements[i] = elem;
7542 return build_vector (type, elements);
7546 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7547 the buffer PTR of length LEN as a constant of type TYPE. For
7548 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7549 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7550 return NULL_TREE. */
7552 tree
7553 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7555 switch (TREE_CODE (type))
7557 case INTEGER_TYPE:
7558 case ENUMERAL_TYPE:
7559 case BOOLEAN_TYPE:
7560 case POINTER_TYPE:
7561 case REFERENCE_TYPE:
7562 return native_interpret_int (type, ptr, len);
7564 case REAL_TYPE:
7565 return native_interpret_real (type, ptr, len);
7567 case FIXED_POINT_TYPE:
7568 return native_interpret_fixed (type, ptr, len);
7570 case COMPLEX_TYPE:
7571 return native_interpret_complex (type, ptr, len);
7573 case VECTOR_TYPE:
7574 return native_interpret_vector (type, ptr, len);
7576 default:
7577 return NULL_TREE;
7581 /* Returns true if we can interpret the contents of a native encoding
7582 as TYPE. */
7584 static bool
7585 can_native_interpret_type_p (tree type)
7587 switch (TREE_CODE (type))
7589 case INTEGER_TYPE:
7590 case ENUMERAL_TYPE:
7591 case BOOLEAN_TYPE:
7592 case POINTER_TYPE:
7593 case REFERENCE_TYPE:
7594 case FIXED_POINT_TYPE:
7595 case REAL_TYPE:
7596 case COMPLEX_TYPE:
7597 case VECTOR_TYPE:
7598 return true;
7599 default:
7600 return false;
7604 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7605 TYPE at compile-time. If we're unable to perform the conversion
7606 return NULL_TREE. */
7608 static tree
7609 fold_view_convert_expr (tree type, tree expr)
7611 /* We support up to 512-bit values (for V8DFmode). */
7612 unsigned char buffer[64];
7613 int len;
7615 /* Check that the host and target are sane. */
7616 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7617 return NULL_TREE;
7619 len = native_encode_expr (expr, buffer, sizeof (buffer));
7620 if (len == 0)
7621 return NULL_TREE;
7623 return native_interpret_expr (type, buffer, len);
7626 /* Build an expression for the address of T. Folds away INDIRECT_REF
7627 to avoid confusing the gimplify process. */
7629 tree
7630 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7632 /* The size of the object is not relevant when talking about its address. */
7633 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7634 t = TREE_OPERAND (t, 0);
7636 if (TREE_CODE (t) == INDIRECT_REF)
7638 t = TREE_OPERAND (t, 0);
7640 if (TREE_TYPE (t) != ptrtype)
7641 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7643 else if (TREE_CODE (t) == MEM_REF
7644 && integer_zerop (TREE_OPERAND (t, 1)))
7645 return TREE_OPERAND (t, 0);
7646 else if (TREE_CODE (t) == MEM_REF
7647 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7648 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7649 TREE_OPERAND (t, 0),
7650 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7651 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7653 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7655 if (TREE_TYPE (t) != ptrtype)
7656 t = fold_convert_loc (loc, ptrtype, t);
7658 else
7659 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7661 return t;
7664 /* Build an expression for the address of T. */
7666 tree
7667 build_fold_addr_expr_loc (location_t loc, tree t)
7669 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7671 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7674 /* Fold a unary expression of code CODE and type TYPE with operand
7675 OP0. Return the folded expression if folding is successful.
7676 Otherwise, return NULL_TREE. */
7678 tree
7679 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7681 tree tem;
7682 tree arg0;
7683 enum tree_code_class kind = TREE_CODE_CLASS (code);
7685 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7686 && TREE_CODE_LENGTH (code) == 1);
7688 arg0 = op0;
7689 if (arg0)
7691 if (CONVERT_EXPR_CODE_P (code)
7692 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7694 /* Don't use STRIP_NOPS, because signedness of argument type
7695 matters. */
7696 STRIP_SIGN_NOPS (arg0);
7698 else
7700 /* Strip any conversions that don't change the mode. This
7701 is safe for every expression, except for a comparison
7702 expression because its signedness is derived from its
7703 operands.
7705 Note that this is done as an internal manipulation within
7706 the constant folder, in order to find the simplest
7707 representation of the arguments so that their form can be
7708 studied. In any cases, the appropriate type conversions
7709 should be put back in the tree that will get out of the
7710 constant folder. */
7711 STRIP_NOPS (arg0);
7714 if (CONSTANT_CLASS_P (arg0))
7716 tree tem = const_unop (code, type, arg0);
7717 if (tem)
7719 if (TREE_TYPE (tem) != type)
7720 tem = fold_convert_loc (loc, type, tem);
7721 return tem;
7726 tem = generic_simplify (loc, code, type, op0);
7727 if (tem)
7728 return tem;
7730 if (TREE_CODE_CLASS (code) == tcc_unary)
7732 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7733 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7734 fold_build1_loc (loc, code, type,
7735 fold_convert_loc (loc, TREE_TYPE (op0),
7736 TREE_OPERAND (arg0, 1))));
7737 else if (TREE_CODE (arg0) == COND_EXPR)
7739 tree arg01 = TREE_OPERAND (arg0, 1);
7740 tree arg02 = TREE_OPERAND (arg0, 2);
7741 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7742 arg01 = fold_build1_loc (loc, code, type,
7743 fold_convert_loc (loc,
7744 TREE_TYPE (op0), arg01));
7745 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7746 arg02 = fold_build1_loc (loc, code, type,
7747 fold_convert_loc (loc,
7748 TREE_TYPE (op0), arg02));
7749 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7750 arg01, arg02);
7752 /* If this was a conversion, and all we did was to move into
7753 inside the COND_EXPR, bring it back out. But leave it if
7754 it is a conversion from integer to integer and the
7755 result precision is no wider than a word since such a
7756 conversion is cheap and may be optimized away by combine,
7757 while it couldn't if it were outside the COND_EXPR. Then return
7758 so we don't get into an infinite recursion loop taking the
7759 conversion out and then back in. */
7761 if ((CONVERT_EXPR_CODE_P (code)
7762 || code == NON_LVALUE_EXPR)
7763 && TREE_CODE (tem) == COND_EXPR
7764 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7765 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7766 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7767 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7768 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7769 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7770 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7771 && (INTEGRAL_TYPE_P
7772 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7773 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7774 || flag_syntax_only))
7775 tem = build1_loc (loc, code, type,
7776 build3 (COND_EXPR,
7777 TREE_TYPE (TREE_OPERAND
7778 (TREE_OPERAND (tem, 1), 0)),
7779 TREE_OPERAND (tem, 0),
7780 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7781 TREE_OPERAND (TREE_OPERAND (tem, 2),
7782 0)));
7783 return tem;
7787 switch (code)
7789 case NON_LVALUE_EXPR:
7790 if (!maybe_lvalue_p (op0))
7791 return fold_convert_loc (loc, type, op0);
7792 return NULL_TREE;
7794 CASE_CONVERT:
7795 case FLOAT_EXPR:
7796 case FIX_TRUNC_EXPR:
7797 if (COMPARISON_CLASS_P (op0))
7799 /* If we have (type) (a CMP b) and type is an integral type, return
7800 new expression involving the new type. Canonicalize
7801 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7802 non-integral type.
7803 Do not fold the result as that would not simplify further, also
7804 folding again results in recursions. */
7805 if (TREE_CODE (type) == BOOLEAN_TYPE)
7806 return build2_loc (loc, TREE_CODE (op0), type,
7807 TREE_OPERAND (op0, 0),
7808 TREE_OPERAND (op0, 1));
7809 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7810 && TREE_CODE (type) != VECTOR_TYPE)
7811 return build3_loc (loc, COND_EXPR, type, op0,
7812 constant_boolean_node (true, type),
7813 constant_boolean_node (false, type));
7816 /* Handle (T *)&A.B.C for A being of type T and B and C
7817 living at offset zero. This occurs frequently in
7818 C++ upcasting and then accessing the base. */
7819 if (TREE_CODE (op0) == ADDR_EXPR
7820 && POINTER_TYPE_P (type)
7821 && handled_component_p (TREE_OPERAND (op0, 0)))
7823 HOST_WIDE_INT bitsize, bitpos;
7824 tree offset;
7825 machine_mode mode;
7826 int unsignedp, reversep, volatilep;
7827 tree base
7828 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7829 &offset, &mode, &unsignedp, &reversep,
7830 &volatilep, false);
7831 /* If the reference was to a (constant) zero offset, we can use
7832 the address of the base if it has the same base type
7833 as the result type and the pointer type is unqualified. */
7834 if (! offset && bitpos == 0
7835 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7836 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7837 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7838 return fold_convert_loc (loc, type,
7839 build_fold_addr_expr_loc (loc, base));
7842 if (TREE_CODE (op0) == MODIFY_EXPR
7843 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7844 /* Detect assigning a bitfield. */
7845 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7846 && DECL_BIT_FIELD
7847 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7849 /* Don't leave an assignment inside a conversion
7850 unless assigning a bitfield. */
7851 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7852 /* First do the assignment, then return converted constant. */
7853 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7854 TREE_NO_WARNING (tem) = 1;
7855 TREE_USED (tem) = 1;
7856 return tem;
7859 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7860 constants (if x has signed type, the sign bit cannot be set
7861 in c). This folds extension into the BIT_AND_EXPR.
7862 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7863 very likely don't have maximal range for their precision and this
7864 transformation effectively doesn't preserve non-maximal ranges. */
7865 if (TREE_CODE (type) == INTEGER_TYPE
7866 && TREE_CODE (op0) == BIT_AND_EXPR
7867 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7869 tree and_expr = op0;
7870 tree and0 = TREE_OPERAND (and_expr, 0);
7871 tree and1 = TREE_OPERAND (and_expr, 1);
7872 int change = 0;
7874 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7875 || (TYPE_PRECISION (type)
7876 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7877 change = 1;
7878 else if (TYPE_PRECISION (TREE_TYPE (and1))
7879 <= HOST_BITS_PER_WIDE_INT
7880 && tree_fits_uhwi_p (and1))
7882 unsigned HOST_WIDE_INT cst;
7884 cst = tree_to_uhwi (and1);
7885 cst &= HOST_WIDE_INT_M1U
7886 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7887 change = (cst == 0);
7888 if (change
7889 && !flag_syntax_only
7890 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7891 == ZERO_EXTEND))
7893 tree uns = unsigned_type_for (TREE_TYPE (and0));
7894 and0 = fold_convert_loc (loc, uns, and0);
7895 and1 = fold_convert_loc (loc, uns, and1);
7898 if (change)
7900 tem = force_fit_type (type, wi::to_widest (and1), 0,
7901 TREE_OVERFLOW (and1));
7902 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7903 fold_convert_loc (loc, type, and0), tem);
7907 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7908 cast (T1)X will fold away. We assume that this happens when X itself
7909 is a cast. */
7910 if (POINTER_TYPE_P (type)
7911 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7912 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7914 tree arg00 = TREE_OPERAND (arg0, 0);
7915 tree arg01 = TREE_OPERAND (arg0, 1);
7917 return fold_build_pointer_plus_loc
7918 (loc, fold_convert_loc (loc, type, arg00), arg01);
7921 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7922 of the same precision, and X is an integer type not narrower than
7923 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7924 if (INTEGRAL_TYPE_P (type)
7925 && TREE_CODE (op0) == BIT_NOT_EXPR
7926 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7927 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7928 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7930 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7931 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7932 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7933 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7934 fold_convert_loc (loc, type, tem));
7937 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7938 type of X and Y (integer types only). */
7939 if (INTEGRAL_TYPE_P (type)
7940 && TREE_CODE (op0) == MULT_EXPR
7941 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7942 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7944 /* Be careful not to introduce new overflows. */
7945 tree mult_type;
7946 if (TYPE_OVERFLOW_WRAPS (type))
7947 mult_type = type;
7948 else
7949 mult_type = unsigned_type_for (type);
7951 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7953 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7954 fold_convert_loc (loc, mult_type,
7955 TREE_OPERAND (op0, 0)),
7956 fold_convert_loc (loc, mult_type,
7957 TREE_OPERAND (op0, 1)));
7958 return fold_convert_loc (loc, type, tem);
7962 return NULL_TREE;
7964 case VIEW_CONVERT_EXPR:
7965 if (TREE_CODE (op0) == MEM_REF)
7967 tem = fold_build2_loc (loc, MEM_REF, type,
7968 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7969 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7970 return tem;
7973 return NULL_TREE;
7975 case NEGATE_EXPR:
7976 tem = fold_negate_expr (loc, arg0);
7977 if (tem)
7978 return fold_convert_loc (loc, type, tem);
7979 return NULL_TREE;
7981 case ABS_EXPR:
7982 /* Convert fabs((double)float) into (double)fabsf(float). */
7983 if (TREE_CODE (arg0) == NOP_EXPR
7984 && TREE_CODE (type) == REAL_TYPE)
7986 tree targ0 = strip_float_extensions (arg0);
7987 if (targ0 != arg0)
7988 return fold_convert_loc (loc, type,
7989 fold_build1_loc (loc, ABS_EXPR,
7990 TREE_TYPE (targ0),
7991 targ0));
7993 return NULL_TREE;
7995 case BIT_NOT_EXPR:
7996 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7997 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7998 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7999 fold_convert_loc (loc, type,
8000 TREE_OPERAND (arg0, 0)))))
8001 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8002 fold_convert_loc (loc, type,
8003 TREE_OPERAND (arg0, 1)));
8004 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8005 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8006 fold_convert_loc (loc, type,
8007 TREE_OPERAND (arg0, 1)))))
8008 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8009 fold_convert_loc (loc, type,
8010 TREE_OPERAND (arg0, 0)), tem);
8012 return NULL_TREE;
8014 case TRUTH_NOT_EXPR:
8015 /* Note that the operand of this must be an int
8016 and its values must be 0 or 1.
8017 ("true" is a fixed value perhaps depending on the language,
8018 but we don't handle values other than 1 correctly yet.) */
8019 tem = fold_truth_not_expr (loc, arg0);
8020 if (!tem)
8021 return NULL_TREE;
8022 return fold_convert_loc (loc, type, tem);
8024 case INDIRECT_REF:
8025 /* Fold *&X to X if X is an lvalue. */
8026 if (TREE_CODE (op0) == ADDR_EXPR)
8028 tree op00 = TREE_OPERAND (op0, 0);
8029 if ((TREE_CODE (op00) == VAR_DECL
8030 || TREE_CODE (op00) == PARM_DECL
8031 || TREE_CODE (op00) == RESULT_DECL)
8032 && !TREE_READONLY (op00))
8033 return op00;
8035 return NULL_TREE;
8037 default:
8038 return NULL_TREE;
8039 } /* switch (code) */
8043 /* If the operation was a conversion do _not_ mark a resulting constant
8044 with TREE_OVERFLOW if the original constant was not. These conversions
8045 have implementation defined behavior and retaining the TREE_OVERFLOW
8046 flag here would confuse later passes such as VRP. */
8047 tree
8048 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8049 tree type, tree op0)
8051 tree res = fold_unary_loc (loc, code, type, op0);
8052 if (res
8053 && TREE_CODE (res) == INTEGER_CST
8054 && TREE_CODE (op0) == INTEGER_CST
8055 && CONVERT_EXPR_CODE_P (code))
8056 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8058 return res;
8061 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8062 operands OP0 and OP1. LOC is the location of the resulting expression.
8063 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8064 Return the folded expression if folding is successful. Otherwise,
8065 return NULL_TREE. */
8066 static tree
8067 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8068 tree arg0, tree arg1, tree op0, tree op1)
8070 tree tem;
8072 /* We only do these simplifications if we are optimizing. */
8073 if (!optimize)
8074 return NULL_TREE;
8076 /* Check for things like (A || B) && (A || C). We can convert this
8077 to A || (B && C). Note that either operator can be any of the four
8078 truth and/or operations and the transformation will still be
8079 valid. Also note that we only care about order for the
8080 ANDIF and ORIF operators. If B contains side effects, this
8081 might change the truth-value of A. */
8082 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8083 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8084 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8085 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8086 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8087 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8089 tree a00 = TREE_OPERAND (arg0, 0);
8090 tree a01 = TREE_OPERAND (arg0, 1);
8091 tree a10 = TREE_OPERAND (arg1, 0);
8092 tree a11 = TREE_OPERAND (arg1, 1);
8093 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8094 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8095 && (code == TRUTH_AND_EXPR
8096 || code == TRUTH_OR_EXPR));
8098 if (operand_equal_p (a00, a10, 0))
8099 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8100 fold_build2_loc (loc, code, type, a01, a11));
8101 else if (commutative && operand_equal_p (a00, a11, 0))
8102 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8103 fold_build2_loc (loc, code, type, a01, a10));
8104 else if (commutative && operand_equal_p (a01, a10, 0))
8105 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8106 fold_build2_loc (loc, code, type, a00, a11));
8108 /* This case if tricky because we must either have commutative
8109 operators or else A10 must not have side-effects. */
8111 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8112 && operand_equal_p (a01, a11, 0))
8113 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8114 fold_build2_loc (loc, code, type, a00, a10),
8115 a01);
8118 /* See if we can build a range comparison. */
8119 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8120 return tem;
8122 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8123 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8125 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8126 if (tem)
8127 return fold_build2_loc (loc, code, type, tem, arg1);
8130 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8131 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8133 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8134 if (tem)
8135 return fold_build2_loc (loc, code, type, arg0, tem);
8138 /* Check for the possibility of merging component references. If our
8139 lhs is another similar operation, try to merge its rhs with our
8140 rhs. Then try to merge our lhs and rhs. */
8141 if (TREE_CODE (arg0) == code
8142 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8143 TREE_OPERAND (arg0, 1), arg1)))
8144 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8146 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8147 return tem;
8149 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8150 && (code == TRUTH_AND_EXPR
8151 || code == TRUTH_ANDIF_EXPR
8152 || code == TRUTH_OR_EXPR
8153 || code == TRUTH_ORIF_EXPR))
8155 enum tree_code ncode, icode;
8157 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8158 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8159 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8161 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8162 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8163 We don't want to pack more than two leafs to a non-IF AND/OR
8164 expression.
8165 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8166 equal to IF-CODE, then we don't want to add right-hand operand.
8167 If the inner right-hand side of left-hand operand has
8168 side-effects, or isn't simple, then we can't add to it,
8169 as otherwise we might destroy if-sequence. */
8170 if (TREE_CODE (arg0) == icode
8171 && simple_operand_p_2 (arg1)
8172 /* Needed for sequence points to handle trappings, and
8173 side-effects. */
8174 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8176 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8177 arg1);
8178 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8179 tem);
8181 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8182 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8183 else if (TREE_CODE (arg1) == icode
8184 && simple_operand_p_2 (arg0)
8185 /* Needed for sequence points to handle trappings, and
8186 side-effects. */
8187 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8189 tem = fold_build2_loc (loc, ncode, type,
8190 arg0, TREE_OPERAND (arg1, 0));
8191 return fold_build2_loc (loc, icode, type, tem,
8192 TREE_OPERAND (arg1, 1));
8194 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8195 into (A OR B).
8196 For sequence point consistancy, we need to check for trapping,
8197 and side-effects. */
8198 else if (code == icode && simple_operand_p_2 (arg0)
8199 && simple_operand_p_2 (arg1))
8200 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8203 return NULL_TREE;
8206 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8207 by changing CODE to reduce the magnitude of constants involved in
8208 ARG0 of the comparison.
8209 Returns a canonicalized comparison tree if a simplification was
8210 possible, otherwise returns NULL_TREE.
8211 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8212 valid if signed overflow is undefined. */
8214 static tree
8215 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8216 tree arg0, tree arg1,
8217 bool *strict_overflow_p)
8219 enum tree_code code0 = TREE_CODE (arg0);
8220 tree t, cst0 = NULL_TREE;
8221 int sgn0;
8223 /* Match A +- CST code arg1. We can change this only if overflow
8224 is undefined. */
8225 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8226 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8227 /* In principle pointers also have undefined overflow behavior,
8228 but that causes problems elsewhere. */
8229 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8230 && (code0 == MINUS_EXPR
8231 || code0 == PLUS_EXPR)
8232 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8233 return NULL_TREE;
8235 /* Identify the constant in arg0 and its sign. */
8236 cst0 = TREE_OPERAND (arg0, 1);
8237 sgn0 = tree_int_cst_sgn (cst0);
8239 /* Overflowed constants and zero will cause problems. */
8240 if (integer_zerop (cst0)
8241 || TREE_OVERFLOW (cst0))
8242 return NULL_TREE;
8244 /* See if we can reduce the magnitude of the constant in
8245 arg0 by changing the comparison code. */
8246 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8247 if (code == LT_EXPR
8248 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8249 code = LE_EXPR;
8250 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8251 else if (code == GT_EXPR
8252 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8253 code = GE_EXPR;
8254 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8255 else if (code == LE_EXPR
8256 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8257 code = LT_EXPR;
8258 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8259 else if (code == GE_EXPR
8260 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8261 code = GT_EXPR;
8262 else
8263 return NULL_TREE;
8264 *strict_overflow_p = true;
8266 /* Now build the constant reduced in magnitude. But not if that
8267 would produce one outside of its types range. */
8268 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8269 && ((sgn0 == 1
8270 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8271 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8272 || (sgn0 == -1
8273 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8274 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8275 return NULL_TREE;
8277 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8278 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8279 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8280 t = fold_convert (TREE_TYPE (arg1), t);
8282 return fold_build2_loc (loc, code, type, t, arg1);
8285 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8286 overflow further. Try to decrease the magnitude of constants involved
8287 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8288 and put sole constants at the second argument position.
8289 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8291 static tree
8292 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8293 tree arg0, tree arg1)
8295 tree t;
8296 bool strict_overflow_p;
8297 const char * const warnmsg = G_("assuming signed overflow does not occur "
8298 "when reducing constant in comparison");
8300 /* Try canonicalization by simplifying arg0. */
8301 strict_overflow_p = false;
8302 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8303 &strict_overflow_p);
8304 if (t)
8306 if (strict_overflow_p)
8307 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8308 return t;
8311 /* Try canonicalization by simplifying arg1 using the swapped
8312 comparison. */
8313 code = swap_tree_comparison (code);
8314 strict_overflow_p = false;
8315 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8316 &strict_overflow_p);
8317 if (t && strict_overflow_p)
8318 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8319 return t;
8322 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8323 space. This is used to avoid issuing overflow warnings for
8324 expressions like &p->x which can not wrap. */
8326 static bool
8327 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8329 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8330 return true;
8332 if (bitpos < 0)
8333 return true;
8335 wide_int wi_offset;
8336 int precision = TYPE_PRECISION (TREE_TYPE (base));
8337 if (offset == NULL_TREE)
8338 wi_offset = wi::zero (precision);
8339 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8340 return true;
8341 else
8342 wi_offset = offset;
8344 bool overflow;
8345 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8346 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8347 if (overflow)
8348 return true;
8350 if (!wi::fits_uhwi_p (total))
8351 return true;
8353 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8354 if (size <= 0)
8355 return true;
8357 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8358 array. */
8359 if (TREE_CODE (base) == ADDR_EXPR)
8361 HOST_WIDE_INT base_size;
8363 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8364 if (base_size > 0 && size < base_size)
8365 size = base_size;
8368 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8371 /* Return a positive integer when the symbol DECL is known to have
8372 a nonzero address, zero when it's known not to (e.g., it's a weak
8373 symbol), and a negative integer when the symbol is not yet in the
8374 symbol table and so whether or not its address is zero is unknown. */
8375 static int
8376 maybe_nonzero_address (tree decl)
8378 if (DECL_P (decl) && decl_in_symtab_p (decl))
8379 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8380 return symbol->nonzero_address ();
8382 return -1;
8385 /* Subroutine of fold_binary. This routine performs all of the
8386 transformations that are common to the equality/inequality
8387 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8388 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8389 fold_binary should call fold_binary. Fold a comparison with
8390 tree code CODE and type TYPE with operands OP0 and OP1. Return
8391 the folded comparison or NULL_TREE. */
8393 static tree
8394 fold_comparison (location_t loc, enum tree_code code, tree type,
8395 tree op0, tree op1)
8397 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8398 tree arg0, arg1, tem;
8400 arg0 = op0;
8401 arg1 = op1;
8403 STRIP_SIGN_NOPS (arg0);
8404 STRIP_SIGN_NOPS (arg1);
8406 /* For comparisons of pointers we can decompose it to a compile time
8407 comparison of the base objects and the offsets into the object.
8408 This requires at least one operand being an ADDR_EXPR or a
8409 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8410 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8411 && (TREE_CODE (arg0) == ADDR_EXPR
8412 || TREE_CODE (arg1) == ADDR_EXPR
8413 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8414 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8416 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8417 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8418 machine_mode mode;
8419 int volatilep, reversep, unsignedp;
8420 bool indirect_base0 = false, indirect_base1 = false;
8422 /* Get base and offset for the access. Strip ADDR_EXPR for
8423 get_inner_reference, but put it back by stripping INDIRECT_REF
8424 off the base object if possible. indirect_baseN will be true
8425 if baseN is not an address but refers to the object itself. */
8426 base0 = arg0;
8427 if (TREE_CODE (arg0) == ADDR_EXPR)
8429 base0
8430 = get_inner_reference (TREE_OPERAND (arg0, 0),
8431 &bitsize, &bitpos0, &offset0, &mode,
8432 &unsignedp, &reversep, &volatilep, false);
8433 if (TREE_CODE (base0) == INDIRECT_REF)
8434 base0 = TREE_OPERAND (base0, 0);
8435 else
8436 indirect_base0 = true;
8438 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8440 base0 = TREE_OPERAND (arg0, 0);
8441 STRIP_SIGN_NOPS (base0);
8442 if (TREE_CODE (base0) == ADDR_EXPR)
8444 base0
8445 = get_inner_reference (TREE_OPERAND (base0, 0),
8446 &bitsize, &bitpos0, &offset0, &mode,
8447 &unsignedp, &reversep, &volatilep,
8448 false);
8449 if (TREE_CODE (base0) == INDIRECT_REF)
8450 base0 = TREE_OPERAND (base0, 0);
8451 else
8452 indirect_base0 = true;
8454 if (offset0 == NULL_TREE || integer_zerop (offset0))
8455 offset0 = TREE_OPERAND (arg0, 1);
8456 else
8457 offset0 = size_binop (PLUS_EXPR, offset0,
8458 TREE_OPERAND (arg0, 1));
8459 if (TREE_CODE (offset0) == INTEGER_CST)
8461 offset_int tem = wi::sext (wi::to_offset (offset0),
8462 TYPE_PRECISION (sizetype));
8463 tem <<= LOG2_BITS_PER_UNIT;
8464 tem += bitpos0;
8465 if (wi::fits_shwi_p (tem))
8467 bitpos0 = tem.to_shwi ();
8468 offset0 = NULL_TREE;
8473 base1 = arg1;
8474 if (TREE_CODE (arg1) == ADDR_EXPR)
8476 base1
8477 = get_inner_reference (TREE_OPERAND (arg1, 0),
8478 &bitsize, &bitpos1, &offset1, &mode,
8479 &unsignedp, &reversep, &volatilep, false);
8480 if (TREE_CODE (base1) == INDIRECT_REF)
8481 base1 = TREE_OPERAND (base1, 0);
8482 else
8483 indirect_base1 = true;
8485 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8487 base1 = TREE_OPERAND (arg1, 0);
8488 STRIP_SIGN_NOPS (base1);
8489 if (TREE_CODE (base1) == ADDR_EXPR)
8491 base1
8492 = get_inner_reference (TREE_OPERAND (base1, 0),
8493 &bitsize, &bitpos1, &offset1, &mode,
8494 &unsignedp, &reversep, &volatilep,
8495 false);
8496 if (TREE_CODE (base1) == INDIRECT_REF)
8497 base1 = TREE_OPERAND (base1, 0);
8498 else
8499 indirect_base1 = true;
8501 if (offset1 == NULL_TREE || integer_zerop (offset1))
8502 offset1 = TREE_OPERAND (arg1, 1);
8503 else
8504 offset1 = size_binop (PLUS_EXPR, offset1,
8505 TREE_OPERAND (arg1, 1));
8506 if (TREE_CODE (offset1) == INTEGER_CST)
8508 offset_int tem = wi::sext (wi::to_offset (offset1),
8509 TYPE_PRECISION (sizetype));
8510 tem <<= LOG2_BITS_PER_UNIT;
8511 tem += bitpos1;
8512 if (wi::fits_shwi_p (tem))
8514 bitpos1 = tem.to_shwi ();
8515 offset1 = NULL_TREE;
8520 /* If we have equivalent bases we might be able to simplify. */
8521 if (indirect_base0 == indirect_base1
8522 && operand_equal_p (base0, base1,
8523 indirect_base0 ? OEP_ADDRESS_OF : 0))
8525 /* We can fold this expression to a constant if the non-constant
8526 offset parts are equal. */
8527 if ((offset0 == offset1
8528 || (offset0 && offset1
8529 && operand_equal_p (offset0, offset1, 0)))
8530 && (code == EQ_EXPR
8531 || code == NE_EXPR
8532 || (indirect_base0 && DECL_P (base0))
8533 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8536 if (!equality_code
8537 && bitpos0 != bitpos1
8538 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8539 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8540 fold_overflow_warning (("assuming pointer wraparound does not "
8541 "occur when comparing P +- C1 with "
8542 "P +- C2"),
8543 WARN_STRICT_OVERFLOW_CONDITIONAL);
8545 switch (code)
8547 case EQ_EXPR:
8548 return constant_boolean_node (bitpos0 == bitpos1, type);
8549 case NE_EXPR:
8550 return constant_boolean_node (bitpos0 != bitpos1, type);
8551 case LT_EXPR:
8552 return constant_boolean_node (bitpos0 < bitpos1, type);
8553 case LE_EXPR:
8554 return constant_boolean_node (bitpos0 <= bitpos1, type);
8555 case GE_EXPR:
8556 return constant_boolean_node (bitpos0 >= bitpos1, type);
8557 case GT_EXPR:
8558 return constant_boolean_node (bitpos0 > bitpos1, type);
8559 default:;
8562 /* We can simplify the comparison to a comparison of the variable
8563 offset parts if the constant offset parts are equal.
8564 Be careful to use signed sizetype here because otherwise we
8565 mess with array offsets in the wrong way. This is possible
8566 because pointer arithmetic is restricted to retain within an
8567 object and overflow on pointer differences is undefined as of
8568 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8569 else if (bitpos0 == bitpos1
8570 && (equality_code
8571 || (indirect_base0 && DECL_P (base0))
8572 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8574 /* By converting to signed sizetype we cover middle-end pointer
8575 arithmetic which operates on unsigned pointer types of size
8576 type size and ARRAY_REF offsets which are properly sign or
8577 zero extended from their type in case it is narrower than
8578 sizetype. */
8579 if (offset0 == NULL_TREE)
8580 offset0 = build_int_cst (ssizetype, 0);
8581 else
8582 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8583 if (offset1 == NULL_TREE)
8584 offset1 = build_int_cst (ssizetype, 0);
8585 else
8586 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8588 if (!equality_code
8589 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8590 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8591 fold_overflow_warning (("assuming pointer wraparound does not "
8592 "occur when comparing P +- C1 with "
8593 "P +- C2"),
8594 WARN_STRICT_OVERFLOW_COMPARISON);
8596 return fold_build2_loc (loc, code, type, offset0, offset1);
8599 /* For equal offsets we can simplify to a comparison of the
8600 base addresses. */
8601 else if (bitpos0 == bitpos1
8602 && (indirect_base0
8603 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8604 && (indirect_base1
8605 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8606 && ((offset0 == offset1)
8607 || (offset0 && offset1
8608 && operand_equal_p (offset0, offset1, 0))))
8610 if (indirect_base0)
8611 base0 = build_fold_addr_expr_loc (loc, base0);
8612 if (indirect_base1)
8613 base1 = build_fold_addr_expr_loc (loc, base1);
8614 return fold_build2_loc (loc, code, type, base0, base1);
8616 /* Comparison between an ordinary (non-weak) symbol and a null
8617 pointer can be eliminated since such symbols must have a non
8618 null address. In C, relational expressions between pointers
8619 to objects and null pointers are undefined. The results
8620 below follow the C++ rules with the additional property that
8621 every object pointer compares greater than a null pointer.
8623 else if (DECL_P (base0)
8624 && maybe_nonzero_address (base0) > 0
8625 /* Avoid folding references to struct members at offset 0 to
8626 prevent tests like '&ptr->firstmember == 0' from getting
8627 eliminated. When ptr is null, although the -> expression
8628 is strictly speaking invalid, GCC retains it as a matter
8629 of QoI. See PR c/44555. */
8630 && (offset0 == NULL_TREE && bitpos0 != 0)
8631 /* The caller guarantees that when one of the arguments is
8632 constant (i.e., null in this case) it is second. */
8633 && integer_zerop (arg1))
8635 switch (code)
8637 case EQ_EXPR:
8638 case LE_EXPR:
8639 case LT_EXPR:
8640 return constant_boolean_node (false, type);
8641 case GE_EXPR:
8642 case GT_EXPR:
8643 case NE_EXPR:
8644 return constant_boolean_node (true, type);
8645 default:
8646 gcc_unreachable ();
8651 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8652 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8653 the resulting offset is smaller in absolute value than the
8654 original one and has the same sign. */
8655 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8656 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8657 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8658 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8659 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8660 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8661 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8662 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8664 tree const1 = TREE_OPERAND (arg0, 1);
8665 tree const2 = TREE_OPERAND (arg1, 1);
8666 tree variable1 = TREE_OPERAND (arg0, 0);
8667 tree variable2 = TREE_OPERAND (arg1, 0);
8668 tree cst;
8669 const char * const warnmsg = G_("assuming signed overflow does not "
8670 "occur when combining constants around "
8671 "a comparison");
8673 /* Put the constant on the side where it doesn't overflow and is
8674 of lower absolute value and of same sign than before. */
8675 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8676 ? MINUS_EXPR : PLUS_EXPR,
8677 const2, const1);
8678 if (!TREE_OVERFLOW (cst)
8679 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8680 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8682 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8683 return fold_build2_loc (loc, code, type,
8684 variable1,
8685 fold_build2_loc (loc, TREE_CODE (arg1),
8686 TREE_TYPE (arg1),
8687 variable2, cst));
8690 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8691 ? MINUS_EXPR : PLUS_EXPR,
8692 const1, const2);
8693 if (!TREE_OVERFLOW (cst)
8694 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8695 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8697 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8698 return fold_build2_loc (loc, code, type,
8699 fold_build2_loc (loc, TREE_CODE (arg0),
8700 TREE_TYPE (arg0),
8701 variable1, cst),
8702 variable2);
8706 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8707 if (tem)
8708 return tem;
8710 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8711 constant, we can simplify it. */
8712 if (TREE_CODE (arg1) == INTEGER_CST
8713 && (TREE_CODE (arg0) == MIN_EXPR
8714 || TREE_CODE (arg0) == MAX_EXPR)
8715 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8717 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8718 if (tem)
8719 return tem;
8722 /* If we are comparing an expression that just has comparisons
8723 of two integer values, arithmetic expressions of those comparisons,
8724 and constants, we can simplify it. There are only three cases
8725 to check: the two values can either be equal, the first can be
8726 greater, or the second can be greater. Fold the expression for
8727 those three values. Since each value must be 0 or 1, we have
8728 eight possibilities, each of which corresponds to the constant 0
8729 or 1 or one of the six possible comparisons.
8731 This handles common cases like (a > b) == 0 but also handles
8732 expressions like ((x > y) - (y > x)) > 0, which supposedly
8733 occur in macroized code. */
8735 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8737 tree cval1 = 0, cval2 = 0;
8738 int save_p = 0;
8740 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8741 /* Don't handle degenerate cases here; they should already
8742 have been handled anyway. */
8743 && cval1 != 0 && cval2 != 0
8744 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8745 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8746 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8747 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8748 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8749 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8750 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8752 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8753 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8755 /* We can't just pass T to eval_subst in case cval1 or cval2
8756 was the same as ARG1. */
8758 tree high_result
8759 = fold_build2_loc (loc, code, type,
8760 eval_subst (loc, arg0, cval1, maxval,
8761 cval2, minval),
8762 arg1);
8763 tree equal_result
8764 = fold_build2_loc (loc, code, type,
8765 eval_subst (loc, arg0, cval1, maxval,
8766 cval2, maxval),
8767 arg1);
8768 tree low_result
8769 = fold_build2_loc (loc, code, type,
8770 eval_subst (loc, arg0, cval1, minval,
8771 cval2, maxval),
8772 arg1);
8774 /* All three of these results should be 0 or 1. Confirm they are.
8775 Then use those values to select the proper code to use. */
8777 if (TREE_CODE (high_result) == INTEGER_CST
8778 && TREE_CODE (equal_result) == INTEGER_CST
8779 && TREE_CODE (low_result) == INTEGER_CST)
8781 /* Make a 3-bit mask with the high-order bit being the
8782 value for `>', the next for '=', and the low for '<'. */
8783 switch ((integer_onep (high_result) * 4)
8784 + (integer_onep (equal_result) * 2)
8785 + integer_onep (low_result))
8787 case 0:
8788 /* Always false. */
8789 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8790 case 1:
8791 code = LT_EXPR;
8792 break;
8793 case 2:
8794 code = EQ_EXPR;
8795 break;
8796 case 3:
8797 code = LE_EXPR;
8798 break;
8799 case 4:
8800 code = GT_EXPR;
8801 break;
8802 case 5:
8803 code = NE_EXPR;
8804 break;
8805 case 6:
8806 code = GE_EXPR;
8807 break;
8808 case 7:
8809 /* Always true. */
8810 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8813 if (save_p)
8815 tem = save_expr (build2 (code, type, cval1, cval2));
8816 SET_EXPR_LOCATION (tem, loc);
8817 return tem;
8819 return fold_build2_loc (loc, code, type, cval1, cval2);
8824 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8825 into a single range test. */
8826 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8827 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8828 && TREE_CODE (arg1) == INTEGER_CST
8829 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8830 && !integer_zerop (TREE_OPERAND (arg0, 1))
8831 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8832 && !TREE_OVERFLOW (arg1))
8834 tem = fold_div_compare (loc, code, type, arg0, arg1);
8835 if (tem != NULL_TREE)
8836 return tem;
8839 return NULL_TREE;
8843 /* Subroutine of fold_binary. Optimize complex multiplications of the
8844 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8845 argument EXPR represents the expression "z" of type TYPE. */
8847 static tree
8848 fold_mult_zconjz (location_t loc, tree type, tree expr)
8850 tree itype = TREE_TYPE (type);
8851 tree rpart, ipart, tem;
8853 if (TREE_CODE (expr) == COMPLEX_EXPR)
8855 rpart = TREE_OPERAND (expr, 0);
8856 ipart = TREE_OPERAND (expr, 1);
8858 else if (TREE_CODE (expr) == COMPLEX_CST)
8860 rpart = TREE_REALPART (expr);
8861 ipart = TREE_IMAGPART (expr);
8863 else
8865 expr = save_expr (expr);
8866 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8867 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8870 rpart = save_expr (rpart);
8871 ipart = save_expr (ipart);
8872 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8873 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8874 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8875 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8876 build_zero_cst (itype));
8880 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8881 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8883 static bool
8884 vec_cst_ctor_to_array (tree arg, tree *elts)
8886 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8888 if (TREE_CODE (arg) == VECTOR_CST)
8890 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8891 elts[i] = VECTOR_CST_ELT (arg, i);
8893 else if (TREE_CODE (arg) == CONSTRUCTOR)
8895 constructor_elt *elt;
8897 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8898 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8899 return false;
8900 else
8901 elts[i] = elt->value;
8903 else
8904 return false;
8905 for (; i < nelts; i++)
8906 elts[i]
8907 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8908 return true;
8911 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8912 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8913 NULL_TREE otherwise. */
8915 static tree
8916 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8918 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8919 tree *elts;
8920 bool need_ctor = false;
8922 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8923 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8924 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8925 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8926 return NULL_TREE;
8928 elts = XALLOCAVEC (tree, nelts * 3);
8929 if (!vec_cst_ctor_to_array (arg0, elts)
8930 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8931 return NULL_TREE;
8933 for (i = 0; i < nelts; i++)
8935 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8936 need_ctor = true;
8937 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8940 if (need_ctor)
8942 vec<constructor_elt, va_gc> *v;
8943 vec_alloc (v, nelts);
8944 for (i = 0; i < nelts; i++)
8945 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8946 return build_constructor (type, v);
8948 else
8949 return build_vector (type, &elts[2 * nelts]);
8952 /* Try to fold a pointer difference of type TYPE two address expressions of
8953 array references AREF0 and AREF1 using location LOC. Return a
8954 simplified expression for the difference or NULL_TREE. */
8956 static tree
8957 fold_addr_of_array_ref_difference (location_t loc, tree type,
8958 tree aref0, tree aref1)
8960 tree base0 = TREE_OPERAND (aref0, 0);
8961 tree base1 = TREE_OPERAND (aref1, 0);
8962 tree base_offset = build_int_cst (type, 0);
8964 /* If the bases are array references as well, recurse. If the bases
8965 are pointer indirections compute the difference of the pointers.
8966 If the bases are equal, we are set. */
8967 if ((TREE_CODE (base0) == ARRAY_REF
8968 && TREE_CODE (base1) == ARRAY_REF
8969 && (base_offset
8970 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8971 || (INDIRECT_REF_P (base0)
8972 && INDIRECT_REF_P (base1)
8973 && (base_offset
8974 = fold_binary_loc (loc, MINUS_EXPR, type,
8975 fold_convert (type, TREE_OPERAND (base0, 0)),
8976 fold_convert (type,
8977 TREE_OPERAND (base1, 0)))))
8978 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8980 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8981 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8982 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8983 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8984 return fold_build2_loc (loc, PLUS_EXPR, type,
8985 base_offset,
8986 fold_build2_loc (loc, MULT_EXPR, type,
8987 diff, esz));
8989 return NULL_TREE;
8992 /* If the real or vector real constant CST of type TYPE has an exact
8993 inverse, return it, else return NULL. */
8995 tree
8996 exact_inverse (tree type, tree cst)
8998 REAL_VALUE_TYPE r;
8999 tree unit_type, *elts;
9000 machine_mode mode;
9001 unsigned vec_nelts, i;
9003 switch (TREE_CODE (cst))
9005 case REAL_CST:
9006 r = TREE_REAL_CST (cst);
9008 if (exact_real_inverse (TYPE_MODE (type), &r))
9009 return build_real (type, r);
9011 return NULL_TREE;
9013 case VECTOR_CST:
9014 vec_nelts = VECTOR_CST_NELTS (cst);
9015 elts = XALLOCAVEC (tree, vec_nelts);
9016 unit_type = TREE_TYPE (type);
9017 mode = TYPE_MODE (unit_type);
9019 for (i = 0; i < vec_nelts; i++)
9021 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9022 if (!exact_real_inverse (mode, &r))
9023 return NULL_TREE;
9024 elts[i] = build_real (unit_type, r);
9027 return build_vector (type, elts);
9029 default:
9030 return NULL_TREE;
9034 /* Mask out the tz least significant bits of X of type TYPE where
9035 tz is the number of trailing zeroes in Y. */
9036 static wide_int
9037 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9039 int tz = wi::ctz (y);
9040 if (tz > 0)
9041 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9042 return x;
9045 /* Return true when T is an address and is known to be nonzero.
9046 For floating point we further ensure that T is not denormal.
9047 Similar logic is present in nonzero_address in rtlanal.h.
9049 If the return value is based on the assumption that signed overflow
9050 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9051 change *STRICT_OVERFLOW_P. */
9053 static bool
9054 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9056 tree type = TREE_TYPE (t);
9057 enum tree_code code;
9059 /* Doing something useful for floating point would need more work. */
9060 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9061 return false;
9063 code = TREE_CODE (t);
9064 switch (TREE_CODE_CLASS (code))
9066 case tcc_unary:
9067 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9068 strict_overflow_p);
9069 case tcc_binary:
9070 case tcc_comparison:
9071 return tree_binary_nonzero_warnv_p (code, type,
9072 TREE_OPERAND (t, 0),
9073 TREE_OPERAND (t, 1),
9074 strict_overflow_p);
9075 case tcc_constant:
9076 case tcc_declaration:
9077 case tcc_reference:
9078 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9080 default:
9081 break;
9084 switch (code)
9086 case TRUTH_NOT_EXPR:
9087 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9088 strict_overflow_p);
9090 case TRUTH_AND_EXPR:
9091 case TRUTH_OR_EXPR:
9092 case TRUTH_XOR_EXPR:
9093 return tree_binary_nonzero_warnv_p (code, type,
9094 TREE_OPERAND (t, 0),
9095 TREE_OPERAND (t, 1),
9096 strict_overflow_p);
9098 case COND_EXPR:
9099 case CONSTRUCTOR:
9100 case OBJ_TYPE_REF:
9101 case ASSERT_EXPR:
9102 case ADDR_EXPR:
9103 case WITH_SIZE_EXPR:
9104 case SSA_NAME:
9105 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9107 case COMPOUND_EXPR:
9108 case MODIFY_EXPR:
9109 case BIND_EXPR:
9110 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9111 strict_overflow_p);
9113 case SAVE_EXPR:
9114 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9115 strict_overflow_p);
9117 case CALL_EXPR:
9119 tree fndecl = get_callee_fndecl (t);
9120 if (!fndecl) return false;
9121 if (flag_delete_null_pointer_checks && !flag_check_new
9122 && DECL_IS_OPERATOR_NEW (fndecl)
9123 && !TREE_NOTHROW (fndecl))
9124 return true;
9125 if (flag_delete_null_pointer_checks
9126 && lookup_attribute ("returns_nonnull",
9127 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9128 return true;
9129 return alloca_call_p (t);
9132 default:
9133 break;
9135 return false;
9138 /* Return true when T is an address and is known to be nonzero.
9139 Handle warnings about undefined signed overflow. */
9141 static bool
9142 tree_expr_nonzero_p (tree t)
9144 bool ret, strict_overflow_p;
9146 strict_overflow_p = false;
9147 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9148 if (strict_overflow_p)
9149 fold_overflow_warning (("assuming signed overflow does not occur when "
9150 "determining that expression is always "
9151 "non-zero"),
9152 WARN_STRICT_OVERFLOW_MISC);
9153 return ret;
9156 /* Return true if T is known not to be equal to an integer W. */
9158 bool
9159 expr_not_equal_to (tree t, const wide_int &w)
9161 wide_int min, max, nz;
9162 value_range_type rtype;
9163 switch (TREE_CODE (t))
9165 case INTEGER_CST:
9166 return wi::ne_p (t, w);
9168 case SSA_NAME:
9169 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9170 return false;
9171 rtype = get_range_info (t, &min, &max);
9172 if (rtype == VR_RANGE)
9174 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9175 return true;
9176 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9177 return true;
9179 else if (rtype == VR_ANTI_RANGE
9180 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9181 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9182 return true;
9183 /* If T has some known zero bits and W has any of those bits set,
9184 then T is known not to be equal to W. */
9185 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9186 TYPE_PRECISION (TREE_TYPE (t))), 0))
9187 return true;
9188 return false;
9190 default:
9191 return false;
9195 /* Fold a binary expression of code CODE and type TYPE with operands
9196 OP0 and OP1. LOC is the location of the resulting expression.
9197 Return the folded expression if folding is successful. Otherwise,
9198 return NULL_TREE. */
9200 tree
9201 fold_binary_loc (location_t loc,
9202 enum tree_code code, tree type, tree op0, tree op1)
9204 enum tree_code_class kind = TREE_CODE_CLASS (code);
9205 tree arg0, arg1, tem;
9206 tree t1 = NULL_TREE;
9207 bool strict_overflow_p;
9208 unsigned int prec;
9210 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9211 && TREE_CODE_LENGTH (code) == 2
9212 && op0 != NULL_TREE
9213 && op1 != NULL_TREE);
9215 arg0 = op0;
9216 arg1 = op1;
9218 /* Strip any conversions that don't change the mode. This is
9219 safe for every expression, except for a comparison expression
9220 because its signedness is derived from its operands. So, in
9221 the latter case, only strip conversions that don't change the
9222 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9223 preserved.
9225 Note that this is done as an internal manipulation within the
9226 constant folder, in order to find the simplest representation
9227 of the arguments so that their form can be studied. In any
9228 cases, the appropriate type conversions should be put back in
9229 the tree that will get out of the constant folder. */
9231 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9233 STRIP_SIGN_NOPS (arg0);
9234 STRIP_SIGN_NOPS (arg1);
9236 else
9238 STRIP_NOPS (arg0);
9239 STRIP_NOPS (arg1);
9242 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9243 constant but we can't do arithmetic on them. */
9244 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9246 tem = const_binop (code, type, arg0, arg1);
9247 if (tem != NULL_TREE)
9249 if (TREE_TYPE (tem) != type)
9250 tem = fold_convert_loc (loc, type, tem);
9251 return tem;
9255 /* If this is a commutative operation, and ARG0 is a constant, move it
9256 to ARG1 to reduce the number of tests below. */
9257 if (commutative_tree_code (code)
9258 && tree_swap_operands_p (arg0, arg1, true))
9259 return fold_build2_loc (loc, code, type, op1, op0);
9261 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9262 to ARG1 to reduce the number of tests below. */
9263 if (kind == tcc_comparison
9264 && tree_swap_operands_p (arg0, arg1, true))
9265 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9267 tem = generic_simplify (loc, code, type, op0, op1);
9268 if (tem)
9269 return tem;
9271 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9273 First check for cases where an arithmetic operation is applied to a
9274 compound, conditional, or comparison operation. Push the arithmetic
9275 operation inside the compound or conditional to see if any folding
9276 can then be done. Convert comparison to conditional for this purpose.
9277 The also optimizes non-constant cases that used to be done in
9278 expand_expr.
9280 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9281 one of the operands is a comparison and the other is a comparison, a
9282 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9283 code below would make the expression more complex. Change it to a
9284 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9285 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9287 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9288 || code == EQ_EXPR || code == NE_EXPR)
9289 && TREE_CODE (type) != VECTOR_TYPE
9290 && ((truth_value_p (TREE_CODE (arg0))
9291 && (truth_value_p (TREE_CODE (arg1))
9292 || (TREE_CODE (arg1) == BIT_AND_EXPR
9293 && integer_onep (TREE_OPERAND (arg1, 1)))))
9294 || (truth_value_p (TREE_CODE (arg1))
9295 && (truth_value_p (TREE_CODE (arg0))
9296 || (TREE_CODE (arg0) == BIT_AND_EXPR
9297 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9299 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9300 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9301 : TRUTH_XOR_EXPR,
9302 boolean_type_node,
9303 fold_convert_loc (loc, boolean_type_node, arg0),
9304 fold_convert_loc (loc, boolean_type_node, arg1));
9306 if (code == EQ_EXPR)
9307 tem = invert_truthvalue_loc (loc, tem);
9309 return fold_convert_loc (loc, type, tem);
9312 if (TREE_CODE_CLASS (code) == tcc_binary
9313 || TREE_CODE_CLASS (code) == tcc_comparison)
9315 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9317 tem = fold_build2_loc (loc, code, type,
9318 fold_convert_loc (loc, TREE_TYPE (op0),
9319 TREE_OPERAND (arg0, 1)), op1);
9320 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9321 tem);
9323 if (TREE_CODE (arg1) == COMPOUND_EXPR
9324 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9326 tem = fold_build2_loc (loc, code, type, op0,
9327 fold_convert_loc (loc, TREE_TYPE (op1),
9328 TREE_OPERAND (arg1, 1)));
9329 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9330 tem);
9333 if (TREE_CODE (arg0) == COND_EXPR
9334 || TREE_CODE (arg0) == VEC_COND_EXPR
9335 || COMPARISON_CLASS_P (arg0))
9337 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9338 arg0, arg1,
9339 /*cond_first_p=*/1);
9340 if (tem != NULL_TREE)
9341 return tem;
9344 if (TREE_CODE (arg1) == COND_EXPR
9345 || TREE_CODE (arg1) == VEC_COND_EXPR
9346 || COMPARISON_CLASS_P (arg1))
9348 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9349 arg1, arg0,
9350 /*cond_first_p=*/0);
9351 if (tem != NULL_TREE)
9352 return tem;
9356 switch (code)
9358 case MEM_REF:
9359 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9360 if (TREE_CODE (arg0) == ADDR_EXPR
9361 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9363 tree iref = TREE_OPERAND (arg0, 0);
9364 return fold_build2 (MEM_REF, type,
9365 TREE_OPERAND (iref, 0),
9366 int_const_binop (PLUS_EXPR, arg1,
9367 TREE_OPERAND (iref, 1)));
9370 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9371 if (TREE_CODE (arg0) == ADDR_EXPR
9372 && handled_component_p (TREE_OPERAND (arg0, 0)))
9374 tree base;
9375 HOST_WIDE_INT coffset;
9376 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9377 &coffset);
9378 if (!base)
9379 return NULL_TREE;
9380 return fold_build2 (MEM_REF, type,
9381 build_fold_addr_expr (base),
9382 int_const_binop (PLUS_EXPR, arg1,
9383 size_int (coffset)));
9386 return NULL_TREE;
9388 case POINTER_PLUS_EXPR:
9389 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9390 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9391 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9392 return fold_convert_loc (loc, type,
9393 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9394 fold_convert_loc (loc, sizetype,
9395 arg1),
9396 fold_convert_loc (loc, sizetype,
9397 arg0)));
9399 return NULL_TREE;
9401 case PLUS_EXPR:
9402 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9404 /* X + (X / CST) * -CST is X % CST. */
9405 if (TREE_CODE (arg1) == MULT_EXPR
9406 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9407 && operand_equal_p (arg0,
9408 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9410 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9411 tree cst1 = TREE_OPERAND (arg1, 1);
9412 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9413 cst1, cst0);
9414 if (sum && integer_zerop (sum))
9415 return fold_convert_loc (loc, type,
9416 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9417 TREE_TYPE (arg0), arg0,
9418 cst0));
9422 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9423 one. Make sure the type is not saturating and has the signedness of
9424 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9425 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9426 if ((TREE_CODE (arg0) == MULT_EXPR
9427 || TREE_CODE (arg1) == MULT_EXPR)
9428 && !TYPE_SATURATING (type)
9429 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9430 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9431 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9433 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9434 if (tem)
9435 return tem;
9438 if (! FLOAT_TYPE_P (type))
9440 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9441 (plus (plus (mult) (mult)) (foo)) so that we can
9442 take advantage of the factoring cases below. */
9443 if (ANY_INTEGRAL_TYPE_P (type)
9444 && TYPE_OVERFLOW_WRAPS (type)
9445 && (((TREE_CODE (arg0) == PLUS_EXPR
9446 || TREE_CODE (arg0) == MINUS_EXPR)
9447 && TREE_CODE (arg1) == MULT_EXPR)
9448 || ((TREE_CODE (arg1) == PLUS_EXPR
9449 || TREE_CODE (arg1) == MINUS_EXPR)
9450 && TREE_CODE (arg0) == MULT_EXPR)))
9452 tree parg0, parg1, parg, marg;
9453 enum tree_code pcode;
9455 if (TREE_CODE (arg1) == MULT_EXPR)
9456 parg = arg0, marg = arg1;
9457 else
9458 parg = arg1, marg = arg0;
9459 pcode = TREE_CODE (parg);
9460 parg0 = TREE_OPERAND (parg, 0);
9461 parg1 = TREE_OPERAND (parg, 1);
9462 STRIP_NOPS (parg0);
9463 STRIP_NOPS (parg1);
9465 if (TREE_CODE (parg0) == MULT_EXPR
9466 && TREE_CODE (parg1) != MULT_EXPR)
9467 return fold_build2_loc (loc, pcode, type,
9468 fold_build2_loc (loc, PLUS_EXPR, type,
9469 fold_convert_loc (loc, type,
9470 parg0),
9471 fold_convert_loc (loc, type,
9472 marg)),
9473 fold_convert_loc (loc, type, parg1));
9474 if (TREE_CODE (parg0) != MULT_EXPR
9475 && TREE_CODE (parg1) == MULT_EXPR)
9476 return
9477 fold_build2_loc (loc, PLUS_EXPR, type,
9478 fold_convert_loc (loc, type, parg0),
9479 fold_build2_loc (loc, pcode, type,
9480 fold_convert_loc (loc, type, marg),
9481 fold_convert_loc (loc, type,
9482 parg1)));
9485 else
9487 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9488 to __complex__ ( x, y ). This is not the same for SNaNs or
9489 if signed zeros are involved. */
9490 if (!HONOR_SNANS (element_mode (arg0))
9491 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9492 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9494 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9495 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9496 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9497 bool arg0rz = false, arg0iz = false;
9498 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9499 || (arg0i && (arg0iz = real_zerop (arg0i))))
9501 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9502 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9503 if (arg0rz && arg1i && real_zerop (arg1i))
9505 tree rp = arg1r ? arg1r
9506 : build1 (REALPART_EXPR, rtype, arg1);
9507 tree ip = arg0i ? arg0i
9508 : build1 (IMAGPART_EXPR, rtype, arg0);
9509 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9511 else if (arg0iz && arg1r && real_zerop (arg1r))
9513 tree rp = arg0r ? arg0r
9514 : build1 (REALPART_EXPR, rtype, arg0);
9515 tree ip = arg1i ? arg1i
9516 : build1 (IMAGPART_EXPR, rtype, arg1);
9517 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9522 if (flag_unsafe_math_optimizations
9523 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9524 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9525 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9526 return tem;
9528 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9529 We associate floats only if the user has specified
9530 -fassociative-math. */
9531 if (flag_associative_math
9532 && TREE_CODE (arg1) == PLUS_EXPR
9533 && TREE_CODE (arg0) != MULT_EXPR)
9535 tree tree10 = TREE_OPERAND (arg1, 0);
9536 tree tree11 = TREE_OPERAND (arg1, 1);
9537 if (TREE_CODE (tree11) == MULT_EXPR
9538 && TREE_CODE (tree10) == MULT_EXPR)
9540 tree tree0;
9541 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9542 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9545 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9546 We associate floats only if the user has specified
9547 -fassociative-math. */
9548 if (flag_associative_math
9549 && TREE_CODE (arg0) == PLUS_EXPR
9550 && TREE_CODE (arg1) != MULT_EXPR)
9552 tree tree00 = TREE_OPERAND (arg0, 0);
9553 tree tree01 = TREE_OPERAND (arg0, 1);
9554 if (TREE_CODE (tree01) == MULT_EXPR
9555 && TREE_CODE (tree00) == MULT_EXPR)
9557 tree tree0;
9558 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9559 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9564 bit_rotate:
9565 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9566 is a rotate of A by C1 bits. */
9567 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9568 is a rotate of A by B bits. */
9570 enum tree_code code0, code1;
9571 tree rtype;
9572 code0 = TREE_CODE (arg0);
9573 code1 = TREE_CODE (arg1);
9574 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9575 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9576 && operand_equal_p (TREE_OPERAND (arg0, 0),
9577 TREE_OPERAND (arg1, 0), 0)
9578 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9579 TYPE_UNSIGNED (rtype))
9580 /* Only create rotates in complete modes. Other cases are not
9581 expanded properly. */
9582 && (element_precision (rtype)
9583 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9585 tree tree01, tree11;
9586 enum tree_code code01, code11;
9588 tree01 = TREE_OPERAND (arg0, 1);
9589 tree11 = TREE_OPERAND (arg1, 1);
9590 STRIP_NOPS (tree01);
9591 STRIP_NOPS (tree11);
9592 code01 = TREE_CODE (tree01);
9593 code11 = TREE_CODE (tree11);
9594 if (code01 == INTEGER_CST
9595 && code11 == INTEGER_CST
9596 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9597 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9599 tem = build2_loc (loc, LROTATE_EXPR,
9600 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9601 TREE_OPERAND (arg0, 0),
9602 code0 == LSHIFT_EXPR
9603 ? TREE_OPERAND (arg0, 1)
9604 : TREE_OPERAND (arg1, 1));
9605 return fold_convert_loc (loc, type, tem);
9607 else if (code11 == MINUS_EXPR)
9609 tree tree110, tree111;
9610 tree110 = TREE_OPERAND (tree11, 0);
9611 tree111 = TREE_OPERAND (tree11, 1);
9612 STRIP_NOPS (tree110);
9613 STRIP_NOPS (tree111);
9614 if (TREE_CODE (tree110) == INTEGER_CST
9615 && 0 == compare_tree_int (tree110,
9616 element_precision
9617 (TREE_TYPE (TREE_OPERAND
9618 (arg0, 0))))
9619 && operand_equal_p (tree01, tree111, 0))
9620 return
9621 fold_convert_loc (loc, type,
9622 build2 ((code0 == LSHIFT_EXPR
9623 ? LROTATE_EXPR
9624 : RROTATE_EXPR),
9625 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9626 TREE_OPERAND (arg0, 0),
9627 TREE_OPERAND (arg0, 1)));
9629 else if (code01 == MINUS_EXPR)
9631 tree tree010, tree011;
9632 tree010 = TREE_OPERAND (tree01, 0);
9633 tree011 = TREE_OPERAND (tree01, 1);
9634 STRIP_NOPS (tree010);
9635 STRIP_NOPS (tree011);
9636 if (TREE_CODE (tree010) == INTEGER_CST
9637 && 0 == compare_tree_int (tree010,
9638 element_precision
9639 (TREE_TYPE (TREE_OPERAND
9640 (arg0, 0))))
9641 && operand_equal_p (tree11, tree011, 0))
9642 return fold_convert_loc
9643 (loc, type,
9644 build2 ((code0 != LSHIFT_EXPR
9645 ? LROTATE_EXPR
9646 : RROTATE_EXPR),
9647 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9648 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9653 associate:
9654 /* In most languages, can't associate operations on floats through
9655 parentheses. Rather than remember where the parentheses were, we
9656 don't associate floats at all, unless the user has specified
9657 -fassociative-math.
9658 And, we need to make sure type is not saturating. */
9660 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9661 && !TYPE_SATURATING (type))
9663 tree var0, con0, lit0, minus_lit0;
9664 tree var1, con1, lit1, minus_lit1;
9665 tree atype = type;
9666 bool ok = true;
9668 /* Split both trees into variables, constants, and literals. Then
9669 associate each group together, the constants with literals,
9670 then the result with variables. This increases the chances of
9671 literals being recombined later and of generating relocatable
9672 expressions for the sum of a constant and literal. */
9673 var0 = split_tree (loc, arg0, type, code,
9674 &con0, &lit0, &minus_lit0, 0);
9675 var1 = split_tree (loc, arg1, type, code,
9676 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9678 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9679 if (code == MINUS_EXPR)
9680 code = PLUS_EXPR;
9682 /* With undefined overflow prefer doing association in a type
9683 which wraps on overflow, if that is one of the operand types. */
9684 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9685 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9687 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9688 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9689 atype = TREE_TYPE (arg0);
9690 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9691 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9692 atype = TREE_TYPE (arg1);
9693 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9696 /* With undefined overflow we can only associate constants with one
9697 variable, and constants whose association doesn't overflow. */
9698 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9699 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9701 if (var0 && var1)
9703 tree tmp0 = var0;
9704 tree tmp1 = var1;
9705 bool one_neg = false;
9707 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9709 tmp0 = TREE_OPERAND (tmp0, 0);
9710 one_neg = !one_neg;
9712 if (CONVERT_EXPR_P (tmp0)
9713 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9714 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9715 <= TYPE_PRECISION (atype)))
9716 tmp0 = TREE_OPERAND (tmp0, 0);
9717 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9719 tmp1 = TREE_OPERAND (tmp1, 0);
9720 one_neg = !one_neg;
9722 if (CONVERT_EXPR_P (tmp1)
9723 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9724 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9725 <= TYPE_PRECISION (atype)))
9726 tmp1 = TREE_OPERAND (tmp1, 0);
9727 /* The only case we can still associate with two variables
9728 is if they cancel out. */
9729 if (!one_neg
9730 || !operand_equal_p (tmp0, tmp1, 0))
9731 ok = false;
9735 /* Only do something if we found more than two objects. Otherwise,
9736 nothing has changed and we risk infinite recursion. */
9737 if (ok
9738 && (2 < ((var0 != 0) + (var1 != 0)
9739 + (con0 != 0) + (con1 != 0)
9740 + (lit0 != 0) + (lit1 != 0)
9741 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9743 bool any_overflows = false;
9744 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9745 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9746 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9747 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9748 var0 = associate_trees (loc, var0, var1, code, atype);
9749 con0 = associate_trees (loc, con0, con1, code, atype);
9750 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9751 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9752 code, atype);
9754 /* Preserve the MINUS_EXPR if the negative part of the literal is
9755 greater than the positive part. Otherwise, the multiplicative
9756 folding code (i.e extract_muldiv) may be fooled in case
9757 unsigned constants are subtracted, like in the following
9758 example: ((X*2 + 4) - 8U)/2. */
9759 if (minus_lit0 && lit0)
9761 if (TREE_CODE (lit0) == INTEGER_CST
9762 && TREE_CODE (minus_lit0) == INTEGER_CST
9763 && tree_int_cst_lt (lit0, minus_lit0))
9765 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9766 MINUS_EXPR, atype);
9767 lit0 = 0;
9769 else
9771 lit0 = associate_trees (loc, lit0, minus_lit0,
9772 MINUS_EXPR, atype);
9773 minus_lit0 = 0;
9777 /* Don't introduce overflows through reassociation. */
9778 if (!any_overflows
9779 && ((lit0 && TREE_OVERFLOW_P (lit0))
9780 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9781 return NULL_TREE;
9783 if (minus_lit0)
9785 if (con0 == 0)
9786 return
9787 fold_convert_loc (loc, type,
9788 associate_trees (loc, var0, minus_lit0,
9789 MINUS_EXPR, atype));
9790 else
9792 con0 = associate_trees (loc, con0, minus_lit0,
9793 MINUS_EXPR, atype);
9794 return
9795 fold_convert_loc (loc, type,
9796 associate_trees (loc, var0, con0,
9797 PLUS_EXPR, atype));
9801 con0 = associate_trees (loc, con0, lit0, code, atype);
9802 return
9803 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9804 code, atype));
9808 return NULL_TREE;
9810 case MINUS_EXPR:
9811 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9812 if (TREE_CODE (arg0) == NEGATE_EXPR
9813 && negate_expr_p (op1)
9814 && reorder_operands_p (arg0, arg1))
9815 return fold_build2_loc (loc, MINUS_EXPR, type,
9816 negate_expr (op1),
9817 fold_convert_loc (loc, type,
9818 TREE_OPERAND (arg0, 0)));
9820 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9821 __complex__ ( x, -y ). This is not the same for SNaNs or if
9822 signed zeros are involved. */
9823 if (!HONOR_SNANS (element_mode (arg0))
9824 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9825 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9827 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9828 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9829 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9830 bool arg0rz = false, arg0iz = false;
9831 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9832 || (arg0i && (arg0iz = real_zerop (arg0i))))
9834 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9835 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9836 if (arg0rz && arg1i && real_zerop (arg1i))
9838 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9839 arg1r ? arg1r
9840 : build1 (REALPART_EXPR, rtype, arg1));
9841 tree ip = arg0i ? arg0i
9842 : build1 (IMAGPART_EXPR, rtype, arg0);
9843 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9845 else if (arg0iz && arg1r && real_zerop (arg1r))
9847 tree rp = arg0r ? arg0r
9848 : build1 (REALPART_EXPR, rtype, arg0);
9849 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9850 arg1i ? arg1i
9851 : build1 (IMAGPART_EXPR, rtype, arg1));
9852 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9857 /* A - B -> A + (-B) if B is easily negatable. */
9858 if (negate_expr_p (op1)
9859 && ! TYPE_OVERFLOW_SANITIZED (type)
9860 && ((FLOAT_TYPE_P (type)
9861 /* Avoid this transformation if B is a positive REAL_CST. */
9862 && (TREE_CODE (op1) != REAL_CST
9863 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9864 || INTEGRAL_TYPE_P (type)))
9865 return fold_build2_loc (loc, PLUS_EXPR, type,
9866 fold_convert_loc (loc, type, arg0),
9867 negate_expr (op1));
9869 /* Fold &a[i] - &a[j] to i-j. */
9870 if (TREE_CODE (arg0) == ADDR_EXPR
9871 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9872 && TREE_CODE (arg1) == ADDR_EXPR
9873 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9875 tree tem = fold_addr_of_array_ref_difference (loc, type,
9876 TREE_OPERAND (arg0, 0),
9877 TREE_OPERAND (arg1, 0));
9878 if (tem)
9879 return tem;
9882 if (FLOAT_TYPE_P (type)
9883 && flag_unsafe_math_optimizations
9884 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9885 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9886 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9887 return tem;
9889 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9890 one. Make sure the type is not saturating and has the signedness of
9891 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9892 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9893 if ((TREE_CODE (arg0) == MULT_EXPR
9894 || TREE_CODE (arg1) == MULT_EXPR)
9895 && !TYPE_SATURATING (type)
9896 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9897 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9898 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9900 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9901 if (tem)
9902 return tem;
9905 goto associate;
9907 case MULT_EXPR:
9908 if (! FLOAT_TYPE_P (type))
9910 /* Transform x * -C into -x * C if x is easily negatable. */
9911 if (TREE_CODE (op1) == INTEGER_CST
9912 && tree_int_cst_sgn (op1) == -1
9913 && negate_expr_p (op0)
9914 && (tem = negate_expr (op1)) != op1
9915 && ! TREE_OVERFLOW (tem))
9916 return fold_build2_loc (loc, MULT_EXPR, type,
9917 fold_convert_loc (loc, type,
9918 negate_expr (op0)), tem);
9920 strict_overflow_p = false;
9921 if (TREE_CODE (arg1) == INTEGER_CST
9922 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9923 &strict_overflow_p)))
9925 if (strict_overflow_p)
9926 fold_overflow_warning (("assuming signed overflow does not "
9927 "occur when simplifying "
9928 "multiplication"),
9929 WARN_STRICT_OVERFLOW_MISC);
9930 return fold_convert_loc (loc, type, tem);
9933 /* Optimize z * conj(z) for integer complex numbers. */
9934 if (TREE_CODE (arg0) == CONJ_EXPR
9935 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9936 return fold_mult_zconjz (loc, type, arg1);
9937 if (TREE_CODE (arg1) == CONJ_EXPR
9938 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9939 return fold_mult_zconjz (loc, type, arg0);
9941 else
9943 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9944 This is not the same for NaNs or if signed zeros are
9945 involved. */
9946 if (!HONOR_NANS (arg0)
9947 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9948 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9949 && TREE_CODE (arg1) == COMPLEX_CST
9950 && real_zerop (TREE_REALPART (arg1)))
9952 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9953 if (real_onep (TREE_IMAGPART (arg1)))
9954 return
9955 fold_build2_loc (loc, COMPLEX_EXPR, type,
9956 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9957 rtype, arg0)),
9958 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9959 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9960 return
9961 fold_build2_loc (loc, COMPLEX_EXPR, type,
9962 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9963 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9964 rtype, arg0)));
9967 /* Optimize z * conj(z) for floating point complex numbers.
9968 Guarded by flag_unsafe_math_optimizations as non-finite
9969 imaginary components don't produce scalar results. */
9970 if (flag_unsafe_math_optimizations
9971 && TREE_CODE (arg0) == CONJ_EXPR
9972 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9973 return fold_mult_zconjz (loc, type, arg1);
9974 if (flag_unsafe_math_optimizations
9975 && TREE_CODE (arg1) == CONJ_EXPR
9976 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9977 return fold_mult_zconjz (loc, type, arg0);
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) & -(1 << CST) to X * Y if Y is a constant
10085 multiple of 1 << CST. */
10086 if (TREE_CODE (arg1) == INTEGER_CST)
10088 wide_int cst1 = arg1;
10089 wide_int ncst1 = -cst1;
10090 if ((cst1 & ncst1) == ncst1
10091 && multiple_of_p (type, arg0,
10092 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10093 return fold_convert_loc (loc, type, arg0);
10096 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10097 bits from CST2. */
10098 if (TREE_CODE (arg1) == INTEGER_CST
10099 && TREE_CODE (arg0) == MULT_EXPR
10100 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10102 wide_int warg1 = arg1;
10103 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10105 if (masked == 0)
10106 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10107 arg0, arg1);
10108 else if (masked != warg1)
10110 /* Avoid the transform if arg1 is a mask of some
10111 mode which allows further optimizations. */
10112 int pop = wi::popcount (warg1);
10113 if (!(pop >= BITS_PER_UNIT
10114 && exact_log2 (pop) != -1
10115 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10116 return fold_build2_loc (loc, code, type, op0,
10117 wide_int_to_tree (type, masked));
10121 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10122 ((A & N) + B) & M -> (A + B) & M
10123 Similarly if (N & M) == 0,
10124 ((A | N) + B) & M -> (A + B) & M
10125 and for - instead of + (or unary - instead of +)
10126 and/or ^ instead of |.
10127 If B is constant and (B & M) == 0, fold into A & M. */
10128 if (TREE_CODE (arg1) == INTEGER_CST)
10130 wide_int cst1 = arg1;
10131 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10132 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10133 && (TREE_CODE (arg0) == PLUS_EXPR
10134 || TREE_CODE (arg0) == MINUS_EXPR
10135 || TREE_CODE (arg0) == NEGATE_EXPR)
10136 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10137 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10139 tree pmop[2];
10140 int which = 0;
10141 wide_int cst0;
10143 /* Now we know that arg0 is (C + D) or (C - D) or
10144 -C and arg1 (M) is == (1LL << cst) - 1.
10145 Store C into PMOP[0] and D into PMOP[1]. */
10146 pmop[0] = TREE_OPERAND (arg0, 0);
10147 pmop[1] = NULL;
10148 if (TREE_CODE (arg0) != NEGATE_EXPR)
10150 pmop[1] = TREE_OPERAND (arg0, 1);
10151 which = 1;
10154 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10155 which = -1;
10157 for (; which >= 0; which--)
10158 switch (TREE_CODE (pmop[which]))
10160 case BIT_AND_EXPR:
10161 case BIT_IOR_EXPR:
10162 case BIT_XOR_EXPR:
10163 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10164 != INTEGER_CST)
10165 break;
10166 cst0 = TREE_OPERAND (pmop[which], 1);
10167 cst0 &= cst1;
10168 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10170 if (cst0 != cst1)
10171 break;
10173 else if (cst0 != 0)
10174 break;
10175 /* If C or D is of the form (A & N) where
10176 (N & M) == M, or of the form (A | N) or
10177 (A ^ N) where (N & M) == 0, replace it with A. */
10178 pmop[which] = TREE_OPERAND (pmop[which], 0);
10179 break;
10180 case INTEGER_CST:
10181 /* If C or D is a N where (N & M) == 0, it can be
10182 omitted (assumed 0). */
10183 if ((TREE_CODE (arg0) == PLUS_EXPR
10184 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10185 && (cst1 & pmop[which]) == 0)
10186 pmop[which] = NULL;
10187 break;
10188 default:
10189 break;
10192 /* Only build anything new if we optimized one or both arguments
10193 above. */
10194 if (pmop[0] != TREE_OPERAND (arg0, 0)
10195 || (TREE_CODE (arg0) != NEGATE_EXPR
10196 && pmop[1] != TREE_OPERAND (arg0, 1)))
10198 tree utype = TREE_TYPE (arg0);
10199 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10201 /* Perform the operations in a type that has defined
10202 overflow behavior. */
10203 utype = unsigned_type_for (TREE_TYPE (arg0));
10204 if (pmop[0] != NULL)
10205 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10206 if (pmop[1] != NULL)
10207 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10210 if (TREE_CODE (arg0) == NEGATE_EXPR)
10211 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10212 else if (TREE_CODE (arg0) == PLUS_EXPR)
10214 if (pmop[0] != NULL && pmop[1] != NULL)
10215 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10216 pmop[0], pmop[1]);
10217 else if (pmop[0] != NULL)
10218 tem = pmop[0];
10219 else if (pmop[1] != NULL)
10220 tem = pmop[1];
10221 else
10222 return build_int_cst (type, 0);
10224 else if (pmop[0] == NULL)
10225 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10226 else
10227 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10228 pmop[0], pmop[1]);
10229 /* TEM is now the new binary +, - or unary - replacement. */
10230 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10231 fold_convert_loc (loc, utype, arg1));
10232 return fold_convert_loc (loc, type, tem);
10237 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10238 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10239 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10241 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10243 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10244 if (mask == -1)
10245 return
10246 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10249 goto associate;
10251 case RDIV_EXPR:
10252 /* Don't touch a floating-point divide by zero unless the mode
10253 of the constant can represent infinity. */
10254 if (TREE_CODE (arg1) == REAL_CST
10255 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10256 && real_zerop (arg1))
10257 return NULL_TREE;
10259 /* (-A) / (-B) -> A / B */
10260 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10261 return fold_build2_loc (loc, RDIV_EXPR, type,
10262 TREE_OPERAND (arg0, 0),
10263 negate_expr (arg1));
10264 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10265 return fold_build2_loc (loc, RDIV_EXPR, type,
10266 negate_expr (arg0),
10267 TREE_OPERAND (arg1, 0));
10268 return NULL_TREE;
10270 case TRUNC_DIV_EXPR:
10271 /* Fall through */
10273 case FLOOR_DIV_EXPR:
10274 /* Simplify A / (B << N) where A and B are positive and B is
10275 a power of 2, to A >> (N + log2(B)). */
10276 strict_overflow_p = false;
10277 if (TREE_CODE (arg1) == LSHIFT_EXPR
10278 && (TYPE_UNSIGNED (type)
10279 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10281 tree sval = TREE_OPERAND (arg1, 0);
10282 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10284 tree sh_cnt = TREE_OPERAND (arg1, 1);
10285 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10286 wi::exact_log2 (sval));
10288 if (strict_overflow_p)
10289 fold_overflow_warning (("assuming signed overflow does not "
10290 "occur when simplifying A / (B << N)"),
10291 WARN_STRICT_OVERFLOW_MISC);
10293 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10294 sh_cnt, pow2);
10295 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10296 fold_convert_loc (loc, type, arg0), sh_cnt);
10300 /* Fall through */
10302 case ROUND_DIV_EXPR:
10303 case CEIL_DIV_EXPR:
10304 case EXACT_DIV_EXPR:
10305 if (integer_zerop (arg1))
10306 return NULL_TREE;
10308 /* Convert -A / -B to A / B when the type is signed and overflow is
10309 undefined. */
10310 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10311 && TREE_CODE (arg0) == NEGATE_EXPR
10312 && negate_expr_p (op1))
10314 if (INTEGRAL_TYPE_P (type))
10315 fold_overflow_warning (("assuming signed overflow does not occur "
10316 "when distributing negation across "
10317 "division"),
10318 WARN_STRICT_OVERFLOW_MISC);
10319 return fold_build2_loc (loc, code, type,
10320 fold_convert_loc (loc, type,
10321 TREE_OPERAND (arg0, 0)),
10322 negate_expr (op1));
10324 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10325 && TREE_CODE (arg1) == NEGATE_EXPR
10326 && negate_expr_p (op0))
10328 if (INTEGRAL_TYPE_P (type))
10329 fold_overflow_warning (("assuming signed overflow does not occur "
10330 "when distributing negation across "
10331 "division"),
10332 WARN_STRICT_OVERFLOW_MISC);
10333 return fold_build2_loc (loc, code, type,
10334 negate_expr (op0),
10335 fold_convert_loc (loc, type,
10336 TREE_OPERAND (arg1, 0)));
10339 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10340 operation, EXACT_DIV_EXPR.
10342 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10343 At one time others generated faster code, it's not clear if they do
10344 after the last round to changes to the DIV code in expmed.c. */
10345 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10346 && multiple_of_p (type, arg0, arg1))
10347 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10348 fold_convert (type, arg0),
10349 fold_convert (type, arg1));
10351 strict_overflow_p = false;
10352 if (TREE_CODE (arg1) == INTEGER_CST
10353 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10354 &strict_overflow_p)))
10356 if (strict_overflow_p)
10357 fold_overflow_warning (("assuming signed overflow does not occur "
10358 "when simplifying division"),
10359 WARN_STRICT_OVERFLOW_MISC);
10360 return fold_convert_loc (loc, type, tem);
10363 return NULL_TREE;
10365 case CEIL_MOD_EXPR:
10366 case FLOOR_MOD_EXPR:
10367 case ROUND_MOD_EXPR:
10368 case TRUNC_MOD_EXPR:
10369 strict_overflow_p = false;
10370 if (TREE_CODE (arg1) == INTEGER_CST
10371 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10372 &strict_overflow_p)))
10374 if (strict_overflow_p)
10375 fold_overflow_warning (("assuming signed overflow does not occur "
10376 "when simplifying modulus"),
10377 WARN_STRICT_OVERFLOW_MISC);
10378 return fold_convert_loc (loc, type, tem);
10381 return NULL_TREE;
10383 case LROTATE_EXPR:
10384 case RROTATE_EXPR:
10385 case RSHIFT_EXPR:
10386 case LSHIFT_EXPR:
10387 /* Since negative shift count is not well-defined,
10388 don't try to compute it in the compiler. */
10389 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10390 return NULL_TREE;
10392 prec = element_precision (type);
10394 /* If we have a rotate of a bit operation with the rotate count and
10395 the second operand of the bit operation both constant,
10396 permute the two operations. */
10397 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10398 && (TREE_CODE (arg0) == BIT_AND_EXPR
10399 || TREE_CODE (arg0) == BIT_IOR_EXPR
10400 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10401 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10402 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10403 fold_build2_loc (loc, code, type,
10404 TREE_OPERAND (arg0, 0), arg1),
10405 fold_build2_loc (loc, code, type,
10406 TREE_OPERAND (arg0, 1), arg1));
10408 /* Two consecutive rotates adding up to the some integer
10409 multiple of the precision of the type can be ignored. */
10410 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10411 && TREE_CODE (arg0) == RROTATE_EXPR
10412 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10413 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10414 prec) == 0)
10415 return TREE_OPERAND (arg0, 0);
10417 return NULL_TREE;
10419 case MIN_EXPR:
10420 case MAX_EXPR:
10421 goto associate;
10423 case TRUTH_ANDIF_EXPR:
10424 /* Note that the operands of this must be ints
10425 and their values must be 0 or 1.
10426 ("true" is a fixed value perhaps depending on the language.) */
10427 /* If first arg is constant zero, return it. */
10428 if (integer_zerop (arg0))
10429 return fold_convert_loc (loc, type, arg0);
10430 case TRUTH_AND_EXPR:
10431 /* If either arg is constant true, drop it. */
10432 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10433 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10434 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10435 /* Preserve sequence points. */
10436 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10437 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10438 /* If second arg is constant zero, result is zero, but first arg
10439 must be evaluated. */
10440 if (integer_zerop (arg1))
10441 return omit_one_operand_loc (loc, type, arg1, arg0);
10442 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10443 case will be handled here. */
10444 if (integer_zerop (arg0))
10445 return omit_one_operand_loc (loc, type, arg0, arg1);
10447 /* !X && X is always false. */
10448 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10449 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10450 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10451 /* X && !X is always false. */
10452 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10453 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10454 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10456 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10457 means A >= Y && A != MAX, but in this case we know that
10458 A < X <= MAX. */
10460 if (!TREE_SIDE_EFFECTS (arg0)
10461 && !TREE_SIDE_EFFECTS (arg1))
10463 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10464 if (tem && !operand_equal_p (tem, arg0, 0))
10465 return fold_build2_loc (loc, code, type, tem, arg1);
10467 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10468 if (tem && !operand_equal_p (tem, arg1, 0))
10469 return fold_build2_loc (loc, code, type, arg0, tem);
10472 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10473 != NULL_TREE)
10474 return tem;
10476 return NULL_TREE;
10478 case TRUTH_ORIF_EXPR:
10479 /* Note that the operands of this must be ints
10480 and their values must be 0 or true.
10481 ("true" is a fixed value perhaps depending on the language.) */
10482 /* If first arg is constant true, return it. */
10483 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10484 return fold_convert_loc (loc, type, arg0);
10485 case TRUTH_OR_EXPR:
10486 /* If either arg is constant zero, drop it. */
10487 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10488 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10489 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10490 /* Preserve sequence points. */
10491 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10492 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10493 /* If second arg is constant true, result is true, but we must
10494 evaluate first arg. */
10495 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10496 return omit_one_operand_loc (loc, type, arg1, arg0);
10497 /* Likewise for first arg, but note this only occurs here for
10498 TRUTH_OR_EXPR. */
10499 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10500 return omit_one_operand_loc (loc, type, arg0, arg1);
10502 /* !X || X is always true. */
10503 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10504 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10505 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10506 /* X || !X is always true. */
10507 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10508 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10509 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10511 /* (X && !Y) || (!X && Y) is X ^ Y */
10512 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10513 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10515 tree a0, a1, l0, l1, n0, n1;
10517 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10518 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10520 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10521 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10523 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10524 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10526 if ((operand_equal_p (n0, a0, 0)
10527 && operand_equal_p (n1, a1, 0))
10528 || (operand_equal_p (n0, a1, 0)
10529 && operand_equal_p (n1, a0, 0)))
10530 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10533 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10534 != NULL_TREE)
10535 return tem;
10537 return NULL_TREE;
10539 case TRUTH_XOR_EXPR:
10540 /* If the second arg is constant zero, drop it. */
10541 if (integer_zerop (arg1))
10542 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10543 /* If the second arg is constant true, this is a logical inversion. */
10544 if (integer_onep (arg1))
10546 tem = invert_truthvalue_loc (loc, arg0);
10547 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10549 /* Identical arguments cancel to zero. */
10550 if (operand_equal_p (arg0, arg1, 0))
10551 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10553 /* !X ^ X is always true. */
10554 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10555 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10556 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10558 /* X ^ !X is always true. */
10559 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10560 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10561 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10563 return NULL_TREE;
10565 case EQ_EXPR:
10566 case NE_EXPR:
10567 STRIP_NOPS (arg0);
10568 STRIP_NOPS (arg1);
10570 tem = fold_comparison (loc, code, type, op0, op1);
10571 if (tem != NULL_TREE)
10572 return tem;
10574 /* bool_var != 1 becomes !bool_var. */
10575 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10576 && code == NE_EXPR)
10577 return fold_convert_loc (loc, type,
10578 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10579 TREE_TYPE (arg0), arg0));
10581 /* bool_var == 0 becomes !bool_var. */
10582 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10583 && code == EQ_EXPR)
10584 return fold_convert_loc (loc, type,
10585 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10586 TREE_TYPE (arg0), arg0));
10588 /* !exp != 0 becomes !exp */
10589 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10590 && code == NE_EXPR)
10591 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10593 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10594 if ((TREE_CODE (arg0) == PLUS_EXPR
10595 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10596 || TREE_CODE (arg0) == MINUS_EXPR)
10597 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10598 0)),
10599 arg1, 0)
10600 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10601 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10603 tree val = TREE_OPERAND (arg0, 1);
10604 val = fold_build2_loc (loc, code, type, val,
10605 build_int_cst (TREE_TYPE (val), 0));
10606 return omit_two_operands_loc (loc, type, val,
10607 TREE_OPERAND (arg0, 0), arg1);
10610 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10611 if ((TREE_CODE (arg1) == PLUS_EXPR
10612 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10613 || TREE_CODE (arg1) == MINUS_EXPR)
10614 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10615 0)),
10616 arg0, 0)
10617 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10618 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10620 tree val = TREE_OPERAND (arg1, 1);
10621 val = fold_build2_loc (loc, code, type, val,
10622 build_int_cst (TREE_TYPE (val), 0));
10623 return omit_two_operands_loc (loc, type, val,
10624 TREE_OPERAND (arg1, 0), arg0);
10627 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10628 if (TREE_CODE (arg0) == MINUS_EXPR
10629 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10630 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10631 1)),
10632 arg1, 0)
10633 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10634 return omit_two_operands_loc (loc, type,
10635 code == NE_EXPR
10636 ? boolean_true_node : boolean_false_node,
10637 TREE_OPERAND (arg0, 1), arg1);
10639 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10640 if (TREE_CODE (arg1) == MINUS_EXPR
10641 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10642 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10643 1)),
10644 arg0, 0)
10645 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10646 return omit_two_operands_loc (loc, type,
10647 code == NE_EXPR
10648 ? boolean_true_node : boolean_false_node,
10649 TREE_OPERAND (arg1, 1), arg0);
10651 /* If this is an EQ or NE comparison with zero and ARG0 is
10652 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10653 two operations, but the latter can be done in one less insn
10654 on machines that have only two-operand insns or on which a
10655 constant cannot be the first operand. */
10656 if (TREE_CODE (arg0) == BIT_AND_EXPR
10657 && integer_zerop (arg1))
10659 tree arg00 = TREE_OPERAND (arg0, 0);
10660 tree arg01 = TREE_OPERAND (arg0, 1);
10661 if (TREE_CODE (arg00) == LSHIFT_EXPR
10662 && integer_onep (TREE_OPERAND (arg00, 0)))
10664 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10665 arg01, TREE_OPERAND (arg00, 1));
10666 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10667 build_int_cst (TREE_TYPE (arg0), 1));
10668 return fold_build2_loc (loc, code, type,
10669 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10670 arg1);
10672 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10673 && integer_onep (TREE_OPERAND (arg01, 0)))
10675 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10676 arg00, TREE_OPERAND (arg01, 1));
10677 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10678 build_int_cst (TREE_TYPE (arg0), 1));
10679 return fold_build2_loc (loc, code, type,
10680 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10681 arg1);
10685 /* If this is an NE or EQ comparison of zero against the result of a
10686 signed MOD operation whose second operand is a power of 2, make
10687 the MOD operation unsigned since it is simpler and equivalent. */
10688 if (integer_zerop (arg1)
10689 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10690 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10691 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10692 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10693 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10694 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10696 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10697 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10698 fold_convert_loc (loc, newtype,
10699 TREE_OPERAND (arg0, 0)),
10700 fold_convert_loc (loc, newtype,
10701 TREE_OPERAND (arg0, 1)));
10703 return fold_build2_loc (loc, code, type, newmod,
10704 fold_convert_loc (loc, newtype, arg1));
10707 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10708 C1 is a valid shift constant, and C2 is a power of two, i.e.
10709 a single bit. */
10710 if (TREE_CODE (arg0) == BIT_AND_EXPR
10711 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10712 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10713 == INTEGER_CST
10714 && integer_pow2p (TREE_OPERAND (arg0, 1))
10715 && integer_zerop (arg1))
10717 tree itype = TREE_TYPE (arg0);
10718 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10719 prec = TYPE_PRECISION (itype);
10721 /* Check for a valid shift count. */
10722 if (wi::ltu_p (arg001, prec))
10724 tree arg01 = TREE_OPERAND (arg0, 1);
10725 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10726 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10727 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10728 can be rewritten as (X & (C2 << C1)) != 0. */
10729 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10731 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10732 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10733 return fold_build2_loc (loc, code, type, tem,
10734 fold_convert_loc (loc, itype, arg1));
10736 /* Otherwise, for signed (arithmetic) shifts,
10737 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10738 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10739 else if (!TYPE_UNSIGNED (itype))
10740 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10741 arg000, build_int_cst (itype, 0));
10742 /* Otherwise, of unsigned (logical) shifts,
10743 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10744 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10745 else
10746 return omit_one_operand_loc (loc, type,
10747 code == EQ_EXPR ? integer_one_node
10748 : integer_zero_node,
10749 arg000);
10753 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10754 Similarly for NE_EXPR. */
10755 if (TREE_CODE (arg0) == BIT_AND_EXPR
10756 && TREE_CODE (arg1) == INTEGER_CST
10757 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10759 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10760 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10761 TREE_OPERAND (arg0, 1));
10762 tree dandnotc
10763 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10764 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10765 notc);
10766 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10767 if (integer_nonzerop (dandnotc))
10768 return omit_one_operand_loc (loc, type, rslt, arg0);
10771 /* If this is a comparison of a field, we may be able to simplify it. */
10772 if ((TREE_CODE (arg0) == COMPONENT_REF
10773 || TREE_CODE (arg0) == BIT_FIELD_REF)
10774 /* Handle the constant case even without -O
10775 to make sure the warnings are given. */
10776 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10778 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10779 if (t1)
10780 return t1;
10783 /* Optimize comparisons of strlen vs zero to a compare of the
10784 first character of the string vs zero. To wit,
10785 strlen(ptr) == 0 => *ptr == 0
10786 strlen(ptr) != 0 => *ptr != 0
10787 Other cases should reduce to one of these two (or a constant)
10788 due to the return value of strlen being unsigned. */
10789 if (TREE_CODE (arg0) == CALL_EXPR
10790 && integer_zerop (arg1))
10792 tree fndecl = get_callee_fndecl (arg0);
10794 if (fndecl
10795 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10796 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10797 && call_expr_nargs (arg0) == 1
10798 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10800 tree iref = build_fold_indirect_ref_loc (loc,
10801 CALL_EXPR_ARG (arg0, 0));
10802 return fold_build2_loc (loc, code, type, iref,
10803 build_int_cst (TREE_TYPE (iref), 0));
10807 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10808 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10809 if (TREE_CODE (arg0) == RSHIFT_EXPR
10810 && integer_zerop (arg1)
10811 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10813 tree arg00 = TREE_OPERAND (arg0, 0);
10814 tree arg01 = TREE_OPERAND (arg0, 1);
10815 tree itype = TREE_TYPE (arg00);
10816 if (wi::eq_p (arg01, element_precision (itype) - 1))
10818 if (TYPE_UNSIGNED (itype))
10820 itype = signed_type_for (itype);
10821 arg00 = fold_convert_loc (loc, itype, arg00);
10823 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10824 type, arg00, build_zero_cst (itype));
10828 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10829 (X & C) == 0 when C is a single bit. */
10830 if (TREE_CODE (arg0) == BIT_AND_EXPR
10831 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10832 && integer_zerop (arg1)
10833 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10835 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10836 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10837 TREE_OPERAND (arg0, 1));
10838 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10839 type, tem,
10840 fold_convert_loc (loc, TREE_TYPE (arg0),
10841 arg1));
10844 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10845 constant C is a power of two, i.e. a single bit. */
10846 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10847 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10848 && integer_zerop (arg1)
10849 && integer_pow2p (TREE_OPERAND (arg0, 1))
10850 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10851 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10853 tree arg00 = TREE_OPERAND (arg0, 0);
10854 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10855 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10858 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10859 when is C is a power of two, i.e. a single bit. */
10860 if (TREE_CODE (arg0) == BIT_AND_EXPR
10861 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10862 && integer_zerop (arg1)
10863 && integer_pow2p (TREE_OPERAND (arg0, 1))
10864 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10865 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10867 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10868 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10869 arg000, TREE_OPERAND (arg0, 1));
10870 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10871 tem, build_int_cst (TREE_TYPE (tem), 0));
10874 if (integer_zerop (arg1)
10875 && tree_expr_nonzero_p (arg0))
10877 tree res = constant_boolean_node (code==NE_EXPR, type);
10878 return omit_one_operand_loc (loc, type, res, arg0);
10881 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10882 if (TREE_CODE (arg0) == BIT_AND_EXPR
10883 && TREE_CODE (arg1) == BIT_AND_EXPR)
10885 tree arg00 = TREE_OPERAND (arg0, 0);
10886 tree arg01 = TREE_OPERAND (arg0, 1);
10887 tree arg10 = TREE_OPERAND (arg1, 0);
10888 tree arg11 = TREE_OPERAND (arg1, 1);
10889 tree itype = TREE_TYPE (arg0);
10891 if (operand_equal_p (arg01, arg11, 0))
10892 return fold_build2_loc (loc, code, type,
10893 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10894 fold_build2_loc (loc,
10895 BIT_XOR_EXPR, itype,
10896 arg00, arg10),
10897 arg01),
10898 build_zero_cst (itype));
10900 if (operand_equal_p (arg01, arg10, 0))
10901 return fold_build2_loc (loc, code, type,
10902 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10903 fold_build2_loc (loc,
10904 BIT_XOR_EXPR, itype,
10905 arg00, arg11),
10906 arg01),
10907 build_zero_cst (itype));
10909 if (operand_equal_p (arg00, arg11, 0))
10910 return fold_build2_loc (loc, code, type,
10911 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10912 fold_build2_loc (loc,
10913 BIT_XOR_EXPR, itype,
10914 arg01, arg10),
10915 arg00),
10916 build_zero_cst (itype));
10918 if (operand_equal_p (arg00, arg10, 0))
10919 return fold_build2_loc (loc, code, type,
10920 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10921 fold_build2_loc (loc,
10922 BIT_XOR_EXPR, itype,
10923 arg01, arg11),
10924 arg00),
10925 build_zero_cst (itype));
10928 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10929 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10931 tree arg00 = TREE_OPERAND (arg0, 0);
10932 tree arg01 = TREE_OPERAND (arg0, 1);
10933 tree arg10 = TREE_OPERAND (arg1, 0);
10934 tree arg11 = TREE_OPERAND (arg1, 1);
10935 tree itype = TREE_TYPE (arg0);
10937 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10938 operand_equal_p guarantees no side-effects so we don't need
10939 to use omit_one_operand on Z. */
10940 if (operand_equal_p (arg01, arg11, 0))
10941 return fold_build2_loc (loc, code, type, arg00,
10942 fold_convert_loc (loc, TREE_TYPE (arg00),
10943 arg10));
10944 if (operand_equal_p (arg01, arg10, 0))
10945 return fold_build2_loc (loc, code, type, arg00,
10946 fold_convert_loc (loc, TREE_TYPE (arg00),
10947 arg11));
10948 if (operand_equal_p (arg00, arg11, 0))
10949 return fold_build2_loc (loc, code, type, arg01,
10950 fold_convert_loc (loc, TREE_TYPE (arg01),
10951 arg10));
10952 if (operand_equal_p (arg00, arg10, 0))
10953 return fold_build2_loc (loc, code, type, arg01,
10954 fold_convert_loc (loc, TREE_TYPE (arg01),
10955 arg11));
10957 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10958 if (TREE_CODE (arg01) == INTEGER_CST
10959 && TREE_CODE (arg11) == INTEGER_CST)
10961 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10962 fold_convert_loc (loc, itype, arg11));
10963 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10964 return fold_build2_loc (loc, code, type, tem,
10965 fold_convert_loc (loc, itype, arg10));
10969 /* Attempt to simplify equality/inequality comparisons of complex
10970 values. Only lower the comparison if the result is known or
10971 can be simplified to a single scalar comparison. */
10972 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10973 || TREE_CODE (arg0) == COMPLEX_CST)
10974 && (TREE_CODE (arg1) == COMPLEX_EXPR
10975 || TREE_CODE (arg1) == COMPLEX_CST))
10977 tree real0, imag0, real1, imag1;
10978 tree rcond, icond;
10980 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10982 real0 = TREE_OPERAND (arg0, 0);
10983 imag0 = TREE_OPERAND (arg0, 1);
10985 else
10987 real0 = TREE_REALPART (arg0);
10988 imag0 = TREE_IMAGPART (arg0);
10991 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10993 real1 = TREE_OPERAND (arg1, 0);
10994 imag1 = TREE_OPERAND (arg1, 1);
10996 else
10998 real1 = TREE_REALPART (arg1);
10999 imag1 = TREE_IMAGPART (arg1);
11002 rcond = fold_binary_loc (loc, code, type, real0, real1);
11003 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11005 if (integer_zerop (rcond))
11007 if (code == EQ_EXPR)
11008 return omit_two_operands_loc (loc, type, boolean_false_node,
11009 imag0, imag1);
11010 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11012 else
11014 if (code == NE_EXPR)
11015 return omit_two_operands_loc (loc, type, boolean_true_node,
11016 imag0, imag1);
11017 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11021 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11022 if (icond && TREE_CODE (icond) == INTEGER_CST)
11024 if (integer_zerop (icond))
11026 if (code == EQ_EXPR)
11027 return omit_two_operands_loc (loc, type, boolean_false_node,
11028 real0, real1);
11029 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11031 else
11033 if (code == NE_EXPR)
11034 return omit_two_operands_loc (loc, type, boolean_true_node,
11035 real0, real1);
11036 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11041 return NULL_TREE;
11043 case LT_EXPR:
11044 case GT_EXPR:
11045 case LE_EXPR:
11046 case GE_EXPR:
11047 tem = fold_comparison (loc, code, type, op0, op1);
11048 if (tem != NULL_TREE)
11049 return tem;
11051 /* Transform comparisons of the form X +- C CMP X. */
11052 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11053 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11054 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11055 && !HONOR_SNANS (arg0))
11056 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11057 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11059 tree arg01 = TREE_OPERAND (arg0, 1);
11060 enum tree_code code0 = TREE_CODE (arg0);
11061 int is_positive;
11063 if (TREE_CODE (arg01) == REAL_CST)
11064 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11065 else
11066 is_positive = tree_int_cst_sgn (arg01);
11068 /* (X - c) > X becomes false. */
11069 if (code == GT_EXPR
11070 && ((code0 == MINUS_EXPR && is_positive >= 0)
11071 || (code0 == PLUS_EXPR && is_positive <= 0)))
11073 if (TREE_CODE (arg01) == INTEGER_CST
11074 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11075 fold_overflow_warning (("assuming signed overflow does not "
11076 "occur when assuming that (X - c) > X "
11077 "is always false"),
11078 WARN_STRICT_OVERFLOW_ALL);
11079 return constant_boolean_node (0, type);
11082 /* Likewise (X + c) < X becomes false. */
11083 if (code == LT_EXPR
11084 && ((code0 == PLUS_EXPR && is_positive >= 0)
11085 || (code0 == MINUS_EXPR && is_positive <= 0)))
11087 if (TREE_CODE (arg01) == INTEGER_CST
11088 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11089 fold_overflow_warning (("assuming signed overflow does not "
11090 "occur when assuming that "
11091 "(X + c) < X is always false"),
11092 WARN_STRICT_OVERFLOW_ALL);
11093 return constant_boolean_node (0, type);
11096 /* Convert (X - c) <= X to true. */
11097 if (!HONOR_NANS (arg1)
11098 && code == LE_EXPR
11099 && ((code0 == MINUS_EXPR && is_positive >= 0)
11100 || (code0 == PLUS_EXPR && is_positive <= 0)))
11102 if (TREE_CODE (arg01) == INTEGER_CST
11103 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11104 fold_overflow_warning (("assuming signed overflow does not "
11105 "occur when assuming that "
11106 "(X - c) <= X is always true"),
11107 WARN_STRICT_OVERFLOW_ALL);
11108 return constant_boolean_node (1, type);
11111 /* Convert (X + c) >= X to true. */
11112 if (!HONOR_NANS (arg1)
11113 && code == GE_EXPR
11114 && ((code0 == PLUS_EXPR && is_positive >= 0)
11115 || (code0 == MINUS_EXPR && is_positive <= 0)))
11117 if (TREE_CODE (arg01) == INTEGER_CST
11118 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11119 fold_overflow_warning (("assuming signed overflow does not "
11120 "occur when assuming that "
11121 "(X + c) >= X is always true"),
11122 WARN_STRICT_OVERFLOW_ALL);
11123 return constant_boolean_node (1, type);
11126 if (TREE_CODE (arg01) == INTEGER_CST)
11128 /* Convert X + c > X and X - c < X to true for integers. */
11129 if (code == GT_EXPR
11130 && ((code0 == PLUS_EXPR && is_positive > 0)
11131 || (code0 == MINUS_EXPR && is_positive < 0)))
11133 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11134 fold_overflow_warning (("assuming signed overflow does "
11135 "not occur when assuming that "
11136 "(X + c) > X is always true"),
11137 WARN_STRICT_OVERFLOW_ALL);
11138 return constant_boolean_node (1, type);
11141 if (code == LT_EXPR
11142 && ((code0 == MINUS_EXPR && is_positive > 0)
11143 || (code0 == PLUS_EXPR && is_positive < 0)))
11145 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11146 fold_overflow_warning (("assuming signed overflow does "
11147 "not occur when assuming that "
11148 "(X - c) < X is always true"),
11149 WARN_STRICT_OVERFLOW_ALL);
11150 return constant_boolean_node (1, type);
11153 /* Convert X + c <= X and X - c >= X to false for integers. */
11154 if (code == LE_EXPR
11155 && ((code0 == PLUS_EXPR && is_positive > 0)
11156 || (code0 == MINUS_EXPR && is_positive < 0)))
11158 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11159 fold_overflow_warning (("assuming signed overflow does "
11160 "not occur when assuming that "
11161 "(X + c) <= X is always false"),
11162 WARN_STRICT_OVERFLOW_ALL);
11163 return constant_boolean_node (0, type);
11166 if (code == GE_EXPR
11167 && ((code0 == MINUS_EXPR && is_positive > 0)
11168 || (code0 == PLUS_EXPR && is_positive < 0)))
11170 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11171 fold_overflow_warning (("assuming signed overflow does "
11172 "not occur when assuming that "
11173 "(X - c) >= X is always false"),
11174 WARN_STRICT_OVERFLOW_ALL);
11175 return constant_boolean_node (0, type);
11180 /* If we are comparing an ABS_EXPR with a constant, we can
11181 convert all the cases into explicit comparisons, but they may
11182 well not be faster than doing the ABS and one comparison.
11183 But ABS (X) <= C is a range comparison, which becomes a subtraction
11184 and a comparison, and is probably faster. */
11185 if (code == LE_EXPR
11186 && TREE_CODE (arg1) == INTEGER_CST
11187 && TREE_CODE (arg0) == ABS_EXPR
11188 && ! TREE_SIDE_EFFECTS (arg0)
11189 && (0 != (tem = negate_expr (arg1)))
11190 && TREE_CODE (tem) == INTEGER_CST
11191 && !TREE_OVERFLOW (tem))
11192 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11193 build2 (GE_EXPR, type,
11194 TREE_OPERAND (arg0, 0), tem),
11195 build2 (LE_EXPR, type,
11196 TREE_OPERAND (arg0, 0), arg1));
11198 /* Convert ABS_EXPR<x> >= 0 to true. */
11199 strict_overflow_p = false;
11200 if (code == GE_EXPR
11201 && (integer_zerop (arg1)
11202 || (! HONOR_NANS (arg0)
11203 && real_zerop (arg1)))
11204 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11206 if (strict_overflow_p)
11207 fold_overflow_warning (("assuming signed overflow does not occur "
11208 "when simplifying comparison of "
11209 "absolute value and zero"),
11210 WARN_STRICT_OVERFLOW_CONDITIONAL);
11211 return omit_one_operand_loc (loc, type,
11212 constant_boolean_node (true, type),
11213 arg0);
11216 /* Convert ABS_EXPR<x> < 0 to false. */
11217 strict_overflow_p = false;
11218 if (code == LT_EXPR
11219 && (integer_zerop (arg1) || real_zerop (arg1))
11220 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11222 if (strict_overflow_p)
11223 fold_overflow_warning (("assuming signed overflow does not occur "
11224 "when simplifying comparison of "
11225 "absolute value and zero"),
11226 WARN_STRICT_OVERFLOW_CONDITIONAL);
11227 return omit_one_operand_loc (loc, type,
11228 constant_boolean_node (false, type),
11229 arg0);
11232 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11233 and similarly for >= into !=. */
11234 if ((code == LT_EXPR || code == GE_EXPR)
11235 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11236 && TREE_CODE (arg1) == LSHIFT_EXPR
11237 && integer_onep (TREE_OPERAND (arg1, 0)))
11238 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11239 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11240 TREE_OPERAND (arg1, 1)),
11241 build_zero_cst (TREE_TYPE (arg0)));
11243 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11244 otherwise Y might be >= # of bits in X's type and thus e.g.
11245 (unsigned char) (1 << Y) for Y 15 might be 0.
11246 If the cast is widening, then 1 << Y should have unsigned type,
11247 otherwise if Y is number of bits in the signed shift type minus 1,
11248 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11249 31 might be 0xffffffff80000000. */
11250 if ((code == LT_EXPR || code == GE_EXPR)
11251 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11252 && CONVERT_EXPR_P (arg1)
11253 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11254 && (element_precision (TREE_TYPE (arg1))
11255 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11256 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11257 || (element_precision (TREE_TYPE (arg1))
11258 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11259 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11261 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11262 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11263 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11264 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11265 build_zero_cst (TREE_TYPE (arg0)));
11268 return NULL_TREE;
11270 case UNORDERED_EXPR:
11271 case ORDERED_EXPR:
11272 case UNLT_EXPR:
11273 case UNLE_EXPR:
11274 case UNGT_EXPR:
11275 case UNGE_EXPR:
11276 case UNEQ_EXPR:
11277 case LTGT_EXPR:
11278 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11280 tree targ0 = strip_float_extensions (arg0);
11281 tree targ1 = strip_float_extensions (arg1);
11282 tree newtype = TREE_TYPE (targ0);
11284 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11285 newtype = TREE_TYPE (targ1);
11287 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11288 return fold_build2_loc (loc, code, type,
11289 fold_convert_loc (loc, newtype, targ0),
11290 fold_convert_loc (loc, newtype, targ1));
11293 return NULL_TREE;
11295 case COMPOUND_EXPR:
11296 /* When pedantic, a compound expression can be neither an lvalue
11297 nor an integer constant expression. */
11298 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11299 return NULL_TREE;
11300 /* Don't let (0, 0) be null pointer constant. */
11301 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11302 : fold_convert_loc (loc, type, arg1);
11303 return pedantic_non_lvalue_loc (loc, tem);
11305 case ASSERT_EXPR:
11306 /* An ASSERT_EXPR should never be passed to fold_binary. */
11307 gcc_unreachable ();
11309 default:
11310 return NULL_TREE;
11311 } /* switch (code) */
11314 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11315 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11316 of GOTO_EXPR. */
11318 static tree
11319 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11321 switch (TREE_CODE (*tp))
11323 case LABEL_EXPR:
11324 return *tp;
11326 case GOTO_EXPR:
11327 *walk_subtrees = 0;
11329 /* ... fall through ... */
11331 default:
11332 return NULL_TREE;
11336 /* Return whether the sub-tree ST contains a label which is accessible from
11337 outside the sub-tree. */
11339 static bool
11340 contains_label_p (tree st)
11342 return
11343 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11346 /* Fold a ternary expression of code CODE and type TYPE with operands
11347 OP0, OP1, and OP2. Return the folded expression if folding is
11348 successful. Otherwise, return NULL_TREE. */
11350 tree
11351 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11352 tree op0, tree op1, tree op2)
11354 tree tem;
11355 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11356 enum tree_code_class kind = TREE_CODE_CLASS (code);
11358 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11359 && TREE_CODE_LENGTH (code) == 3);
11361 /* If this is a commutative operation, and OP0 is a constant, move it
11362 to OP1 to reduce the number of tests below. */
11363 if (commutative_ternary_tree_code (code)
11364 && tree_swap_operands_p (op0, op1, true))
11365 return fold_build3_loc (loc, code, type, op1, op0, op2);
11367 tem = generic_simplify (loc, code, type, op0, op1, op2);
11368 if (tem)
11369 return tem;
11371 /* Strip any conversions that don't change the mode. This is safe
11372 for every expression, except for a comparison expression because
11373 its signedness is derived from its operands. So, in the latter
11374 case, only strip conversions that don't change the signedness.
11376 Note that this is done as an internal manipulation within the
11377 constant folder, in order to find the simplest representation of
11378 the arguments so that their form can be studied. In any cases,
11379 the appropriate type conversions should be put back in the tree
11380 that will get out of the constant folder. */
11381 if (op0)
11383 arg0 = op0;
11384 STRIP_NOPS (arg0);
11387 if (op1)
11389 arg1 = op1;
11390 STRIP_NOPS (arg1);
11393 if (op2)
11395 arg2 = op2;
11396 STRIP_NOPS (arg2);
11399 switch (code)
11401 case COMPONENT_REF:
11402 if (TREE_CODE (arg0) == CONSTRUCTOR
11403 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11405 unsigned HOST_WIDE_INT idx;
11406 tree field, value;
11407 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11408 if (field == arg1)
11409 return value;
11411 return NULL_TREE;
11413 case COND_EXPR:
11414 case VEC_COND_EXPR:
11415 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11416 so all simple results must be passed through pedantic_non_lvalue. */
11417 if (TREE_CODE (arg0) == INTEGER_CST)
11419 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11420 tem = integer_zerop (arg0) ? op2 : op1;
11421 /* Only optimize constant conditions when the selected branch
11422 has the same type as the COND_EXPR. This avoids optimizing
11423 away "c ? x : throw", where the throw has a void type.
11424 Avoid throwing away that operand which contains label. */
11425 if ((!TREE_SIDE_EFFECTS (unused_op)
11426 || !contains_label_p (unused_op))
11427 && (! VOID_TYPE_P (TREE_TYPE (tem))
11428 || VOID_TYPE_P (type)))
11429 return pedantic_non_lvalue_loc (loc, tem);
11430 return NULL_TREE;
11432 else if (TREE_CODE (arg0) == VECTOR_CST)
11434 if ((TREE_CODE (arg1) == VECTOR_CST
11435 || TREE_CODE (arg1) == CONSTRUCTOR)
11436 && (TREE_CODE (arg2) == VECTOR_CST
11437 || TREE_CODE (arg2) == CONSTRUCTOR))
11439 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11440 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11441 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11442 for (i = 0; i < nelts; i++)
11444 tree val = VECTOR_CST_ELT (arg0, i);
11445 if (integer_all_onesp (val))
11446 sel[i] = i;
11447 else if (integer_zerop (val))
11448 sel[i] = nelts + i;
11449 else /* Currently unreachable. */
11450 return NULL_TREE;
11452 tree t = fold_vec_perm (type, arg1, arg2, sel);
11453 if (t != NULL_TREE)
11454 return t;
11458 /* If we have A op B ? A : C, we may be able to convert this to a
11459 simpler expression, depending on the operation and the values
11460 of B and C. Signed zeros prevent all of these transformations,
11461 for reasons given above each one.
11463 Also try swapping the arguments and inverting the conditional. */
11464 if (COMPARISON_CLASS_P (arg0)
11465 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11466 arg1, TREE_OPERAND (arg0, 1))
11467 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11469 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11470 if (tem)
11471 return tem;
11474 if (COMPARISON_CLASS_P (arg0)
11475 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11476 op2,
11477 TREE_OPERAND (arg0, 1))
11478 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11480 location_t loc0 = expr_location_or (arg0, loc);
11481 tem = fold_invert_truthvalue (loc0, arg0);
11482 if (tem && COMPARISON_CLASS_P (tem))
11484 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11485 if (tem)
11486 return tem;
11490 /* If the second operand is simpler than the third, swap them
11491 since that produces better jump optimization results. */
11492 if (truth_value_p (TREE_CODE (arg0))
11493 && tree_swap_operands_p (op1, op2, false))
11495 location_t loc0 = expr_location_or (arg0, loc);
11496 /* See if this can be inverted. If it can't, possibly because
11497 it was a floating-point inequality comparison, don't do
11498 anything. */
11499 tem = fold_invert_truthvalue (loc0, arg0);
11500 if (tem)
11501 return fold_build3_loc (loc, code, type, tem, op2, op1);
11504 /* Convert A ? 1 : 0 to simply A. */
11505 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11506 : (integer_onep (op1)
11507 && !VECTOR_TYPE_P (type)))
11508 && integer_zerop (op2)
11509 /* If we try to convert OP0 to our type, the
11510 call to fold will try to move the conversion inside
11511 a COND, which will recurse. In that case, the COND_EXPR
11512 is probably the best choice, so leave it alone. */
11513 && type == TREE_TYPE (arg0))
11514 return pedantic_non_lvalue_loc (loc, arg0);
11516 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11517 over COND_EXPR in cases such as floating point comparisons. */
11518 if (integer_zerop (op1)
11519 && code == COND_EXPR
11520 && integer_onep (op2)
11521 && !VECTOR_TYPE_P (type)
11522 && truth_value_p (TREE_CODE (arg0)))
11523 return pedantic_non_lvalue_loc (loc,
11524 fold_convert_loc (loc, type,
11525 invert_truthvalue_loc (loc,
11526 arg0)));
11528 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11529 if (TREE_CODE (arg0) == LT_EXPR
11530 && integer_zerop (TREE_OPERAND (arg0, 1))
11531 && integer_zerop (op2)
11532 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11534 /* sign_bit_p looks through both zero and sign extensions,
11535 but for this optimization only sign extensions are
11536 usable. */
11537 tree tem2 = TREE_OPERAND (arg0, 0);
11538 while (tem != tem2)
11540 if (TREE_CODE (tem2) != NOP_EXPR
11541 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11543 tem = NULL_TREE;
11544 break;
11546 tem2 = TREE_OPERAND (tem2, 0);
11548 /* sign_bit_p only checks ARG1 bits within A's precision.
11549 If <sign bit of A> has wider type than A, bits outside
11550 of A's precision in <sign bit of A> need to be checked.
11551 If they are all 0, this optimization needs to be done
11552 in unsigned A's type, if they are all 1 in signed A's type,
11553 otherwise this can't be done. */
11554 if (tem
11555 && TYPE_PRECISION (TREE_TYPE (tem))
11556 < TYPE_PRECISION (TREE_TYPE (arg1))
11557 && TYPE_PRECISION (TREE_TYPE (tem))
11558 < TYPE_PRECISION (type))
11560 int inner_width, outer_width;
11561 tree tem_type;
11563 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11564 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11565 if (outer_width > TYPE_PRECISION (type))
11566 outer_width = TYPE_PRECISION (type);
11568 wide_int mask = wi::shifted_mask
11569 (inner_width, outer_width - inner_width, false,
11570 TYPE_PRECISION (TREE_TYPE (arg1)));
11572 wide_int common = mask & arg1;
11573 if (common == mask)
11575 tem_type = signed_type_for (TREE_TYPE (tem));
11576 tem = fold_convert_loc (loc, tem_type, tem);
11578 else if (common == 0)
11580 tem_type = unsigned_type_for (TREE_TYPE (tem));
11581 tem = fold_convert_loc (loc, tem_type, tem);
11583 else
11584 tem = NULL;
11587 if (tem)
11588 return
11589 fold_convert_loc (loc, type,
11590 fold_build2_loc (loc, BIT_AND_EXPR,
11591 TREE_TYPE (tem), tem,
11592 fold_convert_loc (loc,
11593 TREE_TYPE (tem),
11594 arg1)));
11597 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11598 already handled above. */
11599 if (TREE_CODE (arg0) == BIT_AND_EXPR
11600 && integer_onep (TREE_OPERAND (arg0, 1))
11601 && integer_zerop (op2)
11602 && integer_pow2p (arg1))
11604 tree tem = TREE_OPERAND (arg0, 0);
11605 STRIP_NOPS (tem);
11606 if (TREE_CODE (tem) == RSHIFT_EXPR
11607 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11608 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11609 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11610 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11611 TREE_OPERAND (tem, 0), arg1);
11614 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11615 is probably obsolete because the first operand should be a
11616 truth value (that's why we have the two cases above), but let's
11617 leave it in until we can confirm this for all front-ends. */
11618 if (integer_zerop (op2)
11619 && TREE_CODE (arg0) == NE_EXPR
11620 && integer_zerop (TREE_OPERAND (arg0, 1))
11621 && integer_pow2p (arg1)
11622 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11623 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11624 arg1, OEP_ONLY_CONST))
11625 return pedantic_non_lvalue_loc (loc,
11626 fold_convert_loc (loc, type,
11627 TREE_OPERAND (arg0, 0)));
11629 /* Disable the transformations below for vectors, since
11630 fold_binary_op_with_conditional_arg may undo them immediately,
11631 yielding an infinite loop. */
11632 if (code == VEC_COND_EXPR)
11633 return NULL_TREE;
11635 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11636 if (integer_zerop (op2)
11637 && truth_value_p (TREE_CODE (arg0))
11638 && truth_value_p (TREE_CODE (arg1))
11639 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11640 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11641 : TRUTH_ANDIF_EXPR,
11642 type, fold_convert_loc (loc, type, arg0), arg1);
11644 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11645 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11646 && truth_value_p (TREE_CODE (arg0))
11647 && truth_value_p (TREE_CODE (arg1))
11648 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11650 location_t loc0 = expr_location_or (arg0, loc);
11651 /* Only perform transformation if ARG0 is easily inverted. */
11652 tem = fold_invert_truthvalue (loc0, arg0);
11653 if (tem)
11654 return fold_build2_loc (loc, code == VEC_COND_EXPR
11655 ? BIT_IOR_EXPR
11656 : TRUTH_ORIF_EXPR,
11657 type, fold_convert_loc (loc, type, tem),
11658 arg1);
11661 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11662 if (integer_zerop (arg1)
11663 && truth_value_p (TREE_CODE (arg0))
11664 && truth_value_p (TREE_CODE (op2))
11665 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11667 location_t loc0 = expr_location_or (arg0, loc);
11668 /* Only perform transformation if ARG0 is easily inverted. */
11669 tem = fold_invert_truthvalue (loc0, arg0);
11670 if (tem)
11671 return fold_build2_loc (loc, code == VEC_COND_EXPR
11672 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11673 type, fold_convert_loc (loc, type, tem),
11674 op2);
11677 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11678 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11679 && truth_value_p (TREE_CODE (arg0))
11680 && truth_value_p (TREE_CODE (op2))
11681 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11682 return fold_build2_loc (loc, code == VEC_COND_EXPR
11683 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11684 type, fold_convert_loc (loc, type, arg0), op2);
11686 return NULL_TREE;
11688 case CALL_EXPR:
11689 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11690 of fold_ternary on them. */
11691 gcc_unreachable ();
11693 case BIT_FIELD_REF:
11694 if (TREE_CODE (arg0) == VECTOR_CST
11695 && (type == TREE_TYPE (TREE_TYPE (arg0))
11696 || (TREE_CODE (type) == VECTOR_TYPE
11697 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11699 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11700 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11701 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11702 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11704 if (n != 0
11705 && (idx % width) == 0
11706 && (n % width) == 0
11707 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11709 idx = idx / width;
11710 n = n / width;
11712 if (TREE_CODE (arg0) == VECTOR_CST)
11714 if (n == 1)
11715 return VECTOR_CST_ELT (arg0, idx);
11717 tree *vals = XALLOCAVEC (tree, n);
11718 for (unsigned i = 0; i < n; ++i)
11719 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11720 return build_vector (type, vals);
11725 /* On constants we can use native encode/interpret to constant
11726 fold (nearly) all BIT_FIELD_REFs. */
11727 if (CONSTANT_CLASS_P (arg0)
11728 && can_native_interpret_type_p (type)
11729 && BITS_PER_UNIT == 8)
11731 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11732 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11733 /* Limit us to a reasonable amount of work. To relax the
11734 other limitations we need bit-shifting of the buffer
11735 and rounding up the size. */
11736 if (bitpos % BITS_PER_UNIT == 0
11737 && bitsize % BITS_PER_UNIT == 0
11738 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11740 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11741 unsigned HOST_WIDE_INT len
11742 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11743 bitpos / BITS_PER_UNIT);
11744 if (len > 0
11745 && len * BITS_PER_UNIT >= bitsize)
11747 tree v = native_interpret_expr (type, b,
11748 bitsize / BITS_PER_UNIT);
11749 if (v)
11750 return v;
11755 return NULL_TREE;
11757 case FMA_EXPR:
11758 /* For integers we can decompose the FMA if possible. */
11759 if (TREE_CODE (arg0) == INTEGER_CST
11760 && TREE_CODE (arg1) == INTEGER_CST)
11761 return fold_build2_loc (loc, PLUS_EXPR, type,
11762 const_binop (MULT_EXPR, arg0, arg1), arg2);
11763 if (integer_zerop (arg2))
11764 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11766 return fold_fma (loc, type, arg0, arg1, arg2);
11768 case VEC_PERM_EXPR:
11769 if (TREE_CODE (arg2) == VECTOR_CST)
11771 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11772 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11773 unsigned char *sel2 = sel + nelts;
11774 bool need_mask_canon = false;
11775 bool need_mask_canon2 = false;
11776 bool all_in_vec0 = true;
11777 bool all_in_vec1 = true;
11778 bool maybe_identity = true;
11779 bool single_arg = (op0 == op1);
11780 bool changed = false;
11782 mask2 = 2 * nelts - 1;
11783 mask = single_arg ? (nelts - 1) : mask2;
11784 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11785 for (i = 0; i < nelts; i++)
11787 tree val = VECTOR_CST_ELT (arg2, i);
11788 if (TREE_CODE (val) != INTEGER_CST)
11789 return NULL_TREE;
11791 /* Make sure that the perm value is in an acceptable
11792 range. */
11793 wide_int t = val;
11794 need_mask_canon |= wi::gtu_p (t, mask);
11795 need_mask_canon2 |= wi::gtu_p (t, mask2);
11796 sel[i] = t.to_uhwi () & mask;
11797 sel2[i] = t.to_uhwi () & mask2;
11799 if (sel[i] < nelts)
11800 all_in_vec1 = false;
11801 else
11802 all_in_vec0 = false;
11804 if ((sel[i] & (nelts-1)) != i)
11805 maybe_identity = false;
11808 if (maybe_identity)
11810 if (all_in_vec0)
11811 return op0;
11812 if (all_in_vec1)
11813 return op1;
11816 if (all_in_vec0)
11817 op1 = op0;
11818 else if (all_in_vec1)
11820 op0 = op1;
11821 for (i = 0; i < nelts; i++)
11822 sel[i] -= nelts;
11823 need_mask_canon = true;
11826 if ((TREE_CODE (op0) == VECTOR_CST
11827 || TREE_CODE (op0) == CONSTRUCTOR)
11828 && (TREE_CODE (op1) == VECTOR_CST
11829 || TREE_CODE (op1) == CONSTRUCTOR))
11831 tree t = fold_vec_perm (type, op0, op1, sel);
11832 if (t != NULL_TREE)
11833 return t;
11836 if (op0 == op1 && !single_arg)
11837 changed = true;
11839 /* Some targets are deficient and fail to expand a single
11840 argument permutation while still allowing an equivalent
11841 2-argument version. */
11842 if (need_mask_canon && arg2 == op2
11843 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11844 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11846 need_mask_canon = need_mask_canon2;
11847 sel = sel2;
11850 if (need_mask_canon && arg2 == op2)
11852 tree *tsel = XALLOCAVEC (tree, nelts);
11853 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11854 for (i = 0; i < nelts; i++)
11855 tsel[i] = build_int_cst (eltype, sel[i]);
11856 op2 = build_vector (TREE_TYPE (arg2), tsel);
11857 changed = true;
11860 if (changed)
11861 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11863 return NULL_TREE;
11865 case BIT_INSERT_EXPR:
11866 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11867 if (TREE_CODE (arg0) == INTEGER_CST
11868 && TREE_CODE (arg1) == INTEGER_CST)
11870 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11871 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11872 wide_int tem = wi::bit_and (arg0,
11873 wi::shifted_mask (bitpos, bitsize, true,
11874 TYPE_PRECISION (type)));
11875 wide_int tem2
11876 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11877 bitsize), bitpos);
11878 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11880 else if (TREE_CODE (arg0) == VECTOR_CST
11881 && CONSTANT_CLASS_P (arg1)
11882 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11883 TREE_TYPE (arg1)))
11885 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11886 unsigned HOST_WIDE_INT elsize
11887 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11888 if (bitpos % elsize == 0)
11890 unsigned k = bitpos / elsize;
11891 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11892 return arg0;
11893 else
11895 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11896 memcpy (elts, VECTOR_CST_ELTS (arg0),
11897 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11898 elts[k] = arg1;
11899 return build_vector (type, elts);
11903 return NULL_TREE;
11905 default:
11906 return NULL_TREE;
11907 } /* switch (code) */
11910 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11911 of an array (or vector). */
11913 tree
11914 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11916 tree index_type = NULL_TREE;
11917 offset_int low_bound = 0;
11919 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11921 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11922 if (domain_type && TYPE_MIN_VALUE (domain_type))
11924 /* Static constructors for variably sized objects makes no sense. */
11925 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11926 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11927 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11931 if (index_type)
11932 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11933 TYPE_SIGN (index_type));
11935 offset_int index = low_bound - 1;
11936 if (index_type)
11937 index = wi::ext (index, TYPE_PRECISION (index_type),
11938 TYPE_SIGN (index_type));
11940 offset_int max_index;
11941 unsigned HOST_WIDE_INT cnt;
11942 tree cfield, cval;
11944 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11946 /* Array constructor might explicitly set index, or specify a range,
11947 or leave index NULL meaning that it is next index after previous
11948 one. */
11949 if (cfield)
11951 if (TREE_CODE (cfield) == INTEGER_CST)
11952 max_index = index = wi::to_offset (cfield);
11953 else
11955 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11956 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11957 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11960 else
11962 index += 1;
11963 if (index_type)
11964 index = wi::ext (index, TYPE_PRECISION (index_type),
11965 TYPE_SIGN (index_type));
11966 max_index = index;
11969 /* Do we have match? */
11970 if (wi::cmpu (access_index, index) >= 0
11971 && wi::cmpu (access_index, max_index) <= 0)
11972 return cval;
11974 return NULL_TREE;
11977 /* Perform constant folding and related simplification of EXPR.
11978 The related simplifications include x*1 => x, x*0 => 0, etc.,
11979 and application of the associative law.
11980 NOP_EXPR conversions may be removed freely (as long as we
11981 are careful not to change the type of the overall expression).
11982 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11983 but we can constant-fold them if they have constant operands. */
11985 #ifdef ENABLE_FOLD_CHECKING
11986 # define fold(x) fold_1 (x)
11987 static tree fold_1 (tree);
11988 static
11989 #endif
11990 tree
11991 fold (tree expr)
11993 const tree t = expr;
11994 enum tree_code code = TREE_CODE (t);
11995 enum tree_code_class kind = TREE_CODE_CLASS (code);
11996 tree tem;
11997 location_t loc = EXPR_LOCATION (expr);
11999 /* Return right away if a constant. */
12000 if (kind == tcc_constant)
12001 return t;
12003 /* CALL_EXPR-like objects with variable numbers of operands are
12004 treated specially. */
12005 if (kind == tcc_vl_exp)
12007 if (code == CALL_EXPR)
12009 tem = fold_call_expr (loc, expr, false);
12010 return tem ? tem : expr;
12012 return expr;
12015 if (IS_EXPR_CODE_CLASS (kind))
12017 tree type = TREE_TYPE (t);
12018 tree op0, op1, op2;
12020 switch (TREE_CODE_LENGTH (code))
12022 case 1:
12023 op0 = TREE_OPERAND (t, 0);
12024 tem = fold_unary_loc (loc, code, type, op0);
12025 return tem ? tem : expr;
12026 case 2:
12027 op0 = TREE_OPERAND (t, 0);
12028 op1 = TREE_OPERAND (t, 1);
12029 tem = fold_binary_loc (loc, code, type, op0, op1);
12030 return tem ? tem : expr;
12031 case 3:
12032 op0 = TREE_OPERAND (t, 0);
12033 op1 = TREE_OPERAND (t, 1);
12034 op2 = TREE_OPERAND (t, 2);
12035 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12036 return tem ? tem : expr;
12037 default:
12038 break;
12042 switch (code)
12044 case ARRAY_REF:
12046 tree op0 = TREE_OPERAND (t, 0);
12047 tree op1 = TREE_OPERAND (t, 1);
12049 if (TREE_CODE (op1) == INTEGER_CST
12050 && TREE_CODE (op0) == CONSTRUCTOR
12051 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12053 tree val = get_array_ctor_element_at_index (op0,
12054 wi::to_offset (op1));
12055 if (val)
12056 return val;
12059 return t;
12062 /* Return a VECTOR_CST if possible. */
12063 case CONSTRUCTOR:
12065 tree type = TREE_TYPE (t);
12066 if (TREE_CODE (type) != VECTOR_TYPE)
12067 return t;
12069 unsigned i;
12070 tree val;
12071 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12072 if (! CONSTANT_CLASS_P (val))
12073 return t;
12075 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12078 case CONST_DECL:
12079 return fold (DECL_INITIAL (t));
12081 default:
12082 return t;
12083 } /* switch (code) */
12086 #ifdef ENABLE_FOLD_CHECKING
12087 #undef fold
12089 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12090 hash_table<nofree_ptr_hash<const tree_node> > *);
12091 static void fold_check_failed (const_tree, const_tree);
12092 void print_fold_checksum (const_tree);
12094 /* When --enable-checking=fold, compute a digest of expr before
12095 and after actual fold call to see if fold did not accidentally
12096 change original expr. */
12098 tree
12099 fold (tree expr)
12101 tree ret;
12102 struct md5_ctx ctx;
12103 unsigned char checksum_before[16], checksum_after[16];
12104 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12106 md5_init_ctx (&ctx);
12107 fold_checksum_tree (expr, &ctx, &ht);
12108 md5_finish_ctx (&ctx, checksum_before);
12109 ht.empty ();
12111 ret = fold_1 (expr);
12113 md5_init_ctx (&ctx);
12114 fold_checksum_tree (expr, &ctx, &ht);
12115 md5_finish_ctx (&ctx, checksum_after);
12117 if (memcmp (checksum_before, checksum_after, 16))
12118 fold_check_failed (expr, ret);
12120 return ret;
12123 void
12124 print_fold_checksum (const_tree expr)
12126 struct md5_ctx ctx;
12127 unsigned char checksum[16], cnt;
12128 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12130 md5_init_ctx (&ctx);
12131 fold_checksum_tree (expr, &ctx, &ht);
12132 md5_finish_ctx (&ctx, checksum);
12133 for (cnt = 0; cnt < 16; ++cnt)
12134 fprintf (stderr, "%02x", checksum[cnt]);
12135 putc ('\n', stderr);
12138 static void
12139 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12141 internal_error ("fold check: original tree changed by fold");
12144 static void
12145 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12146 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12148 const tree_node **slot;
12149 enum tree_code code;
12150 union tree_node buf;
12151 int i, len;
12153 recursive_label:
12154 if (expr == NULL)
12155 return;
12156 slot = ht->find_slot (expr, INSERT);
12157 if (*slot != NULL)
12158 return;
12159 *slot = expr;
12160 code = TREE_CODE (expr);
12161 if (TREE_CODE_CLASS (code) == tcc_declaration
12162 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12164 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12165 memcpy ((char *) &buf, expr, tree_size (expr));
12166 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12167 buf.decl_with_vis.symtab_node = NULL;
12168 expr = (tree) &buf;
12170 else if (TREE_CODE_CLASS (code) == tcc_type
12171 && (TYPE_POINTER_TO (expr)
12172 || TYPE_REFERENCE_TO (expr)
12173 || TYPE_CACHED_VALUES_P (expr)
12174 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12175 || TYPE_NEXT_VARIANT (expr)
12176 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12178 /* Allow these fields to be modified. */
12179 tree tmp;
12180 memcpy ((char *) &buf, expr, tree_size (expr));
12181 expr = tmp = (tree) &buf;
12182 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12183 TYPE_POINTER_TO (tmp) = NULL;
12184 TYPE_REFERENCE_TO (tmp) = NULL;
12185 TYPE_NEXT_VARIANT (tmp) = NULL;
12186 TYPE_ALIAS_SET (tmp) = -1;
12187 if (TYPE_CACHED_VALUES_P (tmp))
12189 TYPE_CACHED_VALUES_P (tmp) = 0;
12190 TYPE_CACHED_VALUES (tmp) = NULL;
12193 md5_process_bytes (expr, tree_size (expr), ctx);
12194 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12195 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12196 if (TREE_CODE_CLASS (code) != tcc_type
12197 && TREE_CODE_CLASS (code) != tcc_declaration
12198 && code != TREE_LIST
12199 && code != SSA_NAME
12200 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12201 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12202 switch (TREE_CODE_CLASS (code))
12204 case tcc_constant:
12205 switch (code)
12207 case STRING_CST:
12208 md5_process_bytes (TREE_STRING_POINTER (expr),
12209 TREE_STRING_LENGTH (expr), ctx);
12210 break;
12211 case COMPLEX_CST:
12212 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12213 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12214 break;
12215 case VECTOR_CST:
12216 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12217 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12218 break;
12219 default:
12220 break;
12222 break;
12223 case tcc_exceptional:
12224 switch (code)
12226 case TREE_LIST:
12227 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12228 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12229 expr = TREE_CHAIN (expr);
12230 goto recursive_label;
12231 break;
12232 case TREE_VEC:
12233 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12234 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12235 break;
12236 default:
12237 break;
12239 break;
12240 case tcc_expression:
12241 case tcc_reference:
12242 case tcc_comparison:
12243 case tcc_unary:
12244 case tcc_binary:
12245 case tcc_statement:
12246 case tcc_vl_exp:
12247 len = TREE_OPERAND_LENGTH (expr);
12248 for (i = 0; i < len; ++i)
12249 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12250 break;
12251 case tcc_declaration:
12252 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12253 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12254 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12256 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12257 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12258 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12259 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12260 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12263 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12265 if (TREE_CODE (expr) == FUNCTION_DECL)
12267 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12268 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12270 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12272 break;
12273 case tcc_type:
12274 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12275 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12276 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12277 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12278 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12279 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12280 if (INTEGRAL_TYPE_P (expr)
12281 || SCALAR_FLOAT_TYPE_P (expr))
12283 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12284 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12286 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12287 if (TREE_CODE (expr) == RECORD_TYPE
12288 || TREE_CODE (expr) == UNION_TYPE
12289 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12290 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12291 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12292 break;
12293 default:
12294 break;
12298 /* Helper function for outputting the checksum of a tree T. When
12299 debugging with gdb, you can "define mynext" to be "next" followed
12300 by "call debug_fold_checksum (op0)", then just trace down till the
12301 outputs differ. */
12303 DEBUG_FUNCTION void
12304 debug_fold_checksum (const_tree t)
12306 int i;
12307 unsigned char checksum[16];
12308 struct md5_ctx ctx;
12309 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12311 md5_init_ctx (&ctx);
12312 fold_checksum_tree (t, &ctx, &ht);
12313 md5_finish_ctx (&ctx, checksum);
12314 ht.empty ();
12316 for (i = 0; i < 16; i++)
12317 fprintf (stderr, "%d ", checksum[i]);
12319 fprintf (stderr, "\n");
12322 #endif
12324 /* Fold a unary tree expression with code CODE of type TYPE with an
12325 operand OP0. LOC is the location of the resulting expression.
12326 Return a folded expression if successful. Otherwise, return a tree
12327 expression with code CODE of type TYPE with an operand OP0. */
12329 tree
12330 fold_build1_stat_loc (location_t loc,
12331 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12333 tree tem;
12334 #ifdef ENABLE_FOLD_CHECKING
12335 unsigned char checksum_before[16], checksum_after[16];
12336 struct md5_ctx ctx;
12337 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12339 md5_init_ctx (&ctx);
12340 fold_checksum_tree (op0, &ctx, &ht);
12341 md5_finish_ctx (&ctx, checksum_before);
12342 ht.empty ();
12343 #endif
12345 tem = fold_unary_loc (loc, code, type, op0);
12346 if (!tem)
12347 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12349 #ifdef ENABLE_FOLD_CHECKING
12350 md5_init_ctx (&ctx);
12351 fold_checksum_tree (op0, &ctx, &ht);
12352 md5_finish_ctx (&ctx, checksum_after);
12354 if (memcmp (checksum_before, checksum_after, 16))
12355 fold_check_failed (op0, tem);
12356 #endif
12357 return tem;
12360 /* Fold a binary tree expression with code CODE of type TYPE with
12361 operands OP0 and OP1. LOC is the location of the resulting
12362 expression. Return a folded expression if successful. Otherwise,
12363 return a tree expression with code CODE of type TYPE with operands
12364 OP0 and OP1. */
12366 tree
12367 fold_build2_stat_loc (location_t loc,
12368 enum tree_code code, tree type, tree op0, tree op1
12369 MEM_STAT_DECL)
12371 tree tem;
12372 #ifdef ENABLE_FOLD_CHECKING
12373 unsigned char checksum_before_op0[16],
12374 checksum_before_op1[16],
12375 checksum_after_op0[16],
12376 checksum_after_op1[16];
12377 struct md5_ctx ctx;
12378 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12380 md5_init_ctx (&ctx);
12381 fold_checksum_tree (op0, &ctx, &ht);
12382 md5_finish_ctx (&ctx, checksum_before_op0);
12383 ht.empty ();
12385 md5_init_ctx (&ctx);
12386 fold_checksum_tree (op1, &ctx, &ht);
12387 md5_finish_ctx (&ctx, checksum_before_op1);
12388 ht.empty ();
12389 #endif
12391 tem = fold_binary_loc (loc, code, type, op0, op1);
12392 if (!tem)
12393 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12395 #ifdef ENABLE_FOLD_CHECKING
12396 md5_init_ctx (&ctx);
12397 fold_checksum_tree (op0, &ctx, &ht);
12398 md5_finish_ctx (&ctx, checksum_after_op0);
12399 ht.empty ();
12401 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12402 fold_check_failed (op0, tem);
12404 md5_init_ctx (&ctx);
12405 fold_checksum_tree (op1, &ctx, &ht);
12406 md5_finish_ctx (&ctx, checksum_after_op1);
12408 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12409 fold_check_failed (op1, tem);
12410 #endif
12411 return tem;
12414 /* Fold a ternary tree expression with code CODE of type TYPE with
12415 operands OP0, OP1, and OP2. Return a folded expression if
12416 successful. Otherwise, return a tree expression with code CODE of
12417 type TYPE with operands OP0, OP1, and OP2. */
12419 tree
12420 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12421 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12423 tree tem;
12424 #ifdef ENABLE_FOLD_CHECKING
12425 unsigned char checksum_before_op0[16],
12426 checksum_before_op1[16],
12427 checksum_before_op2[16],
12428 checksum_after_op0[16],
12429 checksum_after_op1[16],
12430 checksum_after_op2[16];
12431 struct md5_ctx ctx;
12432 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12434 md5_init_ctx (&ctx);
12435 fold_checksum_tree (op0, &ctx, &ht);
12436 md5_finish_ctx (&ctx, checksum_before_op0);
12437 ht.empty ();
12439 md5_init_ctx (&ctx);
12440 fold_checksum_tree (op1, &ctx, &ht);
12441 md5_finish_ctx (&ctx, checksum_before_op1);
12442 ht.empty ();
12444 md5_init_ctx (&ctx);
12445 fold_checksum_tree (op2, &ctx, &ht);
12446 md5_finish_ctx (&ctx, checksum_before_op2);
12447 ht.empty ();
12448 #endif
12450 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12451 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12452 if (!tem)
12453 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12455 #ifdef ENABLE_FOLD_CHECKING
12456 md5_init_ctx (&ctx);
12457 fold_checksum_tree (op0, &ctx, &ht);
12458 md5_finish_ctx (&ctx, checksum_after_op0);
12459 ht.empty ();
12461 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12462 fold_check_failed (op0, tem);
12464 md5_init_ctx (&ctx);
12465 fold_checksum_tree (op1, &ctx, &ht);
12466 md5_finish_ctx (&ctx, checksum_after_op1);
12467 ht.empty ();
12469 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12470 fold_check_failed (op1, tem);
12472 md5_init_ctx (&ctx);
12473 fold_checksum_tree (op2, &ctx, &ht);
12474 md5_finish_ctx (&ctx, checksum_after_op2);
12476 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12477 fold_check_failed (op2, tem);
12478 #endif
12479 return tem;
12482 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12483 arguments in ARGARRAY, and a null static chain.
12484 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12485 of type TYPE from the given operands as constructed by build_call_array. */
12487 tree
12488 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12489 int nargs, tree *argarray)
12491 tree tem;
12492 #ifdef ENABLE_FOLD_CHECKING
12493 unsigned char checksum_before_fn[16],
12494 checksum_before_arglist[16],
12495 checksum_after_fn[16],
12496 checksum_after_arglist[16];
12497 struct md5_ctx ctx;
12498 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12499 int i;
12501 md5_init_ctx (&ctx);
12502 fold_checksum_tree (fn, &ctx, &ht);
12503 md5_finish_ctx (&ctx, checksum_before_fn);
12504 ht.empty ();
12506 md5_init_ctx (&ctx);
12507 for (i = 0; i < nargs; i++)
12508 fold_checksum_tree (argarray[i], &ctx, &ht);
12509 md5_finish_ctx (&ctx, checksum_before_arglist);
12510 ht.empty ();
12511 #endif
12513 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12514 if (!tem)
12515 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12517 #ifdef ENABLE_FOLD_CHECKING
12518 md5_init_ctx (&ctx);
12519 fold_checksum_tree (fn, &ctx, &ht);
12520 md5_finish_ctx (&ctx, checksum_after_fn);
12521 ht.empty ();
12523 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12524 fold_check_failed (fn, tem);
12526 md5_init_ctx (&ctx);
12527 for (i = 0; i < nargs; i++)
12528 fold_checksum_tree (argarray[i], &ctx, &ht);
12529 md5_finish_ctx (&ctx, checksum_after_arglist);
12531 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12532 fold_check_failed (NULL_TREE, tem);
12533 #endif
12534 return tem;
12537 /* Perform constant folding and related simplification of initializer
12538 expression EXPR. These behave identically to "fold_buildN" but ignore
12539 potential run-time traps and exceptions that fold must preserve. */
12541 #define START_FOLD_INIT \
12542 int saved_signaling_nans = flag_signaling_nans;\
12543 int saved_trapping_math = flag_trapping_math;\
12544 int saved_rounding_math = flag_rounding_math;\
12545 int saved_trapv = flag_trapv;\
12546 int saved_folding_initializer = folding_initializer;\
12547 flag_signaling_nans = 0;\
12548 flag_trapping_math = 0;\
12549 flag_rounding_math = 0;\
12550 flag_trapv = 0;\
12551 folding_initializer = 1;
12553 #define END_FOLD_INIT \
12554 flag_signaling_nans = saved_signaling_nans;\
12555 flag_trapping_math = saved_trapping_math;\
12556 flag_rounding_math = saved_rounding_math;\
12557 flag_trapv = saved_trapv;\
12558 folding_initializer = saved_folding_initializer;
12560 tree
12561 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12562 tree type, tree op)
12564 tree result;
12565 START_FOLD_INIT;
12567 result = fold_build1_loc (loc, code, type, op);
12569 END_FOLD_INIT;
12570 return result;
12573 tree
12574 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12575 tree type, tree op0, tree op1)
12577 tree result;
12578 START_FOLD_INIT;
12580 result = fold_build2_loc (loc, code, type, op0, op1);
12582 END_FOLD_INIT;
12583 return result;
12586 tree
12587 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12588 int nargs, tree *argarray)
12590 tree result;
12591 START_FOLD_INIT;
12593 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12595 END_FOLD_INIT;
12596 return result;
12599 #undef START_FOLD_INIT
12600 #undef END_FOLD_INIT
12602 /* Determine if first argument is a multiple of second argument. Return 0 if
12603 it is not, or we cannot easily determined it to be.
12605 An example of the sort of thing we care about (at this point; this routine
12606 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12607 fold cases do now) is discovering that
12609 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12611 is a multiple of
12613 SAVE_EXPR (J * 8)
12615 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12617 This code also handles discovering that
12619 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12621 is a multiple of 8 so we don't have to worry about dealing with a
12622 possible remainder.
12624 Note that we *look* inside a SAVE_EXPR only to determine how it was
12625 calculated; it is not safe for fold to do much of anything else with the
12626 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12627 at run time. For example, the latter example above *cannot* be implemented
12628 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12629 evaluation time of the original SAVE_EXPR is not necessarily the same at
12630 the time the new expression is evaluated. The only optimization of this
12631 sort that would be valid is changing
12633 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12635 divided by 8 to
12637 SAVE_EXPR (I) * SAVE_EXPR (J)
12639 (where the same SAVE_EXPR (J) is used in the original and the
12640 transformed version). */
12643 multiple_of_p (tree type, const_tree top, const_tree bottom)
12645 if (operand_equal_p (top, bottom, 0))
12646 return 1;
12648 if (TREE_CODE (type) != INTEGER_TYPE)
12649 return 0;
12651 switch (TREE_CODE (top))
12653 case BIT_AND_EXPR:
12654 /* Bitwise and provides a power of two multiple. If the mask is
12655 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12656 if (!integer_pow2p (bottom))
12657 return 0;
12658 /* FALLTHRU */
12660 case MULT_EXPR:
12661 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12662 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12664 case PLUS_EXPR:
12665 case MINUS_EXPR:
12666 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12667 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12669 case LSHIFT_EXPR:
12670 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12672 tree op1, t1;
12674 op1 = TREE_OPERAND (top, 1);
12675 /* const_binop may not detect overflow correctly,
12676 so check for it explicitly here. */
12677 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12678 && 0 != (t1 = fold_convert (type,
12679 const_binop (LSHIFT_EXPR,
12680 size_one_node,
12681 op1)))
12682 && !TREE_OVERFLOW (t1))
12683 return multiple_of_p (type, t1, bottom);
12685 return 0;
12687 case NOP_EXPR:
12688 /* Can't handle conversions from non-integral or wider integral type. */
12689 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12690 || (TYPE_PRECISION (type)
12691 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12692 return 0;
12694 /* .. fall through ... */
12696 case SAVE_EXPR:
12697 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12699 case COND_EXPR:
12700 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12701 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12703 case INTEGER_CST:
12704 if (TREE_CODE (bottom) != INTEGER_CST
12705 || integer_zerop (bottom)
12706 || (TYPE_UNSIGNED (type)
12707 && (tree_int_cst_sgn (top) < 0
12708 || tree_int_cst_sgn (bottom) < 0)))
12709 return 0;
12710 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12711 SIGNED);
12713 default:
12714 return 0;
12718 #define tree_expr_nonnegative_warnv_p(X, Y) \
12719 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12721 #define RECURSE(X) \
12722 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12724 /* Return true if CODE or TYPE is known to be non-negative. */
12726 static bool
12727 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12729 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12730 && truth_value_p (code))
12731 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12732 have a signed:1 type (where the value is -1 and 0). */
12733 return true;
12734 return false;
12737 /* Return true if (CODE OP0) is known to be non-negative. If the return
12738 value is based on the assumption that signed overflow is undefined,
12739 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12740 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12742 bool
12743 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12744 bool *strict_overflow_p, int depth)
12746 if (TYPE_UNSIGNED (type))
12747 return true;
12749 switch (code)
12751 case ABS_EXPR:
12752 /* We can't return 1 if flag_wrapv is set because
12753 ABS_EXPR<INT_MIN> = INT_MIN. */
12754 if (!ANY_INTEGRAL_TYPE_P (type))
12755 return true;
12756 if (TYPE_OVERFLOW_UNDEFINED (type))
12758 *strict_overflow_p = true;
12759 return true;
12761 break;
12763 case NON_LVALUE_EXPR:
12764 case FLOAT_EXPR:
12765 case FIX_TRUNC_EXPR:
12766 return RECURSE (op0);
12768 CASE_CONVERT:
12770 tree inner_type = TREE_TYPE (op0);
12771 tree outer_type = type;
12773 if (TREE_CODE (outer_type) == REAL_TYPE)
12775 if (TREE_CODE (inner_type) == REAL_TYPE)
12776 return RECURSE (op0);
12777 if (INTEGRAL_TYPE_P (inner_type))
12779 if (TYPE_UNSIGNED (inner_type))
12780 return true;
12781 return RECURSE (op0);
12784 else if (INTEGRAL_TYPE_P (outer_type))
12786 if (TREE_CODE (inner_type) == REAL_TYPE)
12787 return RECURSE (op0);
12788 if (INTEGRAL_TYPE_P (inner_type))
12789 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12790 && TYPE_UNSIGNED (inner_type);
12793 break;
12795 default:
12796 return tree_simple_nonnegative_warnv_p (code, type);
12799 /* We don't know sign of `t', so be conservative and return false. */
12800 return false;
12803 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12804 value is based on the assumption that signed overflow is undefined,
12805 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12806 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12808 bool
12809 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12810 tree op1, bool *strict_overflow_p,
12811 int depth)
12813 if (TYPE_UNSIGNED (type))
12814 return true;
12816 switch (code)
12818 case POINTER_PLUS_EXPR:
12819 case PLUS_EXPR:
12820 if (FLOAT_TYPE_P (type))
12821 return RECURSE (op0) && RECURSE (op1);
12823 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12824 both unsigned and at least 2 bits shorter than the result. */
12825 if (TREE_CODE (type) == INTEGER_TYPE
12826 && TREE_CODE (op0) == NOP_EXPR
12827 && TREE_CODE (op1) == NOP_EXPR)
12829 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12830 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12831 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12832 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12834 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12835 TYPE_PRECISION (inner2)) + 1;
12836 return prec < TYPE_PRECISION (type);
12839 break;
12841 case MULT_EXPR:
12842 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12844 /* x * x is always non-negative for floating point x
12845 or without overflow. */
12846 if (operand_equal_p (op0, op1, 0)
12847 || (RECURSE (op0) && RECURSE (op1)))
12849 if (ANY_INTEGRAL_TYPE_P (type)
12850 && TYPE_OVERFLOW_UNDEFINED (type))
12851 *strict_overflow_p = true;
12852 return true;
12856 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12857 both unsigned and their total bits is shorter than the result. */
12858 if (TREE_CODE (type) == INTEGER_TYPE
12859 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12860 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12862 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12863 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12864 : TREE_TYPE (op0);
12865 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12866 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12867 : TREE_TYPE (op1);
12869 bool unsigned0 = TYPE_UNSIGNED (inner0);
12870 bool unsigned1 = TYPE_UNSIGNED (inner1);
12872 if (TREE_CODE (op0) == INTEGER_CST)
12873 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12875 if (TREE_CODE (op1) == INTEGER_CST)
12876 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12878 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12879 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12881 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12882 ? tree_int_cst_min_precision (op0, UNSIGNED)
12883 : TYPE_PRECISION (inner0);
12885 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12886 ? tree_int_cst_min_precision (op1, UNSIGNED)
12887 : TYPE_PRECISION (inner1);
12889 return precision0 + precision1 < TYPE_PRECISION (type);
12892 return false;
12894 case BIT_AND_EXPR:
12895 case MAX_EXPR:
12896 return RECURSE (op0) || RECURSE (op1);
12898 case BIT_IOR_EXPR:
12899 case BIT_XOR_EXPR:
12900 case MIN_EXPR:
12901 case RDIV_EXPR:
12902 case TRUNC_DIV_EXPR:
12903 case CEIL_DIV_EXPR:
12904 case FLOOR_DIV_EXPR:
12905 case ROUND_DIV_EXPR:
12906 return RECURSE (op0) && RECURSE (op1);
12908 case TRUNC_MOD_EXPR:
12909 return RECURSE (op0);
12911 case FLOOR_MOD_EXPR:
12912 return RECURSE (op1);
12914 case CEIL_MOD_EXPR:
12915 case ROUND_MOD_EXPR:
12916 default:
12917 return tree_simple_nonnegative_warnv_p (code, type);
12920 /* We don't know sign of `t', so be conservative and return false. */
12921 return false;
12924 /* Return true if T is known to be non-negative. If the return
12925 value is based on the assumption that signed overflow is undefined,
12926 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12927 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12929 bool
12930 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12932 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12933 return true;
12935 switch (TREE_CODE (t))
12937 case INTEGER_CST:
12938 return tree_int_cst_sgn (t) >= 0;
12940 case REAL_CST:
12941 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12943 case FIXED_CST:
12944 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12946 case COND_EXPR:
12947 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12949 case SSA_NAME:
12950 /* Limit the depth of recursion to avoid quadratic behavior.
12951 This is expected to catch almost all occurrences in practice.
12952 If this code misses important cases that unbounded recursion
12953 would not, passes that need this information could be revised
12954 to provide it through dataflow propagation. */
12955 return (!name_registered_for_update_p (t)
12956 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12957 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12958 strict_overflow_p, depth));
12960 default:
12961 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12965 /* Return true if T is known to be non-negative. If the return
12966 value is based on the assumption that signed overflow is undefined,
12967 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12968 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12970 bool
12971 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12972 bool *strict_overflow_p, int depth)
12974 switch (fn)
12976 CASE_CFN_ACOS:
12977 CASE_CFN_ACOSH:
12978 CASE_CFN_CABS:
12979 CASE_CFN_COSH:
12980 CASE_CFN_ERFC:
12981 CASE_CFN_EXP:
12982 CASE_CFN_EXP10:
12983 CASE_CFN_EXP2:
12984 CASE_CFN_FABS:
12985 CASE_CFN_FDIM:
12986 CASE_CFN_HYPOT:
12987 CASE_CFN_POW10:
12988 CASE_CFN_FFS:
12989 CASE_CFN_PARITY:
12990 CASE_CFN_POPCOUNT:
12991 CASE_CFN_CLZ:
12992 CASE_CFN_CLRSB:
12993 case CFN_BUILT_IN_BSWAP32:
12994 case CFN_BUILT_IN_BSWAP64:
12995 /* Always true. */
12996 return true;
12998 CASE_CFN_SQRT:
12999 /* sqrt(-0.0) is -0.0. */
13000 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13001 return true;
13002 return RECURSE (arg0);
13004 CASE_CFN_ASINH:
13005 CASE_CFN_ATAN:
13006 CASE_CFN_ATANH:
13007 CASE_CFN_CBRT:
13008 CASE_CFN_CEIL:
13009 CASE_CFN_ERF:
13010 CASE_CFN_EXPM1:
13011 CASE_CFN_FLOOR:
13012 CASE_CFN_FMOD:
13013 CASE_CFN_FREXP:
13014 CASE_CFN_ICEIL:
13015 CASE_CFN_IFLOOR:
13016 CASE_CFN_IRINT:
13017 CASE_CFN_IROUND:
13018 CASE_CFN_LCEIL:
13019 CASE_CFN_LDEXP:
13020 CASE_CFN_LFLOOR:
13021 CASE_CFN_LLCEIL:
13022 CASE_CFN_LLFLOOR:
13023 CASE_CFN_LLRINT:
13024 CASE_CFN_LLROUND:
13025 CASE_CFN_LRINT:
13026 CASE_CFN_LROUND:
13027 CASE_CFN_MODF:
13028 CASE_CFN_NEARBYINT:
13029 CASE_CFN_RINT:
13030 CASE_CFN_ROUND:
13031 CASE_CFN_SCALB:
13032 CASE_CFN_SCALBLN:
13033 CASE_CFN_SCALBN:
13034 CASE_CFN_SIGNBIT:
13035 CASE_CFN_SIGNIFICAND:
13036 CASE_CFN_SINH:
13037 CASE_CFN_TANH:
13038 CASE_CFN_TRUNC:
13039 /* True if the 1st argument is nonnegative. */
13040 return RECURSE (arg0);
13042 CASE_CFN_FMAX:
13043 /* True if the 1st OR 2nd arguments are nonnegative. */
13044 return RECURSE (arg0) || RECURSE (arg1);
13046 CASE_CFN_FMIN:
13047 /* True if the 1st AND 2nd arguments are nonnegative. */
13048 return RECURSE (arg0) && RECURSE (arg1);
13050 CASE_CFN_COPYSIGN:
13051 /* True if the 2nd argument is nonnegative. */
13052 return RECURSE (arg1);
13054 CASE_CFN_POWI:
13055 /* True if the 1st argument is nonnegative or the second
13056 argument is an even integer. */
13057 if (TREE_CODE (arg1) == INTEGER_CST
13058 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13059 return true;
13060 return RECURSE (arg0);
13062 CASE_CFN_POW:
13063 /* True if the 1st argument is nonnegative or the second
13064 argument is an even integer valued real. */
13065 if (TREE_CODE (arg1) == REAL_CST)
13067 REAL_VALUE_TYPE c;
13068 HOST_WIDE_INT n;
13070 c = TREE_REAL_CST (arg1);
13071 n = real_to_integer (&c);
13072 if ((n & 1) == 0)
13074 REAL_VALUE_TYPE cint;
13075 real_from_integer (&cint, VOIDmode, n, SIGNED);
13076 if (real_identical (&c, &cint))
13077 return true;
13080 return RECURSE (arg0);
13082 default:
13083 break;
13085 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13088 /* Return true if T is known to be non-negative. If the return
13089 value is based on the assumption that signed overflow is undefined,
13090 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13091 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13093 static bool
13094 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13096 enum tree_code code = TREE_CODE (t);
13097 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13098 return true;
13100 switch (code)
13102 case TARGET_EXPR:
13104 tree temp = TARGET_EXPR_SLOT (t);
13105 t = TARGET_EXPR_INITIAL (t);
13107 /* If the initializer is non-void, then it's a normal expression
13108 that will be assigned to the slot. */
13109 if (!VOID_TYPE_P (t))
13110 return RECURSE (t);
13112 /* Otherwise, the initializer sets the slot in some way. One common
13113 way is an assignment statement at the end of the initializer. */
13114 while (1)
13116 if (TREE_CODE (t) == BIND_EXPR)
13117 t = expr_last (BIND_EXPR_BODY (t));
13118 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13119 || TREE_CODE (t) == TRY_CATCH_EXPR)
13120 t = expr_last (TREE_OPERAND (t, 0));
13121 else if (TREE_CODE (t) == STATEMENT_LIST)
13122 t = expr_last (t);
13123 else
13124 break;
13126 if (TREE_CODE (t) == MODIFY_EXPR
13127 && TREE_OPERAND (t, 0) == temp)
13128 return RECURSE (TREE_OPERAND (t, 1));
13130 return false;
13133 case CALL_EXPR:
13135 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13136 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13138 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13139 get_call_combined_fn (t),
13140 arg0,
13141 arg1,
13142 strict_overflow_p, depth);
13144 case COMPOUND_EXPR:
13145 case MODIFY_EXPR:
13146 return RECURSE (TREE_OPERAND (t, 1));
13148 case BIND_EXPR:
13149 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13151 case SAVE_EXPR:
13152 return RECURSE (TREE_OPERAND (t, 0));
13154 default:
13155 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13159 #undef RECURSE
13160 #undef tree_expr_nonnegative_warnv_p
13162 /* Return true if T is known to be non-negative. If the return
13163 value is based on the assumption that signed overflow is undefined,
13164 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13165 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13167 bool
13168 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13170 enum tree_code code;
13171 if (t == error_mark_node)
13172 return false;
13174 code = TREE_CODE (t);
13175 switch (TREE_CODE_CLASS (code))
13177 case tcc_binary:
13178 case tcc_comparison:
13179 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13180 TREE_TYPE (t),
13181 TREE_OPERAND (t, 0),
13182 TREE_OPERAND (t, 1),
13183 strict_overflow_p, depth);
13185 case tcc_unary:
13186 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13187 TREE_TYPE (t),
13188 TREE_OPERAND (t, 0),
13189 strict_overflow_p, depth);
13191 case tcc_constant:
13192 case tcc_declaration:
13193 case tcc_reference:
13194 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13196 default:
13197 break;
13200 switch (code)
13202 case TRUTH_AND_EXPR:
13203 case TRUTH_OR_EXPR:
13204 case TRUTH_XOR_EXPR:
13205 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13206 TREE_TYPE (t),
13207 TREE_OPERAND (t, 0),
13208 TREE_OPERAND (t, 1),
13209 strict_overflow_p, depth);
13210 case TRUTH_NOT_EXPR:
13211 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13212 TREE_TYPE (t),
13213 TREE_OPERAND (t, 0),
13214 strict_overflow_p, depth);
13216 case COND_EXPR:
13217 case CONSTRUCTOR:
13218 case OBJ_TYPE_REF:
13219 case ASSERT_EXPR:
13220 case ADDR_EXPR:
13221 case WITH_SIZE_EXPR:
13222 case SSA_NAME:
13223 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13225 default:
13226 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13230 /* Return true if `t' is known to be non-negative. Handle warnings
13231 about undefined signed overflow. */
13233 bool
13234 tree_expr_nonnegative_p (tree t)
13236 bool ret, strict_overflow_p;
13238 strict_overflow_p = false;
13239 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13240 if (strict_overflow_p)
13241 fold_overflow_warning (("assuming signed overflow does not occur when "
13242 "determining that expression is always "
13243 "non-negative"),
13244 WARN_STRICT_OVERFLOW_MISC);
13245 return ret;
13249 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13250 For floating point we further ensure that T is not denormal.
13251 Similar logic is present in nonzero_address in rtlanal.h.
13253 If the return value is based on the assumption that signed overflow
13254 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13255 change *STRICT_OVERFLOW_P. */
13257 bool
13258 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13259 bool *strict_overflow_p)
13261 switch (code)
13263 case ABS_EXPR:
13264 return tree_expr_nonzero_warnv_p (op0,
13265 strict_overflow_p);
13267 case NOP_EXPR:
13269 tree inner_type = TREE_TYPE (op0);
13270 tree outer_type = type;
13272 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13273 && tree_expr_nonzero_warnv_p (op0,
13274 strict_overflow_p));
13276 break;
13278 case NON_LVALUE_EXPR:
13279 return tree_expr_nonzero_warnv_p (op0,
13280 strict_overflow_p);
13282 default:
13283 break;
13286 return false;
13289 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13290 For floating point we further ensure that T is not denormal.
13291 Similar logic is present in nonzero_address in rtlanal.h.
13293 If the return value is based on the assumption that signed overflow
13294 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13295 change *STRICT_OVERFLOW_P. */
13297 bool
13298 tree_binary_nonzero_warnv_p (enum tree_code code,
13299 tree type,
13300 tree op0,
13301 tree op1, bool *strict_overflow_p)
13303 bool sub_strict_overflow_p;
13304 switch (code)
13306 case POINTER_PLUS_EXPR:
13307 case PLUS_EXPR:
13308 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13310 /* With the presence of negative values it is hard
13311 to say something. */
13312 sub_strict_overflow_p = false;
13313 if (!tree_expr_nonnegative_warnv_p (op0,
13314 &sub_strict_overflow_p)
13315 || !tree_expr_nonnegative_warnv_p (op1,
13316 &sub_strict_overflow_p))
13317 return false;
13318 /* One of operands must be positive and the other non-negative. */
13319 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13320 overflows, on a twos-complement machine the sum of two
13321 nonnegative numbers can never be zero. */
13322 return (tree_expr_nonzero_warnv_p (op0,
13323 strict_overflow_p)
13324 || tree_expr_nonzero_warnv_p (op1,
13325 strict_overflow_p));
13327 break;
13329 case MULT_EXPR:
13330 if (TYPE_OVERFLOW_UNDEFINED (type))
13332 if (tree_expr_nonzero_warnv_p (op0,
13333 strict_overflow_p)
13334 && tree_expr_nonzero_warnv_p (op1,
13335 strict_overflow_p))
13337 *strict_overflow_p = true;
13338 return true;
13341 break;
13343 case MIN_EXPR:
13344 sub_strict_overflow_p = false;
13345 if (tree_expr_nonzero_warnv_p (op0,
13346 &sub_strict_overflow_p)
13347 && tree_expr_nonzero_warnv_p (op1,
13348 &sub_strict_overflow_p))
13350 if (sub_strict_overflow_p)
13351 *strict_overflow_p = true;
13353 break;
13355 case MAX_EXPR:
13356 sub_strict_overflow_p = false;
13357 if (tree_expr_nonzero_warnv_p (op0,
13358 &sub_strict_overflow_p))
13360 if (sub_strict_overflow_p)
13361 *strict_overflow_p = true;
13363 /* When both operands are nonzero, then MAX must be too. */
13364 if (tree_expr_nonzero_warnv_p (op1,
13365 strict_overflow_p))
13366 return true;
13368 /* MAX where operand 0 is positive is positive. */
13369 return tree_expr_nonnegative_warnv_p (op0,
13370 strict_overflow_p);
13372 /* MAX where operand 1 is positive is positive. */
13373 else if (tree_expr_nonzero_warnv_p (op1,
13374 &sub_strict_overflow_p)
13375 && tree_expr_nonnegative_warnv_p (op1,
13376 &sub_strict_overflow_p))
13378 if (sub_strict_overflow_p)
13379 *strict_overflow_p = true;
13380 return true;
13382 break;
13384 case BIT_IOR_EXPR:
13385 return (tree_expr_nonzero_warnv_p (op1,
13386 strict_overflow_p)
13387 || tree_expr_nonzero_warnv_p (op0,
13388 strict_overflow_p));
13390 default:
13391 break;
13394 return false;
13397 /* Return true when T is an address and is known to be nonzero.
13398 For floating point we further ensure that T is not denormal.
13399 Similar logic is present in nonzero_address in rtlanal.h.
13401 If the return value is based on the assumption that signed overflow
13402 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13403 change *STRICT_OVERFLOW_P. */
13405 bool
13406 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13408 bool sub_strict_overflow_p;
13409 switch (TREE_CODE (t))
13411 case INTEGER_CST:
13412 return !integer_zerop (t);
13414 case ADDR_EXPR:
13416 tree base = TREE_OPERAND (t, 0);
13418 if (!DECL_P (base))
13419 base = get_base_address (base);
13421 if (base && TREE_CODE (base) == TARGET_EXPR)
13422 base = TARGET_EXPR_SLOT (base);
13424 if (!base)
13425 return false;
13427 /* For objects in symbol table check if we know they are non-zero.
13428 Don't do anything for variables and functions before symtab is built;
13429 it is quite possible that they will be declared weak later. */
13430 int nonzero_addr = maybe_nonzero_address (base);
13431 if (nonzero_addr >= 0)
13432 return nonzero_addr;
13434 /* Function local objects are never NULL. */
13435 if (DECL_P (base)
13436 && (DECL_CONTEXT (base)
13437 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13438 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13439 return true;
13441 /* Constants are never weak. */
13442 if (CONSTANT_CLASS_P (base))
13443 return true;
13445 return false;
13448 case COND_EXPR:
13449 sub_strict_overflow_p = false;
13450 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13451 &sub_strict_overflow_p)
13452 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13453 &sub_strict_overflow_p))
13455 if (sub_strict_overflow_p)
13456 *strict_overflow_p = true;
13457 return true;
13459 break;
13461 default:
13462 break;
13464 return false;
13467 #define integer_valued_real_p(X) \
13468 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13470 #define RECURSE(X) \
13471 ((integer_valued_real_p) (X, depth + 1))
13473 /* Return true if the floating point result of (CODE OP0) has an
13474 integer value. We also allow +Inf, -Inf and NaN to be considered
13475 integer values. Return false for signaling NaN.
13477 DEPTH is the current nesting depth of the query. */
13479 bool
13480 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13482 switch (code)
13484 case FLOAT_EXPR:
13485 return true;
13487 case ABS_EXPR:
13488 return RECURSE (op0);
13490 CASE_CONVERT:
13492 tree type = TREE_TYPE (op0);
13493 if (TREE_CODE (type) == INTEGER_TYPE)
13494 return true;
13495 if (TREE_CODE (type) == REAL_TYPE)
13496 return RECURSE (op0);
13497 break;
13500 default:
13501 break;
13503 return false;
13506 /* Return true if the floating point result of (CODE OP0 OP1) has an
13507 integer value. We also allow +Inf, -Inf and NaN to be considered
13508 integer values. Return false for signaling NaN.
13510 DEPTH is the current nesting depth of the query. */
13512 bool
13513 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13515 switch (code)
13517 case PLUS_EXPR:
13518 case MINUS_EXPR:
13519 case MULT_EXPR:
13520 case MIN_EXPR:
13521 case MAX_EXPR:
13522 return RECURSE (op0) && RECURSE (op1);
13524 default:
13525 break;
13527 return false;
13530 /* Return true if the floating point result of calling FNDECL with arguments
13531 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13532 considered integer values. Return false for signaling NaN. If FNDECL
13533 takes fewer than 2 arguments, the remaining ARGn are null.
13535 DEPTH is the current nesting depth of the query. */
13537 bool
13538 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13540 switch (fn)
13542 CASE_CFN_CEIL:
13543 CASE_CFN_FLOOR:
13544 CASE_CFN_NEARBYINT:
13545 CASE_CFN_RINT:
13546 CASE_CFN_ROUND:
13547 CASE_CFN_TRUNC:
13548 return true;
13550 CASE_CFN_FMIN:
13551 CASE_CFN_FMAX:
13552 return RECURSE (arg0) && RECURSE (arg1);
13554 default:
13555 break;
13557 return false;
13560 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13561 has an integer value. We also allow +Inf, -Inf and NaN to be
13562 considered integer values. Return false for signaling NaN.
13564 DEPTH is the current nesting depth of the query. */
13566 bool
13567 integer_valued_real_single_p (tree t, int depth)
13569 switch (TREE_CODE (t))
13571 case REAL_CST:
13572 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13574 case COND_EXPR:
13575 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13577 case SSA_NAME:
13578 /* Limit the depth of recursion to avoid quadratic behavior.
13579 This is expected to catch almost all occurrences in practice.
13580 If this code misses important cases that unbounded recursion
13581 would not, passes that need this information could be revised
13582 to provide it through dataflow propagation. */
13583 return (!name_registered_for_update_p (t)
13584 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13585 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13586 depth));
13588 default:
13589 break;
13591 return false;
13594 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13595 has an integer value. We also allow +Inf, -Inf and NaN to be
13596 considered integer values. Return false for signaling NaN.
13598 DEPTH is the current nesting depth of the query. */
13600 static bool
13601 integer_valued_real_invalid_p (tree t, int depth)
13603 switch (TREE_CODE (t))
13605 case COMPOUND_EXPR:
13606 case MODIFY_EXPR:
13607 case BIND_EXPR:
13608 return RECURSE (TREE_OPERAND (t, 1));
13610 case SAVE_EXPR:
13611 return RECURSE (TREE_OPERAND (t, 0));
13613 default:
13614 break;
13616 return false;
13619 #undef RECURSE
13620 #undef integer_valued_real_p
13622 /* Return true if the floating point expression T has an integer value.
13623 We also allow +Inf, -Inf and NaN to be considered integer values.
13624 Return false for signaling NaN.
13626 DEPTH is the current nesting depth of the query. */
13628 bool
13629 integer_valued_real_p (tree t, int depth)
13631 if (t == error_mark_node)
13632 return false;
13634 tree_code code = TREE_CODE (t);
13635 switch (TREE_CODE_CLASS (code))
13637 case tcc_binary:
13638 case tcc_comparison:
13639 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13640 TREE_OPERAND (t, 1), depth);
13642 case tcc_unary:
13643 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13645 case tcc_constant:
13646 case tcc_declaration:
13647 case tcc_reference:
13648 return integer_valued_real_single_p (t, depth);
13650 default:
13651 break;
13654 switch (code)
13656 case COND_EXPR:
13657 case SSA_NAME:
13658 return integer_valued_real_single_p (t, depth);
13660 case CALL_EXPR:
13662 tree arg0 = (call_expr_nargs (t) > 0
13663 ? CALL_EXPR_ARG (t, 0)
13664 : NULL_TREE);
13665 tree arg1 = (call_expr_nargs (t) > 1
13666 ? CALL_EXPR_ARG (t, 1)
13667 : NULL_TREE);
13668 return integer_valued_real_call_p (get_call_combined_fn (t),
13669 arg0, arg1, depth);
13672 default:
13673 return integer_valued_real_invalid_p (t, depth);
13677 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13678 attempt to fold the expression to a constant without modifying TYPE,
13679 OP0 or OP1.
13681 If the expression could be simplified to a constant, then return
13682 the constant. If the expression would not be simplified to a
13683 constant, then return NULL_TREE. */
13685 tree
13686 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13688 tree tem = fold_binary (code, type, op0, op1);
13689 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13692 /* Given the components of a unary expression CODE, TYPE and OP0,
13693 attempt to fold the expression to a constant without modifying
13694 TYPE or OP0.
13696 If the expression could be simplified to a constant, then return
13697 the constant. If the expression would not be simplified to a
13698 constant, then return NULL_TREE. */
13700 tree
13701 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13703 tree tem = fold_unary (code, type, op0);
13704 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13707 /* If EXP represents referencing an element in a constant string
13708 (either via pointer arithmetic or array indexing), return the
13709 tree representing the value accessed, otherwise return NULL. */
13711 tree
13712 fold_read_from_constant_string (tree exp)
13714 if ((TREE_CODE (exp) == INDIRECT_REF
13715 || TREE_CODE (exp) == ARRAY_REF)
13716 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13718 tree exp1 = TREE_OPERAND (exp, 0);
13719 tree index;
13720 tree string;
13721 location_t loc = EXPR_LOCATION (exp);
13723 if (TREE_CODE (exp) == INDIRECT_REF)
13724 string = string_constant (exp1, &index);
13725 else
13727 tree low_bound = array_ref_low_bound (exp);
13728 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13730 /* Optimize the special-case of a zero lower bound.
13732 We convert the low_bound to sizetype to avoid some problems
13733 with constant folding. (E.g. suppose the lower bound is 1,
13734 and its mode is QI. Without the conversion,l (ARRAY
13735 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13736 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13737 if (! integer_zerop (low_bound))
13738 index = size_diffop_loc (loc, index,
13739 fold_convert_loc (loc, sizetype, low_bound));
13741 string = exp1;
13744 if (string
13745 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13746 && TREE_CODE (string) == STRING_CST
13747 && TREE_CODE (index) == INTEGER_CST
13748 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13749 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13750 == MODE_INT)
13751 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13752 return build_int_cst_type (TREE_TYPE (exp),
13753 (TREE_STRING_POINTER (string)
13754 [TREE_INT_CST_LOW (index)]));
13756 return NULL;
13759 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13760 an integer constant, real, or fixed-point constant.
13762 TYPE is the type of the result. */
13764 static tree
13765 fold_negate_const (tree arg0, tree type)
13767 tree t = NULL_TREE;
13769 switch (TREE_CODE (arg0))
13771 case INTEGER_CST:
13773 bool overflow;
13774 wide_int val = wi::neg (arg0, &overflow);
13775 t = force_fit_type (type, val, 1,
13776 (overflow | TREE_OVERFLOW (arg0))
13777 && !TYPE_UNSIGNED (type));
13778 break;
13781 case REAL_CST:
13782 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13783 break;
13785 case FIXED_CST:
13787 FIXED_VALUE_TYPE f;
13788 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13789 &(TREE_FIXED_CST (arg0)), NULL,
13790 TYPE_SATURATING (type));
13791 t = build_fixed (type, f);
13792 /* Propagate overflow flags. */
13793 if (overflow_p | TREE_OVERFLOW (arg0))
13794 TREE_OVERFLOW (t) = 1;
13795 break;
13798 default:
13799 gcc_unreachable ();
13802 return t;
13805 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13806 an integer constant or real constant.
13808 TYPE is the type of the result. */
13810 tree
13811 fold_abs_const (tree arg0, tree type)
13813 tree t = NULL_TREE;
13815 switch (TREE_CODE (arg0))
13817 case INTEGER_CST:
13819 /* If the value is unsigned or non-negative, then the absolute value
13820 is the same as the ordinary value. */
13821 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13822 t = arg0;
13824 /* If the value is negative, then the absolute value is
13825 its negation. */
13826 else
13828 bool overflow;
13829 wide_int val = wi::neg (arg0, &overflow);
13830 t = force_fit_type (type, val, -1,
13831 overflow | TREE_OVERFLOW (arg0));
13834 break;
13836 case REAL_CST:
13837 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13838 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13839 else
13840 t = arg0;
13841 break;
13843 default:
13844 gcc_unreachable ();
13847 return t;
13850 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13851 constant. TYPE is the type of the result. */
13853 static tree
13854 fold_not_const (const_tree arg0, tree type)
13856 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13858 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13861 /* Given CODE, a relational operator, the target type, TYPE and two
13862 constant operands OP0 and OP1, return the result of the
13863 relational operation. If the result is not a compile time
13864 constant, then return NULL_TREE. */
13866 static tree
13867 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13869 int result, invert;
13871 /* From here on, the only cases we handle are when the result is
13872 known to be a constant. */
13874 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13876 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13877 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13879 /* Handle the cases where either operand is a NaN. */
13880 if (real_isnan (c0) || real_isnan (c1))
13882 switch (code)
13884 case EQ_EXPR:
13885 case ORDERED_EXPR:
13886 result = 0;
13887 break;
13889 case NE_EXPR:
13890 case UNORDERED_EXPR:
13891 case UNLT_EXPR:
13892 case UNLE_EXPR:
13893 case UNGT_EXPR:
13894 case UNGE_EXPR:
13895 case UNEQ_EXPR:
13896 result = 1;
13897 break;
13899 case LT_EXPR:
13900 case LE_EXPR:
13901 case GT_EXPR:
13902 case GE_EXPR:
13903 case LTGT_EXPR:
13904 if (flag_trapping_math)
13905 return NULL_TREE;
13906 result = 0;
13907 break;
13909 default:
13910 gcc_unreachable ();
13913 return constant_boolean_node (result, type);
13916 return constant_boolean_node (real_compare (code, c0, c1), type);
13919 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13921 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13922 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13923 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13926 /* Handle equality/inequality of complex constants. */
13927 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13929 tree rcond = fold_relational_const (code, type,
13930 TREE_REALPART (op0),
13931 TREE_REALPART (op1));
13932 tree icond = fold_relational_const (code, type,
13933 TREE_IMAGPART (op0),
13934 TREE_IMAGPART (op1));
13935 if (code == EQ_EXPR)
13936 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13937 else if (code == NE_EXPR)
13938 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13939 else
13940 return NULL_TREE;
13943 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13945 if (!VECTOR_TYPE_P (type))
13947 /* Have vector comparison with scalar boolean result. */
13948 bool result = true;
13949 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13950 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13951 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13953 tree elem0 = VECTOR_CST_ELT (op0, i);
13954 tree elem1 = VECTOR_CST_ELT (op1, i);
13955 tree tmp = fold_relational_const (code, type, elem0, elem1);
13956 result &= integer_onep (tmp);
13958 if (code == NE_EXPR)
13959 result = !result;
13960 return constant_boolean_node (result, type);
13962 unsigned count = VECTOR_CST_NELTS (op0);
13963 tree *elts = XALLOCAVEC (tree, count);
13964 gcc_assert (VECTOR_CST_NELTS (op1) == count
13965 && TYPE_VECTOR_SUBPARTS (type) == count);
13967 for (unsigned i = 0; i < count; i++)
13969 tree elem_type = TREE_TYPE (type);
13970 tree elem0 = VECTOR_CST_ELT (op0, i);
13971 tree elem1 = VECTOR_CST_ELT (op1, i);
13973 tree tem = fold_relational_const (code, elem_type,
13974 elem0, elem1);
13976 if (tem == NULL_TREE)
13977 return NULL_TREE;
13979 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13982 return build_vector (type, elts);
13985 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13987 To compute GT, swap the arguments and do LT.
13988 To compute GE, do LT and invert the result.
13989 To compute LE, swap the arguments, do LT and invert the result.
13990 To compute NE, do EQ and invert the result.
13992 Therefore, the code below must handle only EQ and LT. */
13994 if (code == LE_EXPR || code == GT_EXPR)
13996 std::swap (op0, op1);
13997 code = swap_tree_comparison (code);
14000 /* Note that it is safe to invert for real values here because we
14001 have already handled the one case that it matters. */
14003 invert = 0;
14004 if (code == NE_EXPR || code == GE_EXPR)
14006 invert = 1;
14007 code = invert_tree_comparison (code, false);
14010 /* Compute a result for LT or EQ if args permit;
14011 Otherwise return T. */
14012 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14014 if (code == EQ_EXPR)
14015 result = tree_int_cst_equal (op0, op1);
14016 else
14017 result = tree_int_cst_lt (op0, op1);
14019 else
14020 return NULL_TREE;
14022 if (invert)
14023 result ^= 1;
14024 return constant_boolean_node (result, type);
14027 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14028 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14029 itself. */
14031 tree
14032 fold_build_cleanup_point_expr (tree type, tree expr)
14034 /* If the expression does not have side effects then we don't have to wrap
14035 it with a cleanup point expression. */
14036 if (!TREE_SIDE_EFFECTS (expr))
14037 return expr;
14039 /* If the expression is a return, check to see if the expression inside the
14040 return has no side effects or the right hand side of the modify expression
14041 inside the return. If either don't have side effects set we don't need to
14042 wrap the expression in a cleanup point expression. Note we don't check the
14043 left hand side of the modify because it should always be a return decl. */
14044 if (TREE_CODE (expr) == RETURN_EXPR)
14046 tree op = TREE_OPERAND (expr, 0);
14047 if (!op || !TREE_SIDE_EFFECTS (op))
14048 return expr;
14049 op = TREE_OPERAND (op, 1);
14050 if (!TREE_SIDE_EFFECTS (op))
14051 return expr;
14054 return build1 (CLEANUP_POINT_EXPR, type, expr);
14057 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14058 of an indirection through OP0, or NULL_TREE if no simplification is
14059 possible. */
14061 tree
14062 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14064 tree sub = op0;
14065 tree subtype;
14067 STRIP_NOPS (sub);
14068 subtype = TREE_TYPE (sub);
14069 if (!POINTER_TYPE_P (subtype))
14070 return NULL_TREE;
14072 if (TREE_CODE (sub) == ADDR_EXPR)
14074 tree op = TREE_OPERAND (sub, 0);
14075 tree optype = TREE_TYPE (op);
14076 /* *&CONST_DECL -> to the value of the const decl. */
14077 if (TREE_CODE (op) == CONST_DECL)
14078 return DECL_INITIAL (op);
14079 /* *&p => p; make sure to handle *&"str"[cst] here. */
14080 if (type == optype)
14082 tree fop = fold_read_from_constant_string (op);
14083 if (fop)
14084 return fop;
14085 else
14086 return op;
14088 /* *(foo *)&fooarray => fooarray[0] */
14089 else if (TREE_CODE (optype) == ARRAY_TYPE
14090 && type == TREE_TYPE (optype)
14091 && (!in_gimple_form
14092 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14094 tree type_domain = TYPE_DOMAIN (optype);
14095 tree min_val = size_zero_node;
14096 if (type_domain && TYPE_MIN_VALUE (type_domain))
14097 min_val = TYPE_MIN_VALUE (type_domain);
14098 if (in_gimple_form
14099 && TREE_CODE (min_val) != INTEGER_CST)
14100 return NULL_TREE;
14101 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14102 NULL_TREE, NULL_TREE);
14104 /* *(foo *)&complexfoo => __real__ complexfoo */
14105 else if (TREE_CODE (optype) == COMPLEX_TYPE
14106 && type == TREE_TYPE (optype))
14107 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14108 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14109 else if (TREE_CODE (optype) == VECTOR_TYPE
14110 && type == TREE_TYPE (optype))
14112 tree part_width = TYPE_SIZE (type);
14113 tree index = bitsize_int (0);
14114 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14118 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14119 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14121 tree op00 = TREE_OPERAND (sub, 0);
14122 tree op01 = TREE_OPERAND (sub, 1);
14124 STRIP_NOPS (op00);
14125 if (TREE_CODE (op00) == ADDR_EXPR)
14127 tree op00type;
14128 op00 = TREE_OPERAND (op00, 0);
14129 op00type = TREE_TYPE (op00);
14131 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14132 if (TREE_CODE (op00type) == VECTOR_TYPE
14133 && type == TREE_TYPE (op00type))
14135 tree part_width = TYPE_SIZE (type);
14136 unsigned HOST_WIDE_INT max_offset
14137 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14138 * TYPE_VECTOR_SUBPARTS (op00type));
14139 if (tree_int_cst_sign_bit (op01) == 0
14140 && compare_tree_int (op01, max_offset) == -1)
14142 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14143 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14144 tree index = bitsize_int (indexi);
14145 return fold_build3_loc (loc,
14146 BIT_FIELD_REF, type, op00,
14147 part_width, index);
14150 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14151 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14152 && type == TREE_TYPE (op00type))
14154 tree size = TYPE_SIZE_UNIT (type);
14155 if (tree_int_cst_equal (size, op01))
14156 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14158 /* ((foo *)&fooarray)[1] => fooarray[1] */
14159 else if (TREE_CODE (op00type) == ARRAY_TYPE
14160 && type == TREE_TYPE (op00type))
14162 tree type_domain = TYPE_DOMAIN (op00type);
14163 tree min_val = size_zero_node;
14164 if (type_domain && TYPE_MIN_VALUE (type_domain))
14165 min_val = TYPE_MIN_VALUE (type_domain);
14166 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14167 TYPE_SIZE_UNIT (type));
14168 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14169 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14170 NULL_TREE, NULL_TREE);
14175 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14176 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14177 && type == TREE_TYPE (TREE_TYPE (subtype))
14178 && (!in_gimple_form
14179 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14181 tree type_domain;
14182 tree min_val = size_zero_node;
14183 sub = build_fold_indirect_ref_loc (loc, sub);
14184 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14185 if (type_domain && TYPE_MIN_VALUE (type_domain))
14186 min_val = TYPE_MIN_VALUE (type_domain);
14187 if (in_gimple_form
14188 && TREE_CODE (min_val) != INTEGER_CST)
14189 return NULL_TREE;
14190 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14191 NULL_TREE);
14194 return NULL_TREE;
14197 /* Builds an expression for an indirection through T, simplifying some
14198 cases. */
14200 tree
14201 build_fold_indirect_ref_loc (location_t loc, tree t)
14203 tree type = TREE_TYPE (TREE_TYPE (t));
14204 tree sub = fold_indirect_ref_1 (loc, type, t);
14206 if (sub)
14207 return sub;
14209 return build1_loc (loc, INDIRECT_REF, type, t);
14212 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14214 tree
14215 fold_indirect_ref_loc (location_t loc, tree t)
14217 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14219 if (sub)
14220 return sub;
14221 else
14222 return t;
14225 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14226 whose result is ignored. The type of the returned tree need not be
14227 the same as the original expression. */
14229 tree
14230 fold_ignored_result (tree t)
14232 if (!TREE_SIDE_EFFECTS (t))
14233 return integer_zero_node;
14235 for (;;)
14236 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14238 case tcc_unary:
14239 t = TREE_OPERAND (t, 0);
14240 break;
14242 case tcc_binary:
14243 case tcc_comparison:
14244 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14245 t = TREE_OPERAND (t, 0);
14246 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14247 t = TREE_OPERAND (t, 1);
14248 else
14249 return t;
14250 break;
14252 case tcc_expression:
14253 switch (TREE_CODE (t))
14255 case COMPOUND_EXPR:
14256 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14257 return t;
14258 t = TREE_OPERAND (t, 0);
14259 break;
14261 case COND_EXPR:
14262 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14263 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14264 return t;
14265 t = TREE_OPERAND (t, 0);
14266 break;
14268 default:
14269 return t;
14271 break;
14273 default:
14274 return t;
14278 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14280 tree
14281 round_up_loc (location_t loc, tree value, unsigned int divisor)
14283 tree div = NULL_TREE;
14285 if (divisor == 1)
14286 return value;
14288 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14289 have to do anything. Only do this when we are not given a const,
14290 because in that case, this check is more expensive than just
14291 doing it. */
14292 if (TREE_CODE (value) != INTEGER_CST)
14294 div = build_int_cst (TREE_TYPE (value), divisor);
14296 if (multiple_of_p (TREE_TYPE (value), value, div))
14297 return value;
14300 /* If divisor is a power of two, simplify this to bit manipulation. */
14301 if (divisor == (divisor & -divisor))
14303 if (TREE_CODE (value) == INTEGER_CST)
14305 wide_int val = value;
14306 bool overflow_p;
14308 if ((val & (divisor - 1)) == 0)
14309 return value;
14311 overflow_p = TREE_OVERFLOW (value);
14312 val += divisor - 1;
14313 val &= - (int) divisor;
14314 if (val == 0)
14315 overflow_p = true;
14317 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14319 else
14321 tree t;
14323 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14324 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14325 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14326 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14329 else
14331 if (!div)
14332 div = build_int_cst (TREE_TYPE (value), divisor);
14333 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14334 value = size_binop_loc (loc, MULT_EXPR, value, div);
14337 return value;
14340 /* Likewise, but round down. */
14342 tree
14343 round_down_loc (location_t loc, tree value, int divisor)
14345 tree div = NULL_TREE;
14347 gcc_assert (divisor > 0);
14348 if (divisor == 1)
14349 return value;
14351 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14352 have to do anything. Only do this when we are not given a const,
14353 because in that case, this check is more expensive than just
14354 doing it. */
14355 if (TREE_CODE (value) != INTEGER_CST)
14357 div = build_int_cst (TREE_TYPE (value), divisor);
14359 if (multiple_of_p (TREE_TYPE (value), value, div))
14360 return value;
14363 /* If divisor is a power of two, simplify this to bit manipulation. */
14364 if (divisor == (divisor & -divisor))
14366 tree t;
14368 t = build_int_cst (TREE_TYPE (value), -divisor);
14369 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14371 else
14373 if (!div)
14374 div = build_int_cst (TREE_TYPE (value), divisor);
14375 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14376 value = size_binop_loc (loc, MULT_EXPR, value, div);
14379 return value;
14382 /* Returns the pointer to the base of the object addressed by EXP and
14383 extracts the information about the offset of the access, storing it
14384 to PBITPOS and POFFSET. */
14386 static tree
14387 split_address_to_core_and_offset (tree exp,
14388 HOST_WIDE_INT *pbitpos, tree *poffset)
14390 tree core;
14391 machine_mode mode;
14392 int unsignedp, reversep, volatilep;
14393 HOST_WIDE_INT bitsize;
14394 location_t loc = EXPR_LOCATION (exp);
14396 if (TREE_CODE (exp) == ADDR_EXPR)
14398 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14399 poffset, &mode, &unsignedp, &reversep,
14400 &volatilep, false);
14401 core = build_fold_addr_expr_loc (loc, core);
14403 else
14405 core = exp;
14406 *pbitpos = 0;
14407 *poffset = NULL_TREE;
14410 return core;
14413 /* Returns true if addresses of E1 and E2 differ by a constant, false
14414 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14416 bool
14417 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14419 tree core1, core2;
14420 HOST_WIDE_INT bitpos1, bitpos2;
14421 tree toffset1, toffset2, tdiff, type;
14423 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14424 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14426 if (bitpos1 % BITS_PER_UNIT != 0
14427 || bitpos2 % BITS_PER_UNIT != 0
14428 || !operand_equal_p (core1, core2, 0))
14429 return false;
14431 if (toffset1 && toffset2)
14433 type = TREE_TYPE (toffset1);
14434 if (type != TREE_TYPE (toffset2))
14435 toffset2 = fold_convert (type, toffset2);
14437 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14438 if (!cst_and_fits_in_hwi (tdiff))
14439 return false;
14441 *diff = int_cst_value (tdiff);
14443 else if (toffset1 || toffset2)
14445 /* If only one of the offsets is non-constant, the difference cannot
14446 be a constant. */
14447 return false;
14449 else
14450 *diff = 0;
14452 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14453 return true;
14456 /* Return OFF converted to a pointer offset type suitable as offset for
14457 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14458 tree
14459 convert_to_ptrofftype_loc (location_t loc, tree off)
14461 return fold_convert_loc (loc, sizetype, off);
14464 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14465 tree
14466 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14468 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14469 ptr, convert_to_ptrofftype_loc (loc, off));
14472 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14473 tree
14474 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14476 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14477 ptr, size_int (off));
14480 /* Return a char pointer for a C string if it is a string constant
14481 or sum of string constant and integer constant. */
14483 const char *
14484 c_getstr (tree src)
14486 tree offset_node;
14488 src = string_constant (src, &offset_node);
14489 if (src == 0)
14490 return 0;
14492 if (offset_node == 0)
14493 return TREE_STRING_POINTER (src);
14494 else if (!tree_fits_uhwi_p (offset_node)
14495 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
14496 return 0;
14498 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
14501 #if CHECKING_P
14503 namespace selftest {
14505 /* Helper functions for writing tests of folding trees. */
14507 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14509 static void
14510 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14511 tree constant)
14513 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14516 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14517 wrapping WRAPPED_EXPR. */
14519 static void
14520 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14521 tree wrapped_expr)
14523 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14524 ASSERT_NE (wrapped_expr, result);
14525 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14526 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14529 /* Verify that various arithmetic binary operations are folded
14530 correctly. */
14532 static void
14533 test_arithmetic_folding ()
14535 tree type = integer_type_node;
14536 tree x = create_tmp_var_raw (type, "x");
14537 tree zero = build_zero_cst (type);
14538 tree one = build_int_cst (type, 1);
14540 /* Addition. */
14541 /* 1 <-- (0 + 1) */
14542 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14543 one);
14544 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14545 one);
14547 /* (nonlvalue)x <-- (x + 0) */
14548 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14551 /* Subtraction. */
14552 /* 0 <-- (x - x) */
14553 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14554 zero);
14555 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14558 /* Multiplication. */
14559 /* 0 <-- (x * 0) */
14560 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14561 zero);
14563 /* (nonlvalue)x <-- (x * 1) */
14564 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14568 /* Run all of the selftests within this file. */
14570 void
14571 fold_const_c_tests ()
14573 test_arithmetic_folding ();
14576 } // namespace selftest
14578 #endif /* CHECKING_P */