Branch data Line data Source code
1 : : /* Fold a constant sub-tree into a single node for C-compiler
2 : : Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : /*@@ This file should be rewritten to use an arbitrary precision
21 : : @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 : : @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 : : @@ The routines that translate from the ap rep should
24 : : @@ warn if precision et. al. is lost.
25 : : @@ This would also make life easier when this technology is used
26 : : @@ for cross-compilers. */
27 : :
28 : : /* The entry points in this file are fold, size_int 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 : :
90 : : /* Nonzero if we are folding constants inside an initializer or a C++
91 : : manifestly-constant-evaluated context; zero otherwise.
92 : : Should be used when folding in initializer enables additional
93 : : optimizations. */
94 : : int folding_initializer = 0;
95 : :
96 : : /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
97 : : otherwise.
98 : : Should be used when certain constructs shouldn't be optimized
99 : : during folding in that context. */
100 : : bool folding_cxx_constexpr = false;
101 : :
102 : : /* The following constants represent a bit based encoding of GCC's
103 : : comparison operators. This encoding simplifies transformations
104 : : on relational comparison operators, such as AND and OR. */
105 : : enum comparison_code {
106 : : COMPCODE_FALSE = 0,
107 : : COMPCODE_LT = 1,
108 : : COMPCODE_EQ = 2,
109 : : COMPCODE_LE = 3,
110 : : COMPCODE_GT = 4,
111 : : COMPCODE_LTGT = 5,
112 : : COMPCODE_GE = 6,
113 : : COMPCODE_ORD = 7,
114 : : COMPCODE_UNORD = 8,
115 : : COMPCODE_UNLT = 9,
116 : : COMPCODE_UNEQ = 10,
117 : : COMPCODE_UNLE = 11,
118 : : COMPCODE_UNGT = 12,
119 : : COMPCODE_NE = 13,
120 : : COMPCODE_UNGE = 14,
121 : : COMPCODE_TRUE = 15
122 : : };
123 : :
124 : : static bool negate_expr_p (tree);
125 : : static tree negate_expr (tree);
126 : : static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
127 : : static enum comparison_code comparison_to_compcode (enum tree_code);
128 : : static enum tree_code compcode_to_comparison (enum comparison_code);
129 : : static bool twoval_comparison_p (tree, tree *, tree *);
130 : : static tree eval_subst (location_t, tree, tree, tree, tree, tree);
131 : : static tree optimize_bit_field_compare (location_t, enum tree_code,
132 : : tree, tree, tree);
133 : : static bool simple_operand_p (const_tree);
134 : : static tree range_binop (enum tree_code, tree, tree, int, tree, int);
135 : : static tree range_predecessor (tree);
136 : : static tree range_successor (tree);
137 : : static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
138 : : static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
139 : : tree, tree, tree, tree);
140 : : static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
141 : : static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
142 : : static tree fold_binary_op_with_conditional_arg (location_t,
143 : : enum tree_code, tree,
144 : : tree, tree,
145 : : tree, tree, int);
146 : : static tree fold_negate_const (tree, tree);
147 : : static tree fold_not_const (const_tree, tree);
148 : : static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 : : static tree fold_convert_const (enum tree_code, tree, tree);
150 : : static tree fold_view_convert_expr (tree, tree);
151 : : static tree fold_negate_expr (location_t, tree);
152 : :
153 : : /* This is a helper function to detect min/max for some operands of COND_EXPR.
154 : : The form is "(exp0 CMP cst1) ? exp0 : cst2". */
155 : : tree_code
156 : 127797 : minmax_from_comparison (tree_code cmp, tree exp0,
157 : : const widest_int cst1,
158 : : const widest_int cst2)
159 : : {
160 : 127797 : if (cst1 == cst2)
161 : : {
162 : 252 : if (cmp == LE_EXPR || cmp == LT_EXPR)
163 : : return MIN_EXPR;
164 : 100 : if (cmp == GT_EXPR || cmp == GE_EXPR)
165 : : return MAX_EXPR;
166 : : }
167 : 127645 : if (cst1 == cst2 - 1)
168 : : {
169 : : /* X <= Y - 1 equals to X < Y. */
170 : 79476 : if (cmp == LE_EXPR)
171 : : return MIN_EXPR;
172 : : /* X > Y - 1 equals to X >= Y. */
173 : 79086 : if (cmp == GT_EXPR)
174 : : return MAX_EXPR;
175 : : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
176 : 69313 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
177 : : {
178 : 17230 : int_range_max r;
179 : 34460 : get_range_query (cfun)->range_of_expr (r, exp0);
180 : 17230 : if (r.undefined_p ())
181 : 0 : r.set_varying (TREE_TYPE (exp0));
182 : :
183 : 17230 : widest_int min = widest_int::from (r.lower_bound (),
184 : 34460 : TYPE_SIGN (TREE_TYPE (exp0)));
185 : 17230 : if (min == cst1)
186 : 772 : return MAX_EXPR;
187 : 17230 : }
188 : : }
189 : 116710 : if (cst1 == cst2 + 1)
190 : : {
191 : : /* X < Y + 1 equals to X <= Y. */
192 : 1032 : if (cmp == LT_EXPR)
193 : : return MIN_EXPR;
194 : : /* X >= Y + 1 equals to X > Y. */
195 : 1004 : if (cmp == GE_EXPR)
196 : : return MAX_EXPR;
197 : : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
198 : 876 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
199 : : {
200 : 517 : int_range_max r;
201 : 1034 : get_range_query (cfun)->range_of_expr (r, exp0);
202 : 517 : if (r.undefined_p ())
203 : 0 : r.set_varying (TREE_TYPE (exp0));
204 : :
205 : 517 : widest_int max = widest_int::from (r.upper_bound (),
206 : 1034 : TYPE_SIGN (TREE_TYPE (exp0)));
207 : 517 : if (max == cst1)
208 : 43 : return MIN_EXPR;
209 : 517 : }
210 : : }
211 : : return ERROR_MARK;
212 : : }
213 : :
214 : :
215 : : /* This is a helper function to detect min/max for some operands of COND_EXPR.
216 : : The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
217 : : tree_code
218 : 141109 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
219 : : {
220 : 141109 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
221 : 11 : return ERROR_MARK;
222 : :
223 : 141098 : if (!operand_equal_p (exp0, exp2))
224 : : return ERROR_MARK;
225 : :
226 : 141098 : if (operand_equal_p (exp1, exp3))
227 : : {
228 : 13600 : if (cmp == LT_EXPR || cmp == LE_EXPR)
229 : : return MIN_EXPR;
230 : 11577 : if (cmp == GT_EXPR || cmp == GE_EXPR)
231 : : return MAX_EXPR;
232 : : }
233 : 127598 : if (TREE_CODE (exp3) == INTEGER_CST
234 : 127514 : && TREE_CODE (exp1) == INTEGER_CST)
235 : 126965 : return minmax_from_comparison (cmp, exp0, wi::to_widest (exp1), wi::to_widest (exp3));
236 : : return ERROR_MARK;
237 : : }
238 : :
239 : : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
240 : : Otherwise, return LOC. */
241 : :
242 : : static location_t
243 : 2474732 : expr_location_or (tree t, location_t loc)
244 : : {
245 : 668515 : location_t tloc = EXPR_LOCATION (t);
246 : 2458930 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
247 : : }
248 : :
249 : : /* Similar to protected_set_expr_location, but never modify x in place,
250 : : if location can and needs to be set, unshare it. */
251 : :
252 : : tree
253 : 6145244 : protected_set_expr_location_unshare (tree x, location_t loc)
254 : : {
255 : 6145244 : if (CAN_HAVE_LOCATION_P (x)
256 : 5499642 : && EXPR_LOCATION (x) != loc
257 : 2923497 : && !(TREE_CODE (x) == SAVE_EXPR
258 : 1461978 : || TREE_CODE (x) == TARGET_EXPR
259 : : || TREE_CODE (x) == BIND_EXPR))
260 : : {
261 : 1461207 : x = copy_node (x);
262 : 1461207 : SET_EXPR_LOCATION (x, loc);
263 : : }
264 : 6145244 : return x;
265 : : }
266 : :
267 : : /* This is nonzero if we should defer warnings about undefined
268 : : overflow. This facility exists because these warnings are a
269 : : special case. The code to estimate loop iterations does not want
270 : : to issue any warnings, since it works with expressions which do not
271 : : occur in user code. Various bits of cleanup code call fold(), but
272 : : only use the result if it has certain characteristics (e.g., is a
273 : : constant); that code only wants to issue a warning if the result is
274 : : used. */
275 : :
276 : : static int fold_deferring_overflow_warnings;
277 : :
278 : : /* If a warning about undefined overflow is deferred, this is the
279 : : warning. Note that this may cause us to turn two warnings into
280 : : one, but that is fine since it is sufficient to only give one
281 : : warning per expression. */
282 : :
283 : : static const char* fold_deferred_overflow_warning;
284 : :
285 : : /* If a warning about undefined overflow is deferred, this is the
286 : : level at which the warning should be emitted. */
287 : :
288 : : static enum warn_strict_overflow_code fold_deferred_overflow_code;
289 : :
290 : : /* Start deferring overflow warnings. We could use a stack here to
291 : : permit nested calls, but at present it is not necessary. */
292 : :
293 : : void
294 : 1203123999 : fold_defer_overflow_warnings (void)
295 : : {
296 : 1203123999 : ++fold_deferring_overflow_warnings;
297 : 1203123999 : }
298 : :
299 : : /* Stop deferring overflow warnings. If there is a pending warning,
300 : : and ISSUE is true, then issue the warning if appropriate. STMT is
301 : : the statement with which the warning should be associated (used for
302 : : location information); STMT may be NULL. CODE is the level of the
303 : : warning--a warn_strict_overflow_code value. This function will use
304 : : the smaller of CODE and the deferred code when deciding whether to
305 : : issue the warning. CODE may be zero to mean to always use the
306 : : deferred code. */
307 : :
308 : : void
309 : 1203123999 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
310 : : {
311 : 1203123999 : const char *warnmsg;
312 : 1203123999 : location_t locus;
313 : :
314 : 1203123999 : gcc_assert (fold_deferring_overflow_warnings > 0);
315 : 1203123999 : --fold_deferring_overflow_warnings;
316 : 1203123999 : if (fold_deferring_overflow_warnings > 0)
317 : : {
318 : 9096573 : if (fold_deferred_overflow_warning != NULL
319 : 1785760 : && code != 0
320 : 0 : && code < (int) fold_deferred_overflow_code)
321 : 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
322 : 9096573 : return;
323 : : }
324 : :
325 : 1194027426 : warnmsg = fold_deferred_overflow_warning;
326 : 1194027426 : fold_deferred_overflow_warning = NULL;
327 : :
328 : 1194027426 : if (!issue || warnmsg == NULL)
329 : : return;
330 : :
331 : 10787 : if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
332 : : return;
333 : :
334 : : /* Use the smallest code level when deciding to issue the
335 : : warning. */
336 : 10787 : if (code == 0 || code > (int) fold_deferred_overflow_code)
337 : 10787 : code = fold_deferred_overflow_code;
338 : :
339 : 10787 : if (!issue_strict_overflow_warning (code))
340 : : return;
341 : :
342 : 0 : if (stmt == NULL)
343 : : locus = input_location;
344 : : else
345 : 0 : locus = gimple_location (stmt);
346 : 0 : warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
347 : : }
348 : :
349 : : /* Stop deferring overflow warnings, ignoring any deferred
350 : : warnings. */
351 : :
352 : : void
353 : 188746927 : fold_undefer_and_ignore_overflow_warnings (void)
354 : : {
355 : 188746927 : fold_undefer_overflow_warnings (false, NULL, 0);
356 : 188746927 : }
357 : :
358 : : /* Whether we are deferring overflow warnings. */
359 : :
360 : : bool
361 : 324196742 : fold_deferring_overflow_warnings_p (void)
362 : : {
363 : 324196742 : return fold_deferring_overflow_warnings > 0;
364 : : }
365 : :
366 : : /* This is called when we fold something based on the fact that signed
367 : : overflow is undefined. */
368 : :
369 : : void
370 : 1742024 : fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
371 : : {
372 : 1742024 : if (fold_deferring_overflow_warnings > 0)
373 : : {
374 : 1690632 : if (fold_deferred_overflow_warning == NULL
375 : 740170 : || wc < fold_deferred_overflow_code)
376 : : {
377 : 973551 : fold_deferred_overflow_warning = gmsgid;
378 : 973551 : fold_deferred_overflow_code = wc;
379 : : }
380 : : }
381 : 51392 : else if (issue_strict_overflow_warning (wc))
382 : 7 : warning (OPT_Wstrict_overflow, gmsgid);
383 : 1742024 : }
384 : :
385 : : /* Return true if the built-in mathematical function specified by CODE
386 : : is odd, i.e. -f(x) == f(-x). */
387 : :
388 : : bool
389 : 2094004 : negate_mathfn_p (combined_fn fn)
390 : : {
391 : 2094004 : switch (fn)
392 : : {
393 : : CASE_CFN_ASIN:
394 : : CASE_CFN_ASIN_FN:
395 : : CASE_CFN_ASINH:
396 : : CASE_CFN_ASINH_FN:
397 : : CASE_CFN_ASINPI:
398 : : CASE_CFN_ASINPI_FN:
399 : : CASE_CFN_ATAN:
400 : : CASE_CFN_ATAN_FN:
401 : : CASE_CFN_ATANH:
402 : : CASE_CFN_ATANH_FN:
403 : : CASE_CFN_ATANPI:
404 : : CASE_CFN_ATANPI_FN:
405 : : CASE_CFN_CASIN:
406 : : CASE_CFN_CASIN_FN:
407 : : CASE_CFN_CASINH:
408 : : CASE_CFN_CASINH_FN:
409 : : CASE_CFN_CATAN:
410 : : CASE_CFN_CATAN_FN:
411 : : CASE_CFN_CATANH:
412 : : CASE_CFN_CATANH_FN:
413 : : CASE_CFN_CBRT:
414 : : CASE_CFN_CBRT_FN:
415 : : CASE_CFN_CPROJ:
416 : : CASE_CFN_CPROJ_FN:
417 : : CASE_CFN_CSIN:
418 : : CASE_CFN_CSIN_FN:
419 : : CASE_CFN_CSINH:
420 : : CASE_CFN_CSINH_FN:
421 : : CASE_CFN_CTAN:
422 : : CASE_CFN_CTAN_FN:
423 : : CASE_CFN_CTANH:
424 : : CASE_CFN_CTANH_FN:
425 : : CASE_CFN_ERF:
426 : : CASE_CFN_ERF_FN:
427 : : CASE_CFN_LLROUND:
428 : : CASE_CFN_LLROUND_FN:
429 : : CASE_CFN_LROUND:
430 : : CASE_CFN_LROUND_FN:
431 : : CASE_CFN_ROUND:
432 : : CASE_CFN_ROUNDEVEN:
433 : : CASE_CFN_ROUNDEVEN_FN:
434 : : CASE_CFN_SIN:
435 : : CASE_CFN_SIN_FN:
436 : : CASE_CFN_SINH:
437 : : CASE_CFN_SINH_FN:
438 : : CASE_CFN_SINPI:
439 : : CASE_CFN_SINPI_FN:
440 : : CASE_CFN_TAN:
441 : : CASE_CFN_TAN_FN:
442 : : CASE_CFN_TANH:
443 : : CASE_CFN_TANH_FN:
444 : : CASE_CFN_TANPI:
445 : : CASE_CFN_TANPI_FN:
446 : : CASE_CFN_TRUNC:
447 : : CASE_CFN_TRUNC_FN:
448 : : return true;
449 : :
450 : 408 : CASE_CFN_LLRINT:
451 : 408 : CASE_CFN_LLRINT_FN:
452 : 408 : CASE_CFN_LRINT:
453 : 408 : CASE_CFN_LRINT_FN:
454 : 408 : CASE_CFN_NEARBYINT:
455 : 408 : CASE_CFN_NEARBYINT_FN:
456 : 408 : CASE_CFN_RINT:
457 : 408 : CASE_CFN_RINT_FN:
458 : 408 : return !flag_rounding_math;
459 : :
460 : 2090003 : default:
461 : 2090003 : break;
462 : : }
463 : 2090003 : return false;
464 : : }
465 : :
466 : : /* Check whether we may negate an integer constant T without causing
467 : : overflow. */
468 : :
469 : : bool
470 : 3116065 : may_negate_without_overflow_p (const_tree t)
471 : : {
472 : 3116065 : tree type;
473 : :
474 : 3116065 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
475 : :
476 : 3116065 : type = TREE_TYPE (t);
477 : 3116065 : if (TYPE_UNSIGNED (type))
478 : : return false;
479 : :
480 : 3116065 : return !wi::only_sign_bit_p (wi::to_wide (t));
481 : : }
482 : :
483 : : /* Determine whether an expression T can be cheaply negated using
484 : : the function negate_expr without introducing undefined overflow. */
485 : :
486 : : static bool
487 : 27342651 : negate_expr_p (tree t)
488 : : {
489 : 27499438 : tree type;
490 : :
491 : 27499438 : if (t == 0)
492 : : return false;
493 : :
494 : 27499438 : type = TREE_TYPE (t);
495 : :
496 : 27499438 : STRIP_SIGN_NOPS (t);
497 : 27499438 : switch (TREE_CODE (t))
498 : : {
499 : 1600948 : case INTEGER_CST:
500 : 1600948 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
501 : : return true;
502 : :
503 : : /* Check that -CST will not overflow type. */
504 : 383023 : return may_negate_without_overflow_p (t);
505 : 539 : case BIT_NOT_EXPR:
506 : 539 : return (INTEGRAL_TYPE_P (type)
507 : 539 : && TYPE_OVERFLOW_WRAPS (type));
508 : :
509 : : case FIXED_CST:
510 : : return true;
511 : :
512 : 1308 : case NEGATE_EXPR:
513 : 1308 : return !TYPE_OVERFLOW_SANITIZED (type);
514 : :
515 : 1355136 : case REAL_CST:
516 : : /* We want to canonicalize to positive real constants. Pretend
517 : : that only negative ones can be easily negated. */
518 : 1355136 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
519 : :
520 : 454 : case COMPLEX_CST:
521 : 454 : return negate_expr_p (TREE_REALPART (t))
522 : 572 : && negate_expr_p (TREE_IMAGPART (t));
523 : :
524 : 101 : case VECTOR_CST:
525 : 101 : {
526 : 101 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
527 : : return true;
528 : :
529 : : /* Steps don't prevent negation. */
530 : 101 : unsigned int count = vector_cst_encoded_nelts (t);
531 : 202 : for (unsigned int i = 0; i < count; ++i)
532 : 101 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
533 : : return false;
534 : :
535 : : return true;
536 : : }
537 : :
538 : 705 : case COMPLEX_EXPR:
539 : 705 : return negate_expr_p (TREE_OPERAND (t, 0))
540 : 705 : && negate_expr_p (TREE_OPERAND (t, 1));
541 : :
542 : 33 : case CONJ_EXPR:
543 : 33 : return negate_expr_p (TREE_OPERAND (t, 0));
544 : :
545 : 1539716 : case PLUS_EXPR:
546 : 1539716 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
547 : 1539710 : || HONOR_SIGNED_ZEROS (type)
548 : 2777003 : || (ANY_INTEGRAL_TYPE_P (type)
549 : 1237101 : && ! TYPE_OVERFLOW_WRAPS (type)))
550 : 756575 : return false;
551 : : /* -(A + B) -> (-B) - A. */
552 : 783141 : if (negate_expr_p (TREE_OPERAND (t, 1)))
553 : : return true;
554 : : /* -(A + B) -> (-A) - B. */
555 : 141941 : return negate_expr_p (TREE_OPERAND (t, 0));
556 : :
557 : 257778 : case MINUS_EXPR:
558 : : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
559 : 257778 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
560 : 257778 : && !HONOR_SIGNED_ZEROS (type)
561 : 333022 : && (! ANY_INTEGRAL_TYPE_P (type)
562 : 75021 : || TYPE_OVERFLOW_WRAPS (type));
563 : :
564 : 2474479 : case MULT_EXPR:
565 : 2474479 : if (TYPE_UNSIGNED (type))
566 : : break;
567 : : /* INT_MIN/n * n doesn't overflow while negating one operand it does
568 : : if n is a (negative) power of two. */
569 : 4305120 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
570 : 160024 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
571 : 2310246 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
572 : 0 : && (wi::popcount
573 : 2152560 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
574 : 157686 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
575 : 134924 : && (wi::popcount
576 : 4417282 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
577 : : break;
578 : :
579 : : /* Fall through. */
580 : :
581 : 2446474 : case RDIV_EXPR:
582 : 2446474 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
583 : 2446473 : return negate_expr_p (TREE_OPERAND (t, 1))
584 : 2446473 : || negate_expr_p (TREE_OPERAND (t, 0));
585 : : break;
586 : :
587 : 2872 : case TRUNC_DIV_EXPR:
588 : 2872 : case ROUND_DIV_EXPR:
589 : 2872 : case EXACT_DIV_EXPR:
590 : 2872 : if (TYPE_UNSIGNED (type))
591 : : break;
592 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
593 : : B is not 1 we change the sign of the result. */
594 : 552 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
595 : 552 : && negate_expr_p (TREE_OPERAND (t, 0)))
596 : : return true;
597 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
598 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
599 : : and actually traps on some architectures. */
600 : 774 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
601 : 387 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
602 : 689 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
603 : 292 : && ! integer_onep (TREE_OPERAND (t, 1))))
604 : 377 : return negate_expr_p (TREE_OPERAND (t, 1));
605 : : break;
606 : :
607 : 4826183 : case NOP_EXPR:
608 : : /* Negate -((double)float) as (double)(-float). */
609 : 4826183 : if (SCALAR_FLOAT_TYPE_P (type))
610 : : {
611 : 14584 : tree tem = strip_float_extensions (t);
612 : 14584 : if (tem != t)
613 : : return negate_expr_p (tem);
614 : : }
615 : : break;
616 : :
617 : 1053882 : case CALL_EXPR:
618 : : /* Negate -f(x) as f(-x). */
619 : 1053882 : if (negate_mathfn_p (get_call_combined_fn (t)))
620 : 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
621 : : break;
622 : :
623 : 643 : case RSHIFT_EXPR:
624 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
625 : 643 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
626 : : {
627 : 498 : tree op1 = TREE_OPERAND (t, 1);
628 : 498 : if (wi::to_wide (op1) == element_precision (type) - 1)
629 : : return true;
630 : : }
631 : : break;
632 : :
633 : : default:
634 : : break;
635 : : }
636 : : return false;
637 : : }
638 : :
639 : : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
640 : : simplification is possible.
641 : : If negate_expr_p would return true for T, NULL_TREE will never be
642 : : returned. */
643 : :
644 : : static tree
645 : 38277221 : fold_negate_expr_1 (location_t loc, tree t)
646 : : {
647 : 38277221 : tree type = TREE_TYPE (t);
648 : 38277221 : tree tem;
649 : :
650 : 38277221 : switch (TREE_CODE (t))
651 : : {
652 : : /* Convert - (~A) to A + 1. */
653 : 162 : case BIT_NOT_EXPR:
654 : 162 : if (INTEGRAL_TYPE_P (type))
655 : 162 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
656 : 162 : build_one_cst (type));
657 : : break;
658 : :
659 : 29577518 : case INTEGER_CST:
660 : 29577518 : tem = fold_negate_const (t, type);
661 : 29577518 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
662 : 9188 : || (ANY_INTEGRAL_TYPE_P (type)
663 : 9188 : && !TYPE_OVERFLOW_TRAPS (type)
664 : 9188 : && TYPE_OVERFLOW_WRAPS (type))
665 : 29585918 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
666 : : return tem;
667 : : break;
668 : :
669 : 2076150 : case POLY_INT_CST:
670 : 2076150 : case REAL_CST:
671 : 2076150 : case FIXED_CST:
672 : 2076150 : tem = fold_negate_const (t, type);
673 : 2076150 : return tem;
674 : :
675 : 66143 : case COMPLEX_CST:
676 : 66143 : {
677 : 66143 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
678 : 66143 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
679 : 66143 : if (rpart && ipart)
680 : 66143 : return build_complex (type, rpart, ipart);
681 : : }
682 : : break;
683 : :
684 : 51056 : case VECTOR_CST:
685 : 51056 : {
686 : 51056 : tree_vector_builder elts;
687 : 51056 : elts.new_unary_operation (type, t, true);
688 : 51056 : unsigned int count = elts.encoded_nelts ();
689 : 124013 : for (unsigned int i = 0; i < count; ++i)
690 : : {
691 : 72957 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
692 : 72957 : if (elt == NULL_TREE)
693 : 0 : return NULL_TREE;
694 : 72957 : elts.quick_push (elt);
695 : : }
696 : :
697 : 51056 : return elts.build ();
698 : 51056 : }
699 : :
700 : 78 : case COMPLEX_EXPR:
701 : 78 : if (negate_expr_p (t))
702 : 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
703 : 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
704 : 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
705 : : break;
706 : :
707 : 21 : case CONJ_EXPR:
708 : 21 : if (negate_expr_p (t))
709 : 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
710 : 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
711 : : break;
712 : :
713 : 1234 : case NEGATE_EXPR:
714 : 1234 : if (!TYPE_OVERFLOW_SANITIZED (type))
715 : 1221 : return TREE_OPERAND (t, 0);
716 : : break;
717 : :
718 : 697604 : case PLUS_EXPR:
719 : 697604 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
720 : 697604 : && !HONOR_SIGNED_ZEROS (type))
721 : : {
722 : : /* -(A + B) -> (-B) - A. */
723 : 697494 : if (negate_expr_p (TREE_OPERAND (t, 1)))
724 : : {
725 : 645036 : tem = negate_expr (TREE_OPERAND (t, 1));
726 : 645036 : return fold_build2_loc (loc, MINUS_EXPR, type,
727 : 1290072 : tem, TREE_OPERAND (t, 0));
728 : : }
729 : :
730 : : /* -(A + B) -> (-A) - B. */
731 : 52458 : if (negate_expr_p (TREE_OPERAND (t, 0)))
732 : : {
733 : 1029 : tem = negate_expr (TREE_OPERAND (t, 0));
734 : 1029 : return fold_build2_loc (loc, MINUS_EXPR, type,
735 : 2058 : tem, TREE_OPERAND (t, 1));
736 : : }
737 : : }
738 : : break;
739 : :
740 : 150999 : case MINUS_EXPR:
741 : : /* - (A - B) -> B - A */
742 : 150999 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
743 : 150999 : && !HONOR_SIGNED_ZEROS (type))
744 : 69972 : return fold_build2_loc (loc, MINUS_EXPR, type,
745 : 139944 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
746 : : break;
747 : :
748 : 167176 : case MULT_EXPR:
749 : 167176 : if (TYPE_UNSIGNED (type))
750 : : break;
751 : :
752 : : /* Fall through. */
753 : :
754 : 34615 : case RDIV_EXPR:
755 : 34615 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
756 : : {
757 : 34615 : tem = TREE_OPERAND (t, 1);
758 : 34615 : if (negate_expr_p (tem))
759 : 63088 : return fold_build2_loc (loc, TREE_CODE (t), type,
760 : 63088 : TREE_OPERAND (t, 0), negate_expr (tem));
761 : 3071 : tem = TREE_OPERAND (t, 0);
762 : 3071 : if (negate_expr_p (tem))
763 : 68 : return fold_build2_loc (loc, TREE_CODE (t), type,
764 : 136 : negate_expr (tem), TREE_OPERAND (t, 1));
765 : : }
766 : : break;
767 : :
768 : 2129 : case TRUNC_DIV_EXPR:
769 : 2129 : case ROUND_DIV_EXPR:
770 : 2129 : case EXACT_DIV_EXPR:
771 : 2129 : if (TYPE_UNSIGNED (type))
772 : : break;
773 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
774 : : B is not 1 we change the sign of the result. */
775 : 728 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
776 : 728 : && negate_expr_p (TREE_OPERAND (t, 0)))
777 : 323 : return fold_build2_loc (loc, TREE_CODE (t), type,
778 : 323 : negate_expr (TREE_OPERAND (t, 0)),
779 : 646 : TREE_OPERAND (t, 1));
780 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
781 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
782 : : and actually traps on some architectures. */
783 : 810 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
784 : 405 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
785 : 321 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
786 : 298 : && ! integer_onep (TREE_OPERAND (t, 1))))
787 : 787 : && negate_expr_p (TREE_OPERAND (t, 1)))
788 : 752 : return fold_build2_loc (loc, TREE_CODE (t), type,
789 : 376 : TREE_OPERAND (t, 0),
790 : 752 : negate_expr (TREE_OPERAND (t, 1)));
791 : : break;
792 : :
793 : 1910532 : case NOP_EXPR:
794 : : /* Convert -((double)float) into (double)(-float). */
795 : 1910532 : if (SCALAR_FLOAT_TYPE_P (type))
796 : : {
797 : 11356 : tem = strip_float_extensions (t);
798 : 11356 : if (tem != t && negate_expr_p (tem))
799 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
800 : : }
801 : : break;
802 : :
803 : 308101 : case CALL_EXPR:
804 : : /* Negate -f(x) as f(-x). */
805 : 308101 : if (negate_mathfn_p (get_call_combined_fn (t))
806 : 309390 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
807 : : {
808 : 1191 : tree fndecl, arg;
809 : :
810 : 1191 : fndecl = get_callee_fndecl (t);
811 : 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
812 : 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
813 : : }
814 : : break;
815 : :
816 : 414 : case RSHIFT_EXPR:
817 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
818 : 414 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
819 : : {
820 : 396 : tree op1 = TREE_OPERAND (t, 1);
821 : 396 : if (wi::to_wide (op1) == element_precision (type) - 1)
822 : : {
823 : 72 : tree ntype = TYPE_UNSIGNED (type)
824 : 72 : ? signed_type_for (type)
825 : 72 : : unsigned_type_for (type);
826 : 72 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
827 : 72 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
828 : 72 : return fold_convert_loc (loc, type, temp);
829 : : }
830 : : }
831 : : break;
832 : :
833 : : default:
834 : : break;
835 : : }
836 : :
837 : : return NULL_TREE;
838 : : }
839 : :
840 : : /* A wrapper for fold_negate_expr_1. */
841 : :
842 : : static tree
843 : 38277221 : fold_negate_expr (location_t loc, tree t)
844 : : {
845 : 38277221 : tree type = TREE_TYPE (t);
846 : 38277221 : STRIP_SIGN_NOPS (t);
847 : 38277221 : tree tem = fold_negate_expr_1 (loc, t);
848 : 38277221 : if (tem == NULL_TREE)
849 : : return NULL_TREE;
850 : 32521790 : return fold_convert_loc (loc, type, tem);
851 : : }
852 : :
853 : : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
854 : : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
855 : : return NULL_TREE. */
856 : :
857 : : static tree
858 : 3639173 : negate_expr (tree t)
859 : : {
860 : 3639173 : tree type, tem;
861 : 3639173 : location_t loc;
862 : :
863 : 3639173 : if (t == NULL_TREE)
864 : : return NULL_TREE;
865 : :
866 : 3639173 : loc = EXPR_LOCATION (t);
867 : 3639173 : type = TREE_TYPE (t);
868 : 3639173 : STRIP_SIGN_NOPS (t);
869 : :
870 : 3639173 : tem = fold_negate_expr (loc, t);
871 : 3639173 : if (!tem)
872 : 1759253 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
873 : 3639173 : return fold_convert_loc (loc, type, tem);
874 : : }
875 : :
876 : : /* Split a tree IN into a constant, literal and variable parts that could be
877 : : combined with CODE to make IN. "constant" means an expression with
878 : : TREE_CONSTANT but that isn't an actual constant. CODE must be a
879 : : commutative arithmetic operation. Store the constant part into *CONP,
880 : : the literal in *LITP and return the variable part. If a part isn't
881 : : present, set it to null. If the tree does not decompose in this way,
882 : : return the entire tree as the variable part and the other parts as null.
883 : :
884 : : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
885 : : case, we negate an operand that was subtracted. Except if it is a
886 : : literal for which we use *MINUS_LITP instead.
887 : :
888 : : If NEGATE_P is true, we are negating all of IN, again except a literal
889 : : for which we use *MINUS_LITP instead. If a variable part is of pointer
890 : : type, it is negated after converting to TYPE. This prevents us from
891 : : generating illegal MINUS pointer expression. LOC is the location of
892 : : the converted variable part.
893 : :
894 : : If IN is itself a literal or constant, return it as appropriate.
895 : :
896 : : Note that we do not guarantee that any of the three values will be the
897 : : same type as IN, but they will have the same signedness and mode. */
898 : :
899 : : static tree
900 : 220611540 : split_tree (tree in, tree type, enum tree_code code,
901 : : tree *minus_varp, tree *conp, tree *minus_conp,
902 : : tree *litp, tree *minus_litp, int negate_p)
903 : : {
904 : 220611540 : tree var = 0;
905 : 220611540 : *minus_varp = 0;
906 : 220611540 : *conp = 0;
907 : 220611540 : *minus_conp = 0;
908 : 220611540 : *litp = 0;
909 : 220611540 : *minus_litp = 0;
910 : :
911 : : /* Strip any conversions that don't change the machine mode or signedness. */
912 : 220611540 : STRIP_SIGN_NOPS (in);
913 : :
914 : 220611540 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
915 : 140326925 : || TREE_CODE (in) == FIXED_CST)
916 : 80284615 : *litp = in;
917 : 140326925 : else if (TREE_CODE (in) == code
918 : 140326925 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
919 : 135939484 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
920 : : /* We can associate addition and subtraction together (even
921 : : though the C standard doesn't say so) for integers because
922 : : the value is not affected. For reals, the value might be
923 : : affected, so we can't. */
924 : 135939484 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
925 : 56866675 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
926 : 134227941 : || (code == MINUS_EXPR
927 : 21698898 : && (TREE_CODE (in) == PLUS_EXPR
928 : 19938327 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
929 : : {
930 : 8289793 : tree op0 = TREE_OPERAND (in, 0);
931 : 8289793 : tree op1 = TREE_OPERAND (in, 1);
932 : 8289793 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
933 : 8289793 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
934 : :
935 : : /* First see if either of the operands is a literal, then a constant. */
936 : 8289793 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
937 : 8058818 : || TREE_CODE (op0) == FIXED_CST)
938 : 230975 : *litp = op0, op0 = 0;
939 : 8058818 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
940 : 5275229 : || TREE_CODE (op1) == FIXED_CST)
941 : 2783589 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
942 : :
943 : 8289793 : if (op0 != 0 && TREE_CONSTANT (op0))
944 : 15213 : *conp = op0, op0 = 0;
945 : 8274580 : else if (op1 != 0 && TREE_CONSTANT (op1))
946 : 52134 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
947 : :
948 : : /* If we haven't dealt with either operand, this is not a case we can
949 : : decompose. Otherwise, VAR is either of the ones remaining, if any. */
950 : 8289793 : if (op0 != 0 && op1 != 0)
951 : : var = in;
952 : 3073934 : else if (op0 != 0)
953 : : var = op0;
954 : : else
955 : 246188 : var = op1, neg_var_p = neg1_p;
956 : :
957 : : /* Now do any needed negations. */
958 : 8289793 : if (neg_litp_p)
959 : 45007 : *minus_litp = *litp, *litp = 0;
960 : 8289793 : if (neg_conp_p && *conp)
961 : 11818 : *minus_conp = *conp, *conp = 0;
962 : 8289793 : if (neg_var_p && var)
963 : 234858 : *minus_varp = var, var = 0;
964 : : }
965 : 132037132 : else if (TREE_CONSTANT (in))
966 : 797496 : *conp = in;
967 : 131239636 : else if (TREE_CODE (in) == BIT_NOT_EXPR
968 : 484264 : && code == PLUS_EXPR)
969 : : {
970 : : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
971 : : when IN is constant. */
972 : 375005 : *litp = build_minus_one_cst (type);
973 : 375005 : *minus_varp = TREE_OPERAND (in, 0);
974 : : }
975 : : else
976 : : var = in;
977 : :
978 : 220611540 : if (negate_p)
979 : : {
980 : 11970324 : if (*litp)
981 : 1250412 : *minus_litp = *litp, *litp = 0;
982 : 10719912 : else if (*minus_litp)
983 : 152 : *litp = *minus_litp, *minus_litp = 0;
984 : 11970324 : if (*conp)
985 : 48173 : *minus_conp = *conp, *conp = 0;
986 : 11922151 : else if (*minus_conp)
987 : 0 : *conp = *minus_conp, *minus_conp = 0;
988 : 11970324 : if (var)
989 : 11909889 : *minus_varp = var, var = 0;
990 : 60435 : else if (*minus_varp)
991 : 757 : var = *minus_varp, *minus_varp = 0;
992 : : }
993 : :
994 : 220611540 : if (*litp
995 : 220611540 : && TREE_OVERFLOW_P (*litp))
996 : 18666 : *litp = drop_tree_overflow (*litp);
997 : 220611540 : if (*minus_litp
998 : 220611540 : && TREE_OVERFLOW_P (*minus_litp))
999 : 0 : *minus_litp = drop_tree_overflow (*minus_litp);
1000 : :
1001 : 220611540 : return var;
1002 : : }
1003 : :
1004 : : /* Re-associate trees split by the above function. T1 and T2 are
1005 : : either expressions to associate or null. Return the new
1006 : : expression, if any. LOC is the location of the new expression. If
1007 : : we build an operation, do it in TYPE and with CODE. */
1008 : :
1009 : : static tree
1010 : 21106965 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1011 : : {
1012 : 21106965 : if (t1 == 0)
1013 : : {
1014 : 13391004 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
1015 : : return t2;
1016 : : }
1017 : 7715961 : else if (t2 == 0)
1018 : : return t1;
1019 : :
1020 : : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1021 : : try to fold this since we will have infinite recursion. But do
1022 : : deal with any NEGATE_EXPRs. */
1023 : 4318377 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1024 : 3398354 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1025 : 3336154 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1026 : : {
1027 : 1702776 : if (code == PLUS_EXPR)
1028 : : {
1029 : 973095 : if (TREE_CODE (t1) == NEGATE_EXPR)
1030 : 54 : return build2_loc (loc, MINUS_EXPR, type,
1031 : : fold_convert_loc (loc, type, t2),
1032 : : fold_convert_loc (loc, type,
1033 : 108 : TREE_OPERAND (t1, 0)));
1034 : 973041 : else if (TREE_CODE (t2) == NEGATE_EXPR)
1035 : 1 : return build2_loc (loc, MINUS_EXPR, type,
1036 : : fold_convert_loc (loc, type, t1),
1037 : : fold_convert_loc (loc, type,
1038 : 2 : TREE_OPERAND (t2, 0)));
1039 : 973040 : else if (integer_zerop (t2))
1040 : 58798 : return fold_convert_loc (loc, type, t1);
1041 : : }
1042 : 729681 : else if (code == MINUS_EXPR)
1043 : : {
1044 : 708021 : if (integer_zerop (t2))
1045 : 0 : return fold_convert_loc (loc, type, t1);
1046 : : }
1047 : :
1048 : 1643923 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1049 : 1643923 : fold_convert_loc (loc, type, t2));
1050 : : }
1051 : :
1052 : 2615601 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1053 : 2615601 : fold_convert_loc (loc, type, t2));
1054 : : }
1055 : :
1056 : : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1057 : : for use in int_const_binop, size_binop and size_diffop. */
1058 : :
1059 : : static bool
1060 : 2247131930 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1061 : : {
1062 : 2247131930 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1063 : : return false;
1064 : 2247131930 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1065 : : return false;
1066 : :
1067 : 2247131930 : switch (code)
1068 : : {
1069 : : case LSHIFT_EXPR:
1070 : : case RSHIFT_EXPR:
1071 : : case LROTATE_EXPR:
1072 : : case RROTATE_EXPR:
1073 : : return true;
1074 : :
1075 : 2247131930 : default:
1076 : 2247131930 : break;
1077 : : }
1078 : :
1079 : 2247131930 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1080 : 2247131930 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1081 : 4494263860 : && TYPE_MODE (type1) == TYPE_MODE (type2);
1082 : : }
1083 : :
1084 : : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1085 : : a new constant in RES. Return FALSE if we don't know how to
1086 : : evaluate CODE at compile-time. */
1087 : :
1088 : : bool
1089 : 1253177869 : wide_int_binop (wide_int &res,
1090 : : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1091 : : signop sign, wi::overflow_type *overflow)
1092 : : {
1093 : 1253177869 : wide_int tmp;
1094 : 1253177869 : *overflow = wi::OVF_NONE;
1095 : 1253177869 : switch (code)
1096 : : {
1097 : 2082143 : case BIT_IOR_EXPR:
1098 : 2082143 : res = wi::bit_or (arg1, arg2);
1099 : 2082143 : break;
1100 : :
1101 : 100308 : case BIT_XOR_EXPR:
1102 : 100308 : res = wi::bit_xor (arg1, arg2);
1103 : 100308 : break;
1104 : :
1105 : 20450457 : case BIT_AND_EXPR:
1106 : 20450457 : res = wi::bit_and (arg1, arg2);
1107 : 20450457 : break;
1108 : :
1109 : 13060627 : case LSHIFT_EXPR:
1110 : 13060627 : if (wi::neg_p (arg2))
1111 : : return false;
1112 : 13030291 : res = wi::lshift (arg1, arg2);
1113 : 13030291 : break;
1114 : :
1115 : 7749169 : case RSHIFT_EXPR:
1116 : 7749169 : if (wi::neg_p (arg2))
1117 : : return false;
1118 : : /* It's unclear from the C standard whether shifts can overflow.
1119 : : The following code ignores overflow; perhaps a C standard
1120 : : interpretation ruling is needed. */
1121 : 7748973 : res = wi::rshift (arg1, arg2, sign);
1122 : 7748973 : break;
1123 : :
1124 : 1838 : case RROTATE_EXPR:
1125 : 1838 : case LROTATE_EXPR:
1126 : 1838 : if (wi::neg_p (arg2))
1127 : : {
1128 : 14 : tmp = -arg2;
1129 : 14 : if (code == RROTATE_EXPR)
1130 : : code = LROTATE_EXPR;
1131 : : else
1132 : : code = RROTATE_EXPR;
1133 : : }
1134 : : else
1135 : 1824 : tmp = arg2;
1136 : :
1137 : 1824 : if (code == RROTATE_EXPR)
1138 : 1656 : res = wi::rrotate (arg1, tmp);
1139 : : else
1140 : 182 : res = wi::lrotate (arg1, tmp);
1141 : : break;
1142 : :
1143 : 210036451 : case PLUS_EXPR:
1144 : 210036451 : res = wi::add (arg1, arg2, sign, overflow);
1145 : 210036451 : break;
1146 : :
1147 : 71543290 : case MINUS_EXPR:
1148 : 71543290 : res = wi::sub (arg1, arg2, sign, overflow);
1149 : 71543290 : break;
1150 : :
1151 : 342483737 : case MULT_EXPR:
1152 : 342483737 : res = wi::mul (arg1, arg2, sign, overflow);
1153 : 342483737 : break;
1154 : :
1155 : 5072 : case MULT_HIGHPART_EXPR:
1156 : 5072 : res = wi::mul_high (arg1, arg2, sign);
1157 : 5072 : break;
1158 : :
1159 : 302694180 : case TRUNC_DIV_EXPR:
1160 : 302694180 : case EXACT_DIV_EXPR:
1161 : 302694180 : if (arg2 == 0)
1162 : : return false;
1163 : 302688235 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1164 : 302688235 : break;
1165 : :
1166 : 69741940 : case FLOOR_DIV_EXPR:
1167 : 69741940 : if (arg2 == 0)
1168 : : return false;
1169 : 69741940 : res = wi::div_floor (arg1, arg2, sign, overflow);
1170 : 69741940 : break;
1171 : :
1172 : 71997904 : case CEIL_DIV_EXPR:
1173 : 71997904 : if (arg2 == 0)
1174 : : return false;
1175 : 71997904 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1176 : 71997904 : break;
1177 : :
1178 : 0 : case ROUND_DIV_EXPR:
1179 : 0 : if (arg2 == 0)
1180 : : return false;
1181 : 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1182 : 0 : break;
1183 : :
1184 : 746495 : case TRUNC_MOD_EXPR:
1185 : 746495 : if (arg2 == 0)
1186 : : return false;
1187 : 745392 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1188 : 745392 : break;
1189 : :
1190 : 58273812 : case FLOOR_MOD_EXPR:
1191 : 58273812 : if (arg2 == 0)
1192 : : return false;
1193 : 58273812 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1194 : 58273812 : break;
1195 : :
1196 : 178 : case CEIL_MOD_EXPR:
1197 : 178 : if (arg2 == 0)
1198 : : return false;
1199 : 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1200 : 178 : break;
1201 : :
1202 : 0 : case ROUND_MOD_EXPR:
1203 : 0 : if (arg2 == 0)
1204 : : return false;
1205 : 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1206 : 0 : break;
1207 : :
1208 : 28143 : case MIN_EXPR:
1209 : 28143 : res = wi::min (arg1, arg2, sign);
1210 : 28143 : break;
1211 : :
1212 : 82181998 : case MAX_EXPR:
1213 : 82181998 : res = wi::max (arg1, arg2, sign);
1214 : 82181998 : break;
1215 : :
1216 : : default:
1217 : : return false;
1218 : : }
1219 : : return true;
1220 : 1253177869 : }
1221 : :
1222 : : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1223 : : min value to RES. */
1224 : : bool
1225 : 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1226 : : {
1227 : 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1228 : : {
1229 : 0 : res = wi::to_poly_wide (arg1);
1230 : 0 : return true;
1231 : : }
1232 : 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1233 : : {
1234 : 0 : res = wi::to_poly_wide (arg2);
1235 : 0 : return true;
1236 : : }
1237 : :
1238 : : return false;
1239 : : }
1240 : :
1241 : : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1242 : : produce a new constant in RES. Return FALSE if we don't know how
1243 : : to evaluate CODE at compile-time. */
1244 : :
1245 : : bool
1246 : 1253177869 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1247 : : const_tree arg1, const_tree arg2,
1248 : : signop sign, wi::overflow_type *overflow)
1249 : : {
1250 : 1253177869 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1251 : :
1252 : 1253177869 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1253 : : {
1254 : 1253177869 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1255 : 1253177869 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1256 : 1253177869 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1257 : : return NULL_TREE;
1258 : 1253140162 : res = wi_res;
1259 : 1253140162 : return true;
1260 : 1253178088 : }
1261 : :
1262 : : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1263 : :
1264 : : switch (code)
1265 : : {
1266 : : case PLUS_EXPR:
1267 : : res = wi::add (wi::to_poly_wide (arg1),
1268 : : wi::to_poly_wide (arg2), sign, overflow);
1269 : : break;
1270 : :
1271 : : case MINUS_EXPR:
1272 : : res = wi::sub (wi::to_poly_wide (arg1),
1273 : : wi::to_poly_wide (arg2), sign, overflow);
1274 : : break;
1275 : :
1276 : : case MULT_EXPR:
1277 : : if (TREE_CODE (arg2) == INTEGER_CST)
1278 : : res = wi::mul (wi::to_poly_wide (arg1),
1279 : : wi::to_wide (arg2), sign, overflow);
1280 : : else if (TREE_CODE (arg1) == INTEGER_CST)
1281 : : res = wi::mul (wi::to_poly_wide (arg2),
1282 : : wi::to_wide (arg1), sign, overflow);
1283 : : else
1284 : : return NULL_TREE;
1285 : : break;
1286 : :
1287 : : case LSHIFT_EXPR:
1288 : : if (TREE_CODE (arg2) == INTEGER_CST)
1289 : : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1290 : : else
1291 : : return false;
1292 : : break;
1293 : :
1294 : : case BIT_AND_EXPR:
1295 : : if (TREE_CODE (arg2) != INTEGER_CST
1296 : : || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1297 : : &res))
1298 : : return false;
1299 : : break;
1300 : :
1301 : : case BIT_IOR_EXPR:
1302 : : if (TREE_CODE (arg2) != INTEGER_CST
1303 : : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1304 : : &res))
1305 : : return false;
1306 : : break;
1307 : :
1308 : : case MIN_EXPR:
1309 : : if (!can_min_p (arg1, arg2, res))
1310 : : return false;
1311 : : break;
1312 : :
1313 : : default:
1314 : : return false;
1315 : : }
1316 : : return true;
1317 : : }
1318 : :
1319 : : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1320 : : produce a new constant. Return NULL_TREE if we don't know how to
1321 : : evaluate CODE at compile-time. */
1322 : :
1323 : : tree
1324 : 1253177869 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1325 : : int overflowable)
1326 : : {
1327 : 1253177869 : poly_wide_int poly_res;
1328 : 1253177869 : tree type = TREE_TYPE (arg1);
1329 : 1253177869 : signop sign = TYPE_SIGN (type);
1330 : 1253177869 : wi::overflow_type overflow = wi::OVF_NONE;
1331 : :
1332 : 1253177869 : if (!poly_int_tree_p (arg1)
1333 : 1253177869 : || !poly_int_tree_p (arg2)
1334 : 2506355738 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1335 : 37707 : return NULL_TREE;
1336 : 1253140162 : return force_fit_type (type, poly_res, overflowable,
1337 : 1253140162 : (((sign == SIGNED || overflowable == -1)
1338 : 1253140162 : && overflow)
1339 : 1253140162 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1340 : 1253177869 : }
1341 : :
1342 : : /* Return true if binary operation OP distributes over addition in operand
1343 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
1344 : :
1345 : : static bool
1346 : 181465 : distributes_over_addition_p (tree_code op, int opno)
1347 : : {
1348 : 0 : switch (op)
1349 : : {
1350 : : case PLUS_EXPR:
1351 : : case MINUS_EXPR:
1352 : : case MULT_EXPR:
1353 : : return true;
1354 : :
1355 : 0 : case LSHIFT_EXPR:
1356 : 0 : return opno == 1;
1357 : :
1358 : 3826 : default:
1359 : 3826 : return false;
1360 : : }
1361 : : }
1362 : :
1363 : : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1364 : : is the other operand. Try to use the value of OP to simplify the
1365 : : operation in one step, without having to process individual elements. */
1366 : : static tree
1367 : 434588 : simplify_const_binop (tree_code code, tree op, tree other_op,
1368 : : int index ATTRIBUTE_UNUSED)
1369 : : {
1370 : : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1371 : 434588 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1372 : : {
1373 : 347384 : if (integer_zerop (other_op))
1374 : : {
1375 : 26135 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1376 : : return op;
1377 : 25250 : else if (code == BIT_AND_EXPR)
1378 : : return other_op;
1379 : : }
1380 : : }
1381 : :
1382 : : return NULL_TREE;
1383 : : }
1384 : :
1385 : : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1386 : : be an elementwise vector operation, try to fold the operation to a
1387 : : constant vector, using ELT_CONST_BINOP to fold each element. Return
1388 : : the folded value on success, otherwise return null. */
1389 : : tree
1390 : 264057 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1391 : : tree (*elt_const_binop) (enum tree_code, tree, tree))
1392 : : {
1393 : 183797 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1394 : 440931 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1395 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1396 : : {
1397 : 176874 : tree type = TREE_TYPE (arg1);
1398 : 176874 : bool step_ok_p;
1399 : 176874 : if (VECTOR_CST_STEPPED_P (arg1)
1400 : 176874 : && VECTOR_CST_STEPPED_P (arg2))
1401 : : /* We can operate directly on the encoding if:
1402 : :
1403 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1404 : : implies
1405 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1406 : :
1407 : : Addition and subtraction are the supported operators
1408 : : for which this is true. */
1409 : 2332 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1410 : 174542 : else if (VECTOR_CST_STEPPED_P (arg1))
1411 : : /* We can operate directly on stepped encodings if:
1412 : :
1413 : : a3 - a2 == a2 - a1
1414 : : implies:
1415 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1416 : :
1417 : : which is true if (x -> x op c) distributes over addition. */
1418 : 51268 : step_ok_p = distributes_over_addition_p (code, 1);
1419 : : else
1420 : : /* Similarly in reverse. */
1421 : 123274 : step_ok_p = distributes_over_addition_p (code, 2);
1422 : 176874 : tree_vector_builder elts;
1423 : 176874 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1424 : : return NULL_TREE;
1425 : 176874 : unsigned int count = elts.encoded_nelts ();
1426 : 697928 : for (unsigned int i = 0; i < count; ++i)
1427 : : {
1428 : 521385 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1429 : 521385 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1430 : :
1431 : 521385 : tree elt = elt_const_binop (code, elem1, elem2);
1432 : :
1433 : : /* It is possible that const_binop cannot handle the given
1434 : : code and return NULL_TREE */
1435 : 521385 : if (elt == NULL_TREE)
1436 : 331 : return NULL_TREE;
1437 : 521054 : elts.quick_push (elt);
1438 : : }
1439 : :
1440 : 176543 : return elts.build ();
1441 : 176874 : }
1442 : :
1443 : 87183 : if (TREE_CODE (arg1) == VECTOR_CST
1444 : 6923 : && TREE_CODE (arg2) == INTEGER_CST)
1445 : : {
1446 : 6923 : tree type = TREE_TYPE (arg1);
1447 : 6923 : bool step_ok_p = distributes_over_addition_p (code, 1);
1448 : 6923 : tree_vector_builder elts;
1449 : 6923 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1450 : : return NULL_TREE;
1451 : 6923 : unsigned int count = elts.encoded_nelts ();
1452 : 31040 : for (unsigned int i = 0; i < count; ++i)
1453 : : {
1454 : 24204 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1455 : :
1456 : 24204 : tree elt = elt_const_binop (code, elem1, arg2);
1457 : :
1458 : : /* It is possible that const_binop cannot handle the given
1459 : : code and return NULL_TREE. */
1460 : 24204 : if (elt == NULL_TREE)
1461 : 87 : return NULL_TREE;
1462 : 24117 : elts.quick_push (elt);
1463 : : }
1464 : :
1465 : 6836 : return elts.build ();
1466 : 6923 : }
1467 : : return NULL_TREE;
1468 : : }
1469 : :
1470 : : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1471 : : constant. We assume ARG1 and ARG2 have the same data type, or at least
1472 : : are the same kind of constant and the same machine mode. Return zero if
1473 : : combining the constants is not allowed in the current operating mode. */
1474 : :
1475 : : static tree
1476 : 175329871 : const_binop (enum tree_code code, tree arg1, tree arg2)
1477 : : {
1478 : : /* Sanity check for the recursive cases. */
1479 : 175329871 : if (!arg1 || !arg2)
1480 : : return NULL_TREE;
1481 : :
1482 : 175328607 : STRIP_NOPS (arg1);
1483 : 175328607 : STRIP_NOPS (arg2);
1484 : :
1485 : 175328607 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1486 : : {
1487 : 169522834 : if (code == POINTER_PLUS_EXPR)
1488 : 104151 : return int_const_binop (PLUS_EXPR,
1489 : 208302 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1490 : :
1491 : 169418683 : return int_const_binop (code, arg1, arg2);
1492 : : }
1493 : :
1494 : 5805773 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1495 : : {
1496 : 5525833 : machine_mode mode;
1497 : 5525833 : REAL_VALUE_TYPE d1;
1498 : 5525833 : REAL_VALUE_TYPE d2;
1499 : 5525833 : REAL_VALUE_TYPE value;
1500 : 5525833 : REAL_VALUE_TYPE result;
1501 : 5525833 : bool inexact;
1502 : 5525833 : tree t, type;
1503 : :
1504 : : /* The following codes are handled by real_arithmetic. */
1505 : 5525833 : switch (code)
1506 : : {
1507 : 5525833 : case PLUS_EXPR:
1508 : 5525833 : case MINUS_EXPR:
1509 : 5525833 : case MULT_EXPR:
1510 : 5525833 : case RDIV_EXPR:
1511 : 5525833 : case MIN_EXPR:
1512 : 5525833 : case MAX_EXPR:
1513 : 5525833 : break;
1514 : :
1515 : : default:
1516 : : return NULL_TREE;
1517 : : }
1518 : :
1519 : 5525833 : d1 = TREE_REAL_CST (arg1);
1520 : 5525833 : d2 = TREE_REAL_CST (arg2);
1521 : :
1522 : 5525833 : type = TREE_TYPE (arg1);
1523 : 5525833 : mode = TYPE_MODE (type);
1524 : :
1525 : : /* Don't perform operation if we honor signaling NaNs and
1526 : : either operand is a signaling NaN. */
1527 : 5525833 : if (HONOR_SNANS (mode)
1528 : 5525833 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1529 : 6949 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1530 : 33 : return NULL_TREE;
1531 : :
1532 : : /* Don't perform operation if it would raise a division
1533 : : by zero exception. */
1534 : 5525800 : if (code == RDIV_EXPR
1535 : 2478741 : && real_equal (&d2, &dconst0)
1536 : 5536535 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1537 : 7447 : return NULL_TREE;
1538 : :
1539 : : /* If either operand is a NaN, just return it. Otherwise, set up
1540 : : for floating-point trap; we return an overflow. */
1541 : 5518353 : if (REAL_VALUE_ISNAN (d1))
1542 : : {
1543 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1544 : : is off. */
1545 : 239 : d1.signalling = 0;
1546 : 239 : t = build_real (type, d1);
1547 : 239 : return t;
1548 : : }
1549 : 5518114 : else if (REAL_VALUE_ISNAN (d2))
1550 : : {
1551 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1552 : : is off. */
1553 : 61 : d2.signalling = 0;
1554 : 61 : t = build_real (type, d2);
1555 : 61 : return t;
1556 : : }
1557 : :
1558 : 5518053 : inexact = real_arithmetic (&value, code, &d1, &d2);
1559 : 5518053 : real_convert (&result, mode, &value);
1560 : :
1561 : : /* Don't constant fold this floating point operation if
1562 : : both operands are not NaN but the result is NaN, and
1563 : : flag_trapping_math. Such operations should raise an
1564 : : invalid operation exception. */
1565 : 5518053 : if (flag_trapping_math
1566 : 21438802 : && MODE_HAS_NANS (mode)
1567 : 5501246 : && REAL_VALUE_ISNAN (result)
1568 : 2504 : && !REAL_VALUE_ISNAN (d1)
1569 : 5520557 : && !REAL_VALUE_ISNAN (d2))
1570 : 2504 : return NULL_TREE;
1571 : :
1572 : : /* Don't constant fold this floating point operation if
1573 : : the result has overflowed and flag_trapping_math. */
1574 : 5515549 : if (flag_trapping_math
1575 : 21429128 : && MODE_HAS_INFINITIES (mode)
1576 : 5498742 : && REAL_VALUE_ISINF (result)
1577 : 7497 : && !REAL_VALUE_ISINF (d1)
1578 : 5522463 : && !REAL_VALUE_ISINF (d2))
1579 : 4631 : return NULL_TREE;
1580 : :
1581 : : /* Don't constant fold this floating point operation if the
1582 : : result may dependent upon the run-time rounding mode and
1583 : : flag_rounding_math is set, or if GCC's software emulation
1584 : : is unable to accurately represent the result. */
1585 : 5510918 : if ((flag_rounding_math
1586 : 37430613 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1587 : 5510918 : && (inexact || !real_identical (&result, &value)))
1588 : 1107 : return NULL_TREE;
1589 : :
1590 : 5509811 : t = build_real (type, result);
1591 : :
1592 : 5509811 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1593 : 5509811 : return t;
1594 : : }
1595 : :
1596 : 279940 : if (TREE_CODE (arg1) == FIXED_CST)
1597 : : {
1598 : 0 : FIXED_VALUE_TYPE f1;
1599 : 0 : FIXED_VALUE_TYPE f2;
1600 : 0 : FIXED_VALUE_TYPE result;
1601 : 0 : tree t, type;
1602 : 0 : bool sat_p;
1603 : 0 : bool overflow_p;
1604 : :
1605 : : /* The following codes are handled by fixed_arithmetic. */
1606 : 0 : switch (code)
1607 : : {
1608 : 0 : case PLUS_EXPR:
1609 : 0 : case MINUS_EXPR:
1610 : 0 : case MULT_EXPR:
1611 : 0 : case TRUNC_DIV_EXPR:
1612 : 0 : if (TREE_CODE (arg2) != FIXED_CST)
1613 : : return NULL_TREE;
1614 : 0 : f2 = TREE_FIXED_CST (arg2);
1615 : 0 : break;
1616 : :
1617 : 0 : case LSHIFT_EXPR:
1618 : 0 : case RSHIFT_EXPR:
1619 : 0 : {
1620 : 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1621 : 0 : return NULL_TREE;
1622 : 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1623 : 0 : f2.data.high = w2.elt (1);
1624 : 0 : f2.data.low = w2.ulow ();
1625 : 0 : f2.mode = SImode;
1626 : : }
1627 : 0 : break;
1628 : :
1629 : : default:
1630 : : return NULL_TREE;
1631 : : }
1632 : :
1633 : 0 : f1 = TREE_FIXED_CST (arg1);
1634 : 0 : type = TREE_TYPE (arg1);
1635 : 0 : sat_p = TYPE_SATURATING (type);
1636 : 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1637 : 0 : t = build_fixed (type, result);
1638 : : /* Propagate overflow flags. */
1639 : 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1640 : 0 : TREE_OVERFLOW (t) = 1;
1641 : 0 : return t;
1642 : : }
1643 : :
1644 : 279940 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1645 : : {
1646 : 11243 : tree type = TREE_TYPE (arg1);
1647 : 11243 : tree r1 = TREE_REALPART (arg1);
1648 : 11243 : tree i1 = TREE_IMAGPART (arg1);
1649 : 11243 : tree r2 = TREE_REALPART (arg2);
1650 : 11243 : tree i2 = TREE_IMAGPART (arg2);
1651 : 11243 : tree real, imag;
1652 : :
1653 : 11243 : switch (code)
1654 : : {
1655 : 5355 : case PLUS_EXPR:
1656 : 5355 : case MINUS_EXPR:
1657 : 5355 : real = const_binop (code, r1, r2);
1658 : 5355 : imag = const_binop (code, i1, i2);
1659 : 5355 : break;
1660 : :
1661 : 3927 : case MULT_EXPR:
1662 : 3927 : if (COMPLEX_FLOAT_TYPE_P (type))
1663 : 2775 : return do_mpc_arg2 (arg1, arg2, type,
1664 : : /* do_nonfinite= */ folding_initializer,
1665 : 2775 : mpc_mul);
1666 : :
1667 : 1152 : real = const_binop (MINUS_EXPR,
1668 : : const_binop (MULT_EXPR, r1, r2),
1669 : : const_binop (MULT_EXPR, i1, i2));
1670 : 1152 : imag = const_binop (PLUS_EXPR,
1671 : : const_binop (MULT_EXPR, r1, i2),
1672 : : const_binop (MULT_EXPR, i1, r2));
1673 : 1152 : break;
1674 : :
1675 : 1707 : case RDIV_EXPR:
1676 : 1707 : if (COMPLEX_FLOAT_TYPE_P (type))
1677 : 1707 : return do_mpc_arg2 (arg1, arg2, type,
1678 : : /* do_nonfinite= */ folding_initializer,
1679 : 1707 : mpc_div);
1680 : : /* Fallthru. */
1681 : 254 : case TRUNC_DIV_EXPR:
1682 : 254 : case CEIL_DIV_EXPR:
1683 : 254 : case FLOOR_DIV_EXPR:
1684 : 254 : case ROUND_DIV_EXPR:
1685 : 254 : if (flag_complex_method == 0)
1686 : : {
1687 : : /* Keep this algorithm in sync with
1688 : : tree-complex.cc:expand_complex_div_straight().
1689 : :
1690 : : Expand complex division to scalars, straightforward algorithm.
1691 : : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1692 : : t = br*br + bi*bi
1693 : : */
1694 : 0 : tree magsquared
1695 : 0 : = const_binop (PLUS_EXPR,
1696 : : const_binop (MULT_EXPR, r2, r2),
1697 : : const_binop (MULT_EXPR, i2, i2));
1698 : 0 : tree t1
1699 : 0 : = const_binop (PLUS_EXPR,
1700 : : const_binop (MULT_EXPR, r1, r2),
1701 : : const_binop (MULT_EXPR, i1, i2));
1702 : 0 : tree t2
1703 : 0 : = const_binop (MINUS_EXPR,
1704 : : const_binop (MULT_EXPR, i1, r2),
1705 : : const_binop (MULT_EXPR, r1, i2));
1706 : :
1707 : 0 : real = const_binop (code, t1, magsquared);
1708 : 0 : imag = const_binop (code, t2, magsquared);
1709 : : }
1710 : : else
1711 : : {
1712 : : /* Keep this algorithm in sync with
1713 : : tree-complex.cc:expand_complex_div_wide().
1714 : :
1715 : : Expand complex division to scalars, modified algorithm to minimize
1716 : : overflow with wide input ranges. */
1717 : 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1718 : : fold_abs_const (r2, TREE_TYPE (type)),
1719 : : fold_abs_const (i2, TREE_TYPE (type)));
1720 : :
1721 : 254 : if (integer_nonzerop (compare))
1722 : : {
1723 : : /* In the TRUE branch, we compute
1724 : : ratio = br/bi;
1725 : : div = (br * ratio) + bi;
1726 : : tr = (ar * ratio) + ai;
1727 : : ti = (ai * ratio) - ar;
1728 : : tr = tr / div;
1729 : : ti = ti / div; */
1730 : 48 : tree ratio = const_binop (code, r2, i2);
1731 : 48 : tree div = const_binop (PLUS_EXPR, i2,
1732 : : const_binop (MULT_EXPR, r2, ratio));
1733 : 48 : real = const_binop (MULT_EXPR, r1, ratio);
1734 : 48 : real = const_binop (PLUS_EXPR, real, i1);
1735 : 48 : real = const_binop (code, real, div);
1736 : :
1737 : 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1738 : 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1739 : 48 : imag = const_binop (code, imag, div);
1740 : : }
1741 : : else
1742 : : {
1743 : : /* In the FALSE branch, we compute
1744 : : ratio = d/c;
1745 : : divisor = (d * ratio) + c;
1746 : : tr = (b * ratio) + a;
1747 : : ti = b - (a * ratio);
1748 : : tr = tr / div;
1749 : : ti = ti / div; */
1750 : 206 : tree ratio = const_binop (code, i2, r2);
1751 : 206 : tree div = const_binop (PLUS_EXPR, r2,
1752 : : const_binop (MULT_EXPR, i2, ratio));
1753 : :
1754 : 206 : real = const_binop (MULT_EXPR, i1, ratio);
1755 : 206 : real = const_binop (PLUS_EXPR, real, r1);
1756 : 206 : real = const_binop (code, real, div);
1757 : :
1758 : 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1759 : 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1760 : 206 : imag = const_binop (code, imag, div);
1761 : : }
1762 : : }
1763 : : break;
1764 : :
1765 : : default:
1766 : : return NULL_TREE;
1767 : : }
1768 : :
1769 : 6761 : if (real && imag)
1770 : 6603 : return build_complex (type, real, imag);
1771 : : }
1772 : :
1773 : 268855 : tree simplified;
1774 : 268855 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1775 : : return simplified;
1776 : :
1777 : 268493 : if (commutative_tree_code (code)
1778 : 268493 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1779 : : return simplified;
1780 : :
1781 : 264057 : return vector_const_binop (code, arg1, arg2, const_binop);
1782 : : }
1783 : :
1784 : : /* Overload that adds a TYPE parameter to be able to dispatch
1785 : : to fold_relational_const. */
1786 : :
1787 : : tree
1788 : 226916423 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1789 : : {
1790 : 226916423 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1791 : 59064986 : return fold_relational_const (code, type, arg1, arg2);
1792 : :
1793 : : /* ??? Until we make the const_binop worker take the type of the
1794 : : result as argument put those cases that need it here. */
1795 : 167851437 : switch (code)
1796 : : {
1797 : 18 : case VEC_SERIES_EXPR:
1798 : 18 : if (CONSTANT_CLASS_P (arg1)
1799 : 18 : && CONSTANT_CLASS_P (arg2))
1800 : 18 : return build_vec_series (type, arg1, arg2);
1801 : : return NULL_TREE;
1802 : :
1803 : 267734 : case COMPLEX_EXPR:
1804 : 267734 : if ((TREE_CODE (arg1) == REAL_CST
1805 : 257228 : && TREE_CODE (arg2) == REAL_CST)
1806 : 10508 : || (TREE_CODE (arg1) == INTEGER_CST
1807 : 10506 : && TREE_CODE (arg2) == INTEGER_CST))
1808 : 267732 : return build_complex (type, arg1, arg2);
1809 : : return NULL_TREE;
1810 : :
1811 : 15643 : case POINTER_DIFF_EXPR:
1812 : 15643 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1813 : : {
1814 : 30102 : poly_offset_int res = (wi::to_poly_offset (arg1)
1815 : 15051 : - wi::to_poly_offset (arg2));
1816 : 15051 : return force_fit_type (type, res, 1,
1817 : 15051 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1818 : : }
1819 : : return NULL_TREE;
1820 : :
1821 : 14447 : case VEC_PACK_TRUNC_EXPR:
1822 : 14447 : case VEC_PACK_FIX_TRUNC_EXPR:
1823 : 14447 : case VEC_PACK_FLOAT_EXPR:
1824 : 14447 : {
1825 : 14447 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1826 : :
1827 : 14447 : if (TREE_CODE (arg1) != VECTOR_CST
1828 : 14447 : || TREE_CODE (arg2) != VECTOR_CST)
1829 : : return NULL_TREE;
1830 : :
1831 : 14447 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1832 : : return NULL_TREE;
1833 : :
1834 : 14447 : out_nelts = in_nelts * 2;
1835 : 14447 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1836 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1837 : :
1838 : 14447 : tree_vector_builder elts (type, out_nelts, 1);
1839 : 187011 : for (i = 0; i < out_nelts; i++)
1840 : : {
1841 : 172576 : tree elt = (i < in_nelts
1842 : 172576 : ? VECTOR_CST_ELT (arg1, i)
1843 : 86282 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1844 : 173620 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1845 : : ? NOP_EXPR
1846 : : : code == VEC_PACK_FLOAT_EXPR
1847 : 1044 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1848 : 172576 : TREE_TYPE (type), elt);
1849 : 172576 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1850 : 12 : return NULL_TREE;
1851 : 172564 : elts.quick_push (elt);
1852 : : }
1853 : :
1854 : 14435 : return elts.build ();
1855 : 14447 : }
1856 : :
1857 : 198 : case VEC_WIDEN_MULT_LO_EXPR:
1858 : 198 : case VEC_WIDEN_MULT_HI_EXPR:
1859 : 198 : case VEC_WIDEN_MULT_EVEN_EXPR:
1860 : 198 : case VEC_WIDEN_MULT_ODD_EXPR:
1861 : 198 : {
1862 : 198 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1863 : :
1864 : 198 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1865 : : return NULL_TREE;
1866 : :
1867 : 198 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1868 : : return NULL_TREE;
1869 : 198 : out_nelts = in_nelts / 2;
1870 : 198 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1871 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1872 : :
1873 : 198 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1874 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1875 : : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1876 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1877 : : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1878 : : scale = 1, ofs = 0;
1879 : : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1880 : 198 : scale = 1, ofs = 1;
1881 : :
1882 : 198 : tree_vector_builder elts (type, out_nelts, 1);
1883 : 698 : for (out = 0; out < out_nelts; out++)
1884 : : {
1885 : 500 : unsigned int in = (out << scale) + ofs;
1886 : 500 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1887 : : VECTOR_CST_ELT (arg1, in));
1888 : 500 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1889 : : VECTOR_CST_ELT (arg2, in));
1890 : :
1891 : 500 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1892 : 0 : return NULL_TREE;
1893 : 500 : tree elt = const_binop (MULT_EXPR, t1, t2);
1894 : 500 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1895 : : return NULL_TREE;
1896 : 500 : elts.quick_push (elt);
1897 : : }
1898 : :
1899 : 198 : return elts.build ();
1900 : 198 : }
1901 : :
1902 : 167553397 : default:;
1903 : : }
1904 : :
1905 : 167553397 : if (TREE_CODE_CLASS (code) != tcc_binary)
1906 : : return NULL_TREE;
1907 : :
1908 : : /* Make sure type and arg0 have the same saturating flag. */
1909 : 164851578 : gcc_checking_assert (TYPE_SATURATING (type)
1910 : : == TYPE_SATURATING (TREE_TYPE (arg1)));
1911 : :
1912 : 164851578 : return const_binop (code, arg1, arg2);
1913 : : }
1914 : :
1915 : : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1916 : : Return zero if computing the constants is not possible. */
1917 : :
1918 : : tree
1919 : 287769234 : const_unop (enum tree_code code, tree type, tree arg0)
1920 : : {
1921 : : /* Don't perform the operation, other than NEGATE and ABS, if
1922 : : flag_signaling_nans is on and the operand is a signaling NaN. */
1923 : 287769234 : if (TREE_CODE (arg0) == REAL_CST
1924 : 11144678 : && HONOR_SNANS (arg0)
1925 : 17129 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1926 : 4740 : && code != NEGATE_EXPR
1927 : 4740 : && code != ABS_EXPR
1928 : 287773939 : && code != ABSU_EXPR)
1929 : : return NULL_TREE;
1930 : :
1931 : 287764529 : switch (code)
1932 : : {
1933 : 201874334 : CASE_CONVERT:
1934 : 201874334 : case FLOAT_EXPR:
1935 : 201874334 : case FIX_TRUNC_EXPR:
1936 : 201874334 : case FIXED_CONVERT_EXPR:
1937 : 201874334 : return fold_convert_const (code, type, arg0);
1938 : :
1939 : 0 : case ADDR_SPACE_CONVERT_EXPR:
1940 : : /* If the source address is 0, and the source address space
1941 : : cannot have a valid object at 0, fold to dest type null. */
1942 : 0 : if (integer_zerop (arg0)
1943 : 0 : && !(targetm.addr_space.zero_address_valid
1944 : 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1945 : 0 : return fold_convert_const (code, type, arg0);
1946 : : break;
1947 : :
1948 : 11639029 : case VIEW_CONVERT_EXPR:
1949 : 11639029 : return fold_view_convert_expr (type, arg0);
1950 : :
1951 : 30587734 : case NEGATE_EXPR:
1952 : 30587734 : {
1953 : : /* Can't call fold_negate_const directly here as that doesn't
1954 : : handle all cases and we might not be able to negate some
1955 : : constants. */
1956 : 30587734 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1957 : 30587734 : if (tem && CONSTANT_CLASS_P (tem))
1958 : : return tem;
1959 : : break;
1960 : : }
1961 : :
1962 : 34114 : case ABS_EXPR:
1963 : 34114 : case ABSU_EXPR:
1964 : 34114 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1965 : 33825 : return fold_abs_const (arg0, type);
1966 : : break;
1967 : :
1968 : 24552 : case CONJ_EXPR:
1969 : 24552 : if (TREE_CODE (arg0) == COMPLEX_CST)
1970 : : {
1971 : 24549 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1972 : 24549 : TREE_TYPE (type));
1973 : 24549 : return build_complex (type, TREE_REALPART (arg0), ipart);
1974 : : }
1975 : : break;
1976 : :
1977 : 2288289 : case BIT_NOT_EXPR:
1978 : 2288289 : if (TREE_CODE (arg0) == INTEGER_CST)
1979 : 2287145 : return fold_not_const (arg0, type);
1980 : 1144 : else if (POLY_INT_CST_P (arg0))
1981 : : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1982 : : /* Perform BIT_NOT_EXPR on each element individually. */
1983 : 1144 : else if (TREE_CODE (arg0) == VECTOR_CST)
1984 : : {
1985 : 539 : tree elem;
1986 : :
1987 : : /* This can cope with stepped encodings because ~x == -1 - x. */
1988 : 539 : tree_vector_builder elements;
1989 : 539 : elements.new_unary_operation (type, arg0, true);
1990 : 539 : unsigned int i, count = elements.encoded_nelts ();
1991 : 2230 : for (i = 0; i < count; ++i)
1992 : : {
1993 : 1691 : elem = VECTOR_CST_ELT (arg0, i);
1994 : 1691 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1995 : 1691 : if (elem == NULL_TREE)
1996 : : break;
1997 : 1691 : elements.quick_push (elem);
1998 : : }
1999 : 539 : if (i == count)
2000 : 539 : return elements.build ();
2001 : 539 : }
2002 : : break;
2003 : :
2004 : 7820610 : case TRUTH_NOT_EXPR:
2005 : 7820610 : if (TREE_CODE (arg0) == INTEGER_CST)
2006 : 7487065 : return constant_boolean_node (integer_zerop (arg0), type);
2007 : : break;
2008 : :
2009 : 179290 : case REALPART_EXPR:
2010 : 179290 : if (TREE_CODE (arg0) == COMPLEX_CST)
2011 : 179089 : return fold_convert (type, TREE_REALPART (arg0));
2012 : : break;
2013 : :
2014 : 183152 : case IMAGPART_EXPR:
2015 : 183152 : if (TREE_CODE (arg0) == COMPLEX_CST)
2016 : 182964 : return fold_convert (type, TREE_IMAGPART (arg0));
2017 : : break;
2018 : :
2019 : 18712 : case VEC_UNPACK_LO_EXPR:
2020 : 18712 : case VEC_UNPACK_HI_EXPR:
2021 : 18712 : case VEC_UNPACK_FLOAT_LO_EXPR:
2022 : 18712 : case VEC_UNPACK_FLOAT_HI_EXPR:
2023 : 18712 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2024 : 18712 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2025 : 18712 : {
2026 : 18712 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2027 : 18712 : enum tree_code subcode;
2028 : :
2029 : 18712 : if (TREE_CODE (arg0) != VECTOR_CST)
2030 : : return NULL_TREE;
2031 : :
2032 : 18712 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2033 : : return NULL_TREE;
2034 : 18712 : out_nelts = in_nelts / 2;
2035 : 18712 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2036 : :
2037 : 18712 : unsigned int offset = 0;
2038 : 18712 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2039 : 18712 : || code == VEC_UNPACK_FLOAT_LO_EXPR
2040 : : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2041 : 9347 : offset = out_nelts;
2042 : :
2043 : 18712 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
2044 : : subcode = NOP_EXPR;
2045 : 8040 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
2046 : 8040 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
2047 : : subcode = FLOAT_EXPR;
2048 : : else
2049 : 4 : subcode = FIX_TRUNC_EXPR;
2050 : :
2051 : 18712 : tree_vector_builder elts (type, out_nelts, 1);
2052 : 100566 : for (i = 0; i < out_nelts; i++)
2053 : : {
2054 : 81854 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2055 : 81854 : VECTOR_CST_ELT (arg0, i + offset));
2056 : 81854 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2057 : 0 : return NULL_TREE;
2058 : 81854 : elts.quick_push (elt);
2059 : : }
2060 : :
2061 : 18712 : return elts.build ();
2062 : 18712 : }
2063 : :
2064 : 4 : case VEC_DUPLICATE_EXPR:
2065 : 4 : if (CONSTANT_CLASS_P (arg0))
2066 : 4 : return build_vector_from_val (type, arg0);
2067 : : return NULL_TREE;
2068 : :
2069 : : default:
2070 : : break;
2071 : : }
2072 : :
2073 : : return NULL_TREE;
2074 : : }
2075 : :
2076 : : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2077 : : indicates which particular sizetype to create. */
2078 : :
2079 : : tree
2080 : 3173028963 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2081 : : {
2082 : 3173028963 : return build_int_cst (sizetype_tab[(int) kind], number);
2083 : : }
2084 : :
2085 : : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2086 : : is a tree code. The type of the result is taken from the operands.
2087 : : Both must be equivalent integer types, ala int_binop_types_match_p.
2088 : : If the operands are constant, so is the result. */
2089 : :
2090 : : tree
2091 : 2212177846 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2092 : : {
2093 : 2212177846 : tree type = TREE_TYPE (arg0);
2094 : :
2095 : 2212177846 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2096 : : return error_mark_node;
2097 : :
2098 : 2212177846 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2099 : : TREE_TYPE (arg1)));
2100 : :
2101 : : /* Handle the special case of two poly_int constants faster. */
2102 : 2212177846 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2103 : : {
2104 : : /* And some specific cases even faster than that. */
2105 : 2180294881 : if (code == PLUS_EXPR)
2106 : : {
2107 : 996103255 : if (integer_zerop (arg0)
2108 : 996103255 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2109 : : return arg1;
2110 : 274744264 : if (integer_zerop (arg1)
2111 : 274744264 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2112 : : return arg0;
2113 : : }
2114 : 1184191626 : else if (code == MINUS_EXPR)
2115 : : {
2116 : 109388904 : if (integer_zerop (arg1)
2117 : 109388904 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2118 : : return arg0;
2119 : : }
2120 : 1074802722 : else if (code == MULT_EXPR)
2121 : : {
2122 : 497038682 : if (integer_onep (arg0)
2123 : 497038682 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2124 : : return arg1;
2125 : : }
2126 : :
2127 : : /* Handle general case of two integer constants. For sizetype
2128 : : constant calculations we always want to know about overflow,
2129 : : even in the unsigned case. */
2130 : 1067407921 : tree res = int_const_binop (code, arg0, arg1, -1);
2131 : 1067407921 : if (res != NULL_TREE)
2132 : : return res;
2133 : : }
2134 : :
2135 : 31882965 : return fold_build2_loc (loc, code, type, arg0, arg1);
2136 : : }
2137 : :
2138 : : /* Given two values, either both of sizetype or both of bitsizetype,
2139 : : compute the difference between the two values. Return the value
2140 : : in signed type corresponding to the type of the operands. */
2141 : :
2142 : : tree
2143 : 34954084 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2144 : : {
2145 : 34954084 : tree type = TREE_TYPE (arg0);
2146 : 34954084 : tree ctype;
2147 : :
2148 : 34954084 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2149 : : TREE_TYPE (arg1)));
2150 : :
2151 : : /* If the type is already signed, just do the simple thing. */
2152 : 34954084 : if (!TYPE_UNSIGNED (type))
2153 : 9869415 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2154 : :
2155 : 25084669 : if (type == sizetype)
2156 : 25084669 : ctype = ssizetype;
2157 : 0 : else if (type == bitsizetype)
2158 : 0 : ctype = sbitsizetype;
2159 : : else
2160 : 0 : ctype = signed_type_for (type);
2161 : :
2162 : : /* If either operand is not a constant, do the conversions to the signed
2163 : : type and subtract. The hardware will do the right thing with any
2164 : : overflow in the subtraction. */
2165 : 25084669 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2166 : 18018 : return size_binop_loc (loc, MINUS_EXPR,
2167 : : fold_convert_loc (loc, ctype, arg0),
2168 : 18018 : fold_convert_loc (loc, ctype, arg1));
2169 : :
2170 : : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2171 : : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2172 : : overflow) and negate (which can't either). Special-case a result
2173 : : of zero while we're here. */
2174 : 25066651 : if (tree_int_cst_equal (arg0, arg1))
2175 : 21786171 : return build_int_cst (ctype, 0);
2176 : 3280480 : else if (tree_int_cst_lt (arg1, arg0))
2177 : 2117609 : return fold_convert_loc (loc, ctype,
2178 : 2117609 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2179 : : else
2180 : 1162871 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2181 : : fold_convert_loc (loc, ctype,
2182 : : size_binop_loc (loc,
2183 : : MINUS_EXPR,
2184 : : arg1, arg0)));
2185 : : }
2186 : :
2187 : : /* Convert integer constant ARG1 to TYPE, which is an integral or offset
2188 : : or pointer type. */
2189 : :
2190 : : tree
2191 : 1162423468 : int_const_convert (tree type, const_tree arg1, int overflowable)
2192 : : {
2193 : : /* Given an integer constant, make new constant with new type,
2194 : : appropriately sign-extended or truncated. Use widest_int
2195 : : so that any extension is done according ARG1's type. */
2196 : 1162423468 : tree arg1_type = TREE_TYPE (arg1);
2197 : 1162423468 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2198 : 1162423468 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2199 : 1162423468 : TYPE_SIGN (arg1_type)),
2200 : : overflowable,
2201 : 1162423468 : TREE_OVERFLOW (arg1));
2202 : : }
2203 : :
2204 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2205 : : to an integer type. */
2206 : :
2207 : : static tree
2208 : 39523 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2209 : : {
2210 : 39523 : bool overflow = false;
2211 : 39523 : tree t;
2212 : :
2213 : : /* The following code implements the floating point to integer
2214 : : conversion rules required by the Java Language Specification,
2215 : : that IEEE NaNs are mapped to zero and values that overflow
2216 : : the target precision saturate, i.e. values greater than
2217 : : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2218 : : are mapped to INT_MIN. These semantics are allowed by the
2219 : : C and C++ standards that simply state that the behavior of
2220 : : FP-to-integer conversion is unspecified upon overflow. */
2221 : :
2222 : 39523 : wide_int val;
2223 : 39523 : REAL_VALUE_TYPE r;
2224 : 39523 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2225 : :
2226 : 39523 : switch (code)
2227 : : {
2228 : 39523 : case FIX_TRUNC_EXPR:
2229 : 39523 : real_trunc (&r, VOIDmode, &x);
2230 : 39523 : break;
2231 : :
2232 : 0 : default:
2233 : 0 : gcc_unreachable ();
2234 : : }
2235 : :
2236 : : /* If R is NaN, return zero and show we have an overflow. */
2237 : 39523 : if (REAL_VALUE_ISNAN (r))
2238 : : {
2239 : 3638 : overflow = true;
2240 : 3638 : val = wi::zero (TYPE_PRECISION (type));
2241 : : }
2242 : :
2243 : : /* See if R is less than the lower bound or greater than the
2244 : : upper bound. */
2245 : :
2246 : 39523 : if (! overflow)
2247 : : {
2248 : 35885 : tree lt = TYPE_MIN_VALUE (type);
2249 : 35885 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2250 : 35885 : if (real_less (&r, &l))
2251 : : {
2252 : 1974 : overflow = true;
2253 : 1974 : val = wi::to_wide (lt);
2254 : : }
2255 : : }
2256 : :
2257 : 39523 : if (! overflow)
2258 : : {
2259 : 33911 : tree ut = TYPE_MAX_VALUE (type);
2260 : 33911 : if (ut)
2261 : : {
2262 : 33911 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2263 : 33911 : if (real_less (&u, &r))
2264 : : {
2265 : 1921 : overflow = true;
2266 : 1921 : val = wi::to_wide (ut);
2267 : : }
2268 : : }
2269 : : }
2270 : :
2271 : 39523 : if (! overflow)
2272 : 31992 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2273 : :
2274 : : /* According to IEEE standard, for conversions from floating point to
2275 : : integer. When a NaN or infinite operand cannot be represented in the
2276 : : destination format and this cannot otherwise be indicated, the invalid
2277 : : operation exception shall be signaled. When a numeric operand would
2278 : : convert to an integer outside the range of the destination format, the
2279 : : invalid operation exception shall be signaled if this situation cannot
2280 : : otherwise be indicated. */
2281 : 39523 : if (!flag_trapping_math || !overflow)
2282 : 32246 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2283 : : else
2284 : : t = NULL_TREE;
2285 : :
2286 : 39523 : return t;
2287 : 39523 : }
2288 : :
2289 : : /* A subroutine of fold_convert_const handling conversions of a
2290 : : FIXED_CST to an integer type. */
2291 : :
2292 : : static tree
2293 : 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2294 : : {
2295 : 0 : tree t;
2296 : 0 : double_int temp, temp_trunc;
2297 : 0 : scalar_mode mode;
2298 : :
2299 : : /* Right shift FIXED_CST to temp by fbit. */
2300 : 0 : temp = TREE_FIXED_CST (arg1).data;
2301 : 0 : mode = TREE_FIXED_CST (arg1).mode;
2302 : 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2303 : : {
2304 : 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2305 : : HOST_BITS_PER_DOUBLE_INT,
2306 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2307 : :
2308 : : /* Left shift temp to temp_trunc by fbit. */
2309 : 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2310 : : HOST_BITS_PER_DOUBLE_INT,
2311 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2312 : : }
2313 : : else
2314 : : {
2315 : 0 : temp = double_int_zero;
2316 : 0 : temp_trunc = double_int_zero;
2317 : : }
2318 : :
2319 : : /* If FIXED_CST is negative, we need to round the value toward 0.
2320 : : By checking if the fractional bits are not zero to add 1 to temp. */
2321 : 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2322 : 0 : && temp_trunc.is_negative ()
2323 : 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2324 : 0 : temp += double_int_one;
2325 : :
2326 : : /* Given a fixed-point constant, make new constant with new type,
2327 : : appropriately sign-extended or truncated. */
2328 : 0 : t = force_fit_type (type, temp, -1,
2329 : 0 : (temp.is_negative ()
2330 : 0 : && (TYPE_UNSIGNED (type)
2331 : 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2332 : 0 : | TREE_OVERFLOW (arg1));
2333 : :
2334 : 0 : return t;
2335 : : }
2336 : :
2337 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2338 : : to another floating point type. */
2339 : :
2340 : : static tree
2341 : 2076301 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2342 : : {
2343 : 2076301 : REAL_VALUE_TYPE value;
2344 : 2076301 : tree t;
2345 : :
2346 : : /* If the underlying modes are the same, simply treat it as
2347 : : copy and rebuild with TREE_REAL_CST information and the
2348 : : given type. */
2349 : 2076301 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2350 : : {
2351 : 98234 : t = build_real (type, TREE_REAL_CST (arg1));
2352 : 98234 : return t;
2353 : : }
2354 : :
2355 : : /* Don't perform the operation if flag_signaling_nans is on
2356 : : and the operand is a signaling NaN. */
2357 : 1978067 : if (HONOR_SNANS (arg1)
2358 : 1979949 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2359 : : return NULL_TREE;
2360 : :
2361 : : /* With flag_rounding_math we should respect the current rounding mode
2362 : : unless the conversion is exact. */
2363 : 1978067 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2364 : 1978723 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2365 : 509 : return NULL_TREE;
2366 : :
2367 : 1977558 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2368 : 1977558 : t = build_real (type, value);
2369 : :
2370 : : /* If converting an infinity or NAN to a representation that doesn't
2371 : : have one, set the overflow bit so that we can produce some kind of
2372 : : error message at the appropriate point if necessary. It's not the
2373 : : most user-friendly message, but it's better than nothing. */
2374 : 1977558 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2375 : 2107301 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2376 : 0 : TREE_OVERFLOW (t) = 1;
2377 : 1977558 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2378 : 2103032 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2379 : 0 : TREE_OVERFLOW (t) = 1;
2380 : : /* Regular overflow, conversion produced an infinity in a mode that
2381 : : can't represent them. */
2382 : 9884396 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2383 : 0 : && REAL_VALUE_ISINF (value)
2384 : 1977558 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2385 : 0 : TREE_OVERFLOW (t) = 1;
2386 : : else
2387 : 1977558 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2388 : : return t;
2389 : : }
2390 : :
2391 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2392 : : to a floating point type. */
2393 : :
2394 : : static tree
2395 : 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2396 : : {
2397 : 0 : REAL_VALUE_TYPE value;
2398 : 0 : tree t;
2399 : :
2400 : 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2401 : 0 : &TREE_FIXED_CST (arg1));
2402 : 0 : t = build_real (type, value);
2403 : :
2404 : 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2405 : 0 : return t;
2406 : : }
2407 : :
2408 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2409 : : to another fixed-point type. */
2410 : :
2411 : : static tree
2412 : 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2413 : : {
2414 : 0 : FIXED_VALUE_TYPE value;
2415 : 0 : tree t;
2416 : 0 : bool overflow_p;
2417 : :
2418 : 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2419 : 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2420 : 0 : t = build_fixed (type, value);
2421 : :
2422 : : /* Propagate overflow flags. */
2423 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2424 : 0 : TREE_OVERFLOW (t) = 1;
2425 : 0 : return t;
2426 : : }
2427 : :
2428 : : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2429 : : to a fixed-point type. */
2430 : :
2431 : : static tree
2432 : 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2433 : : {
2434 : 0 : FIXED_VALUE_TYPE value;
2435 : 0 : tree t;
2436 : 0 : bool overflow_p;
2437 : 0 : double_int di;
2438 : :
2439 : 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2440 : :
2441 : 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2442 : 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2443 : 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2444 : : else
2445 : 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2446 : :
2447 : 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2448 : 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2449 : 0 : TYPE_SATURATING (type));
2450 : 0 : t = build_fixed (type, value);
2451 : :
2452 : : /* Propagate overflow flags. */
2453 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2454 : 0 : TREE_OVERFLOW (t) = 1;
2455 : 0 : return t;
2456 : : }
2457 : :
2458 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2459 : : to a fixed-point type. */
2460 : :
2461 : : static tree
2462 : 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2463 : : {
2464 : 0 : FIXED_VALUE_TYPE value;
2465 : 0 : tree t;
2466 : 0 : bool overflow_p;
2467 : :
2468 : 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2469 : 0 : &TREE_REAL_CST (arg1),
2470 : 0 : TYPE_SATURATING (type));
2471 : 0 : t = build_fixed (type, value);
2472 : :
2473 : : /* Propagate overflow flags. */
2474 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2475 : 0 : TREE_OVERFLOW (t) = 1;
2476 : 0 : return t;
2477 : : }
2478 : :
2479 : : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2480 : : type TYPE. If no simplification can be done return NULL_TREE. */
2481 : :
2482 : : static tree
2483 : 1223401683 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2484 : : {
2485 : 1223401683 : tree arg_type = TREE_TYPE (arg1);
2486 : 1223401683 : if (arg_type == type)
2487 : : return arg1;
2488 : :
2489 : : /* We can't widen types, since the runtime value could overflow the
2490 : : original type before being extended to the new type. */
2491 : 1212308403 : if (POLY_INT_CST_P (arg1)
2492 : : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2493 : : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2494 : : return build_poly_int_cst (type,
2495 : : poly_wide_int::from (poly_int_cst_value (arg1),
2496 : : TYPE_PRECISION (type),
2497 : : TYPE_SIGN (arg_type)));
2498 : :
2499 : 1212308403 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2500 : : || TREE_CODE (type) == OFFSET_TYPE)
2501 : : {
2502 : 1180312682 : if (TREE_CODE (arg1) == INTEGER_CST)
2503 : 1162423468 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2504 : 17889214 : else if (TREE_CODE (arg1) == REAL_CST)
2505 : 39523 : return fold_convert_const_int_from_real (code, type, arg1);
2506 : 17849691 : else if (TREE_CODE (arg1) == FIXED_CST)
2507 : 0 : return fold_convert_const_int_from_fixed (type, arg1);
2508 : : }
2509 : : else if (SCALAR_FLOAT_TYPE_P (type))
2510 : : {
2511 : 31941132 : if (TREE_CODE (arg1) == INTEGER_CST)
2512 : : {
2513 : 24482290 : tree res = build_real_from_int_cst (type, arg1);
2514 : : /* Avoid the folding if flag_rounding_math is on and the
2515 : : conversion is not exact. */
2516 : 24482290 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2517 : : {
2518 : 2882 : bool fail = false;
2519 : 5764 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2520 : 2882 : TYPE_PRECISION (TREE_TYPE (arg1)));
2521 : 2882 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2522 : 1726 : return NULL_TREE;
2523 : 2882 : }
2524 : 24480564 : return res;
2525 : : }
2526 : 7458842 : else if (TREE_CODE (arg1) == REAL_CST)
2527 : 2076301 : return fold_convert_const_real_from_real (type, arg1);
2528 : 5382541 : else if (TREE_CODE (arg1) == FIXED_CST)
2529 : 0 : return fold_convert_const_real_from_fixed (type, arg1);
2530 : : }
2531 : : else if (FIXED_POINT_TYPE_P (type))
2532 : : {
2533 : 0 : if (TREE_CODE (arg1) == FIXED_CST)
2534 : 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2535 : 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2536 : 0 : return fold_convert_const_fixed_from_int (type, arg1);
2537 : 0 : else if (TREE_CODE (arg1) == REAL_CST)
2538 : 0 : return fold_convert_const_fixed_from_real (type, arg1);
2539 : : }
2540 : : else if (VECTOR_TYPE_P (type))
2541 : : {
2542 : 4749 : if (TREE_CODE (arg1) == VECTOR_CST
2543 : 4749 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2544 : : {
2545 : 4749 : tree elttype = TREE_TYPE (type);
2546 : 4749 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2547 : : /* We can't handle steps directly when extending, since the
2548 : : values need to wrap at the original precision first. */
2549 : 4749 : bool step_ok_p
2550 : 4749 : = (INTEGRAL_TYPE_P (elttype)
2551 : 311 : && INTEGRAL_TYPE_P (arg1_elttype)
2552 : 5002 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2553 : 4749 : tree_vector_builder v;
2554 : 4749 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2555 : : return NULL_TREE;
2556 : 4749 : unsigned int len = v.encoded_nelts ();
2557 : 28617 : for (unsigned int i = 0; i < len; ++i)
2558 : : {
2559 : 23868 : tree elt = VECTOR_CST_ELT (arg1, i);
2560 : 23868 : tree cvt = fold_convert_const (code, elttype, elt);
2561 : 23868 : if (cvt == NULL_TREE)
2562 : 0 : return NULL_TREE;
2563 : 23868 : v.quick_push (cvt);
2564 : : }
2565 : 4749 : return v.build ();
2566 : 4749 : }
2567 : : }
2568 : : return NULL_TREE;
2569 : : }
2570 : :
2571 : : /* Construct a vector of zero elements of vector type TYPE. */
2572 : :
2573 : : static tree
2574 : 17059 : build_zero_vector (tree type)
2575 : : {
2576 : 17059 : tree t;
2577 : :
2578 : 17059 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2579 : 17059 : return build_vector_from_val (type, t);
2580 : : }
2581 : :
2582 : : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2583 : :
2584 : : bool
2585 : 2320 : fold_convertible_p (const_tree type, const_tree arg)
2586 : : {
2587 : 2320 : const_tree orig = TREE_TYPE (arg);
2588 : :
2589 : 2320 : if (type == orig)
2590 : : return true;
2591 : :
2592 : 2320 : if (TREE_CODE (arg) == ERROR_MARK
2593 : 2320 : || TREE_CODE (type) == ERROR_MARK
2594 : 2320 : || TREE_CODE (orig) == ERROR_MARK)
2595 : : return false;
2596 : :
2597 : 2320 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2598 : : return true;
2599 : :
2600 : 2320 : switch (TREE_CODE (type))
2601 : : {
2602 : 875 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2603 : 875 : case POINTER_TYPE: case REFERENCE_TYPE:
2604 : 875 : case OFFSET_TYPE:
2605 : 875 : return (INTEGRAL_TYPE_P (orig)
2606 : 248 : || (POINTER_TYPE_P (orig)
2607 : 105 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2608 : 1018 : || TREE_CODE (orig) == OFFSET_TYPE);
2609 : :
2610 : 42 : case REAL_TYPE:
2611 : 42 : case FIXED_POINT_TYPE:
2612 : 42 : case VOID_TYPE:
2613 : 42 : return TREE_CODE (type) == TREE_CODE (orig);
2614 : :
2615 : 201 : case VECTOR_TYPE:
2616 : 201 : return (VECTOR_TYPE_P (orig)
2617 : 306 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2618 : : TYPE_VECTOR_SUBPARTS (orig))
2619 : 210 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2620 : :
2621 : : default:
2622 : : return false;
2623 : : }
2624 : : }
2625 : :
2626 : : /* Convert expression ARG to type TYPE. Used by the middle-end for
2627 : : simple conversions in preference to calling the front-end's convert. */
2628 : :
2629 : : tree
2630 : 1910151533 : fold_convert_loc (location_t loc, tree type, tree arg)
2631 : : {
2632 : 1910151533 : tree orig = TREE_TYPE (arg);
2633 : 1910151533 : tree tem;
2634 : :
2635 : 1910151533 : if (type == orig)
2636 : : return arg;
2637 : :
2638 : 1234721390 : if (TREE_CODE (arg) == ERROR_MARK
2639 : 1234720367 : || TREE_CODE (type) == ERROR_MARK
2640 : 1234720366 : || TREE_CODE (orig) == ERROR_MARK)
2641 : 1024 : return error_mark_node;
2642 : :
2643 : 1234720366 : switch (TREE_CODE (type))
2644 : : {
2645 : 59980114 : case POINTER_TYPE:
2646 : 59980114 : case REFERENCE_TYPE:
2647 : : /* Handle conversions between pointers to different address spaces. */
2648 : 59980114 : if (POINTER_TYPE_P (orig)
2649 : 59980114 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2650 : 49242736 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2651 : 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2652 : : /* fall through */
2653 : :
2654 : 1203407426 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2655 : 1203407426 : case OFFSET_TYPE: case BITINT_TYPE:
2656 : 1203407426 : if (TREE_CODE (arg) == INTEGER_CST)
2657 : : {
2658 : 1021053986 : tem = fold_convert_const (NOP_EXPR, type, arg);
2659 : 1021053986 : if (tem != NULL_TREE)
2660 : : return tem;
2661 : : }
2662 : 182353440 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2663 : 3194 : || TREE_CODE (orig) == OFFSET_TYPE)
2664 : 182353440 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2665 : 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2666 : 0 : return fold_convert_loc (loc, type,
2667 : : fold_build1_loc (loc, REALPART_EXPR,
2668 : 0 : TREE_TYPE (orig), arg));
2669 : 0 : gcc_assert (VECTOR_TYPE_P (orig)
2670 : : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2671 : 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2672 : :
2673 : 551661 : case REAL_TYPE:
2674 : 551661 : if (TREE_CODE (arg) == INTEGER_CST)
2675 : : {
2676 : 57304 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2677 : 57304 : if (tem != NULL_TREE)
2678 : : return tem;
2679 : : }
2680 : 494357 : else if (TREE_CODE (arg) == REAL_CST)
2681 : : {
2682 : 119702 : tem = fold_convert_const (NOP_EXPR, type, arg);
2683 : 119702 : if (tem != NULL_TREE)
2684 : : return tem;
2685 : : }
2686 : 374655 : else if (TREE_CODE (arg) == FIXED_CST)
2687 : : {
2688 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2689 : 0 : if (tem != NULL_TREE)
2690 : : return tem;
2691 : : }
2692 : :
2693 : 374657 : switch (TREE_CODE (orig))
2694 : : {
2695 : 650 : case INTEGER_TYPE: case BITINT_TYPE:
2696 : 650 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2697 : 650 : case POINTER_TYPE: case REFERENCE_TYPE:
2698 : 650 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2699 : :
2700 : 374007 : case REAL_TYPE:
2701 : 374007 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2702 : :
2703 : 0 : case FIXED_POINT_TYPE:
2704 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2705 : :
2706 : 0 : case COMPLEX_TYPE:
2707 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2708 : 0 : return fold_convert_loc (loc, type, tem);
2709 : :
2710 : 0 : default:
2711 : 0 : gcc_unreachable ();
2712 : : }
2713 : :
2714 : 0 : case FIXED_POINT_TYPE:
2715 : 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2716 : 0 : || TREE_CODE (arg) == REAL_CST)
2717 : : {
2718 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2719 : 0 : if (tem != NULL_TREE)
2720 : 0 : goto fold_convert_exit;
2721 : : }
2722 : :
2723 : 0 : switch (TREE_CODE (orig))
2724 : : {
2725 : 0 : case FIXED_POINT_TYPE:
2726 : 0 : case INTEGER_TYPE:
2727 : 0 : case ENUMERAL_TYPE:
2728 : 0 : case BOOLEAN_TYPE:
2729 : 0 : case REAL_TYPE:
2730 : 0 : case BITINT_TYPE:
2731 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2732 : :
2733 : 0 : case COMPLEX_TYPE:
2734 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2735 : 0 : return fold_convert_loc (loc, type, tem);
2736 : :
2737 : 0 : default:
2738 : 0 : gcc_unreachable ();
2739 : : }
2740 : :
2741 : 2254 : case COMPLEX_TYPE:
2742 : 2254 : switch (TREE_CODE (orig))
2743 : : {
2744 : 584 : case INTEGER_TYPE: case BITINT_TYPE:
2745 : 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2746 : 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2747 : 584 : case REAL_TYPE:
2748 : 584 : case FIXED_POINT_TYPE:
2749 : 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2750 : 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2751 : 584 : fold_convert_loc (loc, TREE_TYPE (type),
2752 : 584 : integer_zero_node));
2753 : 1670 : case COMPLEX_TYPE:
2754 : 1670 : {
2755 : 1670 : tree rpart, ipart;
2756 : :
2757 : 1670 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2758 : : {
2759 : 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2760 : 1534 : TREE_OPERAND (arg, 0));
2761 : 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2762 : 1534 : TREE_OPERAND (arg, 1));
2763 : 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2764 : : }
2765 : :
2766 : 136 : arg = save_expr (arg);
2767 : 136 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2768 : 136 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2769 : 136 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2770 : 136 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2771 : 136 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2772 : : }
2773 : :
2774 : 0 : default:
2775 : 0 : gcc_unreachable ();
2776 : : }
2777 : :
2778 : 30644175 : case VECTOR_TYPE:
2779 : 30644175 : if (integer_zerop (arg))
2780 : 17059 : return build_zero_vector (type);
2781 : 30627116 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2782 : 30627116 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2783 : : || VECTOR_TYPE_P (orig));
2784 : 30627116 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2785 : :
2786 : 111450 : case VOID_TYPE:
2787 : 111450 : tem = fold_ignored_result (arg);
2788 : 111450 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2789 : :
2790 : 3276 : default:
2791 : 3276 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2792 : 3276 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2793 : 0 : gcc_unreachable ();
2794 : : }
2795 : 0 : fold_convert_exit:
2796 : 0 : tem = protected_set_expr_location_unshare (tem, loc);
2797 : 0 : return tem;
2798 : : }
2799 : :
2800 : : /* Return false if expr can be assumed not to be an lvalue, true
2801 : : otherwise. */
2802 : :
2803 : : static bool
2804 : 50873403 : maybe_lvalue_p (const_tree x)
2805 : : {
2806 : : /* We only need to wrap lvalue tree codes. */
2807 : 50873403 : switch (TREE_CODE (x))
2808 : : {
2809 : : case VAR_DECL:
2810 : : case PARM_DECL:
2811 : : case RESULT_DECL:
2812 : : case LABEL_DECL:
2813 : : case FUNCTION_DECL:
2814 : : case SSA_NAME:
2815 : : case COMPOUND_LITERAL_EXPR:
2816 : :
2817 : : case COMPONENT_REF:
2818 : : case MEM_REF:
2819 : : case INDIRECT_REF:
2820 : : case ARRAY_REF:
2821 : : case ARRAY_RANGE_REF:
2822 : : case BIT_FIELD_REF:
2823 : : case OBJ_TYPE_REF:
2824 : :
2825 : : case REALPART_EXPR:
2826 : : case IMAGPART_EXPR:
2827 : : case PREINCREMENT_EXPR:
2828 : : case PREDECREMENT_EXPR:
2829 : : case SAVE_EXPR:
2830 : : case TRY_CATCH_EXPR:
2831 : : case WITH_CLEANUP_EXPR:
2832 : : case COMPOUND_EXPR:
2833 : : case MODIFY_EXPR:
2834 : : case TARGET_EXPR:
2835 : : case COND_EXPR:
2836 : : case BIND_EXPR:
2837 : : case VIEW_CONVERT_EXPR:
2838 : : break;
2839 : :
2840 : 38009064 : default:
2841 : : /* Assume the worst for front-end tree codes. */
2842 : 38009064 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2843 : : break;
2844 : : return false;
2845 : : }
2846 : :
2847 : 12928049 : return true;
2848 : : }
2849 : :
2850 : : /* Return an expr equal to X but certainly not valid as an lvalue. */
2851 : :
2852 : : tree
2853 : 45630905 : non_lvalue_loc (location_t loc, tree x)
2854 : : {
2855 : : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2856 : : us. */
2857 : 45630905 : if (in_gimple_form)
2858 : : return x;
2859 : :
2860 : 9898683 : if (! maybe_lvalue_p (x))
2861 : : return x;
2862 : 2155670 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2863 : : }
2864 : :
2865 : : /* Given a tree comparison code, return the code that is the logical inverse.
2866 : : It is generally not safe to do this for floating-point comparisons, except
2867 : : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2868 : : ERROR_MARK in this case. */
2869 : :
2870 : : enum tree_code
2871 : 117792182 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2872 : : {
2873 : 117792182 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2874 : 1096282 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2875 : : return ERROR_MARK;
2876 : :
2877 : 116966854 : switch (code)
2878 : : {
2879 : : case EQ_EXPR:
2880 : : return NE_EXPR;
2881 : 51900372 : case NE_EXPR:
2882 : 51900372 : return EQ_EXPR;
2883 : 11645087 : case GT_EXPR:
2884 : 11645087 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2885 : 13221000 : case GE_EXPR:
2886 : 13221000 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2887 : 7273032 : case LT_EXPR:
2888 : 7273032 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2889 : 7538328 : case LE_EXPR:
2890 : 7538328 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2891 : 255 : case LTGT_EXPR:
2892 : 255 : return UNEQ_EXPR;
2893 : 303 : case UNEQ_EXPR:
2894 : 303 : return LTGT_EXPR;
2895 : : case UNGT_EXPR:
2896 : : return LE_EXPR;
2897 : : case UNGE_EXPR:
2898 : : return LT_EXPR;
2899 : : case UNLT_EXPR:
2900 : : return GE_EXPR;
2901 : : case UNLE_EXPR:
2902 : : return GT_EXPR;
2903 : 243165 : case ORDERED_EXPR:
2904 : 243165 : return UNORDERED_EXPR;
2905 : 55987 : case UNORDERED_EXPR:
2906 : 55987 : return ORDERED_EXPR;
2907 : 0 : default:
2908 : 0 : gcc_unreachable ();
2909 : : }
2910 : : }
2911 : :
2912 : : /* Similar, but return the comparison that results if the operands are
2913 : : swapped. This is safe for floating-point. */
2914 : :
2915 : : enum tree_code
2916 : 151439577 : swap_tree_comparison (enum tree_code code)
2917 : : {
2918 : 151439577 : switch (code)
2919 : : {
2920 : : case EQ_EXPR:
2921 : : case NE_EXPR:
2922 : : case ORDERED_EXPR:
2923 : : case UNORDERED_EXPR:
2924 : : case LTGT_EXPR:
2925 : : case UNEQ_EXPR:
2926 : : return code;
2927 : 35592581 : case GT_EXPR:
2928 : 35592581 : return LT_EXPR;
2929 : 10595121 : case GE_EXPR:
2930 : 10595121 : return LE_EXPR;
2931 : 21100066 : case LT_EXPR:
2932 : 21100066 : return GT_EXPR;
2933 : 15099099 : case LE_EXPR:
2934 : 15099099 : return GE_EXPR;
2935 : 278529 : case UNGT_EXPR:
2936 : 278529 : return UNLT_EXPR;
2937 : 19460 : case UNGE_EXPR:
2938 : 19460 : return UNLE_EXPR;
2939 : 418976 : case UNLT_EXPR:
2940 : 418976 : return UNGT_EXPR;
2941 : 119516 : case UNLE_EXPR:
2942 : 119516 : return UNGE_EXPR;
2943 : 0 : default:
2944 : 0 : gcc_unreachable ();
2945 : : }
2946 : : }
2947 : :
2948 : :
2949 : : /* Convert a comparison tree code from an enum tree_code representation
2950 : : into a compcode bit-based encoding. This function is the inverse of
2951 : : compcode_to_comparison. */
2952 : :
2953 : : static enum comparison_code
2954 : 55408 : comparison_to_compcode (enum tree_code code)
2955 : : {
2956 : 55408 : switch (code)
2957 : : {
2958 : : case LT_EXPR:
2959 : : return COMPCODE_LT;
2960 : : case EQ_EXPR:
2961 : : return COMPCODE_EQ;
2962 : : case LE_EXPR:
2963 : : return COMPCODE_LE;
2964 : : case GT_EXPR:
2965 : : return COMPCODE_GT;
2966 : : case NE_EXPR:
2967 : : return COMPCODE_NE;
2968 : : case GE_EXPR:
2969 : : return COMPCODE_GE;
2970 : : case ORDERED_EXPR:
2971 : : return COMPCODE_ORD;
2972 : : case UNORDERED_EXPR:
2973 : : return COMPCODE_UNORD;
2974 : : case UNLT_EXPR:
2975 : : return COMPCODE_UNLT;
2976 : : case UNEQ_EXPR:
2977 : : return COMPCODE_UNEQ;
2978 : : case UNLE_EXPR:
2979 : : return COMPCODE_UNLE;
2980 : : case UNGT_EXPR:
2981 : : return COMPCODE_UNGT;
2982 : : case LTGT_EXPR:
2983 : : return COMPCODE_LTGT;
2984 : : case UNGE_EXPR:
2985 : : return COMPCODE_UNGE;
2986 : 0 : default:
2987 : 0 : gcc_unreachable ();
2988 : : }
2989 : : }
2990 : :
2991 : : /* Convert a compcode bit-based encoding of a comparison operator back
2992 : : to GCC's enum tree_code representation. This function is the
2993 : : inverse of comparison_to_compcode. */
2994 : :
2995 : : static enum tree_code
2996 : 13589 : compcode_to_comparison (enum comparison_code code)
2997 : : {
2998 : 13589 : switch (code)
2999 : : {
3000 : : case COMPCODE_LT:
3001 : : return LT_EXPR;
3002 : : case COMPCODE_EQ:
3003 : : return EQ_EXPR;
3004 : : case COMPCODE_LE:
3005 : : return LE_EXPR;
3006 : : case COMPCODE_GT:
3007 : : return GT_EXPR;
3008 : : case COMPCODE_NE:
3009 : : return NE_EXPR;
3010 : : case COMPCODE_GE:
3011 : : return GE_EXPR;
3012 : : case COMPCODE_ORD:
3013 : : return ORDERED_EXPR;
3014 : : case COMPCODE_UNORD:
3015 : : return UNORDERED_EXPR;
3016 : : case COMPCODE_UNLT:
3017 : : return UNLT_EXPR;
3018 : : case COMPCODE_UNEQ:
3019 : : return UNEQ_EXPR;
3020 : : case COMPCODE_UNLE:
3021 : : return UNLE_EXPR;
3022 : : case COMPCODE_UNGT:
3023 : : return UNGT_EXPR;
3024 : : case COMPCODE_LTGT:
3025 : : return LTGT_EXPR;
3026 : : case COMPCODE_UNGE:
3027 : : return UNGE_EXPR;
3028 : 0 : default:
3029 : 0 : gcc_unreachable ();
3030 : : }
3031 : : }
3032 : :
3033 : : /* Return true if COND1 tests the opposite condition of COND2. */
3034 : :
3035 : : bool
3036 : 1445140 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3037 : : {
3038 : 1445140 : return (COMPARISON_CLASS_P (cond1)
3039 : 1354124 : && COMPARISON_CLASS_P (cond2)
3040 : 1349532 : && (invert_tree_comparison
3041 : 1349532 : (TREE_CODE (cond1),
3042 : 2699064 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3043 : 73042 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3044 : 73042 : TREE_OPERAND (cond2, 0), 0)
3045 : 1468225 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3046 : 23085 : TREE_OPERAND (cond2, 1), 0));
3047 : : }
3048 : :
3049 : : /* Return a tree for the comparison which is the combination of
3050 : : doing the AND or OR (depending on CODE) of the two operations LCODE
3051 : : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3052 : : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3053 : : if this makes the transformation invalid. */
3054 : :
3055 : : tree
3056 : 27704 : combine_comparisons (location_t loc,
3057 : : enum tree_code code, enum tree_code lcode,
3058 : : enum tree_code rcode, tree truth_type,
3059 : : tree ll_arg, tree lr_arg)
3060 : : {
3061 : 27704 : bool honor_nans = HONOR_NANS (ll_arg);
3062 : 27704 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3063 : 27704 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3064 : 27704 : int compcode;
3065 : :
3066 : 27704 : switch (code)
3067 : : {
3068 : 18265 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3069 : 18265 : compcode = lcompcode & rcompcode;
3070 : 18265 : break;
3071 : :
3072 : 9439 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3073 : 9439 : compcode = lcompcode | rcompcode;
3074 : 9439 : break;
3075 : :
3076 : : default:
3077 : : return NULL_TREE;
3078 : : }
3079 : :
3080 : 27704 : if (!honor_nans)
3081 : : {
3082 : : /* Eliminate unordered comparisons, as well as LTGT and ORD
3083 : : which are not used unless the mode has NaNs. */
3084 : 22629 : compcode &= ~COMPCODE_UNORD;
3085 : 22629 : if (compcode == COMPCODE_LTGT)
3086 : : compcode = COMPCODE_NE;
3087 : 21548 : else if (compcode == COMPCODE_ORD)
3088 : : compcode = COMPCODE_TRUE;
3089 : : }
3090 : 5075 : else if (flag_trapping_math)
3091 : : {
3092 : : /* Check that the original operation and the optimized ones will trap
3093 : : under the same condition. */
3094 : 8308 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3095 : 3518 : && (lcompcode != COMPCODE_EQ)
3096 : 4154 : && (lcompcode != COMPCODE_ORD);
3097 : 8308 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3098 : 3666 : && (rcompcode != COMPCODE_EQ)
3099 : 4154 : && (rcompcode != COMPCODE_ORD);
3100 : 8308 : bool trap = (compcode & COMPCODE_UNORD) == 0
3101 : 3731 : && (compcode != COMPCODE_EQ)
3102 : 4154 : && (compcode != COMPCODE_ORD);
3103 : :
3104 : : /* In a short-circuited boolean expression the LHS might be
3105 : : such that the RHS, if evaluated, will never trap. For
3106 : : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3107 : : if neither x nor y is NaN. (This is a mixed blessing: for
3108 : : example, the expression above will never trap, hence
3109 : : optimizing it to x < y would be invalid). */
3110 : 4154 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3111 : 3753 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3112 : 4154 : rtrap = false;
3113 : :
3114 : : /* If the comparison was short-circuited, and only the RHS
3115 : : trapped, we may now generate a spurious trap. */
3116 : 4154 : if (rtrap && !ltrap
3117 : 118 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3118 : : return NULL_TREE;
3119 : :
3120 : : /* If we changed the conditions that cause a trap, we lose. */
3121 : 4036 : if ((ltrap || rtrap) != trap)
3122 : : return NULL_TREE;
3123 : : }
3124 : :
3125 : 1642 : if (compcode == COMPCODE_TRUE)
3126 : 1238 : return constant_boolean_node (true, truth_type);
3127 : 23033 : else if (compcode == COMPCODE_FALSE)
3128 : 9444 : return constant_boolean_node (false, truth_type);
3129 : : else
3130 : : {
3131 : 13589 : enum tree_code tcode;
3132 : :
3133 : 13589 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3134 : 13589 : return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3135 : : }
3136 : : }
3137 : :
3138 : : /* Return nonzero if two operands (typically of the same tree node)
3139 : : are necessarily equal. FLAGS modifies behavior as follows:
3140 : :
3141 : : If OEP_ONLY_CONST is set, only return nonzero for constants.
3142 : : This function tests whether the operands are indistinguishable;
3143 : : it does not test whether they are equal using C's == operation.
3144 : : The distinction is important for IEEE floating point, because
3145 : : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3146 : : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3147 : :
3148 : : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3149 : : even though it may hold multiple values during a function.
3150 : : This is because a GCC tree node guarantees that nothing else is
3151 : : executed between the evaluation of its "operands" (which may often
3152 : : be evaluated in arbitrary order). Hence if the operands themselves
3153 : : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3154 : : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3155 : : unset means assuming isochronic (or instantaneous) tree equivalence.
3156 : : Unless comparing arbitrary expression trees, such as from different
3157 : : statements, this flag can usually be left unset.
3158 : :
3159 : : If OEP_PURE_SAME is set, then pure functions with identical arguments
3160 : : are considered the same. It is used when the caller has other ways
3161 : : to ensure that global memory is unchanged in between.
3162 : :
3163 : : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3164 : : not values of expressions.
3165 : :
3166 : : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3167 : : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3168 : :
3169 : : If OEP_BITWISE is set, then require the values to be bitwise identical
3170 : : rather than simply numerically equal. Do not take advantage of things
3171 : : like math-related flags or undefined behavior; only return true for
3172 : : values that are provably bitwise identical in all circumstances.
3173 : :
3174 : : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3175 : : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3176 : : rather than just numerically equivalent. The compared expressions must
3177 : : however perform the same operations but may do intermediate computations in
3178 : : differing signs. Because this comparison ignores any possible UB it cannot
3179 : : be used blindly without ensuring that the context you are using it in itself
3180 : : doesn't guarantee that there will be no UB. Conditional expressions are
3181 : : excluded from this relaxation.
3182 : :
3183 : : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3184 : : differing hashes even for cases where operand_compare::operand_equal_p
3185 : : compares equal.
3186 : :
3187 : : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3188 : : any operand with side effect. This is unnecesarily conservative in the
3189 : : case we know that arg0 and arg1 are in disjoint code paths (such as in
3190 : : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3191 : : addresses with TREE_CONSTANT flag set so we know that &var == &var
3192 : : even if var is volatile. */
3193 : :
3194 : : bool
3195 : 7020728815 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3196 : : unsigned int flags)
3197 : : {
3198 : 7020728815 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3199 : : }
3200 : :
3201 : : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3202 : : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3203 : : expression is being compared under for equality. This means that they can
3204 : : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3205 : :
3206 : : bool
3207 : 7021442531 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3208 : : tree type1, const_tree arg1,
3209 : : unsigned int flags)
3210 : : {
3211 : 7021442531 : bool r;
3212 : 7021442531 : if (verify_hash_value (arg0, arg1, flags, &r))
3213 : 2951891908 : return r;
3214 : :
3215 : 4069550623 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3216 : 4069550623 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3217 : :
3218 : : /* If either is ERROR_MARK, they aren't equal. */
3219 : 4069550623 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3220 : 4069550019 : || type0 == error_mark_node
3221 : 4069550017 : || type1 == error_mark_node)
3222 : : return false;
3223 : :
3224 : : /* Similar, if either does not have a type (like a template id),
3225 : : they aren't equal. */
3226 : 4069550016 : if (!type0 || !type1)
3227 : : return false;
3228 : :
3229 : : /* Bitwise identity makes no sense if the values have different layouts. */
3230 : 4069547345 : if ((flags & OEP_BITWISE)
3231 : 4069547345 : && !tree_nop_conversion_p (type0, type1))
3232 : : return false;
3233 : :
3234 : : /* We cannot consider pointers to different address space equal. */
3235 : 4069547345 : if (POINTER_TYPE_P (type0)
3236 : 599957264 : && POINTER_TYPE_P (type1)
3237 : 4578269185 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3238 : 508721840 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3239 : : return false;
3240 : :
3241 : : /* Check equality of integer constants before bailing out due to
3242 : : precision differences. */
3243 : 4069547154 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3244 : : {
3245 : : /* Address of INTEGER_CST is not defined; check that we did not forget
3246 : : to drop the OEP_ADDRESS_OF flags. */
3247 : 645744565 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3248 : 645744565 : return tree_int_cst_equal (arg0, arg1);
3249 : : }
3250 : :
3251 : 3423802589 : if ((flags & OEP_ASSUME_WRAPV)
3252 : 2032554 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3253 : : {
3254 : 767866 : const_tree t_arg0 = arg0;
3255 : 767866 : const_tree t_arg1 = arg1;
3256 : 767866 : STRIP_NOPS (arg0);
3257 : 767866 : STRIP_NOPS (arg1);
3258 : : /* Only recurse if the conversion was one that was valid to strip. */
3259 : 767866 : if (t_arg0 != arg0 || t_arg1 != arg1)
3260 : 713716 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3261 : : }
3262 : :
3263 : 3423088873 : if (!(flags & OEP_ADDRESS_OF))
3264 : : {
3265 : : /* Check if we are checking an operation where the two's compliment
3266 : : bitwise representation of the result is not the same between signed and
3267 : : unsigned arithmetic. */
3268 : 3044290588 : bool enforce_signedness = true;
3269 : 3044290588 : if (flags & OEP_ASSUME_WRAPV)
3270 : : {
3271 : 1232631 : switch (TREE_CODE (arg0))
3272 : : {
3273 : : case PLUS_EXPR:
3274 : : case MINUS_EXPR:
3275 : : case MULT_EXPR:
3276 : : case BIT_IOR_EXPR:
3277 : : case BIT_XOR_EXPR:
3278 : : case BIT_AND_EXPR:
3279 : : case BIT_NOT_EXPR:
3280 : : case ABS_EXPR:
3281 : : CASE_CONVERT:
3282 : : case SSA_NAME:
3283 : : case INTEGER_CST:
3284 : : case VAR_DECL:
3285 : : case PARM_DECL:
3286 : : case RESULT_DECL:
3287 : 3044290588 : enforce_signedness = false;
3288 : : break;
3289 : :
3290 : : default:
3291 : : break;
3292 : : }
3293 : : }
3294 : :
3295 : : /* If both types don't have the same signedness, then we can't consider
3296 : : them equal. We must check this before the STRIP_NOPS calls
3297 : : because they may change the signedness of the arguments. As pointers
3298 : : strictly don't have a signedness, require either two pointers or
3299 : : two non-pointers as well. */
3300 : 3044290588 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3301 : 3044290588 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3302 : 140444280 : && enforce_signedness))
3303 : : return false;
3304 : :
3305 : : /* If both types don't have the same precision, then it is not safe
3306 : : to strip NOPs. */
3307 : 2752510259 : if (element_precision (type0) != element_precision (type1))
3308 : : return false;
3309 : :
3310 : 2607122656 : STRIP_NOPS (arg0);
3311 : 2607122656 : STRIP_NOPS (arg1);
3312 : :
3313 : 2607122656 : type0 = TREE_TYPE (arg0);
3314 : 2607122656 : type1 = TREE_TYPE (arg1);
3315 : : }
3316 : : #if 0
3317 : : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3318 : : sanity check once the issue is solved. */
3319 : : else
3320 : : /* Addresses of conversions and SSA_NAMEs (and many other things)
3321 : : are not defined. Check that we did not forget to drop the
3322 : : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3323 : : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3324 : : && TREE_CODE (arg0) != SSA_NAME);
3325 : : #endif
3326 : :
3327 : : /* In case both args are comparisons but with different comparison
3328 : : code, try to swap the comparison operands of one arg to produce
3329 : : a match and compare that variant. */
3330 : 2985920941 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3331 : 1214980773 : && COMPARISON_CLASS_P (arg0)
3332 : 6301551 : && COMPARISON_CLASS_P (arg1))
3333 : : {
3334 : 4648186 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3335 : :
3336 : 4648186 : if (TREE_CODE (arg0) == swap_code)
3337 : 2056578 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3338 : 2056578 : TREE_OPERAND (arg1, 1), flags)
3339 : 2076473 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3340 : 19895 : TREE_OPERAND (arg1, 0), flags);
3341 : : }
3342 : :
3343 : 2983864363 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3344 : : {
3345 : : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3346 : 1212924195 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3347 : : ;
3348 : 1212897967 : else if (flags & OEP_ADDRESS_OF)
3349 : : {
3350 : : /* If we are interested in comparing addresses ignore
3351 : : MEM_REF wrappings of the base that can appear just for
3352 : : TBAA reasons. */
3353 : 48103884 : if (TREE_CODE (arg0) == MEM_REF
3354 : 7352698 : && DECL_P (arg1)
3355 : 5068001 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3356 : 1123570 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3357 : 48644681 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3358 : : return true;
3359 : 47856181 : else if (TREE_CODE (arg1) == MEM_REF
3360 : 30404770 : && DECL_P (arg0)
3361 : 11052854 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3362 : 2399753 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3363 : 48439415 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3364 : : return true;
3365 : 47450198 : return false;
3366 : : }
3367 : : else
3368 : : return false;
3369 : : }
3370 : :
3371 : : /* When not checking adddresses, this is needed for conversions and for
3372 : : COMPONENT_REF. Might as well play it safe and always test this. */
3373 : 1770966396 : if (TREE_CODE (type0) == ERROR_MARK
3374 : 1770966396 : || TREE_CODE (type1) == ERROR_MARK
3375 : 3541932792 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3376 : 24761774 : && !(flags & OEP_ADDRESS_OF)))
3377 : 3711235 : return false;
3378 : :
3379 : : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3380 : : We don't care about side effects in that case because the SAVE_EXPR
3381 : : takes care of that for us. In all other cases, two expressions are
3382 : : equal if they have no side effects. If we have two identical
3383 : : expressions with side effects that should be treated the same due
3384 : : to the only side effects being identical SAVE_EXPR's, that will
3385 : : be detected in the recursive calls below.
3386 : : If we are taking an invariant address of two identical objects
3387 : : they are necessarily equal as well. */
3388 : 314115008 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3389 : 2081369991 : && (TREE_CODE (arg0) == SAVE_EXPR
3390 : 314087804 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3391 : 279972549 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3392 : : return true;
3393 : :
3394 : : /* Next handle constant cases, those for which we can return 1 even
3395 : : if ONLY_CONST is set. */
3396 : 1453293673 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3397 : 19519977 : switch (TREE_CODE (arg0))
3398 : : {
3399 : 153 : case INTEGER_CST:
3400 : 153 : return tree_int_cst_equal (arg0, arg1);
3401 : :
3402 : 0 : case FIXED_CST:
3403 : 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3404 : : TREE_FIXED_CST (arg1));
3405 : :
3406 : 3656926 : case REAL_CST:
3407 : 3656926 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3408 : : return true;
3409 : :
3410 : 2634018 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3411 : : {
3412 : : /* If we do not distinguish between signed and unsigned zero,
3413 : : consider them equal. */
3414 : 13891 : if (real_zerop (arg0) && real_zerop (arg1))
3415 : : return true;
3416 : : }
3417 : 2634009 : return false;
3418 : :
3419 : 781046 : case VECTOR_CST:
3420 : 781046 : {
3421 : 781046 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3422 : 781046 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3423 : : return false;
3424 : :
3425 : 760410 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3426 : 760410 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3427 : : return false;
3428 : :
3429 : 728988 : unsigned int count = vector_cst_encoded_nelts (arg0);
3430 : 1069870 : for (unsigned int i = 0; i < count; ++i)
3431 : 1681802 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3432 : 840901 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3433 : : return false;
3434 : : return true;
3435 : : }
3436 : :
3437 : 13293 : case COMPLEX_CST:
3438 : 13293 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3439 : : flags)
3440 : 13293 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3441 : : flags));
3442 : :
3443 : 1223476 : case STRING_CST:
3444 : 1223476 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3445 : 1223476 : && ! memcmp (TREE_STRING_POINTER (arg0),
3446 : 796659 : TREE_STRING_POINTER (arg1),
3447 : 796659 : TREE_STRING_LENGTH (arg0)));
3448 : :
3449 : 0 : case RAW_DATA_CST:
3450 : 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3451 : 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3452 : 0 : RAW_DATA_POINTER (arg1),
3453 : 0 : RAW_DATA_LENGTH (arg0)));
3454 : :
3455 : 12881560 : case ADDR_EXPR:
3456 : 12881560 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3457 : 12881560 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3458 : : flags | OEP_ADDRESS_OF
3459 : 12881560 : | OEP_MATCH_SIDE_EFFECTS);
3460 : 178585 : case CONSTRUCTOR:
3461 : 178585 : {
3462 : : /* In GIMPLE empty constructors are allowed in initializers of
3463 : : aggregates. */
3464 : 178585 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3465 : : return true;
3466 : :
3467 : : /* See sem_variable::equals in ipa-icf for a similar approach. */
3468 : 137906 : if (TREE_CODE (type0) != TREE_CODE (type1))
3469 : : return false;
3470 : 137906 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3471 : : {
3472 : : /* For arrays, check that the sizes all match. */
3473 : 44 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3474 : 44 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3475 : 44 : || siz0 < 0
3476 : 88 : || siz0 != int_size_in_bytes (type1))
3477 : 0 : return false;
3478 : : }
3479 : 137862 : else if (!types_compatible_p (type0, type1))
3480 : : return false;
3481 : :
3482 : 137906 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3483 : 137906 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3484 : 413718 : if (vec_safe_length (v0) != vec_safe_length (v1))
3485 : : return false;
3486 : :
3487 : : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3488 : : of the CONSTRUCTOR referenced indirectly. */
3489 : 137906 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3490 : :
3491 : 349056967 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3492 : : {
3493 : 196451 : constructor_elt *c0 = &(*v0)[idx];
3494 : 196451 : constructor_elt *c1 = &(*v1)[idx];
3495 : :
3496 : : /* Check that the values are the same... */
3497 : 196451 : if (c0->value != c1->value
3498 : 196451 : && !operand_equal_p (c0->value, c1->value, flags))
3499 : : return false;
3500 : :
3501 : : /* ... and that they apply to the same field! */
3502 : 107884 : if (c0->index != c1->index
3503 : 107884 : && (TREE_CODE (type0) == ARRAY_TYPE
3504 : 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3505 : 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3506 : 0 : DECL_FIELD_OFFSET (c1->index),
3507 : : flags)
3508 : 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3509 : 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3510 : : flags)))
3511 : 0 : return false;
3512 : : }
3513 : :
3514 : : return true;
3515 : : }
3516 : :
3517 : : default:
3518 : : break;
3519 : : }
3520 : :
3521 : : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3522 : : two instances of undefined behavior will give identical results. */
3523 : 1434558634 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3524 : : return false;
3525 : :
3526 : : /* Define macros to test an operand from arg0 and arg1 for equality and a
3527 : : variant that allows null and views null as being different from any
3528 : : non-null value. In the latter case, if either is null, the both
3529 : : must be; otherwise, do the normal comparison. */
3530 : : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3531 : : TREE_OPERAND (arg1, N), flags)
3532 : :
3533 : : #define OP_SAME_WITH_NULL(N) \
3534 : : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3535 : : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3536 : :
3537 : 1434558634 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3538 : : {
3539 : 7203489 : case tcc_unary:
3540 : : /* Two conversions are equal only if signedness and modes match. */
3541 : 7203489 : switch (TREE_CODE (arg0))
3542 : : {
3543 : 6828073 : CASE_CONVERT:
3544 : 6828073 : case FIX_TRUNC_EXPR:
3545 : 6828073 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3546 : : return false;
3547 : : break;
3548 : : default:
3549 : : break;
3550 : : }
3551 : :
3552 : 7203468 : return OP_SAME_WITH_NULL (0);
3553 : :
3554 : :
3555 : 22091776 : case tcc_comparison:
3556 : 22091776 : case tcc_binary:
3557 : 22091776 : if (OP_SAME (0) && OP_SAME (1))
3558 : : return true;
3559 : :
3560 : : /* For commutative ops, allow the other order. */
3561 : 16274061 : return (commutative_tree_code (TREE_CODE (arg0))
3562 : 12428497 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3563 : 12428497 : TREE_OPERAND (arg1, 1), flags)
3564 : 16488469 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3565 : 214408 : TREE_OPERAND (arg1, 0), flags));
3566 : :
3567 : 865858567 : case tcc_reference:
3568 : : /* If either of the pointer (or reference) expressions we are
3569 : : dereferencing contain a side effect, these cannot be equal,
3570 : : but their addresses can be. */
3571 : 865858567 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3572 : 865858567 : && (TREE_SIDE_EFFECTS (arg0)
3573 : 804588964 : || TREE_SIDE_EFFECTS (arg1)))
3574 : : return false;
3575 : :
3576 : 865603733 : switch (TREE_CODE (arg0))
3577 : : {
3578 : 4073277 : case INDIRECT_REF:
3579 : 4073277 : if (!(flags & OEP_ADDRESS_OF))
3580 : : {
3581 : 4050109 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3582 : : return false;
3583 : : /* Verify that the access types are compatible. */
3584 : 4048231 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3585 : : return false;
3586 : : }
3587 : 4007716 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3588 : 4007716 : return OP_SAME (0);
3589 : :
3590 : 655188 : case IMAGPART_EXPR:
3591 : : /* Require the same offset. */
3592 : 655188 : if (!operand_equal_p (TYPE_SIZE (type0),
3593 : 655188 : TYPE_SIZE (type1),
3594 : : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3595 : : return false;
3596 : :
3597 : : /* Fallthru. */
3598 : 2447390 : case REALPART_EXPR:
3599 : 2447390 : case VIEW_CONVERT_EXPR:
3600 : 2447390 : return OP_SAME (0);
3601 : :
3602 : 75755919 : case TARGET_MEM_REF:
3603 : 75755919 : case MEM_REF:
3604 : 75755919 : if (!(flags & OEP_ADDRESS_OF))
3605 : : {
3606 : : /* Require equal access sizes */
3607 : 16359521 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3608 : 16359521 : && (!TYPE_SIZE (type0)
3609 : 1060217 : || !TYPE_SIZE (type1)
3610 : 1055605 : || !operand_equal_p (TYPE_SIZE (type0),
3611 : 1055605 : TYPE_SIZE (type1),
3612 : : flags)))
3613 : 1055457 : return false;
3614 : : /* Verify that access happens in similar types. */
3615 : 15304064 : if (!types_compatible_p (type0, type1))
3616 : : return false;
3617 : : /* Verify that accesses are TBAA compatible. */
3618 : 14942613 : if (!alias_ptr_types_compatible_p
3619 : 14942613 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3620 : 14942613 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3621 : 14005776 : || (MR_DEPENDENCE_CLIQUE (arg0)
3622 : 14005776 : != MR_DEPENDENCE_CLIQUE (arg1))
3623 : 27147191 : || (MR_DEPENDENCE_BASE (arg0)
3624 : 12204578 : != MR_DEPENDENCE_BASE (arg1)))
3625 : : return false;
3626 : : /* Verify that alignment is compatible. */
3627 : 11737608 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3628 : : return false;
3629 : : }
3630 : 70984588 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3631 : 120465865 : return (OP_SAME (0) && OP_SAME (1)
3632 : : /* TARGET_MEM_REF require equal extra operands. */
3633 : 95131890 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3634 : 579788 : || (OP_SAME_WITH_NULL (2)
3635 : 272949 : && OP_SAME_WITH_NULL (3)
3636 : 267121 : && OP_SAME_WITH_NULL (4))));
3637 : :
3638 : 38243090 : case ARRAY_REF:
3639 : 38243090 : case ARRAY_RANGE_REF:
3640 : 38243090 : if (!OP_SAME (0))
3641 : : return false;
3642 : 33399149 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3643 : : /* Compare the array index by value if it is constant first as we
3644 : : may have different types but same value here. */
3645 : 33399149 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3646 : 33399149 : TREE_OPERAND (arg1, 1))
3647 : 30444757 : || OP_SAME (1))
3648 : 5935890 : && OP_SAME_WITH_NULL (2)
3649 : 5934350 : && OP_SAME_WITH_NULL (3)
3650 : : /* Compare low bound and element size as with OEP_ADDRESS_OF
3651 : : we have to account for the offset of the ref. */
3652 : 42301444 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3653 : 2967175 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3654 : 2636 : || (operand_equal_p (array_ref_low_bound
3655 : 2636 : (CONST_CAST_TREE (arg0)),
3656 : : array_ref_low_bound
3657 : 2636 : (CONST_CAST_TREE (arg1)), flags)
3658 : 2636 : && operand_equal_p (array_ref_element_size
3659 : 2636 : (CONST_CAST_TREE (arg0)),
3660 : : array_ref_element_size
3661 : 2636 : (CONST_CAST_TREE (arg1)),
3662 : : flags))));
3663 : :
3664 : 744581472 : case COMPONENT_REF:
3665 : : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3666 : : may be NULL when we're called to compare MEM_EXPRs. */
3667 : 744581472 : if (!OP_SAME_WITH_NULL (0))
3668 : : return false;
3669 : 56683148 : {
3670 : 56683148 : bool compare_address = flags & OEP_ADDRESS_OF;
3671 : :
3672 : : /* Most of time we only need to compare FIELD_DECLs for equality.
3673 : : However when determining address look into actual offsets.
3674 : : These may match for unions and unshared record types. */
3675 : 56683148 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3676 : 56683148 : if (!OP_SAME (1))
3677 : : {
3678 : 32796111 : if (compare_address
3679 : 598061 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3680 : : {
3681 : 598058 : tree field0 = TREE_OPERAND (arg0, 1);
3682 : 598058 : tree field1 = TREE_OPERAND (arg1, 1);
3683 : :
3684 : : /* Non-FIELD_DECL operands can appear in C++ templates. */
3685 : 598058 : if (TREE_CODE (field0) != FIELD_DECL
3686 : 598058 : || TREE_CODE (field1) != FIELD_DECL)
3687 : : return false;
3688 : :
3689 : 598058 : if (!DECL_FIELD_OFFSET (field0)
3690 : 598058 : || !DECL_FIELD_OFFSET (field1))
3691 : 3 : return field0 == field1;
3692 : :
3693 : 598055 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3694 : 598055 : DECL_FIELD_OFFSET (field1), flags)
3695 : 789435 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3696 : 191380 : DECL_FIELD_BIT_OFFSET (field1),
3697 : : flags))
3698 : 558346 : return false;
3699 : : }
3700 : : else
3701 : : return false;
3702 : : }
3703 : : }
3704 : 23926746 : return OP_SAME_WITH_NULL (2);
3705 : :
3706 : 502537 : case BIT_FIELD_REF:
3707 : 502537 : if (!OP_SAME (0))
3708 : : return false;
3709 : 373342 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3710 : 373342 : return OP_SAME (1) && OP_SAME (2);
3711 : :
3712 : : default:
3713 : : return false;
3714 : : }
3715 : :
3716 : 51004879 : case tcc_expression:
3717 : 51004879 : switch (TREE_CODE (arg0))
3718 : : {
3719 : 45927056 : case ADDR_EXPR:
3720 : : /* Be sure we pass right ADDRESS_OF flag. */
3721 : 45927056 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3722 : 45927056 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3723 : 45927056 : TREE_OPERAND (arg1, 0),
3724 : 45927056 : flags | OEP_ADDRESS_OF);
3725 : :
3726 : 684415 : case TRUTH_NOT_EXPR:
3727 : 684415 : return OP_SAME (0);
3728 : :
3729 : 43613 : case TRUTH_ANDIF_EXPR:
3730 : 43613 : case TRUTH_ORIF_EXPR:
3731 : 43613 : return OP_SAME (0) && OP_SAME (1);
3732 : :
3733 : 0 : case WIDEN_MULT_PLUS_EXPR:
3734 : 0 : case WIDEN_MULT_MINUS_EXPR:
3735 : 0 : if (!OP_SAME (2))
3736 : : return false;
3737 : : /* The multiplcation operands are commutative. */
3738 : : /* FALLTHRU */
3739 : :
3740 : 20601 : case TRUTH_AND_EXPR:
3741 : 20601 : case TRUTH_OR_EXPR:
3742 : 20601 : case TRUTH_XOR_EXPR:
3743 : 20601 : if (OP_SAME (0) && OP_SAME (1))
3744 : : return true;
3745 : :
3746 : : /* Otherwise take into account this is a commutative operation. */
3747 : 20583 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3748 : 20583 : TREE_OPERAND (arg1, 1), flags)
3749 : 20586 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3750 : 3 : TREE_OPERAND (arg1, 0), flags));
3751 : :
3752 : 129029 : case COND_EXPR:
3753 : 129029 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3754 : 49583 : return false;
3755 : 79446 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3756 : 79446 : return OP_SAME (0);
3757 : :
3758 : 4 : case BIT_INSERT_EXPR:
3759 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
3760 : : of op1. Need to check to make sure they are the same. */
3761 : 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3762 : 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3763 : 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3764 : 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3765 : : return false;
3766 : : /* FALLTHRU */
3767 : :
3768 : 191 : case VEC_COND_EXPR:
3769 : 191 : case DOT_PROD_EXPR:
3770 : 191 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3771 : :
3772 : 24441 : case MODIFY_EXPR:
3773 : 24441 : case INIT_EXPR:
3774 : 24441 : case COMPOUND_EXPR:
3775 : 24441 : case PREDECREMENT_EXPR:
3776 : 24441 : case PREINCREMENT_EXPR:
3777 : 24441 : case POSTDECREMENT_EXPR:
3778 : 24441 : case POSTINCREMENT_EXPR:
3779 : 24441 : if (flags & OEP_LEXICOGRAPHIC)
3780 : 165 : return OP_SAME (0) && OP_SAME (1);
3781 : : return false;
3782 : :
3783 : 115002 : case CLEANUP_POINT_EXPR:
3784 : 115002 : case EXPR_STMT:
3785 : 115002 : case SAVE_EXPR:
3786 : 115002 : if (flags & OEP_LEXICOGRAPHIC)
3787 : 208 : return OP_SAME (0);
3788 : : return false;
3789 : :
3790 : 115982 : case OBJ_TYPE_REF:
3791 : : /* Virtual table reference. */
3792 : 231964 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3793 : 115982 : OBJ_TYPE_REF_EXPR (arg1), flags))
3794 : : return false;
3795 : 16169 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3796 : 16169 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3797 : 16169 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3798 : : return false;
3799 : 16169 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3800 : 16169 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3801 : : return false;
3802 : 16169 : if (virtual_method_call_p (arg0))
3803 : : {
3804 : 16169 : if (!virtual_method_call_p (arg1))
3805 : : return false;
3806 : 16169 : return types_same_for_odr (obj_type_ref_class (arg0),
3807 : 32338 : obj_type_ref_class (arg1));
3808 : : }
3809 : : return false;
3810 : :
3811 : : default:
3812 : : return false;
3813 : : }
3814 : :
3815 : 2964780 : case tcc_vl_exp:
3816 : 2964780 : switch (TREE_CODE (arg0))
3817 : : {
3818 : 2964780 : case CALL_EXPR:
3819 : 2964780 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3820 : 2964780 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3821 : : /* If not both CALL_EXPRs are either internal or normal function
3822 : : functions, then they are not equal. */
3823 : : return false;
3824 : 2964780 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3825 : : {
3826 : : /* If the CALL_EXPRs call different internal functions, then they
3827 : : are not equal. */
3828 : 2 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3829 : : return false;
3830 : : }
3831 : : else
3832 : : {
3833 : : /* If the CALL_EXPRs call different functions, then they are not
3834 : : equal. */
3835 : 2964778 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3836 : : flags))
3837 : : return false;
3838 : : }
3839 : :
3840 : : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3841 : 1910466 : {
3842 : 1910466 : unsigned int cef = call_expr_flags (arg0);
3843 : 1910466 : if (flags & OEP_PURE_SAME)
3844 : 0 : cef &= ECF_CONST | ECF_PURE;
3845 : : else
3846 : 1910466 : cef &= ECF_CONST;
3847 : 1910466 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3848 : : return false;
3849 : : }
3850 : :
3851 : : /* Now see if all the arguments are the same. */
3852 : 33445 : {
3853 : 33445 : const_call_expr_arg_iterator iter0, iter1;
3854 : 33445 : const_tree a0, a1;
3855 : 66890 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3856 : 33445 : a1 = first_const_call_expr_arg (arg1, &iter1);
3857 : 41550 : a0 && a1;
3858 : 8105 : a0 = next_const_call_expr_arg (&iter0),
3859 : 8105 : a1 = next_const_call_expr_arg (&iter1))
3860 : 34933 : if (! operand_equal_p (a0, a1, flags))
3861 : : return false;
3862 : :
3863 : : /* If we get here and both argument lists are exhausted
3864 : : then the CALL_EXPRs are equal. */
3865 : 6617 : return ! (a0 || a1);
3866 : : }
3867 : : default:
3868 : : return false;
3869 : : }
3870 : :
3871 : 161639465 : case tcc_declaration:
3872 : : /* Consider __builtin_sqrt equal to sqrt. */
3873 : 161639465 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3874 : 6436728 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3875 : 351258 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3876 : 5755068 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3877 : 351258 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3878 : :
3879 : 155884397 : if (DECL_P (arg0)
3880 : 155884397 : && (flags & OEP_DECL_NAME)
3881 : 35 : && (flags & OEP_LEXICOGRAPHIC))
3882 : : {
3883 : : /* Consider decls with the same name equal. The caller needs
3884 : : to make sure they refer to the same entity (such as a function
3885 : : formal parameter). */
3886 : 35 : tree a0name = DECL_NAME (arg0);
3887 : 35 : tree a1name = DECL_NAME (arg1);
3888 : 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3889 : 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3890 : 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3891 : : }
3892 : : return false;
3893 : :
3894 : 321495937 : case tcc_exceptional:
3895 : 321495937 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3896 : : {
3897 : 19267 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3898 : : return false;
3899 : :
3900 : : /* In GIMPLE constructors are used only to build vectors from
3901 : : elements. Individual elements in the constructor must be
3902 : : indexed in increasing order and form an initial sequence.
3903 : :
3904 : : We make no effort to compare nonconstant ones in GENERIC. */
3905 : 19267 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3906 : : return false;
3907 : :
3908 : : /* Be sure that vectors constructed have the same representation.
3909 : : We only tested element precision and modes to match.
3910 : : Vectors may be BLKmode and thus also check that the number of
3911 : : parts match. */
3912 : 564 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3913 : 1128 : TYPE_VECTOR_SUBPARTS (type1)))
3914 : : return false;
3915 : :
3916 : 564 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3917 : 564 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3918 : 564 : unsigned int len = vec_safe_length (v0);
3919 : :
3920 : 1128 : if (len != vec_safe_length (v1))
3921 : : return false;
3922 : :
3923 : 3494 : for (unsigned int i = 0; i < len; i++)
3924 : : {
3925 : 3105 : constructor_elt *c0 = &(*v0)[i];
3926 : 3105 : constructor_elt *c1 = &(*v1)[i];
3927 : :
3928 : 3105 : if (!operand_equal_p (c0->value, c1->value, flags)
3929 : : /* In GIMPLE the indexes can be either NULL or matching i.
3930 : : Double check this so we won't get false
3931 : : positives for GENERIC. */
3932 : 2930 : || (c0->index
3933 : 2588 : && (TREE_CODE (c0->index) != INTEGER_CST
3934 : 2588 : || compare_tree_int (c0->index, i)))
3935 : 6035 : || (c1->index
3936 : 2588 : && (TREE_CODE (c1->index) != INTEGER_CST
3937 : 2588 : || compare_tree_int (c1->index, i))))
3938 : 175 : return false;
3939 : : }
3940 : : return true;
3941 : : }
3942 : 321476670 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3943 : 3004 : && (flags & OEP_LEXICOGRAPHIC))
3944 : : {
3945 : : /* Compare the STATEMENT_LISTs. */
3946 : 16 : tree_stmt_iterator tsi1, tsi2;
3947 : 16 : tree body1 = CONST_CAST_TREE (arg0);
3948 : 16 : tree body2 = CONST_CAST_TREE (arg1);
3949 : 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3950 : 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3951 : : {
3952 : : /* The lists don't have the same number of statements. */
3953 : 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3954 : : return false;
3955 : 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3956 : : return true;
3957 : 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3958 : : flags & (OEP_LEXICOGRAPHIC
3959 : : | OEP_NO_HASH_CHECK)))
3960 : : return false;
3961 : : }
3962 : : }
3963 : : return false;
3964 : :
3965 : 2299556 : case tcc_statement:
3966 : 2299556 : switch (TREE_CODE (arg0))
3967 : : {
3968 : 52 : case RETURN_EXPR:
3969 : 52 : if (flags & OEP_LEXICOGRAPHIC)
3970 : 52 : return OP_SAME_WITH_NULL (0);
3971 : : return false;
3972 : 4 : case DEBUG_BEGIN_STMT:
3973 : 4 : if (flags & OEP_LEXICOGRAPHIC)
3974 : : return true;
3975 : : return false;
3976 : : default:
3977 : : return false;
3978 : : }
3979 : :
3980 : : default:
3981 : : return false;
3982 : : }
3983 : :
3984 : : #undef OP_SAME
3985 : : #undef OP_SAME_WITH_NULL
3986 : : }
3987 : :
3988 : : /* Generate a hash value for an expression. This can be used iteratively
3989 : : by passing a previous result as the HSTATE argument. */
3990 : :
3991 : : void
3992 : 2803264390 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3993 : : unsigned int flags)
3994 : : {
3995 : 2803264390 : int i;
3996 : 2803264390 : enum tree_code code;
3997 : 2803264390 : enum tree_code_class tclass;
3998 : :
3999 : 2803264390 : if (t == NULL_TREE || t == error_mark_node)
4000 : : {
4001 : 75164238 : hstate.merge_hash (0);
4002 : 75164238 : return;
4003 : : }
4004 : :
4005 : 2728100152 : STRIP_ANY_LOCATION_WRAPPER (t);
4006 : :
4007 : 2728100152 : if (!(flags & OEP_ADDRESS_OF))
4008 : 2477913178 : STRIP_NOPS (t);
4009 : :
4010 : 2728100152 : code = TREE_CODE (t);
4011 : :
4012 : 2728100152 : switch (code)
4013 : : {
4014 : : /* Alas, constants aren't shared, so we can't rely on pointer
4015 : : identity. */
4016 : 828 : case VOID_CST:
4017 : 828 : hstate.merge_hash (0);
4018 : 828 : return;
4019 : 802287704 : case INTEGER_CST:
4020 : 802287704 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4021 : 1620772752 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4022 : 818485048 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4023 : : return;
4024 : 15523407 : case REAL_CST:
4025 : 15523407 : {
4026 : 15523407 : unsigned int val2;
4027 : 15523407 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4028 : : val2 = rvc_zero;
4029 : : else
4030 : 15298928 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4031 : 15523407 : hstate.merge_hash (val2);
4032 : 15523407 : return;
4033 : : }
4034 : 0 : case FIXED_CST:
4035 : 0 : {
4036 : 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4037 : 0 : hstate.merge_hash (val2);
4038 : 0 : return;
4039 : : }
4040 : 10518812 : case STRING_CST:
4041 : 10518812 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4042 : 10518812 : TREE_STRING_LENGTH (t));
4043 : 10518812 : return;
4044 : 198 : case RAW_DATA_CST:
4045 : 198 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4046 : 198 : RAW_DATA_LENGTH (t));
4047 : 198 : return;
4048 : 208730 : case COMPLEX_CST:
4049 : 208730 : hash_operand (TREE_REALPART (t), hstate, flags);
4050 : 208730 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4051 : 208730 : return;
4052 : 3017652 : case VECTOR_CST:
4053 : 3017652 : {
4054 : 3017652 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4055 : 3017652 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4056 : 3017652 : unsigned int count = vector_cst_encoded_nelts (t);
4057 : 9489594 : for (unsigned int i = 0; i < count; ++i)
4058 : 6471942 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4059 : : return;
4060 : : }
4061 : 878611698 : case SSA_NAME:
4062 : : /* We can just compare by pointer. */
4063 : 878611698 : hstate.add_hwi (SSA_NAME_VERSION (t));
4064 : 878611698 : return;
4065 : : case PLACEHOLDER_EXPR:
4066 : : /* The node itself doesn't matter. */
4067 : : return;
4068 : : case BLOCK:
4069 : : case OMP_CLAUSE:
4070 : : case OMP_NEXT_VARIANT:
4071 : : case OMP_TARGET_DEVICE_MATCHES:
4072 : : /* Ignore. */
4073 : : return;
4074 : : case TREE_LIST:
4075 : : /* A list of expressions, for a CALL_EXPR or as the elements of a
4076 : : VECTOR_CST. */
4077 : 279718 : for (; t; t = TREE_CHAIN (t))
4078 : 139859 : hash_operand (TREE_VALUE (t), hstate, flags);
4079 : : return;
4080 : 4890273 : case CONSTRUCTOR:
4081 : 4890273 : {
4082 : 4890273 : unsigned HOST_WIDE_INT idx;
4083 : 4890273 : tree field, value;
4084 : 4890273 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4085 : 4890273 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4086 : 19945013 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4087 : : {
4088 : : /* In GIMPLE the indexes can be either NULL or matching i. */
4089 : 15054740 : if (field == NULL_TREE)
4090 : 1090330 : field = bitsize_int (idx);
4091 : 15054740 : if (TREE_CODE (field) == FIELD_DECL)
4092 : : {
4093 : 9920274 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4094 : 9920274 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4095 : : }
4096 : : else
4097 : 5134466 : hash_operand (field, hstate, flags);
4098 : 15054740 : hash_operand (value, hstate, flags);
4099 : : }
4100 : : return;
4101 : : }
4102 : 182 : case STATEMENT_LIST:
4103 : 182 : {
4104 : 182 : tree_stmt_iterator i;
4105 : 182 : for (i = tsi_start (CONST_CAST_TREE (t));
4106 : 550 : !tsi_end_p (i); tsi_next (&i))
4107 : 368 : hash_operand (tsi_stmt (i), hstate, flags);
4108 : 182 : return;
4109 : : }
4110 : : case TREE_VEC:
4111 : 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4112 : 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4113 : : return;
4114 : 4 : case IDENTIFIER_NODE:
4115 : 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4116 : 4 : return;
4117 : 20901793 : case FUNCTION_DECL:
4118 : : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4119 : : Otherwise nodes that compare equal according to operand_equal_p might
4120 : : get different hash codes. However, don't do this for machine specific
4121 : : or front end builtins, since the function code is overloaded in those
4122 : : cases. */
4123 : 20901793 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4124 : 20901793 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4125 : : {
4126 : 7076363 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4127 : 7076363 : code = TREE_CODE (t);
4128 : : }
4129 : : /* FALL THROUGH */
4130 : 1012900681 : default:
4131 : 1012900681 : if (POLY_INT_CST_P (t))
4132 : : {
4133 : : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4134 : : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4135 : : return;
4136 : : }
4137 : 1012900681 : tclass = TREE_CODE_CLASS (code);
4138 : :
4139 : 1012900681 : if (tclass == tcc_declaration)
4140 : : {
4141 : : /* DECL's have a unique ID */
4142 : 684456692 : hstate.add_hwi (DECL_UID (t));
4143 : : }
4144 : 328443989 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4145 : : {
4146 : : /* For comparisons that can be swapped, use the lower
4147 : : tree code. */
4148 : 144075 : enum tree_code ccode = swap_tree_comparison (code);
4149 : 144075 : if (code < ccode)
4150 : 60348 : ccode = code;
4151 : 144075 : hstate.add_object (ccode);
4152 : 144075 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4153 : 144075 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4154 : : }
4155 : 328299914 : else if (CONVERT_EXPR_CODE_P (code))
4156 : : {
4157 : : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4158 : : operand_equal_p. */
4159 : 5172540 : enum tree_code ccode = NOP_EXPR;
4160 : 5172540 : hstate.add_object (ccode);
4161 : :
4162 : : /* Don't hash the type, that can lead to having nodes which
4163 : : compare equal according to operand_equal_p, but which
4164 : : have different hash codes. Make sure to include signedness
4165 : : in the hash computation. */
4166 : 5172540 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4167 : 5172540 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4168 : : }
4169 : : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4170 : 323127374 : else if (code == MEM_REF
4171 : 78361455 : && (flags & OEP_ADDRESS_OF) != 0
4172 : 69340327 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4173 : 13880520 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4174 : 336754063 : && integer_zerop (TREE_OPERAND (t, 1)))
4175 : 6225983 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4176 : : hstate, flags);
4177 : : /* Don't ICE on FE specific trees, or their arguments etc.
4178 : : during operand_equal_p hash verification. */
4179 : 316901391 : else if (!IS_EXPR_CODE_CLASS (tclass))
4180 : 252 : gcc_assert (flags & OEP_HASH_CHECK);
4181 : : else
4182 : : {
4183 : 316901139 : unsigned int sflags = flags;
4184 : :
4185 : 316901139 : hstate.add_object (code);
4186 : :
4187 : 316901139 : switch (code)
4188 : : {
4189 : 126523155 : case ADDR_EXPR:
4190 : 126523155 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4191 : 126523155 : flags |= OEP_ADDRESS_OF;
4192 : 126523155 : sflags = flags;
4193 : 126523155 : break;
4194 : :
4195 : 76579565 : case INDIRECT_REF:
4196 : 76579565 : case MEM_REF:
4197 : 76579565 : case TARGET_MEM_REF:
4198 : 76579565 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4199 : 76579565 : sflags = flags;
4200 : 76579565 : break;
4201 : :
4202 : 74912711 : case COMPONENT_REF:
4203 : 74912711 : if (sflags & OEP_ADDRESS_OF)
4204 : : {
4205 : 37637876 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4206 : 37637876 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4207 : : hstate, flags & ~OEP_ADDRESS_OF);
4208 : 37637876 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4209 : : hstate, flags & ~OEP_ADDRESS_OF);
4210 : 37637876 : return;
4211 : : }
4212 : : break;
4213 : 15244699 : case ARRAY_REF:
4214 : 15244699 : case ARRAY_RANGE_REF:
4215 : 15244699 : case BIT_FIELD_REF:
4216 : 15244699 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4217 : 15244699 : break;
4218 : :
4219 : 8497 : case COND_EXPR:
4220 : 8497 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4221 : 8497 : break;
4222 : :
4223 : 0 : case WIDEN_MULT_PLUS_EXPR:
4224 : 0 : case WIDEN_MULT_MINUS_EXPR:
4225 : 0 : {
4226 : : /* The multiplication operands are commutative. */
4227 : 0 : inchash::hash one, two;
4228 : 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4229 : 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4230 : 0 : hstate.add_commutative (one, two);
4231 : 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4232 : 0 : return;
4233 : : }
4234 : :
4235 : 54421 : case CALL_EXPR:
4236 : 54421 : if (CALL_EXPR_FN (t) == NULL_TREE)
4237 : 6 : hstate.add_int (CALL_EXPR_IFN (t));
4238 : : break;
4239 : :
4240 : 72 : case TARGET_EXPR:
4241 : : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4242 : : Usually different TARGET_EXPRs just should use
4243 : : different temporaries in their slots. */
4244 : 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4245 : 72 : return;
4246 : :
4247 : 325565 : case OBJ_TYPE_REF:
4248 : : /* Virtual table reference. */
4249 : 325565 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4250 : 325565 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4251 : 325565 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4252 : 325565 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4253 : 325565 : if (!virtual_method_call_p (t))
4254 : : return;
4255 : 325550 : if (tree c = obj_type_ref_class (t))
4256 : : {
4257 : 325550 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4258 : : /* We compute mangled names only when free_lang_data is run.
4259 : : In that case we can hash precisely. */
4260 : 325550 : if (TREE_CODE (c) == TYPE_DECL
4261 : 325550 : && DECL_ASSEMBLER_NAME_SET_P (c))
4262 : 6327 : hstate.add_object
4263 : 6327 : (IDENTIFIER_HASH_VALUE
4264 : : (DECL_ASSEMBLER_NAME (c)));
4265 : : }
4266 : 325550 : return;
4267 : : default:
4268 : : break;
4269 : : }
4270 : :
4271 : : /* Don't hash the type, that can lead to having nodes which
4272 : : compare equal according to operand_equal_p, but which
4273 : : have different hash codes. */
4274 : 278937626 : if (code == NON_LVALUE_EXPR)
4275 : : {
4276 : : /* Make sure to include signness in the hash computation. */
4277 : 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4278 : 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4279 : : }
4280 : :
4281 : 278937626 : else if (commutative_tree_code (code))
4282 : : {
4283 : : /* It's a commutative expression. We want to hash it the same
4284 : : however it appears. We do this by first hashing both operands
4285 : : and then rehashing based on the order of their independent
4286 : : hashes. */
4287 : 17257219 : inchash::hash one, two;
4288 : 17257219 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4289 : 17257219 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4290 : 17257219 : hstate.add_commutative (one, two);
4291 : : }
4292 : : else
4293 : 731794693 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4294 : 678548414 : hash_operand (TREE_OPERAND (t, i), hstate,
4295 : : i == 0 ? flags : sflags);
4296 : : }
4297 : : return;
4298 : : }
4299 : : }
4300 : :
4301 : : bool
4302 : 7026317716 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4303 : : unsigned int flags, bool *ret)
4304 : : {
4305 : : /* When checking and unless comparing DECL names, verify that if
4306 : : the outermost operand_equal_p call returns non-zero then ARG0
4307 : : and ARG1 have the same hash value. */
4308 : 7026317716 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4309 : : {
4310 : 2954018658 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4311 : : {
4312 : 448818459 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4313 : : {
4314 : 82111582 : inchash::hash hstate0 (0), hstate1 (0);
4315 : 82111582 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4316 : 82111582 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4317 : 82111582 : hashval_t h0 = hstate0.end ();
4318 : 82111582 : hashval_t h1 = hstate1.end ();
4319 : 82111582 : gcc_assert (h0 == h1);
4320 : : }
4321 : 448818459 : *ret = true;
4322 : : }
4323 : : else
4324 : 2505200199 : *ret = false;
4325 : :
4326 : 2954018658 : return true;
4327 : : }
4328 : :
4329 : : return false;
4330 : : }
4331 : :
4332 : :
4333 : : static operand_compare default_compare_instance;
4334 : :
4335 : : /* Conveinece wrapper around operand_compare class because usually we do
4336 : : not need to play with the valueizer. */
4337 : :
4338 : : bool
4339 : 2951900917 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4340 : : {
4341 : 2951900917 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4342 : : }
4343 : :
4344 : : namespace inchash
4345 : : {
4346 : :
4347 : : /* Generate a hash value for an expression. This can be used iteratively
4348 : : by passing a previous result as the HSTATE argument.
4349 : :
4350 : : This function is intended to produce the same hash for expressions which
4351 : : would compare equal using operand_equal_p. */
4352 : : void
4353 : 1959719493 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4354 : : {
4355 : 1959719493 : default_compare_instance.hash_operand (t, hstate, flags);
4356 : 1959719493 : }
4357 : :
4358 : : }
4359 : :
4360 : : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4361 : : with a different signedness or a narrower precision. */
4362 : :
4363 : : static bool
4364 : 18413620 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4365 : : {
4366 : 18413620 : if (operand_equal_p (arg0, arg1, 0))
4367 : : return true;
4368 : :
4369 : 35116212 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4370 : 29889041 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4371 : : return false;
4372 : :
4373 : : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4374 : : and see if the inner values are the same. This removes any
4375 : : signedness comparison, which doesn't matter here. */
4376 : 5907980 : tree op0 = arg0;
4377 : 5907980 : tree op1 = arg1;
4378 : 5907980 : STRIP_NOPS (op0);
4379 : 5907980 : STRIP_NOPS (op1);
4380 : 5907980 : if (operand_equal_p (op0, op1, 0))
4381 : : return true;
4382 : :
4383 : : /* Discard a single widening conversion from ARG1 and see if the inner
4384 : : value is the same as ARG0. */
4385 : 4945376 : if (CONVERT_EXPR_P (arg1)
4386 : 773690 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4387 : 773644 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4388 : 773644 : < TYPE_PRECISION (TREE_TYPE (arg1))
4389 : 6023804 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4390 : : return true;
4391 : :
4392 : : return false;
4393 : : }
4394 : :
4395 : : /* See if ARG is an expression that is either a comparison or is performing
4396 : : arithmetic on comparisons. The comparisons must only be comparing
4397 : : two different values, which will be stored in *CVAL1 and *CVAL2; if
4398 : : they are nonzero it means that some operands have already been found.
4399 : : No variables may be used anywhere else in the expression except in the
4400 : : comparisons.
4401 : :
4402 : : If this is true, return 1. Otherwise, return zero. */
4403 : :
4404 : : static bool
4405 : 56697936 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4406 : : {
4407 : 60478350 : enum tree_code code = TREE_CODE (arg);
4408 : 60478350 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4409 : :
4410 : : /* We can handle some of the tcc_expression cases here. */
4411 : 60478350 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4412 : : tclass = tcc_unary;
4413 : 59868409 : else if (tclass == tcc_expression
4414 : 615030 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4415 : 615030 : || code == COMPOUND_EXPR))
4416 : : tclass = tcc_binary;
4417 : :
4418 : 59857591 : switch (tclass)
4419 : : {
4420 : 3780414 : case tcc_unary:
4421 : 3780414 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4422 : :
4423 : 5226915 : case tcc_binary:
4424 : 5226915 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4425 : 5226915 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4426 : :
4427 : : case tcc_constant:
4428 : : return true;
4429 : :
4430 : 604212 : case tcc_expression:
4431 : 604212 : if (code == COND_EXPR)
4432 : 724 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4433 : 724 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4434 : 788 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4435 : : return false;
4436 : :
4437 : 614570 : case tcc_comparison:
4438 : : /* First see if we can handle the first operand, then the second. For
4439 : : the second operand, we know *CVAL1 can't be zero. It must be that
4440 : : one side of the comparison is each of the values; test for the
4441 : : case where this isn't true by failing if the two operands
4442 : : are the same. */
4443 : :
4444 : 614570 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4445 : 614570 : TREE_OPERAND (arg, 1), 0))
4446 : : return false;
4447 : :
4448 : 614570 : if (*cval1 == 0)
4449 : 612475 : *cval1 = TREE_OPERAND (arg, 0);
4450 : 2095 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4451 : : ;
4452 : 1976 : else if (*cval2 == 0)
4453 : 0 : *cval2 = TREE_OPERAND (arg, 0);
4454 : 1976 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4455 : : ;
4456 : : else
4457 : : return false;
4458 : :
4459 : 612594 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4460 : : ;
4461 : 612594 : else if (*cval2 == 0)
4462 : 612475 : *cval2 = TREE_OPERAND (arg, 1);
4463 : 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4464 : : ;
4465 : : else
4466 : : return false;
4467 : :
4468 : : return true;
4469 : :
4470 : : default:
4471 : : return false;
4472 : : }
4473 : : }
4474 : :
4475 : : /* ARG is a tree that is known to contain just arithmetic operations and
4476 : : comparisons. Evaluate the operations in the tree substituting NEW0 for
4477 : : any occurrence of OLD0 as an operand of a comparison and likewise for
4478 : : NEW1 and OLD1. */
4479 : :
4480 : : static tree
4481 : 702 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4482 : : tree old1, tree new1)
4483 : : {
4484 : 702 : tree type = TREE_TYPE (arg);
4485 : 702 : enum tree_code code = TREE_CODE (arg);
4486 : 702 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4487 : :
4488 : : /* We can handle some of the tcc_expression cases here. */
4489 : 702 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4490 : : tclass = tcc_unary;
4491 : 702 : else if (tclass == tcc_expression
4492 : 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4493 : : tclass = tcc_binary;
4494 : :
4495 : 693 : switch (tclass)
4496 : : {
4497 : 165 : case tcc_unary:
4498 : 165 : return fold_build1_loc (loc, code, type,
4499 : 165 : eval_subst (loc, TREE_OPERAND (arg, 0),
4500 : 165 : old0, new0, old1, new1));
4501 : :
4502 : 168 : case tcc_binary:
4503 : 336 : return fold_build2_loc (loc, code, type,
4504 : 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4505 : : old0, new0, old1, new1),
4506 : 168 : eval_subst (loc, TREE_OPERAND (arg, 1),
4507 : 168 : old0, new0, old1, new1));
4508 : :
4509 : 9 : case tcc_expression:
4510 : 9 : switch (code)
4511 : : {
4512 : 0 : case SAVE_EXPR:
4513 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4514 : 0 : old1, new1);
4515 : :
4516 : 0 : case COMPOUND_EXPR:
4517 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4518 : 0 : old1, new1);
4519 : :
4520 : 9 : case COND_EXPR:
4521 : 27 : return fold_build3_loc (loc, code, type,
4522 : 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4523 : : old0, new0, old1, new1),
4524 : 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4525 : : old0, new0, old1, new1),
4526 : 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4527 : 9 : old0, new0, old1, new1));
4528 : : default:
4529 : : break;
4530 : : }
4531 : : /* Fall through - ??? */
4532 : :
4533 : 180 : case tcc_comparison:
4534 : 180 : {
4535 : 180 : tree arg0 = TREE_OPERAND (arg, 0);
4536 : 180 : tree arg1 = TREE_OPERAND (arg, 1);
4537 : :
4538 : : /* We need to check both for exact equality and tree equality. The
4539 : : former will be true if the operand has a side-effect. In that
4540 : : case, we know the operand occurred exactly once. */
4541 : :
4542 : 180 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4543 : : arg0 = new0;
4544 : 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4545 : : arg0 = new1;
4546 : :
4547 : 180 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4548 : : arg1 = new0;
4549 : 180 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4550 : : arg1 = new1;
4551 : :
4552 : 180 : return fold_build2_loc (loc, code, type, arg0, arg1);
4553 : : }
4554 : :
4555 : : default:
4556 : : return arg;
4557 : : }
4558 : : }
4559 : :
4560 : : /* Return a tree for the case when the result of an expression is RESULT
4561 : : converted to TYPE and OMITTED was previously an operand of the expression
4562 : : but is now not needed (e.g., we folded OMITTED * 0).
4563 : :
4564 : : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4565 : : the conversion of RESULT to TYPE. */
4566 : :
4567 : : tree
4568 : 284559 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4569 : : {
4570 : 284559 : tree t = fold_convert_loc (loc, type, result);
4571 : :
4572 : : /* If the resulting operand is an empty statement, just return the omitted
4573 : : statement casted to void. */
4574 : 284559 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4575 : 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4576 : 0 : fold_ignored_result (omitted));
4577 : :
4578 : 284559 : if (TREE_SIDE_EFFECTS (omitted))
4579 : 13822 : return build2_loc (loc, COMPOUND_EXPR, type,
4580 : 13822 : fold_ignored_result (omitted), t);
4581 : :
4582 : 270737 : return non_lvalue_loc (loc, t);
4583 : : }
4584 : :
4585 : : /* Return a tree for the case when the result of an expression is RESULT
4586 : : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4587 : : of the expression but are now not needed.
4588 : :
4589 : : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4590 : : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4591 : : evaluated before OMITTED2. Otherwise, if neither has side effects,
4592 : : just do the conversion of RESULT to TYPE. */
4593 : :
4594 : : tree
4595 : 5564 : omit_two_operands_loc (location_t loc, tree type, tree result,
4596 : : tree omitted1, tree omitted2)
4597 : : {
4598 : 5564 : tree t = fold_convert_loc (loc, type, result);
4599 : :
4600 : 5564 : if (TREE_SIDE_EFFECTS (omitted2))
4601 : 72 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4602 : 5564 : if (TREE_SIDE_EFFECTS (omitted1))
4603 : 179 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4604 : :
4605 : 5564 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4606 : : }
4607 : :
4608 : :
4609 : : /* Return a simplified tree node for the truth-negation of ARG. This
4610 : : never alters ARG itself. We assume that ARG is an operation that
4611 : : returns a truth value (0 or 1).
4612 : :
4613 : : FIXME: one would think we would fold the result, but it causes
4614 : : problems with the dominator optimizer. */
4615 : :
4616 : : static tree
4617 : 48514856 : fold_truth_not_expr (location_t loc, tree arg)
4618 : : {
4619 : 48514856 : tree type = TREE_TYPE (arg);
4620 : 48514856 : enum tree_code code = TREE_CODE (arg);
4621 : 48514856 : location_t loc1, loc2;
4622 : :
4623 : : /* If this is a comparison, we can simply invert it, except for
4624 : : floating-point non-equality comparisons, in which case we just
4625 : : enclose a TRUTH_NOT_EXPR around what we have. */
4626 : :
4627 : 48514856 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4628 : : {
4629 : 37771866 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4630 : 31708855 : if (FLOAT_TYPE_P (op_type)
4631 : 6073098 : && flag_trapping_math
4632 : 6043063 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4633 : 43774417 : && code != NE_EXPR && code != EQ_EXPR)
4634 : : return NULL_TREE;
4635 : :
4636 : 32464558 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4637 : 32464558 : if (code == ERROR_MARK)
4638 : : return NULL_TREE;
4639 : :
4640 : 32464558 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4641 : 32464558 : TREE_OPERAND (arg, 1));
4642 : 32464558 : copy_warning (ret, arg);
4643 : 32464558 : return ret;
4644 : : }
4645 : :
4646 : 10742990 : switch (code)
4647 : : {
4648 : 0 : case INTEGER_CST:
4649 : 0 : return constant_boolean_node (integer_zerop (arg), type);
4650 : :
4651 : 48628 : case TRUTH_AND_EXPR:
4652 : 48628 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4653 : 48628 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4654 : 97256 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4655 : 48628 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4656 : 97256 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4657 : :
4658 : 2448 : case TRUTH_OR_EXPR:
4659 : 2448 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4660 : 2448 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4661 : 4896 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4662 : 2448 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4663 : 4896 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4664 : :
4665 : 36371 : case TRUTH_XOR_EXPR:
4666 : : /* Here we can invert either operand. We invert the first operand
4667 : : unless the second operand is a TRUTH_NOT_EXPR in which case our
4668 : : result is the XOR of the first operand with the inside of the
4669 : : negation of the second operand. */
4670 : :
4671 : 36371 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4672 : 35 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4673 : 70 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4674 : : else
4675 : 36336 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4676 : 36336 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4677 : 72672 : TREE_OPERAND (arg, 1));
4678 : :
4679 : 253698 : case TRUTH_ANDIF_EXPR:
4680 : 253698 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4681 : 253698 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4682 : 507396 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4683 : 253698 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4684 : 507396 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4685 : :
4686 : 19476 : case TRUTH_ORIF_EXPR:
4687 : 19476 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4688 : 19476 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4689 : 38952 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4690 : 19476 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4691 : 38952 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4692 : :
4693 : 782812 : case TRUTH_NOT_EXPR:
4694 : 782812 : return TREE_OPERAND (arg, 0);
4695 : :
4696 : 9751 : case COND_EXPR:
4697 : 9751 : {
4698 : 9751 : tree arg1 = TREE_OPERAND (arg, 1);
4699 : 9751 : tree arg2 = TREE_OPERAND (arg, 2);
4700 : :
4701 : 9751 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4702 : 9751 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4703 : :
4704 : : /* A COND_EXPR may have a throw as one operand, which
4705 : : then has void type. Just leave void operands
4706 : : as they are. */
4707 : 9751 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4708 : 9751 : VOID_TYPE_P (TREE_TYPE (arg1))
4709 : 9751 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4710 : 9751 : VOID_TYPE_P (TREE_TYPE (arg2))
4711 : 19499 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4712 : : }
4713 : :
4714 : 374 : case COMPOUND_EXPR:
4715 : 374 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4716 : 748 : return build2_loc (loc, COMPOUND_EXPR, type,
4717 : 374 : TREE_OPERAND (arg, 0),
4718 : 748 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4719 : :
4720 : 0 : case NON_LVALUE_EXPR:
4721 : 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4722 : 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4723 : :
4724 : 72084 : CASE_CONVERT:
4725 : 72084 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4726 : 72020 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4727 : :
4728 : : /* fall through */
4729 : :
4730 : 64 : case FLOAT_EXPR:
4731 : 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4732 : 64 : return build1_loc (loc, TREE_CODE (arg), type,
4733 : 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4734 : :
4735 : 503 : case BIT_AND_EXPR:
4736 : 503 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4737 : : return NULL_TREE;
4738 : 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4739 : :
4740 : 2 : case SAVE_EXPR:
4741 : 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4742 : :
4743 : 75 : case CLEANUP_POINT_EXPR:
4744 : 75 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4745 : 75 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4746 : 150 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4747 : :
4748 : : default:
4749 : : return NULL_TREE;
4750 : : }
4751 : : }
4752 : :
4753 : : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4754 : : assume that ARG is an operation that returns a truth value (0 or 1
4755 : : for scalars, 0 or -1 for vectors). Return the folded expression if
4756 : : folding is successful. Otherwise, return NULL_TREE. */
4757 : :
4758 : : static tree
4759 : 1806217 : fold_invert_truthvalue (location_t loc, tree arg)
4760 : : {
4761 : 1806217 : tree type = TREE_TYPE (arg);
4762 : 3612410 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4763 : : ? BIT_NOT_EXPR
4764 : : : TRUTH_NOT_EXPR,
4765 : 1806217 : type, arg);
4766 : : }
4767 : :
4768 : : /* Return a simplified tree node for the truth-negation of ARG. This
4769 : : never alters ARG itself. We assume that ARG is an operation that
4770 : : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4771 : :
4772 : : tree
4773 : 41076049 : invert_truthvalue_loc (location_t loc, tree arg)
4774 : : {
4775 : 41076049 : if (TREE_CODE (arg) == ERROR_MARK)
4776 : : return arg;
4777 : :
4778 : 41076049 : tree type = TREE_TYPE (arg);
4779 : 82152098 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4780 : : ? BIT_NOT_EXPR
4781 : : : TRUTH_NOT_EXPR,
4782 : 41076049 : type, arg);
4783 : : }
4784 : :
4785 : : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4786 : : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4787 : : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4788 : : is the original memory reference used to preserve the alias set of
4789 : : the access. */
4790 : :
4791 : : tree
4792 : 884282 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4793 : : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4794 : : int unsignedp, int reversep)
4795 : : {
4796 : 884282 : tree result, bftype;
4797 : :
4798 : : /* Attempt not to lose the access path if possible. */
4799 : 884282 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4800 : : {
4801 : 880356 : tree ninner = TREE_OPERAND (orig_inner, 0);
4802 : 880356 : machine_mode nmode;
4803 : 880356 : poly_int64 nbitsize, nbitpos;
4804 : 880356 : tree noffset;
4805 : 880356 : int nunsignedp, nreversep, nvolatilep = 0;
4806 : 880356 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4807 : : &noffset, &nmode, &nunsignedp,
4808 : : &nreversep, &nvolatilep);
4809 : 880356 : if (base == inner
4810 : 880224 : && noffset == NULL_TREE
4811 : 880224 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4812 : 880202 : && !reversep
4813 : 880130 : && !nreversep
4814 : 1760486 : && !nvolatilep)
4815 : : {
4816 : 880130 : inner = ninner;
4817 : 880356 : bitpos -= nbitpos;
4818 : : }
4819 : : }
4820 : :
4821 : 884282 : alias_set_type iset = get_alias_set (orig_inner);
4822 : 884282 : if (iset == 0 && get_alias_set (inner) != iset)
4823 : 229 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4824 : : build_fold_addr_expr (inner),
4825 : : build_int_cst (ptr_type_node, 0));
4826 : :
4827 : 884282 : if (known_eq (bitpos, 0) && !reversep)
4828 : : {
4829 : 18902 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4830 : 37804 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4831 : 18698 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4832 : 208 : && tree_fits_shwi_p (size)
4833 : 19110 : && tree_to_shwi (size) == bitsize)
4834 : 185 : return fold_convert_loc (loc, type, inner);
4835 : : }
4836 : :
4837 : 884097 : bftype = type;
4838 : 884097 : if (TYPE_PRECISION (bftype) != bitsize
4839 : 884097 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4840 : 389 : bftype = build_nonstandard_integer_type (bitsize, 0);
4841 : :
4842 : 884097 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4843 : 884097 : bitsize_int (bitsize), bitsize_int (bitpos));
4844 : 884097 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4845 : :
4846 : 884097 : if (bftype != type)
4847 : 389 : result = fold_convert_loc (loc, type, result);
4848 : :
4849 : : return result;
4850 : : }
4851 : :
4852 : : /* Optimize a bit-field compare.
4853 : :
4854 : : There are two cases: First is a compare against a constant and the
4855 : : second is a comparison of two items where the fields are at the same
4856 : : bit position relative to the start of a chunk (byte, halfword, word)
4857 : : large enough to contain it. In these cases we can avoid the shift
4858 : : implicit in bitfield extractions.
4859 : :
4860 : : For constants, we emit a compare of the shifted constant with the
4861 : : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4862 : : compared. For two fields at the same position, we do the ANDs with the
4863 : : similar mask and compare the result of the ANDs.
4864 : :
4865 : : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4866 : : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4867 : : are the left and right operands of the comparison, respectively.
4868 : :
4869 : : If the optimization described above can be done, we return the resulting
4870 : : tree. Otherwise we return zero. */
4871 : :
4872 : : static tree
4873 : 4327076 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4874 : : tree compare_type, tree lhs, tree rhs)
4875 : : {
4876 : 4327076 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4877 : 4327076 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4878 : 4327076 : tree type = TREE_TYPE (lhs);
4879 : 4327076 : tree unsigned_type;
4880 : 4327076 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4881 : 4327076 : machine_mode lmode, rmode;
4882 : 4327076 : scalar_int_mode nmode;
4883 : 4327076 : int lunsignedp, runsignedp;
4884 : 4327076 : int lreversep, rreversep;
4885 : 4327076 : int lvolatilep = 0, rvolatilep = 0;
4886 : 4327076 : tree linner, rinner = NULL_TREE;
4887 : 4327076 : tree mask;
4888 : 4327076 : tree offset;
4889 : :
4890 : : /* Get all the information about the extractions being done. If the bit size
4891 : : is the same as the size of the underlying object, we aren't doing an
4892 : : extraction at all and so can do nothing. We also don't want to
4893 : : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4894 : : then will no longer be able to replace it. */
4895 : 4327076 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4896 : : &lunsignedp, &lreversep, &lvolatilep);
4897 : 4327076 : if (linner == lhs
4898 : 4327076 : || !known_size_p (plbitsize)
4899 : 4327076 : || !plbitsize.is_constant (&lbitsize)
4900 : 4327076 : || !plbitpos.is_constant (&lbitpos)
4901 : 8654152 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4902 : 866747 : || offset != 0
4903 : 866722 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4904 : 5193798 : || lvolatilep)
4905 : 3460414 : return 0;
4906 : :
4907 : 866662 : if (const_p)
4908 : 850058 : rreversep = lreversep;
4909 : : else
4910 : : {
4911 : : /* If this is not a constant, we can only do something if bit positions,
4912 : : sizes, signedness and storage order are the same. */
4913 : 16604 : rinner
4914 : 16604 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4915 : : &runsignedp, &rreversep, &rvolatilep);
4916 : :
4917 : 16604 : if (rinner == rhs
4918 : 16560 : || maybe_ne (lbitpos, rbitpos)
4919 : 16526 : || maybe_ne (lbitsize, rbitsize)
4920 : 16526 : || lunsignedp != runsignedp
4921 : 16526 : || lreversep != rreversep
4922 : 16526 : || offset != 0
4923 : 16526 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4924 : 33130 : || rvolatilep)
4925 : : return 0;
4926 : : }
4927 : :
4928 : : /* Honor the C++ memory model and mimic what RTL expansion does. */
4929 : 866584 : poly_uint64 bitstart = 0;
4930 : 866584 : poly_uint64 bitend = 0;
4931 : 866584 : if (TREE_CODE (lhs) == COMPONENT_REF)
4932 : : {
4933 : 866584 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4934 : 866584 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4935 : : return 0;
4936 : : }
4937 : :
4938 : : /* See if we can find a mode to refer to this field. We should be able to,
4939 : : but fail if we can't. */
4940 : 1733168 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4941 : 850058 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4942 : 16526 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4943 : : TYPE_ALIGN (TREE_TYPE (rinner))),
4944 : 866584 : BITS_PER_WORD, false, &nmode))
4945 : : return 0;
4946 : :
4947 : : /* Set signed and unsigned types of the precision of this mode for the
4948 : : shifts below. */
4949 : 864587 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4950 : :
4951 : : /* Compute the bit position and size for the new reference and our offset
4952 : : within it. If the new reference is the same size as the original, we
4953 : : won't optimize anything, so return zero. */
4954 : 864587 : nbitsize = GET_MODE_BITSIZE (nmode);
4955 : 864587 : nbitpos = lbitpos & ~ (nbitsize - 1);
4956 : 864587 : lbitpos -= nbitpos;
4957 : 864587 : if (nbitsize == lbitsize)
4958 : : return 0;
4959 : :
4960 : 864587 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4961 : 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4962 : :
4963 : : /* Make the mask to be used against the extracted field. */
4964 : 864587 : mask = build_int_cst_type (unsigned_type, -1);
4965 : 864587 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4966 : 864587 : mask = const_binop (RSHIFT_EXPR, mask,
4967 : 864587 : size_int (nbitsize - lbitsize - lbitpos));
4968 : :
4969 : 864587 : if (! const_p)
4970 : : {
4971 : 14971 : if (nbitpos < 0)
4972 : : return 0;
4973 : :
4974 : : /* If not comparing with constant, just rework the comparison
4975 : : and return. */
4976 : 14971 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4977 : 14971 : nbitsize, nbitpos, 1, lreversep);
4978 : 14971 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4979 : 14971 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4980 : 14971 : nbitsize, nbitpos, 1, rreversep);
4981 : 14971 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4982 : 14971 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4983 : : }
4984 : :
4985 : : /* Otherwise, we are handling the constant case. See if the constant is too
4986 : : big for the field. Warn and return a tree for 0 (false) if so. We do
4987 : : this not only for its own sake, but to avoid having to test for this
4988 : : error case below. If we didn't, we might generate wrong code.
4989 : :
4990 : : For unsigned fields, the constant shifted right by the field length should
4991 : : be all zero. For signed fields, the high-order bits should agree with
4992 : : the sign bit. */
4993 : :
4994 : 849616 : if (lunsignedp)
4995 : : {
4996 : 848497 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4997 : : {
4998 : 0 : warning (0, "comparison is always %d due to width of bit-field",
4999 : : code == NE_EXPR);
5000 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5001 : : }
5002 : : }
5003 : : else
5004 : : {
5005 : 1119 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5006 : 1119 : if (tem != 0 && tem != -1)
5007 : : {
5008 : 0 : warning (0, "comparison is always %d due to width of bit-field",
5009 : : code == NE_EXPR);
5010 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5011 : : }
5012 : 1119 : }
5013 : :
5014 : 849616 : if (nbitpos < 0)
5015 : : return 0;
5016 : :
5017 : : /* Single-bit compares should always be against zero. */
5018 : 849616 : if (lbitsize == 1 && ! integer_zerop (rhs))
5019 : : {
5020 : 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5021 : 175 : rhs = build_int_cst (type, 0);
5022 : : }
5023 : :
5024 : : /* Make a new bitfield reference, shift the constant over the
5025 : : appropriate number of bits and mask it with the computed mask
5026 : : (in case this was a signed field). If we changed it, make a new one. */
5027 : 849616 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5028 : 849616 : nbitsize, nbitpos, 1, lreversep);
5029 : :
5030 : 849616 : rhs = const_binop (BIT_AND_EXPR,
5031 : : const_binop (LSHIFT_EXPR,
5032 : : fold_convert_loc (loc, unsigned_type, rhs),
5033 : 849616 : size_int (lbitpos)),
5034 : : mask);
5035 : :
5036 : 849616 : lhs = build2_loc (loc, code, compare_type,
5037 : : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5038 : 849616 : return lhs;
5039 : : }
5040 : :
5041 : : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5042 : : represents the sign bit of EXP's type. If EXP represents a sign
5043 : : or zero extension, also test VAL against the unextended type.
5044 : : The return value is the (sub)expression whose sign bit is VAL,
5045 : : or NULL_TREE otherwise. */
5046 : :
5047 : : tree
5048 : 2155 : sign_bit_p (tree exp, const_tree val)
5049 : : {
5050 : 2155 : int width;
5051 : 2155 : tree t;
5052 : :
5053 : : /* Tree EXP must have an integral type. */
5054 : 2155 : t = TREE_TYPE (exp);
5055 : 2155 : if (! INTEGRAL_TYPE_P (t))
5056 : : return NULL_TREE;
5057 : :
5058 : : /* Tree VAL must be an integer constant. */
5059 : 1823 : if (TREE_CODE (val) != INTEGER_CST
5060 : 1823 : || TREE_OVERFLOW (val))
5061 : : return NULL_TREE;
5062 : :
5063 : 1451 : width = TYPE_PRECISION (t);
5064 : 1451 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5065 : : return exp;
5066 : :
5067 : : /* Handle extension from a narrower type. */
5068 : 814 : if (TREE_CODE (exp) == NOP_EXPR
5069 : 814 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5070 : 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5071 : :
5072 : : return NULL_TREE;
5073 : : }
5074 : :
5075 : : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5076 : : operand is simple enough to be evaluated unconditionally. */
5077 : :
5078 : : static bool
5079 : 62911246 : simple_operand_p (const_tree exp)
5080 : : {
5081 : : /* Strip any conversions that don't change the machine mode. */
5082 : 62911246 : STRIP_NOPS (exp);
5083 : :
5084 : 62911246 : return (CONSTANT_CLASS_P (exp)
5085 : 43606780 : || TREE_CODE (exp) == SSA_NAME
5086 : 77610826 : || (DECL_P (exp)
5087 : 4719677 : && ! TREE_ADDRESSABLE (exp)
5088 : 4632089 : && ! TREE_THIS_VOLATILE (exp)
5089 : 4632089 : && ! DECL_NONLOCAL (exp)
5090 : : /* Don't regard global variables as simple. They may be
5091 : : allocated in ways unknown to the compiler (shared memory,
5092 : : #pragma weak, etc). */
5093 : 4630637 : && ! TREE_PUBLIC (exp)
5094 : 4609991 : && ! DECL_EXTERNAL (exp)
5095 : : /* DECL_VALUE_EXPR will expand to something non-simple. */
5096 : 4609991 : && ! ((VAR_P (exp)
5097 : : || TREE_CODE (exp) == PARM_DECL
5098 : : || TREE_CODE (exp) == RESULT_DECL)
5099 : 4609991 : && DECL_HAS_VALUE_EXPR_P (exp))
5100 : : /* Weakrefs are not safe to be read, since they can be NULL.
5101 : : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5102 : : have DECL_WEAK flag set. */
5103 : 4609340 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5104 : : /* Loading a static variable is unduly expensive, but global
5105 : : registers aren't expensive. */
5106 : 4609340 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5107 : : }
5108 : :
5109 : : /* Determine if an operand is simple enough to be evaluated unconditionally.
5110 : : In addition to simple_operand_p, we assume that comparisons, conversions,
5111 : : and logic-not operations are simple, if their operands are simple, too. */
5112 : :
5113 : : bool
5114 : 6116296 : simple_condition_p (tree exp)
5115 : : {
5116 : 6191694 : enum tree_code code;
5117 : :
5118 : 6191694 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5119 : 4413694 : return false;
5120 : :
5121 : 1791253 : while (CONVERT_EXPR_P (exp))
5122 : 13253 : exp = TREE_OPERAND (exp, 0);
5123 : :
5124 : 1778000 : code = TREE_CODE (exp);
5125 : :
5126 : 1778000 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5127 : 1354004 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5128 : 1354004 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5129 : :
5130 : 423996 : if (code == TRUTH_NOT_EXPR)
5131 : 75398 : return simple_condition_p (TREE_OPERAND (exp, 0));
5132 : :
5133 : 348598 : return simple_operand_p (exp);
5134 : : }
5135 : :
5136 : :
5137 : : /* The following functions are subroutines to fold_range_test and allow it to
5138 : : try to change a logical combination of comparisons into a range test.
5139 : :
5140 : : For example, both
5141 : : X == 2 || X == 3 || X == 4 || X == 5
5142 : : and
5143 : : X >= 2 && X <= 5
5144 : : are converted to
5145 : : (unsigned) (X - 2) <= 3
5146 : :
5147 : : We describe each set of comparisons as being either inside or outside
5148 : : a range, using a variable named like IN_P, and then describe the
5149 : : range with a lower and upper bound. If one of the bounds is omitted,
5150 : : it represents either the highest or lowest value of the type.
5151 : :
5152 : : In the comments below, we represent a range by two numbers in brackets
5153 : : preceded by a "+" to designate being inside that range, or a "-" to
5154 : : designate being outside that range, so the condition can be inverted by
5155 : : flipping the prefix. An omitted bound is represented by a "-". For
5156 : : example, "- [-, 10]" means being outside the range starting at the lowest
5157 : : possible value and ending at 10, in other words, being greater than 10.
5158 : : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5159 : : always false.
5160 : :
5161 : : We set up things so that the missing bounds are handled in a consistent
5162 : : manner so neither a missing bound nor "true" and "false" need to be
5163 : : handled using a special case. */
5164 : :
5165 : : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5166 : : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5167 : : and UPPER1_P are nonzero if the respective argument is an upper bound
5168 : : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5169 : : must be specified for a comparison. ARG1 will be converted to ARG0's
5170 : : type if both are specified. */
5171 : :
5172 : : static tree
5173 : 21893656 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5174 : : tree arg1, int upper1_p)
5175 : : {
5176 : 21893656 : tree tem;
5177 : 21893656 : int result;
5178 : 21893656 : int sgn0, sgn1;
5179 : :
5180 : : /* If neither arg represents infinity, do the normal operation.
5181 : : Else, if not a comparison, return infinity. Else handle the special
5182 : : comparison rules. Note that most of the cases below won't occur, but
5183 : : are handled for consistency. */
5184 : :
5185 : 21893656 : if (arg0 != 0 && arg1 != 0)
5186 : : {
5187 : 11453767 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5188 : : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5189 : 11453767 : STRIP_NOPS (tem);
5190 : 11453767 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5191 : : }
5192 : :
5193 : 10439889 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5194 : : return 0;
5195 : :
5196 : : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5197 : : for neither. In real maths, we cannot assume open ended ranges are
5198 : : the same. But, this is computer arithmetic, where numbers are finite.
5199 : : We can therefore make the transformation of any unbounded range with
5200 : : the value Z, Z being greater than any representable number. This permits
5201 : : us to treat unbounded ranges as equal. */
5202 : 10431452 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5203 : 10431452 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5204 : 10431452 : switch (code)
5205 : : {
5206 : 4879610 : case EQ_EXPR:
5207 : 4879610 : result = sgn0 == sgn1;
5208 : 4879610 : break;
5209 : 0 : case NE_EXPR:
5210 : 0 : result = sgn0 != sgn1;
5211 : 0 : break;
5212 : 377625 : case LT_EXPR:
5213 : 377625 : result = sgn0 < sgn1;
5214 : 377625 : break;
5215 : 2373704 : case LE_EXPR:
5216 : 2373704 : result = sgn0 <= sgn1;
5217 : 2373704 : break;
5218 : 2800513 : case GT_EXPR:
5219 : 2800513 : result = sgn0 > sgn1;
5220 : 2800513 : break;
5221 : 0 : case GE_EXPR:
5222 : 0 : result = sgn0 >= sgn1;
5223 : 0 : break;
5224 : 0 : default:
5225 : 0 : gcc_unreachable ();
5226 : : }
5227 : :
5228 : 10431452 : return constant_boolean_node (result, type);
5229 : : }
5230 : :
5231 : : /* Helper routine for make_range. Perform one step for it, return
5232 : : new expression if the loop should continue or NULL_TREE if it should
5233 : : stop. */
5234 : :
5235 : : tree
5236 : 58116522 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5237 : : tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
5238 : : bool *strict_overflow_p)
5239 : : {
5240 : 58116522 : tree arg0_type = TREE_TYPE (arg0);
5241 : 58116522 : tree n_low, n_high, low = *p_low, high = *p_high;
5242 : 58116522 : int in_p = *p_in_p, n_in_p;
5243 : :
5244 : 58116522 : switch (code)
5245 : : {
5246 : 1659073 : case TRUTH_NOT_EXPR:
5247 : : /* We can only do something if the range is testing for zero. */
5248 : 1659073 : if (low == NULL_TREE || high == NULL_TREE
5249 : 1659073 : || ! integer_zerop (low) || ! integer_zerop (high))
5250 : 0 : return NULL_TREE;
5251 : 1659073 : *p_in_p = ! in_p;
5252 : 1659073 : return arg0;
5253 : :
5254 : 45634472 : case EQ_EXPR: case NE_EXPR:
5255 : 45634472 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5256 : : /* We can only do something if the range is testing for zero
5257 : : and if the second operand is an integer constant. Note that
5258 : : saying something is "in" the range we make is done by
5259 : : complementing IN_P since it will set in the initial case of
5260 : : being not equal to zero; "out" is leaving it alone. */
5261 : 45634472 : if (low == NULL_TREE || high == NULL_TREE
5262 : 45634472 : || ! integer_zerop (low) || ! integer_zerop (high)
5263 : 91268857 : || TREE_CODE (arg1) != INTEGER_CST)
5264 : 16947104 : return NULL_TREE;
5265 : :
5266 : 28687368 : switch (code)
5267 : : {
5268 : : case NE_EXPR: /* - [c, c] */
5269 : : low = high = arg1;
5270 : : break;
5271 : 7896352 : case EQ_EXPR: /* + [c, c] */
5272 : 7896352 : in_p = ! in_p, low = high = arg1;
5273 : 7896352 : break;
5274 : 2149601 : case GT_EXPR: /* - [-, c] */
5275 : 2149601 : low = 0, high = arg1;
5276 : 2149601 : break;
5277 : 732707 : case GE_EXPR: /* + [c, -] */
5278 : 732707 : in_p = ! in_p, low = arg1, high = 0;
5279 : 732707 : break;
5280 : 5453147 : case LT_EXPR: /* - [c, -] */
5281 : 5453147 : low = arg1, high = 0;
5282 : 5453147 : break;
5283 : 4339817 : case LE_EXPR: /* + [-, c] */
5284 : 4339817 : in_p = ! in_p, low = 0, high = arg1;
5285 : 4339817 : break;
5286 : 0 : default:
5287 : 0 : gcc_unreachable ();
5288 : : }
5289 : :
5290 : : /* If this is an unsigned comparison, we also know that EXP is
5291 : : greater than or equal to zero. We base the range tests we make
5292 : : on that fact, so we record it here so we can parse existing
5293 : : range tests. We test arg0_type since often the return type
5294 : : of, e.g. EQ_EXPR, is boolean. */
5295 : 28687368 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5296 : : {
5297 : 1835306 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5298 : : in_p, low, high, 1,
5299 : : build_int_cst (arg0_type, 0),
5300 : : NULL_TREE))
5301 : : return NULL_TREE;
5302 : :
5303 : 1835297 : in_p = n_in_p, low = n_low, high = n_high;
5304 : :
5305 : : /* If the high bound is missing, but we have a nonzero low
5306 : : bound, reverse the range so it goes from zero to the low bound
5307 : : minus 1. */
5308 : 1835297 : if (high == 0 && low && ! integer_zerop (low))
5309 : : {
5310 : 809979 : in_p = ! in_p;
5311 : 809979 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5312 : 809979 : build_int_cst (TREE_TYPE (low), 1), 0);
5313 : 809979 : low = build_int_cst (arg0_type, 0);
5314 : : }
5315 : : }
5316 : :
5317 : 28687359 : *p_low = low;
5318 : 28687359 : *p_high = high;
5319 : 28687359 : *p_in_p = in_p;
5320 : 28687359 : return arg0;
5321 : :
5322 : 240 : case NEGATE_EXPR:
5323 : : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5324 : : low and high are non-NULL, then normalize will DTRT. */
5325 : 240 : if (!TYPE_UNSIGNED (arg0_type)
5326 : 240 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5327 : : {
5328 : 95 : if (low == NULL_TREE)
5329 : 12 : low = TYPE_MIN_VALUE (arg0_type);
5330 : 95 : if (high == NULL_TREE)
5331 : 47 : high = TYPE_MAX_VALUE (arg0_type);
5332 : : }
5333 : :
5334 : : /* (-x) IN [a,b] -> x in [-b, -a] */
5335 : 240 : n_low = range_binop (MINUS_EXPR, exp_type,
5336 : : build_int_cst (exp_type, 0),
5337 : : 0, high, 1);
5338 : 240 : n_high = range_binop (MINUS_EXPR, exp_type,
5339 : : build_int_cst (exp_type, 0),
5340 : : 0, low, 0);
5341 : 240 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5342 : : return NULL_TREE;
5343 : 228 : goto normalize;
5344 : :
5345 : 12 : case BIT_NOT_EXPR:
5346 : : /* ~ X -> -X - 1 */
5347 : 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5348 : : build_int_cst (exp_type, 1));
5349 : :
5350 : 868417 : case PLUS_EXPR:
5351 : 868417 : case MINUS_EXPR:
5352 : 868417 : if (TREE_CODE (arg1) != INTEGER_CST)
5353 : : return NULL_TREE;
5354 : :
5355 : : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5356 : : move a constant to the other side. */
5357 : 662829 : if (!TYPE_UNSIGNED (arg0_type)
5358 : 662829 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5359 : : return NULL_TREE;
5360 : :
5361 : : /* If EXP is signed, any overflow in the computation is undefined,
5362 : : so we don't worry about it so long as our computations on
5363 : : the bounds don't overflow. For unsigned, overflow is defined
5364 : : and this is exactly the right thing. */
5365 : 932782 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5366 : : arg0_type, low, 0, arg1, 0);
5367 : 467885 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5368 : : arg0_type, high, 1, arg1, 0);
5369 : 464966 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5370 : 932839 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5371 : : return NULL_TREE;
5372 : :
5373 : 467873 : if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5374 : 21868 : *strict_overflow_p = true;
5375 : :
5376 : 0 : normalize:
5377 : : /* Check for an unsigned range which has wrapped around the maximum
5378 : : value thus making n_high < n_low, and normalize it. */
5379 : 468101 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5380 : : {
5381 : 134418 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5382 : 134418 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5383 : 134418 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5384 : 134418 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5385 : :
5386 : : /* If the range is of the form +/- [ x+1, x ], we won't
5387 : : be able to normalize it. But then, it represents the
5388 : : whole range or the empty set, so make it
5389 : : +/- [ -, - ]. */
5390 : 134418 : if (tree_int_cst_equal (n_low, low)
5391 : 134418 : && tree_int_cst_equal (n_high, high))
5392 : : low = high = 0;
5393 : : else
5394 : 134418 : in_p = ! in_p;
5395 : : }
5396 : : else
5397 : 333683 : low = n_low, high = n_high;
5398 : :
5399 : 468101 : *p_low = low;
5400 : 468101 : *p_high = high;
5401 : 468101 : *p_in_p = in_p;
5402 : 468101 : return arg0;
5403 : :
5404 : 2563576 : CASE_CONVERT:
5405 : 2563576 : case NON_LVALUE_EXPR:
5406 : 2563576 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5407 : : return NULL_TREE;
5408 : :
5409 : 1130092 : if (! INTEGRAL_TYPE_P (arg0_type)
5410 : 1095518 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5411 : 987170 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5412 : : return NULL_TREE;
5413 : :
5414 : 966451 : n_low = low, n_high = high;
5415 : :
5416 : 966451 : if (n_low != 0)
5417 : 801076 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5418 : :
5419 : 966451 : if (n_high != 0)
5420 : 894606 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5421 : :
5422 : : /* If we're converting arg0 from an unsigned type, to exp,
5423 : : a signed type, we will be doing the comparison as unsigned.
5424 : : The tests above have already verified that LOW and HIGH
5425 : : are both positive.
5426 : :
5427 : : So we have to ensure that we will handle large unsigned
5428 : : values the same way that the current signed bounds treat
5429 : : negative values. */
5430 : :
5431 : 966451 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5432 : : {
5433 : 261950 : tree high_positive;
5434 : 261950 : tree equiv_type;
5435 : : /* For fixed-point modes, we need to pass the saturating flag
5436 : : as the 2nd parameter. */
5437 : 261950 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5438 : 0 : equiv_type
5439 : 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5440 : 0 : TYPE_SATURATING (arg0_type));
5441 : 261950 : else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5442 : : equiv_type = arg0_type;
5443 : : else
5444 : 261942 : equiv_type
5445 : 261942 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5446 : :
5447 : : /* A range without an upper bound is, naturally, unbounded.
5448 : : Since convert would have cropped a very large value, use
5449 : : the max value for the destination type. */
5450 : 261950 : high_positive
5451 : 261950 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5452 : 0 : : TYPE_MAX_VALUE (arg0_type);
5453 : :
5454 : 261950 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5455 : 241253 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5456 : : fold_convert_loc (loc, arg0_type,
5457 : : high_positive),
5458 : : build_int_cst (arg0_type, 1));
5459 : :
5460 : : /* If the low bound is specified, "and" the range with the
5461 : : range for which the original unsigned value will be
5462 : : positive. */
5463 : 261950 : if (low != 0)
5464 : : {
5465 : 101955 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, 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 : 101955 : in_p = (n_in_p == in_p);
5472 : : }
5473 : : else
5474 : : {
5475 : : /* Otherwise, "or" the range with the range of the input
5476 : : that will be interpreted as negative. */
5477 : 159995 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5478 : : 1, fold_convert_loc (loc, arg0_type,
5479 : : integer_zero_node),
5480 : : high_positive))
5481 : : return NULL_TREE;
5482 : :
5483 : 159995 : in_p = (in_p != n_in_p);
5484 : : }
5485 : : }
5486 : :
5487 : : /* Otherwise, if we are converting arg0 from signed type, to exp,
5488 : : an unsigned type, we will do the comparison as signed. If
5489 : : high is non-NULL, we punt above if it doesn't fit in the signed
5490 : : type, so if we get through here, +[-, high] or +[low, high] are
5491 : : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5492 : : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5493 : : high is not, the +[low, -] range is equivalent to union of
5494 : : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5495 : : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5496 : : low being 0, which should be treated as [-, -]. */
5497 : 704501 : else if (TYPE_UNSIGNED (exp_type)
5498 : 685658 : && !TYPE_UNSIGNED (arg0_type)
5499 : 387270 : && low
5500 : 1091771 : && !high)
5501 : : {
5502 : 12 : if (integer_zerop (low))
5503 : 12 : n_low = NULL_TREE;
5504 : : else
5505 : : {
5506 : 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5507 : : n_low, build_int_cst (arg0_type, -1));
5508 : 0 : n_low = build_zero_cst (arg0_type);
5509 : 0 : in_p = !in_p;
5510 : : }
5511 : : }
5512 : :
5513 : 966451 : *p_low = n_low;
5514 : 966451 : *p_high = n_high;
5515 : 966451 : *p_in_p = in_p;
5516 : 966451 : return arg0;
5517 : :
5518 : : default:
5519 : : return NULL_TREE;
5520 : : }
5521 : : }
5522 : :
5523 : : /* Given EXP, a logical expression, set the range it is testing into
5524 : : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5525 : : actually being tested. *PLOW and *PHIGH will be made of the same
5526 : : type as the returned expression. If EXP is not a comparison, we
5527 : : will most likely not be returning a useful value and range. Set
5528 : : *STRICT_OVERFLOW_P to true if the return value is only valid
5529 : : because signed overflow is undefined; otherwise, do not change
5530 : : *STRICT_OVERFLOW_P. */
5531 : :
5532 : : tree
5533 : 48081838 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5534 : : bool *strict_overflow_p)
5535 : : {
5536 : 48081838 : enum tree_code code;
5537 : 48081838 : tree arg0, arg1 = NULL_TREE;
5538 : 48081838 : tree exp_type, nexp;
5539 : 48081838 : int in_p;
5540 : 48081838 : tree low, high;
5541 : 48081838 : location_t loc = EXPR_LOCATION (exp);
5542 : :
5543 : : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5544 : : and see if we can refine the range. Some of the cases below may not
5545 : : happen, but it doesn't seem worth worrying about this. We "continue"
5546 : : the outer loop when we've changed something; otherwise we "break"
5547 : : the switch, which will "break" the while. */
5548 : :
5549 : 48081838 : in_p = 0;
5550 : 48081838 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5551 : :
5552 : 76884897 : while (1)
5553 : : {
5554 : 76884897 : code = TREE_CODE (exp);
5555 : 76884897 : exp_type = TREE_TYPE (exp);
5556 : 76884897 : arg0 = NULL_TREE;
5557 : :
5558 : 76884897 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5559 : : {
5560 : 53397486 : if (TREE_OPERAND_LENGTH (exp) > 0)
5561 : 53397486 : arg0 = TREE_OPERAND (exp, 0);
5562 : 53397486 : if (TREE_CODE_CLASS (code) == tcc_binary
5563 : 50132706 : || TREE_CODE_CLASS (code) == tcc_comparison
5564 : 61895316 : || (TREE_CODE_CLASS (code) == tcc_expression
5565 : 2717609 : && TREE_OPERAND_LENGTH (exp) > 1))
5566 : 45937648 : arg1 = TREE_OPERAND (exp, 1);
5567 : : }
5568 : 53397486 : if (arg0 == NULL_TREE)
5569 : : break;
5570 : :
5571 : 53397472 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5572 : : &high, &in_p, strict_overflow_p);
5573 : 53397472 : if (nexp == NULL_TREE)
5574 : : break;
5575 : : exp = nexp;
5576 : : }
5577 : :
5578 : : /* If EXP is a constant, we can evaluate whether this is true or false. */
5579 : 48081838 : if (TREE_CODE (exp) == INTEGER_CST)
5580 : : {
5581 : 35220 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5582 : : exp, 0, low, 0))
5583 : 35220 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5584 : : exp, 1, high, 1)));
5585 : 35220 : low = high = 0;
5586 : 35220 : exp = 0;
5587 : : }
5588 : :
5589 : 48081838 : *pin_p = in_p, *plow = low, *phigh = high;
5590 : 48081838 : return exp;
5591 : : }
5592 : :
5593 : : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5594 : : a bitwise check i.e. when
5595 : : LOW == 0xXX...X00...0
5596 : : HIGH == 0xXX...X11...1
5597 : : Return corresponding mask in MASK and stem in VALUE. */
5598 : :
5599 : : static bool
5600 : 125 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5601 : : tree *value)
5602 : : {
5603 : 125 : if (TREE_CODE (low) != INTEGER_CST
5604 : 125 : || TREE_CODE (high) != INTEGER_CST)
5605 : : return false;
5606 : :
5607 : 125 : unsigned prec = TYPE_PRECISION (type);
5608 : 125 : wide_int lo = wi::to_wide (low, prec);
5609 : 125 : wide_int hi = wi::to_wide (high, prec);
5610 : :
5611 : 125 : wide_int end_mask = lo ^ hi;
5612 : 250 : if ((end_mask & (end_mask + 1)) != 0
5613 : 235 : || (lo & end_mask) != 0)
5614 : : return false;
5615 : :
5616 : 86 : wide_int stem_mask = ~end_mask;
5617 : 86 : wide_int stem = lo & stem_mask;
5618 : 86 : if (stem != (hi & stem_mask))
5619 : : return false;
5620 : :
5621 : 86 : *mask = wide_int_to_tree (type, stem_mask);
5622 : 86 : *value = wide_int_to_tree (type, stem);
5623 : :
5624 : 86 : return true;
5625 : 211 : }
5626 : :
5627 : : /* Helper routine for build_range_check and match.pd. Return the type to
5628 : : perform the check or NULL if it shouldn't be optimized. */
5629 : :
5630 : : tree
5631 : 564593 : range_check_type (tree etype)
5632 : : {
5633 : : /* First make sure that arithmetics in this type is valid, then make sure
5634 : : that it wraps around. */
5635 : 564593 : if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5636 : 64859 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5637 : :
5638 : 564593 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5639 : : {
5640 : 411779 : tree utype, minv, maxv;
5641 : :
5642 : : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5643 : : for the type in question, as we rely on this here. */
5644 : 411779 : utype = unsigned_type_for (etype);
5645 : 411779 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5646 : 411779 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5647 : 411779 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5648 : 411779 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5649 : :
5650 : 411779 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5651 : : minv, 1, maxv, 1)))
5652 : : etype = utype;
5653 : : else
5654 : 0 : return NULL_TREE;
5655 : : }
5656 : 152814 : else if (POINTER_TYPE_P (etype)
5657 : : || TREE_CODE (etype) == OFFSET_TYPE
5658 : : /* Right now all BITINT_TYPEs satisfy
5659 : : (unsigned) max + 1 == (unsigned) min, so no need to verify
5660 : : that like for INTEGER_TYPEs. */
5661 : : || TREE_CODE (etype) == BITINT_TYPE)
5662 : 1365 : etype = unsigned_type_for (etype);
5663 : : return etype;
5664 : : }
5665 : :
5666 : : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5667 : : type, TYPE, return an expression to test if EXP is in (or out of, depending
5668 : : on IN_P) the range. Return 0 if the test couldn't be created. */
5669 : :
5670 : : tree
5671 : 1448192 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5672 : : tree low, tree high)
5673 : : {
5674 : 2542364 : tree etype = TREE_TYPE (exp), mask, value;
5675 : :
5676 : : /* Disable this optimization for function pointer expressions
5677 : : on targets that require function pointer canonicalization. */
5678 : 2542364 : if (targetm.have_canonicalize_funcptr_for_compare ()
5679 : 0 : && POINTER_TYPE_P (etype)
5680 : 2542364 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5681 : : return NULL_TREE;
5682 : :
5683 : 2542364 : if (! in_p)
5684 : : {
5685 : 338534 : value = build_range_check (loc, type, exp, 1, low, high);
5686 : 338534 : if (value != 0)
5687 : 338534 : return invert_truthvalue_loc (loc, value);
5688 : :
5689 : : return 0;
5690 : : }
5691 : :
5692 : 2203830 : if (low == 0 && high == 0)
5693 : 131165 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5694 : :
5695 : 2072665 : if (low == 0)
5696 : 707609 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5697 : 707609 : fold_convert_loc (loc, etype, high));
5698 : :
5699 : 1365056 : if (high == 0)
5700 : 72494 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5701 : 72494 : fold_convert_loc (loc, etype, low));
5702 : :
5703 : 1292562 : if (operand_equal_p (low, high, 0))
5704 : 198206 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5705 : 198206 : fold_convert_loc (loc, etype, low));
5706 : :
5707 : 1094356 : if (TREE_CODE (exp) == BIT_AND_EXPR
5708 : 1094356 : && maskable_range_p (low, high, etype, &mask, &value))
5709 : 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5710 : : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5711 : : exp, mask),
5712 : 86 : value);
5713 : :
5714 : 1094270 : if (integer_zerop (low))
5715 : : {
5716 : 622103 : if (! TYPE_UNSIGNED (etype))
5717 : : {
5718 : 130186 : etype = unsigned_type_for (etype);
5719 : 130186 : high = fold_convert_loc (loc, etype, high);
5720 : 130186 : exp = fold_convert_loc (loc, etype, exp);
5721 : : }
5722 : 622103 : return build_range_check (loc, type, exp, 1, 0, high);
5723 : : }
5724 : :
5725 : : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5726 : 472167 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5727 : : {
5728 : 107298 : int prec = TYPE_PRECISION (etype);
5729 : :
5730 : 107298 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5731 : : {
5732 : 98 : if (TYPE_UNSIGNED (etype))
5733 : : {
5734 : 92 : tree signed_etype = signed_type_for (etype);
5735 : 92 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5736 : 0 : etype
5737 : 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5738 : : else
5739 : : etype = signed_etype;
5740 : 92 : exp = fold_convert_loc (loc, etype, exp);
5741 : : }
5742 : 98 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5743 : : build_int_cst (etype, 0));
5744 : : }
5745 : : }
5746 : :
5747 : : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5748 : : This requires wrap-around arithmetics for the type of the expression. */
5749 : 472069 : etype = range_check_type (etype);
5750 : 472069 : if (etype == NULL_TREE)
5751 : : return NULL_TREE;
5752 : :
5753 : 472069 : high = fold_convert_loc (loc, etype, high);
5754 : 472069 : low = fold_convert_loc (loc, etype, low);
5755 : 472069 : exp = fold_convert_loc (loc, etype, exp);
5756 : :
5757 : 472069 : value = const_binop (MINUS_EXPR, high, low);
5758 : :
5759 : 472069 : if (value != 0 && !TREE_OVERFLOW (value))
5760 : 472069 : return build_range_check (loc, type,
5761 : : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5762 : : 1, build_int_cst (etype, 0), value);
5763 : :
5764 : : return 0;
5765 : : }
5766 : :
5767 : : /* Return the predecessor of VAL in its type, handling the infinite case. */
5768 : :
5769 : : static tree
5770 : 158714 : range_predecessor (tree val)
5771 : : {
5772 : 158714 : tree type = TREE_TYPE (val);
5773 : :
5774 : 158714 : if (INTEGRAL_TYPE_P (type)
5775 : 158714 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5776 : : return 0;
5777 : : else
5778 : 158714 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5779 : 158714 : build_int_cst (TREE_TYPE (val), 1), 0);
5780 : : }
5781 : :
5782 : : /* Return the successor of VAL in its type, handling the infinite case. */
5783 : :
5784 : : static tree
5785 : 1543591 : range_successor (tree val)
5786 : : {
5787 : 1543591 : tree type = TREE_TYPE (val);
5788 : :
5789 : 1543591 : if (INTEGRAL_TYPE_P (type)
5790 : 1543591 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5791 : : return 0;
5792 : : else
5793 : 1543582 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5794 : 1543582 : build_int_cst (TREE_TYPE (val), 1), 0);
5795 : : }
5796 : :
5797 : : /* Given two ranges, see if we can merge them into one. Return 1 if we
5798 : : can, 0 if we can't. Set the output range into the specified parameters. */
5799 : :
5800 : : bool
5801 : 3409122 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5802 : : tree high0, int in1_p, tree low1, tree high1)
5803 : : {
5804 : 3409122 : bool no_overlap;
5805 : 3409122 : int subset;
5806 : 3409122 : int temp;
5807 : 3409122 : tree tem;
5808 : 3409122 : int in_p;
5809 : 3409122 : tree low, high;
5810 : 3409122 : int lowequal = ((low0 == 0 && low1 == 0)
5811 : 3409122 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5812 : 3409122 : low0, 0, low1, 0)));
5813 : 3409122 : int highequal = ((high0 == 0 && high1 == 0)
5814 : 3409122 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5815 : 3409122 : high0, 1, high1, 1)));
5816 : :
5817 : : /* Make range 0 be the range that starts first, or ends last if they
5818 : : start at the same value. Swap them if it isn't. */
5819 : 3409122 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5820 : : low0, 0, low1, 0))
5821 : 3409122 : || (lowequal
5822 : 549963 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5823 : : high1, 1, high0, 1))))
5824 : : {
5825 : : temp = in0_p, in0_p = in1_p, in1_p = temp;
5826 : : tem = low0, low0 = low1, low1 = tem;
5827 : : tem = high0, high0 = high1, high1 = tem;
5828 : : }
5829 : :
5830 : : /* If the second range is != high1 where high1 is the type maximum of
5831 : : the type, try first merging with < high1 range. */
5832 : 3409122 : if (low1
5833 : 3409122 : && high1
5834 : 936873 : && TREE_CODE (low1) == INTEGER_CST
5835 : 936873 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5836 : 152019 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5837 : 212222 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5838 : : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5839 : 4300087 : && operand_equal_p (low1, high1, 0))
5840 : : {
5841 : 488988 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5842 : 488988 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5843 : : !in1_p, NULL_TREE, range_predecessor (low1)))
5844 : : return true;
5845 : : /* Similarly for the second range != low1 where low1 is the type minimum
5846 : : of the type, try first merging with > low1 range. */
5847 : 398678 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5848 : 398678 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5849 : : !in1_p, range_successor (low1), NULL_TREE))
5850 : : return true;
5851 : : }
5852 : :
5853 : : /* Now flag two cases, whether the ranges are disjoint or whether the
5854 : : second range is totally subsumed in the first. Note that the tests
5855 : : below are simplified by the ones above. */
5856 : 3252807 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5857 : : high0, 1, low1, 0));
5858 : 3252807 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5859 : : high1, 1, high0, 1));
5860 : :
5861 : : /* We now have four cases, depending on whether we are including or
5862 : : excluding the two ranges. */
5863 : 3252807 : if (in0_p && in1_p)
5864 : : {
5865 : : /* If they don't overlap, the result is false. If the second range
5866 : : is a subset it is the result. Otherwise, the range is from the start
5867 : : of the second to the end of the first. */
5868 : 1462297 : if (no_overlap)
5869 : : in_p = 0, low = high = 0;
5870 : 1460705 : else if (subset)
5871 : : in_p = 1, low = low1, high = high1;
5872 : : else
5873 : 1330538 : in_p = 1, low = low1, high = high0;
5874 : : }
5875 : :
5876 : 1790510 : else if (in0_p && ! in1_p)
5877 : : {
5878 : : /* If they don't overlap, the result is the first range. If they are
5879 : : equal, the result is false. If the second range is a subset of the
5880 : : first, and the ranges begin at the same place, we go from just after
5881 : : the end of the second range to the end of the first. If the second
5882 : : range is not a subset of the first, or if it is a subset and both
5883 : : ranges end at the same place, the range starts at the start of the
5884 : : first range and ends just before the second range.
5885 : : Otherwise, we can't describe this as a single range. */
5886 : 312233 : if (no_overlap)
5887 : : in_p = 1, low = low0, high = high0;
5888 : 307845 : else if (lowequal && highequal)
5889 : : in_p = 0, low = high = 0;
5890 : 307024 : else if (subset && lowequal)
5891 : : {
5892 : 227479 : low = range_successor (high1);
5893 : 227479 : high = high0;
5894 : 227479 : in_p = 1;
5895 : 227479 : if (low == 0)
5896 : : {
5897 : : /* We are in the weird situation where high0 > high1 but
5898 : : high1 has no successor. Punt. */
5899 : : return 0;
5900 : : }
5901 : : }
5902 : 79545 : else if (! subset || highequal)
5903 : : {
5904 : 55537 : low = low0;
5905 : 55537 : high = range_predecessor (low1);
5906 : 55537 : in_p = 1;
5907 : 55537 : if (high == 0)
5908 : : {
5909 : : /* low0 < low1 but low1 has no predecessor. Punt. */
5910 : : return 0;
5911 : : }
5912 : : }
5913 : : else
5914 : : return 0;
5915 : : }
5916 : :
5917 : 1478277 : else if (! in0_p && in1_p)
5918 : : {
5919 : : /* If they don't overlap, the result is the second range. If the second
5920 : : is a subset of the first, the result is false. Otherwise,
5921 : : the range starts just after the first range and ends at the
5922 : : end of the second. */
5923 : 1133228 : if (no_overlap)
5924 : : in_p = 1, low = low1, high = high1;
5925 : 1124799 : else if (subset || highequal)
5926 : : in_p = 0, low = high = 0;
5927 : : else
5928 : : {
5929 : 1010653 : low = range_successor (high0);
5930 : 1010653 : high = high1;
5931 : 1010653 : in_p = 1;
5932 : 1010653 : if (low == 0)
5933 : : {
5934 : : /* high1 > high0 but high0 has no successor. Punt. */
5935 : : return 0;
5936 : : }
5937 : : }
5938 : : }
5939 : :
5940 : : else
5941 : : {
5942 : : /* The case where we are excluding both ranges. Here the complex case
5943 : : is if they don't overlap. In that case, the only time we have a
5944 : : range is if they are adjacent. If the second is a subset of the
5945 : : first, the result is the first. Otherwise, the range to exclude
5946 : : starts at the beginning of the first range and ends at the end of the
5947 : : second. */
5948 : 345049 : if (no_overlap)
5949 : : {
5950 : 239124 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5951 : : range_successor (high0),
5952 : : 1, low1, 0)))
5953 : : in_p = 0, low = low0, high = high1;
5954 : : else
5955 : : {
5956 : : /* Canonicalize - [min, x] into - [-, x]. */
5957 : 183391 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5958 : 182326 : switch (TREE_CODE (TREE_TYPE (low0)))
5959 : : {
5960 : 69276 : case ENUMERAL_TYPE:
5961 : 69276 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5962 : : GET_MODE_BITSIZE
5963 : 138552 : (TYPE_MODE (TREE_TYPE (low0)))))
5964 : : break;
5965 : : /* FALLTHROUGH */
5966 : 182133 : case INTEGER_TYPE:
5967 : 182133 : if (tree_int_cst_equal (low0,
5968 : 182133 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5969 : 6683 : low0 = 0;
5970 : : break;
5971 : 193 : case POINTER_TYPE:
5972 : 193 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5973 : 193 : && integer_zerop (low0))
5974 : : low0 = 0;
5975 : : break;
5976 : : default:
5977 : : break;
5978 : : }
5979 : :
5980 : : /* Canonicalize - [x, max] into - [x, -]. */
5981 : 183391 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5982 : 183193 : switch (TREE_CODE (TREE_TYPE (high1)))
5983 : : {
5984 : 69284 : case ENUMERAL_TYPE:
5985 : 69284 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5986 : : GET_MODE_BITSIZE
5987 : 138568 : (TYPE_MODE (TREE_TYPE (high1)))))
5988 : : break;
5989 : : /* FALLTHROUGH */
5990 : 183000 : case INTEGER_TYPE:
5991 : 183000 : if (tree_int_cst_equal (high1,
5992 : 183000 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5993 : 12869 : high1 = 0;
5994 : : break;
5995 : 193 : case POINTER_TYPE:
5996 : 193 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5997 : 386 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5998 : : high1, 1,
5999 : 193 : build_int_cst (TREE_TYPE (high1), 1),
6000 : : 1)))
6001 : 133 : high1 = 0;
6002 : : break;
6003 : : default:
6004 : : break;
6005 : : }
6006 : :
6007 : : /* The ranges might be also adjacent between the maximum and
6008 : : minimum values of the given type. For
6009 : : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6010 : : return + [x + 1, y - 1]. */
6011 : 183391 : if (low0 == 0 && high1 == 0)
6012 : : {
6013 : 330 : low = range_successor (high0);
6014 : 330 : high = range_predecessor (low1);
6015 : 330 : if (low == 0 || high == 0)
6016 : : return 0;
6017 : :
6018 : : in_p = 1;
6019 : : }
6020 : : else
6021 : : return 0;
6022 : : }
6023 : : }
6024 : 105925 : else if (subset)
6025 : : in_p = 0, low = low0, high = high0;
6026 : : else
6027 : 14881 : in_p = 0, low = low0, high = high1;
6028 : : }
6029 : :
6030 : 3045729 : *pin_p = in_p, *plow = low, *phigh = high;
6031 : 3045729 : return 1;
6032 : : }
6033 : :
6034 : :
6035 : : /* Subroutine of fold, looking inside expressions of the form
6036 : : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6037 : : are the three operands of the COND_EXPR. This function is
6038 : : being used also to optimize A op B ? C : A, by reversing the
6039 : : comparison first.
6040 : :
6041 : : Return a folded expression whose code is not a COND_EXPR
6042 : : anymore, or NULL_TREE if no folding opportunity is found. */
6043 : :
6044 : : static tree
6045 : 452794 : fold_cond_expr_with_comparison (location_t loc, tree type,
6046 : : enum tree_code comp_code,
6047 : : tree arg00, tree arg01, tree arg1, tree arg2)
6048 : : {
6049 : 452794 : tree arg1_type = TREE_TYPE (arg1);
6050 : 452794 : tree tem;
6051 : :
6052 : 452794 : STRIP_NOPS (arg1);
6053 : 452794 : STRIP_NOPS (arg2);
6054 : :
6055 : : /* If we have A op 0 ? A : -A, consider applying the following
6056 : : transformations:
6057 : :
6058 : : A == 0? A : -A same as -A
6059 : : A != 0? A : -A same as A
6060 : : A >= 0? A : -A same as abs (A)
6061 : : A > 0? A : -A same as abs (A)
6062 : : A <= 0? A : -A same as -abs (A)
6063 : : A < 0? A : -A same as -abs (A)
6064 : :
6065 : : None of these transformations work for modes with signed
6066 : : zeros. If A is +/-0, the first two transformations will
6067 : : change the sign of the result (from +0 to -0, or vice
6068 : : versa). The last four will fix the sign of the result,
6069 : : even though the original expressions could be positive or
6070 : : negative, depending on the sign of A.
6071 : :
6072 : : Note that all these transformations are correct if A is
6073 : : NaN, since the two alternatives (A and -A) are also NaNs. */
6074 : 452794 : if (!HONOR_SIGNED_ZEROS (type)
6075 : 905598 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6076 : 452794 : ? real_zerop (arg01)
6077 : 451734 : : integer_zerop (arg01))
6078 : 1200917 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6079 : 1497 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6080 : : /* In the case that A is of the form X-Y, '-A' (arg2) may
6081 : : have already been folded to Y-X, check for that. */
6082 : 294056 : || (TREE_CODE (arg1) == MINUS_EXPR
6083 : 1718 : && TREE_CODE (arg2) == MINUS_EXPR
6084 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6085 : 0 : TREE_OPERAND (arg2, 1), 0)
6086 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6087 : 0 : TREE_OPERAND (arg2, 0), 0))))
6088 : 1273 : switch (comp_code)
6089 : : {
6090 : 0 : case EQ_EXPR:
6091 : 0 : case UNEQ_EXPR:
6092 : 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6093 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6094 : 0 : case NE_EXPR:
6095 : 0 : case LTGT_EXPR:
6096 : 0 : return fold_convert_loc (loc, type, arg1);
6097 : 0 : case UNGE_EXPR:
6098 : 0 : case UNGT_EXPR:
6099 : 0 : if (flag_trapping_math)
6100 : : break;
6101 : : /* Fall through. */
6102 : 1273 : case GE_EXPR:
6103 : 1273 : case GT_EXPR:
6104 : 1273 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6105 : : break;
6106 : 1257 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6107 : 1257 : return fold_convert_loc (loc, type, tem);
6108 : 0 : case UNLE_EXPR:
6109 : 0 : case UNLT_EXPR:
6110 : 0 : if (flag_trapping_math)
6111 : : break;
6112 : : /* FALLTHRU */
6113 : 0 : case LE_EXPR:
6114 : 0 : case LT_EXPR:
6115 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6116 : : break;
6117 : 0 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6118 : 0 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6119 : : {
6120 : : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6121 : : is not, invokes UB both in abs and in the negation of it.
6122 : : So, use ABSU_EXPR instead. */
6123 : 0 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6124 : 0 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6125 : 0 : tem = negate_expr (tem);
6126 : 0 : return fold_convert_loc (loc, type, tem);
6127 : : }
6128 : : else
6129 : : {
6130 : 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6131 : 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6132 : : }
6133 : 0 : default:
6134 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6135 : : break;
6136 : : }
6137 : :
6138 : : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6139 : : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6140 : : both transformations are correct when A is NaN: A != 0
6141 : : is then true, and A == 0 is false. */
6142 : :
6143 : 451537 : if (!HONOR_SIGNED_ZEROS (type)
6144 : 451537 : && integer_zerop (arg01) && integer_zerop (arg2))
6145 : : {
6146 : 247240 : if (comp_code == NE_EXPR)
6147 : 145 : return fold_convert_loc (loc, type, arg1);
6148 : 247095 : else if (comp_code == EQ_EXPR)
6149 : 0 : return build_zero_cst (type);
6150 : : }
6151 : :
6152 : : /* Try some transformations of A op B ? A : B.
6153 : :
6154 : : A == B? A : B same as B
6155 : : A != B? A : B same as A
6156 : : A >= B? A : B same as max (A, B)
6157 : : A > B? A : B same as max (B, A)
6158 : : A <= B? A : B same as min (A, B)
6159 : : A < B? A : B same as min (B, A)
6160 : :
6161 : : As above, these transformations don't work in the presence
6162 : : of signed zeros. For example, if A and B are zeros of
6163 : : opposite sign, the first two transformations will change
6164 : : the sign of the result. In the last four, the original
6165 : : expressions give different results for (A=+0, B=-0) and
6166 : : (A=-0, B=+0), but the transformed expressions do not.
6167 : :
6168 : : The first two transformations are correct if either A or B
6169 : : is a NaN. In the first transformation, the condition will
6170 : : be false, and B will indeed be chosen. In the case of the
6171 : : second transformation, the condition A != B will be true,
6172 : : and A will be chosen.
6173 : :
6174 : : The conversions to max() and min() are not correct if B is
6175 : : a number and A is not. The conditions in the original
6176 : : expressions will be false, so all four give B. The min()
6177 : : and max() versions would give a NaN instead. */
6178 : 451392 : if (!HONOR_SIGNED_ZEROS (type)
6179 : 451392 : && operand_equal_for_comparison_p (arg01, arg2)
6180 : : /* Avoid these transformations if the COND_EXPR may be used
6181 : : as an lvalue in the C++ front-end. PR c++/19199. */
6182 : 706990 : && (in_gimple_form
6183 : 16580 : || VECTOR_TYPE_P (type)
6184 : 16518 : || (! lang_GNU_CXX ()
6185 : 13907 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6186 : 2611 : || ! maybe_lvalue_p (arg1)
6187 : 2590 : || ! maybe_lvalue_p (arg2)))
6188 : : {
6189 : 253947 : tree comp_op0 = arg00;
6190 : 253947 : tree comp_op1 = arg01;
6191 : 253947 : tree comp_type = TREE_TYPE (comp_op0);
6192 : :
6193 : 253947 : switch (comp_code)
6194 : : {
6195 : 0 : case EQ_EXPR:
6196 : 0 : return fold_convert_loc (loc, type, arg2);
6197 : 1 : case NE_EXPR:
6198 : 1 : return fold_convert_loc (loc, type, arg1);
6199 : 5865 : case LE_EXPR:
6200 : 5865 : case LT_EXPR:
6201 : 5865 : case UNLE_EXPR:
6202 : 5865 : case UNLT_EXPR:
6203 : : /* In C++ a ?: expression can be an lvalue, so put the
6204 : : operand which will be used if they are equal first
6205 : : so that we can convert this back to the
6206 : : corresponding COND_EXPR. */
6207 : 5865 : if (!HONOR_NANS (arg1))
6208 : : {
6209 : 5865 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6210 : 5865 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6211 : 5865 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6212 : 5865 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6213 : 4522 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6214 : : comp_op1, comp_op0);
6215 : 5865 : return fold_convert_loc (loc, type, tem);
6216 : : }
6217 : : break;
6218 : 248081 : case GE_EXPR:
6219 : 248081 : case GT_EXPR:
6220 : 248081 : case UNGE_EXPR:
6221 : 248081 : case UNGT_EXPR:
6222 : 248081 : if (!HONOR_NANS (arg1))
6223 : : {
6224 : 248079 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6225 : 248079 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6226 : 248079 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6227 : 248079 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6228 : 3543 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6229 : : comp_op1, comp_op0);
6230 : 248079 : return fold_convert_loc (loc, type, tem);
6231 : : }
6232 : : break;
6233 : 0 : case UNEQ_EXPR:
6234 : 0 : if (!HONOR_NANS (arg1))
6235 : 0 : return fold_convert_loc (loc, type, arg2);
6236 : : break;
6237 : 0 : case LTGT_EXPR:
6238 : 0 : if (!HONOR_NANS (arg1))
6239 : 0 : return fold_convert_loc (loc, type, arg1);
6240 : : break;
6241 : 0 : default:
6242 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6243 : : break;
6244 : : }
6245 : : }
6246 : :
6247 : : return NULL_TREE;
6248 : : }
6249 : :
6250 : :
6251 : :
6252 : : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6253 : : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6254 : : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6255 : : false) >= 2)
6256 : : #endif
6257 : :
6258 : : /* EXP is some logical combination of boolean tests. See if we can
6259 : : merge it into some range test. Return the new tree if so. */
6260 : :
6261 : : static tree
6262 : 24040413 : fold_range_test (location_t loc, enum tree_code code, tree type,
6263 : : tree op0, tree op1)
6264 : : {
6265 : 24040413 : int or_op = (code == TRUTH_ORIF_EXPR
6266 : 24040413 : || code == TRUTH_OR_EXPR);
6267 : 24040413 : int in0_p, in1_p, in_p;
6268 : 24040413 : tree low0, low1, low, high0, high1, high;
6269 : 24040413 : bool strict_overflow_p = false;
6270 : 24040413 : tree tem, lhs, rhs;
6271 : 24040413 : const char * const warnmsg = G_("assuming signed overflow does not occur "
6272 : : "when simplifying range test");
6273 : :
6274 : 24040413 : if (!INTEGRAL_TYPE_P (type))
6275 : : return 0;
6276 : :
6277 : 24040413 : lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
6278 : : /* If op0 is known true or false and this is a short-circuiting
6279 : : operation we must not merge with op1 since that makes side-effects
6280 : : unconditional. So special-case this. */
6281 : 24040413 : if (!lhs
6282 : 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6283 : 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6284 : : return op0;
6285 : 24040411 : rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
6286 : :
6287 : : /* If this is an OR operation, invert both sides; we will invert
6288 : : again at the end. */
6289 : 24040411 : if (or_op)
6290 : 11607610 : in0_p = ! in0_p, in1_p = ! in1_p;
6291 : :
6292 : : /* If both expressions are the same, if we can merge the ranges, and we
6293 : : can build the range test, return it or it inverted. If one of the
6294 : : ranges is always true or always false, consider it to be the same
6295 : : expression as the other. */
6296 : 24005195 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6297 : 1109703 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6298 : : in1_p, low1, high1)
6299 : 24978040 : && (tem = (build_range_check (loc, type,
6300 : : lhs != 0 ? lhs
6301 : 0 : : rhs != 0 ? rhs : integer_zero_node,
6302 : : in_p, low, high))) != 0)
6303 : : {
6304 : 937629 : if (strict_overflow_p)
6305 : 171 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6306 : 937629 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6307 : : }
6308 : :
6309 : : /* On machines where the branch cost is expensive, if this is a
6310 : : short-circuited branch and the underlying object on both sides
6311 : : is the same, make a non-short-circuit operation. */
6312 : 23102782 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6313 : 23102782 : if (param_logical_op_non_short_circuit != -1)
6314 : 7781 : logical_op_non_short_circuit
6315 : 7781 : = param_logical_op_non_short_circuit;
6316 : 23102782 : if (logical_op_non_short_circuit
6317 : 23098891 : && !sanitize_coverage_p ()
6318 : 23098888 : && lhs != 0 && rhs != 0
6319 : 23098585 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6320 : 27893145 : && operand_equal_p (lhs, rhs, 0))
6321 : : {
6322 : : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6323 : : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6324 : : which cases we can't do this. */
6325 : 142309 : if (simple_operand_p (lhs))
6326 : 136595 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6327 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6328 : 68821 : type, op0, op1);
6329 : :
6330 : 73488 : else if (!lang_hooks.decls.global_bindings_p ()
6331 : 73488 : && !CONTAINS_PLACEHOLDER_P (lhs))
6332 : : {
6333 : 72835 : tree common = save_expr (lhs);
6334 : :
6335 : 119925 : if ((lhs = build_range_check (loc, type, common,
6336 : 47090 : or_op ? ! in0_p : in0_p,
6337 : : low0, high0)) != 0
6338 : 119925 : && (rhs = build_range_check (loc, type, common,
6339 : 47090 : or_op ? ! in1_p : in1_p,
6340 : : low1, high1)) != 0)
6341 : : {
6342 : 72835 : if (strict_overflow_p)
6343 : 0 : fold_overflow_warning (warnmsg,
6344 : : WARN_STRICT_OVERFLOW_COMPARISON);
6345 : 119925 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6346 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6347 : 72835 : type, lhs, rhs);
6348 : : }
6349 : : }
6350 : : }
6351 : :
6352 : : return 0;
6353 : : }
6354 : :
6355 : : /* For an expression that has the form
6356 : : (A && B) || ~B
6357 : : or
6358 : : (A || B) && ~B,
6359 : : we can drop one of the inner expressions and simplify to
6360 : : A || ~B
6361 : : or
6362 : : A && ~B
6363 : : LOC is the location of the resulting expression. OP is the inner
6364 : : logical operation; the left-hand side in the examples above, while CMPOP
6365 : : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6366 : : removing a condition that guards another, as in
6367 : : (A != NULL && A->...) || A == NULL
6368 : : which we must not transform. If RHS_ONLY is true, only eliminate the
6369 : : right-most operand of the inner logical operation. */
6370 : :
6371 : : static tree
6372 : 142749 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6373 : : bool rhs_only)
6374 : : {
6375 : 142749 : enum tree_code code = TREE_CODE (cmpop);
6376 : 142749 : enum tree_code truthop_code = TREE_CODE (op);
6377 : 142749 : tree lhs = TREE_OPERAND (op, 0);
6378 : 142749 : tree rhs = TREE_OPERAND (op, 1);
6379 : 142749 : tree orig_lhs = lhs, orig_rhs = rhs;
6380 : 142749 : enum tree_code rhs_code = TREE_CODE (rhs);
6381 : 142749 : enum tree_code lhs_code = TREE_CODE (lhs);
6382 : 142749 : enum tree_code inv_code;
6383 : :
6384 : 142749 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6385 : : return NULL_TREE;
6386 : :
6387 : 95635 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6388 : : return NULL_TREE;
6389 : :
6390 : 54058 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6391 : :
6392 : 54058 : if (rhs_code == truthop_code)
6393 : : {
6394 : 33 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6395 : 33 : if (newrhs != NULL_TREE)
6396 : : {
6397 : 0 : rhs = newrhs;
6398 : 0 : rhs_code = TREE_CODE (rhs);
6399 : : }
6400 : : }
6401 : 54058 : if (lhs_code == truthop_code && !rhs_only)
6402 : : {
6403 : 464 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6404 : 464 : if (newlhs != NULL_TREE)
6405 : : {
6406 : 0 : lhs = newlhs;
6407 : 0 : lhs_code = TREE_CODE (lhs);
6408 : : }
6409 : : }
6410 : :
6411 : 54058 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6412 : 54058 : if (inv_code == rhs_code
6413 : 925 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6414 : 54094 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6415 : : return lhs;
6416 : 54045 : if (!rhs_only && inv_code == lhs_code
6417 : 630 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6418 : 54137 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6419 : : return rhs;
6420 : 53954 : if (rhs != orig_rhs || lhs != orig_lhs)
6421 : 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6422 : 0 : lhs, rhs);
6423 : : return NULL_TREE;
6424 : : }
6425 : :
6426 : : /* Find ways of folding logical expressions of LHS and RHS:
6427 : : Try to merge two comparisons to the same innermost item.
6428 : : Look for range tests like "ch >= '0' && ch <= '9'".
6429 : : Look for combinations of simple terms on machines with expensive branches
6430 : : and evaluate the RHS unconditionally.
6431 : :
6432 : : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6433 : : function and the one above.
6434 : :
6435 : : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6436 : : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6437 : :
6438 : : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6439 : : two operands.
6440 : :
6441 : : We return the simplified tree or 0 if no optimization is possible. */
6442 : :
6443 : : static tree
6444 : 23706601 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6445 : : tree lhs, tree rhs)
6446 : : {
6447 : : /* If this is the "or" of two comparisons, we can do something if
6448 : : the comparisons are NE_EXPR. If this is the "and", we can do something
6449 : : if the comparisons are EQ_EXPR. I.e.,
6450 : : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6451 : :
6452 : : WANTED_CODE is this operation code. For single bit fields, we can
6453 : : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6454 : : comparison for one-bit fields. */
6455 : :
6456 : 23706601 : enum tree_code lcode, rcode;
6457 : 23706601 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6458 : 23706601 : tree result;
6459 : :
6460 : : /* Start by getting the comparison codes. Fail if anything is volatile.
6461 : : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6462 : : it were surrounded with a NE_EXPR. */
6463 : :
6464 : 23706601 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6465 : : return 0;
6466 : :
6467 : 21333084 : lcode = TREE_CODE (lhs);
6468 : 21333084 : rcode = TREE_CODE (rhs);
6469 : :
6470 : 21333084 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6471 : : {
6472 : 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6473 : 0 : build_int_cst (TREE_TYPE (lhs), 0));
6474 : 0 : lcode = NE_EXPR;
6475 : : }
6476 : :
6477 : 21333084 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6478 : : {
6479 : 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6480 : 0 : build_int_cst (TREE_TYPE (rhs), 0));
6481 : 0 : rcode = NE_EXPR;
6482 : : }
6483 : :
6484 : 21333084 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6485 : 18995320 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6486 : : return 0;
6487 : :
6488 : 17846743 : ll_arg = TREE_OPERAND (lhs, 0);
6489 : 17846743 : lr_arg = TREE_OPERAND (lhs, 1);
6490 : 17846743 : rl_arg = TREE_OPERAND (rhs, 0);
6491 : 17846743 : rr_arg = TREE_OPERAND (rhs, 1);
6492 : :
6493 : : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6494 : 17846743 : if (simple_operand_p (ll_arg)
6495 : 17846743 : && simple_operand_p (lr_arg))
6496 : : {
6497 : 14484206 : if (operand_equal_p (ll_arg, rl_arg, 0)
6498 : 14484206 : && operand_equal_p (lr_arg, rr_arg, 0))
6499 : : {
6500 : 20541 : result = combine_comparisons (loc, code, lcode, rcode,
6501 : : truth_type, ll_arg, lr_arg);
6502 : 20541 : if (result)
6503 : : return result;
6504 : : }
6505 : 14463665 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6506 : 14463665 : && operand_equal_p (lr_arg, rl_arg, 0))
6507 : : {
6508 : 286 : result = combine_comparisons (loc, code, lcode,
6509 : : swap_tree_comparison (rcode),
6510 : : truth_type, ll_arg, lr_arg);
6511 : 286 : if (result)
6512 : : return result;
6513 : : }
6514 : : }
6515 : :
6516 : 8567169 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6517 : 17826501 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6518 : :
6519 : : /* If the RHS can be evaluated unconditionally and its operands are
6520 : : simple, it wins to evaluate the RHS unconditionally on machines
6521 : : with expensive branches. In this case, this isn't a comparison
6522 : : that can be merged. */
6523 : :
6524 : 17826501 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6525 : : false) >= 2
6526 : 17826398 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6527 : 16800441 : && simple_operand_p (rl_arg)
6528 : 27374518 : && simple_operand_p (rr_arg))
6529 : : {
6530 : : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6531 : 10648558 : if (code == TRUTH_OR_EXPR
6532 : 1464903 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6533 : 599455 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6534 : 25934 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6535 : 10668026 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6536 : 38320 : return build2_loc (loc, NE_EXPR, truth_type,
6537 : 19160 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6538 : : ll_arg, rl_arg),
6539 : 19160 : build_int_cst (TREE_TYPE (ll_arg), 0));
6540 : :
6541 : : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6542 : 10629398 : if (code == TRUTH_AND_EXPR
6543 : 1624207 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6544 : 757644 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6545 : 5310 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6546 : 10630975 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6547 : 2688 : return build2_loc (loc, EQ_EXPR, truth_type,
6548 : 1344 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6549 : : ll_arg, rl_arg),
6550 : 1344 : build_int_cst (TREE_TYPE (ll_arg), 0));
6551 : : }
6552 : :
6553 : : return 0;
6554 : : }
6555 : :
6556 : : /* T is an integer expression that is being multiplied, divided, or taken a
6557 : : modulus (CODE says which and what kind of divide or modulus) by a
6558 : : constant C. See if we can eliminate that operation by folding it with
6559 : : other operations already in T. WIDE_TYPE, if non-null, is a type that
6560 : : should be used for the computation if wider than our type.
6561 : :
6562 : : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6563 : : (X * 2) + (Y * 4). We must, however, be assured that either the original
6564 : : expression would not overflow or that overflow is undefined for the type
6565 : : in the language in question.
6566 : :
6567 : : If we return a non-null expression, it is an equivalent form of the
6568 : : original computation, but need not be in the original type.
6569 : :
6570 : : We set *STRICT_OVERFLOW_P to true if the return values depends on
6571 : : signed overflow being undefined. Otherwise we do not change
6572 : : *STRICT_OVERFLOW_P. */
6573 : :
6574 : : static tree
6575 : 94300435 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6576 : : bool *strict_overflow_p)
6577 : : {
6578 : : /* To avoid exponential search depth, refuse to allow recursion past
6579 : : three levels. Beyond that (1) it's highly unlikely that we'll find
6580 : : something interesting and (2) we've probably processed it before
6581 : : when we built the inner expression. */
6582 : :
6583 : 94300435 : static int depth;
6584 : 94300435 : tree ret;
6585 : :
6586 : 94300435 : if (depth > 3)
6587 : : return NULL;
6588 : :
6589 : 91031077 : depth++;
6590 : 91031077 : ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6591 : 91031077 : depth--;
6592 : :
6593 : 91031077 : return ret;
6594 : : }
6595 : :
6596 : : static tree
6597 : 91031077 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6598 : : bool *strict_overflow_p)
6599 : : {
6600 : 91031077 : tree type = TREE_TYPE (t);
6601 : 91031077 : enum tree_code tcode = TREE_CODE (t);
6602 : 91031077 : tree ctype = type;
6603 : 91031077 : if (wide_type)
6604 : : {
6605 : 30426309 : if (TREE_CODE (type) == BITINT_TYPE
6606 : 30426190 : || TREE_CODE (wide_type) == BITINT_TYPE)
6607 : : {
6608 : 119 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6609 : 8683067 : ctype = wide_type;
6610 : : }
6611 : 30426190 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6612 : 60852380 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6613 : 8683067 : ctype = wide_type;
6614 : : }
6615 : 91031077 : tree t1, t2;
6616 : 91031077 : bool same_p = tcode == code;
6617 : 91031077 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6618 : 91031077 : bool sub_strict_overflow_p;
6619 : :
6620 : : /* Don't deal with constants of zero here; they confuse the code below. */
6621 : 91031077 : if (integer_zerop (c))
6622 : : return NULL_TREE;
6623 : :
6624 : 91009057 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6625 : 36380641 : op0 = TREE_OPERAND (t, 0);
6626 : :
6627 : 91009057 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6628 : 11005237 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6629 : :
6630 : : /* Note that we need not handle conditional operations here since fold
6631 : : already handles those cases. So just do arithmetic here. */
6632 : 91009057 : switch (tcode)
6633 : : {
6634 : 4161093 : case INTEGER_CST:
6635 : : /* For a constant, we can always simplify if we are a multiply
6636 : : or (for divide and modulus) if it is a multiple of our constant. */
6637 : 4161093 : if (code == MULT_EXPR
6638 : 5381151 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6639 : 1220058 : TYPE_SIGN (type)))
6640 : : {
6641 : 3346283 : tree tem = const_binop (code, fold_convert (ctype, t),
6642 : : fold_convert (ctype, c));
6643 : : /* If the multiplication overflowed, we lost information on it.
6644 : : See PR68142 and PR69845. */
6645 : 3346283 : if (TREE_OVERFLOW (tem))
6646 : : return NULL_TREE;
6647 : : return tem;
6648 : : }
6649 : : break;
6650 : :
6651 : 35733016 : CASE_CONVERT: case NON_LVALUE_EXPR:
6652 : 35733016 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6653 : : break;
6654 : : /* If op0 is an expression ... */
6655 : 34506294 : if ((COMPARISON_CLASS_P (op0)
6656 : : || UNARY_CLASS_P (op0)
6657 : 34506294 : || BINARY_CLASS_P (op0)
6658 : 31586510 : || VL_EXP_CLASS_P (op0)
6659 : 31550952 : || EXPRESSION_CLASS_P (op0))
6660 : : /* ... and has wrapping overflow, and its type is smaller
6661 : : than ctype, then we cannot pass through as widening. */
6662 : 34629894 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6663 : 1085557 : && (TYPE_PRECISION (ctype)
6664 : 1085557 : > TYPE_PRECISION (TREE_TYPE (op0))))
6665 : : /* ... or this is a truncation (t is narrower than op0),
6666 : : then we cannot pass through this narrowing. */
6667 : 2491120 : || (TYPE_PRECISION (type)
6668 : 2491120 : < TYPE_PRECISION (TREE_TYPE (op0)))
6669 : : /* ... or signedness changes for division or modulus,
6670 : : then we cannot pass through this conversion. */
6671 : 2457950 : || (code != MULT_EXPR
6672 : 114262 : && (TYPE_UNSIGNED (ctype)
6673 : 114262 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6674 : : /* ... or has undefined overflow while the converted to
6675 : : type has not, we cannot do the operation in the inner type
6676 : : as that would introduce undefined overflow. */
6677 : 2363553 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6678 : 1855798 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6679 : : break;
6680 : :
6681 : : /* Pass the constant down and see if we can make a simplification. If
6682 : : we can, replace this expression with the inner simplification for
6683 : : possible later conversion to our or some other type. */
6684 : 32023196 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6685 : 32023196 : && TREE_CODE (t2) == INTEGER_CST
6686 : 32023196 : && !TREE_OVERFLOW (t2)
6687 : 64892191 : && (t1 = extract_muldiv (op0, t2, code,
6688 : : code == MULT_EXPR ? ctype : NULL_TREE,
6689 : : strict_overflow_p)) != 0)
6690 : : return t1;
6691 : : break;
6692 : :
6693 : 295 : case ABS_EXPR:
6694 : : /* If widening the type changes it from signed to unsigned, then we
6695 : : must avoid building ABS_EXPR itself as unsigned. */
6696 : 295 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6697 : : {
6698 : 0 : tree cstype = (*signed_type_for) (ctype);
6699 : 0 : if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6700 : : != 0)
6701 : : {
6702 : 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6703 : 0 : return fold_convert (ctype, t1);
6704 : : }
6705 : : break;
6706 : : }
6707 : : /* If the constant is negative, we cannot simplify this. */
6708 : 295 : if (tree_int_cst_sgn (c) == -1)
6709 : : break;
6710 : : /* FALLTHROUGH */
6711 : 51420 : case NEGATE_EXPR:
6712 : : /* For division and modulus, type can't be unsigned, as e.g.
6713 : : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6714 : : For signed types, even with wrapping overflow, this is fine. */
6715 : 51420 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6716 : : break;
6717 : 49621 : if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6718 : : != 0)
6719 : 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6720 : : break;
6721 : :
6722 : 772 : case MIN_EXPR: case MAX_EXPR:
6723 : : /* If widening the type changes the signedness, then we can't perform
6724 : : this optimization as that changes the result. */
6725 : 772 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6726 : : break;
6727 : :
6728 : : /* Punt for multiplication altogether.
6729 : : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6730 : : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6731 : : 0U, the latter is 2U.
6732 : : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6733 : : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6734 : : well defined 0, the latter invokes UB.
6735 : : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6736 : : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6737 : : well defined 25, the latter invokes UB. */
6738 : 772 : if (code == MULT_EXPR)
6739 : : break;
6740 : : /* For division/modulo, punt on c being -1 for MAX, as
6741 : : MAX (INT_MIN, 0) / -1 is not equivalent to
6742 : : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6743 : : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6744 : : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6745 : : transformation won't make it worse. */
6746 : 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6747 : : break;
6748 : :
6749 : : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6750 : 8 : sub_strict_overflow_p = false;
6751 : 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type,
6752 : : &sub_strict_overflow_p)) != 0
6753 : 8 : && (t2 = extract_muldiv (op1, c, code, wide_type,
6754 : : &sub_strict_overflow_p)) != 0)
6755 : : {
6756 : 0 : if (tree_int_cst_sgn (c) < 0)
6757 : 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6758 : 0 : if (sub_strict_overflow_p)
6759 : 0 : *strict_overflow_p = true;
6760 : 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6761 : : fold_convert (ctype, t2));
6762 : : }
6763 : : break;
6764 : :
6765 : 1328 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6766 : : /* If the second operand is constant, this is a multiplication
6767 : : or floor division, by a power of two, so we can treat it that
6768 : : way unless the multiplier or divisor overflows. Signed
6769 : : left-shift overflow is implementation-defined rather than
6770 : : undefined in C90, so do not convert signed left shift into
6771 : : multiplication. */
6772 : 1328 : if (TREE_CODE (op1) == INTEGER_CST
6773 : 1312 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6774 : : /* const_binop may not detect overflow correctly,
6775 : : so check for it explicitly here. */
6776 : 1195 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6777 : 1337 : wi::to_wide (op1))
6778 : 1186 : && (t1 = fold_convert (ctype,
6779 : : const_binop (LSHIFT_EXPR, size_one_node,
6780 : : op1))) != 0
6781 : 2514 : && !TREE_OVERFLOW (t1))
6782 : 2170 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6783 : : ? MULT_EXPR : FLOOR_DIV_EXPR,
6784 : : ctype,
6785 : : fold_convert (ctype, op0),
6786 : : t1),
6787 : 1186 : c, code, wide_type, strict_overflow_p);
6788 : : break;
6789 : :
6790 : 7744289 : case PLUS_EXPR: case MINUS_EXPR:
6791 : : /* See if we can eliminate the operation on both sides. If we can, we
6792 : : can return a new PLUS or MINUS. If we can't, the only remaining
6793 : : cases where we can do anything are if the second operand is a
6794 : : constant. */
6795 : 7744289 : sub_strict_overflow_p = false;
6796 : 7744289 : t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6797 : 7744289 : t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6798 : 810441 : if (t1 != 0 && t2 != 0
6799 : 275508 : && TYPE_OVERFLOW_WRAPS (ctype)
6800 : 8010578 : && (code == MULT_EXPR
6801 : : /* If not multiplication, we can only do this if both operands
6802 : : are divisible by c. */
6803 : 0 : || (multiple_of_p (ctype, op0, c)
6804 : 0 : && multiple_of_p (ctype, op1, c))))
6805 : : {
6806 : 266289 : if (sub_strict_overflow_p)
6807 : 0 : *strict_overflow_p = true;
6808 : 266289 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6809 : : fold_convert (ctype, t2));
6810 : : }
6811 : :
6812 : : /* If this was a subtraction, negate OP1 and set it to be an addition.
6813 : : This simplifies the logic below. */
6814 : 7478000 : if (tcode == MINUS_EXPR)
6815 : : {
6816 : 1984054 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6817 : : /* If OP1 was not easily negatable, the constant may be OP0. */
6818 : 1984054 : if (TREE_CODE (op0) == INTEGER_CST)
6819 : : {
6820 : 366318 : std::swap (op0, op1);
6821 : 366318 : std::swap (t1, t2);
6822 : : }
6823 : : }
6824 : :
6825 : 7478000 : if (TREE_CODE (op1) != INTEGER_CST)
6826 : : break;
6827 : :
6828 : : /* If either OP1 or C are negative, this optimization is not safe for
6829 : : some of the division and remainder types while for others we need
6830 : : to change the code. */
6831 : 3483781 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6832 : : {
6833 : 171561 : if (code == CEIL_DIV_EXPR)
6834 : : code = FLOOR_DIV_EXPR;
6835 : 171559 : else if (code == FLOOR_DIV_EXPR)
6836 : : code = CEIL_DIV_EXPR;
6837 : 171146 : else if (code != MULT_EXPR
6838 : 171146 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6839 : : break;
6840 : : }
6841 : :
6842 : : /* If it's a multiply or a division/modulus operation of a multiple
6843 : : of our constant, do the operation and verify it doesn't overflow. */
6844 : 3478440 : if (code == MULT_EXPR
6845 : 4693893 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6846 : 1215453 : TYPE_SIGN (type)))
6847 : : {
6848 : 2664178 : op1 = const_binop (code, fold_convert (ctype, op1),
6849 : : fold_convert (ctype, c));
6850 : : /* We allow the constant to overflow with wrapping semantics. */
6851 : 2664178 : if (op1 == 0
6852 : 2664178 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6853 : : break;
6854 : : }
6855 : : else
6856 : : break;
6857 : :
6858 : : /* If we have an unsigned type, we cannot widen the operation since it
6859 : : will change the result if the original computation overflowed. */
6860 : 2660837 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6861 : : break;
6862 : :
6863 : : /* The last case is if we are a multiply. In that case, we can
6864 : : apply the distributive law to commute the multiply and addition
6865 : : if the multiplication of the constants doesn't overflow
6866 : : and overflow is defined. With undefined overflow
6867 : : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6868 : : But fold_plusminus_mult_expr would factor back any power-of-two
6869 : : value so do not distribute in the first place in this case. */
6870 : 2660837 : if (code == MULT_EXPR
6871 : 2260381 : && TYPE_OVERFLOW_WRAPS (ctype)
6872 : 4587552 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6873 : 471465 : return fold_build2 (tcode, ctype,
6874 : : fold_build2 (code, ctype,
6875 : : fold_convert (ctype, op0),
6876 : : fold_convert (ctype, c)),
6877 : : op1);
6878 : :
6879 : : break;
6880 : :
6881 : 2179279 : case MULT_EXPR:
6882 : : /* We have a special case here if we are doing something like
6883 : : (C * 8) % 4 since we know that's zero. */
6884 : 2179279 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6885 : 2179279 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6886 : : /* If the multiplication can overflow we cannot optimize this. */
6887 : 10968 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6888 : 338 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6889 : 2190247 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6890 : 293 : TYPE_SIGN (type)))
6891 : : {
6892 : 8 : *strict_overflow_p = true;
6893 : 8 : return omit_one_operand (type, integer_zero_node, op0);
6894 : : }
6895 : :
6896 : : /* ... fall through ... */
6897 : :
6898 : 2368146 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6899 : 2368146 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6900 : : /* If we can extract our operation from the LHS, do so and return a
6901 : : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6902 : : do something only if the second operand is a constant. */
6903 : 2368146 : if (same_p
6904 : 2039684 : && TYPE_OVERFLOW_WRAPS (ctype)
6905 : 4232271 : && (t1 = extract_muldiv (op0, c, code, wide_type,
6906 : : strict_overflow_p)) != 0)
6907 : 66390 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6908 : : fold_convert (ctype, op1));
6909 : 2301756 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6910 : 1970627 : && TYPE_OVERFLOW_WRAPS (ctype)
6911 : 4096872 : && (t1 = extract_muldiv (op1, c, code, wide_type,
6912 : : strict_overflow_p)) != 0)
6913 : 929474 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6914 : : fold_convert (ctype, t1));
6915 : 1372282 : else if (TREE_CODE (op1) != INTEGER_CST)
6916 : : return 0;
6917 : :
6918 : : /* If these are the same operation types, we can associate them
6919 : : assuming no overflow. */
6920 : 521354 : if (tcode == code)
6921 : : {
6922 : 193423 : bool overflow_p = false;
6923 : 193423 : wi::overflow_type overflow_mul;
6924 : 193423 : signop sign = TYPE_SIGN (ctype);
6925 : 193423 : unsigned prec = TYPE_PRECISION (ctype);
6926 : 386846 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6927 : 193423 : wi::to_wide (c, prec),
6928 : 193423 : sign, &overflow_mul);
6929 : 193423 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6930 : 193423 : if (overflow_mul
6931 : 1291 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6932 : : overflow_p = true;
6933 : 193356 : if (!overflow_p)
6934 : 193356 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6935 : : wide_int_to_tree (ctype, mul));
6936 : 193423 : }
6937 : :
6938 : : /* If these operations "cancel" each other, we have the main
6939 : : optimizations of this pass, which occur when either constant is a
6940 : : multiple of the other, in which case we replace this with either an
6941 : : operation or CODE or TCODE.
6942 : :
6943 : : If we have an unsigned type, we cannot do this since it will change
6944 : : the result if the original computation overflowed. */
6945 : 327998 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6946 : 31824 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6947 : 359779 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6948 : 31741 : || (tcode == MULT_EXPR
6949 : 31741 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6950 : 853 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6951 : 849 : && code != MULT_EXPR)))
6952 : : {
6953 : 883 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6954 : 883 : TYPE_SIGN (type)))
6955 : : {
6956 : 106 : *strict_overflow_p = true;
6957 : 106 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6958 : : fold_convert (ctype,
6959 : : const_binop (TRUNC_DIV_EXPR,
6960 : : op1, c)));
6961 : : }
6962 : 777 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6963 : 777 : TYPE_SIGN (type)))
6964 : : {
6965 : 64 : *strict_overflow_p = true;
6966 : 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6967 : : fold_convert (ctype,
6968 : : const_binop (TRUNC_DIV_EXPR,
6969 : : c, op1)));
6970 : : }
6971 : : }
6972 : : break;
6973 : :
6974 : : default:
6975 : : break;
6976 : : }
6977 : :
6978 : : return 0;
6979 : : }
6980 : :
6981 : : /* Return a node which has the indicated constant VALUE (either 0 or
6982 : : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6983 : : and is of the indicated TYPE. */
6984 : :
6985 : : tree
6986 : 86806845 : constant_boolean_node (bool value, tree type)
6987 : : {
6988 : 86806845 : if (type == integer_type_node)
6989 : 19388811 : return value ? integer_one_node : integer_zero_node;
6990 : 67418034 : else if (type == boolean_type_node)
6991 : 63753504 : return value ? boolean_true_node : boolean_false_node;
6992 : 3664530 : else if (VECTOR_TYPE_P (type))
6993 : 872 : return build_vector_from_val (type,
6994 : 872 : build_int_cst (TREE_TYPE (type),
6995 : 1361 : value ? -1 : 0));
6996 : : else
6997 : 3663658 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6998 : : }
6999 : :
7000 : :
7001 : : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7002 : : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7003 : : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7004 : : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7005 : : COND is the first argument to CODE; otherwise (as in the example
7006 : : given here), it is the second argument. TYPE is the type of the
7007 : : original expression. Return NULL_TREE if no simplification is
7008 : : possible. */
7009 : :
7010 : : static tree
7011 : 986061 : fold_binary_op_with_conditional_arg (location_t loc,
7012 : : enum tree_code code,
7013 : : tree type, tree op0, tree op1,
7014 : : tree cond, tree arg, int cond_first_p)
7015 : : {
7016 : 986061 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7017 : 986061 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7018 : 986061 : tree test, true_value, false_value;
7019 : 986061 : tree lhs = NULL_TREE;
7020 : 986061 : tree rhs = NULL_TREE;
7021 : 986061 : enum tree_code cond_code = COND_EXPR;
7022 : :
7023 : : /* Do not move possibly trapping operations into the conditional as this
7024 : : pessimizes code and causes gimplification issues when applied late. */
7025 : 1006696 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7026 : 203894 : ANY_INTEGRAL_TYPE_P (type)
7027 : 988551 : && TYPE_OVERFLOW_TRAPS (type), op1))
7028 : : return NULL_TREE;
7029 : :
7030 : 965260 : if (TREE_CODE (cond) == COND_EXPR
7031 : 384650 : || TREE_CODE (cond) == VEC_COND_EXPR)
7032 : : {
7033 : 582937 : test = TREE_OPERAND (cond, 0);
7034 : 582937 : true_value = TREE_OPERAND (cond, 1);
7035 : 582937 : false_value = TREE_OPERAND (cond, 2);
7036 : : /* If this operand throws an expression, then it does not make
7037 : : sense to try to perform a logical or arithmetic operation
7038 : : involving it. */
7039 : 582937 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7040 : 7463 : lhs = true_value;
7041 : 582937 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7042 : 6 : rhs = false_value;
7043 : : }
7044 : 382323 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7045 : 382169 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7046 : : {
7047 : 380131 : tree testtype = TREE_TYPE (cond);
7048 : 380131 : test = cond;
7049 : 380131 : true_value = constant_boolean_node (true, testtype);
7050 : 380131 : false_value = constant_boolean_node (false, testtype);
7051 : : }
7052 : : else
7053 : : /* Detect the case of mixing vector and scalar types - bail out. */
7054 : : return NULL_TREE;
7055 : :
7056 : 963068 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7057 : 2481 : cond_code = VEC_COND_EXPR;
7058 : :
7059 : : /* This transformation is only worthwhile if we don't have to wrap ARG
7060 : : in a SAVE_EXPR and the operation can be simplified without recursing
7061 : : on at least one of the branches once its pushed inside the COND_EXPR. */
7062 : 963068 : if (!TREE_CONSTANT (arg)
7063 : 963068 : && (TREE_SIDE_EFFECTS (arg)
7064 : 466054 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7065 : 461810 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7066 : : return NULL_TREE;
7067 : :
7068 : 512472 : arg = fold_convert_loc (loc, arg_type, arg);
7069 : 512472 : if (lhs == 0)
7070 : : {
7071 : 506441 : true_value = fold_convert_loc (loc, cond_type, true_value);
7072 : 506441 : if (cond_first_p)
7073 : 496827 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7074 : : else
7075 : 9614 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7076 : : }
7077 : 512472 : if (rhs == 0)
7078 : : {
7079 : 512466 : false_value = fold_convert_loc (loc, cond_type, false_value);
7080 : 512466 : if (cond_first_p)
7081 : 502291 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7082 : : else
7083 : 10175 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7084 : : }
7085 : :
7086 : : /* Check that we have simplified at least one of the branches. */
7087 : 512472 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7088 : : return NULL_TREE;
7089 : :
7090 : 492838 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7091 : : }
7092 : :
7093 : :
7094 : : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7095 : :
7096 : : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7097 : : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7098 : : if ARG - ZERO_ARG is the same as X.
7099 : :
7100 : : If ARG is NULL, check for any value of type TYPE.
7101 : :
7102 : : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7103 : : and finite. The problematic cases are when X is zero, and its mode
7104 : : has signed zeros. In the case of rounding towards -infinity,
7105 : : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7106 : : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7107 : :
7108 : : bool
7109 : 644789 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7110 : : const_tree zero_arg, int negate)
7111 : : {
7112 : 644789 : if (!real_zerop (zero_arg))
7113 : : return false;
7114 : :
7115 : : /* Don't allow the fold with -fsignaling-nans. */
7116 : 644112 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7117 : : return false;
7118 : :
7119 : : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7120 : 640774 : if (!HONOR_SIGNED_ZEROS (type))
7121 : : return true;
7122 : :
7123 : : /* There is no case that is safe for all rounding modes. */
7124 : 623895 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7125 : : return false;
7126 : :
7127 : : /* In a vector or complex, we would need to check the sign of all zeros. */
7128 : 623232 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7129 : 1467 : zero_arg = uniform_vector_p (zero_arg);
7130 : 623232 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7131 : 1178 : return false;
7132 : :
7133 : : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7134 : 622054 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7135 : 249 : negate = !negate;
7136 : :
7137 : : /* The mode has signed zeros, and we have to honor their sign.
7138 : : In this situation, there are only two cases we can return true for.
7139 : : (i) X - 0 is the same as X with default rounding.
7140 : : (ii) X + 0 is X when X can't possibly be -0.0. */
7141 : 622054 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7142 : : }
7143 : :
7144 : : /* Subroutine of match.pd that optimizes comparisons of a division by
7145 : : a nonzero integer constant against an integer constant, i.e.
7146 : : X/C1 op C2.
7147 : :
7148 : : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7149 : : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7150 : :
7151 : : enum tree_code
7152 : 1759325 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7153 : : tree *hi, bool *neg_overflow)
7154 : : {
7155 : 1759325 : tree prod, tmp, type = TREE_TYPE (c1);
7156 : 1759325 : signop sign = TYPE_SIGN (type);
7157 : 1759325 : wi::overflow_type overflow;
7158 : :
7159 : : /* We have to do this the hard way to detect unsigned overflow.
7160 : : prod = int_const_binop (MULT_EXPR, c1, c2); */
7161 : 1759325 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7162 : 1759325 : prod = force_fit_type (type, val, -1, overflow);
7163 : 1759325 : *neg_overflow = false;
7164 : :
7165 : 1759325 : if (sign == UNSIGNED)
7166 : : {
7167 : 1730550 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7168 : 1730550 : *lo = prod;
7169 : :
7170 : : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7171 : 1730550 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7172 : 1730550 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7173 : : }
7174 : 28775 : else if (tree_int_cst_sgn (c1) >= 0)
7175 : : {
7176 : 27372 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7177 : 27372 : switch (tree_int_cst_sgn (c2))
7178 : : {
7179 : 4712 : case -1:
7180 : 4712 : *neg_overflow = true;
7181 : 4712 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7182 : 4712 : *hi = prod;
7183 : 4712 : break;
7184 : :
7185 : 14102 : case 0:
7186 : 14102 : *lo = fold_negate_const (tmp, type);
7187 : 14102 : *hi = tmp;
7188 : 14102 : break;
7189 : :
7190 : 8558 : case 1:
7191 : 8558 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7192 : 8558 : *lo = prod;
7193 : 8558 : break;
7194 : :
7195 : 0 : default:
7196 : 0 : gcc_unreachable ();
7197 : : }
7198 : : }
7199 : : else
7200 : : {
7201 : : /* A negative divisor reverses the relational operators. */
7202 : 1403 : code = swap_tree_comparison (code);
7203 : :
7204 : 1403 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7205 : 1403 : switch (tree_int_cst_sgn (c2))
7206 : : {
7207 : 132 : case -1:
7208 : 132 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7209 : 132 : *lo = prod;
7210 : 132 : break;
7211 : :
7212 : 173 : case 0:
7213 : 173 : *hi = fold_negate_const (tmp, type);
7214 : 173 : *lo = tmp;
7215 : 173 : break;
7216 : :
7217 : 1098 : case 1:
7218 : 1098 : *neg_overflow = true;
7219 : 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7220 : 1098 : *hi = prod;
7221 : 1098 : break;
7222 : :
7223 : 0 : default:
7224 : 0 : gcc_unreachable ();
7225 : : }
7226 : : }
7227 : :
7228 : 1759325 : if (code != EQ_EXPR && code != NE_EXPR)
7229 : : return code;
7230 : :
7231 : 16609 : if (TREE_OVERFLOW (*lo)
7232 : 16609 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7233 : 709 : *lo = NULL_TREE;
7234 : 16609 : if (TREE_OVERFLOW (*hi)
7235 : 16609 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7236 : 92 : *hi = NULL_TREE;
7237 : :
7238 : : return code;
7239 : 1759325 : }
7240 : :
7241 : : /* Test whether it is preferable to swap two operands, ARG0 and
7242 : : ARG1, for example because ARG0 is an integer constant and ARG1
7243 : : isn't. */
7244 : :
7245 : : bool
7246 : 1560782311 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7247 : : {
7248 : 1560782311 : if (CONSTANT_CLASS_P (arg1))
7249 : : return false;
7250 : 502566297 : if (CONSTANT_CLASS_P (arg0))
7251 : : return true;
7252 : :
7253 : 462430370 : STRIP_NOPS (arg0);
7254 : 462430370 : STRIP_NOPS (arg1);
7255 : :
7256 : 462430370 : if (TREE_CONSTANT (arg1))
7257 : : return false;
7258 : 448100396 : if (TREE_CONSTANT (arg0))
7259 : : return true;
7260 : :
7261 : : /* Put addresses in arg1. */
7262 : 447516758 : if (TREE_CODE (arg1) == ADDR_EXPR)
7263 : : return false;
7264 : 433924339 : if (TREE_CODE (arg0) == ADDR_EXPR)
7265 : : return true;
7266 : :
7267 : : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7268 : : for commutative and comparison operators. Ensuring a canonical
7269 : : form allows the optimizers to find additional redundancies without
7270 : : having to explicitly check for both orderings. */
7271 : 433524188 : if (TREE_CODE (arg0) == SSA_NAME
7272 : 327063734 : && TREE_CODE (arg1) == SSA_NAME
7273 : 754736333 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7274 : : return true;
7275 : :
7276 : : /* Put SSA_NAMEs last. */
7277 : 410882592 : if (TREE_CODE (arg1) == SSA_NAME)
7278 : : return false;
7279 : 97698132 : if (TREE_CODE (arg0) == SSA_NAME)
7280 : : return true;
7281 : :
7282 : : /* Put variables last. */
7283 : 91846543 : if (DECL_P (arg1))
7284 : : return false;
7285 : 48844617 : if (DECL_P (arg0))
7286 : : return true;
7287 : :
7288 : : return false;
7289 : : }
7290 : :
7291 : :
7292 : : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7293 : : means A >= Y && A != MAX, but in this case we know that
7294 : : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7295 : :
7296 : : static tree
7297 : 23033911 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7298 : : {
7299 : 23033911 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7300 : :
7301 : 23033911 : if (TREE_CODE (bound) == LT_EXPR)
7302 : 4901911 : a = TREE_OPERAND (bound, 0);
7303 : 18132000 : else if (TREE_CODE (bound) == GT_EXPR)
7304 : 2557918 : a = TREE_OPERAND (bound, 1);
7305 : : else
7306 : : return NULL_TREE;
7307 : :
7308 : 7459829 : typea = TREE_TYPE (a);
7309 : 7459829 : if (!INTEGRAL_TYPE_P (typea)
7310 : 383203 : && !POINTER_TYPE_P (typea))
7311 : : return NULL_TREE;
7312 : :
7313 : 7273423 : if (TREE_CODE (ineq) == LT_EXPR)
7314 : : {
7315 : 1438904 : a1 = TREE_OPERAND (ineq, 1);
7316 : 1438904 : y = TREE_OPERAND (ineq, 0);
7317 : : }
7318 : 5834519 : else if (TREE_CODE (ineq) == GT_EXPR)
7319 : : {
7320 : 1071629 : a1 = TREE_OPERAND (ineq, 0);
7321 : 1071629 : y = TREE_OPERAND (ineq, 1);
7322 : : }
7323 : : else
7324 : : return NULL_TREE;
7325 : :
7326 : 2510533 : if (TREE_TYPE (a1) != typea)
7327 : : return NULL_TREE;
7328 : :
7329 : 1762338 : if (POINTER_TYPE_P (typea))
7330 : : {
7331 : : /* Convert the pointer types into integer before taking the difference. */
7332 : 9104 : tree ta = fold_convert_loc (loc, ssizetype, a);
7333 : 9104 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7334 : 9104 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7335 : : }
7336 : : else
7337 : 1753234 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7338 : :
7339 : 1762338 : if (!diff || !integer_onep (diff))
7340 : 1751811 : return NULL_TREE;
7341 : :
7342 : 10527 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7343 : : }
7344 : :
7345 : : /* Fold a sum or difference of at least one multiplication.
7346 : : Returns the folded tree or NULL if no simplification could be made. */
7347 : :
7348 : : static tree
7349 : 8682324 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7350 : : tree arg0, tree arg1)
7351 : : {
7352 : 8682324 : tree arg00, arg01, arg10, arg11;
7353 : 8682324 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7354 : :
7355 : : /* (A * C) +- (B * C) -> (A+-B) * C.
7356 : : (A * C) +- A -> A * (C+-1).
7357 : : We are most concerned about the case where C is a constant,
7358 : : but other combinations show up during loop reduction. Since
7359 : : it is not difficult, try all four possibilities. */
7360 : :
7361 : 8682324 : if (TREE_CODE (arg0) == MULT_EXPR)
7362 : : {
7363 : 7676582 : arg00 = TREE_OPERAND (arg0, 0);
7364 : 7676582 : arg01 = TREE_OPERAND (arg0, 1);
7365 : : }
7366 : 1005742 : else if (TREE_CODE (arg0) == INTEGER_CST)
7367 : : {
7368 : 71640 : arg00 = build_one_cst (type);
7369 : 71640 : arg01 = arg0;
7370 : : }
7371 : : else
7372 : : {
7373 : : /* We cannot generate constant 1 for fract. */
7374 : 934102 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7375 : 0 : return NULL_TREE;
7376 : 934102 : arg00 = arg0;
7377 : 934102 : arg01 = build_one_cst (type);
7378 : : }
7379 : 8682324 : if (TREE_CODE (arg1) == MULT_EXPR)
7380 : : {
7381 : 2356025 : arg10 = TREE_OPERAND (arg1, 0);
7382 : 2356025 : arg11 = TREE_OPERAND (arg1, 1);
7383 : : }
7384 : 6326299 : else if (TREE_CODE (arg1) == INTEGER_CST)
7385 : : {
7386 : 3386379 : arg10 = build_one_cst (type);
7387 : : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7388 : : the purpose of this canonicalization. */
7389 : 6539121 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7390 : 236893 : && negate_expr_p (arg1)
7391 : 3620016 : && code == PLUS_EXPR)
7392 : : {
7393 : 233637 : arg11 = negate_expr (arg1);
7394 : 233637 : code = MINUS_EXPR;
7395 : : }
7396 : : else
7397 : : arg11 = arg1;
7398 : : }
7399 : : else
7400 : : {
7401 : : /* We cannot generate constant 1 for fract. */
7402 : 2939920 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7403 : 0 : return NULL_TREE;
7404 : 2939920 : arg10 = arg1;
7405 : 2939920 : arg11 = build_one_cst (type);
7406 : : }
7407 : 8682324 : same = NULL_TREE;
7408 : :
7409 : : /* Prefer factoring a common non-constant. */
7410 : 8682324 : if (operand_equal_p (arg00, arg10, 0))
7411 : : same = arg00, alt0 = arg01, alt1 = arg11;
7412 : 8678403 : else if (operand_equal_p (arg01, arg11, 0))
7413 : : same = arg01, alt0 = arg00, alt1 = arg10;
7414 : 8581766 : else if (operand_equal_p (arg00, arg11, 0))
7415 : : same = arg00, alt0 = arg01, alt1 = arg10;
7416 : 8581728 : else if (operand_equal_p (arg01, arg10, 0))
7417 : : same = arg01, alt0 = arg00, alt1 = arg11;
7418 : :
7419 : : /* No identical multiplicands; see if we can find a common
7420 : : power-of-two factor in non-power-of-two multiplies. This
7421 : : can help in multi-dimensional array access. */
7422 : 8576891 : else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7423 : : {
7424 : 7205419 : HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7425 : 7205419 : HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7426 : 7205419 : HOST_WIDE_INT tmp;
7427 : 7205419 : bool swap = false;
7428 : 7205419 : tree maybe_same;
7429 : :
7430 : : /* Move min of absolute values to int11. */
7431 : 7205419 : if (absu_hwi (int01) < absu_hwi (int11))
7432 : : {
7433 : : tmp = int01, int01 = int11, int11 = tmp;
7434 : : alt0 = arg00, arg00 = arg10, arg10 = alt0;
7435 : : maybe_same = arg01;
7436 : : swap = true;
7437 : : }
7438 : : else
7439 : 3664251 : maybe_same = arg11;
7440 : :
7441 : 7205419 : const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7442 : 7205419 : if (factor > 1
7443 : 9509586 : && pow2p_hwi (factor)
7444 : 2092425 : && (int01 & (factor - 1)) == 0
7445 : : /* The remainder should not be a constant, otherwise we
7446 : : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7447 : : increased the number of multiplications necessary. */
7448 : 8497006 : && TREE_CODE (arg10) != INTEGER_CST)
7449 : : {
7450 : 1159730 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7451 : 1159730 : build_int_cst (TREE_TYPE (arg00),
7452 : 1159730 : int01 / int11));
7453 : 1159730 : alt1 = arg10;
7454 : 1159730 : same = maybe_same;
7455 : 1159730 : if (swap)
7456 : 1047753 : maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7457 : : }
7458 : : }
7459 : :
7460 : 1265163 : if (!same)
7461 : 7417161 : return NULL_TREE;
7462 : :
7463 : 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7464 : 1265163 : || TYPE_OVERFLOW_WRAPS (type)
7465 : : /* We are neither factoring zero nor minus one. */
7466 : 1392893 : || TREE_CODE (same) == INTEGER_CST)
7467 : 1253604 : return fold_build2_loc (loc, MULT_EXPR, type,
7468 : : fold_build2_loc (loc, code, type,
7469 : : fold_convert_loc (loc, type, alt0),
7470 : : fold_convert_loc (loc, type, alt1)),
7471 : 1253604 : fold_convert_loc (loc, type, same));
7472 : :
7473 : : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7474 : : same may be minus one and thus the multiplication may overflow. Perform
7475 : : the sum operation in an unsigned type. */
7476 : 11559 : tree utype = unsigned_type_for (type);
7477 : 11559 : tree tem = fold_build2_loc (loc, code, utype,
7478 : : fold_convert_loc (loc, utype, alt0),
7479 : : fold_convert_loc (loc, utype, alt1));
7480 : : /* If the sum evaluated to a constant that is not -INF the multiplication
7481 : : cannot overflow. */
7482 : 23118 : if (TREE_CODE (tem) == INTEGER_CST
7483 : 18257 : && (wi::to_wide (tem)
7484 : 18257 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7485 : 3336 : return fold_build2_loc (loc, MULT_EXPR, type,
7486 : 3336 : fold_convert (type, tem), same);
7487 : :
7488 : : /* Do not resort to unsigned multiplication because
7489 : : we lose the no-overflow property of the expression. */
7490 : : return NULL_TREE;
7491 : : }
7492 : :
7493 : :
7494 : : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7495 : : into the buffer PTR of length LEN bytes.
7496 : : Return the number of bytes placed in the buffer, or zero
7497 : : upon failure. */
7498 : :
7499 : : int
7500 : 22015233 : native_encode_wide_int (tree type, const wide_int_ref &val,
7501 : : unsigned char *ptr, int len, int off)
7502 : : {
7503 : 22015233 : int total_bytes;
7504 : 22015233 : if (TREE_CODE (type) == BITINT_TYPE)
7505 : : {
7506 : 17093 : struct bitint_info info;
7507 : 17093 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7508 : 17093 : gcc_assert (ok);
7509 : 17093 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7510 : 17093 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7511 : : {
7512 : 16970 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7513 : : /* More work is needed when adding _BitInt support to PDP endian
7514 : : if limb is smaller than word, or if _BitInt limb ordering doesn't
7515 : : match target endianity here. */
7516 : 16970 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7517 : : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7518 : : || (GET_MODE_SIZE (limb_mode)
7519 : : >= UNITS_PER_WORD)));
7520 : : }
7521 : : else
7522 : 246 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7523 : : }
7524 : : else
7525 : 43996280 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7526 : 22015233 : int byte, offset, word, words;
7527 : 22015233 : unsigned char value;
7528 : :
7529 : 22015233 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7530 : : return 0;
7531 : 22014745 : if (off == -1)
7532 : 21106042 : off = 0;
7533 : :
7534 : 22014745 : if (ptr == NULL)
7535 : : /* Dry run. */
7536 : 2779424 : return MIN (len, total_bytes - off);
7537 : :
7538 : : words = total_bytes / UNITS_PER_WORD;
7539 : :
7540 : 93986005 : for (byte = 0; byte < total_bytes; byte++)
7541 : : {
7542 : 74750684 : int bitpos = byte * BITS_PER_UNIT;
7543 : : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7544 : : number of bytes. */
7545 : 74750684 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7546 : :
7547 : 74750684 : if (total_bytes > UNITS_PER_WORD)
7548 : : {
7549 : 74750684 : word = byte / UNITS_PER_WORD;
7550 : 74750684 : if (WORDS_BIG_ENDIAN)
7551 : : word = (words - 1) - word;
7552 : 74750684 : offset = word * UNITS_PER_WORD;
7553 : 74750684 : if (BYTES_BIG_ENDIAN)
7554 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7555 : : else
7556 : 74750684 : offset += byte % UNITS_PER_WORD;
7557 : : }
7558 : : else
7559 : : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7560 : 74750684 : if (offset >= off && offset - off < len)
7561 : 73463395 : ptr[offset - off] = value;
7562 : : }
7563 : 19235321 : return MIN (len, total_bytes - off);
7564 : : }
7565 : :
7566 : : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7567 : : specified by EXPR into the buffer PTR of length LEN bytes.
7568 : : Return the number of bytes placed in the buffer, or zero
7569 : : upon failure. */
7570 : :
7571 : : static int
7572 : 22015233 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7573 : : {
7574 : 22015233 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7575 : 22015233 : ptr, len, off);
7576 : : }
7577 : :
7578 : :
7579 : : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7580 : : specified by EXPR into the buffer PTR of length LEN bytes.
7581 : : Return the number of bytes placed in the buffer, or zero
7582 : : upon failure. */
7583 : :
7584 : : static int
7585 : 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7586 : : {
7587 : 0 : tree type = TREE_TYPE (expr);
7588 : 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7589 : 0 : int total_bytes = GET_MODE_SIZE (mode);
7590 : 0 : FIXED_VALUE_TYPE value;
7591 : 0 : tree i_value, i_type;
7592 : :
7593 : 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7594 : : return 0;
7595 : :
7596 : 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7597 : :
7598 : 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7599 : : return 0;
7600 : :
7601 : 0 : value = TREE_FIXED_CST (expr);
7602 : 0 : i_value = double_int_to_tree (i_type, value.data);
7603 : :
7604 : 0 : return native_encode_int (i_value, ptr, len, off);
7605 : : }
7606 : :
7607 : :
7608 : : /* Subroutine of native_encode_expr. Encode the REAL_CST
7609 : : specified by EXPR into the buffer PTR of length LEN bytes.
7610 : : Return the number of bytes placed in the buffer, or zero
7611 : : upon failure. */
7612 : :
7613 : : int
7614 : 609698 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7615 : : unsigned char *ptr, int len, int off)
7616 : : {
7617 : 609698 : int total_bytes = GET_MODE_SIZE (mode);
7618 : 609698 : int byte, offset, word, words, bitpos;
7619 : 609698 : unsigned char value;
7620 : :
7621 : : /* There are always 32 bits in each long, no matter the size of
7622 : : the hosts long. We handle floating point representations with
7623 : : up to 192 bits. */
7624 : 609698 : long tmp[6];
7625 : :
7626 : 609698 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7627 : : return 0;
7628 : 607871 : if (off == -1)
7629 : 470763 : off = 0;
7630 : :
7631 : 607871 : if (ptr == NULL)
7632 : : /* Dry run. */
7633 : 130040 : return MIN (len, total_bytes - off);
7634 : :
7635 : 477831 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7636 : :
7637 : 477831 : real_to_target (tmp, val, mode);
7638 : :
7639 : 3990129 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7640 : 3512298 : bitpos += BITS_PER_UNIT)
7641 : : {
7642 : 3512298 : byte = (bitpos / BITS_PER_UNIT) & 3;
7643 : 3512298 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7644 : :
7645 : 3512298 : if (UNITS_PER_WORD < 4)
7646 : : {
7647 : : word = byte / UNITS_PER_WORD;
7648 : : if (WORDS_BIG_ENDIAN)
7649 : : word = (words - 1) - word;
7650 : : offset = word * UNITS_PER_WORD;
7651 : : if (BYTES_BIG_ENDIAN)
7652 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7653 : : else
7654 : : offset += byte % UNITS_PER_WORD;
7655 : : }
7656 : : else
7657 : : {
7658 : 3512298 : offset = byte;
7659 : 3512298 : if (BYTES_BIG_ENDIAN)
7660 : : {
7661 : : /* Reverse bytes within each long, or within the entire float
7662 : : if it's smaller than a long (for HFmode). */
7663 : : offset = MIN (3, total_bytes - 1) - offset;
7664 : : gcc_assert (offset >= 0);
7665 : : }
7666 : : }
7667 : 3512298 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7668 : 3512298 : if (offset >= off
7669 : 3509030 : && offset - off < len)
7670 : 3491546 : ptr[offset - off] = value;
7671 : : }
7672 : 477831 : return MIN (len, total_bytes - off);
7673 : : }
7674 : :
7675 : : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7676 : : specified by EXPR into the buffer PTR of length LEN bytes.
7677 : : Return the number of bytes placed in the buffer, or zero
7678 : : upon failure. */
7679 : :
7680 : : static int
7681 : 15424 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7682 : : {
7683 : 15424 : int rsize, isize;
7684 : 15424 : tree part;
7685 : :
7686 : 15424 : part = TREE_REALPART (expr);
7687 : 15424 : rsize = native_encode_expr (part, ptr, len, off);
7688 : 15424 : if (off == -1 && rsize == 0)
7689 : : return 0;
7690 : 15424 : part = TREE_IMAGPART (expr);
7691 : 15424 : if (off != -1)
7692 : 30526 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7693 : 15424 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7694 : : len - rsize, off);
7695 : 15424 : if (off == -1 && isize != rsize)
7696 : : return 0;
7697 : 15424 : return rsize + isize;
7698 : : }
7699 : :
7700 : : /* Like native_encode_vector, but only encode the first COUNT elements.
7701 : : The other arguments are as for native_encode_vector. */
7702 : :
7703 : : static int
7704 : 879031 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7705 : : int off, unsigned HOST_WIDE_INT count)
7706 : : {
7707 : 879031 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7708 : 1758062 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7709 : 880063 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7710 : : {
7711 : : /* This is the only case in which elements can be smaller than a byte.
7712 : : Element 0 is always in the lsb of the containing byte. */
7713 : 954 : unsigned int elt_bits = TYPE_PRECISION (itype);
7714 : 954 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7715 : 954 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7716 : : return 0;
7717 : :
7718 : 954 : if (off == -1)
7719 : 954 : off = 0;
7720 : :
7721 : : /* Zero the buffer and then set bits later where necessary. */
7722 : 954 : int extract_bytes = MIN (len, total_bytes - off);
7723 : 954 : if (ptr)
7724 : 954 : memset (ptr, 0, extract_bytes);
7725 : :
7726 : 954 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7727 : 954 : unsigned int first_elt = off * elts_per_byte;
7728 : 954 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7729 : 954 : unsigned int elt_mask = (1 << elt_bits) - 1;
7730 : 17491 : for (unsigned int i = 0; i < extract_elts; ++i)
7731 : : {
7732 : 16537 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7733 : 16537 : if (TREE_CODE (elt) != INTEGER_CST)
7734 : : return 0;
7735 : :
7736 : 16537 : if (ptr && integer_nonzerop (elt))
7737 : : {
7738 : 8477 : unsigned int bit = i * elt_bits;
7739 : 8477 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7740 : : }
7741 : : }
7742 : : return extract_bytes;
7743 : : }
7744 : :
7745 : 878077 : int offset = 0;
7746 : 878077 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7747 : 2880660 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7748 : : {
7749 : 2546919 : if (off >= size)
7750 : : {
7751 : 22833 : off -= size;
7752 : 22833 : continue;
7753 : : }
7754 : 2524086 : tree elem = VECTOR_CST_ELT (expr, i);
7755 : 2524086 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7756 : : len - offset, off);
7757 : 2524086 : if ((off == -1 && res != size) || res == 0)
7758 : : return 0;
7759 : 2523557 : offset += res;
7760 : 2523557 : if (offset >= len)
7761 : 543807 : return (off == -1 && i < count - 1) ? 0 : offset;
7762 : 1979750 : if (off != -1)
7763 : 379490 : off = 0;
7764 : : }
7765 : : return offset;
7766 : : }
7767 : :
7768 : : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7769 : : specified by EXPR into the buffer PTR of length LEN bytes.
7770 : : Return the number of bytes placed in the buffer, or zero
7771 : : upon failure. */
7772 : :
7773 : : static int
7774 : 759543 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7775 : : {
7776 : 759543 : unsigned HOST_WIDE_INT count;
7777 : 759543 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7778 : : return 0;
7779 : 759543 : return native_encode_vector_part (expr, ptr, len, off, count);
7780 : : }
7781 : :
7782 : :
7783 : : /* Subroutine of native_encode_expr. Encode the STRING_CST
7784 : : specified by EXPR into the buffer PTR of length LEN bytes.
7785 : : Return the number of bytes placed in the buffer, or zero
7786 : : upon failure. */
7787 : :
7788 : : static int
7789 : 136577 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7790 : : {
7791 : 136577 : tree type = TREE_TYPE (expr);
7792 : :
7793 : : /* Wide-char strings are encoded in target byte-order so native
7794 : : encoding them is trivial. */
7795 : 136577 : if (BITS_PER_UNIT != CHAR_BIT
7796 : 136577 : || TREE_CODE (type) != ARRAY_TYPE
7797 : 136577 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7798 : 273154 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7799 : : return 0;
7800 : :
7801 : 136577 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7802 : 136577 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7803 : : return 0;
7804 : 135705 : if (off == -1)
7805 : 55237 : off = 0;
7806 : 135705 : len = MIN (total_bytes - off, len);
7807 : 135705 : if (ptr == NULL)
7808 : : /* Dry run. */;
7809 : : else
7810 : : {
7811 : 135705 : int written = 0;
7812 : 135705 : if (off < TREE_STRING_LENGTH (expr))
7813 : : {
7814 : 135230 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7815 : 135230 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7816 : : }
7817 : 135705 : memset (ptr + written, 0, len - written);
7818 : : }
7819 : : return len;
7820 : : }
7821 : :
7822 : : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7823 : : specified by EXPR into the buffer PTR of length LEN bytes.
7824 : : Return the number of bytes placed in the buffer, or zero
7825 : : upon failure. */
7826 : :
7827 : : static int
7828 : 45294 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7829 : : {
7830 : : /* We are only concerned with zero-initialization constructors here. That's
7831 : : all we expect to see in GIMPLE, so that's all native_encode_expr should
7832 : : deal with. For more general handling of constructors, there is
7833 : : native_encode_initializer. */
7834 : 45294 : if (CONSTRUCTOR_NELTS (expr))
7835 : : return 0;
7836 : :
7837 : : /* Wide-char strings are encoded in target byte-order so native
7838 : : encoding them is trivial. */
7839 : 84394 : if (BITS_PER_UNIT != CHAR_BIT
7840 : 42197 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7841 : : return 0;
7842 : :
7843 : 42197 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7844 : 42197 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7845 : : return 0;
7846 : 42197 : if (off == -1)
7847 : 0 : off = 0;
7848 : 42197 : len = MIN (total_bytes - off, len);
7849 : 42197 : if (ptr == NULL)
7850 : : /* Dry run. */;
7851 : : else
7852 : 42197 : memset (ptr, 0, len);
7853 : : return len;
7854 : : }
7855 : :
7856 : : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7857 : : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7858 : : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7859 : : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7860 : : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7861 : : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7862 : : Return the number of bytes placed in the buffer, or zero upon failure. */
7863 : :
7864 : : int
7865 : 36642228 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7866 : : {
7867 : : /* We don't support starting at negative offset and -1 is special. */
7868 : 36642228 : if (off < -1)
7869 : : return 0;
7870 : :
7871 : 36642216 : switch (TREE_CODE (expr))
7872 : : {
7873 : 22012994 : case INTEGER_CST:
7874 : 22012994 : return native_encode_int (expr, ptr, len, off);
7875 : :
7876 : 609698 : case REAL_CST:
7877 : 609698 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7878 : 1219396 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7879 : :
7880 : 0 : case FIXED_CST:
7881 : 0 : return native_encode_fixed (expr, ptr, len, off);
7882 : :
7883 : 15424 : case COMPLEX_CST:
7884 : 15424 : return native_encode_complex (expr, ptr, len, off);
7885 : :
7886 : 759543 : case VECTOR_CST:
7887 : 759543 : return native_encode_vector (expr, ptr, len, off);
7888 : :
7889 : 136577 : case STRING_CST:
7890 : 136577 : return native_encode_string (expr, ptr, len, off);
7891 : :
7892 : 45294 : case CONSTRUCTOR:
7893 : 45294 : return native_encode_constructor (expr, ptr, len, off);
7894 : :
7895 : : default:
7896 : : return 0;
7897 : : }
7898 : : }
7899 : :
7900 : : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7901 : : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7902 : : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7903 : : machine modes, we can't just use build_nonstandard_integer_type. */
7904 : :
7905 : : tree
7906 : 541 : find_bitfield_repr_type (int fieldsize, int len)
7907 : : {
7908 : 541 : machine_mode mode;
7909 : 1063 : for (int pass = 0; pass < 2; pass++)
7910 : : {
7911 : 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7912 : 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7913 : 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7914 : 7286 : && known_eq (GET_MODE_PRECISION (mode),
7915 : : GET_MODE_BITSIZE (mode))
7916 : 11274 : && known_le (GET_MODE_SIZE (mode), len))
7917 : : {
7918 : 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7919 : 280 : if (ret && TYPE_MODE (ret) == mode)
7920 : : return ret;
7921 : : }
7922 : : }
7923 : :
7924 : 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7925 : 261 : if (int_n_enabled_p[i]
7926 : 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7927 : 261 : && int_n_trees[i].unsigned_type)
7928 : : {
7929 : 261 : tree ret = int_n_trees[i].unsigned_type;
7930 : 261 : mode = TYPE_MODE (ret);
7931 : 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7932 : 522 : && known_eq (GET_MODE_PRECISION (mode),
7933 : : GET_MODE_BITSIZE (mode))
7934 : 783 : && known_le (GET_MODE_SIZE (mode), len))
7935 : : return ret;
7936 : : }
7937 : :
7938 : : return NULL_TREE;
7939 : : }
7940 : :
7941 : : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7942 : : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7943 : : to be non-NULL and OFF zero), then in addition to filling the
7944 : : bytes pointed by PTR with the value also clear any bits pointed
7945 : : by MASK that are known to be initialized, keep them as is for
7946 : : e.g. uninitialized padding bits or uninitialized fields. */
7947 : :
7948 : : int
7949 : 14820886 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7950 : : int off, unsigned char *mask)
7951 : : {
7952 : 14820886 : int r;
7953 : :
7954 : : /* We don't support starting at negative offset and -1 is special. */
7955 : 14820886 : if (off < -1 || init == NULL_TREE)
7956 : : return 0;
7957 : :
7958 : 14820886 : gcc_assert (mask == NULL || (off == 0 && ptr));
7959 : :
7960 : 14820886 : STRIP_NOPS (init);
7961 : 14820886 : switch (TREE_CODE (init))
7962 : : {
7963 : 0 : case VIEW_CONVERT_EXPR:
7964 : 0 : case NON_LVALUE_EXPR:
7965 : 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7966 : 0 : mask);
7967 : 13838547 : default:
7968 : 13838547 : r = native_encode_expr (init, ptr, len, off);
7969 : 13838547 : if (mask)
7970 : 1514 : memset (mask, 0, r);
7971 : : return r;
7972 : 982339 : case CONSTRUCTOR:
7973 : 982339 : tree type = TREE_TYPE (init);
7974 : 982339 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7975 : 982339 : if (total_bytes < 0)
7976 : : return 0;
7977 : 982339 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7978 : : return 0;
7979 : 982339 : int o = off == -1 ? 0 : off;
7980 : 982339 : if (TREE_CODE (type) == ARRAY_TYPE)
7981 : : {
7982 : 261288 : tree min_index;
7983 : 261288 : unsigned HOST_WIDE_INT cnt;
7984 : 261288 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7985 : 261288 : constructor_elt *ce;
7986 : :
7987 : 261288 : if (!TYPE_DOMAIN (type)
7988 : 261288 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7989 : : return 0;
7990 : :
7991 : 261288 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7992 : 261288 : if (fieldsize <= 0)
7993 : : return 0;
7994 : :
7995 : 261288 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7996 : 261288 : if (ptr)
7997 : 261288 : memset (ptr, '\0', MIN (total_bytes - off, len));
7998 : :
7999 : 13749120 : for (cnt = 0; ; cnt++)
8000 : : {
8001 : 14010408 : tree val = NULL_TREE, index = NULL_TREE;
8002 : 14010408 : HOST_WIDE_INT pos = curpos, count = 0;
8003 : 14010408 : bool full = false;
8004 : 14010408 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8005 : : {
8006 : 13962460 : val = ce->value;
8007 : 13962460 : index = ce->index;
8008 : : }
8009 : 47948 : else if (mask == NULL
8010 : 228 : || CONSTRUCTOR_NO_CLEARING (init)
8011 : 48176 : || curpos >= total_bytes)
8012 : : break;
8013 : : else
8014 : : pos = total_bytes;
8015 : :
8016 : 13962460 : if (index && TREE_CODE (index) == RANGE_EXPR)
8017 : : {
8018 : 16 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8019 : 16 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8020 : 0 : return 0;
8021 : 16 : offset_int wpos
8022 : 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8023 : 32 : - wi::to_offset (min_index),
8024 : 16 : TYPE_PRECISION (sizetype));
8025 : 16 : wpos *= fieldsize;
8026 : 16 : if (!wi::fits_shwi_p (pos))
8027 : : return 0;
8028 : 16 : pos = wpos.to_shwi ();
8029 : 16 : offset_int wcount
8030 : 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8031 : 32 : - wi::to_offset (TREE_OPERAND (index, 0)),
8032 : 16 : TYPE_PRECISION (sizetype));
8033 : 16 : if (!wi::fits_shwi_p (wcount))
8034 : : return 0;
8035 : 16 : count = wcount.to_shwi ();
8036 : 16 : }
8037 : 13345701 : else if (index)
8038 : : {
8039 : 13345701 : if (TREE_CODE (index) != INTEGER_CST)
8040 : 0 : return 0;
8041 : 13345701 : offset_int wpos
8042 : 13345701 : = wi::sext (wi::to_offset (index)
8043 : 26691402 : - wi::to_offset (min_index),
8044 : 13345701 : TYPE_PRECISION (sizetype));
8045 : 13345701 : wpos *= fieldsize;
8046 : 13345701 : if (!wi::fits_shwi_p (wpos))
8047 : : return 0;
8048 : 13345701 : pos = wpos.to_shwi ();
8049 : : }
8050 : :
8051 : 13963131 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8052 : : {
8053 : 14 : if (valueinit == -1)
8054 : : {
8055 : 14 : tree zero = build_zero_cst (TREE_TYPE (type));
8056 : 28 : r = native_encode_initializer (zero, ptr + curpos,
8057 : : fieldsize, 0,
8058 : 14 : mask + curpos);
8059 : 14 : if (TREE_CODE (zero) == CONSTRUCTOR)
8060 : 0 : ggc_free (zero);
8061 : 14 : if (!r)
8062 : : return 0;
8063 : 14 : valueinit = curpos;
8064 : 14 : curpos += fieldsize;
8065 : : }
8066 : 44 : while (curpos != pos)
8067 : : {
8068 : 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8069 : 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8070 : 30 : curpos += fieldsize;
8071 : : }
8072 : : }
8073 : :
8074 : 13962474 : curpos = pos;
8075 : 13962474 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8076 : : {
8077 : 486 : if (count)
8078 : : return 0;
8079 : 486 : if (off == -1
8080 : 486 : || (curpos >= off
8081 : 0 : && (curpos + RAW_DATA_LENGTH (val)
8082 : 0 : <= (HOST_WIDE_INT) off + len)))
8083 : : {
8084 : 486 : if (ptr)
8085 : 486 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8086 : 486 : RAW_DATA_LENGTH (val));
8087 : 486 : if (mask)
8088 : 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8089 : : }
8090 : 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8091 : 0 : && curpos < (HOST_WIDE_INT) off + len)
8092 : : {
8093 : : /* Partial overlap. */
8094 : 0 : unsigned char *p = NULL;
8095 : 0 : int no = 0;
8096 : 0 : int l;
8097 : 0 : gcc_assert (mask == NULL);
8098 : 0 : if (curpos >= off)
8099 : : {
8100 : 0 : if (ptr)
8101 : 0 : p = ptr + curpos - off;
8102 : 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8103 : : RAW_DATA_LENGTH (val));
8104 : : }
8105 : : else
8106 : : {
8107 : 0 : p = ptr;
8108 : 0 : no = off - curpos;
8109 : 0 : l = len;
8110 : : }
8111 : 0 : if (p)
8112 : 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8113 : : }
8114 : 486 : curpos += RAW_DATA_LENGTH (val);
8115 : 486 : val = NULL_TREE;
8116 : : }
8117 : 486 : if (val)
8118 : 14040014 : do
8119 : : {
8120 : 14040014 : if (off == -1
8121 : 720875 : || (curpos >= off
8122 : 232797 : && (curpos + fieldsize
8123 : 232797 : <= (HOST_WIDE_INT) off + len)))
8124 : : {
8125 : 13522068 : if (full)
8126 : : {
8127 : 78040 : if (ptr)
8128 : 78040 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8129 : : fieldsize);
8130 : 78040 : if (mask)
8131 : 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8132 : : }
8133 : 27091628 : else if (!native_encode_initializer (val,
8134 : : ptr
8135 : 13444028 : ? ptr + curpos - o
8136 : : : NULL,
8137 : : fieldsize,
8138 : : off == -1 ? -1
8139 : : : 0,
8140 : : mask
8141 : 643 : ? mask + curpos
8142 : : : NULL))
8143 : : return 0;
8144 : : else
8145 : : {
8146 : : full = true;
8147 : : pos = curpos;
8148 : : }
8149 : : }
8150 : 517946 : else if (curpos + fieldsize > off
8151 : 32282 : && curpos < (HOST_WIDE_INT) off + len)
8152 : : {
8153 : : /* Partial overlap. */
8154 : 9227 : unsigned char *p = NULL;
8155 : 9227 : int no = 0;
8156 : 9227 : int l;
8157 : 9227 : gcc_assert (mask == NULL);
8158 : 9227 : if (curpos >= off)
8159 : : {
8160 : 6813 : if (ptr)
8161 : 6813 : p = ptr + curpos - off;
8162 : 6813 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8163 : : fieldsize);
8164 : : }
8165 : : else
8166 : : {
8167 : 2414 : p = ptr;
8168 : 2414 : no = off - curpos;
8169 : 2414 : l = len;
8170 : : }
8171 : 9227 : if (!native_encode_initializer (val, p, l, no, NULL))
8172 : : return 0;
8173 : : }
8174 : 13826660 : curpos += fieldsize;
8175 : : }
8176 : 13826660 : while (count-- != 0);
8177 : 13749120 : }
8178 : 47934 : return MIN (total_bytes - off, len);
8179 : : }
8180 : 721051 : else if (TREE_CODE (type) == RECORD_TYPE
8181 : 721051 : || TREE_CODE (type) == UNION_TYPE)
8182 : : {
8183 : 721051 : unsigned HOST_WIDE_INT cnt;
8184 : 721051 : constructor_elt *ce;
8185 : 721051 : tree fld_base = TYPE_FIELDS (type);
8186 : 721051 : tree to_free = NULL_TREE;
8187 : :
8188 : 721051 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8189 : 721051 : if (ptr != NULL)
8190 : 721051 : memset (ptr, '\0', MIN (total_bytes - o, len));
8191 : 125168 : for (cnt = 0; ; cnt++)
8192 : : {
8193 : 846219 : tree val = NULL_TREE, field = NULL_TREE;
8194 : 846219 : HOST_WIDE_INT pos = 0, fieldsize;
8195 : 846219 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8196 : :
8197 : 846219 : if (to_free)
8198 : : {
8199 : 0 : ggc_free (to_free);
8200 : 0 : to_free = NULL_TREE;
8201 : : }
8202 : :
8203 : 846219 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8204 : : {
8205 : 148804 : val = ce->value;
8206 : 148804 : field = ce->index;
8207 : 148804 : if (field == NULL_TREE)
8208 : : return 0;
8209 : :
8210 : 148804 : pos = int_byte_position (field);
8211 : 148804 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8212 : 1433 : continue;
8213 : : }
8214 : 697415 : else if (mask == NULL
8215 : 697415 : || CONSTRUCTOR_NO_CLEARING (init))
8216 : : break;
8217 : : else
8218 : : pos = total_bytes;
8219 : :
8220 : 149592 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8221 : : {
8222 : : tree fld;
8223 : 11825 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8224 : : {
8225 : 11320 : if (TREE_CODE (fld) != FIELD_DECL)
8226 : 10155 : continue;
8227 : 1165 : if (fld == field)
8228 : : break;
8229 : 146 : if (DECL_PADDING_P (fld))
8230 : 87 : continue;
8231 : 59 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8232 : 59 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8233 : : return 0;
8234 : 59 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8235 : 2 : continue;
8236 : : break;
8237 : : }
8238 : 1581 : if (fld == NULL_TREE)
8239 : : {
8240 : 505 : if (ce == NULL)
8241 : : break;
8242 : : return 0;
8243 : : }
8244 : 1076 : fld_base = DECL_CHAIN (fld);
8245 : 1076 : if (fld != field)
8246 : : {
8247 : 57 : cnt--;
8248 : 57 : field = fld;
8249 : 57 : pos = int_byte_position (field);
8250 : 57 : val = build_zero_cst (TREE_TYPE (fld));
8251 : 57 : if (TREE_CODE (val) == CONSTRUCTOR)
8252 : 0 : to_free = val;
8253 : : }
8254 : : }
8255 : :
8256 : 147428 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8257 : 6885 : && TYPE_DOMAIN (TREE_TYPE (field))
8258 : 154313 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8259 : : {
8260 : 81 : if (mask || off != -1)
8261 : : return 0;
8262 : 81 : if (val == NULL_TREE)
8263 : 0 : continue;
8264 : 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8265 : : return 0;
8266 : 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8267 : 81 : if (fieldsize < 0
8268 : 81 : || (int) fieldsize != fieldsize
8269 : 81 : || (pos + fieldsize) > INT_MAX)
8270 : : return 0;
8271 : 81 : if (pos + fieldsize > total_bytes)
8272 : : {
8273 : 81 : if (ptr != NULL && total_bytes < len)
8274 : 81 : memset (ptr + total_bytes, '\0',
8275 : 81 : MIN (pos + fieldsize, len) - total_bytes);
8276 : : total_bytes = pos + fieldsize;
8277 : : }
8278 : : }
8279 : : else
8280 : : {
8281 : 147347 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8282 : 147347 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8283 : : return 0;
8284 : 147347 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8285 : : }
8286 : 147428 : if (fieldsize == 0)
8287 : 1 : continue;
8288 : :
8289 : : /* Prepare to deal with integral bit-fields and filter out other
8290 : : bit-fields that do not start and end on a byte boundary. */
8291 : 147427 : if (DECL_BIT_FIELD (field))
8292 : : {
8293 : 2704 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8294 : : return 0;
8295 : 2704 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8296 : 2704 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8297 : : {
8298 : 2704 : bpos %= BITS_PER_UNIT;
8299 : 2704 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8300 : 2704 : epos = fieldsize % BITS_PER_UNIT;
8301 : 2704 : fieldsize += BITS_PER_UNIT - 1;
8302 : 2704 : fieldsize /= BITS_PER_UNIT;
8303 : : }
8304 : 0 : else if (bpos % BITS_PER_UNIT
8305 : 0 : || DECL_SIZE (field) == NULL_TREE
8306 : 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8307 : 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8308 : : return 0;
8309 : : }
8310 : :
8311 : 147427 : if (off != -1 && pos + fieldsize <= off)
8312 : 3298 : continue;
8313 : :
8314 : 144129 : if (val == NULL_TREE)
8315 : 0 : continue;
8316 : :
8317 : 144129 : if (DECL_BIT_FIELD (field)
8318 : 144129 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8319 : : {
8320 : : /* FIXME: Handle PDP endian. */
8321 : 2500 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8322 : 261 : return 0;
8323 : :
8324 : 2500 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8325 : 6 : val = TREE_OPERAND (val, 0);
8326 : 2500 : if (TREE_CODE (val) != INTEGER_CST)
8327 : : return 0;
8328 : :
8329 : 2500 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8330 : 2500 : tree repr_type = NULL_TREE;
8331 : 2500 : HOST_WIDE_INT rpos = 0;
8332 : 2500 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8333 : : {
8334 : 1971 : rpos = int_byte_position (repr);
8335 : 1971 : repr_type = TREE_TYPE (repr);
8336 : : }
8337 : : else
8338 : : {
8339 : 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8340 : 529 : if (repr_type == NULL_TREE)
8341 : : return 0;
8342 : 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8343 : 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8344 : 268 : if (pos + repr_size <= o + len)
8345 : : rpos = pos;
8346 : : else
8347 : : {
8348 : 14 : rpos = o + len - repr_size;
8349 : 14 : gcc_assert (rpos <= pos);
8350 : : }
8351 : : }
8352 : :
8353 : 2239 : if (rpos > pos)
8354 : : return 0;
8355 : 2239 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8356 : 2239 : int diff = (TYPE_PRECISION (repr_type)
8357 : 2239 : - TYPE_PRECISION (TREE_TYPE (field)));
8358 : 2239 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8359 : 2239 : if (!BYTES_BIG_ENDIAN)
8360 : 2239 : w = wi::lshift (w, bitoff);
8361 : : else
8362 : : w = wi::lshift (w, diff - bitoff);
8363 : 2239 : val = wide_int_to_tree (repr_type, w);
8364 : :
8365 : 2239 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8366 : : / BITS_PER_UNIT + 1];
8367 : 2239 : int l = native_encode_int (val, buf, sizeof buf, 0);
8368 : 2239 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8369 : 0 : return 0;
8370 : :
8371 : 2239 : if (ptr == NULL)
8372 : 0 : continue;
8373 : :
8374 : : /* If the bitfield does not start at byte boundary, handle
8375 : : the partial byte at the start. */
8376 : 2239 : if (bpos
8377 : 1342 : && (off == -1 || (pos >= off && len >= 1)))
8378 : : {
8379 : 1267 : if (!BYTES_BIG_ENDIAN)
8380 : : {
8381 : 1267 : int msk = (1 << bpos) - 1;
8382 : 1267 : buf[pos - rpos] &= ~msk;
8383 : 1267 : buf[pos - rpos] |= ptr[pos - o] & msk;
8384 : 1267 : if (mask)
8385 : : {
8386 : 147 : if (fieldsize > 1 || epos == 0)
8387 : 129 : mask[pos] &= msk;
8388 : : else
8389 : 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8390 : : }
8391 : : }
8392 : : else
8393 : : {
8394 : : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8395 : : buf[pos - rpos] &= msk;
8396 : : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8397 : : if (mask)
8398 : : {
8399 : : if (fieldsize > 1 || epos == 0)
8400 : : mask[pos] &= ~msk;
8401 : : else
8402 : : mask[pos] &= (~msk
8403 : : | ((1 << (BITS_PER_UNIT - epos))
8404 : : - 1));
8405 : : }
8406 : : }
8407 : : }
8408 : : /* If the bitfield does not end at byte boundary, handle
8409 : : the partial byte at the end. */
8410 : 2239 : if (epos
8411 : 1717 : && (off == -1
8412 : 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8413 : : {
8414 : 1614 : if (!BYTES_BIG_ENDIAN)
8415 : : {
8416 : 1614 : int msk = (1 << epos) - 1;
8417 : 1614 : buf[pos - rpos + fieldsize - 1] &= msk;
8418 : 1614 : buf[pos - rpos + fieldsize - 1]
8419 : 1614 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8420 : 1614 : if (mask && (fieldsize > 1 || bpos == 0))
8421 : 156 : mask[pos + fieldsize - 1] &= ~msk;
8422 : : }
8423 : : else
8424 : : {
8425 : : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8426 : : buf[pos - rpos + fieldsize - 1] &= ~msk;
8427 : : buf[pos - rpos + fieldsize - 1]
8428 : : |= ptr[pos + fieldsize - 1 - o] & msk;
8429 : : if (mask && (fieldsize > 1 || bpos == 0))
8430 : : mask[pos + fieldsize - 1] &= msk;
8431 : : }
8432 : : }
8433 : 2239 : if (off == -1
8434 : 1301 : || (pos >= off
8435 : 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8436 : : {
8437 : 2048 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8438 : 2048 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8439 : 75 : memset (mask + pos + (bpos != 0), 0,
8440 : 75 : fieldsize - (bpos != 0) - (epos != 0));
8441 : : }
8442 : : else
8443 : : {
8444 : : /* Partial overlap. */
8445 : 191 : HOST_WIDE_INT fsz = fieldsize;
8446 : 191 : gcc_assert (mask == NULL);
8447 : 191 : if (pos < off)
8448 : : {
8449 : 89 : fsz -= (off - pos);
8450 : 89 : pos = off;
8451 : : }
8452 : 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8453 : 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8454 : 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8455 : : }
8456 : 2239 : continue;
8457 : 2239 : }
8458 : :
8459 : 141629 : if (off == -1
8460 : 19748 : || (pos >= off
8461 : 18954 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8462 : : {
8463 : 132168 : int fldsize = fieldsize;
8464 : 10287 : if (off == -1)
8465 : : {
8466 : 121881 : tree fld = DECL_CHAIN (field);
8467 : 1594082 : while (fld)
8468 : : {
8469 : 1482278 : if (TREE_CODE (fld) == FIELD_DECL)
8470 : : break;
8471 : 1472201 : fld = DECL_CHAIN (fld);
8472 : : }
8473 : 121881 : if (fld == NULL_TREE)
8474 : 111804 : fldsize = len - pos;
8475 : : }
8476 : 143291 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8477 : : : NULL,
8478 : : fldsize,
8479 : : off == -1 ? -1 : 0,
8480 : 836 : mask ? mask + pos : NULL);
8481 : 132168 : if (!r)
8482 : : return 0;
8483 : 117214 : if (off == -1
8484 : 115297 : && fldsize != fieldsize
8485 : 324 : && r > fieldsize
8486 : 54 : && pos + r > total_bytes)
8487 : 125168 : total_bytes = pos + r;
8488 : : }
8489 : : else
8490 : : {
8491 : : /* Partial overlap. */
8492 : 9461 : unsigned char *p = NULL;
8493 : 9461 : int no = 0;
8494 : 9461 : int l;
8495 : 9461 : gcc_assert (mask == NULL);
8496 : 9461 : if (pos >= off)
8497 : : {
8498 : 8667 : if (ptr)
8499 : 8667 : p = ptr + pos - off;
8500 : 8667 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8501 : : fieldsize);
8502 : : }
8503 : : else
8504 : : {
8505 : 794 : p = ptr;
8506 : 794 : no = off - pos;
8507 : 794 : l = len;
8508 : : }
8509 : 9461 : if (!native_encode_initializer (val, p, l, no, NULL))
8510 : : return 0;
8511 : : }
8512 : 125168 : }
8513 : 697358 : return MIN (total_bytes - off, len);
8514 : : }
8515 : : return 0;
8516 : : }
8517 : : }
8518 : :
8519 : :
8520 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8521 : : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8522 : : If the buffer cannot be interpreted, return NULL_TREE. */
8523 : :
8524 : : static tree
8525 : 2799713 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8526 : : {
8527 : 2799713 : int total_bytes;
8528 : 2799713 : if (TREE_CODE (type) == BITINT_TYPE)
8529 : : {
8530 : 17 : struct bitint_info info;
8531 : 17 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8532 : 17 : gcc_assert (ok);
8533 : 17 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8534 : 17 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8535 : : {
8536 : 17 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8537 : : /* More work is needed when adding _BitInt support to PDP endian
8538 : : if limb is smaller than word, or if _BitInt limb ordering doesn't
8539 : : match target endianity here. */
8540 : 17 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8541 : : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8542 : : || (GET_MODE_SIZE (limb_mode)
8543 : : >= UNITS_PER_WORD)));
8544 : : }
8545 : : else
8546 : 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8547 : : }
8548 : : else
8549 : 5599392 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8550 : :
8551 : 2799713 : if (total_bytes > len)
8552 : : return NULL_TREE;
8553 : :
8554 : 2799473 : wide_int result = wi::from_buffer (ptr, total_bytes);
8555 : :
8556 : 2799473 : return wide_int_to_tree (type, result);
8557 : 2799473 : }
8558 : :
8559 : :
8560 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8561 : : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8562 : : If the buffer cannot be interpreted, return NULL_TREE. */
8563 : :
8564 : : static tree
8565 : 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8566 : : {
8567 : 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8568 : 0 : int total_bytes = GET_MODE_SIZE (mode);
8569 : 0 : double_int result;
8570 : 0 : FIXED_VALUE_TYPE fixed_value;
8571 : :
8572 : 0 : if (total_bytes > len
8573 : 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8574 : : return NULL_TREE;
8575 : :
8576 : 0 : result = double_int::from_buffer (ptr, total_bytes);
8577 : 0 : fixed_value = fixed_from_double_int (result, mode);
8578 : :
8579 : 0 : return build_fixed (type, fixed_value);
8580 : : }
8581 : :
8582 : :
8583 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8584 : : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8585 : : If the buffer cannot be interpreted, return NULL_TREE. */
8586 : :
8587 : : tree
8588 : 32030 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8589 : : {
8590 : 32030 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8591 : 32030 : int total_bytes = GET_MODE_SIZE (mode);
8592 : 32030 : unsigned char value;
8593 : : /* There are always 32 bits in each long, no matter the size of
8594 : : the hosts long. We handle floating point representations with
8595 : : up to 192 bits. */
8596 : 32030 : REAL_VALUE_TYPE r;
8597 : 32030 : long tmp[6];
8598 : :
8599 : 32030 : if (total_bytes > len || total_bytes > 24)
8600 : : return NULL_TREE;
8601 : 31969 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8602 : :
8603 : 31969 : memset (tmp, 0, sizeof (tmp));
8604 : 228453 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8605 : 196484 : bitpos += BITS_PER_UNIT)
8606 : : {
8607 : : /* Both OFFSET and BYTE index within a long;
8608 : : bitpos indexes the whole float. */
8609 : 196484 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8610 : 196484 : if (UNITS_PER_WORD < 4)
8611 : : {
8612 : : int word = byte / UNITS_PER_WORD;
8613 : : if (WORDS_BIG_ENDIAN)
8614 : : word = (words - 1) - word;
8615 : : offset = word * UNITS_PER_WORD;
8616 : : if (BYTES_BIG_ENDIAN)
8617 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8618 : : else
8619 : : offset += byte % UNITS_PER_WORD;
8620 : : }
8621 : : else
8622 : : {
8623 : 196484 : offset = byte;
8624 : 196484 : if (BYTES_BIG_ENDIAN)
8625 : : {
8626 : : /* Reverse bytes within each long, or within the entire float
8627 : : if it's smaller than a long (for HFmode). */
8628 : : offset = MIN (3, total_bytes - 1) - offset;
8629 : : gcc_assert (offset >= 0);
8630 : : }
8631 : : }
8632 : 196484 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8633 : :
8634 : 196484 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8635 : : }
8636 : :
8637 : 31969 : real_from_target (&r, tmp, mode);
8638 : 31969 : return build_real (type, r);
8639 : : }
8640 : :
8641 : :
8642 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8643 : : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8644 : : If the buffer cannot be interpreted, return NULL_TREE. */
8645 : :
8646 : : static tree
8647 : 1405 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8648 : : {
8649 : 1405 : tree etype, rpart, ipart;
8650 : 1405 : int size;
8651 : :
8652 : 1405 : etype = TREE_TYPE (type);
8653 : 1405 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8654 : 1405 : if (size * 2 > len)
8655 : : return NULL_TREE;
8656 : 1370 : rpart = native_interpret_expr (etype, ptr, size);
8657 : 1370 : if (!rpart)
8658 : : return NULL_TREE;
8659 : 1369 : ipart = native_interpret_expr (etype, ptr+size, size);
8660 : 1369 : if (!ipart)
8661 : : return NULL_TREE;
8662 : 1369 : return build_complex (type, rpart, ipart);
8663 : : }
8664 : :
8665 : : /* Read a vector of type TYPE from the target memory image given by BYTES,
8666 : : which contains LEN bytes. The vector is known to be encodable using
8667 : : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8668 : :
8669 : : Return the vector on success, otherwise return null. */
8670 : :
8671 : : static tree
8672 : 193538 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8673 : : unsigned int len, unsigned int npatterns,
8674 : : unsigned int nelts_per_pattern)
8675 : : {
8676 : 193538 : tree elt_type = TREE_TYPE (type);
8677 : 193538 : if (VECTOR_BOOLEAN_TYPE_P (type)
8678 : 193541 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8679 : : {
8680 : : /* This is the only case in which elements can be smaller than a byte.
8681 : : Element 0 is always in the lsb of the containing byte. */
8682 : 1 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8683 : 1 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8684 : : return NULL_TREE;
8685 : :
8686 : 1 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8687 : 17 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8688 : : {
8689 : 16 : unsigned int bit_index = i * elt_bits;
8690 : 16 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8691 : 16 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8692 : 32 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8693 : 17 : ? build_all_ones_cst (elt_type)
8694 : 1 : : build_zero_cst (elt_type));
8695 : : }
8696 : 1 : return builder.build ();
8697 : 1 : }
8698 : :
8699 : 193537 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8700 : 193537 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8701 : : return NULL_TREE;
8702 : :
8703 : 193537 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8704 : 785280 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8705 : : {
8706 : 591781 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8707 : 591781 : if (!elt)
8708 : 38 : return NULL_TREE;
8709 : 591743 : builder.quick_push (elt);
8710 : 591743 : bytes += elt_bytes;
8711 : : }
8712 : 193499 : return builder.build ();
8713 : 193537 : }
8714 : :
8715 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8716 : : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8717 : : If the buffer cannot be interpreted, return NULL_TREE. */
8718 : :
8719 : : static tree
8720 : 74052 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8721 : : {
8722 : 74052 : unsigned HOST_WIDE_INT size;
8723 : :
8724 : 74052 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8725 : 74052 : || size > len)
8726 : 2 : return NULL_TREE;
8727 : :
8728 : 74050 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8729 : 74050 : return native_interpret_vector_part (type, ptr, len, count, 1);
8730 : : }
8731 : :
8732 : :
8733 : : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8734 : : the buffer PTR of length LEN as a constant of type TYPE. For
8735 : : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8736 : : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8737 : : return NULL_TREE. */
8738 : :
8739 : : tree
8740 : 2956438 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8741 : : {
8742 : 2956438 : switch (TREE_CODE (type))
8743 : : {
8744 : 2799713 : case INTEGER_TYPE:
8745 : 2799713 : case ENUMERAL_TYPE:
8746 : 2799713 : case BOOLEAN_TYPE:
8747 : 2799713 : case POINTER_TYPE:
8748 : 2799713 : case REFERENCE_TYPE:
8749 : 2799713 : case OFFSET_TYPE:
8750 : 2799713 : case BITINT_TYPE:
8751 : 2799713 : return native_interpret_int (type, ptr, len);
8752 : :
8753 : 30812 : case REAL_TYPE:
8754 : 30812 : if (tree ret = native_interpret_real (type, ptr, len))
8755 : : {
8756 : : /* For floating point values in composite modes, punt if this
8757 : : folding doesn't preserve bit representation. As the mode doesn't
8758 : : have fixed precision while GCC pretends it does, there could be
8759 : : valid values that GCC can't really represent accurately.
8760 : : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8761 : : bit combinationations which GCC doesn't preserve. */
8762 : 30751 : unsigned char buf[24 * 2];
8763 : 30751 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8764 : 30751 : int total_bytes = GET_MODE_SIZE (mode);
8765 : 30751 : memcpy (buf + 24, ptr, total_bytes);
8766 : 30751 : clear_type_padding_in_mask (type, buf + 24);
8767 : 30751 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8768 : 30751 : || memcmp (buf + 24, buf, total_bytes) != 0)
8769 : 156 : return NULL_TREE;
8770 : : return ret;
8771 : : }
8772 : : return NULL_TREE;
8773 : :
8774 : 0 : case FIXED_POINT_TYPE:
8775 : 0 : return native_interpret_fixed (type, ptr, len);
8776 : :
8777 : 1405 : case COMPLEX_TYPE:
8778 : 1405 : return native_interpret_complex (type, ptr, len);
8779 : :
8780 : 74052 : case VECTOR_TYPE:
8781 : 74052 : return native_interpret_vector (type, ptr, len);
8782 : :
8783 : : default:
8784 : : return NULL_TREE;
8785 : : }
8786 : : }
8787 : :
8788 : : /* Returns true if we can interpret the contents of a native encoding
8789 : : as TYPE. */
8790 : :
8791 : : bool
8792 : 393146 : can_native_interpret_type_p (tree type)
8793 : : {
8794 : 393146 : switch (TREE_CODE (type))
8795 : : {
8796 : : case INTEGER_TYPE:
8797 : : case ENUMERAL_TYPE:
8798 : : case BOOLEAN_TYPE:
8799 : : case POINTER_TYPE:
8800 : : case REFERENCE_TYPE:
8801 : : case FIXED_POINT_TYPE:
8802 : : case REAL_TYPE:
8803 : : case COMPLEX_TYPE:
8804 : : case VECTOR_TYPE:
8805 : : case OFFSET_TYPE:
8806 : : return true;
8807 : 80016 : default:
8808 : 80016 : return false;
8809 : : }
8810 : : }
8811 : :
8812 : : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8813 : : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8814 : :
8815 : : tree
8816 : 609 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8817 : : int len)
8818 : : {
8819 : 609 : vec<constructor_elt, va_gc> *elts = NULL;
8820 : 609 : if (TREE_CODE (type) == ARRAY_TYPE)
8821 : : {
8822 : 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8823 : 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8824 : : return NULL_TREE;
8825 : :
8826 : 197 : HOST_WIDE_INT cnt = 0;
8827 : 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8828 : : {
8829 : 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8830 : : return NULL_TREE;
8831 : 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8832 : : }
8833 : 197 : if (eltsz == 0)
8834 : 0 : cnt = 0;
8835 : 197 : HOST_WIDE_INT pos = 0;
8836 : 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8837 : : {
8838 : 439 : tree v = NULL_TREE;
8839 : 439 : if (pos >= len || pos + eltsz > len)
8840 : 609 : return NULL_TREE;
8841 : 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8842 : : {
8843 : 367 : v = native_interpret_expr (TREE_TYPE (type),
8844 : 367 : ptr + off + pos, eltsz);
8845 : 367 : if (v == NULL_TREE)
8846 : : return NULL_TREE;
8847 : : }
8848 : 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8849 : 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8850 : 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8851 : : eltsz);
8852 : 72 : if (v == NULL_TREE)
8853 : 0 : return NULL_TREE;
8854 : 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8855 : : }
8856 : 197 : return build_constructor (type, elts);
8857 : : }
8858 : 412 : if (TREE_CODE (type) != RECORD_TYPE)
8859 : : return NULL_TREE;
8860 : 6708 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8861 : : {
8862 : 1144 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8863 : 7440 : || is_empty_type (TREE_TYPE (field)))
8864 : 5242 : continue;
8865 : 1054 : tree fld = field;
8866 : 1054 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8867 : 1054 : int diff = 0;
8868 : 1054 : tree v = NULL_TREE;
8869 : 1054 : if (DECL_BIT_FIELD (field))
8870 : : {
8871 : 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8872 : 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8873 : : {
8874 : 168 : poly_int64 bitoffset;
8875 : 168 : poly_uint64 field_offset, fld_offset;
8876 : 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8877 : 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8878 : 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8879 : : else
8880 : : bitoffset = 0;
8881 : 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8882 : 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8883 : 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8884 : 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8885 : 168 : if (!bitoffset.is_constant (&bitoff)
8886 : 168 : || bitoff < 0
8887 : 168 : || bitoff > diff)
8888 : 0 : return NULL_TREE;
8889 : : }
8890 : : else
8891 : : {
8892 : 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8893 : : return NULL_TREE;
8894 : 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8895 : 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8896 : 12 : bpos %= BITS_PER_UNIT;
8897 : 12 : fieldsize += bpos;
8898 : 12 : fieldsize += BITS_PER_UNIT - 1;
8899 : 12 : fieldsize /= BITS_PER_UNIT;
8900 : 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8901 : 12 : if (repr_type == NULL_TREE)
8902 : : return NULL_TREE;
8903 : 12 : sz = int_size_in_bytes (repr_type);
8904 : 12 : if (sz < 0 || sz > len)
8905 : : return NULL_TREE;
8906 : 12 : pos = int_byte_position (field);
8907 : 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8908 : : return NULL_TREE;
8909 : 12 : HOST_WIDE_INT rpos;
8910 : 12 : if (pos + sz <= len)
8911 : : rpos = pos;
8912 : : else
8913 : : {
8914 : 0 : rpos = len - sz;
8915 : 0 : gcc_assert (rpos <= pos);
8916 : : }
8917 : 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8918 : 12 : pos = rpos;
8919 : 12 : diff = (TYPE_PRECISION (repr_type)
8920 : 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8921 : 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8922 : 12 : if (v == NULL_TREE)
8923 : : return NULL_TREE;
8924 : : fld = NULL_TREE;
8925 : : }
8926 : : }
8927 : :
8928 : 168 : if (fld)
8929 : : {
8930 : 1042 : sz = int_size_in_bytes (TREE_TYPE (fld));
8931 : 1042 : if (sz < 0 || sz > len)
8932 : : return NULL_TREE;
8933 : 1042 : tree byte_pos = byte_position (fld);
8934 : 1042 : if (!tree_fits_shwi_p (byte_pos))
8935 : : return NULL_TREE;
8936 : 1042 : pos = tree_to_shwi (byte_pos);
8937 : 1042 : if (pos < 0 || pos > len || pos + sz > len)
8938 : : return NULL_TREE;
8939 : : }
8940 : 1042 : if (fld == NULL_TREE)
8941 : : /* Already handled above. */;
8942 : 1042 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8943 : : {
8944 : 850 : v = native_interpret_expr (TREE_TYPE (fld),
8945 : 850 : ptr + off + pos, sz);
8946 : 850 : if (v == NULL_TREE)
8947 : : return NULL_TREE;
8948 : : }
8949 : 192 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8950 : 192 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8951 : 192 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8952 : 204 : if (v == NULL_TREE)
8953 : : return NULL_TREE;
8954 : 1054 : if (fld != field)
8955 : : {
8956 : 180 : if (TREE_CODE (v) != INTEGER_CST)
8957 : : return NULL_TREE;
8958 : :
8959 : : /* FIXME: Figure out how to handle PDP endian bitfields. */
8960 : 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8961 : : return NULL_TREE;
8962 : 180 : if (!BYTES_BIG_ENDIAN)
8963 : 180 : v = wide_int_to_tree (TREE_TYPE (field),
8964 : 360 : wi::lrshift (wi::to_wide (v), bitoff));
8965 : : else
8966 : : v = wide_int_to_tree (TREE_TYPE (field),
8967 : : wi::lrshift (wi::to_wide (v),
8968 : : diff - bitoff));
8969 : : }
8970 : 1054 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8971 : : }
8972 : 412 : return build_constructor (type, elts);
8973 : : }
8974 : :
8975 : : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8976 : : or extracted constant positions and/or sizes aren't byte aligned. */
8977 : :
8978 : : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8979 : : bits between adjacent elements. AMNT should be within
8980 : : [0, BITS_PER_UNIT).
8981 : : Example, AMNT = 2:
8982 : : 00011111|11100000 << 2 = 01111111|10000000
8983 : : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8984 : :
8985 : : void
8986 : 38863 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8987 : : unsigned int amnt)
8988 : : {
8989 : 38863 : if (amnt == 0)
8990 : : return;
8991 : :
8992 : 22772 : unsigned char carry_over = 0U;
8993 : 22772 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8994 : 22772 : unsigned char clear_mask = (~0U) << amnt;
8995 : :
8996 : 128980 : for (unsigned int i = 0; i < sz; i++)
8997 : : {
8998 : 106208 : unsigned prev_carry_over = carry_over;
8999 : 106208 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9000 : :
9001 : 106208 : ptr[i] <<= amnt;
9002 : 106208 : if (i != 0)
9003 : : {
9004 : 83436 : ptr[i] &= clear_mask;
9005 : 83436 : ptr[i] |= prev_carry_over;
9006 : : }
9007 : : }
9008 : : }
9009 : :
9010 : : /* Like shift_bytes_in_array_left but for big-endian.
9011 : : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9012 : : bits between adjacent elements. AMNT should be within
9013 : : [0, BITS_PER_UNIT).
9014 : : Example, AMNT = 2:
9015 : : 00011111|11100000 >> 2 = 00000111|11111000
9016 : : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9017 : :
9018 : : void
9019 : 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9020 : : unsigned int amnt)
9021 : : {
9022 : 8 : if (amnt == 0)
9023 : : return;
9024 : :
9025 : 4 : unsigned char carry_over = 0U;
9026 : 4 : unsigned char carry_mask = ~(~0U << amnt);
9027 : :
9028 : 12 : for (unsigned int i = 0; i < sz; i++)
9029 : : {
9030 : 8 : unsigned prev_carry_over = carry_over;
9031 : 8 : carry_over = ptr[i] & carry_mask;
9032 : :
9033 : 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9034 : 8 : ptr[i] >>= amnt;
9035 : 8 : ptr[i] |= prev_carry_over;
9036 : : }
9037 : : }
9038 : :
9039 : : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9040 : : directly on the VECTOR_CST encoding, in a way that works for variable-
9041 : : length vectors. Return the resulting VECTOR_CST on success or null
9042 : : on failure. */
9043 : :
9044 : : static tree
9045 : 126723 : fold_view_convert_vector_encoding (tree type, tree expr)
9046 : : {
9047 : 126723 : tree expr_type = TREE_TYPE (expr);
9048 : 126723 : poly_uint64 type_bits, expr_bits;
9049 : 126723 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9050 : 126723 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9051 : 0 : return NULL_TREE;
9052 : :
9053 : 126723 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9054 : 126723 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9055 : 126723 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9056 : 126723 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9057 : :
9058 : : /* We can only preserve the semantics of a stepped pattern if the new
9059 : : vector element is an integer of the same size. */
9060 : 126723 : if (VECTOR_CST_STEPPED_P (expr)
9061 : 126723 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9062 : : return NULL_TREE;
9063 : :
9064 : : /* The number of bits needed to encode one element from every pattern
9065 : : of the original vector. */
9066 : 119488 : unsigned int expr_sequence_bits
9067 : 119488 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9068 : :
9069 : : /* The number of bits needed to encode one element from every pattern
9070 : : of the result. */
9071 : 119488 : unsigned int type_sequence_bits
9072 : 119488 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9073 : :
9074 : : /* Don't try to read more bytes than are available, which can happen
9075 : : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9076 : : The general VIEW_CONVERT handling can cope with that case, so there's
9077 : : no point complicating things here. */
9078 : 119488 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9079 : 119488 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9080 : : BITS_PER_UNIT);
9081 : 119488 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9082 : 119488 : if (known_gt (buffer_bits, expr_bits))
9083 : : return NULL_TREE;
9084 : :
9085 : : /* Get enough bytes of EXPR to form the new encoding. */
9086 : 119488 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9087 : 119488 : buffer.quick_grow (buffer_bytes);
9088 : 119488 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9089 : 119488 : buffer_bits / expr_elt_bits)
9090 : : != (int) buffer_bytes)
9091 : : return NULL_TREE;
9092 : :
9093 : : /* Reencode the bytes as TYPE. */
9094 : 119488 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9095 : 238976 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9096 : 119488 : type_npatterns, nelts_per_pattern);
9097 : 119488 : }
9098 : :
9099 : : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9100 : : TYPE at compile-time. If we're unable to perform the conversion
9101 : : return NULL_TREE. */
9102 : :
9103 : : static tree
9104 : 11639029 : fold_view_convert_expr (tree type, tree expr)
9105 : : {
9106 : 11639029 : unsigned char buffer[128];
9107 : 11639029 : unsigned char *buf;
9108 : 11639029 : int len;
9109 : 11639029 : HOST_WIDE_INT l;
9110 : :
9111 : : /* Check that the host and target are sane. */
9112 : 11639029 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9113 : : return NULL_TREE;
9114 : :
9115 : 11639029 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9116 : 126723 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9117 : : return res;
9118 : :
9119 : 11519560 : l = int_size_in_bytes (type);
9120 : 11519560 : if (l > (int) sizeof (buffer)
9121 : 11519560 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9122 : : {
9123 : 0 : buf = XALLOCAVEC (unsigned char, l);
9124 : 0 : len = l;
9125 : : }
9126 : : else
9127 : : {
9128 : : buf = buffer;
9129 : : len = sizeof (buffer);
9130 : : }
9131 : 11519560 : len = native_encode_expr (expr, buf, len);
9132 : 11519560 : if (len == 0)
9133 : : return NULL_TREE;
9134 : :
9135 : 1704981 : return native_interpret_expr (type, buf, len);
9136 : : }
9137 : :
9138 : : /* Build an expression for the address of T. Folds away INDIRECT_REF
9139 : : to avoid confusing the gimplify process. */
9140 : :
9141 : : tree
9142 : 449021804 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9143 : : {
9144 : : /* The size of the object is not relevant when talking about its address. */
9145 : 449021804 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9146 : 0 : t = TREE_OPERAND (t, 0);
9147 : :
9148 : 449021804 : if (INDIRECT_REF_P (t))
9149 : : {
9150 : 50940643 : t = TREE_OPERAND (t, 0);
9151 : :
9152 : 50940643 : if (TREE_TYPE (t) != ptrtype)
9153 : 32769772 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9154 : : }
9155 : 398081161 : else if (TREE_CODE (t) == MEM_REF
9156 : 398081161 : && integer_zerop (TREE_OPERAND (t, 1)))
9157 : : {
9158 : 1624740 : t = TREE_OPERAND (t, 0);
9159 : :
9160 : 1624740 : if (TREE_TYPE (t) != ptrtype)
9161 : 1070242 : t = fold_convert_loc (loc, ptrtype, t);
9162 : : }
9163 : 396456421 : else if (TREE_CODE (t) == MEM_REF
9164 : 396456421 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9165 : 661 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9166 : : TREE_OPERAND (t, 0),
9167 : : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9168 : 396455760 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9169 : : {
9170 : 27818221 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9171 : :
9172 : 27818221 : if (TREE_TYPE (t) != ptrtype)
9173 : 13947 : t = fold_convert_loc (loc, ptrtype, t);
9174 : : }
9175 : : else
9176 : 368637539 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9177 : :
9178 : : return t;
9179 : : }
9180 : :
9181 : : /* Build an expression for the address of T. */
9182 : :
9183 : : tree
9184 : 413667901 : build_fold_addr_expr_loc (location_t loc, tree t)
9185 : : {
9186 : 413667901 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9187 : :
9188 : 413667901 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9189 : : }
9190 : :
9191 : : /* Fold a unary expression of code CODE and type TYPE with operand
9192 : : OP0. Return the folded expression if folding is successful.
9193 : : Otherwise, return NULL_TREE. */
9194 : :
9195 : : tree
9196 : 1669856496 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9197 : : {
9198 : 1669856496 : tree tem;
9199 : 1669856496 : tree arg0;
9200 : 1669856496 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9201 : :
9202 : 1669856496 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9203 : : && TREE_CODE_LENGTH (code) == 1);
9204 : :
9205 : 1669856496 : arg0 = op0;
9206 : 1669856496 : if (arg0)
9207 : : {
9208 : 1669843330 : if (CONVERT_EXPR_CODE_P (code)
9209 : : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9210 : : {
9211 : : /* Don't use STRIP_NOPS, because signedness of argument type
9212 : : matters. */
9213 : 833051657 : STRIP_SIGN_NOPS (arg0);
9214 : : }
9215 : : else
9216 : : {
9217 : : /* Strip any conversions that don't change the mode. This
9218 : : is safe for every expression, except for a comparison
9219 : : expression because its signedness is derived from its
9220 : : operands.
9221 : :
9222 : : Note that this is done as an internal manipulation within
9223 : : the constant folder, in order to find the simplest
9224 : : representation of the arguments so that their form can be
9225 : : studied. In any cases, the appropriate type conversions
9226 : : should be put back in the tree that will get out of the
9227 : : constant folder. */
9228 : 836791673 : STRIP_NOPS (arg0);
9229 : : }
9230 : :
9231 : 1669843330 : if (CONSTANT_CLASS_P (arg0))
9232 : : {
9233 : 244441203 : tree tem = const_unop (code, type, arg0);
9234 : 244441203 : if (tem)
9235 : : {
9236 : 207669597 : if (TREE_TYPE (tem) != type)
9237 : 69436 : tem = fold_convert_loc (loc, type, tem);
9238 : 207669597 : return tem;
9239 : : }
9240 : : }
9241 : : }
9242 : :
9243 : 1462186899 : tem = generic_simplify (loc, code, type, op0);
9244 : 1462186899 : if (tem)
9245 : : return tem;
9246 : :
9247 : 1104486175 : if (TREE_CODE_CLASS (code) == tcc_unary)
9248 : : {
9249 : 563787543 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9250 : 1042339 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9251 : : fold_build1_loc (loc, code, type,
9252 : 1042339 : fold_convert_loc (loc, TREE_TYPE (op0),
9253 : 2084678 : TREE_OPERAND (arg0, 1))));
9254 : 562745204 : else if (TREE_CODE (arg0) == COND_EXPR)
9255 : : {
9256 : 446742 : tree arg01 = TREE_OPERAND (arg0, 1);
9257 : 446742 : tree arg02 = TREE_OPERAND (arg0, 2);
9258 : 446742 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9259 : 442560 : arg01 = fold_build1_loc (loc, code, type,
9260 : : fold_convert_loc (loc,
9261 : 442560 : TREE_TYPE (op0), arg01));
9262 : 446742 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9263 : 443181 : arg02 = fold_build1_loc (loc, code, type,
9264 : : fold_convert_loc (loc,
9265 : 443181 : TREE_TYPE (op0), arg02));
9266 : 446742 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9267 : : arg01, arg02);
9268 : :
9269 : : /* If this was a conversion, and all we did was to move into
9270 : : inside the COND_EXPR, bring it back out. But leave it if
9271 : : it is a conversion from integer to integer and the
9272 : : result precision is no wider than a word since such a
9273 : : conversion is cheap and may be optimized away by combine,
9274 : : while it couldn't if it were outside the COND_EXPR. Then return
9275 : : so we don't get into an infinite recursion loop taking the
9276 : : conversion out and then back in. */
9277 : :
9278 : 446742 : if ((CONVERT_EXPR_CODE_P (code)
9279 : 10466 : || code == NON_LVALUE_EXPR)
9280 : 436295 : && TREE_CODE (tem) == COND_EXPR
9281 : 418022 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9282 : 382043 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9283 : 192286 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9284 : 192074 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9285 : 192074 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9286 : 192074 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9287 : 645243 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9288 : 7277 : && (INTEGRAL_TYPE_P
9289 : : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9290 : 7237 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9291 : 7124 : || flag_syntax_only))
9292 : 184140 : tem = build1_loc (loc, code, type,
9293 : : build3 (COND_EXPR,
9294 : 184140 : TREE_TYPE (TREE_OPERAND
9295 : : (TREE_OPERAND (tem, 1), 0)),
9296 : 184140 : TREE_OPERAND (tem, 0),
9297 : 184140 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9298 : 184140 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9299 : : 0)));
9300 : 446742 : return tem;
9301 : : }
9302 : : }
9303 : :
9304 : 1102997094 : switch (code)
9305 : : {
9306 : 40969519 : case NON_LVALUE_EXPR:
9307 : 40969519 : if (!maybe_lvalue_p (op0))
9308 : 30201381 : return fold_convert_loc (loc, type, op0);
9309 : : return NULL_TREE;
9310 : :
9311 : 511929889 : CASE_CONVERT:
9312 : 511929889 : case FLOAT_EXPR:
9313 : 511929889 : case FIX_TRUNC_EXPR:
9314 : 511929889 : if (COMPARISON_CLASS_P (op0))
9315 : : {
9316 : : /* If we have (type) (a CMP b) and type is an integral type, return
9317 : : new expression involving the new type. Canonicalize
9318 : : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9319 : : non-integral type.
9320 : : Do not fold the result as that would not simplify further, also
9321 : : folding again results in recursions. */
9322 : 407406 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9323 : 87488 : return build2_loc (loc, TREE_CODE (op0), type,
9324 : 87488 : TREE_OPERAND (op0, 0),
9325 : 174976 : TREE_OPERAND (op0, 1));
9326 : 319918 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9327 : 7594 : && TREE_CODE (type) != VECTOR_TYPE)
9328 : 7594 : return build3_loc (loc, COND_EXPR, type, op0,
9329 : : constant_boolean_node (true, type),
9330 : 7594 : constant_boolean_node (false, type));
9331 : : }
9332 : :
9333 : : /* Handle (T *)&A.B.C for A being of type T and B and C
9334 : : living at offset zero. This occurs frequently in
9335 : : C++ upcasting and then accessing the base. */
9336 : 511834807 : if (TREE_CODE (op0) == ADDR_EXPR
9337 : 117875422 : && POINTER_TYPE_P (type)
9338 : 623099848 : && handled_component_p (TREE_OPERAND (op0, 0)))
9339 : : {
9340 : 20916716 : poly_int64 bitsize, bitpos;
9341 : 20916716 : tree offset;
9342 : 20916716 : machine_mode mode;
9343 : 20916716 : int unsignedp, reversep, volatilep;
9344 : 20916716 : tree base
9345 : 20916716 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9346 : : &offset, &mode, &unsignedp, &reversep,
9347 : : &volatilep);
9348 : : /* If the reference was to a (constant) zero offset, we can use
9349 : : the address of the base if it has the same base type
9350 : : as the result type and the pointer type is unqualified. */
9351 : 20916716 : if (!offset
9352 : 20802006 : && known_eq (bitpos, 0)
9353 : 14068655 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9354 : 14068655 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9355 : 20927951 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9356 : 11034 : return fold_convert_loc (loc, type,
9357 : 11034 : build_fold_addr_expr_loc (loc, base));
9358 : : }
9359 : :
9360 : 511823773 : if (TREE_CODE (op0) == MODIFY_EXPR
9361 : 264837 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9362 : : /* Detect assigning a bitfield. */
9363 : 511825777 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9364 : 109 : && DECL_BIT_FIELD
9365 : : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9366 : : {
9367 : : /* Don't leave an assignment inside a conversion
9368 : : unless assigning a bitfield. */
9369 : 1956 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9370 : : /* First do the assignment, then return converted constant. */
9371 : 1956 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9372 : 1956 : suppress_warning (tem /* What warning? */);
9373 : 1956 : TREE_USED (tem) = 1;
9374 : 1956 : return tem;
9375 : : }
9376 : :
9377 : : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9378 : : constants (if x has signed type, the sign bit cannot be set
9379 : : in c). This folds extension into the BIT_AND_EXPR.
9380 : : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9381 : : very likely don't have maximal range for their precision and this
9382 : : transformation effectively doesn't preserve non-maximal ranges. */
9383 : 511821817 : if (TREE_CODE (type) == INTEGER_TYPE
9384 : 227039143 : && TREE_CODE (op0) == BIT_AND_EXPR
9385 : 512326430 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9386 : : {
9387 : 237273 : tree and_expr = op0;
9388 : 237273 : tree and0 = TREE_OPERAND (and_expr, 0);
9389 : 237273 : tree and1 = TREE_OPERAND (and_expr, 1);
9390 : 237273 : int change = 0;
9391 : :
9392 : 237273 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9393 : 237273 : || (TYPE_PRECISION (type)
9394 : 81591 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9395 : : change = 1;
9396 : 23987 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9397 : : <= HOST_BITS_PER_WIDE_INT
9398 : 23987 : && tree_fits_uhwi_p (and1))
9399 : : {
9400 : 22881 : unsigned HOST_WIDE_INT cst;
9401 : :
9402 : 22881 : cst = tree_to_uhwi (and1);
9403 : 45762 : cst &= HOST_WIDE_INT_M1U
9404 : 22881 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9405 : 22881 : change = (cst == 0);
9406 : 22881 : if (change
9407 : 22881 : && !flag_syntax_only
9408 : 45762 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9409 : : == ZERO_EXTEND))
9410 : : {
9411 : : tree uns = unsigned_type_for (TREE_TYPE (and0));
9412 : : and0 = fold_convert_loc (loc, uns, and0);
9413 : : and1 = fold_convert_loc (loc, uns, and1);
9414 : : }
9415 : : }
9416 : 22881 : if (change)
9417 : : {
9418 : 236167 : tree and1_type = TREE_TYPE (and1);
9419 : 236167 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9420 : : TYPE_PRECISION (type));
9421 : 236167 : tem = force_fit_type (type,
9422 : 236167 : wide_int::from (wi::to_wide (and1), prec,
9423 : 236167 : TYPE_SIGN (and1_type)),
9424 : 236167 : 0, TREE_OVERFLOW (and1));
9425 : 236167 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9426 : 236167 : fold_convert_loc (loc, type, and0), tem);
9427 : : }
9428 : : }
9429 : :
9430 : : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9431 : : cast (T1)X will fold away. We assume that this happens when X itself
9432 : : is a cast. */
9433 : 511585650 : if (POINTER_TYPE_P (type)
9434 : 248078708 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9435 : 516972306 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9436 : : {
9437 : 3031160 : tree arg00 = TREE_OPERAND (arg0, 0);
9438 : 3031160 : tree arg01 = TREE_OPERAND (arg0, 1);
9439 : :
9440 : : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9441 : : when the pointed type needs higher alignment than
9442 : : the p+ first operand's pointed type. */
9443 : 3031160 : if (!in_gimple_form
9444 : 3017394 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9445 : 3032374 : && (min_align_of_type (TREE_TYPE (type))
9446 : 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9447 : : return NULL_TREE;
9448 : :
9449 : : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9450 : : when type is a reference type and arg00's type is not,
9451 : : because arg00 could be validly nullptr and if arg01 doesn't return,
9452 : : we don't want false positive binding of reference to nullptr. */
9453 : 3031093 : if (TREE_CODE (type) == REFERENCE_TYPE
9454 : 2036900 : && !in_gimple_form
9455 : 2036877 : && sanitize_flags_p (SANITIZE_NULL)
9456 : 3031524 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9457 : : return NULL_TREE;
9458 : :
9459 : 3030662 : arg00 = fold_convert_loc (loc, type, arg00);
9460 : 3030662 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9461 : : }
9462 : :
9463 : : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9464 : : of the same precision, and X is an integer type not narrower than
9465 : : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9466 : 508554490 : if (INTEGRAL_TYPE_P (type)
9467 : 232932982 : && TREE_CODE (op0) == BIT_NOT_EXPR
9468 : 534339 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9469 : 534339 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9470 : 508882606 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9471 : : {
9472 : 326402 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9473 : 395948 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9474 : 395946 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9475 : 264835 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9476 : 264835 : fold_convert_loc (loc, type, tem));
9477 : : }
9478 : :
9479 : : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9480 : : type of X and Y (integer types only). */
9481 : 508289655 : if (INTEGRAL_TYPE_P (type)
9482 : 232668147 : && TREE_CODE (op0) == MULT_EXPR
9483 : 8141152 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9484 : 8119605 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9485 : 508350460 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9486 : 14131 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9487 : : {
9488 : : /* Be careful not to introduce new overflows. */
9489 : 60765 : tree mult_type;
9490 : 60765 : if (TYPE_OVERFLOW_WRAPS (type))
9491 : : mult_type = type;
9492 : : else
9493 : 2040 : mult_type = unsigned_type_for (type);
9494 : :
9495 : 60765 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9496 : : {
9497 : 121530 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9498 : : fold_convert_loc (loc, mult_type,
9499 : 60765 : TREE_OPERAND (op0, 0)),
9500 : : fold_convert_loc (loc, mult_type,
9501 : 60765 : TREE_OPERAND (op0, 1)));
9502 : 60765 : return fold_convert_loc (loc, type, tem);
9503 : : }
9504 : : }
9505 : :
9506 : : return NULL_TREE;
9507 : :
9508 : 229588174 : case VIEW_CONVERT_EXPR:
9509 : 229588174 : if (TREE_CODE (op0) == MEM_REF)
9510 : : {
9511 : 2584 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9512 : 5 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9513 : 2584 : tem = fold_build2_loc (loc, MEM_REF, type,
9514 : 2584 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9515 : 2584 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9516 : 2584 : return tem;
9517 : : }
9518 : :
9519 : : return NULL_TREE;
9520 : :
9521 : 3845010 : case NEGATE_EXPR:
9522 : 3845010 : tem = fold_negate_expr (loc, arg0);
9523 : 3845010 : if (tem)
9524 : 1552 : return fold_convert_loc (loc, type, tem);
9525 : : return NULL_TREE;
9526 : :
9527 : 2789906 : case ABS_EXPR:
9528 : : /* Convert fabs((double)float) into (double)fabsf(float). */
9529 : 2789906 : if (TREE_CODE (arg0) == NOP_EXPR
9530 : 23915 : && TREE_CODE (type) == REAL_TYPE)
9531 : : {
9532 : 23859 : tree targ0 = strip_float_extensions (arg0);
9533 : 23859 : if (targ0 != arg0)
9534 : 23655 : return fold_convert_loc (loc, type,
9535 : : fold_build1_loc (loc, ABS_EXPR,
9536 : 23655 : TREE_TYPE (targ0),
9537 : 23655 : targ0));
9538 : : }
9539 : : return NULL_TREE;
9540 : :
9541 : 2758417 : case BIT_NOT_EXPR:
9542 : : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9543 : 2758417 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9544 : 2760102 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9545 : : fold_convert_loc (loc, type,
9546 : 1685 : TREE_OPERAND (arg0, 0)))))
9547 : 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9548 : : fold_convert_loc (loc, type,
9549 : 28 : TREE_OPERAND (arg0, 1)));
9550 : 2758403 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9551 : 2760074 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9552 : : fold_convert_loc (loc, type,
9553 : 1671 : TREE_OPERAND (arg0, 1)))))
9554 : 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9555 : : fold_convert_loc (loc, type,
9556 : 46 : TREE_OPERAND (arg0, 0)), tem);
9557 : :
9558 : : return NULL_TREE;
9559 : :
9560 : 48514856 : case TRUTH_NOT_EXPR:
9561 : : /* Note that the operand of this must be an int
9562 : : and its values must be 0 or 1.
9563 : : ("true" is a fixed value perhaps depending on the language,
9564 : : but we don't handle values other than 1 correctly yet.) */
9565 : 48514856 : tem = fold_truth_not_expr (loc, arg0);
9566 : 48514856 : if (!tem)
9567 : : return NULL_TREE;
9568 : 33690277 : return fold_convert_loc (loc, type, tem);
9569 : :
9570 : 61242257 : case INDIRECT_REF:
9571 : : /* Fold *&X to X if X is an lvalue. */
9572 : 61242257 : if (TREE_CODE (op0) == ADDR_EXPR)
9573 : : {
9574 : 6639 : tree op00 = TREE_OPERAND (op0, 0);
9575 : 6639 : if ((VAR_P (op00)
9576 : : || TREE_CODE (op00) == PARM_DECL
9577 : : || TREE_CODE (op00) == RESULT_DECL)
9578 : 5473 : && !TREE_READONLY (op00))
9579 : : return op00;
9580 : : }
9581 : : return NULL_TREE;
9582 : :
9583 : : default:
9584 : : return NULL_TREE;
9585 : : } /* switch (code) */
9586 : : }
9587 : :
9588 : :
9589 : : /* If the operation was a conversion do _not_ mark a resulting constant
9590 : : with TREE_OVERFLOW if the original constant was not. These conversions
9591 : : have implementation defined behavior and retaining the TREE_OVERFLOW
9592 : : flag here would confuse later passes such as VRP. */
9593 : : tree
9594 : 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9595 : : tree type, tree op0)
9596 : : {
9597 : 0 : tree res = fold_unary_loc (loc, code, type, op0);
9598 : 0 : if (res
9599 : 0 : && TREE_CODE (res) == INTEGER_CST
9600 : 0 : && TREE_CODE (op0) == INTEGER_CST
9601 : 0 : && CONVERT_EXPR_CODE_P (code))
9602 : 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9603 : :
9604 : 0 : return res;
9605 : : }
9606 : :
9607 : : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9608 : : operands OP0 and OP1. LOC is the location of the resulting expression.
9609 : : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9610 : : Return the folded expression if folding is successful. Otherwise,
9611 : : return NULL_TREE. */
9612 : : static tree
9613 : 24326193 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9614 : : tree arg0, tree arg1, tree op0, tree op1)
9615 : : {
9616 : 24326193 : tree tem;
9617 : :
9618 : : /* We only do these simplifications if we are optimizing. */
9619 : 24326193 : if (!optimize)
9620 : : return NULL_TREE;
9621 : :
9622 : : /* Check for things like (A || B) && (A || C). We can convert this
9623 : : to A || (B && C). Note that either operator can be any of the four
9624 : : truth and/or operations and the transformation will still be
9625 : : valid. Also note that we only care about order for the
9626 : : ANDIF and ORIF operators. If B contains side effects, this
9627 : : might change the truth-value of A. */
9628 : 24041305 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9629 : 5396064 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9630 : : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9631 : : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9632 : 5396064 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9633 : 24072498 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9634 : : {
9635 : 30666 : tree a00 = TREE_OPERAND (arg0, 0);
9636 : 30666 : tree a01 = TREE_OPERAND (arg0, 1);
9637 : 30666 : tree a10 = TREE_OPERAND (arg1, 0);
9638 : 30666 : tree a11 = TREE_OPERAND (arg1, 1);
9639 : 61332 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9640 : 30666 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9641 : 30666 : && (code == TRUTH_AND_EXPR
9642 : 9651 : || code == TRUTH_OR_EXPR));
9643 : :
9644 : 30666 : if (operand_equal_p (a00, a10, 0))
9645 : 849 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9646 : 849 : fold_build2_loc (loc, code, type, a01, a11));
9647 : 29817 : else if (commutative && operand_equal_p (a00, a11, 0))
9648 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9649 : 0 : fold_build2_loc (loc, code, type, a01, a10));
9650 : 29817 : else if (commutative && operand_equal_p (a01, a10, 0))
9651 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9652 : 0 : fold_build2_loc (loc, code, type, a00, a11));
9653 : :
9654 : : /* This case if tricky because we must either have commutative
9655 : : operators or else A10 must not have side-effects. */
9656 : :
9657 : 29775 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9658 : 59040 : && operand_equal_p (a01, a11, 0))
9659 : 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9660 : : fold_build2_loc (loc, code, type, a00, a10),
9661 : 43 : a01);
9662 : : }
9663 : :
9664 : : /* See if we can build a range comparison. */
9665 : 24040413 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9666 : : return tem;
9667 : :
9668 : 22961126 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9669 : 22959120 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9670 : : {
9671 : 22092 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9672 : 22092 : if (tem)
9673 : 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9674 : : }
9675 : :
9676 : 22961113 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9677 : 22949053 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9678 : : {
9679 : 120160 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9680 : 120160 : if (tem)
9681 : 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9682 : : }
9683 : :
9684 : : /* Check for the possibility of merging component references. If our
9685 : : lhs is another similar operation, try to merge its rhs with our
9686 : : rhs. Then try to merge our lhs and rhs. */
9687 : 22961022 : if (TREE_CODE (arg0) == code
9688 : 23706686 : && (tem = fold_truth_andor_1 (loc, code, type,
9689 : 745664 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9690 : 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9691 : :
9692 : 22960937 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9693 : : return tem;
9694 : :
9695 : 22920276 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9696 : 22920276 : if (param_logical_op_non_short_circuit != -1)
9697 : 7698 : logical_op_non_short_circuit
9698 : 7698 : = param_logical_op_non_short_circuit;
9699 : 22920276 : if (logical_op_non_short_circuit
9700 : 22916413 : && !sanitize_coverage_p ()
9701 : 22920276 : && (code == TRUTH_AND_EXPR
9702 : 22916410 : || code == TRUTH_ANDIF_EXPR
9703 : 10870893 : || code == TRUTH_OR_EXPR
9704 : 10870893 : || code == TRUTH_ORIF_EXPR))
9705 : : {
9706 : 22916410 : enum tree_code ncode, icode;
9707 : :
9708 : 22916410 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9709 : 22916410 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9710 : 12045517 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9711 : :
9712 : : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9713 : : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9714 : : We don't want to pack more than two leafs to a non-IF AND/OR
9715 : : expression.
9716 : : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9717 : : equal to IF-CODE, then we don't want to add right-hand operand.
9718 : : If the inner right-hand side of left-hand operand has
9719 : : side-effects, or isn't simple, then we can't add to it,
9720 : : as otherwise we might destroy if-sequence. */
9721 : 22916410 : if (TREE_CODE (arg0) == icode
9722 : 737132 : && simple_condition_p (arg1)
9723 : : /* Needed for sequence points to handle trappings, and
9724 : : side-effects. */
9725 : 22965418 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9726 : : {
9727 : 42134 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9728 : : arg1);
9729 : 42134 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9730 : 42134 : tem);
9731 : : }
9732 : : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9733 : : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9734 : 22874276 : else if (TREE_CODE (arg1) == icode
9735 : 2990 : && simple_condition_p (arg0)
9736 : : /* Needed for sequence points to handle trappings, and
9737 : : side-effects. */
9738 : 22875229 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9739 : : {
9740 : 36 : tem = fold_build2_loc (loc, ncode, type,
9741 : 36 : arg0, TREE_OPERAND (arg1, 0));
9742 : 36 : return fold_build2_loc (loc, icode, type, tem,
9743 : 72 : TREE_OPERAND (arg1, 1));
9744 : : }
9745 : : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9746 : : into (A OR B).
9747 : : For sequence point consistancy, we need to check for trapping,
9748 : : and side-effects. */
9749 : 4603356 : else if (code == icode && simple_condition_p (arg0)
9750 : 23596578 : && simple_condition_p (arg1))
9751 : 381443 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9752 : : }
9753 : :
9754 : : return NULL_TREE;
9755 : : }
9756 : :
9757 : : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9758 : : by changing CODE to reduce the magnitude of constants involved in
9759 : : ARG0 of the comparison.
9760 : : Returns a canonicalized comparison tree if a simplification was
9761 : : possible, otherwise returns NULL_TREE.
9762 : : Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9763 : : valid if signed overflow is undefined. */
9764 : :
9765 : : static tree
9766 : 170448186 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9767 : : tree arg0, tree arg1,
9768 : : bool *strict_overflow_p)
9769 : : {
9770 : 170448186 : enum tree_code code0 = TREE_CODE (arg0);
9771 : 170448186 : tree t, cst0 = NULL_TREE;
9772 : 170448186 : int sgn0;
9773 : :
9774 : : /* Match A +- CST code arg1. We can change this only if overflow
9775 : : is undefined. */
9776 : 170448186 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9777 : 129689979 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9778 : : /* In principle pointers also have undefined overflow behavior,
9779 : : but that causes problems elsewhere. */
9780 : 63124041 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9781 : 63124041 : && (code0 == MINUS_EXPR
9782 : 63124041 : || code0 == PLUS_EXPR)
9783 : 2552664 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9784 : : return NULL_TREE;
9785 : :
9786 : : /* Identify the constant in arg0 and its sign. */
9787 : 2093393 : cst0 = TREE_OPERAND (arg0, 1);
9788 : 2093393 : sgn0 = tree_int_cst_sgn (cst0);
9789 : :
9790 : : /* Overflowed constants and zero will cause problems. */
9791 : 2093393 : if (integer_zerop (cst0)
9792 : 2093393 : || TREE_OVERFLOW (cst0))
9793 : : return NULL_TREE;
9794 : :
9795 : : /* See if we can reduce the magnitude of the constant in
9796 : : arg0 by changing the comparison code. */
9797 : : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9798 : 2093393 : if (code == LT_EXPR
9799 : 1221183 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9800 : : code = LE_EXPR;
9801 : : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9802 : 1913605 : else if (code == GT_EXPR
9803 : 565523 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9804 : : code = GE_EXPR;
9805 : : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9806 : 1736519 : else if (code == LE_EXPR
9807 : 642032 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9808 : : code = LT_EXPR;
9809 : : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9810 : 1515945 : else if (code == GE_EXPR
9811 : 469465 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9812 : : code = GT_EXPR;
9813 : : else
9814 : : return NULL_TREE;
9815 : 779391 : *strict_overflow_p = true;
9816 : :
9817 : : /* Now build the constant reduced in magnitude. But not if that
9818 : : would produce one outside of its types range. */
9819 : 1558782 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9820 : 1558782 : && ((sgn0 == 1
9821 : 397522 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9822 : 397522 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9823 : 779391 : || (sgn0 == -1
9824 : 381869 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9825 : 381869 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9826 : 0 : return NULL_TREE;
9827 : :
9828 : 1176913 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9829 : 779391 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9830 : 779391 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9831 : 779391 : t = fold_convert (TREE_TYPE (arg1), t);
9832 : :
9833 : 779391 : return fold_build2_loc (loc, code, type, t, arg1);
9834 : : }
9835 : :
9836 : : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9837 : : overflow further. Try to decrease the magnitude of constants involved
9838 : : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9839 : : and put sole constants at the second argument position.
9840 : : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9841 : :
9842 : : static tree
9843 : 85593839 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9844 : : tree arg0, tree arg1)
9845 : : {
9846 : 85593839 : tree t;
9847 : 85593839 : bool strict_overflow_p;
9848 : 85593839 : const char * const warnmsg = G_("assuming signed overflow does not occur "
9849 : : "when reducing constant in comparison");
9850 : :
9851 : : /* Try canonicalization by simplifying arg0. */
9852 : 85593839 : strict_overflow_p = false;
9853 : 85593839 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9854 : : &strict_overflow_p);
9855 : 85593839 : if (t)
9856 : : {
9857 : 739492 : if (strict_overflow_p)
9858 : 739492 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9859 : 739492 : return t;
9860 : : }
9861 : :
9862 : : /* Try canonicalization by simplifying arg1 using the swapped
9863 : : comparison. */
9864 : 84854347 : code = swap_tree_comparison (code);
9865 : 84854347 : strict_overflow_p = false;
9866 : 84854347 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9867 : : &strict_overflow_p);
9868 : 84854347 : if (t && strict_overflow_p)
9869 : 39899 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9870 : : return t;
9871 : : }
9872 : :
9873 : : /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9874 : : space. This is used to avoid issuing overflow warnings for
9875 : : expressions like &p->x which cannot wrap. */
9876 : :
9877 : : static bool
9878 : 19250 : pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9879 : : {
9880 : 19250 : if (!POINTER_TYPE_P (TREE_TYPE (base)))
9881 : : return true;
9882 : :
9883 : 10668 : if (maybe_lt (bitpos, 0))
9884 : : return true;
9885 : :
9886 : : poly_wide_int wi_offset;
9887 : 9594 : int precision = TYPE_PRECISION (TREE_TYPE (base));
9888 : 9594 : if (offset == NULL_TREE)
9889 : 4690 : wi_offset = wi::zero (precision);
9890 : 4904 : else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
9891 : : return true;
9892 : : else
9893 : 0 : wi_offset = wi::to_poly_wide (offset);
9894 : :
9895 : 4690 : wi::overflow_type overflow;
9896 : 4690 : poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9897 : 4690 : precision);
9898 : 4690 : poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9899 : 4690 : if (overflow)
9900 : : return true;
9901 : :
9902 : 4690 : poly_uint64 total_hwi, size;
9903 : 4690 : if (!total.to_uhwi (&total_hwi)
9904 : 4690 : || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9905 : : &size)
9906 : 9286 : || known_eq (size, 0U))
9907 : 94 : return true;
9908 : :
9909 : 4596 : if (known_le (total_hwi, size))
9910 : : return false;
9911 : :
9912 : : /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9913 : : array. */
9914 : 1153 : if (TREE_CODE (base) == ADDR_EXPR
9915 : 0 : && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
9916 : : &size)
9917 : 0 : && maybe_ne (size, 0U)
9918 : 1153 : && known_le (total_hwi, size))
9919 : : return false;
9920 : :
9921 : : return true;
9922 : 9594 : }
9923 : :
9924 : : /* Return a positive integer when the symbol DECL is known to have
9925 : : a nonzero address, zero when it's known not to (e.g., it's a weak
9926 : : symbol), and a negative integer when the symbol is not yet in the
9927 : : symbol table and so whether or not its address is zero is unknown.
9928 : : For function local objects always return positive integer. */
9929 : : static int
9930 : 12022875 : maybe_nonzero_address (tree decl)
9931 : : {
9932 : 12022875 : if (!DECL_P (decl))
9933 : : return -1;
9934 : :
9935 : : /* Normally, don't do anything for variables and functions before symtab is
9936 : : built; it is quite possible that DECL will be declared weak later.
9937 : : But if folding_initializer, we need a constant answer now, so create
9938 : : the symtab entry and prevent later weak declaration. */
9939 : 9572454 : if (decl_in_symtab_p (decl))
9940 : : {
9941 : 4134050 : if (struct symtab_node *symbol
9942 : 4134050 : = (folding_initializer
9943 : 4134050 : ? symtab_node::get_create (decl)
9944 : 4117515 : : symtab_node::get (decl)))
9945 : 4115231 : return symbol->nonzero_address ();
9946 : : }
9947 : 5438404 : else if (folding_cxx_constexpr)
9948 : : /* Anything that doesn't go in the symtab has non-zero address. */
9949 : : return 1;
9950 : :
9951 : : /* Function local objects are never NULL. */
9952 : 5394848 : if (DECL_CONTEXT (decl)
9953 : 5378709 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9954 : 10770226 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9955 : : return 1;
9956 : :
9957 : : return -1;
9958 : : }
9959 : :
9960 : : /* Subroutine of fold_binary. This routine performs all of the
9961 : : transformations that are common to the equality/inequality
9962 : : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9963 : : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9964 : : fold_binary should call fold_binary. Fold a comparison with
9965 : : tree code CODE and type TYPE with operands OP0 and OP1. Return
9966 : : the folded comparison or NULL_TREE. */
9967 : :
9968 : : static tree
9969 : 85668077 : fold_comparison (location_t loc, enum tree_code code, tree type,
9970 : : tree op0, tree op1)
9971 : : {
9972 : 85668077 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9973 : 85668077 : tree arg0, arg1, tem;
9974 : :
9975 : 85668077 : arg0 = op0;
9976 : 85668077 : arg1 = op1;
9977 : :
9978 : 85668077 : STRIP_SIGN_NOPS (arg0);
9979 : 85668077 : STRIP_SIGN_NOPS (arg1);
9980 : :
9981 : : /* For comparisons of pointers we can decompose it to a compile time
9982 : : comparison of the base objects and the offsets into the object.
9983 : : This requires at least one operand being an ADDR_EXPR or a
9984 : : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9985 : 158755199 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9986 : 85908130 : && (TREE_CODE (arg0) == ADDR_EXPR
9987 : 12772141 : || TREE_CODE (arg1) == ADDR_EXPR
9988 : 11217928 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9989 : 10512938 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9990 : : {
9991 : 2317871 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9992 : 2317871 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9993 : 2317871 : machine_mode mode;
9994 : 2317871 : int volatilep, reversep, unsignedp;
9995 : 2317871 : bool indirect_base0 = false, indirect_base1 = false;
9996 : :
9997 : : /* Get base and offset for the access. Strip ADDR_EXPR for
9998 : : get_inner_reference, but put it back by stripping INDIRECT_REF
9999 : : off the base object if possible. indirect_baseN will be true
10000 : : if baseN is not an address but refers to the object itself. */
10001 : 2317871 : base0 = arg0;
10002 : 2317871 : if (TREE_CODE (arg0) == ADDR_EXPR)
10003 : : {
10004 : 48867 : base0
10005 : 48867 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10006 : : &bitsize, &bitpos0, &offset0, &mode,
10007 : : &unsignedp, &reversep, &volatilep);
10008 : 48867 : if (INDIRECT_REF_P (base0))
10009 : 1850 : base0 = TREE_OPERAND (base0, 0);
10010 : : else
10011 : : indirect_base0 = true;
10012 : : }
10013 : 2269004 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10014 : : {
10015 : 775221 : base0 = TREE_OPERAND (arg0, 0);
10016 : 775221 : STRIP_SIGN_NOPS (base0);
10017 : 775221 : if (TREE_CODE (base0) == ADDR_EXPR)
10018 : : {
10019 : 46646 : base0
10020 : 46646 : = get_inner_reference (TREE_OPERAND (base0, 0),
10021 : : &bitsize, &bitpos0, &offset0, &mode,
10022 : : &unsignedp, &reversep, &volatilep);
10023 : 46646 : if (INDIRECT_REF_P (base0))
10024 : 14 : base0 = TREE_OPERAND (base0, 0);
10025 : : else
10026 : : indirect_base0 = true;
10027 : : }
10028 : 775221 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10029 : 775221 : offset0 = TREE_OPERAND (arg0, 1);
10030 : : else
10031 : 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10032 : : TREE_OPERAND (arg0, 1));
10033 : 775221 : if (poly_int_tree_p (offset0))
10034 : : {
10035 : 650163 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10036 : 650163 : TYPE_PRECISION (sizetype));
10037 : 650163 : tem <<= LOG2_BITS_PER_UNIT;
10038 : 650163 : tem += bitpos0;
10039 : 650163 : if (tem.to_shwi (&bitpos0))
10040 : 650160 : offset0 = NULL_TREE;
10041 : : }
10042 : : }
10043 : :
10044 : 2317871 : base1 = arg1;
10045 : 2317871 : if (TREE_CODE (arg1) == ADDR_EXPR)
10046 : : {
10047 : 1580688 : base1
10048 : 1580688 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10049 : : &bitsize, &bitpos1, &offset1, &mode,
10050 : : &unsignedp, &reversep, &volatilep);
10051 : 1580688 : if (INDIRECT_REF_P (base1))
10052 : 74227 : base1 = TREE_OPERAND (base1, 0);
10053 : : else
10054 : : indirect_base1 = true;
10055 : : }
10056 : 737183 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10057 : : {
10058 : 78610 : base1 = TREE_OPERAND (arg1, 0);
10059 : 78610 : STRIP_SIGN_NOPS (base1);
10060 : 78610 : if (TREE_CODE (base1) == ADDR_EXPR)
10061 : : {
10062 : 11372 : base1
10063 : 11372 : = get_inner_reference (TREE_OPERAND (base1, 0),
10064 : : &bitsize, &bitpos1, &offset1, &mode,
10065 : : &unsignedp, &reversep, &volatilep);
10066 : 11372 : if (INDIRECT_REF_P (base1))
10067 : 0 : base1 = TREE_OPERAND (base1, 0);
10068 : : else
10069 : : indirect_base1 = true;
10070 : : }
10071 : 78610 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10072 : 78586 : offset1 = TREE_OPERAND (arg1, 1);
10073 : : else
10074 : 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10075 : : TREE_OPERAND (arg1, 1));
10076 : 78610 : if (poly_int_tree_p (offset1))
10077 : : {
10078 : 66504 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10079 : 66504 : TYPE_PRECISION (sizetype));
10080 : 66504 : tem <<= LOG2_BITS_PER_UNIT;
10081 : 66504 : tem += bitpos1;
10082 : 66504 : if (tem.to_shwi (&bitpos1))
10083 : 66504 : offset1 = NULL_TREE;
10084 : : }
10085 : : }
10086 : :
10087 : : /* If we have equivalent bases we might be able to simplify. */
10088 : 2317871 : if (indirect_base0 == indirect_base1
10089 : 3062565 : && operand_equal_p (base0, base1,
10090 : : indirect_base0 ? OEP_ADDRESS_OF : 0))
10091 : : {
10092 : : /* We can fold this expression to a constant if the non-constant
10093 : : offset parts are equal. */
10094 : 23306 : if ((offset0 == offset1
10095 : 6972 : || (offset0 && offset1
10096 : 2557 : && operand_equal_p (offset0, offset1, 0)))
10097 : 23306 : && (equality_code
10098 : 12180 : || (indirect_base0
10099 : 8317 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10100 : 3863 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10101 : : {
10102 : 16296 : if (!equality_code
10103 : 12142 : && maybe_ne (bitpos0, bitpos1)
10104 : 28411 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10105 : 1939 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10106 : 10611 : fold_overflow_warning (("assuming pointer wraparound does not "
10107 : : "occur when comparing P +- C1 with "
10108 : : "P +- C2"),
10109 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
10110 : :
10111 : 16296 : switch (code)
10112 : : {
10113 : 56 : case EQ_EXPR:
10114 : 56 : if (known_eq (bitpos0, bitpos1))
10115 : 52539 : return constant_boolean_node (true, type);
10116 : 21 : if (known_ne (bitpos0, bitpos1))
10117 : 21 : return constant_boolean_node (false, type);
10118 : : break;
10119 : 4098 : case NE_EXPR:
10120 : 4098 : if (known_ne (bitpos0, bitpos1))
10121 : 4093 : return constant_boolean_node (true, type);
10122 : 5 : if (known_eq (bitpos0, bitpos1))
10123 : 5 : return constant_boolean_node (false, type);
10124 : : break;
10125 : 2796 : case LT_EXPR:
10126 : 2796 : if (known_lt (bitpos0, bitpos1))
10127 : 2648 : return constant_boolean_node (true, type);
10128 : 148 : if (known_ge (bitpos0, bitpos1))
10129 : 148 : return constant_boolean_node (false, type);
10130 : : break;
10131 : 2428 : case LE_EXPR:
10132 : 2428 : if (known_le (bitpos0, bitpos1))
10133 : 288 : return constant_boolean_node (true, type);
10134 : 2140 : if (known_gt (bitpos0, bitpos1))
10135 : 2140 : return constant_boolean_node (false, type);
10136 : : break;
10137 : 5045 : case GE_EXPR:
10138 : 5045 : if (known_ge (bitpos0, bitpos1))
10139 : 2181 : return constant_boolean_node (true, type);
10140 : 2864 : if (known_lt (bitpos0, bitpos1))
10141 : 2864 : return constant_boolean_node (false, type);
10142 : : break;
10143 : 1873 : case GT_EXPR:
10144 : 1873 : if (known_gt (bitpos0, bitpos1))
10145 : 1808 : return constant_boolean_node (true, type);
10146 : 65 : if (known_le (bitpos0, bitpos1))
10147 : 65 : return constant_boolean_node (false, type);
10148 : : break;
10149 : : default:;
10150 : : }
10151 : : }
10152 : : /* We can simplify the comparison to a comparison of the variable
10153 : : offset parts if the constant offset parts are equal.
10154 : : Be careful to use signed sizetype here because otherwise we
10155 : : mess with array offsets in the wrong way. This is possible
10156 : : because pointer arithmetic is restricted to retain within an
10157 : : object and overflow on pointer differences is undefined as of
10158 : : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10159 : 7010 : else if (known_eq (bitpos0, bitpos1)
10160 : 7010 : && (equality_code
10161 : 5196 : || (indirect_base0
10162 : 292 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10163 : 4904 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10164 : : {
10165 : : /* By converting to signed sizetype we cover middle-end pointer
10166 : : arithmetic which operates on unsigned pointer types of size
10167 : : type size and ARRAY_REF offsets which are properly sign or
10168 : : zero extended from their type in case it is narrower than
10169 : : sizetype. */
10170 : 5306 : if (offset0 == NULL_TREE)
10171 : 0 : offset0 = build_int_cst (ssizetype, 0);
10172 : : else
10173 : 5306 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10174 : 5306 : if (offset1 == NULL_TREE)
10175 : 2781 : offset1 = build_int_cst (ssizetype, 0);
10176 : : else
10177 : 2525 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10178 : :
10179 : 5306 : if (!equality_code
10180 : 5306 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10181 : 0 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10182 : 5196 : fold_overflow_warning (("assuming pointer wraparound does not "
10183 : : "occur when comparing P +- C1 with "
10184 : : "P +- C2"),
10185 : : WARN_STRICT_OVERFLOW_COMPARISON);
10186 : :
10187 : 5306 : return fold_build2_loc (loc, code, type, offset0, offset1);
10188 : : }
10189 : : }
10190 : : /* For equal offsets we can simplify to a comparison of the
10191 : : base addresses. */
10192 : 2294565 : else if (known_eq (bitpos0, bitpos1)
10193 : 51401 : && (indirect_base0
10194 : 945515 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10195 : 12565 : && (indirect_base1
10196 : 145801 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10197 : 2476945 : && ((offset0 == offset1)
10198 : 6028 : || (offset0 && offset1
10199 : 5812 : && operand_equal_p (offset0, offset1, 0))))
10200 : : {
10201 : 30874 : if (indirect_base0)
10202 : 2645 : base0 = build_fold_addr_expr_loc (loc, base0);
10203 : 30874 : if (indirect_base1)
10204 : 3923 : base1 = build_fold_addr_expr_loc (loc, base1);
10205 : 30874 : return fold_build2_loc (loc, code, type, base0, base1);
10206 : : }
10207 : : /* Comparison between an ordinary (non-weak) symbol and a null
10208 : : pointer can be eliminated since such symbols must have a non
10209 : : null address. In C, relational expressions between pointers
10210 : : to objects and null pointers are undefined. The results
10211 : : below follow the C++ rules with the additional property that
10212 : : every object pointer compares greater than a null pointer.
10213 : : */
10214 : 2263691 : else if (((DECL_P (base0)
10215 : 216786 : && maybe_nonzero_address (base0) > 0
10216 : : /* Avoid folding references to struct members at offset 0 to
10217 : : prevent tests like '&ptr->firstmember == 0' from getting
10218 : : eliminated. When ptr is null, although the -> expression
10219 : : is strictly speaking invalid, GCC retains it as a matter
10220 : : of QoI. See PR c/44555. */
10221 : 203832 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10222 : 2233608 : || CONSTANT_CLASS_P (base0))
10223 : 34951 : && indirect_base0
10224 : : /* The caller guarantees that when one of the arguments is
10225 : : constant (i.e., null in this case) it is second. */
10226 : 2295922 : && integer_zerop (arg1))
10227 : : {
10228 : 63 : switch (code)
10229 : : {
10230 : 24 : case EQ_EXPR:
10231 : 24 : case LE_EXPR:
10232 : 24 : case LT_EXPR:
10233 : 24 : return constant_boolean_node (false, type);
10234 : 39 : case GE_EXPR:
10235 : 39 : case GT_EXPR:
10236 : 39 : case NE_EXPR:
10237 : 39 : return constant_boolean_node (true, type);
10238 : 0 : default:
10239 : 0 : gcc_unreachable ();
10240 : : }
10241 : : }
10242 : : }
10243 : :
10244 : : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10245 : : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10246 : : the resulting offset is smaller in absolute value than the
10247 : : original one and has the same sign. */
10248 : 168195376 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10249 : 130472876 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10250 : 31953468 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10251 : 2301524 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10252 : 1896003 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10253 : 1896003 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10254 : 151018406 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10255 : 164052 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10256 : : {
10257 : 164052 : tree const1 = TREE_OPERAND (arg0, 1);
10258 : 164052 : tree const2 = TREE_OPERAND (arg1, 1);
10259 : 164052 : tree variable1 = TREE_OPERAND (arg0, 0);
10260 : 164052 : tree variable2 = TREE_OPERAND (arg1, 0);
10261 : 164052 : tree cst;
10262 : 164052 : const char * const warnmsg = G_("assuming signed overflow does not "
10263 : : "occur when combining constants around "
10264 : : "a comparison");
10265 : :
10266 : : /* Put the constant on the side where it doesn't overflow and is
10267 : : of lower absolute value and of same sign than before. */
10268 : 164053 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10269 : : ? MINUS_EXPR : PLUS_EXPR,
10270 : : const2, const1);
10271 : 164052 : if (!TREE_OVERFLOW (cst)
10272 : 164036 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10273 : 185751 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10274 : : {
10275 : 5572 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10276 : 5572 : return fold_build2_loc (loc, code, type,
10277 : : variable1,
10278 : 5572 : fold_build2_loc (loc, TREE_CODE (arg1),
10279 : 5572 : TREE_TYPE (arg1),
10280 : 5572 : variable2, cst));
10281 : : }
10282 : :
10283 : 158481 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10284 : : ? MINUS_EXPR : PLUS_EXPR,
10285 : : const1, const2);
10286 : 158480 : if (!TREE_OVERFLOW (cst)
10287 : 158464 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10288 : 174607 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10289 : : {
10290 : 16127 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10291 : 16127 : return fold_build2_loc (loc, code, type,
10292 : 16127 : fold_build2_loc (loc, TREE_CODE (arg0),
10293 : 16127 : TREE_TYPE (arg0),
10294 : : variable1, cst),
10295 : 16127 : variable2);
10296 : : }
10297 : : }
10298 : :
10299 : 85593839 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10300 : 85593839 : if (tem)
10301 : : return tem;
10302 : :
10303 : : /* If we are comparing an expression that just has comparisons
10304 : : of two integer values, arithmetic expressions of those comparisons,
10305 : : and constants, we can simplify it. There are only three cases
10306 : : to check: the two values can either be equal, the first can be
10307 : : greater, or the second can be greater. Fold the expression for
10308 : : those three values. Since each value must be 0 or 1, we have
10309 : : eight possibilities, each of which corresponds to the constant 0
10310 : : or 1 or one of the six possible comparisons.
10311 : :
10312 : : This handles common cases like (a > b) == 0 but also handles
10313 : : expressions like ((x > y) - (y > x)) > 0, which supposedly
10314 : : occur in macroized code. */
10315 : :
10316 : 84814448 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10317 : : {
10318 : 51396519 : tree cval1 = 0, cval2 = 0;
10319 : :
10320 : 51396519 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10321 : : /* Don't handle degenerate cases here; they should already
10322 : : have been handled anyway. */
10323 : 610740 : && cval1 != 0 && cval2 != 0
10324 : 609556 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10325 : 609556 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10326 : 609550 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10327 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10328 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10329 : 51396577 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10330 : 58 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10331 : : {
10332 : 58 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10333 : 58 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10334 : :
10335 : : /* We can't just pass T to eval_subst in case cval1 or cval2
10336 : : was the same as ARG1. */
10337 : :
10338 : 58 : tree high_result
10339 : 58 : = fold_build2_loc (loc, code, type,
10340 : : eval_subst (loc, arg0, cval1, maxval,
10341 : : cval2, minval),
10342 : : arg1);
10343 : 58 : tree equal_result
10344 : 58 : = fold_build2_loc (loc, code, type,
10345 : : eval_subst (loc, arg0, cval1, maxval,
10346 : : cval2, maxval),
10347 : : arg1);
10348 : 58 : tree low_result
10349 : 58 : = fold_build2_loc (loc, code, type,
10350 : : eval_subst (loc, arg0, cval1, minval,
10351 : : cval2, maxval),
10352 : : arg1);
10353 : :
10354 : : /* All three of these results should be 0 or 1. Confirm they are.
10355 : : Then use those values to select the proper code to use. */
10356 : :
10357 : 58 : if (TREE_CODE (high_result) == INTEGER_CST
10358 : 49 : && TREE_CODE (equal_result) == INTEGER_CST
10359 : 39 : && TREE_CODE (low_result) == INTEGER_CST)
10360 : : {
10361 : : /* Make a 3-bit mask with the high-order bit being the
10362 : : value for `>', the next for '=', and the low for '<'. */
10363 : 39 : switch ((integer_onep (high_result) * 4)
10364 : 39 : + (integer_onep (equal_result) * 2)
10365 : 39 : + integer_onep (low_result))
10366 : : {
10367 : 21 : case 0:
10368 : : /* Always false. */
10369 : 39 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10370 : : case 1:
10371 : : code = LT_EXPR;
10372 : : break;
10373 : 2 : case 2:
10374 : 2 : code = EQ_EXPR;
10375 : 2 : break;
10376 : 0 : case 3:
10377 : 0 : code = LE_EXPR;
10378 : 0 : break;
10379 : 0 : case 4:
10380 : 0 : code = GT_EXPR;
10381 : 0 : break;
10382 : 1 : case 5:
10383 : 1 : code = NE_EXPR;
10384 : 1 : break;
10385 : 0 : case 6:
10386 : 0 : code = GE_EXPR;
10387 : 0 : break;
10388 : 15 : case 7:
10389 : : /* Always true. */
10390 : 15 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10391 : : }
10392 : :
10393 : 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10394 : : }
10395 : : }
10396 : : }
10397 : :
10398 : : return NULL_TREE;
10399 : : }
10400 : :
10401 : :
10402 : : /* Subroutine of fold_binary. Optimize complex multiplications of the
10403 : : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10404 : : argument EXPR represents the expression "z" of type TYPE. */
10405 : :
10406 : : static tree
10407 : 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10408 : : {
10409 : 2 : tree itype = TREE_TYPE (type);
10410 : 2 : tree rpart, ipart, tem;
10411 : :
10412 : 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10413 : : {
10414 : 0 : rpart = TREE_OPERAND (expr, 0);
10415 : 0 : ipart = TREE_OPERAND (expr, 1);
10416 : : }
10417 : 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10418 : : {
10419 : 0 : rpart = TREE_REALPART (expr);
10420 : 0 : ipart = TREE_IMAGPART (expr);
10421 : : }
10422 : : else
10423 : : {
10424 : 2 : expr = save_expr (expr);
10425 : 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10426 : 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10427 : : }
10428 : :
10429 : 2 : rpart = save_expr (rpart);
10430 : 2 : ipart = save_expr (ipart);
10431 : 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10432 : : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10433 : : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10434 : 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10435 : 2 : build_zero_cst (itype));
10436 : : }
10437 : :
10438 : :
10439 : : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10440 : : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10441 : : true if successful. */
10442 : :
10443 : : static bool
10444 : 11708 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10445 : : {
10446 : 11708 : unsigned HOST_WIDE_INT i, nunits;
10447 : :
10448 : 11708 : if (TREE_CODE (arg) == VECTOR_CST
10449 : 11708 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10450 : : {
10451 : 1895 : for (i = 0; i < nunits; ++i)
10452 : 1502 : elts[i] = VECTOR_CST_ELT (arg, i);
10453 : : }
10454 : 11315 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10455 : : {
10456 : : constructor_elt *elt;
10457 : :
10458 : 36163 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10459 : 30241 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10460 : 5393 : return false;
10461 : : else
10462 : 24848 : elts[i] = elt->value;
10463 : : }
10464 : : else
10465 : : return false;
10466 : 7089 : for (; i < nelts; i++)
10467 : 1548 : elts[i]
10468 : 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10469 : : return true;
10470 : : }
10471 : :
10472 : : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10473 : : mask for VLA vec_perm folding.
10474 : : REASON if specified, will contain the reason why SEL is not suitable.
10475 : : Used only for debugging and unit-testing. */
10476 : :
10477 : : static bool
10478 : 7827 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10479 : : const vec_perm_indices &sel,
10480 : : const char **reason = NULL)
10481 : : {
10482 : 7827 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10483 : 7827 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10484 : :
10485 : 15654 : if (!(pow2p_hwi (sel_npatterns)
10486 : 7827 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10487 : 7827 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10488 : : {
10489 : 0 : if (reason)
10490 : 0 : *reason = "npatterns is not power of 2";
10491 : 0 : return false;
10492 : : }
10493 : :
10494 : : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10495 : : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10496 : 7827 : poly_uint64 esel;
10497 : 7827 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10498 : : {
10499 : 0 : if (reason)
10500 : 0 : *reason = "sel.length is not multiple of sel_npatterns";
10501 : 0 : return false;
10502 : : }
10503 : :
10504 : 7827 : if (sel_nelts_per_pattern < 3)
10505 : : return true;
10506 : :
10507 : 5547 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10508 : : {
10509 : 4196 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10510 : 4196 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10511 : 4196 : HOST_WIDE_INT step;
10512 : 4196 : if (!poly_int64 (a2 - a1).is_constant (&step))
10513 : : {
10514 : : if (reason)
10515 : : *reason = "step is not constant";
10516 : 987 : return false;
10517 : : }
10518 : : // FIXME: Punt on step < 0 for now, revisit later.
10519 : 4196 : if (step < 0)
10520 : : return false;
10521 : 4132 : if (step == 0)
10522 : 0 : continue;
10523 : :
10524 : 4132 : if (!pow2p_hwi (step))
10525 : : {
10526 : 0 : if (reason)
10527 : 0 : *reason = "step is not power of 2";
10528 : 0 : return false;
10529 : : }
10530 : :
10531 : : /* Ensure that stepped sequence of the pattern selects elements
10532 : : only from the same input vector. */
10533 : 4132 : uint64_t q1, qe;
10534 : 4132 : poly_uint64 r1, re;
10535 : 4132 : poly_uint64 ae = a1 + (esel - 2) * step;
10536 : 4132 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10537 : :
10538 : 4132 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10539 : 4132 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10540 : : && q1 == qe))
10541 : : {
10542 : 328 : if (reason)
10543 : 0 : *reason = "crossed input vectors";
10544 : 328 : return false;
10545 : : }
10546 : :
10547 : : /* Ensure that the stepped sequence always selects from the same
10548 : : input pattern. */
10549 : 3804 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10550 : 3804 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10551 : :
10552 : 3804 : if (!multiple_p (step, arg_npatterns))
10553 : : {
10554 : 593 : if (reason)
10555 : 0 : *reason = "step is not multiple of npatterns";
10556 : 593 : return false;
10557 : : }
10558 : :
10559 : : /* If a1 chooses base element from arg, ensure that it's a natural
10560 : : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10561 : : to preserve arg's encoding. */
10562 : :
10563 : 3211 : if (maybe_lt (r1, arg_npatterns))
10564 : : {
10565 : 2 : unsigned HOST_WIDE_INT index;
10566 : 2 : if (!r1.is_constant (&index))
10567 : 2 : return false;
10568 : :
10569 : 2 : tree arg_elem0 = vector_cst_elt (arg, index);
10570 : 2 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10571 : 2 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10572 : :
10573 : 2 : tree step1, step2;
10574 : 2 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10575 : 2 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10576 : 4 : || !operand_equal_p (step1, step2, 0))
10577 : : {
10578 : 2 : if (reason)
10579 : 0 : *reason = "not a natural stepped sequence";
10580 : 2 : return false;
10581 : : }
10582 : : }
10583 : : }
10584 : :
10585 : : return true;
10586 : : }
10587 : :
10588 : : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10589 : : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10590 : : REASON has same purpose as described in
10591 : : valid_mask_for_fold_vec_perm_cst_p. */
10592 : :
10593 : : static tree
10594 : 7827 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10595 : : const char **reason = NULL)
10596 : : {
10597 : 7827 : unsigned res_npatterns, res_nelts_per_pattern;
10598 : 7827 : unsigned HOST_WIDE_INT res_nelts;
10599 : :
10600 : : /* First try to implement the fold in a VLA-friendly way.
10601 : :
10602 : : (1) If the selector is simply a duplication of N elements, the
10603 : : result is likewise a duplication of N elements.
10604 : :
10605 : : (2) If the selector is N elements followed by a duplication
10606 : : of N elements, the result is too.
10607 : :
10608 : : (3) If the selector is N elements followed by an interleaving
10609 : : of N linear series, the situation is more complex.
10610 : :
10611 : : valid_mask_for_fold_vec_perm_cst_p detects whether we
10612 : : can handle this case. If we can, then each of the N linear
10613 : : series either (a) selects the same element each time or
10614 : : (b) selects a linear series from one of the input patterns.
10615 : :
10616 : : If (b) holds for one of the linear series, the result
10617 : : will contain a linear series, and so the result will have
10618 : : the same shape as the selector. If (a) holds for all of
10619 : : the linear series, the result will be the same as (2) above.
10620 : :
10621 : : (b) can only hold if one of the input patterns has a
10622 : : stepped encoding. */
10623 : :
10624 : 7827 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10625 : : {
10626 : 6840 : res_npatterns = sel.encoding ().npatterns ();
10627 : 6840 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10628 : 6840 : if (res_nelts_per_pattern == 3
10629 : 1351 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10630 : 7710 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10631 : : res_nelts_per_pattern = 2;
10632 : 6840 : res_nelts = res_npatterns * res_nelts_per_pattern;
10633 : : }
10634 : 987 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10635 : : {
10636 : 987 : res_npatterns = res_nelts;
10637 : 987 : res_nelts_per_pattern = 1;
10638 : : }
10639 : : else
10640 : : return NULL_TREE;
10641 : :
10642 : 7827 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10643 : 49330 : for (unsigned i = 0; i < res_nelts; i++)
10644 : : {
10645 : 41503 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10646 : 41503 : uint64_t q;
10647 : 41503 : poly_uint64 r;
10648 : 41503 : unsigned HOST_WIDE_INT index;
10649 : :
10650 : : /* Punt if sel[i] /trunc_div len cannot be determined,
10651 : : because the input vector to be chosen will depend on
10652 : : runtime vector length.
10653 : : For example if len == 4 + 4x, and sel[i] == 4,
10654 : : If len at runtime equals 4, we choose arg1[0].
10655 : : For any other value of len > 4 at runtime, we choose arg0[4].
10656 : : which makes the element choice dependent on runtime vector length. */
10657 : 41503 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10658 : : {
10659 : : if (reason)
10660 : : *reason = "cannot divide selector element by arg len";
10661 : : return NULL_TREE;
10662 : : }
10663 : :
10664 : : /* sel[i] % len will give the index of element in the chosen input
10665 : : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10666 : : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10667 : 41503 : if (!r.is_constant (&index))
10668 : : {
10669 : : if (reason)
10670 : : *reason = "remainder is not constant";
10671 : : return NULL_TREE;
10672 : : }
10673 : :
10674 : 41503 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10675 : 41503 : tree elem = vector_cst_elt (arg, index);
10676 : 41503 : out_elts.quick_push (elem);
10677 : : }
10678 : :
10679 : 7827 : return out_elts.build ();
10680 : 7827 : }
10681 : :
10682 : : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10683 : : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10684 : : NULL_TREE otherwise. */
10685 : :
10686 : : tree
10687 : 26673 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10688 : : {
10689 : 26673 : unsigned int i;
10690 : 26673 : unsigned HOST_WIDE_INT nelts;
10691 : :
10692 : 26673 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10693 : : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10694 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10695 : :
10696 : 26673 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10697 : 26673 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10698 : : return NULL_TREE;
10699 : :
10700 : 16368 : if (TREE_CODE (arg0) == VECTOR_CST
10701 : 8084 : && TREE_CODE (arg1) == VECTOR_CST)
10702 : 7827 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10703 : :
10704 : : /* For fall back case, we want to ensure we have VLS vectors
10705 : : with equal length. */
10706 : 8541 : if (!sel.length ().is_constant (&nelts))
10707 : : return NULL_TREE;
10708 : :
10709 : 8541 : gcc_assert (known_eq (sel.length (),
10710 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))));
10711 : 8541 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10712 : 8541 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10713 : 8541 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10714 : 5393 : return NULL_TREE;
10715 : :
10716 : 3148 : vec<constructor_elt, va_gc> *v;
10717 : 3148 : vec_alloc (v, nelts);
10718 : 16634 : for (i = 0; i < nelts; i++)
10719 : : {
10720 : 13486 : HOST_WIDE_INT index;
10721 : 13486 : if (!sel[i].is_constant (&index))
10722 : : return NULL_TREE;
10723 : 13486 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10724 : : }
10725 : 3148 : return build_constructor (type, v);
10726 : : }
10727 : :
10728 : : /* Try to fold a pointer difference of type TYPE two address expressions of
10729 : : array references AREF0 and AREF1 using location LOC. Return a
10730 : : simplified expression for the difference or NULL_TREE. */
10731 : :
10732 : : static tree
10733 : 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10734 : : tree aref0, tree aref1,
10735 : : bool use_pointer_diff)
10736 : : {
10737 : 39 : tree base0 = TREE_OPERAND (aref0, 0);
10738 : 39 : tree base1 = TREE_OPERAND (aref1, 0);
10739 : 39 : tree base_offset = build_int_cst (type, 0);
10740 : :
10741 : : /* If the bases are array references as well, recurse. If the bases
10742 : : are pointer indirections compute the difference of the pointers.
10743 : : If the bases are equal, we are set. */
10744 : 39 : if ((TREE_CODE (base0) == ARRAY_REF
10745 : 1 : && TREE_CODE (base1) == ARRAY_REF
10746 : 1 : && (base_offset
10747 : 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10748 : : use_pointer_diff)))
10749 : 38 : || (INDIRECT_REF_P (base0)
10750 : 7 : && INDIRECT_REF_P (base1)
10751 : 7 : && (base_offset
10752 : : = use_pointer_diff
10753 : 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10754 : 1 : TREE_OPERAND (base0, 0),
10755 : 1 : TREE_OPERAND (base1, 0))
10756 : 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10757 : 6 : fold_convert (type,
10758 : : TREE_OPERAND (base0, 0)),
10759 : 6 : fold_convert (type,
10760 : : TREE_OPERAND (base1, 0)))))
10761 : 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10762 : : {
10763 : 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10764 : 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10765 : 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10766 : 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10767 : 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10768 : : base_offset,
10769 : : fold_build2_loc (loc, MULT_EXPR, type,
10770 : 15 : diff, esz));
10771 : : }
10772 : : return NULL_TREE;
10773 : : }
10774 : :
10775 : : /* If the real or vector real constant CST of type TYPE has an exact
10776 : : inverse, return it, else return NULL. */
10777 : :
10778 : : tree
10779 : 1220445 : exact_inverse (tree type, tree cst)
10780 : : {
10781 : 1220445 : REAL_VALUE_TYPE r;
10782 : 1220445 : tree unit_type;
10783 : 1220445 : machine_mode mode;
10784 : :
10785 : 1220445 : switch (TREE_CODE (cst))
10786 : : {
10787 : 1219932 : case REAL_CST:
10788 : 1219932 : r = TREE_REAL_CST (cst);
10789 : :
10790 : 1219932 : if (exact_real_inverse (TYPE_MODE (type), &r))
10791 : 347025 : return build_real (type, r);
10792 : :
10793 : : return NULL_TREE;
10794 : :
10795 : 513 : case VECTOR_CST:
10796 : 513 : {
10797 : 513 : unit_type = TREE_TYPE (type);
10798 : 513 : mode = TYPE_MODE (unit_type);
10799 : :
10800 : 513 : tree_vector_builder elts;
10801 : 513 : if (!elts.new_unary_operation (type, cst, false))
10802 : : return NULL_TREE;
10803 : 513 : unsigned int count = elts.encoded_nelts ();
10804 : 573 : for (unsigned int i = 0; i < count; ++i)
10805 : : {
10806 : 513 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10807 : 513 : if (!exact_real_inverse (mode, &r))
10808 : : return NULL_TREE;
10809 : 60 : elts.quick_push (build_real (unit_type, r));
10810 : : }
10811 : :
10812 : 60 : return elts.build ();
10813 : 513 : }
10814 : :
10815 : : default:
10816 : : return NULL_TREE;
10817 : : }
10818 : : }
10819 : :
10820 : : /* Mask out the tz least significant bits of X of type TYPE where
10821 : : tz is the number of trailing zeroes in Y. */
10822 : : static wide_int
10823 : 107327 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10824 : : {
10825 : 107327 : int tz = wi::ctz (y);
10826 : 107327 : if (tz > 0)
10827 : 6460 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10828 : 100867 : return x;
10829 : : }
10830 : :
10831 : : /* Return true when T is an address and is known to be nonzero.
10832 : : For floating point we further ensure that T is not denormal.
10833 : : Similar logic is present in nonzero_address in rtlanal.h.
10834 : :
10835 : : If the return value is based on the assumption that signed overflow
10836 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10837 : : change *STRICT_OVERFLOW_P. */
10838 : :
10839 : : static bool
10840 : 146924555 : tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10841 : : {
10842 : 147249195 : tree type = TREE_TYPE (t);
10843 : 147249195 : enum tree_code code;
10844 : :
10845 : : /* Doing something useful for floating point would need more work. */
10846 : 147249195 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10847 : : return false;
10848 : :
10849 : 147124861 : code = TREE_CODE (t);
10850 : 147124861 : switch (TREE_CODE_CLASS (code))
10851 : : {
10852 : 859200 : case tcc_unary:
10853 : 859200 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10854 : 859200 : strict_overflow_p);
10855 : 2925434 : case tcc_binary:
10856 : 2925434 : case tcc_comparison:
10857 : 2925434 : return tree_binary_nonzero_warnv_p (code, type,
10858 : 2925434 : TREE_OPERAND (t, 0),
10859 : 2925434 : TREE_OPERAND (t, 1),
10860 : 2925434 : strict_overflow_p);
10861 : 12028523 : case tcc_constant:
10862 : 12028523 : case tcc_declaration:
10863 : 12028523 : case tcc_reference:
10864 : 12028523 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10865 : :
10866 : 131311704 : default:
10867 : 131311704 : break;
10868 : : }
10869 : :
10870 : 131311704 : switch (code)
10871 : : {
10872 : 609109 : case TRUTH_NOT_EXPR:
10873 : 609109 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10874 : 609109 : strict_overflow_p);
10875 : :
10876 : 79064 : case TRUTH_AND_EXPR:
10877 : 79064 : case TRUTH_OR_EXPR:
10878 : 79064 : case TRUTH_XOR_EXPR:
10879 : 79064 : return tree_binary_nonzero_warnv_p (code, type,
10880 : 79064 : TREE_OPERAND (t, 0),
10881 : 79064 : TREE_OPERAND (t, 1),
10882 : 79064 : strict_overflow_p);
10883 : :
10884 : 127583538 : case COND_EXPR:
10885 : 127583538 : case CONSTRUCTOR:
10886 : 127583538 : case OBJ_TYPE_REF:
10887 : 127583538 : case ADDR_EXPR:
10888 : 127583538 : case WITH_SIZE_EXPR:
10889 : 127583538 : case SSA_NAME:
10890 : 127583538 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10891 : :
10892 : 86060 : case COMPOUND_EXPR:
10893 : 86060 : case MODIFY_EXPR:
10894 : 86060 : case BIND_EXPR:
10895 : 86060 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10896 : 86060 : strict_overflow_p);
10897 : :
10898 : 238580 : case SAVE_EXPR:
10899 : 238580 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10900 : 238580 : strict_overflow_p);
10901 : :
10902 : 2659502 : case CALL_EXPR:
10903 : 2659502 : {
10904 : 2659502 : tree fndecl = get_callee_fndecl (t);
10905 : 2659502 : if (!fndecl) return false;
10906 : 2657753 : if (flag_delete_null_pointer_checks && !flag_check_new
10907 : 2657753 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10908 : 2658447 : && !TREE_NOTHROW (fndecl))
10909 : : return true;
10910 : 2658447 : if (flag_delete_null_pointer_checks
10911 : 5316200 : && lookup_attribute ("returns_nonnull",
10912 : 2657753 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10913 : : return true;
10914 : 2658439 : return alloca_call_p (t);
10915 : : }
10916 : :
10917 : : default:
10918 : : break;
10919 : : }
10920 : : return false;
10921 : : }
10922 : :
10923 : : /* Return true when T is an address and is known to be nonzero.
10924 : : Handle warnings about undefined signed overflow. */
10925 : :
10926 : : bool
10927 : 145812394 : tree_expr_nonzero_p (tree t)
10928 : : {
10929 : 145812394 : bool ret, strict_overflow_p;
10930 : :
10931 : 145812394 : strict_overflow_p = false;
10932 : 145812394 : ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10933 : 145812394 : if (strict_overflow_p)
10934 : 0 : fold_overflow_warning (("assuming signed overflow does not occur when "
10935 : : "determining that expression is always "
10936 : : "non-zero"),
10937 : : WARN_STRICT_OVERFLOW_MISC);
10938 : 145812394 : return ret;
10939 : : }
10940 : :
10941 : : /* Return true if T is known not to be equal to an integer W.
10942 : : If STMT is specified, the check is if T on STMT is not equal
10943 : : to W. */
10944 : :
10945 : : bool
10946 : 100297610 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10947 : : {
10948 : 100297610 : int_range_max vr;
10949 : 100297610 : switch (TREE_CODE (t))
10950 : : {
10951 : 1075540 : case INTEGER_CST:
10952 : 1075540 : return wi::to_wide (t) != w;
10953 : :
10954 : 99220975 : case SSA_NAME:
10955 : 99220975 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10956 : : return false;
10957 : :
10958 : 198441950 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10959 : 99220975 : if (!vr.undefined_p () && !vr.contains_p (w))
10960 : : return true;
10961 : : /* If T has some known zero bits and W has any of those bits set,
10962 : : then T is known not to be equal to W. */
10963 : 99092625 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10964 : 198184874 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10965 : : return true;
10966 : : return false;
10967 : :
10968 : : default:
10969 : : return false;
10970 : : }
10971 : 100297610 : }
10972 : :
10973 : : /* Fold a binary expression of code CODE and type TYPE with operands
10974 : : OP0 and OP1. LOC is the location of the resulting expression.
10975 : : Return the folded expression if folding is successful. Otherwise,
10976 : : return NULL_TREE. */
10977 : :
10978 : : tree
10979 : 804008944 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10980 : : tree op0, tree op1)
10981 : : {
10982 : 804008944 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10983 : 804008944 : tree arg0, arg1, tem;
10984 : 804008944 : tree t1 = NULL_TREE;
10985 : 804008944 : bool strict_overflow_p;
10986 : 804008944 : unsigned int prec;
10987 : :
10988 : 804008944 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10989 : : && TREE_CODE_LENGTH (code) == 2
10990 : : && op0 != NULL_TREE
10991 : : && op1 != NULL_TREE);
10992 : :
10993 : 804008944 : arg0 = op0;
10994 : 804008944 : arg1 = op1;
10995 : :
10996 : : /* Strip any conversions that don't change the mode. This is
10997 : : safe for every expression, except for a comparison expression
10998 : : because its signedness is derived from its operands. So, in
10999 : : the latter case, only strip conversions that don't change the
11000 : : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
11001 : : preserved.
11002 : :
11003 : : Note that this is done as an internal manipulation within the
11004 : : constant folder, in order to find the simplest representation
11005 : : of the arguments so that their form can be studied. In any
11006 : : cases, the appropriate type conversions should be put back in
11007 : : the tree that will get out of the constant folder. */
11008 : :
11009 : 804008944 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
11010 : : {
11011 : 178533018 : STRIP_SIGN_NOPS (arg0);
11012 : 178533018 : STRIP_SIGN_NOPS (arg1);
11013 : : }
11014 : : else
11015 : : {
11016 : 625475926 : STRIP_NOPS (arg0);
11017 : 625475926 : STRIP_NOPS (arg1);
11018 : : }
11019 : :
11020 : : /* Note that TREE_CONSTANT isn't enough: static var addresses are
11021 : : constant but we can't do arithmetic on them. */
11022 : 804008944 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11023 : : {
11024 : 201369453 : tem = const_binop (code, type, arg0, arg1);
11025 : 201369453 : if (tem != NULL_TREE)
11026 : : {
11027 : 198627004 : if (TREE_TYPE (tem) != type)
11028 : 1797248 : tem = fold_convert_loc (loc, type, tem);
11029 : 198627004 : return tem;
11030 : : }
11031 : : }
11032 : :
11033 : : /* If this is a commutative operation, and ARG0 is a constant, move it
11034 : : to ARG1 to reduce the number of tests below. */
11035 : 605381940 : if (commutative_tree_code (code)
11036 : 605381940 : && tree_swap_operands_p (arg0, arg1))
11037 : 32759843 : return fold_build2_loc (loc, code, type, op1, op0);
11038 : :
11039 : : /* Likewise if this is a comparison, and ARG0 is a constant, move it
11040 : : to ARG1 to reduce the number of tests below. */
11041 : 572622097 : if (kind == tcc_comparison
11042 : 572622097 : && tree_swap_operands_p (arg0, arg1))
11043 : 7798683 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11044 : :
11045 : 564823414 : tem = generic_simplify (loc, code, type, op0, op1);
11046 : 564823414 : if (tem)
11047 : : return tem;
11048 : :
11049 : : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11050 : :
11051 : : First check for cases where an arithmetic operation is applied to a
11052 : : compound, conditional, or comparison operation. Push the arithmetic
11053 : : operation inside the compound or conditional to see if any folding
11054 : : can then be done. Convert comparison to conditional for this purpose.
11055 : : The also optimizes non-constant cases that used to be done in
11056 : : expand_expr.
11057 : :
11058 : : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11059 : : one of the operands is a comparison and the other is a comparison, a
11060 : : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11061 : : code below would make the expression more complex. Change it to a
11062 : : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11063 : : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11064 : :
11065 : 475726002 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11066 : : || code == EQ_EXPR || code == NE_EXPR)
11067 : 56001724 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11068 : 55427431 : && ((truth_value_p (TREE_CODE (arg0))
11069 : 1242878 : && (truth_value_p (TREE_CODE (arg1))
11070 : 931758 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11071 : 40 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11072 : 55116295 : || (truth_value_p (TREE_CODE (arg1))
11073 : 6768 : && (truth_value_p (TREE_CODE (arg0))
11074 : 6768 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11075 : 167 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11076 : : {
11077 : 353795 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11078 : 42645 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11079 : : : TRUTH_XOR_EXPR,
11080 : : boolean_type_node,
11081 : : fold_convert_loc (loc, boolean_type_node, arg0),
11082 : : fold_convert_loc (loc, boolean_type_node, arg1));
11083 : :
11084 : 311150 : if (code == EQ_EXPR)
11085 : 36357 : tem = invert_truthvalue_loc (loc, tem);
11086 : :
11087 : 311150 : return fold_convert_loc (loc, type, tem);
11088 : : }
11089 : :
11090 : 475414852 : if (TREE_CODE_CLASS (code) == tcc_binary
11091 : 270882268 : || TREE_CODE_CLASS (code) == tcc_comparison)
11092 : : {
11093 : 296358313 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11094 : : {
11095 : 81482 : tem = fold_build2_loc (loc, code, type,
11096 : 81482 : fold_convert_loc (loc, TREE_TYPE (op0),
11097 : 81482 : TREE_OPERAND (arg0, 1)), op1);
11098 : 81482 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11099 : 81482 : tem);
11100 : : }
11101 : 296276831 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11102 : : {
11103 : 3137 : tem = fold_build2_loc (loc, code, type, op0,
11104 : 3137 : fold_convert_loc (loc, TREE_TYPE (op1),
11105 : 3137 : TREE_OPERAND (arg1, 1)));
11106 : 3137 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11107 : 3137 : tem);
11108 : : }
11109 : :
11110 : 296273694 : if (TREE_CODE (arg0) == COND_EXPR
11111 : 295900099 : || TREE_CODE (arg0) == VEC_COND_EXPR
11112 : 295897989 : || COMPARISON_CLASS_P (arg0))
11113 : : {
11114 : 743949 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11115 : : arg0, arg1,
11116 : : /*cond_first_p=*/1);
11117 : 743949 : if (tem != NULL_TREE)
11118 : : return tem;
11119 : : }
11120 : :
11121 : 295788544 : if (TREE_CODE (arg1) == COND_EXPR
11122 : 295560897 : || TREE_CODE (arg1) == VEC_COND_EXPR
11123 : 295560673 : || COMPARISON_CLASS_P (arg1))
11124 : : {
11125 : 242112 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11126 : : arg1, arg0,
11127 : : /*cond_first_p=*/0);
11128 : 242112 : if (tem != NULL_TREE)
11129 : : return tem;
11130 : : }
11131 : : }
11132 : :
11133 : 474837395 : switch (code)
11134 : : {
11135 : 51636201 : case MEM_REF:
11136 : : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11137 : 51636201 : if (TREE_CODE (arg0) == ADDR_EXPR
11138 : 51636201 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11139 : : {
11140 : 860893 : tree iref = TREE_OPERAND (arg0, 0);
11141 : 860893 : return fold_build2 (MEM_REF, type,
11142 : : TREE_OPERAND (iref, 0),
11143 : : int_const_binop (PLUS_EXPR, arg1,
11144 : : TREE_OPERAND (iref, 1)));
11145 : : }
11146 : :
11147 : : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11148 : 50775308 : if (TREE_CODE (arg0) == ADDR_EXPR
11149 : 50775308 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11150 : : {
11151 : 2495294 : tree base;
11152 : 2495294 : poly_int64 coffset;
11153 : 2495294 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11154 : : &coffset);
11155 : 2495294 : if (!base)
11156 : : return NULL_TREE;
11157 : 2491554 : return fold_build2 (MEM_REF, type,
11158 : : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11159 : : int_const_binop (PLUS_EXPR, arg1,
11160 : : size_int (coffset)));
11161 : : }
11162 : :
11163 : : return NULL_TREE;
11164 : :
11165 : 32569197 : case POINTER_PLUS_EXPR:
11166 : : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11167 : 65137978 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11168 : 65128513 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11169 : 35517 : return fold_convert_loc (loc, type,
11170 : : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11171 : : fold_convert_loc (loc, sizetype,
11172 : : arg1),
11173 : : fold_convert_loc (loc, sizetype,
11174 : 35517 : arg0)));
11175 : :
11176 : : return NULL_TREE;
11177 : :
11178 : 60846876 : case PLUS_EXPR:
11179 : 60846876 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11180 : : {
11181 : : /* X + (X / CST) * -CST is X % CST. */
11182 : 48874982 : if (TREE_CODE (arg1) == MULT_EXPR
11183 : 2287941 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11184 : 48881155 : && operand_equal_p (arg0,
11185 : 6173 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11186 : : {
11187 : 214 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11188 : 214 : tree cst1 = TREE_OPERAND (arg1, 1);
11189 : 214 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11190 : : cst1, cst0);
11191 : 214 : if (sum && integer_zerop (sum))
11192 : 214 : return fold_convert_loc (loc, type,
11193 : : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11194 : 214 : TREE_TYPE (arg0), arg0,
11195 : 214 : cst0));
11196 : : }
11197 : : }
11198 : :
11199 : : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11200 : : one. Make sure the type is not saturating and has the signedness of
11201 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11202 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11203 : 60846662 : if ((TREE_CODE (arg0) == MULT_EXPR
11204 : 49433134 : || TREE_CODE (arg1) == MULT_EXPR)
11205 : 12768842 : && !TYPE_SATURATING (type)
11206 : 12768842 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11207 : 12367994 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11208 : 72559681 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11209 : : {
11210 : 8323789 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11211 : 8323789 : if (tem)
11212 : : return tem;
11213 : : }
11214 : :
11215 : 59640446 : if (! FLOAT_TYPE_P (type))
11216 : : {
11217 : : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11218 : : (plus (plus (mult) (mult)) (foo)) so that we can
11219 : : take advantage of the factoring cases below. */
11220 : 280890 : if (ANY_INTEGRAL_TYPE_P (type)
11221 : 47670900 : && TYPE_OVERFLOW_WRAPS (type)
11222 : 47670900 : && (((TREE_CODE (arg0) == PLUS_EXPR
11223 : 29877925 : || TREE_CODE (arg0) == MINUS_EXPR)
11224 : 3401135 : && TREE_CODE (arg1) == MULT_EXPR)
11225 : 29374169 : || ((TREE_CODE (arg1) == PLUS_EXPR
11226 : 29374169 : || TREE_CODE (arg1) == MINUS_EXPR)
11227 : 441654 : && TREE_CODE (arg0) == MULT_EXPR)))
11228 : : {
11229 : 552774 : tree parg0, parg1, parg, marg;
11230 : 552774 : enum tree_code pcode;
11231 : :
11232 : 552774 : if (TREE_CODE (arg1) == MULT_EXPR)
11233 : : parg = arg0, marg = arg1;
11234 : : else
11235 : 49018 : parg = arg1, marg = arg0;
11236 : 552774 : pcode = TREE_CODE (parg);
11237 : 552774 : parg0 = TREE_OPERAND (parg, 0);
11238 : 552774 : parg1 = TREE_OPERAND (parg, 1);
11239 : 552774 : STRIP_NOPS (parg0);
11240 : 552774 : STRIP_NOPS (parg1);
11241 : :
11242 : 552774 : if (TREE_CODE (parg0) == MULT_EXPR
11243 : 256414 : && TREE_CODE (parg1) != MULT_EXPR)
11244 : 227009 : return fold_build2_loc (loc, pcode, type,
11245 : : fold_build2_loc (loc, PLUS_EXPR, type,
11246 : : fold_convert_loc (loc, type,
11247 : : parg0),
11248 : : fold_convert_loc (loc, type,
11249 : : marg)),
11250 : 227009 : fold_convert_loc (loc, type, parg1));
11251 : 325765 : if (TREE_CODE (parg0) != MULT_EXPR
11252 : 296360 : && TREE_CODE (parg1) == MULT_EXPR)
11253 : 99383 : return
11254 : 99383 : fold_build2_loc (loc, PLUS_EXPR, type,
11255 : : fold_convert_loc (loc, type, parg0),
11256 : : fold_build2_loc (loc, pcode, type,
11257 : : fold_convert_loc (loc, type, marg),
11258 : : fold_convert_loc (loc, type,
11259 : 99383 : parg1)));
11260 : : }
11261 : : }
11262 : : else
11263 : : {
11264 : : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11265 : : to __complex__ ( x, y ). This is not the same for SNaNs or
11266 : : if signed zeros are involved. */
11267 : 11969546 : if (!HONOR_SNANS (arg0)
11268 : 11967886 : && !HONOR_SIGNED_ZEROS (arg0)
11269 : 11990618 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11270 : : {
11271 : 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11272 : 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11273 : 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11274 : 3086 : bool arg0rz = false, arg0iz = false;
11275 : 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11276 : 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11277 : : {
11278 : 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11279 : 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11280 : 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11281 : : {
11282 : 22 : tree rp = arg1r ? arg1r
11283 : 0 : : build1 (REALPART_EXPR, rtype, arg1);
11284 : 22 : tree ip = arg0i ? arg0i
11285 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11286 : 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11287 : : }
11288 : 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11289 : : {
11290 : 53 : tree rp = arg0r ? arg0r
11291 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11292 : 53 : tree ip = arg1i ? arg1i
11293 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11294 : 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11295 : : }
11296 : : }
11297 : : }
11298 : :
11299 : : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11300 : : We associate floats only if the user has specified
11301 : : -fassociative-math. */
11302 : 11969471 : if (flag_associative_math
11303 : 20975 : && TREE_CODE (arg1) == PLUS_EXPR
11304 : 36 : && TREE_CODE (arg0) != MULT_EXPR)
11305 : : {
11306 : 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11307 : 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11308 : 21 : if (TREE_CODE (tree11) == MULT_EXPR
11309 : 5 : && TREE_CODE (tree10) == MULT_EXPR)
11310 : : {
11311 : 1 : tree tree0;
11312 : 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11313 : 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11314 : : }
11315 : : }
11316 : : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11317 : : We associate floats only if the user has specified
11318 : : -fassociative-math. */
11319 : 11969470 : if (flag_associative_math
11320 : 20974 : && TREE_CODE (arg0) == PLUS_EXPR
11321 : 1222 : && TREE_CODE (arg1) != MULT_EXPR)
11322 : : {
11323 : 832 : tree tree00 = TREE_OPERAND (arg0, 0);
11324 : 832 : tree tree01 = TREE_OPERAND (arg0, 1);
11325 : 832 : if (TREE_CODE (tree01) == MULT_EXPR
11326 : 49 : && TREE_CODE (tree00) == MULT_EXPR)
11327 : : {
11328 : 9 : tree tree0;
11329 : 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11330 : 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11331 : : }
11332 : : }
11333 : : }
11334 : :
11335 : 11968638 : bit_rotate:
11336 : : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11337 : : is a rotate of A by C1 bits. */
11338 : : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11339 : : is a rotate of A by B bits.
11340 : : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11341 : : though in this case CODE must be | and not + or ^, otherwise
11342 : : it doesn't return A when B is 0. */
11343 : 61899128 : {
11344 : 61899128 : enum tree_code code0, code1;
11345 : 61899128 : tree rtype;
11346 : 61899128 : code0 = TREE_CODE (arg0);
11347 : 61899128 : code1 = TREE_CODE (arg1);
11348 : 55233 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11349 : 61882780 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11350 : 39704 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11351 : 39704 : TREE_OPERAND (arg1, 0), 0)
11352 : 36955 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11353 : 36955 : TYPE_UNSIGNED (rtype))
11354 : : /* Only create rotates in complete modes. Other cases are not
11355 : : expanded properly. */
11356 : 61925913 : && (element_precision (rtype)
11357 : 53570 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11358 : : {
11359 : 26715 : tree tree01, tree11;
11360 : 26715 : tree orig_tree01, orig_tree11;
11361 : 26715 : enum tree_code code01, code11;
11362 : :
11363 : 26715 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11364 : 26715 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11365 : 26715 : STRIP_NOPS (tree01);
11366 : 26715 : STRIP_NOPS (tree11);
11367 : 26715 : code01 = TREE_CODE (tree01);
11368 : 26715 : code11 = TREE_CODE (tree11);
11369 : 26715 : if (code11 != MINUS_EXPR
11370 : 26029 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11371 : : {
11372 : 1478 : std::swap (code0, code1);
11373 : 1478 : std::swap (code01, code11);
11374 : 1478 : std::swap (tree01, tree11);
11375 : 1478 : std::swap (orig_tree01, orig_tree11);
11376 : : }
11377 : 53430 : if (code01 == INTEGER_CST
11378 : 3152 : && code11 == INTEGER_CST
11379 : 33017 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11380 : 33017 : == element_precision (rtype)))
11381 : : {
11382 : 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11383 : 3011 : rtype, TREE_OPERAND (arg0, 0),
11384 : : code0 == LSHIFT_EXPR
11385 : : ? orig_tree01 : orig_tree11);
11386 : 3011 : return fold_convert_loc (loc, type, tem);
11387 : : }
11388 : 23704 : else if (code11 == MINUS_EXPR)
11389 : : {
11390 : 941 : tree tree110, tree111;
11391 : 941 : tree110 = TREE_OPERAND (tree11, 0);
11392 : 941 : tree111 = TREE_OPERAND (tree11, 1);
11393 : 941 : STRIP_NOPS (tree110);
11394 : 941 : STRIP_NOPS (tree111);
11395 : 941 : if (TREE_CODE (tree110) == INTEGER_CST
11396 : 930 : && compare_tree_int (tree110,
11397 : 930 : element_precision (rtype)) == 0
11398 : 1855 : && operand_equal_p (tree01, tree111, 0))
11399 : : {
11400 : 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11401 : : ? LROTATE_EXPR : RROTATE_EXPR),
11402 : 558 : rtype, TREE_OPERAND (arg0, 0),
11403 : : orig_tree01);
11404 : 558 : return fold_convert_loc (loc, type, tem);
11405 : : }
11406 : : }
11407 : 22763 : else if (code == BIT_IOR_EXPR
11408 : 21649 : && code11 == BIT_AND_EXPR
11409 : 44337 : && pow2p_hwi (element_precision (rtype)))
11410 : : {
11411 : 21574 : tree tree110, tree111;
11412 : 21574 : tree110 = TREE_OPERAND (tree11, 0);
11413 : 21574 : tree111 = TREE_OPERAND (tree11, 1);
11414 : 21574 : STRIP_NOPS (tree110);
11415 : 21574 : STRIP_NOPS (tree111);
11416 : 21574 : if (TREE_CODE (tree110) == NEGATE_EXPR
11417 : 21087 : && TREE_CODE (tree111) == INTEGER_CST
11418 : 21087 : && compare_tree_int (tree111,
11419 : 21087 : element_precision (rtype) - 1) == 0
11420 : 42647 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11421 : : {
11422 : 31457 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11423 : : ? LROTATE_EXPR : RROTATE_EXPR),
11424 : 20995 : rtype, TREE_OPERAND (arg0, 0),
11425 : : orig_tree01);
11426 : 20995 : return fold_convert_loc (loc, type, tem);
11427 : : }
11428 : : }
11429 : : }
11430 : : }
11431 : :
11432 : 152771746 : associate:
11433 : : /* In most languages, can't associate operations on floats through
11434 : : parentheses. Rather than remember where the parentheses were, we
11435 : : don't associate floats at all, unless the user has specified
11436 : : -fassociative-math.
11437 : : And, we need to make sure type is not saturating. */
11438 : :
11439 : 152771746 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11440 : 110333952 : && !TYPE_SATURATING (type)
11441 : 263105698 : && !TYPE_OVERFLOW_SANITIZED (type))
11442 : : {
11443 : 110305770 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11444 : 110305770 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11445 : 110305770 : tree atype = type;
11446 : 110305770 : bool ok = true;
11447 : :
11448 : : /* Split both trees into variables, constants, and literals. Then
11449 : : associate each group together, the constants with literals,
11450 : : then the result with variables. This increases the chances of
11451 : : literals being recombined later and of generating relocatable
11452 : : expressions for the sum of a constant and literal. */
11453 : 110305770 : var0 = split_tree (arg0, type, code,
11454 : : &minus_var0, &con0, &minus_con0,
11455 : : &lit0, &minus_lit0, 0);
11456 : 110305770 : var1 = split_tree (arg1, type, code,
11457 : : &minus_var1, &con1, &minus_con1,
11458 : : &lit1, &minus_lit1, code == MINUS_EXPR);
11459 : :
11460 : : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11461 : 110305770 : if (code == MINUS_EXPR)
11462 : 11970324 : code = PLUS_EXPR;
11463 : :
11464 : : /* With undefined overflow prefer doing association in a type
11465 : : which wraps on overflow, if that is one of the operand types. */
11466 : 110305539 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11467 : 219371875 : && !TYPE_OVERFLOW_WRAPS (type))
11468 : : {
11469 : 59404256 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11470 : 58744318 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11471 : 755388 : atype = TREE_TYPE (arg0);
11472 : 57877613 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11473 : 57647024 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11474 : 220082 : atype = TREE_TYPE (arg1);
11475 : 29966531 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11476 : : }
11477 : :
11478 : : /* With undefined overflow we can only associate constants with one
11479 : : variable, and constants whose association doesn't overflow. */
11480 : 110305539 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11481 : 219371875 : && !TYPE_OVERFLOW_WRAPS (atype))
11482 : : {
11483 : 28991061 : if ((var0 && var1) || (minus_var0 && minus_var1))
11484 : : {
11485 : : /* ??? If split_tree would handle NEGATE_EXPR we could
11486 : : simply reject these cases and the allowed cases would
11487 : : be the var0/minus_var1 ones. */
11488 : 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11489 : 5403306 : tree tmp1 = var1 ? var1 : minus_var1;
11490 : 5403306 : bool one_neg = false;
11491 : :
11492 : 5403306 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11493 : : {
11494 : 1644 : tmp0 = TREE_OPERAND (tmp0, 0);
11495 : 1644 : one_neg = !one_neg;
11496 : : }
11497 : 4856850 : if (CONVERT_EXPR_P (tmp0)
11498 : 565296 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11499 : 5967475 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11500 : 564169 : <= TYPE_PRECISION (atype)))
11501 : 550330 : tmp0 = TREE_OPERAND (tmp0, 0);
11502 : 5403306 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11503 : : {
11504 : 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11505 : 170 : one_neg = !one_neg;
11506 : : }
11507 : 5087610 : if (CONVERT_EXPR_P (tmp1)
11508 : 345509 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11509 : 5748764 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11510 : 345458 : <= TYPE_PRECISION (atype)))
11511 : 335241 : tmp1 = TREE_OPERAND (tmp1, 0);
11512 : : /* The only case we can still associate with two variables
11513 : : is if they cancel out. */
11514 : 5403306 : if (!one_neg
11515 : 5403306 : || !operand_equal_p (tmp0, tmp1, 0))
11516 : : ok = false;
11517 : : }
11518 : 23184620 : else if ((var0 && minus_var1
11519 : 3882650 : && ! operand_equal_p (var0, minus_var1, 0))
11520 : 42889726 : || (minus_var0 && var1
11521 : 11321 : && ! operand_equal_p (minus_var0, var1, 0)))
11522 : : ok = false;
11523 : : }
11524 : :
11525 : : /* Only do something if we found more than two objects. Otherwise,
11526 : : nothing has changed and we risk infinite recursion. */
11527 : : if (ok
11528 : 101008565 : && ((var0 != 0) + (var1 != 0)
11529 : 101008565 : + (minus_var0 != 0) + (minus_var1 != 0)
11530 : 101008565 : + (con0 != 0) + (con1 != 0)
11531 : 101008565 : + (minus_con0 != 0) + (minus_con1 != 0)
11532 : 101008565 : + (lit0 != 0) + (lit1 != 0)
11533 : 101008565 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11534 : : {
11535 : 2054431 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11536 : 4108862 : int minus_var0_origin
11537 : 2054431 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11538 : 2054431 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11539 : 4108862 : int minus_con0_origin
11540 : 2054431 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11541 : 2054431 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11542 : 4108862 : int minus_lit0_origin
11543 : 2054431 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11544 : 2054431 : var0 = associate_trees (loc, var0, var1, code, atype);
11545 : 2054431 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11546 : : code, atype);
11547 : 2054431 : con0 = associate_trees (loc, con0, con1, code, atype);
11548 : 2054431 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11549 : : code, atype);
11550 : 2054431 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11551 : 2054431 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11552 : : code, atype);
11553 : :
11554 : 2054431 : if (minus_var0 && var0)
11555 : : {
11556 : 1339993 : var0_origin |= minus_var0_origin;
11557 : 1339993 : var0 = associate_trees (loc, var0, minus_var0,
11558 : : MINUS_EXPR, atype);
11559 : 1339993 : minus_var0 = 0;
11560 : 1339993 : minus_var0_origin = 0;
11561 : : }
11562 : 2054431 : if (minus_con0 && con0)
11563 : : {
11564 : 4194 : con0_origin |= minus_con0_origin;
11565 : 4194 : con0 = associate_trees (loc, con0, minus_con0,
11566 : : MINUS_EXPR, atype);
11567 : 4194 : minus_con0 = 0;
11568 : 4194 : minus_con0_origin = 0;
11569 : : }
11570 : :
11571 : : /* Preserve the MINUS_EXPR if the negative part of the literal is
11572 : : greater than the positive part. Otherwise, the multiplicative
11573 : : folding code (i.e extract_muldiv) may be fooled in case
11574 : : unsigned constants are subtracted, like in the following
11575 : : example: ((X*2 + 4) - 8U)/2. */
11576 : 2054431 : if (minus_lit0 && lit0)
11577 : : {
11578 : 239163 : if (TREE_CODE (lit0) == INTEGER_CST
11579 : 239163 : && TREE_CODE (minus_lit0) == INTEGER_CST
11580 : 239163 : && tree_int_cst_lt (lit0, minus_lit0)
11581 : : /* But avoid ending up with only negated parts. */
11582 : 296451 : && (var0 || con0))
11583 : : {
11584 : 52328 : minus_lit0_origin |= lit0_origin;
11585 : 52328 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11586 : : MINUS_EXPR, atype);
11587 : 52328 : lit0 = 0;
11588 : 52328 : lit0_origin = 0;
11589 : : }
11590 : : else
11591 : : {
11592 : 186835 : lit0_origin |= minus_lit0_origin;
11593 : 186835 : lit0 = associate_trees (loc, lit0, minus_lit0,
11594 : : MINUS_EXPR, atype);
11595 : 186835 : minus_lit0 = 0;
11596 : 186835 : minus_lit0_origin = 0;
11597 : : }
11598 : : }
11599 : :
11600 : : /* Don't introduce overflows through reassociation. */
11601 : 1371713 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11602 : 3426103 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11603 : 2054431 : return NULL_TREE;
11604 : :
11605 : : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11606 : 2054390 : con0_origin |= lit0_origin;
11607 : 2054390 : con0 = associate_trees (loc, con0, lit0, code, atype);
11608 : 2054390 : minus_con0_origin |= minus_lit0_origin;
11609 : 2054390 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11610 : : code, atype);
11611 : :
11612 : : /* Eliminate minus_con0. */
11613 : 2054390 : if (minus_con0)
11614 : : {
11615 : 687330 : if (con0)
11616 : : {
11617 : 15891 : con0_origin |= minus_con0_origin;
11618 : 15891 : con0 = associate_trees (loc, con0, minus_con0,
11619 : : MINUS_EXPR, atype);
11620 : : }
11621 : 671439 : else if (var0)
11622 : : {
11623 : 671439 : var0_origin |= minus_con0_origin;
11624 : 671439 : var0 = associate_trees (loc, var0, minus_con0,
11625 : : MINUS_EXPR, atype);
11626 : : }
11627 : : else
11628 : 0 : gcc_unreachable ();
11629 : : }
11630 : :
11631 : : /* Eliminate minus_var0. */
11632 : 2054390 : if (minus_var0)
11633 : : {
11634 : 348204 : if (con0)
11635 : : {
11636 : 348204 : con0_origin |= minus_var0_origin;
11637 : 348204 : con0 = associate_trees (loc, con0, minus_var0,
11638 : : MINUS_EXPR, atype);
11639 : : }
11640 : : else
11641 : 0 : gcc_unreachable ();
11642 : : }
11643 : :
11644 : : /* Reassociate only if there has been any actual association
11645 : : between subtrees from op0 and subtrees from op1 in at
11646 : : least one of the operands, otherwise we risk infinite
11647 : : recursion. See PR114084. */
11648 : 2054390 : if (var0_origin != 3 && con0_origin != 3)
11649 : : return NULL_TREE;
11650 : :
11651 : 2052715 : return
11652 : 2052715 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11653 : 2052715 : code, atype));
11654 : : }
11655 : : }
11656 : :
11657 : : return NULL_TREE;
11658 : :
11659 : 22901595 : case POINTER_DIFF_EXPR:
11660 : 22901595 : case MINUS_EXPR:
11661 : : /* Fold &a[i] - &a[j] to i-j. */
11662 : 22901595 : if (TREE_CODE (arg0) == ADDR_EXPR
11663 : 52836 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11664 : 6171 : && TREE_CODE (arg1) == ADDR_EXPR
11665 : 22902145 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11666 : : {
11667 : 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11668 : 38 : TREE_OPERAND (arg0, 0),
11669 : 38 : TREE_OPERAND (arg1, 0),
11670 : : code
11671 : : == POINTER_DIFF_EXPR);
11672 : 38 : if (tem)
11673 : : return tem;
11674 : : }
11675 : :
11676 : : /* Further transformations are not for pointers. */
11677 : 22901581 : if (code == POINTER_DIFF_EXPR)
11678 : : return NULL_TREE;
11679 : :
11680 : : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11681 : 20222268 : if (TREE_CODE (arg0) == NEGATE_EXPR
11682 : 149018 : && negate_expr_p (op1)
11683 : : /* If arg0 is e.g. unsigned int and type is int, then this could
11684 : : introduce UB, because if A is INT_MIN at runtime, the original
11685 : : expression can be well defined while the latter is not.
11686 : : See PR83269. */
11687 : 20223087 : && !(ANY_INTEGRAL_TYPE_P (type)
11688 : 819 : && TYPE_OVERFLOW_UNDEFINED (type)
11689 : 807 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11690 : 807 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11691 : 812 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11692 : : fold_convert_loc (loc, type,
11693 : 1624 : TREE_OPERAND (arg0, 0)));
11694 : :
11695 : : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11696 : : __complex__ ( x, -y ). This is not the same for SNaNs or if
11697 : : signed zeros are involved. */
11698 : 20221456 : if (!HONOR_SNANS (arg0)
11699 : 20220305 : && !HONOR_SIGNED_ZEROS (arg0)
11700 : 32989430 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11701 : : {
11702 : 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11703 : 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11704 : 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11705 : 53 : bool arg0rz = false, arg0iz = false;
11706 : 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11707 : 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11708 : : {
11709 : 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11710 : 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11711 : 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11712 : : {
11713 : 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11714 : : arg1r ? arg1r
11715 : 0 : : build1 (REALPART_EXPR, rtype, arg1));
11716 : 9 : tree ip = arg0i ? arg0i
11717 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11718 : 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11719 : : }
11720 : 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11721 : : {
11722 : 15 : tree rp = arg0r ? arg0r
11723 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11724 : 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11725 : : arg1i ? arg1i
11726 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11727 : 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11728 : : }
11729 : : }
11730 : : }
11731 : :
11732 : : /* A - B -> A + (-B) if B is easily negatable. */
11733 : 20221432 : if (negate_expr_p (op1)
11734 : 741971 : && ! TYPE_OVERFLOW_SANITIZED (type)
11735 : 20960910 : && ((FLOAT_TYPE_P (type)
11736 : : /* Avoid this transformation if B is a positive REAL_CST. */
11737 : 65 : && (TREE_CODE (op1) != REAL_CST
11738 : 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11739 : 739413 : || INTEGRAL_TYPE_P (type)))
11740 : 739293 : return fold_build2_loc (loc, PLUS_EXPR, type,
11741 : : fold_convert_loc (loc, type, arg0),
11742 : 739293 : negate_expr (op1));
11743 : :
11744 : : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11745 : : one. Make sure the type is not saturating and has the signedness of
11746 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11747 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11748 : 19482139 : if ((TREE_CODE (arg0) == MULT_EXPR
11749 : 18149718 : || TREE_CODE (arg1) == MULT_EXPR)
11750 : 2723789 : && !TYPE_SATURATING (type)
11751 : 2723789 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11752 : 2590449 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11753 : 22017422 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11754 : : {
11755 : 358535 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11756 : 358535 : if (tem)
11757 : : return tem;
11758 : : }
11759 : :
11760 : 19431415 : goto associate;
11761 : :
11762 : 65313601 : case MULT_EXPR:
11763 : 65313601 : if (! FLOAT_TYPE_P (type))
11764 : : {
11765 : : /* Transform x * -C into -x * C if x is easily negatable. */
11766 : 42247966 : if (TREE_CODE (op1) == INTEGER_CST
11767 : 39440499 : && tree_int_cst_sgn (op1) == -1
11768 : 214632 : && negate_expr_p (op0)
11769 : 336 : && negate_expr_p (op1)
11770 : 320 : && (tem = negate_expr (op1)) != op1
11771 : 42248286 : && ! TREE_OVERFLOW (tem))
11772 : 320 : return fold_build2_loc (loc, MULT_EXPR, type,
11773 : : fold_convert_loc (loc, type,
11774 : 320 : negate_expr (op0)), tem);
11775 : :
11776 : 42247646 : strict_overflow_p = false;
11777 : 42247646 : if (TREE_CODE (arg1) == INTEGER_CST
11778 : 42247646 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11779 : : &strict_overflow_p)) != 0)
11780 : : {
11781 : 516164 : if (strict_overflow_p)
11782 : 10 : fold_overflow_warning (("assuming signed overflow does not "
11783 : : "occur when simplifying "
11784 : : "multiplication"),
11785 : : WARN_STRICT_OVERFLOW_MISC);
11786 : 516164 : return fold_convert_loc (loc, type, tem);
11787 : : }
11788 : :
11789 : : /* Optimize z * conj(z) for integer complex numbers. */
11790 : 41731482 : if (TREE_CODE (arg0) == CONJ_EXPR
11791 : 41731482 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11792 : 1 : return fold_mult_zconjz (loc, type, arg1);
11793 : 41731481 : if (TREE_CODE (arg1) == CONJ_EXPR
11794 : 41731481 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11795 : 0 : return fold_mult_zconjz (loc, type, arg0);
11796 : : }
11797 : : else
11798 : : {
11799 : : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11800 : : This is not the same for NaNs or if signed zeros are
11801 : : involved. */
11802 : 23065635 : if (!HONOR_NANS (arg0)
11803 : 32788 : && !HONOR_SIGNED_ZEROS (arg0)
11804 : 32488 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11805 : 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11806 : 23065860 : && real_zerop (TREE_REALPART (arg1)))
11807 : : {
11808 : 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11809 : 218 : if (real_onep (TREE_IMAGPART (arg1)))
11810 : : {
11811 : 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11812 : 63 : arg0 = save_expr (arg0);
11813 : 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11814 : : rtype, arg0);
11815 : 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11816 : : rtype, arg0);
11817 : 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11818 : : negate_expr (iarg0),
11819 : 208 : rarg0);
11820 : : }
11821 : 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11822 : : {
11823 : 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11824 : 0 : arg0 = save_expr (arg0);
11825 : 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11826 : : rtype, arg0);
11827 : 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11828 : : rtype, arg0);
11829 : 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11830 : : iarg0,
11831 : 10 : negate_expr (rarg0));
11832 : : }
11833 : : }
11834 : :
11835 : : /* Optimize z * conj(z) for floating point complex numbers.
11836 : : Guarded by flag_unsafe_math_optimizations as non-finite
11837 : : imaginary components don't produce scalar results. */
11838 : 23065417 : if (flag_unsafe_math_optimizations
11839 : 32317 : && TREE_CODE (arg0) == CONJ_EXPR
11840 : 23065419 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11841 : 1 : return fold_mult_zconjz (loc, type, arg1);
11842 : 23065416 : if (flag_unsafe_math_optimizations
11843 : 32316 : && TREE_CODE (arg1) == CONJ_EXPR
11844 : 23065420 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11845 : 0 : return fold_mult_zconjz (loc, type, arg0);
11846 : : }
11847 : 64796897 : goto associate;
11848 : :
11849 : 1863538 : case BIT_IOR_EXPR:
11850 : : /* Canonicalize (X & C1) | C2. */
11851 : 1863538 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11852 : 138949 : && TREE_CODE (arg1) == INTEGER_CST
11853 : 1956336 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11854 : : {
11855 : 92790 : int width = TYPE_PRECISION (type), w;
11856 : 92790 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11857 : 92790 : wide_int c2 = wi::to_wide (arg1);
11858 : :
11859 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11860 : 92790 : if ((c1 & c2) == c1)
11861 : 0 : return omit_one_operand_loc (loc, type, arg1,
11862 : 0 : TREE_OPERAND (arg0, 0));
11863 : :
11864 : 92790 : wide_int msk = wi::mask (width, false,
11865 : 92790 : TYPE_PRECISION (TREE_TYPE (arg1)));
11866 : :
11867 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11868 : 92790 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11869 : : {
11870 : 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11871 : 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11872 : : }
11873 : :
11874 : : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11875 : : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11876 : : mode which allows further optimizations. */
11877 : 92784 : c1 &= msk;
11878 : 92784 : c2 &= msk;
11879 : 92784 : wide_int c3 = wi::bit_and_not (c1, c2);
11880 : 286826 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11881 : : {
11882 : 194284 : wide_int mask = wi::mask (w, false,
11883 : 194284 : TYPE_PRECISION (type));
11884 : 388568 : if (((c1 | c2) & mask) == mask
11885 : 388568 : && wi::bit_and_not (c1, mask) == 0)
11886 : : {
11887 : 242 : c3 = mask;
11888 : 242 : break;
11889 : : }
11890 : 194284 : }
11891 : :
11892 : 92784 : if (c3 != c1)
11893 : : {
11894 : 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11895 : 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11896 : 558 : wide_int_to_tree (type, c3));
11897 : 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11898 : : }
11899 : 93912 : }
11900 : :
11901 : : /* See if this can be simplified into a rotate first. If that
11902 : : is unsuccessful continue in the association code. */
11903 : 1862974 : goto bit_rotate;
11904 : :
11905 : 722185 : case BIT_XOR_EXPR:
11906 : : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11907 : 722185 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11908 : 2420 : && INTEGRAL_TYPE_P (type)
11909 : 1815 : && integer_onep (TREE_OPERAND (arg0, 1))
11910 : 722188 : && integer_onep (arg1))
11911 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11912 : 0 : build_zero_cst (TREE_TYPE (arg0)));
11913 : :
11914 : : /* See if this can be simplified into a rotate first. If that
11915 : : is unsuccessful continue in the association code. */
11916 : 722185 : goto bit_rotate;
11917 : :
11918 : 6232489 : case BIT_AND_EXPR:
11919 : : /* Fold !X & 1 as X == 0. */
11920 : 6232489 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11921 : 6232489 : && integer_onep (arg1))
11922 : : {
11923 : 0 : tem = TREE_OPERAND (arg0, 0);
11924 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11925 : 0 : build_zero_cst (TREE_TYPE (tem)));
11926 : : }
11927 : :
11928 : : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11929 : : multiple of 1 << CST. */
11930 : 6232489 : if (TREE_CODE (arg1) == INTEGER_CST)
11931 : : {
11932 : 4555667 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11933 : 4555667 : wide_int ncst1 = -cst1;
11934 : 4555667 : if ((cst1 & ncst1) == ncst1
11935 : 4727472 : && multiple_of_p (type, arg0,
11936 : 4727472 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11937 : 467 : return fold_convert_loc (loc, type, arg0);
11938 : 4555667 : }
11939 : :
11940 : : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11941 : : bits from CST2. */
11942 : 6232022 : if (TREE_CODE (arg1) == INTEGER_CST
11943 : 4555200 : && TREE_CODE (arg0) == MULT_EXPR
11944 : 6339391 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11945 : : {
11946 : 107327 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11947 : 107327 : wide_int masked
11948 : 107327 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11949 : :
11950 : 107327 : if (masked == 0)
11951 : 5087 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11952 : 5087 : arg0, arg1);
11953 : 102240 : else if (masked != warg1)
11954 : : {
11955 : : /* Avoid the transform if arg1 is a mask of some
11956 : : mode which allows further optimizations. */
11957 : 662 : int pop = wi::popcount (warg1);
11958 : 695 : if (!(pop >= BITS_PER_UNIT
11959 : 59 : && pow2p_hwi (pop)
11960 : 728 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11961 : 1258 : return fold_build2_loc (loc, code, type, op0,
11962 : 1258 : wide_int_to_tree (type, masked));
11963 : : }
11964 : 107327 : }
11965 : :
11966 : : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11967 : 4549484 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11968 : 6518610 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11969 : : {
11970 : 199889 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11971 : :
11972 : 199889 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11973 : 199889 : if (mask == -1)
11974 : 2589 : return
11975 : 2589 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11976 : 199889 : }
11977 : :
11978 : 6223717 : goto associate;
11979 : :
11980 : 6402336 : case RDIV_EXPR:
11981 : : /* Don't touch a floating-point divide by zero unless the mode
11982 : : of the constant can represent infinity. */
11983 : 6402336 : if (TREE_CODE (arg1) == REAL_CST
11984 : 3225513 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11985 : 6402336 : && real_zerop (arg1))
11986 : 0 : return NULL_TREE;
11987 : :
11988 : : /* (-A) / (-B) -> A / B */
11989 : 6402336 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11990 : 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11991 : 3 : TREE_OPERAND (arg0, 0),
11992 : 3 : negate_expr (arg1));
11993 : 6402333 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11994 : 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11995 : : negate_expr (arg0),
11996 : 0 : TREE_OPERAND (arg1, 0));
11997 : : return NULL_TREE;
11998 : :
11999 : 2212492 : case TRUNC_DIV_EXPR:
12000 : : /* Fall through */
12001 : :
12002 : 2212492 : case FLOOR_DIV_EXPR:
12003 : : /* Simplify A / (B << N) where A and B are positive and B is
12004 : : a power of 2, to A >> (N + log2(B)). */
12005 : 2212492 : strict_overflow_p = false;
12006 : 2212492 : if (TREE_CODE (arg1) == LSHIFT_EXPR
12007 : 2212492 : && (TYPE_UNSIGNED (type)
12008 : 8 : || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12009 : : {
12010 : 17 : tree sval = TREE_OPERAND (arg1, 0);
12011 : 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12012 : : {
12013 : 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
12014 : 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
12015 : 16 : wi::exact_log2 (wi::to_wide (sval)));
12016 : :
12017 : 16 : if (strict_overflow_p)
12018 : 0 : fold_overflow_warning (("assuming signed overflow does not "
12019 : : "occur when simplifying A / (B << N)"),
12020 : : WARN_STRICT_OVERFLOW_MISC);
12021 : :
12022 : 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12023 : : sh_cnt, pow2);
12024 : 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
12025 : 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
12026 : : }
12027 : : }
12028 : :
12029 : : /* Fall through */
12030 : :
12031 : 3457885 : case ROUND_DIV_EXPR:
12032 : 3457885 : case CEIL_DIV_EXPR:
12033 : 3457885 : case EXACT_DIV_EXPR:
12034 : 3457885 : if (integer_zerop (arg1))
12035 : : return NULL_TREE;
12036 : :
12037 : : /* Convert -A / -B to A / B when the type is signed and overflow is
12038 : : undefined. */
12039 : 3454863 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12040 : 849930 : && TREE_CODE (op0) == NEGATE_EXPR
12041 : 3454925 : && negate_expr_p (op1))
12042 : : {
12043 : 30 : if (ANY_INTEGRAL_TYPE_P (type))
12044 : 30 : fold_overflow_warning (("assuming signed overflow does not occur "
12045 : : "when distributing negation across "
12046 : : "division"),
12047 : : WARN_STRICT_OVERFLOW_MISC);
12048 : 60 : return fold_build2_loc (loc, code, type,
12049 : : fold_convert_loc (loc, type,
12050 : 30 : TREE_OPERAND (arg0, 0)),
12051 : 30 : negate_expr (op1));
12052 : : }
12053 : 3454833 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12054 : 849900 : && TREE_CODE (arg1) == NEGATE_EXPR
12055 : 3455077 : && negate_expr_p (op0))
12056 : : {
12057 : 36 : if (ANY_INTEGRAL_TYPE_P (type))
12058 : 36 : fold_overflow_warning (("assuming signed overflow does not occur "
12059 : : "when distributing negation across "
12060 : : "division"),
12061 : : WARN_STRICT_OVERFLOW_MISC);
12062 : 36 : return fold_build2_loc (loc, code, type,
12063 : : negate_expr (op0),
12064 : : fold_convert_loc (loc, type,
12065 : 72 : TREE_OPERAND (arg1, 0)));
12066 : : }
12067 : :
12068 : : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12069 : : operation, EXACT_DIV_EXPR.
12070 : :
12071 : : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12072 : : At one time others generated faster code, it's not clear if they do
12073 : : after the last round to changes to the DIV code in expmed.cc. */
12074 : 3454797 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12075 : 3454797 : && multiple_of_p (type, arg0, arg1))
12076 : 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12077 : : fold_convert (type, arg0),
12078 : 0 : fold_convert (type, arg1));
12079 : :
12080 : 3454797 : strict_overflow_p = false;
12081 : 3454797 : if (TREE_CODE (arg1) == INTEGER_CST
12082 : 3454797 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12083 : : &strict_overflow_p)) != 0)
12084 : : {
12085 : 2774 : if (strict_overflow_p)
12086 : 157 : fold_overflow_warning (("assuming signed overflow does not occur "
12087 : : "when simplifying division"),
12088 : : WARN_STRICT_OVERFLOW_MISC);
12089 : 2774 : return fold_convert_loc (loc, type, tem);
12090 : : }
12091 : :
12092 : : return NULL_TREE;
12093 : :
12094 : 648517 : case CEIL_MOD_EXPR:
12095 : 648517 : case FLOOR_MOD_EXPR:
12096 : 648517 : case ROUND_MOD_EXPR:
12097 : 648517 : case TRUNC_MOD_EXPR:
12098 : 648517 : strict_overflow_p = false;
12099 : 648517 : if (TREE_CODE (arg1) == INTEGER_CST
12100 : 648517 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12101 : : &strict_overflow_p)) != 0)
12102 : : {
12103 : 0 : if (strict_overflow_p)
12104 : 0 : fold_overflow_warning (("assuming signed overflow does not occur "
12105 : : "when simplifying modulus"),
12106 : : WARN_STRICT_OVERFLOW_MISC);
12107 : 0 : return fold_convert_loc (loc, type, tem);
12108 : : }
12109 : :
12110 : : return NULL_TREE;
12111 : :
12112 : 2134535 : case LROTATE_EXPR:
12113 : 2134535 : case RROTATE_EXPR:
12114 : 2134535 : case RSHIFT_EXPR:
12115 : 2134535 : case LSHIFT_EXPR:
12116 : : /* Since negative shift count is not well-defined,
12117 : : don't try to compute it in the compiler. */
12118 : 2134535 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12119 : : return NULL_TREE;
12120 : :
12121 : 2133482 : prec = element_precision (type);
12122 : :
12123 : : /* If we have a rotate of a bit operation with the rotate count and
12124 : : the second operand of the bit operation both constant,
12125 : : permute the two operations. */
12126 : 2701 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12127 : 2167 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12128 : 2167 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12129 : 2167 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12130 : 2133482 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12131 : : {
12132 : 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12133 : 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12134 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12135 : : fold_build2_loc (loc, code, type,
12136 : : arg00, arg1),
12137 : : fold_build2_loc (loc, code, type,
12138 : 0 : arg01, arg1));
12139 : : }
12140 : :
12141 : : return NULL_TREE;
12142 : :
12143 : 445153 : case MIN_EXPR:
12144 : 445153 : case MAX_EXPR:
12145 : 445153 : goto associate;
12146 : :
12147 : 5921304 : case TRUTH_ANDIF_EXPR:
12148 : : /* Note that the operands of this must be ints
12149 : : and their values must be 0 or 1.
12150 : : ("true" is a fixed value perhaps depending on the language.) */
12151 : : /* If first arg is constant zero, return it. */
12152 : 5921304 : if (integer_zerop (arg0))
12153 : 1216893 : return fold_convert_loc (loc, type, arg0);
12154 : : /* FALLTHRU */
12155 : 15385108 : case TRUTH_AND_EXPR:
12156 : : /* If either arg is constant true, drop it. */
12157 : 15385108 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12158 : 2006474 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12159 : 843648 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12160 : : /* Preserve sequence points. */
12161 : 14175072 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12162 : 767790 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12163 : : /* If second arg is constant zero, result is zero, but first arg
12164 : : must be evaluated. */
12165 : 12610844 : if (integer_zerop (arg1))
12166 : 47210 : return omit_one_operand_loc (loc, type, arg1, arg0);
12167 : : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12168 : : case will be handled here. */
12169 : 12563634 : if (integer_zerop (arg0))
12170 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12171 : :
12172 : : /* !X && X is always false. */
12173 : 12563634 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12174 : 12563634 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12175 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12176 : : /* X && !X is always false. */
12177 : 12563634 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12178 : 12563634 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12179 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12180 : :
12181 : : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12182 : : means A >= Y && A != MAX, but in this case we know that
12183 : : A < X <= MAX. */
12184 : :
12185 : 12563634 : if (!TREE_SIDE_EFFECTS (arg0)
12186 : 12563634 : && !TREE_SIDE_EFFECTS (arg1))
12187 : : {
12188 : 11517172 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12189 : 11517172 : if (tem && !operand_equal_p (tem, arg0, 0))
12190 : 433 : return fold_convert (type,
12191 : : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12192 : : tem, arg1));
12193 : :
12194 : 11516739 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12195 : 11516739 : if (tem && !operand_equal_p (tem, arg1, 0))
12196 : 10094 : return fold_convert (type,
12197 : : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12198 : : arg0, tem));
12199 : : }
12200 : :
12201 : 12553107 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12202 : : != NULL_TREE)
12203 : : return tem;
12204 : :
12205 : : return NULL_TREE;
12206 : :
12207 : 3200116 : case TRUTH_ORIF_EXPR:
12208 : : /* Note that the operands of this must be ints
12209 : : and their values must be 0 or true.
12210 : : ("true" is a fixed value perhaps depending on the language.) */
12211 : : /* If first arg is constant true, return it. */
12212 : 3200116 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12213 : 132782 : return fold_convert_loc (loc, type, arg0);
12214 : : /* FALLTHRU */
12215 : 12433147 : case TRUTH_OR_EXPR:
12216 : : /* If either arg is constant zero, drop it. */
12217 : 12433147 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12218 : 183563 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12219 : 487616 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12220 : : /* Preserve sequence points. */
12221 : 12685783 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12222 : 425067 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12223 : : /* If second arg is constant true, result is true, but we must
12224 : : evaluate first arg. */
12225 : 11824517 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12226 : 51417 : return omit_one_operand_loc (loc, type, arg1, arg0);
12227 : : /* Likewise for first arg, but note this only occurs here for
12228 : : TRUTH_OR_EXPR. */
12229 : 11773100 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12230 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12231 : :
12232 : : /* !X || X is always true. */
12233 : 11773100 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12234 : 11773100 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12235 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12236 : : /* X || !X is always true. */
12237 : 11773100 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12238 : 11773100 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12239 : 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12240 : :
12241 : : /* (X && !Y) || (!X && Y) is X ^ Y */
12242 : 11773099 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12243 : 1628 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12244 : : {
12245 : 681 : tree a0, a1, l0, l1, n0, n1;
12246 : :
12247 : 681 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12248 : 681 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12249 : :
12250 : 681 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12251 : 681 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12252 : :
12253 : 681 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12254 : 681 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12255 : :
12256 : 681 : if ((operand_equal_p (n0, a0, 0)
12257 : 18 : && operand_equal_p (n1, a1, 0))
12258 : 689 : || (operand_equal_p (n0, a1, 0)
12259 : 3 : && operand_equal_p (n1, a0, 0)))
12260 : 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12261 : : }
12262 : :
12263 : 11773086 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12264 : : != NULL_TREE)
12265 : : return tem;
12266 : :
12267 : : return NULL_TREE;
12268 : :
12269 : 41980 : case TRUTH_XOR_EXPR:
12270 : : /* If the second arg is constant zero, drop it. */
12271 : 41980 : if (integer_zerop (arg1))
12272 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12273 : : /* If the second arg is constant true, this is a logical inversion. */
12274 : 41980 : if (integer_onep (arg1))
12275 : : {
12276 : 0 : tem = invert_truthvalue_loc (loc, arg0);
12277 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12278 : : }
12279 : : /* Identical arguments cancel to zero. */
12280 : 41980 : if (operand_equal_p (arg0, arg1, 0))
12281 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12282 : :
12283 : : /* !X ^ X is always true. */
12284 : 41980 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12285 : 41980 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12286 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12287 : :
12288 : : /* X ^ !X is always true. */
12289 : 41980 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12290 : 41980 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12291 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12292 : :
12293 : : return NULL_TREE;
12294 : :
12295 : 47155088 : case EQ_EXPR:
12296 : 47155088 : case NE_EXPR:
12297 : 47155088 : STRIP_NOPS (arg0);
12298 : 47155088 : STRIP_NOPS (arg1);
12299 : :
12300 : 47155088 : tem = fold_comparison (loc, code, type, op0, op1);
12301 : 47155088 : if (tem != NULL_TREE)
12302 : : return tem;
12303 : :
12304 : : /* bool_var != 1 becomes !bool_var. */
12305 : 48281874 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12306 : 47190882 : && code == NE_EXPR)
12307 : 39766 : return fold_convert_loc (loc, type,
12308 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12309 : 79532 : TREE_TYPE (arg0), arg0));
12310 : :
12311 : : /* bool_var == 0 becomes !bool_var. */
12312 : 48202342 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12313 : 48023286 : && code == EQ_EXPR)
12314 : 206699 : return fold_convert_loc (loc, type,
12315 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12316 : 413398 : TREE_TYPE (arg0), arg0));
12317 : :
12318 : : /* !exp != 0 becomes !exp */
12319 : 609663 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12320 : 47508429 : && code == NE_EXPR)
12321 : 601965 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12322 : :
12323 : : /* If this is an EQ or NE comparison with zero and ARG0 is
12324 : : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12325 : : two operations, but the latter can be done in one less insn
12326 : : on machines that have only two-operand insns or on which a
12327 : : constant cannot be the first operand. */
12328 : 46297516 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12329 : 46297516 : && integer_zerop (arg1))
12330 : : {
12331 : 1507171 : tree arg00 = TREE_OPERAND (arg0, 0);
12332 : 1507171 : tree arg01 = TREE_OPERAND (arg0, 1);
12333 : 1507171 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12334 : 1507171 : && integer_onep (TREE_OPERAND (arg00, 0)))
12335 : : {
12336 : 4599 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12337 : 4599 : arg01, TREE_OPERAND (arg00, 1));
12338 : 4599 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12339 : 4599 : build_one_cst (TREE_TYPE (arg0)));
12340 : 4599 : return fold_build2_loc (loc, code, type,
12341 : 4599 : fold_convert_loc (loc, TREE_TYPE (arg1),
12342 : 4599 : tem), arg1);
12343 : : }
12344 : 1502572 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12345 : 1502572 : && integer_onep (TREE_OPERAND (arg01, 0)))
12346 : : {
12347 : 305 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12348 : 305 : arg00, TREE_OPERAND (arg01, 1));
12349 : 305 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12350 : 305 : build_one_cst (TREE_TYPE (arg0)));
12351 : 305 : return fold_build2_loc (loc, code, type,
12352 : 305 : fold_convert_loc (loc, TREE_TYPE (arg1),
12353 : 305 : tem), arg1);
12354 : : }
12355 : : }
12356 : :
12357 : : /* If this is a comparison of a field, we may be able to simplify it. */
12358 : 46292612 : if ((TREE_CODE (arg0) == COMPONENT_REF
12359 : 46292612 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12360 : : /* Handle the constant case even without -O
12361 : : to make sure the warnings are given. */
12362 : 4618017 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12363 : : {
12364 : 4327076 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12365 : 4327076 : if (t1)
12366 : : return t1;
12367 : : }
12368 : :
12369 : : /* Optimize comparisons of strlen vs zero to a compare of the
12370 : : first character of the string vs zero. To wit,
12371 : : strlen(ptr) == 0 => *ptr == 0
12372 : : strlen(ptr) != 0 => *ptr != 0
12373 : : Other cases should reduce to one of these two (or a constant)
12374 : : due to the return value of strlen being unsigned. */
12375 : 45428025 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12376 : : {
12377 : 2616765 : tree fndecl = get_callee_fndecl (arg0);
12378 : :
12379 : 2616765 : if (fndecl
12380 : 2615871 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12381 : 537 : && call_expr_nargs (arg0) == 1
12382 : 2617302 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12383 : : == POINTER_TYPE))
12384 : : {
12385 : 537 : tree ptrtype
12386 : 537 : = build_pointer_type (build_qualified_type (char_type_node,
12387 : : TYPE_QUAL_CONST));
12388 : 1074 : tree ptr = fold_convert_loc (loc, ptrtype,
12389 : 537 : CALL_EXPR_ARG (arg0, 0));
12390 : 537 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12391 : 537 : return fold_build2_loc (loc, code, type, iref,
12392 : 537 : build_int_cst (TREE_TYPE (iref), 0));
12393 : : }
12394 : : }
12395 : :
12396 : : /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12397 : : of X. Similarly fold (X >> C) == 0 into X >= 0. */
12398 : 45427488 : if (TREE_CODE (arg0) == RSHIFT_EXPR
12399 : 38801 : && integer_zerop (arg1)
12400 : 45439508 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12401 : : {
12402 : 9948 : tree arg00 = TREE_OPERAND (arg0, 0);
12403 : 9948 : tree arg01 = TREE_OPERAND (arg0, 1);
12404 : 9948 : tree itype = TREE_TYPE (arg00);
12405 : 9948 : if (wi::to_wide (arg01) == element_precision (itype) - 1)
12406 : : {
12407 : 987 : if (TYPE_UNSIGNED (itype))
12408 : : {
12409 : 906 : itype = signed_type_for (itype);
12410 : 906 : arg00 = fold_convert_loc (loc, itype, arg00);
12411 : : }
12412 : 1920 : return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12413 : 987 : type, arg00, build_zero_cst (itype));
12414 : : }
12415 : : }
12416 : :
12417 : : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12418 : : (X & C) == 0 when C is a single bit. */
12419 : 45426501 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12420 : 1713311 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12421 : 966 : && integer_zerop (arg1)
12422 : 45427069 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12423 : : {
12424 : 253 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12425 : 253 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12426 : 253 : TREE_OPERAND (arg0, 1));
12427 : 393 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12428 : : type, tem,
12429 : 253 : fold_convert_loc (loc, TREE_TYPE (arg0),
12430 : 253 : arg1));
12431 : : }
12432 : :
12433 : : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12434 : : constant C is a power of two, i.e. a single bit. */
12435 : 45426248 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12436 : 4646 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12437 : 0 : && integer_zerop (arg1)
12438 : 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12439 : 45426248 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12440 : 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12441 : : {
12442 : 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12443 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12444 : 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12445 : : }
12446 : :
12447 : : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12448 : : when is C is a power of two, i.e. a single bit. */
12449 : 45426248 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12450 : 1713058 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12451 : 15612 : && integer_zerop (arg1)
12452 : 15612 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12453 : 45439205 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12454 : 12957 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12455 : : {
12456 : 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12457 : 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12458 : 0 : arg000, TREE_OPERAND (arg0, 1));
12459 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12460 : 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12461 : : }
12462 : :
12463 : 45426248 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12464 : 4646 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12465 : : {
12466 : 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12467 : 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12468 : 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12469 : 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12470 : 482 : tree itype = TREE_TYPE (arg0);
12471 : :
12472 : : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12473 : : operand_equal_p guarantees no side-effects so we don't need
12474 : : to use omit_one_operand on Z. */
12475 : 482 : if (operand_equal_p (arg01, arg11, 0))
12476 : 8 : return fold_build2_loc (loc, code, type, arg00,
12477 : 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12478 : 8 : arg10));
12479 : 474 : if (operand_equal_p (arg01, arg10, 0))
12480 : 0 : return fold_build2_loc (loc, code, type, arg00,
12481 : 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12482 : 0 : arg11));
12483 : 474 : if (operand_equal_p (arg00, arg11, 0))
12484 : 0 : return fold_build2_loc (loc, code, type, arg01,
12485 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12486 : 0 : arg10));
12487 : 474 : if (operand_equal_p (arg00, arg10, 0))
12488 : 0 : return fold_build2_loc (loc, code, type, arg01,
12489 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12490 : 0 : arg11));
12491 : :
12492 : : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12493 : 474 : if (TREE_CODE (arg01) == INTEGER_CST
12494 : 8 : && TREE_CODE (arg11) == INTEGER_CST)
12495 : : {
12496 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12497 : : fold_convert_loc (loc, itype, arg11));
12498 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12499 : 8 : return fold_build2_loc (loc, code, type, tem,
12500 : 8 : fold_convert_loc (loc, itype, arg10));
12501 : : }
12502 : : }
12503 : :
12504 : : /* Attempt to simplify equality/inequality comparisons of complex
12505 : : values. Only lower the comparison if the result is known or
12506 : : can be simplified to a single scalar comparison. */
12507 : 45426232 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12508 : 45423705 : || TREE_CODE (arg0) == COMPLEX_CST)
12509 : 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12510 : 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12511 : : {
12512 : 1726 : tree real0, imag0, real1, imag1;
12513 : 1726 : tree rcond, icond;
12514 : :
12515 : 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12516 : : {
12517 : 1726 : real0 = TREE_OPERAND (arg0, 0);
12518 : 1726 : imag0 = TREE_OPERAND (arg0, 1);
12519 : : }
12520 : : else
12521 : : {
12522 : 0 : real0 = TREE_REALPART (arg0);
12523 : 0 : imag0 = TREE_IMAGPART (arg0);
12524 : : }
12525 : :
12526 : 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12527 : : {
12528 : 192 : real1 = TREE_OPERAND (arg1, 0);
12529 : 192 : imag1 = TREE_OPERAND (arg1, 1);
12530 : : }
12531 : : else
12532 : : {
12533 : 1534 : real1 = TREE_REALPART (arg1);
12534 : 1534 : imag1 = TREE_IMAGPART (arg1);
12535 : : }
12536 : :
12537 : 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12538 : 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12539 : : {
12540 : 11 : if (integer_zerop (rcond))
12541 : : {
12542 : 11 : if (code == EQ_EXPR)
12543 : 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12544 : 0 : imag0, imag1);
12545 : 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12546 : : }
12547 : : else
12548 : : {
12549 : 0 : if (code == NE_EXPR)
12550 : 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12551 : 0 : imag0, imag1);
12552 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12553 : : }
12554 : : }
12555 : :
12556 : 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12557 : 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12558 : : {
12559 : 9 : if (integer_zerop (icond))
12560 : : {
12561 : 7 : if (code == EQ_EXPR)
12562 : 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12563 : 1 : real0, real1);
12564 : 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12565 : : }
12566 : : else
12567 : : {
12568 : 2 : if (code == NE_EXPR)
12569 : 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12570 : 1 : real0, real1);
12571 : 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12572 : : }
12573 : : }
12574 : : }
12575 : :
12576 : : return NULL_TREE;
12577 : :
12578 : 38512989 : case LT_EXPR:
12579 : 38512989 : case GT_EXPR:
12580 : 38512989 : case LE_EXPR:
12581 : 38512989 : case GE_EXPR:
12582 : 38512989 : tem = fold_comparison (loc, code, type, op0, op1);
12583 : 38512989 : if (tem != NULL_TREE)
12584 : : return tem;
12585 : :
12586 : : /* Transform comparisons of the form X +- C CMP X. */
12587 : 37668463 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12588 : 4685286 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12589 : 50765 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12590 : 37668481 : && !HONOR_SNANS (arg0))
12591 : : {
12592 : 16 : tree arg01 = TREE_OPERAND (arg0, 1);
12593 : 16 : enum tree_code code0 = TREE_CODE (arg0);
12594 : 16 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12595 : :
12596 : : /* (X - c) > X becomes false. */
12597 : 16 : if (code == GT_EXPR
12598 : 7 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12599 : 3 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12600 : 4 : return constant_boolean_node (0, type);
12601 : :
12602 : : /* Likewise (X + c) < X becomes false. */
12603 : 12 : if (code == LT_EXPR
12604 : 2 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12605 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12606 : 2 : return constant_boolean_node (0, type);
12607 : :
12608 : : /* Convert (X - c) <= X to true. */
12609 : 10 : if (!HONOR_NANS (arg1)
12610 : 6 : && code == LE_EXPR
12611 : 14 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12612 : 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12613 : 4 : return constant_boolean_node (1, type);
12614 : :
12615 : : /* Convert (X + c) >= X to true. */
12616 : 6 : if (!HONOR_NANS (arg1)
12617 : 2 : && code == GE_EXPR
12618 : 8 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12619 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12620 : 2 : return constant_boolean_node (1, type);
12621 : : }
12622 : :
12623 : : /* If we are comparing an ABS_EXPR with a constant, we can
12624 : : convert all the cases into explicit comparisons, but they may
12625 : : well not be faster than doing the ABS and one comparison.
12626 : : But ABS (X) <= C is a range comparison, which becomes a subtraction
12627 : : and a comparison, and is probably faster. */
12628 : 37668451 : if (code == LE_EXPR
12629 : 7159436 : && TREE_CODE (arg1) == INTEGER_CST
12630 : 5068234 : && TREE_CODE (arg0) == ABS_EXPR
12631 : 871 : && ! TREE_SIDE_EFFECTS (arg0)
12632 : 871 : && (tem = negate_expr (arg1)) != 0
12633 : 871 : && TREE_CODE (tem) == INTEGER_CST
12634 : 37669322 : && !TREE_OVERFLOW (tem))
12635 : 1742 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12636 : : build2 (GE_EXPR, type,
12637 : 871 : TREE_OPERAND (arg0, 0), tem),
12638 : : build2 (LE_EXPR, type,
12639 : 1742 : TREE_OPERAND (arg0, 0), arg1));
12640 : :
12641 : : /* Convert ABS_EXPR<x> >= 0 to true. */
12642 : 37667580 : strict_overflow_p = false;
12643 : 37667580 : if (code == GE_EXPR
12644 : 3956398 : && (integer_zerop (arg1)
12645 : 3011184 : || (! HONOR_NANS (arg0)
12646 : 2373228 : && real_zerop (arg1)))
12647 : 38613035 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12648 : : {
12649 : 1154 : if (strict_overflow_p)
12650 : 6 : fold_overflow_warning (("assuming signed overflow does not occur "
12651 : : "when simplifying comparison of "
12652 : : "absolute value and zero"),
12653 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12654 : 1154 : return omit_one_operand_loc (loc, type,
12655 : : constant_boolean_node (true, type),
12656 : 1154 : arg0);
12657 : : }
12658 : :
12659 : : /* Convert ABS_EXPR<x> < 0 to false. */
12660 : 37666426 : strict_overflow_p = false;
12661 : 37666426 : if (code == LT_EXPR
12662 : 12782411 : && (integer_zerop (arg1) || real_zerop (arg1))
12663 : 40680768 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12664 : : {
12665 : 3186 : if (strict_overflow_p)
12666 : 207 : fold_overflow_warning (("assuming signed overflow does not occur "
12667 : : "when simplifying comparison of "
12668 : : "absolute value and zero"),
12669 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12670 : 3186 : return omit_one_operand_loc (loc, type,
12671 : : constant_boolean_node (false, type),
12672 : 3186 : arg0);
12673 : : }
12674 : :
12675 : : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12676 : : and similarly for >= into !=. */
12677 : 37663240 : if ((code == LT_EXPR || code == GE_EXPR)
12678 : 16734469 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12679 : 5243988 : && TREE_CODE (arg1) == LSHIFT_EXPR
12680 : 37664743 : && integer_onep (TREE_OPERAND (arg1, 0)))
12681 : 4054 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12682 : 1355 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12683 : 1355 : TREE_OPERAND (arg1, 1)),
12684 : 2710 : build_zero_cst (TREE_TYPE (arg0)));
12685 : :
12686 : : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12687 : : otherwise Y might be >= # of bits in X's type and thus e.g.
12688 : : (unsigned char) (1 << Y) for Y 15 might be 0.
12689 : : If the cast is widening, then 1 << Y should have unsigned type,
12690 : : otherwise if Y is number of bits in the signed shift type minus 1,
12691 : : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12692 : : 31 might be 0xffffffff80000000. */
12693 : 37661885 : if ((code == LT_EXPR || code == GE_EXPR)
12694 : 16733114 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12695 : 5639382 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12696 : 11116208 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12697 : 3903292 : && CONVERT_EXPR_P (arg1)
12698 : 1092745 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12699 : 42 : && (element_precision (TREE_TYPE (arg1))
12700 : 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12701 : 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12702 : 14 : || (element_precision (TREE_TYPE (arg1))
12703 : 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12704 : 37661892 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12705 : : {
12706 : 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12707 : 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12708 : 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12709 : 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12710 : 14 : build_zero_cst (TREE_TYPE (arg0)));
12711 : : }
12712 : :
12713 : : return NULL_TREE;
12714 : :
12715 : 5712457 : case UNORDERED_EXPR:
12716 : 5712457 : case ORDERED_EXPR:
12717 : 5712457 : case UNLT_EXPR:
12718 : 5712457 : case UNLE_EXPR:
12719 : 5712457 : case UNGT_EXPR:
12720 : 5712457 : case UNGE_EXPR:
12721 : 5712457 : case UNEQ_EXPR:
12722 : 5712457 : case LTGT_EXPR:
12723 : : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12724 : 5712457 : {
12725 : 5712457 : tree targ0 = strip_float_extensions (arg0);
12726 : 5712457 : tree targ1 = strip_float_extensions (arg1);
12727 : 5712457 : tree newtype = TREE_TYPE (targ0);
12728 : :
12729 : 5712457 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12730 : 1289 : newtype = TREE_TYPE (targ1);
12731 : :
12732 : 5712457 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12733 : 5712457 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12734 : 328 : return fold_build2_loc (loc, code, type,
12735 : : fold_convert_loc (loc, newtype, targ0),
12736 : 328 : fold_convert_loc (loc, newtype, targ1));
12737 : : }
12738 : :
12739 : : return NULL_TREE;
12740 : :
12741 : 6507983 : case COMPOUND_EXPR:
12742 : : /* When pedantic, a compound expression can be neither an lvalue
12743 : : nor an integer constant expression. */
12744 : 6507983 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12745 : : return NULL_TREE;
12746 : : /* Don't let (0, 0) be null pointer constant. */
12747 : 450553 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12748 : 450553 : : fold_convert_loc (loc, type, arg1);
12749 : : return tem;
12750 : :
12751 : : default:
12752 : : return NULL_TREE;
12753 : : } /* switch (code) */
12754 : : }
12755 : :
12756 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12757 : : ((A & N) + B) & M -> (A + B) & M
12758 : : Similarly if (N & M) == 0,
12759 : : ((A | N) + B) & M -> (A + B) & M
12760 : : and for - instead of + (or unary - instead of +)
12761 : : and/or ^ instead of |.
12762 : : If B is constant and (B & M) == 0, fold into A & M.
12763 : :
12764 : : This function is a helper for match.pd patterns. Return non-NULL
12765 : : type in which the simplified operation should be performed only
12766 : : if any optimization is possible.
12767 : :
12768 : : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12769 : : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12770 : : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12771 : : +/-. */
12772 : : tree
12773 : 1258271 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12774 : : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12775 : : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12776 : : tree *pmop)
12777 : : {
12778 : 1258271 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12779 : 1258271 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12780 : 1258271 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12781 : 2516542 : if (~cst1 == 0
12782 : 3768675 : || (cst1 & (cst1 + 1)) != 0
12783 : 1083545 : || !INTEGRAL_TYPE_P (type)
12784 : 1083545 : || (!TYPE_OVERFLOW_WRAPS (type)
12785 : 40090 : && TREE_CODE (type) != INTEGER_TYPE)
12786 : 4680563 : || (wi::max_value (type) & cst1) != cst1)
12787 : : return NULL_TREE;
12788 : :
12789 : 1083545 : enum tree_code codes[2] = { code00, code01 };
12790 : 1083545 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12791 : 1083545 : int which = 0;
12792 : 1083545 : wide_int cst0;
12793 : :
12794 : : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12795 : : arg1 (M) is == (1LL << cst) - 1.
12796 : : Store C into PMOP[0] and D into PMOP[1]. */
12797 : 1083545 : pmop[0] = arg00;
12798 : 1083545 : pmop[1] = arg01;
12799 : 1083545 : which = code != NEGATE_EXPR;
12800 : :
12801 : 3249739 : for (; which >= 0; which--)
12802 : 2166194 : switch (codes[which])
12803 : : {
12804 : 20679 : case BIT_AND_EXPR:
12805 : 20679 : case BIT_IOR_EXPR:
12806 : 20679 : case BIT_XOR_EXPR:
12807 : 20679 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12808 : 20679 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12809 : 20679 : if (codes[which] == BIT_AND_EXPR)
12810 : : {
12811 : 20565 : if (cst0 != cst1)
12812 : : break;
12813 : : }
12814 : 114 : else if (cst0 != 0)
12815 : : break;
12816 : : /* If C or D is of the form (A & N) where
12817 : : (N & M) == M, or of the form (A | N) or
12818 : : (A ^ N) where (N & M) == 0, replace it with A. */
12819 : 19138 : pmop[which] = arg0xx[2 * which];
12820 : 19138 : break;
12821 : 2145515 : case ERROR_MARK:
12822 : 2145515 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12823 : : break;
12824 : : /* If C or D is a N where (N & M) == 0, it can be
12825 : : omitted (replaced with 0). */
12826 : 898222 : if ((code == PLUS_EXPR
12827 : 208625 : || (code == MINUS_EXPR && which == 0))
12828 : 657736 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12829 : 143552 : pmop[which] = build_int_cst (type, 0);
12830 : : /* Similarly, with C - N where (-N & M) == 0. */
12831 : 898222 : if (code == MINUS_EXPR
12832 : 449111 : && which == 1
12833 : 650385 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12834 : 192151 : pmop[which] = build_int_cst (type, 0);
12835 : : break;
12836 : 0 : default:
12837 : 0 : gcc_unreachable ();
12838 : : }
12839 : :
12840 : : /* Only build anything new if we optimized one or both arguments above. */
12841 : 1083545 : if (pmop[0] == arg00 && pmop[1] == arg01)
12842 : : return NULL_TREE;
12843 : :
12844 : 354114 : if (TYPE_OVERFLOW_WRAPS (type))
12845 : : return type;
12846 : : else
12847 : 2452 : return unsigned_type_for (type);
12848 : 1083545 : }
12849 : :
12850 : : /* Used by contains_label_[p1]. */
12851 : :
12852 : : struct contains_label_data
12853 : : {
12854 : : hash_set<tree> *pset;
12855 : : bool inside_switch_p;
12856 : : };
12857 : :
12858 : : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12859 : : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12860 : : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12861 : :
12862 : : static tree
12863 : 4240699 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12864 : : {
12865 : 4240699 : contains_label_data *d = (contains_label_data *) data;
12866 : 4240699 : switch (TREE_CODE (*tp))
12867 : : {
12868 : : case LABEL_EXPR:
12869 : : return *tp;
12870 : :
12871 : 0 : case CASE_LABEL_EXPR:
12872 : 0 : if (!d->inside_switch_p)
12873 : : return *tp;
12874 : : return NULL_TREE;
12875 : :
12876 : 0 : case SWITCH_EXPR:
12877 : 0 : if (!d->inside_switch_p)
12878 : : {
12879 : 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12880 : 0 : return *tp;
12881 : 0 : d->inside_switch_p = true;
12882 : 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12883 : 0 : return *tp;
12884 : 0 : d->inside_switch_p = false;
12885 : 0 : *walk_subtrees = 0;
12886 : : }
12887 : : return NULL_TREE;
12888 : :
12889 : 6031 : case GOTO_EXPR:
12890 : 6031 : *walk_subtrees = 0;
12891 : 6031 : return NULL_TREE;
12892 : :
12893 : : default:
12894 : : return NULL_TREE;
12895 : : }
12896 : : }
12897 : :
12898 : : /* Return whether the sub-tree ST contains a label which is accessible from
12899 : : outside the sub-tree. */
12900 : :
12901 : : static bool
12902 : 302473 : contains_label_p (tree st)
12903 : : {
12904 : 302473 : hash_set<tree> pset;
12905 : 302473 : contains_label_data data = { &pset, false };
12906 : 302473 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12907 : 302473 : }
12908 : :
12909 : : /* Fold a ternary expression of code CODE and type TYPE with operands
12910 : : OP0, OP1, and OP2. Return the folded expression if folding is
12911 : : successful. Otherwise, return NULL_TREE. */
12912 : :
12913 : : tree
12914 : 29132782 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12915 : : tree op0, tree op1, tree op2)
12916 : : {
12917 : 29132782 : tree tem;
12918 : 29132782 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12919 : 29132782 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12920 : :
12921 : 29132782 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12922 : : && TREE_CODE_LENGTH (code) == 3);
12923 : :
12924 : : /* If this is a commutative operation, and OP0 is a constant, move it
12925 : : to OP1 to reduce the number of tests below. */
12926 : 29132782 : if (commutative_ternary_tree_code (code)
12927 : 29132782 : && tree_swap_operands_p (op0, op1))
12928 : 28 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12929 : :
12930 : 29132754 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12931 : 29132754 : if (tem)
12932 : : return tem;
12933 : :
12934 : : /* Strip any conversions that don't change the mode. This is safe
12935 : : for every expression, except for a comparison expression because
12936 : : its signedness is derived from its operands. So, in the latter
12937 : : case, only strip conversions that don't change the signedness.
12938 : :
12939 : : Note that this is done as an internal manipulation within the
12940 : : constant folder, in order to find the simplest representation of
12941 : : the arguments so that their form can be studied. In any cases,
12942 : : the appropriate type conversions should be put back in the tree
12943 : : that will get out of the constant folder. */
12944 : 28169976 : if (op0)
12945 : : {
12946 : 28105143 : arg0 = op0;
12947 : 28105143 : STRIP_NOPS (arg0);
12948 : : }
12949 : :
12950 : 28169976 : if (op1)
12951 : : {
12952 : 28169976 : arg1 = op1;
12953 : 28169976 : STRIP_NOPS (arg1);
12954 : : }
12955 : :
12956 : 28169976 : if (op2)
12957 : : {
12958 : 13218818 : arg2 = op2;
12959 : 13218818 : STRIP_NOPS (arg2);
12960 : : }
12961 : :
12962 : 28169976 : switch (code)
12963 : : {
12964 : 14950683 : case COMPONENT_REF:
12965 : 14950683 : if (TREE_CODE (arg0) == CONSTRUCTOR
12966 : 14950683 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12967 : : {
12968 : : unsigned HOST_WIDE_INT idx;
12969 : : tree field, value;
12970 : 870 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12971 : 672 : if (field == arg1)
12972 : : return value;
12973 : : }
12974 : : return NULL_TREE;
12975 : :
12976 : 11144853 : case COND_EXPR:
12977 : 11144853 : case VEC_COND_EXPR:
12978 : : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12979 : : so all simple results must be passed through pedantic_non_lvalue. */
12980 : 11144853 : if (TREE_CODE (arg0) == INTEGER_CST)
12981 : : {
12982 : 425691 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12983 : 425691 : tem = integer_zerop (arg0) ? op2 : op1;
12984 : : /* Only optimize constant conditions when the selected branch
12985 : : has the same type as the COND_EXPR. This avoids optimizing
12986 : : away "c ? x : throw", where the throw has a void type.
12987 : : Avoid throwing away that operand which contains label. */
12988 : 425691 : if ((!TREE_SIDE_EFFECTS (unused_op)
12989 : 302473 : || !contains_label_p (unused_op))
12990 : 723784 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12991 : 343886 : || VOID_TYPE_P (type)))
12992 : 412107 : return protected_set_expr_location_unshare (tem, loc);
12993 : 13584 : return NULL_TREE;
12994 : : }
12995 : 10719162 : else if (TREE_CODE (arg0) == VECTOR_CST)
12996 : : {
12997 : 9720 : unsigned HOST_WIDE_INT nelts;
12998 : 9720 : if ((TREE_CODE (arg1) == VECTOR_CST
12999 : 7499 : || TREE_CODE (arg1) == CONSTRUCTOR)
13000 : 2221 : && (TREE_CODE (arg2) == VECTOR_CST
13001 : 55 : || TREE_CODE (arg2) == CONSTRUCTOR)
13002 : 19440 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
13003 : : {
13004 : 2166 : vec_perm_builder sel (nelts, nelts, 1);
13005 : 22658 : for (unsigned int i = 0; i < nelts; i++)
13006 : : {
13007 : 20492 : tree val = VECTOR_CST_ELT (arg0, i);
13008 : 20492 : if (integer_all_onesp (val))
13009 : 10324 : sel.quick_push (i);
13010 : 10168 : else if (integer_zerop (val))
13011 : 10168 : sel.quick_push (nelts + i);
13012 : : else /* Currently unreachable. */
13013 : 1694 : return NULL_TREE;
13014 : : }
13015 : 2166 : vec_perm_indices indices (sel, 2, nelts);
13016 : 2166 : tree t = fold_vec_perm (type, arg1, arg2, indices);
13017 : 2166 : if (t != NULL_TREE)
13018 : 1694 : return t;
13019 : 3860 : }
13020 : : }
13021 : :
13022 : : /* If we have A op B ? A : C, we may be able to convert this to a
13023 : : simpler expression, depending on the operation and the values
13024 : : of B and C. Signed zeros prevent all of these transformations,
13025 : : for reasons given above each one.
13026 : :
13027 : : Also try swapping the arguments and inverting the conditional. */
13028 : 10717468 : if (COMPARISON_CLASS_P (arg0)
13029 : 8984359 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
13030 : 10838029 : && !HONOR_SIGNED_ZEROS (op1))
13031 : : {
13032 : 109916 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
13033 : 109916 : TREE_OPERAND (arg0, 0),
13034 : 109916 : TREE_OPERAND (arg0, 1),
13035 : : op1, op2);
13036 : 109916 : if (tem)
13037 : : return tem;
13038 : : }
13039 : :
13040 : 10710978 : if (COMPARISON_CLASS_P (arg0)
13041 : 8977869 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
13042 : 11148181 : && !HONOR_SIGNED_ZEROS (op2))
13043 : : {
13044 : 342878 : enum tree_code comp_code = TREE_CODE (arg0);
13045 : 342878 : tree arg00 = TREE_OPERAND (arg0, 0);
13046 : 342878 : tree arg01 = TREE_OPERAND (arg0, 1);
13047 : 342878 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
13048 : 342878 : if (comp_code != ERROR_MARK)
13049 : 342878 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
13050 : : arg00,
13051 : : arg01,
13052 : : op2, op1);
13053 : 342878 : if (tem)
13054 : : return tem;
13055 : : }
13056 : :
13057 : : /* If the second operand is simpler than the third, swap them
13058 : : since that produces better jump optimization results. */
13059 : 10462121 : if (truth_value_p (TREE_CODE (arg0))
13060 : 10462121 : && tree_swap_operands_p (op1, op2))
13061 : : {
13062 : 1769633 : location_t loc0 = expr_location_or (arg0, loc);
13063 : : /* See if this can be inverted. If it can't, possibly because
13064 : : it was a floating-point inequality comparison, don't do
13065 : : anything. */
13066 : 1769633 : tem = fold_invert_truthvalue (loc0, arg0);
13067 : 1769633 : if (tem)
13068 : 1172922 : return fold_build3_loc (loc, code, type, tem, op2, op1);
13069 : : }
13070 : :
13071 : : /* Convert A ? 1 : 0 to simply A. */
13072 : 9289199 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13073 : 8959438 : : (integer_onep (op1)
13074 : 397969 : && !VECTOR_TYPE_P (type)))
13075 : 598885 : && integer_zerop (op2)
13076 : : /* If we try to convert OP0 to our type, the
13077 : : call to fold will try to move the conversion inside
13078 : : a COND, which will recurse. In that case, the COND_EXPR
13079 : : is probably the best choice, so leave it alone. */
13080 : 10205761 : && type == TREE_TYPE (arg0))
13081 : 32361 : return protected_set_expr_location_unshare (arg0, loc);
13082 : :
13083 : : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13084 : : over COND_EXPR in cases such as floating point comparisons. */
13085 : 9256838 : if (integer_zerop (op1)
13086 : 260178 : && code == COND_EXPR
13087 : 258416 : && integer_onep (op2)
13088 : 33411 : && !VECTOR_TYPE_P (type)
13089 : 9290249 : && truth_value_p (TREE_CODE (arg0)))
13090 : 31568 : return fold_convert_loc (loc, type,
13091 : 31568 : invert_truthvalue_loc (loc, arg0));
13092 : :
13093 : : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13094 : 9225270 : if (TREE_CODE (arg0) == LT_EXPR
13095 : 1043725 : && integer_zerop (TREE_OPERAND (arg0, 1))
13096 : 16808 : && integer_zerop (op2)
13097 : 9226189 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13098 : : {
13099 : : /* sign_bit_p looks through both zero and sign extensions,
13100 : : but for this optimization only sign extensions are
13101 : : usable. */
13102 : 56 : tree tem2 = TREE_OPERAND (arg0, 0);
13103 : 56 : while (tem != tem2)
13104 : : {
13105 : 0 : if (TREE_CODE (tem2) != NOP_EXPR
13106 : 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13107 : : {
13108 : : tem = NULL_TREE;
13109 : : break;
13110 : : }
13111 : 0 : tem2 = TREE_OPERAND (tem2, 0);
13112 : : }
13113 : : /* sign_bit_p only checks ARG1 bits within A's precision.
13114 : : If <sign bit of A> has wider type than A, bits outside
13115 : : of A's precision in <sign bit of A> need to be checked.
13116 : : If they are all 0, this optimization needs to be done
13117 : : in unsigned A's type, if they are all 1 in signed A's type,
13118 : : otherwise this can't be done. */
13119 : 56 : if (tem
13120 : 56 : && TYPE_PRECISION (TREE_TYPE (tem))
13121 : 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
13122 : 112 : && TYPE_PRECISION (TREE_TYPE (tem))
13123 : 56 : < TYPE_PRECISION (type))
13124 : : {
13125 : 56 : int inner_width, outer_width;
13126 : 56 : tree tem_type;
13127 : :
13128 : 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13129 : 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13130 : 56 : if (outer_width > TYPE_PRECISION (type))
13131 : 0 : outer_width = TYPE_PRECISION (type);
13132 : :
13133 : 56 : wide_int mask = wi::shifted_mask
13134 : 56 : (inner_width, outer_width - inner_width, false,
13135 : 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13136 : :
13137 : 56 : wide_int common = mask & wi::to_wide (arg1);
13138 : 56 : if (common == mask)
13139 : : {
13140 : 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13141 : 28 : tem = fold_convert_loc (loc, tem_type, tem);
13142 : : }
13143 : 28 : else if (common == 0)
13144 : : {
13145 : 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13146 : 0 : tem = fold_convert_loc (loc, tem_type, tem);
13147 : : }
13148 : : else
13149 : : tem = NULL;
13150 : 56 : }
13151 : :
13152 : 56 : if (tem)
13153 : 28 : return
13154 : 56 : fold_convert_loc (loc, type,
13155 : : fold_build2_loc (loc, BIT_AND_EXPR,
13156 : 28 : TREE_TYPE (tem), tem,
13157 : : fold_convert_loc (loc,
13158 : 28 : TREE_TYPE (tem),
13159 : 28 : arg1)));
13160 : : }
13161 : :
13162 : : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13163 : : already handled above. */
13164 : 9225242 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13165 : 347 : && integer_onep (TREE_OPERAND (arg0, 1))
13166 : 3 : && integer_zerop (op2)
13167 : 9225242 : && integer_pow2p (arg1))
13168 : : {
13169 : 0 : tree tem = TREE_OPERAND (arg0, 0);
13170 : 0 : STRIP_NOPS (tem);
13171 : 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13172 : 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13173 : 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13174 : 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13175 : 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13176 : : fold_convert_loc (loc, type,
13177 : 0 : TREE_OPERAND (tem, 0)),
13178 : 0 : op1);
13179 : : }
13180 : :
13181 : : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13182 : : is probably obsolete because the first operand should be a
13183 : : truth value (that's why we have the two cases above), but let's
13184 : : leave it in until we can confirm this for all front-ends. */
13185 : 9225242 : if (integer_zerop (op2)
13186 : 1959457 : && TREE_CODE (arg0) == NE_EXPR
13187 : 550792 : && integer_zerop (TREE_OPERAND (arg0, 1))
13188 : 301994 : && integer_pow2p (arg1)
13189 : 31896 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13190 : 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13191 : : arg1, OEP_ONLY_CONST)
13192 : : /* operand_equal_p compares just value, not precision, so e.g.
13193 : : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13194 : : second operand 32-bit -128, which is not a power of two (or vice
13195 : : versa. */
13196 : 9225242 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13197 : 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13198 : :
13199 : : /* Disable the transformations below for vectors, since
13200 : : fold_binary_op_with_conditional_arg may undo them immediately,
13201 : : yielding an infinite loop. */
13202 : 9225242 : if (code == VEC_COND_EXPR)
13203 : : return NULL_TREE;
13204 : :
13205 : : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13206 : 8895481 : if (integer_zerop (op2)
13207 : 1693099 : && truth_value_p (TREE_CODE (arg0))
13208 : 1568615 : && truth_value_p (TREE_CODE (arg1))
13209 : 8928237 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13210 : 32756 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13211 : : : TRUTH_ANDIF_EXPR,
13212 : 32756 : type, fold_convert_loc (loc, type, arg0), op1);
13213 : :
13214 : : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13215 : 8862725 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13216 : 464221 : && truth_value_p (TREE_CODE (arg0))
13217 : 322830 : && truth_value_p (TREE_CODE (arg1))
13218 : 8899281 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13219 : : {
13220 : 36556 : location_t loc0 = expr_location_or (arg0, loc);
13221 : : /* Only perform transformation if ARG0 is easily inverted. */
13222 : 36556 : tem = fold_invert_truthvalue (loc0, arg0);
13223 : 36556 : if (tem)
13224 : 36292 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13225 : : ? BIT_IOR_EXPR
13226 : : : TRUTH_ORIF_EXPR,
13227 : : type, fold_convert_loc (loc, type, tem),
13228 : 36292 : op1);
13229 : : }
13230 : :
13231 : : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13232 : 8826433 : if (integer_zerop (arg1)
13233 : 226927 : && truth_value_p (TREE_CODE (arg0))
13234 : 50524 : && truth_value_p (TREE_CODE (op2))
13235 : 8826461 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13236 : : {
13237 : 28 : location_t loc0 = expr_location_or (arg0, loc);
13238 : : /* Only perform transformation if ARG0 is easily inverted. */
13239 : 28 : tem = fold_invert_truthvalue (loc0, arg0);
13240 : 28 : if (tem)
13241 : 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13242 : : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13243 : : type, fold_convert_loc (loc, type, tem),
13244 : 0 : op2);
13245 : : }
13246 : :
13247 : : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13248 : 8826433 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13249 : 365608 : && truth_value_p (TREE_CODE (arg0))
13250 : 283617 : && truth_value_p (TREE_CODE (op2))
13251 : 8826619 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13252 : 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13253 : : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13254 : 186 : type, fold_convert_loc (loc, type, arg0), op2);
13255 : :
13256 : : return NULL_TREE;
13257 : :
13258 : 0 : case CALL_EXPR:
13259 : : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13260 : : of fold_ternary on them. */
13261 : 0 : gcc_unreachable ();
13262 : :
13263 : 633806 : case BIT_FIELD_REF:
13264 : 633806 : if (TREE_CODE (arg0) == VECTOR_CST
13265 : 25562 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13266 : 1808 : || (VECTOR_TYPE_P (type)
13267 : 1150 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13268 : 24884 : && tree_fits_uhwi_p (op1)
13269 : 658690 : && tree_fits_uhwi_p (op2))
13270 : : {
13271 : 24884 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13272 : 24884 : unsigned HOST_WIDE_INT width
13273 : 24884 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13274 : 24884 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13275 : 24884 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13276 : 24884 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13277 : :
13278 : 24884 : if (n != 0
13279 : 24884 : && (idx % width) == 0
13280 : 24884 : && (n % width) == 0
13281 : 49768 : && known_le ((idx + n) / width,
13282 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13283 : : {
13284 : 24884 : idx = idx / width;
13285 : 24884 : n = n / width;
13286 : :
13287 : 24884 : if (TREE_CODE (arg0) == VECTOR_CST)
13288 : : {
13289 : 24884 : if (n == 1)
13290 : : {
13291 : 23758 : tem = VECTOR_CST_ELT (arg0, idx);
13292 : 23758 : if (VECTOR_TYPE_P (type))
13293 : 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13294 : 23758 : return tem;
13295 : : }
13296 : :
13297 : 1126 : tree_vector_builder vals (type, n, 1);
13298 : 6934 : for (unsigned i = 0; i < n; ++i)
13299 : 5808 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13300 : 1126 : return vals.build ();
13301 : 1126 : }
13302 : : }
13303 : : }
13304 : :
13305 : : /* On constants we can use native encode/interpret to constant
13306 : : fold (nearly) all BIT_FIELD_REFs. */
13307 : 608922 : if (CONSTANT_CLASS_P (arg0)
13308 : 1479 : && can_native_interpret_type_p (type)
13309 : : && BITS_PER_UNIT == 8
13310 : 1479 : && tree_fits_uhwi_p (op1)
13311 : 610401 : && tree_fits_uhwi_p (op2))
13312 : : {
13313 : 1479 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13314 : 1479 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13315 : : /* Limit us to a reasonable amount of work. To relax the
13316 : : other limitations we need bit-shifting of the buffer
13317 : : and rounding up the size. */
13318 : 1479 : if (bitpos % BITS_PER_UNIT == 0
13319 : 1479 : && bitsize % BITS_PER_UNIT == 0
13320 : 1479 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13321 : : {
13322 : 1479 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13323 : 1479 : unsigned HOST_WIDE_INT len
13324 : 1479 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13325 : 1479 : bitpos / BITS_PER_UNIT);
13326 : 1479 : if (len > 0
13327 : 1479 : && len * BITS_PER_UNIT >= bitsize)
13328 : : {
13329 : 1479 : tree v = native_interpret_expr (type, b,
13330 : : bitsize / BITS_PER_UNIT);
13331 : 1479 : if (v)
13332 : 1473 : return v;
13333 : : }
13334 : : }
13335 : : }
13336 : :
13337 : : return NULL_TREE;
13338 : :
13339 : 714667 : case VEC_PERM_EXPR:
13340 : : /* Perform constant folding of BIT_INSERT_EXPR. */
13341 : 714667 : if (TREE_CODE (arg2) == VECTOR_CST
13342 : 703435 : && TREE_CODE (op0) == VECTOR_CST
13343 : 15232 : && TREE_CODE (op1) == VECTOR_CST)
13344 : : {
13345 : : /* Build a vector of integers from the tree mask. */
13346 : 3915 : vec_perm_builder builder;
13347 : 3915 : if (!tree_to_vec_perm_builder (&builder, arg2))
13348 : : return NULL_TREE;
13349 : :
13350 : : /* Create a vec_perm_indices for the integer vector. */
13351 : 3915 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13352 : 3915 : bool single_arg = (op0 == op1);
13353 : 7830 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13354 : 3915 : return fold_vec_perm (type, op0, op1, sel);
13355 : 7830 : }
13356 : : return NULL_TREE;
13357 : :
13358 : 14442 : case BIT_INSERT_EXPR:
13359 : : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13360 : 14442 : if (TREE_CODE (arg0) == INTEGER_CST
13361 : 14 : && TREE_CODE (arg1) == INTEGER_CST)
13362 : : {
13363 : 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13364 : 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13365 : 2 : if (BYTES_BIG_ENDIAN)
13366 : : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13367 : 2 : wide_int tem = (wi::to_wide (arg0)
13368 : 4 : & wi::shifted_mask (bitpos, bitsize, true,
13369 : 4 : TYPE_PRECISION (type)));
13370 : 2 : wide_int tem2
13371 : 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13372 : 2 : bitsize), bitpos);
13373 : 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13374 : 2 : }
13375 : 14440 : else if (TREE_CODE (arg0) == VECTOR_CST
13376 : 906 : && CONSTANT_CLASS_P (arg1)
13377 : 14742 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13378 : 302 : TREE_TYPE (arg1)))
13379 : : {
13380 : 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13381 : 302 : unsigned HOST_WIDE_INT elsize
13382 : 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13383 : 302 : if (bitpos % elsize == 0)
13384 : : {
13385 : 302 : unsigned k = bitpos / elsize;
13386 : 302 : unsigned HOST_WIDE_INT nelts;
13387 : 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13388 : 29132782 : return arg0;
13389 : 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13390 : : {
13391 : 290 : tree_vector_builder elts (type, nelts, 1);
13392 : 290 : elts.quick_grow (nelts);
13393 : 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13394 : 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13395 : 290 : return elts.build ();
13396 : 290 : }
13397 : : }
13398 : : }
13399 : : return NULL_TREE;
13400 : :
13401 : : default:
13402 : : return NULL_TREE;
13403 : : } /* switch (code) */
13404 : : }
13405 : :
13406 : : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13407 : : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13408 : : constructor element index of the value returned. If the element is
13409 : : not found NULL_TREE is returned and *CTOR_IDX is updated to
13410 : : the index of the element after the ACCESS_INDEX position (which
13411 : : may be outside of the CTOR array). */
13412 : :
13413 : : tree
13414 : 715401 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13415 : : unsigned *ctor_idx)
13416 : : {
13417 : 715401 : tree index_type = NULL_TREE;
13418 : 715401 : signop index_sgn = UNSIGNED;
13419 : 715401 : offset_int low_bound = 0;
13420 : :
13421 : 715401 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13422 : : {
13423 : 715401 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13424 : 715401 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13425 : : {
13426 : : /* Static constructors for variably sized objects makes no sense. */
13427 : 715401 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13428 : 715401 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13429 : : /* ??? When it is obvious that the range is signed, treat it so. */
13430 : 715401 : if (TYPE_UNSIGNED (index_type)
13431 : 369849 : && TYPE_MAX_VALUE (domain_type)
13432 : 1085219 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13433 : 369818 : TYPE_MIN_VALUE (domain_type)))
13434 : : {
13435 : 0 : index_sgn = SIGNED;
13436 : 0 : low_bound
13437 : 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13438 : : SIGNED);
13439 : : }
13440 : : else
13441 : : {
13442 : 715401 : index_sgn = TYPE_SIGN (index_type);
13443 : 715401 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13444 : : }
13445 : : }
13446 : : }
13447 : :
13448 : 715401 : if (index_type)
13449 : 715401 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13450 : : index_sgn);
13451 : :
13452 : 715401 : offset_int index = low_bound;
13453 : 715401 : if (index_type)
13454 : 715401 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13455 : :
13456 : 715401 : offset_int max_index = index;
13457 : 715401 : unsigned cnt;
13458 : 715401 : tree cfield, cval;
13459 : 715401 : bool first_p = true;
13460 : :
13461 : 15025826 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13462 : : {
13463 : : /* Array constructor might explicitly set index, or specify a range,
13464 : : or leave index NULL meaning that it is next index after previous
13465 : : one. */
13466 : 15024693 : if (cfield)
13467 : : {
13468 : 6884263 : if (TREE_CODE (cfield) == INTEGER_CST)
13469 : 13767052 : max_index = index
13470 : 6883526 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13471 : : else
13472 : : {
13473 : 737 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13474 : 737 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13475 : : index_sgn);
13476 : 737 : max_index
13477 : 737 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13478 : : index_sgn);
13479 : 737 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13480 : : }
13481 : : }
13482 : 8140430 : else if (!first_p)
13483 : : {
13484 : 7890842 : index = max_index + 1;
13485 : 7890842 : if (index_type)
13486 : 7890842 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13487 : 7890842 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13488 : 7890842 : max_index = index;
13489 : : }
13490 : : else
13491 : : first_p = false;
13492 : :
13493 : 15024693 : if (TREE_CODE (cval) == RAW_DATA_CST)
13494 : 2630 : max_index += RAW_DATA_LENGTH (cval) - 1;
13495 : :
13496 : : /* Do we have match? */
13497 : 15024693 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13498 : : {
13499 : 15024405 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13500 : : {
13501 : 714152 : if (ctor_idx)
13502 : 714152 : *ctor_idx = cnt;
13503 : 714152 : return cval;
13504 : : }
13505 : : }
13506 : 288 : else if (in_gimple_form)
13507 : : /* We're past the element we search for. Note during parsing
13508 : : the elements might not be sorted.
13509 : : ??? We should use a binary search and a flag on the
13510 : : CONSTRUCTOR as to whether elements are sorted in declaration
13511 : : order. */
13512 : : break;
13513 : : }
13514 : 1249 : if (ctor_idx)
13515 : 1249 : *ctor_idx = cnt;
13516 : : return NULL_TREE;
13517 : : }
13518 : :
13519 : : /* Perform constant folding and related simplification of EXPR.
13520 : : The related simplifications include x*1 => x, x*0 => 0, etc.,
13521 : : and application of the associative law.
13522 : : NOP_EXPR conversions may be removed freely (as long as we
13523 : : are careful not to change the type of the overall expression).
13524 : : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13525 : : but we can constant-fold them if they have constant operands. */
13526 : :
13527 : : #ifdef ENABLE_FOLD_CHECKING
13528 : : # define fold(x) fold_1 (x)
13529 : : static tree fold_1 (tree);
13530 : : static
13531 : : #endif
13532 : : tree
13533 : 1256264446 : fold (tree expr)
13534 : : {
13535 : 1256435645 : const tree t = expr;
13536 : 1256435645 : enum tree_code code = TREE_CODE (t);
13537 : 1256435645 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13538 : 1256435645 : tree tem;
13539 : 1256435645 : location_t loc = EXPR_LOCATION (expr);
13540 : :
13541 : : /* Return right away if a constant. */
13542 : 1256435645 : if (kind == tcc_constant)
13543 : : return t;
13544 : :
13545 : : /* CALL_EXPR-like objects with variable numbers of operands are
13546 : : treated specially. */
13547 : 1154302332 : if (kind == tcc_vl_exp)
13548 : : {
13549 : 167257967 : if (code == CALL_EXPR)
13550 : : {
13551 : 167257422 : tem = fold_call_expr (loc, expr, false);
13552 : 331795875 : return tem ? tem : expr;
13553 : : }
13554 : : return expr;
13555 : : }
13556 : :
13557 : 987044365 : if (IS_EXPR_CODE_CLASS (kind))
13558 : : {
13559 : 984869684 : tree type = TREE_TYPE (t);
13560 : 984869684 : tree op0, op1, op2;
13561 : :
13562 : 984869684 : switch (TREE_CODE_LENGTH (code))
13563 : : {
13564 : 896153777 : case 1:
13565 : 896153777 : op0 = TREE_OPERAND (t, 0);
13566 : 896153777 : tem = fold_unary_loc (loc, code, type, op0);
13567 : 1527647845 : return tem ? tem : expr;
13568 : 79832086 : case 2:
13569 : 79832086 : op0 = TREE_OPERAND (t, 0);
13570 : 79832086 : op1 = TREE_OPERAND (t, 1);
13571 : 79832086 : tem = fold_binary_loc (loc, code, type, op0, op1);
13572 : 150718978 : return tem ? tem : expr;
13573 : 4266405 : case 3:
13574 : 4266405 : op0 = TREE_OPERAND (t, 0);
13575 : 4266405 : op1 = TREE_OPERAND (t, 1);
13576 : 4266405 : op2 = TREE_OPERAND (t, 2);
13577 : 4266405 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13578 : 8267754 : return tem ? tem : expr;
13579 : : default:
13580 : : break;
13581 : : }
13582 : : }
13583 : :
13584 : 6792097 : switch (code)
13585 : : {
13586 : 4513691 : case ARRAY_REF:
13587 : 4513691 : {
13588 : 4513691 : tree op0 = TREE_OPERAND (t, 0);
13589 : 4513691 : tree op1 = TREE_OPERAND (t, 1);
13590 : :
13591 : 4513691 : if (TREE_CODE (op1) == INTEGER_CST
13592 : 2925421 : && TREE_CODE (op0) == CONSTRUCTOR
13593 : 4515146 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13594 : : {
13595 : 1455 : unsigned int idx;
13596 : 1455 : tree val
13597 : 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13598 : : &idx);
13599 : 1455 : if (val)
13600 : : {
13601 : 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13602 : : return val;
13603 : 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13604 : 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13605 : : != INTEGER_CST))
13606 : : return t;
13607 : 2 : offset_int o
13608 : 2 : = (wi::to_offset (op1)
13609 : 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13610 : 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13611 : 2 : return build_int_cst (TREE_TYPE (val),
13612 : 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13613 : : }
13614 : : }
13615 : :
13616 : : return t;
13617 : : }
13618 : :
13619 : : /* Return a VECTOR_CST if possible. */
13620 : 114625 : case CONSTRUCTOR:
13621 : 114625 : {
13622 : 114625 : tree type = TREE_TYPE (t);
13623 : 114625 : if (TREE_CODE (type) != VECTOR_TYPE)
13624 : : return t;
13625 : :
13626 : : unsigned i;
13627 : : tree val;
13628 : 279246 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13629 : 241917 : if (! CONSTANT_CLASS_P (val))
13630 : : return t;
13631 : :
13632 : 37329 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13633 : : }
13634 : :
13635 : 171199 : case CONST_DECL:
13636 : 171199 : return fold (DECL_INITIAL (t));
13637 : :
13638 : : default:
13639 : : return t;
13640 : : } /* switch (code) */
13641 : : }
13642 : :
13643 : : #ifdef ENABLE_FOLD_CHECKING
13644 : : #undef fold
13645 : :
13646 : : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13647 : : hash_table<nofree_ptr_hash<const tree_node> > *);
13648 : : static void fold_check_failed (const_tree, const_tree);
13649 : : void print_fold_checksum (const_tree);
13650 : :
13651 : : /* When --enable-checking=fold, compute a digest of expr before
13652 : : and after actual fold call to see if fold did not accidentally
13653 : : change original expr. */
13654 : :
13655 : : tree
13656 : : fold (tree expr)
13657 : : {
13658 : : tree ret;
13659 : : struct md5_ctx ctx;
13660 : : unsigned char checksum_before[16], checksum_after[16];
13661 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13662 : :
13663 : : md5_init_ctx (&ctx);
13664 : : fold_checksum_tree (expr, &ctx, &ht);
13665 : : md5_finish_ctx (&ctx, checksum_before);
13666 : : ht.empty ();
13667 : :
13668 : : ret = fold_1 (expr);
13669 : :
13670 : : md5_init_ctx (&ctx);
13671 : : fold_checksum_tree (expr, &ctx, &ht);
13672 : : md5_finish_ctx (&ctx, checksum_after);
13673 : :
13674 : : if (memcmp (checksum_before, checksum_after, 16))
13675 : : fold_check_failed (expr, ret);
13676 : :
13677 : : return ret;
13678 : : }
13679 : :
13680 : : void
13681 : : print_fold_checksum (const_tree expr)
13682 : : {
13683 : : struct md5_ctx ctx;
13684 : : unsigned char checksum[16], cnt;
13685 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13686 : :
13687 : : md5_init_ctx (&ctx);
13688 : : fold_checksum_tree (expr, &ctx, &ht);
13689 : : md5_finish_ctx (&ctx, checksum);
13690 : : for (cnt = 0; cnt < 16; ++cnt)
13691 : : fprintf (stderr, "%02x", checksum[cnt]);
13692 : : putc ('\n', stderr);
13693 : : }
13694 : :
13695 : : static void
13696 : : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13697 : : {
13698 : : internal_error ("fold check: original tree changed by fold");
13699 : : }
13700 : :
13701 : : static void
13702 : : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13703 : : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13704 : : {
13705 : : const tree_node **slot;
13706 : : enum tree_code code;
13707 : : union tree_node *buf;
13708 : : int i, len;
13709 : :
13710 : : recursive_label:
13711 : : if (expr == NULL)
13712 : : return;
13713 : : slot = ht->find_slot (expr, INSERT);
13714 : : if (*slot != NULL)
13715 : : return;
13716 : : *slot = expr;
13717 : : code = TREE_CODE (expr);
13718 : : if (TREE_CODE_CLASS (code) == tcc_declaration
13719 : : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13720 : : {
13721 : : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13722 : : size_t sz = tree_size (expr);
13723 : : buf = XALLOCAVAR (union tree_node, sz);
13724 : : memcpy ((char *) buf, expr, sz);
13725 : : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13726 : : buf->decl_with_vis.symtab_node = NULL;
13727 : : buf->base.nowarning_flag = 0;
13728 : : expr = (tree) buf;
13729 : : }
13730 : : else if (TREE_CODE_CLASS (code) == tcc_type
13731 : : && (TYPE_POINTER_TO (expr)
13732 : : || TYPE_REFERENCE_TO (expr)
13733 : : || TYPE_CACHED_VALUES_P (expr)
13734 : : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13735 : : || TYPE_NEXT_VARIANT (expr)
13736 : : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13737 : : {
13738 : : /* Allow these fields to be modified. */
13739 : : tree tmp;
13740 : : size_t sz = tree_size (expr);
13741 : : buf = XALLOCAVAR (union tree_node, sz);
13742 : : memcpy ((char *) buf, expr, sz);
13743 : : expr = tmp = (tree) buf;
13744 : : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13745 : : TYPE_POINTER_TO (tmp) = NULL;
13746 : : TYPE_REFERENCE_TO (tmp) = NULL;
13747 : : TYPE_NEXT_VARIANT (tmp) = NULL;
13748 : : TYPE_ALIAS_SET (tmp) = -1;
13749 : : if (TYPE_CACHED_VALUES_P (tmp))
13750 : : {
13751 : : TYPE_CACHED_VALUES_P (tmp) = 0;
13752 : : TYPE_CACHED_VALUES (tmp) = NULL;
13753 : : }
13754 : : }
13755 : : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13756 : : {
13757 : : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13758 : : that and change builtins.cc etc. instead - see PR89543. */
13759 : : size_t sz = tree_size (expr);
13760 : : buf = XALLOCAVAR (union tree_node, sz);
13761 : : memcpy ((char *) buf, expr, sz);
13762 : : buf->base.nowarning_flag = 0;
13763 : : expr = (tree) buf;
13764 : : }
13765 : : md5_process_bytes (expr, tree_size (expr), ctx);
13766 : : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13767 : : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13768 : : if (TREE_CODE_CLASS (code) != tcc_type
13769 : : && TREE_CODE_CLASS (code) != tcc_declaration
13770 : : && code != TREE_LIST
13771 : : && code != SSA_NAME
13772 : : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13773 : : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13774 : : switch (TREE_CODE_CLASS (code))
13775 : : {
13776 : : case tcc_constant:
13777 : : switch (code)
13778 : : {
13779 : : case STRING_CST:
13780 : : md5_process_bytes (TREE_STRING_POINTER (expr),
13781 : : TREE_STRING_LENGTH (expr), ctx);
13782 : : break;
13783 : : case COMPLEX_CST:
13784 : : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13785 : : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13786 : : break;
13787 : : case VECTOR_CST:
13788 : : len = vector_cst_encoded_nelts (expr);
13789 : : for (i = 0; i < len; ++i)
13790 : : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13791 : : break;
13792 : : default:
13793 : : break;
13794 : : }
13795 : : break;
13796 : : case tcc_exceptional:
13797 : : switch (code)
13798 : : {
13799 : : case TREE_LIST:
13800 : : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13801 : : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13802 : : expr = TREE_CHAIN (expr);
13803 : : goto recursive_label;
13804 : : break;
13805 : : case TREE_VEC:
13806 : : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13807 : : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13808 : : break;
13809 : : default:
13810 : : break;
13811 : : }
13812 : : break;
13813 : : case tcc_expression:
13814 : : case tcc_reference:
13815 : : case tcc_comparison:
13816 : : case tcc_unary:
13817 : : case tcc_binary:
13818 : : case tcc_statement:
13819 : : case tcc_vl_exp:
13820 : : len = TREE_OPERAND_LENGTH (expr);
13821 : : for (i = 0; i < len; ++i)
13822 : : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13823 : : break;
13824 : : case tcc_declaration:
13825 : : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13826 : : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13827 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13828 : : {
13829 : : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13830 : : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13831 : : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13832 : : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13833 : : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13834 : : }
13835 : :
13836 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13837 : : {
13838 : : if (TREE_CODE (expr) == FUNCTION_DECL)
13839 : : {
13840 : : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13841 : : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13842 : : }
13843 : : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13844 : : }
13845 : : break;
13846 : : case tcc_type:
13847 : : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13848 : : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13849 : : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13850 : : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13851 : : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13852 : : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13853 : : if (INTEGRAL_TYPE_P (expr)
13854 : : || SCALAR_FLOAT_TYPE_P (expr))
13855 : : {
13856 : : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13857 : : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13858 : : }
13859 : : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13860 : : if (RECORD_OR_UNION_TYPE_P (expr))
13861 : : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13862 : : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13863 : : break;
13864 : : default:
13865 : : break;
13866 : : }
13867 : : }
13868 : :
13869 : : /* Helper function for outputting the checksum of a tree T. When
13870 : : debugging with gdb, you can "define mynext" to be "next" followed
13871 : : by "call debug_fold_checksum (op0)", then just trace down till the
13872 : : outputs differ. */
13873 : :
13874 : : DEBUG_FUNCTION void
13875 : : debug_fold_checksum (const_tree t)
13876 : : {
13877 : : int i;
13878 : : unsigned char checksum[16];
13879 : : struct md5_ctx ctx;
13880 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13881 : :
13882 : : md5_init_ctx (&ctx);
13883 : : fold_checksum_tree (t, &ctx, &ht);
13884 : : md5_finish_ctx (&ctx, checksum);
13885 : : ht.empty ();
13886 : :
13887 : : for (i = 0; i < 16; i++)
13888 : : fprintf (stderr, "%d ", checksum[i]);
13889 : :
13890 : : fprintf (stderr, "\n");
13891 : : }
13892 : :
13893 : : #endif
13894 : :
13895 : : /* Fold a unary tree expression with code CODE of type TYPE with an
13896 : : operand OP0. LOC is the location of the resulting expression.
13897 : : Return a folded expression if successful. Otherwise, return a tree
13898 : : expression with code CODE of type TYPE with an operand OP0. */
13899 : :
13900 : : tree
13901 : 749827783 : fold_build1_loc (location_t loc,
13902 : : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13903 : : {
13904 : 749827783 : tree tem;
13905 : : #ifdef ENABLE_FOLD_CHECKING
13906 : : unsigned char checksum_before[16], checksum_after[16];
13907 : : struct md5_ctx ctx;
13908 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13909 : :
13910 : : md5_init_ctx (&ctx);
13911 : : fold_checksum_tree (op0, &ctx, &ht);
13912 : : md5_finish_ctx (&ctx, checksum_before);
13913 : : ht.empty ();
13914 : : #endif
13915 : :
13916 : 749827783 : tem = fold_unary_loc (loc, code, type, op0);
13917 : 749827783 : if (!tem)
13918 : 400226030 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13919 : :
13920 : : #ifdef ENABLE_FOLD_CHECKING
13921 : : md5_init_ctx (&ctx);
13922 : : fold_checksum_tree (op0, &ctx, &ht);
13923 : : md5_finish_ctx (&ctx, checksum_after);
13924 : :
13925 : : if (memcmp (checksum_before, checksum_after, 16))
13926 : : fold_check_failed (op0, tem);
13927 : : #endif
13928 : 749827783 : return tem;
13929 : : }
13930 : :
13931 : : /* Fold a binary tree expression with code CODE of type TYPE with
13932 : : operands OP0 and OP1. LOC is the location of the resulting
13933 : : expression. Return a folded expression if successful. Otherwise,
13934 : : return a tree expression with code CODE of type TYPE with operands
13935 : : OP0 and OP1. */
13936 : :
13937 : : tree
13938 : 579559465 : fold_build2_loc (location_t loc,
13939 : : enum tree_code code, tree type, tree op0, tree op1
13940 : : MEM_STAT_DECL)
13941 : : {
13942 : 579559465 : tree tem;
13943 : : #ifdef ENABLE_FOLD_CHECKING
13944 : : unsigned char checksum_before_op0[16],
13945 : : checksum_before_op1[16],
13946 : : checksum_after_op0[16],
13947 : : checksum_after_op1[16];
13948 : : struct md5_ctx ctx;
13949 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13950 : :
13951 : : md5_init_ctx (&ctx);
13952 : : fold_checksum_tree (op0, &ctx, &ht);
13953 : : md5_finish_ctx (&ctx, checksum_before_op0);
13954 : : ht.empty ();
13955 : :
13956 : : md5_init_ctx (&ctx);
13957 : : fold_checksum_tree (op1, &ctx, &ht);
13958 : : md5_finish_ctx (&ctx, checksum_before_op1);
13959 : : ht.empty ();
13960 : : #endif
13961 : :
13962 : 579559465 : tem = fold_binary_loc (loc, code, type, op0, op1);
13963 : 579559465 : if (!tem)
13964 : 324007841 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13965 : :
13966 : : #ifdef ENABLE_FOLD_CHECKING
13967 : : md5_init_ctx (&ctx);
13968 : : fold_checksum_tree (op0, &ctx, &ht);
13969 : : md5_finish_ctx (&ctx, checksum_after_op0);
13970 : : ht.empty ();
13971 : :
13972 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13973 : : fold_check_failed (op0, tem);
13974 : :
13975 : : md5_init_ctx (&ctx);
13976 : : fold_checksum_tree (op1, &ctx, &ht);
13977 : : md5_finish_ctx (&ctx, checksum_after_op1);
13978 : :
13979 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13980 : : fold_check_failed (op1, tem);
13981 : : #endif
13982 : 579559465 : return tem;
13983 : : }
13984 : :
13985 : : /* Fold a ternary tree expression with code CODE of type TYPE with
13986 : : operands OP0, OP1, and OP2. Return a folded expression if
13987 : : successful. Otherwise, return a tree expression with code CODE of
13988 : : type TYPE with operands OP0, OP1, and OP2. */
13989 : :
13990 : : tree
13991 : 23230197 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13992 : : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13993 : : {
13994 : 23230197 : tree tem;
13995 : : #ifdef ENABLE_FOLD_CHECKING
13996 : : unsigned char checksum_before_op0[16],
13997 : : checksum_before_op1[16],
13998 : : checksum_before_op2[16],
13999 : : checksum_after_op0[16],
14000 : : checksum_after_op1[16],
14001 : : checksum_after_op2[16];
14002 : : struct md5_ctx ctx;
14003 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14004 : :
14005 : : md5_init_ctx (&ctx);
14006 : : fold_checksum_tree (op0, &ctx, &ht);
14007 : : md5_finish_ctx (&ctx, checksum_before_op0);
14008 : : ht.empty ();
14009 : :
14010 : : md5_init_ctx (&ctx);
14011 : : fold_checksum_tree (op1, &ctx, &ht);
14012 : : md5_finish_ctx (&ctx, checksum_before_op1);
14013 : : ht.empty ();
14014 : :
14015 : : md5_init_ctx (&ctx);
14016 : : fold_checksum_tree (op2, &ctx, &ht);
14017 : : md5_finish_ctx (&ctx, checksum_before_op2);
14018 : : ht.empty ();
14019 : : #endif
14020 : :
14021 : 23230197 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14022 : 23230197 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14023 : 23230197 : if (!tem)
14024 : 20575440 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14025 : :
14026 : : #ifdef ENABLE_FOLD_CHECKING
14027 : : md5_init_ctx (&ctx);
14028 : : fold_checksum_tree (op0, &ctx, &ht);
14029 : : md5_finish_ctx (&ctx, checksum_after_op0);
14030 : : ht.empty ();
14031 : :
14032 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14033 : : fold_check_failed (op0, tem);
14034 : :
14035 : : md5_init_ctx (&ctx);
14036 : : fold_checksum_tree (op1, &ctx, &ht);
14037 : : md5_finish_ctx (&ctx, checksum_after_op1);
14038 : : ht.empty ();
14039 : :
14040 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14041 : : fold_check_failed (op1, tem);
14042 : :
14043 : : md5_init_ctx (&ctx);
14044 : : fold_checksum_tree (op2, &ctx, &ht);
14045 : : md5_finish_ctx (&ctx, checksum_after_op2);
14046 : :
14047 : : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14048 : : fold_check_failed (op2, tem);
14049 : : #endif
14050 : 23230197 : return tem;
14051 : : }
14052 : :
14053 : : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14054 : : arguments in ARGARRAY, and a null static chain.
14055 : : Return a folded expression if successful. Otherwise, return a CALL_EXPR
14056 : : of type TYPE from the given operands as constructed by build_call_array. */
14057 : :
14058 : : tree
14059 : 51380827 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
14060 : : int nargs, tree *argarray)
14061 : : {
14062 : 51380827 : tree tem;
14063 : : #ifdef ENABLE_FOLD_CHECKING
14064 : : unsigned char checksum_before_fn[16],
14065 : : checksum_before_arglist[16],
14066 : : checksum_after_fn[16],
14067 : : checksum_after_arglist[16];
14068 : : struct md5_ctx ctx;
14069 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14070 : : int i;
14071 : :
14072 : : md5_init_ctx (&ctx);
14073 : : fold_checksum_tree (fn, &ctx, &ht);
14074 : : md5_finish_ctx (&ctx, checksum_before_fn);
14075 : : ht.empty ();
14076 : :
14077 : : md5_init_ctx (&ctx);
14078 : : for (i = 0; i < nargs; i++)
14079 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14080 : : md5_finish_ctx (&ctx, checksum_before_arglist);
14081 : : ht.empty ();
14082 : : #endif
14083 : :
14084 : 51380827 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14085 : 51380827 : if (!tem)
14086 : 49662270 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14087 : :
14088 : : #ifdef ENABLE_FOLD_CHECKING
14089 : : md5_init_ctx (&ctx);
14090 : : fold_checksum_tree (fn, &ctx, &ht);
14091 : : md5_finish_ctx (&ctx, checksum_after_fn);
14092 : : ht.empty ();
14093 : :
14094 : : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14095 : : fold_check_failed (fn, tem);
14096 : :
14097 : : md5_init_ctx (&ctx);
14098 : : for (i = 0; i < nargs; i++)
14099 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14100 : : md5_finish_ctx (&ctx, checksum_after_arglist);
14101 : :
14102 : : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14103 : : fold_check_failed (NULL_TREE, tem);
14104 : : #endif
14105 : 51380827 : return tem;
14106 : : }
14107 : :
14108 : : /* Perform constant folding and related simplification of initializer
14109 : : expression EXPR. These behave identically to "fold_buildN" but ignore
14110 : : potential run-time traps and exceptions that fold must preserve. */
14111 : :
14112 : : #define START_FOLD_INIT \
14113 : : int saved_signaling_nans = flag_signaling_nans;\
14114 : : int saved_trapping_math = flag_trapping_math;\
14115 : : int saved_rounding_math = flag_rounding_math;\
14116 : : int saved_trapv = flag_trapv;\
14117 : : int saved_folding_initializer = folding_initializer;\
14118 : : flag_signaling_nans = 0;\
14119 : : flag_trapping_math = 0;\
14120 : : flag_rounding_math = 0;\
14121 : : flag_trapv = 0;\
14122 : : folding_initializer = 1;
14123 : :
14124 : : #define END_FOLD_INIT \
14125 : : flag_signaling_nans = saved_signaling_nans;\
14126 : : flag_trapping_math = saved_trapping_math;\
14127 : : flag_rounding_math = saved_rounding_math;\
14128 : : flag_trapv = saved_trapv;\
14129 : : folding_initializer = saved_folding_initializer;
14130 : :
14131 : : tree
14132 : 543280 : fold_init (tree expr)
14133 : : {
14134 : 543280 : tree result;
14135 : 543280 : START_FOLD_INIT;
14136 : :
14137 : 543280 : result = fold (expr);
14138 : :
14139 : 543280 : END_FOLD_INIT;
14140 : 543280 : return result;
14141 : : }
14142 : :
14143 : : tree
14144 : 2988531 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14145 : : tree type, tree op)
14146 : : {
14147 : 2988531 : tree result;
14148 : 2988531 : START_FOLD_INIT;
14149 : :
14150 : 2988531 : result = fold_build1_loc (loc, code, type, op);
14151 : :
14152 : 2988531 : END_FOLD_INIT;
14153 : 2988531 : return result;
14154 : : }
14155 : :
14156 : : tree
14157 : 50474 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14158 : : tree type, tree op0, tree op1)
14159 : : {
14160 : 50474 : tree result;
14161 : 50474 : START_FOLD_INIT;
14162 : :
14163 : 50474 : result = fold_build2_loc (loc, code, type, op0, op1);
14164 : :
14165 : 50474 : END_FOLD_INIT;
14166 : 50474 : return result;
14167 : : }
14168 : :
14169 : : tree
14170 : 3489 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14171 : : int nargs, tree *argarray)
14172 : : {
14173 : 3489 : tree result;
14174 : 3489 : START_FOLD_INIT;
14175 : :
14176 : 3489 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14177 : :
14178 : 3489 : END_FOLD_INIT;
14179 : 3489 : return result;
14180 : : }
14181 : :
14182 : : tree
14183 : 22786739 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14184 : : tree lhs, tree rhs)
14185 : : {
14186 : 22786739 : tree result;
14187 : 22786739 : START_FOLD_INIT;
14188 : :
14189 : 22786739 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14190 : :
14191 : 22786739 : END_FOLD_INIT;
14192 : 22786739 : return result;
14193 : : }
14194 : :
14195 : : #undef START_FOLD_INIT
14196 : : #undef END_FOLD_INIT
14197 : :
14198 : : /* Determine if first argument is a multiple of second argument. Return
14199 : : false if it is not, or we cannot easily determined it to be.
14200 : :
14201 : : An example of the sort of thing we care about (at this point; this routine
14202 : : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14203 : : fold cases do now) is discovering that
14204 : :
14205 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14206 : :
14207 : : is a multiple of
14208 : :
14209 : : SAVE_EXPR (J * 8)
14210 : :
14211 : : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14212 : :
14213 : : This code also handles discovering that
14214 : :
14215 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14216 : :
14217 : : is a multiple of 8 so we don't have to worry about dealing with a
14218 : : possible remainder.
14219 : :
14220 : : Note that we *look* inside a SAVE_EXPR only to determine how it was
14221 : : calculated; it is not safe for fold to do much of anything else with the
14222 : : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14223 : : at run time. For example, the latter example above *cannot* be implemented
14224 : : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14225 : : evaluation time of the original SAVE_EXPR is not necessarily the same at
14226 : : the time the new expression is evaluated. The only optimization of this
14227 : : sort that would be valid is changing
14228 : :
14229 : : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14230 : :
14231 : : divided by 8 to
14232 : :
14233 : : SAVE_EXPR (I) * SAVE_EXPR (J)
14234 : :
14235 : : (where the same SAVE_EXPR (J) is used in the original and the
14236 : : transformed version).
14237 : :
14238 : : NOWRAP specifies whether all outer operations in TYPE should
14239 : : be considered not wrapping. Any type conversion within TOP acts
14240 : : as a barrier and we will fall back to NOWRAP being false.
14241 : : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14242 : : as not wrapping even though they are generally using unsigned arithmetic. */
14243 : :
14244 : : bool
14245 : 1544486 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14246 : : {
14247 : 1544486 : gimple *stmt;
14248 : 1544486 : tree op1, op2;
14249 : :
14250 : 1544486 : if (operand_equal_p (top, bottom, 0))
14251 : : return true;
14252 : :
14253 : 1075674 : if (TREE_CODE (type) != INTEGER_TYPE)
14254 : : return false;
14255 : :
14256 : 1075669 : switch (TREE_CODE (top))
14257 : : {
14258 : 636 : case BIT_AND_EXPR:
14259 : : /* Bitwise and provides a power of two multiple. If the mask is
14260 : : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14261 : 636 : if (!integer_pow2p (bottom))
14262 : : return false;
14263 : 636 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14264 : 636 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14265 : :
14266 : 377199 : case MULT_EXPR:
14267 : : /* If the multiplication can wrap we cannot recurse further unless
14268 : : the bottom is a power of two which is where wrapping does not
14269 : : matter. */
14270 : 377199 : if (!nowrap
14271 : 13740 : && !TYPE_OVERFLOW_UNDEFINED (type)
14272 : 381760 : && !integer_pow2p (bottom))
14273 : : return false;
14274 : 376766 : if (TREE_CODE (bottom) == INTEGER_CST)
14275 : : {
14276 : 375050 : op1 = TREE_OPERAND (top, 0);
14277 : 375050 : op2 = TREE_OPERAND (top, 1);
14278 : 375050 : if (TREE_CODE (op1) == INTEGER_CST)
14279 : 0 : std::swap (op1, op2);
14280 : 375050 : if (TREE_CODE (op2) == INTEGER_CST)
14281 : : {
14282 : 365074 : if (multiple_of_p (type, op2, bottom, nowrap))
14283 : : return true;
14284 : : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14285 : 3007 : if (multiple_of_p (type, bottom, op2, nowrap))
14286 : : {
14287 : 1900 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14288 : 1900 : wi::to_widest (op2));
14289 : 1900 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14290 : : {
14291 : 1900 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14292 : 1900 : return multiple_of_p (type, op1, op2, nowrap);
14293 : : }
14294 : 1900 : }
14295 : 1107 : return multiple_of_p (type, op1, bottom, nowrap);
14296 : : }
14297 : : }
14298 : 11692 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14299 : 11692 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14300 : :
14301 : 403 : case LSHIFT_EXPR:
14302 : : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14303 : 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14304 : : {
14305 : 403 : op1 = TREE_OPERAND (top, 1);
14306 : 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14307 : : {
14308 : 403 : wide_int mul_op
14309 : 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14310 : 806 : return multiple_of_p (type,
14311 : 806 : wide_int_to_tree (type, mul_op), bottom,
14312 : : nowrap);
14313 : 403 : }
14314 : : }
14315 : : return false;
14316 : :
14317 : 214130 : case MINUS_EXPR:
14318 : 214130 : case PLUS_EXPR:
14319 : : /* If the addition or subtraction can wrap we cannot recurse further
14320 : : unless bottom is a power of two which is where wrapping does not
14321 : : matter. */
14322 : 214130 : if (!nowrap
14323 : 171623 : && !TYPE_OVERFLOW_UNDEFINED (type)
14324 : 384318 : && !integer_pow2p (bottom))
14325 : : return false;
14326 : :
14327 : : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14328 : : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14329 : : but 0xfffffffd is not. */
14330 : 187644 : op1 = TREE_OPERAND (top, 1);
14331 : 187644 : if (TREE_CODE (top) == PLUS_EXPR
14332 : 181721 : && nowrap
14333 : 36671 : && TYPE_UNSIGNED (type)
14334 : 223616 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14335 : 31306 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14336 : :
14337 : : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14338 : : precisely, so be conservative here checking if both op0 and op1
14339 : : are multiple of bottom. Note we check the second operand first
14340 : : since it's usually simpler. */
14341 : 187644 : return (multiple_of_p (type, op1, bottom, nowrap)
14342 : 187644 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14343 : :
14344 : 139773 : CASE_CONVERT:
14345 : : /* Can't handle conversions from non-integral or wider integral type. */
14346 : 139773 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14347 : 139773 : || (TYPE_PRECISION (type)
14348 : 38663 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14349 : : return false;
14350 : : /* NOWRAP only extends to operations in the outermost type so
14351 : : make sure to strip it off here. */
14352 : 38355 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14353 : 76710 : TREE_OPERAND (top, 0), bottom, false);
14354 : :
14355 : 12424 : case SAVE_EXPR:
14356 : 12424 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14357 : :
14358 : 86 : case COND_EXPR:
14359 : 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14360 : 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14361 : :
14362 : 131560 : case INTEGER_CST:
14363 : 131560 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14364 : 2724 : return false;
14365 : 128836 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14366 : : SIGNED);
14367 : :
14368 : 65452 : case SSA_NAME:
14369 : 65452 : if (TREE_CODE (bottom) == INTEGER_CST
14370 : 62489 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14371 : 127941 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14372 : : {
14373 : 27637 : enum tree_code code = gimple_assign_rhs_code (stmt);
14374 : :
14375 : : /* Check for special cases to see if top is defined as multiple
14376 : : of bottom:
14377 : :
14378 : : top = (X & ~(bottom - 1) ; bottom is power of 2
14379 : :
14380 : : or
14381 : :
14382 : : Y = X % bottom
14383 : : top = X - Y. */
14384 : 27637 : if (code == BIT_AND_EXPR
14385 : 296 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14386 : 296 : && TREE_CODE (op2) == INTEGER_CST
14387 : 188 : && integer_pow2p (bottom)
14388 : 27825 : && wi::multiple_of_p (wi::to_widest (op2),
14389 : 188 : wi::to_widest (bottom), SIGNED))
14390 : 179 : return true;
14391 : :
14392 : 27458 : op1 = gimple_assign_rhs1 (stmt);
14393 : 27458 : if (code == MINUS_EXPR
14394 : 2810 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14395 : 2810 : && TREE_CODE (op2) == SSA_NAME
14396 : 2810 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14397 : 2810 : && gimple_code (stmt) == GIMPLE_ASSIGN
14398 : 2430 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14399 : 62 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14400 : 27520 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14401 : : return true;
14402 : : }
14403 : :
14404 : : /* fall through */
14405 : :
14406 : : default:
14407 : : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14408 : : return multiple_p (wi::to_poly_widest (top),
14409 : : wi::to_poly_widest (bottom));
14410 : :
14411 : : return false;
14412 : : }
14413 : : }
14414 : :
14415 : : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14416 : : This function returns true for integer expressions, and returns
14417 : : false if uncertain. */
14418 : :
14419 : : bool
14420 : 620769 : tree_expr_finite_p (const_tree x)
14421 : : {
14422 : 620773 : machine_mode mode = element_mode (x);
14423 : 620773 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14424 : : return true;
14425 : 620579 : switch (TREE_CODE (x))
14426 : : {
14427 : 592 : case REAL_CST:
14428 : 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14429 : 0 : case COMPLEX_CST:
14430 : 0 : return tree_expr_finite_p (TREE_REALPART (x))
14431 : 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14432 : : case FLOAT_EXPR:
14433 : : return true;
14434 : 4 : case ABS_EXPR:
14435 : 4 : case CONVERT_EXPR:
14436 : 4 : case NON_LVALUE_EXPR:
14437 : 4 : case NEGATE_EXPR:
14438 : 4 : case SAVE_EXPR:
14439 : 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14440 : 0 : case MIN_EXPR:
14441 : 0 : case MAX_EXPR:
14442 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14443 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14444 : 0 : case COND_EXPR:
14445 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14446 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14447 : 38 : case CALL_EXPR:
14448 : 38 : switch (get_call_combined_fn (x))
14449 : : {
14450 : 0 : CASE_CFN_FABS:
14451 : 0 : CASE_CFN_FABS_FN:
14452 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14453 : 0 : CASE_CFN_FMAX:
14454 : 0 : CASE_CFN_FMAX_FN:
14455 : 0 : CASE_CFN_FMIN:
14456 : 0 : CASE_CFN_FMIN_FN:
14457 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14458 : 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14459 : : default:
14460 : : return false;
14461 : : }
14462 : :
14463 : : default:
14464 : : return false;
14465 : : }
14466 : : }
14467 : :
14468 : : /* Return true if expression X evaluates to an infinity.
14469 : : This function returns false for integer expressions. */
14470 : :
14471 : : bool
14472 : 877338 : tree_expr_infinite_p (const_tree x)
14473 : : {
14474 : 877788 : if (!HONOR_INFINITIES (x))
14475 : : return false;
14476 : 877673 : switch (TREE_CODE (x))
14477 : : {
14478 : 0 : case REAL_CST:
14479 : 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14480 : 450 : case ABS_EXPR:
14481 : 450 : case NEGATE_EXPR:
14482 : 450 : case NON_LVALUE_EXPR:
14483 : 450 : case SAVE_EXPR:
14484 : 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14485 : 0 : case COND_EXPR:
14486 : 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14487 : 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14488 : : default:
14489 : : return false;
14490 : : }
14491 : : }
14492 : :
14493 : : /* Return true if expression X could evaluate to an infinity.
14494 : : This function returns false for integer expressions, and returns
14495 : : true if uncertain. */
14496 : :
14497 : : bool
14498 : 387608 : tree_expr_maybe_infinite_p (const_tree x)
14499 : : {
14500 : 387616 : if (!HONOR_INFINITIES (x))
14501 : : return false;
14502 : 387289 : switch (TREE_CODE (x))
14503 : : {
14504 : 192 : case REAL_CST:
14505 : 192 : return real_isinf (TREE_REAL_CST_PTR (x));
14506 : : case FLOAT_EXPR:
14507 : : return false;
14508 : 8 : case ABS_EXPR:
14509 : 8 : case NEGATE_EXPR:
14510 : 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14511 : 1 : case COND_EXPR:
14512 : 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14513 : 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14514 : : default:
14515 : : return true;
14516 : : }
14517 : : }
14518 : :
14519 : : /* Return true if expression X evaluates to a signaling NaN.
14520 : : This function returns false for integer expressions. */
14521 : :
14522 : : bool
14523 : 389 : tree_expr_signaling_nan_p (const_tree x)
14524 : : {
14525 : 389 : if (!HONOR_SNANS (x))
14526 : : return false;
14527 : 124 : switch (TREE_CODE (x))
14528 : : {
14529 : 124 : case REAL_CST:
14530 : 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14531 : 0 : case NON_LVALUE_EXPR:
14532 : 0 : case SAVE_EXPR:
14533 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14534 : 0 : case COND_EXPR:
14535 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14536 : 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14537 : : default:
14538 : : return false;
14539 : : }
14540 : : }
14541 : :
14542 : : /* Return true if expression X could evaluate to a signaling NaN.
14543 : : This function returns false for integer expressions, and returns
14544 : : true if uncertain. */
14545 : :
14546 : : bool
14547 : 728217 : tree_expr_maybe_signaling_nan_p (const_tree x)
14548 : : {
14549 : 728217 : if (!HONOR_SNANS (x))
14550 : : return false;
14551 : 5028 : switch (TREE_CODE (x))
14552 : : {
14553 : 1452 : case REAL_CST:
14554 : 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14555 : : case FLOAT_EXPR:
14556 : : return false;
14557 : 0 : case ABS_EXPR:
14558 : 0 : case CONVERT_EXPR:
14559 : 0 : case NEGATE_EXPR:
14560 : 0 : case NON_LVALUE_EXPR:
14561 : 0 : case SAVE_EXPR:
14562 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14563 : 0 : case MIN_EXPR:
14564 : 0 : case MAX_EXPR:
14565 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14566 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14567 : 0 : case COND_EXPR:
14568 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14569 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14570 : 0 : case CALL_EXPR:
14571 : 0 : switch (get_call_combined_fn (x))
14572 : : {
14573 : 0 : CASE_CFN_FABS:
14574 : 0 : CASE_CFN_FABS_FN:
14575 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14576 : 0 : CASE_CFN_FMAX:
14577 : 0 : CASE_CFN_FMAX_FN:
14578 : 0 : CASE_CFN_FMIN:
14579 : 0 : CASE_CFN_FMIN_FN:
14580 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14581 : 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14582 : : default:
14583 : : return true;
14584 : : }
14585 : : default:
14586 : : return true;
14587 : : }
14588 : : }
14589 : :
14590 : : /* Return true if expression X evaluates to a NaN.
14591 : : This function returns false for integer expressions. */
14592 : :
14593 : : bool
14594 : 4176954 : tree_expr_nan_p (const_tree x)
14595 : : {
14596 : 4572296 : if (!HONOR_NANS (x))
14597 : : return false;
14598 : 4571946 : switch (TREE_CODE (x))
14599 : : {
14600 : 3792 : case REAL_CST:
14601 : 3792 : return real_isnan (TREE_REAL_CST_PTR (x));
14602 : 395342 : case NON_LVALUE_EXPR:
14603 : 395342 : case SAVE_EXPR:
14604 : 395342 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14605 : 900 : case COND_EXPR:
14606 : 900 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14607 : 900 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14608 : : default:
14609 : : return false;
14610 : : }
14611 : : }
14612 : :
14613 : : /* Return true if expression X could evaluate to a NaN.
14614 : : This function returns false for integer expressions, and returns
14615 : : true if uncertain. */
14616 : :
14617 : : bool
14618 : 5121226 : tree_expr_maybe_nan_p (const_tree x)
14619 : : {
14620 : 7175501 : if (!HONOR_NANS (x))
14621 : : return false;
14622 : 7001125 : switch (TREE_CODE (x))
14623 : : {
14624 : 3288 : case REAL_CST:
14625 : 3288 : return real_isnan (TREE_REAL_CST_PTR (x));
14626 : : case FLOAT_EXPR:
14627 : : return false;
14628 : 13851 : case PLUS_EXPR:
14629 : 13851 : case MINUS_EXPR:
14630 : 13851 : case MULT_EXPR:
14631 : 13851 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14632 : 13851 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14633 : 2054275 : case ABS_EXPR:
14634 : 2054275 : case CONVERT_EXPR:
14635 : 2054275 : case NEGATE_EXPR:
14636 : 2054275 : case NON_LVALUE_EXPR:
14637 : 2054275 : case SAVE_EXPR:
14638 : 2054275 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14639 : 176 : case MIN_EXPR:
14640 : 176 : case MAX_EXPR:
14641 : 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14642 : 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14643 : 557 : case COND_EXPR:
14644 : 557 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14645 : 557 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14646 : 1082 : case CALL_EXPR:
14647 : 1082 : switch (get_call_combined_fn (x))
14648 : : {
14649 : 0 : CASE_CFN_FABS:
14650 : 0 : CASE_CFN_FABS_FN:
14651 : 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14652 : 108 : CASE_CFN_FMAX:
14653 : 108 : CASE_CFN_FMAX_FN:
14654 : 108 : CASE_CFN_FMIN:
14655 : 108 : CASE_CFN_FMIN_FN:
14656 : 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14657 : 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14658 : : default:
14659 : : return true;
14660 : : }
14661 : : default:
14662 : : return true;
14663 : : }
14664 : : }
14665 : :
14666 : : /* Return true if expression X could evaluate to -0.0.
14667 : : This function returns true if uncertain. */
14668 : :
14669 : : bool
14670 : 602408 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14671 : : {
14672 : 602408 : if (!HONOR_SIGNED_ZEROS (x))
14673 : : return false;
14674 : 602408 : switch (TREE_CODE (x))
14675 : : {
14676 : 0 : case REAL_CST:
14677 : 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14678 : : case INTEGER_CST:
14679 : : case FLOAT_EXPR:
14680 : : case ABS_EXPR:
14681 : : return false;
14682 : 0 : case NON_LVALUE_EXPR:
14683 : 0 : case SAVE_EXPR:
14684 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14685 : 0 : case COND_EXPR:
14686 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14687 : 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14688 : 1 : case CALL_EXPR:
14689 : 1 : switch (get_call_combined_fn (x))
14690 : : {
14691 : : CASE_CFN_FABS:
14692 : : CASE_CFN_FABS_FN:
14693 : : return false;
14694 : : default:
14695 : : break;
14696 : : }
14697 : : default:
14698 : : break;
14699 : : }
14700 : : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14701 : : * but currently those predicates require tree and not const_tree. */
14702 : : return true;
14703 : : }
14704 : :
14705 : : #define tree_expr_nonnegative_warnv_p(X, Y) \
14706 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14707 : :
14708 : : #define RECURSE(X) \
14709 : : ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14710 : :
14711 : : /* Return true if CODE or TYPE is known to be non-negative. */
14712 : :
14713 : : static bool
14714 : 40118547 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14715 : : {
14716 : 40118547 : if (!VECTOR_TYPE_P (type)
14717 : 40099721 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14718 : 80217295 : && truth_value_p (code))
14719 : : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14720 : : have a signed:1 type (where the value is -1 and 0). */
14721 : : return true;
14722 : : return false;
14723 : : }
14724 : :
14725 : : /* Return true if (CODE OP0) is known to be non-negative. If the return
14726 : : value is based on the assumption that signed overflow is undefined,
14727 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14728 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14729 : :
14730 : : bool
14731 : 14463791 : tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14732 : : bool *strict_overflow_p, int depth)
14733 : : {
14734 : 14463791 : if (TYPE_UNSIGNED (type))
14735 : : return true;
14736 : :
14737 : 5659272 : switch (code)
14738 : : {
14739 : 332882 : case ABS_EXPR:
14740 : : /* We can't return 1 if flag_wrapv is set because
14741 : : ABS_EXPR<INT_MIN> = INT_MIN. */
14742 : 332882 : if (!ANY_INTEGRAL_TYPE_P (type))
14743 : : return true;
14744 : 18286 : if (TYPE_OVERFLOW_UNDEFINED (type))
14745 : : {
14746 : 17338 : *strict_overflow_p = true;
14747 : 17338 : return true;
14748 : : }
14749 : : break;
14750 : :
14751 : 225008 : case NON_LVALUE_EXPR:
14752 : 225008 : case FLOAT_EXPR:
14753 : 225008 : case FIX_TRUNC_EXPR:
14754 : 225008 : return RECURSE (op0);
14755 : :
14756 : 5000804 : CASE_CONVERT:
14757 : 5000804 : {
14758 : 5000804 : tree inner_type = TREE_TYPE (op0);
14759 : 5000804 : tree outer_type = type;
14760 : :
14761 : 5000804 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14762 : : {
14763 : 318440 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14764 : 318440 : return RECURSE (op0);
14765 : 0 : if (INTEGRAL_TYPE_P (inner_type))
14766 : : {
14767 : 0 : if (TYPE_UNSIGNED (inner_type))
14768 : : return true;
14769 : 0 : return RECURSE (op0);
14770 : : }
14771 : : }
14772 : 4682364 : else if (INTEGRAL_TYPE_P (outer_type))
14773 : : {
14774 : 4682327 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14775 : 0 : return RECURSE (op0);
14776 : 4682327 : if (INTEGRAL_TYPE_P (inner_type))
14777 : 4514577 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14778 : 4514577 : && TYPE_UNSIGNED (inner_type);
14779 : : }
14780 : : }
14781 : : break;
14782 : :
14783 : 100578 : default:
14784 : 100578 : return tree_simple_nonnegative_warnv_p (code, type);
14785 : : }
14786 : :
14787 : : /* We don't know sign of `t', so be conservative and return false. */
14788 : : return false;
14789 : : }
14790 : :
14791 : : /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14792 : : value is based on the assumption that signed overflow is undefined,
14793 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14794 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14795 : :
14796 : : bool
14797 : 42993587 : tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14798 : : tree op1, bool *strict_overflow_p,
14799 : : int depth)
14800 : : {
14801 : 42993587 : if (TYPE_UNSIGNED (type))
14802 : : return true;
14803 : :
14804 : 16498212 : switch (code)
14805 : : {
14806 : 6125448 : case POINTER_PLUS_EXPR:
14807 : 6125448 : case PLUS_EXPR:
14808 : 6125448 : if (FLOAT_TYPE_P (type))
14809 : 69621 : return RECURSE (op0) && RECURSE (op1);
14810 : :
14811 : : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14812 : : both unsigned and at least 2 bits shorter than the result. */
14813 : 6055827 : if (TREE_CODE (type) == INTEGER_TYPE
14814 : 6049397 : && TREE_CODE (op0) == NOP_EXPR
14815 : 7788 : && TREE_CODE (op1) == NOP_EXPR)
14816 : : {
14817 : 206 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14818 : 206 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14819 : 206 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14820 : 313 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14821 : : {
14822 : 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14823 : 95 : TYPE_PRECISION (inner2)) + 1;
14824 : 95 : return prec < TYPE_PRECISION (type);
14825 : : }
14826 : : }
14827 : : break;
14828 : :
14829 : 1504604 : case MULT_EXPR:
14830 : 1504604 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14831 : : {
14832 : : /* x * x is always non-negative for floating point x
14833 : : or without overflow. */
14834 : 1442659 : if (operand_equal_p (op0, op1, 0)
14835 : 1442659 : || (RECURSE (op0) && RECURSE (op1)))
14836 : : {
14837 : 1231 : if (ANY_INTEGRAL_TYPE_P (type)
14838 : 13741 : && TYPE_OVERFLOW_UNDEFINED (type))
14839 : 12510 : *strict_overflow_p = true;
14840 : 13720 : return true;
14841 : : }
14842 : : }
14843 : :
14844 : : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14845 : : both unsigned and their total bits is shorter than the result. */
14846 : 1490884 : if (TREE_CODE (type) == INTEGER_TYPE
14847 : 1411652 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14848 : 152 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14849 : : {
14850 : 116 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14851 : 116 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14852 : 116 : : TREE_TYPE (op0);
14853 : 116 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14854 : 116 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14855 : 116 : : TREE_TYPE (op1);
14856 : :
14857 : 116 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14858 : 116 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14859 : :
14860 : 116 : if (TREE_CODE (op0) == INTEGER_CST)
14861 : 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14862 : :
14863 : 116 : if (TREE_CODE (op1) == INTEGER_CST)
14864 : 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14865 : :
14866 : 116 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14867 : 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14868 : : {
14869 : 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14870 : 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14871 : 0 : : TYPE_PRECISION (inner0);
14872 : :
14873 : 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14874 : 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14875 : 0 : : TYPE_PRECISION (inner1);
14876 : :
14877 : 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14878 : : }
14879 : : }
14880 : : return false;
14881 : :
14882 : 96584 : case BIT_AND_EXPR:
14883 : 96584 : return RECURSE (op0) || RECURSE (op1);
14884 : :
14885 : 81279 : case MAX_EXPR:
14886 : : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14887 : : things. */
14888 : 81279 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14889 : 76 : return RECURSE (op0) && RECURSE (op1);
14890 : 81203 : return RECURSE (op0) || RECURSE (op1);
14891 : :
14892 : 819297 : case BIT_IOR_EXPR:
14893 : 819297 : case BIT_XOR_EXPR:
14894 : 819297 : case MIN_EXPR:
14895 : 819297 : case RDIV_EXPR:
14896 : 819297 : case TRUNC_DIV_EXPR:
14897 : 819297 : case CEIL_DIV_EXPR:
14898 : 819297 : case FLOOR_DIV_EXPR:
14899 : 819297 : case ROUND_DIV_EXPR:
14900 : 819297 : return RECURSE (op0) && RECURSE (op1);
14901 : :
14902 : 108347 : case TRUNC_MOD_EXPR:
14903 : 108347 : return RECURSE (op0);
14904 : :
14905 : 394 : case FLOOR_MOD_EXPR:
14906 : 394 : return RECURSE (op1);
14907 : :
14908 : 7762259 : case CEIL_MOD_EXPR:
14909 : 7762259 : case ROUND_MOD_EXPR:
14910 : 7762259 : default:
14911 : 7762259 : return tree_simple_nonnegative_warnv_p (code, type);
14912 : : }
14913 : :
14914 : : /* We don't know sign of `t', so be conservative and return false. */
14915 : : return false;
14916 : : }
14917 : :
14918 : : /* Return true if T is known to be non-negative. If the return
14919 : : value is based on the assumption that signed overflow is undefined,
14920 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14921 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14922 : :
14923 : : bool
14924 : 50696645 : tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14925 : : {
14926 : 50696645 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14927 : : return true;
14928 : :
14929 : 33214468 : switch (TREE_CODE (t))
14930 : : {
14931 : 3526745 : case INTEGER_CST:
14932 : 3526745 : return tree_int_cst_sgn (t) >= 0;
14933 : :
14934 : 1012408 : case REAL_CST:
14935 : 1012408 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14936 : :
14937 : 0 : case FIXED_CST:
14938 : 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14939 : :
14940 : 490 : case COND_EXPR:
14941 : 490 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14942 : :
14943 : 18263238 : case SSA_NAME:
14944 : : /* Limit the depth of recursion to avoid quadratic behavior.
14945 : : This is expected to catch almost all occurrences in practice.
14946 : : If this code misses important cases that unbounded recursion
14947 : : would not, passes that need this information could be revised
14948 : : to provide it through dataflow propagation. */
14949 : 18263238 : return (!name_registered_for_update_p (t)
14950 : 18263221 : && depth < param_max_ssa_name_query_depth
14951 : 35084159 : && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14952 : : strict_overflow_p, depth));
14953 : :
14954 : 10411587 : default:
14955 : 10411587 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14956 : : }
14957 : : }
14958 : :
14959 : : /* Return true if T is known to be non-negative. If the return
14960 : : value is based on the assumption that signed overflow is undefined,
14961 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14962 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14963 : :
14964 : : bool
14965 : 22011627 : tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14966 : : bool *strict_overflow_p, int depth)
14967 : : {
14968 : 22011627 : switch (fn)
14969 : : {
14970 : : CASE_CFN_ACOS:
14971 : : CASE_CFN_ACOS_FN:
14972 : : CASE_CFN_ACOSH:
14973 : : CASE_CFN_ACOSH_FN:
14974 : : CASE_CFN_ACOSPI:
14975 : : CASE_CFN_ACOSPI_FN:
14976 : : CASE_CFN_CABS:
14977 : : CASE_CFN_CABS_FN:
14978 : : CASE_CFN_COSH:
14979 : : CASE_CFN_COSH_FN:
14980 : : CASE_CFN_ERFC:
14981 : : CASE_CFN_ERFC_FN:
14982 : : CASE_CFN_EXP:
14983 : : CASE_CFN_EXP_FN:
14984 : : CASE_CFN_EXP10:
14985 : : CASE_CFN_EXP2:
14986 : : CASE_CFN_EXP2_FN:
14987 : : CASE_CFN_FABS:
14988 : : CASE_CFN_FABS_FN:
14989 : : CASE_CFN_FDIM:
14990 : : CASE_CFN_FDIM_FN:
14991 : : CASE_CFN_HYPOT:
14992 : : CASE_CFN_HYPOT_FN:
14993 : : CASE_CFN_POW10:
14994 : : CASE_CFN_FFS:
14995 : : CASE_CFN_PARITY:
14996 : : CASE_CFN_POPCOUNT:
14997 : : CASE_CFN_CLRSB:
14998 : : case CFN_BUILT_IN_BSWAP16:
14999 : : case CFN_BUILT_IN_BSWAP32:
15000 : : case CFN_BUILT_IN_BSWAP64:
15001 : : case CFN_BUILT_IN_BSWAP128:
15002 : : /* Always true. */
15003 : : return true;
15004 : :
15005 : 1071 : CASE_CFN_CLZ:
15006 : 1071 : CASE_CFN_CTZ:
15007 : 1071 : if (arg1)
15008 : 0 : return RECURSE (arg1);
15009 : : return true;
15010 : :
15011 : 1024 : CASE_CFN_SQRT:
15012 : 1024 : CASE_CFN_SQRT_FN:
15013 : : /* sqrt(-0.0) is -0.0. */
15014 : 1024 : if (!HONOR_SIGNED_ZEROS (type))
15015 : : return true;
15016 : 987 : return RECURSE (arg0);
15017 : :
15018 : 123234 : CASE_CFN_ASINH:
15019 : 123234 : CASE_CFN_ASINH_FN:
15020 : 123234 : CASE_CFN_ASINPI:
15021 : 123234 : CASE_CFN_ASINPI_FN:
15022 : 123234 : CASE_CFN_ATAN:
15023 : 123234 : CASE_CFN_ATAN_FN:
15024 : 123234 : CASE_CFN_ATANH:
15025 : 123234 : CASE_CFN_ATANH_FN:
15026 : 123234 : CASE_CFN_ATANPI:
15027 : 123234 : CASE_CFN_ATANPI_FN:
15028 : 123234 : CASE_CFN_CBRT:
15029 : 123234 : CASE_CFN_CBRT_FN:
15030 : 123234 : CASE_CFN_CEIL:
15031 : 123234 : CASE_CFN_CEIL_FN:
15032 : 123234 : CASE_CFN_ERF:
15033 : 123234 : CASE_CFN_ERF_FN:
15034 : 123234 : CASE_CFN_EXPM1:
15035 : 123234 : CASE_CFN_EXPM1_FN:
15036 : 123234 : CASE_CFN_FLOOR:
15037 : 123234 : CASE_CFN_FLOOR_FN:
15038 : 123234 : CASE_CFN_FMOD:
15039 : 123234 : CASE_CFN_FMOD_FN:
15040 : 123234 : CASE_CFN_FREXP:
15041 : 123234 : CASE_CFN_FREXP_FN:
15042 : 123234 : CASE_CFN_ICEIL:
15043 : 123234 : CASE_CFN_IFLOOR:
15044 : 123234 : CASE_CFN_IRINT:
15045 : 123234 : CASE_CFN_IROUND:
15046 : 123234 : CASE_CFN_LCEIL:
15047 : 123234 : CASE_CFN_LDEXP:
15048 : 123234 : CASE_CFN_LFLOOR:
15049 : 123234 : CASE_CFN_LLCEIL:
15050 : 123234 : CASE_CFN_LLFLOOR:
15051 : 123234 : CASE_CFN_LLRINT:
15052 : 123234 : CASE_CFN_LLRINT_FN:
15053 : 123234 : CASE_CFN_LLROUND:
15054 : 123234 : CASE_CFN_LLROUND_FN:
15055 : 123234 : CASE_CFN_LRINT:
15056 : 123234 : CASE_CFN_LRINT_FN:
15057 : 123234 : CASE_CFN_LROUND:
15058 : 123234 : CASE_CFN_LROUND_FN:
15059 : 123234 : CASE_CFN_MODF:
15060 : 123234 : CASE_CFN_MODF_FN:
15061 : 123234 : CASE_CFN_NEARBYINT:
15062 : 123234 : CASE_CFN_NEARBYINT_FN:
15063 : 123234 : CASE_CFN_RINT:
15064 : 123234 : CASE_CFN_RINT_FN:
15065 : 123234 : CASE_CFN_ROUND:
15066 : 123234 : CASE_CFN_ROUND_FN:
15067 : 123234 : CASE_CFN_ROUNDEVEN:
15068 : 123234 : CASE_CFN_ROUNDEVEN_FN:
15069 : 123234 : CASE_CFN_SCALB:
15070 : 123234 : CASE_CFN_SCALBLN:
15071 : 123234 : CASE_CFN_SCALBLN_FN:
15072 : 123234 : CASE_CFN_SCALBN:
15073 : 123234 : CASE_CFN_SCALBN_FN:
15074 : 123234 : CASE_CFN_SIGNBIT:
15075 : 123234 : CASE_CFN_SIGNIFICAND:
15076 : 123234 : CASE_CFN_SINH:
15077 : 123234 : CASE_CFN_SINH_FN:
15078 : 123234 : CASE_CFN_TANH:
15079 : 123234 : CASE_CFN_TANH_FN:
15080 : 123234 : CASE_CFN_TRUNC:
15081 : 123234 : CASE_CFN_TRUNC_FN:
15082 : : /* True if the 1st argument is nonnegative. */
15083 : 123234 : return RECURSE (arg0);
15084 : :
15085 : 1335 : CASE_CFN_FMAX:
15086 : 1335 : CASE_CFN_FMAX_FN:
15087 : : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15088 : : things. In the presence of sNaNs, we're only guaranteed to be
15089 : : non-negative if both operands are non-negative. In the presence
15090 : : of qNaNs, we're non-negative if either operand is non-negative
15091 : : and can't be a qNaN, or if both operands are non-negative. */
15092 : 1335 : if (tree_expr_maybe_signaling_nan_p (arg0)
15093 : 1335 : || tree_expr_maybe_signaling_nan_p (arg1))
15094 : 136 : return RECURSE (arg0) && RECURSE (arg1);
15095 : 1199 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15096 : 332 : || RECURSE (arg1))
15097 : 867 : : (RECURSE (arg1)
15098 : 867 : && !tree_expr_maybe_nan_p (arg1));
15099 : :
15100 : 946 : CASE_CFN_FMIN:
15101 : 946 : CASE_CFN_FMIN_FN:
15102 : : /* True if the 1st AND 2nd arguments are nonnegative. */
15103 : 946 : return RECURSE (arg0) && RECURSE (arg1);
15104 : :
15105 : 828 : CASE_CFN_COPYSIGN:
15106 : 828 : CASE_CFN_COPYSIGN_FN:
15107 : : /* True if the 2nd argument is nonnegative. */
15108 : 828 : return RECURSE (arg1);
15109 : :
15110 : 2336 : CASE_CFN_POWI:
15111 : : /* True if the 1st argument is nonnegative or the second
15112 : : argument is an even integer. */
15113 : 2336 : if (TREE_CODE (arg1) == INTEGER_CST
15114 : 2336 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15115 : : return true;
15116 : 2255 : return RECURSE (arg0);
15117 : :
15118 : 4906 : CASE_CFN_POW:
15119 : 4906 : CASE_CFN_POW_FN:
15120 : : /* True if the 1st argument is nonnegative or the second
15121 : : argument is an even integer valued real. */
15122 : 4906 : if (TREE_CODE (arg1) == REAL_CST)
15123 : : {
15124 : 2134 : REAL_VALUE_TYPE c;
15125 : 2134 : HOST_WIDE_INT n;
15126 : :
15127 : 2134 : c = TREE_REAL_CST (arg1);
15128 : 2134 : n = real_to_integer (&c);
15129 : 2134 : if ((n & 1) == 0)
15130 : : {
15131 : 1512 : REAL_VALUE_TYPE cint;
15132 : 1512 : real_from_integer (&cint, VOIDmode, n, SIGNED);
15133 : 1512 : if (real_identical (&c, &cint))
15134 : 502 : return true;
15135 : : }
15136 : : }
15137 : 4404 : return RECURSE (arg0);
15138 : :
15139 : 21842463 : default:
15140 : 21842463 : break;
15141 : : }
15142 : 21842463 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15143 : : }
15144 : :
15145 : : /* Return true if T is known to be non-negative. If the return
15146 : : value is based on the assumption that signed overflow is undefined,
15147 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15148 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15149 : :
15150 : : static bool
15151 : 1013565 : tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15152 : : {
15153 : 1013565 : enum tree_code code = TREE_CODE (t);
15154 : 1013565 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15155 : : return true;
15156 : :
15157 : 816040 : switch (code)
15158 : : {
15159 : 217 : case TARGET_EXPR:
15160 : 217 : {
15161 : 217 : tree temp = TARGET_EXPR_SLOT (t);
15162 : 217 : t = TARGET_EXPR_INITIAL (t);
15163 : :
15164 : : /* If the initializer is non-void, then it's a normal expression
15165 : : that will be assigned to the slot. */
15166 : 217 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15167 : 0 : return RECURSE (t);
15168 : :
15169 : : /* Otherwise, the initializer sets the slot in some way. One common
15170 : : way is an assignment statement at the end of the initializer. */
15171 : 419 : while (1)
15172 : : {
15173 : 419 : if (TREE_CODE (t) == BIND_EXPR)
15174 : 202 : t = expr_last (BIND_EXPR_BODY (t));
15175 : 217 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15176 : 217 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15177 : 0 : t = expr_last (TREE_OPERAND (t, 0));
15178 : 217 : else if (TREE_CODE (t) == STATEMENT_LIST)
15179 : 0 : t = expr_last (t);
15180 : : else
15181 : : break;
15182 : : }
15183 : 217 : if (TREE_CODE (t) == MODIFY_EXPR
15184 : 217 : && TREE_OPERAND (t, 0) == temp)
15185 : 202 : return RECURSE (TREE_OPERAND (t, 1));
15186 : :
15187 : : return false;
15188 : : }
15189 : :
15190 : 380123 : case CALL_EXPR:
15191 : 380123 : {
15192 : 380123 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15193 : 380123 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15194 : :
15195 : 380123 : return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15196 : : get_call_combined_fn (t),
15197 : : arg0,
15198 : : arg1,
15199 : 380123 : strict_overflow_p, depth);
15200 : : }
15201 : 742 : case COMPOUND_EXPR:
15202 : 742 : case MODIFY_EXPR:
15203 : 742 : return RECURSE (TREE_OPERAND (t, 1));
15204 : :
15205 : 9 : case BIND_EXPR:
15206 : 9 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15207 : :
15208 : 433289 : case SAVE_EXPR:
15209 : 433289 : return RECURSE (TREE_OPERAND (t, 0));
15210 : :
15211 : 1660 : default:
15212 : 1660 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15213 : : }
15214 : : }
15215 : :
15216 : : #undef RECURSE
15217 : : #undef tree_expr_nonnegative_warnv_p
15218 : :
15219 : : /* Return true if T is known to be non-negative. If the return
15220 : : value is based on the assumption that signed overflow is undefined,
15221 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15222 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15223 : :
15224 : : bool
15225 : 25808646 : tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15226 : : {
15227 : 25808646 : enum tree_code code;
15228 : 25808646 : if (error_operand_p (t))
15229 : : return false;
15230 : :
15231 : 25808645 : code = TREE_CODE (t);
15232 : 25808645 : switch (TREE_CODE_CLASS (code))
15233 : : {
15234 : 1115543 : case tcc_binary:
15235 : 1115543 : case tcc_comparison:
15236 : 1115543 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15237 : 1115543 : TREE_TYPE (t),
15238 : 1115543 : TREE_OPERAND (t, 0),
15239 : 1115543 : TREE_OPERAND (t, 1),
15240 : 1115543 : strict_overflow_p, depth);
15241 : :
15242 : 1700915 : case tcc_unary:
15243 : 1700915 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15244 : 1700915 : TREE_TYPE (t),
15245 : 1700915 : TREE_OPERAND (t, 0),
15246 : 1700915 : strict_overflow_p, depth);
15247 : :
15248 : 12037221 : case tcc_constant:
15249 : 12037221 : case tcc_declaration:
15250 : 12037221 : case tcc_reference:
15251 : 12037221 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15252 : :
15253 : 10954966 : default:
15254 : 10954966 : break;
15255 : : }
15256 : :
15257 : 10954966 : switch (code)
15258 : : {
15259 : 7 : case TRUTH_AND_EXPR:
15260 : 7 : case TRUTH_OR_EXPR:
15261 : 7 : case TRUTH_XOR_EXPR:
15262 : 7 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15263 : 7 : TREE_TYPE (t),
15264 : 7 : TREE_OPERAND (t, 0),
15265 : 7 : TREE_OPERAND (t, 1),
15266 : 7 : strict_overflow_p, depth);
15267 : 72 : case TRUTH_NOT_EXPR:
15268 : 72 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15269 : 72 : TREE_TYPE (t),
15270 : 72 : TREE_OPERAND (t, 0),
15271 : 72 : strict_overflow_p, depth);
15272 : :
15273 : 9941322 : case COND_EXPR:
15274 : 9941322 : case CONSTRUCTOR:
15275 : 9941322 : case OBJ_TYPE_REF:
15276 : 9941322 : case ADDR_EXPR:
15277 : 9941322 : case WITH_SIZE_EXPR:
15278 : 9941322 : case SSA_NAME:
15279 : 9941322 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15280 : :
15281 : 1013565 : default:
15282 : 1013565 : return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15283 : : }
15284 : : }
15285 : :
15286 : : /* Return true if `t' is known to be non-negative. Handle warnings
15287 : : about undefined signed overflow. */
15288 : :
15289 : : bool
15290 : 17654937 : tree_expr_nonnegative_p (tree t)
15291 : : {
15292 : 17654937 : bool ret, strict_overflow_p;
15293 : :
15294 : 17654937 : strict_overflow_p = false;
15295 : 17654937 : ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15296 : 17654937 : if (strict_overflow_p)
15297 : 17550 : fold_overflow_warning (("assuming signed overflow does not occur when "
15298 : : "determining that expression is always "
15299 : : "non-negative"),
15300 : : WARN_STRICT_OVERFLOW_MISC);
15301 : 17654937 : return ret;
15302 : : }
15303 : :
15304 : :
15305 : : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15306 : : For floating point we further ensure that T is not denormal.
15307 : : Similar logic is present in nonzero_address in rtlanal.h.
15308 : :
15309 : : If the return value is based on the assumption that signed overflow
15310 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15311 : : change *STRICT_OVERFLOW_P. */
15312 : :
15313 : : bool
15314 : 1468309 : tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15315 : : bool *strict_overflow_p)
15316 : : {
15317 : 1468309 : switch (code)
15318 : : {
15319 : 1 : case ABS_EXPR:
15320 : 1 : return tree_expr_nonzero_warnv_p (op0,
15321 : 1 : strict_overflow_p);
15322 : :
15323 : 800086 : case NOP_EXPR:
15324 : 800086 : {
15325 : 800086 : tree inner_type = TREE_TYPE (op0);
15326 : 800086 : tree outer_type = type;
15327 : :
15328 : 800086 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15329 : 800086 : && tree_expr_nonzero_warnv_p (op0,
15330 : : strict_overflow_p));
15331 : : }
15332 : 28113 : break;
15333 : :
15334 : 28113 : case NON_LVALUE_EXPR:
15335 : 28113 : return tree_expr_nonzero_warnv_p (op0,
15336 : 28113 : strict_overflow_p);
15337 : :
15338 : : default:
15339 : : break;
15340 : : }
15341 : :
15342 : : return false;
15343 : : }
15344 : :
15345 : : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15346 : : For floating point we further ensure that T is not denormal.
15347 : : Similar logic is present in nonzero_address in rtlanal.h.
15348 : :
15349 : : If the return value is based on the assumption that signed overflow
15350 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15351 : : change *STRICT_OVERFLOW_P. */
15352 : :
15353 : : bool
15354 : 3004498 : tree_binary_nonzero_warnv_p (enum tree_code code,
15355 : : tree type,
15356 : : tree op0,
15357 : : tree op1, bool *strict_overflow_p)
15358 : : {
15359 : 3004498 : bool sub_strict_overflow_p;
15360 : 3004498 : switch (code)
15361 : : {
15362 : 475940 : case POINTER_PLUS_EXPR:
15363 : 475940 : case PLUS_EXPR:
15364 : 475940 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15365 : : {
15366 : : /* With the presence of negative values it is hard
15367 : : to say something. */
15368 : 104819 : sub_strict_overflow_p = false;
15369 : 104819 : if (!tree_expr_nonnegative_warnv_p (op0,
15370 : : &sub_strict_overflow_p)
15371 : 104819 : || !tree_expr_nonnegative_warnv_p (op1,
15372 : : &sub_strict_overflow_p))
15373 : 102466 : return false;
15374 : : /* One of operands must be positive and the other non-negative. */
15375 : : /* We don't set *STRICT_OVERFLOW_P here: even if this value
15376 : : overflows, on a twos-complement machine the sum of two
15377 : : nonnegative numbers can never be zero. */
15378 : 2353 : return (tree_expr_nonzero_warnv_p (op0,
15379 : : strict_overflow_p)
15380 : 2353 : || tree_expr_nonzero_warnv_p (op1,
15381 : : strict_overflow_p));
15382 : : }
15383 : : break;
15384 : :
15385 : 21127 : case MULT_EXPR:
15386 : 21127 : if (TYPE_OVERFLOW_UNDEFINED (type))
15387 : : {
15388 : 444 : if (tree_expr_nonzero_warnv_p (op0,
15389 : : strict_overflow_p)
15390 : 444 : && tree_expr_nonzero_warnv_p (op1,
15391 : : strict_overflow_p))
15392 : : {
15393 : 0 : *strict_overflow_p = true;
15394 : 0 : return true;
15395 : : }
15396 : : }
15397 : : break;
15398 : :
15399 : 13826 : case MIN_EXPR:
15400 : 13826 : sub_strict_overflow_p = false;
15401 : 13826 : if (tree_expr_nonzero_warnv_p (op0,
15402 : : &sub_strict_overflow_p)
15403 : 13826 : && tree_expr_nonzero_warnv_p (op1,
15404 : : &sub_strict_overflow_p))
15405 : : {
15406 : 0 : if (sub_strict_overflow_p)
15407 : 0 : *strict_overflow_p = true;
15408 : : }
15409 : : break;
15410 : :
15411 : 44 : case MAX_EXPR:
15412 : 44 : sub_strict_overflow_p = false;
15413 : 44 : if (tree_expr_nonzero_warnv_p (op0,
15414 : : &sub_strict_overflow_p))
15415 : : {
15416 : 0 : if (sub_strict_overflow_p)
15417 : 0 : *strict_overflow_p = true;
15418 : :
15419 : : /* When both operands are nonzero, then MAX must be too. */
15420 : 0 : if (tree_expr_nonzero_warnv_p (op1,
15421 : : strict_overflow_p))
15422 : : return true;
15423 : :
15424 : : /* MAX where operand 0 is positive is positive. */
15425 : 0 : return tree_expr_nonnegative_warnv_p (op0,
15426 : 0 : strict_overflow_p);
15427 : : }
15428 : : /* MAX where operand 1 is positive is positive. */
15429 : 44 : else if (tree_expr_nonzero_warnv_p (op1,
15430 : : &sub_strict_overflow_p)
15431 : 44 : && tree_expr_nonnegative_warnv_p (op1,
15432 : : &sub_strict_overflow_p))
15433 : : {
15434 : 0 : if (sub_strict_overflow_p)
15435 : 0 : *strict_overflow_p = true;
15436 : 0 : return true;
15437 : : }
15438 : : break;
15439 : :
15440 : 271781 : case BIT_IOR_EXPR:
15441 : 271781 : return (tree_expr_nonzero_warnv_p (op1,
15442 : : strict_overflow_p)
15443 : 271781 : || tree_expr_nonzero_warnv_p (op0,
15444 : : strict_overflow_p));
15445 : :
15446 : : default:
15447 : : break;
15448 : : }
15449 : :
15450 : : return false;
15451 : : }
15452 : :
15453 : : /* Return true when T is an address and is known to be nonzero.
15454 : : For floating point we further ensure that T is not denormal.
15455 : : Similar logic is present in nonzero_address in rtlanal.h.
15456 : :
15457 : : If the return value is based on the assumption that signed overflow
15458 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15459 : : change *STRICT_OVERFLOW_P. */
15460 : :
15461 : : bool
15462 : 151198793 : tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15463 : : {
15464 : 151198793 : bool sub_strict_overflow_p;
15465 : 151198793 : switch (TREE_CODE (t))
15466 : : {
15467 : 1135046 : case INTEGER_CST:
15468 : 1135046 : return !integer_zerop (t);
15469 : :
15470 : 11806089 : case ADDR_EXPR:
15471 : 11806089 : {
15472 : 11806089 : tree base = TREE_OPERAND (t, 0);
15473 : :
15474 : 11806089 : if (!DECL_P (base))
15475 : 6152667 : base = get_base_address (base);
15476 : :
15477 : 11806089 : if (base && TREE_CODE (base) == TARGET_EXPR)
15478 : 775 : base = TARGET_EXPR_SLOT (base);
15479 : :
15480 : 775 : if (!base)
15481 : 0 : return false;
15482 : :
15483 : : /* For objects in symbol table check if we know they are non-zero.
15484 : : Don't do anything for variables and functions before symtab is built;
15485 : : it is quite possible that they will be declared weak later. */
15486 : 11806089 : int nonzero_addr = maybe_nonzero_address (base);
15487 : 11806089 : if (nonzero_addr >= 0)
15488 : 9220436 : return nonzero_addr;
15489 : :
15490 : : /* Constants are never weak. */
15491 : 2585653 : if (CONSTANT_CLASS_P (base))
15492 : : return true;
15493 : :
15494 : : return false;
15495 : : }
15496 : :
15497 : 31931 : case COND_EXPR:
15498 : 31931 : sub_strict_overflow_p = false;
15499 : 31931 : if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15500 : : &sub_strict_overflow_p)
15501 : 31931 : && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15502 : : &sub_strict_overflow_p))
15503 : : {
15504 : 1222 : if (sub_strict_overflow_p)
15505 : 0 : *strict_overflow_p = true;
15506 : 1222 : return true;
15507 : : }
15508 : : break;
15509 : :
15510 : 127332000 : case SSA_NAME:
15511 : 127332000 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15512 : : break;
15513 : 99159126 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15514 : :
15515 : : default:
15516 : : break;
15517 : : }
15518 : : return false;
15519 : : }
15520 : :
15521 : : #define integer_valued_real_p(X) \
15522 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15523 : :
15524 : : #define RECURSE(X) \
15525 : : ((integer_valued_real_p) (X, depth + 1))
15526 : :
15527 : : /* Return true if the floating point result of (CODE OP0) has an
15528 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15529 : : integer values. Return false for signaling NaN.
15530 : :
15531 : : DEPTH is the current nesting depth of the query. */
15532 : :
15533 : : bool
15534 : 14869 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15535 : : {
15536 : 14869 : switch (code)
15537 : : {
15538 : : case FLOAT_EXPR:
15539 : : return true;
15540 : :
15541 : 1406 : case ABS_EXPR:
15542 : 1406 : return RECURSE (op0);
15543 : :
15544 : 9841 : CASE_CONVERT:
15545 : 9841 : {
15546 : 9841 : tree type = TREE_TYPE (op0);
15547 : 9841 : if (TREE_CODE (type) == INTEGER_TYPE)
15548 : : return true;
15549 : 9841 : if (SCALAR_FLOAT_TYPE_P (type))
15550 : 9841 : return RECURSE (op0);
15551 : : break;
15552 : : }
15553 : :
15554 : : default:
15555 : : break;
15556 : : }
15557 : : return false;
15558 : : }
15559 : :
15560 : : /* Return true if the floating point result of (CODE OP0 OP1) has an
15561 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15562 : : integer values. Return false for signaling NaN.
15563 : :
15564 : : DEPTH is the current nesting depth of the query. */
15565 : :
15566 : : bool
15567 : 13236 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15568 : : {
15569 : 13236 : switch (code)
15570 : : {
15571 : 7000 : case PLUS_EXPR:
15572 : 7000 : case MINUS_EXPR:
15573 : 7000 : case MULT_EXPR:
15574 : 7000 : case MIN_EXPR:
15575 : 7000 : case MAX_EXPR:
15576 : 7000 : return RECURSE (op0) && RECURSE (op1);
15577 : :
15578 : : default:
15579 : : break;
15580 : : }
15581 : : return false;
15582 : : }
15583 : :
15584 : : /* Return true if the floating point result of calling FNDECL with arguments
15585 : : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15586 : : considered integer values. Return false for signaling NaN. If FNDECL
15587 : : takes fewer than 2 arguments, the remaining ARGn are null.
15588 : :
15589 : : DEPTH is the current nesting depth of the query. */
15590 : :
15591 : : bool
15592 : 989 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15593 : : {
15594 : 989 : switch (fn)
15595 : : {
15596 : : CASE_CFN_CEIL:
15597 : : CASE_CFN_CEIL_FN:
15598 : : CASE_CFN_FLOOR:
15599 : : CASE_CFN_FLOOR_FN:
15600 : : CASE_CFN_NEARBYINT:
15601 : : CASE_CFN_NEARBYINT_FN:
15602 : : CASE_CFN_RINT:
15603 : : CASE_CFN_RINT_FN:
15604 : : CASE_CFN_ROUND:
15605 : : CASE_CFN_ROUND_FN:
15606 : : CASE_CFN_ROUNDEVEN:
15607 : : CASE_CFN_ROUNDEVEN_FN:
15608 : : CASE_CFN_TRUNC:
15609 : : CASE_CFN_TRUNC_FN:
15610 : : return true;
15611 : :
15612 : 336 : CASE_CFN_FMIN:
15613 : 336 : CASE_CFN_FMIN_FN:
15614 : 336 : CASE_CFN_FMAX:
15615 : 336 : CASE_CFN_FMAX_FN:
15616 : 336 : return RECURSE (arg0) && RECURSE (arg1);
15617 : :
15618 : : default:
15619 : : break;
15620 : : }
15621 : : return false;
15622 : : }
15623 : :
15624 : : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15625 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15626 : : considered integer values. Return false for signaling NaN.
15627 : :
15628 : : DEPTH is the current nesting depth of the query. */
15629 : :
15630 : : bool
15631 : 126862 : integer_valued_real_single_p (tree t, int depth)
15632 : : {
15633 : 126862 : switch (TREE_CODE (t))
15634 : : {
15635 : 2128 : case REAL_CST:
15636 : 2128 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15637 : :
15638 : 0 : case COND_EXPR:
15639 : 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15640 : :
15641 : 88989 : case SSA_NAME:
15642 : : /* Limit the depth of recursion to avoid quadratic behavior.
15643 : : This is expected to catch almost all occurrences in practice.
15644 : : If this code misses important cases that unbounded recursion
15645 : : would not, passes that need this information could be revised
15646 : : to provide it through dataflow propagation. */
15647 : 88989 : return (!name_registered_for_update_p (t)
15648 : 88989 : && depth < param_max_ssa_name_query_depth
15649 : 177244 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15650 : : depth));
15651 : :
15652 : : default:
15653 : : break;
15654 : : }
15655 : : return false;
15656 : : }
15657 : :
15658 : : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15659 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15660 : : considered integer values. Return false for signaling NaN.
15661 : :
15662 : : DEPTH is the current nesting depth of the query. */
15663 : :
15664 : : static bool
15665 : 0 : integer_valued_real_invalid_p (tree t, int depth)
15666 : : {
15667 : 0 : switch (TREE_CODE (t))
15668 : : {
15669 : 0 : case COMPOUND_EXPR:
15670 : 0 : case MODIFY_EXPR:
15671 : 0 : case BIND_EXPR:
15672 : 0 : return RECURSE (TREE_OPERAND (t, 1));
15673 : :
15674 : 0 : case SAVE_EXPR:
15675 : 0 : return RECURSE (TREE_OPERAND (t, 0));
15676 : :
15677 : : default:
15678 : : break;
15679 : : }
15680 : : return false;
15681 : : }
15682 : :
15683 : : #undef RECURSE
15684 : : #undef integer_valued_real_p
15685 : :
15686 : : /* Return true if the floating point expression T has an integer value.
15687 : : We also allow +Inf, -Inf and NaN to be considered integer values.
15688 : : Return false for signaling NaN.
15689 : :
15690 : : DEPTH is the current nesting depth of the query. */
15691 : :
15692 : : bool
15693 : 95898 : integer_valued_real_p (tree t, int depth)
15694 : : {
15695 : 95898 : if (t == error_mark_node)
15696 : : return false;
15697 : :
15698 : 95898 : STRIP_ANY_LOCATION_WRAPPER (t);
15699 : :
15700 : 95898 : tree_code code = TREE_CODE (t);
15701 : 95898 : switch (TREE_CODE_CLASS (code))
15702 : : {
15703 : 0 : case tcc_binary:
15704 : 0 : case tcc_comparison:
15705 : 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15706 : 0 : TREE_OPERAND (t, 1), depth);
15707 : :
15708 : 0 : case tcc_unary:
15709 : 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15710 : :
15711 : 8317 : case tcc_constant:
15712 : 8317 : case tcc_declaration:
15713 : 8317 : case tcc_reference:
15714 : 8317 : return integer_valued_real_single_p (t, depth);
15715 : :
15716 : 87581 : default:
15717 : 87581 : break;
15718 : : }
15719 : :
15720 : 87581 : switch (code)
15721 : : {
15722 : 87581 : case COND_EXPR:
15723 : 87581 : case SSA_NAME:
15724 : 87581 : return integer_valued_real_single_p (t, depth);
15725 : :
15726 : 0 : case CALL_EXPR:
15727 : 0 : {
15728 : 0 : tree arg0 = (call_expr_nargs (t) > 0
15729 : 0 : ? CALL_EXPR_ARG (t, 0)
15730 : 0 : : NULL_TREE);
15731 : 0 : tree arg1 = (call_expr_nargs (t) > 1
15732 : 0 : ? CALL_EXPR_ARG (t, 1)
15733 : 0 : : NULL_TREE);
15734 : 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15735 : 0 : arg0, arg1, depth);
15736 : : }
15737 : :
15738 : 0 : default:
15739 : 0 : return integer_valued_real_invalid_p (t, depth);
15740 : : }
15741 : : }
15742 : :
15743 : : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15744 : : attempt to fold the expression to a constant without modifying TYPE,
15745 : : OP0 or OP1.
15746 : :
15747 : : If the expression could be simplified to a constant, then return
15748 : : the constant. If the expression would not be simplified to a
15749 : : constant, then return NULL_TREE. */
15750 : :
15751 : : tree
15752 : 15717025 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15753 : : {
15754 : 15717025 : tree tem = fold_binary (code, type, op0, op1);
15755 : 15717025 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15756 : : }
15757 : :
15758 : : /* Given the components of a unary expression CODE, TYPE and OP0,
15759 : : attempt to fold the expression to a constant without modifying
15760 : : TYPE or OP0.
15761 : :
15762 : : If the expression could be simplified to a constant, then return
15763 : : the constant. If the expression would not be simplified to a
15764 : : constant, then return NULL_TREE. */
15765 : :
15766 : : tree
15767 : 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15768 : : {
15769 : 0 : tree tem = fold_unary (code, type, op0);
15770 : 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15771 : : }
15772 : :
15773 : : /* If EXP represents referencing an element in a constant string
15774 : : (either via pointer arithmetic or array indexing), return the
15775 : : tree representing the value accessed, otherwise return NULL. */
15776 : :
15777 : : tree
15778 : 167852634 : fold_read_from_constant_string (tree exp)
15779 : : {
15780 : 167852634 : if ((INDIRECT_REF_P (exp)
15781 : 167852616 : || TREE_CODE (exp) == ARRAY_REF)
15782 : 179093958 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15783 : : {
15784 : 8066911 : tree exp1 = TREE_OPERAND (exp, 0);
15785 : 8066911 : tree index;
15786 : 8066911 : tree string;
15787 : 8066911 : location_t loc = EXPR_LOCATION (exp);
15788 : :
15789 : 8066911 : if (INDIRECT_REF_P (exp))
15790 : 0 : string = string_constant (exp1, &index, NULL, NULL);
15791 : : else
15792 : : {
15793 : 8066911 : tree low_bound = array_ref_low_bound (exp);
15794 : 8066911 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15795 : :
15796 : : /* Optimize the special-case of a zero lower bound.
15797 : :
15798 : : We convert the low_bound to sizetype to avoid some problems
15799 : : with constant folding. (E.g. suppose the lower bound is 1,
15800 : : and its mode is QI. Without the conversion,l (ARRAY
15801 : : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15802 : : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15803 : 8066911 : if (! integer_zerop (low_bound))
15804 : 149554 : index = size_diffop_loc (loc, index,
15805 : : fold_convert_loc (loc, sizetype, low_bound));
15806 : :
15807 : : string = exp1;
15808 : : }
15809 : :
15810 : 8066911 : scalar_int_mode char_mode;
15811 : 8066911 : if (string
15812 : 8066911 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15813 : 8066911 : && TREE_CODE (string) == STRING_CST
15814 : 62567 : && tree_fits_uhwi_p (index)
15815 : 56982 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15816 : 8123861 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15817 : : &char_mode)
15818 : 16133822 : && GET_MODE_SIZE (char_mode) == 1)
15819 : 110768 : return build_int_cst_type (TREE_TYPE (exp),
15820 : 55384 : (TREE_STRING_POINTER (string)
15821 : 55384 : [TREE_INT_CST_LOW (index)]));
15822 : : }
15823 : : return NULL;
15824 : : }
15825 : :
15826 : : /* Folds a read from vector element at IDX of vector ARG. */
15827 : :
15828 : : tree
15829 : 5471 : fold_read_from_vector (tree arg, poly_uint64 idx)
15830 : : {
15831 : 5471 : unsigned HOST_WIDE_INT i;
15832 : 5471 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15833 : 5471 : && known_ge (idx, 0u)
15834 : 5471 : && idx.is_constant (&i))
15835 : : {
15836 : 5471 : if (TREE_CODE (arg) == VECTOR_CST)
15837 : 1947 : return VECTOR_CST_ELT (arg, i);
15838 : 3524 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15839 : : {
15840 : 1584 : if (CONSTRUCTOR_NELTS (arg)
15841 : 1544 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15842 : : return NULL_TREE;
15843 : 1582 : if (i >= CONSTRUCTOR_NELTS (arg))
15844 : 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15845 : 1542 : return CONSTRUCTOR_ELT (arg, i)->value;
15846 : : }
15847 : : }
15848 : : return NULL_TREE;
15849 : : }
15850 : :
15851 : : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15852 : : an integer constant, real, or fixed-point constant.
15853 : :
15854 : : TYPE is the type of the result. */
15855 : :
15856 : : static tree
15857 : 31692492 : fold_negate_const (tree arg0, tree type)
15858 : : {
15859 : 31692492 : tree t = NULL_TREE;
15860 : :
15861 : 31692492 : switch (TREE_CODE (arg0))
15862 : : {
15863 : 2100687 : case REAL_CST:
15864 : 2100687 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15865 : 2100687 : break;
15866 : :
15867 : 0 : case FIXED_CST:
15868 : 0 : {
15869 : 0 : FIXED_VALUE_TYPE f;
15870 : 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15871 : 0 : &(TREE_FIXED_CST (arg0)), NULL,
15872 : 0 : TYPE_SATURATING (type));
15873 : 0 : t = build_fixed (type, f);
15874 : : /* Propagate overflow flags. */
15875 : 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15876 : 0 : TREE_OVERFLOW (t) = 1;
15877 : 0 : break;
15878 : : }
15879 : :
15880 : 29591805 : default:
15881 : 29591805 : if (poly_int_tree_p (arg0))
15882 : : {
15883 : 29591805 : wi::overflow_type overflow;
15884 : 29591805 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15885 : 29591805 : t = force_fit_type (type, res, 1,
15886 : 259061 : (overflow && ! TYPE_UNSIGNED (type))
15887 : 29841639 : || TREE_OVERFLOW (arg0));
15888 : 29591805 : break;
15889 : 29591805 : }
15890 : :
15891 : 0 : gcc_unreachable ();
15892 : : }
15893 : :
15894 : 31692492 : return t;
15895 : : }
15896 : :
15897 : : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15898 : : an integer constant or real constant.
15899 : :
15900 : : TYPE is the type of the result. */
15901 : :
15902 : : tree
15903 : 34333 : fold_abs_const (tree arg0, tree type)
15904 : : {
15905 : 34333 : tree t = NULL_TREE;
15906 : :
15907 : 34333 : switch (TREE_CODE (arg0))
15908 : : {
15909 : 6809 : case INTEGER_CST:
15910 : 6809 : {
15911 : : /* If the value is unsigned or non-negative, then the absolute value
15912 : : is the same as the ordinary value. */
15913 : 6809 : wide_int val = wi::to_wide (arg0);
15914 : 6809 : wi::overflow_type overflow = wi::OVF_NONE;
15915 : 6809 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15916 : : ;
15917 : :
15918 : : /* If the value is negative, then the absolute value is
15919 : : its negation. */
15920 : : else
15921 : 2986 : val = wi::neg (val, &overflow);
15922 : :
15923 : : /* Force to the destination type, set TREE_OVERFLOW for signed
15924 : : TYPE only. */
15925 : 6809 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15926 : 6809 : }
15927 : 6809 : break;
15928 : :
15929 : 27524 : case REAL_CST:
15930 : 27524 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15931 : 7332 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15932 : : else
15933 : : t = arg0;
15934 : : break;
15935 : :
15936 : 0 : default:
15937 : 0 : gcc_unreachable ();
15938 : : }
15939 : :
15940 : 34333 : return t;
15941 : : }
15942 : :
15943 : : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15944 : : constant. TYPE is the type of the result. */
15945 : :
15946 : : static tree
15947 : 2287145 : fold_not_const (const_tree arg0, tree type)
15948 : : {
15949 : 2287145 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15950 : :
15951 : 2287145 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15952 : : }
15953 : :
15954 : : /* Given CODE, a relational operator, the target type, TYPE and two
15955 : : constant operands OP0 and OP1, return the result of the
15956 : : relational operation. If the result is not a compile time
15957 : : constant, then return NULL_TREE. */
15958 : :
15959 : : static tree
15960 : 59161748 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15961 : : {
15962 : 59161748 : int result, invert;
15963 : :
15964 : : /* From here on, the only cases we handle are when the result is
15965 : : known to be a constant. */
15966 : :
15967 : 59161748 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15968 : : {
15969 : 1110147 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15970 : 1110147 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15971 : :
15972 : : /* Handle the cases where either operand is a NaN. */
15973 : 1110147 : if (real_isnan (c0) || real_isnan (c1))
15974 : : {
15975 : 14165 : switch (code)
15976 : : {
15977 : : case EQ_EXPR:
15978 : : case ORDERED_EXPR:
15979 : : result = 0;
15980 : : break;
15981 : :
15982 : : case NE_EXPR:
15983 : : case UNORDERED_EXPR:
15984 : : case UNLT_EXPR:
15985 : : case UNLE_EXPR:
15986 : : case UNGT_EXPR:
15987 : : case UNGE_EXPR:
15988 : : case UNEQ_EXPR:
15989 : 6618 : result = 1;
15990 : : break;
15991 : :
15992 : 7569 : case LT_EXPR:
15993 : 7569 : case LE_EXPR:
15994 : 7569 : case GT_EXPR:
15995 : 7569 : case GE_EXPR:
15996 : 7569 : case LTGT_EXPR:
15997 : 7569 : if (flag_trapping_math)
15998 : : return NULL_TREE;
15999 : : result = 0;
16000 : : break;
16001 : :
16002 : 0 : default:
16003 : 0 : gcc_unreachable ();
16004 : : }
16005 : :
16006 : 6618 : return constant_boolean_node (result, type);
16007 : : }
16008 : :
16009 : 1095982 : return constant_boolean_node (real_compare (code, c0, c1), type);
16010 : : }
16011 : :
16012 : 58051601 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
16013 : : {
16014 : 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16015 : 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16016 : 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
16017 : : }
16018 : :
16019 : : /* Handle equality/inequality of complex constants. */
16020 : 58051601 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16021 : : {
16022 : 58374 : tree rcond = fold_relational_const (code, type,
16023 : 29187 : TREE_REALPART (op0),
16024 : 29187 : TREE_REALPART (op1));
16025 : 116748 : tree icond = fold_relational_const (code, type,
16026 : 29187 : TREE_IMAGPART (op0),
16027 : 29187 : TREE_IMAGPART (op1));
16028 : 29187 : if (code == EQ_EXPR)
16029 : 286 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16030 : 28901 : else if (code == NE_EXPR)
16031 : 28901 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16032 : : else
16033 : : return NULL_TREE;
16034 : : }
16035 : :
16036 : 58022414 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16037 : : {
16038 : 9053 : if (!VECTOR_TYPE_P (type))
16039 : : {
16040 : : /* Have vector comparison with scalar boolean result. */
16041 : 207 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
16042 : : && known_eq (VECTOR_CST_NELTS (op0),
16043 : : VECTOR_CST_NELTS (op1)));
16044 : 207 : unsigned HOST_WIDE_INT nunits;
16045 : 207 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
16046 : : return NULL_TREE;
16047 : 942 : for (unsigned i = 0; i < nunits; i++)
16048 : : {
16049 : 842 : tree elem0 = VECTOR_CST_ELT (op0, i);
16050 : 842 : tree elem1 = VECTOR_CST_ELT (op1, i);
16051 : 842 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
16052 : 842 : if (tmp == NULL_TREE)
16053 : : return NULL_TREE;
16054 : 842 : if (integer_zerop (tmp))
16055 : 107 : return constant_boolean_node (code == NE_EXPR, type);
16056 : : }
16057 : 100 : return constant_boolean_node (code == EQ_EXPR, type);
16058 : : }
16059 : 8846 : tree_vector_builder elts;
16060 : 8846 : if (!elts.new_binary_operation (type, op0, op1, false))
16061 : : return NULL_TREE;
16062 : 8846 : unsigned int count = elts.encoded_nelts ();
16063 : 46392 : for (unsigned i = 0; i < count; i++)
16064 : : {
16065 : 37546 : tree elem_type = TREE_TYPE (type);
16066 : 37546 : tree elem0 = VECTOR_CST_ELT (op0, i);
16067 : 37546 : tree elem1 = VECTOR_CST_ELT (op1, i);
16068 : :
16069 : 37546 : tree tem = fold_relational_const (code, elem_type,
16070 : : elem0, elem1);
16071 : :
16072 : 37546 : if (tem == NULL_TREE)
16073 : : return NULL_TREE;
16074 : :
16075 : 37546 : elts.quick_push (build_int_cst (elem_type,
16076 : 55766 : integer_zerop (tem) ? 0 : -1));
16077 : : }
16078 : :
16079 : 8846 : return elts.build ();
16080 : 8846 : }
16081 : :
16082 : : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16083 : :
16084 : : To compute GT, swap the arguments and do LT.
16085 : : To compute GE, do LT and invert the result.
16086 : : To compute LE, swap the arguments, do LT and invert the result.
16087 : : To compute NE, do EQ and invert the result.
16088 : :
16089 : : Therefore, the code below must handle only EQ and LT. */
16090 : :
16091 : 58013361 : if (code == LE_EXPR || code == GT_EXPR)
16092 : : {
16093 : 11672941 : std::swap (op0, op1);
16094 : 11672941 : code = swap_tree_comparison (code);
16095 : : }
16096 : :
16097 : : /* Note that it is safe to invert for real values here because we
16098 : : have already handled the one case that it matters. */
16099 : :
16100 : 58013361 : invert = 0;
16101 : 58013361 : if (code == NE_EXPR || code == GE_EXPR)
16102 : : {
16103 : 28200569 : invert = 1;
16104 : 28200569 : code = invert_tree_comparison (code, false);
16105 : : }
16106 : :
16107 : : /* Compute a result for LT or EQ if args permit;
16108 : : Otherwise return T. */
16109 : 58013361 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16110 : : {
16111 : 57993787 : if (code == EQ_EXPR)
16112 : 29367710 : result = tree_int_cst_equal (op0, op1);
16113 : : else
16114 : 28626077 : result = tree_int_cst_lt (op0, op1);
16115 : : }
16116 : : else
16117 : : return NULL_TREE;
16118 : :
16119 : 57993787 : if (invert)
16120 : 28199847 : result ^= 1;
16121 : 57993787 : return constant_boolean_node (result, type);
16122 : : }
16123 : :
16124 : : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16125 : : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16126 : : itself. */
16127 : :
16128 : : tree
16129 : 119362501 : fold_build_cleanup_point_expr (tree type, tree expr)
16130 : : {
16131 : : /* If the expression does not have side effects then we don't have to wrap
16132 : : it with a cleanup point expression. */
16133 : 119362501 : if (!TREE_SIDE_EFFECTS (expr))
16134 : : return expr;
16135 : :
16136 : : /* If the expression is a return, check to see if the expression inside the
16137 : : return has no side effects or the right hand side of the modify expression
16138 : : inside the return. If either don't have side effects set we don't need to
16139 : : wrap the expression in a cleanup point expression. Note we don't check the
16140 : : left hand side of the modify because it should always be a return decl. */
16141 : 102795398 : if (TREE_CODE (expr) == RETURN_EXPR)
16142 : : {
16143 : 38953336 : tree op = TREE_OPERAND (expr, 0);
16144 : 38953336 : if (!op || !TREE_SIDE_EFFECTS (op))
16145 : : return expr;
16146 : 38231724 : op = TREE_OPERAND (op, 1);
16147 : 38231724 : if (!TREE_SIDE_EFFECTS (op))
16148 : : return expr;
16149 : : }
16150 : :
16151 : 82283885 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16152 : : }
16153 : :
16154 : : /* Given a pointer value OP0 and a type TYPE, return a simplified version
16155 : : of an indirection through OP0, or NULL_TREE if no simplification is
16156 : : possible. */
16157 : :
16158 : : tree
16159 : 19237256 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16160 : : {
16161 : 19237256 : tree sub = op0;
16162 : 19237256 : tree subtype;
16163 : 19237256 : poly_uint64 const_op01;
16164 : :
16165 : 19237256 : STRIP_NOPS (sub);
16166 : 19237256 : subtype = TREE_TYPE (sub);
16167 : 19237256 : if (!POINTER_TYPE_P (subtype)
16168 : 19237256 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16169 : : return NULL_TREE;
16170 : :
16171 : 19089965 : if (TREE_CODE (sub) == ADDR_EXPR)
16172 : : {
16173 : 3490579 : tree op = TREE_OPERAND (sub, 0);
16174 : 3490579 : tree optype = TREE_TYPE (op);
16175 : :
16176 : : /* *&CONST_DECL -> to the value of the const decl. */
16177 : 3490579 : if (TREE_CODE (op) == CONST_DECL)
16178 : 2853 : return DECL_INITIAL (op);
16179 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
16180 : 3487726 : if (type == optype)
16181 : : {
16182 : 2566171 : tree fop = fold_read_from_constant_string (op);
16183 : 2566171 : if (fop)
16184 : : return fop;
16185 : : else
16186 : 2523127 : return op;
16187 : : }
16188 : : /* *(foo *)&fooarray => fooarray[0] */
16189 : 921555 : else if (TREE_CODE (optype) == ARRAY_TYPE
16190 : 13490 : && type == TREE_TYPE (optype)
16191 : 933917 : && (!in_gimple_form
16192 : 2894 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16193 : : {
16194 : 12362 : tree type_domain = TYPE_DOMAIN (optype);
16195 : 12362 : tree min_val = size_zero_node;
16196 : 12362 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16197 : 12324 : min_val = TYPE_MIN_VALUE (type_domain);
16198 : 12362 : if (in_gimple_form
16199 : 2894 : && TREE_CODE (min_val) != INTEGER_CST)
16200 : : return NULL_TREE;
16201 : 12362 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16202 : 12362 : NULL_TREE, NULL_TREE);
16203 : : }
16204 : : /* *(foo *)&complexfoo => __real__ complexfoo */
16205 : 909193 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16206 : 909193 : && type == TREE_TYPE (optype))
16207 : 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16208 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16209 : 909193 : else if (VECTOR_TYPE_P (optype)
16210 : 909193 : && type == TREE_TYPE (optype))
16211 : : {
16212 : 70 : tree part_width = TYPE_SIZE (type);
16213 : 70 : tree index = bitsize_int (0);
16214 : 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16215 : 70 : index);
16216 : : }
16217 : : }
16218 : :
16219 : 16508509 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16220 : 16508509 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16221 : : {
16222 : 262520 : tree op00 = TREE_OPERAND (sub, 0);
16223 : 262520 : tree op01 = TREE_OPERAND (sub, 1);
16224 : :
16225 : 262520 : STRIP_NOPS (op00);
16226 : 262520 : if (TREE_CODE (op00) == ADDR_EXPR)
16227 : : {
16228 : 2862 : tree op00type;
16229 : 2862 : op00 = TREE_OPERAND (op00, 0);
16230 : 2862 : op00type = TREE_TYPE (op00);
16231 : :
16232 : : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16233 : 2862 : if (VECTOR_TYPE_P (op00type)
16234 : 240 : && type == TREE_TYPE (op00type)
16235 : : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16236 : : but we want to treat offsets with MSB set as negative.
16237 : : For the code below negative offsets are invalid and
16238 : : TYPE_SIZE of the element is something unsigned, so
16239 : : check whether op01 fits into poly_int64, which implies
16240 : : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16241 : : then just use poly_uint64 because we want to treat the
16242 : : value as unsigned. */
16243 : 3055 : && tree_fits_poly_int64_p (op01))
16244 : : {
16245 : 179 : tree part_width = TYPE_SIZE (type);
16246 : 179 : poly_uint64 max_offset
16247 : 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16248 : 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16249 : 179 : if (known_lt (const_op01, max_offset))
16250 : : {
16251 : 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16252 : 179 : return fold_build3_loc (loc,
16253 : : BIT_FIELD_REF, type, op00,
16254 : 179 : part_width, index);
16255 : : }
16256 : : }
16257 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16258 : 2683 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16259 : 2683 : && type == TREE_TYPE (op00type))
16260 : : {
16261 : 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16262 : : const_op01))
16263 : 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16264 : : }
16265 : : /* ((foo *)&fooarray)[1] => fooarray[1] */
16266 : 2683 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16267 : 2683 : && type == TREE_TYPE (op00type))
16268 : : {
16269 : 1169 : tree type_domain = TYPE_DOMAIN (op00type);
16270 : 1169 : tree min_val = size_zero_node;
16271 : 1169 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16272 : 1168 : min_val = TYPE_MIN_VALUE (type_domain);
16273 : 1169 : poly_uint64 type_size, index;
16274 : 1169 : if (poly_int_tree_p (min_val)
16275 : 1169 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16276 : 1169 : && multiple_p (const_op01, type_size, &index))
16277 : : {
16278 : 1169 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16279 : 1169 : op01 = wide_int_to_tree (sizetype, off);
16280 : 1169 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16281 : : NULL_TREE, NULL_TREE);
16282 : : }
16283 : : }
16284 : : }
16285 : : }
16286 : :
16287 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16288 : 16507161 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16289 : 652826 : && type == TREE_TYPE (TREE_TYPE (subtype))
16290 : 16510164 : && (!in_gimple_form
16291 : 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16292 : : {
16293 : 3002 : tree type_domain;
16294 : 3002 : tree min_val = size_zero_node;
16295 : 3002 : sub = build_fold_indirect_ref_loc (loc, sub);
16296 : 3002 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16297 : 3002 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16298 : 3002 : min_val = TYPE_MIN_VALUE (type_domain);
16299 : 3002 : if (in_gimple_form
16300 : 11 : && TREE_CODE (min_val) != INTEGER_CST)
16301 : : return NULL_TREE;
16302 : 3002 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16303 : 3002 : NULL_TREE);
16304 : : }
16305 : :
16306 : : return NULL_TREE;
16307 : : }
16308 : :
16309 : : /* Builds an expression for an indirection through T, simplifying some
16310 : : cases. */
16311 : :
16312 : : tree
16313 : 8576610 : build_fold_indirect_ref_loc (location_t loc, tree t)
16314 : : {
16315 : 8576610 : tree type = TREE_TYPE (TREE_TYPE (t));
16316 : 8576610 : tree sub = fold_indirect_ref_1 (loc, type, t);
16317 : :
16318 : 8576610 : if (sub)
16319 : : return sub;
16320 : :
16321 : 6013949 : return build1_loc (loc, INDIRECT_REF, type, t);
16322 : : }
16323 : :
16324 : : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16325 : :
16326 : : tree
16327 : 10351322 : fold_indirect_ref_loc (location_t loc, tree t)
16328 : : {
16329 : 10351322 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16330 : :
16331 : 10351322 : if (sub)
16332 : : return sub;
16333 : : else
16334 : 10330459 : return t;
16335 : : }
16336 : :
16337 : : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16338 : : whose result is ignored. The type of the returned tree need not be
16339 : : the same as the original expression. */
16340 : :
16341 : : tree
16342 : 138512 : fold_ignored_result (tree t)
16343 : : {
16344 : 138512 : if (!TREE_SIDE_EFFECTS (t))
16345 : 18871 : return integer_zero_node;
16346 : :
16347 : 159818 : for (;;)
16348 : 159818 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16349 : : {
16350 : 3838 : case tcc_unary:
16351 : 3838 : t = TREE_OPERAND (t, 0);
16352 : 3838 : break;
16353 : :
16354 : 5024 : case tcc_binary:
16355 : 5024 : case tcc_comparison:
16356 : 5024 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16357 : 3134 : t = TREE_OPERAND (t, 0);
16358 : 1890 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16359 : 18 : t = TREE_OPERAND (t, 1);
16360 : : else
16361 : : return t;
16362 : : break;
16363 : :
16364 : 103243 : case tcc_expression:
16365 : 103243 : switch (TREE_CODE (t))
16366 : : {
16367 : 33172 : case COMPOUND_EXPR:
16368 : 33172 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16369 : : return t;
16370 : 32893 : t = TREE_OPERAND (t, 0);
16371 : 32893 : break;
16372 : :
16373 : 381 : case COND_EXPR:
16374 : 381 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16375 : 381 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16376 : : return t;
16377 : 294 : t = TREE_OPERAND (t, 0);
16378 : 294 : break;
16379 : :
16380 : : default:
16381 : : return t;
16382 : : }
16383 : : break;
16384 : :
16385 : : default:
16386 : : return t;
16387 : : }
16388 : : }
16389 : :
16390 : : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16391 : :
16392 : : tree
16393 : 2780166318 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16394 : : {
16395 : 2780166318 : tree div = NULL_TREE;
16396 : :
16397 : 2780166318 : if (divisor == 1)
16398 : : return value;
16399 : :
16400 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16401 : : have to do anything. Only do this when we are not given a const,
16402 : : because in that case, this check is more expensive than just
16403 : : doing it. */
16404 : 1725450881 : if (TREE_CODE (value) != INTEGER_CST)
16405 : : {
16406 : 351161 : div = build_int_cst (TREE_TYPE (value), divisor);
16407 : :
16408 : 351161 : if (multiple_of_p (TREE_TYPE (value), value, div))
16409 : : return value;
16410 : : }
16411 : :
16412 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16413 : 1725101553 : if (pow2_or_zerop (divisor))
16414 : : {
16415 : 1725101553 : if (TREE_CODE (value) == INTEGER_CST)
16416 : : {
16417 : 1725099720 : wide_int val = wi::to_wide (value);
16418 : 1725099720 : bool overflow_p;
16419 : :
16420 : 1725099720 : if ((val & (divisor - 1)) == 0)
16421 : : return value;
16422 : :
16423 : 3684210 : overflow_p = TREE_OVERFLOW (value);
16424 : 3684210 : val += divisor - 1;
16425 : 3684210 : val &= (int) -divisor;
16426 : 3684210 : if (val == 0)
16427 : 6 : overflow_p = true;
16428 : :
16429 : 3684210 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16430 : 1725099720 : }
16431 : : else
16432 : : {
16433 : 1833 : tree t;
16434 : :
16435 : 1833 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16436 : 1833 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16437 : 1833 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16438 : 1833 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16439 : : }
16440 : : }
16441 : : else
16442 : : {
16443 : 0 : if (!div)
16444 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16445 : 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16446 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16447 : : }
16448 : :
16449 : : return value;
16450 : : }
16451 : :
16452 : : /* Likewise, but round down. */
16453 : :
16454 : : tree
16455 : 18607603 : round_down_loc (location_t loc, tree value, int divisor)
16456 : : {
16457 : 18607603 : tree div = NULL_TREE;
16458 : :
16459 : 18607603 : gcc_assert (divisor > 0);
16460 : 18607603 : if (divisor == 1)
16461 : : return value;
16462 : :
16463 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16464 : : have to do anything. Only do this when we are not given a const,
16465 : : because in that case, this check is more expensive than just
16466 : : doing it. */
16467 : 18607603 : if (TREE_CODE (value) != INTEGER_CST)
16468 : : {
16469 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16470 : :
16471 : 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16472 : : return value;
16473 : : }
16474 : :
16475 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16476 : 18607603 : if (pow2_or_zerop (divisor))
16477 : : {
16478 : 18607603 : tree t;
16479 : :
16480 : 18607603 : t = build_int_cst (TREE_TYPE (value), -divisor);
16481 : 18607603 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16482 : : }
16483 : : else
16484 : : {
16485 : 0 : if (!div)
16486 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16487 : 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16488 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16489 : : }
16490 : :
16491 : : return value;
16492 : : }
16493 : :
16494 : : /* Returns the pointer to the base of the object addressed by EXP and
16495 : : extracts the information about the offset of the access, storing it
16496 : : to PBITPOS and POFFSET. */
16497 : :
16498 : : static tree
16499 : 1713048 : split_address_to_core_and_offset (tree exp,
16500 : : poly_int64 *pbitpos, tree *poffset)
16501 : : {
16502 : 1713048 : tree core;
16503 : 1713048 : machine_mode mode;
16504 : 1713048 : int unsignedp, reversep, volatilep;
16505 : 1713048 : poly_int64 bitsize;
16506 : 1713048 : location_t loc = EXPR_LOCATION (exp);
16507 : :
16508 : 1713048 : if (TREE_CODE (exp) == SSA_NAME)
16509 : 522760 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16510 : 364074 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16511 : 30236 : exp = gimple_assign_rhs1 (def);
16512 : :
16513 : 1713048 : if (TREE_CODE (exp) == ADDR_EXPR)
16514 : : {
16515 : 995035 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16516 : : poffset, &mode, &unsignedp, &reversep,
16517 : : &volatilep);
16518 : : /* If we are left with MEM[a + CST] strip that and add it to the
16519 : : pbitpos and return a. */
16520 : 995035 : if (TREE_CODE (core) == MEM_REF)
16521 : : {
16522 : 27299 : poly_offset_int tem;
16523 : 27299 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16524 : 27299 : tem <<= LOG2_BITS_PER_UNIT;
16525 : 27299 : tem += *pbitpos;
16526 : 27299 : if (tem.to_shwi (pbitpos))
16527 : 27121 : return TREE_OPERAND (core, 0);
16528 : : }
16529 : 967914 : core = build_fold_addr_expr_loc (loc, core);
16530 : : }
16531 : 718013 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16532 : : {
16533 : 109037 : core = TREE_OPERAND (exp, 0);
16534 : 109037 : STRIP_NOPS (core);
16535 : 109037 : *pbitpos = 0;
16536 : 109037 : *poffset = TREE_OPERAND (exp, 1);
16537 : 109037 : if (poly_int_tree_p (*poffset))
16538 : : {
16539 : 108943 : poly_offset_int tem
16540 : 108943 : = wi::sext (wi::to_poly_offset (*poffset),
16541 : 108943 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16542 : 108943 : tem <<= LOG2_BITS_PER_UNIT;
16543 : 108943 : if (tem.to_shwi (pbitpos))
16544 : 108943 : *poffset = NULL_TREE;
16545 : : }
16546 : : }
16547 : : else
16548 : : {
16549 : 608976 : core = exp;
16550 : 608976 : *pbitpos = 0;
16551 : 608976 : *poffset = NULL_TREE;
16552 : : }
16553 : :
16554 : : return core;
16555 : : }
16556 : :
16557 : : /* Returns true if addresses of E1 and E2 differ by a constant, false
16558 : : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16559 : :
16560 : : bool
16561 : 856524 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16562 : : {
16563 : 856524 : tree core1, core2;
16564 : 856524 : poly_int64 bitpos1, bitpos2;
16565 : 856524 : tree toffset1, toffset2, tdiff, type;
16566 : :
16567 : 856524 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16568 : 856524 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16569 : :
16570 : 856524 : poly_int64 bytepos1, bytepos2;
16571 : 856524 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16572 : 1463347 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16573 : 1713048 : || !operand_equal_p (core1, core2, 0))
16574 : 606823 : return false;
16575 : :
16576 : 249701 : if (toffset1 && toffset2)
16577 : : {
16578 : 29 : type = TREE_TYPE (toffset1);
16579 : 29 : if (type != TREE_TYPE (toffset2))
16580 : 0 : toffset2 = fold_convert (type, toffset2);
16581 : :
16582 : 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16583 : 29 : if (!cst_and_fits_in_hwi (tdiff))
16584 : : return false;
16585 : :
16586 : 15 : *diff = int_cst_value (tdiff);
16587 : : }
16588 : 249672 : else if (toffset1 || toffset2)
16589 : : {
16590 : : /* If only one of the offsets is non-constant, the difference cannot
16591 : : be a constant. */
16592 : : return false;
16593 : : }
16594 : : else
16595 : 231919 : *diff = 0;
16596 : :
16597 : 231934 : *diff += bytepos1 - bytepos2;
16598 : 231934 : return true;
16599 : : }
16600 : :
16601 : : /* Return OFF converted to a pointer offset type suitable as offset for
16602 : : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16603 : : tree
16604 : 19730915 : convert_to_ptrofftype_loc (location_t loc, tree off)
16605 : : {
16606 : 19730915 : if (ptrofftype_p (TREE_TYPE (off)))
16607 : : return off;
16608 : 2039977 : return fold_convert_loc (loc, sizetype, off);
16609 : : }
16610 : :
16611 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16612 : : tree
16613 : 17805868 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16614 : : {
16615 : 17805868 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16616 : 17805868 : ptr, convert_to_ptrofftype_loc (loc, off));
16617 : : }
16618 : :
16619 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16620 : : tree
16621 : 163633 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16622 : : {
16623 : 163633 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16624 : 163633 : ptr, size_int (off));
16625 : : }
16626 : :
16627 : : /* Return a pointer to a NUL-terminated string containing the sequence
16628 : : of bytes corresponding to the representation of the object referred to
16629 : : by SRC (or a subsequence of such bytes within it if SRC is a reference
16630 : : to an initialized constant array plus some constant offset).
16631 : : Set *STRSIZE the number of bytes in the constant sequence including
16632 : : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16633 : : where A is the array that stores the constant sequence that SRC points
16634 : : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16635 : : need not point to a string or even an array of characters but may point
16636 : : to an object of any type. */
16637 : :
16638 : : const char *
16639 : 12354913 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16640 : : {
16641 : : /* The offset into the array A storing the string, and A's byte size. */
16642 : 12354913 : tree offset_node;
16643 : 12354913 : tree mem_size;
16644 : :
16645 : 12354913 : if (strsize)
16646 : 4557667 : *strsize = 0;
16647 : :
16648 : 12354913 : if (strsize)
16649 : 4557667 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16650 : : else
16651 : 7797246 : src = string_constant (src, &offset_node, &mem_size, NULL);
16652 : 12354913 : if (!src)
16653 : : return NULL;
16654 : :
16655 : 2743508 : unsigned HOST_WIDE_INT offset = 0;
16656 : 2743508 : if (offset_node != NULL_TREE)
16657 : : {
16658 : 2743508 : if (!tree_fits_uhwi_p (offset_node))
16659 : : return NULL;
16660 : : else
16661 : 2741744 : offset = tree_to_uhwi (offset_node);
16662 : : }
16663 : :
16664 : 2741744 : if (!tree_fits_uhwi_p (mem_size))
16665 : : return NULL;
16666 : :
16667 : : /* ARRAY_SIZE is the byte size of the array the constant sequence
16668 : : is stored in and equal to sizeof A. INIT_BYTES is the number
16669 : : of bytes in the constant sequence used to initialize the array,
16670 : : including any embedded NULs as well as the terminating NUL (for
16671 : : strings), but not including any trailing zeros/NULs past
16672 : : the terminating one appended implicitly to a string literal to
16673 : : zero out the remainder of the array it's stored in. For example,
16674 : : given:
16675 : : const char a[7] = "abc\0d";
16676 : : n = strlen (a + 1);
16677 : : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16678 : : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16679 : : is equal to strlen (A) + 1. */
16680 : 2741744 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16681 : 2741744 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16682 : 2741744 : const char *string = TREE_STRING_POINTER (src);
16683 : :
16684 : : /* Ideally this would turn into a gcc_checking_assert over time. */
16685 : 2741744 : if (init_bytes > array_size)
16686 : : init_bytes = array_size;
16687 : :
16688 : 2741744 : if (init_bytes == 0 || offset >= array_size)
16689 : : return NULL;
16690 : :
16691 : 2740553 : if (strsize)
16692 : : {
16693 : : /* Compute and store the number of characters from the beginning
16694 : : of the substring at OFFSET to the end, including the terminating
16695 : : nul. Offsets past the initial length refer to null strings. */
16696 : 1427450 : if (offset < init_bytes)
16697 : 1427450 : *strsize = init_bytes - offset;
16698 : : else
16699 : 0 : *strsize = 1;
16700 : : }
16701 : : else
16702 : : {
16703 : 1313103 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16704 : : /* Support only properly NUL-terminated single byte strings. */
16705 : 1313103 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16706 : : return NULL;
16707 : 1307460 : if (string[init_bytes - 1] != '\0')
16708 : : return NULL;
16709 : : }
16710 : :
16711 : 2713393 : return offset < init_bytes ? string + offset : "";
16712 : : }
16713 : :
16714 : : /* Return a pointer to a NUL-terminated string corresponding to
16715 : : the expression STR referencing a constant string, possibly
16716 : : involving a constant offset. Return null if STR either doesn't
16717 : : reference a constant string or if it involves a nonconstant
16718 : : offset. */
16719 : :
16720 : : const char *
16721 : 7797246 : c_getstr (tree str)
16722 : : {
16723 : 7797246 : return getbyterep (str, NULL);
16724 : : }
16725 : :
16726 : : /* Given a tree T, compute which bits in T may be nonzero. */
16727 : :
16728 : : wide_int
16729 : 237846706 : tree_nonzero_bits (const_tree t)
16730 : : {
16731 : 237846706 : switch (TREE_CODE (t))
16732 : : {
16733 : 8082081 : case INTEGER_CST:
16734 : 8082081 : return wi::to_wide (t);
16735 : 134203562 : case SSA_NAME:
16736 : 134203562 : return get_nonzero_bits (t);
16737 : 246437 : case NON_LVALUE_EXPR:
16738 : 246437 : case SAVE_EXPR:
16739 : 246437 : return tree_nonzero_bits (TREE_OPERAND (t, 0));
16740 : 528833 : case BIT_AND_EXPR:
16741 : 1057666 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16742 : 1586499 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16743 : 4325 : case BIT_IOR_EXPR:
16744 : 4325 : case BIT_XOR_EXPR:
16745 : 8650 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16746 : 12975 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16747 : 63788 : case COND_EXPR:
16748 : 127576 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16749 : 191364 : tree_nonzero_bits (TREE_OPERAND (t, 2)));
16750 : 49923937 : CASE_CONVERT:
16751 : 99847874 : return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16752 : 49923937 : TYPE_PRECISION (TREE_TYPE (t)),
16753 : 149771811 : TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16754 : 13424105 : case PLUS_EXPR:
16755 : 13424105 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16756 : : {
16757 : 13424105 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16758 : 13424105 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16759 : 13424105 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16760 : 497189 : return wi::bit_or (nzbits1, nzbits2);
16761 : 13424105 : }
16762 : : break;
16763 : 165508 : case LSHIFT_EXPR:
16764 : 165508 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16765 : : {
16766 : 93754 : tree type = TREE_TYPE (t);
16767 : 93754 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16768 : 187508 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16769 : 93754 : TYPE_PRECISION (type));
16770 : 93754 : return wi::neg_p (arg1)
16771 : 187508 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16772 : 93754 : : wi::lshift (nzbits, arg1);
16773 : 93754 : }
16774 : : break;
16775 : 155930 : case RSHIFT_EXPR:
16776 : 155930 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16777 : : {
16778 : 154372 : tree type = TREE_TYPE (t);
16779 : 154372 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16780 : 308744 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16781 : 154372 : TYPE_PRECISION (type));
16782 : 154372 : return wi::neg_p (arg1)
16783 : 308744 : ? wi::lshift (nzbits, -arg1)
16784 : 154372 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16785 : 154372 : }
16786 : : break;
16787 : : default:
16788 : : break;
16789 : : }
16790 : :
16791 : 44048428 : return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16792 : : }
16793 : :
16794 : : /* Helper function for address compare simplifications in match.pd.
16795 : : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16796 : : TYPE is the type of comparison operands.
16797 : : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16798 : : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16799 : : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16800 : : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16801 : : and 2 if unknown. */
16802 : :
16803 : : int
16804 : 1427587 : address_compare (tree_code code, tree type, tree op0, tree op1,
16805 : : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16806 : : bool generic)
16807 : : {
16808 : 1427587 : if (TREE_CODE (op0) == SSA_NAME)
16809 : 27970 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16810 : 1427587 : if (TREE_CODE (op1) == SSA_NAME)
16811 : 3952 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16812 : 1427587 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16813 : 1427587 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16814 : 1427587 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16815 : 1427587 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16816 : 1427587 : if (base0 && TREE_CODE (base0) == MEM_REF)
16817 : : {
16818 : 28265 : off0 += mem_ref_offset (base0).force_shwi ();
16819 : 28265 : base0 = TREE_OPERAND (base0, 0);
16820 : : }
16821 : 1427587 : if (base1 && TREE_CODE (base1) == MEM_REF)
16822 : : {
16823 : 2498 : off1 += mem_ref_offset (base1).force_shwi ();
16824 : 2498 : base1 = TREE_OPERAND (base1, 0);
16825 : : }
16826 : 1427587 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16827 : : return 2;
16828 : :
16829 : 1419337 : int equal = 2;
16830 : : /* Punt in GENERIC on variables with value expressions;
16831 : : the value expressions might point to fields/elements
16832 : : of other vars etc. */
16833 : 1419337 : if (generic
16834 : 1419337 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16835 : 1279002 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16836 : : return 2;
16837 : 1418779 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16838 : : {
16839 : 105533 : symtab_node *node0 = symtab_node::get_create (base0);
16840 : 105533 : symtab_node *node1 = symtab_node::get_create (base1);
16841 : 105533 : equal = node0->equal_address_to (node1);
16842 : : }
16843 : 1313246 : else if ((DECL_P (base0)
16844 : 232187 : || TREE_CODE (base0) == SSA_NAME
16845 : 204789 : || TREE_CODE (base0) == STRING_CST)
16846 : 1313084 : && (DECL_P (base1)
16847 : 207304 : || TREE_CODE (base1) == SSA_NAME
16848 : 205036 : || TREE_CODE (base1) == STRING_CST))
16849 : 1313062 : equal = (base0 == base1);
16850 : : /* Assume different STRING_CSTs with the same content will be
16851 : : merged. */
16852 : 1418595 : if (equal == 0
16853 : 62719 : && TREE_CODE (base0) == STRING_CST
16854 : 17621 : && TREE_CODE (base1) == STRING_CST
16855 : 17564 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16856 : 1418595 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16857 : 6546 : TREE_STRING_LENGTH (base0)) == 0)
16858 : : equal = 1;
16859 : 1413896 : if (equal == 1)
16860 : : {
16861 : 1336229 : if (code == EQ_EXPR
16862 : 1336229 : || code == NE_EXPR
16863 : : /* If the offsets are equal we can ignore overflow. */
16864 : 131092 : || known_eq (off0, off1)
16865 : 261962 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16866 : : /* Or if we compare using pointers to decls or strings. */
16867 : 1467210 : || (POINTER_TYPE_P (type)
16868 : 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16869 : : return 1;
16870 : : return 2;
16871 : : }
16872 : 82550 : if (equal != 0)
16873 : : return equal;
16874 : 57836 : if (code != EQ_EXPR && code != NE_EXPR)
16875 : : return 2;
16876 : :
16877 : : /* At this point we know (or assume) the two pointers point at
16878 : : different objects. */
16879 : 53684 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16880 : 53684 : off0.is_constant (&ioff0);
16881 : 53684 : off1.is_constant (&ioff1);
16882 : : /* Punt on non-zero offsets from functions. */
16883 : 53684 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16884 : 53684 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16885 : : return 2;
16886 : : /* Or if the bases are neither decls nor string literals. */
16887 : 53684 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16888 : : return 2;
16889 : 26770 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16890 : : return 2;
16891 : : /* For initializers, assume addresses of different functions are
16892 : : different. */
16893 : 26770 : if (folding_initializer
16894 : 4289 : && TREE_CODE (base0) == FUNCTION_DECL
16895 : 14 : && TREE_CODE (base1) == FUNCTION_DECL)
16896 : : return 0;
16897 : :
16898 : : /* Compute whether one address points to the start of one
16899 : : object and another one to the end of another one. */
16900 : 26756 : poly_int64 size0 = 0, size1 = 0;
16901 : 26756 : if (TREE_CODE (base0) == STRING_CST)
16902 : : {
16903 : 12687 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16904 : : equal = 2;
16905 : : else
16906 : : size0 = TREE_STRING_LENGTH (base0);
16907 : : }
16908 : 14069 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16909 : : size0 = 1;
16910 : : else
16911 : : {
16912 : 13907 : tree sz0 = DECL_SIZE_UNIT (base0);
16913 : 13907 : if (!tree_fits_poly_int64_p (sz0))
16914 : : equal = 2;
16915 : : else
16916 : 13907 : size0 = tree_to_poly_int64 (sz0);
16917 : : }
16918 : 26756 : if (TREE_CODE (base1) == STRING_CST)
16919 : : {
16920 : 12807 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16921 : : equal = 2;
16922 : : else
16923 : : size1 = TREE_STRING_LENGTH (base1);
16924 : : }
16925 : 13949 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16926 : : size1 = 1;
16927 : : else
16928 : : {
16929 : 13791 : tree sz1 = DECL_SIZE_UNIT (base1);
16930 : 13791 : if (!tree_fits_poly_int64_p (sz1))
16931 : : equal = 2;
16932 : : else
16933 : 13791 : size1 = tree_to_poly_int64 (sz1);
16934 : : }
16935 : 26756 : if (equal == 0)
16936 : : {
16937 : : /* If one offset is pointing (or could be) to the beginning of one
16938 : : object and the other is pointing to one past the last byte of the
16939 : : other object, punt. */
16940 : 26744 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16941 : : equal = 2;
16942 : 26607 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16943 : : equal = 2;
16944 : : /* If both offsets are the same, there are some cases we know that are
16945 : : ok. Either if we know they aren't zero, or if we know both sizes
16946 : : are no zero. */
16947 : : if (equal == 2
16948 : 273 : && known_eq (off0, off1)
16949 : 22 : && (known_ne (off0, 0)
16950 : 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16951 : : equal = 0;
16952 : : }
16953 : :
16954 : : /* At this point, equal is 2 if either one or both pointers are out of
16955 : : bounds of their object, or one points to start of its object and the
16956 : : other points to end of its object. This is unspecified behavior
16957 : : e.g. in C++. Otherwise equal is 0. */
16958 : 26756 : if (folding_cxx_constexpr && equal)
16959 : : return equal;
16960 : :
16961 : : /* When both pointers point to string literals, even when equal is 0,
16962 : : due to tail merging of string literals the pointers might be the same. */
16963 : 26693 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16964 : : {
16965 : 12663 : if (ioff0 < 0
16966 : 12663 : || ioff1 < 0
16967 : 12663 : || ioff0 > TREE_STRING_LENGTH (base0)
16968 : 25314 : || ioff1 > TREE_STRING_LENGTH (base1))
16969 : : return 2;
16970 : :
16971 : : /* If the bytes in the string literals starting at the pointers
16972 : : differ, the pointers need to be different. */
16973 : 12651 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16974 : 12651 : TREE_STRING_POINTER (base1) + ioff1,
16975 : 12651 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16976 : : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16977 : : {
16978 : 3897 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16979 : 3897 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16980 : 3897 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16981 : : ioffmin) == 0)
16982 : : /* If even the bytes in the string literal before the
16983 : : pointers are the same, the string literals could be
16984 : : tail merged. */
16985 : : return 2;
16986 : : }
16987 : : return 0;
16988 : : }
16989 : :
16990 : 14030 : if (folding_cxx_constexpr)
16991 : : return 0;
16992 : :
16993 : : /* If this is a pointer comparison, ignore for now even
16994 : : valid equalities where one pointer is the offset zero
16995 : : of one object and the other to one past end of another one. */
16996 : 9861 : if (!INTEGRAL_TYPE_P (type))
16997 : : return 0;
16998 : :
16999 : : /* Assume that string literals can't be adjacent to variables
17000 : : (automatic or global). */
17001 : 300 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
17002 : : return 0;
17003 : :
17004 : : /* Assume that automatic variables can't be adjacent to global
17005 : : variables. */
17006 : 295 : if (is_global_var (base0) != is_global_var (base1))
17007 : : return 0;
17008 : :
17009 : : return equal;
17010 : : }
17011 : :
17012 : : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
17013 : : tree
17014 : 52 : ctor_single_nonzero_element (const_tree t)
17015 : : {
17016 : 52 : unsigned HOST_WIDE_INT idx;
17017 : 52 : constructor_elt *ce;
17018 : 52 : tree elt = NULL_TREE;
17019 : :
17020 : 52 : if (TREE_CODE (t) != CONSTRUCTOR)
17021 : : return NULL_TREE;
17022 : 113 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
17023 : 110 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
17024 : : {
17025 : 101 : if (elt)
17026 : : return NULL_TREE;
17027 : 52 : elt = ce->value;
17028 : : }
17029 : : return elt;
17030 : : }
17031 : :
17032 : : #if CHECKING_P
17033 : :
17034 : : namespace selftest {
17035 : :
17036 : : /* Helper functions for writing tests of folding trees. */
17037 : :
17038 : : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17039 : :
17040 : : static void
17041 : 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
17042 : : tree constant)
17043 : : {
17044 : 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
17045 : 16 : }
17046 : :
17047 : : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17048 : : wrapping WRAPPED_EXPR. */
17049 : :
17050 : : static void
17051 : 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
17052 : : tree wrapped_expr)
17053 : : {
17054 : 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
17055 : 12 : ASSERT_NE (wrapped_expr, result);
17056 : 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
17057 : 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
17058 : 12 : }
17059 : :
17060 : : /* Verify that various arithmetic binary operations are folded
17061 : : correctly. */
17062 : :
17063 : : static void
17064 : 4 : test_arithmetic_folding ()
17065 : : {
17066 : 4 : tree type = integer_type_node;
17067 : 4 : tree x = create_tmp_var_raw (type, "x");
17068 : 4 : tree zero = build_zero_cst (type);
17069 : 4 : tree one = build_int_cst (type, 1);
17070 : :
17071 : : /* Addition. */
17072 : : /* 1 <-- (0 + 1) */
17073 : 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
17074 : : one);
17075 : 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
17076 : : one);
17077 : :
17078 : : /* (nonlvalue)x <-- (x + 0) */
17079 : 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
17080 : : x);
17081 : :
17082 : : /* Subtraction. */
17083 : : /* 0 <-- (x - x) */
17084 : 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
17085 : : zero);
17086 : 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
17087 : : x);
17088 : :
17089 : : /* Multiplication. */
17090 : : /* 0 <-- (x * 0) */
17091 : 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
17092 : : zero);
17093 : :
17094 : : /* (nonlvalue)x <-- (x * 1) */
17095 : 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
17096 : : x);
17097 : 4 : }
17098 : :
17099 : : namespace test_operand_equality {
17100 : :
17101 : : /* Verify structural equality. */
17102 : :
17103 : : /* Execute fold_vec_perm_cst unit tests. */
17104 : :
17105 : : static void
17106 : 4 : test ()
17107 : : {
17108 : 4 : tree stype = integer_type_node;
17109 : 4 : tree utype = unsigned_type_node;
17110 : 4 : tree x = create_tmp_var_raw (stype, "x");
17111 : 4 : tree y = create_tmp_var_raw (stype, "y");
17112 : 4 : tree z = create_tmp_var_raw (stype, "z");
17113 : 4 : tree four = build_int_cst (stype, 4);
17114 : 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
17115 : 4 : tree rhs1 = fold_convert (stype,
17116 : : fold_build2 (PLUS_EXPR, utype,
17117 : : fold_convert (utype, x),
17118 : : fold_convert (utype, y)));
17119 : :
17120 : : /* (int)((unsigned x) + (unsigned y)) == x + y. */
17121 : 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17122 : 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17123 : :
17124 : : /* (int)(unsigned) x == x. */
17125 : 4 : tree lhs2 = build1 (NOP_EXPR, stype,
17126 : : build1 (NOP_EXPR, utype, x));
17127 : 4 : tree rhs2 = x;
17128 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17129 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17130 : :
17131 : : /* (unsigned x) + (unsigned y) == x + y. */
17132 : 4 : tree lhs3 = lhs1;
17133 : 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17134 : : fold_convert (utype, x),
17135 : : fold_convert (utype, y));
17136 : 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17137 : 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17138 : :
17139 : : /* (unsigned x) / (unsigned y) == x / y. */
17140 : 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17141 : 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17142 : : fold_convert (utype, x),
17143 : : fold_convert (utype, y));
17144 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17145 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17146 : :
17147 : : /* (long x) / 4 == (long)(x / 4). */
17148 : 4 : tree lstype = long_long_integer_type_node;
17149 : 4 : tree lfour = build_int_cst (lstype, 4);
17150 : 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17151 : : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17152 : 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17153 : : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17154 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17155 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17156 : :
17157 : : /* (unsigned x) / 4 == x / 4. */
17158 : 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17159 : 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17160 : : fold_convert (utype, x),
17161 : : fold_convert (utype, four));
17162 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17163 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17164 : :
17165 : : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17166 : 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17167 : 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17168 : 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17169 : 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17170 : :
17171 : : /* (unsigned x) + 4 == x + 4. */
17172 : 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17173 : 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17174 : : fold_convert (utype, x),
17175 : : fold_convert (utype, four));
17176 : 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17177 : 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17178 : :
17179 : : /* (unsigned x) + 4 == 4 + x. */
17180 : 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17181 : 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17182 : : fold_convert (utype, x),
17183 : : fold_convert (utype, four));
17184 : 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17185 : 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17186 : :
17187 : : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17188 : 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17189 : : fold_build2 (MULT_EXPR, stype,
17190 : : fold_build2 (PLUS_EXPR, stype, four, x),
17191 : : y),
17192 : : z);
17193 : 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17194 : : fold_build2 (PLUS_EXPR, utype,
17195 : : fold_convert (utype, x),
17196 : : fold_convert (utype, four)),
17197 : : fold_convert (utype, y));
17198 : 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17199 : 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17200 : 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17201 : 4 : }
17202 : : }
17203 : :
17204 : : namespace test_fold_vec_perm_cst {
17205 : :
17206 : : /* Build a VECTOR_CST corresponding to VMODE, and has
17207 : : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17208 : : Fill it with randomized elements, using rand() % THRESHOLD. */
17209 : :
17210 : : static tree
17211 : 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17212 : : unsigned nelts_per_pattern,
17213 : : int step = 0, bool natural_stepped = false,
17214 : : int threshold = 100)
17215 : : {
17216 : 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17217 : 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17218 : 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17219 : :
17220 : : // Fill a0 for each pattern
17221 : 0 : for (unsigned i = 0; i < npatterns; i++)
17222 : 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17223 : :
17224 : 0 : if (nelts_per_pattern == 1)
17225 : 0 : return builder.build ();
17226 : :
17227 : : // Fill a1 for each pattern
17228 : 0 : for (unsigned i = 0; i < npatterns; i++)
17229 : : {
17230 : 0 : tree a1;
17231 : 0 : if (natural_stepped)
17232 : : {
17233 : 0 : tree a0 = builder[i];
17234 : 0 : wide_int a0_val = wi::to_wide (a0);
17235 : 0 : wide_int a1_val = a0_val + step;
17236 : 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17237 : 0 : }
17238 : : else
17239 : 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17240 : 0 : builder.quick_push (a1);
17241 : : }
17242 : 0 : if (nelts_per_pattern == 2)
17243 : 0 : return builder.build ();
17244 : :
17245 : 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17246 : : {
17247 : 0 : tree prev_elem = builder[i - npatterns];
17248 : 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17249 : 0 : wide_int val = prev_elem_val + step;
17250 : 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17251 : 0 : }
17252 : :
17253 : 0 : return builder.build ();
17254 : 0 : }
17255 : :
17256 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17257 : : when result is VLA. */
17258 : :
17259 : : static void
17260 : 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17261 : : tree res, tree *expected_res)
17262 : : {
17263 : : /* Actual npatterns and encoded_elts in res may be less than expected due
17264 : : to canonicalization. */
17265 : 0 : ASSERT_TRUE (res != NULL_TREE);
17266 : 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17267 : 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17268 : :
17269 : 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17270 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17271 : 0 : }
17272 : :
17273 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17274 : : when the result is VLS. */
17275 : :
17276 : : static void
17277 : 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17278 : : {
17279 : 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17280 : 0 : for (unsigned i = 0; i < expected_nelts; i++)
17281 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17282 : 0 : }
17283 : :
17284 : : /* Helper routine to push multiple elements into BUILDER. */
17285 : : template<unsigned N>
17286 : 0 : static void builder_push_elems (vec_perm_builder& builder,
17287 : : poly_uint64 (&elems)[N])
17288 : : {
17289 : 0 : for (unsigned i = 0; i < N; i++)
17290 : 0 : builder.quick_push (elems[i]);
17291 : 0 : }
17292 : :
17293 : : #define ARG0(index) vector_cst_elt (arg0, index)
17294 : : #define ARG1(index) vector_cst_elt (arg1, index)
17295 : :
17296 : : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17297 : :
17298 : : static void
17299 : 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17300 : : {
17301 : 0 : for (int i = 0; i < 10; i++)
17302 : : {
17303 : : /* Case 1:
17304 : : sel = { 0, 4, 1, 5, ... }
17305 : : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17306 : 0 : {
17307 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17308 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17309 : :
17310 : 0 : tree inner_type
17311 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17312 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17313 : :
17314 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17315 : 0 : vec_perm_builder builder (res_len, 4, 1);
17316 : 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17317 : 0 : builder_push_elems (builder, mask_elems);
17318 : :
17319 : 0 : vec_perm_indices sel (builder, 2, res_len);
17320 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17321 : :
17322 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17323 : 0 : validate_res (4, 1, res, expected_res);
17324 : 0 : }
17325 : :
17326 : : /* Case 2: Same as case 1, but contains an out of bounds access which
17327 : : should wrap around.
17328 : : sel = {0, 8, 4, 12, ...} (4, 1)
17329 : : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17330 : 0 : {
17331 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17332 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17333 : :
17334 : 0 : tree inner_type
17335 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17336 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17337 : :
17338 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17339 : 0 : vec_perm_builder builder (res_len, 4, 1);
17340 : 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17341 : 0 : builder_push_elems (builder, mask_elems);
17342 : :
17343 : 0 : vec_perm_indices sel (builder, 2, res_len);
17344 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17345 : :
17346 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17347 : 0 : validate_res (4, 1, res, expected_res);
17348 : 0 : }
17349 : : }
17350 : 0 : }
17351 : :
17352 : : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17353 : :
17354 : : static void
17355 : 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17356 : : {
17357 : 0 : for (int i = 0; i < 10; i++)
17358 : : {
17359 : : /* Case 1:
17360 : : sel = { 0, 1, 2, 3}
17361 : : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17362 : 0 : {
17363 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17364 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17365 : :
17366 : 0 : tree inner_type
17367 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17368 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17369 : :
17370 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17371 : 0 : vec_perm_builder builder (res_len, 4, 1);
17372 : 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17373 : 0 : builder_push_elems (builder, mask_elems);
17374 : :
17375 : 0 : vec_perm_indices sel (builder, 2, res_len);
17376 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17377 : :
17378 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17379 : 0 : validate_res_vls (res, expected_res, 4);
17380 : 0 : }
17381 : :
17382 : : /* Case 2: Same as Case 1, but crossing input vector.
17383 : : sel = {0, 2, 4, 6}
17384 : : In this case,the index 4 is ambiguous since len = 4 + 4x.
17385 : : Since we cannot determine, which vector to choose from during
17386 : : compile time, should return NULL_TREE. */
17387 : 0 : {
17388 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17389 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17390 : :
17391 : 0 : tree inner_type
17392 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17393 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17394 : :
17395 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17396 : 0 : vec_perm_builder builder (res_len, 4, 1);
17397 : 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17398 : 0 : builder_push_elems (builder, mask_elems);
17399 : :
17400 : 0 : vec_perm_indices sel (builder, 2, res_len);
17401 : 0 : const char *reason;
17402 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17403 : :
17404 : 0 : ASSERT_TRUE (res == NULL_TREE);
17405 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17406 : 0 : }
17407 : : }
17408 : 0 : }
17409 : :
17410 : : /* Test all input vectors. */
17411 : :
17412 : : static void
17413 : 0 : test_all_nunits (machine_mode vmode)
17414 : : {
17415 : : /* Test with 10 different inputs. */
17416 : 0 : for (int i = 0; i < 10; i++)
17417 : : {
17418 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17419 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17420 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17421 : :
17422 : : /* Case 1: mask = {0, ...} // (1, 1)
17423 : : res = { arg0[0], ... } // (1, 1) */
17424 : 0 : {
17425 : 0 : vec_perm_builder builder (len, 1, 1);
17426 : 0 : builder.quick_push (0);
17427 : 0 : vec_perm_indices sel (builder, 2, len);
17428 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17429 : 0 : tree expected_res[] = { ARG0(0) };
17430 : 0 : validate_res (1, 1, res, expected_res);
17431 : 0 : }
17432 : :
17433 : : /* Case 2: mask = {len, ...} // (1, 1)
17434 : : res = { arg1[0], ... } // (1, 1) */
17435 : 0 : {
17436 : 0 : vec_perm_builder builder (len, 1, 1);
17437 : 0 : builder.quick_push (len);
17438 : 0 : vec_perm_indices sel (builder, 2, len);
17439 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17440 : :
17441 : 0 : tree expected_res[] = { ARG1(0) };
17442 : 0 : validate_res (1, 1, res, expected_res);
17443 : 0 : }
17444 : : }
17445 : 0 : }
17446 : :
17447 : : /* Test all vectors which contain at-least 2 elements. */
17448 : :
17449 : : static void
17450 : 0 : test_nunits_min_2 (machine_mode vmode)
17451 : : {
17452 : 0 : for (int i = 0; i < 10; i++)
17453 : : {
17454 : : /* Case 1: mask = { 0, len, ... } // (2, 1)
17455 : : res = { arg0[0], arg1[0], ... } // (2, 1) */
17456 : 0 : {
17457 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17458 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17459 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17460 : :
17461 : 0 : vec_perm_builder builder (len, 2, 1);
17462 : 0 : poly_uint64 mask_elems[] = { 0, len };
17463 : 0 : builder_push_elems (builder, mask_elems);
17464 : :
17465 : 0 : vec_perm_indices sel (builder, 2, len);
17466 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17467 : :
17468 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17469 : 0 : validate_res (2, 1, res, expected_res);
17470 : 0 : }
17471 : :
17472 : : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17473 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17474 : 0 : {
17475 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17476 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17477 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17478 : :
17479 : 0 : vec_perm_builder builder (len, 2, 2);
17480 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17481 : 0 : builder_push_elems (builder, mask_elems);
17482 : :
17483 : 0 : vec_perm_indices sel (builder, 2, len);
17484 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17485 : :
17486 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17487 : 0 : validate_res (2, 2, res, expected_res);
17488 : 0 : }
17489 : :
17490 : : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17491 : : Test that the stepped sequence of the pattern selects from
17492 : : same input pattern. Since input vectors have npatterns = 2,
17493 : : and step (a2 - a1) = 1, step is not a multiple of npatterns
17494 : : in input vector. So return NULL_TREE. */
17495 : 0 : {
17496 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17497 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17498 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17499 : :
17500 : 0 : vec_perm_builder builder (len, 1, 3);
17501 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17502 : 0 : builder_push_elems (builder, mask_elems);
17503 : :
17504 : 0 : vec_perm_indices sel (builder, 2, len);
17505 : 0 : const char *reason;
17506 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17507 : : &reason);
17508 : 0 : ASSERT_TRUE (res == NULL_TREE);
17509 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17510 : 0 : }
17511 : :
17512 : : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17513 : : Test that stepped sequence of the pattern selects from arg0.
17514 : : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17515 : 0 : {
17516 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17517 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17518 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17519 : :
17520 : 0 : vec_perm_builder builder (len, 1, 3);
17521 : 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17522 : 0 : builder_push_elems (builder, mask_elems);
17523 : :
17524 : 0 : vec_perm_indices sel (builder, 2, len);
17525 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17526 : :
17527 : 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17528 : 0 : validate_res (1, 3, res, expected_res);
17529 : 0 : }
17530 : :
17531 : : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17532 : : In this case ensure that arg has a natural stepped sequence
17533 : : to preserve arg's encoding.
17534 : :
17535 : : As a concrete example, consider:
17536 : : arg0: { -16, -9, -10, ... } // (1, 3)
17537 : : arg1: { -12, -5, -6, ... } // (1, 3)
17538 : : sel = { 0, len, len + 1, ... } // (1, 3)
17539 : :
17540 : : This will create res with following encoding:
17541 : : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17542 : : = { -16, -12, -5, ... }
17543 : :
17544 : : The step in above encoding would be: (-5) - (-12) = 7
17545 : : And hence res[3] would be computed as -5 + 7 = 2.
17546 : : instead of arg1[2], ie, -6.
17547 : : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17548 : : for this case. */
17549 : 0 : {
17550 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17551 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17552 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17553 : :
17554 : 0 : vec_perm_builder builder (len, 1, 3);
17555 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17556 : 0 : builder_push_elems (builder, mask_elems);
17557 : :
17558 : 0 : vec_perm_indices sel (builder, 2, len);
17559 : 0 : const char *reason;
17560 : : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17561 : : stepped pattern, even if we didn't explicitly tell it to. So folding
17562 : : may not always fail, but if it does, ensure that's because arg1 does
17563 : : not have a natural stepped sequence (and not due to other reason) */
17564 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17565 : 0 : if (res == NULL_TREE)
17566 : 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17567 : 0 : }
17568 : :
17569 : : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17570 : : sequence and thus folding should be valid for this case. */
17571 : 0 : {
17572 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17573 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17574 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17575 : :
17576 : 0 : vec_perm_builder builder (len, 1, 3);
17577 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17578 : 0 : builder_push_elems (builder, mask_elems);
17579 : :
17580 : 0 : vec_perm_indices sel (builder, 2, len);
17581 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17582 : :
17583 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17584 : 0 : validate_res (1, 3, res, expected_res);
17585 : 0 : }
17586 : :
17587 : : /* Case 8: Same as aarch64/sve/slp_3.c:
17588 : : arg0, arg1 are dup vectors.
17589 : : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17590 : : So res = { arg0[0], arg1[0], ... } // (2, 1)
17591 : :
17592 : : In this case, since the input vectors are dup, only the first two
17593 : : elements per pattern in sel are considered significant. */
17594 : 0 : {
17595 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17596 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17597 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17598 : :
17599 : 0 : vec_perm_builder builder (len, 2, 3);
17600 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17601 : 0 : builder_push_elems (builder, mask_elems);
17602 : :
17603 : 0 : vec_perm_indices sel (builder, 2, len);
17604 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17605 : :
17606 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17607 : 0 : validate_res (2, 1, res, expected_res);
17608 : 0 : }
17609 : : }
17610 : 0 : }
17611 : :
17612 : : /* Test all vectors which contain at-least 4 elements. */
17613 : :
17614 : : static void
17615 : 0 : test_nunits_min_4 (machine_mode vmode)
17616 : : {
17617 : 0 : for (int i = 0; i < 10; i++)
17618 : : {
17619 : : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17620 : : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17621 : 0 : {
17622 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17623 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17624 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17625 : :
17626 : 0 : vec_perm_builder builder (len, 4, 1);
17627 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17628 : 0 : builder_push_elems (builder, mask_elems);
17629 : :
17630 : 0 : vec_perm_indices sel (builder, 2, len);
17631 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17632 : :
17633 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17634 : 0 : validate_res (4, 1, res, expected_res);
17635 : 0 : }
17636 : :
17637 : : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17638 : : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17639 : 0 : {
17640 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17641 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17642 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17643 : :
17644 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17645 : 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17646 : 0 : builder_push_elems (builder, mask_elems);
17647 : :
17648 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17649 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17650 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17651 : 0 : validate_res (1, 3, res, expected_res);
17652 : 0 : }
17653 : :
17654 : : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17655 : : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17656 : 0 : {
17657 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17658 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17659 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17660 : :
17661 : 0 : vec_perm_builder builder (len, 1, 3);
17662 : 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17663 : 0 : builder_push_elems (builder, mask_elems);
17664 : :
17665 : 0 : vec_perm_indices sel (builder, 2, len);
17666 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17667 : 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17668 : 0 : validate_res (1, 3, res, expected_res);
17669 : 0 : }
17670 : :
17671 : : /* Case 4:
17672 : : sel = { len, 0, 2, ... } // (1, 3)
17673 : : This should return NULL because we cross the input vectors.
17674 : : Because,
17675 : : Let's assume len = C + Cx
17676 : : a1 = 0
17677 : : S = 2
17678 : : esel = arg0_len / sel_npatterns = C + Cx
17679 : : ae = 0 + (esel - 2) * S
17680 : : = 0 + (C + Cx - 2) * 2
17681 : : = 2(C-2) + 2Cx
17682 : :
17683 : : For C >= 4:
17684 : : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17685 : : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17686 : : Since q1 != qe, we cross input vectors.
17687 : : So return NULL_TREE. */
17688 : 0 : {
17689 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17690 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17691 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17692 : :
17693 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17694 : 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17695 : 0 : builder_push_elems (builder, mask_elems);
17696 : :
17697 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17698 : 0 : const char *reason;
17699 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17700 : 0 : ASSERT_TRUE (res == NULL_TREE);
17701 : 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17702 : 0 : }
17703 : :
17704 : : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17705 : : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17706 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17707 : :
17708 : : Note that fold_vec_perm_cst will set
17709 : : res_npatterns = max(4, max(4, 2)) = 4
17710 : : However after canonicalizing, we will end up with shape (2, 2). */
17711 : 0 : {
17712 : 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17713 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17714 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17715 : :
17716 : 0 : vec_perm_builder builder (len, 2, 2);
17717 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17718 : 0 : builder_push_elems (builder, mask_elems);
17719 : :
17720 : 0 : vec_perm_indices sel (builder, 2, len);
17721 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17722 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17723 : 0 : validate_res (2, 2, res, expected_res);
17724 : 0 : }
17725 : :
17726 : : /* Case 6: Test combination in sel, where one pattern is dup and other
17727 : : is stepped sequence.
17728 : : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17729 : : res = { arg0[0], arg0[0], arg0[0],
17730 : : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17731 : 0 : {
17732 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17733 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17734 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17735 : :
17736 : 0 : vec_perm_builder builder (len, 2, 3);
17737 : 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17738 : 0 : builder_push_elems (builder, mask_elems);
17739 : :
17740 : 0 : vec_perm_indices sel (builder, 2, len);
17741 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17742 : :
17743 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17744 : 0 : ARG0(1), ARG0(0), ARG0(2) };
17745 : 0 : validate_res (2, 3, res, expected_res);
17746 : 0 : }
17747 : :
17748 : : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17749 : : when arg0, arg1 and sel have different number of patterns.
17750 : : arg0 is of shape (1, 1)
17751 : : arg1 is of shape (4, 1)
17752 : : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17753 : :
17754 : : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17755 : : However,
17756 : : step = (len+2) - (len+1) = 1
17757 : : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17758 : : Since step is not a multiple of arg_npatterns,
17759 : : valid_mask_for_fold_vec_perm_cst should return false,
17760 : : and thus fold_vec_perm_cst should return NULL_TREE. */
17761 : 0 : {
17762 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17763 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17764 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17765 : :
17766 : 0 : vec_perm_builder builder (len, 2, 3);
17767 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17768 : 0 : builder_push_elems (builder, mask_elems);
17769 : :
17770 : 0 : vec_perm_indices sel (builder, 2, len);
17771 : 0 : const char *reason;
17772 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17773 : :
17774 : 0 : ASSERT_TRUE (res == NULL_TREE);
17775 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17776 : 0 : }
17777 : :
17778 : : /* Case 8: PR111754: When input vector is not a stepped sequence,
17779 : : check that the result is not a stepped sequence either, even
17780 : : if sel has a stepped sequence. */
17781 : 0 : {
17782 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17783 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17784 : :
17785 : 0 : vec_perm_builder builder (len, 1, 3);
17786 : 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17787 : 0 : builder_push_elems (builder, mask_elems);
17788 : :
17789 : 0 : vec_perm_indices sel (builder, 1, len);
17790 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17791 : :
17792 : 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17793 : 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17794 : 0 : }
17795 : :
17796 : : /* Case 9: If sel doesn't contain a stepped sequence,
17797 : : check that the result has same encoding as sel, irrespective
17798 : : of shape of input vectors. */
17799 : 0 : {
17800 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17801 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17802 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17803 : :
17804 : 0 : vec_perm_builder builder (len, 1, 2);
17805 : 0 : poly_uint64 mask_elems[] = { 0, len };
17806 : 0 : builder_push_elems (builder, mask_elems);
17807 : :
17808 : 0 : vec_perm_indices sel (builder, 2, len);
17809 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17810 : :
17811 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17812 : 0 : validate_res (sel.encoding ().npatterns (),
17813 : 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17814 : 0 : }
17815 : : }
17816 : 0 : }
17817 : :
17818 : : /* Test all vectors which contain at-least 8 elements. */
17819 : :
17820 : : static void
17821 : 0 : test_nunits_min_8 (machine_mode vmode)
17822 : : {
17823 : 0 : for (int i = 0; i < 10; i++)
17824 : : {
17825 : : /* Case 1: sel_npatterns (4) > input npatterns (2)
17826 : : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17827 : : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17828 : : arg0[2], arg0[0], arg0[3], arg1[0],
17829 : : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17830 : 0 : {
17831 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17832 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17833 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17834 : :
17835 : 0 : vec_perm_builder builder(len, 4, 3);
17836 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17837 : 0 : 4, 0, 5, len };
17838 : 0 : builder_push_elems (builder, mask_elems);
17839 : :
17840 : 0 : vec_perm_indices sel (builder, 2, len);
17841 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17842 : :
17843 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17844 : 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17845 : 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17846 : 0 : validate_res (4, 3, res, expected_res);
17847 : 0 : }
17848 : : }
17849 : 0 : }
17850 : :
17851 : : /* Test vectors for which nunits[0] <= 4. */
17852 : :
17853 : : static void
17854 : 0 : test_nunits_max_4 (machine_mode vmode)
17855 : : {
17856 : : /* Case 1: mask = {0, 4, ...} // (1, 2)
17857 : : This should return NULL_TREE because the index 4 may choose
17858 : : from either arg0 or arg1 depending on vector length. */
17859 : 0 : {
17860 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17861 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17862 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17863 : :
17864 : 0 : vec_perm_builder builder (len, 1, 2);
17865 : 0 : poly_uint64 mask_elems[] = {0, 4};
17866 : 0 : builder_push_elems (builder, mask_elems);
17867 : :
17868 : 0 : vec_perm_indices sel (builder, 2, len);
17869 : 0 : const char *reason;
17870 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17871 : 0 : ASSERT_TRUE (res == NULL_TREE);
17872 : 0 : ASSERT_TRUE (reason != NULL);
17873 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17874 : 0 : }
17875 : 0 : }
17876 : :
17877 : : #undef ARG0
17878 : : #undef ARG1
17879 : :
17880 : : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17881 : :
17882 : : static bool
17883 : 0 : is_simple_vla_size (poly_uint64 size)
17884 : : {
17885 : 124 : if (size.is_constant ()
17886 : : || !pow2p_hwi (size.coeffs[0]))
17887 : 0 : return false;
17888 : : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17889 : : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17890 : : return false;
17891 : : return true;
17892 : : }
17893 : :
17894 : : /* Execute fold_vec_perm_cst unit tests. */
17895 : :
17896 : : static void
17897 : 4 : test ()
17898 : : {
17899 : 4 : machine_mode vnx4si_mode = E_VOIDmode;
17900 : 4 : machine_mode v4si_mode = E_VOIDmode;
17901 : :
17902 : 4 : machine_mode vmode;
17903 : 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17904 : : {
17905 : : /* Obtain modes corresponding to VNx4SI and V4SI,
17906 : : to call mixed mode tests below.
17907 : : FIXME: Is there a better way to do this ? */
17908 : 124 : if (GET_MODE_INNER (vmode) == SImode)
17909 : : {
17910 : 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17911 : 124 : if (is_simple_vla_size (nunits)
17912 : : && nunits.coeffs[0] == 4)
17913 : : vnx4si_mode = vmode;
17914 : 124 : else if (known_eq (nunits, poly_uint64 (4)))
17915 : 124 : v4si_mode = vmode;
17916 : : }
17917 : :
17918 : 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17919 : : || !targetm.vector_mode_supported_p (vmode))
17920 : 124 : continue;
17921 : :
17922 : : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17923 : : test_all_nunits (vmode);
17924 : : if (nunits.coeffs[0] >= 2)
17925 : : test_nunits_min_2 (vmode);
17926 : : if (nunits.coeffs[0] >= 4)
17927 : : test_nunits_min_4 (vmode);
17928 : : if (nunits.coeffs[0] >= 8)
17929 : : test_nunits_min_8 (vmode);
17930 : :
17931 : : if (nunits.coeffs[0] <= 4)
17932 : : test_nunits_max_4 (vmode);
17933 : : }
17934 : :
17935 : 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17936 : : && targetm.vector_mode_supported_p (vnx4si_mode)
17937 : : && targetm.vector_mode_supported_p (v4si_mode))
17938 : : {
17939 : : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17940 : : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17941 : : }
17942 : 4 : }
17943 : : } // end of test_fold_vec_perm_cst namespace
17944 : :
17945 : : /* Verify that various binary operations on vectors are folded
17946 : : correctly. */
17947 : :
17948 : : static void
17949 : 4 : test_vector_folding ()
17950 : : {
17951 : 4 : tree inner_type = integer_type_node;
17952 : 4 : tree type = build_vector_type (inner_type, 4);
17953 : 4 : tree zero = build_zero_cst (type);
17954 : 4 : tree one = build_one_cst (type);
17955 : 4 : tree index = build_index_vector (type, 0, 1);
17956 : :
17957 : : /* Verify equality tests that return a scalar boolean result. */
17958 : 4 : tree res_type = boolean_type_node;
17959 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17960 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17961 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17962 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17963 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17964 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17965 : : index, one)));
17966 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17967 : : index, index)));
17968 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17969 : : index, index)));
17970 : 4 : }
17971 : :
17972 : : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17973 : :
17974 : : static void
17975 : 4 : test_vec_duplicate_folding ()
17976 : : {
17977 : 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17978 : 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17979 : : /* This will be 1 if VEC_MODE isn't a vector mode. */
17980 : 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17981 : :
17982 : 4 : tree type = build_vector_type (ssizetype, nunits);
17983 : 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17984 : 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17985 : 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17986 : 4 : }
17987 : :
17988 : : /* Run all of the selftests within this file. */
17989 : :
17990 : : void
17991 : 4 : fold_const_cc_tests ()
17992 : : {
17993 : 4 : test_arithmetic_folding ();
17994 : 4 : test_vector_folding ();
17995 : 4 : test_vec_duplicate_folding ();
17996 : 4 : test_fold_vec_perm_cst::test ();
17997 : 4 : test_operand_equality::test ();
17998 : 4 : }
17999 : :
18000 : : } // namespace selftest
18001 : :
18002 : : #endif /* CHECKING_P */
|