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 : 127010 : minmax_from_comparison (tree_code cmp, tree exp0,
157 : : const widest_int cst1,
158 : : const widest_int cst2)
159 : : {
160 : 127010 : if (cst1 == cst2)
161 : : {
162 : 279 : if (cmp == LE_EXPR || cmp == LT_EXPR)
163 : : return MIN_EXPR;
164 : 115 : if (cmp == GT_EXPR || cmp == GE_EXPR)
165 : : return MAX_EXPR;
166 : : }
167 : 126846 : if (cst1 == cst2 - 1)
168 : : {
169 : : /* X <= Y - 1 equals to X < Y. */
170 : 79048 : if (cmp == LE_EXPR)
171 : : return MIN_EXPR;
172 : : /* X > Y - 1 equals to X >= Y. */
173 : 78658 : 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 : 68987 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
177 : : {
178 : 17198 : int_range_max r;
179 : 34396 : get_range_query (cfun)->range_of_expr (r, exp0);
180 : 17198 : if (r.undefined_p ())
181 : 0 : r.set_varying (TREE_TYPE (exp0));
182 : :
183 : 17198 : widest_int min = widest_int::from (r.lower_bound (),
184 : 34396 : TYPE_SIGN (TREE_TYPE (exp0)));
185 : 17198 : if (min == cst1)
186 : 764 : return MAX_EXPR;
187 : 17198 : }
188 : : }
189 : 116021 : 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 : 140058 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
219 : : {
220 : 140058 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
221 : 11 : return ERROR_MARK;
222 : :
223 : 140047 : if (!operand_equal_p (exp0, exp2))
224 : : return ERROR_MARK;
225 : :
226 : 140047 : if (operand_equal_p (exp1, exp3))
227 : : {
228 : 13386 : if (cmp == LT_EXPR || cmp == LE_EXPR)
229 : : return MIN_EXPR;
230 : 11395 : if (cmp == GT_EXPR || cmp == GE_EXPR)
231 : : return MAX_EXPR;
232 : : }
233 : 126776 : if (TREE_CODE (exp3) == INTEGER_CST
234 : 126692 : && TREE_CODE (exp1) == INTEGER_CST)
235 : 126166 : 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 : 2428350 : expr_location_or (tree t, location_t loc)
244 : : {
245 : 656169 : location_t tloc = EXPR_LOCATION (t);
246 : 2412566 : 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 : 5845862 : protected_set_expr_location_unshare (tree x, location_t loc)
254 : : {
255 : 5845862 : if (CAN_HAVE_LOCATION_P (x)
256 : 5227238 : && EXPR_LOCATION (x) != loc
257 : 2774857 : && !(TREE_CODE (x) == SAVE_EXPR
258 : 1387658 : || TREE_CODE (x) == TARGET_EXPR
259 : : || TREE_CODE (x) == BIND_EXPR))
260 : : {
261 : 1386887 : x = copy_node (x);
262 : 1386887 : SET_EXPR_LOCATION (x, loc);
263 : : }
264 : 5845862 : 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 : 1198621980 : fold_defer_overflow_warnings (void)
295 : : {
296 : 1198621980 : ++fold_deferring_overflow_warnings;
297 : 1198621980 : }
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 : 1198621980 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
310 : : {
311 : 1198621980 : const char *warnmsg;
312 : 1198621980 : location_t locus;
313 : :
314 : 1198621980 : gcc_assert (fold_deferring_overflow_warnings > 0);
315 : 1198621980 : --fold_deferring_overflow_warnings;
316 : 1198621980 : if (fold_deferring_overflow_warnings > 0)
317 : : {
318 : 9075233 : if (fold_deferred_overflow_warning != NULL
319 : 1779284 : && code != 0
320 : 0 : && code < (int) fold_deferred_overflow_code)
321 : 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
322 : 9075233 : return;
323 : : }
324 : :
325 : 1189546747 : warnmsg = fold_deferred_overflow_warning;
326 : 1189546747 : fold_deferred_overflow_warning = NULL;
327 : :
328 : 1189546747 : if (!issue || warnmsg == NULL)
329 : : return;
330 : :
331 : 10661 : 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 : 10661 : if (code == 0 || code > (int) fold_deferred_overflow_code)
337 : 10661 : code = fold_deferred_overflow_code;
338 : :
339 : 10661 : 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 : 186933478 : fold_undefer_and_ignore_overflow_warnings (void)
354 : : {
355 : 186933478 : fold_undefer_overflow_warnings (false, NULL, 0);
356 : 186933478 : }
357 : :
358 : : /* Whether we are deferring overflow warnings. */
359 : :
360 : : bool
361 : 322664757 : fold_deferring_overflow_warnings_p (void)
362 : : {
363 : 322664757 : 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 : 1735833 : fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
371 : : {
372 : 1735833 : if (fold_deferring_overflow_warnings > 0)
373 : : {
374 : 1684492 : if (fold_deferred_overflow_warning == NULL
375 : 738097 : || wc < fold_deferred_overflow_code)
376 : : {
377 : 969305 : fold_deferred_overflow_warning = gmsgid;
378 : 969305 : fold_deferred_overflow_code = wc;
379 : : }
380 : : }
381 : 51341 : else if (issue_strict_overflow_warning (wc))
382 : 7 : warning (OPT_Wstrict_overflow, gmsgid);
383 : 1735833 : }
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 : 2053371 : negate_mathfn_p (combined_fn fn)
390 : : {
391 : 2053371 : 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 : 2049370 : default:
461 : 2049370 : break;
462 : : }
463 : 2049370 : return false;
464 : : }
465 : :
466 : : /* Check whether we may negate an integer constant T without causing
467 : : overflow. */
468 : :
469 : : bool
470 : 3104711 : may_negate_without_overflow_p (const_tree t)
471 : : {
472 : 3104711 : tree type;
473 : :
474 : 3104711 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
475 : :
476 : 3104711 : type = TREE_TYPE (t);
477 : 3104711 : if (TYPE_UNSIGNED (type))
478 : : return false;
479 : :
480 : 3104711 : 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 : 27148512 : negate_expr_p (tree t)
488 : : {
489 : 27305225 : tree type;
490 : :
491 : 27305225 : if (t == 0)
492 : : return false;
493 : :
494 : 27305225 : type = TREE_TYPE (t);
495 : :
496 : 27305225 : STRIP_SIGN_NOPS (t);
497 : 27305225 : switch (TREE_CODE (t))
498 : : {
499 : 1593789 : case INTEGER_CST:
500 : 1593789 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
501 : : return true;
502 : :
503 : : /* Check that -CST will not overflow type. */
504 : 382586 : 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 : 1348838 : case REAL_CST:
516 : : /* We want to canonicalize to positive real constants. Pretend
517 : : that only negative ones can be easily negated. */
518 : 1348838 : 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 : 1534197 : case PLUS_EXPR:
546 : 1534197 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
547 : 1534191 : || HONOR_SIGNED_ZEROS (type)
548 : 2767475 : || (ANY_INTEGRAL_TYPE_P (type)
549 : 1233092 : && ! TYPE_OVERFLOW_WRAPS (type)))
550 : 754233 : return false;
551 : : /* -(A + B) -> (-B) - A. */
552 : 779964 : if (negate_expr_p (TREE_OPERAND (t, 1)))
553 : : return true;
554 : : /* -(A + B) -> (-A) - B. */
555 : 142131 : return negate_expr_p (TREE_OPERAND (t, 0));
556 : :
557 : 254878 : case MINUS_EXPR:
558 : : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
559 : 254878 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
560 : 254878 : && !HONOR_SIGNED_ZEROS (type)
561 : 328140 : && (! ANY_INTEGRAL_TYPE_P (type)
562 : 73039 : || TYPE_OVERFLOW_WRAPS (type));
563 : :
564 : 2464563 : case MULT_EXPR:
565 : 2464563 : 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 : 4285434 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
570 : 160125 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
571 : 2300504 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
572 : 0 : && (wi::popcount
573 : 2142717 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
574 : 157787 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
575 : 135032 : && (wi::popcount
576 : 4397711 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
577 : : break;
578 : :
579 : : /* Fall through. */
580 : :
581 : 2434408 : case RDIV_EXPR:
582 : 2434408 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
583 : 2434407 : return negate_expr_p (TREE_OPERAND (t, 1))
584 : 2434407 : || 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 : 4807671 : case NOP_EXPR:
608 : : /* Negate -((double)float) as (double)(-float). */
609 : 4807671 : if (SCALAR_FLOAT_TYPE_P (type))
610 : : {
611 : 14320 : tree tem = strip_float_extensions (t);
612 : 14320 : if (tem != t)
613 : : return negate_expr_p (tem);
614 : : }
615 : : break;
616 : :
617 : 1027603 : case CALL_EXPR:
618 : : /* Negate -f(x) as f(-x). */
619 : 1027603 : 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 : 37897827 : fold_negate_expr_1 (location_t loc, tree t)
646 : : {
647 : 37897827 : tree type = TREE_TYPE (t);
648 : 37897827 : tree tem;
649 : :
650 : 37897827 : 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 : 29306559 : case INTEGER_CST:
660 : 29306559 : tem = fold_negate_const (t, type);
661 : 29306559 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
662 : 9196 : || (ANY_INTEGRAL_TYPE_P (type)
663 : 9196 : && !TYPE_OVERFLOW_TRAPS (type)
664 : 9196 : && TYPE_OVERFLOW_WRAPS (type))
665 : 29314967 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
666 : : return tem;
667 : : break;
668 : :
669 : 2053563 : case POLY_INT_CST:
670 : 2053563 : case REAL_CST:
671 : 2053563 : case FIXED_CST:
672 : 2053563 : tem = fold_negate_const (t, type);
673 : 2053563 : 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 : 53010 : case VECTOR_CST:
685 : 53010 : {
686 : 53010 : tree_vector_builder elts;
687 : 53010 : elts.new_unary_operation (type, t, true);
688 : 53010 : unsigned int count = elts.encoded_nelts ();
689 : 127910 : for (unsigned int i = 0; i < count; ++i)
690 : : {
691 : 74900 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
692 : 74900 : if (elt == NULL_TREE)
693 : 0 : return NULL_TREE;
694 : 74900 : elts.quick_push (elt);
695 : : }
696 : :
697 : 53010 : return elts.build ();
698 : 53010 : }
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 : 694289 : case PLUS_EXPR:
719 : 694289 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
720 : 694289 : && !HONOR_SIGNED_ZEROS (type))
721 : : {
722 : : /* -(A + B) -> (-B) - A. */
723 : 694179 : if (negate_expr_p (TREE_OPERAND (t, 1)))
724 : : {
725 : 641669 : tem = negate_expr (TREE_OPERAND (t, 1));
726 : 641669 : return fold_build2_loc (loc, MINUS_EXPR, type,
727 : 1283338 : tem, TREE_OPERAND (t, 0));
728 : : }
729 : :
730 : : /* -(A + B) -> (-A) - B. */
731 : 52510 : 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 : 148621 : case MINUS_EXPR:
741 : : /* - (A - B) -> B - A */
742 : 148621 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
743 : 148621 : && !HONOR_SIGNED_ZEROS (type))
744 : 67998 : return fold_build2_loc (loc, MINUS_EXPR, type,
745 : 135996 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
746 : : break;
747 : :
748 : 156827 : case MULT_EXPR:
749 : 156827 : 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 : 1865790 : case NOP_EXPR:
794 : : /* Convert -((double)float) into (double)(-float). */
795 : 1865790 : if (SCALAR_FLOAT_TYPE_P (type))
796 : : {
797 : 11306 : tem = strip_float_extensions (t);
798 : 11306 : if (tem != t && negate_expr_p (tem))
799 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
800 : : }
801 : : break;
802 : :
803 : 306198 : case CALL_EXPR:
804 : : /* Negate -f(x) as f(-x). */
805 : 306198 : if (negate_mathfn_p (get_call_combined_fn (t))
806 : 307487 : && 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 : 408 : case RSHIFT_EXPR:
817 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
818 : 408 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
819 : : {
820 : 390 : tree op1 = TREE_OPERAND (t, 1);
821 : 390 : 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 : 37897827 : fold_negate_expr (location_t loc, tree t)
844 : : {
845 : 37897827 : tree type = TREE_TYPE (t);
846 : 37897827 : STRIP_SIGN_NOPS (t);
847 : 37897827 : tree tem = fold_negate_expr_1 (loc, t);
848 : 37897827 : if (tem == NULL_TREE)
849 : : return NULL_TREE;
850 : 32224857 : 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 : 3620146 : negate_expr (tree t)
859 : : {
860 : 3620146 : tree type, tem;
861 : 3620146 : location_t loc;
862 : :
863 : 3620146 : if (t == NULL_TREE)
864 : : return NULL_TREE;
865 : :
866 : 3620146 : loc = EXPR_LOCATION (t);
867 : 3620146 : type = TREE_TYPE (t);
868 : 3620146 : STRIP_SIGN_NOPS (t);
869 : :
870 : 3620146 : tem = fold_negate_expr (loc, t);
871 : 3620146 : if (!tem)
872 : 1749818 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
873 : 3620146 : 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 : 218830946 : 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 : 218830946 : tree var = 0;
905 : 218830946 : *minus_varp = 0;
906 : 218830946 : *conp = 0;
907 : 218830946 : *minus_conp = 0;
908 : 218830946 : *litp = 0;
909 : 218830946 : *minus_litp = 0;
910 : :
911 : : /* Strip any conversions that don't change the machine mode or signedness. */
912 : 218830946 : STRIP_SIGN_NOPS (in);
913 : :
914 : 218830946 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
915 : 139144526 : || TREE_CODE (in) == FIXED_CST)
916 : 79686420 : *litp = in;
917 : 139144526 : else if (TREE_CODE (in) == code
918 : 139144526 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
919 : 134773845 : && ! 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 : 134773845 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
925 : 56511575 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
926 : 133070673 : || (code == MINUS_EXPR
927 : 21486208 : && (TREE_CODE (in) == PLUS_EXPR
928 : 19732913 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
929 : : {
930 : 8256325 : tree op0 = TREE_OPERAND (in, 0);
931 : 8256325 : tree op1 = TREE_OPERAND (in, 1);
932 : 8256325 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
933 : 8256325 : 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 : 8256325 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
937 : 8026416 : || TREE_CODE (op0) == FIXED_CST)
938 : 229909 : *litp = op0, op0 = 0;
939 : 8026416 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
940 : 5248800 : || TREE_CODE (op1) == FIXED_CST)
941 : 2777616 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
942 : :
943 : 8256325 : if (op0 != 0 && TREE_CONSTANT (op0))
944 : 15206 : *conp = op0, op0 = 0;
945 : 8241119 : else if (op1 != 0 && TREE_CONSTANT (op1))
946 : 52251 : *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 : 8256325 : if (op0 != 0 && op1 != 0)
951 : : var = in;
952 : 3066984 : else if (op0 != 0)
953 : : var = op0;
954 : : else
955 : 245115 : var = op1, neg_var_p = neg1_p;
956 : :
957 : : /* Now do any needed negations. */
958 : 8256325 : if (neg_litp_p)
959 : 45053 : *minus_litp = *litp, *litp = 0;
960 : 8256325 : if (neg_conp_p && *conp)
961 : 11847 : *minus_conp = *conp, *conp = 0;
962 : 8256325 : if (neg_var_p && var)
963 : 233764 : *minus_varp = var, var = 0;
964 : : }
965 : 130888201 : else if (TREE_CONSTANT (in))
966 : 796687 : *conp = in;
967 : 130091514 : else if (TREE_CODE (in) == BIT_NOT_EXPR
968 : 480655 : && code == PLUS_EXPR)
969 : : {
970 : : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
971 : : when IN is constant. */
972 : 372839 : *litp = build_minus_one_cst (type);
973 : 372839 : *minus_varp = TREE_OPERAND (in, 0);
974 : : }
975 : : else
976 : : var = in;
977 : :
978 : 218830946 : if (negate_p)
979 : : {
980 : 11853802 : if (*litp)
981 : 1250247 : *minus_litp = *litp, *litp = 0;
982 : 10603555 : else if (*minus_litp)
983 : 152 : *litp = *minus_litp, *minus_litp = 0;
984 : 11853802 : if (*conp)
985 : 48270 : *minus_conp = *conp, *conp = 0;
986 : 11805532 : else if (*minus_conp)
987 : 0 : *conp = *minus_conp, *minus_conp = 0;
988 : 11853802 : if (var)
989 : 11793302 : *minus_varp = var, var = 0;
990 : 60500 : else if (*minus_varp)
991 : 757 : var = *minus_varp, *minus_varp = 0;
992 : : }
993 : :
994 : 218830946 : if (*litp
995 : 218830946 : && TREE_OVERFLOW_P (*litp))
996 : 18652 : *litp = drop_tree_overflow (*litp);
997 : 218830946 : if (*minus_litp
998 : 218830946 : && TREE_OVERFLOW_P (*minus_litp))
999 : 0 : *minus_litp = drop_tree_overflow (*minus_litp);
1000 : :
1001 : 218830946 : 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 : 21059939 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1011 : : {
1012 : 21059939 : if (t1 == 0)
1013 : : {
1014 : 13363987 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
1015 : : return t2;
1016 : : }
1017 : 7695952 : 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 : 4306722 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1024 : 3389156 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1025 : 3326696 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1026 : : {
1027 : 1696577 : if (code == PLUS_EXPR)
1028 : : {
1029 : 967814 : 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 : 967760 : 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 : 967759 : else if (integer_zerop (t2))
1040 : 58735 : return fold_convert_loc (loc, type, t1);
1041 : : }
1042 : 728763 : else if (code == MINUS_EXPR)
1043 : : {
1044 : 707123 : if (integer_zerop (t2))
1045 : 0 : return fold_convert_loc (loc, type, t1);
1046 : : }
1047 : :
1048 : 1637787 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1049 : 1637787 : fold_convert_loc (loc, type, t2));
1050 : : }
1051 : :
1052 : 2610145 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1053 : 2610145 : 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 : 2189162070 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1061 : : {
1062 : 2189162070 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1063 : : return false;
1064 : 2189162070 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1065 : : return false;
1066 : :
1067 : 2189162070 : switch (code)
1068 : : {
1069 : : case LSHIFT_EXPR:
1070 : : case RSHIFT_EXPR:
1071 : : case LROTATE_EXPR:
1072 : : case RROTATE_EXPR:
1073 : : return true;
1074 : :
1075 : 2189162070 : default:
1076 : 2189162070 : break;
1077 : : }
1078 : :
1079 : 2189162070 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1080 : 2189162070 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1081 : 4378324140 : && 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 : 1223174676 : 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 : 1223174676 : wide_int tmp;
1094 : 1223174676 : *overflow = wi::OVF_NONE;
1095 : 1223174676 : switch (code)
1096 : : {
1097 : 2044411 : case BIT_IOR_EXPR:
1098 : 2044411 : res = wi::bit_or (arg1, arg2);
1099 : 2044411 : break;
1100 : :
1101 : 100390 : case BIT_XOR_EXPR:
1102 : 100390 : res = wi::bit_xor (arg1, arg2);
1103 : 100390 : break;
1104 : :
1105 : 19834433 : case BIT_AND_EXPR:
1106 : 19834433 : res = wi::bit_and (arg1, arg2);
1107 : 19834433 : break;
1108 : :
1109 : 12720427 : case LSHIFT_EXPR:
1110 : 12720427 : if (wi::neg_p (arg2))
1111 : : return false;
1112 : 12690091 : res = wi::lshift (arg1, arg2);
1113 : 12690091 : break;
1114 : :
1115 : 7793841 : case RSHIFT_EXPR:
1116 : 7793841 : 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 : 7793643 : res = wi::rshift (arg1, arg2, sign);
1122 : 7793643 : 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 : 207789954 : case PLUS_EXPR:
1144 : 207789954 : res = wi::add (arg1, arg2, sign, overflow);
1145 : 207789954 : break;
1146 : :
1147 : 70981085 : case MINUS_EXPR:
1148 : 70981085 : res = wi::sub (arg1, arg2, sign, overflow);
1149 : 70981085 : break;
1150 : :
1151 : 333973281 : case MULT_EXPR:
1152 : 333973281 : res = wi::mul (arg1, arg2, sign, overflow);
1153 : 333973281 : break;
1154 : :
1155 : 5072 : case MULT_HIGHPART_EXPR:
1156 : 5072 : res = wi::mul_high (arg1, arg2, sign);
1157 : 5072 : break;
1158 : :
1159 : 295071421 : case TRUNC_DIV_EXPR:
1160 : 295071421 : case EXACT_DIV_EXPR:
1161 : 295071421 : if (arg2 == 0)
1162 : : return false;
1163 : 295065476 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1164 : 295065476 : break;
1165 : :
1166 : 67989937 : case FLOOR_DIV_EXPR:
1167 : 67989937 : if (arg2 == 0)
1168 : : return false;
1169 : 67989937 : res = wi::div_floor (arg1, arg2, sign, overflow);
1170 : 67989937 : break;
1171 : :
1172 : 68785602 : case CEIL_DIV_EXPR:
1173 : 68785602 : if (arg2 == 0)
1174 : : return false;
1175 : 68785602 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1176 : 68785602 : 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 : 730643 : case TRUNC_MOD_EXPR:
1185 : 730643 : if (arg2 == 0)
1186 : : return false;
1187 : 729540 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1188 : 729540 : break;
1189 : :
1190 : 56511543 : case FLOOR_MOD_EXPR:
1191 : 56511543 : if (arg2 == 0)
1192 : : return false;
1193 : 56511543 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1194 : 56511543 : 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 : 27906 : case MIN_EXPR:
1209 : 27906 : res = wi::min (arg1, arg2, sign);
1210 : 27906 : break;
1211 : :
1212 : 78812587 : case MAX_EXPR:
1213 : 78812587 : res = wi::max (arg1, arg2, sign);
1214 : 78812587 : break;
1215 : :
1216 : : default:
1217 : : return false;
1218 : : }
1219 : : return true;
1220 : 1223174676 : }
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 : 1223174676 : 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 : 1223174676 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1251 : :
1252 : 1223174676 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1253 : : {
1254 : 1223174676 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1255 : 1223174676 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1256 : 1223174676 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1257 : : return NULL_TREE;
1258 : 1223136967 : res = wi_res;
1259 : 1223136967 : return true;
1260 : 1223174895 : }
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 : 1223174676 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1325 : : int overflowable)
1326 : : {
1327 : 1223174676 : poly_wide_int poly_res;
1328 : 1223174676 : tree type = TREE_TYPE (arg1);
1329 : 1223174676 : signop sign = TYPE_SIGN (type);
1330 : 1223174676 : wi::overflow_type overflow = wi::OVF_NONE;
1331 : :
1332 : 1223174676 : if (!poly_int_tree_p (arg1)
1333 : 1223174676 : || !poly_int_tree_p (arg2)
1334 : 2446349352 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1335 : 37709 : return NULL_TREE;
1336 : 1223136967 : return force_fit_type (type, poly_res, overflowable,
1337 : 1223136967 : (((sign == SIGNED || overflowable == -1)
1338 : 1223136967 : && overflow)
1339 : 1223136967 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1340 : 1223174676 : }
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 : 195877 : 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 : 465089 : 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 : 465089 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1372 : : {
1373 : 376209 : if (integer_zerop (other_op))
1374 : : {
1375 : 26867 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1376 : : return op;
1377 : 25981 : 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 : 280145 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1391 : : tree (*elt_const_binop) (enum tree_code, tree, tree))
1392 : : {
1393 : 198209 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1394 : 471431 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1395 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1396 : : {
1397 : 191286 : tree type = TREE_TYPE (arg1);
1398 : 191286 : bool step_ok_p;
1399 : 191286 : if (VECTOR_CST_STEPPED_P (arg1)
1400 : 191286 : && 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 : 188954 : 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 : 53874 : step_ok_p = distributes_over_addition_p (code, 1);
1419 : : else
1420 : : /* Similarly in reverse. */
1421 : 135080 : step_ok_p = distributes_over_addition_p (code, 2);
1422 : 191286 : tree_vector_builder elts;
1423 : 191286 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1424 : : return NULL_TREE;
1425 : 191286 : unsigned int count = elts.encoded_nelts ();
1426 : 741590 : for (unsigned int i = 0; i < count; ++i)
1427 : : {
1428 : 550635 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1429 : 550635 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1430 : :
1431 : 550635 : 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 : 550635 : if (elt == NULL_TREE)
1436 : 331 : return NULL_TREE;
1437 : 550304 : elts.quick_push (elt);
1438 : : }
1439 : :
1440 : 190955 : return elts.build ();
1441 : 191286 : }
1442 : :
1443 : 88859 : 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 : 173267320 : const_binop (enum tree_code code, tree arg1, tree arg2)
1477 : : {
1478 : : /* Sanity check for the recursive cases. */
1479 : 173267320 : if (!arg1 || !arg2)
1480 : : return NULL_TREE;
1481 : :
1482 : 173266056 : STRIP_NOPS (arg1);
1483 : 173266056 : STRIP_NOPS (arg2);
1484 : :
1485 : 173266056 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1486 : : {
1487 : 167478384 : if (code == POINTER_PLUS_EXPR)
1488 : 103587 : return int_const_binop (PLUS_EXPR,
1489 : 207174 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1490 : :
1491 : 167374797 : return int_const_binop (code, arg1, arg2);
1492 : : }
1493 : :
1494 : 5787672 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1495 : : {
1496 : 5491643 : machine_mode mode;
1497 : 5491643 : REAL_VALUE_TYPE d1;
1498 : 5491643 : REAL_VALUE_TYPE d2;
1499 : 5491643 : REAL_VALUE_TYPE value;
1500 : 5491643 : REAL_VALUE_TYPE result;
1501 : 5491643 : bool inexact;
1502 : 5491643 : tree t, type;
1503 : :
1504 : : /* The following codes are handled by real_arithmetic. */
1505 : 5491643 : switch (code)
1506 : : {
1507 : 5491643 : case PLUS_EXPR:
1508 : 5491643 : case MINUS_EXPR:
1509 : 5491643 : case MULT_EXPR:
1510 : 5491643 : case RDIV_EXPR:
1511 : 5491643 : case MIN_EXPR:
1512 : 5491643 : case MAX_EXPR:
1513 : 5491643 : break;
1514 : :
1515 : : default:
1516 : : return NULL_TREE;
1517 : : }
1518 : :
1519 : 5491643 : d1 = TREE_REAL_CST (arg1);
1520 : 5491643 : d2 = TREE_REAL_CST (arg2);
1521 : :
1522 : 5491643 : type = TREE_TYPE (arg1);
1523 : 5491643 : mode = TYPE_MODE (type);
1524 : :
1525 : : /* Don't perform operation if we honor signaling NaNs and
1526 : : either operand is a signaling NaN. */
1527 : 5491643 : if (HONOR_SNANS (mode)
1528 : 5491643 : && (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 : 5491610 : if (code == RDIV_EXPR
1535 : 2461045 : && real_equal (&d2, &dconst0)
1536 : 5502345 : && (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 : 5484163 : 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 : 5483924 : 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 : 5483863 : inexact = real_arithmetic (&value, code, &d1, &d2);
1559 : 5483863 : 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 : 5483863 : if (flag_trapping_math
1566 : 21302234 : && MODE_HAS_NANS (mode)
1567 : 5467104 : && REAL_VALUE_ISNAN (result)
1568 : 2504 : && !REAL_VALUE_ISNAN (d1)
1569 : 5486367 : && !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 : 5481359 : if (flag_trapping_math
1575 : 21292560 : && MODE_HAS_INFINITIES (mode)
1576 : 5464600 : && REAL_VALUE_ISINF (result)
1577 : 7497 : && !REAL_VALUE_ISINF (d1)
1578 : 5488273 : && !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 : 5476728 : if ((flag_rounding_math
1586 : 37191283 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1587 : 5476728 : && (inexact || !real_identical (&result, &value)))
1588 : 1107 : return NULL_TREE;
1589 : :
1590 : 5475621 : t = build_real (type, result);
1591 : :
1592 : 5475621 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1593 : 5475621 : return t;
1594 : : }
1595 : :
1596 : 296029 : 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 : 296029 : 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 : 284944 : tree simplified;
1774 : 284944 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1775 : : return simplified;
1776 : :
1777 : 284581 : if (commutative_tree_code (code)
1778 : 284581 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1779 : : return simplified;
1780 : :
1781 : 280145 : 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 : 224032497 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1789 : : {
1790 : 224032497 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1791 : 57944825 : 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 : 166087672 : 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 : 267733 : case COMPLEX_EXPR:
1804 : 267733 : if ((TREE_CODE (arg1) == REAL_CST
1805 : 257228 : && TREE_CODE (arg2) == REAL_CST)
1806 : 10506 : || (TREE_CODE (arg1) == INTEGER_CST
1807 : 10505 : && TREE_CODE (arg2) == INTEGER_CST))
1808 : 267732 : return build_complex (type, arg1, arg2);
1809 : : return NULL_TREE;
1810 : :
1811 : 15324 : case POINTER_DIFF_EXPR:
1812 : 15324 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1813 : : {
1814 : 29464 : poly_offset_int res = (wi::to_poly_offset (arg1)
1815 : 14732 : - wi::to_poly_offset (arg2));
1816 : 14732 : return force_fit_type (type, res, 1,
1817 : 14732 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1818 : : }
1819 : : return NULL_TREE;
1820 : :
1821 : 14428 : case VEC_PACK_TRUNC_EXPR:
1822 : 14428 : case VEC_PACK_FIX_TRUNC_EXPR:
1823 : 14428 : case VEC_PACK_FLOAT_EXPR:
1824 : 14428 : {
1825 : 14428 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1826 : :
1827 : 14428 : if (TREE_CODE (arg1) != VECTOR_CST
1828 : 14428 : || TREE_CODE (arg2) != VECTOR_CST)
1829 : : return NULL_TREE;
1830 : :
1831 : 14428 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1832 : : return NULL_TREE;
1833 : :
1834 : 14428 : out_nelts = in_nelts * 2;
1835 : 14428 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1836 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1837 : :
1838 : 14428 : tree_vector_builder elts (type, out_nelts, 1);
1839 : 186792 : for (i = 0; i < out_nelts; i++)
1840 : : {
1841 : 172376 : tree elt = (i < in_nelts
1842 : 172376 : ? VECTOR_CST_ELT (arg1, i)
1843 : 86182 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1844 : 173420 : 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 : 172376 : TREE_TYPE (type), elt);
1849 : 172376 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1850 : 12 : return NULL_TREE;
1851 : 172364 : elts.quick_push (elt);
1852 : : }
1853 : :
1854 : 14416 : return elts.build ();
1855 : 14428 : }
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 : 165789971 : default:;
1903 : : }
1904 : :
1905 : 165789971 : if (TREE_CODE_CLASS (code) != tcc_binary)
1906 : : return NULL_TREE;
1907 : :
1908 : : /* Make sure type and arg0 have the same saturating flag. */
1909 : 163134454 : gcc_checking_assert (TYPE_SATURATING (type)
1910 : : == TYPE_SATURATING (TREE_TYPE (arg1)));
1911 : :
1912 : 163134454 : 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 : 279740685 : 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 : 279740685 : if (TREE_CODE (arg0) == REAL_CST
1924 : 10928536 : && HONOR_SNANS (arg0)
1925 : 17129 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1926 : 4740 : && code != NEGATE_EXPR
1927 : 4740 : && code != ABS_EXPR
1928 : 279745390 : && code != ABSU_EXPR)
1929 : : return NULL_TREE;
1930 : :
1931 : 279735980 : switch (code)
1932 : : {
1933 : 195473380 : CASE_CONVERT:
1934 : 195473380 : case FLOAT_EXPR:
1935 : 195473380 : case FIX_TRUNC_EXPR:
1936 : 195473380 : case FIXED_CONVERT_EXPR:
1937 : 195473380 : 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 : 11375597 : case VIEW_CONVERT_EXPR:
1949 : 11375597 : return fold_view_convert_expr (type, arg0);
1950 : :
1951 : 30297680 : case NEGATE_EXPR:
1952 : 30297680 : {
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 : 30297680 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1957 : 30297680 : if (tem && CONSTANT_CLASS_P (tem))
1958 : : return tem;
1959 : : break;
1960 : : }
1961 : :
1962 : 34100 : case ABS_EXPR:
1963 : 34100 : case ABSU_EXPR:
1964 : 34100 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1965 : 33811 : 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 : 2236993 : case BIT_NOT_EXPR:
1978 : 2236993 : if (TREE_CODE (arg0) == INTEGER_CST)
1979 : 2235859 : return fold_not_const (arg0, type);
1980 : 1134 : 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 : 1134 : else if (TREE_CODE (arg0) == VECTOR_CST)
1984 : : {
1985 : 527 : tree elem;
1986 : :
1987 : : /* This can cope with stepped encodings because ~x == -1 - x. */
1988 : 527 : tree_vector_builder elements;
1989 : 527 : elements.new_unary_operation (type, arg0, true);
1990 : 527 : unsigned int i, count = elements.encoded_nelts ();
1991 : 2206 : for (i = 0; i < count; ++i)
1992 : : {
1993 : 1679 : elem = VECTOR_CST_ELT (arg0, i);
1994 : 1679 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1995 : 1679 : if (elem == NULL_TREE)
1996 : : break;
1997 : 1679 : elements.quick_push (elem);
1998 : : }
1999 : 527 : if (i == count)
2000 : 527 : return elements.build ();
2001 : 527 : }
2002 : : break;
2003 : :
2004 : 7525464 : case TRUTH_NOT_EXPR:
2005 : 7525464 : if (TREE_CODE (arg0) == INTEGER_CST)
2006 : 7199041 : return constant_boolean_node (integer_zerop (arg0), type);
2007 : : break;
2008 : :
2009 : 179270 : case REALPART_EXPR:
2010 : 179270 : if (TREE_CODE (arg0) == COMPLEX_CST)
2011 : 179069 : return fold_convert (type, TREE_REALPART (arg0));
2012 : : break;
2013 : :
2014 : 183132 : case IMAGPART_EXPR:
2015 : 183132 : if (TREE_CODE (arg0) == COMPLEX_CST)
2016 : 182944 : 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 : 3129848180 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2081 : : {
2082 : 3129848180 : 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 : 2154877290 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2092 : : {
2093 : 2154877290 : tree type = TREE_TYPE (arg0);
2094 : :
2095 : 2154877290 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2096 : : return error_mark_node;
2097 : :
2098 : 2154877290 : 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 : 2154877290 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2103 : : {
2104 : : /* And some specific cases even faster than that. */
2105 : 2123072475 : if (code == PLUS_EXPR)
2106 : : {
2107 : 971958127 : if (integer_zerop (arg0)
2108 : 971958127 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2109 : : return arg1;
2110 : 270544263 : if (integer_zerop (arg1)
2111 : 270544263 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2112 : : return arg0;
2113 : : }
2114 : 1151114348 : else if (code == MINUS_EXPR)
2115 : : {
2116 : 107532613 : if (integer_zerop (arg1)
2117 : 107532613 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2118 : : return arg0;
2119 : : }
2120 : 1043581735 : else if (code == MULT_EXPR)
2121 : : {
2122 : 483806927 : if (integer_onep (arg0)
2123 : 483806927 : && !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 : 1039649859 : tree res = int_const_binop (code, arg0, arg1, -1);
2131 : 1039649859 : if (res != NULL_TREE)
2132 : : return res;
2133 : : }
2134 : :
2135 : 31804815 : 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 : 34284780 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2144 : : {
2145 : 34284780 : tree type = TREE_TYPE (arg0);
2146 : 34284780 : tree ctype;
2147 : :
2148 : 34284780 : 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 : 34284780 : if (!TYPE_UNSIGNED (type))
2153 : 9758771 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2154 : :
2155 : 24526009 : if (type == sizetype)
2156 : 24526009 : 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 : 24526009 : 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 : 24507991 : if (tree_int_cst_equal (arg0, arg1))
2175 : 21238387 : return build_int_cst (ctype, 0);
2176 : 3269604 : else if (tree_int_cst_lt (arg1, arg0))
2177 : 2112069 : return fold_convert_loc (loc, ctype,
2178 : 2112069 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2179 : : else
2180 : 1157535 : 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 : : /* A subroutine of fold_convert_const handling conversions of an
2188 : : INTEGER_CST to another integer type. */
2189 : :
2190 : : static tree
2191 : 1132159559 : fold_convert_const_int_from_int (tree type, const_tree arg1)
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 : 1132159559 : tree arg1_type = TREE_TYPE (arg1);
2197 : 1132159559 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2198 : 1132159559 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2199 : 1132159559 : TYPE_SIGN (arg1_type)),
2200 : 1132159559 : !POINTER_TYPE_P (TREE_TYPE (arg1)),
2201 : 1132159559 : 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 : 36488 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2209 : : {
2210 : 36488 : bool overflow = false;
2211 : 36488 : 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 : 36488 : wide_int val;
2223 : 36488 : REAL_VALUE_TYPE r;
2224 : 36488 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2225 : :
2226 : 36488 : switch (code)
2227 : : {
2228 : 36488 : case FIX_TRUNC_EXPR:
2229 : 36488 : real_trunc (&r, VOIDmode, &x);
2230 : 36488 : 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 : 36488 : 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 : 36488 : if (! overflow)
2247 : : {
2248 : 32850 : tree lt = TYPE_MIN_VALUE (type);
2249 : 32850 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2250 : 32850 : if (real_less (&r, &l))
2251 : : {
2252 : 1974 : overflow = true;
2253 : 1974 : val = wi::to_wide (lt);
2254 : : }
2255 : : }
2256 : :
2257 : 36488 : if (! overflow)
2258 : : {
2259 : 30876 : tree ut = TYPE_MAX_VALUE (type);
2260 : 30876 : if (ut)
2261 : : {
2262 : 30876 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2263 : 30876 : if (real_less (&u, &r))
2264 : : {
2265 : 1921 : overflow = true;
2266 : 1921 : val = wi::to_wide (ut);
2267 : : }
2268 : : }
2269 : : }
2270 : :
2271 : 36488 : if (! overflow)
2272 : 28957 : 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 : 36488 : if (!flag_trapping_math || !overflow)
2282 : 29211 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2283 : : else
2284 : : t = NULL_TREE;
2285 : :
2286 : 36488 : return t;
2287 : 36488 : }
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 : 2016823 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2342 : : {
2343 : 2016823 : REAL_VALUE_TYPE value;
2344 : 2016823 : 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 : 2016823 : 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 : 1918589 : if (HONOR_SNANS (arg1)
2358 : 1920471 : && 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 : 1918589 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2364 : 1919245 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2365 : 509 : return NULL_TREE;
2366 : :
2367 : 1918080 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2368 : 1918080 : 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 : 1918080 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2375 : 2036726 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2376 : 0 : TREE_OVERFLOW (t) = 1;
2377 : 1918080 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2378 : 2032457 : && !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 : 9587006 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2383 : 0 : && REAL_VALUE_ISINF (value)
2384 : 1918080 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2385 : 0 : TREE_OVERFLOW (t) = 1;
2386 : : else
2387 : 1918080 : 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 : 1191992771 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2484 : : {
2485 : 1191992771 : tree arg_type = TREE_TYPE (arg1);
2486 : 1191992771 : 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 : 1181103624 : 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 : 1181103624 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2500 : : || TREE_CODE (type) == OFFSET_TYPE)
2501 : : {
2502 : 1149339151 : if (TREE_CODE (arg1) == INTEGER_CST)
2503 : 1132159559 : return fold_convert_const_int_from_int (type, arg1);
2504 : 17179592 : else if (TREE_CODE (arg1) == REAL_CST)
2505 : 36488 : return fold_convert_const_int_from_real (code, type, arg1);
2506 : 17143104 : 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 : 31707890 : if (TREE_CODE (arg1) == INTEGER_CST)
2512 : : {
2513 : 24348539 : 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 : 24348539 : 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 : 24346813 : return res;
2525 : : }
2526 : 7359351 : else if (TREE_CODE (arg1) == REAL_CST)
2527 : 2016823 : return fold_convert_const_real_from_real (type, arg1);
2528 : 5342528 : 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 : 7285 : if (TREE_CODE (arg1) == VECTOR_CST
2543 : 7285 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2544 : : {
2545 : 7285 : tree elttype = TREE_TYPE (type);
2546 : 7285 : 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 : 7285 : bool step_ok_p
2550 : 7285 : = (INTEGRAL_TYPE_P (elttype)
2551 : 315 : && INTEGRAL_TYPE_P (arg1_elttype)
2552 : 7542 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2553 : 7285 : tree_vector_builder v;
2554 : 7285 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2555 : : return NULL_TREE;
2556 : 7285 : unsigned int len = v.encoded_nelts ();
2557 : 41169 : for (unsigned int i = 0; i < len; ++i)
2558 : : {
2559 : 33884 : tree elt = VECTOR_CST_ELT (arg1, i);
2560 : 33884 : tree cvt = fold_convert_const (code, elttype, elt);
2561 : 33884 : if (cvt == NULL_TREE)
2562 : 0 : return NULL_TREE;
2563 : 33884 : v.quick_push (cvt);
2564 : : }
2565 : 7285 : return v.build ();
2566 : 7285 : }
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 : 1876154500 : fold_convert_loc (location_t loc, tree type, tree arg)
2631 : : {
2632 : 1876154500 : tree orig = TREE_TYPE (arg);
2633 : 1876154500 : tree tem;
2634 : :
2635 : 1876154500 : if (type == orig)
2636 : : return arg;
2637 : :
2638 : 1207626350 : if (TREE_CODE (arg) == ERROR_MARK
2639 : 1207625330 : || TREE_CODE (type) == ERROR_MARK
2640 : 1207625329 : || TREE_CODE (orig) == ERROR_MARK)
2641 : 1021 : return error_mark_node;
2642 : :
2643 : 1207625329 : switch (TREE_CODE (type))
2644 : : {
2645 : 58450841 : case POINTER_TYPE:
2646 : 58450841 : case REFERENCE_TYPE:
2647 : : /* Handle conversions between pointers to different address spaces. */
2648 : 58450841 : if (POINTER_TYPE_P (orig)
2649 : 58450841 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2650 : 47834050 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2651 : 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2652 : : /* fall through */
2653 : :
2654 : 1176313548 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2655 : 1176313548 : case OFFSET_TYPE: case BITINT_TYPE:
2656 : 1176313548 : if (TREE_CODE (arg) == INTEGER_CST)
2657 : : {
2658 : 996036356 : tem = fold_convert_const (NOP_EXPR, type, arg);
2659 : 996036356 : if (tem != NULL_TREE)
2660 : : return tem;
2661 : : }
2662 : 180277192 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2663 : 3110 : || TREE_CODE (orig) == OFFSET_TYPE)
2664 : 180277192 : 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 : 551145 : case REAL_TYPE:
2674 : 551145 : 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 : 493841 : else if (TREE_CODE (arg) == REAL_CST)
2681 : : {
2682 : 119558 : tem = fold_convert_const (NOP_EXPR, type, arg);
2683 : 119558 : if (tem != NULL_TREE)
2684 : : return tem;
2685 : : }
2686 : 374283 : 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 : 374285 : 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 : 373635 : case REAL_TYPE:
2701 : 373635 : 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 : 30644253 : case VECTOR_TYPE:
2779 : 30644253 : if (integer_zerop (arg))
2780 : 17059 : return build_zero_vector (type);
2781 : 30627194 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2782 : 30627194 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2783 : : || VECTOR_TYPE_P (orig));
2784 : 30627194 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2785 : :
2786 : 110729 : case VOID_TYPE:
2787 : 110729 : tem = fold_ignored_result (arg);
2788 : 110729 : 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 : 49768332 : maybe_lvalue_p (const_tree x)
2805 : : {
2806 : : /* We only need to wrap lvalue tree codes. */
2807 : 49768332 : 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 : 37128152 : default:
2841 : : /* Assume the worst for front-end tree codes. */
2842 : 37128152 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2843 : : break;
2844 : : return false;
2845 : : }
2846 : :
2847 : 12699394 : return true;
2848 : : }
2849 : :
2850 : : /* Return an expr equal to X but certainly not valid as an lvalue. */
2851 : :
2852 : : tree
2853 : 45269385 : 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 : 45269385 : if (in_gimple_form)
2858 : : return x;
2859 : :
2860 : 9630715 : if (! maybe_lvalue_p (x))
2861 : : return x;
2862 : 2062693 : 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 : 117077114 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2872 : : {
2873 : 117077114 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2874 : 1095160 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2875 : : return ERROR_MARK;
2876 : :
2877 : 116251350 : switch (code)
2878 : : {
2879 : : case EQ_EXPR:
2880 : : return NE_EXPR;
2881 : 51612710 : case NE_EXPR:
2882 : 51612710 : return EQ_EXPR;
2883 : 11652874 : case GT_EXPR:
2884 : 11652874 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2885 : 13087216 : case GE_EXPR:
2886 : 13087216 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2887 : 7211332 : case LT_EXPR:
2888 : 7211332 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2889 : 7525362 : case LE_EXPR:
2890 : 7525362 : 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 : 241583 : case ORDERED_EXPR:
2904 : 241583 : 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 : 149479920 : swap_tree_comparison (enum tree_code code)
2917 : : {
2918 : 149479920 : 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 : 35197995 : case GT_EXPR:
2928 : 35197995 : return LT_EXPR;
2929 : 10494306 : case GE_EXPR:
2930 : 10494306 : return LE_EXPR;
2931 : 20816613 : case LT_EXPR:
2932 : 20816613 : return GT_EXPR;
2933 : 14846911 : case LE_EXPR:
2934 : 14846911 : return GE_EXPR;
2935 : 276401 : case UNGT_EXPR:
2936 : 276401 : return UNLT_EXPR;
2937 : 19460 : case UNGE_EXPR:
2938 : 19460 : return UNLE_EXPR;
2939 : 415478 : case UNLT_EXPR:
2940 : 415478 : return UNGT_EXPR;
2941 : 118880 : case UNLE_EXPR:
2942 : 118880 : 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 : 54748 : comparison_to_compcode (enum tree_code code)
2955 : : {
2956 : 54748 : 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 : 13439 : compcode_to_comparison (enum comparison_code code)
2997 : : {
2998 : 13439 : 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 : 1399481 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3037 : : {
3038 : 1399481 : return (COMPARISON_CLASS_P (cond1)
3039 : 1310253 : && COMPARISON_CLASS_P (cond2)
3040 : 1305868 : && (invert_tree_comparison
3041 : 1305868 : (TREE_CODE (cond1),
3042 : 2611736 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3043 : 72689 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3044 : 72689 : TREE_OPERAND (cond2, 0), 0)
3045 : 1422460 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3046 : 22979 : 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 : 27374 : 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 : 27374 : bool honor_nans = HONOR_NANS (ll_arg);
3062 : 27374 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3063 : 27374 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3064 : 27374 : int compcode;
3065 : :
3066 : 27374 : switch (code)
3067 : : {
3068 : 18031 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3069 : 18031 : compcode = lcompcode & rcompcode;
3070 : 18031 : break;
3071 : :
3072 : 9343 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3073 : 9343 : compcode = lcompcode | rcompcode;
3074 : 9343 : break;
3075 : :
3076 : : default:
3077 : : return NULL_TREE;
3078 : : }
3079 : :
3080 : 27374 : 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 : 22299 : compcode &= ~COMPCODE_UNORD;
3085 : 22299 : if (compcode == COMPCODE_LTGT)
3086 : : compcode = COMPCODE_NE;
3087 : 21253 : 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 : 22703 : else if (compcode == COMPCODE_FALSE)
3128 : 9264 : return constant_boolean_node (false, truth_type);
3129 : : else
3130 : : {
3131 : 13439 : enum tree_code tcode;
3132 : :
3133 : 13439 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3134 : 13439 : 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 : 6999219039 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3196 : : unsigned int flags)
3197 : : {
3198 : 6999219039 : 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 : 6999934776 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3208 : : tree type1, const_tree arg1,
3209 : : unsigned int flags)
3210 : : {
3211 : 6999934776 : bool r;
3212 : 6999934776 : if (verify_hash_value (arg0, arg1, flags, &r))
3213 : 2941439998 : return r;
3214 : :
3215 : 4058494778 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3216 : 4058494778 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3217 : :
3218 : : /* If either is ERROR_MARK, they aren't equal. */
3219 : 4058494778 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3220 : 4058493995 : || type0 == error_mark_node
3221 : 4058493993 : || 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 : 4058493992 : if (!type0 || !type1)
3227 : : return false;
3228 : :
3229 : : /* Bitwise identity makes no sense if the values have different layouts. */
3230 : 4058491322 : if ((flags & OEP_BITWISE)
3231 : 4058491322 : && !tree_nop_conversion_p (type0, type1))
3232 : : return false;
3233 : :
3234 : : /* We cannot consider pointers to different address space equal. */
3235 : 4058491322 : if (POINTER_TYPE_P (type0)
3236 : 597486574 : && POINTER_TYPE_P (type1)
3237 : 4564963565 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3238 : 506472243 : != 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 : 4058491131 : 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 : 643299800 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3248 : 643299800 : return tree_int_cst_equal (arg0, arg1);
3249 : : }
3250 : :
3251 : 3415191331 : if ((flags & OEP_ASSUME_WRAPV)
3252 : 2035272 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3253 : : {
3254 : 769653 : const_tree t_arg0 = arg0;
3255 : 769653 : const_tree t_arg1 = arg1;
3256 : 769653 : STRIP_NOPS (arg0);
3257 : 769653 : STRIP_NOPS (arg1);
3258 : : /* Only recurse if the conversion was one that was valid to strip. */
3259 : 769653 : if (t_arg0 != arg0 || t_arg1 != arg1)
3260 : 715737 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3261 : : }
3262 : :
3263 : 3414475594 : 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 : 3034930669 : bool enforce_signedness = true;
3269 : 3034930669 : if (flags & OEP_ASSUME_WRAPV)
3270 : : {
3271 : 1233690 : 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 : 3034930669 : 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 : 3034930669 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3301 : 3034930669 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3302 : 139928439 : && 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 : 2744130442 : if (element_precision (type0) != element_precision (type1))
3308 : : return false;
3309 : :
3310 : 2598038234 : STRIP_NOPS (arg0);
3311 : 2598038234 : STRIP_NOPS (arg1);
3312 : :
3313 : 2598038234 : type0 = TREE_TYPE (arg0);
3314 : 2598038234 : 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 : 2977583159 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3331 : 1207458334 : && COMPARISON_CLASS_P (arg0)
3332 : 6220814 : && COMPARISON_CLASS_P (arg1))
3333 : : {
3334 : 4577608 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3335 : :
3336 : 4577608 : if (TREE_CODE (arg0) == swap_code)
3337 : 2023237 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3338 : 2023237 : TREE_OPERAND (arg1, 1), flags)
3339 : 2043136 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3340 : 19899 : TREE_OPERAND (arg1, 0), flags);
3341 : : }
3342 : :
3343 : 2975559922 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3344 : : {
3345 : : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3346 : 1205435097 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3347 : : ;
3348 : 1205415116 : 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 : 48128022 : if (TREE_CODE (arg0) == MEM_REF
3354 : 7341502 : && DECL_P (arg1)
3355 : 5059135 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3356 : 1117280 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3357 : 48661284 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3358 : : return true;
3359 : 47886220 : else if (TREE_CODE (arg1) == MEM_REF
3360 : 30447962 : && DECL_P (arg0)
3361 : 11061149 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3362 : 2402623 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3363 : 48464047 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3364 : : return true;
3365 : 47484279 : 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 : 1770144806 : if (TREE_CODE (type0) == ERROR_MARK
3374 : 1770144806 : || TREE_CODE (type1) == ERROR_MARK
3375 : 3540289612 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3376 : 24732713 : && !(flags & OEP_ADDRESS_OF)))
3377 : 3697762 : 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 : 313814719 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3389 : 2080261585 : && (TREE_CODE (arg0) == SAVE_EXPR
3390 : 313787515 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3391 : 279696259 : || (! 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 : 1452785843 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3397 : 19402973 : 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 : 3690716 : case REAL_CST:
3407 : 3690716 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3408 : : return true;
3409 : :
3410 : 2651813 : 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 : 13945 : if (real_zerop (arg0) && real_zerop (arg1))
3415 : : return true;
3416 : : }
3417 : 2651804 : return false;
3418 : :
3419 : 785320 : case VECTOR_CST:
3420 : 785320 : {
3421 : 785320 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3422 : 785320 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3423 : : return false;
3424 : :
3425 : 764844 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3426 : 764844 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3427 : : return false;
3428 : :
3429 : 733012 : unsigned int count = vector_cst_encoded_nelts (arg0);
3430 : 1074857 : for (unsigned int i = 0; i < count; ++i)
3431 : 1690174 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3432 : 845087 : 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 : 1204243 : case STRING_CST:
3444 : 1204243 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3445 : 1204243 : && ! memcmp (TREE_STRING_POINTER (arg0),
3446 : 779046 : TREE_STRING_POINTER (arg1),
3447 : 779046 : 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 : 12771710 : case ADDR_EXPR:
3456 : 12771710 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3457 : 12771710 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3458 : : flags | OEP_ADDRESS_OF
3459 : 12771710 : | OEP_MATCH_SIDE_EFFECTS);
3460 : 178123 : case CONSTRUCTOR:
3461 : 178123 : {
3462 : : /* In GIMPLE empty constructors are allowed in initializers of
3463 : : aggregates. */
3464 : 178123 : 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 : 348626976 : 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 : 1434142285 : 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 : 1434142285 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3538 : : {
3539 : 7122908 : case tcc_unary:
3540 : : /* Two conversions are equal only if signedness and modes match. */
3541 : 7122908 : switch (TREE_CODE (arg0))
3542 : : {
3543 : 6749107 : CASE_CONVERT:
3544 : 6749107 : case FIX_TRUNC_EXPR:
3545 : 6749107 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3546 : : return false;
3547 : : break;
3548 : : default:
3549 : : break;
3550 : : }
3551 : :
3552 : 7122887 : return OP_SAME_WITH_NULL (0);
3553 : :
3554 : :
3555 : 21985400 : case tcc_comparison:
3556 : 21985400 : case tcc_binary:
3557 : 21985400 : if (OP_SAME (0) && OP_SAME (1))
3558 : : return true;
3559 : :
3560 : : /* For commutative ops, allow the other order. */
3561 : 16180935 : return (commutative_tree_code (TREE_CODE (arg0))
3562 : 12363252 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3563 : 12363252 : TREE_OPERAND (arg1, 1), flags)
3564 : 16397827 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3565 : 216892 : TREE_OPERAND (arg1, 0), flags));
3566 : :
3567 : 866347742 : 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 : 866347742 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3572 : 866347742 : && (TREE_SIDE_EFFECTS (arg0)
3573 : 804989762 : || TREE_SIDE_EFFECTS (arg1)))
3574 : : return false;
3575 : :
3576 : 866111205 : switch (TREE_CODE (arg0))
3577 : : {
3578 : 3892929 : case INDIRECT_REF:
3579 : 3892929 : if (!(flags & OEP_ADDRESS_OF))
3580 : : {
3581 : 3869937 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3582 : : return false;
3583 : : /* Verify that the access types are compatible. */
3584 : 3868209 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3585 : : return false;
3586 : : }
3587 : 3827938 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3588 : 3827938 : return OP_SAME (0);
3589 : :
3590 : 655184 : case IMAGPART_EXPR:
3591 : : /* Require the same offset. */
3592 : 655184 : if (!operand_equal_p (TYPE_SIZE (type0),
3593 : 655184 : TYPE_SIZE (type1),
3594 : : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3595 : : return false;
3596 : :
3597 : : /* Fallthru. */
3598 : 2445234 : case REALPART_EXPR:
3599 : 2445234 : case VIEW_CONVERT_EXPR:
3600 : 2445234 : return OP_SAME (0);
3601 : :
3602 : 75964258 : case TARGET_MEM_REF:
3603 : 75964258 : case MEM_REF:
3604 : 75964258 : if (!(flags & OEP_ADDRESS_OF))
3605 : : {
3606 : : /* Require equal access sizes */
3607 : 16652519 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3608 : 16652519 : && (!TYPE_SIZE (type0)
3609 : 1087243 : || !TYPE_SIZE (type1)
3610 : 1082358 : || !operand_equal_p (TYPE_SIZE (type0),
3611 : 1082358 : TYPE_SIZE (type1),
3612 : : flags)))
3613 : 1082863 : return false;
3614 : : /* Verify that access happens in similar types. */
3615 : 15569656 : if (!types_compatible_p (type0, type1))
3616 : : return false;
3617 : : /* Verify that accesses are TBAA compatible. */
3618 : 15213712 : if (!alias_ptr_types_compatible_p
3619 : 15213712 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3620 : 15213712 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3621 : 14281517 : || (MR_DEPENDENCE_CLIQUE (arg0)
3622 : 14281517 : != MR_DEPENDENCE_CLIQUE (arg1))
3623 : 27317301 : || (MR_DEPENDENCE_BASE (arg0)
3624 : 12103589 : != MR_DEPENDENCE_BASE (arg1)))
3625 : : return false;
3626 : : /* Verify that alignment is compatible. */
3627 : 11637809 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3628 : : return false;
3629 : : }
3630 : 70792335 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3631 : 120120065 : return (OP_SAME (0) && OP_SAME (1)
3632 : : /* TARGET_MEM_REF require equal extra operands. */
3633 : 94826943 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3634 : 577458 : || (OP_SAME_WITH_NULL (2)
3635 : 270783 : && OP_SAME_WITH_NULL (3)
3636 : 264946 : && OP_SAME_WITH_NULL (4))));
3637 : :
3638 : 38234675 : case ARRAY_REF:
3639 : 38234675 : case ARRAY_RANGE_REF:
3640 : 38234675 : if (!OP_SAME (0))
3641 : : return false;
3642 : 33379747 : 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 : 33379747 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3646 : 33379747 : TREE_OPERAND (arg1, 1))
3647 : 30432003 : || OP_SAME (1))
3648 : 5922462 : && OP_SAME_WITH_NULL (2)
3649 : 5920922 : && 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 : 42261900 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3653 : 2960461 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3654 : 2622 : || (operand_equal_p (array_ref_low_bound
3655 : 2622 : (CONST_CAST_TREE (arg0)),
3656 : : array_ref_low_bound
3657 : 2622 : (CONST_CAST_TREE (arg1)), flags)
3658 : 2622 : && operand_equal_p (array_ref_element_size
3659 : 2622 : (CONST_CAST_TREE (arg0)),
3660 : : array_ref_element_size
3661 : 2622 : (CONST_CAST_TREE (arg1)),
3662 : : flags))));
3663 : :
3664 : 745098987 : 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 : 745098987 : if (!OP_SAME_WITH_NULL (0))
3668 : : return false;
3669 : 56661280 : {
3670 : 56661280 : 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 : 56661280 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3676 : 56661280 : if (!OP_SAME (1))
3677 : : {
3678 : 32932801 : if (compare_address
3679 : 595522 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3680 : : {
3681 : 595519 : tree field0 = TREE_OPERAND (arg0, 1);
3682 : 595519 : tree field1 = TREE_OPERAND (arg1, 1);
3683 : :
3684 : : /* Non-FIELD_DECL operands can appear in C++ templates. */
3685 : 595519 : if (TREE_CODE (field0) != FIELD_DECL
3686 : 595519 : || TREE_CODE (field1) != FIELD_DECL)
3687 : : return false;
3688 : :
3689 : 595519 : if (!DECL_FIELD_OFFSET (field0)
3690 : 595519 : || !DECL_FIELD_OFFSET (field1))
3691 : 3 : return field0 == field1;
3692 : :
3693 : 595516 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3694 : 595516 : DECL_FIELD_OFFSET (field1), flags)
3695 : 785228 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3696 : 189712 : DECL_FIELD_BIT_OFFSET (field1),
3697 : : flags))
3698 : 555893 : return false;
3699 : : }
3700 : : else
3701 : : return false;
3702 : : }
3703 : : }
3704 : 23768102 : return OP_SAME_WITH_NULL (2);
3705 : :
3706 : 475074 : case BIT_FIELD_REF:
3707 : 475074 : if (!OP_SAME (0))
3708 : : return false;
3709 : 368507 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3710 : 368507 : return OP_SAME (1) && OP_SAME (2);
3711 : :
3712 : : default:
3713 : : return false;
3714 : : }
3715 : :
3716 : 50924768 : case tcc_expression:
3717 : 50924768 : switch (TREE_CODE (arg0))
3718 : : {
3719 : 45876188 : case ADDR_EXPR:
3720 : : /* Be sure we pass right ADDRESS_OF flag. */
3721 : 45876188 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3722 : 45876188 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3723 : 45876188 : TREE_OPERAND (arg1, 0),
3724 : 45876188 : flags | OEP_ADDRESS_OF);
3725 : :
3726 : 678613 : case TRUTH_NOT_EXPR:
3727 : 678613 : return OP_SAME (0);
3728 : :
3729 : 41529 : case TRUTH_ANDIF_EXPR:
3730 : 41529 : case TRUTH_ORIF_EXPR:
3731 : 41529 : 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 : 19777 : case TRUTH_AND_EXPR:
3741 : 19777 : case TRUTH_OR_EXPR:
3742 : 19777 : case TRUTH_XOR_EXPR:
3743 : 19777 : if (OP_SAME (0) && OP_SAME (1))
3744 : : return true;
3745 : :
3746 : : /* Otherwise take into account this is a commutative operation. */
3747 : 19759 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3748 : 19759 : TREE_OPERAND (arg1, 1), flags)
3749 : 19762 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3750 : 3 : TREE_OPERAND (arg1, 0), flags));
3751 : :
3752 : 123319 : case COND_EXPR:
3753 : 123319 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3754 : 49264 : return false;
3755 : 74055 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3756 : 74055 : 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 : 23893 : case MODIFY_EXPR:
3773 : 23893 : case INIT_EXPR:
3774 : 23893 : case COMPOUND_EXPR:
3775 : 23893 : case PREDECREMENT_EXPR:
3776 : 23893 : case PREINCREMENT_EXPR:
3777 : 23893 : case POSTDECREMENT_EXPR:
3778 : 23893 : case POSTINCREMENT_EXPR:
3779 : 23893 : if (flags & OEP_LEXICOGRAPHIC)
3780 : 165 : return OP_SAME (0) && OP_SAME (1);
3781 : : return false;
3782 : :
3783 : 100976 : case CLEANUP_POINT_EXPR:
3784 : 100976 : case EXPR_STMT:
3785 : 100976 : case SAVE_EXPR:
3786 : 100976 : if (flags & OEP_LEXICOGRAPHIC)
3787 : 208 : return OP_SAME (0);
3788 : : return false;
3789 : :
3790 : 121494 : case OBJ_TYPE_REF:
3791 : : /* Virtual table reference. */
3792 : 242988 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3793 : 121494 : OBJ_TYPE_REF_EXPR (arg1), flags))
3794 : : return false;
3795 : 16147 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3796 : 16147 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3797 : 16147 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3798 : : return false;
3799 : 16147 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3800 : 16147 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3801 : : return false;
3802 : 16147 : if (virtual_method_call_p (arg0))
3803 : : {
3804 : 16147 : if (!virtual_method_call_p (arg1))
3805 : : return false;
3806 : 16147 : return types_same_for_odr (obj_type_ref_class (arg0),
3807 : 32294 : obj_type_ref_class (arg1));
3808 : : }
3809 : : return false;
3810 : :
3811 : : default:
3812 : : return false;
3813 : : }
3814 : :
3815 : 2907077 : case tcc_vl_exp:
3816 : 2907077 : switch (TREE_CODE (arg0))
3817 : : {
3818 : 2907077 : case CALL_EXPR:
3819 : 2907077 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3820 : 2907077 : != (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 : 2907077 : 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 : 2907075 : 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 : 1880981 : {
3842 : 1880981 : unsigned int cef = call_expr_flags (arg0);
3843 : 1880981 : if (flags & OEP_PURE_SAME)
3844 : 0 : cef &= ECF_CONST | ECF_PURE;
3845 : : else
3846 : 1880981 : cef &= ECF_CONST;
3847 : 1880981 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3848 : : return false;
3849 : : }
3850 : :
3851 : : /* Now see if all the arguments are the same. */
3852 : 32863 : {
3853 : 32863 : const_call_expr_arg_iterator iter0, iter1;
3854 : 32863 : const_tree a0, a1;
3855 : 65726 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3856 : 32863 : a1 = first_const_call_expr_arg (arg1, &iter1);
3857 : 40968 : a0 && a1;
3858 : 8105 : a0 = next_const_call_expr_arg (&iter0),
3859 : 8105 : a1 = next_const_call_expr_arg (&iter1))
3860 : 34355 : 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 : 6613 : return ! (a0 || a1);
3866 : : }
3867 : : default:
3868 : : return false;
3869 : : }
3870 : :
3871 : 161965979 : case tcc_declaration:
3872 : : /* Consider __builtin_sqrt equal to sqrt. */
3873 : 161965979 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3874 : 6368444 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3875 : 344093 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3876 : 5695709 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3877 : 344093 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3878 : :
3879 : 156270270 : if (DECL_P (arg0)
3880 : 156270270 : && (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 : 320650251 : case tcc_exceptional:
3895 : 320650251 : 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 : 320630984 : 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 : 2237975 : case tcc_statement:
3966 : 2237975 : 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 : 2771599059 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3993 : : unsigned int flags)
3994 : : {
3995 : 2771599059 : int i;
3996 : 2771599059 : enum tree_code code;
3997 : 2771599059 : enum tree_code_class tclass;
3998 : :
3999 : 2771599059 : if (t == NULL_TREE || t == error_mark_node)
4000 : : {
4001 : 74901421 : hstate.merge_hash (0);
4002 : 74901421 : return;
4003 : : }
4004 : :
4005 : 2696697638 : STRIP_ANY_LOCATION_WRAPPER (t);
4006 : :
4007 : 2696697638 : if (!(flags & OEP_ADDRESS_OF))
4008 : 2446985239 : STRIP_NOPS (t);
4009 : :
4010 : 2696697638 : code = TREE_CODE (t);
4011 : :
4012 : 2696697638 : 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 : 796025457 : case INTEGER_CST:
4020 : 796025457 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4021 : 1617543636 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4022 : 821518179 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4023 : : return;
4024 : 15541251 : case REAL_CST:
4025 : 15541251 : {
4026 : 15541251 : unsigned int val2;
4027 : 15541251 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4028 : : val2 = rvc_zero;
4029 : : else
4030 : 15316705 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4031 : 15541251 : hstate.merge_hash (val2);
4032 : 15541251 : 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 : 10405936 : case STRING_CST:
4041 : 10405936 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4042 : 10405936 : TREE_STRING_LENGTH (t));
4043 : 10405936 : 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 : 208710 : case COMPLEX_CST:
4049 : 208710 : hash_operand (TREE_REALPART (t), hstate, flags);
4050 : 208710 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4051 : 208710 : return;
4052 : 3064194 : case VECTOR_CST:
4053 : 3064194 : {
4054 : 3064194 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4055 : 3064194 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4056 : 3064194 : unsigned int count = vector_cst_encoded_nelts (t);
4057 : 9626114 : for (unsigned int i = 0; i < count; ++i)
4058 : 6561920 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4059 : : return;
4060 : : }
4061 : 875432395 : case SSA_NAME:
4062 : : /* We can just compare by pointer. */
4063 : 875432395 : hstate.add_hwi (SSA_NAME_VERSION (t));
4064 : 875432395 : 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 : 279116 : for (; t; t = TREE_CHAIN (t))
4078 : 139558 : hash_operand (TREE_VALUE (t), hstate, flags);
4079 : : return;
4080 : 4890537 : case CONSTRUCTOR:
4081 : 4890537 : {
4082 : 4890537 : unsigned HOST_WIDE_INT idx;
4083 : 4890537 : tree field, value;
4084 : 4890537 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4085 : 4890537 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4086 : 19929419 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4087 : : {
4088 : : /* In GIMPLE the indexes can be either NULL or matching i. */
4089 : 15038882 : if (field == NULL_TREE)
4090 : 1091224 : field = bitsize_int (idx);
4091 : 15038882 : if (TREE_CODE (field) == FIELD_DECL)
4092 : : {
4093 : 9918541 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4094 : 9918541 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4095 : : }
4096 : : else
4097 : 5120341 : hash_operand (field, hstate, flags);
4098 : 15038882 : 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 : 20823096 : 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 : 20823096 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4124 : 20823096 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4125 : : {
4126 : 7026979 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4127 : 7026979 : code = TREE_CODE (t);
4128 : : }
4129 : : /* FALL THROUGH */
4130 : 990988264 : default:
4131 : 990988264 : 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 : 990988264 : tclass = TREE_CODE_CLASS (code);
4138 : :
4139 : 990988264 : if (tclass == tcc_declaration)
4140 : : {
4141 : : /* DECL's have a unique ID */
4142 : 663457180 : hstate.add_hwi (DECL_UID (t));
4143 : : }
4144 : 327531084 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4145 : : {
4146 : : /* For comparisons that can be swapped, use the lower
4147 : : tree code. */
4148 : 145115 : enum tree_code ccode = swap_tree_comparison (code);
4149 : 145115 : if (code < ccode)
4150 : 60890 : ccode = code;
4151 : 145115 : hstate.add_object (ccode);
4152 : 145115 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4153 : 145115 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4154 : : }
4155 : 327385969 : else if (CONVERT_EXPR_CODE_P (code))
4156 : : {
4157 : : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4158 : : operand_equal_p. */
4159 : 5156975 : enum tree_code ccode = NOP_EXPR;
4160 : 5156975 : 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 : 5156975 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4167 : 5156975 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4168 : : }
4169 : : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4170 : 322228994 : else if (code == MEM_REF
4171 : 78134765 : && (flags & OEP_ADDRESS_OF) != 0
4172 : 69173583 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4173 : 13888249 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4174 : 335863406 : && integer_zerop (TREE_OPERAND (t, 1)))
4175 : 6239577 : 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 : 315989417 : else if (!IS_EXPR_CODE_CLASS (tclass))
4180 : 252 : gcc_assert (flags & OEP_HASH_CHECK);
4181 : : else
4182 : : {
4183 : 315989165 : unsigned int sflags = flags;
4184 : :
4185 : 315989165 : hstate.add_object (code);
4186 : :
4187 : 315989165 : switch (code)
4188 : : {
4189 : 126239084 : case ADDR_EXPR:
4190 : 126239084 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4191 : 126239084 : flags |= OEP_ADDRESS_OF;
4192 : 126239084 : sflags = flags;
4193 : 126239084 : break;
4194 : :
4195 : 76300511 : case INDIRECT_REF:
4196 : 76300511 : case MEM_REF:
4197 : 76300511 : case TARGET_MEM_REF:
4198 : 76300511 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4199 : 76300511 : sflags = flags;
4200 : 76300511 : break;
4201 : :
4202 : 74648291 : case COMPONENT_REF:
4203 : 74648291 : if (sflags & OEP_ADDRESS_OF)
4204 : : {
4205 : 37580888 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4206 : 37580888 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4207 : : hstate, flags & ~OEP_ADDRESS_OF);
4208 : 37580888 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4209 : : hstate, flags & ~OEP_ADDRESS_OF);
4210 : 37580888 : return;
4211 : : }
4212 : : break;
4213 : 15220774 : case ARRAY_REF:
4214 : 15220774 : case ARRAY_RANGE_REF:
4215 : 15220774 : case BIT_FIELD_REF:
4216 : 15220774 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4217 : 15220774 : break;
4218 : :
4219 : 8475 : case COND_EXPR:
4220 : 8475 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4221 : 8475 : 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 : 54405 : case CALL_EXPR:
4236 : 54405 : 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 : 324588 : case OBJ_TYPE_REF:
4248 : : /* Virtual table reference. */
4249 : 324588 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4250 : 324588 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4251 : 324588 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4252 : 324588 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4253 : 324588 : if (!virtual_method_call_p (t))
4254 : : return;
4255 : 324573 : if (tree c = obj_type_ref_class (t))
4256 : : {
4257 : 324573 : 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 : 324573 : if (TREE_CODE (c) == TYPE_DECL
4261 : 324573 : && DECL_ASSEMBLER_NAME_SET_P (c))
4262 : 6327 : hstate.add_object
4263 : 6327 : (IDENTIFIER_HASH_VALUE
4264 : : (DECL_ASSEMBLER_NAME (c)));
4265 : : }
4266 : 324573 : 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 : 278083617 : 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 : 278083617 : 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 : 17216765 : inchash::hash one, two;
4288 : 17216765 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4289 : 17216765 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4290 : 17216765 : hstate.add_commutative (one, two);
4291 : : }
4292 : : else
4293 : 729419372 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4294 : 676238466 : hash_operand (TREE_OPERAND (t, i), hstate,
4295 : : i == 0 ? flags : sflags);
4296 : : }
4297 : : return;
4298 : : }
4299 : : }
4300 : :
4301 : : bool
4302 : 7004805807 : 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 : 7004805807 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4309 : : {
4310 : 2943565154 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4311 : : {
4312 : 446515945 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4313 : : {
4314 : 81887776 : inchash::hash hstate0 (0), hstate1 (0);
4315 : 81887776 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4316 : 81887776 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4317 : 81887776 : hashval_t h0 = hstate0.end ();
4318 : 81887776 : hashval_t h1 = hstate1.end ();
4319 : 81887776 : gcc_assert (h0 == h1);
4320 : : }
4321 : 446515945 : *ret = true;
4322 : : }
4323 : : else
4324 : 2497049209 : *ret = false;
4325 : :
4326 : 2943565154 : 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 : 2941449007 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4340 : : {
4341 : 2941449007 : 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 : 1930284866 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4354 : : {
4355 : 1930284866 : default_compare_instance.hash_operand (t, hstate, flags);
4356 : 1930284866 : }
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 : 18023935 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4365 : : {
4366 : 18023935 : if (operand_equal_p (arg0, arg1, 0))
4367 : : return true;
4368 : :
4369 : 34354356 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4370 : 29215661 : || ! 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 : 5813090 : tree op0 = arg0;
4377 : 5813090 : tree op1 = arg1;
4378 : 5813090 : STRIP_NOPS (op0);
4379 : 5813090 : STRIP_NOPS (op1);
4380 : 5813090 : 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 : 4860065 : if (CONVERT_EXPR_P (arg1)
4386 : 764235 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4387 : 764189 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4388 : 764189 : < TYPE_PRECISION (TREE_TYPE (arg1))
4389 : 5926813 : && 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 : 55829120 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4406 : : {
4407 : 59549628 : enum tree_code code = TREE_CODE (arg);
4408 : 59549628 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4409 : :
4410 : : /* We can handle some of the tcc_expression cases here. */
4411 : 59549628 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4412 : : tclass = tcc_unary;
4413 : 58949035 : else if (tclass == tcc_expression
4414 : 594063 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4415 : 594063 : || code == COMPOUND_EXPR))
4416 : : tclass = tcc_binary;
4417 : :
4418 : 58938255 : switch (tclass)
4419 : : {
4420 : 3720508 : case tcc_unary:
4421 : 3720508 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4422 : :
4423 : 5171341 : case tcc_binary:
4424 : 5171341 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4425 : 5171341 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4426 : :
4427 : : case tcc_constant:
4428 : : return true;
4429 : :
4430 : 583283 : case tcc_expression:
4431 : 583283 : 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 : 605150 : 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 : 605150 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4445 : 605150 : TREE_OPERAND (arg, 1), 0))
4446 : : return false;
4447 : :
4448 : 605150 : if (*cval1 == 0)
4449 : 603091 : *cval1 = TREE_OPERAND (arg, 0);
4450 : 2059 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4451 : : ;
4452 : 1940 : else if (*cval2 == 0)
4453 : 0 : *cval2 = TREE_OPERAND (arg, 0);
4454 : 1940 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4455 : : ;
4456 : : else
4457 : : return false;
4458 : :
4459 : 603210 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4460 : : ;
4461 : 603210 : else if (*cval2 == 0)
4462 : 603091 : *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 : 283330 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4569 : : {
4570 : 283330 : 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 : 283330 : 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 : 283330 : if (TREE_SIDE_EFFECTS (omitted))
4579 : 13568 : return build2_loc (loc, COMPOUND_EXPR, type,
4580 : 13568 : fold_ignored_result (omitted), t);
4581 : :
4582 : 269762 : 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 : 5386 : omit_two_operands_loc (location_t loc, tree type, tree result,
4596 : : tree omitted1, tree omitted2)
4597 : : {
4598 : 5386 : tree t = fold_convert_loc (loc, type, result);
4599 : :
4600 : 5386 : if (TREE_SIDE_EFFECTS (omitted2))
4601 : 72 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4602 : 5386 : if (TREE_SIDE_EFFECTS (omitted1))
4603 : 179 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4604 : :
4605 : 5386 : 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 : 47913658 : fold_truth_not_expr (location_t loc, tree arg)
4618 : : {
4619 : 47913658 : tree type = TREE_TYPE (arg);
4620 : 47913658 : enum tree_code code = TREE_CODE (arg);
4621 : 47913658 : 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 : 47913658 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4628 : : {
4629 : 37491440 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4630 : 31516038 : if (FLOAT_TYPE_P (op_type)
4631 : 5985489 : && flag_trapping_math
4632 : 5955490 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4633 : 43406418 : && code != NE_EXPR && code != EQ_EXPR)
4634 : : return NULL_TREE;
4635 : :
4636 : 32270237 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4637 : 32270237 : if (code == ERROR_MARK)
4638 : : return NULL_TREE;
4639 : :
4640 : 32270237 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4641 : 32270237 : TREE_OPERAND (arg, 1));
4642 : 32270237 : copy_warning (ret, arg);
4643 : 32270237 : return ret;
4644 : : }
4645 : :
4646 : 10422218 : switch (code)
4647 : : {
4648 : 0 : case INTEGER_CST:
4649 : 0 : return constant_boolean_node (integer_zerop (arg), type);
4650 : :
4651 : 48599 : case TRUTH_AND_EXPR:
4652 : 48599 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4653 : 48599 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4654 : 97198 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4655 : 48599 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4656 : 97198 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4657 : :
4658 : 2442 : case TRUTH_OR_EXPR:
4659 : 2442 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4660 : 2442 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4661 : 4884 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4662 : 2442 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4663 : 4884 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4664 : :
4665 : 34230 : 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 : 34230 : 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 : 34195 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4676 : 34195 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4677 : 68390 : TREE_OPERAND (arg, 1));
4678 : :
4679 : 248101 : case TRUTH_ANDIF_EXPR:
4680 : 248101 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4681 : 248101 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4682 : 496202 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4683 : 248101 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4684 : 496202 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4685 : :
4686 : 18969 : case TRUTH_ORIF_EXPR:
4687 : 18969 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4688 : 18969 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4689 : 37938 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4690 : 18969 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4691 : 37938 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4692 : :
4693 : 774513 : case TRUTH_NOT_EXPR:
4694 : 774513 : return TREE_OPERAND (arg, 0);
4695 : :
4696 : 9745 : case COND_EXPR:
4697 : 9745 : {
4698 : 9745 : tree arg1 = TREE_OPERAND (arg, 1);
4699 : 9745 : tree arg2 = TREE_OPERAND (arg, 2);
4700 : :
4701 : 9745 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4702 : 9745 : 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 : 9745 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4708 : 9745 : VOID_TYPE_P (TREE_TYPE (arg1))
4709 : 9745 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4710 : 9745 : VOID_TYPE_P (TREE_TYPE (arg2))
4711 : 19487 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4712 : : }
4713 : :
4714 : 318 : case COMPOUND_EXPR:
4715 : 318 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4716 : 636 : return build2_loc (loc, COMPOUND_EXPR, type,
4717 : 318 : TREE_OPERAND (arg, 0),
4718 : 636 : 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 : 71562 : CASE_CONVERT:
4725 : 71562 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4726 : 71498 : 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 : 1772181 : fold_invert_truthvalue (location_t loc, tree arg)
4760 : : {
4761 : 1772181 : tree type = TREE_TYPE (arg);
4762 : 3544338 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4763 : : ? BIT_NOT_EXPR
4764 : : : TRUTH_NOT_EXPR,
4765 : 1772181 : 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 : 40591295 : invert_truthvalue_loc (location_t loc, tree arg)
4774 : : {
4775 : 40591295 : if (TREE_CODE (arg) == ERROR_MARK)
4776 : : return arg;
4777 : :
4778 : 40591295 : tree type = TREE_TYPE (arg);
4779 : 81182590 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4780 : : ? BIT_NOT_EXPR
4781 : : : TRUTH_NOT_EXPR,
4782 : 40591295 : 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 : 789017 : 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 : 789017 : tree result, bftype;
4797 : :
4798 : : /* Attempt not to lose the access path if possible. */
4799 : 789017 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4800 : : {
4801 : 785091 : tree ninner = TREE_OPERAND (orig_inner, 0);
4802 : 785091 : machine_mode nmode;
4803 : 785091 : poly_int64 nbitsize, nbitpos;
4804 : 785091 : tree noffset;
4805 : 785091 : int nunsignedp, nreversep, nvolatilep = 0;
4806 : 785091 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4807 : : &noffset, &nmode, &nunsignedp,
4808 : : &nreversep, &nvolatilep);
4809 : 785091 : if (base == inner
4810 : 784959 : && noffset == NULL_TREE
4811 : 784959 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4812 : 784937 : && !reversep
4813 : 784865 : && !nreversep
4814 : 1569956 : && !nvolatilep)
4815 : : {
4816 : 784865 : inner = ninner;
4817 : 785091 : bitpos -= nbitpos;
4818 : : }
4819 : : }
4820 : :
4821 : 789017 : alias_set_type iset = get_alias_set (orig_inner);
4822 : 789017 : 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 : 789017 : if (known_eq (bitpos, 0) && !reversep)
4828 : : {
4829 : 18090 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4830 : 36180 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4831 : 17886 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4832 : 208 : && tree_fits_shwi_p (size)
4833 : 18298 : && tree_to_shwi (size) == bitsize)
4834 : 185 : return fold_convert_loc (loc, type, inner);
4835 : : }
4836 : :
4837 : 788832 : bftype = type;
4838 : 788832 : if (TYPE_PRECISION (bftype) != bitsize
4839 : 788832 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4840 : 389 : bftype = build_nonstandard_integer_type (bitsize, 0);
4841 : :
4842 : 788832 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4843 : 788832 : bitsize_int (bitsize), bitsize_int (bitpos));
4844 : 788832 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4845 : :
4846 : 788832 : 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 : 4137936 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4874 : : tree compare_type, tree lhs, tree rhs)
4875 : : {
4876 : 4137936 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4877 : 4137936 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4878 : 4137936 : tree type = TREE_TYPE (lhs);
4879 : 4137936 : tree unsigned_type;
4880 : 4137936 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4881 : 4137936 : machine_mode lmode, rmode;
4882 : 4137936 : scalar_int_mode nmode;
4883 : 4137936 : int lunsignedp, runsignedp;
4884 : 4137936 : int lreversep, rreversep;
4885 : 4137936 : int lvolatilep = 0, rvolatilep = 0;
4886 : 4137936 : tree linner, rinner = NULL_TREE;
4887 : 4137936 : tree mask;
4888 : 4137936 : 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 : 4137936 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4896 : : &lunsignedp, &lreversep, &lvolatilep);
4897 : 4137936 : if (linner == lhs
4898 : 4137936 : || !known_size_p (plbitsize)
4899 : 4137936 : || !plbitsize.is_constant (&lbitsize)
4900 : 4137936 : || !plbitpos.is_constant (&lbitpos)
4901 : 8275872 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4902 : 775247 : || offset != 0
4903 : 775222 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4904 : 4913158 : || lvolatilep)
4905 : 3362774 : return 0;
4906 : :
4907 : 775162 : if (const_p)
4908 : 762323 : 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 : 12839 : rinner
4914 : 12839 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4915 : : &runsignedp, &rreversep, &rvolatilep);
4916 : :
4917 : 12839 : if (rinner == rhs
4918 : 12795 : || maybe_ne (lbitpos, rbitpos)
4919 : 12761 : || maybe_ne (lbitsize, rbitsize)
4920 : 12761 : || lunsignedp != runsignedp
4921 : 12761 : || lreversep != rreversep
4922 : 12761 : || offset != 0
4923 : 12761 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4924 : 25600 : || rvolatilep)
4925 : : return 0;
4926 : : }
4927 : :
4928 : : /* Honor the C++ memory model and mimic what RTL expansion does. */
4929 : 775084 : poly_uint64 bitstart = 0;
4930 : 775084 : poly_uint64 bitend = 0;
4931 : 775084 : if (TREE_CODE (lhs) == COMPONENT_REF)
4932 : : {
4933 : 775084 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4934 : 775084 : 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 : 1550168 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4941 : 762323 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4942 : 12761 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4943 : : TYPE_ALIGN (TREE_TYPE (rinner))),
4944 : 775084 : 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 : 773087 : 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 : 773087 : nbitsize = GET_MODE_BITSIZE (nmode);
4955 : 773087 : nbitpos = lbitpos & ~ (nbitsize - 1);
4956 : 773087 : lbitpos -= nbitpos;
4957 : 773087 : if (nbitsize == lbitsize)
4958 : : return 0;
4959 : :
4960 : 773087 : 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 : 773087 : mask = build_int_cst_type (unsigned_type, -1);
4965 : 773087 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4966 : 773087 : mask = const_binop (RSHIFT_EXPR, mask,
4967 : 773087 : size_int (nbitsize - lbitsize - lbitpos));
4968 : :
4969 : 773087 : if (! const_p)
4970 : : {
4971 : 11206 : if (nbitpos < 0)
4972 : : return 0;
4973 : :
4974 : : /* If not comparing with constant, just rework the comparison
4975 : : and return. */
4976 : 11206 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4977 : 11206 : nbitsize, nbitpos, 1, lreversep);
4978 : 11206 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4979 : 11206 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4980 : 11206 : nbitsize, nbitpos, 1, rreversep);
4981 : 11206 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4982 : 11206 : 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 : 761881 : if (lunsignedp)
4995 : : {
4996 : 760762 : 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 : 761881 : if (nbitpos < 0)
5015 : : return 0;
5016 : :
5017 : : /* Single-bit compares should always be against zero. */
5018 : 761881 : 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 : 761881 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5028 : 761881 : nbitsize, nbitpos, 1, lreversep);
5029 : :
5030 : 761881 : rhs = const_binop (BIT_AND_EXPR,
5031 : : const_binop (LSHIFT_EXPR,
5032 : : fold_convert_loc (loc, unsigned_type, rhs),
5033 : 761881 : size_int (lbitpos)),
5034 : : mask);
5035 : :
5036 : 761881 : lhs = build2_loc (loc, code, compare_type,
5037 : : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5038 : 761881 : 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 : 2165 : sign_bit_p (tree exp, const_tree val)
5049 : : {
5050 : 2165 : int width;
5051 : 2165 : tree t;
5052 : :
5053 : : /* Tree EXP must have an integral type. */
5054 : 2165 : t = TREE_TYPE (exp);
5055 : 2165 : if (! INTEGRAL_TYPE_P (t))
5056 : : return NULL_TREE;
5057 : :
5058 : : /* Tree VAL must be an integer constant. */
5059 : 1833 : if (TREE_CODE (val) != INTEGER_CST
5060 : 1833 : || 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 : 62545941 : simple_operand_p (const_tree exp)
5080 : : {
5081 : : /* Strip any conversions that don't change the machine mode. */
5082 : 62545941 : STRIP_NOPS (exp);
5083 : :
5084 : 62545941 : return (CONSTANT_CLASS_P (exp)
5085 : 43305565 : || TREE_CODE (exp) == SSA_NAME
5086 : 77017335 : || (DECL_P (exp)
5087 : 4592382 : && ! TREE_ADDRESSABLE (exp)
5088 : 4505610 : && ! TREE_THIS_VOLATILE (exp)
5089 : 4505610 : && ! 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 : 4504158 : && ! TREE_PUBLIC (exp)
5094 : 4483514 : && ! DECL_EXTERNAL (exp)
5095 : : /* DECL_VALUE_EXPR will expand to something non-simple. */
5096 : 4483514 : && ! ((VAR_P (exp)
5097 : : || TREE_CODE (exp) == PARM_DECL
5098 : : || TREE_CODE (exp) == RESULT_DECL)
5099 : 4483514 : && 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 : 4482863 : && (! 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 : 4482863 : && (! 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 : 5971999 : simple_condition_p (tree exp)
5115 : : {
5116 : 6043176 : enum tree_code code;
5117 : :
5118 : 6043176 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5119 : 4326753 : return false;
5120 : :
5121 : 1725911 : while (CONVERT_EXPR_P (exp))
5122 : 9488 : exp = TREE_OPERAND (exp, 0);
5123 : :
5124 : 1716423 : code = TREE_CODE (exp);
5125 : :
5126 : 1716423 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5127 : 1313134 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5128 : 1313134 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5129 : :
5130 : 403289 : if (code == TRUTH_NOT_EXPR)
5131 : 71177 : return simple_condition_p (TREE_OPERAND (exp, 0));
5132 : :
5133 : 332112 : 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 : 22030567 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5174 : : tree arg1, int upper1_p)
5175 : : {
5176 : 22030567 : tree tem;
5177 : 22030567 : int result;
5178 : 22030567 : 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 : 22030567 : if (arg0 != 0 && arg1 != 0)
5186 : : {
5187 : 11436421 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5188 : : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5189 : 11436421 : STRIP_NOPS (tem);
5190 : 11436421 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5191 : : }
5192 : :
5193 : 10594146 : 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 : 10585762 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5203 : 10585762 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5204 : 10585762 : switch (code)
5205 : : {
5206 : 4960104 : case EQ_EXPR:
5207 : 4960104 : result = sgn0 == sgn1;
5208 : 4960104 : break;
5209 : 0 : case NE_EXPR:
5210 : 0 : result = sgn0 != sgn1;
5211 : 0 : break;
5212 : 373733 : case LT_EXPR:
5213 : 373733 : result = sgn0 < sgn1;
5214 : 373733 : break;
5215 : 2413373 : case LE_EXPR:
5216 : 2413373 : result = sgn0 <= sgn1;
5217 : 2413373 : break;
5218 : 2838552 : case GT_EXPR:
5219 : 2838552 : result = sgn0 > sgn1;
5220 : 2838552 : break;
5221 : 0 : case GE_EXPR:
5222 : 0 : result = sgn0 >= sgn1;
5223 : 0 : break;
5224 : 0 : default:
5225 : 0 : gcc_unreachable ();
5226 : : }
5227 : :
5228 : 10585762 : 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 : 57667638 : 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 : 57667638 : tree arg0_type = TREE_TYPE (arg0);
5241 : 57667638 : tree n_low, n_high, low = *p_low, high = *p_high;
5242 : 57667638 : int in_p = *p_in_p, n_in_p;
5243 : :
5244 : 57667638 : switch (code)
5245 : : {
5246 : 1635163 : case TRUTH_NOT_EXPR:
5247 : : /* We can only do something if the range is testing for zero. */
5248 : 1635163 : if (low == NULL_TREE || high == NULL_TREE
5249 : 1635163 : || ! integer_zerop (low) || ! integer_zerop (high))
5250 : 0 : return NULL_TREE;
5251 : 1635163 : *p_in_p = ! in_p;
5252 : 1635163 : return arg0;
5253 : :
5254 : 45351943 : case EQ_EXPR: case NE_EXPR:
5255 : 45351943 : 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 : 45351943 : if (low == NULL_TREE || high == NULL_TREE
5262 : 45351943 : || ! integer_zerop (low) || ! integer_zerop (high)
5263 : 90703799 : || TREE_CODE (arg1) != INTEGER_CST)
5264 : 16820001 : return NULL_TREE;
5265 : :
5266 : 28531942 : switch (code)
5267 : : {
5268 : : case NE_EXPR: /* - [c, c] */
5269 : : low = high = arg1;
5270 : : break;
5271 : 7853113 : case EQ_EXPR: /* + [c, c] */
5272 : 7853113 : in_p = ! in_p, low = high = arg1;
5273 : 7853113 : break;
5274 : 2171347 : case GT_EXPR: /* - [-, c] */
5275 : 2171347 : low = 0, high = arg1;
5276 : 2171347 : break;
5277 : 719473 : case GE_EXPR: /* + [c, -] */
5278 : 719473 : in_p = ! in_p, low = arg1, high = 0;
5279 : 719473 : break;
5280 : 5420726 : case LT_EXPR: /* - [c, -] */
5281 : 5420726 : low = arg1, high = 0;
5282 : 5420726 : break;
5283 : 4351933 : case LE_EXPR: /* + [-, c] */
5284 : 4351933 : in_p = ! in_p, low = 0, high = arg1;
5285 : 4351933 : 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 : 28531942 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5296 : : {
5297 : 1883711 : 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 : 1883702 : 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 : 1883702 : if (high == 0 && low && ! integer_zerop (low))
5309 : : {
5310 : 837401 : in_p = ! in_p;
5311 : 837401 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5312 : 837401 : build_int_cst (TREE_TYPE (low), 1), 0);
5313 : 837401 : low = build_int_cst (arg0_type, 0);
5314 : : }
5315 : : }
5316 : :
5317 : 28531933 : *p_low = low;
5318 : 28531933 : *p_high = high;
5319 : 28531933 : *p_in_p = in_p;
5320 : 28531933 : 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 : 864395 : case PLUS_EXPR:
5351 : 864395 : case MINUS_EXPR:
5352 : 864395 : 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 : 659866 : if (!TYPE_UNSIGNED (arg0_type)
5358 : 659866 : && !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 : 926960 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5366 : : arg0_type, low, 0, arg1, 0);
5367 : 464974 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5368 : : arg0_type, high, 1, arg1, 0);
5369 : 462062 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5370 : 927024 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5371 : : return NULL_TREE;
5372 : :
5373 : 464962 : if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5374 : 21587 : *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 : 465190 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5380 : : {
5381 : 133831 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5382 : 133831 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5383 : 133831 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5384 : 133831 : 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 : 133831 : if (tree_int_cst_equal (n_low, low)
5391 : 133831 : && tree_int_cst_equal (n_high, high))
5392 : : low = high = 0;
5393 : : else
5394 : 133831 : in_p = ! in_p;
5395 : : }
5396 : : else
5397 : 331359 : low = n_low, high = n_high;
5398 : :
5399 : 465190 : *p_low = low;
5400 : 465190 : *p_high = high;
5401 : 465190 : *p_in_p = in_p;
5402 : 465190 : return arg0;
5403 : :
5404 : 2531261 : CASE_CONVERT:
5405 : 2531261 : case NON_LVALUE_EXPR:
5406 : 2531261 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5407 : : return NULL_TREE;
5408 : :
5409 : 1110178 : if (! INTEGRAL_TYPE_P (arg0_type)
5410 : 1075855 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5411 : 968455 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5412 : : return NULL_TREE;
5413 : :
5414 : 947757 : n_low = low, n_high = high;
5415 : :
5416 : 947757 : if (n_low != 0)
5417 : 783203 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5418 : :
5419 : 947757 : if (n_high != 0)
5420 : 877658 : 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 : 947757 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5432 : : {
5433 : 259342 : tree high_positive;
5434 : 259342 : tree equiv_type;
5435 : : /* For fixed-point modes, we need to pass the saturating flag
5436 : : as the 2nd parameter. */
5437 : 259342 : 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 : 259342 : else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5442 : : equiv_type = arg0_type;
5443 : : else
5444 : 259334 : equiv_type
5445 : 259334 : = 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 : 259342 : high_positive
5451 : 259342 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5452 : 0 : : TYPE_MAX_VALUE (arg0_type);
5453 : :
5454 : 259342 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5455 : 238762 : 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 : 259342 : if (low != 0)
5464 : : {
5465 : 100166 : 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 : 100166 : 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 : 159176 : 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 : 159176 : 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 : 688415 : else if (TYPE_UNSIGNED (exp_type)
5498 : 669825 : && !TYPE_UNSIGNED (arg0_type)
5499 : 385039 : && low
5500 : 1073454 : && !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 : 947757 : *p_low = n_low;
5514 : 947757 : *p_high = n_high;
5515 : 947757 : *p_in_p = in_p;
5516 : 947757 : 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 : 47736298 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5534 : : bool *strict_overflow_p)
5535 : : {
5536 : 47736298 : enum tree_code code;
5537 : 47736298 : tree arg0, arg1 = NULL_TREE;
5538 : 47736298 : tree exp_type, nexp;
5539 : 47736298 : int in_p;
5540 : 47736298 : tree low, high;
5541 : 47736298 : 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 : 47736298 : in_p = 0;
5550 : 47736298 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5551 : :
5552 : 76359185 : while (1)
5553 : : {
5554 : 76359185 : code = TREE_CODE (exp);
5555 : 76359185 : exp_type = TREE_TYPE (exp);
5556 : 76359185 : arg0 = NULL_TREE;
5557 : :
5558 : 76359185 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5559 : : {
5560 : 52978638 : if (TREE_OPERAND_LENGTH (exp) > 0)
5561 : 52978638 : arg0 = TREE_OPERAND (exp, 0);
5562 : 52978638 : if (TREE_CODE_CLASS (code) == tcc_binary
5563 : 49714048 : || TREE_CODE_CLASS (code) == tcc_comparison
5564 : 61319738 : || (TREE_CODE_CLASS (code) == tcc_expression
5565 : 2678569 : && TREE_OPERAND_LENGTH (exp) > 1))
5566 : 45660596 : arg1 = TREE_OPERAND (exp, 1);
5567 : : }
5568 : 52978638 : if (arg0 == NULL_TREE)
5569 : : break;
5570 : :
5571 : 52978624 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5572 : : &high, &in_p, strict_overflow_p);
5573 : 52978624 : 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 : 47736298 : if (TREE_CODE (exp) == INTEGER_CST)
5580 : : {
5581 : 34706 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5582 : : exp, 0, low, 0))
5583 : 34706 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5584 : : exp, 1, high, 1)));
5585 : 34706 : low = high = 0;
5586 : 34706 : exp = 0;
5587 : : }
5588 : :
5589 : 47736298 : *pin_p = in_p, *plow = low, *phigh = high;
5590 : 47736298 : 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 : 557261 : 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 : 557261 : if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5636 : 64194 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5637 : :
5638 : 557261 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5639 : : {
5640 : 408260 : 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 : 408260 : utype = unsigned_type_for (etype);
5645 : 408260 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5646 : 408260 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5647 : 408260 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5648 : 408260 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5649 : :
5650 : 408260 : 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 : 149001 : 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 : 1361 : 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 : 1427287 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5672 : : tree low, tree high)
5673 : : {
5674 : 2503256 : 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 : 2503256 : if (targetm.have_canonicalize_funcptr_for_compare ()
5679 : 0 : && POINTER_TYPE_P (etype)
5680 : 2503256 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5681 : : return NULL_TREE;
5682 : :
5683 : 2503256 : if (! in_p)
5684 : : {
5685 : 335719 : value = build_range_check (loc, type, exp, 1, low, high);
5686 : 335719 : if (value != 0)
5687 : 335719 : return invert_truthvalue_loc (loc, value);
5688 : :
5689 : : return 0;
5690 : : }
5691 : :
5692 : 2167537 : if (low == 0 && high == 0)
5693 : 130455 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5694 : :
5695 : 2037082 : if (low == 0)
5696 : 696436 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5697 : 696436 : fold_convert_loc (loc, etype, high));
5698 : :
5699 : 1340646 : if (high == 0)
5700 : 72028 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5701 : 72028 : fold_convert_loc (loc, etype, low));
5702 : :
5703 : 1268618 : if (operand_equal_p (low, high, 0))
5704 : 192469 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5705 : 192469 : fold_convert_loc (loc, etype, low));
5706 : :
5707 : 1076149 : if (TREE_CODE (exp) == BIT_AND_EXPR
5708 : 1076149 : && 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 : 1076063 : if (integer_zerop (low))
5715 : : {
5716 : 611135 : if (! TYPE_UNSIGNED (etype))
5717 : : {
5718 : 126827 : etype = unsigned_type_for (etype);
5719 : 126827 : high = fold_convert_loc (loc, etype, high);
5720 : 126827 : exp = fold_convert_loc (loc, etype, exp);
5721 : : }
5722 : 611135 : return build_range_check (loc, type, exp, 1, 0, high);
5723 : : }
5724 : :
5725 : : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5726 : 464928 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5727 : : {
5728 : 105798 : int prec = TYPE_PRECISION (etype);
5729 : :
5730 : 105798 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5731 : : {
5732 : 94 : if (TYPE_UNSIGNED (etype))
5733 : : {
5734 : 88 : tree signed_etype = signed_type_for (etype);
5735 : 88 : 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 : 88 : exp = fold_convert_loc (loc, etype, exp);
5741 : : }
5742 : 94 : 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 : 464834 : etype = range_check_type (etype);
5750 : 464834 : if (etype == NULL_TREE)
5751 : : return NULL_TREE;
5752 : :
5753 : 464834 : high = fold_convert_loc (loc, etype, high);
5754 : 464834 : low = fold_convert_loc (loc, etype, low);
5755 : 464834 : exp = fold_convert_loc (loc, etype, exp);
5756 : :
5757 : 464834 : value = const_binop (MINUS_EXPR, high, low);
5758 : :
5759 : 464834 : if (value != 0 && !TREE_OVERFLOW (value))
5760 : 464834 : 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 : 157425 : range_predecessor (tree val)
5771 : : {
5772 : 157425 : tree type = TREE_TYPE (val);
5773 : :
5774 : 157425 : if (INTEGRAL_TYPE_P (type)
5775 : 157425 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5776 : : return 0;
5777 : : else
5778 : 157425 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5779 : 157425 : 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 : 1557329 : range_successor (tree val)
5786 : : {
5787 : 1557329 : tree type = TREE_TYPE (val);
5788 : :
5789 : 1557329 : if (INTEGRAL_TYPE_P (type)
5790 : 1557329 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5791 : : return 0;
5792 : : else
5793 : 1557320 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5794 : 1557320 : 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 : 3433376 : 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 : 3433376 : bool no_overlap;
5805 : 3433376 : int subset;
5806 : 3433376 : int temp;
5807 : 3433376 : tree tem;
5808 : 3433376 : int in_p;
5809 : 3433376 : tree low, high;
5810 : 3433376 : int lowequal = ((low0 == 0 && low1 == 0)
5811 : 3433376 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5812 : 3433376 : low0, 0, low1, 0)));
5813 : 3433376 : int highequal = ((high0 == 0 && high1 == 0)
5814 : 3433376 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5815 : 3433376 : 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 : 3433376 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5820 : : low0, 0, low1, 0))
5821 : 3433376 : || (lowequal
5822 : 542161 : && 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 : 3433376 : if (low1
5833 : 3433376 : && high1
5834 : 921224 : && TREE_CODE (low1) == INTEGER_CST
5835 : 921224 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5836 : 148722 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5837 : 206672 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5838 : : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5839 : 4309214 : && operand_equal_p (low1, high1, 0))
5840 : : {
5841 : 479707 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5842 : 479707 : && 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 : 389996 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5848 : 389996 : && 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 : 3279094 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5857 : : high0, 1, low1, 0));
5858 : 3279094 : 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 : 3279094 : 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 : 1475038 : if (no_overlap)
5869 : : in_p = 0, low = high = 0;
5870 : 1473446 : else if (subset)
5871 : : in_p = 1, low = low1, high = high1;
5872 : : else
5873 : 1345527 : in_p = 1, low = low1, high = high0;
5874 : : }
5875 : :
5876 : 1804056 : 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 : 307763 : if (no_overlap)
5887 : : in_p = 1, low = low0, high = high0;
5888 : 303400 : else if (lowequal && highequal)
5889 : : in_p = 0, low = high = 0;
5890 : 302579 : else if (subset && lowequal)
5891 : : {
5892 : 223730 : low = range_successor (high1);
5893 : 223730 : high = high0;
5894 : 223730 : in_p = 1;
5895 : 223730 : 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 : 78849 : else if (! subset || highequal)
5903 : : {
5904 : 55265 : low = low0;
5905 : 55265 : high = range_predecessor (low1);
5906 : 55265 : in_p = 1;
5907 : 55265 : 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 : 1496293 : 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 : 1158709 : if (no_overlap)
5924 : : in_p = 1, low = low1, high = high1;
5925 : 1150278 : else if (subset || highequal)
5926 : : in_p = 0, low = high = 0;
5927 : : else
5928 : : {
5929 : 1036842 : low = range_successor (high0);
5930 : 1036842 : high = high1;
5931 : 1036842 : in_p = 1;
5932 : 1036842 : 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 : 337584 : if (no_overlap)
5949 : : {
5950 : 231862 : 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 : 177035 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5958 : 175970 : switch (TREE_CODE (TREE_TYPE (low0)))
5959 : : {
5960 : 67078 : case ENUMERAL_TYPE:
5961 : 67078 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5962 : : GET_MODE_BITSIZE
5963 : 134156 : (TYPE_MODE (TREE_TYPE (low0)))))
5964 : : break;
5965 : : /* FALLTHROUGH */
5966 : 175777 : case INTEGER_TYPE:
5967 : 175777 : if (tree_int_cst_equal (low0,
5968 : 175777 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5969 : 6677 : 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 : 177035 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5982 : 176837 : switch (TREE_CODE (TREE_TYPE (high1)))
5983 : : {
5984 : 67086 : case ENUMERAL_TYPE:
5985 : 67086 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5986 : : GET_MODE_BITSIZE
5987 : 134172 : (TYPE_MODE (TREE_TYPE (high1)))))
5988 : : break;
5989 : : /* FALLTHROUGH */
5990 : 176644 : case INTEGER_TYPE:
5991 : 176644 : if (tree_int_cst_equal (high1,
5992 : 176644 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5993 : 12457 : 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 : 177035 : if (low0 == 0 && high1 == 0)
6012 : : {
6013 : 324 : low = range_successor (high0);
6014 : 324 : high = range_predecessor (low1);
6015 : 324 : if (low == 0 || high == 0)
6016 : : return 0;
6017 : :
6018 : : in_p = 1;
6019 : : }
6020 : : else
6021 : : return 0;
6022 : : }
6023 : : }
6024 : 105722 : 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 : 3078790 : *pin_p = in_p, *plow = low, *phigh = high;
6031 : 3078790 : 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 : 449383 : 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 : 449383 : tree arg1_type = TREE_TYPE (arg1);
6050 : 449383 : tree tem;
6051 : :
6052 : 449383 : STRIP_NOPS (arg1);
6053 : 449383 : 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 : 449383 : if (!HONOR_SIGNED_ZEROS (type)
6075 : 898776 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6076 : 449383 : ? real_zerop (arg01)
6077 : 448323 : : integer_zerop (arg01))
6078 : 1191340 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6079 : 1493 : && 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 : 291305 : || (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 : 1269 : 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 : 1269 : case GE_EXPR:
6103 : 1269 : case GT_EXPR:
6104 : 1269 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6105 : : break;
6106 : 1253 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6107 : 1253 : 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 : 448130 : if (!HONOR_SIGNED_ZEROS (type)
6144 : 448130 : && integer_zerop (arg01) && integer_zerop (arg2))
6145 : : {
6146 : 245598 : if (comp_code == NE_EXPR)
6147 : 145 : return fold_convert_loc (loc, type, arg1);
6148 : 245453 : 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 : 447985 : if (!HONOR_SIGNED_ZEROS (type)
6179 : 447985 : && 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 : 701917 : && (in_gimple_form
6183 : 16438 : || VECTOR_TYPE_P (type)
6184 : 16376 : || (! lang_GNU_CXX ()
6185 : 13771 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6186 : 2605 : || ! maybe_lvalue_p (arg1)
6187 : 2584 : || ! maybe_lvalue_p (arg2)))
6188 : : {
6189 : 252281 : tree comp_op0 = arg00;
6190 : 252281 : tree comp_op1 = arg01;
6191 : 252281 : tree comp_type = TREE_TYPE (comp_op0);
6192 : :
6193 : 252281 : 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 : 5841 : case LE_EXPR:
6200 : 5841 : case LT_EXPR:
6201 : 5841 : case UNLE_EXPR:
6202 : 5841 : 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 : 5841 : if (!HONOR_NANS (arg1))
6208 : : {
6209 : 5841 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6210 : 5841 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6211 : 5841 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6212 : 5841 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6213 : 4502 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6214 : : comp_op1, comp_op0);
6215 : 5841 : return fold_convert_loc (loc, type, tem);
6216 : : }
6217 : : break;
6218 : 246439 : case GE_EXPR:
6219 : 246439 : case GT_EXPR:
6220 : 246439 : case UNGE_EXPR:
6221 : 246439 : case UNGT_EXPR:
6222 : 246439 : if (!HONOR_NANS (arg1))
6223 : : {
6224 : 246437 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6225 : 246437 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6226 : 246437 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6227 : 246437 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6228 : 3530 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6229 : : comp_op1, comp_op0);
6230 : 246437 : 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 : 23867643 : fold_range_test (location_t loc, enum tree_code code, tree type,
6263 : : tree op0, tree op1)
6264 : : {
6265 : 23867643 : int or_op = (code == TRUTH_ORIF_EXPR
6266 : 23867643 : || code == TRUTH_OR_EXPR);
6267 : 23867643 : int in0_p, in1_p, in_p;
6268 : 23867643 : tree low0, low1, low, high0, high1, high;
6269 : 23867643 : bool strict_overflow_p = false;
6270 : 23867643 : tree tem, lhs, rhs;
6271 : 23867643 : const char * const warnmsg = G_("assuming signed overflow does not occur "
6272 : : "when simplifying range test");
6273 : :
6274 : 23867643 : if (!INTEGRAL_TYPE_P (type))
6275 : : return 0;
6276 : :
6277 : 23867643 : 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 : 23867643 : if (!lhs
6282 : 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6283 : 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6284 : : return op0;
6285 : 23867641 : 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 : 23867641 : if (or_op)
6290 : 11559245 : 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 : 23832939 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6297 : 1091079 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6298 : : in1_p, low1, high1)
6299 : 24793008 : && (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 : 925367 : if (strict_overflow_p)
6305 : 171 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6306 : 925367 : 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 : 22942274 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6313 : 22942274 : if (param_logical_op_non_short_circuit != -1)
6314 : 7781 : logical_op_non_short_circuit
6315 : 7781 : = param_logical_op_non_short_circuit;
6316 : 22942274 : if (logical_op_non_short_circuit
6317 : 22938383 : && !sanitize_coverage_p ()
6318 : 22938380 : && lhs != 0 && rhs != 0
6319 : 22938077 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6320 : 27614672 : && 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 : 135957 : if (simple_operand_p (lhs))
6326 : 129185 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6327 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6328 : 65116 : type, op0, op1);
6329 : :
6330 : 70841 : else if (!lang_hooks.decls.global_bindings_p ()
6331 : 70841 : && !CONTAINS_PLACEHOLDER_P (lhs))
6332 : : {
6333 : 70188 : tree common = save_expr (lhs);
6334 : :
6335 : 115129 : if ((lhs = build_range_check (loc, type, common,
6336 : 44941 : or_op ? ! in0_p : in0_p,
6337 : : low0, high0)) != 0
6338 : 115129 : && (rhs = build_range_check (loc, type, common,
6339 : 44941 : or_op ? ! in1_p : in1_p,
6340 : : low1, high1)) != 0)
6341 : : {
6342 : 70188 : if (strict_overflow_p)
6343 : 0 : fold_overflow_warning (warnmsg,
6344 : : WARN_STRICT_OVERFLOW_COMPARISON);
6345 : 115129 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6346 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6347 : 70188 : 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 : 144557 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6373 : : bool rhs_only)
6374 : : {
6375 : 144557 : enum tree_code code = TREE_CODE (cmpop);
6376 : 144557 : enum tree_code truthop_code = TREE_CODE (op);
6377 : 144557 : tree lhs = TREE_OPERAND (op, 0);
6378 : 144557 : tree rhs = TREE_OPERAND (op, 1);
6379 : 144557 : tree orig_lhs = lhs, orig_rhs = rhs;
6380 : 144557 : enum tree_code rhs_code = TREE_CODE (rhs);
6381 : 144557 : enum tree_code lhs_code = TREE_CODE (lhs);
6382 : 144557 : enum tree_code inv_code;
6383 : :
6384 : 144557 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6385 : : return NULL_TREE;
6386 : :
6387 : 94542 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6388 : : return NULL_TREE;
6389 : :
6390 : 54965 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6391 : :
6392 : 54965 : 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 : 54965 : 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 : 54965 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6412 : 54965 : if (inv_code == rhs_code
6413 : 925 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6414 : 55001 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6415 : : return lhs;
6416 : 54952 : if (!rhs_only && inv_code == lhs_code
6417 : 630 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6418 : 55044 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6419 : : return rhs;
6420 : 54861 : 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 : 23538768 : 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 : 23538768 : enum tree_code lcode, rcode;
6457 : 23538768 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6458 : 23538768 : 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 : 23538768 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6465 : : return 0;
6466 : :
6467 : 21207585 : lcode = TREE_CODE (lhs);
6468 : 21207585 : rcode = TREE_CODE (rhs);
6469 : :
6470 : 21207585 : 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 : 21207585 : 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 : 21207585 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6485 : 18899498 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6486 : : return 0;
6487 : :
6488 : 17754602 : ll_arg = TREE_OPERAND (lhs, 0);
6489 : 17754602 : lr_arg = TREE_OPERAND (lhs, 1);
6490 : 17754602 : rl_arg = TREE_OPERAND (rhs, 0);
6491 : 17754602 : rr_arg = TREE_OPERAND (rhs, 1);
6492 : :
6493 : : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6494 : 17754602 : if (simple_operand_p (ll_arg)
6495 : 17754602 : && simple_operand_p (lr_arg))
6496 : : {
6497 : 14449567 : if (operand_equal_p (ll_arg, rl_arg, 0)
6498 : 14449567 : && operand_equal_p (lr_arg, rr_arg, 0))
6499 : : {
6500 : 20249 : result = combine_comparisons (loc, code, lcode, rcode,
6501 : : truth_type, ll_arg, lr_arg);
6502 : 20249 : if (result)
6503 : : return result;
6504 : : }
6505 : 14429318 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6506 : 14429318 : && 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 : 8542247 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6517 : 17734652 : ? 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 : 17734652 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6525 : : false) >= 2
6526 : 17734549 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6527 : 16716234 : && simple_operand_p (rl_arg)
6528 : 27217202 : && simple_operand_p (rr_arg))
6529 : : {
6530 : : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6531 : 10578806 : if (code == TRUTH_OR_EXPR
6532 : 1462471 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6533 : 597830 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6534 : 25934 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6535 : 10598274 : && 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 : 10559646 : if (code == TRUTH_AND_EXPR
6543 : 1615626 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6544 : 750711 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6545 : 5321 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6546 : 10561219 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6547 : 2680 : return build2_loc (loc, EQ_EXPR, truth_type,
6548 : 1340 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6549 : : ll_arg, rl_arg),
6550 : 1340 : 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 : 93880602 : 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 : 93880602 : static int depth;
6584 : 93880602 : tree ret;
6585 : :
6586 : 93880602 : if (depth > 3)
6587 : : return NULL;
6588 : :
6589 : 90606865 : depth++;
6590 : 90606865 : ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6591 : 90606865 : depth--;
6592 : :
6593 : 90606865 : return ret;
6594 : : }
6595 : :
6596 : : static tree
6597 : 90606865 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6598 : : bool *strict_overflow_p)
6599 : : {
6600 : 90606865 : tree type = TREE_TYPE (t);
6601 : 90606865 : enum tree_code tcode = TREE_CODE (t);
6602 : 90606865 : tree ctype = type;
6603 : 90606865 : if (wide_type)
6604 : : {
6605 : 30338718 : if (TREE_CODE (type) == BITINT_TYPE
6606 : 30338599 : || TREE_CODE (wide_type) == BITINT_TYPE)
6607 : : {
6608 : 119 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6609 : 8655359 : ctype = wide_type;
6610 : : }
6611 : 30338599 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6612 : 60677198 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6613 : 8655359 : ctype = wide_type;
6614 : : }
6615 : 90606865 : tree t1, t2;
6616 : 90606865 : bool same_p = tcode == code;
6617 : 90606865 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6618 : 90606865 : bool sub_strict_overflow_p;
6619 : :
6620 : : /* Don't deal with constants of zero here; they confuse the code below. */
6621 : 90606865 : if (integer_zerop (c))
6622 : : return NULL_TREE;
6623 : :
6624 : 90585705 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6625 : 36252919 : op0 = TREE_OPERAND (t, 0);
6626 : :
6627 : 90585705 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6628 : 10967156 : 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 : 90585705 : switch (tcode)
6633 : : {
6634 : 4154712 : 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 : 4154712 : if (code == MULT_EXPR
6638 : 5371684 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6639 : 1216972 : TYPE_SIGN (type)))
6640 : : {
6641 : 3341871 : 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 : 3341871 : if (TREE_OVERFLOW (tem))
6646 : : return NULL_TREE;
6647 : : return tem;
6648 : : }
6649 : : break;
6650 : :
6651 : 35614792 : CASE_CONVERT: case NON_LVALUE_EXPR:
6652 : 35614792 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6653 : : break;
6654 : : /* If op0 is an expression ... */
6655 : 34392738 : if ((COMPARISON_CLASS_P (op0)
6656 : : || UNARY_CLASS_P (op0)
6657 : 34392738 : || BINARY_CLASS_P (op0)
6658 : 31477714 : || VL_EXP_CLASS_P (op0)
6659 : 31443084 : || 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 : 34515234 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6663 : 1085375 : && (TYPE_PRECISION (ctype)
6664 : 1085375 : > 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 : 2488057 : || (TYPE_PRECISION (type)
6668 : 2488057 : < TYPE_PRECISION (TREE_TYPE (op0)))
6669 : : /* ... or signedness changes for division or modulus,
6670 : : then we cannot pass through this conversion. */
6671 : 2454566 : || (code != MULT_EXPR
6672 : 113965 : && (TYPE_UNSIGNED (ctype)
6673 : 113965 : != 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 : 2360192 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6678 : 1850139 : && !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 : 31917015 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6685 : 31917015 : && TREE_CODE (t2) == INTEGER_CST
6686 : 31917015 : && !TREE_OVERFLOW (t2)
6687 : 64664550 : && (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 : 51417 : 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 : 51417 : 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 : 1320 : 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 : 1320 : if (TREE_CODE (op1) == INTEGER_CST
6773 : 1304 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6774 : : /* const_binop may not detect overflow correctly,
6775 : : so check for it explicitly here. */
6776 : 1187 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6777 : 1329 : wi::to_wide (op1))
6778 : 1178 : && (t1 = fold_convert (ctype,
6779 : : const_binop (LSHIFT_EXPR, size_one_node,
6780 : : op1))) != 0
6781 : 2498 : && !TREE_OVERFLOW (t1))
6782 : 2154 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6783 : : ? MULT_EXPR : FLOOR_DIV_EXPR,
6784 : : ctype,
6785 : : fold_convert (ctype, op0),
6786 : : t1),
6787 : 1178 : c, code, wide_type, strict_overflow_p);
6788 : : break;
6789 : :
6790 : 7725126 : 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 : 7725126 : sub_strict_overflow_p = false;
6796 : 7725126 : t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6797 : 7725126 : t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6798 : 810610 : if (t1 != 0 && t2 != 0
6799 : 275436 : && TYPE_OVERFLOW_WRAPS (ctype)
6800 : 7991363 : && (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 : 266237 : if (sub_strict_overflow_p)
6807 : 0 : *strict_overflow_p = true;
6808 : 266237 : 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 : 7458889 : if (tcode == MINUS_EXPR)
6815 : : {
6816 : 1974134 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6817 : : /* If OP1 was not easily negatable, the constant may be OP0. */
6818 : 1974134 : if (TREE_CODE (op0) == INTEGER_CST)
6819 : : {
6820 : 365572 : std::swap (op0, op1);
6821 : 365572 : std::swap (t1, t2);
6822 : : }
6823 : : }
6824 : :
6825 : 7458889 : 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 : 3477324 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6832 : : {
6833 : 171234 : if (code == CEIL_DIV_EXPR)
6834 : : code = FLOOR_DIV_EXPR;
6835 : 171232 : else if (code == FLOOR_DIV_EXPR)
6836 : : code = CEIL_DIV_EXPR;
6837 : 170823 : else if (code != MULT_EXPR
6838 : 170823 : && 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 : 3471989 : if (code == MULT_EXPR
6845 : 4684358 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6846 : 1212369 : TYPE_SIGN (type)))
6847 : : {
6848 : 2659626 : op1 = const_binop (code, fold_convert (ctype, op1),
6849 : : fold_convert (ctype, c));
6850 : : /* We allow the constant to overflow with wrapping semantics. */
6851 : 2659626 : if (op1 == 0
6852 : 2659626 : || (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 : 2656285 : 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 : 2656285 : if (code == MULT_EXPR
6871 : 2257010 : && TYPE_OVERFLOW_WRAPS (ctype)
6872 : 4579972 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6873 : 468461 : 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 : 2175509 : 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 : 2175509 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6885 : 2175509 : || 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 : 2186477 : && 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 : 2361284 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6899 : 2361284 : 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 : 2361284 : if (same_p
6904 : 2038655 : && TYPE_OVERFLOW_WRAPS (ctype)
6905 : 4224564 : && (t1 = extract_muldiv (op0, c, code, wide_type,
6906 : : strict_overflow_p)) != 0)
6907 : 66434 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6908 : : fold_convert (ctype, op1));
6909 : 2294850 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6910 : 1969763 : && TYPE_OVERFLOW_WRAPS (ctype)
6911 : 4089286 : && (t1 = extract_muldiv (op1, c, code, wide_type,
6912 : : strict_overflow_p)) != 0)
6913 : 929880 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6914 : : fold_convert (ctype, t1));
6915 : 1364970 : 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 : 515730 : if (tcode == code)
6921 : : {
6922 : 193632 : bool overflow_p = false;
6923 : 193632 : wi::overflow_type overflow_mul;
6924 : 193632 : signop sign = TYPE_SIGN (ctype);
6925 : 193632 : unsigned prec = TYPE_PRECISION (ctype);
6926 : 387264 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6927 : 193632 : wi::to_wide (c, prec),
6928 : 193632 : sign, &overflow_mul);
6929 : 193632 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6930 : 193632 : if (overflow_mul
6931 : 1291 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6932 : : overflow_p = true;
6933 : 193565 : if (!overflow_p)
6934 : 193565 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6935 : : wide_int_to_tree (ctype, mul));
6936 : 193632 : }
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 : 322165 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6946 : 29750 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6947 : 351872 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6948 : 29667 : || (tcode == MULT_EXPR
6949 : 29667 : && 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 : 85461755 : constant_boolean_node (bool value, tree type)
6987 : : {
6988 : 85461755 : if (type == integer_type_node)
6989 : 19438550 : return value ? integer_one_node : integer_zero_node;
6990 : 66023205 : else if (type == boolean_type_node)
6991 : 62589263 : return value ? boolean_true_node : boolean_false_node;
6992 : 3433942 : 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 : 3433070 : 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 : 978559 : 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 : 978559 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7017 : 978559 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7018 : 978559 : tree test, true_value, false_value;
7019 : 978559 : tree lhs = NULL_TREE;
7020 : 978559 : tree rhs = NULL_TREE;
7021 : 978559 : 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 : 999092 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7026 : 202966 : ANY_INTEGRAL_TYPE_P (type)
7027 : 981073 : && TYPE_OVERFLOW_TRAPS (type), op1))
7028 : : return NULL_TREE;
7029 : :
7030 : 957860 : if (TREE_CODE (cond) == COND_EXPR
7031 : 379073 : || TREE_CODE (cond) == VEC_COND_EXPR)
7032 : : {
7033 : 581138 : test = TREE_OPERAND (cond, 0);
7034 : 581138 : true_value = TREE_OPERAND (cond, 1);
7035 : 581138 : 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 : 581138 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7040 : 7463 : lhs = true_value;
7041 : 581138 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7042 : 6 : rhs = false_value;
7043 : : }
7044 : 376722 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7045 : 376568 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7046 : : {
7047 : 374970 : tree testtype = TREE_TYPE (cond);
7048 : 374970 : test = cond;
7049 : 374970 : true_value = constant_boolean_node (true, testtype);
7050 : 374970 : 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 : 956108 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7057 : 2505 : 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 : 956108 : if (!TREE_CONSTANT (arg)
7063 : 956108 : && (TREE_SIDE_EFFECTS (arg)
7064 : 461480 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7065 : 457236 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7066 : : return NULL_TREE;
7067 : :
7068 : 510083 : arg = fold_convert_loc (loc, arg_type, arg);
7069 : 510083 : if (lhs == 0)
7070 : : {
7071 : 504052 : true_value = fold_convert_loc (loc, cond_type, true_value);
7072 : 504052 : if (cond_first_p)
7073 : 494437 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7074 : : else
7075 : 9615 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7076 : : }
7077 : 510083 : if (rhs == 0)
7078 : : {
7079 : 510077 : false_value = fold_convert_loc (loc, cond_type, false_value);
7080 : 510077 : if (cond_first_p)
7081 : 499901 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7082 : : else
7083 : 10176 : 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 : 510083 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7088 : : return NULL_TREE;
7089 : :
7090 : 490449 : 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 : 644793 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7110 : : const_tree zero_arg, int negate)
7111 : : {
7112 : 644793 : 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 : 1750157 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7153 : : tree *hi, bool *neg_overflow)
7154 : : {
7155 : 1750157 : tree prod, tmp, type = TREE_TYPE (c1);
7156 : 1750157 : signop sign = TYPE_SIGN (type);
7157 : 1750157 : 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 : 1750157 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7162 : 1750157 : prod = force_fit_type (type, val, -1, overflow);
7163 : 1750157 : *neg_overflow = false;
7164 : :
7165 : 1750157 : if (sign == UNSIGNED)
7166 : : {
7167 : 1721431 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7168 : 1721431 : *lo = prod;
7169 : :
7170 : : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7171 : 1721431 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7172 : 1721431 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7173 : : }
7174 : 28726 : else if (tree_int_cst_sgn (c1) >= 0)
7175 : : {
7176 : 27323 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7177 : 27323 : switch (tree_int_cst_sgn (c2))
7178 : : {
7179 : 4676 : case -1:
7180 : 4676 : *neg_overflow = true;
7181 : 4676 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7182 : 4676 : *hi = prod;
7183 : 4676 : break;
7184 : :
7185 : 14085 : case 0:
7186 : 14085 : *lo = fold_negate_const (tmp, type);
7187 : 14085 : *hi = tmp;
7188 : 14085 : break;
7189 : :
7190 : 8562 : case 1:
7191 : 8562 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7192 : 8562 : *lo = prod;
7193 : 8562 : 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 : 1750157 : if (code != EQ_EXPR && code != NE_EXPR)
7229 : : return code;
7230 : :
7231 : 16603 : if (TREE_OVERFLOW (*lo)
7232 : 16603 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7233 : 709 : *lo = NULL_TREE;
7234 : 16603 : if (TREE_OVERFLOW (*hi)
7235 : 16603 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7236 : 92 : *hi = NULL_TREE;
7237 : :
7238 : : return code;
7239 : 1750157 : }
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 : 1546697525 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7247 : : {
7248 : 1546697525 : if (CONSTANT_CLASS_P (arg1))
7249 : : return false;
7250 : 499010700 : if (CONSTANT_CLASS_P (arg0))
7251 : : return true;
7252 : :
7253 : 459076227 : STRIP_NOPS (arg0);
7254 : 459076227 : STRIP_NOPS (arg1);
7255 : :
7256 : 459076227 : if (TREE_CONSTANT (arg1))
7257 : : return false;
7258 : 444818039 : if (TREE_CONSTANT (arg0))
7259 : : return true;
7260 : :
7261 : : /* Put addresses in arg1. */
7262 : 444248362 : if (TREE_CODE (arg1) == ADDR_EXPR)
7263 : : return false;
7264 : 431042417 : 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 : 430647678 : if (TREE_CODE (arg0) == SSA_NAME
7272 : 325327018 : && TREE_CODE (arg1) == SSA_NAME
7273 : 750147412 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7274 : : return true;
7275 : :
7276 : : /* Put SSA_NAMEs last. */
7277 : 405702646 : if (TREE_CODE (arg1) == SSA_NAME)
7278 : : return false;
7279 : 96582321 : if (TREE_CODE (arg0) == SSA_NAME)
7280 : : return true;
7281 : :
7282 : : /* Put variables last. */
7283 : 90755037 : if (DECL_P (arg1))
7284 : : return false;
7285 : 48246658 : 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 : 22835803 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7298 : : {
7299 : 22835803 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7300 : :
7301 : 22835803 : if (TREE_CODE (bound) == LT_EXPR)
7302 : 4872211 : a = TREE_OPERAND (bound, 0);
7303 : 17963592 : else if (TREE_CODE (bound) == GT_EXPR)
7304 : 2525958 : a = TREE_OPERAND (bound, 1);
7305 : : else
7306 : : return NULL_TREE;
7307 : :
7308 : 7398169 : typea = TREE_TYPE (a);
7309 : 7398169 : if (!INTEGRAL_TYPE_P (typea)
7310 : 361971 : && !POINTER_TYPE_P (typea))
7311 : : return NULL_TREE;
7312 : :
7313 : 7212677 : if (TREE_CODE (ineq) == LT_EXPR)
7314 : : {
7315 : 1426811 : a1 = TREE_OPERAND (ineq, 1);
7316 : 1426811 : y = TREE_OPERAND (ineq, 0);
7317 : : }
7318 : 5785866 : else if (TREE_CODE (ineq) == GT_EXPR)
7319 : : {
7320 : 1063387 : a1 = TREE_OPERAND (ineq, 0);
7321 : 1063387 : y = TREE_OPERAND (ineq, 1);
7322 : : }
7323 : : else
7324 : : return NULL_TREE;
7325 : :
7326 : 2490198 : if (TREE_TYPE (a1) != typea)
7327 : : return NULL_TREE;
7328 : :
7329 : 1751034 : if (POINTER_TYPE_P (typea))
7330 : : {
7331 : : /* Convert the pointer types into integer before taking the difference. */
7332 : 8718 : tree ta = fold_convert_loc (loc, ssizetype, a);
7333 : 8718 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7334 : 8718 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7335 : : }
7336 : : else
7337 : 1742316 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7338 : :
7339 : 1751034 : if (!diff || !integer_onep (diff))
7340 : 1740704 : return NULL_TREE;
7341 : :
7342 : 10330 : 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 : 8655689 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7350 : : tree arg0, tree arg1)
7351 : : {
7352 : 8655689 : tree arg00, arg01, arg10, arg11;
7353 : 8655689 : 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 : 8655689 : if (TREE_CODE (arg0) == MULT_EXPR)
7362 : : {
7363 : 7651583 : arg00 = TREE_OPERAND (arg0, 0);
7364 : 7651583 : arg01 = TREE_OPERAND (arg0, 1);
7365 : : }
7366 : 1004106 : else if (TREE_CODE (arg0) == INTEGER_CST)
7367 : : {
7368 : 71665 : arg00 = build_one_cst (type);
7369 : 71665 : arg01 = arg0;
7370 : : }
7371 : : else
7372 : : {
7373 : : /* We cannot generate constant 1 for fract. */
7374 : 932441 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7375 : 0 : return NULL_TREE;
7376 : 932441 : arg00 = arg0;
7377 : 932441 : arg01 = build_one_cst (type);
7378 : : }
7379 : 8655689 : if (TREE_CODE (arg1) == MULT_EXPR)
7380 : : {
7381 : 2350653 : arg10 = TREE_OPERAND (arg1, 0);
7382 : 2350653 : arg11 = TREE_OPERAND (arg1, 1);
7383 : : }
7384 : 6305036 : else if (TREE_CODE (arg1) == INTEGER_CST)
7385 : : {
7386 : 3369860 : 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 : 6506482 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7390 : 236494 : && negate_expr_p (arg1)
7391 : 3603098 : && code == PLUS_EXPR)
7392 : : {
7393 : 233238 : arg11 = negate_expr (arg1);
7394 : 233238 : code = MINUS_EXPR;
7395 : : }
7396 : : else
7397 : : arg11 = arg1;
7398 : : }
7399 : : else
7400 : : {
7401 : : /* We cannot generate constant 1 for fract. */
7402 : 2935176 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7403 : 0 : return NULL_TREE;
7404 : 2935176 : arg10 = arg1;
7405 : 2935176 : arg11 = build_one_cst (type);
7406 : : }
7407 : 8655689 : same = NULL_TREE;
7408 : :
7409 : : /* Prefer factoring a common non-constant. */
7410 : 8655689 : if (operand_equal_p (arg00, arg10, 0))
7411 : : same = arg00, alt0 = arg01, alt1 = arg11;
7412 : 8651764 : else if (operand_equal_p (arg01, arg11, 0))
7413 : : same = arg01, alt0 = arg00, alt1 = arg10;
7414 : 8556388 : else if (operand_equal_p (arg00, arg11, 0))
7415 : : same = arg00, alt0 = arg01, alt1 = arg10;
7416 : 8556350 : 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 : 8551515 : else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7423 : : {
7424 : 7187200 : HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7425 : 7187200 : HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7426 : 7187200 : HOST_WIDE_INT tmp;
7427 : 7187200 : bool swap = false;
7428 : 7187200 : tree maybe_same;
7429 : :
7430 : : /* Move min of absolute values to int11. */
7431 : 7187200 : 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 : 3651038 : maybe_same = arg11;
7440 : :
7441 : 7187200 : const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7442 : 7187200 : if (factor > 1
7443 : 9478977 : && pow2p_hwi (factor)
7444 : 2086825 : && (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 : 8478219 : && TREE_CODE (arg10) != INTEGER_CST)
7449 : : {
7450 : 1159363 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7451 : 1159363 : build_int_cst (TREE_TYPE (arg00),
7452 : 1159363 : int01 / int11));
7453 : 1159363 : alt1 = arg10;
7454 : 1159363 : same = maybe_same;
7455 : 1159363 : if (swap)
7456 : 1047208 : maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7457 : : }
7458 : : }
7459 : :
7460 : 1263537 : if (!same)
7461 : 7392152 : return NULL_TREE;
7462 : :
7463 : 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7464 : 1263537 : || TYPE_OVERFLOW_WRAPS (type)
7465 : : /* We are neither factoring zero nor minus one. */
7466 : 1391178 : || TREE_CODE (same) == INTEGER_CST)
7467 : 1251976 : 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 : 1251976 : 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 : 11561 : tree utype = unsigned_type_for (type);
7477 : 11561 : 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 : 23122 : if (TREE_CODE (tem) == INTEGER_CST
7483 : 18259 : && (wi::to_wide (tem)
7484 : 18259 : != 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 : 20965224 : native_encode_wide_int (tree type, const wide_int_ref &val,
7501 : : unsigned char *ptr, int len, int off)
7502 : : {
7503 : 20965224 : int total_bytes;
7504 : 20965224 : if (TREE_CODE (type) == BITINT_TYPE)
7505 : : {
7506 : 17084 : struct bitint_info info;
7507 : 17084 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7508 : 17084 : gcc_assert (ok);
7509 : 17084 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7510 : 17084 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7511 : : {
7512 : 16961 : 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 : 16961 : 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 : 41896280 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7526 : 20965224 : int byte, offset, word, words;
7527 : 20965224 : unsigned char value;
7528 : :
7529 : 20965224 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7530 : : return 0;
7531 : 20964736 : if (off == -1)
7532 : 20044539 : off = 0;
7533 : :
7534 : 20964736 : if (ptr == NULL)
7535 : : /* Dry run. */
7536 : 2783086 : return MIN (len, total_bytes - off);
7537 : :
7538 : : words = total_bytes / UNITS_PER_WORD;
7539 : :
7540 : 88900548 : for (byte = 0; byte < total_bytes; byte++)
7541 : : {
7542 : 70718898 : 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 : 70718898 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7546 : :
7547 : 70718898 : if (total_bytes > UNITS_PER_WORD)
7548 : : {
7549 : 70718898 : word = byte / UNITS_PER_WORD;
7550 : 70718898 : if (WORDS_BIG_ENDIAN)
7551 : : word = (words - 1) - word;
7552 : 70718898 : offset = word * UNITS_PER_WORD;
7553 : 70718898 : if (BYTES_BIG_ENDIAN)
7554 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7555 : : else
7556 : 70718898 : offset += byte % UNITS_PER_WORD;
7557 : : }
7558 : : else
7559 : : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7560 : 70718898 : if (offset >= off && offset - off < len)
7561 : 69433777 : ptr[offset - off] = value;
7562 : : }
7563 : 18181650 : 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 : 20965224 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7573 : : {
7574 : 20965224 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7575 : 20965224 : 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 : 569197 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7615 : : unsigned char *ptr, int len, int off)
7616 : : {
7617 : 569197 : int total_bytes = GET_MODE_SIZE (mode);
7618 : 569197 : int byte, offset, word, words, bitpos;
7619 : 569197 : 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 : 569197 : long tmp[6];
7625 : :
7626 : 569197 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7627 : : return 0;
7628 : 567370 : if (off == -1)
7629 : 447194 : off = 0;
7630 : :
7631 : 567370 : if (ptr == NULL)
7632 : : /* Dry run. */
7633 : 139677 : return MIN (len, total_bytes - off);
7634 : :
7635 : 427693 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7636 : :
7637 : 427693 : real_to_target (tmp, val, mode);
7638 : :
7639 : 3460475 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7640 : 3032782 : bitpos += BITS_PER_UNIT)
7641 : : {
7642 : 3032782 : byte = (bitpos / BITS_PER_UNIT) & 3;
7643 : 3032782 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7644 : :
7645 : 3032782 : 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 : 3032782 : offset = byte;
7659 : 3032782 : 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 : 3032782 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7668 : 3032782 : if (offset >= off
7669 : 3029514 : && offset - off < len)
7670 : 3012030 : ptr[offset - off] = value;
7671 : : }
7672 : 427693 : 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 : 15394 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7682 : : {
7683 : 15394 : int rsize, isize;
7684 : 15394 : tree part;
7685 : :
7686 : 15394 : part = TREE_REALPART (expr);
7687 : 15394 : rsize = native_encode_expr (part, ptr, len, off);
7688 : 15394 : if (off == -1 && rsize == 0)
7689 : : return 0;
7690 : 15394 : part = TREE_IMAGPART (expr);
7691 : 15394 : if (off != -1)
7692 : 30466 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7693 : 15394 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7694 : : len - rsize, off);
7695 : 15394 : if (off == -1 && isize != rsize)
7696 : : return 0;
7697 : 15394 : 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 : 886666 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7705 : : int off, unsigned HOST_WIDE_INT count)
7706 : : {
7707 : 886666 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7708 : 1773332 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7709 : 887694 : && 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 : 950 : unsigned int elt_bits = TYPE_PRECISION (itype);
7714 : 950 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7715 : 950 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7716 : : return 0;
7717 : :
7718 : 950 : if (off == -1)
7719 : 950 : off = 0;
7720 : :
7721 : : /* Zero the buffer and then set bits later where necessary. */
7722 : 950 : int extract_bytes = MIN (len, total_bytes - off);
7723 : 950 : if (ptr)
7724 : 950 : memset (ptr, 0, extract_bytes);
7725 : :
7726 : 950 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7727 : 950 : unsigned int first_elt = off * elts_per_byte;
7728 : 950 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7729 : 950 : unsigned int elt_mask = (1 << elt_bits) - 1;
7730 : 17231 : for (unsigned int i = 0; i < extract_elts; ++i)
7731 : : {
7732 : 16281 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7733 : 16281 : if (TREE_CODE (elt) != INTEGER_CST)
7734 : : return 0;
7735 : :
7736 : 16281 : if (ptr && integer_nonzerop (elt))
7737 : : {
7738 : 8221 : unsigned int bit = i * elt_bits;
7739 : 8221 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7740 : : }
7741 : : }
7742 : : return extract_bytes;
7743 : : }
7744 : :
7745 : 885716 : int offset = 0;
7746 : 885716 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7747 : 2909928 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7748 : : {
7749 : 2571752 : if (off >= size)
7750 : : {
7751 : 23196 : off -= size;
7752 : 23196 : continue;
7753 : : }
7754 : 2548556 : tree elem = VECTOR_CST_ELT (expr, i);
7755 : 2548556 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7756 : : len - offset, off);
7757 : 2548556 : if ((off == -1 && res != size) || res == 0)
7758 : : return 0;
7759 : 2548027 : offset += res;
7760 : 2548027 : if (offset >= len)
7761 : 547011 : return (off == -1 && i < count - 1) ? 0 : offset;
7762 : 2001016 : if (off != -1)
7763 : 381065 : 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 : 767129 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7775 : : {
7776 : 767129 : unsigned HOST_WIDE_INT count;
7777 : 767129 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7778 : : return 0;
7779 : 767129 : 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 : 135891 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7790 : : {
7791 : 135891 : tree type = TREE_TYPE (expr);
7792 : :
7793 : : /* Wide-char strings are encoded in target byte-order so native
7794 : : encoding them is trivial. */
7795 : 135891 : if (BITS_PER_UNIT != CHAR_BIT
7796 : 135891 : || TREE_CODE (type) != ARRAY_TYPE
7797 : 135891 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7798 : 271782 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7799 : : return 0;
7800 : :
7801 : 135891 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7802 : 135891 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7803 : : return 0;
7804 : 135033 : if (off == -1)
7805 : 55133 : off = 0;
7806 : 135033 : len = MIN (total_bytes - off, len);
7807 : 135033 : if (ptr == NULL)
7808 : : /* Dry run. */;
7809 : : else
7810 : : {
7811 : 135033 : int written = 0;
7812 : 135033 : if (off < TREE_STRING_LENGTH (expr))
7813 : : {
7814 : 134558 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7815 : 134558 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7816 : : }
7817 : 135033 : 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 : 45434 : 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 : 45434 : 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 : 85020 : if (BITS_PER_UNIT != CHAR_BIT
7840 : 42510 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7841 : : return 0;
7842 : :
7843 : 42510 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7844 : 42510 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7845 : : return 0;
7846 : 42510 : if (off == -1)
7847 : 0 : off = 0;
7848 : 42510 : len = MIN (total_bytes - off, len);
7849 : 42510 : if (ptr == NULL)
7850 : : /* Dry run. */;
7851 : : else
7852 : 42510 : 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 : 35322082 : 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 : 35322082 : if (off < -1)
7869 : : return 0;
7870 : :
7871 : 35322070 : switch (TREE_CODE (expr))
7872 : : {
7873 : 20962985 : case INTEGER_CST:
7874 : 20962985 : return native_encode_int (expr, ptr, len, off);
7875 : :
7876 : 569197 : case REAL_CST:
7877 : 569197 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7878 : 1138394 : 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 : 15394 : case COMPLEX_CST:
7884 : 15394 : return native_encode_complex (expr, ptr, len, off);
7885 : :
7886 : 767129 : case VECTOR_CST:
7887 : 767129 : return native_encode_vector (expr, ptr, len, off);
7888 : :
7889 : 135891 : case STRING_CST:
7890 : 135891 : return native_encode_string (expr, ptr, len, off);
7891 : :
7892 : 45434 : case CONSTRUCTOR:
7893 : 45434 : 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 : 13633941 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7950 : : int off, unsigned char *mask)
7951 : : {
7952 : 13633941 : int r;
7953 : :
7954 : : /* We don't support starting at negative offset and -1 is special. */
7955 : 13633941 : if (off < -1 || init == NULL_TREE)
7956 : : return 0;
7957 : :
7958 : 13633941 : gcc_assert (mask == NULL || (off == 0 && ptr));
7959 : :
7960 : 13633941 : STRIP_NOPS (init);
7961 : 13633941 : 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 : 12748292 : default:
7968 : 12748292 : r = native_encode_expr (init, ptr, len, off);
7969 : 12748292 : if (mask)
7970 : 1514 : memset (mask, 0, r);
7971 : : return r;
7972 : 885649 : case CONSTRUCTOR:
7973 : 885649 : tree type = TREE_TYPE (init);
7974 : 885649 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7975 : 885649 : if (total_bytes < 0)
7976 : : return 0;
7977 : 885649 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7978 : : return 0;
7979 : 885649 : int o = off == -1 ? 0 : off;
7980 : 885649 : if (TREE_CODE (type) == ARRAY_TYPE)
7981 : : {
7982 : 257791 : tree min_index;
7983 : 257791 : unsigned HOST_WIDE_INT cnt;
7984 : 257791 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7985 : 257791 : constructor_elt *ce;
7986 : :
7987 : 257791 : if (!TYPE_DOMAIN (type)
7988 : 257791 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7989 : : return 0;
7990 : :
7991 : 257791 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7992 : 257791 : if (fieldsize <= 0)
7993 : : return 0;
7994 : :
7995 : 257791 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7996 : 257791 : if (ptr)
7997 : 257791 : memset (ptr, '\0', MIN (total_bytes - off, len));
7998 : :
7999 : 12717515 : for (cnt = 0; ; cnt++)
8000 : : {
8001 : 12975306 : tree val = NULL_TREE, index = NULL_TREE;
8002 : 12975306 : HOST_WIDE_INT pos = curpos, count = 0;
8003 : 12975306 : bool full = false;
8004 : 12975306 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8005 : : {
8006 : 12929135 : val = ce->value;
8007 : 12929135 : index = ce->index;
8008 : : }
8009 : 46171 : else if (mask == NULL
8010 : 228 : || CONSTRUCTOR_NO_CLEARING (init)
8011 : 46399 : || curpos >= total_bytes)
8012 : : break;
8013 : : else
8014 : : pos = total_bytes;
8015 : :
8016 : 12929135 : 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 : 12316550 : else if (index)
8038 : : {
8039 : 12316550 : if (TREE_CODE (index) != INTEGER_CST)
8040 : 0 : return 0;
8041 : 12316550 : offset_int wpos
8042 : 12316550 : = wi::sext (wi::to_offset (index)
8043 : 24633100 : - wi::to_offset (min_index),
8044 : 12316550 : TYPE_PRECISION (sizetype));
8045 : 12316550 : wpos *= fieldsize;
8046 : 12316550 : if (!wi::fits_shwi_p (wpos))
8047 : : return 0;
8048 : 12316550 : pos = wpos.to_shwi ();
8049 : : }
8050 : :
8051 : 12929806 : 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 : 12929149 : curpos = pos;
8075 : 12929149 : 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 : 13006689 : do
8119 : : {
8120 : 13006689 : if (off == -1
8121 : 716681 : || (curpos >= off
8122 : 231099 : && (curpos + fieldsize
8123 : 231099 : <= (HOST_WIDE_INT) off + len)))
8124 : : {
8125 : 12491251 : 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 : 25028308 : else if (!native_encode_initializer (val,
8134 : : ptr
8135 : 12413211 : ? 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 : 515438 : else if (curpos + fieldsize > off
8151 : 32268 : && curpos < (HOST_WIDE_INT) off + len)
8152 : : {
8153 : : /* Partial overlap. */
8154 : 9223 : unsigned char *p = NULL;
8155 : 9223 : int no = 0;
8156 : 9223 : int l;
8157 : 9223 : gcc_assert (mask == NULL);
8158 : 9223 : if (curpos >= off)
8159 : : {
8160 : 6811 : if (ptr)
8161 : 6811 : p = ptr + curpos - off;
8162 : 6811 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8163 : : fieldsize);
8164 : : }
8165 : : else
8166 : : {
8167 : 2412 : p = ptr;
8168 : 2412 : no = off - curpos;
8169 : 2412 : l = len;
8170 : : }
8171 : 9223 : if (!native_encode_initializer (val, p, l, no, NULL))
8172 : : return 0;
8173 : : }
8174 : 12795055 : curpos += fieldsize;
8175 : : }
8176 : 12795055 : while (count-- != 0);
8177 : 12717515 : }
8178 : 46157 : return MIN (total_bytes - off, len);
8179 : : }
8180 : 627858 : else if (TREE_CODE (type) == RECORD_TYPE
8181 : 627858 : || TREE_CODE (type) == UNION_TYPE)
8182 : : {
8183 : 627858 : unsigned HOST_WIDE_INT cnt;
8184 : 627858 : constructor_elt *ce;
8185 : 627858 : tree fld_base = TYPE_FIELDS (type);
8186 : 627858 : tree to_free = NULL_TREE;
8187 : :
8188 : 627858 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8189 : 627858 : if (ptr != NULL)
8190 : 627858 : memset (ptr, '\0', MIN (total_bytes - o, len));
8191 : 108731 : for (cnt = 0; ; cnt++)
8192 : : {
8193 : 736589 : tree val = NULL_TREE, field = NULL_TREE;
8194 : 736589 : HOST_WIDE_INT pos = 0, fieldsize;
8195 : 736589 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8196 : :
8197 : 736589 : if (to_free)
8198 : : {
8199 : 0 : ggc_free (to_free);
8200 : 0 : to_free = NULL_TREE;
8201 : : }
8202 : :
8203 : 736589 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8204 : : {
8205 : 132099 : val = ce->value;
8206 : 132099 : field = ce->index;
8207 : 132099 : if (field == NULL_TREE)
8208 : : return 0;
8209 : :
8210 : 132099 : pos = int_byte_position (field);
8211 : 132099 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8212 : 1433 : continue;
8213 : : }
8214 : 604490 : else if (mask == NULL
8215 : 604490 : || CONSTRUCTOR_NO_CLEARING (init))
8216 : : break;
8217 : : else
8218 : : pos = total_bytes;
8219 : :
8220 : 132887 : 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 : 130723 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8257 : 6607 : && TYPE_DOMAIN (TREE_TYPE (field))
8258 : 137330 : && ! 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 : 130642 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8282 : 130642 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8283 : : return 0;
8284 : 130642 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8285 : : }
8286 : 130723 : 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 : 130722 : 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 : 130722 : if (off != -1 && pos + fieldsize <= off)
8312 : 3298 : continue;
8313 : :
8314 : 127424 : if (val == NULL_TREE)
8315 : 0 : continue;
8316 : :
8317 : 127424 : if (DECL_BIT_FIELD (field)
8318 : 127424 : && 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 : 124924 : if (off == -1
8460 : 19748 : || (pos >= off
8461 : 18954 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8462 : : {
8463 : 115463 : int fldsize = fieldsize;
8464 : 10287 : if (off == -1)
8465 : : {
8466 : 105176 : tree fld = DECL_CHAIN (field);
8467 : 1390406 : while (fld)
8468 : : {
8469 : 1295039 : if (TREE_CODE (fld) == FIELD_DECL)
8470 : : break;
8471 : 1285230 : fld = DECL_CHAIN (fld);
8472 : : }
8473 : 105176 : if (fld == NULL_TREE)
8474 : 95367 : fldsize = len - pos;
8475 : : }
8476 : 126586 : 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 : 115463 : if (!r)
8482 : : return 0;
8483 : 100777 : if (off == -1
8484 : 98860 : && fldsize != fieldsize
8485 : 324 : && r > fieldsize
8486 : 54 : && pos + r > total_bytes)
8487 : 108731 : 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 : 108731 : }
8513 : 604433 : 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 : 2782780 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8526 : : {
8527 : 2782780 : int total_bytes;
8528 : 2782780 : 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 : 5565526 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8550 : :
8551 : 2782780 : if (total_bytes > len)
8552 : : return NULL_TREE;
8553 : :
8554 : 2782540 : wide_int result = wi::from_buffer (ptr, total_bytes);
8555 : :
8556 : 2782540 : return wide_int_to_tree (type, result);
8557 : 2782540 : }
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 : 32188 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8589 : : {
8590 : 32188 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8591 : 32188 : int total_bytes = GET_MODE_SIZE (mode);
8592 : 32188 : 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 : 32188 : REAL_VALUE_TYPE r;
8597 : 32188 : long tmp[6];
8598 : :
8599 : 32188 : if (total_bytes > len || total_bytes > 24)
8600 : : return NULL_TREE;
8601 : 32127 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8602 : :
8603 : 32127 : memset (tmp, 0, sizeof (tmp));
8604 : 229795 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8605 : 197668 : bitpos += BITS_PER_UNIT)
8606 : : {
8607 : : /* Both OFFSET and BYTE index within a long;
8608 : : bitpos indexes the whole float. */
8609 : 197668 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8610 : 197668 : 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 : 197668 : offset = byte;
8624 : 197668 : 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 : 197668 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8633 : :
8634 : 197668 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8635 : : }
8636 : :
8637 : 32127 : real_from_target (&r, tmp, mode);
8638 : 32127 : 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 : 1385 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8648 : : {
8649 : 1385 : tree etype, rpart, ipart;
8650 : 1385 : int size;
8651 : :
8652 : 1385 : etype = TREE_TYPE (type);
8653 : 1385 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8654 : 1385 : if (size * 2 > len)
8655 : : return NULL_TREE;
8656 : 1350 : rpart = native_interpret_expr (etype, ptr, size);
8657 : 1350 : if (!rpart)
8658 : : return NULL_TREE;
8659 : 1349 : ipart = native_interpret_expr (etype, ptr+size, size);
8660 : 1349 : if (!ipart)
8661 : : return NULL_TREE;
8662 : 1349 : 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 : 196294 : 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 : 196294 : tree elt_type = TREE_TYPE (type);
8677 : 196294 : if (VECTOR_BOOLEAN_TYPE_P (type)
8678 : 196297 : && 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 : 196293 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8700 : 196293 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8701 : : return NULL_TREE;
8702 : :
8703 : 196293 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8704 : 798357 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8705 : : {
8706 : 602102 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8707 : 602102 : if (!elt)
8708 : 38 : return NULL_TREE;
8709 : 602064 : builder.quick_push (elt);
8710 : 602064 : bytes += elt_bytes;
8711 : : }
8712 : 196255 : return builder.build ();
8713 : 196293 : }
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 : 76759 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8721 : : {
8722 : 76759 : unsigned HOST_WIDE_INT size;
8723 : :
8724 : 76759 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8725 : 76759 : || size > len)
8726 : 2 : return NULL_TREE;
8727 : :
8728 : 76757 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8729 : 76757 : 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 : 2940443 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8741 : : {
8742 : 2940443 : switch (TREE_CODE (type))
8743 : : {
8744 : 2782780 : case INTEGER_TYPE:
8745 : 2782780 : case ENUMERAL_TYPE:
8746 : 2782780 : case BOOLEAN_TYPE:
8747 : 2782780 : case POINTER_TYPE:
8748 : 2782780 : case REFERENCE_TYPE:
8749 : 2782780 : case OFFSET_TYPE:
8750 : 2782780 : case BITINT_TYPE:
8751 : 2782780 : return native_interpret_int (type, ptr, len);
8752 : :
8753 : 30970 : case REAL_TYPE:
8754 : 30970 : 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 : 30909 : unsigned char buf[24 * 2];
8763 : 30909 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8764 : 30909 : int total_bytes = GET_MODE_SIZE (mode);
8765 : 30909 : memcpy (buf + 24, ptr, total_bytes);
8766 : 30909 : clear_type_padding_in_mask (type, buf + 24);
8767 : 30909 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8768 : 30909 : || 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 : 1385 : case COMPLEX_TYPE:
8778 : 1385 : return native_interpret_complex (type, ptr, len);
8779 : :
8780 : 76759 : case VECTOR_TYPE:
8781 : 76759 : 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 : 389040 : can_native_interpret_type_p (tree type)
8793 : : {
8794 : 389040 : 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 : 78009 : default:
8808 : 78009 : 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 : 38889 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8987 : : unsigned int amnt)
8988 : : {
8989 : 38889 : if (amnt == 0)
8990 : : return;
8991 : :
8992 : 22770 : unsigned char carry_over = 0U;
8993 : 22770 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8994 : 22770 : unsigned char clear_mask = (~0U) << amnt;
8995 : :
8996 : 128974 : for (unsigned int i = 0; i < sz; i++)
8997 : : {
8998 : 106204 : unsigned prev_carry_over = carry_over;
8999 : 106204 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9000 : :
9001 : 106204 : ptr[i] <<= amnt;
9002 : 106204 : if (i != 0)
9003 : : {
9004 : 83434 : ptr[i] &= clear_mask;
9005 : 83434 : 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 : 126778 : fold_view_convert_vector_encoding (tree type, tree expr)
9046 : : {
9047 : 126778 : tree expr_type = TREE_TYPE (expr);
9048 : 126778 : poly_uint64 type_bits, expr_bits;
9049 : 126778 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9050 : 126778 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9051 : 0 : return NULL_TREE;
9052 : :
9053 : 126778 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9054 : 126778 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9055 : 126778 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9056 : 126778 : 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 : 126778 : if (VECTOR_CST_STEPPED_P (expr)
9061 : 126778 : && (!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 : 119537 : unsigned int expr_sequence_bits
9067 : 119537 : = 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 : 119537 : unsigned int type_sequence_bits
9072 : 119537 : = 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 : 119537 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9079 : 119537 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9080 : : BITS_PER_UNIT);
9081 : 119537 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9082 : 119537 : if (known_gt (buffer_bits, expr_bits))
9083 : : return NULL_TREE;
9084 : :
9085 : : /* Get enough bytes of EXPR to form the new encoding. */
9086 : 119537 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9087 : 119537 : buffer.quick_grow (buffer_bytes);
9088 : 119537 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9089 : 119537 : buffer_bits / expr_elt_bits)
9090 : : != (int) buffer_bytes)
9091 : : return NULL_TREE;
9092 : :
9093 : : /* Reencode the bytes as TYPE. */
9094 : 119537 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9095 : 239074 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9096 : 119537 : type_npatterns, nelts_per_pattern);
9097 : 119537 : }
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 : 11375597 : fold_view_convert_expr (tree type, tree expr)
9105 : : {
9106 : 11375597 : unsigned char buffer[128];
9107 : 11375597 : unsigned char *buf;
9108 : 11375597 : int len;
9109 : 11375597 : HOST_WIDE_INT l;
9110 : :
9111 : : /* Check that the host and target are sane. */
9112 : 11375597 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9113 : : return NULL_TREE;
9114 : :
9115 : 11375597 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9116 : 126778 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9117 : : return res;
9118 : :
9119 : 11256079 : l = int_size_in_bytes (type);
9120 : 11256079 : if (l > (int) sizeof (buffer)
9121 : 11256079 : && 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 : 11256079 : len = native_encode_expr (expr, buf, len);
9132 : 11256079 : if (len == 0)
9133 : : return NULL_TREE;
9134 : :
9135 : 1690099 : 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 : 435405992 : 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 : 435405992 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9146 : 0 : t = TREE_OPERAND (t, 0);
9147 : :
9148 : 435405992 : if (INDIRECT_REF_P (t))
9149 : : {
9150 : 49718732 : t = TREE_OPERAND (t, 0);
9151 : :
9152 : 49718732 : if (TREE_TYPE (t) != ptrtype)
9153 : 32042040 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9154 : : }
9155 : 385687260 : else if (TREE_CODE (t) == MEM_REF
9156 : 385687260 : && integer_zerop (TREE_OPERAND (t, 1)))
9157 : : {
9158 : 1613000 : t = TREE_OPERAND (t, 0);
9159 : :
9160 : 1613000 : if (TREE_TYPE (t) != ptrtype)
9161 : 1059395 : t = fold_convert_loc (loc, ptrtype, t);
9162 : : }
9163 : 384074260 : else if (TREE_CODE (t) == MEM_REF
9164 : 384074260 : && 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 : 384073599 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9169 : : {
9170 : 26525364 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9171 : :
9172 : 26525364 : if (TREE_TYPE (t) != ptrtype)
9173 : 13947 : t = fold_convert_loc (loc, ptrtype, t);
9174 : : }
9175 : : else
9176 : 357548235 : 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 : 402200886 : build_fold_addr_expr_loc (location_t loc, tree t)
9185 : : {
9186 : 402200886 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9187 : :
9188 : 402200886 : 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 : 1637408529 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9197 : : {
9198 : 1637408529 : tree tem;
9199 : 1637408529 : tree arg0;
9200 : 1637408529 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9201 : :
9202 : 1637408529 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9203 : : && TREE_CODE_LENGTH (code) == 1);
9204 : :
9205 : 1637408529 : arg0 = op0;
9206 : 1637408529 : if (arg0)
9207 : : {
9208 : 1637395363 : 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 : 811882953 : 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 : 825512410 : STRIP_NOPS (arg0);
9229 : : }
9230 : :
9231 : 1637395363 : if (CONSTANT_CLASS_P (arg0))
9232 : : {
9233 : 237412518 : tree tem = const_unop (code, type, arg0);
9234 : 237412518 : if (tem)
9235 : : {
9236 : 201472838 : if (TREE_TYPE (tem) != type)
9237 : 68895 : tem = fold_convert_loc (loc, type, tem);
9238 : 201472838 : return tem;
9239 : : }
9240 : : }
9241 : : }
9242 : :
9243 : 1435935691 : tem = generic_simplify (loc, code, type, op0);
9244 : 1435935691 : if (tem)
9245 : : return tem;
9246 : :
9247 : 1086026223 : if (TREE_CODE_CLASS (code) == tcc_unary)
9248 : : {
9249 : 551529897 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9250 : 1033939 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9251 : : fold_build1_loc (loc, code, type,
9252 : 1033939 : fold_convert_loc (loc, TREE_TYPE (op0),
9253 : 2067878 : TREE_OPERAND (arg0, 1))));
9254 : 550495958 : else if (TREE_CODE (arg0) == COND_EXPR)
9255 : : {
9256 : 427826 : tree arg01 = TREE_OPERAND (arg0, 1);
9257 : 427826 : tree arg02 = TREE_OPERAND (arg0, 2);
9258 : 427826 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9259 : 423644 : arg01 = fold_build1_loc (loc, code, type,
9260 : : fold_convert_loc (loc,
9261 : 423644 : TREE_TYPE (op0), arg01));
9262 : 427826 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9263 : 424466 : arg02 = fold_build1_loc (loc, code, type,
9264 : : fold_convert_loc (loc,
9265 : 424466 : TREE_TYPE (op0), arg02));
9266 : 427826 : 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 : 427826 : if ((CONVERT_EXPR_CODE_P (code)
9279 : 10469 : || code == NON_LVALUE_EXPR)
9280 : 417376 : && TREE_CODE (tem) == COND_EXPR
9281 : 399103 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9282 : 364064 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9283 : 176358 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9284 : 176146 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9285 : 176146 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9286 : 176146 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9287 : 609717 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9288 : 6591 : && (INTEGRAL_TYPE_P
9289 : : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9290 : 6551 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9291 : 6438 : || flag_syntax_only))
9292 : 168902 : tem = build1_loc (loc, code, type,
9293 : : build3 (COND_EXPR,
9294 : 168902 : TREE_TYPE (TREE_OPERAND
9295 : : (TREE_OPERAND (tem, 1), 0)),
9296 : 168902 : TREE_OPERAND (tem, 0),
9297 : 168902 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9298 : 168902 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9299 : : 0)));
9300 : 427826 : return tem;
9301 : : }
9302 : : }
9303 : :
9304 : 1084564458 : switch (code)
9305 : : {
9306 : 40132428 : case NON_LVALUE_EXPR:
9307 : 40132428 : if (!maybe_lvalue_p (op0))
9308 : 29499962 : return fold_convert_loc (loc, type, op0);
9309 : : return NULL_TREE;
9310 : :
9311 : 500694243 : CASE_CONVERT:
9312 : 500694243 : case FLOAT_EXPR:
9313 : 500694243 : case FIX_TRUNC_EXPR:
9314 : 500694243 : 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 : 400179 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9323 : 83261 : return build2_loc (loc, TREE_CODE (op0), type,
9324 : 83261 : TREE_OPERAND (op0, 0),
9325 : 166522 : TREE_OPERAND (op0, 1));
9326 : 316918 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9327 : 7580 : && TREE_CODE (type) != VECTOR_TYPE)
9328 : 7580 : return build3_loc (loc, COND_EXPR, type, op0,
9329 : : constant_boolean_node (true, type),
9330 : 7580 : 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 : 500603402 : if (TREE_CODE (op0) == ADDR_EXPR
9337 : 113490627 : && POINTER_TYPE_P (type)
9338 : 607470224 : && handled_component_p (TREE_OPERAND (op0, 0)))
9339 : : {
9340 : 19631169 : poly_int64 bitsize, bitpos;
9341 : 19631169 : tree offset;
9342 : 19631169 : machine_mode mode;
9343 : 19631169 : int unsignedp, reversep, volatilep;
9344 : 19631169 : tree base
9345 : 19631169 : = 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 : 19631169 : if (!offset
9352 : 19519574 : && known_eq (bitpos, 0)
9353 : 13204089 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9354 : 13204089 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9355 : 19642404 : && 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 : 500592368 : if (TREE_CODE (op0) == MODIFY_EXPR
9361 : 263331 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9362 : : /* Detect assigning a bitfield. */
9363 : 500594369 : && !(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 : 1953 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9370 : : /* First do the assignment, then return converted constant. */
9371 : 1953 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9372 : 1953 : suppress_warning (tem /* What warning? */);
9373 : 1953 : TREE_USED (tem) = 1;
9374 : 1953 : 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 : 500590415 : if (TREE_CODE (type) == INTEGER_TYPE
9384 : 224058902 : && TREE_CODE (op0) == BIT_AND_EXPR
9385 : 501074628 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9386 : : {
9387 : 226036 : tree and_expr = op0;
9388 : 226036 : tree and0 = TREE_OPERAND (and_expr, 0);
9389 : 226036 : tree and1 = TREE_OPERAND (and_expr, 1);
9390 : 226036 : int change = 0;
9391 : :
9392 : 226036 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9393 : 226036 : || (TYPE_PRECISION (type)
9394 : 77907 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9395 : : change = 1;
9396 : 23559 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9397 : : <= HOST_BITS_PER_WIDE_INT
9398 : 23559 : && tree_fits_uhwi_p (and1))
9399 : : {
9400 : 22453 : unsigned HOST_WIDE_INT cst;
9401 : :
9402 : 22453 : cst = tree_to_uhwi (and1);
9403 : 44906 : cst &= HOST_WIDE_INT_M1U
9404 : 22453 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9405 : 22453 : change = (cst == 0);
9406 : 22453 : if (change
9407 : 22453 : && !flag_syntax_only
9408 : 44906 : && (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 : 22453 : if (change)
9417 : : {
9418 : 224930 : tree and1_type = TREE_TYPE (and1);
9419 : 224930 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9420 : : TYPE_PRECISION (type));
9421 : 224930 : tem = force_fit_type (type,
9422 : 224930 : wide_int::from (wi::to_wide (and1), prec,
9423 : 224930 : TYPE_SIGN (and1_type)),
9424 : 224930 : 0, TREE_OVERFLOW (and1));
9425 : 224930 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9426 : 224930 : 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 : 500365485 : if (POINTER_TYPE_P (type)
9434 : 240192149 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9435 : 505575256 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9436 : : {
9437 : 2924867 : tree arg00 = TREE_OPERAND (arg0, 0);
9438 : 2924867 : 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 : 2924867 : if (!in_gimple_form
9444 : 2910409 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9445 : 2926081 : && (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 : 2924800 : if (TREE_CODE (type) == REFERENCE_TYPE
9454 : 1942115 : && !in_gimple_form
9455 : 1942098 : && sanitize_flags_p (SANITIZE_NULL)
9456 : 2925231 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9457 : : return NULL_TREE;
9458 : :
9459 : 2924369 : arg00 = fold_convert_loc (loc, type, arg00);
9460 : 2924369 : 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 : 497440618 : if (INTEGRAL_TYPE_P (type)
9467 : 229808891 : && TREE_CODE (op0) == BIT_NOT_EXPR
9468 : 528664 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9469 : 528664 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9470 : 497766378 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9471 : : {
9472 : 324051 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9473 : 393106 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9474 : 393104 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9475 : 262787 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9476 : 262787 : 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 : 497177831 : if (INTEGRAL_TYPE_P (type)
9482 : 229546104 : && TREE_CODE (op0) == MULT_EXPR
9483 : 8015086 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9484 : 7993645 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9485 : 497238296 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9486 : 13911 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9487 : : {
9488 : : /* Be careful not to introduce new overflows. */
9489 : 60425 : tree mult_type;
9490 : 60425 : if (TYPE_OVERFLOW_WRAPS (type))
9491 : : mult_type = type;
9492 : : else
9493 : 2040 : mult_type = unsigned_type_for (type);
9494 : :
9495 : 60425 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9496 : : {
9497 : 120850 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9498 : : fold_convert_loc (loc, mult_type,
9499 : 60425 : TREE_OPERAND (op0, 0)),
9500 : : fold_convert_loc (loc, mult_type,
9501 : 60425 : TREE_OPERAND (op0, 1)));
9502 : 60425 : return fold_convert_loc (loc, type, tem);
9503 : : }
9504 : : }
9505 : :
9506 : : return NULL_TREE;
9507 : :
9508 : 229585110 : case VIEW_CONVERT_EXPR:
9509 : 229585110 : if (TREE_CODE (op0) == MEM_REF)
9510 : : {
9511 : 2545 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9512 : 5 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9513 : 2545 : tem = fold_build2_loc (loc, MEM_REF, type,
9514 : 2545 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9515 : 2545 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9516 : 2545 : return tem;
9517 : : }
9518 : :
9519 : : return NULL_TREE;
9520 : :
9521 : 3772754 : case NEGATE_EXPR:
9522 : 3772754 : tem = fold_negate_expr (loc, arg0);
9523 : 3772754 : if (tem)
9524 : 1552 : return fold_convert_loc (loc, type, tem);
9525 : : return NULL_TREE;
9526 : :
9527 : 2732778 : case ABS_EXPR:
9528 : : /* Convert fabs((double)float) into (double)fabsf(float). */
9529 : 2732778 : if (TREE_CODE (arg0) == NOP_EXPR
9530 : 23531 : && TREE_CODE (type) == REAL_TYPE)
9531 : : {
9532 : 23475 : tree targ0 = strip_float_extensions (arg0);
9533 : 23475 : if (targ0 != arg0)
9534 : 23271 : return fold_convert_loc (loc, type,
9535 : : fold_build1_loc (loc, ABS_EXPR,
9536 : 23271 : TREE_TYPE (targ0),
9537 : 23271 : targ0));
9538 : : }
9539 : : return NULL_TREE;
9540 : :
9541 : 2730208 : case BIT_NOT_EXPR:
9542 : : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9543 : 2730208 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9544 : 2731893 : && (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 : 2730194 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9551 : 2731865 : && (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 : 47913658 : 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 : 47913658 : tem = fold_truth_not_expr (loc, arg0);
9566 : 47913658 : if (!tem)
9567 : : return NULL_TREE;
9568 : 33478793 : return fold_convert_loc (loc, type, tem);
9569 : :
9570 : 59556947 : case INDIRECT_REF:
9571 : : /* Fold *&X to X if X is an lvalue. */
9572 : 59556947 : if (TREE_CODE (op0) == ADDR_EXPR)
9573 : : {
9574 : 6550 : tree op00 = TREE_OPERAND (op0, 0);
9575 : 6550 : if ((VAR_P (op00)
9576 : : || TREE_CODE (op00) == PARM_DECL
9577 : : || TREE_CODE (op00) == RESULT_DECL)
9578 : 5383 : && !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 : 24146249 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9614 : : tree arg0, tree arg1, tree op0, tree op1)
9615 : : {
9616 : 24146249 : tree tem;
9617 : :
9618 : : /* We only do these simplifications if we are optimizing. */
9619 : 24146249 : 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 : 23868535 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9629 : 5304847 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9630 : : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9631 : : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9632 : 5304847 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9633 : 23898295 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9634 : : {
9635 : 29233 : tree a00 = TREE_OPERAND (arg0, 0);
9636 : 29233 : tree a01 = TREE_OPERAND (arg0, 1);
9637 : 29233 : tree a10 = TREE_OPERAND (arg1, 0);
9638 : 29233 : tree a11 = TREE_OPERAND (arg1, 1);
9639 : 58466 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9640 : 29233 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9641 : 29233 : && (code == TRUTH_AND_EXPR
9642 : 9242 : || code == TRUTH_OR_EXPR));
9643 : :
9644 : 29233 : 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 : 28384 : 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 : 28384 : 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 : 28340 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9658 : 56196 : && 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 : 23867643 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9666 : : return tem;
9667 : :
9668 : 22806970 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9669 : 22804992 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9670 : : {
9671 : 21068 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9672 : 21068 : if (tem)
9673 : 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9674 : : }
9675 : :
9676 : 22806957 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9677 : 22794930 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9678 : : {
9679 : 122992 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9680 : 122992 : 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 : 22806866 : if (TREE_CODE (arg0) == code
9688 : 23538853 : && (tem = fold_truth_andor_1 (loc, code, type,
9689 : 731987 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9690 : 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9691 : :
9692 : 22806781 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9693 : : return tem;
9694 : :
9695 : 22766416 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9696 : 22766416 : if (param_logical_op_non_short_circuit != -1)
9697 : 7698 : logical_op_non_short_circuit
9698 : 7698 : = param_logical_op_non_short_circuit;
9699 : 22766416 : if (logical_op_non_short_circuit
9700 : 22762553 : && !sanitize_coverage_p ()
9701 : 22766416 : && (code == TRUTH_AND_EXPR
9702 : 22762550 : || code == TRUTH_ANDIF_EXPR
9703 : 10833886 : || code == TRUTH_OR_EXPR
9704 : 10833886 : || code == TRUTH_ORIF_EXPR))
9705 : : {
9706 : 22762550 : enum tree_code ncode, icode;
9707 : :
9708 : 22762550 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9709 : 22762550 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9710 : 11928664 : 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 : 22762550 : if (TREE_CODE (arg0) == icode
9722 : 723455 : && simple_condition_p (arg1)
9723 : : /* Needed for sequence points to handle trappings, and
9724 : : side-effects. */
9725 : 22811351 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9726 : : {
9727 : 41927 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9728 : : arg1);
9729 : 41927 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9730 : 41927 : 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 : 22720623 : 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 : 22721576 : && 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 : 4491950 : else if (code == icode && simple_condition_p (arg0)
9750 : 23423922 : && simple_condition_p (arg1))
9751 : 370613 : 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 : 167882056 : 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 : 167882056 : enum tree_code code0 = TREE_CODE (arg0);
9771 : 167882056 : tree t, cst0 = NULL_TREE;
9772 : 167882056 : int sgn0;
9773 : :
9774 : : /* Match A +- CST code arg1. We can change this only if overflow
9775 : : is undefined. */
9776 : 167882056 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9777 : 127646077 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9778 : : /* In principle pointers also have undefined overflow behavior,
9779 : : but that causes problems elsewhere. */
9780 : 62313565 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9781 : 62313565 : && (code0 == MINUS_EXPR
9782 : 62313565 : || code0 == PLUS_EXPR)
9783 : 2541945 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9784 : : return NULL_TREE;
9785 : :
9786 : : /* Identify the constant in arg0 and its sign. */
9787 : 2084312 : cst0 = TREE_OPERAND (arg0, 1);
9788 : 2084312 : sgn0 = tree_int_cst_sgn (cst0);
9789 : :
9790 : : /* Overflowed constants and zero will cause problems. */
9791 : 2084312 : if (integer_zerop (cst0)
9792 : 2084312 : || 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 : 2084312 : if (code == LT_EXPR
9799 : 1214136 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9800 : : code = LE_EXPR;
9801 : : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9802 : 1905019 : else if (code == GT_EXPR
9803 : 563488 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9804 : : code = GE_EXPR;
9805 : : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9806 : 1728756 : else if (code == LE_EXPR
9807 : 637776 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9808 : : code = LT_EXPR;
9809 : : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9810 : 1510188 : else if (code == GE_EXPR
9811 : 468156 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9812 : : code = GT_EXPR;
9813 : : else
9814 : : return NULL_TREE;
9815 : 775490 : *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 : 1550980 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9820 : 1550980 : && ((sgn0 == 1
9821 : 394693 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9822 : 394693 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9823 : 775490 : || (sgn0 == -1
9824 : 380797 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9825 : 380797 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9826 : 0 : return NULL_TREE;
9827 : :
9828 : 1170183 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9829 : 775490 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9830 : 775490 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9831 : 775490 : t = fold_convert (TREE_TYPE (arg1), t);
9832 : :
9833 : 775490 : 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 : 84308904 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9844 : : tree arg0, tree arg1)
9845 : : {
9846 : 84308904 : tree t;
9847 : 84308904 : bool strict_overflow_p;
9848 : 84308904 : 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 : 84308904 : strict_overflow_p = false;
9853 : 84308904 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9854 : : &strict_overflow_p);
9855 : 84308904 : if (t)
9856 : : {
9857 : 735752 : if (strict_overflow_p)
9858 : 735752 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9859 : 735752 : return t;
9860 : : }
9861 : :
9862 : : /* Try canonicalization by simplifying arg1 using the swapped
9863 : : comparison. */
9864 : 83573152 : code = swap_tree_comparison (code);
9865 : 83573152 : strict_overflow_p = false;
9866 : 83573152 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9867 : : &strict_overflow_p);
9868 : 83573152 : if (t && strict_overflow_p)
9869 : 39738 : 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 : 19108 : pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9879 : : {
9880 : 19108 : if (!POINTER_TYPE_P (TREE_TYPE (base)))
9881 : : return true;
9882 : :
9883 : 10623 : if (maybe_lt (bitpos, 0))
9884 : : return true;
9885 : :
9886 : : poly_wide_int wi_offset;
9887 : 9549 : int precision = TYPE_PRECISION (TREE_TYPE (base));
9888 : 9549 : if (offset == NULL_TREE)
9889 : 4680 : wi_offset = wi::zero (precision);
9890 : 4869 : 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 : 4680 : wi::overflow_type overflow;
9896 : 4680 : poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9897 : 4680 : precision);
9898 : 4680 : poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9899 : 4680 : if (overflow)
9900 : : return true;
9901 : :
9902 : 4680 : poly_uint64 total_hwi, size;
9903 : 4680 : if (!total.to_uhwi (&total_hwi)
9904 : 4680 : || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9905 : : &size)
9906 : 9266 : || known_eq (size, 0U))
9907 : 94 : return true;
9908 : :
9909 : 4586 : 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 : 1154 : 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 : 1154 : && known_le (total_hwi, size))
9919 : : return false;
9920 : :
9921 : : return true;
9922 : 9549 : }
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 : 11805501 : maybe_nonzero_address (tree decl)
9931 : : {
9932 : 11805501 : 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 : 9477562 : if (decl_in_symtab_p (decl))
9940 : : {
9941 : 4124288 : if (struct symtab_node *symbol
9942 : 4124288 : = (folding_initializer
9943 : 4124288 : ? symtab_node::get_create (decl)
9944 : 4107753 : : symtab_node::get (decl)))
9945 : 4105493 : return symbol->nonzero_address ();
9946 : : }
9947 : 5353274 : 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 : 5310806 : if (DECL_CONTEXT (decl)
9953 : 5294820 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9954 : 10602299 : && 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 : 84381049 : fold_comparison (location_t loc, enum tree_code code, tree type,
9970 : : tree op0, tree op1)
9971 : : {
9972 : 84381049 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9973 : 84381049 : tree arg0, arg1, tem;
9974 : :
9975 : 84381049 : arg0 = op0;
9976 : 84381049 : arg1 = op1;
9977 : :
9978 : 84381049 : STRIP_SIGN_NOPS (arg0);
9979 : 84381049 : 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 : 156386050 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9986 : 84615653 : && (TREE_CODE (arg0) == ADDR_EXPR
9987 : 12563471 : || TREE_CODE (arg1) == ADDR_EXPR
9988 : 11038996 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9989 : 10344476 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9990 : : {
9991 : 2275911 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9992 : 2275911 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9993 : 2275911 : machine_mode mode;
9994 : 2275911 : int volatilep, reversep, unsignedp;
9995 : 2275911 : 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 : 2275911 : base0 = arg0;
10002 : 2275911 : if (TREE_CODE (arg0) == ADDR_EXPR)
10003 : : {
10004 : 47181 : base0
10005 : 47181 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10006 : : &bitsize, &bitpos0, &offset0, &mode,
10007 : : &unsignedp, &reversep, &volatilep);
10008 : 47181 : if (INDIRECT_REF_P (base0))
10009 : 1830 : base0 = TREE_OPERAND (base0, 0);
10010 : : else
10011 : : indirect_base0 = true;
10012 : : }
10013 : 2228730 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10014 : : {
10015 : 764474 : base0 = TREE_OPERAND (arg0, 0);
10016 : 764474 : STRIP_SIGN_NOPS (base0);
10017 : 764474 : if (TREE_CODE (base0) == ADDR_EXPR)
10018 : : {
10019 : 46438 : base0
10020 : 46438 : = get_inner_reference (TREE_OPERAND (base0, 0),
10021 : : &bitsize, &bitpos0, &offset0, &mode,
10022 : : &unsignedp, &reversep, &volatilep);
10023 : 46438 : if (INDIRECT_REF_P (base0))
10024 : 14 : base0 = TREE_OPERAND (base0, 0);
10025 : : else
10026 : : indirect_base0 = true;
10027 : : }
10028 : 764474 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10029 : 764474 : offset0 = TREE_OPERAND (arg0, 1);
10030 : : else
10031 : 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10032 : : TREE_OPERAND (arg0, 1));
10033 : 764474 : if (poly_int_tree_p (offset0))
10034 : : {
10035 : 647215 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10036 : 647215 : TYPE_PRECISION (sizetype));
10037 : 647215 : tem <<= LOG2_BITS_PER_UNIT;
10038 : 647215 : tem += bitpos0;
10039 : 647215 : if (tem.to_shwi (&bitpos0))
10040 : 647212 : offset0 = NULL_TREE;
10041 : : }
10042 : : }
10043 : :
10044 : 2275911 : base1 = arg1;
10045 : 2275911 : if (TREE_CODE (arg1) == ADDR_EXPR)
10046 : : {
10047 : 1549395 : base1
10048 : 1549395 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10049 : : &bitsize, &bitpos1, &offset1, &mode,
10050 : : &unsignedp, &reversep, &volatilep);
10051 : 1549395 : if (INDIRECT_REF_P (base1))
10052 : 73561 : base1 = TREE_OPERAND (base1, 0);
10053 : : else
10054 : : indirect_base1 = true;
10055 : : }
10056 : 726516 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10057 : : {
10058 : 77945 : base1 = TREE_OPERAND (arg1, 0);
10059 : 77945 : STRIP_SIGN_NOPS (base1);
10060 : 77945 : if (TREE_CODE (base1) == ADDR_EXPR)
10061 : : {
10062 : 11374 : base1
10063 : 11374 : = get_inner_reference (TREE_OPERAND (base1, 0),
10064 : : &bitsize, &bitpos1, &offset1, &mode,
10065 : : &unsignedp, &reversep, &volatilep);
10066 : 11374 : if (INDIRECT_REF_P (base1))
10067 : 0 : base1 = TREE_OPERAND (base1, 0);
10068 : : else
10069 : : indirect_base1 = true;
10070 : : }
10071 : 77945 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10072 : 77921 : offset1 = TREE_OPERAND (arg1, 1);
10073 : : else
10074 : 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10075 : : TREE_OPERAND (arg1, 1));
10076 : 77945 : if (poly_int_tree_p (offset1))
10077 : : {
10078 : 65854 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10079 : 65854 : TYPE_PRECISION (sizetype));
10080 : 65854 : tem <<= LOG2_BITS_PER_UNIT;
10081 : 65854 : tem += bitpos1;
10082 : 65854 : if (tem.to_shwi (&bitpos1))
10083 : 65854 : offset1 = NULL_TREE;
10084 : : }
10085 : : }
10086 : :
10087 : : /* If we have equivalent bases we might be able to simplify. */
10088 : 2275911 : if (indirect_base0 == indirect_base1
10089 : 3009486 : && 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 : 22868 : if ((offset0 == offset1
10095 : 6937 : || (offset0 && offset1
10096 : 2525 : && operand_equal_p (offset0, offset1, 0)))
10097 : 22868 : && (equality_code
10098 : 12078 : || (indirect_base0
10099 : 8220 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10100 : 3858 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10101 : : {
10102 : 15893 : if (!equality_code
10103 : 12040 : && maybe_ne (bitpos0, bitpos1)
10104 : 27906 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10105 : 1934 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10106 : 10515 : 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 : 15893 : switch (code)
10112 : : {
10113 : 56 : case EQ_EXPR:
10114 : 56 : if (known_eq (bitpos0, bitpos1))
10115 : 50506 : return constant_boolean_node (true, type);
10116 : 21 : if (known_ne (bitpos0, bitpos1))
10117 : 21 : return constant_boolean_node (false, type);
10118 : : break;
10119 : 3797 : case NE_EXPR:
10120 : 3797 : if (known_ne (bitpos0, bitpos1))
10121 : 3792 : return constant_boolean_node (true, type);
10122 : 5 : if (known_eq (bitpos0, bitpos1))
10123 : 5 : return constant_boolean_node (false, type);
10124 : : break;
10125 : 2722 : case LT_EXPR:
10126 : 2722 : if (known_lt (bitpos0, bitpos1))
10127 : 2574 : return constant_boolean_node (true, type);
10128 : 148 : if (known_ge (bitpos0, bitpos1))
10129 : 148 : return constant_boolean_node (false, type);
10130 : : break;
10131 : 2465 : case LE_EXPR:
10132 : 2465 : if (known_le (bitpos0, bitpos1))
10133 : 289 : return constant_boolean_node (true, type);
10134 : 2176 : if (known_gt (bitpos0, bitpos1))
10135 : 2176 : return constant_boolean_node (false, type);
10136 : : break;
10137 : 4986 : case GE_EXPR:
10138 : 4986 : if (known_ge (bitpos0, bitpos1))
10139 : 2217 : return constant_boolean_node (true, type);
10140 : 2769 : if (known_lt (bitpos0, bitpos1))
10141 : 2769 : return constant_boolean_node (false, type);
10142 : : break;
10143 : 1867 : case GT_EXPR:
10144 : 1867 : if (known_gt (bitpos0, bitpos1))
10145 : 1802 : 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 : 6975 : else if (known_eq (bitpos0, bitpos1)
10160 : 6975 : && (equality_code
10161 : 5161 : || (indirect_base0
10162 : 292 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10163 : 4869 : || 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 : 5271 : if (offset0 == NULL_TREE)
10171 : 0 : offset0 = build_int_cst (ssizetype, 0);
10172 : : else
10173 : 5271 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10174 : 5271 : if (offset1 == NULL_TREE)
10175 : 2778 : offset1 = build_int_cst (ssizetype, 0);
10176 : : else
10177 : 2493 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10178 : :
10179 : 5271 : if (!equality_code
10180 : 5271 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10181 : 0 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10182 : 5161 : 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 : 5271 : 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 : 2253043 : else if (known_eq (bitpos0, bitpos1)
10193 : 49808 : && (indirect_base0
10194 : 932418 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10195 : 11012 : && (indirect_base1
10196 : 136455 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10197 : 2424509 : && ((offset0 == offset1)
10198 : 6054 : || (offset0 && offset1
10199 : 5838 : && operand_equal_p (offset0, offset1, 0))))
10200 : : {
10201 : 29279 : if (indirect_base0)
10202 : 1126 : base0 = build_fold_addr_expr_loc (loc, base0);
10203 : 29279 : if (indirect_base1)
10204 : 2405 : base1 = build_fold_addr_expr_loc (loc, base1);
10205 : 29279 : 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 : 2223764 : else if (((DECL_P (base0)
10215 : 209909 : && 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 : 197010 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10222 : 2193801 : || CONSTANT_CLASS_P (base0))
10223 : 34831 : && 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 : 2255903 : && 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 : 165687597 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10249 : 128425114 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10250 : 31546300 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10251 : 2291692 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10252 : 1887633 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10253 : 1887633 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10254 : 148709321 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10255 : 163843 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10256 : : {
10257 : 163843 : tree const1 = TREE_OPERAND (arg0, 1);
10258 : 163843 : tree const2 = TREE_OPERAND (arg1, 1);
10259 : 163843 : tree variable1 = TREE_OPERAND (arg0, 0);
10260 : 163843 : tree variable2 = TREE_OPERAND (arg1, 0);
10261 : 163843 : tree cst;
10262 : 163843 : 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 : 163844 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10269 : : ? MINUS_EXPR : PLUS_EXPR,
10270 : : const2, const1);
10271 : 163843 : if (!TREE_OVERFLOW (cst)
10272 : 163827 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10273 : 185482 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10274 : : {
10275 : 5576 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10276 : 5576 : return fold_build2_loc (loc, code, type,
10277 : : variable1,
10278 : 5576 : fold_build2_loc (loc, TREE_CODE (arg1),
10279 : 5576 : TREE_TYPE (arg1),
10280 : 5576 : variable2, cst));
10281 : : }
10282 : :
10283 : 158268 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10284 : : ? MINUS_EXPR : PLUS_EXPR,
10285 : : const1, const2);
10286 : 158267 : if (!TREE_OVERFLOW (cst)
10287 : 158251 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10288 : 174330 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10289 : : {
10290 : 16063 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10291 : 16063 : return fold_build2_loc (loc, code, type,
10292 : 16063 : fold_build2_loc (loc, TREE_CODE (arg0),
10293 : 16063 : TREE_TYPE (arg0),
10294 : : variable1, cst),
10295 : 16063 : variable2);
10296 : : }
10297 : : }
10298 : :
10299 : 84308904 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10300 : 84308904 : 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 : 83533414 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10317 : : {
10318 : 50584494 : tree cval1 = 0, cval2 = 0;
10319 : :
10320 : 50584494 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10321 : : /* Don't handle degenerate cases here; they should already
10322 : : have been handled anyway. */
10323 : 601392 : && cval1 != 0 && cval2 != 0
10324 : 600208 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10325 : 600208 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10326 : 600202 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10327 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10328 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10329 : 50584552 : && ! 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 : 11740 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10445 : : {
10446 : 11740 : unsigned HOST_WIDE_INT i, nunits;
10447 : :
10448 : 11740 : if (TREE_CODE (arg) == VECTOR_CST
10449 : 11740 : && 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 : 11347 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10455 : : {
10456 : : constructor_elt *elt;
10457 : :
10458 : 36251 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10459 : 30301 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10460 : 5397 : return false;
10461 : : else
10462 : 24904 : elts[i] = elt->value;
10463 : : }
10464 : : else
10465 : : return false;
10466 : 7117 : 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 : 7831 : 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 : 7831 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10483 : 7831 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10484 : :
10485 : 15662 : if (!(pow2p_hwi (sel_npatterns)
10486 : 7831 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10487 : 7831 : && 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 : 7831 : poly_uint64 esel;
10497 : 7831 : 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 : 7831 : if (sel_nelts_per_pattern < 3)
10505 : : return true;
10506 : :
10507 : 5551 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10508 : : {
10509 : 4200 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10510 : 4200 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10511 : 4200 : HOST_WIDE_INT step;
10512 : 4200 : if (!poly_int64 (a2 - a1).is_constant (&step))
10513 : : {
10514 : : if (reason)
10515 : : *reason = "step is not constant";
10516 : 991 : return false;
10517 : : }
10518 : : // FIXME: Punt on step < 0 for now, revisit later.
10519 : 4200 : 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 : 7831 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10595 : : const char **reason = NULL)
10596 : : {
10597 : 7831 : unsigned res_npatterns, res_nelts_per_pattern;
10598 : 7831 : 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 : 7831 : 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 : 991 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10635 : : {
10636 : 991 : res_npatterns = res_nelts;
10637 : 991 : res_nelts_per_pattern = 1;
10638 : : }
10639 : : else
10640 : : return NULL_TREE;
10641 : :
10642 : 7831 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10643 : 49350 : for (unsigned i = 0; i < res_nelts; i++)
10644 : : {
10645 : 41519 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10646 : 41519 : uint64_t q;
10647 : 41519 : poly_uint64 r;
10648 : 41519 : 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 : 41519 : 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 : 41519 : if (!r.is_constant (&index))
10668 : : {
10669 : : if (reason)
10670 : : *reason = "remainder is not constant";
10671 : : return NULL_TREE;
10672 : : }
10673 : :
10674 : 41519 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10675 : 41519 : tree elem = vector_cst_elt (arg, index);
10676 : 41519 : out_elts.quick_push (elem);
10677 : : }
10678 : :
10679 : 7831 : return out_elts.build ();
10680 : 7831 : }
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 : 26695 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10688 : : {
10689 : 26695 : unsigned int i;
10690 : 26695 : unsigned HOST_WIDE_INT nelts;
10691 : :
10692 : 26695 : 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 : 26695 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10697 : 26695 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10698 : : return NULL_TREE;
10699 : :
10700 : 16390 : if (TREE_CODE (arg0) == VECTOR_CST
10701 : 8088 : && TREE_CODE (arg1) == VECTOR_CST)
10702 : 7831 : 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 : 8559 : if (!sel.length ().is_constant (&nelts))
10707 : : return NULL_TREE;
10708 : :
10709 : 8559 : gcc_assert (known_eq (sel.length (),
10710 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))));
10711 : 8559 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10712 : 8559 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10713 : 8559 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10714 : 5397 : return NULL_TREE;
10715 : :
10716 : 3162 : vec<constructor_elt, va_gc> *v;
10717 : 3162 : vec_alloc (v, nelts);
10718 : 16676 : for (i = 0; i < nelts; i++)
10719 : : {
10720 : 13514 : HOST_WIDE_INT index;
10721 : 13514 : if (!sel[i].is_constant (&index))
10722 : : return NULL_TREE;
10723 : 13514 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10724 : : }
10725 : 3162 : 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 : 1214660 : exact_inverse (tree type, tree cst)
10780 : : {
10781 : 1214660 : REAL_VALUE_TYPE r;
10782 : 1214660 : tree unit_type;
10783 : 1214660 : machine_mode mode;
10784 : :
10785 : 1214660 : switch (TREE_CODE (cst))
10786 : : {
10787 : 1214147 : case REAL_CST:
10788 : 1214147 : r = TREE_REAL_CST (cst);
10789 : :
10790 : 1214147 : if (exact_real_inverse (TYPE_MODE (type), &r))
10791 : 345283 : 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 : 106487 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10824 : : {
10825 : 106487 : int tz = wi::ctz (y);
10826 : 106487 : if (tz > 0)
10827 : 6282 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10828 : 100205 : 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 : 145437167 : tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10841 : : {
10842 : 145757669 : tree type = TREE_TYPE (t);
10843 : 145757669 : enum tree_code code;
10844 : :
10845 : : /* Doing something useful for floating point would need more work. */
10846 : 145757669 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10847 : : return false;
10848 : :
10849 : 145638410 : code = TREE_CODE (t);
10850 : 145638410 : switch (TREE_CODE_CLASS (code))
10851 : : {
10852 : 836472 : case tcc_unary:
10853 : 836472 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10854 : 836472 : strict_overflow_p);
10855 : 2891909 : case tcc_binary:
10856 : 2891909 : case tcc_comparison:
10857 : 2891909 : return tree_binary_nonzero_warnv_p (code, type,
10858 : 2891909 : TREE_OPERAND (t, 0),
10859 : 2891909 : TREE_OPERAND (t, 1),
10860 : 2891909 : strict_overflow_p);
10861 : 11782203 : case tcc_constant:
10862 : 11782203 : case tcc_declaration:
10863 : 11782203 : case tcc_reference:
10864 : 11782203 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10865 : :
10866 : 130127826 : default:
10867 : 130127826 : break;
10868 : : }
10869 : :
10870 : 130127826 : switch (code)
10871 : : {
10872 : 599761 : case TRUTH_NOT_EXPR:
10873 : 599761 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10874 : 599761 : strict_overflow_p);
10875 : :
10876 : 78428 : case TRUTH_AND_EXPR:
10877 : 78428 : case TRUTH_OR_EXPR:
10878 : 78428 : case TRUTH_XOR_EXPR:
10879 : 78428 : return tree_binary_nonzero_warnv_p (code, type,
10880 : 78428 : TREE_OPERAND (t, 0),
10881 : 78428 : TREE_OPERAND (t, 1),
10882 : 78428 : strict_overflow_p);
10883 : :
10884 : 126471412 : case COND_EXPR:
10885 : 126471412 : case CONSTRUCTOR:
10886 : 126471412 : case OBJ_TYPE_REF:
10887 : 126471412 : case ADDR_EXPR:
10888 : 126471412 : case WITH_SIZE_EXPR:
10889 : 126471412 : case SSA_NAME:
10890 : 126471412 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10891 : :
10892 : 85930 : case COMPOUND_EXPR:
10893 : 85930 : case MODIFY_EXPR:
10894 : 85930 : case BIND_EXPR:
10895 : 85930 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10896 : 85930 : strict_overflow_p);
10897 : :
10898 : 234572 : case SAVE_EXPR:
10899 : 234572 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10900 : 234572 : strict_overflow_p);
10901 : :
10902 : 2603828 : case CALL_EXPR:
10903 : 2603828 : {
10904 : 2603828 : tree fndecl = get_callee_fndecl (t);
10905 : 2603828 : if (!fndecl) return false;
10906 : 2602095 : if (flag_delete_null_pointer_checks && !flag_check_new
10907 : 2602095 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10908 : 2602773 : && !TREE_NOTHROW (fndecl))
10909 : : return true;
10910 : 2602773 : if (flag_delete_null_pointer_checks
10911 : 5204868 : && lookup_attribute ("returns_nonnull",
10912 : 2602095 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10913 : : return true;
10914 : 2602765 : 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 : 144335849 : tree_expr_nonzero_p (tree t)
10928 : : {
10929 : 144335849 : bool ret, strict_overflow_p;
10930 : :
10931 : 144335849 : strict_overflow_p = false;
10932 : 144335849 : ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10933 : 144335849 : 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 : 144335849 : return ret;
10939 : : }
10940 : :
10941 : : /* Return true if T is known not to be equal to an integer W. */
10942 : :
10943 : : bool
10944 : 99443199 : expr_not_equal_to (tree t, const wide_int &w)
10945 : : {
10946 : 99443199 : int_range_max vr;
10947 : 99443199 : switch (TREE_CODE (t))
10948 : : {
10949 : 1076127 : case INTEGER_CST:
10950 : 1076127 : return wi::to_wide (t) != w;
10951 : :
10952 : 98365977 : case SSA_NAME:
10953 : 98365977 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10954 : : return false;
10955 : :
10956 : 196731954 : get_range_query (cfun)->range_of_expr (vr, t);
10957 : 98365977 : if (!vr.undefined_p () && !vr.contains_p (w))
10958 : : return true;
10959 : : /* If T has some known zero bits and W has any of those bits set,
10960 : : then T is known not to be equal to W. */
10961 : 98238298 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10962 : 196476220 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10963 : : return true;
10964 : : return false;
10965 : :
10966 : : default:
10967 : : return false;
10968 : : }
10969 : 99443199 : }
10970 : :
10971 : : /* Fold a binary expression of code CODE and type TYPE with operands
10972 : : OP0 and OP1. LOC is the location of the resulting expression.
10973 : : Return the folded expression if folding is successful. Otherwise,
10974 : : return NULL_TREE. */
10975 : :
10976 : : tree
10977 : 793178651 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10978 : : tree op0, tree op1)
10979 : : {
10980 : 793178651 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10981 : 793178651 : tree arg0, arg1, tem;
10982 : 793178651 : tree t1 = NULL_TREE;
10983 : 793178651 : bool strict_overflow_p;
10984 : 793178651 : unsigned int prec;
10985 : :
10986 : 793178651 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10987 : : && TREE_CODE_LENGTH (code) == 2
10988 : : && op0 != NULL_TREE
10989 : : && op1 != NULL_TREE);
10990 : :
10991 : 793178651 : arg0 = op0;
10992 : 793178651 : arg1 = op1;
10993 : :
10994 : : /* Strip any conversions that don't change the mode. This is
10995 : : safe for every expression, except for a comparison expression
10996 : : because its signedness is derived from its operands. So, in
10997 : : the latter case, only strip conversions that don't change the
10998 : : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10999 : : preserved.
11000 : :
11001 : : Note that this is done as an internal manipulation within the
11002 : : constant folder, in order to find the simplest representation
11003 : : of the arguments so that their form can be studied. In any
11004 : : cases, the appropriate type conversions should be put back in
11005 : : the tree that will get out of the constant folder. */
11006 : :
11007 : 793178651 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
11008 : : {
11009 : 175429463 : STRIP_SIGN_NOPS (arg0);
11010 : 175429463 : STRIP_SIGN_NOPS (arg1);
11011 : : }
11012 : : else
11013 : : {
11014 : 617749188 : STRIP_NOPS (arg0);
11015 : 617749188 : STRIP_NOPS (arg1);
11016 : : }
11017 : :
11018 : : /* Note that TREE_CONSTANT isn't enough: static var addresses are
11019 : : constant but we can't do arithmetic on them. */
11020 : 793178651 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11021 : : {
11022 : 198524239 : tem = const_binop (code, type, arg0, arg1);
11023 : 198524239 : if (tem != NULL_TREE)
11024 : : {
11025 : 195828040 : if (TREE_TYPE (tem) != type)
11026 : 1758593 : tem = fold_convert_loc (loc, type, tem);
11027 : 195828040 : return tem;
11028 : : }
11029 : : }
11030 : :
11031 : : /* If this is a commutative operation, and ARG0 is a constant, move it
11032 : : to ARG1 to reduce the number of tests below. */
11033 : 597350611 : if (commutative_tree_code (code)
11034 : 597350611 : && tree_swap_operands_p (arg0, arg1))
11035 : 32592998 : return fold_build2_loc (loc, code, type, op1, op0);
11036 : :
11037 : : /* Likewise if this is a comparison, and ARG0 is a constant, move it
11038 : : to ARG1 to reduce the number of tests below. */
11039 : 564757613 : if (kind == tcc_comparison
11040 : 564757613 : && tree_swap_operands_p (arg0, arg1))
11041 : 7661907 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11042 : :
11043 : 557095706 : tem = generic_simplify (loc, code, type, op0, op1);
11044 : 557095706 : if (tem)
11045 : : return tem;
11046 : :
11047 : : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11048 : :
11049 : : First check for cases where an arithmetic operation is applied to a
11050 : : compound, conditional, or comparison operation. Push the arithmetic
11051 : : operation inside the compound or conditional to see if any folding
11052 : : can then be done. Convert comparison to conditional for this purpose.
11053 : : The also optimizes non-constant cases that used to be done in
11054 : : expand_expr.
11055 : :
11056 : : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11057 : : one of the operands is a comparison and the other is a comparison, a
11058 : : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11059 : : code below would make the expression more complex. Change it to a
11060 : : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11061 : : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11062 : :
11063 : 469020790 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11064 : : || code == EQ_EXPR || code == NE_EXPR)
11065 : 55026351 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11066 : 54452092 : && ((truth_value_p (TREE_CODE (arg0))
11067 : 1227178 : && (truth_value_p (TREE_CODE (arg1))
11068 : 920595 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11069 : 40 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11070 : 54145493 : || (truth_value_p (TREE_CODE (arg1))
11071 : 6766 : && (truth_value_p (TREE_CODE (arg0))
11072 : 6766 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11073 : 167 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11074 : : {
11075 : 346949 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11076 : 40336 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11077 : : : TRUTH_XOR_EXPR,
11078 : : boolean_type_node,
11079 : : fold_convert_loc (loc, boolean_type_node, arg0),
11080 : : fold_convert_loc (loc, boolean_type_node, arg1));
11081 : :
11082 : 306613 : if (code == EQ_EXPR)
11083 : 34216 : tem = invert_truthvalue_loc (loc, tem);
11084 : :
11085 : 306613 : return fold_convert_loc (loc, type, tem);
11086 : : }
11087 : :
11088 : 468714177 : if (TREE_CODE_CLASS (code) == tcc_binary
11089 : 266278629 : || TREE_CODE_CLASS (code) == tcc_comparison)
11090 : : {
11091 : 292892236 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11092 : : {
11093 : 81495 : tem = fold_build2_loc (loc, code, type,
11094 : 81495 : fold_convert_loc (loc, TREE_TYPE (op0),
11095 : 81495 : TREE_OPERAND (arg0, 1)), op1);
11096 : 81495 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11097 : 81495 : tem);
11098 : : }
11099 : 292810741 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11100 : : {
11101 : 3137 : tem = fold_build2_loc (loc, code, type, op0,
11102 : 3137 : fold_convert_loc (loc, TREE_TYPE (op1),
11103 : 3137 : TREE_OPERAND (arg1, 1)));
11104 : 3137 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11105 : 3137 : tem);
11106 : : }
11107 : :
11108 : 292807604 : if (TREE_CODE (arg0) == COND_EXPR
11109 : 292434577 : || TREE_CODE (arg0) == VEC_COND_EXPR
11110 : 292432443 : || COMPARISON_CLASS_P (arg0))
11111 : : {
11112 : 737917 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11113 : : arg0, arg1,
11114 : : /*cond_first_p=*/1);
11115 : 737917 : if (tem != NULL_TREE)
11116 : : return tem;
11117 : : }
11118 : :
11119 : 292324844 : if (TREE_CODE (arg1) == COND_EXPR
11120 : 292098554 : || TREE_CODE (arg1) == VEC_COND_EXPR
11121 : 292098330 : || COMPARISON_CLASS_P (arg1))
11122 : : {
11123 : 240642 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11124 : : arg1, arg0,
11125 : : /*cond_first_p=*/0);
11126 : 240642 : if (tem != NULL_TREE)
11127 : : return tem;
11128 : : }
11129 : : }
11130 : :
11131 : 468139096 : switch (code)
11132 : : {
11133 : 51315062 : case MEM_REF:
11134 : : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11135 : 51315062 : if (TREE_CODE (arg0) == ADDR_EXPR
11136 : 51315062 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11137 : : {
11138 : 875004 : tree iref = TREE_OPERAND (arg0, 0);
11139 : 875004 : return fold_build2 (MEM_REF, type,
11140 : : TREE_OPERAND (iref, 0),
11141 : : int_const_binop (PLUS_EXPR, arg1,
11142 : : TREE_OPERAND (iref, 1)));
11143 : : }
11144 : :
11145 : : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11146 : 50440058 : if (TREE_CODE (arg0) == ADDR_EXPR
11147 : 50440058 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11148 : : {
11149 : 2394537 : tree base;
11150 : 2394537 : poly_int64 coffset;
11151 : 2394537 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11152 : : &coffset);
11153 : 2394537 : if (!base)
11154 : : return NULL_TREE;
11155 : 2390797 : return fold_build2 (MEM_REF, type,
11156 : : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11157 : : int_const_binop (PLUS_EXPR, arg1,
11158 : : size_int (coffset)));
11159 : : }
11160 : :
11161 : : return NULL_TREE;
11162 : :
11163 : 31806919 : case POINTER_PLUS_EXPR:
11164 : : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11165 : 63613422 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11166 : 63603979 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11167 : 35157 : return fold_convert_loc (loc, type,
11168 : : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11169 : : fold_convert_loc (loc, sizetype,
11170 : : arg1),
11171 : : fold_convert_loc (loc, sizetype,
11172 : 35157 : arg0)));
11173 : :
11174 : : return NULL_TREE;
11175 : :
11176 : 60509093 : case PLUS_EXPR:
11177 : 60509093 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11178 : : {
11179 : : /* X + (X / CST) * -CST is X % CST. */
11180 : 48597691 : if (TREE_CODE (arg1) == MULT_EXPR
11181 : 2283302 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11182 : 48603864 : && operand_equal_p (arg0,
11183 : 6173 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11184 : : {
11185 : 214 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11186 : 214 : tree cst1 = TREE_OPERAND (arg1, 1);
11187 : 214 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11188 : : cst1, cst0);
11189 : 214 : if (sum && integer_zerop (sum))
11190 : 214 : return fold_convert_loc (loc, type,
11191 : : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11192 : 214 : TREE_TYPE (arg0), arg0,
11193 : 214 : cst0));
11194 : : }
11195 : : }
11196 : :
11197 : : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11198 : : one. Make sure the type is not saturating and has the signedness of
11199 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11200 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11201 : 60508879 : if ((TREE_CODE (arg0) == MULT_EXPR
11202 : 49137986 : || TREE_CODE (arg1) == MULT_EXPR)
11203 : 12722464 : && !TYPE_SATURATING (type)
11204 : 12722464 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11205 : 12321385 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11206 : 72174980 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11207 : : {
11208 : 8297328 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11209 : 8297328 : if (tem)
11210 : : return tem;
11211 : : }
11212 : :
11213 : 59304311 : if (! FLOAT_TYPE_P (type))
11214 : : {
11215 : : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11216 : : (plus (plus (mult) (mult)) (foo)) so that we can
11217 : : take advantage of the factoring cases below. */
11218 : 280875 : if (ANY_INTEGRAL_TYPE_P (type)
11219 : 47395257 : && TYPE_OVERFLOW_WRAPS (type)
11220 : 47395257 : && (((TREE_CODE (arg0) == PLUS_EXPR
11221 : 29657777 : || TREE_CODE (arg0) == MINUS_EXPR)
11222 : 3390194 : && TREE_CODE (arg1) == MULT_EXPR)
11223 : 29155129 : || ((TREE_CODE (arg1) == PLUS_EXPR
11224 : 29155129 : || TREE_CODE (arg1) == MINUS_EXPR)
11225 : 437831 : && TREE_CODE (arg0) == MULT_EXPR)))
11226 : : {
11227 : 550995 : tree parg0, parg1, parg, marg;
11228 : 550995 : enum tree_code pcode;
11229 : :
11230 : 550995 : if (TREE_CODE (arg1) == MULT_EXPR)
11231 : : parg = arg0, marg = arg1;
11232 : : else
11233 : 48347 : parg = arg1, marg = arg0;
11234 : 550995 : pcode = TREE_CODE (parg);
11235 : 550995 : parg0 = TREE_OPERAND (parg, 0);
11236 : 550995 : parg1 = TREE_OPERAND (parg, 1);
11237 : 550995 : STRIP_NOPS (parg0);
11238 : 550995 : STRIP_NOPS (parg1);
11239 : :
11240 : 550995 : if (TREE_CODE (parg0) == MULT_EXPR
11241 : 254767 : && TREE_CODE (parg1) != MULT_EXPR)
11242 : 226906 : return fold_build2_loc (loc, pcode, type,
11243 : : fold_build2_loc (loc, PLUS_EXPR, type,
11244 : : fold_convert_loc (loc, type,
11245 : : parg0),
11246 : : fold_convert_loc (loc, type,
11247 : : marg)),
11248 : 226906 : fold_convert_loc (loc, type, parg1));
11249 : 324089 : if (TREE_CODE (parg0) != MULT_EXPR
11250 : 296228 : && TREE_CODE (parg1) == MULT_EXPR)
11251 : 99403 : return
11252 : 99403 : fold_build2_loc (loc, PLUS_EXPR, type,
11253 : : fold_convert_loc (loc, type, parg0),
11254 : : fold_build2_loc (loc, pcode, type,
11255 : : fold_convert_loc (loc, type, marg),
11256 : : fold_convert_loc (loc, type,
11257 : 99403 : parg1)));
11258 : : }
11259 : : }
11260 : : else
11261 : : {
11262 : : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11263 : : to __complex__ ( x, y ). This is not the same for SNaNs or
11264 : : if signed zeros are involved. */
11265 : 11909054 : if (!HONOR_SNANS (arg0)
11266 : 11907394 : && !HONOR_SIGNED_ZEROS (arg0)
11267 : 11930108 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11268 : : {
11269 : 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11270 : 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11271 : 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11272 : 3086 : bool arg0rz = false, arg0iz = false;
11273 : 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11274 : 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11275 : : {
11276 : 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11277 : 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11278 : 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11279 : : {
11280 : 22 : tree rp = arg1r ? arg1r
11281 : 0 : : build1 (REALPART_EXPR, rtype, arg1);
11282 : 22 : tree ip = arg0i ? arg0i
11283 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11284 : 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11285 : : }
11286 : 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11287 : : {
11288 : 53 : tree rp = arg0r ? arg0r
11289 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11290 : 53 : tree ip = arg1i ? arg1i
11291 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11292 : 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11293 : : }
11294 : : }
11295 : : }
11296 : :
11297 : : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11298 : : We associate floats only if the user has specified
11299 : : -fassociative-math. */
11300 : 11908979 : if (flag_associative_math
11301 : 20957 : && TREE_CODE (arg1) == PLUS_EXPR
11302 : 36 : && TREE_CODE (arg0) != MULT_EXPR)
11303 : : {
11304 : 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11305 : 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11306 : 21 : if (TREE_CODE (tree11) == MULT_EXPR
11307 : 5 : && TREE_CODE (tree10) == MULT_EXPR)
11308 : : {
11309 : 1 : tree tree0;
11310 : 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11311 : 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11312 : : }
11313 : : }
11314 : : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11315 : : We associate floats only if the user has specified
11316 : : -fassociative-math. */
11317 : 11908978 : if (flag_associative_math
11318 : 20956 : && TREE_CODE (arg0) == PLUS_EXPR
11319 : 1222 : && TREE_CODE (arg1) != MULT_EXPR)
11320 : : {
11321 : 832 : tree tree00 = TREE_OPERAND (arg0, 0);
11322 : 832 : tree tree01 = TREE_OPERAND (arg0, 1);
11323 : 832 : if (TREE_CODE (tree01) == MULT_EXPR
11324 : 49 : && TREE_CODE (tree00) == MULT_EXPR)
11325 : : {
11326 : 9 : tree tree0;
11327 : 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11328 : 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11329 : : }
11330 : : }
11331 : : }
11332 : :
11333 : 11908146 : bit_rotate:
11334 : : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11335 : : is a rotate of A by C1 bits. */
11336 : : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11337 : : is a rotate of A by B bits.
11338 : : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11339 : : though in this case CODE must be | and not + or ^, otherwise
11340 : : it doesn't return A when B is 0. */
11341 : 61485425 : {
11342 : 61485425 : enum tree_code code0, code1;
11343 : 61485425 : tree rtype;
11344 : 61485425 : code0 = TREE_CODE (arg0);
11345 : 61485425 : code1 = TREE_CODE (arg1);
11346 : 53961 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11347 : 61469077 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11348 : 39704 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11349 : 39704 : TREE_OPERAND (arg1, 0), 0)
11350 : 36955 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11351 : 36955 : TYPE_UNSIGNED (rtype))
11352 : : /* Only create rotates in complete modes. Other cases are not
11353 : : expanded properly. */
11354 : 61512210 : && (element_precision (rtype)
11355 : 53570 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11356 : : {
11357 : 26715 : tree tree01, tree11;
11358 : 26715 : tree orig_tree01, orig_tree11;
11359 : 26715 : enum tree_code code01, code11;
11360 : :
11361 : 26715 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11362 : 26715 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11363 : 26715 : STRIP_NOPS (tree01);
11364 : 26715 : STRIP_NOPS (tree11);
11365 : 26715 : code01 = TREE_CODE (tree01);
11366 : 26715 : code11 = TREE_CODE (tree11);
11367 : 26715 : if (code11 != MINUS_EXPR
11368 : 26029 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11369 : : {
11370 : 1478 : std::swap (code0, code1);
11371 : 1478 : std::swap (code01, code11);
11372 : 1478 : std::swap (tree01, tree11);
11373 : 1478 : std::swap (orig_tree01, orig_tree11);
11374 : : }
11375 : 53430 : if (code01 == INTEGER_CST
11376 : 3152 : && code11 == INTEGER_CST
11377 : 33017 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11378 : 33017 : == element_precision (rtype)))
11379 : : {
11380 : 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11381 : 3011 : rtype, TREE_OPERAND (arg0, 0),
11382 : : code0 == LSHIFT_EXPR
11383 : : ? orig_tree01 : orig_tree11);
11384 : 3011 : return fold_convert_loc (loc, type, tem);
11385 : : }
11386 : 23704 : else if (code11 == MINUS_EXPR)
11387 : : {
11388 : 941 : tree tree110, tree111;
11389 : 941 : tree110 = TREE_OPERAND (tree11, 0);
11390 : 941 : tree111 = TREE_OPERAND (tree11, 1);
11391 : 941 : STRIP_NOPS (tree110);
11392 : 941 : STRIP_NOPS (tree111);
11393 : 941 : if (TREE_CODE (tree110) == INTEGER_CST
11394 : 930 : && compare_tree_int (tree110,
11395 : 930 : element_precision (rtype)) == 0
11396 : 1855 : && operand_equal_p (tree01, tree111, 0))
11397 : : {
11398 : 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11399 : : ? LROTATE_EXPR : RROTATE_EXPR),
11400 : 558 : rtype, TREE_OPERAND (arg0, 0),
11401 : : orig_tree01);
11402 : 558 : return fold_convert_loc (loc, type, tem);
11403 : : }
11404 : : }
11405 : 22763 : else if (code == BIT_IOR_EXPR
11406 : 21649 : && code11 == BIT_AND_EXPR
11407 : 44337 : && pow2p_hwi (element_precision (rtype)))
11408 : : {
11409 : 21574 : tree tree110, tree111;
11410 : 21574 : tree110 = TREE_OPERAND (tree11, 0);
11411 : 21574 : tree111 = TREE_OPERAND (tree11, 1);
11412 : 21574 : STRIP_NOPS (tree110);
11413 : 21574 : STRIP_NOPS (tree111);
11414 : 21574 : if (TREE_CODE (tree110) == NEGATE_EXPR
11415 : 21087 : && TREE_CODE (tree111) == INTEGER_CST
11416 : 21087 : && compare_tree_int (tree111,
11417 : 21087 : element_precision (rtype) - 1) == 0
11418 : 42647 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11419 : : {
11420 : 31457 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11421 : : ? LROTATE_EXPR : RROTATE_EXPR),
11422 : 20995 : rtype, TREE_OPERAND (arg0, 0),
11423 : : orig_tree01);
11424 : 20995 : return fold_convert_loc (loc, type, tem);
11425 : : }
11426 : : }
11427 : : }
11428 : : }
11429 : :
11430 : 151657342 : associate:
11431 : : /* In most languages, can't associate operations on floats through
11432 : : parentheses. Rather than remember where the parentheses were, we
11433 : : don't associate floats at all, unless the user has specified
11434 : : -fassociative-math.
11435 : : And, we need to make sure type is not saturating. */
11436 : :
11437 : 151657342 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11438 : 109443612 : && !TYPE_SATURATING (type)
11439 : 261100954 : && !TYPE_OVERFLOW_SANITIZED (type))
11440 : : {
11441 : 109415473 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11442 : 109415473 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11443 : 109415473 : tree atype = type;
11444 : 109415473 : bool ok = true;
11445 : :
11446 : : /* Split both trees into variables, constants, and literals. Then
11447 : : associate each group together, the constants with literals,
11448 : : then the result with variables. This increases the chances of
11449 : : literals being recombined later and of generating relocatable
11450 : : expressions for the sum of a constant and literal. */
11451 : 109415473 : var0 = split_tree (arg0, type, code,
11452 : : &minus_var0, &con0, &minus_con0,
11453 : : &lit0, &minus_lit0, 0);
11454 : 109415473 : var1 = split_tree (arg1, type, code,
11455 : : &minus_var1, &con1, &minus_con1,
11456 : : &lit1, &minus_lit1, code == MINUS_EXPR);
11457 : :
11458 : : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11459 : 109415473 : if (code == MINUS_EXPR)
11460 : 11853802 : code = PLUS_EXPR;
11461 : :
11462 : : /* With undefined overflow prefer doing association in a type
11463 : : which wraps on overflow, if that is one of the operand types. */
11464 : 109415242 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11465 : 217591159 : && !TYPE_OVERFLOW_WRAPS (type))
11466 : : {
11467 : 59181282 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11468 : 58520539 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11469 : 753953 : atype = TREE_TYPE (arg0);
11470 : 57658142 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11471 : 57427687 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11472 : 220966 : atype = TREE_TYPE (arg1);
11473 : 29853494 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11474 : : }
11475 : :
11476 : : /* With undefined overflow we can only associate constants with one
11477 : : variable, and constants whose association doesn't overflow. */
11478 : 109415242 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11479 : 217591159 : && !TYPE_OVERFLOW_WRAPS (atype))
11480 : : {
11481 : 28878575 : if ((var0 && var1) || (minus_var0 && minus_var1))
11482 : : {
11483 : : /* ??? If split_tree would handle NEGATE_EXPR we could
11484 : : simply reject these cases and the allowed cases would
11485 : : be the var0/minus_var1 ones. */
11486 : 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11487 : 5376071 : tree tmp1 = var1 ? var1 : minus_var1;
11488 : 5376071 : bool one_neg = false;
11489 : :
11490 : 5376071 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11491 : : {
11492 : 1644 : tmp0 = TREE_OPERAND (tmp0, 0);
11493 : 1644 : one_neg = !one_neg;
11494 : : }
11495 : 4836585 : if (CONVERT_EXPR_P (tmp0)
11496 : 555372 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11497 : 5930316 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11498 : 554245 : <= TYPE_PRECISION (atype)))
11499 : 541795 : tmp0 = TREE_OPERAND (tmp0, 0);
11500 : 5376071 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11501 : : {
11502 : 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11503 : 170 : one_neg = !one_neg;
11504 : : }
11505 : 5066514 : if (CONVERT_EXPR_P (tmp1)
11506 : 333062 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11507 : 5709085 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11508 : 333014 : <= TYPE_PRECISION (atype)))
11509 : 322987 : tmp1 = TREE_OPERAND (tmp1, 0);
11510 : : /* The only case we can still associate with two variables
11511 : : is if they cancel out. */
11512 : 5376071 : if (!one_neg
11513 : 5376071 : || !operand_equal_p (tmp0, tmp1, 0))
11514 : : ok = false;
11515 : : }
11516 : 23103955 : else if ((var0 && minus_var1
11517 : 3864085 : && ! operand_equal_p (var0, minus_var1, 0))
11518 : 42742375 : || (minus_var0 && var1
11519 : 11319 : && ! operand_equal_p (minus_var0, var1, 0)))
11520 : : ok = false;
11521 : : }
11522 : :
11523 : : /* Only do something if we found more than two objects. Otherwise,
11524 : : nothing has changed and we risk infinite recursion. */
11525 : : if (ok
11526 : 100164070 : && ((var0 != 0) + (var1 != 0)
11527 : 100164070 : + (minus_var0 != 0) + (minus_var1 != 0)
11528 : 100164070 : + (con0 != 0) + (con1 != 0)
11529 : 100164070 : + (minus_con0 != 0) + (minus_con1 != 0)
11530 : 100164070 : + (lit0 != 0) + (lit1 != 0)
11531 : 100164070 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11532 : : {
11533 : 2049598 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11534 : 4099196 : int minus_var0_origin
11535 : 2049598 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11536 : 2049598 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11537 : 4099196 : int minus_con0_origin
11538 : 2049598 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11539 : 2049598 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11540 : 4099196 : int minus_lit0_origin
11541 : 2049598 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11542 : 2049598 : var0 = associate_trees (loc, var0, var1, code, atype);
11543 : 2049598 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11544 : : code, atype);
11545 : 2049598 : con0 = associate_trees (loc, con0, con1, code, atype);
11546 : 2049598 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11547 : : code, atype);
11548 : 2049598 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11549 : 2049598 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11550 : : code, atype);
11551 : :
11552 : 2049598 : if (minus_var0 && var0)
11553 : : {
11554 : 1334685 : var0_origin |= minus_var0_origin;
11555 : 1334685 : var0 = associate_trees (loc, var0, minus_var0,
11556 : : MINUS_EXPR, atype);
11557 : 1334685 : minus_var0 = 0;
11558 : 1334685 : minus_var0_origin = 0;
11559 : : }
11560 : 2049598 : if (minus_con0 && con0)
11561 : : {
11562 : 4194 : con0_origin |= minus_con0_origin;
11563 : 4194 : con0 = associate_trees (loc, con0, minus_con0,
11564 : : MINUS_EXPR, atype);
11565 : 4194 : minus_con0 = 0;
11566 : 4194 : minus_con0_origin = 0;
11567 : : }
11568 : :
11569 : : /* Preserve the MINUS_EXPR if the negative part of the literal is
11570 : : greater than the positive part. Otherwise, the multiplicative
11571 : : folding code (i.e extract_muldiv) may be fooled in case
11572 : : unsigned constants are subtracted, like in the following
11573 : : example: ((X*2 + 4) - 8U)/2. */
11574 : 2049598 : if (minus_lit0 && lit0)
11575 : : {
11576 : 240614 : if (TREE_CODE (lit0) == INTEGER_CST
11577 : 240614 : && TREE_CODE (minus_lit0) == INTEGER_CST
11578 : 240614 : && tree_int_cst_lt (lit0, minus_lit0)
11579 : : /* But avoid ending up with only negated parts. */
11580 : 297540 : && (var0 || con0))
11581 : : {
11582 : 51969 : minus_lit0_origin |= lit0_origin;
11583 : 51969 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11584 : : MINUS_EXPR, atype);
11585 : 51969 : lit0 = 0;
11586 : 51969 : lit0_origin = 0;
11587 : : }
11588 : : else
11589 : : {
11590 : 188645 : lit0_origin |= minus_lit0_origin;
11591 : 188645 : lit0 = associate_trees (loc, lit0, minus_lit0,
11592 : : MINUS_EXPR, atype);
11593 : 188645 : minus_lit0 = 0;
11594 : 188645 : minus_lit0_origin = 0;
11595 : : }
11596 : : }
11597 : :
11598 : : /* Don't introduce overflows through reassociation. */
11599 : 1367967 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11600 : 3417524 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11601 : 2049598 : return NULL_TREE;
11602 : :
11603 : : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11604 : 2049557 : con0_origin |= lit0_origin;
11605 : 2049557 : con0 = associate_trees (loc, con0, lit0, code, atype);
11606 : 2049557 : minus_con0_origin |= minus_lit0_origin;
11607 : 2049557 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11608 : : code, atype);
11609 : :
11610 : : /* Eliminate minus_con0. */
11611 : 2049557 : if (minus_con0)
11612 : : {
11613 : 686276 : if (con0)
11614 : : {
11615 : 15921 : con0_origin |= minus_con0_origin;
11616 : 15921 : con0 = associate_trees (loc, con0, minus_con0,
11617 : : MINUS_EXPR, atype);
11618 : : }
11619 : 670355 : else if (var0)
11620 : : {
11621 : 670355 : var0_origin |= minus_con0_origin;
11622 : 670355 : var0 = associate_trees (loc, var0, minus_con0,
11623 : : MINUS_EXPR, atype);
11624 : : }
11625 : : else
11626 : 0 : gcc_unreachable ();
11627 : : }
11628 : :
11629 : : /* Eliminate minus_var0. */
11630 : 2049557 : if (minus_var0)
11631 : : {
11632 : 349614 : if (con0)
11633 : : {
11634 : 349614 : con0_origin |= minus_var0_origin;
11635 : 349614 : con0 = associate_trees (loc, con0, minus_var0,
11636 : : MINUS_EXPR, atype);
11637 : : }
11638 : : else
11639 : 0 : gcc_unreachable ();
11640 : : }
11641 : :
11642 : : /* Reassociate only if there has been any actual association
11643 : : between subtrees from op0 and subtrees from op1 in at
11644 : : least one of the operands, otherwise we risk infinite
11645 : : recursion. See PR114084. */
11646 : 2049557 : if (var0_origin != 3 && con0_origin != 3)
11647 : : return NULL_TREE;
11648 : :
11649 : 2047854 : return
11650 : 2047854 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11651 : 2047854 : code, atype));
11652 : : }
11653 : : }
11654 : :
11655 : : return NULL_TREE;
11656 : :
11657 : 22687508 : case POINTER_DIFF_EXPR:
11658 : 22687508 : case MINUS_EXPR:
11659 : : /* Fold &a[i] - &a[j] to i-j. */
11660 : 22687508 : if (TREE_CODE (arg0) == ADDR_EXPR
11661 : 52627 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11662 : 6171 : && TREE_CODE (arg1) == ADDR_EXPR
11663 : 22688058 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11664 : : {
11665 : 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11666 : 38 : TREE_OPERAND (arg0, 0),
11667 : 38 : TREE_OPERAND (arg1, 0),
11668 : : code
11669 : : == POINTER_DIFF_EXPR);
11670 : 38 : if (tem)
11671 : : return tem;
11672 : : }
11673 : :
11674 : : /* Further transformations are not for pointers. */
11675 : 22687494 : if (code == POINTER_DIFF_EXPR)
11676 : : return NULL_TREE;
11677 : :
11678 : : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11679 : 20060074 : if (TREE_CODE (arg0) == NEGATE_EXPR
11680 : 148489 : && negate_expr_p (op1)
11681 : : /* If arg0 is e.g. unsigned int and type is int, then this could
11682 : : introduce UB, because if A is INT_MIN at runtime, the original
11683 : : expression can be well defined while the latter is not.
11684 : : See PR83269. */
11685 : 20060893 : && !(ANY_INTEGRAL_TYPE_P (type)
11686 : 819 : && TYPE_OVERFLOW_UNDEFINED (type)
11687 : 807 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11688 : 807 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11689 : 812 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11690 : : fold_convert_loc (loc, type,
11691 : 1624 : TREE_OPERAND (arg0, 0)));
11692 : :
11693 : : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11694 : : __complex__ ( x, -y ). This is not the same for SNaNs or if
11695 : : signed zeros are involved. */
11696 : 20059262 : if (!HONOR_SNANS (arg0)
11697 : 20058111 : && !HONOR_SIGNED_ZEROS (arg0)
11698 : 32705365 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11699 : : {
11700 : 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11701 : 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11702 : 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11703 : 53 : bool arg0rz = false, arg0iz = false;
11704 : 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11705 : 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11706 : : {
11707 : 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11708 : 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11709 : 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11710 : : {
11711 : 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11712 : : arg1r ? arg1r
11713 : 0 : : build1 (REALPART_EXPR, rtype, arg1));
11714 : 9 : tree ip = arg0i ? arg0i
11715 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11716 : 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11717 : : }
11718 : 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11719 : : {
11720 : 15 : tree rp = arg0r ? arg0r
11721 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11722 : 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11723 : : arg1i ? arg1i
11724 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11725 : 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11726 : : }
11727 : : }
11728 : : }
11729 : :
11730 : : /* A - B -> A + (-B) if B is easily negatable. */
11731 : 20059238 : if (negate_expr_p (op1)
11732 : 736604 : && ! TYPE_OVERFLOW_SANITIZED (type)
11733 : 20793375 : && ((FLOAT_TYPE_P (type)
11734 : : /* Avoid this transformation if B is a positive REAL_CST. */
11735 : 65 : && (TREE_CODE (op1) != REAL_CST
11736 : 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11737 : 734072 : || INTEGRAL_TYPE_P (type)))
11738 : 733952 : return fold_build2_loc (loc, PLUS_EXPR, type,
11739 : : fold_convert_loc (loc, type, arg0),
11740 : 733952 : negate_expr (op1));
11741 : :
11742 : : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11743 : : one. Make sure the type is not saturating and has the signedness of
11744 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11745 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11746 : 19325286 : if ((TREE_CODE (arg0) == MULT_EXPR
11747 : 17997835 : || TREE_CODE (arg1) == MULT_EXPR)
11748 : 2713421 : && !TYPE_SATURATING (type)
11749 : 2713421 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11750 : 2579435 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11751 : 21849517 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11752 : : {
11753 : 358361 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11754 : 358361 : if (tem)
11755 : : return tem;
11756 : : }
11757 : :
11758 : 19274542 : goto associate;
11759 : :
11760 : 64940913 : case MULT_EXPR:
11761 : 64940913 : if (! FLOAT_TYPE_P (type))
11762 : : {
11763 : : /* Transform x * -C into -x * C if x is easily negatable. */
11764 : 41998595 : if (TREE_CODE (op1) == INTEGER_CST
11765 : 39207445 : && tree_int_cst_sgn (op1) == -1
11766 : 214595 : && negate_expr_p (op0)
11767 : 336 : && negate_expr_p (op1)
11768 : 320 : && (tem = negate_expr (op1)) != op1
11769 : 41998915 : && ! TREE_OVERFLOW (tem))
11770 : 320 : return fold_build2_loc (loc, MULT_EXPR, type,
11771 : : fold_convert_loc (loc, type,
11772 : 320 : negate_expr (op0)), tem);
11773 : :
11774 : 41998275 : strict_overflow_p = false;
11775 : 41998275 : if (TREE_CODE (arg1) == INTEGER_CST
11776 : 41998275 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11777 : : &strict_overflow_p)) != 0)
11778 : : {
11779 : 513136 : if (strict_overflow_p)
11780 : 10 : fold_overflow_warning (("assuming signed overflow does not "
11781 : : "occur when simplifying "
11782 : : "multiplication"),
11783 : : WARN_STRICT_OVERFLOW_MISC);
11784 : 513136 : return fold_convert_loc (loc, type, tem);
11785 : : }
11786 : :
11787 : : /* Optimize z * conj(z) for integer complex numbers. */
11788 : 41485139 : if (TREE_CODE (arg0) == CONJ_EXPR
11789 : 41485139 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11790 : 1 : return fold_mult_zconjz (loc, type, arg1);
11791 : 41485138 : if (TREE_CODE (arg1) == CONJ_EXPR
11792 : 41485138 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11793 : 0 : return fold_mult_zconjz (loc, type, arg0);
11794 : : }
11795 : : else
11796 : : {
11797 : : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11798 : : This is not the same for NaNs or if signed zeros are
11799 : : involved. */
11800 : 22942318 : if (!HONOR_NANS (arg0)
11801 : 32738 : && !HONOR_SIGNED_ZEROS (arg0)
11802 : 32438 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11803 : 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11804 : 22942543 : && real_zerop (TREE_REALPART (arg1)))
11805 : : {
11806 : 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11807 : 218 : if (real_onep (TREE_IMAGPART (arg1)))
11808 : : {
11809 : 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11810 : 63 : arg0 = save_expr (arg0);
11811 : 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11812 : : rtype, arg0);
11813 : 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11814 : : rtype, arg0);
11815 : 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11816 : : negate_expr (iarg0),
11817 : 208 : rarg0);
11818 : : }
11819 : 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11820 : : {
11821 : 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11822 : 0 : arg0 = save_expr (arg0);
11823 : 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11824 : : rtype, arg0);
11825 : 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11826 : : rtype, arg0);
11827 : 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11828 : : iarg0,
11829 : 10 : negate_expr (rarg0));
11830 : : }
11831 : : }
11832 : :
11833 : : /* Optimize z * conj(z) for floating point complex numbers.
11834 : : Guarded by flag_unsafe_math_optimizations as non-finite
11835 : : imaginary components don't produce scalar results. */
11836 : 22942100 : if (flag_unsafe_math_optimizations
11837 : 32267 : && TREE_CODE (arg0) == CONJ_EXPR
11838 : 22942102 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11839 : 1 : return fold_mult_zconjz (loc, type, arg1);
11840 : 22942099 : if (flag_unsafe_math_optimizations
11841 : 32266 : && TREE_CODE (arg1) == CONJ_EXPR
11842 : 22942103 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11843 : 0 : return fold_mult_zconjz (loc, type, arg0);
11844 : : }
11845 : 64427237 : goto associate;
11846 : :
11847 : 1827303 : case BIT_IOR_EXPR:
11848 : : /* Canonicalize (X & C1) | C2. */
11849 : 1827303 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11850 : 132903 : && TREE_CODE (arg1) == INTEGER_CST
11851 : 1914139 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11852 : : {
11853 : 86828 : int width = TYPE_PRECISION (type), w;
11854 : 86828 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11855 : 86828 : wide_int c2 = wi::to_wide (arg1);
11856 : :
11857 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11858 : 86828 : if ((c1 & c2) == c1)
11859 : 0 : return omit_one_operand_loc (loc, type, arg1,
11860 : 0 : TREE_OPERAND (arg0, 0));
11861 : :
11862 : 86828 : wide_int msk = wi::mask (width, false,
11863 : 86828 : TYPE_PRECISION (TREE_TYPE (arg1)));
11864 : :
11865 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11866 : 86828 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11867 : : {
11868 : 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11869 : 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11870 : : }
11871 : :
11872 : : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11873 : : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11874 : : mode which allows further optimizations. */
11875 : 86822 : c1 &= msk;
11876 : 86822 : c2 &= msk;
11877 : 86822 : wide_int c3 = wi::bit_and_not (c1, c2);
11878 : 268830 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11879 : : {
11880 : 182250 : wide_int mask = wi::mask (w, false,
11881 : 182250 : TYPE_PRECISION (type));
11882 : 364500 : if (((c1 | c2) & mask) == mask
11883 : 364500 : && wi::bit_and_not (c1, mask) == 0)
11884 : : {
11885 : 242 : c3 = mask;
11886 : 242 : break;
11887 : : }
11888 : 182250 : }
11889 : :
11890 : 86822 : if (c3 != c1)
11891 : : {
11892 : 562 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11893 : 1124 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11894 : 562 : wide_int_to_tree (type, c3));
11895 : 562 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11896 : : }
11897 : 87958 : }
11898 : :
11899 : : /* See if this can be simplified into a rotate first. If that
11900 : : is unsuccessful continue in the association code. */
11901 : 1826735 : goto bit_rotate;
11902 : :
11903 : 680773 : case BIT_XOR_EXPR:
11904 : : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11905 : 680773 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11906 : 2420 : && INTEGRAL_TYPE_P (type)
11907 : 1815 : && integer_onep (TREE_OPERAND (arg0, 1))
11908 : 680776 : && integer_onep (arg1))
11909 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11910 : 0 : build_zero_cst (TREE_TYPE (arg0)));
11911 : :
11912 : : /* See if this can be simplified into a rotate first. If that
11913 : : is unsuccessful continue in the association code. */
11914 : 680773 : goto bit_rotate;
11915 : :
11916 : 6060507 : case BIT_AND_EXPR:
11917 : : /* Fold !X & 1 as X == 0. */
11918 : 6060507 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11919 : 6060507 : && integer_onep (arg1))
11920 : : {
11921 : 0 : tem = TREE_OPERAND (arg0, 0);
11922 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11923 : 0 : build_zero_cst (TREE_TYPE (tem)));
11924 : : }
11925 : :
11926 : : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11927 : : multiple of 1 << CST. */
11928 : 6060507 : if (TREE_CODE (arg1) == INTEGER_CST)
11929 : : {
11930 : 4401283 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11931 : 4401283 : wide_int ncst1 = -cst1;
11932 : 4401283 : if ((cst1 & ncst1) == ncst1
11933 : 4571847 : && multiple_of_p (type, arg0,
11934 : 4571847 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11935 : 467 : return fold_convert_loc (loc, type, arg0);
11936 : 4401283 : }
11937 : :
11938 : : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11939 : : bits from CST2. */
11940 : 6060040 : if (TREE_CODE (arg1) == INTEGER_CST
11941 : 4400816 : && TREE_CODE (arg0) == MULT_EXPR
11942 : 6166569 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11943 : : {
11944 : 106487 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11945 : 106487 : wide_int masked
11946 : 106487 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11947 : :
11948 : 106487 : if (masked == 0)
11949 : 4909 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11950 : 4909 : arg0, arg1);
11951 : 101578 : else if (masked != warg1)
11952 : : {
11953 : : /* Avoid the transform if arg1 is a mask of some
11954 : : mode which allows further optimizations. */
11955 : 662 : int pop = wi::popcount (warg1);
11956 : 695 : if (!(pop >= BITS_PER_UNIT
11957 : 59 : && pow2p_hwi (pop)
11958 : 728 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11959 : 1258 : return fold_build2_loc (loc, code, type, op0,
11960 : 1258 : wide_int_to_tree (type, masked));
11961 : : }
11962 : 106487 : }
11963 : :
11964 : : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11965 : 4395278 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11966 : 6335011 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11967 : : {
11968 : 189106 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11969 : :
11970 : 189106 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11971 : 189106 : if (mask == -1)
11972 : 2545 : return
11973 : 2545 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11974 : 189106 : }
11975 : :
11976 : 6051957 : goto associate;
11977 : :
11978 : 6368144 : case RDIV_EXPR:
11979 : : /* Don't touch a floating-point divide by zero unless the mode
11980 : : of the constant can represent infinity. */
11981 : 6368144 : if (TREE_CODE (arg1) == REAL_CST
11982 : 3209589 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11983 : 6368144 : && real_zerop (arg1))
11984 : 0 : return NULL_TREE;
11985 : :
11986 : : /* (-A) / (-B) -> A / B */
11987 : 6368144 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11988 : 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11989 : 3 : TREE_OPERAND (arg0, 0),
11990 : 3 : negate_expr (arg1));
11991 : 6368141 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11992 : 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11993 : : negate_expr (arg0),
11994 : 0 : TREE_OPERAND (arg1, 0));
11995 : : return NULL_TREE;
11996 : :
11997 : 2189284 : case TRUNC_DIV_EXPR:
11998 : : /* Fall through */
11999 : :
12000 : 2189284 : case FLOOR_DIV_EXPR:
12001 : : /* Simplify A / (B << N) where A and B are positive and B is
12002 : : a power of 2, to A >> (N + log2(B)). */
12003 : 2189284 : strict_overflow_p = false;
12004 : 2189284 : if (TREE_CODE (arg1) == LSHIFT_EXPR
12005 : 2189284 : && (TYPE_UNSIGNED (type)
12006 : 8 : || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12007 : : {
12008 : 17 : tree sval = TREE_OPERAND (arg1, 0);
12009 : 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12010 : : {
12011 : 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
12012 : 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
12013 : 16 : wi::exact_log2 (wi::to_wide (sval)));
12014 : :
12015 : 16 : if (strict_overflow_p)
12016 : 0 : fold_overflow_warning (("assuming signed overflow does not "
12017 : : "occur when simplifying A / (B << N)"),
12018 : : WARN_STRICT_OVERFLOW_MISC);
12019 : :
12020 : 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12021 : : sh_cnt, pow2);
12022 : 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
12023 : 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
12024 : : }
12025 : : }
12026 : :
12027 : : /* Fall through */
12028 : :
12029 : 3421015 : case ROUND_DIV_EXPR:
12030 : 3421015 : case CEIL_DIV_EXPR:
12031 : 3421015 : case EXACT_DIV_EXPR:
12032 : 3421015 : if (integer_zerop (arg1))
12033 : : return NULL_TREE;
12034 : :
12035 : : /* Convert -A / -B to A / B when the type is signed and overflow is
12036 : : undefined. */
12037 : 3417997 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12038 : 838105 : && TREE_CODE (op0) == NEGATE_EXPR
12039 : 3418059 : && negate_expr_p (op1))
12040 : : {
12041 : 30 : if (ANY_INTEGRAL_TYPE_P (type))
12042 : 30 : fold_overflow_warning (("assuming signed overflow does not occur "
12043 : : "when distributing negation across "
12044 : : "division"),
12045 : : WARN_STRICT_OVERFLOW_MISC);
12046 : 60 : return fold_build2_loc (loc, code, type,
12047 : : fold_convert_loc (loc, type,
12048 : 30 : TREE_OPERAND (arg0, 0)),
12049 : 30 : negate_expr (op1));
12050 : : }
12051 : 3417967 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12052 : 838075 : && TREE_CODE (arg1) == NEGATE_EXPR
12053 : 3418211 : && negate_expr_p (op0))
12054 : : {
12055 : 36 : if (ANY_INTEGRAL_TYPE_P (type))
12056 : 36 : fold_overflow_warning (("assuming signed overflow does not occur "
12057 : : "when distributing negation across "
12058 : : "division"),
12059 : : WARN_STRICT_OVERFLOW_MISC);
12060 : 36 : return fold_build2_loc (loc, code, type,
12061 : : negate_expr (op0),
12062 : : fold_convert_loc (loc, type,
12063 : 72 : TREE_OPERAND (arg1, 0)));
12064 : : }
12065 : :
12066 : : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12067 : : operation, EXACT_DIV_EXPR.
12068 : :
12069 : : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12070 : : At one time others generated faster code, it's not clear if they do
12071 : : after the last round to changes to the DIV code in expmed.cc. */
12072 : 3417931 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12073 : 3417931 : && multiple_of_p (type, arg0, arg1))
12074 : 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12075 : : fold_convert (type, arg0),
12076 : 0 : fold_convert (type, arg1));
12077 : :
12078 : 3417931 : strict_overflow_p = false;
12079 : 3417931 : if (TREE_CODE (arg1) == INTEGER_CST
12080 : 3417931 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12081 : : &strict_overflow_p)) != 0)
12082 : : {
12083 : 2565 : if (strict_overflow_p)
12084 : 157 : fold_overflow_warning (("assuming signed overflow does not occur "
12085 : : "when simplifying division"),
12086 : : WARN_STRICT_OVERFLOW_MISC);
12087 : 2565 : return fold_convert_loc (loc, type, tem);
12088 : : }
12089 : :
12090 : : return NULL_TREE;
12091 : :
12092 : 635841 : case CEIL_MOD_EXPR:
12093 : 635841 : case FLOOR_MOD_EXPR:
12094 : 635841 : case ROUND_MOD_EXPR:
12095 : 635841 : case TRUNC_MOD_EXPR:
12096 : 635841 : strict_overflow_p = false;
12097 : 635841 : if (TREE_CODE (arg1) == INTEGER_CST
12098 : 635841 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12099 : : &strict_overflow_p)) != 0)
12100 : : {
12101 : 0 : if (strict_overflow_p)
12102 : 0 : fold_overflow_warning (("assuming signed overflow does not occur "
12103 : : "when simplifying modulus"),
12104 : : WARN_STRICT_OVERFLOW_MISC);
12105 : 0 : return fold_convert_loc (loc, type, tem);
12106 : : }
12107 : :
12108 : : return NULL_TREE;
12109 : :
12110 : 2072582 : case LROTATE_EXPR:
12111 : 2072582 : case RROTATE_EXPR:
12112 : 2072582 : case RSHIFT_EXPR:
12113 : 2072582 : case LSHIFT_EXPR:
12114 : : /* Since negative shift count is not well-defined,
12115 : : don't try to compute it in the compiler. */
12116 : 2072582 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12117 : : return NULL_TREE;
12118 : :
12119 : 2071529 : prec = element_precision (type);
12120 : :
12121 : : /* If we have a rotate of a bit operation with the rotate count and
12122 : : the second operand of the bit operation both constant,
12123 : : permute the two operations. */
12124 : 2644 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12125 : 2110 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12126 : 2110 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12127 : 2110 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12128 : 2071529 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12129 : : {
12130 : 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12131 : 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12132 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12133 : : fold_build2_loc (loc, code, type,
12134 : : arg00, arg1),
12135 : : fold_build2_loc (loc, code, type,
12136 : 0 : arg01, arg1));
12137 : : }
12138 : :
12139 : : return NULL_TREE;
12140 : :
12141 : 442745 : case MIN_EXPR:
12142 : 442745 : case MAX_EXPR:
12143 : 442745 : goto associate;
12144 : :
12145 : 5757682 : case TRUTH_ANDIF_EXPR:
12146 : : /* Note that the operands of this must be ints
12147 : : and their values must be 0 or 1.
12148 : : ("true" is a fixed value perhaps depending on the language.) */
12149 : : /* If first arg is constant zero, return it. */
12150 : 5757682 : if (integer_zerop (arg0))
12151 : 1173634 : return fold_convert_loc (loc, type, arg0);
12152 : : /* FALLTHRU */
12153 : 15232415 : case TRUTH_AND_EXPR:
12154 : : /* If either arg is constant true, drop it. */
12155 : 15232415 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12156 : 1988385 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12157 : 839976 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12158 : : /* Preserve sequence points. */
12159 : 14037052 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12160 : 764890 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12161 : : /* If second arg is constant zero, result is zero, but first arg
12162 : : must be evaluated. */
12163 : 12479140 : if (integer_zerop (arg1))
12164 : 46954 : return omit_one_operand_loc (loc, type, arg1, arg0);
12165 : : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12166 : : case will be handled here. */
12167 : 12432186 : if (integer_zerop (arg0))
12168 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12169 : :
12170 : : /* !X && X is always false. */
12171 : 12432186 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12172 : 12432186 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12173 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12174 : : /* X && !X is always false. */
12175 : 12432186 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12176 : 12432186 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12177 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12178 : :
12179 : : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12180 : : means A >= Y && A != MAX, but in this case we know that
12181 : : A < X <= MAX. */
12182 : :
12183 : 12432186 : if (!TREE_SIDE_EFFECTS (arg0)
12184 : 12432186 : && !TREE_SIDE_EFFECTS (arg1))
12185 : : {
12186 : 11418118 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12187 : 11418118 : if (tem && !operand_equal_p (tem, arg0, 0))
12188 : 433 : return fold_convert (type,
12189 : : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12190 : : tem, arg1));
12191 : :
12192 : 11417685 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12193 : 11417685 : if (tem && !operand_equal_p (tem, arg1, 0))
12194 : 9897 : return fold_convert (type,
12195 : : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12196 : : arg0, tem));
12197 : : }
12198 : :
12199 : 12421856 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12200 : : != NULL_TREE)
12201 : : return tem;
12202 : :
12203 : : return NULL_TREE;
12204 : :
12205 : 3151825 : case TRUTH_ORIF_EXPR:
12206 : : /* Note that the operands of this must be ints
12207 : : and their values must be 0 or true.
12208 : : ("true" is a fixed value perhaps depending on the language.) */
12209 : : /* If first arg is constant true, return it. */
12210 : 3151825 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12211 : 128672 : return fold_convert_loc (loc, type, arg0);
12212 : : /* FALLTHRU */
12213 : 12372408 : case TRUTH_OR_EXPR:
12214 : : /* If either arg is constant zero, drop it. */
12215 : 12372408 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12216 : 173114 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12217 : 486013 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12218 : : /* Preserve sequence points. */
12219 : 12634059 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12220 : 423639 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12221 : : /* If second arg is constant true, result is true, but we must
12222 : : evaluate first arg. */
12223 : 11775655 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12224 : 51248 : return omit_one_operand_loc (loc, type, arg1, arg0);
12225 : : /* Likewise for first arg, but note this only occurs here for
12226 : : TRUTH_OR_EXPR. */
12227 : 11724407 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12228 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12229 : :
12230 : : /* !X || X is always true. */
12231 : 11724407 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12232 : 11724407 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12233 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12234 : : /* X || !X is always true. */
12235 : 11724407 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12236 : 11724407 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12237 : 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12238 : :
12239 : : /* (X && !Y) || (!X && Y) is X ^ Y */
12240 : 11724406 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12241 : 1628 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12242 : : {
12243 : 681 : tree a0, a1, l0, l1, n0, n1;
12244 : :
12245 : 681 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12246 : 681 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12247 : :
12248 : 681 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12249 : 681 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12250 : :
12251 : 681 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12252 : 681 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12253 : :
12254 : 681 : if ((operand_equal_p (n0, a0, 0)
12255 : 18 : && operand_equal_p (n1, a1, 0))
12256 : 689 : || (operand_equal_p (n0, a1, 0)
12257 : 3 : && operand_equal_p (n1, a0, 0)))
12258 : 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12259 : : }
12260 : :
12261 : 11724393 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12262 : : != NULL_TREE)
12263 : : return tem;
12264 : :
12265 : : return NULL_TREE;
12266 : :
12267 : 39839 : case TRUTH_XOR_EXPR:
12268 : : /* If the second arg is constant zero, drop it. */
12269 : 39839 : if (integer_zerop (arg1))
12270 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12271 : : /* If the second arg is constant true, this is a logical inversion. */
12272 : 39839 : if (integer_onep (arg1))
12273 : : {
12274 : 0 : tem = invert_truthvalue_loc (loc, arg0);
12275 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12276 : : }
12277 : : /* Identical arguments cancel to zero. */
12278 : 39839 : if (operand_equal_p (arg0, arg1, 0))
12279 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12280 : :
12281 : : /* !X ^ X is always true. */
12282 : 39839 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12283 : 39839 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12284 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12285 : :
12286 : : /* X ^ !X is always true. */
12287 : 39839 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12288 : 39839 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12289 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12290 : :
12291 : : return NULL_TREE;
12292 : :
12293 : 46394545 : case EQ_EXPR:
12294 : 46394545 : case NE_EXPR:
12295 : 46394545 : STRIP_NOPS (arg0);
12296 : 46394545 : STRIP_NOPS (arg1);
12297 : :
12298 : 46394545 : tem = fold_comparison (loc, code, type, op0, op1);
12299 : 46394545 : if (tem != NULL_TREE)
12300 : : return tem;
12301 : :
12302 : : /* bool_var != 1 becomes !bool_var. */
12303 : 47518672 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12304 : 46431207 : && code == NE_EXPR)
12305 : 39124 : return fold_convert_loc (loc, type,
12306 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12307 : 78248 : TREE_TYPE (arg0), arg0));
12308 : :
12309 : : /* bool_var == 0 becomes !bool_var. */
12310 : 47440424 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12311 : 47261833 : && code == EQ_EXPR)
12312 : 204673 : return fold_convert_loc (loc, type,
12313 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12314 : 409346 : TREE_TYPE (arg0), arg0));
12315 : :
12316 : : /* !exp != 0 becomes !exp */
12317 : 600315 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12318 : 46743028 : && code == NE_EXPR)
12319 : 592617 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12320 : :
12321 : : /* If this is an EQ or NE comparison with zero and ARG0 is
12322 : : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12323 : : two operations, but the latter can be done in one less insn
12324 : : on machines that have only two-operand insns or on which a
12325 : : constant cannot be the first operand. */
12326 : 45550811 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12327 : 45550811 : && integer_zerop (arg1))
12328 : : {
12329 : 1487331 : tree arg00 = TREE_OPERAND (arg0, 0);
12330 : 1487331 : tree arg01 = TREE_OPERAND (arg0, 1);
12331 : 1487331 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12332 : 1487331 : && integer_onep (TREE_OPERAND (arg00, 0)))
12333 : : {
12334 : 4600 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12335 : 4600 : arg01, TREE_OPERAND (arg00, 1));
12336 : 4600 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12337 : 4600 : build_one_cst (TREE_TYPE (arg0)));
12338 : 4600 : return fold_build2_loc (loc, code, type,
12339 : 4600 : fold_convert_loc (loc, TREE_TYPE (arg1),
12340 : 4600 : tem), arg1);
12341 : : }
12342 : 1482731 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12343 : 1482731 : && integer_onep (TREE_OPERAND (arg01, 0)))
12344 : : {
12345 : 305 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12346 : 305 : arg00, TREE_OPERAND (arg01, 1));
12347 : 305 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12348 : 305 : build_one_cst (TREE_TYPE (arg0)));
12349 : 305 : return fold_build2_loc (loc, code, type,
12350 : 305 : fold_convert_loc (loc, TREE_TYPE (arg1),
12351 : 305 : tem), arg1);
12352 : : }
12353 : : }
12354 : :
12355 : : /* If this is a comparison of a field, we may be able to simplify it. */
12356 : 45545906 : if ((TREE_CODE (arg0) == COMPONENT_REF
12357 : 45545906 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12358 : : /* Handle the constant case even without -O
12359 : : to make sure the warnings are given. */
12360 : 4426055 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12361 : : {
12362 : 4137936 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12363 : 4137936 : if (t1)
12364 : : return t1;
12365 : : }
12366 : :
12367 : : /* Optimize comparisons of strlen vs zero to a compare of the
12368 : : first character of the string vs zero. To wit,
12369 : : strlen(ptr) == 0 => *ptr == 0
12370 : : strlen(ptr) != 0 => *ptr != 0
12371 : : Other cases should reduce to one of these two (or a constant)
12372 : : due to the return value of strlen being unsigned. */
12373 : 44772819 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12374 : : {
12375 : 2561894 : tree fndecl = get_callee_fndecl (arg0);
12376 : :
12377 : 2561894 : if (fndecl
12378 : 2561000 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12379 : 537 : && call_expr_nargs (arg0) == 1
12380 : 2562431 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12381 : : == POINTER_TYPE))
12382 : : {
12383 : 537 : tree ptrtype
12384 : 537 : = build_pointer_type (build_qualified_type (char_type_node,
12385 : : TYPE_QUAL_CONST));
12386 : 1074 : tree ptr = fold_convert_loc (loc, ptrtype,
12387 : 537 : CALL_EXPR_ARG (arg0, 0));
12388 : 537 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12389 : 537 : return fold_build2_loc (loc, code, type, iref,
12390 : 537 : build_int_cst (TREE_TYPE (iref), 0));
12391 : : }
12392 : : }
12393 : :
12394 : : /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12395 : : of X. Similarly fold (X >> C) == 0 into X >= 0. */
12396 : 44772282 : if (TREE_CODE (arg0) == RSHIFT_EXPR
12397 : 38908 : && integer_zerop (arg1)
12398 : 44784285 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12399 : : {
12400 : 9931 : tree arg00 = TREE_OPERAND (arg0, 0);
12401 : 9931 : tree arg01 = TREE_OPERAND (arg0, 1);
12402 : 9931 : tree itype = TREE_TYPE (arg00);
12403 : 9931 : if (wi::to_wide (arg01) == element_precision (itype) - 1)
12404 : : {
12405 : 987 : if (TYPE_UNSIGNED (itype))
12406 : : {
12407 : 906 : itype = signed_type_for (itype);
12408 : 906 : arg00 = fold_convert_loc (loc, itype, arg00);
12409 : : }
12410 : 1920 : return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12411 : 987 : type, arg00, build_zero_cst (itype));
12412 : : }
12413 : : }
12414 : :
12415 : : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12416 : : (X & C) == 0 when C is a single bit. */
12417 : 44771295 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12418 : 1693125 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12419 : 963 : && integer_zerop (arg1)
12420 : 44771860 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12421 : : {
12422 : 250 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12423 : 250 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12424 : 250 : TREE_OPERAND (arg0, 1));
12425 : 390 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12426 : : type, tem,
12427 : 250 : fold_convert_loc (loc, TREE_TYPE (arg0),
12428 : 250 : arg1));
12429 : : }
12430 : :
12431 : : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12432 : : constant C is a power of two, i.e. a single bit. */
12433 : 44771045 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12434 : 4650 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12435 : 0 : && integer_zerop (arg1)
12436 : 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12437 : 44771045 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12438 : 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12439 : : {
12440 : 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12441 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12442 : 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12443 : : }
12444 : :
12445 : : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12446 : : when is C is a power of two, i.e. a single bit. */
12447 : 44771045 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12448 : 1692875 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12449 : 11847 : && integer_zerop (arg1)
12450 : 11847 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12451 : 44780237 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12452 : 9192 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12453 : : {
12454 : 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12455 : 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12456 : 0 : arg000, TREE_OPERAND (arg0, 1));
12457 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12458 : 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12459 : : }
12460 : :
12461 : 44771045 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12462 : 4650 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12463 : : {
12464 : 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12465 : 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12466 : 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12467 : 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12468 : 482 : tree itype = TREE_TYPE (arg0);
12469 : :
12470 : : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12471 : : operand_equal_p guarantees no side-effects so we don't need
12472 : : to use omit_one_operand on Z. */
12473 : 482 : if (operand_equal_p (arg01, arg11, 0))
12474 : 8 : return fold_build2_loc (loc, code, type, arg00,
12475 : 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12476 : 8 : arg10));
12477 : 474 : if (operand_equal_p (arg01, arg10, 0))
12478 : 0 : return fold_build2_loc (loc, code, type, arg00,
12479 : 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12480 : 0 : arg11));
12481 : 474 : if (operand_equal_p (arg00, arg11, 0))
12482 : 0 : return fold_build2_loc (loc, code, type, arg01,
12483 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12484 : 0 : arg10));
12485 : 474 : if (operand_equal_p (arg00, arg10, 0))
12486 : 0 : return fold_build2_loc (loc, code, type, arg01,
12487 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12488 : 0 : arg11));
12489 : :
12490 : : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12491 : 474 : if (TREE_CODE (arg01) == INTEGER_CST
12492 : 8 : && TREE_CODE (arg11) == INTEGER_CST)
12493 : : {
12494 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12495 : : fold_convert_loc (loc, itype, arg11));
12496 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12497 : 8 : return fold_build2_loc (loc, code, type, tem,
12498 : 8 : fold_convert_loc (loc, itype, arg10));
12499 : : }
12500 : : }
12501 : :
12502 : : /* Attempt to simplify equality/inequality comparisons of complex
12503 : : values. Only lower the comparison if the result is known or
12504 : : can be simplified to a single scalar comparison. */
12505 : 44771029 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12506 : 44768502 : || TREE_CODE (arg0) == COMPLEX_CST)
12507 : 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12508 : 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12509 : : {
12510 : 1726 : tree real0, imag0, real1, imag1;
12511 : 1726 : tree rcond, icond;
12512 : :
12513 : 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12514 : : {
12515 : 1726 : real0 = TREE_OPERAND (arg0, 0);
12516 : 1726 : imag0 = TREE_OPERAND (arg0, 1);
12517 : : }
12518 : : else
12519 : : {
12520 : 0 : real0 = TREE_REALPART (arg0);
12521 : 0 : imag0 = TREE_IMAGPART (arg0);
12522 : : }
12523 : :
12524 : 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12525 : : {
12526 : 192 : real1 = TREE_OPERAND (arg1, 0);
12527 : 192 : imag1 = TREE_OPERAND (arg1, 1);
12528 : : }
12529 : : else
12530 : : {
12531 : 1534 : real1 = TREE_REALPART (arg1);
12532 : 1534 : imag1 = TREE_IMAGPART (arg1);
12533 : : }
12534 : :
12535 : 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12536 : 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12537 : : {
12538 : 11 : if (integer_zerop (rcond))
12539 : : {
12540 : 11 : if (code == EQ_EXPR)
12541 : 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12542 : 0 : imag0, imag1);
12543 : 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12544 : : }
12545 : : else
12546 : : {
12547 : 0 : if (code == NE_EXPR)
12548 : 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12549 : 0 : imag0, imag1);
12550 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12551 : : }
12552 : : }
12553 : :
12554 : 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12555 : 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12556 : : {
12557 : 9 : if (integer_zerop (icond))
12558 : : {
12559 : 7 : if (code == EQ_EXPR)
12560 : 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12561 : 1 : real0, real1);
12562 : 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12563 : : }
12564 : : else
12565 : : {
12566 : 2 : if (code == NE_EXPR)
12567 : 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12568 : 1 : real0, real1);
12569 : 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12570 : : }
12571 : : }
12572 : : }
12573 : :
12574 : : return NULL_TREE;
12575 : :
12576 : 37986504 : case LT_EXPR:
12577 : 37986504 : case GT_EXPR:
12578 : 37986504 : case LE_EXPR:
12579 : 37986504 : case GE_EXPR:
12580 : 37986504 : tem = fold_comparison (loc, code, type, op0, op1);
12581 : 37986504 : if (tem != NULL_TREE)
12582 : : return tem;
12583 : :
12584 : : /* Transform comparisons of the form X +- C CMP X. */
12585 : 37146150 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12586 : 4653823 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12587 : 50018 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12588 : 37146168 : && !HONOR_SNANS (arg0))
12589 : : {
12590 : 16 : tree arg01 = TREE_OPERAND (arg0, 1);
12591 : 16 : enum tree_code code0 = TREE_CODE (arg0);
12592 : 16 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12593 : :
12594 : : /* (X - c) > X becomes false. */
12595 : 16 : if (code == GT_EXPR
12596 : 7 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12597 : 3 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12598 : 4 : return constant_boolean_node (0, type);
12599 : :
12600 : : /* Likewise (X + c) < X becomes false. */
12601 : 12 : if (code == LT_EXPR
12602 : 2 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12603 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12604 : 2 : return constant_boolean_node (0, type);
12605 : :
12606 : : /* Convert (X - c) <= X to true. */
12607 : 10 : if (!HONOR_NANS (arg1)
12608 : 6 : && code == LE_EXPR
12609 : 14 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12610 : 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12611 : 4 : return constant_boolean_node (1, type);
12612 : :
12613 : : /* Convert (X + c) >= X to true. */
12614 : 6 : if (!HONOR_NANS (arg1)
12615 : 2 : && code == GE_EXPR
12616 : 8 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12617 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12618 : 2 : return constant_boolean_node (1, type);
12619 : : }
12620 : :
12621 : : /* If we are comparing an ABS_EXPR with a constant, we can
12622 : : convert all the cases into explicit comparisons, but they may
12623 : : well not be faster than doing the ABS and one comparison.
12624 : : But ABS (X) <= C is a range comparison, which becomes a subtraction
12625 : : and a comparison, and is probably faster. */
12626 : 37146138 : if (code == LE_EXPR
12627 : 7064132 : && TREE_CODE (arg1) == INTEGER_CST
12628 : 4989131 : && TREE_CODE (arg0) == ABS_EXPR
12629 : 871 : && ! TREE_SIDE_EFFECTS (arg0)
12630 : 871 : && (tem = negate_expr (arg1)) != 0
12631 : 871 : && TREE_CODE (tem) == INTEGER_CST
12632 : 37147009 : && !TREE_OVERFLOW (tem))
12633 : 1742 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12634 : : build2 (GE_EXPR, type,
12635 : 871 : TREE_OPERAND (arg0, 0), tem),
12636 : : build2 (LE_EXPR, type,
12637 : 1742 : TREE_OPERAND (arg0, 0), arg1));
12638 : :
12639 : : /* Convert ABS_EXPR<x> >= 0 to true. */
12640 : 37145267 : strict_overflow_p = false;
12641 : 37145267 : if (code == GE_EXPR
12642 : 3883000 : && (integer_zerop (arg1)
12643 : 2972103 : || (! HONOR_NANS (arg0)
12644 : 2341259 : && real_zerop (arg1)))
12645 : 38056381 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12646 : : {
12647 : 1144 : if (strict_overflow_p)
12648 : 6 : fold_overflow_warning (("assuming signed overflow does not occur "
12649 : : "when simplifying comparison of "
12650 : : "absolute value and zero"),
12651 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12652 : 1144 : return omit_one_operand_loc (loc, type,
12653 : : constant_boolean_node (true, type),
12654 : 1144 : arg0);
12655 : : }
12656 : :
12657 : : /* Convert ABS_EXPR<x> < 0 to false. */
12658 : 37144123 : strict_overflow_p = false;
12659 : 37144123 : if (code == LT_EXPR
12660 : 12574256 : && (integer_zerop (arg1) || real_zerop (arg1))
12661 : 40100837 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12662 : : {
12663 : 3102 : if (strict_overflow_p)
12664 : 189 : fold_overflow_warning (("assuming signed overflow does not occur "
12665 : : "when simplifying comparison of "
12666 : : "absolute value and zero"),
12667 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12668 : 3102 : return omit_one_operand_loc (loc, type,
12669 : : constant_boolean_node (false, type),
12670 : 3102 : arg0);
12671 : : }
12672 : :
12673 : : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12674 : : and similarly for >= into !=. */
12675 : 37141021 : if ((code == LT_EXPR || code == GE_EXPR)
12676 : 16453010 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12677 : 5096448 : && TREE_CODE (arg1) == LSHIFT_EXPR
12678 : 37142526 : && integer_onep (TREE_OPERAND (arg1, 0)))
12679 : 4058 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12680 : 1357 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12681 : 1357 : TREE_OPERAND (arg1, 1)),
12682 : 2714 : build_zero_cst (TREE_TYPE (arg0)));
12683 : :
12684 : : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12685 : : otherwise Y might be >= # of bits in X's type and thus e.g.
12686 : : (unsigned char) (1 << Y) for Y 15 might be 0.
12687 : : If the cast is widening, then 1 << Y should have unsigned type,
12688 : : otherwise if Y is number of bits in the signed shift type minus 1,
12689 : : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12690 : : 31 might be 0xffffffff80000000. */
12691 : 37139664 : if ((code == LT_EXPR || code == GE_EXPR)
12692 : 16451653 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12693 : 5577961 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12694 : 10896137 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12695 : 3788740 : && CONVERT_EXPR_P (arg1)
12696 : 1078623 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12697 : 42 : && (element_precision (TREE_TYPE (arg1))
12698 : 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12699 : 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12700 : 14 : || (element_precision (TREE_TYPE (arg1))
12701 : 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12702 : 37139671 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12703 : : {
12704 : 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12705 : 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12706 : 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12707 : 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12708 : 14 : build_zero_cst (TREE_TYPE (arg0)));
12709 : : }
12710 : :
12711 : : return NULL_TREE;
12712 : :
12713 : 5632597 : case UNORDERED_EXPR:
12714 : 5632597 : case ORDERED_EXPR:
12715 : 5632597 : case UNLT_EXPR:
12716 : 5632597 : case UNLE_EXPR:
12717 : 5632597 : case UNGT_EXPR:
12718 : 5632597 : case UNGE_EXPR:
12719 : 5632597 : case UNEQ_EXPR:
12720 : 5632597 : case LTGT_EXPR:
12721 : : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12722 : 5632597 : {
12723 : 5632597 : tree targ0 = strip_float_extensions (arg0);
12724 : 5632597 : tree targ1 = strip_float_extensions (arg1);
12725 : 5632597 : tree newtype = TREE_TYPE (targ0);
12726 : :
12727 : 5632597 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12728 : 1289 : newtype = TREE_TYPE (targ1);
12729 : :
12730 : 5632597 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12731 : 5632597 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12732 : 328 : return fold_build2_loc (loc, code, type,
12733 : : fold_convert_loc (loc, newtype, targ0),
12734 : 328 : fold_convert_loc (loc, newtype, targ1));
12735 : : }
12736 : :
12737 : : return NULL_TREE;
12738 : :
12739 : 6285761 : case COMPOUND_EXPR:
12740 : : /* When pedantic, a compound expression can be neither an lvalue
12741 : : nor an integer constant expression. */
12742 : 6285761 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12743 : : return NULL_TREE;
12744 : : /* Don't let (0, 0) be null pointer constant. */
12745 : 452563 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12746 : 452563 : : fold_convert_loc (loc, type, arg1);
12747 : : return tem;
12748 : :
12749 : : default:
12750 : : return NULL_TREE;
12751 : : } /* switch (code) */
12752 : : }
12753 : :
12754 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12755 : : ((A & N) + B) & M -> (A + B) & M
12756 : : Similarly if (N & M) == 0,
12757 : : ((A | N) + B) & M -> (A + B) & M
12758 : : and for - instead of + (or unary - instead of +)
12759 : : and/or ^ instead of |.
12760 : : If B is constant and (B & M) == 0, fold into A & M.
12761 : :
12762 : : This function is a helper for match.pd patterns. Return non-NULL
12763 : : type in which the simplified operation should be performed only
12764 : : if any optimization is possible.
12765 : :
12766 : : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12767 : : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12768 : : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12769 : : +/-. */
12770 : : tree
12771 : 1248558 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12772 : : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12773 : : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12774 : : tree *pmop)
12775 : : {
12776 : 1248558 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12777 : 1248558 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12778 : 1248558 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12779 : 2497116 : if (~cst1 == 0
12780 : 3739568 : || (cst1 & (cst1 + 1)) != 0
12781 : 1075461 : || !INTEGRAL_TYPE_P (type)
12782 : 1075461 : || (!TYPE_OVERFLOW_WRAPS (type)
12783 : 39952 : && TREE_CODE (type) != INTEGER_TYPE)
12784 : 4644985 : || (wi::max_value (type) & cst1) != cst1)
12785 : : return NULL_TREE;
12786 : :
12787 : 1075461 : enum tree_code codes[2] = { code00, code01 };
12788 : 1075461 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12789 : 1075461 : int which = 0;
12790 : 1075461 : wide_int cst0;
12791 : :
12792 : : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12793 : : arg1 (M) is == (1LL << cst) - 1.
12794 : : Store C into PMOP[0] and D into PMOP[1]. */
12795 : 1075461 : pmop[0] = arg00;
12796 : 1075461 : pmop[1] = arg01;
12797 : 1075461 : which = code != NEGATE_EXPR;
12798 : :
12799 : 3225487 : for (; which >= 0; which--)
12800 : 2150026 : switch (codes[which])
12801 : : {
12802 : 20671 : case BIT_AND_EXPR:
12803 : 20671 : case BIT_IOR_EXPR:
12804 : 20671 : case BIT_XOR_EXPR:
12805 : 20671 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12806 : 20671 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12807 : 20671 : if (codes[which] == BIT_AND_EXPR)
12808 : : {
12809 : 20557 : if (cst0 != cst1)
12810 : : break;
12811 : : }
12812 : 114 : else if (cst0 != 0)
12813 : : break;
12814 : : /* If C or D is of the form (A & N) where
12815 : : (N & M) == M, or of the form (A | N) or
12816 : : (A ^ N) where (N & M) == 0, replace it with A. */
12817 : 19130 : pmop[which] = arg0xx[2 * which];
12818 : 19130 : break;
12819 : 2129355 : case ERROR_MARK:
12820 : 2129355 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12821 : : break;
12822 : : /* If C or D is a N where (N & M) == 0, it can be
12823 : : omitted (replaced with 0). */
12824 : 893294 : if ((code == PLUS_EXPR
12825 : 207586 : || (code == MINUS_EXPR && which == 0))
12826 : 654233 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12827 : 142877 : pmop[which] = build_int_cst (type, 0);
12828 : : /* Similarly, with C - N where (-N & M) == 0. */
12829 : 893294 : if (code == MINUS_EXPR
12830 : 446647 : && which == 1
12831 : 646924 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12832 : 191220 : pmop[which] = build_int_cst (type, 0);
12833 : : break;
12834 : 0 : default:
12835 : 0 : gcc_unreachable ();
12836 : : }
12837 : :
12838 : : /* Only build anything new if we optimized one or both arguments above. */
12839 : 1075461 : if (pmop[0] == arg00 && pmop[1] == arg01)
12840 : : return NULL_TREE;
12841 : :
12842 : 352506 : if (TYPE_OVERFLOW_WRAPS (type))
12843 : : return type;
12844 : : else
12845 : 2444 : return unsigned_type_for (type);
12846 : 1075461 : }
12847 : :
12848 : : /* Used by contains_label_[p1]. */
12849 : :
12850 : : struct contains_label_data
12851 : : {
12852 : : hash_set<tree> *pset;
12853 : : bool inside_switch_p;
12854 : : };
12855 : :
12856 : : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12857 : : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12858 : : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12859 : :
12860 : : static tree
12861 : 4211818 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12862 : : {
12863 : 4211818 : contains_label_data *d = (contains_label_data *) data;
12864 : 4211818 : switch (TREE_CODE (*tp))
12865 : : {
12866 : : case LABEL_EXPR:
12867 : : return *tp;
12868 : :
12869 : 0 : case CASE_LABEL_EXPR:
12870 : 0 : if (!d->inside_switch_p)
12871 : : return *tp;
12872 : : return NULL_TREE;
12873 : :
12874 : 0 : case SWITCH_EXPR:
12875 : 0 : if (!d->inside_switch_p)
12876 : : {
12877 : 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12878 : 0 : return *tp;
12879 : 0 : d->inside_switch_p = true;
12880 : 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12881 : 0 : return *tp;
12882 : 0 : d->inside_switch_p = false;
12883 : 0 : *walk_subtrees = 0;
12884 : : }
12885 : : return NULL_TREE;
12886 : :
12887 : 6031 : case GOTO_EXPR:
12888 : 6031 : *walk_subtrees = 0;
12889 : 6031 : return NULL_TREE;
12890 : :
12891 : : default:
12892 : : return NULL_TREE;
12893 : : }
12894 : : }
12895 : :
12896 : : /* Return whether the sub-tree ST contains a label which is accessible from
12897 : : outside the sub-tree. */
12898 : :
12899 : : static bool
12900 : 300022 : contains_label_p (tree st)
12901 : : {
12902 : 300022 : hash_set<tree> pset;
12903 : 300022 : contains_label_data data = { &pset, false };
12904 : 300022 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12905 : 300022 : }
12906 : :
12907 : : /* Fold a ternary expression of code CODE and type TYPE with operands
12908 : : OP0, OP1, and OP2. Return the folded expression if folding is
12909 : : successful. Otherwise, return NULL_TREE. */
12910 : :
12911 : : tree
12912 : 28517229 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12913 : : tree op0, tree op1, tree op2)
12914 : : {
12915 : 28517229 : tree tem;
12916 : 28517229 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12917 : 28517229 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12918 : :
12919 : 28517229 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12920 : : && TREE_CODE_LENGTH (code) == 3);
12921 : :
12922 : : /* If this is a commutative operation, and OP0 is a constant, move it
12923 : : to OP1 to reduce the number of tests below. */
12924 : 28517229 : if (commutative_ternary_tree_code (code)
12925 : 28517229 : && tree_swap_operands_p (op0, op1))
12926 : 28 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12927 : :
12928 : 28517201 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12929 : 28517201 : if (tem)
12930 : : return tem;
12931 : :
12932 : : /* Strip any conversions that don't change the mode. This is safe
12933 : : for every expression, except for a comparison expression because
12934 : : its signedness is derived from its operands. So, in the latter
12935 : : case, only strip conversions that don't change the signedness.
12936 : :
12937 : : Note that this is done as an internal manipulation within the
12938 : : constant folder, in order to find the simplest representation of
12939 : : the arguments so that their form can be studied. In any cases,
12940 : : the appropriate type conversions should be put back in the tree
12941 : : that will get out of the constant folder. */
12942 : 27580068 : if (op0)
12943 : : {
12944 : 27515386 : arg0 = op0;
12945 : 27515386 : STRIP_NOPS (arg0);
12946 : : }
12947 : :
12948 : 27580068 : if (op1)
12949 : : {
12950 : 27580068 : arg1 = op1;
12951 : 27580068 : STRIP_NOPS (arg1);
12952 : : }
12953 : :
12954 : 27580068 : if (op2)
12955 : : {
12956 : 12999000 : arg2 = op2;
12957 : 12999000 : STRIP_NOPS (arg2);
12958 : : }
12959 : :
12960 : 27580068 : switch (code)
12961 : : {
12962 : 14580593 : case COMPONENT_REF:
12963 : 14580593 : if (TREE_CODE (arg0) == CONSTRUCTOR
12964 : 14580593 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12965 : : {
12966 : : unsigned HOST_WIDE_INT idx;
12967 : : tree field, value;
12968 : 870 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12969 : 672 : if (field == arg1)
12970 : : return value;
12971 : : }
12972 : : return NULL_TREE;
12973 : :
12974 : 10918054 : case COND_EXPR:
12975 : 10918054 : case VEC_COND_EXPR:
12976 : : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12977 : : so all simple results must be passed through pedantic_non_lvalue. */
12978 : 10918054 : if (TREE_CODE (arg0) == INTEGER_CST)
12979 : : {
12980 : 422525 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12981 : 422525 : tem = integer_zerop (arg0) ? op2 : op1;
12982 : : /* Only optimize constant conditions when the selected branch
12983 : : has the same type as the COND_EXPR. This avoids optimizing
12984 : : away "c ? x : throw", where the throw has a void type.
12985 : : Avoid throwing away that operand which contains label. */
12986 : 422525 : if ((!TREE_SIDE_EFFECTS (unused_op)
12987 : 300022 : || !contains_label_p (unused_op))
12988 : 718167 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12989 : 341223 : || VOID_TYPE_P (type)))
12990 : 408941 : return protected_set_expr_location_unshare (tem, loc);
12991 : 13584 : return NULL_TREE;
12992 : : }
12993 : 10495529 : else if (TREE_CODE (arg0) == VECTOR_CST)
12994 : : {
12995 : 9720 : unsigned HOST_WIDE_INT nelts;
12996 : 9720 : if ((TREE_CODE (arg1) == VECTOR_CST
12997 : 7499 : || TREE_CODE (arg1) == CONSTRUCTOR)
12998 : 2221 : && (TREE_CODE (arg2) == VECTOR_CST
12999 : 55 : || TREE_CODE (arg2) == CONSTRUCTOR)
13000 : 19440 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
13001 : : {
13002 : 2166 : vec_perm_builder sel (nelts, nelts, 1);
13003 : 22658 : for (unsigned int i = 0; i < nelts; i++)
13004 : : {
13005 : 20492 : tree val = VECTOR_CST_ELT (arg0, i);
13006 : 20492 : if (integer_all_onesp (val))
13007 : 10324 : sel.quick_push (i);
13008 : 10168 : else if (integer_zerop (val))
13009 : 10168 : sel.quick_push (nelts + i);
13010 : : else /* Currently unreachable. */
13011 : 1694 : return NULL_TREE;
13012 : : }
13013 : 2166 : vec_perm_indices indices (sel, 2, nelts);
13014 : 2166 : tree t = fold_vec_perm (type, arg1, arg2, indices);
13015 : 2166 : if (t != NULL_TREE)
13016 : 1694 : return t;
13017 : 3860 : }
13018 : : }
13019 : :
13020 : : /* If we have A op B ? A : C, we may be able to convert this to a
13021 : : simpler expression, depending on the operation and the values
13022 : : of B and C. Signed zeros prevent all of these transformations,
13023 : : for reasons given above each one.
13024 : :
13025 : : Also try swapping the arguments and inverting the conditional. */
13026 : 10493835 : if (COMPARISON_CLASS_P (arg0)
13027 : 8791210 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
13028 : 10613608 : && !HONOR_SIGNED_ZEROS (op1))
13029 : : {
13030 : 109128 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
13031 : 109128 : TREE_OPERAND (arg0, 0),
13032 : 109128 : TREE_OPERAND (arg0, 1),
13033 : : op1, op2);
13034 : 109128 : if (tem)
13035 : : return tem;
13036 : : }
13037 : :
13038 : 10487365 : if (COMPARISON_CLASS_P (arg0)
13039 : 8784740 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
13040 : 10921309 : && !HONOR_SIGNED_ZEROS (op2))
13041 : : {
13042 : 340255 : enum tree_code comp_code = TREE_CODE (arg0);
13043 : 340255 : tree arg00 = TREE_OPERAND (arg0, 0);
13044 : 340255 : tree arg01 = TREE_OPERAND (arg0, 1);
13045 : 340255 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
13046 : 340255 : if (comp_code != ERROR_MARK)
13047 : 340255 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
13048 : : arg00,
13049 : : arg01,
13050 : : op2, op1);
13051 : 340255 : if (tem)
13052 : : return tem;
13053 : : }
13054 : :
13055 : : /* If the second operand is simpler than the third, swap them
13056 : : since that produces better jump optimization results. */
13057 : 10240158 : if (truth_value_p (TREE_CODE (arg0))
13058 : 10240158 : && tree_swap_operands_p (op1, op2))
13059 : : {
13060 : 1735679 : location_t loc0 = expr_location_or (arg0, loc);
13061 : : /* See if this can be inverted. If it can't, possibly because
13062 : : it was a floating-point inequality comparison, don't do
13063 : : anything. */
13064 : 1735679 : tem = fold_invert_truthvalue (loc0, arg0);
13065 : 1735679 : if (tem)
13066 : 1152432 : return fold_build3_loc (loc, code, type, tem, op2, op1);
13067 : : }
13068 : :
13069 : : /* Convert A ? 1 : 0 to simply A. */
13070 : 9087726 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13071 : 8757909 : : (integer_onep (op1)
13072 : 394256 : && !VECTOR_TYPE_P (type)))
13073 : 595258 : && integer_zerop (op2)
13074 : : /* If we try to convert OP0 to our type, the
13075 : : call to fold will try to move the conversion inside
13076 : : a COND, which will recurse. In that case, the COND_EXPR
13077 : : is probably the best choice, so leave it alone. */
13078 : 10000837 : && type == TREE_TYPE (arg0))
13079 : 32005 : return protected_set_expr_location_unshare (arg0, loc);
13080 : :
13081 : : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13082 : : over COND_EXPR in cases such as floating point comparisons. */
13083 : 9055721 : if (integer_zerop (op1)
13084 : 253753 : && code == COND_EXPR
13085 : 251991 : && integer_onep (op2)
13086 : 33311 : && !VECTOR_TYPE_P (type)
13087 : 9089032 : && truth_value_p (TREE_CODE (arg0)))
13088 : 31468 : return fold_convert_loc (loc, type,
13089 : 31468 : invert_truthvalue_loc (loc, arg0));
13090 : :
13091 : : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13092 : 9024253 : if (TREE_CODE (arg0) == LT_EXPR
13093 : 1008416 : && integer_zerop (TREE_OPERAND (arg0, 1))
13094 : 16127 : && integer_zerop (op2)
13095 : 9025182 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13096 : : {
13097 : : /* sign_bit_p looks through both zero and sign extensions,
13098 : : but for this optimization only sign extensions are
13099 : : usable. */
13100 : 56 : tree tem2 = TREE_OPERAND (arg0, 0);
13101 : 56 : while (tem != tem2)
13102 : : {
13103 : 0 : if (TREE_CODE (tem2) != NOP_EXPR
13104 : 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13105 : : {
13106 : : tem = NULL_TREE;
13107 : : break;
13108 : : }
13109 : 0 : tem2 = TREE_OPERAND (tem2, 0);
13110 : : }
13111 : : /* sign_bit_p only checks ARG1 bits within A's precision.
13112 : : If <sign bit of A> has wider type than A, bits outside
13113 : : of A's precision in <sign bit of A> need to be checked.
13114 : : If they are all 0, this optimization needs to be done
13115 : : in unsigned A's type, if they are all 1 in signed A's type,
13116 : : otherwise this can't be done. */
13117 : 56 : if (tem
13118 : 56 : && TYPE_PRECISION (TREE_TYPE (tem))
13119 : 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
13120 : 112 : && TYPE_PRECISION (TREE_TYPE (tem))
13121 : 56 : < TYPE_PRECISION (type))
13122 : : {
13123 : 56 : int inner_width, outer_width;
13124 : 56 : tree tem_type;
13125 : :
13126 : 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13127 : 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13128 : 56 : if (outer_width > TYPE_PRECISION (type))
13129 : 0 : outer_width = TYPE_PRECISION (type);
13130 : :
13131 : 56 : wide_int mask = wi::shifted_mask
13132 : 56 : (inner_width, outer_width - inner_width, false,
13133 : 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13134 : :
13135 : 56 : wide_int common = mask & wi::to_wide (arg1);
13136 : 56 : if (common == mask)
13137 : : {
13138 : 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13139 : 28 : tem = fold_convert_loc (loc, tem_type, tem);
13140 : : }
13141 : 28 : else if (common == 0)
13142 : : {
13143 : 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13144 : 0 : tem = fold_convert_loc (loc, tem_type, tem);
13145 : : }
13146 : : else
13147 : : tem = NULL;
13148 : 56 : }
13149 : :
13150 : 56 : if (tem)
13151 : 28 : return
13152 : 56 : fold_convert_loc (loc, type,
13153 : : fold_build2_loc (loc, BIT_AND_EXPR,
13154 : 28 : TREE_TYPE (tem), tem,
13155 : : fold_convert_loc (loc,
13156 : 28 : TREE_TYPE (tem),
13157 : 28 : arg1)));
13158 : : }
13159 : :
13160 : : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13161 : : already handled above. */
13162 : 9024225 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13163 : 347 : && integer_onep (TREE_OPERAND (arg0, 1))
13164 : 3 : && integer_zerop (op2)
13165 : 9024225 : && integer_pow2p (arg1))
13166 : : {
13167 : 0 : tree tem = TREE_OPERAND (arg0, 0);
13168 : 0 : STRIP_NOPS (tem);
13169 : 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13170 : 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13171 : 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13172 : 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13173 : 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13174 : : fold_convert_loc (loc, type,
13175 : 0 : TREE_OPERAND (tem, 0)),
13176 : 0 : op1);
13177 : : }
13178 : :
13179 : : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13180 : : is probably obsolete because the first operand should be a
13181 : : truth value (that's why we have the two cases above), but let's
13182 : : leave it in until we can confirm this for all front-ends. */
13183 : 9024225 : if (integer_zerop (op2)
13184 : 1941252 : && TREE_CODE (arg0) == NE_EXPR
13185 : 543606 : && integer_zerop (TREE_OPERAND (arg0, 1))
13186 : 297740 : && integer_pow2p (arg1)
13187 : 31708 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13188 : 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13189 : : arg1, OEP_ONLY_CONST)
13190 : : /* operand_equal_p compares just value, not precision, so e.g.
13191 : : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13192 : : second operand 32-bit -128, which is not a power of two (or vice
13193 : : versa. */
13194 : 9024225 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13195 : 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13196 : :
13197 : : /* Disable the transformations below for vectors, since
13198 : : fold_binary_op_with_conditional_arg may undo them immediately,
13199 : : yielding an infinite loop. */
13200 : 9024225 : if (code == VEC_COND_EXPR)
13201 : : return NULL_TREE;
13202 : :
13203 : : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13204 : 8694408 : if (integer_zerop (op2)
13205 : 1674724 : && truth_value_p (TREE_CODE (arg0))
13206 : 1551606 : && truth_value_p (TREE_CODE (arg1))
13207 : 8727152 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13208 : 32744 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13209 : : : TRUTH_ANDIF_EXPR,
13210 : 32744 : type, fold_convert_loc (loc, type, arg0), op1);
13211 : :
13212 : : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13213 : 8661664 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13214 : 458259 : && truth_value_p (TREE_CODE (arg0))
13215 : 320955 : && truth_value_p (TREE_CODE (arg1))
13216 : 8698138 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13217 : : {
13218 : 36474 : location_t loc0 = expr_location_or (arg0, loc);
13219 : : /* Only perform transformation if ARG0 is easily inverted. */
13220 : 36474 : tem = fold_invert_truthvalue (loc0, arg0);
13221 : 36474 : if (tem)
13222 : 36210 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13223 : : ? BIT_IOR_EXPR
13224 : : : TRUTH_ORIF_EXPR,
13225 : : type, fold_convert_loc (loc, type, tem),
13226 : 36210 : op1);
13227 : : }
13228 : :
13229 : : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13230 : 8625454 : if (integer_zerop (arg1)
13231 : 220602 : && truth_value_p (TREE_CODE (arg0))
13232 : 49110 : && truth_value_p (TREE_CODE (op2))
13233 : 8625482 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13234 : : {
13235 : 28 : location_t loc0 = expr_location_or (arg0, loc);
13236 : : /* Only perform transformation if ARG0 is easily inverted. */
13237 : 28 : tem = fold_invert_truthvalue (loc0, arg0);
13238 : 28 : if (tem)
13239 : 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13240 : : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13241 : : type, fold_convert_loc (loc, type, tem),
13242 : 0 : op2);
13243 : : }
13244 : :
13245 : : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13246 : 8625454 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13247 : 362251 : && truth_value_p (TREE_CODE (arg0))
13248 : 281701 : && truth_value_p (TREE_CODE (op2))
13249 : 8625640 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13250 : 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13251 : : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13252 : 186 : type, fold_convert_loc (loc, type, arg0), op2);
13253 : :
13254 : : return NULL_TREE;
13255 : :
13256 : 0 : case CALL_EXPR:
13257 : : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13258 : : of fold_ternary on them. */
13259 : 0 : gcc_unreachable ();
13260 : :
13261 : 641541 : case BIT_FIELD_REF:
13262 : 641541 : if (TREE_CODE (arg0) == VECTOR_CST
13263 : 28420 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13264 : 1810 : || (VECTOR_TYPE_P (type)
13265 : 1150 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13266 : 27740 : && tree_fits_uhwi_p (op1)
13267 : 669281 : && tree_fits_uhwi_p (op2))
13268 : : {
13269 : 27740 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13270 : 27740 : unsigned HOST_WIDE_INT width
13271 : 27740 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13272 : 27740 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13273 : 27740 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13274 : 27740 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13275 : :
13276 : 27740 : if (n != 0
13277 : 27740 : && (idx % width) == 0
13278 : 27740 : && (n % width) == 0
13279 : 55480 : && known_le ((idx + n) / width,
13280 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13281 : : {
13282 : 27740 : idx = idx / width;
13283 : 27740 : n = n / width;
13284 : :
13285 : 27740 : if (TREE_CODE (arg0) == VECTOR_CST)
13286 : : {
13287 : 27740 : if (n == 1)
13288 : : {
13289 : 26614 : tem = VECTOR_CST_ELT (arg0, idx);
13290 : 26614 : if (VECTOR_TYPE_P (type))
13291 : 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13292 : 26614 : return tem;
13293 : : }
13294 : :
13295 : 1126 : tree_vector_builder vals (type, n, 1);
13296 : 6934 : for (unsigned i = 0; i < n; ++i)
13297 : 5808 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13298 : 1126 : return vals.build ();
13299 : 1126 : }
13300 : : }
13301 : : }
13302 : :
13303 : : /* On constants we can use native encode/interpret to constant
13304 : : fold (nearly) all BIT_FIELD_REFs. */
13305 : 613801 : if (CONSTANT_CLASS_P (arg0)
13306 : 1481 : && can_native_interpret_type_p (type)
13307 : : && BITS_PER_UNIT == 8
13308 : 1481 : && tree_fits_uhwi_p (op1)
13309 : 615282 : && tree_fits_uhwi_p (op2))
13310 : : {
13311 : 1481 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13312 : 1481 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13313 : : /* Limit us to a reasonable amount of work. To relax the
13314 : : other limitations we need bit-shifting of the buffer
13315 : : and rounding up the size. */
13316 : 1481 : if (bitpos % BITS_PER_UNIT == 0
13317 : 1481 : && bitsize % BITS_PER_UNIT == 0
13318 : 1481 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13319 : : {
13320 : 1481 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13321 : 1481 : unsigned HOST_WIDE_INT len
13322 : 1481 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13323 : 1481 : bitpos / BITS_PER_UNIT);
13324 : 1481 : if (len > 0
13325 : 1481 : && len * BITS_PER_UNIT >= bitsize)
13326 : : {
13327 : 1481 : tree v = native_interpret_expr (type, b,
13328 : : bitsize / BITS_PER_UNIT);
13329 : 1481 : if (v)
13330 : 1475 : return v;
13331 : : }
13332 : : }
13333 : : }
13334 : :
13335 : : return NULL_TREE;
13336 : :
13337 : 714179 : case VEC_PERM_EXPR:
13338 : : /* Perform constant folding of BIT_INSERT_EXPR. */
13339 : 714179 : if (TREE_CODE (arg2) == VECTOR_CST
13340 : 702947 : && TREE_CODE (op0) == VECTOR_CST
13341 : 15232 : && TREE_CODE (op1) == VECTOR_CST)
13342 : : {
13343 : : /* Build a vector of integers from the tree mask. */
13344 : 3915 : vec_perm_builder builder;
13345 : 3915 : if (!tree_to_vec_perm_builder (&builder, arg2))
13346 : : return NULL_TREE;
13347 : :
13348 : : /* Create a vec_perm_indices for the integer vector. */
13349 : 3915 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13350 : 3915 : bool single_arg = (op0 == op1);
13351 : 7830 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13352 : 3915 : return fold_vec_perm (type, op0, op1, sel);
13353 : 7830 : }
13354 : : return NULL_TREE;
13355 : :
13356 : 14304 : case BIT_INSERT_EXPR:
13357 : : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13358 : 14304 : if (TREE_CODE (arg0) == INTEGER_CST
13359 : 14 : && TREE_CODE (arg1) == INTEGER_CST)
13360 : : {
13361 : 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13362 : 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13363 : 2 : if (BYTES_BIG_ENDIAN)
13364 : : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13365 : 2 : wide_int tem = (wi::to_wide (arg0)
13366 : 4 : & wi::shifted_mask (bitpos, bitsize, true,
13367 : 4 : TYPE_PRECISION (type)));
13368 : 2 : wide_int tem2
13369 : 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13370 : 2 : bitsize), bitpos);
13371 : 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13372 : 2 : }
13373 : 14302 : else if (TREE_CODE (arg0) == VECTOR_CST
13374 : 906 : && CONSTANT_CLASS_P (arg1)
13375 : 14604 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13376 : 302 : TREE_TYPE (arg1)))
13377 : : {
13378 : 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13379 : 302 : unsigned HOST_WIDE_INT elsize
13380 : 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13381 : 302 : if (bitpos % elsize == 0)
13382 : : {
13383 : 302 : unsigned k = bitpos / elsize;
13384 : 302 : unsigned HOST_WIDE_INT nelts;
13385 : 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13386 : 28517229 : return arg0;
13387 : 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13388 : : {
13389 : 290 : tree_vector_builder elts (type, nelts, 1);
13390 : 290 : elts.quick_grow (nelts);
13391 : 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13392 : 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13393 : 290 : return elts.build ();
13394 : 290 : }
13395 : : }
13396 : : }
13397 : : return NULL_TREE;
13398 : :
13399 : : default:
13400 : : return NULL_TREE;
13401 : : } /* switch (code) */
13402 : : }
13403 : :
13404 : : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13405 : : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13406 : : constructor element index of the value returned. If the element is
13407 : : not found NULL_TREE is returned and *CTOR_IDX is updated to
13408 : : the index of the element after the ACCESS_INDEX position (which
13409 : : may be outside of the CTOR array). */
13410 : :
13411 : : tree
13412 : 715704 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13413 : : unsigned *ctor_idx)
13414 : : {
13415 : 715704 : tree index_type = NULL_TREE;
13416 : 715704 : signop index_sgn = UNSIGNED;
13417 : 715704 : offset_int low_bound = 0;
13418 : :
13419 : 715704 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13420 : : {
13421 : 715704 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13422 : 715704 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13423 : : {
13424 : : /* Static constructors for variably sized objects makes no sense. */
13425 : 715704 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13426 : 715704 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13427 : : /* ??? When it is obvious that the range is signed, treat it so. */
13428 : 715704 : if (TYPE_UNSIGNED (index_type)
13429 : 365074 : && TYPE_MAX_VALUE (domain_type)
13430 : 1080747 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13431 : 365043 : TYPE_MIN_VALUE (domain_type)))
13432 : : {
13433 : 0 : index_sgn = SIGNED;
13434 : 0 : low_bound
13435 : 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13436 : : SIGNED);
13437 : : }
13438 : : else
13439 : : {
13440 : 715704 : index_sgn = TYPE_SIGN (index_type);
13441 : 715704 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13442 : : }
13443 : : }
13444 : : }
13445 : :
13446 : 715704 : if (index_type)
13447 : 715704 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13448 : : index_sgn);
13449 : :
13450 : 715704 : offset_int index = low_bound;
13451 : 715704 : if (index_type)
13452 : 715704 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13453 : :
13454 : 715704 : offset_int max_index = index;
13455 : 715704 : unsigned cnt;
13456 : 715704 : tree cfield, cval;
13457 : 715704 : bool first_p = true;
13458 : :
13459 : 15083904 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13460 : : {
13461 : : /* Array constructor might explicitly set index, or specify a range,
13462 : : or leave index NULL meaning that it is next index after previous
13463 : : one. */
13464 : 15082705 : if (cfield)
13465 : : {
13466 : 6939296 : if (TREE_CODE (cfield) == INTEGER_CST)
13467 : 13877108 : max_index = index
13468 : 6938554 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13469 : : else
13470 : : {
13471 : 742 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13472 : 742 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13473 : : index_sgn);
13474 : 742 : max_index
13475 : 742 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13476 : : index_sgn);
13477 : 742 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13478 : : }
13479 : : }
13480 : 8143409 : else if (!first_p)
13481 : : {
13482 : 7888358 : index = max_index + 1;
13483 : 7888358 : if (index_type)
13484 : 7888358 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13485 : 7888358 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13486 : 7888358 : max_index = index;
13487 : : }
13488 : : else
13489 : : first_p = false;
13490 : :
13491 : 15082705 : if (TREE_CODE (cval) == RAW_DATA_CST)
13492 : 2630 : max_index += RAW_DATA_LENGTH (cval) - 1;
13493 : :
13494 : : /* Do we have match? */
13495 : 15082705 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13496 : : {
13497 : 15082414 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13498 : : {
13499 : 714386 : if (ctor_idx)
13500 : 714386 : *ctor_idx = cnt;
13501 : 714386 : return cval;
13502 : : }
13503 : : }
13504 : 291 : else if (in_gimple_form)
13505 : : /* We're past the element we search for. Note during parsing
13506 : : the elements might not be sorted.
13507 : : ??? We should use a binary search and a flag on the
13508 : : CONSTRUCTOR as to whether elements are sorted in declaration
13509 : : order. */
13510 : : break;
13511 : : }
13512 : 1318 : if (ctor_idx)
13513 : 1318 : *ctor_idx = cnt;
13514 : : return NULL_TREE;
13515 : : }
13516 : :
13517 : : /* Perform constant folding and related simplification of EXPR.
13518 : : The related simplifications include x*1 => x, x*0 => 0, etc.,
13519 : : and application of the associative law.
13520 : : NOP_EXPR conversions may be removed freely (as long as we
13521 : : are careful not to change the type of the overall expression).
13522 : : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13523 : : but we can constant-fold them if they have constant operands. */
13524 : :
13525 : : #ifdef ENABLE_FOLD_CHECKING
13526 : : # define fold(x) fold_1 (x)
13527 : : static tree fold_1 (tree);
13528 : : static
13529 : : #endif
13530 : : tree
13531 : 1234569765 : fold (tree expr)
13532 : : {
13533 : 1234739568 : const tree t = expr;
13534 : 1234739568 : enum tree_code code = TREE_CODE (t);
13535 : 1234739568 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13536 : 1234739568 : tree tem;
13537 : 1234739568 : location_t loc = EXPR_LOCATION (expr);
13538 : :
13539 : : /* Return right away if a constant. */
13540 : 1234739568 : if (kind == tcc_constant)
13541 : : return t;
13542 : :
13543 : : /* CALL_EXPR-like objects with variable numbers of operands are
13544 : : treated specially. */
13545 : 1134170333 : if (kind == tcc_vl_exp)
13546 : : {
13547 : 164355421 : if (code == CALL_EXPR)
13548 : : {
13549 : 164354874 : tem = fold_call_expr (loc, expr, false);
13550 : 326060763 : return tem ? tem : expr;
13551 : : }
13552 : : return expr;
13553 : : }
13554 : :
13555 : 969814912 : if (IS_EXPR_CODE_CLASS (kind))
13556 : : {
13557 : 967687101 : tree type = TREE_TYPE (t);
13558 : 967687101 : tree op0, op1, op2;
13559 : :
13560 : 967687101 : switch (TREE_CODE_LENGTH (code))
13561 : : {
13562 : 880903583 : case 1:
13563 : 880903583 : op0 = TREE_OPERAND (t, 0);
13564 : 880903583 : tem = fold_unary_loc (loc, code, type, op0);
13565 : 1502631555 : return tem ? tem : expr;
13566 : 77962297 : case 2:
13567 : 77962297 : op0 = TREE_OPERAND (t, 0);
13568 : 77962297 : op1 = TREE_OPERAND (t, 1);
13569 : 77962297 : tem = fold_binary_loc (loc, code, type, op0, op1);
13570 : 147278895 : return tem ? tem : expr;
13571 : 4225074 : case 3:
13572 : 4225074 : op0 = TREE_OPERAND (t, 0);
13573 : 4225074 : op1 = TREE_OPERAND (t, 1);
13574 : 4225074 : op2 = TREE_OPERAND (t, 2);
13575 : 4225074 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13576 : 8187241 : return tem ? tem : expr;
13577 : : default:
13578 : : break;
13579 : : }
13580 : : }
13581 : :
13582 : 6723958 : switch (code)
13583 : : {
13584 : 4492428 : case ARRAY_REF:
13585 : 4492428 : {
13586 : 4492428 : tree op0 = TREE_OPERAND (t, 0);
13587 : 4492428 : tree op1 = TREE_OPERAND (t, 1);
13588 : :
13589 : 4492428 : if (TREE_CODE (op1) == INTEGER_CST
13590 : 2920191 : && TREE_CODE (op0) == CONSTRUCTOR
13591 : 4493883 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13592 : : {
13593 : 1455 : unsigned int idx;
13594 : 1455 : tree val
13595 : 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13596 : : &idx);
13597 : 1455 : if (val)
13598 : : {
13599 : 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13600 : : return val;
13601 : 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13602 : 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13603 : : != INTEGER_CST))
13604 : : return t;
13605 : 2 : offset_int o
13606 : 2 : = (wi::to_offset (op1)
13607 : 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13608 : 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13609 : 2 : return build_int_cst (TREE_TYPE (val),
13610 : 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13611 : : }
13612 : : }
13613 : :
13614 : : return t;
13615 : : }
13616 : :
13617 : : /* Return a VECTOR_CST if possible. */
13618 : 114336 : case CONSTRUCTOR:
13619 : 114336 : {
13620 : 114336 : tree type = TREE_TYPE (t);
13621 : 114336 : if (TREE_CODE (type) != VECTOR_TYPE)
13622 : : return t;
13623 : :
13624 : : unsigned i;
13625 : : tree val;
13626 : 279237 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13627 : 241911 : if (! CONSTANT_CLASS_P (val))
13628 : : return t;
13629 : :
13630 : 37326 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13631 : : }
13632 : :
13633 : 169803 : case CONST_DECL:
13634 : 169803 : return fold (DECL_INITIAL (t));
13635 : :
13636 : : default:
13637 : : return t;
13638 : : } /* switch (code) */
13639 : : }
13640 : :
13641 : : #ifdef ENABLE_FOLD_CHECKING
13642 : : #undef fold
13643 : :
13644 : : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13645 : : hash_table<nofree_ptr_hash<const tree_node> > *);
13646 : : static void fold_check_failed (const_tree, const_tree);
13647 : : void print_fold_checksum (const_tree);
13648 : :
13649 : : /* When --enable-checking=fold, compute a digest of expr before
13650 : : and after actual fold call to see if fold did not accidentally
13651 : : change original expr. */
13652 : :
13653 : : tree
13654 : : fold (tree expr)
13655 : : {
13656 : : tree ret;
13657 : : struct md5_ctx ctx;
13658 : : unsigned char checksum_before[16], checksum_after[16];
13659 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13660 : :
13661 : : md5_init_ctx (&ctx);
13662 : : fold_checksum_tree (expr, &ctx, &ht);
13663 : : md5_finish_ctx (&ctx, checksum_before);
13664 : : ht.empty ();
13665 : :
13666 : : ret = fold_1 (expr);
13667 : :
13668 : : md5_init_ctx (&ctx);
13669 : : fold_checksum_tree (expr, &ctx, &ht);
13670 : : md5_finish_ctx (&ctx, checksum_after);
13671 : :
13672 : : if (memcmp (checksum_before, checksum_after, 16))
13673 : : fold_check_failed (expr, ret);
13674 : :
13675 : : return ret;
13676 : : }
13677 : :
13678 : : void
13679 : : print_fold_checksum (const_tree expr)
13680 : : {
13681 : : struct md5_ctx ctx;
13682 : : unsigned char checksum[16], cnt;
13683 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13684 : :
13685 : : md5_init_ctx (&ctx);
13686 : : fold_checksum_tree (expr, &ctx, &ht);
13687 : : md5_finish_ctx (&ctx, checksum);
13688 : : for (cnt = 0; cnt < 16; ++cnt)
13689 : : fprintf (stderr, "%02x", checksum[cnt]);
13690 : : putc ('\n', stderr);
13691 : : }
13692 : :
13693 : : static void
13694 : : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13695 : : {
13696 : : internal_error ("fold check: original tree changed by fold");
13697 : : }
13698 : :
13699 : : static void
13700 : : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13701 : : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13702 : : {
13703 : : const tree_node **slot;
13704 : : enum tree_code code;
13705 : : union tree_node *buf;
13706 : : int i, len;
13707 : :
13708 : : recursive_label:
13709 : : if (expr == NULL)
13710 : : return;
13711 : : slot = ht->find_slot (expr, INSERT);
13712 : : if (*slot != NULL)
13713 : : return;
13714 : : *slot = expr;
13715 : : code = TREE_CODE (expr);
13716 : : if (TREE_CODE_CLASS (code) == tcc_declaration
13717 : : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13718 : : {
13719 : : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13720 : : size_t sz = tree_size (expr);
13721 : : buf = XALLOCAVAR (union tree_node, sz);
13722 : : memcpy ((char *) buf, expr, sz);
13723 : : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13724 : : buf->decl_with_vis.symtab_node = NULL;
13725 : : buf->base.nowarning_flag = 0;
13726 : : expr = (tree) buf;
13727 : : }
13728 : : else if (TREE_CODE_CLASS (code) == tcc_type
13729 : : && (TYPE_POINTER_TO (expr)
13730 : : || TYPE_REFERENCE_TO (expr)
13731 : : || TYPE_CACHED_VALUES_P (expr)
13732 : : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13733 : : || TYPE_NEXT_VARIANT (expr)
13734 : : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13735 : : {
13736 : : /* Allow these fields to be modified. */
13737 : : tree tmp;
13738 : : size_t sz = tree_size (expr);
13739 : : buf = XALLOCAVAR (union tree_node, sz);
13740 : : memcpy ((char *) buf, expr, sz);
13741 : : expr = tmp = (tree) buf;
13742 : : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13743 : : TYPE_POINTER_TO (tmp) = NULL;
13744 : : TYPE_REFERENCE_TO (tmp) = NULL;
13745 : : TYPE_NEXT_VARIANT (tmp) = NULL;
13746 : : TYPE_ALIAS_SET (tmp) = -1;
13747 : : if (TYPE_CACHED_VALUES_P (tmp))
13748 : : {
13749 : : TYPE_CACHED_VALUES_P (tmp) = 0;
13750 : : TYPE_CACHED_VALUES (tmp) = NULL;
13751 : : }
13752 : : }
13753 : : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13754 : : {
13755 : : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13756 : : that and change builtins.cc etc. instead - see PR89543. */
13757 : : size_t sz = tree_size (expr);
13758 : : buf = XALLOCAVAR (union tree_node, sz);
13759 : : memcpy ((char *) buf, expr, sz);
13760 : : buf->base.nowarning_flag = 0;
13761 : : expr = (tree) buf;
13762 : : }
13763 : : md5_process_bytes (expr, tree_size (expr), ctx);
13764 : : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13765 : : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13766 : : if (TREE_CODE_CLASS (code) != tcc_type
13767 : : && TREE_CODE_CLASS (code) != tcc_declaration
13768 : : && code != TREE_LIST
13769 : : && code != SSA_NAME
13770 : : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13771 : : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13772 : : switch (TREE_CODE_CLASS (code))
13773 : : {
13774 : : case tcc_constant:
13775 : : switch (code)
13776 : : {
13777 : : case STRING_CST:
13778 : : md5_process_bytes (TREE_STRING_POINTER (expr),
13779 : : TREE_STRING_LENGTH (expr), ctx);
13780 : : break;
13781 : : case COMPLEX_CST:
13782 : : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13783 : : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13784 : : break;
13785 : : case VECTOR_CST:
13786 : : len = vector_cst_encoded_nelts (expr);
13787 : : for (i = 0; i < len; ++i)
13788 : : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13789 : : break;
13790 : : default:
13791 : : break;
13792 : : }
13793 : : break;
13794 : : case tcc_exceptional:
13795 : : switch (code)
13796 : : {
13797 : : case TREE_LIST:
13798 : : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13799 : : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13800 : : expr = TREE_CHAIN (expr);
13801 : : goto recursive_label;
13802 : : break;
13803 : : case TREE_VEC:
13804 : : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13805 : : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13806 : : break;
13807 : : default:
13808 : : break;
13809 : : }
13810 : : break;
13811 : : case tcc_expression:
13812 : : case tcc_reference:
13813 : : case tcc_comparison:
13814 : : case tcc_unary:
13815 : : case tcc_binary:
13816 : : case tcc_statement:
13817 : : case tcc_vl_exp:
13818 : : len = TREE_OPERAND_LENGTH (expr);
13819 : : for (i = 0; i < len; ++i)
13820 : : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13821 : : break;
13822 : : case tcc_declaration:
13823 : : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13824 : : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13825 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13826 : : {
13827 : : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13828 : : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13829 : : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13830 : : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13831 : : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13832 : : }
13833 : :
13834 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13835 : : {
13836 : : if (TREE_CODE (expr) == FUNCTION_DECL)
13837 : : {
13838 : : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13839 : : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13840 : : }
13841 : : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13842 : : }
13843 : : break;
13844 : : case tcc_type:
13845 : : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13846 : : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13847 : : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13848 : : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13849 : : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13850 : : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13851 : : if (INTEGRAL_TYPE_P (expr)
13852 : : || SCALAR_FLOAT_TYPE_P (expr))
13853 : : {
13854 : : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13855 : : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13856 : : }
13857 : : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13858 : : if (RECORD_OR_UNION_TYPE_P (expr))
13859 : : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13860 : : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13861 : : break;
13862 : : default:
13863 : : break;
13864 : : }
13865 : : }
13866 : :
13867 : : /* Helper function for outputting the checksum of a tree T. When
13868 : : debugging with gdb, you can "define mynext" to be "next" followed
13869 : : by "call debug_fold_checksum (op0)", then just trace down till the
13870 : : outputs differ. */
13871 : :
13872 : : DEBUG_FUNCTION void
13873 : : debug_fold_checksum (const_tree t)
13874 : : {
13875 : : int i;
13876 : : unsigned char checksum[16];
13877 : : struct md5_ctx ctx;
13878 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13879 : :
13880 : : md5_init_ctx (&ctx);
13881 : : fold_checksum_tree (t, &ctx, &ht);
13882 : : md5_finish_ctx (&ctx, checksum);
13883 : : ht.empty ();
13884 : :
13885 : : for (i = 0; i < 16; i++)
13886 : : fprintf (stderr, "%d ", checksum[i]);
13887 : :
13888 : : fprintf (stderr, "\n");
13889 : : }
13890 : :
13891 : : #endif
13892 : :
13893 : : /* Fold a unary tree expression with code CODE of type TYPE with an
13894 : : operand OP0. LOC is the location of the resulting expression.
13895 : : Return a folded expression if successful. Otherwise, return a tree
13896 : : expression with code CODE of type TYPE with an operand OP0. */
13897 : :
13898 : : tree
13899 : 732973246 : fold_build1_loc (location_t loc,
13900 : : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13901 : : {
13902 : 732973246 : tree tem;
13903 : : #ifdef ENABLE_FOLD_CHECKING
13904 : : unsigned char checksum_before[16], checksum_after[16];
13905 : : struct md5_ctx ctx;
13906 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13907 : :
13908 : : md5_init_ctx (&ctx);
13909 : : fold_checksum_tree (op0, &ctx, &ht);
13910 : : md5_finish_ctx (&ctx, checksum_before);
13911 : : ht.empty ();
13912 : : #endif
13913 : :
13914 : 732973246 : tem = fold_unary_loc (loc, code, type, op0);
13915 : 732973246 : if (!tem)
13916 : 392615708 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13917 : :
13918 : : #ifdef ENABLE_FOLD_CHECKING
13919 : : md5_init_ctx (&ctx);
13920 : : fold_checksum_tree (op0, &ctx, &ht);
13921 : : md5_finish_ctx (&ctx, checksum_after);
13922 : :
13923 : : if (memcmp (checksum_before, checksum_after, 16))
13924 : : fold_check_failed (op0, tem);
13925 : : #endif
13926 : 732973246 : return tem;
13927 : : }
13928 : :
13929 : : /* Fold a binary tree expression with code CODE of type TYPE with
13930 : : operands OP0 and OP1. LOC is the location of the resulting
13931 : : expression. Return a folded expression if successful. Otherwise,
13932 : : return a tree expression with code CODE of type TYPE with operands
13933 : : OP0 and OP1. */
13934 : :
13935 : : tree
13936 : 572994433 : fold_build2_loc (location_t loc,
13937 : : enum tree_code code, tree type, tree op0, tree op1
13938 : : MEM_STAT_DECL)
13939 : : {
13940 : 572994433 : tree tem;
13941 : : #ifdef ENABLE_FOLD_CHECKING
13942 : : unsigned char checksum_before_op0[16],
13943 : : checksum_before_op1[16],
13944 : : checksum_after_op0[16],
13945 : : checksum_after_op1[16];
13946 : : struct md5_ctx ctx;
13947 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13948 : :
13949 : : md5_init_ctx (&ctx);
13950 : : fold_checksum_tree (op0, &ctx, &ht);
13951 : : md5_finish_ctx (&ctx, checksum_before_op0);
13952 : : ht.empty ();
13953 : :
13954 : : md5_init_ctx (&ctx);
13955 : : fold_checksum_tree (op1, &ctx, &ht);
13956 : : md5_finish_ctx (&ctx, checksum_before_op1);
13957 : : ht.empty ();
13958 : : #endif
13959 : :
13960 : 572994433 : tem = fold_binary_loc (loc, code, type, op0, op1);
13961 : 572994433 : if (!tem)
13962 : 319627765 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13963 : :
13964 : : #ifdef ENABLE_FOLD_CHECKING
13965 : : md5_init_ctx (&ctx);
13966 : : fold_checksum_tree (op0, &ctx, &ht);
13967 : : md5_finish_ctx (&ctx, checksum_after_op0);
13968 : : ht.empty ();
13969 : :
13970 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13971 : : fold_check_failed (op0, tem);
13972 : :
13973 : : md5_init_ctx (&ctx);
13974 : : fold_checksum_tree (op1, &ctx, &ht);
13975 : : md5_finish_ctx (&ctx, checksum_after_op1);
13976 : :
13977 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13978 : : fold_check_failed (op1, tem);
13979 : : #endif
13980 : 572994433 : return tem;
13981 : : }
13982 : :
13983 : : /* Fold a ternary tree expression with code CODE of type TYPE with
13984 : : operands OP0, OP1, and OP2. Return a folded expression if
13985 : : successful. Otherwise, return a tree expression with code CODE of
13986 : : type TYPE with operands OP0, OP1, and OP2. */
13987 : :
13988 : : tree
13989 : 22648484 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13990 : : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13991 : : {
13992 : 22648484 : tree tem;
13993 : : #ifdef ENABLE_FOLD_CHECKING
13994 : : unsigned char checksum_before_op0[16],
13995 : : checksum_before_op1[16],
13996 : : checksum_before_op2[16],
13997 : : checksum_after_op0[16],
13998 : : checksum_after_op1[16],
13999 : : checksum_after_op2[16];
14000 : : struct md5_ctx ctx;
14001 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14002 : :
14003 : : md5_init_ctx (&ctx);
14004 : : fold_checksum_tree (op0, &ctx, &ht);
14005 : : md5_finish_ctx (&ctx, checksum_before_op0);
14006 : : ht.empty ();
14007 : :
14008 : : md5_init_ctx (&ctx);
14009 : : fold_checksum_tree (op1, &ctx, &ht);
14010 : : md5_finish_ctx (&ctx, checksum_before_op1);
14011 : : ht.empty ();
14012 : :
14013 : : md5_init_ctx (&ctx);
14014 : : fold_checksum_tree (op2, &ctx, &ht);
14015 : : md5_finish_ctx (&ctx, checksum_before_op2);
14016 : : ht.empty ();
14017 : : #endif
14018 : :
14019 : 22648484 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14020 : 22648484 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14021 : 22648484 : if (!tem)
14022 : 20043570 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14023 : :
14024 : : #ifdef ENABLE_FOLD_CHECKING
14025 : : md5_init_ctx (&ctx);
14026 : : fold_checksum_tree (op0, &ctx, &ht);
14027 : : md5_finish_ctx (&ctx, checksum_after_op0);
14028 : : ht.empty ();
14029 : :
14030 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14031 : : fold_check_failed (op0, tem);
14032 : :
14033 : : md5_init_ctx (&ctx);
14034 : : fold_checksum_tree (op1, &ctx, &ht);
14035 : : md5_finish_ctx (&ctx, checksum_after_op1);
14036 : : ht.empty ();
14037 : :
14038 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14039 : : fold_check_failed (op1, tem);
14040 : :
14041 : : md5_init_ctx (&ctx);
14042 : : fold_checksum_tree (op2, &ctx, &ht);
14043 : : md5_finish_ctx (&ctx, checksum_after_op2);
14044 : :
14045 : : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14046 : : fold_check_failed (op2, tem);
14047 : : #endif
14048 : 22648484 : return tem;
14049 : : }
14050 : :
14051 : : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14052 : : arguments in ARGARRAY, and a null static chain.
14053 : : Return a folded expression if successful. Otherwise, return a CALL_EXPR
14054 : : of type TYPE from the given operands as constructed by build_call_array. */
14055 : :
14056 : : tree
14057 : 50571680 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
14058 : : int nargs, tree *argarray)
14059 : : {
14060 : 50571680 : tree tem;
14061 : : #ifdef ENABLE_FOLD_CHECKING
14062 : : unsigned char checksum_before_fn[16],
14063 : : checksum_before_arglist[16],
14064 : : checksum_after_fn[16],
14065 : : checksum_after_arglist[16];
14066 : : struct md5_ctx ctx;
14067 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14068 : : int i;
14069 : :
14070 : : md5_init_ctx (&ctx);
14071 : : fold_checksum_tree (fn, &ctx, &ht);
14072 : : md5_finish_ctx (&ctx, checksum_before_fn);
14073 : : ht.empty ();
14074 : :
14075 : : md5_init_ctx (&ctx);
14076 : : for (i = 0; i < nargs; i++)
14077 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14078 : : md5_finish_ctx (&ctx, checksum_before_arglist);
14079 : : ht.empty ();
14080 : : #endif
14081 : :
14082 : 50571680 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14083 : 50571680 : if (!tem)
14084 : 48886683 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14085 : :
14086 : : #ifdef ENABLE_FOLD_CHECKING
14087 : : md5_init_ctx (&ctx);
14088 : : fold_checksum_tree (fn, &ctx, &ht);
14089 : : md5_finish_ctx (&ctx, checksum_after_fn);
14090 : : ht.empty ();
14091 : :
14092 : : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14093 : : fold_check_failed (fn, tem);
14094 : :
14095 : : md5_init_ctx (&ctx);
14096 : : for (i = 0; i < nargs; i++)
14097 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14098 : : md5_finish_ctx (&ctx, checksum_after_arglist);
14099 : :
14100 : : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14101 : : fold_check_failed (NULL_TREE, tem);
14102 : : #endif
14103 : 50571680 : return tem;
14104 : : }
14105 : :
14106 : : /* Perform constant folding and related simplification of initializer
14107 : : expression EXPR. These behave identically to "fold_buildN" but ignore
14108 : : potential run-time traps and exceptions that fold must preserve. */
14109 : :
14110 : : #define START_FOLD_INIT \
14111 : : int saved_signaling_nans = flag_signaling_nans;\
14112 : : int saved_trapping_math = flag_trapping_math;\
14113 : : int saved_rounding_math = flag_rounding_math;\
14114 : : int saved_trapv = flag_trapv;\
14115 : : int saved_folding_initializer = folding_initializer;\
14116 : : flag_signaling_nans = 0;\
14117 : : flag_trapping_math = 0;\
14118 : : flag_rounding_math = 0;\
14119 : : flag_trapv = 0;\
14120 : : folding_initializer = 1;
14121 : :
14122 : : #define END_FOLD_INIT \
14123 : : flag_signaling_nans = saved_signaling_nans;\
14124 : : flag_trapping_math = saved_trapping_math;\
14125 : : flag_rounding_math = saved_rounding_math;\
14126 : : flag_trapv = saved_trapv;\
14127 : : folding_initializer = saved_folding_initializer;
14128 : :
14129 : : tree
14130 : 542365 : fold_init (tree expr)
14131 : : {
14132 : 542365 : tree result;
14133 : 542365 : START_FOLD_INIT;
14134 : :
14135 : 542365 : result = fold (expr);
14136 : :
14137 : 542365 : END_FOLD_INIT;
14138 : 542365 : return result;
14139 : : }
14140 : :
14141 : : tree
14142 : 2988471 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14143 : : tree type, tree op)
14144 : : {
14145 : 2988471 : tree result;
14146 : 2988471 : START_FOLD_INIT;
14147 : :
14148 : 2988471 : result = fold_build1_loc (loc, code, type, op);
14149 : :
14150 : 2988471 : END_FOLD_INIT;
14151 : 2988471 : return result;
14152 : : }
14153 : :
14154 : : tree
14155 : 50474 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14156 : : tree type, tree op0, tree op1)
14157 : : {
14158 : 50474 : tree result;
14159 : 50474 : START_FOLD_INIT;
14160 : :
14161 : 50474 : result = fold_build2_loc (loc, code, type, op0, op1);
14162 : :
14163 : 50474 : END_FOLD_INIT;
14164 : 50474 : return result;
14165 : : }
14166 : :
14167 : : tree
14168 : 3488 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14169 : : int nargs, tree *argarray)
14170 : : {
14171 : 3488 : tree result;
14172 : 3488 : START_FOLD_INIT;
14173 : :
14174 : 3488 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14175 : :
14176 : 3488 : END_FOLD_INIT;
14177 : 3488 : return result;
14178 : : }
14179 : :
14180 : : tree
14181 : 21876610 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14182 : : tree lhs, tree rhs)
14183 : : {
14184 : 21876610 : tree result;
14185 : 21876610 : START_FOLD_INIT;
14186 : :
14187 : 21876610 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14188 : :
14189 : 21876610 : END_FOLD_INIT;
14190 : 21876610 : return result;
14191 : : }
14192 : :
14193 : : #undef START_FOLD_INIT
14194 : : #undef END_FOLD_INIT
14195 : :
14196 : : /* Determine if first argument is a multiple of second argument. Return
14197 : : false if it is not, or we cannot easily determined it to be.
14198 : :
14199 : : An example of the sort of thing we care about (at this point; this routine
14200 : : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14201 : : fold cases do now) is discovering that
14202 : :
14203 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14204 : :
14205 : : is a multiple of
14206 : :
14207 : : SAVE_EXPR (J * 8)
14208 : :
14209 : : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14210 : :
14211 : : This code also handles discovering that
14212 : :
14213 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14214 : :
14215 : : is a multiple of 8 so we don't have to worry about dealing with a
14216 : : possible remainder.
14217 : :
14218 : : Note that we *look* inside a SAVE_EXPR only to determine how it was
14219 : : calculated; it is not safe for fold to do much of anything else with the
14220 : : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14221 : : at run time. For example, the latter example above *cannot* be implemented
14222 : : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14223 : : evaluation time of the original SAVE_EXPR is not necessarily the same at
14224 : : the time the new expression is evaluated. The only optimization of this
14225 : : sort that would be valid is changing
14226 : :
14227 : : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14228 : :
14229 : : divided by 8 to
14230 : :
14231 : : SAVE_EXPR (I) * SAVE_EXPR (J)
14232 : :
14233 : : (where the same SAVE_EXPR (J) is used in the original and the
14234 : : transformed version).
14235 : :
14236 : : NOWRAP specifies whether all outer operations in TYPE should
14237 : : be considered not wrapping. Any type conversion within TOP acts
14238 : : as a barrier and we will fall back to NOWRAP being false.
14239 : : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14240 : : as not wrapping even though they are generally using unsigned arithmetic. */
14241 : :
14242 : : bool
14243 : 1527925 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14244 : : {
14245 : 1527925 : gimple *stmt;
14246 : 1527925 : tree op1, op2;
14247 : :
14248 : 1527925 : if (operand_equal_p (top, bottom, 0))
14249 : : return true;
14250 : :
14251 : 1066234 : if (TREE_CODE (type) != INTEGER_TYPE)
14252 : : return false;
14253 : :
14254 : 1066229 : switch (TREE_CODE (top))
14255 : : {
14256 : 636 : case BIT_AND_EXPR:
14257 : : /* Bitwise and provides a power of two multiple. If the mask is
14258 : : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14259 : 636 : if (!integer_pow2p (bottom))
14260 : : return false;
14261 : 636 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14262 : 636 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14263 : :
14264 : 370142 : case MULT_EXPR:
14265 : : /* If the multiplication can wrap we cannot recurse further unless
14266 : : the bottom is a power of two which is where wrapping does not
14267 : : matter. */
14268 : 370142 : if (!nowrap
14269 : 13734 : && !TYPE_OVERFLOW_UNDEFINED (type)
14270 : 374697 : && !integer_pow2p (bottom))
14271 : : return false;
14272 : 369709 : if (TREE_CODE (bottom) == INTEGER_CST)
14273 : : {
14274 : 367993 : op1 = TREE_OPERAND (top, 0);
14275 : 367993 : op2 = TREE_OPERAND (top, 1);
14276 : 367993 : if (TREE_CODE (op1) == INTEGER_CST)
14277 : 0 : std::swap (op1, op2);
14278 : 367993 : if (TREE_CODE (op2) == INTEGER_CST)
14279 : : {
14280 : 358029 : if (multiple_of_p (type, op2, bottom, nowrap))
14281 : : return true;
14282 : : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14283 : 3007 : if (multiple_of_p (type, bottom, op2, nowrap))
14284 : : {
14285 : 1900 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14286 : 1900 : wi::to_widest (op2));
14287 : 1900 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14288 : : {
14289 : 1900 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14290 : 1900 : return multiple_of_p (type, op1, op2, nowrap);
14291 : : }
14292 : 1900 : }
14293 : 1107 : return multiple_of_p (type, op1, bottom, nowrap);
14294 : : }
14295 : : }
14296 : 11680 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14297 : 11680 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14298 : :
14299 : 403 : case LSHIFT_EXPR:
14300 : : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14301 : 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14302 : : {
14303 : 403 : op1 = TREE_OPERAND (top, 1);
14304 : 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14305 : : {
14306 : 403 : wide_int mul_op
14307 : 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14308 : 806 : return multiple_of_p (type,
14309 : 806 : wide_int_to_tree (type, mul_op), bottom,
14310 : : nowrap);
14311 : 403 : }
14312 : : }
14313 : : return false;
14314 : :
14315 : 213487 : case MINUS_EXPR:
14316 : 213487 : case PLUS_EXPR:
14317 : : /* If the addition or subtraction can wrap we cannot recurse further
14318 : : unless bottom is a power of two which is where wrapping does not
14319 : : matter. */
14320 : 213487 : if (!nowrap
14321 : 171131 : && !TYPE_OVERFLOW_UNDEFINED (type)
14322 : 383183 : && !integer_pow2p (bottom))
14323 : : return false;
14324 : :
14325 : : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14326 : : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14327 : : but 0xfffffffd is not. */
14328 : 187287 : op1 = TREE_OPERAND (top, 1);
14329 : 187287 : if (TREE_CODE (top) == PLUS_EXPR
14330 : 181367 : && nowrap
14331 : 36523 : && TYPE_UNSIGNED (type)
14332 : 223115 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14333 : 31162 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14334 : :
14335 : : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14336 : : precisely, so be conservative here checking if both op0 and op1
14337 : : are multiple of bottom. Note we check the second operand first
14338 : : since it's usually simpler. */
14339 : 187287 : return (multiple_of_p (type, op1, bottom, nowrap)
14340 : 187287 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14341 : :
14342 : 139562 : CASE_CONVERT:
14343 : : /* Can't handle conversions from non-integral or wider integral type. */
14344 : 139562 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14345 : 139562 : || (TYPE_PRECISION (type)
14346 : 38623 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14347 : : return false;
14348 : : /* NOWRAP only extends to operations in the outermost type so
14349 : : make sure to strip it off here. */
14350 : 38315 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14351 : 76630 : TREE_OPERAND (top, 0), bottom, false);
14352 : :
14353 : 12412 : case SAVE_EXPR:
14354 : 12412 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14355 : :
14356 : 86 : case COND_EXPR:
14357 : 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14358 : 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14359 : :
14360 : 131262 : case INTEGER_CST:
14361 : 131262 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14362 : 2724 : return false;
14363 : 128538 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14364 : : SIGNED);
14365 : :
14366 : 65449 : case SSA_NAME:
14367 : 65449 : if (TREE_CODE (bottom) == INTEGER_CST
14368 : 62486 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14369 : 127935 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14370 : : {
14371 : 27590 : enum tree_code code = gimple_assign_rhs_code (stmt);
14372 : :
14373 : : /* Check for special cases to see if top is defined as multiple
14374 : : of bottom:
14375 : :
14376 : : top = (X & ~(bottom - 1) ; bottom is power of 2
14377 : :
14378 : : or
14379 : :
14380 : : Y = X % bottom
14381 : : top = X - Y. */
14382 : 27590 : if (code == BIT_AND_EXPR
14383 : 296 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14384 : 296 : && TREE_CODE (op2) == INTEGER_CST
14385 : 188 : && integer_pow2p (bottom)
14386 : 27778 : && wi::multiple_of_p (wi::to_widest (op2),
14387 : 188 : wi::to_widest (bottom), SIGNED))
14388 : 179 : return true;
14389 : :
14390 : 27411 : op1 = gimple_assign_rhs1 (stmt);
14391 : 27411 : if (code == MINUS_EXPR
14392 : 2812 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14393 : 2812 : && TREE_CODE (op2) == SSA_NAME
14394 : 2812 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14395 : 2812 : && gimple_code (stmt) == GIMPLE_ASSIGN
14396 : 2432 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14397 : 62 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14398 : 27473 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14399 : : return true;
14400 : : }
14401 : :
14402 : : /* fall through */
14403 : :
14404 : : default:
14405 : : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14406 : : return multiple_p (wi::to_poly_widest (top),
14407 : : wi::to_poly_widest (bottom));
14408 : :
14409 : : return false;
14410 : : }
14411 : : }
14412 : :
14413 : : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14414 : : This function returns true for integer expressions, and returns
14415 : : false if uncertain. */
14416 : :
14417 : : bool
14418 : 594061 : tree_expr_finite_p (const_tree x)
14419 : : {
14420 : 594065 : machine_mode mode = element_mode (x);
14421 : 594065 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14422 : : return true;
14423 : 593871 : switch (TREE_CODE (x))
14424 : : {
14425 : 592 : case REAL_CST:
14426 : 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14427 : 0 : case COMPLEX_CST:
14428 : 0 : return tree_expr_finite_p (TREE_REALPART (x))
14429 : 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14430 : : case FLOAT_EXPR:
14431 : : return true;
14432 : 4 : case ABS_EXPR:
14433 : 4 : case CONVERT_EXPR:
14434 : 4 : case NON_LVALUE_EXPR:
14435 : 4 : case NEGATE_EXPR:
14436 : 4 : case SAVE_EXPR:
14437 : 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14438 : 0 : case MIN_EXPR:
14439 : 0 : case MAX_EXPR:
14440 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14441 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14442 : 0 : case COND_EXPR:
14443 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14444 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14445 : 38 : case CALL_EXPR:
14446 : 38 : switch (get_call_combined_fn (x))
14447 : : {
14448 : 0 : CASE_CFN_FABS:
14449 : 0 : CASE_CFN_FABS_FN:
14450 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14451 : 0 : CASE_CFN_FMAX:
14452 : 0 : CASE_CFN_FMAX_FN:
14453 : 0 : CASE_CFN_FMIN:
14454 : 0 : CASE_CFN_FMIN_FN:
14455 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14456 : 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14457 : : default:
14458 : : return false;
14459 : : }
14460 : :
14461 : : default:
14462 : : return false;
14463 : : }
14464 : : }
14465 : :
14466 : : /* Return true if expression X evaluates to an infinity.
14467 : : This function returns false for integer expressions. */
14468 : :
14469 : : bool
14470 : 848406 : tree_expr_infinite_p (const_tree x)
14471 : : {
14472 : 848856 : if (!HONOR_INFINITIES (x))
14473 : : return false;
14474 : 848741 : switch (TREE_CODE (x))
14475 : : {
14476 : 0 : case REAL_CST:
14477 : 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14478 : 450 : case ABS_EXPR:
14479 : 450 : case NEGATE_EXPR:
14480 : 450 : case NON_LVALUE_EXPR:
14481 : 450 : case SAVE_EXPR:
14482 : 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14483 : 0 : case COND_EXPR:
14484 : 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14485 : 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14486 : : default:
14487 : : return false;
14488 : : }
14489 : : }
14490 : :
14491 : : /* Return true if expression X could evaluate to an infinity.
14492 : : This function returns false for integer expressions, and returns
14493 : : true if uncertain. */
14494 : :
14495 : : bool
14496 : 384428 : tree_expr_maybe_infinite_p (const_tree x)
14497 : : {
14498 : 384436 : if (!HONOR_INFINITIES (x))
14499 : : return false;
14500 : 384109 : switch (TREE_CODE (x))
14501 : : {
14502 : 192 : case REAL_CST:
14503 : 192 : return real_isinf (TREE_REAL_CST_PTR (x));
14504 : : case FLOAT_EXPR:
14505 : : return false;
14506 : 8 : case ABS_EXPR:
14507 : 8 : case NEGATE_EXPR:
14508 : 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14509 : 1 : case COND_EXPR:
14510 : 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14511 : 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14512 : : default:
14513 : : return true;
14514 : : }
14515 : : }
14516 : :
14517 : : /* Return true if expression X evaluates to a signaling NaN.
14518 : : This function returns false for integer expressions. */
14519 : :
14520 : : bool
14521 : 389 : tree_expr_signaling_nan_p (const_tree x)
14522 : : {
14523 : 389 : if (!HONOR_SNANS (x))
14524 : : return false;
14525 : 124 : switch (TREE_CODE (x))
14526 : : {
14527 : 124 : case REAL_CST:
14528 : 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14529 : 0 : case NON_LVALUE_EXPR:
14530 : 0 : case SAVE_EXPR:
14531 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14532 : 0 : case COND_EXPR:
14533 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14534 : 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14535 : : default:
14536 : : return false;
14537 : : }
14538 : : }
14539 : :
14540 : : /* Return true if expression X could evaluate to a signaling NaN.
14541 : : This function returns false for integer expressions, and returns
14542 : : true if uncertain. */
14543 : :
14544 : : bool
14545 : 728015 : tree_expr_maybe_signaling_nan_p (const_tree x)
14546 : : {
14547 : 728015 : if (!HONOR_SNANS (x))
14548 : : return false;
14549 : 5028 : switch (TREE_CODE (x))
14550 : : {
14551 : 1452 : case REAL_CST:
14552 : 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14553 : : case FLOAT_EXPR:
14554 : : return false;
14555 : 0 : case ABS_EXPR:
14556 : 0 : case CONVERT_EXPR:
14557 : 0 : case NEGATE_EXPR:
14558 : 0 : case NON_LVALUE_EXPR:
14559 : 0 : case SAVE_EXPR:
14560 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14561 : 0 : case MIN_EXPR:
14562 : 0 : case MAX_EXPR:
14563 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14564 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14565 : 0 : case COND_EXPR:
14566 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14567 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14568 : 0 : case CALL_EXPR:
14569 : 0 : switch (get_call_combined_fn (x))
14570 : : {
14571 : 0 : CASE_CFN_FABS:
14572 : 0 : CASE_CFN_FABS_FN:
14573 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14574 : 0 : CASE_CFN_FMAX:
14575 : 0 : CASE_CFN_FMAX_FN:
14576 : 0 : CASE_CFN_FMIN:
14577 : 0 : CASE_CFN_FMIN_FN:
14578 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14579 : 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14580 : : default:
14581 : : return true;
14582 : : }
14583 : : default:
14584 : : return true;
14585 : : }
14586 : : }
14587 : :
14588 : : /* Return true if expression X evaluates to a NaN.
14589 : : This function returns false for integer expressions. */
14590 : :
14591 : : bool
14592 : 4126362 : tree_expr_nan_p (const_tree x)
14593 : : {
14594 : 4518484 : if (!HONOR_NANS (x))
14595 : : return false;
14596 : 4518134 : switch (TREE_CODE (x))
14597 : : {
14598 : 3792 : case REAL_CST:
14599 : 3792 : return real_isnan (TREE_REAL_CST_PTR (x));
14600 : 392122 : case NON_LVALUE_EXPR:
14601 : 392122 : case SAVE_EXPR:
14602 : 392122 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14603 : 900 : case COND_EXPR:
14604 : 900 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14605 : 900 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14606 : : default:
14607 : : return false;
14608 : : }
14609 : : }
14610 : :
14611 : : /* Return true if expression X could evaluate to a NaN.
14612 : : This function returns false for integer expressions, and returns
14613 : : true if uncertain. */
14614 : :
14615 : : bool
14616 : 5062305 : tree_expr_maybe_nan_p (const_tree x)
14617 : : {
14618 : 7078088 : if (!HONOR_NANS (x))
14619 : : return false;
14620 : 6904098 : switch (TREE_CODE (x))
14621 : : {
14622 : 3288 : case REAL_CST:
14623 : 3288 : return real_isnan (TREE_REAL_CST_PTR (x));
14624 : : case FLOAT_EXPR:
14625 : : return false;
14626 : 13849 : case PLUS_EXPR:
14627 : 13849 : case MINUS_EXPR:
14628 : 13849 : case MULT_EXPR:
14629 : 13849 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14630 : 13849 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14631 : 2015783 : case ABS_EXPR:
14632 : 2015783 : case CONVERT_EXPR:
14633 : 2015783 : case NEGATE_EXPR:
14634 : 2015783 : case NON_LVALUE_EXPR:
14635 : 2015783 : case SAVE_EXPR:
14636 : 2015783 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14637 : 176 : case MIN_EXPR:
14638 : 176 : case MAX_EXPR:
14639 : 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14640 : 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14641 : 557 : case COND_EXPR:
14642 : 557 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14643 : 557 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14644 : 1082 : case CALL_EXPR:
14645 : 1082 : switch (get_call_combined_fn (x))
14646 : : {
14647 : 0 : CASE_CFN_FABS:
14648 : 0 : CASE_CFN_FABS_FN:
14649 : 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14650 : 108 : CASE_CFN_FMAX:
14651 : 108 : CASE_CFN_FMAX_FN:
14652 : 108 : CASE_CFN_FMIN:
14653 : 108 : CASE_CFN_FMIN_FN:
14654 : 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14655 : 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14656 : : default:
14657 : : return true;
14658 : : }
14659 : : default:
14660 : : return true;
14661 : : }
14662 : : }
14663 : :
14664 : : /* Return true if expression X could evaluate to -0.0.
14665 : : This function returns true if uncertain. */
14666 : :
14667 : : bool
14668 : 602408 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14669 : : {
14670 : 602408 : if (!HONOR_SIGNED_ZEROS (x))
14671 : : return false;
14672 : 602408 : switch (TREE_CODE (x))
14673 : : {
14674 : 0 : case REAL_CST:
14675 : 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14676 : : case INTEGER_CST:
14677 : : case FLOAT_EXPR:
14678 : : case ABS_EXPR:
14679 : : return false;
14680 : 0 : case NON_LVALUE_EXPR:
14681 : 0 : case SAVE_EXPR:
14682 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14683 : 0 : case COND_EXPR:
14684 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14685 : 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14686 : 1 : case CALL_EXPR:
14687 : 1 : switch (get_call_combined_fn (x))
14688 : : {
14689 : : CASE_CFN_FABS:
14690 : : CASE_CFN_FABS_FN:
14691 : : return false;
14692 : : default:
14693 : : break;
14694 : : }
14695 : : default:
14696 : : break;
14697 : : }
14698 : : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14699 : : * but currently those predicates require tree and not const_tree. */
14700 : : return true;
14701 : : }
14702 : :
14703 : : #define tree_expr_nonnegative_warnv_p(X, Y) \
14704 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14705 : :
14706 : : #define RECURSE(X) \
14707 : : ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14708 : :
14709 : : /* Return true if CODE or TYPE is known to be non-negative. */
14710 : :
14711 : : static bool
14712 : 39703589 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14713 : : {
14714 : 39703589 : if (!VECTOR_TYPE_P (type)
14715 : 39684730 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14716 : 79387369 : && truth_value_p (code))
14717 : : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14718 : : have a signed:1 type (where the value is -1 and 0). */
14719 : : return true;
14720 : : return false;
14721 : : }
14722 : :
14723 : : /* Return true if (CODE OP0) is known to be non-negative. If the return
14724 : : value is based on the assumption that signed overflow is undefined,
14725 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14726 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14727 : :
14728 : : bool
14729 : 14360587 : tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14730 : : bool *strict_overflow_p, int depth)
14731 : : {
14732 : 14360587 : if (TYPE_UNSIGNED (type))
14733 : : return true;
14734 : :
14735 : 5637994 : switch (code)
14736 : : {
14737 : 330343 : case ABS_EXPR:
14738 : : /* We can't return 1 if flag_wrapv is set because
14739 : : ABS_EXPR<INT_MIN> = INT_MIN. */
14740 : 330343 : if (!ANY_INTEGRAL_TYPE_P (type))
14741 : : return true;
14742 : 18291 : if (TYPE_OVERFLOW_UNDEFINED (type))
14743 : : {
14744 : 17343 : *strict_overflow_p = true;
14745 : 17343 : return true;
14746 : : }
14747 : : break;
14748 : :
14749 : 225086 : case NON_LVALUE_EXPR:
14750 : 225086 : case FLOAT_EXPR:
14751 : 225086 : case FIX_TRUNC_EXPR:
14752 : 225086 : return RECURSE (op0);
14753 : :
14754 : 4981239 : CASE_CONVERT:
14755 : 4981239 : {
14756 : 4981239 : tree inner_type = TREE_TYPE (op0);
14757 : 4981239 : tree outer_type = type;
14758 : :
14759 : 4981239 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14760 : : {
14761 : 311004 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14762 : 311004 : return RECURSE (op0);
14763 : 0 : if (INTEGRAL_TYPE_P (inner_type))
14764 : : {
14765 : 0 : if (TYPE_UNSIGNED (inner_type))
14766 : : return true;
14767 : 0 : return RECURSE (op0);
14768 : : }
14769 : : }
14770 : 4670235 : else if (INTEGRAL_TYPE_P (outer_type))
14771 : : {
14772 : 4670198 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14773 : 0 : return RECURSE (op0);
14774 : 4670198 : if (INTEGRAL_TYPE_P (inner_type))
14775 : 4503001 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14776 : 4503001 : && TYPE_UNSIGNED (inner_type);
14777 : : }
14778 : : }
14779 : : break;
14780 : :
14781 : 101326 : default:
14782 : 101326 : return tree_simple_nonnegative_warnv_p (code, type);
14783 : : }
14784 : :
14785 : : /* We don't know sign of `t', so be conservative and return false. */
14786 : : return false;
14787 : : }
14788 : :
14789 : : /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14790 : : value is based on the assumption that signed overflow is undefined,
14791 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14792 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14793 : :
14794 : : bool
14795 : 42591755 : tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14796 : : tree op1, bool *strict_overflow_p,
14797 : : int depth)
14798 : : {
14799 : 42591755 : if (TYPE_UNSIGNED (type))
14800 : : return true;
14801 : :
14802 : 16383458 : switch (code)
14803 : : {
14804 : 6121970 : case POINTER_PLUS_EXPR:
14805 : 6121970 : case PLUS_EXPR:
14806 : 6121970 : if (FLOAT_TYPE_P (type))
14807 : 69619 : return RECURSE (op0) && RECURSE (op1);
14808 : :
14809 : : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14810 : : both unsigned and at least 2 bits shorter than the result. */
14811 : 6052351 : if (TREE_CODE (type) == INTEGER_TYPE
14812 : 6045915 : && TREE_CODE (op0) == NOP_EXPR
14813 : 7780 : && TREE_CODE (op1) == NOP_EXPR)
14814 : : {
14815 : 209 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14816 : 209 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14817 : 209 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14818 : 316 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14819 : : {
14820 : 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14821 : 95 : TYPE_PRECISION (inner2)) + 1;
14822 : 95 : return prec < TYPE_PRECISION (type);
14823 : : }
14824 : : }
14825 : : break;
14826 : :
14827 : 1501096 : case MULT_EXPR:
14828 : 1501096 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14829 : : {
14830 : : /* x * x is always non-negative for floating point x
14831 : : or without overflow. */
14832 : 1439153 : if (operand_equal_p (op0, op1, 0)
14833 : 1439153 : || (RECURSE (op0) && RECURSE (op1)))
14834 : : {
14835 : 1231 : if (ANY_INTEGRAL_TYPE_P (type)
14836 : 13743 : && TYPE_OVERFLOW_UNDEFINED (type))
14837 : 12512 : *strict_overflow_p = true;
14838 : 13722 : return true;
14839 : : }
14840 : : }
14841 : :
14842 : : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14843 : : both unsigned and their total bits is shorter than the result. */
14844 : 1487374 : if (TREE_CODE (type) == INTEGER_TYPE
14845 : 1407975 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14846 : 152 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14847 : : {
14848 : 116 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14849 : 116 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14850 : 116 : : TREE_TYPE (op0);
14851 : 116 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14852 : 116 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14853 : 116 : : TREE_TYPE (op1);
14854 : :
14855 : 116 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14856 : 116 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14857 : :
14858 : 116 : if (TREE_CODE (op0) == INTEGER_CST)
14859 : 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14860 : :
14861 : 116 : if (TREE_CODE (op1) == INTEGER_CST)
14862 : 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14863 : :
14864 : 116 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14865 : 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14866 : : {
14867 : 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14868 : 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14869 : 0 : : TYPE_PRECISION (inner0);
14870 : :
14871 : 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14872 : 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14873 : 0 : : TYPE_PRECISION (inner1);
14874 : :
14875 : 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14876 : : }
14877 : : }
14878 : : return false;
14879 : :
14880 : 95608 : case BIT_AND_EXPR:
14881 : 95608 : return RECURSE (op0) || RECURSE (op1);
14882 : :
14883 : 81086 : case MAX_EXPR:
14884 : : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14885 : : things. */
14886 : 81086 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14887 : 76 : return RECURSE (op0) && RECURSE (op1);
14888 : 81010 : return RECURSE (op0) || RECURSE (op1);
14889 : :
14890 : 823310 : case BIT_IOR_EXPR:
14891 : 823310 : case BIT_XOR_EXPR:
14892 : 823310 : case MIN_EXPR:
14893 : 823310 : case RDIV_EXPR:
14894 : 823310 : case TRUNC_DIV_EXPR:
14895 : 823310 : case CEIL_DIV_EXPR:
14896 : 823310 : case FLOOR_DIV_EXPR:
14897 : 823310 : case ROUND_DIV_EXPR:
14898 : 823310 : return RECURSE (op0) && RECURSE (op1);
14899 : :
14900 : 106916 : case TRUNC_MOD_EXPR:
14901 : 106916 : return RECURSE (op0);
14902 : :
14903 : 394 : case FLOOR_MOD_EXPR:
14904 : 394 : return RECURSE (op1);
14905 : :
14906 : 7653078 : case CEIL_MOD_EXPR:
14907 : 7653078 : case ROUND_MOD_EXPR:
14908 : 7653078 : default:
14909 : 7653078 : return tree_simple_nonnegative_warnv_p (code, type);
14910 : : }
14911 : :
14912 : : /* We don't know sign of `t', so be conservative and return false. */
14913 : : return false;
14914 : : }
14915 : :
14916 : : /* Return true if T is known to be non-negative. If the return
14917 : : value is based on the assumption that signed overflow is undefined,
14918 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14919 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14920 : :
14921 : : bool
14922 : 50501470 : tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14923 : : {
14924 : 50501470 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14925 : : return true;
14926 : :
14927 : 33124478 : switch (TREE_CODE (t))
14928 : : {
14929 : 3509354 : case INTEGER_CST:
14930 : 3509354 : return tree_int_cst_sgn (t) >= 0;
14931 : :
14932 : 1005023 : case REAL_CST:
14933 : 1005023 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14934 : :
14935 : 0 : case FIXED_CST:
14936 : 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14937 : :
14938 : 490 : case COND_EXPR:
14939 : 490 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14940 : :
14941 : 18330405 : case SSA_NAME:
14942 : : /* Limit the depth of recursion to avoid quadratic behavior.
14943 : : This is expected to catch almost all occurrences in practice.
14944 : : If this code misses important cases that unbounded recursion
14945 : : would not, passes that need this information could be revised
14946 : : to provide it through dataflow propagation. */
14947 : 18330405 : return (!name_registered_for_update_p (t)
14948 : 18330388 : && depth < param_max_ssa_name_query_depth
14949 : 35185267 : && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14950 : : strict_overflow_p, depth));
14951 : :
14952 : 10279206 : default:
14953 : 10279206 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14954 : : }
14955 : : }
14956 : :
14957 : : /* Return true if T is known to be non-negative. If the return
14958 : : value is based on the assumption that signed overflow is undefined,
14959 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14960 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14961 : :
14962 : : bool
14963 : 21837526 : tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14964 : : bool *strict_overflow_p, int depth)
14965 : : {
14966 : 21837526 : switch (fn)
14967 : : {
14968 : : CASE_CFN_ACOS:
14969 : : CASE_CFN_ACOS_FN:
14970 : : CASE_CFN_ACOSH:
14971 : : CASE_CFN_ACOSH_FN:
14972 : : CASE_CFN_ACOSPI:
14973 : : CASE_CFN_ACOSPI_FN:
14974 : : CASE_CFN_CABS:
14975 : : CASE_CFN_CABS_FN:
14976 : : CASE_CFN_COSH:
14977 : : CASE_CFN_COSH_FN:
14978 : : CASE_CFN_ERFC:
14979 : : CASE_CFN_ERFC_FN:
14980 : : CASE_CFN_EXP:
14981 : : CASE_CFN_EXP_FN:
14982 : : CASE_CFN_EXP10:
14983 : : CASE_CFN_EXP2:
14984 : : CASE_CFN_EXP2_FN:
14985 : : CASE_CFN_FABS:
14986 : : CASE_CFN_FABS_FN:
14987 : : CASE_CFN_FDIM:
14988 : : CASE_CFN_FDIM_FN:
14989 : : CASE_CFN_HYPOT:
14990 : : CASE_CFN_HYPOT_FN:
14991 : : CASE_CFN_POW10:
14992 : : CASE_CFN_FFS:
14993 : : CASE_CFN_PARITY:
14994 : : CASE_CFN_POPCOUNT:
14995 : : CASE_CFN_CLRSB:
14996 : : case CFN_BUILT_IN_BSWAP16:
14997 : : case CFN_BUILT_IN_BSWAP32:
14998 : : case CFN_BUILT_IN_BSWAP64:
14999 : : case CFN_BUILT_IN_BSWAP128:
15000 : : /* Always true. */
15001 : : return true;
15002 : :
15003 : 1071 : CASE_CFN_CLZ:
15004 : 1071 : CASE_CFN_CTZ:
15005 : 1071 : if (arg1)
15006 : 0 : return RECURSE (arg1);
15007 : : return true;
15008 : :
15009 : 1024 : CASE_CFN_SQRT:
15010 : 1024 : CASE_CFN_SQRT_FN:
15011 : : /* sqrt(-0.0) is -0.0. */
15012 : 1024 : if (!HONOR_SIGNED_ZEROS (type))
15013 : : return true;
15014 : 987 : return RECURSE (arg0);
15015 : :
15016 : 123277 : CASE_CFN_ASINH:
15017 : 123277 : CASE_CFN_ASINH_FN:
15018 : 123277 : CASE_CFN_ASINPI:
15019 : 123277 : CASE_CFN_ASINPI_FN:
15020 : 123277 : CASE_CFN_ATAN:
15021 : 123277 : CASE_CFN_ATAN_FN:
15022 : 123277 : CASE_CFN_ATANH:
15023 : 123277 : CASE_CFN_ATANH_FN:
15024 : 123277 : CASE_CFN_ATANPI:
15025 : 123277 : CASE_CFN_ATANPI_FN:
15026 : 123277 : CASE_CFN_CBRT:
15027 : 123277 : CASE_CFN_CBRT_FN:
15028 : 123277 : CASE_CFN_CEIL:
15029 : 123277 : CASE_CFN_CEIL_FN:
15030 : 123277 : CASE_CFN_ERF:
15031 : 123277 : CASE_CFN_ERF_FN:
15032 : 123277 : CASE_CFN_EXPM1:
15033 : 123277 : CASE_CFN_EXPM1_FN:
15034 : 123277 : CASE_CFN_FLOOR:
15035 : 123277 : CASE_CFN_FLOOR_FN:
15036 : 123277 : CASE_CFN_FMOD:
15037 : 123277 : CASE_CFN_FMOD_FN:
15038 : 123277 : CASE_CFN_FREXP:
15039 : 123277 : CASE_CFN_FREXP_FN:
15040 : 123277 : CASE_CFN_ICEIL:
15041 : 123277 : CASE_CFN_IFLOOR:
15042 : 123277 : CASE_CFN_IRINT:
15043 : 123277 : CASE_CFN_IROUND:
15044 : 123277 : CASE_CFN_LCEIL:
15045 : 123277 : CASE_CFN_LDEXP:
15046 : 123277 : CASE_CFN_LFLOOR:
15047 : 123277 : CASE_CFN_LLCEIL:
15048 : 123277 : CASE_CFN_LLFLOOR:
15049 : 123277 : CASE_CFN_LLRINT:
15050 : 123277 : CASE_CFN_LLRINT_FN:
15051 : 123277 : CASE_CFN_LLROUND:
15052 : 123277 : CASE_CFN_LLROUND_FN:
15053 : 123277 : CASE_CFN_LRINT:
15054 : 123277 : CASE_CFN_LRINT_FN:
15055 : 123277 : CASE_CFN_LROUND:
15056 : 123277 : CASE_CFN_LROUND_FN:
15057 : 123277 : CASE_CFN_MODF:
15058 : 123277 : CASE_CFN_MODF_FN:
15059 : 123277 : CASE_CFN_NEARBYINT:
15060 : 123277 : CASE_CFN_NEARBYINT_FN:
15061 : 123277 : CASE_CFN_RINT:
15062 : 123277 : CASE_CFN_RINT_FN:
15063 : 123277 : CASE_CFN_ROUND:
15064 : 123277 : CASE_CFN_ROUND_FN:
15065 : 123277 : CASE_CFN_ROUNDEVEN:
15066 : 123277 : CASE_CFN_ROUNDEVEN_FN:
15067 : 123277 : CASE_CFN_SCALB:
15068 : 123277 : CASE_CFN_SCALBLN:
15069 : 123277 : CASE_CFN_SCALBLN_FN:
15070 : 123277 : CASE_CFN_SCALBN:
15071 : 123277 : CASE_CFN_SCALBN_FN:
15072 : 123277 : CASE_CFN_SIGNBIT:
15073 : 123277 : CASE_CFN_SIGNIFICAND:
15074 : 123277 : CASE_CFN_SINH:
15075 : 123277 : CASE_CFN_SINH_FN:
15076 : 123277 : CASE_CFN_TANH:
15077 : 123277 : CASE_CFN_TANH_FN:
15078 : 123277 : CASE_CFN_TRUNC:
15079 : 123277 : CASE_CFN_TRUNC_FN:
15080 : : /* True if the 1st argument is nonnegative. */
15081 : 123277 : return RECURSE (arg0);
15082 : :
15083 : 1335 : CASE_CFN_FMAX:
15084 : 1335 : CASE_CFN_FMAX_FN:
15085 : : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15086 : : things. In the presence of sNaNs, we're only guaranteed to be
15087 : : non-negative if both operands are non-negative. In the presence
15088 : : of qNaNs, we're non-negative if either operand is non-negative
15089 : : and can't be a qNaN, or if both operands are non-negative. */
15090 : 1335 : if (tree_expr_maybe_signaling_nan_p (arg0)
15091 : 1335 : || tree_expr_maybe_signaling_nan_p (arg1))
15092 : 136 : return RECURSE (arg0) && RECURSE (arg1);
15093 : 1199 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15094 : 332 : || RECURSE (arg1))
15095 : 867 : : (RECURSE (arg1)
15096 : 867 : && !tree_expr_maybe_nan_p (arg1));
15097 : :
15098 : 946 : CASE_CFN_FMIN:
15099 : 946 : CASE_CFN_FMIN_FN:
15100 : : /* True if the 1st AND 2nd arguments are nonnegative. */
15101 : 946 : return RECURSE (arg0) && RECURSE (arg1);
15102 : :
15103 : 828 : CASE_CFN_COPYSIGN:
15104 : 828 : CASE_CFN_COPYSIGN_FN:
15105 : : /* True if the 2nd argument is nonnegative. */
15106 : 828 : return RECURSE (arg1);
15107 : :
15108 : 2336 : CASE_CFN_POWI:
15109 : : /* True if the 1st argument is nonnegative or the second
15110 : : argument is an even integer. */
15111 : 2336 : if (TREE_CODE (arg1) == INTEGER_CST
15112 : 2336 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15113 : : return true;
15114 : 2255 : return RECURSE (arg0);
15115 : :
15116 : 4906 : CASE_CFN_POW:
15117 : 4906 : CASE_CFN_POW_FN:
15118 : : /* True if the 1st argument is nonnegative or the second
15119 : : argument is an even integer valued real. */
15120 : 4906 : if (TREE_CODE (arg1) == REAL_CST)
15121 : : {
15122 : 2134 : REAL_VALUE_TYPE c;
15123 : 2134 : HOST_WIDE_INT n;
15124 : :
15125 : 2134 : c = TREE_REAL_CST (arg1);
15126 : 2134 : n = real_to_integer (&c);
15127 : 2134 : if ((n & 1) == 0)
15128 : : {
15129 : 1512 : REAL_VALUE_TYPE cint;
15130 : 1512 : real_from_integer (&cint, VOIDmode, n, SIGNED);
15131 : 1512 : if (real_identical (&c, &cint))
15132 : 502 : return true;
15133 : : }
15134 : : }
15135 : 4404 : return RECURSE (arg0);
15136 : :
15137 : 21668319 : default:
15138 : 21668319 : break;
15139 : : }
15140 : 21668319 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15141 : : }
15142 : :
15143 : : /* Return true if T is known to be non-negative. If the return
15144 : : value is based on the assumption that signed overflow is undefined,
15145 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15146 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15147 : :
15148 : : static bool
15149 : 958370 : tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15150 : : {
15151 : 958370 : enum tree_code code = TREE_CODE (t);
15152 : 958370 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15153 : : return true;
15154 : :
15155 : 776616 : switch (code)
15156 : : {
15157 : 217 : case TARGET_EXPR:
15158 : 217 : {
15159 : 217 : tree temp = TARGET_EXPR_SLOT (t);
15160 : 217 : t = TARGET_EXPR_INITIAL (t);
15161 : :
15162 : : /* If the initializer is non-void, then it's a normal expression
15163 : : that will be assigned to the slot. */
15164 : 217 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15165 : 0 : return RECURSE (t);
15166 : :
15167 : : /* Otherwise, the initializer sets the slot in some way. One common
15168 : : way is an assignment statement at the end of the initializer. */
15169 : 419 : while (1)
15170 : : {
15171 : 419 : if (TREE_CODE (t) == BIND_EXPR)
15172 : 202 : t = expr_last (BIND_EXPR_BODY (t));
15173 : 217 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15174 : 217 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15175 : 0 : t = expr_last (TREE_OPERAND (t, 0));
15176 : 217 : else if (TREE_CODE (t) == STATEMENT_LIST)
15177 : 0 : t = expr_last (t);
15178 : : else
15179 : : break;
15180 : : }
15181 : 217 : if (TREE_CODE (t) == MODIFY_EXPR
15182 : 217 : && TREE_OPERAND (t, 0) == temp)
15183 : 202 : return RECURSE (TREE_OPERAND (t, 1));
15184 : :
15185 : : return false;
15186 : : }
15187 : :
15188 : 358252 : case CALL_EXPR:
15189 : 358252 : {
15190 : 358252 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15191 : 358252 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15192 : :
15193 : 358252 : return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15194 : : get_call_combined_fn (t),
15195 : : arg0,
15196 : : arg1,
15197 : 358252 : strict_overflow_p, depth);
15198 : : }
15199 : 767 : case COMPOUND_EXPR:
15200 : 767 : case MODIFY_EXPR:
15201 : 767 : return RECURSE (TREE_OPERAND (t, 1));
15202 : :
15203 : 9 : case BIND_EXPR:
15204 : 9 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15205 : :
15206 : 415711 : case SAVE_EXPR:
15207 : 415711 : return RECURSE (TREE_OPERAND (t, 0));
15208 : :
15209 : 1660 : default:
15210 : 1660 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15211 : : }
15212 : : }
15213 : :
15214 : : #undef RECURSE
15215 : : #undef tree_expr_nonnegative_warnv_p
15216 : :
15217 : : /* Return true if T is known to be non-negative. If the return
15218 : : value is based on the assumption that signed overflow is undefined,
15219 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15220 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15221 : :
15222 : : bool
15223 : 25384056 : tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15224 : : {
15225 : 25384056 : enum tree_code code;
15226 : 25384056 : if (error_operand_p (t))
15227 : : return false;
15228 : :
15229 : 25384055 : code = TREE_CODE (t);
15230 : 25384055 : switch (TREE_CODE_CLASS (code))
15231 : : {
15232 : 1074646 : case tcc_binary:
15233 : 1074646 : case tcc_comparison:
15234 : 1074646 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15235 : 1074646 : TREE_TYPE (t),
15236 : 1074646 : TREE_OPERAND (t, 0),
15237 : 1074646 : TREE_OPERAND (t, 1),
15238 : 1074646 : strict_overflow_p, depth);
15239 : :
15240 : 1663944 : case tcc_unary:
15241 : 1663944 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15242 : 1663944 : TREE_TYPE (t),
15243 : 1663944 : TREE_OPERAND (t, 0),
15244 : 1663944 : strict_overflow_p, depth);
15245 : :
15246 : 11786648 : case tcc_constant:
15247 : 11786648 : case tcc_declaration:
15248 : 11786648 : case tcc_reference:
15249 : 11786648 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15250 : :
15251 : 10858817 : default:
15252 : 10858817 : break;
15253 : : }
15254 : :
15255 : 10858817 : switch (code)
15256 : : {
15257 : 7 : case TRUTH_AND_EXPR:
15258 : 7 : case TRUTH_OR_EXPR:
15259 : 7 : case TRUTH_XOR_EXPR:
15260 : 7 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15261 : 7 : TREE_TYPE (t),
15262 : 7 : TREE_OPERAND (t, 0),
15263 : 7 : TREE_OPERAND (t, 1),
15264 : 7 : strict_overflow_p, depth);
15265 : 72 : case TRUTH_NOT_EXPR:
15266 : 72 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15267 : 72 : TREE_TYPE (t),
15268 : 72 : TREE_OPERAND (t, 0),
15269 : 72 : strict_overflow_p, depth);
15270 : :
15271 : 9900368 : case COND_EXPR:
15272 : 9900368 : case CONSTRUCTOR:
15273 : 9900368 : case OBJ_TYPE_REF:
15274 : 9900368 : case ADDR_EXPR:
15275 : 9900368 : case WITH_SIZE_EXPR:
15276 : 9900368 : case SSA_NAME:
15277 : 9900368 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15278 : :
15279 : 958370 : default:
15280 : 958370 : return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15281 : : }
15282 : : }
15283 : :
15284 : : /* Return true if `t' is known to be non-negative. Handle warnings
15285 : : about undefined signed overflow. */
15286 : :
15287 : : bool
15288 : 17356806 : tree_expr_nonnegative_p (tree t)
15289 : : {
15290 : 17356806 : bool ret, strict_overflow_p;
15291 : :
15292 : 17356806 : strict_overflow_p = false;
15293 : 17356806 : ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15294 : 17356806 : if (strict_overflow_p)
15295 : 17538 : fold_overflow_warning (("assuming signed overflow does not occur when "
15296 : : "determining that expression is always "
15297 : : "non-negative"),
15298 : : WARN_STRICT_OVERFLOW_MISC);
15299 : 17356806 : return ret;
15300 : : }
15301 : :
15302 : :
15303 : : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15304 : : For floating point we further ensure that T is not denormal.
15305 : : Similar logic is present in nonzero_address in rtlanal.h.
15306 : :
15307 : : If the return value is based on the assumption that signed overflow
15308 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15309 : : change *STRICT_OVERFLOW_P. */
15310 : :
15311 : : bool
15312 : 1436233 : tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15313 : : bool *strict_overflow_p)
15314 : : {
15315 : 1436233 : switch (code)
15316 : : {
15317 : 1 : case ABS_EXPR:
15318 : 1 : return tree_expr_nonzero_warnv_p (op0,
15319 : 1 : strict_overflow_p);
15320 : :
15321 : 777358 : case NOP_EXPR:
15322 : 777358 : {
15323 : 777358 : tree inner_type = TREE_TYPE (op0);
15324 : 777358 : tree outer_type = type;
15325 : :
15326 : 777358 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15327 : 777358 : && tree_expr_nonzero_warnv_p (op0,
15328 : : strict_overflow_p));
15329 : : }
15330 : 28113 : break;
15331 : :
15332 : 28113 : case NON_LVALUE_EXPR:
15333 : 28113 : return tree_expr_nonzero_warnv_p (op0,
15334 : 28113 : strict_overflow_p);
15335 : :
15336 : : default:
15337 : : break;
15338 : : }
15339 : :
15340 : : return false;
15341 : : }
15342 : :
15343 : : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15344 : : For floating point we further ensure that T is not denormal.
15345 : : Similar logic is present in nonzero_address in rtlanal.h.
15346 : :
15347 : : If the return value is based on the assumption that signed overflow
15348 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15349 : : change *STRICT_OVERFLOW_P. */
15350 : :
15351 : : bool
15352 : 2970337 : tree_binary_nonzero_warnv_p (enum tree_code code,
15353 : : tree type,
15354 : : tree op0,
15355 : : tree op1, bool *strict_overflow_p)
15356 : : {
15357 : 2970337 : bool sub_strict_overflow_p;
15358 : 2970337 : switch (code)
15359 : : {
15360 : 468496 : case POINTER_PLUS_EXPR:
15361 : 468496 : case PLUS_EXPR:
15362 : 468496 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15363 : : {
15364 : : /* With the presence of negative values it is hard
15365 : : to say something. */
15366 : 101868 : sub_strict_overflow_p = false;
15367 : 101868 : if (!tree_expr_nonnegative_warnv_p (op0,
15368 : : &sub_strict_overflow_p)
15369 : 101868 : || !tree_expr_nonnegative_warnv_p (op1,
15370 : : &sub_strict_overflow_p))
15371 : 99526 : return false;
15372 : : /* One of operands must be positive and the other non-negative. */
15373 : : /* We don't set *STRICT_OVERFLOW_P here: even if this value
15374 : : overflows, on a twos-complement machine the sum of two
15375 : : nonnegative numbers can never be zero. */
15376 : 2342 : return (tree_expr_nonzero_warnv_p (op0,
15377 : : strict_overflow_p)
15378 : 2342 : || tree_expr_nonzero_warnv_p (op1,
15379 : : strict_overflow_p));
15380 : : }
15381 : : break;
15382 : :
15383 : 21069 : case MULT_EXPR:
15384 : 21069 : if (TYPE_OVERFLOW_UNDEFINED (type))
15385 : : {
15386 : 444 : if (tree_expr_nonzero_warnv_p (op0,
15387 : : strict_overflow_p)
15388 : 444 : && tree_expr_nonzero_warnv_p (op1,
15389 : : strict_overflow_p))
15390 : : {
15391 : 0 : *strict_overflow_p = true;
15392 : 0 : return true;
15393 : : }
15394 : : }
15395 : : break;
15396 : :
15397 : 13817 : case MIN_EXPR:
15398 : 13817 : sub_strict_overflow_p = false;
15399 : 13817 : if (tree_expr_nonzero_warnv_p (op0,
15400 : : &sub_strict_overflow_p)
15401 : 13817 : && tree_expr_nonzero_warnv_p (op1,
15402 : : &sub_strict_overflow_p))
15403 : : {
15404 : 0 : if (sub_strict_overflow_p)
15405 : 0 : *strict_overflow_p = true;
15406 : : }
15407 : : break;
15408 : :
15409 : 44 : case MAX_EXPR:
15410 : 44 : sub_strict_overflow_p = false;
15411 : 44 : if (tree_expr_nonzero_warnv_p (op0,
15412 : : &sub_strict_overflow_p))
15413 : : {
15414 : 0 : if (sub_strict_overflow_p)
15415 : 0 : *strict_overflow_p = true;
15416 : :
15417 : : /* When both operands are nonzero, then MAX must be too. */
15418 : 0 : if (tree_expr_nonzero_warnv_p (op1,
15419 : : strict_overflow_p))
15420 : : return true;
15421 : :
15422 : : /* MAX where operand 0 is positive is positive. */
15423 : 0 : return tree_expr_nonnegative_warnv_p (op0,
15424 : 0 : strict_overflow_p);
15425 : : }
15426 : : /* MAX where operand 1 is positive is positive. */
15427 : 44 : else if (tree_expr_nonzero_warnv_p (op1,
15428 : : &sub_strict_overflow_p)
15429 : 44 : && tree_expr_nonnegative_warnv_p (op1,
15430 : : &sub_strict_overflow_p))
15431 : : {
15432 : 0 : if (sub_strict_overflow_p)
15433 : 0 : *strict_overflow_p = true;
15434 : 0 : return true;
15435 : : }
15436 : : break;
15437 : :
15438 : 271061 : case BIT_IOR_EXPR:
15439 : 271061 : return (tree_expr_nonzero_warnv_p (op1,
15440 : : strict_overflow_p)
15441 : 271061 : || tree_expr_nonzero_warnv_p (op0,
15442 : : strict_overflow_p));
15443 : :
15444 : : default:
15445 : : break;
15446 : : }
15447 : :
15448 : : return false;
15449 : : }
15450 : :
15451 : : /* Return true when T is an address and is known to be nonzero.
15452 : : For floating point we further ensure that T is not denormal.
15453 : : Similar logic is present in nonzero_address in rtlanal.h.
15454 : :
15455 : : If the return value is based on the assumption that signed overflow
15456 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15457 : : change *STRICT_OVERFLOW_P. */
15458 : :
15459 : : bool
15460 : 149586339 : tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15461 : : {
15462 : 149586339 : bool sub_strict_overflow_p;
15463 : 149586339 : switch (TREE_CODE (t))
15464 : : {
15465 : 1134946 : case INTEGER_CST:
15466 : 1134946 : return !integer_zerop (t);
15467 : :
15468 : 11595592 : case ADDR_EXPR:
15469 : 11595592 : {
15470 : 11595592 : tree base = TREE_OPERAND (t, 0);
15471 : :
15472 : 11595592 : if (!DECL_P (base))
15473 : 5948481 : base = get_base_address (base);
15474 : :
15475 : 11595592 : if (base && TREE_CODE (base) == TARGET_EXPR)
15476 : 727 : base = TARGET_EXPR_SLOT (base);
15477 : :
15478 : 727 : if (!base)
15479 : 0 : return false;
15480 : :
15481 : : /* For objects in symbol table check if we know they are non-zero.
15482 : : Don't do anything for variables and functions before symtab is built;
15483 : : it is quite possible that they will be declared weak later. */
15484 : 11595592 : int nonzero_addr = maybe_nonzero_address (base);
15485 : 11595592 : if (nonzero_addr >= 0)
15486 : 9132562 : return nonzero_addr;
15487 : :
15488 : : /* Constants are never weak. */
15489 : 2463030 : if (CONSTANT_CLASS_P (base))
15490 : : return true;
15491 : :
15492 : : return false;
15493 : : }
15494 : :
15495 : 31579 : case COND_EXPR:
15496 : 31579 : sub_strict_overflow_p = false;
15497 : 31579 : if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15498 : : &sub_strict_overflow_p)
15499 : 31579 : && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15500 : : &sub_strict_overflow_p))
15501 : : {
15502 : 1222 : if (sub_strict_overflow_p)
15503 : 0 : *strict_overflow_p = true;
15504 : 1222 : return true;
15505 : : }
15506 : : break;
15507 : :
15508 : 126176905 : case SSA_NAME:
15509 : 126176905 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15510 : : break;
15511 : 98304285 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15512 : :
15513 : : default:
15514 : : break;
15515 : : }
15516 : : return false;
15517 : : }
15518 : :
15519 : : #define integer_valued_real_p(X) \
15520 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15521 : :
15522 : : #define RECURSE(X) \
15523 : : ((integer_valued_real_p) (X, depth + 1))
15524 : :
15525 : : /* Return true if the floating point result of (CODE OP0) has an
15526 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15527 : : integer values. Return false for signaling NaN.
15528 : :
15529 : : DEPTH is the current nesting depth of the query. */
15530 : :
15531 : : bool
15532 : 14869 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15533 : : {
15534 : 14869 : switch (code)
15535 : : {
15536 : : case FLOAT_EXPR:
15537 : : return true;
15538 : :
15539 : 1406 : case ABS_EXPR:
15540 : 1406 : return RECURSE (op0);
15541 : :
15542 : 9841 : CASE_CONVERT:
15543 : 9841 : {
15544 : 9841 : tree type = TREE_TYPE (op0);
15545 : 9841 : if (TREE_CODE (type) == INTEGER_TYPE)
15546 : : return true;
15547 : 9841 : if (SCALAR_FLOAT_TYPE_P (type))
15548 : 9841 : return RECURSE (op0);
15549 : : break;
15550 : : }
15551 : :
15552 : : default:
15553 : : break;
15554 : : }
15555 : : return false;
15556 : : }
15557 : :
15558 : : /* Return true if the floating point result of (CODE OP0 OP1) has an
15559 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15560 : : integer values. Return false for signaling NaN.
15561 : :
15562 : : DEPTH is the current nesting depth of the query. */
15563 : :
15564 : : bool
15565 : 13236 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15566 : : {
15567 : 13236 : switch (code)
15568 : : {
15569 : 7000 : case PLUS_EXPR:
15570 : 7000 : case MINUS_EXPR:
15571 : 7000 : case MULT_EXPR:
15572 : 7000 : case MIN_EXPR:
15573 : 7000 : case MAX_EXPR:
15574 : 7000 : return RECURSE (op0) && RECURSE (op1);
15575 : :
15576 : : default:
15577 : : break;
15578 : : }
15579 : : return false;
15580 : : }
15581 : :
15582 : : /* Return true if the floating point result of calling FNDECL with arguments
15583 : : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15584 : : considered integer values. Return false for signaling NaN. If FNDECL
15585 : : takes fewer than 2 arguments, the remaining ARGn are null.
15586 : :
15587 : : DEPTH is the current nesting depth of the query. */
15588 : :
15589 : : bool
15590 : 989 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15591 : : {
15592 : 989 : switch (fn)
15593 : : {
15594 : : CASE_CFN_CEIL:
15595 : : CASE_CFN_CEIL_FN:
15596 : : CASE_CFN_FLOOR:
15597 : : CASE_CFN_FLOOR_FN:
15598 : : CASE_CFN_NEARBYINT:
15599 : : CASE_CFN_NEARBYINT_FN:
15600 : : CASE_CFN_RINT:
15601 : : CASE_CFN_RINT_FN:
15602 : : CASE_CFN_ROUND:
15603 : : CASE_CFN_ROUND_FN:
15604 : : CASE_CFN_ROUNDEVEN:
15605 : : CASE_CFN_ROUNDEVEN_FN:
15606 : : CASE_CFN_TRUNC:
15607 : : CASE_CFN_TRUNC_FN:
15608 : : return true;
15609 : :
15610 : 336 : CASE_CFN_FMIN:
15611 : 336 : CASE_CFN_FMIN_FN:
15612 : 336 : CASE_CFN_FMAX:
15613 : 336 : CASE_CFN_FMAX_FN:
15614 : 336 : return RECURSE (arg0) && RECURSE (arg1);
15615 : :
15616 : : default:
15617 : : break;
15618 : : }
15619 : : return false;
15620 : : }
15621 : :
15622 : : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15623 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15624 : : considered integer values. Return false for signaling NaN.
15625 : :
15626 : : DEPTH is the current nesting depth of the query. */
15627 : :
15628 : : bool
15629 : 126996 : integer_valued_real_single_p (tree t, int depth)
15630 : : {
15631 : 126996 : switch (TREE_CODE (t))
15632 : : {
15633 : 2128 : case REAL_CST:
15634 : 2128 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15635 : :
15636 : 0 : case COND_EXPR:
15637 : 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15638 : :
15639 : 89056 : case SSA_NAME:
15640 : : /* Limit the depth of recursion to avoid quadratic behavior.
15641 : : This is expected to catch almost all occurrences in practice.
15642 : : If this code misses important cases that unbounded recursion
15643 : : would not, passes that need this information could be revised
15644 : : to provide it through dataflow propagation. */
15645 : 89056 : return (!name_registered_for_update_p (t)
15646 : 89056 : && depth < param_max_ssa_name_query_depth
15647 : 177378 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15648 : : depth));
15649 : :
15650 : : default:
15651 : : break;
15652 : : }
15653 : : return false;
15654 : : }
15655 : :
15656 : : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15657 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15658 : : considered integer values. Return false for signaling NaN.
15659 : :
15660 : : DEPTH is the current nesting depth of the query. */
15661 : :
15662 : : static bool
15663 : 0 : integer_valued_real_invalid_p (tree t, int depth)
15664 : : {
15665 : 0 : switch (TREE_CODE (t))
15666 : : {
15667 : 0 : case COMPOUND_EXPR:
15668 : 0 : case MODIFY_EXPR:
15669 : 0 : case BIND_EXPR:
15670 : 0 : return RECURSE (TREE_OPERAND (t, 1));
15671 : :
15672 : 0 : case SAVE_EXPR:
15673 : 0 : return RECURSE (TREE_OPERAND (t, 0));
15674 : :
15675 : : default:
15676 : : break;
15677 : : }
15678 : : return false;
15679 : : }
15680 : :
15681 : : #undef RECURSE
15682 : : #undef integer_valued_real_p
15683 : :
15684 : : /* Return true if the floating point expression T has an integer value.
15685 : : We also allow +Inf, -Inf and NaN to be considered integer values.
15686 : : Return false for signaling NaN.
15687 : :
15688 : : DEPTH is the current nesting depth of the query. */
15689 : :
15690 : : bool
15691 : 95965 : integer_valued_real_p (tree t, int depth)
15692 : : {
15693 : 95965 : if (t == error_mark_node)
15694 : : return false;
15695 : :
15696 : 95965 : STRIP_ANY_LOCATION_WRAPPER (t);
15697 : :
15698 : 95965 : tree_code code = TREE_CODE (t);
15699 : 95965 : switch (TREE_CODE_CLASS (code))
15700 : : {
15701 : 0 : case tcc_binary:
15702 : 0 : case tcc_comparison:
15703 : 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15704 : 0 : TREE_OPERAND (t, 1), depth);
15705 : :
15706 : 0 : case tcc_unary:
15707 : 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15708 : :
15709 : 8317 : case tcc_constant:
15710 : 8317 : case tcc_declaration:
15711 : 8317 : case tcc_reference:
15712 : 8317 : return integer_valued_real_single_p (t, depth);
15713 : :
15714 : 87648 : default:
15715 : 87648 : break;
15716 : : }
15717 : :
15718 : 87648 : switch (code)
15719 : : {
15720 : 87648 : case COND_EXPR:
15721 : 87648 : case SSA_NAME:
15722 : 87648 : return integer_valued_real_single_p (t, depth);
15723 : :
15724 : 0 : case CALL_EXPR:
15725 : 0 : {
15726 : 0 : tree arg0 = (call_expr_nargs (t) > 0
15727 : 0 : ? CALL_EXPR_ARG (t, 0)
15728 : 0 : : NULL_TREE);
15729 : 0 : tree arg1 = (call_expr_nargs (t) > 1
15730 : 0 : ? CALL_EXPR_ARG (t, 1)
15731 : 0 : : NULL_TREE);
15732 : 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15733 : 0 : arg0, arg1, depth);
15734 : : }
15735 : :
15736 : 0 : default:
15737 : 0 : return integer_valued_real_invalid_p (t, depth);
15738 : : }
15739 : : }
15740 : :
15741 : : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15742 : : attempt to fold the expression to a constant without modifying TYPE,
15743 : : OP0 or OP1.
15744 : :
15745 : : If the expression could be simplified to a constant, then return
15746 : : the constant. If the expression would not be simplified to a
15747 : : constant, then return NULL_TREE. */
15748 : :
15749 : : tree
15750 : 15752137 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15751 : : {
15752 : 15752137 : tree tem = fold_binary (code, type, op0, op1);
15753 : 15752137 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15754 : : }
15755 : :
15756 : : /* Given the components of a unary expression CODE, TYPE and OP0,
15757 : : attempt to fold the expression to a constant without modifying
15758 : : TYPE or OP0.
15759 : :
15760 : : If the expression could be simplified to a constant, then return
15761 : : the constant. If the expression would not be simplified to a
15762 : : constant, then return NULL_TREE. */
15763 : :
15764 : : tree
15765 : 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15766 : : {
15767 : 0 : tree tem = fold_unary (code, type, op0);
15768 : 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15769 : : }
15770 : :
15771 : : /* If EXP represents referencing an element in a constant string
15772 : : (either via pointer arithmetic or array indexing), return the
15773 : : tree representing the value accessed, otherwise return NULL. */
15774 : :
15775 : : tree
15776 : 165214077 : fold_read_from_constant_string (tree exp)
15777 : : {
15778 : 165214077 : if ((INDIRECT_REF_P (exp)
15779 : 165214059 : || TREE_CODE (exp) == ARRAY_REF)
15780 : 176335972 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15781 : : {
15782 : 7945448 : tree exp1 = TREE_OPERAND (exp, 0);
15783 : 7945448 : tree index;
15784 : 7945448 : tree string;
15785 : 7945448 : location_t loc = EXPR_LOCATION (exp);
15786 : :
15787 : 7945448 : if (INDIRECT_REF_P (exp))
15788 : 0 : string = string_constant (exp1, &index, NULL, NULL);
15789 : : else
15790 : : {
15791 : 7945448 : tree low_bound = array_ref_low_bound (exp);
15792 : 7945448 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15793 : :
15794 : : /* Optimize the special-case of a zero lower bound.
15795 : :
15796 : : We convert the low_bound to sizetype to avoid some problems
15797 : : with constant folding. (E.g. suppose the lower bound is 1,
15798 : : and its mode is QI. Without the conversion,l (ARRAY
15799 : : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15800 : : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15801 : 7945448 : if (! integer_zerop (low_bound))
15802 : 149023 : index = size_diffop_loc (loc, index,
15803 : : fold_convert_loc (loc, sizetype, low_bound));
15804 : :
15805 : : string = exp1;
15806 : : }
15807 : :
15808 : 7945448 : scalar_int_mode char_mode;
15809 : 7945448 : if (string
15810 : 7945448 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15811 : 7945448 : && TREE_CODE (string) == STRING_CST
15812 : 62036 : && tree_fits_uhwi_p (index)
15813 : 56451 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15814 : 8001867 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15815 : : &char_mode)
15816 : 15890896 : && GET_MODE_SIZE (char_mode) == 1)
15817 : 109706 : return build_int_cst_type (TREE_TYPE (exp),
15818 : 54853 : (TREE_STRING_POINTER (string)
15819 : 54853 : [TREE_INT_CST_LOW (index)]));
15820 : : }
15821 : : return NULL;
15822 : : }
15823 : :
15824 : : /* Folds a read from vector element at IDX of vector ARG. */
15825 : :
15826 : : tree
15827 : 5487 : fold_read_from_vector (tree arg, poly_uint64 idx)
15828 : : {
15829 : 5487 : unsigned HOST_WIDE_INT i;
15830 : 5487 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15831 : 5487 : && known_ge (idx, 0u)
15832 : 5487 : && idx.is_constant (&i))
15833 : : {
15834 : 5487 : if (TREE_CODE (arg) == VECTOR_CST)
15835 : 1947 : return VECTOR_CST_ELT (arg, i);
15836 : 3540 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15837 : : {
15838 : 1590 : if (CONSTRUCTOR_NELTS (arg)
15839 : 1550 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15840 : : return NULL_TREE;
15841 : 1588 : if (i >= CONSTRUCTOR_NELTS (arg))
15842 : 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15843 : 1548 : return CONSTRUCTOR_ELT (arg, i)->value;
15844 : : }
15845 : : }
15846 : : return NULL_TREE;
15847 : : }
15848 : :
15849 : : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15850 : : an integer constant, real, or fixed-point constant.
15851 : :
15852 : : TYPE is the type of the result. */
15853 : :
15854 : : static tree
15855 : 31398929 : fold_negate_const (tree arg0, tree type)
15856 : : {
15857 : 31398929 : tree t = NULL_TREE;
15858 : :
15859 : 31398929 : switch (TREE_CODE (arg0))
15860 : : {
15861 : 2078100 : case REAL_CST:
15862 : 2078100 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15863 : 2078100 : break;
15864 : :
15865 : 0 : case FIXED_CST:
15866 : 0 : {
15867 : 0 : FIXED_VALUE_TYPE f;
15868 : 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15869 : 0 : &(TREE_FIXED_CST (arg0)), NULL,
15870 : 0 : TYPE_SATURATING (type));
15871 : 0 : t = build_fixed (type, f);
15872 : : /* Propagate overflow flags. */
15873 : 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15874 : 0 : TREE_OVERFLOW (t) = 1;
15875 : 0 : break;
15876 : : }
15877 : :
15878 : 29320829 : default:
15879 : 29320829 : if (poly_int_tree_p (arg0))
15880 : : {
15881 : 29320829 : wi::overflow_type overflow;
15882 : 29320829 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15883 : 29320829 : t = force_fit_type (type, res, 1,
15884 : 258075 : (overflow && ! TYPE_UNSIGNED (type))
15885 : 29569669 : || TREE_OVERFLOW (arg0));
15886 : 29320829 : break;
15887 : 29320829 : }
15888 : :
15889 : 0 : gcc_unreachable ();
15890 : : }
15891 : :
15892 : 31398929 : return t;
15893 : : }
15894 : :
15895 : : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15896 : : an integer constant or real constant.
15897 : :
15898 : : TYPE is the type of the result. */
15899 : :
15900 : : tree
15901 : 34319 : fold_abs_const (tree arg0, tree type)
15902 : : {
15903 : 34319 : tree t = NULL_TREE;
15904 : :
15905 : 34319 : switch (TREE_CODE (arg0))
15906 : : {
15907 : 6795 : case INTEGER_CST:
15908 : 6795 : {
15909 : : /* If the value is unsigned or non-negative, then the absolute value
15910 : : is the same as the ordinary value. */
15911 : 6795 : wide_int val = wi::to_wide (arg0);
15912 : 6795 : wi::overflow_type overflow = wi::OVF_NONE;
15913 : 6795 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15914 : : ;
15915 : :
15916 : : /* If the value is negative, then the absolute value is
15917 : : its negation. */
15918 : : else
15919 : 2986 : val = wi::neg (val, &overflow);
15920 : :
15921 : : /* Force to the destination type, set TREE_OVERFLOW for signed
15922 : : TYPE only. */
15923 : 6795 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15924 : 6795 : }
15925 : 6795 : break;
15926 : :
15927 : 27524 : case REAL_CST:
15928 : 27524 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15929 : 7332 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15930 : : else
15931 : : t = arg0;
15932 : : break;
15933 : :
15934 : 0 : default:
15935 : 0 : gcc_unreachable ();
15936 : : }
15937 : :
15938 : 34319 : return t;
15939 : : }
15940 : :
15941 : : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15942 : : constant. TYPE is the type of the result. */
15943 : :
15944 : : static tree
15945 : 2235859 : fold_not_const (const_tree arg0, tree type)
15946 : : {
15947 : 2235859 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15948 : :
15949 : 2235859 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15950 : : }
15951 : :
15952 : : /* Given CODE, a relational operator, the target type, TYPE and two
15953 : : constant operands OP0 and OP1, return the result of the
15954 : : relational operation. If the result is not a compile time
15955 : : constant, then return NULL_TREE. */
15956 : :
15957 : : static tree
15958 : 58041498 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15959 : : {
15960 : 58041498 : int result, invert;
15961 : :
15962 : : /* From here on, the only cases we handle are when the result is
15963 : : known to be a constant. */
15964 : :
15965 : 58041498 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15966 : : {
15967 : 1109300 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15968 : 1109300 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15969 : :
15970 : : /* Handle the cases where either operand is a NaN. */
15971 : 1109300 : if (real_isnan (c0) || real_isnan (c1))
15972 : : {
15973 : 14590 : switch (code)
15974 : : {
15975 : : case EQ_EXPR:
15976 : : case ORDERED_EXPR:
15977 : : result = 0;
15978 : : break;
15979 : :
15980 : : case NE_EXPR:
15981 : : case UNORDERED_EXPR:
15982 : : case UNLT_EXPR:
15983 : : case UNLE_EXPR:
15984 : : case UNGT_EXPR:
15985 : : case UNGE_EXPR:
15986 : : case UNEQ_EXPR:
15987 : 6619 : result = 1;
15988 : : break;
15989 : :
15990 : 7993 : case LT_EXPR:
15991 : 7993 : case LE_EXPR:
15992 : 7993 : case GT_EXPR:
15993 : 7993 : case GE_EXPR:
15994 : 7993 : case LTGT_EXPR:
15995 : 7993 : if (flag_trapping_math)
15996 : : return NULL_TREE;
15997 : : result = 0;
15998 : : break;
15999 : :
16000 : 0 : default:
16001 : 0 : gcc_unreachable ();
16002 : : }
16003 : :
16004 : 6619 : return constant_boolean_node (result, type);
16005 : : }
16006 : :
16007 : 1094710 : return constant_boolean_node (real_compare (code, c0, c1), type);
16008 : : }
16009 : :
16010 : 56932198 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
16011 : : {
16012 : 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16013 : 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16014 : 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
16015 : : }
16016 : :
16017 : : /* Handle equality/inequality of complex constants. */
16018 : 56932198 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16019 : : {
16020 : 58302 : tree rcond = fold_relational_const (code, type,
16021 : 29151 : TREE_REALPART (op0),
16022 : 29151 : TREE_REALPART (op1));
16023 : 116604 : tree icond = fold_relational_const (code, type,
16024 : 29151 : TREE_IMAGPART (op0),
16025 : 29151 : TREE_IMAGPART (op1));
16026 : 29151 : if (code == EQ_EXPR)
16027 : 286 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16028 : 28865 : else if (code == NE_EXPR)
16029 : 28865 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16030 : : else
16031 : : return NULL_TREE;
16032 : : }
16033 : :
16034 : 56903047 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16035 : : {
16036 : 9053 : if (!VECTOR_TYPE_P (type))
16037 : : {
16038 : : /* Have vector comparison with scalar boolean result. */
16039 : 200 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
16040 : : && known_eq (VECTOR_CST_NELTS (op0),
16041 : : VECTOR_CST_NELTS (op1)));
16042 : 200 : unsigned HOST_WIDE_INT nunits;
16043 : 200 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
16044 : : return NULL_TREE;
16045 : 887 : for (unsigned i = 0; i < nunits; i++)
16046 : : {
16047 : 794 : tree elem0 = VECTOR_CST_ELT (op0, i);
16048 : 794 : tree elem1 = VECTOR_CST_ELT (op1, i);
16049 : 794 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
16050 : 794 : if (tmp == NULL_TREE)
16051 : : return NULL_TREE;
16052 : 794 : if (integer_zerop (tmp))
16053 : 107 : return constant_boolean_node (code == NE_EXPR, type);
16054 : : }
16055 : 93 : return constant_boolean_node (code == EQ_EXPR, type);
16056 : : }
16057 : 8853 : tree_vector_builder elts;
16058 : 8853 : if (!elts.new_binary_operation (type, op0, op1, false))
16059 : : return NULL_TREE;
16060 : 8853 : unsigned int count = elts.encoded_nelts ();
16061 : 46430 : for (unsigned i = 0; i < count; i++)
16062 : : {
16063 : 37577 : tree elem_type = TREE_TYPE (type);
16064 : 37577 : tree elem0 = VECTOR_CST_ELT (op0, i);
16065 : 37577 : tree elem1 = VECTOR_CST_ELT (op1, i);
16066 : :
16067 : 37577 : tree tem = fold_relational_const (code, elem_type,
16068 : : elem0, elem1);
16069 : :
16070 : 37577 : if (tem == NULL_TREE)
16071 : : return NULL_TREE;
16072 : :
16073 : 37577 : elts.quick_push (build_int_cst (elem_type,
16074 : 55793 : integer_zerop (tem) ? 0 : -1));
16075 : : }
16076 : :
16077 : 8853 : return elts.build ();
16078 : 8853 : }
16079 : :
16080 : : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16081 : :
16082 : : To compute GT, swap the arguments and do LT.
16083 : : To compute GE, do LT and invert the result.
16084 : : To compute LE, swap the arguments, do LT and invert the result.
16085 : : To compute NE, do EQ and invert the result.
16086 : :
16087 : : Therefore, the code below must handle only EQ and LT. */
16088 : :
16089 : 56893994 : if (code == LE_EXPR || code == GT_EXPR)
16090 : : {
16091 : 11488257 : std::swap (op0, op1);
16092 : 11488257 : code = swap_tree_comparison (code);
16093 : : }
16094 : :
16095 : : /* Note that it is safe to invert for real values here because we
16096 : : have already handled the one case that it matters. */
16097 : :
16098 : 56893994 : invert = 0;
16099 : 56893994 : if (code == NE_EXPR || code == GE_EXPR)
16100 : : {
16101 : 27994588 : invert = 1;
16102 : 27994588 : code = invert_tree_comparison (code, false);
16103 : : }
16104 : :
16105 : : /* Compute a result for LT or EQ if args permit;
16106 : : Otherwise return T. */
16107 : 56893994 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16108 : : {
16109 : 56872845 : if (code == EQ_EXPR)
16110 : 28984079 : result = tree_int_cst_equal (op0, op1);
16111 : : else
16112 : 27888766 : result = tree_int_cst_lt (op0, op1);
16113 : : }
16114 : : else
16115 : : return NULL_TREE;
16116 : :
16117 : 56872845 : if (invert)
16118 : 27993868 : result ^= 1;
16119 : 56872845 : return constant_boolean_node (result, type);
16120 : : }
16121 : :
16122 : : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16123 : : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16124 : : itself. */
16125 : :
16126 : : tree
16127 : 115480465 : fold_build_cleanup_point_expr (tree type, tree expr)
16128 : : {
16129 : : /* If the expression does not have side effects then we don't have to wrap
16130 : : it with a cleanup point expression. */
16131 : 115480465 : if (!TREE_SIDE_EFFECTS (expr))
16132 : : return expr;
16133 : :
16134 : : /* If the expression is a return, check to see if the expression inside the
16135 : : return has no side effects or the right hand side of the modify expression
16136 : : inside the return. If either don't have side effects set we don't need to
16137 : : wrap the expression in a cleanup point expression. Note we don't check the
16138 : : left hand side of the modify because it should always be a return decl. */
16139 : 99639742 : if (TREE_CODE (expr) == RETURN_EXPR)
16140 : : {
16141 : 37560980 : tree op = TREE_OPERAND (expr, 0);
16142 : 37560980 : if (!op || !TREE_SIDE_EFFECTS (op))
16143 : : return expr;
16144 : 36870465 : op = TREE_OPERAND (op, 1);
16145 : 36870465 : if (!TREE_SIDE_EFFECTS (op))
16146 : : return expr;
16147 : : }
16148 : :
16149 : 79944651 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16150 : : }
16151 : :
16152 : : /* Given a pointer value OP0 and a type TYPE, return a simplified version
16153 : : of an indirection through OP0, or NULL_TREE if no simplification is
16154 : : possible. */
16155 : :
16156 : : tree
16157 : 18823974 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16158 : : {
16159 : 18823974 : tree sub = op0;
16160 : 18823974 : tree subtype;
16161 : 18823974 : poly_uint64 const_op01;
16162 : :
16163 : 18823974 : STRIP_NOPS (sub);
16164 : 18823974 : subtype = TREE_TYPE (sub);
16165 : 18823974 : if (!POINTER_TYPE_P (subtype)
16166 : 18823974 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16167 : : return NULL_TREE;
16168 : :
16169 : 18678127 : if (TREE_CODE (sub) == ADDR_EXPR)
16170 : : {
16171 : 3318536 : tree op = TREE_OPERAND (sub, 0);
16172 : 3318536 : tree optype = TREE_TYPE (op);
16173 : :
16174 : : /* *&CONST_DECL -> to the value of the const decl. */
16175 : 3318536 : if (TREE_CODE (op) == CONST_DECL)
16176 : 2785 : return DECL_INITIAL (op);
16177 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
16178 : 3315751 : if (type == optype)
16179 : : {
16180 : 2422835 : tree fop = fold_read_from_constant_string (op);
16181 : 2422835 : if (fop)
16182 : : return fop;
16183 : : else
16184 : 2380322 : return op;
16185 : : }
16186 : : /* *(foo *)&fooarray => fooarray[0] */
16187 : 892916 : else if (TREE_CODE (optype) == ARRAY_TYPE
16188 : 13217 : && type == TREE_TYPE (optype)
16189 : 905005 : && (!in_gimple_form
16190 : 2645 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16191 : : {
16192 : 12089 : tree type_domain = TYPE_DOMAIN (optype);
16193 : 12089 : tree min_val = size_zero_node;
16194 : 12089 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16195 : 12051 : min_val = TYPE_MIN_VALUE (type_domain);
16196 : 12089 : if (in_gimple_form
16197 : 2645 : && TREE_CODE (min_val) != INTEGER_CST)
16198 : : return NULL_TREE;
16199 : 12089 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16200 : 12089 : NULL_TREE, NULL_TREE);
16201 : : }
16202 : : /* *(foo *)&complexfoo => __real__ complexfoo */
16203 : 880827 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16204 : 880827 : && type == TREE_TYPE (optype))
16205 : 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16206 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16207 : 880827 : else if (VECTOR_TYPE_P (optype)
16208 : 880827 : && type == TREE_TYPE (optype))
16209 : : {
16210 : 70 : tree part_width = TYPE_SIZE (type);
16211 : 70 : tree index = bitsize_int (0);
16212 : 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16213 : 70 : index);
16214 : : }
16215 : : }
16216 : :
16217 : 16240348 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16218 : 16240348 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16219 : : {
16220 : 262121 : tree op00 = TREE_OPERAND (sub, 0);
16221 : 262121 : tree op01 = TREE_OPERAND (sub, 1);
16222 : :
16223 : 262121 : STRIP_NOPS (op00);
16224 : 262121 : if (TREE_CODE (op00) == ADDR_EXPR)
16225 : : {
16226 : 2862 : tree op00type;
16227 : 2862 : op00 = TREE_OPERAND (op00, 0);
16228 : 2862 : op00type = TREE_TYPE (op00);
16229 : :
16230 : : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16231 : 2862 : if (VECTOR_TYPE_P (op00type)
16232 : 240 : && type == TREE_TYPE (op00type)
16233 : : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16234 : : but we want to treat offsets with MSB set as negative.
16235 : : For the code below negative offsets are invalid and
16236 : : TYPE_SIZE of the element is something unsigned, so
16237 : : check whether op01 fits into poly_int64, which implies
16238 : : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16239 : : then just use poly_uint64 because we want to treat the
16240 : : value as unsigned. */
16241 : 3055 : && tree_fits_poly_int64_p (op01))
16242 : : {
16243 : 179 : tree part_width = TYPE_SIZE (type);
16244 : 179 : poly_uint64 max_offset
16245 : 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16246 : 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16247 : 179 : if (known_lt (const_op01, max_offset))
16248 : : {
16249 : 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16250 : 179 : return fold_build3_loc (loc,
16251 : : BIT_FIELD_REF, type, op00,
16252 : 179 : part_width, index);
16253 : : }
16254 : : }
16255 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16256 : 2683 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16257 : 2683 : && type == TREE_TYPE (op00type))
16258 : : {
16259 : 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16260 : : const_op01))
16261 : 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16262 : : }
16263 : : /* ((foo *)&fooarray)[1] => fooarray[1] */
16264 : 2683 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16265 : 2683 : && type == TREE_TYPE (op00type))
16266 : : {
16267 : 1169 : tree type_domain = TYPE_DOMAIN (op00type);
16268 : 1169 : tree min_val = size_zero_node;
16269 : 1169 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16270 : 1168 : min_val = TYPE_MIN_VALUE (type_domain);
16271 : 1169 : poly_uint64 type_size, index;
16272 : 1169 : if (poly_int_tree_p (min_val)
16273 : 1169 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16274 : 1169 : && multiple_p (const_op01, type_size, &index))
16275 : : {
16276 : 1169 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16277 : 1169 : op01 = wide_int_to_tree (sizetype, off);
16278 : 1169 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16279 : : NULL_TREE, NULL_TREE);
16280 : : }
16281 : : }
16282 : : }
16283 : : }
16284 : :
16285 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16286 : 16239000 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16287 : 649608 : && type == TREE_TYPE (TREE_TYPE (subtype))
16288 : 16241979 : && (!in_gimple_form
16289 : 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16290 : : {
16291 : 2978 : tree type_domain;
16292 : 2978 : tree min_val = size_zero_node;
16293 : 2978 : sub = build_fold_indirect_ref_loc (loc, sub);
16294 : 2978 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16295 : 2978 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16296 : 2978 : min_val = TYPE_MIN_VALUE (type_domain);
16297 : 2978 : if (in_gimple_form
16298 : 11 : && TREE_CODE (min_val) != INTEGER_CST)
16299 : : return NULL_TREE;
16300 : 2978 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16301 : 2978 : NULL_TREE);
16302 : : }
16303 : :
16304 : : return NULL_TREE;
16305 : : }
16306 : :
16307 : : /* Builds an expression for an indirection through T, simplifying some
16308 : : cases. */
16309 : :
16310 : : tree
16311 : 8219813 : build_fold_indirect_ref_loc (location_t loc, tree t)
16312 : : {
16313 : 8219813 : tree type = TREE_TYPE (TREE_TYPE (t));
16314 : 8219813 : tree sub = fold_indirect_ref_1 (loc, type, t);
16315 : :
16316 : 8219813 : if (sub)
16317 : : return sub;
16318 : :
16319 : 5800392 : return build1_loc (loc, INDIRECT_REF, type, t);
16320 : : }
16321 : :
16322 : : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16323 : :
16324 : : tree
16325 : 10299149 : fold_indirect_ref_loc (location_t loc, tree t)
16326 : : {
16327 : 10299149 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16328 : :
16329 : 10299149 : if (sub)
16330 : : return sub;
16331 : : else
16332 : 10278498 : return t;
16333 : : }
16334 : :
16335 : : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16336 : : whose result is ignored. The type of the returned tree need not be
16337 : : the same as the original expression. */
16338 : :
16339 : : tree
16340 : 137380 : fold_ignored_result (tree t)
16341 : : {
16342 : 137380 : if (!TREE_SIDE_EFFECTS (t))
16343 : 18750 : return integer_zero_node;
16344 : :
16345 : 158693 : for (;;)
16346 : 158693 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16347 : : {
16348 : 3718 : case tcc_unary:
16349 : 3718 : t = TREE_OPERAND (t, 0);
16350 : 3718 : break;
16351 : :
16352 : 5030 : case tcc_binary:
16353 : 5030 : case tcc_comparison:
16354 : 5030 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16355 : 3139 : t = TREE_OPERAND (t, 0);
16356 : 1891 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16357 : 19 : t = TREE_OPERAND (t, 1);
16358 : : else
16359 : : return t;
16360 : : break;
16361 : :
16362 : 102990 : case tcc_expression:
16363 : 102990 : switch (TREE_CODE (t))
16364 : : {
16365 : 33172 : case COMPOUND_EXPR:
16366 : 33172 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16367 : : return t;
16368 : 32893 : t = TREE_OPERAND (t, 0);
16369 : 32893 : break;
16370 : :
16371 : 381 : case COND_EXPR:
16372 : 381 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16373 : 381 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16374 : : return t;
16375 : 294 : t = TREE_OPERAND (t, 0);
16376 : 294 : break;
16377 : :
16378 : : default:
16379 : : return t;
16380 : : }
16381 : : break;
16382 : :
16383 : : default:
16384 : : return t;
16385 : : }
16386 : : }
16387 : :
16388 : : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16389 : :
16390 : : tree
16391 : 2744096092 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16392 : : {
16393 : 2744096092 : tree div = NULL_TREE;
16394 : :
16395 : 2744096092 : if (divisor == 1)
16396 : : return value;
16397 : :
16398 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16399 : : have to do anything. Only do this when we are not given a const,
16400 : : because in that case, this check is more expensive than just
16401 : : doing it. */
16402 : 1705216487 : if (TREE_CODE (value) != INTEGER_CST)
16403 : : {
16404 : 344122 : div = build_int_cst (TREE_TYPE (value), divisor);
16405 : :
16406 : 344122 : if (multiple_of_p (TREE_TYPE (value), value, div))
16407 : : return value;
16408 : : }
16409 : :
16410 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16411 : 1704874198 : if (pow2_or_zerop (divisor))
16412 : : {
16413 : 1704874198 : if (TREE_CODE (value) == INTEGER_CST)
16414 : : {
16415 : 1704872365 : wide_int val = wi::to_wide (value);
16416 : 1704872365 : bool overflow_p;
16417 : :
16418 : 1704872365 : if ((val & (divisor - 1)) == 0)
16419 : : return value;
16420 : :
16421 : 3629205 : overflow_p = TREE_OVERFLOW (value);
16422 : 3629205 : val += divisor - 1;
16423 : 3629205 : val &= (int) -divisor;
16424 : 3629205 : if (val == 0)
16425 : 6 : overflow_p = true;
16426 : :
16427 : 3629205 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16428 : 1704872365 : }
16429 : : else
16430 : : {
16431 : 1833 : tree t;
16432 : :
16433 : 1833 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16434 : 1833 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16435 : 1833 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16436 : 1833 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16437 : : }
16438 : : }
16439 : : else
16440 : : {
16441 : 0 : if (!div)
16442 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16443 : 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16444 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16445 : : }
16446 : :
16447 : : return value;
16448 : : }
16449 : :
16450 : : /* Likewise, but round down. */
16451 : :
16452 : : tree
16453 : 18095063 : round_down_loc (location_t loc, tree value, int divisor)
16454 : : {
16455 : 18095063 : tree div = NULL_TREE;
16456 : :
16457 : 18095063 : gcc_assert (divisor > 0);
16458 : 18095063 : if (divisor == 1)
16459 : : return value;
16460 : :
16461 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16462 : : have to do anything. Only do this when we are not given a const,
16463 : : because in that case, this check is more expensive than just
16464 : : doing it. */
16465 : 18095063 : if (TREE_CODE (value) != INTEGER_CST)
16466 : : {
16467 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16468 : :
16469 : 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16470 : : return value;
16471 : : }
16472 : :
16473 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16474 : 18095063 : if (pow2_or_zerop (divisor))
16475 : : {
16476 : 18095063 : tree t;
16477 : :
16478 : 18095063 : t = build_int_cst (TREE_TYPE (value), -divisor);
16479 : 18095063 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16480 : : }
16481 : : else
16482 : : {
16483 : 0 : if (!div)
16484 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16485 : 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16486 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16487 : : }
16488 : :
16489 : : return value;
16490 : : }
16491 : :
16492 : : /* Returns the pointer to the base of the object addressed by EXP and
16493 : : extracts the information about the offset of the access, storing it
16494 : : to PBITPOS and POFFSET. */
16495 : :
16496 : : static tree
16497 : 1688660 : split_address_to_core_and_offset (tree exp,
16498 : : poly_int64 *pbitpos, tree *poffset)
16499 : : {
16500 : 1688660 : tree core;
16501 : 1688660 : machine_mode mode;
16502 : 1688660 : int unsignedp, reversep, volatilep;
16503 : 1688660 : poly_int64 bitsize;
16504 : 1688660 : location_t loc = EXPR_LOCATION (exp);
16505 : :
16506 : 1688660 : if (TREE_CODE (exp) == SSA_NAME)
16507 : 524979 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16508 : 365933 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16509 : 30027 : exp = gimple_assign_rhs1 (def);
16510 : :
16511 : 1688660 : if (TREE_CODE (exp) == ADDR_EXPR)
16512 : : {
16513 : 985936 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16514 : : poffset, &mode, &unsignedp, &reversep,
16515 : : &volatilep);
16516 : : /* If we are left with MEM[a + CST] strip that and add it to the
16517 : : pbitpos and return a. */
16518 : 985936 : if (TREE_CODE (core) == MEM_REF)
16519 : : {
16520 : 27090 : poly_offset_int tem;
16521 : 27090 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16522 : 27090 : tem <<= LOG2_BITS_PER_UNIT;
16523 : 27090 : tem += *pbitpos;
16524 : 27090 : if (tem.to_shwi (pbitpos))
16525 : 26912 : return TREE_OPERAND (core, 0);
16526 : : }
16527 : 959024 : core = build_fold_addr_expr_loc (loc, core);
16528 : : }
16529 : 702724 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16530 : : {
16531 : 96226 : core = TREE_OPERAND (exp, 0);
16532 : 96226 : STRIP_NOPS (core);
16533 : 96226 : *pbitpos = 0;
16534 : 96226 : *poffset = TREE_OPERAND (exp, 1);
16535 : 96226 : if (poly_int_tree_p (*poffset))
16536 : : {
16537 : 96132 : poly_offset_int tem
16538 : 96132 : = wi::sext (wi::to_poly_offset (*poffset),
16539 : 96132 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16540 : 96132 : tem <<= LOG2_BITS_PER_UNIT;
16541 : 96132 : if (tem.to_shwi (pbitpos))
16542 : 96132 : *poffset = NULL_TREE;
16543 : : }
16544 : : }
16545 : : else
16546 : : {
16547 : 606498 : core = exp;
16548 : 606498 : *pbitpos = 0;
16549 : 606498 : *poffset = NULL_TREE;
16550 : : }
16551 : :
16552 : : return core;
16553 : : }
16554 : :
16555 : : /* Returns true if addresses of E1 and E2 differ by a constant, false
16556 : : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16557 : :
16558 : : bool
16559 : 844330 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16560 : : {
16561 : 844330 : tree core1, core2;
16562 : 844330 : poly_int64 bitpos1, bitpos2;
16563 : 844330 : tree toffset1, toffset2, tdiff, type;
16564 : :
16565 : 844330 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16566 : 844330 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16567 : :
16568 : 844330 : poly_int64 bytepos1, bytepos2;
16569 : 844330 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16570 : 1448726 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16571 : 1688660 : || !operand_equal_p (core1, core2, 0))
16572 : 604396 : return false;
16573 : :
16574 : 239934 : if (toffset1 && toffset2)
16575 : : {
16576 : 29 : type = TREE_TYPE (toffset1);
16577 : 29 : if (type != TREE_TYPE (toffset2))
16578 : 0 : toffset2 = fold_convert (type, toffset2);
16579 : :
16580 : 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16581 : 29 : if (!cst_and_fits_in_hwi (tdiff))
16582 : : return false;
16583 : :
16584 : 15 : *diff = int_cst_value (tdiff);
16585 : : }
16586 : 239905 : else if (toffset1 || toffset2)
16587 : : {
16588 : : /* If only one of the offsets is non-constant, the difference cannot
16589 : : be a constant. */
16590 : : return false;
16591 : : }
16592 : : else
16593 : 222152 : *diff = 0;
16594 : :
16595 : 222167 : *diff += bytepos1 - bytepos2;
16596 : 222167 : return true;
16597 : : }
16598 : :
16599 : : /* Return OFF converted to a pointer offset type suitable as offset for
16600 : : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16601 : : tree
16602 : 19327137 : convert_to_ptrofftype_loc (location_t loc, tree off)
16603 : : {
16604 : 19327137 : if (ptrofftype_p (TREE_TYPE (off)))
16605 : : return off;
16606 : 1982192 : return fold_convert_loc (loc, sizetype, off);
16607 : : }
16608 : :
16609 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16610 : : tree
16611 : 17460014 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16612 : : {
16613 : 17460014 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16614 : 17460014 : ptr, convert_to_ptrofftype_loc (loc, off));
16615 : : }
16616 : :
16617 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16618 : : tree
16619 : 163378 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16620 : : {
16621 : 163378 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16622 : 163378 : ptr, size_int (off));
16623 : : }
16624 : :
16625 : : /* Return a pointer to a NUL-terminated string containing the sequence
16626 : : of bytes corresponding to the representation of the object referred to
16627 : : by SRC (or a subsequence of such bytes within it if SRC is a reference
16628 : : to an initialized constant array plus some constant offset).
16629 : : Set *STRSIZE the number of bytes in the constant sequence including
16630 : : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16631 : : where A is the array that stores the constant sequence that SRC points
16632 : : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16633 : : need not point to a string or even an array of characters but may point
16634 : : to an object of any type. */
16635 : :
16636 : : const char *
16637 : 12297677 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16638 : : {
16639 : : /* The offset into the array A storing the string, and A's byte size. */
16640 : 12297677 : tree offset_node;
16641 : 12297677 : tree mem_size;
16642 : :
16643 : 12297677 : if (strsize)
16644 : 4533580 : *strsize = 0;
16645 : :
16646 : 12297677 : if (strsize)
16647 : 4533580 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16648 : : else
16649 : 7764097 : src = string_constant (src, &offset_node, &mem_size, NULL);
16650 : 12297677 : if (!src)
16651 : : return NULL;
16652 : :
16653 : 2703004 : unsigned HOST_WIDE_INT offset = 0;
16654 : 2703004 : if (offset_node != NULL_TREE)
16655 : : {
16656 : 2703004 : if (!tree_fits_uhwi_p (offset_node))
16657 : : return NULL;
16658 : : else
16659 : 2701240 : offset = tree_to_uhwi (offset_node);
16660 : : }
16661 : :
16662 : 2701240 : if (!tree_fits_uhwi_p (mem_size))
16663 : : return NULL;
16664 : :
16665 : : /* ARRAY_SIZE is the byte size of the array the constant sequence
16666 : : is stored in and equal to sizeof A. INIT_BYTES is the number
16667 : : of bytes in the constant sequence used to initialize the array,
16668 : : including any embedded NULs as well as the terminating NUL (for
16669 : : strings), but not including any trailing zeros/NULs past
16670 : : the terminating one appended implicitly to a string literal to
16671 : : zero out the remainder of the array it's stored in. For example,
16672 : : given:
16673 : : const char a[7] = "abc\0d";
16674 : : n = strlen (a + 1);
16675 : : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16676 : : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16677 : : is equal to strlen (A) + 1. */
16678 : 2701240 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16679 : 2701240 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16680 : 2701240 : const char *string = TREE_STRING_POINTER (src);
16681 : :
16682 : : /* Ideally this would turn into a gcc_checking_assert over time. */
16683 : 2701240 : if (init_bytes > array_size)
16684 : : init_bytes = array_size;
16685 : :
16686 : 2701240 : if (init_bytes == 0 || offset >= array_size)
16687 : : return NULL;
16688 : :
16689 : 2700052 : if (strsize)
16690 : : {
16691 : : /* Compute and store the number of characters from the beginning
16692 : : of the substring at OFFSET to the end, including the terminating
16693 : : nul. Offsets past the initial length refer to null strings. */
16694 : 1427738 : if (offset < init_bytes)
16695 : 1427738 : *strsize = init_bytes - offset;
16696 : : else
16697 : 0 : *strsize = 1;
16698 : : }
16699 : : else
16700 : : {
16701 : 1272314 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16702 : : /* Support only properly NUL-terminated single byte strings. */
16703 : 1272314 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16704 : : return NULL;
16705 : 1266671 : if (string[init_bytes - 1] != '\0')
16706 : : return NULL;
16707 : : }
16708 : :
16709 : 2672994 : return offset < init_bytes ? string + offset : "";
16710 : : }
16711 : :
16712 : : /* Return a pointer to a NUL-terminated string corresponding to
16713 : : the expression STR referencing a constant string, possibly
16714 : : involving a constant offset. Return null if STR either doesn't
16715 : : reference a constant string or if it involves a nonconstant
16716 : : offset. */
16717 : :
16718 : : const char *
16719 : 7764097 : c_getstr (tree str)
16720 : : {
16721 : 7764097 : return getbyterep (str, NULL);
16722 : : }
16723 : :
16724 : : /* Given a tree T, compute which bits in T may be nonzero. */
16725 : :
16726 : : wide_int
16727 : 236928990 : tree_nonzero_bits (const_tree t)
16728 : : {
16729 : 236928990 : switch (TREE_CODE (t))
16730 : : {
16731 : 8047807 : case INTEGER_CST:
16732 : 8047807 : return wi::to_wide (t);
16733 : 134060363 : case SSA_NAME:
16734 : 134060363 : return get_nonzero_bits (t);
16735 : 245066 : case NON_LVALUE_EXPR:
16736 : 245066 : case SAVE_EXPR:
16737 : 245066 : return tree_nonzero_bits (TREE_OPERAND (t, 0));
16738 : 506846 : case BIT_AND_EXPR:
16739 : 1013692 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16740 : 1520538 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16741 : 4325 : case BIT_IOR_EXPR:
16742 : 4325 : case BIT_XOR_EXPR:
16743 : 8650 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16744 : 12975 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16745 : 63780 : case COND_EXPR:
16746 : 127560 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16747 : 191340 : tree_nonzero_bits (TREE_OPERAND (t, 2)));
16748 : 49742200 : CASE_CONVERT:
16749 : 99484400 : return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16750 : 49742200 : TYPE_PRECISION (TREE_TYPE (t)),
16751 : 149226600 : TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16752 : 13415429 : case PLUS_EXPR:
16753 : 13415429 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16754 : : {
16755 : 13415429 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16756 : 13415429 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16757 : 13415429 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16758 : 497082 : return wi::bit_or (nzbits1, nzbits2);
16759 : 13415429 : }
16760 : : break;
16761 : 165500 : case LSHIFT_EXPR:
16762 : 165500 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16763 : : {
16764 : 93754 : tree type = TREE_TYPE (t);
16765 : 93754 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16766 : 187508 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16767 : 93754 : TYPE_PRECISION (type));
16768 : 93754 : return wi::neg_p (arg1)
16769 : 187508 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16770 : 93754 : : wi::lshift (nzbits, arg1);
16771 : 93754 : }
16772 : : break;
16773 : 154821 : case RSHIFT_EXPR:
16774 : 154821 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16775 : : {
16776 : 153263 : tree type = TREE_TYPE (t);
16777 : 153263 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16778 : 306526 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16779 : 153263 : TYPE_PRECISION (type));
16780 : 153263 : return wi::neg_p (arg1)
16781 : 306526 : ? wi::lshift (nzbits, -arg1)
16782 : 153263 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16783 : 153263 : }
16784 : : break;
16785 : : default:
16786 : : break;
16787 : : }
16788 : :
16789 : 43514504 : return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16790 : : }
16791 : :
16792 : : /* Helper function for address compare simplifications in match.pd.
16793 : : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16794 : : TYPE is the type of comparison operands.
16795 : : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16796 : : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16797 : : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16798 : : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16799 : : and 2 if unknown. */
16800 : :
16801 : : int
16802 : 1360774 : address_compare (tree_code code, tree type, tree op0, tree op1,
16803 : : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16804 : : bool generic)
16805 : : {
16806 : 1360774 : if (TREE_CODE (op0) == SSA_NAME)
16807 : 19992 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16808 : 1360774 : if (TREE_CODE (op1) == SSA_NAME)
16809 : 3922 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16810 : 1360774 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16811 : 1360774 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16812 : 1360774 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16813 : 1360774 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16814 : 1360774 : if (base0 && TREE_CODE (base0) == MEM_REF)
16815 : : {
16816 : 18821 : off0 += mem_ref_offset (base0).force_shwi ();
16817 : 18821 : base0 = TREE_OPERAND (base0, 0);
16818 : : }
16819 : 1360774 : if (base1 && TREE_CODE (base1) == MEM_REF)
16820 : : {
16821 : 2462 : off1 += mem_ref_offset (base1).force_shwi ();
16822 : 2462 : base1 = TREE_OPERAND (base1, 0);
16823 : : }
16824 : 1360774 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16825 : : return 2;
16826 : :
16827 : 1352613 : int equal = 2;
16828 : : /* Punt in GENERIC on variables with value expressions;
16829 : : the value expressions might point to fields/elements
16830 : : of other vars etc. */
16831 : 1352613 : if (generic
16832 : 1352613 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16833 : 1220887 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16834 : : return 2;
16835 : 1352055 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16836 : : {
16837 : 105491 : symtab_node *node0 = symtab_node::get_create (base0);
16838 : 105491 : symtab_node *node1 = symtab_node::get_create (base1);
16839 : 105491 : equal = node0->equal_address_to (node1);
16840 : : }
16841 : 1246564 : else if ((DECL_P (base0)
16842 : 222740 : || TREE_CODE (base0) == SSA_NAME
16843 : 204786 : || TREE_CODE (base0) == STRING_CST)
16844 : 1246402 : && (DECL_P (base1)
16845 : 207269 : || TREE_CODE (base1) == SSA_NAME
16846 : 205037 : || TREE_CODE (base1) == STRING_CST))
16847 : 1246380 : equal = (base0 == base1);
16848 : : /* Assume different STRING_CSTs with the same content will be
16849 : : merged. */
16850 : 1351871 : if (equal == 0
16851 : 52764 : && TREE_CODE (base0) == STRING_CST
16852 : 17617 : && TREE_CODE (base1) == STRING_CST
16853 : 17564 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16854 : 1351871 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16855 : 6546 : TREE_STRING_LENGTH (base0)) == 0)
16856 : : equal = 1;
16857 : 1347172 : if (equal == 1)
16858 : : {
16859 : 1279460 : if (code == EQ_EXPR
16860 : 1279460 : || code == NE_EXPR
16861 : : /* If the offsets are equal we can ignore overflow. */
16862 : 128787 : || known_eq (off0, off1)
16863 : 257352 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16864 : : /* Or if we compare using pointers to decls or strings. */
16865 : 1408136 : || (POINTER_TYPE_P (type)
16866 : 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16867 : : return 1;
16868 : : return 2;
16869 : : }
16870 : 72595 : if (equal != 0)
16871 : : return equal;
16872 : 47881 : if (code != EQ_EXPR && code != NE_EXPR)
16873 : : return 2;
16874 : :
16875 : : /* At this point we know (or assume) the two pointers point at
16876 : : different objects. */
16877 : 43708 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16878 : 43708 : off0.is_constant (&ioff0);
16879 : 43708 : off1.is_constant (&ioff1);
16880 : : /* Punt on non-zero offsets from functions. */
16881 : 43708 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16882 : 43708 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16883 : : return 2;
16884 : : /* Or if the bases are neither decls nor string literals. */
16885 : 43708 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16886 : : return 2;
16887 : 26236 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16888 : : return 2;
16889 : : /* For initializers, assume addresses of different functions are
16890 : : different. */
16891 : 26236 : if (folding_initializer
16892 : 4289 : && TREE_CODE (base0) == FUNCTION_DECL
16893 : 14 : && TREE_CODE (base1) == FUNCTION_DECL)
16894 : : return 0;
16895 : :
16896 : : /* Compute whether one address points to the start of one
16897 : : object and another one to the end of another one. */
16898 : 26222 : poly_int64 size0 = 0, size1 = 0;
16899 : 26222 : if (TREE_CODE (base0) == STRING_CST)
16900 : : {
16901 : 12686 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16902 : : equal = 2;
16903 : : else
16904 : : size0 = TREE_STRING_LENGTH (base0);
16905 : : }
16906 : 13536 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16907 : : size0 = 1;
16908 : : else
16909 : : {
16910 : 13374 : tree sz0 = DECL_SIZE_UNIT (base0);
16911 : 13374 : if (!tree_fits_poly_int64_p (sz0))
16912 : : equal = 2;
16913 : : else
16914 : 13374 : size0 = tree_to_poly_int64 (sz0);
16915 : : }
16916 : 26222 : if (TREE_CODE (base1) == STRING_CST)
16917 : : {
16918 : 12807 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16919 : : equal = 2;
16920 : : else
16921 : : size1 = TREE_STRING_LENGTH (base1);
16922 : : }
16923 : 13415 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16924 : : size1 = 1;
16925 : : else
16926 : : {
16927 : 13257 : tree sz1 = DECL_SIZE_UNIT (base1);
16928 : 13257 : if (!tree_fits_poly_int64_p (sz1))
16929 : : equal = 2;
16930 : : else
16931 : 13257 : size1 = tree_to_poly_int64 (sz1);
16932 : : }
16933 : 26222 : if (equal == 0)
16934 : : {
16935 : : /* If one offset is pointing (or could be) to the beginning of one
16936 : : object and the other is pointing to one past the last byte of the
16937 : : other object, punt. */
16938 : 26210 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16939 : : equal = 2;
16940 : 26073 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16941 : : equal = 2;
16942 : : /* If both offsets are the same, there are some cases we know that are
16943 : : ok. Either if we know they aren't zero, or if we know both sizes
16944 : : are no zero. */
16945 : : if (equal == 2
16946 : 273 : && known_eq (off0, off1)
16947 : 22 : && (known_ne (off0, 0)
16948 : 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16949 : : equal = 0;
16950 : : }
16951 : :
16952 : : /* At this point, equal is 2 if either one or both pointers are out of
16953 : : bounds of their object, or one points to start of its object and the
16954 : : other points to end of its object. This is unspecified behavior
16955 : : e.g. in C++. Otherwise equal is 0. */
16956 : 26222 : if (folding_cxx_constexpr && equal)
16957 : : return equal;
16958 : :
16959 : : /* When both pointers point to string literals, even when equal is 0,
16960 : : due to tail merging of string literals the pointers might be the same. */
16961 : 26159 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16962 : : {
16963 : 12663 : if (ioff0 < 0
16964 : 12663 : || ioff1 < 0
16965 : 12663 : || ioff0 > TREE_STRING_LENGTH (base0)
16966 : 25314 : || ioff1 > TREE_STRING_LENGTH (base1))
16967 : : return 2;
16968 : :
16969 : : /* If the bytes in the string literals starting at the pointers
16970 : : differ, the pointers need to be different. */
16971 : 12651 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16972 : 12651 : TREE_STRING_POINTER (base1) + ioff1,
16973 : 12651 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16974 : : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16975 : : {
16976 : 3897 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16977 : 3897 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16978 : 3897 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16979 : : ioffmin) == 0)
16980 : : /* If even the bytes in the string literal before the
16981 : : pointers are the same, the string literals could be
16982 : : tail merged. */
16983 : : return 2;
16984 : : }
16985 : : return 0;
16986 : : }
16987 : :
16988 : 13496 : if (folding_cxx_constexpr)
16989 : : return 0;
16990 : :
16991 : : /* If this is a pointer comparison, ignore for now even
16992 : : valid equalities where one pointer is the offset zero
16993 : : of one object and the other to one past end of another one. */
16994 : 9327 : if (!INTEGRAL_TYPE_P (type))
16995 : : return 0;
16996 : :
16997 : : /* Assume that string literals can't be adjacent to variables
16998 : : (automatic or global). */
16999 : 299 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
17000 : : return 0;
17001 : :
17002 : : /* Assume that automatic variables can't be adjacent to global
17003 : : variables. */
17004 : 295 : if (is_global_var (base0) != is_global_var (base1))
17005 : : return 0;
17006 : :
17007 : : return equal;
17008 : : }
17009 : :
17010 : : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
17011 : : tree
17012 : 52 : ctor_single_nonzero_element (const_tree t)
17013 : : {
17014 : 52 : unsigned HOST_WIDE_INT idx;
17015 : 52 : constructor_elt *ce;
17016 : 52 : tree elt = NULL_TREE;
17017 : :
17018 : 52 : if (TREE_CODE (t) != CONSTRUCTOR)
17019 : : return NULL_TREE;
17020 : 113 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
17021 : 110 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
17022 : : {
17023 : 101 : if (elt)
17024 : : return NULL_TREE;
17025 : 52 : elt = ce->value;
17026 : : }
17027 : : return elt;
17028 : : }
17029 : :
17030 : : #if CHECKING_P
17031 : :
17032 : : namespace selftest {
17033 : :
17034 : : /* Helper functions for writing tests of folding trees. */
17035 : :
17036 : : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17037 : :
17038 : : static void
17039 : 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
17040 : : tree constant)
17041 : : {
17042 : 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
17043 : 16 : }
17044 : :
17045 : : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17046 : : wrapping WRAPPED_EXPR. */
17047 : :
17048 : : static void
17049 : 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
17050 : : tree wrapped_expr)
17051 : : {
17052 : 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
17053 : 12 : ASSERT_NE (wrapped_expr, result);
17054 : 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
17055 : 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
17056 : 12 : }
17057 : :
17058 : : /* Verify that various arithmetic binary operations are folded
17059 : : correctly. */
17060 : :
17061 : : static void
17062 : 4 : test_arithmetic_folding ()
17063 : : {
17064 : 4 : tree type = integer_type_node;
17065 : 4 : tree x = create_tmp_var_raw (type, "x");
17066 : 4 : tree zero = build_zero_cst (type);
17067 : 4 : tree one = build_int_cst (type, 1);
17068 : :
17069 : : /* Addition. */
17070 : : /* 1 <-- (0 + 1) */
17071 : 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
17072 : : one);
17073 : 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
17074 : : one);
17075 : :
17076 : : /* (nonlvalue)x <-- (x + 0) */
17077 : 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
17078 : : x);
17079 : :
17080 : : /* Subtraction. */
17081 : : /* 0 <-- (x - x) */
17082 : 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
17083 : : zero);
17084 : 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
17085 : : x);
17086 : :
17087 : : /* Multiplication. */
17088 : : /* 0 <-- (x * 0) */
17089 : 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
17090 : : zero);
17091 : :
17092 : : /* (nonlvalue)x <-- (x * 1) */
17093 : 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
17094 : : x);
17095 : 4 : }
17096 : :
17097 : : namespace test_operand_equality {
17098 : :
17099 : : /* Verify structural equality. */
17100 : :
17101 : : /* Execute fold_vec_perm_cst unit tests. */
17102 : :
17103 : : static void
17104 : 4 : test ()
17105 : : {
17106 : 4 : tree stype = integer_type_node;
17107 : 4 : tree utype = unsigned_type_node;
17108 : 4 : tree x = create_tmp_var_raw (stype, "x");
17109 : 4 : tree y = create_tmp_var_raw (stype, "y");
17110 : 4 : tree z = create_tmp_var_raw (stype, "z");
17111 : 4 : tree four = build_int_cst (stype, 4);
17112 : 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
17113 : 4 : tree rhs1 = fold_convert (stype,
17114 : : fold_build2 (PLUS_EXPR, utype,
17115 : : fold_convert (utype, x),
17116 : : fold_convert (utype, y)));
17117 : :
17118 : : /* (int)((unsigned x) + (unsigned y)) == x + y. */
17119 : 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17120 : 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17121 : :
17122 : : /* (int)(unsigned) x == x. */
17123 : 4 : tree lhs2 = build1 (NOP_EXPR, stype,
17124 : : build1 (NOP_EXPR, utype, x));
17125 : 4 : tree rhs2 = x;
17126 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17127 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17128 : :
17129 : : /* (unsigned x) + (unsigned y) == x + y. */
17130 : 4 : tree lhs3 = lhs1;
17131 : 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17132 : : fold_convert (utype, x),
17133 : : fold_convert (utype, y));
17134 : 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17135 : 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17136 : :
17137 : : /* (unsigned x) / (unsigned y) == x / y. */
17138 : 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17139 : 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17140 : : fold_convert (utype, x),
17141 : : fold_convert (utype, y));
17142 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17143 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17144 : :
17145 : : /* (long x) / 4 == (long)(x / 4). */
17146 : 4 : tree lstype = long_long_integer_type_node;
17147 : 4 : tree lfour = build_int_cst (lstype, 4);
17148 : 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17149 : : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17150 : 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17151 : : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17152 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17153 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17154 : :
17155 : : /* (unsigned x) / 4 == x / 4. */
17156 : 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17157 : 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17158 : : fold_convert (utype, x),
17159 : : fold_convert (utype, four));
17160 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17161 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17162 : :
17163 : : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17164 : 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17165 : 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17166 : 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17167 : 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17168 : :
17169 : : /* (unsigned x) + 4 == x + 4. */
17170 : 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17171 : 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17172 : : fold_convert (utype, x),
17173 : : fold_convert (utype, four));
17174 : 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17175 : 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17176 : :
17177 : : /* (unsigned x) + 4 == 4 + x. */
17178 : 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17179 : 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17180 : : fold_convert (utype, x),
17181 : : fold_convert (utype, four));
17182 : 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17183 : 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17184 : :
17185 : : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17186 : 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17187 : : fold_build2 (MULT_EXPR, stype,
17188 : : fold_build2 (PLUS_EXPR, stype, four, x),
17189 : : y),
17190 : : z);
17191 : 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17192 : : fold_build2 (PLUS_EXPR, utype,
17193 : : fold_convert (utype, x),
17194 : : fold_convert (utype, four)),
17195 : : fold_convert (utype, y));
17196 : 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17197 : 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17198 : 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17199 : 4 : }
17200 : : }
17201 : :
17202 : : namespace test_fold_vec_perm_cst {
17203 : :
17204 : : /* Build a VECTOR_CST corresponding to VMODE, and has
17205 : : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17206 : : Fill it with randomized elements, using rand() % THRESHOLD. */
17207 : :
17208 : : static tree
17209 : 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17210 : : unsigned nelts_per_pattern,
17211 : : int step = 0, bool natural_stepped = false,
17212 : : int threshold = 100)
17213 : : {
17214 : 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17215 : 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17216 : 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17217 : :
17218 : : // Fill a0 for each pattern
17219 : 0 : for (unsigned i = 0; i < npatterns; i++)
17220 : 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17221 : :
17222 : 0 : if (nelts_per_pattern == 1)
17223 : 0 : return builder.build ();
17224 : :
17225 : : // Fill a1 for each pattern
17226 : 0 : for (unsigned i = 0; i < npatterns; i++)
17227 : : {
17228 : 0 : tree a1;
17229 : 0 : if (natural_stepped)
17230 : : {
17231 : 0 : tree a0 = builder[i];
17232 : 0 : wide_int a0_val = wi::to_wide (a0);
17233 : 0 : wide_int a1_val = a0_val + step;
17234 : 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17235 : 0 : }
17236 : : else
17237 : 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17238 : 0 : builder.quick_push (a1);
17239 : : }
17240 : 0 : if (nelts_per_pattern == 2)
17241 : 0 : return builder.build ();
17242 : :
17243 : 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17244 : : {
17245 : 0 : tree prev_elem = builder[i - npatterns];
17246 : 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17247 : 0 : wide_int val = prev_elem_val + step;
17248 : 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17249 : 0 : }
17250 : :
17251 : 0 : return builder.build ();
17252 : 0 : }
17253 : :
17254 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17255 : : when result is VLA. */
17256 : :
17257 : : static void
17258 : 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17259 : : tree res, tree *expected_res)
17260 : : {
17261 : : /* Actual npatterns and encoded_elts in res may be less than expected due
17262 : : to canonicalization. */
17263 : 0 : ASSERT_TRUE (res != NULL_TREE);
17264 : 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17265 : 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17266 : :
17267 : 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17268 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17269 : 0 : }
17270 : :
17271 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17272 : : when the result is VLS. */
17273 : :
17274 : : static void
17275 : 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17276 : : {
17277 : 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17278 : 0 : for (unsigned i = 0; i < expected_nelts; i++)
17279 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17280 : 0 : }
17281 : :
17282 : : /* Helper routine to push multiple elements into BUILDER. */
17283 : : template<unsigned N>
17284 : 0 : static void builder_push_elems (vec_perm_builder& builder,
17285 : : poly_uint64 (&elems)[N])
17286 : : {
17287 : 0 : for (unsigned i = 0; i < N; i++)
17288 : 0 : builder.quick_push (elems[i]);
17289 : 0 : }
17290 : :
17291 : : #define ARG0(index) vector_cst_elt (arg0, index)
17292 : : #define ARG1(index) vector_cst_elt (arg1, index)
17293 : :
17294 : : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17295 : :
17296 : : static void
17297 : 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17298 : : {
17299 : 0 : for (int i = 0; i < 10; i++)
17300 : : {
17301 : : /* Case 1:
17302 : : sel = { 0, 4, 1, 5, ... }
17303 : : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17304 : 0 : {
17305 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17306 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17307 : :
17308 : 0 : tree inner_type
17309 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17310 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17311 : :
17312 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17313 : 0 : vec_perm_builder builder (res_len, 4, 1);
17314 : 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17315 : 0 : builder_push_elems (builder, mask_elems);
17316 : :
17317 : 0 : vec_perm_indices sel (builder, 2, res_len);
17318 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17319 : :
17320 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17321 : 0 : validate_res (4, 1, res, expected_res);
17322 : 0 : }
17323 : :
17324 : : /* Case 2: Same as case 1, but contains an out of bounds access which
17325 : : should wrap around.
17326 : : sel = {0, 8, 4, 12, ...} (4, 1)
17327 : : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17328 : 0 : {
17329 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17330 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17331 : :
17332 : 0 : tree inner_type
17333 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17334 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17335 : :
17336 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17337 : 0 : vec_perm_builder builder (res_len, 4, 1);
17338 : 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17339 : 0 : builder_push_elems (builder, mask_elems);
17340 : :
17341 : 0 : vec_perm_indices sel (builder, 2, res_len);
17342 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17343 : :
17344 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17345 : 0 : validate_res (4, 1, res, expected_res);
17346 : 0 : }
17347 : : }
17348 : 0 : }
17349 : :
17350 : : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17351 : :
17352 : : static void
17353 : 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17354 : : {
17355 : 0 : for (int i = 0; i < 10; i++)
17356 : : {
17357 : : /* Case 1:
17358 : : sel = { 0, 1, 2, 3}
17359 : : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17360 : 0 : {
17361 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17362 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17363 : :
17364 : 0 : tree inner_type
17365 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17366 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17367 : :
17368 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17369 : 0 : vec_perm_builder builder (res_len, 4, 1);
17370 : 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17371 : 0 : builder_push_elems (builder, mask_elems);
17372 : :
17373 : 0 : vec_perm_indices sel (builder, 2, res_len);
17374 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17375 : :
17376 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17377 : 0 : validate_res_vls (res, expected_res, 4);
17378 : 0 : }
17379 : :
17380 : : /* Case 2: Same as Case 1, but crossing input vector.
17381 : : sel = {0, 2, 4, 6}
17382 : : In this case,the index 4 is ambiguous since len = 4 + 4x.
17383 : : Since we cannot determine, which vector to choose from during
17384 : : compile time, should return NULL_TREE. */
17385 : 0 : {
17386 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17387 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17388 : :
17389 : 0 : tree inner_type
17390 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17391 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17392 : :
17393 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17394 : 0 : vec_perm_builder builder (res_len, 4, 1);
17395 : 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17396 : 0 : builder_push_elems (builder, mask_elems);
17397 : :
17398 : 0 : vec_perm_indices sel (builder, 2, res_len);
17399 : 0 : const char *reason;
17400 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17401 : :
17402 : 0 : ASSERT_TRUE (res == NULL_TREE);
17403 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17404 : 0 : }
17405 : : }
17406 : 0 : }
17407 : :
17408 : : /* Test all input vectors. */
17409 : :
17410 : : static void
17411 : 0 : test_all_nunits (machine_mode vmode)
17412 : : {
17413 : : /* Test with 10 different inputs. */
17414 : 0 : for (int i = 0; i < 10; i++)
17415 : : {
17416 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17417 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17418 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17419 : :
17420 : : /* Case 1: mask = {0, ...} // (1, 1)
17421 : : res = { arg0[0], ... } // (1, 1) */
17422 : 0 : {
17423 : 0 : vec_perm_builder builder (len, 1, 1);
17424 : 0 : builder.quick_push (0);
17425 : 0 : vec_perm_indices sel (builder, 2, len);
17426 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17427 : 0 : tree expected_res[] = { ARG0(0) };
17428 : 0 : validate_res (1, 1, res, expected_res);
17429 : 0 : }
17430 : :
17431 : : /* Case 2: mask = {len, ...} // (1, 1)
17432 : : res = { arg1[0], ... } // (1, 1) */
17433 : 0 : {
17434 : 0 : vec_perm_builder builder (len, 1, 1);
17435 : 0 : builder.quick_push (len);
17436 : 0 : vec_perm_indices sel (builder, 2, len);
17437 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17438 : :
17439 : 0 : tree expected_res[] = { ARG1(0) };
17440 : 0 : validate_res (1, 1, res, expected_res);
17441 : 0 : }
17442 : : }
17443 : 0 : }
17444 : :
17445 : : /* Test all vectors which contain at-least 2 elements. */
17446 : :
17447 : : static void
17448 : 0 : test_nunits_min_2 (machine_mode vmode)
17449 : : {
17450 : 0 : for (int i = 0; i < 10; i++)
17451 : : {
17452 : : /* Case 1: mask = { 0, len, ... } // (2, 1)
17453 : : res = { arg0[0], arg1[0], ... } // (2, 1) */
17454 : 0 : {
17455 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17456 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17457 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17458 : :
17459 : 0 : vec_perm_builder builder (len, 2, 1);
17460 : 0 : poly_uint64 mask_elems[] = { 0, len };
17461 : 0 : builder_push_elems (builder, mask_elems);
17462 : :
17463 : 0 : vec_perm_indices sel (builder, 2, len);
17464 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17465 : :
17466 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17467 : 0 : validate_res (2, 1, res, expected_res);
17468 : 0 : }
17469 : :
17470 : : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17471 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17472 : 0 : {
17473 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17474 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17475 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17476 : :
17477 : 0 : vec_perm_builder builder (len, 2, 2);
17478 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17479 : 0 : builder_push_elems (builder, mask_elems);
17480 : :
17481 : 0 : vec_perm_indices sel (builder, 2, len);
17482 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17483 : :
17484 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17485 : 0 : validate_res (2, 2, res, expected_res);
17486 : 0 : }
17487 : :
17488 : : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17489 : : Test that the stepped sequence of the pattern selects from
17490 : : same input pattern. Since input vectors have npatterns = 2,
17491 : : and step (a2 - a1) = 1, step is not a multiple of npatterns
17492 : : in input vector. So return NULL_TREE. */
17493 : 0 : {
17494 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17495 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17496 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17497 : :
17498 : 0 : vec_perm_builder builder (len, 1, 3);
17499 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17500 : 0 : builder_push_elems (builder, mask_elems);
17501 : :
17502 : 0 : vec_perm_indices sel (builder, 2, len);
17503 : 0 : const char *reason;
17504 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17505 : : &reason);
17506 : 0 : ASSERT_TRUE (res == NULL_TREE);
17507 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17508 : 0 : }
17509 : :
17510 : : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17511 : : Test that stepped sequence of the pattern selects from arg0.
17512 : : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17513 : 0 : {
17514 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17515 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17516 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17517 : :
17518 : 0 : vec_perm_builder builder (len, 1, 3);
17519 : 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17520 : 0 : builder_push_elems (builder, mask_elems);
17521 : :
17522 : 0 : vec_perm_indices sel (builder, 2, len);
17523 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17524 : :
17525 : 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17526 : 0 : validate_res (1, 3, res, expected_res);
17527 : 0 : }
17528 : :
17529 : : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17530 : : In this case ensure that arg has a natural stepped sequence
17531 : : to preserve arg's encoding.
17532 : :
17533 : : As a concrete example, consider:
17534 : : arg0: { -16, -9, -10, ... } // (1, 3)
17535 : : arg1: { -12, -5, -6, ... } // (1, 3)
17536 : : sel = { 0, len, len + 1, ... } // (1, 3)
17537 : :
17538 : : This will create res with following encoding:
17539 : : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17540 : : = { -16, -12, -5, ... }
17541 : :
17542 : : The step in above encoding would be: (-5) - (-12) = 7
17543 : : And hence res[3] would be computed as -5 + 7 = 2.
17544 : : instead of arg1[2], ie, -6.
17545 : : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17546 : : for this case. */
17547 : 0 : {
17548 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17549 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17550 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17551 : :
17552 : 0 : vec_perm_builder builder (len, 1, 3);
17553 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17554 : 0 : builder_push_elems (builder, mask_elems);
17555 : :
17556 : 0 : vec_perm_indices sel (builder, 2, len);
17557 : 0 : const char *reason;
17558 : : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17559 : : stepped pattern, even if we didn't explicitly tell it to. So folding
17560 : : may not always fail, but if it does, ensure that's because arg1 does
17561 : : not have a natural stepped sequence (and not due to other reason) */
17562 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17563 : 0 : if (res == NULL_TREE)
17564 : 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17565 : 0 : }
17566 : :
17567 : : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17568 : : sequence and thus folding should be valid for this case. */
17569 : 0 : {
17570 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17571 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17572 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17573 : :
17574 : 0 : vec_perm_builder builder (len, 1, 3);
17575 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17576 : 0 : builder_push_elems (builder, mask_elems);
17577 : :
17578 : 0 : vec_perm_indices sel (builder, 2, len);
17579 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17580 : :
17581 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17582 : 0 : validate_res (1, 3, res, expected_res);
17583 : 0 : }
17584 : :
17585 : : /* Case 8: Same as aarch64/sve/slp_3.c:
17586 : : arg0, arg1 are dup vectors.
17587 : : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17588 : : So res = { arg0[0], arg1[0], ... } // (2, 1)
17589 : :
17590 : : In this case, since the input vectors are dup, only the first two
17591 : : elements per pattern in sel are considered significant. */
17592 : 0 : {
17593 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17594 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17595 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17596 : :
17597 : 0 : vec_perm_builder builder (len, 2, 3);
17598 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17599 : 0 : builder_push_elems (builder, mask_elems);
17600 : :
17601 : 0 : vec_perm_indices sel (builder, 2, len);
17602 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17603 : :
17604 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17605 : 0 : validate_res (2, 1, res, expected_res);
17606 : 0 : }
17607 : : }
17608 : 0 : }
17609 : :
17610 : : /* Test all vectors which contain at-least 4 elements. */
17611 : :
17612 : : static void
17613 : 0 : test_nunits_min_4 (machine_mode vmode)
17614 : : {
17615 : 0 : for (int i = 0; i < 10; i++)
17616 : : {
17617 : : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17618 : : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17619 : 0 : {
17620 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17621 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17622 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17623 : :
17624 : 0 : vec_perm_builder builder (len, 4, 1);
17625 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17626 : 0 : builder_push_elems (builder, mask_elems);
17627 : :
17628 : 0 : vec_perm_indices sel (builder, 2, len);
17629 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17630 : :
17631 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17632 : 0 : validate_res (4, 1, res, expected_res);
17633 : 0 : }
17634 : :
17635 : : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17636 : : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17637 : 0 : {
17638 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17639 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17640 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17641 : :
17642 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17643 : 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17644 : 0 : builder_push_elems (builder, mask_elems);
17645 : :
17646 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17647 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17648 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17649 : 0 : validate_res (1, 3, res, expected_res);
17650 : 0 : }
17651 : :
17652 : : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17653 : : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17654 : 0 : {
17655 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17656 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17657 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17658 : :
17659 : 0 : vec_perm_builder builder (len, 1, 3);
17660 : 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17661 : 0 : builder_push_elems (builder, mask_elems);
17662 : :
17663 : 0 : vec_perm_indices sel (builder, 2, len);
17664 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17665 : 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17666 : 0 : validate_res (1, 3, res, expected_res);
17667 : 0 : }
17668 : :
17669 : : /* Case 4:
17670 : : sel = { len, 0, 2, ... } // (1, 3)
17671 : : This should return NULL because we cross the input vectors.
17672 : : Because,
17673 : : Let's assume len = C + Cx
17674 : : a1 = 0
17675 : : S = 2
17676 : : esel = arg0_len / sel_npatterns = C + Cx
17677 : : ae = 0 + (esel - 2) * S
17678 : : = 0 + (C + Cx - 2) * 2
17679 : : = 2(C-2) + 2Cx
17680 : :
17681 : : For C >= 4:
17682 : : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17683 : : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17684 : : Since q1 != qe, we cross input vectors.
17685 : : So return NULL_TREE. */
17686 : 0 : {
17687 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17688 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17689 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17690 : :
17691 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17692 : 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17693 : 0 : builder_push_elems (builder, mask_elems);
17694 : :
17695 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17696 : 0 : const char *reason;
17697 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17698 : 0 : ASSERT_TRUE (res == NULL_TREE);
17699 : 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17700 : 0 : }
17701 : :
17702 : : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17703 : : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17704 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17705 : :
17706 : : Note that fold_vec_perm_cst will set
17707 : : res_npatterns = max(4, max(4, 2)) = 4
17708 : : However after canonicalizing, we will end up with shape (2, 2). */
17709 : 0 : {
17710 : 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17711 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17712 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17713 : :
17714 : 0 : vec_perm_builder builder (len, 2, 2);
17715 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17716 : 0 : builder_push_elems (builder, mask_elems);
17717 : :
17718 : 0 : vec_perm_indices sel (builder, 2, len);
17719 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17720 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17721 : 0 : validate_res (2, 2, res, expected_res);
17722 : 0 : }
17723 : :
17724 : : /* Case 6: Test combination in sel, where one pattern is dup and other
17725 : : is stepped sequence.
17726 : : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17727 : : res = { arg0[0], arg0[0], arg0[0],
17728 : : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17729 : 0 : {
17730 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17731 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17732 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17733 : :
17734 : 0 : vec_perm_builder builder (len, 2, 3);
17735 : 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17736 : 0 : builder_push_elems (builder, mask_elems);
17737 : :
17738 : 0 : vec_perm_indices sel (builder, 2, len);
17739 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17740 : :
17741 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17742 : 0 : ARG0(1), ARG0(0), ARG0(2) };
17743 : 0 : validate_res (2, 3, res, expected_res);
17744 : 0 : }
17745 : :
17746 : : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17747 : : when arg0, arg1 and sel have different number of patterns.
17748 : : arg0 is of shape (1, 1)
17749 : : arg1 is of shape (4, 1)
17750 : : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17751 : :
17752 : : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17753 : : However,
17754 : : step = (len+2) - (len+1) = 1
17755 : : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17756 : : Since step is not a multiple of arg_npatterns,
17757 : : valid_mask_for_fold_vec_perm_cst should return false,
17758 : : and thus fold_vec_perm_cst should return NULL_TREE. */
17759 : 0 : {
17760 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17761 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17762 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17763 : :
17764 : 0 : vec_perm_builder builder (len, 2, 3);
17765 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17766 : 0 : builder_push_elems (builder, mask_elems);
17767 : :
17768 : 0 : vec_perm_indices sel (builder, 2, len);
17769 : 0 : const char *reason;
17770 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17771 : :
17772 : 0 : ASSERT_TRUE (res == NULL_TREE);
17773 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17774 : 0 : }
17775 : :
17776 : : /* Case 8: PR111754: When input vector is not a stepped sequence,
17777 : : check that the result is not a stepped sequence either, even
17778 : : if sel has a stepped sequence. */
17779 : 0 : {
17780 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17781 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17782 : :
17783 : 0 : vec_perm_builder builder (len, 1, 3);
17784 : 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17785 : 0 : builder_push_elems (builder, mask_elems);
17786 : :
17787 : 0 : vec_perm_indices sel (builder, 1, len);
17788 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17789 : :
17790 : 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17791 : 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17792 : 0 : }
17793 : :
17794 : : /* Case 9: If sel doesn't contain a stepped sequence,
17795 : : check that the result has same encoding as sel, irrespective
17796 : : of shape of input vectors. */
17797 : 0 : {
17798 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17799 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17800 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17801 : :
17802 : 0 : vec_perm_builder builder (len, 1, 2);
17803 : 0 : poly_uint64 mask_elems[] = { 0, len };
17804 : 0 : builder_push_elems (builder, mask_elems);
17805 : :
17806 : 0 : vec_perm_indices sel (builder, 2, len);
17807 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17808 : :
17809 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17810 : 0 : validate_res (sel.encoding ().npatterns (),
17811 : 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17812 : 0 : }
17813 : : }
17814 : 0 : }
17815 : :
17816 : : /* Test all vectors which contain at-least 8 elements. */
17817 : :
17818 : : static void
17819 : 0 : test_nunits_min_8 (machine_mode vmode)
17820 : : {
17821 : 0 : for (int i = 0; i < 10; i++)
17822 : : {
17823 : : /* Case 1: sel_npatterns (4) > input npatterns (2)
17824 : : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17825 : : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17826 : : arg0[2], arg0[0], arg0[3], arg1[0],
17827 : : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17828 : 0 : {
17829 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17830 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17831 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17832 : :
17833 : 0 : vec_perm_builder builder(len, 4, 3);
17834 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17835 : 0 : 4, 0, 5, len };
17836 : 0 : builder_push_elems (builder, mask_elems);
17837 : :
17838 : 0 : vec_perm_indices sel (builder, 2, len);
17839 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17840 : :
17841 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17842 : 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17843 : 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17844 : 0 : validate_res (4, 3, res, expected_res);
17845 : 0 : }
17846 : : }
17847 : 0 : }
17848 : :
17849 : : /* Test vectors for which nunits[0] <= 4. */
17850 : :
17851 : : static void
17852 : 0 : test_nunits_max_4 (machine_mode vmode)
17853 : : {
17854 : : /* Case 1: mask = {0, 4, ...} // (1, 2)
17855 : : This should return NULL_TREE because the index 4 may choose
17856 : : from either arg0 or arg1 depending on vector length. */
17857 : 0 : {
17858 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17859 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17860 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17861 : :
17862 : 0 : vec_perm_builder builder (len, 1, 2);
17863 : 0 : poly_uint64 mask_elems[] = {0, 4};
17864 : 0 : builder_push_elems (builder, mask_elems);
17865 : :
17866 : 0 : vec_perm_indices sel (builder, 2, len);
17867 : 0 : const char *reason;
17868 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17869 : 0 : ASSERT_TRUE (res == NULL_TREE);
17870 : 0 : ASSERT_TRUE (reason != NULL);
17871 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17872 : 0 : }
17873 : 0 : }
17874 : :
17875 : : #undef ARG0
17876 : : #undef ARG1
17877 : :
17878 : : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17879 : :
17880 : : static bool
17881 : 0 : is_simple_vla_size (poly_uint64 size)
17882 : : {
17883 : 124 : if (size.is_constant ()
17884 : : || !pow2p_hwi (size.coeffs[0]))
17885 : 0 : return false;
17886 : : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17887 : : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17888 : : return false;
17889 : : return true;
17890 : : }
17891 : :
17892 : : /* Execute fold_vec_perm_cst unit tests. */
17893 : :
17894 : : static void
17895 : 4 : test ()
17896 : : {
17897 : 4 : machine_mode vnx4si_mode = E_VOIDmode;
17898 : 4 : machine_mode v4si_mode = E_VOIDmode;
17899 : :
17900 : 4 : machine_mode vmode;
17901 : 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17902 : : {
17903 : : /* Obtain modes corresponding to VNx4SI and V4SI,
17904 : : to call mixed mode tests below.
17905 : : FIXME: Is there a better way to do this ? */
17906 : 124 : if (GET_MODE_INNER (vmode) == SImode)
17907 : : {
17908 : 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17909 : 124 : if (is_simple_vla_size (nunits)
17910 : : && nunits.coeffs[0] == 4)
17911 : : vnx4si_mode = vmode;
17912 : 124 : else if (known_eq (nunits, poly_uint64 (4)))
17913 : 124 : v4si_mode = vmode;
17914 : : }
17915 : :
17916 : 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17917 : : || !targetm.vector_mode_supported_p (vmode))
17918 : 124 : continue;
17919 : :
17920 : : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17921 : : test_all_nunits (vmode);
17922 : : if (nunits.coeffs[0] >= 2)
17923 : : test_nunits_min_2 (vmode);
17924 : : if (nunits.coeffs[0] >= 4)
17925 : : test_nunits_min_4 (vmode);
17926 : : if (nunits.coeffs[0] >= 8)
17927 : : test_nunits_min_8 (vmode);
17928 : :
17929 : : if (nunits.coeffs[0] <= 4)
17930 : : test_nunits_max_4 (vmode);
17931 : : }
17932 : :
17933 : 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17934 : : && targetm.vector_mode_supported_p (vnx4si_mode)
17935 : : && targetm.vector_mode_supported_p (v4si_mode))
17936 : : {
17937 : : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17938 : : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17939 : : }
17940 : 4 : }
17941 : : } // end of test_fold_vec_perm_cst namespace
17942 : :
17943 : : /* Verify that various binary operations on vectors are folded
17944 : : correctly. */
17945 : :
17946 : : static void
17947 : 4 : test_vector_folding ()
17948 : : {
17949 : 4 : tree inner_type = integer_type_node;
17950 : 4 : tree type = build_vector_type (inner_type, 4);
17951 : 4 : tree zero = build_zero_cst (type);
17952 : 4 : tree one = build_one_cst (type);
17953 : 4 : tree index = build_index_vector (type, 0, 1);
17954 : :
17955 : : /* Verify equality tests that return a scalar boolean result. */
17956 : 4 : tree res_type = boolean_type_node;
17957 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17958 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17959 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17960 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17961 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17962 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17963 : : index, one)));
17964 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17965 : : index, index)));
17966 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17967 : : index, index)));
17968 : 4 : }
17969 : :
17970 : : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17971 : :
17972 : : static void
17973 : 4 : test_vec_duplicate_folding ()
17974 : : {
17975 : 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17976 : 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17977 : : /* This will be 1 if VEC_MODE isn't a vector mode. */
17978 : 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17979 : :
17980 : 4 : tree type = build_vector_type (ssizetype, nunits);
17981 : 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17982 : 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17983 : 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17984 : 4 : }
17985 : :
17986 : : /* Run all of the selftests within this file. */
17987 : :
17988 : : void
17989 : 4 : fold_const_cc_tests ()
17990 : : {
17991 : 4 : test_arithmetic_folding ();
17992 : 4 : test_vector_folding ();
17993 : 4 : test_vec_duplicate_folding ();
17994 : 4 : test_fold_vec_perm_cst::test ();
17995 : 4 : test_operand_equality::test ();
17996 : 4 : }
17997 : :
17998 : : } // namespace selftest
17999 : :
18000 : : #endif /* CHECKING_P */
|