Line data Source code
1 : /* Fold a constant sub-tree into a single node for C-compiler
2 : Copyright (C) 1987-2026 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 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);
142 : static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
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 cst1) ? exp0 : cst2". */
156 : tree_code
157 136153 : minmax_from_comparison (tree_code cmp, tree exp0,
158 : const widest_int cst1,
159 : const widest_int cst2)
160 : {
161 136153 : if (cst1 == cst2)
162 : {
163 133 : if (cmp == LE_EXPR || cmp == LT_EXPR)
164 : return MIN_EXPR;
165 114 : if (cmp == GT_EXPR || cmp == GE_EXPR)
166 : return MAX_EXPR;
167 : }
168 136134 : if (cst1 == cst2 - 1)
169 : {
170 : /* X <= Y - 1 equals to X < Y. */
171 81532 : if (cmp == LE_EXPR)
172 : return MIN_EXPR;
173 : /* X > Y - 1 equals to X >= Y. */
174 81103 : if (cmp == GT_EXPR)
175 : return MAX_EXPR;
176 : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
177 69100 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : {
179 18002 : int_range_max r;
180 36004 : get_range_query (cfun)->range_of_expr (r, exp0);
181 18002 : if (r.undefined_p ())
182 0 : r.set_varying (TREE_TYPE (exp0));
183 :
184 18002 : widest_int min = widest_int::from (r.lower_bound (),
185 36004 : TYPE_SIGN (TREE_TYPE (exp0)));
186 18002 : if (min == cst1)
187 715 : return MAX_EXPR;
188 18002 : }
189 : }
190 122987 : if (cst1 == cst2 + 1)
191 : {
192 : /* X < Y + 1 equals to X <= Y. */
193 1137 : if (cmp == LT_EXPR)
194 : return MIN_EXPR;
195 : /* X >= Y + 1 equals to X > Y. */
196 1109 : if (cmp == GE_EXPR)
197 : return MAX_EXPR;
198 : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
199 979 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : {
201 614 : int_range_max r;
202 1228 : get_range_query (cfun)->range_of_expr (r, exp0);
203 614 : if (r.undefined_p ())
204 0 : r.set_varying (TREE_TYPE (exp0));
205 :
206 614 : widest_int max = widest_int::from (r.upper_bound (),
207 1228 : TYPE_SIGN (TREE_TYPE (exp0)));
208 614 : if (max == cst1)
209 140 : return MIN_EXPR;
210 614 : }
211 : }
212 : return ERROR_MARK;
213 : }
214 :
215 :
216 : /* This is a helper function to detect min/max for some operands of COND_EXPR.
217 : The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
218 : tree_code
219 168538 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
220 : {
221 168538 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
222 11 : return ERROR_MARK;
223 :
224 168527 : if (!operand_equal_p (exp0, exp2))
225 : return ERROR_MARK;
226 :
227 168527 : if (operand_equal_p (exp1, exp3))
228 : {
229 32643 : if (cmp == LT_EXPR || cmp == LE_EXPR)
230 : return MIN_EXPR;
231 30521 : if (cmp == GT_EXPR || cmp == GE_EXPR)
232 : return MAX_EXPR;
233 : }
234 135998 : if (TREE_CODE (exp3) == INTEGER_CST
235 135912 : && TREE_CODE (exp1) == INTEGER_CST)
236 135454 : return minmax_from_comparison (cmp, exp0, wi::to_widest (exp1), wi::to_widest (exp3));
237 : return ERROR_MARK;
238 : }
239 :
240 : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
241 : Otherwise, return LOC. */
242 :
243 : static location_t
244 2891035 : expr_location_or (tree t, location_t loc)
245 : {
246 895811 : location_t tloc = EXPR_LOCATION (t);
247 2875374 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
248 : }
249 :
250 : /* Similar to protected_set_expr_location, but never modify x in place,
251 : if location can and needs to be set, unshare it. */
252 :
253 : tree
254 7377171 : protected_set_expr_location_unshare (tree x, location_t loc)
255 : {
256 7377171 : if (CAN_HAVE_LOCATION_P (x)
257 6545540 : && EXPR_LOCATION (x) != loc
258 2110766 : && !(TREE_CODE (x) == SAVE_EXPR
259 1055598 : || TREE_CODE (x) == TARGET_EXPR
260 : || TREE_CODE (x) == BIND_EXPR))
261 : {
262 1054844 : x = copy_node (x);
263 1054844 : SET_EXPR_LOCATION (x, loc);
264 : }
265 7377171 : return x;
266 : }
267 :
268 : /* This is nonzero if we should defer warnings about undefined
269 : overflow. This facility exists because these warnings are a
270 : special case. The code to estimate loop iterations does not want
271 : to issue any warnings, since it works with expressions which do not
272 : occur in user code. Various bits of cleanup code call fold(), but
273 : only use the result if it has certain characteristics (e.g., is a
274 : constant); that code only wants to issue a warning if the result is
275 : used. */
276 :
277 : static int fold_deferring_overflow_warnings;
278 :
279 : /* If a warning about undefined overflow is deferred, this is the
280 : warning. Note that this may cause us to turn two warnings into
281 : one, but that is fine since it is sufficient to only give one
282 : warning per expression. */
283 :
284 : static const char* fold_deferred_overflow_warning;
285 :
286 : /* If a warning about undefined overflow is deferred, this is the
287 : level at which the warning should be emitted. */
288 :
289 : static enum warn_strict_overflow_code fold_deferred_overflow_code;
290 :
291 : /* Start deferring overflow warnings. We could use a stack here to
292 : permit nested calls, but at present it is not necessary. */
293 :
294 : void
295 1202306195 : fold_defer_overflow_warnings (void)
296 : {
297 1202306195 : ++fold_deferring_overflow_warnings;
298 1202306195 : }
299 :
300 : /* Stop deferring overflow warnings. If there is a pending warning,
301 : and ISSUE is true, then issue the warning if appropriate. STMT is
302 : the statement with which the warning should be associated (used for
303 : location information); STMT may be NULL. CODE is the level of the
304 : warning--a warn_strict_overflow_code value. This function will use
305 : the smaller of CODE and the deferred code when deciding whether to
306 : issue the warning. CODE may be zero to mean to always use the
307 : deferred code. */
308 :
309 : void
310 1202306195 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
311 : {
312 1202306195 : const char *warnmsg;
313 1202306195 : location_t locus;
314 :
315 1202306195 : gcc_assert (fold_deferring_overflow_warnings > 0);
316 1202306195 : --fold_deferring_overflow_warnings;
317 1202306195 : if (fold_deferring_overflow_warnings > 0)
318 : {
319 9069383 : if (fold_deferred_overflow_warning != NULL
320 0 : && code != 0
321 0 : && code < (int) fold_deferred_overflow_code)
322 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
323 9069383 : return;
324 : }
325 :
326 1193236812 : warnmsg = fold_deferred_overflow_warning;
327 1193236812 : fold_deferred_overflow_warning = NULL;
328 :
329 1193236812 : if (!issue || warnmsg == NULL)
330 : return;
331 :
332 0 : if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
333 : return;
334 :
335 : /* Use the smallest code level when deciding to issue the
336 : warning. */
337 0 : if (code == 0 || code > (int) fold_deferred_overflow_code)
338 0 : code = fold_deferred_overflow_code;
339 :
340 0 : if (!issue_strict_overflow_warning (code))
341 : return;
342 :
343 0 : if (stmt == NULL)
344 : locus = input_location;
345 : else
346 0 : locus = gimple_location (stmt);
347 0 : warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
348 : }
349 :
350 : /* Stop deferring overflow warnings, ignoring any deferred
351 : warnings. */
352 :
353 : void
354 185192138 : fold_undefer_and_ignore_overflow_warnings (void)
355 : {
356 185192138 : fold_undefer_overflow_warnings (false, NULL, 0);
357 185192138 : }
358 :
359 : /* Whether we are deferring overflow warnings. */
360 :
361 : bool
362 322100758 : fold_deferring_overflow_warnings_p (void)
363 : {
364 322100758 : return fold_deferring_overflow_warnings > 0;
365 : }
366 :
367 : /* Return true if the built-in mathematical function specified by CODE
368 : is odd, i.e. -f(x) == f(-x). */
369 :
370 : bool
371 2079564 : negate_mathfn_p (combined_fn fn)
372 : {
373 2079564 : switch (fn)
374 : {
375 : CASE_CFN_ASIN:
376 : CASE_CFN_ASIN_FN:
377 : CASE_CFN_ASINH:
378 : CASE_CFN_ASINH_FN:
379 : CASE_CFN_ASINPI:
380 : CASE_CFN_ASINPI_FN:
381 : CASE_CFN_ATAN:
382 : CASE_CFN_ATAN_FN:
383 : CASE_CFN_ATANH:
384 : CASE_CFN_ATANH_FN:
385 : CASE_CFN_ATANPI:
386 : CASE_CFN_ATANPI_FN:
387 : CASE_CFN_CASIN:
388 : CASE_CFN_CASIN_FN:
389 : CASE_CFN_CASINH:
390 : CASE_CFN_CASINH_FN:
391 : CASE_CFN_CATAN:
392 : CASE_CFN_CATAN_FN:
393 : CASE_CFN_CATANH:
394 : CASE_CFN_CATANH_FN:
395 : CASE_CFN_CBRT:
396 : CASE_CFN_CBRT_FN:
397 : CASE_CFN_CPROJ:
398 : CASE_CFN_CPROJ_FN:
399 : CASE_CFN_CSIN:
400 : CASE_CFN_CSIN_FN:
401 : CASE_CFN_CSINH:
402 : CASE_CFN_CSINH_FN:
403 : CASE_CFN_CTAN:
404 : CASE_CFN_CTAN_FN:
405 : CASE_CFN_CTANH:
406 : CASE_CFN_CTANH_FN:
407 : CASE_CFN_ERF:
408 : CASE_CFN_ERF_FN:
409 : CASE_CFN_LLROUND:
410 : CASE_CFN_LLROUND_FN:
411 : CASE_CFN_LROUND:
412 : CASE_CFN_LROUND_FN:
413 : CASE_CFN_ROUND:
414 : CASE_CFN_ROUNDEVEN:
415 : CASE_CFN_ROUNDEVEN_FN:
416 : CASE_CFN_SIN:
417 : CASE_CFN_SIN_FN:
418 : CASE_CFN_SINH:
419 : CASE_CFN_SINH_FN:
420 : CASE_CFN_SINPI:
421 : CASE_CFN_SINPI_FN:
422 : CASE_CFN_TAN:
423 : CASE_CFN_TAN_FN:
424 : CASE_CFN_TANH:
425 : CASE_CFN_TANH_FN:
426 : CASE_CFN_TANPI:
427 : CASE_CFN_TANPI_FN:
428 : CASE_CFN_TRUNC:
429 : CASE_CFN_TRUNC_FN:
430 : return true;
431 :
432 408 : CASE_CFN_LLRINT:
433 408 : CASE_CFN_LLRINT_FN:
434 408 : CASE_CFN_LRINT:
435 408 : CASE_CFN_LRINT_FN:
436 408 : CASE_CFN_NEARBYINT:
437 408 : CASE_CFN_NEARBYINT_FN:
438 408 : CASE_CFN_RINT:
439 408 : CASE_CFN_RINT_FN:
440 408 : return !flag_rounding_math;
441 :
442 2075548 : default:
443 2075548 : break;
444 : }
445 2075548 : return false;
446 : }
447 :
448 : /* Check whether we may negate an integer constant T without causing
449 : overflow. */
450 :
451 : bool
452 3109644 : may_negate_without_overflow_p (const_tree t)
453 : {
454 3109644 : tree type;
455 :
456 3109644 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
457 :
458 3109644 : type = TREE_TYPE (t);
459 3109644 : if (TYPE_UNSIGNED (type))
460 : return false;
461 :
462 3109644 : return !wi::only_sign_bit_p (wi::to_wide (t));
463 : }
464 :
465 : /* Determine whether an expression T can be cheaply negated using
466 : the function negate_expr without introducing undefined overflow. */
467 :
468 : static bool
469 27005967 : negate_expr_p (tree t)
470 : {
471 27160848 : tree type;
472 :
473 27160848 : if (t == 0)
474 : return false;
475 :
476 27160848 : type = TREE_TYPE (t);
477 :
478 27160848 : STRIP_SIGN_NOPS (t);
479 27160848 : switch (TREE_CODE (t))
480 : {
481 1565819 : case INTEGER_CST:
482 1565819 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
483 : return true;
484 :
485 : /* Check that -CST will not overflow type. */
486 372947 : return may_negate_without_overflow_p (t);
487 539 : case BIT_NOT_EXPR:
488 539 : return (INTEGRAL_TYPE_P (type)
489 539 : && TYPE_OVERFLOW_WRAPS (type));
490 :
491 : case FIXED_CST:
492 : return true;
493 :
494 1296 : case NEGATE_EXPR:
495 1296 : return !TYPE_OVERFLOW_SANITIZED (type);
496 :
497 1265169 : case REAL_CST:
498 : /* We want to canonicalize to positive real constants. Pretend
499 : that only negative ones can be easily negated. */
500 1265169 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
501 :
502 454 : case COMPLEX_CST:
503 454 : return negate_expr_p (TREE_REALPART (t))
504 572 : && negate_expr_p (TREE_IMAGPART (t));
505 :
506 120 : case VECTOR_CST:
507 120 : {
508 120 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
509 : return true;
510 :
511 : /* Steps don't prevent negation. */
512 120 : unsigned int count = vector_cst_encoded_nelts (t);
513 240 : for (unsigned int i = 0; i < count; ++i)
514 120 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
515 : return false;
516 :
517 : return true;
518 : }
519 :
520 702 : case COMPLEX_EXPR:
521 702 : return negate_expr_p (TREE_OPERAND (t, 0))
522 702 : && negate_expr_p (TREE_OPERAND (t, 1));
523 :
524 33 : case CONJ_EXPR:
525 33 : return negate_expr_p (TREE_OPERAND (t, 0));
526 :
527 1504476 : case PLUS_EXPR:
528 1504476 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
529 1504470 : || HONOR_SIGNED_ZEROS (type)
530 2728365 : || (ANY_INTEGRAL_TYPE_P (type)
531 1223703 : && ! TYPE_OVERFLOW_WRAPS (type)))
532 729701 : return false;
533 : /* -(A + B) -> (-B) - A. */
534 774775 : if (negate_expr_p (TREE_OPERAND (t, 1)))
535 : return true;
536 : /* -(A + B) -> (-A) - B. */
537 146608 : return negate_expr_p (TREE_OPERAND (t, 0));
538 :
539 254479 : case MINUS_EXPR:
540 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
541 254479 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
542 254479 : && !HONOR_SIGNED_ZEROS (type)
543 339510 : && (! ANY_INTEGRAL_TYPE_P (type)
544 84808 : || TYPE_OVERFLOW_WRAPS (type));
545 :
546 2344785 : case MULT_EXPR:
547 2344785 : if (TYPE_UNSIGNED (type))
548 : break;
549 : /* INT_MIN/n * n doesn't overflow while negating one operand it does
550 : if n is a (negative) power of two. */
551 4021178 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
552 160727 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
553 2168974 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
554 0 : && (wi::popcount
555 2010589 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
556 158385 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
557 135462 : && (wi::popcount
558 4133717 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
559 : break;
560 :
561 : /* Fall through. */
562 :
563 2273113 : case RDIV_EXPR:
564 2273113 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
565 2273112 : return negate_expr_p (TREE_OPERAND (t, 1))
566 2273112 : || negate_expr_p (TREE_OPERAND (t, 0));
567 : break;
568 :
569 2501 : case TRUNC_DIV_EXPR:
570 2501 : case ROUND_DIV_EXPR:
571 2501 : case EXACT_DIV_EXPR:
572 2501 : if (TYPE_UNSIGNED (type))
573 : break;
574 : /* In general we can't negate A in A / B, because if A is INT_MIN and
575 : B is not 1 we change the sign of the result. */
576 542 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
577 542 : && negate_expr_p (TREE_OPERAND (t, 0)))
578 : return true;
579 : /* In general we can't negate B in A / B, because if A is INT_MIN and
580 : B is 1, we may turn this into INT_MIN / -1 which is undefined
581 : and actually traps on some architectures. */
582 754 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
583 377 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
584 669 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
585 282 : && ! integer_onep (TREE_OPERAND (t, 1))))
586 367 : return negate_expr_p (TREE_OPERAND (t, 1));
587 : break;
588 :
589 4907347 : case NOP_EXPR:
590 : /* Negate -((double)float) as (double)(-float). */
591 4907347 : if (SCALAR_FLOAT_TYPE_P (type))
592 : {
593 8021 : tree tem = strip_float_extensions (t);
594 8021 : if (tem != t)
595 : return negate_expr_p (tem);
596 : }
597 : break;
598 :
599 1042313 : case CALL_EXPR:
600 : /* Negate -f(x) as f(-x). */
601 1042313 : if (negate_mathfn_p (get_call_combined_fn (t)))
602 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
603 : break;
604 :
605 8494 : case RSHIFT_EXPR:
606 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
607 8494 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
608 : {
609 8349 : tree op1 = TREE_OPERAND (t, 1);
610 8349 : if (wi::to_wide (op1) == element_precision (type) - 1)
611 : return true;
612 : }
613 : break;
614 :
615 : default:
616 : break;
617 : }
618 : return false;
619 : }
620 :
621 : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
622 : simplification is possible.
623 : If negate_expr_p would return true for T, NULL_TREE will never be
624 : returned. */
625 :
626 : static tree
627 38956448 : fold_negate_expr_1 (location_t loc, tree t)
628 : {
629 38956448 : tree type = TREE_TYPE (t);
630 38956448 : tree tem;
631 :
632 38956448 : switch (TREE_CODE (t))
633 : {
634 : /* Convert - (~A) to A + 1. */
635 162 : case BIT_NOT_EXPR:
636 162 : if (INTEGRAL_TYPE_P (type))
637 162 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
638 162 : build_one_cst (type));
639 : break;
640 :
641 29965209 : case INTEGER_CST:
642 29965209 : tem = fold_negate_const (t, type);
643 29965209 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
644 10104 : || (ANY_INTEGRAL_TYPE_P (type)
645 10104 : && !TYPE_OVERFLOW_TRAPS (type)
646 10104 : && TYPE_OVERFLOW_WRAPS (type))
647 29974525 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
648 : return tem;
649 : break;
650 :
651 1981628 : case POLY_INT_CST:
652 1981628 : case REAL_CST:
653 1981628 : case FIXED_CST:
654 1981628 : tem = fold_negate_const (t, type);
655 1981628 : return tem;
656 :
657 66141 : case COMPLEX_CST:
658 66141 : {
659 66141 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
660 66141 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
661 66141 : if (rpart && ipart)
662 66141 : return build_complex (type, rpart, ipart);
663 : }
664 : break;
665 :
666 49639 : case VECTOR_CST:
667 49639 : {
668 49639 : tree_vector_builder elts;
669 49639 : elts.new_unary_operation (type, t, true);
670 49639 : unsigned int count = elts.encoded_nelts ();
671 121564 : for (unsigned int i = 0; i < count; ++i)
672 : {
673 71925 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
674 71925 : if (elt == NULL_TREE)
675 0 : return NULL_TREE;
676 71925 : elts.quick_push (elt);
677 : }
678 :
679 49639 : return elts.build ();
680 49639 : }
681 :
682 78 : case COMPLEX_EXPR:
683 78 : if (negate_expr_p (t))
684 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
685 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
686 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
687 : break;
688 :
689 21 : case CONJ_EXPR:
690 21 : if (negate_expr_p (t))
691 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
692 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
693 : break;
694 :
695 1222 : case NEGATE_EXPR:
696 1222 : if (!TYPE_OVERFLOW_SANITIZED (type))
697 1209 : return TREE_OPERAND (t, 0);
698 : break;
699 :
700 686484 : case PLUS_EXPR:
701 686484 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
702 686484 : && !HONOR_SIGNED_ZEROS (type))
703 : {
704 : /* -(A + B) -> (-B) - A. */
705 686374 : if (negate_expr_p (TREE_OPERAND (t, 1)))
706 : {
707 631880 : tem = negate_expr (TREE_OPERAND (t, 1));
708 631880 : return fold_build2_loc (loc, MINUS_EXPR, type,
709 1263760 : tem, TREE_OPERAND (t, 0));
710 : }
711 :
712 : /* -(A + B) -> (-A) - B. */
713 54494 : if (negate_expr_p (TREE_OPERAND (t, 0)))
714 : {
715 1021 : tem = negate_expr (TREE_OPERAND (t, 0));
716 1021 : return fold_build2_loc (loc, MINUS_EXPR, type,
717 2042 : tem, TREE_OPERAND (t, 1));
718 : }
719 : }
720 : break;
721 :
722 154716 : case MINUS_EXPR:
723 : /* - (A - B) -> B - A */
724 154716 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
725 154716 : && !HONOR_SIGNED_ZEROS (type))
726 79519 : return fold_build2_loc (loc, MINUS_EXPR, type,
727 159038 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
728 : break;
729 :
730 257819 : case MULT_EXPR:
731 257819 : if (TYPE_UNSIGNED (type))
732 : break;
733 :
734 : /* Fall through. */
735 :
736 34541 : case RDIV_EXPR:
737 34541 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
738 : {
739 34541 : tem = TREE_OPERAND (t, 1);
740 34541 : if (negate_expr_p (tem))
741 62940 : return fold_build2_loc (loc, TREE_CODE (t), type,
742 62940 : TREE_OPERAND (t, 0), negate_expr (tem));
743 3071 : tem = TREE_OPERAND (t, 0);
744 3071 : if (negate_expr_p (tem))
745 56 : return fold_build2_loc (loc, TREE_CODE (t), type,
746 112 : negate_expr (tem), TREE_OPERAND (t, 1));
747 : }
748 : break;
749 :
750 2038 : case TRUNC_DIV_EXPR:
751 2038 : case ROUND_DIV_EXPR:
752 2038 : case EXACT_DIV_EXPR:
753 2038 : if (TYPE_UNSIGNED (type))
754 : break;
755 : /* In general we can't negate A in A / B, because if A is INT_MIN and
756 : B is not 1 we change the sign of the result. */
757 720 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
758 720 : && negate_expr_p (TREE_OPERAND (t, 0)))
759 323 : return fold_build2_loc (loc, TREE_CODE (t), type,
760 323 : negate_expr (TREE_OPERAND (t, 0)),
761 646 : TREE_OPERAND (t, 1));
762 : /* In general we can't negate B in A / B, because if A is INT_MIN and
763 : B is 1, we may turn this into INT_MIN / -1 which is undefined
764 : and actually traps on some architectures. */
765 794 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
766 397 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
767 313 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
768 290 : && ! integer_onep (TREE_OPERAND (t, 1))))
769 771 : && negate_expr_p (TREE_OPERAND (t, 1)))
770 736 : return fold_build2_loc (loc, TREE_CODE (t), type,
771 368 : TREE_OPERAND (t, 0),
772 736 : negate_expr (TREE_OPERAND (t, 1)));
773 : break;
774 :
775 2323618 : case NOP_EXPR:
776 : /* Convert -((double)float) into (double)(-float). */
777 2323618 : if (SCALAR_FLOAT_TYPE_P (type))
778 : {
779 10626 : tem = strip_float_extensions (t);
780 10626 : if (tem != t && negate_expr_p (tem))
781 0 : return fold_convert_loc (loc, type, negate_expr (tem));
782 : }
783 : break;
784 :
785 287141 : case CALL_EXPR:
786 : /* Negate -f(x) as f(-x). */
787 287141 : if (negate_mathfn_p (get_call_combined_fn (t))
788 288430 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
789 : {
790 1191 : tree fndecl, arg;
791 :
792 1191 : fndecl = get_callee_fndecl (t);
793 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
794 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
795 : }
796 : break;
797 :
798 8270 : case RSHIFT_EXPR:
799 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
800 8270 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
801 : {
802 8252 : tree op1 = TREE_OPERAND (t, 1);
803 8252 : if (wi::to_wide (op1) == element_precision (type) - 1)
804 : {
805 7923 : tree ntype = TYPE_UNSIGNED (type)
806 7923 : ? signed_type_for (type)
807 72 : : unsigned_type_for (type);
808 7923 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
809 7923 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
810 7923 : return fold_convert_loc (loc, type, temp);
811 : }
812 : }
813 : break;
814 :
815 : default:
816 : break;
817 : }
818 :
819 : return NULL_TREE;
820 : }
821 :
822 : /* A wrapper for fold_negate_expr_1. */
823 :
824 : static tree
825 38956448 : fold_negate_expr (location_t loc, tree t)
826 : {
827 38956448 : tree type = TREE_TYPE (t);
828 38956448 : STRIP_SIGN_NOPS (t);
829 38956448 : tree tem = fold_negate_expr_1 (loc, t);
830 38956448 : if (tem == NULL_TREE)
831 : return NULL_TREE;
832 32817668 : return fold_convert_loc (loc, type, tem);
833 : }
834 :
835 : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
836 : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
837 : return NULL_TREE. */
838 :
839 : static tree
840 3770565 : negate_expr (tree t)
841 : {
842 3770565 : tree type, tem;
843 3770565 : location_t loc;
844 :
845 3770565 : if (t == NULL_TREE)
846 : return NULL_TREE;
847 :
848 3770565 : loc = EXPR_LOCATION (t);
849 3770565 : type = TREE_TYPE (t);
850 3770565 : STRIP_SIGN_NOPS (t);
851 :
852 3770565 : tem = fold_negate_expr (loc, t);
853 3770565 : if (!tem)
854 1902173 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
855 3770565 : return fold_convert_loc (loc, type, tem);
856 : }
857 :
858 : /* Split a tree IN into a constant, literal and variable parts that could be
859 : combined with CODE to make IN. "constant" means an expression with
860 : TREE_CONSTANT but that isn't an actual constant. CODE must be a
861 : commutative arithmetic operation. Store the constant part into *CONP,
862 : the literal in *LITP and return the variable part. If a part isn't
863 : present, set it to null. If the tree does not decompose in this way,
864 : return the entire tree as the variable part and the other parts as null.
865 :
866 : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
867 : case, we negate an operand that was subtracted. Except if it is a
868 : literal for which we use *MINUS_LITP instead.
869 :
870 : If NEGATE_P is true, we are negating all of IN, again except a literal
871 : for which we use *MINUS_LITP instead. If a variable part is of pointer
872 : type, it is negated after converting to TYPE. This prevents us from
873 : generating illegal MINUS pointer expression. LOC is the location of
874 : the converted variable part.
875 :
876 : If IN is itself a literal or constant, return it as appropriate.
877 :
878 : Note that we do not guarantee that any of the three values will be the
879 : same type as IN, but they will have the same signedness and mode. */
880 :
881 : static tree
882 226122126 : split_tree (tree in, tree type, enum tree_code code,
883 : tree *minus_varp, tree *conp, tree *minus_conp,
884 : tree *litp, tree *minus_litp, int negate_p)
885 : {
886 226122126 : tree var = 0;
887 226122126 : *minus_varp = 0;
888 226122126 : *conp = 0;
889 226122126 : *minus_conp = 0;
890 226122126 : *litp = 0;
891 226122126 : *minus_litp = 0;
892 :
893 : /* Strip any conversions that don't change the machine mode or signedness. */
894 226122126 : STRIP_SIGN_NOPS (in);
895 :
896 226122126 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
897 144411581 : || TREE_CODE (in) == FIXED_CST)
898 81710545 : *litp = in;
899 144411581 : else if (TREE_CODE (in) == code
900 144411581 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
901 139882913 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
902 : /* We can associate addition and subtraction together (even
903 : though the C standard doesn't say so) for integers because
904 : the value is not affected. For reals, the value might be
905 : affected, so we can't. */
906 139882913 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
907 58140134 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
908 138192476 : || (code == MINUS_EXPR
909 22587422 : && (TREE_CODE (in) == PLUS_EXPR
910 20734440 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
911 : {
912 8513676 : tree op0 = TREE_OPERAND (in, 0);
913 8513676 : tree op1 = TREE_OPERAND (in, 1);
914 8513676 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
915 8513676 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
916 :
917 : /* First see if either of the operands is a literal, then a constant. */
918 8513676 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
919 8296536 : || TREE_CODE (op0) == FIXED_CST)
920 217140 : *litp = op0, op0 = 0;
921 8296536 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
922 5547392 : || TREE_CODE (op1) == FIXED_CST)
923 2749144 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
924 :
925 8513676 : if (op0 != 0 && TREE_CONSTANT (op0))
926 15269 : *conp = op0, op0 = 0;
927 8498407 : else if (op1 != 0 && TREE_CONSTANT (op1))
928 51732 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
929 :
930 : /* If we haven't dealt with either operand, this is not a case we can
931 : decompose. Otherwise, VAR is either of the ones remaining, if any. */
932 8513676 : if (op0 != 0 && op1 != 0)
933 : var = in;
934 3026166 : else if (op0 != 0)
935 : var = op0;
936 : else
937 232409 : var = op1, neg_var_p = neg1_p;
938 :
939 : /* Now do any needed negations. */
940 8513676 : if (neg_litp_p)
941 27617 : *minus_litp = *litp, *litp = 0;
942 8513676 : if (neg_conp_p && *conp)
943 11176 : *minus_conp = *conp, *conp = 0;
944 8513676 : if (neg_var_p && var)
945 221920 : *minus_varp = var, var = 0;
946 : }
947 135897905 : else if (TREE_CONSTANT (in))
948 803097 : *conp = in;
949 135094808 : else if (TREE_CODE (in) == BIT_NOT_EXPR
950 504897 : && code == PLUS_EXPR)
951 : {
952 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
953 : when IN is constant. */
954 358568 : *litp = build_minus_one_cst (type);
955 358568 : *minus_varp = TREE_OPERAND (in, 0);
956 : }
957 : else
958 : var = in;
959 :
960 226122126 : if (negate_p)
961 : {
962 12471568 : if (*litp)
963 1232066 : *minus_litp = *litp, *litp = 0;
964 11239502 : else if (*minus_litp)
965 152 : *litp = *minus_litp, *minus_litp = 0;
966 12471568 : if (*conp)
967 46305 : *minus_conp = *conp, *conp = 0;
968 12425263 : else if (*minus_conp)
969 0 : *conp = *minus_conp, *minus_conp = 0;
970 12471568 : if (var)
971 12412960 : *minus_varp = var, var = 0;
972 58608 : else if (*minus_varp)
973 770 : var = *minus_varp, *minus_varp = 0;
974 : }
975 :
976 226122126 : if (*litp
977 226122126 : && TREE_OVERFLOW_P (*litp))
978 19729 : *litp = drop_tree_overflow (*litp);
979 226122126 : if (*minus_litp
980 226122126 : && TREE_OVERFLOW_P (*minus_litp))
981 24 : *minus_litp = drop_tree_overflow (*minus_litp);
982 :
983 226122126 : return var;
984 : }
985 :
986 : /* Re-associate trees split by the above function. T1 and T2 are
987 : either expressions to associate or null. Return the new
988 : expression, if any. LOC is the location of the new expression. If
989 : we build an operation, do it in TYPE and with CODE. */
990 :
991 : static tree
992 20523974 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
993 : {
994 20523974 : if (t1 == 0)
995 : {
996 13013917 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
997 : return t2;
998 : }
999 7510057 : else if (t2 == 0)
1000 : return t1;
1001 :
1002 : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1003 : try to fold this since we will have infinite recursion. But do
1004 : deal with any NEGATE_EXPRs. */
1005 4193524 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1006 3313233 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1007 3247304 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1008 : {
1009 1639623 : if (code == PLUS_EXPR)
1010 : {
1011 925135 : if (TREE_CODE (t1) == NEGATE_EXPR)
1012 54 : return build2_loc (loc, MINUS_EXPR, type,
1013 : fold_convert_loc (loc, type, t2),
1014 : fold_convert_loc (loc, type,
1015 108 : TREE_OPERAND (t1, 0)));
1016 925081 : else if (TREE_CODE (t2) == NEGATE_EXPR)
1017 1 : return build2_loc (loc, MINUS_EXPR, type,
1018 : fold_convert_loc (loc, type, t1),
1019 : fold_convert_loc (loc, type,
1020 2 : TREE_OPERAND (t2, 0)));
1021 925080 : else if (integer_zerop (t2))
1022 38548 : return fold_convert_loc (loc, type, t1);
1023 : }
1024 714488 : else if (code == MINUS_EXPR)
1025 : {
1026 690589 : if (integer_zerop (t2))
1027 0 : return fold_convert_loc (loc, type, t1);
1028 : }
1029 :
1030 1601020 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1031 1601020 : fold_convert_loc (loc, type, t2));
1032 : }
1033 :
1034 2553901 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1035 2553901 : fold_convert_loc (loc, type, t2));
1036 : }
1037 :
1038 : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1039 : for use in int_const_binop, size_binop and size_diffop. */
1040 :
1041 : static bool
1042 2538801595 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1043 : {
1044 2538801595 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1045 : return false;
1046 2538801595 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1047 : return false;
1048 :
1049 2538801595 : switch (code)
1050 : {
1051 : case LSHIFT_EXPR:
1052 : case RSHIFT_EXPR:
1053 : case LROTATE_EXPR:
1054 : case RROTATE_EXPR:
1055 : return true;
1056 :
1057 2538801595 : default:
1058 2538801595 : break;
1059 : }
1060 :
1061 2538801595 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1062 2538801595 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1063 5077603190 : && TYPE_MODE (type1) == TYPE_MODE (type2);
1064 : }
1065 :
1066 : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1067 : a new constant in RES. Return FALSE if we don't know how to
1068 : evaluate CODE at compile-time. */
1069 :
1070 : bool
1071 1413882428 : wide_int_binop (wide_int &res,
1072 : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1073 : signop sign, wi::overflow_type *overflow)
1074 : {
1075 1413882428 : wide_int tmp;
1076 1413882428 : *overflow = wi::OVF_NONE;
1077 1413882428 : switch (code)
1078 : {
1079 2743684 : case BIT_IOR_EXPR:
1080 2743684 : res = wi::bit_or (arg1, arg2);
1081 2743684 : break;
1082 :
1083 90648 : case BIT_XOR_EXPR:
1084 90648 : res = wi::bit_xor (arg1, arg2);
1085 90648 : break;
1086 :
1087 22293392 : case BIT_AND_EXPR:
1088 22293392 : res = wi::bit_and (arg1, arg2);
1089 22293392 : break;
1090 :
1091 13271141 : case LSHIFT_EXPR:
1092 13271141 : if (wi::neg_p (arg2))
1093 : return false;
1094 13240817 : res = wi::lshift (arg1, arg2);
1095 13240817 : break;
1096 :
1097 6364900 : case RSHIFT_EXPR:
1098 6364900 : if (wi::neg_p (arg2))
1099 : return false;
1100 : /* It's unclear from the C standard whether shifts can overflow.
1101 : The following code ignores overflow; perhaps a C standard
1102 : interpretation ruling is needed. */
1103 6364704 : res = wi::rshift (arg1, arg2, sign);
1104 6364704 : break;
1105 :
1106 1885 : case RROTATE_EXPR:
1107 1885 : case LROTATE_EXPR:
1108 1885 : if (wi::neg_p (arg2))
1109 : {
1110 14 : tmp = -arg2;
1111 14 : if (code == RROTATE_EXPR)
1112 : code = LROTATE_EXPR;
1113 : else
1114 : code = RROTATE_EXPR;
1115 : }
1116 : else
1117 1871 : tmp = arg2;
1118 :
1119 1871 : if (code == RROTATE_EXPR)
1120 1698 : res = wi::rrotate (arg1, tmp);
1121 : else
1122 187 : res = wi::lrotate (arg1, tmp);
1123 : break;
1124 :
1125 232235509 : case PLUS_EXPR:
1126 232235509 : res = wi::add (arg1, arg2, sign, overflow);
1127 232235509 : break;
1128 :
1129 72437255 : case MINUS_EXPR:
1130 72437255 : res = wi::sub (arg1, arg2, sign, overflow);
1131 72437255 : break;
1132 :
1133 397448096 : case MULT_EXPR:
1134 397448096 : res = wi::mul (arg1, arg2, sign, overflow);
1135 397448096 : break;
1136 :
1137 5288 : case MULT_HIGHPART_EXPR:
1138 5288 : res = wi::mul_high (arg1, arg2, sign);
1139 5288 : break;
1140 :
1141 340002169 : case TRUNC_DIV_EXPR:
1142 340002169 : case EXACT_DIV_EXPR:
1143 340002169 : if (arg2 == 0)
1144 : return false;
1145 339996228 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1146 339996228 : break;
1147 :
1148 78120405 : case FLOOR_DIV_EXPR:
1149 78120405 : if (arg2 == 0)
1150 : return false;
1151 78120405 : res = wi::div_floor (arg1, arg2, sign, overflow);
1152 78120405 : break;
1153 :
1154 82819951 : case CEIL_DIV_EXPR:
1155 82819951 : if (arg2 == 0)
1156 : return false;
1157 82819951 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1158 82819951 : break;
1159 :
1160 0 : case ROUND_DIV_EXPR:
1161 0 : if (arg2 == 0)
1162 : return false;
1163 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1164 0 : break;
1165 :
1166 1231012 : case TRUNC_MOD_EXPR:
1167 1231012 : if (arg2 == 0)
1168 : return false;
1169 1229907 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1170 1229907 : break;
1171 :
1172 66532987 : case FLOOR_MOD_EXPR:
1173 66532987 : if (arg2 == 0)
1174 : return false;
1175 66532987 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1176 66532987 : break;
1177 :
1178 178 : case CEIL_MOD_EXPR:
1179 178 : if (arg2 == 0)
1180 : return false;
1181 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1182 178 : break;
1183 :
1184 0 : case ROUND_MOD_EXPR:
1185 0 : if (arg2 == 0)
1186 : return false;
1187 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1188 0 : break;
1189 :
1190 47498 : case MIN_EXPR:
1191 47498 : res = wi::min (arg1, arg2, sign);
1192 47498 : break;
1193 :
1194 98236303 : case MAX_EXPR:
1195 98236303 : res = wi::max (arg1, arg2, sign);
1196 98236303 : break;
1197 :
1198 : default:
1199 : return false;
1200 : }
1201 : return true;
1202 1413882428 : }
1203 :
1204 : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1205 : min value to RES. */
1206 : bool
1207 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1208 : {
1209 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1210 : {
1211 0 : res = wi::to_poly_wide (arg1);
1212 0 : return true;
1213 : }
1214 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1215 : {
1216 0 : res = wi::to_poly_wide (arg2);
1217 0 : return true;
1218 : }
1219 :
1220 : return false;
1221 : }
1222 :
1223 : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1224 : produce a new constant in RES. Return FALSE if we don't know how
1225 : to evaluate CODE at compile-time. */
1226 :
1227 : bool
1228 1413882428 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1229 : const_tree arg1, const_tree arg2,
1230 : signop sign, wi::overflow_type *overflow)
1231 : {
1232 1413882428 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1233 :
1234 1413882428 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1235 : {
1236 1413882428 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1237 1413882428 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1238 1413882428 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1239 : return NULL_TREE;
1240 1413844735 : res = wi_res;
1241 1413844735 : return true;
1242 1413882677 : }
1243 :
1244 : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1245 :
1246 : switch (code)
1247 : {
1248 : case PLUS_EXPR:
1249 : res = wi::add (wi::to_poly_wide (arg1),
1250 : wi::to_poly_wide (arg2), sign, overflow);
1251 : break;
1252 :
1253 : case MINUS_EXPR:
1254 : res = wi::sub (wi::to_poly_wide (arg1),
1255 : wi::to_poly_wide (arg2), sign, overflow);
1256 : break;
1257 :
1258 : case MULT_EXPR:
1259 : if (TREE_CODE (arg2) == INTEGER_CST)
1260 : res = wi::mul (wi::to_poly_wide (arg1),
1261 : wi::to_wide (arg2), sign, overflow);
1262 : else if (TREE_CODE (arg1) == INTEGER_CST)
1263 : res = wi::mul (wi::to_poly_wide (arg2),
1264 : wi::to_wide (arg1), sign, overflow);
1265 : else
1266 : return NULL_TREE;
1267 : break;
1268 :
1269 : case LSHIFT_EXPR:
1270 : if (TREE_CODE (arg2) == INTEGER_CST)
1271 : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1272 : else
1273 : return false;
1274 : break;
1275 :
1276 : case BIT_AND_EXPR:
1277 : if (TREE_CODE (arg2) != INTEGER_CST
1278 : || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1279 : &res))
1280 : return false;
1281 : break;
1282 :
1283 : case BIT_IOR_EXPR:
1284 : if (TREE_CODE (arg2) != INTEGER_CST
1285 : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1286 : &res))
1287 : return false;
1288 : break;
1289 :
1290 : case MIN_EXPR:
1291 : if (!can_min_p (arg1, arg2, res))
1292 : return false;
1293 : break;
1294 :
1295 : default:
1296 : return false;
1297 : }
1298 : return true;
1299 : }
1300 :
1301 : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1302 : produce a new constant. Return NULL_TREE if we don't know how to
1303 : evaluate CODE at compile-time. */
1304 :
1305 : tree
1306 1413882428 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1307 : int overflowable)
1308 : {
1309 1413882428 : poly_wide_int poly_res;
1310 1413882428 : tree type = TREE_TYPE (arg1);
1311 1413882428 : signop sign = TYPE_SIGN (type);
1312 1413882428 : wi::overflow_type overflow = wi::OVF_NONE;
1313 :
1314 1413882428 : if (!poly_int_tree_p (arg1)
1315 1413882428 : || !poly_int_tree_p (arg2)
1316 2827764856 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1317 37693 : return NULL_TREE;
1318 1413844735 : return force_fit_type (type, poly_res, overflowable,
1319 1413844735 : (((sign == SIGNED || overflowable == -1)
1320 1413844735 : && overflow)
1321 1413844735 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1322 1413882428 : }
1323 :
1324 : /* Return true if binary operation OP distributes over addition in operand
1325 : OPNO, with the other operand being held constant. OPNO counts from 1. */
1326 :
1327 : static bool
1328 187091 : distributes_over_addition_p (tree_code op, int opno)
1329 : {
1330 0 : switch (op)
1331 : {
1332 : case PLUS_EXPR:
1333 : case MINUS_EXPR:
1334 : case MULT_EXPR:
1335 : return true;
1336 :
1337 0 : case LSHIFT_EXPR:
1338 0 : return opno == 1;
1339 :
1340 3860 : default:
1341 3860 : return false;
1342 : }
1343 : }
1344 :
1345 : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1346 : is the other operand. Try to use the value of OP to simplify the
1347 : operation in one step, without having to process individual elements. */
1348 : static tree
1349 441665 : simplify_const_binop (tree_code code, tree op, tree other_op,
1350 : int index ATTRIBUTE_UNUSED)
1351 : {
1352 : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1353 441665 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1354 : {
1355 357438 : if (integer_zerop (other_op))
1356 : {
1357 27329 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1358 : return op;
1359 26235 : else if (code == BIT_AND_EXPR)
1360 : return other_op;
1361 : }
1362 : }
1363 :
1364 : return NULL_TREE;
1365 : }
1366 :
1367 : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1368 : be an elementwise vector operation, try to fold the operation to a
1369 : constant vector, using ELT_CONST_BINOP to fold each element. Return
1370 : the folded value on success, otherwise return null. */
1371 : tree
1372 265830 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1373 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1374 : {
1375 189788 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1376 447454 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1377 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1378 : {
1379 181624 : tree type = TREE_TYPE (arg1);
1380 181624 : bool step_ok_p;
1381 181624 : if (VECTOR_CST_STEPPED_P (arg1)
1382 181624 : && VECTOR_CST_STEPPED_P (arg2))
1383 : /* We can operate directly on the encoding if:
1384 :
1385 : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1386 : implies
1387 : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1388 :
1389 : Addition and subtraction are the supported operators
1390 : for which this is true. */
1391 2697 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1392 178927 : else if (VECTOR_CST_STEPPED_P (arg1))
1393 : /* We can operate directly on stepped encodings if:
1394 :
1395 : a3 - a2 == a2 - a1
1396 : implies:
1397 : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1398 :
1399 : which is true if (x -> x op c) distributes over addition. */
1400 50119 : step_ok_p = distributes_over_addition_p (code, 1);
1401 : else
1402 : /* Similarly in reverse. */
1403 128808 : step_ok_p = distributes_over_addition_p (code, 2);
1404 181624 : tree_vector_builder elts;
1405 181624 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1406 : return NULL_TREE;
1407 181624 : unsigned int count = elts.encoded_nelts ();
1408 705385 : for (unsigned int i = 0; i < count; ++i)
1409 : {
1410 524080 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1411 524080 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1412 :
1413 524080 : tree elt = elt_const_binop (code, elem1, elem2);
1414 :
1415 : /* It is possible that const_binop cannot handle the given
1416 : code and return NULL_TREE */
1417 524080 : if (elt == NULL_TREE)
1418 319 : return NULL_TREE;
1419 523761 : elts.quick_push (elt);
1420 : }
1421 :
1422 181305 : return elts.build ();
1423 181624 : }
1424 :
1425 84206 : if (TREE_CODE (arg1) == VECTOR_CST
1426 8164 : && TREE_CODE (arg2) == INTEGER_CST)
1427 : {
1428 8164 : tree type = TREE_TYPE (arg1);
1429 8164 : bool step_ok_p = distributes_over_addition_p (code, 1);
1430 8164 : tree_vector_builder elts;
1431 8164 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1432 : return NULL_TREE;
1433 8164 : unsigned int count = elts.encoded_nelts ();
1434 34737 : for (unsigned int i = 0; i < count; ++i)
1435 : {
1436 26660 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1437 :
1438 26660 : tree elt = elt_const_binop (code, elem1, arg2);
1439 :
1440 : /* It is possible that const_binop cannot handle the given
1441 : code and return NULL_TREE. */
1442 26660 : if (elt == NULL_TREE)
1443 87 : return NULL_TREE;
1444 26573 : elts.quick_push (elt);
1445 : }
1446 :
1447 8077 : return elts.build ();
1448 8164 : }
1449 : return NULL_TREE;
1450 : }
1451 :
1452 : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1453 : constant. We assume ARG1 and ARG2 have the same data type, or at least
1454 : are the same kind of constant and the same machine mode. Return zero if
1455 : combining the constants is not allowed in the current operating mode. */
1456 :
1457 : static tree
1458 195650761 : const_binop (enum tree_code code, tree arg1, tree arg2)
1459 : {
1460 : /* Sanity check for the recursive cases. */
1461 195650761 : if (!arg1 || !arg2)
1462 : return NULL_TREE;
1463 :
1464 195649497 : STRIP_NOPS (arg1);
1465 195649497 : STRIP_NOPS (arg2);
1466 :
1467 195649497 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1468 : {
1469 189975335 : if (code == POINTER_PLUS_EXPR)
1470 97448 : return int_const_binop (PLUS_EXPR,
1471 194896 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1472 :
1473 189877887 : return int_const_binop (code, arg1, arg2);
1474 : }
1475 :
1476 5674162 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1477 : {
1478 5392006 : machine_mode mode;
1479 5392006 : REAL_VALUE_TYPE d1;
1480 5392006 : REAL_VALUE_TYPE d2;
1481 5392006 : REAL_VALUE_TYPE value;
1482 5392006 : REAL_VALUE_TYPE result;
1483 5392006 : bool inexact;
1484 5392006 : tree t, type;
1485 :
1486 : /* The following codes are handled by real_arithmetic. */
1487 5392006 : switch (code)
1488 : {
1489 5392006 : case PLUS_EXPR:
1490 5392006 : case MINUS_EXPR:
1491 5392006 : case MULT_EXPR:
1492 5392006 : case RDIV_EXPR:
1493 5392006 : case MIN_EXPR:
1494 5392006 : case MAX_EXPR:
1495 5392006 : break;
1496 :
1497 : default:
1498 : return NULL_TREE;
1499 : }
1500 :
1501 5392006 : d1 = TREE_REAL_CST (arg1);
1502 5392006 : d2 = TREE_REAL_CST (arg2);
1503 :
1504 5392006 : type = TREE_TYPE (arg1);
1505 5392006 : mode = TYPE_MODE (type);
1506 :
1507 : /* Don't perform operation if we honor signaling NaNs and
1508 : either operand is a signaling NaN. */
1509 5392006 : if (HONOR_SNANS (mode)
1510 5392006 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1511 6949 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1512 33 : return NULL_TREE;
1513 :
1514 : /* Don't perform operation if it would raise a division
1515 : by zero exception. */
1516 5391973 : if (code == RDIV_EXPR
1517 2340974 : && real_equal (&d2, &dconst0)
1518 5402422 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1519 7541 : return NULL_TREE;
1520 :
1521 : /* If either operand is a NaN, just return it. Otherwise, set up
1522 : for floating-point trap; we return an overflow. */
1523 5384432 : if (REAL_VALUE_ISNAN (d1))
1524 : {
1525 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1526 : is off. */
1527 250 : d1.signalling = 0;
1528 250 : t = build_real (type, d1);
1529 250 : return t;
1530 : }
1531 5384182 : else if (REAL_VALUE_ISNAN (d2))
1532 : {
1533 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1534 : is off. */
1535 61 : d2.signalling = 0;
1536 61 : t = build_real (type, d2);
1537 61 : return t;
1538 : }
1539 :
1540 5384121 : inexact = real_arithmetic (&value, code, &d1, &d2);
1541 5384121 : real_convert (&result, mode, &value);
1542 :
1543 : /* Don't constant fold this floating point operation if
1544 : both operands are not NaN but the result is NaN, and
1545 : flag_trapping_math. Such operations should raise an
1546 : invalid operation exception. */
1547 5384121 : if (flag_trapping_math
1548 20895410 : && MODE_HAS_NANS (mode)
1549 5365401 : && REAL_VALUE_ISNAN (result)
1550 2459 : && !REAL_VALUE_ISNAN (d1)
1551 5386580 : && !REAL_VALUE_ISNAN (d2))
1552 2459 : return NULL_TREE;
1553 :
1554 : /* Don't constant fold this floating point operation if
1555 : the result has overflowed and flag_trapping_math. */
1556 5381662 : if (flag_trapping_math
1557 20885916 : && MODE_HAS_INFINITIES (mode)
1558 5362942 : && REAL_VALUE_ISINF (result)
1559 7488 : && !REAL_VALUE_ISINF (d1)
1560 5388576 : && !REAL_VALUE_ISINF (d2))
1561 4631 : return NULL_TREE;
1562 :
1563 : /* Don't constant fold this floating point operation if the
1564 : result may dependent upon the run-time rounding mode and
1565 : flag_rounding_math is set, or if GCC's software emulation
1566 : is unable to accurately represent the result. */
1567 5377031 : if ((flag_rounding_math
1568 36493380 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1569 5377031 : && (inexact || !real_identical (&result, &value)))
1570 1107 : return NULL_TREE;
1571 :
1572 5375924 : t = build_real (type, result);
1573 :
1574 5375924 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1575 5375924 : return t;
1576 : }
1577 :
1578 282156 : if (TREE_CODE (arg1) == FIXED_CST)
1579 : {
1580 0 : FIXED_VALUE_TYPE f1;
1581 0 : FIXED_VALUE_TYPE f2;
1582 0 : FIXED_VALUE_TYPE result;
1583 0 : tree t, type;
1584 0 : bool sat_p;
1585 0 : bool overflow_p;
1586 :
1587 : /* The following codes are handled by fixed_arithmetic. */
1588 0 : switch (code)
1589 : {
1590 0 : case PLUS_EXPR:
1591 0 : case MINUS_EXPR:
1592 0 : case MULT_EXPR:
1593 0 : case TRUNC_DIV_EXPR:
1594 0 : if (TREE_CODE (arg2) != FIXED_CST)
1595 : return NULL_TREE;
1596 0 : f2 = TREE_FIXED_CST (arg2);
1597 0 : break;
1598 :
1599 0 : case LSHIFT_EXPR:
1600 0 : case RSHIFT_EXPR:
1601 0 : {
1602 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1603 0 : return NULL_TREE;
1604 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1605 0 : f2.data.high = w2.elt (1);
1606 0 : f2.data.low = w2.ulow ();
1607 0 : f2.mode = SImode;
1608 : }
1609 0 : break;
1610 :
1611 : default:
1612 : return NULL_TREE;
1613 : }
1614 :
1615 0 : f1 = TREE_FIXED_CST (arg1);
1616 0 : type = TREE_TYPE (arg1);
1617 0 : sat_p = TYPE_SATURATING (type);
1618 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1619 0 : t = build_fixed (type, result);
1620 : /* Propagate overflow flags. */
1621 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1622 0 : TREE_OVERFLOW (t) = 1;
1623 0 : return t;
1624 : }
1625 :
1626 282156 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1627 : {
1628 11190 : tree type = TREE_TYPE (arg1);
1629 11190 : tree r1 = TREE_REALPART (arg1);
1630 11190 : tree i1 = TREE_IMAGPART (arg1);
1631 11190 : tree r2 = TREE_REALPART (arg2);
1632 11190 : tree i2 = TREE_IMAGPART (arg2);
1633 11190 : tree real, imag;
1634 :
1635 11190 : switch (code)
1636 : {
1637 5303 : case PLUS_EXPR:
1638 5303 : case MINUS_EXPR:
1639 5303 : real = const_binop (code, r1, r2);
1640 5303 : imag = const_binop (code, i1, i2);
1641 5303 : break;
1642 :
1643 3929 : case MULT_EXPR:
1644 3929 : if (COMPLEX_FLOAT_TYPE_P (type))
1645 2777 : return do_mpc_arg2 (arg1, arg2, type,
1646 : /* do_nonfinite= */ folding_initializer,
1647 2777 : mpc_mul);
1648 :
1649 1152 : real = const_binop (MINUS_EXPR,
1650 : const_binop (MULT_EXPR, r1, r2),
1651 : const_binop (MULT_EXPR, i1, i2));
1652 1152 : imag = const_binop (PLUS_EXPR,
1653 : const_binop (MULT_EXPR, r1, i2),
1654 : const_binop (MULT_EXPR, i1, r2));
1655 1152 : break;
1656 :
1657 1704 : case RDIV_EXPR:
1658 1704 : if (COMPLEX_FLOAT_TYPE_P (type))
1659 1704 : return do_mpc_arg2 (arg1, arg2, type,
1660 : /* do_nonfinite= */ folding_initializer,
1661 1704 : mpc_div);
1662 : /* Fallthru. */
1663 254 : case TRUNC_DIV_EXPR:
1664 254 : case CEIL_DIV_EXPR:
1665 254 : case FLOOR_DIV_EXPR:
1666 254 : case ROUND_DIV_EXPR:
1667 254 : if (flag_complex_method == 0)
1668 : {
1669 : /* Keep this algorithm in sync with
1670 : tree-complex.cc:expand_complex_div_straight().
1671 :
1672 : Expand complex division to scalars, straightforward algorithm.
1673 : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1674 : t = br*br + bi*bi
1675 : */
1676 0 : tree magsquared
1677 0 : = const_binop (PLUS_EXPR,
1678 : const_binop (MULT_EXPR, r2, r2),
1679 : const_binop (MULT_EXPR, i2, i2));
1680 0 : tree t1
1681 0 : = const_binop (PLUS_EXPR,
1682 : const_binop (MULT_EXPR, r1, r2),
1683 : const_binop (MULT_EXPR, i1, i2));
1684 0 : tree t2
1685 0 : = const_binop (MINUS_EXPR,
1686 : const_binop (MULT_EXPR, i1, r2),
1687 : const_binop (MULT_EXPR, r1, i2));
1688 :
1689 0 : real = const_binop (code, t1, magsquared);
1690 0 : imag = const_binop (code, t2, magsquared);
1691 : }
1692 : else
1693 : {
1694 : /* Keep this algorithm in sync with
1695 : tree-complex.cc:expand_complex_div_wide().
1696 :
1697 : Expand complex division to scalars, modified algorithm to minimize
1698 : overflow with wide input ranges. */
1699 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1700 : fold_abs_const (r2, TREE_TYPE (type)),
1701 : fold_abs_const (i2, TREE_TYPE (type)));
1702 :
1703 254 : if (integer_nonzerop (compare))
1704 : {
1705 : /* In the TRUE branch, we compute
1706 : ratio = br/bi;
1707 : div = (br * ratio) + bi;
1708 : tr = (ar * ratio) + ai;
1709 : ti = (ai * ratio) - ar;
1710 : tr = tr / div;
1711 : ti = ti / div; */
1712 48 : tree ratio = const_binop (code, r2, i2);
1713 48 : tree div = const_binop (PLUS_EXPR, i2,
1714 : const_binop (MULT_EXPR, r2, ratio));
1715 48 : real = const_binop (MULT_EXPR, r1, ratio);
1716 48 : real = const_binop (PLUS_EXPR, real, i1);
1717 48 : real = const_binop (code, real, div);
1718 :
1719 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1720 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1721 48 : imag = const_binop (code, imag, div);
1722 : }
1723 : else
1724 : {
1725 : /* In the FALSE branch, we compute
1726 : ratio = d/c;
1727 : divisor = (d * ratio) + c;
1728 : tr = (b * ratio) + a;
1729 : ti = b - (a * ratio);
1730 : tr = tr / div;
1731 : ti = ti / div; */
1732 206 : tree ratio = const_binop (code, i2, r2);
1733 206 : tree div = const_binop (PLUS_EXPR, r2,
1734 : const_binop (MULT_EXPR, i2, ratio));
1735 :
1736 206 : real = const_binop (MULT_EXPR, i1, ratio);
1737 206 : real = const_binop (PLUS_EXPR, real, r1);
1738 206 : real = const_binop (code, real, div);
1739 :
1740 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1741 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1742 206 : imag = const_binop (code, imag, div);
1743 : }
1744 : }
1745 : break;
1746 :
1747 : default:
1748 : return NULL_TREE;
1749 : }
1750 :
1751 6709 : if (real && imag)
1752 6551 : return build_complex (type, real, imag);
1753 : }
1754 :
1755 271124 : tree simplified;
1756 271124 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1757 : return simplified;
1758 :
1759 270625 : if (commutative_tree_code (code)
1760 270625 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1761 : return simplified;
1762 :
1763 265830 : return vector_const_binop (code, arg1, arg2, const_binop);
1764 : }
1765 :
1766 : /* Overload that adds a TYPE parameter to be able to dispatch
1767 : to fold_relational_const. */
1768 :
1769 : tree
1770 255410036 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1771 : {
1772 255410036 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1773 66978339 : return fold_relational_const (code, type, arg1, arg2);
1774 :
1775 : /* ??? Until we make the const_binop worker take the type of the
1776 : result as argument put those cases that need it here. */
1777 188431697 : switch (code)
1778 : {
1779 18 : case VEC_SERIES_EXPR:
1780 18 : if (CONSTANT_CLASS_P (arg1)
1781 18 : && CONSTANT_CLASS_P (arg2))
1782 18 : return build_vec_series (type, arg1, arg2);
1783 : return NULL_TREE;
1784 :
1785 268494 : case COMPLEX_EXPR:
1786 268494 : if ((TREE_CODE (arg1) == REAL_CST
1787 257973 : && TREE_CODE (arg2) == REAL_CST)
1788 10523 : || (TREE_CODE (arg1) == INTEGER_CST
1789 10521 : && TREE_CODE (arg2) == INTEGER_CST))
1790 268492 : return build_complex (type, arg1, arg2);
1791 : return NULL_TREE;
1792 :
1793 95350 : case POINTER_DIFF_EXPR:
1794 95350 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1795 : {
1796 189428 : poly_offset_int res = (wi::to_poly_offset (arg1)
1797 94714 : - wi::to_poly_offset (arg2));
1798 94714 : return force_fit_type (type, res, 1,
1799 94714 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1800 : }
1801 : return NULL_TREE;
1802 :
1803 14804 : case VEC_PACK_TRUNC_EXPR:
1804 14804 : case VEC_PACK_FIX_TRUNC_EXPR:
1805 14804 : case VEC_PACK_FLOAT_EXPR:
1806 14804 : {
1807 14804 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1808 :
1809 14804 : if (TREE_CODE (arg1) != VECTOR_CST
1810 14804 : || TREE_CODE (arg2) != VECTOR_CST)
1811 : return NULL_TREE;
1812 :
1813 14804 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1814 : return NULL_TREE;
1815 :
1816 14804 : out_nelts = in_nelts * 2;
1817 14804 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1818 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1819 :
1820 14804 : tree_vector_builder elts (type, out_nelts, 1);
1821 185640 : for (i = 0; i < out_nelts; i++)
1822 : {
1823 170848 : tree elt = (i < in_nelts
1824 170848 : ? VECTOR_CST_ELT (arg1, i)
1825 85418 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1826 171892 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1827 : ? NOP_EXPR
1828 : : code == VEC_PACK_FLOAT_EXPR
1829 1044 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1830 170848 : TREE_TYPE (type), elt);
1831 170848 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1832 12 : return NULL_TREE;
1833 170836 : elts.quick_push (elt);
1834 : }
1835 :
1836 14792 : return elts.build ();
1837 14804 : }
1838 :
1839 206 : case VEC_WIDEN_MULT_LO_EXPR:
1840 206 : case VEC_WIDEN_MULT_HI_EXPR:
1841 206 : case VEC_WIDEN_MULT_EVEN_EXPR:
1842 206 : case VEC_WIDEN_MULT_ODD_EXPR:
1843 206 : {
1844 206 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1845 :
1846 206 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1847 : return NULL_TREE;
1848 :
1849 206 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1850 : return NULL_TREE;
1851 206 : out_nelts = in_nelts / 2;
1852 206 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1853 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1854 :
1855 206 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1856 : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1857 : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1858 : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1859 : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1860 : scale = 1, ofs = 0;
1861 : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1862 206 : scale = 1, ofs = 1;
1863 :
1864 206 : tree_vector_builder elts (type, out_nelts, 1);
1865 738 : for (out = 0; out < out_nelts; out++)
1866 : {
1867 532 : unsigned int in = (out << scale) + ofs;
1868 532 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1869 : VECTOR_CST_ELT (arg1, in));
1870 532 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1871 : VECTOR_CST_ELT (arg2, in));
1872 :
1873 532 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1874 0 : return NULL_TREE;
1875 532 : tree elt = const_binop (MULT_EXPR, t1, t2);
1876 532 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1877 : return NULL_TREE;
1878 532 : elts.quick_push (elt);
1879 : }
1880 :
1881 206 : return elts.build ();
1882 206 : }
1883 :
1884 188052825 : default:;
1885 : }
1886 :
1887 188052825 : if (TREE_CODE_CLASS (code) != tcc_binary)
1888 : return NULL_TREE;
1889 :
1890 : /* Make sure type and arg0 have the same saturating flag. */
1891 185712488 : gcc_checking_assert (TYPE_SATURATING (type)
1892 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1893 :
1894 185712488 : return const_binop (code, arg1, arg2);
1895 : }
1896 :
1897 : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1898 : Return zero if computing the constants is not possible. */
1899 :
1900 : tree
1901 344987414 : const_unop (enum tree_code code, tree type, tree arg0)
1902 : {
1903 : /* Don't perform the operation, other than NEGATE and ABS, if
1904 : flag_signaling_nans is on and the operand is a signaling NaN. */
1905 344987414 : if (TREE_CODE (arg0) == REAL_CST
1906 10647858 : && HONOR_SNANS (arg0)
1907 17129 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1908 4740 : && code != NEGATE_EXPR
1909 4740 : && code != ABS_EXPR
1910 344992119 : && code != ABSU_EXPR)
1911 : return NULL_TREE;
1912 :
1913 344982709 : switch (code)
1914 : {
1915 256828478 : CASE_CONVERT:
1916 256828478 : case FLOAT_EXPR:
1917 256828478 : case FIX_TRUNC_EXPR:
1918 256828478 : case FIXED_CONVERT_EXPR:
1919 256828478 : return fold_convert_const (code, type, arg0);
1920 :
1921 0 : case ADDR_SPACE_CONVERT_EXPR:
1922 : /* If the source address is 0, and the source address space
1923 : cannot have a valid object at 0, fold to dest type null. */
1924 0 : if (integer_zerop (arg0)
1925 0 : && !(targetm.addr_space.zero_address_valid
1926 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1927 0 : return fold_convert_const (code, type, arg0);
1928 : break;
1929 :
1930 12417000 : case VIEW_CONVERT_EXPR:
1931 12417000 : return fold_view_convert_expr (type, arg0);
1932 :
1933 30885203 : case NEGATE_EXPR:
1934 30885203 : {
1935 : /* Can't call fold_negate_const directly here as that doesn't
1936 : handle all cases and we might not be able to negate some
1937 : constants. */
1938 30885203 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1939 30885203 : if (tem && CONSTANT_CLASS_P (tem))
1940 : return tem;
1941 : break;
1942 : }
1943 :
1944 36689 : case ABS_EXPR:
1945 36689 : case ABSU_EXPR:
1946 36689 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1947 34289 : return fold_abs_const (arg0, type);
1948 : break;
1949 :
1950 25512 : case CONJ_EXPR:
1951 25512 : if (TREE_CODE (arg0) == COMPLEX_CST)
1952 : {
1953 25509 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1954 25509 : TREE_TYPE (type));
1955 25509 : return build_complex (type, TREE_REALPART (arg0), ipart);
1956 : }
1957 : break;
1958 :
1959 2366608 : case BIT_NOT_EXPR:
1960 2366608 : if (TREE_CODE (arg0) == INTEGER_CST)
1961 2363566 : return fold_not_const (arg0, type);
1962 3042 : else if (POLY_INT_CST_P (arg0))
1963 : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1964 : /* Perform BIT_NOT_EXPR on each element individually. */
1965 3042 : else if (TREE_CODE (arg0) == VECTOR_CST)
1966 : {
1967 2418 : tree elem;
1968 :
1969 : /* This can cope with stepped encodings because ~x == -1 - x. */
1970 2418 : tree_vector_builder elements;
1971 2418 : elements.new_unary_operation (type, arg0, true);
1972 2418 : unsigned int i, count = elements.encoded_nelts ();
1973 9092 : for (i = 0; i < count; ++i)
1974 : {
1975 6674 : elem = VECTOR_CST_ELT (arg0, i);
1976 6674 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1977 6674 : if (elem == NULL_TREE)
1978 : break;
1979 6674 : elements.quick_push (elem);
1980 : }
1981 2418 : if (i == count)
1982 2418 : return elements.build ();
1983 2418 : }
1984 : break;
1985 :
1986 8777853 : case TRUTH_NOT_EXPR:
1987 8777853 : if (TREE_CODE (arg0) == INTEGER_CST)
1988 8533740 : return constant_boolean_node (integer_zerop (arg0), type);
1989 : break;
1990 :
1991 179083 : case REALPART_EXPR:
1992 179083 : if (TREE_CODE (arg0) == COMPLEX_CST)
1993 178882 : return fold_convert (type, TREE_REALPART (arg0));
1994 : break;
1995 :
1996 184881 : case IMAGPART_EXPR:
1997 184881 : if (TREE_CODE (arg0) == COMPLEX_CST)
1998 184693 : return fold_convert (type, TREE_IMAGPART (arg0));
1999 : break;
2000 :
2001 18984 : case VEC_UNPACK_LO_EXPR:
2002 18984 : case VEC_UNPACK_HI_EXPR:
2003 18984 : case VEC_UNPACK_FLOAT_LO_EXPR:
2004 18984 : case VEC_UNPACK_FLOAT_HI_EXPR:
2005 18984 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2006 18984 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2007 18984 : {
2008 18984 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2009 18984 : enum tree_code subcode;
2010 :
2011 18984 : if (TREE_CODE (arg0) != VECTOR_CST)
2012 : return NULL_TREE;
2013 :
2014 18984 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2015 : return NULL_TREE;
2016 18984 : out_nelts = in_nelts / 2;
2017 18984 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2018 :
2019 18984 : unsigned int offset = 0;
2020 18984 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2021 18984 : || code == VEC_UNPACK_FLOAT_LO_EXPR
2022 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2023 9484 : offset = out_nelts;
2024 :
2025 18984 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
2026 : subcode = NOP_EXPR;
2027 7830 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
2028 7830 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
2029 : subcode = FLOAT_EXPR;
2030 : else
2031 4 : subcode = FIX_TRUNC_EXPR;
2032 :
2033 18984 : tree_vector_builder elts (type, out_nelts, 1);
2034 101210 : for (i = 0; i < out_nelts; i++)
2035 : {
2036 82226 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2037 82226 : VECTOR_CST_ELT (arg0, i + offset));
2038 82226 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2039 0 : return NULL_TREE;
2040 82226 : elts.quick_push (elt);
2041 : }
2042 :
2043 18984 : return elts.build ();
2044 18984 : }
2045 :
2046 4 : case VEC_DUPLICATE_EXPR:
2047 4 : if (CONSTANT_CLASS_P (arg0))
2048 4 : return build_vector_from_val (type, arg0);
2049 : return NULL_TREE;
2050 :
2051 : default:
2052 : break;
2053 : }
2054 :
2055 : return NULL_TREE;
2056 : }
2057 :
2058 : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2059 : indicates which particular sizetype to create. */
2060 :
2061 : tree
2062 3457985165 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2063 : {
2064 3457985165 : return build_int_cst (sizetype_tab[(int) kind], number);
2065 : }
2066 :
2067 : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2068 : is a tree code. The type of the result is taken from the operands.
2069 : Both must be equivalent integer types, ala int_binop_types_match_p.
2070 : If the operands are constant, so is the result. */
2071 :
2072 : tree
2073 2501726253 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2074 : {
2075 2501726253 : tree type = TREE_TYPE (arg0);
2076 :
2077 2501726253 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2078 : return error_mark_node;
2079 :
2080 2501726253 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2081 : TREE_TYPE (arg1)));
2082 :
2083 : /* Handle the special case of two poly_int constants faster. */
2084 2501726253 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2085 : {
2086 : /* And some specific cases even faster than that. */
2087 2469029278 : if (code == PLUS_EXPR)
2088 : {
2089 1124095300 : if (integer_zerop (arg0)
2090 1124095300 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2091 : return arg1;
2092 294369015 : if (integer_zerop (arg1)
2093 294369015 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2094 : return arg0;
2095 : }
2096 1344933978 : else if (code == MINUS_EXPR)
2097 : {
2098 116734357 : if (integer_zerop (arg1)
2099 116734357 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2100 : return arg0;
2101 : }
2102 1228199621 : else if (code == MULT_EXPR)
2103 : {
2104 571479263 : if (integer_onep (arg0)
2105 571479263 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2106 : return arg1;
2107 : }
2108 :
2109 : /* Handle general case of two integer constants. For sizetype
2110 : constant calculations we always want to know about overflow,
2111 : even in the unsigned case. */
2112 1203200175 : tree res = int_const_binop (code, arg0, arg1, -1);
2113 1203200175 : if (res != NULL_TREE)
2114 : return res;
2115 : }
2116 :
2117 32696975 : return fold_build2_loc (loc, code, type, arg0, arg1);
2118 : }
2119 :
2120 : /* Given two values, either both of sizetype or both of bitsizetype,
2121 : compute the difference between the two values. Return the value
2122 : in signed type corresponding to the type of the operands. */
2123 :
2124 : tree
2125 37075342 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2126 : {
2127 37075342 : tree type = TREE_TYPE (arg0);
2128 37075342 : tree ctype;
2129 :
2130 37075342 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2131 : TREE_TYPE (arg1)));
2132 :
2133 : /* If the type is already signed, just do the simple thing. */
2134 37075342 : if (!TYPE_UNSIGNED (type))
2135 10065654 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2136 :
2137 27009688 : if (type == sizetype)
2138 27009688 : ctype = ssizetype;
2139 0 : else if (type == bitsizetype)
2140 0 : ctype = sbitsizetype;
2141 : else
2142 0 : ctype = signed_type_for (type);
2143 :
2144 : /* If either operand is not a constant, do the conversions to the signed
2145 : type and subtract. The hardware will do the right thing with any
2146 : overflow in the subtraction. */
2147 27009688 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2148 17568 : return size_binop_loc (loc, MINUS_EXPR,
2149 : fold_convert_loc (loc, ctype, arg0),
2150 17568 : fold_convert_loc (loc, ctype, arg1));
2151 :
2152 : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2153 : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2154 : overflow) and negate (which can't either). Special-case a result
2155 : of zero while we're here. */
2156 26992120 : if (tree_int_cst_equal (arg0, arg1))
2157 23754512 : return build_int_cst (ctype, 0);
2158 3237608 : else if (tree_int_cst_lt (arg1, arg0))
2159 2134867 : return fold_convert_loc (loc, ctype,
2160 2134867 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2161 : else
2162 1102741 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2163 : fold_convert_loc (loc, ctype,
2164 : size_binop_loc (loc,
2165 : MINUS_EXPR,
2166 : arg1, arg0)));
2167 : }
2168 :
2169 : /* Convert integer constant ARG1 to TYPE, which is an integral or offset
2170 : or pointer type. */
2171 :
2172 : tree
2173 1348824137 : int_const_convert (tree type, const_tree arg1, int overflowable)
2174 : {
2175 : /* Given an integer constant, make new constant with new type,
2176 : appropriately sign-extended or truncated. Use widest_int
2177 : so that any extension is done according ARG1's type. */
2178 1348824137 : tree arg1_type = TREE_TYPE (arg1);
2179 1348824137 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2180 1348824137 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2181 1348824137 : TYPE_SIGN (arg1_type)),
2182 : overflowable,
2183 1348824137 : TREE_OVERFLOW (arg1));
2184 : }
2185 :
2186 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2187 : to an integer type. */
2188 :
2189 : static tree
2190 54226 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2191 : {
2192 54226 : bool overflow = false;
2193 54226 : tree t;
2194 :
2195 : /* The following code implements the floating point to integer
2196 : conversion rules required by the Java Language Specification,
2197 : that IEEE NaNs are mapped to zero and values that overflow
2198 : the target precision saturate, i.e. values greater than
2199 : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2200 : are mapped to INT_MIN. These semantics are allowed by the
2201 : C and C++ standards that simply state that the behavior of
2202 : FP-to-integer conversion is unspecified upon overflow. */
2203 :
2204 54226 : wide_int val;
2205 54226 : REAL_VALUE_TYPE r;
2206 54226 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2207 :
2208 54226 : switch (code)
2209 : {
2210 54226 : case FIX_TRUNC_EXPR:
2211 54226 : real_trunc (&r, VOIDmode, &x);
2212 54226 : break;
2213 :
2214 0 : default:
2215 0 : gcc_unreachable ();
2216 : }
2217 :
2218 : /* If R is NaN, return zero and show we have an overflow. */
2219 54226 : if (REAL_VALUE_ISNAN (r))
2220 : {
2221 3638 : overflow = true;
2222 3638 : val = wi::zero (TYPE_PRECISION (type));
2223 : }
2224 :
2225 : /* See if R is less than the lower bound or greater than the
2226 : upper bound. */
2227 :
2228 54226 : if (! overflow)
2229 : {
2230 50588 : tree lt = TYPE_MIN_VALUE (type);
2231 50588 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2232 50588 : if (real_less (&r, &l))
2233 : {
2234 1974 : overflow = true;
2235 1974 : val = wi::to_wide (lt);
2236 : }
2237 : }
2238 :
2239 54226 : if (! overflow)
2240 : {
2241 48614 : tree ut = TYPE_MAX_VALUE (type);
2242 48614 : if (ut)
2243 : {
2244 48614 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2245 48614 : if (real_less (&u, &r))
2246 : {
2247 1921 : overflow = true;
2248 1921 : val = wi::to_wide (ut);
2249 : }
2250 : }
2251 : }
2252 :
2253 54226 : if (! overflow)
2254 46695 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2255 :
2256 : /* According to IEEE standard, for conversions from floating point to
2257 : integer. When a NaN or infinite operand cannot be represented in the
2258 : destination format and this cannot otherwise be indicated, the invalid
2259 : operation exception shall be signaled. When a numeric operand would
2260 : convert to an integer outside the range of the destination format, the
2261 : invalid operation exception shall be signaled if this situation cannot
2262 : otherwise be indicated. */
2263 54226 : if (!flag_trapping_math || !overflow)
2264 46949 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2265 : else
2266 : t = NULL_TREE;
2267 :
2268 54226 : return t;
2269 54226 : }
2270 :
2271 : /* A subroutine of fold_convert_const handling conversions of a
2272 : FIXED_CST to an integer type. */
2273 :
2274 : static tree
2275 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2276 : {
2277 0 : tree t;
2278 0 : double_int temp, temp_trunc;
2279 0 : scalar_mode mode;
2280 :
2281 : /* Right shift FIXED_CST to temp by fbit. */
2282 0 : temp = TREE_FIXED_CST (arg1).data;
2283 0 : mode = TREE_FIXED_CST (arg1).mode;
2284 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2285 : {
2286 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2287 : HOST_BITS_PER_DOUBLE_INT,
2288 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2289 :
2290 : /* Left shift temp to temp_trunc by fbit. */
2291 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2292 : HOST_BITS_PER_DOUBLE_INT,
2293 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2294 : }
2295 : else
2296 : {
2297 0 : temp = double_int_zero;
2298 0 : temp_trunc = double_int_zero;
2299 : }
2300 :
2301 : /* If FIXED_CST is negative, we need to round the value toward 0.
2302 : By checking if the fractional bits are not zero to add 1 to temp. */
2303 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2304 0 : && temp_trunc.is_negative ()
2305 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2306 0 : temp += double_int_one;
2307 :
2308 : /* Given a fixed-point constant, make new constant with new type,
2309 : appropriately sign-extended or truncated. */
2310 0 : t = force_fit_type (type, temp, -1,
2311 0 : (temp.is_negative ()
2312 0 : && (TYPE_UNSIGNED (type)
2313 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2314 0 : | TREE_OVERFLOW (arg1));
2315 :
2316 0 : return t;
2317 : }
2318 :
2319 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2320 : to another floating point type. */
2321 :
2322 : static tree
2323 2105706 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2324 : {
2325 2105706 : REAL_VALUE_TYPE value;
2326 2105706 : tree t;
2327 :
2328 : /* If the underlying modes are the same, simply treat it as
2329 : copy and rebuild with TREE_REAL_CST information and the
2330 : given type. */
2331 2105706 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2332 : {
2333 98274 : t = build_real (type, TREE_REAL_CST (arg1));
2334 98274 : return t;
2335 : }
2336 :
2337 : /* Don't perform the operation if flag_signaling_nans is on
2338 : and the operand is a signaling NaN. */
2339 2007432 : if (HONOR_SNANS (arg1)
2340 2009314 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2341 : return NULL_TREE;
2342 :
2343 : /* With flag_rounding_math we should respect the current rounding mode
2344 : unless the conversion is exact. */
2345 2007432 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2346 2008088 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2347 509 : return NULL_TREE;
2348 :
2349 2006923 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2350 2006923 : t = build_real (type, value);
2351 :
2352 : /* If converting an infinity or NAN to a representation that doesn't
2353 : have one, set the overflow bit so that we can produce some kind of
2354 : error message at the appropriate point if necessary. It's not the
2355 : most user-friendly message, but it's better than nothing. */
2356 2006923 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2357 2134983 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2358 0 : TREE_OVERFLOW (t) = 1;
2359 2006923 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2360 2130741 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2361 0 : TREE_OVERFLOW (t) = 1;
2362 : /* Regular overflow, conversion produced an infinity in a mode that
2363 : can't represent them. */
2364 10031289 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2365 0 : && REAL_VALUE_ISINF (value)
2366 2006923 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2367 0 : TREE_OVERFLOW (t) = 1;
2368 : else
2369 2006923 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2370 : return t;
2371 : }
2372 :
2373 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2374 : to a floating point type. */
2375 :
2376 : static tree
2377 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2378 : {
2379 0 : REAL_VALUE_TYPE value;
2380 0 : tree t;
2381 :
2382 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2383 0 : &TREE_FIXED_CST (arg1));
2384 0 : t = build_real (type, value);
2385 :
2386 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2387 0 : return t;
2388 : }
2389 :
2390 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2391 : to another fixed-point type. */
2392 :
2393 : static tree
2394 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2395 : {
2396 0 : FIXED_VALUE_TYPE value;
2397 0 : tree t;
2398 0 : bool overflow_p;
2399 :
2400 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2401 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2402 0 : t = build_fixed (type, value);
2403 :
2404 : /* Propagate overflow flags. */
2405 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2406 0 : TREE_OVERFLOW (t) = 1;
2407 0 : return t;
2408 : }
2409 :
2410 : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2411 : to a fixed-point type. */
2412 :
2413 : static tree
2414 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2415 : {
2416 0 : FIXED_VALUE_TYPE value;
2417 0 : tree t;
2418 0 : bool overflow_p;
2419 0 : double_int di;
2420 :
2421 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2422 :
2423 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2424 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2425 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2426 : else
2427 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2428 :
2429 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2430 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2431 0 : TYPE_SATURATING (type));
2432 0 : t = build_fixed (type, value);
2433 :
2434 : /* Propagate overflow flags. */
2435 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2436 0 : TREE_OVERFLOW (t) = 1;
2437 0 : return t;
2438 : }
2439 :
2440 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2441 : to a fixed-point type. */
2442 :
2443 : static tree
2444 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2445 : {
2446 0 : FIXED_VALUE_TYPE value;
2447 0 : tree t;
2448 0 : bool overflow_p;
2449 :
2450 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2451 0 : &TREE_REAL_CST (arg1),
2452 0 : TYPE_SATURATING (type));
2453 0 : t = build_fixed (type, value);
2454 :
2455 : /* Propagate overflow flags. */
2456 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2457 0 : TREE_OVERFLOW (t) = 1;
2458 0 : return t;
2459 : }
2460 :
2461 : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2462 : type TYPE. If no simplification can be done return NULL_TREE. */
2463 :
2464 : static tree
2465 1408865844 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2466 : {
2467 1408865844 : tree arg_type = TREE_TYPE (arg1);
2468 1408865844 : if (arg_type == type)
2469 : return arg1;
2470 :
2471 : /* We can't widen types, since the runtime value could overflow the
2472 : original type before being extended to the new type. */
2473 1399634384 : if (POLY_INT_CST_P (arg1)
2474 : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2475 : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2476 : return build_poly_int_cst (type,
2477 : poly_wide_int::from (poly_int_cst_value (arg1),
2478 : TYPE_PRECISION (type),
2479 : TYPE_SIGN (arg_type)));
2480 :
2481 1399634384 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2482 : || TREE_CODE (type) == OFFSET_TYPE)
2483 : {
2484 1369461224 : if (TREE_CODE (arg1) == INTEGER_CST)
2485 1348824137 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2486 20637087 : else if (TREE_CODE (arg1) == REAL_CST)
2487 54226 : return fold_convert_const_int_from_real (code, type, arg1);
2488 20582861 : else if (TREE_CODE (arg1) == FIXED_CST)
2489 0 : return fold_convert_const_int_from_fixed (type, arg1);
2490 : }
2491 : else if (SCALAR_FLOAT_TYPE_P (type))
2492 : {
2493 30117942 : if (TREE_CODE (arg1) == INTEGER_CST)
2494 : {
2495 23251600 : tree res = build_real_from_int_cst (type, arg1);
2496 : /* Avoid the folding if flag_rounding_math is on and the
2497 : conversion is not exact. */
2498 23251600 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2499 : {
2500 2902 : bool fail = false;
2501 5804 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2502 2902 : TYPE_PRECISION (TREE_TYPE (arg1)));
2503 2902 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2504 1743 : return NULL_TREE;
2505 2902 : }
2506 23249857 : return res;
2507 : }
2508 6866342 : else if (TREE_CODE (arg1) == REAL_CST)
2509 2105706 : return fold_convert_const_real_from_real (type, arg1);
2510 4760636 : else if (TREE_CODE (arg1) == FIXED_CST)
2511 0 : return fold_convert_const_real_from_fixed (type, arg1);
2512 : }
2513 : else if (FIXED_POINT_TYPE_P (type))
2514 : {
2515 0 : if (TREE_CODE (arg1) == FIXED_CST)
2516 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2517 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2518 0 : return fold_convert_const_fixed_from_int (type, arg1);
2519 0 : else if (TREE_CODE (arg1) == REAL_CST)
2520 0 : return fold_convert_const_fixed_from_real (type, arg1);
2521 : }
2522 : else if (VECTOR_TYPE_P (type))
2523 : {
2524 4627 : if (TREE_CODE (arg1) == VECTOR_CST
2525 4627 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2526 : {
2527 4627 : tree elttype = TREE_TYPE (type);
2528 4627 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2529 : /* We can't handle steps directly when extending, since the
2530 : values need to wrap at the original precision first. */
2531 4627 : bool step_ok_p
2532 4627 : = (INTEGRAL_TYPE_P (elttype)
2533 291 : && INTEGRAL_TYPE_P (arg1_elttype)
2534 4860 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2535 4627 : tree_vector_builder v;
2536 4627 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2537 : return NULL_TREE;
2538 4627 : unsigned int len = v.encoded_nelts ();
2539 27602 : for (unsigned int i = 0; i < len; ++i)
2540 : {
2541 22975 : tree elt = VECTOR_CST_ELT (arg1, i);
2542 22975 : tree cvt = fold_convert_const (code, elttype, elt);
2543 22975 : if (cvt == NULL_TREE)
2544 0 : return NULL_TREE;
2545 22975 : v.quick_push (cvt);
2546 : }
2547 4627 : return v.build ();
2548 4627 : }
2549 : }
2550 11399 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2551 11399 : return build_zero_cst (type);
2552 : return NULL_TREE;
2553 : }
2554 :
2555 : /* Construct a vector of zero elements of vector type TYPE. */
2556 :
2557 : static tree
2558 17215 : build_zero_vector (tree type)
2559 : {
2560 17215 : tree t;
2561 :
2562 17215 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2563 17215 : return build_vector_from_val (type, t);
2564 : }
2565 :
2566 : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2567 :
2568 : bool
2569 4372 : fold_convertible_p (const_tree type, const_tree arg)
2570 : {
2571 4372 : const_tree orig = TREE_TYPE (arg);
2572 :
2573 4372 : if (type == orig)
2574 : return true;
2575 :
2576 4372 : if (TREE_CODE (arg) == ERROR_MARK
2577 4372 : || TREE_CODE (type) == ERROR_MARK
2578 4372 : || TREE_CODE (orig) == ERROR_MARK)
2579 : return false;
2580 :
2581 4372 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2582 : return true;
2583 :
2584 4372 : switch (TREE_CODE (type))
2585 : {
2586 3796 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2587 3796 : case POINTER_TYPE: case REFERENCE_TYPE:
2588 3796 : case OFFSET_TYPE:
2589 3796 : return (INTEGRAL_TYPE_P (orig)
2590 383 : || (POINTER_TYPE_P (orig)
2591 248 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2592 3931 : || TREE_CODE (orig) == OFFSET_TYPE);
2593 :
2594 130 : case REAL_TYPE:
2595 130 : case FIXED_POINT_TYPE:
2596 130 : case VOID_TYPE:
2597 130 : return TREE_CODE (type) == TREE_CODE (orig);
2598 :
2599 209 : case VECTOR_TYPE:
2600 209 : return (VECTOR_TYPE_P (orig)
2601 322 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2602 : TYPE_VECTOR_SUBPARTS (orig))
2603 226 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2604 :
2605 : default:
2606 : return false;
2607 : }
2608 : }
2609 :
2610 : /* Convert expression ARG to type TYPE. Used by the middle-end for
2611 : simple conversions in preference to calling the front-end's convert. */
2612 :
2613 : tree
2614 2074878506 : fold_convert_loc (location_t loc, tree type, tree arg)
2615 : {
2616 2074878506 : tree orig = TREE_TYPE (arg);
2617 2074878506 : tree tem;
2618 :
2619 2074878506 : if (type == orig)
2620 : return arg;
2621 :
2622 1381409813 : if (TREE_CODE (arg) == ERROR_MARK
2623 1381408791 : || TREE_CODE (type) == ERROR_MARK
2624 1381408790 : || TREE_CODE (orig) == ERROR_MARK)
2625 1023 : return error_mark_node;
2626 :
2627 1381408790 : switch (TREE_CODE (type))
2628 : {
2629 78318699 : case POINTER_TYPE:
2630 78318699 : case REFERENCE_TYPE:
2631 : /* Handle conversions between pointers to different address spaces. */
2632 78318699 : if (POINTER_TYPE_P (orig)
2633 78318699 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2634 63880392 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2635 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2636 : /* fall through */
2637 :
2638 1349900502 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2639 1349900502 : case OFFSET_TYPE: case BITINT_TYPE:
2640 1349900502 : if (TREE_CODE (arg) == INTEGER_CST)
2641 : {
2642 1151567202 : tem = fold_convert_const (NOP_EXPR, type, arg);
2643 1151567202 : if (tem != NULL_TREE)
2644 : return tem;
2645 : }
2646 198333300 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2647 2421 : || TREE_CODE (orig) == OFFSET_TYPE)
2648 198333300 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2649 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2650 0 : return fold_convert_loc (loc, type,
2651 : fold_build1_loc (loc, REALPART_EXPR,
2652 0 : TREE_TYPE (orig), arg));
2653 0 : gcc_assert (VECTOR_TYPE_P (orig)
2654 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2655 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2656 :
2657 543938 : case REAL_TYPE:
2658 543938 : if (TREE_CODE (arg) == INTEGER_CST)
2659 : {
2660 57872 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2661 57872 : if (tem != NULL_TREE)
2662 : return tem;
2663 : }
2664 486066 : else if (TREE_CODE (arg) == REAL_CST)
2665 : {
2666 117964 : tem = fold_convert_const (NOP_EXPR, type, arg);
2667 117964 : if (tem != NULL_TREE)
2668 : return tem;
2669 : }
2670 368102 : else if (TREE_CODE (arg) == FIXED_CST)
2671 : {
2672 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2673 0 : if (tem != NULL_TREE)
2674 : return tem;
2675 : }
2676 :
2677 368104 : switch (TREE_CODE (orig))
2678 : {
2679 651 : case INTEGER_TYPE: case BITINT_TYPE:
2680 651 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2681 651 : case POINTER_TYPE: case REFERENCE_TYPE:
2682 651 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2683 :
2684 367453 : case REAL_TYPE:
2685 367453 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2686 :
2687 0 : case FIXED_POINT_TYPE:
2688 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2689 :
2690 0 : case COMPLEX_TYPE:
2691 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2692 0 : return fold_convert_loc (loc, type, tem);
2693 :
2694 0 : default:
2695 0 : gcc_unreachable ();
2696 : }
2697 :
2698 0 : case FIXED_POINT_TYPE:
2699 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2700 0 : || TREE_CODE (arg) == REAL_CST)
2701 : {
2702 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2703 0 : if (tem != NULL_TREE)
2704 0 : goto fold_convert_exit;
2705 : }
2706 :
2707 0 : switch (TREE_CODE (orig))
2708 : {
2709 0 : case FIXED_POINT_TYPE:
2710 0 : case INTEGER_TYPE:
2711 0 : case ENUMERAL_TYPE:
2712 0 : case BOOLEAN_TYPE:
2713 0 : case REAL_TYPE:
2714 0 : case BITINT_TYPE:
2715 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2716 :
2717 0 : case COMPLEX_TYPE:
2718 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2719 0 : return fold_convert_loc (loc, type, tem);
2720 :
2721 0 : default:
2722 0 : gcc_unreachable ();
2723 : }
2724 :
2725 2263 : case COMPLEX_TYPE:
2726 2263 : switch (TREE_CODE (orig))
2727 : {
2728 584 : case INTEGER_TYPE: case BITINT_TYPE:
2729 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2730 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2731 584 : case REAL_TYPE:
2732 584 : case FIXED_POINT_TYPE:
2733 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2734 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2735 584 : fold_convert_loc (loc, TREE_TYPE (type),
2736 584 : integer_zero_node));
2737 1679 : case COMPLEX_TYPE:
2738 1679 : {
2739 1679 : tree rpart, ipart;
2740 :
2741 1679 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2742 : {
2743 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2744 1534 : TREE_OPERAND (arg, 0));
2745 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2746 1534 : TREE_OPERAND (arg, 1));
2747 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2748 : }
2749 :
2750 145 : arg = save_expr (arg);
2751 145 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2752 145 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2753 145 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2754 145 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2755 145 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2756 : }
2757 :
2758 0 : default:
2759 0 : gcc_unreachable ();
2760 : }
2761 :
2762 30848002 : case VECTOR_TYPE:
2763 30848002 : if (integer_zerop (arg))
2764 17215 : return build_zero_vector (type);
2765 30830787 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2766 30830787 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2767 : || VECTOR_TYPE_P (orig));
2768 30830787 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2769 :
2770 110190 : case VOID_TYPE:
2771 110190 : tem = fold_ignored_result (arg);
2772 110190 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2773 :
2774 63 : case NULLPTR_TYPE:
2775 63 : if (integer_zerop (arg))
2776 17 : return build_zero_cst (type);
2777 : /* FALLTHRU */
2778 3754 : default:
2779 3754 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2780 3754 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2781 0 : gcc_unreachable ();
2782 : }
2783 0 : fold_convert_exit:
2784 0 : tem = protected_set_expr_location_unshare (tem, loc);
2785 0 : return tem;
2786 : }
2787 :
2788 : /* Return false if expr can be assumed not to be an lvalue, true
2789 : otherwise. */
2790 :
2791 : static bool
2792 50400997 : maybe_lvalue_p (const_tree x)
2793 : {
2794 : /* We only need to wrap lvalue tree codes. */
2795 50400997 : switch (TREE_CODE (x))
2796 : {
2797 : case VAR_DECL:
2798 : case PARM_DECL:
2799 : case RESULT_DECL:
2800 : case LABEL_DECL:
2801 : case FUNCTION_DECL:
2802 : case SSA_NAME:
2803 : case COMPOUND_LITERAL_EXPR:
2804 :
2805 : case COMPONENT_REF:
2806 : case MEM_REF:
2807 : case INDIRECT_REF:
2808 : case ARRAY_REF:
2809 : case ARRAY_RANGE_REF:
2810 : case BIT_FIELD_REF:
2811 : case OBJ_TYPE_REF:
2812 :
2813 : case REALPART_EXPR:
2814 : case IMAGPART_EXPR:
2815 : case PREINCREMENT_EXPR:
2816 : case PREDECREMENT_EXPR:
2817 : case SAVE_EXPR:
2818 : case TRY_CATCH_EXPR:
2819 : case WITH_CLEANUP_EXPR:
2820 : case COMPOUND_EXPR:
2821 : case MODIFY_EXPR:
2822 : case TARGET_EXPR:
2823 : case COND_EXPR:
2824 : case BIND_EXPR:
2825 : case VIEW_CONVERT_EXPR:
2826 : break;
2827 :
2828 36756276 : default:
2829 : /* Assume the worst for front-end tree codes. */
2830 36756276 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2831 : break;
2832 : return false;
2833 : }
2834 :
2835 13668187 : return true;
2836 : }
2837 :
2838 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2839 :
2840 : tree
2841 45450211 : non_lvalue_loc (location_t loc, tree x)
2842 : {
2843 : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2844 : us. */
2845 45450211 : if (in_gimple_form)
2846 : return x;
2847 :
2848 9636837 : if (! maybe_lvalue_p (x))
2849 : return x;
2850 2386566 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2851 : }
2852 :
2853 : /* Given a tree comparison code, return the code that is the logical inverse.
2854 : It is generally not safe to do this for floating-point comparisons, except
2855 : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2856 : ERROR_MARK in this case. */
2857 :
2858 : enum tree_code
2859 120967775 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2860 : {
2861 120967775 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2862 990492 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2863 : return ERROR_MARK;
2864 :
2865 120216988 : switch (code)
2866 : {
2867 : case EQ_EXPR:
2868 : return NE_EXPR;
2869 52833130 : case NE_EXPR:
2870 52833130 : return EQ_EXPR;
2871 11805101 : case GT_EXPR:
2872 11805101 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2873 15053932 : case GE_EXPR:
2874 15053932 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2875 7517337 : case LT_EXPR:
2876 7517337 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2877 7586302 : case LE_EXPR:
2878 7586302 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2879 254 : case LTGT_EXPR:
2880 254 : return UNEQ_EXPR;
2881 291 : case UNEQ_EXPR:
2882 291 : return LTGT_EXPR;
2883 : case UNGT_EXPR:
2884 : return LE_EXPR;
2885 : case UNGE_EXPR:
2886 : return LT_EXPR;
2887 : case UNLT_EXPR:
2888 : return GE_EXPR;
2889 : case UNLE_EXPR:
2890 : return GT_EXPR;
2891 211645 : case ORDERED_EXPR:
2892 211645 : return UNORDERED_EXPR;
2893 56199 : case UNORDERED_EXPR:
2894 56199 : return ORDERED_EXPR;
2895 0 : default:
2896 0 : gcc_unreachable ();
2897 : }
2898 : }
2899 :
2900 : /* Similar, but return the comparison that results if the operands are
2901 : swapped. This is safe for floating-point. */
2902 :
2903 : enum tree_code
2904 154425671 : swap_tree_comparison (enum tree_code code)
2905 : {
2906 154425671 : switch (code)
2907 : {
2908 : case EQ_EXPR:
2909 : case NE_EXPR:
2910 : case ORDERED_EXPR:
2911 : case UNORDERED_EXPR:
2912 : case LTGT_EXPR:
2913 : case UNEQ_EXPR:
2914 : return code;
2915 37174246 : case GT_EXPR:
2916 37174246 : return LT_EXPR;
2917 10824735 : case GE_EXPR:
2918 10824735 : return LE_EXPR;
2919 21547162 : case LT_EXPR:
2920 21547162 : return GT_EXPR;
2921 15881761 : case LE_EXPR:
2922 15881761 : return GE_EXPR;
2923 232397 : case UNGT_EXPR:
2924 232397 : return UNLT_EXPR;
2925 19410 : case UNGE_EXPR:
2926 19410 : return UNLE_EXPR;
2927 349294 : case UNLT_EXPR:
2928 349294 : return UNGT_EXPR;
2929 106989 : case UNLE_EXPR:
2930 106989 : return UNGE_EXPR;
2931 0 : default:
2932 0 : gcc_unreachable ();
2933 : }
2934 : }
2935 :
2936 :
2937 : /* Convert a comparison tree code from an enum tree_code representation
2938 : into a compcode bit-based encoding. This function is the inverse of
2939 : compcode_to_comparison. */
2940 :
2941 : static enum comparison_code
2942 56632 : comparison_to_compcode (enum tree_code code)
2943 : {
2944 56632 : switch (code)
2945 : {
2946 : case LT_EXPR:
2947 : return COMPCODE_LT;
2948 : case EQ_EXPR:
2949 : return COMPCODE_EQ;
2950 : case LE_EXPR:
2951 : return COMPCODE_LE;
2952 : case GT_EXPR:
2953 : return COMPCODE_GT;
2954 : case NE_EXPR:
2955 : return COMPCODE_NE;
2956 : case GE_EXPR:
2957 : return COMPCODE_GE;
2958 : case ORDERED_EXPR:
2959 : return COMPCODE_ORD;
2960 : case UNORDERED_EXPR:
2961 : return COMPCODE_UNORD;
2962 : case UNLT_EXPR:
2963 : return COMPCODE_UNLT;
2964 : case UNEQ_EXPR:
2965 : return COMPCODE_UNEQ;
2966 : case UNLE_EXPR:
2967 : return COMPCODE_UNLE;
2968 : case UNGT_EXPR:
2969 : return COMPCODE_UNGT;
2970 : case LTGT_EXPR:
2971 : return COMPCODE_LTGT;
2972 : case UNGE_EXPR:
2973 : return COMPCODE_UNGE;
2974 0 : default:
2975 0 : gcc_unreachable ();
2976 : }
2977 : }
2978 :
2979 : /* Convert a compcode bit-based encoding of a comparison operator back
2980 : to GCC's enum tree_code representation. This function is the
2981 : inverse of comparison_to_compcode. */
2982 :
2983 : static enum tree_code
2984 14234 : compcode_to_comparison (enum comparison_code code)
2985 : {
2986 14234 : switch (code)
2987 : {
2988 : case COMPCODE_LT:
2989 : return LT_EXPR;
2990 : case COMPCODE_EQ:
2991 : return EQ_EXPR;
2992 : case COMPCODE_LE:
2993 : return LE_EXPR;
2994 : case COMPCODE_GT:
2995 : return GT_EXPR;
2996 : case COMPCODE_NE:
2997 : return NE_EXPR;
2998 : case COMPCODE_GE:
2999 : return GE_EXPR;
3000 : case COMPCODE_ORD:
3001 : return ORDERED_EXPR;
3002 : case COMPCODE_UNORD:
3003 : return UNORDERED_EXPR;
3004 : case COMPCODE_UNLT:
3005 : return UNLT_EXPR;
3006 : case COMPCODE_UNEQ:
3007 : return UNEQ_EXPR;
3008 : case COMPCODE_UNLE:
3009 : return UNLE_EXPR;
3010 : case COMPCODE_UNGT:
3011 : return UNGT_EXPR;
3012 : case COMPCODE_LTGT:
3013 : return LTGT_EXPR;
3014 : case COMPCODE_UNGE:
3015 : return UNGE_EXPR;
3016 0 : default:
3017 0 : gcc_unreachable ();
3018 : }
3019 : }
3020 :
3021 : /* Return true if COND1 tests the opposite condition of COND2. */
3022 :
3023 : bool
3024 1690897 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3025 : {
3026 1690897 : return (COMPARISON_CLASS_P (cond1)
3027 1600265 : && COMPARISON_CLASS_P (cond2)
3028 1589215 : && (invert_tree_comparison
3029 1589215 : (TREE_CODE (cond1),
3030 3178430 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3031 68195 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3032 68195 : TREE_OPERAND (cond2, 0), 0)
3033 1712725 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3034 21828 : TREE_OPERAND (cond2, 1), 0));
3035 : }
3036 :
3037 : /* Return a tree for the comparison which is the combination of
3038 : doing the AND or OR (depending on CODE) of the two operations LCODE
3039 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3040 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3041 : if this makes the transformation invalid. */
3042 :
3043 : tree
3044 28316 : combine_comparisons (location_t loc,
3045 : enum tree_code code, enum tree_code lcode,
3046 : enum tree_code rcode, tree truth_type,
3047 : tree ll_arg, tree lr_arg)
3048 : {
3049 28316 : bool honor_nans = HONOR_NANS (ll_arg);
3050 28316 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3051 28316 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3052 28316 : int compcode;
3053 :
3054 28316 : switch (code)
3055 : {
3056 18772 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3057 18772 : compcode = lcompcode & rcompcode;
3058 18772 : break;
3059 :
3060 9544 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3061 9544 : compcode = lcompcode | rcompcode;
3062 9544 : break;
3063 :
3064 : default:
3065 : return NULL_TREE;
3066 : }
3067 :
3068 28316 : if (!honor_nans)
3069 : {
3070 : /* Eliminate unordered comparisons, as well as LTGT and ORD
3071 : which are not used unless the mode has NaNs. */
3072 23420 : compcode &= ~COMPCODE_UNORD;
3073 23420 : if (compcode == COMPCODE_LTGT)
3074 : compcode = COMPCODE_NE;
3075 22260 : else if (compcode == COMPCODE_ORD)
3076 : compcode = COMPCODE_TRUE;
3077 : }
3078 4896 : else if (flag_trapping_math)
3079 : {
3080 : /* Check that the original operation and the optimized ones will trap
3081 : under the same condition. */
3082 8212 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3083 3496 : && (lcompcode != COMPCODE_EQ)
3084 4106 : && (lcompcode != COMPCODE_ORD);
3085 8212 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3086 3630 : && (rcompcode != COMPCODE_EQ)
3087 4106 : && (rcompcode != COMPCODE_ORD);
3088 8212 : bool trap = (compcode & COMPCODE_UNORD) == 0
3089 3729 : && (compcode != COMPCODE_EQ)
3090 4106 : && (compcode != COMPCODE_ORD);
3091 :
3092 : /* In a short-circuited boolean expression the LHS might be
3093 : such that the RHS, if evaluated, will never trap. For
3094 : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3095 : if neither x nor y is NaN. (This is a mixed blessing: for
3096 : example, the expression above will never trap, hence
3097 : optimizing it to x < y would be invalid). */
3098 4106 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3099 3749 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3100 4106 : rtrap = false;
3101 :
3102 : /* If the comparison was short-circuited, and only the RHS
3103 : trapped, we may now generate a spurious trap. */
3104 4106 : if (rtrap && !ltrap
3105 102 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3106 : return NULL_TREE;
3107 :
3108 : /* If we changed the conditions that cause a trap, we lose. */
3109 4004 : if ((ltrap || rtrap) != trap)
3110 : return NULL_TREE;
3111 : }
3112 :
3113 1447 : if (compcode == COMPCODE_TRUE)
3114 1244 : return constant_boolean_node (true, truth_type);
3115 23623 : else if (compcode == COMPCODE_FALSE)
3116 9389 : return constant_boolean_node (false, truth_type);
3117 : else
3118 : {
3119 14234 : enum tree_code tcode;
3120 :
3121 14234 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3122 14234 : return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3123 : }
3124 : }
3125 :
3126 : /* Return nonzero if two operands (typically of the same tree node)
3127 : are necessarily equal. FLAGS modifies behavior as follows:
3128 :
3129 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3130 : This function tests whether the operands are indistinguishable;
3131 : it does not test whether they are equal using C's == operation.
3132 : The distinction is important for IEEE floating point, because
3133 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3134 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3135 :
3136 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3137 : even though it may hold multiple values during a function.
3138 : This is because a GCC tree node guarantees that nothing else is
3139 : executed between the evaluation of its "operands" (which may often
3140 : be evaluated in arbitrary order). Hence if the operands themselves
3141 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3142 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3143 : unset means assuming isochronic (or instantaneous) tree equivalence.
3144 : Unless comparing arbitrary expression trees, such as from different
3145 : statements, this flag can usually be left unset.
3146 :
3147 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3148 : are considered the same. It is used when the caller has other ways
3149 : to ensure that global memory is unchanged in between.
3150 :
3151 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3152 : not values of expressions.
3153 :
3154 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3155 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3156 :
3157 : If OEP_BITWISE is set, then require the values to be bitwise identical
3158 : rather than simply numerically equal. Do not take advantage of things
3159 : like math-related flags or undefined behavior; only return true for
3160 : values that are provably bitwise identical in all circumstances.
3161 :
3162 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3163 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3164 : rather than just numerically equivalent. The compared expressions must
3165 : however perform the same operations but may do intermediate computations in
3166 : differing signs. Because this comparison ignores any possible UB it cannot
3167 : be used blindly without ensuring that the context you are using it in itself
3168 : doesn't guarantee that there will be no UB. Conditional expressions are
3169 : excluded from this relaxation.
3170 :
3171 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3172 : differing hashes even for cases where operand_compare::operand_equal_p
3173 : compares equal.
3174 :
3175 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3176 : any operand with side effect. This is unnecesarily conservative in the
3177 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3178 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3179 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3180 : even if var is volatile. */
3181 :
3182 : bool
3183 7049251906 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3184 : unsigned int flags)
3185 : {
3186 7049251906 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3187 : }
3188 :
3189 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3190 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3191 : expression is being compared under for equality. This means that they can
3192 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3193 :
3194 : bool
3195 7049985290 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3196 : tree type1, const_tree arg1,
3197 : unsigned int flags)
3198 : {
3199 7049985290 : bool r;
3200 7049985290 : if (verify_hash_value (arg0, arg1, flags, &r))
3201 2955719104 : return r;
3202 :
3203 4094266186 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3204 4094266186 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3205 :
3206 : /* If either is ERROR_MARK, they aren't equal. */
3207 4094266186 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3208 4094265570 : || type0 == error_mark_node
3209 4094265568 : || type1 == error_mark_node)
3210 : return false;
3211 :
3212 : /* Similar, if either does not have a type (like a template id),
3213 : they aren't equal. */
3214 4094265567 : if (!type0 || !type1)
3215 : return false;
3216 :
3217 : /* Bitwise identity makes no sense if the values have different layouts. */
3218 4094260420 : if ((flags & OEP_BITWISE)
3219 4094260420 : && !tree_nop_conversion_p (type0, type1))
3220 : return false;
3221 :
3222 : /* We cannot consider pointers to different address space equal. */
3223 4094260420 : if (POINTER_TYPE_P (type0)
3224 620635079 : && POINTER_TYPE_P (type1)
3225 4624037558 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3226 529777138 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3227 : return false;
3228 :
3229 : /* Check equality of integer constants before bailing out due to
3230 : precision differences. */
3231 4094260185 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3232 : {
3233 : /* Address of INTEGER_CST is not defined; check that we did not forget
3234 : to drop the OEP_ADDRESS_OF flags. */
3235 645623728 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3236 645623728 : return tree_int_cst_equal (arg0, arg1);
3237 : }
3238 :
3239 3448636457 : if ((flags & OEP_ASSUME_WRAPV)
3240 2080751 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3241 : {
3242 788476 : const_tree t_arg0 = arg0;
3243 788476 : const_tree t_arg1 = arg1;
3244 788476 : STRIP_NOPS (arg0);
3245 788476 : STRIP_NOPS (arg1);
3246 : /* Only recurse if the conversion was one that was valid to strip. */
3247 788476 : if (t_arg0 != arg0 || t_arg1 != arg1)
3248 733384 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3249 : }
3250 :
3251 3447903073 : if (!(flags & OEP_ADDRESS_OF))
3252 : {
3253 : /* Check if we are checking an operation where the two's compliment
3254 : bitwise representation of the result is not the same between signed and
3255 : unsigned arithmetic. */
3256 3050709194 : bool enforce_signedness = true;
3257 3050709194 : if (flags & OEP_ASSUME_WRAPV)
3258 : {
3259 1254877 : switch (TREE_CODE (arg0))
3260 : {
3261 : case PLUS_EXPR:
3262 : case MINUS_EXPR:
3263 : case MULT_EXPR:
3264 : case BIT_IOR_EXPR:
3265 : case BIT_XOR_EXPR:
3266 : case BIT_AND_EXPR:
3267 : case BIT_NOT_EXPR:
3268 : case ABS_EXPR:
3269 : CASE_CONVERT:
3270 : case SSA_NAME:
3271 : case INTEGER_CST:
3272 : case VAR_DECL:
3273 : case PARM_DECL:
3274 : case RESULT_DECL:
3275 3050709194 : enforce_signedness = false;
3276 : break;
3277 :
3278 : default:
3279 : break;
3280 : }
3281 : }
3282 :
3283 : /* If both types don't have the same signedness, then we can't consider
3284 : them equal. We must check this before the STRIP_NOPS calls
3285 : because they may change the signedness of the arguments. As pointers
3286 : strictly don't have a signedness, require either two pointers or
3287 : two non-pointers as well. */
3288 3050709194 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3289 3050709194 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3290 140287469 : && enforce_signedness))
3291 : return false;
3292 :
3293 : /* If both types don't have the same precision, then it is not safe
3294 : to strip NOPs. */
3295 2759118880 : if (element_precision (type0) != element_precision (type1))
3296 : return false;
3297 :
3298 2611867857 : STRIP_NOPS (arg0);
3299 2611867857 : STRIP_NOPS (arg1);
3300 :
3301 2611867857 : type0 = TREE_TYPE (arg0);
3302 2611867857 : type1 = TREE_TYPE (arg1);
3303 : }
3304 : #if 0
3305 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3306 : sanity check once the issue is solved. */
3307 : else
3308 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3309 : are not defined. Check that we did not forget to drop the
3310 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3311 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3312 : && TREE_CODE (arg0) != SSA_NAME);
3313 : #endif
3314 :
3315 : /* In case both args are comparisons but with different comparison
3316 : code, try to swap the comparison operands of one arg to produce
3317 : a match and compare that variant. */
3318 3009061736 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3319 1206494163 : && COMPARISON_CLASS_P (arg0)
3320 6592129 : && COMPARISON_CLASS_P (arg1))
3321 : {
3322 4898934 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3323 :
3324 4898934 : if (TREE_CODE (arg0) == swap_code)
3325 2078599 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3326 2078599 : TREE_OPERAND (arg1, 1), flags)
3327 2098228 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3328 19629 : TREE_OPERAND (arg1, 0), flags);
3329 : }
3330 :
3331 3006983137 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3332 : {
3333 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3334 1204415564 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3335 : ;
3336 1204354866 : else if (flags & OEP_ADDRESS_OF)
3337 : {
3338 : /* If we are interested in comparing addresses ignore
3339 : MEM_REF wrappings of the base that can appear just for
3340 : TBAA reasons. */
3341 48474232 : if (TREE_CODE (arg0) == MEM_REF
3342 7674781 : && DECL_P (arg1)
3343 5379500 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3344 1120822 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3345 49014486 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3346 : return true;
3347 48255771 : else if (TREE_CODE (arg1) == MEM_REF
3348 30420406 : && DECL_P (arg0)
3349 10918382 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3350 2142288 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3351 48789289 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3352 : return true;
3353 47887240 : return false;
3354 : }
3355 : else
3356 : return false;
3357 : }
3358 :
3359 : /* When not checking adddresses, this is needed for conversions and for
3360 : COMPONENT_REF. Might as well play it safe and always test this. */
3361 1802628271 : if (TREE_CODE (type0) == ERROR_MARK
3362 1802628271 : || TREE_CODE (type1) == ERROR_MARK
3363 3605256542 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3364 25337307 : && !(flags & OEP_ADDRESS_OF)))
3365 3768809 : return false;
3366 :
3367 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3368 : We don't care about side effects in that case because the SAVE_EXPR
3369 : takes care of that for us. In all other cases, two expressions are
3370 : equal if they have no side effects. If we have two identical
3371 : expressions with side effects that should be treated the same due
3372 : to the only side effects being identical SAVE_EXPR's, that will
3373 : be detected in the recursive calls below.
3374 : If we are taking an invariant address of two identical objects
3375 : they are necessarily equal as well. */
3376 322505754 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3377 2121365038 : && (TREE_CODE (arg0) == SAVE_EXPR
3378 322480243 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3379 284502052 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3380 : return true;
3381 :
3382 : /* Next handle constant cases, those for which we can return 1 even
3383 : if ONLY_CONST is set. */
3384 1476495077 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3385 22564080 : switch (TREE_CODE (arg0))
3386 : {
3387 151 : case INTEGER_CST:
3388 151 : return tree_int_cst_equal (arg0, arg1);
3389 :
3390 0 : case FIXED_CST:
3391 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3392 : TREE_FIXED_CST (arg1));
3393 :
3394 3583266 : case REAL_CST:
3395 3583266 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3396 : return true;
3397 :
3398 2567167 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3399 : {
3400 : /* If we do not distinguish between signed and unsigned zero,
3401 : consider them equal. */
3402 14203 : if (real_zerop (arg0) && real_zerop (arg1))
3403 : return true;
3404 : }
3405 2567157 : return false;
3406 :
3407 880970 : case VECTOR_CST:
3408 880970 : {
3409 880970 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3410 880970 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3411 : return false;
3412 :
3413 859987 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3414 859987 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3415 : return false;
3416 :
3417 826729 : unsigned int count = vector_cst_encoded_nelts (arg0);
3418 1181606 : for (unsigned int i = 0; i < count; ++i)
3419 1892612 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3420 946306 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3421 : return false;
3422 : return true;
3423 : }
3424 :
3425 13847 : case COMPLEX_CST:
3426 13847 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3427 : flags)
3428 13847 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3429 : flags));
3430 :
3431 930094 : case STRING_CST:
3432 930094 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3433 930094 : && ! memcmp (TREE_STRING_POINTER (arg0),
3434 517325 : TREE_STRING_POINTER (arg1),
3435 517325 : TREE_STRING_LENGTH (arg0)));
3436 :
3437 0 : case RAW_DATA_CST:
3438 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3439 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3440 0 : RAW_DATA_POINTER (arg1),
3441 0 : RAW_DATA_LENGTH (arg0)));
3442 :
3443 16037163 : case ADDR_EXPR:
3444 16037163 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3445 16037163 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3446 : flags | OEP_ADDRESS_OF
3447 16037163 : | OEP_MATCH_SIDE_EFFECTS);
3448 178849 : case CONSTRUCTOR:
3449 178849 : {
3450 : /* In GIMPLE empty constructors are allowed in initializers of
3451 : aggregates. */
3452 178849 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3453 : return true;
3454 :
3455 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3456 138139 : if (TREE_CODE (type0) != TREE_CODE (type1))
3457 : return false;
3458 138139 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3459 : {
3460 : /* For arrays, check that the sizes all match. */
3461 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3462 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3463 264 : || siz0 < 0
3464 528 : || siz0 != int_size_in_bytes (type1))
3465 0 : return false;
3466 : }
3467 137875 : else if (!types_compatible_p (type0, type1))
3468 : return false;
3469 :
3470 138139 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3471 138139 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3472 414417 : if (vec_safe_length (v0) != vec_safe_length (v1))
3473 : return false;
3474 :
3475 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3476 : of the CONSTRUCTOR referenced indirectly. */
3477 138139 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3478 :
3479 357710112 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3480 : {
3481 206325 : constructor_elt *c0 = &(*v0)[idx];
3482 206325 : constructor_elt *c1 = &(*v1)[idx];
3483 :
3484 : /* Check that the values are the same... */
3485 206325 : if (c0->value != c1->value
3486 206325 : && !operand_equal_p (c0->value, c1->value, flags))
3487 : return false;
3488 :
3489 : /* ... and that they apply to the same field! */
3490 117761 : if (c0->index != c1->index
3491 117761 : && (TREE_CODE (type0) == ARRAY_TYPE
3492 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3493 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3494 0 : DECL_FIELD_OFFSET (c1->index),
3495 : flags)
3496 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3497 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3498 : flags)))
3499 0 : return false;
3500 : }
3501 :
3502 : return true;
3503 : }
3504 :
3505 : default:
3506 : break;
3507 : }
3508 :
3509 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3510 : two instances of undefined behavior will give identical results. */
3511 1454870737 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3512 : return false;
3513 :
3514 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3515 : variant that allows null and views null as being different from any
3516 : non-null value. In the latter case, if either is null, the both
3517 : must be; otherwise, do the normal comparison. */
3518 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3519 : TREE_OPERAND (arg1, N), flags)
3520 :
3521 : #define OP_SAME_WITH_NULL(N) \
3522 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3523 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3524 :
3525 1454870737 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3526 : {
3527 7550543 : case tcc_unary:
3528 : /* Two conversions are equal only if signedness and modes match. */
3529 7550543 : switch (TREE_CODE (arg0))
3530 : {
3531 7174111 : CASE_CONVERT:
3532 7174111 : case FIX_TRUNC_EXPR:
3533 7174111 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3534 : return false;
3535 : break;
3536 : default:
3537 : break;
3538 : }
3539 :
3540 7550522 : return OP_SAME_WITH_NULL (0);
3541 :
3542 :
3543 22409859 : case tcc_comparison:
3544 22409859 : case tcc_binary:
3545 22409859 : if (OP_SAME (0) && OP_SAME (1))
3546 : return true;
3547 :
3548 : /* For commutative ops, allow the other order. */
3549 16410686 : return (commutative_tree_code (TREE_CODE (arg0))
3550 12425626 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3551 12425626 : TREE_OPERAND (arg1, 1), flags)
3552 16626504 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3553 215818 : TREE_OPERAND (arg1, 0), flags));
3554 :
3555 872307015 : case tcc_reference:
3556 : /* If either of the pointer (or reference) expressions we are
3557 : dereferencing contain a side effect, these cannot be equal,
3558 : but their addresses can be. */
3559 872307015 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3560 872307015 : && (TREE_SIDE_EFFECTS (arg0)
3561 805825585 : || TREE_SIDE_EFFECTS (arg1)))
3562 : return false;
3563 :
3564 871936087 : switch (TREE_CODE (arg0))
3565 : {
3566 5011523 : case INDIRECT_REF:
3567 5011523 : if (!(flags & OEP_ADDRESS_OF))
3568 : {
3569 4989704 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3570 : return false;
3571 : /* Verify that the access types are compatible. */
3572 4983698 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3573 : return false;
3574 : }
3575 4941715 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3576 4941715 : return OP_SAME (0);
3577 :
3578 654070 : case IMAGPART_EXPR:
3579 : /* Require the same offset. */
3580 654070 : if (!operand_equal_p (TYPE_SIZE (type0),
3581 654070 : TYPE_SIZE (type1),
3582 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3583 : return false;
3584 :
3585 : /* Fallthru. */
3586 2438946 : case REALPART_EXPR:
3587 2438946 : case VIEW_CONVERT_EXPR:
3588 2438946 : return OP_SAME (0);
3589 :
3590 83462446 : case TARGET_MEM_REF:
3591 83462446 : case MEM_REF:
3592 83462446 : if (!(flags & OEP_ADDRESS_OF))
3593 : {
3594 : /* Require equal access sizes */
3595 16347728 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3596 16347728 : && (!TYPE_SIZE (type0)
3597 1120989 : || !TYPE_SIZE (type1)
3598 1115641 : || !operand_equal_p (TYPE_SIZE (type0),
3599 1115641 : TYPE_SIZE (type1),
3600 : flags)))
3601 1117626 : return false;
3602 : /* Verify that access happens in similar types. */
3603 15230102 : if (!types_compatible_p (type0, type1))
3604 : return false;
3605 : /* Verify that accesses are TBAA compatible. */
3606 14900405 : if (!alias_ptr_types_compatible_p
3607 14900405 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3608 14900405 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3609 14012907 : || (MR_DEPENDENCE_CLIQUE (arg0)
3610 14012907 : != MR_DEPENDENCE_CLIQUE (arg1))
3611 27225376 : || (MR_DEPENDENCE_BASE (arg0)
3612 12324971 : != MR_DEPENDENCE_BASE (arg1)))
3613 : return false;
3614 : /* Verify that alignment is compatible. */
3615 11808906 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3616 : return false;
3617 : }
3618 78729276 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3619 134002965 : return (OP_SAME (0) && OP_SAME (1)
3620 : /* TARGET_MEM_REF require equal extra operands. */
3621 102974838 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3622 600856 : || (OP_SAME_WITH_NULL (2)
3623 272137 : && OP_SAME_WITH_NULL (3)
3624 266383 : && OP_SAME_WITH_NULL (4))));
3625 :
3626 35171873 : case ARRAY_REF:
3627 35171873 : case ARRAY_RANGE_REF:
3628 35171873 : if (!OP_SAME (0))
3629 : return false;
3630 30406067 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3631 : /* Compare the array index by value if it is constant first as we
3632 : may have different types but same value here. */
3633 30406067 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3634 30406067 : TREE_OPERAND (arg1, 1))
3635 27408632 : || OP_SAME (1))
3636 6026604 : && OP_SAME_WITH_NULL (2)
3637 6025280 : && OP_SAME_WITH_NULL (3)
3638 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3639 : we have to account for the offset of the ref. */
3640 39444649 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3641 3012640 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3642 2682 : || (operand_equal_p (array_ref_low_bound
3643 2682 : (const_cast<tree> (arg0)),
3644 : array_ref_low_bound
3645 2682 : (const_cast<tree> (arg1)),
3646 : flags)
3647 2682 : && operand_equal_p (array_ref_element_size
3648 2682 : (const_cast<tree> (arg0)),
3649 : array_ref_element_size
3650 2682 : (const_cast<tree> (arg1)),
3651 : flags))));
3652 :
3653 745234179 : case COMPONENT_REF:
3654 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3655 : may be NULL when we're called to compare MEM_EXPRs. */
3656 745234179 : if (!OP_SAME_WITH_NULL (0))
3657 : return false;
3658 57074182 : {
3659 57074182 : bool compare_address = flags & OEP_ADDRESS_OF;
3660 :
3661 : /* Most of time we only need to compare FIELD_DECLs for equality.
3662 : However when determining address look into actual offsets.
3663 : These may match for unions and unshared record types. */
3664 57074182 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3665 57074182 : if (!OP_SAME (1))
3666 : {
3667 33277948 : if (compare_address
3668 616355 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3669 : {
3670 616352 : tree field0 = TREE_OPERAND (arg0, 1);
3671 616352 : tree field1 = TREE_OPERAND (arg1, 1);
3672 :
3673 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3674 616352 : if (TREE_CODE (field0) != FIELD_DECL
3675 616352 : || TREE_CODE (field1) != FIELD_DECL)
3676 : return false;
3677 :
3678 616352 : if (!DECL_FIELD_OFFSET (field0)
3679 616352 : || !DECL_FIELD_OFFSET (field1))
3680 3 : return field0 == field1;
3681 :
3682 616349 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3683 616349 : DECL_FIELD_OFFSET (field1), flags)
3684 804148 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3685 187799 : DECL_FIELD_BIT_OFFSET (field1),
3686 : flags))
3687 576118 : return false;
3688 : }
3689 : else
3690 : return false;
3691 : }
3692 : }
3693 23836465 : return OP_SAME_WITH_NULL (2);
3694 :
3695 617072 : case BIT_FIELD_REF:
3696 617072 : if (!OP_SAME (0))
3697 : return false;
3698 360937 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3699 360937 : return OP_SAME (1) && OP_SAME (2);
3700 :
3701 : default:
3702 : return false;
3703 : }
3704 :
3705 57539468 : case tcc_expression:
3706 57539468 : switch (TREE_CODE (arg0))
3707 : {
3708 52250807 : case ADDR_EXPR:
3709 : /* Be sure we pass right ADDRESS_OF flag. */
3710 52250807 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3711 52250807 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3712 52250807 : TREE_OPERAND (arg1, 0),
3713 52250807 : flags | OEP_ADDRESS_OF);
3714 :
3715 571363 : case TRUTH_NOT_EXPR:
3716 571363 : return OP_SAME (0);
3717 :
3718 74953 : case TRUTH_ANDIF_EXPR:
3719 74953 : case TRUTH_ORIF_EXPR:
3720 74953 : return OP_SAME (0) && OP_SAME (1);
3721 :
3722 0 : case WIDEN_MULT_PLUS_EXPR:
3723 0 : case WIDEN_MULT_MINUS_EXPR:
3724 0 : if (!OP_SAME (2))
3725 : return false;
3726 : /* The multiplcation operands are commutative. */
3727 : /* FALLTHRU */
3728 :
3729 46393 : case TRUTH_AND_EXPR:
3730 46393 : case TRUTH_OR_EXPR:
3731 46393 : case TRUTH_XOR_EXPR:
3732 46393 : if (OP_SAME (0) && OP_SAME (1))
3733 : return true;
3734 :
3735 : /* Otherwise take into account this is a commutative operation. */
3736 46375 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3737 46375 : TREE_OPERAND (arg1, 1), flags)
3738 46378 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3739 3 : TREE_OPERAND (arg1, 0), flags));
3740 :
3741 202163 : case COND_EXPR:
3742 202163 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3743 43326 : return false;
3744 158837 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3745 158837 : return OP_SAME (0);
3746 :
3747 4 : case BIT_INSERT_EXPR:
3748 : /* BIT_INSERT_EXPR has an implict operand as the type precision
3749 : of op1. Need to check to make sure they are the same. */
3750 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3751 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3752 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3753 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3754 : return false;
3755 : /* FALLTHRU */
3756 :
3757 279 : case VEC_COND_EXPR:
3758 279 : case DOT_PROD_EXPR:
3759 279 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3760 :
3761 37488 : case MODIFY_EXPR:
3762 37488 : case INIT_EXPR:
3763 37488 : case COMPOUND_EXPR:
3764 37488 : case PREDECREMENT_EXPR:
3765 37488 : case PREINCREMENT_EXPR:
3766 37488 : case POSTDECREMENT_EXPR:
3767 37488 : case POSTINCREMENT_EXPR:
3768 37488 : if (flags & OEP_LEXICOGRAPHIC)
3769 165 : return OP_SAME (0) && OP_SAME (1);
3770 : return false;
3771 :
3772 304737 : case CLEANUP_POINT_EXPR:
3773 304737 : case EXPR_STMT:
3774 304737 : case SAVE_EXPR:
3775 304737 : if (flags & OEP_LEXICOGRAPHIC)
3776 208 : return OP_SAME (0);
3777 : return false;
3778 :
3779 78189 : case OBJ_TYPE_REF:
3780 : /* Virtual table reference. */
3781 156378 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3782 78189 : OBJ_TYPE_REF_EXPR (arg1), flags))
3783 : return false;
3784 14649 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3785 14649 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3786 14649 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3787 : return false;
3788 14649 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3789 14649 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3790 : return false;
3791 14649 : if (virtual_method_call_p (arg0))
3792 : {
3793 14649 : if (!virtual_method_call_p (arg1))
3794 : return false;
3795 14649 : return types_same_for_odr (obj_type_ref_class (arg0),
3796 29298 : obj_type_ref_class (arg1));
3797 : }
3798 : return false;
3799 :
3800 598 : case OMP_ARRAY_SECTION:
3801 598 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3802 :
3803 : default:
3804 : return false;
3805 : }
3806 :
3807 3651208 : case tcc_vl_exp:
3808 3651208 : switch (TREE_CODE (arg0))
3809 : {
3810 3651208 : case CALL_EXPR:
3811 3651208 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3812 3651208 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3813 : /* If not both CALL_EXPRs are either internal or normal function
3814 : functions, then they are not equal. */
3815 : return false;
3816 3651208 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3817 : {
3818 : /* If the CALL_EXPRs call different internal functions, then they
3819 : are not equal. */
3820 2 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3821 : return false;
3822 : }
3823 : else
3824 : {
3825 : /* If the CALL_EXPRs call different functions, then they are not
3826 : equal. */
3827 3651206 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3828 : flags))
3829 : return false;
3830 : }
3831 :
3832 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3833 2139334 : {
3834 2139334 : unsigned int cef = call_expr_flags (arg0);
3835 2139334 : if (flags & OEP_PURE_SAME)
3836 0 : cef &= ECF_CONST | ECF_PURE;
3837 : else
3838 2139334 : cef &= ECF_CONST;
3839 2139334 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3840 : return false;
3841 : }
3842 :
3843 : /* Now see if all the arguments are the same. */
3844 33557 : {
3845 33557 : const_call_expr_arg_iterator iter0, iter1;
3846 33557 : const_tree a0, a1;
3847 67114 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3848 33557 : a1 = first_const_call_expr_arg (arg1, &iter1);
3849 41670 : a0 && a1;
3850 8113 : a0 = next_const_call_expr_arg (&iter0),
3851 8113 : a1 = next_const_call_expr_arg (&iter1))
3852 35052 : if (! operand_equal_p (a0, a1, flags))
3853 : return false;
3854 :
3855 : /* If we get here and both argument lists are exhausted
3856 : then the CALL_EXPRs are equal. */
3857 6618 : return ! (a0 || a1);
3858 : }
3859 : default:
3860 : return false;
3861 : }
3862 :
3863 167143620 : case tcc_declaration:
3864 : /* Consider __builtin_sqrt equal to sqrt. */
3865 167143620 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3866 6768409 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3867 287801 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3868 6158099 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3869 287801 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3870 :
3871 160985521 : if (DECL_P (arg0)
3872 160985521 : && (flags & OEP_DECL_NAME)
3873 35 : && (flags & OEP_LEXICOGRAPHIC))
3874 : {
3875 : /* Consider decls with the same name equal. The caller needs
3876 : to make sure they refer to the same entity (such as a function
3877 : formal parameter). */
3878 35 : tree a0name = DECL_NAME (arg0);
3879 35 : tree a1name = DECL_NAME (arg1);
3880 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3881 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3882 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3883 : }
3884 : return false;
3885 :
3886 321777752 : case tcc_exceptional:
3887 321777752 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3888 : {
3889 19468 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3890 : return false;
3891 :
3892 : /* In GIMPLE constructors are used only to build vectors from
3893 : elements. Individual elements in the constructor must be
3894 : indexed in increasing order and form an initial sequence.
3895 :
3896 : We make no effort to compare nonconstant ones in GENERIC. */
3897 19468 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3898 : return false;
3899 :
3900 : /* Be sure that vectors constructed have the same representation.
3901 : We only tested element precision and modes to match.
3902 : Vectors may be BLKmode and thus also check that the number of
3903 : parts match. */
3904 753 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3905 1506 : TYPE_VECTOR_SUBPARTS (type1)))
3906 : return false;
3907 :
3908 753 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3909 753 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3910 753 : unsigned int len = vec_safe_length (v0);
3911 :
3912 1506 : if (len != vec_safe_length (v1))
3913 : return false;
3914 :
3915 3890 : for (unsigned int i = 0; i < len; i++)
3916 : {
3917 3400 : constructor_elt *c0 = &(*v0)[i];
3918 3400 : constructor_elt *c1 = &(*v1)[i];
3919 :
3920 3400 : if (!operand_equal_p (c0->value, c1->value, flags)
3921 : /* In GIMPLE the indexes can be either NULL or matching i.
3922 : Double check this so we won't get false
3923 : positives for GENERIC. */
3924 3152 : || (c0->index
3925 2588 : && (TREE_CODE (c0->index) != INTEGER_CST
3926 2588 : || compare_tree_int (c0->index, i)))
3927 6552 : || (c1->index
3928 2588 : && (TREE_CODE (c1->index) != INTEGER_CST
3929 2588 : || compare_tree_int (c1->index, i))))
3930 248 : return false;
3931 : }
3932 : return true;
3933 : }
3934 321758284 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3935 3182 : && (flags & OEP_LEXICOGRAPHIC))
3936 : {
3937 : /* Compare the STATEMENT_LISTs. */
3938 16 : tree_stmt_iterator tsi1, tsi2;
3939 16 : tree body1 = const_cast<tree> (arg0);
3940 16 : tree body2 = const_cast<tree> (arg1);
3941 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3942 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3943 : {
3944 : /* The lists don't have the same number of statements. */
3945 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3946 : return false;
3947 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3948 : return true;
3949 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3950 : flags & (OEP_LEXICOGRAPHIC
3951 : | OEP_NO_HASH_CHECK)))
3952 : return false;
3953 : }
3954 : }
3955 : return false;
3956 :
3957 2491086 : case tcc_statement:
3958 2491086 : switch (TREE_CODE (arg0))
3959 : {
3960 52 : case RETURN_EXPR:
3961 52 : if (flags & OEP_LEXICOGRAPHIC)
3962 52 : return OP_SAME_WITH_NULL (0);
3963 : return false;
3964 4 : case DEBUG_BEGIN_STMT:
3965 4 : if (flags & OEP_LEXICOGRAPHIC)
3966 : return true;
3967 : return false;
3968 : default:
3969 : return false;
3970 : }
3971 :
3972 : default:
3973 : return false;
3974 : }
3975 :
3976 : #undef OP_SAME
3977 : #undef OP_SAME_WITH_NULL
3978 : }
3979 :
3980 : /* Generate a hash value for an expression. This can be used iteratively
3981 : by passing a previous result as the HSTATE argument. */
3982 :
3983 : void
3984 3164215990 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3985 : unsigned int flags)
3986 : {
3987 3164215990 : int i;
3988 3164215990 : enum tree_code code;
3989 3164215990 : enum tree_code_class tclass;
3990 :
3991 3164215990 : if (t == NULL_TREE || t == error_mark_node)
3992 : {
3993 76636911 : hstate.merge_hash (0);
3994 76636911 : return;
3995 : }
3996 :
3997 3087579079 : STRIP_ANY_LOCATION_WRAPPER (t);
3998 :
3999 3087579079 : if (!(flags & OEP_ADDRESS_OF))
4000 2838366433 : STRIP_NOPS (t);
4001 :
4002 3087579079 : code = TREE_CODE (t);
4003 :
4004 3087579079 : switch (code)
4005 : {
4006 : /* Alas, constants aren't shared, so we can't rely on pointer
4007 : identity. */
4008 828 : case VOID_CST:
4009 828 : hstate.merge_hash (0);
4010 828 : return;
4011 862981538 : case INTEGER_CST:
4012 862981538 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4013 1743845672 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4014 880864134 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4015 : return;
4016 15530626 : case REAL_CST:
4017 15530626 : {
4018 15530626 : unsigned int val2;
4019 15530626 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4020 : val2 = rvc_zero;
4021 : else
4022 15317795 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4023 15530626 : hstate.merge_hash (val2);
4024 15530626 : return;
4025 : }
4026 0 : case FIXED_CST:
4027 0 : {
4028 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4029 0 : hstate.merge_hash (val2);
4030 0 : return;
4031 : }
4032 11263222 : case STRING_CST:
4033 11263222 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4034 11263222 : TREE_STRING_LENGTH (t));
4035 11263222 : return;
4036 209 : case RAW_DATA_CST:
4037 209 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4038 209 : RAW_DATA_LENGTH (t));
4039 209 : return;
4040 210171 : case COMPLEX_CST:
4041 210171 : hash_operand (TREE_REALPART (t), hstate, flags);
4042 210171 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4043 210171 : return;
4044 3188671 : case VECTOR_CST:
4045 3188671 : {
4046 3188671 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4047 3188671 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4048 3188671 : unsigned int count = vector_cst_encoded_nelts (t);
4049 9999330 : for (unsigned int i = 0; i < count; ++i)
4050 6810659 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4051 : return;
4052 : }
4053 873608626 : case SSA_NAME:
4054 : /* We can just compare by pointer. */
4055 873608626 : hstate.add_hwi (SSA_NAME_VERSION (t));
4056 873608626 : return;
4057 : case PLACEHOLDER_EXPR:
4058 : /* The node itself doesn't matter. */
4059 : return;
4060 : case BLOCK:
4061 : case OMP_CLAUSE:
4062 : case OMP_NEXT_VARIANT:
4063 : case OMP_TARGET_DEVICE_MATCHES:
4064 : /* Ignore. */
4065 : return;
4066 : case TREE_LIST:
4067 : /* A list of expressions, for a CALL_EXPR or as the elements of a
4068 : VECTOR_CST. */
4069 288564 : for (; t; t = TREE_CHAIN (t))
4070 144282 : hash_operand (TREE_VALUE (t), hstate, flags);
4071 : return;
4072 4877630 : case CONSTRUCTOR:
4073 4877630 : {
4074 4877630 : unsigned HOST_WIDE_INT idx;
4075 4877630 : tree field, value;
4076 4877630 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4077 4877630 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4078 19708878 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4079 : {
4080 : /* In GIMPLE the indexes can be either NULL or matching i. */
4081 14831248 : if (field == NULL_TREE)
4082 1087400 : field = bitsize_int (idx);
4083 14831248 : if (TREE_CODE (field) == FIELD_DECL)
4084 : {
4085 9936525 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4086 9936525 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4087 : }
4088 : else
4089 4894723 : hash_operand (field, hstate, flags);
4090 14831248 : hash_operand (value, hstate, flags);
4091 : }
4092 : return;
4093 : }
4094 182 : case STATEMENT_LIST:
4095 182 : {
4096 182 : tree_stmt_iterator i;
4097 182 : for (i = tsi_start (const_cast<tree> (t));
4098 550 : !tsi_end_p (i); tsi_next (&i))
4099 368 : hash_operand (tsi_stmt (i), hstate, flags);
4100 182 : return;
4101 : }
4102 : case TREE_VEC:
4103 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4104 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4105 : return;
4106 4 : case IDENTIFIER_NODE:
4107 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4108 4 : return;
4109 20924181 : case FUNCTION_DECL:
4110 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4111 : Otherwise nodes that compare equal according to operand_equal_p might
4112 : get different hash codes. However, don't do this for machine specific
4113 : or front end builtins, since the function code is overloaded in those
4114 : cases. */
4115 20924181 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4116 20924181 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4117 : {
4118 7068246 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4119 7068246 : code = TREE_CODE (t);
4120 : }
4121 : /* FALL THROUGH */
4122 1315772963 : default:
4123 1315772963 : if (POLY_INT_CST_P (t))
4124 : {
4125 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4126 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4127 : return;
4128 : }
4129 1315772963 : tclass = TREE_CODE_CLASS (code);
4130 :
4131 1315772963 : if (tclass == tcc_declaration)
4132 : {
4133 : /* DECL's have a unique ID */
4134 986721552 : hstate.add_hwi (DECL_UID (t));
4135 : }
4136 329051411 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4137 : {
4138 : /* For comparisons that can be swapped, use the lower
4139 : tree code. */
4140 141333 : enum tree_code ccode = swap_tree_comparison (code);
4141 141333 : if (code < ccode)
4142 60409 : ccode = code;
4143 141333 : hstate.add_object (ccode);
4144 141333 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4145 141333 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4146 : }
4147 328910078 : else if (CONVERT_EXPR_CODE_P (code))
4148 : {
4149 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4150 : operand_equal_p. */
4151 5319057 : enum tree_code ccode = NOP_EXPR;
4152 5319057 : hstate.add_object (ccode);
4153 :
4154 : /* Don't hash the type, that can lead to having nodes which
4155 : compare equal according to operand_equal_p, but which
4156 : have different hash codes. Make sure to include signedness
4157 : in the hash computation. */
4158 5319057 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4159 5319057 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4160 : }
4161 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4162 323591021 : else if (code == MEM_REF
4163 77664205 : && (flags & OEP_ADDRESS_OF) != 0
4164 68504524 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4165 13337366 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4166 336732052 : && integer_zerop (TREE_OPERAND (t, 1)))
4167 6045552 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4168 : hstate, flags);
4169 : /* Don't ICE on FE specific trees, or their arguments etc.
4170 : during operand_equal_p hash verification. */
4171 317545469 : else if (!IS_EXPR_CODE_CLASS (tclass))
4172 252 : gcc_assert (flags & OEP_HASH_CHECK);
4173 : else
4174 : {
4175 317545217 : unsigned int sflags = flags;
4176 :
4177 317545217 : hstate.add_object (code);
4178 :
4179 317545217 : switch (code)
4180 : {
4181 126550582 : case ADDR_EXPR:
4182 126550582 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4183 126550582 : flags |= OEP_ADDRESS_OF;
4184 126550582 : sflags = flags;
4185 126550582 : break;
4186 :
4187 76488624 : case INDIRECT_REF:
4188 76488624 : case MEM_REF:
4189 76488624 : case TARGET_MEM_REF:
4190 76488624 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4191 76488624 : sflags = flags;
4192 76488624 : break;
4193 :
4194 74771366 : case COMPONENT_REF:
4195 74771366 : if (sflags & OEP_ADDRESS_OF)
4196 : {
4197 37461119 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4198 37461119 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4199 : hstate, flags & ~OEP_ADDRESS_OF);
4200 37461119 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4201 : hstate, flags & ~OEP_ADDRESS_OF);
4202 37461119 : return;
4203 : }
4204 : break;
4205 15351676 : case ARRAY_REF:
4206 15351676 : case ARRAY_RANGE_REF:
4207 15351676 : case BIT_FIELD_REF:
4208 15351676 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4209 15351676 : break;
4210 :
4211 8430 : case COND_EXPR:
4212 8430 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4213 8430 : break;
4214 :
4215 0 : case WIDEN_MULT_PLUS_EXPR:
4216 0 : case WIDEN_MULT_MINUS_EXPR:
4217 0 : {
4218 : /* The multiplication operands are commutative. */
4219 0 : inchash::hash one, two;
4220 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4221 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4222 0 : hstate.add_commutative (one, two);
4223 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4224 0 : return;
4225 : }
4226 :
4227 52716 : case CALL_EXPR:
4228 52716 : if (CALL_EXPR_FN (t) == NULL_TREE)
4229 6 : hstate.add_int (CALL_EXPR_IFN (t));
4230 : break;
4231 :
4232 72 : case TARGET_EXPR:
4233 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4234 : Usually different TARGET_EXPRs just should use
4235 : different temporaries in their slots. */
4236 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4237 72 : return;
4238 :
4239 280691 : case OBJ_TYPE_REF:
4240 : /* Virtual table reference. */
4241 280691 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4242 280691 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4243 280691 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4244 280691 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4245 280691 : if (!virtual_method_call_p (t))
4246 : return;
4247 280676 : if (tree c = obj_type_ref_class (t))
4248 : {
4249 280676 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4250 : /* We compute mangled names only when free_lang_data is run.
4251 : In that case we can hash precisely. */
4252 280676 : if (TREE_CODE (c) == TYPE_DECL
4253 280676 : && DECL_ASSEMBLER_NAME_SET_P (c))
4254 7301 : hstate.add_object
4255 7301 : (IDENTIFIER_HASH_VALUE
4256 : (DECL_ASSEMBLER_NAME (c)));
4257 : }
4258 280676 : return;
4259 : default:
4260 : break;
4261 : }
4262 :
4263 : /* Don't hash the type, that can lead to having nodes which
4264 : compare equal according to operand_equal_p, but which
4265 : have different hash codes. */
4266 279803335 : if (code == NON_LVALUE_EXPR)
4267 : {
4268 : /* Make sure to include signness in the hash computation. */
4269 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4270 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4271 : }
4272 :
4273 279803335 : else if (commutative_tree_code (code))
4274 : {
4275 : /* It's a commutative expression. We want to hash it the same
4276 : however it appears. We do this by first hashing both operands
4277 : and then rehashing based on the order of their independent
4278 : hashes. */
4279 17581007 : inchash::hash one, two;
4280 17581007 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4281 17581007 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4282 17581007 : hstate.add_commutative (one, two);
4283 : }
4284 : else
4285 733404401 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4286 680142080 : hash_operand (TREE_OPERAND (t, i), hstate,
4287 : i == 0 ? flags : sflags);
4288 : }
4289 : return;
4290 : }
4291 : }
4292 :
4293 : bool
4294 7055041767 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4295 : unsigned int flags, bool *ret)
4296 : {
4297 : /* When checking and unless comparing DECL names, verify that if
4298 : the outermost operand_equal_p call returns non-zero then ARG0
4299 : and ARG1 have the same hash value. */
4300 7055041767 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4301 : {
4302 2957911347 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4303 : {
4304 462632851 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4305 : {
4306 82376452 : inchash::hash hstate0 (0), hstate1 (0);
4307 82376452 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4308 82376452 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4309 82376452 : hashval_t h0 = hstate0.end ();
4310 82376452 : hashval_t h1 = hstate1.end ();
4311 82376452 : gcc_assert (h0 == h1);
4312 : }
4313 462632851 : *ret = true;
4314 : }
4315 : else
4316 2495278496 : *ret = false;
4317 :
4318 2957911347 : return true;
4319 : }
4320 :
4321 : return false;
4322 : }
4323 :
4324 :
4325 : static operand_compare default_compare_instance;
4326 :
4327 : /* Convenience wrapper around operand_compare class because usually we do
4328 : not need to play with the valueizer. */
4329 :
4330 : bool
4331 2955728008 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4332 : {
4333 2955728008 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4334 : }
4335 :
4336 : namespace inchash
4337 : {
4338 :
4339 : /* Generate a hash value for an expression. This can be used iteratively
4340 : by passing a previous result as the HSTATE argument.
4341 :
4342 : This function is intended to produce the same hash for expressions which
4343 : would compare equal using operand_equal_p. */
4344 : void
4345 2319349387 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4346 : {
4347 2319349387 : default_compare_instance.hash_operand (t, hstate, flags);
4348 2319349387 : }
4349 :
4350 : }
4351 :
4352 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4353 : with a different signedness or a narrower precision. */
4354 :
4355 : static bool
4356 19469517 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4357 : {
4358 19469517 : if (operand_equal_p (arg0, arg1, 0))
4359 : return true;
4360 :
4361 37238864 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4362 31830293 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4363 : return false;
4364 :
4365 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4366 : and see if the inner values are the same. This removes any
4367 : signedness comparison, which doesn't matter here. */
4368 5929571 : tree op0 = arg0;
4369 5929571 : tree op1 = arg1;
4370 5929571 : STRIP_NOPS (op0);
4371 5929571 : STRIP_NOPS (op1);
4372 5929571 : if (operand_equal_p (op0, op1, 0))
4373 : return true;
4374 :
4375 : /* Discard a single widening conversion from ARG1 and see if the inner
4376 : value is the same as ARG0. */
4377 4894224 : if (CONVERT_EXPR_P (arg1)
4378 853098 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4379 853050 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4380 853050 : < TYPE_PRECISION (TREE_TYPE (arg1))
4381 6047109 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4382 : return true;
4383 :
4384 : return false;
4385 : }
4386 :
4387 : /* See if ARG is an expression that is either a comparison or is performing
4388 : arithmetic on comparisons. The comparisons must only be comparing
4389 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4390 : they are nonzero it means that some operands have already been found.
4391 : No variables may be used anywhere else in the expression except in the
4392 : comparisons.
4393 :
4394 : If this is true, return 1. Otherwise, return zero. */
4395 :
4396 : static bool
4397 58184077 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4398 : {
4399 61972006 : enum tree_code code = TREE_CODE (arg);
4400 61972006 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4401 :
4402 : /* We can handle some of the tcc_expression cases here. */
4403 61972006 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4404 : tclass = tcc_unary;
4405 61495413 : else if (tclass == tcc_expression
4406 652053 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4407 652053 : || code == COMPOUND_EXPR))
4408 : tclass = tcc_binary;
4409 :
4410 61484690 : switch (tclass)
4411 : {
4412 3787929 : case tcc_unary:
4413 3787929 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4414 :
4415 5262911 : case tcc_binary:
4416 5262911 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4417 5262911 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4418 :
4419 : case tcc_constant:
4420 : return true;
4421 :
4422 641330 : case tcc_expression:
4423 641330 : if (code == COND_EXPR)
4424 711 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4425 711 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4426 775 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4427 : return false;
4428 :
4429 481015 : case tcc_comparison:
4430 : /* First see if we can handle the first operand, then the second. For
4431 : the second operand, we know *CVAL1 can't be zero. It must be that
4432 : one side of the comparison is each of the values; test for the
4433 : case where this isn't true by failing if the two operands
4434 : are the same. */
4435 :
4436 481015 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4437 481015 : TREE_OPERAND (arg, 1), 0))
4438 : return false;
4439 :
4440 481015 : if (*cval1 == 0)
4441 479009 : *cval1 = TREE_OPERAND (arg, 0);
4442 2006 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4443 : ;
4444 1887 : else if (*cval2 == 0)
4445 0 : *cval2 = TREE_OPERAND (arg, 0);
4446 1887 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4447 : ;
4448 : else
4449 : return false;
4450 :
4451 479128 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4452 : ;
4453 479128 : else if (*cval2 == 0)
4454 479009 : *cval2 = TREE_OPERAND (arg, 1);
4455 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4456 : ;
4457 : else
4458 : return false;
4459 :
4460 : return true;
4461 :
4462 : default:
4463 : return false;
4464 : }
4465 : }
4466 :
4467 : /* ARG is a tree that is known to contain just arithmetic operations and
4468 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4469 : any occurrence of OLD0 as an operand of a comparison and likewise for
4470 : NEW1 and OLD1. */
4471 :
4472 : static tree
4473 708 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4474 : tree old1, tree new1)
4475 : {
4476 708 : tree type = TREE_TYPE (arg);
4477 708 : enum tree_code code = TREE_CODE (arg);
4478 708 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4479 :
4480 : /* We can handle some of the tcc_expression cases here. */
4481 708 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4482 : tclass = tcc_unary;
4483 708 : else if (tclass == tcc_expression
4484 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4485 : tclass = tcc_binary;
4486 :
4487 699 : switch (tclass)
4488 : {
4489 168 : case tcc_unary:
4490 168 : return fold_build1_loc (loc, code, type,
4491 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4492 168 : old0, new0, old1, new1));
4493 :
4494 168 : case tcc_binary:
4495 336 : return fold_build2_loc (loc, code, type,
4496 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4497 : old0, new0, old1, new1),
4498 168 : eval_subst (loc, TREE_OPERAND (arg, 1),
4499 168 : old0, new0, old1, new1));
4500 :
4501 9 : case tcc_expression:
4502 9 : switch (code)
4503 : {
4504 0 : case SAVE_EXPR:
4505 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4506 0 : old1, new1);
4507 :
4508 0 : case COMPOUND_EXPR:
4509 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4510 0 : old1, new1);
4511 :
4512 9 : case COND_EXPR:
4513 27 : return fold_build3_loc (loc, code, type,
4514 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4515 : old0, new0, old1, new1),
4516 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4517 : old0, new0, old1, new1),
4518 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4519 9 : old0, new0, old1, new1));
4520 : default:
4521 : break;
4522 : }
4523 : /* Fall through - ??? */
4524 :
4525 183 : case tcc_comparison:
4526 183 : {
4527 183 : tree arg0 = TREE_OPERAND (arg, 0);
4528 183 : tree arg1 = TREE_OPERAND (arg, 1);
4529 :
4530 : /* We need to check both for exact equality and tree equality. The
4531 : former will be true if the operand has a side-effect. In that
4532 : case, we know the operand occurred exactly once. */
4533 :
4534 183 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4535 : arg0 = new0;
4536 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4537 : arg0 = new1;
4538 :
4539 183 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4540 : arg1 = new0;
4541 183 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4542 : arg1 = new1;
4543 :
4544 183 : return fold_build2_loc (loc, code, type, arg0, arg1);
4545 : }
4546 :
4547 : default:
4548 : return arg;
4549 : }
4550 : }
4551 :
4552 : /* Return a tree for the case when the result of an expression is RESULT
4553 : converted to TYPE and OMITTED was previously an operand of the expression
4554 : but is now not needed (e.g., we folded OMITTED * 0).
4555 :
4556 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4557 : the conversion of RESULT to TYPE. */
4558 :
4559 : tree
4560 282001 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4561 : {
4562 282001 : tree t = fold_convert_loc (loc, type, result);
4563 :
4564 : /* If the resulting operand is an empty statement, just return the omitted
4565 : statement casted to void. */
4566 282001 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4567 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4568 0 : fold_ignored_result (omitted));
4569 :
4570 282001 : if (TREE_SIDE_EFFECTS (omitted))
4571 11332 : return build2_loc (loc, COMPOUND_EXPR, type,
4572 11332 : fold_ignored_result (omitted), t);
4573 :
4574 270669 : return non_lvalue_loc (loc, t);
4575 : }
4576 :
4577 : /* Return a tree for the case when the result of an expression is RESULT
4578 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4579 : of the expression but are now not needed.
4580 :
4581 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4582 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4583 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4584 : just do the conversion of RESULT to TYPE. */
4585 :
4586 : tree
4587 5429 : omit_two_operands_loc (location_t loc, tree type, tree result,
4588 : tree omitted1, tree omitted2)
4589 : {
4590 5429 : tree t = fold_convert_loc (loc, type, result);
4591 :
4592 5429 : if (TREE_SIDE_EFFECTS (omitted2))
4593 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4594 5429 : if (TREE_SIDE_EFFECTS (omitted1))
4595 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4596 :
4597 5429 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4598 : }
4599 :
4600 :
4601 : /* Return a simplified tree node for the truth-negation of ARG. This
4602 : never alters ARG itself. We assume that ARG is an operation that
4603 : returns a truth value (0 or 1).
4604 :
4605 : FIXME: one would think we would fold the result, but it causes
4606 : problems with the dominator optimizer. */
4607 :
4608 : static tree
4609 48362214 : fold_truth_not_expr (location_t loc, tree arg)
4610 : {
4611 48362214 : tree type = TREE_TYPE (arg);
4612 48362214 : enum tree_code code = TREE_CODE (arg);
4613 48362214 : location_t loc1, loc2;
4614 :
4615 : /* If this is a comparison, we can simply invert it, except for
4616 : floating-point non-equality comparisons, in which case we just
4617 : enclose a TRUTH_NOT_EXPR around what we have. */
4618 :
4619 48362214 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4620 : {
4621 36940746 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4622 31745502 : if (FLOAT_TYPE_P (op_type)
4623 5205331 : && flag_trapping_math
4624 5175156 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4625 42075473 : && code != NE_EXPR && code != EQ_EXPR)
4626 : return NULL_TREE;
4627 :
4628 32485464 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4629 32485464 : if (code == ERROR_MARK)
4630 : return NULL_TREE;
4631 :
4632 32485464 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4633 32485464 : TREE_OPERAND (arg, 1));
4634 32485464 : copy_warning (ret, arg);
4635 32485464 : return ret;
4636 : }
4637 :
4638 11421468 : switch (code)
4639 : {
4640 0 : case INTEGER_CST:
4641 0 : return constant_boolean_node (integer_zerop (arg), type);
4642 :
4643 45213 : case TRUTH_AND_EXPR:
4644 45213 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4645 45213 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4646 90426 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4647 45213 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4648 90426 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4649 :
4650 2505 : case TRUTH_OR_EXPR:
4651 2505 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4652 2505 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4653 5010 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4654 2505 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4655 5010 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4656 :
4657 68673 : case TRUTH_XOR_EXPR:
4658 : /* Here we can invert either operand. We invert the first operand
4659 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4660 : result is the XOR of the first operand with the inside of the
4661 : negation of the second operand. */
4662 :
4663 68673 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4664 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4665 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4666 : else
4667 68485 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4668 68485 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4669 136970 : TREE_OPERAND (arg, 1));
4670 :
4671 373909 : case TRUTH_ANDIF_EXPR:
4672 373909 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4673 373909 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4674 747818 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4675 373909 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4676 747818 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4677 :
4678 15922 : case TRUTH_ORIF_EXPR:
4679 15922 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4680 15922 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4681 31844 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4682 15922 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4683 31844 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4684 :
4685 784995 : case TRUTH_NOT_EXPR:
4686 784995 : return TREE_OPERAND (arg, 0);
4687 :
4688 9748 : case COND_EXPR:
4689 9748 : {
4690 9748 : tree arg1 = TREE_OPERAND (arg, 1);
4691 9748 : tree arg2 = TREE_OPERAND (arg, 2);
4692 :
4693 9748 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4694 9748 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4695 :
4696 : /* A COND_EXPR may have a throw as one operand, which
4697 : then has void type. Just leave void operands
4698 : as they are. */
4699 9748 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4700 9748 : VOID_TYPE_P (TREE_TYPE (arg1))
4701 9748 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4702 9748 : VOID_TYPE_P (TREE_TYPE (arg2))
4703 19493 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4704 : }
4705 :
4706 937 : case COMPOUND_EXPR:
4707 937 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4708 1874 : return build2_loc (loc, COMPOUND_EXPR, type,
4709 937 : TREE_OPERAND (arg, 0),
4710 1874 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4711 :
4712 0 : case NON_LVALUE_EXPR:
4713 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4714 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4715 :
4716 74023 : CASE_CONVERT:
4717 74023 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4718 73959 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4719 :
4720 : /* fall through */
4721 :
4722 64 : case FLOAT_EXPR:
4723 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4724 64 : return build1_loc (loc, TREE_CODE (arg), type,
4725 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4726 :
4727 500 : case BIT_AND_EXPR:
4728 500 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4729 : return NULL_TREE;
4730 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4731 :
4732 2 : case SAVE_EXPR:
4733 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4734 :
4735 216 : case CLEANUP_POINT_EXPR:
4736 216 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4737 216 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4738 432 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4739 :
4740 : default:
4741 : return NULL_TREE;
4742 : }
4743 : }
4744 :
4745 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4746 : assume that ARG is an operation that returns a truth value (0 or 1
4747 : for scalars, 0 or -1 for vectors). Return the folded expression if
4748 : folding is successful. Otherwise, return NULL_TREE. */
4749 :
4750 : static tree
4751 1995224 : fold_invert_truthvalue (location_t loc, tree arg)
4752 : {
4753 1995224 : tree type = TREE_TYPE (arg);
4754 3990424 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4755 : ? BIT_NOT_EXPR
4756 : : TRUTH_NOT_EXPR,
4757 1995224 : type, arg);
4758 : }
4759 :
4760 : /* Return a simplified tree node for the truth-negation of ARG. This
4761 : never alters ARG itself. We assume that ARG is an operation that
4762 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4763 :
4764 : tree
4765 42224693 : invert_truthvalue_loc (location_t loc, tree arg)
4766 : {
4767 42224693 : if (TREE_CODE (arg) == ERROR_MARK)
4768 : return arg;
4769 :
4770 42224693 : tree type = TREE_TYPE (arg);
4771 84449386 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4772 : ? BIT_NOT_EXPR
4773 : : TRUTH_NOT_EXPR,
4774 42224693 : type, arg);
4775 : }
4776 :
4777 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4778 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4779 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4780 : is the original memory reference used to preserve the alias set of
4781 : the access. */
4782 :
4783 : tree
4784 731344 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4785 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4786 : int unsignedp, int reversep)
4787 : {
4788 731344 : tree result, bftype;
4789 :
4790 : /* Attempt not to lose the access path if possible. */
4791 731344 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4792 : {
4793 727608 : tree ninner = TREE_OPERAND (orig_inner, 0);
4794 727608 : machine_mode nmode;
4795 727608 : poly_int64 nbitsize, nbitpos;
4796 727608 : tree noffset;
4797 727608 : int nunsignedp, nreversep, nvolatilep = 0;
4798 727608 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4799 : &noffset, &nmode, &nunsignedp,
4800 : &nreversep, &nvolatilep);
4801 727608 : if (base == inner
4802 727471 : && noffset == NULL_TREE
4803 727471 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4804 727464 : && !reversep
4805 727392 : && !nreversep
4806 1455000 : && !nvolatilep)
4807 : {
4808 727392 : inner = ninner;
4809 727608 : bitpos -= nbitpos;
4810 : }
4811 : }
4812 :
4813 731344 : alias_set_type iset = get_alias_set (orig_inner);
4814 731344 : if (iset == 0 && get_alias_set (inner) != iset)
4815 228 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4816 : build_fold_addr_expr (inner),
4817 : build_int_cst (ptr_type_node, 0));
4818 :
4819 731344 : if (known_eq (bitpos, 0) && !reversep)
4820 : {
4821 11546 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4822 23092 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4823 11388 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4824 162 : && tree_fits_shwi_p (size)
4825 11708 : && tree_to_shwi (size) == bitsize)
4826 139 : return fold_convert_loc (loc, type, inner);
4827 : }
4828 :
4829 731205 : bftype = type;
4830 731205 : if (TYPE_PRECISION (bftype) != bitsize
4831 731205 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4832 396 : bftype = build_nonstandard_integer_type (bitsize, 0);
4833 :
4834 731205 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4835 731205 : bitsize_int (bitsize), bitsize_int (bitpos));
4836 731205 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4837 :
4838 731205 : if (bftype != type)
4839 396 : result = fold_convert_loc (loc, type, result);
4840 :
4841 : return result;
4842 : }
4843 :
4844 : /* Optimize a bit-field compare.
4845 :
4846 : There are two cases: First is a compare against a constant and the
4847 : second is a comparison of two items where the fields are at the same
4848 : bit position relative to the start of a chunk (byte, halfword, word)
4849 : large enough to contain it. In these cases we can avoid the shift
4850 : implicit in bitfield extractions.
4851 :
4852 : For constants, we emit a compare of the shifted constant with the
4853 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4854 : compared. For two fields at the same position, we do the ANDs with the
4855 : similar mask and compare the result of the ANDs.
4856 :
4857 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4858 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4859 : are the left and right operands of the comparison, respectively.
4860 :
4861 : If the optimization described above can be done, we return the resulting
4862 : tree. Otherwise we return zero. */
4863 :
4864 : static tree
4865 4363325 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4866 : tree compare_type, tree lhs, tree rhs)
4867 : {
4868 4363325 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4869 4363325 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4870 4363325 : tree type = TREE_TYPE (lhs);
4871 4363325 : tree unsigned_type;
4872 4363325 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4873 4363325 : machine_mode lmode, rmode;
4874 4363325 : scalar_int_mode nmode;
4875 4363325 : int lunsignedp, runsignedp;
4876 4363325 : int lreversep, rreversep;
4877 4363325 : int lvolatilep = 0, rvolatilep = 0;
4878 4363325 : tree linner, rinner = NULL_TREE;
4879 4363325 : tree mask;
4880 4363325 : tree offset;
4881 :
4882 : /* Get all the information about the extractions being done. If the bit size
4883 : is the same as the size of the underlying object, we aren't doing an
4884 : extraction at all and so can do nothing. We also don't want to
4885 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4886 : then will no longer be able to replace it. */
4887 4363325 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4888 : &lunsignedp, &lreversep, &lvolatilep);
4889 4363325 : if (linner == lhs
4890 4363325 : || !known_size_p (plbitsize)
4891 4363325 : || !plbitsize.is_constant (&lbitsize)
4892 4363325 : || !plbitpos.is_constant (&lbitpos)
4893 8726650 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4894 692892 : || offset != 0
4895 692867 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4896 5056192 : || lvolatilep)
4897 3670518 : return 0;
4898 :
4899 692807 : if (const_p)
4900 655065 : rreversep = lreversep;
4901 : else
4902 : {
4903 : /* If this is not a constant, we can only do something if bit positions,
4904 : sizes, signedness and storage order are the same. */
4905 37742 : rinner
4906 37742 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4907 : &runsignedp, &rreversep, &rvolatilep);
4908 :
4909 37742 : if (rinner == rhs
4910 37698 : || maybe_ne (lbitpos, rbitpos)
4911 37664 : || maybe_ne (lbitsize, rbitsize)
4912 37664 : || lunsignedp != runsignedp
4913 37664 : || lreversep != rreversep
4914 37664 : || offset != 0
4915 37664 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4916 75406 : || rvolatilep)
4917 : return 0;
4918 : }
4919 :
4920 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4921 692729 : poly_uint64 bitstart = 0;
4922 692729 : poly_uint64 bitend = 0;
4923 692729 : if (TREE_CODE (lhs) == COMPONENT_REF)
4924 : {
4925 692729 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4926 692729 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4927 : return 0;
4928 : }
4929 :
4930 : /* See if we can find a mode to refer to this field. We should be able to,
4931 : but fail if we can't. */
4932 1385458 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4933 655065 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4934 37664 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4935 : TYPE_ALIGN (TREE_TYPE (rinner))),
4936 692729 : BITS_PER_WORD, false, &nmode))
4937 : return 0;
4938 :
4939 : /* Set signed and unsigned types of the precision of this mode for the
4940 : shifts below. */
4941 690726 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4942 :
4943 : /* Compute the bit position and size for the new reference and our offset
4944 : within it. If the new reference is the same size as the original, we
4945 : won't optimize anything, so return zero. */
4946 690726 : nbitsize = GET_MODE_BITSIZE (nmode);
4947 690726 : nbitpos = lbitpos & ~ (nbitsize - 1);
4948 690726 : lbitpos -= nbitpos;
4949 690726 : if (nbitsize == lbitsize)
4950 : return 0;
4951 :
4952 690726 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4953 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4954 :
4955 : /* Make the mask to be used against the extracted field. */
4956 690726 : mask = build_int_cst_type (unsigned_type, -1);
4957 690726 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4958 690726 : mask = const_binop (RSHIFT_EXPR, mask,
4959 690726 : size_int (nbitsize - lbitsize - lbitpos));
4960 :
4961 690726 : if (! const_p)
4962 : {
4963 36109 : if (nbitpos < 0)
4964 : return 0;
4965 :
4966 : /* If not comparing with constant, just rework the comparison
4967 : and return. */
4968 36109 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4969 36109 : nbitsize, nbitpos, 1, lreversep);
4970 36109 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4971 36109 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4972 36109 : nbitsize, nbitpos, 1, rreversep);
4973 36109 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4974 36109 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4975 : }
4976 :
4977 : /* Otherwise, we are handling the constant case. See if the constant is too
4978 : big for the field. Warn and return a tree for 0 (false) if so. We do
4979 : this not only for its own sake, but to avoid having to test for this
4980 : error case below. If we didn't, we might generate wrong code.
4981 :
4982 : For unsigned fields, the constant shifted right by the field length should
4983 : be all zero. For signed fields, the high-order bits should agree with
4984 : the sign bit. */
4985 :
4986 654617 : if (lunsignedp)
4987 : {
4988 653452 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4989 : {
4990 0 : warning (0, "comparison is always %d due to width of bit-field",
4991 : code == NE_EXPR);
4992 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4993 : }
4994 : }
4995 : else
4996 : {
4997 1165 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4998 1165 : if (tem != 0 && tem != -1)
4999 : {
5000 0 : warning (0, "comparison is always %d due to width of bit-field",
5001 : code == NE_EXPR);
5002 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5003 : }
5004 1165 : }
5005 :
5006 654617 : if (nbitpos < 0)
5007 : return 0;
5008 :
5009 : /* Single-bit compares should always be against zero. */
5010 654617 : if (lbitsize == 1 && ! integer_zerop (rhs))
5011 : {
5012 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5013 175 : rhs = build_int_cst (type, 0);
5014 : }
5015 :
5016 : /* Make a new bitfield reference, shift the constant over the
5017 : appropriate number of bits and mask it with the computed mask
5018 : (in case this was a signed field). If we changed it, make a new one. */
5019 654617 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5020 654617 : nbitsize, nbitpos, 1, lreversep);
5021 :
5022 654617 : rhs = const_binop (BIT_AND_EXPR,
5023 : const_binop (LSHIFT_EXPR,
5024 : fold_convert_loc (loc, unsigned_type, rhs),
5025 654617 : size_int (lbitpos)),
5026 : mask);
5027 :
5028 654617 : lhs = build2_loc (loc, code, compare_type,
5029 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5030 654617 : return lhs;
5031 : }
5032 :
5033 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5034 : represents the sign bit of EXP's type. If EXP represents a sign
5035 : or zero extension, also test VAL against the unextended type.
5036 : The return value is the (sub)expression whose sign bit is VAL,
5037 : or NULL_TREE otherwise. */
5038 :
5039 : tree
5040 2205 : sign_bit_p (tree exp, const_tree val)
5041 : {
5042 2205 : int width;
5043 2205 : tree t;
5044 :
5045 : /* Tree EXP must have an integral type. */
5046 2205 : t = TREE_TYPE (exp);
5047 2205 : if (! INTEGRAL_TYPE_P (t))
5048 : return NULL_TREE;
5049 :
5050 : /* Tree VAL must be an integer constant. */
5051 1859 : if (TREE_CODE (val) != INTEGER_CST
5052 1859 : || TREE_OVERFLOW (val))
5053 : return NULL_TREE;
5054 :
5055 1499 : width = TYPE_PRECISION (t);
5056 1499 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5057 : return exp;
5058 :
5059 : /* Handle extension from a narrower type. */
5060 862 : if (TREE_CODE (exp) == NOP_EXPR
5061 862 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5062 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5063 :
5064 : return NULL_TREE;
5065 : }
5066 :
5067 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5068 : operand is simple enough to be evaluated unconditionally. */
5069 :
5070 : static bool
5071 64992296 : simple_operand_p (const_tree exp)
5072 : {
5073 : /* Strip any conversions that don't change the machine mode. */
5074 64992296 : STRIP_NOPS (exp);
5075 :
5076 64992296 : return (CONSTANT_CLASS_P (exp)
5077 45081458 : || TREE_CODE (exp) == SSA_NAME
5078 80773811 : || (DECL_P (exp)
5079 5481375 : && ! TREE_ADDRESSABLE (exp)
5080 5397320 : && ! TREE_THIS_VOLATILE (exp)
5081 5397320 : && ! DECL_NONLOCAL (exp)
5082 : /* Don't regard global variables as simple. They may be
5083 : allocated in ways unknown to the compiler (shared memory,
5084 : #pragma weak, etc). */
5085 5395667 : && ! TREE_PUBLIC (exp)
5086 5374965 : && ! DECL_EXTERNAL (exp)
5087 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5088 5374965 : && ! ((VAR_P (exp)
5089 : || TREE_CODE (exp) == PARM_DECL
5090 : || TREE_CODE (exp) == RESULT_DECL)
5091 5374965 : && DECL_HAS_VALUE_EXPR_P (exp))
5092 : /* Weakrefs are not safe to be read, since they can be NULL.
5093 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5094 : have DECL_WEAK flag set. */
5095 5374376 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5096 : /* Loading a static variable is unduly expensive, but global
5097 : registers aren't expensive. */
5098 5374376 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5099 : }
5100 :
5101 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5102 : In addition to simple_operand_p, we assume that comparisons, conversions,
5103 : and logic-not operations are simple, if their operands are simple, too. */
5104 :
5105 : bool
5106 6796056 : simple_condition_p (tree exp)
5107 : {
5108 6887005 : enum tree_code code;
5109 :
5110 6887005 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5111 4673383 : return false;
5112 :
5113 2247925 : while (CONVERT_EXPR_P (exp))
5114 34303 : exp = TREE_OPERAND (exp, 0);
5115 :
5116 2213622 : code = TREE_CODE (exp);
5117 :
5118 2213622 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5119 1732933 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5120 1732933 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5121 :
5122 480689 : if (code == TRUTH_NOT_EXPR)
5123 90949 : return simple_condition_p (TREE_OPERAND (exp, 0));
5124 :
5125 389740 : return simple_operand_p (exp);
5126 : }
5127 :
5128 :
5129 : /* The following functions are subroutines to fold_range_test and allow it to
5130 : try to change a logical combination of comparisons into a range test.
5131 :
5132 : For example, both
5133 : X == 2 || X == 3 || X == 4 || X == 5
5134 : and
5135 : X >= 2 && X <= 5
5136 : are converted to
5137 : (unsigned) (X - 2) <= 3
5138 :
5139 : We describe each set of comparisons as being either inside or outside
5140 : a range, using a variable named like IN_P, and then describe the
5141 : range with a lower and upper bound. If one of the bounds is omitted,
5142 : it represents either the highest or lowest value of the type.
5143 :
5144 : In the comments below, we represent a range by two numbers in brackets
5145 : preceded by a "+" to designate being inside that range, or a "-" to
5146 : designate being outside that range, so the condition can be inverted by
5147 : flipping the prefix. An omitted bound is represented by a "-". For
5148 : example, "- [-, 10]" means being outside the range starting at the lowest
5149 : possible value and ending at 10, in other words, being greater than 10.
5150 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5151 : always false.
5152 :
5153 : We set up things so that the missing bounds are handled in a consistent
5154 : manner so neither a missing bound nor "true" and "false" need to be
5155 : handled using a special case. */
5156 :
5157 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5158 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5159 : and UPPER1_P are nonzero if the respective argument is an upper bound
5160 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5161 : must be specified for a comparison. ARG1 will be converted to ARG0's
5162 : type if both are specified. */
5163 :
5164 : static tree
5165 22601641 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5166 : tree arg1, int upper1_p)
5167 : {
5168 22601641 : tree tem;
5169 22601641 : int result;
5170 22601641 : int sgn0, sgn1;
5171 :
5172 : /* If neither arg represents infinity, do the normal operation.
5173 : Else, if not a comparison, return infinity. Else handle the special
5174 : comparison rules. Note that most of the cases below won't occur, but
5175 : are handled for consistency. */
5176 :
5177 22601641 : if (arg0 != 0 && arg1 != 0)
5178 : {
5179 11837661 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5180 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5181 11837661 : STRIP_NOPS (tem);
5182 11837661 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5183 : }
5184 :
5185 10763980 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5186 : return 0;
5187 :
5188 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5189 : for neither. In real maths, we cannot assume open ended ranges are
5190 : the same. But, this is computer arithmetic, where numbers are finite.
5191 : We can therefore make the transformation of any unbounded range with
5192 : the value Z, Z being greater than any representable number. This permits
5193 : us to treat unbounded ranges as equal. */
5194 10755444 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5195 10755444 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5196 10755444 : switch (code)
5197 : {
5198 5058563 : case EQ_EXPR:
5199 5058563 : result = sgn0 == sgn1;
5200 5058563 : break;
5201 0 : case NE_EXPR:
5202 0 : result = sgn0 != sgn1;
5203 0 : break;
5204 367002 : case LT_EXPR:
5205 367002 : result = sgn0 < sgn1;
5206 367002 : break;
5207 2465444 : case LE_EXPR:
5208 2465444 : result = sgn0 <= sgn1;
5209 2465444 : break;
5210 2864435 : case GT_EXPR:
5211 2864435 : result = sgn0 > sgn1;
5212 2864435 : break;
5213 0 : case GE_EXPR:
5214 0 : result = sgn0 >= sgn1;
5215 0 : break;
5216 0 : default:
5217 0 : gcc_unreachable ();
5218 : }
5219 :
5220 10755444 : return constant_boolean_node (result, type);
5221 : }
5222 :
5223 : /* Helper routine for make_range. Perform one step for it, return
5224 : new expression if the loop should continue or NULL_TREE if it should
5225 : stop. */
5226 :
5227 : tree
5228 59404167 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5229 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5230 : {
5231 59404167 : tree arg0_type = TREE_TYPE (arg0);
5232 59404167 : tree n_low, n_high, low = *p_low, high = *p_high;
5233 59404167 : int in_p = *p_in_p, n_in_p;
5234 :
5235 59404167 : switch (code)
5236 : {
5237 1612436 : case TRUTH_NOT_EXPR:
5238 : /* We can only do something if the range is testing for zero. */
5239 1612436 : if (low == NULL_TREE || high == NULL_TREE
5240 1612436 : || ! integer_zerop (low) || ! integer_zerop (high))
5241 0 : return NULL_TREE;
5242 1612436 : *p_in_p = ! in_p;
5243 1612436 : return arg0;
5244 :
5245 46394396 : case EQ_EXPR: case NE_EXPR:
5246 46394396 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5247 : /* We can only do something if the range is testing for zero
5248 : and if the second operand is an integer constant. Note that
5249 : saying something is "in" the range we make is done by
5250 : complementing IN_P since it will set in the initial case of
5251 : being not equal to zero; "out" is leaving it alone. */
5252 46394396 : if (low == NULL_TREE || high == NULL_TREE
5253 46394396 : || ! integer_zerop (low) || ! integer_zerop (high)
5254 92788704 : || TREE_CODE (arg1) != INTEGER_CST)
5255 17122036 : return NULL_TREE;
5256 :
5257 29272360 : switch (code)
5258 : {
5259 : case NE_EXPR: /* - [c, c] */
5260 : low = high = arg1;
5261 : break;
5262 7929446 : case EQ_EXPR: /* + [c, c] */
5263 7929446 : in_p = ! in_p, low = high = arg1;
5264 7929446 : break;
5265 2178069 : case GT_EXPR: /* - [-, c] */
5266 2178069 : low = 0, high = arg1;
5267 2178069 : break;
5268 728608 : case GE_EXPR: /* + [c, -] */
5269 728608 : in_p = ! in_p, low = arg1, high = 0;
5270 728608 : break;
5271 5702378 : case LT_EXPR: /* - [c, -] */
5272 5702378 : low = arg1, high = 0;
5273 5702378 : break;
5274 4390143 : case LE_EXPR: /* + [-, c] */
5275 4390143 : in_p = ! in_p, low = 0, high = arg1;
5276 4390143 : break;
5277 0 : default:
5278 0 : gcc_unreachable ();
5279 : }
5280 :
5281 : /* If this is an unsigned comparison, we also know that EXP is
5282 : greater than or equal to zero. We base the range tests we make
5283 : on that fact, so we record it here so we can parse existing
5284 : range tests. We test arg0_type since often the return type
5285 : of, e.g. EQ_EXPR, is boolean. */
5286 29272360 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5287 : {
5288 1877573 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5289 : in_p, low, high, 1,
5290 : build_int_cst (arg0_type, 0),
5291 : NULL_TREE))
5292 : return NULL_TREE;
5293 :
5294 1877564 : in_p = n_in_p, low = n_low, high = n_high;
5295 :
5296 : /* If the high bound is missing, but we have a nonzero low
5297 : bound, reverse the range so it goes from zero to the low bound
5298 : minus 1. */
5299 1877564 : if (high == 0 && low && ! integer_zerop (low))
5300 : {
5301 830389 : in_p = ! in_p;
5302 830389 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5303 830389 : build_int_cst (TREE_TYPE (low), 1), 0);
5304 830389 : low = build_int_cst (arg0_type, 0);
5305 : }
5306 : }
5307 :
5308 29272351 : *p_low = low;
5309 29272351 : *p_high = high;
5310 29272351 : *p_in_p = in_p;
5311 29272351 : return arg0;
5312 :
5313 290 : case NEGATE_EXPR:
5314 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5315 : low and high are non-NULL, then normalize will DTRT. */
5316 290 : if (!TYPE_UNSIGNED (arg0_type)
5317 290 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5318 : {
5319 95 : if (low == NULL_TREE)
5320 12 : low = TYPE_MIN_VALUE (arg0_type);
5321 95 : if (high == NULL_TREE)
5322 47 : high = TYPE_MAX_VALUE (arg0_type);
5323 : }
5324 :
5325 : /* (-x) IN [a,b] -> x in [-b, -a] */
5326 290 : n_low = range_binop (MINUS_EXPR, exp_type,
5327 : build_int_cst (exp_type, 0),
5328 : 0, high, 1);
5329 290 : n_high = range_binop (MINUS_EXPR, exp_type,
5330 : build_int_cst (exp_type, 0),
5331 : 0, low, 0);
5332 290 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5333 : return NULL_TREE;
5334 278 : goto normalize;
5335 :
5336 12 : case BIT_NOT_EXPR:
5337 : /* ~ X -> -X - 1 */
5338 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5339 : build_int_cst (exp_type, 1));
5340 :
5341 842582 : case PLUS_EXPR:
5342 842582 : case MINUS_EXPR:
5343 842582 : if (TREE_CODE (arg1) != INTEGER_CST)
5344 : return NULL_TREE;
5345 :
5346 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5347 : move a constant to the other side. */
5348 637694 : if (!TYPE_UNSIGNED (arg0_type)
5349 637694 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5350 : return NULL_TREE;
5351 :
5352 : /* If EXP is signed, any overflow in the computation is undefined,
5353 : so we don't worry about it so long as our computations on
5354 : the bounds don't overflow. For unsigned, overflow is defined
5355 : and this is exactly the right thing. */
5356 893532 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5357 : arg0_type, low, 0, arg1, 0);
5358 448262 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5359 : arg0_type, high, 1, arg1, 0);
5360 445269 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5361 893519 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5362 : return NULL_TREE;
5363 :
5364 448528 : normalize:
5365 : /* Check for an unsigned range which has wrapped around the maximum
5366 : value thus making n_high < n_low, and normalize it. */
5367 448528 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5368 : {
5369 124079 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5370 124079 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5371 124079 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5372 124079 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5373 :
5374 : /* If the range is of the form +/- [ x+1, x ], we won't
5375 : be able to normalize it. But then, it represents the
5376 : whole range or the empty set, so make it
5377 : +/- [ -, - ]. */
5378 124079 : if (tree_int_cst_equal (n_low, low)
5379 124079 : && tree_int_cst_equal (n_high, high))
5380 : low = high = 0;
5381 : else
5382 124079 : in_p = ! in_p;
5383 : }
5384 : else
5385 324449 : low = n_low, high = n_high;
5386 :
5387 448528 : *p_low = low;
5388 448528 : *p_high = high;
5389 448528 : *p_in_p = in_p;
5390 448528 : return arg0;
5391 :
5392 2485451 : CASE_CONVERT:
5393 2485451 : case NON_LVALUE_EXPR:
5394 2485451 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5395 : return NULL_TREE;
5396 :
5397 1159204 : if (! INTEGRAL_TYPE_P (arg0_type)
5398 1126160 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5399 1024445 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5400 : return NULL_TREE;
5401 :
5402 1004231 : n_low = low, n_high = high;
5403 :
5404 1004231 : if (n_low != 0)
5405 838412 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5406 :
5407 1004231 : if (n_high != 0)
5408 940044 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5409 :
5410 : /* If we're converting arg0 from an unsigned type, to exp,
5411 : a signed type, we will be doing the comparison as unsigned.
5412 : The tests above have already verified that LOW and HIGH
5413 : are both positive.
5414 :
5415 : So we have to ensure that we will handle large unsigned
5416 : values the same way that the current signed bounds treat
5417 : negative values. */
5418 :
5419 1004231 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5420 : {
5421 255227 : tree high_positive;
5422 255227 : tree equiv_type;
5423 : /* For fixed-point modes, we need to pass the saturating flag
5424 : as the 2nd parameter. */
5425 255227 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5426 0 : equiv_type
5427 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5428 0 : TYPE_SATURATING (arg0_type));
5429 255227 : else if (BITINT_TYPE_P (arg0_type))
5430 : equiv_type = arg0_type;
5431 : else
5432 255219 : equiv_type
5433 255219 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5434 :
5435 : /* A range without an upper bound is, naturally, unbounded.
5436 : Since convert would have cropped a very large value, use
5437 : the max value for the destination type. */
5438 255227 : high_positive
5439 255227 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5440 0 : : TYPE_MAX_VALUE (arg0_type);
5441 :
5442 255227 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5443 234309 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5444 : fold_convert_loc (loc, arg0_type,
5445 : high_positive),
5446 : build_int_cst (arg0_type, 1));
5447 :
5448 : /* If the low bound is specified, "and" the range with the
5449 : range for which the original unsigned value will be
5450 : positive. */
5451 255227 : if (low != 0)
5452 : {
5453 94843 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5454 : 1, fold_convert_loc (loc, arg0_type,
5455 : integer_zero_node),
5456 : high_positive))
5457 : return NULL_TREE;
5458 :
5459 94843 : in_p = (n_in_p == in_p);
5460 : }
5461 : else
5462 : {
5463 : /* Otherwise, "or" the range with the range of the input
5464 : that will be interpreted as negative. */
5465 160384 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5466 : 1, fold_convert_loc (loc, arg0_type,
5467 : integer_zero_node),
5468 : high_positive))
5469 : return NULL_TREE;
5470 :
5471 160384 : in_p = (in_p != n_in_p);
5472 : }
5473 : }
5474 :
5475 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5476 : an unsigned type, we will do the comparison as signed. If
5477 : high is non-NULL, we punt above if it doesn't fit in the signed
5478 : type, so if we get through here, +[-, high] or +[low, high] are
5479 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5480 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5481 : high is not, the +[low, -] range is equivalent to union of
5482 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5483 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5484 : low being 0, which should be treated as [-, -]. */
5485 749004 : else if (TYPE_UNSIGNED (exp_type)
5486 730671 : && !TYPE_UNSIGNED (arg0_type)
5487 375839 : && low
5488 1124843 : && !high)
5489 : {
5490 12 : if (integer_zerop (low))
5491 12 : n_low = NULL_TREE;
5492 : else
5493 : {
5494 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5495 : n_low, build_int_cst (arg0_type, -1));
5496 0 : n_low = build_zero_cst (arg0_type);
5497 0 : in_p = !in_p;
5498 : }
5499 : }
5500 :
5501 1004231 : *p_low = n_low;
5502 1004231 : *p_high = n_high;
5503 1004231 : *p_in_p = in_p;
5504 1004231 : return arg0;
5505 :
5506 : default:
5507 : return NULL_TREE;
5508 : }
5509 : }
5510 :
5511 : /* Given EXP, a logical expression, set the range it is testing into
5512 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5513 : actually being tested. *PLOW and *PHIGH will be made of the same
5514 : type as the returned expression. If EXP is not a comparison, we
5515 : will most likely not be returning a useful value and range. */
5516 :
5517 : tree
5518 49286394 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5519 : {
5520 49286394 : enum tree_code code;
5521 49286394 : tree arg0, arg1 = NULL_TREE;
5522 49286394 : tree exp_type, nexp;
5523 49286394 : int in_p;
5524 49286394 : tree low, high;
5525 49286394 : location_t loc = EXPR_LOCATION (exp);
5526 :
5527 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5528 : and see if we can refine the range. Some of the cases below may not
5529 : happen, but it doesn't seem worth worrying about this. We "continue"
5530 : the outer loop when we've changed something; otherwise we "break"
5531 : the switch, which will "break" the while. */
5532 :
5533 49286394 : in_p = 0;
5534 49286394 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5535 :
5536 78656573 : while (1)
5537 : {
5538 78656573 : code = TREE_CODE (exp);
5539 78656573 : exp_type = TREE_TYPE (exp);
5540 78656573 : arg0 = NULL_TREE;
5541 :
5542 78656573 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5543 : {
5544 54742825 : if (TREE_OPERAND_LENGTH (exp) > 0)
5545 54742825 : arg0 = TREE_OPERAND (exp, 0);
5546 54742825 : if (TREE_CODE_CLASS (code) == tcc_binary
5547 51691873 : || TREE_CODE_CLASS (code) == tcc_comparison
5548 64074666 : || (TREE_CODE_CLASS (code) == tcc_expression
5549 2764510 : && TREE_OPERAND_LENGTH (exp) > 1))
5550 46546427 : arg1 = TREE_OPERAND (exp, 1);
5551 : }
5552 54742825 : if (arg0 == NULL_TREE)
5553 : break;
5554 :
5555 54742811 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5556 : &high, &in_p);
5557 54742811 : if (nexp == NULL_TREE)
5558 : break;
5559 : exp = nexp;
5560 : }
5561 :
5562 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5563 49286394 : if (TREE_CODE (exp) == INTEGER_CST)
5564 : {
5565 30202 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5566 : exp, 0, low, 0))
5567 30202 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5568 : exp, 1, high, 1)));
5569 30202 : low = high = 0;
5570 30202 : exp = 0;
5571 : }
5572 :
5573 49286394 : *pin_p = in_p, *plow = low, *phigh = high;
5574 49286394 : return exp;
5575 : }
5576 :
5577 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5578 : a bitwise check i.e. when
5579 : LOW == 0xXX...X00...0
5580 : HIGH == 0xXX...X11...1
5581 : Return corresponding mask in MASK and stem in VALUE. */
5582 :
5583 : static bool
5584 125 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5585 : tree *value)
5586 : {
5587 125 : if (TREE_CODE (low) != INTEGER_CST
5588 125 : || TREE_CODE (high) != INTEGER_CST)
5589 : return false;
5590 :
5591 125 : unsigned prec = TYPE_PRECISION (type);
5592 125 : wide_int lo = wi::to_wide (low, prec);
5593 125 : wide_int hi = wi::to_wide (high, prec);
5594 :
5595 125 : wide_int end_mask = lo ^ hi;
5596 250 : if ((end_mask & (end_mask + 1)) != 0
5597 235 : || (lo & end_mask) != 0)
5598 : return false;
5599 :
5600 86 : wide_int stem_mask = ~end_mask;
5601 86 : wide_int stem = lo & stem_mask;
5602 86 : if (stem != (hi & stem_mask))
5603 : return false;
5604 :
5605 86 : *mask = wide_int_to_tree (type, stem_mask);
5606 86 : *value = wide_int_to_tree (type, stem);
5607 :
5608 86 : return true;
5609 211 : }
5610 :
5611 : /* Helper routine for build_range_check and match.pd. Return the type to
5612 : perform the check or NULL if it shouldn't be optimized. */
5613 :
5614 : tree
5615 570997 : range_check_type (tree etype)
5616 : {
5617 : /* First make sure that arithmetics in this type is valid, then make sure
5618 : that it wraps around. */
5619 570997 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5620 0 : etype = TREE_TYPE (etype);
5621 570997 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5622 57880 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5623 :
5624 570997 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5625 : {
5626 394304 : tree utype, minv, maxv;
5627 :
5628 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5629 : for the type in question, as we rely on this here. */
5630 394304 : utype = unsigned_type_for (etype);
5631 394304 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5632 394304 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5633 394304 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5634 394304 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5635 :
5636 394304 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5637 : minv, 1, maxv, 1)))
5638 : etype = utype;
5639 : else
5640 0 : return NULL_TREE;
5641 : }
5642 176693 : else if (POINTER_TYPE_P (etype)
5643 : || TREE_CODE (etype) == OFFSET_TYPE
5644 : /* Right now all BITINT_TYPEs satisfy
5645 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5646 : that like for INTEGER_TYPEs. */
5647 : || TREE_CODE (etype) == BITINT_TYPE)
5648 1366 : etype = unsigned_type_for (etype);
5649 : return etype;
5650 : }
5651 :
5652 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5653 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5654 : on IN_P) the range. Return 0 if the test couldn't be created. */
5655 :
5656 : tree
5657 1597972 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5658 : tree low, tree high)
5659 : {
5660 2761604 : tree etype = TREE_TYPE (exp), mask, value;
5661 :
5662 : /* Disable this optimization for function pointer expressions
5663 : on targets that require function pointer canonicalization. */
5664 2761604 : if (targetm.have_canonicalize_funcptr_for_compare ()
5665 0 : && POINTER_TYPE_P (etype)
5666 2761604 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5667 : return NULL_TREE;
5668 :
5669 2761604 : if (! in_p)
5670 : {
5671 317759 : value = build_range_check (loc, type, exp, 1, low, high);
5672 317759 : if (value != 0)
5673 317759 : return invert_truthvalue_loc (loc, value);
5674 :
5675 : return 0;
5676 : }
5677 :
5678 2443845 : if (low == 0 && high == 0)
5679 128861 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5680 :
5681 2314984 : if (low == 0)
5682 765798 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5683 765798 : fold_convert_loc (loc, etype, high));
5684 :
5685 1549186 : if (high == 0)
5686 74906 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5687 74906 : fold_convert_loc (loc, etype, low));
5688 :
5689 1474280 : if (operand_equal_p (low, high, 0))
5690 310460 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5691 310460 : fold_convert_loc (loc, etype, low));
5692 :
5693 1163820 : if (TREE_CODE (exp) == BIT_AND_EXPR
5694 1163820 : && maskable_range_p (low, high, etype, &mask, &value))
5695 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5696 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5697 : exp, mask),
5698 86 : value);
5699 :
5700 1163734 : if (integer_zerop (low))
5701 : {
5702 679375 : if (! TYPE_UNSIGNED (etype))
5703 : {
5704 178377 : etype = unsigned_type_for (etype);
5705 178377 : high = fold_convert_loc (loc, etype, high);
5706 178377 : exp = fold_convert_loc (loc, etype, exp);
5707 : }
5708 679375 : return build_range_check (loc, type, exp, 1, 0, high);
5709 : }
5710 :
5711 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5712 484359 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5713 : {
5714 125207 : int prec = TYPE_PRECISION (etype);
5715 :
5716 125207 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5717 : {
5718 102 : if (TYPE_UNSIGNED (etype))
5719 : {
5720 96 : tree signed_etype = signed_type_for (etype);
5721 96 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5722 0 : etype
5723 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5724 : else
5725 : etype = signed_etype;
5726 96 : exp = fold_convert_loc (loc, etype, exp);
5727 : }
5728 102 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5729 : build_int_cst (etype, 0));
5730 : }
5731 : }
5732 :
5733 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5734 : This requires wrap-around arithmetics for the type of the expression. */
5735 484257 : etype = range_check_type (etype);
5736 484257 : if (etype == NULL_TREE)
5737 : return NULL_TREE;
5738 :
5739 484257 : high = fold_convert_loc (loc, etype, high);
5740 484257 : low = fold_convert_loc (loc, etype, low);
5741 484257 : exp = fold_convert_loc (loc, etype, exp);
5742 :
5743 484257 : value = const_binop (MINUS_EXPR, high, low);
5744 :
5745 484257 : if (value != 0 && !TREE_OVERFLOW (value))
5746 484257 : return build_range_check (loc, type,
5747 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5748 : 1, build_int_cst (etype, 0), value);
5749 :
5750 : return 0;
5751 : }
5752 :
5753 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5754 :
5755 : static tree
5756 172523 : range_predecessor (tree val)
5757 : {
5758 172523 : tree type = TREE_TYPE (val);
5759 :
5760 172523 : if (INTEGRAL_TYPE_P (type)
5761 172523 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5762 : return 0;
5763 : else
5764 172523 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5765 172523 : build_int_cst (TREE_TYPE (val), 1), 0);
5766 : }
5767 :
5768 : /* Return the successor of VAL in its type, handling the infinite case. */
5769 :
5770 : static tree
5771 1614890 : range_successor (tree val)
5772 : {
5773 1614890 : tree type = TREE_TYPE (val);
5774 :
5775 1614890 : if (INTEGRAL_TYPE_P (type)
5776 1614890 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5777 : return 0;
5778 : else
5779 1614881 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5780 1614881 : build_int_cst (TREE_TYPE (val), 1), 0);
5781 : }
5782 :
5783 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5784 : can, 0 if we can't. Set the output range into the specified parameters. */
5785 :
5786 : bool
5787 3561285 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5788 : tree high0, int in1_p, tree low1, tree high1)
5789 : {
5790 3561285 : bool no_overlap;
5791 3561285 : int subset;
5792 3561285 : int temp;
5793 3561285 : tree tem;
5794 3561285 : int in_p;
5795 3561285 : tree low, high;
5796 3561285 : int lowequal = ((low0 == 0 && low1 == 0)
5797 3561285 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5798 3561285 : low0, 0, low1, 0)));
5799 3561285 : int highequal = ((high0 == 0 && high1 == 0)
5800 3561285 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5801 3561285 : high0, 1, high1, 1)));
5802 :
5803 : /* Make range 0 be the range that starts first, or ends last if they
5804 : start at the same value. Swap them if it isn't. */
5805 3561285 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5806 : low0, 0, low1, 0))
5807 3561285 : || (lowequal
5808 574502 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5809 : high1, 1, high0, 1))))
5810 : {
5811 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5812 : tem = low0, low0 = low1, low1 = tem;
5813 : tem = high0, high0 = high1, high1 = tem;
5814 : }
5815 :
5816 : /* If the second range is != high1 where high1 is the type maximum of
5817 : the type, try first merging with < high1 range. */
5818 3561285 : if (low1
5819 3561285 : && high1
5820 982677 : && TREE_CODE (low1) == INTEGER_CST
5821 982677 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5822 120051 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5823 159104 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5824 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5825 4503463 : && operand_equal_p (low1, high1, 0))
5826 : {
5827 546540 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5828 546540 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5829 : !in1_p, NULL_TREE, range_predecessor (low1)))
5830 : return true;
5831 : /* Similarly for the second range != low1 where low1 is the type minimum
5832 : of the type, try first merging with > low1 range. */
5833 438468 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5834 438468 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5835 : !in1_p, range_successor (low1), NULL_TREE))
5836 : return true;
5837 : }
5838 :
5839 : /* Now flag two cases, whether the ranges are disjoint or whether the
5840 : second range is totally subsumed in the first. Note that the tests
5841 : below are simplified by the ones above. */
5842 3360269 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5843 : high0, 1, low1, 0));
5844 3360269 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5845 : high1, 1, high0, 1));
5846 :
5847 : /* We now have four cases, depending on whether we are including or
5848 : excluding the two ranges. */
5849 3360269 : if (in0_p && in1_p)
5850 : {
5851 : /* If they don't overlap, the result is false. If the second range
5852 : is a subset it is the result. Otherwise, the range is from the start
5853 : of the second to the end of the first. */
5854 1531229 : if (no_overlap)
5855 : in_p = 0, low = high = 0;
5856 1529518 : else if (subset)
5857 : in_p = 1, low = low1, high = high1;
5858 : else
5859 1411951 : in_p = 1, low = low1, high = high0;
5860 : }
5861 :
5862 1829040 : else if (in0_p && ! in1_p)
5863 : {
5864 : /* If they don't overlap, the result is the first range. If they are
5865 : equal, the result is false. If the second range is a subset of the
5866 : first, and the ranges begin at the same place, we go from just after
5867 : the end of the second range to the end of the first. If the second
5868 : range is not a subset of the first, or if it is a subset and both
5869 : ranges end at the same place, the range starts at the start of the
5870 : first range and ends just before the second range.
5871 : Otherwise, we can't describe this as a single range. */
5872 317031 : if (no_overlap)
5873 : in_p = 1, low = low0, high = high0;
5874 311479 : else if (lowequal && highequal)
5875 : in_p = 0, low = high = 0;
5876 310684 : else if (subset && lowequal)
5877 : {
5878 235417 : low = range_successor (high1);
5879 235417 : high = high0;
5880 235417 : in_p = 1;
5881 235417 : if (low == 0)
5882 : {
5883 : /* We are in the weird situation where high0 > high1 but
5884 : high1 has no successor. Punt. */
5885 : return 0;
5886 : }
5887 : }
5888 75267 : else if (! subset || highequal)
5889 : {
5890 55486 : low = low0;
5891 55486 : high = range_predecessor (low1);
5892 55486 : in_p = 1;
5893 55486 : if (high == 0)
5894 : {
5895 : /* low0 < low1 but low1 has no predecessor. Punt. */
5896 : return 0;
5897 : }
5898 : }
5899 : else
5900 : return 0;
5901 : }
5902 :
5903 1512009 : else if (! in0_p && in1_p)
5904 : {
5905 : /* If they don't overlap, the result is the second range. If the second
5906 : is a subset of the first, the result is false. Otherwise,
5907 : the range starts just after the first range and ends at the
5908 : end of the second. */
5909 1153617 : if (no_overlap)
5910 : in_p = 1, low = low1, high = high1;
5911 1145623 : else if (subset || highequal)
5912 : in_p = 0, low = high = 0;
5913 : else
5914 : {
5915 1033810 : low = range_successor (high0);
5916 1033810 : high = high1;
5917 1033810 : in_p = 1;
5918 1033810 : if (low == 0)
5919 : {
5920 : /* high1 > high0 but high0 has no successor. Punt. */
5921 : return 0;
5922 : }
5923 : }
5924 : }
5925 :
5926 : else
5927 : {
5928 : /* The case where we are excluding both ranges. Here the complex case
5929 : is if they don't overlap. In that case, the only time we have a
5930 : range is if they are adjacent. If the second is a subset of the
5931 : first, the result is the first. Otherwise, the range to exclude
5932 : starts at the beginning of the first range and ends at the end of the
5933 : second. */
5934 358392 : if (no_overlap)
5935 : {
5936 252390 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5937 : range_successor (high0),
5938 : 1, low1, 0)))
5939 : in_p = 0, low = low0, high = high1;
5940 : else
5941 : {
5942 : /* Canonicalize - [min, x] into - [-, x]. */
5943 204724 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5944 203657 : switch (TREE_CODE (TREE_TYPE (low0)))
5945 : {
5946 47408 : case ENUMERAL_TYPE:
5947 47408 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5948 : GET_MODE_BITSIZE
5949 94816 : (TYPE_MODE (TREE_TYPE (low0)))))
5950 : break;
5951 : /* FALLTHROUGH */
5952 203456 : case INTEGER_TYPE:
5953 203456 : if (tree_int_cst_equal (low0,
5954 203456 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5955 6571 : low0 = 0;
5956 : break;
5957 201 : case POINTER_TYPE:
5958 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5959 201 : && integer_zerop (low0))
5960 : low0 = 0;
5961 : break;
5962 : default:
5963 : break;
5964 : }
5965 :
5966 : /* Canonicalize - [x, max] into - [x, -]. */
5967 204724 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5968 204522 : switch (TREE_CODE (TREE_TYPE (high1)))
5969 : {
5970 47416 : case ENUMERAL_TYPE:
5971 47416 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5972 : GET_MODE_BITSIZE
5973 94832 : (TYPE_MODE (TREE_TYPE (high1)))))
5974 : break;
5975 : /* FALLTHROUGH */
5976 204321 : case INTEGER_TYPE:
5977 204321 : if (tree_int_cst_equal (high1,
5978 204321 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5979 8972 : high1 = 0;
5980 : break;
5981 201 : case POINTER_TYPE:
5982 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5983 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5984 : high1, 1,
5985 201 : build_int_cst (TREE_TYPE (high1), 1),
5986 : 1)))
5987 133 : high1 = 0;
5988 : break;
5989 : default:
5990 : break;
5991 : }
5992 :
5993 : /* The ranges might be also adjacent between the maximum and
5994 : minimum values of the given type. For
5995 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5996 : return + [x + 1, y - 1]. */
5997 204724 : if (low0 == 0 && high1 == 0)
5998 : {
5999 329 : low = range_successor (high0);
6000 329 : high = range_predecessor (low1);
6001 329 : if (low == 0 || high == 0)
6002 : return 0;
6003 :
6004 : in_p = 1;
6005 : }
6006 : else
6007 : return 0;
6008 : }
6009 : }
6010 106002 : else if (subset)
6011 : in_p = 0, low = low0, high = high0;
6012 : else
6013 14810 : in_p = 0, low = low0, high = high1;
6014 : }
6015 :
6016 3136084 : *pin_p = in_p, *plow = low, *phigh = high;
6017 3136084 : return 1;
6018 : }
6019 :
6020 :
6021 : /* Subroutine of fold, looking inside expressions of the form
6022 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6023 : are the three operands of the COND_EXPR. This function is
6024 : being used also to optimize A op B ? C : A, by reversing the
6025 : comparison first.
6026 :
6027 : Return a folded expression whose code is not a COND_EXPR
6028 : anymore, or NULL_TREE if no folding opportunity is found. */
6029 :
6030 : static tree
6031 501282 : fold_cond_expr_with_comparison (location_t loc, tree type,
6032 : enum tree_code comp_code,
6033 : tree arg00, tree arg01, tree arg1, tree arg2)
6034 : {
6035 501282 : tree arg1_type = TREE_TYPE (arg1);
6036 501282 : tree tem;
6037 :
6038 501282 : STRIP_NOPS (arg1);
6039 501282 : STRIP_NOPS (arg2);
6040 :
6041 : /* If we have A op 0 ? A : -A, consider applying the following
6042 : transformations:
6043 :
6044 : A == 0? A : -A same as -A
6045 : A != 0? A : -A same as A
6046 : A >= 0? A : -A same as abs (A)
6047 : A > 0? A : -A same as abs (A)
6048 : A <= 0? A : -A same as -abs (A)
6049 : A < 0? A : -A same as -abs (A)
6050 :
6051 : None of these transformations work for modes with signed
6052 : zeros. If A is +/-0, the first two transformations will
6053 : change the sign of the result (from +0 to -0, or vice
6054 : versa). The last four will fix the sign of the result,
6055 : even though the original expressions could be positive or
6056 : negative, depending on the sign of A.
6057 :
6058 : Note that all these transformations are correct if A is
6059 : NaN, since the two alternatives (A and -A) are also NaNs. */
6060 501282 : if (!HONOR_SIGNED_ZEROS (type)
6061 1002574 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6062 501282 : ? real_zerop (arg01)
6063 500222 : : integer_zerop (arg01))
6064 1347497 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6065 1567 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6066 : /* In the case that A is of the form X-Y, '-A' (arg2) may
6067 : have already been folded to Y-X, check for that. */
6068 343590 : || (TREE_CODE (arg1) == MINUS_EXPR
6069 1718 : && TREE_CODE (arg2) == MINUS_EXPR
6070 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6071 0 : TREE_OPERAND (arg2, 1), 0)
6072 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6073 0 : TREE_OPERAND (arg2, 0), 0))))
6074 1343 : switch (comp_code)
6075 : {
6076 0 : case EQ_EXPR:
6077 0 : case UNEQ_EXPR:
6078 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6079 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6080 0 : case NE_EXPR:
6081 0 : case LTGT_EXPR:
6082 0 : return fold_convert_loc (loc, type, arg1);
6083 0 : case UNGE_EXPR:
6084 0 : case UNGT_EXPR:
6085 0 : if (flag_trapping_math)
6086 : break;
6087 : /* Fall through. */
6088 1327 : case GE_EXPR:
6089 1327 : case GT_EXPR:
6090 1327 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6091 : break;
6092 1311 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6093 1311 : return fold_convert_loc (loc, type, tem);
6094 0 : case UNLE_EXPR:
6095 0 : case UNLT_EXPR:
6096 0 : if (flag_trapping_math)
6097 : break;
6098 : /* FALLTHRU */
6099 16 : case LE_EXPR:
6100 16 : case LT_EXPR:
6101 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6102 : break;
6103 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6104 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6105 : {
6106 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6107 : is not, invokes UB both in abs and in the negation of it.
6108 : So, use ABSU_EXPR instead. */
6109 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6110 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6111 16 : tem = negate_expr (tem);
6112 16 : return fold_convert_loc (loc, type, tem);
6113 : }
6114 : else
6115 : {
6116 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6117 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6118 : }
6119 0 : default:
6120 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6121 : break;
6122 : }
6123 :
6124 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6125 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6126 : both transformations are correct when A is NaN: A != 0
6127 : is then true, and A == 0 is false. */
6128 :
6129 499955 : if (!HONOR_SIGNED_ZEROS (type)
6130 499955 : && integer_zerop (arg01) && integer_zerop (arg2))
6131 : {
6132 262167 : if (comp_code == NE_EXPR)
6133 145 : return fold_convert_loc (loc, type, arg1);
6134 262022 : else if (comp_code == EQ_EXPR)
6135 0 : return build_zero_cst (type);
6136 : }
6137 :
6138 : /* Try some transformations of A op B ? A : B.
6139 :
6140 : A == B? A : B same as B
6141 : A != B? A : B same as A
6142 : A >= B? A : B same as max (A, B)
6143 : A > B? A : B same as max (B, A)
6144 : A <= B? A : B same as min (A, B)
6145 : A < B? A : B same as min (B, A)
6146 :
6147 : As above, these transformations don't work in the presence
6148 : of signed zeros. For example, if A and B are zeros of
6149 : opposite sign, the first two transformations will change
6150 : the sign of the result. In the last four, the original
6151 : expressions give different results for (A=+0, B=-0) and
6152 : (A=-0, B=+0), but the transformed expressions do not.
6153 :
6154 : The first two transformations are correct if either A or B
6155 : is a NaN. In the first transformation, the condition will
6156 : be false, and B will indeed be chosen. In the case of the
6157 : second transformation, the condition A != B will be true,
6158 : and A will be chosen.
6159 :
6160 : The conversions to max() and min() are not correct if B is
6161 : a number and A is not. The conditions in the original
6162 : expressions will be false, so all four give B. The min()
6163 : and max() versions would give a NaN instead. */
6164 499810 : if (!HONOR_SIGNED_ZEROS (type)
6165 499810 : && operand_equal_for_comparison_p (arg01, arg2)
6166 : /* Avoid these transformations if the COND_EXPR may be used
6167 : as an lvalue in the C++ front-end. PR c++/19199. */
6168 770534 : && (in_gimple_form
6169 17132 : || VECTOR_TYPE_P (type)
6170 17070 : || (! lang_GNU_CXX ()
6171 14669 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6172 2401 : || ! maybe_lvalue_p (arg1)
6173 2380 : || ! maybe_lvalue_p (arg2)))
6174 : {
6175 269031 : tree comp_op0 = arg00;
6176 269031 : tree comp_op1 = arg01;
6177 269031 : tree comp_type = TREE_TYPE (comp_op0);
6178 :
6179 269031 : switch (comp_code)
6180 : {
6181 0 : case EQ_EXPR:
6182 0 : return fold_convert_loc (loc, type, arg2);
6183 1 : case NE_EXPR:
6184 1 : return fold_convert_loc (loc, type, arg1);
6185 5965 : case LE_EXPR:
6186 5965 : case LT_EXPR:
6187 5965 : case UNLE_EXPR:
6188 5965 : case UNLT_EXPR:
6189 : /* In C++ a ?: expression can be an lvalue, so put the
6190 : operand which will be used if they are equal first
6191 : so that we can convert this back to the
6192 : corresponding COND_EXPR. */
6193 5965 : if (!HONOR_NANS (arg1))
6194 : {
6195 5965 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6196 5965 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6197 5965 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6198 5965 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6199 4620 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6200 : comp_op1, comp_op0);
6201 5965 : return fold_convert_loc (loc, type, tem);
6202 : }
6203 : break;
6204 263065 : case GE_EXPR:
6205 263065 : case GT_EXPR:
6206 263065 : case UNGE_EXPR:
6207 263065 : case UNGT_EXPR:
6208 263065 : if (!HONOR_NANS (arg1))
6209 : {
6210 263063 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6211 263063 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6212 263063 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6213 263063 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6214 3642 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6215 : comp_op1, comp_op0);
6216 263063 : return fold_convert_loc (loc, type, tem);
6217 : }
6218 : break;
6219 0 : case UNEQ_EXPR:
6220 0 : if (!HONOR_NANS (arg1))
6221 0 : return fold_convert_loc (loc, type, arg2);
6222 : break;
6223 0 : case LTGT_EXPR:
6224 0 : if (!HONOR_NANS (arg1))
6225 0 : return fold_convert_loc (loc, type, arg1);
6226 : break;
6227 0 : default:
6228 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6229 : break;
6230 : }
6231 : }
6232 :
6233 : return NULL_TREE;
6234 : }
6235 :
6236 :
6237 :
6238 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6239 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6240 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6241 : false) >= 2)
6242 : #endif
6243 :
6244 : /* EXP is some logical combination of boolean tests. See if we can
6245 : merge it into some range test. Return the new tree if so. */
6246 :
6247 : static tree
6248 24642691 : fold_range_test (location_t loc, enum tree_code code, tree type,
6249 : tree op0, tree op1)
6250 : {
6251 24642691 : int or_op = (code == TRUTH_ORIF_EXPR
6252 24642691 : || code == TRUTH_OR_EXPR);
6253 24642691 : int in0_p, in1_p, in_p;
6254 24642691 : tree low0, low1, low, high0, high1, high;
6255 24642691 : tree tem, lhs, rhs;
6256 :
6257 24642691 : if (!INTEGRAL_TYPE_P (type))
6258 : return 0;
6259 :
6260 24642691 : lhs = make_range (op0, &in0_p, &low0, &high0);
6261 : /* If op0 is known true or false and this is a short-circuiting
6262 : operation we must not merge with op1 since that makes side-effects
6263 : unconditional. So special-case this. */
6264 24642691 : if (!lhs
6265 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6266 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6267 : return op0;
6268 24642689 : rhs = make_range (op1, &in1_p, &low1, &high1);
6269 :
6270 : /* If this is an OR operation, invert both sides; we will invert
6271 : again at the end. */
6272 24642689 : if (or_op)
6273 11595303 : in0_p = ! in0_p, in1_p = ! in1_p;
6274 :
6275 : /* If both expressions are the same, if we can merge the ranges, and we
6276 : can build the range test, return it or it inverted. If one of the
6277 : ranges is always true or always false, consider it to be the same
6278 : expression as the other. */
6279 24612491 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6280 1184112 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6281 : in1_p, low1, high1)
6282 25631899 : && (tem = (build_range_check (loc, type,
6283 : lhs != 0 ? lhs
6284 0 : : rhs != 0 ? rhs : integer_zero_node,
6285 : in_p, low, high))) != 0)
6286 : {
6287 989210 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6288 : }
6289 :
6290 : /* On machines where the branch cost is expensive, if this is a
6291 : short-circuited branch and the underlying object on both sides
6292 : is the same, make a non-short-circuit operation. */
6293 23653479 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6294 23653479 : if (param_logical_op_non_short_circuit != -1)
6295 7753 : logical_op_non_short_circuit
6296 7753 : = param_logical_op_non_short_circuit;
6297 23653479 : if (logical_op_non_short_circuit
6298 23649603 : && !sanitize_coverage_p ()
6299 23649600 : && lhs != 0 && rhs != 0
6300 23649292 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6301 28934038 : && operand_equal_p (lhs, rhs, 0))
6302 : {
6303 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6304 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6305 : which cases we can't do this. */
6306 165280 : if (simple_operand_p (lhs))
6307 66957 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6308 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6309 33992 : type, op0, op1);
6310 :
6311 131288 : else if (!lang_hooks.decls.global_bindings_p ()
6312 131288 : && !CONTAINS_PLACEHOLDER_P (lhs))
6313 : {
6314 130635 : tree common = save_expr (lhs);
6315 :
6316 240735 : if ((lhs = build_range_check (loc, type, common,
6317 110100 : or_op ? ! in0_p : in0_p,
6318 : low0, high0)) != 0
6319 240735 : && (rhs = build_range_check (loc, type, common,
6320 110100 : or_op ? ! in1_p : in1_p,
6321 : low1, high1)) != 0)
6322 : {
6323 240735 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6324 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6325 130635 : type, lhs, rhs);
6326 : }
6327 : }
6328 : }
6329 :
6330 : return 0;
6331 : }
6332 :
6333 : /* For an expression that has the form
6334 : (A && B) || ~B
6335 : or
6336 : (A || B) && ~B,
6337 : we can drop one of the inner expressions and simplify to
6338 : A || ~B
6339 : or
6340 : A && ~B
6341 : LOC is the location of the resulting expression. OP is the inner
6342 : logical operation; the left-hand side in the examples above, while CMPOP
6343 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6344 : removing a condition that guards another, as in
6345 : (A != NULL && A->...) || A == NULL
6346 : which we must not transform. If RHS_ONLY is true, only eliminate the
6347 : right-most operand of the inner logical operation. */
6348 :
6349 : static tree
6350 124225 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6351 : bool rhs_only)
6352 : {
6353 124225 : enum tree_code code = TREE_CODE (cmpop);
6354 124225 : enum tree_code truthop_code = TREE_CODE (op);
6355 124225 : tree lhs = TREE_OPERAND (op, 0);
6356 124225 : tree rhs = TREE_OPERAND (op, 1);
6357 124225 : tree orig_lhs = lhs, orig_rhs = rhs;
6358 124225 : enum tree_code rhs_code = TREE_CODE (rhs);
6359 124225 : enum tree_code lhs_code = TREE_CODE (lhs);
6360 124225 : enum tree_code inv_code;
6361 :
6362 124225 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6363 : return NULL_TREE;
6364 :
6365 111841 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6366 : return NULL_TREE;
6367 :
6368 38624 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6369 :
6370 38624 : if (rhs_code == truthop_code)
6371 : {
6372 33 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6373 33 : if (newrhs != NULL_TREE)
6374 : {
6375 0 : rhs = newrhs;
6376 0 : rhs_code = TREE_CODE (rhs);
6377 : }
6378 : }
6379 38624 : if (lhs_code == truthop_code && !rhs_only)
6380 : {
6381 472 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6382 472 : if (newlhs != NULL_TREE)
6383 : {
6384 0 : lhs = newlhs;
6385 0 : lhs_code = TREE_CODE (lhs);
6386 : }
6387 : }
6388 :
6389 38624 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6390 38624 : if (inv_code == rhs_code
6391 922 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6392 38660 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6393 : return lhs;
6394 38611 : if (!rhs_only && inv_code == lhs_code
6395 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6396 38703 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6397 : return rhs;
6398 38520 : if (rhs != orig_rhs || lhs != orig_lhs)
6399 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6400 0 : lhs, rhs);
6401 : return NULL_TREE;
6402 : }
6403 :
6404 : /* Find ways of folding logical expressions of LHS and RHS:
6405 : Try to merge two comparisons to the same innermost item.
6406 : Look for range tests like "ch >= '0' && ch <= '9'".
6407 : Look for combinations of simple terms on machines with expensive branches
6408 : and evaluate the RHS unconditionally.
6409 :
6410 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6411 : function and the one above.
6412 :
6413 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6414 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6415 :
6416 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6417 : two operands.
6418 :
6419 : We return the simplified tree or 0 if no optimization is possible. */
6420 :
6421 : static tree
6422 24308931 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6423 : tree lhs, tree rhs)
6424 : {
6425 : /* If this is the "or" of two comparisons, we can do something if
6426 : the comparisons are NE_EXPR. If this is the "and", we can do something
6427 : if the comparisons are EQ_EXPR. I.e.,
6428 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6429 :
6430 : WANTED_CODE is this operation code. For single bit fields, we can
6431 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6432 : comparison for one-bit fields. */
6433 :
6434 24308931 : enum tree_code lcode, rcode;
6435 24308931 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6436 24308931 : tree result;
6437 :
6438 : /* Start by getting the comparison codes. Fail if anything is volatile.
6439 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6440 : it were surrounded with a NE_EXPR. */
6441 :
6442 24308931 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6443 : return 0;
6444 :
6445 21575317 : lcode = TREE_CODE (lhs);
6446 21575317 : rcode = TREE_CODE (rhs);
6447 :
6448 21575317 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6449 : {
6450 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6451 0 : build_int_cst (TREE_TYPE (lhs), 0));
6452 0 : lcode = NE_EXPR;
6453 : }
6454 :
6455 21575317 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6456 : {
6457 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6458 0 : build_int_cst (TREE_TYPE (rhs), 0));
6459 0 : rcode = NE_EXPR;
6460 : }
6461 :
6462 21575317 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6463 19295540 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6464 : return 0;
6465 :
6466 18214261 : ll_arg = TREE_OPERAND (lhs, 0);
6467 18214261 : lr_arg = TREE_OPERAND (lhs, 1);
6468 18214261 : rl_arg = TREE_OPERAND (rhs, 0);
6469 18214261 : rr_arg = TREE_OPERAND (rhs, 1);
6470 :
6471 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6472 18214261 : if (simple_operand_p (ll_arg)
6473 18214261 : && simple_operand_p (lr_arg))
6474 : {
6475 14771947 : if (operand_equal_p (ll_arg, rl_arg, 0)
6476 14771947 : && operand_equal_p (lr_arg, rr_arg, 0))
6477 : {
6478 20713 : result = combine_comparisons (loc, code, lcode, rcode,
6479 : truth_type, ll_arg, lr_arg);
6480 20713 : if (result)
6481 : return result;
6482 : }
6483 14751234 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6484 14751234 : && operand_equal_p (lr_arg, rl_arg, 0))
6485 : {
6486 252 : result = combine_comparisons (loc, code, lcode,
6487 : swap_tree_comparison (rcode),
6488 : truth_type, ll_arg, lr_arg);
6489 252 : if (result)
6490 : return result;
6491 : }
6492 : }
6493 :
6494 8615106 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6495 18193841 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6496 :
6497 : /* If the RHS can be evaluated unconditionally and its operands are
6498 : simple, it wins to evaluate the RHS unconditionally on machines
6499 : with expensive branches. In this case, this isn't a comparison
6500 : that can be merged. */
6501 :
6502 18193841 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6503 : false) >= 2
6504 18193742 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6505 17180896 : && simple_operand_p (rl_arg)
6506 28118898 : && simple_operand_p (rr_arg))
6507 : {
6508 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6509 11085973 : if (code == TRUTH_OR_EXPR
6510 1501431 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6511 618535 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6512 23410 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6513 11105350 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6514 38138 : return build2_loc (loc, NE_EXPR, truth_type,
6515 19069 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6516 : ll_arg, rl_arg),
6517 19069 : build_int_cst (TREE_TYPE (ll_arg), 0));
6518 :
6519 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6520 11066904 : if (code == TRUTH_AND_EXPR
6521 1688675 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6522 814499 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6523 6799 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6524 11068459 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6525 2694 : return build2_loc (loc, EQ_EXPR, truth_type,
6526 1347 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6527 : ll_arg, rl_arg),
6528 1347 : build_int_cst (TREE_TYPE (ll_arg), 0));
6529 : }
6530 :
6531 : return 0;
6532 : }
6533 :
6534 : /* T is an integer expression that is being multiplied, divided, or taken a
6535 : modulus (CODE says which and what kind of divide or modulus) by a
6536 : constant C. See if we can eliminate that operation by folding it with
6537 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6538 : should be used for the computation if wider than our type.
6539 :
6540 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6541 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6542 : expression would not overflow or that overflow is undefined for the type
6543 : in the language in question.
6544 :
6545 : If we return a non-null expression, it is an equivalent form of the
6546 : original computation, but need not be in the original type. */
6547 :
6548 : static tree
6549 97827698 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6550 : {
6551 : /* To avoid exponential search depth, refuse to allow recursion past
6552 : three levels. Beyond that (1) it's highly unlikely that we'll find
6553 : something interesting and (2) we've probably processed it before
6554 : when we built the inner expression. */
6555 :
6556 97827698 : static int depth;
6557 97827698 : tree ret;
6558 :
6559 97827698 : if (depth > 3)
6560 : return NULL;
6561 :
6562 94477275 : depth++;
6563 94477275 : ret = extract_muldiv_1 (t, c, code, wide_type);
6564 94477275 : depth--;
6565 :
6566 94477275 : return ret;
6567 : }
6568 :
6569 : static tree
6570 94477275 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6571 : {
6572 94477275 : tree type = TREE_TYPE (t);
6573 94477275 : enum tree_code tcode = TREE_CODE (t);
6574 94477275 : tree ctype = type;
6575 94477275 : if (wide_type)
6576 : {
6577 31300799 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6578 : {
6579 119 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6580 8890437 : ctype = wide_type;
6581 : }
6582 31300680 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6583 62601360 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6584 8890437 : ctype = wide_type;
6585 : }
6586 94477275 : tree t1, t2;
6587 94477275 : bool same_p = tcode == code;
6588 94477275 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6589 :
6590 : /* Don't deal with constants of zero here; they confuse the code below. */
6591 94477275 : if (integer_zerop (c))
6592 : return NULL_TREE;
6593 :
6594 94463129 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6595 37683725 : op0 = TREE_OPERAND (t, 0);
6596 :
6597 94463129 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6598 11594147 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6599 :
6600 : /* Note that we need not handle conditional operations here since fold
6601 : already handles those cases. So just do arithmetic here. */
6602 94463129 : switch (tcode)
6603 : {
6604 4203354 : case INTEGER_CST:
6605 : /* For a constant, we can always simplify if we are a multiply
6606 : or (for divide and modulus) if it is a multiple of our constant. */
6607 4203354 : if (code == MULT_EXPR
6608 5389887 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6609 1186533 : TYPE_SIGN (type)))
6610 : {
6611 3430097 : tree tem = const_binop (code, fold_convert (ctype, t),
6612 : fold_convert (ctype, c));
6613 : /* If the multiplication overflowed, we lost information on it.
6614 : See PR68142 and PR69845. */
6615 3430097 : if (TREE_OVERFLOW (tem))
6616 : return NULL_TREE;
6617 : return tem;
6618 : }
6619 : break;
6620 :
6621 37045296 : CASE_CONVERT: case NON_LVALUE_EXPR:
6622 37045296 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6623 : break;
6624 : /* If op0 is an expression ... */
6625 35827305 : if ((COMPARISON_CLASS_P (op0)
6626 : || UNARY_CLASS_P (op0)
6627 35827305 : || BINARY_CLASS_P (op0)
6628 32882352 : || VL_EXP_CLASS_P (op0)
6629 32822906 : || EXPRESSION_CLASS_P (op0))
6630 : /* ... and has wrapping overflow, and its type is smaller
6631 : than ctype, then we cannot pass through as widening. */
6632 35976795 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6633 1077601 : && (TYPE_PRECISION (ctype)
6634 1077601 : > TYPE_PRECISION (TREE_TYPE (op0))))
6635 : /* ... or this is a truncation (t is narrower than op0),
6636 : then we cannot pass through this narrowing. */
6637 2550908 : || (TYPE_PRECISION (type)
6638 2550908 : < TYPE_PRECISION (TREE_TYPE (op0)))
6639 : /* ... or signedness changes for division or modulus,
6640 : then we cannot pass through this conversion. */
6641 2522051 : || (code != MULT_EXPR
6642 122548 : && (TYPE_UNSIGNED (ctype)
6643 122548 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6644 : /* ... or has undefined overflow while the converted to
6645 : type has not, we cannot do the operation in the inner type
6646 : as that would introduce undefined overflow. */
6647 2424976 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6648 1912535 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6649 : break;
6650 :
6651 : /* Pass the constant down and see if we can make a simplification. If
6652 : we can, replace this expression with the inner simplification for
6653 : possible later conversion to our or some other type. */
6654 33315586 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6655 33315586 : && TREE_CODE (t2) == INTEGER_CST
6656 33315586 : && !TREE_OVERFLOW (t2)
6657 67882728 : && (t1 = extract_muldiv (op0, t2, code,
6658 : code == MULT_EXPR ? ctype : NULL_TREE))
6659 : != 0)
6660 : return t1;
6661 : break;
6662 :
6663 185 : case ABS_EXPR:
6664 : /* If widening the type changes it from signed to unsigned, then we
6665 : must avoid building ABS_EXPR itself as unsigned. */
6666 185 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6667 : {
6668 0 : tree cstype = (*signed_type_for) (ctype);
6669 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6670 : {
6671 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6672 0 : return fold_convert (ctype, t1);
6673 : }
6674 : break;
6675 : }
6676 : /* If the constant is negative, we cannot simplify this. */
6677 185 : if (tree_int_cst_sgn (c) == -1)
6678 : break;
6679 : /* FALLTHROUGH */
6680 49505 : case NEGATE_EXPR:
6681 : /* For division and modulus, type can't be unsigned, as e.g.
6682 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6683 : For signed types, even with wrapping overflow, this is fine. */
6684 49505 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6685 : break;
6686 47996 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6687 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6688 : break;
6689 :
6690 785 : case MIN_EXPR: case MAX_EXPR:
6691 : /* If widening the type changes the signedness, then we can't perform
6692 : this optimization as that changes the result. */
6693 785 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6694 : break;
6695 :
6696 : /* Punt for multiplication altogether.
6697 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6698 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6699 : 0U, the latter is 2U.
6700 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6701 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6702 : well defined 0, the latter invokes UB.
6703 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6704 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6705 : well defined 25, the latter invokes UB. */
6706 785 : if (code == MULT_EXPR)
6707 : break;
6708 : /* For division/modulo, punt on c being -1 for MAX, as
6709 : MAX (INT_MIN, 0) / -1 is not equivalent to
6710 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6711 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6712 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6713 : transformation won't make it worse. */
6714 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6715 : break;
6716 :
6717 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6718 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6719 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6720 : {
6721 0 : if (tree_int_cst_sgn (c) < 0)
6722 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6723 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6724 : fold_convert (ctype, t2));
6725 : }
6726 : break;
6727 :
6728 1347 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6729 : /* If the second operand is constant, this is a multiplication
6730 : or floor division, by a power of two, so we can treat it that
6731 : way unless the multiplier or divisor overflows. Signed
6732 : left-shift overflow is implementation-defined rather than
6733 : undefined in C90, so do not convert signed left shift into
6734 : multiplication. */
6735 1347 : if (TREE_CODE (op1) == INTEGER_CST
6736 1331 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6737 : /* const_binop may not detect overflow correctly,
6738 : so check for it explicitly here. */
6739 1213 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6740 1356 : wi::to_wide (op1))
6741 1204 : && (t1 = fold_convert (ctype,
6742 : const_binop (LSHIFT_EXPR, size_one_node,
6743 : op1))) != 0
6744 2551 : && !TREE_OVERFLOW (t1))
6745 2206 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6746 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6747 : ctype,
6748 : fold_convert (ctype, op0),
6749 : t1),
6750 1204 : c, code, wide_type);
6751 : break;
6752 :
6753 8059686 : case PLUS_EXPR: case MINUS_EXPR:
6754 : /* See if we can eliminate the operation on both sides. If we can, we
6755 : can return a new PLUS or MINUS. If we can't, the only remaining
6756 : cases where we can do anything are if the second operand is a
6757 : constant. */
6758 8059686 : t1 = extract_muldiv (op0, c, code, wide_type);
6759 8059686 : t2 = extract_muldiv (op1, c, code, wide_type);
6760 809058 : if (t1 != 0 && t2 != 0
6761 275840 : && TYPE_OVERFLOW_WRAPS (ctype)
6762 8326290 : && (code == MULT_EXPR
6763 : /* If not multiplication, we can only do this if both operands
6764 : are divisible by c. */
6765 0 : || (multiple_of_p (ctype, op0, c)
6766 0 : && multiple_of_p (ctype, op1, c))))
6767 : {
6768 266604 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6769 : fold_convert (ctype, t2));
6770 : }
6771 :
6772 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6773 : This simplifies the logic below. */
6774 7793082 : if (tcode == MINUS_EXPR)
6775 : {
6776 2133366 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6777 : /* If OP1 was not easily negatable, the constant may be OP0. */
6778 2133366 : if (TREE_CODE (op0) == INTEGER_CST)
6779 : {
6780 359523 : std::swap (op0, op1);
6781 359523 : std::swap (t1, t2);
6782 : }
6783 : }
6784 :
6785 7793082 : if (TREE_CODE (op1) != INTEGER_CST)
6786 : break;
6787 :
6788 : /* If either OP1 or C are negative, this optimization is not safe for
6789 : some of the division and remainder types while for others we need
6790 : to change the code. */
6791 3538524 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6792 : {
6793 174754 : if (code == CEIL_DIV_EXPR)
6794 : code = FLOOR_DIV_EXPR;
6795 174752 : else if (code == FLOOR_DIV_EXPR)
6796 : code = CEIL_DIV_EXPR;
6797 174339 : else if (code != MULT_EXPR
6798 174339 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6799 : break;
6800 : }
6801 :
6802 : /* If it's a multiply or a division/modulus operation of a multiple
6803 : of our constant, do the operation and verify it doesn't overflow. */
6804 3533320 : if (code == MULT_EXPR
6805 4715385 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6806 1182065 : TYPE_SIGN (type)))
6807 : {
6808 2760758 : op1 = const_binop (code, fold_convert (ctype, op1),
6809 : fold_convert (ctype, c));
6810 : /* We allow the constant to overflow with wrapping semantics. */
6811 2760758 : if (op1 == 0
6812 2760758 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6813 : break;
6814 : }
6815 : else
6816 : break;
6817 :
6818 : /* If we have an unsigned type, we cannot widen the operation since it
6819 : will change the result if the original computation overflowed. */
6820 2757418 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6821 : break;
6822 :
6823 : /* The last case is if we are a multiply. In that case, we can
6824 : apply the distributive law to commute the multiply and addition
6825 : if the multiplication of the constants doesn't overflow
6826 : and overflow is defined. With undefined overflow
6827 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6828 : But fold_plusminus_mult_expr would factor back any power-of-two
6829 : value so do not distribute in the first place in this case. */
6830 2757418 : if (code == MULT_EXPR
6831 2348650 : && TYPE_OVERFLOW_WRAPS (ctype)
6832 4771671 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6833 547140 : return fold_build2 (tcode, ctype,
6834 : fold_build2 (code, ctype,
6835 : fold_convert (ctype, op0),
6836 : fold_convert (ctype, c)),
6837 : op1);
6838 :
6839 : break;
6840 :
6841 2208302 : case MULT_EXPR:
6842 : /* We have a special case here if we are doing something like
6843 : (C * 8) % 4 since we know that's zero. */
6844 2208302 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6845 2208302 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6846 : /* If the multiplication can overflow we cannot optimize this. */
6847 10982 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6848 338 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6849 2219284 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6850 293 : TYPE_SIGN (type)))
6851 : {
6852 8 : return omit_one_operand (type, integer_zero_node, op0);
6853 : }
6854 :
6855 : /* ... fall through ... */
6856 :
6857 2490205 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6858 2490205 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6859 : /* If we can extract our operation from the LHS, do so and return a
6860 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6861 : do something only if the second operand is a constant. */
6862 2490205 : if (same_p
6863 2075503 : && TYPE_OVERFLOW_WRAPS (ctype)
6864 4388242 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6865 65876 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6866 : fold_convert (ctype, op1));
6867 2424329 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6868 2000499 : && TYPE_OVERFLOW_WRAPS (ctype)
6869 4247410 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6870 935025 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6871 : fold_convert (ctype, t1));
6872 1489304 : else if (TREE_CODE (op1) != INTEGER_CST)
6873 : return 0;
6874 :
6875 : /* If these are the same operation types, we can associate them
6876 : assuming no overflow. */
6877 615015 : if (tcode == code)
6878 : {
6879 200847 : bool overflow_p = false;
6880 200847 : wi::overflow_type overflow_mul;
6881 200847 : signop sign = TYPE_SIGN (ctype);
6882 200847 : unsigned prec = TYPE_PRECISION (ctype);
6883 401694 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6884 200847 : wi::to_wide (c, prec),
6885 200847 : sign, &overflow_mul);
6886 200847 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6887 200847 : if (overflow_mul
6888 1279 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6889 : overflow_p = true;
6890 200780 : if (!overflow_p)
6891 200780 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6892 : wide_int_to_tree (ctype, mul));
6893 200847 : }
6894 :
6895 : /* If these operations "cancel" each other, we have the main
6896 : optimizations of this pass, which occur when either constant is a
6897 : multiple of the other, in which case we replace this with either an
6898 : operation or CODE or TCODE.
6899 :
6900 : If we have an unsigned type, we cannot do this since it will change
6901 : the result if the original computation overflowed. */
6902 414235 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6903 96770 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6904 510962 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6905 96687 : || (tcode == MULT_EXPR
6906 96687 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6907 851 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6908 847 : && code != MULT_EXPR)))
6909 : {
6910 881 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6911 881 : TYPE_SIGN (type)))
6912 : {
6913 105 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6914 : fold_convert (ctype,
6915 : const_binop (TRUNC_DIV_EXPR,
6916 : op1, c)));
6917 : }
6918 776 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6919 776 : TYPE_SIGN (type)))
6920 : {
6921 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6922 : fold_convert (ctype,
6923 : const_binop (TRUNC_DIV_EXPR,
6924 : c, op1)));
6925 : }
6926 : }
6927 : break;
6928 :
6929 : default:
6930 : break;
6931 : }
6932 :
6933 : return 0;
6934 : }
6935 :
6936 : /* Return a node which has the indicated constant VALUE (either 0 or
6937 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6938 : and is of the indicated TYPE. */
6939 :
6940 : tree
6941 98022930 : constant_boolean_node (bool value, tree type)
6942 : {
6943 98022930 : if (type == integer_type_node)
6944 20037607 : return value ? integer_one_node : integer_zero_node;
6945 77985323 : else if (type == boolean_type_node)
6946 73766553 : return value ? boolean_true_node : boolean_false_node;
6947 4218770 : else if (VECTOR_TYPE_P (type))
6948 1423 : return build_vector_from_val (type,
6949 1423 : build_int_cst (TREE_TYPE (type),
6950 2053 : value ? -1 : 0));
6951 : else
6952 4217347 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6953 : }
6954 :
6955 :
6956 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6957 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6958 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6959 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6960 : COND is the first argument to CODE; otherwise (as in the example
6961 : given here), it is the second argument. TYPE is the type of the
6962 : original expression. Return NULL_TREE if no simplification is
6963 : possible. */
6964 :
6965 : static tree
6966 945000 : fold_binary_op_with_conditional_arg (location_t loc,
6967 : enum tree_code code,
6968 : tree type, tree op0, tree op1,
6969 : tree cond, tree arg, int cond_first_p)
6970 : {
6971 945000 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6972 945000 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6973 945000 : tree test, true_value, false_value;
6974 945000 : tree lhs = NULL_TREE;
6975 945000 : tree rhs = NULL_TREE;
6976 945000 : enum tree_code cond_code = COND_EXPR;
6977 :
6978 : /* Do not move possibly trapping operations into the conditional as this
6979 : pessimizes code and causes gimplification issues when applied late. */
6980 964177 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6981 189530 : ANY_INTEGRAL_TYPE_P (type)
6982 948189 : && TYPE_OVERFLOW_TRAPS (type), op1))
6983 : return NULL_TREE;
6984 :
6985 925659 : if (TREE_CODE (cond) == COND_EXPR
6986 327680 : || TREE_CODE (cond) == VEC_COND_EXPR)
6987 : {
6988 601005 : test = TREE_OPERAND (cond, 0);
6989 601005 : true_value = TREE_OPERAND (cond, 1);
6990 601005 : false_value = TREE_OPERAND (cond, 2);
6991 : /* If this operand throws an expression, then it does not make
6992 : sense to try to perform a logical or arithmetic operation
6993 : involving it. */
6994 601005 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
6995 7463 : lhs = true_value;
6996 601005 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
6997 6 : rhs = false_value;
6998 : }
6999 324654 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7000 324500 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7001 : {
7002 322688 : tree testtype = TREE_TYPE (cond);
7003 322688 : test = cond;
7004 322688 : true_value = constant_boolean_node (true, testtype);
7005 322688 : false_value = constant_boolean_node (false, testtype);
7006 : }
7007 : else
7008 : /* Detect the case of mixing vector and scalar types - bail out. */
7009 : return NULL_TREE;
7010 :
7011 923693 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7012 3180 : cond_code = VEC_COND_EXPR;
7013 :
7014 : /* This transformation is only worthwhile if we don't have to wrap ARG
7015 : in a SAVE_EXPR and the operation can be simplified without recursing
7016 : on at least one of the branches once its pushed inside the COND_EXPR. */
7017 923693 : if (!TREE_CONSTANT (arg)
7018 923693 : && (TREE_SIDE_EFFECTS (arg)
7019 442533 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7020 438091 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7021 : return NULL_TREE;
7022 :
7023 497019 : arg = fold_convert_loc (loc, arg_type, arg);
7024 497019 : if (lhs == 0)
7025 : {
7026 490988 : true_value = fold_convert_loc (loc, cond_type, true_value);
7027 490988 : if (cond_first_p)
7028 481116 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7029 : else
7030 9872 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7031 : }
7032 497019 : if (rhs == 0)
7033 : {
7034 497013 : false_value = fold_convert_loc (loc, cond_type, false_value);
7035 497013 : if (cond_first_p)
7036 486580 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7037 : else
7038 10433 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7039 : }
7040 :
7041 : /* Check that we have simplified at least one of the branches. */
7042 497019 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7043 : return NULL_TREE;
7044 :
7045 476961 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7046 : }
7047 :
7048 :
7049 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7050 :
7051 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7052 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7053 : if ARG - ZERO_ARG is the same as X.
7054 :
7055 : If ARG is NULL, check for any value of type TYPE.
7056 :
7057 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7058 : and finite. The problematic cases are when X is zero, and its mode
7059 : has signed zeros. In the case of rounding towards -infinity,
7060 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7061 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7062 :
7063 : bool
7064 643435 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7065 : const_tree zero_arg, int negate)
7066 : {
7067 643435 : if (!real_zerop (zero_arg))
7068 : return false;
7069 :
7070 : /* Don't allow the fold with -fsignaling-nans. */
7071 642754 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7072 : return false;
7073 :
7074 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7075 639416 : if (!HONOR_SIGNED_ZEROS (type))
7076 : return true;
7077 :
7078 : /* There is no case that is safe for all rounding modes. */
7079 622486 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7080 : return false;
7081 :
7082 : /* In a vector or complex, we would need to check the sign of all zeros. */
7083 621823 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7084 1708 : zero_arg = uniform_vector_p (zero_arg);
7085 621823 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7086 1178 : return false;
7087 :
7088 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7089 620645 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7090 252 : negate = !negate;
7091 :
7092 : /* The mode has signed zeros, and we have to honor their sign.
7093 : In this situation, there are only two cases we can return true for.
7094 : (i) X - 0 is the same as X with default rounding.
7095 : (ii) X + 0 is X when X can't possibly be -0.0. */
7096 620645 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7097 : }
7098 :
7099 : /* Subroutine of match.pd that optimizes comparisons of a division by
7100 : a nonzero integer constant against an integer constant, i.e.
7101 : X/C1 op C2.
7102 :
7103 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7104 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7105 :
7106 : enum tree_code
7107 1781590 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7108 : tree *hi, bool *neg_overflow)
7109 : {
7110 1781590 : tree prod, tmp, type = TREE_TYPE (c1);
7111 1781590 : signop sign = TYPE_SIGN (type);
7112 1781590 : wi::overflow_type overflow;
7113 :
7114 : /* We have to do this the hard way to detect unsigned overflow.
7115 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7116 1781590 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7117 1781590 : prod = force_fit_type (type, val, -1, overflow);
7118 1781590 : *neg_overflow = false;
7119 :
7120 1781590 : if (sign == UNSIGNED)
7121 : {
7122 1751968 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7123 1751968 : *lo = prod;
7124 :
7125 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7126 1751968 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7127 1751968 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7128 : }
7129 29622 : else if (tree_int_cst_sgn (c1) >= 0)
7130 : {
7131 28217 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7132 28217 : switch (tree_int_cst_sgn (c2))
7133 : {
7134 4842 : case -1:
7135 4842 : *neg_overflow = true;
7136 4842 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7137 4842 : *hi = prod;
7138 4842 : break;
7139 :
7140 14862 : case 0:
7141 14862 : *lo = fold_negate_const (tmp, type);
7142 14862 : *hi = tmp;
7143 14862 : break;
7144 :
7145 8513 : case 1:
7146 8513 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7147 8513 : *lo = prod;
7148 8513 : break;
7149 :
7150 0 : default:
7151 0 : gcc_unreachable ();
7152 : }
7153 : }
7154 : else
7155 : {
7156 : /* A negative divisor reverses the relational operators. */
7157 1405 : code = swap_tree_comparison (code);
7158 :
7159 1405 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7160 1405 : switch (tree_int_cst_sgn (c2))
7161 : {
7162 134 : case -1:
7163 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7164 134 : *lo = prod;
7165 134 : break;
7166 :
7167 173 : case 0:
7168 173 : *hi = fold_negate_const (tmp, type);
7169 173 : *lo = tmp;
7170 173 : break;
7171 :
7172 1098 : case 1:
7173 1098 : *neg_overflow = true;
7174 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7175 1098 : *hi = prod;
7176 1098 : break;
7177 :
7178 0 : default:
7179 0 : gcc_unreachable ();
7180 : }
7181 : }
7182 :
7183 1781590 : if (code != EQ_EXPR && code != NE_EXPR)
7184 : return code;
7185 :
7186 16676 : if (TREE_OVERFLOW (*lo)
7187 16676 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7188 702 : *lo = NULL_TREE;
7189 16676 : if (TREE_OVERFLOW (*hi)
7190 16676 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7191 95 : *hi = NULL_TREE;
7192 :
7193 : return code;
7194 1781590 : }
7195 :
7196 : /* Test whether it is preferable to swap two operands, ARG0 and
7197 : ARG1, for example because ARG0 is an integer constant and ARG1
7198 : isn't. */
7199 :
7200 : bool
7201 1564368220 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7202 : {
7203 1564368220 : if (CONSTANT_CLASS_P (arg1))
7204 : return false;
7205 508798118 : if (CONSTANT_CLASS_P (arg0))
7206 : return true;
7207 :
7208 468221342 : STRIP_NOPS (arg0);
7209 468221342 : STRIP_NOPS (arg1);
7210 :
7211 468221342 : if (TREE_CONSTANT (arg1))
7212 : return false;
7213 453694442 : if (TREE_CONSTANT (arg0))
7214 : return true;
7215 :
7216 : /* Put addresses in arg1. */
7217 452952806 : if (TREE_CODE (arg1) == ADDR_EXPR)
7218 : return false;
7219 436055304 : if (TREE_CODE (arg0) == ADDR_EXPR)
7220 : return true;
7221 :
7222 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7223 : for commutative and comparison operators. Ensuring a canonical
7224 : form allows the optimizers to find additional redundancies without
7225 : having to explicitly check for both orderings. */
7226 435712445 : if (TREE_CODE (arg0) == SSA_NAME
7227 329164076 : && TREE_CODE (arg1) == SSA_NAME
7228 759067173 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7229 : return true;
7230 :
7231 : /* Put SSA_NAMEs last. */
7232 413176697 : if (TREE_CODE (arg1) == SSA_NAME)
7233 : return false;
7234 97841170 : if (TREE_CODE (arg0) == SSA_NAME)
7235 : return true;
7236 :
7237 : /* Put variables last. */
7238 92031822 : if (DECL_P (arg1))
7239 : return false;
7240 50115380 : if (DECL_P (arg0))
7241 : return true;
7242 :
7243 : return false;
7244 : }
7245 :
7246 :
7247 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7248 : means A >= Y && A != MAX, but in this case we know that
7249 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7250 :
7251 : static tree
7252 23605946 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7253 : {
7254 23605946 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7255 :
7256 23605946 : if (TREE_CODE (bound) == LT_EXPR)
7257 4971063 : a = TREE_OPERAND (bound, 0);
7258 18634883 : else if (TREE_CODE (bound) == GT_EXPR)
7259 2759011 : a = TREE_OPERAND (bound, 1);
7260 : else
7261 : return NULL_TREE;
7262 :
7263 7730074 : typea = TREE_TYPE (a);
7264 7730074 : if (!INTEGRAL_TYPE_P (typea)
7265 470447 : && !POINTER_TYPE_P (typea))
7266 : return NULL_TREE;
7267 :
7268 7556773 : if (TREE_CODE (ineq) == LT_EXPR)
7269 : {
7270 1470175 : a1 = TREE_OPERAND (ineq, 1);
7271 1470175 : y = TREE_OPERAND (ineq, 0);
7272 : }
7273 6086598 : else if (TREE_CODE (ineq) == GT_EXPR)
7274 : {
7275 1153252 : a1 = TREE_OPERAND (ineq, 0);
7276 1153252 : y = TREE_OPERAND (ineq, 1);
7277 : }
7278 : else
7279 : return NULL_TREE;
7280 :
7281 2623427 : if (TREE_TYPE (a1) != typea)
7282 : return NULL_TREE;
7283 :
7284 1874978 : if (POINTER_TYPE_P (typea))
7285 : {
7286 : /* Convert the pointer types into integer before taking the difference. */
7287 10441 : tree ta = fold_convert_loc (loc, ssizetype, a);
7288 10441 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7289 10441 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7290 : }
7291 : else
7292 1864537 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7293 :
7294 1874978 : if (!diff || !integer_onep (diff))
7295 1864763 : return NULL_TREE;
7296 :
7297 10215 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7298 : }
7299 :
7300 : /* Fold a sum or difference of at least one multiplication.
7301 : Returns the folded tree or NULL if no simplification could be made. */
7302 :
7303 : static tree
7304 8854304 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7305 : tree arg0, tree arg1)
7306 : {
7307 8854304 : tree arg00, arg01, arg10, arg11;
7308 8854304 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7309 :
7310 : /* (A * C) +- (B * C) -> (A+-B) * C.
7311 : (A * C) +- A -> A * (C+-1).
7312 : We are most concerned about the case where C is a constant,
7313 : but other combinations show up during loop reduction. Since
7314 : it is not difficult, try all four possibilities. */
7315 :
7316 8854304 : if (TREE_CODE (arg0) == MULT_EXPR)
7317 : {
7318 7825805 : arg00 = TREE_OPERAND (arg0, 0);
7319 7825805 : arg01 = TREE_OPERAND (arg0, 1);
7320 : }
7321 1028499 : else if (TREE_CODE (arg0) == INTEGER_CST)
7322 : {
7323 74099 : arg00 = build_one_cst (type);
7324 74099 : arg01 = arg0;
7325 : }
7326 : else
7327 : {
7328 : /* We cannot generate constant 1 for fract. */
7329 954400 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7330 0 : return NULL_TREE;
7331 954400 : arg00 = arg0;
7332 954400 : arg01 = build_one_cst (type);
7333 : }
7334 8854304 : if (TREE_CODE (arg1) == MULT_EXPR)
7335 : {
7336 2403540 : arg10 = TREE_OPERAND (arg1, 0);
7337 2403540 : arg11 = TREE_OPERAND (arg1, 1);
7338 : }
7339 6450764 : else if (TREE_CODE (arg1) == INTEGER_CST)
7340 : {
7341 3459040 : arg10 = build_one_cst (type);
7342 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7343 : the purpose of this canonicalization. */
7344 6693289 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7345 228079 : && negate_expr_p (arg1)
7346 3683831 : && code == PLUS_EXPR)
7347 : {
7348 224791 : arg11 = negate_expr (arg1);
7349 224791 : code = MINUS_EXPR;
7350 : }
7351 : else
7352 : arg11 = arg1;
7353 : }
7354 : else
7355 : {
7356 : /* We cannot generate constant 1 for fract. */
7357 2991724 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7358 0 : return NULL_TREE;
7359 2991724 : arg10 = arg1;
7360 2991724 : arg11 = build_one_cst (type);
7361 : }
7362 8854304 : same = NULL_TREE;
7363 :
7364 : /* Prefer factoring a common non-constant. */
7365 8854304 : if (operand_equal_p (arg00, arg10, 0))
7366 : same = arg00, alt0 = arg01, alt1 = arg11;
7367 8850252 : else if (operand_equal_p (arg01, arg11, 0))
7368 : same = arg01, alt0 = arg00, alt1 = arg10;
7369 8747748 : else if (operand_equal_p (arg00, arg11, 0))
7370 : same = arg00, alt0 = arg01, alt1 = arg10;
7371 8747710 : else if (operand_equal_p (arg01, arg10, 0))
7372 : same = arg01, alt0 = arg00, alt1 = arg11;
7373 :
7374 : /* No identical multiplicands; see if we can find a common
7375 : power-of-two factor in non-power-of-two multiplies. This
7376 : can help in multi-dimensional array access. */
7377 8742899 : else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7378 : {
7379 7327758 : HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7380 7327758 : HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7381 7327758 : HOST_WIDE_INT tmp;
7382 7327758 : bool swap = false;
7383 7327758 : tree maybe_same;
7384 :
7385 : /* Move min of absolute values to int11. */
7386 7327758 : if (absu_hwi (int01) < absu_hwi (int11))
7387 : {
7388 : tmp = int01, int01 = int11, int11 = tmp;
7389 : alt0 = arg00, arg00 = arg10, arg10 = alt0;
7390 : maybe_same = arg01;
7391 : swap = true;
7392 : }
7393 : else
7394 3746404 : maybe_same = arg11;
7395 :
7396 7327758 : const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7397 7327758 : if (factor > 1
7398 9676791 : && pow2p_hwi (factor)
7399 2100428 : && (int01 & (factor - 1)) == 0
7400 : /* The remainder should not be a constant, otherwise we
7401 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7402 : increased the number of multiplications necessary. */
7403 8626934 : && TREE_CODE (arg10) != INTEGER_CST)
7404 : {
7405 1166536 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7406 1166536 : build_int_cst (TREE_TYPE (arg00),
7407 1166536 : int01 / int11));
7408 1166536 : alt1 = arg10;
7409 1166536 : same = maybe_same;
7410 1166536 : if (swap)
7411 1054707 : maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7412 : }
7413 : }
7414 :
7415 1277941 : if (!same)
7416 7576363 : return NULL_TREE;
7417 :
7418 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7419 1277941 : || TYPE_OVERFLOW_WRAPS (type)
7420 : /* We are neither factoring zero nor minus one. */
7421 1402831 : || TREE_CODE (same) == INTEGER_CST)
7422 1266237 : return fold_build2_loc (loc, MULT_EXPR, type,
7423 : fold_build2_loc (loc, code, type,
7424 : fold_convert_loc (loc, type, alt0),
7425 : fold_convert_loc (loc, type, alt1)),
7426 1266237 : fold_convert_loc (loc, type, same));
7427 :
7428 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7429 : same may be minus one and thus the multiplication may overflow. Perform
7430 : the sum operation in an unsigned type. */
7431 11704 : tree utype = unsigned_type_for (type);
7432 11704 : tree tem = fold_build2_loc (loc, code, utype,
7433 : fold_convert_loc (loc, utype, alt0),
7434 : fold_convert_loc (loc, utype, alt1));
7435 : /* If the sum evaluated to a constant that is not -INF the multiplication
7436 : cannot overflow. */
7437 23408 : if (TREE_CODE (tem) == INTEGER_CST
7438 18366 : && (wi::to_wide (tem)
7439 18366 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7440 3318 : return fold_build2_loc (loc, MULT_EXPR, type,
7441 3318 : fold_convert (type, tem), same);
7442 :
7443 : /* Do not resort to unsigned multiplication because
7444 : we lose the no-overflow property of the expression. */
7445 : return NULL_TREE;
7446 : }
7447 :
7448 :
7449 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7450 : into the buffer PTR of length LEN bytes.
7451 : Return the number of bytes placed in the buffer, or zero
7452 : upon failure. */
7453 :
7454 : int
7455 54338111 : native_encode_wide_int (tree type, const wide_int_ref &val,
7456 : unsigned char *ptr, int len, int off)
7457 : {
7458 54338111 : int total_bytes;
7459 54338111 : if (BITINT_TYPE_P (type))
7460 : {
7461 17350 : struct bitint_info info;
7462 17350 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7463 17350 : gcc_assert (ok);
7464 17350 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7465 17350 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7466 : {
7467 17045 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7468 : /* More work is needed when adding _BitInt support to PDP endian
7469 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7470 : match target endianity here. */
7471 17045 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7472 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7473 : || (GET_MODE_SIZE (limb_mode)
7474 : >= UNITS_PER_WORD)));
7475 : }
7476 : else
7477 610 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7478 : }
7479 : else
7480 108641522 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7481 54338111 : int byte, offset, word, words;
7482 54338111 : unsigned char value;
7483 :
7484 54338111 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7485 : return 0;
7486 54337623 : if (off == -1)
7487 53421192 : off = 0;
7488 :
7489 54337623 : if (ptr == NULL)
7490 : /* Dry run. */
7491 2781263 : return MIN (len, total_bytes - off);
7492 :
7493 : words = total_bytes / UNITS_PER_WORD;
7494 :
7495 249059031 : for (byte = 0; byte < total_bytes; byte++)
7496 : {
7497 197502671 : int bitpos = byte * BITS_PER_UNIT;
7498 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7499 : number of bytes. */
7500 197502671 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7501 :
7502 197502671 : if (total_bytes > UNITS_PER_WORD)
7503 : {
7504 197502671 : word = byte / UNITS_PER_WORD;
7505 197502671 : if (WORDS_BIG_ENDIAN)
7506 : word = (words - 1) - word;
7507 197502671 : offset = word * UNITS_PER_WORD;
7508 197502671 : if (BYTES_BIG_ENDIAN)
7509 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7510 : else
7511 197502671 : offset += byte % UNITS_PER_WORD;
7512 : }
7513 : else
7514 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7515 197502671 : if (offset >= off && offset - off < len)
7516 196206196 : ptr[offset - off] = value;
7517 : }
7518 51556360 : return MIN (len, total_bytes - off);
7519 : }
7520 :
7521 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7522 : specified by EXPR into the buffer PTR of length LEN bytes.
7523 : Return the number of bytes placed in the buffer, or zero
7524 : upon failure. */
7525 :
7526 : static int
7527 54338111 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7528 : {
7529 54338111 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7530 54338111 : ptr, len, off);
7531 : }
7532 :
7533 :
7534 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7535 : specified by EXPR into the buffer PTR of length LEN bytes.
7536 : Return the number of bytes placed in the buffer, or zero
7537 : upon failure. */
7538 :
7539 : static int
7540 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7541 : {
7542 0 : tree type = TREE_TYPE (expr);
7543 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7544 0 : int total_bytes = GET_MODE_SIZE (mode);
7545 0 : FIXED_VALUE_TYPE value;
7546 0 : tree i_value, i_type;
7547 :
7548 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7549 : return 0;
7550 :
7551 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7552 :
7553 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7554 : return 0;
7555 :
7556 0 : value = TREE_FIXED_CST (expr);
7557 0 : i_value = double_int_to_tree (i_type, value.data);
7558 :
7559 0 : return native_encode_int (i_value, ptr, len, off);
7560 : }
7561 :
7562 :
7563 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7564 : specified by EXPR into the buffer PTR of length LEN bytes.
7565 : Return the number of bytes placed in the buffer, or zero
7566 : upon failure. */
7567 :
7568 : int
7569 845662 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7570 : unsigned char *ptr, int len, int off)
7571 : {
7572 845662 : int total_bytes = GET_MODE_SIZE (mode);
7573 845662 : int byte, offset, word, words, bitpos;
7574 845662 : unsigned char value;
7575 :
7576 : /* There are always 32 bits in each long, no matter the size of
7577 : the hosts long. We handle floating point representations with
7578 : up to 192 bits. */
7579 845662 : long tmp[6];
7580 :
7581 845662 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7582 : return 0;
7583 843458 : if (off == -1)
7584 699921 : off = 0;
7585 :
7586 843458 : if (ptr == NULL)
7587 : /* Dry run. */
7588 133523 : return MIN (len, total_bytes - off);
7589 :
7590 709935 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7591 :
7592 709935 : real_to_target (tmp, val, mode);
7593 :
7594 6827961 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7595 6118026 : bitpos += BITS_PER_UNIT)
7596 : {
7597 6118026 : byte = (bitpos / BITS_PER_UNIT) & 3;
7598 6118026 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7599 :
7600 6118026 : if (UNITS_PER_WORD < 4)
7601 : {
7602 : word = byte / UNITS_PER_WORD;
7603 : if (WORDS_BIG_ENDIAN)
7604 : word = (words - 1) - word;
7605 : offset = word * UNITS_PER_WORD;
7606 : if (BYTES_BIG_ENDIAN)
7607 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7608 : else
7609 : offset += byte % UNITS_PER_WORD;
7610 : }
7611 : else
7612 : {
7613 6118026 : offset = byte;
7614 6118026 : if (BYTES_BIG_ENDIAN)
7615 : {
7616 : /* Reverse bytes within each long, or within the entire float
7617 : if it's smaller than a long (for HFmode). */
7618 : offset = MIN (3, total_bytes - 1) - offset;
7619 : gcc_assert (offset >= 0);
7620 : }
7621 : }
7622 6118026 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7623 6118026 : if (offset >= off
7624 6114786 : && offset - off < len)
7625 6093094 : ptr[offset - off] = value;
7626 : }
7627 709935 : return MIN (len, total_bytes - off);
7628 : }
7629 :
7630 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7631 : specified by EXPR into the buffer PTR of length LEN bytes.
7632 : Return the number of bytes placed in the buffer, or zero
7633 : upon failure. */
7634 :
7635 : static int
7636 16172 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7637 : {
7638 16172 : int rsize, isize;
7639 16172 : tree part;
7640 :
7641 16172 : part = TREE_REALPART (expr);
7642 16172 : rsize = native_encode_expr (part, ptr, len, off);
7643 16172 : if (off == -1 && rsize == 0)
7644 : return 0;
7645 16172 : part = TREE_IMAGPART (expr);
7646 16172 : if (off != -1)
7647 32403 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7648 16172 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7649 : len - rsize, off);
7650 16172 : if (off == -1 && isize != rsize)
7651 : return 0;
7652 16172 : return rsize + isize;
7653 : }
7654 :
7655 : /* Like native_encode_vector, but only encode the first COUNT elements.
7656 : The other arguments are as for native_encode_vector. */
7657 :
7658 : static int
7659 938176 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7660 : int off, unsigned HOST_WIDE_INT count)
7661 : {
7662 938176 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7663 1876352 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7664 939194 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7665 : {
7666 : /* This is the only case in which elements can be smaller than a byte.
7667 : Element 0 is always in the lsb of the containing byte. */
7668 940 : unsigned int elt_bits = TYPE_PRECISION (itype);
7669 940 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7670 940 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7671 : return 0;
7672 :
7673 940 : if (off == -1)
7674 940 : off = 0;
7675 :
7676 : /* Zero the buffer and then set bits later where necessary. */
7677 940 : int extract_bytes = MIN (len, total_bytes - off);
7678 940 : if (ptr)
7679 940 : memset (ptr, 0, extract_bytes);
7680 :
7681 940 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7682 940 : unsigned int first_elt = off * elts_per_byte;
7683 940 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7684 940 : unsigned int elt_mask = (1 << elt_bits) - 1;
7685 17333 : for (unsigned int i = 0; i < extract_elts; ++i)
7686 : {
7687 16393 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7688 16393 : if (TREE_CODE (elt) != INTEGER_CST)
7689 : return 0;
7690 :
7691 16393 : if (ptr && integer_nonzerop (elt))
7692 : {
7693 8441 : unsigned int bit = i * elt_bits;
7694 8441 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7695 : }
7696 : }
7697 : return extract_bytes;
7698 : }
7699 :
7700 937236 : int offset = 0;
7701 937236 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7702 3317685 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7703 : {
7704 2940647 : if (off >= size)
7705 : {
7706 22207 : off -= size;
7707 22207 : continue;
7708 : }
7709 2918440 : tree elem = VECTOR_CST_ELT (expr, i);
7710 2918440 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7711 : len - offset, off);
7712 2918440 : if ((off == -1 && res != size) || res == 0)
7713 : return 0;
7714 2917911 : offset += res;
7715 2917911 : if (offset >= len)
7716 559669 : return (off == -1 && i < count - 1) ? 0 : offset;
7717 2358242 : if (off != -1)
7718 393615 : off = 0;
7719 : }
7720 : return offset;
7721 : }
7722 :
7723 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7724 : specified by EXPR into the buffer PTR of length LEN bytes.
7725 : Return the number of bytes placed in the buffer, or zero
7726 : upon failure. */
7727 :
7728 : static int
7729 807775 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7730 : {
7731 807775 : unsigned HOST_WIDE_INT count;
7732 807775 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7733 : return 0;
7734 807775 : return native_encode_vector_part (expr, ptr, len, off, count);
7735 : }
7736 :
7737 :
7738 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7739 : specified by EXPR into the buffer PTR of length LEN bytes.
7740 : Return the number of bytes placed in the buffer, or zero
7741 : upon failure. */
7742 :
7743 : static int
7744 142635 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7745 : {
7746 142635 : tree type = TREE_TYPE (expr);
7747 :
7748 : /* Wide-char strings are encoded in target byte-order so native
7749 : encoding them is trivial. */
7750 142635 : if (BITS_PER_UNIT != CHAR_BIT
7751 142635 : || TREE_CODE (type) != ARRAY_TYPE
7752 142635 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7753 285270 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7754 : return 0;
7755 :
7756 142635 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7757 142635 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7758 : return 0;
7759 141767 : if (off == -1)
7760 56149 : off = 0;
7761 141767 : len = MIN (total_bytes - off, len);
7762 141767 : if (ptr == NULL)
7763 : /* Dry run. */;
7764 : else
7765 : {
7766 141767 : int written = 0;
7767 141767 : if (off < TREE_STRING_LENGTH (expr))
7768 : {
7769 141294 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7770 141294 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7771 : }
7772 141767 : memset (ptr + written, 0, len - written);
7773 : }
7774 : return len;
7775 : }
7776 :
7777 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7778 : specified by EXPR into the buffer PTR of length LEN bytes.
7779 : Return the number of bytes placed in the buffer, or zero
7780 : upon failure. */
7781 :
7782 : static int
7783 46093 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7784 : {
7785 : /* We are only concerned with zero-initialization constructors here. That's
7786 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7787 : deal with. For more general handling of constructors, there is
7788 : native_encode_initializer. */
7789 46093 : if (CONSTRUCTOR_NELTS (expr))
7790 : return 0;
7791 :
7792 : /* Wide-char strings are encoded in target byte-order so native
7793 : encoding them is trivial. */
7794 85940 : if (BITS_PER_UNIT != CHAR_BIT
7795 42970 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7796 : return 0;
7797 :
7798 42970 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7799 42970 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7800 : return 0;
7801 42970 : if (off == -1)
7802 0 : off = 0;
7803 42970 : len = MIN (total_bytes - off, len);
7804 42970 : if (ptr == NULL)
7805 : /* Dry run. */;
7806 : else
7807 42970 : memset (ptr, 0, len);
7808 : return len;
7809 : }
7810 :
7811 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7812 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7813 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7814 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7815 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7816 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7817 : Return the number of bytes placed in the buffer, or zero upon failure. */
7818 :
7819 : int
7820 69938659 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7821 : {
7822 : /* We don't support starting at negative offset and -1 is special. */
7823 69938659 : if (off < -1)
7824 : return 0;
7825 :
7826 69938647 : switch (TREE_CODE (expr))
7827 : {
7828 54335865 : case INTEGER_CST:
7829 54335865 : return native_encode_int (expr, ptr, len, off);
7830 :
7831 845662 : case REAL_CST:
7832 845662 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7833 1691324 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7834 :
7835 0 : case FIXED_CST:
7836 0 : return native_encode_fixed (expr, ptr, len, off);
7837 :
7838 16172 : case COMPLEX_CST:
7839 16172 : return native_encode_complex (expr, ptr, len, off);
7840 :
7841 807775 : case VECTOR_CST:
7842 807775 : return native_encode_vector (expr, ptr, len, off);
7843 :
7844 142635 : case STRING_CST:
7845 142635 : return native_encode_string (expr, ptr, len, off);
7846 :
7847 46093 : case CONSTRUCTOR:
7848 46093 : return native_encode_constructor (expr, ptr, len, off);
7849 :
7850 : default:
7851 : return 0;
7852 : }
7853 : }
7854 :
7855 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7856 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7857 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7858 : machine modes, we can't just use build_nonstandard_integer_type. */
7859 :
7860 : tree
7861 541 : find_bitfield_repr_type (int fieldsize, int len)
7862 : {
7863 541 : machine_mode mode;
7864 1063 : for (int pass = 0; pass < 2; pass++)
7865 : {
7866 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7867 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7868 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7869 7286 : && known_eq (GET_MODE_PRECISION (mode),
7870 : GET_MODE_BITSIZE (mode))
7871 11274 : && known_le (GET_MODE_SIZE (mode), len))
7872 : {
7873 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7874 280 : if (ret && TYPE_MODE (ret) == mode)
7875 : return ret;
7876 : }
7877 : }
7878 :
7879 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7880 261 : if (int_n_enabled_p[i]
7881 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7882 261 : && int_n_trees[i].unsigned_type)
7883 : {
7884 261 : tree ret = int_n_trees[i].unsigned_type;
7885 261 : mode = TYPE_MODE (ret);
7886 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7887 522 : && known_eq (GET_MODE_PRECISION (mode),
7888 : GET_MODE_BITSIZE (mode))
7889 783 : && known_le (GET_MODE_SIZE (mode), len))
7890 : return ret;
7891 : }
7892 :
7893 : return NULL_TREE;
7894 : }
7895 :
7896 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7897 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7898 : to be non-NULL and OFF zero), then in addition to filling the
7899 : bytes pointed by PTR with the value also clear any bits pointed
7900 : by MASK that are known to be initialized, keep them as is for
7901 : e.g. uninitialized padding bits or uninitialized fields. */
7902 :
7903 : int
7904 48105349 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7905 : int off, unsigned char *mask)
7906 : {
7907 48105349 : int r;
7908 :
7909 : /* We don't support starting at negative offset and -1 is special. */
7910 48105349 : if (off < -1 || init == NULL_TREE)
7911 : return 0;
7912 :
7913 48105349 : gcc_assert (mask == NULL || (off == 0 && ptr));
7914 :
7915 48105349 : STRIP_NOPS (init);
7916 48105349 : switch (TREE_CODE (init))
7917 : {
7918 0 : case VIEW_CONVERT_EXPR:
7919 0 : case NON_LVALUE_EXPR:
7920 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7921 0 : mask);
7922 45982519 : default:
7923 45982519 : r = native_encode_expr (init, ptr, len, off);
7924 45982519 : if (mask)
7925 1487 : memset (mask, 0, r);
7926 : return r;
7927 2122830 : case CONSTRUCTOR:
7928 2122830 : tree type = TREE_TYPE (init);
7929 2122830 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7930 2122830 : if (total_bytes < 0)
7931 : return 0;
7932 2122830 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7933 : return 0;
7934 2122795 : int o = off == -1 ? 0 : off;
7935 2122795 : if (TREE_CODE (type) == ARRAY_TYPE)
7936 : {
7937 307404 : tree min_index;
7938 307404 : unsigned HOST_WIDE_INT cnt;
7939 307404 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7940 307404 : constructor_elt *ce;
7941 :
7942 307404 : if (!TYPE_DOMAIN (type)
7943 307404 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7944 : return 0;
7945 :
7946 307404 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7947 307404 : if (fieldsize <= 0)
7948 : return 0;
7949 :
7950 307404 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7951 307404 : if (ptr)
7952 307404 : memset (ptr, '\0', MIN (total_bytes - off, len));
7953 :
7954 45441725 : for (cnt = 0; ; cnt++)
7955 : {
7956 45749129 : tree val = NULL_TREE, index = NULL_TREE;
7957 45749129 : HOST_WIDE_INT pos = curpos, count = 0;
7958 45749129 : bool full = false;
7959 45749129 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7960 : {
7961 45640982 : val = ce->value;
7962 45640982 : index = ce->index;
7963 : }
7964 108147 : else if (mask == NULL
7965 287 : || CONSTRUCTOR_NO_CLEARING (init)
7966 108434 : || curpos >= total_bytes)
7967 : break;
7968 : else
7969 : pos = total_bytes;
7970 :
7971 45640982 : if (index && TREE_CODE (index) == RANGE_EXPR)
7972 : {
7973 16 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
7974 16 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
7975 0 : return 0;
7976 16 : offset_int wpos
7977 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
7978 32 : - wi::to_offset (min_index),
7979 16 : TYPE_PRECISION (sizetype));
7980 16 : wpos *= fieldsize;
7981 16 : if (!wi::fits_shwi_p (pos))
7982 : return 0;
7983 16 : pos = wpos.to_shwi ();
7984 16 : offset_int wcount
7985 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
7986 32 : - wi::to_offset (TREE_OPERAND (index, 0)),
7987 16 : TYPE_PRECISION (sizetype));
7988 16 : if (!wi::fits_shwi_p (wcount))
7989 : return 0;
7990 16 : count = wcount.to_shwi ();
7991 16 : }
7992 45074762 : else if (index)
7993 : {
7994 45074762 : if (TREE_CODE (index) != INTEGER_CST)
7995 0 : return 0;
7996 45074762 : offset_int wpos
7997 45074762 : = wi::sext (wi::to_offset (index)
7998 90149524 : - wi::to_offset (min_index),
7999 45074762 : TYPE_PRECISION (sizetype));
8000 45074762 : wpos *= fieldsize;
8001 45074762 : if (!wi::fits_shwi_p (wpos))
8002 : return 0;
8003 45074762 : pos = wpos.to_shwi ();
8004 : }
8005 :
8006 45641753 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8007 : {
8008 40 : if (valueinit == -1)
8009 : {
8010 40 : tree zero = build_zero_cst (TREE_TYPE (type));
8011 80 : r = native_encode_initializer (zero, ptr + curpos,
8012 : fieldsize, 0,
8013 40 : mask + curpos);
8014 40 : if (TREE_CODE (zero) == CONSTRUCTOR)
8015 0 : ggc_free (zero);
8016 40 : if (!r)
8017 : return 0;
8018 40 : valueinit = curpos;
8019 40 : curpos += fieldsize;
8020 : }
8021 70 : while (curpos != pos)
8022 : {
8023 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8024 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8025 30 : curpos += fieldsize;
8026 : }
8027 : }
8028 :
8029 45641022 : curpos = pos;
8030 45641022 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8031 : {
8032 488 : if (count)
8033 : return 0;
8034 488 : if (off == -1
8035 488 : || (curpos >= off
8036 0 : && (curpos + RAW_DATA_LENGTH (val)
8037 0 : <= (HOST_WIDE_INT) off + len)))
8038 : {
8039 488 : if (ptr)
8040 488 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8041 488 : RAW_DATA_LENGTH (val));
8042 488 : if (mask)
8043 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8044 : }
8045 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8046 0 : && curpos < (HOST_WIDE_INT) off + len)
8047 : {
8048 : /* Partial overlap. */
8049 0 : unsigned char *p = NULL;
8050 0 : int no = 0;
8051 0 : int l;
8052 0 : gcc_assert (mask == NULL);
8053 0 : if (curpos >= off)
8054 : {
8055 0 : if (ptr)
8056 0 : p = ptr + curpos - off;
8057 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8058 : RAW_DATA_LENGTH (val));
8059 : }
8060 : else
8061 : {
8062 0 : p = ptr;
8063 0 : no = off - curpos;
8064 0 : l = len;
8065 : }
8066 0 : if (p)
8067 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8068 : }
8069 488 : curpos += RAW_DATA_LENGTH (val);
8070 488 : val = NULL_TREE;
8071 : }
8072 488 : if (val)
8073 45718534 : do
8074 : {
8075 45718534 : if (off == -1
8076 671901 : || (curpos >= off
8077 221933 : && (curpos + fieldsize
8078 221933 : <= (HOST_WIDE_INT) off + len)))
8079 : {
8080 45237235 : if (full)
8081 : {
8082 78040 : if (ptr)
8083 78040 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8084 : fieldsize);
8085 78040 : if (mask)
8086 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8087 : }
8088 90509683 : else if (!native_encode_initializer (val,
8089 : ptr
8090 45159195 : ? ptr + curpos - o
8091 : : NULL,
8092 : fieldsize,
8093 : off == -1 ? -1
8094 : : 0,
8095 : mask
8096 691 : ? mask + curpos
8097 : : NULL))
8098 : return 0;
8099 : else
8100 : {
8101 : full = true;
8102 : pos = curpos;
8103 : }
8104 : }
8105 481299 : else if (curpos + fieldsize > off
8106 33804 : && curpos < (HOST_WIDE_INT) off + len)
8107 : {
8108 : /* Partial overlap. */
8109 9299 : unsigned char *p = NULL;
8110 9299 : int no = 0;
8111 9299 : int l;
8112 9299 : gcc_assert (mask == NULL);
8113 9299 : if (curpos >= off)
8114 : {
8115 6826 : if (ptr)
8116 6826 : p = ptr + curpos - off;
8117 6826 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8118 : fieldsize);
8119 : }
8120 : else
8121 : {
8122 2473 : p = ptr;
8123 2473 : no = off - curpos;
8124 2473 : l = len;
8125 : }
8126 9299 : if (!native_encode_initializer (val, p, l, no, NULL))
8127 : return 0;
8128 : }
8129 45519237 : curpos += fieldsize;
8130 : }
8131 45519237 : while (count-- != 0);
8132 45441725 : }
8133 108107 : return MIN (total_bytes - off, len);
8134 : }
8135 1815391 : else if (TREE_CODE (type) == RECORD_TYPE
8136 1815391 : || TREE_CODE (type) == UNION_TYPE)
8137 : {
8138 1815391 : unsigned HOST_WIDE_INT cnt;
8139 1815391 : constructor_elt *ce;
8140 1815391 : tree fld_base = TYPE_FIELDS (type);
8141 1815391 : tree to_free = NULL_TREE;
8142 :
8143 1815391 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8144 1815391 : if (ptr != NULL)
8145 1815391 : memset (ptr, '\0', MIN (total_bytes - o, len));
8146 338993 : for (cnt = 0; ; cnt++)
8147 : {
8148 2154384 : tree val = NULL_TREE, field = NULL_TREE;
8149 2154384 : HOST_WIDE_INT pos = 0, fieldsize;
8150 2154384 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8151 :
8152 2154384 : if (to_free)
8153 : {
8154 0 : ggc_free (to_free);
8155 0 : to_free = NULL_TREE;
8156 : }
8157 :
8158 2154384 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8159 : {
8160 367525 : val = ce->value;
8161 367525 : field = ce->index;
8162 367525 : if (field == NULL_TREE)
8163 : return 0;
8164 :
8165 367525 : pos = int_byte_position (field);
8166 367525 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8167 1496 : continue;
8168 : }
8169 1786859 : else if (mask == NULL
8170 1786859 : || CONSTRUCTOR_NO_CLEARING (init))
8171 : break;
8172 : else
8173 : pos = total_bytes;
8174 :
8175 368094 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8176 : {
8177 : tree fld;
8178 10265 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8179 : {
8180 9864 : if (TREE_CODE (fld) != FIELD_DECL)
8181 8803 : continue;
8182 1061 : if (fld == field)
8183 : break;
8184 146 : if (DECL_PADDING_P (fld))
8185 87 : continue;
8186 59 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8187 59 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8188 : return 0;
8189 59 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8190 2 : continue;
8191 : break;
8192 : }
8193 1373 : if (fld == NULL_TREE)
8194 : {
8195 401 : if (ce == NULL)
8196 : break;
8197 : return 0;
8198 : }
8199 972 : fld_base = DECL_CHAIN (fld);
8200 972 : if (fld != field)
8201 : {
8202 57 : cnt--;
8203 57 : field = fld;
8204 57 : pos = int_byte_position (field);
8205 57 : val = build_zero_cst (TREE_TYPE (fld));
8206 57 : if (TREE_CODE (val) == CONSTRUCTOR)
8207 0 : to_free = val;
8208 : }
8209 : }
8210 :
8211 366086 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8212 13978 : && TYPE_DOMAIN (TREE_TYPE (field))
8213 380064 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8214 : {
8215 81 : if (mask || off != -1)
8216 : return 0;
8217 81 : if (val == NULL_TREE)
8218 0 : continue;
8219 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8220 : return 0;
8221 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8222 81 : if (fieldsize < 0
8223 81 : || (int) fieldsize != fieldsize
8224 81 : || (pos + fieldsize) > INT_MAX)
8225 : return 0;
8226 81 : if (pos + fieldsize > total_bytes)
8227 : {
8228 81 : if (ptr != NULL && total_bytes < len)
8229 81 : memset (ptr + total_bytes, '\0',
8230 81 : MIN (pos + fieldsize, len) - total_bytes);
8231 : total_bytes = pos + fieldsize;
8232 : }
8233 : }
8234 : else
8235 : {
8236 366005 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8237 366005 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8238 : return 0;
8239 366005 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8240 : }
8241 366086 : if (fieldsize == 0)
8242 1 : continue;
8243 :
8244 : /* Prepare to deal with integral bit-fields and filter out other
8245 : bit-fields that do not start and end on a byte boundary. */
8246 366085 : if (DECL_BIT_FIELD (field))
8247 : {
8248 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8249 : return 0;
8250 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8251 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8252 : {
8253 2711 : bpos %= BITS_PER_UNIT;
8254 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8255 2711 : epos = fieldsize % BITS_PER_UNIT;
8256 2711 : fieldsize += BITS_PER_UNIT - 1;
8257 2711 : fieldsize /= BITS_PER_UNIT;
8258 : }
8259 0 : else if (bpos % BITS_PER_UNIT
8260 0 : || DECL_SIZE (field) == NULL_TREE
8261 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8262 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8263 : return 0;
8264 : }
8265 :
8266 366085 : if (off != -1 && pos + fieldsize <= off)
8267 3408 : continue;
8268 :
8269 362677 : if (val == NULL_TREE)
8270 0 : continue;
8271 :
8272 362677 : if (DECL_BIT_FIELD (field)
8273 362677 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8274 : {
8275 : /* FIXME: Handle PDP endian. */
8276 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8277 261 : return 0;
8278 :
8279 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8280 6 : val = TREE_OPERAND (val, 0);
8281 2507 : if (TREE_CODE (val) != INTEGER_CST)
8282 : return 0;
8283 :
8284 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8285 2507 : tree repr_type = NULL_TREE;
8286 2507 : HOST_WIDE_INT rpos = 0;
8287 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8288 : {
8289 1978 : rpos = int_byte_position (repr);
8290 1978 : repr_type = TREE_TYPE (repr);
8291 : }
8292 : else
8293 : {
8294 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8295 529 : if (repr_type == NULL_TREE)
8296 : return 0;
8297 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8298 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8299 268 : if (pos + repr_size <= o + len)
8300 : rpos = pos;
8301 : else
8302 : {
8303 14 : rpos = o + len - repr_size;
8304 14 : gcc_assert (rpos <= pos);
8305 : }
8306 : }
8307 :
8308 2246 : if (rpos > pos)
8309 : return 0;
8310 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8311 2246 : int diff = (TYPE_PRECISION (repr_type)
8312 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8313 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8314 2246 : if (!BYTES_BIG_ENDIAN)
8315 2246 : w = wi::lshift (w, bitoff);
8316 : else
8317 : w = wi::lshift (w, diff - bitoff);
8318 2246 : val = wide_int_to_tree (repr_type, w);
8319 :
8320 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8321 : / BITS_PER_UNIT + 1];
8322 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8323 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8324 0 : return 0;
8325 :
8326 2246 : if (ptr == NULL)
8327 0 : continue;
8328 :
8329 : /* If the bitfield does not start at byte boundary, handle
8330 : the partial byte at the start. */
8331 2246 : if (bpos
8332 1351 : && (off == -1 || (pos >= off && len >= 1)))
8333 : {
8334 1276 : if (!BYTES_BIG_ENDIAN)
8335 : {
8336 1276 : int msk = (1 << bpos) - 1;
8337 1276 : buf[pos - rpos] &= ~msk;
8338 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8339 1276 : if (mask)
8340 : {
8341 147 : if (fieldsize > 1 || epos == 0)
8342 129 : mask[pos] &= msk;
8343 : else
8344 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8345 : }
8346 : }
8347 : else
8348 : {
8349 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8350 : buf[pos - rpos] &= msk;
8351 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8352 : if (mask)
8353 : {
8354 : if (fieldsize > 1 || epos == 0)
8355 : mask[pos] &= ~msk;
8356 : else
8357 : mask[pos] &= (~msk
8358 : | ((1 << (BITS_PER_UNIT - epos))
8359 : - 1));
8360 : }
8361 : }
8362 : }
8363 : /* If the bitfield does not end at byte boundary, handle
8364 : the partial byte at the end. */
8365 2246 : if (epos
8366 1724 : && (off == -1
8367 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8368 : {
8369 1621 : if (!BYTES_BIG_ENDIAN)
8370 : {
8371 1621 : int msk = (1 << epos) - 1;
8372 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8373 1621 : buf[pos - rpos + fieldsize - 1]
8374 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8375 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8376 156 : mask[pos + fieldsize - 1] &= ~msk;
8377 : }
8378 : else
8379 : {
8380 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8381 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8382 : buf[pos - rpos + fieldsize - 1]
8383 : |= ptr[pos + fieldsize - 1 - o] & msk;
8384 : if (mask && (fieldsize > 1 || bpos == 0))
8385 : mask[pos + fieldsize - 1] &= msk;
8386 : }
8387 : }
8388 2246 : if (off == -1
8389 1301 : || (pos >= off
8390 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8391 : {
8392 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8393 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8394 75 : memset (mask + pos + (bpos != 0), 0,
8395 75 : fieldsize - (bpos != 0) - (epos != 0));
8396 : }
8397 : else
8398 : {
8399 : /* Partial overlap. */
8400 191 : HOST_WIDE_INT fsz = fieldsize;
8401 191 : gcc_assert (mask == NULL);
8402 191 : if (pos < off)
8403 : {
8404 89 : fsz -= (off - pos);
8405 89 : pos = off;
8406 : }
8407 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8408 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8409 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8410 : }
8411 2246 : continue;
8412 2246 : }
8413 :
8414 360170 : if (off == -1
8415 19942 : || (pos >= off
8416 19112 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8417 : {
8418 350637 : int fldsize = fieldsize;
8419 10409 : if (off == -1)
8420 : {
8421 340228 : tree fld = DECL_CHAIN (field);
8422 5087403 : while (fld)
8423 : {
8424 4762940 : if (TREE_CODE (fld) == FIELD_DECL)
8425 : break;
8426 4747175 : fld = DECL_CHAIN (fld);
8427 : }
8428 340228 : if (fld == NULL_TREE)
8429 324463 : fldsize = len - pos;
8430 : }
8431 361934 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8432 : : NULL,
8433 : fldsize,
8434 : off == -1 ? -1 : 0,
8435 888 : mask ? mask + pos : NULL);
8436 350637 : if (!r)
8437 : return 0;
8438 330821 : if (off == -1
8439 328816 : && fldsize != fieldsize
8440 620 : && r > fieldsize
8441 327 : && pos + r > total_bytes)
8442 338993 : total_bytes = pos + r;
8443 : }
8444 : else
8445 : {
8446 : /* Partial overlap. */
8447 9533 : unsigned char *p = NULL;
8448 9533 : int no = 0;
8449 9533 : int l;
8450 9533 : gcc_assert (mask == NULL);
8451 9533 : if (pos >= off)
8452 : {
8453 8703 : if (ptr)
8454 8703 : p = ptr + pos - off;
8455 8703 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8456 : fieldsize);
8457 : }
8458 : else
8459 : {
8460 830 : p = ptr;
8461 830 : no = off - pos;
8462 830 : l = len;
8463 : }
8464 9533 : if (!native_encode_initializer (val, p, l, no, NULL))
8465 : return 0;
8466 : }
8467 338993 : }
8468 1786802 : return MIN (total_bytes - off, len);
8469 : }
8470 : return 0;
8471 : }
8472 : }
8473 :
8474 :
8475 : /* Subroutine of native_interpret_expr. Interpret the contents of
8476 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8477 : If the buffer cannot be interpreted, return NULL_TREE. */
8478 :
8479 : static tree
8480 2711647 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8481 : {
8482 2711647 : int total_bytes;
8483 2711647 : if (BITINT_TYPE_P (type))
8484 : {
8485 31 : struct bitint_info info;
8486 31 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8487 31 : gcc_assert (ok);
8488 31 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8489 31 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8490 : {
8491 31 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8492 : /* More work is needed when adding _BitInt support to PDP endian
8493 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8494 : match target endianity here. */
8495 31 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8496 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8497 : || (GET_MODE_SIZE (limb_mode)
8498 : >= UNITS_PER_WORD)));
8499 : }
8500 : else
8501 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8502 : }
8503 : else
8504 5423232 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8505 :
8506 2711647 : if (total_bytes > len)
8507 : return NULL_TREE;
8508 :
8509 2711407 : wide_int result = wi::from_buffer (ptr, total_bytes);
8510 :
8511 2711407 : return wide_int_to_tree (type, result);
8512 2711407 : }
8513 :
8514 :
8515 : /* Subroutine of native_interpret_expr. Interpret the contents of
8516 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8517 : If the buffer cannot be interpreted, return NULL_TREE. */
8518 :
8519 : static tree
8520 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8521 : {
8522 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8523 0 : int total_bytes = GET_MODE_SIZE (mode);
8524 0 : double_int result;
8525 0 : FIXED_VALUE_TYPE fixed_value;
8526 :
8527 0 : if (total_bytes > len
8528 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8529 : return NULL_TREE;
8530 :
8531 0 : result = double_int::from_buffer (ptr, total_bytes);
8532 0 : fixed_value = fixed_from_double_int (result, mode);
8533 :
8534 0 : return build_fixed (type, fixed_value);
8535 : }
8536 :
8537 :
8538 : /* Subroutine of native_interpret_expr. Interpret the contents of
8539 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8540 : If the buffer cannot be interpreted, return NULL_TREE. */
8541 :
8542 : tree
8543 35336 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8544 : {
8545 35336 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8546 35336 : int total_bytes = GET_MODE_SIZE (mode);
8547 35336 : unsigned char value;
8548 : /* There are always 32 bits in each long, no matter the size of
8549 : the hosts long. We handle floating point representations with
8550 : up to 192 bits. */
8551 35336 : REAL_VALUE_TYPE r;
8552 35336 : long tmp[6];
8553 :
8554 35336 : if (total_bytes > len || total_bytes > 24)
8555 : return NULL_TREE;
8556 35275 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8557 :
8558 35275 : memset (tmp, 0, sizeof (tmp));
8559 260059 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8560 224784 : bitpos += BITS_PER_UNIT)
8561 : {
8562 : /* Both OFFSET and BYTE index within a long;
8563 : bitpos indexes the whole float. */
8564 224784 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8565 224784 : if (UNITS_PER_WORD < 4)
8566 : {
8567 : int word = byte / UNITS_PER_WORD;
8568 : if (WORDS_BIG_ENDIAN)
8569 : word = (words - 1) - word;
8570 : offset = word * UNITS_PER_WORD;
8571 : if (BYTES_BIG_ENDIAN)
8572 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8573 : else
8574 : offset += byte % UNITS_PER_WORD;
8575 : }
8576 : else
8577 : {
8578 224784 : offset = byte;
8579 224784 : if (BYTES_BIG_ENDIAN)
8580 : {
8581 : /* Reverse bytes within each long, or within the entire float
8582 : if it's smaller than a long (for HFmode). */
8583 : offset = MIN (3, total_bytes - 1) - offset;
8584 : gcc_assert (offset >= 0);
8585 : }
8586 : }
8587 224784 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8588 :
8589 224784 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8590 : }
8591 :
8592 35275 : real_from_target (&r, tmp, mode);
8593 35275 : return build_real (type, r);
8594 : }
8595 :
8596 :
8597 : /* Subroutine of native_interpret_expr. Interpret the contents of
8598 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8599 : If the buffer cannot be interpreted, return NULL_TREE. */
8600 :
8601 : static tree
8602 1590 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8603 : {
8604 1590 : tree etype, rpart, ipart;
8605 1590 : int size;
8606 :
8607 1590 : etype = TREE_TYPE (type);
8608 1590 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8609 1590 : if (size * 2 > len)
8610 : return NULL_TREE;
8611 1557 : rpart = native_interpret_expr (etype, ptr, size);
8612 1557 : if (!rpart)
8613 : return NULL_TREE;
8614 1376 : ipart = native_interpret_expr (etype, ptr+size, size);
8615 1376 : if (!ipart)
8616 : return NULL_TREE;
8617 1376 : return build_complex (type, rpart, ipart);
8618 : }
8619 :
8620 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8621 : which contains LEN bytes. The vector is known to be encodable using
8622 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8623 :
8624 : Return the vector on success, otherwise return null. */
8625 :
8626 : static tree
8627 206836 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8628 : unsigned int len, unsigned int npatterns,
8629 : unsigned int nelts_per_pattern)
8630 : {
8631 206836 : tree elt_type = TREE_TYPE (type);
8632 206836 : if (VECTOR_BOOLEAN_TYPE_P (type)
8633 206839 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8634 : {
8635 : /* This is the only case in which elements can be smaller than a byte.
8636 : Element 0 is always in the lsb of the containing byte. */
8637 1 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8638 1 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8639 : return NULL_TREE;
8640 :
8641 1 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8642 17 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8643 : {
8644 16 : unsigned int bit_index = i * elt_bits;
8645 16 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8646 16 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8647 32 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8648 17 : ? build_all_ones_cst (elt_type)
8649 1 : : build_zero_cst (elt_type));
8650 : }
8651 1 : return builder.build ();
8652 1 : }
8653 :
8654 206835 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8655 206835 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8656 : return NULL_TREE;
8657 :
8658 206835 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8659 836274 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8660 : {
8661 629477 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8662 629477 : if (!elt)
8663 38 : return NULL_TREE;
8664 629439 : builder.quick_push (elt);
8665 629439 : bytes += elt_bytes;
8666 : }
8667 206797 : return builder.build ();
8668 206835 : }
8669 :
8670 : /* Subroutine of native_interpret_expr. Interpret the contents of
8671 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8672 : If the buffer cannot be interpreted, return NULL_TREE. */
8673 :
8674 : static tree
8675 76437 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8676 : {
8677 76437 : unsigned HOST_WIDE_INT size;
8678 :
8679 76437 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8680 76437 : || size > len)
8681 2 : return NULL_TREE;
8682 :
8683 76435 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8684 76435 : return native_interpret_vector_part (type, ptr, len, count, 1);
8685 : }
8686 :
8687 :
8688 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8689 : the buffer PTR of length LEN as a constant of type TYPE. For
8690 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8691 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8692 : return NULL_TREE. */
8693 :
8694 : tree
8695 2914743 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8696 : {
8697 2914743 : switch (TREE_CODE (type))
8698 : {
8699 2711647 : case INTEGER_TYPE:
8700 2711647 : case ENUMERAL_TYPE:
8701 2711647 : case BOOLEAN_TYPE:
8702 2711647 : case POINTER_TYPE:
8703 2711647 : case REFERENCE_TYPE:
8704 2711647 : case OFFSET_TYPE:
8705 2711647 : case BITINT_TYPE:
8706 2711647 : return native_interpret_int (type, ptr, len);
8707 :
8708 33749 : case REAL_TYPE:
8709 33749 : if (tree ret = native_interpret_real (type, ptr, len))
8710 : {
8711 : /* For floating point values in composite modes, punt if this
8712 : folding doesn't preserve bit representation. As the mode doesn't
8713 : have fixed precision while GCC pretends it does, there could be
8714 : valid values that GCC can't really represent accurately.
8715 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8716 : bit combinationations which GCC doesn't preserve. */
8717 33688 : unsigned char buf[24 * 2];
8718 33688 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8719 33688 : int total_bytes = GET_MODE_SIZE (mode);
8720 33688 : memcpy (buf + 24, ptr, total_bytes);
8721 33688 : clear_type_padding_in_mask (type, buf + 24);
8722 33688 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8723 33688 : || memcmp (buf + 24, buf, total_bytes) != 0)
8724 336 : return NULL_TREE;
8725 : return ret;
8726 : }
8727 : return NULL_TREE;
8728 :
8729 0 : case FIXED_POINT_TYPE:
8730 0 : return native_interpret_fixed (type, ptr, len);
8731 :
8732 1590 : case COMPLEX_TYPE:
8733 1590 : return native_interpret_complex (type, ptr, len);
8734 :
8735 76437 : case VECTOR_TYPE:
8736 76437 : return native_interpret_vector (type, ptr, len);
8737 :
8738 : default:
8739 : return NULL_TREE;
8740 : }
8741 : }
8742 :
8743 : /* Returns true if we can interpret the contents of a native encoding
8744 : as TYPE. */
8745 :
8746 : bool
8747 386881 : can_native_interpret_type_p (tree type)
8748 : {
8749 386881 : switch (TREE_CODE (type))
8750 : {
8751 : case INTEGER_TYPE:
8752 : case ENUMERAL_TYPE:
8753 : case BOOLEAN_TYPE:
8754 : case POINTER_TYPE:
8755 : case REFERENCE_TYPE:
8756 : case FIXED_POINT_TYPE:
8757 : case REAL_TYPE:
8758 : case COMPLEX_TYPE:
8759 : case VECTOR_TYPE:
8760 : case OFFSET_TYPE:
8761 : return true;
8762 80266 : default:
8763 80266 : return false;
8764 : }
8765 : }
8766 :
8767 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8768 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8769 :
8770 : tree
8771 707 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8772 : int len)
8773 : {
8774 707 : vec<constructor_elt, va_gc> *elts = NULL;
8775 707 : if (TREE_CODE (type) == ARRAY_TYPE)
8776 : {
8777 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8778 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8779 : return NULL_TREE;
8780 :
8781 197 : HOST_WIDE_INT cnt = 0;
8782 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8783 : {
8784 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8785 : return NULL_TREE;
8786 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8787 : }
8788 197 : if (eltsz == 0)
8789 0 : cnt = 0;
8790 197 : HOST_WIDE_INT pos = 0;
8791 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8792 : {
8793 439 : tree v = NULL_TREE;
8794 439 : if (pos >= len || pos + eltsz > len)
8795 707 : return NULL_TREE;
8796 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8797 : {
8798 367 : v = native_interpret_expr (TREE_TYPE (type),
8799 367 : ptr + off + pos, eltsz);
8800 367 : if (v == NULL_TREE)
8801 : return NULL_TREE;
8802 : }
8803 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8804 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8805 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8806 : eltsz);
8807 72 : if (v == NULL_TREE)
8808 0 : return NULL_TREE;
8809 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8810 : }
8811 197 : return build_constructor (type, elts);
8812 : }
8813 510 : if (TREE_CODE (type) != RECORD_TYPE)
8814 : return NULL_TREE;
8815 13482 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8816 : {
8817 1236 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8818 14208 : || is_empty_type (TREE_TYPE (field)))
8819 11834 : continue;
8820 1138 : tree fld = field;
8821 1138 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8822 1138 : int diff = 0;
8823 1138 : tree v = NULL_TREE;
8824 1138 : if (DECL_BIT_FIELD (field))
8825 : {
8826 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8827 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8828 : {
8829 168 : poly_int64 bitoffset;
8830 168 : poly_uint64 field_offset, fld_offset;
8831 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8832 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8833 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8834 : else
8835 : bitoffset = 0;
8836 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8837 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8838 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8839 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8840 168 : if (!bitoffset.is_constant (&bitoff)
8841 168 : || bitoff < 0
8842 168 : || bitoff > diff)
8843 0 : return NULL_TREE;
8844 : }
8845 : else
8846 : {
8847 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8848 : return NULL_TREE;
8849 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8850 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8851 12 : bpos %= BITS_PER_UNIT;
8852 12 : fieldsize += bpos;
8853 12 : fieldsize += BITS_PER_UNIT - 1;
8854 12 : fieldsize /= BITS_PER_UNIT;
8855 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8856 12 : if (repr_type == NULL_TREE)
8857 : return NULL_TREE;
8858 12 : sz = int_size_in_bytes (repr_type);
8859 12 : if (sz < 0 || sz > len)
8860 : return NULL_TREE;
8861 12 : pos = int_byte_position (field);
8862 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8863 : return NULL_TREE;
8864 12 : HOST_WIDE_INT rpos;
8865 12 : if (pos + sz <= len)
8866 : rpos = pos;
8867 : else
8868 : {
8869 0 : rpos = len - sz;
8870 0 : gcc_assert (rpos <= pos);
8871 : }
8872 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8873 12 : pos = rpos;
8874 12 : diff = (TYPE_PRECISION (repr_type)
8875 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8876 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8877 12 : if (v == NULL_TREE)
8878 : return NULL_TREE;
8879 : fld = NULL_TREE;
8880 : }
8881 : }
8882 :
8883 168 : if (fld)
8884 : {
8885 1126 : sz = int_size_in_bytes (TREE_TYPE (fld));
8886 1126 : if (sz < 0 || sz > len)
8887 : return NULL_TREE;
8888 1126 : tree byte_pos = byte_position (fld);
8889 1126 : if (!tree_fits_shwi_p (byte_pos))
8890 : return NULL_TREE;
8891 1126 : pos = tree_to_shwi (byte_pos);
8892 1126 : if (pos < 0 || pos > len || pos + sz > len)
8893 : return NULL_TREE;
8894 : }
8895 1126 : if (fld == NULL_TREE)
8896 : /* Already handled above. */;
8897 1126 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8898 : {
8899 830 : v = native_interpret_expr (TREE_TYPE (fld),
8900 830 : ptr + off + pos, sz);
8901 830 : if (v == NULL_TREE)
8902 : return NULL_TREE;
8903 : }
8904 296 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8905 296 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8906 296 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8907 308 : if (v == NULL_TREE)
8908 : return NULL_TREE;
8909 1138 : if (fld != field)
8910 : {
8911 180 : if (TREE_CODE (v) != INTEGER_CST)
8912 : return NULL_TREE;
8913 :
8914 : /* FIXME: Figure out how to handle PDP endian bitfields. */
8915 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8916 : return NULL_TREE;
8917 180 : if (!BYTES_BIG_ENDIAN)
8918 180 : v = wide_int_to_tree (TREE_TYPE (field),
8919 360 : wi::lrshift (wi::to_wide (v), bitoff));
8920 : else
8921 : v = wide_int_to_tree (TREE_TYPE (field),
8922 : wi::lrshift (wi::to_wide (v),
8923 : diff - bitoff));
8924 : }
8925 1138 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8926 : }
8927 510 : return build_constructor (type, elts);
8928 : }
8929 :
8930 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8931 : or extracted constant positions and/or sizes aren't byte aligned. */
8932 :
8933 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8934 : bits between adjacent elements. AMNT should be within
8935 : [0, BITS_PER_UNIT).
8936 : Example, AMNT = 2:
8937 : 00011111|11100000 << 2 = 01111111|10000000
8938 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8939 :
8940 : void
8941 27510 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8942 : unsigned int amnt)
8943 : {
8944 27510 : if (amnt == 0)
8945 : return;
8946 :
8947 15904 : unsigned char carry_over = 0U;
8948 15904 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8949 15904 : unsigned char clear_mask = (~0U) << amnt;
8950 :
8951 94404 : for (unsigned int i = 0; i < sz; i++)
8952 : {
8953 78500 : unsigned prev_carry_over = carry_over;
8954 78500 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8955 :
8956 78500 : ptr[i] <<= amnt;
8957 78500 : if (i != 0)
8958 : {
8959 62596 : ptr[i] &= clear_mask;
8960 62596 : ptr[i] |= prev_carry_over;
8961 : }
8962 : }
8963 : }
8964 :
8965 : /* Like shift_bytes_in_array_left but for big-endian.
8966 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
8967 : bits between adjacent elements. AMNT should be within
8968 : [0, BITS_PER_UNIT).
8969 : Example, AMNT = 2:
8970 : 00011111|11100000 >> 2 = 00000111|11111000
8971 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
8972 :
8973 : void
8974 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
8975 : unsigned int amnt)
8976 : {
8977 8 : if (amnt == 0)
8978 : return;
8979 :
8980 4 : unsigned char carry_over = 0U;
8981 4 : unsigned char carry_mask = ~(~0U << amnt);
8982 :
8983 12 : for (unsigned int i = 0; i < sz; i++)
8984 : {
8985 8 : unsigned prev_carry_over = carry_over;
8986 8 : carry_over = ptr[i] & carry_mask;
8987 :
8988 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
8989 8 : ptr[i] >>= amnt;
8990 8 : ptr[i] |= prev_carry_over;
8991 : }
8992 : }
8993 :
8994 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
8995 : directly on the VECTOR_CST encoding, in a way that works for variable-
8996 : length vectors. Return the resulting VECTOR_CST on success or null
8997 : on failure. */
8998 :
8999 : static tree
9000 139325 : fold_view_convert_vector_encoding (tree type, tree expr)
9001 : {
9002 139325 : tree expr_type = TREE_TYPE (expr);
9003 139325 : poly_uint64 type_bits, expr_bits;
9004 139325 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9005 139325 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9006 0 : return NULL_TREE;
9007 :
9008 139325 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9009 139325 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9010 139325 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9011 139325 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9012 :
9013 : /* We can only preserve the semantics of a stepped pattern if the new
9014 : vector element is an integer of the same size. */
9015 139325 : if (VECTOR_CST_STEPPED_P (expr)
9016 139325 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9017 : return NULL_TREE;
9018 :
9019 : /* The number of bits needed to encode one element from every pattern
9020 : of the original vector. */
9021 130401 : unsigned int expr_sequence_bits
9022 130401 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9023 :
9024 : /* The number of bits needed to encode one element from every pattern
9025 : of the result. */
9026 130401 : unsigned int type_sequence_bits
9027 130401 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9028 :
9029 : /* Don't try to read more bytes than are available, which can happen
9030 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9031 : The general VIEW_CONVERT handling can cope with that case, so there's
9032 : no point complicating things here. */
9033 130401 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9034 130401 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9035 : BITS_PER_UNIT);
9036 130401 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9037 130401 : if (known_gt (buffer_bits, expr_bits))
9038 : return NULL_TREE;
9039 :
9040 : /* Get enough bytes of EXPR to form the new encoding. */
9041 130401 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9042 130401 : buffer.quick_grow (buffer_bytes);
9043 130401 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9044 130401 : buffer_bits / expr_elt_bits)
9045 : != (int) buffer_bytes)
9046 : return NULL_TREE;
9047 :
9048 : /* Reencode the bytes as TYPE. */
9049 130401 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9050 260802 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9051 130401 : type_npatterns, nelts_per_pattern);
9052 130401 : }
9053 :
9054 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9055 : TYPE at compile-time. If we're unable to perform the conversion
9056 : return NULL_TREE. */
9057 :
9058 : static tree
9059 12417000 : fold_view_convert_expr (tree type, tree expr)
9060 : {
9061 12417000 : unsigned char buffer[128];
9062 12417000 : unsigned char *buf;
9063 12417000 : int len;
9064 12417000 : HOST_WIDE_INT l;
9065 :
9066 : /* Check that the host and target are sane. */
9067 12417000 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9068 : return NULL_TREE;
9069 :
9070 12417000 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9071 139325 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9072 : return res;
9073 :
9074 12286618 : l = int_size_in_bytes (type);
9075 12286618 : if (l > (int) sizeof (buffer)
9076 12286618 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9077 : {
9078 0 : buf = XALLOCAVEC (unsigned char, l);
9079 0 : len = l;
9080 : }
9081 : else
9082 : {
9083 : buf = buffer;
9084 : len = sizeof (buffer);
9085 : }
9086 12286618 : len = native_encode_expr (expr, buf, len);
9087 12286618 : if (len == 0)
9088 : return NULL_TREE;
9089 :
9090 1760083 : return native_interpret_expr (type, buf, len);
9091 : }
9092 :
9093 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9094 : to avoid confusing the gimplify process. */
9095 :
9096 : tree
9097 500181161 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9098 : {
9099 : /* The size of the object is not relevant when talking about its address. */
9100 500181161 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9101 0 : t = TREE_OPERAND (t, 0);
9102 :
9103 500181161 : if (INDIRECT_REF_P (t))
9104 : {
9105 59147304 : t = TREE_OPERAND (t, 0);
9106 :
9107 59147304 : if (TREE_TYPE (t) != ptrtype)
9108 37877991 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9109 : }
9110 441033857 : else if (TREE_CODE (t) == MEM_REF
9111 441033857 : && integer_zerop (TREE_OPERAND (t, 1)))
9112 : {
9113 1653032 : t = TREE_OPERAND (t, 0);
9114 :
9115 1653032 : if (TREE_TYPE (t) != ptrtype)
9116 1088403 : t = fold_convert_loc (loc, ptrtype, t);
9117 : }
9118 439380825 : else if (TREE_CODE (t) == MEM_REF
9119 439380825 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9120 661 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9121 : TREE_OPERAND (t, 0),
9122 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9123 439380164 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9124 : {
9125 28405157 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9126 :
9127 28405157 : if (TREE_TYPE (t) != ptrtype)
9128 14933 : t = fold_convert_loc (loc, ptrtype, t);
9129 : }
9130 : else
9131 410975007 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9132 :
9133 : return t;
9134 : }
9135 :
9136 : /* Build an expression for the address of T. */
9137 :
9138 : tree
9139 449723679 : build_fold_addr_expr_loc (location_t loc, tree t)
9140 : {
9141 449723679 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9142 :
9143 449723679 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9144 : }
9145 :
9146 : /* Fold a unary expression of code CODE and type TYPE with operand
9147 : OP0. Return the folded expression if folding is successful.
9148 : Otherwise, return NULL_TREE. */
9149 :
9150 : tree
9151 1827379341 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9152 : {
9153 1827379341 : tree tem;
9154 1827379341 : tree arg0;
9155 1827379341 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9156 :
9157 1827379341 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9158 : && TREE_CODE_LENGTH (code) == 1);
9159 :
9160 1827379341 : arg0 = op0;
9161 1827379341 : if (arg0)
9162 : {
9163 1827366089 : if (CONVERT_EXPR_CODE_P (code)
9164 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9165 : {
9166 : /* Don't use STRIP_NOPS, because signedness of argument type
9167 : matters. */
9168 974353143 : STRIP_SIGN_NOPS (arg0);
9169 : }
9170 : else
9171 : {
9172 : /* Strip any conversions that don't change the mode. This
9173 : is safe for every expression, except for a comparison
9174 : expression because its signedness is derived from its
9175 : operands.
9176 :
9177 : Note that this is done as an internal manipulation within
9178 : the constant folder, in order to find the simplest
9179 : representation of the arguments so that their form can be
9180 : studied. In any cases, the appropriate type conversions
9181 : should be put back in the tree that will get out of the
9182 : constant folder. */
9183 853012946 : STRIP_NOPS (arg0);
9184 : }
9185 :
9186 1827366089 : if (CONSTANT_CLASS_P (arg0))
9187 : {
9188 297395733 : tree tem = const_unop (code, type, arg0);
9189 297395733 : if (tem)
9190 : {
9191 259798546 : if (TREE_TYPE (tem) != type)
9192 9455 : tem = fold_convert_loc (loc, type, tem);
9193 259798546 : return tem;
9194 : }
9195 : }
9196 : }
9197 :
9198 1567580795 : tem = generic_simplify (loc, code, type, op0);
9199 1567580795 : if (tem)
9200 : return tem;
9201 :
9202 1187256012 : if (TREE_CODE_CLASS (code) == tcc_unary)
9203 : {
9204 630530971 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9205 1050401 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9206 : fold_build1_loc (loc, code, type,
9207 1050401 : fold_convert_loc (loc, TREE_TYPE (op0),
9208 2100802 : TREE_OPERAND (arg0, 1))));
9209 629480570 : else if (TREE_CODE (arg0) == COND_EXPR)
9210 : {
9211 510975 : tree arg01 = TREE_OPERAND (arg0, 1);
9212 510975 : tree arg02 = TREE_OPERAND (arg0, 2);
9213 510975 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9214 506793 : arg01 = fold_build1_loc (loc, code, type,
9215 : fold_convert_loc (loc,
9216 506793 : TREE_TYPE (op0), arg01));
9217 510975 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9218 500963 : arg02 = fold_build1_loc (loc, code, type,
9219 : fold_convert_loc (loc,
9220 500963 : TREE_TYPE (op0), arg02));
9221 510975 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9222 : arg01, arg02);
9223 :
9224 : /* If this was a conversion, and all we did was to move into
9225 : inside the COND_EXPR, bring it back out. But leave it if
9226 : it is a conversion from integer to integer and the
9227 : result precision is no wider than a word since such a
9228 : conversion is cheap and may be optimized away by combine,
9229 : while it couldn't if it were outside the COND_EXPR. Then return
9230 : so we don't get into an infinite recursion loop taking the
9231 : conversion out and then back in. */
9232 :
9233 510975 : if ((CONVERT_EXPR_CODE_P (code)
9234 10253 : || code == NON_LVALUE_EXPR)
9235 500741 : && TREE_CODE (tem) == COND_EXPR
9236 481191 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9237 420056 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9238 266787 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9239 266575 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9240 266575 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9241 266575 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9242 797780 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9243 21088 : && (INTEGRAL_TYPE_P
9244 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9245 21048 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9246 20935 : || flag_syntax_only))
9247 244822 : tem = build1_loc (loc, code, type,
9248 : build3 (COND_EXPR,
9249 244822 : TREE_TYPE (TREE_OPERAND
9250 : (TREE_OPERAND (tem, 1), 0)),
9251 244822 : TREE_OPERAND (tem, 0),
9252 244822 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9253 244822 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9254 : 0)));
9255 510975 : return tem;
9256 : }
9257 : }
9258 :
9259 1185694636 : switch (code)
9260 : {
9261 40759379 : case NON_LVALUE_EXPR:
9262 40759379 : if (!maybe_lvalue_p (op0))
9263 29481831 : return fold_convert_loc (loc, type, op0);
9264 : return NULL_TREE;
9265 :
9266 579276597 : CASE_CONVERT:
9267 579276597 : case FLOAT_EXPR:
9268 579276597 : case FIX_TRUNC_EXPR:
9269 579276597 : if (COMPARISON_CLASS_P (op0))
9270 : {
9271 : /* If we have (type) (a CMP b) and type is an integral type, return
9272 : new expression involving the new type. Canonicalize
9273 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9274 : non-integral type.
9275 : Do not fold the result as that would not simplify further, also
9276 : folding again results in recursions. */
9277 603013 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9278 155920 : return build2_loc (loc, TREE_CODE (op0), type,
9279 155920 : TREE_OPERAND (op0, 0),
9280 311840 : TREE_OPERAND (op0, 1));
9281 447093 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9282 7629 : && TREE_CODE (type) != VECTOR_TYPE)
9283 7629 : return build3_loc (loc, COND_EXPR, type, op0,
9284 : constant_boolean_node (true, type),
9285 7629 : constant_boolean_node (false, type));
9286 : }
9287 :
9288 : /* Handle (T *)&A.B.C for A being of type T and B and C
9289 : living at offset zero. This occurs frequently in
9290 : C++ upcasting and then accessing the base. */
9291 579113048 : if (TREE_CODE (op0) == ADDR_EXPR
9292 158753532 : && POINTER_TYPE_P (type)
9293 731227868 : && handled_component_p (TREE_OPERAND (op0, 0)))
9294 : {
9295 35483421 : poly_int64 bitsize, bitpos;
9296 35483421 : tree offset;
9297 35483421 : machine_mode mode;
9298 35483421 : int unsignedp, reversep, volatilep;
9299 35483421 : tree base
9300 35483421 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9301 : &offset, &mode, &unsignedp, &reversep,
9302 : &volatilep);
9303 : /* If the reference was to a (constant) zero offset, we can use
9304 : the address of the base if it has the same base type
9305 : as the result type and the pointer type is unqualified. */
9306 35483421 : if (!offset
9307 35342972 : && known_eq (bitpos, 0)
9308 25735874 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9309 25735874 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9310 35492849 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9311 9227 : return fold_convert_loc (loc, type,
9312 9227 : build_fold_addr_expr_loc (loc, base));
9313 : }
9314 :
9315 579103821 : if (TREE_CODE (op0) == MODIFY_EXPR
9316 287533 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9317 : /* Detect assigning a bitfield. */
9318 579105799 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9319 118 : && DECL_BIT_FIELD
9320 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9321 : {
9322 : /* Don't leave an assignment inside a conversion
9323 : unless assigning a bitfield. */
9324 1930 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9325 : /* First do the assignment, then return converted constant. */
9326 1930 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9327 1930 : suppress_warning (tem /* What warning? */);
9328 1930 : TREE_USED (tem) = 1;
9329 1930 : return tem;
9330 : }
9331 :
9332 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9333 : constants (if x has signed type, the sign bit cannot be set
9334 : in c). This folds extension into the BIT_AND_EXPR.
9335 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9336 : very likely don't have maximal range for their precision and this
9337 : transformation effectively doesn't preserve non-maximal ranges. */
9338 579101891 : if (TREE_CODE (type) == INTEGER_TYPE
9339 237053952 : && TREE_CODE (op0) == BIT_AND_EXPR
9340 579572463 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9341 : {
9342 138128 : tree and_expr = op0;
9343 138128 : tree and0 = TREE_OPERAND (and_expr, 0);
9344 138128 : tree and1 = TREE_OPERAND (and_expr, 1);
9345 138128 : int change = 0;
9346 :
9347 138128 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9348 138128 : || (TYPE_PRECISION (type)
9349 51036 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9350 : change = 1;
9351 14550 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9352 : <= HOST_BITS_PER_WIDE_INT
9353 14550 : && tree_fits_uhwi_p (and1))
9354 : {
9355 13444 : unsigned HOST_WIDE_INT cst;
9356 :
9357 13444 : cst = tree_to_uhwi (and1);
9358 26888 : cst &= HOST_WIDE_INT_M1U
9359 13444 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9360 13444 : change = (cst == 0);
9361 13444 : if (change
9362 13444 : && !flag_syntax_only
9363 26888 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9364 : == ZERO_EXTEND))
9365 : {
9366 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9367 : and0 = fold_convert_loc (loc, uns, and0);
9368 : and1 = fold_convert_loc (loc, uns, and1);
9369 : }
9370 : }
9371 13444 : if (change)
9372 : {
9373 137022 : tree and1_type = TREE_TYPE (and1);
9374 137022 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9375 : TYPE_PRECISION (type));
9376 137022 : tem = force_fit_type (type,
9377 137022 : wide_int::from (wi::to_wide (and1), prec,
9378 137022 : TYPE_SIGN (and1_type)),
9379 137022 : 0, TREE_OVERFLOW (and1));
9380 137022 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9381 137022 : fold_convert_loc (loc, type, and0), tem);
9382 : }
9383 : }
9384 :
9385 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9386 : cast (T1)X will fold away. We assume that this happens when X itself
9387 : is a cast. */
9388 578964869 : if (POINTER_TYPE_P (type)
9389 308710737 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9390 589509833 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9391 : {
9392 8093524 : tree arg00 = TREE_OPERAND (arg0, 0);
9393 8093524 : tree arg01 = TREE_OPERAND (arg0, 1);
9394 :
9395 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9396 : when the pointed type needs higher alignment than
9397 : the p+ first operand's pointed type. */
9398 8093524 : if (!in_gimple_form
9399 8074576 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9400 8094738 : && (min_align_of_type (TREE_TYPE (type))
9401 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9402 : return NULL_TREE;
9403 :
9404 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9405 : when type is a reference type and arg00's type is not,
9406 : because arg00 could be validly nullptr and if arg01 doesn't return,
9407 : we don't want false positive binding of reference to nullptr. */
9408 8093457 : if (TREE_CODE (type) == REFERENCE_TYPE
9409 5180663 : && !in_gimple_form
9410 5180643 : && sanitize_flags_p (SANITIZE_NULL)
9411 8093888 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9412 : return NULL_TREE;
9413 :
9414 8093026 : arg00 = fold_convert_loc (loc, type, arg00);
9415 8093026 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9416 : }
9417 :
9418 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9419 : of the same precision, and X is an integer type not narrower than
9420 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9421 570871345 : if (INTEGRAL_TYPE_P (type)
9422 242920488 : && TREE_CODE (op0) == BIT_NOT_EXPR
9423 507776 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9424 507776 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9425 571198240 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9426 : {
9427 324924 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9428 393170 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9429 393168 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9430 264653 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9431 264653 : fold_convert_loc (loc, type, tem));
9432 : }
9433 :
9434 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9435 : type of X and Y (integer types only). */
9436 570606692 : if (INTEGRAL_TYPE_P (type)
9437 242655835 : && TREE_CODE (op0) == MULT_EXPR
9438 8867828 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9439 8847632 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9440 570675267 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9441 20652 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9442 : {
9443 : /* Be careful not to introduce new overflows. */
9444 68521 : tree mult_type;
9445 68521 : if (TYPE_OVERFLOW_WRAPS (type))
9446 : mult_type = type;
9447 : else
9448 2105 : mult_type = unsigned_type_for (type);
9449 :
9450 68521 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9451 : {
9452 137042 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9453 : fold_convert_loc (loc, mult_type,
9454 68521 : TREE_OPERAND (op0, 0)),
9455 : fold_convert_loc (loc, mult_type,
9456 68521 : TREE_OPERAND (op0, 1)));
9457 68521 : return fold_convert_loc (loc, type, tem);
9458 : }
9459 : }
9460 :
9461 : return NULL_TREE;
9462 :
9463 231032905 : case VIEW_CONVERT_EXPR:
9464 231032905 : if (TREE_CODE (op0) == MEM_REF)
9465 : {
9466 2511 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9467 5 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9468 2511 : tem = fold_build2_loc (loc, MEM_REF, type,
9469 2511 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9470 2511 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9471 2511 : return tem;
9472 : }
9473 :
9474 : return NULL_TREE;
9475 :
9476 4096412 : case NEGATE_EXPR:
9477 4096412 : tem = fold_negate_expr (loc, arg0);
9478 4096412 : if (tem)
9479 1540 : return fold_convert_loc (loc, type, tem);
9480 : return NULL_TREE;
9481 :
9482 2140892 : case ABS_EXPR:
9483 : /* Convert fabs((double)float) into (double)fabsf(float). */
9484 2140892 : if (TREE_CODE (arg0) == NOP_EXPR
9485 17231 : && TREE_CODE (type) == REAL_TYPE)
9486 : {
9487 17175 : tree targ0 = strip_float_extensions (arg0);
9488 17175 : if (targ0 != arg0)
9489 16971 : return fold_convert_loc (loc, type,
9490 : fold_build1_loc (loc, ABS_EXPR,
9491 16971 : TREE_TYPE (targ0),
9492 16971 : targ0));
9493 : }
9494 : return NULL_TREE;
9495 :
9496 2690255 : case BIT_NOT_EXPR:
9497 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9498 2690255 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9499 2691940 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9500 : fold_convert_loc (loc, type,
9501 1685 : TREE_OPERAND (arg0, 0)))))
9502 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9503 : fold_convert_loc (loc, type,
9504 28 : TREE_OPERAND (arg0, 1)));
9505 2690241 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9506 2691912 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9507 : fold_convert_loc (loc, type,
9508 1671 : TREE_OPERAND (arg0, 1)))))
9509 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9510 : fold_convert_loc (loc, type,
9511 46 : TREE_OPERAND (arg0, 0)), tem);
9512 :
9513 : return NULL_TREE;
9514 :
9515 48362214 : case TRUTH_NOT_EXPR:
9516 : /* Note that the operand of this must be an int
9517 : and its values must be 0 or 1.
9518 : ("true" is a fixed value perhaps depending on the language,
9519 : but we don't handle values other than 1 correctly yet.) */
9520 48362214 : tem = fold_truth_not_expr (loc, arg0);
9521 48362214 : if (!tem)
9522 : return NULL_TREE;
9523 33861607 : return fold_convert_loc (loc, type, tem);
9524 :
9525 65994660 : case INDIRECT_REF:
9526 : /* Fold *&X to X if X is an lvalue. */
9527 65994660 : if (TREE_CODE (op0) == ADDR_EXPR)
9528 : {
9529 7466 : tree op00 = TREE_OPERAND (op0, 0);
9530 7466 : if ((VAR_P (op00)
9531 : || TREE_CODE (op00) == PARM_DECL
9532 : || TREE_CODE (op00) == RESULT_DECL)
9533 6295 : && !TREE_READONLY (op00))
9534 : return op00;
9535 : }
9536 : return NULL_TREE;
9537 :
9538 : default:
9539 : return NULL_TREE;
9540 : } /* switch (code) */
9541 : }
9542 :
9543 :
9544 : /* If the operation was a conversion do _not_ mark a resulting constant
9545 : with TREE_OVERFLOW if the original constant was not. These conversions
9546 : have implementation defined behavior and retaining the TREE_OVERFLOW
9547 : flag here would confuse later passes such as VRP. */
9548 : tree
9549 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9550 : tree type, tree op0)
9551 : {
9552 0 : tree res = fold_unary_loc (loc, code, type, op0);
9553 0 : if (res
9554 0 : && TREE_CODE (res) == INTEGER_CST
9555 0 : && TREE_CODE (op0) == INTEGER_CST
9556 0 : && CONVERT_EXPR_CODE_P (code))
9557 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9558 :
9559 0 : return res;
9560 : }
9561 :
9562 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9563 : operands OP0 and OP1. LOC is the location of the resulting expression.
9564 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9565 : Return the folded expression if folding is successful. Otherwise,
9566 : return NULL_TREE. */
9567 : static tree
9568 25027501 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9569 : tree arg0, tree arg1, tree op0, tree op1)
9570 : {
9571 25027501 : tree tem;
9572 :
9573 : /* We only do these simplifications if we are optimizing. */
9574 25027501 : if (!optimize)
9575 : return NULL_TREE;
9576 :
9577 : /* Check for things like (A || B) && (A || C). We can convert this
9578 : to A || (B && C). Note that either operator can be any of the four
9579 : truth and/or operations and the transformation will still be
9580 : valid. Also note that we only care about order for the
9581 : ANDIF and ORIF operators. If B contains side effects, this
9582 : might change the truth-value of A. */
9583 24643583 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9584 5756655 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9585 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9586 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9587 5756655 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9588 24703372 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9589 : {
9590 59295 : tree a00 = TREE_OPERAND (arg0, 0);
9591 59295 : tree a01 = TREE_OPERAND (arg0, 1);
9592 59295 : tree a10 = TREE_OPERAND (arg1, 0);
9593 59295 : tree a11 = TREE_OPERAND (arg1, 1);
9594 118590 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9595 59295 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9596 59295 : && (code == TRUTH_AND_EXPR
9597 22532 : || code == TRUTH_OR_EXPR));
9598 :
9599 59295 : if (operand_equal_p (a00, a10, 0))
9600 849 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9601 849 : fold_build2_loc (loc, code, type, a01, a11));
9602 58446 : else if (commutative && operand_equal_p (a00, a11, 0))
9603 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9604 0 : fold_build2_loc (loc, code, type, a01, a10));
9605 58446 : else if (commutative && operand_equal_p (a01, a10, 0))
9606 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9607 0 : fold_build2_loc (loc, code, type, a00, a11));
9608 :
9609 : /* This case if tricky because we must either have commutative
9610 : operators or else A10 must not have side-effects. */
9611 :
9612 58402 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9613 116356 : && operand_equal_p (a01, a11, 0))
9614 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9615 : fold_build2_loc (loc, code, type, a00, a10),
9616 43 : a01);
9617 : }
9618 :
9619 : /* See if we can build a range comparison. */
9620 24642691 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9621 : return tem;
9622 :
9623 23488852 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9624 23486890 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9625 : {
9626 37811 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9627 37811 : if (tem)
9628 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9629 : }
9630 :
9631 23488839 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9632 23478316 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9633 : {
9634 85909 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9635 85909 : if (tem)
9636 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9637 : }
9638 :
9639 : /* Check for the possibility of merging component references. If our
9640 : lhs is another similar operation, try to merge its rhs with our
9641 : rhs. Then try to merge our lhs and rhs. */
9642 23488748 : if (TREE_CODE (arg0) == code
9643 24309016 : && (tem = fold_truth_andor_1 (loc, code, type,
9644 820268 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9645 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9646 :
9647 23488663 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9648 : return tem;
9649 :
9650 23447912 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9651 23447912 : if (param_logical_op_non_short_circuit != -1)
9652 7664 : logical_op_non_short_circuit
9653 7664 : = param_logical_op_non_short_circuit;
9654 23447912 : if (logical_op_non_short_circuit
9655 23444067 : && !sanitize_coverage_p ()
9656 23447912 : && (code == TRUTH_AND_EXPR
9657 23444064 : || code == TRUTH_ANDIF_EXPR
9658 10822807 : || code == TRUTH_OR_EXPR
9659 10822807 : || code == TRUTH_ORIF_EXPR))
9660 : {
9661 23444064 : enum tree_code ncode, icode;
9662 :
9663 23444064 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9664 23444064 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9665 12621257 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9666 :
9667 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9668 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9669 : We don't want to pack more than two leafs to a non-IF AND/OR
9670 : expression.
9671 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9672 : equal to IF-CODE, then we don't want to add right-hand operand.
9673 : If the inner right-hand side of left-hand operand has
9674 : side-effects, or isn't simple, then we can't add to it,
9675 : as otherwise we might destroy if-sequence. */
9676 23444064 : if (TREE_CODE (arg0) == icode
9677 811516 : && simple_condition_p (arg1)
9678 : /* Needed for sequence points to handle trappings, and
9679 : side-effects. */
9680 23491733 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9681 : {
9682 40841 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9683 : arg1);
9684 40841 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9685 40841 : tem);
9686 : }
9687 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9688 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9689 23403223 : else if (TREE_CODE (arg1) == icode
9690 4718 : && simple_condition_p (arg0)
9691 : /* Needed for sequence points to handle trappings, and
9692 : side-effects. */
9693 23404176 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9694 : {
9695 36 : tem = fold_build2_loc (loc, ncode, type,
9696 36 : arg0, TREE_OPERAND (arg1, 0));
9697 36 : return fold_build2_loc (loc, icode, type, tem,
9698 72 : TREE_OPERAND (arg1, 1));
9699 : }
9700 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9701 : into (A OR B).
9702 : For sequence point consistancy, we need to check for trapping,
9703 : and side-effects. */
9704 5072050 : else if (code == icode && simple_condition_p (arg0)
9705 24261826 : && simple_condition_p (arg1))
9706 431519 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9707 : }
9708 :
9709 : return NULL_TREE;
9710 : }
9711 :
9712 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9713 : by changing CODE to reduce the magnitude of constants involved in
9714 : ARG0 of the comparison.
9715 : Returns a canonicalized comparison tree if a simplification was
9716 : possible, otherwise returns NULL_TREE. */
9717 :
9718 : static tree
9719 174422190 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9720 : tree arg0, tree arg1)
9721 : {
9722 174422190 : enum tree_code code0 = TREE_CODE (arg0);
9723 174422190 : tree t, cst0 = NULL_TREE;
9724 174422190 : int sgn0;
9725 :
9726 : /* Match A +- CST code arg1. We can change this only if overflow
9727 : is undefined. */
9728 174422190 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9729 132936049 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9730 : /* In principle pointers also have undefined overflow behavior,
9731 : but that causes problems elsewhere. */
9732 65570971 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9733 65570971 : && (code0 == MINUS_EXPR
9734 65570971 : || code0 == PLUS_EXPR)
9735 2600161 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9736 : return NULL_TREE;
9737 :
9738 : /* Identify the constant in arg0 and its sign. */
9739 2123336 : cst0 = TREE_OPERAND (arg0, 1);
9740 2123336 : sgn0 = tree_int_cst_sgn (cst0);
9741 :
9742 : /* Overflowed constants and zero will cause problems. */
9743 2123336 : if (integer_zerop (cst0)
9744 2123336 : || TREE_OVERFLOW (cst0))
9745 : return NULL_TREE;
9746 :
9747 : /* See if we can reduce the magnitude of the constant in
9748 : arg0 by changing the comparison code. */
9749 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9750 2123336 : if (code == LT_EXPR
9751 1249248 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9752 : code = LE_EXPR;
9753 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9754 1942363 : else if (code == GT_EXPR
9755 572124 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9756 : code = GE_EXPR;
9757 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9758 1762303 : else if (code == LE_EXPR
9759 665469 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9760 : code = LT_EXPR;
9761 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9762 1529967 : else if (code == GE_EXPR
9763 487855 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9764 : code = GT_EXPR;
9765 : else
9766 : return NULL_TREE;
9767 :
9768 : /* Now build the constant reduced in magnitude. But not if that
9769 : would produce one outside of its types range. */
9770 1592972 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9771 1592972 : && ((sgn0 == 1
9772 412258 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9773 412258 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9774 796486 : || (sgn0 == -1
9775 384228 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9776 384228 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9777 0 : return NULL_TREE;
9778 :
9779 1208744 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9780 796486 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9781 796486 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9782 796486 : t = fold_convert (TREE_TYPE (arg1), t);
9783 :
9784 796486 : return fold_build2_loc (loc, code, type, t, arg1);
9785 : }
9786 :
9787 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9788 : overflow further. Try to decrease the magnitude of constants involved
9789 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9790 : and put sole constants at the second argument position.
9791 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9792 :
9793 : static tree
9794 87588332 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9795 : tree arg0, tree arg1)
9796 : {
9797 87588332 : tree t;
9798 :
9799 : /* Try canonicalization by simplifying arg0. */
9800 87588332 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9801 87588332 : if (t)
9802 : return t;
9803 :
9804 : /* Try canonicalization by simplifying arg1 using the swapped
9805 : comparison. */
9806 86833858 : code = swap_tree_comparison (code);
9807 86833858 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9808 86833858 : return t;
9809 : }
9810 :
9811 : /* Return a positive integer when the symbol DECL is known to have
9812 : a nonzero address, zero when it's known not to (e.g., it's a weak
9813 : symbol), and a negative integer when the symbol is not yet in the
9814 : symbol table and so whether or not its address is zero is unknown.
9815 : For function local objects always return positive integer. */
9816 : static int
9817 11845365 : maybe_nonzero_address (tree decl)
9818 : {
9819 11845365 : if (!DECL_P (decl))
9820 : return -1;
9821 :
9822 : /* Normally, don't do anything for variables and functions before symtab is
9823 : built; it is quite possible that DECL will be declared weak later.
9824 : But if folding_initializer, we need a constant answer now, so create
9825 : the symtab entry and prevent later weak declaration. */
9826 9256749 : if (decl_in_symtab_p (decl))
9827 : {
9828 4145678 : if (struct symtab_node *symbol
9829 4145678 : = (folding_initializer
9830 4145678 : ? symtab_node::get_create (decl)
9831 4129064 : : symtab_node::get (decl)))
9832 4126693 : return symbol->nonzero_address ();
9833 : }
9834 5111071 : else if (folding_cxx_constexpr)
9835 : /* Anything that doesn't go in the symtab has non-zero address. */
9836 : return 1;
9837 :
9838 : /* Function local objects are never NULL. */
9839 5034253 : if (DECL_CONTEXT (decl)
9840 5017745 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9841 10048565 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9842 : return 1;
9843 :
9844 : return -1;
9845 : }
9846 :
9847 : /* Subroutine of fold_binary. This routine performs all of the
9848 : transformations that are common to the equality/inequality
9849 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9850 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9851 : fold_binary should call fold_binary. Fold a comparison with
9852 : tree code CODE and type TYPE with operands OP0 and OP1. Return
9853 : the folded comparison or NULL_TREE. */
9854 :
9855 : static tree
9856 87661749 : fold_comparison (location_t loc, enum tree_code code, tree type,
9857 : tree op0, tree op1)
9858 : {
9859 87661749 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9860 87661749 : tree arg0, arg1, tem;
9861 :
9862 87661749 : arg0 = op0;
9863 87661749 : arg1 = op1;
9864 :
9865 87661749 : STRIP_SIGN_NOPS (arg0);
9866 87661749 : STRIP_SIGN_NOPS (arg1);
9867 :
9868 : /* For comparisons of pointers we can decompose it to a compile time
9869 : comparison of the base objects and the offsets into the object.
9870 : This requires at least one operand being an ADDR_EXPR or a
9871 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9872 162180078 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9873 87871698 : && (TREE_CODE (arg0) == ADDR_EXPR
9874 13300906 : || TREE_CODE (arg1) == ADDR_EXPR
9875 11779240 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9876 11036156 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9877 : {
9878 2326295 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9879 2326295 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9880 2326295 : machine_mode mode;
9881 2326295 : int volatilep, reversep, unsignedp;
9882 2326295 : bool indirect_base0 = false, indirect_base1 = false;
9883 :
9884 : /* Get base and offset for the access. Strip ADDR_EXPR for
9885 : get_inner_reference, but put it back by stripping INDIRECT_REF
9886 : off the base object if possible. indirect_baseN will be true
9887 : if baseN is not an address but refers to the object itself. */
9888 2326295 : base0 = arg0;
9889 2326295 : if (TREE_CODE (arg0) == ADDR_EXPR)
9890 : {
9891 52463 : base0
9892 52463 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9893 : &bitsize, &bitpos0, &offset0, &mode,
9894 : &unsignedp, &reversep, &volatilep);
9895 52463 : if (INDIRECT_REF_P (base0))
9896 2146 : base0 = TREE_OPERAND (base0, 0);
9897 : else
9898 : indirect_base0 = true;
9899 : }
9900 2273832 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9901 : {
9902 805780 : base0 = TREE_OPERAND (arg0, 0);
9903 805780 : STRIP_SIGN_NOPS (base0);
9904 805780 : if (TREE_CODE (base0) == ADDR_EXPR)
9905 : {
9906 31257 : base0
9907 31257 : = get_inner_reference (TREE_OPERAND (base0, 0),
9908 : &bitsize, &bitpos0, &offset0, &mode,
9909 : &unsignedp, &reversep, &volatilep);
9910 31257 : if (INDIRECT_REF_P (base0))
9911 20 : base0 = TREE_OPERAND (base0, 0);
9912 : else
9913 : indirect_base0 = true;
9914 : }
9915 805780 : if (offset0 == NULL_TREE || integer_zerop (offset0))
9916 805780 : offset0 = TREE_OPERAND (arg0, 1);
9917 : else
9918 0 : offset0 = size_binop (PLUS_EXPR, offset0,
9919 : TREE_OPERAND (arg0, 1));
9920 805780 : if (poly_int_tree_p (offset0))
9921 : {
9922 646893 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
9923 646893 : TYPE_PRECISION (sizetype));
9924 646893 : tem <<= LOG2_BITS_PER_UNIT;
9925 646893 : tem += bitpos0;
9926 646893 : if (tem.to_shwi (&bitpos0))
9927 646893 : offset0 = NULL_TREE;
9928 : }
9929 : }
9930 :
9931 2326295 : base1 = arg1;
9932 2326295 : if (TREE_CODE (arg1) == ADDR_EXPR)
9933 : {
9934 1550889 : base1
9935 1550889 : = get_inner_reference (TREE_OPERAND (arg1, 0),
9936 : &bitsize, &bitpos1, &offset1, &mode,
9937 : &unsignedp, &reversep, &volatilep);
9938 1550889 : if (INDIRECT_REF_P (base1))
9939 67295 : base1 = TREE_OPERAND (base1, 0);
9940 : else
9941 : indirect_base1 = true;
9942 : }
9943 775406 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9944 : {
9945 88981 : base1 = TREE_OPERAND (arg1, 0);
9946 88981 : STRIP_SIGN_NOPS (base1);
9947 88981 : if (TREE_CODE (base1) == ADDR_EXPR)
9948 : {
9949 10111 : base1
9950 10111 : = get_inner_reference (TREE_OPERAND (base1, 0),
9951 : &bitsize, &bitpos1, &offset1, &mode,
9952 : &unsignedp, &reversep, &volatilep);
9953 10111 : if (INDIRECT_REF_P (base1))
9954 0 : base1 = TREE_OPERAND (base1, 0);
9955 : else
9956 : indirect_base1 = true;
9957 : }
9958 88981 : if (offset1 == NULL_TREE || integer_zerop (offset1))
9959 88957 : offset1 = TREE_OPERAND (arg1, 1);
9960 : else
9961 24 : offset1 = size_binop (PLUS_EXPR, offset1,
9962 : TREE_OPERAND (arg1, 1));
9963 88981 : if (poly_int_tree_p (offset1))
9964 : {
9965 78391 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
9966 78391 : TYPE_PRECISION (sizetype));
9967 78391 : tem <<= LOG2_BITS_PER_UNIT;
9968 78391 : tem += bitpos1;
9969 78391 : if (tem.to_shwi (&bitpos1))
9970 78391 : offset1 = NULL_TREE;
9971 : }
9972 : }
9973 :
9974 : /* If we have equivalent bases we might be able to simplify. */
9975 2326295 : if (indirect_base0 == indirect_base1
9976 3116783 : && operand_equal_p (base0, base1,
9977 : indirect_base0 ? OEP_ADDRESS_OF : 0))
9978 : {
9979 : /* We can fold this expression to a constant if the non-constant
9980 : offset parts are equal. */
9981 20957 : if ((offset0 == offset1
9982 6607 : || (offset0 && offset1
9983 2660 : && operand_equal_p (offset0, offset1, 0)))
9984 20957 : && (equality_code
9985 9098 : || (indirect_base0
9986 5970 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
9987 3128 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9988 : {
9989 14312 : switch (code)
9990 : {
9991 56 : case EQ_EXPR:
9992 56 : if (known_eq (bitpos0, bitpos1))
9993 51494 : return constant_boolean_node (true, type);
9994 21 : if (known_ne (bitpos0, bitpos1))
9995 21 : return constant_boolean_node (false, type);
9996 : break;
9997 5196 : case NE_EXPR:
9998 5196 : if (known_ne (bitpos0, bitpos1))
9999 5191 : return constant_boolean_node (true, type);
10000 5 : if (known_eq (bitpos0, bitpos1))
10001 5 : return constant_boolean_node (false, type);
10002 : break;
10003 2225 : case LT_EXPR:
10004 2225 : if (known_lt (bitpos0, bitpos1))
10005 2096 : return constant_boolean_node (true, type);
10006 129 : if (known_ge (bitpos0, bitpos1))
10007 129 : return constant_boolean_node (false, type);
10008 : break;
10009 1636 : case LE_EXPR:
10010 1636 : if (known_le (bitpos0, bitpos1))
10011 182 : return constant_boolean_node (true, type);
10012 1454 : if (known_gt (bitpos0, bitpos1))
10013 1454 : return constant_boolean_node (false, type);
10014 : break;
10015 3430 : case GE_EXPR:
10016 3430 : if (known_ge (bitpos0, bitpos1))
10017 1362 : return constant_boolean_node (true, type);
10018 2068 : if (known_lt (bitpos0, bitpos1))
10019 2068 : return constant_boolean_node (false, type);
10020 : break;
10021 1769 : case GT_EXPR:
10022 1769 : if (known_gt (bitpos0, bitpos1))
10023 1716 : return constant_boolean_node (true, type);
10024 53 : if (known_le (bitpos0, bitpos1))
10025 53 : return constant_boolean_node (false, type);
10026 : break;
10027 : default:;
10028 : }
10029 : }
10030 : /* We can simplify the comparison to a comparison of the variable
10031 : offset parts if the constant offset parts are equal.
10032 : Be careful to use signed sizetype here because otherwise we
10033 : mess with array offsets in the wrong way. This is possible
10034 : because pointer arithmetic is restricted to retain within an
10035 : object and overflow on pointer differences is undefined as of
10036 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10037 6645 : else if (known_eq (bitpos0, bitpos1)
10038 6645 : && (equality_code
10039 5197 : || (indirect_base0
10040 267 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10041 4930 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10042 : {
10043 : /* By converting to signed sizetype we cover middle-end pointer
10044 : arithmetic which operates on unsigned pointer types of size
10045 : type size and ARRAY_REF offsets which are properly sign or
10046 : zero extended from their type in case it is narrower than
10047 : sizetype. */
10048 5306 : if (offset0 == NULL_TREE)
10049 0 : offset0 = build_int_cst (ssizetype, 0);
10050 : else
10051 5306 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10052 5306 : if (offset1 == NULL_TREE)
10053 2678 : offset1 = build_int_cst (ssizetype, 0);
10054 : else
10055 2628 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10056 :
10057 5306 : return fold_build2_loc (loc, code, type, offset0, offset1);
10058 : }
10059 : }
10060 : /* For equal offsets we can simplify to a comparison of the
10061 : base addresses. */
10062 2305338 : else if (known_eq (bitpos0, bitpos1)
10063 54803 : && (indirect_base0
10064 979109 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10065 15131 : && (indirect_base1
10066 182485 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10067 2524056 : && ((offset0 == offset1)
10068 4421 : || (offset0 && offset1
10069 4151 : && operand_equal_p (offset0, offset1, 0))))
10070 : {
10071 31813 : if (indirect_base0)
10072 3401 : base0 = build_fold_addr_expr_loc (loc, base0);
10073 31813 : if (indirect_base1)
10074 4719 : base1 = build_fold_addr_expr_loc (loc, base1);
10075 31813 : return fold_build2_loc (loc, code, type, base0, base1);
10076 : }
10077 : /* Comparison between an ordinary (non-weak) symbol and a null
10078 : pointer can be eliminated since such symbols must have a non
10079 : null address. In C, relational expressions between pointers
10080 : to objects and null pointers are undefined. The results
10081 : below follow the C++ rules with the additional property that
10082 : every object pointer compares greater than a null pointer.
10083 : */
10084 2273525 : else if (((DECL_P (base0)
10085 247358 : && maybe_nonzero_address (base0) > 0
10086 : /* Avoid folding references to struct members at offset 0 to
10087 : prevent tests like '&ptr->firstmember == 0' from getting
10088 : eliminated. When ptr is null, although the -> expression
10089 : is strictly speaking invalid, GCC retains it as a matter
10090 : of QoI. See PR c/44555. */
10091 233028 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10092 2257560 : || CONSTANT_CLASS_P (base0))
10093 20773 : && indirect_base0
10094 : /* The caller guarantees that when one of the arguments is
10095 : constant (i.e., null in this case) it is second. */
10096 2291387 : && integer_zerop (arg1))
10097 : {
10098 63 : switch (code)
10099 : {
10100 24 : case EQ_EXPR:
10101 24 : case LE_EXPR:
10102 24 : case LT_EXPR:
10103 24 : return constant_boolean_node (false, type);
10104 39 : case GE_EXPR:
10105 39 : case GT_EXPR:
10106 39 : case NE_EXPR:
10107 39 : return constant_boolean_node (true, type);
10108 0 : default:
10109 0 : gcc_unreachable ();
10110 : }
10111 : }
10112 : }
10113 :
10114 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10115 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10116 : the resulting offset is smaller in absolute value than the
10117 : original one and has the same sign. */
10118 172675856 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10119 133734378 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10120 33184649 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10121 2345302 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10122 1922568 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10123 1922568 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10124 154644026 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10125 164240 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10126 : {
10127 164240 : tree const1 = TREE_OPERAND (arg0, 1);
10128 164240 : tree const2 = TREE_OPERAND (arg1, 1);
10129 164240 : tree variable1 = TREE_OPERAND (arg0, 0);
10130 164240 : tree variable2 = TREE_OPERAND (arg1, 0);
10131 164240 : tree cst;
10132 :
10133 : /* Put the constant on the side where it doesn't overflow and is
10134 : of lower absolute value and of same sign than before. */
10135 164241 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10136 : ? MINUS_EXPR : PLUS_EXPR,
10137 : const2, const1);
10138 164240 : if (!TREE_OVERFLOW (cst)
10139 164224 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10140 186163 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10141 5659 : return fold_build2_loc (loc, code, type,
10142 : variable1,
10143 5659 : fold_build2_loc (loc, TREE_CODE (arg1),
10144 5659 : TREE_TYPE (arg1),
10145 5659 : variable2, cst));
10146 :
10147 158582 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10148 : ? MINUS_EXPR : PLUS_EXPR,
10149 : const1, const2);
10150 158581 : if (!TREE_OVERFLOW (cst)
10151 158565 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10152 174845 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10153 16264 : return fold_build2_loc (loc, code, type,
10154 16264 : fold_build2_loc (loc, TREE_CODE (arg0),
10155 16264 : TREE_TYPE (arg0),
10156 : variable1, cst),
10157 16264 : variable2);
10158 : }
10159 :
10160 87588332 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10161 87588332 : if (tem)
10162 : return tem;
10163 :
10164 : /* If we are comparing an expression that just has comparisons
10165 : of two integer values, arithmetic expressions of those comparisons,
10166 : and constants, we can simplify it. There are only three cases
10167 : to check: the two values can either be equal, the first can be
10168 : greater, or the second can be greater. Fold the expression for
10169 : those three values. Since each value must be 0 or 1, we have
10170 : eight possibilities, each of which corresponds to the constant 0
10171 : or 1 or one of the six possible comparisons.
10172 :
10173 : This handles common cases like (a > b) == 0 but also handles
10174 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10175 : occur in macroized code. */
10176 :
10177 86791846 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10178 : {
10179 52847279 : tree cval1 = 0, cval2 = 0;
10180 :
10181 52847279 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10182 : /* Don't handle degenerate cases here; they should already
10183 : have been handled anyway. */
10184 477386 : && cval1 != 0 && cval2 != 0
10185 476200 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10186 476200 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10187 476194 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10188 59 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10189 59 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10190 52847338 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10191 59 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10192 : {
10193 59 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10194 59 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10195 :
10196 : /* We can't just pass T to eval_subst in case cval1 or cval2
10197 : was the same as ARG1. */
10198 :
10199 59 : tree high_result
10200 59 : = fold_build2_loc (loc, code, type,
10201 : eval_subst (loc, arg0, cval1, maxval,
10202 : cval2, minval),
10203 : arg1);
10204 59 : tree equal_result
10205 59 : = fold_build2_loc (loc, code, type,
10206 : eval_subst (loc, arg0, cval1, maxval,
10207 : cval2, maxval),
10208 : arg1);
10209 59 : tree low_result
10210 59 : = fold_build2_loc (loc, code, type,
10211 : eval_subst (loc, arg0, cval1, minval,
10212 : cval2, maxval),
10213 : arg1);
10214 :
10215 : /* All three of these results should be 0 or 1. Confirm they are.
10216 : Then use those values to select the proper code to use. */
10217 :
10218 59 : if (TREE_CODE (high_result) == INTEGER_CST
10219 50 : && TREE_CODE (equal_result) == INTEGER_CST
10220 40 : && TREE_CODE (low_result) == INTEGER_CST)
10221 : {
10222 : /* Make a 3-bit mask with the high-order bit being the
10223 : value for `>', the next for '=', and the low for '<'. */
10224 40 : switch ((integer_onep (high_result) * 4)
10225 40 : + (integer_onep (equal_result) * 2)
10226 40 : + integer_onep (low_result))
10227 : {
10228 21 : case 0:
10229 : /* Always false. */
10230 40 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10231 : case 1:
10232 : code = LT_EXPR;
10233 : break;
10234 2 : case 2:
10235 2 : code = EQ_EXPR;
10236 2 : break;
10237 0 : case 3:
10238 0 : code = LE_EXPR;
10239 0 : break;
10240 0 : case 4:
10241 0 : code = GT_EXPR;
10242 0 : break;
10243 1 : case 5:
10244 1 : code = NE_EXPR;
10245 1 : break;
10246 0 : case 6:
10247 0 : code = GE_EXPR;
10248 0 : break;
10249 16 : case 7:
10250 : /* Always true. */
10251 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10252 : }
10253 :
10254 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10255 : }
10256 : }
10257 : }
10258 :
10259 : return NULL_TREE;
10260 : }
10261 :
10262 :
10263 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10264 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10265 : argument EXPR represents the expression "z" of type TYPE. */
10266 :
10267 : static tree
10268 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10269 : {
10270 2 : tree itype = TREE_TYPE (type);
10271 2 : tree rpart, ipart, tem;
10272 :
10273 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10274 : {
10275 0 : rpart = TREE_OPERAND (expr, 0);
10276 0 : ipart = TREE_OPERAND (expr, 1);
10277 : }
10278 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10279 : {
10280 0 : rpart = TREE_REALPART (expr);
10281 0 : ipart = TREE_IMAGPART (expr);
10282 : }
10283 : else
10284 : {
10285 2 : expr = save_expr (expr);
10286 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10287 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10288 : }
10289 :
10290 2 : rpart = save_expr (rpart);
10291 2 : ipart = save_expr (ipart);
10292 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10293 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10294 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10295 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10296 2 : build_zero_cst (itype));
10297 : }
10298 :
10299 :
10300 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10301 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10302 : true if successful. */
10303 :
10304 : static bool
10305 16115 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10306 : {
10307 16115 : unsigned HOST_WIDE_INT i, nunits;
10308 :
10309 16115 : if (TREE_CODE (arg) == VECTOR_CST
10310 16115 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10311 : {
10312 2120 : for (i = 0; i < nunits; ++i)
10313 1710 : elts[i] = VECTOR_CST_ELT (arg, i);
10314 : }
10315 15705 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10316 : {
10317 : constructor_elt *elt;
10318 :
10319 35165 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10320 30248 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10321 10788 : return false;
10322 : else
10323 19460 : elts[i] = elt->value;
10324 : }
10325 : else
10326 : return false;
10327 6101 : for (; i < nelts; i++)
10328 1548 : elts[i]
10329 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10330 : return true;
10331 : }
10332 :
10333 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10334 : mask for VLA vec_perm folding.
10335 : REASON if specified, will contain the reason why SEL is not suitable.
10336 : Used only for debugging and unit-testing. */
10337 :
10338 : static bool
10339 15938 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10340 : const vec_perm_indices &sel,
10341 : const char **reason = NULL)
10342 : {
10343 15938 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10344 15938 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10345 :
10346 31876 : if (!(pow2p_hwi (sel_npatterns)
10347 15938 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10348 15938 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10349 : {
10350 0 : if (reason)
10351 0 : *reason = "npatterns is not power of 2";
10352 0 : return false;
10353 : }
10354 :
10355 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10356 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10357 15938 : poly_uint64 esel;
10358 15938 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10359 : {
10360 0 : if (reason)
10361 0 : *reason = "sel.length is not multiple of sel_npatterns";
10362 0 : return false;
10363 : }
10364 :
10365 15938 : if (sel_nelts_per_pattern < 3)
10366 : return true;
10367 :
10368 5758 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10369 : {
10370 4361 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10371 4361 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10372 4361 : HOST_WIDE_INT step;
10373 4361 : if (!poly_int64 (a2 - a1).is_constant (&step))
10374 : {
10375 : if (reason)
10376 : *reason = "step is not constant";
10377 1062 : return false;
10378 : }
10379 : // FIXME: Punt on step < 0 for now, revisit later.
10380 4361 : if (step < 0)
10381 : return false;
10382 4306 : if (step == 0)
10383 0 : continue;
10384 :
10385 4306 : if (!pow2p_hwi (step))
10386 : {
10387 0 : if (reason)
10388 0 : *reason = "step is not power of 2";
10389 0 : return false;
10390 : }
10391 :
10392 : /* Ensure that stepped sequence of the pattern selects elements
10393 : only from the same input vector. */
10394 4306 : uint64_t q1, qe;
10395 4306 : poly_uint64 r1, re;
10396 4306 : poly_uint64 ae = a1 + (esel - 2) * step;
10397 4306 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10398 :
10399 4306 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10400 4306 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10401 : && q1 == qe))
10402 : {
10403 404 : if (reason)
10404 0 : *reason = "crossed input vectors";
10405 404 : return false;
10406 : }
10407 :
10408 : /* Ensure that the stepped sequence always selects from the same
10409 : input pattern. */
10410 3902 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10411 3902 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10412 :
10413 3902 : if (!multiple_p (step, arg_npatterns))
10414 : {
10415 601 : if (reason)
10416 0 : *reason = "step is not multiple of npatterns";
10417 601 : return false;
10418 : }
10419 :
10420 : /* If a1 chooses base element from arg, ensure that it's a natural
10421 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10422 : to preserve arg's encoding. */
10423 :
10424 3301 : if (maybe_lt (r1, arg_npatterns))
10425 : {
10426 13 : unsigned HOST_WIDE_INT index;
10427 13 : if (!r1.is_constant (&index))
10428 2 : return false;
10429 :
10430 13 : tree arg_elem0 = vector_cst_elt (arg, index);
10431 13 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10432 13 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10433 :
10434 13 : tree step1, step2;
10435 13 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10436 13 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10437 26 : || !operand_equal_p (step1, step2, 0))
10438 : {
10439 2 : if (reason)
10440 0 : *reason = "not a natural stepped sequence";
10441 2 : return false;
10442 : }
10443 : }
10444 : }
10445 :
10446 : return true;
10447 : }
10448 :
10449 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10450 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10451 : REASON has same purpose as described in
10452 : valid_mask_for_fold_vec_perm_cst_p. */
10453 :
10454 : static tree
10455 15938 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10456 : const char **reason = NULL)
10457 : {
10458 15938 : unsigned res_npatterns, res_nelts_per_pattern;
10459 15938 : unsigned HOST_WIDE_INT res_nelts;
10460 :
10461 : /* First try to implement the fold in a VLA-friendly way.
10462 :
10463 : (1) If the selector is simply a duplication of N elements, the
10464 : result is likewise a duplication of N elements.
10465 :
10466 : (2) If the selector is N elements followed by a duplication
10467 : of N elements, the result is too.
10468 :
10469 : (3) If the selector is N elements followed by an interleaving
10470 : of N linear series, the situation is more complex.
10471 :
10472 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10473 : can handle this case. If we can, then each of the N linear
10474 : series either (a) selects the same element each time or
10475 : (b) selects a linear series from one of the input patterns.
10476 :
10477 : If (b) holds for one of the linear series, the result
10478 : will contain a linear series, and so the result will have
10479 : the same shape as the selector. If (a) holds for all of
10480 : the linear series, the result will be the same as (2) above.
10481 :
10482 : (b) can only hold if one of the input patterns has a
10483 : stepped encoding. */
10484 :
10485 15938 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10486 : {
10487 14876 : res_npatterns = sel.encoding ().npatterns ();
10488 14876 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10489 14876 : if (res_nelts_per_pattern == 3
10490 1397 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10491 15784 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10492 : res_nelts_per_pattern = 2;
10493 14876 : res_nelts = res_npatterns * res_nelts_per_pattern;
10494 : }
10495 1062 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10496 : {
10497 1062 : res_npatterns = res_nelts;
10498 1062 : res_nelts_per_pattern = 1;
10499 : }
10500 : else
10501 : return NULL_TREE;
10502 :
10503 15938 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10504 89041 : for (unsigned i = 0; i < res_nelts; i++)
10505 : {
10506 73103 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10507 73103 : uint64_t q;
10508 73103 : poly_uint64 r;
10509 73103 : unsigned HOST_WIDE_INT index;
10510 :
10511 : /* Punt if sel[i] /trunc_div len cannot be determined,
10512 : because the input vector to be chosen will depend on
10513 : runtime vector length.
10514 : For example if len == 4 + 4x, and sel[i] == 4,
10515 : If len at runtime equals 4, we choose arg1[0].
10516 : For any other value of len > 4 at runtime, we choose arg0[4].
10517 : which makes the element choice dependent on runtime vector length. */
10518 73103 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10519 : {
10520 : if (reason)
10521 : *reason = "cannot divide selector element by arg len";
10522 : return NULL_TREE;
10523 : }
10524 :
10525 : /* sel[i] % len will give the index of element in the chosen input
10526 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10527 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10528 73103 : if (!r.is_constant (&index))
10529 : {
10530 : if (reason)
10531 : *reason = "remainder is not constant";
10532 : return NULL_TREE;
10533 : }
10534 :
10535 73103 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10536 73103 : tree elem = vector_cst_elt (arg, index);
10537 73103 : out_elts.quick_push (elem);
10538 : }
10539 :
10540 15938 : return out_elts.build ();
10541 15938 : }
10542 :
10543 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10544 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10545 : NULL_TREE otherwise. */
10546 :
10547 : tree
10548 39986 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10549 : {
10550 39986 : unsigned int i;
10551 39986 : unsigned HOST_WIDE_INT nelts;
10552 :
10553 39986 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10554 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10555 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10556 :
10557 39986 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10558 39986 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10559 : return NULL_TREE;
10560 :
10561 29373 : if (TREE_CODE (arg0) == VECTOR_CST
10562 16212 : && TREE_CODE (arg1) == VECTOR_CST)
10563 15938 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10564 :
10565 : /* For fall back case, we want to ensure we have VLS vectors
10566 : with equal length. */
10567 13435 : if (!sel.length ().is_constant (&nelts)
10568 13435 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10569 0 : return NULL_TREE;
10570 :
10571 13435 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10572 13435 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10573 13435 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10574 10788 : return NULL_TREE;
10575 :
10576 2647 : vec<constructor_elt, va_gc> *v;
10577 2647 : vec_alloc (v, nelts);
10578 13487 : for (i = 0; i < nelts; i++)
10579 : {
10580 10840 : HOST_WIDE_INT index;
10581 10840 : if (!sel[i].is_constant (&index))
10582 : return NULL_TREE;
10583 10840 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10584 : }
10585 2647 : return build_constructor (type, v);
10586 : }
10587 :
10588 : /* Try to fold a pointer difference of type TYPE two address expressions of
10589 : array references AREF0 and AREF1 using location LOC. Return a
10590 : simplified expression for the difference or NULL_TREE. */
10591 :
10592 : static tree
10593 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10594 : tree aref0, tree aref1,
10595 : bool use_pointer_diff)
10596 : {
10597 39 : tree base0 = TREE_OPERAND (aref0, 0);
10598 39 : tree base1 = TREE_OPERAND (aref1, 0);
10599 39 : tree base_offset = build_int_cst (type, 0);
10600 :
10601 : /* If the bases are array references as well, recurse. If the bases
10602 : are pointer indirections compute the difference of the pointers.
10603 : If the bases are equal, we are set. */
10604 39 : if ((TREE_CODE (base0) == ARRAY_REF
10605 1 : && TREE_CODE (base1) == ARRAY_REF
10606 1 : && (base_offset
10607 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10608 : use_pointer_diff)))
10609 38 : || (INDIRECT_REF_P (base0)
10610 7 : && INDIRECT_REF_P (base1)
10611 7 : && (base_offset
10612 : = use_pointer_diff
10613 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10614 1 : TREE_OPERAND (base0, 0),
10615 1 : TREE_OPERAND (base1, 0))
10616 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10617 6 : fold_convert (type,
10618 : TREE_OPERAND (base0, 0)),
10619 6 : fold_convert (type,
10620 : TREE_OPERAND (base1, 0)))))
10621 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10622 : {
10623 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10624 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10625 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10626 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10627 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10628 : base_offset,
10629 : fold_build2_loc (loc, MULT_EXPR, type,
10630 15 : diff, esz));
10631 : }
10632 : return NULL_TREE;
10633 : }
10634 :
10635 : /* If the real or vector real constant CST of type TYPE has an exact
10636 : inverse, return it, else return NULL. */
10637 :
10638 : tree
10639 1125786 : exact_inverse (tree type, tree cst)
10640 : {
10641 1125786 : REAL_VALUE_TYPE r;
10642 1125786 : tree unit_type;
10643 1125786 : machine_mode mode;
10644 :
10645 1125786 : switch (TREE_CODE (cst))
10646 : {
10647 1125207 : case REAL_CST:
10648 1125207 : r = TREE_REAL_CST (cst);
10649 :
10650 1125207 : if (exact_real_inverse (TYPE_MODE (type), &r))
10651 322244 : return build_real (type, r);
10652 :
10653 : return NULL_TREE;
10654 :
10655 579 : case VECTOR_CST:
10656 579 : {
10657 579 : unit_type = TREE_TYPE (type);
10658 579 : mode = TYPE_MODE (unit_type);
10659 :
10660 579 : tree_vector_builder elts;
10661 579 : if (!elts.new_unary_operation (type, cst, false))
10662 : return NULL_TREE;
10663 579 : unsigned int count = elts.encoded_nelts ();
10664 639 : for (unsigned int i = 0; i < count; ++i)
10665 : {
10666 579 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10667 579 : if (!exact_real_inverse (mode, &r))
10668 : return NULL_TREE;
10669 60 : elts.quick_push (build_real (unit_type, r));
10670 : }
10671 :
10672 60 : return elts.build ();
10673 579 : }
10674 :
10675 : default:
10676 : return NULL_TREE;
10677 : }
10678 : }
10679 :
10680 : /* Mask out the tz least significant bits of X of type TYPE where
10681 : tz is the number of trailing zeroes in Y. */
10682 : static wide_int
10683 134726 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10684 : {
10685 134726 : int tz = wi::ctz (y);
10686 134726 : if (tz > 0)
10687 6275 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10688 128451 : return x;
10689 : }
10690 :
10691 : /* Return true when T is an address and is known to be nonzero.
10692 : For floating point we further ensure that T is not denormal.
10693 : Similar logic is present in nonzero_address in rtlanal.h. */
10694 :
10695 : bool
10696 145500733 : tree_expr_nonzero_p (tree t)
10697 : {
10698 145833022 : tree type = TREE_TYPE (t);
10699 145833022 : enum tree_code code;
10700 :
10701 : /* Doing something useful for floating point would need more work. */
10702 145833022 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10703 : return false;
10704 :
10705 145714290 : code = TREE_CODE (t);
10706 145714290 : switch (TREE_CODE_CLASS (code))
10707 : {
10708 934716 : case tcc_unary:
10709 934716 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10710 2863971 : case tcc_binary:
10711 2863971 : case tcc_comparison:
10712 2863971 : return tree_binary_nonzero_p (code, type,
10713 2863971 : TREE_OPERAND (t, 0),
10714 5727942 : TREE_OPERAND (t, 1));
10715 12671166 : case tcc_constant:
10716 12671166 : case tcc_declaration:
10717 12671166 : case tcc_reference:
10718 12671166 : return tree_single_nonzero_p (t);
10719 :
10720 129244437 : default:
10721 129244437 : break;
10722 : }
10723 :
10724 129244437 : switch (code)
10725 : {
10726 475761 : case TRUTH_NOT_EXPR:
10727 475761 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10728 :
10729 66563 : case TRUTH_AND_EXPR:
10730 66563 : case TRUTH_OR_EXPR:
10731 66563 : case TRUTH_XOR_EXPR:
10732 66563 : return tree_binary_nonzero_p (code, type,
10733 66563 : TREE_OPERAND (t, 0),
10734 133126 : TREE_OPERAND (t, 1));
10735 :
10736 125437871 : case COND_EXPR:
10737 125437871 : case CONSTRUCTOR:
10738 125437871 : case OBJ_TYPE_REF:
10739 125437871 : case ADDR_EXPR:
10740 125437871 : case WITH_SIZE_EXPR:
10741 125437871 : case SSA_NAME:
10742 125437871 : return tree_single_nonzero_p (t);
10743 :
10744 84589 : case COMPOUND_EXPR:
10745 84589 : case MODIFY_EXPR:
10746 84589 : case BIND_EXPR:
10747 84589 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10748 :
10749 247700 : case SAVE_EXPR:
10750 247700 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10751 :
10752 2885280 : case CALL_EXPR:
10753 2885280 : {
10754 2885280 : tree fndecl = get_callee_fndecl (t);
10755 2885280 : if (!fndecl) return false;
10756 2883340 : if (flag_delete_null_pointer_checks && !flag_check_new
10757 2883340 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10758 2884036 : && !TREE_NOTHROW (fndecl))
10759 : return true;
10760 2884036 : if (flag_delete_null_pointer_checks
10761 5767376 : && lookup_attribute ("returns_nonnull",
10762 2883340 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10763 : return true;
10764 2884028 : return alloca_call_p (t);
10765 : }
10766 :
10767 : default:
10768 : break;
10769 : }
10770 : return false;
10771 : }
10772 :
10773 : /* Return true if T is known not to be equal to an integer W.
10774 : If STMT is specified, the check is if T on STMT is not equal
10775 : to W. */
10776 :
10777 : bool
10778 98121852 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10779 : {
10780 98121852 : int_range_max vr;
10781 98121852 : switch (TREE_CODE (t))
10782 : {
10783 1067394 : case INTEGER_CST:
10784 1067394 : return wi::to_wide (t) != w;
10785 :
10786 97053388 : case SSA_NAME:
10787 97053388 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10788 : return false;
10789 :
10790 194106776 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10791 97053388 : if (!vr.undefined_p () && !vr.contains_p (w))
10792 : return true;
10793 : /* If T has some known zero bits and W has any of those bits set,
10794 : then T is known not to be equal to W. */
10795 96922321 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10796 193844202 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10797 : return true;
10798 : return false;
10799 :
10800 : default:
10801 : return false;
10802 : }
10803 98121852 : }
10804 :
10805 : /* Fold a binary expression of code CODE and type TYPE with operands
10806 : OP0 and OP1. LOC is the location of the resulting expression.
10807 : Return the folded expression if folding is successful. Otherwise,
10808 : return NULL_TREE. */
10809 :
10810 : tree
10811 864164144 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10812 : tree op0, tree op1)
10813 : {
10814 864164144 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10815 864164144 : tree arg0, arg1, tem;
10816 864164144 : tree t1 = NULL_TREE;
10817 864164144 : unsigned int prec;
10818 :
10819 864164144 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10820 : && TREE_CODE_LENGTH (code) == 2
10821 : && op0 != NULL_TREE
10822 : && op1 != NULL_TREE);
10823 :
10824 864164144 : arg0 = op0;
10825 864164144 : arg1 = op1;
10826 :
10827 : /* Strip any conversions that don't change the mode. This is
10828 : safe for every expression, except for a comparison expression
10829 : because its signedness is derived from its operands. So, in
10830 : the latter case, only strip conversions that don't change the
10831 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10832 : preserved.
10833 :
10834 : Note that this is done as an internal manipulation within the
10835 : constant folder, in order to find the simplest representation
10836 : of the arguments so that their form can be studied. In any
10837 : cases, the appropriate type conversions should be put back in
10838 : the tree that will get out of the constant folder. */
10839 :
10840 864164144 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10841 : {
10842 190340450 : STRIP_SIGN_NOPS (arg0);
10843 190340450 : STRIP_SIGN_NOPS (arg1);
10844 : }
10845 : else
10846 : {
10847 673823694 : STRIP_NOPS (arg0);
10848 673823694 : STRIP_NOPS (arg1);
10849 : }
10850 :
10851 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
10852 : constant but we can't do arithmetic on them. */
10853 864164144 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10854 : {
10855 229070872 : tem = const_binop (code, type, arg0, arg1);
10856 229070872 : if (tem != NULL_TREE)
10857 : {
10858 226690117 : if (TREE_TYPE (tem) != type)
10859 2933438 : tem = fold_convert_loc (loc, type, tem);
10860 226690117 : return tem;
10861 : }
10862 : }
10863 :
10864 : /* If this is a commutative operation, and ARG0 is a constant, move it
10865 : to ARG1 to reduce the number of tests below. */
10866 637474027 : if (commutative_tree_code (code)
10867 637474027 : && tree_swap_operands_p (arg0, arg1))
10868 32623973 : return fold_build2_loc (loc, code, type, op1, op0);
10869 :
10870 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
10871 : to ARG1 to reduce the number of tests below. */
10872 604850054 : if (kind == tcc_comparison
10873 604850054 : && tree_swap_operands_p (arg0, arg1))
10874 8171173 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10875 :
10876 596678881 : tem = generic_simplify (loc, code, type, op0, op1);
10877 596678881 : if (tem)
10878 : return tem;
10879 :
10880 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10881 :
10882 : First check for cases where an arithmetic operation is applied to a
10883 : compound, conditional, or comparison operation. Push the arithmetic
10884 : operation inside the compound or conditional to see if any folding
10885 : can then be done. Convert comparison to conditional for this purpose.
10886 : The also optimizes non-constant cases that used to be done in
10887 : expand_expr.
10888 :
10889 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10890 : one of the operands is a comparison and the other is a comparison, a
10891 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10892 : code below would make the expression more complex. Change it to a
10893 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10894 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10895 :
10896 501517592 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10897 : || code == EQ_EXPR || code == NE_EXPR)
10898 56164417 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10899 55584817 : && ((truth_value_p (TREE_CODE (arg0))
10900 1073635 : && (truth_value_p (TREE_CODE (arg1))
10901 773904 : || (TREE_CODE (arg1) == BIT_AND_EXPR
10902 45 : && integer_onep (TREE_OPERAND (arg1, 1)))))
10903 55285070 : || (truth_value_p (TREE_CODE (arg1))
10904 6603 : && (truth_value_p (TREE_CODE (arg0))
10905 6603 : || (TREE_CODE (arg0) == BIT_AND_EXPR
10906 177 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
10907 : {
10908 374932 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10909 75171 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10910 : : TRUTH_XOR_EXPR,
10911 : boolean_type_node,
10912 : fold_convert_loc (loc, boolean_type_node, arg0),
10913 : fold_convert_loc (loc, boolean_type_node, arg1));
10914 :
10915 299761 : if (code == EQ_EXPR)
10916 68659 : tem = invert_truthvalue_loc (loc, tem);
10917 :
10918 299761 : return fold_convert_loc (loc, type, tem);
10919 : }
10920 :
10921 501217831 : if (TREE_CODE_CLASS (code) == tcc_binary
10922 286337813 : || TREE_CODE_CLASS (code) == tcc_comparison)
10923 : {
10924 307482745 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
10925 : {
10926 81798 : tem = fold_build2_loc (loc, code, type,
10927 81798 : fold_convert_loc (loc, TREE_TYPE (op0),
10928 81798 : TREE_OPERAND (arg0, 1)), op1);
10929 81798 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10930 81798 : tem);
10931 : }
10932 307400947 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
10933 : {
10934 3143 : tem = fold_build2_loc (loc, code, type, op0,
10935 3143 : fold_convert_loc (loc, TREE_TYPE (op1),
10936 3143 : TREE_OPERAND (arg1, 1)));
10937 3143 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10938 3143 : tem);
10939 : }
10940 :
10941 307397804 : if (TREE_CODE (arg0) == COND_EXPR
10942 307017267 : || TREE_CODE (arg0) == VEC_COND_EXPR
10943 307014585 : || COMPARISON_CLASS_P (arg0))
10944 : {
10945 696483 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10946 : arg0, arg1,
10947 : /*cond_first_p=*/1);
10948 696483 : if (tem != NULL_TREE)
10949 : return tem;
10950 : }
10951 :
10952 306928775 : if (TREE_CODE (arg1) == COND_EXPR
10953 306692161 : || TREE_CODE (arg1) == VEC_COND_EXPR
10954 306691810 : || COMPARISON_CLASS_P (arg1))
10955 : {
10956 248517 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10957 : arg1, arg0,
10958 : /*cond_first_p=*/0);
10959 248517 : if (tem != NULL_TREE)
10960 : return tem;
10961 : }
10962 : }
10963 :
10964 500655929 : switch (code)
10965 : {
10966 57897870 : case MEM_REF:
10967 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10968 57897870 : if (TREE_CODE (arg0) == ADDR_EXPR
10969 57897870 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10970 : {
10971 840634 : tree iref = TREE_OPERAND (arg0, 0);
10972 840634 : return fold_build2 (MEM_REF, type,
10973 : TREE_OPERAND (iref, 0),
10974 : int_const_binop (PLUS_EXPR, arg1,
10975 : TREE_OPERAND (iref, 1)));
10976 : }
10977 :
10978 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10979 57057236 : if (TREE_CODE (arg0) == ADDR_EXPR
10980 57057236 : && handled_component_p (TREE_OPERAND (arg0, 0)))
10981 : {
10982 5824077 : tree base;
10983 5824077 : poly_int64 coffset;
10984 5824077 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10985 : &coffset);
10986 5824077 : if (!base)
10987 : return NULL_TREE;
10988 5820075 : return fold_build2 (MEM_REF, type,
10989 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
10990 : int_const_binop (PLUS_EXPR, arg1,
10991 : size_int (coffset)));
10992 : }
10993 :
10994 : return NULL_TREE;
10995 :
10996 43429822 : case POINTER_PLUS_EXPR:
10997 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10998 86859228 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10999 86850809 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11000 35862 : return fold_convert_loc (loc, type,
11001 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11002 : fold_convert_loc (loc, sizetype,
11003 : arg1),
11004 : fold_convert_loc (loc, sizetype,
11005 35862 : arg0)));
11006 :
11007 : return NULL_TREE;
11008 :
11009 60926408 : case PLUS_EXPR:
11010 60926408 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11011 : {
11012 : /* X + (X / CST) * -CST is X % CST. */
11013 49741487 : if (TREE_CODE (arg1) == MULT_EXPR
11014 2326115 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11015 49747512 : && operand_equal_p (arg0,
11016 6025 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11017 : {
11018 214 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11019 214 : tree cst1 = TREE_OPERAND (arg1, 1);
11020 214 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11021 : cst1, cst0);
11022 214 : if (sum && integer_zerop (sum))
11023 214 : return fold_convert_loc (loc, type,
11024 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11025 214 : TREE_TYPE (arg0), arg0,
11026 214 : cst0));
11027 : }
11028 : }
11029 :
11030 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11031 : one. Make sure the type is not saturating and has the signedness of
11032 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11033 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11034 60926194 : if ((TREE_CODE (arg0) == MULT_EXPR
11035 49520466 : || TREE_CODE (arg1) == MULT_EXPR)
11036 12729931 : && !TYPE_SATURATING (type)
11037 12729931 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11038 12325693 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11039 72591841 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11040 : {
11041 8486025 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11042 8486025 : if (tem)
11043 : return tem;
11044 : }
11045 :
11046 59706865 : if (! FLOAT_TYPE_P (type))
11047 : {
11048 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11049 : (plus (plus (mult) (mult)) (foo)) so that we can
11050 : take advantage of the factoring cases below. */
11051 282096 : if (ANY_INTEGRAL_TYPE_P (type)
11052 48524292 : && TYPE_OVERFLOW_WRAPS (type)
11053 48524292 : && (((TREE_CODE (arg0) == PLUS_EXPR
11054 30506408 : || TREE_CODE (arg0) == MINUS_EXPR)
11055 3389608 : && TREE_CODE (arg1) == MULT_EXPR)
11056 29993047 : || ((TREE_CODE (arg1) == PLUS_EXPR
11057 29993047 : || TREE_CODE (arg1) == MINUS_EXPR)
11058 433052 : && TREE_CODE (arg0) == MULT_EXPR)))
11059 : {
11060 562895 : tree parg0, parg1, parg, marg;
11061 562895 : enum tree_code pcode;
11062 :
11063 562895 : if (TREE_CODE (arg1) == MULT_EXPR)
11064 : parg = arg0, marg = arg1;
11065 : else
11066 49534 : parg = arg1, marg = arg0;
11067 562895 : pcode = TREE_CODE (parg);
11068 562895 : parg0 = TREE_OPERAND (parg, 0);
11069 562895 : parg1 = TREE_OPERAND (parg, 1);
11070 562895 : STRIP_NOPS (parg0);
11071 562895 : STRIP_NOPS (parg1);
11072 :
11073 562895 : if (TREE_CODE (parg0) == MULT_EXPR
11074 265648 : && TREE_CODE (parg1) != MULT_EXPR)
11075 228603 : return fold_build2_loc (loc, pcode, type,
11076 : fold_build2_loc (loc, PLUS_EXPR, type,
11077 : fold_convert_loc (loc, type,
11078 : parg0),
11079 : fold_convert_loc (loc, type,
11080 : marg)),
11081 228603 : fold_convert_loc (loc, type, parg1));
11082 334292 : if (TREE_CODE (parg0) != MULT_EXPR
11083 297247 : && TREE_CODE (parg1) == MULT_EXPR)
11084 100432 : return
11085 100432 : fold_build2_loc (loc, PLUS_EXPR, type,
11086 : fold_convert_loc (loc, type, parg0),
11087 : fold_build2_loc (loc, pcode, type,
11088 : fold_convert_loc (loc, type, marg),
11089 : fold_convert_loc (loc, type,
11090 100432 : parg1)));
11091 : }
11092 : }
11093 : else
11094 : {
11095 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11096 : to __complex__ ( x, y ). This is not the same for SNaNs or
11097 : if signed zeros are involved. */
11098 11182573 : if (!HONOR_SNANS (arg0)
11099 11180913 : && !HONOR_SIGNED_ZEROS (arg0)
11100 11203677 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11101 : {
11102 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11103 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11104 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11105 3086 : bool arg0rz = false, arg0iz = false;
11106 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11107 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11108 : {
11109 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11110 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11111 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11112 : {
11113 22 : tree rp = arg1r ? arg1r
11114 0 : : build1 (REALPART_EXPR, rtype, arg1);
11115 22 : tree ip = arg0i ? arg0i
11116 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11117 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11118 : }
11119 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11120 : {
11121 53 : tree rp = arg0r ? arg0r
11122 0 : : build1 (REALPART_EXPR, rtype, arg0);
11123 53 : tree ip = arg1i ? arg1i
11124 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11125 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11126 : }
11127 : }
11128 : }
11129 :
11130 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11131 : We associate floats only if the user has specified
11132 : -fassociative-math. */
11133 11182498 : if (flag_associative_math
11134 21007 : && TREE_CODE (arg1) == PLUS_EXPR
11135 36 : && TREE_CODE (arg0) != MULT_EXPR)
11136 : {
11137 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11138 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11139 21 : if (TREE_CODE (tree11) == MULT_EXPR
11140 5 : && TREE_CODE (tree10) == MULT_EXPR)
11141 : {
11142 1 : tree tree0;
11143 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11144 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11145 : }
11146 : }
11147 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11148 : We associate floats only if the user has specified
11149 : -fassociative-math. */
11150 11182497 : if (flag_associative_math
11151 21006 : && TREE_CODE (arg0) == PLUS_EXPR
11152 1221 : && TREE_CODE (arg1) != MULT_EXPR)
11153 : {
11154 831 : tree tree00 = TREE_OPERAND (arg0, 0);
11155 831 : tree tree01 = TREE_OPERAND (arg0, 1);
11156 831 : if (TREE_CODE (tree01) == MULT_EXPR
11157 49 : && TREE_CODE (tree00) == MULT_EXPR)
11158 : {
11159 9 : tree tree0;
11160 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11161 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11162 : }
11163 : }
11164 : }
11165 :
11166 11181666 : bit_rotate:
11167 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11168 : is a rotate of A by C1 bits. */
11169 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11170 : is a rotate of A by B bits.
11171 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11172 : though in this case CODE must be | and not + or ^, otherwise
11173 : it doesn't return A when B is 0. */
11174 62128800 : {
11175 62128800 : enum tree_code code0, code1;
11176 62128800 : tree rtype;
11177 62128800 : code0 = TREE_CODE (arg0);
11178 62128800 : code1 = TREE_CODE (arg1);
11179 68627 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11180 62112480 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11181 39641 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11182 39641 : TREE_OPERAND (arg1, 0), 0)
11183 36871 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11184 36871 : TYPE_UNSIGNED (rtype))
11185 : /* Only create rotates in complete modes. Other cases are not
11186 : expanded properly. */
11187 62155573 : && (element_precision (rtype)
11188 53546 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11189 : {
11190 26703 : tree tree01, tree11;
11191 26703 : tree orig_tree01, orig_tree11;
11192 26703 : enum tree_code code01, code11;
11193 :
11194 26703 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11195 26703 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11196 26703 : STRIP_NOPS (tree01);
11197 26703 : STRIP_NOPS (tree11);
11198 26703 : code01 = TREE_CODE (tree01);
11199 26703 : code11 = TREE_CODE (tree11);
11200 26703 : if (code11 != MINUS_EXPR
11201 26017 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11202 : {
11203 1446 : std::swap (code0, code1);
11204 1446 : std::swap (code01, code11);
11205 1446 : std::swap (tree01, tree11);
11206 1446 : std::swap (orig_tree01, orig_tree11);
11207 : }
11208 53406 : if (code01 == INTEGER_CST
11209 3152 : && code11 == INTEGER_CST
11210 33005 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11211 33005 : == element_precision (rtype)))
11212 : {
11213 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11214 3011 : rtype, TREE_OPERAND (arg0, 0),
11215 : code0 == LSHIFT_EXPR
11216 : ? orig_tree01 : orig_tree11);
11217 3011 : return fold_convert_loc (loc, type, tem);
11218 : }
11219 23692 : else if (code11 == MINUS_EXPR)
11220 : {
11221 941 : tree tree110, tree111;
11222 941 : tree110 = TREE_OPERAND (tree11, 0);
11223 941 : tree111 = TREE_OPERAND (tree11, 1);
11224 941 : STRIP_NOPS (tree110);
11225 941 : STRIP_NOPS (tree111);
11226 941 : if (TREE_CODE (tree110) == INTEGER_CST
11227 930 : && compare_tree_int (tree110,
11228 930 : element_precision (rtype)) == 0
11229 1855 : && operand_equal_p (tree01, tree111, 0))
11230 : {
11231 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11232 : ? LROTATE_EXPR : RROTATE_EXPR),
11233 558 : rtype, TREE_OPERAND (arg0, 0),
11234 : orig_tree01);
11235 558 : return fold_convert_loc (loc, type, tem);
11236 : }
11237 : }
11238 22751 : else if (code == BIT_IOR_EXPR
11239 21637 : && code11 == BIT_AND_EXPR
11240 44313 : && pow2p_hwi (element_precision (rtype)))
11241 : {
11242 21562 : tree tree110, tree111;
11243 21562 : tree110 = TREE_OPERAND (tree11, 0);
11244 21562 : tree111 = TREE_OPERAND (tree11, 1);
11245 21562 : STRIP_NOPS (tree110);
11246 21562 : STRIP_NOPS (tree111);
11247 21562 : if (TREE_CODE (tree110) == NEGATE_EXPR
11248 21139 : && TREE_CODE (tree111) == INTEGER_CST
11249 21139 : && compare_tree_int (tree111,
11250 21139 : element_precision (rtype) - 1) == 0
11251 42687 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11252 : {
11253 31583 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11254 : ? LROTATE_EXPR : RROTATE_EXPR),
11255 21079 : rtype, TREE_OPERAND (arg0, 0),
11256 : orig_tree01);
11257 21079 : return fold_convert_loc (loc, type, tem);
11258 : }
11259 : }
11260 : }
11261 : }
11262 :
11263 152719796 : associate:
11264 : /* In most languages, can't associate operations on floats through
11265 : parentheses. Rather than remember where the parentheses were, we
11266 : don't associate floats at all, unless the user has specified
11267 : -fassociative-math.
11268 : And, we need to make sure type is not saturating. */
11269 :
11270 152719796 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11271 113089309 : && !TYPE_SATURATING (type)
11272 265809105 : && !TYPE_OVERFLOW_SANITIZED (type))
11273 : {
11274 113061063 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11275 113061063 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11276 113061063 : tree atype = type;
11277 113061063 : bool ok = true;
11278 :
11279 : /* Split both trees into variables, constants, and literals. Then
11280 : associate each group together, the constants with literals,
11281 : then the result with variables. This increases the chances of
11282 : literals being recombined later and of generating relocatable
11283 : expressions for the sum of a constant and literal. */
11284 113061063 : var0 = split_tree (arg0, type, code,
11285 : &minus_var0, &con0, &minus_con0,
11286 : &lit0, &minus_lit0, 0);
11287 113061063 : var1 = split_tree (arg1, type, code,
11288 : &minus_var1, &con1, &minus_con1,
11289 : &lit1, &minus_lit1, code == MINUS_EXPR);
11290 :
11291 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11292 113061063 : if (code == MINUS_EXPR)
11293 12471568 : code = PLUS_EXPR;
11294 :
11295 : /* With undefined overflow prefer doing association in a type
11296 : which wraps on overflow, if that is one of the operand types. */
11297 113060832 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11298 224877020 : && !TYPE_OVERFLOW_WRAPS (type))
11299 : {
11300 60127817 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11301 59484996 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11302 765842 : atype = TREE_TYPE (arg0);
11303 58560281 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11304 58332863 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11305 233662 : atype = TREE_TYPE (arg1);
11306 30312259 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11307 : }
11308 :
11309 : /* With undefined overflow we can only associate constants with one
11310 : variable, and constants whose association doesn't overflow. */
11311 113060832 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11312 224877020 : && !TYPE_OVERFLOW_WRAPS (atype))
11313 : {
11314 29312755 : if ((var0 && var1) || (minus_var0 && minus_var1))
11315 : {
11316 : /* ??? If split_tree would handle NEGATE_EXPR we could
11317 : simply reject these cases and the allowed cases would
11318 : be the var0/minus_var1 ones. */
11319 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11320 5581675 : tree tmp1 = var1 ? var1 : minus_var1;
11321 5581675 : bool one_neg = false;
11322 :
11323 5581675 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11324 : {
11325 1237 : tmp0 = TREE_OPERAND (tmp0, 0);
11326 1237 : one_neg = !one_neg;
11327 : }
11328 4940137 : if (CONVERT_EXPR_P (tmp0)
11329 665902 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11330 6246627 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11331 664952 : <= TYPE_PRECISION (atype)))
11332 652733 : tmp0 = TREE_OPERAND (tmp0, 0);
11333 5581675 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11334 : {
11335 168 : tmp1 = TREE_OPERAND (tmp1, 0);
11336 168 : one_neg = !one_neg;
11337 : }
11338 5260890 : if (CONVERT_EXPR_P (tmp1)
11339 385993 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11340 5967544 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11341 385869 : <= TYPE_PRECISION (atype)))
11342 369092 : tmp1 = TREE_OPERAND (tmp1, 0);
11343 : /* The only case we can still associate with two variables
11344 : is if they cancel out. */
11345 5581675 : if (!one_neg
11346 5581675 : || !operand_equal_p (tmp0, tmp1, 0))
11347 : ok = false;
11348 : }
11349 23352308 : else if ((var0 && minus_var1
11350 4057699 : && ! operand_equal_p (var0, minus_var1, 0))
11351 43025690 : || (minus_var0 && var1
11352 11392 : && ! operand_equal_p (minus_var0, var1, 0)))
11353 : ok = false;
11354 : }
11355 :
11356 : /* Only do something if we found more than two objects. Otherwise,
11357 : nothing has changed and we risk infinite recursion. */
11358 : if (ok
11359 103410370 : && ((var0 != 0) + (var1 != 0)
11360 103410370 : + (minus_var0 != 0) + (minus_var1 != 0)
11361 103410370 : + (con0 != 0) + (con1 != 0)
11362 103410370 : + (minus_con0 != 0) + (minus_con1 != 0)
11363 103410370 : + (lit0 != 0) + (lit1 != 0)
11364 103410370 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11365 : {
11366 1996617 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11367 3993234 : int minus_var0_origin
11368 1996617 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11369 1996617 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11370 3993234 : int minus_con0_origin
11371 1996617 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11372 1996617 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11373 3993234 : int minus_lit0_origin
11374 1996617 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11375 1996617 : var0 = associate_trees (loc, var0, var1, code, atype);
11376 1996617 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11377 : code, atype);
11378 1996617 : con0 = associate_trees (loc, con0, con1, code, atype);
11379 1996617 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11380 : code, atype);
11381 1996617 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11382 1996617 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11383 : code, atype);
11384 :
11385 1996617 : if (minus_var0 && var0)
11386 : {
11387 1311330 : var0_origin |= minus_var0_origin;
11388 1311330 : var0 = associate_trees (loc, var0, minus_var0,
11389 : MINUS_EXPR, atype);
11390 1311330 : minus_var0 = 0;
11391 1311330 : minus_var0_origin = 0;
11392 : }
11393 1996617 : if (minus_con0 && con0)
11394 : {
11395 3566 : con0_origin |= minus_con0_origin;
11396 3566 : con0 = associate_trees (loc, con0, minus_con0,
11397 : MINUS_EXPR, atype);
11398 3566 : minus_con0 = 0;
11399 3566 : minus_con0_origin = 0;
11400 : }
11401 :
11402 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11403 : greater than the positive part. Otherwise, the multiplicative
11404 : folding code (i.e extract_muldiv) may be fooled in case
11405 : unsigned constants are subtracted, like in the following
11406 : example: ((X*2 + 4) - 8U)/2. */
11407 1996617 : if (minus_lit0 && lit0)
11408 : {
11409 229986 : if (TREE_CODE (lit0) == INTEGER_CST
11410 229986 : && TREE_CODE (minus_lit0) == INTEGER_CST
11411 229986 : && tree_int_cst_lt (lit0, minus_lit0)
11412 : /* But avoid ending up with only negated parts. */
11413 289402 : && (var0 || con0))
11414 : {
11415 55337 : minus_lit0_origin |= lit0_origin;
11416 55337 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11417 : MINUS_EXPR, atype);
11418 55337 : lit0 = 0;
11419 55337 : lit0_origin = 0;
11420 : }
11421 : else
11422 : {
11423 174649 : lit0_origin |= minus_lit0_origin;
11424 174649 : lit0 = associate_trees (loc, lit0, minus_lit0,
11425 : MINUS_EXPR, atype);
11426 174649 : minus_lit0 = 0;
11427 174649 : minus_lit0_origin = 0;
11428 : }
11429 : }
11430 :
11431 : /* Don't introduce overflows through reassociation. */
11432 1331700 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11433 3328279 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11434 1996617 : return NULL_TREE;
11435 :
11436 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11437 1996579 : con0_origin |= lit0_origin;
11438 1996579 : con0 = associate_trees (loc, con0, lit0, code, atype);
11439 1996579 : minus_con0_origin |= minus_lit0_origin;
11440 1996579 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11441 : code, atype);
11442 :
11443 : /* Eliminate minus_con0. */
11444 1996579 : if (minus_con0)
11445 : {
11446 669612 : if (con0)
11447 : {
11448 15633 : con0_origin |= minus_con0_origin;
11449 15633 : con0 = associate_trees (loc, con0, minus_con0,
11450 : MINUS_EXPR, atype);
11451 : }
11452 653979 : else if (var0)
11453 : {
11454 653979 : var0_origin |= minus_con0_origin;
11455 653979 : var0 = associate_trees (loc, var0, minus_con0,
11456 : MINUS_EXPR, atype);
11457 : }
11458 : else
11459 0 : gcc_unreachable ();
11460 : }
11461 :
11462 : /* Eliminate minus_var0. */
11463 1996579 : if (minus_var0)
11464 : {
11465 341682 : if (con0)
11466 : {
11467 341682 : con0_origin |= minus_var0_origin;
11468 341682 : con0 = associate_trees (loc, con0, minus_var0,
11469 : MINUS_EXPR, atype);
11470 : }
11471 : else
11472 0 : gcc_unreachable ();
11473 : }
11474 :
11475 : /* Reassociate only if there has been any actual association
11476 : between subtrees from op0 and subtrees from op1 in at
11477 : least one of the operands, otherwise we risk infinite
11478 : recursion. See PR114084. */
11479 1996579 : if (var0_origin != 3 && con0_origin != 3)
11480 : return NULL_TREE;
11481 :
11482 1994938 : return
11483 1994938 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11484 1994938 : code, atype));
11485 : }
11486 : }
11487 :
11488 : return NULL_TREE;
11489 :
11490 22847232 : case POINTER_DIFF_EXPR:
11491 22847232 : case MINUS_EXPR:
11492 : /* Fold &a[i] - &a[j] to i-j. */
11493 22847232 : if (TREE_CODE (arg0) == ADDR_EXPR
11494 44400 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11495 6177 : && TREE_CODE (arg1) == ADDR_EXPR
11496 22847836 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11497 : {
11498 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11499 38 : TREE_OPERAND (arg0, 0),
11500 38 : TREE_OPERAND (arg1, 0),
11501 : code
11502 : == POINTER_DIFF_EXPR);
11503 38 : if (tem)
11504 : return tem;
11505 : }
11506 :
11507 : /* Further transformations are not for pointers. */
11508 22847218 : if (code == POINTER_DIFF_EXPR)
11509 : return NULL_TREE;
11510 :
11511 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11512 20272629 : if (TREE_CODE (arg0) == NEGATE_EXPR
11513 140494 : && negate_expr_p (op1)
11514 : /* If arg0 is e.g. unsigned int and type is int, then this could
11515 : introduce UB, because if A is INT_MIN at runtime, the original
11516 : expression can be well defined while the latter is not.
11517 : See PR83269. */
11518 20273464 : && !(ANY_INTEGRAL_TYPE_P (type)
11519 835 : && TYPE_OVERFLOW_UNDEFINED (type)
11520 823 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11521 823 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11522 828 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11523 : fold_convert_loc (loc, type,
11524 1656 : TREE_OPERAND (arg0, 0)));
11525 :
11526 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11527 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11528 : signed zeros are involved. */
11529 20271801 : if (!HONOR_SNANS (arg0)
11530 20270650 : && !HONOR_SIGNED_ZEROS (arg0)
11531 33544750 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11532 : {
11533 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11534 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11535 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11536 53 : bool arg0rz = false, arg0iz = false;
11537 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11538 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11539 : {
11540 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11541 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11542 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11543 : {
11544 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11545 : arg1r ? arg1r
11546 0 : : build1 (REALPART_EXPR, rtype, arg1));
11547 9 : tree ip = arg0i ? arg0i
11548 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11549 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11550 : }
11551 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11552 : {
11553 15 : tree rp = arg0r ? arg0r
11554 0 : : build1 (REALPART_EXPR, rtype, arg0);
11555 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11556 : arg1i ? arg1i
11557 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11558 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11559 : }
11560 : }
11561 : }
11562 :
11563 : /* A - B -> A + (-B) if B is easily negatable. */
11564 20271777 : if (negate_expr_p (op1)
11565 746205 : && ! TYPE_OVERFLOW_SANITIZED (type)
11566 21015471 : && ((FLOAT_TYPE_P (type)
11567 : /* Avoid this transformation if B is a positive REAL_CST. */
11568 65 : && (TREE_CODE (op1) != REAL_CST
11569 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11570 743629 : || INTEGRAL_TYPE_P (type)))
11571 743490 : return fold_build2_loc (loc, PLUS_EXPR, type,
11572 : fold_convert_loc (loc, type, arg0),
11573 743490 : negate_expr (op1));
11574 :
11575 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11576 : one. Make sure the type is not saturating and has the signedness of
11577 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11578 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11579 19528287 : if ((TREE_CODE (arg0) == MULT_EXPR
11580 18272472 : || TREE_CODE (arg1) == MULT_EXPR)
11581 2578560 : && !TYPE_SATURATING (type)
11582 2578560 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11583 2443624 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11584 21917567 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11585 : {
11586 368279 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11587 368279 : if (tem)
11588 : return tem;
11589 : }
11590 :
11591 19478061 : goto associate;
11592 :
11593 65104338 : case MULT_EXPR:
11594 65104338 : if (! FLOAT_TYPE_P (type))
11595 : {
11596 : /* Transform x * -C into -x * C if x is easily negatable. */
11597 43604365 : if (TREE_CODE (op1) == INTEGER_CST
11598 40712377 : && tree_int_cst_sgn (op1) == -1
11599 214818 : && negate_expr_p (op0)
11600 340 : && negate_expr_p (op1)
11601 324 : && (tem = negate_expr (op1)) != op1
11602 43604689 : && ! TREE_OVERFLOW (tem))
11603 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11604 : fold_convert_loc (loc, type,
11605 324 : negate_expr (op0)), tem);
11606 :
11607 43604041 : if (TREE_CODE (arg1) == INTEGER_CST
11608 43604041 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11609 : {
11610 591852 : return fold_convert_loc (loc, type, tem);
11611 : }
11612 :
11613 : /* Optimize z * conj(z) for integer complex numbers. */
11614 43012189 : if (TREE_CODE (arg0) == CONJ_EXPR
11615 43012189 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11616 1 : return fold_mult_zconjz (loc, type, arg1);
11617 43012188 : if (TREE_CODE (arg1) == CONJ_EXPR
11618 43012188 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11619 0 : return fold_mult_zconjz (loc, type, arg0);
11620 : }
11621 : else
11622 : {
11623 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11624 : This is not the same for NaNs or if signed zeros are
11625 : involved. */
11626 21499973 : if (!HONOR_NANS (arg0)
11627 32810 : && !HONOR_SIGNED_ZEROS (arg0)
11628 32509 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11629 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11630 21500198 : && real_zerop (TREE_REALPART (arg1)))
11631 : {
11632 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11633 218 : if (real_onep (TREE_IMAGPART (arg1)))
11634 : {
11635 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11636 63 : arg0 = save_expr (arg0);
11637 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11638 : rtype, arg0);
11639 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11640 : rtype, arg0);
11641 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11642 : negate_expr (iarg0),
11643 208 : rarg0);
11644 : }
11645 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11646 : {
11647 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11648 0 : arg0 = save_expr (arg0);
11649 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11650 : rtype, arg0);
11651 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11652 : rtype, arg0);
11653 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11654 : iarg0,
11655 10 : negate_expr (rarg0));
11656 : }
11657 : }
11658 :
11659 : /* Optimize z * conj(z) for floating point complex numbers.
11660 : Guarded by flag_unsafe_math_optimizations as non-finite
11661 : imaginary components don't produce scalar results. */
11662 21499755 : if (flag_unsafe_math_optimizations
11663 32338 : && TREE_CODE (arg0) == CONJ_EXPR
11664 21499757 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11665 1 : return fold_mult_zconjz (loc, type, arg1);
11666 21499754 : if (flag_unsafe_math_optimizations
11667 32337 : && TREE_CODE (arg1) == CONJ_EXPR
11668 21499758 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11669 0 : return fold_mult_zconjz (loc, type, arg0);
11670 : }
11671 64511942 : goto associate;
11672 :
11673 1843994 : case BIT_IOR_EXPR:
11674 : /* Canonicalize (X & C1) | C2. */
11675 1843994 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11676 79045 : && TREE_CODE (arg1) == INTEGER_CST
11677 1878264 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11678 : {
11679 34262 : int width = TYPE_PRECISION (type), w;
11680 34262 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11681 34262 : wide_int c2 = wi::to_wide (arg1);
11682 :
11683 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11684 34262 : if ((c1 & c2) == c1)
11685 0 : return omit_one_operand_loc (loc, type, arg1,
11686 0 : TREE_OPERAND (arg0, 0));
11687 :
11688 34262 : wide_int msk = wi::mask (width, false,
11689 34262 : TYPE_PRECISION (TREE_TYPE (arg1)));
11690 :
11691 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11692 34262 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11693 : {
11694 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11695 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11696 : }
11697 :
11698 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11699 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11700 : mode which allows further optimizations. */
11701 34256 : c1 &= msk;
11702 34256 : c2 &= msk;
11703 34256 : wide_int c3 = wi::bit_and_not (c1, c2);
11704 107962 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11705 : {
11706 73948 : wide_int mask = wi::mask (w, false,
11707 73948 : TYPE_PRECISION (type));
11708 147896 : if (((c1 | c2) & mask) == mask
11709 147896 : && wi::bit_and_not (c1, mask) == 0)
11710 : {
11711 242 : c3 = mask;
11712 242 : break;
11713 : }
11714 73948 : }
11715 :
11716 34256 : if (c3 != c1)
11717 : {
11718 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11719 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11720 558 : wide_int_to_tree (type, c3));
11721 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11722 : }
11723 35384 : }
11724 :
11725 : /* See if this can be simplified into a rotate first. If that
11726 : is unsuccessful continue in the association code. */
11727 1843430 : goto bit_rotate;
11728 :
11729 907625 : case BIT_XOR_EXPR:
11730 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11731 907625 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11732 2956 : && INTEGRAL_TYPE_P (type)
11733 2351 : && integer_onep (TREE_OPERAND (arg0, 1))
11734 908148 : && integer_onep (arg1))
11735 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11736 0 : build_zero_cst (TREE_TYPE (arg0)));
11737 :
11738 : /* See if this can be simplified into a rotate first. If that
11739 : is unsuccessful continue in the association code. */
11740 907625 : goto bit_rotate;
11741 :
11742 6170007 : case BIT_AND_EXPR:
11743 : /* Fold !X & 1 as X == 0. */
11744 6170007 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11745 6170007 : && integer_onep (arg1))
11746 : {
11747 0 : tem = TREE_OPERAND (arg0, 0);
11748 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11749 0 : build_zero_cst (TREE_TYPE (tem)));
11750 : }
11751 :
11752 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11753 : multiple of 1 << CST. */
11754 6170007 : if (TREE_CODE (arg1) == INTEGER_CST)
11755 : {
11756 4389018 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11757 4389018 : wide_int ncst1 = -cst1;
11758 4389018 : if ((cst1 & ncst1) == ncst1
11759 4543599 : && multiple_of_p (type, arg0,
11760 4543599 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11761 467 : return fold_convert_loc (loc, type, arg0);
11762 4389018 : }
11763 :
11764 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11765 : bits from CST2. */
11766 6169540 : if (TREE_CODE (arg1) == INTEGER_CST
11767 4388551 : && TREE_CODE (arg0) == MULT_EXPR
11768 6304312 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11769 : {
11770 134726 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11771 134726 : wide_int masked
11772 134726 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11773 :
11774 134726 : if (masked == 0)
11775 4955 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11776 4955 : arg0, arg1);
11777 129771 : else if (masked != warg1)
11778 : {
11779 : /* Avoid the transform if arg1 is a mask of some
11780 : mode which allows further optimizations. */
11781 630 : int pop = wi::popcount (warg1);
11782 652 : if (!(pop >= BITS_PER_UNIT
11783 48 : && pow2p_hwi (pop)
11784 674 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11785 1216 : return fold_build2_loc (loc, code, type, op0,
11786 1216 : wide_int_to_tree (type, masked));
11787 : }
11788 134726 : }
11789 :
11790 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11791 4382988 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11792 6336658 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11793 : {
11794 97051 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11795 :
11796 97051 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11797 97051 : if (mask == -1)
11798 2419 : return
11799 2419 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11800 97051 : }
11801 :
11802 6161558 : goto associate;
11803 :
11804 5885823 : case RDIV_EXPR:
11805 : /* Don't touch a floating-point divide by zero unless the mode
11806 : of the constant can represent infinity. */
11807 5885823 : if (TREE_CODE (arg1) == REAL_CST
11808 2958373 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11809 5885823 : && real_zerop (arg1))
11810 0 : return NULL_TREE;
11811 :
11812 : /* (-A) / (-B) -> A / B */
11813 5885823 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11814 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11815 3 : TREE_OPERAND (arg0, 0),
11816 3 : negate_expr (arg1));
11817 5885820 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11818 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11819 : negate_expr (arg0),
11820 0 : TREE_OPERAND (arg1, 0));
11821 : return NULL_TREE;
11822 :
11823 2194958 : case TRUNC_DIV_EXPR:
11824 : /* Fall through */
11825 :
11826 2194958 : case FLOOR_DIV_EXPR:
11827 : /* Simplify A / (B << N) where A and B are positive and B is
11828 : a power of 2, to A >> (N + log2(B)). */
11829 2194958 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11830 2194958 : && (TYPE_UNSIGNED (type)
11831 8 : || tree_expr_nonnegative_p (op0)))
11832 : {
11833 17 : tree sval = TREE_OPERAND (arg1, 0);
11834 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11835 : {
11836 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11837 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11838 16 : wi::exact_log2 (wi::to_wide (sval)));
11839 :
11840 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11841 : sh_cnt, pow2);
11842 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11843 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11844 : }
11845 : }
11846 :
11847 : /* Fall through */
11848 :
11849 3530977 : case ROUND_DIV_EXPR:
11850 3530977 : case CEIL_DIV_EXPR:
11851 3530977 : case EXACT_DIV_EXPR:
11852 3530977 : if (integer_zerop (arg1))
11853 : return NULL_TREE;
11854 :
11855 : /* Convert -A / -B to A / B when the type is signed and overflow is
11856 : undefined. */
11857 3527952 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11858 958021 : && TREE_CODE (op0) == NEGATE_EXPR
11859 3528014 : && negate_expr_p (op1))
11860 60 : return fold_build2_loc (loc, code, type,
11861 : fold_convert_loc (loc, type,
11862 30 : TREE_OPERAND (arg0, 0)),
11863 30 : negate_expr (op1));
11864 3527922 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11865 957991 : && TREE_CODE (arg1) == NEGATE_EXPR
11866 3528166 : && negate_expr_p (op0))
11867 36 : return fold_build2_loc (loc, code, type,
11868 : negate_expr (op0),
11869 : fold_convert_loc (loc, type,
11870 72 : TREE_OPERAND (arg1, 0)));
11871 :
11872 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11873 : operation, EXACT_DIV_EXPR.
11874 :
11875 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11876 : At one time others generated faster code, it's not clear if they do
11877 : after the last round to changes to the DIV code in expmed.cc. */
11878 3527886 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11879 3527886 : && multiple_of_p (type, arg0, arg1))
11880 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11881 : fold_convert (type, arg0),
11882 0 : fold_convert (type, arg1));
11883 :
11884 3527886 : if (TREE_CODE (arg1) == INTEGER_CST
11885 3527886 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11886 9234 : return fold_convert_loc (loc, type, tem);
11887 :
11888 : return NULL_TREE;
11889 :
11890 899284 : case CEIL_MOD_EXPR:
11891 899284 : case FLOOR_MOD_EXPR:
11892 899284 : case ROUND_MOD_EXPR:
11893 899284 : case TRUNC_MOD_EXPR:
11894 899284 : if (TREE_CODE (arg1) == INTEGER_CST
11895 899284 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11896 0 : return fold_convert_loc (loc, type, tem);
11897 :
11898 : return NULL_TREE;
11899 :
11900 2160967 : case LROTATE_EXPR:
11901 2160967 : case RROTATE_EXPR:
11902 2160967 : case RSHIFT_EXPR:
11903 2160967 : case LSHIFT_EXPR:
11904 : /* Since negative shift count is not well-defined,
11905 : don't try to compute it in the compiler. */
11906 2160967 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11907 : return NULL_TREE;
11908 :
11909 2159926 : prec = element_precision (type);
11910 :
11911 : /* If we have a rotate of a bit operation with the rotate count and
11912 : the second operand of the bit operation both constant,
11913 : permute the two operations. */
11914 2693 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11915 2159 : && (TREE_CODE (arg0) == BIT_AND_EXPR
11916 2159 : || TREE_CODE (arg0) == BIT_IOR_EXPR
11917 2159 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
11918 2159926 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11919 : {
11920 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11921 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11922 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
11923 : fold_build2_loc (loc, code, type,
11924 : arg00, arg1),
11925 : fold_build2_loc (loc, code, type,
11926 0 : arg01, arg1));
11927 : }
11928 :
11929 : return NULL_TREE;
11930 :
11931 464083 : case MIN_EXPR:
11932 464083 : case MAX_EXPR:
11933 464083 : goto associate;
11934 :
11935 6319673 : case TRUTH_ANDIF_EXPR:
11936 : /* Note that the operands of this must be ints
11937 : and their values must be 0 or 1.
11938 : ("true" is a fixed value perhaps depending on the language.) */
11939 : /* If first arg is constant zero, return it. */
11940 6319673 : if (integer_zerop (arg0))
11941 1591203 : return fold_convert_loc (loc, type, arg0);
11942 : /* FALLTHRU */
11943 15402295 : case TRUTH_AND_EXPR:
11944 : /* If either arg is constant true, drop it. */
11945 15402295 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11946 1417036 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11947 802692 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11948 : /* Preserve sequence points. */
11949 14743045 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11950 734115 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11951 : /* If second arg is constant zero, result is zero, but first arg
11952 : must be evaluated. */
11953 13251144 : if (integer_zerop (arg1))
11954 44906 : return omit_one_operand_loc (loc, type, arg1, arg0);
11955 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11956 : case will be handled here. */
11957 13206238 : if (integer_zerop (arg0))
11958 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
11959 :
11960 : /* !X && X is always false. */
11961 13206238 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11962 13206238 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11963 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11964 : /* X && !X is always false. */
11965 13206238 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11966 13206238 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11967 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11968 :
11969 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11970 : means A >= Y && A != MAX, but in this case we know that
11971 : A < X <= MAX. */
11972 :
11973 13206238 : if (!TREE_SIDE_EFFECTS (arg0)
11974 13206238 : && !TREE_SIDE_EFFECTS (arg1))
11975 : {
11976 11803194 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11977 11803194 : if (tem && !operand_equal_p (tem, arg0, 0))
11978 442 : return fold_convert (type,
11979 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
11980 : tem, arg1));
11981 :
11982 11802752 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11983 11802752 : if (tem && !operand_equal_p (tem, arg1, 0))
11984 9773 : return fold_convert (type,
11985 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
11986 : arg0, tem));
11987 : }
11988 :
11989 13196023 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
11990 : != NULL_TREE)
11991 : return tem;
11992 :
11993 : return NULL_TREE;
11994 :
11995 3249530 : case TRUTH_ORIF_EXPR:
11996 : /* Note that the operands of this must be ints
11997 : and their values must be 0 or true.
11998 : ("true" is a fixed value perhaps depending on the language.) */
11999 : /* If first arg is constant true, return it. */
12000 3249530 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12001 133781 : return fold_convert_loc (loc, type, arg0);
12002 : /* FALLTHRU */
12003 12519892 : case TRUTH_OR_EXPR:
12004 : /* If either arg is constant zero, drop it. */
12005 12519892 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12006 214656 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12007 484890 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12008 : /* Preserve sequence points. */
12009 12737692 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12010 421310 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12011 : /* If second arg is constant true, result is true, but we must
12012 : evaluate first arg. */
12013 11883926 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12014 52434 : return omit_one_operand_loc (loc, type, arg1, arg0);
12015 : /* Likewise for first arg, but note this only occurs here for
12016 : TRUTH_OR_EXPR. */
12017 11831492 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12018 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12019 :
12020 : /* !X || X is always true. */
12021 11831492 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12022 11831492 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12023 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12024 : /* X || !X is always true. */
12025 11831492 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12026 11831492 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12027 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12028 :
12029 : /* (X && !Y) || (!X && Y) is X ^ Y */
12030 11831491 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12031 1621 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12032 : {
12033 669 : tree a0, a1, l0, l1, n0, n1;
12034 :
12035 669 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12036 669 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12037 :
12038 669 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12039 669 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12040 :
12041 669 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12042 669 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12043 :
12044 669 : if ((operand_equal_p (n0, a0, 0)
12045 18 : && operand_equal_p (n1, a1, 0))
12046 677 : || (operand_equal_p (n0, a1, 0)
12047 3 : && operand_equal_p (n1, a0, 0)))
12048 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12049 : }
12050 :
12051 11831478 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12052 : != NULL_TREE)
12053 : return tem;
12054 :
12055 : return NULL_TREE;
12056 :
12057 74256 : case TRUTH_XOR_EXPR:
12058 : /* If the second arg is constant zero, drop it. */
12059 74256 : if (integer_zerop (arg1))
12060 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12061 : /* If the second arg is constant true, this is a logical inversion. */
12062 74256 : if (integer_onep (arg1))
12063 : {
12064 0 : tem = invert_truthvalue_loc (loc, arg0);
12065 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12066 : }
12067 : /* Identical arguments cancel to zero. */
12068 74256 : if (operand_equal_p (arg0, arg1, 0))
12069 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12070 :
12071 : /* !X ^ X is always true. */
12072 74256 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12073 74256 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12074 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12075 :
12076 : /* X ^ !X is always true. */
12077 74256 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12078 74256 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12079 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12080 :
12081 : return NULL_TREE;
12082 :
12083 47430195 : case EQ_EXPR:
12084 47430195 : case NE_EXPR:
12085 47430195 : STRIP_NOPS (arg0);
12086 47430195 : STRIP_NOPS (arg1);
12087 :
12088 47430195 : tem = fold_comparison (loc, code, type, op0, op1);
12089 47430195 : if (tem != NULL_TREE)
12090 : return tem;
12091 :
12092 : /* bool_var != 1 becomes !bool_var. */
12093 48569906 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12094 47463218 : && code == NE_EXPR)
12095 39396 : return fold_convert_loc (loc, type,
12096 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12097 78792 : TREE_TYPE (arg0), arg0));
12098 :
12099 : /* bool_var == 0 becomes !bool_var. */
12100 48491114 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12101 48313342 : && code == EQ_EXPR)
12102 193953 : return fold_convert_loc (loc, type,
12103 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12104 387906 : TREE_TYPE (arg0), arg0));
12105 :
12106 : /* !exp != 0 becomes !exp */
12107 476302 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12108 47661014 : && code == NE_EXPR)
12109 468608 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12110 :
12111 : /* If this is an EQ or NE comparison with zero and ARG0 is
12112 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12113 : two operations, but the latter can be done in one less insn
12114 : on machines that have only two-operand insns or on which a
12115 : constant cannot be the first operand. */
12116 46716815 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12117 46716815 : && integer_zerop (arg1))
12118 : {
12119 1488952 : tree arg00 = TREE_OPERAND (arg0, 0);
12120 1488952 : tree arg01 = TREE_OPERAND (arg0, 1);
12121 1488952 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12122 1488952 : && integer_onep (TREE_OPERAND (arg00, 0)))
12123 : {
12124 4311 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12125 4311 : arg01, TREE_OPERAND (arg00, 1));
12126 4311 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12127 4311 : build_one_cst (TREE_TYPE (arg0)));
12128 4311 : return fold_build2_loc (loc, code, type,
12129 4311 : fold_convert_loc (loc, TREE_TYPE (arg1),
12130 4311 : tem), arg1);
12131 : }
12132 1484641 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12133 1484641 : && integer_onep (TREE_OPERAND (arg01, 0)))
12134 : {
12135 365 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12136 365 : arg00, TREE_OPERAND (arg01, 1));
12137 365 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12138 365 : build_one_cst (TREE_TYPE (arg0)));
12139 365 : return fold_build2_loc (loc, code, type,
12140 365 : fold_convert_loc (loc, TREE_TYPE (arg1),
12141 365 : tem), arg1);
12142 : }
12143 : }
12144 :
12145 : /* If this is a comparison of a field, we may be able to simplify it. */
12146 46712139 : if ((TREE_CODE (arg0) == COMPONENT_REF
12147 46712139 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12148 : /* Handle the constant case even without -O
12149 : to make sure the warnings are given. */
12150 4666483 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12151 : {
12152 4363325 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12153 4363325 : if (t1)
12154 : return t1;
12155 : }
12156 :
12157 : /* Optimize comparisons of strlen vs zero to a compare of the
12158 : first character of the string vs zero. To wit,
12159 : strlen(ptr) == 0 => *ptr == 0
12160 : strlen(ptr) != 0 => *ptr != 0
12161 : Other cases should reduce to one of these two (or a constant)
12162 : due to the return value of strlen being unsigned. */
12163 46021413 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12164 : {
12165 2840774 : tree fndecl = get_callee_fndecl (arg0);
12166 :
12167 2840774 : if (fndecl
12168 2839697 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12169 549 : && call_expr_nargs (arg0) == 1
12170 2841323 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12171 : == POINTER_TYPE))
12172 : {
12173 549 : tree ptrtype
12174 549 : = build_pointer_type (build_qualified_type (char_type_node,
12175 : TYPE_QUAL_CONST));
12176 1098 : tree ptr = fold_convert_loc (loc, ptrtype,
12177 549 : CALL_EXPR_ARG (arg0, 0));
12178 549 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12179 549 : return fold_build2_loc (loc, code, type, iref,
12180 549 : build_int_cst (TREE_TYPE (iref), 0));
12181 : }
12182 : }
12183 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12184 : (X & C) == 0 when C is a single bit. */
12185 46020864 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12186 1647292 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12187 853 : && integer_zerop (arg1)
12188 46021319 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12189 : {
12190 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12191 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12192 140 : TREE_OPERAND (arg0, 1));
12193 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12194 : type, tem,
12195 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12196 140 : arg1));
12197 : }
12198 :
12199 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12200 : constant C is a power of two, i.e. a single bit. */
12201 46020724 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12202 4667 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12203 0 : && integer_zerop (arg1)
12204 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12205 46020724 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12206 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12207 : {
12208 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12209 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12210 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12211 : }
12212 :
12213 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12214 : when is C is a power of two, i.e. a single bit. */
12215 46020724 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12216 1647152 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12217 36748 : && integer_zerop (arg1)
12218 36748 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12219 46054819 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12220 34095 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12221 : {
12222 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12223 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12224 0 : arg000, TREE_OPERAND (arg0, 1));
12225 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12226 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12227 : }
12228 :
12229 46020724 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12230 4667 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12231 : {
12232 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12233 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12234 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12235 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12236 482 : tree itype = TREE_TYPE (arg0);
12237 :
12238 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12239 : operand_equal_p guarantees no side-effects so we don't need
12240 : to use omit_one_operand on Z. */
12241 482 : if (operand_equal_p (arg01, arg11, 0))
12242 8 : return fold_build2_loc (loc, code, type, arg00,
12243 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12244 8 : arg10));
12245 474 : if (operand_equal_p (arg01, arg10, 0))
12246 0 : return fold_build2_loc (loc, code, type, arg00,
12247 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12248 0 : arg11));
12249 474 : if (operand_equal_p (arg00, arg11, 0))
12250 0 : return fold_build2_loc (loc, code, type, arg01,
12251 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12252 0 : arg10));
12253 474 : if (operand_equal_p (arg00, arg10, 0))
12254 0 : return fold_build2_loc (loc, code, type, arg01,
12255 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12256 0 : arg11));
12257 :
12258 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12259 474 : if (TREE_CODE (arg01) == INTEGER_CST
12260 8 : && TREE_CODE (arg11) == INTEGER_CST)
12261 : {
12262 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12263 : fold_convert_loc (loc, itype, arg11));
12264 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12265 8 : return fold_build2_loc (loc, code, type, tem,
12266 8 : fold_convert_loc (loc, itype, arg10));
12267 : }
12268 : }
12269 :
12270 : /* Attempt to simplify equality/inequality comparisons of complex
12271 : values. Only lower the comparison if the result is known or
12272 : can be simplified to a single scalar comparison. */
12273 46020708 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12274 46018181 : || TREE_CODE (arg0) == COMPLEX_CST)
12275 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12276 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12277 : {
12278 1726 : tree real0, imag0, real1, imag1;
12279 1726 : tree rcond, icond;
12280 :
12281 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12282 : {
12283 1726 : real0 = TREE_OPERAND (arg0, 0);
12284 1726 : imag0 = TREE_OPERAND (arg0, 1);
12285 : }
12286 : else
12287 : {
12288 0 : real0 = TREE_REALPART (arg0);
12289 0 : imag0 = TREE_IMAGPART (arg0);
12290 : }
12291 :
12292 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12293 : {
12294 192 : real1 = TREE_OPERAND (arg1, 0);
12295 192 : imag1 = TREE_OPERAND (arg1, 1);
12296 : }
12297 : else
12298 : {
12299 1534 : real1 = TREE_REALPART (arg1);
12300 1534 : imag1 = TREE_IMAGPART (arg1);
12301 : }
12302 :
12303 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12304 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12305 : {
12306 11 : if (integer_zerop (rcond))
12307 : {
12308 11 : if (code == EQ_EXPR)
12309 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12310 0 : imag0, imag1);
12311 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12312 : }
12313 : else
12314 : {
12315 0 : if (code == NE_EXPR)
12316 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12317 0 : imag0, imag1);
12318 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12319 : }
12320 : }
12321 :
12322 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12323 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12324 : {
12325 9 : if (integer_zerop (icond))
12326 : {
12327 7 : if (code == EQ_EXPR)
12328 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12329 1 : real0, real1);
12330 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12331 : }
12332 : else
12333 : {
12334 2 : if (code == NE_EXPR)
12335 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12336 1 : real0, real1);
12337 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12338 : }
12339 : }
12340 : }
12341 :
12342 : return NULL_TREE;
12343 :
12344 40231554 : case LT_EXPR:
12345 40231554 : case GT_EXPR:
12346 40231554 : case LE_EXPR:
12347 40231554 : case GE_EXPR:
12348 40231554 : tem = fold_comparison (loc, code, type, op0, op1);
12349 40231554 : if (tem != NULL_TREE)
12350 : return tem;
12351 :
12352 : /* Transform comparisons of the form X +- C CMP X. */
12353 39373034 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12354 4714707 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12355 50779 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12356 39373050 : && !HONOR_SNANS (arg0))
12357 : {
12358 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12359 14 : enum tree_code code0 = TREE_CODE (arg0);
12360 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12361 :
12362 : /* (X - c) > X becomes false. */
12363 14 : if (code == GT_EXPR
12364 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12365 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12366 4 : return constant_boolean_node (0, type);
12367 :
12368 : /* Likewise (X + c) < X becomes false. */
12369 10 : if (code == LT_EXPR
12370 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12371 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12372 3 : return constant_boolean_node (0, type);
12373 :
12374 : /* Convert (X - c) <= X to true. */
12375 7 : if (!HONOR_NANS (arg1)
12376 6 : && code == LE_EXPR
12377 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12378 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12379 4 : return constant_boolean_node (1, type);
12380 :
12381 : /* Convert (X + c) >= X to true. */
12382 3 : if (!HONOR_NANS (arg1)
12383 2 : && code == GE_EXPR
12384 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12385 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12386 2 : return constant_boolean_node (1, type);
12387 : }
12388 :
12389 : /* If we are comparing an ABS_EXPR with a constant, we can
12390 : convert all the cases into explicit comparisons, but they may
12391 : well not be faster than doing the ABS and one comparison.
12392 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12393 : and a comparison, and is probably faster. */
12394 39373021 : if (code == LE_EXPR
12395 7556359 : && TREE_CODE (arg1) == INTEGER_CST
12396 5376922 : && TREE_CODE (arg0) == ABS_EXPR
12397 818 : && ! TREE_SIDE_EFFECTS (arg0)
12398 818 : && (tem = negate_expr (arg1)) != 0
12399 818 : && TREE_CODE (tem) == INTEGER_CST
12400 39373839 : && !TREE_OVERFLOW (tem))
12401 1636 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12402 : build2 (GE_EXPR, type,
12403 818 : TREE_OPERAND (arg0, 0), tem),
12404 : build2 (LE_EXPR, type,
12405 1636 : TREE_OPERAND (arg0, 0), arg1));
12406 :
12407 : /* Convert ABS_EXPR<x> >= 0 to true. */
12408 39372203 : if (code == GE_EXPR
12409 4105279 : && (integer_zerop (arg1)
12410 2995991 : || (! HONOR_NANS (arg0)
12411 2351212 : && real_zerop (arg1)))
12412 40481732 : && tree_expr_nonnegative_p (arg0))
12413 1148 : return omit_one_operand_loc (loc, type,
12414 : constant_boolean_node (true, type),
12415 1148 : arg0);
12416 :
12417 : /* Convert ABS_EXPR<x> < 0 to false. */
12418 39371055 : if (code == LT_EXPR
12419 13065124 : && (integer_zerop (arg1) || real_zerop (arg1))
12420 42584650 : && tree_expr_nonnegative_p (arg0))
12421 3289 : return omit_one_operand_loc (loc, type,
12422 : constant_boolean_node (false, type),
12423 3289 : arg0);
12424 :
12425 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12426 : and similarly for >= into !=. */
12427 39367766 : if ((code == LT_EXPR || code == GE_EXPR)
12428 17165966 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12429 5324117 : && TREE_CODE (arg1) == LSHIFT_EXPR
12430 39369269 : && integer_onep (TREE_OPERAND (arg1, 0)))
12431 4054 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12432 1355 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12433 1355 : TREE_OPERAND (arg1, 1)),
12434 2710 : build_zero_cst (TREE_TYPE (arg0)));
12435 :
12436 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12437 : otherwise Y might be >= # of bits in X's type and thus e.g.
12438 : (unsigned char) (1 << Y) for Y 15 might be 0.
12439 : If the cast is widening, then 1 << Y should have unsigned type,
12440 : otherwise if Y is number of bits in the signed shift type minus 1,
12441 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12442 : 31 might be 0xffffffff80000000. */
12443 39366411 : if ((code == LT_EXPR || code == GE_EXPR)
12444 17164611 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12445 5547406 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12446 11640197 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12447 3874011 : && CONVERT_EXPR_P (arg1)
12448 1057907 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12449 42 : && (element_precision (TREE_TYPE (arg1))
12450 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12451 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12452 14 : || (element_precision (TREE_TYPE (arg1))
12453 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12454 39366418 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12455 : {
12456 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12457 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12458 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12459 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12460 14 : build_zero_cst (TREE_TYPE (arg0)));
12461 : }
12462 :
12463 : return NULL_TREE;
12464 :
12465 4514754 : case UNORDERED_EXPR:
12466 4514754 : case ORDERED_EXPR:
12467 4514754 : case UNLT_EXPR:
12468 4514754 : case UNLE_EXPR:
12469 4514754 : case UNGT_EXPR:
12470 4514754 : case UNGE_EXPR:
12471 4514754 : case UNEQ_EXPR:
12472 4514754 : case LTGT_EXPR:
12473 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12474 4514754 : {
12475 4514754 : tree targ0 = strip_float_extensions (arg0);
12476 4514754 : tree targ1 = strip_float_extensions (arg1);
12477 4514754 : tree newtype = TREE_TYPE (targ0);
12478 :
12479 4514754 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12480 1289 : newtype = TREE_TYPE (targ1);
12481 :
12482 4514754 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12483 4514754 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12484 328 : return fold_build2_loc (loc, code, type,
12485 : fold_convert_loc (loc, newtype, targ0),
12486 328 : fold_convert_loc (loc, newtype, targ1));
12487 : }
12488 :
12489 : return NULL_TREE;
12490 :
12491 8255895 : case COMPOUND_EXPR:
12492 : /* When pedantic, a compound expression can be neither an lvalue
12493 : nor an integer constant expression. */
12494 8255895 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12495 : return NULL_TREE;
12496 : /* Don't let (0, 0) be null pointer constant. */
12497 454217 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12498 454217 : : fold_convert_loc (loc, type, arg1);
12499 : return tem;
12500 :
12501 : default:
12502 : return NULL_TREE;
12503 : } /* switch (code) */
12504 : }
12505 :
12506 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12507 : ((A & N) + B) & M -> (A + B) & M
12508 : Similarly if (N & M) == 0,
12509 : ((A | N) + B) & M -> (A + B) & M
12510 : and for - instead of + (or unary - instead of +)
12511 : and/or ^ instead of |.
12512 : If B is constant and (B & M) == 0, fold into A & M.
12513 :
12514 : This function is a helper for match.pd patterns. Return non-NULL
12515 : type in which the simplified operation should be performed only
12516 : if any optimization is possible.
12517 :
12518 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12519 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12520 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12521 : +/-. */
12522 : tree
12523 1233048 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12524 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12525 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12526 : tree *pmop)
12527 : {
12528 1233048 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12529 1233048 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12530 1233048 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12531 2466096 : if (~cst1 == 0
12532 3694086 : || (cst1 & (cst1 + 1)) != 0
12533 1027423 : || !INTEGRAL_TYPE_P (type)
12534 1027423 : || (!TYPE_OVERFLOW_WRAPS (type)
12535 41560 : && TREE_CODE (type) != INTEGER_TYPE)
12536 4518413 : || (wi::max_value (type) & cst1) != cst1)
12537 : return NULL_TREE;
12538 :
12539 1027423 : enum tree_code codes[2] = { code00, code01 };
12540 1027423 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12541 1027423 : int which = 0;
12542 1027423 : wide_int cst0;
12543 :
12544 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12545 : arg1 (M) is == (1LL << cst) - 1.
12546 : Store C into PMOP[0] and D into PMOP[1]. */
12547 1027423 : pmop[0] = arg00;
12548 1027423 : pmop[1] = arg01;
12549 1027423 : which = code != NEGATE_EXPR;
12550 :
12551 3081353 : for (; which >= 0; which--)
12552 2053930 : switch (codes[which])
12553 : {
12554 20706 : case BIT_AND_EXPR:
12555 20706 : case BIT_IOR_EXPR:
12556 20706 : case BIT_XOR_EXPR:
12557 20706 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12558 20706 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12559 20706 : if (codes[which] == BIT_AND_EXPR)
12560 : {
12561 20592 : if (cst0 != cst1)
12562 : break;
12563 : }
12564 114 : else if (cst0 != 0)
12565 : break;
12566 : /* If C or D is of the form (A & N) where
12567 : (N & M) == M, or of the form (A | N) or
12568 : (A ^ N) where (N & M) == 0, replace it with A. */
12569 19165 : pmop[which] = arg0xx[2 * which];
12570 19165 : break;
12571 2033224 : case ERROR_MARK:
12572 2033224 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12573 : break;
12574 : /* If C or D is a N where (N & M) == 0, it can be
12575 : omitted (replaced with 0). */
12576 854742 : if ((code == PLUS_EXPR
12577 204390 : || (code == MINUS_EXPR && which == 0))
12578 631761 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12579 133886 : pmop[which] = build_int_cst (type, 0);
12580 : /* Similarly, with C - N where (-N & M) == 0. */
12581 854742 : if (code == MINUS_EXPR
12582 427371 : && which == 1
12583 625120 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12584 189723 : pmop[which] = build_int_cst (type, 0);
12585 : break;
12586 0 : default:
12587 0 : gcc_unreachable ();
12588 : }
12589 :
12590 : /* Only build anything new if we optimized one or both arguments above. */
12591 1027423 : if (pmop[0] == arg00 && pmop[1] == arg01)
12592 : return NULL_TREE;
12593 :
12594 342038 : if (TYPE_OVERFLOW_WRAPS (type))
12595 : return type;
12596 : else
12597 2333 : return unsigned_type_for (type);
12598 1027423 : }
12599 :
12600 : /* Used by contains_label_[p1]. */
12601 :
12602 : struct contains_label_data
12603 : {
12604 : hash_set<tree> *pset;
12605 : bool inside_switch_p;
12606 : };
12607 :
12608 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12609 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12610 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12611 :
12612 : static tree
12613 4250454 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12614 : {
12615 4250454 : contains_label_data *d = (contains_label_data *) data;
12616 4250454 : switch (TREE_CODE (*tp))
12617 : {
12618 : case LABEL_EXPR:
12619 : return *tp;
12620 :
12621 0 : case CASE_LABEL_EXPR:
12622 0 : if (!d->inside_switch_p)
12623 : return *tp;
12624 : return NULL_TREE;
12625 :
12626 0 : case SWITCH_EXPR:
12627 0 : if (!d->inside_switch_p)
12628 : {
12629 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12630 0 : return *tp;
12631 0 : d->inside_switch_p = true;
12632 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12633 0 : return *tp;
12634 0 : d->inside_switch_p = false;
12635 0 : *walk_subtrees = 0;
12636 : }
12637 : return NULL_TREE;
12638 :
12639 6533 : case GOTO_EXPR:
12640 6533 : *walk_subtrees = 0;
12641 6533 : return NULL_TREE;
12642 :
12643 : default:
12644 : return NULL_TREE;
12645 : }
12646 : }
12647 :
12648 : /* Return whether the sub-tree ST contains a label which is accessible from
12649 : outside the sub-tree. */
12650 :
12651 : static bool
12652 308434 : contains_label_p (tree st)
12653 : {
12654 308434 : hash_set<tree> pset;
12655 308434 : contains_label_data data = { &pset, false };
12656 308434 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12657 308434 : }
12658 :
12659 : /* Fold a ternary expression of code CODE and type TYPE with operands
12660 : OP0, OP1, and OP2. Return the folded expression if folding is
12661 : successful. Otherwise, return NULL_TREE. */
12662 :
12663 : tree
12664 34934378 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12665 : tree op0, tree op1, tree op2)
12666 : {
12667 34934378 : tree tem;
12668 34934378 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12669 34934378 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12670 :
12671 34934378 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12672 : && TREE_CODE_LENGTH (code) == 3);
12673 :
12674 : /* If this is a commutative operation, and OP0 is a constant, move it
12675 : to OP1 to reduce the number of tests below. */
12676 34934378 : if (commutative_ternary_tree_code (code)
12677 34934378 : && tree_swap_operands_p (op0, op1))
12678 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12679 :
12680 34934345 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12681 34934345 : if (tem)
12682 : return tem;
12683 :
12684 : /* Strip any conversions that don't change the mode. This is safe
12685 : for every expression, except for a comparison expression because
12686 : its signedness is derived from its operands. So, in the latter
12687 : case, only strip conversions that don't change the signedness.
12688 :
12689 : Note that this is done as an internal manipulation within the
12690 : constant folder, in order to find the simplest representation of
12691 : the arguments so that their form can be studied. In any cases,
12692 : the appropriate type conversions should be put back in the tree
12693 : that will get out of the constant folder. */
12694 33954018 : if (op0)
12695 : {
12696 33887669 : arg0 = op0;
12697 33887669 : STRIP_NOPS (arg0);
12698 : }
12699 :
12700 33954018 : if (op1)
12701 : {
12702 33954018 : arg1 = op1;
12703 33954018 : STRIP_NOPS (arg1);
12704 : }
12705 :
12706 33954018 : if (op2)
12707 : {
12708 14168562 : arg2 = op2;
12709 14168562 : STRIP_NOPS (arg2);
12710 : }
12711 :
12712 33954018 : switch (code)
12713 : {
12714 19784981 : case COMPONENT_REF:
12715 19784981 : if (TREE_CODE (arg0) == CONSTRUCTOR
12716 19784981 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12717 : {
12718 : unsigned HOST_WIDE_INT idx;
12719 : tree field, value;
12720 884 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12721 679 : if (field == arg1)
12722 : return value;
12723 : }
12724 : return NULL_TREE;
12725 :
12726 11912155 : case COND_EXPR:
12727 11912155 : case VEC_COND_EXPR:
12728 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12729 : so all simple results must be passed through pedantic_non_lvalue. */
12730 11912155 : if (TREE_CODE (arg0) == INTEGER_CST)
12731 : {
12732 433688 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12733 433688 : tem = integer_zerop (arg0) ? op2 : op1;
12734 : /* Only optimize constant conditions when the selected branch
12735 : has the same type as the COND_EXPR. This avoids optimizing
12736 : away "c ? x : throw", where the throw has a void type.
12737 : Avoid throwing away that operand which contains label. */
12738 433688 : if ((!TREE_SIDE_EFFECTS (unused_op)
12739 308434 : || !contains_label_p (unused_op))
12740 737301 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12741 350949 : || VOID_TYPE_P (type)))
12742 419663 : return protected_set_expr_location_unshare (tem, loc);
12743 14025 : return NULL_TREE;
12744 : }
12745 11478467 : else if (TREE_CODE (arg0) == VECTOR_CST)
12746 : {
12747 10762 : unsigned HOST_WIDE_INT nelts;
12748 10762 : if ((TREE_CODE (arg1) == VECTOR_CST
12749 8187 : || TREE_CODE (arg1) == CONSTRUCTOR)
12750 2575 : && (TREE_CODE (arg2) == VECTOR_CST
12751 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12752 21524 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12753 : {
12754 2575 : vec_perm_builder sel (nelts, nelts, 1);
12755 25997 : for (unsigned int i = 0; i < nelts; i++)
12756 : {
12757 23422 : tree val = VECTOR_CST_ELT (arg0, i);
12758 23422 : if (integer_all_onesp (val))
12759 11633 : sel.quick_push (i);
12760 11789 : else if (integer_zerop (val))
12761 11789 : sel.quick_push (nelts + i);
12762 : else /* Currently unreachable. */
12763 1883 : return NULL_TREE;
12764 : }
12765 2575 : vec_perm_indices indices (sel, 2, nelts);
12766 2575 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12767 2575 : if (t != NULL_TREE)
12768 1883 : return t;
12769 4458 : }
12770 : }
12771 :
12772 : /* If we have A op B ? A : C, we may be able to convert this to a
12773 : simpler expression, depending on the operation and the values
12774 : of B and C. Signed zeros prevent all of these transformations,
12775 : for reasons given above each one.
12776 :
12777 : Also try swapping the arguments and inverting the conditional. */
12778 11476584 : if (COMPARISON_CLASS_P (arg0)
12779 9488184 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12780 11611414 : && !HONOR_SIGNED_ZEROS (op1))
12781 : {
12782 124090 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12783 124090 : TREE_OPERAND (arg0, 0),
12784 124090 : TREE_OPERAND (arg0, 1),
12785 : op1, op2);
12786 124090 : if (tem)
12787 : return tem;
12788 : }
12789 :
12790 11469923 : if (COMPARISON_CLASS_P (arg0)
12791 9481523 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12792 11928950 : && !HONOR_SIGNED_ZEROS (op2))
12793 : {
12794 377192 : enum tree_code comp_code = TREE_CODE (arg0);
12795 377192 : tree arg00 = TREE_OPERAND (arg0, 0);
12796 377192 : tree arg01 = TREE_OPERAND (arg0, 1);
12797 377192 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12798 377192 : if (comp_code != ERROR_MARK)
12799 377192 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12800 : arg00,
12801 : arg01,
12802 : op2, op1);
12803 377192 : if (tem)
12804 : return tem;
12805 : }
12806 :
12807 : /* If the second operand is simpler than the third, swap them
12808 : since that produces better jump optimization results. */
12809 11206083 : if (truth_value_p (TREE_CODE (arg0))
12810 11206083 : && tree_swap_operands_p (op1, op2))
12811 : {
12812 1958026 : location_t loc0 = expr_location_or (arg0, loc);
12813 : /* See if this can be inverted. If it can't, possibly because
12814 : it was a floating-point inequality comparison, don't do
12815 : anything. */
12816 1958026 : tem = fold_invert_truthvalue (loc0, arg0);
12817 1958026 : if (tem)
12818 1251442 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12819 : }
12820 :
12821 : /* Convert A ? 1 : 0 to simply A. */
12822 9954641 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12823 9570681 : : (integer_onep (op1)
12824 397424 : && !VECTOR_TYPE_P (type)))
12825 632831 : && integer_zerop (op2)
12826 : /* If we try to convert OP0 to our type, the
12827 : call to fold will try to move the conversion inside
12828 : a COND, which will recurse. In that case, the COND_EXPR
12829 : is probably the best choice, so leave it alone. */
12830 10957988 : && type == TREE_TYPE (arg0))
12831 31846 : return protected_set_expr_location_unshare (arg0, loc);
12832 :
12833 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12834 : over COND_EXPR in cases such as floating point comparisons. */
12835 9922795 : if (integer_zerop (op1)
12836 361595 : && code == COND_EXPR
12837 349971 : && integer_onep (op2)
12838 30046 : && !VECTOR_TYPE_P (type)
12839 9952841 : && truth_value_p (TREE_CODE (arg0)))
12840 28491 : return fold_convert_loc (loc, type,
12841 28491 : invert_truthvalue_loc (loc, arg0));
12842 :
12843 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12844 9894304 : if (TREE_CODE (arg0) == LT_EXPR
12845 1314307 : && integer_zerop (TREE_OPERAND (arg0, 1))
12846 36641 : && integer_zerop (op2)
12847 9895273 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12848 : {
12849 : /* sign_bit_p looks through both zero and sign extensions,
12850 : but for this optimization only sign extensions are
12851 : usable. */
12852 56 : tree tem2 = TREE_OPERAND (arg0, 0);
12853 56 : while (tem != tem2)
12854 : {
12855 0 : if (TREE_CODE (tem2) != NOP_EXPR
12856 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12857 : {
12858 : tem = NULL_TREE;
12859 : break;
12860 : }
12861 0 : tem2 = TREE_OPERAND (tem2, 0);
12862 : }
12863 : /* sign_bit_p only checks ARG1 bits within A's precision.
12864 : If <sign bit of A> has wider type than A, bits outside
12865 : of A's precision in <sign bit of A> need to be checked.
12866 : If they are all 0, this optimization needs to be done
12867 : in unsigned A's type, if they are all 1 in signed A's type,
12868 : otherwise this can't be done. */
12869 56 : if (tem
12870 56 : && TYPE_PRECISION (TREE_TYPE (tem))
12871 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
12872 112 : && TYPE_PRECISION (TREE_TYPE (tem))
12873 56 : < TYPE_PRECISION (type))
12874 : {
12875 56 : int inner_width, outer_width;
12876 56 : tree tem_type;
12877 :
12878 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12879 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12880 56 : if (outer_width > TYPE_PRECISION (type))
12881 0 : outer_width = TYPE_PRECISION (type);
12882 :
12883 56 : wide_int mask = wi::shifted_mask
12884 56 : (inner_width, outer_width - inner_width, false,
12885 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
12886 :
12887 56 : wide_int common = mask & wi::to_wide (arg1);
12888 56 : if (common == mask)
12889 : {
12890 28 : tem_type = signed_type_for (TREE_TYPE (tem));
12891 28 : tem = fold_convert_loc (loc, tem_type, tem);
12892 : }
12893 28 : else if (common == 0)
12894 : {
12895 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
12896 0 : tem = fold_convert_loc (loc, tem_type, tem);
12897 : }
12898 : else
12899 : tem = NULL;
12900 56 : }
12901 :
12902 56 : if (tem)
12903 28 : return
12904 56 : fold_convert_loc (loc, type,
12905 : fold_build2_loc (loc, BIT_AND_EXPR,
12906 28 : TREE_TYPE (tem), tem,
12907 : fold_convert_loc (loc,
12908 28 : TREE_TYPE (tem),
12909 28 : arg1)));
12910 : }
12911 :
12912 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12913 : already handled above. */
12914 9894276 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12915 347 : && integer_onep (TREE_OPERAND (arg0, 1))
12916 3 : && integer_zerop (op2)
12917 9894276 : && integer_pow2p (arg1))
12918 : {
12919 0 : tree tem = TREE_OPERAND (arg0, 0);
12920 0 : STRIP_NOPS (tem);
12921 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
12922 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
12923 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
12924 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
12925 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
12926 : fold_convert_loc (loc, type,
12927 0 : TREE_OPERAND (tem, 0)),
12928 0 : op1);
12929 : }
12930 :
12931 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
12932 : is probably obsolete because the first operand should be a
12933 : truth value (that's why we have the two cases above), but let's
12934 : leave it in until we can confirm this for all front-ends. */
12935 9894276 : if (integer_zerop (op2)
12936 1888085 : && TREE_CODE (arg0) == NE_EXPR
12937 491668 : && integer_zerop (TREE_OPERAND (arg0, 1))
12938 260463 : && integer_pow2p (arg1)
12939 31644 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12940 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12941 : arg1, OEP_ONLY_CONST)
12942 : /* operand_equal_p compares just value, not precision, so e.g.
12943 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
12944 : second operand 32-bit -128, which is not a power of two (or vice
12945 : versa. */
12946 9894276 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
12947 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12948 :
12949 : /* Disable the transformations below for vectors, since
12950 : fold_binary_op_with_conditional_arg may undo them immediately,
12951 : yielding an infinite loop. */
12952 9894276 : if (code == VEC_COND_EXPR)
12953 : return NULL_TREE;
12954 :
12955 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
12956 9510316 : if (integer_zerop (op2)
12957 1581708 : && truth_value_p (TREE_CODE (arg0))
12958 1412451 : && truth_value_p (TREE_CODE (arg1))
12959 9543680 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12960 33364 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
12961 : : TRUTH_ANDIF_EXPR,
12962 33364 : type, fold_convert_loc (loc, type, arg0), op1);
12963 :
12964 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
12965 9476952 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
12966 433503 : && truth_value_p (TREE_CODE (arg0))
12967 292498 : && truth_value_p (TREE_CODE (arg1))
12968 9514122 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12969 : {
12970 37170 : location_t loc0 = expr_location_or (arg0, loc);
12971 : /* Only perform transformation if ARG0 is easily inverted. */
12972 37170 : tem = fold_invert_truthvalue (loc0, arg0);
12973 37170 : if (tem)
12974 36906 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12975 : ? BIT_IOR_EXPR
12976 : : TRUTH_ORIF_EXPR,
12977 : type, fold_convert_loc (loc, type, tem),
12978 36906 : op1);
12979 : }
12980 :
12981 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
12982 9440046 : if (integer_zerop (arg1)
12983 321557 : && truth_value_p (TREE_CODE (arg0))
12984 81469 : && truth_value_p (TREE_CODE (op2))
12985 9440074 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12986 : {
12987 28 : location_t loc0 = expr_location_or (arg0, loc);
12988 : /* Only perform transformation if ARG0 is easily inverted. */
12989 28 : tem = fold_invert_truthvalue (loc0, arg0);
12990 28 : if (tem)
12991 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12992 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
12993 : type, fold_convert_loc (loc, type, tem),
12994 0 : op2);
12995 : }
12996 :
12997 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
12998 9440046 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
12999 365578 : && truth_value_p (TREE_CODE (arg0))
13000 259747 : && truth_value_p (TREE_CODE (op2))
13001 9440232 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13002 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13003 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13004 186 : type, fold_convert_loc (loc, type, arg0), op2);
13005 :
13006 : return NULL_TREE;
13007 :
13008 0 : case CALL_EXPR:
13009 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13010 : of fold_ternary on them. */
13011 0 : gcc_unreachable ();
13012 :
13013 762112 : case BIT_FIELD_REF:
13014 762112 : if (TREE_CODE (arg0) == VECTOR_CST
13015 44937 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13016 15983 : || (VECTOR_TYPE_P (type)
13017 15261 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13018 44195 : && tree_fits_uhwi_p (op1)
13019 806307 : && tree_fits_uhwi_p (op2))
13020 : {
13021 44195 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13022 44195 : unsigned HOST_WIDE_INT width
13023 44195 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13024 44195 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13025 44195 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13026 44195 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13027 :
13028 44195 : if (n != 0
13029 44195 : && (idx % width) == 0
13030 44195 : && (n % width) == 0
13031 88390 : && known_le ((idx + n) / width,
13032 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13033 : {
13034 44195 : idx = idx / width;
13035 44195 : n = n / width;
13036 :
13037 44195 : if (TREE_CODE (arg0) == VECTOR_CST)
13038 : {
13039 44195 : if (n == 1)
13040 : {
13041 28958 : tem = VECTOR_CST_ELT (arg0, idx);
13042 28958 : if (VECTOR_TYPE_P (type))
13043 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13044 28958 : return tem;
13045 : }
13046 :
13047 15237 : tree_vector_builder vals (type, n, 1);
13048 71149 : for (unsigned i = 0; i < n; ++i)
13049 55912 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13050 15237 : return vals.build ();
13051 15237 : }
13052 : }
13053 : }
13054 :
13055 : /* On constants we can use native encode/interpret to constant
13056 : fold (nearly) all BIT_FIELD_REFs. */
13057 717917 : if (CONSTANT_CLASS_P (arg0)
13058 1546 : && can_native_interpret_type_p (type)
13059 : && BITS_PER_UNIT == 8
13060 1546 : && tree_fits_uhwi_p (op1)
13061 719463 : && tree_fits_uhwi_p (op2))
13062 : {
13063 1546 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13064 1546 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13065 : /* Limit us to a reasonable amount of work. To relax the
13066 : other limitations we need bit-shifting of the buffer
13067 : and rounding up the size. */
13068 1546 : if (bitpos % BITS_PER_UNIT == 0
13069 1546 : && bitsize % BITS_PER_UNIT == 0
13070 1546 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13071 : {
13072 1546 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13073 1546 : unsigned HOST_WIDE_INT len
13074 1546 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13075 1546 : bitpos / BITS_PER_UNIT);
13076 1546 : if (len > 0
13077 1546 : && len * BITS_PER_UNIT >= bitsize)
13078 : {
13079 1546 : tree v = native_interpret_expr (type, b,
13080 : bitsize / BITS_PER_UNIT);
13081 1546 : if (v)
13082 1540 : return v;
13083 : }
13084 : }
13085 : }
13086 :
13087 : return NULL_TREE;
13088 :
13089 747614 : case VEC_PERM_EXPR:
13090 : /* Perform constant folding of BIT_INSERT_EXPR. */
13091 747614 : if (TREE_CODE (arg2) == VECTOR_CST
13092 736146 : && TREE_CODE (op0) == VECTOR_CST
13093 15342 : && TREE_CODE (op1) == VECTOR_CST)
13094 : {
13095 : /* Build a vector of integers from the tree mask. */
13096 3951 : vec_perm_builder builder;
13097 3951 : if (!tree_to_vec_perm_builder (&builder, arg2))
13098 : return NULL_TREE;
13099 :
13100 : /* Create a vec_perm_indices for the integer vector. */
13101 3951 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13102 3951 : bool single_arg = (op0 == op1);
13103 7902 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13104 3951 : return fold_vec_perm (type, op0, op1, sel);
13105 7902 : }
13106 : return NULL_TREE;
13107 :
13108 14844 : case BIT_INSERT_EXPR:
13109 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13110 14844 : if (TREE_CODE (arg0) == INTEGER_CST
13111 14 : && TREE_CODE (arg1) == INTEGER_CST)
13112 : {
13113 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13114 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13115 2 : if (BYTES_BIG_ENDIAN)
13116 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13117 2 : wide_int tem = (wi::to_wide (arg0)
13118 4 : & wi::shifted_mask (bitpos, bitsize, true,
13119 4 : TYPE_PRECISION (type)));
13120 2 : wide_int tem2
13121 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13122 2 : bitsize), bitpos);
13123 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13124 2 : }
13125 14842 : else if (TREE_CODE (arg0) == VECTOR_CST
13126 906 : && CONSTANT_CLASS_P (arg1)
13127 15144 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13128 302 : TREE_TYPE (arg1)))
13129 : {
13130 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13131 302 : unsigned HOST_WIDE_INT elsize
13132 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13133 302 : if (bitpos % elsize == 0)
13134 : {
13135 302 : unsigned k = bitpos / elsize;
13136 302 : unsigned HOST_WIDE_INT nelts;
13137 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13138 34934378 : return arg0;
13139 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13140 : {
13141 290 : tree_vector_builder elts (type, nelts, 1);
13142 290 : elts.quick_grow (nelts);
13143 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13144 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13145 290 : return elts.build ();
13146 290 : }
13147 : }
13148 : }
13149 : return NULL_TREE;
13150 :
13151 : default:
13152 : return NULL_TREE;
13153 : } /* switch (code) */
13154 : }
13155 :
13156 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13157 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13158 : constructor element index of the value returned. If the element is
13159 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13160 : the index of the element after the ACCESS_INDEX position (which
13161 : may be outside of the CTOR array). */
13162 :
13163 : tree
13164 712016 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13165 : unsigned *ctor_idx)
13166 : {
13167 712016 : tree index_type = NULL_TREE;
13168 712016 : signop index_sgn = UNSIGNED;
13169 712016 : offset_int low_bound = 0;
13170 :
13171 712016 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13172 : {
13173 712016 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13174 712016 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13175 : {
13176 : /* Static constructors for variably sized objects makes no sense. */
13177 712016 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13178 712016 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13179 : /* ??? When it is obvious that the range is signed, treat it so. */
13180 712016 : if (TYPE_UNSIGNED (index_type)
13181 355301 : && TYPE_MAX_VALUE (domain_type)
13182 1067286 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13183 355270 : TYPE_MIN_VALUE (domain_type)))
13184 : {
13185 0 : index_sgn = SIGNED;
13186 0 : low_bound
13187 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13188 : SIGNED);
13189 : }
13190 : else
13191 : {
13192 712016 : index_sgn = TYPE_SIGN (index_type);
13193 712016 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13194 : }
13195 : }
13196 : }
13197 :
13198 712016 : if (index_type)
13199 712016 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13200 : index_sgn);
13201 :
13202 712016 : offset_int index = low_bound;
13203 712016 : if (index_type)
13204 712016 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13205 :
13206 712016 : offset_int max_index = index;
13207 712016 : unsigned cnt;
13208 712016 : tree cfield, cval;
13209 712016 : bool first_p = true;
13210 :
13211 13720200 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13212 : {
13213 : /* Array constructor might explicitly set index, or specify a range,
13214 : or leave index NULL meaning that it is next index after previous
13215 : one. */
13216 13719075 : if (cfield)
13217 : {
13218 5630621 : if (TREE_CODE (cfield) == INTEGER_CST)
13219 11259792 : max_index = index
13220 5629896 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13221 : else
13222 : {
13223 725 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13224 725 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13225 : index_sgn);
13226 725 : max_index
13227 725 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13228 : index_sgn);
13229 725 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13230 : }
13231 : }
13232 8088454 : else if (!first_p)
13233 : {
13234 7851291 : index = max_index + 1;
13235 7851291 : if (index_type)
13236 7851291 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13237 7851291 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13238 7851291 : max_index = index;
13239 : }
13240 : else
13241 : first_p = false;
13242 :
13243 13719075 : if (TREE_CODE (cval) == RAW_DATA_CST)
13244 2657 : max_index += RAW_DATA_LENGTH (cval) - 1;
13245 :
13246 : /* Do we have match? */
13247 13719075 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13248 : {
13249 13718787 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13250 : {
13251 710775 : if (ctor_idx)
13252 710775 : *ctor_idx = cnt;
13253 710775 : return cval;
13254 : }
13255 : }
13256 288 : else if (in_gimple_form)
13257 : /* We're past the element we search for. Note during parsing
13258 : the elements might not be sorted.
13259 : ??? We should use a binary search and a flag on the
13260 : CONSTRUCTOR as to whether elements are sorted in declaration
13261 : order. */
13262 : break;
13263 : }
13264 1241 : if (ctor_idx)
13265 1241 : *ctor_idx = cnt;
13266 : return NULL_TREE;
13267 : }
13268 :
13269 : /* Perform constant folding and related simplification of EXPR.
13270 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13271 : and application of the associative law.
13272 : NOP_EXPR conversions may be removed freely (as long as we
13273 : are careful not to change the type of the overall expression).
13274 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13275 : but we can constant-fold them if they have constant operands. */
13276 :
13277 : #ifdef ENABLE_FOLD_CHECKING
13278 : # define fold(x) fold_1 (x)
13279 : static tree fold_1 (tree);
13280 : static
13281 : #endif
13282 : tree
13283 1308364193 : fold (tree expr)
13284 : {
13285 1308536943 : const tree t = expr;
13286 1308536943 : enum tree_code code = TREE_CODE (t);
13287 1308536943 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13288 1308536943 : tree tem;
13289 1308536943 : location_t loc = EXPR_LOCATION (expr);
13290 :
13291 : /* Return right away if a constant. */
13292 1308536943 : if (kind == tcc_constant)
13293 : return t;
13294 :
13295 : /* CALL_EXPR-like objects with variable numbers of operands are
13296 : treated specially. */
13297 1206324265 : if (kind == tcc_vl_exp)
13298 : {
13299 172205281 : if (code == CALL_EXPR)
13300 : {
13301 172204777 : tem = fold_call_expr (loc, expr, false);
13302 342016078 : return tem ? tem : expr;
13303 : }
13304 : return expr;
13305 : }
13306 :
13307 1034118984 : if (IS_EXPR_CODE_CLASS (kind))
13308 : {
13309 1032005020 : tree type = TREE_TYPE (t);
13310 1032005020 : tree op0, op1, op2;
13311 :
13312 1032005020 : switch (TREE_CODE_LENGTH (code))
13313 : {
13314 936154013 : case 1:
13315 936154013 : op0 = TREE_OPERAND (t, 0);
13316 936154013 : tem = fold_unary_loc (loc, code, type, op0);
13317 1602382020 : return tem ? tem : expr;
13318 86845190 : case 2:
13319 86845190 : op0 = TREE_OPERAND (t, 0);
13320 86845190 : op1 = TREE_OPERAND (t, 1);
13321 86845190 : tem = fold_binary_loc (loc, code, type, op0, op1);
13322 163445263 : return tem ? tem : expr;
13323 4304893 : case 3:
13324 4304893 : op0 = TREE_OPERAND (t, 0);
13325 4304893 : op1 = TREE_OPERAND (t, 1);
13326 4304893 : op2 = TREE_OPERAND (t, 2);
13327 4304893 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13328 8338209 : return tem ? tem : expr;
13329 : default:
13330 : break;
13331 : }
13332 : }
13333 :
13334 6814888 : switch (code)
13335 : {
13336 4597020 : case ARRAY_REF:
13337 4597020 : {
13338 4597020 : tree op0 = TREE_OPERAND (t, 0);
13339 4597020 : tree op1 = TREE_OPERAND (t, 1);
13340 :
13341 4597020 : if (TREE_CODE (op1) == INTEGER_CST
13342 2903271 : && TREE_CODE (op0) == CONSTRUCTOR
13343 4598475 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13344 : {
13345 1455 : unsigned int idx;
13346 1455 : tree val
13347 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13348 : &idx);
13349 1455 : if (val)
13350 : {
13351 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13352 : return val;
13353 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13354 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13355 : != INTEGER_CST))
13356 : return t;
13357 2 : offset_int o
13358 2 : = (wi::to_offset (op1)
13359 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13360 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13361 2 : return build_int_cst (TREE_TYPE (val),
13362 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13363 : }
13364 : }
13365 :
13366 : return t;
13367 : }
13368 :
13369 : /* Return a VECTOR_CST if possible. */
13370 155205 : case CONSTRUCTOR:
13371 155205 : {
13372 155205 : tree type = TREE_TYPE (t);
13373 155205 : if (TREE_CODE (type) != VECTOR_TYPE)
13374 : return t;
13375 :
13376 : unsigned i;
13377 : tree val;
13378 324645 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13379 282029 : if (! CONSTANT_CLASS_P (val))
13380 : return t;
13381 :
13382 42616 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13383 : }
13384 :
13385 172750 : case CONST_DECL:
13386 172750 : return fold (DECL_INITIAL (t));
13387 :
13388 : default:
13389 : return t;
13390 : } /* switch (code) */
13391 : }
13392 :
13393 : #ifdef ENABLE_FOLD_CHECKING
13394 : #undef fold
13395 :
13396 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13397 : hash_table<nofree_ptr_hash<const tree_node> > *);
13398 : static void fold_check_failed (const_tree, const_tree);
13399 : void print_fold_checksum (const_tree);
13400 :
13401 : /* When --enable-checking=fold, compute a digest of expr before
13402 : and after actual fold call to see if fold did not accidentally
13403 : change original expr. */
13404 :
13405 : tree
13406 : fold (tree expr)
13407 : {
13408 : tree ret;
13409 : struct md5_ctx ctx;
13410 : unsigned char checksum_before[16], checksum_after[16];
13411 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13412 :
13413 : md5_init_ctx (&ctx);
13414 : fold_checksum_tree (expr, &ctx, &ht);
13415 : md5_finish_ctx (&ctx, checksum_before);
13416 : ht.empty ();
13417 :
13418 : ret = fold_1 (expr);
13419 :
13420 : md5_init_ctx (&ctx);
13421 : fold_checksum_tree (expr, &ctx, &ht);
13422 : md5_finish_ctx (&ctx, checksum_after);
13423 :
13424 : if (memcmp (checksum_before, checksum_after, 16))
13425 : fold_check_failed (expr, ret);
13426 :
13427 : return ret;
13428 : }
13429 :
13430 : void
13431 : print_fold_checksum (const_tree expr)
13432 : {
13433 : struct md5_ctx ctx;
13434 : unsigned char checksum[16], cnt;
13435 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13436 :
13437 : md5_init_ctx (&ctx);
13438 : fold_checksum_tree (expr, &ctx, &ht);
13439 : md5_finish_ctx (&ctx, checksum);
13440 : for (cnt = 0; cnt < 16; ++cnt)
13441 : fprintf (stderr, "%02x", checksum[cnt]);
13442 : putc ('\n', stderr);
13443 : }
13444 :
13445 : static void
13446 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13447 : {
13448 : internal_error ("fold check: original tree changed by fold");
13449 : }
13450 :
13451 : static void
13452 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13453 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13454 : {
13455 : const tree_node **slot;
13456 : enum tree_code code;
13457 : union tree_node *buf;
13458 : int i, len;
13459 :
13460 : recursive_label:
13461 : if (expr == NULL)
13462 : return;
13463 : slot = ht->find_slot (expr, INSERT);
13464 : if (*slot != NULL)
13465 : return;
13466 : *slot = expr;
13467 : code = TREE_CODE (expr);
13468 : if (TREE_CODE_CLASS (code) == tcc_declaration
13469 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13470 : {
13471 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13472 : size_t sz = tree_size (expr);
13473 : buf = XALLOCAVAR (union tree_node, sz);
13474 : memcpy ((char *) buf, expr, sz);
13475 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13476 : buf->decl_with_vis.symtab_node = NULL;
13477 : buf->base.nowarning_flag = 0;
13478 : expr = (tree) buf;
13479 : }
13480 : else if (TREE_CODE_CLASS (code) == tcc_type
13481 : && (TYPE_POINTER_TO (expr)
13482 : || TYPE_REFERENCE_TO (expr)
13483 : || TYPE_CACHED_VALUES_P (expr)
13484 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13485 : || TYPE_NEXT_VARIANT (expr)
13486 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13487 : {
13488 : /* Allow these fields to be modified. */
13489 : tree tmp;
13490 : size_t sz = tree_size (expr);
13491 : buf = XALLOCAVAR (union tree_node, sz);
13492 : memcpy ((char *) buf, expr, sz);
13493 : expr = tmp = (tree) buf;
13494 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13495 : TYPE_POINTER_TO (tmp) = NULL;
13496 : TYPE_REFERENCE_TO (tmp) = NULL;
13497 : TYPE_NEXT_VARIANT (tmp) = NULL;
13498 : TYPE_ALIAS_SET (tmp) = -1;
13499 : if (TYPE_CACHED_VALUES_P (tmp))
13500 : {
13501 : TYPE_CACHED_VALUES_P (tmp) = 0;
13502 : TYPE_CACHED_VALUES (tmp) = NULL;
13503 : }
13504 : }
13505 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13506 : {
13507 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13508 : that and change builtins.cc etc. instead - see PR89543. */
13509 : size_t sz = tree_size (expr);
13510 : buf = XALLOCAVAR (union tree_node, sz);
13511 : memcpy ((char *) buf, expr, sz);
13512 : buf->base.nowarning_flag = 0;
13513 : expr = (tree) buf;
13514 : }
13515 : md5_process_bytes (expr, tree_size (expr), ctx);
13516 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13517 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13518 : if (TREE_CODE_CLASS (code) != tcc_type
13519 : && TREE_CODE_CLASS (code) != tcc_declaration
13520 : && code != TREE_LIST
13521 : && code != SSA_NAME
13522 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13523 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13524 : switch (TREE_CODE_CLASS (code))
13525 : {
13526 : case tcc_constant:
13527 : switch (code)
13528 : {
13529 : case STRING_CST:
13530 : md5_process_bytes (TREE_STRING_POINTER (expr),
13531 : TREE_STRING_LENGTH (expr), ctx);
13532 : break;
13533 : case COMPLEX_CST:
13534 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13535 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13536 : break;
13537 : case VECTOR_CST:
13538 : len = vector_cst_encoded_nelts (expr);
13539 : for (i = 0; i < len; ++i)
13540 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13541 : break;
13542 : default:
13543 : break;
13544 : }
13545 : break;
13546 : case tcc_exceptional:
13547 : switch (code)
13548 : {
13549 : case TREE_LIST:
13550 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13551 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13552 : expr = TREE_CHAIN (expr);
13553 : goto recursive_label;
13554 : break;
13555 : case TREE_VEC:
13556 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13557 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13558 : break;
13559 : default:
13560 : break;
13561 : }
13562 : break;
13563 : case tcc_expression:
13564 : case tcc_reference:
13565 : case tcc_comparison:
13566 : case tcc_unary:
13567 : case tcc_binary:
13568 : case tcc_statement:
13569 : case tcc_vl_exp:
13570 : len = TREE_OPERAND_LENGTH (expr);
13571 : for (i = 0; i < len; ++i)
13572 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13573 : break;
13574 : case tcc_declaration:
13575 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13576 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13577 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13578 : {
13579 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13580 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13581 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13582 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13583 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13584 : }
13585 :
13586 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13587 : {
13588 : if (TREE_CODE (expr) == FUNCTION_DECL)
13589 : {
13590 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13591 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13592 : }
13593 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13594 : }
13595 : break;
13596 : case tcc_type:
13597 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13598 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13599 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13600 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13601 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13602 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13603 : if (INTEGRAL_TYPE_P (expr)
13604 : || SCALAR_FLOAT_TYPE_P (expr))
13605 : {
13606 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13607 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13608 : }
13609 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13610 : if (RECORD_OR_UNION_TYPE_P (expr))
13611 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13612 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13613 : break;
13614 : default:
13615 : break;
13616 : }
13617 : }
13618 :
13619 : /* Helper function for outputting the checksum of a tree T. When
13620 : debugging with gdb, you can "define mynext" to be "next" followed
13621 : by "call debug_fold_checksum (op0)", then just trace down till the
13622 : outputs differ. */
13623 :
13624 : DEBUG_FUNCTION void
13625 : debug_fold_checksum (const_tree t)
13626 : {
13627 : int i;
13628 : unsigned char checksum[16];
13629 : struct md5_ctx ctx;
13630 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13631 :
13632 : md5_init_ctx (&ctx);
13633 : fold_checksum_tree (t, &ctx, &ht);
13634 : md5_finish_ctx (&ctx, checksum);
13635 : ht.empty ();
13636 :
13637 : for (i = 0; i < 16; i++)
13638 : fprintf (stderr, "%d ", checksum[i]);
13639 :
13640 : fprintf (stderr, "\n");
13641 : }
13642 :
13643 : #endif
13644 :
13645 : /* Fold a unary tree expression with code CODE of type TYPE with an
13646 : operand OP0. LOC is the location of the resulting expression.
13647 : Return a folded expression if successful. Otherwise, return a tree
13648 : expression with code CODE of type TYPE with an operand OP0. */
13649 :
13650 : tree
13651 866930731 : fold_build1_loc (location_t loc,
13652 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13653 : {
13654 866930731 : tree tem;
13655 : #ifdef ENABLE_FOLD_CHECKING
13656 : unsigned char checksum_before[16], checksum_after[16];
13657 : struct md5_ctx ctx;
13658 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13659 :
13660 : md5_init_ctx (&ctx);
13661 : fold_checksum_tree (op0, &ctx, &ht);
13662 : md5_finish_ctx (&ctx, checksum_before);
13663 : ht.empty ();
13664 : #endif
13665 :
13666 866930731 : tem = fold_unary_loc (loc, code, type, op0);
13667 866930731 : if (!tem)
13668 443578832 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13669 :
13670 : #ifdef ENABLE_FOLD_CHECKING
13671 : md5_init_ctx (&ctx);
13672 : fold_checksum_tree (op0, &ctx, &ht);
13673 : md5_finish_ctx (&ctx, checksum_after);
13674 :
13675 : if (memcmp (checksum_before, checksum_after, 16))
13676 : fold_check_failed (op0, tem);
13677 : #endif
13678 866930731 : return tem;
13679 : }
13680 :
13681 : /* Fold a binary tree expression with code CODE of type TYPE with
13682 : operands OP0 and OP1. LOC is the location of the resulting
13683 : expression. Return a folded expression if successful. Otherwise,
13684 : return a tree expression with code CODE of type TYPE with operands
13685 : OP0 and OP1. */
13686 :
13687 : tree
13688 612980986 : fold_build2_loc (location_t loc,
13689 : enum tree_code code, tree type, tree op0, tree op1
13690 : MEM_STAT_DECL)
13691 : {
13692 612980986 : tree tem;
13693 : #ifdef ENABLE_FOLD_CHECKING
13694 : unsigned char checksum_before_op0[16],
13695 : checksum_before_op1[16],
13696 : checksum_after_op0[16],
13697 : checksum_after_op1[16];
13698 : struct md5_ctx ctx;
13699 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13700 :
13701 : md5_init_ctx (&ctx);
13702 : fold_checksum_tree (op0, &ctx, &ht);
13703 : md5_finish_ctx (&ctx, checksum_before_op0);
13704 : ht.empty ();
13705 :
13706 : md5_init_ctx (&ctx);
13707 : fold_checksum_tree (op1, &ctx, &ht);
13708 : md5_finish_ctx (&ctx, checksum_before_op1);
13709 : ht.empty ();
13710 : #endif
13711 :
13712 612980986 : tem = fold_binary_loc (loc, code, type, op0, op1);
13713 612980986 : if (!tem)
13714 339549804 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13715 :
13716 : #ifdef ENABLE_FOLD_CHECKING
13717 : md5_init_ctx (&ctx);
13718 : fold_checksum_tree (op0, &ctx, &ht);
13719 : md5_finish_ctx (&ctx, checksum_after_op0);
13720 : ht.empty ();
13721 :
13722 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13723 : fold_check_failed (op0, tem);
13724 :
13725 : md5_init_ctx (&ctx);
13726 : fold_checksum_tree (op1, &ctx, &ht);
13727 : md5_finish_ctx (&ctx, checksum_after_op1);
13728 :
13729 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13730 : fold_check_failed (op1, tem);
13731 : #endif
13732 612980986 : return tem;
13733 : }
13734 :
13735 : /* Fold a ternary tree expression with code CODE of type TYPE with
13736 : operands OP0, OP1, and OP2. Return a folded expression if
13737 : successful. Otherwise, return a tree expression with code CODE of
13738 : type TYPE with operands OP0, OP1, and OP2. */
13739 :
13740 : tree
13741 28745300 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13742 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13743 : {
13744 28745300 : tree tem;
13745 : #ifdef ENABLE_FOLD_CHECKING
13746 : unsigned char checksum_before_op0[16],
13747 : checksum_before_op1[16],
13748 : checksum_before_op2[16],
13749 : checksum_after_op0[16],
13750 : checksum_after_op1[16],
13751 : checksum_after_op2[16];
13752 : struct md5_ctx ctx;
13753 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13754 :
13755 : md5_init_ctx (&ctx);
13756 : fold_checksum_tree (op0, &ctx, &ht);
13757 : md5_finish_ctx (&ctx, checksum_before_op0);
13758 : ht.empty ();
13759 :
13760 : md5_init_ctx (&ctx);
13761 : fold_checksum_tree (op1, &ctx, &ht);
13762 : md5_finish_ctx (&ctx, checksum_before_op1);
13763 : ht.empty ();
13764 :
13765 : md5_init_ctx (&ctx);
13766 : fold_checksum_tree (op2, &ctx, &ht);
13767 : md5_finish_ctx (&ctx, checksum_before_op2);
13768 : ht.empty ();
13769 : #endif
13770 :
13771 28745300 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13772 28745300 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13773 28745300 : if (!tem)
13774 25999513 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13775 :
13776 : #ifdef ENABLE_FOLD_CHECKING
13777 : md5_init_ctx (&ctx);
13778 : fold_checksum_tree (op0, &ctx, &ht);
13779 : md5_finish_ctx (&ctx, checksum_after_op0);
13780 : ht.empty ();
13781 :
13782 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13783 : fold_check_failed (op0, tem);
13784 :
13785 : md5_init_ctx (&ctx);
13786 : fold_checksum_tree (op1, &ctx, &ht);
13787 : md5_finish_ctx (&ctx, checksum_after_op1);
13788 : ht.empty ();
13789 :
13790 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13791 : fold_check_failed (op1, tem);
13792 :
13793 : md5_init_ctx (&ctx);
13794 : fold_checksum_tree (op2, &ctx, &ht);
13795 : md5_finish_ctx (&ctx, checksum_after_op2);
13796 :
13797 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13798 : fold_check_failed (op2, tem);
13799 : #endif
13800 28745300 : return tem;
13801 : }
13802 :
13803 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13804 : arguments in ARGARRAY, and a null static chain.
13805 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13806 : of type TYPE from the given operands as constructed by build_call_array. */
13807 :
13808 : tree
13809 54638586 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13810 : int nargs, tree *argarray)
13811 : {
13812 54638586 : tree tem;
13813 : #ifdef ENABLE_FOLD_CHECKING
13814 : unsigned char checksum_before_fn[16],
13815 : checksum_before_arglist[16],
13816 : checksum_after_fn[16],
13817 : checksum_after_arglist[16];
13818 : struct md5_ctx ctx;
13819 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13820 : int i;
13821 :
13822 : md5_init_ctx (&ctx);
13823 : fold_checksum_tree (fn, &ctx, &ht);
13824 : md5_finish_ctx (&ctx, checksum_before_fn);
13825 : ht.empty ();
13826 :
13827 : md5_init_ctx (&ctx);
13828 : for (i = 0; i < nargs; i++)
13829 : fold_checksum_tree (argarray[i], &ctx, &ht);
13830 : md5_finish_ctx (&ctx, checksum_before_arglist);
13831 : ht.empty ();
13832 : #endif
13833 :
13834 54638586 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13835 54638586 : if (!tem)
13836 53320907 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13837 :
13838 : #ifdef ENABLE_FOLD_CHECKING
13839 : md5_init_ctx (&ctx);
13840 : fold_checksum_tree (fn, &ctx, &ht);
13841 : md5_finish_ctx (&ctx, checksum_after_fn);
13842 : ht.empty ();
13843 :
13844 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13845 : fold_check_failed (fn, tem);
13846 :
13847 : md5_init_ctx (&ctx);
13848 : for (i = 0; i < nargs; i++)
13849 : fold_checksum_tree (argarray[i], &ctx, &ht);
13850 : md5_finish_ctx (&ctx, checksum_after_arglist);
13851 :
13852 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13853 : fold_check_failed (NULL_TREE, tem);
13854 : #endif
13855 54638586 : return tem;
13856 : }
13857 :
13858 : /* Perform constant folding and related simplification of initializer
13859 : expression EXPR. These behave identically to "fold_buildN" but ignore
13860 : potential run-time traps and exceptions that fold must preserve. */
13861 :
13862 : #define START_FOLD_INIT \
13863 : int saved_signaling_nans = flag_signaling_nans;\
13864 : int saved_trapping_math = flag_trapping_math;\
13865 : int saved_rounding_math = flag_rounding_math;\
13866 : int saved_trapv = flag_trapv;\
13867 : int saved_folding_initializer = folding_initializer;\
13868 : flag_signaling_nans = 0;\
13869 : flag_trapping_math = 0;\
13870 : flag_rounding_math = 0;\
13871 : flag_trapv = 0;\
13872 : folding_initializer = 1;
13873 :
13874 : #define END_FOLD_INIT \
13875 : flag_signaling_nans = saved_signaling_nans;\
13876 : flag_trapping_math = saved_trapping_math;\
13877 : flag_rounding_math = saved_rounding_math;\
13878 : flag_trapv = saved_trapv;\
13879 : folding_initializer = saved_folding_initializer;
13880 :
13881 : tree
13882 543904 : fold_init (tree expr)
13883 : {
13884 543904 : tree result;
13885 543904 : START_FOLD_INIT;
13886 :
13887 543904 : result = fold (expr);
13888 :
13889 543904 : END_FOLD_INIT;
13890 543904 : return result;
13891 : }
13892 :
13893 : tree
13894 2988608 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
13895 : tree type, tree op)
13896 : {
13897 2988608 : tree result;
13898 2988608 : START_FOLD_INIT;
13899 :
13900 2988608 : result = fold_build1_loc (loc, code, type, op);
13901 :
13902 2988608 : END_FOLD_INIT;
13903 2988608 : return result;
13904 : }
13905 :
13906 : tree
13907 50372 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
13908 : tree type, tree op0, tree op1)
13909 : {
13910 50372 : tree result;
13911 50372 : START_FOLD_INIT;
13912 :
13913 50372 : result = fold_build2_loc (loc, code, type, op0, op1);
13914 :
13915 50372 : END_FOLD_INIT;
13916 50372 : return result;
13917 : }
13918 :
13919 : tree
13920 3460 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13921 : int nargs, tree *argarray)
13922 : {
13923 3460 : tree result;
13924 3460 : START_FOLD_INIT;
13925 :
13926 3460 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13927 :
13928 3460 : END_FOLD_INIT;
13929 3460 : return result;
13930 : }
13931 :
13932 : tree
13933 40492983 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
13934 : tree lhs, tree rhs)
13935 : {
13936 40492983 : tree result;
13937 40492983 : START_FOLD_INIT;
13938 :
13939 40492983 : result = fold_binary_loc (loc, code, type, lhs, rhs);
13940 :
13941 40492983 : END_FOLD_INIT;
13942 40492983 : return result;
13943 : }
13944 :
13945 : #undef START_FOLD_INIT
13946 : #undef END_FOLD_INIT
13947 :
13948 : /* Determine if first argument is a multiple of second argument. Return
13949 : false if it is not, or we cannot easily determined it to be.
13950 :
13951 : An example of the sort of thing we care about (at this point; this routine
13952 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
13953 : fold cases do now) is discovering that
13954 :
13955 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13956 :
13957 : is a multiple of
13958 :
13959 : SAVE_EXPR (J * 8)
13960 :
13961 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13962 :
13963 : This code also handles discovering that
13964 :
13965 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13966 :
13967 : is a multiple of 8 so we don't have to worry about dealing with a
13968 : possible remainder.
13969 :
13970 : Note that we *look* inside a SAVE_EXPR only to determine how it was
13971 : calculated; it is not safe for fold to do much of anything else with the
13972 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13973 : at run time. For example, the latter example above *cannot* be implemented
13974 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13975 : evaluation time of the original SAVE_EXPR is not necessarily the same at
13976 : the time the new expression is evaluated. The only optimization of this
13977 : sort that would be valid is changing
13978 :
13979 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13980 :
13981 : divided by 8 to
13982 :
13983 : SAVE_EXPR (I) * SAVE_EXPR (J)
13984 :
13985 : (where the same SAVE_EXPR (J) is used in the original and the
13986 : transformed version).
13987 :
13988 : NOWRAP specifies whether all outer operations in TYPE should
13989 : be considered not wrapping. Any type conversion within TOP acts
13990 : as a barrier and we will fall back to NOWRAP being false.
13991 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
13992 : as not wrapping even though they are generally using unsigned arithmetic. */
13993 :
13994 : bool
13995 1560553 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
13996 : {
13997 1560553 : gimple *stmt;
13998 1560553 : tree op1, op2;
13999 :
14000 1560553 : if (operand_equal_p (top, bottom, 0))
14001 : return true;
14002 :
14003 1076943 : if (TREE_CODE (type) != INTEGER_TYPE)
14004 : return false;
14005 :
14006 1076924 : switch (TREE_CODE (top))
14007 : {
14008 702 : case BIT_AND_EXPR:
14009 : /* Bitwise and provides a power of two multiple. If the mask is
14010 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14011 702 : if (!integer_pow2p (bottom))
14012 : return false;
14013 702 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14014 702 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14015 :
14016 394435 : case MULT_EXPR:
14017 : /* If the multiplication can wrap we cannot recurse further unless
14018 : the bottom is a power of two which is where wrapping does not
14019 : matter. */
14020 394435 : if (!nowrap
14021 14359 : && !TYPE_OVERFLOW_UNDEFINED (type)
14022 398905 : && !integer_pow2p (bottom))
14023 : return false;
14024 393998 : if (TREE_CODE (bottom) == INTEGER_CST)
14025 : {
14026 392302 : op1 = TREE_OPERAND (top, 0);
14027 392302 : op2 = TREE_OPERAND (top, 1);
14028 392302 : if (TREE_CODE (op1) == INTEGER_CST)
14029 0 : std::swap (op1, op2);
14030 392302 : if (TREE_CODE (op2) == INTEGER_CST)
14031 : {
14032 382288 : if (multiple_of_p (type, op2, bottom, nowrap))
14033 : return true;
14034 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14035 3283 : if (multiple_of_p (type, bottom, op2, nowrap))
14036 : {
14037 1888 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14038 1888 : wi::to_widest (op2));
14039 1888 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14040 : {
14041 1888 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14042 1888 : return multiple_of_p (type, op1, op2, nowrap);
14043 : }
14044 1888 : }
14045 1395 : return multiple_of_p (type, op1, bottom, nowrap);
14046 : }
14047 : }
14048 11710 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14049 11710 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14050 :
14051 403 : case LSHIFT_EXPR:
14052 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14053 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14054 : {
14055 403 : op1 = TREE_OPERAND (top, 1);
14056 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14057 : {
14058 403 : wide_int mul_op
14059 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14060 806 : return multiple_of_p (type,
14061 806 : wide_int_to_tree (type, mul_op), bottom,
14062 : nowrap);
14063 403 : }
14064 : }
14065 : return false;
14066 :
14067 220877 : case MINUS_EXPR:
14068 220877 : case PLUS_EXPR:
14069 : /* If the addition or subtraction can wrap we cannot recurse further
14070 : unless bottom is a power of two which is where wrapping does not
14071 : matter. */
14072 220877 : if (!nowrap
14073 169599 : && !TYPE_OVERFLOW_UNDEFINED (type)
14074 389042 : && !integer_pow2p (bottom))
14075 : return false;
14076 :
14077 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14078 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14079 : but 0xfffffffd is not. */
14080 193712 : op1 = TREE_OPERAND (top, 1);
14081 193712 : if (TREE_CODE (top) == PLUS_EXPR
14082 187531 : && nowrap
14083 45184 : && TYPE_UNSIGNED (type)
14084 238187 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14085 32531 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14086 :
14087 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14088 : precisely, so be conservative here checking if both op0 and op1
14089 : are multiple of bottom. Note we check the second operand first
14090 : since it's usually simpler. */
14091 193712 : return (multiple_of_p (type, op1, bottom, nowrap)
14092 193712 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14093 :
14094 140923 : CASE_CONVERT:
14095 : /* Can't handle conversions from non-integral or wider integral type. */
14096 140923 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14097 140923 : || (TYPE_PRECISION (type)
14098 38055 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14099 : return false;
14100 : /* NOWRAP only extends to operations in the outermost type so
14101 : make sure to strip it off here. */
14102 37800 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14103 75600 : TREE_OPERAND (top, 0), bottom, false);
14104 :
14105 12816 : case SAVE_EXPR:
14106 12816 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14107 :
14108 86 : case COND_EXPR:
14109 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14110 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14111 :
14112 136348 : case INTEGER_CST:
14113 136348 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14114 2694 : return false;
14115 133654 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14116 : SIGNED);
14117 :
14118 61938 : case SSA_NAME:
14119 61938 : if (TREE_CODE (bottom) == INTEGER_CST
14120 58753 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14121 120691 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14122 : {
14123 25828 : enum tree_code code = gimple_assign_rhs_code (stmt);
14124 :
14125 : /* Check for special cases to see if top is defined as multiple
14126 : of bottom:
14127 :
14128 : top = (X & ~(bottom - 1) ; bottom is power of 2
14129 :
14130 : or
14131 :
14132 : Y = X % bottom
14133 : top = X - Y. */
14134 25828 : if (code == BIT_AND_EXPR
14135 296 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14136 296 : && TREE_CODE (op2) == INTEGER_CST
14137 188 : && integer_pow2p (bottom)
14138 26016 : && wi::multiple_of_p (wi::to_widest (op2),
14139 188 : wi::to_widest (bottom), SIGNED))
14140 179 : return true;
14141 :
14142 25649 : op1 = gimple_assign_rhs1 (stmt);
14143 25649 : if (code == MINUS_EXPR
14144 1471 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14145 1471 : && TREE_CODE (op2) == SSA_NAME
14146 1471 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14147 1471 : && gimple_code (stmt) == GIMPLE_ASSIGN
14148 1113 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14149 62 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14150 25711 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14151 : return true;
14152 : }
14153 :
14154 : /* fall through */
14155 :
14156 : default:
14157 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14158 : return multiple_p (wi::to_poly_widest (top),
14159 : wi::to_poly_widest (bottom));
14160 :
14161 : return false;
14162 : }
14163 : }
14164 :
14165 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14166 : This function returns true for integer expressions, and returns
14167 : false if uncertain. */
14168 :
14169 : bool
14170 349301 : tree_expr_finite_p (const_tree x)
14171 : {
14172 349305 : machine_mode mode = element_mode (x);
14173 349305 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14174 : return true;
14175 349111 : switch (TREE_CODE (x))
14176 : {
14177 592 : case REAL_CST:
14178 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14179 0 : case COMPLEX_CST:
14180 0 : return tree_expr_finite_p (TREE_REALPART (x))
14181 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14182 : case FLOAT_EXPR:
14183 : return true;
14184 4 : case ABS_EXPR:
14185 4 : case CONVERT_EXPR:
14186 4 : case NON_LVALUE_EXPR:
14187 4 : case NEGATE_EXPR:
14188 4 : case SAVE_EXPR:
14189 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14190 0 : case MIN_EXPR:
14191 0 : case MAX_EXPR:
14192 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14193 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14194 0 : case COND_EXPR:
14195 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14196 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14197 38 : case CALL_EXPR:
14198 38 : switch (get_call_combined_fn (x))
14199 : {
14200 0 : CASE_CFN_FABS:
14201 0 : CASE_CFN_FABS_FN:
14202 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14203 0 : CASE_CFN_FMAX:
14204 0 : CASE_CFN_FMAX_FN:
14205 0 : CASE_CFN_FMIN:
14206 0 : CASE_CFN_FMIN_FN:
14207 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14208 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14209 : default:
14210 : return false;
14211 : }
14212 :
14213 : default:
14214 : return false;
14215 : }
14216 : }
14217 :
14218 : /* Return true if expression X evaluates to an infinity.
14219 : This function returns false for integer expressions. */
14220 :
14221 : bool
14222 561961 : tree_expr_infinite_p (const_tree x)
14223 : {
14224 562411 : if (!HONOR_INFINITIES (x))
14225 : return false;
14226 562296 : switch (TREE_CODE (x))
14227 : {
14228 0 : case REAL_CST:
14229 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14230 450 : case ABS_EXPR:
14231 450 : case NEGATE_EXPR:
14232 450 : case NON_LVALUE_EXPR:
14233 450 : case SAVE_EXPR:
14234 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14235 0 : case COND_EXPR:
14236 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14237 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14238 : default:
14239 : return false;
14240 : }
14241 : }
14242 :
14243 : /* Return true if expression X could evaluate to an infinity.
14244 : This function returns false for integer expressions, and returns
14245 : true if uncertain. */
14246 :
14247 : bool
14248 324854 : tree_expr_maybe_infinite_p (const_tree x)
14249 : {
14250 324862 : if (!HONOR_INFINITIES (x))
14251 : return false;
14252 324535 : switch (TREE_CODE (x))
14253 : {
14254 177 : case REAL_CST:
14255 177 : return real_isinf (TREE_REAL_CST_PTR (x));
14256 : case FLOAT_EXPR:
14257 : return false;
14258 8 : case ABS_EXPR:
14259 8 : case NEGATE_EXPR:
14260 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14261 1 : case COND_EXPR:
14262 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14263 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14264 : default:
14265 : return true;
14266 : }
14267 : }
14268 :
14269 : /* Return true if expression X evaluates to a signaling NaN.
14270 : This function returns false for integer expressions. */
14271 :
14272 : bool
14273 357 : tree_expr_signaling_nan_p (const_tree x)
14274 : {
14275 357 : if (!HONOR_SNANS (x))
14276 : return false;
14277 124 : switch (TREE_CODE (x))
14278 : {
14279 124 : case REAL_CST:
14280 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14281 0 : case NON_LVALUE_EXPR:
14282 0 : case SAVE_EXPR:
14283 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14284 0 : case COND_EXPR:
14285 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14286 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14287 : default:
14288 : return false;
14289 : }
14290 : }
14291 :
14292 : /* Return true if expression X could evaluate to a signaling NaN.
14293 : This function returns false for integer expressions, and returns
14294 : true if uncertain. */
14295 :
14296 : bool
14297 723332 : tree_expr_maybe_signaling_nan_p (const_tree x)
14298 : {
14299 723332 : if (!HONOR_SNANS (x))
14300 : return false;
14301 5028 : switch (TREE_CODE (x))
14302 : {
14303 1452 : case REAL_CST:
14304 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14305 : case FLOAT_EXPR:
14306 : return false;
14307 0 : case ABS_EXPR:
14308 0 : case CONVERT_EXPR:
14309 0 : case NEGATE_EXPR:
14310 0 : case NON_LVALUE_EXPR:
14311 0 : case SAVE_EXPR:
14312 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14313 0 : case MIN_EXPR:
14314 0 : case MAX_EXPR:
14315 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14316 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14317 0 : case COND_EXPR:
14318 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14319 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14320 0 : case CALL_EXPR:
14321 0 : switch (get_call_combined_fn (x))
14322 : {
14323 0 : CASE_CFN_FABS:
14324 0 : CASE_CFN_FABS_FN:
14325 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14326 0 : CASE_CFN_FMAX:
14327 0 : CASE_CFN_FMAX_FN:
14328 0 : CASE_CFN_FMIN:
14329 0 : CASE_CFN_FMIN_FN:
14330 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14331 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14332 : default:
14333 : return true;
14334 : }
14335 : default:
14336 : return true;
14337 : }
14338 : }
14339 :
14340 : /* Return true if expression X evaluates to a NaN.
14341 : This function returns false for integer expressions. */
14342 :
14343 : bool
14344 3439355 : tree_expr_nan_p (const_tree x)
14345 : {
14346 3771927 : if (!HONOR_NANS (x))
14347 : return false;
14348 3771577 : switch (TREE_CODE (x))
14349 : {
14350 3774 : case REAL_CST:
14351 3774 : return real_isnan (TREE_REAL_CST_PTR (x));
14352 332572 : case NON_LVALUE_EXPR:
14353 332572 : case SAVE_EXPR:
14354 332572 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14355 900 : case COND_EXPR:
14356 900 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14357 900 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14358 : default:
14359 : return false;
14360 : }
14361 : }
14362 :
14363 : /* Return true if expression X could evaluate to a NaN.
14364 : This function returns false for integer expressions, and returns
14365 : true if uncertain. */
14366 :
14367 : bool
14368 4172684 : tree_expr_maybe_nan_p (const_tree x)
14369 : {
14370 5723322 : if (!HONOR_NANS (x))
14371 : return false;
14372 5606319 : switch (TREE_CODE (x))
14373 : {
14374 3302 : case REAL_CST:
14375 3302 : return real_isnan (TREE_REAL_CST_PTR (x));
14376 : case FLOAT_EXPR:
14377 : return false;
14378 13846 : case PLUS_EXPR:
14379 13846 : case MINUS_EXPR:
14380 13846 : case MULT_EXPR:
14381 13846 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14382 13846 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14383 1550638 : case ABS_EXPR:
14384 1550638 : case CONVERT_EXPR:
14385 1550638 : case NEGATE_EXPR:
14386 1550638 : case NON_LVALUE_EXPR:
14387 1550638 : case SAVE_EXPR:
14388 1550638 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14389 176 : case MIN_EXPR:
14390 176 : case MAX_EXPR:
14391 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14392 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14393 557 : case COND_EXPR:
14394 557 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14395 557 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14396 1081 : case CALL_EXPR:
14397 1081 : switch (get_call_combined_fn (x))
14398 : {
14399 0 : CASE_CFN_FABS:
14400 0 : CASE_CFN_FABS_FN:
14401 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14402 108 : CASE_CFN_FMAX:
14403 108 : CASE_CFN_FMAX_FN:
14404 108 : CASE_CFN_FMIN:
14405 108 : CASE_CFN_FMIN_FN:
14406 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14407 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14408 : default:
14409 : return true;
14410 : }
14411 : default:
14412 : return true;
14413 : }
14414 : }
14415 :
14416 : /* Return true if expression X could evaluate to -0.0.
14417 : This function returns true if uncertain. */
14418 :
14419 : bool
14420 600854 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14421 : {
14422 600854 : if (!HONOR_SIGNED_ZEROS (x))
14423 : return false;
14424 600854 : switch (TREE_CODE (x))
14425 : {
14426 0 : case REAL_CST:
14427 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14428 : case INTEGER_CST:
14429 : case FLOAT_EXPR:
14430 : case ABS_EXPR:
14431 : return false;
14432 0 : case NON_LVALUE_EXPR:
14433 0 : case SAVE_EXPR:
14434 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14435 0 : case COND_EXPR:
14436 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14437 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14438 1 : case CALL_EXPR:
14439 1 : switch (get_call_combined_fn (x))
14440 : {
14441 : CASE_CFN_FABS:
14442 : CASE_CFN_FABS_FN:
14443 : return false;
14444 : default:
14445 : break;
14446 : }
14447 : default:
14448 : break;
14449 : }
14450 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14451 : * but currently those predicates require tree and not const_tree. */
14452 : return true;
14453 : }
14454 :
14455 : #define tree_expr_nonnegative_p(X, Y) \
14456 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14457 :
14458 : #define RECURSE(X) \
14459 : ((tree_expr_nonnegative_p) (X, depth + 1))
14460 :
14461 : /* Return true if CODE or TYPE is known to be non-negative. */
14462 :
14463 : static bool
14464 24566022 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14465 : {
14466 24566022 : if (!VECTOR_TYPE_P (type)
14467 24539343 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14468 49105110 : && truth_value_p (code))
14469 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14470 : have a signed:1 type (where the value is -1 and 0). */
14471 : return true;
14472 : return false;
14473 : }
14474 :
14475 : /* Return true if (CODE OP0) is known to be non-negative.
14476 : DEPTH is the current nesting depth of the query. */
14477 :
14478 : bool
14479 2379189 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14480 : {
14481 2379189 : if (TYPE_UNSIGNED (type))
14482 : return true;
14483 :
14484 1950079 : switch (code)
14485 : {
14486 274340 : case ABS_EXPR:
14487 : /* We can't return 1 if flag_wrapv is set because
14488 : ABS_EXPR<INT_MIN> = INT_MIN. */
14489 274340 : if (!ANY_INTEGRAL_TYPE_P (type))
14490 : return true;
14491 10066 : if (TYPE_OVERFLOW_UNDEFINED (type))
14492 : return true;
14493 : break;
14494 :
14495 70476 : case NON_LVALUE_EXPR:
14496 70476 : case FLOAT_EXPR:
14497 70476 : case FIX_TRUNC_EXPR:
14498 70476 : return RECURSE (op0);
14499 :
14500 1573422 : CASE_CONVERT:
14501 1573422 : {
14502 1573422 : tree inner_type = TREE_TYPE (op0);
14503 1573422 : tree outer_type = type;
14504 :
14505 1573422 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14506 : {
14507 368820 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14508 368820 : return RECURSE (op0);
14509 0 : if (INTEGRAL_TYPE_P (inner_type))
14510 : {
14511 0 : if (TYPE_UNSIGNED (inner_type))
14512 : return true;
14513 0 : return RECURSE (op0);
14514 : }
14515 : }
14516 1204602 : else if (INTEGRAL_TYPE_P (outer_type))
14517 : {
14518 1204528 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14519 0 : return RECURSE (op0);
14520 1204528 : if (INTEGRAL_TYPE_P (inner_type))
14521 1198783 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14522 1198783 : && TYPE_UNSIGNED (inner_type);
14523 : }
14524 : }
14525 : break;
14526 :
14527 31841 : default:
14528 31841 : return tree_simple_nonnegative_warnv_p (code, type);
14529 : }
14530 :
14531 : /* We don't know sign of `t', so be conservative and return false. */
14532 : return false;
14533 : }
14534 :
14535 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14536 : DEPTH is the current nesting depth of the query. */
14537 :
14538 : bool
14539 5063847 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14540 : tree op1, int depth)
14541 : {
14542 5063847 : if (TYPE_UNSIGNED (type))
14543 : return true;
14544 :
14545 4808266 : switch (code)
14546 : {
14547 1309808 : case POINTER_PLUS_EXPR:
14548 1309808 : case PLUS_EXPR:
14549 1309808 : if (FLOAT_TYPE_P (type))
14550 45276 : return RECURSE (op0) && RECURSE (op1);
14551 :
14552 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14553 : both unsigned and at least 2 bits shorter than the result. */
14554 1264532 : if (TREE_CODE (type) == INTEGER_TYPE
14555 1258512 : && TREE_CODE (op0) == NOP_EXPR
14556 17003 : && TREE_CODE (op1) == NOP_EXPR)
14557 : {
14558 200 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14559 200 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14560 200 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14561 301 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14562 : {
14563 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14564 95 : TYPE_PRECISION (inner2)) + 1;
14565 95 : return prec < TYPE_PRECISION (type);
14566 : }
14567 : }
14568 : break;
14569 :
14570 174223 : case MULT_EXPR:
14571 174223 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14572 : {
14573 : /* x * x is always non-negative for floating point x
14574 : or without overflow. */
14575 157704 : if (operand_equal_p (op0, op1, 0)
14576 157704 : || (RECURSE (op0) && RECURSE (op1)))
14577 1928 : return true;
14578 : }
14579 :
14580 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14581 : both unsigned and their total bits is shorter than the result. */
14582 172295 : if (TREE_CODE (type) == INTEGER_TYPE
14583 105711 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14584 122 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14585 : {
14586 116 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14587 116 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14588 116 : : TREE_TYPE (op0);
14589 116 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14590 116 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14591 116 : : TREE_TYPE (op1);
14592 :
14593 116 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14594 116 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14595 :
14596 116 : if (TREE_CODE (op0) == INTEGER_CST)
14597 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14598 :
14599 116 : if (TREE_CODE (op1) == INTEGER_CST)
14600 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14601 :
14602 116 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14603 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14604 : {
14605 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14606 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14607 0 : : TYPE_PRECISION (inner0);
14608 :
14609 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14610 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14611 0 : : TYPE_PRECISION (inner1);
14612 :
14613 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14614 : }
14615 : }
14616 : return false;
14617 :
14618 26132 : case BIT_AND_EXPR:
14619 26132 : return RECURSE (op0) || RECURSE (op1);
14620 :
14621 52590 : case MAX_EXPR:
14622 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14623 : things. */
14624 52590 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14625 76 : return RECURSE (op0) && RECURSE (op1);
14626 52514 : return RECURSE (op0) || RECURSE (op1);
14627 :
14628 167549 : case BIT_IOR_EXPR:
14629 167549 : case BIT_XOR_EXPR:
14630 167549 : case MIN_EXPR:
14631 167549 : case RDIV_EXPR:
14632 167549 : case TRUNC_DIV_EXPR:
14633 167549 : case CEIL_DIV_EXPR:
14634 167549 : case FLOOR_DIV_EXPR:
14635 167549 : case ROUND_DIV_EXPR:
14636 167549 : return RECURSE (op0) && RECURSE (op1);
14637 :
14638 85813 : case TRUNC_MOD_EXPR:
14639 85813 : return RECURSE (op0);
14640 :
14641 233 : case FLOOR_MOD_EXPR:
14642 233 : return RECURSE (op1);
14643 :
14644 2991918 : case CEIL_MOD_EXPR:
14645 2991918 : case ROUND_MOD_EXPR:
14646 2991918 : default:
14647 2991918 : return tree_simple_nonnegative_warnv_p (code, type);
14648 : }
14649 :
14650 : /* We don't know sign of `t', so be conservative and return false. */
14651 : return false;
14652 : }
14653 :
14654 : /* Return true if T is known to be non-negative.
14655 : DEPTH is the current nesting depth of the query. */
14656 :
14657 : bool
14658 22964427 : tree_single_nonnegative_p (tree t, int depth)
14659 : {
14660 22964427 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14661 : return true;
14662 :
14663 19704384 : switch (TREE_CODE (t))
14664 : {
14665 2465122 : case INTEGER_CST:
14666 2465122 : return tree_int_cst_sgn (t) >= 0;
14667 :
14668 869081 : case REAL_CST:
14669 869081 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14670 :
14671 0 : case FIXED_CST:
14672 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14673 :
14674 915 : case COND_EXPR:
14675 915 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14676 :
14677 8486856 : case SSA_NAME:
14678 : /* Limit the depth of recursion to avoid quadratic behavior.
14679 : This is expected to catch almost all occurrences in practice.
14680 : If this code misses important cases that unbounded recursion
14681 : would not, passes that need this information could be revised
14682 : to provide it through dataflow propagation. */
14683 8486856 : return (!name_registered_for_update_p (t)
14684 8486855 : && depth < param_max_ssa_name_query_depth
14685 16828310 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14686 :
14687 7882410 : default:
14688 7882410 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14689 : }
14690 : }
14691 :
14692 : /* Return true if T is known to be non-negative.
14693 : DEPTH is the current nesting depth of the query. */
14694 :
14695 : bool
14696 13751721 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14697 : int depth)
14698 : {
14699 13751721 : switch (fn)
14700 : {
14701 : CASE_CFN_ACOS:
14702 : CASE_CFN_ACOS_FN:
14703 : CASE_CFN_ACOSH:
14704 : CASE_CFN_ACOSH_FN:
14705 : CASE_CFN_ACOSPI:
14706 : CASE_CFN_ACOSPI_FN:
14707 : CASE_CFN_CABS:
14708 : CASE_CFN_CABS_FN:
14709 : CASE_CFN_COSH:
14710 : CASE_CFN_COSH_FN:
14711 : CASE_CFN_ERFC:
14712 : CASE_CFN_ERFC_FN:
14713 : CASE_CFN_EXP:
14714 : CASE_CFN_EXP_FN:
14715 : CASE_CFN_EXP10:
14716 : CASE_CFN_EXP2:
14717 : CASE_CFN_EXP2_FN:
14718 : CASE_CFN_FABS:
14719 : CASE_CFN_FABS_FN:
14720 : CASE_CFN_FDIM:
14721 : CASE_CFN_FDIM_FN:
14722 : CASE_CFN_HYPOT:
14723 : CASE_CFN_HYPOT_FN:
14724 : CASE_CFN_POW10:
14725 : CASE_CFN_FFS:
14726 : CASE_CFN_PARITY:
14727 : CASE_CFN_POPCOUNT:
14728 : CASE_CFN_CLRSB:
14729 : case CFN_BUILT_IN_BSWAP16:
14730 : case CFN_BUILT_IN_BSWAP32:
14731 : case CFN_BUILT_IN_BSWAP64:
14732 : case CFN_BUILT_IN_BSWAP128:
14733 : case CFN_BUILT_IN_BITREVERSE8:
14734 : case CFN_BUILT_IN_BITREVERSE16:
14735 : case CFN_BUILT_IN_BITREVERSE32:
14736 : case CFN_BUILT_IN_BITREVERSE64:
14737 : case CFN_BUILT_IN_BITREVERSE128:
14738 : /* Always true. */
14739 : return true;
14740 :
14741 706 : CASE_CFN_CLZ:
14742 706 : CASE_CFN_CTZ:
14743 706 : if (arg1)
14744 0 : return RECURSE (arg1);
14745 : return true;
14746 :
14747 945 : CASE_CFN_SQRT:
14748 945 : CASE_CFN_SQRT_FN:
14749 : /* sqrt(-0.0) is -0.0. */
14750 945 : if (!HONOR_SIGNED_ZEROS (type))
14751 : return true;
14752 913 : return RECURSE (arg0);
14753 :
14754 54439 : CASE_CFN_ASINH:
14755 54439 : CASE_CFN_ASINH_FN:
14756 54439 : CASE_CFN_ASINPI:
14757 54439 : CASE_CFN_ASINPI_FN:
14758 54439 : CASE_CFN_ATAN:
14759 54439 : CASE_CFN_ATAN_FN:
14760 54439 : CASE_CFN_ATANH:
14761 54439 : CASE_CFN_ATANH_FN:
14762 54439 : CASE_CFN_ATANPI:
14763 54439 : CASE_CFN_ATANPI_FN:
14764 54439 : CASE_CFN_CBRT:
14765 54439 : CASE_CFN_CBRT_FN:
14766 54439 : CASE_CFN_CEIL:
14767 54439 : CASE_CFN_CEIL_FN:
14768 54439 : CASE_CFN_ERF:
14769 54439 : CASE_CFN_ERF_FN:
14770 54439 : CASE_CFN_EXPM1:
14771 54439 : CASE_CFN_EXPM1_FN:
14772 54439 : CASE_CFN_FLOOR:
14773 54439 : CASE_CFN_FLOOR_FN:
14774 54439 : CASE_CFN_FMOD:
14775 54439 : CASE_CFN_FMOD_FN:
14776 54439 : CASE_CFN_FREXP:
14777 54439 : CASE_CFN_FREXP_FN:
14778 54439 : CASE_CFN_ICEIL:
14779 54439 : CASE_CFN_IFLOOR:
14780 54439 : CASE_CFN_IRINT:
14781 54439 : CASE_CFN_IROUND:
14782 54439 : CASE_CFN_LCEIL:
14783 54439 : CASE_CFN_LDEXP:
14784 54439 : CASE_CFN_LFLOOR:
14785 54439 : CASE_CFN_LLCEIL:
14786 54439 : CASE_CFN_LLFLOOR:
14787 54439 : CASE_CFN_LLRINT:
14788 54439 : CASE_CFN_LLRINT_FN:
14789 54439 : CASE_CFN_LLROUND:
14790 54439 : CASE_CFN_LLROUND_FN:
14791 54439 : CASE_CFN_LRINT:
14792 54439 : CASE_CFN_LRINT_FN:
14793 54439 : CASE_CFN_LROUND:
14794 54439 : CASE_CFN_LROUND_FN:
14795 54439 : CASE_CFN_MODF:
14796 54439 : CASE_CFN_MODF_FN:
14797 54439 : CASE_CFN_NEARBYINT:
14798 54439 : CASE_CFN_NEARBYINT_FN:
14799 54439 : CASE_CFN_RINT:
14800 54439 : CASE_CFN_RINT_FN:
14801 54439 : CASE_CFN_ROUND:
14802 54439 : CASE_CFN_ROUND_FN:
14803 54439 : CASE_CFN_ROUNDEVEN:
14804 54439 : CASE_CFN_ROUNDEVEN_FN:
14805 54439 : CASE_CFN_SCALB:
14806 54439 : CASE_CFN_SCALBLN:
14807 54439 : CASE_CFN_SCALBLN_FN:
14808 54439 : CASE_CFN_SCALBN:
14809 54439 : CASE_CFN_SCALBN_FN:
14810 54439 : CASE_CFN_SIGNBIT:
14811 54439 : CASE_CFN_SIGNIFICAND:
14812 54439 : CASE_CFN_SINH:
14813 54439 : CASE_CFN_SINH_FN:
14814 54439 : CASE_CFN_TANH:
14815 54439 : CASE_CFN_TANH_FN:
14816 54439 : CASE_CFN_TRUNC:
14817 54439 : CASE_CFN_TRUNC_FN:
14818 : /* True if the 1st argument is nonnegative. */
14819 54439 : return RECURSE (arg0);
14820 :
14821 1319 : CASE_CFN_FMAX:
14822 1319 : CASE_CFN_FMAX_FN:
14823 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14824 : things. In the presence of sNaNs, we're only guaranteed to be
14825 : non-negative if both operands are non-negative. In the presence
14826 : of qNaNs, we're non-negative if either operand is non-negative
14827 : and can't be a qNaN, or if both operands are non-negative. */
14828 1319 : if (tree_expr_maybe_signaling_nan_p (arg0)
14829 1319 : || tree_expr_maybe_signaling_nan_p (arg1))
14830 136 : return RECURSE (arg0) && RECURSE (arg1);
14831 1183 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14832 332 : || RECURSE (arg1))
14833 851 : : (RECURSE (arg1)
14834 851 : && !tree_expr_maybe_nan_p (arg1));
14835 :
14836 910 : CASE_CFN_FMIN:
14837 910 : CASE_CFN_FMIN_FN:
14838 : /* True if the 1st AND 2nd arguments are nonnegative. */
14839 910 : return RECURSE (arg0) && RECURSE (arg1);
14840 :
14841 742 : CASE_CFN_COPYSIGN:
14842 742 : CASE_CFN_COPYSIGN_FN:
14843 : /* True if the 2nd argument is nonnegative. */
14844 742 : return RECURSE (arg1);
14845 :
14846 2336 : CASE_CFN_POWI:
14847 : /* True if the 1st argument is nonnegative or the second
14848 : argument is an even integer. */
14849 2336 : if (TREE_CODE (arg1) == INTEGER_CST
14850 2336 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14851 : return true;
14852 2255 : return RECURSE (arg0);
14853 :
14854 4912 : CASE_CFN_POW:
14855 4912 : CASE_CFN_POW_FN:
14856 : /* True if the 1st argument is nonnegative or the second
14857 : argument is an even integer valued real. */
14858 4912 : if (TREE_CODE (arg1) == REAL_CST)
14859 : {
14860 2208 : REAL_VALUE_TYPE c;
14861 2208 : HOST_WIDE_INT n;
14862 :
14863 2208 : c = TREE_REAL_CST (arg1);
14864 2208 : n = real_to_integer (&c);
14865 2208 : if ((n & 1) == 0)
14866 : {
14867 1579 : REAL_VALUE_TYPE cint;
14868 1579 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14869 1579 : if (real_identical (&c, &cint))
14870 574 : return true;
14871 : }
14872 : }
14873 4338 : return RECURSE (arg0);
14874 :
14875 13657143 : default:
14876 13657143 : break;
14877 : }
14878 13657143 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14879 : }
14880 :
14881 : /* Return true if T is known to be non-negative.
14882 : DEPTH is the current nesting depth of the query. */
14883 :
14884 : static bool
14885 1657260 : tree_invalid_nonnegative_p (tree t, int depth)
14886 : {
14887 1657260 : enum tree_code code = TREE_CODE (t);
14888 1657260 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14889 : return true;
14890 :
14891 1257367 : switch (code)
14892 : {
14893 254 : case TARGET_EXPR:
14894 254 : {
14895 254 : tree temp = TARGET_EXPR_SLOT (t);
14896 254 : t = TARGET_EXPR_INITIAL (t);
14897 :
14898 : /* If the initializer is non-void, then it's a normal expression
14899 : that will be assigned to the slot. */
14900 254 : if (!VOID_TYPE_P (TREE_TYPE (t)))
14901 52 : return RECURSE (t);
14902 :
14903 : /* Otherwise, the initializer sets the slot in some way. One common
14904 : way is an assignment statement at the end of the initializer. */
14905 404 : while (1)
14906 : {
14907 404 : if (TREE_CODE (t) == BIND_EXPR)
14908 202 : t = expr_last (BIND_EXPR_BODY (t));
14909 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14910 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
14911 0 : t = expr_last (TREE_OPERAND (t, 0));
14912 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
14913 0 : t = expr_last (t);
14914 : else
14915 : break;
14916 : }
14917 202 : if (TREE_CODE (t) == MODIFY_EXPR
14918 202 : && TREE_OPERAND (t, 0) == temp)
14919 202 : return RECURSE (TREE_OPERAND (t, 1));
14920 :
14921 : return false;
14922 : }
14923 :
14924 616361 : case CALL_EXPR:
14925 616361 : {
14926 616361 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14927 616361 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14928 :
14929 616361 : return tree_call_nonnegative_p (TREE_TYPE (t),
14930 : get_call_combined_fn (t),
14931 : arg0,
14932 : arg1,
14933 616361 : depth);
14934 : }
14935 3371 : case COMPOUND_EXPR:
14936 3371 : case MODIFY_EXPR:
14937 3371 : return RECURSE (TREE_OPERAND (t, 1));
14938 :
14939 15 : case BIND_EXPR:
14940 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
14941 :
14942 634656 : case SAVE_EXPR:
14943 634656 : return RECURSE (TREE_OPERAND (t, 0));
14944 :
14945 2710 : default:
14946 2710 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14947 : }
14948 : }
14949 :
14950 : #undef RECURSE
14951 : #undef tree_expr_nonnegative_p
14952 :
14953 : /* Return true if T is known to be non-negative.
14954 : DEPTH is the current nesting depth of the query. */
14955 :
14956 : bool
14957 24110694 : tree_expr_nonnegative_p (tree t, int depth)
14958 : {
14959 24110694 : enum tree_code code;
14960 24110694 : if (error_operand_p (t))
14961 : return false;
14962 :
14963 24110693 : code = TREE_CODE (t);
14964 24110693 : switch (TREE_CODE_CLASS (code))
14965 : {
14966 1334680 : case tcc_binary:
14967 1334680 : case tcc_comparison:
14968 1334680 : return tree_binary_nonnegative_p (TREE_CODE (t),
14969 1334680 : TREE_TYPE (t),
14970 1334680 : TREE_OPERAND (t, 0),
14971 1334680 : TREE_OPERAND (t, 1),
14972 1334680 : depth);
14973 :
14974 1877865 : case tcc_unary:
14975 1877865 : return tree_unary_nonnegative_p (TREE_CODE (t),
14976 1877865 : TREE_TYPE (t),
14977 1877865 : TREE_OPERAND (t, 0),
14978 1877865 : depth);
14979 :
14980 11568562 : case tcc_constant:
14981 11568562 : case tcc_declaration:
14982 11568562 : case tcc_reference:
14983 11568562 : return tree_single_nonnegative_p (t, depth);
14984 :
14985 9329586 : default:
14986 9329586 : break;
14987 : }
14988 :
14989 9329586 : switch (code)
14990 : {
14991 7 : case TRUTH_AND_EXPR:
14992 7 : case TRUTH_OR_EXPR:
14993 7 : case TRUTH_XOR_EXPR:
14994 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
14995 7 : TREE_TYPE (t),
14996 7 : TREE_OPERAND (t, 0),
14997 7 : TREE_OPERAND (t, 1),
14998 7 : depth);
14999 72 : case TRUTH_NOT_EXPR:
15000 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15001 72 : TREE_TYPE (t),
15002 72 : TREE_OPERAND (t, 0),
15003 72 : depth);
15004 :
15005 7672247 : case COND_EXPR:
15006 7672247 : case CONSTRUCTOR:
15007 7672247 : case OBJ_TYPE_REF:
15008 7672247 : case ADDR_EXPR:
15009 7672247 : case WITH_SIZE_EXPR:
15010 7672247 : case SSA_NAME:
15011 7672247 : return tree_single_nonnegative_p (t, depth);
15012 :
15013 1657260 : default:
15014 1657260 : return tree_invalid_nonnegative_p (t, depth);
15015 : }
15016 : }
15017 :
15018 :
15019 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15020 : For floating point we further ensure that T is not denormal.
15021 : Similar logic is present in nonzero_address in rtlanal.h. */
15022 :
15023 : bool
15024 1410477 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15025 : {
15026 1410477 : switch (code)
15027 : {
15028 1 : case ABS_EXPR:
15029 1 : return tree_expr_nonzero_p (op0);
15030 :
15031 875503 : case NOP_EXPR:
15032 875503 : {
15033 875503 : tree inner_type = TREE_TYPE (op0);
15034 875503 : tree outer_type = type;
15035 :
15036 875503 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15037 875503 : && tree_expr_nonzero_p (op0));
15038 : }
15039 28113 : break;
15040 :
15041 28113 : case NON_LVALUE_EXPR:
15042 28113 : return tree_expr_nonzero_p (op0);
15043 :
15044 : default:
15045 : break;
15046 : }
15047 :
15048 : return false;
15049 : }
15050 :
15051 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15052 : For floating point we further ensure that T is not denormal.
15053 : Similar logic is present in nonzero_address in rtlanal.h. */
15054 :
15055 : bool
15056 2930534 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15057 : {
15058 2930534 : switch (code)
15059 : {
15060 473055 : case POINTER_PLUS_EXPR:
15061 473055 : case PLUS_EXPR:
15062 473055 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15063 : {
15064 : /* With the presence of negative values it is hard
15065 : to say something. */
15066 105410 : if (!tree_expr_nonnegative_p (op0)
15067 105410 : || !tree_expr_nonnegative_p (op1))
15068 102966 : return false;
15069 : /* One of operands must be positive and the other non-negative. */
15070 2444 : return (tree_expr_nonzero_p (op0)
15071 2444 : || tree_expr_nonzero_p (op1));
15072 : }
15073 : break;
15074 :
15075 19060 : case MULT_EXPR:
15076 19060 : if (TYPE_OVERFLOW_UNDEFINED (type))
15077 : {
15078 535 : if (tree_expr_nonzero_p (op0)
15079 535 : && tree_expr_nonzero_p (op1))
15080 : return true;
15081 : }
15082 : break;
15083 :
15084 : case MIN_EXPR:
15085 : break;
15086 :
15087 44 : case MAX_EXPR:
15088 44 : if (tree_expr_nonzero_p (op0))
15089 : {
15090 :
15091 : /* When both operands are nonzero, then MAX must be too. */
15092 0 : if (tree_expr_nonzero_p (op1))
15093 : return true;
15094 :
15095 : /* MAX where operand 0 is positive is positive. */
15096 0 : return tree_expr_nonnegative_p (op0);
15097 : }
15098 : /* MAX where operand 1 is positive is positive. */
15099 44 : else if (tree_expr_nonzero_p (op1)
15100 44 : && tree_expr_nonnegative_p (op1))
15101 : return true;
15102 : break;
15103 :
15104 263671 : case BIT_IOR_EXPR:
15105 263671 : return (tree_expr_nonzero_p (op1)
15106 263671 : || tree_expr_nonzero_p (op0));
15107 :
15108 : default:
15109 : break;
15110 : }
15111 :
15112 : return false;
15113 : }
15114 :
15115 : /* Return true when T is an address and is known to be nonzero.
15116 : For floating point we further ensure that T is not denormal.
15117 : Similar logic is present in nonzero_address in rtlanal.h. */
15118 :
15119 : bool
15120 149165651 : tree_single_nonzero_p (tree t)
15121 : {
15122 149165651 : switch (TREE_CODE (t))
15123 : {
15124 1136016 : case INTEGER_CST:
15125 1136016 : return !integer_zerop (t);
15126 :
15127 11598007 : case ADDR_EXPR:
15128 11598007 : {
15129 11598007 : tree base = TREE_OPERAND (t, 0);
15130 :
15131 11598007 : if (!DECL_P (base))
15132 5930513 : base = get_base_address (base);
15133 :
15134 11598007 : if (base && TREE_CODE (base) == TARGET_EXPR)
15135 779 : base = TARGET_EXPR_SLOT (base);
15136 :
15137 779 : if (!base)
15138 0 : return false;
15139 :
15140 : /* For objects in symbol table check if we know they are non-zero.
15141 : Don't do anything for variables and functions before symtab is built;
15142 : it is quite possible that they will be declared weak later. */
15143 11598007 : int nonzero_addr = maybe_nonzero_address (base);
15144 11598007 : if (nonzero_addr >= 0)
15145 8870263 : return nonzero_addr;
15146 :
15147 : /* Constants are never weak. */
15148 2727744 : if (CONSTANT_CLASS_P (base))
15149 : return true;
15150 :
15151 : return false;
15152 : }
15153 :
15154 37055 : case COND_EXPR:
15155 37055 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15156 37055 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15157 : return true;
15158 : break;
15159 :
15160 124859174 : case SSA_NAME:
15161 124859174 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15162 : break;
15163 96921901 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15164 :
15165 : default:
15166 : break;
15167 : }
15168 : return false;
15169 : }
15170 :
15171 : #define integer_valued_real_p(X) \
15172 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15173 :
15174 : #define RECURSE(X) \
15175 : ((integer_valued_real_p) (X, depth + 1))
15176 :
15177 : /* Return true if the floating point result of (CODE OP0) has an
15178 : integer value. We also allow +Inf, -Inf and NaN to be considered
15179 : integer values. Return false for signaling NaN.
15180 :
15181 : DEPTH is the current nesting depth of the query. */
15182 :
15183 : bool
15184 15024 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15185 : {
15186 15024 : switch (code)
15187 : {
15188 : case FLOAT_EXPR:
15189 : return true;
15190 :
15191 1403 : case ABS_EXPR:
15192 1403 : return RECURSE (op0);
15193 :
15194 9841 : CASE_CONVERT:
15195 9841 : {
15196 9841 : tree type = TREE_TYPE (op0);
15197 9841 : if (TREE_CODE (type) == INTEGER_TYPE)
15198 : return true;
15199 9841 : if (SCALAR_FLOAT_TYPE_P (type))
15200 9841 : return RECURSE (op0);
15201 : break;
15202 : }
15203 :
15204 : default:
15205 : break;
15206 : }
15207 : return false;
15208 : }
15209 :
15210 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15211 : integer value. We also allow +Inf, -Inf and NaN to be considered
15212 : integer values. Return false for signaling NaN.
15213 :
15214 : DEPTH is the current nesting depth of the query. */
15215 :
15216 : bool
15217 13260 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15218 : {
15219 13260 : switch (code)
15220 : {
15221 7586 : case PLUS_EXPR:
15222 7586 : case MINUS_EXPR:
15223 7586 : case MULT_EXPR:
15224 7586 : case MIN_EXPR:
15225 7586 : case MAX_EXPR:
15226 7586 : return RECURSE (op0) && RECURSE (op1);
15227 :
15228 : default:
15229 : break;
15230 : }
15231 : return false;
15232 : }
15233 :
15234 : /* Return true if the floating point result of calling FNDECL with arguments
15235 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15236 : considered integer values. Return false for signaling NaN. If FNDECL
15237 : takes fewer than 2 arguments, the remaining ARGn are null.
15238 :
15239 : DEPTH is the current nesting depth of the query. */
15240 :
15241 : bool
15242 1089 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15243 : {
15244 1089 : switch (fn)
15245 : {
15246 : CASE_CFN_CEIL:
15247 : CASE_CFN_CEIL_FN:
15248 : CASE_CFN_FLOOR:
15249 : CASE_CFN_FLOOR_FN:
15250 : CASE_CFN_NEARBYINT:
15251 : CASE_CFN_NEARBYINT_FN:
15252 : CASE_CFN_RINT:
15253 : CASE_CFN_RINT_FN:
15254 : CASE_CFN_ROUND:
15255 : CASE_CFN_ROUND_FN:
15256 : CASE_CFN_ROUNDEVEN:
15257 : CASE_CFN_ROUNDEVEN_FN:
15258 : CASE_CFN_TRUNC:
15259 : CASE_CFN_TRUNC_FN:
15260 : return true;
15261 :
15262 336 : CASE_CFN_FMIN:
15263 336 : CASE_CFN_FMIN_FN:
15264 336 : CASE_CFN_FMAX:
15265 336 : CASE_CFN_FMAX_FN:
15266 336 : return RECURSE (arg0) && RECURSE (arg1);
15267 :
15268 : default:
15269 : break;
15270 : }
15271 : return false;
15272 : }
15273 :
15274 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15275 : has an integer value. We also allow +Inf, -Inf and NaN to be
15276 : considered integer values. Return false for signaling NaN.
15277 :
15278 : DEPTH is the current nesting depth of the query. */
15279 :
15280 : bool
15281 127008 : integer_valued_real_single_p (tree t, int depth)
15282 : {
15283 127008 : switch (TREE_CODE (t))
15284 : {
15285 2271 : case REAL_CST:
15286 2271 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15287 :
15288 0 : case COND_EXPR:
15289 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15290 :
15291 89305 : case SSA_NAME:
15292 : /* Limit the depth of recursion to avoid quadratic behavior.
15293 : This is expected to catch almost all occurrences in practice.
15294 : If this code misses important cases that unbounded recursion
15295 : would not, passes that need this information could be revised
15296 : to provide it through dataflow propagation. */
15297 89305 : return (!name_registered_for_update_p (t)
15298 89305 : && depth < param_max_ssa_name_query_depth
15299 177814 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15300 : depth));
15301 :
15302 : default:
15303 : break;
15304 : }
15305 : return false;
15306 : }
15307 :
15308 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15309 : has an integer value. We also allow +Inf, -Inf and NaN to be
15310 : considered integer values. Return false for signaling NaN.
15311 :
15312 : DEPTH is the current nesting depth of the query. */
15313 :
15314 : static bool
15315 0 : integer_valued_real_invalid_p (tree t, int depth)
15316 : {
15317 0 : switch (TREE_CODE (t))
15318 : {
15319 0 : case COMPOUND_EXPR:
15320 0 : case MODIFY_EXPR:
15321 0 : case BIND_EXPR:
15322 0 : return RECURSE (TREE_OPERAND (t, 1));
15323 :
15324 0 : case SAVE_EXPR:
15325 0 : return RECURSE (TREE_OPERAND (t, 0));
15326 :
15327 : default:
15328 : break;
15329 : }
15330 : return false;
15331 : }
15332 :
15333 : #undef RECURSE
15334 : #undef integer_valued_real_p
15335 :
15336 : /* Return true if the floating point expression T has an integer value.
15337 : We also allow +Inf, -Inf and NaN to be considered integer values.
15338 : Return false for signaling NaN.
15339 :
15340 : DEPTH is the current nesting depth of the query. */
15341 :
15342 : bool
15343 96250 : integer_valued_real_p (tree t, int depth)
15344 : {
15345 96250 : if (t == error_mark_node)
15346 : return false;
15347 :
15348 96250 : STRIP_ANY_LOCATION_WRAPPER (t);
15349 :
15350 96250 : tree_code code = TREE_CODE (t);
15351 96250 : switch (TREE_CODE_CLASS (code))
15352 : {
15353 0 : case tcc_binary:
15354 0 : case tcc_comparison:
15355 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15356 0 : TREE_OPERAND (t, 1), depth);
15357 :
15358 0 : case tcc_unary:
15359 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15360 :
15361 8446 : case tcc_constant:
15362 8446 : case tcc_declaration:
15363 8446 : case tcc_reference:
15364 8446 : return integer_valued_real_single_p (t, depth);
15365 :
15366 87804 : default:
15367 87804 : break;
15368 : }
15369 :
15370 87804 : switch (code)
15371 : {
15372 87804 : case COND_EXPR:
15373 87804 : case SSA_NAME:
15374 87804 : return integer_valued_real_single_p (t, depth);
15375 :
15376 0 : case CALL_EXPR:
15377 0 : {
15378 0 : tree arg0 = (call_expr_nargs (t) > 0
15379 0 : ? CALL_EXPR_ARG (t, 0)
15380 0 : : NULL_TREE);
15381 0 : tree arg1 = (call_expr_nargs (t) > 1
15382 0 : ? CALL_EXPR_ARG (t, 1)
15383 0 : : NULL_TREE);
15384 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15385 0 : arg0, arg1, depth);
15386 : }
15387 :
15388 0 : default:
15389 0 : return integer_valued_real_invalid_p (t, depth);
15390 : }
15391 : }
15392 :
15393 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15394 : attempt to fold the expression to a constant without modifying TYPE,
15395 : OP0 or OP1.
15396 :
15397 : If the expression could be simplified to a constant, then return
15398 : the constant. If the expression would not be simplified to a
15399 : constant, then return NULL_TREE. */
15400 :
15401 : tree
15402 15624873 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15403 : {
15404 15624873 : tree tem = fold_binary (code, type, op0, op1);
15405 15624873 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15406 : }
15407 :
15408 : /* Given the components of a unary expression CODE, TYPE and OP0,
15409 : attempt to fold the expression to a constant without modifying
15410 : TYPE or OP0.
15411 :
15412 : If the expression could be simplified to a constant, then return
15413 : the constant. If the expression would not be simplified to a
15414 : constant, then return NULL_TREE. */
15415 :
15416 : tree
15417 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15418 : {
15419 0 : tree tem = fold_unary (code, type, op0);
15420 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15421 : }
15422 :
15423 : /* If EXP represents referencing an element in a constant string
15424 : (either via pointer arithmetic or array indexing), return the
15425 : tree representing the value accessed, otherwise return NULL. */
15426 :
15427 : tree
15428 183495268 : fold_read_from_constant_string (tree exp)
15429 : {
15430 183495268 : if ((INDIRECT_REF_P (exp)
15431 183495249 : || TREE_CODE (exp) == ARRAY_REF)
15432 195489815 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15433 : {
15434 8803996 : tree exp1 = TREE_OPERAND (exp, 0);
15435 8803996 : tree index;
15436 8803996 : tree string;
15437 8803996 : location_t loc = EXPR_LOCATION (exp);
15438 :
15439 8803996 : if (INDIRECT_REF_P (exp))
15440 0 : string = string_constant (exp1, &index, NULL, NULL);
15441 : else
15442 : {
15443 8803996 : tree low_bound = array_ref_low_bound (exp);
15444 8803996 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15445 :
15446 : /* Optimize the special-case of a zero lower bound.
15447 :
15448 : We convert the low_bound to sizetype to avoid some problems
15449 : with constant folding. (E.g. suppose the lower bound is 1,
15450 : and its mode is QI. Without the conversion,l (ARRAY
15451 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15452 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15453 8803996 : if (! integer_zerop (low_bound))
15454 153119 : index = size_diffop_loc (loc, index,
15455 : fold_convert_loc (loc, sizetype, low_bound));
15456 :
15457 : string = exp1;
15458 : }
15459 :
15460 8803996 : scalar_int_mode char_mode;
15461 8803996 : if (string
15462 8803996 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15463 8803996 : && TREE_CODE (string) == STRING_CST
15464 154103 : && tree_fits_uhwi_p (index)
15465 150189 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15466 8954004 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15467 : &char_mode)
15468 17607992 : && GET_MODE_SIZE (char_mode) == 1)
15469 297480 : return build_int_cst_type (TREE_TYPE (exp),
15470 148740 : (TREE_STRING_POINTER (string)
15471 148740 : [TREE_INT_CST_LOW (index)]));
15472 : }
15473 : return NULL;
15474 : }
15475 :
15476 : /* Folds a read from vector element at IDX of vector ARG. */
15477 :
15478 : tree
15479 6075 : fold_read_from_vector (tree arg, poly_uint64 idx)
15480 : {
15481 6075 : unsigned HOST_WIDE_INT i;
15482 6075 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15483 6075 : && known_ge (idx, 0u)
15484 6075 : && idx.is_constant (&i))
15485 : {
15486 6075 : if (TREE_CODE (arg) == VECTOR_CST)
15487 1963 : return VECTOR_CST_ELT (arg, i);
15488 4112 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15489 : {
15490 2042 : if (CONSTRUCTOR_NELTS (arg)
15491 2002 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15492 : return NULL_TREE;
15493 1706 : if (i >= CONSTRUCTOR_NELTS (arg))
15494 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15495 1666 : return CONSTRUCTOR_ELT (arg, i)->value;
15496 : }
15497 : }
15498 : return NULL_TREE;
15499 : }
15500 :
15501 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15502 : an integer constant, real, or fixed-point constant.
15503 :
15504 : TYPE is the type of the result. */
15505 :
15506 : static tree
15507 31987381 : fold_negate_const (tree arg0, tree type)
15508 : {
15509 31987381 : tree t = NULL_TREE;
15510 :
15511 31987381 : switch (TREE_CODE (arg0))
15512 : {
15513 2007125 : case REAL_CST:
15514 2007125 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15515 2007125 : break;
15516 :
15517 0 : case FIXED_CST:
15518 0 : {
15519 0 : FIXED_VALUE_TYPE f;
15520 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15521 0 : &(TREE_FIXED_CST (arg0)), NULL,
15522 0 : TYPE_SATURATING (type));
15523 0 : t = build_fixed (type, f);
15524 : /* Propagate overflow flags. */
15525 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15526 0 : TREE_OVERFLOW (t) = 1;
15527 0 : break;
15528 : }
15529 :
15530 29980256 : default:
15531 29980256 : if (poly_int_tree_p (arg0))
15532 : {
15533 29980256 : wi::overflow_type overflow;
15534 29980256 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15535 29980256 : t = force_fit_type (type, res, 1,
15536 229641 : (overflow && ! TYPE_UNSIGNED (type))
15537 30199754 : || TREE_OVERFLOW (arg0));
15538 29980256 : break;
15539 29980256 : }
15540 :
15541 0 : gcc_unreachable ();
15542 : }
15543 :
15544 31987381 : return t;
15545 : }
15546 :
15547 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15548 : an integer constant or real constant.
15549 :
15550 : TYPE is the type of the result. */
15551 :
15552 : tree
15553 34797 : fold_abs_const (tree arg0, tree type)
15554 : {
15555 34797 : tree t = NULL_TREE;
15556 :
15557 34797 : switch (TREE_CODE (arg0))
15558 : {
15559 7187 : case INTEGER_CST:
15560 7187 : {
15561 : /* If the value is unsigned or non-negative, then the absolute value
15562 : is the same as the ordinary value. */
15563 7187 : wide_int val = wi::to_wide (arg0);
15564 7187 : wi::overflow_type overflow = wi::OVF_NONE;
15565 7187 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15566 : ;
15567 :
15568 : /* If the value is negative, then the absolute value is
15569 : its negation. */
15570 : else
15571 3082 : val = wi::neg (val, &overflow);
15572 :
15573 : /* Force to the destination type, set TREE_OVERFLOW for signed
15574 : TYPE only. */
15575 7187 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15576 7187 : }
15577 7187 : break;
15578 :
15579 27610 : case REAL_CST:
15580 27610 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15581 7577 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15582 : else
15583 : t = arg0;
15584 : break;
15585 :
15586 0 : default:
15587 0 : gcc_unreachable ();
15588 : }
15589 :
15590 34797 : return t;
15591 : }
15592 :
15593 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15594 : constant. TYPE is the type of the result. */
15595 :
15596 : static tree
15597 2363566 : fold_not_const (const_tree arg0, tree type)
15598 : {
15599 2363566 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15600 :
15601 2363566 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15602 : }
15603 :
15604 : /* Given CODE, a relational operator, the target type, TYPE and two
15605 : constant operands OP0 and OP1, return the result of the
15606 : relational operation. If the result is not a compile time
15607 : constant, then return NULL_TREE. */
15608 :
15609 : static tree
15610 67082599 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15611 : {
15612 67082599 : int result, invert;
15613 :
15614 : /* From here on, the only cases we handle are when the result is
15615 : known to be a constant. */
15616 :
15617 67082599 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15618 : {
15619 1172708 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15620 1172708 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15621 :
15622 : /* Handle the cases where either operand is a NaN. */
15623 1172708 : if (real_isnan (c0) || real_isnan (c1))
15624 : {
15625 13419 : switch (code)
15626 : {
15627 : case EQ_EXPR:
15628 : case ORDERED_EXPR:
15629 : result = 0;
15630 : break;
15631 :
15632 : case NE_EXPR:
15633 : case UNORDERED_EXPR:
15634 : case UNLT_EXPR:
15635 : case UNLE_EXPR:
15636 : case UNGT_EXPR:
15637 : case UNGE_EXPR:
15638 : case UNEQ_EXPR:
15639 6527 : result = 1;
15640 : break;
15641 :
15642 6911 : case LT_EXPR:
15643 6911 : case LE_EXPR:
15644 6911 : case GT_EXPR:
15645 6911 : case GE_EXPR:
15646 6911 : case LTGT_EXPR:
15647 6911 : if (flag_trapping_math)
15648 : return NULL_TREE;
15649 : result = 0;
15650 : break;
15651 :
15652 0 : default:
15653 0 : gcc_unreachable ();
15654 : }
15655 :
15656 6527 : return constant_boolean_node (result, type);
15657 : }
15658 :
15659 1159289 : return constant_boolean_node (real_compare (code, c0, c1), type);
15660 : }
15661 :
15662 65909891 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15663 : {
15664 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15665 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15666 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15667 : }
15668 :
15669 : /* Handle equality/inequality of complex constants. */
15670 65909891 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15671 : {
15672 58578 : tree rcond = fold_relational_const (code, type,
15673 29289 : TREE_REALPART (op0),
15674 29289 : TREE_REALPART (op1));
15675 117156 : tree icond = fold_relational_const (code, type,
15676 29289 : TREE_IMAGPART (op0),
15677 29289 : TREE_IMAGPART (op1));
15678 29289 : if (code == EQ_EXPR)
15679 307 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15680 28982 : else if (code == NE_EXPR)
15681 28982 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15682 : else
15683 : return NULL_TREE;
15684 : }
15685 :
15686 65880602 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15687 : {
15688 14137 : if (!VECTOR_TYPE_P (type))
15689 : {
15690 : /* Have vector comparison with scalar boolean result. */
15691 172 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15692 : && known_eq (VECTOR_CST_NELTS (op0),
15693 : VECTOR_CST_NELTS (op1)));
15694 172 : unsigned HOST_WIDE_INT nunits;
15695 172 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15696 : return NULL_TREE;
15697 479 : for (unsigned i = 0; i < nunits; i++)
15698 : {
15699 420 : tree elem0 = VECTOR_CST_ELT (op0, i);
15700 420 : tree elem1 = VECTOR_CST_ELT (op1, i);
15701 420 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15702 420 : if (tmp == NULL_TREE)
15703 : return NULL_TREE;
15704 420 : if (integer_zerop (tmp))
15705 113 : return constant_boolean_node (code == NE_EXPR, type);
15706 : }
15707 59 : return constant_boolean_node (code == EQ_EXPR, type);
15708 : }
15709 13965 : tree_vector_builder elts;
15710 13965 : if (!elts.new_binary_operation (type, op0, op1, false))
15711 : return NULL_TREE;
15712 13965 : unsigned int count = elts.encoded_nelts ();
15713 59227 : for (unsigned i = 0; i < count; i++)
15714 : {
15715 45262 : tree elem_type = TREE_TYPE (type);
15716 45262 : tree elem0 = VECTOR_CST_ELT (op0, i);
15717 45262 : tree elem1 = VECTOR_CST_ELT (op1, i);
15718 :
15719 45262 : tree tem = fold_relational_const (code, elem_type,
15720 : elem0, elem1);
15721 :
15722 45262 : if (tem == NULL_TREE)
15723 : return NULL_TREE;
15724 :
15725 45262 : elts.quick_push (build_int_cst (elem_type,
15726 69974 : integer_zerop (tem) ? 0 : -1));
15727 : }
15728 :
15729 13965 : return elts.build ();
15730 13965 : }
15731 :
15732 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15733 :
15734 : To compute GT, swap the arguments and do LT.
15735 : To compute GE, do LT and invert the result.
15736 : To compute LE, swap the arguments, do LT and invert the result.
15737 : To compute NE, do EQ and invert the result.
15738 :
15739 : Therefore, the code below must handle only EQ and LT. */
15740 :
15741 65866465 : if (code == LE_EXPR || code == GT_EXPR)
15742 : {
15743 12512106 : std::swap (op0, op1);
15744 12512106 : code = swap_tree_comparison (code);
15745 : }
15746 :
15747 : /* Note that it is safe to invert for real values here because we
15748 : have already handled the one case that it matters. */
15749 :
15750 65866465 : invert = 0;
15751 65866465 : if (code == NE_EXPR || code == GE_EXPR)
15752 : {
15753 31017402 : invert = 1;
15754 31017402 : code = invert_tree_comparison (code, false);
15755 : }
15756 :
15757 : /* Compute a result for LT or EQ if args permit;
15758 : Otherwise return T. */
15759 65866465 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15760 : {
15761 65841110 : if (code == EQ_EXPR)
15762 32149329 : result = tree_int_cst_equal (op0, op1);
15763 : else
15764 33691781 : result = tree_int_cst_lt (op0, op1);
15765 : }
15766 : else
15767 : return NULL_TREE;
15768 :
15769 65841110 : if (invert)
15770 31015571 : result ^= 1;
15771 65841110 : return constant_boolean_node (result, type);
15772 : }
15773 :
15774 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15775 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15776 : itself. */
15777 :
15778 : tree
15779 128340321 : fold_build_cleanup_point_expr (tree type, tree expr)
15780 : {
15781 : /* If the expression does not have side effects then we don't have to wrap
15782 : it with a cleanup point expression. */
15783 128340321 : if (!TREE_SIDE_EFFECTS (expr))
15784 : return expr;
15785 :
15786 : /* If the expression is a return, check to see if the expression inside the
15787 : return has no side effects or the right hand side of the modify expression
15788 : inside the return. If either don't have side effects set we don't need to
15789 : wrap the expression in a cleanup point expression. Note we don't check the
15790 : left hand side of the modify because it should always be a return decl. */
15791 110160314 : if (TREE_CODE (expr) == RETURN_EXPR)
15792 : {
15793 43419692 : tree op = TREE_OPERAND (expr, 0);
15794 43419692 : if (!op || !TREE_SIDE_EFFECTS (op))
15795 : return expr;
15796 42784111 : op = TREE_OPERAND (op, 1);
15797 42784111 : if (!TREE_SIDE_EFFECTS (op))
15798 : return expr;
15799 : }
15800 :
15801 87054619 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15802 : }
15803 :
15804 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15805 : of an indirection through OP0, or NULL_TREE if no simplification is
15806 : possible. */
15807 :
15808 : tree
15809 21238871 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15810 : {
15811 21238871 : tree sub = op0;
15812 21238871 : tree subtype;
15813 21238871 : poly_uint64 const_op01;
15814 :
15815 21238871 : STRIP_NOPS (sub);
15816 21238871 : subtype = TREE_TYPE (sub);
15817 21238871 : if (!POINTER_TYPE_P (subtype)
15818 21238871 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15819 : return NULL_TREE;
15820 :
15821 21090026 : if (TREE_CODE (sub) == ADDR_EXPR)
15822 : {
15823 4624125 : tree op = TREE_OPERAND (sub, 0);
15824 4624125 : tree optype = TREE_TYPE (op);
15825 :
15826 : /* *&CONST_DECL -> to the value of the const decl. */
15827 4624125 : if (TREE_CODE (op) == CONST_DECL)
15828 3106 : return DECL_INITIAL (op);
15829 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15830 4621019 : if (type == optype)
15831 : {
15832 3471305 : tree fop = fold_read_from_constant_string (op);
15833 3471305 : if (fop)
15834 : return fop;
15835 : else
15836 3426158 : return op;
15837 : }
15838 : /* *(foo *)&fooarray => fooarray[0] */
15839 1149714 : else if (TREE_CODE (optype) == ARRAY_TYPE
15840 13661 : && type == TREE_TYPE (optype)
15841 1162230 : && (!in_gimple_form
15842 2942 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15843 : {
15844 12516 : tree type_domain = TYPE_DOMAIN (optype);
15845 12516 : tree min_val = size_zero_node;
15846 12516 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15847 12477 : min_val = TYPE_MIN_VALUE (type_domain);
15848 12516 : if (in_gimple_form
15849 2942 : && TREE_CODE (min_val) != INTEGER_CST)
15850 : return NULL_TREE;
15851 12516 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15852 12516 : NULL_TREE, NULL_TREE);
15853 : }
15854 : /* *(foo *)&complexfoo => __real__ complexfoo */
15855 1137198 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15856 1137198 : && type == TREE_TYPE (optype))
15857 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15858 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15859 1137198 : else if (VECTOR_TYPE_P (optype)
15860 1137198 : && type == TREE_TYPE (optype))
15861 : {
15862 70 : tree part_width = TYPE_SIZE (type);
15863 70 : tree index = bitsize_int (0);
15864 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15865 70 : index);
15866 : }
15867 : }
15868 :
15869 17603029 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15870 17603029 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15871 : {
15872 260175 : tree op00 = TREE_OPERAND (sub, 0);
15873 260175 : tree op01 = TREE_OPERAND (sub, 1);
15874 :
15875 260175 : STRIP_NOPS (op00);
15876 260175 : if (TREE_CODE (op00) == ADDR_EXPR)
15877 : {
15878 2022 : tree op00type;
15879 2022 : op00 = TREE_OPERAND (op00, 0);
15880 2022 : op00type = TREE_TYPE (op00);
15881 :
15882 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15883 2022 : if (VECTOR_TYPE_P (op00type)
15884 240 : && type == TREE_TYPE (op00type)
15885 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15886 : but we want to treat offsets with MSB set as negative.
15887 : For the code below negative offsets are invalid and
15888 : TYPE_SIZE of the element is something unsigned, so
15889 : check whether op01 fits into poly_int64, which implies
15890 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15891 : then just use poly_uint64 because we want to treat the
15892 : value as unsigned. */
15893 2215 : && tree_fits_poly_int64_p (op01))
15894 : {
15895 179 : tree part_width = TYPE_SIZE (type);
15896 179 : poly_uint64 max_offset
15897 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
15898 179 : * TYPE_VECTOR_SUBPARTS (op00type));
15899 179 : if (known_lt (const_op01, max_offset))
15900 : {
15901 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
15902 179 : return fold_build3_loc (loc,
15903 : BIT_FIELD_REF, type, op00,
15904 179 : part_width, index);
15905 : }
15906 : }
15907 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15908 1843 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
15909 1843 : && type == TREE_TYPE (op00type))
15910 : {
15911 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
15912 : const_op01))
15913 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15914 : }
15915 : /* ((foo *)&fooarray)[1] => fooarray[1] */
15916 1843 : else if (TREE_CODE (op00type) == ARRAY_TYPE
15917 1843 : && type == TREE_TYPE (op00type))
15918 : {
15919 722 : tree type_domain = TYPE_DOMAIN (op00type);
15920 722 : tree min_val = size_zero_node;
15921 722 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15922 721 : min_val = TYPE_MIN_VALUE (type_domain);
15923 722 : poly_uint64 type_size, index;
15924 722 : if (poly_int_tree_p (min_val)
15925 722 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
15926 722 : && multiple_p (const_op01, type_size, &index))
15927 : {
15928 722 : poly_offset_int off = index + wi::to_poly_offset (min_val);
15929 722 : op01 = wide_int_to_tree (sizetype, off);
15930 722 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
15931 : NULL_TREE, NULL_TREE);
15932 : }
15933 : }
15934 : }
15935 : }
15936 :
15937 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15938 17602128 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15939 675055 : && type == TREE_TYPE (TREE_TYPE (subtype))
15940 17605181 : && (!in_gimple_form
15941 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15942 : {
15943 3052 : tree type_domain;
15944 3052 : tree min_val = size_zero_node;
15945 3052 : sub = build_fold_indirect_ref_loc (loc, sub);
15946 3052 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15947 3052 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15948 3052 : min_val = TYPE_MIN_VALUE (type_domain);
15949 3052 : if (in_gimple_form
15950 11 : && TREE_CODE (min_val) != INTEGER_CST)
15951 : return NULL_TREE;
15952 3052 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15953 3052 : NULL_TREE);
15954 : }
15955 :
15956 : return NULL_TREE;
15957 : }
15958 :
15959 : /* Builds an expression for an indirection through T, simplifying some
15960 : cases. */
15961 :
15962 : tree
15963 10797974 : build_fold_indirect_ref_loc (location_t loc, tree t)
15964 : {
15965 10797974 : tree type = TREE_TYPE (TREE_TYPE (t));
15966 10797974 : tree sub = fold_indirect_ref_1 (loc, type, t);
15967 :
15968 10797974 : if (sub)
15969 : return sub;
15970 :
15971 7329850 : return build1_loc (loc, INDIRECT_REF, type, t);
15972 : }
15973 :
15974 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
15975 :
15976 : tree
15977 10080024 : fold_indirect_ref_loc (location_t loc, tree t)
15978 : {
15979 10080024 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15980 :
15981 10080024 : if (sub)
15982 : return sub;
15983 : else
15984 10059619 : return t;
15985 : }
15986 :
15987 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
15988 : whose result is ignored. The type of the returned tree need not be
15989 : the same as the original expression. */
15990 :
15991 : tree
15992 133847 : fold_ignored_result (tree t)
15993 : {
15994 133847 : if (!TREE_SIDE_EFFECTS (t))
15995 17273 : return integer_zero_node;
15996 :
15997 155685 : for (;;)
15998 155685 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
15999 : {
16000 3838 : case tcc_unary:
16001 3838 : t = TREE_OPERAND (t, 0);
16002 3838 : break;
16003 :
16004 5031 : case tcc_binary:
16005 5031 : case tcc_comparison:
16006 5031 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16007 3129 : t = TREE_OPERAND (t, 0);
16008 1902 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16009 30 : t = TREE_OPERAND (t, 1);
16010 : else
16011 : return t;
16012 : break;
16013 :
16014 99767 : case tcc_expression:
16015 99767 : switch (TREE_CODE (t))
16016 : {
16017 32115 : case COMPOUND_EXPR:
16018 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16019 : return t;
16020 31820 : t = TREE_OPERAND (t, 0);
16021 31820 : break;
16022 :
16023 381 : case COND_EXPR:
16024 381 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16025 381 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16026 : return t;
16027 294 : t = TREE_OPERAND (t, 0);
16028 294 : break;
16029 :
16030 : default:
16031 : return t;
16032 : }
16033 : break;
16034 :
16035 : default:
16036 : return t;
16037 : }
16038 : }
16039 :
16040 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16041 :
16042 : tree
16043 3004572635 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16044 : {
16045 3004572635 : tree div = NULL_TREE;
16046 :
16047 3004572635 : if (divisor == 1)
16048 : return value;
16049 :
16050 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16051 : have to do anything. Only do this when we are not given a const,
16052 : because in that case, this check is more expensive than just
16053 : doing it. */
16054 1870548782 : if (TREE_CODE (value) != INTEGER_CST)
16055 : {
16056 367533 : div = build_int_cst (TREE_TYPE (value), divisor);
16057 :
16058 367533 : if (multiple_of_p (TREE_TYPE (value), value, div))
16059 : return value;
16060 : }
16061 :
16062 : /* If divisor is a power of two, simplify this to bit manipulation. */
16063 1870183149 : if (pow2_or_zerop (divisor))
16064 : {
16065 1870183149 : if (TREE_CODE (value) == INTEGER_CST)
16066 : {
16067 1870181249 : wide_int val = wi::to_wide (value);
16068 1870181249 : bool overflow_p;
16069 :
16070 1870181249 : if ((val & (divisor - 1)) == 0)
16071 : return value;
16072 :
16073 3932370 : overflow_p = TREE_OVERFLOW (value);
16074 3932370 : val += divisor - 1;
16075 3932370 : val &= (int) -divisor;
16076 3932370 : if (val == 0)
16077 4 : overflow_p = true;
16078 :
16079 3932370 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16080 1870181249 : }
16081 : else
16082 : {
16083 1900 : tree t;
16084 :
16085 1900 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16086 1900 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16087 1900 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16088 1900 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16089 : }
16090 : }
16091 : else
16092 : {
16093 0 : if (!div)
16094 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16095 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16096 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16097 : }
16098 :
16099 : return value;
16100 : }
16101 :
16102 : /* Likewise, but round down. */
16103 :
16104 : tree
16105 20509658 : round_down_loc (location_t loc, tree value, int divisor)
16106 : {
16107 20509658 : tree div = NULL_TREE;
16108 :
16109 20509658 : gcc_assert (divisor > 0);
16110 20509658 : if (divisor == 1)
16111 : return value;
16112 :
16113 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16114 : have to do anything. Only do this when we are not given a const,
16115 : because in that case, this check is more expensive than just
16116 : doing it. */
16117 20509658 : if (TREE_CODE (value) != INTEGER_CST)
16118 : {
16119 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16120 :
16121 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16122 : return value;
16123 : }
16124 :
16125 : /* If divisor is a power of two, simplify this to bit manipulation. */
16126 20509658 : if (pow2_or_zerop (divisor))
16127 : {
16128 20509658 : tree t;
16129 :
16130 20509658 : t = build_int_cst (TREE_TYPE (value), -divisor);
16131 20509658 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16132 : }
16133 : else
16134 : {
16135 0 : if (!div)
16136 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16137 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16138 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16139 : }
16140 :
16141 : return value;
16142 : }
16143 :
16144 : /* Returns the pointer to the base of the object addressed by EXP and
16145 : extracts the information about the offset of the access, storing it
16146 : to PBITPOS and POFFSET. */
16147 :
16148 : static tree
16149 2018066 : split_address_to_core_and_offset (tree exp,
16150 : poly_int64 *pbitpos, tree *poffset)
16151 : {
16152 2018066 : tree core;
16153 2018066 : machine_mode mode;
16154 2018066 : int unsignedp, reversep, volatilep;
16155 2018066 : poly_int64 bitsize;
16156 2018066 : location_t loc = EXPR_LOCATION (exp);
16157 :
16158 2018066 : if (TREE_CODE (exp) == SSA_NAME)
16159 438235 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16160 330960 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16161 30056 : exp = gimple_assign_rhs1 (def);
16162 :
16163 2018066 : if (TREE_CODE (exp) == ADDR_EXPR)
16164 : {
16165 1141031 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16166 : poffset, &mode, &unsignedp, &reversep,
16167 : &volatilep);
16168 : /* If we are left with MEM[a + CST] strip that and add it to the
16169 : pbitpos and return a. */
16170 1141031 : if (TREE_CODE (core) == MEM_REF)
16171 : {
16172 26881 : poly_offset_int tem;
16173 26881 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16174 26881 : tem <<= LOG2_BITS_PER_UNIT;
16175 26881 : tem += *pbitpos;
16176 26881 : if (tem.to_shwi (pbitpos))
16177 26703 : return TREE_OPERAND (core, 0);
16178 : }
16179 1114328 : core = build_fold_addr_expr_loc (loc, core);
16180 : }
16181 877035 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16182 : {
16183 257063 : core = TREE_OPERAND (exp, 0);
16184 257063 : STRIP_NOPS (core);
16185 257063 : *pbitpos = 0;
16186 257063 : *poffset = TREE_OPERAND (exp, 1);
16187 257063 : if (poly_int_tree_p (*poffset))
16188 : {
16189 256978 : poly_offset_int tem
16190 256978 : = wi::sext (wi::to_poly_offset (*poffset),
16191 256978 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16192 256978 : tem <<= LOG2_BITS_PER_UNIT;
16193 256978 : if (tem.to_shwi (pbitpos))
16194 256978 : *poffset = NULL_TREE;
16195 : }
16196 : }
16197 : else
16198 : {
16199 619972 : core = exp;
16200 619972 : *pbitpos = 0;
16201 619972 : *poffset = NULL_TREE;
16202 : }
16203 :
16204 : return core;
16205 : }
16206 :
16207 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16208 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16209 :
16210 : bool
16211 1009033 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16212 : {
16213 1009033 : tree core1, core2;
16214 1009033 : poly_int64 bitpos1, bitpos2;
16215 1009033 : tree toffset1, toffset2, tdiff, type;
16216 :
16217 1009033 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16218 1009033 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16219 :
16220 1009033 : poly_int64 bytepos1, bytepos2;
16221 1009033 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16222 1618095 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16223 2018066 : || !operand_equal_p (core1, core2, 0))
16224 609062 : return false;
16225 :
16226 399971 : if (toffset1 && toffset2)
16227 : {
16228 29 : type = TREE_TYPE (toffset1);
16229 29 : if (type != TREE_TYPE (toffset2))
16230 0 : toffset2 = fold_convert (type, toffset2);
16231 :
16232 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16233 29 : if (!cst_and_fits_in_hwi (tdiff))
16234 : return false;
16235 :
16236 15 : *diff = int_cst_value (tdiff);
16237 : }
16238 399942 : else if (toffset1 || toffset2)
16239 : {
16240 : /* If only one of the offsets is non-constant, the difference cannot
16241 : be a constant. */
16242 : return false;
16243 : }
16244 : else
16245 381624 : *diff = 0;
16246 :
16247 381639 : *diff += bytepos1 - bytepos2;
16248 381639 : return true;
16249 : }
16250 :
16251 : /* Return OFF converted to a pointer offset type suitable as offset for
16252 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16253 : tree
16254 27577372 : convert_to_ptrofftype_loc (location_t loc, tree off)
16255 : {
16256 27577372 : if (ptrofftype_p (TREE_TYPE (off)))
16257 : return off;
16258 3944901 : return fold_convert_loc (loc, sizetype, off);
16259 : }
16260 :
16261 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16262 : tree
16263 23695130 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16264 : {
16265 23695130 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16266 23695130 : ptr, convert_to_ptrofftype_loc (loc, off));
16267 : }
16268 :
16269 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16270 : tree
16271 163746 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16272 : {
16273 163746 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16274 163746 : ptr, size_int (off));
16275 : }
16276 :
16277 : /* Return a pointer to a NUL-terminated string containing the sequence
16278 : of bytes corresponding to the representation of the object referred to
16279 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16280 : to an initialized constant array plus some constant offset).
16281 : Set *STRSIZE the number of bytes in the constant sequence including
16282 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16283 : where A is the array that stores the constant sequence that SRC points
16284 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16285 : need not point to a string or even an array of characters but may point
16286 : to an object of any type. */
16287 :
16288 : const char *
16289 12363712 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16290 : {
16291 : /* The offset into the array A storing the string, and A's byte size. */
16292 12363712 : tree offset_node;
16293 12363712 : tree mem_size;
16294 :
16295 12363712 : if (strsize)
16296 4649518 : *strsize = 0;
16297 :
16298 12363712 : if (strsize)
16299 4649518 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16300 : else
16301 7714194 : src = string_constant (src, &offset_node, &mem_size, NULL);
16302 12363712 : if (!src)
16303 : return NULL;
16304 :
16305 2778206 : unsigned HOST_WIDE_INT offset = 0;
16306 2778206 : if (offset_node != NULL_TREE)
16307 : {
16308 2778206 : if (!tree_fits_uhwi_p (offset_node))
16309 : return NULL;
16310 : else
16311 2776442 : offset = tree_to_uhwi (offset_node);
16312 : }
16313 :
16314 2776442 : if (!tree_fits_uhwi_p (mem_size))
16315 : return NULL;
16316 :
16317 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16318 : is stored in and equal to sizeof A. INIT_BYTES is the number
16319 : of bytes in the constant sequence used to initialize the array,
16320 : including any embedded NULs as well as the terminating NUL (for
16321 : strings), but not including any trailing zeros/NULs past
16322 : the terminating one appended implicitly to a string literal to
16323 : zero out the remainder of the array it's stored in. For example,
16324 : given:
16325 : const char a[7] = "abc\0d";
16326 : n = strlen (a + 1);
16327 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16328 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16329 : is equal to strlen (A) + 1. */
16330 2776442 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16331 2776442 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16332 2776442 : const char *string = TREE_STRING_POINTER (src);
16333 :
16334 : /* Ideally this would turn into a gcc_checking_assert over time. */
16335 2776442 : if (init_bytes > array_size)
16336 : init_bytes = array_size;
16337 :
16338 2776442 : if (init_bytes == 0 || offset >= array_size)
16339 : return NULL;
16340 :
16341 2775177 : if (strsize)
16342 : {
16343 : /* Compute and store the number of characters from the beginning
16344 : of the substring at OFFSET to the end, including the terminating
16345 : nul. Offsets past the initial length refer to null strings. */
16346 1440945 : if (offset < init_bytes)
16347 1440945 : *strsize = init_bytes - offset;
16348 : else
16349 0 : *strsize = 1;
16350 : }
16351 : else
16352 : {
16353 1334232 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16354 : /* Support only properly NUL-terminated single byte strings. */
16355 1334232 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16356 : return NULL;
16357 1329524 : if (string[init_bytes - 1] != '\0')
16358 : return NULL;
16359 : }
16360 :
16361 2748542 : return offset < init_bytes ? string + offset : "";
16362 : }
16363 :
16364 : /* Return a pointer to a NUL-terminated string corresponding to
16365 : the expression STR referencing a constant string, possibly
16366 : involving a constant offset. Return null if STR either doesn't
16367 : reference a constant string or if it involves a nonconstant
16368 : offset. */
16369 :
16370 : const char *
16371 7714194 : c_getstr (tree str)
16372 : {
16373 7714194 : return getbyterep (str, NULL);
16374 : }
16375 :
16376 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16377 : may be nonzero, with precision PREC, the precision of T's type. */
16378 :
16379 : static wide_int
16380 246170395 : tree_nonzero_bits (const_tree t, unsigned prec)
16381 : {
16382 246170395 : switch (TREE_CODE (t))
16383 : {
16384 8269373 : case INTEGER_CST:
16385 8269373 : return wi::to_wide (t);
16386 137968375 : case SSA_NAME:
16387 137968375 : return get_nonzero_bits (t);
16388 253805 : case NON_LVALUE_EXPR:
16389 253805 : case SAVE_EXPR:
16390 253805 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16391 450591 : case BIT_AND_EXPR:
16392 901182 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16393 1351773 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16394 5387 : case BIT_IOR_EXPR:
16395 5387 : case BIT_XOR_EXPR:
16396 10774 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16397 16161 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16398 66562 : case COND_EXPR:
16399 133124 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16400 199686 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16401 51777117 : CASE_CONVERT:
16402 51777117 : if (TREE_TYPE (t) != error_mark_node
16403 51777117 : && !error_operand_p (TREE_OPERAND (t, 0)))
16404 : {
16405 51777116 : tree op0 = TREE_OPERAND (t, 0);
16406 51777116 : tree inner_type = TREE_TYPE (op0);
16407 51777116 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16408 103554232 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16409 103554232 : prec, TYPE_SIGN (inner_type));
16410 : }
16411 : break;
16412 13577801 : case PLUS_EXPR:
16413 13577801 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16414 : {
16415 13577801 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16416 13577801 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16417 13577801 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16418 494255 : return wi::bit_or (nzbits1, nzbits2);
16419 13577801 : }
16420 : break;
16421 165679 : case LSHIFT_EXPR:
16422 165679 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16423 165679 : && TREE_TYPE (t) != error_mark_node)
16424 : {
16425 93800 : tree type = TREE_TYPE (t);
16426 93800 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16427 93800 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16428 93800 : return wi::neg_p (arg1)
16429 187600 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16430 93800 : : wi::lshift (nzbits, arg1);
16431 93800 : }
16432 : break;
16433 158068 : case RSHIFT_EXPR:
16434 158068 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16435 158068 : && TREE_TYPE (t) != error_mark_node)
16436 : {
16437 156344 : tree type = TREE_TYPE (t);
16438 156344 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16439 156344 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16440 156344 : return wi::neg_p (arg1)
16441 312688 : ? wi::lshift (nzbits, -arg1)
16442 156344 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16443 156344 : }
16444 : break;
16445 : default:
16446 : break;
16447 : }
16448 :
16449 46634787 : return wi::shwi (-1, prec);
16450 : }
16451 :
16452 : /* Given a tree T, compute which bits in T may be nonzero. */
16453 :
16454 : wide_int
16455 165688648 : tree_nonzero_bits (const_tree t)
16456 : {
16457 165688648 : if (error_operand_p (t))
16458 0 : return wi::shwi (-1, 64);
16459 165688648 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16460 : }
16461 :
16462 : /* Helper function for address compare simplifications in match.pd.
16463 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16464 : TYPE is the type of comparison operands.
16465 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16466 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16467 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16468 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16469 : and 2 if unknown. */
16470 :
16471 : int
16472 3265776 : address_compare (tree_code code, tree type, tree op0, tree op1,
16473 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16474 : bool generic)
16475 : {
16476 3265776 : if (TREE_CODE (op0) == SSA_NAME)
16477 35023 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16478 3265776 : if (TREE_CODE (op1) == SSA_NAME)
16479 4469 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16480 3265776 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16481 3265776 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16482 3265776 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16483 3265776 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16484 3265776 : if (base0 && TREE_CODE (base0) == MEM_REF)
16485 : {
16486 34277 : off0 += mem_ref_offset (base0).force_shwi ();
16487 34277 : base0 = TREE_OPERAND (base0, 0);
16488 : }
16489 3265776 : if (base1 && TREE_CODE (base1) == MEM_REF)
16490 : {
16491 3605 : off1 += mem_ref_offset (base1).force_shwi ();
16492 3605 : base1 = TREE_OPERAND (base1, 0);
16493 : }
16494 3265776 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16495 : return 2;
16496 :
16497 3253890 : int equal = 2;
16498 : /* Punt in GENERIC on variables with value expressions;
16499 : the value expressions might point to fields/elements
16500 : of other vars etc. */
16501 3253890 : if (generic
16502 3253890 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16503 3124921 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16504 : return 2;
16505 3253332 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16506 : {
16507 100624 : symtab_node *node0 = symtab_node::get_create (base0);
16508 100624 : symtab_node *node1 = symtab_node::get_create (base1);
16509 100624 : equal = node0->equal_address_to (node1);
16510 : }
16511 3152708 : else if ((DECL_P (base0)
16512 199782 : || TREE_CODE (base0) == SSA_NAME
16513 166624 : || TREE_CODE (base0) == STRING_CST)
16514 3152511 : && (DECL_P (base1)
16515 170106 : || TREE_CODE (base1) == SSA_NAME
16516 166727 : || TREE_CODE (base1) == STRING_CST))
16517 3152496 : equal = (base0 == base1);
16518 : /* Assume different STRING_CSTs with the same content will be
16519 : merged. */
16520 3253120 : if (equal == 0
16521 73989 : && TREE_CODE (base0) == STRING_CST
16522 17620 : && TREE_CODE (base1) == STRING_CST
16523 17432 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16524 3253120 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16525 6199 : TREE_STRING_LENGTH (base0)) == 0)
16526 : equal = 1;
16527 3248899 : if (equal == 1)
16528 : {
16529 3158840 : if (code == EQ_EXPR
16530 3158840 : || code == NE_EXPR
16531 : /* If the offsets are equal we can ignore overflow. */
16532 122148 : || known_eq (off0, off1)
16533 244072 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16534 : /* Or if we compare using pointers to decls or strings. */
16535 3280876 : || (POINTER_TYPE_P (type)
16536 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16537 : return 1;
16538 : return 2;
16539 : }
16540 94492 : if (equal != 0)
16541 : return equal;
16542 69556 : if (code != EQ_EXPR && code != NE_EXPR)
16543 : return 2;
16544 :
16545 : /* At this point we know (or assume) the two pointers point at
16546 : different objects. */
16547 64364 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16548 64364 : off0.is_constant (&ioff0);
16549 64364 : off1.is_constant (&ioff1);
16550 : /* Punt on non-zero offsets from functions. */
16551 64364 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16552 64364 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16553 : return 2;
16554 : /* Or if the bases are neither decls nor string literals. */
16555 64364 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16556 : return 2;
16557 31940 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16558 : return 2;
16559 : /* For initializers, assume addresses of different functions are
16560 : different. */
16561 31940 : if (folding_initializer
16562 7139 : && TREE_CODE (base0) == FUNCTION_DECL
16563 17 : && TREE_CODE (base1) == FUNCTION_DECL)
16564 : return 0;
16565 :
16566 : /* Compute whether one address points to the start of one
16567 : object and another one to the end of another one. */
16568 31923 : poly_int64 size0 = 0, size1 = 0;
16569 31923 : if (TREE_CODE (base0) == STRING_CST)
16570 : {
16571 13021 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16572 : equal = 2;
16573 : else
16574 : size0 = TREE_STRING_LENGTH (base0);
16575 : }
16576 18902 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16577 : size0 = 1;
16578 : else
16579 : {
16580 18640 : tree sz0 = DECL_SIZE_UNIT (base0);
16581 18640 : if (!tree_fits_poly_int64_p (sz0))
16582 : equal = 2;
16583 : else
16584 18640 : size0 = tree_to_poly_int64 (sz0);
16585 : }
16586 31923 : if (TREE_CODE (base1) == STRING_CST)
16587 : {
16588 13125 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16589 : equal = 2;
16590 : else
16591 : size1 = TREE_STRING_LENGTH (base1);
16592 : }
16593 18798 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16594 : size1 = 1;
16595 : else
16596 : {
16597 18540 : tree sz1 = DECL_SIZE_UNIT (base1);
16598 18540 : if (!tree_fits_poly_int64_p (sz1))
16599 : equal = 2;
16600 : else
16601 18540 : size1 = tree_to_poly_int64 (sz1);
16602 : }
16603 31923 : if (equal == 0)
16604 : {
16605 : /* If one offset is pointing (or could be) to the beginning of one
16606 : object and the other is pointing to one past the last byte of the
16607 : other object, punt. */
16608 31911 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16609 : equal = 2;
16610 31774 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16611 : equal = 2;
16612 : /* If both offsets are the same, there are some cases we know that are
16613 : ok. Either if we know they aren't zero, or if we know both sizes
16614 : are no zero. */
16615 : if (equal == 2
16616 273 : && known_eq (off0, off1)
16617 22 : && (known_ne (off0, 0)
16618 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16619 : equal = 0;
16620 : }
16621 :
16622 : /* At this point, equal is 2 if either one or both pointers are out of
16623 : bounds of their object, or one points to start of its object and the
16624 : other points to end of its object. This is unspecified behavior
16625 : e.g. in C++. Otherwise equal is 0. */
16626 31923 : if (folding_cxx_constexpr && equal)
16627 : return equal;
16628 :
16629 : /* When both pointers point to string literals, even when equal is 0,
16630 : due to tail merging of string literals the pointers might be the same. */
16631 31860 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16632 : {
16633 12981 : if (ioff0 < 0
16634 12981 : || ioff1 < 0
16635 12981 : || ioff0 > TREE_STRING_LENGTH (base0)
16636 25950 : || ioff1 > TREE_STRING_LENGTH (base1))
16637 : return 2;
16638 :
16639 : /* If the bytes in the string literals starting at the pointers
16640 : differ, the pointers need to be different. */
16641 12969 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16642 12969 : TREE_STRING_POINTER (base1) + ioff1,
16643 12969 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16644 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16645 : {
16646 3897 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16647 3897 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16648 3897 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16649 : ioffmin) == 0)
16650 : /* If even the bytes in the string literal before the
16651 : pointers are the same, the string literals could be
16652 : tail merged. */
16653 : return 2;
16654 : }
16655 : return 0;
16656 : }
16657 :
16658 18879 : if (folding_cxx_constexpr)
16659 : return 0;
16660 :
16661 : /* If this is a pointer comparison, ignore for now even
16662 : valid equalities where one pointer is the offset zero
16663 : of one object and the other to one past end of another one. */
16664 11860 : if (!INTEGRAL_TYPE_P (type))
16665 : return 0;
16666 :
16667 : /* Assume that string literals can't be adjacent to variables
16668 : (automatic or global). */
16669 317 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16670 : return 0;
16671 :
16672 : /* Assume that automatic variables can't be adjacent to global
16673 : variables. */
16674 296 : if (is_global_var (base0) != is_global_var (base1))
16675 : return 0;
16676 :
16677 : return equal;
16678 : }
16679 :
16680 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16681 : tree
16682 52 : ctor_single_nonzero_element (const_tree t)
16683 : {
16684 52 : unsigned HOST_WIDE_INT idx;
16685 52 : constructor_elt *ce;
16686 52 : tree elt = NULL_TREE;
16687 :
16688 52 : if (TREE_CODE (t) != CONSTRUCTOR)
16689 : return NULL_TREE;
16690 113 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16691 110 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16692 : {
16693 101 : if (elt)
16694 : return NULL_TREE;
16695 52 : elt = ce->value;
16696 : }
16697 : return elt;
16698 : }
16699 :
16700 : #if CHECKING_P
16701 :
16702 : namespace selftest {
16703 :
16704 : /* Helper functions for writing tests of folding trees. */
16705 :
16706 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16707 :
16708 : static void
16709 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16710 : tree constant)
16711 : {
16712 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16713 16 : }
16714 :
16715 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16716 : wrapping WRAPPED_EXPR. */
16717 :
16718 : static void
16719 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16720 : tree wrapped_expr)
16721 : {
16722 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16723 12 : ASSERT_NE (wrapped_expr, result);
16724 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16725 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16726 12 : }
16727 :
16728 : /* Verify that various arithmetic binary operations are folded
16729 : correctly. */
16730 :
16731 : static void
16732 4 : test_arithmetic_folding ()
16733 : {
16734 4 : tree type = integer_type_node;
16735 4 : tree x = create_tmp_var_raw (type, "x");
16736 4 : tree zero = build_zero_cst (type);
16737 4 : tree one = build_int_cst (type, 1);
16738 :
16739 : /* Addition. */
16740 : /* 1 <-- (0 + 1) */
16741 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16742 : one);
16743 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16744 : one);
16745 :
16746 : /* (nonlvalue)x <-- (x + 0) */
16747 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16748 : x);
16749 :
16750 : /* Subtraction. */
16751 : /* 0 <-- (x - x) */
16752 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16753 : zero);
16754 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16755 : x);
16756 :
16757 : /* Multiplication. */
16758 : /* 0 <-- (x * 0) */
16759 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16760 : zero);
16761 :
16762 : /* (nonlvalue)x <-- (x * 1) */
16763 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16764 : x);
16765 4 : }
16766 :
16767 : namespace test_operand_equality {
16768 :
16769 : /* Verify structural equality. */
16770 :
16771 : /* Execute fold_vec_perm_cst unit tests. */
16772 :
16773 : static void
16774 4 : test ()
16775 : {
16776 4 : tree stype = integer_type_node;
16777 4 : tree utype = unsigned_type_node;
16778 4 : tree x = create_tmp_var_raw (stype, "x");
16779 4 : tree y = create_tmp_var_raw (stype, "y");
16780 4 : tree z = create_tmp_var_raw (stype, "z");
16781 4 : tree four = build_int_cst (stype, 4);
16782 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16783 4 : tree rhs1 = fold_convert (stype,
16784 : fold_build2 (PLUS_EXPR, utype,
16785 : fold_convert (utype, x),
16786 : fold_convert (utype, y)));
16787 :
16788 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16789 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16790 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16791 :
16792 : /* (int)(unsigned) x == x. */
16793 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16794 : build1 (NOP_EXPR, utype, x));
16795 4 : tree rhs2 = x;
16796 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16797 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16798 :
16799 : /* (unsigned x) + (unsigned y) == x + y. */
16800 4 : tree lhs3 = lhs1;
16801 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16802 : fold_convert (utype, x),
16803 : fold_convert (utype, y));
16804 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16805 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16806 :
16807 : /* (unsigned x) / (unsigned y) == x / y. */
16808 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16809 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16810 : fold_convert (utype, x),
16811 : fold_convert (utype, y));
16812 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16813 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16814 :
16815 : /* (long x) / 4 == (long)(x / 4). */
16816 4 : tree lstype = long_long_integer_type_node;
16817 4 : tree lfour = build_int_cst (lstype, 4);
16818 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16819 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16820 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16821 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16822 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16823 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16824 :
16825 : /* (unsigned x) / 4 == x / 4. */
16826 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16827 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16828 : fold_convert (utype, x),
16829 : fold_convert (utype, four));
16830 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16831 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16832 :
16833 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16834 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16835 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16836 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16837 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16838 :
16839 : /* (unsigned x) + 4 == x + 4. */
16840 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16841 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16842 : fold_convert (utype, x),
16843 : fold_convert (utype, four));
16844 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16845 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16846 :
16847 : /* (unsigned x) + 4 == 4 + x. */
16848 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16849 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16850 : fold_convert (utype, x),
16851 : fold_convert (utype, four));
16852 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16853 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16854 :
16855 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16856 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16857 : fold_build2 (MULT_EXPR, stype,
16858 : fold_build2 (PLUS_EXPR, stype, four, x),
16859 : y),
16860 : z);
16861 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16862 : fold_build2 (PLUS_EXPR, utype,
16863 : fold_convert (utype, x),
16864 : fold_convert (utype, four)),
16865 : fold_convert (utype, y));
16866 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16867 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16868 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16869 4 : }
16870 : }
16871 :
16872 : namespace test_fold_vec_perm_cst {
16873 :
16874 : /* Build a VECTOR_CST corresponding to VMODE, and has
16875 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16876 : Fill it with randomized elements, using rand() % THRESHOLD. */
16877 :
16878 : static tree
16879 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
16880 : unsigned nelts_per_pattern,
16881 : int step = 0, bool natural_stepped = false,
16882 : int threshold = 100)
16883 : {
16884 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
16885 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
16886 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
16887 :
16888 : // Fill a0 for each pattern
16889 0 : for (unsigned i = 0; i < npatterns; i++)
16890 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
16891 :
16892 0 : if (nelts_per_pattern == 1)
16893 0 : return builder.build ();
16894 :
16895 : // Fill a1 for each pattern
16896 0 : for (unsigned i = 0; i < npatterns; i++)
16897 : {
16898 0 : tree a1;
16899 0 : if (natural_stepped)
16900 : {
16901 0 : tree a0 = builder[i];
16902 0 : wide_int a0_val = wi::to_wide (a0);
16903 0 : wide_int a1_val = a0_val + step;
16904 0 : a1 = wide_int_to_tree (inner_type, a1_val);
16905 0 : }
16906 : else
16907 0 : a1 = build_int_cst (inner_type, rand () % threshold);
16908 0 : builder.quick_push (a1);
16909 : }
16910 0 : if (nelts_per_pattern == 2)
16911 0 : return builder.build ();
16912 :
16913 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
16914 : {
16915 0 : tree prev_elem = builder[i - npatterns];
16916 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
16917 0 : wide_int val = prev_elem_val + step;
16918 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
16919 0 : }
16920 :
16921 0 : return builder.build ();
16922 0 : }
16923 :
16924 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16925 : when result is VLA. */
16926 :
16927 : static void
16928 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
16929 : tree res, tree *expected_res)
16930 : {
16931 : /* Actual npatterns and encoded_elts in res may be less than expected due
16932 : to canonicalization. */
16933 0 : ASSERT_TRUE (res != NULL_TREE);
16934 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
16935 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
16936 :
16937 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
16938 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16939 0 : }
16940 :
16941 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16942 : when the result is VLS. */
16943 :
16944 : static void
16945 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
16946 : {
16947 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
16948 0 : for (unsigned i = 0; i < expected_nelts; i++)
16949 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16950 0 : }
16951 :
16952 : /* Helper routine to push multiple elements into BUILDER. */
16953 : template<unsigned N>
16954 0 : static void builder_push_elems (vec_perm_builder& builder,
16955 : poly_uint64 (&elems)[N])
16956 : {
16957 0 : for (unsigned i = 0; i < N; i++)
16958 0 : builder.quick_push (elems[i]);
16959 0 : }
16960 :
16961 : #define ARG0(index) vector_cst_elt (arg0, index)
16962 : #define ARG1(index) vector_cst_elt (arg1, index)
16963 :
16964 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
16965 :
16966 : static void
16967 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
16968 : {
16969 0 : for (int i = 0; i < 10; i++)
16970 : {
16971 : /* Case 1:
16972 : sel = { 0, 4, 1, 5, ... }
16973 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
16974 0 : {
16975 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16976 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16977 :
16978 0 : tree inner_type
16979 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
16980 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
16981 :
16982 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16983 0 : vec_perm_builder builder (res_len, 4, 1);
16984 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
16985 0 : builder_push_elems (builder, mask_elems);
16986 :
16987 0 : vec_perm_indices sel (builder, 2, res_len);
16988 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
16989 :
16990 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
16991 0 : validate_res (4, 1, res, expected_res);
16992 0 : }
16993 :
16994 : /* Case 2: Same as case 1, but contains an out of bounds access which
16995 : should wrap around.
16996 : sel = {0, 8, 4, 12, ...} (4, 1)
16997 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
16998 0 : {
16999 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17000 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17001 :
17002 0 : tree inner_type
17003 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17004 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17005 :
17006 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17007 0 : vec_perm_builder builder (res_len, 4, 1);
17008 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17009 0 : builder_push_elems (builder, mask_elems);
17010 :
17011 0 : vec_perm_indices sel (builder, 2, res_len);
17012 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17013 :
17014 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17015 0 : validate_res (4, 1, res, expected_res);
17016 0 : }
17017 : }
17018 0 : }
17019 :
17020 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17021 :
17022 : static void
17023 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17024 : {
17025 0 : for (int i = 0; i < 10; i++)
17026 : {
17027 : /* Case 1:
17028 : sel = { 0, 1, 2, 3}
17029 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17030 0 : {
17031 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17032 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17033 :
17034 0 : tree inner_type
17035 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17036 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17037 :
17038 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17039 0 : vec_perm_builder builder (res_len, 4, 1);
17040 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17041 0 : builder_push_elems (builder, mask_elems);
17042 :
17043 0 : vec_perm_indices sel (builder, 2, res_len);
17044 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17045 :
17046 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17047 0 : validate_res_vls (res, expected_res, 4);
17048 0 : }
17049 :
17050 : /* Case 2: Same as Case 1, but crossing input vector.
17051 : sel = {0, 2, 4, 6}
17052 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17053 : Since we cannot determine, which vector to choose from during
17054 : compile time, should return NULL_TREE. */
17055 0 : {
17056 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17057 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17058 :
17059 0 : tree inner_type
17060 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17061 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17062 :
17063 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17064 0 : vec_perm_builder builder (res_len, 4, 1);
17065 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17066 0 : builder_push_elems (builder, mask_elems);
17067 :
17068 0 : vec_perm_indices sel (builder, 2, res_len);
17069 0 : const char *reason;
17070 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17071 :
17072 0 : ASSERT_TRUE (res == NULL_TREE);
17073 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17074 0 : }
17075 : }
17076 0 : }
17077 :
17078 : /* Test all input vectors. */
17079 :
17080 : static void
17081 0 : test_all_nunits (machine_mode vmode)
17082 : {
17083 : /* Test with 10 different inputs. */
17084 0 : for (int i = 0; i < 10; i++)
17085 : {
17086 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17087 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17088 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17089 :
17090 : /* Case 1: mask = {0, ...} // (1, 1)
17091 : res = { arg0[0], ... } // (1, 1) */
17092 0 : {
17093 0 : vec_perm_builder builder (len, 1, 1);
17094 0 : builder.quick_push (0);
17095 0 : vec_perm_indices sel (builder, 2, len);
17096 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17097 0 : tree expected_res[] = { ARG0(0) };
17098 0 : validate_res (1, 1, res, expected_res);
17099 0 : }
17100 :
17101 : /* Case 2: mask = {len, ...} // (1, 1)
17102 : res = { arg1[0], ... } // (1, 1) */
17103 0 : {
17104 0 : vec_perm_builder builder (len, 1, 1);
17105 0 : builder.quick_push (len);
17106 0 : vec_perm_indices sel (builder, 2, len);
17107 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17108 :
17109 0 : tree expected_res[] = { ARG1(0) };
17110 0 : validate_res (1, 1, res, expected_res);
17111 0 : }
17112 : }
17113 0 : }
17114 :
17115 : /* Test all vectors which contain at-least 2 elements. */
17116 :
17117 : static void
17118 0 : test_nunits_min_2 (machine_mode vmode)
17119 : {
17120 0 : for (int i = 0; i < 10; i++)
17121 : {
17122 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17123 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17124 0 : {
17125 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17126 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17127 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17128 :
17129 0 : vec_perm_builder builder (len, 2, 1);
17130 0 : poly_uint64 mask_elems[] = { 0, len };
17131 0 : builder_push_elems (builder, mask_elems);
17132 :
17133 0 : vec_perm_indices sel (builder, 2, len);
17134 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17135 :
17136 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17137 0 : validate_res (2, 1, res, expected_res);
17138 0 : }
17139 :
17140 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17141 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17142 0 : {
17143 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17144 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17145 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17146 :
17147 0 : vec_perm_builder builder (len, 2, 2);
17148 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17149 0 : builder_push_elems (builder, mask_elems);
17150 :
17151 0 : vec_perm_indices sel (builder, 2, len);
17152 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17153 :
17154 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17155 0 : validate_res (2, 2, res, expected_res);
17156 0 : }
17157 :
17158 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17159 : Test that the stepped sequence of the pattern selects from
17160 : same input pattern. Since input vectors have npatterns = 2,
17161 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17162 : in input vector. So return NULL_TREE. */
17163 0 : {
17164 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17165 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17166 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17167 :
17168 0 : vec_perm_builder builder (len, 1, 3);
17169 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17170 0 : builder_push_elems (builder, mask_elems);
17171 :
17172 0 : vec_perm_indices sel (builder, 2, len);
17173 0 : const char *reason;
17174 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17175 : &reason);
17176 0 : ASSERT_TRUE (res == NULL_TREE);
17177 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17178 0 : }
17179 :
17180 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17181 : Test that stepped sequence of the pattern selects from arg0.
17182 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17183 0 : {
17184 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17185 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17186 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17187 :
17188 0 : vec_perm_builder builder (len, 1, 3);
17189 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17190 0 : builder_push_elems (builder, mask_elems);
17191 :
17192 0 : vec_perm_indices sel (builder, 2, len);
17193 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17194 :
17195 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17196 0 : validate_res (1, 3, res, expected_res);
17197 0 : }
17198 :
17199 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17200 : In this case ensure that arg has a natural stepped sequence
17201 : to preserve arg's encoding.
17202 :
17203 : As a concrete example, consider:
17204 : arg0: { -16, -9, -10, ... } // (1, 3)
17205 : arg1: { -12, -5, -6, ... } // (1, 3)
17206 : sel = { 0, len, len + 1, ... } // (1, 3)
17207 :
17208 : This will create res with following encoding:
17209 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17210 : = { -16, -12, -5, ... }
17211 :
17212 : The step in above encoding would be: (-5) - (-12) = 7
17213 : And hence res[3] would be computed as -5 + 7 = 2.
17214 : instead of arg1[2], ie, -6.
17215 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17216 : for this case. */
17217 0 : {
17218 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17219 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17220 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17221 :
17222 0 : vec_perm_builder builder (len, 1, 3);
17223 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17224 0 : builder_push_elems (builder, mask_elems);
17225 :
17226 0 : vec_perm_indices sel (builder, 2, len);
17227 0 : const char *reason;
17228 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17229 : stepped pattern, even if we didn't explicitly tell it to. So folding
17230 : may not always fail, but if it does, ensure that's because arg1 does
17231 : not have a natural stepped sequence (and not due to other reason) */
17232 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17233 0 : if (res == NULL_TREE)
17234 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17235 0 : }
17236 :
17237 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17238 : sequence and thus folding should be valid for this case. */
17239 0 : {
17240 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17241 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17242 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17243 :
17244 0 : vec_perm_builder builder (len, 1, 3);
17245 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17246 0 : builder_push_elems (builder, mask_elems);
17247 :
17248 0 : vec_perm_indices sel (builder, 2, len);
17249 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17250 :
17251 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17252 0 : validate_res (1, 3, res, expected_res);
17253 0 : }
17254 :
17255 : /* Case 8: Same as aarch64/sve/slp_3.c:
17256 : arg0, arg1 are dup vectors.
17257 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17258 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17259 :
17260 : In this case, since the input vectors are dup, only the first two
17261 : elements per pattern in sel are considered significant. */
17262 0 : {
17263 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17264 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17265 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17266 :
17267 0 : vec_perm_builder builder (len, 2, 3);
17268 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17269 0 : builder_push_elems (builder, mask_elems);
17270 :
17271 0 : vec_perm_indices sel (builder, 2, len);
17272 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17273 :
17274 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17275 0 : validate_res (2, 1, res, expected_res);
17276 0 : }
17277 : }
17278 0 : }
17279 :
17280 : /* Test all vectors which contain at-least 4 elements. */
17281 :
17282 : static void
17283 0 : test_nunits_min_4 (machine_mode vmode)
17284 : {
17285 0 : for (int i = 0; i < 10; i++)
17286 : {
17287 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17288 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17289 0 : {
17290 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17291 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17292 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17293 :
17294 0 : vec_perm_builder builder (len, 4, 1);
17295 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17296 0 : builder_push_elems (builder, mask_elems);
17297 :
17298 0 : vec_perm_indices sel (builder, 2, len);
17299 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17300 :
17301 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17302 0 : validate_res (4, 1, res, expected_res);
17303 0 : }
17304 :
17305 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17306 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17307 0 : {
17308 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17309 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17310 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17311 :
17312 0 : vec_perm_builder builder (arg0_len, 1, 3);
17313 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17314 0 : builder_push_elems (builder, mask_elems);
17315 :
17316 0 : vec_perm_indices sel (builder, 2, arg0_len);
17317 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17318 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17319 0 : validate_res (1, 3, res, expected_res);
17320 0 : }
17321 :
17322 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17323 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17324 0 : {
17325 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17326 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17327 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17328 :
17329 0 : vec_perm_builder builder (len, 1, 3);
17330 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17331 0 : builder_push_elems (builder, mask_elems);
17332 :
17333 0 : vec_perm_indices sel (builder, 2, len);
17334 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17335 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17336 0 : validate_res (1, 3, res, expected_res);
17337 0 : }
17338 :
17339 : /* Case 4:
17340 : sel = { len, 0, 2, ... } // (1, 3)
17341 : This should return NULL because we cross the input vectors.
17342 : Because,
17343 : Let's assume len = C + Cx
17344 : a1 = 0
17345 : S = 2
17346 : esel = arg0_len / sel_npatterns = C + Cx
17347 : ae = 0 + (esel - 2) * S
17348 : = 0 + (C + Cx - 2) * 2
17349 : = 2(C-2) + 2Cx
17350 :
17351 : For C >= 4:
17352 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17353 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17354 : Since q1 != qe, we cross input vectors.
17355 : So return NULL_TREE. */
17356 0 : {
17357 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17358 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17359 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17360 :
17361 0 : vec_perm_builder builder (arg0_len, 1, 3);
17362 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17363 0 : builder_push_elems (builder, mask_elems);
17364 :
17365 0 : vec_perm_indices sel (builder, 2, arg0_len);
17366 0 : const char *reason;
17367 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17368 0 : ASSERT_TRUE (res == NULL_TREE);
17369 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17370 0 : }
17371 :
17372 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17373 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17374 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17375 :
17376 : Note that fold_vec_perm_cst will set
17377 : res_npatterns = max(4, max(4, 2)) = 4
17378 : However after canonicalizing, we will end up with shape (2, 2). */
17379 0 : {
17380 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17381 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17382 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17383 :
17384 0 : vec_perm_builder builder (len, 2, 2);
17385 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17386 0 : builder_push_elems (builder, mask_elems);
17387 :
17388 0 : vec_perm_indices sel (builder, 2, len);
17389 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17390 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17391 0 : validate_res (2, 2, res, expected_res);
17392 0 : }
17393 :
17394 : /* Case 6: Test combination in sel, where one pattern is dup and other
17395 : is stepped sequence.
17396 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17397 : res = { arg0[0], arg0[0], arg0[0],
17398 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17399 0 : {
17400 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17401 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17402 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17403 :
17404 0 : vec_perm_builder builder (len, 2, 3);
17405 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17406 0 : builder_push_elems (builder, mask_elems);
17407 :
17408 0 : vec_perm_indices sel (builder, 2, len);
17409 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17410 :
17411 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17412 0 : ARG0(1), ARG0(0), ARG0(2) };
17413 0 : validate_res (2, 3, res, expected_res);
17414 0 : }
17415 :
17416 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17417 : when arg0, arg1 and sel have different number of patterns.
17418 : arg0 is of shape (1, 1)
17419 : arg1 is of shape (4, 1)
17420 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17421 :
17422 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17423 : However,
17424 : step = (len+2) - (len+1) = 1
17425 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17426 : Since step is not a multiple of arg_npatterns,
17427 : valid_mask_for_fold_vec_perm_cst should return false,
17428 : and thus fold_vec_perm_cst should return NULL_TREE. */
17429 0 : {
17430 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17431 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17432 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17433 :
17434 0 : vec_perm_builder builder (len, 2, 3);
17435 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17436 0 : builder_push_elems (builder, mask_elems);
17437 :
17438 0 : vec_perm_indices sel (builder, 2, len);
17439 0 : const char *reason;
17440 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17441 :
17442 0 : ASSERT_TRUE (res == NULL_TREE);
17443 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17444 0 : }
17445 :
17446 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17447 : check that the result is not a stepped sequence either, even
17448 : if sel has a stepped sequence. */
17449 0 : {
17450 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17451 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17452 :
17453 0 : vec_perm_builder builder (len, 1, 3);
17454 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17455 0 : builder_push_elems (builder, mask_elems);
17456 :
17457 0 : vec_perm_indices sel (builder, 1, len);
17458 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17459 :
17460 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17461 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17462 0 : }
17463 :
17464 : /* Case 9: If sel doesn't contain a stepped sequence,
17465 : check that the result has same encoding as sel, irrespective
17466 : of shape of input vectors. */
17467 0 : {
17468 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17469 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17470 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17471 :
17472 0 : vec_perm_builder builder (len, 1, 2);
17473 0 : poly_uint64 mask_elems[] = { 0, len };
17474 0 : builder_push_elems (builder, mask_elems);
17475 :
17476 0 : vec_perm_indices sel (builder, 2, len);
17477 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17478 :
17479 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17480 0 : validate_res (sel.encoding ().npatterns (),
17481 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17482 0 : }
17483 : }
17484 0 : }
17485 :
17486 : /* Test all vectors which contain at-least 8 elements. */
17487 :
17488 : static void
17489 0 : test_nunits_min_8 (machine_mode vmode)
17490 : {
17491 0 : for (int i = 0; i < 10; i++)
17492 : {
17493 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17494 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17495 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17496 : arg0[2], arg0[0], arg0[3], arg1[0],
17497 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17498 0 : {
17499 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17500 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17501 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17502 :
17503 0 : vec_perm_builder builder(len, 4, 3);
17504 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17505 0 : 4, 0, 5, len };
17506 0 : builder_push_elems (builder, mask_elems);
17507 :
17508 0 : vec_perm_indices sel (builder, 2, len);
17509 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17510 :
17511 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17512 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17513 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17514 0 : validate_res (4, 3, res, expected_res);
17515 0 : }
17516 : }
17517 0 : }
17518 :
17519 : /* Test vectors for which nunits[0] <= 4. */
17520 :
17521 : static void
17522 0 : test_nunits_max_4 (machine_mode vmode)
17523 : {
17524 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17525 : This should return NULL_TREE because the index 4 may choose
17526 : from either arg0 or arg1 depending on vector length. */
17527 0 : {
17528 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17529 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17530 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17531 :
17532 0 : vec_perm_builder builder (len, 1, 2);
17533 0 : poly_uint64 mask_elems[] = {0, 4};
17534 0 : builder_push_elems (builder, mask_elems);
17535 :
17536 0 : vec_perm_indices sel (builder, 2, len);
17537 0 : const char *reason;
17538 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17539 0 : ASSERT_TRUE (res == NULL_TREE);
17540 0 : ASSERT_TRUE (reason != NULL);
17541 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17542 0 : }
17543 0 : }
17544 :
17545 : #undef ARG0
17546 : #undef ARG1
17547 :
17548 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17549 :
17550 : static bool
17551 0 : is_simple_vla_size (poly_uint64 size)
17552 : {
17553 124 : if (size.is_constant ()
17554 : || !pow2p_hwi (size.coeffs[0]))
17555 0 : return false;
17556 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17557 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17558 : return false;
17559 : return true;
17560 : }
17561 :
17562 : /* Execute fold_vec_perm_cst unit tests. */
17563 :
17564 : static void
17565 4 : test ()
17566 : {
17567 4 : machine_mode vnx4si_mode = E_VOIDmode;
17568 4 : machine_mode v4si_mode = E_VOIDmode;
17569 :
17570 4 : machine_mode vmode;
17571 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17572 : {
17573 : /* Obtain modes corresponding to VNx4SI and V4SI,
17574 : to call mixed mode tests below.
17575 : FIXME: Is there a better way to do this ? */
17576 124 : if (GET_MODE_INNER (vmode) == SImode)
17577 : {
17578 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17579 124 : if (is_simple_vla_size (nunits)
17580 : && nunits.coeffs[0] == 4)
17581 : vnx4si_mode = vmode;
17582 124 : else if (known_eq (nunits, poly_uint64 (4)))
17583 124 : v4si_mode = vmode;
17584 : }
17585 :
17586 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17587 : || !targetm.vector_mode_supported_p (vmode))
17588 124 : continue;
17589 :
17590 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17591 : test_all_nunits (vmode);
17592 : if (nunits.coeffs[0] >= 2)
17593 : test_nunits_min_2 (vmode);
17594 : if (nunits.coeffs[0] >= 4)
17595 : test_nunits_min_4 (vmode);
17596 : if (nunits.coeffs[0] >= 8)
17597 : test_nunits_min_8 (vmode);
17598 :
17599 : if (nunits.coeffs[0] <= 4)
17600 : test_nunits_max_4 (vmode);
17601 : }
17602 :
17603 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17604 : && targetm.vector_mode_supported_p (vnx4si_mode)
17605 : && targetm.vector_mode_supported_p (v4si_mode))
17606 : {
17607 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17608 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17609 : }
17610 4 : }
17611 : } // end of test_fold_vec_perm_cst namespace
17612 :
17613 : /* Verify that various binary operations on vectors are folded
17614 : correctly. */
17615 :
17616 : static void
17617 4 : test_vector_folding ()
17618 : {
17619 4 : tree inner_type = integer_type_node;
17620 4 : tree type = build_vector_type (inner_type, 4);
17621 4 : tree zero = build_zero_cst (type);
17622 4 : tree one = build_one_cst (type);
17623 4 : tree index = build_index_vector (type, 0, 1);
17624 :
17625 : /* Verify equality tests that return a scalar boolean result. */
17626 4 : tree res_type = boolean_type_node;
17627 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17628 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17629 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17630 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17631 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17632 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17633 : index, one)));
17634 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17635 : index, index)));
17636 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17637 : index, index)));
17638 4 : }
17639 :
17640 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17641 :
17642 : static void
17643 4 : test_vec_duplicate_folding ()
17644 : {
17645 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17646 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17647 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17648 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17649 :
17650 4 : tree type = build_vector_type (ssizetype, nunits);
17651 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17652 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17653 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17654 4 : }
17655 :
17656 : /* Run all of the selftests within this file. */
17657 :
17658 : void
17659 4 : fold_const_cc_tests ()
17660 : {
17661 4 : test_arithmetic_folding ();
17662 4 : test_vector_folding ();
17663 4 : test_vec_duplicate_folding ();
17664 4 : test_fold_vec_perm_cst::test ();
17665 4 : test_operand_equality::test ();
17666 4 : }
17667 :
17668 : } // namespace selftest
17669 :
17670 : #endif /* CHECKING_P */
|