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_wide and size_binop.
29 : :
30 : : fold takes a tree as argument and returns a simplified tree.
31 : :
32 : : size_binop takes a tree code for an arithmetic operation
33 : : and two operands that are trees, and produces a tree for the
34 : : result, assuming the type comes from `sizetype'.
35 : :
36 : : size_int takes an integer value, and creates a tree constant
37 : : with type from `sizetype'.
38 : :
39 : : Note: Since the folders get called on non-gimple code as well as
40 : : gimple code, we need to handle GIMPLE tuples as well as their
41 : : corresponding tree equivalents. */
42 : :
43 : : #define INCLUDE_ALGORITHM
44 : : #include "config.h"
45 : : #include "system.h"
46 : : #include "coretypes.h"
47 : : #include "backend.h"
48 : : #include "target.h"
49 : : #include "rtl.h"
50 : : #include "tree.h"
51 : : #include "gimple.h"
52 : : #include "predict.h"
53 : : #include "memmodel.h"
54 : : #include "tm_p.h"
55 : : #include "tree-ssa-operands.h"
56 : : #include "optabs-query.h"
57 : : #include "cgraph.h"
58 : : #include "diagnostic-core.h"
59 : : #include "flags.h"
60 : : #include "alias.h"
61 : : #include "fold-const.h"
62 : : #include "fold-const-call.h"
63 : : #include "stor-layout.h"
64 : : #include "calls.h"
65 : : #include "tree-iterator.h"
66 : : #include "expr.h"
67 : : #include "intl.h"
68 : : #include "langhooks.h"
69 : : #include "tree-eh.h"
70 : : #include "gimplify.h"
71 : : #include "tree-dfa.h"
72 : : #include "builtins.h"
73 : : #include "generic-match.h"
74 : : #include "gimple-iterator.h"
75 : : #include "gimple-fold.h"
76 : : #include "tree-into-ssa.h"
77 : : #include "md5.h"
78 : : #include "case-cfn-macros.h"
79 : : #include "stringpool.h"
80 : : #include "tree-vrp.h"
81 : : #include "tree-ssanames.h"
82 : : #include "selftest.h"
83 : : #include "stringpool.h"
84 : : #include "attribs.h"
85 : : #include "tree-vector-builder.h"
86 : : #include "vec-perm-indices.h"
87 : : #include "asan.h"
88 : : #include "gimple-range.h"
89 : :
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 EXP1) ? EXP2 : EXP3". */
155 : : tree_code
156 : 137733 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
157 : : {
158 : 137733 : enum tree_code code = ERROR_MARK;
159 : :
160 : 137733 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
161 : 11 : return ERROR_MARK;
162 : :
163 : 137722 : if (!operand_equal_p (exp0, exp2))
164 : : return ERROR_MARK;
165 : :
166 : 137722 : if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST)
167 : : {
168 : 135074 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
169 : : {
170 : : /* X <= Y - 1 equals to X < Y. */
171 : 77715 : if (cmp == LE_EXPR)
172 : : code = LT_EXPR;
173 : : /* X > Y - 1 equals to X >= Y. */
174 : 77352 : if (cmp == GT_EXPR)
175 : : code = GE_EXPR;
176 : : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
177 : 67938 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : : {
179 : 16961 : int_range_max r;
180 : 33922 : get_range_query (cfun)->range_of_expr (r, exp0);
181 : 16961 : if (r.undefined_p ())
182 : 0 : r.set_varying (TREE_TYPE (exp0));
183 : :
184 : 16961 : widest_int min = widest_int::from (r.lower_bound (),
185 : 33922 : TYPE_SIGN (TREE_TYPE (exp0)));
186 : 16961 : if (min == wi::to_widest (exp1))
187 : 736 : code = MAX_EXPR;
188 : 16961 : }
189 : : }
190 : 135074 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
191 : : {
192 : : /* X < Y + 1 equals to X <= Y. */
193 : 1007 : if (cmp == LT_EXPR)
194 : : code = LE_EXPR;
195 : : /* X >= Y + 1 equals to X > Y. */
196 : 979 : if (cmp == GE_EXPR)
197 : : code = GT_EXPR;
198 : : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
199 : 882 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : : {
201 : 495 : int_range_max r;
202 : 990 : get_range_query (cfun)->range_of_expr (r, exp0);
203 : 495 : if (r.undefined_p ())
204 : 0 : r.set_varying (TREE_TYPE (exp0));
205 : :
206 : 495 : widest_int max = widest_int::from (r.upper_bound (),
207 : 990 : TYPE_SIGN (TREE_TYPE (exp0)));
208 : 495 : if (max == wi::to_widest (exp1))
209 : 43 : code = MIN_EXPR;
210 : 495 : }
211 : : }
212 : : }
213 : 135074 : if (code != ERROR_MARK
214 : 137722 : || operand_equal_p (exp1, exp3))
215 : : {
216 : 24276 : if (cmp == LT_EXPR || cmp == LE_EXPR)
217 : : code = MIN_EXPR;
218 : 21896 : if (cmp == GT_EXPR || cmp == GE_EXPR)
219 : 20994 : code = MAX_EXPR;
220 : : }
221 : : return code;
222 : : }
223 : :
224 : : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
225 : : Otherwise, return LOC. */
226 : :
227 : : static location_t
228 : 2384104 : expr_location_or (tree t, location_t loc)
229 : : {
230 : 660407 : location_t tloc = EXPR_LOCATION (t);
231 : 2369939 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
232 : : }
233 : :
234 : : /* Similar to protected_set_expr_location, but never modify x in place,
235 : : if location can and needs to be set, unshare it. */
236 : :
237 : : tree
238 : 4569518 : protected_set_expr_location_unshare (tree x, location_t loc)
239 : : {
240 : 4569518 : if (CAN_HAVE_LOCATION_P (x)
241 : 4054209 : && EXPR_LOCATION (x) != loc
242 : 1825083 : && !(TREE_CODE (x) == SAVE_EXPR
243 : 912762 : || TREE_CODE (x) == TARGET_EXPR
244 : : || TREE_CODE (x) == BIND_EXPR))
245 : : {
246 : 912009 : x = copy_node (x);
247 : 912009 : SET_EXPR_LOCATION (x, loc);
248 : : }
249 : 4569518 : return x;
250 : : }
251 : :
252 : : /* If ARG2 divides ARG1 with zero remainder, carries out the exact
253 : : division and returns the quotient. Otherwise returns
254 : : NULL_TREE. */
255 : :
256 : : tree
257 : 0 : div_if_zero_remainder (const_tree arg1, const_tree arg2)
258 : : {
259 : 0 : widest_int quo;
260 : :
261 : 0 : if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
262 : : SIGNED, &quo))
263 : 0 : return wide_int_to_tree (TREE_TYPE (arg1), quo);
264 : :
265 : : return NULL_TREE;
266 : 0 : }
267 : :
268 : : /* This is nonzero if we should defer warnings about undefined
269 : : overflow. This facility exists because these warnings are a
270 : : special case. The code to estimate loop iterations does not want
271 : : to issue any warnings, since it works with expressions which do not
272 : : occur in user code. Various bits of cleanup code call fold(), but
273 : : only use the result if it has certain characteristics (e.g., is a
274 : : constant); that code only wants to issue a warning if the result is
275 : : used. */
276 : :
277 : : static int fold_deferring_overflow_warnings;
278 : :
279 : : /* If a warning about undefined overflow is deferred, this is the
280 : : warning. Note that this may cause us to turn two warnings into
281 : : one, but that is fine since it is sufficient to only give one
282 : : warning per expression. */
283 : :
284 : : static const char* fold_deferred_overflow_warning;
285 : :
286 : : /* If a warning about undefined overflow is deferred, this is the
287 : : level at which the warning should be emitted. */
288 : :
289 : : static enum warn_strict_overflow_code fold_deferred_overflow_code;
290 : :
291 : : /* Start deferring overflow warnings. We could use a stack here to
292 : : permit nested calls, but at present it is not necessary. */
293 : :
294 : : void
295 : 1167426498 : fold_defer_overflow_warnings (void)
296 : : {
297 : 1167426498 : ++fold_deferring_overflow_warnings;
298 : 1167426498 : }
299 : :
300 : : /* Stop deferring overflow warnings. If there is a pending warning,
301 : : and ISSUE is true, then issue the warning if appropriate. STMT is
302 : : the statement with which the warning should be associated (used for
303 : : location information); STMT may be NULL. CODE is the level of the
304 : : warning--a warn_strict_overflow_code value. This function will use
305 : : the smaller of CODE and the deferred code when deciding whether to
306 : : issue the warning. CODE may be zero to mean to always use the
307 : : deferred code. */
308 : :
309 : : void
310 : 1167426498 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
311 : : {
312 : 1167426498 : const char *warnmsg;
313 : 1167426498 : location_t locus;
314 : :
315 : 1167426498 : gcc_assert (fold_deferring_overflow_warnings > 0);
316 : 1167426498 : --fold_deferring_overflow_warnings;
317 : 1167426498 : if (fold_deferring_overflow_warnings > 0)
318 : : {
319 : 8782140 : if (fold_deferred_overflow_warning != NULL
320 : 1803712 : && code != 0
321 : 0 : && code < (int) fold_deferred_overflow_code)
322 : 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
323 : 8782140 : return;
324 : : }
325 : :
326 : 1158644358 : warnmsg = fold_deferred_overflow_warning;
327 : 1158644358 : fold_deferred_overflow_warning = NULL;
328 : :
329 : 1158644358 : if (!issue || warnmsg == NULL)
330 : : return;
331 : :
332 : 10511 : if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
333 : : return;
334 : :
335 : : /* Use the smallest code level when deciding to issue the
336 : : warning. */
337 : 10511 : if (code == 0 || code > (int) fold_deferred_overflow_code)
338 : 10511 : code = fold_deferred_overflow_code;
339 : :
340 : 10511 : if (!issue_strict_overflow_warning (code))
341 : : return;
342 : :
343 : 0 : if (stmt == NULL)
344 : : locus = input_location;
345 : : else
346 : 0 : locus = gimple_location (stmt);
347 : 0 : warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
348 : : }
349 : :
350 : : /* Stop deferring overflow warnings, ignoring any deferred
351 : : warnings. */
352 : :
353 : : void
354 : 179794226 : fold_undefer_and_ignore_overflow_warnings (void)
355 : : {
356 : 179794226 : fold_undefer_overflow_warnings (false, NULL, 0);
357 : 179794226 : }
358 : :
359 : : /* Whether we are deferring overflow warnings. */
360 : :
361 : : bool
362 : 315886490 : fold_deferring_overflow_warnings_p (void)
363 : : {
364 : 315886490 : return fold_deferring_overflow_warnings > 0;
365 : : }
366 : :
367 : : /* This is called when we fold something based on the fact that signed
368 : : overflow is undefined. */
369 : :
370 : : void
371 : 1754973 : fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
372 : : {
373 : 1754973 : if (fold_deferring_overflow_warnings > 0)
374 : : {
375 : 1703774 : if (fold_deferred_overflow_warning == NULL
376 : 761695 : || wc < fold_deferred_overflow_code)
377 : : {
378 : 964935 : fold_deferred_overflow_warning = gmsgid;
379 : 964935 : fold_deferred_overflow_code = wc;
380 : : }
381 : : }
382 : 51199 : else if (issue_strict_overflow_warning (wc))
383 : 7 : warning (OPT_Wstrict_overflow, gmsgid);
384 : 1754973 : }
385 : :
386 : : /* Return true if the built-in mathematical function specified by CODE
387 : : is odd, i.e. -f(x) == f(-x). */
388 : :
389 : : bool
390 : 2008071 : negate_mathfn_p (combined_fn fn)
391 : : {
392 : 2008071 : switch (fn)
393 : : {
394 : : CASE_CFN_ASIN:
395 : : CASE_CFN_ASIN_FN:
396 : : CASE_CFN_ASINH:
397 : : CASE_CFN_ASINH_FN:
398 : : CASE_CFN_ASINPI:
399 : : CASE_CFN_ASINPI_FN:
400 : : CASE_CFN_ATAN:
401 : : CASE_CFN_ATAN_FN:
402 : : CASE_CFN_ATANH:
403 : : CASE_CFN_ATANH_FN:
404 : : CASE_CFN_ATANPI:
405 : : CASE_CFN_ATANPI_FN:
406 : : CASE_CFN_CASIN:
407 : : CASE_CFN_CASIN_FN:
408 : : CASE_CFN_CASINH:
409 : : CASE_CFN_CASINH_FN:
410 : : CASE_CFN_CATAN:
411 : : CASE_CFN_CATAN_FN:
412 : : CASE_CFN_CATANH:
413 : : CASE_CFN_CATANH_FN:
414 : : CASE_CFN_CBRT:
415 : : CASE_CFN_CBRT_FN:
416 : : CASE_CFN_CPROJ:
417 : : CASE_CFN_CPROJ_FN:
418 : : CASE_CFN_CSIN:
419 : : CASE_CFN_CSIN_FN:
420 : : CASE_CFN_CSINH:
421 : : CASE_CFN_CSINH_FN:
422 : : CASE_CFN_CTAN:
423 : : CASE_CFN_CTAN_FN:
424 : : CASE_CFN_CTANH:
425 : : CASE_CFN_CTANH_FN:
426 : : CASE_CFN_ERF:
427 : : CASE_CFN_ERF_FN:
428 : : CASE_CFN_LLROUND:
429 : : CASE_CFN_LLROUND_FN:
430 : : CASE_CFN_LROUND:
431 : : CASE_CFN_LROUND_FN:
432 : : CASE_CFN_ROUND:
433 : : CASE_CFN_ROUNDEVEN:
434 : : CASE_CFN_ROUNDEVEN_FN:
435 : : CASE_CFN_SIN:
436 : : CASE_CFN_SIN_FN:
437 : : CASE_CFN_SINH:
438 : : CASE_CFN_SINH_FN:
439 : : CASE_CFN_SINPI:
440 : : CASE_CFN_SINPI_FN:
441 : : CASE_CFN_TAN:
442 : : CASE_CFN_TAN_FN:
443 : : CASE_CFN_TANH:
444 : : CASE_CFN_TANH_FN:
445 : : CASE_CFN_TANPI:
446 : : CASE_CFN_TANPI_FN:
447 : : CASE_CFN_TRUNC:
448 : : CASE_CFN_TRUNC_FN:
449 : : return true;
450 : :
451 : 390 : CASE_CFN_LLRINT:
452 : 390 : CASE_CFN_LLRINT_FN:
453 : 390 : CASE_CFN_LRINT:
454 : 390 : CASE_CFN_LRINT_FN:
455 : 390 : CASE_CFN_NEARBYINT:
456 : 390 : CASE_CFN_NEARBYINT_FN:
457 : 390 : CASE_CFN_RINT:
458 : 390 : CASE_CFN_RINT_FN:
459 : 390 : return !flag_rounding_math;
460 : :
461 : 2004089 : default:
462 : 2004089 : break;
463 : : }
464 : 2004089 : return false;
465 : : }
466 : :
467 : : /* Check whether we may negate an integer constant T without causing
468 : : overflow. */
469 : :
470 : : bool
471 : 3052325 : may_negate_without_overflow_p (const_tree t)
472 : : {
473 : 3052325 : tree type;
474 : :
475 : 3052325 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
476 : :
477 : 3052325 : type = TREE_TYPE (t);
478 : 3052325 : if (TYPE_UNSIGNED (type))
479 : : return false;
480 : :
481 : 3052325 : return !wi::only_sign_bit_p (wi::to_wide (t));
482 : : }
483 : :
484 : : /* Determine whether an expression T can be cheaply negated using
485 : : the function negate_expr without introducing undefined overflow. */
486 : :
487 : : static bool
488 : 26352042 : negate_expr_p (tree t)
489 : : {
490 : 26504077 : tree type;
491 : :
492 : 26504077 : if (t == 0)
493 : : return false;
494 : :
495 : 26504077 : type = TREE_TYPE (t);
496 : :
497 : 26504077 : STRIP_SIGN_NOPS (t);
498 : 26504077 : switch (TREE_CODE (t))
499 : : {
500 : 1532412 : case INTEGER_CST:
501 : 1532412 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
502 : : return true;
503 : :
504 : : /* Check that -CST will not overflow type. */
505 : 380731 : return may_negate_without_overflow_p (t);
506 : 515 : case BIT_NOT_EXPR:
507 : 515 : return (INTEGRAL_TYPE_P (type)
508 : 515 : && TYPE_OVERFLOW_WRAPS (type));
509 : :
510 : : case FIXED_CST:
511 : : return true;
512 : :
513 : 1308 : case NEGATE_EXPR:
514 : 1308 : return !TYPE_OVERFLOW_SANITIZED (type);
515 : :
516 : 1325885 : case REAL_CST:
517 : : /* We want to canonicalize to positive real constants. Pretend
518 : : that only negative ones can be easily negated. */
519 : 1325885 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
520 : :
521 : 454 : case COMPLEX_CST:
522 : 454 : return negate_expr_p (TREE_REALPART (t))
523 : 572 : && negate_expr_p (TREE_IMAGPART (t));
524 : :
525 : 97 : case VECTOR_CST:
526 : 97 : {
527 : 97 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
528 : : return true;
529 : :
530 : : /* Steps don't prevent negation. */
531 : 97 : unsigned int count = vector_cst_encoded_nelts (t);
532 : 194 : for (unsigned int i = 0; i < count; ++i)
533 : 97 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
534 : : return false;
535 : :
536 : : return true;
537 : : }
538 : :
539 : 705 : case COMPLEX_EXPR:
540 : 705 : return negate_expr_p (TREE_OPERAND (t, 0))
541 : 705 : && negate_expr_p (TREE_OPERAND (t, 1));
542 : :
543 : 33 : case CONJ_EXPR:
544 : 33 : return negate_expr_p (TREE_OPERAND (t, 0));
545 : :
546 : 1467238 : case PLUS_EXPR:
547 : 1467238 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
548 : 1467232 : || HONOR_SIGNED_ZEROS (type)
549 : 2638617 : || (ANY_INTEGRAL_TYPE_P (type)
550 : 1171193 : && ! TYPE_OVERFLOW_WRAPS (type)))
551 : 719890 : return false;
552 : : /* -(A + B) -> (-B) - A. */
553 : 747348 : if (negate_expr_p (TREE_OPERAND (t, 1)))
554 : : return true;
555 : : /* -(A + B) -> (-A) - B. */
556 : 138321 : return negate_expr_p (TREE_OPERAND (t, 0));
557 : :
558 : 250311 : case MINUS_EXPR:
559 : : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
560 : 250311 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
561 : 250311 : && !HONOR_SIGNED_ZEROS (type)
562 : 322066 : && (! ANY_INTEGRAL_TYPE_P (type)
563 : 71532 : || TYPE_OVERFLOW_WRAPS (type));
564 : :
565 : 2423623 : case MULT_EXPR:
566 : 2423623 : if (TYPE_UNSIGNED (type))
567 : : break;
568 : : /* INT_MIN/n * n doesn't overflow while negating one operand it does
569 : : if n is a (negative) power of two. */
570 : 4215006 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
571 : 158152 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
572 : 2263334 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
573 : 0 : && (wi::popcount
574 : 2107503 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
575 : 155831 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
576 : 133130 : && (wi::popcount
577 : 4325435 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
578 : : break;
579 : :
580 : : /* Fall through. */
581 : :
582 : 2393254 : case RDIV_EXPR:
583 : 2393254 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
584 : 2393253 : return negate_expr_p (TREE_OPERAND (t, 1))
585 : 2393253 : || negate_expr_p (TREE_OPERAND (t, 0));
586 : : break;
587 : :
588 : 2429 : case TRUNC_DIV_EXPR:
589 : 2429 : case ROUND_DIV_EXPR:
590 : 2429 : case EXACT_DIV_EXPR:
591 : 2429 : if (TYPE_UNSIGNED (type))
592 : : break;
593 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
594 : : B is not 1 we change the sign of the result. */
595 : 491 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
596 : 491 : && negate_expr_p (TREE_OPERAND (t, 0)))
597 : : return true;
598 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
599 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
600 : : and actually traps on some architectures. */
601 : 652 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
602 : 326 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
603 : 567 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
604 : 232 : && ! integer_onep (TREE_OPERAND (t, 1))))
605 : 317 : return negate_expr_p (TREE_OPERAND (t, 1));
606 : : break;
607 : :
608 : 4550009 : case NOP_EXPR:
609 : : /* Negate -((double)float) as (double)(-float). */
610 : 4550009 : if (SCALAR_FLOAT_TYPE_P (type))
611 : : {
612 : 13512 : tree tem = strip_float_extensions (t);
613 : 13512 : if (tem != t)
614 : : return negate_expr_p (tem);
615 : : }
616 : : break;
617 : :
618 : 1001248 : case CALL_EXPR:
619 : : /* Negate -f(x) as f(-x). */
620 : 1001248 : if (negate_mathfn_p (get_call_combined_fn (t)))
621 : 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
622 : : break;
623 : :
624 : 643 : case RSHIFT_EXPR:
625 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
626 : 643 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
627 : : {
628 : 498 : tree op1 = TREE_OPERAND (t, 1);
629 : 498 : if (wi::to_wide (op1) == element_precision (type) - 1)
630 : : return true;
631 : : }
632 : : break;
633 : :
634 : : default:
635 : : break;
636 : : }
637 : : return false;
638 : : }
639 : :
640 : : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
641 : : simplification is possible.
642 : : If negate_expr_p would return true for T, NULL_TREE will never be
643 : : returned. */
644 : :
645 : : static tree
646 : 36531580 : fold_negate_expr_1 (location_t loc, tree t)
647 : : {
648 : 36531580 : tree type = TREE_TYPE (t);
649 : 36531580 : tree tem;
650 : :
651 : 36531580 : switch (TREE_CODE (t))
652 : : {
653 : : /* Convert - (~A) to A + 1. */
654 : 138 : case BIT_NOT_EXPR:
655 : 138 : if (INTEGRAL_TYPE_P (type))
656 : 138 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
657 : 138 : build_one_cst (type));
658 : : break;
659 : :
660 : 28245678 : case INTEGER_CST:
661 : 28245678 : tem = fold_negate_const (t, type);
662 : 28245678 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
663 : 8791 : || (ANY_INTEGRAL_TYPE_P (type)
664 : 8791 : && !TYPE_OVERFLOW_TRAPS (type)
665 : 8791 : && TYPE_OVERFLOW_WRAPS (type))
666 : 28253776 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
667 : : return tem;
668 : : break;
669 : :
670 : 2023592 : case POLY_INT_CST:
671 : 2023592 : case REAL_CST:
672 : 2023592 : case FIXED_CST:
673 : 2023592 : tem = fold_negate_const (t, type);
674 : 2023592 : return tem;
675 : :
676 : 66138 : case COMPLEX_CST:
677 : 66138 : {
678 : 66138 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
679 : 66138 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
680 : 66138 : if (rpart && ipart)
681 : 66138 : return build_complex (type, rpart, ipart);
682 : : }
683 : : break;
684 : :
685 : 49953 : case VECTOR_CST:
686 : 49953 : {
687 : 49953 : tree_vector_builder elts;
688 : 49953 : elts.new_unary_operation (type, t, true);
689 : 49953 : unsigned int count = elts.encoded_nelts ();
690 : 121194 : for (unsigned int i = 0; i < count; ++i)
691 : : {
692 : 71241 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
693 : 71241 : if (elt == NULL_TREE)
694 : 0 : return NULL_TREE;
695 : 71241 : elts.quick_push (elt);
696 : : }
697 : :
698 : 49953 : return elts.build ();
699 : 49953 : }
700 : :
701 : 78 : case COMPLEX_EXPR:
702 : 78 : if (negate_expr_p (t))
703 : 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
704 : 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
705 : 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
706 : : break;
707 : :
708 : 21 : case CONJ_EXPR:
709 : 21 : if (negate_expr_p (t))
710 : 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
711 : 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
712 : : break;
713 : :
714 : 1234 : case NEGATE_EXPR:
715 : 1234 : if (!TYPE_OVERFLOW_SANITIZED (type))
716 : 1221 : return TREE_OPERAND (t, 0);
717 : : break;
718 : :
719 : 663905 : case PLUS_EXPR:
720 : 663905 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
721 : 663905 : && !HONOR_SIGNED_ZEROS (type))
722 : : {
723 : : /* -(A + B) -> (-B) - A. */
724 : 663795 : if (negate_expr_p (TREE_OPERAND (t, 1)))
725 : : {
726 : 612682 : tem = negate_expr (TREE_OPERAND (t, 1));
727 : 612682 : return fold_build2_loc (loc, MINUS_EXPR, type,
728 : 1225364 : tem, TREE_OPERAND (t, 0));
729 : : }
730 : :
731 : : /* -(A + B) -> (-A) - B. */
732 : 51113 : if (negate_expr_p (TREE_OPERAND (t, 0)))
733 : : {
734 : 995 : tem = negate_expr (TREE_OPERAND (t, 0));
735 : 995 : return fold_build2_loc (loc, MINUS_EXPR, type,
736 : 1990 : tem, TREE_OPERAND (t, 1));
737 : : }
738 : : }
739 : : break;
740 : :
741 : 145919 : case MINUS_EXPR:
742 : : /* - (A - B) -> B - A */
743 : 145919 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
744 : 145919 : && !HONOR_SIGNED_ZEROS (type))
745 : 66648 : return fold_build2_loc (loc, MINUS_EXPR, type,
746 : 133296 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
747 : : break;
748 : :
749 : 124020 : case MULT_EXPR:
750 : 124020 : if (TYPE_UNSIGNED (type))
751 : : break;
752 : :
753 : : /* Fall through. */
754 : :
755 : 34242 : case RDIV_EXPR:
756 : 34242 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
757 : : {
758 : 34242 : tem = TREE_OPERAND (t, 1);
759 : 34242 : if (negate_expr_p (tem))
760 : 61530 : return fold_build2_loc (loc, TREE_CODE (t), type,
761 : 61530 : TREE_OPERAND (t, 0), negate_expr (tem));
762 : 3477 : tem = TREE_OPERAND (t, 0);
763 : 3477 : if (negate_expr_p (tem))
764 : 68 : return fold_build2_loc (loc, TREE_CODE (t), type,
765 : 136 : negate_expr (tem), TREE_OPERAND (t, 1));
766 : : }
767 : : break;
768 : :
769 : 1799 : case TRUNC_DIV_EXPR:
770 : 1799 : case ROUND_DIV_EXPR:
771 : 1799 : case EXACT_DIV_EXPR:
772 : 1799 : if (TYPE_UNSIGNED (type))
773 : : break;
774 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
775 : : B is not 1 we change the sign of the result. */
776 : 668 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
777 : 668 : && negate_expr_p (TREE_OPERAND (t, 0)))
778 : 323 : return fold_build2_loc (loc, TREE_CODE (t), type,
779 : 323 : negate_expr (TREE_OPERAND (t, 0)),
780 : 646 : TREE_OPERAND (t, 1));
781 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
782 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
783 : : and actually traps on some architectures. */
784 : 690 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
785 : 345 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
786 : 261 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
787 : 238 : && ! integer_onep (TREE_OPERAND (t, 1))))
788 : 667 : && negate_expr_p (TREE_OPERAND (t, 1)))
789 : 632 : return fold_build2_loc (loc, TREE_CODE (t), type,
790 : 316 : TREE_OPERAND (t, 0),
791 : 632 : negate_expr (TREE_OPERAND (t, 1)));
792 : : break;
793 : :
794 : 1744657 : case NOP_EXPR:
795 : : /* Convert -((double)float) into (double)(-float). */
796 : 1744657 : if (SCALAR_FLOAT_TYPE_P (type))
797 : : {
798 : 11138 : tem = strip_float_extensions (t);
799 : 11138 : if (tem != t && negate_expr_p (tem))
800 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
801 : : }
802 : : break;
803 : :
804 : 300409 : case CALL_EXPR:
805 : : /* Negate -f(x) as f(-x). */
806 : 300409 : if (negate_mathfn_p (get_call_combined_fn (t))
807 : 301698 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
808 : : {
809 : 1191 : tree fndecl, arg;
810 : :
811 : 1191 : fndecl = get_callee_fndecl (t);
812 : 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
813 : 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
814 : : }
815 : : break;
816 : :
817 : 388 : case RSHIFT_EXPR:
818 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
819 : 388 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
820 : : {
821 : 370 : tree op1 = TREE_OPERAND (t, 1);
822 : 370 : if (wi::to_wide (op1) == element_precision (type) - 1)
823 : : {
824 : 72 : tree ntype = TYPE_UNSIGNED (type)
825 : 72 : ? signed_type_for (type)
826 : 72 : : unsigned_type_for (type);
827 : 72 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
828 : 72 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
829 : 72 : return fold_convert_loc (loc, type, temp);
830 : : }
831 : : }
832 : : break;
833 : :
834 : : default:
835 : : break;
836 : : }
837 : :
838 : : return NULL_TREE;
839 : : }
840 : :
841 : : /* A wrapper for fold_negate_expr_1. */
842 : :
843 : : static tree
844 : 36531580 : fold_negate_expr (location_t loc, tree t)
845 : : {
846 : 36531580 : tree type = TREE_TYPE (t);
847 : 36531580 : STRIP_SIGN_NOPS (t);
848 : 36531580 : tree tem = fold_negate_expr_1 (loc, t);
849 : 36531580 : if (tem == NULL_TREE)
850 : : return NULL_TREE;
851 : 31099709 : return fold_convert_loc (loc, type, tem);
852 : : }
853 : :
854 : : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
855 : : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
856 : : return NULL_TREE. */
857 : :
858 : : static tree
859 : 3474216 : negate_expr (tree t)
860 : : {
861 : 3474216 : tree type, tem;
862 : 3474216 : location_t loc;
863 : :
864 : 3474216 : if (t == NULL_TREE)
865 : : return NULL_TREE;
866 : :
867 : 3474216 : loc = EXPR_LOCATION (t);
868 : 3474216 : type = TREE_TYPE (t);
869 : 3474216 : STRIP_SIGN_NOPS (t);
870 : :
871 : 3474216 : tem = fold_negate_expr (loc, t);
872 : 3474216 : if (!tem)
873 : 1678620 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
874 : 3474216 : return fold_convert_loc (loc, type, tem);
875 : : }
876 : :
877 : : /* Split a tree IN into a constant, literal and variable parts that could be
878 : : combined with CODE to make IN. "constant" means an expression with
879 : : TREE_CONSTANT but that isn't an actual constant. CODE must be a
880 : : commutative arithmetic operation. Store the constant part into *CONP,
881 : : the literal in *LITP and return the variable part. If a part isn't
882 : : present, set it to null. If the tree does not decompose in this way,
883 : : return the entire tree as the variable part and the other parts as null.
884 : :
885 : : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
886 : : case, we negate an operand that was subtracted. Except if it is a
887 : : literal for which we use *MINUS_LITP instead.
888 : :
889 : : If NEGATE_P is true, we are negating all of IN, again except a literal
890 : : for which we use *MINUS_LITP instead. If a variable part is of pointer
891 : : type, it is negated after converting to TYPE. This prevents us from
892 : : generating illegal MINUS pointer expression. LOC is the location of
893 : : the converted variable part.
894 : :
895 : : If IN is itself a literal or constant, return it as appropriate.
896 : :
897 : : Note that we do not guarantee that any of the three values will be the
898 : : same type as IN, but they will have the same signedness and mode. */
899 : :
900 : : static tree
901 : 214659410 : split_tree (tree in, tree type, enum tree_code code,
902 : : tree *minus_varp, tree *conp, tree *minus_conp,
903 : : tree *litp, tree *minus_litp, int negate_p)
904 : : {
905 : 214659410 : tree var = 0;
906 : 214659410 : *minus_varp = 0;
907 : 214659410 : *conp = 0;
908 : 214659410 : *minus_conp = 0;
909 : 214659410 : *litp = 0;
910 : 214659410 : *minus_litp = 0;
911 : :
912 : : /* Strip any conversions that don't change the machine mode or signedness. */
913 : 214659410 : STRIP_SIGN_NOPS (in);
914 : :
915 : 214659410 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
916 : 136229749 : || TREE_CODE (in) == FIXED_CST)
917 : 78429661 : *litp = in;
918 : 136229749 : else if (TREE_CODE (in) == code
919 : 136229749 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
920 : 131975338 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
921 : : /* We can associate addition and subtraction together (even
922 : : though the C standard doesn't say so) for integers because
923 : : the value is not affected. For reals, the value might be
924 : : affected, so we can't. */
925 : 131975338 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
926 : 55279432 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
927 : 130378852 : || (code == MINUS_EXPR
928 : 20605794 : && (TREE_CODE (in) == PLUS_EXPR
929 : 18931530 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
930 : : {
931 : 7928394 : tree op0 = TREE_OPERAND (in, 0);
932 : 7928394 : tree op1 = TREE_OPERAND (in, 1);
933 : 7928394 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
934 : 7928394 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
935 : :
936 : : /* First see if either of the operands is a literal, then a constant. */
937 : 7928394 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
938 : 7706617 : || TREE_CODE (op0) == FIXED_CST)
939 : 221777 : *litp = op0, op0 = 0;
940 : 7706617 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
941 : 5106072 : || TREE_CODE (op1) == FIXED_CST)
942 : 2600545 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
943 : :
944 : 7928394 : if (op0 != 0 && TREE_CONSTANT (op0))
945 : 14825 : *conp = op0, op0 = 0;
946 : 7913569 : else if (op1 != 0 && TREE_CONSTANT (op1))
947 : 44667 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
948 : :
949 : : /* If we haven't dealt with either operand, this is not a case we can
950 : : decompose. Otherwise, VAR is either of the ones remaining, if any. */
951 : 7928394 : if (op0 != 0 && op1 != 0)
952 : : var = in;
953 : 2874622 : else if (op0 != 0)
954 : : var = op0;
955 : : else
956 : 236602 : var = op1, neg_var_p = neg1_p;
957 : :
958 : : /* Now do any needed negations. */
959 : 7928394 : if (neg_litp_p)
960 : 22783 : *minus_litp = *litp, *litp = 0;
961 : 7928394 : if (neg_conp_p && *conp)
962 : 3314 : *minus_conp = *conp, *conp = 0;
963 : 7928394 : if (neg_var_p && var)
964 : 226055 : *minus_varp = var, var = 0;
965 : : }
966 : 128301355 : else if (TREE_CONSTANT (in))
967 : 774339 : *conp = in;
968 : 127527016 : else if (TREE_CODE (in) == BIT_NOT_EXPR
969 : 458652 : && code == PLUS_EXPR)
970 : : {
971 : : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
972 : : when IN is constant. */
973 : 356269 : *litp = build_minus_one_cst (type);
974 : 356269 : *minus_varp = TREE_OPERAND (in, 0);
975 : : }
976 : : else
977 : : var = in;
978 : :
979 : 214659410 : if (negate_p)
980 : : {
981 : 11369144 : if (*litp)
982 : 1171222 : *minus_litp = *litp, *litp = 0;
983 : 10197922 : else if (*minus_litp)
984 : 162 : *litp = *minus_litp, *minus_litp = 0;
985 : 11369144 : if (*conp)
986 : 21824 : *minus_conp = *conp, *conp = 0;
987 : 11347320 : else if (*minus_conp)
988 : 0 : *conp = *minus_conp, *minus_conp = 0;
989 : 11369144 : if (var)
990 : 11335138 : *minus_varp = var, var = 0;
991 : 34006 : else if (*minus_varp)
992 : 757 : var = *minus_varp, *minus_varp = 0;
993 : : }
994 : :
995 : 214659410 : if (*litp
996 : 214659410 : && TREE_OVERFLOW_P (*litp))
997 : 20845 : *litp = drop_tree_overflow (*litp);
998 : 214659410 : if (*minus_litp
999 : 214659410 : && TREE_OVERFLOW_P (*minus_litp))
1000 : 0 : *minus_litp = drop_tree_overflow (*minus_litp);
1001 : :
1002 : 214659410 : return var;
1003 : : }
1004 : :
1005 : : /* Re-associate trees split by the above function. T1 and T2 are
1006 : : either expressions to associate or null. Return the new
1007 : : expression, if any. LOC is the location of the new expression. If
1008 : : we build an operation, do it in TYPE and with CODE. */
1009 : :
1010 : : static tree
1011 : 19648794 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1012 : : {
1013 : 19648794 : if (t1 == 0)
1014 : : {
1015 : 12482513 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
1016 : : return t2;
1017 : : }
1018 : 7166281 : else if (t2 == 0)
1019 : : return t1;
1020 : :
1021 : : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1022 : : try to fold this since we will have infinite recursion. But do
1023 : : deal with any NEGATE_EXPRs. */
1024 : 4000598 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1025 : 3162912 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1026 : 3102863 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1027 : : {
1028 : 1548058 : if (code == PLUS_EXPR)
1029 : : {
1030 : 869149 : if (TREE_CODE (t1) == NEGATE_EXPR)
1031 : 54 : return build2_loc (loc, MINUS_EXPR, type,
1032 : : fold_convert_loc (loc, type, t2),
1033 : : fold_convert_loc (loc, type,
1034 : 108 : TREE_OPERAND (t1, 0)));
1035 : 869095 : else if (TREE_CODE (t2) == NEGATE_EXPR)
1036 : 1 : return build2_loc (loc, MINUS_EXPR, type,
1037 : : fold_convert_loc (loc, type, t1),
1038 : : fold_convert_loc (loc, type,
1039 : 2 : TREE_OPERAND (t2, 0)));
1040 : 869094 : else if (integer_zerop (t2))
1041 : 32816 : return fold_convert_loc (loc, type, t1);
1042 : : }
1043 : 678909 : else if (code == MINUS_EXPR)
1044 : : {
1045 : 657819 : if (integer_zerop (t2))
1046 : 0 : return fold_convert_loc (loc, type, t1);
1047 : : }
1048 : :
1049 : 1515187 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1050 : 1515187 : fold_convert_loc (loc, type, t2));
1051 : : }
1052 : :
1053 : 2452540 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1054 : 2452540 : fold_convert_loc (loc, type, t2));
1055 : : }
1056 : :
1057 : : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1058 : : for use in int_const_binop, size_binop and size_diffop. */
1059 : :
1060 : : static bool
1061 : 2109265991 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1062 : : {
1063 : 2109265991 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1064 : : return false;
1065 : 2109265991 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1066 : : return false;
1067 : :
1068 : 2109265991 : switch (code)
1069 : : {
1070 : : case LSHIFT_EXPR:
1071 : : case RSHIFT_EXPR:
1072 : : case LROTATE_EXPR:
1073 : : case RROTATE_EXPR:
1074 : : return true;
1075 : :
1076 : 2109265991 : default:
1077 : 2109265991 : break;
1078 : : }
1079 : :
1080 : 2109265991 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1081 : 2109265991 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1082 : 4218531982 : && TYPE_MODE (type1) == TYPE_MODE (type2);
1083 : : }
1084 : :
1085 : : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1086 : : a new constant in RES. Return FALSE if we don't know how to
1087 : : evaluate CODE at compile-time. */
1088 : :
1089 : : bool
1090 : 1200937501 : wide_int_binop (wide_int &res,
1091 : : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1092 : : signop sign, wi::overflow_type *overflow)
1093 : : {
1094 : 1200937501 : wide_int tmp;
1095 : 1200937501 : *overflow = wi::OVF_NONE;
1096 : 1200937501 : switch (code)
1097 : : {
1098 : 2029703 : case BIT_IOR_EXPR:
1099 : 2029703 : res = wi::bit_or (arg1, arg2);
1100 : 2029703 : break;
1101 : :
1102 : 98997 : case BIT_XOR_EXPR:
1103 : 98997 : res = wi::bit_xor (arg1, arg2);
1104 : 98997 : break;
1105 : :
1106 : 18360310 : case BIT_AND_EXPR:
1107 : 18360310 : res = wi::bit_and (arg1, arg2);
1108 : 18360310 : break;
1109 : :
1110 : 12364425 : case LSHIFT_EXPR:
1111 : 12364425 : if (wi::neg_p (arg2))
1112 : : return false;
1113 : 12334113 : res = wi::lshift (arg1, arg2);
1114 : 12334113 : break;
1115 : :
1116 : 7579194 : case RSHIFT_EXPR:
1117 : 7579194 : if (wi::neg_p (arg2))
1118 : : return false;
1119 : : /* It's unclear from the C standard whether shifts can overflow.
1120 : : The following code ignores overflow; perhaps a C standard
1121 : : interpretation ruling is needed. */
1122 : 7578996 : res = wi::rshift (arg1, arg2, sign);
1123 : 7578996 : break;
1124 : :
1125 : 1146 : case RROTATE_EXPR:
1126 : 1146 : case LROTATE_EXPR:
1127 : 1146 : if (wi::neg_p (arg2))
1128 : : {
1129 : 14 : tmp = -arg2;
1130 : 14 : if (code == RROTATE_EXPR)
1131 : : code = LROTATE_EXPR;
1132 : : else
1133 : : code = RROTATE_EXPR;
1134 : : }
1135 : : else
1136 : 1132 : tmp = arg2;
1137 : :
1138 : 1132 : if (code == RROTATE_EXPR)
1139 : 964 : res = wi::rrotate (arg1, tmp);
1140 : : else
1141 : 182 : res = wi::lrotate (arg1, tmp);
1142 : : break;
1143 : :
1144 : 201841473 : case PLUS_EXPR:
1145 : 201841473 : res = wi::add (arg1, arg2, sign, overflow);
1146 : 201841473 : break;
1147 : :
1148 : 69275639 : case MINUS_EXPR:
1149 : 69275639 : res = wi::sub (arg1, arg2, sign, overflow);
1150 : 69275639 : break;
1151 : :
1152 : 316405348 : case MULT_EXPR:
1153 : 316405348 : res = wi::mul (arg1, arg2, sign, overflow);
1154 : 316405348 : break;
1155 : :
1156 : 4776 : case MULT_HIGHPART_EXPR:
1157 : 4776 : res = wi::mul_high (arg1, arg2, sign);
1158 : 4776 : break;
1159 : :
1160 : 282537526 : case TRUNC_DIV_EXPR:
1161 : 282537526 : case EXACT_DIV_EXPR:
1162 : 282537526 : if (arg2 == 0)
1163 : : return false;
1164 : 282531583 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1165 : 282531583 : break;
1166 : :
1167 : 64973344 : case FLOOR_DIV_EXPR:
1168 : 64973344 : if (arg2 == 0)
1169 : : return false;
1170 : 64973344 : res = wi::div_floor (arg1, arg2, sign, overflow);
1171 : 64973344 : break;
1172 : :
1173 : 97143299 : case CEIL_DIV_EXPR:
1174 : 97143299 : if (arg2 == 0)
1175 : : return false;
1176 : 97143299 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1177 : 97143299 : break;
1178 : :
1179 : 0 : case ROUND_DIV_EXPR:
1180 : 0 : if (arg2 == 0)
1181 : : return false;
1182 : 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1183 : 0 : break;
1184 : :
1185 : 672541 : case TRUNC_MOD_EXPR:
1186 : 672541 : if (arg2 == 0)
1187 : : return false;
1188 : 671467 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1189 : 671467 : break;
1190 : :
1191 : 53830006 : case FLOOR_MOD_EXPR:
1192 : 53830006 : if (arg2 == 0)
1193 : : return false;
1194 : 53830006 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1195 : 53830006 : break;
1196 : :
1197 : 178 : case CEIL_MOD_EXPR:
1198 : 178 : if (arg2 == 0)
1199 : : return false;
1200 : 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1201 : 178 : break;
1202 : :
1203 : 0 : case ROUND_MOD_EXPR:
1204 : 0 : if (arg2 == 0)
1205 : : return false;
1206 : 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1207 : 0 : break;
1208 : :
1209 : 27220 : case MIN_EXPR:
1210 : 27220 : res = wi::min (arg1, arg2, sign);
1211 : 27220 : break;
1212 : :
1213 : 73792249 : case MAX_EXPR:
1214 : 73792249 : res = wi::max (arg1, arg2, sign);
1215 : 73792249 : break;
1216 : :
1217 : : default:
1218 : : return false;
1219 : : }
1220 : : return true;
1221 : 1200937501 : }
1222 : :
1223 : : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1224 : : min value to RES. */
1225 : : bool
1226 : 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1227 : : {
1228 : 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1229 : : {
1230 : 0 : res = wi::to_poly_wide (arg1);
1231 : 0 : return true;
1232 : : }
1233 : 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1234 : : {
1235 : 0 : res = wi::to_poly_wide (arg2);
1236 : 0 : return true;
1237 : : }
1238 : :
1239 : : return false;
1240 : : }
1241 : :
1242 : : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1243 : : produce a new constant in RES. Return FALSE if we don't know how
1244 : : to evaluate CODE at compile-time. */
1245 : :
1246 : : bool
1247 : 1200937501 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1248 : : const_tree arg1, const_tree arg2,
1249 : : signop sign, wi::overflow_type *overflow)
1250 : : {
1251 : 1200937501 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1252 : :
1253 : 1200937501 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1254 : : {
1255 : 1200937501 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1256 : 1200937501 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1257 : 1200937501 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1258 : : return NULL_TREE;
1259 : 1200899847 : res = wi_res;
1260 : 1200899847 : return true;
1261 : 1200937720 : }
1262 : :
1263 : : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1264 : :
1265 : : switch (code)
1266 : : {
1267 : : case PLUS_EXPR:
1268 : : res = wi::add (wi::to_poly_wide (arg1),
1269 : : wi::to_poly_wide (arg2), sign, overflow);
1270 : : break;
1271 : :
1272 : : case MINUS_EXPR:
1273 : : res = wi::sub (wi::to_poly_wide (arg1),
1274 : : wi::to_poly_wide (arg2), sign, overflow);
1275 : : break;
1276 : :
1277 : : case MULT_EXPR:
1278 : : if (TREE_CODE (arg2) == INTEGER_CST)
1279 : : res = wi::mul (wi::to_poly_wide (arg1),
1280 : : wi::to_wide (arg2), sign, overflow);
1281 : : else if (TREE_CODE (arg1) == INTEGER_CST)
1282 : : res = wi::mul (wi::to_poly_wide (arg2),
1283 : : wi::to_wide (arg1), sign, overflow);
1284 : : else
1285 : : return NULL_TREE;
1286 : : break;
1287 : :
1288 : : case LSHIFT_EXPR:
1289 : : if (TREE_CODE (arg2) == INTEGER_CST)
1290 : : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1291 : : else
1292 : : return false;
1293 : : break;
1294 : :
1295 : : case BIT_AND_EXPR:
1296 : : if (TREE_CODE (arg2) != INTEGER_CST
1297 : : || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1298 : : &res))
1299 : : return false;
1300 : : break;
1301 : :
1302 : : case BIT_IOR_EXPR:
1303 : : if (TREE_CODE (arg2) != INTEGER_CST
1304 : : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1305 : : &res))
1306 : : return false;
1307 : : break;
1308 : :
1309 : : case MIN_EXPR:
1310 : : if (!can_min_p (arg1, arg2, res))
1311 : : return false;
1312 : : break;
1313 : :
1314 : : default:
1315 : : return false;
1316 : : }
1317 : : return true;
1318 : : }
1319 : :
1320 : : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1321 : : produce a new constant. Return NULL_TREE if we don't know how to
1322 : : evaluate CODE at compile-time. */
1323 : :
1324 : : tree
1325 : 1200937501 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1326 : : int overflowable)
1327 : : {
1328 : 1200937501 : poly_wide_int poly_res;
1329 : 1200937501 : tree type = TREE_TYPE (arg1);
1330 : 1200937501 : signop sign = TYPE_SIGN (type);
1331 : 1200937501 : wi::overflow_type overflow = wi::OVF_NONE;
1332 : :
1333 : 1200937501 : if (!poly_int_tree_p (arg1)
1334 : 1200937501 : || !poly_int_tree_p (arg2)
1335 : 2401875002 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1336 : 37654 : return NULL_TREE;
1337 : 1200899847 : return force_fit_type (type, poly_res, overflowable,
1338 : 1200899847 : (((sign == SIGNED || overflowable == -1)
1339 : 1200899847 : && overflow)
1340 : 1200899847 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1341 : 1200937501 : }
1342 : :
1343 : : /* Return true if binary operation OP distributes over addition in operand
1344 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
1345 : :
1346 : : static bool
1347 : 174893 : distributes_over_addition_p (tree_code op, int opno)
1348 : : {
1349 : 0 : switch (op)
1350 : : {
1351 : : case PLUS_EXPR:
1352 : : case MINUS_EXPR:
1353 : : case MULT_EXPR:
1354 : : return true;
1355 : :
1356 : 0 : case LSHIFT_EXPR:
1357 : 0 : return opno == 1;
1358 : :
1359 : 3388 : default:
1360 : 3388 : return false;
1361 : : }
1362 : : }
1363 : :
1364 : : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1365 : : is the other operand. Try to use the value of OP to simplify the
1366 : : operation in one step, without having to process individual elements. */
1367 : : static tree
1368 : 422726 : simplify_const_binop (tree_code code, tree op, tree other_op,
1369 : : int index ATTRIBUTE_UNUSED)
1370 : : {
1371 : : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1372 : 422726 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1373 : : {
1374 : 338308 : if (integer_zerop (other_op))
1375 : : {
1376 : 23863 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1377 : : return op;
1378 : 23067 : else if (code == BIT_AND_EXPR)
1379 : : return other_op;
1380 : : }
1381 : : }
1382 : :
1383 : : return NULL_TREE;
1384 : : }
1385 : :
1386 : : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1387 : : be an elementwise vector operation, try to fold the operation to a
1388 : : constant vector, using ELT_CONST_BINOP to fold each element. Return
1389 : : the folded value on success, otherwise return null. */
1390 : : tree
1391 : 255304 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1392 : : tree (*elt_const_binop) (enum tree_code, tree, tree))
1393 : : {
1394 : 176998 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1395 : 426211 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1396 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1397 : : {
1398 : 170907 : tree type = TREE_TYPE (arg1);
1399 : 170907 : bool step_ok_p;
1400 : 170907 : if (VECTOR_CST_STEPPED_P (arg1)
1401 : 170907 : && VECTOR_CST_STEPPED_P (arg2))
1402 : : /* We can operate directly on the encoding if:
1403 : :
1404 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1405 : : implies
1406 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1407 : :
1408 : : Addition and subtraction are the supported operators
1409 : : for which this is true. */
1410 : 2105 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1411 : 168802 : else if (VECTOR_CST_STEPPED_P (arg1))
1412 : : /* We can operate directly on stepped encodings if:
1413 : :
1414 : : a3 - a2 == a2 - a1
1415 : : implies:
1416 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1417 : :
1418 : : which is true if (x -> x op c) distributes over addition. */
1419 : 49525 : step_ok_p = distributes_over_addition_p (code, 1);
1420 : : else
1421 : : /* Similarly in reverse. */
1422 : 119277 : step_ok_p = distributes_over_addition_p (code, 2);
1423 : 170907 : tree_vector_builder elts;
1424 : 170907 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1425 : : return NULL_TREE;
1426 : 170907 : unsigned int count = elts.encoded_nelts ();
1427 : 637609 : for (unsigned int i = 0; i < count; ++i)
1428 : : {
1429 : 467033 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1430 : 467033 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1431 : :
1432 : 467033 : tree elt = elt_const_binop (code, elem1, elem2);
1433 : :
1434 : : /* It is possible that const_binop cannot handle the given
1435 : : code and return NULL_TREE */
1436 : 467033 : if (elt == NULL_TREE)
1437 : 331 : return NULL_TREE;
1438 : 466702 : elts.quick_push (elt);
1439 : : }
1440 : :
1441 : 170576 : return elts.build ();
1442 : 170907 : }
1443 : :
1444 : 84397 : if (TREE_CODE (arg1) == VECTOR_CST
1445 : 6091 : && TREE_CODE (arg2) == INTEGER_CST)
1446 : : {
1447 : 6091 : tree type = TREE_TYPE (arg1);
1448 : 6091 : bool step_ok_p = distributes_over_addition_p (code, 1);
1449 : 6091 : tree_vector_builder elts;
1450 : 6091 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1451 : : return NULL_TREE;
1452 : 6091 : unsigned int count = elts.encoded_nelts ();
1453 : 30348 : for (unsigned int i = 0; i < count; ++i)
1454 : : {
1455 : 24344 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1456 : :
1457 : 24344 : tree elt = elt_const_binop (code, elem1, arg2);
1458 : :
1459 : : /* It is possible that const_binop cannot handle the given
1460 : : code and return NULL_TREE. */
1461 : 24344 : if (elt == NULL_TREE)
1462 : 87 : return NULL_TREE;
1463 : 24257 : elts.quick_push (elt);
1464 : : }
1465 : :
1466 : 6004 : return elts.build ();
1467 : 6091 : }
1468 : : return NULL_TREE;
1469 : : }
1470 : :
1471 : : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1472 : : constant. We assume ARG1 and ARG2 have the same data type, or at least
1473 : : are the same kind of constant and the same machine mode. Return zero if
1474 : : combining the constants is not allowed in the current operating mode. */
1475 : :
1476 : : static tree
1477 : 168977091 : const_binop (enum tree_code code, tree arg1, tree arg2)
1478 : : {
1479 : : /* Sanity check for the recursive cases. */
1480 : 168977091 : if (!arg1 || !arg2)
1481 : : return NULL_TREE;
1482 : :
1483 : 168975827 : STRIP_NOPS (arg1);
1484 : 168975827 : STRIP_NOPS (arg2);
1485 : :
1486 : 168975827 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1487 : : {
1488 : 163281670 : if (code == POINTER_PLUS_EXPR)
1489 : 102761 : return int_const_binop (PLUS_EXPR,
1490 : 205522 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1491 : :
1492 : 163178909 : return int_const_binop (code, arg1, arg2);
1493 : : }
1494 : :
1495 : 5694157 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1496 : : {
1497 : 5423303 : machine_mode mode;
1498 : 5423303 : REAL_VALUE_TYPE d1;
1499 : 5423303 : REAL_VALUE_TYPE d2;
1500 : 5423303 : REAL_VALUE_TYPE value;
1501 : 5423303 : REAL_VALUE_TYPE result;
1502 : 5423303 : bool inexact;
1503 : 5423303 : tree t, type;
1504 : :
1505 : : /* The following codes are handled by real_arithmetic. */
1506 : 5423303 : switch (code)
1507 : : {
1508 : 5423303 : case PLUS_EXPR:
1509 : 5423303 : case MINUS_EXPR:
1510 : 5423303 : case MULT_EXPR:
1511 : 5423303 : case RDIV_EXPR:
1512 : 5423303 : case MIN_EXPR:
1513 : 5423303 : case MAX_EXPR:
1514 : 5423303 : break;
1515 : :
1516 : : default:
1517 : : return NULL_TREE;
1518 : : }
1519 : :
1520 : 5423303 : d1 = TREE_REAL_CST (arg1);
1521 : 5423303 : d2 = TREE_REAL_CST (arg2);
1522 : :
1523 : 5423303 : type = TREE_TYPE (arg1);
1524 : 5423303 : mode = TYPE_MODE (type);
1525 : :
1526 : : /* Don't perform operation if we honor signaling NaNs and
1527 : : either operand is a signaling NaN. */
1528 : 5423303 : if (HONOR_SNANS (mode)
1529 : 5423303 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1530 : 3625 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1531 : 33 : return NULL_TREE;
1532 : :
1533 : : /* Don't perform operation if it would raise a division
1534 : : by zero exception. */
1535 : 5423270 : if (code == RDIV_EXPR
1536 : 2421330 : && real_equal (&d2, &dconst0)
1537 : 5433803 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1538 : 7245 : return NULL_TREE;
1539 : :
1540 : : /* If either operand is a NaN, just return it. Otherwise, set up
1541 : : for floating-point trap; we return an overflow. */
1542 : 5416025 : if (REAL_VALUE_ISNAN (d1))
1543 : : {
1544 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1545 : : is off. */
1546 : 239 : d1.signalling = 0;
1547 : 239 : t = build_real (type, d1);
1548 : 239 : return t;
1549 : : }
1550 : 5415786 : else if (REAL_VALUE_ISNAN (d2))
1551 : : {
1552 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1553 : : is off. */
1554 : 61 : d2.signalling = 0;
1555 : 61 : t = build_real (type, d2);
1556 : 61 : return t;
1557 : : }
1558 : :
1559 : 5415725 : inexact = real_arithmetic (&value, code, &d1, &d2);
1560 : 5415725 : real_convert (&result, mode, &value);
1561 : :
1562 : : /* Don't constant fold this floating point operation if
1563 : : both operands are not NaN but the result is NaN, and
1564 : : flag_trapping_math. Such operations should raise an
1565 : : invalid operation exception. */
1566 : 5415725 : if (flag_trapping_math
1567 : 21029910 : && MODE_HAS_NANS (mode)
1568 : 5399023 : && REAL_VALUE_ISNAN (result)
1569 : 2498 : && !REAL_VALUE_ISNAN (d1)
1570 : 5418223 : && !REAL_VALUE_ISNAN (d2))
1571 : 2498 : return NULL_TREE;
1572 : :
1573 : : /* Don't constant fold this floating point operation if
1574 : : the result has overflowed and flag_trapping_math. */
1575 : 5413227 : if (flag_trapping_math
1576 : 21020260 : && MODE_HAS_INFINITIES (mode)
1577 : 5396525 : && REAL_VALUE_ISINF (result)
1578 : 7497 : && !REAL_VALUE_ISINF (d1)
1579 : 5420141 : && !REAL_VALUE_ISINF (d2))
1580 : 4631 : return NULL_TREE;
1581 : :
1582 : : /* Don't constant fold this floating point operation if the
1583 : : result may dependent upon the run-time rounding mode and
1584 : : flag_rounding_math is set, or if GCC's software emulation
1585 : : is unable to accurately represent the result. */
1586 : 5408596 : if ((flag_rounding_math
1587 : 36714359 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1588 : 5408596 : && (inexact || !real_identical (&result, &value)))
1589 : 1107 : return NULL_TREE;
1590 : :
1591 : 5407489 : t = build_real (type, result);
1592 : :
1593 : 5407489 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1594 : 5407489 : return t;
1595 : : }
1596 : :
1597 : 270854 : if (TREE_CODE (arg1) == FIXED_CST)
1598 : : {
1599 : 0 : FIXED_VALUE_TYPE f1;
1600 : 0 : FIXED_VALUE_TYPE f2;
1601 : 0 : FIXED_VALUE_TYPE result;
1602 : 0 : tree t, type;
1603 : 0 : bool sat_p;
1604 : 0 : bool overflow_p;
1605 : :
1606 : : /* The following codes are handled by fixed_arithmetic. */
1607 : 0 : switch (code)
1608 : : {
1609 : 0 : case PLUS_EXPR:
1610 : 0 : case MINUS_EXPR:
1611 : 0 : case MULT_EXPR:
1612 : 0 : case TRUNC_DIV_EXPR:
1613 : 0 : if (TREE_CODE (arg2) != FIXED_CST)
1614 : : return NULL_TREE;
1615 : 0 : f2 = TREE_FIXED_CST (arg2);
1616 : 0 : break;
1617 : :
1618 : 0 : case LSHIFT_EXPR:
1619 : 0 : case RSHIFT_EXPR:
1620 : 0 : {
1621 : 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1622 : 0 : return NULL_TREE;
1623 : 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1624 : 0 : f2.data.high = w2.elt (1);
1625 : 0 : f2.data.low = w2.ulow ();
1626 : 0 : f2.mode = SImode;
1627 : : }
1628 : 0 : break;
1629 : :
1630 : : default:
1631 : : return NULL_TREE;
1632 : : }
1633 : :
1634 : 0 : f1 = TREE_FIXED_CST (arg1);
1635 : 0 : type = TREE_TYPE (arg1);
1636 : 0 : sat_p = TYPE_SATURATING (type);
1637 : 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1638 : 0 : t = build_fixed (type, result);
1639 : : /* Propagate overflow flags. */
1640 : 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1641 : 0 : TREE_OVERFLOW (t) = 1;
1642 : 0 : return t;
1643 : : }
1644 : :
1645 : 270854 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1646 : : {
1647 : 11239 : tree type = TREE_TYPE (arg1);
1648 : 11239 : tree r1 = TREE_REALPART (arg1);
1649 : 11239 : tree i1 = TREE_IMAGPART (arg1);
1650 : 11239 : tree r2 = TREE_REALPART (arg2);
1651 : 11239 : tree i2 = TREE_IMAGPART (arg2);
1652 : 11239 : tree real, imag;
1653 : :
1654 : 11239 : switch (code)
1655 : : {
1656 : 5355 : case PLUS_EXPR:
1657 : 5355 : case MINUS_EXPR:
1658 : 5355 : real = const_binop (code, r1, r2);
1659 : 5355 : imag = const_binop (code, i1, i2);
1660 : 5355 : break;
1661 : :
1662 : 3927 : case MULT_EXPR:
1663 : 3927 : if (COMPLEX_FLOAT_TYPE_P (type))
1664 : 2775 : return do_mpc_arg2 (arg1, arg2, type,
1665 : : /* do_nonfinite= */ folding_initializer,
1666 : 2775 : mpc_mul);
1667 : :
1668 : 1152 : real = const_binop (MINUS_EXPR,
1669 : : const_binop (MULT_EXPR, r1, r2),
1670 : : const_binop (MULT_EXPR, i1, i2));
1671 : 1152 : imag = const_binop (PLUS_EXPR,
1672 : : const_binop (MULT_EXPR, r1, i2),
1673 : : const_binop (MULT_EXPR, i1, r2));
1674 : 1152 : break;
1675 : :
1676 : 1703 : case RDIV_EXPR:
1677 : 1703 : if (COMPLEX_FLOAT_TYPE_P (type))
1678 : 1703 : return do_mpc_arg2 (arg1, arg2, type,
1679 : : /* do_nonfinite= */ folding_initializer,
1680 : 1703 : mpc_div);
1681 : : /* Fallthru. */
1682 : 254 : case TRUNC_DIV_EXPR:
1683 : 254 : case CEIL_DIV_EXPR:
1684 : 254 : case FLOOR_DIV_EXPR:
1685 : 254 : case ROUND_DIV_EXPR:
1686 : 254 : if (flag_complex_method == 0)
1687 : : {
1688 : : /* Keep this algorithm in sync with
1689 : : tree-complex.cc:expand_complex_div_straight().
1690 : :
1691 : : Expand complex division to scalars, straightforward algorithm.
1692 : : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1693 : : t = br*br + bi*bi
1694 : : */
1695 : 0 : tree magsquared
1696 : 0 : = const_binop (PLUS_EXPR,
1697 : : const_binop (MULT_EXPR, r2, r2),
1698 : : const_binop (MULT_EXPR, i2, i2));
1699 : 0 : tree t1
1700 : 0 : = const_binop (PLUS_EXPR,
1701 : : const_binop (MULT_EXPR, r1, r2),
1702 : : const_binop (MULT_EXPR, i1, i2));
1703 : 0 : tree t2
1704 : 0 : = const_binop (MINUS_EXPR,
1705 : : const_binop (MULT_EXPR, i1, r2),
1706 : : const_binop (MULT_EXPR, r1, i2));
1707 : :
1708 : 0 : real = const_binop (code, t1, magsquared);
1709 : 0 : imag = const_binop (code, t2, magsquared);
1710 : : }
1711 : : else
1712 : : {
1713 : : /* Keep this algorithm in sync with
1714 : : tree-complex.cc:expand_complex_div_wide().
1715 : :
1716 : : Expand complex division to scalars, modified algorithm to minimize
1717 : : overflow with wide input ranges. */
1718 : 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1719 : : fold_abs_const (r2, TREE_TYPE (type)),
1720 : : fold_abs_const (i2, TREE_TYPE (type)));
1721 : :
1722 : 254 : if (integer_nonzerop (compare))
1723 : : {
1724 : : /* In the TRUE branch, we compute
1725 : : ratio = br/bi;
1726 : : div = (br * ratio) + bi;
1727 : : tr = (ar * ratio) + ai;
1728 : : ti = (ai * ratio) - ar;
1729 : : tr = tr / div;
1730 : : ti = ti / div; */
1731 : 48 : tree ratio = const_binop (code, r2, i2);
1732 : 48 : tree div = const_binop (PLUS_EXPR, i2,
1733 : : const_binop (MULT_EXPR, r2, ratio));
1734 : 48 : real = const_binop (MULT_EXPR, r1, ratio);
1735 : 48 : real = const_binop (PLUS_EXPR, real, i1);
1736 : 48 : real = const_binop (code, real, div);
1737 : :
1738 : 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1739 : 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1740 : 48 : imag = const_binop (code, imag, div);
1741 : : }
1742 : : else
1743 : : {
1744 : : /* In the FALSE branch, we compute
1745 : : ratio = d/c;
1746 : : divisor = (d * ratio) + c;
1747 : : tr = (b * ratio) + a;
1748 : : ti = b - (a * ratio);
1749 : : tr = tr / div;
1750 : : ti = ti / div; */
1751 : 206 : tree ratio = const_binop (code, i2, r2);
1752 : 206 : tree div = const_binop (PLUS_EXPR, r2,
1753 : : const_binop (MULT_EXPR, i2, ratio));
1754 : :
1755 : 206 : real = const_binop (MULT_EXPR, i1, ratio);
1756 : 206 : real = const_binop (PLUS_EXPR, real, r1);
1757 : 206 : real = const_binop (code, real, div);
1758 : :
1759 : 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1760 : 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1761 : 206 : imag = const_binop (code, imag, div);
1762 : : }
1763 : : }
1764 : : break;
1765 : :
1766 : : default:
1767 : : return NULL_TREE;
1768 : : }
1769 : :
1770 : 6761 : if (real && imag)
1771 : 6603 : return build_complex (type, real, imag);
1772 : : }
1773 : :
1774 : 259773 : tree simplified;
1775 : 259773 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1776 : : return simplified;
1777 : :
1778 : 259516 : if (commutative_tree_code (code)
1779 : 259516 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1780 : : return simplified;
1781 : :
1782 : 255304 : return vector_const_binop (code, arg1, arg2, const_binop);
1783 : : }
1784 : :
1785 : : /* Overload that adds a TYPE parameter to be able to dispatch
1786 : : to fold_relational_const. */
1787 : :
1788 : : tree
1789 : 217683987 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1790 : : {
1791 : 217683987 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1792 : 55521770 : return fold_relational_const (code, type, arg1, arg2);
1793 : :
1794 : : /* ??? Until we make the const_binop worker take the type of the
1795 : : result as argument put those cases that need it here. */
1796 : 162162217 : switch (code)
1797 : : {
1798 : 18 : case VEC_SERIES_EXPR:
1799 : 18 : if (CONSTANT_CLASS_P (arg1)
1800 : 18 : && CONSTANT_CLASS_P (arg2))
1801 : 18 : return build_vec_series (type, arg1, arg2);
1802 : : return NULL_TREE;
1803 : :
1804 : 267669 : case COMPLEX_EXPR:
1805 : 267669 : if ((TREE_CODE (arg1) == REAL_CST
1806 : 257166 : && TREE_CODE (arg2) == REAL_CST)
1807 : 10504 : || (TREE_CODE (arg1) == INTEGER_CST
1808 : 10503 : && TREE_CODE (arg2) == INTEGER_CST))
1809 : 267668 : return build_complex (type, arg1, arg2);
1810 : : return NULL_TREE;
1811 : :
1812 : 10443 : case POINTER_DIFF_EXPR:
1813 : 10443 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1814 : : {
1815 : 19698 : poly_offset_int res = (wi::to_poly_offset (arg1)
1816 : 9849 : - wi::to_poly_offset (arg2));
1817 : 9849 : return force_fit_type (type, res, 1,
1818 : 9849 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1819 : : }
1820 : : return NULL_TREE;
1821 : :
1822 : 11936 : case VEC_PACK_TRUNC_EXPR:
1823 : 11936 : case VEC_PACK_FIX_TRUNC_EXPR:
1824 : 11936 : case VEC_PACK_FLOAT_EXPR:
1825 : 11936 : {
1826 : 11936 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1827 : :
1828 : 11936 : if (TREE_CODE (arg1) != VECTOR_CST
1829 : 11936 : || TREE_CODE (arg2) != VECTOR_CST)
1830 : : return NULL_TREE;
1831 : :
1832 : 11936 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1833 : : return NULL_TREE;
1834 : :
1835 : 11936 : out_nelts = in_nelts * 2;
1836 : 11936 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1837 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1838 : :
1839 : 11936 : tree_vector_builder elts (type, out_nelts, 1);
1840 : 147156 : for (i = 0; i < out_nelts; i++)
1841 : : {
1842 : 135232 : tree elt = (i < in_nelts
1843 : 135232 : ? VECTOR_CST_ELT (arg1, i)
1844 : 67610 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1845 : 136100 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1846 : : ? NOP_EXPR
1847 : : : code == VEC_PACK_FLOAT_EXPR
1848 : 868 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1849 : 135232 : TREE_TYPE (type), elt);
1850 : 135232 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1851 : 12 : return NULL_TREE;
1852 : 135220 : elts.quick_push (elt);
1853 : : }
1854 : :
1855 : 11924 : return elts.build ();
1856 : 11936 : }
1857 : :
1858 : 186 : case VEC_WIDEN_MULT_LO_EXPR:
1859 : 186 : case VEC_WIDEN_MULT_HI_EXPR:
1860 : 186 : case VEC_WIDEN_MULT_EVEN_EXPR:
1861 : 186 : case VEC_WIDEN_MULT_ODD_EXPR:
1862 : 186 : {
1863 : 186 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1864 : :
1865 : 186 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1866 : : return NULL_TREE;
1867 : :
1868 : 186 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1869 : : return NULL_TREE;
1870 : 186 : out_nelts = in_nelts / 2;
1871 : 186 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1872 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1873 : :
1874 : 186 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1875 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1876 : : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1877 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1878 : : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1879 : : scale = 1, ofs = 0;
1880 : : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1881 : 186 : scale = 1, ofs = 1;
1882 : :
1883 : 186 : tree_vector_builder elts (type, out_nelts, 1);
1884 : 646 : for (out = 0; out < out_nelts; out++)
1885 : : {
1886 : 460 : unsigned int in = (out << scale) + ofs;
1887 : 460 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1888 : : VECTOR_CST_ELT (arg1, in));
1889 : 460 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1890 : : VECTOR_CST_ELT (arg2, in));
1891 : :
1892 : 460 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1893 : 0 : return NULL_TREE;
1894 : 460 : tree elt = const_binop (MULT_EXPR, t1, t2);
1895 : 460 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1896 : : return NULL_TREE;
1897 : 460 : elts.quick_push (elt);
1898 : : }
1899 : :
1900 : 186 : return elts.build ();
1901 : 186 : }
1902 : :
1903 : 161871965 : default:;
1904 : : }
1905 : :
1906 : 161871965 : if (TREE_CODE_CLASS (code) != tcc_binary)
1907 : : return NULL_TREE;
1908 : :
1909 : : /* Make sure type and arg0 have the same saturating flag. */
1910 : 159205442 : gcc_checking_assert (TYPE_SATURATING (type)
1911 : : == TYPE_SATURATING (TREE_TYPE (arg1)));
1912 : :
1913 : 159205442 : return const_binop (code, arg1, arg2);
1914 : : }
1915 : :
1916 : : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1917 : : Return zero if computing the constants is not possible. */
1918 : :
1919 : : tree
1920 : 268573459 : const_unop (enum tree_code code, tree type, tree arg0)
1921 : : {
1922 : : /* Don't perform the operation, other than NEGATE and ABS, if
1923 : : flag_signaling_nans is on and the operand is a signaling NaN. */
1924 : 268573459 : if (TREE_CODE (arg0) == REAL_CST
1925 : 10667491 : && HONOR_SNANS (arg0)
1926 : 7345 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1927 : 454 : && code != NEGATE_EXPR
1928 : 454 : && code != ABS_EXPR
1929 : 268573878 : && code != ABSU_EXPR)
1930 : : return NULL_TREE;
1931 : :
1932 : 268573040 : switch (code)
1933 : : {
1934 : 187405952 : CASE_CONVERT:
1935 : 187405952 : case FLOAT_EXPR:
1936 : 187405952 : case FIX_TRUNC_EXPR:
1937 : 187405952 : case FIXED_CONVERT_EXPR:
1938 : 187405952 : return fold_convert_const (code, type, arg0);
1939 : :
1940 : 0 : case ADDR_SPACE_CONVERT_EXPR:
1941 : : /* If the source address is 0, and the source address space
1942 : : cannot have a valid object at 0, fold to dest type null. */
1943 : 0 : if (integer_zerop (arg0)
1944 : 0 : && !(targetm.addr_space.zero_address_valid
1945 : 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1946 : 0 : return fold_convert_const (code, type, arg0);
1947 : : break;
1948 : :
1949 : 11026180 : case VIEW_CONVERT_EXPR:
1950 : 11026180 : return fold_view_convert_expr (type, arg0);
1951 : :
1952 : 29248041 : case NEGATE_EXPR:
1953 : 29248041 : {
1954 : : /* Can't call fold_negate_const directly here as that doesn't
1955 : : handle all cases and we might not be able to negate some
1956 : : constants. */
1957 : 29248041 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1958 : 29248041 : if (tem && CONSTANT_CLASS_P (tem))
1959 : : return tem;
1960 : : break;
1961 : : }
1962 : :
1963 : 34290 : case ABS_EXPR:
1964 : 34290 : case ABSU_EXPR:
1965 : 34290 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1966 : 34007 : return fold_abs_const (arg0, type);
1967 : : break;
1968 : :
1969 : 24552 : case CONJ_EXPR:
1970 : 24552 : if (TREE_CODE (arg0) == COMPLEX_CST)
1971 : : {
1972 : 24549 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1973 : 24549 : TREE_TYPE (type));
1974 : 24549 : return build_complex (type, TREE_REALPART (arg0), ipart);
1975 : : }
1976 : : break;
1977 : :
1978 : 2291947 : case BIT_NOT_EXPR:
1979 : 2291947 : if (TREE_CODE (arg0) == INTEGER_CST)
1980 : 2290841 : return fold_not_const (arg0, type);
1981 : 1106 : else if (POLY_INT_CST_P (arg0))
1982 : : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1983 : : /* Perform BIT_NOT_EXPR on each element individually. */
1984 : 1106 : else if (TREE_CODE (arg0) == VECTOR_CST)
1985 : : {
1986 : 499 : tree elem;
1987 : :
1988 : : /* This can cope with stepped encodings because ~x == -1 - x. */
1989 : 499 : tree_vector_builder elements;
1990 : 499 : elements.new_unary_operation (type, arg0, true);
1991 : 499 : unsigned int i, count = elements.encoded_nelts ();
1992 : 2080 : for (i = 0; i < count; ++i)
1993 : : {
1994 : 1581 : elem = VECTOR_CST_ELT (arg0, i);
1995 : 1581 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1996 : 1581 : if (elem == NULL_TREE)
1997 : : break;
1998 : 1581 : elements.quick_push (elem);
1999 : : }
2000 : 499 : if (i == count)
2001 : 499 : return elements.build ();
2002 : 499 : }
2003 : : break;
2004 : :
2005 : 6833239 : case TRUTH_NOT_EXPR:
2006 : 6833239 : if (TREE_CODE (arg0) == INTEGER_CST)
2007 : 6516728 : return constant_boolean_node (integer_zerop (arg0), type);
2008 : : break;
2009 : :
2010 : 178995 : case REALPART_EXPR:
2011 : 178995 : if (TREE_CODE (arg0) == COMPLEX_CST)
2012 : 178794 : return fold_convert (type, TREE_REALPART (arg0));
2013 : : break;
2014 : :
2015 : 182853 : case IMAGPART_EXPR:
2016 : 182853 : if (TREE_CODE (arg0) == COMPLEX_CST)
2017 : 182665 : return fold_convert (type, TREE_IMAGPART (arg0));
2018 : : break;
2019 : :
2020 : 12274 : case VEC_UNPACK_LO_EXPR:
2021 : 12274 : case VEC_UNPACK_HI_EXPR:
2022 : 12274 : case VEC_UNPACK_FLOAT_LO_EXPR:
2023 : 12274 : case VEC_UNPACK_FLOAT_HI_EXPR:
2024 : 12274 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2025 : 12274 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2026 : 12274 : {
2027 : 12274 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2028 : 12274 : enum tree_code subcode;
2029 : :
2030 : 12274 : if (TREE_CODE (arg0) != VECTOR_CST)
2031 : : return NULL_TREE;
2032 : :
2033 : 12274 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2034 : : return NULL_TREE;
2035 : 12274 : out_nelts = in_nelts / 2;
2036 : 12274 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2037 : :
2038 : 12274 : unsigned int offset = 0;
2039 : 12274 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2040 : 12274 : || code == VEC_UNPACK_FLOAT_LO_EXPR
2041 : : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2042 : 6128 : offset = out_nelts;
2043 : :
2044 : 12274 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
2045 : : subcode = NOP_EXPR;
2046 : 7416 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
2047 : 7416 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
2048 : : subcode = FLOAT_EXPR;
2049 : : else
2050 : 4 : subcode = FIX_TRUNC_EXPR;
2051 : :
2052 : 12274 : tree_vector_builder elts (type, out_nelts, 1);
2053 : 58280 : for (i = 0; i < out_nelts; i++)
2054 : : {
2055 : 46006 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2056 : 46006 : VECTOR_CST_ELT (arg0, i + offset));
2057 : 46006 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2058 : 0 : return NULL_TREE;
2059 : 46006 : elts.quick_push (elt);
2060 : : }
2061 : :
2062 : 12274 : return elts.build ();
2063 : 12274 : }
2064 : :
2065 : 4 : case VEC_DUPLICATE_EXPR:
2066 : 4 : if (CONSTANT_CLASS_P (arg0))
2067 : 4 : return build_vector_from_val (type, arg0);
2068 : : return NULL_TREE;
2069 : :
2070 : : default:
2071 : : break;
2072 : : }
2073 : :
2074 : : return NULL_TREE;
2075 : : }
2076 : :
2077 : : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2078 : : indicates which particular sizetype to create. */
2079 : :
2080 : : tree
2081 : 3058743871 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2082 : : {
2083 : 3058743871 : return build_int_cst (sizetype_tab[(int) kind], number);
2084 : : }
2085 : :
2086 : : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2087 : : is a tree code. The type of the result is taken from the operands.
2088 : : Both must be equivalent integer types, ala int_binop_types_match_p.
2089 : : If the operands are constant, so is the result. */
2090 : :
2091 : : tree
2092 : 2076627125 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2093 : : {
2094 : 2076627125 : tree type = TREE_TYPE (arg0);
2095 : :
2096 : 2076627125 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2097 : : return error_mark_node;
2098 : :
2099 : 2076627125 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2100 : : TREE_TYPE (arg1)));
2101 : :
2102 : : /* Handle the special case of two poly_int constants faster. */
2103 : 2076627125 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2104 : : {
2105 : : /* And some specific cases even faster than that. */
2106 : 2044701215 : if (code == PLUS_EXPR)
2107 : : {
2108 : 920807721 : if (integer_zerop (arg0)
2109 : 920807721 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2110 : : return arg1;
2111 : 260693389 : if (integer_zerop (arg1)
2112 : 260693389 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2113 : : return arg0;
2114 : : }
2115 : 1123893494 : else if (code == MINUS_EXPR)
2116 : : {
2117 : 102783608 : if (integer_zerop (arg1)
2118 : 102783608 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2119 : : return arg0;
2120 : : }
2121 : 1021109886 : else if (code == MULT_EXPR)
2122 : : {
2123 : 456692378 : if (integer_onep (arg0)
2124 : 456692378 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2125 : : return arg1;
2126 : : }
2127 : :
2128 : : /* Handle general case of two integer constants. For sizetype
2129 : : constant calculations we always want to know about overflow,
2130 : : even in the unsigned case. */
2131 : 1021942740 : tree res = int_const_binop (code, arg0, arg1, -1);
2132 : 1021942740 : if (res != NULL_TREE)
2133 : : return res;
2134 : : }
2135 : :
2136 : 31925910 : return fold_build2_loc (loc, code, type, arg0, arg1);
2137 : : }
2138 : :
2139 : : /* Given two values, either both of sizetype or both of bitsizetype,
2140 : : compute the difference between the two values. Return the value
2141 : : in signed type corresponding to the type of the operands. */
2142 : :
2143 : : tree
2144 : 32638866 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2145 : : {
2146 : 32638866 : tree type = TREE_TYPE (arg0);
2147 : 32638866 : tree ctype;
2148 : :
2149 : 32638866 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2150 : : TREE_TYPE (arg1)));
2151 : :
2152 : : /* If the type is already signed, just do the simple thing. */
2153 : 32638866 : if (!TYPE_UNSIGNED (type))
2154 : 9525089 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2155 : :
2156 : 23113777 : if (type == sizetype)
2157 : 23113777 : ctype = ssizetype;
2158 : 0 : else if (type == bitsizetype)
2159 : 0 : ctype = sbitsizetype;
2160 : : else
2161 : 0 : ctype = signed_type_for (type);
2162 : :
2163 : : /* If either operand is not a constant, do the conversions to the signed
2164 : : type and subtract. The hardware will do the right thing with any
2165 : : overflow in the subtraction. */
2166 : 23113777 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2167 : 11793 : return size_binop_loc (loc, MINUS_EXPR,
2168 : : fold_convert_loc (loc, ctype, arg0),
2169 : 11793 : fold_convert_loc (loc, ctype, arg1));
2170 : :
2171 : : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2172 : : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2173 : : overflow) and negate (which can't either). Special-case a result
2174 : : of zero while we're here. */
2175 : 23101984 : if (tree_int_cst_equal (arg0, arg1))
2176 : 19862904 : return build_int_cst (ctype, 0);
2177 : 3239080 : else if (tree_int_cst_lt (arg1, arg0))
2178 : 2100236 : return fold_convert_loc (loc, ctype,
2179 : 2100236 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2180 : : else
2181 : 1138844 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2182 : : fold_convert_loc (loc, ctype,
2183 : : size_binop_loc (loc,
2184 : : MINUS_EXPR,
2185 : : arg1, arg0)));
2186 : : }
2187 : :
2188 : : /* A subroutine of fold_convert_const handling conversions of an
2189 : : INTEGER_CST to another integer type. */
2190 : :
2191 : : static tree
2192 : 1067409048 : fold_convert_const_int_from_int (tree type, const_tree arg1)
2193 : : {
2194 : : /* Given an integer constant, make new constant with new type,
2195 : : appropriately sign-extended or truncated. Use widest_int
2196 : : so that any extension is done according ARG1's type. */
2197 : 1067409048 : tree arg1_type = TREE_TYPE (arg1);
2198 : 1067409048 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2199 : 1067409048 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2200 : 1067409048 : TYPE_SIGN (arg1_type)),
2201 : 1067409048 : !POINTER_TYPE_P (TREE_TYPE (arg1)),
2202 : 1067409048 : TREE_OVERFLOW (arg1));
2203 : : }
2204 : :
2205 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2206 : : to an integer type. */
2207 : :
2208 : : static tree
2209 : 36073 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2210 : : {
2211 : 36073 : bool overflow = false;
2212 : 36073 : tree t;
2213 : :
2214 : : /* The following code implements the floating point to integer
2215 : : conversion rules required by the Java Language Specification,
2216 : : that IEEE NaNs are mapped to zero and values that overflow
2217 : : the target precision saturate, i.e. values greater than
2218 : : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2219 : : are mapped to INT_MIN. These semantics are allowed by the
2220 : : C and C++ standards that simply state that the behavior of
2221 : : FP-to-integer conversion is unspecified upon overflow. */
2222 : :
2223 : 36073 : wide_int val;
2224 : 36073 : REAL_VALUE_TYPE r;
2225 : 36073 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2226 : :
2227 : 36073 : switch (code)
2228 : : {
2229 : 36073 : case FIX_TRUNC_EXPR:
2230 : 36073 : real_trunc (&r, VOIDmode, &x);
2231 : 36073 : break;
2232 : :
2233 : 0 : default:
2234 : 0 : gcc_unreachable ();
2235 : : }
2236 : :
2237 : : /* If R is NaN, return zero and show we have an overflow. */
2238 : 36073 : if (REAL_VALUE_ISNAN (r))
2239 : : {
2240 : 3638 : overflow = true;
2241 : 3638 : val = wi::zero (TYPE_PRECISION (type));
2242 : : }
2243 : :
2244 : : /* See if R is less than the lower bound or greater than the
2245 : : upper bound. */
2246 : :
2247 : 36073 : if (! overflow)
2248 : : {
2249 : 32435 : tree lt = TYPE_MIN_VALUE (type);
2250 : 32435 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2251 : 32435 : if (real_less (&r, &l))
2252 : : {
2253 : 1974 : overflow = true;
2254 : 1974 : val = wi::to_wide (lt);
2255 : : }
2256 : : }
2257 : :
2258 : 36073 : if (! overflow)
2259 : : {
2260 : 30461 : tree ut = TYPE_MAX_VALUE (type);
2261 : 30461 : if (ut)
2262 : : {
2263 : 30461 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2264 : 30461 : if (real_less (&u, &r))
2265 : : {
2266 : 1921 : overflow = true;
2267 : 1921 : val = wi::to_wide (ut);
2268 : : }
2269 : : }
2270 : : }
2271 : :
2272 : 36073 : if (! overflow)
2273 : 28542 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2274 : :
2275 : : /* According to IEEE standard, for conversions from floating point to
2276 : : integer. When a NaN or infinite operand cannot be represented in the
2277 : : destination format and this cannot otherwise be indicated, the invalid
2278 : : operation exception shall be signaled. When a numeric operand would
2279 : : convert to an integer outside the range of the destination format, the
2280 : : invalid operation exception shall be signaled if this situation cannot
2281 : : otherwise be indicated. */
2282 : 36073 : if (!flag_trapping_math || !overflow)
2283 : 28796 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2284 : : else
2285 : : t = NULL_TREE;
2286 : :
2287 : 36073 : return t;
2288 : 36073 : }
2289 : :
2290 : : /* A subroutine of fold_convert_const handling conversions of a
2291 : : FIXED_CST to an integer type. */
2292 : :
2293 : : static tree
2294 : 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2295 : : {
2296 : 0 : tree t;
2297 : 0 : double_int temp, temp_trunc;
2298 : 0 : scalar_mode mode;
2299 : :
2300 : : /* Right shift FIXED_CST to temp by fbit. */
2301 : 0 : temp = TREE_FIXED_CST (arg1).data;
2302 : 0 : mode = TREE_FIXED_CST (arg1).mode;
2303 : 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2304 : : {
2305 : 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2306 : : HOST_BITS_PER_DOUBLE_INT,
2307 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2308 : :
2309 : : /* Left shift temp to temp_trunc by fbit. */
2310 : 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2311 : : HOST_BITS_PER_DOUBLE_INT,
2312 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2313 : : }
2314 : : else
2315 : : {
2316 : 0 : temp = double_int_zero;
2317 : 0 : temp_trunc = double_int_zero;
2318 : : }
2319 : :
2320 : : /* If FIXED_CST is negative, we need to round the value toward 0.
2321 : : By checking if the fractional bits are not zero to add 1 to temp. */
2322 : 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2323 : 0 : && temp_trunc.is_negative ()
2324 : 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2325 : 0 : temp += double_int_one;
2326 : :
2327 : : /* Given a fixed-point constant, make new constant with new type,
2328 : : appropriately sign-extended or truncated. */
2329 : 0 : t = force_fit_type (type, temp, -1,
2330 : 0 : (temp.is_negative ()
2331 : 0 : && (TYPE_UNSIGNED (type)
2332 : 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2333 : 0 : | TREE_OVERFLOW (arg1));
2334 : :
2335 : 0 : return t;
2336 : : }
2337 : :
2338 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2339 : : to another floating point type. */
2340 : :
2341 : : static tree
2342 : 1948508 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2343 : : {
2344 : 1948508 : REAL_VALUE_TYPE value;
2345 : 1948508 : tree t;
2346 : :
2347 : : /* If the underlying modes are the same, simply treat it as
2348 : : copy and rebuild with TREE_REAL_CST information and the
2349 : : given type. */
2350 : 1948508 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2351 : : {
2352 : 97584 : t = build_real (type, TREE_REAL_CST (arg1));
2353 : 97584 : return t;
2354 : : }
2355 : :
2356 : : /* Don't perform the operation if flag_signaling_nans is on
2357 : : and the operand is a signaling NaN. */
2358 : 1850924 : if (HONOR_SNANS (arg1)
2359 : 1851284 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2360 : : return NULL_TREE;
2361 : :
2362 : : /* With flag_rounding_math we should respect the current rounding mode
2363 : : unless the conversion is exact. */
2364 : 1850924 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2365 : 1851580 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2366 : 509 : return NULL_TREE;
2367 : :
2368 : 1850415 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2369 : 1850415 : t = build_real (type, value);
2370 : :
2371 : : /* If converting an infinity or NAN to a representation that doesn't
2372 : : have one, set the overflow bit so that we can produce some kind of
2373 : : error message at the appropriate point if necessary. It's not the
2374 : : most user-friendly message, but it's better than nothing. */
2375 : 1850415 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2376 : 1960472 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2377 : 0 : TREE_OVERFLOW (t) = 1;
2378 : 1850415 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2379 : 1955585 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2380 : 0 : TREE_OVERFLOW (t) = 1;
2381 : : /* Regular overflow, conversion produced an infinity in a mode that
2382 : : can't represent them. */
2383 : 9248662 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2384 : 0 : && REAL_VALUE_ISINF (value)
2385 : 1850415 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2386 : 0 : TREE_OVERFLOW (t) = 1;
2387 : : else
2388 : 1850415 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2389 : : return t;
2390 : : }
2391 : :
2392 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2393 : : to a floating point type. */
2394 : :
2395 : : static tree
2396 : 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2397 : : {
2398 : 0 : REAL_VALUE_TYPE value;
2399 : 0 : tree t;
2400 : :
2401 : 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2402 : 0 : &TREE_FIXED_CST (arg1));
2403 : 0 : t = build_real (type, value);
2404 : :
2405 : 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2406 : 0 : return t;
2407 : : }
2408 : :
2409 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2410 : : to another fixed-point type. */
2411 : :
2412 : : static tree
2413 : 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2414 : : {
2415 : 0 : FIXED_VALUE_TYPE value;
2416 : 0 : tree t;
2417 : 0 : bool overflow_p;
2418 : :
2419 : 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2420 : 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2421 : 0 : t = build_fixed (type, value);
2422 : :
2423 : : /* Propagate overflow flags. */
2424 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2425 : 0 : TREE_OVERFLOW (t) = 1;
2426 : 0 : return t;
2427 : : }
2428 : :
2429 : : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2430 : : to a fixed-point type. */
2431 : :
2432 : : static tree
2433 : 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2434 : : {
2435 : 0 : FIXED_VALUE_TYPE value;
2436 : 0 : tree t;
2437 : 0 : bool overflow_p;
2438 : 0 : double_int di;
2439 : :
2440 : 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2441 : :
2442 : 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2443 : 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2444 : 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2445 : : else
2446 : 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2447 : :
2448 : 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2449 : 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2450 : 0 : TYPE_SATURATING (type));
2451 : 0 : t = build_fixed (type, value);
2452 : :
2453 : : /* Propagate overflow flags. */
2454 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2455 : 0 : TREE_OVERFLOW (t) = 1;
2456 : 0 : return t;
2457 : : }
2458 : :
2459 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2460 : : to a fixed-point type. */
2461 : :
2462 : : static tree
2463 : 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2464 : : {
2465 : 0 : FIXED_VALUE_TYPE value;
2466 : 0 : tree t;
2467 : 0 : bool overflow_p;
2468 : :
2469 : 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2470 : 0 : &TREE_REAL_CST (arg1),
2471 : 0 : TYPE_SATURATING (type));
2472 : 0 : t = build_fixed (type, value);
2473 : :
2474 : : /* Propagate overflow flags. */
2475 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2476 : 0 : TREE_OVERFLOW (t) = 1;
2477 : 0 : return t;
2478 : : }
2479 : :
2480 : : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2481 : : type TYPE. If no simplification can be done return NULL_TREE. */
2482 : :
2483 : : static tree
2484 : 1125760313 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2485 : : {
2486 : 1125760313 : tree arg_type = TREE_TYPE (arg1);
2487 : 1125760313 : if (arg_type == type)
2488 : : return arg1;
2489 : :
2490 : : /* We can't widen types, since the runtime value could overflow the
2491 : : original type before being extended to the new type. */
2492 : 1115027395 : if (POLY_INT_CST_P (arg1)
2493 : : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2494 : : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2495 : : return build_poly_int_cst (type,
2496 : : poly_wide_int::from (poly_int_cst_value (arg1),
2497 : : TYPE_PRECISION (type),
2498 : : TYPE_SIGN (arg_type)));
2499 : :
2500 : 1115027395 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2501 : : || TREE_CODE (type) == OFFSET_TYPE)
2502 : : {
2503 : 1083893891 : if (TREE_CODE (arg1) == INTEGER_CST)
2504 : 1067409048 : return fold_convert_const_int_from_int (type, arg1);
2505 : 16484843 : else if (TREE_CODE (arg1) == REAL_CST)
2506 : 36073 : return fold_convert_const_int_from_real (code, type, arg1);
2507 : 16448770 : else if (TREE_CODE (arg1) == FIXED_CST)
2508 : 0 : return fold_convert_const_int_from_fixed (type, arg1);
2509 : : }
2510 : : else if (SCALAR_FLOAT_TYPE_P (type))
2511 : : {
2512 : 31078704 : if (TREE_CODE (arg1) == INTEGER_CST)
2513 : : {
2514 : 23925143 : tree res = build_real_from_int_cst (type, arg1);
2515 : : /* Avoid the folding if flag_rounding_math is on and the
2516 : : conversion is not exact. */
2517 : 23925143 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2518 : : {
2519 : 2878 : bool fail = false;
2520 : 5756 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2521 : 2878 : TYPE_PRECISION (TREE_TYPE (arg1)));
2522 : 2878 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2523 : 1722 : return NULL_TREE;
2524 : 2878 : }
2525 : 23923421 : return res;
2526 : : }
2527 : 7153561 : else if (TREE_CODE (arg1) == REAL_CST)
2528 : 1948508 : return fold_convert_const_real_from_real (type, arg1);
2529 : 5205053 : else if (TREE_CODE (arg1) == FIXED_CST)
2530 : 0 : return fold_convert_const_real_from_fixed (type, arg1);
2531 : : }
2532 : : else if (FIXED_POINT_TYPE_P (type))
2533 : : {
2534 : 0 : if (TREE_CODE (arg1) == FIXED_CST)
2535 : 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2536 : 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2537 : 0 : return fold_convert_const_fixed_from_int (type, arg1);
2538 : 0 : else if (TREE_CODE (arg1) == REAL_CST)
2539 : 0 : return fold_convert_const_fixed_from_real (type, arg1);
2540 : : }
2541 : : else if (VECTOR_TYPE_P (type))
2542 : : {
2543 : 7155 : if (TREE_CODE (arg1) == VECTOR_CST
2544 : 7155 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2545 : : {
2546 : 7155 : tree elttype = TREE_TYPE (type);
2547 : 7155 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2548 : : /* We can't handle steps directly when extending, since the
2549 : : values need to wrap at the original precision first. */
2550 : 7155 : bool step_ok_p
2551 : 7155 : = (INTEGRAL_TYPE_P (elttype)
2552 : 284 : && INTEGRAL_TYPE_P (arg1_elttype)
2553 : 7381 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2554 : 7155 : tree_vector_builder v;
2555 : 7155 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2556 : : return NULL_TREE;
2557 : 7155 : unsigned int len = v.encoded_nelts ();
2558 : 40408 : for (unsigned int i = 0; i < len; ++i)
2559 : : {
2560 : 33253 : tree elt = VECTOR_CST_ELT (arg1, i);
2561 : 33253 : tree cvt = fold_convert_const (code, elttype, elt);
2562 : 33253 : if (cvt == NULL_TREE)
2563 : 0 : return NULL_TREE;
2564 : 33253 : v.quick_push (cvt);
2565 : : }
2566 : 7155 : return v.build ();
2567 : 7155 : }
2568 : : }
2569 : : return NULL_TREE;
2570 : : }
2571 : :
2572 : : /* Construct a vector of zero elements of vector type TYPE. */
2573 : :
2574 : : static tree
2575 : 16747 : build_zero_vector (tree type)
2576 : : {
2577 : 16747 : tree t;
2578 : :
2579 : 16747 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2580 : 16747 : return build_vector_from_val (type, t);
2581 : : }
2582 : :
2583 : : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2584 : :
2585 : : bool
2586 : 2289 : fold_convertible_p (const_tree type, const_tree arg)
2587 : : {
2588 : 2289 : const_tree orig = TREE_TYPE (arg);
2589 : :
2590 : 2289 : if (type == orig)
2591 : : return true;
2592 : :
2593 : 2289 : if (TREE_CODE (arg) == ERROR_MARK
2594 : 2289 : || TREE_CODE (type) == ERROR_MARK
2595 : 2289 : || TREE_CODE (orig) == ERROR_MARK)
2596 : : return false;
2597 : :
2598 : 2289 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2599 : : return true;
2600 : :
2601 : 2289 : switch (TREE_CODE (type))
2602 : : {
2603 : 865 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2604 : 865 : case POINTER_TYPE: case REFERENCE_TYPE:
2605 : 865 : case OFFSET_TYPE:
2606 : 865 : return (INTEGRAL_TYPE_P (orig)
2607 : 240 : || (POINTER_TYPE_P (orig)
2608 : 105 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2609 : 1000 : || TREE_CODE (orig) == OFFSET_TYPE);
2610 : :
2611 : 42 : case REAL_TYPE:
2612 : 42 : case FIXED_POINT_TYPE:
2613 : 42 : case VOID_TYPE:
2614 : 42 : return TREE_CODE (type) == TREE_CODE (orig);
2615 : :
2616 : 201 : case VECTOR_TYPE:
2617 : 201 : return (VECTOR_TYPE_P (orig)
2618 : 306 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2619 : : TYPE_VECTOR_SUBPARTS (orig))
2620 : 210 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2621 : :
2622 : : default:
2623 : : return false;
2624 : : }
2625 : : }
2626 : :
2627 : : /* Convert expression ARG to type TYPE. Used by the middle-end for
2628 : : simple conversions in preference to calling the front-end's convert. */
2629 : :
2630 : : tree
2631 : 1789422153 : fold_convert_loc (location_t loc, tree type, tree arg)
2632 : : {
2633 : 1789422153 : tree orig = TREE_TYPE (arg);
2634 : 1789422153 : tree tem;
2635 : :
2636 : 1789422153 : if (type == orig)
2637 : : return arg;
2638 : :
2639 : 1142037314 : if (TREE_CODE (arg) == ERROR_MARK
2640 : 1142036303 : || TREE_CODE (type) == ERROR_MARK
2641 : 1142036302 : || TREE_CODE (orig) == ERROR_MARK)
2642 : 1012 : return error_mark_node;
2643 : :
2644 : 1142036302 : switch (TREE_CODE (type))
2645 : : {
2646 : 54607229 : case POINTER_TYPE:
2647 : 54607229 : case REFERENCE_TYPE:
2648 : : /* Handle conversions between pointers to different address spaces. */
2649 : 54607229 : if (POINTER_TYPE_P (orig)
2650 : 54607229 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2651 : 44402986 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2652 : 125 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2653 : : /* fall through */
2654 : :
2655 : 1111378720 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2656 : 1111378720 : case OFFSET_TYPE: case BITINT_TYPE:
2657 : 1111378720 : if (TREE_CODE (arg) == INTEGER_CST)
2658 : : {
2659 : 937947659 : tem = fold_convert_const (NOP_EXPR, type, arg);
2660 : 937947659 : if (tem != NULL_TREE)
2661 : : return tem;
2662 : : }
2663 : 173431061 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2664 : 3028 : || TREE_CODE (orig) == OFFSET_TYPE)
2665 : 173431061 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2666 : 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2667 : 0 : return fold_convert_loc (loc, type,
2668 : : fold_build1_loc (loc, REALPART_EXPR,
2669 : 0 : TREE_TYPE (orig), arg));
2670 : 0 : gcc_assert (VECTOR_TYPE_P (orig)
2671 : : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2672 : 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2673 : :
2674 : 537814 : case REAL_TYPE:
2675 : 537814 : if (TREE_CODE (arg) == INTEGER_CST)
2676 : : {
2677 : 57255 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2678 : 57255 : if (tem != NULL_TREE)
2679 : : return tem;
2680 : : }
2681 : 480559 : else if (TREE_CODE (arg) == REAL_CST)
2682 : : {
2683 : 117289 : tem = fold_convert_const (NOP_EXPR, type, arg);
2684 : 117289 : if (tem != NULL_TREE)
2685 : : return tem;
2686 : : }
2687 : 363270 : else if (TREE_CODE (arg) == FIXED_CST)
2688 : : {
2689 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2690 : 0 : if (tem != NULL_TREE)
2691 : : return tem;
2692 : : }
2693 : :
2694 : 363272 : switch (TREE_CODE (orig))
2695 : : {
2696 : 642 : case INTEGER_TYPE: case BITINT_TYPE:
2697 : 642 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2698 : 642 : case POINTER_TYPE: case REFERENCE_TYPE:
2699 : 642 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2700 : :
2701 : 362630 : case REAL_TYPE:
2702 : 362630 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2703 : :
2704 : 0 : case FIXED_POINT_TYPE:
2705 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2706 : :
2707 : 0 : case COMPLEX_TYPE:
2708 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2709 : 0 : return fold_convert_loc (loc, type, tem);
2710 : :
2711 : 0 : default:
2712 : 0 : gcc_unreachable ();
2713 : : }
2714 : :
2715 : 0 : case FIXED_POINT_TYPE:
2716 : 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2717 : 0 : || TREE_CODE (arg) == REAL_CST)
2718 : : {
2719 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2720 : 0 : if (tem != NULL_TREE)
2721 : 0 : goto fold_convert_exit;
2722 : : }
2723 : :
2724 : 0 : switch (TREE_CODE (orig))
2725 : : {
2726 : 0 : case FIXED_POINT_TYPE:
2727 : 0 : case INTEGER_TYPE:
2728 : 0 : case ENUMERAL_TYPE:
2729 : 0 : case BOOLEAN_TYPE:
2730 : 0 : case REAL_TYPE:
2731 : 0 : case BITINT_TYPE:
2732 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2733 : :
2734 : 0 : case COMPLEX_TYPE:
2735 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2736 : 0 : return fold_convert_loc (loc, type, tem);
2737 : :
2738 : 0 : default:
2739 : 0 : gcc_unreachable ();
2740 : : }
2741 : :
2742 : 2254 : case COMPLEX_TYPE:
2743 : 2254 : switch (TREE_CODE (orig))
2744 : : {
2745 : 584 : case INTEGER_TYPE: case BITINT_TYPE:
2746 : 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2747 : 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2748 : 584 : case REAL_TYPE:
2749 : 584 : case FIXED_POINT_TYPE:
2750 : 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2751 : 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2752 : 584 : fold_convert_loc (loc, TREE_TYPE (type),
2753 : 584 : integer_zero_node));
2754 : 1670 : case COMPLEX_TYPE:
2755 : 1670 : {
2756 : 1670 : tree rpart, ipart;
2757 : :
2758 : 1670 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2759 : : {
2760 : 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2761 : 1534 : TREE_OPERAND (arg, 0));
2762 : 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2763 : 1534 : TREE_OPERAND (arg, 1));
2764 : 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2765 : : }
2766 : :
2767 : 136 : arg = save_expr (arg);
2768 : 136 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2769 : 136 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2770 : 136 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2771 : 136 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2772 : 136 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2773 : : }
2774 : :
2775 : 0 : default:
2776 : 0 : gcc_unreachable ();
2777 : : }
2778 : :
2779 : 30004022 : case VECTOR_TYPE:
2780 : 30004022 : if (integer_zerop (arg))
2781 : 16747 : return build_zero_vector (type);
2782 : 29987275 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2783 : 29987275 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2784 : : || VECTOR_TYPE_P (orig));
2785 : 29987275 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2786 : :
2787 : 110100 : case VOID_TYPE:
2788 : 110100 : tem = fold_ignored_result (arg);
2789 : 110100 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2790 : :
2791 : 3267 : default:
2792 : 3267 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2793 : 3267 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2794 : 0 : gcc_unreachable ();
2795 : : }
2796 : 0 : fold_convert_exit:
2797 : 0 : tem = protected_set_expr_location_unshare (tem, loc);
2798 : 0 : return tem;
2799 : : }
2800 : :
2801 : : /* Return false if expr can be assumed not to be an lvalue, true
2802 : : otherwise. */
2803 : :
2804 : : static bool
2805 : 47808796 : maybe_lvalue_p (const_tree x)
2806 : : {
2807 : : /* We only need to wrap lvalue tree codes. */
2808 : 47808796 : switch (TREE_CODE (x))
2809 : : {
2810 : : case VAR_DECL:
2811 : : case PARM_DECL:
2812 : : case RESULT_DECL:
2813 : : case LABEL_DECL:
2814 : : case FUNCTION_DECL:
2815 : : case SSA_NAME:
2816 : : case COMPOUND_LITERAL_EXPR:
2817 : :
2818 : : case COMPONENT_REF:
2819 : : case MEM_REF:
2820 : : case INDIRECT_REF:
2821 : : case ARRAY_REF:
2822 : : case ARRAY_RANGE_REF:
2823 : : case BIT_FIELD_REF:
2824 : : case OBJ_TYPE_REF:
2825 : :
2826 : : case REALPART_EXPR:
2827 : : case IMAGPART_EXPR:
2828 : : case PREINCREMENT_EXPR:
2829 : : case PREDECREMENT_EXPR:
2830 : : case SAVE_EXPR:
2831 : : case TRY_CATCH_EXPR:
2832 : : case WITH_CLEANUP_EXPR:
2833 : : case COMPOUND_EXPR:
2834 : : case MODIFY_EXPR:
2835 : : case TARGET_EXPR:
2836 : : case COND_EXPR:
2837 : : case BIND_EXPR:
2838 : : case VIEW_CONVERT_EXPR:
2839 : : break;
2840 : :
2841 : 35967230 : default:
2842 : : /* Assume the worst for front-end tree codes. */
2843 : 35967230 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2844 : : break;
2845 : : return false;
2846 : : }
2847 : :
2848 : 11898167 : return true;
2849 : : }
2850 : :
2851 : : /* Return an expr equal to X but certainly not valid as an lvalue. */
2852 : :
2853 : : tree
2854 : 44665868 : non_lvalue_loc (location_t loc, tree x)
2855 : : {
2856 : : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2857 : : us. */
2858 : 44665868 : if (in_gimple_form)
2859 : : return x;
2860 : :
2861 : 9373565 : if (! maybe_lvalue_p (x))
2862 : : return x;
2863 : 2021496 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2864 : : }
2865 : :
2866 : : /* Given a tree comparison code, return the code that is the logical inverse.
2867 : : It is generally not safe to do this for floating-point comparisons, except
2868 : : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2869 : : ERROR_MARK in this case. */
2870 : :
2871 : : enum tree_code
2872 : 115282006 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2873 : : {
2874 : 115282006 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2875 : 1076662 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2876 : : return ERROR_MARK;
2877 : :
2878 : 114469164 : switch (code)
2879 : : {
2880 : : case EQ_EXPR:
2881 : : return NE_EXPR;
2882 : 51165179 : case NE_EXPR:
2883 : 51165179 : return EQ_EXPR;
2884 : 11366810 : case GT_EXPR:
2885 : 11366810 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2886 : 12818685 : case GE_EXPR:
2887 : 12818685 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2888 : 7137268 : case LT_EXPR:
2889 : 7137268 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2890 : 7438401 : case LE_EXPR:
2891 : 7438401 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2892 : 255 : case LTGT_EXPR:
2893 : 255 : return UNEQ_EXPR;
2894 : 303 : case UNEQ_EXPR:
2895 : 303 : return LTGT_EXPR;
2896 : : case UNGT_EXPR:
2897 : : return LE_EXPR;
2898 : : case UNGE_EXPR:
2899 : : return LT_EXPR;
2900 : : case UNLT_EXPR:
2901 : : return GE_EXPR;
2902 : : case UNLE_EXPR:
2903 : : return GT_EXPR;
2904 : 237708 : case ORDERED_EXPR:
2905 : 237708 : return UNORDERED_EXPR;
2906 : 53702 : case UNORDERED_EXPR:
2907 : 53702 : return ORDERED_EXPR;
2908 : 0 : default:
2909 : 0 : gcc_unreachable ();
2910 : : }
2911 : : }
2912 : :
2913 : : /* Similar, but return the comparison that results if the operands are
2914 : : swapped. This is safe for floating-point. */
2915 : :
2916 : : enum tree_code
2917 : 146051511 : swap_tree_comparison (enum tree_code code)
2918 : : {
2919 : 146051511 : switch (code)
2920 : : {
2921 : : case EQ_EXPR:
2922 : : case NE_EXPR:
2923 : : case ORDERED_EXPR:
2924 : : case UNORDERED_EXPR:
2925 : : case LTGT_EXPR:
2926 : : case UNEQ_EXPR:
2927 : : return code;
2928 : 34220987 : case GT_EXPR:
2929 : 34220987 : return LT_EXPR;
2930 : 10396260 : case GE_EXPR:
2931 : 10396260 : return LE_EXPR;
2932 : 20303833 : case LT_EXPR:
2933 : 20303833 : return GT_EXPR;
2934 : 14510061 : case LE_EXPR:
2935 : 14510061 : return GE_EXPR;
2936 : 270437 : case UNGT_EXPR:
2937 : 270437 : return UNLT_EXPR;
2938 : 19551 : case UNGE_EXPR:
2939 : 19551 : return UNLE_EXPR;
2940 : 404290 : case UNLT_EXPR:
2941 : 404290 : return UNGT_EXPR;
2942 : 131755 : case UNLE_EXPR:
2943 : 131755 : return UNGE_EXPR;
2944 : 0 : default:
2945 : 0 : gcc_unreachable ();
2946 : : }
2947 : : }
2948 : :
2949 : :
2950 : : /* Convert a comparison tree code from an enum tree_code representation
2951 : : into a compcode bit-based encoding. This function is the inverse of
2952 : : compcode_to_comparison. */
2953 : :
2954 : : static enum comparison_code
2955 : 54296 : comparison_to_compcode (enum tree_code code)
2956 : : {
2957 : 54296 : switch (code)
2958 : : {
2959 : : case LT_EXPR:
2960 : : return COMPCODE_LT;
2961 : : case EQ_EXPR:
2962 : : return COMPCODE_EQ;
2963 : : case LE_EXPR:
2964 : : return COMPCODE_LE;
2965 : : case GT_EXPR:
2966 : : return COMPCODE_GT;
2967 : : case NE_EXPR:
2968 : : return COMPCODE_NE;
2969 : : case GE_EXPR:
2970 : : return COMPCODE_GE;
2971 : : case ORDERED_EXPR:
2972 : : return COMPCODE_ORD;
2973 : : case UNORDERED_EXPR:
2974 : : return COMPCODE_UNORD;
2975 : : case UNLT_EXPR:
2976 : : return COMPCODE_UNLT;
2977 : : case UNEQ_EXPR:
2978 : : return COMPCODE_UNEQ;
2979 : : case UNLE_EXPR:
2980 : : return COMPCODE_UNLE;
2981 : : case UNGT_EXPR:
2982 : : return COMPCODE_UNGT;
2983 : : case LTGT_EXPR:
2984 : : return COMPCODE_LTGT;
2985 : : case UNGE_EXPR:
2986 : : return COMPCODE_UNGE;
2987 : 0 : default:
2988 : 0 : gcc_unreachable ();
2989 : : }
2990 : : }
2991 : :
2992 : : /* Convert a compcode bit-based encoding of a comparison operator back
2993 : : to GCC's enum tree_code representation. This function is the
2994 : : inverse of comparison_to_compcode. */
2995 : :
2996 : : static enum tree_code
2997 : 13401 : compcode_to_comparison (enum comparison_code code)
2998 : : {
2999 : 13401 : switch (code)
3000 : : {
3001 : : case COMPCODE_LT:
3002 : : return LT_EXPR;
3003 : : case COMPCODE_EQ:
3004 : : return EQ_EXPR;
3005 : : case COMPCODE_LE:
3006 : : return LE_EXPR;
3007 : : case COMPCODE_GT:
3008 : : return GT_EXPR;
3009 : : case COMPCODE_NE:
3010 : : return NE_EXPR;
3011 : : case COMPCODE_GE:
3012 : : return GE_EXPR;
3013 : : case COMPCODE_ORD:
3014 : : return ORDERED_EXPR;
3015 : : case COMPCODE_UNORD:
3016 : : return UNORDERED_EXPR;
3017 : : case COMPCODE_UNLT:
3018 : : return UNLT_EXPR;
3019 : : case COMPCODE_UNEQ:
3020 : : return UNEQ_EXPR;
3021 : : case COMPCODE_UNLE:
3022 : : return UNLE_EXPR;
3023 : : case COMPCODE_UNGT:
3024 : : return UNGT_EXPR;
3025 : : case COMPCODE_LTGT:
3026 : : return LTGT_EXPR;
3027 : : case COMPCODE_UNGE:
3028 : : return UNGE_EXPR;
3029 : 0 : default:
3030 : 0 : gcc_unreachable ();
3031 : : }
3032 : : }
3033 : :
3034 : : /* Return true if COND1 tests the opposite condition of COND2. */
3035 : :
3036 : : bool
3037 : 1327547 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3038 : : {
3039 : 1327547 : return (COMPARISON_CLASS_P (cond1)
3040 : 1238963 : && COMPARISON_CLASS_P (cond2)
3041 : 1234708 : && (invert_tree_comparison
3042 : 1234708 : (TREE_CODE (cond1),
3043 : 2469416 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3044 : 71815 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3045 : 71815 : TREE_OPERAND (cond2, 0), 0)
3046 : 1350485 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3047 : 22938 : TREE_OPERAND (cond2, 1), 0));
3048 : : }
3049 : :
3050 : : /* Return a tree for the comparison which is the combination of
3051 : : doing the AND or OR (depending on CODE) of the two operations LCODE
3052 : : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3053 : : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3054 : : if this makes the transformation invalid. */
3055 : :
3056 : : tree
3057 : 27148 : combine_comparisons (location_t loc,
3058 : : enum tree_code code, enum tree_code lcode,
3059 : : enum tree_code rcode, tree truth_type,
3060 : : tree ll_arg, tree lr_arg)
3061 : : {
3062 : 27148 : bool honor_nans = HONOR_NANS (ll_arg);
3063 : 27148 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3064 : 27148 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3065 : 27148 : int compcode;
3066 : :
3067 : 27148 : switch (code)
3068 : : {
3069 : 17786 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3070 : 17786 : compcode = lcompcode & rcompcode;
3071 : 17786 : break;
3072 : :
3073 : 9362 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3074 : 9362 : compcode = lcompcode | rcompcode;
3075 : 9362 : break;
3076 : :
3077 : : default:
3078 : : return NULL_TREE;
3079 : : }
3080 : :
3081 : 27148 : if (!honor_nans)
3082 : : {
3083 : : /* Eliminate unordered comparisons, as well as LTGT and ORD
3084 : : which are not used unless the mode has NaNs. */
3085 : 22073 : compcode &= ~COMPCODE_UNORD;
3086 : 22073 : if (compcode == COMPCODE_LTGT)
3087 : : compcode = COMPCODE_NE;
3088 : 21091 : else if (compcode == COMPCODE_ORD)
3089 : : compcode = COMPCODE_TRUE;
3090 : : }
3091 : 5075 : else if (flag_trapping_math)
3092 : : {
3093 : : /* Check that the original operation and the optimized ones will trap
3094 : : under the same condition. */
3095 : 8308 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3096 : 3518 : && (lcompcode != COMPCODE_EQ)
3097 : 4154 : && (lcompcode != COMPCODE_ORD);
3098 : 8308 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3099 : 3666 : && (rcompcode != COMPCODE_EQ)
3100 : 4154 : && (rcompcode != COMPCODE_ORD);
3101 : 8308 : bool trap = (compcode & COMPCODE_UNORD) == 0
3102 : 3731 : && (compcode != COMPCODE_EQ)
3103 : 4154 : && (compcode != COMPCODE_ORD);
3104 : :
3105 : : /* In a short-circuited boolean expression the LHS might be
3106 : : such that the RHS, if evaluated, will never trap. For
3107 : : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3108 : : if neither x nor y is NaN. (This is a mixed blessing: for
3109 : : example, the expression above will never trap, hence
3110 : : optimizing it to x < y would be invalid). */
3111 : 4154 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3112 : 3753 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3113 : 4154 : rtrap = false;
3114 : :
3115 : : /* If the comparison was short-circuited, and only the RHS
3116 : : trapped, we may now generate a spurious trap. */
3117 : 4154 : if (rtrap && !ltrap
3118 : 118 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3119 : : return NULL_TREE;
3120 : :
3121 : : /* If we changed the conditions that cause a trap, we lose. */
3122 : 4036 : if ((ltrap || rtrap) != trap)
3123 : : return NULL_TREE;
3124 : : }
3125 : :
3126 : 1642 : if (compcode == COMPCODE_TRUE)
3127 : 1223 : return constant_boolean_node (true, truth_type);
3128 : 22492 : else if (compcode == COMPCODE_FALSE)
3129 : 9091 : return constant_boolean_node (false, truth_type);
3130 : : else
3131 : : {
3132 : 13401 : enum tree_code tcode;
3133 : :
3134 : 13401 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3135 : 13401 : return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3136 : : }
3137 : : }
3138 : :
3139 : : /* Return nonzero if two operands (typically of the same tree node)
3140 : : are necessarily equal. FLAGS modifies behavior as follows:
3141 : :
3142 : : If OEP_ONLY_CONST is set, only return nonzero for constants.
3143 : : This function tests whether the operands are indistinguishable;
3144 : : it does not test whether they are equal using C's == operation.
3145 : : The distinction is important for IEEE floating point, because
3146 : : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3147 : : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3148 : :
3149 : : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3150 : : even though it may hold multiple values during a function.
3151 : : This is because a GCC tree node guarantees that nothing else is
3152 : : executed between the evaluation of its "operands" (which may often
3153 : : be evaluated in arbitrary order). Hence if the operands themselves
3154 : : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3155 : : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3156 : : unset means assuming isochronic (or instantaneous) tree equivalence.
3157 : : Unless comparing arbitrary expression trees, such as from different
3158 : : statements, this flag can usually be left unset.
3159 : :
3160 : : If OEP_PURE_SAME is set, then pure functions with identical arguments
3161 : : are considered the same. It is used when the caller has other ways
3162 : : to ensure that global memory is unchanged in between.
3163 : :
3164 : : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3165 : : not values of expressions.
3166 : :
3167 : : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3168 : : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3169 : :
3170 : : If OEP_BITWISE is set, then require the values to be bitwise identical
3171 : : rather than simply numerically equal. Do not take advantage of things
3172 : : like math-related flags or undefined behavior; only return true for
3173 : : values that are provably bitwise identical in all circumstances.
3174 : :
3175 : : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3176 : : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3177 : : rather than just numerically equivalent. The compared expressions must
3178 : : however perform the same operations but may do intermediate computations in
3179 : : differing signs. Because this comparison ignores any possible UB it cannot
3180 : : be used blindly without ensuring that the context you are using it in itself
3181 : : doesn't guarantee that there will be no UB. Conditional expressions are
3182 : : excluded from this relaxation.
3183 : :
3184 : : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3185 : : differing hashes even for cases where operand_compare::operand_equal_p
3186 : : compares equal.
3187 : :
3188 : : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3189 : : any operand with side effect. This is unnecesarily conservative in the
3190 : : case we know that arg0 and arg1 are in disjoint code paths (such as in
3191 : : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3192 : : addresses with TREE_CONSTANT flag set so we know that &var == &var
3193 : : even if var is volatile. */
3194 : :
3195 : : bool
3196 : 6881592155 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3197 : : unsigned int flags)
3198 : : {
3199 : 6881592155 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3200 : : }
3201 : :
3202 : : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3203 : : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3204 : : expression is being compared under for equality. This means that they can
3205 : : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3206 : :
3207 : : bool
3208 : 6882320498 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3209 : : tree type1, const_tree arg1,
3210 : : unsigned int flags)
3211 : : {
3212 : 6882320498 : bool r;
3213 : 6882320498 : if (verify_hash_value (arg0, arg1, flags, &r))
3214 : 2889987167 : return r;
3215 : :
3216 : 3992333331 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3217 : 3992333331 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3218 : :
3219 : : /* If either is ERROR_MARK, they aren't equal. */
3220 : 3992333331 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3221 : 3992332548 : || type0 == error_mark_node
3222 : 3992332546 : || type1 == error_mark_node)
3223 : : return false;
3224 : :
3225 : : /* Similar, if either does not have a type (like a template id),
3226 : : they aren't equal. */
3227 : 3992332545 : if (!type0 || !type1)
3228 : : return false;
3229 : :
3230 : : /* Bitwise identity makes no sense if the values have different layouts. */
3231 : 3992330041 : if ((flags & OEP_BITWISE)
3232 : 3992330041 : && !tree_nop_conversion_p (type0, type1))
3233 : : return false;
3234 : :
3235 : : /* We cannot consider pointers to different address space equal. */
3236 : 3992330041 : if (POINTER_TYPE_P (type0)
3237 : 581536426 : && POINTER_TYPE_P (type1)
3238 : 4484287304 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3239 : 491957263 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3240 : : return false;
3241 : :
3242 : : /* Check equality of integer constants before bailing out due to
3243 : : precision differences. */
3244 : 3992329848 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3245 : : {
3246 : : /* Address of INTEGER_CST is not defined; check that we did not forget
3247 : : to drop the OEP_ADDRESS_OF flags. */
3248 : 629863786 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3249 : 629863786 : return tree_int_cst_equal (arg0, arg1);
3250 : : }
3251 : :
3252 : 3362466062 : if ((flags & OEP_ASSUME_WRAPV)
3253 : 2063723 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3254 : : {
3255 : 781718 : const_tree t_arg0 = arg0;
3256 : 781718 : const_tree t_arg1 = arg1;
3257 : 781718 : STRIP_NOPS (arg0);
3258 : 781718 : STRIP_NOPS (arg1);
3259 : : /* Only recurse if the conversion was one that was valid to strip. */
3260 : 781718 : if (t_arg0 != arg0 || t_arg1 != arg1)
3261 : 728343 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3262 : : }
3263 : :
3264 : 3361737719 : if (!(flags & OEP_ADDRESS_OF))
3265 : : {
3266 : : /* Check if we are checking an operation where the two's compliment
3267 : : bitwise representation of the result is not the same between signed and
3268 : : unsigned arithmetic. */
3269 : 2986503530 : bool enforce_signedness = true;
3270 : 2986503530 : if (flags & OEP_ASSUME_WRAPV)
3271 : : {
3272 : 1247402 : switch (TREE_CODE (arg0))
3273 : : {
3274 : : case PLUS_EXPR:
3275 : : case MINUS_EXPR:
3276 : : case MULT_EXPR:
3277 : : case BIT_IOR_EXPR:
3278 : : case BIT_XOR_EXPR:
3279 : : case BIT_AND_EXPR:
3280 : : case BIT_NOT_EXPR:
3281 : : case ABS_EXPR:
3282 : : CASE_CONVERT:
3283 : : case SSA_NAME:
3284 : : case INTEGER_CST:
3285 : : case VAR_DECL:
3286 : : case PARM_DECL:
3287 : : case RESULT_DECL:
3288 : 2986503530 : enforce_signedness = false;
3289 : : break;
3290 : :
3291 : : default:
3292 : : break;
3293 : : }
3294 : : }
3295 : :
3296 : : /* If both types don't have the same signedness, then we can't consider
3297 : : them equal. We must check this before the STRIP_NOPS calls
3298 : : because they may change the signedness of the arguments. As pointers
3299 : : strictly don't have a signedness, require either two pointers or
3300 : : two non-pointers as well. */
3301 : 2986503530 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3302 : 2986503530 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3303 : 137449315 : && enforce_signedness))
3304 : : return false;
3305 : :
3306 : : /* If both types don't have the same precision, then it is not safe
3307 : : to strip NOPs. */
3308 : 2699820881 : if (element_precision (type0) != element_precision (type1))
3309 : : return false;
3310 : :
3311 : 2554587372 : STRIP_NOPS (arg0);
3312 : 2554587372 : STRIP_NOPS (arg1);
3313 : :
3314 : 2554587372 : type0 = TREE_TYPE (arg0);
3315 : 2554587372 : type1 = TREE_TYPE (arg1);
3316 : : }
3317 : : #if 0
3318 : : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3319 : : sanity check once the issue is solved. */
3320 : : else
3321 : : /* Addresses of conversions and SSA_NAMEs (and many other things)
3322 : : are not defined. Check that we did not forget to drop the
3323 : : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3324 : : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3325 : : && TREE_CODE (arg0) != SSA_NAME);
3326 : : #endif
3327 : :
3328 : : /* In case both args are comparisons but with different comparison
3329 : : code, try to swap the comparison operands of one arg to produce
3330 : : a match and compare that variant. */
3331 : 2929821561 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3332 : 1184590058 : && COMPARISON_CLASS_P (arg0)
3333 : 6053834 : && COMPARISON_CLASS_P (arg1))
3334 : : {
3335 : 4471856 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3336 : :
3337 : 4471856 : if (TREE_CODE (arg0) == swap_code)
3338 : 1968724 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3339 : 1968724 : TREE_OPERAND (arg1, 1), flags)
3340 : 1988758 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3341 : 20034 : TREE_OPERAND (arg1, 0), flags);
3342 : : }
3343 : :
3344 : 2927852837 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3345 : : {
3346 : : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3347 : 1182621334 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3348 : : ;
3349 : 1182603227 : else if (flags & OEP_ADDRESS_OF)
3350 : : {
3351 : : /* If we are interested in comparing addresses ignore
3352 : : MEM_REF wrappings of the base that can appear just for
3353 : : TBAA reasons. */
3354 : 47236688 : if (TREE_CODE (arg0) == MEM_REF
3355 : 7274740 : && DECL_P (arg1)
3356 : 5115772 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3357 : 1227691 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3358 : 47728846 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3359 : : return true;
3360 : 47013830 : else if (TREE_CODE (arg1) == MEM_REF
3361 : 29847929 : && DECL_P (arg0)
3362 : 11028697 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3363 : 2616513 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3364 : 47686870 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3365 : : return true;
3366 : 46508864 : return false;
3367 : : }
3368 : : else
3369 : : return false;
3370 : : }
3371 : :
3372 : : /* When not checking adddresses, this is needed for conversions and for
3373 : : COMPONENT_REF. Might as well play it safe and always test this. */
3374 : 1745249610 : if (TREE_CODE (type0) == ERROR_MARK
3375 : 1745249610 : || TREE_CODE (type1) == ERROR_MARK
3376 : 3490499220 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3377 : 23812841 : && !(flags & OEP_ADDRESS_OF)))
3378 : 3331289 : return false;
3379 : :
3380 : : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3381 : : We don't care about side effects in that case because the SAVE_EXPR
3382 : : takes care of that for us. In all other cases, two expressions are
3383 : : equal if they have no side effects. If we have two identical
3384 : : expressions with side effects that should be treated the same due
3385 : : to the only side effects being identical SAVE_EXPR's, that will
3386 : : be detected in the recursive calls below.
3387 : : If we are taking an invariant address of two identical objects
3388 : : they are necessarily equal as well. */
3389 : 309208995 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3390 : 2051127138 : && (TREE_CODE (arg0) == SAVE_EXPR
3391 : 309196257 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3392 : 275655306 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3393 : : return true;
3394 : :
3395 : : /* Next handle constant cases, those for which we can return 1 even
3396 : : if ONLY_CONST is set. */
3397 : 1432851318 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3398 : 18992891 : switch (TREE_CODE (arg0))
3399 : : {
3400 : 153 : case INTEGER_CST:
3401 : 153 : return tree_int_cst_equal (arg0, arg1);
3402 : :
3403 : 0 : case FIXED_CST:
3404 : 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3405 : : TREE_FIXED_CST (arg1));
3406 : :
3407 : 3634011 : case REAL_CST:
3408 : 3634011 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3409 : : return true;
3410 : :
3411 : 2620410 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3412 : : {
3413 : : /* If we do not distinguish between signed and unsigned zero,
3414 : : consider them equal. */
3415 : 13996 : if (real_zerop (arg0) && real_zerop (arg1))
3416 : : return true;
3417 : : }
3418 : 2620405 : return false;
3419 : :
3420 : 721423 : case VECTOR_CST:
3421 : 721423 : {
3422 : 721423 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3423 : 721423 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3424 : : return false;
3425 : :
3426 : 709888 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3427 : 709888 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3428 : : return false;
3429 : :
3430 : 684866 : unsigned int count = vector_cst_encoded_nelts (arg0);
3431 : 943993 : for (unsigned int i = 0; i < count; ++i)
3432 : 1457684 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3433 : 728842 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3434 : : return false;
3435 : : return true;
3436 : : }
3437 : :
3438 : 12762 : case COMPLEX_CST:
3439 : 12762 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3440 : : flags)
3441 : 12762 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3442 : : flags));
3443 : :
3444 : 1153625 : case STRING_CST:
3445 : 1153625 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3446 : 1153625 : && ! memcmp (TREE_STRING_POINTER (arg0),
3447 : 740853 : TREE_STRING_POINTER (arg1),
3448 : 740853 : TREE_STRING_LENGTH (arg0)));
3449 : :
3450 : 0 : case RAW_DATA_CST:
3451 : 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3452 : 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3453 : 0 : RAW_DATA_POINTER (arg1),
3454 : 0 : RAW_DATA_LENGTH (arg0)));
3455 : :
3456 : 12571918 : case ADDR_EXPR:
3457 : 12571918 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3458 : 12571918 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3459 : : flags | OEP_ADDRESS_OF
3460 : 12571918 : | OEP_MATCH_SIDE_EFFECTS);
3461 : 174685 : case CONSTRUCTOR:
3462 : 174685 : {
3463 : : /* In GIMPLE empty constructors are allowed in initializers of
3464 : : aggregates. */
3465 : 174685 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3466 : : return true;
3467 : :
3468 : : /* See sem_variable::equals in ipa-icf for a similar approach. */
3469 : 135319 : if (TREE_CODE (type0) != TREE_CODE (type1))
3470 : : return false;
3471 : 135319 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3472 : : {
3473 : : /* For arrays, check that the sizes all match. */
3474 : 14 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3475 : 14 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3476 : 14 : || siz0 < 0
3477 : 28 : || siz0 != int_size_in_bytes (type1))
3478 : 0 : return false;
3479 : : }
3480 : 135305 : else if (!types_compatible_p (type0, type1))
3481 : : return false;
3482 : :
3483 : 135319 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3484 : 135319 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3485 : 405957 : if (vec_safe_length (v0) != vec_safe_length (v1))
3486 : : return false;
3487 : :
3488 : : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3489 : : of the CONSTRUCTOR referenced indirectly. */
3490 : 135319 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3491 : :
3492 : 344442885 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3493 : : {
3494 : 193257 : constructor_elt *c0 = &(*v0)[idx];
3495 : 193257 : constructor_elt *c1 = &(*v1)[idx];
3496 : :
3497 : : /* Check that the values are the same... */
3498 : 193257 : if (c0->value != c1->value
3499 : 193257 : && !operand_equal_p (c0->value, c1->value, flags))
3500 : : return false;
3501 : :
3502 : : /* ... and that they apply to the same field! */
3503 : 107223 : if (c0->index != c1->index
3504 : 107223 : && (TREE_CODE (type0) == ARRAY_TYPE
3505 : 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3506 : 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3507 : 0 : DECL_FIELD_OFFSET (c1->index),
3508 : : flags)
3509 : 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3510 : 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3511 : : flags)))
3512 : 0 : return false;
3513 : : }
3514 : :
3515 : : return true;
3516 : : }
3517 : :
3518 : : default:
3519 : : break;
3520 : : }
3521 : :
3522 : : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3523 : : two instances of undefined behavior will give identical results. */
3524 : 1414582741 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3525 : : return false;
3526 : :
3527 : : /* Define macros to test an operand from arg0 and arg1 for equality and a
3528 : : variant that allows null and views null as being different from any
3529 : : non-null value. In the latter case, if either is null, the both
3530 : : must be; otherwise, do the normal comparison. */
3531 : : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3532 : : TREE_OPERAND (arg1, N), flags)
3533 : :
3534 : : #define OP_SAME_WITH_NULL(N) \
3535 : : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3536 : : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3537 : :
3538 : 1414582741 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3539 : : {
3540 : 7089373 : case tcc_unary:
3541 : : /* Two conversions are equal only if signedness and modes match. */
3542 : 7089373 : switch (TREE_CODE (arg0))
3543 : : {
3544 : 6723642 : CASE_CONVERT:
3545 : 6723642 : case FIX_TRUNC_EXPR:
3546 : 6723642 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3547 : : return false;
3548 : : break;
3549 : : default:
3550 : : break;
3551 : : }
3552 : :
3553 : 7089352 : return OP_SAME_WITH_NULL (0);
3554 : :
3555 : :
3556 : 21553654 : case tcc_comparison:
3557 : 21553654 : case tcc_binary:
3558 : 21553654 : if (OP_SAME (0) && OP_SAME (1))
3559 : : return true;
3560 : :
3561 : : /* For commutative ops, allow the other order. */
3562 : 15870386 : return (commutative_tree_code (TREE_CODE (arg0))
3563 : 12146418 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3564 : 12146418 : TREE_OPERAND (arg1, 1), flags)
3565 : 16081451 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3566 : 211065 : TREE_OPERAND (arg1, 0), flags));
3567 : :
3568 : 860048651 : case tcc_reference:
3569 : : /* If either of the pointer (or reference) expressions we are
3570 : : dereferencing contain a side effect, these cannot be equal,
3571 : : but their addresses can be. */
3572 : 860048651 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3573 : 860048651 : && (TREE_SIDE_EFFECTS (arg0)
3574 : 799511164 : || TREE_SIDE_EFFECTS (arg1)))
3575 : : return false;
3576 : :
3577 : 859833551 : switch (TREE_CODE (arg0))
3578 : : {
3579 : 3854295 : case INDIRECT_REF:
3580 : 3854295 : if (!(flags & OEP_ADDRESS_OF))
3581 : : {
3582 : 3831808 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3583 : : return false;
3584 : : /* Verify that the access types are compatible. */
3585 : 3830363 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3586 : : return false;
3587 : : }
3588 : 3790878 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3589 : 3790878 : return OP_SAME (0);
3590 : :
3591 : 655335 : case IMAGPART_EXPR:
3592 : : /* Require the same offset. */
3593 : 655335 : if (!operand_equal_p (TYPE_SIZE (type0),
3594 : 655335 : TYPE_SIZE (type1),
3595 : : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3596 : : return false;
3597 : :
3598 : : /* Fallthru. */
3599 : 2411460 : case REALPART_EXPR:
3600 : 2411460 : case VIEW_CONVERT_EXPR:
3601 : 2411460 : return OP_SAME (0);
3602 : :
3603 : 75669119 : case TARGET_MEM_REF:
3604 : 75669119 : case MEM_REF:
3605 : 75669119 : if (!(flags & OEP_ADDRESS_OF))
3606 : : {
3607 : : /* Require equal access sizes */
3608 : 16031539 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3609 : 16031539 : && (!TYPE_SIZE (type0)
3610 : 1046898 : || !TYPE_SIZE (type1)
3611 : 1042492 : || !operand_equal_p (TYPE_SIZE (type0),
3612 : 1042492 : TYPE_SIZE (type1),
3613 : : flags)))
3614 : 1033846 : return false;
3615 : : /* Verify that access happens in similar types. */
3616 : 14997693 : if (!types_compatible_p (type0, type1))
3617 : : return false;
3618 : : /* Verify that accesses are TBAA compatible. */
3619 : 14655746 : if (!alias_ptr_types_compatible_p
3620 : 14655746 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3621 : 14655746 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3622 : 13749694 : || (MR_DEPENDENCE_CLIQUE (arg0)
3623 : 13749694 : != MR_DEPENDENCE_CLIQUE (arg1))
3624 : 26348322 : || (MR_DEPENDENCE_BASE (arg0)
3625 : 11692576 : != MR_DEPENDENCE_BASE (arg1)))
3626 : : return false;
3627 : : /* Verify that alignment is compatible. */
3628 : 11238335 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3629 : : return false;
3630 : : }
3631 : 70726771 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3632 : 120795257 : return (OP_SAME (0) && OP_SAME (1)
3633 : : /* TARGET_MEM_REF require equal extra operands. */
3634 : 95262470 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3635 : 540426 : || (OP_SAME_WITH_NULL (2)
3636 : 268940 : && OP_SAME_WITH_NULL (3)
3637 : 263844 : && OP_SAME_WITH_NULL (4))));
3638 : :
3639 : 33478264 : case ARRAY_REF:
3640 : 33478264 : case ARRAY_RANGE_REF:
3641 : 33478264 : if (!OP_SAME (0))
3642 : : return false;
3643 : 28748721 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3644 : : /* Compare the array index by value if it is constant first as we
3645 : : may have different types but same value here. */
3646 : 28748721 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3647 : 28748721 : TREE_OPERAND (arg1, 1))
3648 : 25840403 : || OP_SAME (1))
3649 : 5843730 : && OP_SAME_WITH_NULL (2)
3650 : 5843632 : && OP_SAME_WITH_NULL (3)
3651 : : /* Compare low bound and element size as with OEP_ADDRESS_OF
3652 : : we have to account for the offset of the ref. */
3653 : 37514218 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3654 : 2921816 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3655 : 2765 : || (operand_equal_p (array_ref_low_bound
3656 : 2765 : (CONST_CAST_TREE (arg0)),
3657 : : array_ref_low_bound
3658 : 2765 : (CONST_CAST_TREE (arg1)), flags)
3659 : 2765 : && operand_equal_p (array_ref_element_size
3660 : 2765 : (CONST_CAST_TREE (arg0)),
3661 : : array_ref_element_size
3662 : 2765 : (CONST_CAST_TREE (arg1)),
3663 : : flags))));
3664 : :
3665 : 743949189 : case COMPONENT_REF:
3666 : : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3667 : : may be NULL when we're called to compare MEM_EXPRs. */
3668 : 743949189 : if (!OP_SAME_WITH_NULL (0))
3669 : : return false;
3670 : 56114913 : {
3671 : 56114913 : bool compare_address = flags & OEP_ADDRESS_OF;
3672 : :
3673 : : /* Most of time we only need to compare FIELD_DECLs for equality.
3674 : : However when determining address look into actual offsets.
3675 : : These may match for unions and unshared record types. */
3676 : 56114913 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3677 : 56114913 : if (!OP_SAME (1))
3678 : : {
3679 : 32719721 : if (compare_address
3680 : 597172 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3681 : : {
3682 : 597169 : tree field0 = TREE_OPERAND (arg0, 1);
3683 : 597169 : tree field1 = TREE_OPERAND (arg1, 1);
3684 : :
3685 : : /* Non-FIELD_DECL operands can appear in C++ templates. */
3686 : 597169 : if (TREE_CODE (field0) != FIELD_DECL
3687 : 597169 : || TREE_CODE (field1) != FIELD_DECL)
3688 : : return false;
3689 : :
3690 : 597169 : if (!DECL_FIELD_OFFSET (field0)
3691 : 597169 : || !DECL_FIELD_OFFSET (field1))
3692 : 3 : return field0 == field1;
3693 : :
3694 : 597166 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3695 : 597166 : DECL_FIELD_OFFSET (field1), flags)
3696 : 785455 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3697 : 188289 : DECL_FIELD_BIT_OFFSET (field1),
3698 : : flags))
3699 : 559273 : return false;
3700 : : }
3701 : : else
3702 : : return false;
3703 : : }
3704 : : }
3705 : 23433085 : return OP_SAME_WITH_NULL (2);
3706 : :
3707 : 471176 : case BIT_FIELD_REF:
3708 : 471176 : if (!OP_SAME (0))
3709 : : return false;
3710 : 366201 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3711 : 366201 : return OP_SAME (1) && OP_SAME (2);
3712 : :
3713 : : default:
3714 : : return false;
3715 : : }
3716 : :
3717 : 47997029 : case tcc_expression:
3718 : 47997029 : switch (TREE_CODE (arg0))
3719 : : {
3720 : 43076269 : case ADDR_EXPR:
3721 : : /* Be sure we pass right ADDRESS_OF flag. */
3722 : 43076269 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3723 : 43076269 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3724 : 43076269 : TREE_OPERAND (arg1, 0),
3725 : 43076269 : flags | OEP_ADDRESS_OF);
3726 : :
3727 : 660161 : case TRUTH_NOT_EXPR:
3728 : 660161 : return OP_SAME (0);
3729 : :
3730 : 39711 : case TRUTH_ANDIF_EXPR:
3731 : 39711 : case TRUTH_ORIF_EXPR:
3732 : 39711 : return OP_SAME (0) && OP_SAME (1);
3733 : :
3734 : 0 : case WIDEN_MULT_PLUS_EXPR:
3735 : 0 : case WIDEN_MULT_MINUS_EXPR:
3736 : 0 : if (!OP_SAME (2))
3737 : : return false;
3738 : : /* The multiplcation operands are commutative. */
3739 : : /* FALLTHRU */
3740 : :
3741 : 19227 : case TRUTH_AND_EXPR:
3742 : 19227 : case TRUTH_OR_EXPR:
3743 : 19227 : case TRUTH_XOR_EXPR:
3744 : 19227 : if (OP_SAME (0) && OP_SAME (1))
3745 : : return true;
3746 : :
3747 : : /* Otherwise take into account this is a commutative operation. */
3748 : 19209 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3749 : 19209 : TREE_OPERAND (arg1, 1), flags)
3750 : 19212 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3751 : 3 : TREE_OPERAND (arg1, 0), flags));
3752 : :
3753 : 117867 : case COND_EXPR:
3754 : 117867 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3755 : 48017 : return false;
3756 : 69850 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3757 : 69850 : return OP_SAME (0);
3758 : :
3759 : 4 : case BIT_INSERT_EXPR:
3760 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
3761 : : of op1. Need to check to make sure they are the same. */
3762 : 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3763 : 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3764 : 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3765 : 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3766 : : return false;
3767 : : /* FALLTHRU */
3768 : :
3769 : 191 : case VEC_COND_EXPR:
3770 : 191 : case DOT_PROD_EXPR:
3771 : 191 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3772 : :
3773 : 23145 : case MODIFY_EXPR:
3774 : 23145 : case INIT_EXPR:
3775 : 23145 : case COMPOUND_EXPR:
3776 : 23145 : case PREDECREMENT_EXPR:
3777 : 23145 : case PREINCREMENT_EXPR:
3778 : 23145 : case POSTDECREMENT_EXPR:
3779 : 23145 : case POSTINCREMENT_EXPR:
3780 : 23145 : if (flags & OEP_LEXICOGRAPHIC)
3781 : 163 : return OP_SAME (0) && OP_SAME (1);
3782 : : return false;
3783 : :
3784 : 88122 : case CLEANUP_POINT_EXPR:
3785 : 88122 : case EXPR_STMT:
3786 : 88122 : case SAVE_EXPR:
3787 : 88122 : if (flags & OEP_LEXICOGRAPHIC)
3788 : 208 : return OP_SAME (0);
3789 : : return false;
3790 : :
3791 : 100479 : case OBJ_TYPE_REF:
3792 : : /* Virtual table reference. */
3793 : 200958 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3794 : 100479 : OBJ_TYPE_REF_EXPR (arg1), flags))
3795 : : return false;
3796 : 881 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3797 : 881 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3798 : 881 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3799 : : return false;
3800 : 881 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3801 : 881 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3802 : : return false;
3803 : 881 : if (virtual_method_call_p (arg0))
3804 : : {
3805 : 881 : if (!virtual_method_call_p (arg1))
3806 : : return false;
3807 : 881 : return types_same_for_odr (obj_type_ref_class (arg0),
3808 : 1762 : obj_type_ref_class (arg1));
3809 : : }
3810 : : return false;
3811 : :
3812 : : default:
3813 : : return false;
3814 : : }
3815 : :
3816 : 2824387 : case tcc_vl_exp:
3817 : 2824387 : switch (TREE_CODE (arg0))
3818 : : {
3819 : 2824387 : case CALL_EXPR:
3820 : 2824387 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3821 : 2824387 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3822 : : /* If not both CALL_EXPRs are either internal or normal function
3823 : : functions, then they are not equal. */
3824 : : return false;
3825 : 2824387 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3826 : : {
3827 : : /* If the CALL_EXPRs call different internal functions, then they
3828 : : are not equal. */
3829 : 4 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3830 : : return false;
3831 : : }
3832 : : else
3833 : : {
3834 : : /* If the CALL_EXPRs call different functions, then they are not
3835 : : equal. */
3836 : 2824383 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3837 : : flags))
3838 : : return false;
3839 : : }
3840 : :
3841 : : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3842 : 1836182 : {
3843 : 1836182 : unsigned int cef = call_expr_flags (arg0);
3844 : 1836182 : if (flags & OEP_PURE_SAME)
3845 : 0 : cef &= ECF_CONST | ECF_PURE;
3846 : : else
3847 : 1836182 : cef &= ECF_CONST;
3848 : 1836182 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3849 : : return false;
3850 : : }
3851 : :
3852 : : /* Now see if all the arguments are the same. */
3853 : 32563 : {
3854 : 32563 : const_call_expr_arg_iterator iter0, iter1;
3855 : 32563 : const_tree a0, a1;
3856 : 65126 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3857 : 32563 : a1 = first_const_call_expr_arg (arg1, &iter1);
3858 : 40668 : a0 && a1;
3859 : 8105 : a0 = next_const_call_expr_arg (&iter0),
3860 : 8105 : a1 = next_const_call_expr_arg (&iter1))
3861 : 34055 : if (! operand_equal_p (a0, a1, flags))
3862 : : return false;
3863 : :
3864 : : /* If we get here and both argument lists are exhausted
3865 : : then the CALL_EXPRs are equal. */
3866 : 6613 : return ! (a0 || a1);
3867 : : }
3868 : : default:
3869 : : return false;
3870 : : }
3871 : :
3872 : 159978528 : case tcc_declaration:
3873 : : /* Consider __builtin_sqrt equal to sqrt. */
3874 : 159978528 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3875 : 6327874 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3876 : 338901 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3877 : 5656820 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3878 : 338901 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3879 : :
3880 : 154321708 : if (DECL_P (arg0)
3881 : 154321708 : && (flags & OEP_DECL_NAME)
3882 : 35 : && (flags & OEP_LEXICOGRAPHIC))
3883 : : {
3884 : : /* Consider decls with the same name equal. The caller needs
3885 : : to make sure they refer to the same entity (such as a function
3886 : : formal parameter). */
3887 : 35 : tree a0name = DECL_NAME (arg0);
3888 : 35 : tree a1name = DECL_NAME (arg1);
3889 : 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3890 : 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3891 : 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3892 : : }
3893 : : return false;
3894 : :
3895 : 312913569 : case tcc_exceptional:
3896 : 312913569 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3897 : : {
3898 : 19389 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3899 : : return false;
3900 : :
3901 : : /* In GIMPLE constructors are used only to build vectors from
3902 : : elements. Individual elements in the constructor must be
3903 : : indexed in increasing order and form an initial sequence.
3904 : :
3905 : : We make no effort to compare nonconstant ones in GENERIC. */
3906 : 19389 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3907 : : return false;
3908 : :
3909 : : /* Be sure that vectors constructed have the same representation.
3910 : : We only tested element precision and modes to match.
3911 : : Vectors may be BLKmode and thus also check that the number of
3912 : : parts match. */
3913 : 543 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3914 : 1086 : TYPE_VECTOR_SUBPARTS (type1)))
3915 : : return false;
3916 : :
3917 : 543 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3918 : 543 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3919 : 543 : unsigned int len = vec_safe_length (v0);
3920 : :
3921 : 1086 : if (len != vec_safe_length (v1))
3922 : : return false;
3923 : :
3924 : 3377 : for (unsigned int i = 0; i < len; i++)
3925 : : {
3926 : 3004 : constructor_elt *c0 = &(*v0)[i];
3927 : 3004 : constructor_elt *c1 = &(*v1)[i];
3928 : :
3929 : 3004 : if (!operand_equal_p (c0->value, c1->value, flags)
3930 : : /* In GIMPLE the indexes can be either NULL or matching i.
3931 : : Double check this so we won't get false
3932 : : positives for GENERIC. */
3933 : 2834 : || (c0->index
3934 : 2588 : && (TREE_CODE (c0->index) != INTEGER_CST
3935 : 2588 : || compare_tree_int (c0->index, i)))
3936 : 5838 : || (c1->index
3937 : 2588 : && (TREE_CODE (c1->index) != INTEGER_CST
3938 : 2588 : || compare_tree_int (c1->index, i))))
3939 : 170 : return false;
3940 : : }
3941 : : return true;
3942 : : }
3943 : 312894180 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3944 : 2987 : && (flags & OEP_LEXICOGRAPHIC))
3945 : : {
3946 : : /* Compare the STATEMENT_LISTs. */
3947 : 16 : tree_stmt_iterator tsi1, tsi2;
3948 : 16 : tree body1 = CONST_CAST_TREE (arg0);
3949 : 16 : tree body2 = CONST_CAST_TREE (arg1);
3950 : 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3951 : 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3952 : : {
3953 : : /* The lists don't have the same number of statements. */
3954 : 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3955 : : return false;
3956 : 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3957 : : return true;
3958 : 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3959 : : flags & (OEP_LEXICOGRAPHIC
3960 : : | OEP_NO_HASH_CHECK)))
3961 : : return false;
3962 : : }
3963 : : }
3964 : : return false;
3965 : :
3966 : 2177365 : case tcc_statement:
3967 : 2177365 : switch (TREE_CODE (arg0))
3968 : : {
3969 : 52 : case RETURN_EXPR:
3970 : 52 : if (flags & OEP_LEXICOGRAPHIC)
3971 : 52 : return OP_SAME_WITH_NULL (0);
3972 : : return false;
3973 : 4 : case DEBUG_BEGIN_STMT:
3974 : 4 : if (flags & OEP_LEXICOGRAPHIC)
3975 : : return true;
3976 : : return false;
3977 : : default:
3978 : : return false;
3979 : : }
3980 : :
3981 : : default:
3982 : : return false;
3983 : : }
3984 : :
3985 : : #undef OP_SAME
3986 : : #undef OP_SAME_WITH_NULL
3987 : : }
3988 : :
3989 : : /* Generate a hash value for an expression. This can be used iteratively
3990 : : by passing a previous result as the HSTATE argument. */
3991 : :
3992 : : void
3993 : 2732305508 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3994 : : unsigned int flags)
3995 : : {
3996 : 2732305508 : int i;
3997 : 2732305508 : enum tree_code code;
3998 : 2732305508 : enum tree_code_class tclass;
3999 : :
4000 : 2732305508 : if (t == NULL_TREE || t == error_mark_node)
4001 : : {
4002 : 73493687 : hstate.merge_hash (0);
4003 : 73493687 : return;
4004 : : }
4005 : :
4006 : 2658811821 : STRIP_ANY_LOCATION_WRAPPER (t);
4007 : :
4008 : 2658811821 : if (!(flags & OEP_ADDRESS_OF))
4009 : 2412651600 : STRIP_NOPS (t);
4010 : :
4011 : 2658811821 : code = TREE_CODE (t);
4012 : :
4013 : 2658811821 : switch (code)
4014 : : {
4015 : : /* Alas, constants aren't shared, so we can't rely on pointer
4016 : : identity. */
4017 : 788 : case VOID_CST:
4018 : 788 : hstate.merge_hash (0);
4019 : 788 : return;
4020 : 776183606 : case INTEGER_CST:
4021 : 776183606 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4022 : 1576473461 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4023 : 800289855 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4024 : : return;
4025 : 15346979 : case REAL_CST:
4026 : 15346979 : {
4027 : 15346979 : unsigned int val2;
4028 : 15346979 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4029 : : val2 = rvc_zero;
4030 : : else
4031 : 15127634 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4032 : 15346979 : hstate.merge_hash (val2);
4033 : 15346979 : return;
4034 : : }
4035 : 0 : case FIXED_CST:
4036 : 0 : {
4037 : 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4038 : 0 : hstate.merge_hash (val2);
4039 : 0 : return;
4040 : : }
4041 : 10110274 : case STRING_CST:
4042 : 10110274 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4043 : 10110274 : TREE_STRING_LENGTH (t));
4044 : 10110274 : return;
4045 : 188 : case RAW_DATA_CST:
4046 : 188 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4047 : 188 : RAW_DATA_LENGTH (t));
4048 : 188 : return;
4049 : 208565 : case COMPLEX_CST:
4050 : 208565 : hash_operand (TREE_REALPART (t), hstate, flags);
4051 : 208565 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4052 : 208565 : return;
4053 : 2886896 : case VECTOR_CST:
4054 : 2886896 : {
4055 : 2886896 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4056 : 2886896 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4057 : 2886896 : unsigned int count = vector_cst_encoded_nelts (t);
4058 : 8944940 : for (unsigned int i = 0; i < count; ++i)
4059 : 6058044 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4060 : : return;
4061 : : }
4062 : 858343072 : case SSA_NAME:
4063 : : /* We can just compare by pointer. */
4064 : 858343072 : hstate.add_hwi (SSA_NAME_VERSION (t));
4065 : 858343072 : return;
4066 : : case PLACEHOLDER_EXPR:
4067 : : /* The node itself doesn't matter. */
4068 : : return;
4069 : : case BLOCK:
4070 : : case OMP_CLAUSE:
4071 : : case OMP_NEXT_VARIANT:
4072 : : case OMP_TARGET_DEVICE_MATCHES:
4073 : : /* Ignore. */
4074 : : return;
4075 : : case TREE_LIST:
4076 : : /* A list of expressions, for a CALL_EXPR or as the elements of a
4077 : : VECTOR_CST. */
4078 : 271632 : for (; t; t = TREE_CHAIN (t))
4079 : 135816 : hash_operand (TREE_VALUE (t), hstate, flags);
4080 : : return;
4081 : 4855023 : case CONSTRUCTOR:
4082 : 4855023 : {
4083 : 4855023 : unsigned HOST_WIDE_INT idx;
4084 : 4855023 : tree field, value;
4085 : 4855023 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4086 : 4855023 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4087 : 19795669 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4088 : : {
4089 : : /* In GIMPLE the indexes can be either NULL or matching i. */
4090 : 14940646 : if (field == NULL_TREE)
4091 : 1076827 : field = bitsize_int (idx);
4092 : 14940646 : if (TREE_CODE (field) == FIELD_DECL)
4093 : : {
4094 : 9909931 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4095 : 9909931 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4096 : : }
4097 : : else
4098 : 5030715 : hash_operand (field, hstate, flags);
4099 : 14940646 : hash_operand (value, hstate, flags);
4100 : : }
4101 : : return;
4102 : : }
4103 : 182 : case STATEMENT_LIST:
4104 : 182 : {
4105 : 182 : tree_stmt_iterator i;
4106 : 182 : for (i = tsi_start (CONST_CAST_TREE (t));
4107 : 550 : !tsi_end_p (i); tsi_next (&i))
4108 : 368 : hash_operand (tsi_stmt (i), hstate, flags);
4109 : 182 : return;
4110 : : }
4111 : : case TREE_VEC:
4112 : 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4113 : 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4114 : : return;
4115 : 4 : case IDENTIFIER_NODE:
4116 : 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4117 : 4 : return;
4118 : 19823237 : case FUNCTION_DECL:
4119 : : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4120 : : Otherwise nodes that compare equal according to operand_equal_p might
4121 : : get different hash codes. However, don't do this for machine specific
4122 : : or front end builtins, since the function code is overloaded in those
4123 : : cases. */
4124 : 19823237 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4125 : 19823237 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4126 : : {
4127 : 7012875 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4128 : 7012875 : code = TREE_CODE (t);
4129 : : }
4130 : : /* FALL THROUGH */
4131 : 990740304 : default:
4132 : 990740304 : if (POLY_INT_CST_P (t))
4133 : : {
4134 : : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4135 : : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4136 : : return;
4137 : : }
4138 : 990740304 : tclass = TREE_CODE_CLASS (code);
4139 : :
4140 : 990740304 : if (tclass == tcc_declaration)
4141 : : {
4142 : : /* DECL's have a unique ID */
4143 : 667822577 : hstate.add_hwi (DECL_UID (t));
4144 : : }
4145 : 322917727 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4146 : : {
4147 : : /* For comparisons that can be swapped, use the lower
4148 : : tree code. */
4149 : 149223 : enum tree_code ccode = swap_tree_comparison (code);
4150 : 149223 : if (code < ccode)
4151 : 66171 : ccode = code;
4152 : 149223 : hstate.add_object (ccode);
4153 : 149223 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4154 : 149223 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4155 : : }
4156 : 322768504 : else if (CONVERT_EXPR_CODE_P (code))
4157 : : {
4158 : : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4159 : : operand_equal_p. */
4160 : 5169226 : enum tree_code ccode = NOP_EXPR;
4161 : 5169226 : hstate.add_object (ccode);
4162 : :
4163 : : /* Don't hash the type, that can lead to having nodes which
4164 : : compare equal according to operand_equal_p, but which
4165 : : have different hash codes. Make sure to include signedness
4166 : : in the hash computation. */
4167 : 5169226 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4168 : 5169226 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4169 : : }
4170 : : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4171 : 317599278 : else if (code == MEM_REF
4172 : 78571194 : && (flags & OEP_ADDRESS_OF) != 0
4173 : 69560408 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4174 : 13532015 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4175 : 330890033 : && integer_zerop (TREE_OPERAND (t, 1)))
4176 : 6235128 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4177 : : hstate, flags);
4178 : : /* Don't ICE on FE specific trees, or their arguments etc.
4179 : : during operand_equal_p hash verification. */
4180 : 311364150 : else if (!IS_EXPR_CODE_CLASS (tclass))
4181 : 252 : gcc_assert (flags & OEP_HASH_CHECK);
4182 : : else
4183 : : {
4184 : 311363898 : unsigned int sflags = flags;
4185 : :
4186 : 311363898 : hstate.add_object (code);
4187 : :
4188 : 311363898 : switch (code)
4189 : : {
4190 : 123753668 : case ADDR_EXPR:
4191 : 123753668 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4192 : 123753668 : flags |= OEP_ADDRESS_OF;
4193 : 123753668 : sflags = flags;
4194 : 123753668 : break;
4195 : :
4196 : 76676823 : case INDIRECT_REF:
4197 : 76676823 : case MEM_REF:
4198 : 76676823 : case TARGET_MEM_REF:
4199 : 76676823 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4200 : 76676823 : sflags = flags;
4201 : 76676823 : break;
4202 : :
4203 : 73133180 : case COMPONENT_REF:
4204 : 73133180 : if (sflags & OEP_ADDRESS_OF)
4205 : : {
4206 : 36559181 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4207 : 36559181 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4208 : : hstate, flags & ~OEP_ADDRESS_OF);
4209 : 36559181 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4210 : : hstate, flags & ~OEP_ADDRESS_OF);
4211 : 36559181 : return;
4212 : : }
4213 : : break;
4214 : 14803745 : case ARRAY_REF:
4215 : 14803745 : case ARRAY_RANGE_REF:
4216 : 14803745 : case BIT_FIELD_REF:
4217 : 14803745 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4218 : 14803745 : break;
4219 : :
4220 : 8527 : case COND_EXPR:
4221 : 8527 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4222 : 8527 : break;
4223 : :
4224 : 0 : case WIDEN_MULT_PLUS_EXPR:
4225 : 0 : case WIDEN_MULT_MINUS_EXPR:
4226 : 0 : {
4227 : : /* The multiplication operands are commutative. */
4228 : 0 : inchash::hash one, two;
4229 : 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4230 : 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4231 : 0 : hstate.add_commutative (one, two);
4232 : 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4233 : 0 : return;
4234 : : }
4235 : :
4236 : 53382 : case CALL_EXPR:
4237 : 53382 : if (CALL_EXPR_FN (t) == NULL_TREE)
4238 : 2 : hstate.add_int (CALL_EXPR_IFN (t));
4239 : : break;
4240 : :
4241 : 72 : case TARGET_EXPR:
4242 : : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4243 : : Usually different TARGET_EXPRs just should use
4244 : : different temporaries in their slots. */
4245 : 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4246 : 72 : return;
4247 : :
4248 : 211990 : case OBJ_TYPE_REF:
4249 : : /* Virtual table reference. */
4250 : 211990 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4251 : 211990 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4252 : 211990 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4253 : 211990 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4254 : 211990 : if (!virtual_method_call_p (t))
4255 : : return;
4256 : 211975 : if (tree c = obj_type_ref_class (t))
4257 : : {
4258 : 211975 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4259 : : /* We compute mangled names only when free_lang_data is run.
4260 : : In that case we can hash precisely. */
4261 : 211975 : if (TREE_CODE (c) == TYPE_DECL
4262 : 211975 : && DECL_ASSEMBLER_NAME_SET_P (c))
4263 : 662 : hstate.add_object
4264 : 662 : (IDENTIFIER_HASH_VALUE
4265 : : (DECL_ASSEMBLER_NAME (c)));
4266 : : }
4267 : 211975 : return;
4268 : : default:
4269 : : break;
4270 : : }
4271 : :
4272 : : /* Don't hash the type, that can lead to having nodes which
4273 : : compare equal according to operand_equal_p, but which
4274 : : have different hash codes. */
4275 : 274592655 : if (code == NON_LVALUE_EXPR)
4276 : : {
4277 : : /* Make sure to include signness in the hash computation. */
4278 : 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4279 : 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4280 : : }
4281 : :
4282 : 274592655 : else if (commutative_tree_code (code))
4283 : : {
4284 : : /* It's a commutative expression. We want to hash it the same
4285 : : however it appears. We do this by first hashing both operands
4286 : : and then rehashing based on the order of their independent
4287 : : hashes. */
4288 : 16899967 : inchash::hash one, two;
4289 : 16899967 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4290 : 16899967 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4291 : 16899967 : hstate.add_commutative (one, two);
4292 : : }
4293 : : else
4294 : 720981629 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4295 : 668885479 : hash_operand (TREE_OPERAND (t, i), hstate,
4296 : : i == 0 ? flags : sflags);
4297 : : }
4298 : : return;
4299 : : }
4300 : : }
4301 : :
4302 : : bool
4303 : 6887112450 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4304 : : unsigned int flags, bool *ret)
4305 : : {
4306 : : /* When checking and unless comparing DECL names, verify that if
4307 : : the outermost operand_equal_p call returns non-zero then ARG0
4308 : : and ARG1 have the same hash value. */
4309 : 6887112450 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4310 : : {
4311 : 2892076434 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4312 : : {
4313 : 435559073 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4314 : : {
4315 : 81037982 : inchash::hash hstate0 (0), hstate1 (0);
4316 : 81037982 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4317 : 81037982 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4318 : 81037982 : hashval_t h0 = hstate0.end ();
4319 : 81037982 : hashval_t h1 = hstate1.end ();
4320 : 81037982 : gcc_assert (h0 == h1);
4321 : : }
4322 : 435559073 : *ret = true;
4323 : : }
4324 : : else
4325 : 2456517361 : *ret = false;
4326 : :
4327 : 2892076434 : return true;
4328 : : }
4329 : :
4330 : : return false;
4331 : : }
4332 : :
4333 : :
4334 : : static operand_compare default_compare_instance;
4335 : :
4336 : : /* Conveinece wrapper around operand_compare class because usually we do
4337 : : not need to play with the valueizer. */
4338 : :
4339 : : bool
4340 : 2889996004 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4341 : : {
4342 : 2889996004 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4343 : : }
4344 : :
4345 : : namespace inchash
4346 : : {
4347 : :
4348 : : /* Generate a hash value for an expression. This can be used iteratively
4349 : : by passing a previous result as the HSTATE argument.
4350 : :
4351 : : This function is intended to produce the same hash for expressions which
4352 : : would compare equal using operand_equal_p. */
4353 : : void
4354 : 1902552064 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4355 : : {
4356 : 1902552064 : default_compare_instance.hash_operand (t, hstate, flags);
4357 : 1902552064 : }
4358 : :
4359 : : }
4360 : :
4361 : : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4362 : : with a different signedness or a narrower precision. */
4363 : :
4364 : : static bool
4365 : 17505496 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4366 : : {
4367 : 17505496 : if (operand_equal_p (arg0, arg1, 0))
4368 : : return true;
4369 : :
4370 : 33425986 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4371 : 28449295 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4372 : : return false;
4373 : :
4374 : : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4375 : : and see if the inner values are the same. This removes any
4376 : : signedness comparison, which doesn't matter here. */
4377 : 5651696 : tree op0 = arg0;
4378 : 5651696 : tree op1 = arg1;
4379 : 5651696 : STRIP_NOPS (op0);
4380 : 5651696 : STRIP_NOPS (op1);
4381 : 5651696 : if (operand_equal_p (op0, op1, 0))
4382 : : return true;
4383 : :
4384 : : /* Discard a single widening conversion from ARG1 and see if the inner
4385 : : value is the same as ARG0. */
4386 : 4701239 : if (CONVERT_EXPR_P (arg1)
4387 : 764103 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4388 : 764057 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4389 : 764057 : < TYPE_PRECISION (TREE_TYPE (arg1))
4390 : 5762613 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4391 : : return true;
4392 : :
4393 : : return false;
4394 : : }
4395 : :
4396 : : /* See if ARG is an expression that is either a comparison or is performing
4397 : : arithmetic on comparisons. The comparisons must only be comparing
4398 : : two different values, which will be stored in *CVAL1 and *CVAL2; if
4399 : : they are nonzero it means that some operands have already been found.
4400 : : No variables may be used anywhere else in the expression except in the
4401 : : comparisons.
4402 : :
4403 : : If this is true, return 1. Otherwise, return zero. */
4404 : :
4405 : : static bool
4406 : 54300449 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4407 : : {
4408 : 57901454 : enum tree_code code = TREE_CODE (arg);
4409 : 57901454 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4410 : :
4411 : : /* We can handle some of the tcc_expression cases here. */
4412 : 57901454 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4413 : : tclass = tcc_unary;
4414 : 57320766 : else if (tclass == tcc_expression
4415 : 586496 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4416 : 586496 : || code == COMPOUND_EXPR))
4417 : : tclass = tcc_binary;
4418 : :
4419 : 57310011 : switch (tclass)
4420 : : {
4421 : 3601005 : case tcc_unary:
4422 : 3601005 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4423 : :
4424 : 5010097 : case tcc_binary:
4425 : 5010097 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4426 : 5010097 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4427 : :
4428 : : case tcc_constant:
4429 : : return true;
4430 : :
4431 : 575741 : case tcc_expression:
4432 : 575741 : if (code == COND_EXPR)
4433 : 765 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4434 : 765 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4435 : 829 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4436 : : return false;
4437 : :
4438 : 585260 : case tcc_comparison:
4439 : : /* First see if we can handle the first operand, then the second. For
4440 : : the second operand, we know *CVAL1 can't be zero. It must be that
4441 : : one side of the comparison is each of the values; test for the
4442 : : case where this isn't true by failing if the two operands
4443 : : are the same. */
4444 : :
4445 : 585260 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4446 : 585260 : TREE_OPERAND (arg, 1), 0))
4447 : : return false;
4448 : :
4449 : 585260 : if (*cval1 == 0)
4450 : 583208 : *cval1 = TREE_OPERAND (arg, 0);
4451 : 2052 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4452 : : ;
4453 : 1933 : else if (*cval2 == 0)
4454 : 0 : *cval2 = TREE_OPERAND (arg, 0);
4455 : 1933 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4456 : : ;
4457 : : else
4458 : : return false;
4459 : :
4460 : 583327 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4461 : : ;
4462 : 583327 : else if (*cval2 == 0)
4463 : 583208 : *cval2 = TREE_OPERAND (arg, 1);
4464 : 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4465 : : ;
4466 : : else
4467 : : return false;
4468 : :
4469 : : return true;
4470 : :
4471 : : default:
4472 : : return false;
4473 : : }
4474 : : }
4475 : :
4476 : : /* ARG is a tree that is known to contain just arithmetic operations and
4477 : : comparisons. Evaluate the operations in the tree substituting NEW0 for
4478 : : any occurrence of OLD0 as an operand of a comparison and likewise for
4479 : : NEW1 and OLD1. */
4480 : :
4481 : : static tree
4482 : 702 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4483 : : tree old1, tree new1)
4484 : : {
4485 : 702 : tree type = TREE_TYPE (arg);
4486 : 702 : enum tree_code code = TREE_CODE (arg);
4487 : 702 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4488 : :
4489 : : /* We can handle some of the tcc_expression cases here. */
4490 : 702 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4491 : : tclass = tcc_unary;
4492 : 702 : else if (tclass == tcc_expression
4493 : 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4494 : : tclass = tcc_binary;
4495 : :
4496 : 693 : switch (tclass)
4497 : : {
4498 : 165 : case tcc_unary:
4499 : 165 : return fold_build1_loc (loc, code, type,
4500 : 165 : eval_subst (loc, TREE_OPERAND (arg, 0),
4501 : 165 : old0, new0, old1, new1));
4502 : :
4503 : 168 : case tcc_binary:
4504 : 336 : return fold_build2_loc (loc, code, type,
4505 : 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4506 : : old0, new0, old1, new1),
4507 : 168 : eval_subst (loc, TREE_OPERAND (arg, 1),
4508 : 168 : old0, new0, old1, new1));
4509 : :
4510 : 9 : case tcc_expression:
4511 : 9 : switch (code)
4512 : : {
4513 : 0 : case SAVE_EXPR:
4514 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4515 : 0 : old1, new1);
4516 : :
4517 : 0 : case COMPOUND_EXPR:
4518 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4519 : 0 : old1, new1);
4520 : :
4521 : 9 : case COND_EXPR:
4522 : 27 : return fold_build3_loc (loc, code, type,
4523 : 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4524 : : old0, new0, old1, new1),
4525 : 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4526 : : old0, new0, old1, new1),
4527 : 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4528 : 9 : old0, new0, old1, new1));
4529 : : default:
4530 : : break;
4531 : : }
4532 : : /* Fall through - ??? */
4533 : :
4534 : 180 : case tcc_comparison:
4535 : 180 : {
4536 : 180 : tree arg0 = TREE_OPERAND (arg, 0);
4537 : 180 : tree arg1 = TREE_OPERAND (arg, 1);
4538 : :
4539 : : /* We need to check both for exact equality and tree equality. The
4540 : : former will be true if the operand has a side-effect. In that
4541 : : case, we know the operand occurred exactly once. */
4542 : :
4543 : 180 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4544 : : arg0 = new0;
4545 : 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4546 : : arg0 = new1;
4547 : :
4548 : 180 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4549 : : arg1 = new0;
4550 : 180 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4551 : : arg1 = new1;
4552 : :
4553 : 180 : return fold_build2_loc (loc, code, type, arg0, arg1);
4554 : : }
4555 : :
4556 : : default:
4557 : : return arg;
4558 : : }
4559 : : }
4560 : :
4561 : : /* Return a tree for the case when the result of an expression is RESULT
4562 : : converted to TYPE and OMITTED was previously an operand of the expression
4563 : : but is now not needed (e.g., we folded OMITTED * 0).
4564 : :
4565 : : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4566 : : the conversion of RESULT to TYPE. */
4567 : :
4568 : : tree
4569 : 288116 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4570 : : {
4571 : 288116 : tree t = fold_convert_loc (loc, type, result);
4572 : :
4573 : : /* If the resulting operand is an empty statement, just return the omitted
4574 : : statement casted to void. */
4575 : 288116 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4576 : 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4577 : 0 : fold_ignored_result (omitted));
4578 : :
4579 : 288116 : if (TREE_SIDE_EFFECTS (omitted))
4580 : 13160 : return build2_loc (loc, COMPOUND_EXPR, type,
4581 : 13160 : fold_ignored_result (omitted), t);
4582 : :
4583 : 274956 : return non_lvalue_loc (loc, t);
4584 : : }
4585 : :
4586 : : /* Return a tree for the case when the result of an expression is RESULT
4587 : : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4588 : : of the expression but are now not needed.
4589 : :
4590 : : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4591 : : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4592 : : evaluated before OMITTED2. Otherwise, if neither has side effects,
4593 : : just do the conversion of RESULT to TYPE. */
4594 : :
4595 : : tree
4596 : 7169 : omit_two_operands_loc (location_t loc, tree type, tree result,
4597 : : tree omitted1, tree omitted2)
4598 : : {
4599 : 7169 : tree t = fold_convert_loc (loc, type, result);
4600 : :
4601 : 7169 : if (TREE_SIDE_EFFECTS (omitted2))
4602 : 68 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4603 : 7169 : if (TREE_SIDE_EFFECTS (omitted1))
4604 : 175 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4605 : :
4606 : 7169 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4607 : : }
4608 : :
4609 : :
4610 : : /* Return a simplified tree node for the truth-negation of ARG. This
4611 : : never alters ARG itself. We assume that ARG is an operation that
4612 : : returns a truth value (0 or 1).
4613 : :
4614 : : FIXME: one would think we would fold the result, but it causes
4615 : : problems with the dominator optimizer. */
4616 : :
4617 : : static tree
4618 : 47070350 : fold_truth_not_expr (location_t loc, tree arg)
4619 : : {
4620 : 47070350 : tree type = TREE_TYPE (arg);
4621 : 47070350 : enum tree_code code = TREE_CODE (arg);
4622 : 47070350 : location_t loc1, loc2;
4623 : :
4624 : : /* If this is a comparison, we can simply invert it, except for
4625 : : floating-point non-equality comparisons, in which case we just
4626 : : enclose a TRUTH_NOT_EXPR around what we have. */
4627 : :
4628 : 47070350 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4629 : : {
4630 : 37181309 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4631 : 31359176 : if (FLOAT_TYPE_P (op_type)
4632 : 5832220 : && flag_trapping_math
4633 : 5801769 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4634 : 42943626 : && code != NE_EXPR && code != EQ_EXPR)
4635 : : return NULL_TREE;
4636 : :
4637 : 32107604 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4638 : 32107604 : if (code == ERROR_MARK)
4639 : : return NULL_TREE;
4640 : :
4641 : 32107604 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4642 : 32107604 : TREE_OPERAND (arg, 1));
4643 : 32107604 : copy_warning (ret, arg);
4644 : 32107604 : return ret;
4645 : : }
4646 : :
4647 : 9889041 : switch (code)
4648 : : {
4649 : 0 : case INTEGER_CST:
4650 : 0 : return constant_boolean_node (integer_zerop (arg), type);
4651 : :
4652 : 50865 : case TRUTH_AND_EXPR:
4653 : 50865 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4654 : 50865 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4655 : 101730 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4656 : 50865 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4657 : 101730 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4658 : :
4659 : 2448 : case TRUTH_OR_EXPR:
4660 : 2448 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4661 : 2448 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4662 : 4896 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4663 : 2448 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4664 : 4896 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4665 : :
4666 : 32478 : case TRUTH_XOR_EXPR:
4667 : : /* Here we can invert either operand. We invert the first operand
4668 : : unless the second operand is a TRUTH_NOT_EXPR in which case our
4669 : : result is the XOR of the first operand with the inside of the
4670 : : negation of the second operand. */
4671 : :
4672 : 32478 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4673 : 7 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4674 : 14 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4675 : : else
4676 : 32471 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4677 : 32471 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4678 : 64942 : TREE_OPERAND (arg, 1));
4679 : :
4680 : 249941 : case TRUTH_ANDIF_EXPR:
4681 : 249941 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4682 : 249941 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4683 : 499882 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4684 : 249941 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4685 : 499882 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4686 : :
4687 : 18865 : case TRUTH_ORIF_EXPR:
4688 : 18865 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4689 : 18865 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4690 : 37730 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4691 : 18865 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4692 : 37730 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4693 : :
4694 : 800876 : case TRUTH_NOT_EXPR:
4695 : 800876 : return TREE_OPERAND (arg, 0);
4696 : :
4697 : 7945 : case COND_EXPR:
4698 : 7945 : {
4699 : 7945 : tree arg1 = TREE_OPERAND (arg, 1);
4700 : 7945 : tree arg2 = TREE_OPERAND (arg, 2);
4701 : :
4702 : 7945 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4703 : 7945 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4704 : :
4705 : : /* A COND_EXPR may have a throw as one operand, which
4706 : : then has void type. Just leave void operands
4707 : : as they are. */
4708 : 7945 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4709 : 7945 : VOID_TYPE_P (TREE_TYPE (arg1))
4710 : 7945 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4711 : 7945 : VOID_TYPE_P (TREE_TYPE (arg2))
4712 : 15887 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4713 : : }
4714 : :
4715 : 140 : case COMPOUND_EXPR:
4716 : 140 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4717 : 280 : return build2_loc (loc, COMPOUND_EXPR, type,
4718 : 140 : TREE_OPERAND (arg, 0),
4719 : 280 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4720 : :
4721 : 0 : case NON_LVALUE_EXPR:
4722 : 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4723 : 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4724 : :
4725 : 70476 : CASE_CONVERT:
4726 : 70476 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4727 : 70412 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4728 : :
4729 : : /* fall through */
4730 : :
4731 : 64 : case FLOAT_EXPR:
4732 : 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4733 : 64 : return build1_loc (loc, TREE_CODE (arg), type,
4734 : 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4735 : :
4736 : 461 : case BIT_AND_EXPR:
4737 : 461 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4738 : : return NULL_TREE;
4739 : 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4740 : :
4741 : 2 : case SAVE_EXPR:
4742 : 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4743 : :
4744 : 75 : case CLEANUP_POINT_EXPR:
4745 : 75 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4746 : 75 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4747 : 150 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4748 : :
4749 : : default:
4750 : : return NULL_TREE;
4751 : : }
4752 : : }
4753 : :
4754 : : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4755 : : assume that ARG is an operation that returns a truth value (0 or 1
4756 : : for scalars, 0 or -1 for vectors). Return the folded expression if
4757 : : folding is successful. Otherwise, return NULL_TREE. */
4758 : :
4759 : : static tree
4760 : 1723697 : fold_invert_truthvalue (location_t loc, tree arg)
4761 : : {
4762 : 1723697 : tree type = TREE_TYPE (arg);
4763 : 3447370 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4764 : : ? BIT_NOT_EXPR
4765 : : : TRUTH_NOT_EXPR,
4766 : 1723697 : type, arg);
4767 : : }
4768 : :
4769 : : /* Return a simplified tree node for the truth-negation of ARG. This
4770 : : never alters ARG itself. We assume that ARG is an operation that
4771 : : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4772 : :
4773 : : tree
4774 : 39902994 : invert_truthvalue_loc (location_t loc, tree arg)
4775 : : {
4776 : 39902994 : if (TREE_CODE (arg) == ERROR_MARK)
4777 : : return arg;
4778 : :
4779 : 39902994 : tree type = TREE_TYPE (arg);
4780 : 79805988 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4781 : : ? BIT_NOT_EXPR
4782 : : : TRUTH_NOT_EXPR,
4783 : 39902994 : type, arg);
4784 : : }
4785 : :
4786 : : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4787 : : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4788 : : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4789 : : is the original memory reference used to preserve the alias set of
4790 : : the access. */
4791 : :
4792 : : tree
4793 : 755444 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4794 : : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4795 : : int unsignedp, int reversep)
4796 : : {
4797 : 755444 : tree result, bftype;
4798 : :
4799 : : /* Attempt not to lose the access path if possible. */
4800 : 755444 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4801 : : {
4802 : 751840 : tree ninner = TREE_OPERAND (orig_inner, 0);
4803 : 751840 : machine_mode nmode;
4804 : 751840 : poly_int64 nbitsize, nbitpos;
4805 : 751840 : tree noffset;
4806 : 751840 : int nunsignedp, nreversep, nvolatilep = 0;
4807 : 751840 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4808 : : &noffset, &nmode, &nunsignedp,
4809 : : &nreversep, &nvolatilep);
4810 : 751840 : if (base == inner
4811 : 751708 : && noffset == NULL_TREE
4812 : 751708 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4813 : 751684 : && !reversep
4814 : 751612 : && !nreversep
4815 : 1503452 : && !nvolatilep)
4816 : : {
4817 : 751612 : inner = ninner;
4818 : 751840 : bitpos -= nbitpos;
4819 : : }
4820 : : }
4821 : :
4822 : 755444 : alias_set_type iset = get_alias_set (orig_inner);
4823 : 755444 : if (iset == 0 && get_alias_set (inner) != iset)
4824 : 209 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4825 : : build_fold_addr_expr (inner),
4826 : : build_int_cst (ptr_type_node, 0));
4827 : :
4828 : 755444 : if (known_eq (bitpos, 0) && !reversep)
4829 : : {
4830 : 17583 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4831 : 35166 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4832 : 17385 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4833 : 202 : && tree_fits_shwi_p (size)
4834 : 17785 : && tree_to_shwi (size) == bitsize)
4835 : 179 : return fold_convert_loc (loc, type, inner);
4836 : : }
4837 : :
4838 : 755265 : bftype = type;
4839 : 755265 : if (TYPE_PRECISION (bftype) != bitsize
4840 : 755265 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4841 : 365 : bftype = build_nonstandard_integer_type (bitsize, 0);
4842 : :
4843 : 755265 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4844 : 755265 : bitsize_int (bitsize), bitsize_int (bitpos));
4845 : 755265 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4846 : :
4847 : 755265 : if (bftype != type)
4848 : 365 : result = fold_convert_loc (loc, type, result);
4849 : :
4850 : : return result;
4851 : : }
4852 : :
4853 : : /* Optimize a bit-field compare.
4854 : :
4855 : : There are two cases: First is a compare against a constant and the
4856 : : second is a comparison of two items where the fields are at the same
4857 : : bit position relative to the start of a chunk (byte, halfword, word)
4858 : : large enough to contain it. In these cases we can avoid the shift
4859 : : implicit in bitfield extractions.
4860 : :
4861 : : For constants, we emit a compare of the shifted constant with the
4862 : : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4863 : : compared. For two fields at the same position, we do the ANDs with the
4864 : : similar mask and compare the result of the ANDs.
4865 : :
4866 : : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4867 : : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4868 : : are the left and right operands of the comparison, respectively.
4869 : :
4870 : : If the optimization described above can be done, we return the resulting
4871 : : tree. Otherwise we return zero. */
4872 : :
4873 : : static tree
4874 : 4029031 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4875 : : tree compare_type, tree lhs, tree rhs)
4876 : : {
4877 : 4029031 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4878 : 4029031 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4879 : 4029031 : tree type = TREE_TYPE (lhs);
4880 : 4029031 : tree unsigned_type;
4881 : 4029031 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4882 : 4029031 : machine_mode lmode, rmode;
4883 : 4029031 : scalar_int_mode nmode;
4884 : 4029031 : int lunsignedp, runsignedp;
4885 : 4029031 : int lreversep, rreversep;
4886 : 4029031 : int lvolatilep = 0, rvolatilep = 0;
4887 : 4029031 : tree linner, rinner = NULL_TREE;
4888 : 4029031 : tree mask;
4889 : 4029031 : tree offset;
4890 : :
4891 : : /* Get all the information about the extractions being done. If the bit size
4892 : : is the same as the size of the underlying object, we aren't doing an
4893 : : extraction at all and so can do nothing. We also don't want to
4894 : : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4895 : : then will no longer be able to replace it. */
4896 : 4029031 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4897 : : &lunsignedp, &lreversep, &lvolatilep);
4898 : 4029031 : if (linner == lhs
4899 : 4029031 : || !known_size_p (plbitsize)
4900 : 4029031 : || !plbitsize.is_constant (&lbitsize)
4901 : 4029031 : || !plbitpos.is_constant (&lbitpos)
4902 : 8058062 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4903 : 754096 : || offset != 0
4904 : 754071 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4905 : 4783102 : || lvolatilep)
4906 : 3275020 : return 0;
4907 : :
4908 : 754011 : if (const_p)
4909 : 740144 : rreversep = lreversep;
4910 : : else
4911 : : {
4912 : : /* If this is not a constant, we can only do something if bit positions,
4913 : : sizes, signedness and storage order are the same. */
4914 : 13867 : rinner
4915 : 13867 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4916 : : &runsignedp, &rreversep, &rvolatilep);
4917 : :
4918 : 13867 : if (rinner == rhs
4919 : 13703 : || maybe_ne (lbitpos, rbitpos)
4920 : 13669 : || maybe_ne (lbitsize, rbitsize)
4921 : 13669 : || lunsignedp != runsignedp
4922 : 13669 : || lreversep != rreversep
4923 : 13669 : || offset != 0
4924 : 13669 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4925 : 27536 : || rvolatilep)
4926 : : return 0;
4927 : : }
4928 : :
4929 : : /* Honor the C++ memory model and mimic what RTL expansion does. */
4930 : 753813 : poly_uint64 bitstart = 0;
4931 : 753813 : poly_uint64 bitend = 0;
4932 : 753813 : if (TREE_CODE (lhs) == COMPONENT_REF)
4933 : : {
4934 : 753813 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4935 : 753813 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4936 : : return 0;
4937 : : }
4938 : :
4939 : : /* See if we can find a mode to refer to this field. We should be able to,
4940 : : but fail if we can't. */
4941 : 1507626 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4942 : 740144 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4943 : 13669 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4944 : : TYPE_ALIGN (TREE_TYPE (rinner))),
4945 : 753813 : BITS_PER_WORD, false, &nmode))
4946 : : return 0;
4947 : :
4948 : : /* Set signed and unsigned types of the precision of this mode for the
4949 : : shifts below. */
4950 : 751812 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4951 : :
4952 : : /* Compute the bit position and size for the new reference and our offset
4953 : : within it. If the new reference is the same size as the original, we
4954 : : won't optimize anything, so return zero. */
4955 : 751812 : nbitsize = GET_MODE_BITSIZE (nmode);
4956 : 751812 : nbitpos = lbitpos & ~ (nbitsize - 1);
4957 : 751812 : lbitpos -= nbitpos;
4958 : 751812 : if (nbitsize == lbitsize)
4959 : : return 0;
4960 : :
4961 : 740260 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4962 : 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4963 : :
4964 : : /* Make the mask to be used against the extracted field. */
4965 : 740260 : mask = build_int_cst_type (unsigned_type, -1);
4966 : 740260 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4967 : 740260 : mask = const_binop (RSHIFT_EXPR, mask,
4968 : 740260 : size_int (nbitsize - lbitsize - lbitpos));
4969 : :
4970 : 740260 : if (! const_p)
4971 : : {
4972 : 10834 : if (nbitpos < 0)
4973 : : return 0;
4974 : :
4975 : : /* If not comparing with constant, just rework the comparison
4976 : : and return. */
4977 : 10834 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4978 : 10834 : nbitsize, nbitpos, 1, lreversep);
4979 : 10834 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4980 : 10834 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4981 : 10834 : nbitsize, nbitpos, 1, rreversep);
4982 : 10834 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4983 : 10834 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4984 : : }
4985 : :
4986 : : /* Otherwise, we are handling the constant case. See if the constant is too
4987 : : big for the field. Warn and return a tree for 0 (false) if so. We do
4988 : : this not only for its own sake, but to avoid having to test for this
4989 : : error case below. If we didn't, we might generate wrong code.
4990 : :
4991 : : For unsigned fields, the constant shifted right by the field length should
4992 : : be all zero. For signed fields, the high-order bits should agree with
4993 : : the sign bit. */
4994 : :
4995 : 729426 : if (lunsignedp)
4996 : : {
4997 : 728307 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4998 : : {
4999 : 0 : warning (0, "comparison is always %d due to width of bit-field",
5000 : : code == NE_EXPR);
5001 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5002 : : }
5003 : : }
5004 : : else
5005 : : {
5006 : 1119 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5007 : 1119 : if (tem != 0 && tem != -1)
5008 : : {
5009 : 0 : warning (0, "comparison is always %d due to width of bit-field",
5010 : : code == NE_EXPR);
5011 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5012 : : }
5013 : 1119 : }
5014 : :
5015 : 729426 : if (nbitpos < 0)
5016 : : return 0;
5017 : :
5018 : : /* Single-bit compares should always be against zero. */
5019 : 729426 : if (lbitsize == 1 && ! integer_zerop (rhs))
5020 : : {
5021 : 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5022 : 175 : rhs = build_int_cst (type, 0);
5023 : : }
5024 : :
5025 : : /* Make a new bitfield reference, shift the constant over the
5026 : : appropriate number of bits and mask it with the computed mask
5027 : : (in case this was a signed field). If we changed it, make a new one. */
5028 : 729426 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5029 : 729426 : nbitsize, nbitpos, 1, lreversep);
5030 : :
5031 : 729426 : rhs = const_binop (BIT_AND_EXPR,
5032 : : const_binop (LSHIFT_EXPR,
5033 : : fold_convert_loc (loc, unsigned_type, rhs),
5034 : 729426 : size_int (lbitpos)),
5035 : : mask);
5036 : :
5037 : 729426 : lhs = build2_loc (loc, code, compare_type,
5038 : : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5039 : 729426 : return lhs;
5040 : : }
5041 : :
5042 : : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5043 : : represents the sign bit of EXP's type. If EXP represents a sign
5044 : : or zero extension, also test VAL against the unextended type.
5045 : : The return value is the (sub)expression whose sign bit is VAL,
5046 : : or NULL_TREE otherwise. */
5047 : :
5048 : : tree
5049 : 2150 : sign_bit_p (tree exp, const_tree val)
5050 : : {
5051 : 2150 : int width;
5052 : 2150 : tree t;
5053 : :
5054 : : /* Tree EXP must have an integral type. */
5055 : 2150 : t = TREE_TYPE (exp);
5056 : 2150 : if (! INTEGRAL_TYPE_P (t))
5057 : : return NULL_TREE;
5058 : :
5059 : : /* Tree VAL must be an integer constant. */
5060 : 1818 : if (TREE_CODE (val) != INTEGER_CST
5061 : 1818 : || TREE_OVERFLOW (val))
5062 : : return NULL_TREE;
5063 : :
5064 : 1444 : width = TYPE_PRECISION (t);
5065 : 1444 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5066 : : return exp;
5067 : :
5068 : : /* Handle extension from a narrower type. */
5069 : 807 : if (TREE_CODE (exp) == NOP_EXPR
5070 : 807 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5071 : 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5072 : :
5073 : : return NULL_TREE;
5074 : : }
5075 : :
5076 : : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5077 : : operand is simple enough to be evaluated unconditionally. */
5078 : :
5079 : : static bool
5080 : 62963460 : simple_operand_p (const_tree exp)
5081 : : {
5082 : : /* Strip any conversions that don't change the machine mode. */
5083 : 62963460 : STRIP_NOPS (exp);
5084 : :
5085 : 62963460 : return (CONSTANT_CLASS_P (exp)
5086 : 43555626 : || TREE_CODE (exp) == SSA_NAME
5087 : 77197855 : || (DECL_P (exp)
5088 : 4493646 : && ! TREE_ADDRESSABLE (exp)
5089 : 4408589 : && ! TREE_THIS_VOLATILE (exp)
5090 : 4408589 : && ! DECL_NONLOCAL (exp)
5091 : : /* Don't regard global variables as simple. They may be
5092 : : allocated in ways unknown to the compiler (shared memory,
5093 : : #pragma weak, etc). */
5094 : 4407137 : && ! TREE_PUBLIC (exp)
5095 : 4386523 : && ! DECL_EXTERNAL (exp)
5096 : : /* DECL_VALUE_EXPR will expand to something non-simple. */
5097 : 4386523 : && ! ((VAR_P (exp)
5098 : : || TREE_CODE (exp) == PARM_DECL
5099 : : || TREE_CODE (exp) == RESULT_DECL)
5100 : 4386523 : && DECL_HAS_VALUE_EXPR_P (exp))
5101 : : /* Weakrefs are not safe to be read, since they can be NULL.
5102 : : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5103 : : have DECL_WEAK flag set. */
5104 : 4385932 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5105 : : /* Loading a static variable is unduly expensive, but global
5106 : : registers aren't expensive. */
5107 : 4385932 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5108 : : }
5109 : :
5110 : : /* Determine if an operand is simple enough to be evaluated unconditionally.
5111 : : In addition to simple_operand_p, we assume that comparisons, conversions,
5112 : : and logic-not operations are simple, if their operands are simple, too. */
5113 : :
5114 : : bool
5115 : 5808921 : simple_condition_p (tree exp)
5116 : : {
5117 : 5878086 : enum tree_code code;
5118 : :
5119 : 5878086 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5120 : 4208237 : return false;
5121 : :
5122 : 1678965 : while (CONVERT_EXPR_P (exp))
5123 : 9116 : exp = TREE_OPERAND (exp, 0);
5124 : :
5125 : 1669849 : code = TREE_CODE (exp);
5126 : :
5127 : 1669849 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5128 : 1277632 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5129 : 1277632 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5130 : :
5131 : 392217 : if (code == TRUTH_NOT_EXPR)
5132 : 69165 : return simple_condition_p (TREE_OPERAND (exp, 0));
5133 : :
5134 : 323052 : return simple_operand_p (exp);
5135 : : }
5136 : :
5137 : :
5138 : : /* The following functions are subroutines to fold_range_test and allow it to
5139 : : try to change a logical combination of comparisons into a range test.
5140 : :
5141 : : For example, both
5142 : : X == 2 || X == 3 || X == 4 || X == 5
5143 : : and
5144 : : X >= 2 && X <= 5
5145 : : are converted to
5146 : : (unsigned) (X - 2) <= 3
5147 : :
5148 : : We describe each set of comparisons as being either inside or outside
5149 : : a range, using a variable named like IN_P, and then describe the
5150 : : range with a lower and upper bound. If one of the bounds is omitted,
5151 : : it represents either the highest or lowest value of the type.
5152 : :
5153 : : In the comments below, we represent a range by two numbers in brackets
5154 : : preceded by a "+" to designate being inside that range, or a "-" to
5155 : : designate being outside that range, so the condition can be inverted by
5156 : : flipping the prefix. An omitted bound is represented by a "-". For
5157 : : example, "- [-, 10]" means being outside the range starting at the lowest
5158 : : possible value and ending at 10, in other words, being greater than 10.
5159 : : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5160 : : always false.
5161 : :
5162 : : We set up things so that the missing bounds are handled in a consistent
5163 : : manner so neither a missing bound nor "true" and "false" need to be
5164 : : handled using a special case. */
5165 : :
5166 : : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5167 : : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5168 : : and UPPER1_P are nonzero if the respective argument is an upper bound
5169 : : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5170 : : must be specified for a comparison. ARG1 will be converted to ARG0's
5171 : : type if both are specified. */
5172 : :
5173 : : static tree
5174 : 22079878 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5175 : : tree arg1, int upper1_p)
5176 : : {
5177 : 22079878 : tree tem;
5178 : 22079878 : int result;
5179 : 22079878 : int sgn0, sgn1;
5180 : :
5181 : : /* If neither arg represents infinity, do the normal operation.
5182 : : Else, if not a comparison, return infinity. Else handle the special
5183 : : comparison rules. Note that most of the cases below won't occur, but
5184 : : are handled for consistency. */
5185 : :
5186 : 22079878 : if (arg0 != 0 && arg1 != 0)
5187 : : {
5188 : 11347411 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5189 : : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5190 : 11347411 : STRIP_NOPS (tem);
5191 : 11347411 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5192 : : }
5193 : :
5194 : 10732467 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5195 : : return 0;
5196 : :
5197 : : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5198 : : for neither. In real maths, we cannot assume open ended ranges are
5199 : : the same. But, this is computer arithmetic, where numbers are finite.
5200 : : We can therefore make the transformation of any unbounded range with
5201 : : the value Z, Z being greater than any representable number. This permits
5202 : : us to treat unbounded ranges as equal. */
5203 : 10723946 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5204 : 10723946 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5205 : 10723946 : switch (code)
5206 : : {
5207 : 5005669 : case EQ_EXPR:
5208 : 5005669 : result = sgn0 == sgn1;
5209 : 5005669 : break;
5210 : 0 : case NE_EXPR:
5211 : 0 : result = sgn0 != sgn1;
5212 : 0 : break;
5213 : 399190 : case LT_EXPR:
5214 : 399190 : result = sgn0 < sgn1;
5215 : 399190 : break;
5216 : 2468014 : case LE_EXPR:
5217 : 2468014 : result = sgn0 <= sgn1;
5218 : 2468014 : break;
5219 : 2851073 : case GT_EXPR:
5220 : 2851073 : result = sgn0 > sgn1;
5221 : 2851073 : break;
5222 : 0 : case GE_EXPR:
5223 : 0 : result = sgn0 >= sgn1;
5224 : 0 : break;
5225 : 0 : default:
5226 : 0 : gcc_unreachable ();
5227 : : }
5228 : :
5229 : 10723946 : return constant_boolean_node (result, type);
5230 : : }
5231 : :
5232 : : /* Helper routine for make_range. Perform one step for it, return
5233 : : new expression if the loop should continue or NULL_TREE if it should
5234 : : stop. */
5235 : :
5236 : : tree
5237 : 57428088 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5238 : : tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
5239 : : bool *strict_overflow_p)
5240 : : {
5241 : 57428088 : tree arg0_type = TREE_TYPE (arg0);
5242 : 57428088 : tree n_low, n_high, low = *p_low, high = *p_high;
5243 : 57428088 : int in_p = *p_in_p, n_in_p;
5244 : :
5245 : 57428088 : switch (code)
5246 : : {
5247 : 1633816 : case TRUTH_NOT_EXPR:
5248 : : /* We can only do something if the range is testing for zero. */
5249 : 1633816 : if (low == NULL_TREE || high == NULL_TREE
5250 : 1633816 : || ! integer_zerop (low) || ! integer_zerop (high))
5251 : 0 : return NULL_TREE;
5252 : 1633816 : *p_in_p = ! in_p;
5253 : 1633816 : return arg0;
5254 : :
5255 : 45456042 : case EQ_EXPR: case NE_EXPR:
5256 : 45456042 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5257 : : /* We can only do something if the range is testing for zero
5258 : : and if the second operand is an integer constant. Note that
5259 : : saying something is "in" the range we make is done by
5260 : : complementing IN_P since it will set in the initial case of
5261 : : being not equal to zero; "out" is leaving it alone. */
5262 : 45456042 : if (low == NULL_TREE || high == NULL_TREE
5263 : 45456042 : || ! integer_zerop (low) || ! integer_zerop (high)
5264 : 90911997 : || TREE_CODE (arg1) != INTEGER_CST)
5265 : 16813188 : return NULL_TREE;
5266 : :
5267 : 28642854 : switch (code)
5268 : : {
5269 : : case NE_EXPR: /* - [c, c] */
5270 : : low = high = arg1;
5271 : : break;
5272 : 7720651 : case EQ_EXPR: /* + [c, c] */
5273 : 7720651 : in_p = ! in_p, low = high = arg1;
5274 : 7720651 : break;
5275 : 2182223 : case GT_EXPR: /* - [-, c] */
5276 : 2182223 : low = 0, high = arg1;
5277 : 2182223 : break;
5278 : 915524 : case GE_EXPR: /* + [c, -] */
5279 : 915524 : in_p = ! in_p, low = arg1, high = 0;
5280 : 915524 : break;
5281 : 5387233 : case LT_EXPR: /* - [c, -] */
5282 : 5387233 : low = arg1, high = 0;
5283 : 5387233 : break;
5284 : 4443800 : case LE_EXPR: /* + [-, c] */
5285 : 4443800 : in_p = ! in_p, low = 0, high = arg1;
5286 : 4443800 : break;
5287 : 0 : default:
5288 : 0 : gcc_unreachable ();
5289 : : }
5290 : :
5291 : : /* If this is an unsigned comparison, we also know that EXP is
5292 : : greater than or equal to zero. We base the range tests we make
5293 : : on that fact, so we record it here so we can parse existing
5294 : : range tests. We test arg0_type since often the return type
5295 : : of, e.g. EQ_EXPR, is boolean. */
5296 : 28642854 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5297 : : {
5298 : 1923800 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5299 : : in_p, low, high, 1,
5300 : : build_int_cst (arg0_type, 0),
5301 : : NULL_TREE))
5302 : : return NULL_TREE;
5303 : :
5304 : 1923791 : in_p = n_in_p, low = n_low, high = n_high;
5305 : :
5306 : : /* If the high bound is missing, but we have a nonzero low
5307 : : bound, reverse the range so it goes from zero to the low bound
5308 : : minus 1. */
5309 : 1923791 : if (high == 0 && low && ! integer_zerop (low))
5310 : : {
5311 : 878043 : in_p = ! in_p;
5312 : 878043 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5313 : 878043 : build_int_cst (TREE_TYPE (low), 1), 0);
5314 : 878043 : low = build_int_cst (arg0_type, 0);
5315 : : }
5316 : : }
5317 : :
5318 : 28642845 : *p_low = low;
5319 : 28642845 : *p_high = high;
5320 : 28642845 : *p_in_p = in_p;
5321 : 28642845 : return arg0;
5322 : :
5323 : 208 : case NEGATE_EXPR:
5324 : : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5325 : : low and high are non-NULL, then normalize will DTRT. */
5326 : 208 : if (!TYPE_UNSIGNED (arg0_type)
5327 : 208 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5328 : : {
5329 : 95 : if (low == NULL_TREE)
5330 : 12 : low = TYPE_MIN_VALUE (arg0_type);
5331 : 95 : if (high == NULL_TREE)
5332 : 47 : high = TYPE_MAX_VALUE (arg0_type);
5333 : : }
5334 : :
5335 : : /* (-x) IN [a,b] -> x in [-b, -a] */
5336 : 208 : n_low = range_binop (MINUS_EXPR, exp_type,
5337 : : build_int_cst (exp_type, 0),
5338 : : 0, high, 1);
5339 : 208 : n_high = range_binop (MINUS_EXPR, exp_type,
5340 : : build_int_cst (exp_type, 0),
5341 : : 0, low, 0);
5342 : 208 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5343 : : return NULL_TREE;
5344 : 196 : goto normalize;
5345 : :
5346 : 12 : case BIT_NOT_EXPR:
5347 : : /* ~ X -> -X - 1 */
5348 : 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5349 : : build_int_cst (exp_type, 1));
5350 : :
5351 : 798588 : case PLUS_EXPR:
5352 : 798588 : case MINUS_EXPR:
5353 : 798588 : if (TREE_CODE (arg1) != INTEGER_CST)
5354 : : return NULL_TREE;
5355 : :
5356 : : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5357 : : move a constant to the other side. */
5358 : 660438 : if (!TYPE_UNSIGNED (arg0_type)
5359 : 660438 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5360 : : return NULL_TREE;
5361 : :
5362 : : /* If EXP is signed, any overflow in the computation is undefined,
5363 : : so we don't worry about it so long as our computations on
5364 : : the bounds don't overflow. For unsigned, overflow is defined
5365 : : and this is exactly the right thing. */
5366 : 872410 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5367 : : arg0_type, low, 0, arg1, 0);
5368 : 437351 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5369 : : arg0_type, high, 1, arg1, 0);
5370 : 434351 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5371 : 871690 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5372 : : return NULL_TREE;
5373 : :
5374 : 437339 : if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5375 : 20903 : *strict_overflow_p = true;
5376 : :
5377 : 0 : normalize:
5378 : : /* Check for an unsigned range which has wrapped around the maximum
5379 : : value thus making n_high < n_low, and normalize it. */
5380 : 437535 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5381 : : {
5382 : 158987 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5383 : 158987 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5384 : 158987 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5385 : 158987 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5386 : :
5387 : : /* If the range is of the form +/- [ x+1, x ], we won't
5388 : : be able to normalize it. But then, it represents the
5389 : : whole range or the empty set, so make it
5390 : : +/- [ -, - ]. */
5391 : 158987 : if (tree_int_cst_equal (n_low, low)
5392 : 158987 : && tree_int_cst_equal (n_high, high))
5393 : : low = high = 0;
5394 : : else
5395 : 158987 : in_p = ! in_p;
5396 : : }
5397 : : else
5398 : 278548 : low = n_low, high = n_high;
5399 : :
5400 : 437535 : *p_low = low;
5401 : 437535 : *p_high = high;
5402 : 437535 : *p_in_p = in_p;
5403 : 437535 : return arg0;
5404 : :
5405 : 2466446 : CASE_CONVERT:
5406 : 2466446 : case NON_LVALUE_EXPR:
5407 : 2466446 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5408 : : return NULL_TREE;
5409 : :
5410 : 1068835 : if (! INTEGRAL_TYPE_P (arg0_type)
5411 : 1039158 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5412 : 925317 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5413 : : return NULL_TREE;
5414 : :
5415 : 902984 : n_low = low, n_high = high;
5416 : :
5417 : 902984 : if (n_low != 0)
5418 : 796969 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5419 : :
5420 : 902984 : if (n_high != 0)
5421 : 819013 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5422 : :
5423 : : /* If we're converting arg0 from an unsigned type, to exp,
5424 : : a signed type, we will be doing the comparison as unsigned.
5425 : : The tests above have already verified that LOW and HIGH
5426 : : are both positive.
5427 : :
5428 : : So we have to ensure that we will handle large unsigned
5429 : : values the same way that the current signed bounds treat
5430 : : negative values. */
5431 : :
5432 : 902984 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5433 : : {
5434 : 214811 : tree high_positive;
5435 : 214811 : tree equiv_type;
5436 : : /* For fixed-point modes, we need to pass the saturating flag
5437 : : as the 2nd parameter. */
5438 : 214811 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5439 : 0 : equiv_type
5440 : 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5441 : 0 : TYPE_SATURATING (arg0_type));
5442 : 214811 : else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5443 : : equiv_type = arg0_type;
5444 : : else
5445 : 214803 : equiv_type
5446 : 214803 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5447 : :
5448 : : /* A range without an upper bound is, naturally, unbounded.
5449 : : Since convert would have cropped a very large value, use
5450 : : the max value for the destination type. */
5451 : 214811 : high_positive
5452 : 214811 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5453 : 0 : : TYPE_MAX_VALUE (arg0_type);
5454 : :
5455 : 214811 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5456 : 194174 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5457 : : fold_convert_loc (loc, arg0_type,
5458 : : high_positive),
5459 : : build_int_cst (arg0_type, 1));
5460 : :
5461 : : /* If the low bound is specified, "and" the range with the
5462 : : range for which the original unsigned value will be
5463 : : positive. */
5464 : 214811 : if (low != 0)
5465 : : {
5466 : 114047 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5467 : : 1, fold_convert_loc (loc, arg0_type,
5468 : : integer_zero_node),
5469 : : high_positive))
5470 : : return NULL_TREE;
5471 : :
5472 : 114047 : in_p = (n_in_p == in_p);
5473 : : }
5474 : : else
5475 : : {
5476 : : /* Otherwise, "or" the range with the range of the input
5477 : : that will be interpreted as negative. */
5478 : 100764 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5479 : : 1, fold_convert_loc (loc, arg0_type,
5480 : : integer_zero_node),
5481 : : high_positive))
5482 : : return NULL_TREE;
5483 : :
5484 : 100764 : in_p = (in_p != n_in_p);
5485 : : }
5486 : : }
5487 : :
5488 : : /* Otherwise, if we are converting arg0 from signed type, to exp,
5489 : : an unsigned type, we will do the comparison as signed. If
5490 : : high is non-NULL, we punt above if it doesn't fit in the signed
5491 : : type, so if we get through here, +[-, high] or +[low, high] are
5492 : : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5493 : : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5494 : : high is not, the +[low, -] range is equivalent to union of
5495 : : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5496 : : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5497 : : low being 0, which should be treated as [-, -]. */
5498 : 688173 : else if (TYPE_UNSIGNED (exp_type)
5499 : 670236 : && !TYPE_UNSIGNED (arg0_type)
5500 : 384122 : && low
5501 : 1072295 : && !high)
5502 : : {
5503 : 12 : if (integer_zerop (low))
5504 : 12 : n_low = NULL_TREE;
5505 : : else
5506 : : {
5507 : 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5508 : : n_low, build_int_cst (arg0_type, -1));
5509 : 0 : n_low = build_zero_cst (arg0_type);
5510 : 0 : in_p = !in_p;
5511 : : }
5512 : : }
5513 : :
5514 : 902984 : *p_low = n_low;
5515 : 902984 : *p_high = n_high;
5516 : 902984 : *p_in_p = in_p;
5517 : 902984 : return arg0;
5518 : :
5519 : : default:
5520 : : return NULL_TREE;
5521 : : }
5522 : : }
5523 : :
5524 : : /* Given EXP, a logical expression, set the range it is testing into
5525 : : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5526 : : actually being tested. *PLOW and *PHIGH will be made of the same
5527 : : type as the returned expression. If EXP is not a comparison, we
5528 : : will most likely not be returning a useful value and range. Set
5529 : : *STRICT_OVERFLOW_P to true if the return value is only valid
5530 : : because signed overflow is undefined; otherwise, do not change
5531 : : *STRICT_OVERFLOW_P. */
5532 : :
5533 : : tree
5534 : 47807974 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5535 : : bool *strict_overflow_p)
5536 : : {
5537 : 47807974 : enum tree_code code;
5538 : 47807974 : tree arg0, arg1 = NULL_TREE;
5539 : 47807974 : tree exp_type, nexp;
5540 : 47807974 : int in_p;
5541 : 47807974 : tree low, high;
5542 : 47807974 : location_t loc = EXPR_LOCATION (exp);
5543 : :
5544 : : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5545 : : and see if we can refine the range. Some of the cases below may not
5546 : : happen, but it doesn't seem worth worrying about this. We "continue"
5547 : : the outer loop when we've changed something; otherwise we "break"
5548 : : the switch, which will "break" the while. */
5549 : :
5550 : 47807974 : in_p = 0;
5551 : 47807974 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5552 : :
5553 : 76519412 : while (1)
5554 : : {
5555 : 76519412 : code = TREE_CODE (exp);
5556 : 76519412 : exp_type = TREE_TYPE (exp);
5557 : 76519412 : arg0 = NULL_TREE;
5558 : :
5559 : 76519412 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5560 : : {
5561 : 52829396 : if (TREE_OPERAND_LENGTH (exp) > 0)
5562 : 52829396 : arg0 = TREE_OPERAND (exp, 0);
5563 : 52829396 : if (TREE_CODE_CLASS (code) == tcc_binary
5564 : 49665291 : || TREE_CODE_CLASS (code) == tcc_comparison
5565 : 60943459 : || (TREE_CODE_CLASS (code) == tcc_expression
5566 : 2639574 : && TREE_OPERAND_LENGTH (exp) > 1))
5567 : 45706659 : arg1 = TREE_OPERAND (exp, 1);
5568 : : }
5569 : 52829396 : if (arg0 == NULL_TREE)
5570 : : break;
5571 : :
5572 : 52829382 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5573 : : &high, &in_p, strict_overflow_p);
5574 : 52829382 : if (nexp == NULL_TREE)
5575 : : break;
5576 : : exp = nexp;
5577 : : }
5578 : :
5579 : : /* If EXP is a constant, we can evaluate whether this is true or false. */
5580 : 47807974 : if (TREE_CODE (exp) == INTEGER_CST)
5581 : : {
5582 : 33878 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5583 : : exp, 0, low, 0))
5584 : 33878 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5585 : : exp, 1, high, 1)));
5586 : 33878 : low = high = 0;
5587 : 33878 : exp = 0;
5588 : : }
5589 : :
5590 : 47807974 : *pin_p = in_p, *plow = low, *phigh = high;
5591 : 47807974 : return exp;
5592 : : }
5593 : :
5594 : : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5595 : : a bitwise check i.e. when
5596 : : LOW == 0xXX...X00...0
5597 : : HIGH == 0xXX...X11...1
5598 : : Return corresponding mask in MASK and stem in VALUE. */
5599 : :
5600 : : static bool
5601 : 131 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5602 : : tree *value)
5603 : : {
5604 : 131 : if (TREE_CODE (low) != INTEGER_CST
5605 : 131 : || TREE_CODE (high) != INTEGER_CST)
5606 : : return false;
5607 : :
5608 : 131 : unsigned prec = TYPE_PRECISION (type);
5609 : 131 : wide_int lo = wi::to_wide (low, prec);
5610 : 131 : wide_int hi = wi::to_wide (high, prec);
5611 : :
5612 : 131 : wide_int end_mask = lo ^ hi;
5613 : 262 : if ((end_mask & (end_mask + 1)) != 0
5614 : 241 : || (lo & end_mask) != 0)
5615 : : return false;
5616 : :
5617 : 86 : wide_int stem_mask = ~end_mask;
5618 : 86 : wide_int stem = lo & stem_mask;
5619 : 86 : if (stem != (hi & stem_mask))
5620 : : return false;
5621 : :
5622 : 86 : *mask = wide_int_to_tree (type, stem_mask);
5623 : 86 : *value = wide_int_to_tree (type, stem);
5624 : :
5625 : 86 : return true;
5626 : 217 : }
5627 : :
5628 : : /* Helper routine for build_range_check and match.pd. Return the type to
5629 : : perform the check or NULL if it shouldn't be optimized. */
5630 : :
5631 : : tree
5632 : 546879 : range_check_type (tree etype)
5633 : : {
5634 : : /* First make sure that arithmetics in this type is valid, then make sure
5635 : : that it wraps around. */
5636 : 546879 : if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5637 : 62739 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5638 : :
5639 : 546879 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5640 : : {
5641 : 401076 : tree utype, minv, maxv;
5642 : :
5643 : : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5644 : : for the type in question, as we rely on this here. */
5645 : 401076 : utype = unsigned_type_for (etype);
5646 : 401076 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5647 : 401076 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5648 : 401076 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5649 : 401076 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5650 : :
5651 : 401076 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5652 : : minv, 1, maxv, 1)))
5653 : : etype = utype;
5654 : : else
5655 : 0 : return NULL_TREE;
5656 : : }
5657 : 145803 : else if (POINTER_TYPE_P (etype)
5658 : : || TREE_CODE (etype) == OFFSET_TYPE
5659 : : /* Right now all BITINT_TYPEs satisfy
5660 : : (unsigned) max + 1 == (unsigned) min, so no need to verify
5661 : : that like for INTEGER_TYPEs. */
5662 : : || TREE_CODE (etype) == BITINT_TYPE)
5663 : 1357 : etype = unsigned_type_for (etype);
5664 : : return etype;
5665 : : }
5666 : :
5667 : : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5668 : : type, TYPE, return an expression to test if EXP is in (or out of, depending
5669 : : on IN_P) the range. Return 0 if the test couldn't be created. */
5670 : :
5671 : : tree
5672 : 1447201 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5673 : : tree low, tree high)
5674 : : {
5675 : 2506171 : tree etype = TREE_TYPE (exp), mask, value;
5676 : :
5677 : : /* Disable this optimization for function pointer expressions
5678 : : on targets that require function pointer canonicalization. */
5679 : 2506171 : if (targetm.have_canonicalize_funcptr_for_compare ()
5680 : 0 : && POINTER_TYPE_P (etype)
5681 : 2506171 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5682 : : return NULL_TREE;
5683 : :
5684 : 2506171 : if (! in_p)
5685 : : {
5686 : 347870 : value = build_range_check (loc, type, exp, 1, low, high);
5687 : 347870 : if (value != 0)
5688 : 347870 : return invert_truthvalue_loc (loc, value);
5689 : :
5690 : : return 0;
5691 : : }
5692 : :
5693 : 2158301 : if (low == 0 && high == 0)
5694 : 137015 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5695 : :
5696 : 2021286 : if (low == 0)
5697 : 697665 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5698 : 697665 : fold_convert_loc (loc, etype, high));
5699 : :
5700 : 1323621 : if (high == 0)
5701 : 76819 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5702 : 76819 : fold_convert_loc (loc, etype, low));
5703 : :
5704 : 1246802 : if (operand_equal_p (low, high, 0))
5705 : 187636 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5706 : 187636 : fold_convert_loc (loc, etype, low));
5707 : :
5708 : 1059166 : if (TREE_CODE (exp) == BIT_AND_EXPR
5709 : 1059166 : && maskable_range_p (low, high, etype, &mask, &value))
5710 : 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5711 : : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5712 : : exp, mask),
5713 : 86 : value);
5714 : :
5715 : 1059080 : if (integer_zerop (low))
5716 : : {
5717 : 602768 : if (! TYPE_UNSIGNED (etype))
5718 : : {
5719 : 126937 : etype = unsigned_type_for (etype);
5720 : 126937 : high = fold_convert_loc (loc, etype, high);
5721 : 126937 : exp = fold_convert_loc (loc, etype, exp);
5722 : : }
5723 : 602768 : return build_range_check (loc, type, exp, 1, 0, high);
5724 : : }
5725 : :
5726 : : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5727 : 456312 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5728 : : {
5729 : 105824 : int prec = TYPE_PRECISION (etype);
5730 : :
5731 : 105824 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5732 : : {
5733 : 110 : if (TYPE_UNSIGNED (etype))
5734 : : {
5735 : 104 : tree signed_etype = signed_type_for (etype);
5736 : 104 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5737 : 0 : etype
5738 : 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5739 : : else
5740 : : etype = signed_etype;
5741 : 104 : exp = fold_convert_loc (loc, etype, exp);
5742 : : }
5743 : 110 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5744 : : build_int_cst (etype, 0));
5745 : : }
5746 : : }
5747 : :
5748 : : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5749 : : This requires wrap-around arithmetics for the type of the expression. */
5750 : 456202 : etype = range_check_type (etype);
5751 : 456202 : if (etype == NULL_TREE)
5752 : : return NULL_TREE;
5753 : :
5754 : 456202 : high = fold_convert_loc (loc, etype, high);
5755 : 456202 : low = fold_convert_loc (loc, etype, low);
5756 : 456202 : exp = fold_convert_loc (loc, etype, exp);
5757 : :
5758 : 456202 : value = const_binop (MINUS_EXPR, high, low);
5759 : :
5760 : 456202 : if (value != 0 && !TREE_OVERFLOW (value))
5761 : 456202 : return build_range_check (loc, type,
5762 : : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5763 : : 1, build_int_cst (etype, 0), value);
5764 : :
5765 : : return 0;
5766 : : }
5767 : :
5768 : : /* Return the predecessor of VAL in its type, handling the infinite case. */
5769 : :
5770 : : static tree
5771 : 164230 : range_predecessor (tree val)
5772 : : {
5773 : 164230 : tree type = TREE_TYPE (val);
5774 : :
5775 : 164230 : if (INTEGRAL_TYPE_P (type)
5776 : 164230 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5777 : : return 0;
5778 : : else
5779 : 164230 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5780 : 164230 : build_int_cst (TREE_TYPE (val), 1), 0);
5781 : : }
5782 : :
5783 : : /* Return the successor of VAL in its type, handling the infinite case. */
5784 : :
5785 : : static tree
5786 : 1535076 : range_successor (tree val)
5787 : : {
5788 : 1535076 : tree type = TREE_TYPE (val);
5789 : :
5790 : 1535076 : if (INTEGRAL_TYPE_P (type)
5791 : 1535076 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5792 : : return 0;
5793 : : else
5794 : 1535067 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5795 : 1535067 : build_int_cst (TREE_TYPE (val), 1), 0);
5796 : : }
5797 : :
5798 : : /* Given two ranges, see if we can merge them into one. Return 1 if we
5799 : : can, 0 if we can't. Set the output range into the specified parameters. */
5800 : :
5801 : : bool
5802 : 3443560 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5803 : : tree high0, int in1_p, tree low1, tree high1)
5804 : : {
5805 : 3443560 : bool no_overlap;
5806 : 3443560 : int subset;
5807 : 3443560 : int temp;
5808 : 3443560 : tree tem;
5809 : 3443560 : int in_p;
5810 : 3443560 : tree low, high;
5811 : 3443560 : int lowequal = ((low0 == 0 && low1 == 0)
5812 : 3443560 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5813 : 3443560 : low0, 0, low1, 0)));
5814 : 3443560 : int highequal = ((high0 == 0 && high1 == 0)
5815 : 3443560 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5816 : 3443560 : high0, 1, high1, 1)));
5817 : :
5818 : : /* Make range 0 be the range that starts first, or ends last if they
5819 : : start at the same value. Swap them if it isn't. */
5820 : 3443560 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5821 : : low0, 0, low1, 0))
5822 : 3443560 : || (lowequal
5823 : 564755 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5824 : : high1, 1, high0, 1))))
5825 : : {
5826 : : temp = in0_p, in0_p = in1_p, in1_p = temp;
5827 : : tem = low0, low0 = low1, low1 = tem;
5828 : : tem = high0, high0 = high1, high1 = tem;
5829 : : }
5830 : :
5831 : : /* If the second range is != high1 where high1 is the type maximum of
5832 : : the type, try first merging with < high1 range. */
5833 : 3443560 : if (low1
5834 : 3443560 : && high1
5835 : 882017 : && TREE_CODE (low1) == INTEGER_CST
5836 : 882017 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5837 : 144082 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5838 : 200460 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5839 : : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5840 : 4281725 : && operand_equal_p (low1, high1, 0))
5841 : : {
5842 : 486735 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5843 : 486735 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5844 : : !in1_p, NULL_TREE, range_predecessor (low1)))
5845 : : return true;
5846 : : /* Similarly for the second range != low1 where low1 is the type minimum
5847 : : of the type, try first merging with > low1 range. */
5848 : 391379 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5849 : 391379 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5850 : : !in1_p, range_successor (low1), NULL_TREE))
5851 : : return true;
5852 : : }
5853 : :
5854 : : /* Now flag two cases, whether the ranges are disjoint or whether the
5855 : : second range is totally subsumed in the first. Note that the tests
5856 : : below are simplified by the ones above. */
5857 : 3283449 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5858 : : high0, 1, low1, 0));
5859 : 3283449 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5860 : : high1, 1, high0, 1));
5861 : :
5862 : : /* We now have four cases, depending on whether we are including or
5863 : : excluding the two ranges. */
5864 : 3283449 : if (in0_p && in1_p)
5865 : : {
5866 : : /* If they don't overlap, the result is false. If the second range
5867 : : is a subset it is the result. Otherwise, the range is from the start
5868 : : of the second to the end of the first. */
5869 : 1484361 : if (no_overlap)
5870 : : in_p = 0, low = high = 0;
5871 : 1482092 : else if (subset)
5872 : : in_p = 1, low = low1, high = high1;
5873 : : else
5874 : 1340422 : in_p = 1, low = low1, high = high0;
5875 : : }
5876 : :
5877 : 1799088 : else if (in0_p && ! in1_p)
5878 : : {
5879 : : /* If they don't overlap, the result is the first range. If they are
5880 : : equal, the result is false. If the second range is a subset of the
5881 : : first, and the ranges begin at the same place, we go from just after
5882 : : the end of the second range to the end of the first. If the second
5883 : : range is not a subset of the first, or if it is a subset and both
5884 : : ranges end at the same place, the range starts at the start of the
5885 : : first range and ends just before the second range.
5886 : : Otherwise, we can't describe this as a single range. */
5887 : 309750 : if (no_overlap)
5888 : : in_p = 1, low = low0, high = high0;
5889 : 304172 : else if (lowequal && highequal)
5890 : : in_p = 0, low = high = 0;
5891 : 303145 : else if (subset && lowequal)
5892 : : {
5893 : 221552 : low = range_successor (high1);
5894 : 221552 : high = high0;
5895 : 221552 : in_p = 1;
5896 : 221552 : if (low == 0)
5897 : : {
5898 : : /* We are in the weird situation where high0 > high1 but
5899 : : high1 has no successor. Punt. */
5900 : : return 0;
5901 : : }
5902 : : }
5903 : 81593 : else if (! subset || highequal)
5904 : : {
5905 : 56156 : low = low0;
5906 : 56156 : high = range_predecessor (low1);
5907 : 56156 : in_p = 1;
5908 : 56156 : if (high == 0)
5909 : : {
5910 : : /* low0 < low1 but low1 has no predecessor. Punt. */
5911 : : return 0;
5912 : : }
5913 : : }
5914 : : else
5915 : : return 0;
5916 : : }
5917 : :
5918 : 1489338 : else if (! in0_p && in1_p)
5919 : : {
5920 : : /* If they don't overlap, the result is the second range. If the second
5921 : : is a subset of the first, the result is false. Otherwise,
5922 : : the range starts just after the first range and ends at the
5923 : : end of the second. */
5924 : 1149117 : if (no_overlap)
5925 : : in_p = 1, low = low1, high = high1;
5926 : 1141275 : else if (subset || highequal)
5927 : : in_p = 0, low = high = 0;
5928 : : else
5929 : : {
5930 : 1022749 : low = range_successor (high0);
5931 : 1022749 : high = high1;
5932 : 1022749 : in_p = 1;
5933 : 1022749 : if (low == 0)
5934 : : {
5935 : : /* high1 > high0 but high0 has no successor. Punt. */
5936 : : return 0;
5937 : : }
5938 : : }
5939 : : }
5940 : :
5941 : : else
5942 : : {
5943 : : /* The case where we are excluding both ranges. Here the complex case
5944 : : is if they don't overlap. In that case, the only time we have a
5945 : : range is if they are adjacent. If the second is a subset of the
5946 : : first, the result is the first. Otherwise, the range to exclude
5947 : : starts at the beginning of the first range and ends at the end of the
5948 : : second. */
5949 : 340221 : if (no_overlap)
5950 : : {
5951 : 225479 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5952 : : range_successor (high0),
5953 : : 1, low1, 0)))
5954 : : in_p = 0, low = low0, high = high1;
5955 : : else
5956 : : {
5957 : : /* Canonicalize - [min, x] into - [-, x]. */
5958 : 172134 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5959 : 170859 : switch (TREE_CODE (TREE_TYPE (low0)))
5960 : : {
5961 : 64900 : case ENUMERAL_TYPE:
5962 : 64900 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5963 : : GET_MODE_BITSIZE
5964 : 129800 : (TYPE_MODE (TREE_TYPE (low0)))))
5965 : : break;
5966 : : /* FALLTHROUGH */
5967 : 170666 : case INTEGER_TYPE:
5968 : 170666 : if (tree_int_cst_equal (low0,
5969 : 170666 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5970 : 7164 : low0 = 0;
5971 : : break;
5972 : 193 : case POINTER_TYPE:
5973 : 193 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5974 : 193 : && integer_zerop (low0))
5975 : : low0 = 0;
5976 : : break;
5977 : : default:
5978 : : break;
5979 : : }
5980 : :
5981 : : /* Canonicalize - [x, max] into - [x, -]. */
5982 : 172134 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5983 : 171928 : switch (TREE_CODE (TREE_TYPE (high1)))
5984 : : {
5985 : 64908 : case ENUMERAL_TYPE:
5986 : 64908 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5987 : : GET_MODE_BITSIZE
5988 : 129816 : (TYPE_MODE (TREE_TYPE (high1)))))
5989 : : break;
5990 : : /* FALLTHROUGH */
5991 : 171735 : case INTEGER_TYPE:
5992 : 171735 : if (tree_int_cst_equal (high1,
5993 : 171735 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5994 : 12517 : high1 = 0;
5995 : : break;
5996 : 193 : case POINTER_TYPE:
5997 : 193 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5998 : 386 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5999 : : high1, 1,
6000 : 193 : build_int_cst (TREE_TYPE (high1), 1),
6001 : : 1)))
6002 : 133 : high1 = 0;
6003 : : break;
6004 : : default:
6005 : : break;
6006 : : }
6007 : :
6008 : : /* The ranges might be also adjacent between the maximum and
6009 : : minimum values of the given type. For
6010 : : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6011 : : return + [x + 1, y - 1]. */
6012 : 172134 : if (low0 == 0 && high1 == 0)
6013 : : {
6014 : 541 : low = range_successor (high0);
6015 : 541 : high = range_predecessor (low1);
6016 : 541 : if (low == 0 || high == 0)
6017 : : return 0;
6018 : :
6019 : : in_p = 1;
6020 : : }
6021 : : else
6022 : : return 0;
6023 : : }
6024 : : }
6025 : 114742 : else if (subset)
6026 : : in_p = 0, low = low0, high = high0;
6027 : : else
6028 : 15495 : in_p = 0, low = low0, high = high1;
6029 : : }
6030 : :
6031 : 3086410 : *pin_p = in_p, *plow = low, *phigh = high;
6032 : 3086410 : return 1;
6033 : : }
6034 : :
6035 : :
6036 : : /* Subroutine of fold, looking inside expressions of the form
6037 : : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6038 : : are the three operands of the COND_EXPR. This function is
6039 : : being used also to optimize A op B ? C : A, by reversing the
6040 : : comparison first.
6041 : :
6042 : : Return a folded expression whose code is not a COND_EXPR
6043 : : anymore, or NULL_TREE if no folding opportunity is found. */
6044 : :
6045 : : static tree
6046 : 442404 : fold_cond_expr_with_comparison (location_t loc, tree type,
6047 : : enum tree_code comp_code,
6048 : : tree arg00, tree arg01, tree arg1, tree arg2)
6049 : : {
6050 : 442404 : tree arg1_type = TREE_TYPE (arg1);
6051 : 442404 : tree tem;
6052 : :
6053 : 442404 : STRIP_NOPS (arg1);
6054 : 442404 : STRIP_NOPS (arg2);
6055 : :
6056 : : /* If we have A op 0 ? A : -A, consider applying the following
6057 : : transformations:
6058 : :
6059 : : A == 0? A : -A same as -A
6060 : : A != 0? A : -A same as 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 : : A < 0? A : -A same as -abs (A)
6065 : :
6066 : : None of these transformations work for modes with signed
6067 : : zeros. If A is +/-0, the first two transformations will
6068 : : change the sign of the result (from +0 to -0, or vice
6069 : : versa). The last four will fix the sign of the result,
6070 : : even though the original expressions could be positive or
6071 : : negative, depending on the sign of A.
6072 : :
6073 : : Note that all these transformations are correct if A is
6074 : : NaN, since the two alternatives (A and -A) are also NaNs. */
6075 : 442404 : if (!HONOR_SIGNED_ZEROS (type)
6076 : 884818 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6077 : 442404 : ? real_zerop (arg01)
6078 : 441344 : : integer_zerop (arg01))
6079 : 1173515 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6080 : 1490 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6081 : : /* In the case that A is of the form X-Y, '-A' (arg2) may
6082 : : have already been folded to Y-X, check for that. */
6083 : 287441 : || (TREE_CODE (arg1) == MINUS_EXPR
6084 : 1718 : && TREE_CODE (arg2) == MINUS_EXPR
6085 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6086 : 0 : TREE_OPERAND (arg2, 1), 0)
6087 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6088 : 0 : TREE_OPERAND (arg2, 0), 0))))
6089 : 1266 : switch (comp_code)
6090 : : {
6091 : 0 : case EQ_EXPR:
6092 : 0 : case UNEQ_EXPR:
6093 : 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6094 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6095 : 0 : case NE_EXPR:
6096 : 0 : case LTGT_EXPR:
6097 : 0 : return fold_convert_loc (loc, type, arg1);
6098 : 0 : case UNGE_EXPR:
6099 : 0 : case UNGT_EXPR:
6100 : 0 : if (flag_trapping_math)
6101 : : break;
6102 : : /* Fall through. */
6103 : 1266 : case GE_EXPR:
6104 : 1266 : case GT_EXPR:
6105 : 1266 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6106 : : break;
6107 : 1250 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6108 : 1250 : return fold_convert_loc (loc, type, tem);
6109 : 0 : case UNLE_EXPR:
6110 : 0 : case UNLT_EXPR:
6111 : 0 : if (flag_trapping_math)
6112 : : break;
6113 : : /* FALLTHRU */
6114 : 0 : case LE_EXPR:
6115 : 0 : case LT_EXPR:
6116 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6117 : : break;
6118 : 0 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6119 : 0 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6120 : : {
6121 : : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6122 : : is not, invokes UB both in abs and in the negation of it.
6123 : : So, use ABSU_EXPR instead. */
6124 : 0 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6125 : 0 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6126 : 0 : tem = negate_expr (tem);
6127 : 0 : return fold_convert_loc (loc, type, tem);
6128 : : }
6129 : : else
6130 : : {
6131 : 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6132 : 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6133 : : }
6134 : 0 : default:
6135 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6136 : : break;
6137 : : }
6138 : :
6139 : : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6140 : : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6141 : : both transformations are correct when A is NaN: A != 0
6142 : : is then true, and A == 0 is false. */
6143 : :
6144 : 441154 : if (!HONOR_SIGNED_ZEROS (type)
6145 : 441154 : && integer_zerop (arg01) && integer_zerop (arg2))
6146 : : {
6147 : 240240 : if (comp_code == NE_EXPR)
6148 : 145 : return fold_convert_loc (loc, type, arg1);
6149 : 240095 : else if (comp_code == EQ_EXPR)
6150 : 0 : return build_zero_cst (type);
6151 : : }
6152 : :
6153 : : /* Try some transformations of A op B ? A : B.
6154 : :
6155 : : A == B? A : B same as B
6156 : : A != B? A : B same as A
6157 : : A >= B? A : B same as max (A, B)
6158 : : A > B? A : B same as max (B, A)
6159 : : A <= B? A : B same as min (A, B)
6160 : : A < B? A : B same as min (B, A)
6161 : :
6162 : : As above, these transformations don't work in the presence
6163 : : of signed zeros. For example, if A and B are zeros of
6164 : : opposite sign, the first two transformations will change
6165 : : the sign of the result. In the last four, the original
6166 : : expressions give different results for (A=+0, B=-0) and
6167 : : (A=-0, B=+0), but the transformed expressions do not.
6168 : :
6169 : : The first two transformations are correct if either A or B
6170 : : is a NaN. In the first transformation, the condition will
6171 : : be false, and B will indeed be chosen. In the case of the
6172 : : second transformation, the condition A != B will be true,
6173 : : and A will be chosen.
6174 : :
6175 : : The conversions to max() and min() are not correct if B is
6176 : : a number and A is not. The conditions in the original
6177 : : expressions will be false, so all four give B. The min()
6178 : : and max() versions would give a NaN instead. */
6179 : 441009 : if (!HONOR_SIGNED_ZEROS (type)
6180 : 441009 : && operand_equal_for_comparison_p (arg01, arg2)
6181 : : /* Avoid these transformations if the COND_EXPR may be used
6182 : : as an lvalue in the C++ front-end. PR c++/19199. */
6183 : 689595 : && (in_gimple_form
6184 : 16171 : || VECTOR_TYPE_P (type)
6185 : 16109 : || (! lang_GNU_CXX ()
6186 : 13533 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6187 : 2576 : || ! maybe_lvalue_p (arg1)
6188 : 2555 : || ! maybe_lvalue_p (arg2)))
6189 : : {
6190 : 246935 : tree comp_op0 = arg00;
6191 : 246935 : tree comp_op1 = arg01;
6192 : 246935 : tree comp_type = TREE_TYPE (comp_op0);
6193 : :
6194 : 246935 : switch (comp_code)
6195 : : {
6196 : 0 : case EQ_EXPR:
6197 : 0 : return fold_convert_loc (loc, type, arg2);
6198 : 1 : case NE_EXPR:
6199 : 1 : return fold_convert_loc (loc, type, arg1);
6200 : 5859 : case LE_EXPR:
6201 : 5859 : case LT_EXPR:
6202 : 5859 : case UNLE_EXPR:
6203 : 5859 : case UNLT_EXPR:
6204 : : /* In C++ a ?: expression can be an lvalue, so put the
6205 : : operand which will be used if they are equal first
6206 : : so that we can convert this back to the
6207 : : corresponding COND_EXPR. */
6208 : 5859 : if (!HONOR_NANS (arg1))
6209 : : {
6210 : 5859 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6211 : 5859 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6212 : 5859 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6213 : 5859 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6214 : 4499 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6215 : : comp_op1, comp_op0);
6216 : 5859 : return fold_convert_loc (loc, type, tem);
6217 : : }
6218 : : break;
6219 : 241075 : case GE_EXPR:
6220 : 241075 : case GT_EXPR:
6221 : 241075 : case UNGE_EXPR:
6222 : 241075 : case UNGT_EXPR:
6223 : 241075 : if (!HONOR_NANS (arg1))
6224 : : {
6225 : 241073 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6226 : 241073 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6227 : 241073 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6228 : 241073 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6229 : 3440 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6230 : : comp_op1, comp_op0);
6231 : 241073 : return fold_convert_loc (loc, type, tem);
6232 : : }
6233 : : break;
6234 : 0 : case UNEQ_EXPR:
6235 : 0 : if (!HONOR_NANS (arg1))
6236 : 0 : return fold_convert_loc (loc, type, arg2);
6237 : : break;
6238 : 0 : case LTGT_EXPR:
6239 : 0 : if (!HONOR_NANS (arg1))
6240 : 0 : return fold_convert_loc (loc, type, arg1);
6241 : : break;
6242 : 0 : default:
6243 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6244 : : break;
6245 : : }
6246 : : }
6247 : :
6248 : : return NULL_TREE;
6249 : : }
6250 : :
6251 : :
6252 : :
6253 : : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6254 : : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6255 : : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6256 : : false) >= 2)
6257 : : #endif
6258 : :
6259 : : /* EXP is some logical combination of boolean tests. See if we can
6260 : : merge it into some range test. Return the new tree if so. */
6261 : :
6262 : : static tree
6263 : 23903481 : fold_range_test (location_t loc, enum tree_code code, tree type,
6264 : : tree op0, tree op1)
6265 : : {
6266 : 23903481 : int or_op = (code == TRUTH_ORIF_EXPR
6267 : 23903481 : || code == TRUTH_OR_EXPR);
6268 : 23903481 : int in0_p, in1_p, in_p;
6269 : 23903481 : tree low0, low1, low, high0, high1, high;
6270 : 23903481 : bool strict_overflow_p = false;
6271 : 23903481 : tree tem, lhs, rhs;
6272 : 23903481 : const char * const warnmsg = G_("assuming signed overflow does not occur "
6273 : : "when simplifying range test");
6274 : :
6275 : 23903481 : if (!INTEGRAL_TYPE_P (type))
6276 : : return 0;
6277 : :
6278 : 23903481 : lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
6279 : : /* If op0 is known true or false and this is a short-circuiting
6280 : : operation we must not merge with op1 since that makes side-effects
6281 : : unconditional. So special-case this. */
6282 : 23903481 : if (!lhs
6283 : 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6284 : 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6285 : : return op0;
6286 : 23903479 : rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
6287 : :
6288 : : /* If this is an OR operation, invert both sides; we will invert
6289 : : again at the end. */
6290 : 23903479 : if (or_op)
6291 : 11610536 : in0_p = ! in0_p, in1_p = ! in1_p;
6292 : :
6293 : : /* If both expressions are the same, if we can merge the ranges, and we
6294 : : can build the range test, return it or it inverted. If one of the
6295 : : ranges is always true or always false, consider it to be the same
6296 : : expression as the other. */
6297 : 23869605 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6298 : 1101602 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6299 : : in1_p, low1, high1)
6300 : 24841569 : && (tem = (build_range_check (loc, type,
6301 : : lhs != 0 ? lhs
6302 : 0 : : rhs != 0 ? rhs : integer_zero_node,
6303 : : in_p, low, high))) != 0)
6304 : : {
6305 : 938090 : if (strict_overflow_p)
6306 : 259 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6307 : 938090 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6308 : : }
6309 : :
6310 : : /* On machines where the branch cost is expensive, if this is a
6311 : : short-circuited branch and the underlying object on both sides
6312 : : is the same, make a non-short-circuit operation. */
6313 : 22965389 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6314 : 22965389 : if (param_logical_op_non_short_circuit != -1)
6315 : 7777 : logical_op_non_short_circuit
6316 : 7777 : = param_logical_op_non_short_circuit;
6317 : 22965389 : if (logical_op_non_short_circuit
6318 : 22961492 : && !sanitize_coverage_p ()
6319 : 22961489 : && lhs != 0 && rhs != 0
6320 : 22961310 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6321 : 27505458 : && operand_equal_p (lhs, rhs, 0))
6322 : : {
6323 : : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6324 : : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6325 : : which cases we can't do this. */
6326 : 131551 : if (simple_operand_p (lhs))
6327 : 124517 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6328 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6329 : 62776 : type, op0, op1);
6330 : :
6331 : 68775 : else if (!lang_hooks.decls.global_bindings_p ()
6332 : 68775 : && !CONTAINS_PLACEHOLDER_P (lhs))
6333 : : {
6334 : 68122 : tree common = save_expr (lhs);
6335 : :
6336 : 111569 : if ((lhs = build_range_check (loc, type, common,
6337 : 43447 : or_op ? ! in0_p : in0_p,
6338 : : low0, high0)) != 0
6339 : 111569 : && (rhs = build_range_check (loc, type, common,
6340 : 43447 : or_op ? ! in1_p : in1_p,
6341 : : low1, high1)) != 0)
6342 : : {
6343 : 68122 : if (strict_overflow_p)
6344 : 0 : fold_overflow_warning (warnmsg,
6345 : : WARN_STRICT_OVERFLOW_COMPARISON);
6346 : 111569 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6347 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6348 : 68122 : type, lhs, rhs);
6349 : : }
6350 : : }
6351 : : }
6352 : :
6353 : : return 0;
6354 : : }
6355 : :
6356 : : /* For an expression that has the form
6357 : : (A && B) || ~B
6358 : : or
6359 : : (A || B) && ~B,
6360 : : we can drop one of the inner expressions and simplify to
6361 : : A || ~B
6362 : : or
6363 : : A && ~B
6364 : : LOC is the location of the resulting expression. OP is the inner
6365 : : logical operation; the left-hand side in the examples above, while CMPOP
6366 : : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6367 : : removing a condition that guards another, as in
6368 : : (A != NULL && A->...) || A == NULL
6369 : : which we must not transform. If RHS_ONLY is true, only eliminate the
6370 : : right-most operand of the inner logical operation. */
6371 : :
6372 : : static tree
6373 : 136211 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6374 : : bool rhs_only)
6375 : : {
6376 : 136211 : enum tree_code code = TREE_CODE (cmpop);
6377 : 136211 : enum tree_code truthop_code = TREE_CODE (op);
6378 : 136211 : tree lhs = TREE_OPERAND (op, 0);
6379 : 136211 : tree rhs = TREE_OPERAND (op, 1);
6380 : 136211 : tree orig_lhs = lhs, orig_rhs = rhs;
6381 : 136211 : enum tree_code rhs_code = TREE_CODE (rhs);
6382 : 136211 : enum tree_code lhs_code = TREE_CODE (lhs);
6383 : 136211 : enum tree_code inv_code;
6384 : :
6385 : 136211 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6386 : : return NULL_TREE;
6387 : :
6388 : 91927 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6389 : : return NULL_TREE;
6390 : :
6391 : 54124 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6392 : :
6393 : 54124 : if (rhs_code == truthop_code)
6394 : : {
6395 : 29 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6396 : 29 : if (newrhs != NULL_TREE)
6397 : : {
6398 : 0 : rhs = newrhs;
6399 : 0 : rhs_code = TREE_CODE (rhs);
6400 : : }
6401 : : }
6402 : 54124 : if (lhs_code == truthop_code && !rhs_only)
6403 : : {
6404 : 460 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6405 : 460 : if (newlhs != NULL_TREE)
6406 : : {
6407 : 0 : lhs = newlhs;
6408 : 0 : lhs_code = TREE_CODE (lhs);
6409 : : }
6410 : : }
6411 : :
6412 : 54124 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6413 : 54124 : if (inv_code == rhs_code
6414 : 701 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6415 : 54160 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6416 : : return lhs;
6417 : 54111 : if (!rhs_only && inv_code == lhs_code
6418 : 615 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6419 : 54188 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6420 : : return rhs;
6421 : 54035 : if (rhs != orig_rhs || lhs != orig_lhs)
6422 : 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6423 : 0 : lhs, rhs);
6424 : : return NULL_TREE;
6425 : : }
6426 : :
6427 : : /* Find ways of folding logical expressions of LHS and RHS:
6428 : : Try to merge two comparisons to the same innermost item.
6429 : : Look for range tests like "ch >= '0' && ch <= '9'".
6430 : : Look for combinations of simple terms on machines with expensive branches
6431 : : and evaluate the RHS unconditionally.
6432 : :
6433 : : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6434 : : function and the one above.
6435 : :
6436 : : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6437 : : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6438 : :
6439 : : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6440 : : two operands.
6441 : :
6442 : : We return the simplified tree or 0 if no optimization is possible. */
6443 : :
6444 : : static tree
6445 : 23548088 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6446 : : tree lhs, tree rhs)
6447 : : {
6448 : : /* If this is the "or" of two comparisons, we can do something if
6449 : : the comparisons are NE_EXPR. If this is the "and", we can do something
6450 : : if the comparisons are EQ_EXPR. I.e.,
6451 : : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6452 : :
6453 : : WANTED_CODE is this operation code. For single bit fields, we can
6454 : : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6455 : : comparison for one-bit fields. */
6456 : :
6457 : 23548088 : enum tree_code lcode, rcode;
6458 : 23548088 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6459 : 23548088 : tree result;
6460 : :
6461 : : /* Start by getting the comparison codes. Fail if anything is volatile.
6462 : : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6463 : : it were surrounded with a NE_EXPR. */
6464 : :
6465 : 23548088 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6466 : : return 0;
6467 : :
6468 : 21281824 : lcode = TREE_CODE (lhs);
6469 : 21281824 : rcode = TREE_CODE (rhs);
6470 : :
6471 : 21281824 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6472 : : {
6473 : 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6474 : 0 : build_int_cst (TREE_TYPE (lhs), 0));
6475 : 0 : lcode = NE_EXPR;
6476 : : }
6477 : :
6478 : 21281824 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6479 : : {
6480 : 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6481 : 0 : build_int_cst (TREE_TYPE (rhs), 0));
6482 : 0 : rcode = NE_EXPR;
6483 : : }
6484 : :
6485 : 21281824 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6486 : 18989740 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6487 : : return 0;
6488 : :
6489 : 17838103 : ll_arg = TREE_OPERAND (lhs, 0);
6490 : 17838103 : lr_arg = TREE_OPERAND (lhs, 1);
6491 : 17838103 : rl_arg = TREE_OPERAND (rhs, 0);
6492 : 17838103 : rr_arg = TREE_OPERAND (rhs, 1);
6493 : :
6494 : : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6495 : 17838103 : if (simple_operand_p (ll_arg)
6496 : 17838103 : && simple_operand_p (lr_arg))
6497 : : {
6498 : 14533207 : if (operand_equal_p (ll_arg, rl_arg, 0)
6499 : 14533207 : && operand_equal_p (lr_arg, rr_arg, 0))
6500 : : {
6501 : 19986 : result = combine_comparisons (loc, code, lcode, rcode,
6502 : : truth_type, ll_arg, lr_arg);
6503 : 19986 : if (result)
6504 : : return result;
6505 : : }
6506 : 14513221 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6507 : 14513221 : && operand_equal_p (lr_arg, rl_arg, 0))
6508 : : {
6509 : 286 : result = combine_comparisons (loc, code, lcode,
6510 : : swap_tree_comparison (rcode),
6511 : : truth_type, ll_arg, lr_arg);
6512 : 286 : if (result)
6513 : : return result;
6514 : : }
6515 : : }
6516 : :
6517 : 8603801 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6518 : 17818416 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6519 : :
6520 : : /* If the RHS can be evaluated unconditionally and its operands are
6521 : : simple, it wins to evaluate the RHS unconditionally on machines
6522 : : with expensive branches. In this case, this isn't a comparison
6523 : : that can be merged. */
6524 : :
6525 : 17818416 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6526 : : false) >= 2
6527 : 17818307 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6528 : 16822858 : && simple_operand_p (rl_arg)
6529 : 27531640 : && simple_operand_p (rr_arg))
6530 : : {
6531 : : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6532 : 10803701 : if (code == TRUTH_OR_EXPR
6533 : 1486040 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6534 : 585137 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6535 : 23585 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6536 : 10823032 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6537 : 38046 : return build2_loc (loc, NE_EXPR, truth_type,
6538 : 19023 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6539 : : ll_arg, rl_arg),
6540 : 19023 : build_int_cst (TREE_TYPE (ll_arg), 0));
6541 : :
6542 : : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6543 : 10784678 : if (code == TRUTH_AND_EXPR
6544 : 1633626 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6545 : 733702 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6546 : 5079 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6547 : 10786288 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6548 : 2754 : return build2_loc (loc, EQ_EXPR, truth_type,
6549 : 1377 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6550 : : ll_arg, rl_arg),
6551 : 1377 : build_int_cst (TREE_TYPE (ll_arg), 0));
6552 : : }
6553 : :
6554 : : return 0;
6555 : : }
6556 : :
6557 : : /* T is an integer expression that is being multiplied, divided, or taken a
6558 : : modulus (CODE says which and what kind of divide or modulus) by a
6559 : : constant C. See if we can eliminate that operation by folding it with
6560 : : other operations already in T. WIDE_TYPE, if non-null, is a type that
6561 : : should be used for the computation if wider than our type.
6562 : :
6563 : : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6564 : : (X * 2) + (Y * 4). We must, however, be assured that either the original
6565 : : expression would not overflow or that overflow is undefined for the type
6566 : : in the language in question.
6567 : :
6568 : : If we return a non-null expression, it is an equivalent form of the
6569 : : original computation, but need not be in the original type.
6570 : :
6571 : : We set *STRICT_OVERFLOW_P to true if the return values depends on
6572 : : signed overflow being undefined. Otherwise we do not change
6573 : : *STRICT_OVERFLOW_P. */
6574 : :
6575 : : static tree
6576 : 92803133 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6577 : : bool *strict_overflow_p)
6578 : : {
6579 : : /* To avoid exponential search depth, refuse to allow recursion past
6580 : : three levels. Beyond that (1) it's highly unlikely that we'll find
6581 : : something interesting and (2) we've probably processed it before
6582 : : when we built the inner expression. */
6583 : :
6584 : 92803133 : static int depth;
6585 : 92803133 : tree ret;
6586 : :
6587 : 92803133 : if (depth > 3)
6588 : : return NULL;
6589 : :
6590 : 89551092 : depth++;
6591 : 89551092 : ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6592 : 89551092 : depth--;
6593 : :
6594 : 89551092 : return ret;
6595 : : }
6596 : :
6597 : : static tree
6598 : 89551092 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6599 : : bool *strict_overflow_p)
6600 : : {
6601 : 89551092 : tree type = TREE_TYPE (t);
6602 : 89551092 : enum tree_code tcode = TREE_CODE (t);
6603 : 89551092 : tree ctype = type;
6604 : 89551092 : if (wide_type)
6605 : : {
6606 : 30145268 : if (TREE_CODE (type) == BITINT_TYPE
6607 : 30145156 : || TREE_CODE (wide_type) == BITINT_TYPE)
6608 : : {
6609 : 112 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6610 : 8529482 : ctype = wide_type;
6611 : : }
6612 : 30145156 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6613 : 60290312 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6614 : 8529482 : ctype = wide_type;
6615 : : }
6616 : 89551092 : tree t1, t2;
6617 : 89551092 : bool same_p = tcode == code;
6618 : 89551092 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6619 : 89551092 : bool sub_strict_overflow_p;
6620 : :
6621 : : /* Don't deal with constants of zero here; they confuse the code below. */
6622 : 89551092 : if (integer_zerop (c))
6623 : : return NULL_TREE;
6624 : :
6625 : 89526170 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6626 : 35903608 : op0 = TREE_OPERAND (t, 0);
6627 : :
6628 : 89526170 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6629 : 10723890 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6630 : :
6631 : : /* Note that we need not handle conditional operations here since fold
6632 : : already handles those cases. So just do arithmetic here. */
6633 : 89526170 : switch (tcode)
6634 : : {
6635 : 4060903 : case INTEGER_CST:
6636 : : /* For a constant, we can always simplify if we are a multiply
6637 : : or (for divide and modulus) if it is a multiple of our constant. */
6638 : 4060903 : if (code == MULT_EXPR
6639 : 5218039 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6640 : 1157136 : TYPE_SIGN (type)))
6641 : : {
6642 : 3273641 : tree tem = const_binop (code, fold_convert (ctype, t),
6643 : : fold_convert (ctype, c));
6644 : : /* If the multiplication overflowed, we lost information on it.
6645 : : See PR68142 and PR69845. */
6646 : 3273641 : if (TREE_OVERFLOW (tem))
6647 : : return NULL_TREE;
6648 : : return tem;
6649 : : }
6650 : : break;
6651 : :
6652 : 35282183 : CASE_CONVERT: case NON_LVALUE_EXPR:
6653 : 35282183 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6654 : : break;
6655 : : /* If op0 is an expression ... */
6656 : 34163528 : if ((COMPARISON_CLASS_P (op0)
6657 : : || UNARY_CLASS_P (op0)
6658 : 34163528 : || BINARY_CLASS_P (op0)
6659 : 31294545 : || VL_EXP_CLASS_P (op0)
6660 : 31265217 : || EXPRESSION_CLASS_P (op0))
6661 : : /* ... and has wrapping overflow, and its type is smaller
6662 : : than ctype, then we cannot pass through as widening. */
6663 : 34280002 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6664 : 1065791 : && (TYPE_PRECISION (ctype)
6665 : 1065791 : > TYPE_PRECISION (TREE_TYPE (op0))))
6666 : : /* ... or this is a truncation (t is narrower than op0),
6667 : : then we cannot pass through this narrowing. */
6668 : 2446390 : || (TYPE_PRECISION (type)
6669 : 2446390 : < TYPE_PRECISION (TREE_TYPE (op0)))
6670 : : /* ... or signedness changes for division or modulus,
6671 : : then we cannot pass through this conversion. */
6672 : 2413065 : || (code != MULT_EXPR
6673 : 109993 : && (TYPE_UNSIGNED (ctype)
6674 : 109993 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6675 : : /* ... or has undefined overflow while the converted to
6676 : : type has not, we cannot do the operation in the inner type
6677 : : as that would introduce undefined overflow. */
6678 : 2320427 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6679 : 1819386 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6680 : : break;
6681 : :
6682 : : /* Pass the constant down and see if we can make a simplification. If
6683 : : we can, replace this expression with the inner simplification for
6684 : : possible later conversion to our or some other type. */
6685 : 31725495 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6686 : 31725495 : && TREE_CODE (t2) == INTEGER_CST
6687 : 31725495 : && !TREE_OVERFLOW (t2)
6688 : 64270304 : && (t1 = extract_muldiv (op0, t2, code,
6689 : : code == MULT_EXPR ? ctype : NULL_TREE,
6690 : : strict_overflow_p)) != 0)
6691 : : return t1;
6692 : : break;
6693 : :
6694 : 279 : case ABS_EXPR:
6695 : : /* If widening the type changes it from signed to unsigned, then we
6696 : : must avoid building ABS_EXPR itself as unsigned. */
6697 : 279 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6698 : : {
6699 : 0 : tree cstype = (*signed_type_for) (ctype);
6700 : 0 : if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6701 : : != 0)
6702 : : {
6703 : 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6704 : 0 : return fold_convert (ctype, t1);
6705 : : }
6706 : : break;
6707 : : }
6708 : : /* If the constant is negative, we cannot simplify this. */
6709 : 279 : if (tree_int_cst_sgn (c) == -1)
6710 : : break;
6711 : : /* FALLTHROUGH */
6712 : 50905 : case NEGATE_EXPR:
6713 : : /* For division and modulus, type can't be unsigned, as e.g.
6714 : : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6715 : : For signed types, even with wrapping overflow, this is fine. */
6716 : 50905 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6717 : : break;
6718 : 49155 : if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6719 : : != 0)
6720 : 0 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6721 : : break;
6722 : :
6723 : 773 : case MIN_EXPR: case MAX_EXPR:
6724 : : /* If widening the type changes the signedness, then we can't perform
6725 : : this optimization as that changes the result. */
6726 : 773 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6727 : : break;
6728 : :
6729 : : /* Punt for multiplication altogether.
6730 : : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6731 : : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6732 : : 0U, the latter is 2U.
6733 : : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6734 : : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6735 : : well defined 0, the latter invokes UB.
6736 : : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6737 : : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6738 : : well defined 25, the latter invokes UB. */
6739 : 773 : if (code == MULT_EXPR)
6740 : : break;
6741 : : /* For division/modulo, punt on c being -1 for MAX, as
6742 : : MAX (INT_MIN, 0) / -1 is not equivalent to
6743 : : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6744 : : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6745 : : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6746 : : transformation won't make it worse. */
6747 : 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6748 : : break;
6749 : :
6750 : : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6751 : 8 : sub_strict_overflow_p = false;
6752 : 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type,
6753 : : &sub_strict_overflow_p)) != 0
6754 : 8 : && (t2 = extract_muldiv (op1, c, code, wide_type,
6755 : : &sub_strict_overflow_p)) != 0)
6756 : : {
6757 : 0 : if (tree_int_cst_sgn (c) < 0)
6758 : 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6759 : 0 : if (sub_strict_overflow_p)
6760 : 0 : *strict_overflow_p = true;
6761 : 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6762 : : fold_convert (ctype, t2));
6763 : : }
6764 : : break;
6765 : :
6766 : 1251 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6767 : : /* If the second operand is constant, this is a multiplication
6768 : : or floor division, by a power of two, so we can treat it that
6769 : : way unless the multiplier or divisor overflows. Signed
6770 : : left-shift overflow is implementation-defined rather than
6771 : : undefined in C90, so do not convert signed left shift into
6772 : : multiplication. */
6773 : 1251 : if (TREE_CODE (op1) == INTEGER_CST
6774 : 1235 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6775 : : /* const_binop may not detect overflow correctly,
6776 : : so check for it explicitly here. */
6777 : 1118 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6778 : 1260 : wi::to_wide (op1))
6779 : 1109 : && (t1 = fold_convert (ctype,
6780 : : const_binop (LSHIFT_EXPR, size_one_node,
6781 : : op1))) != 0
6782 : 2360 : && !TREE_OVERFLOW (t1))
6783 : 2016 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6784 : : ? MULT_EXPR : FLOOR_DIV_EXPR,
6785 : : ctype,
6786 : : fold_convert (ctype, op0),
6787 : : t1),
6788 : 1109 : c, code, wide_type, strict_overflow_p);
6789 : : break;
6790 : :
6791 : 7528272 : case PLUS_EXPR: case MINUS_EXPR:
6792 : : /* See if we can eliminate the operation on both sides. If we can, we
6793 : : can return a new PLUS or MINUS. If we can't, the only remaining
6794 : : cases where we can do anything are if the second operand is a
6795 : : constant. */
6796 : 7528272 : sub_strict_overflow_p = false;
6797 : 7528272 : t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6798 : 7528272 : t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6799 : 793175 : if (t1 != 0 && t2 != 0
6800 : 277118 : && TYPE_OVERFLOW_WRAPS (ctype)
6801 : 7796529 : && (code == MULT_EXPR
6802 : : /* If not multiplication, we can only do this if both operands
6803 : : are divisible by c. */
6804 : 0 : || (multiple_of_p (ctype, op0, c)
6805 : 0 : && multiple_of_p (ctype, op1, c))))
6806 : : {
6807 : 268257 : if (sub_strict_overflow_p)
6808 : 0 : *strict_overflow_p = true;
6809 : 268257 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6810 : : fold_convert (ctype, t2));
6811 : : }
6812 : :
6813 : : /* If this was a subtraction, negate OP1 and set it to be an addition.
6814 : : This simplifies the logic below. */
6815 : 7260015 : if (tcode == MINUS_EXPR)
6816 : : {
6817 : 1891162 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6818 : : /* If OP1 was not easily negatable, the constant may be OP0. */
6819 : 1891162 : if (TREE_CODE (op0) == INTEGER_CST)
6820 : : {
6821 : 352556 : std::swap (op0, op1);
6822 : 352556 : std::swap (t1, t2);
6823 : : }
6824 : : }
6825 : :
6826 : 7260015 : if (TREE_CODE (op1) != INTEGER_CST)
6827 : : break;
6828 : :
6829 : : /* If either OP1 or C are negative, this optimization is not safe for
6830 : : some of the division and remainder types while for others we need
6831 : : to change the code. */
6832 : 3381322 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6833 : : {
6834 : 169162 : if (code == CEIL_DIV_EXPR)
6835 : : code = FLOOR_DIV_EXPR;
6836 : 169160 : else if (code == FLOOR_DIV_EXPR)
6837 : : code = CEIL_DIV_EXPR;
6838 : 168895 : else if (code != MULT_EXPR
6839 : 168895 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6840 : : break;
6841 : : }
6842 : :
6843 : : /* If it's a multiply or a division/modulus operation of a multiple
6844 : : of our constant, do the operation and verify it doesn't overflow. */
6845 : 3376127 : if (code == MULT_EXPR
6846 : 4528656 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6847 : 1152529 : TYPE_SIGN (type)))
6848 : : {
6849 : 2589406 : op1 = const_binop (code, fold_convert (ctype, op1),
6850 : : fold_convert (ctype, c));
6851 : : /* We allow the constant to overflow with wrapping semantics. */
6852 : 2589406 : if (op1 == 0
6853 : 2589406 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6854 : : break;
6855 : : }
6856 : : else
6857 : : break;
6858 : :
6859 : : /* If we have an unsigned type, we cannot widen the operation since it
6860 : : will change the result if the original computation overflowed. */
6861 : 2586094 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6862 : : break;
6863 : :
6864 : : /* The last case is if we are a multiply. In that case, we can
6865 : : apply the distributive law to commute the multiply and addition
6866 : : if the multiplication of the constants doesn't overflow
6867 : : and overflow is defined. With undefined overflow
6868 : : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6869 : : But fold_plusminus_mult_expr would factor back any power-of-two
6870 : : value so do not distribute in the first place in this case. */
6871 : 2586094 : if (code == MULT_EXPR
6872 : 2220873 : && TYPE_OVERFLOW_WRAPS (ctype)
6873 : 4482637 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6874 : 464838 : return fold_build2 (tcode, ctype,
6875 : : fold_build2 (code, ctype,
6876 : : fold_convert (ctype, op0),
6877 : : fold_convert (ctype, c)),
6878 : : op1);
6879 : :
6880 : : break;
6881 : :
6882 : 2154086 : case MULT_EXPR:
6883 : : /* We have a special case here if we are doing something like
6884 : : (C * 8) % 4 since we know that's zero. */
6885 : 2154086 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6886 : 2154086 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6887 : : /* If the multiplication can overflow we cannot optimize this. */
6888 : 10564 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6889 : 326 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6890 : 2164650 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6891 : 281 : TYPE_SIGN (type)))
6892 : : {
6893 : 8 : *strict_overflow_p = true;
6894 : 8 : return omit_one_operand (type, integer_zero_node, op0);
6895 : : }
6896 : :
6897 : : /* ... fall through ... */
6898 : :
6899 : 2335556 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6900 : 2335556 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6901 : : /* If we can extract our operation from the LHS, do so and return a
6902 : : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6903 : : do something only if the second operand is a constant. */
6904 : 2335556 : if (same_p
6905 : 2020848 : && TYPE_OVERFLOW_WRAPS (ctype)
6906 : 4182444 : && (t1 = extract_muldiv (op0, c, code, wide_type,
6907 : : strict_overflow_p)) != 0)
6908 : 65539 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6909 : : fold_convert (ctype, op1));
6910 : 2270017 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6911 : 1952965 : && TYPE_OVERFLOW_WRAPS (ctype)
6912 : 4049070 : && (t1 = extract_muldiv (op1, c, code, wide_type,
6913 : : strict_overflow_p)) != 0)
6914 : 929911 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6915 : : fold_convert (ctype, t1));
6916 : 1340106 : else if (TREE_CODE (op1) != INTEGER_CST)
6917 : : return 0;
6918 : :
6919 : : /* If these are the same operation types, we can associate them
6920 : : assuming no overflow. */
6921 : 506753 : if (tcode == code)
6922 : : {
6923 : 192535 : bool overflow_p = false;
6924 : 192535 : wi::overflow_type overflow_mul;
6925 : 192535 : signop sign = TYPE_SIGN (ctype);
6926 : 192535 : unsigned prec = TYPE_PRECISION (ctype);
6927 : 385070 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6928 : 192535 : wi::to_wide (c, prec),
6929 : 192535 : sign, &overflow_mul);
6930 : 192535 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6931 : 192535 : if (overflow_mul
6932 : 1279 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6933 : : overflow_p = true;
6934 : 192468 : if (!overflow_p)
6935 : 192468 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6936 : : wide_int_to_tree (ctype, mul));
6937 : 192535 : }
6938 : :
6939 : : /* If these operations "cancel" each other, we have the main
6940 : : optimizations of this pass, which occur when either constant is a
6941 : : multiple of the other, in which case we replace this with either an
6942 : : operation or CODE or TCODE.
6943 : :
6944 : : If we have an unsigned type, we cannot do this since it will change
6945 : : the result if the original computation overflowed. */
6946 : 314285 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6947 : 30546 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6948 : 344788 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6949 : 30463 : || (tcode == MULT_EXPR
6950 : 30463 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6951 : 853 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6952 : 849 : && code != MULT_EXPR)))
6953 : : {
6954 : 883 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6955 : 883 : TYPE_SIGN (type)))
6956 : : {
6957 : 106 : *strict_overflow_p = true;
6958 : 106 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6959 : : fold_convert (ctype,
6960 : : const_binop (TRUNC_DIV_EXPR,
6961 : : op1, c)));
6962 : : }
6963 : 777 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6964 : 777 : TYPE_SIGN (type)))
6965 : : {
6966 : 64 : *strict_overflow_p = true;
6967 : 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6968 : : fold_convert (ctype,
6969 : : const_binop (TRUNC_DIV_EXPR,
6970 : : c, op1)));
6971 : : }
6972 : : }
6973 : : break;
6974 : :
6975 : : default:
6976 : : break;
6977 : : }
6978 : :
6979 : : return 0;
6980 : : }
6981 : :
6982 : : /* Return a node which has the indicated constant VALUE (either 0 or
6983 : : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6984 : : and is of the indicated TYPE. */
6985 : :
6986 : : tree
6987 : 81922409 : constant_boolean_node (bool value, tree type)
6988 : : {
6989 : 81922409 : if (type == integer_type_node)
6990 : 19428229 : return value ? integer_one_node : integer_zero_node;
6991 : 62494180 : else if (type == boolean_type_node)
6992 : 59270121 : return value ? boolean_true_node : boolean_false_node;
6993 : 3224059 : else if (VECTOR_TYPE_P (type))
6994 : 584 : return build_vector_from_val (type,
6995 : 584 : build_int_cst (TREE_TYPE (type),
6996 : 923 : value ? -1 : 0));
6997 : : else
6998 : 3223475 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6999 : : }
7000 : :
7001 : :
7002 : : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7003 : : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7004 : : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7005 : : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7006 : : COND is the first argument to CODE; otherwise (as in the example
7007 : : given here), it is the second argument. TYPE is the type of the
7008 : : original expression. Return NULL_TREE if no simplification is
7009 : : possible. */
7010 : :
7011 : : static tree
7012 : 967554 : fold_binary_op_with_conditional_arg (location_t loc,
7013 : : enum tree_code code,
7014 : : tree type, tree op0, tree op1,
7015 : : tree cond, tree arg, int cond_first_p)
7016 : : {
7017 : 967554 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7018 : 967554 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7019 : 967554 : tree test, true_value, false_value;
7020 : 967554 : tree lhs = NULL_TREE;
7021 : 967554 : tree rhs = NULL_TREE;
7022 : 967554 : enum tree_code cond_code = COND_EXPR;
7023 : :
7024 : : /* Do not move possibly trapping operations into the conditional as this
7025 : : pessimizes code and causes gimplification issues when applied late. */
7026 : 987747 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7027 : 202032 : ANY_INTEGRAL_TYPE_P (type)
7028 : 970140 : && TYPE_OVERFLOW_TRAPS (type), op1))
7029 : : return NULL_TREE;
7030 : :
7031 : 947195 : if (TREE_CODE (cond) == COND_EXPR
7032 : 367645 : || TREE_CODE (cond) == VEC_COND_EXPR)
7033 : : {
7034 : 582103 : test = TREE_OPERAND (cond, 0);
7035 : 582103 : true_value = TREE_OPERAND (cond, 1);
7036 : 582103 : false_value = TREE_OPERAND (cond, 2);
7037 : : /* If this operand throws an expression, then it does not make
7038 : : sense to try to perform a logical or arithmetic operation
7039 : : involving it. */
7040 : 582103 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7041 : 7463 : lhs = true_value;
7042 : 582103 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7043 : 6 : rhs = false_value;
7044 : : }
7045 : 365092 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7046 : 365068 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7047 : : {
7048 : 363439 : tree testtype = TREE_TYPE (cond);
7049 : 363439 : test = cond;
7050 : 363439 : true_value = constant_boolean_node (true, testtype);
7051 : 363439 : false_value = constant_boolean_node (false, testtype);
7052 : : }
7053 : : else
7054 : : /* Detect the case of mixing vector and scalar types - bail out. */
7055 : : return NULL_TREE;
7056 : :
7057 : 945542 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7058 : 2577 : cond_code = VEC_COND_EXPR;
7059 : :
7060 : : /* This transformation is only worthwhile if we don't have to wrap ARG
7061 : : in a SAVE_EXPR and the operation can be simplified without recursing
7062 : : on at least one of the branches once its pushed inside the COND_EXPR. */
7063 : 945542 : if (!TREE_CONSTANT (arg)
7064 : 945542 : && (TREE_SIDE_EFFECTS (arg)
7065 : 451785 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7066 : 447364 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7067 : : return NULL_TREE;
7068 : :
7069 : 508517 : arg = fold_convert_loc (loc, arg_type, arg);
7070 : 508517 : if (lhs == 0)
7071 : : {
7072 : 502486 : true_value = fold_convert_loc (loc, cond_type, true_value);
7073 : 502486 : if (cond_first_p)
7074 : 492267 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7075 : : else
7076 : 10219 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7077 : : }
7078 : 508517 : if (rhs == 0)
7079 : : {
7080 : 508511 : false_value = fold_convert_loc (loc, cond_type, false_value);
7081 : 508511 : if (cond_first_p)
7082 : 497731 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7083 : : else
7084 : 10780 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7085 : : }
7086 : :
7087 : : /* Check that we have simplified at least one of the branches. */
7088 : 508517 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7089 : : return NULL_TREE;
7090 : :
7091 : 489648 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7092 : : }
7093 : :
7094 : :
7095 : : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7096 : :
7097 : : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7098 : : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7099 : : if ARG - ZERO_ARG is the same as X.
7100 : :
7101 : : If ARG is NULL, check for any value of type TYPE.
7102 : :
7103 : : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7104 : : and finite. The problematic cases are when X is zero, and its mode
7105 : : has signed zeros. In the case of rounding towards -infinity,
7106 : : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7107 : : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7108 : :
7109 : : bool
7110 : 645549 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7111 : : const_tree zero_arg, int negate)
7112 : : {
7113 : 645549 : if (!real_zerop (zero_arg))
7114 : : return false;
7115 : :
7116 : : /* Don't allow the fold with -fsignaling-nans. */
7117 : 644919 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7118 : : return false;
7119 : :
7120 : : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7121 : 641581 : if (!HONOR_SIGNED_ZEROS (type))
7122 : : return true;
7123 : :
7124 : : /* There is no case that is safe for all rounding modes. */
7125 : 625148 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7126 : : return false;
7127 : :
7128 : : /* In a vector or complex, we would need to check the sign of all zeros. */
7129 : 624485 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7130 : 1453 : zero_arg = uniform_vector_p (zero_arg);
7131 : 624485 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7132 : 1178 : return false;
7133 : :
7134 : : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7135 : 623307 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7136 : 229 : negate = !negate;
7137 : :
7138 : : /* The mode has signed zeros, and we have to honor their sign.
7139 : : In this situation, there are only two cases we can return true for.
7140 : : (i) X - 0 is the same as X with default rounding.
7141 : : (ii) X + 0 is X when X can't possibly be -0.0. */
7142 : 623307 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7143 : : }
7144 : :
7145 : : /* Subroutine of match.pd that optimizes comparisons of a division by
7146 : : a nonzero integer constant against an integer constant, i.e.
7147 : : X/C1 op C2.
7148 : :
7149 : : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7150 : : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7151 : :
7152 : : enum tree_code
7153 : 1739773 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7154 : : tree *hi, bool *neg_overflow)
7155 : : {
7156 : 1739773 : tree prod, tmp, type = TREE_TYPE (c1);
7157 : 1739773 : signop sign = TYPE_SIGN (type);
7158 : 1739773 : wi::overflow_type overflow;
7159 : :
7160 : : /* We have to do this the hard way to detect unsigned overflow.
7161 : : prod = int_const_binop (MULT_EXPR, c1, c2); */
7162 : 1739773 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7163 : 1739773 : prod = force_fit_type (type, val, -1, overflow);
7164 : 1739773 : *neg_overflow = false;
7165 : :
7166 : 1739773 : if (sign == UNSIGNED)
7167 : : {
7168 : 1711159 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7169 : 1711159 : *lo = prod;
7170 : :
7171 : : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7172 : 1711159 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7173 : 1711159 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7174 : : }
7175 : 28614 : else if (tree_int_cst_sgn (c1) >= 0)
7176 : : {
7177 : 27206 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7178 : 27206 : switch (tree_int_cst_sgn (c2))
7179 : : {
7180 : 4620 : case -1:
7181 : 4620 : *neg_overflow = true;
7182 : 4620 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7183 : 4620 : *hi = prod;
7184 : 4620 : break;
7185 : :
7186 : 14068 : case 0:
7187 : 14068 : *lo = fold_negate_const (tmp, type);
7188 : 14068 : *hi = tmp;
7189 : 14068 : break;
7190 : :
7191 : 8518 : case 1:
7192 : 8518 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7193 : 8518 : *lo = prod;
7194 : 8518 : break;
7195 : :
7196 : 0 : default:
7197 : 0 : gcc_unreachable ();
7198 : : }
7199 : : }
7200 : : else
7201 : : {
7202 : : /* A negative divisor reverses the relational operators. */
7203 : 1408 : code = swap_tree_comparison (code);
7204 : :
7205 : 1408 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7206 : 1408 : switch (tree_int_cst_sgn (c2))
7207 : : {
7208 : 132 : case -1:
7209 : 132 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7210 : 132 : *lo = prod;
7211 : 132 : break;
7212 : :
7213 : 178 : case 0:
7214 : 178 : *hi = fold_negate_const (tmp, type);
7215 : 178 : *lo = tmp;
7216 : 178 : break;
7217 : :
7218 : 1098 : case 1:
7219 : 1098 : *neg_overflow = true;
7220 : 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7221 : 1098 : *hi = prod;
7222 : 1098 : break;
7223 : :
7224 : 0 : default:
7225 : 0 : gcc_unreachable ();
7226 : : }
7227 : : }
7228 : :
7229 : 1739773 : if (code != EQ_EXPR && code != NE_EXPR)
7230 : : return code;
7231 : :
7232 : 16541 : if (TREE_OVERFLOW (*lo)
7233 : 16541 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7234 : 727 : *lo = NULL_TREE;
7235 : 16541 : if (TREE_OVERFLOW (*hi)
7236 : 16541 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7237 : 92 : *hi = NULL_TREE;
7238 : :
7239 : : return code;
7240 : 1739773 : }
7241 : :
7242 : : /* Test whether it is preferable to swap two operands, ARG0 and
7243 : : ARG1, for example because ARG0 is an integer constant and ARG1
7244 : : isn't. */
7245 : :
7246 : : bool
7247 : 1511377967 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7248 : : {
7249 : 1511377967 : if (CONSTANT_CLASS_P (arg1))
7250 : : return false;
7251 : 487078170 : if (CONSTANT_CLASS_P (arg0))
7252 : : return true;
7253 : :
7254 : 447568334 : STRIP_NOPS (arg0);
7255 : 447568334 : STRIP_NOPS (arg1);
7256 : :
7257 : 447568334 : if (TREE_CONSTANT (arg1))
7258 : : return false;
7259 : 435215705 : if (TREE_CONSTANT (arg0))
7260 : : return true;
7261 : :
7262 : : /* Put addresses in arg1. */
7263 : 434699869 : if (TREE_CODE (arg1) == ADDR_EXPR)
7264 : : return false;
7265 : 422095874 : if (TREE_CODE (arg0) == ADDR_EXPR)
7266 : : return true;
7267 : :
7268 : : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7269 : : for commutative and comparison operators. Ensuring a canonical
7270 : : form allows the optimizers to find additional redundancies without
7271 : : having to explicitly check for both orderings. */
7272 : 421716393 : if (TREE_CODE (arg0) == SSA_NAME
7273 : 317919294 : && TREE_CODE (arg1) == SSA_NAME
7274 : 733840144 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7275 : : return true;
7276 : :
7277 : : /* Put SSA_NAMEs last. */
7278 : 397513523 : if (TREE_CODE (arg1) == SSA_NAME)
7279 : : return false;
7280 : 95248909 : if (TREE_CODE (arg0) == SSA_NAME)
7281 : : return true;
7282 : :
7283 : : /* Put variables last. */
7284 : 89453366 : if (DECL_P (arg1))
7285 : : return false;
7286 : 47830046 : if (DECL_P (arg0))
7287 : : return true;
7288 : :
7289 : : return false;
7290 : : }
7291 : :
7292 : :
7293 : : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7294 : : means A >= Y && A != MAX, but in this case we know that
7295 : : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7296 : :
7297 : : static tree
7298 : 22869649 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7299 : : {
7300 : 22869649 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7301 : :
7302 : 22869649 : if (TREE_CODE (bound) == LT_EXPR)
7303 : 4816645 : a = TREE_OPERAND (bound, 0);
7304 : 18053004 : else if (TREE_CODE (bound) == GT_EXPR)
7305 : 2557573 : a = TREE_OPERAND (bound, 1);
7306 : : else
7307 : : return NULL_TREE;
7308 : :
7309 : 7374218 : typea = TREE_TYPE (a);
7310 : 7374218 : if (!INTEGRAL_TYPE_P (typea)
7311 : 346684 : && !POINTER_TYPE_P (typea))
7312 : : return NULL_TREE;
7313 : :
7314 : 7191542 : if (TREE_CODE (ineq) == LT_EXPR)
7315 : : {
7316 : 1402384 : a1 = TREE_OPERAND (ineq, 1);
7317 : 1402384 : y = TREE_OPERAND (ineq, 0);
7318 : : }
7319 : 5789158 : else if (TREE_CODE (ineq) == GT_EXPR)
7320 : : {
7321 : 1047251 : a1 = TREE_OPERAND (ineq, 0);
7322 : 1047251 : y = TREE_OPERAND (ineq, 1);
7323 : : }
7324 : : else
7325 : : return NULL_TREE;
7326 : :
7327 : 2449635 : if (TREE_TYPE (a1) != typea)
7328 : : return NULL_TREE;
7329 : :
7330 : 1703052 : if (POINTER_TYPE_P (typea))
7331 : : {
7332 : : /* Convert the pointer types into integer before taking the difference. */
7333 : 8158 : tree ta = fold_convert_loc (loc, ssizetype, a);
7334 : 8158 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7335 : 8158 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7336 : : }
7337 : : else
7338 : 1694894 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7339 : :
7340 : 1703052 : if (!diff || !integer_onep (diff))
7341 : 1692587 : return NULL_TREE;
7342 : :
7343 : 10465 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7344 : : }
7345 : :
7346 : : /* Fold a sum or difference of at least one multiplication.
7347 : : Returns the folded tree or NULL if no simplification could be made. */
7348 : :
7349 : : static tree
7350 : 8565521 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7351 : : tree arg0, tree arg1)
7352 : : {
7353 : 8565521 : tree arg00, arg01, arg10, arg11;
7354 : 8565521 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7355 : :
7356 : : /* (A * C) +- (B * C) -> (A+-B) * C.
7357 : : (A * C) +- A -> A * (C+-1).
7358 : : We are most concerned about the case where C is a constant,
7359 : : but other combinations show up during loop reduction. Since
7360 : : it is not difficult, try all four possibilities. */
7361 : :
7362 : 8565521 : if (TREE_CODE (arg0) == MULT_EXPR)
7363 : : {
7364 : 7568673 : arg00 = TREE_OPERAND (arg0, 0);
7365 : 7568673 : arg01 = TREE_OPERAND (arg0, 1);
7366 : : }
7367 : 996848 : else if (TREE_CODE (arg0) == INTEGER_CST)
7368 : : {
7369 : 69946 : arg00 = build_one_cst (type);
7370 : 69946 : arg01 = arg0;
7371 : : }
7372 : : else
7373 : : {
7374 : : /* We cannot generate constant 1 for fract. */
7375 : 926902 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7376 : 0 : return NULL_TREE;
7377 : 926902 : arg00 = arg0;
7378 : 926902 : arg01 = build_one_cst (type);
7379 : : }
7380 : 8565521 : if (TREE_CODE (arg1) == MULT_EXPR)
7381 : : {
7382 : 2339057 : arg10 = TREE_OPERAND (arg1, 0);
7383 : 2339057 : arg11 = TREE_OPERAND (arg1, 1);
7384 : : }
7385 : 6226464 : else if (TREE_CODE (arg1) == INTEGER_CST)
7386 : : {
7387 : 3316421 : arg10 = build_one_cst (type);
7388 : : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7389 : : the purpose of this canonicalization. */
7390 : 6401482 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7391 : 234648 : && negate_expr_p (arg1)
7392 : 3547781 : && code == PLUS_EXPR)
7393 : : {
7394 : 231360 : arg11 = negate_expr (arg1);
7395 : 231360 : code = MINUS_EXPR;
7396 : : }
7397 : : else
7398 : : arg11 = arg1;
7399 : : }
7400 : : else
7401 : : {
7402 : : /* We cannot generate constant 1 for fract. */
7403 : 2910043 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7404 : 0 : return NULL_TREE;
7405 : 2910043 : arg10 = arg1;
7406 : 2910043 : arg11 = build_one_cst (type);
7407 : : }
7408 : 8565521 : same = NULL_TREE;
7409 : :
7410 : : /* Prefer factoring a common non-constant. */
7411 : 8565521 : if (operand_equal_p (arg00, arg10, 0))
7412 : : same = arg00, alt0 = arg01, alt1 = arg11;
7413 : 8561690 : else if (operand_equal_p (arg01, arg11, 0))
7414 : : same = arg01, alt0 = arg00, alt1 = arg10;
7415 : 8468456 : else if (operand_equal_p (arg00, arg11, 0))
7416 : : same = arg00, alt0 = arg01, alt1 = arg10;
7417 : 8468380 : else if (operand_equal_p (arg01, arg10, 0))
7418 : : same = arg01, alt0 = arg00, alt1 = arg11;
7419 : :
7420 : : /* No identical multiplicands; see if we can find a common
7421 : : power-of-two factor in non-power-of-two multiplies. This
7422 : : can help in multi-dimensional array access. */
7423 : 8463577 : else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7424 : : {
7425 : 7118320 : HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7426 : 7118320 : HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7427 : 7118320 : HOST_WIDE_INT tmp;
7428 : 7118320 : bool swap = false;
7429 : 7118320 : tree maybe_same;
7430 : :
7431 : : /* Move min of absolute values to int11. */
7432 : 7118320 : if (absu_hwi (int01) < absu_hwi (int11))
7433 : : {
7434 : : tmp = int01, int01 = int11, int11 = tmp;
7435 : : alt0 = arg00, arg00 = arg10, arg10 = alt0;
7436 : : maybe_same = arg01;
7437 : : swap = true;
7438 : : }
7439 : : else
7440 : 3613827 : maybe_same = arg11;
7441 : :
7442 : 7118320 : const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7443 : 7118320 : if (factor > 1
7444 : 9368659 : && pow2p_hwi (factor)
7445 : 2051604 : && (int01 & (factor - 1)) == 0
7446 : : /* The remainder should not be a constant, otherwise we
7447 : : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7448 : : increased the number of multiplications necessary. */
7449 : 8388851 : && TREE_CODE (arg10) != INTEGER_CST)
7450 : : {
7451 : 1146522 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7452 : 1146522 : build_int_cst (TREE_TYPE (arg00),
7453 : 1146522 : int01 / int11));
7454 : 1146522 : alt1 = arg10;
7455 : 1146522 : same = maybe_same;
7456 : 1146522 : if (swap)
7457 : 1034941 : maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7458 : : }
7459 : : }
7460 : :
7461 : 1248466 : if (!same)
7462 : 7317055 : return NULL_TREE;
7463 : :
7464 : 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7465 : 1248466 : || TYPE_OVERFLOW_WRAPS (type)
7466 : : /* We are neither factoring zero nor minus one. */
7467 : 1373661 : || TREE_CODE (same) == INTEGER_CST)
7468 : 1237045 : return fold_build2_loc (loc, MULT_EXPR, type,
7469 : : fold_build2_loc (loc, code, type,
7470 : : fold_convert_loc (loc, type, alt0),
7471 : : fold_convert_loc (loc, type, alt1)),
7472 : 1237045 : fold_convert_loc (loc, type, same));
7473 : :
7474 : : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7475 : : same may be minus one and thus the multiplication may overflow. Perform
7476 : : the sum operation in an unsigned type. */
7477 : 11421 : tree utype = unsigned_type_for (type);
7478 : 11421 : tree tem = fold_build2_loc (loc, code, utype,
7479 : : fold_convert_loc (loc, utype, alt0),
7480 : : fold_convert_loc (loc, utype, alt1));
7481 : : /* If the sum evaluated to a constant that is not -INF the multiplication
7482 : : cannot overflow. */
7483 : 22842 : if (TREE_CODE (tem) == INTEGER_CST
7484 : 18027 : && (wi::to_wide (tem)
7485 : 18027 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7486 : 3290 : return fold_build2_loc (loc, MULT_EXPR, type,
7487 : 3290 : fold_convert (type, tem), same);
7488 : :
7489 : : /* Do not resort to unsigned multiplication because
7490 : : we lose the no-overflow property of the expression. */
7491 : : return NULL_TREE;
7492 : : }
7493 : :
7494 : :
7495 : : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7496 : : into the buffer PTR of length LEN bytes.
7497 : : Return the number of bytes placed in the buffer, or zero
7498 : : upon failure. */
7499 : :
7500 : : int
7501 : 19789930 : native_encode_wide_int (tree type, const wide_int_ref &val,
7502 : : unsigned char *ptr, int len, int off)
7503 : : {
7504 : 19789930 : int total_bytes;
7505 : 19789930 : if (TREE_CODE (type) == BITINT_TYPE)
7506 : : {
7507 : 17148 : struct bitint_info info;
7508 : 17148 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7509 : 17148 : gcc_assert (ok);
7510 : 17148 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7511 : 17148 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7512 : : {
7513 : 17028 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7514 : : /* More work is needed when adding _BitInt support to PDP endian
7515 : : if limb is smaller than word, or if _BitInt limb ordering doesn't
7516 : : match target endianity here. */
7517 : 17028 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7518 : : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7519 : : || (GET_MODE_SIZE (limb_mode)
7520 : : >= UNITS_PER_WORD)));
7521 : : }
7522 : : else
7523 : 240 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7524 : : }
7525 : : else
7526 : 39545564 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7527 : 19789930 : int byte, offset, word, words;
7528 : 19789930 : unsigned char value;
7529 : :
7530 : 19789930 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7531 : : return 0;
7532 : 19789447 : if (off == -1)
7533 : 18694145 : off = 0;
7534 : :
7535 : 19789447 : if (ptr == NULL)
7536 : : /* Dry run. */
7537 : 2741406 : return MIN (len, total_bytes - off);
7538 : :
7539 : : words = total_bytes / UNITS_PER_WORD;
7540 : :
7541 : 83514904 : for (byte = 0; byte < total_bytes; byte++)
7542 : : {
7543 : 66466863 : int bitpos = byte * BITS_PER_UNIT;
7544 : : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7545 : : number of bytes. */
7546 : 66466863 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7547 : :
7548 : 66466863 : if (total_bytes > UNITS_PER_WORD)
7549 : : {
7550 : 66466863 : word = byte / UNITS_PER_WORD;
7551 : 66466863 : if (WORDS_BIG_ENDIAN)
7552 : : word = (words - 1) - word;
7553 : 66466863 : offset = word * UNITS_PER_WORD;
7554 : 66466863 : if (BYTES_BIG_ENDIAN)
7555 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7556 : : else
7557 : 66466863 : offset += byte % UNITS_PER_WORD;
7558 : : }
7559 : : else
7560 : : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7561 : 66466863 : if (offset >= off && offset - off < len)
7562 : 65180536 : ptr[offset - off] = value;
7563 : : }
7564 : 17048041 : return MIN (len, total_bytes - off);
7565 : : }
7566 : :
7567 : : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7568 : : specified by EXPR into the buffer PTR of length LEN bytes.
7569 : : Return the number of bytes placed in the buffer, or zero
7570 : : upon failure. */
7571 : :
7572 : : static int
7573 : 19789930 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7574 : : {
7575 : 19789930 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7576 : 19789930 : ptr, len, off);
7577 : : }
7578 : :
7579 : :
7580 : : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7581 : : specified by EXPR into the buffer PTR of length LEN bytes.
7582 : : Return the number of bytes placed in the buffer, or zero
7583 : : upon failure. */
7584 : :
7585 : : static int
7586 : 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7587 : : {
7588 : 0 : tree type = TREE_TYPE (expr);
7589 : 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7590 : 0 : int total_bytes = GET_MODE_SIZE (mode);
7591 : 0 : FIXED_VALUE_TYPE value;
7592 : 0 : tree i_value, i_type;
7593 : :
7594 : 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7595 : : return 0;
7596 : :
7597 : 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7598 : :
7599 : 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7600 : : return 0;
7601 : :
7602 : 0 : value = TREE_FIXED_CST (expr);
7603 : 0 : i_value = double_int_to_tree (i_type, value.data);
7604 : :
7605 : 0 : return native_encode_int (i_value, ptr, len, off);
7606 : : }
7607 : :
7608 : :
7609 : : /* Subroutine of native_encode_expr. Encode the REAL_CST
7610 : : specified by EXPR into the buffer PTR of length LEN bytes.
7611 : : Return the number of bytes placed in the buffer, or zero
7612 : : upon failure. */
7613 : :
7614 : : int
7615 : 568263 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7616 : : unsigned char *ptr, int len, int off)
7617 : : {
7618 : 568263 : int total_bytes = GET_MODE_SIZE (mode);
7619 : 568263 : int byte, offset, word, words, bitpos;
7620 : 568263 : unsigned char value;
7621 : :
7622 : : /* There are always 32 bits in each long, no matter the size of
7623 : : the hosts long. We handle floating point representations with
7624 : : up to 192 bits. */
7625 : 568263 : long tmp[6];
7626 : :
7627 : 568263 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7628 : : return 0;
7629 : 566438 : if (off == -1)
7630 : 435448 : off = 0;
7631 : :
7632 : 566438 : if (ptr == NULL)
7633 : : /* Dry run. */
7634 : 137663 : return MIN (len, total_bytes - off);
7635 : :
7636 : 428775 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7637 : :
7638 : 428775 : real_to_target (tmp, val, mode);
7639 : :
7640 : 3503613 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7641 : 3074838 : bitpos += BITS_PER_UNIT)
7642 : : {
7643 : 3074838 : byte = (bitpos / BITS_PER_UNIT) & 3;
7644 : 3074838 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7645 : :
7646 : 3074838 : if (UNITS_PER_WORD < 4)
7647 : : {
7648 : : word = byte / UNITS_PER_WORD;
7649 : : if (WORDS_BIG_ENDIAN)
7650 : : word = (words - 1) - word;
7651 : : offset = word * UNITS_PER_WORD;
7652 : : if (BYTES_BIG_ENDIAN)
7653 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7654 : : else
7655 : : offset += byte % UNITS_PER_WORD;
7656 : : }
7657 : : else
7658 : : {
7659 : 3074838 : offset = byte;
7660 : 3074838 : if (BYTES_BIG_ENDIAN)
7661 : : {
7662 : : /* Reverse bytes within each long, or within the entire float
7663 : : if it's smaller than a long (for HFmode). */
7664 : : offset = MIN (3, total_bytes - 1) - offset;
7665 : : gcc_assert (offset >= 0);
7666 : : }
7667 : : }
7668 : 3074838 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7669 : 3074838 : if (offset >= off
7670 : 3071522 : && offset - off < len)
7671 : 3054030 : ptr[offset - off] = value;
7672 : : }
7673 : 428775 : return MIN (len, total_bytes - off);
7674 : : }
7675 : :
7676 : : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7677 : : specified by EXPR into the buffer PTR of length LEN bytes.
7678 : : Return the number of bytes placed in the buffer, or zero
7679 : : upon failure. */
7680 : :
7681 : : static int
7682 : 20121 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7683 : : {
7684 : 20121 : int rsize, isize;
7685 : 20121 : tree part;
7686 : :
7687 : 20121 : part = TREE_REALPART (expr);
7688 : 20121 : rsize = native_encode_expr (part, ptr, len, off);
7689 : 20121 : if (off == -1 && rsize == 0)
7690 : : return 0;
7691 : 20121 : part = TREE_IMAGPART (expr);
7692 : 20121 : if (off != -1)
7693 : 39924 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7694 : 20121 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7695 : : len - rsize, off);
7696 : 20121 : if (off == -1 && isize != rsize)
7697 : : return 0;
7698 : 20121 : return rsize + isize;
7699 : : }
7700 : :
7701 : : /* Like native_encode_vector, but only encode the first COUNT elements.
7702 : : The other arguments are as for native_encode_vector. */
7703 : :
7704 : : static int
7705 : 867760 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7706 : : int off, unsigned HOST_WIDE_INT count)
7707 : : {
7708 : 867760 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7709 : 1735520 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7710 : 868080 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7711 : : {
7712 : : /* This is the only case in which elements can be smaller than a byte.
7713 : : Element 0 is always in the lsb of the containing byte. */
7714 : 244 : unsigned int elt_bits = TYPE_PRECISION (itype);
7715 : 244 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7716 : 244 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7717 : : return 0;
7718 : :
7719 : 244 : if (off == -1)
7720 : 244 : off = 0;
7721 : :
7722 : : /* Zero the buffer and then set bits later where necessary. */
7723 : 244 : int extract_bytes = MIN (len, total_bytes - off);
7724 : 244 : if (ptr)
7725 : 244 : memset (ptr, 0, extract_bytes);
7726 : :
7727 : 244 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7728 : 244 : unsigned int first_elt = off * elts_per_byte;
7729 : 244 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7730 : 244 : unsigned int elt_mask = (1 << elt_bits) - 1;
7731 : 2869 : for (unsigned int i = 0; i < extract_elts; ++i)
7732 : : {
7733 : 2625 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7734 : 2625 : if (TREE_CODE (elt) != INTEGER_CST)
7735 : : return 0;
7736 : :
7737 : 2625 : if (ptr && integer_nonzerop (elt))
7738 : : {
7739 : 913 : unsigned int bit = i * elt_bits;
7740 : 913 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7741 : : }
7742 : : }
7743 : : return extract_bytes;
7744 : : }
7745 : :
7746 : 867516 : int offset = 0;
7747 : 867516 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7748 : 2777772 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7749 : : {
7750 : 2442575 : if (off >= size)
7751 : : {
7752 : 23278 : off -= size;
7753 : 23278 : continue;
7754 : : }
7755 : 2419297 : tree elem = VECTOR_CST_ELT (expr, i);
7756 : 2419297 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7757 : : len - offset, off);
7758 : 2419297 : if ((off == -1 && res != size) || res == 0)
7759 : : return 0;
7760 : 2418773 : offset += res;
7761 : 2418773 : if (offset >= len)
7762 : 531795 : return (off == -1 && i < count - 1) ? 0 : offset;
7763 : 1886978 : if (off != -1)
7764 : 332826 : off = 0;
7765 : : }
7766 : : return offset;
7767 : : }
7768 : :
7769 : : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7770 : : specified by EXPR into the buffer PTR of length LEN bytes.
7771 : : Return the number of bytes placed in the buffer, or zero
7772 : : upon failure. */
7773 : :
7774 : : static int
7775 : 763956 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7776 : : {
7777 : 763956 : unsigned HOST_WIDE_INT count;
7778 : 763956 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7779 : : return 0;
7780 : 763956 : return native_encode_vector_part (expr, ptr, len, off, count);
7781 : : }
7782 : :
7783 : :
7784 : : /* Subroutine of native_encode_expr. Encode the STRING_CST
7785 : : specified by EXPR into the buffer PTR of length LEN bytes.
7786 : : Return the number of bytes placed in the buffer, or zero
7787 : : upon failure. */
7788 : :
7789 : : static int
7790 : 130858 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7791 : : {
7792 : 130858 : tree type = TREE_TYPE (expr);
7793 : :
7794 : : /* Wide-char strings are encoded in target byte-order so native
7795 : : encoding them is trivial. */
7796 : 130858 : if (BITS_PER_UNIT != CHAR_BIT
7797 : 130858 : || TREE_CODE (type) != ARRAY_TYPE
7798 : 130858 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7799 : 261716 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7800 : : return 0;
7801 : :
7802 : 130858 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7803 : 130858 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7804 : : return 0;
7805 : 130000 : if (off == -1)
7806 : 55690 : off = 0;
7807 : 130000 : len = MIN (total_bytes - off, len);
7808 : 130000 : if (ptr == NULL)
7809 : : /* Dry run. */;
7810 : : else
7811 : : {
7812 : 130000 : int written = 0;
7813 : 130000 : if (off < TREE_STRING_LENGTH (expr))
7814 : : {
7815 : 129519 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7816 : 129519 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7817 : : }
7818 : 130000 : memset (ptr + written, 0, len - written);
7819 : : }
7820 : : return len;
7821 : : }
7822 : :
7823 : : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7824 : : specified by EXPR into the buffer PTR of length LEN bytes.
7825 : : Return the number of bytes placed in the buffer, or zero
7826 : : upon failure. */
7827 : :
7828 : : static int
7829 : 44885 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7830 : : {
7831 : : /* We are only concerned with zero-initialization constructors here. That's
7832 : : all we expect to see in GIMPLE, so that's all native_encode_expr should
7833 : : deal with. For more general handling of constructors, there is
7834 : : native_encode_initializer. */
7835 : 44885 : if (CONSTRUCTOR_NELTS (expr))
7836 : : return 0;
7837 : :
7838 : : /* Wide-char strings are encoded in target byte-order so native
7839 : : encoding them is trivial. */
7840 : 84074 : if (BITS_PER_UNIT != CHAR_BIT
7841 : 42037 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7842 : : return 0;
7843 : :
7844 : 42037 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7845 : 42037 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7846 : : return 0;
7847 : 42037 : if (off == -1)
7848 : 0 : off = 0;
7849 : 42037 : len = MIN (total_bytes - off, len);
7850 : 42037 : if (ptr == NULL)
7851 : : /* Dry run. */;
7852 : : else
7853 : 42037 : memset (ptr, 0, len);
7854 : : return len;
7855 : : }
7856 : :
7857 : : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7858 : : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7859 : : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7860 : : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7861 : : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7862 : : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7863 : : Return the number of bytes placed in the buffer, or zero upon failure. */
7864 : :
7865 : : int
7866 : 33855097 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7867 : : {
7868 : : /* We don't support starting at negative offset and -1 is special. */
7869 : 33855097 : if (off < -1)
7870 : : return 0;
7871 : :
7872 : 33855085 : switch (TREE_CODE (expr))
7873 : : {
7874 : 19787732 : case INTEGER_CST:
7875 : 19787732 : return native_encode_int (expr, ptr, len, off);
7876 : :
7877 : 568263 : case REAL_CST:
7878 : 568263 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7879 : 1136526 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7880 : :
7881 : 0 : case FIXED_CST:
7882 : 0 : return native_encode_fixed (expr, ptr, len, off);
7883 : :
7884 : 20121 : case COMPLEX_CST:
7885 : 20121 : return native_encode_complex (expr, ptr, len, off);
7886 : :
7887 : 763956 : case VECTOR_CST:
7888 : 763956 : return native_encode_vector (expr, ptr, len, off);
7889 : :
7890 : 130858 : case STRING_CST:
7891 : 130858 : return native_encode_string (expr, ptr, len, off);
7892 : :
7893 : 44885 : case CONSTRUCTOR:
7894 : 44885 : return native_encode_constructor (expr, ptr, len, off);
7895 : :
7896 : : default:
7897 : : return 0;
7898 : : }
7899 : : }
7900 : :
7901 : : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7902 : : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7903 : : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7904 : : machine modes, we can't just use build_nonstandard_integer_type. */
7905 : :
7906 : : tree
7907 : 541 : find_bitfield_repr_type (int fieldsize, int len)
7908 : : {
7909 : 541 : machine_mode mode;
7910 : 1063 : for (int pass = 0; pass < 2; pass++)
7911 : : {
7912 : 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7913 : 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7914 : 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7915 : 7286 : && known_eq (GET_MODE_PRECISION (mode),
7916 : : GET_MODE_BITSIZE (mode))
7917 : 11274 : && known_le (GET_MODE_SIZE (mode), len))
7918 : : {
7919 : 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7920 : 280 : if (ret && TYPE_MODE (ret) == mode)
7921 : : return ret;
7922 : : }
7923 : : }
7924 : :
7925 : 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7926 : 261 : if (int_n_enabled_p[i]
7927 : 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7928 : 261 : && int_n_trees[i].unsigned_type)
7929 : : {
7930 : 261 : tree ret = int_n_trees[i].unsigned_type;
7931 : 261 : mode = TYPE_MODE (ret);
7932 : 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7933 : 522 : && known_eq (GET_MODE_PRECISION (mode),
7934 : : GET_MODE_BITSIZE (mode))
7935 : 783 : && known_le (GET_MODE_SIZE (mode), len))
7936 : : return ret;
7937 : : }
7938 : :
7939 : : return NULL_TREE;
7940 : : }
7941 : :
7942 : : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7943 : : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7944 : : to be non-NULL and OFF zero), then in addition to filling the
7945 : : bytes pointed by PTR with the value also clear any bits pointed
7946 : : by MASK that are known to be initialized, keep them as is for
7947 : : e.g. uninitialized padding bits or uninitialized fields. */
7948 : :
7949 : : int
7950 : 12347313 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7951 : : int off, unsigned char *mask)
7952 : : {
7953 : 12347313 : int r;
7954 : :
7955 : : /* We don't support starting at negative offset and -1 is special. */
7956 : 12347313 : if (off < -1 || init == NULL_TREE)
7957 : : return 0;
7958 : :
7959 : 12347313 : gcc_assert (mask == NULL || (off == 0 && ptr));
7960 : :
7961 : 12347313 : STRIP_NOPS (init);
7962 : 12347313 : switch (TREE_CODE (init))
7963 : : {
7964 : 0 : case VIEW_CONVERT_EXPR:
7965 : 0 : case NON_LVALUE_EXPR:
7966 : 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7967 : 0 : mask);
7968 : 11483997 : default:
7969 : 11483997 : r = native_encode_expr (init, ptr, len, off);
7970 : 11483997 : if (mask)
7971 : 1502 : memset (mask, 0, r);
7972 : : return r;
7973 : 863316 : case CONSTRUCTOR:
7974 : 863316 : tree type = TREE_TYPE (init);
7975 : 863316 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7976 : 863316 : if (total_bytes < 0)
7977 : : return 0;
7978 : 863316 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7979 : : return 0;
7980 : 863316 : int o = off == -1 ? 0 : off;
7981 : 863316 : if (TREE_CODE (type) == ARRAY_TYPE)
7982 : : {
7983 : 269183 : tree min_index;
7984 : 269183 : unsigned HOST_WIDE_INT cnt;
7985 : 269183 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7986 : 269183 : constructor_elt *ce;
7987 : :
7988 : 269183 : if (!TYPE_DOMAIN (type)
7989 : 269183 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7990 : : return 0;
7991 : :
7992 : 269183 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7993 : 269183 : if (fieldsize <= 0)
7994 : : return 0;
7995 : :
7996 : 269183 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7997 : 269183 : if (ptr)
7998 : 269183 : memset (ptr, '\0', MIN (total_bytes - off, len));
7999 : :
8000 : 11487214 : for (cnt = 0; ; cnt++)
8001 : : {
8002 : 11756397 : tree val = NULL_TREE, index = NULL_TREE;
8003 : 11756397 : HOST_WIDE_INT pos = curpos, count = 0;
8004 : 11756397 : bool full = false;
8005 : 11756397 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8006 : : {
8007 : 11710809 : val = ce->value;
8008 : 11710809 : index = ce->index;
8009 : : }
8010 : 45588 : else if (mask == NULL
8011 : 228 : || CONSTRUCTOR_NO_CLEARING (init)
8012 : 45816 : || curpos >= total_bytes)
8013 : : break;
8014 : : else
8015 : : pos = total_bytes;
8016 : :
8017 : 11710809 : if (index && TREE_CODE (index) == RANGE_EXPR)
8018 : : {
8019 : 16 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8020 : 16 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8021 : 0 : return 0;
8022 : 16 : offset_int wpos
8023 : 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8024 : 32 : - wi::to_offset (min_index),
8025 : 16 : TYPE_PRECISION (sizetype));
8026 : 16 : wpos *= fieldsize;
8027 : 16 : if (!wi::fits_shwi_p (pos))
8028 : : return 0;
8029 : 16 : pos = wpos.to_shwi ();
8030 : 16 : offset_int wcount
8031 : 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8032 : 32 : - wi::to_offset (TREE_OPERAND (index, 0)),
8033 : 16 : TYPE_PRECISION (sizetype));
8034 : 16 : if (!wi::fits_shwi_p (wcount))
8035 : : return 0;
8036 : 16 : count = wcount.to_shwi ();
8037 : 16 : }
8038 : 11056924 : else if (index)
8039 : : {
8040 : 11056924 : if (TREE_CODE (index) != INTEGER_CST)
8041 : 0 : return 0;
8042 : 11056924 : offset_int wpos
8043 : 11056924 : = wi::sext (wi::to_offset (index)
8044 : 22113848 : - wi::to_offset (min_index),
8045 : 11056924 : TYPE_PRECISION (sizetype));
8046 : 11056924 : wpos *= fieldsize;
8047 : 11056924 : if (!wi::fits_shwi_p (wpos))
8048 : : return 0;
8049 : 11056924 : pos = wpos.to_shwi ();
8050 : : }
8051 : :
8052 : 11711480 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8053 : : {
8054 : 14 : if (valueinit == -1)
8055 : : {
8056 : 14 : tree zero = build_zero_cst (TREE_TYPE (type));
8057 : 28 : r = native_encode_initializer (zero, ptr + curpos,
8058 : : fieldsize, 0,
8059 : 14 : mask + curpos);
8060 : 14 : if (TREE_CODE (zero) == CONSTRUCTOR)
8061 : 0 : ggc_free (zero);
8062 : 14 : if (!r)
8063 : : return 0;
8064 : 14 : valueinit = curpos;
8065 : 14 : curpos += fieldsize;
8066 : : }
8067 : 44 : while (curpos != pos)
8068 : : {
8069 : 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8070 : 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8071 : 30 : curpos += fieldsize;
8072 : : }
8073 : : }
8074 : :
8075 : 11710823 : curpos = pos;
8076 : 11710823 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8077 : : {
8078 : 474 : if (count)
8079 : : return 0;
8080 : 474 : if (off == -1
8081 : 474 : || (curpos >= off
8082 : 0 : && (curpos + RAW_DATA_LENGTH (val)
8083 : 0 : <= (HOST_WIDE_INT) off + len)))
8084 : : {
8085 : 474 : if (ptr)
8086 : 474 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8087 : 474 : RAW_DATA_LENGTH (val));
8088 : 474 : if (mask)
8089 : 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8090 : : }
8091 : 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8092 : 0 : && curpos < (HOST_WIDE_INT) off + len)
8093 : : {
8094 : : /* Partial overlap. */
8095 : 0 : unsigned char *p = NULL;
8096 : 0 : int no = 0;
8097 : 0 : int l;
8098 : 0 : gcc_assert (mask == NULL);
8099 : 0 : if (curpos >= off)
8100 : : {
8101 : 0 : if (ptr)
8102 : 0 : p = ptr + curpos - off;
8103 : 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8104 : : RAW_DATA_LENGTH (val));
8105 : : }
8106 : : else
8107 : : {
8108 : 0 : p = ptr;
8109 : 0 : no = off - curpos;
8110 : 0 : l = len;
8111 : : }
8112 : 0 : if (p)
8113 : 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8114 : : }
8115 : 474 : curpos += RAW_DATA_LENGTH (val);
8116 : 474 : val = NULL_TREE;
8117 : : }
8118 : 474 : if (val)
8119 : 11788375 : do
8120 : : {
8121 : 11788375 : if (off == -1
8122 : 761992 : || (curpos >= off
8123 : 247080 : && (curpos + fieldsize
8124 : 247080 : <= (HOST_WIDE_INT) off + len)))
8125 : : {
8126 : 11239743 : if (full)
8127 : : {
8128 : 78040 : if (ptr)
8129 : 78040 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8130 : : fieldsize);
8131 : 78040 : if (mask)
8132 : 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8133 : : }
8134 : 22537409 : else if (!native_encode_initializer (val,
8135 : : ptr
8136 : 11161703 : ? ptr + curpos - o
8137 : : : NULL,
8138 : : fieldsize,
8139 : : off == -1 ? -1
8140 : : : 0,
8141 : : mask
8142 : 643 : ? mask + curpos
8143 : : : NULL))
8144 : : return 0;
8145 : : else
8146 : : {
8147 : : full = true;
8148 : : pos = curpos;
8149 : : }
8150 : : }
8151 : 548632 : else if (curpos + fieldsize > off
8152 : 36295 : && curpos < (HOST_WIDE_INT) off + len)
8153 : : {
8154 : : /* Partial overlap. */
8155 : 9312 : unsigned char *p = NULL;
8156 : 9312 : int no = 0;
8157 : 9312 : int l;
8158 : 9312 : gcc_assert (mask == NULL);
8159 : 9312 : if (curpos >= off)
8160 : : {
8161 : 6737 : if (ptr)
8162 : 6737 : p = ptr + curpos - off;
8163 : 6737 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8164 : : fieldsize);
8165 : : }
8166 : : else
8167 : : {
8168 : 2575 : p = ptr;
8169 : 2575 : no = off - curpos;
8170 : 2575 : l = len;
8171 : : }
8172 : 9312 : if (!native_encode_initializer (val, p, l, no, NULL))
8173 : : return 0;
8174 : : }
8175 : 11564766 : curpos += fieldsize;
8176 : : }
8177 : 11564766 : while (count-- != 0);
8178 : 11487214 : }
8179 : 45574 : return MIN (total_bytes - off, len);
8180 : : }
8181 : 594133 : else if (TREE_CODE (type) == RECORD_TYPE
8182 : 594133 : || TREE_CODE (type) == UNION_TYPE)
8183 : : {
8184 : 594133 : unsigned HOST_WIDE_INT cnt;
8185 : 594133 : constructor_elt *ce;
8186 : 594133 : tree fld_base = TYPE_FIELDS (type);
8187 : 594133 : tree to_free = NULL_TREE;
8188 : :
8189 : 594133 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8190 : 594133 : if (ptr != NULL)
8191 : 594133 : memset (ptr, '\0', MIN (total_bytes - o, len));
8192 : 104146 : for (cnt = 0; ; cnt++)
8193 : : {
8194 : 698279 : tree val = NULL_TREE, field = NULL_TREE;
8195 : 698279 : HOST_WIDE_INT pos = 0, fieldsize;
8196 : 698279 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8197 : :
8198 : 698279 : if (to_free)
8199 : : {
8200 : 0 : ggc_free (to_free);
8201 : 0 : to_free = NULL_TREE;
8202 : : }
8203 : :
8204 : 698279 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8205 : : {
8206 : 127218 : val = ce->value;
8207 : 127218 : field = ce->index;
8208 : 127218 : if (field == NULL_TREE)
8209 : : return 0;
8210 : :
8211 : 127218 : pos = int_byte_position (field);
8212 : 127218 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8213 : 1273 : continue;
8214 : : }
8215 : 571061 : else if (mask == NULL
8216 : 571061 : || CONSTRUCTOR_NO_CLEARING (init))
8217 : : break;
8218 : : else
8219 : : pos = total_bytes;
8220 : :
8221 : 128142 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8222 : : {
8223 : : tree fld;
8224 : 11729 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8225 : : {
8226 : 11230 : if (TREE_CODE (fld) != FIELD_DECL)
8227 : 10077 : continue;
8228 : 1153 : if (fld == field)
8229 : : break;
8230 : 146 : if (DECL_PADDING_P (fld))
8231 : 87 : continue;
8232 : 59 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8233 : 59 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8234 : : return 0;
8235 : 59 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8236 : 2 : continue;
8237 : : break;
8238 : : }
8239 : 1563 : if (fld == NULL_TREE)
8240 : : {
8241 : 499 : if (ce == NULL)
8242 : : break;
8243 : : return 0;
8244 : : }
8245 : 1064 : fld_base = DECL_CHAIN (fld);
8246 : 1064 : if (fld != field)
8247 : : {
8248 : 57 : cnt--;
8249 : 57 : field = fld;
8250 : 57 : pos = int_byte_position (field);
8251 : 57 : val = build_zero_cst (TREE_TYPE (fld));
8252 : 57 : if (TREE_CODE (val) == CONSTRUCTOR)
8253 : 0 : to_free = val;
8254 : : }
8255 : : }
8256 : :
8257 : 126002 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8258 : 5949 : && TYPE_DOMAIN (TREE_TYPE (field))
8259 : 131951 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8260 : : {
8261 : 81 : if (mask || off != -1)
8262 : : return 0;
8263 : 81 : if (val == NULL_TREE)
8264 : 0 : continue;
8265 : 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8266 : : return 0;
8267 : 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8268 : 81 : if (fieldsize < 0
8269 : 81 : || (int) fieldsize != fieldsize
8270 : 81 : || (pos + fieldsize) > INT_MAX)
8271 : : return 0;
8272 : 81 : if (pos + fieldsize > total_bytes)
8273 : : {
8274 : 81 : if (ptr != NULL && total_bytes < len)
8275 : 81 : memset (ptr + total_bytes, '\0',
8276 : 81 : MIN (pos + fieldsize, len) - total_bytes);
8277 : : total_bytes = pos + fieldsize;
8278 : : }
8279 : : }
8280 : : else
8281 : : {
8282 : 125921 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8283 : 125921 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8284 : : return 0;
8285 : 125921 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8286 : : }
8287 : 126002 : if (fieldsize == 0)
8288 : 1 : continue;
8289 : :
8290 : : /* Prepare to deal with integral bit-fields and filter out other
8291 : : bit-fields that do not start and end on a byte boundary. */
8292 : 126001 : if (DECL_BIT_FIELD (field))
8293 : : {
8294 : 2663 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8295 : : return 0;
8296 : 2663 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8297 : 2663 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8298 : : {
8299 : 2663 : bpos %= BITS_PER_UNIT;
8300 : 2663 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8301 : 2663 : epos = fieldsize % BITS_PER_UNIT;
8302 : 2663 : fieldsize += BITS_PER_UNIT - 1;
8303 : 2663 : fieldsize /= BITS_PER_UNIT;
8304 : : }
8305 : 0 : else if (bpos % BITS_PER_UNIT
8306 : 0 : || DECL_SIZE (field) == NULL_TREE
8307 : 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8308 : 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8309 : : return 0;
8310 : : }
8311 : :
8312 : 126001 : if (off != -1 && pos + fieldsize <= off)
8313 : 3768 : continue;
8314 : :
8315 : 122233 : if (val == NULL_TREE)
8316 : 0 : continue;
8317 : :
8318 : 122233 : if (DECL_BIT_FIELD (field)
8319 : 122233 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8320 : : {
8321 : : /* FIXME: Handle PDP endian. */
8322 : 2459 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8323 : 261 : return 0;
8324 : :
8325 : 2459 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8326 : 6 : val = TREE_OPERAND (val, 0);
8327 : 2459 : if (TREE_CODE (val) != INTEGER_CST)
8328 : : return 0;
8329 : :
8330 : 2459 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8331 : 2459 : tree repr_type = NULL_TREE;
8332 : 2459 : HOST_WIDE_INT rpos = 0;
8333 : 2459 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8334 : : {
8335 : 1930 : rpos = int_byte_position (repr);
8336 : 1930 : repr_type = TREE_TYPE (repr);
8337 : : }
8338 : : else
8339 : : {
8340 : 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8341 : 529 : if (repr_type == NULL_TREE)
8342 : : return 0;
8343 : 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8344 : 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8345 : 268 : if (pos + repr_size <= o + len)
8346 : : rpos = pos;
8347 : : else
8348 : : {
8349 : 14 : rpos = o + len - repr_size;
8350 : 14 : gcc_assert (rpos <= pos);
8351 : : }
8352 : : }
8353 : :
8354 : 2198 : if (rpos > pos)
8355 : : return 0;
8356 : 2198 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8357 : 2198 : int diff = (TYPE_PRECISION (repr_type)
8358 : 2198 : - TYPE_PRECISION (TREE_TYPE (field)));
8359 : 2198 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8360 : 2198 : if (!BYTES_BIG_ENDIAN)
8361 : 2198 : w = wi::lshift (w, bitoff);
8362 : : else
8363 : : w = wi::lshift (w, diff - bitoff);
8364 : 2198 : val = wide_int_to_tree (repr_type, w);
8365 : :
8366 : 2198 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8367 : : / BITS_PER_UNIT + 1];
8368 : 2198 : int l = native_encode_int (val, buf, sizeof buf, 0);
8369 : 2198 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8370 : 0 : return 0;
8371 : :
8372 : 2198 : if (ptr == NULL)
8373 : 0 : continue;
8374 : :
8375 : : /* If the bitfield does not start at byte boundary, handle
8376 : : the partial byte at the start. */
8377 : 2198 : if (bpos
8378 : 1316 : && (off == -1 || (pos >= off && len >= 1)))
8379 : : {
8380 : 1241 : if (!BYTES_BIG_ENDIAN)
8381 : : {
8382 : 1241 : int msk = (1 << bpos) - 1;
8383 : 1241 : buf[pos - rpos] &= ~msk;
8384 : 1241 : buf[pos - rpos] |= ptr[pos - o] & msk;
8385 : 1241 : if (mask)
8386 : : {
8387 : 147 : if (fieldsize > 1 || epos == 0)
8388 : 129 : mask[pos] &= msk;
8389 : : else
8390 : 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8391 : : }
8392 : : }
8393 : : else
8394 : : {
8395 : : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8396 : : buf[pos - rpos] &= msk;
8397 : : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8398 : : if (mask)
8399 : : {
8400 : : if (fieldsize > 1 || epos == 0)
8401 : : mask[pos] &= ~msk;
8402 : : else
8403 : : mask[pos] &= (~msk
8404 : : | ((1 << (BITS_PER_UNIT - epos))
8405 : : - 1));
8406 : : }
8407 : : }
8408 : : }
8409 : : /* If the bitfield does not end at byte boundary, handle
8410 : : the partial byte at the end. */
8411 : 2198 : if (epos
8412 : 1691 : && (off == -1
8413 : 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8414 : : {
8415 : 1588 : if (!BYTES_BIG_ENDIAN)
8416 : : {
8417 : 1588 : int msk = (1 << epos) - 1;
8418 : 1588 : buf[pos - rpos + fieldsize - 1] &= msk;
8419 : 1588 : buf[pos - rpos + fieldsize - 1]
8420 : 1588 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8421 : 1588 : if (mask && (fieldsize > 1 || bpos == 0))
8422 : 156 : mask[pos + fieldsize - 1] &= ~msk;
8423 : : }
8424 : : else
8425 : : {
8426 : : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8427 : : buf[pos - rpos + fieldsize - 1] &= ~msk;
8428 : : buf[pos - rpos + fieldsize - 1]
8429 : : |= ptr[pos + fieldsize - 1 - o] & msk;
8430 : : if (mask && (fieldsize > 1 || bpos == 0))
8431 : : mask[pos + fieldsize - 1] &= msk;
8432 : : }
8433 : : }
8434 : 2198 : if (off == -1
8435 : 1301 : || (pos >= off
8436 : 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8437 : : {
8438 : 2007 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8439 : 2007 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8440 : 75 : memset (mask + pos + (bpos != 0), 0,
8441 : 75 : fieldsize - (bpos != 0) - (epos != 0));
8442 : : }
8443 : : else
8444 : : {
8445 : : /* Partial overlap. */
8446 : 191 : HOST_WIDE_INT fsz = fieldsize;
8447 : 191 : gcc_assert (mask == NULL);
8448 : 191 : if (pos < off)
8449 : : {
8450 : 89 : fsz -= (off - pos);
8451 : 89 : pos = off;
8452 : : }
8453 : 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8454 : 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8455 : 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8456 : : }
8457 : 2198 : continue;
8458 : 2198 : }
8459 : :
8460 : 119774 : if (off == -1
8461 : 19911 : || (pos >= off
8462 : 19064 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8463 : : {
8464 : 110221 : int fldsize = fieldsize;
8465 : 10358 : if (off == -1)
8466 : : {
8467 : 99863 : tree fld = DECL_CHAIN (field);
8468 : 1330165 : while (fld)
8469 : : {
8470 : 1239680 : if (TREE_CODE (fld) == FIELD_DECL)
8471 : : break;
8472 : 1230302 : fld = DECL_CHAIN (fld);
8473 : : }
8474 : 99863 : if (fld == NULL_TREE)
8475 : 90485 : fldsize = len - pos;
8476 : : }
8477 : 121403 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8478 : : : NULL,
8479 : : fldsize,
8480 : : off == -1 ? -1 : 0,
8481 : 824 : mask ? mask + pos : NULL);
8482 : 110221 : if (!r)
8483 : : return 0;
8484 : 95856 : if (off == -1
8485 : 93893 : && fldsize != fieldsize
8486 : 324 : && r > fieldsize
8487 : 54 : && pos + r > total_bytes)
8488 : 104146 : total_bytes = pos + r;
8489 : : }
8490 : : else
8491 : : {
8492 : : /* Partial overlap. */
8493 : 9553 : unsigned char *p = NULL;
8494 : 9553 : int no = 0;
8495 : 9553 : int l;
8496 : 9553 : gcc_assert (mask == NULL);
8497 : 9553 : if (pos >= off)
8498 : : {
8499 : 8706 : if (ptr)
8500 : 8706 : p = ptr + pos - off;
8501 : 8706 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8502 : : fieldsize);
8503 : : }
8504 : : else
8505 : : {
8506 : 847 : p = ptr;
8507 : 847 : no = off - pos;
8508 : 847 : l = len;
8509 : : }
8510 : 9553 : if (!native_encode_initializer (val, p, l, no, NULL))
8511 : : return 0;
8512 : : }
8513 : 104146 : }
8514 : 571004 : return MIN (total_bytes - off, len);
8515 : : }
8516 : : return 0;
8517 : : }
8518 : : }
8519 : :
8520 : :
8521 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8522 : : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8523 : : If the buffer cannot be interpreted, return NULL_TREE. */
8524 : :
8525 : : static tree
8526 : 2676730 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8527 : : {
8528 : 2676730 : int total_bytes;
8529 : 2676730 : if (TREE_CODE (type) == BITINT_TYPE)
8530 : : {
8531 : 17 : struct bitint_info info;
8532 : 17 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8533 : 17 : gcc_assert (ok);
8534 : 17 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8535 : 17 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8536 : : {
8537 : 17 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8538 : : /* More work is needed when adding _BitInt support to PDP endian
8539 : : if limb is smaller than word, or if _BitInt limb ordering doesn't
8540 : : match target endianity here. */
8541 : 17 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8542 : : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8543 : : || (GET_MODE_SIZE (limb_mode)
8544 : : >= UNITS_PER_WORD)));
8545 : : }
8546 : : else
8547 : 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8548 : : }
8549 : : else
8550 : 5353426 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8551 : :
8552 : 2676730 : if (total_bytes > len)
8553 : : return NULL_TREE;
8554 : :
8555 : 2676513 : wide_int result = wi::from_buffer (ptr, total_bytes);
8556 : :
8557 : 2676513 : return wide_int_to_tree (type, result);
8558 : 2676513 : }
8559 : :
8560 : :
8561 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8562 : : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8563 : : If the buffer cannot be interpreted, return NULL_TREE. */
8564 : :
8565 : : static tree
8566 : 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8567 : : {
8568 : 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8569 : 0 : int total_bytes = GET_MODE_SIZE (mode);
8570 : 0 : double_int result;
8571 : 0 : FIXED_VALUE_TYPE fixed_value;
8572 : :
8573 : 0 : if (total_bytes > len
8574 : 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8575 : : return NULL_TREE;
8576 : :
8577 : 0 : result = double_int::from_buffer (ptr, total_bytes);
8578 : 0 : fixed_value = fixed_from_double_int (result, mode);
8579 : :
8580 : 0 : return build_fixed (type, fixed_value);
8581 : : }
8582 : :
8583 : :
8584 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8585 : : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8586 : : If the buffer cannot be interpreted, return NULL_TREE. */
8587 : :
8588 : : tree
8589 : 30959 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8590 : : {
8591 : 30959 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8592 : 30959 : int total_bytes = GET_MODE_SIZE (mode);
8593 : 30959 : unsigned char value;
8594 : : /* There are always 32 bits in each long, no matter the size of
8595 : : the hosts long. We handle floating point representations with
8596 : : up to 192 bits. */
8597 : 30959 : REAL_VALUE_TYPE r;
8598 : 30959 : long tmp[6];
8599 : :
8600 : 30959 : if (total_bytes > len || total_bytes > 24)
8601 : : return NULL_TREE;
8602 : 30898 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8603 : :
8604 : 30898 : memset (tmp, 0, sizeof (tmp));
8605 : 223232 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8606 : 192334 : bitpos += BITS_PER_UNIT)
8607 : : {
8608 : : /* Both OFFSET and BYTE index within a long;
8609 : : bitpos indexes the whole float. */
8610 : 192334 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8611 : 192334 : if (UNITS_PER_WORD < 4)
8612 : : {
8613 : : int word = byte / UNITS_PER_WORD;
8614 : : if (WORDS_BIG_ENDIAN)
8615 : : word = (words - 1) - word;
8616 : : offset = word * UNITS_PER_WORD;
8617 : : if (BYTES_BIG_ENDIAN)
8618 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8619 : : else
8620 : : offset += byte % UNITS_PER_WORD;
8621 : : }
8622 : : else
8623 : : {
8624 : 192334 : offset = byte;
8625 : 192334 : if (BYTES_BIG_ENDIAN)
8626 : : {
8627 : : /* Reverse bytes within each long, or within the entire float
8628 : : if it's smaller than a long (for HFmode). */
8629 : : offset = MIN (3, total_bytes - 1) - offset;
8630 : : gcc_assert (offset >= 0);
8631 : : }
8632 : : }
8633 : 192334 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8634 : :
8635 : 192334 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8636 : : }
8637 : :
8638 : 30898 : real_from_target (&r, tmp, mode);
8639 : 30898 : return build_real (type, r);
8640 : : }
8641 : :
8642 : :
8643 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8644 : : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8645 : : If the buffer cannot be interpreted, return NULL_TREE. */
8646 : :
8647 : : static tree
8648 : 1377 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8649 : : {
8650 : 1377 : tree etype, rpart, ipart;
8651 : 1377 : int size;
8652 : :
8653 : 1377 : etype = TREE_TYPE (type);
8654 : 1377 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8655 : 1377 : if (size * 2 > len)
8656 : : return NULL_TREE;
8657 : 1342 : rpart = native_interpret_expr (etype, ptr, size);
8658 : 1342 : if (!rpart)
8659 : : return NULL_TREE;
8660 : 1341 : ipart = native_interpret_expr (etype, ptr+size, size);
8661 : 1341 : if (!ipart)
8662 : : return NULL_TREE;
8663 : 1341 : return build_complex (type, rpart, ipart);
8664 : : }
8665 : :
8666 : : /* Read a vector of type TYPE from the target memory image given by BYTES,
8667 : : which contains LEN bytes. The vector is known to be encodable using
8668 : : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8669 : :
8670 : : Return the vector on success, otherwise return null. */
8671 : :
8672 : : static tree
8673 : 176303 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8674 : : unsigned int len, unsigned int npatterns,
8675 : : unsigned int nelts_per_pattern)
8676 : : {
8677 : 176303 : tree elt_type = TREE_TYPE (type);
8678 : 176303 : if (VECTOR_BOOLEAN_TYPE_P (type)
8679 : 176306 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8680 : : {
8681 : : /* This is the only case in which elements can be smaller than a byte.
8682 : : Element 0 is always in the lsb of the containing byte. */
8683 : 1 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8684 : 1 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8685 : : return NULL_TREE;
8686 : :
8687 : 1 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8688 : 17 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8689 : : {
8690 : 16 : unsigned int bit_index = i * elt_bits;
8691 : 16 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8692 : 16 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8693 : 32 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8694 : 17 : ? build_all_ones_cst (elt_type)
8695 : 1 : : build_zero_cst (elt_type));
8696 : : }
8697 : 1 : return builder.build ();
8698 : 1 : }
8699 : :
8700 : 176302 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8701 : 176302 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8702 : : return NULL_TREE;
8703 : :
8704 : 176302 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8705 : 707869 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8706 : : {
8707 : 531605 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8708 : 531605 : if (!elt)
8709 : 38 : return NULL_TREE;
8710 : 531567 : builder.quick_push (elt);
8711 : 531567 : bytes += elt_bytes;
8712 : : }
8713 : 176264 : return builder.build ();
8714 : 176302 : }
8715 : :
8716 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8717 : : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8718 : : If the buffer cannot be interpreted, return NULL_TREE. */
8719 : :
8720 : : static tree
8721 : 72501 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8722 : : {
8723 : 72501 : unsigned HOST_WIDE_INT size;
8724 : :
8725 : 72501 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8726 : 72501 : || size > len)
8727 : 2 : return NULL_TREE;
8728 : :
8729 : 72499 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8730 : 72499 : return native_interpret_vector_part (type, ptr, len, count, 1);
8731 : : }
8732 : :
8733 : :
8734 : : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8735 : : the buffer PTR of length LEN as a constant of type TYPE. For
8736 : : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8737 : : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8738 : : return NULL_TREE. */
8739 : :
8740 : : tree
8741 : 2834341 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8742 : : {
8743 : 2834341 : switch (TREE_CODE (type))
8744 : : {
8745 : 2676730 : case INTEGER_TYPE:
8746 : 2676730 : case ENUMERAL_TYPE:
8747 : 2676730 : case BOOLEAN_TYPE:
8748 : 2676730 : case POINTER_TYPE:
8749 : 2676730 : case REFERENCE_TYPE:
8750 : 2676730 : case OFFSET_TYPE:
8751 : 2676730 : case BITINT_TYPE:
8752 : 2676730 : return native_interpret_int (type, ptr, len);
8753 : :
8754 : 29745 : case REAL_TYPE:
8755 : 29745 : if (tree ret = native_interpret_real (type, ptr, len))
8756 : : {
8757 : : /* For floating point values in composite modes, punt if this
8758 : : folding doesn't preserve bit representation. As the mode doesn't
8759 : : have fixed precision while GCC pretends it does, there could be
8760 : : valid values that GCC can't really represent accurately.
8761 : : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8762 : : bit combinationations which GCC doesn't preserve. */
8763 : 29684 : unsigned char buf[24 * 2];
8764 : 29684 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8765 : 29684 : int total_bytes = GET_MODE_SIZE (mode);
8766 : 29684 : memcpy (buf + 24, ptr, total_bytes);
8767 : 29684 : clear_type_padding_in_mask (type, buf + 24);
8768 : 29684 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8769 : 29684 : || memcmp (buf + 24, buf, total_bytes) != 0)
8770 : 156 : return NULL_TREE;
8771 : : return ret;
8772 : : }
8773 : : return NULL_TREE;
8774 : :
8775 : 0 : case FIXED_POINT_TYPE:
8776 : 0 : return native_interpret_fixed (type, ptr, len);
8777 : :
8778 : 1377 : case COMPLEX_TYPE:
8779 : 1377 : return native_interpret_complex (type, ptr, len);
8780 : :
8781 : 72501 : case VECTOR_TYPE:
8782 : 72501 : return native_interpret_vector (type, ptr, len);
8783 : :
8784 : : default:
8785 : : return NULL_TREE;
8786 : : }
8787 : : }
8788 : :
8789 : : /* Returns true if we can interpret the contents of a native encoding
8790 : : as TYPE. */
8791 : :
8792 : : bool
8793 : 401386 : can_native_interpret_type_p (tree type)
8794 : : {
8795 : 401386 : switch (TREE_CODE (type))
8796 : : {
8797 : : case INTEGER_TYPE:
8798 : : case ENUMERAL_TYPE:
8799 : : case BOOLEAN_TYPE:
8800 : : case POINTER_TYPE:
8801 : : case REFERENCE_TYPE:
8802 : : case FIXED_POINT_TYPE:
8803 : : case REAL_TYPE:
8804 : : case COMPLEX_TYPE:
8805 : : case VECTOR_TYPE:
8806 : : case OFFSET_TYPE:
8807 : : return true;
8808 : 84236 : default:
8809 : 84236 : return false;
8810 : : }
8811 : : }
8812 : :
8813 : : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8814 : : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8815 : :
8816 : : tree
8817 : 603 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8818 : : int len)
8819 : : {
8820 : 603 : vec<constructor_elt, va_gc> *elts = NULL;
8821 : 603 : if (TREE_CODE (type) == ARRAY_TYPE)
8822 : : {
8823 : 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8824 : 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8825 : : return NULL_TREE;
8826 : :
8827 : 197 : HOST_WIDE_INT cnt = 0;
8828 : 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8829 : : {
8830 : 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8831 : : return NULL_TREE;
8832 : 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8833 : : }
8834 : 197 : if (eltsz == 0)
8835 : 0 : cnt = 0;
8836 : 197 : HOST_WIDE_INT pos = 0;
8837 : 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8838 : : {
8839 : 439 : tree v = NULL_TREE;
8840 : 439 : if (pos >= len || pos + eltsz > len)
8841 : 603 : return NULL_TREE;
8842 : 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8843 : : {
8844 : 367 : v = native_interpret_expr (TREE_TYPE (type),
8845 : 367 : ptr + off + pos, eltsz);
8846 : 367 : if (v == NULL_TREE)
8847 : : return NULL_TREE;
8848 : : }
8849 : 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8850 : 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8851 : 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8852 : : eltsz);
8853 : 72 : if (v == NULL_TREE)
8854 : 0 : return NULL_TREE;
8855 : 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8856 : : }
8857 : 197 : return build_constructor (type, elts);
8858 : : }
8859 : 406 : if (TREE_CODE (type) != RECORD_TYPE)
8860 : : return NULL_TREE;
8861 : 6618 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8862 : : {
8863 : 1138 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8864 : 7350 : || is_empty_type (TREE_TYPE (field)))
8865 : 5164 : continue;
8866 : 1048 : tree fld = field;
8867 : 1048 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8868 : 1048 : int diff = 0;
8869 : 1048 : tree v = NULL_TREE;
8870 : 1048 : if (DECL_BIT_FIELD (field))
8871 : : {
8872 : 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8873 : 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8874 : : {
8875 : 168 : poly_int64 bitoffset;
8876 : 168 : poly_uint64 field_offset, fld_offset;
8877 : 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8878 : 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8879 : 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8880 : : else
8881 : : bitoffset = 0;
8882 : 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8883 : 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8884 : 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8885 : 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8886 : 168 : if (!bitoffset.is_constant (&bitoff)
8887 : 168 : || bitoff < 0
8888 : 168 : || bitoff > diff)
8889 : 0 : return NULL_TREE;
8890 : : }
8891 : : else
8892 : : {
8893 : 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8894 : : return NULL_TREE;
8895 : 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8896 : 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8897 : 12 : bpos %= BITS_PER_UNIT;
8898 : 12 : fieldsize += bpos;
8899 : 12 : fieldsize += BITS_PER_UNIT - 1;
8900 : 12 : fieldsize /= BITS_PER_UNIT;
8901 : 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8902 : 12 : if (repr_type == NULL_TREE)
8903 : : return NULL_TREE;
8904 : 12 : sz = int_size_in_bytes (repr_type);
8905 : 12 : if (sz < 0 || sz > len)
8906 : : return NULL_TREE;
8907 : 12 : pos = int_byte_position (field);
8908 : 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8909 : : return NULL_TREE;
8910 : 12 : HOST_WIDE_INT rpos;
8911 : 12 : if (pos + sz <= len)
8912 : : rpos = pos;
8913 : : else
8914 : : {
8915 : 0 : rpos = len - sz;
8916 : 0 : gcc_assert (rpos <= pos);
8917 : : }
8918 : 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8919 : 12 : pos = rpos;
8920 : 12 : diff = (TYPE_PRECISION (repr_type)
8921 : 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8922 : 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8923 : 12 : if (v == NULL_TREE)
8924 : : return NULL_TREE;
8925 : : fld = NULL_TREE;
8926 : : }
8927 : : }
8928 : :
8929 : 168 : if (fld)
8930 : : {
8931 : 1036 : sz = int_size_in_bytes (TREE_TYPE (fld));
8932 : 1036 : if (sz < 0 || sz > len)
8933 : : return NULL_TREE;
8934 : 1036 : tree byte_pos = byte_position (fld);
8935 : 1036 : if (!tree_fits_shwi_p (byte_pos))
8936 : : return NULL_TREE;
8937 : 1036 : pos = tree_to_shwi (byte_pos);
8938 : 1036 : if (pos < 0 || pos > len || pos + sz > len)
8939 : : return NULL_TREE;
8940 : : }
8941 : 1036 : if (fld == NULL_TREE)
8942 : : /* Already handled above. */;
8943 : 1036 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8944 : : {
8945 : 844 : v = native_interpret_expr (TREE_TYPE (fld),
8946 : 844 : ptr + off + pos, sz);
8947 : 844 : if (v == NULL_TREE)
8948 : : return NULL_TREE;
8949 : : }
8950 : 192 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8951 : 192 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8952 : 192 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8953 : 204 : if (v == NULL_TREE)
8954 : : return NULL_TREE;
8955 : 1048 : if (fld != field)
8956 : : {
8957 : 180 : if (TREE_CODE (v) != INTEGER_CST)
8958 : : return NULL_TREE;
8959 : :
8960 : : /* FIXME: Figure out how to handle PDP endian bitfields. */
8961 : 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8962 : : return NULL_TREE;
8963 : 180 : if (!BYTES_BIG_ENDIAN)
8964 : 180 : v = wide_int_to_tree (TREE_TYPE (field),
8965 : 360 : wi::lrshift (wi::to_wide (v), bitoff));
8966 : : else
8967 : : v = wide_int_to_tree (TREE_TYPE (field),
8968 : : wi::lrshift (wi::to_wide (v),
8969 : : diff - bitoff));
8970 : : }
8971 : 1048 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8972 : : }
8973 : 406 : return build_constructor (type, elts);
8974 : : }
8975 : :
8976 : : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8977 : : or extracted constant positions and/or sizes aren't byte aligned. */
8978 : :
8979 : : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8980 : : bits between adjacent elements. AMNT should be within
8981 : : [0, BITS_PER_UNIT).
8982 : : Example, AMNT = 2:
8983 : : 00011111|11100000 << 2 = 01111111|10000000
8984 : : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8985 : :
8986 : : void
8987 : 40435 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8988 : : unsigned int amnt)
8989 : : {
8990 : 40435 : if (amnt == 0)
8991 : : return;
8992 : :
8993 : 23725 : unsigned char carry_over = 0U;
8994 : 23725 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8995 : 23725 : unsigned char clear_mask = (~0U) << amnt;
8996 : :
8997 : 131135 : for (unsigned int i = 0; i < sz; i++)
8998 : : {
8999 : 107410 : unsigned prev_carry_over = carry_over;
9000 : 107410 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9001 : :
9002 : 107410 : ptr[i] <<= amnt;
9003 : 107410 : if (i != 0)
9004 : : {
9005 : 83685 : ptr[i] &= clear_mask;
9006 : 83685 : ptr[i] |= prev_carry_over;
9007 : : }
9008 : : }
9009 : : }
9010 : :
9011 : : /* Like shift_bytes_in_array_left but for big-endian.
9012 : : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9013 : : bits between adjacent elements. AMNT should be within
9014 : : [0, BITS_PER_UNIT).
9015 : : Example, AMNT = 2:
9016 : : 00011111|11100000 >> 2 = 00000111|11111000
9017 : : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9018 : :
9019 : : void
9020 : 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9021 : : unsigned int amnt)
9022 : : {
9023 : 8 : if (amnt == 0)
9024 : : return;
9025 : :
9026 : 4 : unsigned char carry_over = 0U;
9027 : 4 : unsigned char carry_mask = ~(~0U << amnt);
9028 : :
9029 : 12 : for (unsigned int i = 0; i < sz; i++)
9030 : : {
9031 : 8 : unsigned prev_carry_over = carry_over;
9032 : 8 : carry_over = ptr[i] & carry_mask;
9033 : :
9034 : 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9035 : 8 : ptr[i] >>= amnt;
9036 : 8 : ptr[i] |= prev_carry_over;
9037 : : }
9038 : : }
9039 : :
9040 : : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9041 : : directly on the VECTOR_CST encoding, in a way that works for variable-
9042 : : length vectors. Return the resulting VECTOR_CST on success or null
9043 : : on failure. */
9044 : :
9045 : : static tree
9046 : 109890 : fold_view_convert_vector_encoding (tree type, tree expr)
9047 : : {
9048 : 109890 : tree expr_type = TREE_TYPE (expr);
9049 : 109890 : poly_uint64 type_bits, expr_bits;
9050 : 109890 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9051 : 109890 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9052 : 0 : return NULL_TREE;
9053 : :
9054 : 109890 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9055 : 109890 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9056 : 109890 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9057 : 109890 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9058 : :
9059 : : /* We can only preserve the semantics of a stepped pattern if the new
9060 : : vector element is an integer of the same size. */
9061 : 109890 : if (VECTOR_CST_STEPPED_P (expr)
9062 : 109890 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9063 : : return NULL_TREE;
9064 : :
9065 : : /* The number of bits needed to encode one element from every pattern
9066 : : of the original vector. */
9067 : 103804 : unsigned int expr_sequence_bits
9068 : 103804 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9069 : :
9070 : : /* The number of bits needed to encode one element from every pattern
9071 : : of the result. */
9072 : 103804 : unsigned int type_sequence_bits
9073 : 103804 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9074 : :
9075 : : /* Don't try to read more bytes than are available, which can happen
9076 : : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9077 : : The general VIEW_CONVERT handling can cope with that case, so there's
9078 : : no point complicating things here. */
9079 : 103804 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9080 : 103804 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9081 : : BITS_PER_UNIT);
9082 : 103804 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9083 : 103804 : if (known_gt (buffer_bits, expr_bits))
9084 : : return NULL_TREE;
9085 : :
9086 : : /* Get enough bytes of EXPR to form the new encoding. */
9087 : 103804 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9088 : 103804 : buffer.quick_grow (buffer_bytes);
9089 : 103804 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9090 : 103804 : buffer_bits / expr_elt_bits)
9091 : : != (int) buffer_bytes)
9092 : : return NULL_TREE;
9093 : :
9094 : : /* Reencode the bytes as TYPE. */
9095 : 103804 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9096 : 207608 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9097 : 103804 : type_npatterns, nelts_per_pattern);
9098 : 103804 : }
9099 : :
9100 : : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9101 : : TYPE at compile-time. If we're unable to perform the conversion
9102 : : return NULL_TREE. */
9103 : :
9104 : : static tree
9105 : 11026180 : fold_view_convert_expr (tree type, tree expr)
9106 : : {
9107 : 11026180 : unsigned char buffer[128];
9108 : 11026180 : unsigned char *buf;
9109 : 11026180 : int len;
9110 : 11026180 : HOST_WIDE_INT l;
9111 : :
9112 : : /* Check that the host and target are sane. */
9113 : 11026180 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9114 : : return NULL_TREE;
9115 : :
9116 : 11026180 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9117 : 109890 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9118 : : return res;
9119 : :
9120 : 10922395 : l = int_size_in_bytes (type);
9121 : 10922395 : if (l > (int) sizeof (buffer)
9122 : 10922395 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9123 : : {
9124 : 0 : buf = XALLOCAVEC (unsigned char, l);
9125 : 0 : len = l;
9126 : : }
9127 : : else
9128 : : {
9129 : : buf = buffer;
9130 : : len = sizeof (buffer);
9131 : : }
9132 : 10922395 : len = native_encode_expr (expr, buf, len);
9133 : 10922395 : if (len == 0)
9134 : : return NULL_TREE;
9135 : :
9136 : 1669517 : return native_interpret_expr (type, buf, len);
9137 : : }
9138 : :
9139 : : /* Build an expression for the address of T. Folds away INDIRECT_REF
9140 : : to avoid confusing the gimplify process. */
9141 : :
9142 : : tree
9143 : 410363085 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9144 : : {
9145 : : /* The size of the object is not relevant when talking about its address. */
9146 : 410363085 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9147 : 0 : t = TREE_OPERAND (t, 0);
9148 : :
9149 : 410363085 : if (INDIRECT_REF_P (t))
9150 : : {
9151 : 47302778 : t = TREE_OPERAND (t, 0);
9152 : :
9153 : 47302778 : if (TREE_TYPE (t) != ptrtype)
9154 : 30176661 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9155 : : }
9156 : 363060307 : else if (TREE_CODE (t) == MEM_REF
9157 : 363060307 : && integer_zerop (TREE_OPERAND (t, 1)))
9158 : : {
9159 : 1645188 : t = TREE_OPERAND (t, 0);
9160 : :
9161 : 1645188 : if (TREE_TYPE (t) != ptrtype)
9162 : 1068473 : t = fold_convert_loc (loc, ptrtype, t);
9163 : : }
9164 : 361415119 : else if (TREE_CODE (t) == MEM_REF
9165 : 361415119 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9166 : 658 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9167 : : TREE_OPERAND (t, 0),
9168 : : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9169 : 361414461 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9170 : : {
9171 : 24866479 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9172 : :
9173 : 24866479 : if (TREE_TYPE (t) != ptrtype)
9174 : 13621 : t = fold_convert_loc (loc, ptrtype, t);
9175 : : }
9176 : : else
9177 : 336547982 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9178 : :
9179 : : return t;
9180 : : }
9181 : :
9182 : : /* Build an expression for the address of T. */
9183 : :
9184 : : tree
9185 : 380771914 : build_fold_addr_expr_loc (location_t loc, tree t)
9186 : : {
9187 : 380771914 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9188 : :
9189 : 380771914 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9190 : : }
9191 : :
9192 : : /* Fold a unary expression of code CODE and type TYPE with operand
9193 : : OP0. Return the folded expression if folding is successful.
9194 : : Otherwise, return NULL_TREE. */
9195 : :
9196 : : tree
9197 : 1589439673 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9198 : : {
9199 : 1589439673 : tree tem;
9200 : 1589439673 : tree arg0;
9201 : 1589439673 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9202 : :
9203 : 1589439673 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9204 : : && TREE_CODE_LENGTH (code) == 1);
9205 : :
9206 : 1589439673 : arg0 = op0;
9207 : 1589439673 : if (arg0)
9208 : : {
9209 : 1589426532 : if (CONVERT_EXPR_CODE_P (code)
9210 : : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9211 : : {
9212 : : /* Don't use STRIP_NOPS, because signedness of argument type
9213 : : matters. */
9214 : 781234438 : STRIP_SIGN_NOPS (arg0);
9215 : : }
9216 : : else
9217 : : {
9218 : : /* Strip any conversions that don't change the mode. This
9219 : : is safe for every expression, except for a comparison
9220 : : expression because its signedness is derived from its
9221 : : operands.
9222 : :
9223 : : Note that this is done as an internal manipulation within
9224 : : the constant folder, in order to find the simplest
9225 : : representation of the arguments so that their form can be
9226 : : studied. In any cases, the appropriate type conversions
9227 : : should be put back in the tree that will get out of the
9228 : : constant folder. */
9229 : 808192094 : STRIP_NOPS (arg0);
9230 : : }
9231 : :
9232 : 1589426532 : if (CONSTANT_CLASS_P (arg0))
9233 : : {
9234 : 227280880 : tree tem = const_unop (code, type, arg0);
9235 : 227280880 : if (tem)
9236 : : {
9237 : 192520576 : if (TREE_TYPE (tem) != type)
9238 : 66223 : tem = fold_convert_loc (loc, type, tem);
9239 : 192520576 : return tem;
9240 : : }
9241 : : }
9242 : : }
9243 : :
9244 : 1396919097 : tem = generic_simplify (loc, code, type, op0);
9245 : 1396919097 : if (tem)
9246 : : return tem;
9247 : :
9248 : 1059118010 : if (TREE_CODE_CLASS (code) == tcc_unary)
9249 : : {
9250 : 533880263 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9251 : 1014927 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9252 : : fold_build1_loc (loc, code, type,
9253 : 1014927 : fold_convert_loc (loc, TREE_TYPE (op0),
9254 : 2029854 : TREE_OPERAND (arg0, 1))));
9255 : 532865336 : else if (TREE_CODE (arg0) == COND_EXPR)
9256 : : {
9257 : 406091 : tree arg01 = TREE_OPERAND (arg0, 1);
9258 : 406091 : tree arg02 = TREE_OPERAND (arg0, 2);
9259 : 406091 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9260 : 401909 : arg01 = fold_build1_loc (loc, code, type,
9261 : : fold_convert_loc (loc,
9262 : 401909 : TREE_TYPE (op0), arg01));
9263 : 406091 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9264 : 402874 : arg02 = fold_build1_loc (loc, code, type,
9265 : : fold_convert_loc (loc,
9266 : 402874 : TREE_TYPE (op0), arg02));
9267 : 406091 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9268 : : arg01, arg02);
9269 : :
9270 : : /* If this was a conversion, and all we did was to move into
9271 : : inside the COND_EXPR, bring it back out. But leave it if
9272 : : it is a conversion from integer to integer and the
9273 : : result precision is no wider than a word since such a
9274 : : conversion is cheap and may be optimized away by combine,
9275 : : while it couldn't if it were outside the COND_EXPR. Then return
9276 : : so we don't get into an infinite recursion loop taking the
9277 : : conversion out and then back in. */
9278 : :
9279 : 406091 : if ((CONVERT_EXPR_CODE_P (code)
9280 : 10008 : || code == NON_LVALUE_EXPR)
9281 : 396102 : && TREE_CODE (tem) == COND_EXPR
9282 : 382410 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9283 : 353081 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9284 : 167354 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9285 : 167142 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9286 : 167142 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9287 : 167142 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9288 : 578499 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9289 : 6092 : && (INTEGRAL_TYPE_P
9290 : : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9291 : 6052 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9292 : 6027 : || flag_syntax_only))
9293 : 160329 : tem = build1_loc (loc, code, type,
9294 : : build3 (COND_EXPR,
9295 : 160329 : TREE_TYPE (TREE_OPERAND
9296 : : (TREE_OPERAND (tem, 1), 0)),
9297 : 160329 : TREE_OPERAND (tem, 0),
9298 : 160329 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9299 : 160329 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9300 : : 0)));
9301 : 406091 : return tem;
9302 : : }
9303 : : }
9304 : :
9305 : 1057696992 : switch (code)
9306 : : {
9307 : 38430100 : case NON_LVALUE_EXPR:
9308 : 38430100 : if (!maybe_lvalue_p (op0))
9309 : 28557635 : return fold_convert_loc (loc, type, op0);
9310 : : return NULL_TREE;
9311 : :
9312 : 485139607 : CASE_CONVERT:
9313 : 485139607 : case FLOAT_EXPR:
9314 : 485139607 : case FIX_TRUNC_EXPR:
9315 : 485139607 : if (COMPARISON_CLASS_P (op0))
9316 : : {
9317 : : /* If we have (type) (a CMP b) and type is an integral type, return
9318 : : new expression involving the new type. Canonicalize
9319 : : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9320 : : non-integral type.
9321 : : Do not fold the result as that would not simplify further, also
9322 : : folding again results in recursions. */
9323 : 373677 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9324 : 76859 : return build2_loc (loc, TREE_CODE (op0), type,
9325 : 76859 : TREE_OPERAND (op0, 0),
9326 : 153718 : TREE_OPERAND (op0, 1));
9327 : 296818 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9328 : 5952 : && TREE_CODE (type) != VECTOR_TYPE)
9329 : 5952 : return build3_loc (loc, COND_EXPR, type, op0,
9330 : : constant_boolean_node (true, type),
9331 : 5952 : constant_boolean_node (false, type));
9332 : : }
9333 : :
9334 : : /* Handle (T *)&A.B.C for A being of type T and B and C
9335 : : living at offset zero. This occurs frequently in
9336 : : C++ upcasting and then accessing the base. */
9337 : 485056796 : if (TREE_CODE (op0) == ADDR_EXPR
9338 : 105345980 : && POINTER_TYPE_P (type)
9339 : 583860097 : && handled_component_p (TREE_OPERAND (op0, 0)))
9340 : : {
9341 : 17564015 : poly_int64 bitsize, bitpos;
9342 : 17564015 : tree offset;
9343 : 17564015 : machine_mode mode;
9344 : 17564015 : int unsignedp, reversep, volatilep;
9345 : 17564015 : tree base
9346 : 17564015 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9347 : : &offset, &mode, &unsignedp, &reversep,
9348 : : &volatilep);
9349 : : /* If the reference was to a (constant) zero offset, we can use
9350 : : the address of the base if it has the same base type
9351 : : as the result type and the pointer type is unqualified. */
9352 : 17564015 : if (!offset
9353 : 17455029 : && known_eq (bitpos, 0)
9354 : 11992417 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9355 : 11992417 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9356 : 17574190 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9357 : 9974 : return fold_convert_loc (loc, type,
9358 : 9974 : build_fold_addr_expr_loc (loc, base));
9359 : : }
9360 : :
9361 : 485046822 : if (TREE_CODE (op0) == MODIFY_EXPR
9362 : 256927 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9363 : : /* Detect assigning a bitfield. */
9364 : 485048821 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9365 : 109 : && DECL_BIT_FIELD
9366 : : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9367 : : {
9368 : : /* Don't leave an assignment inside a conversion
9369 : : unless assigning a bitfield. */
9370 : 1951 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9371 : : /* First do the assignment, then return converted constant. */
9372 : 1951 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9373 : 1951 : suppress_warning (tem /* What warning? */);
9374 : 1951 : TREE_USED (tem) = 1;
9375 : 1951 : return tem;
9376 : : }
9377 : :
9378 : : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9379 : : constants (if x has signed type, the sign bit cannot be set
9380 : : in c). This folds extension into the BIT_AND_EXPR.
9381 : : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9382 : : very likely don't have maximal range for their precision and this
9383 : : transformation effectively doesn't preserve non-maximal ranges. */
9384 : 485044871 : if (TREE_CODE (type) == INTEGER_TYPE
9385 : 218007080 : && TREE_CODE (op0) == BIT_AND_EXPR
9386 : 485567558 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9387 : : {
9388 : 270795 : tree and_expr = op0;
9389 : 270795 : tree and0 = TREE_OPERAND (and_expr, 0);
9390 : 270795 : tree and1 = TREE_OPERAND (and_expr, 1);
9391 : 270795 : int change = 0;
9392 : :
9393 : 270795 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9394 : 270795 : || (TYPE_PRECISION (type)
9395 : 128720 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9396 : : change = 1;
9397 : 76801 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9398 : : <= HOST_BITS_PER_WIDE_INT
9399 : 76801 : && tree_fits_uhwi_p (and1))
9400 : : {
9401 : 75699 : unsigned HOST_WIDE_INT cst;
9402 : :
9403 : 75699 : cst = tree_to_uhwi (and1);
9404 : 151398 : cst &= HOST_WIDE_INT_M1U
9405 : 75699 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9406 : 75699 : change = (cst == 0);
9407 : 75699 : if (change
9408 : 75699 : && !flag_syntax_only
9409 : 148563 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9410 : : == ZERO_EXTEND))
9411 : : {
9412 : : tree uns = unsigned_type_for (TREE_TYPE (and0));
9413 : : and0 = fold_convert_loc (loc, uns, and0);
9414 : : and1 = fold_convert_loc (loc, uns, and1);
9415 : : }
9416 : : }
9417 : 75699 : if (change)
9418 : : {
9419 : 269693 : tree and1_type = TREE_TYPE (and1);
9420 : 269693 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9421 : : TYPE_PRECISION (type));
9422 : 269693 : tem = force_fit_type (type,
9423 : 269693 : wide_int::from (wi::to_wide (and1), prec,
9424 : 269693 : TYPE_SIGN (and1_type)),
9425 : 269693 : 0, TREE_OVERFLOW (and1));
9426 : 269693 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9427 : 269693 : fold_convert_loc (loc, type, and0), tem);
9428 : : }
9429 : : }
9430 : :
9431 : : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9432 : : cast (T1)X will fold away. We assume that this happens when X itself
9433 : : is a cast. */
9434 : 484775178 : if (POINTER_TYPE_P (type)
9435 : 231547273 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9436 : 489056079 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9437 : : {
9438 : 2058794 : tree arg00 = TREE_OPERAND (arg0, 0);
9439 : 2058794 : tree arg01 = TREE_OPERAND (arg0, 1);
9440 : :
9441 : : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9442 : : when the pointed type needs higher alignment than
9443 : : the p+ first operand's pointed type. */
9444 : 2058794 : if (!in_gimple_form
9445 : 2045666 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9446 : 2060008 : && (min_align_of_type (TREE_TYPE (type))
9447 : 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9448 : : return NULL_TREE;
9449 : :
9450 : : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9451 : : when type is a reference type and arg00's type is not,
9452 : : because arg00 could be validly nullptr and if arg01 doesn't return,
9453 : : we don't want false positive binding of reference to nullptr. */
9454 : 2058727 : if (TREE_CODE (type) == REFERENCE_TYPE
9455 : 1496231 : && !in_gimple_form
9456 : 1496214 : && sanitize_flags_p (SANITIZE_NULL)
9457 : 2059158 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9458 : : return NULL_TREE;
9459 : :
9460 : 2058296 : arg00 = fold_convert_loc (loc, type, arg00);
9461 : 2058296 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9462 : : }
9463 : :
9464 : : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9465 : : of the same precision, and X is an integer type not narrower than
9466 : : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9467 : 482716384 : if (INTEGRAL_TYPE_P (type)
9468 : 223555146 : && TREE_CODE (op0) == BIT_NOT_EXPR
9469 : 516216 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9470 : 516216 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9471 : 483035768 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9472 : : {
9473 : 317717 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9474 : 385662 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9475 : 385660 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9476 : 257509 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9477 : 257509 : fold_convert_loc (loc, type, tem));
9478 : : }
9479 : :
9480 : : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9481 : : type of X and Y (integer types only). */
9482 : 482458875 : if (INTEGRAL_TYPE_P (type)
9483 : 223297637 : && TREE_CODE (op0) == MULT_EXPR
9484 : 7717644 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9485 : 7696592 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9486 : 482518895 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9487 : 13474 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9488 : : {
9489 : : /* Be careful not to introduce new overflows. */
9490 : 59980 : tree mult_type;
9491 : 59980 : if (TYPE_OVERFLOW_WRAPS (type))
9492 : : mult_type = type;
9493 : : else
9494 : 1967 : mult_type = unsigned_type_for (type);
9495 : :
9496 : 59980 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9497 : : {
9498 : 119960 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9499 : : fold_convert_loc (loc, mult_type,
9500 : 59980 : TREE_OPERAND (op0, 0)),
9501 : : fold_convert_loc (loc, mult_type,
9502 : 59980 : TREE_OPERAND (op0, 1)));
9503 : 59980 : return fold_convert_loc (loc, type, tem);
9504 : : }
9505 : : }
9506 : :
9507 : : return NULL_TREE;
9508 : :
9509 : 224347314 : case VIEW_CONVERT_EXPR:
9510 : 224347314 : if (TREE_CODE (op0) == MEM_REF)
9511 : : {
9512 : 1 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9513 : 1 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9514 : 1 : tem = fold_build2_loc (loc, MEM_REF, type,
9515 : 1 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9516 : 1 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9517 : 1 : return tem;
9518 : : }
9519 : :
9520 : : return NULL_TREE;
9521 : :
9522 : 3605745 : case NEGATE_EXPR:
9523 : 3605745 : tem = fold_negate_expr (loc, arg0);
9524 : 3605745 : if (tem)
9525 : 1549 : return fold_convert_loc (loc, type, tem);
9526 : : return NULL_TREE;
9527 : :
9528 : 2647201 : case ABS_EXPR:
9529 : : /* Convert fabs((double)float) into (double)fabsf(float). */
9530 : 2647201 : if (TREE_CODE (arg0) == NOP_EXPR
9531 : 21979 : && TREE_CODE (type) == REAL_TYPE)
9532 : : {
9533 : 21943 : tree targ0 = strip_float_extensions (arg0);
9534 : 21943 : if (targ0 != arg0)
9535 : 21739 : return fold_convert_loc (loc, type,
9536 : : fold_build1_loc (loc, ABS_EXPR,
9537 : 21739 : TREE_TYPE (targ0),
9538 : 21739 : targ0));
9539 : : }
9540 : : return NULL_TREE;
9541 : :
9542 : 2630889 : case BIT_NOT_EXPR:
9543 : : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9544 : 2630889 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9545 : 2632574 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9546 : : fold_convert_loc (loc, type,
9547 : 1685 : TREE_OPERAND (arg0, 0)))))
9548 : 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9549 : : fold_convert_loc (loc, type,
9550 : 28 : TREE_OPERAND (arg0, 1)));
9551 : 2630875 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9552 : 2632546 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9553 : : fold_convert_loc (loc, type,
9554 : 1671 : TREE_OPERAND (arg0, 1)))))
9555 : 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9556 : : fold_convert_loc (loc, type,
9557 : 46 : TREE_OPERAND (arg0, 0)), tem);
9558 : :
9559 : : return NULL_TREE;
9560 : :
9561 : 47070350 : case TRUTH_NOT_EXPR:
9562 : : /* Note that the operand of this must be an int
9563 : : and its values must be 0 or 1.
9564 : : ("true" is a fixed value perhaps depending on the language,
9565 : : but we don't handle values other than 1 correctly yet.) */
9566 : 47070350 : tem = fold_truth_not_expr (loc, arg0);
9567 : 47070350 : if (!tem)
9568 : : return NULL_TREE;
9569 : 33341715 : return fold_convert_loc (loc, type, tem);
9570 : :
9571 : 62689623 : case INDIRECT_REF:
9572 : : /* Fold *&X to X if X is an lvalue. */
9573 : 62689623 : if (TREE_CODE (op0) == ADDR_EXPR)
9574 : : {
9575 : 6321 : tree op00 = TREE_OPERAND (op0, 0);
9576 : 6321 : if ((VAR_P (op00)
9577 : : || TREE_CODE (op00) == PARM_DECL
9578 : : || TREE_CODE (op00) == RESULT_DECL)
9579 : 5135 : && !TREE_READONLY (op00))
9580 : : return op00;
9581 : : }
9582 : : return NULL_TREE;
9583 : :
9584 : : default:
9585 : : return NULL_TREE;
9586 : : } /* switch (code) */
9587 : : }
9588 : :
9589 : :
9590 : : /* If the operation was a conversion do _not_ mark a resulting constant
9591 : : with TREE_OVERFLOW if the original constant was not. These conversions
9592 : : have implementation defined behavior and retaining the TREE_OVERFLOW
9593 : : flag here would confuse later passes such as VRP. */
9594 : : tree
9595 : 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9596 : : tree type, tree op0)
9597 : : {
9598 : 0 : tree res = fold_unary_loc (loc, code, type, op0);
9599 : 0 : if (res
9600 : 0 : && TREE_CODE (res) == INTEGER_CST
9601 : 0 : && TREE_CODE (op0) == INTEGER_CST
9602 : 0 : && CONVERT_EXPR_CODE_P (code))
9603 : 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9604 : :
9605 : 0 : return res;
9606 : : }
9607 : :
9608 : : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9609 : : operands OP0 and OP1. LOC is the location of the resulting expression.
9610 : : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9611 : : Return the folded expression if folding is successful. Otherwise,
9612 : : return NULL_TREE. */
9613 : : static tree
9614 : 24174345 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9615 : : tree arg0, tree arg1, tree op0, tree op1)
9616 : : {
9617 : 24174345 : tree tem;
9618 : :
9619 : : /* We only do these simplifications if we are optimizing. */
9620 : 24174345 : if (!optimize)
9621 : : return NULL_TREE;
9622 : :
9623 : : /* Check for things like (A || B) && (A || C). We can convert this
9624 : : to A || (B && C). Note that either operator can be any of the four
9625 : : truth and/or operations and the transformation will still be
9626 : : valid. Also note that we only care about order for the
9627 : : ANDIF and ORIF operators. If B contains side effects, this
9628 : : might change the truth-value of A. */
9629 : 23903915 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9630 : 5277060 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9631 : : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9632 : : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9633 : 5277060 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9634 : 23932065 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9635 : : {
9636 : 27632 : tree a00 = TREE_OPERAND (arg0, 0);
9637 : 27632 : tree a01 = TREE_OPERAND (arg0, 1);
9638 : 27632 : tree a10 = TREE_OPERAND (arg1, 0);
9639 : 27632 : tree a11 = TREE_OPERAND (arg1, 1);
9640 : 55264 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9641 : 27632 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9642 : 27632 : && (code == TRUTH_AND_EXPR
9643 : 8983 : || code == TRUTH_OR_EXPR));
9644 : :
9645 : 27632 : if (operand_equal_p (a00, a10, 0))
9646 : 391 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9647 : 391 : fold_build2_loc (loc, code, type, a01, a11));
9648 : 27241 : else if (commutative && operand_equal_p (a00, a11, 0))
9649 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9650 : 0 : fold_build2_loc (loc, code, type, a01, a10));
9651 : 27241 : else if (commutative && operand_equal_p (a01, a10, 0))
9652 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9653 : 0 : fold_build2_loc (loc, code, type, a00, a11));
9654 : :
9655 : : /* This case if tricky because we must either have commutative
9656 : : operators or else A10 must not have side-effects. */
9657 : :
9658 : 27202 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9659 : 53915 : && operand_equal_p (a01, a11, 0))
9660 : 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9661 : : fold_build2_loc (loc, code, type, a00, a10),
9662 : 43 : a01);
9663 : : }
9664 : :
9665 : : /* See if we can build a range comparison. */
9666 : 23903481 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9667 : : return tem;
9668 : :
9669 : 22834491 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9670 : 22832507 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9671 : : {
9672 : 20176 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9673 : 20176 : if (tem)
9674 : 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9675 : : }
9676 : :
9677 : 22834478 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9678 : 22827090 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9679 : : {
9680 : 115546 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9681 : 115546 : if (tem)
9682 : 76 : return fold_build2_loc (loc, code, type, arg0, tem);
9683 : : }
9684 : :
9685 : : /* Check for the possibility of merging component references. If our
9686 : : lhs is another similar operation, try to merge its rhs with our
9687 : : rhs. Then try to merge our lhs and rhs. */
9688 : 22834402 : if (TREE_CODE (arg0) == code
9689 : 23548173 : && (tem = fold_truth_andor_1 (loc, code, type,
9690 : 713771 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9691 : 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9692 : :
9693 : 22834317 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9694 : : return tem;
9695 : :
9696 : 22794315 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9697 : 22794315 : if (param_logical_op_non_short_circuit != -1)
9698 : 7694 : logical_op_non_short_circuit
9699 : 7694 : = param_logical_op_non_short_circuit;
9700 : 22794315 : if (logical_op_non_short_circuit
9701 : 22790446 : && !sanitize_coverage_p ()
9702 : 22794315 : && (code == TRUTH_AND_EXPR
9703 : 22790443 : || code == TRUTH_ANDIF_EXPR
9704 : 10883561 : || code == TRUTH_OR_EXPR
9705 : 10883561 : || code == TRUTH_ORIF_EXPR))
9706 : : {
9707 : 22790443 : enum tree_code ncode, icode;
9708 : :
9709 : 22790443 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9710 : 22790443 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9711 : 11906882 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9712 : :
9713 : : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9714 : : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9715 : : We don't want to pack more than two leafs to a non-IF AND/OR
9716 : : expression.
9717 : : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9718 : : equal to IF-CODE, then we don't want to add right-hand operand.
9719 : : If the inner right-hand side of left-hand operand has
9720 : : side-effects, or isn't simple, then we can't add to it,
9721 : : as otherwise we might destroy if-sequence. */
9722 : 22790443 : if (TREE_CODE (arg0) == icode
9723 : 705311 : && simple_condition_p (arg1)
9724 : : /* Needed for sequence points to handle trappings, and
9725 : : side-effects. */
9726 : 22838268 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9727 : : {
9728 : 41053 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9729 : : arg1);
9730 : 41053 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9731 : 41053 : tem);
9732 : : }
9733 : : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9734 : : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9735 : 22749390 : else if (TREE_CODE (arg1) == icode
9736 : 2448 : && simple_condition_p (arg0)
9737 : : /* Needed for sequence points to handle trappings, and
9738 : : side-effects. */
9739 : 22749994 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9740 : : {
9741 : 21 : tem = fold_build2_loc (loc, ncode, type,
9742 : 21 : arg0, TREE_OPERAND (arg1, 0));
9743 : 21 : return fold_build2_loc (loc, icode, type, tem,
9744 : 42 : TREE_OPERAND (arg1, 1));
9745 : : }
9746 : : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9747 : : into (A OR B).
9748 : : For sequence point consistancy, we need to check for trapping,
9749 : : and side-effects. */
9750 : 4364843 : else if (code == icode && simple_condition_p (arg0)
9751 : 23436747 : && simple_condition_p (arg1))
9752 : 363814 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9753 : : }
9754 : :
9755 : : return NULL_TREE;
9756 : : }
9757 : :
9758 : : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9759 : : by changing CODE to reduce the magnitude of constants involved in
9760 : : ARG0 of the comparison.
9761 : : Returns a canonicalized comparison tree if a simplification was
9762 : : possible, otherwise returns NULL_TREE.
9763 : : Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9764 : : valid if signed overflow is undefined. */
9765 : :
9766 : : static tree
9767 : 163726984 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9768 : : tree arg0, tree arg1,
9769 : : bool *strict_overflow_p)
9770 : : {
9771 : 163726984 : enum tree_code code0 = TREE_CODE (arg0);
9772 : 163726984 : tree t, cst0 = NULL_TREE;
9773 : 163726984 : int sgn0;
9774 : :
9775 : : /* Match A +- CST code arg1. We can change this only if overflow
9776 : : is undefined. */
9777 : 163726984 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9778 : 124797351 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9779 : : /* In principle pointers also have undefined overflow behavior,
9780 : : but that causes problems elsewhere. */
9781 : 61359767 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9782 : 61359767 : && (code0 == MINUS_EXPR
9783 : 61359767 : || code0 == PLUS_EXPR)
9784 : 2574187 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9785 : : return NULL_TREE;
9786 : :
9787 : : /* Identify the constant in arg0 and its sign. */
9788 : 2120301 : cst0 = TREE_OPERAND (arg0, 1);
9789 : 2120301 : sgn0 = tree_int_cst_sgn (cst0);
9790 : :
9791 : : /* Overflowed constants and zero will cause problems. */
9792 : 2120301 : if (integer_zerop (cst0)
9793 : 2120301 : || TREE_OVERFLOW (cst0))
9794 : : return NULL_TREE;
9795 : :
9796 : : /* See if we can reduce the magnitude of the constant in
9797 : : arg0 by changing the comparison code. */
9798 : : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9799 : 2120301 : if (code == LT_EXPR
9800 : 1197684 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9801 : : code = LE_EXPR;
9802 : : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9803 : 1921830 : else if (code == GT_EXPR
9804 : 550493 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9805 : : code = GE_EXPR;
9806 : : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9807 : 1752133 : else if (code == LE_EXPR
9808 : 636509 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9809 : : code = LT_EXPR;
9810 : : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9811 : 1534781 : else if (code == GE_EXPR
9812 : 500614 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9813 : : code = GT_EXPR;
9814 : : else
9815 : : return NULL_TREE;
9816 : 807062 : *strict_overflow_p = true;
9817 : :
9818 : : /* Now build the constant reduced in magnitude. But not if that
9819 : : would produce one outside of its types range. */
9820 : 1614124 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9821 : 1614124 : && ((sgn0 == 1
9822 : 386911 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9823 : 386911 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9824 : 807062 : || (sgn0 == -1
9825 : 420151 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9826 : 420151 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9827 : 0 : return NULL_TREE;
9828 : :
9829 : 1193973 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9830 : 807062 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9831 : 807062 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9832 : 807062 : t = fold_convert (TREE_TYPE (arg1), t);
9833 : :
9834 : 807062 : return fold_build2_loc (loc, code, type, t, arg1);
9835 : : }
9836 : :
9837 : : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9838 : : overflow further. Try to decrease the magnitude of constants involved
9839 : : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9840 : : and put sole constants at the second argument position.
9841 : : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9842 : :
9843 : : static tree
9844 : 82246599 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9845 : : tree arg0, tree arg1)
9846 : : {
9847 : 82246599 : tree t;
9848 : 82246599 : bool strict_overflow_p;
9849 : 82246599 : const char * const warnmsg = G_("assuming signed overflow does not occur "
9850 : : "when reducing constant in comparison");
9851 : :
9852 : : /* Try canonicalization by simplifying arg0. */
9853 : 82246599 : strict_overflow_p = false;
9854 : 82246599 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9855 : : &strict_overflow_p);
9856 : 82246599 : if (t)
9857 : : {
9858 : 766214 : if (strict_overflow_p)
9859 : 766214 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9860 : 766214 : return t;
9861 : : }
9862 : :
9863 : : /* Try canonicalization by simplifying arg1 using the swapped
9864 : : comparison. */
9865 : 81480385 : code = swap_tree_comparison (code);
9866 : 81480385 : strict_overflow_p = false;
9867 : 81480385 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9868 : : &strict_overflow_p);
9869 : 81480385 : if (t && strict_overflow_p)
9870 : 40848 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9871 : : return t;
9872 : : }
9873 : :
9874 : : /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9875 : : space. This is used to avoid issuing overflow warnings for
9876 : : expressions like &p->x which cannot wrap. */
9877 : :
9878 : : static bool
9879 : 17953 : pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9880 : : {
9881 : 17953 : if (!POINTER_TYPE_P (TREE_TYPE (base)))
9882 : : return true;
9883 : :
9884 : 10629 : if (maybe_lt (bitpos, 0))
9885 : : return true;
9886 : :
9887 : : poly_wide_int wi_offset;
9888 : 9593 : int precision = TYPE_PRECISION (TREE_TYPE (base));
9889 : 9593 : if (offset == NULL_TREE)
9890 : 4711 : wi_offset = wi::zero (precision);
9891 : 4882 : else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
9892 : : return true;
9893 : : else
9894 : 0 : wi_offset = wi::to_poly_wide (offset);
9895 : :
9896 : 4711 : wi::overflow_type overflow;
9897 : 4711 : poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9898 : 4711 : precision);
9899 : 4711 : poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9900 : 4711 : if (overflow)
9901 : : return true;
9902 : :
9903 : 4711 : poly_uint64 total_hwi, size;
9904 : 4711 : if (!total.to_uhwi (&total_hwi)
9905 : 4711 : || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9906 : : &size)
9907 : 9328 : || known_eq (size, 0U))
9908 : 94 : return true;
9909 : :
9910 : 4617 : if (known_le (total_hwi, size))
9911 : : return false;
9912 : :
9913 : : /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9914 : : array. */
9915 : 1137 : if (TREE_CODE (base) == ADDR_EXPR
9916 : 0 : && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
9917 : : &size)
9918 : 0 : && maybe_ne (size, 0U)
9919 : 1137 : && known_le (total_hwi, size))
9920 : : return false;
9921 : :
9922 : : return true;
9923 : 9593 : }
9924 : :
9925 : : /* Return a positive integer when the symbol DECL is known to have
9926 : : a nonzero address, zero when it's known not to (e.g., it's a weak
9927 : : symbol), and a negative integer when the symbol is not yet in the
9928 : : symbol table and so whether or not its address is zero is unknown.
9929 : : For function local objects always return positive integer. */
9930 : : static int
9931 : 10992943 : maybe_nonzero_address (tree decl)
9932 : : {
9933 : 10992943 : if (!DECL_P (decl))
9934 : : return -1;
9935 : :
9936 : : /* Normally, don't do anything for variables and functions before symtab is
9937 : : built; it is quite possible that DECL will be declared weak later.
9938 : : But if folding_initializer, we need a constant answer now, so create
9939 : : the symtab entry and prevent later weak declaration. */
9940 : 8813511 : if (decl_in_symtab_p (decl))
9941 : : {
9942 : 3790034 : if (struct symtab_node *symbol
9943 : 3790034 : = (folding_initializer
9944 : 3790034 : ? symtab_node::get_create (decl)
9945 : 3773535 : : symtab_node::get (decl)))
9946 : 3771254 : return symbol->nonzero_address ();
9947 : : }
9948 : 5023477 : else if (folding_cxx_constexpr)
9949 : : /* Anything that doesn't go in the symtab has non-zero address. */
9950 : : return 1;
9951 : :
9952 : : /* Function local objects are never NULL. */
9953 : 5036808 : if (DECL_CONTEXT (decl)
9954 : 5021014 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9955 : 10054510 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9956 : : return 1;
9957 : :
9958 : : return -1;
9959 : : }
9960 : :
9961 : : /* Subroutine of fold_binary. This routine performs all of the
9962 : : transformations that are common to the equality/inequality
9963 : : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9964 : : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9965 : : fold_binary should call fold_binary. Fold a comparison with
9966 : : tree code CODE and type TYPE with operands OP0 and OP1. Return
9967 : : the folded comparison or NULL_TREE. */
9968 : :
9969 : : static tree
9970 : 82317266 : fold_comparison (location_t loc, enum tree_code code, tree type,
9971 : : tree op0, tree op1)
9972 : : {
9973 : 82317266 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9974 : 82317266 : tree arg0, arg1, tem;
9975 : :
9976 : 82317266 : arg0 = op0;
9977 : 82317266 : arg1 = op1;
9978 : :
9979 : 82317266 : STRIP_SIGN_NOPS (arg0);
9980 : 82317266 : STRIP_SIGN_NOPS (arg1);
9981 : :
9982 : : /* For comparisons of pointers we can decompose it to a compile time
9983 : : comparison of the base objects and the offsets into the object.
9984 : : This requires at least one operand being an ADDR_EXPR or a
9985 : : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9986 : 152757119 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9987 : 82546196 : && (TREE_CODE (arg0) == ADDR_EXPR
9988 : 12059196 : || TREE_CODE (arg1) == ADDR_EXPR
9989 : 10712395 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9990 : 10053271 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9991 : : {
9992 : 2061165 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9993 : 2061165 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9994 : 2061165 : machine_mode mode;
9995 : 2061165 : int volatilep, reversep, unsignedp;
9996 : 2061165 : bool indirect_base0 = false, indirect_base1 = false;
9997 : :
9998 : : /* Get base and offset for the access. Strip ADDR_EXPR for
9999 : : get_inner_reference, but put it back by stripping INDIRECT_REF
10000 : : off the base object if possible. indirect_baseN will be true
10001 : : if baseN is not an address but refers to the object itself. */
10002 : 2061165 : base0 = arg0;
10003 : 2061165 : if (TREE_CODE (arg0) == ADDR_EXPR)
10004 : : {
10005 : 47147 : base0
10006 : 47147 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10007 : : &bitsize, &bitpos0, &offset0, &mode,
10008 : : &unsignedp, &reversep, &volatilep);
10009 : 47147 : if (INDIRECT_REF_P (base0))
10010 : 1731 : base0 = TREE_OPERAND (base0, 0);
10011 : : else
10012 : : indirect_base0 = true;
10013 : : }
10014 : 2014018 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10015 : : {
10016 : 725761 : base0 = TREE_OPERAND (arg0, 0);
10017 : 725761 : STRIP_SIGN_NOPS (base0);
10018 : 725761 : if (TREE_CODE (base0) == ADDR_EXPR)
10019 : : {
10020 : 41124 : base0
10021 : 41124 : = get_inner_reference (TREE_OPERAND (base0, 0),
10022 : : &bitsize, &bitpos0, &offset0, &mode,
10023 : : &unsignedp, &reversep, &volatilep);
10024 : 41124 : if (INDIRECT_REF_P (base0))
10025 : 14 : base0 = TREE_OPERAND (base0, 0);
10026 : : else
10027 : : indirect_base0 = true;
10028 : : }
10029 : 725761 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10030 : 725761 : offset0 = TREE_OPERAND (arg0, 1);
10031 : : else
10032 : 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10033 : : TREE_OPERAND (arg0, 1));
10034 : 725761 : if (poly_int_tree_p (offset0))
10035 : : {
10036 : 615536 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10037 : 615536 : TYPE_PRECISION (sizetype));
10038 : 615536 : tem <<= LOG2_BITS_PER_UNIT;
10039 : 615536 : tem += bitpos0;
10040 : 615536 : if (tem.to_shwi (&bitpos0))
10041 : 615533 : offset0 = NULL_TREE;
10042 : : }
10043 : : }
10044 : :
10045 : 2061165 : base1 = arg1;
10046 : 2061165 : if (TREE_CODE (arg1) == ADDR_EXPR)
10047 : : {
10048 : 1372219 : base1
10049 : 1372219 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10050 : : &bitsize, &bitpos1, &offset1, &mode,
10051 : : &unsignedp, &reversep, &volatilep);
10052 : 1372219 : if (INDIRECT_REF_P (base1))
10053 : 67752 : base1 = TREE_OPERAND (base1, 0);
10054 : : else
10055 : : indirect_base1 = true;
10056 : : }
10057 : 688946 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10058 : : {
10059 : 73377 : base1 = TREE_OPERAND (arg1, 0);
10060 : 73377 : STRIP_SIGN_NOPS (base1);
10061 : 73377 : if (TREE_CODE (base1) == ADDR_EXPR)
10062 : : {
10063 : 9610 : base1
10064 : 9610 : = get_inner_reference (TREE_OPERAND (base1, 0),
10065 : : &bitsize, &bitpos1, &offset1, &mode,
10066 : : &unsignedp, &reversep, &volatilep);
10067 : 9610 : if (INDIRECT_REF_P (base1))
10068 : 0 : base1 = TREE_OPERAND (base1, 0);
10069 : : else
10070 : : indirect_base1 = true;
10071 : : }
10072 : 73377 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10073 : 73361 : offset1 = TREE_OPERAND (arg1, 1);
10074 : : else
10075 : 16 : offset1 = size_binop (PLUS_EXPR, offset1,
10076 : : TREE_OPERAND (arg1, 1));
10077 : 73377 : if (poly_int_tree_p (offset1))
10078 : : {
10079 : 62164 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10080 : 62164 : TYPE_PRECISION (sizetype));
10081 : 62164 : tem <<= LOG2_BITS_PER_UNIT;
10082 : 62164 : tem += bitpos1;
10083 : 62164 : if (tem.to_shwi (&bitpos1))
10084 : 62164 : offset1 = NULL_TREE;
10085 : : }
10086 : : }
10087 : :
10088 : : /* If we have equivalent bases we might be able to simplify. */
10089 : 2061165 : if (indirect_base0 == indirect_base1
10090 : 2757538 : && operand_equal_p (base0, base1,
10091 : : indirect_base0 ? OEP_ADDRESS_OF : 0))
10092 : : {
10093 : : /* We can fold this expression to a constant if the non-constant
10094 : : offset parts are equal. */
10095 : 21274 : if ((offset0 == offset1
10096 : 6898 : || (offset0 && offset1
10097 : 2521 : && operand_equal_p (offset0, offset1, 0)))
10098 : 21274 : && (equality_code
10099 : 10884 : || (indirect_base0
10100 : 7081 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10101 : 3803 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10102 : : {
10103 : 14338 : if (!equality_code
10104 : 10846 : && maybe_ne (bitpos0, bitpos1)
10105 : 25163 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10106 : 1982 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10107 : 9327 : fold_overflow_warning (("assuming pointer wraparound does not "
10108 : : "occur when comparing P +- C1 with "
10109 : : "P +- C2"),
10110 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
10111 : :
10112 : 14338 : switch (code)
10113 : : {
10114 : 56 : case EQ_EXPR:
10115 : 56 : if (known_eq (bitpos0, bitpos1))
10116 : 49037 : return constant_boolean_node (true, type);
10117 : 21 : if (known_ne (bitpos0, bitpos1))
10118 : 21 : return constant_boolean_node (false, type);
10119 : : break;
10120 : 3436 : case NE_EXPR:
10121 : 3436 : if (known_ne (bitpos0, bitpos1))
10122 : 3431 : return constant_boolean_node (true, type);
10123 : 5 : if (known_eq (bitpos0, bitpos1))
10124 : 5 : return constant_boolean_node (false, type);
10125 : : break;
10126 : 2475 : case LT_EXPR:
10127 : 2475 : if (known_lt (bitpos0, bitpos1))
10128 : 2327 : return constant_boolean_node (true, type);
10129 : 148 : if (known_ge (bitpos0, bitpos1))
10130 : 148 : return constant_boolean_node (false, type);
10131 : : break;
10132 : 2172 : case LE_EXPR:
10133 : 2172 : if (known_le (bitpos0, bitpos1))
10134 : 275 : return constant_boolean_node (true, type);
10135 : 1897 : if (known_gt (bitpos0, bitpos1))
10136 : 1897 : return constant_boolean_node (false, type);
10137 : : break;
10138 : 4417 : case GE_EXPR:
10139 : 4417 : if (known_ge (bitpos0, bitpos1))
10140 : 1947 : return constant_boolean_node (true, type);
10141 : 2470 : if (known_lt (bitpos0, bitpos1))
10142 : 2470 : return constant_boolean_node (false, type);
10143 : : break;
10144 : 1782 : case GT_EXPR:
10145 : 1782 : if (known_gt (bitpos0, bitpos1))
10146 : 1729 : return constant_boolean_node (true, type);
10147 : 53 : if (known_le (bitpos0, bitpos1))
10148 : 53 : return constant_boolean_node (false, type);
10149 : : break;
10150 : : default:;
10151 : : }
10152 : : }
10153 : : /* We can simplify the comparison to a comparison of the variable
10154 : : offset parts if the constant offset parts are equal.
10155 : : Be careful to use signed sizetype here because otherwise we
10156 : : mess with array offsets in the wrong way. This is possible
10157 : : because pointer arithmetic is restricted to retain within an
10158 : : object and overflow on pointer differences is undefined as of
10159 : : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10160 : 6936 : else if (known_eq (bitpos0, bitpos1)
10161 : 6936 : && (equality_code
10162 : 5146 : || (indirect_base0
10163 : 264 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10164 : 4882 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10165 : : {
10166 : : /* By converting to signed sizetype we cover middle-end pointer
10167 : : arithmetic which operates on unsigned pointer types of size
10168 : : type size and ARRAY_REF offsets which are properly sign or
10169 : : zero extended from their type in case it is narrower than
10170 : : sizetype. */
10171 : 5260 : if (offset0 == NULL_TREE)
10172 : 0 : offset0 = build_int_cst (ssizetype, 0);
10173 : : else
10174 : 5260 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10175 : 5260 : if (offset1 == NULL_TREE)
10176 : 2771 : offset1 = build_int_cst (ssizetype, 0);
10177 : : else
10178 : 2489 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10179 : :
10180 : 5260 : if (!equality_code
10181 : 5260 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10182 : 0 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10183 : 5146 : fold_overflow_warning (("assuming pointer wraparound does not "
10184 : : "occur when comparing P +- C1 with "
10185 : : "P +- C2"),
10186 : : WARN_STRICT_OVERFLOW_COMPARISON);
10187 : :
10188 : 5260 : return fold_build2_loc (loc, code, type, offset0, offset1);
10189 : : }
10190 : : }
10191 : : /* For equal offsets we can simplify to a comparison of the
10192 : : base addresses. */
10193 : 2039891 : else if (known_eq (bitpos0, bitpos1)
10194 : 49360 : && (indirect_base0
10195 : 783238 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10196 : 11702 : && (indirect_base1
10197 : 130297 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10198 : 2204555 : && ((offset0 == offset1)
10199 : 5317 : || (offset0 && offset1
10200 : 5116 : && operand_equal_p (offset0, offset1, 0))))
10201 : : {
10202 : 29376 : if (indirect_base0)
10203 : 1138 : base0 = build_fold_addr_expr_loc (loc, base0);
10204 : 29376 : if (indirect_base1)
10205 : 3274 : base1 = build_fold_addr_expr_loc (loc, base1);
10206 : 29376 : return fold_build2_loc (loc, code, type, base0, base1);
10207 : : }
10208 : : /* Comparison between an ordinary (non-weak) symbol and a null
10209 : : pointer can be eliminated since such symbols must have a non
10210 : : null address. In C, relational expressions between pointers
10211 : : to objects and null pointers are undefined. The results
10212 : : below follow the C++ rules with the additional property that
10213 : : every object pointer compares greater than a null pointer.
10214 : : */
10215 : 2010515 : else if (((DECL_P (base0)
10216 : 204159 : && maybe_nonzero_address (base0) > 0
10217 : : /* Avoid folding references to struct members at offset 0 to
10218 : : prevent tests like '&ptr->firstmember == 0' from getting
10219 : : eliminated. When ptr is null, although the -> expression
10220 : : is strictly speaking invalid, GCC retains it as a matter
10221 : : of QoI. See PR c/44555. */
10222 : 191266 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10223 : 1983713 : || CONSTANT_CLASS_P (base0))
10224 : 31494 : && indirect_base0
10225 : : /* The caller guarantees that when one of the arguments is
10226 : : constant (i.e., null in this case) it is second. */
10227 : 2039425 : && integer_zerop (arg1))
10228 : : {
10229 : 63 : switch (code)
10230 : : {
10231 : 24 : case EQ_EXPR:
10232 : 24 : case LE_EXPR:
10233 : 24 : case LT_EXPR:
10234 : 24 : return constant_boolean_node (false, type);
10235 : 39 : case GE_EXPR:
10236 : 39 : case GT_EXPR:
10237 : 39 : case NE_EXPR:
10238 : 39 : return constant_boolean_node (true, type);
10239 : 0 : default:
10240 : 0 : gcc_unreachable ();
10241 : : }
10242 : : }
10243 : : }
10244 : :
10245 : : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10246 : : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10247 : : the resulting offset is smaller in absolute value than the
10248 : : original one and has the same sign. */
10249 : 161680656 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10250 : 125606832 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10251 : 31084623 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10252 : 2318602 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10253 : 1918170 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10254 : 1918170 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10255 : 145239456 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10256 : 165487 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10257 : : {
10258 : 165487 : tree const1 = TREE_OPERAND (arg0, 1);
10259 : 165487 : tree const2 = TREE_OPERAND (arg1, 1);
10260 : 165487 : tree variable1 = TREE_OPERAND (arg0, 0);
10261 : 165487 : tree variable2 = TREE_OPERAND (arg1, 0);
10262 : 165487 : tree cst;
10263 : 165487 : const char * const warnmsg = G_("assuming signed overflow does not "
10264 : : "occur when combining constants around "
10265 : : "a comparison");
10266 : :
10267 : : /* Put the constant on the side where it doesn't overflow and is
10268 : : of lower absolute value and of same sign than before. */
10269 : 165488 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10270 : : ? MINUS_EXPR : PLUS_EXPR,
10271 : : const2, const1);
10272 : 165487 : if (!TREE_OVERFLOW (cst)
10273 : 165471 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10274 : 187117 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10275 : : {
10276 : 5479 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10277 : 5479 : return fold_build2_loc (loc, code, type,
10278 : : variable1,
10279 : 5479 : fold_build2_loc (loc, TREE_CODE (arg1),
10280 : 5479 : TREE_TYPE (arg1),
10281 : 5479 : variable2, cst));
10282 : : }
10283 : :
10284 : 160009 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10285 : : ? MINUS_EXPR : PLUS_EXPR,
10286 : : const1, const2);
10287 : 160008 : if (!TREE_OVERFLOW (cst)
10288 : 159992 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10289 : 176159 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10290 : : {
10291 : 16151 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10292 : 16151 : return fold_build2_loc (loc, code, type,
10293 : 16151 : fold_build2_loc (loc, TREE_CODE (arg0),
10294 : 16151 : TREE_TYPE (arg0),
10295 : : variable1, cst),
10296 : 16151 : variable2);
10297 : : }
10298 : : }
10299 : :
10300 : 82246599 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10301 : 82246599 : if (tem)
10302 : : return tem;
10303 : :
10304 : : /* If we are comparing an expression that just has comparisons
10305 : : of two integer values, arithmetic expressions of those comparisons,
10306 : : and constants, we can simplify it. There are only three cases
10307 : : to check: the two values can either be equal, the first can be
10308 : : greater, or the second can be greater. Fold the expression for
10309 : : those three values. Since each value must be 0 or 1, we have
10310 : : eight possibilities, each of which corresponds to the constant 0
10311 : : or 1 or one of the six possible comparisons.
10312 : :
10313 : : This handles common cases like (a > b) == 0 but also handles
10314 : : expressions like ((x > y) - (y > x)) > 0, which supposedly
10315 : : occur in macroized code. */
10316 : :
10317 : 81439537 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10318 : : {
10319 : 49219538 : tree cval1 = 0, cval2 = 0;
10320 : :
10321 : 49219538 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10322 : : /* Don't handle degenerate cases here; they should already
10323 : : have been handled anyway. */
10324 : 581474 : && cval1 != 0 && cval2 != 0
10325 : 580303 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10326 : 580303 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10327 : 580297 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10328 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10329 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10330 : 49219596 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10331 : 58 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10332 : : {
10333 : 58 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10334 : 58 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10335 : :
10336 : : /* We can't just pass T to eval_subst in case cval1 or cval2
10337 : : was the same as ARG1. */
10338 : :
10339 : 58 : tree high_result
10340 : 58 : = fold_build2_loc (loc, code, type,
10341 : : eval_subst (loc, arg0, cval1, maxval,
10342 : : cval2, minval),
10343 : : arg1);
10344 : 58 : tree equal_result
10345 : 58 : = fold_build2_loc (loc, code, type,
10346 : : eval_subst (loc, arg0, cval1, maxval,
10347 : : cval2, maxval),
10348 : : arg1);
10349 : 58 : tree low_result
10350 : 58 : = fold_build2_loc (loc, code, type,
10351 : : eval_subst (loc, arg0, cval1, minval,
10352 : : cval2, maxval),
10353 : : arg1);
10354 : :
10355 : : /* All three of these results should be 0 or 1. Confirm they are.
10356 : : Then use those values to select the proper code to use. */
10357 : :
10358 : 58 : if (TREE_CODE (high_result) == INTEGER_CST
10359 : 49 : && TREE_CODE (equal_result) == INTEGER_CST
10360 : 39 : && TREE_CODE (low_result) == INTEGER_CST)
10361 : : {
10362 : : /* Make a 3-bit mask with the high-order bit being the
10363 : : value for `>', the next for '=', and the low for '<'. */
10364 : 39 : switch ((integer_onep (high_result) * 4)
10365 : 39 : + (integer_onep (equal_result) * 2)
10366 : 39 : + integer_onep (low_result))
10367 : : {
10368 : 21 : case 0:
10369 : : /* Always false. */
10370 : 39 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10371 : : case 1:
10372 : : code = LT_EXPR;
10373 : : break;
10374 : 2 : case 2:
10375 : 2 : code = EQ_EXPR;
10376 : 2 : break;
10377 : 0 : case 3:
10378 : 0 : code = LE_EXPR;
10379 : 0 : break;
10380 : 0 : case 4:
10381 : 0 : code = GT_EXPR;
10382 : 0 : break;
10383 : 1 : case 5:
10384 : 1 : code = NE_EXPR;
10385 : 1 : break;
10386 : 0 : case 6:
10387 : 0 : code = GE_EXPR;
10388 : 0 : break;
10389 : 15 : case 7:
10390 : : /* Always true. */
10391 : 15 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10392 : : }
10393 : :
10394 : 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10395 : : }
10396 : : }
10397 : : }
10398 : :
10399 : : return NULL_TREE;
10400 : : }
10401 : :
10402 : :
10403 : : /* Subroutine of fold_binary. Optimize complex multiplications of the
10404 : : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10405 : : argument EXPR represents the expression "z" of type TYPE. */
10406 : :
10407 : : static tree
10408 : 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10409 : : {
10410 : 2 : tree itype = TREE_TYPE (type);
10411 : 2 : tree rpart, ipart, tem;
10412 : :
10413 : 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10414 : : {
10415 : 0 : rpart = TREE_OPERAND (expr, 0);
10416 : 0 : ipart = TREE_OPERAND (expr, 1);
10417 : : }
10418 : 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10419 : : {
10420 : 0 : rpart = TREE_REALPART (expr);
10421 : 0 : ipart = TREE_IMAGPART (expr);
10422 : : }
10423 : : else
10424 : : {
10425 : 2 : expr = save_expr (expr);
10426 : 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10427 : 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10428 : : }
10429 : :
10430 : 2 : rpart = save_expr (rpart);
10431 : 2 : ipart = save_expr (ipart);
10432 : 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10433 : : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10434 : : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10435 : 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10436 : 2 : build_zero_cst (itype));
10437 : : }
10438 : :
10439 : :
10440 : : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10441 : : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10442 : : true if successful. */
10443 : :
10444 : : static bool
10445 : 11420 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10446 : : {
10447 : 11420 : unsigned HOST_WIDE_INT i, nunits;
10448 : :
10449 : 11420 : if (TREE_CODE (arg) == VECTOR_CST
10450 : 11420 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10451 : : {
10452 : 2456 : for (i = 0; i < nunits; ++i)
10453 : 1930 : elts[i] = VECTOR_CST_ELT (arg, i);
10454 : : }
10455 : 10894 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10456 : : {
10457 : : constructor_elt *elt;
10458 : :
10459 : 35594 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10460 : 29935 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10461 : 5235 : return false;
10462 : : else
10463 : 24700 : elts[i] = elt->value;
10464 : : }
10465 : : else
10466 : : return false;
10467 : 6959 : for (; i < nelts; i++)
10468 : 1548 : elts[i]
10469 : 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10470 : : return true;
10471 : : }
10472 : :
10473 : : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10474 : : mask for VLA vec_perm folding.
10475 : : REASON if specified, will contain the reason why SEL is not suitable.
10476 : : Used only for debugging and unit-testing. */
10477 : :
10478 : : static bool
10479 : 7238 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10480 : : const vec_perm_indices &sel,
10481 : : const char **reason = NULL)
10482 : : {
10483 : 7238 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10484 : 7238 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10485 : :
10486 : 14476 : if (!(pow2p_hwi (sel_npatterns)
10487 : 7238 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10488 : 7238 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10489 : : {
10490 : 0 : if (reason)
10491 : 0 : *reason = "npatterns is not power of 2";
10492 : 0 : return false;
10493 : : }
10494 : :
10495 : : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10496 : : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10497 : 7238 : poly_uint64 esel;
10498 : 7238 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10499 : : {
10500 : 0 : if (reason)
10501 : 0 : *reason = "sel.length is not multiple of sel_npatterns";
10502 : 0 : return false;
10503 : : }
10504 : :
10505 : 7238 : if (sel_nelts_per_pattern < 3)
10506 : : return true;
10507 : :
10508 : 4665 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10509 : : {
10510 : 3692 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10511 : 3692 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10512 : 3692 : HOST_WIDE_INT step;
10513 : 3692 : if (!poly_int64 (a2 - a1).is_constant (&step))
10514 : : {
10515 : : if (reason)
10516 : : *reason = "step is not constant";
10517 : 933 : return false;
10518 : : }
10519 : : // FIXME: Punt on step < 0 for now, revisit later.
10520 : 3692 : if (step < 0)
10521 : : return false;
10522 : 3628 : if (step == 0)
10523 : 0 : continue;
10524 : :
10525 : 3628 : if (!pow2p_hwi (step))
10526 : : {
10527 : 0 : if (reason)
10528 : 0 : *reason = "step is not power of 2";
10529 : 0 : return false;
10530 : : }
10531 : :
10532 : : /* Ensure that stepped sequence of the pattern selects elements
10533 : : only from the same input vector. */
10534 : 3628 : uint64_t q1, qe;
10535 : 3628 : poly_uint64 r1, re;
10536 : 3628 : poly_uint64 ae = a1 + (esel - 2) * step;
10537 : 3628 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10538 : :
10539 : 3628 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10540 : 3628 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10541 : : && q1 == qe))
10542 : : {
10543 : 331 : if (reason)
10544 : 0 : *reason = "crossed input vectors";
10545 : 331 : return false;
10546 : : }
10547 : :
10548 : : /* Ensure that the stepped sequence always selects from the same
10549 : : input pattern. */
10550 : 3297 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10551 : 3297 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10552 : :
10553 : 3297 : if (!multiple_p (step, arg_npatterns))
10554 : : {
10555 : 536 : if (reason)
10556 : 0 : *reason = "step is not multiple of npatterns";
10557 : 536 : return false;
10558 : : }
10559 : :
10560 : : /* If a1 chooses base element from arg, ensure that it's a natural
10561 : : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10562 : : to preserve arg's encoding. */
10563 : :
10564 : 2761 : if (maybe_lt (r1, arg_npatterns))
10565 : : {
10566 : 2 : unsigned HOST_WIDE_INT index;
10567 : 2 : if (!r1.is_constant (&index))
10568 : 2 : return false;
10569 : :
10570 : 2 : tree arg_elem0 = vector_cst_elt (arg, index);
10571 : 2 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10572 : 2 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10573 : :
10574 : 2 : tree step1, step2;
10575 : 2 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10576 : 2 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10577 : 4 : || !operand_equal_p (step1, step2, 0))
10578 : : {
10579 : 2 : if (reason)
10580 : 0 : *reason = "not a natural stepped sequence";
10581 : 2 : return false;
10582 : : }
10583 : : }
10584 : : }
10585 : :
10586 : : return true;
10587 : : }
10588 : :
10589 : : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10590 : : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10591 : : REASON has same purpose as described in
10592 : : valid_mask_for_fold_vec_perm_cst_p. */
10593 : :
10594 : : static tree
10595 : 7238 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10596 : : const char **reason = NULL)
10597 : : {
10598 : 7238 : unsigned res_npatterns, res_nelts_per_pattern;
10599 : 7238 : unsigned HOST_WIDE_INT res_nelts;
10600 : :
10601 : : /* First try to implement the fold in a VLA-friendly way.
10602 : :
10603 : : (1) If the selector is simply a duplication of N elements, the
10604 : : result is likewise a duplication of N elements.
10605 : :
10606 : : (2) If the selector is N elements followed by a duplication
10607 : : of N elements, the result is too.
10608 : :
10609 : : (3) If the selector is N elements followed by an interleaving
10610 : : of N linear series, the situation is more complex.
10611 : :
10612 : : valid_mask_for_fold_vec_perm_cst_p detects whether we
10613 : : can handle this case. If we can, then each of the N linear
10614 : : series either (a) selects the same element each time or
10615 : : (b) selects a linear series from one of the input patterns.
10616 : :
10617 : : If (b) holds for one of the linear series, the result
10618 : : will contain a linear series, and so the result will have
10619 : : the same shape as the selector. If (a) holds for all of
10620 : : the linear series, the result will be the same as (2) above.
10621 : :
10622 : : (b) can only hold if one of the input patterns has a
10623 : : stepped encoding. */
10624 : :
10625 : 7238 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10626 : : {
10627 : 6305 : res_npatterns = sel.encoding ().npatterns ();
10628 : 6305 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10629 : 6305 : if (res_nelts_per_pattern == 3
10630 : 973 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10631 : 6811 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10632 : : res_nelts_per_pattern = 2;
10633 : 6305 : res_nelts = res_npatterns * res_nelts_per_pattern;
10634 : : }
10635 : 933 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10636 : : {
10637 : 933 : res_npatterns = res_nelts;
10638 : 933 : res_nelts_per_pattern = 1;
10639 : : }
10640 : : else
10641 : : return NULL_TREE;
10642 : :
10643 : 7238 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10644 : 45979 : for (unsigned i = 0; i < res_nelts; i++)
10645 : : {
10646 : 38741 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10647 : 38741 : uint64_t q;
10648 : 38741 : poly_uint64 r;
10649 : 38741 : unsigned HOST_WIDE_INT index;
10650 : :
10651 : : /* Punt if sel[i] /trunc_div len cannot be determined,
10652 : : because the input vector to be chosen will depend on
10653 : : runtime vector length.
10654 : : For example if len == 4 + 4x, and sel[i] == 4,
10655 : : If len at runtime equals 4, we choose arg1[0].
10656 : : For any other value of len > 4 at runtime, we choose arg0[4].
10657 : : which makes the element choice dependent on runtime vector length. */
10658 : 38741 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10659 : : {
10660 : : if (reason)
10661 : : *reason = "cannot divide selector element by arg len";
10662 : : return NULL_TREE;
10663 : : }
10664 : :
10665 : : /* sel[i] % len will give the index of element in the chosen input
10666 : : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10667 : : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10668 : 38741 : if (!r.is_constant (&index))
10669 : : {
10670 : : if (reason)
10671 : : *reason = "remainder is not constant";
10672 : : return NULL_TREE;
10673 : : }
10674 : :
10675 : 38741 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10676 : 38741 : tree elem = vector_cst_elt (arg, index);
10677 : 38741 : out_elts.quick_push (elem);
10678 : : }
10679 : :
10680 : 7238 : return out_elts.build ();
10681 : 7238 : }
10682 : :
10683 : : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10684 : : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10685 : : NULL_TREE otherwise. */
10686 : :
10687 : : tree
10688 : 25403 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10689 : : {
10690 : 25403 : unsigned int i;
10691 : 25403 : unsigned HOST_WIDE_INT nelts;
10692 : :
10693 : 25403 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10694 : : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10695 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10696 : :
10697 : 25403 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10698 : 25403 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10699 : : return NULL_TREE;
10700 : :
10701 : 15538 : if (TREE_CODE (arg0) == VECTOR_CST
10702 : 7591 : && TREE_CODE (arg1) == VECTOR_CST)
10703 : 7238 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10704 : :
10705 : : /* For fall back case, we want to ensure we have VLS vectors
10706 : : with equal length. */
10707 : 8300 : if (!sel.length ().is_constant (&nelts))
10708 : : return NULL_TREE;
10709 : :
10710 : 8300 : gcc_assert (known_eq (sel.length (),
10711 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))));
10712 : 8300 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10713 : 8300 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10714 : 8300 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10715 : 5235 : return NULL_TREE;
10716 : :
10717 : 3065 : vec<constructor_elt, va_gc> *v;
10718 : 3065 : vec_alloc (v, nelts);
10719 : 16547 : for (i = 0; i < nelts; i++)
10720 : : {
10721 : 13482 : HOST_WIDE_INT index;
10722 : 13482 : if (!sel[i].is_constant (&index))
10723 : : return NULL_TREE;
10724 : 13482 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10725 : : }
10726 : 3065 : return build_constructor (type, v);
10727 : : }
10728 : :
10729 : : /* Try to fold a pointer difference of type TYPE two address expressions of
10730 : : array references AREF0 and AREF1 using location LOC. Return a
10731 : : simplified expression for the difference or NULL_TREE. */
10732 : :
10733 : : static tree
10734 : 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10735 : : tree aref0, tree aref1,
10736 : : bool use_pointer_diff)
10737 : : {
10738 : 39 : tree base0 = TREE_OPERAND (aref0, 0);
10739 : 39 : tree base1 = TREE_OPERAND (aref1, 0);
10740 : 39 : tree base_offset = build_int_cst (type, 0);
10741 : :
10742 : : /* If the bases are array references as well, recurse. If the bases
10743 : : are pointer indirections compute the difference of the pointers.
10744 : : If the bases are equal, we are set. */
10745 : 39 : if ((TREE_CODE (base0) == ARRAY_REF
10746 : 1 : && TREE_CODE (base1) == ARRAY_REF
10747 : 1 : && (base_offset
10748 : 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10749 : : use_pointer_diff)))
10750 : 38 : || (INDIRECT_REF_P (base0)
10751 : 7 : && INDIRECT_REF_P (base1)
10752 : 7 : && (base_offset
10753 : : = use_pointer_diff
10754 : 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10755 : 1 : TREE_OPERAND (base0, 0),
10756 : 1 : TREE_OPERAND (base1, 0))
10757 : 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10758 : 6 : fold_convert (type,
10759 : : TREE_OPERAND (base0, 0)),
10760 : 6 : fold_convert (type,
10761 : : TREE_OPERAND (base1, 0)))))
10762 : 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10763 : : {
10764 : 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10765 : 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10766 : 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10767 : 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10768 : 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10769 : : base_offset,
10770 : : fold_build2_loc (loc, MULT_EXPR, type,
10771 : 15 : diff, esz));
10772 : : }
10773 : : return NULL_TREE;
10774 : : }
10775 : :
10776 : : /* If the real or vector real constant CST of type TYPE has an exact
10777 : : inverse, return it, else return NULL. */
10778 : :
10779 : : tree
10780 : 1194997 : exact_inverse (tree type, tree cst)
10781 : : {
10782 : 1194997 : REAL_VALUE_TYPE r;
10783 : 1194997 : tree unit_type;
10784 : 1194997 : machine_mode mode;
10785 : :
10786 : 1194997 : switch (TREE_CODE (cst))
10787 : : {
10788 : 1194472 : case REAL_CST:
10789 : 1194472 : r = TREE_REAL_CST (cst);
10790 : :
10791 : 1194472 : if (exact_real_inverse (TYPE_MODE (type), &r))
10792 : 339482 : return build_real (type, r);
10793 : :
10794 : : return NULL_TREE;
10795 : :
10796 : 525 : case VECTOR_CST:
10797 : 525 : {
10798 : 525 : unit_type = TREE_TYPE (type);
10799 : 525 : mode = TYPE_MODE (unit_type);
10800 : :
10801 : 525 : tree_vector_builder elts;
10802 : 525 : if (!elts.new_unary_operation (type, cst, false))
10803 : : return NULL_TREE;
10804 : 525 : unsigned int count = elts.encoded_nelts ();
10805 : 585 : for (unsigned int i = 0; i < count; ++i)
10806 : : {
10807 : 525 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10808 : 525 : if (!exact_real_inverse (mode, &r))
10809 : : return NULL_TREE;
10810 : 60 : elts.quick_push (build_real (unit_type, r));
10811 : : }
10812 : :
10813 : 60 : return elts.build ();
10814 : 525 : }
10815 : :
10816 : : default:
10817 : : return NULL_TREE;
10818 : : }
10819 : : }
10820 : :
10821 : : /* Mask out the tz least significant bits of X of type TYPE where
10822 : : tz is the number of trailing zeroes in Y. */
10823 : : static wide_int
10824 : 104483 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10825 : : {
10826 : 104483 : int tz = wi::ctz (y);
10827 : 104483 : if (tz > 0)
10828 : 8078 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10829 : 96405 : return x;
10830 : : }
10831 : :
10832 : : /* Return true when T is an address and is known to be nonzero.
10833 : : For floating point we further ensure that T is not denormal.
10834 : : Similar logic is present in nonzero_address in rtlanal.h.
10835 : :
10836 : : If the return value is based on the assumption that signed overflow
10837 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10838 : : change *STRICT_OVERFLOW_P. */
10839 : :
10840 : : static bool
10841 : 141532238 : tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10842 : : {
10843 : 141834456 : tree type = TREE_TYPE (t);
10844 : 141834456 : enum tree_code code;
10845 : :
10846 : : /* Doing something useful for floating point would need more work. */
10847 : 141834456 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10848 : : return false;
10849 : :
10850 : 141736640 : code = TREE_CODE (t);
10851 : 141736640 : switch (TREE_CODE_CLASS (code))
10852 : : {
10853 : 808097 : case tcc_unary:
10854 : 808097 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10855 : 808097 : strict_overflow_p);
10856 : 2825566 : case tcc_binary:
10857 : 2825566 : case tcc_comparison:
10858 : 2825566 : return tree_binary_nonzero_warnv_p (code, type,
10859 : 2825566 : TREE_OPERAND (t, 0),
10860 : 2825566 : TREE_OPERAND (t, 1),
10861 : 2825566 : strict_overflow_p);
10862 : 11721823 : case tcc_constant:
10863 : 11721823 : case tcc_declaration:
10864 : 11721823 : case tcc_reference:
10865 : 11721823 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10866 : :
10867 : 126381154 : default:
10868 : 126381154 : break;
10869 : : }
10870 : :
10871 : 126381154 : switch (code)
10872 : : {
10873 : 579856 : case TRUTH_NOT_EXPR:
10874 : 579856 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10875 : 579856 : strict_overflow_p);
10876 : :
10877 : 76394 : case TRUTH_AND_EXPR:
10878 : 76394 : case TRUTH_OR_EXPR:
10879 : 76394 : case TRUTH_XOR_EXPR:
10880 : 76394 : return tree_binary_nonzero_warnv_p (code, type,
10881 : 76394 : TREE_OPERAND (t, 0),
10882 : 76394 : TREE_OPERAND (t, 1),
10883 : 76394 : strict_overflow_p);
10884 : :
10885 : 122824489 : case COND_EXPR:
10886 : 122824489 : case CONSTRUCTOR:
10887 : 122824489 : case OBJ_TYPE_REF:
10888 : 122824489 : case ADDR_EXPR:
10889 : 122824489 : case WITH_SIZE_EXPR:
10890 : 122824489 : case SSA_NAME:
10891 : 122824489 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10892 : :
10893 : 80748 : case COMPOUND_EXPR:
10894 : 80748 : case MODIFY_EXPR:
10895 : 80748 : case BIND_EXPR:
10896 : 80748 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10897 : 80748 : strict_overflow_p);
10898 : :
10899 : 221470 : case SAVE_EXPR:
10900 : 221470 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10901 : 221470 : strict_overflow_p);
10902 : :
10903 : 2516882 : case CALL_EXPR:
10904 : 2516882 : {
10905 : 2516882 : tree fndecl = get_callee_fndecl (t);
10906 : 2516882 : if (!fndecl) return false;
10907 : 2515169 : if (flag_delete_null_pointer_checks && !flag_check_new
10908 : 2515160 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10909 : 2515900 : && !TREE_NOTHROW (fndecl))
10910 : : return true;
10911 : 2515810 : if (flag_delete_null_pointer_checks
10912 : 5030943 : && lookup_attribute ("returns_nonnull",
10913 : 2515133 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10914 : : return true;
10915 : 2515802 : return alloca_call_p (t);
10916 : : }
10917 : :
10918 : : default:
10919 : : break;
10920 : : }
10921 : : return false;
10922 : : }
10923 : :
10924 : : /* Return true when T is an address and is known to be nonzero.
10925 : : Handle warnings about undefined signed overflow. */
10926 : :
10927 : : bool
10928 : 140453764 : tree_expr_nonzero_p (tree t)
10929 : : {
10930 : 140453764 : bool ret, strict_overflow_p;
10931 : :
10932 : 140453764 : strict_overflow_p = false;
10933 : 140453764 : ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10934 : 140453764 : if (strict_overflow_p)
10935 : 0 : fold_overflow_warning (("assuming signed overflow does not occur when "
10936 : : "determining that expression is always "
10937 : : "non-zero"),
10938 : : WARN_STRICT_OVERFLOW_MISC);
10939 : 140453764 : return ret;
10940 : : }
10941 : :
10942 : : /* Return true if T is known not to be equal to an integer W. */
10943 : :
10944 : : bool
10945 : 96908434 : expr_not_equal_to (tree t, const wide_int &w)
10946 : : {
10947 : 96908434 : int_range_max vr;
10948 : 96908434 : switch (TREE_CODE (t))
10949 : : {
10950 : 1068878 : case INTEGER_CST:
10951 : 1068878 : return wi::to_wide (t) != w;
10952 : :
10953 : 95838501 : case SSA_NAME:
10954 : 95838501 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10955 : : return false;
10956 : :
10957 : 191677002 : get_range_query (cfun)->range_of_expr (vr, t);
10958 : 95838501 : if (!vr.undefined_p () && !vr.contains_p (w))
10959 : : return true;
10960 : : /* If T has some known zero bits and W has any of those bits set,
10961 : : then T is known not to be equal to W. */
10962 : 95737026 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10963 : 191473676 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10964 : : return true;
10965 : : return false;
10966 : :
10967 : : default:
10968 : : return false;
10969 : : }
10970 : 96908434 : }
10971 : :
10972 : : /* Fold a binary expression of code CODE and type TYPE with operands
10973 : : OP0 and OP1. LOC is the location of the resulting expression.
10974 : : Return the folded expression if folding is successful. Otherwise,
10975 : : return NULL_TREE. */
10976 : :
10977 : : tree
10978 : 782128602 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10979 : : tree op0, tree op1)
10980 : : {
10981 : 782128602 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10982 : 782128602 : tree arg0, arg1, tem;
10983 : 782128602 : tree t1 = NULL_TREE;
10984 : 782128602 : bool strict_overflow_p;
10985 : 782128602 : unsigned int prec;
10986 : :
10987 : 782128602 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10988 : : && TREE_CODE_LENGTH (code) == 2
10989 : : && op0 != NULL_TREE
10990 : : && op1 != NULL_TREE);
10991 : :
10992 : 782128602 : arg0 = op0;
10993 : 782128602 : arg1 = op1;
10994 : :
10995 : : /* Strip any conversions that don't change the mode. This is
10996 : : safe for every expression, except for a comparison expression
10997 : : because its signedness is derived from its operands. So, in
10998 : : the latter case, only strip conversions that don't change the
10999 : : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
11000 : : preserved.
11001 : :
11002 : : Note that this is done as an internal manipulation within the
11003 : : constant folder, in order to find the simplest representation
11004 : : of the arguments so that their form can be studied. In any
11005 : : cases, the appropriate type conversions should be put back in
11006 : : the tree that will get out of the constant folder. */
11007 : :
11008 : 782128602 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
11009 : : {
11010 : 169801369 : STRIP_SIGN_NOPS (arg0);
11011 : 169801369 : STRIP_SIGN_NOPS (arg1);
11012 : : }
11013 : : else
11014 : : {
11015 : 612327233 : STRIP_NOPS (arg0);
11016 : 612327233 : STRIP_NOPS (arg1);
11017 : : }
11018 : :
11019 : : /* Note that TREE_CONSTANT isn't enough: static var addresses are
11020 : : constant but we can't do arithmetic on them. */
11021 : 782128602 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11022 : : {
11023 : 192727149 : tem = const_binop (code, type, arg0, arg1);
11024 : 192727149 : if (tem != NULL_TREE)
11025 : : {
11026 : 190020087 : if (TREE_TYPE (tem) != type)
11027 : 1784851 : tem = fold_convert_loc (loc, type, tem);
11028 : 190020087 : return tem;
11029 : : }
11030 : : }
11031 : :
11032 : : /* If this is a commutative operation, and ARG0 is a constant, move it
11033 : : to ARG1 to reduce the number of tests below. */
11034 : 592108515 : if (commutative_tree_code (code)
11035 : 592108515 : && tree_swap_operands_p (arg0, arg1))
11036 : 32294942 : return fold_build2_loc (loc, code, type, op1, op0);
11037 : :
11038 : : /* Likewise if this is a comparison, and ARG0 is a constant, move it
11039 : : to ARG1 to reduce the number of tests below. */
11040 : 559813573 : if (kind == tcc_comparison
11041 : 559813573 : && tree_swap_operands_p (arg0, arg1))
11042 : 7426291 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11043 : :
11044 : 552387282 : tem = generic_simplify (loc, code, type, op0, op1);
11045 : 552387282 : if (tem)
11046 : : return tem;
11047 : :
11048 : : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11049 : :
11050 : : First check for cases where an arithmetic operation is applied to a
11051 : : compound, conditional, or comparison operation. Push the arithmetic
11052 : : operation inside the compound or conditional to see if any folding
11053 : : can then be done. Convert comparison to conditional for this purpose.
11054 : : The also optimizes non-constant cases that used to be done in
11055 : : expand_expr.
11056 : :
11057 : : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11058 : : one of the operands is a comparison and the other is a comparison, a
11059 : : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11060 : : code below would make the expression more complex. Change it to a
11061 : : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11062 : : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11063 : :
11064 : 466479555 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11065 : : || code == EQ_EXPR || code == NE_EXPR)
11066 : 53557206 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11067 : 53001892 : && ((truth_value_p (TREE_CODE (arg0))
11068 : 1192266 : && (truth_value_p (TREE_CODE (arg1))
11069 : 895156 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11070 : 40 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11071 : 52704766 : || (truth_value_p (TREE_CODE (arg1))
11072 : 6550 : && (truth_value_p (TREE_CODE (arg0))
11073 : 6550 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11074 : 167 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11075 : : {
11076 : 335697 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11077 : 38557 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11078 : : : TRUTH_XOR_EXPR,
11079 : : boolean_type_node,
11080 : : fold_convert_loc (loc, boolean_type_node, arg0),
11081 : : fold_convert_loc (loc, boolean_type_node, arg1));
11082 : :
11083 : 297140 : if (code == EQ_EXPR)
11084 : 32464 : tem = invert_truthvalue_loc (loc, tem);
11085 : :
11086 : 297140 : return fold_convert_loc (loc, type, tem);
11087 : : }
11088 : :
11089 : 466182415 : if (TREE_CODE_CLASS (code) == tcc_binary
11090 : 268681423 : || TREE_CODE_CLASS (code) == tcc_comparison)
11091 : : {
11092 : 285728747 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11093 : : {
11094 : 79838 : tem = fold_build2_loc (loc, code, type,
11095 : 79838 : fold_convert_loc (loc, TREE_TYPE (op0),
11096 : 79838 : TREE_OPERAND (arg0, 1)), op1);
11097 : 79838 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11098 : 79838 : tem);
11099 : : }
11100 : 285648909 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11101 : : {
11102 : 3148 : tem = fold_build2_loc (loc, code, type, op0,
11103 : 3148 : fold_convert_loc (loc, TREE_TYPE (op1),
11104 : 3148 : TREE_OPERAND (arg1, 1)));
11105 : 3148 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11106 : 3148 : tem);
11107 : : }
11108 : :
11109 : 285645761 : if (TREE_CODE (arg0) == COND_EXPR
11110 : 285271311 : || TREE_CODE (arg0) == VEC_COND_EXPR
11111 : 285269076 : || COMPARISON_CLASS_P (arg0))
11112 : : {
11113 : 731889 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11114 : : arg0, arg1,
11115 : : /*cond_first_p=*/1);
11116 : 731889 : if (tem != NULL_TREE)
11117 : : return tem;
11118 : : }
11119 : :
11120 : 285164419 : if (TREE_CODE (arg1) == COND_EXPR
11121 : 284939129 : || TREE_CODE (arg1) == VEC_COND_EXPR
11122 : 284938804 : || COMPARISON_CLASS_P (arg1))
11123 : : {
11124 : 235665 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11125 : : arg1, arg0,
11126 : : /*cond_first_p=*/0);
11127 : 235665 : if (tem != NULL_TREE)
11128 : : return tem;
11129 : : }
11130 : : }
11131 : :
11132 : 465609781 : switch (code)
11133 : : {
11134 : 50894676 : case MEM_REF:
11135 : : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11136 : 50894676 : if (TREE_CODE (arg0) == ADDR_EXPR
11137 : 50894676 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11138 : : {
11139 : 876712 : tree iref = TREE_OPERAND (arg0, 0);
11140 : 876712 : return fold_build2 (MEM_REF, type,
11141 : : TREE_OPERAND (iref, 0),
11142 : : int_const_binop (PLUS_EXPR, arg1,
11143 : : TREE_OPERAND (iref, 1)));
11144 : : }
11145 : :
11146 : : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11147 : 50017964 : if (TREE_CODE (arg0) == ADDR_EXPR
11148 : 50017964 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11149 : : {
11150 : 2307927 : tree base;
11151 : 2307927 : poly_int64 coffset;
11152 : 2307927 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11153 : : &coffset);
11154 : 2307927 : if (!base)
11155 : : return NULL_TREE;
11156 : 2304404 : return fold_build2 (MEM_REF, type,
11157 : : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11158 : : int_const_binop (PLUS_EXPR, arg1,
11159 : : size_int (coffset)));
11160 : : }
11161 : :
11162 : : return NULL_TREE;
11163 : :
11164 : 30177313 : case POINTER_PLUS_EXPR:
11165 : : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11166 : 60354210 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11167 : 60344875 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11168 : 35477 : return fold_convert_loc (loc, type,
11169 : : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11170 : : fold_convert_loc (loc, sizetype,
11171 : : arg1),
11172 : : fold_convert_loc (loc, sizetype,
11173 : 35477 : arg0)));
11174 : :
11175 : : return NULL_TREE;
11176 : :
11177 : 59205765 : case PLUS_EXPR:
11178 : 59205765 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11179 : : {
11180 : : /* X + (X / CST) * -CST is X % CST. */
11181 : 47498734 : if (TREE_CODE (arg1) == MULT_EXPR
11182 : 2253073 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11183 : 47504859 : && operand_equal_p (arg0,
11184 : 6125 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11185 : : {
11186 : 172 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11187 : 172 : tree cst1 = TREE_OPERAND (arg1, 1);
11188 : 172 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11189 : : cst1, cst0);
11190 : 172 : if (sum && integer_zerop (sum))
11191 : 172 : return fold_convert_loc (loc, type,
11192 : : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11193 : 172 : TREE_TYPE (arg0), arg0,
11194 : 172 : cst0));
11195 : : }
11196 : : }
11197 : :
11198 : : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11199 : : one. Make sure the type is not saturating and has the signedness of
11200 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11201 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11202 : 59205593 : if ((TREE_CODE (arg0) == MULT_EXPR
11203 : 47997847 : || TREE_CODE (arg1) == MULT_EXPR)
11204 : 12538604 : && !TYPE_SATURATING (type)
11205 : 12538604 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11206 : 12163546 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11207 : 70728414 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11208 : : {
11209 : 8214292 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11210 : 8214292 : if (tem)
11211 : : return tem;
11212 : : }
11213 : :
11214 : 58016116 : if (! FLOAT_TYPE_P (type))
11215 : : {
11216 : : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11217 : : (plus (plus (mult) (mult)) (foo)) so that we can
11218 : : take advantage of the factoring cases below. */
11219 : 273258 : if (ANY_INTEGRAL_TYPE_P (type)
11220 : 46311433 : && TYPE_OVERFLOW_WRAPS (type)
11221 : 46311433 : && (((TREE_CODE (arg0) == PLUS_EXPR
11222 : 28867128 : || TREE_CODE (arg0) == MINUS_EXPR)
11223 : 3249128 : && TREE_CODE (arg1) == MULT_EXPR)
11224 : 28373554 : || ((TREE_CODE (arg1) == PLUS_EXPR
11225 : 28373554 : || TREE_CODE (arg1) == MINUS_EXPR)
11226 : 401165 : && TREE_CODE (arg0) == MULT_EXPR)))
11227 : : {
11228 : 540187 : tree parg0, parg1, parg, marg;
11229 : 540187 : enum tree_code pcode;
11230 : :
11231 : 540187 : if (TREE_CODE (arg1) == MULT_EXPR)
11232 : : parg = arg0, marg = arg1;
11233 : : else
11234 : 46613 : parg = arg1, marg = arg0;
11235 : 540187 : pcode = TREE_CODE (parg);
11236 : 540187 : parg0 = TREE_OPERAND (parg, 0);
11237 : 540187 : parg1 = TREE_OPERAND (parg, 1);
11238 : 540187 : STRIP_NOPS (parg0);
11239 : 540187 : STRIP_NOPS (parg1);
11240 : :
11241 : 540187 : if (TREE_CODE (parg0) == MULT_EXPR
11242 : 245050 : && TREE_CODE (parg1) != MULT_EXPR)
11243 : 216860 : return fold_build2_loc (loc, pcode, type,
11244 : : fold_build2_loc (loc, PLUS_EXPR, type,
11245 : : fold_convert_loc (loc, type,
11246 : : parg0),
11247 : : fold_convert_loc (loc, type,
11248 : : marg)),
11249 : 216860 : fold_convert_loc (loc, type, parg1));
11250 : 323327 : if (TREE_CODE (parg0) != MULT_EXPR
11251 : 295137 : && TREE_CODE (parg1) == MULT_EXPR)
11252 : 101385 : return
11253 : 101385 : fold_build2_loc (loc, PLUS_EXPR, type,
11254 : : fold_convert_loc (loc, type, parg0),
11255 : : fold_build2_loc (loc, pcode, type,
11256 : : fold_convert_loc (loc, type, marg),
11257 : : fold_convert_loc (loc, type,
11258 : 101385 : parg1)));
11259 : : }
11260 : : }
11261 : : else
11262 : : {
11263 : : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11264 : : to __complex__ ( x, y ). This is not the same for SNaNs or
11265 : : if signed zeros are involved. */
11266 : 11704683 : if (!HONOR_SNANS (arg0)
11267 : 11703519 : && !HONOR_SIGNED_ZEROS (arg0)
11268 : 11723249 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11269 : : {
11270 : 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11271 : 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11272 : 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11273 : 3086 : bool arg0rz = false, arg0iz = false;
11274 : 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11275 : 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11276 : : {
11277 : 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11278 : 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11279 : 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11280 : : {
11281 : 22 : tree rp = arg1r ? arg1r
11282 : 0 : : build1 (REALPART_EXPR, rtype, arg1);
11283 : 22 : tree ip = arg0i ? arg0i
11284 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11285 : 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11286 : : }
11287 : 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11288 : : {
11289 : 53 : tree rp = arg0r ? arg0r
11290 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11291 : 53 : tree ip = arg1i ? arg1i
11292 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11293 : 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11294 : : }
11295 : : }
11296 : : }
11297 : :
11298 : : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11299 : : We associate floats only if the user has specified
11300 : : -fassociative-math. */
11301 : 11704608 : if (flag_associative_math
11302 : 18469 : && TREE_CODE (arg1) == PLUS_EXPR
11303 : 38 : && TREE_CODE (arg0) != MULT_EXPR)
11304 : : {
11305 : 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11306 : 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11307 : 21 : if (TREE_CODE (tree11) == MULT_EXPR
11308 : 5 : && TREE_CODE (tree10) == MULT_EXPR)
11309 : : {
11310 : 1 : tree tree0;
11311 : 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11312 : 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11313 : : }
11314 : : }
11315 : : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11316 : : We associate floats only if the user has specified
11317 : : -fassociative-math. */
11318 : 11704607 : if (flag_associative_math
11319 : 18468 : && TREE_CODE (arg0) == PLUS_EXPR
11320 : 1183 : && TREE_CODE (arg1) != MULT_EXPR)
11321 : : {
11322 : 796 : tree tree00 = TREE_OPERAND (arg0, 0);
11323 : 796 : tree tree01 = TREE_OPERAND (arg0, 1);
11324 : 796 : if (TREE_CODE (tree01) == MULT_EXPR
11325 : 51 : && TREE_CODE (tree00) == MULT_EXPR)
11326 : : {
11327 : 11 : tree tree0;
11328 : 11 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11329 : 11 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11330 : : }
11331 : : }
11332 : : }
11333 : :
11334 : 11703811 : bit_rotate:
11335 : : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11336 : : is a rotate of A by C1 bits. */
11337 : : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11338 : : is a rotate of A by B bits.
11339 : : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11340 : : though in this case CODE must be | and not + or ^, otherwise
11341 : : it doesn't return A when B is 0. */
11342 : 60146625 : {
11343 : 60146625 : enum tree_code code0, code1;
11344 : 60146625 : tree rtype;
11345 : 60146625 : code0 = TREE_CODE (arg0);
11346 : 60146625 : code1 = TREE_CODE (arg1);
11347 : 52840 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11348 : 60130642 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11349 : 38964 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11350 : 38964 : TREE_OPERAND (arg1, 0), 0)
11351 : 36281 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11352 : 36281 : TYPE_UNSIGNED (rtype))
11353 : : /* Only create rotates in complete modes. Other cases are not
11354 : : expanded properly. */
11355 : 60172762 : && (element_precision (rtype)
11356 : 52274 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11357 : : {
11358 : 26068 : tree tree01, tree11;
11359 : 26068 : tree orig_tree01, orig_tree11;
11360 : 26068 : enum tree_code code01, code11;
11361 : :
11362 : 26068 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11363 : 26068 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11364 : 26068 : STRIP_NOPS (tree01);
11365 : 26068 : STRIP_NOPS (tree11);
11366 : 26068 : code01 = TREE_CODE (tree01);
11367 : 26068 : code11 = TREE_CODE (tree11);
11368 : 26068 : if (code11 != MINUS_EXPR
11369 : 25384 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11370 : : {
11371 : 1478 : std::swap (code0, code1);
11372 : 1478 : std::swap (code01, code11);
11373 : 1478 : std::swap (tree01, tree11);
11374 : 1478 : std::swap (orig_tree01, orig_tree11);
11375 : : }
11376 : 52136 : if (code01 == INTEGER_CST
11377 : 3151 : && code11 == INTEGER_CST
11378 : 32368 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11379 : 32368 : == element_precision (rtype)))
11380 : : {
11381 : 6020 : tem = build2_loc (loc, LROTATE_EXPR,
11382 : 3010 : rtype, TREE_OPERAND (arg0, 0),
11383 : : code0 == LSHIFT_EXPR
11384 : : ? orig_tree01 : orig_tree11);
11385 : 3010 : return fold_convert_loc (loc, type, tem);
11386 : : }
11387 : 23058 : else if (code11 == MINUS_EXPR)
11388 : : {
11389 : 939 : tree tree110, tree111;
11390 : 939 : tree110 = TREE_OPERAND (tree11, 0);
11391 : 939 : tree111 = TREE_OPERAND (tree11, 1);
11392 : 939 : STRIP_NOPS (tree110);
11393 : 939 : STRIP_NOPS (tree111);
11394 : 939 : if (TREE_CODE (tree110) == INTEGER_CST
11395 : 928 : && compare_tree_int (tree110,
11396 : 928 : element_precision (rtype)) == 0
11397 : 1851 : && operand_equal_p (tree01, tree111, 0))
11398 : : {
11399 : 775 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11400 : : ? LROTATE_EXPR : RROTATE_EXPR),
11401 : 556 : rtype, TREE_OPERAND (arg0, 0),
11402 : : orig_tree01);
11403 : 556 : return fold_convert_loc (loc, type, tem);
11404 : : }
11405 : : }
11406 : 22119 : else if (code == BIT_IOR_EXPR
11407 : 21005 : && code11 == BIT_AND_EXPR
11408 : 43049 : && pow2p_hwi (element_precision (rtype)))
11409 : : {
11410 : 20930 : tree tree110, tree111;
11411 : 20930 : tree110 = TREE_OPERAND (tree11, 0);
11412 : 20930 : tree111 = TREE_OPERAND (tree11, 1);
11413 : 20930 : STRIP_NOPS (tree110);
11414 : 20930 : STRIP_NOPS (tree111);
11415 : 20930 : if (TREE_CODE (tree110) == NEGATE_EXPR
11416 : 20443 : && TREE_CODE (tree111) == INTEGER_CST
11417 : 20443 : && compare_tree_int (tree111,
11418 : 20443 : element_precision (rtype) - 1) == 0
11419 : 41359 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11420 : : {
11421 : 30491 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11422 : : ? LROTATE_EXPR : RROTATE_EXPR),
11423 : 20351 : rtype, TREE_OPERAND (arg0, 0),
11424 : : orig_tree01);
11425 : 20351 : return fold_convert_loc (loc, type, tem);
11426 : : }
11427 : : }
11428 : : }
11429 : : }
11430 : :
11431 : 148843805 : associate:
11432 : : /* In most languages, can't associate operations on floats through
11433 : : parentheses. Rather than remember where the parentheses were, we
11434 : : don't associate floats at all, unless the user has specified
11435 : : -fassociative-math.
11436 : : And, we need to make sure type is not saturating. */
11437 : :
11438 : 148843805 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11439 : 107357769 : && !TYPE_SATURATING (type)
11440 : 256201574 : && !TYPE_OVERFLOW_SANITIZED (type))
11441 : : {
11442 : 107329705 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11443 : 107329705 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11444 : 107329705 : tree atype = type;
11445 : 107329705 : bool ok = true;
11446 : :
11447 : : /* Split both trees into variables, constants, and literals. Then
11448 : : associate each group together, the constants with literals,
11449 : : then the result with variables. This increases the chances of
11450 : : literals being recombined later and of generating relocatable
11451 : : expressions for the sum of a constant and literal. */
11452 : 107329705 : var0 = split_tree (arg0, type, code,
11453 : : &minus_var0, &con0, &minus_con0,
11454 : : &lit0, &minus_lit0, 0);
11455 : 107329705 : var1 = split_tree (arg1, type, code,
11456 : : &minus_var1, &con1, &minus_con1,
11457 : : &lit1, &minus_lit1, code == MINUS_EXPR);
11458 : :
11459 : : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11460 : 107329705 : if (code == MINUS_EXPR)
11461 : 11369144 : code = PLUS_EXPR;
11462 : :
11463 : : /* With undefined overflow prefer doing association in a type
11464 : : which wraps on overflow, if that is one of the operand types. */
11465 : 107329474 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11466 : 213455499 : && !TYPE_OVERFLOW_WRAPS (type))
11467 : : {
11468 : 58353921 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11469 : 57710005 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11470 : 736409 : atype = TREE_TYPE (arg0);
11471 : 56866584 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11472 : 56653791 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11473 : 217475 : atype = TREE_TYPE (arg1);
11474 : 29435510 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11475 : : }
11476 : :
11477 : : /* With undefined overflow we can only associate constants with one
11478 : : variable, and constants whose association doesn't overflow. */
11479 : 107329474 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11480 : 213455499 : && !TYPE_OVERFLOW_WRAPS (atype))
11481 : : {
11482 : 28481626 : if ((var0 && var1) || (minus_var0 && minus_var1))
11483 : : {
11484 : : /* ??? If split_tree would handle NEGATE_EXPR we could
11485 : : simply reject these cases and the allowed cases would
11486 : : be the var0/minus_var1 ones. */
11487 : 1240 : tree tmp0 = var0 ? var0 : minus_var0;
11488 : 5306505 : tree tmp1 = var1 ? var1 : minus_var1;
11489 : 5306505 : bool one_neg = false;
11490 : :
11491 : 5306505 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11492 : : {
11493 : 1661 : tmp0 = TREE_OPERAND (tmp0, 0);
11494 : 1661 : one_neg = !one_neg;
11495 : : }
11496 : 4777824 : if (CONVERT_EXPR_P (tmp0)
11497 : 544358 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11498 : 5850520 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11499 : 544015 : <= TYPE_PRECISION (atype)))
11500 : 531677 : tmp0 = TREE_OPERAND (tmp0, 0);
11501 : 5306505 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11502 : : {
11503 : 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11504 : 170 : one_neg = !one_neg;
11505 : : }
11506 : 5000329 : if (CONVERT_EXPR_P (tmp1)
11507 : 328952 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11508 : 5635347 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11509 : 328842 : <= TYPE_PRECISION (atype)))
11510 : 319042 : tmp1 = TREE_OPERAND (tmp1, 0);
11511 : : /* The only case we can still associate with two variables
11512 : : is if they cancel out. */
11513 : 5306505 : if (!one_neg
11514 : 5306505 : || !operand_equal_p (tmp0, tmp1, 0))
11515 : : ok = false;
11516 : : }
11517 : 22793358 : else if ((var0 && minus_var1
11518 : 3745346 : && ! operand_equal_p (var0, minus_var1, 0))
11519 : 42223134 : || (minus_var0 && var1
11520 : 11307 : && ! operand_equal_p (minus_var0, var1, 0)))
11521 : : ok = false;
11522 : : }
11523 : :
11524 : : /* Only do something if we found more than two objects. Otherwise,
11525 : : nothing has changed and we risk infinite recursion. */
11526 : : if (ok
11527 : 98266619 : && ((var0 != 0) + (var1 != 0)
11528 : 98266619 : + (minus_var0 != 0) + (minus_var1 != 0)
11529 : 98266619 : + (con0 != 0) + (con1 != 0)
11530 : 98266619 : + (minus_con0 != 0) + (minus_con1 != 0)
11531 : 98266619 : + (lit0 != 0) + (lit1 != 0)
11532 : 98266619 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11533 : : {
11534 : 1911105 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11535 : 3822210 : int minus_var0_origin
11536 : 1911105 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11537 : 1911105 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11538 : 3822210 : int minus_con0_origin
11539 : 1911105 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11540 : 1911105 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11541 : 3822210 : int minus_lit0_origin
11542 : 1911105 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11543 : 1911105 : var0 = associate_trees (loc, var0, var1, code, atype);
11544 : 1911105 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11545 : : code, atype);
11546 : 1911105 : con0 = associate_trees (loc, con0, con1, code, atype);
11547 : 1911105 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11548 : : code, atype);
11549 : 1911105 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11550 : 1911105 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11551 : : code, atype);
11552 : :
11553 : 1911105 : if (minus_var0 && var0)
11554 : : {
11555 : 1255946 : var0_origin |= minus_var0_origin;
11556 : 1255946 : var0 = associate_trees (loc, var0, minus_var0,
11557 : : MINUS_EXPR, atype);
11558 : 1255946 : minus_var0 = 0;
11559 : 1255946 : minus_var0_origin = 0;
11560 : : }
11561 : 1911105 : if (minus_con0 && con0)
11562 : : {
11563 : 3610 : con0_origin |= minus_con0_origin;
11564 : 3610 : con0 = associate_trees (loc, con0, minus_con0,
11565 : : MINUS_EXPR, atype);
11566 : 3610 : minus_con0 = 0;
11567 : 3610 : minus_con0_origin = 0;
11568 : : }
11569 : :
11570 : : /* Preserve the MINUS_EXPR if the negative part of the literal is
11571 : : greater than the positive part. Otherwise, the multiplicative
11572 : : folding code (i.e extract_muldiv) may be fooled in case
11573 : : unsigned constants are subtracted, like in the following
11574 : : example: ((X*2 + 4) - 8U)/2. */
11575 : 1911105 : if (minus_lit0 && lit0)
11576 : : {
11577 : 217036 : if (TREE_CODE (lit0) == INTEGER_CST
11578 : 217036 : && TREE_CODE (minus_lit0) == INTEGER_CST
11579 : 217036 : && tree_int_cst_lt (lit0, minus_lit0)
11580 : : /* But avoid ending up with only negated parts. */
11581 : 273109 : && (var0 || con0))
11582 : : {
11583 : 51619 : minus_lit0_origin |= lit0_origin;
11584 : 51619 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11585 : : MINUS_EXPR, atype);
11586 : 51619 : lit0 = 0;
11587 : 51619 : lit0_origin = 0;
11588 : : }
11589 : : else
11590 : : {
11591 : 165417 : lit0_origin |= minus_lit0_origin;
11592 : 165417 : lit0 = associate_trees (loc, lit0, minus_lit0,
11593 : : MINUS_EXPR, atype);
11594 : 165417 : minus_lit0 = 0;
11595 : 165417 : minus_lit0_origin = 0;
11596 : : }
11597 : : }
11598 : :
11599 : : /* Don't introduce overflows through reassociation. */
11600 : 1278324 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11601 : 3189388 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11602 : 1911105 : return NULL_TREE;
11603 : :
11604 : : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11605 : 1911064 : con0_origin |= lit0_origin;
11606 : 1911064 : con0 = associate_trees (loc, con0, lit0, code, atype);
11607 : 1911064 : minus_con0_origin |= minus_lit0_origin;
11608 : 1911064 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11609 : : code, atype);
11610 : :
11611 : : /* Eliminate minus_con0. */
11612 : 1911064 : if (minus_con0)
11613 : : {
11614 : 630193 : if (con0)
11615 : : {
11616 : 8405 : con0_origin |= minus_con0_origin;
11617 : 8405 : con0 = associate_trees (loc, con0, minus_con0,
11618 : : MINUS_EXPR, atype);
11619 : : }
11620 : 621788 : else if (var0)
11621 : : {
11622 : 621788 : var0_origin |= minus_con0_origin;
11623 : 621788 : var0 = associate_trees (loc, var0, minus_con0,
11624 : : MINUS_EXPR, atype);
11625 : : }
11626 : : else
11627 : 0 : gcc_unreachable ();
11628 : : }
11629 : :
11630 : : /* Eliminate minus_var0. */
11631 : 1911064 : if (minus_var0)
11632 : : {
11633 : 343851 : if (con0)
11634 : : {
11635 : 343851 : con0_origin |= minus_var0_origin;
11636 : 343851 : con0 = associate_trees (loc, con0, minus_var0,
11637 : : MINUS_EXPR, atype);
11638 : : }
11639 : : else
11640 : 0 : gcc_unreachable ();
11641 : : }
11642 : :
11643 : : /* Reassociate only if there has been any actual association
11644 : : between subtrees from op0 and subtrees from op1 in at
11645 : : least one of the operands, otherwise we risk infinite
11646 : : recursion. See PR114084. */
11647 : 1911064 : if (var0_origin != 3 && con0_origin != 3)
11648 : : return NULL_TREE;
11649 : :
11650 : 1909400 : return
11651 : 1909400 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11652 : 1909400 : code, atype));
11653 : : }
11654 : : }
11655 : :
11656 : : return NULL_TREE;
11657 : :
11658 : 21975275 : case POINTER_DIFF_EXPR:
11659 : 21975275 : case MINUS_EXPR:
11660 : : /* Fold &a[i] - &a[j] to i-j. */
11661 : 21975275 : if (TREE_CODE (arg0) == ADDR_EXPR
11662 : 51406 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11663 : 5959 : && TREE_CODE (arg1) == ADDR_EXPR
11664 : 21975825 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11665 : : {
11666 : 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11667 : 38 : TREE_OPERAND (arg0, 0),
11668 : 38 : TREE_OPERAND (arg1, 0),
11669 : : code
11670 : : == POINTER_DIFF_EXPR);
11671 : 38 : if (tem)
11672 : : return tem;
11673 : : }
11674 : :
11675 : : /* Further transformations are not for pointers. */
11676 : 21975261 : if (code == POINTER_DIFF_EXPR)
11677 : : return NULL_TREE;
11678 : :
11679 : : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11680 : 19416264 : if (TREE_CODE (arg0) == NEGATE_EXPR
11681 : 147778 : && negate_expr_p (op1)
11682 : : /* If arg0 is e.g. unsigned int and type is int, then this could
11683 : : introduce UB, because if A is INT_MIN at runtime, the original
11684 : : expression can be well defined while the latter is not.
11685 : : See PR83269. */
11686 : 19417081 : && !(ANY_INTEGRAL_TYPE_P (type)
11687 : 817 : && TYPE_OVERFLOW_UNDEFINED (type)
11688 : 805 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11689 : 805 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11690 : 810 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11691 : : fold_convert_loc (loc, type,
11692 : 1620 : TREE_OPERAND (arg0, 0)));
11693 : :
11694 : : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11695 : : __complex__ ( x, -y ). This is not the same for SNaNs or if
11696 : : signed zeros are involved. */
11697 : 19415454 : if (!HONOR_SNANS (arg0)
11698 : 19414590 : && !HONOR_SIGNED_ZEROS (arg0)
11699 : 31545904 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11700 : : {
11701 : 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11702 : 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11703 : 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11704 : 53 : bool arg0rz = false, arg0iz = false;
11705 : 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11706 : 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11707 : : {
11708 : 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11709 : 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11710 : 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11711 : : {
11712 : 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11713 : : arg1r ? arg1r
11714 : 0 : : build1 (REALPART_EXPR, rtype, arg1));
11715 : 9 : tree ip = arg0i ? arg0i
11716 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11717 : 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11718 : : }
11719 : 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11720 : : {
11721 : 15 : tree rp = arg0r ? arg0r
11722 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11723 : 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11724 : : arg1i ? arg1i
11725 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11726 : 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11727 : : }
11728 : : }
11729 : : }
11730 : :
11731 : : /* A - B -> A + (-B) if B is easily negatable. */
11732 : 19415430 : if (negate_expr_p (op1)
11733 : 705557 : && ! TYPE_OVERFLOW_SANITIZED (type)
11734 : 20118513 : && ((FLOAT_TYPE_P (type)
11735 : : /* Avoid this transformation if B is a positive REAL_CST. */
11736 : 65 : && (TREE_CODE (op1) != REAL_CST
11737 : 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11738 : 703018 : || INTEGRAL_TYPE_P (type)))
11739 : 702902 : return fold_build2_loc (loc, PLUS_EXPR, type,
11740 : : fold_convert_loc (loc, type, arg0),
11741 : 702902 : negate_expr (op1));
11742 : :
11743 : : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11744 : : one. Make sure the type is not saturating and has the signedness of
11745 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11746 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11747 : 18712528 : if ((TREE_CODE (arg0) == MULT_EXPR
11748 : 17407461 : || TREE_CODE (arg1) == MULT_EXPR)
11749 : 2666062 : && !TYPE_SATURATING (type)
11750 : 2666062 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11751 : 2533741 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11752 : 21193295 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11753 : : {
11754 : 351229 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11755 : 351229 : if (tem)
11756 : : return tem;
11757 : : }
11758 : :
11759 : 18661670 : goto associate;
11760 : :
11761 : 64131970 : case MULT_EXPR:
11762 : 64131970 : if (! FLOAT_TYPE_P (type))
11763 : : {
11764 : : /* Transform x * -C into -x * C if x is easily negatable. */
11765 : 41587331 : if (TREE_CODE (op1) == INTEGER_CST
11766 : 38875637 : && tree_int_cst_sgn (op1) == -1
11767 : 211774 : && negate_expr_p (op0)
11768 : 336 : && negate_expr_p (op1)
11769 : 320 : && (tem = negate_expr (op1)) != op1
11770 : 41587651 : && ! TREE_OVERFLOW (tem))
11771 : 320 : return fold_build2_loc (loc, MULT_EXPR, type,
11772 : : fold_convert_loc (loc, type,
11773 : 320 : negate_expr (op0)), tem);
11774 : :
11775 : 41587011 : strict_overflow_p = false;
11776 : 41587011 : if (TREE_CODE (arg1) == INTEGER_CST
11777 : 41587011 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11778 : : &strict_overflow_p)) != 0)
11779 : : {
11780 : 511293 : if (strict_overflow_p)
11781 : 10 : fold_overflow_warning (("assuming signed overflow does not "
11782 : : "occur when simplifying "
11783 : : "multiplication"),
11784 : : WARN_STRICT_OVERFLOW_MISC);
11785 : 511293 : return fold_convert_loc (loc, type, tem);
11786 : : }
11787 : :
11788 : : /* Optimize z * conj(z) for integer complex numbers. */
11789 : 41075718 : if (TREE_CODE (arg0) == CONJ_EXPR
11790 : 41075718 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11791 : 1 : return fold_mult_zconjz (loc, type, arg1);
11792 : 41075717 : if (TREE_CODE (arg1) == CONJ_EXPR
11793 : 41075717 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11794 : 0 : return fold_mult_zconjz (loc, type, arg0);
11795 : : }
11796 : : else
11797 : : {
11798 : : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11799 : : This is not the same for NaNs or if signed zeros are
11800 : : involved. */
11801 : 22544639 : if (!HONOR_NANS (arg0)
11802 : 32715 : && !HONOR_SIGNED_ZEROS (arg0)
11803 : 32415 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11804 : 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11805 : 22544864 : && real_zerop (TREE_REALPART (arg1)))
11806 : : {
11807 : 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11808 : 218 : if (real_onep (TREE_IMAGPART (arg1)))
11809 : : {
11810 : 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11811 : 63 : arg0 = save_expr (arg0);
11812 : 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11813 : : rtype, arg0);
11814 : 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11815 : : rtype, arg0);
11816 : 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11817 : : negate_expr (iarg0),
11818 : 208 : rarg0);
11819 : : }
11820 : 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11821 : : {
11822 : 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11823 : 0 : arg0 = save_expr (arg0);
11824 : 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11825 : : rtype, arg0);
11826 : 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11827 : : rtype, arg0);
11828 : 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11829 : : iarg0,
11830 : 10 : negate_expr (rarg0));
11831 : : }
11832 : : }
11833 : :
11834 : : /* Optimize z * conj(z) for floating point complex numbers.
11835 : : Guarded by flag_unsafe_math_optimizations as non-finite
11836 : : imaginary components don't produce scalar results. */
11837 : 22544421 : if (flag_unsafe_math_optimizations
11838 : 32244 : && TREE_CODE (arg0) == CONJ_EXPR
11839 : 22544423 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11840 : 1 : return fold_mult_zconjz (loc, type, arg1);
11841 : 22544420 : if (flag_unsafe_math_optimizations
11842 : 32243 : && TREE_CODE (arg1) == CONJ_EXPR
11843 : 22544424 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11844 : 0 : return fold_mult_zconjz (loc, type, arg0);
11845 : : }
11846 : 63620137 : goto associate;
11847 : :
11848 : 1786940 : case BIT_IOR_EXPR:
11849 : : /* Canonicalize (X & C1) | C2. */
11850 : 1786940 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11851 : 127378 : && TREE_CODE (arg1) == INTEGER_CST
11852 : 1869734 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11853 : : {
11854 : 82786 : int width = TYPE_PRECISION (type), w;
11855 : 82786 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11856 : 82786 : wide_int c2 = wi::to_wide (arg1);
11857 : :
11858 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11859 : 82786 : if ((c1 & c2) == c1)
11860 : 0 : return omit_one_operand_loc (loc, type, arg1,
11861 : 0 : TREE_OPERAND (arg0, 0));
11862 : :
11863 : 82786 : wide_int msk = wi::mask (width, false,
11864 : 82786 : TYPE_PRECISION (TREE_TYPE (arg1)));
11865 : :
11866 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11867 : 82786 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11868 : : {
11869 : 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11870 : 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11871 : : }
11872 : :
11873 : : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11874 : : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11875 : : mode which allows further optimizations. */
11876 : 82780 : c1 &= msk;
11877 : 82780 : c2 &= msk;
11878 : 82780 : wide_int c3 = wi::bit_and_not (c1, c2);
11879 : 256279 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11880 : : {
11881 : 173741 : wide_int mask = wi::mask (w, false,
11882 : 173741 : TYPE_PRECISION (type));
11883 : 347482 : if (((c1 | c2) & mask) == mask
11884 : 347482 : && wi::bit_and_not (c1, mask) == 0)
11885 : : {
11886 : 242 : c3 = mask;
11887 : 242 : break;
11888 : : }
11889 : 173741 : }
11890 : :
11891 : 82780 : if (c3 != c1)
11892 : : {
11893 : 562 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11894 : 1124 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11895 : 562 : wide_int_to_tree (type, c3));
11896 : 562 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11897 : : }
11898 : 83916 : }
11899 : :
11900 : : /* See if this can be simplified into a rotate first. If that
11901 : : is unsuccessful continue in the association code. */
11902 : 1786372 : goto bit_rotate;
11903 : :
11904 : 662469 : case BIT_XOR_EXPR:
11905 : : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11906 : 662469 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11907 : 2420 : && INTEGRAL_TYPE_P (type)
11908 : 1815 : && integer_onep (TREE_OPERAND (arg0, 1))
11909 : 662472 : && integer_onep (arg1))
11910 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11911 : 0 : build_zero_cst (TREE_TYPE (arg0)));
11912 : :
11913 : : /* See if this can be simplified into a rotate first. If that
11914 : : is unsuccessful continue in the association code. */
11915 : 662469 : goto bit_rotate;
11916 : :
11917 : 6015080 : case BIT_AND_EXPR:
11918 : : /* Fold !X & 1 as X == 0. */
11919 : 6015080 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11920 : 6015080 : && integer_onep (arg1))
11921 : : {
11922 : 0 : tem = TREE_OPERAND (arg0, 0);
11923 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11924 : 0 : build_zero_cst (TREE_TYPE (tem)));
11925 : : }
11926 : :
11927 : : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11928 : : multiple of 1 << CST. */
11929 : 6015080 : if (TREE_CODE (arg1) == INTEGER_CST)
11930 : : {
11931 : 4401456 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11932 : 4401456 : wide_int ncst1 = -cst1;
11933 : 4401456 : if ((cst1 & ncst1) == ncst1
11934 : 4580946 : && multiple_of_p (type, arg0,
11935 : 4580946 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11936 : 466 : return fold_convert_loc (loc, type, arg0);
11937 : 4401456 : }
11938 : :
11939 : : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11940 : : bits from CST2. */
11941 : 6014614 : if (TREE_CODE (arg1) == INTEGER_CST
11942 : 4400990 : && TREE_CODE (arg0) == MULT_EXPR
11943 : 6119127 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11944 : : {
11945 : 104483 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11946 : 104483 : wide_int masked
11947 : 104483 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11948 : :
11949 : 104483 : if (masked == 0)
11950 : 6714 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11951 : 6714 : arg0, arg1);
11952 : 97769 : else if (masked != warg1)
11953 : : {
11954 : : /* Avoid the transform if arg1 is a mask of some
11955 : : mode which allows further optimizations. */
11956 : 653 : int pop = wi::popcount (warg1);
11957 : 677 : if (!(pop >= BITS_PER_UNIT
11958 : 50 : && pow2p_hwi (pop)
11959 : 701 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11960 : 1258 : return fold_build2_loc (loc, code, type, op0,
11961 : 1258 : wide_int_to_tree (type, masked));
11962 : : }
11963 : 104483 : }
11964 : :
11965 : : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11966 : 4393647 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11967 : 6343035 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11968 : : {
11969 : 176828 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11970 : :
11971 : 176828 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11972 : 176828 : if (mask == -1)
11973 : 667 : return
11974 : 667 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11975 : 176828 : }
11976 : :
11977 : 6006604 : goto associate;
11978 : :
11979 : 6253272 : case RDIV_EXPR:
11980 : : /* Don't touch a floating-point divide by zero unless the mode
11981 : : of the constant can represent infinity. */
11982 : 6253272 : if (TREE_CODE (arg1) == REAL_CST
11983 : 3155929 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11984 : 6253272 : && real_zerop (arg1))
11985 : 0 : return NULL_TREE;
11986 : :
11987 : : /* (-A) / (-B) -> A / B */
11988 : 6253272 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11989 : 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11990 : 3 : TREE_OPERAND (arg0, 0),
11991 : 3 : negate_expr (arg1));
11992 : 6253269 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11993 : 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11994 : : negate_expr (arg0),
11995 : 0 : TREE_OPERAND (arg1, 0));
11996 : : return NULL_TREE;
11997 : :
11998 : 2103662 : case TRUNC_DIV_EXPR:
11999 : : /* Fall through */
12000 : :
12001 : 2103662 : case FLOOR_DIV_EXPR:
12002 : : /* Simplify A / (B << N) where A and B are positive and B is
12003 : : a power of 2, to A >> (N + log2(B)). */
12004 : 2103662 : strict_overflow_p = false;
12005 : 2103662 : if (TREE_CODE (arg1) == LSHIFT_EXPR
12006 : 2103662 : && (TYPE_UNSIGNED (type)
12007 : 8 : || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12008 : : {
12009 : 17 : tree sval = TREE_OPERAND (arg1, 0);
12010 : 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12011 : : {
12012 : 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
12013 : 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
12014 : 16 : wi::exact_log2 (wi::to_wide (sval)));
12015 : :
12016 : 16 : if (strict_overflow_p)
12017 : 0 : fold_overflow_warning (("assuming signed overflow does not "
12018 : : "occur when simplifying A / (B << N)"),
12019 : : WARN_STRICT_OVERFLOW_MISC);
12020 : :
12021 : 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12022 : : sh_cnt, pow2);
12023 : 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
12024 : 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
12025 : : }
12026 : : }
12027 : :
12028 : : /* Fall through */
12029 : :
12030 : 3291814 : case ROUND_DIV_EXPR:
12031 : 3291814 : case CEIL_DIV_EXPR:
12032 : 3291814 : case EXACT_DIV_EXPR:
12033 : 3291814 : if (integer_zerop (arg1))
12034 : : return NULL_TREE;
12035 : :
12036 : : /* Convert -A / -B to A / B when the type is signed and overflow is
12037 : : undefined. */
12038 : 3288800 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12039 : 819654 : && TREE_CODE (op0) == NEGATE_EXPR
12040 : 3288862 : && negate_expr_p (op1))
12041 : : {
12042 : 30 : if (ANY_INTEGRAL_TYPE_P (type))
12043 : 30 : fold_overflow_warning (("assuming signed overflow does not occur "
12044 : : "when distributing negation across "
12045 : : "division"),
12046 : : WARN_STRICT_OVERFLOW_MISC);
12047 : 60 : return fold_build2_loc (loc, code, type,
12048 : : fold_convert_loc (loc, type,
12049 : 30 : TREE_OPERAND (arg0, 0)),
12050 : 30 : negate_expr (op1));
12051 : : }
12052 : 3288770 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12053 : 819624 : && TREE_CODE (arg1) == NEGATE_EXPR
12054 : 3289014 : && negate_expr_p (op0))
12055 : : {
12056 : 36 : if (ANY_INTEGRAL_TYPE_P (type))
12057 : 36 : fold_overflow_warning (("assuming signed overflow does not occur "
12058 : : "when distributing negation across "
12059 : : "division"),
12060 : : WARN_STRICT_OVERFLOW_MISC);
12061 : 36 : return fold_build2_loc (loc, code, type,
12062 : : negate_expr (op0),
12063 : : fold_convert_loc (loc, type,
12064 : 72 : TREE_OPERAND (arg1, 0)));
12065 : : }
12066 : :
12067 : : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12068 : : operation, EXACT_DIV_EXPR.
12069 : :
12070 : : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12071 : : At one time others generated faster code, it's not clear if they do
12072 : : after the last round to changes to the DIV code in expmed.cc. */
12073 : 3288734 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12074 : 3288734 : && multiple_of_p (type, arg0, arg1))
12075 : 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12076 : : fold_convert (type, arg0),
12077 : 0 : fold_convert (type, arg1));
12078 : :
12079 : 3288734 : strict_overflow_p = false;
12080 : 3288734 : if (TREE_CODE (arg1) == INTEGER_CST
12081 : 3288734 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12082 : : &strict_overflow_p)) != 0)
12083 : : {
12084 : 2451 : if (strict_overflow_p)
12085 : 157 : fold_overflow_warning (("assuming signed overflow does not occur "
12086 : : "when simplifying division"),
12087 : : WARN_STRICT_OVERFLOW_MISC);
12088 : 2451 : return fold_convert_loc (loc, type, tem);
12089 : : }
12090 : :
12091 : : return NULL_TREE;
12092 : :
12093 : 611960 : case CEIL_MOD_EXPR:
12094 : 611960 : case FLOOR_MOD_EXPR:
12095 : 611960 : case ROUND_MOD_EXPR:
12096 : 611960 : case TRUNC_MOD_EXPR:
12097 : 611960 : strict_overflow_p = false;
12098 : 611960 : if (TREE_CODE (arg1) == INTEGER_CST
12099 : 611960 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12100 : : &strict_overflow_p)) != 0)
12101 : : {
12102 : 0 : if (strict_overflow_p)
12103 : 0 : fold_overflow_warning (("assuming signed overflow does not occur "
12104 : : "when simplifying modulus"),
12105 : : WARN_STRICT_OVERFLOW_MISC);
12106 : 0 : return fold_convert_loc (loc, type, tem);
12107 : : }
12108 : :
12109 : : return NULL_TREE;
12110 : :
12111 : 2008812 : case LROTATE_EXPR:
12112 : 2008812 : case RROTATE_EXPR:
12113 : 2008812 : case RSHIFT_EXPR:
12114 : 2008812 : case LSHIFT_EXPR:
12115 : : /* Since negative shift count is not well-defined,
12116 : : don't try to compute it in the compiler. */
12117 : 2008812 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12118 : : return NULL_TREE;
12119 : :
12120 : 2007855 : prec = element_precision (type);
12121 : :
12122 : : /* If we have a rotate of a bit operation with the rotate count and
12123 : : the second operand of the bit operation both constant,
12124 : : permute the two operations. */
12125 : 2451 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12126 : 1897 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12127 : 1897 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12128 : 1897 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12129 : 2007855 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12130 : : {
12131 : 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12132 : 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12133 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12134 : : fold_build2_loc (loc, code, type,
12135 : : arg00, arg1),
12136 : : fold_build2_loc (loc, code, type,
12137 : 0 : arg01, arg1));
12138 : : }
12139 : :
12140 : : return NULL_TREE;
12141 : :
12142 : 432686 : case MIN_EXPR:
12143 : 432686 : case MAX_EXPR:
12144 : 432686 : goto associate;
12145 : :
12146 : 5670755 : case TRUTH_ANDIF_EXPR:
12147 : : /* Note that the operands of this must be ints
12148 : : and their values must be 0 or 1.
12149 : : ("true" is a fixed value perhaps depending on the language.) */
12150 : : /* If first arg is constant zero, return it. */
12151 : 5670755 : if (integer_zerop (arg0))
12152 : 1134805 : return fold_convert_loc (loc, type, arg0);
12153 : : /* FALLTHRU */
12154 : 15231472 : case TRUTH_AND_EXPR:
12155 : : /* If either arg is constant true, drop it. */
12156 : 15231472 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12157 : 2038708 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12158 : 805621 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12159 : : /* Preserve sequence points. */
12160 : 13952342 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12161 : 732151 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12162 : : /* If second arg is constant zero, result is zero, but first arg
12163 : : must be evaluated. */
12164 : 12460613 : if (integer_zerop (arg1))
12165 : 46043 : return omit_one_operand_loc (loc, type, arg1, arg0);
12166 : : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12167 : : case will be handled here. */
12168 : 12414570 : if (integer_zerop (arg0))
12169 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12170 : :
12171 : : /* !X && X is always false. */
12172 : 12414570 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12173 : 12414570 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12174 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12175 : : /* X && !X is always false. */
12176 : 12414570 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12177 : 12414570 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12178 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12179 : :
12180 : : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12181 : : means A >= Y && A != MAX, but in this case we know that
12182 : : A < X <= MAX. */
12183 : :
12184 : 12414570 : if (!TREE_SIDE_EFFECTS (arg0)
12185 : 12414570 : && !TREE_SIDE_EFFECTS (arg1))
12186 : : {
12187 : 11435059 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12188 : 11435059 : if (tem && !operand_equal_p (tem, arg0, 0))
12189 : 469 : return fold_convert (type,
12190 : : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12191 : : tem, arg1));
12192 : :
12193 : 11434590 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12194 : 11434590 : if (tem && !operand_equal_p (tem, arg1, 0))
12195 : 9996 : return fold_convert (type,
12196 : : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12197 : : arg0, tem));
12198 : : }
12199 : :
12200 : 12404105 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12201 : : != NULL_TREE)
12202 : : return tem;
12203 : :
12204 : : return NULL_TREE;
12205 : :
12206 : 3067257 : case TRUTH_ORIF_EXPR:
12207 : : /* Note that the operands of this must be ints
12208 : : and their values must be 0 or true.
12209 : : ("true" is a fixed value perhaps depending on the language.) */
12210 : : /* If first arg is constant true, return it. */
12211 : 3067257 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12212 : 124195 : return fold_convert_loc (loc, type, arg0);
12213 : : /* FALLTHRU */
12214 : 12392444 : case TRUTH_OR_EXPR:
12215 : : /* If either arg is constant zero, drop it. */
12216 : 12392444 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12217 : 151333 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12218 : 481724 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12219 : : /* Preserve sequence points. */
12220 : 12671873 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12221 : 419895 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12222 : : /* If second arg is constant true, result is true, but we must
12223 : : evaluate first arg. */
12224 : 11821216 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12225 : 50962 : return omit_one_operand_loc (loc, type, arg1, arg0);
12226 : : /* Likewise for first arg, but note this only occurs here for
12227 : : TRUTH_OR_EXPR. */
12228 : 11770254 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12229 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12230 : :
12231 : : /* !X || X is always true. */
12232 : 11770254 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12233 : 11770254 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12234 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12235 : : /* X || !X is always true. */
12236 : 11770254 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12237 : 11770254 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12238 : 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12239 : :
12240 : : /* (X && !Y) || (!X && Y) is X ^ Y */
12241 : 11770253 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12242 : 1601 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12243 : : {
12244 : 659 : tree a0, a1, l0, l1, n0, n1;
12245 : :
12246 : 659 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12247 : 659 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12248 : :
12249 : 659 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12250 : 659 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12251 : :
12252 : 659 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12253 : 659 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12254 : :
12255 : 659 : if ((operand_equal_p (n0, a0, 0)
12256 : 18 : && operand_equal_p (n1, a1, 0))
12257 : 667 : || (operand_equal_p (n0, a1, 0)
12258 : 3 : && operand_equal_p (n1, a0, 0)))
12259 : 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12260 : : }
12261 : :
12262 : 11770240 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12263 : : != NULL_TREE)
12264 : : return tem;
12265 : :
12266 : : return NULL_TREE;
12267 : :
12268 : 38105 : case TRUTH_XOR_EXPR:
12269 : : /* If the second arg is constant zero, drop it. */
12270 : 38105 : if (integer_zerop (arg1))
12271 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12272 : : /* If the second arg is constant true, this is a logical inversion. */
12273 : 38105 : if (integer_onep (arg1))
12274 : : {
12275 : 0 : tem = invert_truthvalue_loc (loc, arg0);
12276 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12277 : : }
12278 : : /* Identical arguments cancel to zero. */
12279 : 38105 : if (operand_equal_p (arg0, arg1, 0))
12280 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12281 : :
12282 : : /* !X ^ X is always true. */
12283 : 38105 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12284 : 38105 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12285 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12286 : :
12287 : : /* X ^ !X is always true. */
12288 : 38105 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12289 : 38105 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12290 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12291 : :
12292 : : return NULL_TREE;
12293 : :
12294 : 45027194 : case EQ_EXPR:
12295 : 45027194 : case NE_EXPR:
12296 : 45027194 : STRIP_NOPS (arg0);
12297 : 45027194 : STRIP_NOPS (arg1);
12298 : :
12299 : 45027194 : tem = fold_comparison (loc, code, type, op0, op1);
12300 : 45027194 : if (tem != NULL_TREE)
12301 : : return tem;
12302 : :
12303 : : /* bool_var != 1 becomes !bool_var. */
12304 : 46120428 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12305 : 45062656 : && code == NE_EXPR)
12306 : 38368 : return fold_convert_loc (loc, type,
12307 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12308 : 76736 : TREE_TYPE (arg0), arg0));
12309 : :
12310 : : /* bool_var == 0 becomes !bool_var. */
12311 : 46043692 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12312 : 45867064 : && code == EQ_EXPR)
12313 : 200972 : return fold_convert_loc (loc, type,
12314 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12315 : 401944 : TREE_TYPE (arg0), arg0));
12316 : :
12317 : : /* !exp != 0 becomes !exp */
12318 : 580408 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12319 : 45359801 : && code == NE_EXPR)
12320 : 575544 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12321 : :
12322 : : /* If this is an EQ or NE comparison with zero and ARG0 is
12323 : : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12324 : : two operations, but the latter can be done in one less insn
12325 : : on machines that have only two-operand insns or on which a
12326 : : constant cannot be the first operand. */
12327 : 44204562 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12328 : 44204562 : && integer_zerop (arg1))
12329 : : {
12330 : 1444473 : tree arg00 = TREE_OPERAND (arg0, 0);
12331 : 1444473 : tree arg01 = TREE_OPERAND (arg0, 1);
12332 : 1444473 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12333 : 1444473 : && integer_onep (TREE_OPERAND (arg00, 0)))
12334 : : {
12335 : 4723 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12336 : 4723 : arg01, TREE_OPERAND (arg00, 1));
12337 : 4723 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12338 : 4723 : build_one_cst (TREE_TYPE (arg0)));
12339 : 4723 : return fold_build2_loc (loc, code, type,
12340 : 4723 : fold_convert_loc (loc, TREE_TYPE (arg1),
12341 : 4723 : tem), arg1);
12342 : : }
12343 : 1439750 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12344 : 1439750 : && integer_onep (TREE_OPERAND (arg01, 0)))
12345 : : {
12346 : 847 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12347 : 847 : arg00, TREE_OPERAND (arg01, 1));
12348 : 847 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12349 : 847 : build_one_cst (TREE_TYPE (arg0)));
12350 : 847 : return fold_build2_loc (loc, code, type,
12351 : 847 : fold_convert_loc (loc, TREE_TYPE (arg1),
12352 : 847 : tem), arg1);
12353 : : }
12354 : : }
12355 : :
12356 : : /* If this is a comparison of a field, we may be able to simplify it. */
12357 : 44198992 : if ((TREE_CODE (arg0) == COMPONENT_REF
12358 : 44198992 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12359 : : /* Handle the constant case even without -O
12360 : : to make sure the warnings are given. */
12361 : 4319343 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12362 : : {
12363 : 4029031 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12364 : 4029031 : if (t1)
12365 : : return t1;
12366 : : }
12367 : :
12368 : : /* Optimize comparisons of strlen vs zero to a compare of the
12369 : : first character of the string vs zero. To wit,
12370 : : strlen(ptr) == 0 => *ptr == 0
12371 : : strlen(ptr) != 0 => *ptr != 0
12372 : : Other cases should reduce to one of these two (or a constant)
12373 : : due to the return value of strlen being unsigned. */
12374 : 43458732 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12375 : : {
12376 : 2478935 : tree fndecl = get_callee_fndecl (arg0);
12377 : :
12378 : 2478935 : if (fndecl
12379 : 2478047 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12380 : 537 : && call_expr_nargs (arg0) == 1
12381 : 2479472 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12382 : : == POINTER_TYPE))
12383 : : {
12384 : 537 : tree ptrtype
12385 : 537 : = build_pointer_type (build_qualified_type (char_type_node,
12386 : : TYPE_QUAL_CONST));
12387 : 1074 : tree ptr = fold_convert_loc (loc, ptrtype,
12388 : 537 : CALL_EXPR_ARG (arg0, 0));
12389 : 537 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12390 : 537 : return fold_build2_loc (loc, code, type, iref,
12391 : 537 : build_int_cst (TREE_TYPE (iref), 0));
12392 : : }
12393 : : }
12394 : :
12395 : : /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12396 : : of X. Similarly fold (X >> C) == 0 into X >= 0. */
12397 : 43458195 : if (TREE_CODE (arg0) == RSHIFT_EXPR
12398 : 38238 : && integer_zerop (arg1)
12399 : 43470215 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12400 : : {
12401 : 9952 : tree arg00 = TREE_OPERAND (arg0, 0);
12402 : 9952 : tree arg01 = TREE_OPERAND (arg0, 1);
12403 : 9952 : tree itype = TREE_TYPE (arg00);
12404 : 9952 : if (wi::to_wide (arg01) == element_precision (itype) - 1)
12405 : : {
12406 : 910 : if (TYPE_UNSIGNED (itype))
12407 : : {
12408 : 829 : itype = signed_type_for (itype);
12409 : 829 : arg00 = fold_convert_loc (loc, itype, arg00);
12410 : : }
12411 : 1789 : return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12412 : 910 : type, arg00, build_zero_cst (itype));
12413 : : }
12414 : : }
12415 : :
12416 : : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12417 : : (X & C) == 0 when C is a single bit. */
12418 : 43457285 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12419 : 1635987 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12420 : 963 : && integer_zerop (arg1)
12421 : 43457850 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12422 : : {
12423 : 248 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12424 : 248 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12425 : 248 : TREE_OPERAND (arg0, 1));
12426 : 388 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12427 : : type, tem,
12428 : 248 : fold_convert_loc (loc, TREE_TYPE (arg0),
12429 : 248 : arg1));
12430 : : }
12431 : :
12432 : : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12433 : : constant C is a power of two, i.e. a single bit. */
12434 : 43457037 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12435 : 4662 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12436 : 0 : && integer_zerop (arg1)
12437 : 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12438 : 43457037 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12439 : 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12440 : : {
12441 : 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12442 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12443 : 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12444 : : }
12445 : :
12446 : : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12447 : : when is C is a power of two, i.e. a single bit. */
12448 : 43457037 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12449 : 1635739 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12450 : 11470 : && integer_zerop (arg1)
12451 : 11470 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12452 : 43465857 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12453 : 8820 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12454 : : {
12455 : 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12456 : 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12457 : 0 : arg000, TREE_OPERAND (arg0, 1));
12458 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12459 : 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12460 : : }
12461 : :
12462 : 43457037 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12463 : 4662 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12464 : : {
12465 : 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12466 : 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12467 : 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12468 : 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12469 : 482 : tree itype = TREE_TYPE (arg0);
12470 : :
12471 : : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12472 : : operand_equal_p guarantees no side-effects so we don't need
12473 : : to use omit_one_operand on Z. */
12474 : 482 : if (operand_equal_p (arg01, arg11, 0))
12475 : 8 : return fold_build2_loc (loc, code, type, arg00,
12476 : 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12477 : 8 : arg10));
12478 : 474 : if (operand_equal_p (arg01, arg10, 0))
12479 : 0 : return fold_build2_loc (loc, code, type, arg00,
12480 : 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12481 : 0 : arg11));
12482 : 474 : if (operand_equal_p (arg00, arg11, 0))
12483 : 0 : return fold_build2_loc (loc, code, type, arg01,
12484 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12485 : 0 : arg10));
12486 : 474 : if (operand_equal_p (arg00, arg10, 0))
12487 : 0 : return fold_build2_loc (loc, code, type, arg01,
12488 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12489 : 0 : arg11));
12490 : :
12491 : : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12492 : 474 : if (TREE_CODE (arg01) == INTEGER_CST
12493 : 8 : && TREE_CODE (arg11) == INTEGER_CST)
12494 : : {
12495 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12496 : : fold_convert_loc (loc, itype, arg11));
12497 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12498 : 8 : return fold_build2_loc (loc, code, type, tem,
12499 : 8 : fold_convert_loc (loc, itype, arg10));
12500 : : }
12501 : : }
12502 : :
12503 : : /* Attempt to simplify equality/inequality comparisons of complex
12504 : : values. Only lower the comparison if the result is known or
12505 : : can be simplified to a single scalar comparison. */
12506 : 43457021 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12507 : 43454494 : || TREE_CODE (arg0) == COMPLEX_CST)
12508 : 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12509 : 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12510 : : {
12511 : 1726 : tree real0, imag0, real1, imag1;
12512 : 1726 : tree rcond, icond;
12513 : :
12514 : 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12515 : : {
12516 : 1726 : real0 = TREE_OPERAND (arg0, 0);
12517 : 1726 : imag0 = TREE_OPERAND (arg0, 1);
12518 : : }
12519 : : else
12520 : : {
12521 : 0 : real0 = TREE_REALPART (arg0);
12522 : 0 : imag0 = TREE_IMAGPART (arg0);
12523 : : }
12524 : :
12525 : 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12526 : : {
12527 : 192 : real1 = TREE_OPERAND (arg1, 0);
12528 : 192 : imag1 = TREE_OPERAND (arg1, 1);
12529 : : }
12530 : : else
12531 : : {
12532 : 1534 : real1 = TREE_REALPART (arg1);
12533 : 1534 : imag1 = TREE_IMAGPART (arg1);
12534 : : }
12535 : :
12536 : 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12537 : 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12538 : : {
12539 : 11 : if (integer_zerop (rcond))
12540 : : {
12541 : 11 : if (code == EQ_EXPR)
12542 : 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12543 : 0 : imag0, imag1);
12544 : 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12545 : : }
12546 : : else
12547 : : {
12548 : 0 : if (code == NE_EXPR)
12549 : 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12550 : 0 : imag0, imag1);
12551 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12552 : : }
12553 : : }
12554 : :
12555 : 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12556 : 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12557 : : {
12558 : 9 : if (integer_zerop (icond))
12559 : : {
12560 : 7 : if (code == EQ_EXPR)
12561 : 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12562 : 1 : real0, real1);
12563 : 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12564 : : }
12565 : : else
12566 : : {
12567 : 2 : if (code == NE_EXPR)
12568 : 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12569 : 1 : real0, real1);
12570 : 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12571 : : }
12572 : : }
12573 : : }
12574 : :
12575 : : return NULL_TREE;
12576 : :
12577 : 37290072 : case LT_EXPR:
12578 : 37290072 : case GT_EXPR:
12579 : 37290072 : case LE_EXPR:
12580 : 37290072 : case GE_EXPR:
12581 : 37290072 : tem = fold_comparison (loc, code, type, op0, op1);
12582 : 37290072 : if (tem != NULL_TREE)
12583 : : return tem;
12584 : :
12585 : : /* Transform comparisons of the form X +- C CMP X. */
12586 : 36420052 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12587 : 4568285 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12588 : 49887 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12589 : 36420070 : && !HONOR_SNANS (arg0))
12590 : : {
12591 : 16 : tree arg01 = TREE_OPERAND (arg0, 1);
12592 : 16 : enum tree_code code0 = TREE_CODE (arg0);
12593 : 16 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12594 : :
12595 : : /* (X - c) > X becomes false. */
12596 : 16 : if (code == GT_EXPR
12597 : 7 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12598 : 3 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12599 : 4 : return constant_boolean_node (0, type);
12600 : :
12601 : : /* Likewise (X + c) < X becomes false. */
12602 : 12 : if (code == LT_EXPR
12603 : 2 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12604 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12605 : 2 : return constant_boolean_node (0, type);
12606 : :
12607 : : /* Convert (X - c) <= X to true. */
12608 : 10 : if (!HONOR_NANS (arg1)
12609 : 6 : && code == LE_EXPR
12610 : 14 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12611 : 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12612 : 4 : return constant_boolean_node (1, type);
12613 : :
12614 : : /* Convert (X + c) >= X to true. */
12615 : 6 : if (!HONOR_NANS (arg1)
12616 : 2 : && code == GE_EXPR
12617 : 8 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12618 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12619 : 2 : return constant_boolean_node (1, type);
12620 : : }
12621 : :
12622 : : /* If we are comparing an ABS_EXPR with a constant, we can
12623 : : convert all the cases into explicit comparisons, but they may
12624 : : well not be faster than doing the ABS and one comparison.
12625 : : But ABS (X) <= C is a range comparison, which becomes a subtraction
12626 : : and a comparison, and is probably faster. */
12627 : 36420040 : if (code == LE_EXPR
12628 : 6983753 : && TREE_CODE (arg1) == INTEGER_CST
12629 : 4909262 : && TREE_CODE (arg0) == ABS_EXPR
12630 : 703 : && ! TREE_SIDE_EFFECTS (arg0)
12631 : 703 : && (tem = negate_expr (arg1)) != 0
12632 : 703 : && TREE_CODE (tem) == INTEGER_CST
12633 : 36420743 : && !TREE_OVERFLOW (tem))
12634 : 1406 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12635 : : build2 (GE_EXPR, type,
12636 : 703 : TREE_OPERAND (arg0, 0), tem),
12637 : : build2 (LE_EXPR, type,
12638 : 1406 : TREE_OPERAND (arg0, 0), arg1));
12639 : :
12640 : : /* Convert ABS_EXPR<x> >= 0 to true. */
12641 : 36419337 : strict_overflow_p = false;
12642 : 36419337 : if (code == GE_EXPR
12643 : 3839624 : && (integer_zerop (arg1)
12644 : 2973240 : || (! HONOR_NANS (arg0)
12645 : 2356067 : && real_zerop (arg1)))
12646 : 37285934 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12647 : : {
12648 : 1174 : if (strict_overflow_p)
12649 : 6 : fold_overflow_warning (("assuming signed overflow does not occur "
12650 : : "when simplifying comparison of "
12651 : : "absolute value and zero"),
12652 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12653 : 1174 : return omit_one_operand_loc (loc, type,
12654 : : constant_boolean_node (true, type),
12655 : 1174 : arg0);
12656 : : }
12657 : :
12658 : : /* Convert ABS_EXPR<x> < 0 to false. */
12659 : 36418163 : strict_overflow_p = false;
12660 : 36418163 : if (code == LT_EXPR
12661 : 12264128 : && (integer_zerop (arg1) || real_zerop (arg1))
12662 : 39294158 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12663 : : {
12664 : 3069 : if (strict_overflow_p)
12665 : 193 : fold_overflow_warning (("assuming signed overflow does not occur "
12666 : : "when simplifying comparison of "
12667 : : "absolute value and zero"),
12668 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12669 : 3069 : return omit_one_operand_loc (loc, type,
12670 : : constant_boolean_node (false, type),
12671 : 3069 : arg0);
12672 : : }
12673 : :
12674 : : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12675 : : and similarly for >= into !=. */
12676 : 36415094 : if ((code == LT_EXPR || code == GE_EXPR)
12677 : 16099509 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12678 : 4922169 : && TREE_CODE (arg1) == LSHIFT_EXPR
12679 : 36416619 : && integer_onep (TREE_OPERAND (arg1, 0)))
12680 : 4070 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12681 : 1361 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12682 : 1361 : TREE_OPERAND (arg1, 1)),
12683 : 2722 : build_zero_cst (TREE_TYPE (arg0)));
12684 : :
12685 : : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12686 : : otherwise Y might be >= # of bits in X's type and thus e.g.
12687 : : (unsigned char) (1 << Y) for Y 15 might be 0.
12688 : : If the cast is widening, then 1 << Y should have unsigned type,
12689 : : otherwise if Y is number of bits in the signed shift type minus 1,
12690 : : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12691 : : 31 might be 0xffffffff80000000. */
12692 : 36413733 : if ((code == LT_EXPR || code == GE_EXPR)
12693 : 16098148 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12694 : 5454213 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12695 : 10665857 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12696 : 3655264 : && CONVERT_EXPR_P (arg1)
12697 : 1060820 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12698 : 42 : && (element_precision (TREE_TYPE (arg1))
12699 : 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12700 : 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12701 : 14 : || (element_precision (TREE_TYPE (arg1))
12702 : 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12703 : 36413740 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12704 : : {
12705 : 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12706 : 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12707 : 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12708 : 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12709 : 14 : build_zero_cst (TREE_TYPE (arg0)));
12710 : : }
12711 : :
12712 : : return NULL_TREE;
12713 : :
12714 : 5474066 : case UNORDERED_EXPR:
12715 : 5474066 : case ORDERED_EXPR:
12716 : 5474066 : case UNLT_EXPR:
12717 : 5474066 : case UNLE_EXPR:
12718 : 5474066 : case UNGT_EXPR:
12719 : 5474066 : case UNGE_EXPR:
12720 : 5474066 : case UNEQ_EXPR:
12721 : 5474066 : case LTGT_EXPR:
12722 : : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12723 : 5474066 : {
12724 : 5474066 : tree targ0 = strip_float_extensions (arg0);
12725 : 5474066 : tree targ1 = strip_float_extensions (arg1);
12726 : 5474066 : tree newtype = TREE_TYPE (targ0);
12727 : :
12728 : 5474066 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12729 : 1289 : newtype = TREE_TYPE (targ1);
12730 : :
12731 : 5474066 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12732 : 5474066 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12733 : 328 : return fold_build2_loc (loc, code, type,
12734 : : fold_convert_loc (loc, newtype, targ0),
12735 : 328 : fold_convert_loc (loc, newtype, targ1));
12736 : : }
12737 : :
12738 : : return NULL_TREE;
12739 : :
12740 : 5539073 : case COMPOUND_EXPR:
12741 : : /* When pedantic, a compound expression can be neither an lvalue
12742 : : nor an integer constant expression. */
12743 : 5539073 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12744 : : return NULL_TREE;
12745 : : /* Don't let (0, 0) be null pointer constant. */
12746 : 451007 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12747 : 451007 : : fold_convert_loc (loc, type, arg1);
12748 : : return tem;
12749 : :
12750 : : default:
12751 : : return NULL_TREE;
12752 : : } /* switch (code) */
12753 : : }
12754 : :
12755 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12756 : : ((A & N) + B) & M -> (A + B) & M
12757 : : Similarly if (N & M) == 0,
12758 : : ((A | N) + B) & M -> (A + B) & M
12759 : : and for - instead of + (or unary - instead of +)
12760 : : and/or ^ instead of |.
12761 : : If B is constant and (B & M) == 0, fold into A & M.
12762 : :
12763 : : This function is a helper for match.pd patterns. Return non-NULL
12764 : : type in which the simplified operation should be performed only
12765 : : if any optimization is possible.
12766 : :
12767 : : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12768 : : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12769 : : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12770 : : +/-. */
12771 : : tree
12772 : 1201507 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12773 : : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12774 : : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12775 : : tree *pmop)
12776 : : {
12777 : 1201507 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12778 : 1201507 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12779 : 1201507 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12780 : 2403014 : if (~cst1 == 0
12781 : 3600159 : || (cst1 & (cst1 + 1)) != 0
12782 : 1031668 : || !INTEGRAL_TYPE_P (type)
12783 : 1031668 : || (!TYPE_OVERFLOW_WRAPS (type)
12784 : 39769 : && TREE_CODE (type) != INTEGER_TYPE)
12785 : 4464169 : || (wi::max_value (type) & cst1) != cst1)
12786 : : return NULL_TREE;
12787 : :
12788 : 1031668 : enum tree_code codes[2] = { code00, code01 };
12789 : 1031668 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12790 : 1031668 : int which = 0;
12791 : 1031668 : wide_int cst0;
12792 : :
12793 : : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12794 : : arg1 (M) is == (1LL << cst) - 1.
12795 : : Store C into PMOP[0] and D into PMOP[1]. */
12796 : 1031668 : pmop[0] = arg00;
12797 : 1031668 : pmop[1] = arg01;
12798 : 1031668 : which = code != NEGATE_EXPR;
12799 : :
12800 : 3094108 : for (; which >= 0; which--)
12801 : 2062440 : switch (codes[which])
12802 : : {
12803 : 20543 : case BIT_AND_EXPR:
12804 : 20543 : case BIT_IOR_EXPR:
12805 : 20543 : case BIT_XOR_EXPR:
12806 : 20543 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12807 : 20543 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12808 : 20543 : if (codes[which] == BIT_AND_EXPR)
12809 : : {
12810 : 20429 : if (cst0 != cst1)
12811 : : break;
12812 : : }
12813 : 114 : else if (cst0 != 0)
12814 : : break;
12815 : : /* If C or D is of the form (A & N) where
12816 : : (N & M) == M, or of the form (A | N) or
12817 : : (A ^ N) where (N & M) == 0, replace it with A. */
12818 : 19002 : pmop[which] = arg0xx[2 * which];
12819 : 19002 : break;
12820 : 2041897 : case ERROR_MARK:
12821 : 2041897 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12822 : : break;
12823 : : /* If C or D is a N where (N & M) == 0, it can be
12824 : : omitted (replaced with 0). */
12825 : 854154 : if ((code == PLUS_EXPR
12826 : 198331 : || (code == MINUS_EXPR && which == 0))
12827 : 625408 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12828 : 135218 : pmop[which] = build_int_cst (type, 0);
12829 : : /* Similarly, with C - N where (-N & M) == 0. */
12830 : 854154 : if (code == MINUS_EXPR
12831 : 427077 : && which == 1
12832 : 618164 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12833 : 181717 : pmop[which] = build_int_cst (type, 0);
12834 : : break;
12835 : 0 : default:
12836 : 0 : gcc_unreachable ();
12837 : : }
12838 : :
12839 : : /* Only build anything new if we optimized one or both arguments above. */
12840 : 1031668 : if (pmop[0] == arg00 && pmop[1] == arg01)
12841 : : return NULL_TREE;
12842 : :
12843 : 335236 : if (TYPE_OVERFLOW_WRAPS (type))
12844 : : return type;
12845 : : else
12846 : 2256 : return unsigned_type_for (type);
12847 : 1031668 : }
12848 : :
12849 : : /* Used by contains_label_[p1]. */
12850 : :
12851 : : struct contains_label_data
12852 : : {
12853 : : hash_set<tree> *pset;
12854 : : bool inside_switch_p;
12855 : : };
12856 : :
12857 : : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12858 : : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12859 : : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12860 : :
12861 : : static tree
12862 : 2381812 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12863 : : {
12864 : 2381812 : contains_label_data *d = (contains_label_data *) data;
12865 : 2381812 : switch (TREE_CODE (*tp))
12866 : : {
12867 : : case LABEL_EXPR:
12868 : : return *tp;
12869 : :
12870 : 0 : case CASE_LABEL_EXPR:
12871 : 0 : if (!d->inside_switch_p)
12872 : : return *tp;
12873 : : return NULL_TREE;
12874 : :
12875 : 0 : case SWITCH_EXPR:
12876 : 0 : if (!d->inside_switch_p)
12877 : : {
12878 : 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12879 : 0 : return *tp;
12880 : 0 : d->inside_switch_p = true;
12881 : 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12882 : 0 : return *tp;
12883 : 0 : d->inside_switch_p = false;
12884 : 0 : *walk_subtrees = 0;
12885 : : }
12886 : : return NULL_TREE;
12887 : :
12888 : 5897 : case GOTO_EXPR:
12889 : 5897 : *walk_subtrees = 0;
12890 : 5897 : return NULL_TREE;
12891 : :
12892 : : default:
12893 : : return NULL_TREE;
12894 : : }
12895 : : }
12896 : :
12897 : : /* Return whether the sub-tree ST contains a label which is accessible from
12898 : : outside the sub-tree. */
12899 : :
12900 : : static bool
12901 : 211859 : contains_label_p (tree st)
12902 : : {
12903 : 211859 : hash_set<tree> pset;
12904 : 211859 : contains_label_data data = { &pset, false };
12905 : 211859 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12906 : 211859 : }
12907 : :
12908 : : /* Fold a ternary expression of code CODE and type TYPE with operands
12909 : : OP0, OP1, and OP2. Return the folded expression if folding is
12910 : : successful. Otherwise, return NULL_TREE. */
12911 : :
12912 : : tree
12913 : 26672561 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12914 : : tree op0, tree op1, tree op2)
12915 : : {
12916 : 26672561 : tree tem;
12917 : 26672561 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12918 : 26672561 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12919 : :
12920 : 26672561 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12921 : : && TREE_CODE_LENGTH (code) == 3);
12922 : :
12923 : : /* If this is a commutative operation, and OP0 is a constant, move it
12924 : : to OP1 to reduce the number of tests below. */
12925 : 26672561 : if (commutative_ternary_tree_code (code)
12926 : 26672561 : && tree_swap_operands_p (op0, op1))
12927 : 41 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12928 : :
12929 : 26672520 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12930 : 26672520 : if (tem)
12931 : : return tem;
12932 : :
12933 : : /* Strip any conversions that don't change the mode. This is safe
12934 : : for every expression, except for a comparison expression because
12935 : : its signedness is derived from its operands. So, in the latter
12936 : : case, only strip conversions that don't change the signedness.
12937 : :
12938 : : Note that this is done as an internal manipulation within the
12939 : : constant folder, in order to find the simplest representation of
12940 : : the arguments so that their form can be studied. In any cases,
12941 : : the appropriate type conversions should be put back in the tree
12942 : : that will get out of the constant folder. */
12943 : 25759566 : if (op0)
12944 : : {
12945 : 25695457 : arg0 = op0;
12946 : 25695457 : STRIP_NOPS (arg0);
12947 : : }
12948 : :
12949 : 25759566 : if (op1)
12950 : : {
12951 : 25759566 : arg1 = op1;
12952 : 25759566 : STRIP_NOPS (arg1);
12953 : : }
12954 : :
12955 : 25759566 : if (op2)
12956 : : {
12957 : 12563963 : arg2 = op2;
12958 : 12563963 : STRIP_NOPS (arg2);
12959 : : }
12960 : :
12961 : 25759566 : switch (code)
12962 : : {
12963 : 13195132 : case COMPONENT_REF:
12964 : 13195132 : if (TREE_CODE (arg0) == CONSTRUCTOR
12965 : 13195132 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12966 : : {
12967 : : unsigned HOST_WIDE_INT idx;
12968 : : tree field, value;
12969 : 862 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12970 : 671 : if (field == arg1)
12971 : : return value;
12972 : : }
12973 : : return NULL_TREE;
12974 : :
12975 : 10516160 : case COND_EXPR:
12976 : 10516160 : case VEC_COND_EXPR:
12977 : : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12978 : : so all simple results must be passed through pedantic_non_lvalue. */
12979 : 10516160 : if (TREE_CODE (arg0) == INTEGER_CST)
12980 : : {
12981 : 340553 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12982 : 340553 : tem = integer_zerop (arg0) ? op2 : op1;
12983 : : /* Only optimize constant conditions when the selected branch
12984 : : has the same type as the COND_EXPR. This avoids optimizing
12985 : : away "c ? x : throw", where the throw has a void type.
12986 : : Avoid throwing away that operand which contains label. */
12987 : 340553 : if ((!TREE_SIDE_EFFECTS (unused_op)
12988 : 211859 : || !contains_label_p (unused_op))
12989 : 548062 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12990 : 260181 : || VOID_TYPE_P (type)))
12991 : 327005 : return protected_set_expr_location_unshare (tem, loc);
12992 : 13548 : return NULL_TREE;
12993 : : }
12994 : 10175607 : else if (TREE_CODE (arg0) == VECTOR_CST)
12995 : : {
12996 : 1526 : unsigned HOST_WIDE_INT nelts;
12997 : 1526 : if ((TREE_CODE (arg1) == VECTOR_CST
12998 : 267 : || TREE_CODE (arg1) == CONSTRUCTOR)
12999 : 1259 : && (TREE_CODE (arg2) == VECTOR_CST
13000 : 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
13001 : 3052 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
13002 : : {
13003 : 1259 : vec_perm_builder sel (nelts, nelts, 1);
13004 : 13603 : for (unsigned int i = 0; i < nelts; i++)
13005 : : {
13006 : 12344 : tree val = VECTOR_CST_ELT (arg0, i);
13007 : 12344 : if (integer_all_onesp (val))
13008 : 6635 : sel.quick_push (i);
13009 : 5709 : else if (integer_zerop (val))
13010 : 5709 : sel.quick_push (nelts + i);
13011 : : else /* Currently unreachable. */
13012 : 1227 : return NULL_TREE;
13013 : : }
13014 : 1259 : vec_perm_indices indices (sel, 2, nelts);
13015 : 1259 : tree t = fold_vec_perm (type, arg1, arg2, indices);
13016 : 1259 : if (t != NULL_TREE)
13017 : 1227 : return t;
13018 : 2486 : }
13019 : : }
13020 : :
13021 : : /* If we have A op B ? A : C, we may be able to convert this to a
13022 : : simpler expression, depending on the operation and the values
13023 : : of B and C. Signed zeros prevent all of these transformations,
13024 : : for reasons given above each one.
13025 : :
13026 : : Also try swapping the arguments and inverting the conditional. */
13027 : 10174380 : if (COMPARISON_CLASS_P (arg0)
13028 : 8535484 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
13029 : 10294403 : && !HONOR_SIGNED_ZEROS (op1))
13030 : : {
13031 : 109640 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
13032 : 109640 : TREE_OPERAND (arg0, 0),
13033 : 109640 : TREE_OPERAND (arg0, 1),
13034 : : op1, op2);
13035 : 109640 : if (tem)
13036 : : return tem;
13037 : : }
13038 : :
13039 : 10167899 : if (COMPARISON_CLASS_P (arg0)
13040 : 8529003 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
13041 : 10590806 : && !HONOR_SIGNED_ZEROS (op2))
13042 : : {
13043 : 332764 : enum tree_code comp_code = TREE_CODE (arg0);
13044 : 332764 : tree arg00 = TREE_OPERAND (arg0, 0);
13045 : 332764 : tree arg01 = TREE_OPERAND (arg0, 1);
13046 : 332764 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
13047 : 332764 : if (comp_code != ERROR_MARK)
13048 : 332764 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
13049 : : arg00,
13050 : : arg01,
13051 : : op2, op1);
13052 : 332764 : if (tem)
13053 : : return tem;
13054 : : }
13055 : :
13056 : : /* If the second operand is simpler than the third, swap them
13057 : : since that produces better jump optimization results. */
13058 : 9926052 : if (truth_value_p (TREE_CODE (arg0))
13059 : 9926052 : && tree_swap_operands_p (op1, op2))
13060 : : {
13061 : 1689471 : location_t loc0 = expr_location_or (arg0, loc);
13062 : : /* See if this can be inverted. If it can't, possibly because
13063 : : it was a floating-point inequality comparison, don't do
13064 : : anything. */
13065 : 1689471 : tem = fold_invert_truthvalue (loc0, arg0);
13066 : 1689471 : if (tem)
13067 : 1123825 : return fold_build3_loc (loc, code, type, tem, op2, op1);
13068 : : }
13069 : :
13070 : : /* Convert A ? 1 : 0 to simply A. */
13071 : 8802227 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13072 : 8494730 : : (integer_onep (op1)
13073 : 384844 : && !VECTOR_TYPE_P (type)))
13074 : 581494 : && integer_zerop (op2)
13075 : : /* If we try to convert OP0 to our type, the
13076 : : call to fold will try to move the conversion inside
13077 : : a COND, which will recurse. In that case, the COND_EXPR
13078 : : is probably the best choice, so leave it alone. */
13079 : 9679551 : && type == TREE_TYPE (arg0))
13080 : 30214 : return protected_set_expr_location_unshare (arg0, loc);
13081 : :
13082 : : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13083 : : over COND_EXPR in cases such as floating point comparisons. */
13084 : 8772013 : if (integer_zerop (op1)
13085 : 247995 : && code == COND_EXPR
13086 : 246272 : && integer_onep (op2)
13087 : 33291 : && !VECTOR_TYPE_P (type)
13088 : 8805304 : && truth_value_p (TREE_CODE (arg0)))
13089 : 31515 : return fold_convert_loc (loc, type,
13090 : 31515 : invert_truthvalue_loc (loc, arg0));
13091 : :
13092 : : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13093 : 8740498 : if (TREE_CODE (arg0) == LT_EXPR
13094 : 970815 : && integer_zerop (TREE_OPERAND (arg0, 1))
13095 : 15701 : && integer_zerop (op2)
13096 : 8741419 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13097 : : {
13098 : : /* sign_bit_p looks through both zero and sign extensions,
13099 : : but for this optimization only sign extensions are
13100 : : usable. */
13101 : 56 : tree tem2 = TREE_OPERAND (arg0, 0);
13102 : 56 : while (tem != tem2)
13103 : : {
13104 : 0 : if (TREE_CODE (tem2) != NOP_EXPR
13105 : 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13106 : : {
13107 : : tem = NULL_TREE;
13108 : : break;
13109 : : }
13110 : 0 : tem2 = TREE_OPERAND (tem2, 0);
13111 : : }
13112 : : /* sign_bit_p only checks ARG1 bits within A's precision.
13113 : : If <sign bit of A> has wider type than A, bits outside
13114 : : of A's precision in <sign bit of A> need to be checked.
13115 : : If they are all 0, this optimization needs to be done
13116 : : in unsigned A's type, if they are all 1 in signed A's type,
13117 : : otherwise this can't be done. */
13118 : 56 : if (tem
13119 : 56 : && TYPE_PRECISION (TREE_TYPE (tem))
13120 : 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
13121 : 112 : && TYPE_PRECISION (TREE_TYPE (tem))
13122 : 56 : < TYPE_PRECISION (type))
13123 : : {
13124 : 56 : int inner_width, outer_width;
13125 : 56 : tree tem_type;
13126 : :
13127 : 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13128 : 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13129 : 56 : if (outer_width > TYPE_PRECISION (type))
13130 : 0 : outer_width = TYPE_PRECISION (type);
13131 : :
13132 : 56 : wide_int mask = wi::shifted_mask
13133 : 56 : (inner_width, outer_width - inner_width, false,
13134 : 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13135 : :
13136 : 56 : wide_int common = mask & wi::to_wide (arg1);
13137 : 56 : if (common == mask)
13138 : : {
13139 : 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13140 : 28 : tem = fold_convert_loc (loc, tem_type, tem);
13141 : : }
13142 : 28 : else if (common == 0)
13143 : : {
13144 : 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13145 : 0 : tem = fold_convert_loc (loc, tem_type, tem);
13146 : : }
13147 : : else
13148 : : tem = NULL;
13149 : 56 : }
13150 : :
13151 : 56 : if (tem)
13152 : 28 : return
13153 : 56 : fold_convert_loc (loc, type,
13154 : : fold_build2_loc (loc, BIT_AND_EXPR,
13155 : 28 : TREE_TYPE (tem), tem,
13156 : : fold_convert_loc (loc,
13157 : 28 : TREE_TYPE (tem),
13158 : 28 : arg1)));
13159 : : }
13160 : :
13161 : : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13162 : : already handled above. */
13163 : 8740470 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13164 : 325 : && integer_onep (TREE_OPERAND (arg0, 1))
13165 : 3 : && integer_zerop (op2)
13166 : 8740470 : && integer_pow2p (arg1))
13167 : : {
13168 : 0 : tree tem = TREE_OPERAND (arg0, 0);
13169 : 0 : STRIP_NOPS (tem);
13170 : 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13171 : 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13172 : 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13173 : 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13174 : 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13175 : : fold_convert_loc (loc, type,
13176 : 0 : TREE_OPERAND (tem, 0)),
13177 : 0 : op1);
13178 : : }
13179 : :
13180 : : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13181 : : is probably obsolete because the first operand should be a
13182 : : truth value (that's why we have the two cases above), but let's
13183 : : leave it in until we can confirm this for all front-ends. */
13184 : 8740470 : if (integer_zerop (op2)
13185 : 1877521 : && TREE_CODE (arg0) == NE_EXPR
13186 : 510428 : && integer_zerop (TREE_OPERAND (arg0, 1))
13187 : 279766 : && integer_pow2p (arg1)
13188 : 31079 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13189 : 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13190 : : arg1, OEP_ONLY_CONST)
13191 : : /* operand_equal_p compares just value, not precision, so e.g.
13192 : : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13193 : : second operand 32-bit -128, which is not a power of two (or vice
13194 : : versa. */
13195 : 8740470 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13196 : 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13197 : :
13198 : : /* Disable the transformations below for vectors, since
13199 : : fold_binary_op_with_conditional_arg may undo them immediately,
13200 : : yielding an infinite loop. */
13201 : 8740470 : if (code == VEC_COND_EXPR)
13202 : : return NULL_TREE;
13203 : :
13204 : : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13205 : 8432973 : if (integer_zerop (op2)
13206 : 1621617 : && truth_value_p (TREE_CODE (arg0))
13207 : 1502483 : && truth_value_p (TREE_CODE (arg1))
13208 : 8463492 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13209 : 30519 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13210 : : : TRUTH_ANDIF_EXPR,
13211 : 30519 : type, fold_convert_loc (loc, type, arg0), op1);
13212 : :
13213 : : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13214 : 8402454 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13215 : 436304 : && truth_value_p (TREE_CODE (arg0))
13216 : 300794 : && truth_value_p (TREE_CODE (arg1))
13217 : 8436652 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13218 : : {
13219 : 34198 : location_t loc0 = expr_location_or (arg0, loc);
13220 : : /* Only perform transformation if ARG0 is easily inverted. */
13221 : 34198 : tem = fold_invert_truthvalue (loc0, arg0);
13222 : 34198 : if (tem)
13223 : 33934 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13224 : : ? BIT_IOR_EXPR
13225 : : : TRUTH_ORIF_EXPR,
13226 : : type, fold_convert_loc (loc, type, tem),
13227 : 33934 : op1);
13228 : : }
13229 : :
13230 : : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13231 : 8368520 : if (integer_zerop (arg1)
13232 : 214836 : && truth_value_p (TREE_CODE (arg0))
13233 : 47943 : && truth_value_p (TREE_CODE (op2))
13234 : 8368548 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13235 : : {
13236 : 28 : location_t loc0 = expr_location_or (arg0, loc);
13237 : : /* Only perform transformation if ARG0 is easily inverted. */
13238 : 28 : tem = fold_invert_truthvalue (loc0, arg0);
13239 : 28 : if (tem)
13240 : 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13241 : : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13242 : : type, fold_convert_loc (loc, type, tem),
13243 : 0 : op2);
13244 : : }
13245 : :
13246 : : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13247 : 8368520 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13248 : 354630 : && truth_value_p (TREE_CODE (arg0))
13249 : 275744 : && truth_value_p (TREE_CODE (op2))
13250 : 8368706 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13251 : 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13252 : : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13253 : 186 : type, fold_convert_loc (loc, type, arg0), op2);
13254 : :
13255 : : return NULL_TREE;
13256 : :
13257 : 0 : case CALL_EXPR:
13258 : : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13259 : : of fold_ternary on them. */
13260 : 0 : gcc_unreachable ();
13261 : :
13262 : 636665 : case BIT_FIELD_REF:
13263 : 636665 : if (TREE_CODE (arg0) == VECTOR_CST
13264 : 28154 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13265 : 1780 : || (VECTOR_TYPE_P (type)
13266 : 1084 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13267 : 27438 : && tree_fits_uhwi_p (op1)
13268 : 664103 : && tree_fits_uhwi_p (op2))
13269 : : {
13270 : 27438 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13271 : 27438 : unsigned HOST_WIDE_INT width
13272 : 27438 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13273 : 27438 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13274 : 27438 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13275 : 27438 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13276 : :
13277 : 27438 : if (n != 0
13278 : 27438 : && (idx % width) == 0
13279 : 27438 : && (n % width) == 0
13280 : 54876 : && known_le ((idx + n) / width,
13281 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13282 : : {
13283 : 27438 : idx = idx / width;
13284 : 27438 : n = n / width;
13285 : :
13286 : 27438 : if (TREE_CODE (arg0) == VECTOR_CST)
13287 : : {
13288 : 27438 : if (n == 1)
13289 : : {
13290 : 26378 : tem = VECTOR_CST_ELT (arg0, idx);
13291 : 26378 : if (VECTOR_TYPE_P (type))
13292 : 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13293 : 26378 : return tem;
13294 : : }
13295 : :
13296 : 1060 : tree_vector_builder vals (type, n, 1);
13297 : 6548 : for (unsigned i = 0; i < n; ++i)
13298 : 5488 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13299 : 1060 : return vals.build ();
13300 : 1060 : }
13301 : : }
13302 : : }
13303 : :
13304 : : /* On constants we can use native encode/interpret to constant
13305 : : fold (nearly) all BIT_FIELD_REFs. */
13306 : 609227 : if (CONSTANT_CLASS_P (arg0)
13307 : 1509 : && can_native_interpret_type_p (type)
13308 : : && BITS_PER_UNIT == 8
13309 : 1509 : && tree_fits_uhwi_p (op1)
13310 : 610736 : && tree_fits_uhwi_p (op2))
13311 : : {
13312 : 1509 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13313 : 1509 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13314 : : /* Limit us to a reasonable amount of work. To relax the
13315 : : other limitations we need bit-shifting of the buffer
13316 : : and rounding up the size. */
13317 : 1509 : if (bitpos % BITS_PER_UNIT == 0
13318 : 1509 : && bitsize % BITS_PER_UNIT == 0
13319 : 1509 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13320 : : {
13321 : 1509 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13322 : 1509 : unsigned HOST_WIDE_INT len
13323 : 1509 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13324 : 1509 : bitpos / BITS_PER_UNIT);
13325 : 1509 : if (len > 0
13326 : 1509 : && len * BITS_PER_UNIT >= bitsize)
13327 : : {
13328 : 1509 : tree v = native_interpret_expr (type, b,
13329 : : bitsize / BITS_PER_UNIT);
13330 : 1509 : if (v)
13331 : 1503 : return v;
13332 : : }
13333 : : }
13334 : : }
13335 : :
13336 : : return NULL_TREE;
13337 : :
13338 : 696752 : case VEC_PERM_EXPR:
13339 : : /* Perform constant folding of BIT_INSERT_EXPR. */
13340 : 696752 : if (TREE_CODE (arg2) == VECTOR_CST
13341 : 685523 : && TREE_CODE (op0) == VECTOR_CST
13342 : 15208 : && TREE_CODE (op1) == VECTOR_CST)
13343 : : {
13344 : : /* Build a vector of integers from the tree mask. */
13345 : 3915 : vec_perm_builder builder;
13346 : 3915 : if (!tree_to_vec_perm_builder (&builder, arg2))
13347 : : return NULL_TREE;
13348 : :
13349 : : /* Create a vec_perm_indices for the integer vector. */
13350 : 3915 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13351 : 3915 : bool single_arg = (op0 == op1);
13352 : 7830 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13353 : 3915 : return fold_vec_perm (type, op0, op1, sel);
13354 : 7830 : }
13355 : : return NULL_TREE;
13356 : :
13357 : 13691 : case BIT_INSERT_EXPR:
13358 : : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13359 : 13691 : if (TREE_CODE (arg0) == INTEGER_CST
13360 : 14 : && TREE_CODE (arg1) == INTEGER_CST)
13361 : : {
13362 : 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13363 : 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13364 : 2 : if (BYTES_BIG_ENDIAN)
13365 : : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13366 : 2 : wide_int tem = (wi::to_wide (arg0)
13367 : 4 : & wi::shifted_mask (bitpos, bitsize, true,
13368 : 4 : TYPE_PRECISION (type)));
13369 : 2 : wide_int tem2
13370 : 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13371 : 2 : bitsize), bitpos);
13372 : 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13373 : 2 : }
13374 : 13689 : else if (TREE_CODE (arg0) == VECTOR_CST
13375 : 906 : && CONSTANT_CLASS_P (arg1)
13376 : 13991 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13377 : 302 : TREE_TYPE (arg1)))
13378 : : {
13379 : 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13380 : 302 : unsigned HOST_WIDE_INT elsize
13381 : 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13382 : 302 : if (bitpos % elsize == 0)
13383 : : {
13384 : 302 : unsigned k = bitpos / elsize;
13385 : 302 : unsigned HOST_WIDE_INT nelts;
13386 : 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13387 : 26672561 : return arg0;
13388 : 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13389 : : {
13390 : 290 : tree_vector_builder elts (type, nelts, 1);
13391 : 290 : elts.quick_grow (nelts);
13392 : 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13393 : 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13394 : 290 : return elts.build ();
13395 : 290 : }
13396 : : }
13397 : : }
13398 : : return NULL_TREE;
13399 : :
13400 : : default:
13401 : : return NULL_TREE;
13402 : : } /* switch (code) */
13403 : : }
13404 : :
13405 : : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13406 : : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13407 : : constructor element index of the value returned. If the element is
13408 : : not found NULL_TREE is returned and *CTOR_IDX is updated to
13409 : : the index of the element after the ACCESS_INDEX position (which
13410 : : may be outside of the CTOR array). */
13411 : :
13412 : : tree
13413 : 721995 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13414 : : unsigned *ctor_idx)
13415 : : {
13416 : 721995 : tree index_type = NULL_TREE;
13417 : 721995 : signop index_sgn = UNSIGNED;
13418 : 721995 : offset_int low_bound = 0;
13419 : :
13420 : 721995 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13421 : : {
13422 : 721995 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13423 : 721995 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13424 : : {
13425 : : /* Static constructors for variably sized objects makes no sense. */
13426 : 721995 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13427 : 721995 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13428 : : /* ??? When it is obvious that the range is signed, treat it so. */
13429 : 721995 : if (TYPE_UNSIGNED (index_type)
13430 : 375143 : && TYPE_MAX_VALUE (domain_type)
13431 : 1097104 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13432 : 375109 : TYPE_MIN_VALUE (domain_type)))
13433 : : {
13434 : 0 : index_sgn = SIGNED;
13435 : 0 : low_bound
13436 : 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13437 : : SIGNED);
13438 : : }
13439 : : else
13440 : : {
13441 : 721995 : index_sgn = TYPE_SIGN (index_type);
13442 : 721995 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13443 : : }
13444 : : }
13445 : : }
13446 : :
13447 : 721995 : if (index_type)
13448 : 721995 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13449 : : index_sgn);
13450 : :
13451 : 721995 : offset_int index = low_bound;
13452 : 721995 : if (index_type)
13453 : 721995 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13454 : :
13455 : 721995 : offset_int max_index = index;
13456 : 721995 : unsigned cnt;
13457 : 721995 : tree cfield, cval;
13458 : 721995 : bool first_p = true;
13459 : :
13460 : 14580321 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13461 : : {
13462 : : /* Array constructor might explicitly set index, or specify a range,
13463 : : or leave index NULL meaning that it is next index after previous
13464 : : one. */
13465 : 14578845 : if (cfield)
13466 : : {
13467 : 6396246 : if (TREE_CODE (cfield) == INTEGER_CST)
13468 : 12791048 : max_index = index
13469 : 6395524 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13470 : : else
13471 : : {
13472 : 722 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13473 : 722 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13474 : : index_sgn);
13475 : 722 : max_index
13476 : 722 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13477 : : index_sgn);
13478 : 722 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13479 : : }
13480 : : }
13481 : 8182599 : else if (!first_p)
13482 : : {
13483 : 7916519 : index = max_index + 1;
13484 : 7916519 : if (index_type)
13485 : 7916519 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13486 : 7916519 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13487 : 7916519 : max_index = index;
13488 : : }
13489 : : else
13490 : : first_p = false;
13491 : :
13492 : 14578845 : if (TREE_CODE (cval) == RAW_DATA_CST)
13493 : 1841 : max_index += RAW_DATA_LENGTH (cval) - 1;
13494 : :
13495 : : /* Do we have match? */
13496 : 14578845 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13497 : : {
13498 : 14578554 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13499 : : {
13500 : 720400 : if (ctor_idx)
13501 : 720400 : *ctor_idx = cnt;
13502 : 720400 : return cval;
13503 : : }
13504 : : }
13505 : 291 : else if (in_gimple_form)
13506 : : /* We're past the element we search for. Note during parsing
13507 : : the elements might not be sorted.
13508 : : ??? We should use a binary search and a flag on the
13509 : : CONSTRUCTOR as to whether elements are sorted in declaration
13510 : : order. */
13511 : : break;
13512 : : }
13513 : 1595 : if (ctor_idx)
13514 : 1595 : *ctor_idx = cnt;
13515 : : return NULL_TREE;
13516 : : }
13517 : :
13518 : : /* Perform constant folding and related simplification of EXPR.
13519 : : The related simplifications include x*1 => x, x*0 => 0, etc.,
13520 : : and application of the associative law.
13521 : : NOP_EXPR conversions may be removed freely (as long as we
13522 : : are careful not to change the type of the overall expression).
13523 : : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13524 : : but we can constant-fold them if they have constant operands. */
13525 : :
13526 : : #ifdef ENABLE_FOLD_CHECKING
13527 : : # define fold(x) fold_1 (x)
13528 : : static tree fold_1 (tree);
13529 : : static
13530 : : #endif
13531 : : tree
13532 : 1236324343 : fold (tree expr)
13533 : : {
13534 : 1236442088 : const tree t = expr;
13535 : 1236442088 : enum tree_code code = TREE_CODE (t);
13536 : 1236442088 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13537 : 1236442088 : tree tem;
13538 : 1236442088 : location_t loc = EXPR_LOCATION (expr);
13539 : :
13540 : : /* Return right away if a constant. */
13541 : 1236442088 : if (kind == tcc_constant)
13542 : : return t;
13543 : :
13544 : : /* CALL_EXPR-like objects with variable numbers of operands are
13545 : : treated specially. */
13546 : 1119133906 : if (kind == tcc_vl_exp)
13547 : : {
13548 : 159250689 : if (code == CALL_EXPR)
13549 : : {
13550 : 159250127 : tem = fold_call_expr (loc, expr, false);
13551 : 315938385 : return tem ? tem : expr;
13552 : : }
13553 : : return expr;
13554 : : }
13555 : :
13556 : 959883217 : if (IS_EXPR_CODE_CLASS (kind))
13557 : : {
13558 : 957981593 : tree type = TREE_TYPE (t);
13559 : 957981593 : tree op0, op1, op2;
13560 : :
13561 : 957981593 : switch (TREE_CODE_LENGTH (code))
13562 : : {
13563 : 865716825 : case 1:
13564 : 865716825 : op0 = TREE_OPERAND (t, 0);
13565 : 865716825 : tem = fold_unary_loc (loc, code, type, op0);
13566 : 1479230405 : return tem ? tem : expr;
13567 : 83619322 : case 2:
13568 : 83619322 : op0 = TREE_OPERAND (t, 0);
13569 : 83619322 : op1 = TREE_OPERAND (t, 1);
13570 : 83619322 : tem = fold_binary_loc (loc, code, type, op0, op1);
13571 : 158971427 : return tem ? tem : expr;
13572 : 4140515 : case 3:
13573 : 4140515 : op0 = TREE_OPERAND (t, 0);
13574 : 4140515 : op1 = TREE_OPERAND (t, 1);
13575 : 4140515 : op2 = TREE_OPERAND (t, 2);
13576 : 4140515 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13577 : 8111858 : return tem ? tem : expr;
13578 : : default:
13579 : : break;
13580 : : }
13581 : : }
13582 : :
13583 : 6406555 : switch (code)
13584 : : {
13585 : 4402182 : case ARRAY_REF:
13586 : 4402182 : {
13587 : 4402182 : tree op0 = TREE_OPERAND (t, 0);
13588 : 4402182 : tree op1 = TREE_OPERAND (t, 1);
13589 : :
13590 : 4402182 : if (TREE_CODE (op1) == INTEGER_CST
13591 : 2861654 : && TREE_CODE (op0) == CONSTRUCTOR
13592 : 4402189 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13593 : : {
13594 : 7 : unsigned int idx;
13595 : 7 : tree val
13596 : 7 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13597 : : &idx);
13598 : 7 : if (val)
13599 : : {
13600 : 7 : if (TREE_CODE (val) != RAW_DATA_CST)
13601 : : return val;
13602 : 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13603 : 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13604 : : != INTEGER_CST))
13605 : : return t;
13606 : 2 : offset_int o
13607 : 2 : = (wi::to_offset (op1)
13608 : 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13609 : 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13610 : 2 : return build_int_cst (TREE_TYPE (val),
13611 : 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13612 : : }
13613 : : }
13614 : :
13615 : : return t;
13616 : : }
13617 : :
13618 : : /* Return a VECTOR_CST if possible. */
13619 : 106349 : case CONSTRUCTOR:
13620 : 106349 : {
13621 : 106349 : tree type = TREE_TYPE (t);
13622 : 106349 : if (TREE_CODE (type) != VECTOR_TYPE)
13623 : : return t;
13624 : :
13625 : : unsigned i;
13626 : : tree val;
13627 : 249170 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13628 : 215575 : if (! CONSTANT_CLASS_P (val))
13629 : : return t;
13630 : :
13631 : 33595 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13632 : : }
13633 : :
13634 : 117745 : case CONST_DECL:
13635 : 117745 : return fold (DECL_INITIAL (t));
13636 : :
13637 : : default:
13638 : : return t;
13639 : : } /* switch (code) */
13640 : : }
13641 : :
13642 : : #ifdef ENABLE_FOLD_CHECKING
13643 : : #undef fold
13644 : :
13645 : : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13646 : : hash_table<nofree_ptr_hash<const tree_node> > *);
13647 : : static void fold_check_failed (const_tree, const_tree);
13648 : : void print_fold_checksum (const_tree);
13649 : :
13650 : : /* When --enable-checking=fold, compute a digest of expr before
13651 : : and after actual fold call to see if fold did not accidentally
13652 : : change original expr. */
13653 : :
13654 : : tree
13655 : : fold (tree expr)
13656 : : {
13657 : : tree ret;
13658 : : struct md5_ctx ctx;
13659 : : unsigned char checksum_before[16], checksum_after[16];
13660 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13661 : :
13662 : : md5_init_ctx (&ctx);
13663 : : fold_checksum_tree (expr, &ctx, &ht);
13664 : : md5_finish_ctx (&ctx, checksum_before);
13665 : : ht.empty ();
13666 : :
13667 : : ret = fold_1 (expr);
13668 : :
13669 : : md5_init_ctx (&ctx);
13670 : : fold_checksum_tree (expr, &ctx, &ht);
13671 : : md5_finish_ctx (&ctx, checksum_after);
13672 : :
13673 : : if (memcmp (checksum_before, checksum_after, 16))
13674 : : fold_check_failed (expr, ret);
13675 : :
13676 : : return ret;
13677 : : }
13678 : :
13679 : : void
13680 : : print_fold_checksum (const_tree expr)
13681 : : {
13682 : : struct md5_ctx ctx;
13683 : : unsigned char checksum[16], cnt;
13684 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13685 : :
13686 : : md5_init_ctx (&ctx);
13687 : : fold_checksum_tree (expr, &ctx, &ht);
13688 : : md5_finish_ctx (&ctx, checksum);
13689 : : for (cnt = 0; cnt < 16; ++cnt)
13690 : : fprintf (stderr, "%02x", checksum[cnt]);
13691 : : putc ('\n', stderr);
13692 : : }
13693 : :
13694 : : static void
13695 : : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13696 : : {
13697 : : internal_error ("fold check: original tree changed by fold");
13698 : : }
13699 : :
13700 : : static void
13701 : : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13702 : : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13703 : : {
13704 : : const tree_node **slot;
13705 : : enum tree_code code;
13706 : : union tree_node *buf;
13707 : : int i, len;
13708 : :
13709 : : recursive_label:
13710 : : if (expr == NULL)
13711 : : return;
13712 : : slot = ht->find_slot (expr, INSERT);
13713 : : if (*slot != NULL)
13714 : : return;
13715 : : *slot = expr;
13716 : : code = TREE_CODE (expr);
13717 : : if (TREE_CODE_CLASS (code) == tcc_declaration
13718 : : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13719 : : {
13720 : : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13721 : : size_t sz = tree_size (expr);
13722 : : buf = XALLOCAVAR (union tree_node, sz);
13723 : : memcpy ((char *) buf, expr, sz);
13724 : : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13725 : : buf->decl_with_vis.symtab_node = NULL;
13726 : : buf->base.nowarning_flag = 0;
13727 : : expr = (tree) buf;
13728 : : }
13729 : : else if (TREE_CODE_CLASS (code) == tcc_type
13730 : : && (TYPE_POINTER_TO (expr)
13731 : : || TYPE_REFERENCE_TO (expr)
13732 : : || TYPE_CACHED_VALUES_P (expr)
13733 : : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13734 : : || TYPE_NEXT_VARIANT (expr)
13735 : : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13736 : : {
13737 : : /* Allow these fields to be modified. */
13738 : : tree tmp;
13739 : : size_t sz = tree_size (expr);
13740 : : buf = XALLOCAVAR (union tree_node, sz);
13741 : : memcpy ((char *) buf, expr, sz);
13742 : : expr = tmp = (tree) buf;
13743 : : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13744 : : TYPE_POINTER_TO (tmp) = NULL;
13745 : : TYPE_REFERENCE_TO (tmp) = NULL;
13746 : : TYPE_NEXT_VARIANT (tmp) = NULL;
13747 : : TYPE_ALIAS_SET (tmp) = -1;
13748 : : if (TYPE_CACHED_VALUES_P (tmp))
13749 : : {
13750 : : TYPE_CACHED_VALUES_P (tmp) = 0;
13751 : : TYPE_CACHED_VALUES (tmp) = NULL;
13752 : : }
13753 : : }
13754 : : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13755 : : {
13756 : : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13757 : : that and change builtins.cc etc. instead - see PR89543. */
13758 : : size_t sz = tree_size (expr);
13759 : : buf = XALLOCAVAR (union tree_node, sz);
13760 : : memcpy ((char *) buf, expr, sz);
13761 : : buf->base.nowarning_flag = 0;
13762 : : expr = (tree) buf;
13763 : : }
13764 : : md5_process_bytes (expr, tree_size (expr), ctx);
13765 : : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13766 : : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13767 : : if (TREE_CODE_CLASS (code) != tcc_type
13768 : : && TREE_CODE_CLASS (code) != tcc_declaration
13769 : : && code != TREE_LIST
13770 : : && code != SSA_NAME
13771 : : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13772 : : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13773 : : switch (TREE_CODE_CLASS (code))
13774 : : {
13775 : : case tcc_constant:
13776 : : switch (code)
13777 : : {
13778 : : case STRING_CST:
13779 : : md5_process_bytes (TREE_STRING_POINTER (expr),
13780 : : TREE_STRING_LENGTH (expr), ctx);
13781 : : break;
13782 : : case COMPLEX_CST:
13783 : : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13784 : : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13785 : : break;
13786 : : case VECTOR_CST:
13787 : : len = vector_cst_encoded_nelts (expr);
13788 : : for (i = 0; i < len; ++i)
13789 : : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13790 : : break;
13791 : : default:
13792 : : break;
13793 : : }
13794 : : break;
13795 : : case tcc_exceptional:
13796 : : switch (code)
13797 : : {
13798 : : case TREE_LIST:
13799 : : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13800 : : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13801 : : expr = TREE_CHAIN (expr);
13802 : : goto recursive_label;
13803 : : break;
13804 : : case TREE_VEC:
13805 : : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13806 : : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13807 : : break;
13808 : : default:
13809 : : break;
13810 : : }
13811 : : break;
13812 : : case tcc_expression:
13813 : : case tcc_reference:
13814 : : case tcc_comparison:
13815 : : case tcc_unary:
13816 : : case tcc_binary:
13817 : : case tcc_statement:
13818 : : case tcc_vl_exp:
13819 : : len = TREE_OPERAND_LENGTH (expr);
13820 : : for (i = 0; i < len; ++i)
13821 : : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13822 : : break;
13823 : : case tcc_declaration:
13824 : : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13825 : : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13826 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13827 : : {
13828 : : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13829 : : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13830 : : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13831 : : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13832 : : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13833 : : }
13834 : :
13835 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13836 : : {
13837 : : if (TREE_CODE (expr) == FUNCTION_DECL)
13838 : : {
13839 : : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13840 : : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13841 : : }
13842 : : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13843 : : }
13844 : : break;
13845 : : case tcc_type:
13846 : : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13847 : : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13848 : : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13849 : : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13850 : : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13851 : : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13852 : : if (INTEGRAL_TYPE_P (expr)
13853 : : || SCALAR_FLOAT_TYPE_P (expr))
13854 : : {
13855 : : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13856 : : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13857 : : }
13858 : : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13859 : : if (RECORD_OR_UNION_TYPE_P (expr))
13860 : : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13861 : : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13862 : : break;
13863 : : default:
13864 : : break;
13865 : : }
13866 : : }
13867 : :
13868 : : /* Helper function for outputting the checksum of a tree T. When
13869 : : debugging with gdb, you can "define mynext" to be "next" followed
13870 : : by "call debug_fold_checksum (op0)", then just trace down till the
13871 : : outputs differ. */
13872 : :
13873 : : DEBUG_FUNCTION void
13874 : : debug_fold_checksum (const_tree t)
13875 : : {
13876 : : int i;
13877 : : unsigned char checksum[16];
13878 : : struct md5_ctx ctx;
13879 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13880 : :
13881 : : md5_init_ctx (&ctx);
13882 : : fold_checksum_tree (t, &ctx, &ht);
13883 : : md5_finish_ctx (&ctx, checksum);
13884 : : ht.empty ();
13885 : :
13886 : : for (i = 0; i < 16; i++)
13887 : : fprintf (stderr, "%d ", checksum[i]);
13888 : :
13889 : : fprintf (stderr, "\n");
13890 : : }
13891 : :
13892 : : #endif
13893 : :
13894 : : /* Fold a unary tree expression with code CODE of type TYPE with an
13895 : : operand OP0. LOC is the location of the resulting expression.
13896 : : Return a folded expression if successful. Otherwise, return a tree
13897 : : expression with code CODE of type TYPE with an operand OP0. */
13898 : :
13899 : : tree
13900 : 701254627 : fold_build1_loc (location_t loc,
13901 : : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13902 : : {
13903 : 701254627 : tree tem;
13904 : : #ifdef ENABLE_FOLD_CHECKING
13905 : : unsigned char checksum_before[16], checksum_after[16];
13906 : : struct md5_ctx ctx;
13907 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13908 : :
13909 : : md5_init_ctx (&ctx);
13910 : : fold_checksum_tree (op0, &ctx, &ht);
13911 : : md5_finish_ctx (&ctx, checksum_before);
13912 : : ht.empty ();
13913 : : #endif
13914 : :
13915 : 701254627 : tem = fold_unary_loc (loc, code, type, op0);
13916 : 701254627 : if (!tem)
13917 : 375979797 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13918 : :
13919 : : #ifdef ENABLE_FOLD_CHECKING
13920 : : md5_init_ctx (&ctx);
13921 : : fold_checksum_tree (op0, &ctx, &ht);
13922 : : md5_finish_ctx (&ctx, checksum_after);
13923 : :
13924 : : if (memcmp (checksum_before, checksum_after, 16))
13925 : : fold_check_failed (op0, tem);
13926 : : #endif
13927 : 701254627 : return tem;
13928 : : }
13929 : :
13930 : : /* Fold a binary tree expression with code CODE of type TYPE with
13931 : : operands OP0 and OP1. LOC is the location of the resulting
13932 : : expression. Return a folded expression if successful. Otherwise,
13933 : : return a tree expression with code CODE of type TYPE with operands
13934 : : OP0 and OP1. */
13935 : :
13936 : : tree
13937 : 561038312 : fold_build2_loc (location_t loc,
13938 : : enum tree_code code, tree type, tree op0, tree op1
13939 : : MEM_STAT_DECL)
13940 : : {
13941 : 561038312 : tree tem;
13942 : : #ifdef ENABLE_FOLD_CHECKING
13943 : : unsigned char checksum_before_op0[16],
13944 : : checksum_before_op1[16],
13945 : : checksum_after_op0[16],
13946 : : checksum_after_op1[16];
13947 : : struct md5_ctx ctx;
13948 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13949 : :
13950 : : md5_init_ctx (&ctx);
13951 : : fold_checksum_tree (op0, &ctx, &ht);
13952 : : md5_finish_ctx (&ctx, checksum_before_op0);
13953 : : ht.empty ();
13954 : :
13955 : : md5_init_ctx (&ctx);
13956 : : fold_checksum_tree (op1, &ctx, &ht);
13957 : : md5_finish_ctx (&ctx, checksum_before_op1);
13958 : : ht.empty ();
13959 : : #endif
13960 : :
13961 : 561038312 : tem = fold_binary_loc (loc, code, type, op0, op1);
13962 : 561038312 : if (!tem)
13963 : 312339311 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13964 : :
13965 : : #ifdef ENABLE_FOLD_CHECKING
13966 : : md5_init_ctx (&ctx);
13967 : : fold_checksum_tree (op0, &ctx, &ht);
13968 : : md5_finish_ctx (&ctx, checksum_after_op0);
13969 : : ht.empty ();
13970 : :
13971 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13972 : : fold_check_failed (op0, tem);
13973 : :
13974 : : md5_init_ctx (&ctx);
13975 : : fold_checksum_tree (op1, &ctx, &ht);
13976 : : md5_finish_ctx (&ctx, checksum_after_op1);
13977 : :
13978 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13979 : : fold_check_failed (op1, tem);
13980 : : #endif
13981 : 561038312 : return tem;
13982 : : }
13983 : :
13984 : : /* Fold a ternary tree expression with code CODE of type TYPE with
13985 : : operands OP0, OP1, and OP2. Return a folded expression if
13986 : : successful. Otherwise, return a tree expression with code CODE of
13987 : : type TYPE with operands OP0, OP1, and OP2. */
13988 : :
13989 : : tree
13990 : 20936133 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13991 : : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13992 : : {
13993 : 20936133 : tree tem;
13994 : : #ifdef ENABLE_FOLD_CHECKING
13995 : : unsigned char checksum_before_op0[16],
13996 : : checksum_before_op1[16],
13997 : : checksum_before_op2[16],
13998 : : checksum_after_op0[16],
13999 : : checksum_after_op1[16],
14000 : : checksum_after_op2[16];
14001 : : struct md5_ctx ctx;
14002 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14003 : :
14004 : : md5_init_ctx (&ctx);
14005 : : fold_checksum_tree (op0, &ctx, &ht);
14006 : : md5_finish_ctx (&ctx, checksum_before_op0);
14007 : : ht.empty ();
14008 : :
14009 : : md5_init_ctx (&ctx);
14010 : : fold_checksum_tree (op1, &ctx, &ht);
14011 : : md5_finish_ctx (&ctx, checksum_before_op1);
14012 : : ht.empty ();
14013 : :
14014 : : md5_init_ctx (&ctx);
14015 : : fold_checksum_tree (op2, &ctx, &ht);
14016 : : md5_finish_ctx (&ctx, checksum_before_op2);
14017 : : ht.empty ();
14018 : : #endif
14019 : :
14020 : 20936133 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14021 : 20936133 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14022 : 20936133 : if (!tem)
14023 : 18383164 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14024 : :
14025 : : #ifdef ENABLE_FOLD_CHECKING
14026 : : md5_init_ctx (&ctx);
14027 : : fold_checksum_tree (op0, &ctx, &ht);
14028 : : md5_finish_ctx (&ctx, checksum_after_op0);
14029 : : ht.empty ();
14030 : :
14031 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14032 : : fold_check_failed (op0, tem);
14033 : :
14034 : : md5_init_ctx (&ctx);
14035 : : fold_checksum_tree (op1, &ctx, &ht);
14036 : : md5_finish_ctx (&ctx, checksum_after_op1);
14037 : : ht.empty ();
14038 : :
14039 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14040 : : fold_check_failed (op1, tem);
14041 : :
14042 : : md5_init_ctx (&ctx);
14043 : : fold_checksum_tree (op2, &ctx, &ht);
14044 : : md5_finish_ctx (&ctx, checksum_after_op2);
14045 : :
14046 : : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14047 : : fold_check_failed (op2, tem);
14048 : : #endif
14049 : 20936133 : return tem;
14050 : : }
14051 : :
14052 : : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14053 : : arguments in ARGARRAY, and a null static chain.
14054 : : Return a folded expression if successful. Otherwise, return a CALL_EXPR
14055 : : of type TYPE from the given operands as constructed by build_call_array. */
14056 : :
14057 : : tree
14058 : 49155155 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
14059 : : int nargs, tree *argarray)
14060 : : {
14061 : 49155155 : tree tem;
14062 : : #ifdef ENABLE_FOLD_CHECKING
14063 : : unsigned char checksum_before_fn[16],
14064 : : checksum_before_arglist[16],
14065 : : checksum_after_fn[16],
14066 : : checksum_after_arglist[16];
14067 : : struct md5_ctx ctx;
14068 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14069 : : int i;
14070 : :
14071 : : md5_init_ctx (&ctx);
14072 : : fold_checksum_tree (fn, &ctx, &ht);
14073 : : md5_finish_ctx (&ctx, checksum_before_fn);
14074 : : ht.empty ();
14075 : :
14076 : : md5_init_ctx (&ctx);
14077 : : for (i = 0; i < nargs; i++)
14078 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14079 : : md5_finish_ctx (&ctx, checksum_before_arglist);
14080 : : ht.empty ();
14081 : : #endif
14082 : :
14083 : 49155155 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14084 : 49155155 : if (!tem)
14085 : 47516653 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14086 : :
14087 : : #ifdef ENABLE_FOLD_CHECKING
14088 : : md5_init_ctx (&ctx);
14089 : : fold_checksum_tree (fn, &ctx, &ht);
14090 : : md5_finish_ctx (&ctx, checksum_after_fn);
14091 : : ht.empty ();
14092 : :
14093 : : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14094 : : fold_check_failed (fn, tem);
14095 : :
14096 : : md5_init_ctx (&ctx);
14097 : : for (i = 0; i < nargs; i++)
14098 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14099 : : md5_finish_ctx (&ctx, checksum_after_arglist);
14100 : :
14101 : : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14102 : : fold_check_failed (NULL_TREE, tem);
14103 : : #endif
14104 : 49155155 : return tem;
14105 : : }
14106 : :
14107 : : /* Perform constant folding and related simplification of initializer
14108 : : expression EXPR. These behave identically to "fold_buildN" but ignore
14109 : : potential run-time traps and exceptions that fold must preserve. */
14110 : :
14111 : : #define START_FOLD_INIT \
14112 : : int saved_signaling_nans = flag_signaling_nans;\
14113 : : int saved_trapping_math = flag_trapping_math;\
14114 : : int saved_rounding_math = flag_rounding_math;\
14115 : : int saved_trapv = flag_trapv;\
14116 : : int saved_folding_initializer = folding_initializer;\
14117 : : flag_signaling_nans = 0;\
14118 : : flag_trapping_math = 0;\
14119 : : flag_rounding_math = 0;\
14120 : : flag_trapv = 0;\
14121 : : folding_initializer = 1;
14122 : :
14123 : : #define END_FOLD_INIT \
14124 : : flag_signaling_nans = saved_signaling_nans;\
14125 : : flag_trapping_math = saved_trapping_math;\
14126 : : flag_rounding_math = saved_rounding_math;\
14127 : : flag_trapv = saved_trapv;\
14128 : : folding_initializer = saved_folding_initializer;
14129 : :
14130 : : tree
14131 : 543894 : fold_init (tree expr)
14132 : : {
14133 : 543894 : tree result;
14134 : 543894 : START_FOLD_INIT;
14135 : :
14136 : 543894 : result = fold (expr);
14137 : :
14138 : 543894 : END_FOLD_INIT;
14139 : 543894 : return result;
14140 : : }
14141 : :
14142 : : tree
14143 : 2988362 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14144 : : tree type, tree op)
14145 : : {
14146 : 2988362 : tree result;
14147 : 2988362 : START_FOLD_INIT;
14148 : :
14149 : 2988362 : result = fold_build1_loc (loc, code, type, op);
14150 : :
14151 : 2988362 : END_FOLD_INIT;
14152 : 2988362 : return result;
14153 : : }
14154 : :
14155 : : tree
14156 : 50474 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14157 : : tree type, tree op0, tree op1)
14158 : : {
14159 : 50474 : tree result;
14160 : 50474 : START_FOLD_INIT;
14161 : :
14162 : 50474 : result = fold_build2_loc (loc, code, type, op0, op1);
14163 : :
14164 : 50474 : END_FOLD_INIT;
14165 : 50474 : return result;
14166 : : }
14167 : :
14168 : : tree
14169 : 3488 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14170 : : int nargs, tree *argarray)
14171 : : {
14172 : 3488 : tree result;
14173 : 3488 : START_FOLD_INIT;
14174 : :
14175 : 3488 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14176 : :
14177 : 3488 : END_FOLD_INIT;
14178 : 3488 : return result;
14179 : : }
14180 : :
14181 : : tree
14182 : 19553972 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14183 : : tree lhs, tree rhs)
14184 : : {
14185 : 19553972 : tree result;
14186 : 19553972 : START_FOLD_INIT;
14187 : :
14188 : 19553972 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14189 : :
14190 : 19553972 : END_FOLD_INIT;
14191 : 19553972 : return result;
14192 : : }
14193 : :
14194 : : #undef START_FOLD_INIT
14195 : : #undef END_FOLD_INIT
14196 : :
14197 : : /* Determine if first argument is a multiple of second argument. Return
14198 : : false if it is not, or we cannot easily determined it to be.
14199 : :
14200 : : An example of the sort of thing we care about (at this point; this routine
14201 : : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14202 : : fold cases do now) is discovering that
14203 : :
14204 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14205 : :
14206 : : is a multiple of
14207 : :
14208 : : SAVE_EXPR (J * 8)
14209 : :
14210 : : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14211 : :
14212 : : This code also handles discovering that
14213 : :
14214 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14215 : :
14216 : : is a multiple of 8 so we don't have to worry about dealing with a
14217 : : possible remainder.
14218 : :
14219 : : Note that we *look* inside a SAVE_EXPR only to determine how it was
14220 : : calculated; it is not safe for fold to do much of anything else with the
14221 : : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14222 : : at run time. For example, the latter example above *cannot* be implemented
14223 : : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14224 : : evaluation time of the original SAVE_EXPR is not necessarily the same at
14225 : : the time the new expression is evaluated. The only optimization of this
14226 : : sort that would be valid is changing
14227 : :
14228 : : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14229 : :
14230 : : divided by 8 to
14231 : :
14232 : : SAVE_EXPR (I) * SAVE_EXPR (J)
14233 : :
14234 : : (where the same SAVE_EXPR (J) is used in the original and the
14235 : : transformed version).
14236 : :
14237 : : NOWRAP specifies whether all outer operations in TYPE should
14238 : : be considered not wrapping. Any type conversion within TOP acts
14239 : : as a barrier and we will fall back to NOWRAP being false.
14240 : : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14241 : : as not wrapping even though they are generally using unsigned arithmetic. */
14242 : :
14243 : : bool
14244 : 1521897 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14245 : : {
14246 : 1521897 : gimple *stmt;
14247 : 1521897 : tree op1, op2;
14248 : :
14249 : 1521897 : if (operand_equal_p (top, bottom, 0))
14250 : : return true;
14251 : :
14252 : 1063893 : if (TREE_CODE (type) != INTEGER_TYPE)
14253 : : return false;
14254 : :
14255 : 1063888 : switch (TREE_CODE (top))
14256 : : {
14257 : 636 : case BIT_AND_EXPR:
14258 : : /* Bitwise and provides a power of two multiple. If the mask is
14259 : : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14260 : 636 : if (!integer_pow2p (bottom))
14261 : : return false;
14262 : 636 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14263 : 636 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14264 : :
14265 : 359672 : case MULT_EXPR:
14266 : : /* If the multiplication can wrap we cannot recurse further unless
14267 : : the bottom is a power of two which is where wrapping does not
14268 : : matter. */
14269 : 359672 : if (!nowrap
14270 : 13954 : && !TYPE_OVERFLOW_UNDEFINED (type)
14271 : 364486 : && !integer_pow2p (bottom))
14272 : : return false;
14273 : 359252 : if (TREE_CODE (bottom) == INTEGER_CST)
14274 : : {
14275 : 357552 : op1 = TREE_OPERAND (top, 0);
14276 : 357552 : op2 = TREE_OPERAND (top, 1);
14277 : 357552 : if (TREE_CODE (op1) == INTEGER_CST)
14278 : 0 : std::swap (op1, op2);
14279 : 357552 : if (TREE_CODE (op2) == INTEGER_CST)
14280 : : {
14281 : 347644 : if (multiple_of_p (type, op2, bottom, nowrap))
14282 : : return true;
14283 : : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14284 : 2991 : if (multiple_of_p (type, bottom, op2, nowrap))
14285 : : {
14286 : 1888 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14287 : 1888 : wi::to_widest (op2));
14288 : 1888 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14289 : : {
14290 : 1888 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14291 : 1888 : return multiple_of_p (type, op1, op2, nowrap);
14292 : : }
14293 : 1888 : }
14294 : 1103 : return multiple_of_p (type, op1, bottom, nowrap);
14295 : : }
14296 : : }
14297 : 11608 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14298 : 11608 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14299 : :
14300 : 284 : case LSHIFT_EXPR:
14301 : : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14302 : 284 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14303 : : {
14304 : 284 : op1 = TREE_OPERAND (top, 1);
14305 : 284 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14306 : : {
14307 : 284 : wide_int mul_op
14308 : 284 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14309 : 568 : return multiple_of_p (type,
14310 : 568 : wide_int_to_tree (type, mul_op), bottom,
14311 : : nowrap);
14312 : 284 : }
14313 : : }
14314 : : return false;
14315 : :
14316 : 210843 : case MINUS_EXPR:
14317 : 210843 : case PLUS_EXPR:
14318 : : /* If the addition or subtraction can wrap we cannot recurse further
14319 : : unless bottom is a power of two which is where wrapping does not
14320 : : matter. */
14321 : 210843 : if (!nowrap
14322 : 169578 : && !TYPE_OVERFLOW_UNDEFINED (type)
14323 : 378992 : && !integer_pow2p (bottom))
14324 : : return false;
14325 : :
14326 : : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14327 : : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14328 : : but 0xfffffffd is not. */
14329 : 184723 : op1 = TREE_OPERAND (top, 1);
14330 : 184723 : if (TREE_CODE (top) == PLUS_EXPR
14331 : 179080 : && nowrap
14332 : 35709 : && TYPE_UNSIGNED (type)
14333 : 219881 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14334 : 30512 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14335 : :
14336 : : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14337 : : precisely, so be conservative here checking if both op0 and op1
14338 : : are multiple of bottom. Note we check the second operand first
14339 : : since it's usually simpler. */
14340 : 184723 : return (multiple_of_p (type, op1, bottom, nowrap)
14341 : 184723 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14342 : :
14343 : 153293 : CASE_CONVERT:
14344 : : /* Can't handle conversions from non-integral or wider integral type. */
14345 : 153293 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14346 : 153293 : || (TYPE_PRECISION (type)
14347 : 53109 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14348 : : return false;
14349 : : /* NOWRAP only extends to operations in the outermost type so
14350 : : make sure to strip it off here. */
14351 : 52803 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14352 : 105606 : TREE_OPERAND (top, 0), bottom, false);
14353 : :
14354 : 12352 : case SAVE_EXPR:
14355 : 12352 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14356 : :
14357 : 0 : case COND_EXPR:
14358 : 0 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14359 : 0 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14360 : :
14361 : 120904 : case INTEGER_CST:
14362 : 120904 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14363 : 2704 : return false;
14364 : 118200 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14365 : : SIGNED);
14366 : :
14367 : 63732 : case SSA_NAME:
14368 : 63732 : if (TREE_CODE (bottom) == INTEGER_CST
14369 : 60786 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14370 : 124518 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14371 : : {
14372 : 27042 : enum tree_code code = gimple_assign_rhs_code (stmt);
14373 : :
14374 : : /* Check for special cases to see if top is defined as multiple
14375 : : of bottom:
14376 : :
14377 : : top = (X & ~(bottom - 1) ; bottom is power of 2
14378 : :
14379 : : or
14380 : :
14381 : : Y = X % bottom
14382 : : top = X - Y. */
14383 : 27042 : if (code == BIT_AND_EXPR
14384 : 296 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14385 : 296 : && TREE_CODE (op2) == INTEGER_CST
14386 : 188 : && integer_pow2p (bottom)
14387 : 27230 : && wi::multiple_of_p (wi::to_widest (op2),
14388 : 188 : wi::to_widest (bottom), SIGNED))
14389 : 179 : return true;
14390 : :
14391 : 26863 : op1 = gimple_assign_rhs1 (stmt);
14392 : 26863 : if (code == MINUS_EXPR
14393 : 2636 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14394 : 2636 : && TREE_CODE (op2) == SSA_NAME
14395 : 2636 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14396 : 2636 : && gimple_code (stmt) == GIMPLE_ASSIGN
14397 : 2282 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14398 : 62 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14399 : 26925 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14400 : : return true;
14401 : : }
14402 : :
14403 : : /* fall through */
14404 : :
14405 : : default:
14406 : : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14407 : : return multiple_p (wi::to_poly_widest (top),
14408 : : wi::to_poly_widest (bottom));
14409 : :
14410 : : return false;
14411 : : }
14412 : : }
14413 : :
14414 : : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14415 : : This function returns true for integer expressions, and returns
14416 : : false if uncertain. */
14417 : :
14418 : : bool
14419 : 571699 : tree_expr_finite_p (const_tree x)
14420 : : {
14421 : 571703 : machine_mode mode = element_mode (x);
14422 : 571703 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14423 : : return true;
14424 : 571509 : switch (TREE_CODE (x))
14425 : : {
14426 : 592 : case REAL_CST:
14427 : 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14428 : 0 : case COMPLEX_CST:
14429 : 0 : return tree_expr_finite_p (TREE_REALPART (x))
14430 : 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14431 : : case FLOAT_EXPR:
14432 : : return true;
14433 : 4 : case ABS_EXPR:
14434 : 4 : case CONVERT_EXPR:
14435 : 4 : case NON_LVALUE_EXPR:
14436 : 4 : case NEGATE_EXPR:
14437 : 4 : case SAVE_EXPR:
14438 : 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14439 : 0 : case MIN_EXPR:
14440 : 0 : case MAX_EXPR:
14441 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14442 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14443 : 0 : case COND_EXPR:
14444 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14445 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14446 : 38 : case CALL_EXPR:
14447 : 38 : switch (get_call_combined_fn (x))
14448 : : {
14449 : 0 : CASE_CFN_FABS:
14450 : 0 : CASE_CFN_FABS_FN:
14451 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14452 : 0 : CASE_CFN_FMAX:
14453 : 0 : CASE_CFN_FMAX_FN:
14454 : 0 : CASE_CFN_FMIN:
14455 : 0 : CASE_CFN_FMIN_FN:
14456 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14457 : 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14458 : : default:
14459 : : return false;
14460 : : }
14461 : :
14462 : : default:
14463 : : return false;
14464 : : }
14465 : : }
14466 : :
14467 : : /* Return true if expression X evaluates to an infinity.
14468 : : This function returns false for integer expressions. */
14469 : :
14470 : : bool
14471 : 818896 : tree_expr_infinite_p (const_tree x)
14472 : : {
14473 : 819346 : if (!HONOR_INFINITIES (x))
14474 : : return false;
14475 : 819231 : switch (TREE_CODE (x))
14476 : : {
14477 : 0 : case REAL_CST:
14478 : 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14479 : 450 : case ABS_EXPR:
14480 : 450 : case NEGATE_EXPR:
14481 : 450 : case NON_LVALUE_EXPR:
14482 : 450 : case SAVE_EXPR:
14483 : 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14484 : 0 : case COND_EXPR:
14485 : 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14486 : 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14487 : : default:
14488 : : return false;
14489 : : }
14490 : : }
14491 : :
14492 : : /* Return true if expression X could evaluate to an infinity.
14493 : : This function returns false for integer expressions, and returns
14494 : : true if uncertain. */
14495 : :
14496 : : bool
14497 : 374175 : tree_expr_maybe_infinite_p (const_tree x)
14498 : : {
14499 : 374183 : if (!HONOR_INFINITIES (x))
14500 : : return false;
14501 : 373856 : switch (TREE_CODE (x))
14502 : : {
14503 : 186 : case REAL_CST:
14504 : 186 : return real_isinf (TREE_REAL_CST_PTR (x));
14505 : : case FLOAT_EXPR:
14506 : : return false;
14507 : 8 : case ABS_EXPR:
14508 : 8 : case NEGATE_EXPR:
14509 : 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14510 : 1 : case COND_EXPR:
14511 : 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14512 : 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14513 : : default:
14514 : : return true;
14515 : : }
14516 : : }
14517 : :
14518 : : /* Return true if expression X evaluates to a signaling NaN.
14519 : : This function returns false for integer expressions. */
14520 : :
14521 : : bool
14522 : 395 : tree_expr_signaling_nan_p (const_tree x)
14523 : : {
14524 : 395 : if (!HONOR_SNANS (x))
14525 : : return false;
14526 : 124 : switch (TREE_CODE (x))
14527 : : {
14528 : 124 : case REAL_CST:
14529 : 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14530 : 0 : case NON_LVALUE_EXPR:
14531 : 0 : case SAVE_EXPR:
14532 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14533 : 0 : case COND_EXPR:
14534 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14535 : 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14536 : : default:
14537 : : return false;
14538 : : }
14539 : : }
14540 : :
14541 : : /* Return true if expression X could evaluate to a signaling NaN.
14542 : : This function returns false for integer expressions, and returns
14543 : : true if uncertain. */
14544 : :
14545 : : bool
14546 : 728153 : tree_expr_maybe_signaling_nan_p (const_tree x)
14547 : : {
14548 : 728153 : if (!HONOR_SNANS (x))
14549 : : return false;
14550 : 5032 : switch (TREE_CODE (x))
14551 : : {
14552 : 1456 : case REAL_CST:
14553 : 1456 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14554 : : case FLOAT_EXPR:
14555 : : return false;
14556 : 0 : case ABS_EXPR:
14557 : 0 : case CONVERT_EXPR:
14558 : 0 : case NEGATE_EXPR:
14559 : 0 : case NON_LVALUE_EXPR:
14560 : 0 : case SAVE_EXPR:
14561 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14562 : 0 : case MIN_EXPR:
14563 : 0 : case MAX_EXPR:
14564 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14565 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14566 : 0 : case COND_EXPR:
14567 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14568 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14569 : 0 : case CALL_EXPR:
14570 : 0 : switch (get_call_combined_fn (x))
14571 : : {
14572 : 0 : CASE_CFN_FABS:
14573 : 0 : CASE_CFN_FABS_FN:
14574 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14575 : 0 : CASE_CFN_FMAX:
14576 : 0 : CASE_CFN_FMAX_FN:
14577 : 0 : CASE_CFN_FMIN:
14578 : 0 : CASE_CFN_FMIN_FN:
14579 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14580 : 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14581 : : default:
14582 : : return true;
14583 : : }
14584 : : default:
14585 : : return true;
14586 : : }
14587 : : }
14588 : :
14589 : : /* Return true if expression X evaluates to a NaN.
14590 : : This function returns false for integer expressions. */
14591 : :
14592 : : bool
14593 : 4017204 : tree_expr_nan_p (const_tree x)
14594 : : {
14595 : 4399066 : if (!HONOR_NANS (x))
14596 : : return false;
14597 : 4398716 : switch (TREE_CODE (x))
14598 : : {
14599 : 3792 : case REAL_CST:
14600 : 3792 : return real_isnan (TREE_REAL_CST_PTR (x));
14601 : 381862 : case NON_LVALUE_EXPR:
14602 : 381862 : case SAVE_EXPR:
14603 : 381862 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14604 : 900 : case COND_EXPR:
14605 : 900 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14606 : 900 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14607 : : default:
14608 : : return false;
14609 : : }
14610 : : }
14611 : :
14612 : : /* Return true if expression X could evaluate to a NaN.
14613 : : This function returns false for integer expressions, and returns
14614 : : true if uncertain. */
14615 : :
14616 : : bool
14617 : 4882836 : tree_expr_maybe_nan_p (const_tree x)
14618 : : {
14619 : 6838436 : if (!HONOR_NANS (x))
14620 : : return false;
14621 : 6678456 : switch (TREE_CODE (x))
14622 : : {
14623 : 3282 : case REAL_CST:
14624 : 3282 : return real_isnan (TREE_REAL_CST_PTR (x));
14625 : : case FLOAT_EXPR:
14626 : : return false;
14627 : 13819 : case PLUS_EXPR:
14628 : 13819 : case MINUS_EXPR:
14629 : 13819 : case MULT_EXPR:
14630 : 13819 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14631 : 13819 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14632 : 1955600 : case ABS_EXPR:
14633 : 1955600 : case CONVERT_EXPR:
14634 : 1955600 : case NEGATE_EXPR:
14635 : 1955600 : case NON_LVALUE_EXPR:
14636 : 1955600 : case SAVE_EXPR:
14637 : 1955600 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14638 : 176 : case MIN_EXPR:
14639 : 176 : case MAX_EXPR:
14640 : 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14641 : 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14642 : 557 : case COND_EXPR:
14643 : 557 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14644 : 557 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14645 : 1064 : case CALL_EXPR:
14646 : 1064 : switch (get_call_combined_fn (x))
14647 : : {
14648 : 0 : CASE_CFN_FABS:
14649 : 0 : CASE_CFN_FABS_FN:
14650 : 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14651 : 108 : CASE_CFN_FMAX:
14652 : 108 : CASE_CFN_FMAX_FN:
14653 : 108 : CASE_CFN_FMIN:
14654 : 108 : CASE_CFN_FMIN_FN:
14655 : 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14656 : 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14657 : : default:
14658 : : return true;
14659 : : }
14660 : : default:
14661 : : return true;
14662 : : }
14663 : : }
14664 : :
14665 : : /* Return true if expression X could evaluate to -0.0.
14666 : : This function returns true if uncertain. */
14667 : :
14668 : : bool
14669 : 603675 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14670 : : {
14671 : 603675 : if (!HONOR_SIGNED_ZEROS (x))
14672 : : return false;
14673 : 603675 : switch (TREE_CODE (x))
14674 : : {
14675 : 0 : case REAL_CST:
14676 : 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14677 : : case INTEGER_CST:
14678 : : case FLOAT_EXPR:
14679 : : case ABS_EXPR:
14680 : : return false;
14681 : 0 : case NON_LVALUE_EXPR:
14682 : 0 : case SAVE_EXPR:
14683 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14684 : 0 : case COND_EXPR:
14685 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14686 : 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14687 : 1 : case CALL_EXPR:
14688 : 1 : switch (get_call_combined_fn (x))
14689 : : {
14690 : : CASE_CFN_FABS:
14691 : : CASE_CFN_FABS_FN:
14692 : : return false;
14693 : : default:
14694 : : break;
14695 : : }
14696 : : default:
14697 : : break;
14698 : : }
14699 : : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14700 : : * but currently those predicates require tree and not const_tree. */
14701 : : return true;
14702 : : }
14703 : :
14704 : : #define tree_expr_nonnegative_warnv_p(X, Y) \
14705 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14706 : :
14707 : : #define RECURSE(X) \
14708 : : ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14709 : :
14710 : : /* Return true if CODE or TYPE is known to be non-negative. */
14711 : :
14712 : : static bool
14713 : 37868722 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14714 : : {
14715 : 37868722 : if (!VECTOR_TYPE_P (type)
14716 : 37850827 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14717 : 75718806 : && truth_value_p (code))
14718 : : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14719 : : have a signed:1 type (where the value is -1 and 0). */
14720 : : return true;
14721 : : return false;
14722 : : }
14723 : :
14724 : : /* Return true if (CODE OP0) is known to be non-negative. If the return
14725 : : value is based on the assumption that signed overflow is undefined,
14726 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14727 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14728 : :
14729 : : bool
14730 : 13363583 : tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14731 : : bool *strict_overflow_p, int depth)
14732 : : {
14733 : 13363583 : if (TYPE_UNSIGNED (type))
14734 : : return true;
14735 : :
14736 : 5291001 : switch (code)
14737 : : {
14738 : 322272 : case ABS_EXPR:
14739 : : /* We can't return 1 if flag_wrapv is set because
14740 : : ABS_EXPR<INT_MIN> = INT_MIN. */
14741 : 322272 : if (!ANY_INTEGRAL_TYPE_P (type))
14742 : : return true;
14743 : 18404 : if (TYPE_OVERFLOW_UNDEFINED (type))
14744 : : {
14745 : 17459 : *strict_overflow_p = true;
14746 : 17459 : return true;
14747 : : }
14748 : : break;
14749 : :
14750 : 220155 : case NON_LVALUE_EXPR:
14751 : 220155 : case FLOAT_EXPR:
14752 : 220155 : case FIX_TRUNC_EXPR:
14753 : 220155 : return RECURSE (op0);
14754 : :
14755 : 4648477 : CASE_CONVERT:
14756 : 4648477 : {
14757 : 4648477 : tree inner_type = TREE_TYPE (op0);
14758 : 4648477 : tree outer_type = type;
14759 : :
14760 : 4648477 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14761 : : {
14762 : 299593 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14763 : 299593 : return RECURSE (op0);
14764 : 0 : if (INTEGRAL_TYPE_P (inner_type))
14765 : : {
14766 : 0 : if (TYPE_UNSIGNED (inner_type))
14767 : : return true;
14768 : 0 : return RECURSE (op0);
14769 : : }
14770 : : }
14771 : 4348884 : else if (INTEGRAL_TYPE_P (outer_type))
14772 : : {
14773 : 4348847 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14774 : 0 : return RECURSE (op0);
14775 : 4348847 : if (INTEGRAL_TYPE_P (inner_type))
14776 : 4188511 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14777 : 4188511 : && TYPE_UNSIGNED (inner_type);
14778 : : }
14779 : : }
14780 : : break;
14781 : :
14782 : 100097 : default:
14783 : 100097 : return tree_simple_nonnegative_warnv_p (code, type);
14784 : : }
14785 : :
14786 : : /* We don't know sign of `t', so be conservative and return false. */
14787 : : return false;
14788 : : }
14789 : :
14790 : : /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14791 : : value is based on the assumption that signed overflow is undefined,
14792 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14793 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14794 : :
14795 : : bool
14796 : 39099289 : tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14797 : : tree op1, bool *strict_overflow_p,
14798 : : int depth)
14799 : : {
14800 : 39099289 : if (TYPE_UNSIGNED (type))
14801 : : return true;
14802 : :
14803 : 15132911 : switch (code)
14804 : : {
14805 : 5663395 : case POINTER_PLUS_EXPR:
14806 : 5663395 : case PLUS_EXPR:
14807 : 5663395 : if (FLOAT_TYPE_P (type))
14808 : 68061 : return RECURSE (op0) && RECURSE (op1);
14809 : :
14810 : : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14811 : : both unsigned and at least 2 bits shorter than the result. */
14812 : 5595334 : if (TREE_CODE (type) == INTEGER_TYPE
14813 : 5589244 : && TREE_CODE (op0) == NOP_EXPR
14814 : 7821 : && TREE_CODE (op1) == NOP_EXPR)
14815 : : {
14816 : 205 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14817 : 205 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14818 : 205 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14819 : 308 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14820 : : {
14821 : 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14822 : 95 : TYPE_PRECISION (inner2)) + 1;
14823 : 95 : return prec < TYPE_PRECISION (type);
14824 : : }
14825 : : }
14826 : : break;
14827 : :
14828 : 1337892 : case MULT_EXPR:
14829 : 1337892 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14830 : : {
14831 : : /* x * x is always non-negative for floating point x
14832 : : or without overflow. */
14833 : 1279379 : if (operand_equal_p (op0, op1, 0)
14834 : 1279379 : || (RECURSE (op0) && RECURSE (op1)))
14835 : : {
14836 : 1234 : if (ANY_INTEGRAL_TYPE_P (type)
14837 : 13561 : && TYPE_OVERFLOW_UNDEFINED (type))
14838 : 12327 : *strict_overflow_p = true;
14839 : 13540 : return true;
14840 : : }
14841 : : }
14842 : :
14843 : : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14844 : : both unsigned and their total bits is shorter than the result. */
14845 : 1324352 : if (TREE_CODE (type) == INTEGER_TYPE
14846 : 1245366 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14847 : 151 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14848 : : {
14849 : 115 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14850 : 115 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14851 : 115 : : TREE_TYPE (op0);
14852 : 115 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14853 : 115 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14854 : 115 : : TREE_TYPE (op1);
14855 : :
14856 : 115 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14857 : 115 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14858 : :
14859 : 115 : if (TREE_CODE (op0) == INTEGER_CST)
14860 : 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14861 : :
14862 : 115 : if (TREE_CODE (op1) == INTEGER_CST)
14863 : 68 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14864 : :
14865 : 115 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14866 : 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14867 : : {
14868 : 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14869 : 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14870 : 0 : : TYPE_PRECISION (inner0);
14871 : :
14872 : 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14873 : 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14874 : 0 : : TYPE_PRECISION (inner1);
14875 : :
14876 : 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14877 : : }
14878 : : }
14879 : : return false;
14880 : :
14881 : 94675 : case BIT_AND_EXPR:
14882 : 94675 : return RECURSE (op0) || RECURSE (op1);
14883 : :
14884 : 74133 : case MAX_EXPR:
14885 : : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14886 : : things. */
14887 : 74133 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14888 : 76 : return RECURSE (op0) && RECURSE (op1);
14889 : 74057 : return RECURSE (op0) || RECURSE (op1);
14890 : :
14891 : 735289 : case BIT_IOR_EXPR:
14892 : 735289 : case BIT_XOR_EXPR:
14893 : 735289 : case MIN_EXPR:
14894 : 735289 : case RDIV_EXPR:
14895 : 735289 : case TRUNC_DIV_EXPR:
14896 : 735289 : case CEIL_DIV_EXPR:
14897 : 735289 : case FLOOR_DIV_EXPR:
14898 : 735289 : case ROUND_DIV_EXPR:
14899 : 735289 : return RECURSE (op0) && RECURSE (op1);
14900 : :
14901 : 92442 : case TRUNC_MOD_EXPR:
14902 : 92442 : return RECURSE (op0);
14903 : :
14904 : 254 : case FLOOR_MOD_EXPR:
14905 : 254 : return RECURSE (op1);
14906 : :
14907 : 7134831 : case CEIL_MOD_EXPR:
14908 : 7134831 : case ROUND_MOD_EXPR:
14909 : 7134831 : default:
14910 : 7134831 : return tree_simple_nonnegative_warnv_p (code, type);
14911 : : }
14912 : :
14913 : : /* We don't know sign of `t', so be conservative and return false. */
14914 : : return false;
14915 : : }
14916 : :
14917 : : /* Return true if T is known to be non-negative. If the return
14918 : : value is based on the assumption that signed overflow is undefined,
14919 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14920 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14921 : :
14922 : : bool
14923 : 47276145 : tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14924 : : {
14925 : 47276145 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14926 : : return true;
14927 : :
14928 : 31777257 : switch (TREE_CODE (t))
14929 : : {
14930 : 3528567 : case INTEGER_CST:
14931 : 3528567 : return tree_int_cst_sgn (t) >= 0;
14932 : :
14933 : 982188 : case REAL_CST:
14934 : 982188 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14935 : :
14936 : 0 : case FIXED_CST:
14937 : 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14938 : :
14939 : 490 : case COND_EXPR:
14940 : 490 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14941 : :
14942 : 17358440 : case SSA_NAME:
14943 : : /* Limit the depth of recursion to avoid quadratic behavior.
14944 : : This is expected to catch almost all occurrences in practice.
14945 : : If this code misses important cases that unbounded recursion
14946 : : would not, passes that need this information could be revised
14947 : : to provide it through dataflow propagation. */
14948 : 17358440 : return (!name_registered_for_update_p (t)
14949 : 17358439 : && depth < param_max_ssa_name_query_depth
14950 : 33323596 : && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14951 : : strict_overflow_p, depth));
14952 : :
14953 : 9907572 : default:
14954 : 9907572 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14955 : : }
14956 : : }
14957 : :
14958 : : /* Return true if T is known to be non-negative. If the return
14959 : : value is based on the assumption that signed overflow is undefined,
14960 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14961 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14962 : :
14963 : : bool
14964 : 20888755 : tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14965 : : bool *strict_overflow_p, int depth)
14966 : : {
14967 : 20888755 : switch (fn)
14968 : : {
14969 : : CASE_CFN_ACOS:
14970 : : CASE_CFN_ACOS_FN:
14971 : : CASE_CFN_ACOSH:
14972 : : CASE_CFN_ACOSH_FN:
14973 : : CASE_CFN_ACOSPI:
14974 : : CASE_CFN_ACOSPI_FN:
14975 : : CASE_CFN_CABS:
14976 : : CASE_CFN_CABS_FN:
14977 : : CASE_CFN_COSH:
14978 : : CASE_CFN_COSH_FN:
14979 : : CASE_CFN_ERFC:
14980 : : CASE_CFN_ERFC_FN:
14981 : : CASE_CFN_EXP:
14982 : : CASE_CFN_EXP_FN:
14983 : : CASE_CFN_EXP10:
14984 : : CASE_CFN_EXP2:
14985 : : CASE_CFN_EXP2_FN:
14986 : : CASE_CFN_FABS:
14987 : : CASE_CFN_FABS_FN:
14988 : : CASE_CFN_FDIM:
14989 : : CASE_CFN_FDIM_FN:
14990 : : CASE_CFN_HYPOT:
14991 : : CASE_CFN_HYPOT_FN:
14992 : : CASE_CFN_POW10:
14993 : : CASE_CFN_FFS:
14994 : : CASE_CFN_PARITY:
14995 : : CASE_CFN_POPCOUNT:
14996 : : CASE_CFN_CLRSB:
14997 : : case CFN_BUILT_IN_BSWAP16:
14998 : : case CFN_BUILT_IN_BSWAP32:
14999 : : case CFN_BUILT_IN_BSWAP64:
15000 : : case CFN_BUILT_IN_BSWAP128:
15001 : : /* Always true. */
15002 : : return true;
15003 : :
15004 : 1021 : CASE_CFN_CLZ:
15005 : 1021 : CASE_CFN_CTZ:
15006 : 1021 : if (arg1)
15007 : 0 : return RECURSE (arg1);
15008 : : return true;
15009 : :
15010 : 1006 : CASE_CFN_SQRT:
15011 : 1006 : CASE_CFN_SQRT_FN:
15012 : : /* sqrt(-0.0) is -0.0. */
15013 : 1006 : if (!HONOR_SIGNED_ZEROS (type))
15014 : : return true;
15015 : 970 : return RECURSE (arg0);
15016 : :
15017 : 119694 : CASE_CFN_ASINH:
15018 : 119694 : CASE_CFN_ASINH_FN:
15019 : 119694 : CASE_CFN_ASINPI:
15020 : 119694 : CASE_CFN_ASINPI_FN:
15021 : 119694 : CASE_CFN_ATAN:
15022 : 119694 : CASE_CFN_ATAN_FN:
15023 : 119694 : CASE_CFN_ATANH:
15024 : 119694 : CASE_CFN_ATANH_FN:
15025 : 119694 : CASE_CFN_ATANPI:
15026 : 119694 : CASE_CFN_ATANPI_FN:
15027 : 119694 : CASE_CFN_CBRT:
15028 : 119694 : CASE_CFN_CBRT_FN:
15029 : 119694 : CASE_CFN_CEIL:
15030 : 119694 : CASE_CFN_CEIL_FN:
15031 : 119694 : CASE_CFN_ERF:
15032 : 119694 : CASE_CFN_ERF_FN:
15033 : 119694 : CASE_CFN_EXPM1:
15034 : 119694 : CASE_CFN_EXPM1_FN:
15035 : 119694 : CASE_CFN_FLOOR:
15036 : 119694 : CASE_CFN_FLOOR_FN:
15037 : 119694 : CASE_CFN_FMOD:
15038 : 119694 : CASE_CFN_FMOD_FN:
15039 : 119694 : CASE_CFN_FREXP:
15040 : 119694 : CASE_CFN_FREXP_FN:
15041 : 119694 : CASE_CFN_ICEIL:
15042 : 119694 : CASE_CFN_IFLOOR:
15043 : 119694 : CASE_CFN_IRINT:
15044 : 119694 : CASE_CFN_IROUND:
15045 : 119694 : CASE_CFN_LCEIL:
15046 : 119694 : CASE_CFN_LDEXP:
15047 : 119694 : CASE_CFN_LFLOOR:
15048 : 119694 : CASE_CFN_LLCEIL:
15049 : 119694 : CASE_CFN_LLFLOOR:
15050 : 119694 : CASE_CFN_LLRINT:
15051 : 119694 : CASE_CFN_LLRINT_FN:
15052 : 119694 : CASE_CFN_LLROUND:
15053 : 119694 : CASE_CFN_LLROUND_FN:
15054 : 119694 : CASE_CFN_LRINT:
15055 : 119694 : CASE_CFN_LRINT_FN:
15056 : 119694 : CASE_CFN_LROUND:
15057 : 119694 : CASE_CFN_LROUND_FN:
15058 : 119694 : CASE_CFN_MODF:
15059 : 119694 : CASE_CFN_MODF_FN:
15060 : 119694 : CASE_CFN_NEARBYINT:
15061 : 119694 : CASE_CFN_NEARBYINT_FN:
15062 : 119694 : CASE_CFN_RINT:
15063 : 119694 : CASE_CFN_RINT_FN:
15064 : 119694 : CASE_CFN_ROUND:
15065 : 119694 : CASE_CFN_ROUND_FN:
15066 : 119694 : CASE_CFN_ROUNDEVEN:
15067 : 119694 : CASE_CFN_ROUNDEVEN_FN:
15068 : 119694 : CASE_CFN_SCALB:
15069 : 119694 : CASE_CFN_SCALBLN:
15070 : 119694 : CASE_CFN_SCALBLN_FN:
15071 : 119694 : CASE_CFN_SCALBN:
15072 : 119694 : CASE_CFN_SCALBN_FN:
15073 : 119694 : CASE_CFN_SIGNBIT:
15074 : 119694 : CASE_CFN_SIGNIFICAND:
15075 : 119694 : CASE_CFN_SINH:
15076 : 119694 : CASE_CFN_SINH_FN:
15077 : 119694 : CASE_CFN_TANH:
15078 : 119694 : CASE_CFN_TANH_FN:
15079 : 119694 : CASE_CFN_TRUNC:
15080 : 119694 : CASE_CFN_TRUNC_FN:
15081 : : /* True if the 1st argument is nonnegative. */
15082 : 119694 : return RECURSE (arg0);
15083 : :
15084 : 1335 : CASE_CFN_FMAX:
15085 : 1335 : CASE_CFN_FMAX_FN:
15086 : : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15087 : : things. In the presence of sNaNs, we're only guaranteed to be
15088 : : non-negative if both operands are non-negative. In the presence
15089 : : of qNaNs, we're non-negative if either operand is non-negative
15090 : : and can't be a qNaN, or if both operands are non-negative. */
15091 : 1335 : if (tree_expr_maybe_signaling_nan_p (arg0)
15092 : 1335 : || tree_expr_maybe_signaling_nan_p (arg1))
15093 : 136 : return RECURSE (arg0) && RECURSE (arg1);
15094 : 1199 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15095 : 332 : || RECURSE (arg1))
15096 : 867 : : (RECURSE (arg1)
15097 : 867 : && !tree_expr_maybe_nan_p (arg1));
15098 : :
15099 : 946 : CASE_CFN_FMIN:
15100 : 946 : CASE_CFN_FMIN_FN:
15101 : : /* True if the 1st AND 2nd arguments are nonnegative. */
15102 : 946 : return RECURSE (arg0) && RECURSE (arg1);
15103 : :
15104 : 827 : CASE_CFN_COPYSIGN:
15105 : 827 : CASE_CFN_COPYSIGN_FN:
15106 : : /* True if the 2nd argument is nonnegative. */
15107 : 827 : return RECURSE (arg1);
15108 : :
15109 : 2336 : CASE_CFN_POWI:
15110 : : /* True if the 1st argument is nonnegative or the second
15111 : : argument is an even integer. */
15112 : 2336 : if (TREE_CODE (arg1) == INTEGER_CST
15113 : 2336 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15114 : : return true;
15115 : 2255 : return RECURSE (arg0);
15116 : :
15117 : 4893 : CASE_CFN_POW:
15118 : 4893 : CASE_CFN_POW_FN:
15119 : : /* True if the 1st argument is nonnegative or the second
15120 : : argument is an even integer valued real. */
15121 : 4893 : if (TREE_CODE (arg1) == REAL_CST)
15122 : : {
15123 : 2146 : REAL_VALUE_TYPE c;
15124 : 2146 : HOST_WIDE_INT n;
15125 : :
15126 : 2146 : c = TREE_REAL_CST (arg1);
15127 : 2146 : n = real_to_integer (&c);
15128 : 2146 : if ((n & 1) == 0)
15129 : : {
15130 : 1513 : REAL_VALUE_TYPE cint;
15131 : 1513 : real_from_integer (&cint, VOIDmode, n, SIGNED);
15132 : 1513 : if (real_identical (&c, &cint))
15133 : 502 : return true;
15134 : : }
15135 : : }
15136 : 4391 : return RECURSE (arg0);
15137 : :
15138 : 20724556 : default:
15139 : 20724556 : break;
15140 : : }
15141 : 20724556 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15142 : : }
15143 : :
15144 : : /* Return true if T is known to be non-negative. If the return
15145 : : value is based on the assumption that signed overflow is undefined,
15146 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15147 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15148 : :
15149 : : static bool
15150 : 879997 : tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15151 : : {
15152 : 879997 : enum tree_code code = TREE_CODE (t);
15153 : 879997 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15154 : : return true;
15155 : :
15156 : 704374 : switch (code)
15157 : : {
15158 : 203 : case TARGET_EXPR:
15159 : 203 : {
15160 : 203 : tree temp = TARGET_EXPR_SLOT (t);
15161 : 203 : t = TARGET_EXPR_INITIAL (t);
15162 : :
15163 : : /* If the initializer is non-void, then it's a normal expression
15164 : : that will be assigned to the slot. */
15165 : 203 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15166 : 0 : return RECURSE (t);
15167 : :
15168 : : /* Otherwise, the initializer sets the slot in some way. One common
15169 : : way is an assignment statement at the end of the initializer. */
15170 : 405 : while (1)
15171 : : {
15172 : 405 : if (TREE_CODE (t) == BIND_EXPR)
15173 : 202 : t = expr_last (BIND_EXPR_BODY (t));
15174 : 203 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15175 : 203 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15176 : 0 : t = expr_last (TREE_OPERAND (t, 0));
15177 : 203 : else if (TREE_CODE (t) == STATEMENT_LIST)
15178 : 0 : t = expr_last (t);
15179 : : else
15180 : : break;
15181 : : }
15182 : 203 : if (TREE_CODE (t) == MODIFY_EXPR
15183 : 203 : && TREE_OPERAND (t, 0) == temp)
15184 : 202 : return RECURSE (TREE_OPERAND (t, 1));
15185 : :
15186 : : return false;
15187 : : }
15188 : :
15189 : 316687 : case CALL_EXPR:
15190 : 316687 : {
15191 : 316687 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15192 : 316687 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15193 : :
15194 : 316687 : return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15195 : : get_call_combined_fn (t),
15196 : : arg0,
15197 : : arg1,
15198 : 316687 : strict_overflow_p, depth);
15199 : : }
15200 : 767 : case COMPOUND_EXPR:
15201 : 767 : case MODIFY_EXPR:
15202 : 767 : return RECURSE (TREE_OPERAND (t, 1));
15203 : :
15204 : 9 : case BIND_EXPR:
15205 : 9 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15206 : :
15207 : 385042 : case SAVE_EXPR:
15208 : 385042 : return RECURSE (TREE_OPERAND (t, 0));
15209 : :
15210 : 1666 : default:
15211 : 1666 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15212 : : }
15213 : : }
15214 : :
15215 : : #undef RECURSE
15216 : : #undef tree_expr_nonnegative_warnv_p
15217 : :
15218 : : /* Return true if T is known to be non-negative. If the return
15219 : : value is based on the assumption that signed overflow is undefined,
15220 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15221 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15222 : :
15223 : : bool
15224 : 24458522 : tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15225 : : {
15226 : 24458522 : enum tree_code code;
15227 : 24458522 : if (error_operand_p (t))
15228 : : return false;
15229 : :
15230 : 24458521 : code = TREE_CODE (t);
15231 : 24458521 : switch (TREE_CODE_CLASS (code))
15232 : : {
15233 : 1033964 : case tcc_binary:
15234 : 1033964 : case tcc_comparison:
15235 : 1033964 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15236 : 1033964 : TREE_TYPE (t),
15237 : 1033964 : TREE_OPERAND (t, 0),
15238 : 1033964 : TREE_OPERAND (t, 1),
15239 : 1033964 : strict_overflow_p, depth);
15240 : :
15241 : 1592397 : case tcc_unary:
15242 : 1592397 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15243 : 1592397 : TREE_TYPE (t),
15244 : 1592397 : TREE_OPERAND (t, 0),
15245 : 1592397 : strict_overflow_p, depth);
15246 : :
15247 : 11490750 : case tcc_constant:
15248 : 11490750 : case tcc_declaration:
15249 : 11490750 : case tcc_reference:
15250 : 11490750 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15251 : :
15252 : 10341410 : default:
15253 : 10341410 : break;
15254 : : }
15255 : :
15256 : 10341410 : switch (code)
15257 : : {
15258 : 7 : case TRUTH_AND_EXPR:
15259 : 7 : case TRUTH_OR_EXPR:
15260 : 7 : case TRUTH_XOR_EXPR:
15261 : 7 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15262 : 7 : TREE_TYPE (t),
15263 : 7 : TREE_OPERAND (t, 0),
15264 : 7 : TREE_OPERAND (t, 1),
15265 : 7 : strict_overflow_p, depth);
15266 : 72 : case TRUTH_NOT_EXPR:
15267 : 72 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15268 : 72 : TREE_TYPE (t),
15269 : 72 : TREE_OPERAND (t, 0),
15270 : 72 : strict_overflow_p, depth);
15271 : :
15272 : 9461334 : case COND_EXPR:
15273 : 9461334 : case CONSTRUCTOR:
15274 : 9461334 : case OBJ_TYPE_REF:
15275 : 9461334 : case ADDR_EXPR:
15276 : 9461334 : case WITH_SIZE_EXPR:
15277 : 9461334 : case SSA_NAME:
15278 : 9461334 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15279 : :
15280 : 879997 : default:
15281 : 879997 : return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15282 : : }
15283 : : }
15284 : :
15285 : : /* Return true if `t' is known to be non-negative. Handle warnings
15286 : : about undefined signed overflow. */
15287 : :
15288 : : bool
15289 : 16891334 : tree_expr_nonnegative_p (tree t)
15290 : : {
15291 : 16891334 : bool ret, strict_overflow_p;
15292 : :
15293 : 16891334 : strict_overflow_p = false;
15294 : 16891334 : ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15295 : 16891334 : if (strict_overflow_p)
15296 : 17647 : fold_overflow_warning (("assuming signed overflow does not occur when "
15297 : : "determining that expression is always "
15298 : : "non-negative"),
15299 : : WARN_STRICT_OVERFLOW_MISC);
15300 : 16891334 : return ret;
15301 : : }
15302 : :
15303 : :
15304 : : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15305 : : For floating point we further ensure that T is not denormal.
15306 : : Similar logic is present in nonzero_address in rtlanal.h.
15307 : :
15308 : : If the return value is based on the assumption that signed overflow
15309 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15310 : : change *STRICT_OVERFLOW_P. */
15311 : :
15312 : : bool
15313 : 1387953 : tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15314 : : bool *strict_overflow_p)
15315 : : {
15316 : 1387953 : switch (code)
15317 : : {
15318 : 1 : case ABS_EXPR:
15319 : 1 : return tree_expr_nonzero_warnv_p (op0,
15320 : 1 : strict_overflow_p);
15321 : :
15322 : 749022 : case NOP_EXPR:
15323 : 749022 : {
15324 : 749022 : tree inner_type = TREE_TYPE (op0);
15325 : 749022 : tree outer_type = type;
15326 : :
15327 : 749022 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15328 : 749022 : && tree_expr_nonzero_warnv_p (op0,
15329 : : strict_overflow_p));
15330 : : }
15331 : 28109 : break;
15332 : :
15333 : 28109 : case NON_LVALUE_EXPR:
15334 : 28109 : return tree_expr_nonzero_warnv_p (op0,
15335 : 28109 : strict_overflow_p);
15336 : :
15337 : : default:
15338 : : break;
15339 : : }
15340 : :
15341 : : return false;
15342 : : }
15343 : :
15344 : : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15345 : : For floating point we further ensure that T is not denormal.
15346 : : Similar logic is present in nonzero_address in rtlanal.h.
15347 : :
15348 : : If the return value is based on the assumption that signed overflow
15349 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15350 : : change *STRICT_OVERFLOW_P. */
15351 : :
15352 : : bool
15353 : 2901960 : tree_binary_nonzero_warnv_p (enum tree_code code,
15354 : : tree type,
15355 : : tree op0,
15356 : : tree op1, bool *strict_overflow_p)
15357 : : {
15358 : 2901960 : bool sub_strict_overflow_p;
15359 : 2901960 : switch (code)
15360 : : {
15361 : 449795 : case POINTER_PLUS_EXPR:
15362 : 449795 : case PLUS_EXPR:
15363 : 449795 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15364 : : {
15365 : : /* With the presence of negative values it is hard
15366 : : to say something. */
15367 : 101805 : sub_strict_overflow_p = false;
15368 : 101805 : if (!tree_expr_nonnegative_warnv_p (op0,
15369 : : &sub_strict_overflow_p)
15370 : 101805 : || !tree_expr_nonnegative_warnv_p (op1,
15371 : : &sub_strict_overflow_p))
15372 : 99464 : return false;
15373 : : /* One of operands must be positive and the other non-negative. */
15374 : : /* We don't set *STRICT_OVERFLOW_P here: even if this value
15375 : : overflows, on a twos-complement machine the sum of two
15376 : : nonnegative numbers can never be zero. */
15377 : 2341 : return (tree_expr_nonzero_warnv_p (op0,
15378 : : strict_overflow_p)
15379 : 2341 : || tree_expr_nonzero_warnv_p (op1,
15380 : : strict_overflow_p));
15381 : : }
15382 : : break;
15383 : :
15384 : 21059 : case MULT_EXPR:
15385 : 21059 : if (TYPE_OVERFLOW_UNDEFINED (type))
15386 : : {
15387 : 443 : if (tree_expr_nonzero_warnv_p (op0,
15388 : : strict_overflow_p)
15389 : 443 : && tree_expr_nonzero_warnv_p (op1,
15390 : : strict_overflow_p))
15391 : : {
15392 : 0 : *strict_overflow_p = true;
15393 : 0 : return true;
15394 : : }
15395 : : }
15396 : : break;
15397 : :
15398 : 12318 : case MIN_EXPR:
15399 : 12318 : sub_strict_overflow_p = false;
15400 : 12318 : if (tree_expr_nonzero_warnv_p (op0,
15401 : : &sub_strict_overflow_p)
15402 : 12318 : && tree_expr_nonzero_warnv_p (op1,
15403 : : &sub_strict_overflow_p))
15404 : : {
15405 : 0 : if (sub_strict_overflow_p)
15406 : 0 : *strict_overflow_p = true;
15407 : : }
15408 : : break;
15409 : :
15410 : 44 : case MAX_EXPR:
15411 : 44 : sub_strict_overflow_p = false;
15412 : 44 : if (tree_expr_nonzero_warnv_p (op0,
15413 : : &sub_strict_overflow_p))
15414 : : {
15415 : 0 : if (sub_strict_overflow_p)
15416 : 0 : *strict_overflow_p = true;
15417 : :
15418 : : /* When both operands are nonzero, then MAX must be too. */
15419 : 0 : if (tree_expr_nonzero_warnv_p (op1,
15420 : : strict_overflow_p))
15421 : : return true;
15422 : :
15423 : : /* MAX where operand 0 is positive is positive. */
15424 : 0 : return tree_expr_nonnegative_warnv_p (op0,
15425 : 0 : strict_overflow_p);
15426 : : }
15427 : : /* MAX where operand 1 is positive is positive. */
15428 : 44 : else if (tree_expr_nonzero_warnv_p (op1,
15429 : : &sub_strict_overflow_p)
15430 : 44 : && tree_expr_nonnegative_warnv_p (op1,
15431 : : &sub_strict_overflow_p))
15432 : : {
15433 : 0 : if (sub_strict_overflow_p)
15434 : 0 : *strict_overflow_p = true;
15435 : 0 : return true;
15436 : : }
15437 : : break;
15438 : :
15439 : 273474 : case BIT_IOR_EXPR:
15440 : 273474 : return (tree_expr_nonzero_warnv_p (op1,
15441 : : strict_overflow_p)
15442 : 273474 : || tree_expr_nonzero_warnv_p (op0,
15443 : : strict_overflow_p));
15444 : :
15445 : : default:
15446 : : break;
15447 : : }
15448 : :
15449 : : return false;
15450 : : }
15451 : :
15452 : : /* Return true when T is an address and is known to be nonzero.
15453 : : For floating point we further ensure that T is not denormal.
15454 : : Similar logic is present in nonzero_address in rtlanal.h.
15455 : :
15456 : : If the return value is based on the assumption that signed overflow
15457 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15458 : : change *STRICT_OVERFLOW_P. */
15459 : :
15460 : : bool
15461 : 145195427 : tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15462 : : {
15463 : 145195427 : bool sub_strict_overflow_p;
15464 : 145195427 : switch (TREE_CODE (t))
15465 : : {
15466 : 1121386 : case INTEGER_CST:
15467 : 1121386 : return !integer_zerop (t);
15468 : :
15469 : 10788784 : case ADDR_EXPR:
15470 : 10788784 : {
15471 : 10788784 : tree base = TREE_OPERAND (t, 0);
15472 : :
15473 : 10788784 : if (!DECL_P (base))
15474 : 5560845 : base = get_base_address (base);
15475 : :
15476 : 10788784 : if (base && TREE_CODE (base) == TARGET_EXPR)
15477 : 714 : base = TARGET_EXPR_SLOT (base);
15478 : :
15479 : 714 : if (!base)
15480 : 0 : return false;
15481 : :
15482 : : /* For objects in symbol table check if we know they are non-zero.
15483 : : Don't do anything for variables and functions before symtab is built;
15484 : : it is quite possible that they will be declared weak later. */
15485 : 10788784 : int nonzero_addr = maybe_nonzero_address (base);
15486 : 10788784 : if (nonzero_addr >= 0)
15487 : 8475087 : return nonzero_addr;
15488 : :
15489 : : /* Constants are never weak. */
15490 : 2313697 : if (CONSTANT_CLASS_P (base))
15491 : : return true;
15492 : :
15493 : : return false;
15494 : : }
15495 : :
15496 : 30725 : case COND_EXPR:
15497 : 30725 : sub_strict_overflow_p = false;
15498 : 30725 : if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15499 : : &sub_strict_overflow_p)
15500 : 30725 : && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15501 : : &sub_strict_overflow_p))
15502 : : {
15503 : 1225 : if (sub_strict_overflow_p)
15504 : 0 : *strict_overflow_p = true;
15505 : 1225 : return true;
15506 : : }
15507 : : break;
15508 : :
15509 : 122654048 : case SSA_NAME:
15510 : 122654048 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15511 : : break;
15512 : 95777315 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15513 : :
15514 : : default:
15515 : : break;
15516 : : }
15517 : : return false;
15518 : : }
15519 : :
15520 : : #define integer_valued_real_p(X) \
15521 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15522 : :
15523 : : #define RECURSE(X) \
15524 : : ((integer_valued_real_p) (X, depth + 1))
15525 : :
15526 : : /* Return true if the floating point result of (CODE OP0) has an
15527 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15528 : : integer values. Return false for signaling NaN.
15529 : :
15530 : : DEPTH is the current nesting depth of the query. */
15531 : :
15532 : : bool
15533 : 15030 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15534 : : {
15535 : 15030 : switch (code)
15536 : : {
15537 : : case FLOAT_EXPR:
15538 : : return true;
15539 : :
15540 : 1413 : case ABS_EXPR:
15541 : 1413 : return RECURSE (op0);
15542 : :
15543 : 9926 : CASE_CONVERT:
15544 : 9926 : {
15545 : 9926 : tree type = TREE_TYPE (op0);
15546 : 9926 : if (TREE_CODE (type) == INTEGER_TYPE)
15547 : : return true;
15548 : 9926 : if (SCALAR_FLOAT_TYPE_P (type))
15549 : 9926 : return RECURSE (op0);
15550 : : break;
15551 : : }
15552 : :
15553 : : default:
15554 : : break;
15555 : : }
15556 : : return false;
15557 : : }
15558 : :
15559 : : /* Return true if the floating point result of (CODE OP0 OP1) has an
15560 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15561 : : integer values. Return false for signaling NaN.
15562 : :
15563 : : DEPTH is the current nesting depth of the query. */
15564 : :
15565 : : bool
15566 : 13641 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15567 : : {
15568 : 13641 : switch (code)
15569 : : {
15570 : 7159 : case PLUS_EXPR:
15571 : 7159 : case MINUS_EXPR:
15572 : 7159 : case MULT_EXPR:
15573 : 7159 : case MIN_EXPR:
15574 : 7159 : case MAX_EXPR:
15575 : 7159 : return RECURSE (op0) && RECURSE (op1);
15576 : :
15577 : : default:
15578 : : break;
15579 : : }
15580 : : return false;
15581 : : }
15582 : :
15583 : : /* Return true if the floating point result of calling FNDECL with arguments
15584 : : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15585 : : considered integer values. Return false for signaling NaN. If FNDECL
15586 : : takes fewer than 2 arguments, the remaining ARGn are null.
15587 : :
15588 : : DEPTH is the current nesting depth of the query. */
15589 : :
15590 : : bool
15591 : 874 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15592 : : {
15593 : 874 : switch (fn)
15594 : : {
15595 : : CASE_CFN_CEIL:
15596 : : CASE_CFN_CEIL_FN:
15597 : : CASE_CFN_FLOOR:
15598 : : CASE_CFN_FLOOR_FN:
15599 : : CASE_CFN_NEARBYINT:
15600 : : CASE_CFN_NEARBYINT_FN:
15601 : : CASE_CFN_RINT:
15602 : : CASE_CFN_RINT_FN:
15603 : : CASE_CFN_ROUND:
15604 : : CASE_CFN_ROUND_FN:
15605 : : CASE_CFN_ROUNDEVEN:
15606 : : CASE_CFN_ROUNDEVEN_FN:
15607 : : CASE_CFN_TRUNC:
15608 : : CASE_CFN_TRUNC_FN:
15609 : : return true;
15610 : :
15611 : 336 : CASE_CFN_FMIN:
15612 : 336 : CASE_CFN_FMIN_FN:
15613 : 336 : CASE_CFN_FMAX:
15614 : 336 : CASE_CFN_FMAX_FN:
15615 : 336 : return RECURSE (arg0) && RECURSE (arg1);
15616 : :
15617 : : default:
15618 : : break;
15619 : : }
15620 : : return false;
15621 : : }
15622 : :
15623 : : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15624 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15625 : : considered integer values. Return false for signaling NaN.
15626 : :
15627 : : DEPTH is the current nesting depth of the query. */
15628 : :
15629 : : bool
15630 : 129205 : integer_valued_real_single_p (tree t, int depth)
15631 : : {
15632 : 129205 : switch (TREE_CODE (t))
15633 : : {
15634 : 2226 : case REAL_CST:
15635 : 2226 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15636 : :
15637 : 0 : case COND_EXPR:
15638 : 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15639 : :
15640 : 90542 : case SSA_NAME:
15641 : : /* Limit the depth of recursion to avoid quadratic behavior.
15642 : : This is expected to catch almost all occurrences in practice.
15643 : : If this code misses important cases that unbounded recursion
15644 : : would not, passes that need this information could be revised
15645 : : to provide it through dataflow propagation. */
15646 : 90542 : return (!name_registered_for_update_p (t)
15647 : 90542 : && depth < param_max_ssa_name_query_depth
15648 : 180305 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15649 : : depth));
15650 : :
15651 : : default:
15652 : : break;
15653 : : }
15654 : : return false;
15655 : : }
15656 : :
15657 : : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15658 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15659 : : considered integer values. Return false for signaling NaN.
15660 : :
15661 : : DEPTH is the current nesting depth of the query. */
15662 : :
15663 : : static bool
15664 : 0 : integer_valued_real_invalid_p (tree t, int depth)
15665 : : {
15666 : 0 : switch (TREE_CODE (t))
15667 : : {
15668 : 0 : case COMPOUND_EXPR:
15669 : 0 : case MODIFY_EXPR:
15670 : 0 : case BIND_EXPR:
15671 : 0 : return RECURSE (TREE_OPERAND (t, 1));
15672 : :
15673 : 0 : case SAVE_EXPR:
15674 : 0 : return RECURSE (TREE_OPERAND (t, 0));
15675 : :
15676 : : default:
15677 : : break;
15678 : : }
15679 : : return false;
15680 : : }
15681 : :
15682 : : #undef RECURSE
15683 : : #undef integer_valued_real_p
15684 : :
15685 : : /* Return true if the floating point expression T has an integer value.
15686 : : We also allow +Inf, -Inf and NaN to be considered integer values.
15687 : : Return false for signaling NaN.
15688 : :
15689 : : DEPTH is the current nesting depth of the query. */
15690 : :
15691 : : bool
15692 : 97413 : integer_valued_real_p (tree t, int depth)
15693 : : {
15694 : 97413 : if (t == error_mark_node)
15695 : : return false;
15696 : :
15697 : 97413 : STRIP_ANY_LOCATION_WRAPPER (t);
15698 : :
15699 : 97413 : tree_code code = TREE_CODE (t);
15700 : 97413 : switch (TREE_CODE_CLASS (code))
15701 : : {
15702 : 0 : case tcc_binary:
15703 : 0 : case tcc_comparison:
15704 : 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15705 : 0 : TREE_OPERAND (t, 1), depth);
15706 : :
15707 : 0 : case tcc_unary:
15708 : 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15709 : :
15710 : 8338 : case tcc_constant:
15711 : 8338 : case tcc_declaration:
15712 : 8338 : case tcc_reference:
15713 : 8338 : return integer_valued_real_single_p (t, depth);
15714 : :
15715 : 89075 : default:
15716 : 89075 : break;
15717 : : }
15718 : :
15719 : 89075 : switch (code)
15720 : : {
15721 : 89075 : case COND_EXPR:
15722 : 89075 : case SSA_NAME:
15723 : 89075 : return integer_valued_real_single_p (t, depth);
15724 : :
15725 : 0 : case CALL_EXPR:
15726 : 0 : {
15727 : 0 : tree arg0 = (call_expr_nargs (t) > 0
15728 : 0 : ? CALL_EXPR_ARG (t, 0)
15729 : 0 : : NULL_TREE);
15730 : 0 : tree arg1 = (call_expr_nargs (t) > 1
15731 : 0 : ? CALL_EXPR_ARG (t, 1)
15732 : 0 : : NULL_TREE);
15733 : 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15734 : 0 : arg0, arg1, depth);
15735 : : }
15736 : :
15737 : 0 : default:
15738 : 0 : return integer_valued_real_invalid_p (t, depth);
15739 : : }
15740 : : }
15741 : :
15742 : : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15743 : : attempt to fold the expression to a constant without modifying TYPE,
15744 : : OP0 or OP1.
15745 : :
15746 : : If the expression could be simplified to a constant, then return
15747 : : the constant. If the expression would not be simplified to a
15748 : : constant, then return NULL_TREE. */
15749 : :
15750 : : tree
15751 : 15234814 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15752 : : {
15753 : 15234814 : tree tem = fold_binary (code, type, op0, op1);
15754 : 15234814 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15755 : : }
15756 : :
15757 : : /* Given the components of a unary expression CODE, TYPE and OP0,
15758 : : attempt to fold the expression to a constant without modifying
15759 : : TYPE or OP0.
15760 : :
15761 : : If the expression could be simplified to a constant, then return
15762 : : the constant. If the expression would not be simplified to a
15763 : : constant, then return NULL_TREE. */
15764 : :
15765 : : tree
15766 : 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15767 : : {
15768 : 0 : tree tem = fold_unary (code, type, op0);
15769 : 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15770 : : }
15771 : :
15772 : : /* If EXP represents referencing an element in a constant string
15773 : : (either via pointer arithmetic or array indexing), return the
15774 : : tree representing the value accessed, otherwise return NULL. */
15775 : :
15776 : : tree
15777 : 157719103 : fold_read_from_constant_string (tree exp)
15778 : : {
15779 : 157719103 : if ((INDIRECT_REF_P (exp)
15780 : 157719085 : || TREE_CODE (exp) == ARRAY_REF)
15781 : 168747196 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15782 : : {
15783 : 7779828 : tree exp1 = TREE_OPERAND (exp, 0);
15784 : 7779828 : tree index;
15785 : 7779828 : tree string;
15786 : 7779828 : location_t loc = EXPR_LOCATION (exp);
15787 : :
15788 : 7779828 : if (INDIRECT_REF_P (exp))
15789 : 0 : string = string_constant (exp1, &index, NULL, NULL);
15790 : : else
15791 : : {
15792 : 7779828 : tree low_bound = array_ref_low_bound (exp);
15793 : 7779828 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15794 : :
15795 : : /* Optimize the special-case of a zero lower bound.
15796 : :
15797 : : We convert the low_bound to sizetype to avoid some problems
15798 : : with constant folding. (E.g. suppose the lower bound is 1,
15799 : : and its mode is QI. Without the conversion,l (ARRAY
15800 : : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15801 : : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15802 : 7779828 : if (! integer_zerop (low_bound))
15803 : 139845 : index = size_diffop_loc (loc, index,
15804 : : fold_convert_loc (loc, sizetype, low_bound));
15805 : :
15806 : : string = exp1;
15807 : : }
15808 : :
15809 : 7779828 : scalar_int_mode char_mode;
15810 : 7779828 : if (string
15811 : 7779828 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15812 : 7779828 : && TREE_CODE (string) == STRING_CST
15813 : 59946 : && tree_fits_uhwi_p (index)
15814 : 55810 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15815 : 7835532 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15816 : : &char_mode)
15817 : 15559656 : && GET_MODE_SIZE (char_mode) == 1)
15818 : 108572 : return build_int_cst_type (TREE_TYPE (exp),
15819 : 54286 : (TREE_STRING_POINTER (string)
15820 : 54286 : [TREE_INT_CST_LOW (index)]));
15821 : : }
15822 : : return NULL;
15823 : : }
15824 : :
15825 : : /* Folds a read from vector element at IDX of vector ARG. */
15826 : :
15827 : : tree
15828 : 5258 : fold_read_from_vector (tree arg, poly_uint64 idx)
15829 : : {
15830 : 5258 : unsigned HOST_WIDE_INT i;
15831 : 5258 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15832 : 5258 : && known_ge (idx, 0u)
15833 : 5258 : && idx.is_constant (&i))
15834 : : {
15835 : 5258 : if (TREE_CODE (arg) == VECTOR_CST)
15836 : 1938 : return VECTOR_CST_ELT (arg, i);
15837 : 3320 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15838 : : {
15839 : 1565 : if (CONSTRUCTOR_NELTS (arg)
15840 : 1525 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15841 : : return NULL_TREE;
15842 : 1563 : if (i >= CONSTRUCTOR_NELTS (arg))
15843 : 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15844 : 1523 : return CONSTRUCTOR_ELT (arg, i)->value;
15845 : : }
15846 : : }
15847 : : return NULL_TREE;
15848 : : }
15849 : :
15850 : : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15851 : : an integer constant, real, or fixed-point constant.
15852 : :
15853 : : TYPE is the type of the result. */
15854 : :
15855 : : static tree
15856 : 30308065 : fold_negate_const (tree arg0, tree type)
15857 : : {
15858 : 30308065 : tree t = NULL_TREE;
15859 : :
15860 : 30308065 : switch (TREE_CODE (arg0))
15861 : : {
15862 : 2048129 : case REAL_CST:
15863 : 2048129 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15864 : 2048129 : break;
15865 : :
15866 : 0 : case FIXED_CST:
15867 : 0 : {
15868 : 0 : FIXED_VALUE_TYPE f;
15869 : 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15870 : 0 : &(TREE_FIXED_CST (arg0)), NULL,
15871 : 0 : TYPE_SATURATING (type));
15872 : 0 : t = build_fixed (type, f);
15873 : : /* Propagate overflow flags. */
15874 : 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15875 : 0 : TREE_OVERFLOW (t) = 1;
15876 : 0 : break;
15877 : : }
15878 : :
15879 : 28259936 : default:
15880 : 28259936 : if (poly_int_tree_p (arg0))
15881 : : {
15882 : 28259936 : wi::overflow_type overflow;
15883 : 28259936 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15884 : 28259936 : t = force_fit_type (type, res, 1,
15885 : 223015 : (overflow && ! TYPE_UNSIGNED (type))
15886 : 28474123 : || TREE_OVERFLOW (arg0));
15887 : 28259936 : break;
15888 : 28259936 : }
15889 : :
15890 : 0 : gcc_unreachable ();
15891 : : }
15892 : :
15893 : 30308065 : return t;
15894 : : }
15895 : :
15896 : : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15897 : : an integer constant or real constant.
15898 : :
15899 : : TYPE is the type of the result. */
15900 : :
15901 : : tree
15902 : 34515 : fold_abs_const (tree arg0, tree type)
15903 : : {
15904 : 34515 : tree t = NULL_TREE;
15905 : :
15906 : 34515 : switch (TREE_CODE (arg0))
15907 : : {
15908 : 7046 : case INTEGER_CST:
15909 : 7046 : {
15910 : : /* If the value is unsigned or non-negative, then the absolute value
15911 : : is the same as the ordinary value. */
15912 : 7046 : wide_int val = wi::to_wide (arg0);
15913 : 7046 : wi::overflow_type overflow = wi::OVF_NONE;
15914 : 7046 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15915 : : ;
15916 : :
15917 : : /* If the value is negative, then the absolute value is
15918 : : its negation. */
15919 : : else
15920 : 3546 : val = wi::neg (val, &overflow);
15921 : :
15922 : : /* Force to the destination type, set TREE_OVERFLOW for signed
15923 : : TYPE only. */
15924 : 7046 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15925 : 7046 : }
15926 : 7046 : break;
15927 : :
15928 : 27469 : case REAL_CST:
15929 : 27469 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15930 : 7322 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15931 : : else
15932 : : t = arg0;
15933 : : break;
15934 : :
15935 : 0 : default:
15936 : 0 : gcc_unreachable ();
15937 : : }
15938 : :
15939 : 34515 : return t;
15940 : : }
15941 : :
15942 : : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15943 : : constant. TYPE is the type of the result. */
15944 : :
15945 : : static tree
15946 : 2290841 : fold_not_const (const_tree arg0, tree type)
15947 : : {
15948 : 2290841 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15949 : :
15950 : 2290841 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15951 : : }
15952 : :
15953 : : /* Given CODE, a relational operator, the target type, TYPE and two
15954 : : constant operands OP0 and OP1, return the result of the
15955 : : relational operation. If the result is not a compile time
15956 : : constant, then return NULL_TREE. */
15957 : :
15958 : : static tree
15959 : 55598330 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15960 : : {
15961 : 55598330 : int result, invert;
15962 : :
15963 : : /* From here on, the only cases we handle are when the result is
15964 : : known to be a constant. */
15965 : :
15966 : 55598330 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15967 : : {
15968 : 1107443 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15969 : 1107443 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15970 : :
15971 : : /* Handle the cases where either operand is a NaN. */
15972 : 1107443 : if (real_isnan (c0) || real_isnan (c1))
15973 : : {
15974 : 14599 : switch (code)
15975 : : {
15976 : : case EQ_EXPR:
15977 : : case ORDERED_EXPR:
15978 : : result = 0;
15979 : : break;
15980 : :
15981 : : case NE_EXPR:
15982 : : case UNORDERED_EXPR:
15983 : : case UNLT_EXPR:
15984 : : case UNLE_EXPR:
15985 : : case UNGT_EXPR:
15986 : : case UNGE_EXPR:
15987 : : case UNEQ_EXPR:
15988 : 6593 : result = 1;
15989 : : break;
15990 : :
15991 : 8028 : case LT_EXPR:
15992 : 8028 : case LE_EXPR:
15993 : 8028 : case GT_EXPR:
15994 : 8028 : case GE_EXPR:
15995 : 8028 : case LTGT_EXPR:
15996 : 8028 : if (flag_trapping_math)
15997 : : return NULL_TREE;
15998 : : result = 0;
15999 : : break;
16000 : :
16001 : 0 : default:
16002 : 0 : gcc_unreachable ();
16003 : : }
16004 : :
16005 : 6593 : return constant_boolean_node (result, type);
16006 : : }
16007 : :
16008 : 1092844 : return constant_boolean_node (real_compare (code, c0, c1), type);
16009 : : }
16010 : :
16011 : 54490887 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
16012 : : {
16013 : 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16014 : 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16015 : 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
16016 : : }
16017 : :
16018 : : /* Handle equality/inequality of complex constants. */
16019 : 54490887 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16020 : : {
16021 : 58276 : tree rcond = fold_relational_const (code, type,
16022 : 29138 : TREE_REALPART (op0),
16023 : 29138 : TREE_REALPART (op1));
16024 : 116552 : tree icond = fold_relational_const (code, type,
16025 : 29138 : TREE_IMAGPART (op0),
16026 : 29138 : TREE_IMAGPART (op1));
16027 : 29138 : if (code == EQ_EXPR)
16028 : 286 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16029 : 28852 : else if (code == NE_EXPR)
16030 : 28852 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16031 : : else
16032 : : return NULL_TREE;
16033 : : }
16034 : :
16035 : 54461749 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16036 : : {
16037 : 4936 : if (!VECTOR_TYPE_P (type))
16038 : : {
16039 : : /* Have vector comparison with scalar boolean result. */
16040 : 142 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
16041 : : && known_eq (VECTOR_CST_NELTS (op0),
16042 : : VECTOR_CST_NELTS (op1)));
16043 : 142 : unsigned HOST_WIDE_INT nunits;
16044 : 142 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
16045 : : return NULL_TREE;
16046 : 805 : for (unsigned i = 0; i < nunits; i++)
16047 : : {
16048 : 718 : tree elem0 = VECTOR_CST_ELT (op0, i);
16049 : 718 : tree elem1 = VECTOR_CST_ELT (op1, i);
16050 : 718 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
16051 : 718 : if (tmp == NULL_TREE)
16052 : : return NULL_TREE;
16053 : 718 : if (integer_zerop (tmp))
16054 : 55 : return constant_boolean_node (code == NE_EXPR, type);
16055 : : }
16056 : 87 : return constant_boolean_node (code == EQ_EXPR, type);
16057 : : }
16058 : 4794 : tree_vector_builder elts;
16059 : 4794 : if (!elts.new_binary_operation (type, op0, op1, false))
16060 : : return NULL_TREE;
16061 : 4794 : unsigned int count = elts.encoded_nelts ();
16062 : 22360 : for (unsigned i = 0; i < count; i++)
16063 : : {
16064 : 17566 : tree elem_type = TREE_TYPE (type);
16065 : 17566 : tree elem0 = VECTOR_CST_ELT (op0, i);
16066 : 17566 : tree elem1 = VECTOR_CST_ELT (op1, i);
16067 : :
16068 : 17566 : tree tem = fold_relational_const (code, elem_type,
16069 : : elem0, elem1);
16070 : :
16071 : 17566 : if (tem == NULL_TREE)
16072 : : return NULL_TREE;
16073 : :
16074 : 17566 : elts.quick_push (build_int_cst (elem_type,
16075 : 24851 : integer_zerop (tem) ? 0 : -1));
16076 : : }
16077 : :
16078 : 4794 : return elts.build ();
16079 : 4794 : }
16080 : :
16081 : : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16082 : :
16083 : : To compute GT, swap the arguments and do LT.
16084 : : To compute GE, do LT and invert the result.
16085 : : To compute LE, swap the arguments, do LT and invert the result.
16086 : : To compute NE, do EQ and invert the result.
16087 : :
16088 : : Therefore, the code below must handle only EQ and LT. */
16089 : :
16090 : 54456813 : if (code == LE_EXPR || code == GT_EXPR)
16091 : : {
16092 : 10875302 : std::swap (op0, op1);
16093 : 10875302 : code = swap_tree_comparison (code);
16094 : : }
16095 : :
16096 : : /* Note that it is safe to invert for real values here because we
16097 : : have already handled the one case that it matters. */
16098 : :
16099 : 54456813 : invert = 0;
16100 : 54456813 : if (code == NE_EXPR || code == GE_EXPR)
16101 : : {
16102 : 27190567 : invert = 1;
16103 : 27190567 : code = invert_tree_comparison (code, false);
16104 : : }
16105 : :
16106 : : /* Compute a result for LT or EQ if args permit;
16107 : : Otherwise return T. */
16108 : 54456813 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16109 : : {
16110 : 54437133 : if (code == EQ_EXPR)
16111 : 28178900 : result = tree_int_cst_equal (op0, op1);
16112 : : else
16113 : 26258233 : result = tree_int_cst_lt (op0, op1);
16114 : : }
16115 : : else
16116 : : return NULL_TREE;
16117 : :
16118 : 54437133 : if (invert)
16119 : 27188419 : result ^= 1;
16120 : 54437133 : return constant_boolean_node (result, type);
16121 : : }
16122 : :
16123 : : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16124 : : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16125 : : itself. */
16126 : :
16127 : : tree
16128 : 115995292 : fold_build_cleanup_point_expr (tree type, tree expr)
16129 : : {
16130 : : /* If the expression does not have side effects then we don't have to wrap
16131 : : it with a cleanup point expression. */
16132 : 115995292 : if (!TREE_SIDE_EFFECTS (expr))
16133 : : return expr;
16134 : :
16135 : : /* If the expression is a return, check to see if the expression inside the
16136 : : return has no side effects or the right hand side of the modify expression
16137 : : inside the return. If either don't have side effects set we don't need to
16138 : : wrap the expression in a cleanup point expression. Note we don't check the
16139 : : left hand side of the modify because it should always be a return decl. */
16140 : 100748827 : if (TREE_CODE (expr) == RETURN_EXPR)
16141 : : {
16142 : 36201845 : tree op = TREE_OPERAND (expr, 0);
16143 : 36201845 : if (!op || !TREE_SIDE_EFFECTS (op))
16144 : : return expr;
16145 : 35537123 : op = TREE_OPERAND (op, 1);
16146 : 35537123 : if (!TREE_SIDE_EFFECTS (op))
16147 : : return expr;
16148 : : }
16149 : :
16150 : 81869365 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16151 : : }
16152 : :
16153 : : /* Given a pointer value OP0 and a type TYPE, return a simplified version
16154 : : of an indirection through OP0, or NULL_TREE if no simplification is
16155 : : possible. */
16156 : :
16157 : : tree
16158 : 17782496 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16159 : : {
16160 : 17782496 : tree sub = op0;
16161 : 17782496 : tree subtype;
16162 : 17782496 : poly_uint64 const_op01;
16163 : :
16164 : 17782496 : STRIP_NOPS (sub);
16165 : 17782496 : subtype = TREE_TYPE (sub);
16166 : 17782496 : if (!POINTER_TYPE_P (subtype)
16167 : 17782496 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16168 : : return NULL_TREE;
16169 : :
16170 : 17634838 : if (TREE_CODE (sub) == ADDR_EXPR)
16171 : : {
16172 : 3157135 : tree op = TREE_OPERAND (sub, 0);
16173 : 3157135 : tree optype = TREE_TYPE (op);
16174 : :
16175 : : /* *&CONST_DECL -> to the value of the const decl. */
16176 : 3157135 : if (TREE_CODE (op) == CONST_DECL)
16177 : 2702 : return DECL_INITIAL (op);
16178 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
16179 : 3154433 : if (type == optype)
16180 : : {
16181 : 2278128 : tree fop = fold_read_from_constant_string (op);
16182 : 2278128 : if (fop)
16183 : : return fop;
16184 : : else
16185 : 2236155 : return op;
16186 : : }
16187 : : /* *(foo *)&fooarray => fooarray[0] */
16188 : 876305 : else if (TREE_CODE (optype) == ARRAY_TYPE
16189 : 13028 : && type == TREE_TYPE (optype)
16190 : 888173 : && (!in_gimple_form
16191 : 2571 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16192 : : {
16193 : 11868 : tree type_domain = TYPE_DOMAIN (optype);
16194 : 11868 : tree min_val = size_zero_node;
16195 : 11868 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16196 : 11830 : min_val = TYPE_MIN_VALUE (type_domain);
16197 : 11868 : if (in_gimple_form
16198 : 2571 : && TREE_CODE (min_val) != INTEGER_CST)
16199 : : return NULL_TREE;
16200 : 11868 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16201 : 11868 : NULL_TREE, NULL_TREE);
16202 : : }
16203 : : /* *(foo *)&complexfoo => __real__ complexfoo */
16204 : 864437 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16205 : 864437 : && type == TREE_TYPE (optype))
16206 : 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16207 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16208 : 864437 : else if (VECTOR_TYPE_P (optype)
16209 : 864437 : && type == TREE_TYPE (optype))
16210 : : {
16211 : 70 : tree part_width = TYPE_SIZE (type);
16212 : 70 : tree index = bitsize_int (0);
16213 : 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16214 : 70 : index);
16215 : : }
16216 : : }
16217 : :
16218 : 15342070 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16219 : 15342070 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16220 : : {
16221 : 264738 : tree op00 = TREE_OPERAND (sub, 0);
16222 : 264738 : tree op01 = TREE_OPERAND (sub, 1);
16223 : :
16224 : 264738 : STRIP_NOPS (op00);
16225 : 264738 : if (TREE_CODE (op00) == ADDR_EXPR)
16226 : : {
16227 : 2668 : tree op00type;
16228 : 2668 : op00 = TREE_OPERAND (op00, 0);
16229 : 2668 : op00type = TREE_TYPE (op00);
16230 : :
16231 : : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16232 : 2668 : if (VECTOR_TYPE_P (op00type)
16233 : 240 : && type == TREE_TYPE (op00type)
16234 : : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16235 : : but we want to treat offsets with MSB set as negative.
16236 : : For the code below negative offsets are invalid and
16237 : : TYPE_SIZE of the element is something unsigned, so
16238 : : check whether op01 fits into poly_int64, which implies
16239 : : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16240 : : then just use poly_uint64 because we want to treat the
16241 : : value as unsigned. */
16242 : 2861 : && tree_fits_poly_int64_p (op01))
16243 : : {
16244 : 179 : tree part_width = TYPE_SIZE (type);
16245 : 179 : poly_uint64 max_offset
16246 : 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16247 : 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16248 : 179 : if (known_lt (const_op01, max_offset))
16249 : : {
16250 : 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16251 : 179 : return fold_build3_loc (loc,
16252 : : BIT_FIELD_REF, type, op00,
16253 : 179 : part_width, index);
16254 : : }
16255 : : }
16256 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16257 : 2489 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16258 : 2489 : && type == TREE_TYPE (op00type))
16259 : : {
16260 : 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16261 : : const_op01))
16262 : 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16263 : : }
16264 : : /* ((foo *)&fooarray)[1] => fooarray[1] */
16265 : 2489 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16266 : 2489 : && type == TREE_TYPE (op00type))
16267 : : {
16268 : 995 : tree type_domain = TYPE_DOMAIN (op00type);
16269 : 995 : tree min_val = size_zero_node;
16270 : 995 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16271 : 994 : min_val = TYPE_MIN_VALUE (type_domain);
16272 : 995 : poly_uint64 type_size, index;
16273 : 995 : if (poly_int_tree_p (min_val)
16274 : 995 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16275 : 995 : && multiple_p (const_op01, type_size, &index))
16276 : : {
16277 : 995 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16278 : 995 : op01 = wide_int_to_tree (sizetype, off);
16279 : 995 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16280 : : NULL_TREE, NULL_TREE);
16281 : : }
16282 : : }
16283 : : }
16284 : : }
16285 : :
16286 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16287 : 15340896 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16288 : 635875 : && type == TREE_TYPE (TREE_TYPE (subtype))
16289 : 15343833 : && (!in_gimple_form
16290 : 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16291 : : {
16292 : 2936 : tree type_domain;
16293 : 2936 : tree min_val = size_zero_node;
16294 : 2936 : sub = build_fold_indirect_ref_loc (loc, sub);
16295 : 2936 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16296 : 2936 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16297 : 2936 : min_val = TYPE_MIN_VALUE (type_domain);
16298 : 2936 : if (in_gimple_form
16299 : 11 : && TREE_CODE (min_val) != INTEGER_CST)
16300 : : return NULL_TREE;
16301 : 2936 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16302 : 2936 : NULL_TREE);
16303 : : }
16304 : :
16305 : : return NULL_TREE;
16306 : : }
16307 : :
16308 : : /* Builds an expression for an indirection through T, simplifying some
16309 : : cases. */
16310 : :
16311 : : tree
16312 : 7114463 : build_fold_indirect_ref_loc (location_t loc, tree t)
16313 : : {
16314 : 7114463 : tree type = TREE_TYPE (TREE_TYPE (t));
16315 : 7114463 : tree sub = fold_indirect_ref_1 (loc, type, t);
16316 : :
16317 : 7114463 : if (sub)
16318 : : return sub;
16319 : :
16320 : 4840350 : return build1_loc (loc, INDIRECT_REF, type, t);
16321 : : }
16322 : :
16323 : : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16324 : :
16325 : : tree
16326 : 10364369 : fold_indirect_ref_loc (location_t loc, tree t)
16327 : : {
16328 : 10364369 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16329 : :
16330 : 10364369 : if (sub)
16331 : : return sub;
16332 : : else
16333 : 10343570 : return t;
16334 : : }
16335 : :
16336 : : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16337 : : whose result is ignored. The type of the returned tree need not be
16338 : : the same as the original expression. */
16339 : :
16340 : : tree
16341 : 133299 : fold_ignored_result (tree t)
16342 : : {
16343 : 133299 : if (!TREE_SIDE_EFFECTS (t))
16344 : 18654 : return integer_zero_node;
16345 : :
16346 : 151984 : for (;;)
16347 : 151984 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16348 : : {
16349 : 3754 : case tcc_unary:
16350 : 3754 : t = TREE_OPERAND (t, 0);
16351 : 3754 : break;
16352 : :
16353 : 4934 : case tcc_binary:
16354 : 4934 : case tcc_comparison:
16355 : 4934 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16356 : 3043 : t = TREE_OPERAND (t, 0);
16357 : 1891 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16358 : 19 : t = TREE_OPERAND (t, 1);
16359 : : else
16360 : : return t;
16361 : : break;
16362 : :
16363 : 99229 : case tcc_expression:
16364 : 99229 : switch (TREE_CODE (t))
16365 : : {
16366 : 30506 : case COMPOUND_EXPR:
16367 : 30506 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16368 : : return t;
16369 : 30228 : t = TREE_OPERAND (t, 0);
16370 : 30228 : break;
16371 : :
16372 : 382 : case COND_EXPR:
16373 : 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16374 : 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16375 : : return t;
16376 : 295 : t = TREE_OPERAND (t, 0);
16377 : 295 : break;
16378 : :
16379 : : default:
16380 : : return t;
16381 : : }
16382 : : break;
16383 : :
16384 : : default:
16385 : : return t;
16386 : : }
16387 : : }
16388 : :
16389 : : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16390 : :
16391 : : tree
16392 : 2651810786 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16393 : : {
16394 : 2651810786 : tree div = NULL_TREE;
16395 : :
16396 : 2651810786 : if (divisor == 1)
16397 : : return value;
16398 : :
16399 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16400 : : have to do anything. Only do this when we are not given a const,
16401 : : because in that case, this check is more expensive than just
16402 : : doing it. */
16403 : 1649421379 : if (TREE_CODE (value) != INTEGER_CST)
16404 : : {
16405 : 333543 : div = build_int_cst (TREE_TYPE (value), divisor);
16406 : :
16407 : 333543 : if (multiple_of_p (TREE_TYPE (value), value, div))
16408 : : return value;
16409 : : }
16410 : :
16411 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16412 : 1649089627 : if (pow2_or_zerop (divisor))
16413 : : {
16414 : 1649089627 : if (TREE_CODE (value) == INTEGER_CST)
16415 : : {
16416 : 1649087836 : wide_int val = wi::to_wide (value);
16417 : 1649087836 : bool overflow_p;
16418 : :
16419 : 1649087836 : if ((val & (divisor - 1)) == 0)
16420 : : return value;
16421 : :
16422 : 3545918 : overflow_p = TREE_OVERFLOW (value);
16423 : 3545918 : val += divisor - 1;
16424 : 3545918 : val &= (int) -divisor;
16425 : 3545918 : if (val == 0)
16426 : 4 : overflow_p = true;
16427 : :
16428 : 3545918 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16429 : 1649087836 : }
16430 : : else
16431 : : {
16432 : 1791 : tree t;
16433 : :
16434 : 1791 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16435 : 1791 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16436 : 1791 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16437 : 1791 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16438 : : }
16439 : : }
16440 : : else
16441 : : {
16442 : 0 : if (!div)
16443 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16444 : 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16445 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16446 : : }
16447 : :
16448 : : return value;
16449 : : }
16450 : :
16451 : : /* Likewise, but round down. */
16452 : :
16453 : : tree
16454 : 16782977 : round_down_loc (location_t loc, tree value, int divisor)
16455 : : {
16456 : 16782977 : tree div = NULL_TREE;
16457 : :
16458 : 16782977 : gcc_assert (divisor > 0);
16459 : 16782977 : if (divisor == 1)
16460 : : return value;
16461 : :
16462 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16463 : : have to do anything. Only do this when we are not given a const,
16464 : : because in that case, this check is more expensive than just
16465 : : doing it. */
16466 : 16782977 : if (TREE_CODE (value) != INTEGER_CST)
16467 : : {
16468 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16469 : :
16470 : 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16471 : : return value;
16472 : : }
16473 : :
16474 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16475 : 16782977 : if (pow2_or_zerop (divisor))
16476 : : {
16477 : 16782977 : tree t;
16478 : :
16479 : 16782977 : t = build_int_cst (TREE_TYPE (value), -divisor);
16480 : 16782977 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16481 : : }
16482 : : else
16483 : : {
16484 : 0 : if (!div)
16485 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16486 : 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16487 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16488 : : }
16489 : :
16490 : : return value;
16491 : : }
16492 : :
16493 : : /* Returns the pointer to the base of the object addressed by EXP and
16494 : : extracts the information about the offset of the access, storing it
16495 : : to PBITPOS and POFFSET. */
16496 : :
16497 : : static tree
16498 : 1560806 : split_address_to_core_and_offset (tree exp,
16499 : : poly_int64 *pbitpos, tree *poffset)
16500 : : {
16501 : 1560806 : tree core;
16502 : 1560806 : machine_mode mode;
16503 : 1560806 : int unsignedp, reversep, volatilep;
16504 : 1560806 : poly_int64 bitsize;
16505 : 1560806 : location_t loc = EXPR_LOCATION (exp);
16506 : :
16507 : 1560806 : if (TREE_CODE (exp) == SSA_NAME)
16508 : 506413 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16509 : 369985 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16510 : 31332 : exp = gimple_assign_rhs1 (def);
16511 : :
16512 : 1560806 : if (TREE_CODE (exp) == ADDR_EXPR)
16513 : : {
16514 : 913662 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16515 : : poffset, &mode, &unsignedp, &reversep,
16516 : : &volatilep);
16517 : 913662 : core = build_fold_addr_expr_loc (loc, core);
16518 : : }
16519 : 647144 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16520 : : {
16521 : 65561 : core = TREE_OPERAND (exp, 0);
16522 : 65561 : STRIP_NOPS (core);
16523 : 65561 : *pbitpos = 0;
16524 : 65561 : *poffset = TREE_OPERAND (exp, 1);
16525 : 65561 : if (poly_int_tree_p (*poffset))
16526 : : {
16527 : 65471 : poly_offset_int tem
16528 : 65471 : = wi::sext (wi::to_poly_offset (*poffset),
16529 : 65471 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16530 : 65471 : tem <<= LOG2_BITS_PER_UNIT;
16531 : 65471 : if (tem.to_shwi (pbitpos))
16532 : 65471 : *poffset = NULL_TREE;
16533 : : }
16534 : : }
16535 : : else
16536 : : {
16537 : 581583 : core = exp;
16538 : 581583 : *pbitpos = 0;
16539 : 581583 : *poffset = NULL_TREE;
16540 : : }
16541 : :
16542 : 1560806 : return core;
16543 : : }
16544 : :
16545 : : /* Returns true if addresses of E1 and E2 differ by a constant, false
16546 : : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16547 : :
16548 : : bool
16549 : 780403 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16550 : : {
16551 : 780403 : tree core1, core2;
16552 : 780403 : poly_int64 bitpos1, bitpos2;
16553 : 780403 : tree toffset1, toffset2, tdiff, type;
16554 : :
16555 : 780403 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16556 : 780403 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16557 : :
16558 : 780403 : poly_int64 bytepos1, bytepos2;
16559 : 780403 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16560 : 1358614 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16561 : 1560806 : || !operand_equal_p (core1, core2, 0))
16562 : 578211 : return false;
16563 : :
16564 : 202192 : if (toffset1 && toffset2)
16565 : : {
16566 : 29 : type = TREE_TYPE (toffset1);
16567 : 29 : if (type != TREE_TYPE (toffset2))
16568 : 0 : toffset2 = fold_convert (type, toffset2);
16569 : :
16570 : 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16571 : 29 : if (!cst_and_fits_in_hwi (tdiff))
16572 : : return false;
16573 : :
16574 : 15 : *diff = int_cst_value (tdiff);
16575 : : }
16576 : 202163 : else if (toffset1 || toffset2)
16577 : : {
16578 : : /* If only one of the offsets is non-constant, the difference cannot
16579 : : be a constant. */
16580 : : return false;
16581 : : }
16582 : : else
16583 : 184402 : *diff = 0;
16584 : :
16585 : 184417 : *diff += bytepos1 - bytepos2;
16586 : 184417 : return true;
16587 : : }
16588 : :
16589 : : /* Return OFF converted to a pointer offset type suitable as offset for
16590 : : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16591 : : tree
16592 : 18086261 : convert_to_ptrofftype_loc (location_t loc, tree off)
16593 : : {
16594 : 18086261 : if (ptrofftype_p (TREE_TYPE (off)))
16595 : : return off;
16596 : 2218966 : return fold_convert_loc (loc, sizetype, off);
16597 : : }
16598 : :
16599 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16600 : : tree
16601 : 16094912 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16602 : : {
16603 : 16094912 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16604 : 16094912 : ptr, convert_to_ptrofftype_loc (loc, off));
16605 : : }
16606 : :
16607 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16608 : : tree
16609 : 160904 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16610 : : {
16611 : 160904 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16612 : 160904 : ptr, size_int (off));
16613 : : }
16614 : :
16615 : : /* Return a pointer to a NUL-terminated string containing the sequence
16616 : : of bytes corresponding to the representation of the object referred to
16617 : : by SRC (or a subsequence of such bytes within it if SRC is a reference
16618 : : to an initialized constant array plus some constant offset).
16619 : : Set *STRSIZE the number of bytes in the constant sequence including
16620 : : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16621 : : where A is the array that stores the constant sequence that SRC points
16622 : : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16623 : : need not point to a string or even an array of characters but may point
16624 : : to an object of any type. */
16625 : :
16626 : : const char *
16627 : 12478636 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16628 : : {
16629 : : /* The offset into the array A storing the string, and A's byte size. */
16630 : 12478636 : tree offset_node;
16631 : 12478636 : tree mem_size;
16632 : :
16633 : 12478636 : if (strsize)
16634 : 4856171 : *strsize = 0;
16635 : :
16636 : 12478636 : if (strsize)
16637 : 4856171 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16638 : : else
16639 : 7622465 : src = string_constant (src, &offset_node, &mem_size, NULL);
16640 : 12478636 : if (!src)
16641 : : return NULL;
16642 : :
16643 : 2808738 : unsigned HOST_WIDE_INT offset = 0;
16644 : 2808738 : if (offset_node != NULL_TREE)
16645 : : {
16646 : 2808738 : if (!tree_fits_uhwi_p (offset_node))
16647 : : return NULL;
16648 : : else
16649 : 2806974 : offset = tree_to_uhwi (offset_node);
16650 : : }
16651 : :
16652 : 2806974 : if (!tree_fits_uhwi_p (mem_size))
16653 : : return NULL;
16654 : :
16655 : : /* ARRAY_SIZE is the byte size of the array the constant sequence
16656 : : is stored in and equal to sizeof A. INIT_BYTES is the number
16657 : : of bytes in the constant sequence used to initialize the array,
16658 : : including any embedded NULs as well as the terminating NUL (for
16659 : : strings), but not including any trailing zeros/NULs past
16660 : : the terminating one appended implicitly to a string literal to
16661 : : zero out the remainder of the array it's stored in. For example,
16662 : : given:
16663 : : const char a[7] = "abc\0d";
16664 : : n = strlen (a + 1);
16665 : : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16666 : : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16667 : : is equal to strlen (A) + 1. */
16668 : 2806974 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16669 : 2806974 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16670 : 2806974 : const char *string = TREE_STRING_POINTER (src);
16671 : :
16672 : : /* Ideally this would turn into a gcc_checking_assert over time. */
16673 : 2806974 : if (init_bytes > array_size)
16674 : : init_bytes = array_size;
16675 : :
16676 : 2806974 : if (init_bytes == 0 || offset >= array_size)
16677 : : return NULL;
16678 : :
16679 : 2805782 : if (strsize)
16680 : : {
16681 : : /* Compute and store the number of characters from the beginning
16682 : : of the substring at OFFSET to the end, including the terminating
16683 : : nul. Offsets past the initial length refer to null strings. */
16684 : 1562594 : if (offset < init_bytes)
16685 : 1562594 : *strsize = init_bytes - offset;
16686 : : else
16687 : 0 : *strsize = 1;
16688 : : }
16689 : : else
16690 : : {
16691 : 1243188 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16692 : : /* Support only properly NUL-terminated single byte strings. */
16693 : 1243188 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16694 : : return NULL;
16695 : 1237747 : if (string[init_bytes - 1] != '\0')
16696 : : return NULL;
16697 : : }
16698 : :
16699 : 2779139 : return offset < init_bytes ? string + offset : "";
16700 : : }
16701 : :
16702 : : /* Return a pointer to a NUL-terminated string corresponding to
16703 : : the expression STR referencing a constant string, possibly
16704 : : involving a constant offset. Return null if STR either doesn't
16705 : : reference a constant string or if it involves a nonconstant
16706 : : offset. */
16707 : :
16708 : : const char *
16709 : 7622465 : c_getstr (tree str)
16710 : : {
16711 : 7622465 : return getbyterep (str, NULL);
16712 : : }
16713 : :
16714 : : /* Given a tree T, compute which bits in T may be nonzero. */
16715 : :
16716 : : wide_int
16717 : 234888053 : tree_nonzero_bits (const_tree t)
16718 : : {
16719 : 234888053 : switch (TREE_CODE (t))
16720 : : {
16721 : 8415844 : case INTEGER_CST:
16722 : 8415844 : return wi::to_wide (t);
16723 : 133450196 : case SSA_NAME:
16724 : 133450196 : return get_nonzero_bits (t);
16725 : 241770 : case NON_LVALUE_EXPR:
16726 : 241770 : case SAVE_EXPR:
16727 : 241770 : return tree_nonzero_bits (TREE_OPERAND (t, 0));
16728 : 490418 : case BIT_AND_EXPR:
16729 : 980836 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16730 : 1471254 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16731 : 4235 : case BIT_IOR_EXPR:
16732 : 4235 : case BIT_XOR_EXPR:
16733 : 8470 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16734 : 12705 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16735 : 62584 : case COND_EXPR:
16736 : 125168 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16737 : 187752 : tree_nonzero_bits (TREE_OPERAND (t, 2)));
16738 : 49204782 : CASE_CONVERT:
16739 : 98409564 : return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16740 : 49204782 : TYPE_PRECISION (TREE_TYPE (t)),
16741 : 147614346 : TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16742 : 13205728 : case PLUS_EXPR:
16743 : 13205728 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16744 : : {
16745 : 13205728 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16746 : 13205728 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16747 : 13205728 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16748 : 493060 : return wi::bit_or (nzbits1, nzbits2);
16749 : 13205728 : }
16750 : : break;
16751 : 161080 : case LSHIFT_EXPR:
16752 : 161080 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16753 : : {
16754 : 93130 : tree type = TREE_TYPE (t);
16755 : 93130 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16756 : 186260 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16757 : 93130 : TYPE_PRECISION (type));
16758 : 93130 : return wi::neg_p (arg1)
16759 : 186260 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16760 : 93130 : : wi::lshift (nzbits, arg1);
16761 : 93130 : }
16762 : : break;
16763 : 152579 : case RSHIFT_EXPR:
16764 : 152579 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16765 : : {
16766 : 151021 : tree type = TREE_TYPE (t);
16767 : 151021 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16768 : 302042 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16769 : 151021 : TYPE_PRECISION (type));
16770 : 151021 : return wi::neg_p (arg1)
16771 : 302042 : ? wi::lshift (nzbits, -arg1)
16772 : 151021 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16773 : 151021 : }
16774 : : break;
16775 : : default:
16776 : : break;
16777 : : }
16778 : :
16779 : 42281013 : return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16780 : : }
16781 : :
16782 : : /* Helper function for address compare simplifications in match.pd.
16783 : : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16784 : : TYPE is the type of comparison operands.
16785 : : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16786 : : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16787 : : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16788 : : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16789 : : and 2 if unknown. */
16790 : :
16791 : : int
16792 : 1187658 : address_compare (tree_code code, tree type, tree op0, tree op1,
16793 : : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16794 : : bool generic)
16795 : : {
16796 : 1187658 : if (TREE_CODE (op0) == SSA_NAME)
16797 : 19727 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16798 : 1187658 : if (TREE_CODE (op1) == SSA_NAME)
16799 : 3932 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16800 : 1187658 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16801 : 1187658 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16802 : 1187658 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16803 : 1187658 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16804 : 1187658 : if (base0 && TREE_CODE (base0) == MEM_REF)
16805 : : {
16806 : 18917 : off0 += mem_ref_offset (base0).force_shwi ();
16807 : 18917 : base0 = TREE_OPERAND (base0, 0);
16808 : : }
16809 : 1187658 : if (base1 && TREE_CODE (base1) == MEM_REF)
16810 : : {
16811 : 2571 : off1 += mem_ref_offset (base1).force_shwi ();
16812 : 2571 : base1 = TREE_OPERAND (base1, 0);
16813 : : }
16814 : 1187658 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16815 : : return 2;
16816 : :
16817 : 1180135 : int equal = 2;
16818 : : /* Punt in GENERIC on variables with value expressions;
16819 : : the value expressions might point to fields/elements
16820 : : of other vars etc. */
16821 : 1180135 : if (generic
16822 : 1180135 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16823 : 1053805 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16824 : : return 2;
16825 : 1179572 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16826 : : {
16827 : 103918 : symtab_node *node0 = symtab_node::get_create (base0);
16828 : 103918 : symtab_node *node1 = symtab_node::get_create (base1);
16829 : 103918 : equal = node0->equal_address_to (node1);
16830 : : }
16831 : 1075654 : else if ((DECL_P (base0)
16832 : 194267 : || TREE_CODE (base0) == SSA_NAME
16833 : 176217 : || TREE_CODE (base0) == STRING_CST)
16834 : 1075500 : && (DECL_P (base1)
16835 : 178829 : || TREE_CODE (base1) == SSA_NAME
16836 : 176484 : || TREE_CODE (base1) == STRING_CST))
16837 : 1075478 : equal = (base0 == base1);
16838 : : /* Assume different STRING_CSTs with the same content will be
16839 : : merged. */
16840 : 1179396 : if (equal == 0
16841 : 47664 : && TREE_CODE (base0) == STRING_CST
16842 : 16885 : && TREE_CODE (base1) == STRING_CST
16843 : 16856 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16844 : 1179396 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16845 : 6012 : TREE_STRING_LENGTH (base0)) == 0)
16846 : : equal = 1;
16847 : 1175214 : if (equal == 1)
16848 : : {
16849 : 1110695 : if (code == EQ_EXPR
16850 : 1110695 : || code == NE_EXPR
16851 : : /* If the offsets are equal we can ignore overflow. */
16852 : 71148 : || known_eq (off0, off1)
16853 : 142078 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16854 : : /* Or if we compare using pointers to decls or strings. */
16855 : 1181734 : || (POINTER_TYPE_P (type)
16856 : 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16857 : : return 1;
16858 : : return 2;
16859 : : }
16860 : 68877 : if (equal != 0)
16861 : : return equal;
16862 : 43306 : if (code != EQ_EXPR && code != NE_EXPR)
16863 : : return 2;
16864 : :
16865 : : /* At this point we know (or assume) the two pointers point at
16866 : : different objects. */
16867 : 39173 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16868 : 39173 : off0.is_constant (&ioff0);
16869 : 39173 : off1.is_constant (&ioff1);
16870 : : /* Punt on non-zero offsets from functions. */
16871 : 39173 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16872 : 39173 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16873 : : return 2;
16874 : : /* Or if the bases are neither decls nor string literals. */
16875 : 39173 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16876 : : return 2;
16877 : 21594 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16878 : : return 2;
16879 : : /* For initializers, assume addresses of different functions are
16880 : : different. */
16881 : 21594 : if (folding_initializer
16882 : 567 : && TREE_CODE (base0) == FUNCTION_DECL
16883 : 14 : && TREE_CODE (base1) == FUNCTION_DECL)
16884 : : return 0;
16885 : :
16886 : : /* Compute whether one address points to the start of one
16887 : : object and another one to the end of another one. */
16888 : 21580 : poly_int64 size0 = 0, size1 = 0;
16889 : 21580 : if (TREE_CODE (base0) == STRING_CST)
16890 : : {
16891 : 12503 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16892 : : equal = 2;
16893 : : else
16894 : : size0 = TREE_STRING_LENGTH (base0);
16895 : : }
16896 : 9077 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16897 : : size0 = 1;
16898 : : else
16899 : : {
16900 : 9004 : tree sz0 = DECL_SIZE_UNIT (base0);
16901 : 9004 : if (!tree_fits_poly_int64_p (sz0))
16902 : : equal = 2;
16903 : : else
16904 : 9004 : size0 = tree_to_poly_int64 (sz0);
16905 : : }
16906 : 21580 : if (TREE_CODE (base1) == STRING_CST)
16907 : : {
16908 : 12624 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16909 : : equal = 2;
16910 : : else
16911 : : size1 = TREE_STRING_LENGTH (base1);
16912 : : }
16913 : 8956 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16914 : : size1 = 1;
16915 : : else
16916 : : {
16917 : 8887 : tree sz1 = DECL_SIZE_UNIT (base1);
16918 : 8887 : if (!tree_fits_poly_int64_p (sz1))
16919 : : equal = 2;
16920 : : else
16921 : 8887 : size1 = tree_to_poly_int64 (sz1);
16922 : : }
16923 : 21580 : if (equal == 0)
16924 : : {
16925 : : /* If one offset is pointing (or could be) to the beginning of one
16926 : : object and the other is pointing to one past the last byte of the
16927 : : other object, punt. */
16928 : 21568 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16929 : : equal = 2;
16930 : 21431 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16931 : : equal = 2;
16932 : : /* If both offsets are the same, there are some cases we know that are
16933 : : ok. Either if we know they aren't zero, or if we know both sizes
16934 : : are no zero. */
16935 : : if (equal == 2
16936 : 274 : && known_eq (off0, off1)
16937 : 22 : && (known_ne (off0, 0)
16938 : 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16939 : : equal = 0;
16940 : : }
16941 : :
16942 : : /* At this point, equal is 2 if either one or both pointers are out of
16943 : : bounds of their object, or one points to start of its object and the
16944 : : other points to end of its object. This is unspecified behavior
16945 : : e.g. in C++. Otherwise equal is 0. */
16946 : 21580 : if (folding_cxx_constexpr && equal)
16947 : : return equal;
16948 : :
16949 : : /* When both pointers point to string literals, even when equal is 0,
16950 : : due to tail merging of string literals the pointers might be the same. */
16951 : 21517 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16952 : : {
16953 : 12480 : if (ioff0 < 0
16954 : 12480 : || ioff1 < 0
16955 : 12480 : || ioff0 > TREE_STRING_LENGTH (base0)
16956 : 24948 : || ioff1 > TREE_STRING_LENGTH (base1))
16957 : : return 2;
16958 : :
16959 : : /* If the bytes in the string literals starting at the pointers
16960 : : differ, the pointers need to be different. */
16961 : 12468 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16962 : 12468 : TREE_STRING_POINTER (base1) + ioff1,
16963 : 12468 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16964 : : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16965 : : {
16966 : 3715 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16967 : 3715 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16968 : 3715 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16969 : : ioffmin) == 0)
16970 : : /* If even the bytes in the string literal before the
16971 : : pointers are the same, the string literals could be
16972 : : tail merged. */
16973 : : return 2;
16974 : : }
16975 : : return 0;
16976 : : }
16977 : :
16978 : 9037 : if (folding_cxx_constexpr)
16979 : : return 0;
16980 : :
16981 : : /* If this is a pointer comparison, ignore for now even
16982 : : valid equalities where one pointer is the offset zero
16983 : : of one object and the other to one past end of another one. */
16984 : 8590 : if (!INTEGRAL_TYPE_P (type))
16985 : : return 0;
16986 : :
16987 : : /* Assume that string literals can't be adjacent to variables
16988 : : (automatic or global). */
16989 : 299 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16990 : : return 0;
16991 : :
16992 : : /* Assume that automatic variables can't be adjacent to global
16993 : : variables. */
16994 : 295 : if (is_global_var (base0) != is_global_var (base1))
16995 : : return 0;
16996 : :
16997 : : return equal;
16998 : : }
16999 : :
17000 : : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
17001 : : tree
17002 : 44 : ctor_single_nonzero_element (const_tree t)
17003 : : {
17004 : 44 : unsigned HOST_WIDE_INT idx;
17005 : 44 : constructor_elt *ce;
17006 : 44 : tree elt = NULL_TREE;
17007 : :
17008 : 44 : if (TREE_CODE (t) != CONSTRUCTOR)
17009 : : return NULL_TREE;
17010 : 97 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
17011 : 94 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
17012 : : {
17013 : 85 : if (elt)
17014 : : return NULL_TREE;
17015 : 44 : elt = ce->value;
17016 : : }
17017 : : return elt;
17018 : : }
17019 : :
17020 : : #if CHECKING_P
17021 : :
17022 : : namespace selftest {
17023 : :
17024 : : /* Helper functions for writing tests of folding trees. */
17025 : :
17026 : : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17027 : :
17028 : : static void
17029 : 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
17030 : : tree constant)
17031 : : {
17032 : 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
17033 : 16 : }
17034 : :
17035 : : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17036 : : wrapping WRAPPED_EXPR. */
17037 : :
17038 : : static void
17039 : 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
17040 : : tree wrapped_expr)
17041 : : {
17042 : 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
17043 : 12 : ASSERT_NE (wrapped_expr, result);
17044 : 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
17045 : 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
17046 : 12 : }
17047 : :
17048 : : /* Verify that various arithmetic binary operations are folded
17049 : : correctly. */
17050 : :
17051 : : static void
17052 : 4 : test_arithmetic_folding ()
17053 : : {
17054 : 4 : tree type = integer_type_node;
17055 : 4 : tree x = create_tmp_var_raw (type, "x");
17056 : 4 : tree zero = build_zero_cst (type);
17057 : 4 : tree one = build_int_cst (type, 1);
17058 : :
17059 : : /* Addition. */
17060 : : /* 1 <-- (0 + 1) */
17061 : 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
17062 : : one);
17063 : 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
17064 : : one);
17065 : :
17066 : : /* (nonlvalue)x <-- (x + 0) */
17067 : 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
17068 : : x);
17069 : :
17070 : : /* Subtraction. */
17071 : : /* 0 <-- (x - x) */
17072 : 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
17073 : : zero);
17074 : 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
17075 : : x);
17076 : :
17077 : : /* Multiplication. */
17078 : : /* 0 <-- (x * 0) */
17079 : 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
17080 : : zero);
17081 : :
17082 : : /* (nonlvalue)x <-- (x * 1) */
17083 : 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
17084 : : x);
17085 : 4 : }
17086 : :
17087 : : namespace test_operand_equality {
17088 : :
17089 : : /* Verify structural equality. */
17090 : :
17091 : : /* Execute fold_vec_perm_cst unit tests. */
17092 : :
17093 : : static void
17094 : 4 : test ()
17095 : : {
17096 : 4 : tree stype = integer_type_node;
17097 : 4 : tree utype = unsigned_type_node;
17098 : 4 : tree x = create_tmp_var_raw (stype, "x");
17099 : 4 : tree y = create_tmp_var_raw (stype, "y");
17100 : 4 : tree z = create_tmp_var_raw (stype, "z");
17101 : 4 : tree four = build_int_cst (stype, 4);
17102 : 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
17103 : 4 : tree rhs1 = fold_convert (stype,
17104 : : fold_build2 (PLUS_EXPR, utype,
17105 : : fold_convert (utype, x),
17106 : : fold_convert (utype, y)));
17107 : :
17108 : : /* (int)((unsigned x) + (unsigned y)) == x + y. */
17109 : 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17110 : 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17111 : :
17112 : : /* (int)(unsigned) x == x. */
17113 : 4 : tree lhs2 = build1 (NOP_EXPR, stype,
17114 : : build1 (NOP_EXPR, utype, x));
17115 : 4 : tree rhs2 = x;
17116 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17117 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17118 : :
17119 : : /* (unsigned x) + (unsigned y) == x + y. */
17120 : 4 : tree lhs3 = lhs1;
17121 : 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17122 : : fold_convert (utype, x),
17123 : : fold_convert (utype, y));
17124 : 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17125 : 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17126 : :
17127 : : /* (unsigned x) / (unsigned y) == x / y. */
17128 : 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17129 : 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17130 : : fold_convert (utype, x),
17131 : : fold_convert (utype, y));
17132 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17133 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17134 : :
17135 : : /* (long x) / 4 == (long)(x / 4). */
17136 : 4 : tree lstype = long_long_integer_type_node;
17137 : 4 : tree lfour = build_int_cst (lstype, 4);
17138 : 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17139 : : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17140 : 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17141 : : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17142 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17143 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17144 : :
17145 : : /* (unsigned x) / 4 == x / 4. */
17146 : 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17147 : 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17148 : : fold_convert (utype, x),
17149 : : fold_convert (utype, four));
17150 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17151 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17152 : :
17153 : : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17154 : 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17155 : 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17156 : 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17157 : 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17158 : :
17159 : : /* (unsigned x) + 4 == x + 4. */
17160 : 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17161 : 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17162 : : fold_convert (utype, x),
17163 : : fold_convert (utype, four));
17164 : 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17165 : 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17166 : :
17167 : : /* (unsigned x) + 4 == 4 + x. */
17168 : 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17169 : 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17170 : : fold_convert (utype, x),
17171 : : fold_convert (utype, four));
17172 : 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17173 : 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17174 : :
17175 : : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17176 : 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17177 : : fold_build2 (MULT_EXPR, stype,
17178 : : fold_build2 (PLUS_EXPR, stype, four, x),
17179 : : y),
17180 : : z);
17181 : 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17182 : : fold_build2 (PLUS_EXPR, utype,
17183 : : fold_convert (utype, x),
17184 : : fold_convert (utype, four)),
17185 : : fold_convert (utype, y));
17186 : 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17187 : 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17188 : 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17189 : 4 : }
17190 : : }
17191 : :
17192 : : namespace test_fold_vec_perm_cst {
17193 : :
17194 : : /* Build a VECTOR_CST corresponding to VMODE, and has
17195 : : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17196 : : Fill it with randomized elements, using rand() % THRESHOLD. */
17197 : :
17198 : : static tree
17199 : 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17200 : : unsigned nelts_per_pattern,
17201 : : int step = 0, bool natural_stepped = false,
17202 : : int threshold = 100)
17203 : : {
17204 : 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17205 : 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17206 : 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17207 : :
17208 : : // Fill a0 for each pattern
17209 : 0 : for (unsigned i = 0; i < npatterns; i++)
17210 : 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17211 : :
17212 : 0 : if (nelts_per_pattern == 1)
17213 : 0 : return builder.build ();
17214 : :
17215 : : // Fill a1 for each pattern
17216 : 0 : for (unsigned i = 0; i < npatterns; i++)
17217 : : {
17218 : 0 : tree a1;
17219 : 0 : if (natural_stepped)
17220 : : {
17221 : 0 : tree a0 = builder[i];
17222 : 0 : wide_int a0_val = wi::to_wide (a0);
17223 : 0 : wide_int a1_val = a0_val + step;
17224 : 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17225 : 0 : }
17226 : : else
17227 : 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17228 : 0 : builder.quick_push (a1);
17229 : : }
17230 : 0 : if (nelts_per_pattern == 2)
17231 : 0 : return builder.build ();
17232 : :
17233 : 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17234 : : {
17235 : 0 : tree prev_elem = builder[i - npatterns];
17236 : 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17237 : 0 : wide_int val = prev_elem_val + step;
17238 : 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17239 : 0 : }
17240 : :
17241 : 0 : return builder.build ();
17242 : 0 : }
17243 : :
17244 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17245 : : when result is VLA. */
17246 : :
17247 : : static void
17248 : 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17249 : : tree res, tree *expected_res)
17250 : : {
17251 : : /* Actual npatterns and encoded_elts in res may be less than expected due
17252 : : to canonicalization. */
17253 : 0 : ASSERT_TRUE (res != NULL_TREE);
17254 : 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17255 : 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17256 : :
17257 : 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17258 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17259 : 0 : }
17260 : :
17261 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17262 : : when the result is VLS. */
17263 : :
17264 : : static void
17265 : 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17266 : : {
17267 : 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17268 : 0 : for (unsigned i = 0; i < expected_nelts; i++)
17269 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17270 : 0 : }
17271 : :
17272 : : /* Helper routine to push multiple elements into BUILDER. */
17273 : : template<unsigned N>
17274 : 0 : static void builder_push_elems (vec_perm_builder& builder,
17275 : : poly_uint64 (&elems)[N])
17276 : : {
17277 : 0 : for (unsigned i = 0; i < N; i++)
17278 : 0 : builder.quick_push (elems[i]);
17279 : 0 : }
17280 : :
17281 : : #define ARG0(index) vector_cst_elt (arg0, index)
17282 : : #define ARG1(index) vector_cst_elt (arg1, index)
17283 : :
17284 : : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17285 : :
17286 : : static void
17287 : 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17288 : : {
17289 : 0 : for (int i = 0; i < 10; i++)
17290 : : {
17291 : : /* Case 1:
17292 : : sel = { 0, 4, 1, 5, ... }
17293 : : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17294 : 0 : {
17295 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17296 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17297 : :
17298 : 0 : tree inner_type
17299 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17300 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17301 : :
17302 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17303 : 0 : vec_perm_builder builder (res_len, 4, 1);
17304 : 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17305 : 0 : builder_push_elems (builder, mask_elems);
17306 : :
17307 : 0 : vec_perm_indices sel (builder, 2, res_len);
17308 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17309 : :
17310 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17311 : 0 : validate_res (4, 1, res, expected_res);
17312 : 0 : }
17313 : :
17314 : : /* Case 2: Same as case 1, but contains an out of bounds access which
17315 : : should wrap around.
17316 : : sel = {0, 8, 4, 12, ...} (4, 1)
17317 : : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17318 : 0 : {
17319 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17320 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17321 : :
17322 : 0 : tree inner_type
17323 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17324 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17325 : :
17326 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17327 : 0 : vec_perm_builder builder (res_len, 4, 1);
17328 : 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17329 : 0 : builder_push_elems (builder, mask_elems);
17330 : :
17331 : 0 : vec_perm_indices sel (builder, 2, res_len);
17332 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17333 : :
17334 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17335 : 0 : validate_res (4, 1, res, expected_res);
17336 : 0 : }
17337 : : }
17338 : 0 : }
17339 : :
17340 : : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17341 : :
17342 : : static void
17343 : 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17344 : : {
17345 : 0 : for (int i = 0; i < 10; i++)
17346 : : {
17347 : : /* Case 1:
17348 : : sel = { 0, 1, 2, 3}
17349 : : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17350 : 0 : {
17351 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17352 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17353 : :
17354 : 0 : tree inner_type
17355 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17356 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17357 : :
17358 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17359 : 0 : vec_perm_builder builder (res_len, 4, 1);
17360 : 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17361 : 0 : builder_push_elems (builder, mask_elems);
17362 : :
17363 : 0 : vec_perm_indices sel (builder, 2, res_len);
17364 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17365 : :
17366 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17367 : 0 : validate_res_vls (res, expected_res, 4);
17368 : 0 : }
17369 : :
17370 : : /* Case 2: Same as Case 1, but crossing input vector.
17371 : : sel = {0, 2, 4, 6}
17372 : : In this case,the index 4 is ambiguous since len = 4 + 4x.
17373 : : Since we cannot determine, which vector to choose from during
17374 : : compile time, should return NULL_TREE. */
17375 : 0 : {
17376 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17377 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17378 : :
17379 : 0 : tree inner_type
17380 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17381 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17382 : :
17383 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17384 : 0 : vec_perm_builder builder (res_len, 4, 1);
17385 : 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17386 : 0 : builder_push_elems (builder, mask_elems);
17387 : :
17388 : 0 : vec_perm_indices sel (builder, 2, res_len);
17389 : 0 : const char *reason;
17390 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17391 : :
17392 : 0 : ASSERT_TRUE (res == NULL_TREE);
17393 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17394 : 0 : }
17395 : : }
17396 : 0 : }
17397 : :
17398 : : /* Test all input vectors. */
17399 : :
17400 : : static void
17401 : 0 : test_all_nunits (machine_mode vmode)
17402 : : {
17403 : : /* Test with 10 different inputs. */
17404 : 0 : for (int i = 0; i < 10; i++)
17405 : : {
17406 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17407 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17408 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17409 : :
17410 : : /* Case 1: mask = {0, ...} // (1, 1)
17411 : : res = { arg0[0], ... } // (1, 1) */
17412 : 0 : {
17413 : 0 : vec_perm_builder builder (len, 1, 1);
17414 : 0 : builder.quick_push (0);
17415 : 0 : vec_perm_indices sel (builder, 2, len);
17416 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17417 : 0 : tree expected_res[] = { ARG0(0) };
17418 : 0 : validate_res (1, 1, res, expected_res);
17419 : 0 : }
17420 : :
17421 : : /* Case 2: mask = {len, ...} // (1, 1)
17422 : : res = { arg1[0], ... } // (1, 1) */
17423 : 0 : {
17424 : 0 : vec_perm_builder builder (len, 1, 1);
17425 : 0 : builder.quick_push (len);
17426 : 0 : vec_perm_indices sel (builder, 2, len);
17427 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17428 : :
17429 : 0 : tree expected_res[] = { ARG1(0) };
17430 : 0 : validate_res (1, 1, res, expected_res);
17431 : 0 : }
17432 : : }
17433 : 0 : }
17434 : :
17435 : : /* Test all vectors which contain at-least 2 elements. */
17436 : :
17437 : : static void
17438 : 0 : test_nunits_min_2 (machine_mode vmode)
17439 : : {
17440 : 0 : for (int i = 0; i < 10; i++)
17441 : : {
17442 : : /* Case 1: mask = { 0, len, ... } // (2, 1)
17443 : : res = { arg0[0], arg1[0], ... } // (2, 1) */
17444 : 0 : {
17445 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17446 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17447 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17448 : :
17449 : 0 : vec_perm_builder builder (len, 2, 1);
17450 : 0 : poly_uint64 mask_elems[] = { 0, len };
17451 : 0 : builder_push_elems (builder, mask_elems);
17452 : :
17453 : 0 : vec_perm_indices sel (builder, 2, len);
17454 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17455 : :
17456 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17457 : 0 : validate_res (2, 1, res, expected_res);
17458 : 0 : }
17459 : :
17460 : : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17461 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17462 : 0 : {
17463 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17464 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17465 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17466 : :
17467 : 0 : vec_perm_builder builder (len, 2, 2);
17468 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17469 : 0 : builder_push_elems (builder, mask_elems);
17470 : :
17471 : 0 : vec_perm_indices sel (builder, 2, len);
17472 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17473 : :
17474 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17475 : 0 : validate_res (2, 2, res, expected_res);
17476 : 0 : }
17477 : :
17478 : : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17479 : : Test that the stepped sequence of the pattern selects from
17480 : : same input pattern. Since input vectors have npatterns = 2,
17481 : : and step (a2 - a1) = 1, step is not a multiple of npatterns
17482 : : in input vector. So return NULL_TREE. */
17483 : 0 : {
17484 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17485 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17486 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17487 : :
17488 : 0 : vec_perm_builder builder (len, 1, 3);
17489 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17490 : 0 : builder_push_elems (builder, mask_elems);
17491 : :
17492 : 0 : vec_perm_indices sel (builder, 2, len);
17493 : 0 : const char *reason;
17494 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17495 : : &reason);
17496 : 0 : ASSERT_TRUE (res == NULL_TREE);
17497 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17498 : 0 : }
17499 : :
17500 : : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17501 : : Test that stepped sequence of the pattern selects from arg0.
17502 : : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17503 : 0 : {
17504 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17505 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17506 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17507 : :
17508 : 0 : vec_perm_builder builder (len, 1, 3);
17509 : 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17510 : 0 : builder_push_elems (builder, mask_elems);
17511 : :
17512 : 0 : vec_perm_indices sel (builder, 2, len);
17513 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17514 : :
17515 : 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17516 : 0 : validate_res (1, 3, res, expected_res);
17517 : 0 : }
17518 : :
17519 : : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17520 : : In this case ensure that arg has a natural stepped sequence
17521 : : to preserve arg's encoding.
17522 : :
17523 : : As a concrete example, consider:
17524 : : arg0: { -16, -9, -10, ... } // (1, 3)
17525 : : arg1: { -12, -5, -6, ... } // (1, 3)
17526 : : sel = { 0, len, len + 1, ... } // (1, 3)
17527 : :
17528 : : This will create res with following encoding:
17529 : : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17530 : : = { -16, -12, -5, ... }
17531 : :
17532 : : The step in above encoding would be: (-5) - (-12) = 7
17533 : : And hence res[3] would be computed as -5 + 7 = 2.
17534 : : instead of arg1[2], ie, -6.
17535 : : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17536 : : for this case. */
17537 : 0 : {
17538 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17539 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17540 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17541 : :
17542 : 0 : vec_perm_builder builder (len, 1, 3);
17543 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17544 : 0 : builder_push_elems (builder, mask_elems);
17545 : :
17546 : 0 : vec_perm_indices sel (builder, 2, len);
17547 : 0 : const char *reason;
17548 : : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17549 : : stepped pattern, even if we didn't explicitly tell it to. So folding
17550 : : may not always fail, but if it does, ensure that's because arg1 does
17551 : : not have a natural stepped sequence (and not due to other reason) */
17552 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17553 : 0 : if (res == NULL_TREE)
17554 : 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17555 : 0 : }
17556 : :
17557 : : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17558 : : sequence and thus folding should be valid for this case. */
17559 : 0 : {
17560 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17561 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17562 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17563 : :
17564 : 0 : vec_perm_builder builder (len, 1, 3);
17565 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17566 : 0 : builder_push_elems (builder, mask_elems);
17567 : :
17568 : 0 : vec_perm_indices sel (builder, 2, len);
17569 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17570 : :
17571 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17572 : 0 : validate_res (1, 3, res, expected_res);
17573 : 0 : }
17574 : :
17575 : : /* Case 8: Same as aarch64/sve/slp_3.c:
17576 : : arg0, arg1 are dup vectors.
17577 : : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17578 : : So res = { arg0[0], arg1[0], ... } // (2, 1)
17579 : :
17580 : : In this case, since the input vectors are dup, only the first two
17581 : : elements per pattern in sel are considered significant. */
17582 : 0 : {
17583 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17584 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17585 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17586 : :
17587 : 0 : vec_perm_builder builder (len, 2, 3);
17588 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17589 : 0 : builder_push_elems (builder, mask_elems);
17590 : :
17591 : 0 : vec_perm_indices sel (builder, 2, len);
17592 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17593 : :
17594 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17595 : 0 : validate_res (2, 1, res, expected_res);
17596 : 0 : }
17597 : : }
17598 : 0 : }
17599 : :
17600 : : /* Test all vectors which contain at-least 4 elements. */
17601 : :
17602 : : static void
17603 : 0 : test_nunits_min_4 (machine_mode vmode)
17604 : : {
17605 : 0 : for (int i = 0; i < 10; i++)
17606 : : {
17607 : : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17608 : : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17609 : 0 : {
17610 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17611 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17612 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17613 : :
17614 : 0 : vec_perm_builder builder (len, 4, 1);
17615 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17616 : 0 : builder_push_elems (builder, mask_elems);
17617 : :
17618 : 0 : vec_perm_indices sel (builder, 2, len);
17619 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17620 : :
17621 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17622 : 0 : validate_res (4, 1, res, expected_res);
17623 : 0 : }
17624 : :
17625 : : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17626 : : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17627 : 0 : {
17628 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17629 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17630 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17631 : :
17632 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17633 : 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17634 : 0 : builder_push_elems (builder, mask_elems);
17635 : :
17636 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17637 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17638 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17639 : 0 : validate_res (1, 3, res, expected_res);
17640 : 0 : }
17641 : :
17642 : : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17643 : : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17644 : 0 : {
17645 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17646 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17647 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17648 : :
17649 : 0 : vec_perm_builder builder (len, 1, 3);
17650 : 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17651 : 0 : builder_push_elems (builder, mask_elems);
17652 : :
17653 : 0 : vec_perm_indices sel (builder, 2, len);
17654 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17655 : 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17656 : 0 : validate_res (1, 3, res, expected_res);
17657 : 0 : }
17658 : :
17659 : : /* Case 4:
17660 : : sel = { len, 0, 2, ... } // (1, 3)
17661 : : This should return NULL because we cross the input vectors.
17662 : : Because,
17663 : : Let's assume len = C + Cx
17664 : : a1 = 0
17665 : : S = 2
17666 : : esel = arg0_len / sel_npatterns = C + Cx
17667 : : ae = 0 + (esel - 2) * S
17668 : : = 0 + (C + Cx - 2) * 2
17669 : : = 2(C-2) + 2Cx
17670 : :
17671 : : For C >= 4:
17672 : : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17673 : : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17674 : : Since q1 != qe, we cross input vectors.
17675 : : So return NULL_TREE. */
17676 : 0 : {
17677 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17678 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17679 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17680 : :
17681 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17682 : 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17683 : 0 : builder_push_elems (builder, mask_elems);
17684 : :
17685 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17686 : 0 : const char *reason;
17687 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17688 : 0 : ASSERT_TRUE (res == NULL_TREE);
17689 : 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17690 : 0 : }
17691 : :
17692 : : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17693 : : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17694 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17695 : :
17696 : : Note that fold_vec_perm_cst will set
17697 : : res_npatterns = max(4, max(4, 2)) = 4
17698 : : However after canonicalizing, we will end up with shape (2, 2). */
17699 : 0 : {
17700 : 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17701 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17702 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17703 : :
17704 : 0 : vec_perm_builder builder (len, 2, 2);
17705 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17706 : 0 : builder_push_elems (builder, mask_elems);
17707 : :
17708 : 0 : vec_perm_indices sel (builder, 2, len);
17709 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17710 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17711 : 0 : validate_res (2, 2, res, expected_res);
17712 : 0 : }
17713 : :
17714 : : /* Case 6: Test combination in sel, where one pattern is dup and other
17715 : : is stepped sequence.
17716 : : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17717 : : res = { arg0[0], arg0[0], arg0[0],
17718 : : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17719 : 0 : {
17720 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17721 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17722 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17723 : :
17724 : 0 : vec_perm_builder builder (len, 2, 3);
17725 : 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17726 : 0 : builder_push_elems (builder, mask_elems);
17727 : :
17728 : 0 : vec_perm_indices sel (builder, 2, len);
17729 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17730 : :
17731 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17732 : 0 : ARG0(1), ARG0(0), ARG0(2) };
17733 : 0 : validate_res (2, 3, res, expected_res);
17734 : 0 : }
17735 : :
17736 : : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17737 : : when arg0, arg1 and sel have different number of patterns.
17738 : : arg0 is of shape (1, 1)
17739 : : arg1 is of shape (4, 1)
17740 : : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17741 : :
17742 : : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17743 : : However,
17744 : : step = (len+2) - (len+1) = 1
17745 : : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17746 : : Since step is not a multiple of arg_npatterns,
17747 : : valid_mask_for_fold_vec_perm_cst should return false,
17748 : : and thus fold_vec_perm_cst should return NULL_TREE. */
17749 : 0 : {
17750 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17751 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17752 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17753 : :
17754 : 0 : vec_perm_builder builder (len, 2, 3);
17755 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17756 : 0 : builder_push_elems (builder, mask_elems);
17757 : :
17758 : 0 : vec_perm_indices sel (builder, 2, len);
17759 : 0 : const char *reason;
17760 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17761 : :
17762 : 0 : ASSERT_TRUE (res == NULL_TREE);
17763 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17764 : 0 : }
17765 : :
17766 : : /* Case 8: PR111754: When input vector is not a stepped sequence,
17767 : : check that the result is not a stepped sequence either, even
17768 : : if sel has a stepped sequence. */
17769 : 0 : {
17770 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17771 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17772 : :
17773 : 0 : vec_perm_builder builder (len, 1, 3);
17774 : 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17775 : 0 : builder_push_elems (builder, mask_elems);
17776 : :
17777 : 0 : vec_perm_indices sel (builder, 1, len);
17778 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17779 : :
17780 : 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17781 : 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17782 : 0 : }
17783 : :
17784 : : /* Case 9: If sel doesn't contain a stepped sequence,
17785 : : check that the result has same encoding as sel, irrespective
17786 : : of shape of input vectors. */
17787 : 0 : {
17788 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17789 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17790 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17791 : :
17792 : 0 : vec_perm_builder builder (len, 1, 2);
17793 : 0 : poly_uint64 mask_elems[] = { 0, len };
17794 : 0 : builder_push_elems (builder, mask_elems);
17795 : :
17796 : 0 : vec_perm_indices sel (builder, 2, len);
17797 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17798 : :
17799 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17800 : 0 : validate_res (sel.encoding ().npatterns (),
17801 : 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17802 : 0 : }
17803 : : }
17804 : 0 : }
17805 : :
17806 : : /* Test all vectors which contain at-least 8 elements. */
17807 : :
17808 : : static void
17809 : 0 : test_nunits_min_8 (machine_mode vmode)
17810 : : {
17811 : 0 : for (int i = 0; i < 10; i++)
17812 : : {
17813 : : /* Case 1: sel_npatterns (4) > input npatterns (2)
17814 : : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17815 : : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17816 : : arg0[2], arg0[0], arg0[3], arg1[0],
17817 : : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17818 : 0 : {
17819 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17820 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17821 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17822 : :
17823 : 0 : vec_perm_builder builder(len, 4, 3);
17824 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17825 : 0 : 4, 0, 5, len };
17826 : 0 : builder_push_elems (builder, mask_elems);
17827 : :
17828 : 0 : vec_perm_indices sel (builder, 2, len);
17829 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17830 : :
17831 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17832 : 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17833 : 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17834 : 0 : validate_res (4, 3, res, expected_res);
17835 : 0 : }
17836 : : }
17837 : 0 : }
17838 : :
17839 : : /* Test vectors for which nunits[0] <= 4. */
17840 : :
17841 : : static void
17842 : 0 : test_nunits_max_4 (machine_mode vmode)
17843 : : {
17844 : : /* Case 1: mask = {0, 4, ...} // (1, 2)
17845 : : This should return NULL_TREE because the index 4 may choose
17846 : : from either arg0 or arg1 depending on vector length. */
17847 : 0 : {
17848 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17849 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17850 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17851 : :
17852 : 0 : vec_perm_builder builder (len, 1, 2);
17853 : 0 : poly_uint64 mask_elems[] = {0, 4};
17854 : 0 : builder_push_elems (builder, mask_elems);
17855 : :
17856 : 0 : vec_perm_indices sel (builder, 2, len);
17857 : 0 : const char *reason;
17858 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17859 : 0 : ASSERT_TRUE (res == NULL_TREE);
17860 : 0 : ASSERT_TRUE (reason != NULL);
17861 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17862 : 0 : }
17863 : 0 : }
17864 : :
17865 : : #undef ARG0
17866 : : #undef ARG1
17867 : :
17868 : : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17869 : :
17870 : : static bool
17871 : 0 : is_simple_vla_size (poly_uint64 size)
17872 : : {
17873 : 128 : if (size.is_constant ()
17874 : : || !pow2p_hwi (size.coeffs[0]))
17875 : 0 : return false;
17876 : : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17877 : : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17878 : : return false;
17879 : : return true;
17880 : : }
17881 : :
17882 : : /* Execute fold_vec_perm_cst unit tests. */
17883 : :
17884 : : static void
17885 : 4 : test ()
17886 : : {
17887 : 4 : machine_mode vnx4si_mode = E_VOIDmode;
17888 : 4 : machine_mode v4si_mode = E_VOIDmode;
17889 : :
17890 : 4 : machine_mode vmode;
17891 : 132 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17892 : : {
17893 : : /* Obtain modes corresponding to VNx4SI and V4SI,
17894 : : to call mixed mode tests below.
17895 : : FIXME: Is there a better way to do this ? */
17896 : 128 : if (GET_MODE_INNER (vmode) == SImode)
17897 : : {
17898 : 128 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17899 : 128 : if (is_simple_vla_size (nunits)
17900 : : && nunits.coeffs[0] == 4)
17901 : : vnx4si_mode = vmode;
17902 : 128 : else if (known_eq (nunits, poly_uint64 (4)))
17903 : 128 : v4si_mode = vmode;
17904 : : }
17905 : :
17906 : 128 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17907 : : || !targetm.vector_mode_supported_p (vmode))
17908 : 128 : continue;
17909 : :
17910 : : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17911 : : test_all_nunits (vmode);
17912 : : if (nunits.coeffs[0] >= 2)
17913 : : test_nunits_min_2 (vmode);
17914 : : if (nunits.coeffs[0] >= 4)
17915 : : test_nunits_min_4 (vmode);
17916 : : if (nunits.coeffs[0] >= 8)
17917 : : test_nunits_min_8 (vmode);
17918 : :
17919 : : if (nunits.coeffs[0] <= 4)
17920 : : test_nunits_max_4 (vmode);
17921 : : }
17922 : :
17923 : 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17924 : : && targetm.vector_mode_supported_p (vnx4si_mode)
17925 : : && targetm.vector_mode_supported_p (v4si_mode))
17926 : : {
17927 : : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17928 : : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17929 : : }
17930 : 4 : }
17931 : : } // end of test_fold_vec_perm_cst namespace
17932 : :
17933 : : /* Verify that various binary operations on vectors are folded
17934 : : correctly. */
17935 : :
17936 : : static void
17937 : 4 : test_vector_folding ()
17938 : : {
17939 : 4 : tree inner_type = integer_type_node;
17940 : 4 : tree type = build_vector_type (inner_type, 4);
17941 : 4 : tree zero = build_zero_cst (type);
17942 : 4 : tree one = build_one_cst (type);
17943 : 4 : tree index = build_index_vector (type, 0, 1);
17944 : :
17945 : : /* Verify equality tests that return a scalar boolean result. */
17946 : 4 : tree res_type = boolean_type_node;
17947 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17948 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17949 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17950 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17951 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17952 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17953 : : index, one)));
17954 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17955 : : index, index)));
17956 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17957 : : index, index)));
17958 : 4 : }
17959 : :
17960 : : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17961 : :
17962 : : static void
17963 : 4 : test_vec_duplicate_folding ()
17964 : : {
17965 : 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17966 : 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17967 : : /* This will be 1 if VEC_MODE isn't a vector mode. */
17968 : 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17969 : :
17970 : 4 : tree type = build_vector_type (ssizetype, nunits);
17971 : 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17972 : 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17973 : 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17974 : 4 : }
17975 : :
17976 : : /* Run all of the selftests within this file. */
17977 : :
17978 : : void
17979 : 4 : fold_const_cc_tests ()
17980 : : {
17981 : 4 : test_arithmetic_folding ();
17982 : 4 : test_vector_folding ();
17983 : 4 : test_vec_duplicate_folding ();
17984 : 4 : test_fold_vec_perm_cst::test ();
17985 : 4 : test_operand_equality::test ();
17986 : 4 : }
17987 : :
17988 : : } // namespace selftest
17989 : :
17990 : : #endif /* CHECKING_P */
|