Debian Codegraph @

gcc/fold-const.cc

raw
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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. */
27
28 /* The entry points in this file are fold, size_int_wide and size_binop.
29
30 fold takes a tree as argument and returns a simplified tree.
31
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'.
35
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
38
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. */
42
43 #define INCLUDE_ALGORITHM
44 #include "config.h"
45 #include "system.h"
46 #include "coretypes.h"
47 #include "backend.h"
48 #include "target.h"
49 #include "rtl.h"
50 #include "tree.h"
51 #include "gimple.h"
52 #include "predict.h"
53 #include "memmodel.h"
54 #include "tm_p.h"
55 #include "tree-ssa-operands.h"
56 #include "optabs-query.h"
57 #include "cgraph.h"
58 #include "diagnostic-core.h"
59 #include "flags.h"
60 #include "alias.h"
61 #include "fold-const.h"
62 #include "fold-const-call.h"
63 #include "stor-layout.h"
64 #include "calls.h"
65 #include "tree-iterator.h"
66 #include "expr.h"
67 #include "intl.h"
68 #include "langhooks.h"
69 #include "tree-eh.h"
70 #include "gimplify.h"
71 #include "tree-dfa.h"
72 #include "builtins.h"
73 #include "generic-match.h"
74 #include "gimple-iterator.h"
75 #include "gimple-fold.h"
76 #include "tree-into-ssa.h"
77 #include "md5.h"
78 #include "case-cfn-macros.h"
79 #include "stringpool.h"
80 #include "tree-vrp.h"
81 #include "tree-ssanames.h"
82 #include "selftest.h"
83 #include "stringpool.h"
84 #include "attribs.h"
85 #include "tree-vector-builder.h"
86 #include "vec-perm-indices.h"
87 #include "asan.h"
88 #include "gimple-range.h"
89 #include "optabs-tree.h"
90
91 /* Nonzero if we are folding constants inside an initializer or a C++
92 manifestly-constant-evaluated context; zero otherwise.
93 Should be used when folding in initializer enables additional
94 optimizations. */
95 int folding_initializer = 0;
96
97 /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
98 otherwise.
99 Should be used when certain constructs shouldn't be optimized
100 during folding in that context. */
101 bool folding_cxx_constexpr = false;
102
103 /* The following constants represent a bit based encoding of GCC's
104 comparison operators. This encoding simplifies transformations
105 on relational comparison operators, such as AND and OR. */
106 enum comparison_code {
107 COMPCODE_FALSE = 0,
108 COMPCODE_LT = 1,
109 COMPCODE_EQ = 2,
110 COMPCODE_LE = 3,
111 COMPCODE_GT = 4,
112 COMPCODE_LTGT = 5,
113 COMPCODE_GE = 6,
114 COMPCODE_ORD = 7,
115 COMPCODE_UNORD = 8,
116 COMPCODE_UNLT = 9,
117 COMPCODE_UNEQ = 10,
118 COMPCODE_UNLE = 11,
119 COMPCODE_UNGT = 12,
120 COMPCODE_NE = 13,
121 COMPCODE_UNGE = 14,
122 COMPCODE_TRUE = 15
123 };
124
125 static bool negate_expr_p (tree);
126 static tree negate_expr (tree);
127 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
128 static enum comparison_code comparison_to_compcode (enum tree_code);
129 static enum tree_code compcode_to_comparison (enum comparison_code);
130 static bool twoval_comparison_p (tree, tree *, tree *);
131 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
132 static tree optimize_bit_field_compare (location_t, enum tree_code,
133 tree, tree, tree);
134 static bool simple_operand_p (const_tree);
135 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
136 static tree range_predecessor (tree);
137 static tree range_successor (tree);
138 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
139 static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
140 tree, tree, tree, tree);
141 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
142 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
143 static tree fold_binary_op_with_conditional_arg (location_t,
144 enum tree_code, tree,
145 tree, tree,
146 tree, tree, int);
147 static tree fold_negate_const (tree, tree);
148 static tree fold_not_const (const_tree, tree);
149 static tree fold_relational_const (enum tree_code, tree, tree, tree);
150 static tree fold_convert_const (enum tree_code, tree, tree);
151 static tree fold_view_convert_expr (tree, tree);
152 static tree fold_negate_expr (location_t, tree);
153
154 /* This is a helper function to detect min/max for some operands of COND_EXPR.
155 The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
156 tree_code
157 minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
158 {
159 enum tree_code code = ERROR_MARK;
160
161 if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
162 return ERROR_MARK;
163
164 if (!operand_equal_p (exp0, exp2))
165 return ERROR_MARK;
166
167 if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST)
168 {
169 if (wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
170 {
171 /* X <= Y - 1 equals to X < Y. */
172 if (cmp == LE_EXPR)
173 code = LT_EXPR;
174 /* X > Y - 1 equals to X >= Y. */
175 if (cmp == GT_EXPR)
176 code = GE_EXPR;
177 /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
178 if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
179 {
180 int_range_max r;
181 get_range_query (cfun)->range_of_expr (r, exp0);
182 if (r.undefined_p ())
183 r.set_varying (TREE_TYPE (exp0));
184
185 widest_int min = widest_int::from (r.lower_bound (),
186 TYPE_SIGN (TREE_TYPE (exp0)));
187 if (min == wi::to_widest (exp1))
188 code = MAX_EXPR;
189 }
190 }
191 if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
192 {
193 /* X < Y + 1 equals to X <= Y. */
194 if (cmp == LT_EXPR)
195 code = LE_EXPR;
196 /* X >= Y + 1 equals to X > Y. */
197 if (cmp == GE_EXPR)
198 code = GT_EXPR;
199 /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
200 if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
201 {
202 int_range_max r;
203 get_range_query (cfun)->range_of_expr (r, exp0);
204 if (r.undefined_p ())
205 r.set_varying (TREE_TYPE (exp0));
206
207 widest_int max = widest_int::from (r.upper_bound (),
208 TYPE_SIGN (TREE_TYPE (exp0)));
209 if (max == wi::to_widest (exp1))
210 code = MIN_EXPR;
211 }
212 }
213 }
214 if (code != ERROR_MARK
215 || operand_equal_p (exp1, exp3))
216 {
217 if (cmp == LT_EXPR || cmp == LE_EXPR)
218 code = MIN_EXPR;
219 if (cmp == GT_EXPR || cmp == GE_EXPR)
220 code = MAX_EXPR;
221 }
222 return code;
223 }
224
225 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
226 Otherwise, return LOC. */
227
228 static location_t
229 expr_location_or (tree t, location_t loc)
230 {
231 location_t tloc = EXPR_LOCATION (t);
232 return tloc == UNKNOWN_LOCATION ? loc : tloc;
233 }
234
235 /* Similar to protected_set_expr_location, but never modify x in place,
236 if location can and needs to be set, unshare it. */
237
238 tree
239 protected_set_expr_location_unshare (tree x, location_t loc)
240 {
241 if (CAN_HAVE_LOCATION_P (x)
242 && EXPR_LOCATION (x) != loc
243 && !(TREE_CODE (x) == SAVE_EXPR
244 || TREE_CODE (x) == TARGET_EXPR
245 || TREE_CODE (x) == BIND_EXPR))
246 {
247 x = copy_node (x);
248 SET_EXPR_LOCATION (x, loc);
249 }
250 return x;
251 }
252
253 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
254 division and returns the quotient. Otherwise returns
255 NULL_TREE. */
256
257 tree
258 div_if_zero_remainder (const_tree arg1, const_tree arg2)
259 {
260 widest_int quo;
261
262 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
263 SIGNED, &quo))
264 return wide_int_to_tree (TREE_TYPE (arg1), quo);
265
266 return NULL_TREE;
267 }
268
269 /* This is nonzero if we should defer warnings about undefined
270 overflow. This facility exists because these warnings are a
271 special case. The code to estimate loop iterations does not want
272 to issue any warnings, since it works with expressions which do not
273 occur in user code. Various bits of cleanup code call fold(), but
274 only use the result if it has certain characteristics (e.g., is a
275 constant); that code only wants to issue a warning if the result is
276 used. */
277
278 static int fold_deferring_overflow_warnings;
279
280 /* If a warning about undefined overflow is deferred, this is the
281 warning. Note that this may cause us to turn two warnings into
282 one, but that is fine since it is sufficient to only give one
283 warning per expression. */
284
285 static const char* fold_deferred_overflow_warning;
286
287 /* If a warning about undefined overflow is deferred, this is the
288 level at which the warning should be emitted. */
289
290 static enum warn_strict_overflow_code fold_deferred_overflow_code;
291
292 /* Start deferring overflow warnings. We could use a stack here to
293 permit nested calls, but at present it is not necessary. */
294
295 void
296 fold_defer_overflow_warnings (void)
297 {
298 ++fold_deferring_overflow_warnings;
299 }
300
301 /* Stop deferring overflow warnings. If there is a pending warning,
302 and ISSUE is true, then issue the warning if appropriate. STMT is
303 the statement with which the warning should be associated (used for
304 location information); STMT may be NULL. CODE is the level of the
305 warning--a warn_strict_overflow_code value. This function will use
306 the smaller of CODE and the deferred code when deciding whether to
307 issue the warning. CODE may be zero to mean to always use the
308 deferred code. */
309
310 void
311 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
312 {
313 const char *warnmsg;
314 location_t locus;
315
316 gcc_assert (fold_deferring_overflow_warnings > 0);
317 --fold_deferring_overflow_warnings;
318 if (fold_deferring_overflow_warnings > 0)
319 {
320 if (fold_deferred_overflow_warning != NULL
321 && code != 0
322 && code < (int) fold_deferred_overflow_code)
323 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
324 return;
325 }
326
327 warnmsg = fold_deferred_overflow_warning;
328 fold_deferred_overflow_warning = NULL;
329
330 if (!issue || warnmsg == NULL)
331 return;
332
333 if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
334 return;
335
336 /* Use the smallest code level when deciding to issue the
337 warning. */
338 if (code == 0 || code > (int) fold_deferred_overflow_code)
339 code = fold_deferred_overflow_code;
340
341 if (!issue_strict_overflow_warning (code))
342 return;
343
344 if (stmt == NULL)
345 locus = input_location;
346 else
347 locus = gimple_location (stmt);
348 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
349 }
350
351 /* Stop deferring overflow warnings, ignoring any deferred
352 warnings. */
353
354 void
355 fold_undefer_and_ignore_overflow_warnings (void)
356 {
357 fold_undefer_overflow_warnings (false, NULL, 0);
358 }
359
360 /* Whether we are deferring overflow warnings. */
361
362 bool
363 fold_deferring_overflow_warnings_p (void)
364 {
365 return fold_deferring_overflow_warnings > 0;
366 }
367
368 /* This is called when we fold something based on the fact that signed
369 overflow is undefined. */
370
371 void
372 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
373 {
374 if (fold_deferring_overflow_warnings > 0)
375 {
376 if (fold_deferred_overflow_warning == NULL
377 || wc < fold_deferred_overflow_code)
378 {
379 fold_deferred_overflow_warning = gmsgid;
380 fold_deferred_overflow_code = wc;
381 }
382 }
383 else if (issue_strict_overflow_warning (wc))
384 warning (OPT_Wstrict_overflow, gmsgid);
385 }
386
387 /* Return true if the built-in mathematical function specified by CODE
388 is odd, i.e. -f(x) == f(-x). */
389
390 bool
391 negate_mathfn_p (combined_fn fn)
392 {
393 switch (fn)
394 {
395 CASE_CFN_ASIN:
396 CASE_CFN_ASIN_FN:
397 CASE_CFN_ASINH:
398 CASE_CFN_ASINH_FN:
399 CASE_CFN_ATAN:
400 CASE_CFN_ATAN_FN:
401 CASE_CFN_ATANH:
402 CASE_CFN_ATANH_FN:
403 CASE_CFN_CASIN:
404 CASE_CFN_CASIN_FN:
405 CASE_CFN_CASINH:
406 CASE_CFN_CASINH_FN:
407 CASE_CFN_CATAN:
408 CASE_CFN_CATAN_FN:
409 CASE_CFN_CATANH:
410 CASE_CFN_CATANH_FN:
411 CASE_CFN_CBRT:
412 CASE_CFN_CBRT_FN:
413 CASE_CFN_CPROJ:
414 CASE_CFN_CPROJ_FN:
415 CASE_CFN_CSIN:
416 CASE_CFN_CSIN_FN:
417 CASE_CFN_CSINH:
418 CASE_CFN_CSINH_FN:
419 CASE_CFN_CTAN:
420 CASE_CFN_CTAN_FN:
421 CASE_CFN_CTANH:
422 CASE_CFN_CTANH_FN:
423 CASE_CFN_ERF:
424 CASE_CFN_ERF_FN:
425 CASE_CFN_LLROUND:
426 CASE_CFN_LLROUND_FN:
427 CASE_CFN_LROUND:
428 CASE_CFN_LROUND_FN:
429 CASE_CFN_ROUND:
430 CASE_CFN_ROUNDEVEN:
431 CASE_CFN_ROUNDEVEN_FN:
432 CASE_CFN_SIN:
433 CASE_CFN_SIN_FN:
434 CASE_CFN_SINH:
435 CASE_CFN_SINH_FN:
436 CASE_CFN_TAN:
437 CASE_CFN_TAN_FN:
438 CASE_CFN_TANH:
439 CASE_CFN_TANH_FN:
440 CASE_CFN_TRUNC:
441 CASE_CFN_TRUNC_FN:
442 return true;
443
444 CASE_CFN_LLRINT:
445 CASE_CFN_LLRINT_FN:
446 CASE_CFN_LRINT:
447 CASE_CFN_LRINT_FN:
448 CASE_CFN_NEARBYINT:
449 CASE_CFN_NEARBYINT_FN:
450 CASE_CFN_RINT:
451 CASE_CFN_RINT_FN:
452 return !flag_rounding_math;
453
454 default:
455 break;
456 }
457 return false;
458 }
459
460 /* Check whether we may negate an integer constant T without causing
461 overflow. */
462
463 bool
464 may_negate_without_overflow_p (const_tree t)
465 {
466 tree type;
467
468 gcc_assert (TREE_CODE (t) == INTEGER_CST);
469
470 type = TREE_TYPE (t);
471 if (TYPE_UNSIGNED (type))
472 return false;
473
474 return !wi::only_sign_bit_p (wi::to_wide (t));
475 }
476
477 /* Determine whether an expression T can be cheaply negated using
478 the function negate_expr without introducing undefined overflow. */
479
480 static bool
481 negate_expr_p (tree t)
482 {
483 tree type;
484
485 if (t == 0)
486 return false;
487
488 type = TREE_TYPE (t);
489
490 STRIP_SIGN_NOPS (t);
491 switch (TREE_CODE (t))
492 {
493 case INTEGER_CST:
494 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
495 return true;
496
497 /* Check that -CST will not overflow type. */
498 return may_negate_without_overflow_p (t);
499 case BIT_NOT_EXPR:
500 return (INTEGRAL_TYPE_P (type)
501 && TYPE_OVERFLOW_WRAPS (type));
502
503 case FIXED_CST:
504 return true;
505
506 case NEGATE_EXPR:
507 return !TYPE_OVERFLOW_SANITIZED (type);
508
509 case REAL_CST:
510 /* We want to canonicalize to positive real constants. Pretend
511 that only negative ones can be easily negated. */
512 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
513
514 case COMPLEX_CST:
515 return negate_expr_p (TREE_REALPART (t))
516 && negate_expr_p (TREE_IMAGPART (t));
517
518 case VECTOR_CST:
519 {
520 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
521 return true;
522
523 /* Steps don't prevent negation. */
524 unsigned int count = vector_cst_encoded_nelts (t);
525 for (unsigned int i = 0; i < count; ++i)
526 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
527 return false;
528
529 return true;
530 }
531
532 case COMPLEX_EXPR:
533 return negate_expr_p (TREE_OPERAND (t, 0))
534 && negate_expr_p (TREE_OPERAND (t, 1));
535
536 case CONJ_EXPR:
537 return negate_expr_p (TREE_OPERAND (t, 0));
538
539 case PLUS_EXPR:
540 if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
541 || HONOR_SIGNED_ZEROS (type)
542 || (ANY_INTEGRAL_TYPE_P (type)
543 && ! TYPE_OVERFLOW_WRAPS (type)))
544 return false;
545 /* -(A + B) -> (-B) - A. */
546 if (negate_expr_p (TREE_OPERAND (t, 1)))
547 return true;
548 /* -(A + B) -> (-A) - B. */
549 return negate_expr_p (TREE_OPERAND (t, 0));
550
551 case MINUS_EXPR:
552 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
553 return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
554 && !HONOR_SIGNED_ZEROS (type)
555 && (! ANY_INTEGRAL_TYPE_P (type)
556 || TYPE_OVERFLOW_WRAPS (type));
557
558 case MULT_EXPR:
559 if (TYPE_UNSIGNED (type))
560 break;
561 /* INT_MIN/n * n doesn't overflow while negating one operand it does
562 if n is a (negative) power of two. */
563 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
564 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
565 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
566 && (wi::popcount
567 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
568 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
569 && (wi::popcount
570 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
571 break;
572
573 /* Fall through. */
574
575 case RDIV_EXPR:
576 if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
577 return negate_expr_p (TREE_OPERAND (t, 1))
578 || negate_expr_p (TREE_OPERAND (t, 0));
579 break;
580
581 case TRUNC_DIV_EXPR:
582 case ROUND_DIV_EXPR:
583 case EXACT_DIV_EXPR:
584 if (TYPE_UNSIGNED (type))
585 break;
586 /* In general we can't negate A in A / B, because if A is INT_MIN and
587 B is not 1 we change the sign of the result. */
588 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
589 && negate_expr_p (TREE_OPERAND (t, 0)))
590 return true;
591 /* In general we can't negate B in A / B, because if A is INT_MIN and
592 B is 1, we may turn this into INT_MIN / -1 which is undefined
593 and actually traps on some architectures. */
594 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
595 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
596 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
597 && ! integer_onep (TREE_OPERAND (t, 1))))
598 return negate_expr_p (TREE_OPERAND (t, 1));
599 break;
600
601 case NOP_EXPR:
602 /* Negate -((double)float) as (double)(-float). */
603 if (SCALAR_FLOAT_TYPE_P (type))
604 {
605 tree tem = strip_float_extensions (t);
606 if (tem != t)
607 return negate_expr_p (tem);
608 }
609 break;
610
611 case CALL_EXPR:
612 /* Negate -f(x) as f(-x). */
613 if (negate_mathfn_p (get_call_combined_fn (t)))
614 return negate_expr_p (CALL_EXPR_ARG (t, 0));
615 break;
616
617 case RSHIFT_EXPR:
618 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
619 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
620 {
621 tree op1 = TREE_OPERAND (t, 1);
622 if (wi::to_wide (op1) == element_precision (type) - 1)
623 return true;
624 }
625 break;
626
627 default:
628 break;
629 }
630 return false;
631 }
632
633 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
634 simplification is possible.
635 If negate_expr_p would return true for T, NULL_TREE will never be
636 returned. */
637
638 static tree
639 fold_negate_expr_1 (location_t loc, tree t)
640 {
641 tree type = TREE_TYPE (t);
642 tree tem;
643
644 switch (TREE_CODE (t))
645 {
646 /* Convert - (~A) to A + 1. */
647 case BIT_NOT_EXPR:
648 if (INTEGRAL_TYPE_P (type))
649 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
650 build_one_cst (type));
651 break;
652
653 case INTEGER_CST:
654 tem = fold_negate_const (t, type);
655 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
656 || (ANY_INTEGRAL_TYPE_P (type)
657 && !TYPE_OVERFLOW_TRAPS (type)
658 && TYPE_OVERFLOW_WRAPS (type))
659 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
660 return tem;
661 break;
662
663 case POLY_INT_CST:
664 case REAL_CST:
665 case FIXED_CST:
666 tem = fold_negate_const (t, type);
667 return tem;
668
669 case COMPLEX_CST:
670 {
671 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
672 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
673 if (rpart && ipart)
674 return build_complex (type, rpart, ipart);
675 }
676 break;
677
678 case VECTOR_CST:
679 {
680 tree_vector_builder elts;
681 elts.new_unary_operation (type, t, true);
682 unsigned int count = elts.encoded_nelts ();
683 for (unsigned int i = 0; i < count; ++i)
684 {
685 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
686 if (elt == NULL_TREE)
687 return NULL_TREE;
688 elts.quick_push (elt);
689 }
690
691 return elts.build ();
692 }
693
694 case COMPLEX_EXPR:
695 if (negate_expr_p (t))
696 return fold_build2_loc (loc, COMPLEX_EXPR, type,
697 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
698 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
699 break;
700
701 case CONJ_EXPR:
702 if (negate_expr_p (t))
703 return fold_build1_loc (loc, CONJ_EXPR, type,
704 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
705 break;
706
707 case NEGATE_EXPR:
708 if (!TYPE_OVERFLOW_SANITIZED (type))
709 return TREE_OPERAND (t, 0);
710 break;
711
712 case PLUS_EXPR:
713 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
714 && !HONOR_SIGNED_ZEROS (type))
715 {
716 /* -(A + B) -> (-B) - A. */
717 if (negate_expr_p (TREE_OPERAND (t, 1)))
718 {
719 tem = negate_expr (TREE_OPERAND (t, 1));
720 return fold_build2_loc (loc, MINUS_EXPR, type,
721 tem, TREE_OPERAND (t, 0));
722 }
723
724 /* -(A + B) -> (-A) - B. */
725 if (negate_expr_p (TREE_OPERAND (t, 0)))
726 {
727 tem = negate_expr (TREE_OPERAND (t, 0));
728 return fold_build2_loc (loc, MINUS_EXPR, type,
729 tem, TREE_OPERAND (t, 1));
730 }
731 }
732 break;
733
734 case MINUS_EXPR:
735 /* - (A - B) -> B - A */
736 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
737 && !HONOR_SIGNED_ZEROS (type))
738 return fold_build2_loc (loc, MINUS_EXPR, type,
739 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
740 break;
741
742 case MULT_EXPR:
743 if (TYPE_UNSIGNED (type))
744 break;
745
746 /* Fall through. */
747
748 case RDIV_EXPR:
749 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
750 {
751 tem = TREE_OPERAND (t, 1);
752 if (negate_expr_p (tem))
753 return fold_build2_loc (loc, TREE_CODE (t), type,
754 TREE_OPERAND (t, 0), negate_expr (tem));
755 tem = TREE_OPERAND (t, 0);
756 if (negate_expr_p (tem))
757 return fold_build2_loc (loc, TREE_CODE (t), type,
758 negate_expr (tem), TREE_OPERAND (t, 1));
759 }
760 break;
761
762 case TRUNC_DIV_EXPR:
763 case ROUND_DIV_EXPR:
764 case EXACT_DIV_EXPR:
765 if (TYPE_UNSIGNED (type))
766 break;
767 /* In general we can't negate A in A / B, because if A is INT_MIN and
768 B is not 1 we change the sign of the result. */
769 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
770 && negate_expr_p (TREE_OPERAND (t, 0)))
771 return fold_build2_loc (loc, TREE_CODE (t), type,
772 negate_expr (TREE_OPERAND (t, 0)),
773 TREE_OPERAND (t, 1));
774 /* In general we can't negate B in A / B, because if A is INT_MIN and
775 B is 1, we may turn this into INT_MIN / -1 which is undefined
776 and actually traps on some architectures. */
777 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
778 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
779 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
780 && ! integer_onep (TREE_OPERAND (t, 1))))
781 && negate_expr_p (TREE_OPERAND (t, 1)))
782 return fold_build2_loc (loc, TREE_CODE (t), type,
783 TREE_OPERAND (t, 0),
784 negate_expr (TREE_OPERAND (t, 1)));
785 break;
786
787 case NOP_EXPR:
788 /* Convert -((double)float) into (double)(-float). */
789 if (SCALAR_FLOAT_TYPE_P (type))
790 {
791 tem = strip_float_extensions (t);
792 if (tem != t && negate_expr_p (tem))
793 return fold_convert_loc (loc, type, negate_expr (tem));
794 }
795 break;
796
797 case CALL_EXPR:
798 /* Negate -f(x) as f(-x). */
799 if (negate_mathfn_p (get_call_combined_fn (t))
800 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
801 {
802 tree fndecl, arg;
803
804 fndecl = get_callee_fndecl (t);
805 arg = negate_expr (CALL_EXPR_ARG (t, 0));
806 return build_call_expr_loc (loc, fndecl, 1, arg);
807 }
808 break;
809
810 case RSHIFT_EXPR:
811 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
812 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
813 {
814 tree op1 = TREE_OPERAND (t, 1);
815 if (wi::to_wide (op1) == element_precision (type) - 1)
816 {
817 tree ntype = TYPE_UNSIGNED (type)
818 ? signed_type_for (type)
819 : unsigned_type_for (type);
820 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
821 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
822 return fold_convert_loc (loc, type, temp);
823 }
824 }
825 break;
826
827 default:
828 break;
829 }
830
831 return NULL_TREE;
832 }
833
834 /* A wrapper for fold_negate_expr_1. */
835
836 static tree
837 fold_negate_expr (location_t loc, tree t)
838 {
839 tree type = TREE_TYPE (t);
840 STRIP_SIGN_NOPS (t);
841 tree tem = fold_negate_expr_1 (loc, t);
842 if (tem == NULL_TREE)
843 return NULL_TREE;
844 return fold_convert_loc (loc, type, tem);
845 }
846
847 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
848 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
849 return NULL_TREE. */
850
851 static tree
852 negate_expr (tree t)
853 {
854 tree type, tem;
855 location_t loc;
856
857 if (t == NULL_TREE)
858 return NULL_TREE;
859
860 loc = EXPR_LOCATION (t);
861 type = TREE_TYPE (t);
862 STRIP_SIGN_NOPS (t);
863
864 tem = fold_negate_expr (loc, t);
865 if (!tem)
866 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
867 return fold_convert_loc (loc, type, tem);
868 }
869
870 /* Split a tree IN into a constant, literal and variable parts that could be
871 combined with CODE to make IN. "constant" means an expression with
872 TREE_CONSTANT but that isn't an actual constant. CODE must be a
873 commutative arithmetic operation. Store the constant part into *CONP,
874 the literal in *LITP and return the variable part. If a part isn't
875 present, set it to null. If the tree does not decompose in this way,
876 return the entire tree as the variable part and the other parts as null.
877
878 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
879 case, we negate an operand that was subtracted. Except if it is a
880 literal for which we use *MINUS_LITP instead.
881
882 If NEGATE_P is true, we are negating all of IN, again except a literal
883 for which we use *MINUS_LITP instead. If a variable part is of pointer
884 type, it is negated after converting to TYPE. This prevents us from
885 generating illegal MINUS pointer expression. LOC is the location of
886 the converted variable part.
887
888 If IN is itself a literal or constant, return it as appropriate.
889
890 Note that we do not guarantee that any of the three values will be the
891 same type as IN, but they will have the same signedness and mode. */
892
893 static tree
894 split_tree (tree in, tree type, enum tree_code code,
895 tree *minus_varp, tree *conp, tree *minus_conp,
896 tree *litp, tree *minus_litp, int negate_p)
897 {
898 tree var = 0;
899 *minus_varp = 0;
900 *conp = 0;
901 *minus_conp = 0;
902 *litp = 0;
903 *minus_litp = 0;
904
905 /* Strip any conversions that don't change the machine mode or signedness. */
906 STRIP_SIGN_NOPS (in);
907
908 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
909 || TREE_CODE (in) == FIXED_CST)
910 *litp = in;
911 else if (TREE_CODE (in) == code
912 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
913 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
914 /* We can associate addition and subtraction together (even
915 though the C standard doesn't say so) for integers because
916 the value is not affected. For reals, the value might be
917 affected, so we can't. */
918 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
919 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
920 || (code == MINUS_EXPR
921 && (TREE_CODE (in) == PLUS_EXPR
922 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
923 {
924 tree op0 = TREE_OPERAND (in, 0);
925 tree op1 = TREE_OPERAND (in, 1);
926 bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
927 bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
928
929 /* First see if either of the operands is a literal, then a constant. */
930 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
931 || TREE_CODE (op0) == FIXED_CST)
932 *litp = op0, op0 = 0;
933 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
934 || TREE_CODE (op1) == FIXED_CST)
935 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
936
937 if (op0 != 0 && TREE_CONSTANT (op0))
938 *conp = op0, op0 = 0;
939 else if (op1 != 0 && TREE_CONSTANT (op1))
940 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
941
942 /* If we haven't dealt with either operand, this is not a case we can
943 decompose. Otherwise, VAR is either of the ones remaining, if any. */
944 if (op0 != 0 && op1 != 0)
945 var = in;
946 else if (op0 != 0)
947 var = op0;
948 else
949 var = op1, neg_var_p = neg1_p;
950
951 /* Now do any needed negations. */
952 if (neg_litp_p)
953 *minus_litp = *litp, *litp = 0;
954 if (neg_conp_p && *conp)
955 *minus_conp = *conp, *conp = 0;
956 if (neg_var_p && var)
957 *minus_varp = var, var = 0;
958 }
959 else if (TREE_CONSTANT (in))
960 *conp = in;
961 else if (TREE_CODE (in) == BIT_NOT_EXPR
962 && code == PLUS_EXPR)
963 {
964 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
965 when IN is constant. */
966 *litp = build_minus_one_cst (type);
967 *minus_varp = TREE_OPERAND (in, 0);
968 }
969 else
970 var = in;
971
972 if (negate_p)
973 {
974 if (*litp)
975 *minus_litp = *litp, *litp = 0;
976 else if (*minus_litp)
977 *litp = *minus_litp, *minus_litp = 0;
978 if (*conp)
979 *minus_conp = *conp, *conp = 0;
980 else if (*minus_conp)
981 *conp = *minus_conp, *minus_conp = 0;
982 if (var)
983 *minus_varp = var, var = 0;
984 else if (*minus_varp)
985 var = *minus_varp, *minus_varp = 0;
986 }
987
988 if (*litp
989 && TREE_OVERFLOW_P (*litp))
990 *litp = drop_tree_overflow (*litp);
991 if (*minus_litp
992 && TREE_OVERFLOW_P (*minus_litp))
993 *minus_litp = drop_tree_overflow (*minus_litp);
994
995 return var;
996 }
997
998 /* Re-associate trees split by the above function. T1 and T2 are
999 either expressions to associate or null. Return the new
1000 expression, if any. LOC is the location of the new expression. If
1001 we build an operation, do it in TYPE and with CODE. */
1002
1003 static tree
1004 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1005 {
1006 if (t1 == 0)
1007 {
1008 gcc_assert (t2 == 0 || code != MINUS_EXPR);
1009 return t2;
1010 }
1011 else if (t2 == 0)
1012 return t1;
1013
1014 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1015 try to fold this since we will have infinite recursion. But do
1016 deal with any NEGATE_EXPRs. */
1017 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1018 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1019 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1020 {
1021 if (code == PLUS_EXPR)
1022 {
1023 if (TREE_CODE (t1) == NEGATE_EXPR)
1024 return build2_loc (loc, MINUS_EXPR, type,
1025 fold_convert_loc (loc, type, t2),
1026 fold_convert_loc (loc, type,
1027 TREE_OPERAND (t1, 0)));
1028 else if (TREE_CODE (t2) == NEGATE_EXPR)
1029 return build2_loc (loc, MINUS_EXPR, type,
1030 fold_convert_loc (loc, type, t1),
1031 fold_convert_loc (loc, type,
1032 TREE_OPERAND (t2, 0)));
1033 else if (integer_zerop (t2))
1034 return fold_convert_loc (loc, type, t1);
1035 }
1036 else if (code == MINUS_EXPR)
1037 {
1038 if (integer_zerop (t2))
1039 return fold_convert_loc (loc, type, t1);
1040 }
1041
1042 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1043 fold_convert_loc (loc, type, t2));
1044 }
1045
1046 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1047 fold_convert_loc (loc, type, t2));
1048 }
1049
1050 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1051 for use in int_const_binop, size_binop and size_diffop. */
1052
1053 static bool
1054 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1055 {
1056 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1057 return false;
1058 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1059 return false;
1060
1061 switch (code)
1062 {
1063 case LSHIFT_EXPR:
1064 case RSHIFT_EXPR:
1065 case LROTATE_EXPR:
1066 case RROTATE_EXPR:
1067 return true;
1068
1069 default:
1070 break;
1071 }
1072
1073 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1074 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1075 && TYPE_MODE (type1) == TYPE_MODE (type2);
1076 }
1077
1078 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1079 a new constant in RES. Return FALSE if we don't know how to
1080 evaluate CODE at compile-time. */
1081
1082 bool
1083 wide_int_binop (wide_int &res,
1084 enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1085 signop sign, wi::overflow_type *overflow)
1086 {
1087 wide_int tmp;
1088 *overflow = wi::OVF_NONE;
1089 switch (code)
1090 {
1091 case BIT_IOR_EXPR:
1092 res = wi::bit_or (arg1, arg2);
1093 break;
1094
1095 case BIT_XOR_EXPR:
1096 res = wi::bit_xor (arg1, arg2);
1097 break;
1098
1099 case BIT_AND_EXPR:
1100 res = wi::bit_and (arg1, arg2);
1101 break;
1102
1103 case LSHIFT_EXPR:
1104 if (wi::neg_p (arg2))
1105 return false;
1106 res = wi::lshift (arg1, arg2);
1107 break;
1108
1109 case RSHIFT_EXPR:
1110 if (wi::neg_p (arg2))
1111 return false;
1112 /* It's unclear from the C standard whether shifts can overflow.
1113 The following code ignores overflow; perhaps a C standard
1114 interpretation ruling is needed. */
1115 res = wi::rshift (arg1, arg2, sign);
1116 break;
1117
1118 case RROTATE_EXPR:
1119 case LROTATE_EXPR:
1120 if (wi::neg_p (arg2))
1121 {
1122 tmp = -arg2;
1123 if (code == RROTATE_EXPR)
1124 code = LROTATE_EXPR;
1125 else
1126 code = RROTATE_EXPR;
1127 }
1128 else
1129 tmp = arg2;
1130
1131 if (code == RROTATE_EXPR)
1132 res = wi::rrotate (arg1, tmp);
1133 else
1134 res = wi::lrotate (arg1, tmp);
1135 break;
1136
1137 case PLUS_EXPR:
1138 res = wi::add (arg1, arg2, sign, overflow);
1139 break;
1140
1141 case MINUS_EXPR:
1142 res = wi::sub (arg1, arg2, sign, overflow);
1143 break;
1144
1145 case MULT_EXPR:
1146 res = wi::mul (arg1, arg2, sign, overflow);
1147 break;
1148
1149 case MULT_HIGHPART_EXPR:
1150 res = wi::mul_high (arg1, arg2, sign);
1151 break;
1152
1153 case TRUNC_DIV_EXPR:
1154 case EXACT_DIV_EXPR:
1155 if (arg2 == 0)
1156 return false;
1157 res = wi::div_trunc (arg1, arg2, sign, overflow);
1158 break;
1159
1160 case FLOOR_DIV_EXPR:
1161 if (arg2 == 0)
1162 return false;
1163 res = wi::div_floor (arg1, arg2, sign, overflow);
1164 break;
1165
1166 case CEIL_DIV_EXPR:
1167 if (arg2 == 0)
1168 return false;
1169 res = wi::div_ceil (arg1, arg2, sign, overflow);
1170 break;
1171
1172 case ROUND_DIV_EXPR:
1173 if (arg2 == 0)
1174 return false;
1175 res = wi::div_round (arg1, arg2, sign, overflow);
1176 break;
1177
1178 case TRUNC_MOD_EXPR:
1179 if (arg2 == 0)
1180 return false;
1181 res = wi::mod_trunc (arg1, arg2, sign, overflow);
1182 break;
1183
1184 case FLOOR_MOD_EXPR:
1185 if (arg2 == 0)
1186 return false;
1187 res = wi::mod_floor (arg1, arg2, sign, overflow);
1188 break;
1189
1190 case CEIL_MOD_EXPR:
1191 if (arg2 == 0)
1192 return false;
1193 res = wi::mod_ceil (arg1, arg2, sign, overflow);
1194 break;
1195
1196 case ROUND_MOD_EXPR:
1197 if (arg2 == 0)
1198 return false;
1199 res = wi::mod_round (arg1, arg2, sign, overflow);
1200 break;
1201
1202 case MIN_EXPR:
1203 res = wi::min (arg1, arg2, sign);
1204 break;
1205
1206 case MAX_EXPR:
1207 res = wi::max (arg1, arg2, sign);
1208 break;
1209
1210 default:
1211 return false;
1212 }
1213 return true;
1214 }
1215
1216 /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1217 min value to RES. */
1218 bool
1219 can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1220 {
1221 if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1222 {
1223 res = wi::to_poly_wide (arg1);
1224 return true;
1225 }
1226 else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1227 {
1228 res = wi::to_poly_wide (arg2);
1229 return true;
1230 }
1231
1232 return false;
1233 }
1234
1235 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1236 produce a new constant in RES. Return FALSE if we don't know how
1237 to evaluate CODE at compile-time. */
1238
1239 bool
1240 poly_int_binop (poly_wide_int &res, enum tree_code code,
1241 const_tree arg1, const_tree arg2,
1242 signop sign, wi::overflow_type *overflow)
1243 {
1244 gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1245
1246 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1247 {
1248 wide_int warg1 = wi::to_wide (arg1), wi_res;
1249 wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1250 if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1251 return NULL_TREE;
1252 res = wi_res;
1253 return true;
1254 }
1255
1256 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1257
1258 switch (code)
1259 {
1260 case PLUS_EXPR:
1261 res = wi::add (wi::to_poly_wide (arg1),
1262 wi::to_poly_wide (arg2), sign, overflow);
1263 break;
1264
1265 case MINUS_EXPR:
1266 res = wi::sub (wi::to_poly_wide (arg1),
1267 wi::to_poly_wide (arg2), sign, overflow);
1268 break;
1269
1270 case MULT_EXPR:
1271 if (TREE_CODE (arg2) == INTEGER_CST)
1272 res = wi::mul (wi::to_poly_wide (arg1),
1273 wi::to_wide (arg2), sign, overflow);
1274 else if (TREE_CODE (arg1) == INTEGER_CST)
1275 res = wi::mul (wi::to_poly_wide (arg2),
1276 wi::to_wide (arg1), sign, overflow);
1277 else
1278 return NULL_TREE;
1279 break;
1280
1281 case LSHIFT_EXPR:
1282 if (TREE_CODE (arg2) == INTEGER_CST)
1283 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1284 else
1285 return false;
1286 break;
1287
1288 case BIT_AND_EXPR:
1289 if (TREE_CODE (arg2) != INTEGER_CST
1290 || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1291 &res))
1292 return false;
1293 break;
1294
1295 case BIT_IOR_EXPR:
1296 if (TREE_CODE (arg2) != INTEGER_CST
1297 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1298 &res))
1299 return false;
1300 break;
1301
1302 case MIN_EXPR:
1303 if (!can_min_p (arg1, arg2, res))
1304 return false;
1305 break;
1306
1307 default:
1308 return false;
1309 }
1310 return true;
1311 }
1312
1313 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1314 produce a new constant. Return NULL_TREE if we don't know how to
1315 evaluate CODE at compile-time. */
1316
1317 tree
1318 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1319 int overflowable)
1320 {
1321 poly_wide_int poly_res;
1322 tree type = TREE_TYPE (arg1);
1323 signop sign = TYPE_SIGN (type);
1324 wi::overflow_type overflow = wi::OVF_NONE;
1325
1326 if (!poly_int_tree_p (arg1)
1327 || !poly_int_tree_p (arg2)
1328 || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1329 return NULL_TREE;
1330 return force_fit_type (type, poly_res, overflowable,
1331 (((sign == SIGNED || overflowable == -1)
1332 && overflow)
1333 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1334 }
1335
1336 /* Return true if binary operation OP distributes over addition in operand
1337 OPNO, with the other operand being held constant. OPNO counts from 1. */
1338
1339 static bool
1340 distributes_over_addition_p (tree_code op, int opno)
1341 {
1342 switch (op)
1343 {
1344 case PLUS_EXPR:
1345 case MINUS_EXPR:
1346 case MULT_EXPR:
1347 return true;
1348
1349 case LSHIFT_EXPR:
1350 return opno == 1;
1351
1352 default:
1353 return false;
1354 }
1355 }
1356
1357 /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1358 is the other operand. Try to use the value of OP to simplify the
1359 operation in one step, without having to process individual elements. */
1360 static tree
1361 simplify_const_binop (tree_code code, tree op, tree other_op,
1362 int index ATTRIBUTE_UNUSED)
1363 {
1364 /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1365 if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1366 {
1367 if (integer_zerop (other_op))
1368 {
1369 if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1370 return op;
1371 else if (code == BIT_AND_EXPR)
1372 return other_op;
1373 }
1374 }
1375
1376 return NULL_TREE;
1377 }
1378
1379 /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1380 be an elementwise vector operation, try to fold the operation to a
1381 constant vector, using ELT_CONST_BINOP to fold each element. Return
1382 the folded value on success, otherwise return null. */
1383 tree
1384 vector_const_binop (tree_code code, tree arg1, tree arg2,
1385 tree (*elt_const_binop) (enum tree_code, tree, tree))
1386 {
1387 if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1388 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1389 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1390 {
1391 tree type = TREE_TYPE (arg1);
1392 bool step_ok_p;
1393 if (VECTOR_CST_STEPPED_P (arg1)
1394 && VECTOR_CST_STEPPED_P (arg2))
1395 /* We can operate directly on the encoding if:
1396
1397 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1398 implies
1399 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1400
1401 Addition and subtraction are the supported operators
1402 for which this is true. */
1403 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1404 else if (VECTOR_CST_STEPPED_P (arg1))
1405 /* We can operate directly on stepped encodings if:
1406
1407 a3 - a2 == a2 - a1
1408 implies:
1409 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1410
1411 which is true if (x -> x op c) distributes over addition. */
1412 step_ok_p = distributes_over_addition_p (code, 1);
1413 else
1414 /* Similarly in reverse. */
1415 step_ok_p = distributes_over_addition_p (code, 2);
1416 tree_vector_builder elts;
1417 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1418 return NULL_TREE;
1419 unsigned int count = elts.encoded_nelts ();
1420 for (unsigned int i = 0; i < count; ++i)
1421 {
1422 tree elem1 = VECTOR_CST_ELT (arg1, i);
1423 tree elem2 = VECTOR_CST_ELT (arg2, i);
1424
1425 tree elt = elt_const_binop (code, elem1, elem2);
1426
1427 /* It is possible that const_binop cannot handle the given
1428 code and return NULL_TREE */
1429 if (elt == NULL_TREE)
1430 return NULL_TREE;
1431 elts.quick_push (elt);
1432 }
1433
1434 return elts.build ();
1435 }
1436
1437 if (TREE_CODE (arg1) == VECTOR_CST
1438 && TREE_CODE (arg2) == INTEGER_CST)
1439 {
1440 tree type = TREE_TYPE (arg1);
1441 bool step_ok_p = distributes_over_addition_p (code, 1);
1442 tree_vector_builder elts;
1443 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1444 return NULL_TREE;
1445 unsigned int count = elts.encoded_nelts ();
1446 for (unsigned int i = 0; i < count; ++i)
1447 {
1448 tree elem1 = VECTOR_CST_ELT (arg1, i);
1449
1450 tree elt = elt_const_binop (code, elem1, arg2);
1451
1452 /* It is possible that const_binop cannot handle the given
1453 code and return NULL_TREE. */
1454 if (elt == NULL_TREE)
1455 return NULL_TREE;
1456 elts.quick_push (elt);
1457 }
1458
1459 return elts.build ();
1460 }
1461 return NULL_TREE;
1462 }
1463
1464 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1465 constant. We assume ARG1 and ARG2 have the same data type, or at least
1466 are the same kind of constant and the same machine mode. Return zero if
1467 combining the constants is not allowed in the current operating mode. */
1468
1469 static tree
1470 const_binop (enum tree_code code, tree arg1, tree arg2)
1471 {
1472 /* Sanity check for the recursive cases. */
1473 if (!arg1 || !arg2)
1474 return NULL_TREE;
1475
1476 STRIP_NOPS (arg1);
1477 STRIP_NOPS (arg2);
1478
1479 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1480 {
1481 if (code == POINTER_PLUS_EXPR)
1482 return int_const_binop (PLUS_EXPR,
1483 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1484
1485 return int_const_binop (code, arg1, arg2);
1486 }
1487
1488 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1489 {
1490 machine_mode mode;
1491 REAL_VALUE_TYPE d1;
1492 REAL_VALUE_TYPE d2;
1493 REAL_VALUE_TYPE value;
1494 REAL_VALUE_TYPE result;
1495 bool inexact;
1496 tree t, type;
1497
1498 /* The following codes are handled by real_arithmetic. */
1499 switch (code)
1500 {
1501 case PLUS_EXPR:
1502 case MINUS_EXPR:
1503 case MULT_EXPR:
1504 case RDIV_EXPR:
1505 case MIN_EXPR:
1506 case MAX_EXPR:
1507 break;
1508
1509 default:
1510 return NULL_TREE;
1511 }
1512
1513 d1 = TREE_REAL_CST (arg1);
1514 d2 = TREE_REAL_CST (arg2);
1515
1516 type = TREE_TYPE (arg1);
1517 mode = TYPE_MODE (type);
1518
1519 /* Don't perform operation if we honor signaling NaNs and
1520 either operand is a signaling NaN. */
1521 if (HONOR_SNANS (mode)
1522 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1523 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1524 return NULL_TREE;
1525
1526 /* Don't perform operation if it would raise a division
1527 by zero exception. */
1528 if (code == RDIV_EXPR
1529 && real_equal (&d2, &dconst0)
1530 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1531 return NULL_TREE;
1532
1533 /* If either operand is a NaN, just return it. Otherwise, set up
1534 for floating-point trap; we return an overflow. */
1535 if (REAL_VALUE_ISNAN (d1))
1536 {
1537 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1538 is off. */
1539 d1.signalling = 0;
1540 t = build_real (type, d1);
1541 return t;
1542 }
1543 else if (REAL_VALUE_ISNAN (d2))
1544 {
1545 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1546 is off. */
1547 d2.signalling = 0;
1548 t = build_real (type, d2);
1549 return t;
1550 }
1551
1552 inexact = real_arithmetic (&value, code, &d1, &d2);
1553 real_convert (&result, mode, &value);
1554
1555 /* Don't constant fold this floating point operation if
1556 both operands are not NaN but the result is NaN, and
1557 flag_trapping_math. Such operations should raise an
1558 invalid operation exception. */
1559 if (flag_trapping_math
1560 && MODE_HAS_NANS (mode)
1561 && REAL_VALUE_ISNAN (result)
1562 && !REAL_VALUE_ISNAN (d1)
1563 && !REAL_VALUE_ISNAN (d2))
1564 return NULL_TREE;
1565
1566 /* Don't constant fold this floating point operation if
1567 the result has overflowed and flag_trapping_math. */
1568 if (flag_trapping_math
1569 && MODE_HAS_INFINITIES (mode)
1570 && REAL_VALUE_ISINF (result)
1571 && !REAL_VALUE_ISINF (d1)
1572 && !REAL_VALUE_ISINF (d2))
1573 return NULL_TREE;
1574
1575 /* Don't constant fold this floating point operation if the
1576 result may dependent upon the run-time rounding mode and
1577 flag_rounding_math is set, or if GCC's software emulation
1578 is unable to accurately represent the result. */
1579 if ((flag_rounding_math
1580 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1581 && (inexact || !real_identical (&result, &value)))
1582 return NULL_TREE;
1583
1584 t = build_real (type, result);
1585
1586 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1587 return t;
1588 }
1589
1590 if (TREE_CODE (arg1) == FIXED_CST)
1591 {
1592 FIXED_VALUE_TYPE f1;
1593 FIXED_VALUE_TYPE f2;
1594 FIXED_VALUE_TYPE result;
1595 tree t, type;
1596 bool sat_p;
1597 bool overflow_p;
1598
1599 /* The following codes are handled by fixed_arithmetic. */
1600 switch (code)
1601 {
1602 case PLUS_EXPR:
1603 case MINUS_EXPR:
1604 case MULT_EXPR:
1605 case TRUNC_DIV_EXPR:
1606 if (TREE_CODE (arg2) != FIXED_CST)
1607 return NULL_TREE;
1608 f2 = TREE_FIXED_CST (arg2);
1609 break;
1610
1611 case LSHIFT_EXPR:
1612 case RSHIFT_EXPR:
1613 {
1614 if (TREE_CODE (arg2) != INTEGER_CST)
1615 return NULL_TREE;
1616 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1617 f2.data.high = w2.elt (1);
1618 f2.data.low = w2.ulow ();
1619 f2.mode = SImode;
1620 }
1621 break;
1622
1623 default:
1624 return NULL_TREE;
1625 }
1626
1627 f1 = TREE_FIXED_CST (arg1);
1628 type = TREE_TYPE (arg1);
1629 sat_p = TYPE_SATURATING (type);
1630 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1631 t = build_fixed (type, result);
1632 /* Propagate overflow flags. */
1633 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1634 TREE_OVERFLOW (t) = 1;
1635 return t;
1636 }
1637
1638 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1639 {
1640 tree type = TREE_TYPE (arg1);
1641 tree r1 = TREE_REALPART (arg1);
1642 tree i1 = TREE_IMAGPART (arg1);
1643 tree r2 = TREE_REALPART (arg2);
1644 tree i2 = TREE_IMAGPART (arg2);
1645 tree real, imag;
1646
1647 switch (code)
1648 {
1649 case PLUS_EXPR:
1650 case MINUS_EXPR:
1651 real = const_binop (code, r1, r2);
1652 imag = const_binop (code, i1, i2);
1653 break;
1654
1655 case MULT_EXPR:
1656 if (COMPLEX_FLOAT_TYPE_P (type))
1657 return do_mpc_arg2 (arg1, arg2, type,
1658 /* do_nonfinite= */ folding_initializer,
1659 mpc_mul);
1660
1661 real = const_binop (MINUS_EXPR,
1662 const_binop (MULT_EXPR, r1, r2),
1663 const_binop (MULT_EXPR, i1, i2));
1664 imag = const_binop (PLUS_EXPR,
1665 const_binop (MULT_EXPR, r1, i2),
1666 const_binop (MULT_EXPR, i1, r2));
1667 break;
1668
1669 case RDIV_EXPR:
1670 if (COMPLEX_FLOAT_TYPE_P (type))
1671 return do_mpc_arg2 (arg1, arg2, type,
1672 /* do_nonfinite= */ folding_initializer,
1673 mpc_div);
1674 /* Fallthru. */
1675 case TRUNC_DIV_EXPR:
1676 case CEIL_DIV_EXPR:
1677 case FLOOR_DIV_EXPR:
1678 case ROUND_DIV_EXPR:
1679 if (flag_complex_method == 0)
1680 {
1681 /* Keep this algorithm in sync with
1682 tree-complex.cc:expand_complex_div_straight().
1683
1684 Expand complex division to scalars, straightforward algorithm.
1685 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1686 t = br*br + bi*bi
1687 */
1688 tree magsquared
1689 = const_binop (PLUS_EXPR,
1690 const_binop (MULT_EXPR, r2, r2),
1691 const_binop (MULT_EXPR, i2, i2));
1692 tree t1
1693 = const_binop (PLUS_EXPR,
1694 const_binop (MULT_EXPR, r1, r2),
1695 const_binop (MULT_EXPR, i1, i2));
1696 tree t2
1697 = const_binop (MINUS_EXPR,
1698 const_binop (MULT_EXPR, i1, r2),
1699 const_binop (MULT_EXPR, r1, i2));
1700
1701 real = const_binop (code, t1, magsquared);
1702 imag = const_binop (code, t2, magsquared);
1703 }
1704 else
1705 {
1706 /* Keep this algorithm in sync with
1707 tree-complex.cc:expand_complex_div_wide().
1708
1709 Expand complex division to scalars, modified algorithm to minimize
1710 overflow with wide input ranges. */
1711 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1712 fold_abs_const (r2, TREE_TYPE (type)),
1713 fold_abs_const (i2, TREE_TYPE (type)));
1714
1715 if (integer_nonzerop (compare))
1716 {
1717 /* In the TRUE branch, we compute
1718 ratio = br/bi;
1719 div = (br * ratio) + bi;
1720 tr = (ar * ratio) + ai;
1721 ti = (ai * ratio) - ar;
1722 tr = tr / div;
1723 ti = ti / div; */
1724 tree ratio = const_binop (code, r2, i2);
1725 tree div = const_binop (PLUS_EXPR, i2,
1726 const_binop (MULT_EXPR, r2, ratio));
1727 real = const_binop (MULT_EXPR, r1, ratio);
1728 real = const_binop (PLUS_EXPR, real, i1);
1729 real = const_binop (code, real, div);
1730
1731 imag = const_binop (MULT_EXPR, i1, ratio);
1732 imag = const_binop (MINUS_EXPR, imag, r1);
1733 imag = const_binop (code, imag, div);
1734 }
1735 else
1736 {
1737 /* In the FALSE branch, we compute
1738 ratio = d/c;
1739 divisor = (d * ratio) + c;
1740 tr = (b * ratio) + a;
1741 ti = b - (a * ratio);
1742 tr = tr / div;
1743 ti = ti / div; */
1744 tree ratio = const_binop (code, i2, r2);
1745 tree div = const_binop (PLUS_EXPR, r2,
1746 const_binop (MULT_EXPR, i2, ratio));
1747
1748 real = const_binop (MULT_EXPR, i1, ratio);
1749 real = const_binop (PLUS_EXPR, real, r1);
1750 real = const_binop (code, real, div);
1751
1752 imag = const_binop (MULT_EXPR, r1, ratio);
1753 imag = const_binop (MINUS_EXPR, i1, imag);
1754 imag = const_binop (code, imag, div);
1755 }
1756 }
1757 break;
1758
1759 default:
1760 return NULL_TREE;
1761 }
1762
1763 if (real && imag)
1764 return build_complex (type, real, imag);
1765 }
1766
1767 tree simplified;
1768 if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1769 return simplified;
1770
1771 if (commutative_tree_code (code)
1772 && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1773 return simplified;
1774
1775 return vector_const_binop (code, arg1, arg2, const_binop);
1776 }
1777
1778 /* Overload that adds a TYPE parameter to be able to dispatch
1779 to fold_relational_const. */
1780
1781 tree
1782 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1783 {
1784 if (TREE_CODE_CLASS (code) == tcc_comparison)
1785 return fold_relational_const (code, type, arg1, arg2);
1786
1787 /* ??? Until we make the const_binop worker take the type of the
1788 result as argument put those cases that need it here. */
1789 switch (code)
1790 {
1791 case VEC_SERIES_EXPR:
1792 if (CONSTANT_CLASS_P (arg1)
1793 && CONSTANT_CLASS_P (arg2))
1794 return build_vec_series (type, arg1, arg2);
1795 return NULL_TREE;
1796
1797 case COMPLEX_EXPR:
1798 if ((TREE_CODE (arg1) == REAL_CST
1799 && TREE_CODE (arg2) == REAL_CST)
1800 || (TREE_CODE (arg1) == INTEGER_CST
1801 && TREE_CODE (arg2) == INTEGER_CST))
1802 return build_complex (type, arg1, arg2);
1803 return NULL_TREE;
1804
1805 case POINTER_DIFF_EXPR:
1806 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1807 {
1808 poly_offset_int res = (wi::to_poly_offset (arg1)
1809 - wi::to_poly_offset (arg2));
1810 return force_fit_type (type, res, 1,
1811 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1812 }
1813 return NULL_TREE;
1814
1815 case VEC_PACK_TRUNC_EXPR:
1816 case VEC_PACK_FIX_TRUNC_EXPR:
1817 case VEC_PACK_FLOAT_EXPR:
1818 {
1819 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1820
1821 if (TREE_CODE (arg1) != VECTOR_CST
1822 || TREE_CODE (arg2) != VECTOR_CST)
1823 return NULL_TREE;
1824
1825 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1826 return NULL_TREE;
1827
1828 out_nelts = in_nelts * 2;
1829 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1830 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1831
1832 tree_vector_builder elts (type, out_nelts, 1);
1833 for (i = 0; i < out_nelts; i++)
1834 {
1835 tree elt = (i < in_nelts
1836 ? VECTOR_CST_ELT (arg1, i)
1837 : VECTOR_CST_ELT (arg2, i - in_nelts));
1838 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1839 ? NOP_EXPR
1840 : code == VEC_PACK_FLOAT_EXPR
1841 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1842 TREE_TYPE (type), elt);
1843 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1844 return NULL_TREE;
1845 elts.quick_push (elt);
1846 }
1847
1848 return elts.build ();
1849 }
1850
1851 case VEC_WIDEN_MULT_LO_EXPR:
1852 case VEC_WIDEN_MULT_HI_EXPR:
1853 case VEC_WIDEN_MULT_EVEN_EXPR:
1854 case VEC_WIDEN_MULT_ODD_EXPR:
1855 {
1856 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1857
1858 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1859 return NULL_TREE;
1860
1861 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1862 return NULL_TREE;
1863 out_nelts = in_nelts / 2;
1864 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1865 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1866
1867 if (code == VEC_WIDEN_MULT_LO_EXPR)
1868 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1869 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1870 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1871 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1872 scale = 1, ofs = 0;
1873 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1874 scale = 1, ofs = 1;
1875
1876 tree_vector_builder elts (type, out_nelts, 1);
1877 for (out = 0; out < out_nelts; out++)
1878 {
1879 unsigned int in = (out << scale) + ofs;
1880 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1881 VECTOR_CST_ELT (arg1, in));
1882 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1883 VECTOR_CST_ELT (arg2, in));
1884
1885 if (t1 == NULL_TREE || t2 == NULL_TREE)
1886 return NULL_TREE;
1887 tree elt = const_binop (MULT_EXPR, t1, t2);
1888 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1889 return NULL_TREE;
1890 elts.quick_push (elt);
1891 }
1892
1893 return elts.build ();
1894 }
1895
1896 default:;
1897 }
1898
1899 if (TREE_CODE_CLASS (code) != tcc_binary)
1900 return NULL_TREE;
1901
1902 /* Make sure type and arg0 have the same saturating flag. */
1903 gcc_checking_assert (TYPE_SATURATING (type)
1904 == TYPE_SATURATING (TREE_TYPE (arg1)));
1905
1906 return const_binop (code, arg1, arg2);
1907 }
1908
1909 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1910 Return zero if computing the constants is not possible. */
1911
1912 tree
1913 const_unop (enum tree_code code, tree type, tree arg0)
1914 {
1915 /* Don't perform the operation, other than NEGATE and ABS, if
1916 flag_signaling_nans is on and the operand is a signaling NaN. */
1917 if (TREE_CODE (arg0) == REAL_CST
1918 && HONOR_SNANS (arg0)
1919 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1920 && code != NEGATE_EXPR
1921 && code != ABS_EXPR
1922 && code != ABSU_EXPR)
1923 return NULL_TREE;
1924
1925 switch (code)
1926 {
1927 CASE_CONVERT:
1928 case FLOAT_EXPR:
1929 case FIX_TRUNC_EXPR:
1930 case FIXED_CONVERT_EXPR:
1931 return fold_convert_const (code, type, arg0);
1932
1933 case ADDR_SPACE_CONVERT_EXPR:
1934 /* If the source address is 0, and the source address space
1935 cannot have a valid object at 0, fold to dest type null. */
1936 if (integer_zerop (arg0)
1937 && !(targetm.addr_space.zero_address_valid
1938 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1939 return fold_convert_const (code, type, arg0);
1940 break;
1941
1942 case VIEW_CONVERT_EXPR:
1943 return fold_view_convert_expr (type, arg0);
1944
1945 case NEGATE_EXPR:
1946 {
1947 /* Can't call fold_negate_const directly here as that doesn't
1948 handle all cases and we might not be able to negate some
1949 constants. */
1950 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1951 if (tem && CONSTANT_CLASS_P (tem))
1952 return tem;
1953 break;
1954 }
1955
1956 case ABS_EXPR:
1957 case ABSU_EXPR:
1958 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1959 return fold_abs_const (arg0, type);
1960 break;
1961
1962 case CONJ_EXPR:
1963 if (TREE_CODE (arg0) == COMPLEX_CST)
1964 {
1965 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1966 TREE_TYPE (type));
1967 return build_complex (type, TREE_REALPART (arg0), ipart);
1968 }
1969 break;
1970
1971 case BIT_NOT_EXPR:
1972 if (TREE_CODE (arg0) == INTEGER_CST)
1973 return fold_not_const (arg0, type);
1974 else if (POLY_INT_CST_P (arg0))
1975 return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1976 /* Perform BIT_NOT_EXPR on each element individually. */
1977 else if (TREE_CODE (arg0) == VECTOR_CST)
1978 {
1979 tree elem;
1980
1981 /* This can cope with stepped encodings because ~x == -1 - x. */
1982 tree_vector_builder elements;
1983 elements.new_unary_operation (type, arg0, true);
1984 unsigned int i, count = elements.encoded_nelts ();
1985 for (i = 0; i < count; ++i)
1986 {
1987 elem = VECTOR_CST_ELT (arg0, i);
1988 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1989 if (elem == NULL_TREE)
1990 break;
1991 elements.quick_push (elem);
1992 }
1993 if (i == count)
1994 return elements.build ();
1995 }
1996 break;
1997
1998 case TRUTH_NOT_EXPR:
1999 if (TREE_CODE (arg0) == INTEGER_CST)
2000 return constant_boolean_node (integer_zerop (arg0), type);
2001 break;
2002
2003 case REALPART_EXPR:
2004 if (TREE_CODE (arg0) == COMPLEX_CST)
2005 return fold_convert (type, TREE_REALPART (arg0));
2006 break;
2007
2008 case IMAGPART_EXPR:
2009 if (TREE_CODE (arg0) == COMPLEX_CST)
2010 return fold_convert (type, TREE_IMAGPART (arg0));
2011 break;
2012
2013 case VEC_UNPACK_LO_EXPR:
2014 case VEC_UNPACK_HI_EXPR:
2015 case VEC_UNPACK_FLOAT_LO_EXPR:
2016 case VEC_UNPACK_FLOAT_HI_EXPR:
2017 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2018 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2019 {
2020 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2021 enum tree_code subcode;
2022
2023 if (TREE_CODE (arg0) != VECTOR_CST)
2024 return NULL_TREE;
2025
2026 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2027 return NULL_TREE;
2028 out_nelts = in_nelts / 2;
2029 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2030
2031 unsigned int offset = 0;
2032 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2033 || code == VEC_UNPACK_FLOAT_LO_EXPR
2034 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2035 offset = out_nelts;
2036
2037 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
2038 subcode = NOP_EXPR;
2039 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
2040 || code == VEC_UNPACK_FLOAT_HI_EXPR)
2041 subcode = FLOAT_EXPR;
2042 else
2043 subcode = FIX_TRUNC_EXPR;
2044
2045 tree_vector_builder elts (type, out_nelts, 1);
2046 for (i = 0; i < out_nelts; i++)
2047 {
2048 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2049 VECTOR_CST_ELT (arg0, i + offset));
2050 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2051 return NULL_TREE;
2052 elts.quick_push (elt);
2053 }
2054
2055 return elts.build ();
2056 }
2057
2058 case VEC_DUPLICATE_EXPR:
2059 if (CONSTANT_CLASS_P (arg0))
2060 return build_vector_from_val (type, arg0);
2061 return NULL_TREE;
2062
2063 default:
2064 break;
2065 }
2066
2067 return NULL_TREE;
2068 }
2069
2070 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2071 indicates which particular sizetype to create. */
2072
2073 tree
2074 size_int_kind (poly_int64 number, enum size_type_kind kind)
2075 {
2076 return build_int_cst (sizetype_tab[(int) kind], number);
2077 }
2078
2079 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2080 is a tree code. The type of the result is taken from the operands.
2081 Both must be equivalent integer types, ala int_binop_types_match_p.
2082 If the operands are constant, so is the result. */
2083
2084 tree
2085 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2086 {
2087 tree type = TREE_TYPE (arg0);
2088
2089 if (arg0 == error_mark_node || arg1 == error_mark_node)
2090 return error_mark_node;
2091
2092 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2093 TREE_TYPE (arg1)));
2094
2095 /* Handle the special case of two poly_int constants faster. */
2096 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2097 {
2098 /* And some specific cases even faster than that. */
2099 if (code == PLUS_EXPR)
2100 {
2101 if (integer_zerop (arg0)
2102 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2103 return arg1;
2104 if (integer_zerop (arg1)
2105 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2106 return arg0;
2107 }
2108 else if (code == MINUS_EXPR)
2109 {
2110 if (integer_zerop (arg1)
2111 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2112 return arg0;
2113 }
2114 else if (code == MULT_EXPR)
2115 {
2116 if (integer_onep (arg0)
2117 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2118 return arg1;
2119 }
2120
2121 /* Handle general case of two integer constants. For sizetype
2122 constant calculations we always want to know about overflow,
2123 even in the unsigned case. */
2124 tree res = int_const_binop (code, arg0, arg1, -1);
2125 if (res != NULL_TREE)
2126 return res;
2127 }
2128
2129 return fold_build2_loc (loc, code, type, arg0, arg1);
2130 }
2131
2132 /* Given two values, either both of sizetype or both of bitsizetype,
2133 compute the difference between the two values. Return the value
2134 in signed type corresponding to the type of the operands. */
2135
2136 tree
2137 size_diffop_loc (location_t loc, tree arg0, tree arg1)
2138 {
2139 tree type = TREE_TYPE (arg0);
2140 tree ctype;
2141
2142 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2143 TREE_TYPE (arg1)));
2144
2145 /* If the type is already signed, just do the simple thing. */
2146 if (!TYPE_UNSIGNED (type))
2147 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2148
2149 if (type == sizetype)
2150 ctype = ssizetype;
2151 else if (type == bitsizetype)
2152 ctype = sbitsizetype;
2153 else
2154 ctype = signed_type_for (type);
2155
2156 /* If either operand is not a constant, do the conversions to the signed
2157 type and subtract. The hardware will do the right thing with any
2158 overflow in the subtraction. */
2159 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2160 return size_binop_loc (loc, MINUS_EXPR,
2161 fold_convert_loc (loc, ctype, arg0),
2162 fold_convert_loc (loc, ctype, arg1));
2163
2164 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2165 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2166 overflow) and negate (which can't either). Special-case a result
2167 of zero while we're here. */
2168 if (tree_int_cst_equal (arg0, arg1))
2169 return build_int_cst (ctype, 0);
2170 else if (tree_int_cst_lt (arg1, arg0))
2171 return fold_convert_loc (loc, ctype,
2172 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2173 else
2174 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2175 fold_convert_loc (loc, ctype,
2176 size_binop_loc (loc,
2177 MINUS_EXPR,
2178 arg1, arg0)));
2179 }
2180
2181 /* A subroutine of fold_convert_const handling conversions of an
2182 INTEGER_CST to another integer type. */
2183
2184 static tree
2185 fold_convert_const_int_from_int (tree type, const_tree arg1)
2186 {
2187 /* Given an integer constant, make new constant with new type,
2188 appropriately sign-extended or truncated. Use widest_int
2189 so that any extension is done according ARG1's type. */
2190 tree arg1_type = TREE_TYPE (arg1);
2191 unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2192 return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2193 TYPE_SIGN (arg1_type)),
2194 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2195 TREE_OVERFLOW (arg1));
2196 }
2197
2198 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2199 to an integer type. */
2200
2201 static tree
2202 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2203 {
2204 bool overflow = false;
2205 tree t;
2206
2207 /* The following code implements the floating point to integer
2208 conversion rules required by the Java Language Specification,
2209 that IEEE NaNs are mapped to zero and values that overflow
2210 the target precision saturate, i.e. values greater than
2211 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2212 are mapped to INT_MIN. These semantics are allowed by the
2213 C and C++ standards that simply state that the behavior of
2214 FP-to-integer conversion is unspecified upon overflow. */
2215
2216 wide_int val;
2217 REAL_VALUE_TYPE r;
2218 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2219
2220 switch (code)
2221 {
2222 case FIX_TRUNC_EXPR:
2223 real_trunc (&r, VOIDmode, &x);
2224 break;
2225
2226 default:
2227 gcc_unreachable ();
2228 }
2229
2230 /* If R is NaN, return zero and show we have an overflow. */
2231 if (REAL_VALUE_ISNAN (r))
2232 {
2233 overflow = true;
2234 val = wi::zero (TYPE_PRECISION (type));
2235 }
2236
2237 /* See if R is less than the lower bound or greater than the
2238 upper bound. */
2239
2240 if (! overflow)
2241 {
2242 tree lt = TYPE_MIN_VALUE (type);
2243 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2244 if (real_less (&r, &l))
2245 {
2246 overflow = true;
2247 val = wi::to_wide (lt);
2248 }
2249 }
2250
2251 if (! overflow)
2252 {
2253 tree ut = TYPE_MAX_VALUE (type);
2254 if (ut)
2255 {
2256 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2257 if (real_less (&u, &r))
2258 {
2259 overflow = true;
2260 val = wi::to_wide (ut);
2261 }
2262 }
2263 }
2264
2265 if (! overflow)
2266 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2267
2268 /* According to IEEE standard, for conversions from floating point to
2269 integer. When a NaN or infinite operand cannot be represented in the
2270 destination format and this cannot otherwise be indicated, the invalid
2271 operation exception shall be signaled. When a numeric operand would
2272 convert to an integer outside the range of the destination format, the
2273 invalid operation exception shall be signaled if this situation cannot
2274 otherwise be indicated. */
2275 if (!flag_trapping_math || !overflow)
2276 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2277 else
2278 t = NULL_TREE;
2279
2280 return t;
2281 }
2282
2283 /* A subroutine of fold_convert_const handling conversions of a
2284 FIXED_CST to an integer type. */
2285
2286 static tree
2287 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2288 {
2289 tree t;
2290 double_int temp, temp_trunc;
2291 scalar_mode mode;
2292
2293 /* Right shift FIXED_CST to temp by fbit. */
2294 temp = TREE_FIXED_CST (arg1).data;
2295 mode = TREE_FIXED_CST (arg1).mode;
2296 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2297 {
2298 temp = temp.rshift (GET_MODE_FBIT (mode),
2299 HOST_BITS_PER_DOUBLE_INT,
2300 SIGNED_FIXED_POINT_MODE_P (mode));
2301
2302 /* Left shift temp to temp_trunc by fbit. */
2303 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2304 HOST_BITS_PER_DOUBLE_INT,
2305 SIGNED_FIXED_POINT_MODE_P (mode));
2306 }
2307 else
2308 {
2309 temp = double_int_zero;
2310 temp_trunc = double_int_zero;
2311 }
2312
2313 /* If FIXED_CST is negative, we need to round the value toward 0.
2314 By checking if the fractional bits are not zero to add 1 to temp. */
2315 if (SIGNED_FIXED_POINT_MODE_P (mode)
2316 && temp_trunc.is_negative ()
2317 && TREE_FIXED_CST (arg1).data != temp_trunc)
2318 temp += double_int_one;
2319
2320 /* Given a fixed-point constant, make new constant with new type,
2321 appropriately sign-extended or truncated. */
2322 t = force_fit_type (type, temp, -1,
2323 (temp.is_negative ()
2324 && (TYPE_UNSIGNED (type)
2325 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2326 | TREE_OVERFLOW (arg1));
2327
2328 return t;
2329 }
2330
2331 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2332 to another floating point type. */
2333
2334 static tree
2335 fold_convert_const_real_from_real (tree type, const_tree arg1)
2336 {
2337 REAL_VALUE_TYPE value;
2338 tree t;
2339
2340 /* If the underlying modes are the same, simply treat it as
2341 copy and rebuild with TREE_REAL_CST information and the
2342 given type. */
2343 if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2344 {
2345 t = build_real (type, TREE_REAL_CST (arg1));
2346 return t;
2347 }
2348
2349 /* Don't perform the operation if flag_signaling_nans is on
2350 and the operand is a signaling NaN. */
2351 if (HONOR_SNANS (arg1)
2352 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2353 return NULL_TREE;
2354
2355 /* With flag_rounding_math we should respect the current rounding mode
2356 unless the conversion is exact. */
2357 if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2358 && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2359 return NULL_TREE;
2360
2361 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2362 t = build_real (type, value);
2363
2364 /* If converting an infinity or NAN to a representation that doesn't
2365 have one, set the overflow bit so that we can produce some kind of
2366 error message at the appropriate point if necessary. It's not the
2367 most user-friendly message, but it's better than nothing. */
2368 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2369 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2370 TREE_OVERFLOW (t) = 1;
2371 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2372 && !MODE_HAS_NANS (TYPE_MODE (type)))
2373 TREE_OVERFLOW (t) = 1;
2374 /* Regular overflow, conversion produced an infinity in a mode that
2375 can't represent them. */
2376 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2377 && REAL_VALUE_ISINF (value)
2378 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2379 TREE_OVERFLOW (t) = 1;
2380 else
2381 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2382 return t;
2383 }
2384
2385 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2386 to a floating point type. */
2387
2388 static tree
2389 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2390 {
2391 REAL_VALUE_TYPE value;
2392 tree t;
2393
2394 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2395 &TREE_FIXED_CST (arg1));
2396 t = build_real (type, value);
2397
2398 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2399 return t;
2400 }
2401
2402 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2403 to another fixed-point type. */
2404
2405 static tree
2406 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2407 {
2408 FIXED_VALUE_TYPE value;
2409 tree t;
2410 bool overflow_p;
2411
2412 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2413 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2414 t = build_fixed (type, value);
2415
2416 /* Propagate overflow flags. */
2417 if (overflow_p | TREE_OVERFLOW (arg1))
2418 TREE_OVERFLOW (t) = 1;
2419 return t;
2420 }
2421
2422 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2423 to a fixed-point type. */
2424
2425 static tree
2426 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2427 {
2428 FIXED_VALUE_TYPE value;
2429 tree t;
2430 bool overflow_p;
2431 double_int di;
2432
2433 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2434
2435 di.low = TREE_INT_CST_ELT (arg1, 0);
2436 if (TREE_INT_CST_NUNITS (arg1) == 1)
2437 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2438 else
2439 di.high = TREE_INT_CST_ELT (arg1, 1);
2440
2441 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2442 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2443 TYPE_SATURATING (type));
2444 t = build_fixed (type, value);
2445
2446 /* Propagate overflow flags. */
2447 if (overflow_p | TREE_OVERFLOW (arg1))
2448 TREE_OVERFLOW (t) = 1;
2449 return t;
2450 }
2451
2452 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2453 to a fixed-point type. */
2454
2455 static tree
2456 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2457 {
2458 FIXED_VALUE_TYPE value;
2459 tree t;
2460 bool overflow_p;
2461
2462 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2463 &TREE_REAL_CST (arg1),
2464 TYPE_SATURATING (type));
2465 t = build_fixed (type, value);
2466
2467 /* Propagate overflow flags. */
2468 if (overflow_p | TREE_OVERFLOW (arg1))
2469 TREE_OVERFLOW (t) = 1;
2470 return t;
2471 }
2472
2473 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2474 type TYPE. If no simplification can be done return NULL_TREE. */
2475
2476 static tree
2477 fold_convert_const (enum tree_code code, tree type, tree arg1)
2478 {
2479 tree arg_type = TREE_TYPE (arg1);
2480 if (arg_type == type)
2481 return arg1;
2482
2483 /* We can't widen types, since the runtime value could overflow the
2484 original type before being extended to the new type. */
2485 if (POLY_INT_CST_P (arg1)
2486 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2487 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2488 return build_poly_int_cst (type,
2489 poly_wide_int::from (poly_int_cst_value (arg1),
2490 TYPE_PRECISION (type),
2491 TYPE_SIGN (arg_type)));
2492
2493 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2494 || TREE_CODE (type) == OFFSET_TYPE)
2495 {
2496 if (TREE_CODE (arg1) == INTEGER_CST)
2497 return fold_convert_const_int_from_int (type, arg1);
2498 else if (TREE_CODE (arg1) == REAL_CST)
2499 return fold_convert_const_int_from_real (code, type, arg1);
2500 else if (TREE_CODE (arg1) == FIXED_CST)
2501 return fold_convert_const_int_from_fixed (type, arg1);
2502 }
2503 else if (SCALAR_FLOAT_TYPE_P (type))
2504 {
2505 if (TREE_CODE (arg1) == INTEGER_CST)
2506 {
2507 tree res = build_real_from_int_cst (type, arg1);
2508 /* Avoid the folding if flag_rounding_math is on and the
2509 conversion is not exact. */
2510 if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2511 {
2512 bool fail = false;
2513 wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2514 TYPE_PRECISION (TREE_TYPE (arg1)));
2515 if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2516 return NULL_TREE;
2517 }
2518 return res;
2519 }
2520 else if (TREE_CODE (arg1) == REAL_CST)
2521 return fold_convert_const_real_from_real (type, arg1);
2522 else if (TREE_CODE (arg1) == FIXED_CST)
2523 return fold_convert_const_real_from_fixed (type, arg1);
2524 }
2525 else if (FIXED_POINT_TYPE_P (type))
2526 {
2527 if (TREE_CODE (arg1) == FIXED_CST)
2528 return fold_convert_const_fixed_from_fixed (type, arg1);
2529 else if (TREE_CODE (arg1) == INTEGER_CST)
2530 return fold_convert_const_fixed_from_int (type, arg1);
2531 else if (TREE_CODE (arg1) == REAL_CST)
2532 return fold_convert_const_fixed_from_real (type, arg1);
2533 }
2534 else if (VECTOR_TYPE_P (type))
2535 {
2536 if (TREE_CODE (arg1) == VECTOR_CST
2537 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2538 {
2539 tree elttype = TREE_TYPE (type);
2540 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2541 /* We can't handle steps directly when extending, since the
2542 values need to wrap at the original precision first. */
2543 bool step_ok_p
2544 = (INTEGRAL_TYPE_P (elttype)
2545 && INTEGRAL_TYPE_P (arg1_elttype)
2546 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2547 tree_vector_builder v;
2548 if (!v.new_unary_operation (type, arg1, step_ok_p))
2549 return NULL_TREE;
2550 unsigned int len = v.encoded_nelts ();
2551 for (unsigned int i = 0; i < len; ++i)
2552 {
2553 tree elt = VECTOR_CST_ELT (arg1, i);
2554 tree cvt = fold_convert_const (code, elttype, elt);
2555 if (cvt == NULL_TREE)
2556 return NULL_TREE;
2557 v.quick_push (cvt);
2558 }
2559 return v.build ();
2560 }
2561 }
2562 else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2563 return build_zero_cst (type);
2564 return NULL_TREE;
2565 }
2566
2567 /* Construct a vector of zero elements of vector type TYPE. */
2568
2569 static tree
2570 build_zero_vector (tree type)
2571 {
2572 tree t;
2573
2574 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2575 return build_vector_from_val (type, t);
2576 }
2577
2578 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2579
2580 bool
2581 fold_convertible_p (const_tree type, const_tree arg)
2582 {
2583 const_tree orig = TREE_TYPE (arg);
2584
2585 if (type == orig)
2586 return true;
2587
2588 if (TREE_CODE (arg) == ERROR_MARK
2589 || TREE_CODE (type) == ERROR_MARK
2590 || TREE_CODE (orig) == ERROR_MARK)
2591 return false;
2592
2593 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2594 return true;
2595
2596 switch (TREE_CODE (type))
2597 {
2598 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2599 case POINTER_TYPE: case REFERENCE_TYPE:
2600 case OFFSET_TYPE:
2601 return (INTEGRAL_TYPE_P (orig)
2602 || (POINTER_TYPE_P (orig)
2603 && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2604 || TREE_CODE (orig) == OFFSET_TYPE);
2605
2606 case REAL_TYPE:
2607 case FIXED_POINT_TYPE:
2608 case VOID_TYPE:
2609 return TREE_CODE (type) == TREE_CODE (orig);
2610
2611 case VECTOR_TYPE:
2612 return (VECTOR_TYPE_P (orig)
2613 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2614 TYPE_VECTOR_SUBPARTS (orig))
2615 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2616
2617 default:
2618 return false;
2619 }
2620 }
2621
2622 /* Convert expression ARG to type TYPE. Used by the middle-end for
2623 simple conversions in preference to calling the front-end's convert. */
2624
2625 tree
2626 fold_convert_loc (location_t loc, tree type, tree arg)
2627 {
2628 tree orig = TREE_TYPE (arg);
2629 tree tem;
2630
2631 if (type == orig)
2632 return arg;
2633
2634 if (TREE_CODE (arg) == ERROR_MARK
2635 || TREE_CODE (type) == ERROR_MARK
2636 || TREE_CODE (orig) == ERROR_MARK)
2637 return error_mark_node;
2638
2639 switch (TREE_CODE (type))
2640 {
2641 case POINTER_TYPE:
2642 case REFERENCE_TYPE:
2643 /* Handle conversions between pointers to different address spaces. */
2644 if (POINTER_TYPE_P (orig)
2645 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2646 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2647 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2648 /* fall through */
2649
2650 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2651 case OFFSET_TYPE: case BITINT_TYPE:
2652 if (TREE_CODE (arg) == INTEGER_CST)
2653 {
2654 tem = fold_convert_const (NOP_EXPR, type, arg);
2655 if (tem != NULL_TREE)
2656 return tem;
2657 }
2658 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2659 || TREE_CODE (orig) == OFFSET_TYPE)
2660 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2661 if (TREE_CODE (orig) == COMPLEX_TYPE)
2662 return fold_convert_loc (loc, type,
2663 fold_build1_loc (loc, REALPART_EXPR,
2664 TREE_TYPE (orig), arg));
2665 gcc_assert (VECTOR_TYPE_P (orig)
2666 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2668
2669 case REAL_TYPE:
2670 if (TREE_CODE (arg) == INTEGER_CST)
2671 {
2672 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2673 if (tem != NULL_TREE)
2674 return tem;
2675 }
2676 else if (TREE_CODE (arg) == REAL_CST)
2677 {
2678 tem = fold_convert_const (NOP_EXPR, type, arg);
2679 if (tem != NULL_TREE)
2680 return tem;
2681 }
2682 else if (TREE_CODE (arg) == FIXED_CST)
2683 {
2684 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2685 if (tem != NULL_TREE)
2686 return tem;
2687 }
2688
2689 switch (TREE_CODE (orig))
2690 {
2691 case INTEGER_TYPE: case BITINT_TYPE:
2692 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2693 case POINTER_TYPE: case REFERENCE_TYPE:
2694 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2695
2696 case REAL_TYPE:
2697 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2698
2699 case FIXED_POINT_TYPE:
2700 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2701
2702 case COMPLEX_TYPE:
2703 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2704 return fold_convert_loc (loc, type, tem);
2705
2706 default:
2707 gcc_unreachable ();
2708 }
2709
2710 case FIXED_POINT_TYPE:
2711 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2712 || TREE_CODE (arg) == REAL_CST)
2713 {
2714 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2715 if (tem != NULL_TREE)
2716 goto fold_convert_exit;
2717 }
2718
2719 switch (TREE_CODE (orig))
2720 {
2721 case FIXED_POINT_TYPE:
2722 case INTEGER_TYPE:
2723 case ENUMERAL_TYPE:
2724 case BOOLEAN_TYPE:
2725 case REAL_TYPE:
2726 case BITINT_TYPE:
2727 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2728
2729 case COMPLEX_TYPE:
2730 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2731 return fold_convert_loc (loc, type, tem);
2732
2733 default:
2734 gcc_unreachable ();
2735 }
2736
2737 case COMPLEX_TYPE:
2738 switch (TREE_CODE (orig))
2739 {
2740 case INTEGER_TYPE: case BITINT_TYPE:
2741 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2742 case POINTER_TYPE: case REFERENCE_TYPE:
2743 case REAL_TYPE:
2744 case FIXED_POINT_TYPE:
2745 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2746 fold_convert_loc (loc, TREE_TYPE (type), arg),
2747 fold_convert_loc (loc, TREE_TYPE (type),
2748 integer_zero_node));
2749 case COMPLEX_TYPE:
2750 {
2751 tree rpart, ipart;
2752
2753 if (TREE_CODE (arg) == COMPLEX_EXPR)
2754 {
2755 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2756 TREE_OPERAND (arg, 0));
2757 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2758 TREE_OPERAND (arg, 1));
2759 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2760 }
2761
2762 arg = save_expr (arg);
2763 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2764 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2765 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2766 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2767 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2768 }
2769
2770 default:
2771 gcc_unreachable ();
2772 }
2773
2774 case VECTOR_TYPE:
2775 if (integer_zerop (arg))
2776 return build_zero_vector (type);
2777 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2778 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2779 || VECTOR_TYPE_P (orig));
2780 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2781
2782 case VOID_TYPE:
2783 tem = fold_ignored_result (arg);
2784 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2785
2786 case NULLPTR_TYPE:
2787 if (integer_zerop (arg))
2788 return build_zero_cst (type);
2789 /* FALLTHRU */
2790 default:
2791 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2792 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2793 gcc_unreachable ();
2794 }
2795 fold_convert_exit:
2796 tem = protected_set_expr_location_unshare (tem, loc);
2797 return tem;
2798 }
2799
2800 /* Return false if expr can be assumed not to be an lvalue, true
2801 otherwise. */
2802
2803 static bool
2804 maybe_lvalue_p (const_tree x)
2805 {
2806 /* We only need to wrap lvalue tree codes. */
2807 switch (TREE_CODE (x))
2808 {
2809 case VAR_DECL:
2810 case PARM_DECL:
2811 case RESULT_DECL:
2812 case LABEL_DECL:
2813 case FUNCTION_DECL:
2814 case SSA_NAME:
2815 case COMPOUND_LITERAL_EXPR:
2816
2817 case COMPONENT_REF:
2818 case MEM_REF:
2819 case INDIRECT_REF:
2820 case ARRAY_REF:
2821 case ARRAY_RANGE_REF:
2822 case BIT_FIELD_REF:
2823 case OBJ_TYPE_REF:
2824
2825 case REALPART_EXPR:
2826 case IMAGPART_EXPR:
2827 case PREINCREMENT_EXPR:
2828 case PREDECREMENT_EXPR:
2829 case SAVE_EXPR:
2830 case TRY_CATCH_EXPR:
2831 case WITH_CLEANUP_EXPR:
2832 case COMPOUND_EXPR:
2833 case MODIFY_EXPR:
2834 case TARGET_EXPR:
2835 case COND_EXPR:
2836 case BIND_EXPR:
2837 case VIEW_CONVERT_EXPR:
2838 break;
2839
2840 default:
2841 /* Assume the worst for front-end tree codes. */
2842 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2843 break;
2844 return false;
2845 }
2846
2847 return true;
2848 }
2849
2850 /* Return an expr equal to X but certainly not valid as an lvalue. */
2851
2852 tree
2853 non_lvalue_loc (location_t loc, tree x)
2854 {
2855 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2856 us. */
2857 if (in_gimple_form)
2858 return x;
2859
2860 if (! maybe_lvalue_p (x))
2861 return x;
2862 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2863 }
2864
2865 /* Given a tree comparison code, return the code that is the logical inverse.
2866 It is generally not safe to do this for floating-point comparisons, except
2867 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2868 ERROR_MARK in this case. */
2869
2870 enum tree_code
2871 invert_tree_comparison (enum tree_code code, bool honor_nans)
2872 {
2873 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2874 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2875 return ERROR_MARK;
2876
2877 switch (code)
2878 {
2879 case EQ_EXPR:
2880 return NE_EXPR;
2881 case NE_EXPR:
2882 return EQ_EXPR;
2883 case GT_EXPR:
2884 return honor_nans ? UNLE_EXPR : LE_EXPR;
2885 case GE_EXPR:
2886 return honor_nans ? UNLT_EXPR : LT_EXPR;
2887 case LT_EXPR:
2888 return honor_nans ? UNGE_EXPR : GE_EXPR;
2889 case LE_EXPR:
2890 return honor_nans ? UNGT_EXPR : GT_EXPR;
2891 case LTGT_EXPR:
2892 return UNEQ_EXPR;
2893 case UNEQ_EXPR:
2894 return LTGT_EXPR;
2895 case UNGT_EXPR:
2896 return LE_EXPR;
2897 case UNGE_EXPR:
2898 return LT_EXPR;
2899 case UNLT_EXPR:
2900 return GE_EXPR;
2901 case UNLE_EXPR:
2902 return GT_EXPR;
2903 case ORDERED_EXPR:
2904 return UNORDERED_EXPR;
2905 case UNORDERED_EXPR:
2906 return ORDERED_EXPR;
2907 default:
2908 gcc_unreachable ();
2909 }
2910 }
2911
2912 /* Similar, but return the comparison that results if the operands are
2913 swapped. This is safe for floating-point. */
2914
2915 enum tree_code
2916 swap_tree_comparison (enum tree_code code)
2917 {
2918 switch (code)
2919 {
2920 case EQ_EXPR:
2921 case NE_EXPR:
2922 case ORDERED_EXPR:
2923 case UNORDERED_EXPR:
2924 case LTGT_EXPR:
2925 case UNEQ_EXPR:
2926 return code;
2927 case GT_EXPR:
2928 return LT_EXPR;
2929 case GE_EXPR:
2930 return LE_EXPR;
2931 case LT_EXPR:
2932 return GT_EXPR;
2933 case LE_EXPR:
2934 return GE_EXPR;
2935 case UNGT_EXPR:
2936 return UNLT_EXPR;
2937 case UNGE_EXPR:
2938 return UNLE_EXPR;
2939 case UNLT_EXPR:
2940 return UNGT_EXPR;
2941 case UNLE_EXPR:
2942 return UNGE_EXPR;
2943 default:
2944 gcc_unreachable ();
2945 }
2946 }
2947
2948
2949 /* Convert a comparison tree code from an enum tree_code representation
2950 into a compcode bit-based encoding. This function is the inverse of
2951 compcode_to_comparison. */
2952
2953 static enum comparison_code
2954 comparison_to_compcode (enum tree_code code)
2955 {
2956 switch (code)
2957 {
2958 case LT_EXPR:
2959 return COMPCODE_LT;
2960 case EQ_EXPR:
2961 return COMPCODE_EQ;
2962 case LE_EXPR:
2963 return COMPCODE_LE;
2964 case GT_EXPR:
2965 return COMPCODE_GT;
2966 case NE_EXPR:
2967 return COMPCODE_NE;
2968 case GE_EXPR:
2969 return COMPCODE_GE;
2970 case ORDERED_EXPR:
2971 return COMPCODE_ORD;
2972 case UNORDERED_EXPR:
2973 return COMPCODE_UNORD;
2974 case UNLT_EXPR:
2975 return COMPCODE_UNLT;
2976 case UNEQ_EXPR:
2977 return COMPCODE_UNEQ;
2978 case UNLE_EXPR:
2979 return COMPCODE_UNLE;
2980 case UNGT_EXPR:
2981 return COMPCODE_UNGT;
2982 case LTGT_EXPR:
2983 return COMPCODE_LTGT;
2984 case UNGE_EXPR:
2985 return COMPCODE_UNGE;
2986 default:
2987 gcc_unreachable ();
2988 }
2989 }
2990
2991 /* Convert a compcode bit-based encoding of a comparison operator back
2992 to GCC's enum tree_code representation. This function is the
2993 inverse of comparison_to_compcode. */
2994
2995 static enum tree_code
2996 compcode_to_comparison (enum comparison_code code)
2997 {
2998 switch (code)
2999 {
3000 case COMPCODE_LT:
3001 return LT_EXPR;
3002 case COMPCODE_EQ:
3003 return EQ_EXPR;
3004 case COMPCODE_LE:
3005 return LE_EXPR;
3006 case COMPCODE_GT:
3007 return GT_EXPR;
3008 case COMPCODE_NE:
3009 return NE_EXPR;
3010 case COMPCODE_GE:
3011 return GE_EXPR;
3012 case COMPCODE_ORD:
3013 return ORDERED_EXPR;
3014 case COMPCODE_UNORD:
3015 return UNORDERED_EXPR;
3016 case COMPCODE_UNLT:
3017 return UNLT_EXPR;
3018 case COMPCODE_UNEQ:
3019 return UNEQ_EXPR;
3020 case COMPCODE_UNLE:
3021 return UNLE_EXPR;
3022 case COMPCODE_UNGT:
3023 return UNGT_EXPR;
3024 case COMPCODE_LTGT:
3025 return LTGT_EXPR;
3026 case COMPCODE_UNGE:
3027 return UNGE_EXPR;
3028 default:
3029 gcc_unreachable ();
3030 }
3031 }
3032
3033 /* Return true if COND1 tests the opposite condition of COND2. */
3034
3035 bool
3036 inverse_conditions_p (const_tree cond1, const_tree cond2)
3037 {
3038 return (COMPARISON_CLASS_P (cond1)
3039 && COMPARISON_CLASS_P (cond2)
3040 && (invert_tree_comparison
3041 (TREE_CODE (cond1),
3042 HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3043 && operand_equal_p (TREE_OPERAND (cond1, 0),
3044 TREE_OPERAND (cond2, 0), 0)
3045 && operand_equal_p (TREE_OPERAND (cond1, 1),
3046 TREE_OPERAND (cond2, 1), 0));
3047 }
3048
3049 /* Return a tree for the comparison which is the combination of
3050 doing the AND or OR (depending on CODE) of the two operations LCODE
3051 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3052 the possibility of trapping if the mode has NaNs, and return NULL_TREE
3053 if this makes the transformation invalid. */
3054
3055 tree
3056 combine_comparisons (location_t loc,
3057 enum tree_code code, enum tree_code lcode,
3058 enum tree_code rcode, tree truth_type,
3059 tree ll_arg, tree lr_arg)
3060 {
3061 bool honor_nans = HONOR_NANS (ll_arg);
3062 enum comparison_code lcompcode = comparison_to_compcode (lcode);
3063 enum comparison_code rcompcode = comparison_to_compcode (rcode);
3064 int compcode;
3065
3066 switch (code)
3067 {
3068 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3069 compcode = lcompcode & rcompcode;
3070 break;
3071
3072 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3073 compcode = lcompcode | rcompcode;
3074 break;
3075
3076 default:
3077 return NULL_TREE;
3078 }
3079
3080 if (!honor_nans)
3081 {
3082 /* Eliminate unordered comparisons, as well as LTGT and ORD
3083 which are not used unless the mode has NaNs. */
3084 compcode &= ~COMPCODE_UNORD;
3085 if (compcode == COMPCODE_LTGT)
3086 compcode = COMPCODE_NE;
3087 else if (compcode == COMPCODE_ORD)
3088 compcode = COMPCODE_TRUE;
3089 }
3090 else if (flag_trapping_math)
3091 {
3092 /* Check that the original operation and the optimized ones will trap
3093 under the same condition. */
3094 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3095 && (lcompcode != COMPCODE_EQ)
3096 && (lcompcode != COMPCODE_ORD);
3097 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3098 && (rcompcode != COMPCODE_EQ)
3099 && (rcompcode != COMPCODE_ORD);
3100 bool trap = (compcode & COMPCODE_UNORD) == 0
3101 && (compcode != COMPCODE_EQ)
3102 && (compcode != COMPCODE_ORD);
3103
3104 /* In a short-circuited boolean expression the LHS might be
3105 such that the RHS, if evaluated, will never trap. For
3106 example, in ORD (x, y) && (x < y), we evaluate the RHS only
3107 if neither x nor y is NaN. (This is a mixed blessing: for
3108 example, the expression above will never trap, hence
3109 optimizing it to x < y would be invalid). */
3110 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3111 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3112 rtrap = false;
3113
3114 /* If the comparison was short-circuited, and only the RHS
3115 trapped, we may now generate a spurious trap. */
3116 if (rtrap && !ltrap
3117 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3118 return NULL_TREE;
3119
3120 /* If we changed the conditions that cause a trap, we lose. */
3121 if ((ltrap || rtrap) != trap)
3122 return NULL_TREE;
3123 }
3124
3125 if (compcode == COMPCODE_TRUE)
3126 return constant_boolean_node (true, truth_type);
3127 else if (compcode == COMPCODE_FALSE)
3128 return constant_boolean_node (false, truth_type);
3129 else
3130 {
3131 enum tree_code tcode;
3132
3133 tcode = compcode_to_comparison ((enum comparison_code) compcode);
3134 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3135 }
3136 }
3137
3138 /* Return nonzero if two operands (typically of the same tree node)
3139 are necessarily equal. FLAGS modifies behavior as follows:
3140
3141 If OEP_ONLY_CONST is set, only return nonzero for constants.
3142 This function tests whether the operands are indistinguishable;
3143 it does not test whether they are equal using C's == operation.
3144 The distinction is important for IEEE floating point, because
3145 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3146 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3147
3148 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3149 even though it may hold multiple values during a function.
3150 This is because a GCC tree node guarantees that nothing else is
3151 executed between the evaluation of its "operands" (which may often
3152 be evaluated in arbitrary order). Hence if the operands themselves
3153 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3154 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3155 unset means assuming isochronic (or instantaneous) tree equivalence.
3156 Unless comparing arbitrary expression trees, such as from different
3157 statements, this flag can usually be left unset.
3158
3159 If OEP_PURE_SAME is set, then pure functions with identical arguments
3160 are considered the same. It is used when the caller has other ways
3161 to ensure that global memory is unchanged in between.
3162
3163 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3164 not values of expressions.
3165
3166 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3167 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3168
3169 If OEP_BITWISE is set, then require the values to be bitwise identical
3170 rather than simply numerically equal. Do not take advantage of things
3171 like math-related flags or undefined behavior; only return true for
3172 values that are provably bitwise identical in all circumstances.
3173
3174 If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3175 under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3176 rather than just numerically equivalent. The compared expressions must
3177 however perform the same operations but may do intermediate computations in
3178 differing signs. Because this comparison ignores any possible UB it cannot
3179 be used blindly without ensuring that the context you are using it in itself
3180 doesn't guarantee that there will be no UB. Conditional expressions are
3181 excluded from this relaxation.
3182
3183 When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3184 differing hashes even for cases where operand_compare::operand_equal_p
3185 compares equal.
3186
3187 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3188 any operand with side effect. This is unnecesarily conservative in the
3189 case we know that arg0 and arg1 are in disjoint code paths (such as in
3190 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3191 addresses with TREE_CONSTANT flag set so we know that &var == &var
3192 even if var is volatile. */
3193
3194 bool
3195 operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3196 unsigned int flags)
3197 {
3198 return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3199 }
3200
3201 /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3202 be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3203 expression is being compared under for equality. This means that they can
3204 differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3205
3206 bool
3207 operand_compare::operand_equal_p (tree type0, const_tree arg0,
3208 tree type1, const_tree arg1,
3209 unsigned int flags)
3210 {
3211 bool r;
3212 if (verify_hash_value (arg0, arg1, flags, &r))
3213 return r;
3214
3215 STRIP_ANY_LOCATION_WRAPPER (arg0);
3216 STRIP_ANY_LOCATION_WRAPPER (arg1);
3217
3218 /* If either is ERROR_MARK, they aren't equal. */
3219 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3220 || type0 == error_mark_node
3221 || type1 == error_mark_node)
3222 return false;
3223
3224 /* Similar, if either does not have a type (like a template id),
3225 they aren't equal. */
3226 if (!type0 || !type1)
3227 return false;
3228
3229 /* Bitwise identity makes no sense if the values have different layouts. */
3230 if ((flags & OEP_BITWISE)
3231 && !tree_nop_conversion_p (type0, type1))
3232 return false;
3233
3234 /* We cannot consider pointers to different address space equal. */
3235 if (POINTER_TYPE_P (type0)
3236 && POINTER_TYPE_P (type1)
3237 && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3238 != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3239 return false;
3240
3241 /* Check equality of integer constants before bailing out due to
3242 precision differences. */
3243 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3244 {
3245 /* Address of INTEGER_CST is not defined; check that we did not forget
3246 to drop the OEP_ADDRESS_OF flags. */
3247 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3248 return tree_int_cst_equal (arg0, arg1);
3249 }
3250
3251 if ((flags & OEP_ASSUME_WRAPV)
3252 && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3253 {
3254 const_tree t_arg0 = arg0;
3255 const_tree t_arg1 = arg1;
3256 STRIP_NOPS (arg0);
3257 STRIP_NOPS (arg1);
3258 /* Only recurse if the conversion was one that was valid to strip. */
3259 if (t_arg0 != arg0 || t_arg1 != arg1)
3260 return operand_equal_p (type0, arg0, type1, arg1, flags);
3261 }
3262
3263 if (!(flags & OEP_ADDRESS_OF))
3264 {
3265 /* Check if we are checking an operation where the two's compliment
3266 bitwise representation of the result is not the same between signed and
3267 unsigned arithmetic. */
3268 bool enforce_signedness = true;
3269 if (flags & OEP_ASSUME_WRAPV)
3270 {
3271 switch (TREE_CODE (arg0))
3272 {
3273 case PLUS_EXPR:
3274 case MINUS_EXPR:
3275 case MULT_EXPR:
3276 case BIT_IOR_EXPR:
3277 case BIT_XOR_EXPR:
3278 case BIT_AND_EXPR:
3279 case BIT_NOT_EXPR:
3280 case ABS_EXPR:
3281 CASE_CONVERT:
3282 case SSA_NAME:
3283 case INTEGER_CST:
3284 case VAR_DECL:
3285 case PARM_DECL:
3286 case RESULT_DECL:
3287 enforce_signedness = false;
3288 break;
3289
3290 default:
3291 break;
3292 }
3293 }
3294
3295 /* If both types don't have the same signedness, then we can't consider
3296 them equal. We must check this before the STRIP_NOPS calls
3297 because they may change the signedness of the arguments. As pointers
3298 strictly don't have a signedness, require either two pointers or
3299 two non-pointers as well. */
3300 if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3301 || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3302 && enforce_signedness))
3303 return false;
3304
3305 /* If both types don't have the same precision, then it is not safe
3306 to strip NOPs. */
3307 if (element_precision (type0) != element_precision (type1))
3308 return false;
3309
3310 STRIP_NOPS (arg0);
3311 STRIP_NOPS (arg1);
3312
3313 type0 = TREE_TYPE (arg0);
3314 type1 = TREE_TYPE (arg1);
3315 }
3316 #if 0
3317 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3318 sanity check once the issue is solved. */
3319 else
3320 /* Addresses of conversions and SSA_NAMEs (and many other things)
3321 are not defined. Check that we did not forget to drop the
3322 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3323 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3324 && TREE_CODE (arg0) != SSA_NAME);
3325 #endif
3326
3327 /* In case both args are comparisons but with different comparison
3328 code, try to swap the comparison operands of one arg to produce
3329 a match and compare that variant. */
3330 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3331 && COMPARISON_CLASS_P (arg0)
3332 && COMPARISON_CLASS_P (arg1))
3333 {
3334 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3335
3336 if (TREE_CODE (arg0) == swap_code)
3337 return operand_equal_p (TREE_OPERAND (arg0, 0),
3338 TREE_OPERAND (arg1, 1), flags)
3339 && operand_equal_p (TREE_OPERAND (arg0, 1),
3340 TREE_OPERAND (arg1, 0), flags);
3341 }
3342
3343 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3344 {
3345 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3346 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3347 ;
3348 else if (flags & OEP_ADDRESS_OF)
3349 {
3350 /* If we are interested in comparing addresses ignore
3351 MEM_REF wrappings of the base that can appear just for
3352 TBAA reasons. */
3353 if (TREE_CODE (arg0) == MEM_REF
3354 && DECL_P (arg1)
3355 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3356 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3357 && integer_zerop (TREE_OPERAND (arg0, 1)))
3358 return true;
3359 else if (TREE_CODE (arg1) == MEM_REF
3360 && DECL_P (arg0)
3361 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3362 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3363 && integer_zerop (TREE_OPERAND (arg1, 1)))
3364 return true;
3365 return false;
3366 }
3367 else
3368 return false;
3369 }
3370
3371 /* When not checking adddresses, this is needed for conversions and for
3372 COMPONENT_REF. Might as well play it safe and always test this. */
3373 if (TREE_CODE (type0) == ERROR_MARK
3374 || TREE_CODE (type1) == ERROR_MARK
3375 || (TYPE_MODE (type0) != TYPE_MODE (type1)
3376 && !(flags & OEP_ADDRESS_OF)))
3377 return false;
3378
3379 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3380 We don't care about side effects in that case because the SAVE_EXPR
3381 takes care of that for us. In all other cases, two expressions are
3382 equal if they have no side effects. If we have two identical
3383 expressions with side effects that should be treated the same due
3384 to the only side effects being identical SAVE_EXPR's, that will
3385 be detected in the recursive calls below.
3386 If we are taking an invariant address of two identical objects
3387 they are necessarily equal as well. */
3388 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3389 && (TREE_CODE (arg0) == SAVE_EXPR
3390 || (flags & OEP_MATCH_SIDE_EFFECTS)
3391 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3392 return true;
3393
3394 /* Next handle constant cases, those for which we can return 1 even
3395 if ONLY_CONST is set. */
3396 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3397 switch (TREE_CODE (arg0))
3398 {
3399 case INTEGER_CST:
3400 return tree_int_cst_equal (arg0, arg1);
3401
3402 case FIXED_CST:
3403 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3404 TREE_FIXED_CST (arg1));
3405
3406 case REAL_CST:
3407 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3408 return true;
3409
3410 if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3411 {
3412 /* If we do not distinguish between signed and unsigned zero,
3413 consider them equal. */
3414 if (real_zerop (arg0) && real_zerop (arg1))
3415 return true;
3416 }
3417 return false;
3418
3419 case VECTOR_CST:
3420 {
3421 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3422 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3423 return false;
3424
3425 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3426 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3427 return false;
3428
3429 unsigned int count = vector_cst_encoded_nelts (arg0);
3430 for (unsigned int i = 0; i < count; ++i)
3431 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3432 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3433 return false;
3434 return true;
3435 }
3436
3437 case COMPLEX_CST:
3438 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3439 flags)
3440 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3441 flags));
3442
3443 case STRING_CST:
3444 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3445 && ! memcmp (TREE_STRING_POINTER (arg0),
3446 TREE_STRING_POINTER (arg1),
3447 TREE_STRING_LENGTH (arg0)));
3448
3449 case RAW_DATA_CST:
3450 return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3451 && ! memcmp (RAW_DATA_POINTER (arg0),
3452 RAW_DATA_POINTER (arg1),
3453 RAW_DATA_LENGTH (arg0)));
3454
3455 case ADDR_EXPR:
3456 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3457 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3458 flags | OEP_ADDRESS_OF
3459 | OEP_MATCH_SIDE_EFFECTS);
3460 case CONSTRUCTOR:
3461 {
3462 /* In GIMPLE empty constructors are allowed in initializers of
3463 aggregates. */
3464 if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3465 return true;
3466
3467 /* See sem_variable::equals in ipa-icf for a similar approach. */
3468 if (TREE_CODE (type0) != TREE_CODE (type1))
3469 return false;
3470 else if (TREE_CODE (type0) == ARRAY_TYPE)
3471 {
3472 /* For arrays, check that the sizes all match. */
3473 const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3474 if (TYPE_MODE (type0) != TYPE_MODE (type1)
3475 || siz0 < 0
3476 || siz0 != int_size_in_bytes (type1))
3477 return false;
3478 }
3479 else if (!types_compatible_p (type0, type1))
3480 return false;
3481
3482 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3483 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3484 if (vec_safe_length (v0) != vec_safe_length (v1))
3485 return false;
3486
3487 /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3488 of the CONSTRUCTOR referenced indirectly. */
3489 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3490
3491 for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3492 {
3493 constructor_elt *c0 = &(*v0)[idx];
3494 constructor_elt *c1 = &(*v1)[idx];
3495
3496 /* Check that the values are the same... */
3497 if (c0->value != c1->value
3498 && !operand_equal_p (c0->value, c1->value, flags))
3499 return false;
3500
3501 /* ... and that they apply to the same field! */
3502 if (c0->index != c1->index
3503 && (TREE_CODE (type0) == ARRAY_TYPE
3504 ? !operand_equal_p (c0->index, c1->index, flags)
3505 : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3506 DECL_FIELD_OFFSET (c1->index),
3507 flags)
3508 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3509 DECL_FIELD_BIT_OFFSET (c1->index),
3510 flags)))
3511 return false;
3512 }
3513
3514 return true;
3515 }
3516
3517 default:
3518 break;
3519 }
3520
3521 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3522 two instances of undefined behavior will give identical results. */
3523 if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3524 return false;
3525
3526 /* Define macros to test an operand from arg0 and arg1 for equality and a
3527 variant that allows null and views null as being different from any
3528 non-null value. In the latter case, if either is null, the both
3529 must be; otherwise, do the normal comparison. */
3530 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3531 TREE_OPERAND (arg1, N), flags)
3532
3533 #define OP_SAME_WITH_NULL(N) \
3534 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3535 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3536
3537 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3538 {
3539 case tcc_unary:
3540 /* Two conversions are equal only if signedness and modes match. */
3541 switch (TREE_CODE (arg0))
3542 {
3543 CASE_CONVERT:
3544 case FIX_TRUNC_EXPR:
3545 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3546 return false;
3547 break;
3548 default:
3549 break;
3550 }
3551
3552 return OP_SAME_WITH_NULL (0);
3553
3554
3555 case tcc_comparison:
3556 case tcc_binary:
3557 if (OP_SAME (0) && OP_SAME (1))
3558 return true;
3559
3560 /* For commutative ops, allow the other order. */
3561 return (commutative_tree_code (TREE_CODE (arg0))
3562 && operand_equal_p (TREE_OPERAND (arg0, 0),
3563 TREE_OPERAND (arg1, 1), flags)
3564 && operand_equal_p (TREE_OPERAND (arg0, 1),
3565 TREE_OPERAND (arg1, 0), flags));
3566
3567 case tcc_reference:
3568 /* If either of the pointer (or reference) expressions we are
3569 dereferencing contain a side effect, these cannot be equal,
3570 but their addresses can be. */
3571 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3572 && (TREE_SIDE_EFFECTS (arg0)
3573 || TREE_SIDE_EFFECTS (arg1)))
3574 return false;
3575
3576 switch (TREE_CODE (arg0))
3577 {
3578 case INDIRECT_REF:
3579 if (!(flags & OEP_ADDRESS_OF))
3580 {
3581 if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3582 return false;
3583 /* Verify that the access types are compatible. */
3584 if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3585 return false;
3586 }
3587 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3588 return OP_SAME (0);
3589
3590 case IMAGPART_EXPR:
3591 /* Require the same offset. */
3592 if (!operand_equal_p (TYPE_SIZE (type0),
3593 TYPE_SIZE (type1),
3594 flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3595 return false;
3596
3597 /* Fallthru. */
3598 case REALPART_EXPR:
3599 case VIEW_CONVERT_EXPR:
3600 return OP_SAME (0);
3601
3602 case TARGET_MEM_REF:
3603 case MEM_REF:
3604 if (!(flags & OEP_ADDRESS_OF))
3605 {
3606 /* Require equal access sizes */
3607 if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3608 && (!TYPE_SIZE (type0)
3609 || !TYPE_SIZE (type1)
3610 || !operand_equal_p (TYPE_SIZE (type0),
3611 TYPE_SIZE (type1),
3612 flags)))
3613 return false;
3614 /* Verify that access happens in similar types. */
3615 if (!types_compatible_p (type0, type1))
3616 return false;
3617 /* Verify that accesses are TBAA compatible. */
3618 if (!alias_ptr_types_compatible_p
3619 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3620 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3621 || (MR_DEPENDENCE_CLIQUE (arg0)
3622 != MR_DEPENDENCE_CLIQUE (arg1))
3623 || (MR_DEPENDENCE_BASE (arg0)
3624 != MR_DEPENDENCE_BASE (arg1)))
3625 return false;
3626 /* Verify that alignment is compatible. */
3627 if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3628 return false;
3629 }
3630 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3631 return (OP_SAME (0) && OP_SAME (1)
3632 /* TARGET_MEM_REF require equal extra operands. */
3633 && (TREE_CODE (arg0) != TARGET_MEM_REF
3634 || (OP_SAME_WITH_NULL (2)
3635 && OP_SAME_WITH_NULL (3)
3636 && OP_SAME_WITH_NULL (4))));
3637
3638 case ARRAY_REF:
3639 case ARRAY_RANGE_REF:
3640 if (!OP_SAME (0))
3641 return false;
3642 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3643 /* Compare the array index by value if it is constant first as we
3644 may have different types but same value here. */
3645 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3646 TREE_OPERAND (arg1, 1))
3647 || OP_SAME (1))
3648 && OP_SAME_WITH_NULL (2)
3649 && OP_SAME_WITH_NULL (3)
3650 /* Compare low bound and element size as with OEP_ADDRESS_OF
3651 we have to account for the offset of the ref. */
3652 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3653 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3654 || (operand_equal_p (array_ref_low_bound
3655 (CONST_CAST_TREE (arg0)),
3656 array_ref_low_bound
3657 (CONST_CAST_TREE (arg1)), flags)
3658 && operand_equal_p (array_ref_element_size
3659 (CONST_CAST_TREE (arg0)),
3660 array_ref_element_size
3661 (CONST_CAST_TREE (arg1)),
3662 flags))));
3663
3664 case COMPONENT_REF:
3665 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3666 may be NULL when we're called to compare MEM_EXPRs. */
3667 if (!OP_SAME_WITH_NULL (0))
3668 return false;
3669 {
3670 bool compare_address = flags & OEP_ADDRESS_OF;
3671
3672 /* Most of time we only need to compare FIELD_DECLs for equality.
3673 However when determining address look into actual offsets.
3674 These may match for unions and unshared record types. */
3675 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3676 if (!OP_SAME (1))
3677 {
3678 if (compare_address
3679 && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3680 {
3681 tree field0 = TREE_OPERAND (arg0, 1);
3682 tree field1 = TREE_OPERAND (arg1, 1);
3683
3684 /* Non-FIELD_DECL operands can appear in C++ templates. */
3685 if (TREE_CODE (field0) != FIELD_DECL
3686 || TREE_CODE (field1) != FIELD_DECL)
3687 return false;
3688
3689 if (!DECL_FIELD_OFFSET (field0)
3690 || !DECL_FIELD_OFFSET (field1))
3691 return field0 == field1;
3692
3693 if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3694 DECL_FIELD_OFFSET (field1), flags)
3695 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3696 DECL_FIELD_BIT_OFFSET (field1),
3697 flags))
3698 return false;
3699 }
3700 else
3701 return false;
3702 }
3703 }
3704 return OP_SAME_WITH_NULL (2);
3705
3706 case BIT_FIELD_REF:
3707 if (!OP_SAME (0))
3708 return false;
3709 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3710 return OP_SAME (1) && OP_SAME (2);
3711
3712 default:
3713 return false;
3714 }
3715
3716 case tcc_expression:
3717 switch (TREE_CODE (arg0))
3718 {
3719 case ADDR_EXPR:
3720 /* Be sure we pass right ADDRESS_OF flag. */
3721 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3722 return operand_equal_p (TREE_OPERAND (arg0, 0),
3723 TREE_OPERAND (arg1, 0),
3724 flags | OEP_ADDRESS_OF);
3725
3726 case TRUTH_NOT_EXPR:
3727 return OP_SAME (0);
3728
3729 case TRUTH_ANDIF_EXPR:
3730 case TRUTH_ORIF_EXPR:
3731 return OP_SAME (0) && OP_SAME (1);
3732
3733 case WIDEN_MULT_PLUS_EXPR:
3734 case WIDEN_MULT_MINUS_EXPR:
3735 if (!OP_SAME (2))
3736 return false;
3737 /* The multiplcation operands are commutative. */
3738 /* FALLTHRU */
3739
3740 case TRUTH_AND_EXPR:
3741 case TRUTH_OR_EXPR:
3742 case TRUTH_XOR_EXPR:
3743 if (OP_SAME (0) && OP_SAME (1))
3744 return true;
3745
3746 /* Otherwise take into account this is a commutative operation. */
3747 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3748 TREE_OPERAND (arg1, 1), flags)
3749 && operand_equal_p (TREE_OPERAND (arg0, 1),
3750 TREE_OPERAND (arg1, 0), flags));
3751
3752 case COND_EXPR:
3753 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3754 return false;
3755 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3756 return OP_SAME (0);
3757
3758 case BIT_INSERT_EXPR:
3759 /* BIT_INSERT_EXPR has an implict operand as the type precision
3760 of op1. Need to check to make sure they are the same. */
3761 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3762 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3763 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3764 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3765 return false;
3766 /* FALLTHRU */
3767
3768 case VEC_COND_EXPR:
3769 case DOT_PROD_EXPR:
3770 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3771
3772 case MODIFY_EXPR:
3773 case INIT_EXPR:
3774 case COMPOUND_EXPR:
3775 case PREDECREMENT_EXPR:
3776 case PREINCREMENT_EXPR:
3777 case POSTDECREMENT_EXPR:
3778 case POSTINCREMENT_EXPR:
3779 if (flags & OEP_LEXICOGRAPHIC)
3780 return OP_SAME (0) && OP_SAME (1);
3781 return false;
3782
3783 case CLEANUP_POINT_EXPR:
3784 case EXPR_STMT:
3785 case SAVE_EXPR:
3786 if (flags & OEP_LEXICOGRAPHIC)
3787 return OP_SAME (0);
3788 return false;
3789
3790 case OBJ_TYPE_REF:
3791 /* Virtual table reference. */
3792 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3793 OBJ_TYPE_REF_EXPR (arg1), flags))
3794 return false;
3795 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3796 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3797 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3798 return false;
3799 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3800 OBJ_TYPE_REF_OBJECT (arg1), flags))
3801 return false;
3802 if (virtual_method_call_p (arg0))
3803 {
3804 if (!virtual_method_call_p (arg1))
3805 return false;
3806 return types_same_for_odr (obj_type_ref_class (arg0),
3807 obj_type_ref_class (arg1));
3808 }
3809 return false;
3810
3811 default:
3812 return false;
3813 }
3814
3815 case tcc_vl_exp:
3816 switch (TREE_CODE (arg0))
3817 {
3818 case CALL_EXPR:
3819 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3820 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3821 /* If not both CALL_EXPRs are either internal or normal function
3822 functions, then they are not equal. */
3823 return false;
3824 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3825 {
3826 /* If the CALL_EXPRs call different internal functions, then they
3827 are not equal. */
3828 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3829 return false;
3830 }
3831 else
3832 {
3833 /* If the CALL_EXPRs call different functions, then they are not
3834 equal. */
3835 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3836 flags))
3837 return false;
3838 }
3839
3840 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3841 {
3842 unsigned int cef = call_expr_flags (arg0);
3843 if (flags & OEP_PURE_SAME)
3844 cef &= ECF_CONST | ECF_PURE;
3845 else
3846 cef &= ECF_CONST;
3847 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3848 return false;
3849 }
3850
3851 /* Now see if all the arguments are the same. */
3852 {
3853 const_call_expr_arg_iterator iter0, iter1;
3854 const_tree a0, a1;
3855 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3856 a1 = first_const_call_expr_arg (arg1, &iter1);
3857 a0 && a1;
3858 a0 = next_const_call_expr_arg (&iter0),
3859 a1 = next_const_call_expr_arg (&iter1))
3860 if (! operand_equal_p (a0, a1, flags))
3861 return false;
3862
3863 /* If we get here and both argument lists are exhausted
3864 then the CALL_EXPRs are equal. */
3865 return ! (a0 || a1);
3866 }
3867 default:
3868 return false;
3869 }
3870
3871 case tcc_declaration:
3872 /* Consider __builtin_sqrt equal to sqrt. */
3873 if (TREE_CODE (arg0) == FUNCTION_DECL)
3874 return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3875 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3876 && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3877 == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3878
3879 if (DECL_P (arg0)
3880 && (flags & OEP_DECL_NAME)
3881 && (flags & OEP_LEXICOGRAPHIC))
3882 {
3883 /* Consider decls with the same name equal. The caller needs
3884 to make sure they refer to the same entity (such as a function
3885 formal parameter). */
3886 tree a0name = DECL_NAME (arg0);
3887 tree a1name = DECL_NAME (arg1);
3888 const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3889 const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3890 return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3891 }
3892 return false;
3893
3894 case tcc_exceptional:
3895 if (TREE_CODE (arg0) == CONSTRUCTOR)
3896 {
3897 if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3898 return false;
3899
3900 /* In GIMPLE constructors are used only to build vectors from
3901 elements. Individual elements in the constructor must be
3902 indexed in increasing order and form an initial sequence.
3903
3904 We make no effort to compare nonconstant ones in GENERIC. */
3905 if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3906 return false;
3907
3908 /* Be sure that vectors constructed have the same representation.
3909 We only tested element precision and modes to match.
3910 Vectors may be BLKmode and thus also check that the number of
3911 parts match. */
3912 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3913 TYPE_VECTOR_SUBPARTS (type1)))
3914 return false;
3915
3916 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3917 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3918 unsigned int len = vec_safe_length (v0);
3919
3920 if (len != vec_safe_length (v1))
3921 return false;
3922
3923 for (unsigned int i = 0; i < len; i++)
3924 {
3925 constructor_elt *c0 = &(*v0)[i];
3926 constructor_elt *c1 = &(*v1)[i];
3927
3928 if (!operand_equal_p (c0->value, c1->value, flags)
3929 /* In GIMPLE the indexes can be either NULL or matching i.
3930 Double check this so we won't get false
3931 positives for GENERIC. */
3932 || (c0->index
3933 && (TREE_CODE (c0->index) != INTEGER_CST
3934 || compare_tree_int (c0->index, i)))
3935 || (c1->index
3936 && (TREE_CODE (c1->index) != INTEGER_CST
3937 || compare_tree_int (c1->index, i))))
3938 return false;
3939 }
3940 return true;
3941 }
3942 else if (TREE_CODE (arg0) == STATEMENT_LIST
3943 && (flags & OEP_LEXICOGRAPHIC))
3944 {
3945 /* Compare the STATEMENT_LISTs. */
3946 tree_stmt_iterator tsi1, tsi2;
3947 tree body1 = CONST_CAST_TREE (arg0);
3948 tree body2 = CONST_CAST_TREE (arg1);
3949 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3950 tsi_next (&tsi1), tsi_next (&tsi2))
3951 {
3952 /* The lists don't have the same number of statements. */
3953 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3954 return false;
3955 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3956 return true;
3957 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3958 flags & (OEP_LEXICOGRAPHIC
3959 | OEP_NO_HASH_CHECK)))
3960 return false;
3961 }
3962 }
3963 return false;
3964
3965 case tcc_statement:
3966 switch (TREE_CODE (arg0))
3967 {
3968 case RETURN_EXPR:
3969 if (flags & OEP_LEXICOGRAPHIC)
3970 return OP_SAME_WITH_NULL (0);
3971 return false;
3972 case DEBUG_BEGIN_STMT:
3973 if (flags & OEP_LEXICOGRAPHIC)
3974 return true;
3975 return false;
3976 default:
3977 return false;
3978 }
3979
3980 default:
3981 return false;
3982 }
3983
3984 #undef OP_SAME
3985 #undef OP_SAME_WITH_NULL
3986 }
3987
3988 /* Generate a hash value for an expression. This can be used iteratively
3989 by passing a previous result as the HSTATE argument. */
3990
3991 void
3992 operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3993 unsigned int flags)
3994 {
3995 int i;
3996 enum tree_code code;
3997 enum tree_code_class tclass;
3998
3999 if (t == NULL_TREE || t == error_mark_node)
4000 {
4001 hstate.merge_hash (0);
4002 return;
4003 }
4004
4005 STRIP_ANY_LOCATION_WRAPPER (t);
4006
4007 if (!(flags & OEP_ADDRESS_OF))
4008 STRIP_NOPS (t);
4009
4010 code = TREE_CODE (t);
4011
4012 switch (code)
4013 {
4014 /* Alas, constants aren't shared, so we can't rely on pointer
4015 identity. */
4016 case VOID_CST:
4017 hstate.merge_hash (0);
4018 return;
4019 case INTEGER_CST:
4020 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4021 for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4022 hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4023 return;
4024 case REAL_CST:
4025 {
4026 unsigned int val2;
4027 if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4028 val2 = rvc_zero;
4029 else
4030 val2 = real_hash (TREE_REAL_CST_PTR (t));
4031 hstate.merge_hash (val2);
4032 return;
4033 }
4034 case FIXED_CST:
4035 {
4036 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4037 hstate.merge_hash (val2);
4038 return;
4039 }
4040 case STRING_CST:
4041 hstate.add ((const void *) TREE_STRING_POINTER (t),
4042 TREE_STRING_LENGTH (t));
4043 return;
4044 case RAW_DATA_CST:
4045 hstate.add ((const void *) RAW_DATA_POINTER (t),
4046 RAW_DATA_LENGTH (t));
4047 return;
4048 case COMPLEX_CST:
4049 hash_operand (TREE_REALPART (t), hstate, flags);
4050 hash_operand (TREE_IMAGPART (t), hstate, flags);
4051 return;
4052 case VECTOR_CST:
4053 {
4054 hstate.add_int (VECTOR_CST_NPATTERNS (t));
4055 hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4056 unsigned int count = vector_cst_encoded_nelts (t);
4057 for (unsigned int i = 0; i < count; ++i)
4058 hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4059 return;
4060 }
4061 case SSA_NAME:
4062 /* We can just compare by pointer. */
4063 hstate.add_hwi (SSA_NAME_VERSION (t));
4064 return;
4065 case PLACEHOLDER_EXPR:
4066 /* The node itself doesn't matter. */
4067 return;
4068 case BLOCK:
4069 case OMP_CLAUSE:
4070 case OMP_NEXT_VARIANT:
4071 case OMP_TARGET_DEVICE_MATCHES:
4072 /* Ignore. */
4073 return;
4074 case TREE_LIST:
4075 /* A list of expressions, for a CALL_EXPR or as the elements of a
4076 VECTOR_CST. */
4077 for (; t; t = TREE_CHAIN (t))
4078 hash_operand (TREE_VALUE (t), hstate, flags);
4079 return;
4080 case CONSTRUCTOR:
4081 {
4082 unsigned HOST_WIDE_INT idx;
4083 tree field, value;
4084 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4085 hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4086 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4087 {
4088 /* In GIMPLE the indexes can be either NULL or matching i. */
4089 if (field == NULL_TREE)
4090 field = bitsize_int (idx);
4091 if (TREE_CODE (field) == FIELD_DECL)
4092 {
4093 hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4094 hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4095 }
4096 else
4097 hash_operand (field, hstate, flags);
4098 hash_operand (value, hstate, flags);
4099 }
4100 return;
4101 }
4102 case STATEMENT_LIST:
4103 {
4104 tree_stmt_iterator i;
4105 for (i = tsi_start (CONST_CAST_TREE (t));
4106 !tsi_end_p (i); tsi_next (&i))
4107 hash_operand (tsi_stmt (i), hstate, flags);
4108 return;
4109 }
4110 case TREE_VEC:
4111 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4112 hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4113 return;
4114 case IDENTIFIER_NODE:
4115 hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4116 return;
4117 case FUNCTION_DECL:
4118 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4119 Otherwise nodes that compare equal according to operand_equal_p might
4120 get different hash codes. However, don't do this for machine specific
4121 or front end builtins, since the function code is overloaded in those
4122 cases. */
4123 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4124 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4125 {
4126 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4127 code = TREE_CODE (t);
4128 }
4129 /* FALL THROUGH */
4130 default:
4131 if (POLY_INT_CST_P (t))
4132 {
4133 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4134 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4135 return;
4136 }
4137 tclass = TREE_CODE_CLASS (code);
4138
4139 if (tclass == tcc_declaration)
4140 {
4141 /* DECL's have a unique ID */
4142 hstate.add_hwi (DECL_UID (t));
4143 }
4144 else if (tclass == tcc_comparison && !commutative_tree_code (code))
4145 {
4146 /* For comparisons that can be swapped, use the lower
4147 tree code. */
4148 enum tree_code ccode = swap_tree_comparison (code);
4149 if (code < ccode)
4150 ccode = code;
4151 hstate.add_object (ccode);
4152 hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4153 hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4154 }
4155 else if (CONVERT_EXPR_CODE_P (code))
4156 {
4157 /* NOP_EXPR and CONVERT_EXPR are considered equal by
4158 operand_equal_p. */
4159 enum tree_code ccode = NOP_EXPR;
4160 hstate.add_object (ccode);
4161
4162 /* Don't hash the type, that can lead to having nodes which
4163 compare equal according to operand_equal_p, but which
4164 have different hash codes. Make sure to include signedness
4165 in the hash computation. */
4166 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4167 hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4168 }
4169 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4170 else if (code == MEM_REF
4171 && (flags & OEP_ADDRESS_OF) != 0
4172 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4173 && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4174 && integer_zerop (TREE_OPERAND (t, 1)))
4175 hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4176 hstate, flags);
4177 /* Don't ICE on FE specific trees, or their arguments etc.
4178 during operand_equal_p hash verification. */
4179 else if (!IS_EXPR_CODE_CLASS (tclass))
4180 gcc_assert (flags & OEP_HASH_CHECK);
4181 else
4182 {
4183 unsigned int sflags = flags;
4184
4185 hstate.add_object (code);
4186
4187 switch (code)
4188 {
4189 case ADDR_EXPR:
4190 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4191 flags |= OEP_ADDRESS_OF;
4192 sflags = flags;
4193 break;
4194
4195 case INDIRECT_REF:
4196 case MEM_REF:
4197 case TARGET_MEM_REF:
4198 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4199 sflags = flags;
4200 break;
4201
4202 case COMPONENT_REF:
4203 if (sflags & OEP_ADDRESS_OF)
4204 {
4205 hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4206 hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4207 hstate, flags & ~OEP_ADDRESS_OF);
4208 hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4209 hstate, flags & ~OEP_ADDRESS_OF);
4210 return;
4211 }
4212 break;
4213 case ARRAY_REF:
4214 case ARRAY_RANGE_REF:
4215 case BIT_FIELD_REF:
4216 sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4217 break;
4218
4219 case COND_EXPR:
4220 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4221 break;
4222
4223 case WIDEN_MULT_PLUS_EXPR:
4224 case WIDEN_MULT_MINUS_EXPR:
4225 {
4226 /* The multiplication operands are commutative. */
4227 inchash::hash one, two;
4228 hash_operand (TREE_OPERAND (t, 0), one, flags);
4229 hash_operand (TREE_OPERAND (t, 1), two, flags);
4230 hstate.add_commutative (one, two);
4231 hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4232 return;
4233 }
4234
4235 case CALL_EXPR:
4236 if (CALL_EXPR_FN (t) == NULL_TREE)
4237 hstate.add_int (CALL_EXPR_IFN (t));
4238 break;
4239
4240 case TARGET_EXPR:
4241 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4242 Usually different TARGET_EXPRs just should use
4243 different temporaries in their slots. */
4244 hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4245 return;
4246
4247 case OBJ_TYPE_REF:
4248 /* Virtual table reference. */
4249 inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4250 flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4251 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4252 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4253 if (!virtual_method_call_p (t))
4254 return;
4255 if (tree c = obj_type_ref_class (t))
4256 {
4257 c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4258 /* We compute mangled names only when free_lang_data is run.
4259 In that case we can hash precisely. */
4260 if (TREE_CODE (c) == TYPE_DECL
4261 && DECL_ASSEMBLER_NAME_SET_P (c))
4262 hstate.add_object
4263 (IDENTIFIER_HASH_VALUE
4264 (DECL_ASSEMBLER_NAME (c)));
4265 }
4266 return;
4267 default:
4268 break;
4269 }
4270
4271 /* Don't hash the type, that can lead to having nodes which
4272 compare equal according to operand_equal_p, but which
4273 have different hash codes. */
4274 if (code == NON_LVALUE_EXPR)
4275 {
4276 /* Make sure to include signness in the hash computation. */
4277 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4278 hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4279 }
4280
4281 else if (commutative_tree_code (code))
4282 {
4283 /* It's a commutative expression. We want to hash it the same
4284 however it appears. We do this by first hashing both operands
4285 and then rehashing based on the order of their independent
4286 hashes. */
4287 inchash::hash one, two;
4288 hash_operand (TREE_OPERAND (t, 0), one, flags);
4289 hash_operand (TREE_OPERAND (t, 1), two, flags);
4290 hstate.add_commutative (one, two);
4291 }
4292 else
4293 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4294 hash_operand (TREE_OPERAND (t, i), hstate,
4295 i == 0 ? flags : sflags);
4296 }
4297 return;
4298 }
4299 }
4300
4301 bool
4302 operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4303 unsigned int flags, bool *ret)
4304 {
4305 /* When checking and unless comparing DECL names, verify that if
4306 the outermost operand_equal_p call returns non-zero then ARG0
4307 and ARG1 have the same hash value. */
4308 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4309 {
4310 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4311 {
4312 if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4313 {
4314 inchash::hash hstate0 (0), hstate1 (0);
4315 hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4316 hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4317 hashval_t h0 = hstate0.end ();
4318 hashval_t h1 = hstate1.end ();
4319 gcc_assert (h0 == h1);
4320 }
4321 *ret = true;
4322 }
4323 else
4324 *ret = false;
4325
4326 return true;
4327 }
4328
4329 return false;
4330 }
4331
4332
4333 static operand_compare default_compare_instance;
4334
4335 /* Conveinece wrapper around operand_compare class because usually we do
4336 not need to play with the valueizer. */
4337
4338 bool
4339 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4340 {
4341 return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4342 }
4343
4344 namespace inchash
4345 {
4346
4347 /* Generate a hash value for an expression. This can be used iteratively
4348 by passing a previous result as the HSTATE argument.
4349
4350 This function is intended to produce the same hash for expressions which
4351 would compare equal using operand_equal_p. */
4352 void
4353 add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4354 {
4355 default_compare_instance.hash_operand (t, hstate, flags);
4356 }
4357
4358 }
4359
4360 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4361 with a different signedness or a narrower precision. */
4362
4363 static bool
4364 operand_equal_for_comparison_p (tree arg0, tree arg1)
4365 {
4366 if (operand_equal_p (arg0, arg1, 0))
4367 return true;
4368
4369 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4370 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4371 return false;
4372
4373 /* Discard any conversions that don't change the modes of ARG0 and ARG1
4374 and see if the inner values are the same. This removes any
4375 signedness comparison, which doesn't matter here. */
4376 tree op0 = arg0;
4377 tree op1 = arg1;
4378 STRIP_NOPS (op0);
4379 STRIP_NOPS (op1);
4380 if (operand_equal_p (op0, op1, 0))
4381 return true;
4382
4383 /* Discard a single widening conversion from ARG1 and see if the inner
4384 value is the same as ARG0. */
4385 if (CONVERT_EXPR_P (arg1)
4386 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4387 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4388 < TYPE_PRECISION (TREE_TYPE (arg1))
4389 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4390 return true;
4391
4392 return false;
4393 }
4394
4395 /* See if ARG is an expression that is either a comparison or is performing
4396 arithmetic on comparisons. The comparisons must only be comparing
4397 two different values, which will be stored in *CVAL1 and *CVAL2; if
4398 they are nonzero it means that some operands have already been found.
4399 No variables may be used anywhere else in the expression except in the
4400 comparisons.
4401
4402 If this is true, return 1. Otherwise, return zero. */
4403
4404 static bool
4405 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4406 {
4407 enum tree_code code = TREE_CODE (arg);
4408 enum tree_code_class tclass = TREE_CODE_CLASS (code);
4409
4410 /* We can handle some of the tcc_expression cases here. */
4411 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4412 tclass = tcc_unary;
4413 else if (tclass == tcc_expression
4414 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4415 || code == COMPOUND_EXPR))
4416 tclass = tcc_binary;
4417
4418 switch (tclass)
4419 {
4420 case tcc_unary:
4421 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4422
4423 case tcc_binary:
4424 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4425 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4426
4427 case tcc_constant:
4428 return true;
4429
4430 case tcc_expression:
4431 if (code == COND_EXPR)
4432 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4433 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4434 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4435 return false;
4436
4437 case tcc_comparison:
4438 /* First see if we can handle the first operand, then the second. For
4439 the second operand, we know *CVAL1 can't be zero. It must be that
4440 one side of the comparison is each of the values; test for the
4441 case where this isn't true by failing if the two operands
4442 are the same. */
4443
4444 if (operand_equal_p (TREE_OPERAND (arg, 0),
4445 TREE_OPERAND (arg, 1), 0))
4446 return false;
4447
4448 if (*cval1 == 0)
4449 *cval1 = TREE_OPERAND (arg, 0);
4450 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4451 ;
4452 else if (*cval2 == 0)
4453 *cval2 = TREE_OPERAND (arg, 0);
4454 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4455 ;
4456 else
4457 return false;
4458
4459 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4460 ;
4461 else if (*cval2 == 0)
4462 *cval2 = TREE_OPERAND (arg, 1);
4463 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4464 ;
4465 else
4466 return false;
4467
4468 return true;
4469
4470 default:
4471 return false;
4472 }
4473 }
4474
4475 /* ARG is a tree that is known to contain just arithmetic operations and
4476 comparisons. Evaluate the operations in the tree substituting NEW0 for
4477 any occurrence of OLD0 as an operand of a comparison and likewise for
4478 NEW1 and OLD1. */
4479
4480 static tree
4481 eval_subst (location_t loc, tree arg, tree old0, tree new0,
4482 tree old1, tree new1)
4483 {
4484 tree type = TREE_TYPE (arg);
4485 enum tree_code code = TREE_CODE (arg);
4486 enum tree_code_class tclass = TREE_CODE_CLASS (code);
4487
4488 /* We can handle some of the tcc_expression cases here. */
4489 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4490 tclass = tcc_unary;
4491 else if (tclass == tcc_expression
4492 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4493 tclass = tcc_binary;
4494
4495 switch (tclass)
4496 {
4497 case tcc_unary:
4498 return fold_build1_loc (loc, code, type,
4499 eval_subst (loc, TREE_OPERAND (arg, 0),
4500 old0, new0, old1, new1));
4501
4502 case tcc_binary:
4503 return fold_build2_loc (loc, code, type,
4504 eval_subst (loc, TREE_OPERAND (arg, 0),
4505 old0, new0, old1, new1),
4506 eval_subst (loc, TREE_OPERAND (arg, 1),
4507 old0, new0, old1, new1));
4508
4509 case tcc_expression:
4510 switch (code)
4511 {
4512 case SAVE_EXPR:
4513 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4514 old1, new1);
4515
4516 case COMPOUND_EXPR:
4517 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4518 old1, new1);
4519
4520 case COND_EXPR:
4521 return fold_build3_loc (loc, code, type,
4522 eval_subst (loc, TREE_OPERAND (arg, 0),
4523 old0, new0, old1, new1),
4524 eval_subst (loc, TREE_OPERAND (arg, 1),
4525 old0, new0, old1, new1),
4526 eval_subst (loc, TREE_OPERAND (arg, 2),
4527 old0, new0, old1, new1));
4528 default:
4529 break;
4530 }
4531 /* Fall through - ??? */
4532
4533 case tcc_comparison:
4534 {
4535 tree arg0 = TREE_OPERAND (arg, 0);
4536 tree arg1 = TREE_OPERAND (arg, 1);
4537
4538 /* We need to check both for exact equality and tree equality. The
4539 former will be true if the operand has a side-effect. In that
4540 case, we know the operand occurred exactly once. */
4541
4542 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4543 arg0 = new0;
4544 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4545 arg0 = new1;
4546
4547 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4548 arg1 = new0;
4549 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4550 arg1 = new1;
4551
4552 return fold_build2_loc (loc, code, type, arg0, arg1);
4553 }
4554
4555 default:
4556 return arg;
4557 }
4558 }
4559
4560 /* Return a tree for the case when the result of an expression is RESULT
4561 converted to TYPE and OMITTED was previously an operand of the expression
4562 but is now not needed (e.g., we folded OMITTED * 0).
4563
4564 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4565 the conversion of RESULT to TYPE. */
4566
4567 tree
4568 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4569 {
4570 tree t = fold_convert_loc (loc, type, result);
4571
4572 /* If the resulting operand is an empty statement, just return the omitted
4573 statement casted to void. */
4574 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4575 return build1_loc (loc, NOP_EXPR, void_type_node,
4576 fold_ignored_result (omitted));
4577
4578 if (TREE_SIDE_EFFECTS (omitted))
4579 return build2_loc (loc, COMPOUND_EXPR, type,
4580 fold_ignored_result (omitted), t);
4581
4582 return non_lvalue_loc (loc, t);
4583 }
4584
4585 /* Return a tree for the case when the result of an expression is RESULT
4586 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4587 of the expression but are now not needed.
4588
4589 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4590 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4591 evaluated before OMITTED2. Otherwise, if neither has side effects,
4592 just do the conversion of RESULT to TYPE. */
4593
4594 tree
4595 omit_two_operands_loc (location_t loc, tree type, tree result,
4596 tree omitted1, tree omitted2)
4597 {
4598 tree t = fold_convert_loc (loc, type, result);
4599
4600 if (TREE_SIDE_EFFECTS (omitted2))
4601 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4602 if (TREE_SIDE_EFFECTS (omitted1))
4603 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4604
4605 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4606 }
4607
4608
4609 /* Return a simplified tree node for the truth-negation of ARG. This
4610 never alters ARG itself. We assume that ARG is an operation that
4611 returns a truth value (0 or 1).
4612
4613 FIXME: one would think we would fold the result, but it causes
4614 problems with the dominator optimizer. */
4615
4616 static tree
4617 fold_truth_not_expr (location_t loc, tree arg)
4618 {
4619 tree type = TREE_TYPE (arg);
4620 enum tree_code code = TREE_CODE (arg);
4621 location_t loc1, loc2;
4622
4623 /* If this is a comparison, we can simply invert it, except for
4624 floating-point non-equality comparisons, in which case we just
4625 enclose a TRUTH_NOT_EXPR around what we have. */
4626
4627 if (TREE_CODE_CLASS (code) == tcc_comparison)
4628 {
4629 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4630 if (FLOAT_TYPE_P (op_type)
4631 && flag_trapping_math
4632 && code != ORDERED_EXPR && code != UNORDERED_EXPR
4633 && code != NE_EXPR && code != EQ_EXPR)
4634 return NULL_TREE;
4635
4636 code = invert_tree_comparison (code, HONOR_NANS (op_type));
4637 if (code == ERROR_MARK)
4638 return NULL_TREE;
4639
4640 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4641 TREE_OPERAND (arg, 1));
4642 copy_warning (ret, arg);
4643 return ret;
4644 }
4645
4646 switch (code)
4647 {
4648 case INTEGER_CST:
4649 return constant_boolean_node (integer_zerop (arg), type);
4650
4651 case TRUTH_AND_EXPR:
4652 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4653 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4654 return build2_loc (loc, TRUTH_OR_EXPR, type,
4655 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4656 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4657
4658 case TRUTH_OR_EXPR:
4659 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4660 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4661 return build2_loc (loc, TRUTH_AND_EXPR, type,
4662 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4663 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4664
4665 case TRUTH_XOR_EXPR:
4666 /* Here we can invert either operand. We invert the first operand
4667 unless the second operand is a TRUTH_NOT_EXPR in which case our
4668 result is the XOR of the first operand with the inside of the
4669 negation of the second operand. */
4670
4671 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4672 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4673 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4674 else
4675 return build2_loc (loc, TRUTH_XOR_EXPR, type,
4676 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4677 TREE_OPERAND (arg, 1));
4678
4679 case TRUTH_ANDIF_EXPR:
4680 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4681 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4682 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4683 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4684 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4685
4686 case TRUTH_ORIF_EXPR:
4687 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4688 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4689 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4690 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4691 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4692
4693 case TRUTH_NOT_EXPR:
4694 return TREE_OPERAND (arg, 0);
4695
4696 case COND_EXPR:
4697 {
4698 tree arg1 = TREE_OPERAND (arg, 1);
4699 tree arg2 = TREE_OPERAND (arg, 2);
4700
4701 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4702 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4703
4704 /* A COND_EXPR may have a throw as one operand, which
4705 then has void type. Just leave void operands
4706 as they are. */
4707 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4708 VOID_TYPE_P (TREE_TYPE (arg1))
4709 ? arg1 : invert_truthvalue_loc (loc1, arg1),
4710 VOID_TYPE_P (TREE_TYPE (arg2))
4711 ? arg2 : invert_truthvalue_loc (loc2, arg2));
4712 }
4713
4714 case COMPOUND_EXPR:
4715 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4716 return build2_loc (loc, COMPOUND_EXPR, type,
4717 TREE_OPERAND (arg, 0),
4718 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4719
4720 case NON_LVALUE_EXPR:
4721 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4722 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4723
4724 CASE_CONVERT:
4725 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4726 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4727
4728 /* fall through */
4729
4730 case FLOAT_EXPR:
4731 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4732 return build1_loc (loc, TREE_CODE (arg), type,
4733 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4734
4735 case BIT_AND_EXPR:
4736 if (!integer_onep (TREE_OPERAND (arg, 1)))
4737 return NULL_TREE;
4738 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4739
4740 case SAVE_EXPR:
4741 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4742
4743 case CLEANUP_POINT_EXPR:
4744 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4745 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4746 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4747
4748 default:
4749 return NULL_TREE;
4750 }
4751 }
4752
4753 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4754 assume that ARG is an operation that returns a truth value (0 or 1
4755 for scalars, 0 or -1 for vectors). Return the folded expression if
4756 folding is successful. Otherwise, return NULL_TREE. */
4757
4758 static tree
4759 fold_invert_truthvalue (location_t loc, tree arg)
4760 {
4761 tree type = TREE_TYPE (arg);
4762 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4763 ? BIT_NOT_EXPR
4764 : TRUTH_NOT_EXPR,
4765 type, arg);
4766 }
4767
4768 /* Return a simplified tree node for the truth-negation of ARG. This
4769 never alters ARG itself. We assume that ARG is an operation that
4770 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4771
4772 tree
4773 invert_truthvalue_loc (location_t loc, tree arg)
4774 {
4775 if (TREE_CODE (arg) == ERROR_MARK)
4776 return arg;
4777
4778 tree type = TREE_TYPE (arg);
4779 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4780 ? BIT_NOT_EXPR
4781 : TRUTH_NOT_EXPR,
4782 type, arg);
4783 }
4784
4785 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4786 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4787 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4788 is the original memory reference used to preserve the alias set of
4789 the access. */
4790
4791 tree
4792 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4793 HOST_WIDE_INT bitsize, poly_int64 bitpos,
4794 int unsignedp, int reversep)
4795 {
4796 tree result, bftype;
4797
4798 /* Attempt not to lose the access path if possible. */
4799 if (TREE_CODE (orig_inner) == COMPONENT_REF)
4800 {
4801 tree ninner = TREE_OPERAND (orig_inner, 0);
4802 machine_mode nmode;
4803 poly_int64 nbitsize, nbitpos;
4804 tree noffset;
4805 int nunsignedp, nreversep, nvolatilep = 0;
4806 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4807 &noffset, &nmode, &nunsignedp,
4808 &nreversep, &nvolatilep);
4809 if (base == inner
4810 && noffset == NULL_TREE
4811 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4812 && !reversep
4813 && !nreversep
4814 && !nvolatilep)
4815 {
4816 inner = ninner;
4817 bitpos -= nbitpos;
4818 }
4819 }
4820
4821 alias_set_type iset = get_alias_set (orig_inner);
4822 if (iset == 0 && get_alias_set (inner) != iset)
4823 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4824 build_fold_addr_expr (inner),
4825 build_int_cst (ptr_type_node, 0));
4826
4827 if (known_eq (bitpos, 0) && !reversep)
4828 {
4829 tree size = TYPE_SIZE (TREE_TYPE (inner));
4830 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4831 || POINTER_TYPE_P (TREE_TYPE (inner)))
4832 && tree_fits_shwi_p (size)
4833 && tree_to_shwi (size) == bitsize)
4834 return fold_convert_loc (loc, type, inner);
4835 }
4836
4837 bftype = type;
4838 if (TYPE_PRECISION (bftype) != bitsize
4839 || TYPE_UNSIGNED (bftype) == !unsignedp)
4840 bftype = build_nonstandard_integer_type (bitsize, 0);
4841
4842 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4843 bitsize_int (bitsize), bitsize_int (bitpos));
4844 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4845
4846 if (bftype != type)
4847 result = fold_convert_loc (loc, type, result);
4848
4849 return result;
4850 }
4851
4852 /* Optimize a bit-field compare.
4853
4854 There are two cases: First is a compare against a constant and the
4855 second is a comparison of two items where the fields are at the same
4856 bit position relative to the start of a chunk (byte, halfword, word)
4857 large enough to contain it. In these cases we can avoid the shift
4858 implicit in bitfield extractions.
4859
4860 For constants, we emit a compare of the shifted constant with the
4861 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4862 compared. For two fields at the same position, we do the ANDs with the
4863 similar mask and compare the result of the ANDs.
4864
4865 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4866 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4867 are the left and right operands of the comparison, respectively.
4868
4869 If the optimization described above can be done, we return the resulting
4870 tree. Otherwise we return zero. */
4871
4872 static tree
4873 optimize_bit_field_compare (location_t loc, enum tree_code code,
4874 tree compare_type, tree lhs, tree rhs)
4875 {
4876 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4877 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4878 tree type = TREE_TYPE (lhs);
4879 tree unsigned_type;
4880 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4881 machine_mode lmode, rmode;
4882 scalar_int_mode nmode;
4883 int lunsignedp, runsignedp;
4884 int lreversep, rreversep;
4885 int lvolatilep = 0, rvolatilep = 0;
4886 tree linner, rinner = NULL_TREE;
4887 tree mask;
4888 tree offset;
4889
4890 /* Get all the information about the extractions being done. If the bit size
4891 is the same as the size of the underlying object, we aren't doing an
4892 extraction at all and so can do nothing. We also don't want to
4893 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4894 then will no longer be able to replace it. */
4895 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4896 &lunsignedp, &lreversep, &lvolatilep);
4897 if (linner == lhs
4898 || !known_size_p (plbitsize)
4899 || !plbitsize.is_constant (&lbitsize)
4900 || !plbitpos.is_constant (&lbitpos)
4901 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4902 || offset != 0
4903 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4904 || lvolatilep)
4905 return 0;
4906
4907 if (const_p)
4908 rreversep = lreversep;
4909 else
4910 {
4911 /* If this is not a constant, we can only do something if bit positions,
4912 sizes, signedness and storage order are the same. */
4913 rinner
4914 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4915 &runsignedp, &rreversep, &rvolatilep);
4916
4917 if (rinner == rhs
4918 || maybe_ne (lbitpos, rbitpos)
4919 || maybe_ne (lbitsize, rbitsize)
4920 || lunsignedp != runsignedp
4921 || lreversep != rreversep
4922 || offset != 0
4923 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4924 || rvolatilep)
4925 return 0;
4926 }
4927
4928 /* Honor the C++ memory model and mimic what RTL expansion does. */
4929 poly_uint64 bitstart = 0;
4930 poly_uint64 bitend = 0;
4931 if (TREE_CODE (lhs) == COMPONENT_REF)
4932 {
4933 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4934 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4935 return 0;
4936 }
4937
4938 /* See if we can find a mode to refer to this field. We should be able to,
4939 but fail if we can't. */
4940 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4941 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4942 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4943 TYPE_ALIGN (TREE_TYPE (rinner))),
4944 BITS_PER_WORD, false, &nmode))
4945 return 0;
4946
4947 /* Set signed and unsigned types of the precision of this mode for the
4948 shifts below. */
4949 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4950
4951 /* Compute the bit position and size for the new reference and our offset
4952 within it. If the new reference is the same size as the original, we
4953 won't optimize anything, so return zero. */
4954 nbitsize = GET_MODE_BITSIZE (nmode);
4955 nbitpos = lbitpos & ~ (nbitsize - 1);
4956 lbitpos -= nbitpos;
4957 if (nbitsize == lbitsize)
4958 return 0;
4959
4960 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4961 lbitpos = nbitsize - lbitsize - lbitpos;
4962
4963 /* Make the mask to be used against the extracted field. */
4964 mask = build_int_cst_type (unsigned_type, -1);
4965 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4966 mask = const_binop (RSHIFT_EXPR, mask,
4967 size_int (nbitsize - lbitsize - lbitpos));
4968
4969 if (! const_p)
4970 {
4971 if (nbitpos < 0)
4972 return 0;
4973
4974 /* If not comparing with constant, just rework the comparison
4975 and return. */
4976 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4977 nbitsize, nbitpos, 1, lreversep);
4978 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4979 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4980 nbitsize, nbitpos, 1, rreversep);
4981 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4982 return fold_build2_loc (loc, code, compare_type, t1, t2);
4983 }
4984
4985 /* Otherwise, we are handling the constant case. See if the constant is too
4986 big for the field. Warn and return a tree for 0 (false) if so. We do
4987 this not only for its own sake, but to avoid having to test for this
4988 error case below. If we didn't, we might generate wrong code.
4989
4990 For unsigned fields, the constant shifted right by the field length should
4991 be all zero. For signed fields, the high-order bits should agree with
4992 the sign bit. */
4993
4994 if (lunsignedp)
4995 {
4996 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4997 {
4998 warning (0, "comparison is always %d due to width of bit-field",
4999 code == NE_EXPR);
5000 return constant_boolean_node (code == NE_EXPR, compare_type);
5001 }
5002 }
5003 else
5004 {
5005 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5006 if (tem != 0 && tem != -1)
5007 {
5008 warning (0, "comparison is always %d due to width of bit-field",
5009 code == NE_EXPR);
5010 return constant_boolean_node (code == NE_EXPR, compare_type);
5011 }
5012 }
5013
5014 if (nbitpos < 0)
5015 return 0;
5016
5017 /* Single-bit compares should always be against zero. */
5018 if (lbitsize == 1 && ! integer_zerop (rhs))
5019 {
5020 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5021 rhs = build_int_cst (type, 0);
5022 }
5023
5024 /* Make a new bitfield reference, shift the constant over the
5025 appropriate number of bits and mask it with the computed mask
5026 (in case this was a signed field). If we changed it, make a new one. */
5027 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5028 nbitsize, nbitpos, 1, lreversep);
5029
5030 rhs = const_binop (BIT_AND_EXPR,
5031 const_binop (LSHIFT_EXPR,
5032 fold_convert_loc (loc, unsigned_type, rhs),
5033 size_int (lbitpos)),
5034 mask);
5035
5036 lhs = build2_loc (loc, code, compare_type,
5037 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5038 return lhs;
5039 }
5040
5041 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5042 represents the sign bit of EXP's type. If EXP represents a sign
5043 or zero extension, also test VAL against the unextended type.
5044 The return value is the (sub)expression whose sign bit is VAL,
5045 or NULL_TREE otherwise. */
5046
5047 tree
5048 sign_bit_p (tree exp, const_tree val)
5049 {
5050 int width;
5051 tree t;
5052
5053 /* Tree EXP must have an integral type. */
5054 t = TREE_TYPE (exp);
5055 if (! INTEGRAL_TYPE_P (t))
5056 return NULL_TREE;
5057
5058 /* Tree VAL must be an integer constant. */
5059 if (TREE_CODE (val) != INTEGER_CST
5060 || TREE_OVERFLOW (val))
5061 return NULL_TREE;
5062
5063 width = TYPE_PRECISION (t);
5064 if (wi::only_sign_bit_p (wi::to_wide (val), width))
5065 return exp;
5066
5067 /* Handle extension from a narrower type. */
5068 if (TREE_CODE (exp) == NOP_EXPR
5069 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5070 return sign_bit_p (TREE_OPERAND (exp, 0), val);
5071
5072 return NULL_TREE;
5073 }
5074
5075 /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5076 operand is simple enough to be evaluated unconditionally. */
5077
5078 static bool
5079 simple_operand_p (const_tree exp)
5080 {
5081 /* Strip any conversions that don't change the machine mode. */
5082 STRIP_NOPS (exp);
5083
5084 return (CONSTANT_CLASS_P (exp)
5085 || TREE_CODE (exp) == SSA_NAME
5086 || (DECL_P (exp)
5087 && ! TREE_ADDRESSABLE (exp)
5088 && ! TREE_THIS_VOLATILE (exp)
5089 && ! DECL_NONLOCAL (exp)
5090 /* Don't regard global variables as simple. They may be
5091 allocated in ways unknown to the compiler (shared memory,
5092 #pragma weak, etc). */
5093 && ! TREE_PUBLIC (exp)
5094 && ! DECL_EXTERNAL (exp)
5095 /* Weakrefs are not safe to be read, since they can be NULL.
5096 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5097 have DECL_WEAK flag set. */
5098 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5099 /* Loading a static variable is unduly expensive, but global
5100 registers aren't expensive. */
5101 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5102 }
5103
5104 /* Determine if an operand is simple enough to be evaluated unconditionally.
5105 In addition to simple_operand_p, we assume that comparisons, conversions,
5106 and logic-not operations are simple, if their operands are simple, too. */
5107
5108 bool
5109 simple_condition_p (tree exp)
5110 {
5111 enum tree_code code;
5112
5113 if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5114 return false;
5115
5116 while (CONVERT_EXPR_P (exp))
5117 exp = TREE_OPERAND (exp, 0);
5118
5119 code = TREE_CODE (exp);
5120
5121 if (TREE_CODE_CLASS (code) == tcc_comparison)
5122 return (simple_operand_p (TREE_OPERAND (exp, 0))
5123 && simple_operand_p (TREE_OPERAND (exp, 1)));
5124
5125 if (code == TRUTH_NOT_EXPR)
5126 return simple_condition_p (TREE_OPERAND (exp, 0));
5127
5128 return simple_operand_p (exp);
5129 }
5130
5131
5132 /* The following functions are subroutines to fold_range_test and allow it to
5133 try to change a logical combination of comparisons into a range test.
5134
5135 For example, both
5136 X == 2 || X == 3 || X == 4 || X == 5
5137 and
5138 X >= 2 && X <= 5
5139 are converted to
5140 (unsigned) (X - 2) <= 3
5141
5142 We describe each set of comparisons as being either inside or outside
5143 a range, using a variable named like IN_P, and then describe the
5144 range with a lower and upper bound. If one of the bounds is omitted,
5145 it represents either the highest or lowest value of the type.
5146
5147 In the comments below, we represent a range by two numbers in brackets
5148 preceded by a "+" to designate being inside that range, or a "-" to
5149 designate being outside that range, so the condition can be inverted by
5150 flipping the prefix. An omitted bound is represented by a "-". For
5151 example, "- [-, 10]" means being outside the range starting at the lowest
5152 possible value and ending at 10, in other words, being greater than 10.
5153 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5154 always false.
5155
5156 We set up things so that the missing bounds are handled in a consistent
5157 manner so neither a missing bound nor "true" and "false" need to be
5158 handled using a special case. */
5159
5160 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5161 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5162 and UPPER1_P are nonzero if the respective argument is an upper bound
5163 and zero for a lower. TYPE, if nonzero, is the type of the result; it
5164 must be specified for a comparison. ARG1 will be converted to ARG0's
5165 type if both are specified. */
5166
5167 static tree
5168 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5169 tree arg1, int upper1_p)
5170 {
5171 tree tem;
5172 int result;
5173 int sgn0, sgn1;
5174
5175 /* If neither arg represents infinity, do the normal operation.
5176 Else, if not a comparison, return infinity. Else handle the special
5177 comparison rules. Note that most of the cases below won't occur, but
5178 are handled for consistency. */
5179
5180 if (arg0 != 0 && arg1 != 0)
5181 {
5182 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5183 arg0, fold_convert (TREE_TYPE (arg0), arg1));
5184 STRIP_NOPS (tem);
5185 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5186 }
5187
5188 if (TREE_CODE_CLASS (code) != tcc_comparison)
5189 return 0;
5190
5191 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5192 for neither. In real maths, we cannot assume open ended ranges are
5193 the same. But, this is computer arithmetic, where numbers are finite.
5194 We can therefore make the transformation of any unbounded range with
5195 the value Z, Z being greater than any representable number. This permits
5196 us to treat unbounded ranges as equal. */
5197 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5198 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5199 switch (code)
5200 {
5201 case EQ_EXPR:
5202 result = sgn0 == sgn1;
5203 break;
5204 case NE_EXPR:
5205 result = sgn0 != sgn1;
5206 break;
5207 case LT_EXPR:
5208 result = sgn0 < sgn1;
5209 break;
5210 case LE_EXPR:
5211 result = sgn0 <= sgn1;
5212 break;
5213 case GT_EXPR:
5214 result = sgn0 > sgn1;
5215 break;
5216 case GE_EXPR:
5217 result = sgn0 >= sgn1;
5218 break;
5219 default:
5220 gcc_unreachable ();
5221 }
5222
5223 return constant_boolean_node (result, type);
5224 }
5225
5226 /* Helper routine for make_range. Perform one step for it, return
5227 new expression if the loop should continue or NULL_TREE if it should
5228 stop. */
5229
5230 tree
5231 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5232 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
5233 bool *strict_overflow_p)
5234 {
5235 tree arg0_type = TREE_TYPE (arg0);
5236 tree n_low, n_high, low = *p_low, high = *p_high;
5237 int in_p = *p_in_p, n_in_p;
5238
5239 switch (code)
5240 {
5241 case TRUTH_NOT_EXPR:
5242 /* We can only do something if the range is testing for zero. */
5243 if (low == NULL_TREE || high == NULL_TREE
5244 || ! integer_zerop (low) || ! integer_zerop (high))
5245 return NULL_TREE;
5246 *p_in_p = ! in_p;
5247 return arg0;
5248
5249 case EQ_EXPR: case NE_EXPR:
5250 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5251 /* We can only do something if the range is testing for zero
5252 and if the second operand is an integer constant. Note that
5253 saying something is "in" the range we make is done by
5254 complementing IN_P since it will set in the initial case of
5255 being not equal to zero; "out" is leaving it alone. */
5256 if (low == NULL_TREE || high == NULL_TREE
5257 || ! integer_zerop (low) || ! integer_zerop (high)
5258 || TREE_CODE (arg1) != INTEGER_CST)
5259 return NULL_TREE;
5260
5261 switch (code)
5262 {
5263 case NE_EXPR: /* - [c, c] */
5264 low = high = arg1;
5265 break;
5266 case EQ_EXPR: /* + [c, c] */
5267 in_p = ! in_p, low = high = arg1;
5268 break;
5269 case GT_EXPR: /* - [-, c] */
5270 low = 0, high = arg1;
5271 break;
5272 case GE_EXPR: /* + [c, -] */
5273 in_p = ! in_p, low = arg1, high = 0;
5274 break;
5275 case LT_EXPR: /* - [c, -] */
5276 low = arg1, high = 0;
5277 break;
5278 case LE_EXPR: /* + [-, c] */
5279 in_p = ! in_p, low = 0, high = arg1;
5280 break;
5281 default:
5282 gcc_unreachable ();
5283 }
5284
5285 /* If this is an unsigned comparison, we also know that EXP is
5286 greater than or equal to zero. We base the range tests we make
5287 on that fact, so we record it here so we can parse existing
5288 range tests. We test arg0_type since often the return type
5289 of, e.g. EQ_EXPR, is boolean. */
5290 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5291 {
5292 if (! merge_ranges (&n_in_p, &n_low, &n_high,
5293 in_p, low, high, 1,
5294 build_int_cst (arg0_type, 0),
5295 NULL_TREE))
5296 return NULL_TREE;
5297
5298 in_p = n_in_p, low = n_low, high = n_high;
5299
5300 /* If the high bound is missing, but we have a nonzero low
5301 bound, reverse the range so it goes from zero to the low bound
5302 minus 1. */
5303 if (high == 0 && low && ! integer_zerop (low))
5304 {
5305 in_p = ! in_p;
5306 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5307 build_int_cst (TREE_TYPE (low), 1), 0);
5308 low = build_int_cst (arg0_type, 0);
5309 }
5310 }
5311
5312 *p_low = low;
5313 *p_high = high;
5314 *p_in_p = in_p;
5315 return arg0;
5316
5317 case NEGATE_EXPR:
5318 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5319 low and high are non-NULL, then normalize will DTRT. */
5320 if (!TYPE_UNSIGNED (arg0_type)
5321 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5322 {
5323 if (low == NULL_TREE)
5324 low = TYPE_MIN_VALUE (arg0_type);
5325 if (high == NULL_TREE)
5326 high = TYPE_MAX_VALUE (arg0_type);
5327 }
5328
5329 /* (-x) IN [a,b] -> x in [-b, -a] */
5330 n_low = range_binop (MINUS_EXPR, exp_type,
5331 build_int_cst (exp_type, 0),
5332 0, high, 1);
5333 n_high = range_binop (MINUS_EXPR, exp_type,
5334 build_int_cst (exp_type, 0),
5335 0, low, 0);
5336 if (n_high != 0 && TREE_OVERFLOW (n_high))
5337 return NULL_TREE;
5338 goto normalize;
5339
5340 case BIT_NOT_EXPR:
5341 /* ~ X -> -X - 1 */
5342 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5343 build_int_cst (exp_type, 1));
5344
5345 case PLUS_EXPR:
5346 case MINUS_EXPR:
5347 if (TREE_CODE (arg1) != INTEGER_CST)
5348 return NULL_TREE;
5349
5350 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5351 move a constant to the other side. */
5352 if (!TYPE_UNSIGNED (arg0_type)
5353 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5354 return NULL_TREE;
5355
5356 /* If EXP is signed, any overflow in the computation is undefined,
5357 so we don't worry about it so long as our computations on
5358 the bounds don't overflow. For unsigned, overflow is defined
5359 and this is exactly the right thing. */
5360 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5361 arg0_type, low, 0, arg1, 0);
5362 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5363 arg0_type, high, 1, arg1, 0);
5364 if ((n_low != 0 && TREE_OVERFLOW (n_low))
5365 || (n_high != 0 && TREE_OVERFLOW (n_high)))
5366 return NULL_TREE;
5367
5368 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5369 *strict_overflow_p = true;
5370
5371 normalize:
5372 /* Check for an unsigned range which has wrapped around the maximum
5373 value thus making n_high < n_low, and normalize it. */
5374 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5375 {
5376 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5377 build_int_cst (TREE_TYPE (n_high), 1), 0);
5378 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5379 build_int_cst (TREE_TYPE (n_low), 1), 0);
5380
5381 /* If the range is of the form +/- [ x+1, x ], we won't
5382 be able to normalize it. But then, it represents the
5383 whole range or the empty set, so make it
5384 +/- [ -, - ]. */
5385 if (tree_int_cst_equal (n_low, low)
5386 && tree_int_cst_equal (n_high, high))
5387 low = high = 0;
5388 else
5389 in_p = ! in_p;
5390 }
5391 else
5392 low = n_low, high = n_high;
5393
5394 *p_low = low;
5395 *p_high = high;
5396 *p_in_p = in_p;
5397 return arg0;
5398
5399 CASE_CONVERT:
5400 case NON_LVALUE_EXPR:
5401 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5402 return NULL_TREE;
5403
5404 if (! INTEGRAL_TYPE_P (arg0_type)
5405 || (low != 0 && ! int_fits_type_p (low, arg0_type))
5406 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5407 return NULL_TREE;
5408
5409 n_low = low, n_high = high;
5410
5411 if (n_low != 0)
5412 n_low = fold_convert_loc (loc, arg0_type, n_low);
5413
5414 if (n_high != 0)
5415 n_high = fold_convert_loc (loc, arg0_type, n_high);
5416
5417 /* If we're converting arg0 from an unsigned type, to exp,
5418 a signed type, we will be doing the comparison as unsigned.
5419 The tests above have already verified that LOW and HIGH
5420 are both positive.
5421
5422 So we have to ensure that we will handle large unsigned
5423 values the same way that the current signed bounds treat
5424 negative values. */
5425
5426 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5427 {
5428 tree high_positive;
5429 tree equiv_type;
5430 /* For fixed-point modes, we need to pass the saturating flag
5431 as the 2nd parameter. */
5432 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5433 equiv_type
5434 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5435 TYPE_SATURATING (arg0_type));
5436 else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5437 equiv_type = arg0_type;
5438 else
5439 equiv_type
5440 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5441
5442 /* A range without an upper bound is, naturally, unbounded.
5443 Since convert would have cropped a very large value, use
5444 the max value for the destination type. */
5445 high_positive
5446 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5447 : TYPE_MAX_VALUE (arg0_type);
5448
5449 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5450 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5451 fold_convert_loc (loc, arg0_type,
5452 high_positive),
5453 build_int_cst (arg0_type, 1));
5454
5455 /* If the low bound is specified, "and" the range with the
5456 range for which the original unsigned value will be
5457 positive. */
5458 if (low != 0)
5459 {
5460 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5461 1, fold_convert_loc (loc, arg0_type,
5462 integer_zero_node),
5463 high_positive))
5464 return NULL_TREE;
5465
5466 in_p = (n_in_p == in_p);
5467 }
5468 else
5469 {
5470 /* Otherwise, "or" the range with the range of the input
5471 that will be interpreted as negative. */
5472 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5473 1, fold_convert_loc (loc, arg0_type,
5474 integer_zero_node),
5475 high_positive))
5476 return NULL_TREE;
5477
5478 in_p = (in_p != n_in_p);
5479 }
5480 }
5481
5482 /* Otherwise, if we are converting arg0 from signed type, to exp,
5483 an unsigned type, we will do the comparison as signed. If
5484 high is non-NULL, we punt above if it doesn't fit in the signed
5485 type, so if we get through here, +[-, high] or +[low, high] are
5486 equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5487 +[-, -] or -[-, -] are equivalent too. But if low is specified and
5488 high is not, the +[low, -] range is equivalent to union of
5489 +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5490 -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5491 low being 0, which should be treated as [-, -]. */
5492 else if (TYPE_UNSIGNED (exp_type)
5493 && !TYPE_UNSIGNED (arg0_type)
5494 && low
5495 && !high)
5496 {
5497 if (integer_zerop (low))
5498 n_low = NULL_TREE;
5499 else
5500 {
5501 n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5502 n_low, build_int_cst (arg0_type, -1));
5503 n_low = build_zero_cst (arg0_type);
5504 in_p = !in_p;
5505 }
5506 }
5507
5508 *p_low = n_low;
5509 *p_high = n_high;
5510 *p_in_p = in_p;
5511 return arg0;
5512
5513 default:
5514 return NULL_TREE;
5515 }
5516 }
5517
5518 /* Given EXP, a logical expression, set the range it is testing into
5519 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5520 actually being tested. *PLOW and *PHIGH will be made of the same
5521 type as the returned expression. If EXP is not a comparison, we
5522 will most likely not be returning a useful value and range. Set
5523 *STRICT_OVERFLOW_P to true if the return value is only valid
5524 because signed overflow is undefined; otherwise, do not change
5525 *STRICT_OVERFLOW_P. */
5526
5527 tree
5528 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5529 bool *strict_overflow_p)
5530 {
5531 enum tree_code code;
5532 tree arg0, arg1 = NULL_TREE;
5533 tree exp_type, nexp;
5534 int in_p;
5535 tree low, high;
5536 location_t loc = EXPR_LOCATION (exp);
5537
5538 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5539 and see if we can refine the range. Some of the cases below may not
5540 happen, but it doesn't seem worth worrying about this. We "continue"
5541 the outer loop when we've changed something; otherwise we "break"
5542 the switch, which will "break" the while. */
5543
5544 in_p = 0;
5545 low = high = build_int_cst (TREE_TYPE (exp), 0);
5546
5547 while (1)
5548 {
5549 code = TREE_CODE (exp);
5550 exp_type = TREE_TYPE (exp);
5551 arg0 = NULL_TREE;
5552
5553 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5554 {
5555 if (TREE_OPERAND_LENGTH (exp) > 0)
5556 arg0 = TREE_OPERAND (exp, 0);
5557 if (TREE_CODE_CLASS (code) == tcc_binary
5558 || TREE_CODE_CLASS (code) == tcc_comparison
5559 || (TREE_CODE_CLASS (code) == tcc_expression
5560 && TREE_OPERAND_LENGTH (exp) > 1))
5561 arg1 = TREE_OPERAND (exp, 1);
5562 }
5563 if (arg0 == NULL_TREE)
5564 break;
5565
5566 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5567 &high, &in_p, strict_overflow_p);
5568 if (nexp == NULL_TREE)
5569 break;
5570 exp = nexp;
5571 }
5572
5573 /* If EXP is a constant, we can evaluate whether this is true or false. */
5574 if (TREE_CODE (exp) == INTEGER_CST)
5575 {
5576 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5577 exp, 0, low, 0))
5578 && integer_onep (range_binop (LE_EXPR, integer_type_node,
5579 exp, 1, high, 1)));
5580 low = high = 0;
5581 exp = 0;
5582 }
5583
5584 *pin_p = in_p, *plow = low, *phigh = high;
5585 return exp;
5586 }
5587
5588 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5589 a bitwise check i.e. when
5590 LOW == 0xXX...X00...0
5591 HIGH == 0xXX...X11...1
5592 Return corresponding mask in MASK and stem in VALUE. */
5593
5594 static bool
5595 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5596 tree *value)
5597 {
5598 if (TREE_CODE (low) != INTEGER_CST
5599 || TREE_CODE (high) != INTEGER_CST)
5600 return false;
5601
5602 unsigned prec = TYPE_PRECISION (type);
5603 wide_int lo = wi::to_wide (low, prec);
5604 wide_int hi = wi::to_wide (high, prec);
5605
5606 wide_int end_mask = lo ^ hi;
5607 if ((end_mask & (end_mask + 1)) != 0
5608 || (lo & end_mask) != 0)
5609 return false;
5610
5611 wide_int stem_mask = ~end_mask;
5612 wide_int stem = lo & stem_mask;
5613 if (stem != (hi & stem_mask))
5614 return false;
5615
5616 *mask = wide_int_to_tree (type, stem_mask);
5617 *value = wide_int_to_tree (type, stem);
5618
5619 return true;
5620 }
5621
5622 /* Helper routine for build_range_check and match.pd. Return the type to
5623 perform the check or NULL if it shouldn't be optimized. */
5624
5625 tree
5626 range_check_type (tree etype)
5627 {
5628 /* First make sure that arithmetics in this type is valid, then make sure
5629 that it wraps around. */
5630 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5631 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5632
5633 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5634 {
5635 tree utype, minv, maxv;
5636
5637 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5638 for the type in question, as we rely on this here. */
5639 utype = unsigned_type_for (etype);
5640 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5641 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5642 build_int_cst (TREE_TYPE (maxv), 1), 1);
5643 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5644
5645 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5646 minv, 1, maxv, 1)))
5647 etype = utype;
5648 else
5649 return NULL_TREE;
5650 }
5651 else if (POINTER_TYPE_P (etype)
5652 || TREE_CODE (etype) == OFFSET_TYPE
5653 /* Right now all BITINT_TYPEs satisfy
5654 (unsigned) max + 1 == (unsigned) min, so no need to verify
5655 that like for INTEGER_TYPEs. */
5656 || TREE_CODE (etype) == BITINT_TYPE)
5657 etype = unsigned_type_for (etype);
5658 return etype;
5659 }
5660
5661 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5662 type, TYPE, return an expression to test if EXP is in (or out of, depending
5663 on IN_P) the range. Return 0 if the test couldn't be created. */
5664
5665 tree
5666 build_range_check (location_t loc, tree type, tree exp, int in_p,
5667 tree low, tree high)
5668 {
5669 tree etype = TREE_TYPE (exp), mask, value;
5670
5671 /* Disable this optimization for function pointer expressions
5672 on targets that require function pointer canonicalization. */
5673 if (targetm.have_canonicalize_funcptr_for_compare ()
5674 && POINTER_TYPE_P (etype)
5675 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5676 return NULL_TREE;
5677
5678 if (! in_p)
5679 {
5680 value = build_range_check (loc, type, exp, 1, low, high);
5681 if (value != 0)
5682 return invert_truthvalue_loc (loc, value);
5683
5684 return 0;
5685 }
5686
5687 if (low == 0 && high == 0)
5688 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5689
5690 if (low == 0)
5691 return fold_build2_loc (loc, LE_EXPR, type, exp,
5692 fold_convert_loc (loc, etype, high));
5693
5694 if (high == 0)
5695 return fold_build2_loc (loc, GE_EXPR, type, exp,
5696 fold_convert_loc (loc, etype, low));
5697
5698 if (operand_equal_p (low, high, 0))
5699 return fold_build2_loc (loc, EQ_EXPR, type, exp,
5700 fold_convert_loc (loc, etype, low));
5701
5702 if (TREE_CODE (exp) == BIT_AND_EXPR
5703 && maskable_range_p (low, high, etype, &mask, &value))
5704 return fold_build2_loc (loc, EQ_EXPR, type,
5705 fold_build2_loc (loc, BIT_AND_EXPR, etype,
5706 exp, mask),
5707 value);
5708
5709 if (integer_zerop (low))
5710 {
5711 if (! TYPE_UNSIGNED (etype))
5712 {
5713 etype = unsigned_type_for (etype);
5714 high = fold_convert_loc (loc, etype, high);
5715 exp = fold_convert_loc (loc, etype, exp);
5716 }
5717 return build_range_check (loc, type, exp, 1, 0, high);
5718 }
5719
5720 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5721 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5722 {
5723 int prec = TYPE_PRECISION (etype);
5724
5725 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5726 {
5727 if (TYPE_UNSIGNED (etype))
5728 {
5729 tree signed_etype = signed_type_for (etype);
5730 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5731 etype
5732 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5733 else
5734 etype = signed_etype;
5735 exp = fold_convert_loc (loc, etype, exp);
5736 }
5737 return fold_build2_loc (loc, GT_EXPR, type, exp,
5738 build_int_cst (etype, 0));
5739 }
5740 }
5741
5742 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5743 This requires wrap-around arithmetics for the type of the expression. */
5744 etype = range_check_type (etype);
5745 if (etype == NULL_TREE)
5746 return NULL_TREE;
5747
5748 high = fold_convert_loc (loc, etype, high);
5749 low = fold_convert_loc (loc, etype, low);
5750 exp = fold_convert_loc (loc, etype, exp);
5751
5752 value = const_binop (MINUS_EXPR, high, low);
5753
5754 if (value != 0 && !TREE_OVERFLOW (value))
5755 return build_range_check (loc, type,
5756 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5757 1, build_int_cst (etype, 0), value);
5758
5759 return 0;
5760 }
5761
5762 /* Return the predecessor of VAL in its type, handling the infinite case. */
5763
5764 static tree
5765 range_predecessor (tree val)
5766 {
5767 tree type = TREE_TYPE (val);
5768
5769 if (INTEGRAL_TYPE_P (type)
5770 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5771 return 0;
5772 else
5773 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5774 build_int_cst (TREE_TYPE (val), 1), 0);
5775 }
5776
5777 /* Return the successor of VAL in its type, handling the infinite case. */
5778
5779 static tree
5780 range_successor (tree val)
5781 {
5782 tree type = TREE_TYPE (val);
5783
5784 if (INTEGRAL_TYPE_P (type)
5785 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5786 return 0;
5787 else
5788 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5789 build_int_cst (TREE_TYPE (val), 1), 0);
5790 }
5791
5792 /* Given two ranges, see if we can merge them into one. Return 1 if we
5793 can, 0 if we can't. Set the output range into the specified parameters. */
5794
5795 bool
5796 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5797 tree high0, int in1_p, tree low1, tree high1)
5798 {
5799 bool no_overlap;
5800 int subset;
5801 int temp;
5802 tree tem;
5803 int in_p;
5804 tree low, high;
5805 int lowequal = ((low0 == 0 && low1 == 0)
5806 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5807 low0, 0, low1, 0)));
5808 int highequal = ((high0 == 0 && high1 == 0)
5809 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5810 high0, 1, high1, 1)));
5811
5812 /* Make range 0 be the range that starts first, or ends last if they
5813 start at the same value. Swap them if it isn't. */
5814 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5815 low0, 0, low1, 0))
5816 || (lowequal
5817 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5818 high1, 1, high0, 1))))
5819 {
5820 temp = in0_p, in0_p = in1_p, in1_p = temp;
5821 tem = low0, low0 = low1, low1 = tem;
5822 tem = high0, high0 = high1, high1 = tem;
5823 }
5824
5825 /* If the second range is != high1 where high1 is the type maximum of
5826 the type, try first merging with < high1 range. */
5827 if (low1
5828 && high1
5829 && TREE_CODE (low1) == INTEGER_CST
5830 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5831 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5832 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5833 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5834 && operand_equal_p (low1, high1, 0))
5835 {
5836 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5837 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5838 !in1_p, NULL_TREE, range_predecessor (low1)))
5839 return true;
5840 /* Similarly for the second range != low1 where low1 is the type minimum
5841 of the type, try first merging with > low1 range. */
5842 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5843 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5844 !in1_p, range_successor (low1), NULL_TREE))
5845 return true;
5846 }
5847
5848 /* Now flag two cases, whether the ranges are disjoint or whether the
5849 second range is totally subsumed in the first. Note that the tests
5850 below are simplified by the ones above. */
5851 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5852 high0, 1, low1, 0));
5853 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5854 high1, 1, high0, 1));
5855
5856 /* We now have four cases, depending on whether we are including or
5857 excluding the two ranges. */
5858 if (in0_p && in1_p)
5859 {
5860 /* If they don't overlap, the result is false. If the second range
5861 is a subset it is the result. Otherwise, the range is from the start
5862 of the second to the end of the first. */
5863 if (no_overlap)
5864 in_p = 0, low = high = 0;
5865 else if (subset)
5866 in_p = 1, low = low1, high = high1;
5867 else
5868 in_p = 1, low = low1, high = high0;
5869 }
5870
5871 else if (in0_p && ! in1_p)
5872 {
5873 /* If they don't overlap, the result is the first range. If they are
5874 equal, the result is false. If the second range is a subset of the
5875 first, and the ranges begin at the same place, we go from just after
5876 the end of the second range to the end of the first. If the second
5877 range is not a subset of the first, or if it is a subset and both
5878 ranges end at the same place, the range starts at the start of the
5879 first range and ends just before the second range.
5880 Otherwise, we can't describe this as a single range. */
5881 if (no_overlap)
5882 in_p = 1, low = low0, high = high0;
5883 else if (lowequal && highequal)
5884 in_p = 0, low = high = 0;
5885 else if (subset && lowequal)
5886 {
5887 low = range_successor (high1);
5888 high = high0;
5889 in_p = 1;
5890 if (low == 0)
5891 {
5892 /* We are in the weird situation where high0 > high1 but
5893 high1 has no successor. Punt. */
5894 return 0;
5895 }
5896 }
5897 else if (! subset || highequal)
5898 {
5899 low = low0;
5900 high = range_predecessor (low1);
5901 in_p = 1;
5902 if (high == 0)
5903 {
5904 /* low0 < low1 but low1 has no predecessor. Punt. */
5905 return 0;
5906 }
5907 }
5908 else
5909 return 0;
5910 }
5911
5912 else if (! in0_p && in1_p)
5913 {
5914 /* If they don't overlap, the result is the second range. If the second
5915 is a subset of the first, the result is false. Otherwise,
5916 the range starts just after the first range and ends at the
5917 end of the second. */
5918 if (no_overlap)
5919 in_p = 1, low = low1, high = high1;
5920 else if (subset || highequal)
5921 in_p = 0, low = high = 0;
5922 else
5923 {
5924 low = range_successor (high0);
5925 high = high1;
5926 in_p = 1;
5927 if (low == 0)
5928 {
5929 /* high1 > high0 but high0 has no successor. Punt. */
5930 return 0;
5931 }
5932 }
5933 }
5934
5935 else
5936 {
5937 /* The case where we are excluding both ranges. Here the complex case
5938 is if they don't overlap. In that case, the only time we have a
5939 range is if they are adjacent. If the second is a subset of the
5940 first, the result is the first. Otherwise, the range to exclude
5941 starts at the beginning of the first range and ends at the end of the
5942 second. */
5943 if (no_overlap)
5944 {
5945 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5946 range_successor (high0),
5947 1, low1, 0)))
5948 in_p = 0, low = low0, high = high1;
5949 else
5950 {
5951 /* Canonicalize - [min, x] into - [-, x]. */
5952 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5953 switch (TREE_CODE (TREE_TYPE (low0)))
5954 {
5955 case ENUMERAL_TYPE:
5956 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5957 GET_MODE_BITSIZE
5958 (TYPE_MODE (TREE_TYPE (low0)))))
5959 break;
5960 /* FALLTHROUGH */
5961 case INTEGER_TYPE:
5962 if (tree_int_cst_equal (low0,
5963 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5964 low0 = 0;
5965 break;
5966 case POINTER_TYPE:
5967 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5968 && integer_zerop (low0))
5969 low0 = 0;
5970 break;
5971 default:
5972 break;
5973 }
5974
5975 /* Canonicalize - [x, max] into - [x, -]. */
5976 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5977 switch (TREE_CODE (TREE_TYPE (high1)))
5978 {
5979 case ENUMERAL_TYPE:
5980 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5981 GET_MODE_BITSIZE
5982 (TYPE_MODE (TREE_TYPE (high1)))))
5983 break;
5984 /* FALLTHROUGH */
5985 case INTEGER_TYPE:
5986 if (tree_int_cst_equal (high1,
5987 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5988 high1 = 0;
5989 break;
5990 case POINTER_TYPE:
5991 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5992 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5993 high1, 1,
5994 build_int_cst (TREE_TYPE (high1), 1),
5995 1)))
5996 high1 = 0;
5997 break;
5998 default:
5999 break;
6000 }
6001
6002 /* The ranges might be also adjacent between the maximum and
6003 minimum values of the given type. For
6004 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6005 return + [x + 1, y - 1]. */
6006 if (low0 == 0 && high1 == 0)
6007 {
6008 low = range_successor (high0);
6009 high = range_predecessor (low1);
6010 if (low == 0 || high == 0)
6011 return 0;
6012
6013 in_p = 1;
6014 }
6015 else
6016 return 0;
6017 }
6018 }
6019 else if (subset)
6020 in_p = 0, low = low0, high = high0;
6021 else
6022 in_p = 0, low = low0, high = high1;
6023 }
6024
6025 *pin_p = in_p, *plow = low, *phigh = high;
6026 return 1;
6027 }
6028
6029
6030 /* Subroutine of fold, looking inside expressions of the form
6031 A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6032 are the three operands of the COND_EXPR. This function is
6033 being used also to optimize A op B ? C : A, by reversing the
6034 comparison first.
6035
6036 Return a folded expression whose code is not a COND_EXPR
6037 anymore, or NULL_TREE if no folding opportunity is found. */
6038
6039 static tree
6040 fold_cond_expr_with_comparison (location_t loc, tree type,
6041 enum tree_code comp_code,
6042 tree arg00, tree arg01, tree arg1, tree arg2)
6043 {
6044 tree arg1_type = TREE_TYPE (arg1);
6045 tree tem;
6046
6047 STRIP_NOPS (arg1);
6048 STRIP_NOPS (arg2);
6049
6050 /* If we have A op 0 ? A : -A, consider applying the following
6051 transformations:
6052
6053 A == 0? A : -A same as -A
6054 A != 0? A : -A same as A
6055 A >= 0? A : -A same as abs (A)
6056 A > 0? A : -A same as abs (A)
6057 A <= 0? A : -A same as -abs (A)
6058 A < 0? A : -A same as -abs (A)
6059
6060 None of these transformations work for modes with signed
6061 zeros. If A is +/-0, the first two transformations will
6062 change the sign of the result (from +0 to -0, or vice
6063 versa). The last four will fix the sign of the result,
6064 even though the original expressions could be positive or
6065 negative, depending on the sign of A.
6066
6067 Note that all these transformations are correct if A is
6068 NaN, since the two alternatives (A and -A) are also NaNs. */
6069 if (!HONOR_SIGNED_ZEROS (type)
6070 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6071 ? real_zerop (arg01)
6072 : integer_zerop (arg01))
6073 && ((TREE_CODE (arg2) == NEGATE_EXPR
6074 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6075 /* In the case that A is of the form X-Y, '-A' (arg2) may
6076 have already been folded to Y-X, check for that. */
6077 || (TREE_CODE (arg1) == MINUS_EXPR
6078 && TREE_CODE (arg2) == MINUS_EXPR
6079 && operand_equal_p (TREE_OPERAND (arg1, 0),
6080 TREE_OPERAND (arg2, 1), 0)
6081 && operand_equal_p (TREE_OPERAND (arg1, 1),
6082 TREE_OPERAND (arg2, 0), 0))))
6083 switch (comp_code)
6084 {
6085 case EQ_EXPR:
6086 case UNEQ_EXPR:
6087 tem = fold_convert_loc (loc, arg1_type, arg1);
6088 return fold_convert_loc (loc, type, negate_expr (tem));
6089 case NE_EXPR:
6090 case LTGT_EXPR:
6091 return fold_convert_loc (loc, type, arg1);
6092 case UNGE_EXPR:
6093 case UNGT_EXPR:
6094 if (flag_trapping_math)
6095 break;
6096 /* Fall through. */
6097 case GE_EXPR:
6098 case GT_EXPR:
6099 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6100 break;
6101 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6102 return fold_convert_loc (loc, type, tem);
6103 case UNLE_EXPR:
6104 case UNLT_EXPR:
6105 if (flag_trapping_math)
6106 break;
6107 /* FALLTHRU */
6108 case LE_EXPR:
6109 case LT_EXPR:
6110 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6111 break;
6112 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6113 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6114 {
6115 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6116 is not, invokes UB both in abs and in the negation of it.
6117 So, use ABSU_EXPR instead. */
6118 tree utype = unsigned_type_for (TREE_TYPE (arg1));
6119 tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6120 tem = negate_expr (tem);
6121 return fold_convert_loc (loc, type, tem);
6122 }
6123 else
6124 {
6125 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6126 return negate_expr (fold_convert_loc (loc, type, tem));
6127 }
6128 default:
6129 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6130 break;
6131 }
6132
6133 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6134 A == 0 ? A : 0 is always 0 unless A is -0. Note that
6135 both transformations are correct when A is NaN: A != 0
6136 is then true, and A == 0 is false. */
6137
6138 if (!HONOR_SIGNED_ZEROS (type)
6139 && integer_zerop (arg01) && integer_zerop (arg2))
6140 {
6141 if (comp_code == NE_EXPR)
6142 return fold_convert_loc (loc, type, arg1);
6143 else if (comp_code == EQ_EXPR)
6144 return build_zero_cst (type);
6145 }
6146
6147 /* Try some transformations of A op B ? A : B.
6148
6149 A == B? A : B same as B
6150 A != B? A : B same as A
6151 A >= B? A : B same as max (A, B)
6152 A > B? A : B same as max (B, A)
6153 A <= B? A : B same as min (A, B)
6154 A < B? A : B same as min (B, A)
6155
6156 As above, these transformations don't work in the presence
6157 of signed zeros. For example, if A and B are zeros of
6158 opposite sign, the first two transformations will change
6159 the sign of the result. In the last four, the original
6160 expressions give different results for (A=+0, B=-0) and
6161 (A=-0, B=+0), but the transformed expressions do not.
6162
6163 The first two transformations are correct if either A or B
6164 is a NaN. In the first transformation, the condition will
6165 be false, and B will indeed be chosen. In the case of the
6166 second transformation, the condition A != B will be true,
6167 and A will be chosen.
6168
6169 The conversions to max() and min() are not correct if B is
6170 a number and A is not. The conditions in the original
6171 expressions will be false, so all four give B. The min()
6172 and max() versions would give a NaN instead. */
6173 if (!HONOR_SIGNED_ZEROS (type)
6174 && operand_equal_for_comparison_p (arg01, arg2)
6175 /* Avoid these transformations if the COND_EXPR may be used
6176 as an lvalue in the C++ front-end. PR c++/19199. */
6177 && (in_gimple_form
6178 || VECTOR_TYPE_P (type)
6179 || (! lang_GNU_CXX ()
6180 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6181 || ! maybe_lvalue_p (arg1)
6182 || ! maybe_lvalue_p (arg2)))
6183 {
6184 tree comp_op0 = arg00;
6185 tree comp_op1 = arg01;
6186 tree comp_type = TREE_TYPE (comp_op0);
6187
6188 switch (comp_code)
6189 {
6190 case EQ_EXPR:
6191 return fold_convert_loc (loc, type, arg2);
6192 case NE_EXPR:
6193 return fold_convert_loc (loc, type, arg1);
6194 case LE_EXPR:
6195 case LT_EXPR:
6196 case UNLE_EXPR:
6197 case UNLT_EXPR:
6198 /* In C++ a ?: expression can be an lvalue, so put the
6199 operand which will be used if they are equal first
6200 so that we can convert this back to the
6201 corresponding COND_EXPR. */
6202 if (!HONOR_NANS (arg1))
6203 {
6204 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6205 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6206 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6207 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6208 : fold_build2_loc (loc, MIN_EXPR, comp_type,
6209 comp_op1, comp_op0);
6210 return fold_convert_loc (loc, type, tem);
6211 }
6212 break;
6213 case GE_EXPR:
6214 case GT_EXPR:
6215 case UNGE_EXPR:
6216 case UNGT_EXPR:
6217 if (!HONOR_NANS (arg1))
6218 {
6219 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6220 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6221 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6222 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6223 : fold_build2_loc (loc, MAX_EXPR, comp_type,
6224 comp_op1, comp_op0);
6225 return fold_convert_loc (loc, type, tem);
6226 }
6227 break;
6228 case UNEQ_EXPR:
6229 if (!HONOR_NANS (arg1))
6230 return fold_convert_loc (loc, type, arg2);
6231 break;
6232 case LTGT_EXPR:
6233 if (!HONOR_NANS (arg1))
6234 return fold_convert_loc (loc, type, arg1);
6235 break;
6236 default:
6237 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6238 break;
6239 }
6240 }
6241
6242 return NULL_TREE;
6243 }
6244
6245
6246
6247 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6248 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6249 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6250 false) >= 2)
6251 #endif
6252
6253 /* EXP is some logical combination of boolean tests. See if we can
6254 merge it into some range test. Return the new tree if so. */
6255
6256 static tree
6257 fold_range_test (location_t loc, enum tree_code code, tree type,
6258 tree op0, tree op1)
6259 {
6260 int or_op = (code == TRUTH_ORIF_EXPR
6261 || code == TRUTH_OR_EXPR);
6262 int in0_p, in1_p, in_p;
6263 tree low0, low1, low, high0, high1, high;
6264 bool strict_overflow_p = false;
6265 tree tem, lhs, rhs;
6266 const char * const warnmsg = G_("assuming signed overflow does not occur "
6267 "when simplifying range test");
6268
6269 if (!INTEGRAL_TYPE_P (type))
6270 return 0;
6271
6272 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
6273 /* If op0 is known true or false and this is a short-circuiting
6274 operation we must not merge with op1 since that makes side-effects
6275 unconditional. So special-case this. */
6276 if (!lhs
6277 && ((code == TRUTH_ORIF_EXPR && in0_p)
6278 || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6279 return op0;
6280 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
6281
6282 /* If this is an OR operation, invert both sides; we will invert
6283 again at the end. */
6284 if (or_op)
6285 in0_p = ! in0_p, in1_p = ! in1_p;
6286
6287 /* If both expressions are the same, if we can merge the ranges, and we
6288 can build the range test, return it or it inverted. If one of the
6289 ranges is always true or always false, consider it to be the same
6290 expression as the other. */
6291 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6292 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6293 in1_p, low1, high1)
6294 && (tem = (build_range_check (loc, type,
6295 lhs != 0 ? lhs
6296 : rhs != 0 ? rhs : integer_zero_node,
6297 in_p, low, high))) != 0)
6298 {
6299 if (strict_overflow_p)
6300 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6301 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6302 }
6303
6304 /* On machines where the branch cost is expensive, if this is a
6305 short-circuited branch and the underlying object on both sides
6306 is the same, make a non-short-circuit operation. */
6307 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6308 if (param_logical_op_non_short_circuit != -1)
6309 logical_op_non_short_circuit
6310 = param_logical_op_non_short_circuit;
6311 if (logical_op_non_short_circuit
6312 && !sanitize_coverage_p ()
6313 && lhs != 0 && rhs != 0
6314 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6315 && operand_equal_p (lhs, rhs, 0))
6316 {
6317 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6318 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6319 which cases we can't do this. */
6320 if (simple_operand_p (lhs))
6321 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6322 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6323 type, op0, op1);
6324
6325 else if (!lang_hooks.decls.global_bindings_p ()
6326 && !CONTAINS_PLACEHOLDER_P (lhs))
6327 {
6328 tree common = save_expr (lhs);
6329
6330 if ((lhs = build_range_check (loc, type, common,
6331 or_op ? ! in0_p : in0_p,
6332 low0, high0)) != 0
6333 && (rhs = build_range_check (loc, type, common,
6334 or_op ? ! in1_p : in1_p,
6335 low1, high1)) != 0)
6336 {
6337 if (strict_overflow_p)
6338 fold_overflow_warning (warnmsg,
6339 WARN_STRICT_OVERFLOW_COMPARISON);
6340 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6341 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6342 type, lhs, rhs);
6343 }
6344 }
6345 }
6346
6347 return 0;
6348 }
6349
6350 /* For an expression that has the form
6351 (A && B) || ~B
6352 or
6353 (A || B) && ~B,
6354 we can drop one of the inner expressions and simplify to
6355 A || ~B
6356 or
6357 A && ~B
6358 LOC is the location of the resulting expression. OP is the inner
6359 logical operation; the left-hand side in the examples above, while CMPOP
6360 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6361 removing a condition that guards another, as in
6362 (A != NULL && A->...) || A == NULL
6363 which we must not transform. If RHS_ONLY is true, only eliminate the
6364 right-most operand of the inner logical operation. */
6365
6366 static tree
6367 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6368 bool rhs_only)
6369 {
6370 enum tree_code code = TREE_CODE (cmpop);
6371 enum tree_code truthop_code = TREE_CODE (op);
6372 tree lhs = TREE_OPERAND (op, 0);
6373 tree rhs = TREE_OPERAND (op, 1);
6374 tree orig_lhs = lhs, orig_rhs = rhs;
6375 enum tree_code rhs_code = TREE_CODE (rhs);
6376 enum tree_code lhs_code = TREE_CODE (lhs);
6377 enum tree_code inv_code;
6378
6379 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6380 return NULL_TREE;
6381
6382 if (TREE_CODE_CLASS (code) != tcc_comparison)
6383 return NULL_TREE;
6384
6385 tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6386
6387 if (rhs_code == truthop_code)
6388 {
6389 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6390 if (newrhs != NULL_TREE)
6391 {
6392 rhs = newrhs;
6393 rhs_code = TREE_CODE (rhs);
6394 }
6395 }
6396 if (lhs_code == truthop_code && !rhs_only)
6397 {
6398 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6399 if (newlhs != NULL_TREE)
6400 {
6401 lhs = newlhs;
6402 lhs_code = TREE_CODE (lhs);
6403 }
6404 }
6405
6406 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6407 if (inv_code == rhs_code
6408 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6409 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6410 return lhs;
6411 if (!rhs_only && inv_code == lhs_code
6412 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6413 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6414 return rhs;
6415 if (rhs != orig_rhs || lhs != orig_lhs)
6416 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6417 lhs, rhs);
6418 return NULL_TREE;
6419 }
6420
6421 /* Find ways of folding logical expressions of LHS and RHS:
6422 Try to merge two comparisons to the same innermost item.
6423 Look for range tests like "ch >= '0' && ch <= '9'".
6424 Look for combinations of simple terms on machines with expensive branches
6425 and evaluate the RHS unconditionally.
6426
6427 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6428 function and the one above.
6429
6430 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6431 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6432
6433 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6434 two operands.
6435
6436 We return the simplified tree or 0 if no optimization is possible. */
6437
6438 static tree
6439 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6440 tree lhs, tree rhs)
6441 {
6442 /* If this is the "or" of two comparisons, we can do something if
6443 the comparisons are NE_EXPR. If this is the "and", we can do something
6444 if the comparisons are EQ_EXPR. I.e.,
6445 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6446
6447 WANTED_CODE is this operation code. For single bit fields, we can
6448 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6449 comparison for one-bit fields. */
6450
6451 enum tree_code lcode, rcode;
6452 tree ll_arg, lr_arg, rl_arg, rr_arg;
6453 tree result;
6454
6455 /* Start by getting the comparison codes. Fail if anything is volatile.
6456 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6457 it were surrounded with a NE_EXPR. */
6458
6459 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6460 return 0;
6461
6462 lcode = TREE_CODE (lhs);
6463 rcode = TREE_CODE (rhs);
6464
6465 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6466 {
6467 lhs = build2 (NE_EXPR, truth_type, lhs,
6468 build_int_cst (TREE_TYPE (lhs), 0));
6469 lcode = NE_EXPR;
6470 }
6471
6472 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6473 {
6474 rhs = build2 (NE_EXPR, truth_type, rhs,
6475 build_int_cst (TREE_TYPE (rhs), 0));
6476 rcode = NE_EXPR;
6477 }
6478
6479 if (TREE_CODE_CLASS (lcode) != tcc_comparison
6480 || TREE_CODE_CLASS (rcode) != tcc_comparison)
6481 return 0;
6482
6483 ll_arg = TREE_OPERAND (lhs, 0);
6484 lr_arg = TREE_OPERAND (lhs, 1);
6485 rl_arg = TREE_OPERAND (rhs, 0);
6486 rr_arg = TREE_OPERAND (rhs, 1);
6487
6488 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6489 if (simple_operand_p (ll_arg)
6490 && simple_operand_p (lr_arg))
6491 {
6492 if (operand_equal_p (ll_arg, rl_arg, 0)
6493 && operand_equal_p (lr_arg, rr_arg, 0))
6494 {
6495 result = combine_comparisons (loc, code, lcode, rcode,
6496 truth_type, ll_arg, lr_arg);
6497 if (result)
6498 return result;
6499 }
6500 else if (operand_equal_p (ll_arg, rr_arg, 0)
6501 && operand_equal_p (lr_arg, rl_arg, 0))
6502 {
6503 result = combine_comparisons (loc, code, lcode,
6504 swap_tree_comparison (rcode),
6505 truth_type, ll_arg, lr_arg);
6506 if (result)
6507 return result;
6508 }
6509 }
6510
6511 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6512 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6513
6514 /* If the RHS can be evaluated unconditionally and its operands are
6515 simple, it wins to evaluate the RHS unconditionally on machines
6516 with expensive branches. In this case, this isn't a comparison
6517 that can be merged. */
6518
6519 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6520 false) >= 2
6521 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6522 && simple_operand_p (rl_arg)
6523 && simple_operand_p (rr_arg))
6524 {
6525 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6526 if (code == TRUTH_OR_EXPR
6527 && lcode == NE_EXPR && integer_zerop (lr_arg)
6528 && rcode == NE_EXPR && integer_zerop (rr_arg)
6529 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6530 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6531 return build2_loc (loc, NE_EXPR, truth_type,
6532 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6533 ll_arg, rl_arg),
6534 build_int_cst (TREE_TYPE (ll_arg), 0));
6535
6536 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6537 if (code == TRUTH_AND_EXPR
6538 && lcode == EQ_EXPR && integer_zerop (lr_arg)
6539 && rcode == EQ_EXPR && integer_zerop (rr_arg)
6540 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6541 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6542 return build2_loc (loc, EQ_EXPR, truth_type,
6543 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6544 ll_arg, rl_arg),
6545 build_int_cst (TREE_TYPE (ll_arg), 0));
6546 }
6547
6548 return 0;
6549 }
6550
6551 /* T is an integer expression that is being multiplied, divided, or taken a
6552 modulus (CODE says which and what kind of divide or modulus) by a
6553 constant C. See if we can eliminate that operation by folding it with
6554 other operations already in T. WIDE_TYPE, if non-null, is a type that
6555 should be used for the computation if wider than our type.
6556
6557 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6558 (X * 2) + (Y * 4). We must, however, be assured that either the original
6559 expression would not overflow or that overflow is undefined for the type
6560 in the language in question.
6561
6562 If we return a non-null expression, it is an equivalent form of the
6563 original computation, but need not be in the original type.
6564
6565 We set *STRICT_OVERFLOW_P to true if the return values depends on
6566 signed overflow being undefined. Otherwise we do not change
6567 *STRICT_OVERFLOW_P. */
6568
6569 static tree
6570 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6571 bool *strict_overflow_p)
6572 {
6573 /* To avoid exponential search depth, refuse to allow recursion past
6574 three levels. Beyond that (1) it's highly unlikely that we'll find
6575 something interesting and (2) we've probably processed it before
6576 when we built the inner expression. */
6577
6578 static int depth;
6579 tree ret;
6580
6581 if (depth > 3)
6582 return NULL;
6583
6584 depth++;
6585 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6586 depth--;
6587
6588 return ret;
6589 }
6590
6591 static tree
6592 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6593 bool *strict_overflow_p)
6594 {
6595 tree type = TREE_TYPE (t);
6596 enum tree_code tcode = TREE_CODE (t);
6597 tree ctype = type;
6598 if (wide_type)
6599 {
6600 if (TREE_CODE (type) == BITINT_TYPE
6601 || TREE_CODE (wide_type) == BITINT_TYPE)
6602 {
6603 if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6604 ctype = wide_type;
6605 }
6606 else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6607 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6608 ctype = wide_type;
6609 }
6610 tree t1, t2;
6611 bool same_p = tcode == code;
6612 tree op0 = NULL_TREE, op1 = NULL_TREE;
6613 bool sub_strict_overflow_p;
6614
6615 /* Don't deal with constants of zero here; they confuse the code below. */
6616 if (integer_zerop (c))
6617 return NULL_TREE;
6618
6619 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6620 op0 = TREE_OPERAND (t, 0);
6621
6622 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6623 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6624
6625 /* Note that we need not handle conditional operations here since fold
6626 already handles those cases. So just do arithmetic here. */
6627 switch (tcode)
6628 {
6629 case INTEGER_CST:
6630 /* For a constant, we can always simplify if we are a multiply
6631 or (for divide and modulus) if it is a multiple of our constant. */
6632 if (code == MULT_EXPR
6633 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6634 TYPE_SIGN (type)))
6635 {
6636 tree tem = const_binop (code, fold_convert (ctype, t),
6637 fold_convert (ctype, c));
6638 /* If the multiplication overflowed, we lost information on it.
6639 See PR68142 and PR69845. */
6640 if (TREE_OVERFLOW (tem))
6641 return NULL_TREE;
6642 return tem;
6643 }
6644 break;
6645
6646 CASE_CONVERT: case NON_LVALUE_EXPR:
6647 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6648 break;
6649 /* If op0 is an expression ... */
6650 if ((COMPARISON_CLASS_P (op0)
6651 || UNARY_CLASS_P (op0)
6652 || BINARY_CLASS_P (op0)
6653 || VL_EXP_CLASS_P (op0)
6654 || EXPRESSION_CLASS_P (op0))
6655 /* ... and has wrapping overflow, and its type is smaller
6656 than ctype, then we cannot pass through as widening. */
6657 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6658 && (TYPE_PRECISION (ctype)
6659 > TYPE_PRECISION (TREE_TYPE (op0))))
6660 /* ... or this is a truncation (t is narrower than op0),
6661 then we cannot pass through this narrowing. */
6662 || (TYPE_PRECISION (type)
6663 < TYPE_PRECISION (TREE_TYPE (op0)))
6664 /* ... or signedness changes for division or modulus,
6665 then we cannot pass through this conversion. */
6666 || (code != MULT_EXPR
6667 && (TYPE_UNSIGNED (ctype)
6668 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6669 /* ... or has undefined overflow while the converted to
6670 type has not, we cannot do the operation in the inner type
6671 as that would introduce undefined overflow. */
6672 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6673 && !TYPE_OVERFLOW_UNDEFINED (type))))
6674 break;
6675
6676 /* Pass the constant down and see if we can make a simplification. If
6677 we can, replace this expression with the inner simplification for
6678 possible later conversion to our or some other type. */
6679 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6680 && TREE_CODE (t2) == INTEGER_CST
6681 && !TREE_OVERFLOW (t2)
6682 && (t1 = extract_muldiv (op0, t2, code,
6683 code == MULT_EXPR ? ctype : NULL_TREE,
6684 strict_overflow_p)) != 0)
6685 return t1;
6686 break;
6687
6688 case ABS_EXPR:
6689 /* If widening the type changes it from signed to unsigned, then we
6690 must avoid building ABS_EXPR itself as unsigned. */
6691 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6692 {
6693 tree cstype = (*signed_type_for) (ctype);
6694 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6695 != 0)
6696 {
6697 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6698 return fold_convert (ctype, t1);
6699 }
6700 break;
6701 }
6702 /* If the constant is negative, we cannot simplify this. */
6703 if (tree_int_cst_sgn (c) == -1)
6704 break;
6705 /* FALLTHROUGH */
6706 case NEGATE_EXPR:
6707 /* For division and modulus, type can't be unsigned, as e.g.
6708 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6709 For signed types, even with wrapping overflow, this is fine. */
6710 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6711 break;
6712 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6713 != 0)
6714 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6715 break;
6716
6717 case MIN_EXPR: case MAX_EXPR:
6718 /* If widening the type changes the signedness, then we can't perform
6719 this optimization as that changes the result. */
6720 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6721 break;
6722
6723 /* Punt for multiplication altogether.
6724 MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6725 MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6726 0U, the latter is 2U.
6727 MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6728 MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6729 well defined 0, the latter invokes UB.
6730 MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6731 MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6732 well defined 25, the latter invokes UB. */
6733 if (code == MULT_EXPR)
6734 break;
6735 /* For division/modulo, punt on c being -1 for MAX, as
6736 MAX (INT_MIN, 0) / -1 is not equivalent to
6737 MIN (INT_MIN / -1, 0 / -1), the former is well defined
6738 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6739 MIN (INT_MIN, 0) / -1 already invokes UB, so the
6740 transformation won't make it worse. */
6741 else if (tcode == MAX_EXPR && integer_minus_onep (c))
6742 break;
6743
6744 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6745 sub_strict_overflow_p = false;
6746 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6747 &sub_strict_overflow_p)) != 0
6748 && (t2 = extract_muldiv (op1, c, code, wide_type,
6749 &sub_strict_overflow_p)) != 0)
6750 {
6751 if (tree_int_cst_sgn (c) < 0)
6752 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6753 if (sub_strict_overflow_p)
6754 *strict_overflow_p = true;
6755 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6756 fold_convert (ctype, t2));
6757 }
6758 break;
6759
6760 case LSHIFT_EXPR: case RSHIFT_EXPR:
6761 /* If the second operand is constant, this is a multiplication
6762 or floor division, by a power of two, so we can treat it that
6763 way unless the multiplier or divisor overflows. Signed
6764 left-shift overflow is implementation-defined rather than
6765 undefined in C90, so do not convert signed left shift into
6766 multiplication. */
6767 if (TREE_CODE (op1) == INTEGER_CST
6768 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6769 /* const_binop may not detect overflow correctly,
6770 so check for it explicitly here. */
6771 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6772 wi::to_wide (op1))
6773 && (t1 = fold_convert (ctype,
6774 const_binop (LSHIFT_EXPR, size_one_node,
6775 op1))) != 0
6776 && !TREE_OVERFLOW (t1))
6777 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6778 ? MULT_EXPR : FLOOR_DIV_EXPR,
6779 ctype,
6780 fold_convert (ctype, op0),
6781 t1),
6782 c, code, wide_type, strict_overflow_p);
6783 break;
6784
6785 case PLUS_EXPR: case MINUS_EXPR:
6786 /* See if we can eliminate the operation on both sides. If we can, we
6787 can return a new PLUS or MINUS. If we can't, the only remaining
6788 cases where we can do anything are if the second operand is a
6789 constant. */
6790 sub_strict_overflow_p = false;
6791 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6792 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6793 if (t1 != 0 && t2 != 0
6794 && TYPE_OVERFLOW_WRAPS (ctype)
6795 && (code == MULT_EXPR
6796 /* If not multiplication, we can only do this if both operands
6797 are divisible by c. */
6798 || (multiple_of_p (ctype, op0, c)
6799 && multiple_of_p (ctype, op1, c))))
6800 {
6801 if (sub_strict_overflow_p)
6802 *strict_overflow_p = true;
6803 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6804 fold_convert (ctype, t2));
6805 }
6806
6807 /* If this was a subtraction, negate OP1 and set it to be an addition.
6808 This simplifies the logic below. */
6809 if (tcode == MINUS_EXPR)
6810 {
6811 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6812 /* If OP1 was not easily negatable, the constant may be OP0. */
6813 if (TREE_CODE (op0) == INTEGER_CST)
6814 {
6815 std::swap (op0, op1);
6816 std::swap (t1, t2);
6817 }
6818 }
6819
6820 if (TREE_CODE (op1) != INTEGER_CST)
6821 break;
6822
6823 /* If either OP1 or C are negative, this optimization is not safe for
6824 some of the division and remainder types while for others we need
6825 to change the code. */
6826 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6827 {
6828 if (code == CEIL_DIV_EXPR)
6829 code = FLOOR_DIV_EXPR;
6830 else if (code == FLOOR_DIV_EXPR)
6831 code = CEIL_DIV_EXPR;
6832 else if (code != MULT_EXPR
6833 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6834 break;
6835 }
6836
6837 /* If it's a multiply or a division/modulus operation of a multiple
6838 of our constant, do the operation and verify it doesn't overflow. */
6839 if (code == MULT_EXPR
6840 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6841 TYPE_SIGN (type)))
6842 {
6843 op1 = const_binop (code, fold_convert (ctype, op1),
6844 fold_convert (ctype, c));
6845 /* We allow the constant to overflow with wrapping semantics. */
6846 if (op1 == 0
6847 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6848 break;
6849 }
6850 else
6851 break;
6852
6853 /* If we have an unsigned type, we cannot widen the operation since it
6854 will change the result if the original computation overflowed. */
6855 if (TYPE_UNSIGNED (ctype) && ctype != type)
6856 break;
6857
6858 /* The last case is if we are a multiply. In that case, we can
6859 apply the distributive law to commute the multiply and addition
6860 if the multiplication of the constants doesn't overflow
6861 and overflow is defined. With undefined overflow
6862 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6863 But fold_plusminus_mult_expr would factor back any power-of-two
6864 value so do not distribute in the first place in this case. */
6865 if (code == MULT_EXPR
6866 && TYPE_OVERFLOW_WRAPS (ctype)
6867 && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6868 return fold_build2 (tcode, ctype,
6869 fold_build2 (code, ctype,
6870 fold_convert (ctype, op0),
6871 fold_convert (ctype, c)),
6872 op1);
6873
6874 break;
6875
6876 case MULT_EXPR:
6877 /* We have a special case here if we are doing something like
6878 (C * 8) % 4 since we know that's zero. */
6879 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6880 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6881 /* If the multiplication can overflow we cannot optimize this. */
6882 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6883 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6884 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6885 TYPE_SIGN (type)))
6886 {
6887 *strict_overflow_p = true;
6888 return omit_one_operand (type, integer_zero_node, op0);
6889 }
6890
6891 /* ... fall through ... */
6892
6893 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6894 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6895 /* If we can extract our operation from the LHS, do so and return a
6896 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6897 do something only if the second operand is a constant. */
6898 if (same_p
6899 && TYPE_OVERFLOW_WRAPS (ctype)
6900 && (t1 = extract_muldiv (op0, c, code, wide_type,
6901 strict_overflow_p)) != 0)
6902 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6903 fold_convert (ctype, op1));
6904 else if (tcode == MULT_EXPR && code == MULT_EXPR
6905 && TYPE_OVERFLOW_WRAPS (ctype)
6906 && (t1 = extract_muldiv (op1, c, code, wide_type,
6907 strict_overflow_p)) != 0)
6908 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6909 fold_convert (ctype, t1));
6910 else if (TREE_CODE (op1) != INTEGER_CST)
6911 return 0;
6912
6913 /* If these are the same operation types, we can associate them
6914 assuming no overflow. */
6915 if (tcode == code)
6916 {
6917 bool overflow_p = false;
6918 wi::overflow_type overflow_mul;
6919 signop sign = TYPE_SIGN (ctype);
6920 unsigned prec = TYPE_PRECISION (ctype);
6921 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6922 wi::to_wide (c, prec),
6923 sign, &overflow_mul);
6924 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6925 if (overflow_mul
6926 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6927 overflow_p = true;
6928 if (!overflow_p)
6929 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6930 wide_int_to_tree (ctype, mul));
6931 }
6932
6933 /* If these operations "cancel" each other, we have the main
6934 optimizations of this pass, which occur when either constant is a
6935 multiple of the other, in which case we replace this with either an
6936 operation or CODE or TCODE.
6937
6938 If we have an unsigned type, we cannot do this since it will change
6939 the result if the original computation overflowed. */
6940 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6941 && !TYPE_OVERFLOW_SANITIZED (ctype)
6942 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6943 || (tcode == MULT_EXPR
6944 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6945 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6946 && code != MULT_EXPR)))
6947 {
6948 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6949 TYPE_SIGN (type)))
6950 {
6951 *strict_overflow_p = true;
6952 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6953 fold_convert (ctype,
6954 const_binop (TRUNC_DIV_EXPR,
6955 op1, c)));
6956 }
6957 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6958 TYPE_SIGN (type)))
6959 {
6960 *strict_overflow_p = true;
6961 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6962 fold_convert (ctype,
6963 const_binop (TRUNC_DIV_EXPR,
6964 c, op1)));
6965 }
6966 }
6967 break;
6968
6969 default:
6970 break;
6971 }
6972
6973 return 0;
6974 }
6975
6976 /* Return a node which has the indicated constant VALUE (either 0 or
6977 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6978 and is of the indicated TYPE. */
6979
6980 tree
6981 constant_boolean_node (bool value, tree type)
6982 {
6983 if (type == integer_type_node)
6984 return value ? integer_one_node : integer_zero_node;
6985 else if (type == boolean_type_node)
6986 return value ? boolean_true_node : boolean_false_node;
6987 else if (VECTOR_TYPE_P (type))
6988 return build_vector_from_val (type,
6989 build_int_cst (TREE_TYPE (type),
6990 value ? -1 : 0));
6991 else
6992 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6993 }
6994
6995
6996 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6997 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6998 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6999 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7000 COND is the first argument to CODE; otherwise (as in the example
7001 given here), it is the second argument. TYPE is the type of the
7002 original expression. Return NULL_TREE if no simplification is
7003 possible. */
7004
7005 static tree
7006 fold_binary_op_with_conditional_arg (location_t loc,
7007 enum tree_code code,
7008 tree type, tree op0, tree op1,
7009 tree cond, tree arg, int cond_first_p)
7010 {
7011 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7012 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7013 tree test, true_value, false_value;
7014 tree lhs = NULL_TREE;
7015 tree rhs = NULL_TREE;
7016 enum tree_code cond_code = COND_EXPR;
7017
7018 /* Do not move possibly trapping operations into the conditional as this
7019 pessimizes code and causes gimplification issues when applied late. */
7020 if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7021 ANY_INTEGRAL_TYPE_P (type)
7022 && TYPE_OVERFLOW_TRAPS (type), op1))
7023 return NULL_TREE;
7024
7025 if (TREE_CODE (cond) == COND_EXPR
7026 || TREE_CODE (cond) == VEC_COND_EXPR)
7027 {
7028 test = TREE_OPERAND (cond, 0);
7029 true_value = TREE_OPERAND (cond, 1);
7030 false_value = TREE_OPERAND (cond, 2);
7031 /* If this operand throws an expression, then it does not make
7032 sense to try to perform a logical or arithmetic operation
7033 involving it. */
7034 if (VOID_TYPE_P (TREE_TYPE (true_value)))
7035 lhs = true_value;
7036 if (VOID_TYPE_P (TREE_TYPE (false_value)))
7037 rhs = false_value;
7038 }
7039 else if (!(TREE_CODE (type) != VECTOR_TYPE
7040 && VECTOR_TYPE_P (TREE_TYPE (cond))))
7041 {
7042 tree testtype = TREE_TYPE (cond);
7043 test = cond;
7044 true_value = constant_boolean_node (true, testtype);
7045 false_value = constant_boolean_node (false, testtype);
7046 }
7047 else
7048 /* Detect the case of mixing vector and scalar types - bail out. */
7049 return NULL_TREE;
7050
7051 if (VECTOR_TYPE_P (TREE_TYPE (test)))
7052 cond_code = VEC_COND_EXPR;
7053
7054 /* This transformation is only worthwhile if we don't have to wrap ARG
7055 in a SAVE_EXPR and the operation can be simplified without recursing
7056 on at least one of the branches once its pushed inside the COND_EXPR. */
7057 if (!TREE_CONSTANT (arg)
7058 && (TREE_SIDE_EFFECTS (arg)
7059 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7060 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7061 return NULL_TREE;
7062
7063 arg = fold_convert_loc (loc, arg_type, arg);
7064 if (lhs == 0)
7065 {
7066 true_value = fold_convert_loc (loc, cond_type, true_value);
7067 if (cond_first_p)
7068 lhs = fold_build2_loc (loc, code, type, true_value, arg);
7069 else
7070 lhs = fold_build2_loc (loc, code, type, arg, true_value);
7071 }
7072 if (rhs == 0)
7073 {
7074 false_value = fold_convert_loc (loc, cond_type, false_value);
7075 if (cond_first_p)
7076 rhs = fold_build2_loc (loc, code, type, false_value, arg);
7077 else
7078 rhs = fold_build2_loc (loc, code, type, arg, false_value);
7079 }
7080
7081 /* Check that we have simplified at least one of the branches. */
7082 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7083 return NULL_TREE;
7084
7085 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7086 }
7087
7088
7089 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7090
7091 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7092 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7093 if ARG - ZERO_ARG is the same as X.
7094
7095 If ARG is NULL, check for any value of type TYPE.
7096
7097 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7098 and finite. The problematic cases are when X is zero, and its mode
7099 has signed zeros. In the case of rounding towards -infinity,
7100 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7101 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7102
7103 bool
7104 fold_real_zero_addition_p (const_tree type, const_tree arg,
7105 const_tree zero_arg, int negate)
7106 {
7107 if (!real_zerop (zero_arg))
7108 return false;
7109
7110 /* Don't allow the fold with -fsignaling-nans. */
7111 if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7112 return false;
7113
7114 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7115 if (!HONOR_SIGNED_ZEROS (type))
7116 return true;
7117
7118 /* There is no case that is safe for all rounding modes. */
7119 if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7120 return false;
7121
7122 /* In a vector or complex, we would need to check the sign of all zeros. */
7123 if (TREE_CODE (zero_arg) == VECTOR_CST)
7124 zero_arg = uniform_vector_p (zero_arg);
7125 if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7126 return false;
7127
7128 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7129 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7130 negate = !negate;
7131
7132 /* The mode has signed zeros, and we have to honor their sign.
7133 In this situation, there are only two cases we can return true for.
7134 (i) X - 0 is the same as X with default rounding.
7135 (ii) X + 0 is X when X can't possibly be -0.0. */
7136 return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7137 }
7138
7139 /* Subroutine of match.pd that optimizes comparisons of a division by
7140 a nonzero integer constant against an integer constant, i.e.
7141 X/C1 op C2.
7142
7143 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7144 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7145
7146 enum tree_code
7147 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7148 tree *hi, bool *neg_overflow)
7149 {
7150 tree prod, tmp, type = TREE_TYPE (c1);
7151 signop sign = TYPE_SIGN (type);
7152 wi::overflow_type overflow;
7153
7154 /* We have to do this the hard way to detect unsigned overflow.
7155 prod = int_const_binop (MULT_EXPR, c1, c2); */
7156 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7157 prod = force_fit_type (type, val, -1, overflow);
7158 *neg_overflow = false;
7159
7160 if (sign == UNSIGNED)
7161 {
7162 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7163 *lo = prod;
7164
7165 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7166 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7167 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7168 }
7169 else if (tree_int_cst_sgn (c1) >= 0)
7170 {
7171 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7172 switch (tree_int_cst_sgn (c2))
7173 {
7174 case -1:
7175 *neg_overflow = true;
7176 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7177 *hi = prod;
7178 break;
7179
7180 case 0:
7181 *lo = fold_negate_const (tmp, type);
7182 *hi = tmp;
7183 break;
7184
7185 case 1:
7186 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7187 *lo = prod;
7188 break;
7189
7190 default:
7191 gcc_unreachable ();
7192 }
7193 }
7194 else
7195 {
7196 /* A negative divisor reverses the relational operators. */
7197 code = swap_tree_comparison (code);
7198
7199 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7200 switch (tree_int_cst_sgn (c2))
7201 {
7202 case -1:
7203 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7204 *lo = prod;
7205 break;
7206
7207 case 0:
7208 *hi = fold_negate_const (tmp, type);
7209 *lo = tmp;
7210 break;
7211
7212 case 1:
7213 *neg_overflow = true;
7214 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7215 *hi = prod;
7216 break;
7217
7218 default:
7219 gcc_unreachable ();
7220 }
7221 }
7222
7223 if (code != EQ_EXPR && code != NE_EXPR)
7224 return code;
7225
7226 if (TREE_OVERFLOW (*lo)
7227 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7228 *lo = NULL_TREE;
7229 if (TREE_OVERFLOW (*hi)
7230 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7231 *hi = NULL_TREE;
7232
7233 return code;
7234 }
7235
7236 /* Test whether it is preferable to swap two operands, ARG0 and
7237 ARG1, for example because ARG0 is an integer constant and ARG1
7238 isn't. */
7239
7240 bool
7241 tree_swap_operands_p (const_tree arg0, const_tree arg1)
7242 {
7243 if (CONSTANT_CLASS_P (arg1))
7244 return false;
7245 if (CONSTANT_CLASS_P (arg0))
7246 return true;
7247
7248 STRIP_NOPS (arg0);
7249 STRIP_NOPS (arg1);
7250
7251 if (TREE_CONSTANT (arg1))
7252 return false;
7253 if (TREE_CONSTANT (arg0))
7254 return true;
7255
7256 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7257 for commutative and comparison operators. Ensuring a canonical
7258 form allows the optimizers to find additional redundancies without
7259 having to explicitly check for both orderings. */
7260 if (TREE_CODE (arg0) == SSA_NAME
7261 && TREE_CODE (arg1) == SSA_NAME
7262 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7263 return true;
7264
7265 /* Put SSA_NAMEs last. */
7266 if (TREE_CODE (arg1) == SSA_NAME)
7267 return false;
7268 if (TREE_CODE (arg0) == SSA_NAME)
7269 return true;
7270
7271 /* Put variables last. */
7272 if (DECL_P (arg1))
7273 return false;
7274 if (DECL_P (arg0))
7275 return true;
7276
7277 return false;
7278 }
7279
7280
7281 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7282 means A >= Y && A != MAX, but in this case we know that
7283 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7284
7285 static tree
7286 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7287 {
7288 tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7289
7290 if (TREE_CODE (bound) == LT_EXPR)
7291 a = TREE_OPERAND (bound, 0);
7292 else if (TREE_CODE (bound) == GT_EXPR)
7293 a = TREE_OPERAND (bound, 1);
7294 else
7295 return NULL_TREE;
7296
7297 typea = TREE_TYPE (a);
7298 if (!INTEGRAL_TYPE_P (typea)
7299 && !POINTER_TYPE_P (typea))
7300 return NULL_TREE;
7301
7302 if (TREE_CODE (ineq) == LT_EXPR)
7303 {
7304 a1 = TREE_OPERAND (ineq, 1);
7305 y = TREE_OPERAND (ineq, 0);
7306 }
7307 else if (TREE_CODE (ineq) == GT_EXPR)
7308 {
7309 a1 = TREE_OPERAND (ineq, 0);
7310 y = TREE_OPERAND (ineq, 1);
7311 }
7312 else
7313 return NULL_TREE;
7314
7315 if (TREE_TYPE (a1) != typea)
7316 return NULL_TREE;
7317
7318 if (POINTER_TYPE_P (typea))
7319 {
7320 /* Convert the pointer types into integer before taking the difference. */
7321 tree ta = fold_convert_loc (loc, ssizetype, a);
7322 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7323 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7324 }
7325 else
7326 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7327
7328 if (!diff || !integer_onep (diff))
7329 return NULL_TREE;
7330
7331 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7332 }
7333
7334 /* Fold a sum or difference of at least one multiplication.
7335 Returns the folded tree or NULL if no simplification could be made. */
7336
7337 static tree
7338 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7339 tree arg0, tree arg1)
7340 {
7341 tree arg00, arg01, arg10, arg11;
7342 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7343
7344 /* (A * C) +- (B * C) -> (A+-B) * C.
7345 (A * C) +- A -> A * (C+-1).
7346 We are most concerned about the case where C is a constant,
7347 but other combinations show up during loop reduction. Since
7348 it is not difficult, try all four possibilities. */
7349
7350 if (TREE_CODE (arg0) == MULT_EXPR)
7351 {
7352 arg00 = TREE_OPERAND (arg0, 0);
7353 arg01 = TREE_OPERAND (arg0, 1);
7354 }
7355 else if (TREE_CODE (arg0) == INTEGER_CST)
7356 {
7357 arg00 = build_one_cst (type);
7358 arg01 = arg0;
7359 }
7360 else
7361 {
7362 /* We cannot generate constant 1 for fract. */
7363 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7364 return NULL_TREE;
7365 arg00 = arg0;
7366 arg01 = build_one_cst (type);
7367 }
7368 if (TREE_CODE (arg1) == MULT_EXPR)
7369 {
7370 arg10 = TREE_OPERAND (arg1, 0);
7371 arg11 = TREE_OPERAND (arg1, 1);
7372 }
7373 else if (TREE_CODE (arg1) == INTEGER_CST)
7374 {
7375 arg10 = build_one_cst (type);
7376 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7377 the purpose of this canonicalization. */
7378 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7379 && negate_expr_p (arg1)
7380 && code == PLUS_EXPR)
7381 {
7382 arg11 = negate_expr (arg1);
7383 code = MINUS_EXPR;
7384 }
7385 else
7386 arg11 = arg1;
7387 }
7388 else
7389 {
7390 /* We cannot generate constant 1 for fract. */
7391 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7392 return NULL_TREE;
7393 arg10 = arg1;
7394 arg11 = build_one_cst (type);
7395 }
7396 same = NULL_TREE;
7397
7398 /* Prefer factoring a common non-constant. */
7399 if (operand_equal_p (arg00, arg10, 0))
7400 same = arg00, alt0 = arg01, alt1 = arg11;
7401 else if (operand_equal_p (arg01, arg11, 0))
7402 same = arg01, alt0 = arg00, alt1 = arg10;
7403 else if (operand_equal_p (arg00, arg11, 0))
7404 same = arg00, alt0 = arg01, alt1 = arg10;
7405 else if (operand_equal_p (arg01, arg10, 0))
7406 same = arg01, alt0 = arg00, alt1 = arg11;
7407
7408 /* No identical multiplicands; see if we can find a common
7409 power-of-two factor in non-power-of-two multiplies. This
7410 can help in multi-dimensional array access. */
7411 else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7412 {
7413 HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7414 HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7415 HOST_WIDE_INT tmp;
7416 bool swap = false;
7417 tree maybe_same;
7418
7419 /* Move min of absolute values to int11. */
7420 if (absu_hwi (int01) < absu_hwi (int11))
7421 {
7422 tmp = int01, int01 = int11, int11 = tmp;
7423 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7424 maybe_same = arg01;
7425 swap = true;
7426 }
7427 else
7428 maybe_same = arg11;
7429
7430 const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7431 if (factor > 1
7432 && pow2p_hwi (factor)
7433 && (int01 & (factor - 1)) == 0
7434 /* The remainder should not be a constant, otherwise we
7435 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7436 increased the number of multiplications necessary. */
7437 && TREE_CODE (arg10) != INTEGER_CST)
7438 {
7439 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7440 build_int_cst (TREE_TYPE (arg00),
7441 int01 / int11));
7442 alt1 = arg10;
7443 same = maybe_same;
7444 if (swap)
7445 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7446 }
7447 }
7448
7449 if (!same)
7450 return NULL_TREE;
7451
7452 if (! ANY_INTEGRAL_TYPE_P (type)
7453 || TYPE_OVERFLOW_WRAPS (type)
7454 /* We are neither factoring zero nor minus one. */
7455 || TREE_CODE (same) == INTEGER_CST)
7456 return fold_build2_loc (loc, MULT_EXPR, type,
7457 fold_build2_loc (loc, code, type,
7458 fold_convert_loc (loc, type, alt0),
7459 fold_convert_loc (loc, type, alt1)),
7460 fold_convert_loc (loc, type, same));
7461
7462 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7463 same may be minus one and thus the multiplication may overflow. Perform
7464 the sum operation in an unsigned type. */
7465 tree utype = unsigned_type_for (type);
7466 tree tem = fold_build2_loc (loc, code, utype,
7467 fold_convert_loc (loc, utype, alt0),
7468 fold_convert_loc (loc, utype, alt1));
7469 /* If the sum evaluated to a constant that is not -INF the multiplication
7470 cannot overflow. */
7471 if (TREE_CODE (tem) == INTEGER_CST
7472 && (wi::to_wide (tem)
7473 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7474 return fold_build2_loc (loc, MULT_EXPR, type,
7475 fold_convert (type, tem), same);
7476
7477 /* Do not resort to unsigned multiplication because
7478 we lose the no-overflow property of the expression. */
7479 return NULL_TREE;
7480 }
7481
7482
7483 /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7484 into the buffer PTR of length LEN bytes.
7485 Return the number of bytes placed in the buffer, or zero
7486 upon failure. */
7487
7488 int
7489 native_encode_wide_int (tree type, const wide_int_ref &val,
7490 unsigned char *ptr, int len, int off)
7491 {
7492 int total_bytes;
7493 if (TREE_CODE (type) == BITINT_TYPE)
7494 {
7495 struct bitint_info info;
7496 bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7497 gcc_assert (ok);
7498 scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7499 if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7500 {
7501 total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7502 /* More work is needed when adding _BitInt support to PDP endian
7503 if limb is smaller than word, or if _BitInt limb ordering doesn't
7504 match target endianity here. */
7505 gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7506 && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7507 || (GET_MODE_SIZE (limb_mode)
7508 >= UNITS_PER_WORD)));
7509 }
7510 else
7511 total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7512 }
7513 else
7514 total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7515 int byte, offset, word, words;
7516 unsigned char value;
7517
7518 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7519 return 0;
7520 if (off == -1)
7521 off = 0;
7522
7523 if (ptr == NULL)
7524 /* Dry run. */
7525 return MIN (len, total_bytes - off);
7526
7527 words = total_bytes / UNITS_PER_WORD;
7528
7529 for (byte = 0; byte < total_bytes; byte++)
7530 {
7531 int bitpos = byte * BITS_PER_UNIT;
7532 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7533 number of bytes. */
7534 value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7535
7536 if (total_bytes > UNITS_PER_WORD)
7537 {
7538 word = byte / UNITS_PER_WORD;
7539 if (WORDS_BIG_ENDIAN)
7540 word = (words - 1) - word;
7541 offset = word * UNITS_PER_WORD;
7542 if (BYTES_BIG_ENDIAN)
7543 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7544 else
7545 offset += byte % UNITS_PER_WORD;
7546 }
7547 else
7548 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7549 if (offset >= off && offset - off < len)
7550 ptr[offset - off] = value;
7551 }
7552 return MIN (len, total_bytes - off);
7553 }
7554
7555 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7556 specified by EXPR into the buffer PTR of length LEN bytes.
7557 Return the number of bytes placed in the buffer, or zero
7558 upon failure. */
7559
7560 static int
7561 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7562 {
7563 return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7564 ptr, len, off);
7565 }
7566
7567
7568 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7569 specified by EXPR into the buffer PTR of length LEN bytes.
7570 Return the number of bytes placed in the buffer, or zero
7571 upon failure. */
7572
7573 static int
7574 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7575 {
7576 tree type = TREE_TYPE (expr);
7577 scalar_mode mode = SCALAR_TYPE_MODE (type);
7578 int total_bytes = GET_MODE_SIZE (mode);
7579 FIXED_VALUE_TYPE value;
7580 tree i_value, i_type;
7581
7582 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7583 return 0;
7584
7585 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7586
7587 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7588 return 0;
7589
7590 value = TREE_FIXED_CST (expr);
7591 i_value = double_int_to_tree (i_type, value.data);
7592
7593 return native_encode_int (i_value, ptr, len, off);
7594 }
7595
7596
7597 /* Subroutine of native_encode_expr. Encode the REAL_CST
7598 specified by EXPR into the buffer PTR of length LEN bytes.
7599 Return the number of bytes placed in the buffer, or zero
7600 upon failure. */
7601
7602 int
7603 native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7604 unsigned char *ptr, int len, int off)
7605 {
7606 int total_bytes = GET_MODE_SIZE (mode);
7607 int byte, offset, word, words, bitpos;
7608 unsigned char value;
7609
7610 /* There are always 32 bits in each long, no matter the size of
7611 the hosts long. We handle floating point representations with
7612 up to 192 bits. */
7613 long tmp[6];
7614
7615 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7616 return 0;
7617 if (off == -1)
7618 off = 0;
7619
7620 if (ptr == NULL)
7621 /* Dry run. */
7622 return MIN (len, total_bytes - off);
7623
7624 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7625
7626 real_to_target (tmp, val, mode);
7627
7628 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7629 bitpos += BITS_PER_UNIT)
7630 {
7631 byte = (bitpos / BITS_PER_UNIT) & 3;
7632 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7633
7634 if (UNITS_PER_WORD < 4)
7635 {
7636 word = byte / UNITS_PER_WORD;
7637 if (WORDS_BIG_ENDIAN)
7638 word = (words - 1) - word;
7639 offset = word * UNITS_PER_WORD;
7640 if (BYTES_BIG_ENDIAN)
7641 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7642 else
7643 offset += byte % UNITS_PER_WORD;
7644 }
7645 else
7646 {
7647 offset = byte;
7648 if (BYTES_BIG_ENDIAN)
7649 {
7650 /* Reverse bytes within each long, or within the entire float
7651 if it's smaller than a long (for HFmode). */
7652 offset = MIN (3, total_bytes - 1) - offset;
7653 gcc_assert (offset >= 0);
7654 }
7655 }
7656 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7657 if (offset >= off
7658 && offset - off < len)
7659 ptr[offset - off] = value;
7660 }
7661 return MIN (len, total_bytes - off);
7662 }
7663
7664 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7665 specified by EXPR into the buffer PTR of length LEN bytes.
7666 Return the number of bytes placed in the buffer, or zero
7667 upon failure. */
7668
7669 static int
7670 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7671 {
7672 int rsize, isize;
7673 tree part;
7674
7675 part = TREE_REALPART (expr);
7676 rsize = native_encode_expr (part, ptr, len, off);
7677 if (off == -1 && rsize == 0)
7678 return 0;
7679 part = TREE_IMAGPART (expr);
7680 if (off != -1)
7681 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7682 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7683 len - rsize, off);
7684 if (off == -1 && isize != rsize)
7685 return 0;
7686 return rsize + isize;
7687 }
7688
7689 /* Like native_encode_vector, but only encode the first COUNT elements.
7690 The other arguments are as for native_encode_vector. */
7691
7692 static int
7693 native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7694 int off, unsigned HOST_WIDE_INT count)
7695 {
7696 tree itype = TREE_TYPE (TREE_TYPE (expr));
7697 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7698 && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7699 {
7700 /* This is the only case in which elements can be smaller than a byte.
7701 Element 0 is always in the lsb of the containing byte. */
7702 unsigned int elt_bits = TYPE_PRECISION (itype);
7703 int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7704 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7705 return 0;
7706
7707 if (off == -1)
7708 off = 0;
7709
7710 /* Zero the buffer and then set bits later where necessary. */
7711 int extract_bytes = MIN (len, total_bytes - off);
7712 if (ptr)
7713 memset (ptr, 0, extract_bytes);
7714
7715 unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7716 unsigned int first_elt = off * elts_per_byte;
7717 unsigned int extract_elts = extract_bytes * elts_per_byte;
7718 unsigned int elt_mask = (1 << elt_bits) - 1;
7719 for (unsigned int i = 0; i < extract_elts; ++i)
7720 {
7721 tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7722 if (TREE_CODE (elt) != INTEGER_CST)
7723 return 0;
7724
7725 if (ptr && integer_nonzerop (elt))
7726 {
7727 unsigned int bit = i * elt_bits;
7728 ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7729 }
7730 }
7731 return extract_bytes;
7732 }
7733
7734 int offset = 0;
7735 int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7736 for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7737 {
7738 if (off >= size)
7739 {
7740 off -= size;
7741 continue;
7742 }
7743 tree elem = VECTOR_CST_ELT (expr, i);
7744 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7745 len - offset, off);
7746 if ((off == -1 && res != size) || res == 0)
7747 return 0;
7748 offset += res;
7749 if (offset >= len)
7750 return (off == -1 && i < count - 1) ? 0 : offset;
7751 if (off != -1)
7752 off = 0;
7753 }
7754 return offset;
7755 }
7756
7757 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7758 specified by EXPR into the buffer PTR of length LEN bytes.
7759 Return the number of bytes placed in the buffer, or zero
7760 upon failure. */
7761
7762 static int
7763 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7764 {
7765 unsigned HOST_WIDE_INT count;
7766 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7767 return 0;
7768 return native_encode_vector_part (expr, ptr, len, off, count);
7769 }
7770
7771
7772 /* Subroutine of native_encode_expr. Encode the STRING_CST
7773 specified by EXPR into the buffer PTR of length LEN bytes.
7774 Return the number of bytes placed in the buffer, or zero
7775 upon failure. */
7776
7777 static int
7778 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7779 {
7780 tree type = TREE_TYPE (expr);
7781
7782 /* Wide-char strings are encoded in target byte-order so native
7783 encoding them is trivial. */
7784 if (BITS_PER_UNIT != CHAR_BIT
7785 || TREE_CODE (type) != ARRAY_TYPE
7786 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7787 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7788 return 0;
7789
7790 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7791 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7792 return 0;
7793 if (off == -1)
7794 off = 0;
7795 len = MIN (total_bytes - off, len);
7796 if (ptr == NULL)
7797 /* Dry run. */;
7798 else
7799 {
7800 int written = 0;
7801 if (off < TREE_STRING_LENGTH (expr))
7802 {
7803 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7804 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7805 }
7806 memset (ptr + written, 0, len - written);
7807 }
7808 return len;
7809 }
7810
7811 /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7812 specified by EXPR into the buffer PTR of length LEN bytes.
7813 Return the number of bytes placed in the buffer, or zero
7814 upon failure. */
7815
7816 static int
7817 native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7818 {
7819 /* We are only concerned with zero-initialization constructors here. That's
7820 all we expect to see in GIMPLE, so that's all native_encode_expr should
7821 deal with. For more general handling of constructors, there is
7822 native_encode_initializer. */
7823 if (CONSTRUCTOR_NELTS (expr))
7824 return 0;
7825
7826 /* Wide-char strings are encoded in target byte-order so native
7827 encoding them is trivial. */
7828 if (BITS_PER_UNIT != CHAR_BIT
7829 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7830 return 0;
7831
7832 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7833 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7834 return 0;
7835 if (off == -1)
7836 off = 0;
7837 len = MIN (total_bytes - off, len);
7838 if (ptr == NULL)
7839 /* Dry run. */;
7840 else
7841 memset (ptr, 0, len);
7842 return len;
7843 }
7844
7845 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7846 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7847 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7848 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7849 sufficient to encode the entire EXPR, or if OFF is out of bounds.
7850 Otherwise, start at byte offset OFF and encode at most LEN bytes.
7851 Return the number of bytes placed in the buffer, or zero upon failure. */
7852
7853 int
7854 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7855 {
7856 /* We don't support starting at negative offset and -1 is special. */
7857 if (off < -1)
7858 return 0;
7859
7860 switch (TREE_CODE (expr))
7861 {
7862 case INTEGER_CST:
7863 return native_encode_int (expr, ptr, len, off);
7864
7865 case REAL_CST:
7866 return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7867 TREE_REAL_CST_PTR (expr), ptr, len, off);
7868
7869 case FIXED_CST:
7870 return native_encode_fixed (expr, ptr, len, off);
7871
7872 case COMPLEX_CST:
7873 return native_encode_complex (expr, ptr, len, off);
7874
7875 case VECTOR_CST:
7876 return native_encode_vector (expr, ptr, len, off);
7877
7878 case STRING_CST:
7879 return native_encode_string (expr, ptr, len, off);
7880
7881 case CONSTRUCTOR:
7882 return native_encode_constructor (expr, ptr, len, off);
7883
7884 default:
7885 return 0;
7886 }
7887 }
7888
7889 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7890 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7891 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7892 machine modes, we can't just use build_nonstandard_integer_type. */
7893
7894 tree
7895 find_bitfield_repr_type (int fieldsize, int len)
7896 {
7897 machine_mode mode;
7898 for (int pass = 0; pass < 2; pass++)
7899 {
7900 enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7901 FOR_EACH_MODE_IN_CLASS (mode, mclass)
7902 if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7903 && known_eq (GET_MODE_PRECISION (mode),
7904 GET_MODE_BITSIZE (mode))
7905 && known_le (GET_MODE_SIZE (mode), len))
7906 {
7907 tree ret = lang_hooks.types.type_for_mode (mode, 1);
7908 if (ret && TYPE_MODE (ret) == mode)
7909 return ret;
7910 }
7911 }
7912
7913 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7914 if (int_n_enabled_p[i]
7915 && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7916 && int_n_trees[i].unsigned_type)
7917 {
7918 tree ret = int_n_trees[i].unsigned_type;
7919 mode = TYPE_MODE (ret);
7920 if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7921 && known_eq (GET_MODE_PRECISION (mode),
7922 GET_MODE_BITSIZE (mode))
7923 && known_le (GET_MODE_SIZE (mode), len))
7924 return ret;
7925 }
7926
7927 return NULL_TREE;
7928 }
7929
7930 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7931 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7932 to be non-NULL and OFF zero), then in addition to filling the
7933 bytes pointed by PTR with the value also clear any bits pointed
7934 by MASK that are known to be initialized, keep them as is for
7935 e.g. uninitialized padding bits or uninitialized fields. */
7936
7937 int
7938 native_encode_initializer (tree init, unsigned char *ptr, int len,
7939 int off, unsigned char *mask)
7940 {
7941 int r;
7942
7943 /* We don't support starting at negative offset and -1 is special. */
7944 if (off < -1 || init == NULL_TREE)
7945 return 0;
7946
7947 gcc_assert (mask == NULL || (off == 0 && ptr));
7948
7949 STRIP_NOPS (init);
7950 switch (TREE_CODE (init))
7951 {
7952 case VIEW_CONVERT_EXPR:
7953 case NON_LVALUE_EXPR:
7954 return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7955 mask);
7956 default:
7957 r = native_encode_expr (init, ptr, len, off);
7958 if (mask)
7959 memset (mask, 0, r);
7960 return r;
7961 case CONSTRUCTOR:
7962 tree type = TREE_TYPE (init);
7963 HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7964 if (total_bytes < 0)
7965 return 0;
7966 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7967 return 0;
7968 int o = off == -1 ? 0 : off;
7969 if (TREE_CODE (type) == ARRAY_TYPE)
7970 {
7971 tree min_index;
7972 unsigned HOST_WIDE_INT cnt;
7973 HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7974 constructor_elt *ce;
7975
7976 if (!TYPE_DOMAIN (type)
7977 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7978 return 0;
7979
7980 fieldsize = int_size_in_bytes (TREE_TYPE (type));
7981 if (fieldsize <= 0)
7982 return 0;
7983
7984 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7985 if (ptr)
7986 memset (ptr, '\0', MIN (total_bytes - off, len));
7987
7988 for (cnt = 0; ; cnt++)
7989 {
7990 tree val = NULL_TREE, index = NULL_TREE;
7991 HOST_WIDE_INT pos = curpos, count = 0;
7992 bool full = false;
7993 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7994 {
7995 val = ce->value;
7996 index = ce->index;
7997 }
7998 else if (mask == NULL
7999 || CONSTRUCTOR_NO_CLEARING (init)
8000 || curpos >= total_bytes)
8001 break;
8002 else
8003 pos = total_bytes;
8004
8005 if (index && TREE_CODE (index) == RANGE_EXPR)
8006 {
8007 if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8008 || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8009 return 0;
8010 offset_int wpos
8011 = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8012 - wi::to_offset (min_index),
8013 TYPE_PRECISION (sizetype));
8014 wpos *= fieldsize;
8015 if (!wi::fits_shwi_p (pos))
8016 return 0;
8017 pos = wpos.to_shwi ();
8018 offset_int wcount
8019 = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8020 - wi::to_offset (TREE_OPERAND (index, 0)),
8021 TYPE_PRECISION (sizetype));
8022 if (!wi::fits_shwi_p (wcount))
8023 return 0;
8024 count = wcount.to_shwi ();
8025 }
8026 else if (index)
8027 {
8028 if (TREE_CODE (index) != INTEGER_CST)
8029 return 0;
8030 offset_int wpos
8031 = wi::sext (wi::to_offset (index)
8032 - wi::to_offset (min_index),
8033 TYPE_PRECISION (sizetype));
8034 wpos *= fieldsize;
8035 if (!wi::fits_shwi_p (wpos))
8036 return 0;
8037 pos = wpos.to_shwi ();
8038 }
8039
8040 if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8041 {
8042 if (valueinit == -1)
8043 {
8044 tree zero = build_zero_cst (TREE_TYPE (type));
8045 r = native_encode_initializer (zero, ptr + curpos,
8046 fieldsize, 0,
8047 mask + curpos);
8048 if (TREE_CODE (zero) == CONSTRUCTOR)
8049 ggc_free (zero);
8050 if (!r)
8051 return 0;
8052 valueinit = curpos;
8053 curpos += fieldsize;
8054 }
8055 while (curpos != pos)
8056 {
8057 memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8058 memcpy (mask + curpos, mask + valueinit, fieldsize);
8059 curpos += fieldsize;
8060 }
8061 }
8062
8063 curpos = pos;
8064 if (val && TREE_CODE (val) == RAW_DATA_CST)
8065 {
8066 if (count)
8067 return 0;
8068 if (off == -1
8069 || (curpos >= off
8070 && (curpos + RAW_DATA_LENGTH (val)
8071 <= (HOST_WIDE_INT) off + len)))
8072 {
8073 if (ptr)
8074 memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8075 RAW_DATA_LENGTH (val));
8076 if (mask)
8077 memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8078 }
8079 else if (curpos + RAW_DATA_LENGTH (val) > off
8080 && curpos < (HOST_WIDE_INT) off + len)
8081 {
8082 /* Partial overlap. */
8083 unsigned char *p = NULL;
8084 int no = 0;
8085 int l;
8086 gcc_assert (mask == NULL);
8087 if (curpos >= off)
8088 {
8089 if (ptr)
8090 p = ptr + curpos - off;
8091 l = MIN ((HOST_WIDE_INT) off + len - curpos,
8092 RAW_DATA_LENGTH (val));
8093 }
8094 else
8095 {
8096 p = ptr;
8097 no = off - curpos;
8098 l = len;
8099 }
8100 if (p)
8101 memcpy (p, RAW_DATA_POINTER (val) + no, l);
8102 }
8103 curpos += RAW_DATA_LENGTH (val);
8104 val = NULL_TREE;
8105 }
8106 if (val)
8107 do
8108 {
8109 if (off == -1
8110 || (curpos >= off
8111 && (curpos + fieldsize
8112 <= (HOST_WIDE_INT) off + len)))
8113 {
8114 if (full)
8115 {
8116 if (ptr)
8117 memcpy (ptr + (curpos - o), ptr + (pos - o),
8118 fieldsize);
8119 if (mask)
8120 memcpy (mask + curpos, mask + pos, fieldsize);
8121 }
8122 else if (!native_encode_initializer (val,
8123 ptr
8124 ? ptr + curpos - o
8125 : NULL,
8126 fieldsize,
8127 off == -1 ? -1
8128 : 0,
8129 mask
8130 ? mask + curpos
8131 : NULL))
8132 return 0;
8133 else
8134 {
8135 full = true;
8136 pos = curpos;
8137 }
8138 }
8139 else if (curpos + fieldsize > off
8140 && curpos < (HOST_WIDE_INT) off + len)
8141 {
8142 /* Partial overlap. */
8143 unsigned char *p = NULL;
8144 int no = 0;
8145 int l;
8146 gcc_assert (mask == NULL);
8147 if (curpos >= off)
8148 {
8149 if (ptr)
8150 p = ptr + curpos - off;
8151 l = MIN ((HOST_WIDE_INT) off + len - curpos,
8152 fieldsize);
8153 }
8154 else
8155 {
8156 p = ptr;
8157 no = off - curpos;
8158 l = len;
8159 }
8160 if (!native_encode_initializer (val, p, l, no, NULL))
8161 return 0;
8162 }
8163 curpos += fieldsize;
8164 }
8165 while (count-- != 0);
8166 }
8167 return MIN (total_bytes - off, len);
8168 }
8169 else if (TREE_CODE (type) == RECORD_TYPE
8170 || TREE_CODE (type) == UNION_TYPE)
8171 {
8172 unsigned HOST_WIDE_INT cnt;
8173 constructor_elt *ce;
8174 tree fld_base = TYPE_FIELDS (type);
8175 tree to_free = NULL_TREE;
8176
8177 gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8178 if (ptr != NULL)
8179 memset (ptr, '\0', MIN (total_bytes - o, len));
8180 for (cnt = 0; ; cnt++)
8181 {
8182 tree val = NULL_TREE, field = NULL_TREE;
8183 HOST_WIDE_INT pos = 0, fieldsize;
8184 unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8185
8186 if (to_free)
8187 {
8188 ggc_free (to_free);
8189 to_free = NULL_TREE;
8190 }
8191
8192 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8193 {
8194 val = ce->value;
8195 field = ce->index;
8196 if (field == NULL_TREE)
8197 return 0;
8198
8199 pos = int_byte_position (field);
8200 if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8201 continue;
8202 }
8203 else if (mask == NULL
8204 || CONSTRUCTOR_NO_CLEARING (init))
8205 break;
8206 else
8207 pos = total_bytes;
8208
8209 if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8210 {
8211 tree fld;
8212 for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8213 {
8214 if (TREE_CODE (fld) != FIELD_DECL)
8215 continue;
8216 if (fld == field)
8217 break;
8218 if (DECL_PADDING_P (fld))
8219 continue;
8220 if (DECL_SIZE_UNIT (fld) == NULL_TREE
8221 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8222 return 0;
8223 if (integer_zerop (DECL_SIZE_UNIT (fld)))
8224 continue;
8225 break;
8226 }
8227 if (fld == NULL_TREE)
8228 {
8229 if (ce == NULL)
8230 break;
8231 return 0;
8232 }
8233 fld_base = DECL_CHAIN (fld);
8234 if (fld != field)
8235 {
8236 cnt--;
8237 field = fld;
8238 pos = int_byte_position (field);
8239 val = build_zero_cst (TREE_TYPE (fld));
8240 if (TREE_CODE (val) == CONSTRUCTOR)
8241 to_free = val;
8242 }
8243 }
8244
8245 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8246 && TYPE_DOMAIN (TREE_TYPE (field))
8247 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8248 {
8249 if (mask || off != -1)
8250 return 0;
8251 if (val == NULL_TREE)
8252 continue;
8253 if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8254 return 0;
8255 fieldsize = int_size_in_bytes (TREE_TYPE (val));
8256 if (fieldsize < 0
8257 || (int) fieldsize != fieldsize
8258 || (pos + fieldsize) > INT_MAX)
8259 return 0;
8260 if (pos + fieldsize > total_bytes)
8261 {
8262 if (ptr != NULL && total_bytes < len)
8263 memset (ptr + total_bytes, '\0',
8264 MIN (pos + fieldsize, len) - total_bytes);
8265 total_bytes = pos + fieldsize;
8266 }
8267 }
8268 else
8269 {
8270 if (DECL_SIZE_UNIT (field) == NULL_TREE
8271 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8272 return 0;
8273 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8274 }
8275 if (fieldsize == 0)
8276 continue;
8277
8278 /* Prepare to deal with integral bit-fields and filter out other
8279 bit-fields that do not start and end on a byte boundary. */
8280 if (DECL_BIT_FIELD (field))
8281 {
8282 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8283 return 0;
8284 bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8285 if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8286 {
8287 bpos %= BITS_PER_UNIT;
8288 fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8289 epos = fieldsize % BITS_PER_UNIT;
8290 fieldsize += BITS_PER_UNIT - 1;
8291 fieldsize /= BITS_PER_UNIT;
8292 }
8293 else if (bpos % BITS_PER_UNIT
8294 || DECL_SIZE (field) == NULL_TREE
8295 || !tree_fits_shwi_p (DECL_SIZE (field))
8296 || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8297 return 0;
8298 }
8299
8300 if (off != -1 && pos + fieldsize <= off)
8301 continue;
8302
8303 if (val == NULL_TREE)
8304 continue;
8305
8306 if (DECL_BIT_FIELD (field)
8307 && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8308 {
8309 /* FIXME: Handle PDP endian. */
8310 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8311 return 0;
8312
8313 if (TREE_CODE (val) == NON_LVALUE_EXPR)
8314 val = TREE_OPERAND (val, 0);
8315 if (TREE_CODE (val) != INTEGER_CST)
8316 return 0;
8317
8318 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8319 tree repr_type = NULL_TREE;
8320 HOST_WIDE_INT rpos = 0;
8321 if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8322 {
8323 rpos = int_byte_position (repr);
8324 repr_type = TREE_TYPE (repr);
8325 }
8326 else
8327 {
8328 repr_type = find_bitfield_repr_type (fieldsize, len);
8329 if (repr_type == NULL_TREE)
8330 return 0;
8331 HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8332 gcc_assert (repr_size > 0 && repr_size <= len);
8333 if (pos + repr_size <= o + len)
8334 rpos = pos;
8335 else
8336 {
8337 rpos = o + len - repr_size;
8338 gcc_assert (rpos <= pos);
8339 }
8340 }
8341
8342 if (rpos > pos)
8343 return 0;
8344 wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8345 int diff = (TYPE_PRECISION (repr_type)
8346 - TYPE_PRECISION (TREE_TYPE (field)));
8347 HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8348 if (!BYTES_BIG_ENDIAN)
8349 w = wi::lshift (w, bitoff);
8350 else
8351 w = wi::lshift (w, diff - bitoff);
8352 val = wide_int_to_tree (repr_type, w);
8353
8354 unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8355 / BITS_PER_UNIT + 1];
8356 int l = native_encode_int (val, buf, sizeof buf, 0);
8357 if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8358 return 0;
8359
8360 if (ptr == NULL)
8361 continue;
8362
8363 /* If the bitfield does not start at byte boundary, handle
8364 the partial byte at the start. */
8365 if (bpos
8366 && (off == -1 || (pos >= off && len >= 1)))
8367 {
8368 if (!BYTES_BIG_ENDIAN)
8369 {
8370 int msk = (1 << bpos) - 1;
8371 buf[pos - rpos] &= ~msk;
8372 buf[pos - rpos] |= ptr[pos - o] & msk;
8373 if (mask)
8374 {
8375 if (fieldsize > 1 || epos == 0)
8376 mask[pos] &= msk;
8377 else
8378 mask[pos] &= (msk | ~((1 << epos) - 1));
8379 }
8380 }
8381 else
8382 {
8383 int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8384 buf[pos - rpos] &= msk;
8385 buf[pos - rpos] |= ptr[pos - o] & ~msk;
8386 if (mask)
8387 {
8388 if (fieldsize > 1 || epos == 0)
8389 mask[pos] &= ~msk;
8390 else
8391 mask[pos] &= (~msk
8392 | ((1 << (BITS_PER_UNIT - epos))
8393 - 1));
8394 }
8395 }
8396 }
8397 /* If the bitfield does not end at byte boundary, handle
8398 the partial byte at the end. */
8399 if (epos
8400 && (off == -1
8401 || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8402 {
8403 if (!BYTES_BIG_ENDIAN)
8404 {
8405 int msk = (1 << epos) - 1;
8406 buf[pos - rpos + fieldsize - 1] &= msk;
8407 buf[pos - rpos + fieldsize - 1]
8408 |= ptr[pos + fieldsize - 1 - o] & ~msk;
8409 if (mask && (fieldsize > 1 || bpos == 0))
8410 mask[pos + fieldsize - 1] &= ~msk;
8411 }
8412 else
8413 {
8414 int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8415 buf[pos - rpos + fieldsize - 1] &= ~msk;
8416 buf[pos - rpos + fieldsize - 1]
8417 |= ptr[pos + fieldsize - 1 - o] & msk;
8418 if (mask && (fieldsize > 1 || bpos == 0))
8419 mask[pos + fieldsize - 1] &= msk;
8420 }
8421 }
8422 if (off == -1
8423 || (pos >= off
8424 && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8425 {
8426 memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8427 if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8428 memset (mask + pos + (bpos != 0), 0,
8429 fieldsize - (bpos != 0) - (epos != 0));
8430 }
8431 else
8432 {
8433 /* Partial overlap. */
8434 HOST_WIDE_INT fsz = fieldsize;
8435 gcc_assert (mask == NULL);
8436 if (pos < off)
8437 {
8438 fsz -= (off - pos);
8439 pos = off;
8440 }
8441 if (pos + fsz > (HOST_WIDE_INT) off + len)
8442 fsz = (HOST_WIDE_INT) off + len - pos;
8443 memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8444 }
8445 continue;
8446 }
8447
8448 if (off == -1
8449 || (pos >= off
8450 && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8451 {
8452 int fldsize = fieldsize;
8453 if (off == -1)
8454 {
8455 tree fld = DECL_CHAIN (field);
8456 while (fld)
8457 {
8458 if (TREE_CODE (fld) == FIELD_DECL)
8459 break;
8460 fld = DECL_CHAIN (fld);
8461 }
8462 if (fld == NULL_TREE)
8463 fldsize = len - pos;
8464 }
8465 r = native_encode_initializer (val, ptr ? ptr + pos - o
8466 : NULL,
8467 fldsize,
8468 off == -1 ? -1 : 0,
8469 mask ? mask + pos : NULL);
8470 if (!r)
8471 return 0;
8472 if (off == -1
8473 && fldsize != fieldsize
8474 && r > fieldsize
8475 && pos + r > total_bytes)
8476 total_bytes = pos + r;
8477 }
8478 else
8479 {
8480 /* Partial overlap. */
8481 unsigned char *p = NULL;
8482 int no = 0;
8483 int l;
8484 gcc_assert (mask == NULL);
8485 if (pos >= off)
8486 {
8487 if (ptr)
8488 p = ptr + pos - off;
8489 l = MIN ((HOST_WIDE_INT) off + len - pos,
8490 fieldsize);
8491 }
8492 else
8493 {
8494 p = ptr;
8495 no = off - pos;
8496 l = len;
8497 }
8498 if (!native_encode_initializer (val, p, l, no, NULL))
8499 return 0;
8500 }
8501 }
8502 return MIN (total_bytes - off, len);
8503 }
8504 return 0;
8505 }
8506 }
8507
8508
8509 /* Subroutine of native_interpret_expr. Interpret the contents of
8510 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8511 If the buffer cannot be interpreted, return NULL_TREE. */
8512
8513 static tree
8514 native_interpret_int (tree type, const unsigned char *ptr, int len)
8515 {
8516 int total_bytes;
8517 if (TREE_CODE (type) == BITINT_TYPE)
8518 {
8519 struct bitint_info info;
8520 bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8521 gcc_assert (ok);
8522 scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8523 if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8524 {
8525 total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8526 /* More work is needed when adding _BitInt support to PDP endian
8527 if limb is smaller than word, or if _BitInt limb ordering doesn't
8528 match target endianity here. */
8529 gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8530 && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8531 || (GET_MODE_SIZE (limb_mode)
8532 >= UNITS_PER_WORD)));
8533 }
8534 else
8535 total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8536 }
8537 else
8538 total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8539
8540 if (total_bytes > len)
8541 return NULL_TREE;
8542
8543 wide_int result = wi::from_buffer (ptr, total_bytes);
8544
8545 return wide_int_to_tree (type, result);
8546 }
8547
8548
8549 /* Subroutine of native_interpret_expr. Interpret the contents of
8550 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8551 If the buffer cannot be interpreted, return NULL_TREE. */
8552
8553 static tree
8554 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8555 {
8556 scalar_mode mode = SCALAR_TYPE_MODE (type);
8557 int total_bytes = GET_MODE_SIZE (mode);
8558 double_int result;
8559 FIXED_VALUE_TYPE fixed_value;
8560
8561 if (total_bytes > len
8562 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8563 return NULL_TREE;
8564
8565 result = double_int::from_buffer (ptr, total_bytes);
8566 fixed_value = fixed_from_double_int (result, mode);
8567
8568 return build_fixed (type, fixed_value);
8569 }
8570
8571
8572 /* Subroutine of native_interpret_expr. Interpret the contents of
8573 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8574 If the buffer cannot be interpreted, return NULL_TREE. */
8575
8576 tree
8577 native_interpret_real (tree type, const unsigned char *ptr, int len)
8578 {
8579 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8580 int total_bytes = GET_MODE_SIZE (mode);
8581 unsigned char value;
8582 /* There are always 32 bits in each long, no matter the size of
8583 the hosts long. We handle floating point representations with
8584 up to 192 bits. */
8585 REAL_VALUE_TYPE r;
8586 long tmp[6];
8587
8588 if (total_bytes > len || total_bytes > 24)
8589 return NULL_TREE;
8590 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8591
8592 memset (tmp, 0, sizeof (tmp));
8593 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8594 bitpos += BITS_PER_UNIT)
8595 {
8596 /* Both OFFSET and BYTE index within a long;
8597 bitpos indexes the whole float. */
8598 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8599 if (UNITS_PER_WORD < 4)
8600 {
8601 int word = byte / UNITS_PER_WORD;
8602 if (WORDS_BIG_ENDIAN)
8603 word = (words - 1) - word;
8604 offset = word * UNITS_PER_WORD;
8605 if (BYTES_BIG_ENDIAN)
8606 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8607 else
8608 offset += byte % UNITS_PER_WORD;
8609 }
8610 else
8611 {
8612 offset = byte;
8613 if (BYTES_BIG_ENDIAN)
8614 {
8615 /* Reverse bytes within each long, or within the entire float
8616 if it's smaller than a long (for HFmode). */
8617 offset = MIN (3, total_bytes - 1) - offset;
8618 gcc_assert (offset >= 0);
8619 }
8620 }
8621 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8622
8623 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8624 }
8625
8626 real_from_target (&r, tmp, mode);
8627 return build_real (type, r);
8628 }
8629
8630
8631 /* Subroutine of native_interpret_expr. Interpret the contents of
8632 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8633 If the buffer cannot be interpreted, return NULL_TREE. */
8634
8635 static tree
8636 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8637 {
8638 tree etype, rpart, ipart;
8639 int size;
8640
8641 etype = TREE_TYPE (type);
8642 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8643 if (size * 2 > len)
8644 return NULL_TREE;
8645 rpart = native_interpret_expr (etype, ptr, size);
8646 if (!rpart)
8647 return NULL_TREE;
8648 ipart = native_interpret_expr (etype, ptr+size, size);
8649 if (!ipart)
8650 return NULL_TREE;
8651 return build_complex (type, rpart, ipart);
8652 }
8653
8654 /* Read a vector of type TYPE from the target memory image given by BYTES,
8655 which contains LEN bytes. The vector is known to be encodable using
8656 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8657
8658 Return the vector on success, otherwise return null. */
8659
8660 static tree
8661 native_interpret_vector_part (tree type, const unsigned char *bytes,
8662 unsigned int len, unsigned int npatterns,
8663 unsigned int nelts_per_pattern)
8664 {
8665 tree elt_type = TREE_TYPE (type);
8666 if (VECTOR_BOOLEAN_TYPE_P (type)
8667 && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8668 {
8669 /* This is the only case in which elements can be smaller than a byte.
8670 Element 0 is always in the lsb of the containing byte. */
8671 unsigned int elt_bits = TYPE_PRECISION (elt_type);
8672 if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8673 return NULL_TREE;
8674
8675 tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8676 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8677 {
8678 unsigned int bit_index = i * elt_bits;
8679 unsigned int byte_index = bit_index / BITS_PER_UNIT;
8680 unsigned int lsb = bit_index % BITS_PER_UNIT;
8681 builder.quick_push (bytes[byte_index] & (1 << lsb)
8682 ? build_all_ones_cst (elt_type)
8683 : build_zero_cst (elt_type));
8684 }
8685 return builder.build ();
8686 }
8687
8688 unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8689 if (elt_bytes * npatterns * nelts_per_pattern > len)
8690 return NULL_TREE;
8691
8692 tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8693 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8694 {
8695 tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8696 if (!elt)
8697 return NULL_TREE;
8698 builder.quick_push (elt);
8699 bytes += elt_bytes;
8700 }
8701 return builder.build ();
8702 }
8703
8704 /* Subroutine of native_interpret_expr. Interpret the contents of
8705 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8706 If the buffer cannot be interpreted, return NULL_TREE. */
8707
8708 static tree
8709 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8710 {
8711 unsigned HOST_WIDE_INT size;
8712
8713 if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8714 || size > len)
8715 return NULL_TREE;
8716
8717 unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8718 return native_interpret_vector_part (type, ptr, len, count, 1);
8719 }
8720
8721
8722 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8723 the buffer PTR of length LEN as a constant of type TYPE. For
8724 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8725 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8726 return NULL_TREE. */
8727
8728 tree
8729 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8730 {
8731 switch (TREE_CODE (type))
8732 {
8733 case INTEGER_TYPE:
8734 case ENUMERAL_TYPE:
8735 case BOOLEAN_TYPE:
8736 case POINTER_TYPE:
8737 case REFERENCE_TYPE:
8738 case OFFSET_TYPE:
8739 case BITINT_TYPE:
8740 return native_interpret_int (type, ptr, len);
8741
8742 case REAL_TYPE:
8743 if (tree ret = native_interpret_real (type, ptr, len))
8744 {
8745 /* For floating point values in composite modes, punt if this
8746 folding doesn't preserve bit representation. As the mode doesn't
8747 have fixed precision while GCC pretends it does, there could be
8748 valid values that GCC can't really represent accurately.
8749 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8750 bit combinationations which GCC doesn't preserve. */
8751 unsigned char buf[24 * 2];
8752 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8753 int total_bytes = GET_MODE_SIZE (mode);
8754 memcpy (buf + 24, ptr, total_bytes);
8755 clear_type_padding_in_mask (type, buf + 24);
8756 if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8757 || memcmp (buf + 24, buf, total_bytes) != 0)
8758 return NULL_TREE;
8759 return ret;
8760 }
8761 return NULL_TREE;
8762
8763 case FIXED_POINT_TYPE:
8764 return native_interpret_fixed (type, ptr, len);
8765
8766 case COMPLEX_TYPE:
8767 return native_interpret_complex (type, ptr, len);
8768
8769 case VECTOR_TYPE:
8770 return native_interpret_vector (type, ptr, len);
8771
8772 default:
8773 return NULL_TREE;
8774 }
8775 }
8776
8777 /* Returns true if we can interpret the contents of a native encoding
8778 as TYPE. */
8779
8780 bool
8781 can_native_interpret_type_p (tree type)
8782 {
8783 switch (TREE_CODE (type))
8784 {
8785 case INTEGER_TYPE:
8786 case ENUMERAL_TYPE:
8787 case BOOLEAN_TYPE:
8788 case POINTER_TYPE:
8789 case REFERENCE_TYPE:
8790 case FIXED_POINT_TYPE:
8791 case REAL_TYPE:
8792 case COMPLEX_TYPE:
8793 case VECTOR_TYPE:
8794 case OFFSET_TYPE:
8795 return true;
8796 default:
8797 return false;
8798 }
8799 }
8800
8801 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8802 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8803
8804 tree
8805 native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8806 int len)
8807 {
8808 vec<constructor_elt, va_gc> *elts = NULL;
8809 if (TREE_CODE (type) == ARRAY_TYPE)
8810 {
8811 HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8812 if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8813 return NULL_TREE;
8814
8815 HOST_WIDE_INT cnt = 0;
8816 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8817 {
8818 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8819 return NULL_TREE;
8820 cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8821 }
8822 if (eltsz == 0)
8823 cnt = 0;
8824 HOST_WIDE_INT pos = 0;
8825 for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8826 {
8827 tree v = NULL_TREE;
8828 if (pos >= len || pos + eltsz > len)
8829 return NULL_TREE;
8830 if (can_native_interpret_type_p (TREE_TYPE (type)))
8831 {
8832 v = native_interpret_expr (TREE_TYPE (type),
8833 ptr + off + pos, eltsz);
8834 if (v == NULL_TREE)
8835 return NULL_TREE;
8836 }
8837 else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8838 || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8839 v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8840 eltsz);
8841 if (v == NULL_TREE)
8842 return NULL_TREE;
8843 CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8844 }
8845 return build_constructor (type, elts);
8846 }
8847 if (TREE_CODE (type) != RECORD_TYPE)
8848 return NULL_TREE;
8849 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8850 {
8851 if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8852 || is_empty_type (TREE_TYPE (field)))
8853 continue;
8854 tree fld = field;
8855 HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8856 int diff = 0;
8857 tree v = NULL_TREE;
8858 if (DECL_BIT_FIELD (field))
8859 {
8860 fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8861 if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8862 {
8863 poly_int64 bitoffset;
8864 poly_uint64 field_offset, fld_offset;
8865 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8866 && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8867 bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8868 else
8869 bitoffset = 0;
8870 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8871 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8872 diff = (TYPE_PRECISION (TREE_TYPE (fld))
8873 - TYPE_PRECISION (TREE_TYPE (field)));
8874 if (!bitoffset.is_constant (&bitoff)
8875 || bitoff < 0
8876 || bitoff > diff)
8877 return NULL_TREE;
8878 }
8879 else
8880 {
8881 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8882 return NULL_TREE;
8883 int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8884 int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8885 bpos %= BITS_PER_UNIT;
8886 fieldsize += bpos;
8887 fieldsize += BITS_PER_UNIT - 1;
8888 fieldsize /= BITS_PER_UNIT;
8889 tree repr_type = find_bitfield_repr_type (fieldsize, len);
8890 if (repr_type == NULL_TREE)
8891 return NULL_TREE;
8892 sz = int_size_in_bytes (repr_type);
8893 if (sz < 0 || sz > len)
8894 return NULL_TREE;
8895 pos = int_byte_position (field);
8896 if (pos < 0 || pos > len || pos + fieldsize > len)
8897 return NULL_TREE;
8898 HOST_WIDE_INT rpos;
8899 if (pos + sz <= len)
8900 rpos = pos;
8901 else
8902 {
8903 rpos = len - sz;
8904 gcc_assert (rpos <= pos);
8905 }
8906 bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8907 pos = rpos;
8908 diff = (TYPE_PRECISION (repr_type)
8909 - TYPE_PRECISION (TREE_TYPE (field)));
8910 v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8911 if (v == NULL_TREE)
8912 return NULL_TREE;
8913 fld = NULL_TREE;
8914 }
8915 }
8916
8917 if (fld)
8918 {
8919 sz = int_size_in_bytes (TREE_TYPE (fld));
8920 if (sz < 0 || sz > len)
8921 return NULL_TREE;
8922 tree byte_pos = byte_position (fld);
8923 if (!tree_fits_shwi_p (byte_pos))
8924 return NULL_TREE;
8925 pos = tree_to_shwi (byte_pos);
8926 if (pos < 0 || pos > len || pos + sz > len)
8927 return NULL_TREE;
8928 }
8929 if (fld == NULL_TREE)
8930 /* Already handled above. */;
8931 else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8932 {
8933 v = native_interpret_expr (TREE_TYPE (fld),
8934 ptr + off + pos, sz);
8935 if (v == NULL_TREE)
8936 return NULL_TREE;
8937 }
8938 else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8939 || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8940 v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8941 if (v == NULL_TREE)
8942 return NULL_TREE;
8943 if (fld != field)
8944 {
8945 if (TREE_CODE (v) != INTEGER_CST)
8946 return NULL_TREE;
8947
8948 /* FIXME: Figure out how to handle PDP endian bitfields. */
8949 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8950 return NULL_TREE;
8951 if (!BYTES_BIG_ENDIAN)
8952 v = wide_int_to_tree (TREE_TYPE (field),
8953 wi::lrshift (wi::to_wide (v), bitoff));
8954 else
8955 v = wide_int_to_tree (TREE_TYPE (field),
8956 wi::lrshift (wi::to_wide (v),
8957 diff - bitoff));
8958 }
8959 CONSTRUCTOR_APPEND_ELT (elts, field, v);
8960 }
8961 return build_constructor (type, elts);
8962 }
8963
8964 /* Routines for manipulation of native_encode_expr encoded data if the encoded
8965 or extracted constant positions and/or sizes aren't byte aligned. */
8966
8967 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8968 bits between adjacent elements. AMNT should be within
8969 [0, BITS_PER_UNIT).
8970 Example, AMNT = 2:
8971 00011111|11100000 << 2 = 01111111|10000000
8972 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8973
8974 void
8975 shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8976 unsigned int amnt)
8977 {
8978 if (amnt == 0)
8979 return;
8980
8981 unsigned char carry_over = 0U;
8982 unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8983 unsigned char clear_mask = (~0U) << amnt;
8984
8985 for (unsigned int i = 0; i < sz; i++)
8986 {
8987 unsigned prev_carry_over = carry_over;
8988 carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8989
8990 ptr[i] <<= amnt;
8991 if (i != 0)
8992 {
8993 ptr[i] &= clear_mask;
8994 ptr[i] |= prev_carry_over;
8995 }
8996 }
8997 }
8998
8999 /* Like shift_bytes_in_array_left but for big-endian.
9000 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9001 bits between adjacent elements. AMNT should be within
9002 [0, BITS_PER_UNIT).
9003 Example, AMNT = 2:
9004 00011111|11100000 >> 2 = 00000111|11111000
9005 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9006
9007 void
9008 shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9009 unsigned int amnt)
9010 {
9011 if (amnt == 0)
9012 return;
9013
9014 unsigned char carry_over = 0U;
9015 unsigned char carry_mask = ~(~0U << amnt);
9016
9017 for (unsigned int i = 0; i < sz; i++)
9018 {
9019 unsigned prev_carry_over = carry_over;
9020 carry_over = ptr[i] & carry_mask;
9021
9022 carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9023 ptr[i] >>= amnt;
9024 ptr[i] |= prev_carry_over;
9025 }
9026 }
9027
9028 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9029 directly on the VECTOR_CST encoding, in a way that works for variable-
9030 length vectors. Return the resulting VECTOR_CST on success or null
9031 on failure. */
9032
9033 static tree
9034 fold_view_convert_vector_encoding (tree type, tree expr)
9035 {
9036 tree expr_type = TREE_TYPE (expr);
9037 poly_uint64 type_bits, expr_bits;
9038 if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9039 || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9040 return NULL_TREE;
9041
9042 poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9043 poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9044 unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9045 unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9046
9047 /* We can only preserve the semantics of a stepped pattern if the new
9048 vector element is an integer of the same size. */
9049 if (VECTOR_CST_STEPPED_P (expr)
9050 && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9051 return NULL_TREE;
9052
9053 /* The number of bits needed to encode one element from every pattern
9054 of the original vector. */
9055 unsigned int expr_sequence_bits
9056 = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9057
9058 /* The number of bits needed to encode one element from every pattern
9059 of the result. */
9060 unsigned int type_sequence_bits
9061 = least_common_multiple (expr_sequence_bits, type_elt_bits);
9062
9063 /* Don't try to read more bytes than are available, which can happen
9064 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9065 The general VIEW_CONVERT handling can cope with that case, so there's
9066 no point complicating things here. */
9067 unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9068 unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9069 BITS_PER_UNIT);
9070 unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9071 if (known_gt (buffer_bits, expr_bits))
9072 return NULL_TREE;
9073
9074 /* Get enough bytes of EXPR to form the new encoding. */
9075 auto_vec<unsigned char, 128> buffer (buffer_bytes);
9076 buffer.quick_grow (buffer_bytes);
9077 if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9078 buffer_bits / expr_elt_bits)
9079 != (int) buffer_bytes)
9080 return NULL_TREE;
9081
9082 /* Reencode the bytes as TYPE. */
9083 unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9084 return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9085 type_npatterns, nelts_per_pattern);
9086 }
9087
9088 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9089 TYPE at compile-time. If we're unable to perform the conversion
9090 return NULL_TREE. */
9091
9092 static tree
9093 fold_view_convert_expr (tree type, tree expr)
9094 {
9095 unsigned char buffer[128];
9096 unsigned char *buf;
9097 int len;
9098 HOST_WIDE_INT l;
9099
9100 /* Check that the host and target are sane. */
9101 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9102 return NULL_TREE;
9103
9104 if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9105 if (tree res = fold_view_convert_vector_encoding (type, expr))
9106 return res;
9107
9108 l = int_size_in_bytes (type);
9109 if (l > (int) sizeof (buffer)
9110 && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9111 {
9112 buf = XALLOCAVEC (unsigned char, l);
9113 len = l;
9114 }
9115 else
9116 {
9117 buf = buffer;
9118 len = sizeof (buffer);
9119 }
9120 len = native_encode_expr (expr, buf, len);
9121 if (len == 0)
9122 return NULL_TREE;
9123
9124 return native_interpret_expr (type, buf, len);
9125 }
9126
9127 /* Build an expression for the address of T. Folds away INDIRECT_REF
9128 to avoid confusing the gimplify process. */
9129
9130 tree
9131 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9132 {
9133 /* The size of the object is not relevant when talking about its address. */
9134 if (TREE_CODE (t) == WITH_SIZE_EXPR)
9135 t = TREE_OPERAND (t, 0);
9136
9137 if (INDIRECT_REF_P (t))
9138 {
9139 t = TREE_OPERAND (t, 0);
9140
9141 if (TREE_TYPE (t) != ptrtype)
9142 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9143 }
9144 else if (TREE_CODE (t) == MEM_REF
9145 && integer_zerop (TREE_OPERAND (t, 1)))
9146 {
9147 t = TREE_OPERAND (t, 0);
9148
9149 if (TREE_TYPE (t) != ptrtype)
9150 t = fold_convert_loc (loc, ptrtype, t);
9151 }
9152 else if (TREE_CODE (t) == MEM_REF
9153 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9154 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9155 TREE_OPERAND (t, 0),
9156 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9157 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9158 {
9159 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9160
9161 if (TREE_TYPE (t) != ptrtype)
9162 t = fold_convert_loc (loc, ptrtype, t);
9163 }
9164 else
9165 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9166
9167 return t;
9168 }
9169
9170 /* Build an expression for the address of T. */
9171
9172 tree
9173 build_fold_addr_expr_loc (location_t loc, tree t)
9174 {
9175 tree ptrtype = build_pointer_type (TREE_TYPE (t));
9176
9177 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9178 }
9179
9180 /* Fold a unary expression of code CODE and type TYPE with operand
9181 OP0. Return the folded expression if folding is successful.
9182 Otherwise, return NULL_TREE. */
9183
9184 tree
9185 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9186 {
9187 tree tem;
9188 tree arg0;
9189 enum tree_code_class kind = TREE_CODE_CLASS (code);
9190
9191 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9192 && TREE_CODE_LENGTH (code) == 1);
9193
9194 arg0 = op0;
9195 if (arg0)
9196 {
9197 if (CONVERT_EXPR_CODE_P (code)
9198 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9199 {
9200 /* Don't use STRIP_NOPS, because signedness of argument type
9201 matters. */
9202 STRIP_SIGN_NOPS (arg0);
9203 }
9204 else
9205 {
9206 /* Strip any conversions that don't change the mode. This
9207 is safe for every expression, except for a comparison
9208 expression because its signedness is derived from its
9209 operands.
9210
9211 Note that this is done as an internal manipulation within
9212 the constant folder, in order to find the simplest
9213 representation of the arguments so that their form can be
9214 studied. In any cases, the appropriate type conversions
9215 should be put back in the tree that will get out of the
9216 constant folder. */
9217 STRIP_NOPS (arg0);
9218 }
9219
9220 if (CONSTANT_CLASS_P (arg0))
9221 {
9222 tree tem = const_unop (code, type, arg0);
9223 if (tem)
9224 {
9225 if (TREE_TYPE (tem) != type)
9226 tem = fold_convert_loc (loc, type, tem);
9227 return tem;
9228 }
9229 }
9230 }
9231
9232 tem = generic_simplify (loc, code, type, op0);
9233 if (tem)
9234 return tem;
9235
9236 if (TREE_CODE_CLASS (code) == tcc_unary)
9237 {
9238 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9239 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9240 fold_build1_loc (loc, code, type,
9241 fold_convert_loc (loc, TREE_TYPE (op0),
9242 TREE_OPERAND (arg0, 1))));
9243 else if (TREE_CODE (arg0) == COND_EXPR)
9244 {
9245 tree arg01 = TREE_OPERAND (arg0, 1);
9246 tree arg02 = TREE_OPERAND (arg0, 2);
9247 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9248 arg01 = fold_build1_loc (loc, code, type,
9249 fold_convert_loc (loc,
9250 TREE_TYPE (op0), arg01));
9251 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9252 arg02 = fold_build1_loc (loc, code, type,
9253 fold_convert_loc (loc,
9254 TREE_TYPE (op0), arg02));
9255 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9256 arg01, arg02);
9257
9258 /* If this was a conversion, and all we did was to move into
9259 inside the COND_EXPR, bring it back out. But leave it if
9260 it is a conversion from integer to integer and the
9261 result precision is no wider than a word since such a
9262 conversion is cheap and may be optimized away by combine,
9263 while it couldn't if it were outside the COND_EXPR. Then return
9264 so we don't get into an infinite recursion loop taking the
9265 conversion out and then back in. */
9266
9267 if ((CONVERT_EXPR_CODE_P (code)
9268 || code == NON_LVALUE_EXPR)
9269 && TREE_CODE (tem) == COND_EXPR
9270 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9271 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9272 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9273 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9274 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9275 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9276 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9277 && (INTEGRAL_TYPE_P
9278 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9279 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9280 || flag_syntax_only))
9281 tem = build1_loc (loc, code, type,
9282 build3 (COND_EXPR,
9283 TREE_TYPE (TREE_OPERAND
9284 (TREE_OPERAND (tem, 1), 0)),
9285 TREE_OPERAND (tem, 0),
9286 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9287 TREE_OPERAND (TREE_OPERAND (tem, 2),
9288 0)));
9289 return tem;
9290 }
9291 }
9292
9293 switch (code)
9294 {
9295 case NON_LVALUE_EXPR:
9296 if (!maybe_lvalue_p (op0))
9297 return fold_convert_loc (loc, type, op0);
9298 return NULL_TREE;
9299
9300 CASE_CONVERT:
9301 case FLOAT_EXPR:
9302 case FIX_TRUNC_EXPR:
9303 if (COMPARISON_CLASS_P (op0))
9304 {
9305 /* If we have (type) (a CMP b) and type is an integral type, return
9306 new expression involving the new type. Canonicalize
9307 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9308 non-integral type.
9309 Do not fold the result as that would not simplify further, also
9310 folding again results in recursions. */
9311 if (TREE_CODE (type) == BOOLEAN_TYPE)
9312 return build2_loc (loc, TREE_CODE (op0), type,
9313 TREE_OPERAND (op0, 0),
9314 TREE_OPERAND (op0, 1));
9315 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9316 && TREE_CODE (type) != VECTOR_TYPE)
9317 return build3_loc (loc, COND_EXPR, type, op0,
9318 constant_boolean_node (true, type),
9319 constant_boolean_node (false, type));
9320 }
9321
9322 /* Handle (T *)&A.B.C for A being of type T and B and C
9323 living at offset zero. This occurs frequently in
9324 C++ upcasting and then accessing the base. */
9325 if (TREE_CODE (op0) == ADDR_EXPR
9326 && POINTER_TYPE_P (type)
9327 && handled_component_p (TREE_OPERAND (op0, 0)))
9328 {
9329 poly_int64 bitsize, bitpos;
9330 tree offset;
9331 machine_mode mode;
9332 int unsignedp, reversep, volatilep;
9333 tree base
9334 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9335 &offset, &mode, &unsignedp, &reversep,
9336 &volatilep);
9337 /* If the reference was to a (constant) zero offset, we can use
9338 the address of the base if it has the same base type
9339 as the result type and the pointer type is unqualified. */
9340 if (!offset
9341 && known_eq (bitpos, 0)
9342 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9343 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9344 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9345 return fold_convert_loc (loc, type,
9346 build_fold_addr_expr_loc (loc, base));
9347 }
9348
9349 if (TREE_CODE (op0) == MODIFY_EXPR
9350 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9351 /* Detect assigning a bitfield. */
9352 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9353 && DECL_BIT_FIELD
9354 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9355 {
9356 /* Don't leave an assignment inside a conversion
9357 unless assigning a bitfield. */
9358 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9359 /* First do the assignment, then return converted constant. */
9360 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9361 suppress_warning (tem /* What warning? */);
9362 TREE_USED (tem) = 1;
9363 return tem;
9364 }
9365
9366 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9367 constants (if x has signed type, the sign bit cannot be set
9368 in c). This folds extension into the BIT_AND_EXPR.
9369 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9370 very likely don't have maximal range for their precision and this
9371 transformation effectively doesn't preserve non-maximal ranges. */
9372 if (TREE_CODE (type) == INTEGER_TYPE
9373 && TREE_CODE (op0) == BIT_AND_EXPR
9374 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9375 {
9376 tree and_expr = op0;
9377 tree and0 = TREE_OPERAND (and_expr, 0);
9378 tree and1 = TREE_OPERAND (and_expr, 1);
9379 int change = 0;
9380
9381 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9382 || (TYPE_PRECISION (type)
9383 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9384 change = 1;
9385 else if (TYPE_PRECISION (TREE_TYPE (and1))
9386 <= HOST_BITS_PER_WIDE_INT
9387 && tree_fits_uhwi_p (and1))
9388 {
9389 unsigned HOST_WIDE_INT cst;
9390
9391 cst = tree_to_uhwi (and1);
9392 cst &= HOST_WIDE_INT_M1U
9393 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9394 change = (cst == 0);
9395 if (change
9396 && !flag_syntax_only
9397 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9398 == ZERO_EXTEND))
9399 {
9400 tree uns = unsigned_type_for (TREE_TYPE (and0));
9401 and0 = fold_convert_loc (loc, uns, and0);
9402 and1 = fold_convert_loc (loc, uns, and1);
9403 }
9404 }
9405 if (change)
9406 {
9407 tree and1_type = TREE_TYPE (and1);
9408 unsigned prec = MAX (TYPE_PRECISION (and1_type),
9409 TYPE_PRECISION (type));
9410 tem = force_fit_type (type,
9411 wide_int::from (wi::to_wide (and1), prec,
9412 TYPE_SIGN (and1_type)),
9413 0, TREE_OVERFLOW (and1));
9414 return fold_build2_loc (loc, BIT_AND_EXPR, type,
9415 fold_convert_loc (loc, type, and0), tem);
9416 }
9417 }
9418
9419 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9420 cast (T1)X will fold away. We assume that this happens when X itself
9421 is a cast. */
9422 if (POINTER_TYPE_P (type)
9423 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9424 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9425 {
9426 tree arg00 = TREE_OPERAND (arg0, 0);
9427 tree arg01 = TREE_OPERAND (arg0, 1);
9428
9429 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9430 when the pointed type needs higher alignment than
9431 the p+ first operand's pointed type. */
9432 if (!in_gimple_form
9433 && sanitize_flags_p (SANITIZE_ALIGNMENT)
9434 && (min_align_of_type (TREE_TYPE (type))
9435 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9436 return NULL_TREE;
9437
9438 /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9439 when type is a reference type and arg00's type is not,
9440 because arg00 could be validly nullptr and if arg01 doesn't return,
9441 we don't want false positive binding of reference to nullptr. */
9442 if (TREE_CODE (type) == REFERENCE_TYPE
9443 && !in_gimple_form
9444 && sanitize_flags_p (SANITIZE_NULL)
9445 && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9446 return NULL_TREE;
9447
9448 arg00 = fold_convert_loc (loc, type, arg00);
9449 return fold_build_pointer_plus_loc (loc, arg00, arg01);
9450 }
9451
9452 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9453 of the same precision, and X is an integer type not narrower than
9454 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9455 if (INTEGRAL_TYPE_P (type)
9456 && TREE_CODE (op0) == BIT_NOT_EXPR
9457 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9458 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9459 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9460 {
9461 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9462 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9463 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9464 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9465 fold_convert_loc (loc, type, tem));
9466 }
9467
9468 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9469 type of X and Y (integer types only). */
9470 if (INTEGRAL_TYPE_P (type)
9471 && TREE_CODE (op0) == MULT_EXPR
9472 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9473 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9474 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9475 || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9476 {
9477 /* Be careful not to introduce new overflows. */
9478 tree mult_type;
9479 if (TYPE_OVERFLOW_WRAPS (type))
9480 mult_type = type;
9481 else
9482 mult_type = unsigned_type_for (type);
9483
9484 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9485 {
9486 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9487 fold_convert_loc (loc, mult_type,
9488 TREE_OPERAND (op0, 0)),
9489 fold_convert_loc (loc, mult_type,
9490 TREE_OPERAND (op0, 1)));
9491 return fold_convert_loc (loc, type, tem);
9492 }
9493 }
9494
9495 return NULL_TREE;
9496
9497 case VIEW_CONVERT_EXPR:
9498 if (TREE_CODE (op0) == MEM_REF)
9499 {
9500 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9501 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9502 tem = fold_build2_loc (loc, MEM_REF, type,
9503 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9504 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9505 return tem;
9506 }
9507
9508 return NULL_TREE;
9509
9510 case NEGATE_EXPR:
9511 tem = fold_negate_expr (loc, arg0);
9512 if (tem)
9513 return fold_convert_loc (loc, type, tem);
9514 return NULL_TREE;
9515
9516 case ABS_EXPR:
9517 /* Convert fabs((double)float) into (double)fabsf(float). */
9518 if (TREE_CODE (arg0) == NOP_EXPR
9519 && TREE_CODE (type) == REAL_TYPE)
9520 {
9521 tree targ0 = strip_float_extensions (arg0);
9522 if (targ0 != arg0)
9523 return fold_convert_loc (loc, type,
9524 fold_build1_loc (loc, ABS_EXPR,
9525 TREE_TYPE (targ0),
9526 targ0));
9527 }
9528 return NULL_TREE;
9529
9530 case BIT_NOT_EXPR:
9531 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9532 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9533 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9534 fold_convert_loc (loc, type,
9535 TREE_OPERAND (arg0, 0)))))
9536 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9537 fold_convert_loc (loc, type,
9538 TREE_OPERAND (arg0, 1)));
9539 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9540 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9541 fold_convert_loc (loc, type,
9542 TREE_OPERAND (arg0, 1)))))
9543 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9544 fold_convert_loc (loc, type,
9545 TREE_OPERAND (arg0, 0)), tem);
9546
9547 return NULL_TREE;
9548
9549 case TRUTH_NOT_EXPR:
9550 /* Note that the operand of this must be an int
9551 and its values must be 0 or 1.
9552 ("true" is a fixed value perhaps depending on the language,
9553 but we don't handle values other than 1 correctly yet.) */
9554 tem = fold_truth_not_expr (loc, arg0);
9555 if (!tem)
9556 return NULL_TREE;
9557 return fold_convert_loc (loc, type, tem);
9558
9559 case INDIRECT_REF:
9560 /* Fold *&X to X if X is an lvalue. */
9561 if (TREE_CODE (op0) == ADDR_EXPR)
9562 {
9563 tree op00 = TREE_OPERAND (op0, 0);
9564 if ((VAR_P (op00)
9565 || TREE_CODE (op00) == PARM_DECL
9566 || TREE_CODE (op00) == RESULT_DECL)
9567 && !TREE_READONLY (op00))
9568 return op00;
9569 }
9570 return NULL_TREE;
9571
9572 default:
9573 return NULL_TREE;
9574 } /* switch (code) */
9575 }
9576
9577
9578 /* If the operation was a conversion do _not_ mark a resulting constant
9579 with TREE_OVERFLOW if the original constant was not. These conversions
9580 have implementation defined behavior and retaining the TREE_OVERFLOW
9581 flag here would confuse later passes such as VRP. */
9582 tree
9583 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9584 tree type, tree op0)
9585 {
9586 tree res = fold_unary_loc (loc, code, type, op0);
9587 if (res
9588 && TREE_CODE (res) == INTEGER_CST
9589 && TREE_CODE (op0) == INTEGER_CST
9590 && CONVERT_EXPR_CODE_P (code))
9591 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9592
9593 return res;
9594 }
9595
9596 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9597 operands OP0 and OP1. LOC is the location of the resulting expression.
9598 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9599 Return the folded expression if folding is successful. Otherwise,
9600 return NULL_TREE. */
9601 static tree
9602 fold_truth_andor (location_t loc, enum tree_code code, tree type,
9603 tree arg0, tree arg1, tree op0, tree op1)
9604 {
9605 tree tem;
9606
9607 /* We only do these simplifications if we are optimizing. */
9608 if (!optimize)
9609 return NULL_TREE;
9610
9611 /* Check for things like (A || B) && (A || C). We can convert this
9612 to A || (B && C). Note that either operator can be any of the four
9613 truth and/or operations and the transformation will still be
9614 valid. Also note that we only care about order for the
9615 ANDIF and ORIF operators. If B contains side effects, this
9616 might change the truth-value of A. */
9617 if (TREE_CODE (arg0) == TREE_CODE (arg1)
9618 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9619 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9620 || TREE_CODE (arg0) == TRUTH_AND_EXPR
9621 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9622 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9623 {
9624 tree a00 = TREE_OPERAND (arg0, 0);
9625 tree a01 = TREE_OPERAND (arg0, 1);
9626 tree a10 = TREE_OPERAND (arg1, 0);
9627 tree a11 = TREE_OPERAND (arg1, 1);
9628 bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9629 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9630 && (code == TRUTH_AND_EXPR
9631 || code == TRUTH_OR_EXPR));
9632
9633 if (operand_equal_p (a00, a10, 0))
9634 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9635 fold_build2_loc (loc, code, type, a01, a11));
9636 else if (commutative && operand_equal_p (a00, a11, 0))
9637 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9638 fold_build2_loc (loc, code, type, a01, a10));
9639 else if (commutative && operand_equal_p (a01, a10, 0))
9640 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9641 fold_build2_loc (loc, code, type, a00, a11));
9642
9643 /* This case if tricky because we must either have commutative
9644 operators or else A10 must not have side-effects. */
9645
9646 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9647 && operand_equal_p (a01, a11, 0))
9648 return fold_build2_loc (loc, TREE_CODE (arg0), type,
9649 fold_build2_loc (loc, code, type, a00, a10),
9650 a01);
9651 }
9652
9653 /* See if we can build a range comparison. */
9654 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9655 return tem;
9656
9657 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9658 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9659 {
9660 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9661 if (tem)
9662 return fold_build2_loc (loc, code, type, tem, arg1);
9663 }
9664
9665 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9666 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9667 {
9668 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9669 if (tem)
9670 return fold_build2_loc (loc, code, type, arg0, tem);
9671 }
9672
9673 /* Check for the possibility of merging component references. If our
9674 lhs is another similar operation, try to merge its rhs with our
9675 rhs. Then try to merge our lhs and rhs. */
9676 if (TREE_CODE (arg0) == code
9677 && (tem = fold_truth_andor_1 (loc, code, type,
9678 TREE_OPERAND (arg0, 1), arg1)) != 0)
9679 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9680
9681 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9682 return tem;
9683
9684 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9685 if (param_logical_op_non_short_circuit != -1)
9686 logical_op_non_short_circuit
9687 = param_logical_op_non_short_circuit;
9688 if (logical_op_non_short_circuit
9689 && !sanitize_coverage_p ()
9690 && (code == TRUTH_AND_EXPR
9691 || code == TRUTH_ANDIF_EXPR
9692 || code == TRUTH_OR_EXPR
9693 || code == TRUTH_ORIF_EXPR))
9694 {
9695 enum tree_code ncode, icode;
9696
9697 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9698 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9699 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9700
9701 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9702 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9703 We don't want to pack more than two leafs to a non-IF AND/OR
9704 expression.
9705 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9706 equal to IF-CODE, then we don't want to add right-hand operand.
9707 If the inner right-hand side of left-hand operand has
9708 side-effects, or isn't simple, then we can't add to it,
9709 as otherwise we might destroy if-sequence. */
9710 if (TREE_CODE (arg0) == icode
9711 && simple_condition_p (arg1)
9712 /* Needed for sequence points to handle trappings, and
9713 side-effects. */
9714 && simple_condition_p (TREE_OPERAND (arg0, 1)))
9715 {
9716 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9717 arg1);
9718 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9719 tem);
9720 }
9721 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9722 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9723 else if (TREE_CODE (arg1) == icode
9724 && simple_condition_p (arg0)
9725 /* Needed for sequence points to handle trappings, and
9726 side-effects. */
9727 && simple_condition_p (TREE_OPERAND (arg1, 0)))
9728 {
9729 tem = fold_build2_loc (loc, ncode, type,
9730 arg0, TREE_OPERAND (arg1, 0));
9731 return fold_build2_loc (loc, icode, type, tem,
9732 TREE_OPERAND (arg1, 1));
9733 }
9734 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9735 into (A OR B).
9736 For sequence point consistancy, we need to check for trapping,
9737 and side-effects. */
9738 else if (code == icode && simple_condition_p (arg0)
9739 && simple_condition_p (arg1))
9740 return fold_build2_loc (loc, ncode, type, arg0, arg1);
9741 }
9742
9743 return NULL_TREE;
9744 }
9745
9746 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9747 by changing CODE to reduce the magnitude of constants involved in
9748 ARG0 of the comparison.
9749 Returns a canonicalized comparison tree if a simplification was
9750 possible, otherwise returns NULL_TREE.
9751 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9752 valid if signed overflow is undefined. */
9753
9754 static tree
9755 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9756 tree arg0, tree arg1,
9757 bool *strict_overflow_p)
9758 {
9759 enum tree_code code0 = TREE_CODE (arg0);
9760 tree t, cst0 = NULL_TREE;
9761 int sgn0;
9762
9763 /* Match A +- CST code arg1. We can change this only if overflow
9764 is undefined. */
9765 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9766 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9767 /* In principle pointers also have undefined overflow behavior,
9768 but that causes problems elsewhere. */
9769 && !POINTER_TYPE_P (TREE_TYPE (arg0))
9770 && (code0 == MINUS_EXPR
9771 || code0 == PLUS_EXPR)
9772 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9773 return NULL_TREE;
9774
9775 /* Identify the constant in arg0 and its sign. */
9776 cst0 = TREE_OPERAND (arg0, 1);
9777 sgn0 = tree_int_cst_sgn (cst0);
9778
9779 /* Overflowed constants and zero will cause problems. */
9780 if (integer_zerop (cst0)
9781 || TREE_OVERFLOW (cst0))
9782 return NULL_TREE;
9783
9784 /* See if we can reduce the magnitude of the constant in
9785 arg0 by changing the comparison code. */
9786 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9787 if (code == LT_EXPR
9788 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9789 code = LE_EXPR;
9790 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9791 else if (code == GT_EXPR
9792 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9793 code = GE_EXPR;
9794 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9795 else if (code == LE_EXPR
9796 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9797 code = LT_EXPR;
9798 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9799 else if (code == GE_EXPR
9800 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9801 code = GT_EXPR;
9802 else
9803 return NULL_TREE;
9804 *strict_overflow_p = true;
9805
9806 /* Now build the constant reduced in magnitude. But not if that
9807 would produce one outside of its types range. */
9808 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9809 && ((sgn0 == 1
9810 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9811 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9812 || (sgn0 == -1
9813 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9814 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9815 return NULL_TREE;
9816
9817 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9818 cst0, build_int_cst (TREE_TYPE (cst0), 1));
9819 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9820 t = fold_convert (TREE_TYPE (arg1), t);
9821
9822 return fold_build2_loc (loc, code, type, t, arg1);
9823 }
9824
9825 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9826 overflow further. Try to decrease the magnitude of constants involved
9827 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9828 and put sole constants at the second argument position.
9829 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9830
9831 static tree
9832 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9833 tree arg0, tree arg1)
9834 {
9835 tree t;
9836 bool strict_overflow_p;
9837 const char * const warnmsg = G_("assuming signed overflow does not occur "
9838 "when reducing constant in comparison");
9839
9840 /* Try canonicalization by simplifying arg0. */
9841 strict_overflow_p = false;
9842 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9843 &strict_overflow_p);
9844 if (t)
9845 {
9846 if (strict_overflow_p)
9847 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9848 return t;
9849 }
9850
9851 /* Try canonicalization by simplifying arg1 using the swapped
9852 comparison. */
9853 code = swap_tree_comparison (code);
9854 strict_overflow_p = false;
9855 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9856 &strict_overflow_p);
9857 if (t && strict_overflow_p)
9858 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9859 return t;
9860 }
9861
9862 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9863 space. This is used to avoid issuing overflow warnings for
9864 expressions like &p->x which cannot wrap. */
9865
9866 static bool
9867 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9868 {
9869 if (!POINTER_TYPE_P (TREE_TYPE (base)))
9870 return true;
9871
9872 if (maybe_lt (bitpos, 0))
9873 return true;
9874
9875 poly_wide_int wi_offset;
9876 int precision = TYPE_PRECISION (TREE_TYPE (base));
9877 if (offset == NULL_TREE)
9878 wi_offset = wi::zero (precision);
9879 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
9880 return true;
9881 else
9882 wi_offset = wi::to_poly_wide (offset);
9883
9884 wi::overflow_type overflow;
9885 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9886 precision);
9887 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9888 if (overflow)
9889 return true;
9890
9891 poly_uint64 total_hwi, size;
9892 if (!total.to_uhwi (&total_hwi)
9893 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9894 &size)
9895 || known_eq (size, 0U))
9896 return true;
9897
9898 if (known_le (total_hwi, size))
9899 return false;
9900
9901 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9902 array. */
9903 if (TREE_CODE (base) == ADDR_EXPR
9904 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
9905 &size)
9906 && maybe_ne (size, 0U)
9907 && known_le (total_hwi, size))
9908 return false;
9909
9910 return true;
9911 }
9912
9913 /* Return a positive integer when the symbol DECL is known to have
9914 a nonzero address, zero when it's known not to (e.g., it's a weak
9915 symbol), and a negative integer when the symbol is not yet in the
9916 symbol table and so whether or not its address is zero is unknown.
9917 For function local objects always return positive integer. */
9918 static int
9919 maybe_nonzero_address (tree decl)
9920 {
9921 /* Normally, don't do anything for variables and functions before symtab is
9922 built; it is quite possible that DECL will be declared weak later.
9923 But if folding_initializer, we need a constant answer now, so create
9924 the symtab entry and prevent later weak declaration. */
9925 if (DECL_P (decl) && decl_in_symtab_p (decl))
9926 if (struct symtab_node *symbol
9927 = (folding_initializer
9928 ? symtab_node::get_create (decl)
9929 : symtab_node::get (decl)))
9930 return symbol->nonzero_address ();
9931
9932 /* Function local objects are never NULL. */
9933 if (DECL_P (decl)
9934 && (DECL_CONTEXT (decl)
9935 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9936 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
9937 return 1;
9938
9939 return -1;
9940 }
9941
9942 /* Subroutine of fold_binary. This routine performs all of the
9943 transformations that are common to the equality/inequality
9944 operators (EQ_EXPR and NE_EXPR) and the ordering operators
9945 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9946 fold_binary should call fold_binary. Fold a comparison with
9947 tree code CODE and type TYPE with operands OP0 and OP1. Return
9948 the folded comparison or NULL_TREE. */
9949
9950 static tree
9951 fold_comparison (location_t loc, enum tree_code code, tree type,
9952 tree op0, tree op1)
9953 {
9954 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9955 tree arg0, arg1, tem;
9956
9957 arg0 = op0;
9958 arg1 = op1;
9959
9960 STRIP_SIGN_NOPS (arg0);
9961 STRIP_SIGN_NOPS (arg1);
9962
9963 /* For comparisons of pointers we can decompose it to a compile time
9964 comparison of the base objects and the offsets into the object.
9965 This requires at least one operand being an ADDR_EXPR or a
9966 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9967 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9968 && (TREE_CODE (arg0) == ADDR_EXPR
9969 || TREE_CODE (arg1) == ADDR_EXPR
9970 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9971 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9972 {
9973 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9974 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9975 machine_mode mode;
9976 int volatilep, reversep, unsignedp;
9977 bool indirect_base0 = false, indirect_base1 = false;
9978
9979 /* Get base and offset for the access. Strip ADDR_EXPR for
9980 get_inner_reference, but put it back by stripping INDIRECT_REF
9981 off the base object if possible. indirect_baseN will be true
9982 if baseN is not an address but refers to the object itself. */
9983 base0 = arg0;
9984 if (TREE_CODE (arg0) == ADDR_EXPR)
9985 {
9986 base0
9987 = get_inner_reference (TREE_OPERAND (arg0, 0),
9988 &bitsize, &bitpos0, &offset0, &mode,
9989 &unsignedp, &reversep, &volatilep);
9990 if (INDIRECT_REF_P (base0))
9991 base0 = TREE_OPERAND (base0, 0);
9992 else
9993 indirect_base0 = true;
9994 }
9995 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9996 {
9997 base0 = TREE_OPERAND (arg0, 0);
9998 STRIP_SIGN_NOPS (base0);
9999 if (TREE_CODE (base0) == ADDR_EXPR)
10000 {
10001 base0
10002 = get_inner_reference (TREE_OPERAND (base0, 0),
10003 &bitsize, &bitpos0, &offset0, &mode,
10004 &unsignedp, &reversep, &volatilep);
10005 if (INDIRECT_REF_P (base0))
10006 base0 = TREE_OPERAND (base0, 0);
10007 else
10008 indirect_base0 = true;
10009 }
10010 if (offset0 == NULL_TREE || integer_zerop (offset0))
10011 offset0 = TREE_OPERAND (arg0, 1);
10012 else
10013 offset0 = size_binop (PLUS_EXPR, offset0,
10014 TREE_OPERAND (arg0, 1));
10015 if (poly_int_tree_p (offset0))
10016 {
10017 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10018 TYPE_PRECISION (sizetype));
10019 tem <<= LOG2_BITS_PER_UNIT;
10020 tem += bitpos0;
10021 if (tem.to_shwi (&bitpos0))
10022 offset0 = NULL_TREE;
10023 }
10024 }
10025
10026 base1 = arg1;
10027 if (TREE_CODE (arg1) == ADDR_EXPR)
10028 {
10029 base1
10030 = get_inner_reference (TREE_OPERAND (arg1, 0),
10031 &bitsize, &bitpos1, &offset1, &mode,
10032 &unsignedp, &reversep, &volatilep);
10033 if (INDIRECT_REF_P (base1))
10034 base1 = TREE_OPERAND (base1, 0);
10035 else
10036 indirect_base1 = true;
10037 }
10038 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10039 {
10040 base1 = TREE_OPERAND (arg1, 0);
10041 STRIP_SIGN_NOPS (base1);
10042 if (TREE_CODE (base1) == ADDR_EXPR)
10043 {
10044 base1
10045 = get_inner_reference (TREE_OPERAND (base1, 0),
10046 &bitsize, &bitpos1, &offset1, &mode,
10047 &unsignedp, &reversep, &volatilep);
10048 if (INDIRECT_REF_P (base1))
10049 base1 = TREE_OPERAND (base1, 0);
10050 else
10051 indirect_base1 = true;
10052 }
10053 if (offset1 == NULL_TREE || integer_zerop (offset1))
10054 offset1 = TREE_OPERAND (arg1, 1);
10055 else
10056 offset1 = size_binop (PLUS_EXPR, offset1,
10057 TREE_OPERAND (arg1, 1));
10058 if (poly_int_tree_p (offset1))
10059 {
10060 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10061 TYPE_PRECISION (sizetype));
10062 tem <<= LOG2_BITS_PER_UNIT;
10063 tem += bitpos1;
10064 if (tem.to_shwi (&bitpos1))
10065 offset1 = NULL_TREE;
10066 }
10067 }
10068
10069 /* If we have equivalent bases we might be able to simplify. */
10070 if (indirect_base0 == indirect_base1
10071 && operand_equal_p (base0, base1,
10072 indirect_base0 ? OEP_ADDRESS_OF : 0))
10073 {
10074 /* We can fold this expression to a constant if the non-constant
10075 offset parts are equal. */
10076 if ((offset0 == offset1
10077 || (offset0 && offset1
10078 && operand_equal_p (offset0, offset1, 0)))
10079 && (equality_code
10080 || (indirect_base0
10081 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10082 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10083 {
10084 if (!equality_code
10085 && maybe_ne (bitpos0, bitpos1)
10086 && (pointer_may_wrap_p (base0, offset0, bitpos0)
10087 || pointer_may_wrap_p (base1, offset1, bitpos1)))
10088 fold_overflow_warning (("assuming pointer wraparound does not "
10089 "occur when comparing P +- C1 with "
10090 "P +- C2"),
10091 WARN_STRICT_OVERFLOW_CONDITIONAL);
10092
10093 switch (code)
10094 {
10095 case EQ_EXPR:
10096 if (known_eq (bitpos0, bitpos1))
10097 return constant_boolean_node (true, type);
10098 if (known_ne (bitpos0, bitpos1))
10099 return constant_boolean_node (false, type);
10100 break;
10101 case NE_EXPR:
10102 if (known_ne (bitpos0, bitpos1))
10103 return constant_boolean_node (true, type);
10104 if (known_eq (bitpos0, bitpos1))
10105 return constant_boolean_node (false, type);
10106 break;
10107 case LT_EXPR:
10108 if (known_lt (bitpos0, bitpos1))
10109 return constant_boolean_node (true, type);
10110 if (known_ge (bitpos0, bitpos1))
10111 return constant_boolean_node (false, type);
10112 break;
10113 case LE_EXPR:
10114 if (known_le (bitpos0, bitpos1))
10115 return constant_boolean_node (true, type);
10116 if (known_gt (bitpos0, bitpos1))
10117 return constant_boolean_node (false, type);
10118 break;
10119 case GE_EXPR:
10120 if (known_ge (bitpos0, bitpos1))
10121 return constant_boolean_node (true, type);
10122 if (known_lt (bitpos0, bitpos1))
10123 return constant_boolean_node (false, type);
10124 break;
10125 case GT_EXPR:
10126 if (known_gt (bitpos0, bitpos1))
10127 return constant_boolean_node (true, type);
10128 if (known_le (bitpos0, bitpos1))
10129 return constant_boolean_node (false, type);
10130 break;
10131 default:;
10132 }
10133 }
10134 /* We can simplify the comparison to a comparison of the variable
10135 offset parts if the constant offset parts are equal.
10136 Be careful to use signed sizetype here because otherwise we
10137 mess with array offsets in the wrong way. This is possible
10138 because pointer arithmetic is restricted to retain within an
10139 object and overflow on pointer differences is undefined as of
10140 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10141 else if (known_eq (bitpos0, bitpos1)
10142 && (equality_code
10143 || (indirect_base0
10144 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10145 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10146 {
10147 /* By converting to signed sizetype we cover middle-end pointer
10148 arithmetic which operates on unsigned pointer types of size
10149 type size and ARRAY_REF offsets which are properly sign or
10150 zero extended from their type in case it is narrower than
10151 sizetype. */
10152 if (offset0 == NULL_TREE)
10153 offset0 = build_int_cst (ssizetype, 0);
10154 else
10155 offset0 = fold_convert_loc (loc, ssizetype, offset0);
10156 if (offset1 == NULL_TREE)
10157 offset1 = build_int_cst (ssizetype, 0);
10158 else
10159 offset1 = fold_convert_loc (loc, ssizetype, offset1);
10160
10161 if (!equality_code
10162 && (pointer_may_wrap_p (base0, offset0, bitpos0)
10163 || pointer_may_wrap_p (base1, offset1, bitpos1)))
10164 fold_overflow_warning (("assuming pointer wraparound does not "
10165 "occur when comparing P +- C1 with "
10166 "P +- C2"),
10167 WARN_STRICT_OVERFLOW_COMPARISON);
10168
10169 return fold_build2_loc (loc, code, type, offset0, offset1);
10170 }
10171 }
10172 /* For equal offsets we can simplify to a comparison of the
10173 base addresses. */
10174 else if (known_eq (bitpos0, bitpos1)
10175 && (indirect_base0
10176 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10177 && (indirect_base1
10178 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10179 && ((offset0 == offset1)
10180 || (offset0 && offset1
10181 && operand_equal_p (offset0, offset1, 0))))
10182 {
10183 if (indirect_base0)
10184 base0 = build_fold_addr_expr_loc (loc, base0);
10185 if (indirect_base1)
10186 base1 = build_fold_addr_expr_loc (loc, base1);
10187 return fold_build2_loc (loc, code, type, base0, base1);
10188 }
10189 /* Comparison between an ordinary (non-weak) symbol and a null
10190 pointer can be eliminated since such symbols must have a non
10191 null address. In C, relational expressions between pointers
10192 to objects and null pointers are undefined. The results
10193 below follow the C++ rules with the additional property that
10194 every object pointer compares greater than a null pointer.
10195 */
10196 else if (((DECL_P (base0)
10197 && maybe_nonzero_address (base0) > 0
10198 /* Avoid folding references to struct members at offset 0 to
10199 prevent tests like '&ptr->firstmember == 0' from getting
10200 eliminated. When ptr is null, although the -> expression
10201 is strictly speaking invalid, GCC retains it as a matter
10202 of QoI. See PR c/44555. */
10203 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10204 || CONSTANT_CLASS_P (base0))
10205 && indirect_base0
10206 /* The caller guarantees that when one of the arguments is
10207 constant (i.e., null in this case) it is second. */
10208 && integer_zerop (arg1))
10209 {
10210 switch (code)
10211 {
10212 case EQ_EXPR:
10213 case LE_EXPR:
10214 case LT_EXPR:
10215 return constant_boolean_node (false, type);
10216 case GE_EXPR:
10217 case GT_EXPR:
10218 case NE_EXPR:
10219 return constant_boolean_node (true, type);
10220 default:
10221 gcc_unreachable ();
10222 }
10223 }
10224 }
10225
10226 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10227 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10228 the resulting offset is smaller in absolute value than the
10229 original one and has the same sign. */
10230 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10231 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10232 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10233 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10234 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10235 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10236 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10237 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10238 {
10239 tree const1 = TREE_OPERAND (arg0, 1);
10240 tree const2 = TREE_OPERAND (arg1, 1);
10241 tree variable1 = TREE_OPERAND (arg0, 0);
10242 tree variable2 = TREE_OPERAND (arg1, 0);
10243 tree cst;
10244 const char * const warnmsg = G_("assuming signed overflow does not "
10245 "occur when combining constants around "
10246 "a comparison");
10247
10248 /* Put the constant on the side where it doesn't overflow and is
10249 of lower absolute value and of same sign than before. */
10250 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10251 ? MINUS_EXPR : PLUS_EXPR,
10252 const2, const1);
10253 if (!TREE_OVERFLOW (cst)
10254 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10255 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10256 {
10257 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10258 return fold_build2_loc (loc, code, type,
10259 variable1,
10260 fold_build2_loc (loc, TREE_CODE (arg1),
10261 TREE_TYPE (arg1),
10262 variable2, cst));
10263 }
10264
10265 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10266 ? MINUS_EXPR : PLUS_EXPR,
10267 const1, const2);
10268 if (!TREE_OVERFLOW (cst)
10269 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10270 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10271 {
10272 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10273 return fold_build2_loc (loc, code, type,
10274 fold_build2_loc (loc, TREE_CODE (arg0),
10275 TREE_TYPE (arg0),
10276 variable1, cst),
10277 variable2);
10278 }
10279 }
10280
10281 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10282 if (tem)
10283 return tem;
10284
10285 /* If we are comparing an expression that just has comparisons
10286 of two integer values, arithmetic expressions of those comparisons,
10287 and constants, we can simplify it. There are only three cases
10288 to check: the two values can either be equal, the first can be
10289 greater, or the second can be greater. Fold the expression for
10290 those three values. Since each value must be 0 or 1, we have
10291 eight possibilities, each of which corresponds to the constant 0
10292 or 1 or one of the six possible comparisons.
10293
10294 This handles common cases like (a > b) == 0 but also handles
10295 expressions like ((x > y) - (y > x)) > 0, which supposedly
10296 occur in macroized code. */
10297
10298 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10299 {
10300 tree cval1 = 0, cval2 = 0;
10301
10302 if (twoval_comparison_p (arg0, &cval1, &cval2)
10303 /* Don't handle degenerate cases here; they should already
10304 have been handled anyway. */
10305 && cval1 != 0 && cval2 != 0
10306 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10307 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10308 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10309 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10310 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10311 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10312 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10313 {
10314 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10315 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10316
10317 /* We can't just pass T to eval_subst in case cval1 or cval2
10318 was the same as ARG1. */
10319
10320 tree high_result
10321 = fold_build2_loc (loc, code, type,
10322 eval_subst (loc, arg0, cval1, maxval,
10323 cval2, minval),
10324 arg1);
10325 tree equal_result
10326 = fold_build2_loc (loc, code, type,
10327 eval_subst (loc, arg0, cval1, maxval,
10328 cval2, maxval),
10329 arg1);
10330 tree low_result
10331 = fold_build2_loc (loc, code, type,
10332 eval_subst (loc, arg0, cval1, minval,
10333 cval2, maxval),
10334 arg1);
10335
10336 /* All three of these results should be 0 or 1. Confirm they are.
10337 Then use those values to select the proper code to use. */
10338
10339 if (TREE_CODE (high_result) == INTEGER_CST
10340 && TREE_CODE (equal_result) == INTEGER_CST
10341 && TREE_CODE (low_result) == INTEGER_CST)
10342 {
10343 /* Make a 3-bit mask with the high-order bit being the
10344 value for `>', the next for '=', and the low for '<'. */
10345 switch ((integer_onep (high_result) * 4)
10346 + (integer_onep (equal_result) * 2)
10347 + integer_onep (low_result))
10348 {
10349 case 0:
10350 /* Always false. */
10351 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10352 case 1:
10353 code = LT_EXPR;
10354 break;
10355 case 2:
10356 code = EQ_EXPR;
10357 break;
10358 case 3:
10359 code = LE_EXPR;
10360 break;
10361 case 4:
10362 code = GT_EXPR;
10363 break;
10364 case 5:
10365 code = NE_EXPR;
10366 break;
10367 case 6:
10368 code = GE_EXPR;
10369 break;
10370 case 7:
10371 /* Always true. */
10372 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10373 }
10374
10375 return fold_build2_loc (loc, code, type, cval1, cval2);
10376 }
10377 }
10378 }
10379
10380 return NULL_TREE;
10381 }
10382
10383
10384 /* Subroutine of fold_binary. Optimize complex multiplications of the
10385 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10386 argument EXPR represents the expression "z" of type TYPE. */
10387
10388 static tree
10389 fold_mult_zconjz (location_t loc, tree type, tree expr)
10390 {
10391 tree itype = TREE_TYPE (type);
10392 tree rpart, ipart, tem;
10393
10394 if (TREE_CODE (expr) == COMPLEX_EXPR)
10395 {
10396 rpart = TREE_OPERAND (expr, 0);
10397 ipart = TREE_OPERAND (expr, 1);
10398 }
10399 else if (TREE_CODE (expr) == COMPLEX_CST)
10400 {
10401 rpart = TREE_REALPART (expr);
10402 ipart = TREE_IMAGPART (expr);
10403 }
10404 else
10405 {
10406 expr = save_expr (expr);
10407 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10408 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10409 }
10410
10411 rpart = save_expr (rpart);
10412 ipart = save_expr (ipart);
10413 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10414 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10415 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10416 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10417 build_zero_cst (itype));
10418 }
10419
10420
10421 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10422 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10423 true if successful. */
10424
10425 static bool
10426 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10427 {
10428 unsigned HOST_WIDE_INT i, nunits;
10429
10430 if (TREE_CODE (arg) == VECTOR_CST
10431 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10432 {
10433 for (i = 0; i < nunits; ++i)
10434 elts[i] = VECTOR_CST_ELT (arg, i);
10435 }
10436 else if (TREE_CODE (arg) == CONSTRUCTOR)
10437 {
10438 constructor_elt *elt;
10439
10440 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10441 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10442 return false;
10443 else
10444 elts[i] = elt->value;
10445 }
10446 else
10447 return false;
10448 for (; i < nelts; i++)
10449 elts[i]
10450 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10451 return true;
10452 }
10453
10454 /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10455 mask for VLA vec_perm folding.
10456 REASON if specified, will contain the reason why SEL is not suitable.
10457 Used only for debugging and unit-testing. */
10458
10459 static bool
10460 valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10461 const vec_perm_indices &sel,
10462 const char **reason = NULL)
10463 {
10464 unsigned sel_npatterns = sel.encoding ().npatterns ();
10465 unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10466
10467 if (!(pow2p_hwi (sel_npatterns)
10468 && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10469 && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10470 {
10471 if (reason)
10472 *reason = "npatterns is not power of 2";
10473 return false;
10474 }
10475
10476 /* We want to avoid cases where sel.length is not a multiple of npatterns.
10477 For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10478 poly_uint64 esel;
10479 if (!multiple_p (sel.length (), sel_npatterns, &esel))
10480 {
10481 if (reason)
10482 *reason = "sel.length is not multiple of sel_npatterns";
10483 return false;
10484 }
10485
10486 if (sel_nelts_per_pattern < 3)
10487 return true;
10488
10489 for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10490 {
10491 poly_uint64 a1 = sel[pattern + sel_npatterns];
10492 poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10493 HOST_WIDE_INT step;
10494 if (!poly_int64 (a2 - a1).is_constant (&step))
10495 {
10496 if (reason)
10497 *reason = "step is not constant";
10498 return false;
10499 }
10500 // FIXME: Punt on step < 0 for now, revisit later.
10501 if (step < 0)
10502 return false;
10503 if (step == 0)
10504 continue;
10505
10506 if (!pow2p_hwi (step))
10507 {
10508 if (reason)
10509 *reason = "step is not power of 2";
10510 return false;
10511 }
10512
10513 /* Ensure that stepped sequence of the pattern selects elements
10514 only from the same input vector. */
10515 uint64_t q1, qe;
10516 poly_uint64 r1, re;
10517 poly_uint64 ae = a1 + (esel - 2) * step;
10518 poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10519
10520 if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10521 && can_div_trunc_p (ae, arg_len, &qe, &re)
10522 && q1 == qe))
10523 {
10524 if (reason)
10525 *reason = "crossed input vectors";
10526 return false;
10527 }
10528
10529 /* Ensure that the stepped sequence always selects from the same
10530 input pattern. */
10531 tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10532 unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10533
10534 if (!multiple_p (step, arg_npatterns))
10535 {
10536 if (reason)
10537 *reason = "step is not multiple of npatterns";
10538 return false;
10539 }
10540
10541 /* If a1 chooses base element from arg, ensure that it's a natural
10542 stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10543 to preserve arg's encoding. */
10544
10545 if (maybe_lt (r1, arg_npatterns))
10546 {
10547 unsigned HOST_WIDE_INT index;
10548 if (!r1.is_constant (&index))
10549 return false;
10550
10551 tree arg_elem0 = vector_cst_elt (arg, index);
10552 tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10553 tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10554
10555 tree step1, step2;
10556 if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10557 || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10558 || !operand_equal_p (step1, step2, 0))
10559 {
10560 if (reason)
10561 *reason = "not a natural stepped sequence";
10562 return false;
10563 }
10564 }
10565 }
10566
10567 return true;
10568 }
10569
10570 /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10571 the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10572 REASON has same purpose as described in
10573 valid_mask_for_fold_vec_perm_cst_p. */
10574
10575 static tree
10576 fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10577 const char **reason = NULL)
10578 {
10579 unsigned res_npatterns, res_nelts_per_pattern;
10580 unsigned HOST_WIDE_INT res_nelts;
10581
10582 /* First try to implement the fold in a VLA-friendly way.
10583
10584 (1) If the selector is simply a duplication of N elements, the
10585 result is likewise a duplication of N elements.
10586
10587 (2) If the selector is N elements followed by a duplication
10588 of N elements, the result is too.
10589
10590 (3) If the selector is N elements followed by an interleaving
10591 of N linear series, the situation is more complex.
10592
10593 valid_mask_for_fold_vec_perm_cst_p detects whether we
10594 can handle this case. If we can, then each of the N linear
10595 series either (a) selects the same element each time or
10596 (b) selects a linear series from one of the input patterns.
10597
10598 If (b) holds for one of the linear series, the result
10599 will contain a linear series, and so the result will have
10600 the same shape as the selector. If (a) holds for all of
10601 the linear series, the result will be the same as (2) above.
10602
10603 (b) can only hold if one of the input patterns has a
10604 stepped encoding. */
10605
10606 if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10607 {
10608 res_npatterns = sel.encoding ().npatterns ();
10609 res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10610 if (res_nelts_per_pattern == 3
10611 && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10612 && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10613 res_nelts_per_pattern = 2;
10614 res_nelts = res_npatterns * res_nelts_per_pattern;
10615 }
10616 else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10617 {
10618 res_npatterns = res_nelts;
10619 res_nelts_per_pattern = 1;
10620 }
10621 else
10622 return NULL_TREE;
10623
10624 tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10625 for (unsigned i = 0; i < res_nelts; i++)
10626 {
10627 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10628 uint64_t q;
10629 poly_uint64 r;
10630 unsigned HOST_WIDE_INT index;
10631
10632 /* Punt if sel[i] /trunc_div len cannot be determined,
10633 because the input vector to be chosen will depend on
10634 runtime vector length.
10635 For example if len == 4 + 4x, and sel[i] == 4,
10636 If len at runtime equals 4, we choose arg1[0].
10637 For any other value of len > 4 at runtime, we choose arg0[4].
10638 which makes the element choice dependent on runtime vector length. */
10639 if (!can_div_trunc_p (sel[i], len, &q, &r))
10640 {
10641 if (reason)
10642 *reason = "cannot divide selector element by arg len";
10643 return NULL_TREE;
10644 }
10645
10646 /* sel[i] % len will give the index of element in the chosen input
10647 vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10648 we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10649 if (!r.is_constant (&index))
10650 {
10651 if (reason)
10652 *reason = "remainder is not constant";
10653 return NULL_TREE;
10654 }
10655
10656 tree arg = ((q & 1) == 0) ? arg0 : arg1;
10657 tree elem = vector_cst_elt (arg, index);
10658 out_elts.quick_push (elem);
10659 }
10660
10661 return out_elts.build ();
10662 }
10663
10664 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10665 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10666 NULL_TREE otherwise. */
10667
10668 tree
10669 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10670 {
10671 unsigned int i;
10672 unsigned HOST_WIDE_INT nelts;
10673
10674 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10675 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10676 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10677
10678 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10679 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10680 return NULL_TREE;
10681
10682 if (TREE_CODE (arg0) == VECTOR_CST
10683 && TREE_CODE (arg1) == VECTOR_CST)
10684 return fold_vec_perm_cst (type, arg0, arg1, sel);
10685
10686 /* For fall back case, we want to ensure we have VLS vectors
10687 with equal length. */
10688 if (!sel.length ().is_constant (&nelts)
10689 || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10690 return NULL_TREE;
10691
10692 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10693 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10694 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10695 return NULL_TREE;
10696
10697 vec<constructor_elt, va_gc> *v;
10698 vec_alloc (v, nelts);
10699 for (i = 0; i < nelts; i++)
10700 {
10701 HOST_WIDE_INT index;
10702 if (!sel[i].is_constant (&index))
10703 return NULL_TREE;
10704 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10705 }
10706 return build_constructor (type, v);
10707 }
10708
10709 /* Try to fold a pointer difference of type TYPE two address expressions of
10710 array references AREF0 and AREF1 using location LOC. Return a
10711 simplified expression for the difference or NULL_TREE. */
10712
10713 static tree
10714 fold_addr_of_array_ref_difference (location_t loc, tree type,
10715 tree aref0, tree aref1,
10716 bool use_pointer_diff)
10717 {
10718 tree base0 = TREE_OPERAND (aref0, 0);
10719 tree base1 = TREE_OPERAND (aref1, 0);
10720 tree base_offset = build_int_cst (type, 0);
10721
10722 /* If the bases are array references as well, recurse. If the bases
10723 are pointer indirections compute the difference of the pointers.
10724 If the bases are equal, we are set. */
10725 if ((TREE_CODE (base0) == ARRAY_REF
10726 && TREE_CODE (base1) == ARRAY_REF
10727 && (base_offset
10728 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10729 use_pointer_diff)))
10730 || (INDIRECT_REF_P (base0)
10731 && INDIRECT_REF_P (base1)
10732 && (base_offset
10733 = use_pointer_diff
10734 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10735 TREE_OPERAND (base0, 0),
10736 TREE_OPERAND (base1, 0))
10737 : fold_binary_loc (loc, MINUS_EXPR, type,
10738 fold_convert (type,
10739 TREE_OPERAND (base0, 0)),
10740 fold_convert (type,
10741 TREE_OPERAND (base1, 0)))))
10742 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10743 {
10744 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10745 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10746 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10747 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10748 return fold_build2_loc (loc, PLUS_EXPR, type,
10749 base_offset,
10750 fold_build2_loc (loc, MULT_EXPR, type,
10751 diff, esz));
10752 }
10753 return NULL_TREE;
10754 }
10755
10756 /* If the real or vector real constant CST of type TYPE has an exact
10757 inverse, return it, else return NULL. */
10758
10759 tree
10760 exact_inverse (tree type, tree cst)
10761 {
10762 REAL_VALUE_TYPE r;
10763 tree unit_type;
10764 machine_mode mode;
10765
10766 switch (TREE_CODE (cst))
10767 {
10768 case REAL_CST:
10769 r = TREE_REAL_CST (cst);
10770
10771 if (exact_real_inverse (TYPE_MODE (type), &r))
10772 return build_real (type, r);
10773
10774 return NULL_TREE;
10775
10776 case VECTOR_CST:
10777 {
10778 unit_type = TREE_TYPE (type);
10779 mode = TYPE_MODE (unit_type);
10780
10781 tree_vector_builder elts;
10782 if (!elts.new_unary_operation (type, cst, false))
10783 return NULL_TREE;
10784 unsigned int count = elts.encoded_nelts ();
10785 for (unsigned int i = 0; i < count; ++i)
10786 {
10787 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10788 if (!exact_real_inverse (mode, &r))
10789 return NULL_TREE;
10790 elts.quick_push (build_real (unit_type, r));
10791 }
10792
10793 return elts.build ();
10794 }
10795
10796 default:
10797 return NULL_TREE;
10798 }
10799 }
10800
10801 /* Mask out the tz least significant bits of X of type TYPE where
10802 tz is the number of trailing zeroes in Y. */
10803 static wide_int
10804 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10805 {
10806 int tz = wi::ctz (y);
10807 if (tz > 0)
10808 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10809 return x;
10810 }
10811
10812 /* Return true when T is an address and is known to be nonzero.
10813 For floating point we further ensure that T is not denormal.
10814 Similar logic is present in nonzero_address in rtlanal.h.
10815
10816 If the return value is based on the assumption that signed overflow
10817 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10818 change *STRICT_OVERFLOW_P. */
10819
10820 static bool
10821 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10822 {
10823 tree type = TREE_TYPE (t);
10824 enum tree_code code;
10825
10826 /* Doing something useful for floating point would need more work. */
10827 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10828 return false;
10829
10830 code = TREE_CODE (t);
10831 switch (TREE_CODE_CLASS (code))
10832 {
10833 case tcc_unary:
10834 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10835 strict_overflow_p);
10836 case tcc_binary:
10837 case tcc_comparison:
10838 return tree_binary_nonzero_warnv_p (code, type,
10839 TREE_OPERAND (t, 0),
10840 TREE_OPERAND (t, 1),
10841 strict_overflow_p);
10842 case tcc_constant:
10843 case tcc_declaration:
10844 case tcc_reference:
10845 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10846
10847 default:
10848 break;
10849 }
10850
10851 switch (code)
10852 {
10853 case TRUTH_NOT_EXPR:
10854 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10855 strict_overflow_p);
10856
10857 case TRUTH_AND_EXPR:
10858 case TRUTH_OR_EXPR:
10859 case TRUTH_XOR_EXPR:
10860 return tree_binary_nonzero_warnv_p (code, type,
10861 TREE_OPERAND (t, 0),
10862 TREE_OPERAND (t, 1),
10863 strict_overflow_p);
10864
10865 case COND_EXPR:
10866 case CONSTRUCTOR:
10867 case OBJ_TYPE_REF:
10868 case ADDR_EXPR:
10869 case WITH_SIZE_EXPR:
10870 case SSA_NAME:
10871 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10872
10873 case COMPOUND_EXPR:
10874 case MODIFY_EXPR:
10875 case BIND_EXPR:
10876 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10877 strict_overflow_p);
10878
10879 case SAVE_EXPR:
10880 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10881 strict_overflow_p);
10882
10883 case CALL_EXPR:
10884 {
10885 tree fndecl = get_callee_fndecl (t);
10886 if (!fndecl) return false;
10887 if (flag_delete_null_pointer_checks && !flag_check_new
10888 && DECL_IS_OPERATOR_NEW_P (fndecl)
10889 && !TREE_NOTHROW (fndecl))
10890 return true;
10891 if (flag_delete_null_pointer_checks
10892 && lookup_attribute ("returns_nonnull",
10893 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10894 return true;
10895 return alloca_call_p (t);
10896 }
10897
10898 default:
10899 break;
10900 }
10901 return false;
10902 }
10903
10904 /* Return true when T is an address and is known to be nonzero.
10905 Handle warnings about undefined signed overflow. */
10906
10907 bool
10908 tree_expr_nonzero_p (tree t)
10909 {
10910 bool ret, strict_overflow_p;
10911
10912 strict_overflow_p = false;
10913 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10914 if (strict_overflow_p)
10915 fold_overflow_warning (("assuming signed overflow does not occur when "
10916 "determining that expression is always "
10917 "non-zero"),
10918 WARN_STRICT_OVERFLOW_MISC);
10919 return ret;
10920 }
10921
10922 /* Return true if T is known not to be equal to an integer W. */
10923
10924 bool
10925 expr_not_equal_to (tree t, const wide_int &w)
10926 {
10927 int_range_max vr;
10928 switch (TREE_CODE (t))
10929 {
10930 case INTEGER_CST:
10931 return wi::to_wide (t) != w;
10932
10933 case SSA_NAME:
10934 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10935 return false;
10936
10937 get_range_query (cfun)->range_of_expr (vr, t);
10938 if (!vr.undefined_p () && !vr.contains_p (w))
10939 return true;
10940 /* If T has some known zero bits and W has any of those bits set,
10941 then T is known not to be equal to W. */
10942 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10943 TYPE_PRECISION (TREE_TYPE (t))), 0))
10944 return true;
10945 return false;
10946
10947 default:
10948 return false;
10949 }
10950 }
10951
10952 /* Fold a binary expression of code CODE and type TYPE with operands
10953 OP0 and OP1. LOC is the location of the resulting expression.
10954 Return the folded expression if folding is successful. Otherwise,
10955 return NULL_TREE. */
10956
10957 tree
10958 fold_binary_loc (location_t loc, enum tree_code code, tree type,
10959 tree op0, tree op1)
10960 {
10961 enum tree_code_class kind = TREE_CODE_CLASS (code);
10962 tree arg0, arg1, tem;
10963 tree t1 = NULL_TREE;
10964 bool strict_overflow_p;
10965 unsigned int prec;
10966
10967 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10968 && TREE_CODE_LENGTH (code) == 2
10969 && op0 != NULL_TREE
10970 && op1 != NULL_TREE);
10971
10972 arg0 = op0;
10973 arg1 = op1;
10974
10975 /* Strip any conversions that don't change the mode. This is
10976 safe for every expression, except for a comparison expression
10977 because its signedness is derived from its operands. So, in
10978 the latter case, only strip conversions that don't change the
10979 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10980 preserved.
10981
10982 Note that this is done as an internal manipulation within the
10983 constant folder, in order to find the simplest representation
10984 of the arguments so that their form can be studied. In any
10985 cases, the appropriate type conversions should be put back in
10986 the tree that will get out of the constant folder. */
10987
10988 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10989 {
10990 STRIP_SIGN_NOPS (arg0);
10991 STRIP_SIGN_NOPS (arg1);
10992 }
10993 else
10994 {
10995 STRIP_NOPS (arg0);
10996 STRIP_NOPS (arg1);
10997 }
10998
10999 /* Note that TREE_CONSTANT isn't enough: static var addresses are
11000 constant but we can't do arithmetic on them. */
11001 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11002 {
11003 tem = const_binop (code, type, arg0, arg1);
11004 if (tem != NULL_TREE)
11005 {
11006 if (TREE_TYPE (tem) != type)
11007 tem = fold_convert_loc (loc, type, tem);
11008 return tem;
11009 }
11010 }
11011
11012 /* If this is a commutative operation, and ARG0 is a constant, move it
11013 to ARG1 to reduce the number of tests below. */
11014 if (commutative_tree_code (code)
11015 && tree_swap_operands_p (arg0, arg1))
11016 return fold_build2_loc (loc, code, type, op1, op0);
11017
11018 /* Likewise if this is a comparison, and ARG0 is a constant, move it
11019 to ARG1 to reduce the number of tests below. */
11020 if (kind == tcc_comparison
11021 && tree_swap_operands_p (arg0, arg1))
11022 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11023
11024 tem = generic_simplify (loc, code, type, op0, op1);
11025 if (tem)
11026 return tem;
11027
11028 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11029
11030 First check for cases where an arithmetic operation is applied to a
11031 compound, conditional, or comparison operation. Push the arithmetic
11032 operation inside the compound or conditional to see if any folding
11033 can then be done. Convert comparison to conditional for this purpose.
11034 The also optimizes non-constant cases that used to be done in
11035 expand_expr.
11036
11037 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11038 one of the operands is a comparison and the other is a comparison, a
11039 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11040 code below would make the expression more complex. Change it to a
11041 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11042 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11043
11044 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11045 || code == EQ_EXPR || code == NE_EXPR)
11046 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11047 && ((truth_value_p (TREE_CODE (arg0))
11048 && (truth_value_p (TREE_CODE (arg1))
11049 || (TREE_CODE (arg1) == BIT_AND_EXPR
11050 && integer_onep (TREE_OPERAND (arg1, 1)))))
11051 || (truth_value_p (TREE_CODE (arg1))
11052 && (truth_value_p (TREE_CODE (arg0))
11053 || (TREE_CODE (arg0) == BIT_AND_EXPR
11054 && integer_onep (TREE_OPERAND (arg0, 1)))))))
11055 {
11056 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11057 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11058 : TRUTH_XOR_EXPR,
11059 boolean_type_node,
11060 fold_convert_loc (loc, boolean_type_node, arg0),
11061 fold_convert_loc (loc, boolean_type_node, arg1));
11062
11063 if (code == EQ_EXPR)
11064 tem = invert_truthvalue_loc (loc, tem);
11065
11066 return fold_convert_loc (loc, type, tem);
11067 }
11068
11069 if (TREE_CODE_CLASS (code) == tcc_binary
11070 || TREE_CODE_CLASS (code) == tcc_comparison)
11071 {
11072 if (TREE_CODE (arg0) == COMPOUND_EXPR)
11073 {
11074 tem = fold_build2_loc (loc, code, type,
11075 fold_convert_loc (loc, TREE_TYPE (op0),
11076 TREE_OPERAND (arg0, 1)), op1);
11077 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11078 tem);
11079 }
11080 if (TREE_CODE (arg1) == COMPOUND_EXPR)
11081 {
11082 tem = fold_build2_loc (loc, code, type, op0,
11083 fold_convert_loc (loc, TREE_TYPE (op1),
11084 TREE_OPERAND (arg1, 1)));
11085 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11086 tem);
11087 }
11088
11089 if (TREE_CODE (arg0) == COND_EXPR
11090 || TREE_CODE (arg0) == VEC_COND_EXPR
11091 || COMPARISON_CLASS_P (arg0))
11092 {
11093 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11094 arg0, arg1,
11095 /*cond_first_p=*/1);
11096 if (tem != NULL_TREE)
11097 return tem;
11098 }
11099
11100 if (TREE_CODE (arg1) == COND_EXPR
11101 || TREE_CODE (arg1) == VEC_COND_EXPR
11102 || COMPARISON_CLASS_P (arg1))
11103 {
11104 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11105 arg1, arg0,
11106 /*cond_first_p=*/0);
11107 if (tem != NULL_TREE)
11108 return tem;
11109 }
11110 }
11111
11112 switch (code)
11113 {
11114 case MEM_REF:
11115 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11116 if (TREE_CODE (arg0) == ADDR_EXPR
11117 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11118 {
11119 tree iref = TREE_OPERAND (arg0, 0);
11120 return fold_build2 (MEM_REF, type,
11121 TREE_OPERAND (iref, 0),
11122 int_const_binop (PLUS_EXPR, arg1,
11123 TREE_OPERAND (iref, 1)));
11124 }
11125
11126 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11127 if (TREE_CODE (arg0) == ADDR_EXPR
11128 && handled_component_p (TREE_OPERAND (arg0, 0)))
11129 {
11130 tree base;
11131 poly_int64 coffset;
11132 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11133 &coffset);
11134 if (!base)
11135 return NULL_TREE;
11136 return fold_build2 (MEM_REF, type,
11137 build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11138 int_const_binop (PLUS_EXPR, arg1,
11139 size_int (coffset)));
11140 }
11141
11142 return NULL_TREE;
11143
11144 case POINTER_PLUS_EXPR:
11145 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11146 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11147 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11148 return fold_convert_loc (loc, type,
11149 fold_build2_loc (loc, PLUS_EXPR, sizetype,
11150 fold_convert_loc (loc, sizetype,
11151 arg1),
11152 fold_convert_loc (loc, sizetype,
11153 arg0)));
11154
11155 return NULL_TREE;
11156
11157 case PLUS_EXPR:
11158 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11159 {
11160 /* X + (X / CST) * -CST is X % CST. */
11161 if (TREE_CODE (arg1) == MULT_EXPR
11162 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11163 && operand_equal_p (arg0,
11164 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11165 {
11166 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11167 tree cst1 = TREE_OPERAND (arg1, 1);
11168 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11169 cst1, cst0);
11170 if (sum && integer_zerop (sum))
11171 return fold_convert_loc (loc, type,
11172 fold_build2_loc (loc, TRUNC_MOD_EXPR,
11173 TREE_TYPE (arg0), arg0,
11174 cst0));
11175 }
11176 }
11177
11178 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11179 one. Make sure the type is not saturating and has the signedness of
11180 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11181 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11182 if ((TREE_CODE (arg0) == MULT_EXPR
11183 || TREE_CODE (arg1) == MULT_EXPR)
11184 && !TYPE_SATURATING (type)
11185 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11186 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11187 && (!FLOAT_TYPE_P (type) || flag_associative_math))
11188 {
11189 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11190 if (tem)
11191 return tem;
11192 }
11193
11194 if (! FLOAT_TYPE_P (type))
11195 {
11196 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11197 (plus (plus (mult) (mult)) (foo)) so that we can
11198 take advantage of the factoring cases below. */
11199 if (ANY_INTEGRAL_TYPE_P (type)
11200 && TYPE_OVERFLOW_WRAPS (type)
11201 && (((TREE_CODE (arg0) == PLUS_EXPR
11202 || TREE_CODE (arg0) == MINUS_EXPR)
11203 && TREE_CODE (arg1) == MULT_EXPR)
11204 || ((TREE_CODE (arg1) == PLUS_EXPR
11205 || TREE_CODE (arg1) == MINUS_EXPR)
11206 && TREE_CODE (arg0) == MULT_EXPR)))
11207 {
11208 tree parg0, parg1, parg, marg;
11209 enum tree_code pcode;
11210
11211 if (TREE_CODE (arg1) == MULT_EXPR)
11212 parg = arg0, marg = arg1;
11213 else
11214 parg = arg1, marg = arg0;
11215 pcode = TREE_CODE (parg);
11216 parg0 = TREE_OPERAND (parg, 0);
11217 parg1 = TREE_OPERAND (parg, 1);
11218 STRIP_NOPS (parg0);
11219 STRIP_NOPS (parg1);
11220
11221 if (TREE_CODE (parg0) == MULT_EXPR
11222 && TREE_CODE (parg1) != MULT_EXPR)
11223 return fold_build2_loc (loc, pcode, type,
11224 fold_build2_loc (loc, PLUS_EXPR, type,
11225 fold_convert_loc (loc, type,
11226 parg0),
11227 fold_convert_loc (loc, type,
11228 marg)),
11229 fold_convert_loc (loc, type, parg1));
11230 if (TREE_CODE (parg0) != MULT_EXPR
11231 && TREE_CODE (parg1) == MULT_EXPR)
11232 return
11233 fold_build2_loc (loc, PLUS_EXPR, type,
11234 fold_convert_loc (loc, type, parg0),
11235 fold_build2_loc (loc, pcode, type,
11236 fold_convert_loc (loc, type, marg),
11237 fold_convert_loc (loc, type,
11238 parg1)));
11239 }
11240 }
11241 else
11242 {
11243 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11244 to __complex__ ( x, y ). This is not the same for SNaNs or
11245 if signed zeros are involved. */
11246 if (!HONOR_SNANS (arg0)
11247 && !HONOR_SIGNED_ZEROS (arg0)
11248 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11249 {
11250 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11251 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11252 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11253 bool arg0rz = false, arg0iz = false;
11254 if ((arg0r && (arg0rz = real_zerop (arg0r)))
11255 || (arg0i && (arg0iz = real_zerop (arg0i))))
11256 {
11257 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11258 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11259 if (arg0rz && arg1i && real_zerop (arg1i))
11260 {
11261 tree rp = arg1r ? arg1r
11262 : build1 (REALPART_EXPR, rtype, arg1);
11263 tree ip = arg0i ? arg0i
11264 : build1 (IMAGPART_EXPR, rtype, arg0);
11265 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11266 }
11267 else if (arg0iz && arg1r && real_zerop (arg1r))
11268 {
11269 tree rp = arg0r ? arg0r
11270 : build1 (REALPART_EXPR, rtype, arg0);
11271 tree ip = arg1i ? arg1i
11272 : build1 (IMAGPART_EXPR, rtype, arg1);
11273 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11274 }
11275 }
11276 }
11277
11278 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11279 We associate floats only if the user has specified
11280 -fassociative-math. */
11281 if (flag_associative_math
11282 && TREE_CODE (arg1) == PLUS_EXPR
11283 && TREE_CODE (arg0) != MULT_EXPR)
11284 {
11285 tree tree10 = TREE_OPERAND (arg1, 0);
11286 tree tree11 = TREE_OPERAND (arg1, 1);
11287 if (TREE_CODE (tree11) == MULT_EXPR
11288 && TREE_CODE (tree10) == MULT_EXPR)
11289 {
11290 tree tree0;
11291 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11292 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11293 }
11294 }
11295 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11296 We associate floats only if the user has specified
11297 -fassociative-math. */
11298 if (flag_associative_math
11299 && TREE_CODE (arg0) == PLUS_EXPR
11300 && TREE_CODE (arg1) != MULT_EXPR)
11301 {
11302 tree tree00 = TREE_OPERAND (arg0, 0);
11303 tree tree01 = TREE_OPERAND (arg0, 1);
11304 if (TREE_CODE (tree01) == MULT_EXPR
11305 && TREE_CODE (tree00) == MULT_EXPR)
11306 {
11307 tree tree0;
11308 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11309 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11310 }
11311 }
11312 }
11313
11314 bit_rotate:
11315 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11316 is a rotate of A by C1 bits. */
11317 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11318 is a rotate of A by B bits.
11319 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11320 though in this case CODE must be | and not + or ^, otherwise
11321 it doesn't return A when B is 0. */
11322 {
11323 enum tree_code code0, code1;
11324 tree rtype;
11325 code0 = TREE_CODE (arg0);
11326 code1 = TREE_CODE (arg1);
11327 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11328 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11329 && operand_equal_p (TREE_OPERAND (arg0, 0),
11330 TREE_OPERAND (arg1, 0), 0)
11331 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11332 TYPE_UNSIGNED (rtype))
11333 /* Only create rotates in complete modes. Other cases are not
11334 expanded properly. */
11335 && (element_precision (rtype)
11336 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11337 {
11338 tree tree01, tree11;
11339 tree orig_tree01, orig_tree11;
11340 enum tree_code code01, code11;
11341
11342 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11343 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11344 STRIP_NOPS (tree01);
11345 STRIP_NOPS (tree11);
11346 code01 = TREE_CODE (tree01);
11347 code11 = TREE_CODE (tree11);
11348 if (code11 != MINUS_EXPR
11349 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11350 {
11351 std::swap (code0, code1);
11352 std::swap (code01, code11);
11353 std::swap (tree01, tree11);
11354 std::swap (orig_tree01, orig_tree11);
11355 }
11356 if (code01 == INTEGER_CST
11357 && code11 == INTEGER_CST
11358 && (wi::to_widest (tree01) + wi::to_widest (tree11)
11359 == element_precision (rtype)))
11360 {
11361 tem = build2_loc (loc, LROTATE_EXPR,
11362 rtype, TREE_OPERAND (arg0, 0),
11363 code0 == LSHIFT_EXPR
11364 ? orig_tree01 : orig_tree11);
11365 return fold_convert_loc (loc, type, tem);
11366 }
11367 else if (code11 == MINUS_EXPR)
11368 {
11369 tree tree110, tree111;
11370 tree110 = TREE_OPERAND (tree11, 0);
11371 tree111 = TREE_OPERAND (tree11, 1);
11372 STRIP_NOPS (tree110);
11373 STRIP_NOPS (tree111);
11374 if (TREE_CODE (tree110) == INTEGER_CST
11375 && compare_tree_int (tree110,
11376 element_precision (rtype)) == 0
11377 && operand_equal_p (tree01, tree111, 0))
11378 {
11379 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11380 ? LROTATE_EXPR : RROTATE_EXPR),
11381 rtype, TREE_OPERAND (arg0, 0),
11382 orig_tree01);
11383 return fold_convert_loc (loc, type, tem);
11384 }
11385 }
11386 else if (code == BIT_IOR_EXPR
11387 && code11 == BIT_AND_EXPR
11388 && pow2p_hwi (element_precision (rtype)))
11389 {
11390 tree tree110, tree111;
11391 tree110 = TREE_OPERAND (tree11, 0);
11392 tree111 = TREE_OPERAND (tree11, 1);
11393 STRIP_NOPS (tree110);
11394 STRIP_NOPS (tree111);
11395 if (TREE_CODE (tree110) == NEGATE_EXPR
11396 && TREE_CODE (tree111) == INTEGER_CST
11397 && compare_tree_int (tree111,
11398 element_precision (rtype) - 1) == 0
11399 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11400 {
11401 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11402 ? LROTATE_EXPR : RROTATE_EXPR),
11403 rtype, TREE_OPERAND (arg0, 0),
11404 orig_tree01);
11405 return fold_convert_loc (loc, type, tem);
11406 }
11407 }
11408 }
11409 }
11410
11411 associate:
11412 /* In most languages, can't associate operations on floats through
11413 parentheses. Rather than remember where the parentheses were, we
11414 don't associate floats at all, unless the user has specified
11415 -fassociative-math.
11416 And, we need to make sure type is not saturating. */
11417
11418 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11419 && !TYPE_SATURATING (type)
11420 && !TYPE_OVERFLOW_SANITIZED (type))
11421 {
11422 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11423 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11424 tree atype = type;
11425 bool ok = true;
11426
11427 /* Split both trees into variables, constants, and literals. Then
11428 associate each group together, the constants with literals,
11429 then the result with variables. This increases the chances of
11430 literals being recombined later and of generating relocatable
11431 expressions for the sum of a constant and literal. */
11432 var0 = split_tree (arg0, type, code,
11433 &minus_var0, &con0, &minus_con0,
11434 &lit0, &minus_lit0, 0);
11435 var1 = split_tree (arg1, type, code,
11436 &minus_var1, &con1, &minus_con1,
11437 &lit1, &minus_lit1, code == MINUS_EXPR);
11438
11439 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11440 if (code == MINUS_EXPR)
11441 code = PLUS_EXPR;
11442
11443 /* With undefined overflow prefer doing association in a type
11444 which wraps on overflow, if that is one of the operand types. */
11445 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11446 && !TYPE_OVERFLOW_WRAPS (type))
11447 {
11448 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11449 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11450 atype = TREE_TYPE (arg0);
11451 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11452 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11453 atype = TREE_TYPE (arg1);
11454 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11455 }
11456
11457 /* With undefined overflow we can only associate constants with one
11458 variable, and constants whose association doesn't overflow. */
11459 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11460 && !TYPE_OVERFLOW_WRAPS (atype))
11461 {
11462 if ((var0 && var1) || (minus_var0 && minus_var1))
11463 {
11464 /* ??? If split_tree would handle NEGATE_EXPR we could
11465 simply reject these cases and the allowed cases would
11466 be the var0/minus_var1 ones. */
11467 tree tmp0 = var0 ? var0 : minus_var0;
11468 tree tmp1 = var1 ? var1 : minus_var1;
11469 bool one_neg = false;
11470
11471 if (TREE_CODE (tmp0) == NEGATE_EXPR)
11472 {
11473 tmp0 = TREE_OPERAND (tmp0, 0);
11474 one_neg = !one_neg;
11475 }
11476 if (CONVERT_EXPR_P (tmp0)
11477 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11478 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11479 <= TYPE_PRECISION (atype)))
11480 tmp0 = TREE_OPERAND (tmp0, 0);
11481 if (TREE_CODE (tmp1) == NEGATE_EXPR)
11482 {
11483 tmp1 = TREE_OPERAND (tmp1, 0);
11484 one_neg = !one_neg;
11485 }
11486 if (CONVERT_EXPR_P (tmp1)
11487 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11488 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11489 <= TYPE_PRECISION (atype)))
11490 tmp1 = TREE_OPERAND (tmp1, 0);
11491 /* The only case we can still associate with two variables
11492 is if they cancel out. */
11493 if (!one_neg
11494 || !operand_equal_p (tmp0, tmp1, 0))
11495 ok = false;
11496 }
11497 else if ((var0 && minus_var1
11498 && ! operand_equal_p (var0, minus_var1, 0))
11499 || (minus_var0 && var1
11500 && ! operand_equal_p (minus_var0, var1, 0)))
11501 ok = false;
11502 }
11503
11504 /* Only do something if we found more than two objects. Otherwise,
11505 nothing has changed and we risk infinite recursion. */
11506 if (ok
11507 && ((var0 != 0) + (var1 != 0)
11508 + (minus_var0 != 0) + (minus_var1 != 0)
11509 + (con0 != 0) + (con1 != 0)
11510 + (minus_con0 != 0) + (minus_con1 != 0)
11511 + (lit0 != 0) + (lit1 != 0)
11512 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11513 {
11514 int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11515 int minus_var0_origin
11516 = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11517 int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11518 int minus_con0_origin
11519 = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11520 int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11521 int minus_lit0_origin
11522 = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11523 var0 = associate_trees (loc, var0, var1, code, atype);
11524 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11525 code, atype);
11526 con0 = associate_trees (loc, con0, con1, code, atype);
11527 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11528 code, atype);
11529 lit0 = associate_trees (loc, lit0, lit1, code, atype);
11530 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11531 code, atype);
11532
11533 if (minus_var0 && var0)
11534 {
11535 var0_origin |= minus_var0_origin;
11536 var0 = associate_trees (loc, var0, minus_var0,
11537 MINUS_EXPR, atype);
11538 minus_var0 = 0;
11539 minus_var0_origin = 0;
11540 }
11541 if (minus_con0 && con0)
11542 {
11543 con0_origin |= minus_con0_origin;
11544 con0 = associate_trees (loc, con0, minus_con0,
11545 MINUS_EXPR, atype);
11546 minus_con0 = 0;
11547 minus_con0_origin = 0;
11548 }
11549
11550 /* Preserve the MINUS_EXPR if the negative part of the literal is
11551 greater than the positive part. Otherwise, the multiplicative
11552 folding code (i.e extract_muldiv) may be fooled in case
11553 unsigned constants are subtracted, like in the following
11554 example: ((X*2 + 4) - 8U)/2. */
11555 if (minus_lit0 && lit0)
11556 {
11557 if (TREE_CODE (lit0) == INTEGER_CST
11558 && TREE_CODE (minus_lit0) == INTEGER_CST
11559 && tree_int_cst_lt (lit0, minus_lit0)
11560 /* But avoid ending up with only negated parts. */
11561 && (var0 || con0))
11562 {
11563 minus_lit0_origin |= lit0_origin;
11564 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11565 MINUS_EXPR, atype);
11566 lit0 = 0;
11567 lit0_origin = 0;
11568 }
11569 else
11570 {
11571 lit0_origin |= minus_lit0_origin;
11572 lit0 = associate_trees (loc, lit0, minus_lit0,
11573 MINUS_EXPR, atype);
11574 minus_lit0 = 0;
11575 minus_lit0_origin = 0;
11576 }
11577 }
11578
11579 /* Don't introduce overflows through reassociation. */
11580 if ((lit0 && TREE_OVERFLOW_P (lit0))
11581 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11582 return NULL_TREE;
11583
11584 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11585 con0_origin |= lit0_origin;
11586 con0 = associate_trees (loc, con0, lit0, code, atype);
11587 minus_con0_origin |= minus_lit0_origin;
11588 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11589 code, atype);
11590
11591 /* Eliminate minus_con0. */
11592 if (minus_con0)
11593 {
11594 if (con0)
11595 {
11596 con0_origin |= minus_con0_origin;
11597 con0 = associate_trees (loc, con0, minus_con0,
11598 MINUS_EXPR, atype);
11599 }
11600 else if (var0)
11601 {
11602 var0_origin |= minus_con0_origin;
11603 var0 = associate_trees (loc, var0, minus_con0,
11604 MINUS_EXPR, atype);
11605 }
11606 else
11607 gcc_unreachable ();
11608 }
11609
11610 /* Eliminate minus_var0. */
11611 if (minus_var0)
11612 {
11613 if (con0)
11614 {
11615 con0_origin |= minus_var0_origin;
11616 con0 = associate_trees (loc, con0, minus_var0,
11617 MINUS_EXPR, atype);
11618 }
11619 else
11620 gcc_unreachable ();
11621 }
11622
11623 /* Reassociate only if there has been any actual association
11624 between subtrees from op0 and subtrees from op1 in at
11625 least one of the operands, otherwise we risk infinite
11626 recursion. See PR114084. */
11627 if (var0_origin != 3 && con0_origin != 3)
11628 return NULL_TREE;
11629
11630 return
11631 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11632 code, atype));
11633 }
11634 }
11635
11636 return NULL_TREE;
11637
11638 case POINTER_DIFF_EXPR:
11639 case MINUS_EXPR:
11640 /* Fold &a[i] - &a[j] to i-j. */
11641 if (TREE_CODE (arg0) == ADDR_EXPR
11642 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11643 && TREE_CODE (arg1) == ADDR_EXPR
11644 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11645 {
11646 tree tem = fold_addr_of_array_ref_difference (loc, type,
11647 TREE_OPERAND (arg0, 0),
11648 TREE_OPERAND (arg1, 0),
11649 code
11650 == POINTER_DIFF_EXPR);
11651 if (tem)
11652 return tem;
11653 }
11654
11655 /* Further transformations are not for pointers. */
11656 if (code == POINTER_DIFF_EXPR)
11657 return NULL_TREE;
11658
11659 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11660 if (TREE_CODE (arg0) == NEGATE_EXPR
11661 && negate_expr_p (op1)
11662 /* If arg0 is e.g. unsigned int and type is int, then this could
11663 introduce UB, because if A is INT_MIN at runtime, the original
11664 expression can be well defined while the latter is not.
11665 See PR83269. */
11666 && !(ANY_INTEGRAL_TYPE_P (type)
11667 && TYPE_OVERFLOW_UNDEFINED (type)
11668 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11669 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11670 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11671 fold_convert_loc (loc, type,
11672 TREE_OPERAND (arg0, 0)));
11673
11674 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11675 __complex__ ( x, -y ). This is not the same for SNaNs or if
11676 signed zeros are involved. */
11677 if (!HONOR_SNANS (arg0)
11678 && !HONOR_SIGNED_ZEROS (arg0)
11679 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11680 {
11681 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11682 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11683 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11684 bool arg0rz = false, arg0iz = false;
11685 if ((arg0r && (arg0rz = real_zerop (arg0r)))
11686 || (arg0i && (arg0iz = real_zerop (arg0i))))
11687 {
11688 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11689 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11690 if (arg0rz && arg1i && real_zerop (arg1i))
11691 {
11692 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11693 arg1r ? arg1r
11694 : build1 (REALPART_EXPR, rtype, arg1));
11695 tree ip = arg0i ? arg0i
11696 : build1 (IMAGPART_EXPR, rtype, arg0);
11697 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11698 }
11699 else if (arg0iz && arg1r && real_zerop (arg1r))
11700 {
11701 tree rp = arg0r ? arg0r
11702 : build1 (REALPART_EXPR, rtype, arg0);
11703 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11704 arg1i ? arg1i
11705 : build1 (IMAGPART_EXPR, rtype, arg1));
11706 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11707 }
11708 }
11709 }
11710
11711 /* A - B -> A + (-B) if B is easily negatable. */
11712 if (negate_expr_p (op1)
11713 && ! TYPE_OVERFLOW_SANITIZED (type)
11714 && ((FLOAT_TYPE_P (type)
11715 /* Avoid this transformation if B is a positive REAL_CST. */
11716 && (TREE_CODE (op1) != REAL_CST
11717 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11718 || INTEGRAL_TYPE_P (type)))
11719 return fold_build2_loc (loc, PLUS_EXPR, type,
11720 fold_convert_loc (loc, type, arg0),
11721 negate_expr (op1));
11722
11723 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11724 one. Make sure the type is not saturating and has the signedness of
11725 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11726 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11727 if ((TREE_CODE (arg0) == MULT_EXPR
11728 || TREE_CODE (arg1) == MULT_EXPR)
11729 && !TYPE_SATURATING (type)
11730 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11731 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11732 && (!FLOAT_TYPE_P (type) || flag_associative_math))
11733 {
11734 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11735 if (tem)
11736 return tem;
11737 }
11738
11739 goto associate;
11740
11741 case MULT_EXPR:
11742 if (! FLOAT_TYPE_P (type))
11743 {
11744 /* Transform x * -C into -x * C if x is easily negatable. */
11745 if (TREE_CODE (op1) == INTEGER_CST
11746 && tree_int_cst_sgn (op1) == -1
11747 && negate_expr_p (op0)
11748 && negate_expr_p (op1)
11749 && (tem = negate_expr (op1)) != op1
11750 && ! TREE_OVERFLOW (tem))
11751 return fold_build2_loc (loc, MULT_EXPR, type,
11752 fold_convert_loc (loc, type,
11753 negate_expr (op0)), tem);
11754
11755 strict_overflow_p = false;
11756 if (TREE_CODE (arg1) == INTEGER_CST
11757 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11758 &strict_overflow_p)) != 0)
11759 {
11760 if (strict_overflow_p)
11761 fold_overflow_warning (("assuming signed overflow does not "
11762 "occur when simplifying "
11763 "multiplication"),
11764 WARN_STRICT_OVERFLOW_MISC);
11765 return fold_convert_loc (loc, type, tem);
11766 }
11767
11768 /* Optimize z * conj(z) for integer complex numbers. */
11769 if (TREE_CODE (arg0) == CONJ_EXPR
11770 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11771 return fold_mult_zconjz (loc, type, arg1);
11772 if (TREE_CODE (arg1) == CONJ_EXPR
11773 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11774 return fold_mult_zconjz (loc, type, arg0);
11775 }
11776 else
11777 {
11778 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11779 This is not the same for NaNs or if signed zeros are
11780 involved. */
11781 if (!HONOR_NANS (arg0)
11782 && !HONOR_SIGNED_ZEROS (arg0)
11783 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11784 && TREE_CODE (arg1) == COMPLEX_CST
11785 && real_zerop (TREE_REALPART (arg1)))
11786 {
11787 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11788 if (real_onep (TREE_IMAGPART (arg1)))
11789 {
11790 if (TREE_CODE (arg0) != COMPLEX_EXPR)
11791 arg0 = save_expr (arg0);
11792 tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11793 rtype, arg0);
11794 tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11795 rtype, arg0);
11796 return fold_build2_loc (loc, COMPLEX_EXPR, type,
11797 negate_expr (iarg0),
11798 rarg0);
11799 }
11800 else if (real_minus_onep (TREE_IMAGPART (arg1)))
11801 {
11802 if (TREE_CODE (arg0) != COMPLEX_EXPR)
11803 arg0 = save_expr (arg0);
11804 tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11805 rtype, arg0);
11806 tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11807 rtype, arg0);
11808 return fold_build2_loc (loc, COMPLEX_EXPR, type,
11809 iarg0,
11810 negate_expr (rarg0));
11811 }
11812 }
11813
11814 /* Optimize z * conj(z) for floating point complex numbers.
11815 Guarded by flag_unsafe_math_optimizations as non-finite
11816 imaginary components don't produce scalar results. */
11817 if (flag_unsafe_math_optimizations
11818 && TREE_CODE (arg0) == CONJ_EXPR
11819 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11820 return fold_mult_zconjz (loc, type, arg1);
11821 if (flag_unsafe_math_optimizations
11822 && TREE_CODE (arg1) == CONJ_EXPR
11823 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11824 return fold_mult_zconjz (loc, type, arg0);
11825 }
11826 goto associate;
11827
11828 case BIT_IOR_EXPR:
11829 /* Canonicalize (X & C1) | C2. */
11830 if (TREE_CODE (arg0) == BIT_AND_EXPR
11831 && TREE_CODE (arg1) == INTEGER_CST
11832 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11833 {
11834 int width = TYPE_PRECISION (type), w;
11835 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11836 wide_int c2 = wi::to_wide (arg1);
11837
11838 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11839 if ((c1 & c2) == c1)
11840 return omit_one_operand_loc (loc, type, arg1,
11841 TREE_OPERAND (arg0, 0));
11842
11843 wide_int msk = wi::mask (width, false,
11844 TYPE_PRECISION (TREE_TYPE (arg1)));
11845
11846 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11847 if (wi::bit_and_not (msk, c1 | c2) == 0)
11848 {
11849 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11850 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11851 }
11852
11853 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11854 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11855 mode which allows further optimizations. */
11856 c1 &= msk;
11857 c2 &= msk;
11858 wide_int c3 = wi::bit_and_not (c1, c2);
11859 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11860 {
11861 wide_int mask = wi::mask (w, false,
11862 TYPE_PRECISION (type));
11863 if (((c1 | c2) & mask) == mask
11864 && wi::bit_and_not (c1, mask) == 0)
11865 {
11866 c3 = mask;
11867 break;
11868 }
11869 }
11870
11871 if (c3 != c1)
11872 {
11873 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11874 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11875 wide_int_to_tree (type, c3));
11876 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11877 }
11878 }
11879
11880 /* See if this can be simplified into a rotate first. If that
11881 is unsuccessful continue in the association code. */
11882 goto bit_rotate;
11883
11884 case BIT_XOR_EXPR:
11885 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11886 if (TREE_CODE (arg0) == BIT_AND_EXPR
11887 && INTEGRAL_TYPE_P (type)
11888 && integer_onep (TREE_OPERAND (arg0, 1))
11889 && integer_onep (arg1))
11890 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11891 build_zero_cst (TREE_TYPE (arg0)));
11892
11893 /* See if this can be simplified into a rotate first. If that
11894 is unsuccessful continue in the association code. */
11895 goto bit_rotate;
11896
11897 case BIT_AND_EXPR:
11898 /* Fold !X & 1 as X == 0. */
11899 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11900 && integer_onep (arg1))
11901 {
11902 tem = TREE_OPERAND (arg0, 0);
11903 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11904 build_zero_cst (TREE_TYPE (tem)));
11905 }
11906
11907 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11908 multiple of 1 << CST. */
11909 if (TREE_CODE (arg1) == INTEGER_CST)
11910 {
11911 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11912 wide_int ncst1 = -cst1;
11913 if ((cst1 & ncst1) == ncst1
11914 && multiple_of_p (type, arg0,
11915 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11916 return fold_convert_loc (loc, type, arg0);
11917 }
11918
11919 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11920 bits from CST2. */
11921 if (TREE_CODE (arg1) == INTEGER_CST
11922 && TREE_CODE (arg0) == MULT_EXPR
11923 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11924 {
11925 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11926 wide_int masked
11927 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11928
11929 if (masked == 0)
11930 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11931 arg0, arg1);
11932 else if (masked != warg1)
11933 {
11934 /* Avoid the transform if arg1 is a mask of some
11935 mode which allows further optimizations. */
11936 int pop = wi::popcount (warg1);
11937 if (!(pop >= BITS_PER_UNIT
11938 && pow2p_hwi (pop)
11939 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11940 return fold_build2_loc (loc, code, type, op0,
11941 wide_int_to_tree (type, masked));
11942 }
11943 }
11944
11945 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11946 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11947 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11948 {
11949 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11950
11951 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11952 if (mask == -1)
11953 return
11954 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11955 }
11956
11957 goto associate;
11958
11959 case RDIV_EXPR:
11960 /* Don't touch a floating-point divide by zero unless the mode
11961 of the constant can represent infinity. */
11962 if (TREE_CODE (arg1) == REAL_CST
11963 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11964 && real_zerop (arg1))
11965 return NULL_TREE;
11966
11967 /* (-A) / (-B) -> A / B */
11968 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11969 return fold_build2_loc (loc, RDIV_EXPR, type,
11970 TREE_OPERAND (arg0, 0),
11971 negate_expr (arg1));
11972 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11973 return fold_build2_loc (loc, RDIV_EXPR, type,
11974 negate_expr (arg0),
11975 TREE_OPERAND (arg1, 0));
11976 return NULL_TREE;
11977
11978 case TRUNC_DIV_EXPR:
11979 /* Fall through */
11980
11981 case FLOOR_DIV_EXPR:
11982 /* Simplify A / (B << N) where A and B are positive and B is
11983 a power of 2, to A >> (N + log2(B)). */
11984 strict_overflow_p = false;
11985 if (TREE_CODE (arg1) == LSHIFT_EXPR
11986 && (TYPE_UNSIGNED (type)
11987 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11988 {
11989 tree sval = TREE_OPERAND (arg1, 0);
11990 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11991 {
11992 tree sh_cnt = TREE_OPERAND (arg1, 1);
11993 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11994 wi::exact_log2 (wi::to_wide (sval)));
11995
11996 if (strict_overflow_p)
11997 fold_overflow_warning (("assuming signed overflow does not "
11998 "occur when simplifying A / (B << N)"),
11999 WARN_STRICT_OVERFLOW_MISC);
12000
12001 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12002 sh_cnt, pow2);
12003 return fold_build2_loc (loc, RSHIFT_EXPR, type,
12004 fold_convert_loc (loc, type, arg0), sh_cnt);
12005 }
12006 }
12007
12008 /* Fall through */
12009
12010 case ROUND_DIV_EXPR:
12011 case CEIL_DIV_EXPR:
12012 case EXACT_DIV_EXPR:
12013 if (integer_zerop (arg1))
12014 return NULL_TREE;
12015
12016 /* Convert -A / -B to A / B when the type is signed and overflow is
12017 undefined. */
12018 if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12019 && TREE_CODE (op0) == NEGATE_EXPR
12020 && negate_expr_p (op1))
12021 {
12022 if (ANY_INTEGRAL_TYPE_P (type))
12023 fold_overflow_warning (("assuming signed overflow does not occur "
12024 "when distributing negation across "
12025 "division"),
12026 WARN_STRICT_OVERFLOW_MISC);
12027 return fold_build2_loc (loc, code, type,
12028 fold_convert_loc (loc, type,
12029 TREE_OPERAND (arg0, 0)),
12030 negate_expr (op1));
12031 }
12032 if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12033 && TREE_CODE (arg1) == NEGATE_EXPR
12034 && negate_expr_p (op0))
12035 {
12036 if (ANY_INTEGRAL_TYPE_P (type))
12037 fold_overflow_warning (("assuming signed overflow does not occur "
12038 "when distributing negation across "
12039 "division"),
12040 WARN_STRICT_OVERFLOW_MISC);
12041 return fold_build2_loc (loc, code, type,
12042 negate_expr (op0),
12043 fold_convert_loc (loc, type,
12044 TREE_OPERAND (arg1, 0)));
12045 }
12046
12047 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12048 operation, EXACT_DIV_EXPR.
12049
12050 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12051 At one time others generated faster code, it's not clear if they do
12052 after the last round to changes to the DIV code in expmed.cc. */
12053 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12054 && multiple_of_p (type, arg0, arg1))
12055 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12056 fold_convert (type, arg0),
12057 fold_convert (type, arg1));
12058
12059 strict_overflow_p = false;
12060 if (TREE_CODE (arg1) == INTEGER_CST
12061 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12062 &strict_overflow_p)) != 0)
12063 {
12064 if (strict_overflow_p)
12065 fold_overflow_warning (("assuming signed overflow does not occur "
12066 "when simplifying division"),
12067 WARN_STRICT_OVERFLOW_MISC);
12068 return fold_convert_loc (loc, type, tem);
12069 }
12070
12071 return NULL_TREE;
12072
12073 case CEIL_MOD_EXPR:
12074 case FLOOR_MOD_EXPR:
12075 case ROUND_MOD_EXPR:
12076 case TRUNC_MOD_EXPR:
12077 strict_overflow_p = false;
12078 if (TREE_CODE (arg1) == INTEGER_CST
12079 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12080 &strict_overflow_p)) != 0)
12081 {
12082 if (strict_overflow_p)
12083 fold_overflow_warning (("assuming signed overflow does not occur "
12084 "when simplifying modulus"),
12085 WARN_STRICT_OVERFLOW_MISC);
12086 return fold_convert_loc (loc, type, tem);
12087 }
12088
12089 return NULL_TREE;
12090
12091 case LROTATE_EXPR:
12092 case RROTATE_EXPR:
12093 case RSHIFT_EXPR:
12094 case LSHIFT_EXPR:
12095 /* Since negative shift count is not well-defined,
12096 don't try to compute it in the compiler. */
12097 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12098 return NULL_TREE;
12099
12100 prec = element_precision (type);
12101
12102 /* If we have a rotate of a bit operation with the rotate count and
12103 the second operand of the bit operation both constant,
12104 permute the two operations. */
12105 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12106 && (TREE_CODE (arg0) == BIT_AND_EXPR
12107 || TREE_CODE (arg0) == BIT_IOR_EXPR
12108 || TREE_CODE (arg0) == BIT_XOR_EXPR)
12109 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12110 {
12111 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12112 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12113 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12114 fold_build2_loc (loc, code, type,
12115 arg00, arg1),
12116 fold_build2_loc (loc, code, type,
12117 arg01, arg1));
12118 }
12119
12120 return NULL_TREE;
12121
12122 case MIN_EXPR:
12123 case MAX_EXPR:
12124 goto associate;
12125
12126 case TRUTH_ANDIF_EXPR:
12127 /* Note that the operands of this must be ints
12128 and their values must be 0 or 1.
12129 ("true" is a fixed value perhaps depending on the language.) */
12130 /* If first arg is constant zero, return it. */
12131 if (integer_zerop (arg0))
12132 return fold_convert_loc (loc, type, arg0);
12133 /* FALLTHRU */
12134 case TRUTH_AND_EXPR:
12135 /* If either arg is constant true, drop it. */
12136 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12137 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12138 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12139 /* Preserve sequence points. */
12140 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12141 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12142 /* If second arg is constant zero, result is zero, but first arg
12143 must be evaluated. */
12144 if (integer_zerop (arg1))
12145 return omit_one_operand_loc (loc, type, arg1, arg0);
12146 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12147 case will be handled here. */
12148 if (integer_zerop (arg0))
12149 return omit_one_operand_loc (loc, type, arg0, arg1);
12150
12151 /* !X && X is always false. */
12152 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12153 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12154 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12155 /* X && !X is always false. */
12156 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12157 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12158 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12159
12160 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12161 means A >= Y && A != MAX, but in this case we know that
12162 A < X <= MAX. */
12163
12164 if (!TREE_SIDE_EFFECTS (arg0)
12165 && !TREE_SIDE_EFFECTS (arg1))
12166 {
12167 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12168 if (tem && !operand_equal_p (tem, arg0, 0))
12169 return fold_convert (type,
12170 fold_build2_loc (loc, code, TREE_TYPE (arg1),
12171 tem, arg1));
12172
12173 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12174 if (tem && !operand_equal_p (tem, arg1, 0))
12175 return fold_convert (type,
12176 fold_build2_loc (loc, code, TREE_TYPE (arg0),
12177 arg0, tem));
12178 }
12179
12180 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12181 != NULL_TREE)
12182 return tem;
12183
12184 return NULL_TREE;
12185
12186 case TRUTH_ORIF_EXPR:
12187 /* Note that the operands of this must be ints
12188 and their values must be 0 or true.
12189 ("true" is a fixed value perhaps depending on the language.) */
12190 /* If first arg is constant true, return it. */
12191 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12192 return fold_convert_loc (loc, type, arg0);
12193 /* FALLTHRU */
12194 case TRUTH_OR_EXPR:
12195 /* If either arg is constant zero, drop it. */
12196 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12197 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12198 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12199 /* Preserve sequence points. */
12200 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12201 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12202 /* If second arg is constant true, result is true, but we must
12203 evaluate first arg. */
12204 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12205 return omit_one_operand_loc (loc, type, arg1, arg0);
12206 /* Likewise for first arg, but note this only occurs here for
12207 TRUTH_OR_EXPR. */
12208 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12209 return omit_one_operand_loc (loc, type, arg0, arg1);
12210
12211 /* !X || X is always true. */
12212 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12213 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12214 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12215 /* X || !X is always true. */
12216 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12217 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12218 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12219
12220 /* (X && !Y) || (!X && Y) is X ^ Y */
12221 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12222 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12223 {
12224 tree a0, a1, l0, l1, n0, n1;
12225
12226 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12227 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12228
12229 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12230 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12231
12232 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12233 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12234
12235 if ((operand_equal_p (n0, a0, 0)
12236 && operand_equal_p (n1, a1, 0))
12237 || (operand_equal_p (n0, a1, 0)
12238 && operand_equal_p (n1, a0, 0)))
12239 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12240 }
12241
12242 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12243 != NULL_TREE)
12244 return tem;
12245
12246 return NULL_TREE;
12247
12248 case TRUTH_XOR_EXPR:
12249 /* If the second arg is constant zero, drop it. */
12250 if (integer_zerop (arg1))
12251 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12252 /* If the second arg is constant true, this is a logical inversion. */
12253 if (integer_onep (arg1))
12254 {
12255 tem = invert_truthvalue_loc (loc, arg0);
12256 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12257 }
12258 /* Identical arguments cancel to zero. */
12259 if (operand_equal_p (arg0, arg1, 0))
12260 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12261
12262 /* !X ^ X is always true. */
12263 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12264 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12265 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12266
12267 /* X ^ !X is always true. */
12268 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12269 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12270 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12271
12272 return NULL_TREE;
12273
12274 case EQ_EXPR:
12275 case NE_EXPR:
12276 STRIP_NOPS (arg0);
12277 STRIP_NOPS (arg1);
12278
12279 tem = fold_comparison (loc, code, type, op0, op1);
12280 if (tem != NULL_TREE)
12281 return tem;
12282
12283 /* bool_var != 1 becomes !bool_var. */
12284 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12285 && code == NE_EXPR)
12286 return fold_convert_loc (loc, type,
12287 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12288 TREE_TYPE (arg0), arg0));
12289
12290 /* bool_var == 0 becomes !bool_var. */
12291 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12292 && code == EQ_EXPR)
12293 return fold_convert_loc (loc, type,
12294 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12295 TREE_TYPE (arg0), arg0));
12296
12297 /* !exp != 0 becomes !exp */
12298 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12299 && code == NE_EXPR)
12300 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12301
12302 /* If this is an EQ or NE comparison with zero and ARG0 is
12303 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12304 two operations, but the latter can be done in one less insn
12305 on machines that have only two-operand insns or on which a
12306 constant cannot be the first operand. */
12307 if (TREE_CODE (arg0) == BIT_AND_EXPR
12308 && integer_zerop (arg1))
12309 {
12310 tree arg00 = TREE_OPERAND (arg0, 0);
12311 tree arg01 = TREE_OPERAND (arg0, 1);
12312 if (TREE_CODE (arg00) == LSHIFT_EXPR
12313 && integer_onep (TREE_OPERAND (arg00, 0)))
12314 {
12315 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12316 arg01, TREE_OPERAND (arg00, 1));
12317 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12318 build_one_cst (TREE_TYPE (arg0)));
12319 return fold_build2_loc (loc, code, type,
12320 fold_convert_loc (loc, TREE_TYPE (arg1),
12321 tem), arg1);
12322 }
12323 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12324 && integer_onep (TREE_OPERAND (arg01, 0)))
12325 {
12326 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12327 arg00, TREE_OPERAND (arg01, 1));
12328 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12329 build_one_cst (TREE_TYPE (arg0)));
12330 return fold_build2_loc (loc, code, type,
12331 fold_convert_loc (loc, TREE_TYPE (arg1),
12332 tem), arg1);
12333 }
12334 }
12335
12336 /* If this is a comparison of a field, we may be able to simplify it. */
12337 if ((TREE_CODE (arg0) == COMPONENT_REF
12338 || TREE_CODE (arg0) == BIT_FIELD_REF)
12339 /* Handle the constant case even without -O
12340 to make sure the warnings are given. */
12341 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12342 {
12343 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12344 if (t1)
12345 return t1;
12346 }
12347
12348 /* Optimize comparisons of strlen vs zero to a compare of the
12349 first character of the string vs zero. To wit,
12350 strlen(ptr) == 0 => *ptr == 0
12351 strlen(ptr) != 0 => *ptr != 0
12352 Other cases should reduce to one of these two (or a constant)
12353 due to the return value of strlen being unsigned. */
12354 if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12355 {
12356 tree fndecl = get_callee_fndecl (arg0);
12357
12358 if (fndecl
12359 && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12360 && call_expr_nargs (arg0) == 1
12361 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12362 == POINTER_TYPE))
12363 {
12364 tree ptrtype
12365 = build_pointer_type (build_qualified_type (char_type_node,
12366 TYPE_QUAL_CONST));
12367 tree ptr = fold_convert_loc (loc, ptrtype,
12368 CALL_EXPR_ARG (arg0, 0));
12369 tree iref = build_fold_indirect_ref_loc (loc, ptr);
12370 return fold_build2_loc (loc, code, type, iref,
12371 build_int_cst (TREE_TYPE (iref), 0));
12372 }
12373 }
12374
12375 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12376 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12377 if (TREE_CODE (arg0) == RSHIFT_EXPR
12378 && integer_zerop (arg1)
12379 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12380 {
12381 tree arg00 = TREE_OPERAND (arg0, 0);
12382 tree arg01 = TREE_OPERAND (arg0, 1);
12383 tree itype = TREE_TYPE (arg00);
12384 if (wi::to_wide (arg01) == element_precision (itype) - 1)
12385 {
12386 if (TYPE_UNSIGNED (itype))
12387 {
12388 itype = signed_type_for (itype);
12389 arg00 = fold_convert_loc (loc, itype, arg00);
12390 }
12391 enum tree_code code2 = code == EQ_EXPR ? GE_EXPR : LT_EXPR;
12392 /* Make sure to transform vector compares only to supported
12393 ones or from unsupported ones and check that only after
12394 IPA so offloaded code is handled correctly in this regard. */
12395 if (!VECTOR_TYPE_P (itype)
12396 || (cfun
12397 && cfun->after_inlining
12398 /* We can jump on EQ/NE but not GE/LT. */
12399 && VECTOR_BOOLEAN_TYPE_P (type)
12400 && (expand_vec_cmp_expr_p (itype, type, code2)
12401 || !expand_vec_cmp_expr_p (TREE_TYPE (op0),
12402 type, code))))
12403 return fold_build2_loc (loc, code2,
12404 type, arg00, build_zero_cst (itype));
12405 }
12406 }
12407
12408 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12409 (X & C) == 0 when C is a single bit. */
12410 if (TREE_CODE (arg0) == BIT_AND_EXPR
12411 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12412 && integer_zerop (arg1)
12413 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12414 {
12415 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12416 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12417 TREE_OPERAND (arg0, 1));
12418 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12419 type, tem,
12420 fold_convert_loc (loc, TREE_TYPE (arg0),
12421 arg1));
12422 }
12423
12424 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12425 constant C is a power of two, i.e. a single bit. */
12426 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12427 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12428 && integer_zerop (arg1)
12429 && integer_pow2p (TREE_OPERAND (arg0, 1))
12430 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12431 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12432 {
12433 tree arg00 = TREE_OPERAND (arg0, 0);
12434 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12435 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12436 }
12437
12438 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12439 when is C is a power of two, i.e. a single bit. */
12440 if (TREE_CODE (arg0) == BIT_AND_EXPR
12441 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12442 && integer_zerop (arg1)
12443 && integer_pow2p (TREE_OPERAND (arg0, 1))
12444 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12445 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12446 {
12447 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12448 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12449 arg000, TREE_OPERAND (arg0, 1));
12450 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12451 tem, build_int_cst (TREE_TYPE (tem), 0));
12452 }
12453
12454 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12455 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12456 {
12457 tree arg00 = TREE_OPERAND (arg0, 0);
12458 tree arg01 = TREE_OPERAND (arg0, 1);
12459 tree arg10 = TREE_OPERAND (arg1, 0);
12460 tree arg11 = TREE_OPERAND (arg1, 1);
12461 tree itype = TREE_TYPE (arg0);
12462
12463 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12464 operand_equal_p guarantees no side-effects so we don't need
12465 to use omit_one_operand on Z. */
12466 if (operand_equal_p (arg01, arg11, 0))
12467 return fold_build2_loc (loc, code, type, arg00,
12468 fold_convert_loc (loc, TREE_TYPE (arg00),
12469 arg10));
12470 if (operand_equal_p (arg01, arg10, 0))
12471 return fold_build2_loc (loc, code, type, arg00,
12472 fold_convert_loc (loc, TREE_TYPE (arg00),
12473 arg11));
12474 if (operand_equal_p (arg00, arg11, 0))
12475 return fold_build2_loc (loc, code, type, arg01,
12476 fold_convert_loc (loc, TREE_TYPE (arg01),
12477 arg10));
12478 if (operand_equal_p (arg00, arg10, 0))
12479 return fold_build2_loc (loc, code, type, arg01,
12480 fold_convert_loc (loc, TREE_TYPE (arg01),
12481 arg11));
12482
12483 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12484 if (TREE_CODE (arg01) == INTEGER_CST
12485 && TREE_CODE (arg11) == INTEGER_CST)
12486 {
12487 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12488 fold_convert_loc (loc, itype, arg11));
12489 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12490 return fold_build2_loc (loc, code, type, tem,
12491 fold_convert_loc (loc, itype, arg10));
12492 }
12493 }
12494
12495 /* Attempt to simplify equality/inequality comparisons of complex
12496 values. Only lower the comparison if the result is known or
12497 can be simplified to a single scalar comparison. */
12498 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12499 || TREE_CODE (arg0) == COMPLEX_CST)
12500 && (TREE_CODE (arg1) == COMPLEX_EXPR
12501 || TREE_CODE (arg1) == COMPLEX_CST))
12502 {
12503 tree real0, imag0, real1, imag1;
12504 tree rcond, icond;
12505
12506 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12507 {
12508 real0 = TREE_OPERAND (arg0, 0);
12509 imag0 = TREE_OPERAND (arg0, 1);
12510 }
12511 else
12512 {
12513 real0 = TREE_REALPART (arg0);
12514 imag0 = TREE_IMAGPART (arg0);
12515 }
12516
12517 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12518 {
12519 real1 = TREE_OPERAND (arg1, 0);
12520 imag1 = TREE_OPERAND (arg1, 1);
12521 }
12522 else
12523 {
12524 real1 = TREE_REALPART (arg1);
12525 imag1 = TREE_IMAGPART (arg1);
12526 }
12527
12528 rcond = fold_binary_loc (loc, code, type, real0, real1);
12529 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12530 {
12531 if (integer_zerop (rcond))
12532 {
12533 if (code == EQ_EXPR)
12534 return omit_two_operands_loc (loc, type, boolean_false_node,
12535 imag0, imag1);
12536 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12537 }
12538 else
12539 {
12540 if (code == NE_EXPR)
12541 return omit_two_operands_loc (loc, type, boolean_true_node,
12542 imag0, imag1);
12543 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12544 }
12545 }
12546
12547 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12548 if (icond && TREE_CODE (icond) == INTEGER_CST)
12549 {
12550 if (integer_zerop (icond))
12551 {
12552 if (code == EQ_EXPR)
12553 return omit_two_operands_loc (loc, type, boolean_false_node,
12554 real0, real1);
12555 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12556 }
12557 else
12558 {
12559 if (code == NE_EXPR)
12560 return omit_two_operands_loc (loc, type, boolean_true_node,
12561 real0, real1);
12562 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12563 }
12564 }
12565 }
12566
12567 return NULL_TREE;
12568
12569 case LT_EXPR:
12570 case GT_EXPR:
12571 case LE_EXPR:
12572 case GE_EXPR:
12573 tem = fold_comparison (loc, code, type, op0, op1);
12574 if (tem != NULL_TREE)
12575 return tem;
12576
12577 /* Transform comparisons of the form X +- C CMP X. */
12578 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12579 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12580 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12581 && !HONOR_SNANS (arg0))
12582 {
12583 tree arg01 = TREE_OPERAND (arg0, 1);
12584 enum tree_code code0 = TREE_CODE (arg0);
12585 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12586
12587 /* (X - c) > X becomes false. */
12588 if (code == GT_EXPR
12589 && ((code0 == MINUS_EXPR && is_positive >= 0)
12590 || (code0 == PLUS_EXPR && is_positive <= 0)))
12591 return constant_boolean_node (0, type);
12592
12593 /* Likewise (X + c) < X becomes false. */
12594 if (code == LT_EXPR
12595 && ((code0 == PLUS_EXPR && is_positive >= 0)
12596 || (code0 == MINUS_EXPR && is_positive <= 0)))
12597 return constant_boolean_node (0, type);
12598
12599 /* Convert (X - c) <= X to true. */
12600 if (!HONOR_NANS (arg1)
12601 && code == LE_EXPR
12602 && ((code0 == MINUS_EXPR && is_positive >= 0)
12603 || (code0 == PLUS_EXPR && is_positive <= 0)))
12604 return constant_boolean_node (1, type);
12605
12606 /* Convert (X + c) >= X to true. */
12607 if (!HONOR_NANS (arg1)
12608 && code == GE_EXPR
12609 && ((code0 == PLUS_EXPR && is_positive >= 0)
12610 || (code0 == MINUS_EXPR && is_positive <= 0)))
12611 return constant_boolean_node (1, type);
12612 }
12613
12614 /* If we are comparing an ABS_EXPR with a constant, we can
12615 convert all the cases into explicit comparisons, but they may
12616 well not be faster than doing the ABS and one comparison.
12617 But ABS (X) <= C is a range comparison, which becomes a subtraction
12618 and a comparison, and is probably faster. */
12619 if (code == LE_EXPR
12620 && TREE_CODE (arg1) == INTEGER_CST
12621 && TREE_CODE (arg0) == ABS_EXPR
12622 && ! TREE_SIDE_EFFECTS (arg0)
12623 && (tem = negate_expr (arg1)) != 0
12624 && TREE_CODE (tem) == INTEGER_CST
12625 && !TREE_OVERFLOW (tem))
12626 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12627 build2 (GE_EXPR, type,
12628 TREE_OPERAND (arg0, 0), tem),
12629 build2 (LE_EXPR, type,
12630 TREE_OPERAND (arg0, 0), arg1));
12631
12632 /* Convert ABS_EXPR<x> >= 0 to true. */
12633 strict_overflow_p = false;
12634 if (code == GE_EXPR
12635 && (integer_zerop (arg1)
12636 || (! HONOR_NANS (arg0)
12637 && real_zerop (arg1)))
12638 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12639 {
12640 if (strict_overflow_p)
12641 fold_overflow_warning (("assuming signed overflow does not occur "
12642 "when simplifying comparison of "
12643 "absolute value and zero"),
12644 WARN_STRICT_OVERFLOW_CONDITIONAL);
12645 return omit_one_operand_loc (loc, type,
12646 constant_boolean_node (true, type),
12647 arg0);
12648 }
12649
12650 /* Convert ABS_EXPR<x> < 0 to false. */
12651 strict_overflow_p = false;
12652 if (code == LT_EXPR
12653 && (integer_zerop (arg1) || real_zerop (arg1))
12654 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12655 {
12656 if (strict_overflow_p)
12657 fold_overflow_warning (("assuming signed overflow does not occur "
12658 "when simplifying comparison of "
12659 "absolute value and zero"),
12660 WARN_STRICT_OVERFLOW_CONDITIONAL);
12661 return omit_one_operand_loc (loc, type,
12662 constant_boolean_node (false, type),
12663 arg0);
12664 }
12665
12666 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12667 and similarly for >= into !=. */
12668 if ((code == LT_EXPR || code == GE_EXPR)
12669 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12670 && TREE_CODE (arg1) == LSHIFT_EXPR
12671 && integer_onep (TREE_OPERAND (arg1, 0)))
12672 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12673 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12674 TREE_OPERAND (arg1, 1)),
12675 build_zero_cst (TREE_TYPE (arg0)));
12676
12677 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12678 otherwise Y might be >= # of bits in X's type and thus e.g.
12679 (unsigned char) (1 << Y) for Y 15 might be 0.
12680 If the cast is widening, then 1 << Y should have unsigned type,
12681 otherwise if Y is number of bits in the signed shift type minus 1,
12682 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12683 31 might be 0xffffffff80000000. */
12684 if ((code == LT_EXPR || code == GE_EXPR)
12685 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12686 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12687 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12688 && CONVERT_EXPR_P (arg1)
12689 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12690 && (element_precision (TREE_TYPE (arg1))
12691 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12692 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12693 || (element_precision (TREE_TYPE (arg1))
12694 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12695 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12696 {
12697 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12698 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12699 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12700 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12701 build_zero_cst (TREE_TYPE (arg0)));
12702 }
12703
12704 return NULL_TREE;
12705
12706 case UNORDERED_EXPR:
12707 case ORDERED_EXPR:
12708 case UNLT_EXPR:
12709 case UNLE_EXPR:
12710 case UNGT_EXPR:
12711 case UNGE_EXPR:
12712 case UNEQ_EXPR:
12713 case LTGT_EXPR:
12714 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12715 {
12716 tree targ0 = strip_float_extensions (arg0);
12717 tree targ1 = strip_float_extensions (arg1);
12718 tree newtype = TREE_TYPE (targ0);
12719
12720 if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12721 newtype = TREE_TYPE (targ1);
12722
12723 if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12724 && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12725 return fold_build2_loc (loc, code, type,
12726 fold_convert_loc (loc, newtype, targ0),
12727 fold_convert_loc (loc, newtype, targ1));
12728 }
12729
12730 return NULL_TREE;
12731
12732 case COMPOUND_EXPR:
12733 /* When pedantic, a compound expression can be neither an lvalue
12734 nor an integer constant expression. */
12735 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12736 return NULL_TREE;
12737 /* Don't let (0, 0) be null pointer constant. */
12738 tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12739 : fold_convert_loc (loc, type, arg1);
12740 return tem;
12741
12742 default:
12743 return NULL_TREE;
12744 } /* switch (code) */
12745 }
12746
12747 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12748 ((A & N) + B) & M -> (A + B) & M
12749 Similarly if (N & M) == 0,
12750 ((A | N) + B) & M -> (A + B) & M
12751 and for - instead of + (or unary - instead of +)
12752 and/or ^ instead of |.
12753 If B is constant and (B & M) == 0, fold into A & M.
12754
12755 This function is a helper for match.pd patterns. Return non-NULL
12756 type in which the simplified operation should be performed only
12757 if any optimization is possible.
12758
12759 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12760 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12761 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12762 +/-. */
12763 tree
12764 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12765 tree arg00, enum tree_code code00, tree arg000, tree arg001,
12766 tree arg01, enum tree_code code01, tree arg010, tree arg011,
12767 tree *pmop)
12768 {
12769 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12770 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12771 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12772 if (~cst1 == 0
12773 || (cst1 & (cst1 + 1)) != 0
12774 || !INTEGRAL_TYPE_P (type)
12775 || (!TYPE_OVERFLOW_WRAPS (type)
12776 && TREE_CODE (type) != INTEGER_TYPE)
12777 || (wi::max_value (type) & cst1) != cst1)
12778 return NULL_TREE;
12779
12780 enum tree_code codes[2] = { code00, code01 };
12781 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12782 int which = 0;
12783 wide_int cst0;
12784
12785 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12786 arg1 (M) is == (1LL << cst) - 1.
12787 Store C into PMOP[0] and D into PMOP[1]. */
12788 pmop[0] = arg00;
12789 pmop[1] = arg01;
12790 which = code != NEGATE_EXPR;
12791
12792 for (; which >= 0; which--)
12793 switch (codes[which])
12794 {
12795 case BIT_AND_EXPR:
12796 case BIT_IOR_EXPR:
12797 case BIT_XOR_EXPR:
12798 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12799 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12800 if (codes[which] == BIT_AND_EXPR)
12801 {
12802 if (cst0 != cst1)
12803 break;
12804 }
12805 else if (cst0 != 0)
12806 break;
12807 /* If C or D is of the form (A & N) where
12808 (N & M) == M, or of the form (A | N) or
12809 (A ^ N) where (N & M) == 0, replace it with A. */
12810 pmop[which] = arg0xx[2 * which];
12811 break;
12812 case ERROR_MARK:
12813 if (TREE_CODE (pmop[which]) != INTEGER_CST)
12814 break;
12815 /* If C or D is a N where (N & M) == 0, it can be
12816 omitted (replaced with 0). */
12817 if ((code == PLUS_EXPR
12818 || (code == MINUS_EXPR && which == 0))
12819 && (cst1 & wi::to_wide (pmop[which])) == 0)
12820 pmop[which] = build_int_cst (type, 0);
12821 /* Similarly, with C - N where (-N & M) == 0. */
12822 if (code == MINUS_EXPR
12823 && which == 1
12824 && (cst1 & -wi::to_wide (pmop[which])) == 0)
12825 pmop[which] = build_int_cst (type, 0);
12826 break;
12827 default:
12828 gcc_unreachable ();
12829 }
12830
12831 /* Only build anything new if we optimized one or both arguments above. */
12832 if (pmop[0] == arg00 && pmop[1] == arg01)
12833 return NULL_TREE;
12834
12835 if (TYPE_OVERFLOW_WRAPS (type))
12836 return type;
12837 else
12838 return unsigned_type_for (type);
12839 }
12840
12841 /* Used by contains_label_[p1]. */
12842
12843 struct contains_label_data
12844 {
12845 hash_set<tree> *pset;
12846 bool inside_switch_p;
12847 };
12848
12849 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12850 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12851 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12852
12853 static tree
12854 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12855 {
12856 contains_label_data *d = (contains_label_data *) data;
12857 switch (TREE_CODE (*tp))
12858 {
12859 case LABEL_EXPR:
12860 return *tp;
12861
12862 case CASE_LABEL_EXPR:
12863 if (!d->inside_switch_p)
12864 return *tp;
12865 return NULL_TREE;
12866
12867 case SWITCH_EXPR:
12868 if (!d->inside_switch_p)
12869 {
12870 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12871 return *tp;
12872 d->inside_switch_p = true;
12873 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12874 return *tp;
12875 d->inside_switch_p = false;
12876 *walk_subtrees = 0;
12877 }
12878 return NULL_TREE;
12879
12880 case GOTO_EXPR:
12881 *walk_subtrees = 0;
12882 return NULL_TREE;
12883
12884 default:
12885 return NULL_TREE;
12886 }
12887 }
12888
12889 /* Return whether the sub-tree ST contains a label which is accessible from
12890 outside the sub-tree. */
12891
12892 static bool
12893 contains_label_p (tree st)
12894 {
12895 hash_set<tree> pset;
12896 contains_label_data data = { &pset, false };
12897 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12898 }
12899
12900 /* Fold a ternary expression of code CODE and type TYPE with operands
12901 OP0, OP1, and OP2. Return the folded expression if folding is
12902 successful. Otherwise, return NULL_TREE. */
12903
12904 tree
12905 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12906 tree op0, tree op1, tree op2)
12907 {
12908 tree tem;
12909 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12910 enum tree_code_class kind = TREE_CODE_CLASS (code);
12911
12912 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12913 && TREE_CODE_LENGTH (code) == 3);
12914
12915 /* If this is a commutative operation, and OP0 is a constant, move it
12916 to OP1 to reduce the number of tests below. */
12917 if (commutative_ternary_tree_code (code)
12918 && tree_swap_operands_p (op0, op1))
12919 return fold_build3_loc (loc, code, type, op1, op0, op2);
12920
12921 tem = generic_simplify (loc, code, type, op0, op1, op2);
12922 if (tem)
12923 return tem;
12924
12925 /* Strip any conversions that don't change the mode. This is safe
12926 for every expression, except for a comparison expression because
12927 its signedness is derived from its operands. So, in the latter
12928 case, only strip conversions that don't change the signedness.
12929
12930 Note that this is done as an internal manipulation within the
12931 constant folder, in order to find the simplest representation of
12932 the arguments so that their form can be studied. In any cases,
12933 the appropriate type conversions should be put back in the tree
12934 that will get out of the constant folder. */
12935 if (op0)
12936 {
12937 arg0 = op0;
12938 STRIP_NOPS (arg0);
12939 }
12940
12941 if (op1)
12942 {
12943 arg1 = op1;
12944 STRIP_NOPS (arg1);
12945 }
12946
12947 if (op2)
12948 {
12949 arg2 = op2;
12950 STRIP_NOPS (arg2);
12951 }
12952
12953 switch (code)
12954 {
12955 case COMPONENT_REF:
12956 if (TREE_CODE (arg0) == CONSTRUCTOR
12957 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12958 {
12959 unsigned HOST_WIDE_INT idx;
12960 tree field, value;
12961 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12962 if (field == arg1)
12963 return value;
12964 }
12965 return NULL_TREE;
12966
12967 case COND_EXPR:
12968 case VEC_COND_EXPR:
12969 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12970 so all simple results must be passed through pedantic_non_lvalue. */
12971 if (TREE_CODE (arg0) == INTEGER_CST)
12972 {
12973 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12974 tem = integer_zerop (arg0) ? op2 : op1;
12975 /* Only optimize constant conditions when the selected branch
12976 has the same type as the COND_EXPR. This avoids optimizing
12977 away "c ? x : throw", where the throw has a void type.
12978 Avoid throwing away that operand which contains label. */
12979 if ((!TREE_SIDE_EFFECTS (unused_op)
12980 || !contains_label_p (unused_op))
12981 && (! VOID_TYPE_P (TREE_TYPE (tem))
12982 || VOID_TYPE_P (type)))
12983 return protected_set_expr_location_unshare (tem, loc);
12984 return NULL_TREE;
12985 }
12986 else if (TREE_CODE (arg0) == VECTOR_CST)
12987 {
12988 unsigned HOST_WIDE_INT nelts;
12989 if ((TREE_CODE (arg1) == VECTOR_CST
12990 || TREE_CODE (arg1) == CONSTRUCTOR)
12991 && (TREE_CODE (arg2) == VECTOR_CST
12992 || TREE_CODE (arg2) == CONSTRUCTOR)
12993 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12994 {
12995 vec_perm_builder sel (nelts, nelts, 1);
12996 for (unsigned int i = 0; i < nelts; i++)
12997 {
12998 tree val = VECTOR_CST_ELT (arg0, i);
12999 if (integer_all_onesp (val))
13000 sel.quick_push (i);
13001 else if (integer_zerop (val))
13002 sel.quick_push (nelts + i);
13003 else /* Currently unreachable. */
13004 return NULL_TREE;
13005 }
13006 vec_perm_indices indices (sel, 2, nelts);
13007 tree t = fold_vec_perm (type, arg1, arg2, indices);
13008 if (t != NULL_TREE)
13009 return t;
13010 }
13011 }
13012
13013 /* If we have A op B ? A : C, we may be able to convert this to a
13014 simpler expression, depending on the operation and the values
13015 of B and C. Signed zeros prevent all of these transformations,
13016 for reasons given above each one.
13017
13018 Also try swapping the arguments and inverting the conditional. */
13019 if (COMPARISON_CLASS_P (arg0)
13020 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
13021 && !HONOR_SIGNED_ZEROS (op1))
13022 {
13023 tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
13024 TREE_OPERAND (arg0, 0),
13025 TREE_OPERAND (arg0, 1),
13026 op1, op2);
13027 if (tem)
13028 return tem;
13029 }
13030
13031 if (COMPARISON_CLASS_P (arg0)
13032 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
13033 && !HONOR_SIGNED_ZEROS (op2))
13034 {
13035 enum tree_code comp_code = TREE_CODE (arg0);
13036 tree arg00 = TREE_OPERAND (arg0, 0);
13037 tree arg01 = TREE_OPERAND (arg0, 1);
13038 comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
13039 if (comp_code != ERROR_MARK)
13040 tem = fold_cond_expr_with_comparison (loc, type, comp_code,
13041 arg00,
13042 arg01,
13043 op2, op1);
13044 if (tem)
13045 return tem;
13046 }
13047
13048 /* If the second operand is simpler than the third, swap them
13049 since that produces better jump optimization results. */
13050 if (truth_value_p (TREE_CODE (arg0))
13051 && tree_swap_operands_p (op1, op2))
13052 {
13053 location_t loc0 = expr_location_or (arg0, loc);
13054 /* See if this can be inverted. If it can't, possibly because
13055 it was a floating-point inequality comparison, don't do
13056 anything. */
13057 tem = fold_invert_truthvalue (loc0, arg0);
13058 if (tem)
13059 return fold_build3_loc (loc, code, type, tem, op2, op1);
13060 }
13061
13062 /* Convert A ? 1 : 0 to simply A. */
13063 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13064 : (integer_onep (op1)
13065 && !VECTOR_TYPE_P (type)))
13066 && integer_zerop (op2)
13067 /* If we try to convert OP0 to our type, the
13068 call to fold will try to move the conversion inside
13069 a COND, which will recurse. In that case, the COND_EXPR
13070 is probably the best choice, so leave it alone. */
13071 && type == TREE_TYPE (arg0))
13072 return protected_set_expr_location_unshare (arg0, loc);
13073
13074 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13075 over COND_EXPR in cases such as floating point comparisons. */
13076 if (integer_zerop (op1)
13077 && code == COND_EXPR
13078 && integer_onep (op2)
13079 && !VECTOR_TYPE_P (type)
13080 && truth_value_p (TREE_CODE (arg0)))
13081 return fold_convert_loc (loc, type,
13082 invert_truthvalue_loc (loc, arg0));
13083
13084 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13085 if (TREE_CODE (arg0) == LT_EXPR
13086 && integer_zerop (TREE_OPERAND (arg0, 1))
13087 && integer_zerop (op2)
13088 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13089 {
13090 /* sign_bit_p looks through both zero and sign extensions,
13091 but for this optimization only sign extensions are
13092 usable. */
13093 tree tem2 = TREE_OPERAND (arg0, 0);
13094 while (tem != tem2)
13095 {
13096 if (TREE_CODE (tem2) != NOP_EXPR
13097 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13098 {
13099 tem = NULL_TREE;
13100 break;
13101 }
13102 tem2 = TREE_OPERAND (tem2, 0);
13103 }
13104 /* sign_bit_p only checks ARG1 bits within A's precision.
13105 If <sign bit of A> has wider type than A, bits outside
13106 of A's precision in <sign bit of A> need to be checked.
13107 If they are all 0, this optimization needs to be done
13108 in unsigned A's type, if they are all 1 in signed A's type,
13109 otherwise this can't be done. */
13110 if (tem
13111 && TYPE_PRECISION (TREE_TYPE (tem))
13112 < TYPE_PRECISION (TREE_TYPE (arg1))
13113 && TYPE_PRECISION (TREE_TYPE (tem))
13114 < TYPE_PRECISION (type))
13115 {
13116 int inner_width, outer_width;
13117 tree tem_type;
13118
13119 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13120 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13121 if (outer_width > TYPE_PRECISION (type))
13122 outer_width = TYPE_PRECISION (type);
13123
13124 wide_int mask = wi::shifted_mask
13125 (inner_width, outer_width - inner_width, false,
13126 TYPE_PRECISION (TREE_TYPE (arg1)));
13127
13128 wide_int common = mask & wi::to_wide (arg1);
13129 if (common == mask)
13130 {
13131 tem_type = signed_type_for (TREE_TYPE (tem));
13132 tem = fold_convert_loc (loc, tem_type, tem);
13133 }
13134 else if (common == 0)
13135 {
13136 tem_type = unsigned_type_for (TREE_TYPE (tem));
13137 tem = fold_convert_loc (loc, tem_type, tem);
13138 }
13139 else
13140 tem = NULL;
13141 }
13142
13143 if (tem)
13144 return
13145 fold_convert_loc (loc, type,
13146 fold_build2_loc (loc, BIT_AND_EXPR,
13147 TREE_TYPE (tem), tem,
13148 fold_convert_loc (loc,
13149 TREE_TYPE (tem),
13150 arg1)));
13151 }
13152
13153 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13154 already handled above. */
13155 if (TREE_CODE (arg0) == BIT_AND_EXPR
13156 && integer_onep (TREE_OPERAND (arg0, 1))
13157 && integer_zerop (op2)
13158 && integer_pow2p (arg1))
13159 {
13160 tree tem = TREE_OPERAND (arg0, 0);
13161 STRIP_NOPS (tem);
13162 if (TREE_CODE (tem) == RSHIFT_EXPR
13163 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13164 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13165 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13166 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13167 fold_convert_loc (loc, type,
13168 TREE_OPERAND (tem, 0)),
13169 op1);
13170 }
13171
13172 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13173 is probably obsolete because the first operand should be a
13174 truth value (that's why we have the two cases above), but let's
13175 leave it in until we can confirm this for all front-ends. */
13176 if (integer_zerop (op2)
13177 && TREE_CODE (arg0) == NE_EXPR
13178 && integer_zerop (TREE_OPERAND (arg0, 1))
13179 && integer_pow2p (arg1)
13180 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13181 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13182 arg1, OEP_ONLY_CONST)
13183 /* operand_equal_p compares just value, not precision, so e.g.
13184 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13185 second operand 32-bit -128, which is not a power of two (or vice
13186 versa. */
13187 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13188 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13189
13190 /* Disable the transformations below for vectors, since
13191 fold_binary_op_with_conditional_arg may undo them immediately,
13192 yielding an infinite loop. */
13193 if (code == VEC_COND_EXPR)
13194 return NULL_TREE;
13195
13196 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13197 if (integer_zerop (op2)
13198 && truth_value_p (TREE_CODE (arg0))
13199 && truth_value_p (TREE_CODE (arg1))
13200 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13201 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13202 : TRUTH_ANDIF_EXPR,
13203 type, fold_convert_loc (loc, type, arg0), op1);
13204
13205 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13206 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13207 && truth_value_p (TREE_CODE (arg0))
13208 && truth_value_p (TREE_CODE (arg1))
13209 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13210 {
13211 location_t loc0 = expr_location_or (arg0, loc);
13212 /* Only perform transformation if ARG0 is easily inverted. */
13213 tem = fold_invert_truthvalue (loc0, arg0);
13214 if (tem)
13215 return fold_build2_loc (loc, code == VEC_COND_EXPR
13216 ? BIT_IOR_EXPR
13217 : TRUTH_ORIF_EXPR,
13218 type, fold_convert_loc (loc, type, tem),
13219 op1);
13220 }
13221
13222 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13223 if (integer_zerop (arg1)
13224 && truth_value_p (TREE_CODE (arg0))
13225 && truth_value_p (TREE_CODE (op2))
13226 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13227 {
13228 location_t loc0 = expr_location_or (arg0, loc);
13229 /* Only perform transformation if ARG0 is easily inverted. */
13230 tem = fold_invert_truthvalue (loc0, arg0);
13231 if (tem)
13232 return fold_build2_loc (loc, code == VEC_COND_EXPR
13233 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13234 type, fold_convert_loc (loc, type, tem),
13235 op2);
13236 }
13237
13238 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13239 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13240 && truth_value_p (TREE_CODE (arg0))
13241 && truth_value_p (TREE_CODE (op2))
13242 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13243 return fold_build2_loc (loc, code == VEC_COND_EXPR
13244 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13245 type, fold_convert_loc (loc, type, arg0), op2);
13246
13247 return NULL_TREE;
13248
13249 case CALL_EXPR:
13250 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13251 of fold_ternary on them. */
13252 gcc_unreachable ();
13253
13254 case BIT_FIELD_REF:
13255 if (TREE_CODE (arg0) == VECTOR_CST
13256 && (type == TREE_TYPE (TREE_TYPE (arg0))
13257 || (VECTOR_TYPE_P (type)
13258 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13259 && tree_fits_uhwi_p (op1)
13260 && tree_fits_uhwi_p (op2))
13261 {
13262 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13263 unsigned HOST_WIDE_INT width
13264 = (TREE_CODE (eltype) == BOOLEAN_TYPE
13265 ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13266 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13267 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13268
13269 if (n != 0
13270 && (idx % width) == 0
13271 && (n % width) == 0
13272 && known_le ((idx + n) / width,
13273 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13274 {
13275 idx = idx / width;
13276 n = n / width;
13277
13278 if (TREE_CODE (arg0) == VECTOR_CST)
13279 {
13280 if (n == 1)
13281 {
13282 tem = VECTOR_CST_ELT (arg0, idx);
13283 if (VECTOR_TYPE_P (type))
13284 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13285 return tem;
13286 }
13287
13288 tree_vector_builder vals (type, n, 1);
13289 for (unsigned i = 0; i < n; ++i)
13290 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13291 return vals.build ();
13292 }
13293 }
13294 }
13295
13296 /* On constants we can use native encode/interpret to constant
13297 fold (nearly) all BIT_FIELD_REFs. */
13298 if (CONSTANT_CLASS_P (arg0)
13299 && can_native_interpret_type_p (type)
13300 && BITS_PER_UNIT == 8
13301 && tree_fits_uhwi_p (op1)
13302 && tree_fits_uhwi_p (op2))
13303 {
13304 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13305 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13306 /* Limit us to a reasonable amount of work. To relax the
13307 other limitations we need bit-shifting of the buffer
13308 and rounding up the size. */
13309 if (bitpos % BITS_PER_UNIT == 0
13310 && bitsize % BITS_PER_UNIT == 0
13311 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13312 {
13313 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13314 unsigned HOST_WIDE_INT len
13315 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13316 bitpos / BITS_PER_UNIT);
13317 if (len > 0
13318 && len * BITS_PER_UNIT >= bitsize)
13319 {
13320 tree v = native_interpret_expr (type, b,
13321 bitsize / BITS_PER_UNIT);
13322 if (v)
13323 return v;
13324 }
13325 }
13326 }
13327
13328 return NULL_TREE;
13329
13330 case VEC_PERM_EXPR:
13331 /* Perform constant folding of BIT_INSERT_EXPR. */
13332 if (TREE_CODE (arg2) == VECTOR_CST
13333 && TREE_CODE (op0) == VECTOR_CST
13334 && TREE_CODE (op1) == VECTOR_CST)
13335 {
13336 /* Build a vector of integers from the tree mask. */
13337 vec_perm_builder builder;
13338 if (!tree_to_vec_perm_builder (&builder, arg2))
13339 return NULL_TREE;
13340
13341 /* Create a vec_perm_indices for the integer vector. */
13342 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13343 bool single_arg = (op0 == op1);
13344 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13345 return fold_vec_perm (type, op0, op1, sel);
13346 }
13347 return NULL_TREE;
13348
13349 case BIT_INSERT_EXPR:
13350 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13351 if (TREE_CODE (arg0) == INTEGER_CST
13352 && TREE_CODE (arg1) == INTEGER_CST)
13353 {
13354 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13355 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13356 if (BYTES_BIG_ENDIAN)
13357 bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13358 wide_int tem = (wi::to_wide (arg0)
13359 & wi::shifted_mask (bitpos, bitsize, true,
13360 TYPE_PRECISION (type)));
13361 wide_int tem2
13362 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13363 bitsize), bitpos);
13364 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13365 }
13366 else if (TREE_CODE (arg0) == VECTOR_CST
13367 && CONSTANT_CLASS_P (arg1)
13368 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13369 TREE_TYPE (arg1)))
13370 {
13371 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13372 unsigned HOST_WIDE_INT elsize
13373 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13374 if (bitpos % elsize == 0)
13375 {
13376 unsigned k = bitpos / elsize;
13377 unsigned HOST_WIDE_INT nelts;
13378 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13379 return arg0;
13380 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13381 {
13382 tree_vector_builder elts (type, nelts, 1);
13383 elts.quick_grow (nelts);
13384 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13385 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13386 return elts.build ();
13387 }
13388 }
13389 }
13390 return NULL_TREE;
13391
13392 default:
13393 return NULL_TREE;
13394 } /* switch (code) */
13395 }
13396
13397 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13398 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13399 constructor element index of the value returned. If the element is
13400 not found NULL_TREE is returned and *CTOR_IDX is updated to
13401 the index of the element after the ACCESS_INDEX position (which
13402 may be outside of the CTOR array). */
13403
13404 tree
13405 get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13406 unsigned *ctor_idx)
13407 {
13408 tree index_type = NULL_TREE;
13409 signop index_sgn = UNSIGNED;
13410 offset_int low_bound = 0;
13411
13412 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13413 {
13414 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13415 if (domain_type && TYPE_MIN_VALUE (domain_type))
13416 {
13417 /* Static constructors for variably sized objects makes no sense. */
13418 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13419 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13420 /* ??? When it is obvious that the range is signed, treat it so. */
13421 if (TYPE_UNSIGNED (index_type)
13422 && TYPE_MAX_VALUE (domain_type)
13423 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13424 TYPE_MIN_VALUE (domain_type)))
13425 {
13426 index_sgn = SIGNED;
13427 low_bound
13428 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13429 SIGNED);
13430 }
13431 else
13432 {
13433 index_sgn = TYPE_SIGN (index_type);
13434 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13435 }
13436 }
13437 }
13438
13439 if (index_type)
13440 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13441 index_sgn);
13442
13443 offset_int index = low_bound;
13444 if (index_type)
13445 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13446
13447 offset_int max_index = index;
13448 unsigned cnt;
13449 tree cfield, cval;
13450 bool first_p = true;
13451
13452 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13453 {
13454 /* Array constructor might explicitly set index, or specify a range,
13455 or leave index NULL meaning that it is next index after previous
13456 one. */
13457 if (cfield)
13458 {
13459 if (TREE_CODE (cfield) == INTEGER_CST)
13460 max_index = index
13461 = offset_int::from (wi::to_wide (cfield), index_sgn);
13462 else
13463 {
13464 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13465 index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13466 index_sgn);
13467 max_index
13468 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13469 index_sgn);
13470 gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13471 }
13472 }
13473 else if (!first_p)
13474 {
13475 index = max_index + 1;
13476 if (index_type)
13477 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13478 gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13479 max_index = index;
13480 }
13481 else
13482 first_p = false;
13483
13484 if (TREE_CODE (cval) == RAW_DATA_CST)
13485 max_index += RAW_DATA_LENGTH (cval) - 1;
13486
13487 /* Do we have match? */
13488 if (wi::cmp (access_index, index, index_sgn) >= 0)
13489 {
13490 if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13491 {
13492 if (ctor_idx)
13493 *ctor_idx = cnt;
13494 return cval;
13495 }
13496 }
13497 else if (in_gimple_form)
13498 /* We're past the element we search for. Note during parsing
13499 the elements might not be sorted.
13500 ??? We should use a binary search and a flag on the
13501 CONSTRUCTOR as to whether elements are sorted in declaration
13502 order. */
13503 break;
13504 }
13505 if (ctor_idx)
13506 *ctor_idx = cnt;
13507 return NULL_TREE;
13508 }
13509
13510 /* Perform constant folding and related simplification of EXPR.
13511 The related simplifications include x*1 => x, x*0 => 0, etc.,
13512 and application of the associative law.
13513 NOP_EXPR conversions may be removed freely (as long as we
13514 are careful not to change the type of the overall expression).
13515 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13516 but we can constant-fold them if they have constant operands. */
13517
13518 #ifdef ENABLE_FOLD_CHECKING
13519 # define fold(x) fold_1 (x)
13520 static tree fold_1 (tree);
13521 static
13522 #endif
13523 tree
13524 fold (tree expr)
13525 {
13526 const tree t = expr;
13527 enum tree_code code = TREE_CODE (t);
13528 enum tree_code_class kind = TREE_CODE_CLASS (code);
13529 tree tem;
13530 location_t loc = EXPR_LOCATION (expr);
13531
13532 /* Return right away if a constant. */
13533 if (kind == tcc_constant)
13534 return t;
13535
13536 /* CALL_EXPR-like objects with variable numbers of operands are
13537 treated specially. */
13538 if (kind == tcc_vl_exp)
13539 {
13540 if (code == CALL_EXPR)
13541 {
13542 tem = fold_call_expr (loc, expr, false);
13543 return tem ? tem : expr;
13544 }
13545 return expr;
13546 }
13547
13548 if (IS_EXPR_CODE_CLASS (kind))
13549 {
13550 tree type = TREE_TYPE (t);
13551 tree op0, op1, op2;
13552
13553 switch (TREE_CODE_LENGTH (code))
13554 {
13555 case 1:
13556 op0 = TREE_OPERAND (t, 0);
13557 tem = fold_unary_loc (loc, code, type, op0);
13558 return tem ? tem : expr;
13559 case 2:
13560 op0 = TREE_OPERAND (t, 0);
13561 op1 = TREE_OPERAND (t, 1);
13562 tem = fold_binary_loc (loc, code, type, op0, op1);
13563 return tem ? tem : expr;
13564 case 3:
13565 op0 = TREE_OPERAND (t, 0);
13566 op1 = TREE_OPERAND (t, 1);
13567 op2 = TREE_OPERAND (t, 2);
13568 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13569 return tem ? tem : expr;
13570 default:
13571 break;
13572 }
13573 }
13574
13575 switch (code)
13576 {
13577 case ARRAY_REF:
13578 {
13579 tree op0 = TREE_OPERAND (t, 0);
13580 tree op1 = TREE_OPERAND (t, 1);
13581
13582 if (TREE_CODE (op1) == INTEGER_CST
13583 && TREE_CODE (op0) == CONSTRUCTOR
13584 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13585 {
13586 unsigned int idx;
13587 tree val
13588 = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13589 &idx);
13590 if (val)
13591 {
13592 if (TREE_CODE (val) != RAW_DATA_CST)
13593 return val;
13594 if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13595 || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13596 != INTEGER_CST))
13597 return t;
13598 offset_int o
13599 = (wi::to_offset (op1)
13600 - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13601 gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13602 return build_int_cst (TREE_TYPE (val),
13603 RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13604 }
13605 }
13606
13607 return t;
13608 }
13609
13610 /* Return a VECTOR_CST if possible. */
13611 case CONSTRUCTOR:
13612 {
13613 tree type = TREE_TYPE (t);
13614 if (TREE_CODE (type) != VECTOR_TYPE)
13615 return t;
13616
13617 unsigned i;
13618 tree val;
13619 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13620 if (! CONSTANT_CLASS_P (val))
13621 return t;
13622
13623 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13624 }
13625
13626 case CONST_DECL:
13627 return fold (DECL_INITIAL (t));
13628
13629 default:
13630 return t;
13631 } /* switch (code) */
13632 }
13633
13634 #ifdef ENABLE_FOLD_CHECKING
13635 #undef fold
13636
13637 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13638 hash_table<nofree_ptr_hash<const tree_node> > *);
13639 static void fold_check_failed (const_tree, const_tree);
13640 void print_fold_checksum (const_tree);
13641
13642 /* When --enable-checking=fold, compute a digest of expr before
13643 and after actual fold call to see if fold did not accidentally
13644 change original expr. */
13645
13646 tree
13647 fold (tree expr)
13648 {
13649 tree ret;
13650 struct md5_ctx ctx;
13651 unsigned char checksum_before[16], checksum_after[16];
13652 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13653
13654 md5_init_ctx (&ctx);
13655 fold_checksum_tree (expr, &ctx, &ht);
13656 md5_finish_ctx (&ctx, checksum_before);
13657 ht.empty ();
13658
13659 ret = fold_1 (expr);
13660
13661 md5_init_ctx (&ctx);
13662 fold_checksum_tree (expr, &ctx, &ht);
13663 md5_finish_ctx (&ctx, checksum_after);
13664
13665 if (memcmp (checksum_before, checksum_after, 16))
13666 fold_check_failed (expr, ret);
13667
13668 return ret;
13669 }
13670
13671 void
13672 print_fold_checksum (const_tree expr)
13673 {
13674 struct md5_ctx ctx;
13675 unsigned char checksum[16], cnt;
13676 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13677
13678 md5_init_ctx (&ctx);
13679 fold_checksum_tree (expr, &ctx, &ht);
13680 md5_finish_ctx (&ctx, checksum);
13681 for (cnt = 0; cnt < 16; ++cnt)
13682 fprintf (stderr, "%02x", checksum[cnt]);
13683 putc ('\n', stderr);
13684 }
13685
13686 static void
13687 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13688 {
13689 internal_error ("fold check: original tree changed by fold");
13690 }
13691
13692 static void
13693 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13694 hash_table<nofree_ptr_hash <const tree_node> > *ht)
13695 {
13696 const tree_node **slot;
13697 enum tree_code code;
13698 union tree_node *buf;
13699 int i, len;
13700
13701 recursive_label:
13702 if (expr == NULL)
13703 return;
13704 slot = ht->find_slot (expr, INSERT);
13705 if (*slot != NULL)
13706 return;
13707 *slot = expr;
13708 code = TREE_CODE (expr);
13709 if (TREE_CODE_CLASS (code) == tcc_declaration
13710 && HAS_DECL_ASSEMBLER_NAME_P (expr))
13711 {
13712 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13713 size_t sz = tree_size (expr);
13714 buf = XALLOCAVAR (union tree_node, sz);
13715 memcpy ((char *) buf, expr, sz);
13716 SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13717 buf->decl_with_vis.symtab_node = NULL;
13718 buf->base.nowarning_flag = 0;
13719 expr = (tree) buf;
13720 }
13721 else if (TREE_CODE_CLASS (code) == tcc_type
13722 && (TYPE_POINTER_TO (expr)
13723 || TYPE_REFERENCE_TO (expr)
13724 || TYPE_CACHED_VALUES_P (expr)
13725 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13726 || TYPE_NEXT_VARIANT (expr)
13727 || TYPE_ALIAS_SET_KNOWN_P (expr)))
13728 {
13729 /* Allow these fields to be modified. */
13730 tree tmp;
13731 size_t sz = tree_size (expr);
13732 buf = XALLOCAVAR (union tree_node, sz);
13733 memcpy ((char *) buf, expr, sz);
13734 expr = tmp = (tree) buf;
13735 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13736 TYPE_POINTER_TO (tmp) = NULL;
13737 TYPE_REFERENCE_TO (tmp) = NULL;
13738 TYPE_NEXT_VARIANT (tmp) = NULL;
13739 TYPE_ALIAS_SET (tmp) = -1;
13740 if (TYPE_CACHED_VALUES_P (tmp))
13741 {
13742 TYPE_CACHED_VALUES_P (tmp) = 0;
13743 TYPE_CACHED_VALUES (tmp) = NULL;
13744 }
13745 }
13746 else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13747 {
13748 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13749 that and change builtins.cc etc. instead - see PR89543. */
13750 size_t sz = tree_size (expr);
13751 buf = XALLOCAVAR (union tree_node, sz);
13752 memcpy ((char *) buf, expr, sz);
13753 buf->base.nowarning_flag = 0;
13754 expr = (tree) buf;
13755 }
13756 md5_process_bytes (expr, tree_size (expr), ctx);
13757 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13758 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13759 if (TREE_CODE_CLASS (code) != tcc_type
13760 && TREE_CODE_CLASS (code) != tcc_declaration
13761 && code != TREE_LIST
13762 && code != SSA_NAME
13763 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13764 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13765 switch (TREE_CODE_CLASS (code))
13766 {
13767 case tcc_constant:
13768 switch (code)
13769 {
13770 case STRING_CST:
13771 md5_process_bytes (TREE_STRING_POINTER (expr),
13772 TREE_STRING_LENGTH (expr), ctx);
13773 break;
13774 case COMPLEX_CST:
13775 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13776 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13777 break;
13778 case VECTOR_CST:
13779 len = vector_cst_encoded_nelts (expr);
13780 for (i = 0; i < len; ++i)
13781 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13782 break;
13783 default:
13784 break;
13785 }
13786 break;
13787 case tcc_exceptional:
13788 switch (code)
13789 {
13790 case TREE_LIST:
13791 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13792 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13793 expr = TREE_CHAIN (expr);
13794 goto recursive_label;
13795 break;
13796 case TREE_VEC:
13797 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13798 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13799 break;
13800 default:
13801 break;
13802 }
13803 break;
13804 case tcc_expression:
13805 case tcc_reference:
13806 case tcc_comparison:
13807 case tcc_unary:
13808 case tcc_binary:
13809 case tcc_statement:
13810 case tcc_vl_exp:
13811 len = TREE_OPERAND_LENGTH (expr);
13812 for (i = 0; i < len; ++i)
13813 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13814 break;
13815 case tcc_declaration:
13816 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13817 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13818 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13819 {
13820 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13821 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13822 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13823 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13824 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13825 }
13826
13827 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13828 {
13829 if (TREE_CODE (expr) == FUNCTION_DECL)
13830 {
13831 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13832 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13833 }
13834 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13835 }
13836 break;
13837 case tcc_type:
13838 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13839 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13840 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13841 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13842 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13843 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13844 if (INTEGRAL_TYPE_P (expr)
13845 || SCALAR_FLOAT_TYPE_P (expr))
13846 {
13847 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13848 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13849 }
13850 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13851 if (RECORD_OR_UNION_TYPE_P (expr))
13852 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13853 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13854 break;
13855 default:
13856 break;
13857 }
13858 }
13859
13860 /* Helper function for outputting the checksum of a tree T. When
13861 debugging with gdb, you can "define mynext" to be "next" followed
13862 by "call debug_fold_checksum (op0)", then just trace down till the
13863 outputs differ. */
13864
13865 DEBUG_FUNCTION void
13866 debug_fold_checksum (const_tree t)
13867 {
13868 int i;
13869 unsigned char checksum[16];
13870 struct md5_ctx ctx;
13871 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13872
13873 md5_init_ctx (&ctx);
13874 fold_checksum_tree (t, &ctx, &ht);
13875 md5_finish_ctx (&ctx, checksum);
13876 ht.empty ();
13877
13878 for (i = 0; i < 16; i++)
13879 fprintf (stderr, "%d ", checksum[i]);
13880
13881 fprintf (stderr, "\n");
13882 }
13883
13884 #endif
13885
13886 /* Fold a unary tree expression with code CODE of type TYPE with an
13887 operand OP0. LOC is the location of the resulting expression.
13888 Return a folded expression if successful. Otherwise, return a tree
13889 expression with code CODE of type TYPE with an operand OP0. */
13890
13891 tree
13892 fold_build1_loc (location_t loc,
13893 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13894 {
13895 tree tem;
13896 #ifdef ENABLE_FOLD_CHECKING
13897 unsigned char checksum_before[16], checksum_after[16];
13898 struct md5_ctx ctx;
13899 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13900
13901 md5_init_ctx (&ctx);
13902 fold_checksum_tree (op0, &ctx, &ht);
13903 md5_finish_ctx (&ctx, checksum_before);
13904 ht.empty ();
13905 #endif
13906
13907 tem = fold_unary_loc (loc, code, type, op0);
13908 if (!tem)
13909 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13910
13911 #ifdef ENABLE_FOLD_CHECKING
13912 md5_init_ctx (&ctx);
13913 fold_checksum_tree (op0, &ctx, &ht);
13914 md5_finish_ctx (&ctx, checksum_after);
13915
13916 if (memcmp (checksum_before, checksum_after, 16))
13917 fold_check_failed (op0, tem);
13918 #endif
13919 return tem;
13920 }
13921
13922 /* Fold a binary tree expression with code CODE of type TYPE with
13923 operands OP0 and OP1. LOC is the location of the resulting
13924 expression. Return a folded expression if successful. Otherwise,
13925 return a tree expression with code CODE of type TYPE with operands
13926 OP0 and OP1. */
13927
13928 tree
13929 fold_build2_loc (location_t loc,
13930 enum tree_code code, tree type, tree op0, tree op1
13931 MEM_STAT_DECL)
13932 {
13933 tree tem;
13934 #ifdef ENABLE_FOLD_CHECKING
13935 unsigned char checksum_before_op0[16],
13936 checksum_before_op1[16],
13937 checksum_after_op0[16],
13938 checksum_after_op1[16];
13939 struct md5_ctx ctx;
13940 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13941
13942 md5_init_ctx (&ctx);
13943 fold_checksum_tree (op0, &ctx, &ht);
13944 md5_finish_ctx (&ctx, checksum_before_op0);
13945 ht.empty ();
13946
13947 md5_init_ctx (&ctx);
13948 fold_checksum_tree (op1, &ctx, &ht);
13949 md5_finish_ctx (&ctx, checksum_before_op1);
13950 ht.empty ();
13951 #endif
13952
13953 tem = fold_binary_loc (loc, code, type, op0, op1);
13954 if (!tem)
13955 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13956
13957 #ifdef ENABLE_FOLD_CHECKING
13958 md5_init_ctx (&ctx);
13959 fold_checksum_tree (op0, &ctx, &ht);
13960 md5_finish_ctx (&ctx, checksum_after_op0);
13961 ht.empty ();
13962
13963 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13964 fold_check_failed (op0, tem);
13965
13966 md5_init_ctx (&ctx);
13967 fold_checksum_tree (op1, &ctx, &ht);
13968 md5_finish_ctx (&ctx, checksum_after_op1);
13969
13970 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13971 fold_check_failed (op1, tem);
13972 #endif
13973 return tem;
13974 }
13975
13976 /* Fold a ternary tree expression with code CODE of type TYPE with
13977 operands OP0, OP1, and OP2. Return a folded expression if
13978 successful. Otherwise, return a tree expression with code CODE of
13979 type TYPE with operands OP0, OP1, and OP2. */
13980
13981 tree
13982 fold_build3_loc (location_t loc, enum tree_code code, tree type,
13983 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13984 {
13985 tree tem;
13986 #ifdef ENABLE_FOLD_CHECKING
13987 unsigned char checksum_before_op0[16],
13988 checksum_before_op1[16],
13989 checksum_before_op2[16],
13990 checksum_after_op0[16],
13991 checksum_after_op1[16],
13992 checksum_after_op2[16];
13993 struct md5_ctx ctx;
13994 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13995
13996 md5_init_ctx (&ctx);
13997 fold_checksum_tree (op0, &ctx, &ht);
13998 md5_finish_ctx (&ctx, checksum_before_op0);
13999 ht.empty ();
14000
14001 md5_init_ctx (&ctx);
14002 fold_checksum_tree (op1, &ctx, &ht);
14003 md5_finish_ctx (&ctx, checksum_before_op1);
14004 ht.empty ();
14005
14006 md5_init_ctx (&ctx);
14007 fold_checksum_tree (op2, &ctx, &ht);
14008 md5_finish_ctx (&ctx, checksum_before_op2);
14009 ht.empty ();
14010 #endif
14011
14012 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14013 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14014 if (!tem)
14015 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14016
14017 #ifdef ENABLE_FOLD_CHECKING
14018 md5_init_ctx (&ctx);
14019 fold_checksum_tree (op0, &ctx, &ht);
14020 md5_finish_ctx (&ctx, checksum_after_op0);
14021 ht.empty ();
14022
14023 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14024 fold_check_failed (op0, tem);
14025
14026 md5_init_ctx (&ctx);
14027 fold_checksum_tree (op1, &ctx, &ht);
14028 md5_finish_ctx (&ctx, checksum_after_op1);
14029 ht.empty ();
14030
14031 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14032 fold_check_failed (op1, tem);
14033
14034 md5_init_ctx (&ctx);
14035 fold_checksum_tree (op2, &ctx, &ht);
14036 md5_finish_ctx (&ctx, checksum_after_op2);
14037
14038 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14039 fold_check_failed (op2, tem);
14040 #endif
14041 return tem;
14042 }
14043
14044 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14045 arguments in ARGARRAY, and a null static chain.
14046 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14047 of type TYPE from the given operands as constructed by build_call_array. */
14048
14049 tree
14050 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14051 int nargs, tree *argarray)
14052 {
14053 tree tem;
14054 #ifdef ENABLE_FOLD_CHECKING
14055 unsigned char checksum_before_fn[16],
14056 checksum_before_arglist[16],
14057 checksum_after_fn[16],
14058 checksum_after_arglist[16];
14059 struct md5_ctx ctx;
14060 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14061 int i;
14062
14063 md5_init_ctx (&ctx);
14064 fold_checksum_tree (fn, &ctx, &ht);
14065 md5_finish_ctx (&ctx, checksum_before_fn);
14066 ht.empty ();
14067
14068 md5_init_ctx (&ctx);
14069 for (i = 0; i < nargs; i++)
14070 fold_checksum_tree (argarray[i], &ctx, &ht);
14071 md5_finish_ctx (&ctx, checksum_before_arglist);
14072 ht.empty ();
14073 #endif
14074
14075 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14076 if (!tem)
14077 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14078
14079 #ifdef ENABLE_FOLD_CHECKING
14080 md5_init_ctx (&ctx);
14081 fold_checksum_tree (fn, &ctx, &ht);
14082 md5_finish_ctx (&ctx, checksum_after_fn);
14083 ht.empty ();
14084
14085 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14086 fold_check_failed (fn, tem);
14087
14088 md5_init_ctx (&ctx);
14089 for (i = 0; i < nargs; i++)
14090 fold_checksum_tree (argarray[i], &ctx, &ht);
14091 md5_finish_ctx (&ctx, checksum_after_arglist);
14092
14093 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14094 fold_check_failed (NULL_TREE, tem);
14095 #endif
14096 return tem;
14097 }
14098
14099 /* Perform constant folding and related simplification of initializer
14100 expression EXPR. These behave identically to "fold_buildN" but ignore
14101 potential run-time traps and exceptions that fold must preserve. */
14102
14103 #define START_FOLD_INIT \
14104 int saved_signaling_nans = flag_signaling_nans;\
14105 int saved_trapping_math = flag_trapping_math;\
14106 int saved_rounding_math = flag_rounding_math;\
14107 int saved_trapv = flag_trapv;\
14108 int saved_folding_initializer = folding_initializer;\
14109 flag_signaling_nans = 0;\
14110 flag_trapping_math = 0;\
14111 flag_rounding_math = 0;\
14112 flag_trapv = 0;\
14113 folding_initializer = 1;
14114
14115 #define END_FOLD_INIT \
14116 flag_signaling_nans = saved_signaling_nans;\
14117 flag_trapping_math = saved_trapping_math;\
14118 flag_rounding_math = saved_rounding_math;\
14119 flag_trapv = saved_trapv;\
14120 folding_initializer = saved_folding_initializer;
14121
14122 tree
14123 fold_init (tree expr)
14124 {
14125 tree result;
14126 START_FOLD_INIT;
14127
14128 result = fold (expr);
14129
14130 END_FOLD_INIT;
14131 return result;
14132 }
14133
14134 tree
14135 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14136 tree type, tree op)
14137 {
14138 tree result;
14139 START_FOLD_INIT;
14140
14141 result = fold_build1_loc (loc, code, type, op);
14142
14143 END_FOLD_INIT;
14144 return result;
14145 }
14146
14147 tree
14148 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14149 tree type, tree op0, tree op1)
14150 {
14151 tree result;
14152 START_FOLD_INIT;
14153
14154 result = fold_build2_loc (loc, code, type, op0, op1);
14155
14156 END_FOLD_INIT;
14157 return result;
14158 }
14159
14160 tree
14161 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14162 int nargs, tree *argarray)
14163 {
14164 tree result;
14165 START_FOLD_INIT;
14166
14167 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14168
14169 END_FOLD_INIT;
14170 return result;
14171 }
14172
14173 tree
14174 fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14175 tree lhs, tree rhs)
14176 {
14177 tree result;
14178 START_FOLD_INIT;
14179
14180 result = fold_binary_loc (loc, code, type, lhs, rhs);
14181
14182 END_FOLD_INIT;
14183 return result;
14184 }
14185
14186 #undef START_FOLD_INIT
14187 #undef END_FOLD_INIT
14188
14189 /* Determine if first argument is a multiple of second argument. Return
14190 false if it is not, or we cannot easily determined it to be.
14191
14192 An example of the sort of thing we care about (at this point; this routine
14193 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14194 fold cases do now) is discovering that
14195
14196 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14197
14198 is a multiple of
14199
14200 SAVE_EXPR (J * 8)
14201
14202 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14203
14204 This code also handles discovering that
14205
14206 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14207
14208 is a multiple of 8 so we don't have to worry about dealing with a
14209 possible remainder.
14210
14211 Note that we *look* inside a SAVE_EXPR only to determine how it was
14212 calculated; it is not safe for fold to do much of anything else with the
14213 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14214 at run time. For example, the latter example above *cannot* be implemented
14215 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14216 evaluation time of the original SAVE_EXPR is not necessarily the same at
14217 the time the new expression is evaluated. The only optimization of this
14218 sort that would be valid is changing
14219
14220 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14221
14222 divided by 8 to
14223
14224 SAVE_EXPR (I) * SAVE_EXPR (J)
14225
14226 (where the same SAVE_EXPR (J) is used in the original and the
14227 transformed version).
14228
14229 NOWRAP specifies whether all outer operations in TYPE should
14230 be considered not wrapping. Any type conversion within TOP acts
14231 as a barrier and we will fall back to NOWRAP being false.
14232 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14233 as not wrapping even though they are generally using unsigned arithmetic. */
14234
14235 bool
14236 multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14237 {
14238 gimple *stmt;
14239 tree op1, op2;
14240
14241 if (operand_equal_p (top, bottom, 0))
14242 return true;
14243
14244 if (TREE_CODE (type) != INTEGER_TYPE)
14245 return false;
14246
14247 switch (TREE_CODE (top))
14248 {
14249 case BIT_AND_EXPR:
14250 /* Bitwise and provides a power of two multiple. If the mask is
14251 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14252 if (!integer_pow2p (bottom))
14253 return false;
14254 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14255 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14256
14257 case MULT_EXPR:
14258 /* If the multiplication can wrap we cannot recurse further unless
14259 the bottom is a power of two which is where wrapping does not
14260 matter. */
14261 if (!nowrap
14262 && !TYPE_OVERFLOW_UNDEFINED (type)
14263 && !integer_pow2p (bottom))
14264 return false;
14265 if (TREE_CODE (bottom) == INTEGER_CST)
14266 {
14267 op1 = TREE_OPERAND (top, 0);
14268 op2 = TREE_OPERAND (top, 1);
14269 if (TREE_CODE (op1) == INTEGER_CST)
14270 std::swap (op1, op2);
14271 if (TREE_CODE (op2) == INTEGER_CST)
14272 {
14273 if (multiple_of_p (type, op2, bottom, nowrap))
14274 return true;
14275 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14276 if (multiple_of_p (type, bottom, op2, nowrap))
14277 {
14278 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14279 wi::to_widest (op2));
14280 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14281 {
14282 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14283 return multiple_of_p (type, op1, op2, nowrap);
14284 }
14285 }
14286 return multiple_of_p (type, op1, bottom, nowrap);
14287 }
14288 }
14289 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14290 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14291
14292 case LSHIFT_EXPR:
14293 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14294 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14295 {
14296 op1 = TREE_OPERAND (top, 1);
14297 if (wi::to_widest (op1) < TYPE_PRECISION (type))
14298 {
14299 wide_int mul_op
14300 = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14301 return multiple_of_p (type,
14302 wide_int_to_tree (type, mul_op), bottom,
14303 nowrap);
14304 }
14305 }
14306 return false;
14307
14308 case MINUS_EXPR:
14309 case PLUS_EXPR:
14310 /* If the addition or subtraction can wrap we cannot recurse further
14311 unless bottom is a power of two which is where wrapping does not
14312 matter. */
14313 if (!nowrap
14314 && !TYPE_OVERFLOW_UNDEFINED (type)
14315 && !integer_pow2p (bottom))
14316 return false;
14317
14318 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14319 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14320 but 0xfffffffd is not. */
14321 op1 = TREE_OPERAND (top, 1);
14322 if (TREE_CODE (top) == PLUS_EXPR
14323 && nowrap
14324 && TYPE_UNSIGNED (type)
14325 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14326 op1 = fold_build1 (NEGATE_EXPR, type, op1);
14327
14328 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14329 precisely, so be conservative here checking if both op0 and op1
14330 are multiple of bottom. Note we check the second operand first
14331 since it's usually simpler. */
14332 return (multiple_of_p (type, op1, bottom, nowrap)
14333 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14334
14335 CASE_CONVERT:
14336 /* Can't handle conversions from non-integral or wider integral type. */
14337 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14338 || (TYPE_PRECISION (type)
14339 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14340 return false;
14341 /* NOWRAP only extends to operations in the outermost type so
14342 make sure to strip it off here. */
14343 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14344 TREE_OPERAND (top, 0), bottom, false);
14345
14346 case SAVE_EXPR:
14347 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14348
14349 case COND_EXPR:
14350 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14351 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14352
14353 case INTEGER_CST:
14354 if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14355 return false;
14356 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14357 SIGNED);
14358
14359 case SSA_NAME:
14360 if (TREE_CODE (bottom) == INTEGER_CST
14361 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14362 && gimple_code (stmt) == GIMPLE_ASSIGN)
14363 {
14364 enum tree_code code = gimple_assign_rhs_code (stmt);
14365
14366 /* Check for special cases to see if top is defined as multiple
14367 of bottom:
14368
14369 top = (X & ~(bottom - 1) ; bottom is power of 2
14370
14371 or
14372
14373 Y = X % bottom
14374 top = X - Y. */
14375 if (code == BIT_AND_EXPR
14376 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14377 && TREE_CODE (op2) == INTEGER_CST
14378 && integer_pow2p (bottom)
14379 && wi::multiple_of_p (wi::to_widest (op2),
14380 wi::to_widest (bottom), SIGNED))
14381 return true;
14382
14383 op1 = gimple_assign_rhs1 (stmt);
14384 if (code == MINUS_EXPR
14385 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14386 && TREE_CODE (op2) == SSA_NAME
14387 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14388 && gimple_code (stmt) == GIMPLE_ASSIGN
14389 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14390 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14391 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14392 return true;
14393 }
14394
14395 /* fall through */
14396
14397 default:
14398 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14399 return multiple_p (wi::to_poly_widest (top),
14400 wi::to_poly_widest (bottom));
14401
14402 return false;
14403 }
14404 }
14405
14406 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14407 This function returns true for integer expressions, and returns
14408 false if uncertain. */
14409
14410 bool
14411 tree_expr_finite_p (const_tree x)
14412 {
14413 machine_mode mode = element_mode (x);
14414 if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14415 return true;
14416 switch (TREE_CODE (x))
14417 {
14418 case REAL_CST:
14419 return real_isfinite (TREE_REAL_CST_PTR (x));
14420 case COMPLEX_CST:
14421 return tree_expr_finite_p (TREE_REALPART (x))
14422 && tree_expr_finite_p (TREE_IMAGPART (x));
14423 case FLOAT_EXPR:
14424 return true;
14425 case ABS_EXPR:
14426 case CONVERT_EXPR:
14427 case NON_LVALUE_EXPR:
14428 case NEGATE_EXPR:
14429 case SAVE_EXPR:
14430 return tree_expr_finite_p (TREE_OPERAND (x, 0));
14431 case MIN_EXPR:
14432 case MAX_EXPR:
14433 return tree_expr_finite_p (TREE_OPERAND (x, 0))
14434 && tree_expr_finite_p (TREE_OPERAND (x, 1));
14435 case COND_EXPR:
14436 return tree_expr_finite_p (TREE_OPERAND (x, 1))
14437 && tree_expr_finite_p (TREE_OPERAND (x, 2));
14438 case CALL_EXPR:
14439 switch (get_call_combined_fn (x))
14440 {
14441 CASE_CFN_FABS:
14442 CASE_CFN_FABS_FN:
14443 return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14444 CASE_CFN_FMAX:
14445 CASE_CFN_FMAX_FN:
14446 CASE_CFN_FMIN:
14447 CASE_CFN_FMIN_FN:
14448 return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14449 && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14450 default:
14451 return false;
14452 }
14453
14454 default:
14455 return false;
14456 }
14457 }
14458
14459 /* Return true if expression X evaluates to an infinity.
14460 This function returns false for integer expressions. */
14461
14462 bool
14463 tree_expr_infinite_p (const_tree x)
14464 {
14465 if (!HONOR_INFINITIES (x))
14466 return false;
14467 switch (TREE_CODE (x))
14468 {
14469 case REAL_CST:
14470 return real_isinf (TREE_REAL_CST_PTR (x));
14471 case ABS_EXPR:
14472 case NEGATE_EXPR:
14473 case NON_LVALUE_EXPR:
14474 case SAVE_EXPR:
14475 return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14476 case COND_EXPR:
14477 return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14478 && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14479 default:
14480 return false;
14481 }
14482 }
14483
14484 /* Return true if expression X could evaluate to an infinity.
14485 This function returns false for integer expressions, and returns
14486 true if uncertain. */
14487
14488 bool
14489 tree_expr_maybe_infinite_p (const_tree x)
14490 {
14491 if (!HONOR_INFINITIES (x))
14492 return false;
14493 switch (TREE_CODE (x))
14494 {
14495 case REAL_CST:
14496 return real_isinf (TREE_REAL_CST_PTR (x));
14497 case FLOAT_EXPR:
14498 return false;
14499 case ABS_EXPR:
14500 case NEGATE_EXPR:
14501 return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14502 case COND_EXPR:
14503 return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14504 || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14505 default:
14506 return true;
14507 }
14508 }
14509
14510 /* Return true if expression X evaluates to a signaling NaN.
14511 This function returns false for integer expressions. */
14512
14513 bool
14514 tree_expr_signaling_nan_p (const_tree x)
14515 {
14516 if (!HONOR_SNANS (x))
14517 return false;
14518 switch (TREE_CODE (x))
14519 {
14520 case REAL_CST:
14521 return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14522 case NON_LVALUE_EXPR:
14523 case SAVE_EXPR:
14524 return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14525 case COND_EXPR:
14526 return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14527 && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14528 default:
14529 return false;
14530 }
14531 }
14532
14533 /* Return true if expression X could evaluate to a signaling NaN.
14534 This function returns false for integer expressions, and returns
14535 true if uncertain. */
14536
14537 bool
14538 tree_expr_maybe_signaling_nan_p (const_tree x)
14539 {
14540 if (!HONOR_SNANS (x))
14541 return false;
14542 switch (TREE_CODE (x))
14543 {
14544 case REAL_CST:
14545 return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14546 case FLOAT_EXPR:
14547 return false;
14548 case ABS_EXPR:
14549 case CONVERT_EXPR:
14550 case NEGATE_EXPR:
14551 case NON_LVALUE_EXPR:
14552 case SAVE_EXPR:
14553 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14554 case MIN_EXPR:
14555 case MAX_EXPR:
14556 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14557 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14558 case COND_EXPR:
14559 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14560 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14561 case CALL_EXPR:
14562 switch (get_call_combined_fn (x))
14563 {
14564 CASE_CFN_FABS:
14565 CASE_CFN_FABS_FN:
14566 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14567 CASE_CFN_FMAX:
14568 CASE_CFN_FMAX_FN:
14569 CASE_CFN_FMIN:
14570 CASE_CFN_FMIN_FN:
14571 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14572 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14573 default:
14574 return true;
14575 }
14576 default:
14577 return true;
14578 }
14579 }
14580
14581 /* Return true if expression X evaluates to a NaN.
14582 This function returns false for integer expressions. */
14583
14584 bool
14585 tree_expr_nan_p (const_tree x)
14586 {
14587 if (!HONOR_NANS (x))
14588 return false;
14589 switch (TREE_CODE (x))
14590 {
14591 case REAL_CST:
14592 return real_isnan (TREE_REAL_CST_PTR (x));
14593 case NON_LVALUE_EXPR:
14594 case SAVE_EXPR:
14595 return tree_expr_nan_p (TREE_OPERAND (x, 0));
14596 case COND_EXPR:
14597 return tree_expr_nan_p (TREE_OPERAND (x, 1))
14598 && tree_expr_nan_p (TREE_OPERAND (x, 2));
14599 default:
14600 return false;
14601 }
14602 }
14603
14604 /* Return true if expression X could evaluate to a NaN.
14605 This function returns false for integer expressions, and returns
14606 true if uncertain. */
14607
14608 bool
14609 tree_expr_maybe_nan_p (const_tree x)
14610 {
14611 if (!HONOR_NANS (x))
14612 return false;
14613 switch (TREE_CODE (x))
14614 {
14615 case REAL_CST:
14616 return real_isnan (TREE_REAL_CST_PTR (x));
14617 case FLOAT_EXPR:
14618 return false;
14619 case PLUS_EXPR:
14620 case MINUS_EXPR:
14621 case MULT_EXPR:
14622 return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14623 || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14624 case ABS_EXPR:
14625 case CONVERT_EXPR:
14626 case NEGATE_EXPR:
14627 case NON_LVALUE_EXPR:
14628 case SAVE_EXPR:
14629 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14630 case MIN_EXPR:
14631 case MAX_EXPR:
14632 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14633 || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14634 case COND_EXPR:
14635 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14636 || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14637 case CALL_EXPR:
14638 switch (get_call_combined_fn (x))
14639 {
14640 CASE_CFN_FABS:
14641 CASE_CFN_FABS_FN:
14642 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14643 CASE_CFN_FMAX:
14644 CASE_CFN_FMAX_FN:
14645 CASE_CFN_FMIN:
14646 CASE_CFN_FMIN_FN:
14647 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14648 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14649 default:
14650 return true;
14651 }
14652 default:
14653 return true;
14654 }
14655 }
14656
14657 /* Return true if expression X could evaluate to -0.0.
14658 This function returns true if uncertain. */
14659
14660 bool
14661 tree_expr_maybe_real_minus_zero_p (const_tree x)
14662 {
14663 if (!HONOR_SIGNED_ZEROS (x))
14664 return false;
14665 switch (TREE_CODE (x))
14666 {
14667 case REAL_CST:
14668 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14669 case INTEGER_CST:
14670 case FLOAT_EXPR:
14671 case ABS_EXPR:
14672 return false;
14673 case NON_LVALUE_EXPR:
14674 case SAVE_EXPR:
14675 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14676 case COND_EXPR:
14677 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14678 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14679 case CALL_EXPR:
14680 switch (get_call_combined_fn (x))
14681 {
14682 CASE_CFN_FABS:
14683 CASE_CFN_FABS_FN:
14684 return false;
14685 default:
14686 break;
14687 }
14688 default:
14689 break;
14690 }
14691 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14692 * but currently those predicates require tree and not const_tree. */
14693 return true;
14694 }
14695
14696 #define tree_expr_nonnegative_warnv_p(X, Y) \
14697 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14698
14699 #define RECURSE(X) \
14700 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14701
14702 /* Return true if CODE or TYPE is known to be non-negative. */
14703
14704 static bool
14705 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14706 {
14707 if (!VECTOR_TYPE_P (type)
14708 && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14709 && truth_value_p (code))
14710 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14711 have a signed:1 type (where the value is -1 and 0). */
14712 return true;
14713 return false;
14714 }
14715
14716 /* Return true if (CODE OP0) is known to be non-negative. If the return
14717 value is based on the assumption that signed overflow is undefined,
14718 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14719 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14720
14721 bool
14722 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14723 bool *strict_overflow_p, int depth)
14724 {
14725 if (TYPE_UNSIGNED (type))
14726 return true;
14727
14728 switch (code)
14729 {
14730 case ABS_EXPR:
14731 /* We can't return 1 if flag_wrapv is set because
14732 ABS_EXPR<INT_MIN> = INT_MIN. */
14733 if (!ANY_INTEGRAL_TYPE_P (type))
14734 return true;
14735 if (TYPE_OVERFLOW_UNDEFINED (type))
14736 {
14737 *strict_overflow_p = true;
14738 return true;
14739 }
14740 break;
14741
14742 case NON_LVALUE_EXPR:
14743 case FLOAT_EXPR:
14744 case FIX_TRUNC_EXPR:
14745 return RECURSE (op0);
14746
14747 CASE_CONVERT:
14748 {
14749 tree inner_type = TREE_TYPE (op0);
14750 tree outer_type = type;
14751
14752 if (SCALAR_FLOAT_TYPE_P (outer_type))
14753 {
14754 if (SCALAR_FLOAT_TYPE_P (inner_type))
14755 return RECURSE (op0);
14756 if (INTEGRAL_TYPE_P (inner_type))
14757 {
14758 if (TYPE_UNSIGNED (inner_type))
14759 return true;
14760 return RECURSE (op0);
14761 }
14762 }
14763 else if (INTEGRAL_TYPE_P (outer_type))
14764 {
14765 if (SCALAR_FLOAT_TYPE_P (inner_type))
14766 return RECURSE (op0);
14767 if (INTEGRAL_TYPE_P (inner_type))
14768 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14769 && TYPE_UNSIGNED (inner_type);
14770 }
14771 }
14772 break;
14773
14774 default:
14775 return tree_simple_nonnegative_warnv_p (code, type);
14776 }
14777
14778 /* We don't know sign of `t', so be conservative and return false. */
14779 return false;
14780 }
14781
14782 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14783 value is based on the assumption that signed overflow is undefined,
14784 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14785 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14786
14787 bool
14788 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14789 tree op1, bool *strict_overflow_p,
14790 int depth)
14791 {
14792 if (TYPE_UNSIGNED (type))
14793 return true;
14794
14795 switch (code)
14796 {
14797 case POINTER_PLUS_EXPR:
14798 case PLUS_EXPR:
14799 if (FLOAT_TYPE_P (type))
14800 return RECURSE (op0) && RECURSE (op1);
14801
14802 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14803 both unsigned and at least 2 bits shorter than the result. */
14804 if (TREE_CODE (type) == INTEGER_TYPE
14805 && TREE_CODE (op0) == NOP_EXPR
14806 && TREE_CODE (op1) == NOP_EXPR)
14807 {
14808 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14809 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14810 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14811 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14812 {
14813 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14814 TYPE_PRECISION (inner2)) + 1;
14815 return prec < TYPE_PRECISION (type);
14816 }
14817 }
14818 break;
14819
14820 case MULT_EXPR:
14821 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14822 {
14823 /* x * x is always non-negative for floating point x
14824 or without overflow. */
14825 if (operand_equal_p (op0, op1, 0)
14826 || (RECURSE (op0) && RECURSE (op1)))
14827 {
14828 if (ANY_INTEGRAL_TYPE_P (type)
14829 && TYPE_OVERFLOW_UNDEFINED (type))
14830 *strict_overflow_p = true;
14831 return true;
14832 }
14833 }
14834
14835 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14836 both unsigned and their total bits is shorter than the result. */
14837 if (TREE_CODE (type) == INTEGER_TYPE
14838 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14839 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14840 {
14841 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14842 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14843 : TREE_TYPE (op0);
14844 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14845 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14846 : TREE_TYPE (op1);
14847
14848 bool unsigned0 = TYPE_UNSIGNED (inner0);
14849 bool unsigned1 = TYPE_UNSIGNED (inner1);
14850
14851 if (TREE_CODE (op0) == INTEGER_CST)
14852 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14853
14854 if (TREE_CODE (op1) == INTEGER_CST)
14855 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14856
14857 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14858 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14859 {
14860 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14861 ? tree_int_cst_min_precision (op0, UNSIGNED)
14862 : TYPE_PRECISION (inner0);
14863
14864 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14865 ? tree_int_cst_min_precision (op1, UNSIGNED)
14866 : TYPE_PRECISION (inner1);
14867
14868 return precision0 + precision1 < TYPE_PRECISION (type);
14869 }
14870 }
14871 return false;
14872
14873 case BIT_AND_EXPR:
14874 return RECURSE (op0) || RECURSE (op1);
14875
14876 case MAX_EXPR:
14877 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14878 things. */
14879 if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14880 return RECURSE (op0) && RECURSE (op1);
14881 return RECURSE (op0) || RECURSE (op1);
14882
14883 case BIT_IOR_EXPR:
14884 case BIT_XOR_EXPR:
14885 case MIN_EXPR:
14886 case RDIV_EXPR:
14887 case TRUNC_DIV_EXPR:
14888 case CEIL_DIV_EXPR:
14889 case FLOOR_DIV_EXPR:
14890 case ROUND_DIV_EXPR:
14891 return RECURSE (op0) && RECURSE (op1);
14892
14893 case TRUNC_MOD_EXPR:
14894 return RECURSE (op0);
14895
14896 case FLOOR_MOD_EXPR:
14897 return RECURSE (op1);
14898
14899 case CEIL_MOD_EXPR:
14900 case ROUND_MOD_EXPR:
14901 default:
14902 return tree_simple_nonnegative_warnv_p (code, type);
14903 }
14904
14905 /* We don't know sign of `t', so be conservative and return false. */
14906 return false;
14907 }
14908
14909 /* Return true if T is known to be non-negative. If the return
14910 value is based on the assumption that signed overflow is undefined,
14911 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14912 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14913
14914 bool
14915 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14916 {
14917 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14918 return true;
14919
14920 switch (TREE_CODE (t))
14921 {
14922 case INTEGER_CST:
14923 return tree_int_cst_sgn (t) >= 0;
14924
14925 case REAL_CST:
14926 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14927
14928 case FIXED_CST:
14929 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14930
14931 case COND_EXPR:
14932 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14933
14934 case SSA_NAME:
14935 /* Limit the depth of recursion to avoid quadratic behavior.
14936 This is expected to catch almost all occurrences in practice.
14937 If this code misses important cases that unbounded recursion
14938 would not, passes that need this information could be revised
14939 to provide it through dataflow propagation. */
14940 return (!name_registered_for_update_p (t)
14941 && depth < param_max_ssa_name_query_depth
14942 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14943 strict_overflow_p, depth));
14944
14945 default:
14946 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14947 }
14948 }
14949
14950 /* Return true if T is known to be non-negative. If the return
14951 value is based on the assumption that signed overflow is undefined,
14952 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14953 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14954
14955 bool
14956 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14957 bool *strict_overflow_p, int depth)
14958 {
14959 switch (fn)
14960 {
14961 CASE_CFN_ACOS:
14962 CASE_CFN_ACOS_FN:
14963 CASE_CFN_ACOSH:
14964 CASE_CFN_ACOSH_FN:
14965 CASE_CFN_CABS:
14966 CASE_CFN_CABS_FN:
14967 CASE_CFN_COSH:
14968 CASE_CFN_COSH_FN:
14969 CASE_CFN_ERFC:
14970 CASE_CFN_ERFC_FN:
14971 CASE_CFN_EXP:
14972 CASE_CFN_EXP_FN:
14973 CASE_CFN_EXP10:
14974 CASE_CFN_EXP2:
14975 CASE_CFN_EXP2_FN:
14976 CASE_CFN_FABS:
14977 CASE_CFN_FABS_FN:
14978 CASE_CFN_FDIM:
14979 CASE_CFN_FDIM_FN:
14980 CASE_CFN_HYPOT:
14981 CASE_CFN_HYPOT_FN:
14982 CASE_CFN_POW10:
14983 CASE_CFN_FFS:
14984 CASE_CFN_PARITY:
14985 CASE_CFN_POPCOUNT:
14986 CASE_CFN_CLRSB:
14987 case CFN_BUILT_IN_BSWAP16:
14988 case CFN_BUILT_IN_BSWAP32:
14989 case CFN_BUILT_IN_BSWAP64:
14990 case CFN_BUILT_IN_BSWAP128:
14991 /* Always true. */
14992 return true;
14993
14994 CASE_CFN_CLZ:
14995 CASE_CFN_CTZ:
14996 if (arg1)
14997 return RECURSE (arg1);
14998 return true;
14999
15000 CASE_CFN_SQRT:
15001 CASE_CFN_SQRT_FN:
15002 /* sqrt(-0.0) is -0.0. */
15003 if (!HONOR_SIGNED_ZEROS (type))
15004 return true;
15005 return RECURSE (arg0);
15006
15007 CASE_CFN_ASINH:
15008 CASE_CFN_ASINH_FN:
15009 CASE_CFN_ATAN:
15010 CASE_CFN_ATAN_FN:
15011 CASE_CFN_ATANH:
15012 CASE_CFN_ATANH_FN:
15013 CASE_CFN_CBRT:
15014 CASE_CFN_CBRT_FN:
15015 CASE_CFN_CEIL:
15016 CASE_CFN_CEIL_FN:
15017 CASE_CFN_ERF:
15018 CASE_CFN_ERF_FN:
15019 CASE_CFN_EXPM1:
15020 CASE_CFN_EXPM1_FN:
15021 CASE_CFN_FLOOR:
15022 CASE_CFN_FLOOR_FN:
15023 CASE_CFN_FMOD:
15024 CASE_CFN_FMOD_FN:
15025 CASE_CFN_FREXP:
15026 CASE_CFN_FREXP_FN:
15027 CASE_CFN_ICEIL:
15028 CASE_CFN_IFLOOR:
15029 CASE_CFN_IRINT:
15030 CASE_CFN_IROUND:
15031 CASE_CFN_LCEIL:
15032 CASE_CFN_LDEXP:
15033 CASE_CFN_LFLOOR:
15034 CASE_CFN_LLCEIL:
15035 CASE_CFN_LLFLOOR:
15036 CASE_CFN_LLRINT:
15037 CASE_CFN_LLRINT_FN:
15038 CASE_CFN_LLROUND:
15039 CASE_CFN_LLROUND_FN:
15040 CASE_CFN_LRINT:
15041 CASE_CFN_LRINT_FN:
15042 CASE_CFN_LROUND:
15043 CASE_CFN_LROUND_FN:
15044 CASE_CFN_MODF:
15045 CASE_CFN_MODF_FN:
15046 CASE_CFN_NEARBYINT:
15047 CASE_CFN_NEARBYINT_FN:
15048 CASE_CFN_RINT:
15049 CASE_CFN_RINT_FN:
15050 CASE_CFN_ROUND:
15051 CASE_CFN_ROUND_FN:
15052 CASE_CFN_ROUNDEVEN:
15053 CASE_CFN_ROUNDEVEN_FN:
15054 CASE_CFN_SCALB:
15055 CASE_CFN_SCALBLN:
15056 CASE_CFN_SCALBLN_FN:
15057 CASE_CFN_SCALBN:
15058 CASE_CFN_SCALBN_FN:
15059 CASE_CFN_SIGNBIT:
15060 CASE_CFN_SIGNIFICAND:
15061 CASE_CFN_SINH:
15062 CASE_CFN_SINH_FN:
15063 CASE_CFN_TANH:
15064 CASE_CFN_TANH_FN:
15065 CASE_CFN_TRUNC:
15066 CASE_CFN_TRUNC_FN:
15067 /* True if the 1st argument is nonnegative. */
15068 return RECURSE (arg0);
15069
15070 CASE_CFN_FMAX:
15071 CASE_CFN_FMAX_FN:
15072 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15073 things. In the presence of sNaNs, we're only guaranteed to be
15074 non-negative if both operands are non-negative. In the presence
15075 of qNaNs, we're non-negative if either operand is non-negative
15076 and can't be a qNaN, or if both operands are non-negative. */
15077 if (tree_expr_maybe_signaling_nan_p (arg0)
15078 || tree_expr_maybe_signaling_nan_p (arg1))
15079 return RECURSE (arg0) && RECURSE (arg1);
15080 return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15081 || RECURSE (arg1))
15082 : (RECURSE (arg1)
15083 && !tree_expr_maybe_nan_p (arg1));
15084
15085 CASE_CFN_FMIN:
15086 CASE_CFN_FMIN_FN:
15087 /* True if the 1st AND 2nd arguments are nonnegative. */
15088 return RECURSE (arg0) && RECURSE (arg1);
15089
15090 CASE_CFN_COPYSIGN:
15091 CASE_CFN_COPYSIGN_FN:
15092 /* True if the 2nd argument is nonnegative. */
15093 return RECURSE (arg1);
15094
15095 CASE_CFN_POWI:
15096 /* True if the 1st argument is nonnegative or the second
15097 argument is an even integer. */
15098 if (TREE_CODE (arg1) == INTEGER_CST
15099 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15100 return true;
15101 return RECURSE (arg0);
15102
15103 CASE_CFN_POW:
15104 CASE_CFN_POW_FN:
15105 /* True if the 1st argument is nonnegative or the second
15106 argument is an even integer valued real. */
15107 if (TREE_CODE (arg1) == REAL_CST)
15108 {
15109 REAL_VALUE_TYPE c;
15110 HOST_WIDE_INT n;
15111
15112 c = TREE_REAL_CST (arg1);
15113 n = real_to_integer (&c);
15114 if ((n & 1) == 0)
15115 {
15116 REAL_VALUE_TYPE cint;
15117 real_from_integer (&cint, VOIDmode, n, SIGNED);
15118 if (real_identical (&c, &cint))
15119 return true;
15120 }
15121 }
15122 return RECURSE (arg0);
15123
15124 default:
15125 break;
15126 }
15127 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15128 }
15129
15130 /* Return true if T is known to be non-negative. If the return
15131 value is based on the assumption that signed overflow is undefined,
15132 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15133 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15134
15135 static bool
15136 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15137 {
15138 enum tree_code code = TREE_CODE (t);
15139 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15140 return true;
15141
15142 switch (code)
15143 {
15144 case TARGET_EXPR:
15145 {
15146 tree temp = TARGET_EXPR_SLOT (t);
15147 t = TARGET_EXPR_INITIAL (t);
15148
15149 /* If the initializer is non-void, then it's a normal expression
15150 that will be assigned to the slot. */
15151 if (!VOID_TYPE_P (TREE_TYPE (t)))
15152 return RECURSE (t);
15153
15154 /* Otherwise, the initializer sets the slot in some way. One common
15155 way is an assignment statement at the end of the initializer. */
15156 while (1)
15157 {
15158 if (TREE_CODE (t) == BIND_EXPR)
15159 t = expr_last (BIND_EXPR_BODY (t));
15160 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15161 || TREE_CODE (t) == TRY_CATCH_EXPR)
15162 t = expr_last (TREE_OPERAND (t, 0));
15163 else if (TREE_CODE (t) == STATEMENT_LIST)
15164 t = expr_last (t);
15165 else
15166 break;
15167 }
15168 if (TREE_CODE (t) == MODIFY_EXPR
15169 && TREE_OPERAND (t, 0) == temp)
15170 return RECURSE (TREE_OPERAND (t, 1));
15171
15172 return false;
15173 }
15174
15175 case CALL_EXPR:
15176 {
15177 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15178 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15179
15180 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15181 get_call_combined_fn (t),
15182 arg0,
15183 arg1,
15184 strict_overflow_p, depth);
15185 }
15186 case COMPOUND_EXPR:
15187 case MODIFY_EXPR:
15188 return RECURSE (TREE_OPERAND (t, 1));
15189
15190 case BIND_EXPR:
15191 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15192
15193 case SAVE_EXPR:
15194 return RECURSE (TREE_OPERAND (t, 0));
15195
15196 default:
15197 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15198 }
15199 }
15200
15201 #undef RECURSE
15202 #undef tree_expr_nonnegative_warnv_p
15203
15204 /* Return true if T is known to be non-negative. If the return
15205 value is based on the assumption that signed overflow is undefined,
15206 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15207 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15208
15209 bool
15210 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15211 {
15212 enum tree_code code;
15213 if (t == error_mark_node)
15214 return false;
15215
15216 code = TREE_CODE (t);
15217 switch (TREE_CODE_CLASS (code))
15218 {
15219 case tcc_binary:
15220 case tcc_comparison:
15221 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15222 TREE_TYPE (t),
15223 TREE_OPERAND (t, 0),
15224 TREE_OPERAND (t, 1),
15225 strict_overflow_p, depth);
15226
15227 case tcc_unary:
15228 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15229 TREE_TYPE (t),
15230 TREE_OPERAND (t, 0),
15231 strict_overflow_p, depth);
15232
15233 case tcc_constant:
15234 case tcc_declaration:
15235 case tcc_reference:
15236 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15237
15238 default:
15239 break;
15240 }
15241
15242 switch (code)
15243 {
15244 case TRUTH_AND_EXPR:
15245 case TRUTH_OR_EXPR:
15246 case TRUTH_XOR_EXPR:
15247 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15248 TREE_TYPE (t),
15249 TREE_OPERAND (t, 0),
15250 TREE_OPERAND (t, 1),
15251 strict_overflow_p, depth);
15252 case TRUTH_NOT_EXPR:
15253 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15254 TREE_TYPE (t),
15255 TREE_OPERAND (t, 0),
15256 strict_overflow_p, depth);
15257
15258 case COND_EXPR:
15259 case CONSTRUCTOR:
15260 case OBJ_TYPE_REF:
15261 case ADDR_EXPR:
15262 case WITH_SIZE_EXPR:
15263 case SSA_NAME:
15264 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15265
15266 default:
15267 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15268 }
15269 }
15270
15271 /* Return true if `t' is known to be non-negative. Handle warnings
15272 about undefined signed overflow. */
15273
15274 bool
15275 tree_expr_nonnegative_p (tree t)
15276 {
15277 bool ret, strict_overflow_p;
15278
15279 strict_overflow_p = false;
15280 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15281 if (strict_overflow_p)
15282 fold_overflow_warning (("assuming signed overflow does not occur when "
15283 "determining that expression is always "
15284 "non-negative"),
15285 WARN_STRICT_OVERFLOW_MISC);
15286 return ret;
15287 }
15288
15289
15290 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15291 For floating point we further ensure that T is not denormal.
15292 Similar logic is present in nonzero_address in rtlanal.h.
15293
15294 If the return value is based on the assumption that signed overflow
15295 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15296 change *STRICT_OVERFLOW_P. */
15297
15298 bool
15299 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15300 bool *strict_overflow_p)
15301 {
15302 switch (code)
15303 {
15304 case ABS_EXPR:
15305 return tree_expr_nonzero_warnv_p (op0,
15306 strict_overflow_p);
15307
15308 case NOP_EXPR:
15309 {
15310 tree inner_type = TREE_TYPE (op0);
15311 tree outer_type = type;
15312
15313 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15314 && tree_expr_nonzero_warnv_p (op0,
15315 strict_overflow_p));
15316 }
15317 break;
15318
15319 case NON_LVALUE_EXPR:
15320 return tree_expr_nonzero_warnv_p (op0,
15321 strict_overflow_p);
15322
15323 default:
15324 break;
15325 }
15326
15327 return false;
15328 }
15329
15330 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15331 For floating point we further ensure that T is not denormal.
15332 Similar logic is present in nonzero_address in rtlanal.h.
15333
15334 If the return value is based on the assumption that signed overflow
15335 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15336 change *STRICT_OVERFLOW_P. */
15337
15338 bool
15339 tree_binary_nonzero_warnv_p (enum tree_code code,
15340 tree type,
15341 tree op0,
15342 tree op1, bool *strict_overflow_p)
15343 {
15344 bool sub_strict_overflow_p;
15345 switch (code)
15346 {
15347 case POINTER_PLUS_EXPR:
15348 case PLUS_EXPR:
15349 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15350 {
15351 /* With the presence of negative values it is hard
15352 to say something. */
15353 sub_strict_overflow_p = false;
15354 if (!tree_expr_nonnegative_warnv_p (op0,
15355 &sub_strict_overflow_p)
15356 || !tree_expr_nonnegative_warnv_p (op1,
15357 &sub_strict_overflow_p))
15358 return false;
15359 /* One of operands must be positive and the other non-negative. */
15360 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15361 overflows, on a twos-complement machine the sum of two
15362 nonnegative numbers can never be zero. */
15363 return (tree_expr_nonzero_warnv_p (op0,
15364 strict_overflow_p)
15365 || tree_expr_nonzero_warnv_p (op1,
15366 strict_overflow_p));
15367 }
15368 break;
15369
15370 case MULT_EXPR:
15371 if (TYPE_OVERFLOW_UNDEFINED (type))
15372 {
15373 if (tree_expr_nonzero_warnv_p (op0,
15374 strict_overflow_p)
15375 && tree_expr_nonzero_warnv_p (op1,
15376 strict_overflow_p))
15377 {
15378 *strict_overflow_p = true;
15379 return true;
15380 }
15381 }
15382 break;
15383
15384 case MIN_EXPR:
15385 sub_strict_overflow_p = false;
15386 if (tree_expr_nonzero_warnv_p (op0,
15387 &sub_strict_overflow_p)
15388 && tree_expr_nonzero_warnv_p (op1,
15389 &sub_strict_overflow_p))
15390 {
15391 if (sub_strict_overflow_p)
15392 *strict_overflow_p = true;
15393 }
15394 break;
15395
15396 case MAX_EXPR:
15397 sub_strict_overflow_p = false;
15398 if (tree_expr_nonzero_warnv_p (op0,
15399 &sub_strict_overflow_p))
15400 {
15401 if (sub_strict_overflow_p)
15402 *strict_overflow_p = true;
15403
15404 /* When both operands are nonzero, then MAX must be too. */
15405 if (tree_expr_nonzero_warnv_p (op1,
15406 strict_overflow_p))
15407 return true;
15408
15409 /* MAX where operand 0 is positive is positive. */
15410 return tree_expr_nonnegative_warnv_p (op0,
15411 strict_overflow_p);
15412 }
15413 /* MAX where operand 1 is positive is positive. */
15414 else if (tree_expr_nonzero_warnv_p (op1,
15415 &sub_strict_overflow_p)
15416 && tree_expr_nonnegative_warnv_p (op1,
15417 &sub_strict_overflow_p))
15418 {
15419 if (sub_strict_overflow_p)
15420 *strict_overflow_p = true;
15421 return true;
15422 }
15423 break;
15424
15425 case BIT_IOR_EXPR:
15426 return (tree_expr_nonzero_warnv_p (op1,
15427 strict_overflow_p)
15428 || tree_expr_nonzero_warnv_p (op0,
15429 strict_overflow_p));
15430
15431 default:
15432 break;
15433 }
15434
15435 return false;
15436 }
15437
15438 /* Return true when T is an address and is known to be nonzero.
15439 For floating point we further ensure that T is not denormal.
15440 Similar logic is present in nonzero_address in rtlanal.h.
15441
15442 If the return value is based on the assumption that signed overflow
15443 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15444 change *STRICT_OVERFLOW_P. */
15445
15446 bool
15447 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15448 {
15449 bool sub_strict_overflow_p;
15450 switch (TREE_CODE (t))
15451 {
15452 case INTEGER_CST:
15453 return !integer_zerop (t);
15454
15455 case ADDR_EXPR:
15456 {
15457 tree base = TREE_OPERAND (t, 0);
15458
15459 if (!DECL_P (base))
15460 base = get_base_address (base);
15461
15462 if (base && TREE_CODE (base) == TARGET_EXPR)
15463 base = TARGET_EXPR_SLOT (base);
15464
15465 if (!base)
15466 return false;
15467
15468 /* For objects in symbol table check if we know they are non-zero.
15469 Don't do anything for variables and functions before symtab is built;
15470 it is quite possible that they will be declared weak later. */
15471 int nonzero_addr = maybe_nonzero_address (base);
15472 if (nonzero_addr >= 0)
15473 return nonzero_addr;
15474
15475 /* Constants are never weak. */
15476 if (CONSTANT_CLASS_P (base))
15477 return true;
15478
15479 return false;
15480 }
15481
15482 case COND_EXPR:
15483 sub_strict_overflow_p = false;
15484 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15485 &sub_strict_overflow_p)
15486 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15487 &sub_strict_overflow_p))
15488 {
15489 if (sub_strict_overflow_p)
15490 *strict_overflow_p = true;
15491 return true;
15492 }
15493 break;
15494
15495 case SSA_NAME:
15496 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15497 break;
15498 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15499
15500 default:
15501 break;
15502 }
15503 return false;
15504 }
15505
15506 #define integer_valued_real_p(X) \
15507 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15508
15509 #define RECURSE(X) \
15510 ((integer_valued_real_p) (X, depth + 1))
15511
15512 /* Return true if the floating point result of (CODE OP0) has an
15513 integer value. We also allow +Inf, -Inf and NaN to be considered
15514 integer values. Return false for signaling NaN.
15515
15516 DEPTH is the current nesting depth of the query. */
15517
15518 bool
15519 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15520 {
15521 switch (code)
15522 {
15523 case FLOAT_EXPR:
15524 return true;
15525
15526 case ABS_EXPR:
15527 return RECURSE (op0);
15528
15529 CASE_CONVERT:
15530 {
15531 tree type = TREE_TYPE (op0);
15532 if (TREE_CODE (type) == INTEGER_TYPE)
15533 return true;
15534 if (SCALAR_FLOAT_TYPE_P (type))
15535 return RECURSE (op0);
15536 break;
15537 }
15538
15539 default:
15540 break;
15541 }
15542 return false;
15543 }
15544
15545 /* Return true if the floating point result of (CODE OP0 OP1) has an
15546 integer value. We also allow +Inf, -Inf and NaN to be considered
15547 integer values. Return false for signaling NaN.
15548
15549 DEPTH is the current nesting depth of the query. */
15550
15551 bool
15552 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15553 {
15554 switch (code)
15555 {
15556 case PLUS_EXPR:
15557 case MINUS_EXPR:
15558 case MULT_EXPR:
15559 case MIN_EXPR:
15560 case MAX_EXPR:
15561 return RECURSE (op0) && RECURSE (op1);
15562
15563 default:
15564 break;
15565 }
15566 return false;
15567 }
15568
15569 /* Return true if the floating point result of calling FNDECL with arguments
15570 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15571 considered integer values. Return false for signaling NaN. If FNDECL
15572 takes fewer than 2 arguments, the remaining ARGn are null.
15573
15574 DEPTH is the current nesting depth of the query. */
15575
15576 bool
15577 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15578 {
15579 switch (fn)
15580 {
15581 CASE_CFN_CEIL:
15582 CASE_CFN_CEIL_FN:
15583 CASE_CFN_FLOOR:
15584 CASE_CFN_FLOOR_FN:
15585 CASE_CFN_NEARBYINT:
15586 CASE_CFN_NEARBYINT_FN:
15587 CASE_CFN_RINT:
15588 CASE_CFN_RINT_FN:
15589 CASE_CFN_ROUND:
15590 CASE_CFN_ROUND_FN:
15591 CASE_CFN_ROUNDEVEN:
15592 CASE_CFN_ROUNDEVEN_FN:
15593 CASE_CFN_TRUNC:
15594 CASE_CFN_TRUNC_FN:
15595 return true;
15596
15597 CASE_CFN_FMIN:
15598 CASE_CFN_FMIN_FN:
15599 CASE_CFN_FMAX:
15600 CASE_CFN_FMAX_FN:
15601 return RECURSE (arg0) && RECURSE (arg1);
15602
15603 default:
15604 break;
15605 }
15606 return false;
15607 }
15608
15609 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15610 has an integer value. We also allow +Inf, -Inf and NaN to be
15611 considered integer values. Return false for signaling NaN.
15612
15613 DEPTH is the current nesting depth of the query. */
15614
15615 bool
15616 integer_valued_real_single_p (tree t, int depth)
15617 {
15618 switch (TREE_CODE (t))
15619 {
15620 case REAL_CST:
15621 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15622
15623 case COND_EXPR:
15624 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15625
15626 case SSA_NAME:
15627 /* Limit the depth of recursion to avoid quadratic behavior.
15628 This is expected to catch almost all occurrences in practice.
15629 If this code misses important cases that unbounded recursion
15630 would not, passes that need this information could be revised
15631 to provide it through dataflow propagation. */
15632 return (!name_registered_for_update_p (t)
15633 && depth < param_max_ssa_name_query_depth
15634 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15635 depth));
15636
15637 default:
15638 break;
15639 }
15640 return false;
15641 }
15642
15643 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15644 has an integer value. We also allow +Inf, -Inf and NaN to be
15645 considered integer values. Return false for signaling NaN.
15646
15647 DEPTH is the current nesting depth of the query. */
15648
15649 static bool
15650 integer_valued_real_invalid_p (tree t, int depth)
15651 {
15652 switch (TREE_CODE (t))
15653 {
15654 case COMPOUND_EXPR:
15655 case MODIFY_EXPR:
15656 case BIND_EXPR:
15657 return RECURSE (TREE_OPERAND (t, 1));
15658
15659 case SAVE_EXPR:
15660 return RECURSE (TREE_OPERAND (t, 0));
15661
15662 default:
15663 break;
15664 }
15665 return false;
15666 }
15667
15668 #undef RECURSE
15669 #undef integer_valued_real_p
15670
15671 /* Return true if the floating point expression T has an integer value.
15672 We also allow +Inf, -Inf and NaN to be considered integer values.
15673 Return false for signaling NaN.
15674
15675 DEPTH is the current nesting depth of the query. */
15676
15677 bool
15678 integer_valued_real_p (tree t, int depth)
15679 {
15680 if (t == error_mark_node)
15681 return false;
15682
15683 STRIP_ANY_LOCATION_WRAPPER (t);
15684
15685 tree_code code = TREE_CODE (t);
15686 switch (TREE_CODE_CLASS (code))
15687 {
15688 case tcc_binary:
15689 case tcc_comparison:
15690 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15691 TREE_OPERAND (t, 1), depth);
15692
15693 case tcc_unary:
15694 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15695
15696 case tcc_constant:
15697 case tcc_declaration:
15698 case tcc_reference:
15699 return integer_valued_real_single_p (t, depth);
15700
15701 default:
15702 break;
15703 }
15704
15705 switch (code)
15706 {
15707 case COND_EXPR:
15708 case SSA_NAME:
15709 return integer_valued_real_single_p (t, depth);
15710
15711 case CALL_EXPR:
15712 {
15713 tree arg0 = (call_expr_nargs (t) > 0
15714 ? CALL_EXPR_ARG (t, 0)
15715 : NULL_TREE);
15716 tree arg1 = (call_expr_nargs (t) > 1
15717 ? CALL_EXPR_ARG (t, 1)
15718 : NULL_TREE);
15719 return integer_valued_real_call_p (get_call_combined_fn (t),
15720 arg0, arg1, depth);
15721 }
15722
15723 default:
15724 return integer_valued_real_invalid_p (t, depth);
15725 }
15726 }
15727
15728 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15729 attempt to fold the expression to a constant without modifying TYPE,
15730 OP0 or OP1.
15731
15732 If the expression could be simplified to a constant, then return
15733 the constant. If the expression would not be simplified to a
15734 constant, then return NULL_TREE. */
15735
15736 tree
15737 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15738 {
15739 tree tem = fold_binary (code, type, op0, op1);
15740 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15741 }
15742
15743 /* Given the components of a unary expression CODE, TYPE and OP0,
15744 attempt to fold the expression to a constant without modifying
15745 TYPE or OP0.
15746
15747 If the expression could be simplified to a constant, then return
15748 the constant. If the expression would not be simplified to a
15749 constant, then return NULL_TREE. */
15750
15751 tree
15752 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15753 {
15754 tree tem = fold_unary (code, type, op0);
15755 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15756 }
15757
15758 /* If EXP represents referencing an element in a constant string
15759 (either via pointer arithmetic or array indexing), return the
15760 tree representing the value accessed, otherwise return NULL. */
15761
15762 tree
15763 fold_read_from_constant_string (tree exp)
15764 {
15765 if ((INDIRECT_REF_P (exp)
15766 || TREE_CODE (exp) == ARRAY_REF)
15767 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15768 {
15769 tree exp1 = TREE_OPERAND (exp, 0);
15770 tree index;
15771 tree string;
15772 location_t loc = EXPR_LOCATION (exp);
15773
15774 if (INDIRECT_REF_P (exp))
15775 string = string_constant (exp1, &index, NULL, NULL);
15776 else
15777 {
15778 tree low_bound = array_ref_low_bound (exp);
15779 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15780
15781 /* Optimize the special-case of a zero lower bound.
15782
15783 We convert the low_bound to sizetype to avoid some problems
15784 with constant folding. (E.g. suppose the lower bound is 1,
15785 and its mode is QI. Without the conversion,l (ARRAY
15786 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15787 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15788 if (! integer_zerop (low_bound))
15789 index = size_diffop_loc (loc, index,
15790 fold_convert_loc (loc, sizetype, low_bound));
15791
15792 string = exp1;
15793 }
15794
15795 scalar_int_mode char_mode;
15796 if (string
15797 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15798 && TREE_CODE (string) == STRING_CST
15799 && tree_fits_uhwi_p (index)
15800 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15801 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15802 &char_mode)
15803 && GET_MODE_SIZE (char_mode) == 1)
15804 return build_int_cst_type (TREE_TYPE (exp),
15805 (TREE_STRING_POINTER (string)
15806 [TREE_INT_CST_LOW (index)]));
15807 }
15808 return NULL;
15809 }
15810
15811 /* Folds a read from vector element at IDX of vector ARG. */
15812
15813 tree
15814 fold_read_from_vector (tree arg, poly_uint64 idx)
15815 {
15816 unsigned HOST_WIDE_INT i;
15817 if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15818 && known_ge (idx, 0u)
15819 && idx.is_constant (&i))
15820 {
15821 if (TREE_CODE (arg) == VECTOR_CST)
15822 return VECTOR_CST_ELT (arg, i);
15823 else if (TREE_CODE (arg) == CONSTRUCTOR)
15824 {
15825 if (CONSTRUCTOR_NELTS (arg)
15826 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15827 return NULL_TREE;
15828 if (i >= CONSTRUCTOR_NELTS (arg))
15829 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15830 return CONSTRUCTOR_ELT (arg, i)->value;
15831 }
15832 }
15833 return NULL_TREE;
15834 }
15835
15836 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15837 an integer constant, real, or fixed-point constant.
15838
15839 TYPE is the type of the result. */
15840
15841 static tree
15842 fold_negate_const (tree arg0, tree type)
15843 {
15844 tree t = NULL_TREE;
15845
15846 switch (TREE_CODE (arg0))
15847 {
15848 case REAL_CST:
15849 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15850 break;
15851
15852 case FIXED_CST:
15853 {
15854 FIXED_VALUE_TYPE f;
15855 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15856 &(TREE_FIXED_CST (arg0)), NULL,
15857 TYPE_SATURATING (type));
15858 t = build_fixed (type, f);
15859 /* Propagate overflow flags. */
15860 if (overflow_p | TREE_OVERFLOW (arg0))
15861 TREE_OVERFLOW (t) = 1;
15862 break;
15863 }
15864
15865 default:
15866 if (poly_int_tree_p (arg0))
15867 {
15868 wi::overflow_type overflow;
15869 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15870 t = force_fit_type (type, res, 1,
15871 (overflow && ! TYPE_UNSIGNED (type))
15872 || TREE_OVERFLOW (arg0));
15873 break;
15874 }
15875
15876 gcc_unreachable ();
15877 }
15878
15879 return t;
15880 }
15881
15882 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15883 an integer constant or real constant.
15884
15885 TYPE is the type of the result. */
15886
15887 tree
15888 fold_abs_const (tree arg0, tree type)
15889 {
15890 tree t = NULL_TREE;
15891
15892 switch (TREE_CODE (arg0))
15893 {
15894 case INTEGER_CST:
15895 {
15896 /* If the value is unsigned or non-negative, then the absolute value
15897 is the same as the ordinary value. */
15898 wide_int val = wi::to_wide (arg0);
15899 wi::overflow_type overflow = wi::OVF_NONE;
15900 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15901 ;
15902
15903 /* If the value is negative, then the absolute value is
15904 its negation. */
15905 else
15906 val = wi::neg (val, &overflow);
15907
15908 /* Force to the destination type, set TREE_OVERFLOW for signed
15909 TYPE only. */
15910 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15911 }
15912 break;
15913
15914 case REAL_CST:
15915 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15916 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15917 else
15918 t = arg0;
15919 break;
15920
15921 default:
15922 gcc_unreachable ();
15923 }
15924
15925 return t;
15926 }
15927
15928 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15929 constant. TYPE is the type of the result. */
15930
15931 static tree
15932 fold_not_const (const_tree arg0, tree type)
15933 {
15934 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15935
15936 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15937 }
15938
15939 /* Given CODE, a relational operator, the target type, TYPE and two
15940 constant operands OP0 and OP1, return the result of the
15941 relational operation. If the result is not a compile time
15942 constant, then return NULL_TREE. */
15943
15944 static tree
15945 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15946 {
15947 int result, invert;
15948
15949 /* From here on, the only cases we handle are when the result is
15950 known to be a constant. */
15951
15952 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15953 {
15954 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15955 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15956
15957 /* Handle the cases where either operand is a NaN. */
15958 if (real_isnan (c0) || real_isnan (c1))
15959 {
15960 switch (code)
15961 {
15962 case EQ_EXPR:
15963 case ORDERED_EXPR:
15964 result = 0;
15965 break;
15966
15967 case NE_EXPR:
15968 case UNORDERED_EXPR:
15969 case UNLT_EXPR:
15970 case UNLE_EXPR:
15971 case UNGT_EXPR:
15972 case UNGE_EXPR:
15973 case UNEQ_EXPR:
15974 result = 1;
15975 break;
15976
15977 case LT_EXPR:
15978 case LE_EXPR:
15979 case GT_EXPR:
15980 case GE_EXPR:
15981 case LTGT_EXPR:
15982 if (flag_trapping_math)
15983 return NULL_TREE;
15984 result = 0;
15985 break;
15986
15987 default:
15988 gcc_unreachable ();
15989 }
15990
15991 return constant_boolean_node (result, type);
15992 }
15993
15994 return constant_boolean_node (real_compare (code, c0, c1), type);
15995 }
15996
15997 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15998 {
15999 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16000 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16001 return constant_boolean_node (fixed_compare (code, c0, c1), type);
16002 }
16003
16004 /* Handle equality/inequality of complex constants. */
16005 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16006 {
16007 tree rcond = fold_relational_const (code, type,
16008 TREE_REALPART (op0),
16009 TREE_REALPART (op1));
16010 tree icond = fold_relational_const (code, type,
16011 TREE_IMAGPART (op0),
16012 TREE_IMAGPART (op1));
16013 if (code == EQ_EXPR)
16014 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16015 else if (code == NE_EXPR)
16016 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16017 else
16018 return NULL_TREE;
16019 }
16020
16021 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16022 {
16023 if (!VECTOR_TYPE_P (type))
16024 {
16025 /* Have vector comparison with scalar boolean result. */
16026 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
16027 && known_eq (VECTOR_CST_NELTS (op0),
16028 VECTOR_CST_NELTS (op1)));
16029 unsigned HOST_WIDE_INT nunits;
16030 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
16031 return NULL_TREE;
16032 for (unsigned i = 0; i < nunits; i++)
16033 {
16034 tree elem0 = VECTOR_CST_ELT (op0, i);
16035 tree elem1 = VECTOR_CST_ELT (op1, i);
16036 tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
16037 if (tmp == NULL_TREE)
16038 return NULL_TREE;
16039 if (integer_zerop (tmp))
16040 return constant_boolean_node (code == NE_EXPR, type);
16041 }
16042 return constant_boolean_node (code == EQ_EXPR, type);
16043 }
16044 tree_vector_builder elts;
16045 if (!elts.new_binary_operation (type, op0, op1, false))
16046 return NULL_TREE;
16047 unsigned int count = elts.encoded_nelts ();
16048 for (unsigned i = 0; i < count; i++)
16049 {
16050 tree elem_type = TREE_TYPE (type);
16051 tree elem0 = VECTOR_CST_ELT (op0, i);
16052 tree elem1 = VECTOR_CST_ELT (op1, i);
16053
16054 tree tem = fold_relational_const (code, elem_type,
16055 elem0, elem1);
16056
16057 if (tem == NULL_TREE)
16058 return NULL_TREE;
16059
16060 elts.quick_push (build_int_cst (elem_type,
16061 integer_zerop (tem) ? 0 : -1));
16062 }
16063
16064 return elts.build ();
16065 }
16066
16067 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16068
16069 To compute GT, swap the arguments and do LT.
16070 To compute GE, do LT and invert the result.
16071 To compute LE, swap the arguments, do LT and invert the result.
16072 To compute NE, do EQ and invert the result.
16073
16074 Therefore, the code below must handle only EQ and LT. */
16075
16076 if (code == LE_EXPR || code == GT_EXPR)
16077 {
16078 std::swap (op0, op1);
16079 code = swap_tree_comparison (code);
16080 }
16081
16082 /* Note that it is safe to invert for real values here because we
16083 have already handled the one case that it matters. */
16084
16085 invert = 0;
16086 if (code == NE_EXPR || code == GE_EXPR)
16087 {
16088 invert = 1;
16089 code = invert_tree_comparison (code, false);
16090 }
16091
16092 /* Compute a result for LT or EQ if args permit;
16093 Otherwise return T. */
16094 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16095 {
16096 if (code == EQ_EXPR)
16097 result = tree_int_cst_equal (op0, op1);
16098 else
16099 result = tree_int_cst_lt (op0, op1);
16100 }
16101 else
16102 return NULL_TREE;
16103
16104 if (invert)
16105 result ^= 1;
16106 return constant_boolean_node (result, type);
16107 }
16108
16109 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16110 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16111 itself. */
16112
16113 tree
16114 fold_build_cleanup_point_expr (tree type, tree expr)
16115 {
16116 /* If the expression does not have side effects then we don't have to wrap
16117 it with a cleanup point expression. */
16118 if (!TREE_SIDE_EFFECTS (expr))
16119 return expr;
16120
16121 /* If the expression is a return, check to see if the expression inside the
16122 return has no side effects or the right hand side of the modify expression
16123 inside the return. If either don't have side effects set we don't need to
16124 wrap the expression in a cleanup point expression. Note we don't check the
16125 left hand side of the modify because it should always be a return decl. */
16126 if (TREE_CODE (expr) == RETURN_EXPR)
16127 {
16128 tree op = TREE_OPERAND (expr, 0);
16129 if (!op || !TREE_SIDE_EFFECTS (op))
16130 return expr;
16131 op = TREE_OPERAND (op, 1);
16132 if (!TREE_SIDE_EFFECTS (op))
16133 return expr;
16134 }
16135
16136 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16137 }
16138
16139 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16140 of an indirection through OP0, or NULL_TREE if no simplification is
16141 possible. */
16142
16143 tree
16144 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16145 {
16146 tree sub = op0;
16147 tree subtype;
16148 poly_uint64 const_op01;
16149
16150 STRIP_NOPS (sub);
16151 subtype = TREE_TYPE (sub);
16152 if (!POINTER_TYPE_P (subtype)
16153 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16154 return NULL_TREE;
16155
16156 if (TREE_CODE (sub) == ADDR_EXPR)
16157 {
16158 tree op = TREE_OPERAND (sub, 0);
16159 tree optype = TREE_TYPE (op);
16160
16161 /* *&CONST_DECL -> to the value of the const decl. */
16162 if (TREE_CODE (op) == CONST_DECL)
16163 return DECL_INITIAL (op);
16164 /* *&p => p; make sure to handle *&"str"[cst] here. */
16165 if (type == optype)
16166 {
16167 tree fop = fold_read_from_constant_string (op);
16168 if (fop)
16169 return fop;
16170 else
16171 return op;
16172 }
16173 /* *(foo *)&fooarray => fooarray[0] */
16174 else if (TREE_CODE (optype) == ARRAY_TYPE
16175 && type == TREE_TYPE (optype)
16176 && (!in_gimple_form
16177 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16178 {
16179 tree type_domain = TYPE_DOMAIN (optype);
16180 tree min_val = size_zero_node;
16181 if (type_domain && TYPE_MIN_VALUE (type_domain))
16182 min_val = TYPE_MIN_VALUE (type_domain);
16183 if (in_gimple_form
16184 && TREE_CODE (min_val) != INTEGER_CST)
16185 return NULL_TREE;
16186 return build4_loc (loc, ARRAY_REF, type, op, min_val,
16187 NULL_TREE, NULL_TREE);
16188 }
16189 /* *(foo *)&complexfoo => __real__ complexfoo */
16190 else if (TREE_CODE (optype) == COMPLEX_TYPE
16191 && type == TREE_TYPE (optype))
16192 return fold_build1_loc (loc, REALPART_EXPR, type, op);
16193 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16194 else if (VECTOR_TYPE_P (optype)
16195 && type == TREE_TYPE (optype))
16196 {
16197 tree part_width = TYPE_SIZE (type);
16198 tree index = bitsize_int (0);
16199 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16200 index);
16201 }
16202 }
16203
16204 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16205 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16206 {
16207 tree op00 = TREE_OPERAND (sub, 0);
16208 tree op01 = TREE_OPERAND (sub, 1);
16209
16210 STRIP_NOPS (op00);
16211 if (TREE_CODE (op00) == ADDR_EXPR)
16212 {
16213 tree op00type;
16214 op00 = TREE_OPERAND (op00, 0);
16215 op00type = TREE_TYPE (op00);
16216
16217 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16218 if (VECTOR_TYPE_P (op00type)
16219 && type == TREE_TYPE (op00type)
16220 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16221 but we want to treat offsets with MSB set as negative.
16222 For the code below negative offsets are invalid and
16223 TYPE_SIZE of the element is something unsigned, so
16224 check whether op01 fits into poly_int64, which implies
16225 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16226 then just use poly_uint64 because we want to treat the
16227 value as unsigned. */
16228 && tree_fits_poly_int64_p (op01))
16229 {
16230 tree part_width = TYPE_SIZE (type);
16231 poly_uint64 max_offset
16232 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16233 * TYPE_VECTOR_SUBPARTS (op00type));
16234 if (known_lt (const_op01, max_offset))
16235 {
16236 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16237 return fold_build3_loc (loc,
16238 BIT_FIELD_REF, type, op00,
16239 part_width, index);
16240 }
16241 }
16242 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16243 else if (TREE_CODE (op00type) == COMPLEX_TYPE
16244 && type == TREE_TYPE (op00type))
16245 {
16246 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16247 const_op01))
16248 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16249 }
16250 /* ((foo *)&fooarray)[1] => fooarray[1] */
16251 else if (TREE_CODE (op00type) == ARRAY_TYPE
16252 && type == TREE_TYPE (op00type))
16253 {
16254 tree type_domain = TYPE_DOMAIN (op00type);
16255 tree min_val = size_zero_node;
16256 if (type_domain && TYPE_MIN_VALUE (type_domain))
16257 min_val = TYPE_MIN_VALUE (type_domain);
16258 poly_uint64 type_size, index;
16259 if (poly_int_tree_p (min_val)
16260 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16261 && multiple_p (const_op01, type_size, &index))
16262 {
16263 poly_offset_int off = index + wi::to_poly_offset (min_val);
16264 op01 = wide_int_to_tree (sizetype, off);
16265 return build4_loc (loc, ARRAY_REF, type, op00, op01,
16266 NULL_TREE, NULL_TREE);
16267 }
16268 }
16269 }
16270 }
16271
16272 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16273 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16274 && type == TREE_TYPE (TREE_TYPE (subtype))
16275 && (!in_gimple_form
16276 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16277 {
16278 tree type_domain;
16279 tree min_val = size_zero_node;
16280 sub = build_fold_indirect_ref_loc (loc, sub);
16281 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16282 if (type_domain && TYPE_MIN_VALUE (type_domain))
16283 min_val = TYPE_MIN_VALUE (type_domain);
16284 if (in_gimple_form
16285 && TREE_CODE (min_val) != INTEGER_CST)
16286 return NULL_TREE;
16287 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16288 NULL_TREE);
16289 }
16290
16291 return NULL_TREE;
16292 }
16293
16294 /* Builds an expression for an indirection through T, simplifying some
16295 cases. */
16296
16297 tree
16298 build_fold_indirect_ref_loc (location_t loc, tree t)
16299 {
16300 tree type = TREE_TYPE (TREE_TYPE (t));
16301 tree sub = fold_indirect_ref_1 (loc, type, t);
16302
16303 if (sub)
16304 return sub;
16305
16306 return build1_loc (loc, INDIRECT_REF, type, t);
16307 }
16308
16309 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16310
16311 tree
16312 fold_indirect_ref_loc (location_t loc, tree t)
16313 {
16314 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16315
16316 if (sub)
16317 return sub;
16318 else
16319 return t;
16320 }
16321
16322 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16323 whose result is ignored. The type of the returned tree need not be
16324 the same as the original expression. */
16325
16326 tree
16327 fold_ignored_result (tree t)
16328 {
16329 if (!TREE_SIDE_EFFECTS (t))
16330 return integer_zero_node;
16331
16332 for (;;)
16333 switch (TREE_CODE_CLASS (TREE_CODE (t)))
16334 {
16335 case tcc_unary:
16336 t = TREE_OPERAND (t, 0);
16337 break;
16338
16339 case tcc_binary:
16340 case tcc_comparison:
16341 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16342 t = TREE_OPERAND (t, 0);
16343 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16344 t = TREE_OPERAND (t, 1);
16345 else
16346 return t;
16347 break;
16348
16349 case tcc_expression:
16350 switch (TREE_CODE (t))
16351 {
16352 case COMPOUND_EXPR:
16353 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16354 return t;
16355 t = TREE_OPERAND (t, 0);
16356 break;
16357
16358 case COND_EXPR:
16359 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16360 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16361 return t;
16362 t = TREE_OPERAND (t, 0);
16363 break;
16364
16365 default:
16366 return t;
16367 }
16368 break;
16369
16370 default:
16371 return t;
16372 }
16373 }
16374
16375 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16376
16377 tree
16378 round_up_loc (location_t loc, tree value, unsigned int divisor)
16379 {
16380 tree div = NULL_TREE;
16381
16382 if (divisor == 1)
16383 return value;
16384
16385 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16386 have to do anything. Only do this when we are not given a const,
16387 because in that case, this check is more expensive than just
16388 doing it. */
16389 if (TREE_CODE (value) != INTEGER_CST)
16390 {
16391 div = build_int_cst (TREE_TYPE (value), divisor);
16392
16393 if (multiple_of_p (TREE_TYPE (value), value, div))
16394 return value;
16395 }
16396
16397 /* If divisor is a power of two, simplify this to bit manipulation. */
16398 if (pow2_or_zerop (divisor))
16399 {
16400 if (TREE_CODE (value) == INTEGER_CST)
16401 {
16402 wide_int val = wi::to_wide (value);
16403 bool overflow_p;
16404
16405 if ((val & (divisor - 1)) == 0)
16406 return value;
16407
16408 overflow_p = TREE_OVERFLOW (value);
16409 val += divisor - 1;
16410 val &= (int) -divisor;
16411 if (val == 0)
16412 overflow_p = true;
16413
16414 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16415 }
16416 else
16417 {
16418 tree t;
16419
16420 t = build_int_cst (TREE_TYPE (value), divisor - 1);
16421 value = size_binop_loc (loc, PLUS_EXPR, value, t);
16422 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16423 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16424 }
16425 }
16426 else
16427 {
16428 if (!div)
16429 div = build_int_cst (TREE_TYPE (value), divisor);
16430 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16431 value = size_binop_loc (loc, MULT_EXPR, value, div);
16432 }
16433
16434 return value;
16435 }
16436
16437 /* Likewise, but round down. */
16438
16439 tree
16440 round_down_loc (location_t loc, tree value, int divisor)
16441 {
16442 tree div = NULL_TREE;
16443
16444 gcc_assert (divisor > 0);
16445 if (divisor == 1)
16446 return value;
16447
16448 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16449 have to do anything. Only do this when we are not given a const,
16450 because in that case, this check is more expensive than just
16451 doing it. */
16452 if (TREE_CODE (value) != INTEGER_CST)
16453 {
16454 div = build_int_cst (TREE_TYPE (value), divisor);
16455
16456 if (multiple_of_p (TREE_TYPE (value), value, div))
16457 return value;
16458 }
16459
16460 /* If divisor is a power of two, simplify this to bit manipulation. */
16461 if (pow2_or_zerop (divisor))
16462 {
16463 tree t;
16464
16465 t = build_int_cst (TREE_TYPE (value), -divisor);
16466 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16467 }
16468 else
16469 {
16470 if (!div)
16471 div = build_int_cst (TREE_TYPE (value), divisor);
16472 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16473 value = size_binop_loc (loc, MULT_EXPR, value, div);
16474 }
16475
16476 return value;
16477 }
16478
16479 /* Returns the pointer to the base of the object addressed by EXP and
16480 extracts the information about the offset of the access, storing it
16481 to PBITPOS and POFFSET. */
16482
16483 static tree
16484 split_address_to_core_and_offset (tree exp,
16485 poly_int64 *pbitpos, tree *poffset)
16486 {
16487 tree core;
16488 machine_mode mode;
16489 int unsignedp, reversep, volatilep;
16490 poly_int64 bitsize;
16491 location_t loc = EXPR_LOCATION (exp);
16492
16493 if (TREE_CODE (exp) == SSA_NAME)
16494 if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16495 if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16496 exp = gimple_assign_rhs1 (def);
16497
16498 if (TREE_CODE (exp) == ADDR_EXPR)
16499 {
16500 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16501 poffset, &mode, &unsignedp, &reversep,
16502 &volatilep);
16503 core = build_fold_addr_expr_loc (loc, core);
16504 }
16505 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16506 {
16507 core = TREE_OPERAND (exp, 0);
16508 STRIP_NOPS (core);
16509 *pbitpos = 0;
16510 *poffset = TREE_OPERAND (exp, 1);
16511 if (poly_int_tree_p (*poffset))
16512 {
16513 poly_offset_int tem
16514 = wi::sext (wi::to_poly_offset (*poffset),
16515 TYPE_PRECISION (TREE_TYPE (*poffset)));
16516 tem <<= LOG2_BITS_PER_UNIT;
16517 if (tem.to_shwi (pbitpos))
16518 *poffset = NULL_TREE;
16519 }
16520 }
16521 else
16522 {
16523 core = exp;
16524 *pbitpos = 0;
16525 *poffset = NULL_TREE;
16526 }
16527
16528 return core;
16529 }
16530
16531 /* Returns true if addresses of E1 and E2 differ by a constant, false
16532 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16533
16534 bool
16535 ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16536 {
16537 tree core1, core2;
16538 poly_int64 bitpos1, bitpos2;
16539 tree toffset1, toffset2, tdiff, type;
16540
16541 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16542 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16543
16544 poly_int64 bytepos1, bytepos2;
16545 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16546 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16547 || !operand_equal_p (core1, core2, 0))
16548 return false;
16549
16550 if (toffset1 && toffset2)
16551 {
16552 type = TREE_TYPE (toffset1);
16553 if (type != TREE_TYPE (toffset2))
16554 toffset2 = fold_convert (type, toffset2);
16555
16556 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16557 if (!cst_and_fits_in_hwi (tdiff))
16558 return false;
16559
16560 *diff = int_cst_value (tdiff);
16561 }
16562 else if (toffset1 || toffset2)
16563 {
16564 /* If only one of the offsets is non-constant, the difference cannot
16565 be a constant. */
16566 return false;
16567 }
16568 else
16569 *diff = 0;
16570
16571 *diff += bytepos1 - bytepos2;
16572 return true;
16573 }
16574
16575 /* Return OFF converted to a pointer offset type suitable as offset for
16576 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16577 tree
16578 convert_to_ptrofftype_loc (location_t loc, tree off)
16579 {
16580 if (ptrofftype_p (TREE_TYPE (off)))
16581 return off;
16582 return fold_convert_loc (loc, sizetype, off);
16583 }
16584
16585 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16586 tree
16587 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16588 {
16589 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16590 ptr, convert_to_ptrofftype_loc (loc, off));
16591 }
16592
16593 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16594 tree
16595 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16596 {
16597 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16598 ptr, size_int (off));
16599 }
16600
16601 /* Return a pointer to a NUL-terminated string containing the sequence
16602 of bytes corresponding to the representation of the object referred to
16603 by SRC (or a subsequence of such bytes within it if SRC is a reference
16604 to an initialized constant array plus some constant offset).
16605 Set *STRSIZE the number of bytes in the constant sequence including
16606 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16607 where A is the array that stores the constant sequence that SRC points
16608 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16609 need not point to a string or even an array of characters but may point
16610 to an object of any type. */
16611
16612 const char *
16613 getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16614 {
16615 /* The offset into the array A storing the string, and A's byte size. */
16616 tree offset_node;
16617 tree mem_size;
16618
16619 if (strsize)
16620 *strsize = 0;
16621
16622 if (strsize)
16623 src = byte_representation (src, &offset_node, &mem_size, NULL);
16624 else
16625 src = string_constant (src, &offset_node, &mem_size, NULL);
16626 if (!src)
16627 return NULL;
16628
16629 unsigned HOST_WIDE_INT offset = 0;
16630 if (offset_node != NULL_TREE)
16631 {
16632 if (!tree_fits_uhwi_p (offset_node))
16633 return NULL;
16634 else
16635 offset = tree_to_uhwi (offset_node);
16636 }
16637
16638 if (!tree_fits_uhwi_p (mem_size))
16639 return NULL;
16640
16641 /* ARRAY_SIZE is the byte size of the array the constant sequence
16642 is stored in and equal to sizeof A. INIT_BYTES is the number
16643 of bytes in the constant sequence used to initialize the array,
16644 including any embedded NULs as well as the terminating NUL (for
16645 strings), but not including any trailing zeros/NULs past
16646 the terminating one appended implicitly to a string literal to
16647 zero out the remainder of the array it's stored in. For example,
16648 given:
16649 const char a[7] = "abc\0d";
16650 n = strlen (a + 1);
16651 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16652 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16653 is equal to strlen (A) + 1. */
16654 const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16655 unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16656 const char *string = TREE_STRING_POINTER (src);
16657
16658 /* Ideally this would turn into a gcc_checking_assert over time. */
16659 if (init_bytes > array_size)
16660 init_bytes = array_size;
16661
16662 if (init_bytes == 0 || offset >= array_size)
16663 return NULL;
16664
16665 if (strsize)
16666 {
16667 /* Compute and store the number of characters from the beginning
16668 of the substring at OFFSET to the end, including the terminating
16669 nul. Offsets past the initial length refer to null strings. */
16670 if (offset < init_bytes)
16671 *strsize = init_bytes - offset;
16672 else
16673 *strsize = 1;
16674 }
16675 else
16676 {
16677 tree eltype = TREE_TYPE (TREE_TYPE (src));
16678 /* Support only properly NUL-terminated single byte strings. */
16679 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16680 return NULL;
16681 if (string[init_bytes - 1] != '\0')
16682 return NULL;
16683 }
16684
16685 return offset < init_bytes ? string + offset : "";
16686 }
16687
16688 /* Return a pointer to a NUL-terminated string corresponding to
16689 the expression STR referencing a constant string, possibly
16690 involving a constant offset. Return null if STR either doesn't
16691 reference a constant string or if it involves a nonconstant
16692 offset. */
16693
16694 const char *
16695 c_getstr (tree str)
16696 {
16697 return getbyterep (str, NULL);
16698 }
16699
16700 /* Given a tree T, compute which bits in T may be nonzero. */
16701
16702 wide_int
16703 tree_nonzero_bits (const_tree t)
16704 {
16705 switch (TREE_CODE (t))
16706 {
16707 case INTEGER_CST:
16708 return wi::to_wide (t);
16709 case SSA_NAME:
16710 return get_nonzero_bits (t);
16711 case NON_LVALUE_EXPR:
16712 case SAVE_EXPR:
16713 return tree_nonzero_bits (TREE_OPERAND (t, 0));
16714 case BIT_AND_EXPR:
16715 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16716 tree_nonzero_bits (TREE_OPERAND (t, 1)));
16717 case BIT_IOR_EXPR:
16718 case BIT_XOR_EXPR:
16719 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16720 tree_nonzero_bits (TREE_OPERAND (t, 1)));
16721 case COND_EXPR:
16722 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16723 tree_nonzero_bits (TREE_OPERAND (t, 2)));
16724 CASE_CONVERT:
16725 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16726 TYPE_PRECISION (TREE_TYPE (t)),
16727 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16728 case PLUS_EXPR:
16729 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16730 {
16731 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16732 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16733 if (wi::bit_and (nzbits1, nzbits2) == 0)
16734 return wi::bit_or (nzbits1, nzbits2);
16735 }
16736 break;
16737 case LSHIFT_EXPR:
16738 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16739 {
16740 tree type = TREE_TYPE (t);
16741 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16742 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16743 TYPE_PRECISION (type));
16744 return wi::neg_p (arg1)
16745 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16746 : wi::lshift (nzbits, arg1);
16747 }
16748 break;
16749 case RSHIFT_EXPR:
16750 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16751 {
16752 tree type = TREE_TYPE (t);
16753 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16754 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16755 TYPE_PRECISION (type));
16756 return wi::neg_p (arg1)
16757 ? wi::lshift (nzbits, -arg1)
16758 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16759 }
16760 break;
16761 default:
16762 break;
16763 }
16764
16765 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16766 }
16767
16768 /* Helper function for address compare simplifications in match.pd.
16769 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16770 TYPE is the type of comparison operands.
16771 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16772 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16773 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16774 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16775 and 2 if unknown. */
16776
16777 int
16778 address_compare (tree_code code, tree type, tree op0, tree op1,
16779 tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16780 bool generic)
16781 {
16782 if (TREE_CODE (op0) == SSA_NAME)
16783 op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16784 if (TREE_CODE (op1) == SSA_NAME)
16785 op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16786 gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16787 gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16788 base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16789 base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16790 if (base0 && TREE_CODE (base0) == MEM_REF)
16791 {
16792 off0 += mem_ref_offset (base0).force_shwi ();
16793 base0 = TREE_OPERAND (base0, 0);
16794 }
16795 if (base1 && TREE_CODE (base1) == MEM_REF)
16796 {
16797 off1 += mem_ref_offset (base1).force_shwi ();
16798 base1 = TREE_OPERAND (base1, 0);
16799 }
16800 if (base0 == NULL_TREE || base1 == NULL_TREE)
16801 return 2;
16802
16803 int equal = 2;
16804 /* Punt in GENERIC on variables with value expressions;
16805 the value expressions might point to fields/elements
16806 of other vars etc. */
16807 if (generic
16808 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16809 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16810 return 2;
16811 else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16812 {
16813 symtab_node *node0 = symtab_node::get_create (base0);
16814 symtab_node *node1 = symtab_node::get_create (base1);
16815 equal = node0->equal_address_to (node1);
16816 }
16817 else if ((DECL_P (base0)
16818 || TREE_CODE (base0) == SSA_NAME
16819 || TREE_CODE (base0) == STRING_CST)
16820 && (DECL_P (base1)
16821 || TREE_CODE (base1) == SSA_NAME
16822 || TREE_CODE (base1) == STRING_CST))
16823 equal = (base0 == base1);
16824 /* Assume different STRING_CSTs with the same content will be
16825 merged. */
16826 if (equal == 0
16827 && TREE_CODE (base0) == STRING_CST
16828 && TREE_CODE (base1) == STRING_CST
16829 && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16830 && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16831 TREE_STRING_LENGTH (base0)) == 0)
16832 equal = 1;
16833 if (equal == 1)
16834 {
16835 if (code == EQ_EXPR
16836 || code == NE_EXPR
16837 /* If the offsets are equal we can ignore overflow. */
16838 || known_eq (off0, off1)
16839 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16840 /* Or if we compare using pointers to decls or strings. */
16841 || (POINTER_TYPE_P (type)
16842 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16843 return 1;
16844 return 2;
16845 }
16846 if (equal != 0)
16847 return equal;
16848 if (code != EQ_EXPR && code != NE_EXPR)
16849 return 2;
16850
16851 /* At this point we know (or assume) the two pointers point at
16852 different objects. */
16853 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16854 off0.is_constant (&ioff0);
16855 off1.is_constant (&ioff1);
16856 /* Punt on non-zero offsets from functions. */
16857 if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16858 || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16859 return 2;
16860 /* Or if the bases are neither decls nor string literals. */
16861 if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16862 return 2;
16863 if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16864 return 2;
16865 /* For initializers, assume addresses of different functions are
16866 different. */
16867 if (folding_initializer
16868 && TREE_CODE (base0) == FUNCTION_DECL
16869 && TREE_CODE (base1) == FUNCTION_DECL)
16870 return 0;
16871
16872 /* Compute whether one address points to the start of one
16873 object and another one to the end of another one. */
16874 poly_int64 size0 = 0, size1 = 0;
16875 if (TREE_CODE (base0) == STRING_CST)
16876 {
16877 if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16878 equal = 2;
16879 else
16880 size0 = TREE_STRING_LENGTH (base0);
16881 }
16882 else if (TREE_CODE (base0) == FUNCTION_DECL)
16883 size0 = 1;
16884 else
16885 {
16886 tree sz0 = DECL_SIZE_UNIT (base0);
16887 if (!tree_fits_poly_int64_p (sz0))
16888 equal = 2;
16889 else
16890 size0 = tree_to_poly_int64 (sz0);
16891 }
16892 if (TREE_CODE (base1) == STRING_CST)
16893 {
16894 if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16895 equal = 2;
16896 else
16897 size1 = TREE_STRING_LENGTH (base1);
16898 }
16899 else if (TREE_CODE (base1) == FUNCTION_DECL)
16900 size1 = 1;
16901 else
16902 {
16903 tree sz1 = DECL_SIZE_UNIT (base1);
16904 if (!tree_fits_poly_int64_p (sz1))
16905 equal = 2;
16906 else
16907 size1 = tree_to_poly_int64 (sz1);
16908 }
16909 if (equal == 0)
16910 {
16911 /* If one offset is pointing (or could be) to the beginning of one
16912 object and the other is pointing to one past the last byte of the
16913 other object, punt. */
16914 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16915 equal = 2;
16916 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16917 equal = 2;
16918 /* If both offsets are the same, there are some cases we know that are
16919 ok. Either if we know they aren't zero, or if we know both sizes
16920 are no zero. */
16921 if (equal == 2
16922 && known_eq (off0, off1)
16923 && (known_ne (off0, 0)
16924 || (known_ne (size0, 0) && known_ne (size1, 0))))
16925 equal = 0;
16926 }
16927
16928 /* At this point, equal is 2 if either one or both pointers are out of
16929 bounds of their object, or one points to start of its object and the
16930 other points to end of its object. This is unspecified behavior
16931 e.g. in C++. Otherwise equal is 0. */
16932 if (folding_cxx_constexpr && equal)
16933 return equal;
16934
16935 /* When both pointers point to string literals, even when equal is 0,
16936 due to tail merging of string literals the pointers might be the same. */
16937 if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16938 {
16939 if (ioff0 < 0
16940 || ioff1 < 0
16941 || ioff0 > TREE_STRING_LENGTH (base0)
16942 || ioff1 > TREE_STRING_LENGTH (base1))
16943 return 2;
16944
16945 /* If the bytes in the string literals starting at the pointers
16946 differ, the pointers need to be different. */
16947 if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16948 TREE_STRING_POINTER (base1) + ioff1,
16949 MIN (TREE_STRING_LENGTH (base0) - ioff0,
16950 TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16951 {
16952 HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16953 if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16954 TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16955 ioffmin) == 0)
16956 /* If even the bytes in the string literal before the
16957 pointers are the same, the string literals could be
16958 tail merged. */
16959 return 2;
16960 }
16961 return 0;
16962 }
16963
16964 if (folding_cxx_constexpr)
16965 return 0;
16966
16967 /* If this is a pointer comparison, ignore for now even
16968 valid equalities where one pointer is the offset zero
16969 of one object and the other to one past end of another one. */
16970 if (!INTEGRAL_TYPE_P (type))
16971 return 0;
16972
16973 /* Assume that string literals can't be adjacent to variables
16974 (automatic or global). */
16975 if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16976 return 0;
16977
16978 /* Assume that automatic variables can't be adjacent to global
16979 variables. */
16980 if (is_global_var (base0) != is_global_var (base1))
16981 return 0;
16982
16983 return equal;
16984 }
16985
16986 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16987 tree
16988 ctor_single_nonzero_element (const_tree t)
16989 {
16990 unsigned HOST_WIDE_INT idx;
16991 constructor_elt *ce;
16992 tree elt = NULL_TREE;
16993
16994 if (TREE_CODE (t) != CONSTRUCTOR)
16995 return NULL_TREE;
16996 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16997 if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16998 {
16999 if (elt)
17000 return NULL_TREE;
17001 elt = ce->value;
17002 }
17003 return elt;
17004 }
17005
17006 #if CHECKING_P
17007
17008 namespace selftest {
17009
17010 /* Helper functions for writing tests of folding trees. */
17011
17012 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17013
17014 static void
17015 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
17016 tree constant)
17017 {
17018 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
17019 }
17020
17021 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17022 wrapping WRAPPED_EXPR. */
17023
17024 static void
17025 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
17026 tree wrapped_expr)
17027 {
17028 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
17029 ASSERT_NE (wrapped_expr, result);
17030 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
17031 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
17032 }
17033
17034 /* Verify that various arithmetic binary operations are folded
17035 correctly. */
17036
17037 static void
17038 test_arithmetic_folding ()
17039 {
17040 tree type = integer_type_node;
17041 tree x = create_tmp_var_raw (type, "x");
17042 tree zero = build_zero_cst (type);
17043 tree one = build_int_cst (type, 1);
17044
17045 /* Addition. */
17046 /* 1 <-- (0 + 1) */
17047 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
17048 one);
17049 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
17050 one);
17051
17052 /* (nonlvalue)x <-- (x + 0) */
17053 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
17054 x);
17055
17056 /* Subtraction. */
17057 /* 0 <-- (x - x) */
17058 assert_binop_folds_to_const (x, MINUS_EXPR, x,
17059 zero);
17060 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
17061 x);
17062
17063 /* Multiplication. */
17064 /* 0 <-- (x * 0) */
17065 assert_binop_folds_to_const (x, MULT_EXPR, zero,
17066 zero);
17067
17068 /* (nonlvalue)x <-- (x * 1) */
17069 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
17070 x);
17071 }
17072
17073 namespace test_operand_equality {
17074
17075 /* Verify structural equality. */
17076
17077 /* Execute fold_vec_perm_cst unit tests. */
17078
17079 static void
17080 test ()
17081 {
17082 tree stype = integer_type_node;
17083 tree utype = unsigned_type_node;
17084 tree x = create_tmp_var_raw (stype, "x");
17085 tree y = create_tmp_var_raw (stype, "y");
17086 tree z = create_tmp_var_raw (stype, "z");
17087 tree four = build_int_cst (stype, 4);
17088 tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
17089 tree rhs1 = fold_convert (stype,
17090 fold_build2 (PLUS_EXPR, utype,
17091 fold_convert (utype, x),
17092 fold_convert (utype, y)));
17093
17094 /* (int)((unsigned x) + (unsigned y)) == x + y. */
17095 ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17096 ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17097
17098 /* (int)(unsigned) x == x. */
17099 tree lhs2 = build1 (NOP_EXPR, stype,
17100 build1 (NOP_EXPR, utype, x));
17101 tree rhs2 = x;
17102 ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17103 ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17104
17105 /* (unsigned x) + (unsigned y) == x + y. */
17106 tree lhs3 = lhs1;
17107 tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17108 fold_convert (utype, x),
17109 fold_convert (utype, y));
17110 ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17111 ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17112
17113 /* (unsigned x) / (unsigned y) == x / y. */
17114 tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17115 tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17116 fold_convert (utype, x),
17117 fold_convert (utype, y));
17118 ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17119 ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17120
17121 /* (long x) / 4 == (long)(x / 4). */
17122 tree lstype = long_long_integer_type_node;
17123 tree lfour = build_int_cst (lstype, 4);
17124 tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17125 fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17126 tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17127 fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17128 ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17129 ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17130
17131 /* (unsigned x) / 4 == x / 4. */
17132 tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17133 tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17134 fold_convert (utype, x),
17135 fold_convert (utype, four));
17136 ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17137 ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17138
17139 /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17140 tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17141 tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17142 ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17143 ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17144
17145 /* (unsigned x) + 4 == x + 4. */
17146 tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17147 tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17148 fold_convert (utype, x),
17149 fold_convert (utype, four));
17150 ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17151 ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17152
17153 /* (unsigned x) + 4 == 4 + x. */
17154 tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17155 tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17156 fold_convert (utype, x),
17157 fold_convert (utype, four));
17158 ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17159 ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17160
17161 /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17162 tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17163 fold_build2 (MULT_EXPR, stype,
17164 fold_build2 (PLUS_EXPR, stype, four, x),
17165 y),
17166 z);
17167 tree rhs10 = fold_build2 (MULT_EXPR, utype,
17168 fold_build2 (PLUS_EXPR, utype,
17169 fold_convert (utype, x),
17170 fold_convert (utype, four)),
17171 fold_convert (utype, y));
17172 rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17173 ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17174 ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17175 }
17176 }
17177
17178 namespace test_fold_vec_perm_cst {
17179
17180 /* Build a VECTOR_CST corresponding to VMODE, and has
17181 encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17182 Fill it with randomized elements, using rand() % THRESHOLD. */
17183
17184 static tree
17185 build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17186 unsigned nelts_per_pattern,
17187 int step = 0, bool natural_stepped = false,
17188 int threshold = 100)
17189 {
17190 tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17191 tree vectype = build_vector_type_for_mode (inner_type, vmode);
17192 tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17193
17194 // Fill a0 for each pattern
17195 for (unsigned i = 0; i < npatterns; i++)
17196 builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17197
17198 if (nelts_per_pattern == 1)
17199 return builder.build ();
17200
17201 // Fill a1 for each pattern
17202 for (unsigned i = 0; i < npatterns; i++)
17203 {
17204 tree a1;
17205 if (natural_stepped)
17206 {
17207 tree a0 = builder[i];
17208 wide_int a0_val = wi::to_wide (a0);
17209 wide_int a1_val = a0_val + step;
17210 a1 = wide_int_to_tree (inner_type, a1_val);
17211 }
17212 else
17213 a1 = build_int_cst (inner_type, rand () % threshold);
17214 builder.quick_push (a1);
17215 }
17216 if (nelts_per_pattern == 2)
17217 return builder.build ();
17218
17219 for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17220 {
17221 tree prev_elem = builder[i - npatterns];
17222 wide_int prev_elem_val = wi::to_wide (prev_elem);
17223 wide_int val = prev_elem_val + step;
17224 builder.quick_push (wide_int_to_tree (inner_type, val));
17225 }
17226
17227 return builder.build ();
17228 }
17229
17230 /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17231 when result is VLA. */
17232
17233 static void
17234 validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17235 tree res, tree *expected_res)
17236 {
17237 /* Actual npatterns and encoded_elts in res may be less than expected due
17238 to canonicalization. */
17239 ASSERT_TRUE (res != NULL_TREE);
17240 ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17241 ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17242
17243 for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17244 ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17245 }
17246
17247 /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17248 when the result is VLS. */
17249
17250 static void
17251 validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17252 {
17253 ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17254 for (unsigned i = 0; i < expected_nelts; i++)
17255 ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17256 }
17257
17258 /* Helper routine to push multiple elements into BUILDER. */
17259 template<unsigned N>
17260 static void builder_push_elems (vec_perm_builder& builder,
17261 poly_uint64 (&elems)[N])
17262 {
17263 for (unsigned i = 0; i < N; i++)
17264 builder.quick_push (elems[i]);
17265 }
17266
17267 #define ARG0(index) vector_cst_elt (arg0, index)
17268 #define ARG1(index) vector_cst_elt (arg1, index)
17269
17270 /* Test cases where result is VNx4SI and input vectors are V4SI. */
17271
17272 static void
17273 test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17274 {
17275 for (int i = 0; i < 10; i++)
17276 {
17277 /* Case 1:
17278 sel = { 0, 4, 1, 5, ... }
17279 res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17280 {
17281 tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17282 tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17283
17284 tree inner_type
17285 = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17286 tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17287
17288 poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17289 vec_perm_builder builder (res_len, 4, 1);
17290 poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17291 builder_push_elems (builder, mask_elems);
17292
17293 vec_perm_indices sel (builder, 2, res_len);
17294 tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17295
17296 tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17297 validate_res (4, 1, res, expected_res);
17298 }
17299
17300 /* Case 2: Same as case 1, but contains an out of bounds access which
17301 should wrap around.
17302 sel = {0, 8, 4, 12, ...} (4, 1)
17303 res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17304 {
17305 tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17306 tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17307
17308 tree inner_type
17309 = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17310 tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17311
17312 poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17313 vec_perm_builder builder (res_len, 4, 1);
17314 poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17315 builder_push_elems (builder, mask_elems);
17316
17317 vec_perm_indices sel (builder, 2, res_len);
17318 tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17319
17320 tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17321 validate_res (4, 1, res, expected_res);
17322 }
17323 }
17324 }
17325
17326 /* Test cases where result is V4SI and input vectors are VNx4SI. */
17327
17328 static void
17329 test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17330 {
17331 for (int i = 0; i < 10; i++)
17332 {
17333 /* Case 1:
17334 sel = { 0, 1, 2, 3}
17335 res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17336 {
17337 tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17338 tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17339
17340 tree inner_type
17341 = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17342 tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17343
17344 poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17345 vec_perm_builder builder (res_len, 4, 1);
17346 poly_uint64 mask_elems[] = {0, 1, 2, 3};
17347 builder_push_elems (builder, mask_elems);
17348
17349 vec_perm_indices sel (builder, 2, res_len);
17350 tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17351
17352 tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17353 validate_res_vls (res, expected_res, 4);
17354 }
17355
17356 /* Case 2: Same as Case 1, but crossing input vector.
17357 sel = {0, 2, 4, 6}
17358 In this case,the index 4 is ambiguous since len = 4 + 4x.
17359 Since we cannot determine, which vector to choose from during
17360 compile time, should return NULL_TREE. */
17361 {
17362 tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17363 tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17364
17365 tree inner_type
17366 = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17367 tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17368
17369 poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17370 vec_perm_builder builder (res_len, 4, 1);
17371 poly_uint64 mask_elems[] = {0, 2, 4, 6};
17372 builder_push_elems (builder, mask_elems);
17373
17374 vec_perm_indices sel (builder, 2, res_len);
17375 const char *reason;
17376 tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17377
17378 ASSERT_TRUE (res == NULL_TREE);
17379 ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17380 }
17381 }
17382 }
17383
17384 /* Test all input vectors. */
17385
17386 static void
17387 test_all_nunits (machine_mode vmode)
17388 {
17389 /* Test with 10 different inputs. */
17390 for (int i = 0; i < 10; i++)
17391 {
17392 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17393 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17394 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17395
17396 /* Case 1: mask = {0, ...} // (1, 1)
17397 res = { arg0[0], ... } // (1, 1) */
17398 {
17399 vec_perm_builder builder (len, 1, 1);
17400 builder.quick_push (0);
17401 vec_perm_indices sel (builder, 2, len);
17402 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17403 tree expected_res[] = { ARG0(0) };
17404 validate_res (1, 1, res, expected_res);
17405 }
17406
17407 /* Case 2: mask = {len, ...} // (1, 1)
17408 res = { arg1[0], ... } // (1, 1) */
17409 {
17410 vec_perm_builder builder (len, 1, 1);
17411 builder.quick_push (len);
17412 vec_perm_indices sel (builder, 2, len);
17413 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17414
17415 tree expected_res[] = { ARG1(0) };
17416 validate_res (1, 1, res, expected_res);
17417 }
17418 }
17419 }
17420
17421 /* Test all vectors which contain at-least 2 elements. */
17422
17423 static void
17424 test_nunits_min_2 (machine_mode vmode)
17425 {
17426 for (int i = 0; i < 10; i++)
17427 {
17428 /* Case 1: mask = { 0, len, ... } // (2, 1)
17429 res = { arg0[0], arg1[0], ... } // (2, 1) */
17430 {
17431 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17432 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17433 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17434
17435 vec_perm_builder builder (len, 2, 1);
17436 poly_uint64 mask_elems[] = { 0, len };
17437 builder_push_elems (builder, mask_elems);
17438
17439 vec_perm_indices sel (builder, 2, len);
17440 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17441
17442 tree expected_res[] = { ARG0(0), ARG1(0) };
17443 validate_res (2, 1, res, expected_res);
17444 }
17445
17446 /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17447 res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17448 {
17449 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17450 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17451 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17452
17453 vec_perm_builder builder (len, 2, 2);
17454 poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17455 builder_push_elems (builder, mask_elems);
17456
17457 vec_perm_indices sel (builder, 2, len);
17458 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17459
17460 tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17461 validate_res (2, 2, res, expected_res);
17462 }
17463
17464 /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17465 Test that the stepped sequence of the pattern selects from
17466 same input pattern. Since input vectors have npatterns = 2,
17467 and step (a2 - a1) = 1, step is not a multiple of npatterns
17468 in input vector. So return NULL_TREE. */
17469 {
17470 tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17471 tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17472 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17473
17474 vec_perm_builder builder (len, 1, 3);
17475 poly_uint64 mask_elems[] = { 0, 0, 1 };
17476 builder_push_elems (builder, mask_elems);
17477
17478 vec_perm_indices sel (builder, 2, len);
17479 const char *reason;
17480 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17481 &reason);
17482 ASSERT_TRUE (res == NULL_TREE);
17483 ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17484 }
17485
17486 /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17487 Test that stepped sequence of the pattern selects from arg0.
17488 res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17489 {
17490 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17491 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17492 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17493
17494 vec_perm_builder builder (len, 1, 3);
17495 poly_uint64 mask_elems[] = { len, 0, 1 };
17496 builder_push_elems (builder, mask_elems);
17497
17498 vec_perm_indices sel (builder, 2, len);
17499 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17500
17501 tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17502 validate_res (1, 3, res, expected_res);
17503 }
17504
17505 /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17506 In this case ensure that arg has a natural stepped sequence
17507 to preserve arg's encoding.
17508
17509 As a concrete example, consider:
17510 arg0: { -16, -9, -10, ... } // (1, 3)
17511 arg1: { -12, -5, -6, ... } // (1, 3)
17512 sel = { 0, len, len + 1, ... } // (1, 3)
17513
17514 This will create res with following encoding:
17515 res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17516 = { -16, -12, -5, ... }
17517
17518 The step in above encoding would be: (-5) - (-12) = 7
17519 And hence res[3] would be computed as -5 + 7 = 2.
17520 instead of arg1[2], ie, -6.
17521 Ensure that valid_mask_for_fold_vec_perm_cst returns false
17522 for this case. */
17523 {
17524 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17525 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17526 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17527
17528 vec_perm_builder builder (len, 1, 3);
17529 poly_uint64 mask_elems[] = { 0, len, len+1 };
17530 builder_push_elems (builder, mask_elems);
17531
17532 vec_perm_indices sel (builder, 2, len);
17533 const char *reason;
17534 /* FIXME: It may happen that build_vec_cst_rand may build a natural
17535 stepped pattern, even if we didn't explicitly tell it to. So folding
17536 may not always fail, but if it does, ensure that's because arg1 does
17537 not have a natural stepped sequence (and not due to other reason) */
17538 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17539 if (res == NULL_TREE)
17540 ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17541 }
17542
17543 /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17544 sequence and thus folding should be valid for this case. */
17545 {
17546 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17547 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17548 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17549
17550 vec_perm_builder builder (len, 1, 3);
17551 poly_uint64 mask_elems[] = { 0, len, len+1 };
17552 builder_push_elems (builder, mask_elems);
17553
17554 vec_perm_indices sel (builder, 2, len);
17555 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17556
17557 tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17558 validate_res (1, 3, res, expected_res);
17559 }
17560
17561 /* Case 8: Same as aarch64/sve/slp_3.c:
17562 arg0, arg1 are dup vectors.
17563 sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17564 So res = { arg0[0], arg1[0], ... } // (2, 1)
17565
17566 In this case, since the input vectors are dup, only the first two
17567 elements per pattern in sel are considered significant. */
17568 {
17569 tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17570 tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17571 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17572
17573 vec_perm_builder builder (len, 2, 3);
17574 poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17575 builder_push_elems (builder, mask_elems);
17576
17577 vec_perm_indices sel (builder, 2, len);
17578 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17579
17580 tree expected_res[] = { ARG0(0), ARG1(0) };
17581 validate_res (2, 1, res, expected_res);
17582 }
17583 }
17584 }
17585
17586 /* Test all vectors which contain at-least 4 elements. */
17587
17588 static void
17589 test_nunits_min_4 (machine_mode vmode)
17590 {
17591 for (int i = 0; i < 10; i++)
17592 {
17593 /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17594 res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17595 {
17596 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17597 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17598 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17599
17600 vec_perm_builder builder (len, 4, 1);
17601 poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17602 builder_push_elems (builder, mask_elems);
17603
17604 vec_perm_indices sel (builder, 2, len);
17605 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17606
17607 tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17608 validate_res (4, 1, res, expected_res);
17609 }
17610
17611 /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17612 res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17613 {
17614 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17615 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17616 poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17617
17618 vec_perm_builder builder (arg0_len, 1, 3);
17619 poly_uint64 mask_elems[] = {0, 1, 2};
17620 builder_push_elems (builder, mask_elems);
17621
17622 vec_perm_indices sel (builder, 2, arg0_len);
17623 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17624 tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17625 validate_res (1, 3, res, expected_res);
17626 }
17627
17628 /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17629 res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17630 {
17631 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17632 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17633 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17634
17635 vec_perm_builder builder (len, 1, 3);
17636 poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17637 builder_push_elems (builder, mask_elems);
17638
17639 vec_perm_indices sel (builder, 2, len);
17640 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17641 tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17642 validate_res (1, 3, res, expected_res);
17643 }
17644
17645 /* Case 4:
17646 sel = { len, 0, 2, ... } // (1, 3)
17647 This should return NULL because we cross the input vectors.
17648 Because,
17649 Let's assume len = C + Cx
17650 a1 = 0
17651 S = 2
17652 esel = arg0_len / sel_npatterns = C + Cx
17653 ae = 0 + (esel - 2) * S
17654 = 0 + (C + Cx - 2) * 2
17655 = 2(C-2) + 2Cx
17656
17657 For C >= 4:
17658 Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17659 Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17660 Since q1 != qe, we cross input vectors.
17661 So return NULL_TREE. */
17662 {
17663 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17664 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17665 poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17666
17667 vec_perm_builder builder (arg0_len, 1, 3);
17668 poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17669 builder_push_elems (builder, mask_elems);
17670
17671 vec_perm_indices sel (builder, 2, arg0_len);
17672 const char *reason;
17673 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17674 ASSERT_TRUE (res == NULL_TREE);
17675 ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17676 }
17677
17678 /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17679 mask = { 0, len, 1, len + 1, ...} // (2, 2)
17680 res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17681
17682 Note that fold_vec_perm_cst will set
17683 res_npatterns = max(4, max(4, 2)) = 4
17684 However after canonicalizing, we will end up with shape (2, 2). */
17685 {
17686 tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17687 tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17688 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17689
17690 vec_perm_builder builder (len, 2, 2);
17691 poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17692 builder_push_elems (builder, mask_elems);
17693
17694 vec_perm_indices sel (builder, 2, len);
17695 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17696 tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17697 validate_res (2, 2, res, expected_res);
17698 }
17699
17700 /* Case 6: Test combination in sel, where one pattern is dup and other
17701 is stepped sequence.
17702 sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17703 res = { arg0[0], arg0[0], arg0[0],
17704 arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17705 {
17706 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17707 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17708 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17709
17710 vec_perm_builder builder (len, 2, 3);
17711 poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17712 builder_push_elems (builder, mask_elems);
17713
17714 vec_perm_indices sel (builder, 2, len);
17715 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17716
17717 tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17718 ARG0(1), ARG0(0), ARG0(2) };
17719 validate_res (2, 3, res, expected_res);
17720 }
17721
17722 /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17723 when arg0, arg1 and sel have different number of patterns.
17724 arg0 is of shape (1, 1)
17725 arg1 is of shape (4, 1)
17726 sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17727
17728 In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17729 However,
17730 step = (len+2) - (len+1) = 1
17731 arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17732 Since step is not a multiple of arg_npatterns,
17733 valid_mask_for_fold_vec_perm_cst should return false,
17734 and thus fold_vec_perm_cst should return NULL_TREE. */
17735 {
17736 tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17737 tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17738 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17739
17740 vec_perm_builder builder (len, 2, 3);
17741 poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17742 builder_push_elems (builder, mask_elems);
17743
17744 vec_perm_indices sel (builder, 2, len);
17745 const char *reason;
17746 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17747
17748 ASSERT_TRUE (res == NULL_TREE);
17749 ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17750 }
17751
17752 /* Case 8: PR111754: When input vector is not a stepped sequence,
17753 check that the result is not a stepped sequence either, even
17754 if sel has a stepped sequence. */
17755 {
17756 tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17757 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17758
17759 vec_perm_builder builder (len, 1, 3);
17760 poly_uint64 mask_elems[] = { 0, 1, 2 };
17761 builder_push_elems (builder, mask_elems);
17762
17763 vec_perm_indices sel (builder, 1, len);
17764 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17765
17766 tree expected_res[] = { ARG0(0), ARG0(1) };
17767 validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17768 }
17769
17770 /* Case 9: If sel doesn't contain a stepped sequence,
17771 check that the result has same encoding as sel, irrespective
17772 of shape of input vectors. */
17773 {
17774 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17775 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17776 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17777
17778 vec_perm_builder builder (len, 1, 2);
17779 poly_uint64 mask_elems[] = { 0, len };
17780 builder_push_elems (builder, mask_elems);
17781
17782 vec_perm_indices sel (builder, 2, len);
17783 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17784
17785 tree expected_res[] = { ARG0(0), ARG1(0) };
17786 validate_res (sel.encoding ().npatterns (),
17787 sel.encoding ().nelts_per_pattern (), res, expected_res);
17788 }
17789 }
17790 }
17791
17792 /* Test all vectors which contain at-least 8 elements. */
17793
17794 static void
17795 test_nunits_min_8 (machine_mode vmode)
17796 {
17797 for (int i = 0; i < 10; i++)
17798 {
17799 /* Case 1: sel_npatterns (4) > input npatterns (2)
17800 sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17801 res: { arg0[0], arg0[0], arg0[0], arg1[0],
17802 arg0[2], arg0[0], arg0[3], arg1[0],
17803 arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17804 {
17805 tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17806 tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17807 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17808
17809 vec_perm_builder builder(len, 4, 3);
17810 poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17811 4, 0, 5, len };
17812 builder_push_elems (builder, mask_elems);
17813
17814 vec_perm_indices sel (builder, 2, len);
17815 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17816
17817 tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17818 ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17819 ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17820 validate_res (4, 3, res, expected_res);
17821 }
17822 }
17823 }
17824
17825 /* Test vectors for which nunits[0] <= 4. */
17826
17827 static void
17828 test_nunits_max_4 (machine_mode vmode)
17829 {
17830 /* Case 1: mask = {0, 4, ...} // (1, 2)
17831 This should return NULL_TREE because the index 4 may choose
17832 from either arg0 or arg1 depending on vector length. */
17833 {
17834 tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17835 tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17836 poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17837
17838 vec_perm_builder builder (len, 1, 2);
17839 poly_uint64 mask_elems[] = {0, 4};
17840 builder_push_elems (builder, mask_elems);
17841
17842 vec_perm_indices sel (builder, 2, len);
17843 const char *reason;
17844 tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17845 ASSERT_TRUE (res == NULL_TREE);
17846 ASSERT_TRUE (reason != NULL);
17847 ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17848 }
17849 }
17850
17851 #undef ARG0
17852 #undef ARG1
17853
17854 /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17855
17856 static bool
17857 is_simple_vla_size (poly_uint64 size)
17858 {
17859 if (size.is_constant ()
17860 || !pow2p_hwi (size.coeffs[0]))
17861 return false;
17862 for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17863 if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17864 return false;
17865 return true;
17866 }
17867
17868 /* Execute fold_vec_perm_cst unit tests. */
17869
17870 static void
17871 test ()
17872 {
17873 machine_mode vnx4si_mode = E_VOIDmode;
17874 machine_mode v4si_mode = E_VOIDmode;
17875
17876 machine_mode vmode;
17877 FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17878 {
17879 /* Obtain modes corresponding to VNx4SI and V4SI,
17880 to call mixed mode tests below.
17881 FIXME: Is there a better way to do this ? */
17882 if (GET_MODE_INNER (vmode) == SImode)
17883 {
17884 poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17885 if (is_simple_vla_size (nunits)
17886 && nunits.coeffs[0] == 4)
17887 vnx4si_mode = vmode;
17888 else if (known_eq (nunits, poly_uint64 (4)))
17889 v4si_mode = vmode;
17890 }
17891
17892 if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17893 || !targetm.vector_mode_supported_p (vmode))
17894 continue;
17895
17896 poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17897 test_all_nunits (vmode);
17898 if (nunits.coeffs[0] >= 2)
17899 test_nunits_min_2 (vmode);
17900 if (nunits.coeffs[0] >= 4)
17901 test_nunits_min_4 (vmode);
17902 if (nunits.coeffs[0] >= 8)
17903 test_nunits_min_8 (vmode);
17904
17905 if (nunits.coeffs[0] <= 4)
17906 test_nunits_max_4 (vmode);
17907 }
17908
17909 if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17910 && targetm.vector_mode_supported_p (vnx4si_mode)
17911 && targetm.vector_mode_supported_p (v4si_mode))
17912 {
17913 test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17914 test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17915 }
17916 }
17917 } // end of test_fold_vec_perm_cst namespace
17918
17919 /* Verify that various binary operations on vectors are folded
17920 correctly. */
17921
17922 static void
17923 test_vector_folding ()
17924 {
17925 tree inner_type = integer_type_node;
17926 tree type = build_vector_type (inner_type, 4);
17927 tree zero = build_zero_cst (type);
17928 tree one = build_one_cst (type);
17929 tree index = build_index_vector (type, 0, 1);
17930
17931 /* Verify equality tests that return a scalar boolean result. */
17932 tree res_type = boolean_type_node;
17933 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17934 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17935 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17936 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17937 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17938 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17939 index, one)));
17940 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17941 index, index)));
17942 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17943 index, index)));
17944 }
17945
17946 /* Verify folding of VEC_DUPLICATE_EXPRs. */
17947
17948 static void
17949 test_vec_duplicate_folding ()
17950 {
17951 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17952 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17953 /* This will be 1 if VEC_MODE isn't a vector mode. */
17954 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17955
17956 tree type = build_vector_type (ssizetype, nunits);
17957 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17958 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17959 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17960 }
17961
17962 /* Run all of the selftests within this file. */
17963
17964 void
17965 fold_const_cc_tests ()
17966 {
17967 test_arithmetic_folding ();
17968 test_vector_folding ();
17969 test_vec_duplicate_folding ();
17970 test_fold_vec_perm_cst::test ();
17971 test_operand_equality::test ();
17972 }
17973
17974 } // namespace selftest
17975
17976 #endif /* CHECKING_P */