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 : 140922 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
157 : : {
158 : 140922 : enum tree_code code = ERROR_MARK;
159 : :
160 : 140922 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
161 : 11 : return ERROR_MARK;
162 : :
163 : 140911 : if (!operand_equal_p (exp0, exp2))
164 : : return ERROR_MARK;
165 : :
166 : 140911 : if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST)
167 : : {
168 : 138264 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
169 : : {
170 : : /* X <= Y - 1 equals to X < Y. */
171 : 80233 : if (cmp == LE_EXPR)
172 : : code = LT_EXPR;
173 : : /* X > Y - 1 equals to X >= Y. */
174 : 79869 : 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 : 70321 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : : {
179 : 17045 : int_range_max r;
180 : 34090 : get_range_query (cfun)->range_of_expr (r, exp0);
181 : 17045 : if (r.undefined_p ())
182 : 0 : r.set_varying (TREE_TYPE (exp0));
183 : :
184 : 17045 : widest_int min = widest_int::from (r.lower_bound (),
185 : 34090 : TYPE_SIGN (TREE_TYPE (exp0)));
186 : 17045 : if (min == wi::to_widest (exp1))
187 : 736 : code = MAX_EXPR;
188 : 17045 : }
189 : : }
190 : 138264 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
191 : : {
192 : : /* X < Y + 1 equals to X <= Y. */
193 : 1008 : if (cmp == LT_EXPR)
194 : : code = LE_EXPR;
195 : : /* X >= Y + 1 equals to X > Y. */
196 : 980 : 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 : 883 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : : {
201 : 496 : int_range_max r;
202 : 992 : get_range_query (cfun)->range_of_expr (r, exp0);
203 : 496 : if (r.undefined_p ())
204 : 0 : r.set_varying (TREE_TYPE (exp0));
205 : :
206 : 496 : widest_int max = widest_int::from (r.upper_bound (),
207 : 992 : TYPE_SIGN (TREE_TYPE (exp0)));
208 : 496 : if (max == wi::to_widest (exp1))
209 : 43 : code = MIN_EXPR;
210 : 496 : }
211 : : }
212 : : }
213 : 138264 : if (code != ERROR_MARK
214 : 140911 : || operand_equal_p (exp1, exp3))
215 : : {
216 : 24477 : if (cmp == LT_EXPR || cmp == LE_EXPR)
217 : : code = MIN_EXPR;
218 : 22096 : if (cmp == GT_EXPR || cmp == GE_EXPR)
219 : 21194 : 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 : 2439630 : expr_location_or (tree t, location_t loc)
229 : : {
230 : 656853 : location_t tloc = EXPR_LOCATION (t);
231 : 2423793 : 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 : 5720207 : protected_set_expr_location_unshare (tree x, location_t loc)
239 : : {
240 : 5720207 : if (CAN_HAVE_LOCATION_P (x)
241 : 5100758 : && EXPR_LOCATION (x) != loc
242 : 2747847 : && !(TREE_CODE (x) == SAVE_EXPR
243 : 1374144 : || TREE_CODE (x) == TARGET_EXPR
244 : : || TREE_CODE (x) == BIND_EXPR))
245 : : {
246 : 1373391 : x = copy_node (x);
247 : 1373391 : SET_EXPR_LOCATION (x, loc);
248 : : }
249 : 5720207 : 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 : 1207296606 : fold_defer_overflow_warnings (void)
296 : : {
297 : 1207296606 : ++fold_deferring_overflow_warnings;
298 : 1207296606 : }
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 : 1207296606 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
311 : : {
312 : 1207296606 : const char *warnmsg;
313 : 1207296606 : location_t locus;
314 : :
315 : 1207296606 : gcc_assert (fold_deferring_overflow_warnings > 0);
316 : 1207296606 : --fold_deferring_overflow_warnings;
317 : 1207296606 : if (fold_deferring_overflow_warnings > 0)
318 : : {
319 : 8939244 : if (fold_deferred_overflow_warning != NULL
320 : 1827114 : && code != 0
321 : 0 : && code < (int) fold_deferred_overflow_code)
322 : 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
323 : 8939244 : return;
324 : : }
325 : :
326 : 1198357362 : warnmsg = fold_deferred_overflow_warning;
327 : 1198357362 : fold_deferred_overflow_warning = NULL;
328 : :
329 : 1198357362 : if (!issue || warnmsg == NULL)
330 : : return;
331 : :
332 : 10592 : 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 : 10592 : if (code == 0 || code > (int) fold_deferred_overflow_code)
338 : 10592 : code = fold_deferred_overflow_code;
339 : :
340 : 10592 : 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 : 184896979 : fold_undefer_and_ignore_overflow_warnings (void)
355 : : {
356 : 184896979 : fold_undefer_overflow_warnings (false, NULL, 0);
357 : 184896979 : }
358 : :
359 : : /* Whether we are deferring overflow warnings. */
360 : :
361 : : bool
362 : 325408420 : fold_deferring_overflow_warnings_p (void)
363 : : {
364 : 325408420 : 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 : 1773241 : fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
372 : : {
373 : 1773241 : if (fold_deferring_overflow_warnings > 0)
374 : : {
375 : 1721938 : if (fold_deferred_overflow_warning == NULL
376 : 772478 : || wc < fold_deferred_overflow_code)
377 : : {
378 : 972310 : fold_deferred_overflow_warning = gmsgid;
379 : 972310 : fold_deferred_overflow_code = wc;
380 : : }
381 : : }
382 : 51303 : else if (issue_strict_overflow_warning (wc))
383 : 7 : warning (OPT_Wstrict_overflow, gmsgid);
384 : 1773241 : }
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 : 2054664 : negate_mathfn_p (combined_fn fn)
391 : : {
392 : 2054664 : 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 : 2050682 : default:
462 : 2050682 : break;
463 : : }
464 : 2050682 : return false;
465 : : }
466 : :
467 : : /* Check whether we may negate an integer constant T without causing
468 : : overflow. */
469 : :
470 : : bool
471 : 3092593 : may_negate_without_overflow_p (const_tree t)
472 : : {
473 : 3092593 : tree type;
474 : :
475 : 3092593 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
476 : :
477 : 3092593 : type = TREE_TYPE (t);
478 : 3092593 : if (TYPE_UNSIGNED (type))
479 : : return false;
480 : :
481 : 3092593 : 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 : 26815739 : negate_expr_p (tree t)
489 : : {
490 : 26968561 : tree type;
491 : :
492 : 26968561 : if (t == 0)
493 : : return false;
494 : :
495 : 26968561 : type = TREE_TYPE (t);
496 : :
497 : 26968561 : STRIP_SIGN_NOPS (t);
498 : 26968561 : switch (TREE_CODE (t))
499 : : {
500 : 1542567 : case INTEGER_CST:
501 : 1542567 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
502 : : return true;
503 : :
504 : : /* Check that -CST will not overflow type. */
505 : 384388 : 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 : 1351394 : case REAL_CST:
517 : : /* We want to canonicalize to positive real constants. Pretend
518 : : that only negative ones can be easily negated. */
519 : 1351394 : 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 : 1479556 : case PLUS_EXPR:
547 : 1479556 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
548 : 1479550 : || HONOR_SIGNED_ZEROS (type)
549 : 2657113 : || (ANY_INTEGRAL_TYPE_P (type)
550 : 1177371 : && ! TYPE_OVERFLOW_WRAPS (type)))
551 : 728619 : return false;
552 : : /* -(A + B) -> (-B) - A. */
553 : 750937 : if (negate_expr_p (TREE_OPERAND (t, 1)))
554 : : return true;
555 : : /* -(A + B) -> (-A) - B. */
556 : 138604 : return negate_expr_p (TREE_OPERAND (t, 0));
557 : :
558 : 255720 : case MINUS_EXPR:
559 : : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
560 : 255720 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
561 : 255720 : && !HONOR_SIGNED_ZEROS (type)
562 : 329176 : && (! ANY_INTEGRAL_TYPE_P (type)
563 : 73233 : || TYPE_OVERFLOW_WRAPS (type));
564 : :
565 : 2464814 : case MULT_EXPR:
566 : 2464814 : 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 : 4296768 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
571 : 158760 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
572 : 2304831 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
573 : 0 : && (wi::popcount
574 : 2148384 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
575 : 156447 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
576 : 133818 : && (wi::popcount
577 : 4407957 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
578 : : break;
579 : :
580 : : /* Fall through. */
581 : :
582 : 2442696 : case RDIV_EXPR:
583 : 2442696 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
584 : 2442695 : return negate_expr_p (TREE_OPERAND (t, 1))
585 : 2442695 : || negate_expr_p (TREE_OPERAND (t, 0));
586 : : break;
587 : :
588 : 2727 : case TRUNC_DIV_EXPR:
589 : 2727 : case ROUND_DIV_EXPR:
590 : 2727 : case EXACT_DIV_EXPR:
591 : 2727 : 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 : 492 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
596 : 492 : && 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 : 654 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
602 : 327 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
603 : 569 : || (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 : 4656913 : case NOP_EXPR:
609 : : /* Negate -((double)float) as (double)(-float). */
610 : 4656913 : if (SCALAR_FLOAT_TYPE_P (type))
611 : : {
612 : 14016 : tree tem = strip_float_extensions (t);
613 : 14016 : if (tem != t)
614 : : return negate_expr_p (tem);
615 : : }
616 : : break;
617 : :
618 : 1024491 : case CALL_EXPR:
619 : : /* Negate -f(x) as f(-x). */
620 : 1024491 : 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 : 37264064 : fold_negate_expr_1 (location_t loc, tree t)
647 : : {
648 : 37264064 : tree type = TREE_TYPE (t);
649 : 37264064 : tree tem;
650 : :
651 : 37264064 : 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 : 28765779 : case INTEGER_CST:
661 : 28765779 : tem = fold_negate_const (t, type);
662 : 28765779 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
663 : 8799 : || (ANY_INTEGRAL_TYPE_P (type)
664 : 8799 : && !TYPE_OVERFLOW_TRAPS (type)
665 : 8799 : && TYPE_OVERFLOW_WRAPS (type))
666 : 28773885 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
667 : : return tem;
668 : : break;
669 : :
670 : 2062960 : case POLY_INT_CST:
671 : 2062960 : case REAL_CST:
672 : 2062960 : case FIXED_CST:
673 : 2062960 : tem = fold_negate_const (t, type);
674 : 2062960 : return tem;
675 : :
676 : 66143 : case COMPLEX_CST:
677 : 66143 : {
678 : 66143 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
679 : 66143 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
680 : 66143 : if (rpart && ipart)
681 : 66143 : return build_complex (type, rpart, ipart);
682 : : }
683 : : break;
684 : :
685 : 51405 : case VECTOR_CST:
686 : 51405 : {
687 : 51405 : tree_vector_builder elts;
688 : 51405 : elts.new_unary_operation (type, t, true);
689 : 51405 : unsigned int count = elts.encoded_nelts ();
690 : 124198 : for (unsigned int i = 0; i < count; ++i)
691 : : {
692 : 72793 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
693 : 72793 : if (elt == NULL_TREE)
694 : 0 : return NULL_TREE;
695 : 72793 : elts.quick_push (elt);
696 : : }
697 : :
698 : 51405 : return elts.build ();
699 : 51405 : }
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 : 667328 : case PLUS_EXPR:
720 : 667328 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
721 : 667328 : && !HONOR_SIGNED_ZEROS (type))
722 : : {
723 : : /* -(A + B) -> (-B) - A. */
724 : 667218 : if (negate_expr_p (TREE_OPERAND (t, 1)))
725 : : {
726 : 616001 : tem = negate_expr (TREE_OPERAND (t, 1));
727 : 616001 : return fold_build2_loc (loc, MINUS_EXPR, type,
728 : 1232002 : tem, TREE_OPERAND (t, 0));
729 : : }
730 : :
731 : : /* -(A + B) -> (-A) - B. */
732 : 51217 : if (negate_expr_p (TREE_OPERAND (t, 0)))
733 : : {
734 : 1019 : tem = negate_expr (TREE_OPERAND (t, 0));
735 : 1019 : return fold_build2_loc (loc, MINUS_EXPR, type,
736 : 2038 : tem, TREE_OPERAND (t, 1));
737 : : }
738 : : }
739 : : break;
740 : :
741 : 149175 : case MINUS_EXPR:
742 : : /* - (A - B) -> B - A */
743 : 149175 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
744 : 149175 : && !HONOR_SIGNED_ZEROS (type))
745 : 68264 : return fold_build2_loc (loc, MINUS_EXPR, type,
746 : 136528 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
747 : : break;
748 : :
749 : 159511 : case MULT_EXPR:
750 : 159511 : if (TYPE_UNSIGNED (type))
751 : : break;
752 : :
753 : : /* Fall through. */
754 : :
755 : 34373 : case RDIV_EXPR:
756 : 34373 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
757 : : {
758 : 34373 : tem = TREE_OPERAND (t, 1);
759 : 34373 : if (negate_expr_p (tem))
760 : 62608 : return fold_build2_loc (loc, TREE_CODE (t), type,
761 : 62608 : TREE_OPERAND (t, 0), negate_expr (tem));
762 : 3069 : tem = TREE_OPERAND (t, 0);
763 : 3069 : 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 : 2012 : case TRUNC_DIV_EXPR:
770 : 2012 : case ROUND_DIV_EXPR:
771 : 2012 : case EXACT_DIV_EXPR:
772 : 2012 : 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 : 1815049 : case NOP_EXPR:
795 : : /* Convert -((double)float) into (double)(-float). */
796 : 1815049 : if (SCALAR_FLOAT_TYPE_P (type))
797 : : {
798 : 11342 : tem = strip_float_extensions (t);
799 : 11342 : if (tem != t && negate_expr_p (tem))
800 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
801 : : }
802 : : break;
803 : :
804 : 307264 : case CALL_EXPR:
805 : : /* Negate -f(x) as f(-x). */
806 : 307264 : if (negate_mathfn_p (get_call_combined_fn (t))
807 : 308553 : && 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 : 394 : case RSHIFT_EXPR:
818 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
819 : 394 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
820 : : {
821 : 376 : tree op1 = TREE_OPERAND (t, 1);
822 : 376 : 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 : 37264064 : fold_negate_expr (location_t loc, tree t)
845 : : {
846 : 37264064 : tree type = TREE_TYPE (t);
847 : 37264064 : STRIP_SIGN_NOPS (t);
848 : 37264064 : tree tem = fold_negate_expr_1 (loc, t);
849 : 37264064 : if (tem == NULL_TREE)
850 : : return NULL_TREE;
851 : 31666133 : 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 : 3535654 : negate_expr (tree t)
860 : : {
861 : 3535654 : tree type, tem;
862 : 3535654 : location_t loc;
863 : :
864 : 3535654 : if (t == NULL_TREE)
865 : : return NULL_TREE;
866 : :
867 : 3535654 : loc = EXPR_LOCATION (t);
868 : 3535654 : type = TREE_TYPE (t);
869 : 3535654 : STRIP_SIGN_NOPS (t);
870 : :
871 : 3535654 : tem = fold_negate_expr (loc, t);
872 : 3535654 : if (!tem)
873 : 1714177 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
874 : 3535654 : 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 : 216726716 : 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 : 216726716 : tree var = 0;
906 : 216726716 : *minus_varp = 0;
907 : 216726716 : *conp = 0;
908 : 216726716 : *minus_conp = 0;
909 : 216726716 : *litp = 0;
910 : 216726716 : *minus_litp = 0;
911 : :
912 : : /* Strip any conversions that don't change the machine mode or signedness. */
913 : 216726716 : STRIP_SIGN_NOPS (in);
914 : :
915 : 216726716 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
916 : 137660471 : || TREE_CODE (in) == FIXED_CST)
917 : 79066245 : *litp = in;
918 : 137660471 : else if (TREE_CODE (in) == code
919 : 137660471 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
920 : 133363589 : && ! 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 : 133363589 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
926 : 55976561 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
927 : 131752289 : || (code == MINUS_EXPR
928 : 20959898 : && (TREE_CODE (in) == PLUS_EXPR
929 : 19273100 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
930 : : {
931 : 8017922 : tree op0 = TREE_OPERAND (in, 0);
932 : 8017922 : tree op1 = TREE_OPERAND (in, 1);
933 : 8017922 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
934 : 8017922 : 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 : 8017922 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
938 : 7794919 : || TREE_CODE (op0) == FIXED_CST)
939 : 223003 : *litp = op0, op0 = 0;
940 : 7794919 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
941 : 5145782 : || TREE_CODE (op1) == FIXED_CST)
942 : 2649137 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
943 : :
944 : 8017922 : if (op0 != 0 && TREE_CONSTANT (op0))
945 : 14748 : *conp = op0, op0 = 0;
946 : 8003174 : else if (op1 != 0 && TREE_CONSTANT (op1))
947 : 43525 : *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 : 8017922 : if (op0 != 0 && op1 != 0)
952 : : var = in;
953 : 2923322 : else if (op0 != 0)
954 : : var = op0;
955 : : else
956 : 237751 : var = op1, neg_var_p = neg1_p;
957 : :
958 : : /* Now do any needed negations. */
959 : 8017922 : if (neg_litp_p)
960 : 23309 : *minus_litp = *litp, *litp = 0;
961 : 8017922 : if (neg_conp_p && *conp)
962 : 3312 : *minus_conp = *conp, *conp = 0;
963 : 8017922 : if (neg_var_p && var)
964 : 227305 : *minus_varp = var, var = 0;
965 : : }
966 : 129642549 : else if (TREE_CONSTANT (in))
967 : 770166 : *conp = in;
968 : 128872383 : else if (TREE_CODE (in) == BIT_NOT_EXPR
969 : 466786 : && code == PLUS_EXPR)
970 : : {
971 : : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
972 : : when IN is constant. */
973 : 360818 : *litp = build_minus_one_cst (type);
974 : 360818 : *minus_varp = TREE_OPERAND (in, 0);
975 : : }
976 : : else
977 : : var = in;
978 : :
979 : 216726716 : if (negate_p)
980 : : {
981 : 11567033 : if (*litp)
982 : 1194531 : *minus_litp = *litp, *litp = 0;
983 : 10372502 : else if (*minus_litp)
984 : 162 : *litp = *minus_litp, *minus_litp = 0;
985 : 11567033 : if (*conp)
986 : 21863 : *minus_conp = *conp, *conp = 0;
987 : 11545170 : else if (*minus_conp)
988 : 0 : *conp = *minus_conp, *minus_conp = 0;
989 : 11567033 : if (var)
990 : 11532988 : *minus_varp = var, var = 0;
991 : 34045 : else if (*minus_varp)
992 : 757 : var = *minus_varp, *minus_varp = 0;
993 : : }
994 : :
995 : 216726716 : if (*litp
996 : 216726716 : && TREE_OVERFLOW_P (*litp))
997 : 18486 : *litp = drop_tree_overflow (*litp);
998 : 216726716 : if (*minus_litp
999 : 216726716 : && TREE_OVERFLOW_P (*minus_litp))
1000 : 0 : *minus_litp = drop_tree_overflow (*minus_litp);
1001 : :
1002 : 216726716 : 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 : 20090874 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1012 : : {
1013 : 20090874 : if (t1 == 0)
1014 : : {
1015 : 12767534 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
1016 : : return t2;
1017 : : }
1018 : 7323340 : 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 : 4088443 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1025 : 3231458 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1026 : 3170925 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1027 : : {
1028 : 1575684 : if (code == PLUS_EXPR)
1029 : : {
1030 : 877078 : 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 : 877024 : 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 : 877023 : else if (integer_zerop (t2))
1041 : 33523 : return fold_convert_loc (loc, type, t1);
1042 : : }
1043 : 698606 : else if (code == MINUS_EXPR)
1044 : : {
1045 : 677419 : if (integer_zerop (t2))
1046 : 0 : return fold_convert_loc (loc, type, t1);
1047 : : }
1048 : :
1049 : 1542106 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1050 : 1542106 : fold_convert_loc (loc, type, t2));
1051 : : }
1052 : :
1053 : 2512759 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1054 : 2512759 : 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 : 2177815269 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1062 : : {
1063 : 2177815269 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1064 : : return false;
1065 : 2177815269 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1066 : : return false;
1067 : :
1068 : 2177815269 : switch (code)
1069 : : {
1070 : : case LSHIFT_EXPR:
1071 : : case RSHIFT_EXPR:
1072 : : case LROTATE_EXPR:
1073 : : case RROTATE_EXPR:
1074 : : return true;
1075 : :
1076 : 2177815269 : default:
1077 : 2177815269 : break;
1078 : : }
1079 : :
1080 : 2177815269 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1081 : 2177815269 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1082 : 4355630538 : && 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 : 1218404675 : 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 : 1218404675 : wide_int tmp;
1095 : 1218404675 : *overflow = wi::OVF_NONE;
1096 : 1218404675 : switch (code)
1097 : : {
1098 : 2098722 : case BIT_IOR_EXPR:
1099 : 2098722 : res = wi::bit_or (arg1, arg2);
1100 : 2098722 : break;
1101 : :
1102 : 99574 : case BIT_XOR_EXPR:
1103 : 99574 : res = wi::bit_xor (arg1, arg2);
1104 : 99574 : break;
1105 : :
1106 : 19373839 : case BIT_AND_EXPR:
1107 : 19373839 : res = wi::bit_and (arg1, arg2);
1108 : 19373839 : break;
1109 : :
1110 : 12753662 : case LSHIFT_EXPR:
1111 : 12753662 : if (wi::neg_p (arg2))
1112 : : return false;
1113 : 12723318 : res = wi::lshift (arg1, arg2);
1114 : 12723318 : break;
1115 : :
1116 : 7731822 : case RSHIFT_EXPR:
1117 : 7731822 : 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 : 7731624 : res = wi::rshift (arg1, arg2, sign);
1123 : 7731624 : break;
1124 : :
1125 : 1838 : case RROTATE_EXPR:
1126 : 1838 : case LROTATE_EXPR:
1127 : 1838 : 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 : 1824 : tmp = arg2;
1137 : :
1138 : 1824 : if (code == RROTATE_EXPR)
1139 : 1656 : res = wi::rrotate (arg1, tmp);
1140 : : else
1141 : 182 : res = wi::lrotate (arg1, tmp);
1142 : : break;
1143 : :
1144 : 207977877 : case PLUS_EXPR:
1145 : 207977877 : res = wi::add (arg1, arg2, sign, overflow);
1146 : 207977877 : break;
1147 : :
1148 : 70893066 : case MINUS_EXPR:
1149 : 70893066 : res = wi::sub (arg1, arg2, sign, overflow);
1150 : 70893066 : break;
1151 : :
1152 : 332969840 : case MULT_EXPR:
1153 : 332969840 : res = wi::mul (arg1, arg2, sign, overflow);
1154 : 332969840 : break;
1155 : :
1156 : 4776 : case MULT_HIGHPART_EXPR:
1157 : 4776 : res = wi::mul_high (arg1, arg2, sign);
1158 : 4776 : break;
1159 : :
1160 : 293774573 : case TRUNC_DIV_EXPR:
1161 : 293774573 : case EXACT_DIV_EXPR:
1162 : 293774573 : if (arg2 == 0)
1163 : : return false;
1164 : 293768628 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1165 : 293768628 : break;
1166 : :
1167 : 67276023 : case FLOOR_DIV_EXPR:
1168 : 67276023 : if (arg2 == 0)
1169 : : return false;
1170 : 67276023 : res = wi::div_floor (arg1, arg2, sign, overflow);
1171 : 67276023 : break;
1172 : :
1173 : 68748166 : case CEIL_DIV_EXPR:
1174 : 68748166 : if (arg2 == 0)
1175 : : return false;
1176 : 68748166 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1177 : 68748166 : 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 : 694793 : case TRUNC_MOD_EXPR:
1186 : 694793 : if (arg2 == 0)
1187 : : return false;
1188 : 693719 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1189 : 693719 : break;
1190 : :
1191 : 56157583 : case FLOOR_MOD_EXPR:
1192 : 56157583 : if (arg2 == 0)
1193 : : return false;
1194 : 56157583 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1195 : 56157583 : 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 : 27767 : case MIN_EXPR:
1210 : 27767 : res = wi::min (arg1, arg2, sign);
1211 : 27767 : break;
1212 : :
1213 : 77820449 : case MAX_EXPR:
1214 : 77820449 : res = wi::max (arg1, arg2, sign);
1215 : 77820449 : break;
1216 : :
1217 : : default:
1218 : : return false;
1219 : : }
1220 : : return true;
1221 : 1218404675 : }
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 : 1218404675 : 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 : 1218404675 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1252 : :
1253 : 1218404675 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1254 : : {
1255 : 1218404675 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1256 : 1218404675 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1257 : 1218404675 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1258 : : return NULL_TREE;
1259 : 1218366987 : res = wi_res;
1260 : 1218366987 : return true;
1261 : 1218404894 : }
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 : 1218404675 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1326 : : int overflowable)
1327 : : {
1328 : 1218404675 : poly_wide_int poly_res;
1329 : 1218404675 : tree type = TREE_TYPE (arg1);
1330 : 1218404675 : signop sign = TYPE_SIGN (type);
1331 : 1218404675 : wi::overflow_type overflow = wi::OVF_NONE;
1332 : :
1333 : 1218404675 : if (!poly_int_tree_p (arg1)
1334 : 1218404675 : || !poly_int_tree_p (arg2)
1335 : 2436809350 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1336 : 37688 : return NULL_TREE;
1337 : 1218366987 : return force_fit_type (type, poly_res, overflowable,
1338 : 1218366987 : (((sign == SIGNED || overflowable == -1)
1339 : 1218366987 : && overflow)
1340 : 1218366987 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1341 : 1218404675 : }
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 : 175611 : 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 : 3833 : default:
1360 : 3833 : 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 : 430162 : 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 : 430162 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1373 : : {
1374 : 339605 : if (integer_zerop (other_op))
1375 : : {
1376 : 24024 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1377 : : return op;
1378 : 23201 : 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 : 261489 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1392 : : tree (*elt_const_binop) (enum tree_code, tree, tree))
1393 : : {
1394 : 177839 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1395 : 432442 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1396 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1397 : : {
1398 : 170953 : tree type = TREE_TYPE (arg1);
1399 : 170953 : bool step_ok_p;
1400 : 170953 : if (VECTOR_CST_STEPPED_P (arg1)
1401 : 170953 : && 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 : 2228 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1411 : 168725 : 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 : 49936 : step_ok_p = distributes_over_addition_p (code, 1);
1420 : : else
1421 : : /* Similarly in reverse. */
1422 : 118789 : step_ok_p = distributes_over_addition_p (code, 2);
1423 : 170953 : tree_vector_builder elts;
1424 : 170953 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1425 : : return NULL_TREE;
1426 : 170953 : unsigned int count = elts.encoded_nelts ();
1427 : 639905 : for (unsigned int i = 0; i < count; ++i)
1428 : : {
1429 : 469283 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1430 : 469283 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1431 : :
1432 : 469283 : 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 : 469283 : if (elt == NULL_TREE)
1437 : 331 : return NULL_TREE;
1438 : 468952 : elts.quick_push (elt);
1439 : : }
1440 : :
1441 : 170622 : return elts.build ();
1442 : 170953 : }
1443 : :
1444 : 90536 : if (TREE_CODE (arg1) == VECTOR_CST
1445 : 6886 : && TREE_CODE (arg2) == INTEGER_CST)
1446 : : {
1447 : 6886 : tree type = TREE_TYPE (arg1);
1448 : 6886 : bool step_ok_p = distributes_over_addition_p (code, 1);
1449 : 6886 : tree_vector_builder elts;
1450 : 6886 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1451 : : return NULL_TREE;
1452 : 6886 : unsigned int count = elts.encoded_nelts ();
1453 : 32160 : for (unsigned int i = 0; i < count; ++i)
1454 : : {
1455 : 25361 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1456 : :
1457 : 25361 : 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 : 25361 : if (elt == NULL_TREE)
1462 : 87 : return NULL_TREE;
1463 : 25274 : elts.quick_push (elt);
1464 : : }
1465 : :
1466 : 6799 : return elts.build ();
1467 : 6886 : }
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 : 171800282 : const_binop (enum tree_code code, tree arg1, tree arg2)
1478 : : {
1479 : : /* Sanity check for the recursive cases. */
1480 : 171800282 : if (!arg1 || !arg2)
1481 : : return NULL_TREE;
1482 : :
1483 : 171799018 : STRIP_NOPS (arg1);
1484 : 171799018 : STRIP_NOPS (arg2);
1485 : :
1486 : 171799018 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1487 : : {
1488 : 166030952 : if (code == POINTER_PLUS_EXPR)
1489 : 105111 : return int_const_binop (PLUS_EXPR,
1490 : 210222 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1491 : :
1492 : 165925841 : return int_const_binop (code, arg1, arg2);
1493 : : }
1494 : :
1495 : 5768066 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1496 : : {
1497 : 5490943 : machine_mode mode;
1498 : 5490943 : REAL_VALUE_TYPE d1;
1499 : 5490943 : REAL_VALUE_TYPE d2;
1500 : 5490943 : REAL_VALUE_TYPE value;
1501 : 5490943 : REAL_VALUE_TYPE result;
1502 : 5490943 : bool inexact;
1503 : 5490943 : tree t, type;
1504 : :
1505 : : /* The following codes are handled by real_arithmetic. */
1506 : 5490943 : switch (code)
1507 : : {
1508 : 5490943 : case PLUS_EXPR:
1509 : 5490943 : case MINUS_EXPR:
1510 : 5490943 : case MULT_EXPR:
1511 : 5490943 : case RDIV_EXPR:
1512 : 5490943 : case MIN_EXPR:
1513 : 5490943 : case MAX_EXPR:
1514 : 5490943 : break;
1515 : :
1516 : : default:
1517 : : return NULL_TREE;
1518 : : }
1519 : :
1520 : 5490943 : d1 = TREE_REAL_CST (arg1);
1521 : 5490943 : d2 = TREE_REAL_CST (arg2);
1522 : :
1523 : 5490943 : type = TREE_TYPE (arg1);
1524 : 5490943 : mode = TYPE_MODE (type);
1525 : :
1526 : : /* Don't perform operation if we honor signaling NaNs and
1527 : : either operand is a signaling NaN. */
1528 : 5490943 : if (HONOR_SNANS (mode)
1529 : 5490943 : && (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 : 5490910 : if (code == RDIV_EXPR
1536 : 2468838 : && real_equal (&d2, &dconst0)
1537 : 5501443 : && (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 : 5483665 : 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 : 5483426 : 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 : 5483365 : inexact = real_arithmetic (&value, code, &d1, &d2);
1560 : 5483365 : 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 : 5483365 : if (flag_trapping_math
1567 : 21300262 : && MODE_HAS_NANS (mode)
1568 : 5466611 : && REAL_VALUE_ISNAN (result)
1569 : 2504 : && !REAL_VALUE_ISNAN (d1)
1570 : 5485869 : && !REAL_VALUE_ISNAN (d2))
1571 : 2504 : return NULL_TREE;
1572 : :
1573 : : /* Don't constant fold this floating point operation if
1574 : : the result has overflowed and flag_trapping_math. */
1575 : 5480861 : if (flag_trapping_math
1576 : 21290588 : && MODE_HAS_INFINITIES (mode)
1577 : 5464107 : && REAL_VALUE_ISINF (result)
1578 : 7497 : && !REAL_VALUE_ISINF (d1)
1579 : 5487775 : && !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 : 5476230 : if ((flag_rounding_math
1587 : 37187797 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1588 : 5476230 : && (inexact || !real_identical (&result, &value)))
1589 : 1107 : return NULL_TREE;
1590 : :
1591 : 5475123 : t = build_real (type, result);
1592 : :
1593 : 5475123 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1594 : 5475123 : return t;
1595 : : }
1596 : :
1597 : 277123 : 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 : 277123 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1646 : : {
1647 : 11243 : tree type = TREE_TYPE (arg1);
1648 : 11243 : tree r1 = TREE_REALPART (arg1);
1649 : 11243 : tree i1 = TREE_IMAGPART (arg1);
1650 : 11243 : tree r2 = TREE_REALPART (arg2);
1651 : 11243 : tree i2 = TREE_IMAGPART (arg2);
1652 : 11243 : tree real, imag;
1653 : :
1654 : 11243 : 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 : 1707 : case RDIV_EXPR:
1677 : 1707 : if (COMPLEX_FLOAT_TYPE_P (type))
1678 : 1707 : return do_mpc_arg2 (arg1, arg2, type,
1679 : : /* do_nonfinite= */ folding_initializer,
1680 : 1707 : 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 : 266038 : tree simplified;
1775 : 266038 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1776 : : return simplified;
1777 : :
1778 : 265745 : if (commutative_tree_code (code)
1779 : 265745 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1780 : : return simplified;
1781 : :
1782 : 261489 : 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 : 222373459 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1790 : : {
1791 : 222373459 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1792 : 57583337 : 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 : 164790122 : 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 : 267659 : case COMPLEX_EXPR:
1805 : 267659 : if ((TREE_CODE (arg1) == REAL_CST
1806 : 257154 : && TREE_CODE (arg2) == REAL_CST)
1807 : 10506 : || (TREE_CODE (arg1) == INTEGER_CST
1808 : 10505 : && TREE_CODE (arg2) == INTEGER_CST))
1809 : 267658 : return build_complex (type, arg1, arg2);
1810 : : return NULL_TREE;
1811 : :
1812 : 15487 : case POINTER_DIFF_EXPR:
1813 : 15487 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1814 : : {
1815 : 29798 : poly_offset_int res = (wi::to_poly_offset (arg1)
1816 : 14899 : - wi::to_poly_offset (arg2));
1817 : 14899 : return force_fit_type (type, res, 1,
1818 : 14899 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1819 : : }
1820 : : return NULL_TREE;
1821 : :
1822 : 12301 : case VEC_PACK_TRUNC_EXPR:
1823 : 12301 : case VEC_PACK_FIX_TRUNC_EXPR:
1824 : 12301 : case VEC_PACK_FLOAT_EXPR:
1825 : 12301 : {
1826 : 12301 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1827 : :
1828 : 12301 : if (TREE_CODE (arg1) != VECTOR_CST
1829 : 12301 : || TREE_CODE (arg2) != VECTOR_CST)
1830 : : return NULL_TREE;
1831 : :
1832 : 12301 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1833 : : return NULL_TREE;
1834 : :
1835 : 12301 : out_nelts = in_nelts * 2;
1836 : 12301 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1837 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1838 : :
1839 : 12301 : tree_vector_builder elts (type, out_nelts, 1);
1840 : 150153 : for (i = 0; i < out_nelts; i++)
1841 : : {
1842 : 137864 : tree elt = (i < in_nelts
1843 : 137864 : ? VECTOR_CST_ELT (arg1, i)
1844 : 68926 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1845 : 138732 : 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 : 137864 : TREE_TYPE (type), elt);
1850 : 137864 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1851 : 12 : return NULL_TREE;
1852 : 137852 : elts.quick_push (elt);
1853 : : }
1854 : :
1855 : 12289 : return elts.build ();
1856 : 12301 : }
1857 : :
1858 : 210 : case VEC_WIDEN_MULT_LO_EXPR:
1859 : 210 : case VEC_WIDEN_MULT_HI_EXPR:
1860 : 210 : case VEC_WIDEN_MULT_EVEN_EXPR:
1861 : 210 : case VEC_WIDEN_MULT_ODD_EXPR:
1862 : 210 : {
1863 : 210 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1864 : :
1865 : 210 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1866 : : return NULL_TREE;
1867 : :
1868 : 210 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1869 : : return NULL_TREE;
1870 : 210 : out_nelts = in_nelts / 2;
1871 : 210 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1872 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1873 : :
1874 : 210 : 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 : 210 : scale = 1, ofs = 1;
1882 : :
1883 : 210 : tree_vector_builder elts (type, out_nelts, 1);
1884 : 734 : for (out = 0; out < out_nelts; out++)
1885 : : {
1886 : 524 : unsigned int in = (out << scale) + ofs;
1887 : 524 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1888 : : VECTOR_CST_ELT (arg1, in));
1889 : 524 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1890 : : VECTOR_CST_ELT (arg2, in));
1891 : :
1892 : 524 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1893 : 0 : return NULL_TREE;
1894 : 524 : tree elt = const_binop (MULT_EXPR, t1, t2);
1895 : 524 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1896 : : return NULL_TREE;
1897 : 524 : elts.quick_push (elt);
1898 : : }
1899 : :
1900 : 210 : return elts.build ();
1901 : 210 : }
1902 : :
1903 : 164494447 : default:;
1904 : : }
1905 : :
1906 : 164494447 : if (TREE_CODE_CLASS (code) != tcc_binary)
1907 : : return NULL_TREE;
1908 : :
1909 : : /* Make sure type and arg0 have the same saturating flag. */
1910 : 161867844 : gcc_checking_assert (TYPE_SATURATING (type)
1911 : : == TYPE_SATURATING (TREE_TYPE (arg1)));
1912 : :
1913 : 161867844 : 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 : 277126459 : 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 : 277126459 : if (TREE_CODE (arg0) == REAL_CST
1925 : 10951850 : && HONOR_SNANS (arg0)
1926 : 7345 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1927 : 454 : && code != NEGATE_EXPR
1928 : 454 : && code != ABS_EXPR
1929 : 277126878 : && code != ABSU_EXPR)
1930 : : return NULL_TREE;
1931 : :
1932 : 277126040 : switch (code)
1933 : : {
1934 : 193677337 : CASE_CONVERT:
1935 : 193677337 : case FLOAT_EXPR:
1936 : 193677337 : case FIX_TRUNC_EXPR:
1937 : 193677337 : case FIXED_CONVERT_EXPR:
1938 : 193677337 : 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 : 11349314 : case VIEW_CONVERT_EXPR:
1950 : 11349314 : return fold_view_convert_expr (type, arg0);
1951 : :
1952 : 29790451 : case NEGATE_EXPR:
1953 : 29790451 : {
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 : 29790451 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1958 : 29790451 : if (tem && CONSTANT_CLASS_P (tem))
1959 : : return tem;
1960 : : break;
1961 : : }
1962 : :
1963 : 33875 : case ABS_EXPR:
1964 : 33875 : case ABSU_EXPR:
1965 : 33875 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1966 : 33592 : 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 : 2223373 : case BIT_NOT_EXPR:
1979 : 2223373 : if (TREE_CODE (arg0) == INTEGER_CST)
1980 : 2222268 : return fold_not_const (arg0, type);
1981 : 1105 : 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 : 1105 : 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 : 7317083 : case TRUTH_NOT_EXPR:
2006 : 7317083 : if (TREE_CODE (arg0) == INTEGER_CST)
2007 : 6992362 : return constant_boolean_node (integer_zerop (arg0), type);
2008 : : break;
2009 : :
2010 : 179249 : case REALPART_EXPR:
2011 : 179249 : if (TREE_CODE (arg0) == COMPLEX_CST)
2012 : 179048 : return fold_convert (type, TREE_REALPART (arg0));
2013 : : break;
2014 : :
2015 : 183101 : case IMAGPART_EXPR:
2016 : 183101 : if (TREE_CODE (arg0) == COMPLEX_CST)
2017 : 182913 : return fold_convert (type, TREE_IMAGPART (arg0));
2018 : : break;
2019 : :
2020 : 12712 : case VEC_UNPACK_LO_EXPR:
2021 : 12712 : case VEC_UNPACK_HI_EXPR:
2022 : 12712 : case VEC_UNPACK_FLOAT_LO_EXPR:
2023 : 12712 : case VEC_UNPACK_FLOAT_HI_EXPR:
2024 : 12712 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2025 : 12712 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2026 : 12712 : {
2027 : 12712 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2028 : 12712 : enum tree_code subcode;
2029 : :
2030 : 12712 : if (TREE_CODE (arg0) != VECTOR_CST)
2031 : : return NULL_TREE;
2032 : :
2033 : 12712 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2034 : : return NULL_TREE;
2035 : 12712 : out_nelts = in_nelts / 2;
2036 : 12712 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2037 : :
2038 : 12712 : unsigned int offset = 0;
2039 : 12712 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2040 : 12712 : || code == VEC_UNPACK_FLOAT_LO_EXPR
2041 : : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2042 : 6347 : offset = out_nelts;
2043 : :
2044 : 12712 : 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 : 12712 : tree_vector_builder elts (type, out_nelts, 1);
2053 : 60118 : for (i = 0; i < out_nelts; i++)
2054 : : {
2055 : 47406 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2056 : 47406 : VECTOR_CST_ELT (arg0, i + offset));
2057 : 47406 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2058 : 0 : return NULL_TREE;
2059 : 47406 : elts.quick_push (elt);
2060 : : }
2061 : :
2062 : 12712 : return elts.build ();
2063 : 12712 : }
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 : 3119408192 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2082 : : {
2083 : 3119408192 : 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 : 2143776985 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2093 : : {
2094 : 2143776985 : tree type = TREE_TYPE (arg0);
2095 : :
2096 : 2143776985 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2097 : : return error_mark_node;
2098 : :
2099 : 2143776985 : 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 : 2143776985 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2104 : : {
2105 : : /* And some specific cases even faster than that. */
2106 : 2112161903 : if (code == PLUS_EXPR)
2107 : : {
2108 : 967377494 : if (integer_zerop (arg0)
2109 : 967377494 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2110 : : return arg1;
2111 : 271097703 : if (integer_zerop (arg1)
2112 : 271097703 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2113 : : return arg0;
2114 : : }
2115 : 1144784409 : else if (code == MINUS_EXPR)
2116 : : {
2117 : 107048061 : if (integer_zerop (arg1)
2118 : 107048061 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2119 : : return arg0;
2120 : : }
2121 : 1037736348 : else if (code == MULT_EXPR)
2122 : : {
2123 : 481024391 : if (integer_onep (arg0)
2124 : 481024391 : && !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 : 1036094303 : tree res = int_const_binop (code, arg0, arg1, -1);
2132 : 1036094303 : if (res != NULL_TREE)
2133 : : return res;
2134 : : }
2135 : :
2136 : 31615082 : 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 : 34038284 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2145 : : {
2146 : 34038284 : tree type = TREE_TYPE (arg0);
2147 : 34038284 : tree ctype;
2148 : :
2149 : 34038284 : 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 : 34038284 : if (!TYPE_UNSIGNED (type))
2154 : 9854902 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2155 : :
2156 : 24183382 : if (type == sizetype)
2157 : 24183382 : 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 : 24183382 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2167 : 18049 : return size_binop_loc (loc, MINUS_EXPR,
2168 : : fold_convert_loc (loc, ctype, arg0),
2169 : 18049 : 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 : 24165333 : if (tree_int_cst_equal (arg0, arg1))
2176 : 20876021 : return build_int_cst (ctype, 0);
2177 : 3289312 : else if (tree_int_cst_lt (arg1, arg0))
2178 : 2119231 : return fold_convert_loc (loc, ctype,
2179 : 2119231 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2180 : : else
2181 : 1170081 : 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 : 1113230394 : 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 : 1113230394 : tree arg1_type = TREE_TYPE (arg1);
2198 : 1113230394 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2199 : 1113230394 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2200 : 1113230394 : TYPE_SIGN (arg1_type)),
2201 : 1113230394 : !POINTER_TYPE_P (TREE_TYPE (arg1)),
2202 : 1113230394 : 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 : 36282 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2210 : : {
2211 : 36282 : bool overflow = false;
2212 : 36282 : 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 : 36282 : wide_int val;
2224 : 36282 : REAL_VALUE_TYPE r;
2225 : 36282 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2226 : :
2227 : 36282 : switch (code)
2228 : : {
2229 : 36282 : case FIX_TRUNC_EXPR:
2230 : 36282 : real_trunc (&r, VOIDmode, &x);
2231 : 36282 : 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 : 36282 : 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 : 36282 : if (! overflow)
2248 : : {
2249 : 32644 : tree lt = TYPE_MIN_VALUE (type);
2250 : 32644 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2251 : 32644 : if (real_less (&r, &l))
2252 : : {
2253 : 1974 : overflow = true;
2254 : 1974 : val = wi::to_wide (lt);
2255 : : }
2256 : : }
2257 : :
2258 : 36282 : if (! overflow)
2259 : : {
2260 : 30670 : tree ut = TYPE_MAX_VALUE (type);
2261 : 30670 : if (ut)
2262 : : {
2263 : 30670 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2264 : 30670 : if (real_less (&u, &r))
2265 : : {
2266 : 1921 : overflow = true;
2267 : 1921 : val = wi::to_wide (ut);
2268 : : }
2269 : : }
2270 : : }
2271 : :
2272 : 36282 : if (! overflow)
2273 : 28751 : 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 : 36282 : if (!flag_trapping_math || !overflow)
2283 : 29005 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2284 : : else
2285 : : t = NULL_TREE;
2286 : :
2287 : 36282 : return t;
2288 : 36282 : }
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 : 2006839 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2343 : : {
2344 : 2006839 : REAL_VALUE_TYPE value;
2345 : 2006839 : 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 : 2006839 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2351 : : {
2352 : 97618 : t = build_real (type, TREE_REAL_CST (arg1));
2353 : 97618 : 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 : 1909221 : if (HONOR_SNANS (arg1)
2359 : 1909581 : && 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 : 1909221 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2365 : 1909877 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2366 : 509 : return NULL_TREE;
2367 : :
2368 : 1908712 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2369 : 1908712 : 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 : 1908712 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2376 : 2027799 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2377 : 0 : TREE_OVERFLOW (t) = 1;
2378 : 1908712 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2379 : 2022912 : && !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 : 9540230 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2384 : 0 : && REAL_VALUE_ISINF (value)
2385 : 1908712 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2386 : 0 : TREE_OVERFLOW (t) = 1;
2387 : : else
2388 : 1908712 : 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 : 1172941083 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2485 : : {
2486 : 1172941083 : tree arg_type = TREE_TYPE (arg1);
2487 : 1172941083 : 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 : 1162063867 : 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 : 1162063867 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2501 : : || TREE_CODE (type) == OFFSET_TYPE)
2502 : : {
2503 : 1130235111 : if (TREE_CODE (arg1) == INTEGER_CST)
2504 : 1113230394 : return fold_convert_const_int_from_int (type, arg1);
2505 : 17004717 : else if (TREE_CODE (arg1) == REAL_CST)
2506 : 36282 : return fold_convert_const_int_from_real (code, type, arg1);
2507 : 16968435 : 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 : 31771944 : if (TREE_CODE (arg1) == INTEGER_CST)
2513 : : {
2514 : 24417853 : 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 : 24417853 : 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 : 24416131 : return res;
2526 : : }
2527 : 7354091 : else if (TREE_CODE (arg1) == REAL_CST)
2528 : 2006839 : return fold_convert_const_real_from_real (type, arg1);
2529 : 5347252 : 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 : 7163 : if (TREE_CODE (arg1) == VECTOR_CST
2544 : 7163 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2545 : : {
2546 : 7163 : tree elttype = TREE_TYPE (type);
2547 : 7163 : 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 : 7163 : bool step_ok_p
2551 : 7163 : = (INTEGRAL_TYPE_P (elttype)
2552 : 286 : && INTEGRAL_TYPE_P (arg1_elttype)
2553 : 7391 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2554 : 7163 : tree_vector_builder v;
2555 : 7163 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2556 : : return NULL_TREE;
2557 : 7163 : unsigned int len = v.encoded_nelts ();
2558 : 40450 : for (unsigned int i = 0; i < len; ++i)
2559 : : {
2560 : 33287 : tree elt = VECTOR_CST_ELT (arg1, i);
2561 : 33287 : tree cvt = fold_convert_const (code, elttype, elt);
2562 : 33287 : if (cvt == NULL_TREE)
2563 : 0 : return NULL_TREE;
2564 : 33287 : v.quick_push (cvt);
2565 : : }
2566 : 7163 : return v.build ();
2567 : 7163 : }
2568 : : }
2569 : : return NULL_TREE;
2570 : : }
2571 : :
2572 : : /* Construct a vector of zero elements of vector type TYPE. */
2573 : :
2574 : : static tree
2575 : 16756 : build_zero_vector (tree type)
2576 : : {
2577 : 16756 : tree t;
2578 : :
2579 : 16756 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2580 : 16756 : 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 : 2320 : fold_convertible_p (const_tree type, const_tree arg)
2587 : : {
2588 : 2320 : const_tree orig = TREE_TYPE (arg);
2589 : :
2590 : 2320 : if (type == orig)
2591 : : return true;
2592 : :
2593 : 2320 : if (TREE_CODE (arg) == ERROR_MARK
2594 : 2320 : || TREE_CODE (type) == ERROR_MARK
2595 : 2320 : || TREE_CODE (orig) == ERROR_MARK)
2596 : : return false;
2597 : :
2598 : 2320 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2599 : : return true;
2600 : :
2601 : 2320 : switch (TREE_CODE (type))
2602 : : {
2603 : 875 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2604 : 875 : case POINTER_TYPE: case REFERENCE_TYPE:
2605 : 875 : case OFFSET_TYPE:
2606 : 875 : return (INTEGRAL_TYPE_P (orig)
2607 : 248 : || (POINTER_TYPE_P (orig)
2608 : 105 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2609 : 1018 : || 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 : 1850999433 : fold_convert_loc (location_t loc, tree type, tree arg)
2632 : : {
2633 : 1850999433 : tree orig = TREE_TYPE (arg);
2634 : 1850999433 : tree tem;
2635 : :
2636 : 1850999433 : if (type == orig)
2637 : : return arg;
2638 : :
2639 : 1186534815 : if (TREE_CODE (arg) == ERROR_MARK
2640 : 1186533800 : || TREE_CODE (type) == ERROR_MARK
2641 : 1186533799 : || TREE_CODE (orig) == ERROR_MARK)
2642 : 1016 : return error_mark_node;
2643 : :
2644 : 1186533799 : switch (TREE_CODE (type))
2645 : : {
2646 : 57555234 : case POINTER_TYPE:
2647 : 57555234 : case REFERENCE_TYPE:
2648 : : /* Handle conversions between pointers to different address spaces. */
2649 : 57555234 : if (POINTER_TYPE_P (orig)
2650 : 57555234 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2651 : 46947565 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2652 : 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2653 : : /* fall through */
2654 : :
2655 : 1155873653 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2656 : 1155873653 : case OFFSET_TYPE: case BITINT_TYPE:
2657 : 1155873653 : if (TREE_CODE (arg) == INTEGER_CST)
2658 : : {
2659 : 978851234 : tem = fold_convert_const (NOP_EXPR, type, arg);
2660 : 978851234 : if (tem != NULL_TREE)
2661 : : return tem;
2662 : : }
2663 : 177022419 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2664 : 2988 : || TREE_CODE (orig) == OFFSET_TYPE)
2665 : 177022419 : 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 : 540270 : case REAL_TYPE:
2675 : 540270 : if (TREE_CODE (arg) == INTEGER_CST)
2676 : : {
2677 : 57282 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2678 : 57282 : if (tem != NULL_TREE)
2679 : : return tem;
2680 : : }
2681 : 482988 : else if (TREE_CODE (arg) == REAL_CST)
2682 : : {
2683 : 118869 : tem = fold_convert_const (NOP_EXPR, type, arg);
2684 : 118869 : if (tem != NULL_TREE)
2685 : : return tem;
2686 : : }
2687 : 364119 : 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 : 364121 : switch (TREE_CODE (orig))
2695 : : {
2696 : 643 : case INTEGER_TYPE: case BITINT_TYPE:
2697 : 643 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2698 : 643 : case POINTER_TYPE: case REFERENCE_TYPE:
2699 : 643 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2700 : :
2701 : 363478 : case REAL_TYPE:
2702 : 363478 : 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 : 30003034 : case VECTOR_TYPE:
2780 : 30003034 : if (integer_zerop (arg))
2781 : 16756 : return build_zero_vector (type);
2782 : 29986278 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2783 : 29986278 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2784 : : || VECTOR_TYPE_P (orig));
2785 : 29986278 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2786 : :
2787 : 111193 : case VOID_TYPE:
2788 : 111193 : tem = fold_ignored_result (arg);
2789 : 111193 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2790 : :
2791 : 3271 : default:
2792 : 3271 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2793 : 3271 : 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 : 49004599 : maybe_lvalue_p (const_tree x)
2806 : : {
2807 : : /* We only need to wrap lvalue tree codes. */
2808 : 49004599 : 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 : 36993247 : default:
2842 : : /* Assume the worst for front-end tree codes. */
2843 : 36993247 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2844 : : break;
2845 : : return false;
2846 : : }
2847 : :
2848 : 12069532 : return true;
2849 : : }
2850 : :
2851 : : /* Return an expr equal to X but certainly not valid as an lvalue. */
2852 : :
2853 : : tree
2854 : 45036738 : 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 : 45036738 : if (in_gimple_form)
2859 : : return x;
2860 : :
2861 : 9590964 : if (! maybe_lvalue_p (x))
2862 : : return x;
2863 : 2065093 : 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 : 117192207 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2873 : : {
2874 : 117192207 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2875 : 1089600 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2876 : : return ERROR_MARK;
2877 : :
2878 : 116370015 : switch (code)
2879 : : {
2880 : : case EQ_EXPR:
2881 : : return NE_EXPR;
2882 : 51635336 : case NE_EXPR:
2883 : 51635336 : return EQ_EXPR;
2884 : 11937328 : case GT_EXPR:
2885 : 11937328 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2886 : 12934386 : case GE_EXPR:
2887 : 12934386 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2888 : 7244495 : case LT_EXPR:
2889 : 7244495 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2890 : 7489137 : case LE_EXPR:
2891 : 7489137 : 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 : 240748 : case ORDERED_EXPR:
2905 : 240748 : return UNORDERED_EXPR;
2906 : 54081 : case UNORDERED_EXPR:
2907 : 54081 : 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 : 149167128 : swap_tree_comparison (enum tree_code code)
2918 : : {
2919 : 149167128 : 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 : 35141142 : case GT_EXPR:
2929 : 35141142 : return LT_EXPR;
2930 : 10521828 : case GE_EXPR:
2931 : 10521828 : return LE_EXPR;
2932 : 20692090 : case LT_EXPR:
2933 : 20692090 : return GT_EXPR;
2934 : 14832639 : case LE_EXPR:
2935 : 14832639 : return GE_EXPR;
2936 : 276437 : case UNGT_EXPR:
2937 : 276437 : return UNLT_EXPR;
2938 : 19510 : case UNGE_EXPR:
2939 : 19510 : return UNLE_EXPR;
2940 : 414590 : case UNLT_EXPR:
2941 : 414590 : return UNGT_EXPR;
2942 : 118632 : case UNLE_EXPR:
2943 : 118632 : 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 : 54514 : comparison_to_compcode (enum tree_code code)
2956 : : {
2957 : 54514 : 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 : 13474 : compcode_to_comparison (enum comparison_code code)
2998 : : {
2999 : 13474 : 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 : 1378671 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3038 : : {
3039 : 1378671 : return (COMPARISON_CLASS_P (cond1)
3040 : 1290205 : && COMPARISON_CLASS_P (cond2)
3041 : 1285861 : && (invert_tree_comparison
3042 : 1285861 : (TREE_CODE (cond1),
3043 : 2571722 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3044 : 73216 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3045 : 73216 : TREE_OPERAND (cond2, 0), 0)
3046 : 1401976 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3047 : 23305 : 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 : 27257 : 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 : 27257 : bool honor_nans = HONOR_NANS (ll_arg);
3063 : 27257 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3064 : 27257 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3065 : 27257 : int compcode;
3066 : :
3067 : 27257 : switch (code)
3068 : : {
3069 : 17851 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3070 : 17851 : compcode = lcompcode & rcompcode;
3071 : 17851 : break;
3072 : :
3073 : 9406 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3074 : 9406 : compcode = lcompcode | rcompcode;
3075 : 9406 : break;
3076 : :
3077 : : default:
3078 : : return NULL_TREE;
3079 : : }
3080 : :
3081 : 27257 : 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 : 22182 : compcode &= ~COMPCODE_UNORD;
3086 : 22182 : if (compcode == COMPCODE_LTGT)
3087 : : compcode = COMPCODE_NE;
3088 : 21177 : 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 : 22601 : else if (compcode == COMPCODE_FALSE)
3129 : 9127 : return constant_boolean_node (false, truth_type);
3130 : : else
3131 : : {
3132 : 13474 : enum tree_code tcode;
3133 : :
3134 : 13474 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3135 : 13474 : 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 : 7057590093 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3197 : : unsigned int flags)
3198 : : {
3199 : 7057590093 : 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 : 7058313813 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3209 : : tree type1, const_tree arg1,
3210 : : unsigned int flags)
3211 : : {
3212 : 7058313813 : bool r;
3213 : 7058313813 : if (verify_hash_value (arg0, arg1, flags, &r))
3214 : 2967050910 : return r;
3215 : :
3216 : 4091262903 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3217 : 4091262903 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3218 : :
3219 : : /* If either is ERROR_MARK, they aren't equal. */
3220 : 4091262903 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3221 : 4091261872 : || type0 == error_mark_node
3222 : 4091261870 : || 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 : 4091261869 : if (!type0 || !type1)
3228 : : return false;
3229 : :
3230 : : /* Bitwise identity makes no sense if the values have different layouts. */
3231 : 4091259307 : if ((flags & OEP_BITWISE)
3232 : 4091259307 : && !tree_nop_conversion_p (type0, type1))
3233 : : return false;
3234 : :
3235 : : /* We cannot consider pointers to different address space equal. */
3236 : 4091259307 : if (POINTER_TYPE_P (type0)
3237 : 606786620 : && POINTER_TYPE_P (type1)
3238 : 4606343335 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3239 : 515084028 : != 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 : 4091259111 : 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 : 655445788 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3249 : 655445788 : return tree_int_cst_equal (arg0, arg1);
3250 : : }
3251 : :
3252 : 3435813323 : if ((flags & OEP_ASSUME_WRAPV)
3253 : 2049282 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3254 : : {
3255 : 776994 : const_tree t_arg0 = arg0;
3256 : 776994 : const_tree t_arg1 = arg1;
3257 : 776994 : STRIP_NOPS (arg0);
3258 : 776994 : STRIP_NOPS (arg1);
3259 : : /* Only recurse if the conversion was one that was valid to strip. */
3260 : 776994 : if (t_arg0 != arg0 || t_arg1 != arg1)
3261 : 723720 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3262 : : }
3263 : :
3264 : 3435089603 : 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 : 3043809656 : bool enforce_signedness = true;
3270 : 3043809656 : if (flags & OEP_ASSUME_WRAPV)
3271 : : {
3272 : 1239805 : 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 : 3043809656 : 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 : 3043809656 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3302 : 3043809656 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3303 : 141752613 : && 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 : 2749566985 : if (element_precision (type0) != element_precision (type1))
3309 : : return false;
3310 : :
3311 : 2602767296 : STRIP_NOPS (arg0);
3312 : 2602767296 : STRIP_NOPS (arg1);
3313 : :
3314 : 2602767296 : type0 = TREE_TYPE (arg0);
3315 : 2602767296 : 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 : 2994047243 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3332 : 1209850801 : && COMPARISON_CLASS_P (arg0)
3333 : 6177858 : && COMPARISON_CLASS_P (arg1))
3334 : : {
3335 : 4538123 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3336 : :
3337 : 4538123 : if (TREE_CODE (arg0) == swap_code)
3338 : 2014689 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3339 : 2014689 : TREE_OPERAND (arg1, 1), flags)
3340 : 2034801 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3341 : 20112 : TREE_OPERAND (arg1, 0), flags);
3342 : : }
3343 : :
3344 : 2992032554 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3345 : : {
3346 : : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3347 : 1207836112 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3348 : : ;
3349 : 1207817623 : 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 : 49767005 : if (TREE_CODE (arg0) == MEM_REF
3355 : 7893658 : && DECL_P (arg1)
3356 : 5560342 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3357 : 1260921 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3358 : 50284323 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3359 : : return true;
3360 : 49536667 : else if (TREE_CODE (arg1) == MEM_REF
3361 : 31514694 : && DECL_P (arg0)
3362 : 11447141 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3363 : 2755668 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3364 : 50248084 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3365 : : return true;
3366 : 49001411 : 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 : 1784214931 : if (TREE_CODE (type0) == ERROR_MARK
3375 : 1784214931 : || TREE_CODE (type1) == ERROR_MARK
3376 : 3568429862 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3377 : 25399217 : && !(flags & OEP_ADDRESS_OF)))
3378 : 3659132 : 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 : 321975564 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3390 : 2102531185 : && (TREE_CODE (arg0) == SAVE_EXPR
3391 : 321948360 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3392 : 287252992 : || (! 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 : 1458723973 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3398 : 19434546 : 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 : 3657413 : case REAL_CST:
3408 : 3657413 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3409 : : return true;
3410 : :
3411 : 2641775 : 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 : 13900 : if (real_zerop (arg0) && real_zerop (arg1))
3416 : : return true;
3417 : : }
3418 : 2641769 : return false;
3419 : :
3420 : 730758 : case VECTOR_CST:
3421 : 730758 : {
3422 : 730758 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3423 : 730758 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3424 : : return false;
3425 : :
3426 : 719201 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3427 : 719201 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3428 : : return false;
3429 : :
3430 : 687593 : unsigned int count = vector_cst_encoded_nelts (arg0);
3431 : 943443 : for (unsigned int i = 0; i < count; ++i)
3432 : 1461892 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3433 : 730946 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3434 : : return false;
3435 : : return true;
3436 : : }
3437 : :
3438 : 12758 : case COMPLEX_CST:
3439 : 12758 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3440 : : flags)
3441 : 12758 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3442 : : flags));
3443 : :
3444 : 1171307 : case STRING_CST:
3445 : 1171307 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3446 : 1171307 : && ! memcmp (TREE_STRING_POINTER (arg0),
3447 : 751733 : TREE_STRING_POINTER (arg1),
3448 : 751733 : 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 : 12962329 : case ADDR_EXPR:
3457 : 12962329 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3458 : 12962329 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3459 : : flags | OEP_ADDRESS_OF
3460 : 12962329 : | OEP_MATCH_SIDE_EFFECTS);
3461 : 172427 : case CONSTRUCTOR:
3462 : 172427 : {
3463 : : /* In GIMPLE empty constructors are allowed in initializers of
3464 : : aggregates. */
3465 : 172427 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3466 : : return true;
3467 : :
3468 : : /* See sem_variable::equals in ipa-icf for a similar approach. */
3469 : 135379 : if (TREE_CODE (type0) != TREE_CODE (type1))
3470 : : return false;
3471 : 135379 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3472 : : {
3473 : : /* For arrays, check that the sizes all match. */
3474 : 44 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3475 : 44 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3476 : 44 : || siz0 < 0
3477 : 88 : || siz0 != int_size_in_bytes (type1))
3478 : 0 : return false;
3479 : : }
3480 : 135335 : else if (!types_compatible_p (type0, type1))
3481 : : return false;
3482 : :
3483 : 135379 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3484 : 135379 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3485 : 406137 : 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 : 135379 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3491 : :
3492 : 357812412 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3493 : : {
3494 : 193813 : constructor_elt *c0 = &(*v0)[idx];
3495 : 193813 : constructor_elt *c1 = &(*v1)[idx];
3496 : :
3497 : : /* Check that the values are the same... */
3498 : 193813 : if (c0->value != c1->value
3499 : 193813 : && !operand_equal_p (c0->value, c1->value, flags))
3500 : : return false;
3501 : :
3502 : : /* ... and that they apply to the same field! */
3503 : 107755 : if (c0->index != c1->index
3504 : 107755 : && (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 : 1440016828 : 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 : 1440016828 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3539 : : {
3540 : 7075132 : case tcc_unary:
3541 : : /* Two conversions are equal only if signedness and modes match. */
3542 : 7075132 : switch (TREE_CODE (arg0))
3543 : : {
3544 : 6702612 : CASE_CONVERT:
3545 : 6702612 : case FIX_TRUNC_EXPR:
3546 : 6702612 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3547 : : return false;
3548 : : break;
3549 : : default:
3550 : : break;
3551 : : }
3552 : :
3553 : 7075111 : return OP_SAME_WITH_NULL (0);
3554 : :
3555 : :
3556 : 22141614 : case tcc_comparison:
3557 : 22141614 : case tcc_binary:
3558 : 22141614 : if (OP_SAME (0) && OP_SAME (1))
3559 : : return true;
3560 : :
3561 : : /* For commutative ops, allow the other order. */
3562 : 16340304 : return (commutative_tree_code (TREE_CODE (arg0))
3563 : 12459712 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3564 : 12459712 : TREE_OPERAND (arg1, 1), flags)
3565 : 16570698 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3566 : 230394 : TREE_OPERAND (arg1, 0), flags));
3567 : :
3568 : 870263434 : 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 : 870263434 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3573 : 870263434 : && (TREE_SIDE_EFFECTS (arg0)
3574 : 807704607 : || TREE_SIDE_EFFECTS (arg1)))
3575 : : return false;
3576 : :
3577 : 870035408 : switch (TREE_CODE (arg0))
3578 : : {
3579 : 3943419 : case INDIRECT_REF:
3580 : 3943419 : if (!(flags & OEP_ADDRESS_OF))
3581 : : {
3582 : 3920435 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3583 : : return false;
3584 : : /* Verify that the access types are compatible. */
3585 : 3918944 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3586 : : return false;
3587 : : }
3588 : 3879277 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3589 : 3879277 : return OP_SAME (0);
3590 : :
3591 : 655180 : case IMAGPART_EXPR:
3592 : : /* Require the same offset. */
3593 : 655180 : if (!operand_equal_p (TYPE_SIZE (type0),
3594 : 655180 : TYPE_SIZE (type1),
3595 : : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3596 : : return false;
3597 : :
3598 : : /* Fallthru. */
3599 : 2438672 : case REALPART_EXPR:
3600 : 2438672 : case VIEW_CONVERT_EXPR:
3601 : 2438672 : return OP_SAME (0);
3602 : :
3603 : 79224815 : case TARGET_MEM_REF:
3604 : 79224815 : case MEM_REF:
3605 : 79224815 : if (!(flags & OEP_ADDRESS_OF))
3606 : : {
3607 : : /* Require equal access sizes */
3608 : 16807477 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3609 : 16807477 : && (!TYPE_SIZE (type0)
3610 : 1105088 : || !TYPE_SIZE (type1)
3611 : 1100439 : || !operand_equal_p (TYPE_SIZE (type0),
3612 : 1100439 : TYPE_SIZE (type1),
3613 : : flags)))
3614 : 1100992 : return false;
3615 : : /* Verify that access happens in similar types. */
3616 : 15706485 : if (!types_compatible_p (type0, type1))
3617 : : return false;
3618 : : /* Verify that accesses are TBAA compatible. */
3619 : 15363298 : if (!alias_ptr_types_compatible_p
3620 : 15363298 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3621 : 15363298 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3622 : 14430763 : || (MR_DEPENDENCE_CLIQUE (arg0)
3623 : 14430763 : != MR_DEPENDENCE_CLIQUE (arg1))
3624 : 27397902 : || (MR_DEPENDENCE_BASE (arg0)
3625 : 12034604 : != MR_DEPENDENCE_BASE (arg1)))
3626 : : return false;
3627 : : /* Verify that alignment is compatible. */
3628 : 11573381 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3629 : : return false;
3630 : : }
3631 : 73832097 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3632 : 125409187 : return (OP_SAME (0) && OP_SAME (1)
3633 : : /* TARGET_MEM_REF require equal extra operands. */
3634 : 98782883 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3635 : 545028 : || (OP_SAME_WITH_NULL (2)
3636 : 273675 : && OP_SAME_WITH_NULL (3)
3637 : 267681 : && OP_SAME_WITH_NULL (4))));
3638 : :
3639 : 38304210 : case ARRAY_REF:
3640 : 38304210 : case ARRAY_RANGE_REF:
3641 : 38304210 : if (!OP_SAME (0))
3642 : : return false;
3643 : 33397733 : 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 : 33397733 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3647 : 33397733 : TREE_OPERAND (arg1, 1))
3648 : 30453322 : || OP_SAME (1))
3649 : 5915796 : && OP_SAME_WITH_NULL (2)
3650 : 5914306 : && 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 : 42269937 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3654 : 2957153 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3655 : 2641 : || (operand_equal_p (array_ref_low_bound
3656 : 2641 : (CONST_CAST_TREE (arg0)),
3657 : : array_ref_low_bound
3658 : 2641 : (CONST_CAST_TREE (arg1)), flags)
3659 : 2641 : && operand_equal_p (array_ref_element_size
3660 : 2641 : (CONST_CAST_TREE (arg0)),
3661 : : array_ref_element_size
3662 : 2641 : (CONST_CAST_TREE (arg1)),
3663 : : flags))));
3664 : :
3665 : 745651180 : 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 : 745651180 : if (!OP_SAME_WITH_NULL (0))
3669 : : return false;
3670 : 56758096 : {
3671 : 56758096 : 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 : 56758096 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3677 : 56758096 : if (!OP_SAME (1))
3678 : : {
3679 : 33038499 : if (compare_address
3680 : 602101 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3681 : : {
3682 : 602098 : tree field0 = TREE_OPERAND (arg0, 1);
3683 : 602098 : tree field1 = TREE_OPERAND (arg1, 1);
3684 : :
3685 : : /* Non-FIELD_DECL operands can appear in C++ templates. */
3686 : 602098 : if (TREE_CODE (field0) != FIELD_DECL
3687 : 602098 : || TREE_CODE (field1) != FIELD_DECL)
3688 : : return false;
3689 : :
3690 : 602098 : if (!DECL_FIELD_OFFSET (field0)
3691 : 602098 : || !DECL_FIELD_OFFSET (field1))
3692 : 3 : return field0 == field1;
3693 : :
3694 : 602095 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3695 : 602095 : DECL_FIELD_OFFSET (field1), flags)
3696 : 793485 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3697 : 191390 : DECL_FIELD_BIT_OFFSET (field1),
3698 : : flags))
3699 : 563290 : return false;
3700 : : }
3701 : : else
3702 : : return false;
3703 : : }
3704 : : }
3705 : 23758402 : return OP_SAME_WITH_NULL (2);
3706 : :
3707 : 473064 : case BIT_FIELD_REF:
3708 : 473064 : if (!OP_SAME (0))
3709 : : return false;
3710 : 366862 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3711 : 366862 : return OP_SAME (1) && OP_SAME (2);
3712 : :
3713 : : default:
3714 : : return false;
3715 : : }
3716 : :
3717 : 51637937 : case tcc_expression:
3718 : 51637937 : switch (TREE_CODE (arg0))
3719 : : {
3720 : 46652738 : case ADDR_EXPR:
3721 : : /* Be sure we pass right ADDRESS_OF flag. */
3722 : 46652738 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3723 : 46652738 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3724 : 46652738 : TREE_OPERAND (arg1, 0),
3725 : 46652738 : flags | OEP_ADDRESS_OF);
3726 : :
3727 : 677275 : case TRUTH_NOT_EXPR:
3728 : 677275 : return OP_SAME (0);
3729 : :
3730 : 40899 : case TRUTH_ANDIF_EXPR:
3731 : 40899 : case TRUTH_ORIF_EXPR:
3732 : 40899 : 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 : 19592 : case TRUTH_AND_EXPR:
3742 : 19592 : case TRUTH_OR_EXPR:
3743 : 19592 : case TRUTH_XOR_EXPR:
3744 : 19592 : if (OP_SAME (0) && OP_SAME (1))
3745 : : return true;
3746 : :
3747 : : /* Otherwise take into account this is a commutative operation. */
3748 : 19574 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3749 : 19574 : TREE_OPERAND (arg1, 1), flags)
3750 : 19577 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3751 : 3 : TREE_OPERAND (arg1, 0), flags));
3752 : :
3753 : 121628 : case COND_EXPR:
3754 : 121628 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3755 : 49189 : return false;
3756 : 72439 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3757 : 72439 : 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 : 23405 : case MODIFY_EXPR:
3774 : 23405 : case INIT_EXPR:
3775 : 23405 : case COMPOUND_EXPR:
3776 : 23405 : case PREDECREMENT_EXPR:
3777 : 23405 : case PREINCREMENT_EXPR:
3778 : 23405 : case POSTDECREMENT_EXPR:
3779 : 23405 : case POSTINCREMENT_EXPR:
3780 : 23405 : if (flags & OEP_LEXICOGRAPHIC)
3781 : 165 : return OP_SAME (0) && OP_SAME (1);
3782 : : return false;
3783 : :
3784 : 90072 : case CLEANUP_POINT_EXPR:
3785 : 90072 : case EXPR_STMT:
3786 : 90072 : case SAVE_EXPR:
3787 : 90072 : if (flags & OEP_LEXICOGRAPHIC)
3788 : 208 : return OP_SAME (0);
3789 : : return false;
3790 : :
3791 : 107026 : case OBJ_TYPE_REF:
3792 : : /* Virtual table reference. */
3793 : 214052 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3794 : 107026 : OBJ_TYPE_REF_EXPR (arg1), flags))
3795 : : return false;
3796 : 895 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3797 : 895 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3798 : 895 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3799 : : return false;
3800 : 895 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3801 : 895 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3802 : : return false;
3803 : 895 : if (virtual_method_call_p (arg0))
3804 : : {
3805 : 895 : if (!virtual_method_call_p (arg1))
3806 : : return false;
3807 : 895 : return types_same_for_odr (obj_type_ref_class (arg0),
3808 : 1790 : obj_type_ref_class (arg1));
3809 : : }
3810 : : return false;
3811 : :
3812 : : default:
3813 : : return false;
3814 : : }
3815 : :
3816 : 2900357 : case tcc_vl_exp:
3817 : 2900357 : switch (TREE_CODE (arg0))
3818 : : {
3819 : 2900357 : case CALL_EXPR:
3820 : 2900357 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3821 : 2900357 : != (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 : 2900357 : 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 : 2 : 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 : 2900355 : 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 : 1882146 : {
3843 : 1882146 : unsigned int cef = call_expr_flags (arg0);
3844 : 1882146 : if (flags & OEP_PURE_SAME)
3845 : 0 : cef &= ECF_CONST | ECF_PURE;
3846 : : else
3847 : 1882146 : cef &= ECF_CONST;
3848 : 1882146 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3849 : : return false;
3850 : : }
3851 : :
3852 : : /* Now see if all the arguments are the same. */
3853 : 33016 : {
3854 : 33016 : const_call_expr_arg_iterator iter0, iter1;
3855 : 33016 : const_tree a0, a1;
3856 : 66032 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3857 : 33016 : a1 = first_const_call_expr_arg (arg1, &iter1);
3858 : 41121 : a0 && a1;
3859 : 8105 : a0 = next_const_call_expr_arg (&iter0),
3860 : 8105 : a1 = next_const_call_expr_arg (&iter1))
3861 : 34508 : 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 : 163603679 : case tcc_declaration:
3873 : : /* Consider __builtin_sqrt equal to sqrt. */
3874 : 163603679 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3875 : 6426423 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3876 : 340010 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3877 : 5754467 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3878 : 340010 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3879 : :
3880 : 157849212 : if (DECL_P (arg0)
3881 : 157849212 : && (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 : 320163380 : case tcc_exceptional:
3896 : 320163380 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3897 : : {
3898 : 19251 : 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 : 19251 : 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 : 538 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3914 : 1076 : TYPE_VECTOR_SUBPARTS (type1)))
3915 : : return false;
3916 : :
3917 : 538 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3918 : 538 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3919 : 538 : unsigned int len = vec_safe_length (v0);
3920 : :
3921 : 1076 : if (len != vec_safe_length (v1))
3922 : : return false;
3923 : :
3924 : 3356 : for (unsigned int i = 0; i < len; i++)
3925 : : {
3926 : 2991 : constructor_elt *c0 = &(*v0)[i];
3927 : 2991 : constructor_elt *c1 = &(*v1)[i];
3928 : :
3929 : 2991 : 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 : 2818 : || (c0->index
3934 : 2588 : && (TREE_CODE (c0->index) != INTEGER_CST
3935 : 2588 : || compare_tree_int (c0->index, i)))
3936 : 5809 : || (c1->index
3937 : 2588 : && (TREE_CODE (c1->index) != INTEGER_CST
3938 : 2588 : || compare_tree_int (c1->index, i))))
3939 : 173 : return false;
3940 : : }
3941 : : return true;
3942 : : }
3943 : 320144129 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3944 : 2972 : && (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 : 2231110 : case tcc_statement:
3967 : 2231110 : 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 : 2671398829 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3994 : : unsigned int flags)
3995 : : {
3996 : 2671398829 : int i;
3997 : 2671398829 : enum tree_code code;
3998 : 2671398829 : enum tree_code_class tclass;
3999 : :
4000 : 2671398829 : if (t == NULL_TREE || t == error_mark_node)
4001 : : {
4002 : 74792005 : hstate.merge_hash (0);
4003 : 74792005 : return;
4004 : : }
4005 : :
4006 : 2596606824 : STRIP_ANY_LOCATION_WRAPPER (t);
4007 : :
4008 : 2596606824 : if (!(flags & OEP_ADDRESS_OF))
4009 : 2343274545 : STRIP_NOPS (t);
4010 : :
4011 : 2596606824 : code = TREE_CODE (t);
4012 : :
4013 : 2596606824 : switch (code)
4014 : : {
4015 : : /* Alas, constants aren't shared, so we can't rely on pointer
4016 : : identity. */
4017 : 825 : case VOID_CST:
4018 : 825 : hstate.merge_hash (0);
4019 : 825 : return;
4020 : 795232661 : case INTEGER_CST:
4021 : 795232661 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4022 : 1616311514 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4023 : 821078853 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4024 : : return;
4025 : 15384929 : case REAL_CST:
4026 : 15384929 : {
4027 : 15384929 : unsigned int val2;
4028 : 15384929 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4029 : : val2 = rvc_zero;
4030 : : else
4031 : 15153460 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4032 : 15384929 : hstate.merge_hash (val2);
4033 : 15384929 : 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 : 10202196 : case STRING_CST:
4042 : 10202196 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4043 : 10202196 : TREE_STRING_LENGTH (t));
4044 : 10202196 : return;
4045 : 190 : case RAW_DATA_CST:
4046 : 190 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4047 : 190 : RAW_DATA_LENGTH (t));
4048 : 190 : return;
4049 : 208597 : case COMPLEX_CST:
4050 : 208597 : hash_operand (TREE_REALPART (t), hstate, flags);
4051 : 208597 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4052 : 208597 : return;
4053 : 2956536 : case VECTOR_CST:
4054 : 2956536 : {
4055 : 2956536 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4056 : 2956536 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4057 : 2956536 : unsigned int count = vector_cst_encoded_nelts (t);
4058 : 9186679 : for (unsigned int i = 0; i < count; ++i)
4059 : 6230143 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4060 : : return;
4061 : : }
4062 : 877605653 : case SSA_NAME:
4063 : : /* We can just compare by pointer. */
4064 : 877605653 : hstate.add_hwi (SSA_NAME_VERSION (t));
4065 : 877605653 : 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 : 275074 : for (; t; t = TREE_CHAIN (t))
4079 : 137537 : hash_operand (TREE_VALUE (t), hstate, flags);
4080 : : return;
4081 : 4868814 : case CONSTRUCTOR:
4082 : 4868814 : {
4083 : 4868814 : unsigned HOST_WIDE_INT idx;
4084 : 4868814 : tree field, value;
4085 : 4868814 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4086 : 4868814 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4087 : 19858737 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4088 : : {
4089 : : /* In GIMPLE the indexes can be either NULL or matching i. */
4090 : 14989923 : if (field == NULL_TREE)
4091 : 1090456 : field = bitsize_int (idx);
4092 : 14989923 : if (TREE_CODE (field) == FIELD_DECL)
4093 : : {
4094 : 9914378 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4095 : 9914378 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4096 : : }
4097 : : else
4098 : 5075545 : hash_operand (field, hstate, flags);
4099 : 14989923 : 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 : 20306607 : 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 : 20306607 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4125 : 20306607 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4126 : : {
4127 : 7137846 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4128 : 7137846 : code = TREE_CODE (t);
4129 : : }
4130 : : /* FALL THROUGH */
4131 : 890008576 : default:
4132 : 890008576 : 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 : 890008576 : tclass = TREE_CODE_CLASS (code);
4139 : :
4140 : 890008576 : if (tclass == tcc_declaration)
4141 : : {
4142 : : /* DECL's have a unique ID */
4143 : 559040470 : hstate.add_hwi (DECL_UID (t));
4144 : : }
4145 : 330968106 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4146 : : {
4147 : : /* For comparisons that can be swapped, use the lower
4148 : : tree code. */
4149 : 144123 : enum tree_code ccode = swap_tree_comparison (code);
4150 : 144123 : if (code < ccode)
4151 : 60809 : ccode = code;
4152 : 144123 : hstate.add_object (ccode);
4153 : 144123 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4154 : 144123 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4155 : : }
4156 : 330823983 : else if (CONVERT_EXPR_CODE_P (code))
4157 : : {
4158 : : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4159 : : operand_equal_p. */
4160 : 5127946 : enum tree_code ccode = NOP_EXPR;
4161 : 5127946 : 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 : 5127946 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4168 : 5127946 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4169 : : }
4170 : : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4171 : 325696037 : else if (code == MEM_REF
4172 : 80516361 : && (flags & OEP_ADDRESS_OF) != 0
4173 : 71562040 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4174 : 14259136 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4175 : 339711156 : && integer_zerop (TREE_OPERAND (t, 1)))
4176 : 6383716 : 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 : 319312321 : else if (!IS_EXPR_CODE_CLASS (tclass))
4181 : 252 : gcc_assert (flags & OEP_HASH_CHECK);
4182 : : else
4183 : : {
4184 : 319312069 : unsigned int sflags = flags;
4185 : :
4186 : 319312069 : hstate.add_object (code);
4187 : :
4188 : 319312069 : switch (code)
4189 : : {
4190 : 126893389 : case ADDR_EXPR:
4191 : 126893389 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4192 : 126893389 : flags |= OEP_ADDRESS_OF;
4193 : 126893389 : sflags = flags;
4194 : 126893389 : break;
4195 : :
4196 : 78575593 : case INDIRECT_REF:
4197 : 78575593 : case MEM_REF:
4198 : 78575593 : case TARGET_MEM_REF:
4199 : 78575593 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4200 : 78575593 : sflags = flags;
4201 : 78575593 : break;
4202 : :
4203 : 75350833 : case COMPONENT_REF:
4204 : 75350833 : if (sflags & OEP_ADDRESS_OF)
4205 : : {
4206 : 38275476 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4207 : 38275476 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4208 : : hstate, flags & ~OEP_ADDRESS_OF);
4209 : 38275476 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4210 : : hstate, flags & ~OEP_ADDRESS_OF);
4211 : 38275476 : return;
4212 : : }
4213 : : break;
4214 : 15066145 : case ARRAY_REF:
4215 : 15066145 : case ARRAY_RANGE_REF:
4216 : 15066145 : case BIT_FIELD_REF:
4217 : 15066145 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4218 : 15066145 : break;
4219 : :
4220 : 8483 : case COND_EXPR:
4221 : 8483 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4222 : 8483 : 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 : 55452 : case CALL_EXPR:
4237 : 55452 : 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 : 228055 : case OBJ_TYPE_REF:
4249 : : /* Virtual table reference. */
4250 : 228055 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4251 : 228055 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4252 : 228055 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4253 : 228055 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4254 : 228055 : if (!virtual_method_call_p (t))
4255 : : return;
4256 : 228040 : if (tree c = obj_type_ref_class (t))
4257 : : {
4258 : 228040 : 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 : 228040 : if (TREE_CODE (c) == TYPE_DECL
4262 : 228040 : && DECL_ASSEMBLER_NAME_SET_P (c))
4263 : 670 : hstate.add_object
4264 : 670 : (IDENTIFIER_HASH_VALUE
4265 : : (DECL_ASSEMBLER_NAME (c)));
4266 : : }
4267 : 228040 : 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 : 280808466 : 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 : 280808466 : 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 : 17168405 : inchash::hash one, two;
4289 : 17168405 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4290 : 17168405 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4291 : 17168405 : hstate.add_commutative (one, two);
4292 : : }
4293 : : else
4294 : 736895021 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4295 : 682870146 : hash_operand (TREE_OPERAND (t, i), hstate,
4296 : : i == 0 ? flags : sflags);
4297 : : }
4298 : : return;
4299 : : }
4300 : : }
4301 : :
4302 : : bool
4303 : 7063188127 : 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 : 7063188127 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4310 : : {
4311 : 2969172057 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4312 : : {
4313 : 452993162 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4314 : : {
4315 : 83093597 : inchash::hash hstate0 (0), hstate1 (0);
4316 : 83093597 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4317 : 83093597 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4318 : 83093597 : hashval_t h0 = hstate0.end ();
4319 : 83093597 : hashval_t h1 = hstate1.end ();
4320 : 83093597 : gcc_assert (h0 == h1);
4321 : : }
4322 : 452993162 : *ret = true;
4323 : : }
4324 : : else
4325 : 2516178895 : *ret = false;
4326 : :
4327 : 2969172057 : 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 : 2967059754 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4341 : : {
4342 : 2967059754 : 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 : 1821418829 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4355 : : {
4356 : 1821418829 : default_compare_instance.hash_operand (t, hstate, flags);
4357 : 1821418829 : }
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 : 17989803 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4366 : : {
4367 : 17989803 : if (operand_equal_p (arg0, arg1, 0))
4368 : : return true;
4369 : :
4370 : 34289496 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4371 : 29186651 : || ! 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 : 5861683 : tree op0 = arg0;
4378 : 5861683 : tree op1 = arg1;
4379 : 5861683 : STRIP_NOPS (op0);
4380 : 5861683 : STRIP_NOPS (op1);
4381 : 5861683 : 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 : 4891124 : if (CONVERT_EXPR_P (arg1)
4387 : 781105 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4388 : 781059 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4389 : 781059 : < TYPE_PRECISION (TREE_TYPE (arg1))
4390 : 5977475 : && 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 : 55971359 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4407 : : {
4408 : 59649048 : enum tree_code code = TREE_CODE (arg);
4409 : 59649048 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4410 : :
4411 : : /* We can handle some of the tcc_expression cases here. */
4412 : 59649048 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4413 : : tclass = tcc_unary;
4414 : 59050735 : else if (tclass == tcc_expression
4415 : 591126 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4416 : 591126 : || code == COMPOUND_EXPR))
4417 : : tclass = tcc_binary;
4418 : :
4419 : 59039973 : switch (tclass)
4420 : : {
4421 : 3677689 : case tcc_unary:
4422 : 3677689 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4423 : :
4424 : 5145190 : case tcc_binary:
4425 : 5145190 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4426 : 5145190 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4427 : :
4428 : : case tcc_constant:
4429 : : return true;
4430 : :
4431 : 580364 : case tcc_expression:
4432 : 580364 : if (code == COND_EXPR)
4433 : 769 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4434 : 769 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4435 : 833 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4436 : : return false;
4437 : :
4438 : 602891 : 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 : 602891 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4446 : 602891 : TREE_OPERAND (arg, 1), 0))
4447 : : return false;
4448 : :
4449 : 602891 : if (*cval1 == 0)
4450 : 600838 : *cval1 = TREE_OPERAND (arg, 0);
4451 : 2053 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4452 : : ;
4453 : 1934 : else if (*cval2 == 0)
4454 : 0 : *cval2 = TREE_OPERAND (arg, 0);
4455 : 1934 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4456 : : ;
4457 : : else
4458 : : return false;
4459 : :
4460 : 600957 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4461 : : ;
4462 : 600957 : else if (*cval2 == 0)
4463 : 600838 : *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 : 294917 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4570 : : {
4571 : 294917 : 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 : 294917 : 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 : 294917 : if (TREE_SIDE_EFFECTS (omitted))
4580 : 13496 : return build2_loc (loc, COMPOUND_EXPR, type,
4581 : 13496 : fold_ignored_result (omitted), t);
4582 : :
4583 : 281421 : 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 : 7139 : omit_two_operands_loc (location_t loc, tree type, tree result,
4597 : : tree omitted1, tree omitted2)
4598 : : {
4599 : 7139 : tree t = fold_convert_loc (loc, type, result);
4600 : :
4601 : 7139 : if (TREE_SIDE_EFFECTS (omitted2))
4602 : 72 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4603 : 7139 : if (TREE_SIDE_EFFECTS (omitted1))
4604 : 179 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4605 : :
4606 : 7139 : 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 : 48458387 : fold_truth_not_expr (location_t loc, tree arg)
4619 : : {
4620 : 48458387 : tree type = TREE_TYPE (arg);
4621 : 48458387 : enum tree_code code = TREE_CODE (arg);
4622 : 48458387 : 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 : 48458387 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4629 : : {
4630 : 38175537 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4631 : 32226987 : if (FLOAT_TYPE_P (op_type)
4632 : 5958637 : && flag_trapping_math
4633 : 5928162 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4634 : 44064253 : && code != NE_EXPR && code != EQ_EXPR)
4635 : : return NULL_TREE;
4636 : :
4637 : 32976008 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4638 : 32976008 : if (code == ERROR_MARK)
4639 : : return NULL_TREE;
4640 : :
4641 : 32976008 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4642 : 32976008 : TREE_OPERAND (arg, 1));
4643 : 32976008 : copy_warning (ret, arg);
4644 : 32976008 : return ret;
4645 : : }
4646 : :
4647 : 10282850 : switch (code)
4648 : : {
4649 : 0 : case INTEGER_CST:
4650 : 0 : return constant_boolean_node (integer_zerop (arg), type);
4651 : :
4652 : 48528 : case TRUTH_AND_EXPR:
4653 : 48528 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4654 : 48528 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4655 : 97056 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4656 : 48528 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4657 : 97056 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4658 : :
4659 : 2442 : case TRUTH_OR_EXPR:
4660 : 2442 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4661 : 2442 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4662 : 4884 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4663 : 2442 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4664 : 4884 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4665 : :
4666 : 33617 : 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 : 33617 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4673 : 35 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4674 : 70 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4675 : : else
4676 : 33582 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4677 : 33582 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4678 : 67164 : TREE_OPERAND (arg, 1));
4679 : :
4680 : 248556 : case TRUTH_ANDIF_EXPR:
4681 : 248556 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4682 : 248556 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4683 : 497112 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4684 : 248556 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4685 : 497112 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4686 : :
4687 : 18909 : case TRUTH_ORIF_EXPR:
4688 : 18909 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4689 : 18909 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4690 : 37818 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4691 : 18909 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4692 : 37818 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4693 : :
4694 : 794186 : case TRUTH_NOT_EXPR:
4695 : 794186 : return TREE_OPERAND (arg, 0);
4696 : :
4697 : 9763 : case COND_EXPR:
4698 : 9763 : {
4699 : 9763 : tree arg1 = TREE_OPERAND (arg, 1);
4700 : 9763 : tree arg2 = TREE_OPERAND (arg, 2);
4701 : :
4702 : 9763 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4703 : 9763 : 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 : 9763 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4709 : 9763 : VOID_TYPE_P (TREE_TYPE (arg1))
4710 : 9763 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4711 : 9763 : VOID_TYPE_P (TREE_TYPE (arg2))
4712 : 19523 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4713 : : }
4714 : :
4715 : 318 : case COMPOUND_EXPR:
4716 : 318 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4717 : 636 : return build2_loc (loc, COMPOUND_EXPR, type,
4718 : 318 : TREE_OPERAND (arg, 0),
4719 : 636 : 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 : 70873 : CASE_CONVERT:
4726 : 70873 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4727 : 70809 : 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 : 503 : case BIT_AND_EXPR:
4737 : 503 : 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 : 1782777 : fold_invert_truthvalue (location_t loc, tree arg)
4761 : : {
4762 : 1782777 : tree type = TREE_TYPE (arg);
4763 : 3565530 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4764 : : ? BIT_NOT_EXPR
4765 : : : TRUTH_NOT_EXPR,
4766 : 1782777 : 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 : 41175424 : invert_truthvalue_loc (location_t loc, tree arg)
4775 : : {
4776 : 41175424 : if (TREE_CODE (arg) == ERROR_MARK)
4777 : : return arg;
4778 : :
4779 : 41175424 : tree type = TREE_TYPE (arg);
4780 : 82350848 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4781 : : ? BIT_NOT_EXPR
4782 : : : TRUTH_NOT_EXPR,
4783 : 41175424 : 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 : 775095 : 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 : 775095 : tree result, bftype;
4798 : :
4799 : : /* Attempt not to lose the access path if possible. */
4800 : 775095 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4801 : : {
4802 : 771186 : tree ninner = TREE_OPERAND (orig_inner, 0);
4803 : 771186 : machine_mode nmode;
4804 : 771186 : poly_int64 nbitsize, nbitpos;
4805 : 771186 : tree noffset;
4806 : 771186 : int nunsignedp, nreversep, nvolatilep = 0;
4807 : 771186 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4808 : : &noffset, &nmode, &nunsignedp,
4809 : : &nreversep, &nvolatilep);
4810 : 771186 : if (base == inner
4811 : 771054 : && noffset == NULL_TREE
4812 : 771054 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4813 : 771030 : && !reversep
4814 : 770958 : && !nreversep
4815 : 1542144 : && !nvolatilep)
4816 : : {
4817 : 770958 : inner = ninner;
4818 : 771186 : bitpos -= nbitpos;
4819 : : }
4820 : : }
4821 : :
4822 : 775095 : alias_set_type iset = get_alias_set (orig_inner);
4823 : 775095 : if (iset == 0 && get_alias_set (inner) != iset)
4824 : 229 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4825 : : build_fold_addr_expr (inner),
4826 : : build_int_cst (ptr_type_node, 0));
4827 : :
4828 : 775095 : if (known_eq (bitpos, 0) && !reversep)
4829 : : {
4830 : 17899 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4831 : 35798 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4832 : 17701 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4833 : 202 : && tree_fits_shwi_p (size)
4834 : 18101 : && tree_to_shwi (size) == bitsize)
4835 : 179 : return fold_convert_loc (loc, type, inner);
4836 : : }
4837 : :
4838 : 774916 : bftype = type;
4839 : 774916 : if (TYPE_PRECISION (bftype) != bitsize
4840 : 774916 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4841 : 388 : bftype = build_nonstandard_integer_type (bitsize, 0);
4842 : :
4843 : 774916 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4844 : 774916 : bitsize_int (bitsize), bitsize_int (bitpos));
4845 : 774916 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4846 : :
4847 : 774916 : if (bftype != type)
4848 : 388 : 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 : 4130721 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4875 : : tree compare_type, tree lhs, tree rhs)
4876 : : {
4877 : 4130721 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4878 : 4130721 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4879 : 4130721 : tree type = TREE_TYPE (lhs);
4880 : 4130721 : tree unsigned_type;
4881 : 4130721 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4882 : 4130721 : machine_mode lmode, rmode;
4883 : 4130721 : scalar_int_mode nmode;
4884 : 4130721 : int lunsignedp, runsignedp;
4885 : 4130721 : int lreversep, rreversep;
4886 : 4130721 : int lvolatilep = 0, rvolatilep = 0;
4887 : 4130721 : tree linner, rinner = NULL_TREE;
4888 : 4130721 : tree mask;
4889 : 4130721 : 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 : 4130721 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4897 : : &lunsignedp, &lreversep, &lvolatilep);
4898 : 4130721 : if (linner == lhs
4899 : 4130721 : || !known_size_p (plbitsize)
4900 : 4130721 : || !plbitsize.is_constant (&lbitsize)
4901 : 4130721 : || !plbitpos.is_constant (&lbitpos)
4902 : 8261442 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4903 : 761481 : || offset != 0
4904 : 761456 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4905 : 4892177 : || lvolatilep)
4906 : 3369325 : return 0;
4907 : :
4908 : 761396 : if (const_p)
4909 : 748698 : 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 : 12698 : rinner
4915 : 12698 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4916 : : &runsignedp, &rreversep, &rvolatilep);
4917 : :
4918 : 12698 : if (rinner == rhs
4919 : 12654 : || maybe_ne (lbitpos, rbitpos)
4920 : 12620 : || maybe_ne (lbitsize, rbitsize)
4921 : 12620 : || lunsignedp != runsignedp
4922 : 12620 : || lreversep != rreversep
4923 : 12620 : || offset != 0
4924 : 12620 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4925 : 25318 : || rvolatilep)
4926 : : return 0;
4927 : : }
4928 : :
4929 : : /* Honor the C++ memory model and mimic what RTL expansion does. */
4930 : 761318 : poly_uint64 bitstart = 0;
4931 : 761318 : poly_uint64 bitend = 0;
4932 : 761318 : if (TREE_CODE (lhs) == COMPONENT_REF)
4933 : : {
4934 : 761318 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4935 : 761318 : 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 : 1522636 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4942 : 748698 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4943 : 12620 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4944 : : TYPE_ALIGN (TREE_TYPE (rinner))),
4945 : 761318 : 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 : 759321 : 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 : 759321 : nbitsize = GET_MODE_BITSIZE (nmode);
4956 : 759321 : nbitpos = lbitpos & ~ (nbitsize - 1);
4957 : 759321 : lbitpos -= nbitpos;
4958 : 759321 : if (nbitsize == lbitsize)
4959 : : return 0;
4960 : :
4961 : 759321 : 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 : 759321 : mask = build_int_cst_type (unsigned_type, -1);
4966 : 759321 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4967 : 759321 : mask = const_binop (RSHIFT_EXPR, mask,
4968 : 759321 : size_int (nbitsize - lbitsize - lbitpos));
4969 : :
4970 : 759321 : if (! const_p)
4971 : : {
4972 : 11065 : if (nbitpos < 0)
4973 : : return 0;
4974 : :
4975 : : /* If not comparing with constant, just rework the comparison
4976 : : and return. */
4977 : 11065 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4978 : 11065 : nbitsize, nbitpos, 1, lreversep);
4979 : 11065 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4980 : 11065 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4981 : 11065 : nbitsize, nbitpos, 1, rreversep);
4982 : 11065 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4983 : 11065 : 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 : 748256 : if (lunsignedp)
4996 : : {
4997 : 747137 : 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 : 748256 : if (nbitpos < 0)
5016 : : return 0;
5017 : :
5018 : : /* Single-bit compares should always be against zero. */
5019 : 748256 : 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 : 748256 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5029 : 748256 : nbitsize, nbitpos, 1, lreversep);
5030 : :
5031 : 748256 : rhs = const_binop (BIT_AND_EXPR,
5032 : : const_binop (LSHIFT_EXPR,
5033 : : fold_convert_loc (loc, unsigned_type, rhs),
5034 : 748256 : size_int (lbitpos)),
5035 : : mask);
5036 : :
5037 : 748256 : lhs = build2_loc (loc, code, compare_type,
5038 : : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5039 : 748256 : 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 : 65533492 : simple_operand_p (const_tree exp)
5081 : : {
5082 : : /* Strip any conversions that don't change the machine mode. */
5083 : 65533492 : STRIP_NOPS (exp);
5084 : :
5085 : 65533492 : return (CONSTANT_CLASS_P (exp)
5086 : 45211450 : || TREE_CODE (exp) == SSA_NAME
5087 : 80245593 : || (DECL_P (exp)
5088 : 4602101 : && ! TREE_ADDRESSABLE (exp)
5089 : 4515430 : && ! TREE_THIS_VOLATILE (exp)
5090 : 4515430 : && ! 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 : 4513978 : && ! TREE_PUBLIC (exp)
5095 : 4493342 : && ! DECL_EXTERNAL (exp)
5096 : : /* DECL_VALUE_EXPR will expand to something non-simple. */
5097 : 4493342 : && ! ((VAR_P (exp)
5098 : : || TREE_CODE (exp) == PARM_DECL
5099 : : || TREE_CODE (exp) == RESULT_DECL)
5100 : 4493342 : && 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 : 4492739 : && (! 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 : 4492739 : && (! 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 : 5961000 : simple_condition_p (tree exp)
5116 : : {
5117 : 6032224 : enum tree_code code;
5118 : :
5119 : 6032224 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5120 : 4319164 : return false;
5121 : :
5122 : 1722407 : while (CONVERT_EXPR_P (exp))
5123 : 9347 : exp = TREE_OPERAND (exp, 0);
5124 : :
5125 : 1713060 : code = TREE_CODE (exp);
5126 : :
5127 : 1713060 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5128 : 1311243 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5129 : 1311243 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5130 : :
5131 : 401817 : if (code == TRUTH_NOT_EXPR)
5132 : 71224 : return simple_condition_p (TREE_OPERAND (exp, 0));
5133 : :
5134 : 330593 : 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 : 23134120 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5175 : : tree arg1, int upper1_p)
5176 : : {
5177 : 23134120 : tree tem;
5178 : 23134120 : int result;
5179 : 23134120 : 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 : 23134120 : if (arg0 != 0 && arg1 != 0)
5187 : : {
5188 : 11807341 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5189 : : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5190 : 11807341 : STRIP_NOPS (tem);
5191 : 11807341 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5192 : : }
5193 : :
5194 : 11326779 : 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 : 11318278 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5204 : 11318278 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5205 : 11318278 : switch (code)
5206 : : {
5207 : 5299633 : case EQ_EXPR:
5208 : 5299633 : result = sgn0 == sgn1;
5209 : 5299633 : break;
5210 : 0 : case NE_EXPR:
5211 : 0 : result = sgn0 != sgn1;
5212 : 0 : break;
5213 : 402946 : case LT_EXPR:
5214 : 402946 : result = sgn0 < sgn1;
5215 : 402946 : break;
5216 : 2614613 : case LE_EXPR:
5217 : 2614613 : result = sgn0 <= sgn1;
5218 : 2614613 : break;
5219 : 3001086 : case GT_EXPR:
5220 : 3001086 : result = sgn0 > sgn1;
5221 : 3001086 : break;
5222 : 0 : case GE_EXPR:
5223 : 0 : result = sgn0 >= sgn1;
5224 : 0 : break;
5225 : 0 : default:
5226 : 0 : gcc_unreachable ();
5227 : : }
5228 : :
5229 : 11318278 : 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 : 59623058 : 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 : 59623058 : tree arg0_type = TREE_TYPE (arg0);
5242 : 59623058 : tree n_low, n_high, low = *p_low, high = *p_high;
5243 : 59623058 : int in_p = *p_in_p, n_in_p;
5244 : :
5245 : 59623058 : switch (code)
5246 : : {
5247 : 1687581 : case TRUTH_NOT_EXPR:
5248 : : /* We can only do something if the range is testing for zero. */
5249 : 1687581 : if (low == NULL_TREE || high == NULL_TREE
5250 : 1687581 : || ! integer_zerop (low) || ! integer_zerop (high))
5251 : 0 : return NULL_TREE;
5252 : 1687581 : *p_in_p = ! in_p;
5253 : 1687581 : return arg0;
5254 : :
5255 : 47131536 : case EQ_EXPR: case NE_EXPR:
5256 : 47131536 : 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 : 47131536 : if (low == NULL_TREE || high == NULL_TREE
5263 : 47131536 : || ! integer_zerop (low) || ! integer_zerop (high)
5264 : 94262985 : || TREE_CODE (arg1) != INTEGER_CST)
5265 : 17128532 : return NULL_TREE;
5266 : :
5267 : 30003004 : switch (code)
5268 : : {
5269 : : case NE_EXPR: /* - [c, c] */
5270 : : low = high = arg1;
5271 : : break;
5272 : 8493011 : case EQ_EXPR: /* + [c, c] */
5273 : 8493011 : in_p = ! in_p, low = high = arg1;
5274 : 8493011 : break;
5275 : 2302424 : case GT_EXPR: /* - [-, c] */
5276 : 2302424 : low = 0, high = arg1;
5277 : 2302424 : break;
5278 : 941810 : case GE_EXPR: /* + [c, -] */
5279 : 941810 : in_p = ! in_p, low = arg1, high = 0;
5280 : 941810 : break;
5281 : 5482390 : case LT_EXPR: /* - [c, -] */
5282 : 5482390 : low = arg1, high = 0;
5283 : 5482390 : break;
5284 : 4564884 : case LE_EXPR: /* + [-, c] */
5285 : 4564884 : in_p = ! in_p, low = 0, high = arg1;
5286 : 4564884 : 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 : 30003004 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5297 : : {
5298 : 2058469 : 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 : 2058460 : 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 : 2058460 : if (high == 0 && low && ! integer_zerop (low))
5310 : : {
5311 : 945707 : in_p = ! in_p;
5312 : 945707 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5313 : 945707 : build_int_cst (TREE_TYPE (low), 1), 0);
5314 : 945707 : low = build_int_cst (arg0_type, 0);
5315 : : }
5316 : : }
5317 : :
5318 : 30002995 : *p_low = low;
5319 : 30002995 : *p_high = high;
5320 : 30002995 : *p_in_p = in_p;
5321 : 30002995 : return arg0;
5322 : :
5323 : 240 : 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 : 240 : if (!TYPE_UNSIGNED (arg0_type)
5327 : 240 : && !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 : 240 : n_low = range_binop (MINUS_EXPR, exp_type,
5337 : : build_int_cst (exp_type, 0),
5338 : : 0, high, 1);
5339 : 240 : n_high = range_binop (MINUS_EXPR, exp_type,
5340 : : build_int_cst (exp_type, 0),
5341 : : 0, low, 0);
5342 : 240 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5343 : : return NULL_TREE;
5344 : 228 : 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 : 826108 : case PLUS_EXPR:
5352 : 826108 : case MINUS_EXPR:
5353 : 826108 : 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 : 687225 : if (!TYPE_UNSIGNED (arg0_type)
5359 : 687225 : && !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 : 926735 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5367 : : arg0_type, low, 0, arg1, 0);
5368 : 464543 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5369 : : arg0_type, high, 1, arg1, 0);
5370 : 461541 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5371 : 926072 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5372 : : return NULL_TREE;
5373 : :
5374 : 464531 : if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5375 : 20984 : *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 : 464759 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5381 : : {
5382 : 161224 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5383 : 161224 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5384 : 161224 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5385 : 161224 : 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 : 161224 : if (tree_int_cst_equal (n_low, low)
5392 : 161224 : && tree_int_cst_equal (n_high, high))
5393 : : low = high = 0;
5394 : : else
5395 : 161224 : in_p = ! in_p;
5396 : : }
5397 : : else
5398 : 303535 : low = n_low, high = n_high;
5399 : :
5400 : 464759 : *p_low = low;
5401 : 464759 : *p_high = high;
5402 : 464759 : *p_in_p = in_p;
5403 : 464759 : return arg0;
5404 : :
5405 : 2509228 : CASE_CONVERT:
5406 : 2509228 : case NON_LVALUE_EXPR:
5407 : 2509228 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5408 : : return NULL_TREE;
5409 : :
5410 : 1088816 : if (! INTEGRAL_TYPE_P (arg0_type)
5411 : 1054635 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5412 : 940380 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5413 : : return NULL_TREE;
5414 : :
5415 : 917931 : n_low = low, n_high = high;
5416 : :
5417 : 917931 : if (n_low != 0)
5418 : 811183 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5419 : :
5420 : 917931 : if (n_high != 0)
5421 : 833062 : 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 : 917931 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5433 : : {
5434 : 216447 : tree high_positive;
5435 : 216447 : tree equiv_type;
5436 : : /* For fixed-point modes, we need to pass the saturating flag
5437 : : as the 2nd parameter. */
5438 : 216447 : 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 : 216447 : else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5443 : : equiv_type = arg0_type;
5444 : : else
5445 : 216439 : equiv_type
5446 : 216439 : = 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 : 216447 : high_positive
5452 : 216447 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5453 : 0 : : TYPE_MAX_VALUE (arg0_type);
5454 : :
5455 : 216447 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5456 : 195674 : 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 : 216447 : if (low != 0)
5465 : : {
5466 : 114980 : 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 : 114980 : 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 : 101467 : 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 : 101467 : 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 : 701484 : else if (TYPE_UNSIGNED (exp_type)
5499 : 683325 : && !TYPE_UNSIGNED (arg0_type)
5500 : 394447 : && low
5501 : 1095931 : && !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 : 917931 : *p_low = n_low;
5515 : 917931 : *p_high = n_high;
5516 : 917931 : *p_in_p = in_p;
5517 : 917931 : 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 : 49623982 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5535 : : bool *strict_overflow_p)
5536 : : {
5537 : 49623982 : enum tree_code code;
5538 : 49623982 : tree arg0, arg1 = NULL_TREE;
5539 : 49623982 : tree exp_type, nexp;
5540 : 49623982 : int in_p;
5541 : 49623982 : tree low, high;
5542 : 49623982 : 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 : 49623982 : in_p = 0;
5551 : 49623982 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5552 : :
5553 : 79753127 : while (1)
5554 : : {
5555 : 79753127 : code = TREE_CODE (exp);
5556 : 79753127 : exp_type = TREE_TYPE (exp);
5557 : 79753127 : arg0 = NULL_TREE;
5558 : :
5559 : 79753127 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5560 : : {
5561 : 54955839 : if (TREE_OPERAND_LENGTH (exp) > 0)
5562 : 54955839 : arg0 = TREE_OPERAND (exp, 0);
5563 : 54955839 : if (TREE_CODE_CLASS (code) == tcc_binary
5564 : 51514009 : || TREE_CODE_CLASS (code) == tcc_comparison
5565 : 63300781 : || (TREE_CODE_CLASS (code) == tcc_expression
5566 : 2722732 : && TREE_OPERAND_LENGTH (exp) > 1))
5567 : 47627454 : arg1 = TREE_OPERAND (exp, 1);
5568 : : }
5569 : 54955839 : if (arg0 == NULL_TREE)
5570 : : break;
5571 : :
5572 : 54955825 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5573 : : &high, &in_p, strict_overflow_p);
5574 : 54955825 : 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 : 49623982 : if (TREE_CODE (exp) == INTEGER_CST)
5581 : : {
5582 : 34576 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5583 : : exp, 0, low, 0))
5584 : 34576 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5585 : : exp, 1, high, 1)));
5586 : 34576 : low = high = 0;
5587 : 34576 : exp = 0;
5588 : : }
5589 : :
5590 : 49623982 : *pin_p = in_p, *plow = low, *phigh = high;
5591 : 49623982 : 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 : 561410 : 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 : 561410 : if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5637 : 64638 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5638 : :
5639 : 561410 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5640 : : {
5641 : 412362 : 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 : 412362 : utype = unsigned_type_for (etype);
5646 : 412362 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5647 : 412362 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5648 : 412362 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5649 : 412362 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5650 : :
5651 : 412362 : 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 : 149048 : 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 : 1479287 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5673 : : tree low, tree high)
5674 : : {
5675 : 2567888 : 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 : 2567888 : if (targetm.have_canonicalize_funcptr_for_compare ()
5680 : 0 : && POINTER_TYPE_P (etype)
5681 : 2567888 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5682 : : return NULL_TREE;
5683 : :
5684 : 2567888 : if (! in_p)
5685 : : {
5686 : 355098 : value = build_range_check (loc, type, exp, 1, low, high);
5687 : 355098 : if (value != 0)
5688 : 355098 : return invert_truthvalue_loc (loc, value);
5689 : :
5690 : : return 0;
5691 : : }
5692 : :
5693 : 2212790 : if (low == 0 && high == 0)
5694 : 141636 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5695 : :
5696 : 2071154 : if (low == 0)
5697 : 714315 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5698 : 714315 : fold_convert_loc (loc, etype, high));
5699 : :
5700 : 1356839 : if (high == 0)
5701 : 76333 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5702 : 76333 : fold_convert_loc (loc, etype, low));
5703 : :
5704 : 1280506 : if (operand_equal_p (low, high, 0))
5705 : 191709 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5706 : 191709 : fold_convert_loc (loc, etype, low));
5707 : :
5708 : 1088797 : if (TREE_CODE (exp) == BIT_AND_EXPR
5709 : 1088797 : && 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 : 1088711 : if (integer_zerop (low))
5716 : : {
5717 : 619347 : if (! TYPE_UNSIGNED (etype))
5718 : : {
5719 : 129914 : etype = unsigned_type_for (etype);
5720 : 129914 : high = fold_convert_loc (loc, etype, high);
5721 : 129914 : exp = fold_convert_loc (loc, etype, exp);
5722 : : }
5723 : 619347 : return build_range_check (loc, type, exp, 1, 0, high);
5724 : : }
5725 : :
5726 : : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5727 : 469364 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5728 : : {
5729 : 107565 : int prec = TYPE_PRECISION (etype);
5730 : :
5731 : 107565 : 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 : 469254 : etype = range_check_type (etype);
5751 : 469254 : if (etype == NULL_TREE)
5752 : : return NULL_TREE;
5753 : :
5754 : 469254 : high = fold_convert_loc (loc, etype, high);
5755 : 469254 : low = fold_convert_loc (loc, etype, low);
5756 : 469254 : exp = fold_convert_loc (loc, etype, exp);
5757 : :
5758 : 469254 : value = const_binop (MINUS_EXPR, high, low);
5759 : :
5760 : 469254 : if (value != 0 && !TREE_OVERFLOW (value))
5761 : 469254 : 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 : 164850 : range_predecessor (tree val)
5772 : : {
5773 : 164850 : tree type = TREE_TYPE (val);
5774 : :
5775 : 164850 : if (INTEGRAL_TYPE_P (type)
5776 : 164850 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5777 : : return 0;
5778 : : else
5779 : 164850 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5780 : 164850 : 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 : 1613243 : range_successor (tree val)
5787 : : {
5788 : 1613243 : tree type = TREE_TYPE (val);
5789 : :
5790 : 1613243 : if (INTEGRAL_TYPE_P (type)
5791 : 1613243 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5792 : : return 0;
5793 : : else
5794 : 1613234 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5795 : 1613234 : 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 : 3606509 : 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 : 3606509 : bool no_overlap;
5806 : 3606509 : int subset;
5807 : 3606509 : int temp;
5808 : 3606509 : tree tem;
5809 : 3606509 : int in_p;
5810 : 3606509 : tree low, high;
5811 : 3606509 : int lowequal = ((low0 == 0 && low1 == 0)
5812 : 3606509 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5813 : 3606509 : low0, 0, low1, 0)));
5814 : 3606509 : int highequal = ((high0 == 0 && high1 == 0)
5815 : 3606509 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5816 : 3606509 : 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 : 3606509 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5821 : : low0, 0, low1, 0))
5822 : 3606509 : || (lowequal
5823 : 573631 : && 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 : 3606509 : if (low1
5834 : 3606509 : && high1
5835 : 896150 : && TREE_CODE (low1) == INTEGER_CST
5836 : 896150 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5837 : 147680 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5838 : 205788 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5839 : : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5840 : 4457873 : && operand_equal_p (low1, high1, 0))
5841 : : {
5842 : 493084 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5843 : 493084 : && 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 : 397334 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5849 : 397334 : && 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 : 3445616 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5858 : : high0, 1, low1, 0));
5859 : 3445616 : 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 : 3445616 : 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 : 1563858 : if (no_overlap)
5870 : : in_p = 0, low = high = 0;
5871 : 1561585 : else if (subset)
5872 : : in_p = 1, low = low1, high = high1;
5873 : : else
5874 : 1418291 : in_p = 1, low = low1, high = high0;
5875 : : }
5876 : :
5877 : 1881758 : 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 : 313408 : if (no_overlap)
5888 : : in_p = 1, low = low0, high = high0;
5889 : 307817 : else if (lowequal && highequal)
5890 : : in_p = 0, low = high = 0;
5891 : 306791 : else if (subset && lowequal)
5892 : : {
5893 : 224959 : low = range_successor (high1);
5894 : 224959 : high = high0;
5895 : 224959 : in_p = 1;
5896 : 224959 : 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 : 81832 : else if (! subset || highequal)
5904 : : {
5905 : 56155 : low = low0;
5906 : 56155 : high = range_predecessor (low1);
5907 : 56155 : in_p = 1;
5908 : 56155 : 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 : 1568350 : 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 : 1222567 : if (no_overlap)
5925 : : in_p = 1, low = low1, high = high1;
5926 : 1214365 : else if (subset || highequal)
5927 : : in_p = 0, low = high = 0;
5928 : : else
5929 : : {
5930 : 1091289 : low = range_successor (high0);
5931 : 1091289 : high = high1;
5932 : 1091289 : in_p = 1;
5933 : 1091289 : 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 : 345783 : if (no_overlap)
5950 : : {
5951 : 231311 : 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 : 176326 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5959 : 175053 : switch (TREE_CODE (TREE_TYPE (low0)))
5960 : : {
5961 : 66524 : case ENUMERAL_TYPE:
5962 : 66524 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5963 : : GET_MODE_BITSIZE
5964 : 133048 : (TYPE_MODE (TREE_TYPE (low0)))))
5965 : : break;
5966 : : /* FALLTHROUGH */
5967 : 174860 : case INTEGER_TYPE:
5968 : 174860 : if (tree_int_cst_equal (low0,
5969 : 174860 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5970 : 6915 : 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 : 176326 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5983 : 176120 : switch (TREE_CODE (TREE_TYPE (high1)))
5984 : : {
5985 : 66532 : case ENUMERAL_TYPE:
5986 : 66532 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5987 : : GET_MODE_BITSIZE
5988 : 133064 : (TYPE_MODE (TREE_TYPE (high1)))))
5989 : : break;
5990 : : /* FALLTHROUGH */
5991 : 175927 : case INTEGER_TYPE:
5992 : 175927 : if (tree_int_cst_equal (high1,
5993 : 175927 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5994 : 12744 : 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 : 176326 : 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 : 114472 : else if (subset)
6026 : : in_p = 0, low = low0, high = high0;
6027 : : else
6028 : 15563 : in_p = 0, low = low0, high = high1;
6029 : : }
6030 : :
6031 : 3244145 : *pin_p = in_p, *plow = low, *phigh = high;
6032 : 3244145 : 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 : 452325 : 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 : 452325 : tree arg1_type = TREE_TYPE (arg1);
6051 : 452325 : tree tem;
6052 : :
6053 : 452325 : STRIP_NOPS (arg1);
6054 : 452325 : 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 : 452325 : if (!HONOR_SIGNED_ZEROS (type)
6076 : 904660 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6077 : 452325 : ? real_zerop (arg01)
6078 : 451265 : : integer_zerop (arg01))
6079 : 1197586 : && ((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 : 291670 : || (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 : 451075 : if (!HONOR_SIGNED_ZEROS (type)
6145 : 451075 : && integer_zerop (arg01) && integer_zerop (arg2))
6146 : : {
6147 : 243970 : if (comp_code == NE_EXPR)
6148 : 145 : return fold_convert_loc (loc, type, arg1);
6149 : 243825 : 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 : 450930 : if (!HONOR_SIGNED_ZEROS (type)
6180 : 450930 : && 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 : 703250 : && (in_gimple_form
6184 : 16234 : || VECTOR_TYPE_P (type)
6185 : 16172 : || (! lang_GNU_CXX ()
6186 : 13576 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6187 : 2596 : || ! maybe_lvalue_p (arg1)
6188 : 2575 : || ! maybe_lvalue_p (arg2)))
6189 : : {
6190 : 250669 : tree comp_op0 = arg00;
6191 : 250669 : tree comp_op1 = arg01;
6192 : 250669 : tree comp_type = TREE_TYPE (comp_op0);
6193 : :
6194 : 250669 : 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 : 244809 : case GE_EXPR:
6220 : 244809 : case GT_EXPR:
6221 : 244809 : case UNGE_EXPR:
6222 : 244809 : case UNGT_EXPR:
6223 : 244809 : if (!HONOR_NANS (arg1))
6224 : : {
6225 : 244807 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6226 : 244807 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6227 : 244807 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6228 : 244807 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6229 : 3444 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6230 : : comp_op1, comp_op0);
6231 : 244807 : 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 : 24811485 : fold_range_test (location_t loc, enum tree_code code, tree type,
6264 : : tree op0, tree op1)
6265 : : {
6266 : 24811485 : int or_op = (code == TRUTH_ORIF_EXPR
6267 : 24811485 : || code == TRUTH_OR_EXPR);
6268 : 24811485 : int in0_p, in1_p, in_p;
6269 : 24811485 : tree low0, low1, low, high0, high1, high;
6270 : 24811485 : bool strict_overflow_p = false;
6271 : 24811485 : tree tem, lhs, rhs;
6272 : 24811485 : const char * const warnmsg = G_("assuming signed overflow does not occur "
6273 : : "when simplifying range test");
6274 : :
6275 : 24811485 : if (!INTEGRAL_TYPE_P (type))
6276 : : return 0;
6277 : :
6278 : 24811485 : 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 : 24811485 : if (!lhs
6283 : 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6284 : 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6285 : : return op0;
6286 : 24811483 : 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 : 24811483 : if (or_op)
6291 : 12047843 : 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 : 24776911 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6298 : 1126078 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6299 : : in1_p, low1, high1)
6300 : 25770699 : && (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 : 959216 : if (strict_overflow_p)
6306 : 259 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6307 : 959216 : 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 : 23852267 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6314 : 23852267 : if (param_logical_op_non_short_circuit != -1)
6315 : 8377 : logical_op_non_short_circuit
6316 : 8377 : = param_logical_op_non_short_circuit;
6317 : 23852267 : if (logical_op_non_short_circuit
6318 : 23848058 : && !sanitize_coverage_p ()
6319 : 23848055 : && lhs != 0 && rhs != 0
6320 : 23847876 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6321 : 28514993 : && 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 : 134739 : if (simple_operand_p (lhs))
6327 : 127571 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6328 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6329 : 64307 : type, op0, op1);
6330 : :
6331 : 70432 : else if (!lang_hooks.decls.global_bindings_p ()
6332 : 70432 : && !CONTAINS_PLACEHOLDER_P (lhs))
6333 : : {
6334 : 69779 : tree common = save_expr (lhs);
6335 : :
6336 : 114337 : if ((lhs = build_range_check (loc, type, common,
6337 : 44558 : or_op ? ! in0_p : in0_p,
6338 : : low0, high0)) != 0
6339 : 114337 : && (rhs = build_range_check (loc, type, common,
6340 : 44558 : or_op ? ! in1_p : in1_p,
6341 : : low1, high1)) != 0)
6342 : : {
6343 : 69779 : if (strict_overflow_p)
6344 : 0 : fold_overflow_warning (warnmsg,
6345 : : WARN_STRICT_OVERFLOW_COMPARISON);
6346 : 114337 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6347 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6348 : 69779 : 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 : 141961 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6374 : : bool rhs_only)
6375 : : {
6376 : 141961 : enum tree_code code = TREE_CODE (cmpop);
6377 : 141961 : enum tree_code truthop_code = TREE_CODE (op);
6378 : 141961 : tree lhs = TREE_OPERAND (op, 0);
6379 : 141961 : tree rhs = TREE_OPERAND (op, 1);
6380 : 141961 : tree orig_lhs = lhs, orig_rhs = rhs;
6381 : 141961 : enum tree_code rhs_code = TREE_CODE (rhs);
6382 : 141961 : enum tree_code lhs_code = TREE_CODE (lhs);
6383 : 141961 : enum tree_code inv_code;
6384 : :
6385 : 141961 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6386 : : return NULL_TREE;
6387 : :
6388 : 93819 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6389 : : return NULL_TREE;
6390 : :
6391 : 54866 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6392 : :
6393 : 54866 : 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 : 54866 : 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 : 54866 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6413 : 54866 : if (inv_code == rhs_code
6414 : 701 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6415 : 54902 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6416 : : return lhs;
6417 : 54853 : if (!rhs_only && inv_code == lhs_code
6418 : 630 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6419 : 54945 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6420 : : return rhs;
6421 : 54762 : 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 : 24446632 : 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 : 24446632 : enum tree_code lcode, rcode;
6458 : 24446632 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6459 : 24446632 : 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 : 24446632 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6466 : : return 0;
6467 : :
6468 : 22121132 : lcode = TREE_CODE (lhs);
6469 : 22121132 : rcode = TREE_CODE (rhs);
6470 : :
6471 : 22121132 : 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 : 22121132 : 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 : 22121132 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6486 : 19761520 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6487 : : return 0;
6488 : :
6489 : 18563698 : ll_arg = TREE_OPERAND (lhs, 0);
6490 : 18563698 : lr_arg = TREE_OPERAND (lhs, 1);
6491 : 18563698 : rl_arg = TREE_OPERAND (rhs, 0);
6492 : 18563698 : rr_arg = TREE_OPERAND (rhs, 1);
6493 : :
6494 : : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6495 : 18563698 : if (simple_operand_p (ll_arg)
6496 : 18563698 : && simple_operand_p (lr_arg))
6497 : : {
6498 : 15176069 : if (operand_equal_p (ll_arg, rl_arg, 0)
6499 : 15176069 : && operand_equal_p (lr_arg, rr_arg, 0))
6500 : : {
6501 : 20030 : result = combine_comparisons (loc, code, lcode, rcode,
6502 : : truth_type, ll_arg, lr_arg);
6503 : 20030 : if (result)
6504 : : return result;
6505 : : }
6506 : 15156039 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6507 : 15156039 : && 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 : 8950635 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6518 : 18543967 : ? 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 : 18543967 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6526 : : false) >= 2
6527 : 18543858 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6528 : 17524411 : && simple_operand_p (rl_arg)
6529 : 28675497 : && simple_operand_p (rr_arg))
6530 : : {
6531 : : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6532 : 11231078 : if (code == TRUTH_OR_EXPR
6533 : 1523412 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6534 : 607580 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6535 : 23582 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6536 : 11250424 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6537 : 38076 : return build2_loc (loc, NE_EXPR, truth_type,
6538 : 19038 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6539 : : ll_arg, rl_arg),
6540 : 19038 : build_int_cst (TREE_TYPE (ll_arg), 0));
6541 : :
6542 : : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6543 : 11212040 : if (code == TRUTH_AND_EXPR
6544 : 1678859 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6545 : 763609 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6546 : 5847 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6547 : 11213649 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6548 : 2752 : return build2_loc (loc, EQ_EXPR, truth_type,
6549 : 1376 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6550 : : ll_arg, rl_arg),
6551 : 1376 : 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 : 93274569 : 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 : 93274569 : static int depth;
6585 : 93274569 : tree ret;
6586 : :
6587 : 93274569 : if (depth > 3)
6588 : : return NULL;
6589 : :
6590 : 90012001 : depth++;
6591 : 90012001 : ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6592 : 90012001 : depth--;
6593 : :
6594 : 90012001 : return ret;
6595 : : }
6596 : :
6597 : : static tree
6598 : 90012001 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6599 : : bool *strict_overflow_p)
6600 : : {
6601 : 90012001 : tree type = TREE_TYPE (t);
6602 : 90012001 : enum tree_code tcode = TREE_CODE (t);
6603 : 90012001 : tree ctype = type;
6604 : 90012001 : if (wide_type)
6605 : : {
6606 : 30149745 : if (TREE_CODE (type) == BITINT_TYPE
6607 : 30149632 : || TREE_CODE (wide_type) == BITINT_TYPE)
6608 : : {
6609 : 113 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6610 : 8547845 : ctype = wide_type;
6611 : : }
6612 : 30149632 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6613 : 60299264 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6614 : 8547845 : ctype = wide_type;
6615 : : }
6616 : 90012001 : tree t1, t2;
6617 : 90012001 : bool same_p = tcode == code;
6618 : 90012001 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6619 : 90012001 : bool sub_strict_overflow_p;
6620 : :
6621 : : /* Don't deal with constants of zero here; they confuse the code below. */
6622 : 90012001 : if (integer_zerop (c))
6623 : : return NULL_TREE;
6624 : :
6625 : 89991060 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6626 : 36018293 : op0 = TREE_OPERAND (t, 0);
6627 : :
6628 : 89991060 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6629 : 10874184 : 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 : 89991060 : switch (tcode)
6634 : : {
6635 : 4107390 : 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 : 4107390 : if (code == MULT_EXPR
6639 : 5291473 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6640 : 1184083 : TYPE_SIGN (type)))
6641 : : {
6642 : 3308771 : 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 : 3308771 : if (TREE_OVERFLOW (tem))
6647 : : return NULL_TREE;
6648 : : return tem;
6649 : : }
6650 : : break;
6651 : :
6652 : 35388201 : CASE_CONVERT: case NON_LVALUE_EXPR:
6653 : 35388201 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6654 : : break;
6655 : : /* If op0 is an expression ... */
6656 : 34206631 : if ((COMPARISON_CLASS_P (op0)
6657 : : || UNARY_CLASS_P (op0)
6658 : 34206631 : || BINARY_CLASS_P (op0)
6659 : 31300141 : || VL_EXP_CLASS_P (op0)
6660 : 31270121 : || 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 : 34323930 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6664 : 1081746 : && (TYPE_PRECISION (ctype)
6665 : 1081746 : > 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 : 2470158 : || (TYPE_PRECISION (type)
6669 : 2470158 : < TYPE_PRECISION (TREE_TYPE (op0)))
6670 : : /* ... or signedness changes for division or modulus,
6671 : : then we cannot pass through this conversion. */
6672 : 2436594 : || (code != MULT_EXPR
6673 : 109741 : && (TYPE_UNSIGNED (ctype)
6674 : 109741 : != 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 : 2343940 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6679 : 1841599 : && !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 : 31732456 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6686 : 31732456 : && TREE_CODE (t2) == INTEGER_CST
6687 : 31732456 : && !TREE_OVERFLOW (t2)
6688 : 64282301 : && (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 : 283 : 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 : 283 : 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 : 283 : if (tree_int_cst_sgn (c) == -1)
6710 : : break;
6711 : : /* FALLTHROUGH */
6712 : 51176 : 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 : 51176 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6717 : : break;
6718 : 49361 : 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 : 1258 : 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 : 1258 : if (TREE_CODE (op1) == INTEGER_CST
6774 : 1242 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6775 : : /* const_binop may not detect overflow correctly,
6776 : : so check for it explicitly here. */
6777 : 1125 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6778 : 1267 : wi::to_wide (op1))
6779 : 1116 : && (t1 = fold_convert (ctype,
6780 : : const_binop (LSHIFT_EXPR, size_one_node,
6781 : : op1))) != 0
6782 : 2374 : && !TREE_OVERFLOW (t1))
6783 : 2030 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6784 : : ? MULT_EXPR : FLOOR_DIV_EXPR,
6785 : : ctype,
6786 : : fold_convert (ctype, op0),
6787 : : t1),
6788 : 1116 : c, code, wide_type, strict_overflow_p);
6789 : : break;
6790 : :
6791 : 7631715 : 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 : 7631715 : sub_strict_overflow_p = false;
6797 : 7631715 : t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6798 : 7631715 : t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6799 : 800758 : if (t1 != 0 && t2 != 0
6800 : 276600 : && TYPE_OVERFLOW_WRAPS (ctype)
6801 : 7899454 : && (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 : 267739 : if (sub_strict_overflow_p)
6808 : 0 : *strict_overflow_p = true;
6809 : 267739 : 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 : 7363976 : if (tcode == MINUS_EXPR)
6816 : : {
6817 : 1940637 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6818 : : /* If OP1 was not easily negatable, the constant may be OP0. */
6819 : 1940637 : if (TREE_CODE (op0) == INTEGER_CST)
6820 : : {
6821 : 354789 : std::swap (op0, op1);
6822 : 354789 : std::swap (t1, t2);
6823 : : }
6824 : : }
6825 : :
6826 : 7363976 : 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 : 3426199 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6833 : : {
6834 : 170563 : if (code == CEIL_DIV_EXPR)
6835 : : code = FLOOR_DIV_EXPR;
6836 : 170561 : else if (code == FLOOR_DIV_EXPR)
6837 : : code = CEIL_DIV_EXPR;
6838 : 170296 : else if (code != MULT_EXPR
6839 : 170296 : && 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 : 3421002 : if (code == MULT_EXPR
6846 : 4600476 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6847 : 1179474 : TYPE_SIGN (type)))
6848 : : {
6849 : 2622893 : op1 = const_binop (code, fold_convert (ctype, op1),
6850 : : fold_convert (ctype, c));
6851 : : /* We allow the constant to overflow with wrapping semantics. */
6852 : 2622893 : if (op1 == 0
6853 : 2622893 : || (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 : 2619520 : 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 : 2619520 : if (code == MULT_EXPR
6872 : 2238742 : && TYPE_OVERFLOW_WRAPS (ctype)
6873 : 4532016 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6874 : 465579 : 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 : 2161076 : 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 : 2161076 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6886 : 2161076 : || 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 : 2171640 : && 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 : 2352257 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6900 : 2352257 : 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 : 2352257 : if (same_p
6905 : 2027114 : && TYPE_OVERFLOW_WRAPS (ctype)
6906 : 4205035 : && (t1 = extract_muldiv (op0, c, code, wide_type,
6907 : : strict_overflow_p)) != 0)
6908 : 65647 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6909 : : fold_convert (ctype, op1));
6910 : 2286610 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6911 : 1959054 : && TYPE_OVERFLOW_WRAPS (ctype)
6912 : 4071376 : && (t1 = extract_muldiv (op1, c, code, wide_type,
6913 : : strict_overflow_p)) != 0)
6914 : 931724 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6915 : : fold_convert (ctype, t1));
6916 : 1354886 : 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 : 517743 : if (tcode == code)
6922 : : {
6923 : 193103 : bool overflow_p = false;
6924 : 193103 : wi::overflow_type overflow_mul;
6925 : 193103 : signop sign = TYPE_SIGN (ctype);
6926 : 193103 : unsigned prec = TYPE_PRECISION (ctype);
6927 : 386206 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6928 : 193103 : wi::to_wide (c, prec),
6929 : 193103 : sign, &overflow_mul);
6930 : 193103 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6931 : 193103 : if (overflow_mul
6932 : 1291 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6933 : : overflow_p = true;
6934 : 193036 : if (!overflow_p)
6935 : 193036 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6936 : : wide_int_to_tree (ctype, mul));
6937 : 193103 : }
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 : 324707 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6947 : 29270 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6948 : 353934 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6949 : 29187 : || (tcode == MULT_EXPR
6950 : 29187 : && 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 : 85765690 : constant_boolean_node (bool value, tree type)
6988 : : {
6989 : 85765690 : if (type == integer_type_node)
6990 : 20310479 : return value ? integer_one_node : integer_zero_node;
6991 : 65455211 : else if (type == boolean_type_node)
6992 : 62052383 : return value ? boolean_true_node : boolean_false_node;
6993 : 3402828 : else if (VECTOR_TYPE_P (type))
6994 : 847 : return build_vector_from_val (type,
6995 : 847 : build_int_cst (TREE_TYPE (type),
6996 : 1312 : value ? -1 : 0));
6997 : : else
6998 : 3401981 : 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 : 980502 : 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 : 980502 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7018 : 980502 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7019 : 980502 : tree test, true_value, false_value;
7020 : 980502 : tree lhs = NULL_TREE;
7021 : 980502 : tree rhs = NULL_TREE;
7022 : 980502 : 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 : 1001107 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7027 : 207323 : ANY_INTEGRAL_TYPE_P (type)
7028 : 983016 : && TYPE_OVERFLOW_TRAPS (type), op1))
7029 : : return NULL_TREE;
7030 : :
7031 : 959731 : if (TREE_CODE (cond) == COND_EXPR
7032 : 373223 : || TREE_CODE (cond) == VEC_COND_EXPR)
7033 : : {
7034 : 588859 : test = TREE_OPERAND (cond, 0);
7035 : 588859 : true_value = TREE_OPERAND (cond, 1);
7036 : 588859 : 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 : 588859 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7041 : 7463 : lhs = true_value;
7042 : 588859 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7043 : 6 : rhs = false_value;
7044 : : }
7045 : 370872 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7046 : 370718 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7047 : : {
7048 : 369189 : tree testtype = TREE_TYPE (cond);
7049 : 369189 : test = cond;
7050 : 369189 : true_value = constant_boolean_node (true, testtype);
7051 : 369189 : 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 : 958048 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7058 : 2505 : 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 : 958048 : if (!TREE_CONSTANT (arg)
7064 : 958048 : && (TREE_SIDE_EFFECTS (arg)
7065 : 458106 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7066 : 453879 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7067 : : return NULL_TREE;
7068 : :
7069 : 514646 : arg = fold_convert_loc (loc, arg_type, arg);
7070 : 514646 : if (lhs == 0)
7071 : : {
7072 : 508615 : true_value = fold_convert_loc (loc, cond_type, true_value);
7073 : 508615 : if (cond_first_p)
7074 : 498266 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7075 : : else
7076 : 10349 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7077 : : }
7078 : 514646 : if (rhs == 0)
7079 : : {
7080 : 514640 : false_value = fold_convert_loc (loc, cond_type, false_value);
7081 : 514640 : if (cond_first_p)
7082 : 503730 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7083 : : else
7084 : 10910 : 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 : 514646 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7089 : : return NULL_TREE;
7090 : :
7091 : 495771 : 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 : 649437 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7111 : : const_tree zero_arg, int negate)
7112 : : {
7113 : 649437 : if (!real_zerop (zero_arg))
7114 : : return false;
7115 : :
7116 : : /* Don't allow the fold with -fsignaling-nans. */
7117 : 648758 : 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 : 645420 : if (!HONOR_SIGNED_ZEROS (type))
7122 : : return true;
7123 : :
7124 : : /* There is no case that is safe for all rounding modes. */
7125 : 625252 : 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 : 624589 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7130 : 1457 : zero_arg = uniform_vector_p (zero_arg);
7131 : 624589 : 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 : 623411 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7136 : 231 : 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 : 623411 : 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 : 1742117 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7154 : : tree *hi, bool *neg_overflow)
7155 : : {
7156 : 1742117 : tree prod, tmp, type = TREE_TYPE (c1);
7157 : 1742117 : signop sign = TYPE_SIGN (type);
7158 : 1742117 : 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 : 1742117 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7163 : 1742117 : prod = force_fit_type (type, val, -1, overflow);
7164 : 1742117 : *neg_overflow = false;
7165 : :
7166 : 1742117 : if (sign == UNSIGNED)
7167 : : {
7168 : 1713387 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7169 : 1713387 : *lo = prod;
7170 : :
7171 : : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7172 : 1713387 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7173 : 1713387 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7174 : : }
7175 : 28730 : else if (tree_int_cst_sgn (c1) >= 0)
7176 : : {
7177 : 27322 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7178 : 27322 : switch (tree_int_cst_sgn (c2))
7179 : : {
7180 : 4674 : case -1:
7181 : 4674 : *neg_overflow = true;
7182 : 4674 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7183 : 4674 : *hi = prod;
7184 : 4674 : break;
7185 : :
7186 : 14068 : case 0:
7187 : 14068 : *lo = fold_negate_const (tmp, type);
7188 : 14068 : *hi = tmp;
7189 : 14068 : break;
7190 : :
7191 : 8580 : case 1:
7192 : 8580 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7193 : 8580 : *lo = prod;
7194 : 8580 : 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 : 1742117 : if (code != EQ_EXPR && code != NE_EXPR)
7230 : : return code;
7231 : :
7232 : 16647 : if (TREE_OVERFLOW (*lo)
7233 : 16647 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7234 : 727 : *lo = NULL_TREE;
7235 : 16647 : if (TREE_OVERFLOW (*hi)
7236 : 16647 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7237 : 92 : *hi = NULL_TREE;
7238 : :
7239 : : return code;
7240 : 1742117 : }
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 : 1540586362 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7248 : : {
7249 : 1540586362 : if (CONSTANT_CLASS_P (arg1))
7250 : : return false;
7251 : 497251587 : if (CONSTANT_CLASS_P (arg0))
7252 : : return true;
7253 : :
7254 : 457517461 : STRIP_NOPS (arg0);
7255 : 457517461 : STRIP_NOPS (arg1);
7256 : :
7257 : 457517461 : if (TREE_CONSTANT (arg1))
7258 : : return false;
7259 : 444834569 : if (TREE_CONSTANT (arg0))
7260 : : return true;
7261 : :
7262 : : /* Put addresses in arg1. */
7263 : 444303429 : if (TREE_CODE (arg1) == ADDR_EXPR)
7264 : : return false;
7265 : 430525047 : 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 : 430135091 : if (TREE_CODE (arg0) == SSA_NAME
7273 : 323980582 : && TREE_CODE (arg1) == SSA_NAME
7274 : 748220402 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7275 : : return true;
7276 : :
7277 : : /* Put SSA_NAMEs last. */
7278 : 405410375 : if (TREE_CODE (arg1) == SSA_NAME)
7279 : : return false;
7280 : 97503531 : if (TREE_CODE (arg0) == SSA_NAME)
7281 : : return true;
7282 : :
7283 : : /* Put variables last. */
7284 : 91608260 : if (DECL_P (arg1))
7285 : : return false;
7286 : 49096210 : 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 : 23763617 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7299 : : {
7300 : 23763617 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7301 : :
7302 : 23763617 : if (TREE_CODE (bound) == LT_EXPR)
7303 : 4889589 : a = TREE_OPERAND (bound, 0);
7304 : 18874028 : else if (TREE_CODE (bound) == GT_EXPR)
7305 : 2639775 : a = TREE_OPERAND (bound, 1);
7306 : : else
7307 : : return NULL_TREE;
7308 : :
7309 : 7529364 : typea = TREE_TYPE (a);
7310 : 7529364 : if (!INTEGRAL_TYPE_P (typea)
7311 : 362560 : && !POINTER_TYPE_P (typea))
7312 : : return NULL_TREE;
7313 : :
7314 : 7342952 : if (TREE_CODE (ineq) == LT_EXPR)
7315 : : {
7316 : 1413728 : a1 = TREE_OPERAND (ineq, 1);
7317 : 1413728 : y = TREE_OPERAND (ineq, 0);
7318 : : }
7319 : 5929224 : else if (TREE_CODE (ineq) == GT_EXPR)
7320 : : {
7321 : 1054925 : a1 = TREE_OPERAND (ineq, 0);
7322 : 1054925 : y = TREE_OPERAND (ineq, 1);
7323 : : }
7324 : : else
7325 : : return NULL_TREE;
7326 : :
7327 : 2468653 : if (TREE_TYPE (a1) != typea)
7328 : : return NULL_TREE;
7329 : :
7330 : 1716027 : if (POINTER_TYPE_P (typea))
7331 : : {
7332 : : /* Convert the pointer types into integer before taking the difference. */
7333 : 8634 : tree ta = fold_convert_loc (loc, ssizetype, a);
7334 : 8634 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7335 : 8634 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7336 : : }
7337 : : else
7338 : 1707393 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7339 : :
7340 : 1716027 : if (!diff || !integer_onep (diff))
7341 : 1705592 : return NULL_TREE;
7342 : :
7343 : 10435 : 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 : 8591694 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7351 : : tree arg0, tree arg1)
7352 : : {
7353 : 8591694 : tree arg00, arg01, arg10, arg11;
7354 : 8591694 : 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 : 8591694 : if (TREE_CODE (arg0) == MULT_EXPR)
7363 : : {
7364 : 7593080 : arg00 = TREE_OPERAND (arg0, 0);
7365 : 7593080 : arg01 = TREE_OPERAND (arg0, 1);
7366 : : }
7367 : 998614 : else if (TREE_CODE (arg0) == INTEGER_CST)
7368 : : {
7369 : 69891 : arg00 = build_one_cst (type);
7370 : 69891 : arg01 = arg0;
7371 : : }
7372 : : else
7373 : : {
7374 : : /* We cannot generate constant 1 for fract. */
7375 : 928723 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7376 : 0 : return NULL_TREE;
7377 : 928723 : arg00 = arg0;
7378 : 928723 : arg01 = build_one_cst (type);
7379 : : }
7380 : 8591694 : if (TREE_CODE (arg1) == MULT_EXPR)
7381 : : {
7382 : 2347394 : arg10 = TREE_OPERAND (arg1, 0);
7383 : 2347394 : arg11 = TREE_OPERAND (arg1, 1);
7384 : : }
7385 : 6244300 : else if (TREE_CODE (arg1) == INTEGER_CST)
7386 : : {
7387 : 3316721 : 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 : 6399648 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7391 : 237082 : && negate_expr_p (arg1)
7392 : 3550515 : && code == PLUS_EXPR)
7393 : : {
7394 : 233794 : arg11 = negate_expr (arg1);
7395 : 233794 : code = MINUS_EXPR;
7396 : : }
7397 : : else
7398 : : arg11 = arg1;
7399 : : }
7400 : : else
7401 : : {
7402 : : /* We cannot generate constant 1 for fract. */
7403 : 2927579 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7404 : 0 : return NULL_TREE;
7405 : 2927579 : arg10 = arg1;
7406 : 2927579 : arg11 = build_one_cst (type);
7407 : : }
7408 : 8591694 : same = NULL_TREE;
7409 : :
7410 : : /* Prefer factoring a common non-constant. */
7411 : 8591694 : if (operand_equal_p (arg00, arg10, 0))
7412 : : same = arg00, alt0 = arg01, alt1 = arg11;
7413 : 8587877 : else if (operand_equal_p (arg01, arg11, 0))
7414 : : same = arg01, alt0 = arg00, alt1 = arg10;
7415 : 8491843 : else if (operand_equal_p (arg00, arg11, 0))
7416 : : same = arg00, alt0 = arg01, alt1 = arg10;
7417 : 8491767 : 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 : 8486974 : else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7424 : : {
7425 : 7138940 : HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7426 : 7138940 : HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7427 : 7138940 : HOST_WIDE_INT tmp;
7428 : 7138940 : bool swap = false;
7429 : 7138940 : tree maybe_same;
7430 : :
7431 : : /* Move min of absolute values to int11. */
7432 : 7138940 : 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 : 3632402 : maybe_same = arg11;
7441 : :
7442 : 7138940 : const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7443 : 7138940 : if (factor > 1
7444 : 9398976 : && pow2p_hwi (factor)
7445 : 2070977 : && (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 : 8423449 : && TREE_CODE (arg10) != INTEGER_CST)
7450 : : {
7451 : 1158975 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7452 : 1158975 : build_int_cst (TREE_TYPE (arg00),
7453 : 1158975 : int01 / int11));
7454 : 1158975 : alt1 = arg10;
7455 : 1158975 : same = maybe_same;
7456 : 1158975 : if (swap)
7457 : 1046958 : maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7458 : : }
7459 : : }
7460 : :
7461 : 1263695 : if (!same)
7462 : 7327999 : return NULL_TREE;
7463 : :
7464 : 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7465 : 1263695 : || TYPE_OVERFLOW_WRAPS (type)
7466 : : /* We are neither factoring zero nor minus one. */
7467 : 1389111 : || TREE_CODE (same) == INTEGER_CST)
7468 : 1252299 : 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 : 1252299 : 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 : 11396 : tree utype = unsigned_type_for (type);
7478 : 11396 : 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 : 22792 : if (TREE_CODE (tem) == INTEGER_CST
7484 : 18002 : && (wi::to_wide (tem)
7485 : 18002 : != 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 : 20056137 : native_encode_wide_int (tree type, const wide_int_ref &val,
7502 : : unsigned char *ptr, int len, int off)
7503 : : {
7504 : 20056137 : int total_bytes;
7505 : 20056137 : if (TREE_CODE (type) == BITINT_TYPE)
7506 : : {
7507 : 17217 : struct bitint_info info;
7508 : 17217 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7509 : 17217 : gcc_assert (ok);
7510 : 17217 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7511 : 17217 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7512 : : {
7513 : 17042 : 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 : 17042 : 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 : 350 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7524 : : }
7525 : : else
7526 : 40077840 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7527 : 20056137 : int byte, offset, word, words;
7528 : 20056137 : unsigned char value;
7529 : :
7530 : 20056137 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7531 : : return 0;
7532 : 20055650 : if (off == -1)
7533 : 19153981 : off = 0;
7534 : :
7535 : 20055650 : if (ptr == NULL)
7536 : : /* Dry run. */
7537 : 2792482 : return MIN (len, total_bytes - off);
7538 : :
7539 : : words = total_bytes / UNITS_PER_WORD;
7540 : :
7541 : 84152748 : for (byte = 0; byte < total_bytes; byte++)
7542 : : {
7543 : 66889580 : 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 : 66889580 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7547 : :
7548 : 66889580 : if (total_bytes > UNITS_PER_WORD)
7549 : : {
7550 : 66889580 : word = byte / UNITS_PER_WORD;
7551 : 66889580 : if (WORDS_BIG_ENDIAN)
7552 : : word = (words - 1) - word;
7553 : 66889580 : offset = word * UNITS_PER_WORD;
7554 : 66889580 : if (BYTES_BIG_ENDIAN)
7555 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7556 : : else
7557 : 66889580 : offset += byte % UNITS_PER_WORD;
7558 : : }
7559 : : else
7560 : : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7561 : 66889580 : if (offset >= off && offset - off < len)
7562 : 65602807 : ptr[offset - off] = value;
7563 : : }
7564 : 17263168 : 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 : 20056137 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7574 : : {
7575 : 20056137 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7576 : 20056137 : 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 : 561837 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7616 : : unsigned char *ptr, int len, int off)
7617 : : {
7618 : 561837 : int total_bytes = GET_MODE_SIZE (mode);
7619 : 561837 : int byte, offset, word, words, bitpos;
7620 : 561837 : 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 : 561837 : long tmp[6];
7626 : :
7627 : 561837 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7628 : : return 0;
7629 : 560010 : if (off == -1)
7630 : 441072 : off = 0;
7631 : :
7632 : 560010 : if (ptr == NULL)
7633 : : /* Dry run. */
7634 : 137647 : return MIN (len, total_bytes - off);
7635 : :
7636 : 422363 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7637 : :
7638 : 422363 : real_to_target (tmp, val, mode);
7639 : :
7640 : 3416287 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7641 : 2993924 : bitpos += BITS_PER_UNIT)
7642 : : {
7643 : 2993924 : byte = (bitpos / BITS_PER_UNIT) & 3;
7644 : 2993924 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7645 : :
7646 : 2993924 : 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 : 2993924 : offset = byte;
7660 : 2993924 : 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 : 2993924 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7669 : 2993924 : if (offset >= off
7670 : 2990608 : && offset - off < len)
7671 : 2973124 : ptr[offset - off] = value;
7672 : : }
7673 : 422363 : 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 : 15385 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7683 : : {
7684 : 15385 : int rsize, isize;
7685 : 15385 : tree part;
7686 : :
7687 : 15385 : part = TREE_REALPART (expr);
7688 : 15385 : rsize = native_encode_expr (part, ptr, len, off);
7689 : 15385 : if (off == -1 && rsize == 0)
7690 : : return 0;
7691 : 15385 : part = TREE_IMAGPART (expr);
7692 : 15385 : if (off != -1)
7693 : 30454 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7694 : 15385 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7695 : : len - rsize, off);
7696 : 15385 : if (off == -1 && isize != rsize)
7697 : : return 0;
7698 : 15385 : 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 : 887355 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7706 : : int off, unsigned HOST_WIDE_INT count)
7707 : : {
7708 : 887355 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7709 : 1774710 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7710 : 887677 : && 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 : 887111 : int offset = 0;
7747 : 887111 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7748 : 2909463 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7749 : : {
7750 : 2567956 : if (off >= size)
7751 : : {
7752 : 23959 : off -= size;
7753 : 23959 : continue;
7754 : : }
7755 : 2543997 : tree elem = VECTOR_CST_ELT (expr, i);
7756 : 2543997 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7757 : : len - offset, off);
7758 : 2543997 : if ((off == -1 && res != size) || res == 0)
7759 : : return 0;
7760 : 2543469 : offset += res;
7761 : 2543469 : if (offset >= len)
7762 : 545076 : return (off == -1 && i < count - 1) ? 0 : offset;
7763 : 1998393 : if (off != -1)
7764 : 371768 : 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 : 773599 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7776 : : {
7777 : 773599 : unsigned HOST_WIDE_INT count;
7778 : 773599 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7779 : : return 0;
7780 : 773599 : 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 : 138506 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7791 : : {
7792 : 138506 : tree type = TREE_TYPE (expr);
7793 : :
7794 : : /* Wide-char strings are encoded in target byte-order so native
7795 : : encoding them is trivial. */
7796 : 138506 : if (BITS_PER_UNIT != CHAR_BIT
7797 : 138506 : || TREE_CODE (type) != ARRAY_TYPE
7798 : 138506 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7799 : 277012 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7800 : : return 0;
7801 : :
7802 : 138506 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7803 : 138506 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7804 : : return 0;
7805 : 137648 : if (off == -1)
7806 : 55690 : off = 0;
7807 : 137648 : len = MIN (total_bytes - off, len);
7808 : 137648 : if (ptr == NULL)
7809 : : /* Dry run. */;
7810 : : else
7811 : : {
7812 : 137648 : int written = 0;
7813 : 137648 : if (off < TREE_STRING_LENGTH (expr))
7814 : : {
7815 : 137167 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7816 : 137167 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7817 : : }
7818 : 137648 : 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 : 45028 : 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 : 45028 : 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 : 84266 : if (BITS_PER_UNIT != CHAR_BIT
7841 : 42133 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7842 : : return 0;
7843 : :
7844 : 42133 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7845 : 42133 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7846 : : return 0;
7847 : 42133 : if (off == -1)
7848 : 0 : off = 0;
7849 : 42133 : len = MIN (total_bytes - off, len);
7850 : 42133 : if (ptr == NULL)
7851 : : /* Dry run. */;
7852 : : else
7853 : 42133 : 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 : 34459062 : 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 : 34459062 : if (off < -1)
7870 : : return 0;
7871 : :
7872 : 34459050 : switch (TREE_CODE (expr))
7873 : : {
7874 : 20053877 : case INTEGER_CST:
7875 : 20053877 : return native_encode_int (expr, ptr, len, off);
7876 : :
7877 : 561837 : case REAL_CST:
7878 : 561837 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7879 : 1123674 : 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 : 15385 : case COMPLEX_CST:
7885 : 15385 : return native_encode_complex (expr, ptr, len, off);
7886 : :
7887 : 773599 : case VECTOR_CST:
7888 : 773599 : return native_encode_vector (expr, ptr, len, off);
7889 : :
7890 : 138506 : case STRING_CST:
7891 : 138506 : return native_encode_string (expr, ptr, len, off);
7892 : :
7893 : 45028 : case CONSTRUCTOR:
7894 : 45028 : 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 : 12762004 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7951 : : int off, unsigned char *mask)
7952 : : {
7953 : 12762004 : int r;
7954 : :
7955 : : /* We don't support starting at negative offset and -1 is special. */
7956 : 12762004 : if (off < -1 || init == NULL_TREE)
7957 : : return 0;
7958 : :
7959 : 12762004 : gcc_assert (mask == NULL || (off == 0 && ptr));
7960 : :
7961 : 12762004 : STRIP_NOPS (init);
7962 : 12762004 : 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 : 11859830 : default:
7969 : 11859830 : r = native_encode_expr (init, ptr, len, off);
7970 : 11859830 : if (mask)
7971 : 1514 : memset (mask, 0, r);
7972 : : return r;
7973 : 902174 : case CONSTRUCTOR:
7974 : 902174 : tree type = TREE_TYPE (init);
7975 : 902174 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7976 : 902174 : if (total_bytes < 0)
7977 : : return 0;
7978 : 902174 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7979 : : return 0;
7980 : 902174 : int o = off == -1 ? 0 : off;
7981 : 902174 : if (TREE_CODE (type) == ARRAY_TYPE)
7982 : : {
7983 : 286150 : tree min_index;
7984 : 286150 : unsigned HOST_WIDE_INT cnt;
7985 : 286150 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7986 : 286150 : constructor_elt *ce;
7987 : :
7988 : 286150 : if (!TYPE_DOMAIN (type)
7989 : 286150 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7990 : : return 0;
7991 : :
7992 : 286150 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7993 : 286150 : if (fieldsize <= 0)
7994 : : return 0;
7995 : :
7996 : 286150 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7997 : 286150 : if (ptr)
7998 : 286150 : memset (ptr, '\0', MIN (total_bytes - off, len));
7999 : :
8000 : 11859139 : for (cnt = 0; ; cnt++)
8001 : : {
8002 : 12145289 : tree val = NULL_TREE, index = NULL_TREE;
8003 : 12145289 : HOST_WIDE_INT pos = curpos, count = 0;
8004 : 12145289 : bool full = false;
8005 : 12145289 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8006 : : {
8007 : 12099070 : val = ce->value;
8008 : 12099070 : index = ce->index;
8009 : : }
8010 : 46219 : else if (mask == NULL
8011 : 228 : || CONSTRUCTOR_NO_CLEARING (init)
8012 : 46447 : || curpos >= total_bytes)
8013 : : break;
8014 : : else
8015 : : pos = total_bytes;
8016 : :
8017 : 12099070 : 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 : 11404565 : else if (index)
8039 : : {
8040 : 11404565 : if (TREE_CODE (index) != INTEGER_CST)
8041 : 0 : return 0;
8042 : 11404565 : offset_int wpos
8043 : 11404565 : = wi::sext (wi::to_offset (index)
8044 : 22809130 : - wi::to_offset (min_index),
8045 : 11404565 : TYPE_PRECISION (sizetype));
8046 : 11404565 : wpos *= fieldsize;
8047 : 11404565 : if (!wi::fits_shwi_p (wpos))
8048 : : return 0;
8049 : 11404565 : pos = wpos.to_shwi ();
8050 : : }
8051 : :
8052 : 12099741 : 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 : 12099084 : curpos = pos;
8076 : 12099084 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8077 : : {
8078 : 490 : if (count)
8079 : : return 0;
8080 : 490 : if (off == -1
8081 : 490 : || (curpos >= off
8082 : 0 : && (curpos + RAW_DATA_LENGTH (val)
8083 : 0 : <= (HOST_WIDE_INT) off + len)))
8084 : : {
8085 : 490 : if (ptr)
8086 : 490 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8087 : 490 : RAW_DATA_LENGTH (val));
8088 : 490 : 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 : 490 : curpos += RAW_DATA_LENGTH (val);
8116 : 490 : val = NULL_TREE;
8117 : : }
8118 : 490 : if (val)
8119 : 12176620 : do
8120 : : {
8121 : 12176620 : if (off == -1
8122 : 798611 : || (curpos >= off
8123 : 259478 : && (curpos + fieldsize
8124 : 259478 : <= (HOST_WIDE_INT) off + len)))
8125 : : {
8126 : 11607631 : 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 : 23289447 : else if (!native_encode_initializer (val,
8135 : : ptr
8136 : 11529591 : ? 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 : 568989 : else if (curpos + fieldsize > off
8152 : 32268 : && curpos < (HOST_WIDE_INT) off + len)
8153 : : {
8154 : : /* Partial overlap. */
8155 : 9223 : unsigned char *p = NULL;
8156 : 9223 : int no = 0;
8157 : 9223 : int l;
8158 : 9223 : gcc_assert (mask == NULL);
8159 : 9223 : if (curpos >= off)
8160 : : {
8161 : 6811 : if (ptr)
8162 : 6811 : p = ptr + curpos - off;
8163 : 6811 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8164 : : fieldsize);
8165 : : }
8166 : : else
8167 : : {
8168 : 2412 : p = ptr;
8169 : 2412 : no = off - curpos;
8170 : 2412 : l = len;
8171 : : }
8172 : 9223 : if (!native_encode_initializer (val, p, l, no, NULL))
8173 : : return 0;
8174 : : }
8175 : 11936675 : curpos += fieldsize;
8176 : : }
8177 : 11936675 : while (count-- != 0);
8178 : 11859139 : }
8179 : 46205 : return MIN (total_bytes - off, len);
8180 : : }
8181 : 616024 : else if (TREE_CODE (type) == RECORD_TYPE
8182 : 616024 : || TREE_CODE (type) == UNION_TYPE)
8183 : : {
8184 : 616024 : unsigned HOST_WIDE_INT cnt;
8185 : 616024 : constructor_elt *ce;
8186 : 616024 : tree fld_base = TYPE_FIELDS (type);
8187 : 616024 : tree to_free = NULL_TREE;
8188 : :
8189 : 616024 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8190 : 616024 : if (ptr != NULL)
8191 : 616024 : memset (ptr, '\0', MIN (total_bytes - o, len));
8192 : 107109 : for (cnt = 0; ; cnt++)
8193 : : {
8194 : 723133 : tree val = NULL_TREE, field = NULL_TREE;
8195 : 723133 : HOST_WIDE_INT pos = 0, fieldsize;
8196 : 723133 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8197 : :
8198 : 723133 : if (to_free)
8199 : : {
8200 : 0 : ggc_free (to_free);
8201 : 0 : to_free = NULL_TREE;
8202 : : }
8203 : :
8204 : 723133 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8205 : : {
8206 : 130327 : val = ce->value;
8207 : 130327 : field = ce->index;
8208 : 130327 : if (field == NULL_TREE)
8209 : : return 0;
8210 : :
8211 : 130327 : pos = int_byte_position (field);
8212 : 130327 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8213 : 1433 : continue;
8214 : : }
8215 : 592806 : else if (mask == NULL
8216 : 592806 : || CONSTRUCTOR_NO_CLEARING (init))
8217 : : break;
8218 : : else
8219 : : pos = total_bytes;
8220 : :
8221 : 131115 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8222 : : {
8223 : : tree fld;
8224 : 11825 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8225 : : {
8226 : 11320 : if (TREE_CODE (fld) != FIELD_DECL)
8227 : 10155 : continue;
8228 : 1165 : 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 : 1581 : if (fld == NULL_TREE)
8240 : : {
8241 : 505 : if (ce == NULL)
8242 : : break;
8243 : : return 0;
8244 : : }
8245 : 1076 : fld_base = DECL_CHAIN (fld);
8246 : 1076 : 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 : 128951 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8258 : 6333 : && TYPE_DOMAIN (TREE_TYPE (field))
8259 : 135284 : && ! 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 : 128870 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8283 : 128870 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8284 : : return 0;
8285 : 128870 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8286 : : }
8287 : 128951 : 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 : 128950 : if (DECL_BIT_FIELD (field))
8293 : : {
8294 : 2725 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8295 : : return 0;
8296 : 2725 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8297 : 2725 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8298 : : {
8299 : 2725 : bpos %= BITS_PER_UNIT;
8300 : 2725 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8301 : 2725 : epos = fieldsize % BITS_PER_UNIT;
8302 : 2725 : fieldsize += BITS_PER_UNIT - 1;
8303 : 2725 : 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 : 128950 : if (off != -1 && pos + fieldsize <= off)
8313 : 3298 : continue;
8314 : :
8315 : 125652 : if (val == NULL_TREE)
8316 : 0 : continue;
8317 : :
8318 : 125652 : if (DECL_BIT_FIELD (field)
8319 : 125652 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8320 : : {
8321 : : /* FIXME: Handle PDP endian. */
8322 : 2521 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8323 : 261 : return 0;
8324 : :
8325 : 2521 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8326 : 6 : val = TREE_OPERAND (val, 0);
8327 : 2521 : if (TREE_CODE (val) != INTEGER_CST)
8328 : : return 0;
8329 : :
8330 : 2521 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8331 : 2521 : tree repr_type = NULL_TREE;
8332 : 2521 : HOST_WIDE_INT rpos = 0;
8333 : 2521 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8334 : : {
8335 : 1992 : rpos = int_byte_position (repr);
8336 : 1992 : 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 : 2260 : if (rpos > pos)
8355 : : return 0;
8356 : 2260 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8357 : 2260 : int diff = (TYPE_PRECISION (repr_type)
8358 : 2260 : - TYPE_PRECISION (TREE_TYPE (field)));
8359 : 2260 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8360 : 2260 : if (!BYTES_BIG_ENDIAN)
8361 : 2260 : w = wi::lshift (w, bitoff);
8362 : : else
8363 : : w = wi::lshift (w, diff - bitoff);
8364 : 2260 : val = wide_int_to_tree (repr_type, w);
8365 : :
8366 : 2260 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8367 : : / BITS_PER_UNIT + 1];
8368 : 2260 : int l = native_encode_int (val, buf, sizeof buf, 0);
8369 : 2260 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8370 : 0 : return 0;
8371 : :
8372 : 2260 : 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 : 2260 : if (bpos
8378 : 1356 : && (off == -1 || (pos >= off && len >= 1)))
8379 : : {
8380 : 1281 : if (!BYTES_BIG_ENDIAN)
8381 : : {
8382 : 1281 : int msk = (1 << bpos) - 1;
8383 : 1281 : buf[pos - rpos] &= ~msk;
8384 : 1281 : buf[pos - rpos] |= ptr[pos - o] & msk;
8385 : 1281 : 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 : 2260 : if (epos
8412 : 1731 : && (off == -1
8413 : 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8414 : : {
8415 : 1628 : if (!BYTES_BIG_ENDIAN)
8416 : : {
8417 : 1628 : int msk = (1 << epos) - 1;
8418 : 1628 : buf[pos - rpos + fieldsize - 1] &= msk;
8419 : 1628 : buf[pos - rpos + fieldsize - 1]
8420 : 1628 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8421 : 1628 : 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 : 2260 : if (off == -1
8435 : 1301 : || (pos >= off
8436 : 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8437 : : {
8438 : 2069 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8439 : 2069 : 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 : 2260 : continue;
8458 : 2260 : }
8459 : :
8460 : 123131 : if (off == -1
8461 : 19748 : || (pos >= off
8462 : 18954 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8463 : : {
8464 : 113670 : int fldsize = fieldsize;
8465 : 10287 : if (off == -1)
8466 : : {
8467 : 103383 : tree fld = DECL_CHAIN (field);
8468 : 1358468 : while (fld)
8469 : : {
8470 : 1264758 : if (TREE_CODE (fld) == FIELD_DECL)
8471 : : break;
8472 : 1255085 : fld = DECL_CHAIN (fld);
8473 : : }
8474 : 103383 : if (fld == NULL_TREE)
8475 : 93710 : fldsize = len - pos;
8476 : : }
8477 : 124793 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8478 : : : NULL,
8479 : : fldsize,
8480 : : off == -1 ? -1 : 0,
8481 : 836 : mask ? mask + pos : NULL);
8482 : 113670 : if (!r)
8483 : : return 0;
8484 : 99134 : if (off == -1
8485 : 97217 : && fldsize != fieldsize
8486 : 324 : && r > fieldsize
8487 : 54 : && pos + r > total_bytes)
8488 : 107109 : total_bytes = pos + r;
8489 : : }
8490 : : else
8491 : : {
8492 : : /* Partial overlap. */
8493 : 9461 : unsigned char *p = NULL;
8494 : 9461 : int no = 0;
8495 : 9461 : int l;
8496 : 9461 : gcc_assert (mask == NULL);
8497 : 9461 : if (pos >= off)
8498 : : {
8499 : 8667 : if (ptr)
8500 : 8667 : p = ptr + pos - off;
8501 : 8667 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8502 : : fieldsize);
8503 : : }
8504 : : else
8505 : : {
8506 : 794 : p = ptr;
8507 : 794 : no = off - pos;
8508 : 794 : l = len;
8509 : : }
8510 : 9461 : if (!native_encode_initializer (val, p, l, no, NULL))
8511 : : return 0;
8512 : : }
8513 : 107109 : }
8514 : 592749 : 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 : 2744648 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8527 : : {
8528 : 2744648 : int total_bytes;
8529 : 2744648 : 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 : 5489262 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8551 : :
8552 : 2744648 : if (total_bytes > len)
8553 : : return NULL_TREE;
8554 : :
8555 : 2744412 : wide_int result = wi::from_buffer (ptr, total_bytes);
8556 : :
8557 : 2744412 : return wide_int_to_tree (type, result);
8558 : 2744412 : }
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 : 31389 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8590 : : {
8591 : 31389 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8592 : 31389 : int total_bytes = GET_MODE_SIZE (mode);
8593 : 31389 : 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 : 31389 : REAL_VALUE_TYPE r;
8598 : 31389 : long tmp[6];
8599 : :
8600 : 31389 : if (total_bytes > len || total_bytes > 24)
8601 : : return NULL_TREE;
8602 : 31328 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8603 : :
8604 : 31328 : memset (tmp, 0, sizeof (tmp));
8605 : 225710 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8606 : 194382 : bitpos += BITS_PER_UNIT)
8607 : : {
8608 : : /* Both OFFSET and BYTE index within a long;
8609 : : bitpos indexes the whole float. */
8610 : 194382 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8611 : 194382 : 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 : 194382 : offset = byte;
8625 : 194382 : 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 : 194382 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8634 : :
8635 : 194382 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8636 : : }
8637 : :
8638 : 31328 : real_from_target (&r, tmp, mode);
8639 : 31328 : 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 : 1385 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8649 : : {
8650 : 1385 : tree etype, rpart, ipart;
8651 : 1385 : int size;
8652 : :
8653 : 1385 : etype = TREE_TYPE (type);
8654 : 1385 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8655 : 1385 : if (size * 2 > len)
8656 : : return NULL_TREE;
8657 : 1350 : rpart = native_interpret_expr (etype, ptr, size);
8658 : 1350 : if (!rpart)
8659 : : return NULL_TREE;
8660 : 1349 : ipart = native_interpret_expr (etype, ptr+size, size);
8661 : 1349 : if (!ipart)
8662 : : return NULL_TREE;
8663 : 1349 : 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 : 188400 : 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 : 188400 : tree elt_type = TREE_TYPE (type);
8678 : 188400 : if (VECTOR_BOOLEAN_TYPE_P (type)
8679 : 188403 : && 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 : 188399 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8701 : 188399 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8702 : : return NULL_TREE;
8703 : :
8704 : 188399 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8705 : 764186 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8706 : : {
8707 : 575825 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8708 : 575825 : if (!elt)
8709 : 38 : return NULL_TREE;
8710 : 575787 : builder.quick_push (elt);
8711 : 575787 : bytes += elt_bytes;
8712 : : }
8713 : 188361 : return builder.build ();
8714 : 188399 : }
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 : 74646 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8722 : : {
8723 : 74646 : unsigned HOST_WIDE_INT size;
8724 : :
8725 : 74646 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8726 : 74646 : || size > len)
8727 : 2 : return NULL_TREE;
8728 : :
8729 : 74644 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8730 : 74644 : 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 : 2899366 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8742 : : {
8743 : 2899366 : switch (TREE_CODE (type))
8744 : : {
8745 : 2744648 : case INTEGER_TYPE:
8746 : 2744648 : case ENUMERAL_TYPE:
8747 : 2744648 : case BOOLEAN_TYPE:
8748 : 2744648 : case POINTER_TYPE:
8749 : 2744648 : case REFERENCE_TYPE:
8750 : 2744648 : case OFFSET_TYPE:
8751 : 2744648 : case BITINT_TYPE:
8752 : 2744648 : return native_interpret_int (type, ptr, len);
8753 : :
8754 : 30175 : case REAL_TYPE:
8755 : 30175 : 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 : 30114 : unsigned char buf[24 * 2];
8764 : 30114 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8765 : 30114 : int total_bytes = GET_MODE_SIZE (mode);
8766 : 30114 : memcpy (buf + 24, ptr, total_bytes);
8767 : 30114 : clear_type_padding_in_mask (type, buf + 24);
8768 : 30114 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8769 : 30114 : || 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 : 1385 : case COMPLEX_TYPE:
8779 : 1385 : return native_interpret_complex (type, ptr, len);
8780 : :
8781 : 74646 : case VECTOR_TYPE:
8782 : 74646 : 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 : 418885 : can_native_interpret_type_p (tree type)
8794 : : {
8795 : 418885 : 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 : 77687 : default:
8809 : 77687 : 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 : 609 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8818 : : int len)
8819 : : {
8820 : 609 : vec<constructor_elt, va_gc> *elts = NULL;
8821 : 609 : 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 : 609 : 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 : 412 : if (TREE_CODE (type) != RECORD_TYPE)
8860 : : return NULL_TREE;
8861 : 6708 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8862 : : {
8863 : 1144 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8864 : 7440 : || is_empty_type (TREE_TYPE (field)))
8865 : 5242 : continue;
8866 : 1054 : tree fld = field;
8867 : 1054 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8868 : 1054 : int diff = 0;
8869 : 1054 : tree v = NULL_TREE;
8870 : 1054 : 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 : 1042 : sz = int_size_in_bytes (TREE_TYPE (fld));
8932 : 1042 : if (sz < 0 || sz > len)
8933 : : return NULL_TREE;
8934 : 1042 : tree byte_pos = byte_position (fld);
8935 : 1042 : if (!tree_fits_shwi_p (byte_pos))
8936 : : return NULL_TREE;
8937 : 1042 : pos = tree_to_shwi (byte_pos);
8938 : 1042 : if (pos < 0 || pos > len || pos + sz > len)
8939 : : return NULL_TREE;
8940 : : }
8941 : 1042 : if (fld == NULL_TREE)
8942 : : /* Already handled above. */;
8943 : 1042 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8944 : : {
8945 : 850 : v = native_interpret_expr (TREE_TYPE (fld),
8946 : 850 : ptr + off + pos, sz);
8947 : 850 : 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 : 1054 : 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 : 1054 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8972 : : }
8973 : 412 : 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 : 42203 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8988 : : unsigned int amnt)
8989 : : {
8990 : 42203 : if (amnt == 0)
8991 : : return;
8992 : :
8993 : 24737 : unsigned char carry_over = 0U;
8994 : 24737 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8995 : 24737 : unsigned char clear_mask = (~0U) << amnt;
8996 : :
8997 : 136615 : for (unsigned int i = 0; i < sz; i++)
8998 : : {
8999 : 111878 : unsigned prev_carry_over = carry_over;
9000 : 111878 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9001 : :
9002 : 111878 : ptr[i] <<= amnt;
9003 : 111878 : if (i != 0)
9004 : : {
9005 : 87141 : ptr[i] &= clear_mask;
9006 : 87141 : 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 : 120903 : fold_view_convert_vector_encoding (tree type, tree expr)
9047 : : {
9048 : 120903 : tree expr_type = TREE_TYPE (expr);
9049 : 120903 : poly_uint64 type_bits, expr_bits;
9050 : 120903 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9051 : 120903 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9052 : 0 : return NULL_TREE;
9053 : :
9054 : 120903 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9055 : 120903 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9056 : 120903 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9057 : 120903 : 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 : 120903 : if (VECTOR_CST_STEPPED_P (expr)
9062 : 120903 : && (!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 : 113756 : unsigned int expr_sequence_bits
9068 : 113756 : = 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 : 113756 : unsigned int type_sequence_bits
9073 : 113756 : = 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 : 113756 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9080 : 113756 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9081 : : BITS_PER_UNIT);
9082 : 113756 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9083 : 113756 : if (known_gt (buffer_bits, expr_bits))
9084 : : return NULL_TREE;
9085 : :
9086 : : /* Get enough bytes of EXPR to form the new encoding. */
9087 : 113756 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9088 : 113756 : buffer.quick_grow (buffer_bytes);
9089 : 113756 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9090 : 113756 : buffer_bits / expr_elt_bits)
9091 : : != (int) buffer_bytes)
9092 : : return NULL_TREE;
9093 : :
9094 : : /* Reencode the bytes as TYPE. */
9095 : 113756 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9096 : 227512 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9097 : 113756 : type_npatterns, nelts_per_pattern);
9098 : 113756 : }
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 : 11349314 : fold_view_convert_expr (tree type, tree expr)
9106 : : {
9107 : 11349314 : unsigned char buffer[128];
9108 : 11349314 : unsigned char *buf;
9109 : 11349314 : int len;
9110 : 11349314 : HOST_WIDE_INT l;
9111 : :
9112 : : /* Check that the host and target are sane. */
9113 : 11349314 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9114 : : return NULL_TREE;
9115 : :
9116 : 11349314 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9117 : 120903 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9118 : : return res;
9119 : :
9120 : 11235577 : l = int_size_in_bytes (type);
9121 : 11235577 : if (l > (int) sizeof (buffer)
9122 : 11235577 : && 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 : 11235577 : len = native_encode_expr (expr, buf, len);
9133 : 11235577 : if (len == 0)
9134 : : return NULL_TREE;
9135 : :
9136 : 1674404 : 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 : 430407922 : 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 : 430407922 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9147 : 0 : t = TREE_OPERAND (t, 0);
9148 : :
9149 : 430407922 : if (INDIRECT_REF_P (t))
9150 : : {
9151 : 49036049 : t = TREE_OPERAND (t, 0);
9152 : :
9153 : 49036049 : if (TREE_TYPE (t) != ptrtype)
9154 : 31346749 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9155 : : }
9156 : 381371873 : else if (TREE_CODE (t) == MEM_REF
9157 : 381371873 : && integer_zerop (TREE_OPERAND (t, 1)))
9158 : : {
9159 : 1612548 : t = TREE_OPERAND (t, 0);
9160 : :
9161 : 1612548 : if (TREE_TYPE (t) != ptrtype)
9162 : 1061285 : t = fold_convert_loc (loc, ptrtype, t);
9163 : : }
9164 : 379759325 : else if (TREE_CODE (t) == MEM_REF
9165 : 379759325 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9166 : 661 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9167 : : TREE_OPERAND (t, 0),
9168 : : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9169 : 379758664 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9170 : : {
9171 : 26053337 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9172 : :
9173 : 26053337 : if (TREE_TYPE (t) != ptrtype)
9174 : 13922 : t = fold_convert_loc (loc, ptrtype, t);
9175 : : }
9176 : : else
9177 : 353705327 : 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 : 398264848 : build_fold_addr_expr_loc (location_t loc, tree t)
9186 : : {
9187 : 398264848 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9188 : :
9189 : 398264848 : 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 : 1629830252 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9198 : : {
9199 : 1629830252 : tree tem;
9200 : 1629830252 : tree arg0;
9201 : 1629830252 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9202 : :
9203 : 1629830252 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9204 : : && TREE_CODE_LENGTH (code) == 1);
9205 : :
9206 : 1629830252 : arg0 = op0;
9207 : 1629830252 : if (arg0)
9208 : : {
9209 : 1629817087 : 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 : 807393848 : 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 : 822423239 : STRIP_NOPS (arg0);
9230 : : }
9231 : :
9232 : 1629817087 : if (CONSTANT_CLASS_P (arg0))
9233 : : {
9234 : 234797351 : tree tem = const_unop (code, type, arg0);
9235 : 234797351 : if (tem)
9236 : : {
9237 : 198927133 : if (TREE_TYPE (tem) != type)
9238 : 68967 : tem = fold_convert_loc (loc, type, tem);
9239 : 198927133 : return tem;
9240 : : }
9241 : : }
9242 : : }
9243 : :
9244 : 1430903119 : tem = generic_simplify (loc, code, type, op0);
9245 : 1430903119 : if (tem)
9246 : : return tem;
9247 : :
9248 : 1083473297 : if (TREE_CODE_CLASS (code) == tcc_unary)
9249 : : {
9250 : 550298722 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9251 : 1041829 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9252 : : fold_build1_loc (loc, code, type,
9253 : 1041829 : fold_convert_loc (loc, TREE_TYPE (op0),
9254 : 2083658 : TREE_OPERAND (arg0, 1))));
9255 : 549256893 : else if (TREE_CODE (arg0) == COND_EXPR)
9256 : : {
9257 : 428195 : tree arg01 = TREE_OPERAND (arg0, 1);
9258 : 428195 : tree arg02 = TREE_OPERAND (arg0, 2);
9259 : 428195 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9260 : 424013 : arg01 = fold_build1_loc (loc, code, type,
9261 : : fold_convert_loc (loc,
9262 : 424013 : TREE_TYPE (op0), arg01));
9263 : 428195 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9264 : 424887 : arg02 = fold_build1_loc (loc, code, type,
9265 : : fold_convert_loc (loc,
9266 : 424887 : TREE_TYPE (op0), arg02));
9267 : 428195 : 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 : 428195 : if ((CONVERT_EXPR_CODE_P (code)
9280 : 10012 : || code == NON_LVALUE_EXPR)
9281 : 418202 : && TREE_CODE (tem) == COND_EXPR
9282 : 399759 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9283 : 364901 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9284 : 172850 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9285 : 172638 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9286 : 172638 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9287 : 172638 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9288 : 606264 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9289 : 6317 : && (INTEGRAL_TYPE_P
9290 : : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9291 : 6277 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9292 : 6164 : || flag_syntax_only))
9293 : 165628 : tem = build1_loc (loc, code, type,
9294 : : build3 (COND_EXPR,
9295 : 165628 : TREE_TYPE (TREE_OPERAND
9296 : : (TREE_OPERAND (tem, 1), 0)),
9297 : 165628 : TREE_OPERAND (tem, 0),
9298 : 165628 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9299 : 165628 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9300 : : 0)));
9301 : 428195 : return tem;
9302 : : }
9303 : : }
9304 : :
9305 : 1082003273 : switch (code)
9306 : : {
9307 : 39408464 : case NON_LVALUE_EXPR:
9308 : 39408464 : if (!maybe_lvalue_p (op0))
9309 : 29408251 : return fold_convert_loc (loc, type, op0);
9310 : : return NULL_TREE;
9311 : :
9312 : 500259018 : CASE_CONVERT:
9313 : 500259018 : case FLOAT_EXPR:
9314 : 500259018 : case FIX_TRUNC_EXPR:
9315 : 500259018 : 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 : 415147 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9324 : 81340 : return build2_loc (loc, TREE_CODE (op0), type,
9325 : 81340 : TREE_OPERAND (op0, 0),
9326 : 162680 : TREE_OPERAND (op0, 1));
9327 : 333807 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9328 : 7602 : && TREE_CODE (type) != VECTOR_TYPE)
9329 : 7602 : return build3_loc (loc, COND_EXPR, type, op0,
9330 : : constant_boolean_node (true, type),
9331 : 7602 : 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 : 500170076 : if (TREE_CODE (op0) == ADDR_EXPR
9338 : 112220941 : && POINTER_TYPE_P (type)
9339 : 605835454 : && handled_component_p (TREE_OPERAND (op0, 0)))
9340 : : {
9341 : 19041694 : poly_int64 bitsize, bitpos;
9342 : 19041694 : tree offset;
9343 : 19041694 : machine_mode mode;
9344 : 19041694 : int unsignedp, reversep, volatilep;
9345 : 19041694 : tree base
9346 : 19041694 : = 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 : 19041694 : if (!offset
9353 : 18931113 : && known_eq (bitpos, 0)
9354 : 12912855 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9355 : 12912855 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9356 : 19052487 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9357 : 10592 : return fold_convert_loc (loc, type,
9358 : 10592 : build_fold_addr_expr_loc (loc, base));
9359 : : }
9360 : :
9361 : 500159484 : if (TREE_CODE (op0) == MODIFY_EXPR
9362 : 265043 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9363 : : /* Detect assigning a bitfield. */
9364 : 500161485 : && !(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 : 1953 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9371 : : /* First do the assignment, then return converted constant. */
9372 : 1953 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9373 : 1953 : suppress_warning (tem /* What warning? */);
9374 : 1953 : TREE_USED (tem) = 1;
9375 : 1953 : 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 : 500157531 : if (TREE_CODE (type) == INTEGER_TYPE
9385 : 220910724 : && TREE_CODE (op0) == BIT_AND_EXPR
9386 : 500641366 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9387 : : {
9388 : 221944 : tree and_expr = op0;
9389 : 221944 : tree and0 = TREE_OPERAND (and_expr, 0);
9390 : 221944 : tree and1 = TREE_OPERAND (and_expr, 1);
9391 : 221944 : int change = 0;
9392 : :
9393 : 221944 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9394 : 221944 : || (TYPE_PRECISION (type)
9395 : 76057 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9396 : : change = 1;
9397 : 22789 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9398 : : <= HOST_BITS_PER_WIDE_INT
9399 : 22789 : && tree_fits_uhwi_p (and1))
9400 : : {
9401 : 21687 : unsigned HOST_WIDE_INT cst;
9402 : :
9403 : 21687 : cst = tree_to_uhwi (and1);
9404 : 43374 : cst &= HOST_WIDE_INT_M1U
9405 : 21687 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9406 : 21687 : change = (cst == 0);
9407 : 21687 : if (change
9408 : 21687 : && !flag_syntax_only
9409 : 43374 : && (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 : 21687 : if (change)
9418 : : {
9419 : 220842 : tree and1_type = TREE_TYPE (and1);
9420 : 220842 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9421 : : TYPE_PRECISION (type));
9422 : 220842 : tem = force_fit_type (type,
9423 : 220842 : wide_int::from (wi::to_wide (and1), prec,
9424 : 220842 : TYPE_SIGN (and1_type)),
9425 : 220842 : 0, TREE_OVERFLOW (and1));
9426 : 220842 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9427 : 220842 : 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 : 499936689 : if (POINTER_TYPE_P (type)
9435 : 242927171 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9436 : 504980401 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9437 : : {
9438 : 2740314 : tree arg00 = TREE_OPERAND (arg0, 0);
9439 : 2740314 : 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 : 2740314 : if (!in_gimple_form
9445 : 2726587 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9446 : 2741528 : && (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 : 2740247 : if (TREE_CODE (type) == REFERENCE_TYPE
9455 : 1823919 : && !in_gimple_form
9456 : 1823902 : && sanitize_flags_p (SANITIZE_NULL)
9457 : 2740678 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9458 : : return NULL_TREE;
9459 : :
9460 : 2739816 : arg00 = fold_convert_loc (loc, type, arg00);
9461 : 2739816 : 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 : 497196375 : if (INTEGRAL_TYPE_P (type)
9468 : 226657943 : && TREE_CODE (op0) == BIT_NOT_EXPR
9469 : 531886 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9470 : 531886 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9471 : 497524459 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9472 : : {
9473 : 326385 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9474 : 395100 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9475 : 395098 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9476 : 264923 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9477 : 264923 : 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 : 496931452 : if (INTEGRAL_TYPE_P (type)
9483 : 226393020 : && TREE_CODE (op0) == MULT_EXPR
9484 : 7928864 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9485 : 7907224 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9486 : 496991569 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9487 : 13553 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9488 : : {
9489 : : /* Be careful not to introduce new overflows. */
9490 : 60077 : tree mult_type;
9491 : 60077 : if (TYPE_OVERFLOW_WRAPS (type))
9492 : : mult_type = type;
9493 : : else
9494 : 1978 : mult_type = unsigned_type_for (type);
9495 : :
9496 : 60077 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9497 : : {
9498 : 120154 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9499 : : fold_convert_loc (loc, mult_type,
9500 : 60077 : TREE_OPERAND (op0, 0)),
9501 : : fold_convert_loc (loc, mult_type,
9502 : 60077 : TREE_OPERAND (op0, 1)));
9503 : 60077 : return fold_convert_loc (loc, type, tem);
9504 : : }
9505 : : }
9506 : :
9507 : : return NULL_TREE;
9508 : :
9509 : 224402962 : case VIEW_CONVERT_EXPR:
9510 : 224402962 : if (TREE_CODE (op0) == MEM_REF)
9511 : : {
9512 : 2468 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9513 : 5 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9514 : 2468 : tem = fold_build2_loc (loc, MEM_REF, type,
9515 : 2468 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9516 : 2468 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9517 : 2468 : return tem;
9518 : : }
9519 : :
9520 : : return NULL_TREE;
9521 : :
9522 : 3732819 : case NEGATE_EXPR:
9523 : 3732819 : tem = fold_negate_expr (loc, arg0);
9524 : 3732819 : if (tem)
9525 : 1555 : return fold_convert_loc (loc, type, tem);
9526 : : return NULL_TREE;
9527 : :
9528 : 2726186 : case ABS_EXPR:
9529 : : /* Convert fabs((double)float) into (double)fabsf(float). */
9530 : 2726186 : if (TREE_CODE (arg0) == NOP_EXPR
9531 : 22759 : && TREE_CODE (type) == REAL_TYPE)
9532 : : {
9533 : 22723 : tree targ0 = strip_float_extensions (arg0);
9534 : 22723 : if (targ0 != arg0)
9535 : 22519 : return fold_convert_loc (loc, type,
9536 : : fold_build1_loc (loc, ABS_EXPR,
9537 : 22519 : TREE_TYPE (targ0),
9538 : 22519 : targ0));
9539 : : }
9540 : : return NULL_TREE;
9541 : :
9542 : 2696497 : case BIT_NOT_EXPR:
9543 : : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9544 : 2696497 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9545 : 2698182 : && (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 : 2696483 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9552 : 2698154 : && (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 : 48458387 : 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 : 48458387 : tem = fold_truth_not_expr (loc, arg0);
9567 : 48458387 : if (!tem)
9568 : : return NULL_TREE;
9569 : 34203277 : return fold_convert_loc (loc, type, tem);
9570 : :
9571 : 64583893 : case INDIRECT_REF:
9572 : : /* Fold *&X to X if X is an lvalue. */
9573 : 64583893 : if (TREE_CODE (op0) == ADDR_EXPR)
9574 : : {
9575 : 6444 : tree op00 = TREE_OPERAND (op0, 0);
9576 : 6444 : if ((VAR_P (op00)
9577 : : || TREE_CODE (op00) == PARM_DECL
9578 : : || TREE_CODE (op00) == RESULT_DECL)
9579 : 5255 : && !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 : 25088177 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9615 : : tree arg0, tree arg1, tree op0, tree op1)
9616 : : {
9617 : 25088177 : tree tem;
9618 : :
9619 : : /* We only do these simplifications if we are optimizing. */
9620 : 25088177 : 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 : 24811925 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9630 : 5474757 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9631 : : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9632 : : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9633 : 5474757 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9634 : 24840818 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9635 : : {
9636 : 28366 : tree a00 = TREE_OPERAND (arg0, 0);
9637 : 28366 : tree a01 = TREE_OPERAND (arg0, 1);
9638 : 28366 : tree a10 = TREE_OPERAND (arg1, 0);
9639 : 28366 : tree a11 = TREE_OPERAND (arg1, 1);
9640 : 56732 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9641 : 28366 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9642 : 28366 : && (code == TRUTH_AND_EXPR
9643 : 9142 : || code == TRUTH_OR_EXPR));
9644 : :
9645 : 28366 : if (operand_equal_p (a00, a10, 0))
9646 : 397 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9647 : 397 : fold_build2_loc (loc, code, type, a01, a11));
9648 : 27969 : 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 : 27969 : 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 : 27927 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9659 : 55368 : && 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 : 24811485 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9667 : : return tem;
9668 : :
9669 : 23718181 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9670 : 23716154 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9671 : : {
9672 : 20760 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9673 : 20760 : if (tem)
9674 : 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9675 : : }
9676 : :
9677 : 23718168 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9678 : 23708999 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9679 : : {
9680 : 120712 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9681 : 120712 : if (tem)
9682 : 91 : 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 : 23718077 : if (TREE_CODE (arg0) == code
9689 : 24446717 : && (tem = fold_truth_andor_1 (loc, code, type,
9690 : 728640 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9691 : 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9692 : :
9693 : 23717992 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9694 : : return tem;
9695 : :
9696 : 23677932 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9697 : 23677932 : if (param_logical_op_non_short_circuit != -1)
9698 : 8294 : logical_op_non_short_circuit
9699 : 8294 : = param_logical_op_non_short_circuit;
9700 : 23677932 : if (logical_op_non_short_circuit
9701 : 23673751 : && !sanitize_coverage_p ()
9702 : 23677932 : && (code == TRUTH_AND_EXPR
9703 : 23673748 : || code == TRUTH_ANDIF_EXPR
9704 : 11301933 : || code == TRUTH_OR_EXPR
9705 : 11301933 : || code == TRUTH_ORIF_EXPR))
9706 : : {
9707 : 23673748 : enum tree_code ncode, icode;
9708 : :
9709 : 23673748 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9710 : 23673748 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9711 : 12371815 : 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 : 23673748 : if (TREE_CODE (arg0) == icode
9723 : 720132 : && simple_condition_p (arg1)
9724 : : /* Needed for sequence points to handle trappings, and
9725 : : side-effects. */
9726 : 23723542 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9727 : : {
9728 : 42928 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9729 : : arg1);
9730 : 42928 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9731 : 42928 : 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 : 23630820 : else if (TREE_CODE (arg1) == icode
9736 : 2956 : && simple_condition_p (arg0)
9737 : : /* Needed for sequence points to handle trappings, and
9738 : : side-effects. */
9739 : 23631769 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9740 : : {
9741 : 36 : tem = fold_build2_loc (loc, ncode, type,
9742 : 36 : arg0, TREE_OPERAND (arg1, 0));
9743 : 36 : return fold_build2_loc (loc, icode, type, tem,
9744 : 72 : 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 : 4482392 : else if (code == icode && simple_condition_p (arg0)
9751 : 24335050 : && simple_condition_p (arg1))
9752 : 371547 : 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 : 168456227 : 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 : 168456227 : enum tree_code code0 = TREE_CODE (arg0);
9772 : 168456227 : tree t, cst0 = NULL_TREE;
9773 : 168456227 : int sgn0;
9774 : :
9775 : : /* Match A +- CST code arg1. We can change this only if overflow
9776 : : is undefined. */
9777 : 168456227 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9778 : 128180880 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9779 : : /* In principle pointers also have undefined overflow behavior,
9780 : : but that causes problems elsewhere. */
9781 : 62649730 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9782 : 62649730 : && (code0 == MINUS_EXPR
9783 : 62649730 : || code0 == PLUS_EXPR)
9784 : 2567951 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9785 : : return NULL_TREE;
9786 : :
9787 : : /* Identify the constant in arg0 and its sign. */
9788 : 2112613 : cst0 = TREE_OPERAND (arg0, 1);
9789 : 2112613 : sgn0 = tree_int_cst_sgn (cst0);
9790 : :
9791 : : /* Overflowed constants and zero will cause problems. */
9792 : 2112613 : if (integer_zerop (cst0)
9793 : 2112613 : || 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 : 2112613 : if (code == LT_EXPR
9800 : 1211638 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9801 : : code = LE_EXPR;
9802 : : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9803 : 1914138 : else if (code == GT_EXPR
9804 : 554757 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9805 : : code = GE_EXPR;
9806 : : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9807 : 1741974 : else if (code == LE_EXPR
9808 : 637849 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9809 : : code = LT_EXPR;
9810 : : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9811 : 1524407 : else if (code == GE_EXPR
9812 : 515066 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9813 : : code = GT_EXPR;
9814 : : else
9815 : : return NULL_TREE;
9816 : 820562 : *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 : 1641124 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9821 : 1641124 : && ((sgn0 == 1
9822 : 389593 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9823 : 389593 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9824 : 820562 : || (sgn0 == -1
9825 : 430969 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9826 : 430969 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9827 : 0 : return NULL_TREE;
9828 : :
9829 : 1210155 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9830 : 820562 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9831 : 820562 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9832 : 820562 : t = fold_convert (TREE_TYPE (arg1), t);
9833 : :
9834 : 820562 : 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 : 84617909 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9845 : : tree arg0, tree arg1)
9846 : : {
9847 : 84617909 : tree t;
9848 : 84617909 : bool strict_overflow_p;
9849 : 84617909 : 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 : 84617909 : strict_overflow_p = false;
9854 : 84617909 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9855 : : &strict_overflow_p);
9856 : 84617909 : if (t)
9857 : : {
9858 : 779591 : if (strict_overflow_p)
9859 : 779591 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9860 : 779591 : return t;
9861 : : }
9862 : :
9863 : : /* Try canonicalization by simplifying arg1 using the swapped
9864 : : comparison. */
9865 : 83838318 : code = swap_tree_comparison (code);
9866 : 83838318 : strict_overflow_p = false;
9867 : 83838318 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9868 : : &strict_overflow_p);
9869 : 83838318 : if (t && strict_overflow_p)
9870 : 40971 : 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 : 18443 : pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9880 : : {
9881 : 18443 : if (!POINTER_TYPE_P (TREE_TYPE (base)))
9882 : : return true;
9883 : :
9884 : 10616 : if (maybe_lt (bitpos, 0))
9885 : : return true;
9886 : :
9887 : : poly_wide_int wi_offset;
9888 : 9527 : int precision = TYPE_PRECISION (TREE_TYPE (base));
9889 : 9527 : if (offset == NULL_TREE)
9890 : 4639 : wi_offset = wi::zero (precision);
9891 : 4888 : 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 : 4639 : wi::overflow_type overflow;
9897 : 4639 : poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9898 : 4639 : precision);
9899 : 4639 : poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9900 : 4639 : if (overflow)
9901 : : return true;
9902 : :
9903 : 4639 : poly_uint64 total_hwi, size;
9904 : 4639 : if (!total.to_uhwi (&total_hwi)
9905 : 4639 : || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9906 : : &size)
9907 : 9184 : || known_eq (size, 0U))
9908 : 94 : return true;
9909 : :
9910 : 4545 : 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 : 1146 : 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 : 1146 : && known_le (total_hwi, size))
9920 : : return false;
9921 : :
9922 : : return true;
9923 : 9527 : }
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 : 11712460 : maybe_nonzero_address (tree decl)
9932 : : {
9933 : 11712460 : 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 : 9134350 : if (decl_in_symtab_p (decl))
9941 : : {
9942 : 3817264 : if (struct symtab_node *symbol
9943 : 3817264 : = (folding_initializer
9944 : 3817264 : ? symtab_node::get_create (decl)
9945 : 3800738 : : symtab_node::get (decl)))
9946 : 3798471 : return symbol->nonzero_address ();
9947 : : }
9948 : 5317086 : 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 : 5277009 : if (DECL_CONTEXT (decl)
9954 : 5261050 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9955 : 10534734 : && 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 : 84689240 : fold_comparison (location_t loc, enum tree_code code, tree type,
9971 : : tree op0, tree op1)
9972 : : {
9973 : 84689240 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9974 : 84689240 : tree arg0, arg1, tem;
9975 : :
9976 : 84689240 : arg0 = op0;
9977 : 84689240 : arg1 = op1;
9978 : :
9979 : 84689240 : STRIP_SIGN_NOPS (arg0);
9980 : 84689240 : 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 : 156979567 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9987 : 84924798 : && (TREE_CODE (arg0) == ADDR_EXPR
9988 : 12587627 : || TREE_CODE (arg1) == ADDR_EXPR
9989 : 11160308 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9990 : 10471838 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9991 : : {
9992 : 2172181 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9993 : 2172181 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9994 : 2172181 : machine_mode mode;
9995 : 2172181 : int volatilep, reversep, unsignedp;
9996 : 2172181 : 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 : 2172181 : base0 = arg0;
10003 : 2172181 : if (TREE_CODE (arg0) == ADDR_EXPR)
10004 : : {
10005 : 46844 : base0
10006 : 46844 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10007 : : &bitsize, &bitpos0, &offset0, &mode,
10008 : : &unsignedp, &reversep, &volatilep);
10009 : 46844 : if (INDIRECT_REF_P (base0))
10010 : 1787 : base0 = TREE_OPERAND (base0, 0);
10011 : : else
10012 : : indirect_base0 = true;
10013 : : }
10014 : 2125337 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10015 : : {
10016 : 756552 : base0 = TREE_OPERAND (arg0, 0);
10017 : 756552 : STRIP_SIGN_NOPS (base0);
10018 : 756552 : if (TREE_CODE (base0) == ADDR_EXPR)
10019 : : {
10020 : 43058 : base0
10021 : 43058 : = get_inner_reference (TREE_OPERAND (base0, 0),
10022 : : &bitsize, &bitpos0, &offset0, &mode,
10023 : : &unsignedp, &reversep, &volatilep);
10024 : 43058 : if (INDIRECT_REF_P (base0))
10025 : 14 : base0 = TREE_OPERAND (base0, 0);
10026 : : else
10027 : : indirect_base0 = true;
10028 : : }
10029 : 756552 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10030 : 756552 : offset0 = TREE_OPERAND (arg0, 1);
10031 : : else
10032 : 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10033 : : TREE_OPERAND (arg0, 1));
10034 : 756552 : if (poly_int_tree_p (offset0))
10035 : : {
10036 : 643944 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10037 : 643944 : TYPE_PRECISION (sizetype));
10038 : 643944 : tem <<= LOG2_BITS_PER_UNIT;
10039 : 643944 : tem += bitpos0;
10040 : 643944 : if (tem.to_shwi (&bitpos0))
10041 : 643941 : offset0 = NULL_TREE;
10042 : : }
10043 : : }
10044 : :
10045 : 2172181 : base1 = arg1;
10046 : 2172181 : if (TREE_CODE (arg1) == ADDR_EXPR)
10047 : : {
10048 : 1452044 : base1
10049 : 1452044 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10050 : : &bitsize, &bitpos1, &offset1, &mode,
10051 : : &unsignedp, &reversep, &volatilep);
10052 : 1452044 : if (INDIRECT_REF_P (base1))
10053 : 70158 : base1 = TREE_OPERAND (base1, 0);
10054 : : else
10055 : : indirect_base1 = true;
10056 : : }
10057 : 720137 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10058 : : {
10059 : 76325 : base1 = TREE_OPERAND (arg1, 0);
10060 : 76325 : STRIP_SIGN_NOPS (base1);
10061 : 76325 : if (TREE_CODE (base1) == ADDR_EXPR)
10062 : : {
10063 : 10993 : base1
10064 : 10993 : = get_inner_reference (TREE_OPERAND (base1, 0),
10065 : : &bitsize, &bitpos1, &offset1, &mode,
10066 : : &unsignedp, &reversep, &volatilep);
10067 : 10993 : if (INDIRECT_REF_P (base1))
10068 : 0 : base1 = TREE_OPERAND (base1, 0);
10069 : : else
10070 : : indirect_base1 = true;
10071 : : }
10072 : 76325 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10073 : 76301 : offset1 = TREE_OPERAND (arg1, 1);
10074 : : else
10075 : 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10076 : : TREE_OPERAND (arg1, 1));
10077 : 76325 : if (poly_int_tree_p (offset1))
10078 : : {
10079 : 64740 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10080 : 64740 : TYPE_PRECISION (sizetype));
10081 : 64740 : tem <<= LOG2_BITS_PER_UNIT;
10082 : 64740 : tem += bitpos1;
10083 : 64740 : if (tem.to_shwi (&bitpos1))
10084 : 64740 : offset1 = NULL_TREE;
10085 : : }
10086 : : }
10087 : :
10088 : : /* If we have equivalent bases we might be able to simplify. */
10089 : 2172181 : if (indirect_base0 == indirect_base1
10090 : 2899097 : && 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 : 22075 : if ((offset0 == offset1
10096 : 6947 : || (offset0 && offset1
10097 : 2525 : && operand_equal_p (offset0, offset1, 0)))
10098 : 22075 : && (equality_code
10099 : 11399 : || (indirect_base0
10100 : 7561 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10101 : 3838 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10102 : : {
10103 : 15090 : if (!equality_code
10104 : 11361 : && maybe_ne (bitpos0, bitpos1)
10105 : 26430 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10106 : 1928 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10107 : 9869 : 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 : 15090 : switch (code)
10113 : : {
10114 : 56 : case EQ_EXPR:
10115 : 56 : if (known_eq (bitpos0, bitpos1))
10116 : 49689 : return constant_boolean_node (true, type);
10117 : 21 : if (known_ne (bitpos0, bitpos1))
10118 : 21 : return constant_boolean_node (false, type);
10119 : : break;
10120 : 3673 : case NE_EXPR:
10121 : 3673 : if (known_ne (bitpos0, bitpos1))
10122 : 3668 : return constant_boolean_node (true, type);
10123 : 5 : if (known_eq (bitpos0, bitpos1))
10124 : 5 : return constant_boolean_node (false, type);
10125 : : break;
10126 : 2601 : case LT_EXPR:
10127 : 2601 : if (known_lt (bitpos0, bitpos1))
10128 : 2453 : return constant_boolean_node (true, type);
10129 : 148 : if (known_ge (bitpos0, bitpos1))
10130 : 148 : return constant_boolean_node (false, type);
10131 : : break;
10132 : 2284 : case LE_EXPR:
10133 : 2284 : if (known_le (bitpos0, bitpos1))
10134 : 267 : return constant_boolean_node (true, type);
10135 : 2017 : if (known_gt (bitpos0, bitpos1))
10136 : 2017 : return constant_boolean_node (false, type);
10137 : : break;
10138 : 4653 : case GE_EXPR:
10139 : 4653 : if (known_ge (bitpos0, bitpos1))
10140 : 2052 : return constant_boolean_node (true, type);
10141 : 2601 : if (known_lt (bitpos0, bitpos1))
10142 : 2601 : return constant_boolean_node (false, type);
10143 : : break;
10144 : 1823 : case GT_EXPR:
10145 : 1823 : if (known_gt (bitpos0, bitpos1))
10146 : 1758 : return constant_boolean_node (true, type);
10147 : 65 : if (known_le (bitpos0, bitpos1))
10148 : 65 : 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 : 6985 : else if (known_eq (bitpos0, bitpos1)
10161 : 6985 : && (equality_code
10162 : 5175 : || (indirect_base0
10163 : 287 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10164 : 4888 : || 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 : 5285 : if (offset0 == NULL_TREE)
10172 : 0 : offset0 = build_int_cst (ssizetype, 0);
10173 : : else
10174 : 5285 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10175 : 5285 : if (offset1 == NULL_TREE)
10176 : 2792 : offset1 = build_int_cst (ssizetype, 0);
10177 : : else
10178 : 2493 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10179 : :
10180 : 5285 : if (!equality_code
10181 : 5285 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10182 : 0 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10183 : 5175 : 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 : 5285 : 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 : 2150106 : else if (known_eq (bitpos0, bitpos1)
10194 : 49375 : && (indirect_base0
10195 : 806759 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10196 : 10955 : && (indirect_base1
10197 : 132364 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10198 : 2316754 : && ((offset0 == offset1)
10199 : 5355 : || (offset0 && offset1
10200 : 5154 : && operand_equal_p (offset0, offset1, 0))))
10201 : : {
10202 : 29251 : if (indirect_base0)
10203 : 1101 : base0 = build_fold_addr_expr_loc (loc, base0);
10204 : 29251 : if (indirect_base1)
10205 : 2482 : base1 = build_fold_addr_expr_loc (loc, base1);
10206 : 29251 : 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 : 2120855 : else if (((DECL_P (base0)
10216 : 208252 : && 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 : 195301 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10223 : 2093060 : || CONSTANT_CLASS_P (base0))
10224 : 32504 : && 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 : 2150391 : && 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 : 166317207 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10250 : 129003762 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10251 : 31736305 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10252 : 2312034 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10253 : 1910132 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10254 : 1910132 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10255 : 149309465 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10256 : 165696 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10257 : : {
10258 : 165696 : tree const1 = TREE_OPERAND (arg0, 1);
10259 : 165696 : tree const2 = TREE_OPERAND (arg1, 1);
10260 : 165696 : tree variable1 = TREE_OPERAND (arg0, 0);
10261 : 165696 : tree variable2 = TREE_OPERAND (arg1, 0);
10262 : 165696 : tree cst;
10263 : 165696 : 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 : 165697 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10270 : : ? MINUS_EXPR : PLUS_EXPR,
10271 : : const2, const1);
10272 : 165696 : if (!TREE_OVERFLOW (cst)
10273 : 165680 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10274 : 187338 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10275 : : {
10276 : 5509 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10277 : 5509 : return fold_build2_loc (loc, code, type,
10278 : : variable1,
10279 : 5509 : fold_build2_loc (loc, TREE_CODE (arg1),
10280 : 5509 : TREE_TYPE (arg1),
10281 : 5509 : variable2, cst));
10282 : : }
10283 : :
10284 : 160188 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10285 : : ? MINUS_EXPR : PLUS_EXPR,
10286 : : const1, const2);
10287 : 160187 : if (!TREE_OVERFLOW (cst)
10288 : 160171 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10289 : 176320 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10290 : : {
10291 : 16133 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10292 : 16133 : return fold_build2_loc (loc, code, type,
10293 : 16133 : fold_build2_loc (loc, TREE_CODE (arg0),
10294 : 16133 : TREE_TYPE (arg0),
10295 : : variable1, cst),
10296 : 16133 : variable2);
10297 : : }
10298 : : }
10299 : :
10300 : 84617909 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10301 : 84617909 : 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 : 83797347 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10318 : : {
10319 : 50755416 : tree cval1 = 0, cval2 = 0;
10320 : :
10321 : 50755416 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10322 : : /* Don't handle degenerate cases here; they should already
10323 : : have been handled anyway. */
10324 : 599105 : && cval1 != 0 && cval2 != 0
10325 : 597928 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10326 : 597928 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10327 : 597922 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10328 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10329 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10330 : 50755474 : && ! 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 : 11405 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10446 : : {
10447 : 11405 : unsigned HOST_WIDE_INT i, nunits;
10448 : :
10449 : 11405 : if (TREE_CODE (arg) == VECTOR_CST
10450 : 11405 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10451 : : {
10452 : 2579 : for (i = 0; i < nunits; ++i)
10453 : 2030 : elts[i] = VECTOR_CST_ELT (arg, i);
10454 : : }
10455 : 10856 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10456 : : {
10457 : : constructor_elt *elt;
10458 : :
10459 : 35920 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10460 : 30178 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10461 : 5114 : return false;
10462 : : else
10463 : 25064 : elts[i] = elt->value;
10464 : : }
10465 : : else
10466 : : return false;
10467 : 7065 : 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 : 7266 : 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 : 7266 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10484 : 7266 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10485 : :
10486 : 14532 : if (!(pow2p_hwi (sel_npatterns)
10487 : 7266 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10488 : 7266 : && 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 : 7266 : poly_uint64 esel;
10498 : 7266 : 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 : 7266 : if (sel_nelts_per_pattern < 3)
10506 : : return true;
10507 : :
10508 : 4679 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10509 : : {
10510 : 3696 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10511 : 3696 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10512 : 3696 : HOST_WIDE_INT step;
10513 : 3696 : if (!poly_int64 (a2 - a1).is_constant (&step))
10514 : : {
10515 : : if (reason)
10516 : : *reason = "step is not constant";
10517 : 927 : return false;
10518 : : }
10519 : : // FIXME: Punt on step < 0 for now, revisit later.
10520 : 3696 : 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 : 321 : if (reason)
10544 : 0 : *reason = "crossed input vectors";
10545 : 321 : return false;
10546 : : }
10547 : :
10548 : : /* Ensure that the stepped sequence always selects from the same
10549 : : input pattern. */
10550 : 3307 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10551 : 3307 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10552 : :
10553 : 3307 : 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 : 2771 : 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 : 7266 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10596 : : const char **reason = NULL)
10597 : : {
10598 : 7266 : unsigned res_npatterns, res_nelts_per_pattern;
10599 : 7266 : 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 : 7266 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10626 : : {
10627 : 6339 : res_npatterns = sel.encoding ().npatterns ();
10628 : 6339 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10629 : 6339 : if (res_nelts_per_pattern == 3
10630 : 983 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10631 : 6847 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10632 : : res_nelts_per_pattern = 2;
10633 : 6339 : res_nelts = res_npatterns * res_nelts_per_pattern;
10634 : : }
10635 : 927 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10636 : : {
10637 : 927 : res_npatterns = res_nelts;
10638 : 927 : res_nelts_per_pattern = 1;
10639 : : }
10640 : : else
10641 : : return NULL_TREE;
10642 : :
10643 : 7266 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10644 : 46091 : for (unsigned i = 0; i < res_nelts; i++)
10645 : : {
10646 : 38825 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10647 : 38825 : uint64_t q;
10648 : 38825 : poly_uint64 r;
10649 : 38825 : 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 : 38825 : 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 : 38825 : if (!r.is_constant (&index))
10669 : : {
10670 : : if (reason)
10671 : : *reason = "remainder is not constant";
10672 : : return NULL_TREE;
10673 : : }
10674 : :
10675 : 38825 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10676 : 38825 : tree elem = vector_cst_elt (arg, index);
10677 : 38825 : out_elts.quick_push (elem);
10678 : : }
10679 : :
10680 : 7266 : return out_elts.build ();
10681 : 7266 : }
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 : 25363 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10689 : : {
10690 : 25363 : unsigned int i;
10691 : 25363 : unsigned HOST_WIDE_INT nelts;
10692 : :
10693 : 25363 : 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 : 25363 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10698 : 25363 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10699 : : return NULL_TREE;
10700 : :
10701 : 15498 : if (TREE_CODE (arg0) == VECTOR_CST
10702 : 7619 : && TREE_CODE (arg1) == VECTOR_CST)
10703 : 7266 : 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 : 8232 : if (!sel.length ().is_constant (&nelts))
10708 : : return NULL_TREE;
10709 : :
10710 : 8232 : gcc_assert (known_eq (sel.length (),
10711 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))));
10712 : 8232 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10713 : 8232 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10714 : 8232 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10715 : 5114 : return NULL_TREE;
10716 : :
10717 : 3118 : vec<constructor_elt, va_gc> *v;
10718 : 3118 : vec_alloc (v, nelts);
10719 : 16832 : for (i = 0; i < nelts; i++)
10720 : : {
10721 : 13714 : HOST_WIDE_INT index;
10722 : 13714 : if (!sel[i].is_constant (&index))
10723 : : return NULL_TREE;
10724 : 13714 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10725 : : }
10726 : 3118 : 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 : 1219400 : exact_inverse (tree type, tree cst)
10781 : : {
10782 : 1219400 : REAL_VALUE_TYPE r;
10783 : 1219400 : tree unit_type;
10784 : 1219400 : machine_mode mode;
10785 : :
10786 : 1219400 : switch (TREE_CODE (cst))
10787 : : {
10788 : 1218875 : case REAL_CST:
10789 : 1218875 : r = TREE_REAL_CST (cst);
10790 : :
10791 : 1218875 : if (exact_real_inverse (TYPE_MODE (type), &r))
10792 : 346480 : 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 : 115363 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10825 : : {
10826 : 115363 : int tz = wi::ctz (y);
10827 : 115363 : if (tz > 0)
10828 : 8026 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10829 : 107337 : 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 : 145001565 : tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10842 : : {
10843 : 145320780 : tree type = TREE_TYPE (t);
10844 : 145320780 : enum tree_code code;
10845 : :
10846 : : /* Doing something useful for floating point would need more work. */
10847 : 145320780 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10848 : : return false;
10849 : :
10850 : 145217797 : code = TREE_CODE (t);
10851 : 145217797 : switch (TREE_CODE_CLASS (code))
10852 : : {
10853 : 837796 : case tcc_unary:
10854 : 837796 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10855 : 837796 : strict_overflow_p);
10856 : 2877491 : case tcc_binary:
10857 : 2877491 : case tcc_comparison:
10858 : 2877491 : return tree_binary_nonzero_warnv_p (code, type,
10859 : 2877491 : TREE_OPERAND (t, 0),
10860 : 2877491 : TREE_OPERAND (t, 1),
10861 : 2877491 : strict_overflow_p);
10862 : 11827897 : case tcc_constant:
10863 : 11827897 : case tcc_declaration:
10864 : 11827897 : case tcc_reference:
10865 : 11827897 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10866 : :
10867 : 129674613 : default:
10868 : 129674613 : break;
10869 : : }
10870 : :
10871 : 129674613 : switch (code)
10872 : : {
10873 : 597481 : case TRUTH_NOT_EXPR:
10874 : 597481 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10875 : 597481 : strict_overflow_p);
10876 : :
10877 : 78274 : case TRUTH_AND_EXPR:
10878 : 78274 : case TRUTH_OR_EXPR:
10879 : 78274 : case TRUTH_XOR_EXPR:
10880 : 78274 : return tree_binary_nonzero_warnv_p (code, type,
10881 : 78274 : TREE_OPERAND (t, 0),
10882 : 78274 : TREE_OPERAND (t, 1),
10883 : 78274 : strict_overflow_p);
10884 : :
10885 : 126048918 : case COND_EXPR:
10886 : 126048918 : case CONSTRUCTOR:
10887 : 126048918 : case OBJ_TYPE_REF:
10888 : 126048918 : case ADDR_EXPR:
10889 : 126048918 : case WITH_SIZE_EXPR:
10890 : 126048918 : case SSA_NAME:
10891 : 126048918 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10892 : :
10893 : 85744 : case COMPOUND_EXPR:
10894 : 85744 : case MODIFY_EXPR:
10895 : 85744 : case BIND_EXPR:
10896 : 85744 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10897 : 85744 : strict_overflow_p);
10898 : :
10899 : 233471 : case SAVE_EXPR:
10900 : 233471 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10901 : 233471 : strict_overflow_p);
10902 : :
10903 : 2577012 : case CALL_EXPR:
10904 : 2577012 : {
10905 : 2577012 : tree fndecl = get_callee_fndecl (t);
10906 : 2577012 : if (!fndecl) return false;
10907 : 2575290 : if (flag_delete_null_pointer_checks && !flag_check_new
10908 : 2575281 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10909 : 2576027 : && !TREE_NOTHROW (fndecl))
10910 : : return true;
10911 : 2575937 : if (flag_delete_null_pointer_checks
10912 : 5151191 : && lookup_attribute ("returns_nonnull",
10913 : 2575254 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10914 : : return true;
10915 : 2575929 : 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 : 143899875 : tree_expr_nonzero_p (tree t)
10929 : : {
10930 : 143899875 : bool ret, strict_overflow_p;
10931 : :
10932 : 143899875 : strict_overflow_p = false;
10933 : 143899875 : ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10934 : 143899875 : 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 : 143899875 : return ret;
10940 : : }
10941 : :
10942 : : /* Return true if T is known not to be equal to an integer W. */
10943 : :
10944 : : bool
10945 : 98792400 : expr_not_equal_to (tree t, const wide_int &w)
10946 : : {
10947 : 98792400 : int_range_max vr;
10948 : 98792400 : switch (TREE_CODE (t))
10949 : : {
10950 : 1069338 : case INTEGER_CST:
10951 : 1069338 : return wi::to_wide (t) != w;
10952 : :
10953 : 97722001 : case SSA_NAME:
10954 : 97722001 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10955 : : return false;
10956 : :
10957 : 195444002 : get_range_query (cfun)->range_of_expr (vr, t);
10958 : 97722001 : 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 : 97622375 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10963 : 195244374 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10964 : : return true;
10965 : : return false;
10966 : :
10967 : : default:
10968 : : return false;
10969 : : }
10970 : 98792400 : }
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 : 801624143 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10979 : : tree op0, tree op1)
10980 : : {
10981 : 801624143 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10982 : 801624143 : tree arg0, arg1, tem;
10983 : 801624143 : tree t1 = NULL_TREE;
10984 : 801624143 : bool strict_overflow_p;
10985 : 801624143 : unsigned int prec;
10986 : :
10987 : 801624143 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10988 : : && TREE_CODE_LENGTH (code) == 2
10989 : : && op0 != NULL_TREE
10990 : : && op1 != NULL_TREE);
10991 : :
10992 : 801624143 : arg0 = op0;
10993 : 801624143 : 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 : 801624143 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
11009 : : {
11010 : 175372490 : STRIP_SIGN_NOPS (arg0);
11011 : 175372490 : STRIP_SIGN_NOPS (arg1);
11012 : : }
11013 : : else
11014 : : {
11015 : 626251653 : STRIP_NOPS (arg0);
11016 : 626251653 : 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 : 801624143 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11022 : : {
11023 : 196986036 : tem = const_binop (code, type, arg0, arg1);
11024 : 196986036 : if (tem != NULL_TREE)
11025 : : {
11026 : 194318761 : if (TREE_TYPE (tem) != type)
11027 : 1771283 : tem = fold_convert_loc (loc, type, tem);
11028 : 194318761 : 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 : 607305382 : if (commutative_tree_code (code)
11035 : 607305382 : && tree_swap_operands_p (arg0, arg1))
11036 : 32547101 : 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 : 574758281 : if (kind == tcc_comparison
11041 : 574758281 : && tree_swap_operands_p (arg0, arg1))
11042 : 7572012 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11043 : :
11044 : 567186269 : tem = generic_simplify (loc, code, type, op0, op1);
11045 : 567186269 : 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 : 479343060 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11065 : : || code == EQ_EXPR || code == NE_EXPR)
11066 : 54985533 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11067 : 54429500 : && ((truth_value_p (TREE_CODE (arg0))
11068 : 1222407 : && (truth_value_p (TREE_CODE (arg1))
11069 : 917106 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11070 : 40 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11071 : 54124183 : || (truth_value_p (TREE_CODE (arg1))
11072 : 6741 : && (truth_value_p (TREE_CODE (arg0))
11073 : 6741 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11074 : 167 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11075 : : {
11076 : 345022 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11077 : 39691 : : 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 : 305331 : if (code == EQ_EXPR)
11084 : 33603 : tem = invert_truthvalue_loc (loc, tem);
11085 : :
11086 : 305331 : return fold_convert_loc (loc, type, tem);
11087 : : }
11088 : :
11089 : 479037729 : if (TREE_CODE_CLASS (code) == tcc_binary
11090 : 277367387 : || TREE_CODE_CLASS (code) == tcc_comparison)
11091 : : {
11092 : 292409984 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11093 : : {
11094 : 80153 : tem = fold_build2_loc (loc, code, type,
11095 : 80153 : fold_convert_loc (loc, TREE_TYPE (op0),
11096 : 80153 : TREE_OPERAND (arg0, 1)), op1);
11097 : 80153 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11098 : 80153 : tem);
11099 : : }
11100 : 292329831 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11101 : : {
11102 : 3133 : tem = fold_build2_loc (loc, code, type, op0,
11103 : 3133 : fold_convert_loc (loc, TREE_TYPE (op1),
11104 : 3133 : TREE_OPERAND (arg1, 1)));
11105 : 3133 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11106 : 3133 : tem);
11107 : : }
11108 : :
11109 : 292326698 : if (TREE_CODE (arg0) == COND_EXPR
11110 : 291949149 : || TREE_CODE (arg0) == VEC_COND_EXPR
11111 : 291947015 : || COMPARISON_CLASS_P (arg0))
11112 : : {
11113 : 740425 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11114 : : arg0, arg1,
11115 : : /*cond_first_p=*/1);
11116 : 740425 : if (tem != NULL_TREE)
11117 : : return tem;
11118 : : }
11119 : :
11120 : 291839357 : if (TREE_CODE (arg1) == COND_EXPR
11121 : 291609796 : || TREE_CODE (arg1) == VEC_COND_EXPR
11122 : 291609572 : || COMPARISON_CLASS_P (arg1))
11123 : : {
11124 : 240077 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11125 : : arg1, arg0,
11126 : : /*cond_first_p=*/0);
11127 : 240077 : if (tem != NULL_TREE)
11128 : : return tem;
11129 : : }
11130 : : }
11131 : :
11132 : 478458672 : switch (code)
11133 : : {
11134 : 52804345 : case MEM_REF:
11135 : : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11136 : 52804345 : if (TREE_CODE (arg0) == ADDR_EXPR
11137 : 52804345 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11138 : : {
11139 : 891612 : tree iref = TREE_OPERAND (arg0, 0);
11140 : 891612 : 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 : 51912733 : if (TREE_CODE (arg0) == ADDR_EXPR
11148 : 51912733 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11149 : : {
11150 : 2362062 : tree base;
11151 : 2362062 : poly_int64 coffset;
11152 : 2362062 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11153 : : &coffset);
11154 : 2362062 : if (!base)
11155 : : return NULL_TREE;
11156 : 2358498 : 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 : 32052629 : case POINTER_PLUS_EXPR:
11165 : : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11166 : 64104842 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11167 : 64095533 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11168 : 35447 : 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 : 35447 : arg0)));
11174 : :
11175 : : return NULL_TREE;
11176 : :
11177 : 60057407 : case PLUS_EXPR:
11178 : 60057407 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11179 : : {
11180 : : /* X + (X / CST) * -CST is X % CST. */
11181 : 48115243 : if (TREE_CODE (arg1) == MULT_EXPR
11182 : 2262110 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11183 : 48121369 : && operand_equal_p (arg0,
11184 : 6126 : 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 : 60057235 : if ((TREE_CODE (arg0) == MULT_EXPR
11203 : 48755774 : || TREE_CODE (arg1) == MULT_EXPR)
11204 : 12644370 : && !TYPE_SATURATING (type)
11205 : 12644370 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11206 : 12267245 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11207 : 71677113 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11208 : : {
11209 : 8241513 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11210 : 8241513 : if (tem)
11211 : : return tem;
11212 : : }
11213 : :
11214 : 58852420 : 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 : 273374 : if (ANY_INTEGRAL_TYPE_P (type)
11220 : 46912604 : && TYPE_OVERFLOW_WRAPS (type)
11221 : 46912604 : && (((TREE_CODE (arg0) == PLUS_EXPR
11222 : 29365292 : || TREE_CODE (arg0) == MINUS_EXPR)
11223 : 3291176 : && TREE_CODE (arg1) == MULT_EXPR)
11224 : 28869525 : || ((TREE_CODE (arg1) == PLUS_EXPR
11225 : 28869525 : || TREE_CODE (arg1) == MINUS_EXPR)
11226 : 402088 : && TREE_CODE (arg0) == MULT_EXPR)))
11227 : : {
11228 : 543671 : tree parg0, parg1, parg, marg;
11229 : 543671 : enum tree_code pcode;
11230 : :
11231 : 543671 : if (TREE_CODE (arg1) == MULT_EXPR)
11232 : : parg = arg0, marg = arg1;
11233 : : else
11234 : 47904 : parg = arg1, marg = arg0;
11235 : 543671 : pcode = TREE_CODE (parg);
11236 : 543671 : parg0 = TREE_OPERAND (parg, 0);
11237 : 543671 : parg1 = TREE_OPERAND (parg, 1);
11238 : 543671 : STRIP_NOPS (parg0);
11239 : 543671 : STRIP_NOPS (parg1);
11240 : :
11241 : 543671 : if (TREE_CODE (parg0) == MULT_EXPR
11242 : 250494 : && TREE_CODE (parg1) != MULT_EXPR)
11243 : 222209 : 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 : 222209 : fold_convert_loc (loc, type, parg1));
11250 : 321462 : if (TREE_CODE (parg0) != MULT_EXPR
11251 : 293177 : && TREE_CODE (parg1) == MULT_EXPR)
11252 : 99474 : return
11253 : 99474 : 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 : 99474 : 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 : 11939816 : if (!HONOR_SNANS (arg0)
11267 : 11938652 : && !HONOR_SIGNED_ZEROS (arg0)
11268 : 11960859 : && 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 : 11939741 : if (flag_associative_math
11302 : 20946 : && TREE_CODE (arg1) == PLUS_EXPR
11303 : 36 : && 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 : 11939740 : if (flag_associative_math
11319 : 20945 : && TREE_CODE (arg0) == PLUS_EXPR
11320 : 1219 : && TREE_CODE (arg1) != MULT_EXPR)
11321 : : {
11322 : 829 : tree tree00 = TREE_OPERAND (arg0, 0);
11323 : 829 : tree tree01 = TREE_OPERAND (arg0, 1);
11324 : 829 : if (TREE_CODE (tree01) == MULT_EXPR
11325 : 49 : && TREE_CODE (tree00) == MULT_EXPR)
11326 : : {
11327 : 9 : tree tree0;
11328 : 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11329 : 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11330 : : }
11331 : : }
11332 : : }
11333 : :
11334 : 11938911 : 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 : 61021537 : {
11343 : 61021537 : enum tree_code code0, code1;
11344 : 61021537 : tree rtype;
11345 : 61021537 : code0 = TREE_CODE (arg0);
11346 : 61021537 : code1 = TREE_CODE (arg1);
11347 : 53292 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11348 : 61005511 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11349 : 39058 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11350 : 39058 : TREE_OPERAND (arg1, 0), 0)
11351 : 36309 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11352 : 36309 : TYPE_UNSIGNED (rtype))
11353 : : /* Only create rotates in complete modes. Other cases are not
11354 : : expanded properly. */
11355 : 61047676 : && (element_precision (rtype)
11356 : 52278 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11357 : : {
11358 : 26069 : tree tree01, tree11;
11359 : 26069 : tree orig_tree01, orig_tree11;
11360 : 26069 : enum tree_code code01, code11;
11361 : :
11362 : 26069 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11363 : 26069 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11364 : 26069 : STRIP_NOPS (tree01);
11365 : 26069 : STRIP_NOPS (tree11);
11366 : 26069 : code01 = TREE_CODE (tree01);
11367 : 26069 : code11 = TREE_CODE (tree11);
11368 : 26069 : if (code11 != MINUS_EXPR
11369 : 25385 : && (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 : 52138 : if (code01 == INTEGER_CST
11377 : 3152 : && code11 == INTEGER_CST
11378 : 32371 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11379 : 32371 : == element_precision (rtype)))
11380 : : {
11381 : 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11382 : 3011 : rtype, TREE_OPERAND (arg0, 0),
11383 : : code0 == LSHIFT_EXPR
11384 : : ? orig_tree01 : orig_tree11);
11385 : 3011 : 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 : 150727073 : 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 : 150727073 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11439 : 108391415 : && !TYPE_SATURATING (type)
11440 : 259118488 : && !TYPE_OVERFLOW_SANITIZED (type))
11441 : : {
11442 : 108363358 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11443 : 108363358 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11444 : 108363358 : tree atype = type;
11445 : 108363358 : 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 : 108363358 : var0 = split_tree (arg0, type, code,
11453 : : &minus_var0, &con0, &minus_con0,
11454 : : &lit0, &minus_lit0, 0);
11455 : 108363358 : 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 : 108363358 : if (code == MINUS_EXPR)
11461 : 11567033 : 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 : 108363127 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11466 : 215520128 : && !TYPE_OVERFLOW_WRAPS (type))
11467 : : {
11468 : 58451499 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11469 : 57784261 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11470 : 745146 : atype = TREE_TYPE (arg0);
11471 : 56945801 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11472 : 56730452 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11473 : 220540 : atype = TREE_TYPE (arg1);
11474 : 29493078 : 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 : 108363127 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11480 : 215520128 : && !TYPE_OVERFLOW_WRAPS (atype))
11481 : : {
11482 : 28527392 : 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 : 1235 : tree tmp0 = var0 ? var0 : minus_var0;
11488 : 5340648 : tree tmp1 = var1 ? var1 : minus_var1;
11489 : 5340648 : bool one_neg = false;
11490 : :
11491 : 5340648 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11492 : : {
11493 : 1644 : tmp0 = TREE_OPERAND (tmp0, 0);
11494 : 1644 : one_neg = !one_neg;
11495 : : }
11496 : 4805206 : if (CONVERT_EXPR_P (tmp0)
11497 : 551204 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11498 : 5891505 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11499 : 550857 : <= TYPE_PRECISION (atype)))
11500 : 538474 : tmp0 = TREE_OPERAND (tmp0, 0);
11501 : 5340648 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11502 : : {
11503 : 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11504 : 170 : one_neg = !one_neg;
11505 : : }
11506 : 5031521 : if (CONVERT_EXPR_P (tmp1)
11507 : 332298 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11508 : 5672836 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11509 : 332188 : <= TYPE_PRECISION (atype)))
11510 : 322315 : tmp1 = TREE_OPERAND (tmp1, 0);
11511 : : /* The only case we can still associate with two variables
11512 : : is if they cancel out. */
11513 : 5340648 : if (!one_neg
11514 : 5340648 : || !operand_equal_p (tmp0, tmp1, 0))
11515 : : ok = false;
11516 : : }
11517 : 22790978 : else if ((var0 && minus_var1
11518 : 3764145 : && ! operand_equal_p (var0, minus_var1, 0))
11519 : 42213578 : || (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 : 99247330 : && ((var0 != 0) + (var1 != 0)
11528 : 99247330 : + (minus_var0 != 0) + (minus_var1 != 0)
11529 : 99247330 : + (con0 != 0) + (con1 != 0)
11530 : 99247330 : + (minus_con0 != 0) + (minus_con1 != 0)
11531 : 99247330 : + (lit0 != 0) + (lit1 != 0)
11532 : 99247330 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11533 : : {
11534 : 1954528 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11535 : 3909056 : int minus_var0_origin
11536 : 1954528 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11537 : 1954528 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11538 : 3909056 : int minus_con0_origin
11539 : 1954528 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11540 : 1954528 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11541 : 3909056 : int minus_lit0_origin
11542 : 1954528 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11543 : 1954528 : var0 = associate_trees (loc, var0, var1, code, atype);
11544 : 1954528 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11545 : : code, atype);
11546 : 1954528 : con0 = associate_trees (loc, con0, con1, code, atype);
11547 : 1954528 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11548 : : code, atype);
11549 : 1954528 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11550 : 1954528 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11551 : : code, atype);
11552 : :
11553 : 1954528 : if (minus_var0 && var0)
11554 : : {
11555 : 1282778 : var0_origin |= minus_var0_origin;
11556 : 1282778 : var0 = associate_trees (loc, var0, minus_var0,
11557 : : MINUS_EXPR, atype);
11558 : 1282778 : minus_var0 = 0;
11559 : 1282778 : minus_var0_origin = 0;
11560 : : }
11561 : 1954528 : if (minus_con0 && con0)
11562 : : {
11563 : 3584 : con0_origin |= minus_con0_origin;
11564 : 3584 : con0 = associate_trees (loc, con0, minus_con0,
11565 : : MINUS_EXPR, atype);
11566 : 3584 : minus_con0 = 0;
11567 : 3584 : 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 : 1954528 : if (minus_lit0 && lit0)
11576 : : {
11577 : 218592 : if (TREE_CODE (lit0) == INTEGER_CST
11578 : 218592 : && TREE_CODE (minus_lit0) == INTEGER_CST
11579 : 218592 : && tree_int_cst_lt (lit0, minus_lit0)
11580 : : /* But avoid ending up with only negated parts. */
11581 : 274952 : && (var0 || con0))
11582 : : {
11583 : 51643 : minus_lit0_origin |= lit0_origin;
11584 : 51643 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11585 : : MINUS_EXPR, atype);
11586 : 51643 : lit0 = 0;
11587 : 51643 : lit0_origin = 0;
11588 : : }
11589 : : else
11590 : : {
11591 : 166949 : lit0_origin |= minus_lit0_origin;
11592 : 166949 : lit0 = associate_trees (loc, lit0, minus_lit0,
11593 : : MINUS_EXPR, atype);
11594 : 166949 : minus_lit0 = 0;
11595 : 166949 : minus_lit0_origin = 0;
11596 : : }
11597 : : }
11598 : :
11599 : : /* Don't introduce overflows through reassociation. */
11600 : 1301923 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11601 : 3256410 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11602 : 1954528 : return NULL_TREE;
11603 : :
11604 : : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11605 : 1954487 : con0_origin |= lit0_origin;
11606 : 1954487 : con0 = associate_trees (loc, con0, lit0, code, atype);
11607 : 1954487 : minus_con0_origin |= minus_lit0_origin;
11608 : 1954487 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11609 : : code, atype);
11610 : :
11611 : : /* Eliminate minus_con0. */
11612 : 1954487 : if (minus_con0)
11613 : : {
11614 : 650038 : if (con0)
11615 : : {
11616 : 8447 : con0_origin |= minus_con0_origin;
11617 : 8447 : con0 = associate_trees (loc, con0, minus_con0,
11618 : : MINUS_EXPR, atype);
11619 : : }
11620 : 641591 : else if (var0)
11621 : : {
11622 : 641591 : var0_origin |= minus_con0_origin;
11623 : 641591 : 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 : 1954487 : if (minus_var0)
11632 : : {
11633 : 346882 : if (con0)
11634 : : {
11635 : 346882 : con0_origin |= minus_var0_origin;
11636 : 346882 : 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 : 1954487 : if (var0_origin != 3 && con0_origin != 3)
11648 : : return NULL_TREE;
11649 : :
11650 : 1952858 : return
11651 : 1952858 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11652 : 1952858 : code, atype));
11653 : : }
11654 : : }
11655 : :
11656 : : return NULL_TREE;
11657 : :
11658 : 22416487 : case POINTER_DIFF_EXPR:
11659 : 22416487 : case MINUS_EXPR:
11660 : : /* Fold &a[i] - &a[j] to i-j. */
11661 : 22416487 : if (TREE_CODE (arg0) == ADDR_EXPR
11662 : 52698 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11663 : 5959 : && TREE_CODE (arg1) == ADDR_EXPR
11664 : 22417037 : && 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 : 22416473 : 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 : 19767853 : if (TREE_CODE (arg0) == NEGATE_EXPR
11681 : 149826 : && 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 : 19768670 : && !(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 : 19767043 : if (!HONOR_SNANS (arg0)
11698 : 19766179 : && !HONOR_SIGNED_ZEROS (arg0)
11699 : 32100799 : && 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 : 19767019 : if (negate_expr_p (op1)
11733 : 711036 : && ! TYPE_OVERFLOW_SANITIZED (type)
11734 : 20475581 : && ((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 : 708497 : || INTEGRAL_TYPE_P (type)))
11739 : 708381 : return fold_build2_loc (loc, PLUS_EXPR, type,
11740 : : fold_convert_loc (loc, type, arg0),
11741 : 708381 : 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 : 19058638 : if ((TREE_CODE (arg0) == MULT_EXPR
11748 : 17733090 : || TREE_CODE (arg1) == MULT_EXPR)
11749 : 2708713 : && !TYPE_SATURATING (type)
11750 : 2708713 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11751 : 2576680 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11752 : 21582369 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11753 : : {
11754 : 350181 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11755 : 350181 : if (tem)
11756 : : return tem;
11757 : : }
11758 : :
11759 : 19007864 : goto associate;
11760 : :
11761 : 64783172 : case MULT_EXPR:
11762 : 64783172 : if (! FLOAT_TYPE_P (type))
11763 : : {
11764 : : /* Transform x * -C into -x * C if x is easily negatable. */
11765 : 41769827 : if (TREE_CODE (op1) == INTEGER_CST
11766 : 39029148 : && tree_int_cst_sgn (op1) == -1
11767 : 212586 : && negate_expr_p (op0)
11768 : 336 : && negate_expr_p (op1)
11769 : 320 : && (tem = negate_expr (op1)) != op1
11770 : 41770147 : && ! 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 : 41769507 : strict_overflow_p = false;
11776 : 41769507 : if (TREE_CODE (arg1) == INTEGER_CST
11777 : 41769507 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11778 : : &strict_overflow_p)) != 0)
11779 : : {
11780 : 510388 : 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 : 510388 : return fold_convert_loc (loc, type, tem);
11786 : : }
11787 : :
11788 : : /* Optimize z * conj(z) for integer complex numbers. */
11789 : 41259119 : if (TREE_CODE (arg0) == CONJ_EXPR
11790 : 41259119 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11791 : 1 : return fold_mult_zconjz (loc, type, arg1);
11792 : 41259118 : if (TREE_CODE (arg1) == CONJ_EXPR
11793 : 41259118 : && 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 : 23013345 : if (!HONOR_NANS (arg0)
11802 : 32738 : && !HONOR_SIGNED_ZEROS (arg0)
11803 : 32438 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11804 : 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11805 : 23013570 : && 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 : 23013127 : if (flag_unsafe_math_optimizations
11838 : 32267 : && TREE_CODE (arg0) == CONJ_EXPR
11839 : 23013129 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11840 : 1 : return fold_mult_zconjz (loc, type, arg1);
11841 : 23013126 : if (flag_unsafe_math_optimizations
11842 : 32266 : && TREE_CODE (arg1) == CONJ_EXPR
11843 : 23013130 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11844 : 0 : return fold_mult_zconjz (loc, type, arg0);
11845 : : }
11846 : 64272244 : goto associate;
11847 : :
11848 : 1810837 : case BIT_IOR_EXPR:
11849 : : /* Canonicalize (X & C1) | C2. */
11850 : 1810837 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11851 : 130113 : && TREE_CODE (arg1) == INTEGER_CST
11852 : 1896019 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11853 : : {
11854 : 85174 : int width = TYPE_PRECISION (type), w;
11855 : 85174 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11856 : 85174 : wide_int c2 = wi::to_wide (arg1);
11857 : :
11858 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11859 : 85174 : if ((c1 & c2) == c1)
11860 : 0 : return omit_one_operand_loc (loc, type, arg1,
11861 : 0 : TREE_OPERAND (arg0, 0));
11862 : :
11863 : 85174 : wide_int msk = wi::mask (width, false,
11864 : 85174 : TYPE_PRECISION (TREE_TYPE (arg1)));
11865 : :
11866 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11867 : 85174 : 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 : 85168 : c1 &= msk;
11877 : 85168 : c2 &= msk;
11878 : 85168 : wide_int c3 = wi::bit_and_not (c1, c2);
11879 : 263699 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11880 : : {
11881 : 178773 : wide_int mask = wi::mask (w, false,
11882 : 178773 : TYPE_PRECISION (type));
11883 : 357546 : if (((c1 | c2) & mask) == mask
11884 : 357546 : && wi::bit_and_not (c1, mask) == 0)
11885 : : {
11886 : 242 : c3 = mask;
11887 : 242 : break;
11888 : : }
11889 : 178773 : }
11890 : :
11891 : 85168 : 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 : 86304 : }
11899 : :
11900 : : /* See if this can be simplified into a rotate first. If that
11901 : : is unsuccessful continue in the association code. */
11902 : 1810269 : goto bit_rotate;
11903 : :
11904 : 680616 : case BIT_XOR_EXPR:
11905 : : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11906 : 680616 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11907 : 2420 : && INTEGRAL_TYPE_P (type)
11908 : 1815 : && integer_onep (TREE_OPERAND (arg0, 1))
11909 : 680619 : && 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 : 680616 : goto bit_rotate;
11916 : :
11917 : 6020701 : case BIT_AND_EXPR:
11918 : : /* Fold !X & 1 as X == 0. */
11919 : 6020701 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11920 : 6020701 : && 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 : 6020701 : if (TREE_CODE (arg1) == INTEGER_CST)
11930 : : {
11931 : 4370250 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11932 : 4370250 : wide_int ncst1 = -cst1;
11933 : 4370250 : if ((cst1 & ncst1) == ncst1
11934 : 4538286 : && multiple_of_p (type, arg0,
11935 : 4538286 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11936 : 466 : return fold_convert_loc (loc, type, arg0);
11937 : 4370250 : }
11938 : :
11939 : : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11940 : : bits from CST2. */
11941 : 6020235 : if (TREE_CODE (arg1) == INTEGER_CST
11942 : 4369784 : && TREE_CODE (arg0) == MULT_EXPR
11943 : 6135628 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11944 : : {
11945 : 115363 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11946 : 115363 : wide_int masked
11947 : 115363 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11948 : :
11949 : 115363 : if (masked == 0)
11950 : 6662 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11951 : 6662 : arg0, arg1);
11952 : 108701 : 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 : 115363 : }
11964 : :
11965 : : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11966 : 4362493 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11967 : 6285943 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11968 : : {
11969 : 185219 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11970 : :
11971 : 185219 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11972 : 185219 : if (mask == -1)
11973 : 2491 : return
11974 : 2491 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11975 : 185219 : }
11976 : :
11977 : 6010453 : goto associate;
11978 : :
11979 : 6385100 : case RDIV_EXPR:
11980 : : /* Don't touch a floating-point divide by zero unless the mode
11981 : : of the constant can represent infinity. */
11982 : 6385100 : if (TREE_CODE (arg1) == REAL_CST
11983 : 3221241 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11984 : 6385100 : && real_zerop (arg1))
11985 : 0 : return NULL_TREE;
11986 : :
11987 : : /* (-A) / (-B) -> A / B */
11988 : 6385100 : 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 : 6385097 : 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 : 2139571 : case TRUNC_DIV_EXPR:
11999 : : /* Fall through */
12000 : :
12001 : 2139571 : 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 : 2139571 : strict_overflow_p = false;
12005 : 2139571 : if (TREE_CODE (arg1) == LSHIFT_EXPR
12006 : 2139571 : && (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 : 3385129 : case ROUND_DIV_EXPR:
12031 : 3385129 : case CEIL_DIV_EXPR:
12032 : 3385129 : case EXACT_DIV_EXPR:
12033 : 3385129 : 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 : 3382113 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12039 : 842947 : && TREE_CODE (op0) == NEGATE_EXPR
12040 : 3382175 : && 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 : 3382083 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12053 : 842917 : && TREE_CODE (arg1) == NEGATE_EXPR
12054 : 3382327 : && 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 : 3382047 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12074 : 3382047 : && 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 : 3382047 : strict_overflow_p = false;
12080 : 3382047 : if (TREE_CODE (arg1) == INTEGER_CST
12081 : 3382047 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12082 : : &strict_overflow_p)) != 0)
12083 : : {
12084 : 2520 : 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 : 2520 : return fold_convert_loc (loc, type, tem);
12089 : : }
12090 : :
12091 : : return NULL_TREE;
12092 : :
12093 : 621101 : case CEIL_MOD_EXPR:
12094 : 621101 : case FLOOR_MOD_EXPR:
12095 : 621101 : case ROUND_MOD_EXPR:
12096 : 621101 : case TRUNC_MOD_EXPR:
12097 : 621101 : strict_overflow_p = false;
12098 : 621101 : if (TREE_CODE (arg1) == INTEGER_CST
12099 : 621101 : && (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 : 2041140 : case LROTATE_EXPR:
12112 : 2041140 : case RROTATE_EXPR:
12113 : 2041140 : case RSHIFT_EXPR:
12114 : 2041140 : case LSHIFT_EXPR:
12115 : : /* Since negative shift count is not well-defined,
12116 : : don't try to compute it in the compiler. */
12117 : 2041140 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12118 : : return NULL_TREE;
12119 : :
12120 : 2040079 : 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 : 2643 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12126 : 2109 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12127 : 2109 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12128 : 2109 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12129 : 2040079 : && 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 : 438893 : case MIN_EXPR:
12143 : 438893 : case MAX_EXPR:
12144 : 438893 : goto associate;
12145 : :
12146 : 5727595 : 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 : 5727595 : if (integer_zerop (arg0))
12152 : 1162605 : return fold_convert_loc (loc, type, arg0);
12153 : : /* FALLTHRU */
12154 : 15690793 : case TRUTH_AND_EXPR:
12155 : : /* If either arg is constant true, drop it. */
12156 : 15690793 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12157 : 1987904 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12158 : 844309 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12159 : : /* Preserve sequence points. */
12160 : 14500316 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12161 : 769311 : 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 : 12933578 : if (integer_zerop (arg1))
12165 : 46882 : 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 : 12886696 : if (integer_zerop (arg0))
12169 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12170 : :
12171 : : /* !X && X is always false. */
12172 : 12886696 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12173 : 12886696 : && 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 : 12886696 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12177 : 12886696 : && 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 : 12886696 : if (!TREE_SIDE_EFFECTS (arg0)
12185 : 12886696 : && !TREE_SIDE_EFFECTS (arg1))
12186 : : {
12187 : 11882043 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12188 : 11882043 : 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 : 11881574 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12194 : 11881574 : if (tem && !operand_equal_p (tem, arg1, 0))
12195 : 9966 : return fold_convert (type,
12196 : : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12197 : : arg0, tem));
12198 : : }
12199 : :
12200 : 12876261 : 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 : 3142451 : 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 : 3142451 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12212 : 126967 : return fold_convert_loc (loc, type, arg0);
12213 : : /* FALLTHRU */
12214 : 12842098 : case TRUTH_OR_EXPR:
12215 : : /* If either arg is constant zero, drop it. */
12216 : 12842098 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12217 : 153257 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12218 : 487913 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12219 : : /* Preserve sequence points. */
12220 : 13125048 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12221 : 425205 : 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 : 12263636 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12225 : 51706 : 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 : 12211930 : 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 : 12211930 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12233 : 12211930 : && 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 : 12211930 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12237 : 12211930 : && 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 : 12211929 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12242 : 1636 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12243 : : {
12244 : 673 : tree a0, a1, l0, l1, n0, n1;
12245 : :
12246 : 673 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12247 : 673 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12248 : :
12249 : 673 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12250 : 673 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12251 : :
12252 : 673 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12253 : 673 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12254 : :
12255 : 673 : if ((operand_equal_p (n0, a0, 0)
12256 : 18 : && operand_equal_p (n1, a1, 0))
12257 : 681 : || (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 : 12211916 : 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 : 39199 : case TRUTH_XOR_EXPR:
12269 : : /* If the second arg is constant zero, drop it. */
12270 : 39199 : 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 : 39199 : 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 : 39199 : 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 : 39199 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12284 : 39199 : && 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 : 39199 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12289 : 39199 : && 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 : 46413400 : case EQ_EXPR:
12295 : 46413400 : case NE_EXPR:
12296 : 46413400 : STRIP_NOPS (arg0);
12297 : 46413400 : STRIP_NOPS (arg1);
12298 : :
12299 : 46413400 : tem = fold_comparison (loc, code, type, op0, op1);
12300 : 46413400 : if (tem != NULL_TREE)
12301 : : return tem;
12302 : :
12303 : : /* bool_var != 1 becomes !bool_var. */
12304 : 47539777 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12305 : 46450959 : && code == NE_EXPR)
12306 : 39070 : return fold_convert_loc (loc, type,
12307 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12308 : 78140 : TREE_TYPE (arg0), arg0));
12309 : :
12310 : : /* bool_var == 0 becomes !bool_var. */
12311 : 47461637 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12312 : 47281764 : && code == EQ_EXPR)
12313 : 206019 : return fold_convert_loc (loc, type,
12314 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12315 : 412038 : TREE_TYPE (arg0), arg0));
12316 : :
12317 : : /* !exp != 0 becomes !exp */
12318 : 598035 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12319 : 46758326 : && code == NE_EXPR)
12320 : 590337 : 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 : 45570669 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12328 : 45570669 : && integer_zerop (arg1))
12329 : : {
12330 : 1488849 : tree arg00 = TREE_OPERAND (arg0, 0);
12331 : 1488849 : tree arg01 = TREE_OPERAND (arg0, 1);
12332 : 1488849 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12333 : 1488849 : && integer_onep (TREE_OPERAND (arg00, 0)))
12334 : : {
12335 : 4593 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12336 : 4593 : arg01, TREE_OPERAND (arg00, 1));
12337 : 4593 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12338 : 4593 : build_one_cst (TREE_TYPE (arg0)));
12339 : 4593 : return fold_build2_loc (loc, code, type,
12340 : 4593 : fold_convert_loc (loc, TREE_TYPE (arg1),
12341 : 4593 : tem), arg1);
12342 : : }
12343 : 1484256 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12344 : 1484256 : && integer_onep (TREE_OPERAND (arg01, 0)))
12345 : : {
12346 : 304 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12347 : 304 : arg00, TREE_OPERAND (arg01, 1));
12348 : 304 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12349 : 304 : build_one_cst (TREE_TYPE (arg0)));
12350 : 304 : return fold_build2_loc (loc, code, type,
12351 : 304 : fold_convert_loc (loc, TREE_TYPE (arg1),
12352 : 304 : tem), arg1);
12353 : : }
12354 : : }
12355 : :
12356 : : /* If this is a comparison of a field, we may be able to simplify it. */
12357 : 45565772 : if ((TREE_CODE (arg0) == COMPONENT_REF
12358 : 45565772 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12359 : : /* Handle the constant case even without -O
12360 : : to make sure the warnings are given. */
12361 : 4419599 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12362 : : {
12363 : 4130721 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12364 : 4130721 : 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 : 44806451 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12375 : : {
12376 : 2535079 : tree fndecl = get_callee_fndecl (arg0);
12377 : :
12378 : 2535079 : if (fndecl
12379 : 2534189 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12380 : 537 : && call_expr_nargs (arg0) == 1
12381 : 2535616 : && (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 : 44805914 : if (TREE_CODE (arg0) == RSHIFT_EXPR
12398 : 38562 : && integer_zerop (arg1)
12399 : 44818061 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12400 : : {
12401 : 10077 : tree arg00 = TREE_OPERAND (arg0, 0);
12402 : 10077 : tree arg01 = TREE_OPERAND (arg0, 1);
12403 : 10077 : tree itype = TREE_TYPE (arg00);
12404 : 10077 : if (wi::to_wide (arg01) == element_precision (itype) - 1)
12405 : : {
12406 : 934 : if (TYPE_UNSIGNED (itype))
12407 : : {
12408 : 853 : itype = signed_type_for (itype);
12409 : 853 : arg00 = fold_convert_loc (loc, itype, arg00);
12410 : : }
12411 : 1813 : return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12412 : 934 : 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 : 44804980 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12419 : 1685792 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12420 : 963 : && integer_zerop (arg1)
12421 : 44805545 : && 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 : 44804732 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12435 : 4671 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12436 : 0 : && integer_zerop (arg1)
12437 : 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12438 : 44804732 : && 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 : 44804732 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12449 : 1685544 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12450 : 11706 : && integer_zerop (arg1)
12451 : 11706 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12452 : 44813783 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12453 : 9051 : 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 : 44804732 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12463 : 4671 : && 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 : 44804716 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12507 : 44802189 : || 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 : 38275840 : case LT_EXPR:
12578 : 38275840 : case GT_EXPR:
12579 : 38275840 : case LE_EXPR:
12580 : 38275840 : case GE_EXPR:
12581 : 38275840 : tem = fold_comparison (loc, code, type, op0, op1);
12582 : 38275840 : if (tem != NULL_TREE)
12583 : : return tem;
12584 : :
12585 : : /* Transform comparisons of the form X +- C CMP X. */
12586 : 37391213 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12587 : 4649498 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12588 : 50070 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12589 : 37391231 : && !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 : 37391201 : if (code == LE_EXPR
12628 : 7110731 : && TREE_CODE (arg1) == INTEGER_CST
12629 : 4979989 : && TREE_CODE (arg0) == ABS_EXPR
12630 : 871 : && ! TREE_SIDE_EFFECTS (arg0)
12631 : 871 : && (tem = negate_expr (arg1)) != 0
12632 : 871 : && TREE_CODE (tem) == INTEGER_CST
12633 : 37392072 : && !TREE_OVERFLOW (tem))
12634 : 1742 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12635 : : build2 (GE_EXPR, type,
12636 : 871 : TREE_OPERAND (arg0, 0), tem),
12637 : : build2 (LE_EXPR, type,
12638 : 1742 : TREE_OPERAND (arg0, 0), arg1));
12639 : :
12640 : : /* Convert ABS_EXPR<x> >= 0 to true. */
12641 : 37390330 : strict_overflow_p = false;
12642 : 37390330 : if (code == GE_EXPR
12643 : 3968924 : && (integer_zerop (arg1)
12644 : 3057752 : || (! HONOR_NANS (arg0)
12645 : 2427405 : && real_zerop (arg1)))
12646 : 38301715 : && 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 : 37389156 : strict_overflow_p = false;
12660 : 37389156 : if (code == LT_EXPR
12661 : 12559918 : && (integer_zerop (arg1) || real_zerop (arg1))
12662 : 40350333 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12663 : : {
12664 : 3095 : 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 : 3095 : return omit_one_operand_loc (loc, type,
12670 : : constant_boolean_node (false, type),
12671 : 3095 : arg0);
12672 : : }
12673 : :
12674 : : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12675 : : and similarly for >= into !=. */
12676 : 37386061 : if ((code == LT_EXPR || code == GE_EXPR)
12677 : 16524573 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12678 : 5077907 : && TREE_CODE (arg1) == LSHIFT_EXPR
12679 : 37387586 : && 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 : 37384700 : if ((code == LT_EXPR || code == GE_EXPR)
12693 : 16523212 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12694 : 5593941 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12695 : 10951211 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12696 : 3761854 : && CONVERT_EXPR_P (arg1)
12697 : 1088969 : && 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 : 37384707 : && 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 : 5609598 : case UNORDERED_EXPR:
12715 : 5609598 : case ORDERED_EXPR:
12716 : 5609598 : case UNLT_EXPR:
12717 : 5609598 : case UNLE_EXPR:
12718 : 5609598 : case UNGT_EXPR:
12719 : 5609598 : case UNGE_EXPR:
12720 : 5609598 : case UNEQ_EXPR:
12721 : 5609598 : case LTGT_EXPR:
12722 : : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12723 : 5609598 : {
12724 : 5609598 : tree targ0 = strip_float_extensions (arg0);
12725 : 5609598 : tree targ1 = strip_float_extensions (arg1);
12726 : 5609598 : tree newtype = TREE_TYPE (targ0);
12727 : :
12728 : 5609598 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12729 : 1289 : newtype = TREE_TYPE (targ1);
12730 : :
12731 : 5609598 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12732 : 5609598 : && (!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 : 6167302 : case COMPOUND_EXPR:
12741 : : /* When pedantic, a compound expression can be neither an lvalue
12742 : : nor an integer constant expression. */
12743 : 6167302 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12744 : : return NULL_TREE;
12745 : : /* Don't let (0, 0) be null pointer constant. */
12746 : 445614 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12747 : 445614 : : 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 : 1247680 : 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 : 1247680 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12778 : 1247680 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12779 : 1247680 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12780 : 2495360 : if (~cst1 == 0
12781 : 3738678 : || (cst1 & (cst1 + 1)) != 0
12782 : 1077437 : || !INTEGRAL_TYPE_P (type)
12783 : 1077437 : || (!TYPE_OVERFLOW_WRAPS (type)
12784 : 39800 : && TREE_CODE (type) != INTEGER_TYPE)
12785 : 4648053 : || (wi::max_value (type) & cst1) != cst1)
12786 : : return NULL_TREE;
12787 : :
12788 : 1077437 : enum tree_code codes[2] = { code00, code01 };
12789 : 1077437 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12790 : 1077437 : int which = 0;
12791 : 1077437 : 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 : 1077437 : pmop[0] = arg00;
12797 : 1077437 : pmop[1] = arg01;
12798 : 1077437 : which = code != NEGATE_EXPR;
12799 : :
12800 : 3231415 : for (; which >= 0; which--)
12801 : 2153978 : switch (codes[which])
12802 : : {
12803 : 20645 : case BIT_AND_EXPR:
12804 : 20645 : case BIT_IOR_EXPR:
12805 : 20645 : case BIT_XOR_EXPR:
12806 : 20645 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12807 : 20645 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12808 : 20645 : if (codes[which] == BIT_AND_EXPR)
12809 : : {
12810 : 20531 : 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 : 19104 : pmop[which] = arg0xx[2 * which];
12819 : 19104 : break;
12820 : 2133333 : case ERROR_MARK:
12821 : 2133333 : 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 : 887496 : if ((code == PLUS_EXPR
12826 : 211581 : || (code == MINUS_EXPR && which == 0))
12827 : 655329 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12828 : 136879 : pmop[which] = build_int_cst (type, 0);
12829 : : /* Similarly, with C - N where (-N & M) == 0. */
12830 : 887496 : if (code == MINUS_EXPR
12831 : 443748 : && which == 1
12832 : 648083 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12833 : 194853 : 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 : 1077437 : if (pmop[0] == arg00 && pmop[1] == arg01)
12841 : : return NULL_TREE;
12842 : :
12843 : 350129 : if (TYPE_OVERFLOW_WRAPS (type))
12844 : : return type;
12845 : : else
12846 : 2258 : return unsigned_type_for (type);
12847 : 1077437 : }
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 : 4166329 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12863 : : {
12864 : 4166329 : contains_label_data *d = (contains_label_data *) data;
12865 : 4166329 : 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 : 5979 : case GOTO_EXPR:
12889 : 5979 : *walk_subtrees = 0;
12890 : 5979 : 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 : 304033 : contains_label_p (tree st)
12902 : : {
12903 : 304033 : hash_set<tree> pset;
12904 : 304033 : contains_label_data data = { &pset, false };
12905 : 304033 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12906 : 304033 : }
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 : 27980102 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12914 : : tree op0, tree op1, tree op2)
12915 : : {
12916 : 27980102 : tree tem;
12917 : 27980102 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12918 : 27980102 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12919 : :
12920 : 27980102 : 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 : 27980102 : if (commutative_ternary_tree_code (code)
12926 : 27980102 : && tree_swap_operands_p (op0, op1))
12927 : 23 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12928 : :
12929 : 27980079 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12930 : 27980079 : 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 : 27020093 : if (op0)
12944 : : {
12945 : 26955677 : arg0 = op0;
12946 : 26955677 : STRIP_NOPS (arg0);
12947 : : }
12948 : :
12949 : 27020093 : if (op1)
12950 : : {
12951 : 27020093 : arg1 = op1;
12952 : 27020093 : STRIP_NOPS (arg1);
12953 : : }
12954 : :
12955 : 27020093 : if (op2)
12956 : : {
12957 : 12981832 : arg2 = op2;
12958 : 12981832 : STRIP_NOPS (arg2);
12959 : : }
12960 : :
12961 : 27020093 : switch (code)
12962 : : {
12963 : 14037790 : case COMPONENT_REF:
12964 : 14037790 : if (TREE_CODE (arg0) == CONSTRUCTOR
12965 : 14037790 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12966 : : {
12967 : : unsigned HOST_WIDE_INT idx;
12968 : : tree field, value;
12969 : 870 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12970 : 672 : if (field == arg1)
12971 : : return value;
12972 : : }
12973 : : return NULL_TREE;
12974 : :
12975 : 10898783 : case COND_EXPR:
12976 : 10898783 : 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 : 10898783 : if (TREE_CODE (arg0) == INTEGER_CST)
12980 : : {
12981 : 437473 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12982 : 437473 : 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 : 437473 : if ((!TREE_SIDE_EFFECTS (unused_op)
12988 : 304033 : || !contains_label_p (unused_op))
12989 : 737156 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12990 : 356239 : || VOID_TYPE_P (type)))
12991 : 423919 : return protected_set_expr_location_unshare (tem, loc);
12992 : 13554 : return NULL_TREE;
12993 : : }
12994 : 10461310 : else if (TREE_CODE (arg0) == VECTOR_CST)
12995 : : {
12996 : 1540 : unsigned HOST_WIDE_INT nelts;
12997 : 1540 : if ((TREE_CODE (arg1) == VECTOR_CST
12998 : 267 : || TREE_CODE (arg1) == CONSTRUCTOR)
12999 : 1273 : && (TREE_CODE (arg2) == VECTOR_CST
13000 : 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
13001 : 3080 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
13002 : : {
13003 : 1273 : vec_perm_builder sel (nelts, nelts, 1);
13004 : 13673 : for (unsigned int i = 0; i < nelts; i++)
13005 : : {
13006 : 12400 : tree val = VECTOR_CST_ELT (arg0, i);
13007 : 12400 : if (integer_all_onesp (val))
13008 : 6675 : sel.quick_push (i);
13009 : 5725 : else if (integer_zerop (val))
13010 : 5725 : sel.quick_push (nelts + i);
13011 : : else /* Currently unreachable. */
13012 : 1241 : return NULL_TREE;
13013 : : }
13014 : 1273 : vec_perm_indices indices (sel, 2, nelts);
13015 : 1273 : tree t = fold_vec_perm (type, arg1, arg2, indices);
13016 : 1273 : if (t != NULL_TREE)
13017 : 1241 : return t;
13018 : 2514 : }
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 : 10460069 : if (COMPARISON_CLASS_P (arg0)
13028 : 8772679 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
13029 : 10583201 : && !HONOR_SIGNED_ZEROS (op1))
13030 : : {
13031 : 112749 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
13032 : 112749 : TREE_OPERAND (arg0, 0),
13033 : 112749 : TREE_OPERAND (arg0, 1),
13034 : : op1, op2);
13035 : 112749 : if (tem)
13036 : : return tem;
13037 : : }
13038 : :
13039 : 10453584 : if (COMPARISON_CLASS_P (arg0)
13040 : 8766194 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
13041 : 10885169 : && !HONOR_SIGNED_ZEROS (op2))
13042 : : {
13043 : 339576 : enum tree_code comp_code = TREE_CODE (arg0);
13044 : 339576 : tree arg00 = TREE_OPERAND (arg0, 0);
13045 : 339576 : tree arg01 = TREE_OPERAND (arg0, 1);
13046 : 339576 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
13047 : 339576 : if (comp_code != ERROR_MARK)
13048 : 339576 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
13049 : : arg00,
13050 : : arg01,
13051 : : op2, op1);
13052 : 339576 : 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 : 10208007 : if (truth_value_p (TREE_CODE (arg0))
13059 : 10208007 : && tree_swap_operands_p (op1, op2))
13060 : : {
13061 : 1746061 : 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 : 1746061 : tem = fold_invert_truthvalue (loc0, arg0);
13066 : 1746061 : if (tem)
13067 : 1165616 : return fold_build3_loc (loc, code, type, tem, op2, op1);
13068 : : }
13069 : :
13070 : : /* Convert A ? 1 : 0 to simply A. */
13071 : 9042391 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13072 : 8733369 : : (integer_onep (op1)
13073 : 393669 : && !VECTOR_TYPE_P (type)))
13074 : 590012 : && 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 : 9929688 : && type == TREE_TYPE (arg0))
13080 : 32039 : 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 : 9010352 : if (integer_zerop (op1)
13085 : 256719 : && code == COND_EXPR
13086 : 254897 : && integer_onep (op2)
13087 : 33262 : && !VECTOR_TYPE_P (type)
13088 : 9043614 : && truth_value_p (TREE_CODE (arg0)))
13089 : 31436 : return fold_convert_loc (loc, type,
13090 : 31436 : invert_truthvalue_loc (loc, arg0));
13091 : :
13092 : : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13093 : 8978916 : if (TREE_CODE (arg0) == LT_EXPR
13094 : 996101 : && integer_zerop (TREE_OPERAND (arg0, 1))
13095 : 15941 : && integer_zerop (op2)
13096 : 8979837 : && (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 : 8978888 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13164 : 345 : && integer_onep (TREE_OPERAND (arg0, 1))
13165 : 3 : && integer_zerop (op2)
13166 : 8978888 : && 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 : 8978888 : if (integer_zerop (op2)
13185 : 1929705 : && TREE_CODE (arg0) == NE_EXPR
13186 : 543795 : && integer_zerop (TREE_OPERAND (arg0, 1))
13187 : 298010 : && integer_pow2p (arg1)
13188 : 32221 : && 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 : 8978888 : && 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 : 8978888 : 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 : 8669866 : if (integer_zerop (op2)
13206 : 1673610 : && truth_value_p (TREE_CODE (arg0))
13207 : 1550052 : && truth_value_p (TREE_CODE (arg1))
13208 : 8702652 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13209 : 32786 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13210 : : : TRUTH_ANDIF_EXPR,
13211 : 32786 : 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 : 8637080 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13215 : 461456 : && truth_value_p (TREE_CODE (arg0))
13216 : 323634 : && truth_value_p (TREE_CODE (arg1))
13217 : 8673768 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13218 : : {
13219 : 36688 : location_t loc0 = expr_location_or (arg0, loc);
13220 : : /* Only perform transformation if ARG0 is easily inverted. */
13221 : 36688 : tem = fold_invert_truthvalue (loc0, arg0);
13222 : 36688 : if (tem)
13223 : 36424 : 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 : 36424 : op1);
13228 : : }
13229 : :
13230 : : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13231 : 8600656 : if (integer_zerop (arg1)
13232 : 223540 : && truth_value_p (TREE_CODE (arg0))
13233 : 49399 : && truth_value_p (TREE_CODE (op2))
13234 : 8600684 : && (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 : 8600656 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13248 : 361630 : && truth_value_p (TREE_CODE (arg0))
13249 : 281348 : && truth_value_p (TREE_CODE (op2))
13250 : 8600842 : && (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 : 626728 : case BIT_FIELD_REF:
13263 : 626728 : if (TREE_CODE (arg0) == VECTOR_CST
13264 : 28317 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13265 : 1830 : || (VECTOR_TYPE_P (type)
13266 : 1148 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13267 : 27615 : && tree_fits_uhwi_p (op1)
13268 : 654343 : && tree_fits_uhwi_p (op2))
13269 : : {
13270 : 27615 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13271 : 27615 : unsigned HOST_WIDE_INT width
13272 : 27615 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13273 : 27615 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13274 : 27615 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13275 : 27615 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13276 : :
13277 : 27615 : if (n != 0
13278 : 27615 : && (idx % width) == 0
13279 : 27615 : && (n % width) == 0
13280 : 55230 : && known_le ((idx + n) / width,
13281 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13282 : : {
13283 : 27615 : idx = idx / width;
13284 : 27615 : n = n / width;
13285 : :
13286 : 27615 : if (TREE_CODE (arg0) == VECTOR_CST)
13287 : : {
13288 : 27615 : if (n == 1)
13289 : : {
13290 : 26491 : tem = VECTOR_CST_ELT (arg0, idx);
13291 : 26491 : if (VECTOR_TYPE_P (type))
13292 : 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13293 : 26491 : return tem;
13294 : : }
13295 : :
13296 : 1124 : tree_vector_builder vals (type, n, 1);
13297 : 6868 : for (unsigned i = 0; i < n; ++i)
13298 : 5744 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13299 : 1124 : return vals.build ();
13300 : 1124 : }
13301 : : }
13302 : : }
13303 : :
13304 : : /* On constants we can use native encode/interpret to constant
13305 : : fold (nearly) all BIT_FIELD_REFs. */
13306 : 599113 : if (CONSTANT_CLASS_P (arg0)
13307 : 1503 : && can_native_interpret_type_p (type)
13308 : : && BITS_PER_UNIT == 8
13309 : 1503 : && tree_fits_uhwi_p (op1)
13310 : 600616 : && tree_fits_uhwi_p (op2))
13311 : : {
13312 : 1503 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13313 : 1503 : 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 : 1503 : if (bitpos % BITS_PER_UNIT == 0
13318 : 1503 : && bitsize % BITS_PER_UNIT == 0
13319 : 1503 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13320 : : {
13321 : 1503 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13322 : 1503 : unsigned HOST_WIDE_INT len
13323 : 1503 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13324 : 1503 : bitpos / BITS_PER_UNIT);
13325 : 1503 : if (len > 0
13326 : 1503 : && len * BITS_PER_UNIT >= bitsize)
13327 : : {
13328 : 1503 : tree v = native_interpret_expr (type, b,
13329 : : bitsize / BITS_PER_UNIT);
13330 : 1503 : if (v)
13331 : 1497 : return v;
13332 : : }
13333 : : }
13334 : : }
13335 : :
13336 : : return NULL_TREE;
13337 : :
13338 : 736893 : case VEC_PERM_EXPR:
13339 : : /* Perform constant folding of BIT_INSERT_EXPR. */
13340 : 736893 : if (TREE_CODE (arg2) == VECTOR_CST
13341 : 725664 : && TREE_CODE (op0) == VECTOR_CST
13342 : 15207 : && 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 : 13717 : case BIT_INSERT_EXPR:
13358 : : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13359 : 13717 : 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 : 13715 : else if (TREE_CODE (arg0) == VECTOR_CST
13375 : 906 : && CONSTANT_CLASS_P (arg1)
13376 : 14017 : && 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 : 27980102 : 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 : 740077 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13414 : : unsigned *ctor_idx)
13415 : : {
13416 : 740077 : tree index_type = NULL_TREE;
13417 : 740077 : signop index_sgn = UNSIGNED;
13418 : 740077 : offset_int low_bound = 0;
13419 : :
13420 : 740077 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13421 : : {
13422 : 740077 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13423 : 740077 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13424 : : {
13425 : : /* Static constructors for variably sized objects makes no sense. */
13426 : 740077 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13427 : 740077 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13428 : : /* ??? When it is obvious that the range is signed, treat it so. */
13429 : 740077 : if (TYPE_UNSIGNED (index_type)
13430 : 392434 : && TYPE_MAX_VALUE (domain_type)
13431 : 1132477 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13432 : 392400 : 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 : 740077 : index_sgn = TYPE_SIGN (index_type);
13442 : 740077 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13443 : : }
13444 : : }
13445 : : }
13446 : :
13447 : 740077 : if (index_type)
13448 : 740077 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13449 : : index_sgn);
13450 : :
13451 : 740077 : offset_int index = low_bound;
13452 : 740077 : if (index_type)
13453 : 740077 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13454 : :
13455 : 740077 : offset_int max_index = index;
13456 : 740077 : unsigned cnt;
13457 : 740077 : tree cfield, cval;
13458 : 740077 : bool first_p = true;
13459 : :
13460 : 14740307 : 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 : 14738923 : if (cfield)
13466 : : {
13467 : 6514635 : if (TREE_CODE (cfield) == INTEGER_CST)
13468 : 13027798 : max_index = index
13469 : 6513899 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13470 : : else
13471 : : {
13472 : 736 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13473 : 736 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13474 : : index_sgn);
13475 : 736 : max_index
13476 : 736 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13477 : : index_sgn);
13478 : 736 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13479 : : }
13480 : : }
13481 : 8224288 : else if (!first_p)
13482 : : {
13483 : 7941540 : index = max_index + 1;
13484 : 7941540 : if (index_type)
13485 : 7941540 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13486 : 7941540 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13487 : 7941540 : max_index = index;
13488 : : }
13489 : : else
13490 : : first_p = false;
13491 : :
13492 : 14738923 : if (TREE_CODE (cval) == RAW_DATA_CST)
13493 : 1841 : max_index += RAW_DATA_LENGTH (cval) - 1;
13494 : :
13495 : : /* Do we have match? */
13496 : 14738923 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13497 : : {
13498 : 14738632 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13499 : : {
13500 : 738574 : if (ctor_idx)
13501 : 738574 : *ctor_idx = cnt;
13502 : 738574 : 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 : 1503 : if (ctor_idx)
13514 : 1503 : *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 : 1247856999 : fold (tree expr)
13533 : : {
13534 : 1248030886 : const tree t = expr;
13535 : 1248030886 : enum tree_code code = TREE_CODE (t);
13536 : 1248030886 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13537 : 1248030886 : tree tem;
13538 : 1248030886 : location_t loc = EXPR_LOCATION (expr);
13539 : :
13540 : : /* Return right away if a constant. */
13541 : 1248030886 : if (kind == tcc_constant)
13542 : : return t;
13543 : :
13544 : : /* CALL_EXPR-like objects with variable numbers of operands are
13545 : : treated specially. */
13546 : 1143782668 : if (kind == tcc_vl_exp)
13547 : : {
13548 : 162748916 : if (code == CALL_EXPR)
13549 : : {
13550 : 162748362 : tem = fold_call_expr (loc, expr, false);
13551 : 322853251 : return tem ? tem : expr;
13552 : : }
13553 : : return expr;
13554 : : }
13555 : :
13556 : 981033752 : if (IS_EXPR_CODE_CLASS (kind))
13557 : : {
13558 : 978799406 : tree type = TREE_TYPE (t);
13559 : 978799406 : tree op0, op1, op2;
13560 : :
13561 : 978799406 : switch (TREE_CODE_LENGTH (code))
13562 : : {
13563 : 883251316 : case 1:
13564 : 883251316 : op0 = TREE_OPERAND (t, 0);
13565 : 883251316 : tem = fold_unary_loc (loc, code, type, op0);
13566 : 1508099995 : return tem ? tem : expr;
13567 : 86790819 : case 2:
13568 : 86790819 : op0 = TREE_OPERAND (t, 0);
13569 : 86790819 : op1 = TREE_OPERAND (t, 1);
13570 : 86790819 : tem = fold_binary_loc (loc, code, type, op0, op1);
13571 : 164653643 : return tem ? tem : expr;
13572 : 4214762 : case 3:
13573 : 4214762 : op0 = TREE_OPERAND (t, 0);
13574 : 4214762 : op1 = TREE_OPERAND (t, 1);
13575 : 4214762 : op2 = TREE_OPERAND (t, 2);
13576 : 4214762 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13577 : 8170838 : return tem ? tem : expr;
13578 : : default:
13579 : : break;
13580 : : }
13581 : : }
13582 : :
13583 : 6776855 : switch (code)
13584 : : {
13585 : 4438818 : case ARRAY_REF:
13586 : 4438818 : {
13587 : 4438818 : tree op0 = TREE_OPERAND (t, 0);
13588 : 4438818 : tree op1 = TREE_OPERAND (t, 1);
13589 : :
13590 : 4438818 : if (TREE_CODE (op1) == INTEGER_CST
13591 : 2875208 : && TREE_CODE (op0) == CONSTRUCTOR
13592 : 4440273 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13593 : : {
13594 : 1455 : unsigned int idx;
13595 : 1455 : tree val
13596 : 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13597 : : &idx);
13598 : 1455 : if (val)
13599 : : {
13600 : 1455 : 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 : 107604 : case CONSTRUCTOR:
13620 : 107604 : {
13621 : 107604 : tree type = TREE_TYPE (t);
13622 : 107604 : if (TREE_CODE (type) != VECTOR_TYPE)
13623 : : return t;
13624 : :
13625 : : unsigned i;
13626 : : tree val;
13627 : 249357 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13628 : 215751 : if (! CONSTANT_CLASS_P (val))
13629 : : return t;
13630 : :
13631 : 33606 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13632 : : }
13633 : :
13634 : 173887 : case CONST_DECL:
13635 : 173887 : 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 : 722999035 : fold_build1_loc (location_t loc,
13901 : : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13902 : : {
13903 : 722999035 : 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 : 722999035 : tem = fold_unary_loc (loc, code, type, op0);
13916 : 722999035 : if (!tem)
13917 : 386534440 : 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 : 722999035 : 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 : 572122989 : fold_build2_loc (location_t loc,
13938 : : enum tree_code code, tree type, tree op0, tree op1
13939 : : MEM_STAT_DECL)
13940 : : {
13941 : 572122989 : 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 : 572122989 : tem = fold_binary_loc (loc, code, type, op0, op1);
13962 : 572122989 : if (!tem)
13963 : 320364174 : 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 : 572122989 : 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 : 22136851 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13991 : : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13992 : : {
13993 : 22136851 : 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 : 22136851 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14021 : 22136851 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14022 : 22136851 : if (!tem)
14023 : 19477543 : 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 : 22136851 : 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 : 49824361 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
14059 : : int nargs, tree *argarray)
14060 : : {
14061 : 49824361 : 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 : 49824361 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14084 : 49824361 : if (!tem)
14085 : 48149620 : 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 : 49824361 : 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 : 544773 : fold_init (tree expr)
14132 : : {
14133 : 544773 : tree result;
14134 : 544773 : START_FOLD_INIT;
14135 : :
14136 : 544773 : result = fold (expr);
14137 : :
14138 : 544773 : END_FOLD_INIT;
14139 : 544773 : return result;
14140 : : }
14141 : :
14142 : : tree
14143 : 2988393 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14144 : : tree type, tree op)
14145 : : {
14146 : 2988393 : tree result;
14147 : 2988393 : START_FOLD_INIT;
14148 : :
14149 : 2988393 : result = fold_build1_loc (loc, code, type, op);
14150 : :
14151 : 2988393 : END_FOLD_INIT;
14152 : 2988393 : 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 : 3487 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14170 : : int nargs, tree *argarray)
14171 : : {
14172 : 3487 : tree result;
14173 : 3487 : START_FOLD_INIT;
14174 : :
14175 : 3487 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14176 : :
14177 : 3487 : END_FOLD_INIT;
14178 : 3487 : return result;
14179 : : }
14180 : :
14181 : : tree
14182 : 21211466 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14183 : : tree lhs, tree rhs)
14184 : : {
14185 : 21211466 : tree result;
14186 : 21211466 : START_FOLD_INIT;
14187 : :
14188 : 21211466 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14189 : :
14190 : 21211466 : END_FOLD_INIT;
14191 : 21211466 : 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 : 1522612 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14245 : : {
14246 : 1522612 : gimple *stmt;
14247 : 1522612 : tree op1, op2;
14248 : :
14249 : 1522612 : if (operand_equal_p (top, bottom, 0))
14250 : : return true;
14251 : :
14252 : 1053411 : if (TREE_CODE (type) != INTEGER_TYPE)
14253 : : return false;
14254 : :
14255 : 1053406 : 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 : 371305 : 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 : 371305 : if (!nowrap
14270 : 13872 : && !TYPE_OVERFLOW_UNDEFINED (type)
14271 : 376021 : && !integer_pow2p (bottom))
14272 : : return false;
14273 : 370883 : if (TREE_CODE (bottom) == INTEGER_CST)
14274 : : {
14275 : 369167 : op1 = TREE_OPERAND (top, 0);
14276 : 369167 : op2 = TREE_OPERAND (top, 1);
14277 : 369167 : if (TREE_CODE (op1) == INTEGER_CST)
14278 : 0 : std::swap (op1, op2);
14279 : 369167 : if (TREE_CODE (op2) == INTEGER_CST)
14280 : : {
14281 : 359229 : if (multiple_of_p (type, op2, bottom, nowrap))
14282 : : return true;
14283 : : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14284 : 2995 : if (multiple_of_p (type, bottom, op2, nowrap))
14285 : : {
14286 : 1892 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14287 : 1892 : wi::to_widest (op2));
14288 : 1892 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14289 : : {
14290 : 1892 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14291 : 1892 : return multiple_of_p (type, op1, op2, nowrap);
14292 : : }
14293 : 1892 : }
14294 : 1103 : return multiple_of_p (type, op1, bottom, nowrap);
14295 : : }
14296 : : }
14297 : 11654 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14298 : 11654 : || 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 : 212375 : case MINUS_EXPR:
14317 : 212375 : 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 : 212375 : if (!nowrap
14322 : 170998 : && !TYPE_OVERFLOW_UNDEFINED (type)
14323 : 381944 : && !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 : 186223 : op1 = TREE_OPERAND (top, 1);
14330 : 186223 : if (TREE_CODE (top) == PLUS_EXPR
14331 : 180531 : && nowrap
14332 : 35772 : && TYPE_UNSIGNED (type)
14333 : 221444 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14334 : 30582 : 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 : 186223 : return (multiple_of_p (type, op1, bottom, nowrap)
14341 : 186223 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14342 : :
14343 : 138587 : CASE_CONVERT:
14344 : : /* Can't handle conversions from non-integral or wider integral type. */
14345 : 138587 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14346 : 138587 : || (TYPE_PRECISION (type)
14347 : 38080 : < 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 : 37774 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14352 : 75548 : TREE_OPERAND (top, 0), bottom, false);
14353 : :
14354 : 12382 : case SAVE_EXPR:
14355 : 12382 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14356 : :
14357 : 86 : case COND_EXPR:
14358 : 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14359 : 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14360 : :
14361 : 122276 : case INTEGER_CST:
14362 : 122276 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14363 : 2724 : return false;
14364 : 119552 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14365 : : SIGNED);
14366 : :
14367 : 64027 : case SSA_NAME:
14368 : 64027 : if (TREE_CODE (bottom) == INTEGER_CST
14369 : 61066 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14370 : 125093 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14371 : : {
14372 : 26733 : 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 : 26733 : 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 : 26921 : && wi::multiple_of_p (wi::to_widest (op2),
14388 : 188 : wi::to_widest (bottom), SIGNED))
14389 : 179 : return true;
14390 : :
14391 : 26554 : op1 = gimple_assign_rhs1 (stmt);
14392 : 26554 : if (code == MINUS_EXPR
14393 : 2135 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14394 : 2135 : && TREE_CODE (op2) == SSA_NAME
14395 : 2135 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14396 : 2135 : && gimple_code (stmt) == GIMPLE_ASSIGN
14397 : 1781 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14398 : 62 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14399 : 26616 : && 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 : 587825 : tree_expr_finite_p (const_tree x)
14420 : : {
14421 : 587829 : machine_mode mode = element_mode (x);
14422 : 587829 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14423 : : return true;
14424 : 587635 : 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 : 841603 : tree_expr_infinite_p (const_tree x)
14472 : : {
14473 : 842053 : if (!HONOR_INFINITIES (x))
14474 : : return false;
14475 : 841938 : 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 : 383593 : tree_expr_maybe_infinite_p (const_tree x)
14498 : : {
14499 : 383601 : if (!HONOR_INFINITIES (x))
14500 : : return false;
14501 : 383274 : switch (TREE_CODE (x))
14502 : : {
14503 : 192 : case REAL_CST:
14504 : 192 : 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 : 732868 : tree_expr_maybe_signaling_nan_p (const_tree x)
14547 : : {
14548 : 732868 : 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 : 4101853 : tree_expr_nan_p (const_tree x)
14594 : : {
14595 : 4493189 : if (!HONOR_NANS (x))
14596 : : return false;
14597 : 4492839 : switch (TREE_CODE (x))
14598 : : {
14599 : 3792 : case REAL_CST:
14600 : 3792 : return real_isnan (TREE_REAL_CST_PTR (x));
14601 : 391336 : case NON_LVALUE_EXPR:
14602 : 391336 : case SAVE_EXPR:
14603 : 391336 : 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 : 4997206 : tree_expr_maybe_nan_p (const_tree x)
14618 : : {
14619 : 7003737 : if (!HONOR_NANS (x))
14620 : : return false;
14621 : 6838997 : switch (TREE_CODE (x))
14622 : : {
14623 : 3288 : case REAL_CST:
14624 : 3288 : return real_isnan (TREE_REAL_CST_PTR (x));
14625 : : case FLOAT_EXPR:
14626 : : return false;
14627 : 13827 : case PLUS_EXPR:
14628 : 13827 : case MINUS_EXPR:
14629 : 13827 : case MULT_EXPR:
14630 : 13827 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14631 : 13827 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14632 : 2006531 : case ABS_EXPR:
14633 : 2006531 : case CONVERT_EXPR:
14634 : 2006531 : case NEGATE_EXPR:
14635 : 2006531 : case NON_LVALUE_EXPR:
14636 : 2006531 : case SAVE_EXPR:
14637 : 2006531 : 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 : 1082 : case CALL_EXPR:
14646 : 1082 : 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 : 603789 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14670 : : {
14671 : 603789 : if (!HONOR_SIGNED_ZEROS (x))
14672 : : return false;
14673 : 603789 : 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 : 38622813 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14714 : : {
14715 : 38622813 : if (!VECTOR_TYPE_P (type)
14716 : 38604201 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14717 : 77226270 : && 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 : 13653164 : tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14731 : : bool *strict_overflow_p, int depth)
14732 : : {
14733 : 13653164 : if (TYPE_UNSIGNED (type))
14734 : : return true;
14735 : :
14736 : 5400021 : switch (code)
14737 : : {
14738 : 329572 : case ABS_EXPR:
14739 : : /* We can't return 1 if flag_wrapv is set because
14740 : : ABS_EXPR<INT_MIN> = INT_MIN. */
14741 : 329572 : if (!ANY_INTEGRAL_TYPE_P (type))
14742 : : return true;
14743 : 18176 : if (TYPE_OVERFLOW_UNDEFINED (type))
14744 : : {
14745 : 17231 : *strict_overflow_p = true;
14746 : 17231 : return true;
14747 : : }
14748 : : break;
14749 : :
14750 : 220397 : case NON_LVALUE_EXPR:
14751 : 220397 : case FLOAT_EXPR:
14752 : 220397 : case FIX_TRUNC_EXPR:
14753 : 220397 : return RECURSE (op0);
14754 : :
14755 : 4749691 : CASE_CONVERT:
14756 : 4749691 : {
14757 : 4749691 : tree inner_type = TREE_TYPE (op0);
14758 : 4749691 : tree outer_type = type;
14759 : :
14760 : 4749691 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14761 : : {
14762 : 309866 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14763 : 309866 : 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 : 4439825 : else if (INTEGRAL_TYPE_P (outer_type))
14772 : : {
14773 : 4439788 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14774 : 0 : return RECURSE (op0);
14775 : 4439788 : if (INTEGRAL_TYPE_P (inner_type))
14776 : 4278869 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14777 : 4278869 : && TYPE_UNSIGNED (inner_type);
14778 : : }
14779 : : }
14780 : : break;
14781 : :
14782 : 100361 : default:
14783 : 100361 : 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 : 39825139 : 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 : 39825139 : if (TYPE_UNSIGNED (type))
14801 : : return true;
14802 : :
14803 : 15349952 : switch (code)
14804 : : {
14805 : 5676606 : case POINTER_PLUS_EXPR:
14806 : 5676606 : case PLUS_EXPR:
14807 : 5676606 : if (FLOAT_TYPE_P (type))
14808 : 68125 : 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 : 5608481 : if (TREE_CODE (type) == INTEGER_TYPE
14813 : 5602189 : && TREE_CODE (op0) == NOP_EXPR
14814 : 7824 : && TREE_CODE (op1) == NOP_EXPR)
14815 : : {
14816 : 209 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14817 : 209 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14818 : 209 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14819 : 316 : && 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 : 1336038 : case MULT_EXPR:
14829 : 1336038 : 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 : 1277452 : if (operand_equal_p (op0, op1, 0)
14834 : 1277452 : || (RECURSE (op0) && RECURSE (op1)))
14835 : : {
14836 : 1236 : if (ANY_INTEGRAL_TYPE_P (type)
14837 : 13028 : && TYPE_OVERFLOW_UNDEFINED (type))
14838 : 11792 : *strict_overflow_p = true;
14839 : 13007 : 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 : 1323031 : if (TREE_CODE (type) == INTEGER_TYPE
14846 : 1244019 : && (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 : 95343 : case BIT_AND_EXPR:
14882 : 95343 : return RECURSE (op0) || RECURSE (op1);
14883 : :
14884 : 76513 : case MAX_EXPR:
14885 : : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14886 : : things. */
14887 : 76513 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14888 : 76 : return RECURSE (op0) && RECURSE (op1);
14889 : 76437 : return RECURSE (op0) || RECURSE (op1);
14890 : :
14891 : 736708 : case BIT_IOR_EXPR:
14892 : 736708 : case BIT_XOR_EXPR:
14893 : 736708 : case MIN_EXPR:
14894 : 736708 : case RDIV_EXPR:
14895 : 736708 : case TRUNC_DIV_EXPR:
14896 : 736708 : case CEIL_DIV_EXPR:
14897 : 736708 : case FLOOR_DIV_EXPR:
14898 : 736708 : case ROUND_DIV_EXPR:
14899 : 736708 : return RECURSE (op0) && RECURSE (op1);
14900 : :
14901 : 93698 : case TRUNC_MOD_EXPR:
14902 : 93698 : return RECURSE (op0);
14903 : :
14904 : 386 : case FLOOR_MOD_EXPR:
14905 : 386 : return RECURSE (op1);
14906 : :
14907 : 7334660 : case CEIL_MOD_EXPR:
14908 : 7334660 : case ROUND_MOD_EXPR:
14909 : 7334660 : default:
14910 : 7334660 : 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 : 48167797 : tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14924 : : {
14925 : 48167797 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14926 : : return true;
14927 : :
14928 : 32227780 : switch (TREE_CODE (t))
14929 : : {
14930 : 3571945 : case INTEGER_CST:
14931 : 3571945 : return tree_int_cst_sgn (t) >= 0;
14932 : :
14933 : 1004504 : case REAL_CST:
14934 : 1004504 : 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 : 17545867 : 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 : 17545867 : return (!name_registered_for_update_p (t)
14949 : 17545866 : && depth < param_max_ssa_name_query_depth
14950 : 33685359 : && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14951 : : strict_overflow_p, depth));
14952 : :
14953 : 10104974 : default:
14954 : 10104974 : 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 : 21245201 : tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14965 : : bool *strict_overflow_p, int depth)
14966 : : {
14967 : 21245201 : 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 : 1017 : CASE_CFN_CLZ:
15005 : 1017 : CASE_CFN_CTZ:
15006 : 1017 : 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 : 119547 : CASE_CFN_ASINH:
15018 : 119547 : CASE_CFN_ASINH_FN:
15019 : 119547 : CASE_CFN_ASINPI:
15020 : 119547 : CASE_CFN_ASINPI_FN:
15021 : 119547 : CASE_CFN_ATAN:
15022 : 119547 : CASE_CFN_ATAN_FN:
15023 : 119547 : CASE_CFN_ATANH:
15024 : 119547 : CASE_CFN_ATANH_FN:
15025 : 119547 : CASE_CFN_ATANPI:
15026 : 119547 : CASE_CFN_ATANPI_FN:
15027 : 119547 : CASE_CFN_CBRT:
15028 : 119547 : CASE_CFN_CBRT_FN:
15029 : 119547 : CASE_CFN_CEIL:
15030 : 119547 : CASE_CFN_CEIL_FN:
15031 : 119547 : CASE_CFN_ERF:
15032 : 119547 : CASE_CFN_ERF_FN:
15033 : 119547 : CASE_CFN_EXPM1:
15034 : 119547 : CASE_CFN_EXPM1_FN:
15035 : 119547 : CASE_CFN_FLOOR:
15036 : 119547 : CASE_CFN_FLOOR_FN:
15037 : 119547 : CASE_CFN_FMOD:
15038 : 119547 : CASE_CFN_FMOD_FN:
15039 : 119547 : CASE_CFN_FREXP:
15040 : 119547 : CASE_CFN_FREXP_FN:
15041 : 119547 : CASE_CFN_ICEIL:
15042 : 119547 : CASE_CFN_IFLOOR:
15043 : 119547 : CASE_CFN_IRINT:
15044 : 119547 : CASE_CFN_IROUND:
15045 : 119547 : CASE_CFN_LCEIL:
15046 : 119547 : CASE_CFN_LDEXP:
15047 : 119547 : CASE_CFN_LFLOOR:
15048 : 119547 : CASE_CFN_LLCEIL:
15049 : 119547 : CASE_CFN_LLFLOOR:
15050 : 119547 : CASE_CFN_LLRINT:
15051 : 119547 : CASE_CFN_LLRINT_FN:
15052 : 119547 : CASE_CFN_LLROUND:
15053 : 119547 : CASE_CFN_LLROUND_FN:
15054 : 119547 : CASE_CFN_LRINT:
15055 : 119547 : CASE_CFN_LRINT_FN:
15056 : 119547 : CASE_CFN_LROUND:
15057 : 119547 : CASE_CFN_LROUND_FN:
15058 : 119547 : CASE_CFN_MODF:
15059 : 119547 : CASE_CFN_MODF_FN:
15060 : 119547 : CASE_CFN_NEARBYINT:
15061 : 119547 : CASE_CFN_NEARBYINT_FN:
15062 : 119547 : CASE_CFN_RINT:
15063 : 119547 : CASE_CFN_RINT_FN:
15064 : 119547 : CASE_CFN_ROUND:
15065 : 119547 : CASE_CFN_ROUND_FN:
15066 : 119547 : CASE_CFN_ROUNDEVEN:
15067 : 119547 : CASE_CFN_ROUNDEVEN_FN:
15068 : 119547 : CASE_CFN_SCALB:
15069 : 119547 : CASE_CFN_SCALBLN:
15070 : 119547 : CASE_CFN_SCALBLN_FN:
15071 : 119547 : CASE_CFN_SCALBN:
15072 : 119547 : CASE_CFN_SCALBN_FN:
15073 : 119547 : CASE_CFN_SIGNBIT:
15074 : 119547 : CASE_CFN_SIGNIFICAND:
15075 : 119547 : CASE_CFN_SINH:
15076 : 119547 : CASE_CFN_SINH_FN:
15077 : 119547 : CASE_CFN_TANH:
15078 : 119547 : CASE_CFN_TANH_FN:
15079 : 119547 : CASE_CFN_TRUNC:
15080 : 119547 : CASE_CFN_TRUNC_FN:
15081 : : /* True if the 1st argument is nonnegative. */
15082 : 119547 : 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 : 4923 : CASE_CFN_POW:
15118 : 4923 : CASE_CFN_POW_FN:
15119 : : /* True if the 1st argument is nonnegative or the second
15120 : : argument is an even integer valued real. */
15121 : 4923 : if (TREE_CODE (arg1) == REAL_CST)
15122 : : {
15123 : 2147 : REAL_VALUE_TYPE c;
15124 : 2147 : HOST_WIDE_INT n;
15125 : :
15126 : 2147 : c = TREE_REAL_CST (arg1);
15127 : 2147 : n = real_to_integer (&c);
15128 : 2147 : 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 : 4421 : return RECURSE (arg0);
15137 : :
15138 : 21081152 : default:
15139 : 21081152 : break;
15140 : : }
15141 : 21081152 : 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 : 916798 : tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15151 : : {
15152 : 916798 : enum tree_code code = TREE_CODE (t);
15153 : 916798 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15154 : : return true;
15155 : :
15156 : 736961 : switch (code)
15157 : : {
15158 : 217 : case TARGET_EXPR:
15159 : 217 : {
15160 : 217 : tree temp = TARGET_EXPR_SLOT (t);
15161 : 217 : 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 : 217 : 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 : 419 : while (1)
15171 : : {
15172 : 419 : if (TREE_CODE (t) == BIND_EXPR)
15173 : 202 : t = expr_last (BIND_EXPR_BODY (t));
15174 : 217 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15175 : 217 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15176 : 0 : t = expr_last (TREE_OPERAND (t, 0));
15177 : 217 : else if (TREE_CODE (t) == STATEMENT_LIST)
15178 : 0 : t = expr_last (t);
15179 : : else
15180 : : break;
15181 : : }
15182 : 217 : if (TREE_CODE (t) == MODIFY_EXPR
15183 : 217 : && TREE_OPERAND (t, 0) == temp)
15184 : 202 : return RECURSE (TREE_OPERAND (t, 1));
15185 : :
15186 : : return false;
15187 : : }
15188 : :
15189 : 339592 : case CALL_EXPR:
15190 : 339592 : {
15191 : 339592 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15192 : 339592 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15193 : :
15194 : 339592 : return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15195 : : get_call_combined_fn (t),
15196 : : arg0,
15197 : : arg1,
15198 : 339592 : 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 : 394710 : case SAVE_EXPR:
15208 : 394710 : 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 : 25015789 : tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15225 : : {
15226 : 25015789 : enum tree_code code;
15227 : 25015789 : if (error_operand_p (t))
15228 : : return false;
15229 : :
15230 : 25015788 : code = TREE_CODE (t);
15231 : 25015788 : switch (TREE_CODE_CLASS (code))
15232 : : {
15233 : 1058038 : case tcc_binary:
15234 : 1058038 : case tcc_comparison:
15235 : 1058038 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15236 : 1058038 : TREE_TYPE (t),
15237 : 1058038 : TREE_OPERAND (t, 0),
15238 : 1058038 : TREE_OPERAND (t, 1),
15239 : 1058038 : strict_overflow_p, depth);
15240 : :
15241 : 1635615 : case tcc_unary:
15242 : 1635615 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15243 : 1635615 : TREE_TYPE (t),
15244 : 1635615 : TREE_OPERAND (t, 0),
15245 : 1635615 : strict_overflow_p, depth);
15246 : :
15247 : 11807973 : case tcc_constant:
15248 : 11807973 : case tcc_declaration:
15249 : 11807973 : case tcc_reference:
15250 : 11807973 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15251 : :
15252 : 10514162 : default:
15253 : 10514162 : break;
15254 : : }
15255 : :
15256 : 10514162 : 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 : 9597285 : case COND_EXPR:
15273 : 9597285 : case CONSTRUCTOR:
15274 : 9597285 : case OBJ_TYPE_REF:
15275 : 9597285 : case ADDR_EXPR:
15276 : 9597285 : case WITH_SIZE_EXPR:
15277 : 9597285 : case SSA_NAME:
15278 : 9597285 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15279 : :
15280 : 916798 : default:
15281 : 916798 : 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 : 17285048 : tree_expr_nonnegative_p (tree t)
15290 : : {
15291 : 17285048 : bool ret, strict_overflow_p;
15292 : :
15293 : 17285048 : strict_overflow_p = false;
15294 : 17285048 : ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15295 : 17285048 : if (strict_overflow_p)
15296 : 17431 : 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 : 17285048 : 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 : 1435277 : tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15314 : : bool *strict_overflow_p)
15315 : : {
15316 : 1435277 : switch (code)
15317 : : {
15318 : 1 : case ABS_EXPR:
15319 : 1 : return tree_expr_nonzero_warnv_p (op0,
15320 : 1 : strict_overflow_p);
15321 : :
15322 : 778758 : case NOP_EXPR:
15323 : 778758 : {
15324 : 778758 : tree inner_type = TREE_TYPE (op0);
15325 : 778758 : tree outer_type = type;
15326 : :
15327 : 778758 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15328 : 778758 : && tree_expr_nonzero_warnv_p (op0,
15329 : : strict_overflow_p));
15330 : : }
15331 : 28113 : break;
15332 : :
15333 : 28113 : case NON_LVALUE_EXPR:
15334 : 28113 : return tree_expr_nonzero_warnv_p (op0,
15335 : 28113 : 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 : 2955765 : tree_binary_nonzero_warnv_p (enum tree_code code,
15354 : : tree type,
15355 : : tree op0,
15356 : : tree op1, bool *strict_overflow_p)
15357 : : {
15358 : 2955765 : bool sub_strict_overflow_p;
15359 : 2955765 : switch (code)
15360 : : {
15361 : 461827 : case POINTER_PLUS_EXPR:
15362 : 461827 : case PLUS_EXPR:
15363 : 461827 : 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 : 104179 : sub_strict_overflow_p = false;
15368 : 104179 : if (!tree_expr_nonnegative_warnv_p (op0,
15369 : : &sub_strict_overflow_p)
15370 : 104179 : || !tree_expr_nonnegative_warnv_p (op1,
15371 : : &sub_strict_overflow_p))
15372 : 101841 : 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 : 2338 : return (tree_expr_nonzero_warnv_p (op0,
15378 : : strict_overflow_p)
15379 : 2338 : || tree_expr_nonzero_warnv_p (op1,
15380 : : strict_overflow_p));
15381 : : }
15382 : : break;
15383 : :
15384 : 21079 : case MULT_EXPR:
15385 : 21079 : if (TYPE_OVERFLOW_UNDEFINED (type))
15386 : : {
15387 : 444 : if (tree_expr_nonzero_warnv_p (op0,
15388 : : strict_overflow_p)
15389 : 444 : && 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 : 13448 : case MIN_EXPR:
15399 : 13448 : sub_strict_overflow_p = false;
15400 : 13448 : if (tree_expr_nonzero_warnv_p (op0,
15401 : : &sub_strict_overflow_p)
15402 : 13448 : && 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 : 270798 : case BIT_IOR_EXPR:
15440 : 270798 : return (tree_expr_nonzero_warnv_p (op1,
15441 : : strict_overflow_p)
15442 : 270798 : || 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 : 148857969 : tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15462 : : {
15463 : 148857969 : bool sub_strict_overflow_p;
15464 : 148857969 : switch (TREE_CODE (t))
15465 : : {
15466 : 1127501 : case INTEGER_CST:
15467 : 1127501 : return !integer_zerop (t);
15468 : :
15469 : 11504208 : case ADDR_EXPR:
15470 : 11504208 : {
15471 : 11504208 : tree base = TREE_OPERAND (t, 0);
15472 : :
15473 : 11504208 : if (!DECL_P (base))
15474 : 6189229 : base = get_base_address (base);
15475 : :
15476 : 11504208 : 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 : 11504208 : int nonzero_addr = maybe_nonzero_address (base);
15486 : 11504208 : if (nonzero_addr >= 0)
15487 : 8791663 : return nonzero_addr;
15488 : :
15489 : : /* Constants are never weak. */
15490 : 2712545 : if (CONSTANT_CLASS_P (base))
15491 : : return true;
15492 : :
15493 : : return false;
15494 : : }
15495 : :
15496 : 31698 : case COND_EXPR:
15497 : 31698 : sub_strict_overflow_p = false;
15498 : 31698 : if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15499 : : &sub_strict_overflow_p)
15500 : 31698 : && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15501 : : &sub_strict_overflow_p))
15502 : : {
15503 : 1226 : if (sub_strict_overflow_p)
15504 : 0 : *strict_overflow_p = true;
15505 : 1226 : return true;
15506 : : }
15507 : : break;
15508 : :
15509 : 125494119 : case SSA_NAME:
15510 : 125494119 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15511 : : break;
15512 : 97661464 : 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 : 15032 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15534 : : {
15535 : 15032 : 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 : 13657 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15567 : : {
15568 : 13657 : 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 : 129423 : integer_valued_real_single_p (tree t, int depth)
15631 : : {
15632 : 129423 : switch (TREE_CODE (t))
15633 : : {
15634 : 2222 : case REAL_CST:
15635 : 2222 : 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 : 90752 : 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 : 90752 : return (!name_registered_for_update_p (t)
15647 : 90752 : && depth < param_max_ssa_name_query_depth
15648 : 180725 : && 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 : 97631 : integer_valued_real_p (tree t, int depth)
15693 : : {
15694 : 97631 : if (t == error_mark_node)
15695 : : return false;
15696 : :
15697 : 97631 : STRIP_ANY_LOCATION_WRAPPER (t);
15698 : :
15699 : 97631 : tree_code code = TREE_CODE (t);
15700 : 97631 : 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 : 8346 : case tcc_constant:
15711 : 8346 : case tcc_declaration:
15712 : 8346 : case tcc_reference:
15713 : 8346 : return integer_valued_real_single_p (t, depth);
15714 : :
15715 : 89285 : default:
15716 : 89285 : break;
15717 : : }
15718 : :
15719 : 89285 : switch (code)
15720 : : {
15721 : 89285 : case COND_EXPR:
15722 : 89285 : case SSA_NAME:
15723 : 89285 : 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 : 15888714 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15752 : : {
15753 : 15888714 : tree tem = fold_binary (code, type, op0, op1);
15754 : 15888714 : 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 : 167370765 : fold_read_from_constant_string (tree exp)
15778 : : {
15779 : 167370765 : if ((INDIRECT_REF_P (exp)
15780 : 167370747 : || TREE_CODE (exp) == ARRAY_REF)
15781 : 178443762 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15782 : : {
15783 : 7902087 : tree exp1 = TREE_OPERAND (exp, 0);
15784 : 7902087 : tree index;
15785 : 7902087 : tree string;
15786 : 7902087 : location_t loc = EXPR_LOCATION (exp);
15787 : :
15788 : 7902087 : if (INDIRECT_REF_P (exp))
15789 : 0 : string = string_constant (exp1, &index, NULL, NULL);
15790 : : else
15791 : : {
15792 : 7902087 : tree low_bound = array_ref_low_bound (exp);
15793 : 7902087 : 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 : 7902087 : if (! integer_zerop (low_bound))
15803 : 148224 : index = size_diffop_loc (loc, index,
15804 : : fold_convert_loc (loc, sizetype, low_bound));
15805 : :
15806 : : string = exp1;
15807 : : }
15808 : :
15809 : 7902087 : scalar_int_mode char_mode;
15810 : 7902087 : if (string
15811 : 7902087 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15812 : 7902087 : && TREE_CODE (string) == STRING_CST
15813 : 60299 : && tree_fits_uhwi_p (index)
15814 : 56000 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15815 : 7958001 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15816 : : &char_mode)
15817 : 15804174 : && GET_MODE_SIZE (char_mode) == 1)
15818 : 108912 : return build_int_cst_type (TREE_TYPE (exp),
15819 : 54456 : (TREE_STRING_POINTER (string)
15820 : 54456 : [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 : 5316 : fold_read_from_vector (tree arg, poly_uint64 idx)
15829 : : {
15830 : 5316 : unsigned HOST_WIDE_INT i;
15831 : 5316 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15832 : 5316 : && known_ge (idx, 0u)
15833 : 5316 : && idx.is_constant (&i))
15834 : : {
15835 : 5316 : if (TREE_CODE (arg) == VECTOR_CST)
15836 : 1943 : return VECTOR_CST_ELT (arg, i);
15837 : 3373 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15838 : : {
15839 : 1582 : if (CONSTRUCTOR_NELTS (arg)
15840 : 1542 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15841 : : return NULL_TREE;
15842 : 1580 : if (i >= CONSTRUCTOR_NELTS (arg))
15843 : 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15844 : 1540 : 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 : 30867534 : fold_negate_const (tree arg0, tree type)
15857 : : {
15858 : 30867534 : tree t = NULL_TREE;
15859 : :
15860 : 30867534 : switch (TREE_CODE (arg0))
15861 : : {
15862 : 2087497 : case REAL_CST:
15863 : 2087497 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15864 : 2087497 : 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 : 28780037 : default:
15880 : 28780037 : if (poly_int_tree_p (arg0))
15881 : : {
15882 : 28780037 : wi::overflow_type overflow;
15883 : 28780037 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15884 : 28780037 : t = force_fit_type (type, res, 1,
15885 : 223854 : (overflow && ! TYPE_UNSIGNED (type))
15886 : 28995055 : || TREE_OVERFLOW (arg0));
15887 : 28780037 : break;
15888 : 28780037 : }
15889 : :
15890 : 0 : gcc_unreachable ();
15891 : : }
15892 : :
15893 : 30867534 : 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 : 34100 : fold_abs_const (tree arg0, tree type)
15903 : : {
15904 : 34100 : tree t = NULL_TREE;
15905 : :
15906 : 34100 : switch (TREE_CODE (arg0))
15907 : : {
15908 : 6622 : case INTEGER_CST:
15909 : 6622 : {
15910 : : /* If the value is unsigned or non-negative, then the absolute value
15911 : : is the same as the ordinary value. */
15912 : 6622 : wide_int val = wi::to_wide (arg0);
15913 : 6622 : wi::overflow_type overflow = wi::OVF_NONE;
15914 : 6622 : 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 : 2912 : val = wi::neg (val, &overflow);
15921 : :
15922 : : /* Force to the destination type, set TREE_OVERFLOW for signed
15923 : : TYPE only. */
15924 : 6622 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15925 : 6622 : }
15926 : 6622 : break;
15927 : :
15928 : 27478 : case REAL_CST:
15929 : 27478 : 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 : 34100 : 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 : 2222268 : fold_not_const (const_tree arg0, tree type)
15947 : : {
15948 : 2222268 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15949 : :
15950 : 2222268 : 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 : 57660542 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15960 : : {
15961 : 57660542 : 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 : 57660542 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15967 : : {
15968 : 1109396 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15969 : 1109396 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15970 : :
15971 : : /* Handle the cases where either operand is a NaN. */
15972 : 1109396 : if (real_isnan (c0) || real_isnan (c1))
15973 : : {
15974 : 14723 : 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 : 6625 : result = 1;
15989 : : break;
15990 : :
15991 : 8120 : case LT_EXPR:
15992 : 8120 : case LE_EXPR:
15993 : 8120 : case GT_EXPR:
15994 : 8120 : case GE_EXPR:
15995 : 8120 : case LTGT_EXPR:
15996 : 8120 : if (flag_trapping_math)
15997 : : return NULL_TREE;
15998 : : result = 0;
15999 : : break;
16000 : :
16001 : 0 : default:
16002 : 0 : gcc_unreachable ();
16003 : : }
16004 : :
16005 : 6625 : return constant_boolean_node (result, type);
16006 : : }
16007 : :
16008 : 1094673 : return constant_boolean_node (real_compare (code, c0, c1), type);
16009 : : }
16010 : :
16011 : 56551146 : 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 : 56551146 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16020 : : {
16021 : 58302 : tree rcond = fold_relational_const (code, type,
16022 : 29151 : TREE_REALPART (op0),
16023 : 29151 : TREE_REALPART (op1));
16024 : 116604 : tree icond = fold_relational_const (code, type,
16025 : 29151 : TREE_IMAGPART (op0),
16026 : 29151 : TREE_IMAGPART (op1));
16027 : 29151 : if (code == EQ_EXPR)
16028 : 286 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16029 : 28865 : else if (code == NE_EXPR)
16030 : 28865 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16031 : : else
16032 : : return NULL_TREE;
16033 : : }
16034 : :
16035 : 56521995 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16036 : : {
16037 : 5199 : if (!VECTOR_TYPE_P (type))
16038 : : {
16039 : : /* Have vector comparison with scalar boolean result. */
16040 : 182 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
16041 : : && known_eq (VECTOR_CST_NELTS (op0),
16042 : : VECTOR_CST_NELTS (op1)));
16043 : 182 : unsigned HOST_WIDE_INT nunits;
16044 : 182 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
16045 : : return NULL_TREE;
16046 : 845 : for (unsigned i = 0; i < nunits; i++)
16047 : : {
16048 : 758 : tree elem0 = VECTOR_CST_ELT (op0, i);
16049 : 758 : tree elem1 = VECTOR_CST_ELT (op1, i);
16050 : 758 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
16051 : 758 : if (tmp == NULL_TREE)
16052 : : return NULL_TREE;
16053 : 758 : if (integer_zerop (tmp))
16054 : 95 : return constant_boolean_node (code == NE_EXPR, type);
16055 : : }
16056 : 87 : return constant_boolean_node (code == EQ_EXPR, type);
16057 : : }
16058 : 5017 : tree_vector_builder elts;
16059 : 5017 : if (!elts.new_binary_operation (type, op0, op1, false))
16060 : : return NULL_TREE;
16061 : 5017 : unsigned int count = elts.encoded_nelts ();
16062 : 23162 : for (unsigned i = 0; i < count; i++)
16063 : : {
16064 : 18145 : tree elem_type = TREE_TYPE (type);
16065 : 18145 : tree elem0 = VECTOR_CST_ELT (op0, i);
16066 : 18145 : tree elem1 = VECTOR_CST_ELT (op1, i);
16067 : :
16068 : 18145 : tree tem = fold_relational_const (code, elem_type,
16069 : : elem0, elem1);
16070 : :
16071 : 18145 : if (tem == NULL_TREE)
16072 : : return NULL_TREE;
16073 : :
16074 : 18145 : elts.quick_push (build_int_cst (elem_type,
16075 : 25664 : integer_zerop (tem) ? 0 : -1));
16076 : : }
16077 : :
16078 : 5017 : return elts.build ();
16079 : 5017 : }
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 : 56516796 : if (code == LE_EXPR || code == GT_EXPR)
16091 : : {
16092 : 11267105 : std::swap (op0, op1);
16093 : 11267105 : 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 : 56516796 : invert = 0;
16100 : 56516796 : if (code == NE_EXPR || code == GE_EXPR)
16101 : : {
16102 : 27861243 : invert = 1;
16103 : 27861243 : code = invert_tree_comparison (code, false);
16104 : : }
16105 : :
16106 : : /* Compute a result for LT or EQ if args permit;
16107 : : Otherwise return T. */
16108 : 56516796 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16109 : : {
16110 : 56491716 : if (code == EQ_EXPR)
16111 : 28883064 : result = tree_int_cst_equal (op0, op1);
16112 : : else
16113 : 27608652 : result = tree_int_cst_lt (op0, op1);
16114 : : }
16115 : : else
16116 : : return NULL_TREE;
16117 : :
16118 : 56491716 : if (invert)
16119 : 27860523 : result ^= 1;
16120 : 56491716 : 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 : 119459720 : 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 : 119459720 : 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 : 103763181 : if (TREE_CODE (expr) == RETURN_EXPR)
16141 : : {
16142 : 37426206 : tree op = TREE_OPERAND (expr, 0);
16143 : 37426206 : if (!op || !TREE_SIDE_EFFECTS (op))
16144 : : return expr;
16145 : 36739360 : op = TREE_OPERAND (op, 1);
16146 : 36739360 : if (!TREE_SIDE_EFFECTS (op))
16147 : : return expr;
16148 : : }
16149 : :
16150 : 84171067 : 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 : 18974904 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16159 : : {
16160 : 18974904 : tree sub = op0;
16161 : 18974904 : tree subtype;
16162 : 18974904 : poly_uint64 const_op01;
16163 : :
16164 : 18974904 : STRIP_NOPS (sub);
16165 : 18974904 : subtype = TREE_TYPE (sub);
16166 : 18974904 : if (!POINTER_TYPE_P (subtype)
16167 : 18974904 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16168 : : return NULL_TREE;
16169 : :
16170 : 18829253 : if (TREE_CODE (sub) == ADDR_EXPR)
16171 : : {
16172 : 3282389 : tree op = TREE_OPERAND (sub, 0);
16173 : 3282389 : tree optype = TREE_TYPE (op);
16174 : :
16175 : : /* *&CONST_DECL -> to the value of the const decl. */
16176 : 3282389 : if (TREE_CODE (op) == CONST_DECL)
16177 : 2726 : return DECL_INITIAL (op);
16178 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
16179 : 3279663 : if (type == optype)
16180 : : {
16181 : 2386570 : tree fop = fold_read_from_constant_string (op);
16182 : 2386570 : if (fop)
16183 : : return fop;
16184 : : else
16185 : 2344434 : return op;
16186 : : }
16187 : : /* *(foo *)&fooarray => fooarray[0] */
16188 : 893093 : else if (TREE_CODE (optype) == ARRAY_TYPE
16189 : 13156 : && type == TREE_TYPE (optype)
16190 : 905127 : && (!in_gimple_form
16191 : 2609 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16192 : : {
16193 : 12034 : tree type_domain = TYPE_DOMAIN (optype);
16194 : 12034 : tree min_val = size_zero_node;
16195 : 12034 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16196 : 11996 : min_val = TYPE_MIN_VALUE (type_domain);
16197 : 12034 : if (in_gimple_form
16198 : 2609 : && TREE_CODE (min_val) != INTEGER_CST)
16199 : : return NULL_TREE;
16200 : 12034 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16201 : 12034 : NULL_TREE, NULL_TREE);
16202 : : }
16203 : : /* *(foo *)&complexfoo => __real__ complexfoo */
16204 : 881059 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16205 : 881059 : && type == TREE_TYPE (optype))
16206 : 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16207 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16208 : 881059 : else if (VECTOR_TYPE_P (optype)
16209 : 881059 : && 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 : 16427853 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16219 : 16427853 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16220 : : {
16221 : 265556 : tree op00 = TREE_OPERAND (sub, 0);
16222 : 265556 : tree op01 = TREE_OPERAND (sub, 1);
16223 : :
16224 : 265556 : STRIP_NOPS (op00);
16225 : 265556 : if (TREE_CODE (op00) == ADDR_EXPR)
16226 : : {
16227 : 2800 : tree op00type;
16228 : 2800 : op00 = TREE_OPERAND (op00, 0);
16229 : 2800 : op00type = TREE_TYPE (op00);
16230 : :
16231 : : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16232 : 2800 : 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 : 2993 : && 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 : 2621 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16258 : 2621 : && 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 : 2621 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16266 : 2621 : && type == TREE_TYPE (op00type))
16267 : : {
16268 : 1121 : tree type_domain = TYPE_DOMAIN (op00type);
16269 : 1121 : tree min_val = size_zero_node;
16270 : 1121 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16271 : 1120 : min_val = TYPE_MIN_VALUE (type_domain);
16272 : 1121 : poly_uint64 type_size, index;
16273 : 1121 : if (poly_int_tree_p (min_val)
16274 : 1121 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16275 : 1121 : && multiple_p (const_op01, type_size, &index))
16276 : : {
16277 : 1121 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16278 : 1121 : op01 = wide_int_to_tree (sizetype, off);
16279 : 1121 : 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 : 16426553 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16288 : 644281 : && type == TREE_TYPE (TREE_TYPE (subtype))
16289 : 16429502 : && (!in_gimple_form
16290 : 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16291 : : {
16292 : 2948 : tree type_domain;
16293 : 2948 : tree min_val = size_zero_node;
16294 : 2948 : sub = build_fold_indirect_ref_loc (loc, sub);
16295 : 2948 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16296 : 2948 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16297 : 2948 : min_val = TYPE_MIN_VALUE (type_domain);
16298 : 2948 : if (in_gimple_form
16299 : 11 : && TREE_CODE (min_val) != INTEGER_CST)
16300 : : return NULL_TREE;
16301 : 2948 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16302 : 2948 : 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 : 8093163 : build_fold_indirect_ref_loc (location_t loc, tree t)
16313 : : {
16314 : 8093163 : tree type = TREE_TYPE (TREE_TYPE (t));
16315 : 8093163 : tree sub = fold_indirect_ref_1 (loc, type, t);
16316 : :
16317 : 8093163 : if (sub)
16318 : : return sub;
16319 : :
16320 : 5710338 : 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 : 10568551 : fold_indirect_ref_loc (location_t loc, tree t)
16327 : : {
16328 : 10568551 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16329 : :
16330 : 10568551 : if (sub)
16331 : : return sub;
16332 : : else
16333 : 10547731 : 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 : 137795 : fold_ignored_result (tree t)
16342 : : {
16343 : 137795 : if (!TREE_SIDE_EFFECTS (t))
16344 : 19243 : return integer_zero_node;
16345 : :
16346 : 158562 : for (;;)
16347 : 158562 : 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 : 4937 : case tcc_binary:
16354 : 4937 : case tcc_comparison:
16355 : 4937 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16356 : 3046 : 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 : 103073 : case tcc_expression:
16364 : 103073 : switch (TREE_CODE (t))
16365 : : {
16366 : 33173 : case COMPOUND_EXPR:
16367 : 33173 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16368 : : return t;
16369 : 32895 : t = TREE_OPERAND (t, 0);
16370 : 32895 : break;
16371 : :
16372 : 383 : case COND_EXPR:
16373 : 383 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16374 : 383 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16375 : : return t;
16376 : 296 : t = TREE_OPERAND (t, 0);
16377 : 296 : 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 : 2734349893 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16393 : : {
16394 : 2734349893 : tree div = NULL_TREE;
16395 : :
16396 : 2734349893 : 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 : 1699602506 : if (TREE_CODE (value) != INTEGER_CST)
16404 : : {
16405 : 345154 : div = build_int_cst (TREE_TYPE (value), divisor);
16406 : :
16407 : 345154 : 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 : 1699259161 : if (pow2_or_zerop (divisor))
16413 : : {
16414 : 1699259161 : if (TREE_CODE (value) == INTEGER_CST)
16415 : : {
16416 : 1699257352 : wide_int val = wi::to_wide (value);
16417 : 1699257352 : bool overflow_p;
16418 : :
16419 : 1699257352 : if ((val & (divisor - 1)) == 0)
16420 : : return value;
16421 : :
16422 : 3659837 : overflow_p = TREE_OVERFLOW (value);
16423 : 3659837 : val += divisor - 1;
16424 : 3659837 : val &= (int) -divisor;
16425 : 3659837 : if (val == 0)
16426 : 6 : overflow_p = true;
16427 : :
16428 : 3659837 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16429 : 1699257352 : }
16430 : : else
16431 : : {
16432 : 1809 : tree t;
16433 : :
16434 : 1809 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16435 : 1809 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16436 : 1809 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16437 : 1809 : 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 : 17699784 : round_down_loc (location_t loc, tree value, int divisor)
16455 : : {
16456 : 17699784 : tree div = NULL_TREE;
16457 : :
16458 : 17699784 : gcc_assert (divisor > 0);
16459 : 17699784 : 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 : 17699784 : 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 : 17699784 : if (pow2_or_zerop (divisor))
16476 : : {
16477 : 17699784 : tree t;
16478 : :
16479 : 17699784 : t = build_int_cst (TREE_TYPE (value), -divisor);
16480 : 17699784 : 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 : 1660346 : split_address_to_core_and_offset (tree exp,
16499 : : poly_int64 *pbitpos, tree *poffset)
16500 : : {
16501 : 1660346 : tree core;
16502 : 1660346 : machine_mode mode;
16503 : 1660346 : int unsignedp, reversep, volatilep;
16504 : 1660346 : poly_int64 bitsize;
16505 : 1660346 : location_t loc = EXPR_LOCATION (exp);
16506 : :
16507 : 1660346 : if (TREE_CODE (exp) == SSA_NAME)
16508 : 514751 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16509 : 371566 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16510 : 30094 : exp = gimple_assign_rhs1 (def);
16511 : :
16512 : 1660346 : if (TREE_CODE (exp) == ADDR_EXPR)
16513 : : {
16514 : 973933 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16515 : : poffset, &mode, &unsignedp, &reversep,
16516 : : &volatilep);
16517 : : /* If we are left with MEM[a + CST] strip that and add it to the
16518 : : pbitpos and return a. */
16519 : 973933 : if (TREE_CODE (core) == MEM_REF)
16520 : : {
16521 : 27142 : poly_offset_int tem;
16522 : 27142 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16523 : 27142 : tem <<= LOG2_BITS_PER_UNIT;
16524 : 27142 : tem += *pbitpos;
16525 : 27142 : if (tem.to_shwi (pbitpos))
16526 : 26974 : return TREE_OPERAND (core, 0);
16527 : : }
16528 : 946959 : core = build_fold_addr_expr_loc (loc, core);
16529 : : }
16530 : 686413 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16531 : : {
16532 : 92203 : core = TREE_OPERAND (exp, 0);
16533 : 92203 : STRIP_NOPS (core);
16534 : 92203 : *pbitpos = 0;
16535 : 92203 : *poffset = TREE_OPERAND (exp, 1);
16536 : 92203 : if (poly_int_tree_p (*poffset))
16537 : : {
16538 : 92113 : poly_offset_int tem
16539 : 92113 : = wi::sext (wi::to_poly_offset (*poffset),
16540 : 92113 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16541 : 92113 : tem <<= LOG2_BITS_PER_UNIT;
16542 : 92113 : if (tem.to_shwi (pbitpos))
16543 : 92113 : *poffset = NULL_TREE;
16544 : : }
16545 : : }
16546 : : else
16547 : : {
16548 : 594210 : core = exp;
16549 : 594210 : *pbitpos = 0;
16550 : 594210 : *poffset = NULL_TREE;
16551 : : }
16552 : :
16553 : : return core;
16554 : : }
16555 : :
16556 : : /* Returns true if addresses of E1 and E2 differ by a constant, false
16557 : : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16558 : :
16559 : : bool
16560 : 830173 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16561 : : {
16562 : 830173 : tree core1, core2;
16563 : 830173 : poly_int64 bitpos1, bitpos2;
16564 : 830173 : tree toffset1, toffset2, tdiff, type;
16565 : :
16566 : 830173 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16567 : 830173 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16568 : :
16569 : 830173 : poly_int64 bytepos1, bytepos2;
16570 : 830173 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16571 : 1419770 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16572 : 1660346 : || !operand_equal_p (core1, core2, 0))
16573 : 589597 : return false;
16574 : :
16575 : 240576 : if (toffset1 && toffset2)
16576 : : {
16577 : 29 : type = TREE_TYPE (toffset1);
16578 : 29 : if (type != TREE_TYPE (toffset2))
16579 : 0 : toffset2 = fold_convert (type, toffset2);
16580 : :
16581 : 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16582 : 29 : if (!cst_and_fits_in_hwi (tdiff))
16583 : : return false;
16584 : :
16585 : 15 : *diff = int_cst_value (tdiff);
16586 : : }
16587 : 240547 : else if (toffset1 || toffset2)
16588 : : {
16589 : : /* If only one of the offsets is non-constant, the difference cannot
16590 : : be a constant. */
16591 : : return false;
16592 : : }
16593 : : else
16594 : 222794 : *diff = 0;
16595 : :
16596 : 222809 : *diff += bytepos1 - bytepos2;
16597 : 222809 : return true;
16598 : : }
16599 : :
16600 : : /* Return OFF converted to a pointer offset type suitable as offset for
16601 : : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16602 : : tree
16603 : 19376989 : convert_to_ptrofftype_loc (location_t loc, tree off)
16604 : : {
16605 : 19376989 : if (ptrofftype_p (TREE_TYPE (off)))
16606 : : return off;
16607 : 2227052 : return fold_convert_loc (loc, sizetype, off);
16608 : : }
16609 : :
16610 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16611 : : tree
16612 : 17280825 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16613 : : {
16614 : 17280825 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16615 : 17280825 : ptr, convert_to_ptrofftype_loc (loc, off));
16616 : : }
16617 : :
16618 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16619 : : tree
16620 : 162694 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16621 : : {
16622 : 162694 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16623 : 162694 : ptr, size_int (off));
16624 : : }
16625 : :
16626 : : /* Return a pointer to a NUL-terminated string containing the sequence
16627 : : of bytes corresponding to the representation of the object referred to
16628 : : by SRC (or a subsequence of such bytes within it if SRC is a reference
16629 : : to an initialized constant array plus some constant offset).
16630 : : Set *STRSIZE the number of bytes in the constant sequence including
16631 : : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16632 : : where A is the array that stores the constant sequence that SRC points
16633 : : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16634 : : need not point to a string or even an array of characters but may point
16635 : : to an object of any type. */
16636 : :
16637 : : const char *
16638 : 12680414 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16639 : : {
16640 : : /* The offset into the array A storing the string, and A's byte size. */
16641 : 12680414 : tree offset_node;
16642 : 12680414 : tree mem_size;
16643 : :
16644 : 12680414 : if (strsize)
16645 : 4868595 : *strsize = 0;
16646 : :
16647 : 12680414 : if (strsize)
16648 : 4868595 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16649 : : else
16650 : 7811819 : src = string_constant (src, &offset_node, &mem_size, NULL);
16651 : 12680414 : if (!src)
16652 : : return NULL;
16653 : :
16654 : 2855428 : unsigned HOST_WIDE_INT offset = 0;
16655 : 2855428 : if (offset_node != NULL_TREE)
16656 : : {
16657 : 2855428 : if (!tree_fits_uhwi_p (offset_node))
16658 : : return NULL;
16659 : : else
16660 : 2853664 : offset = tree_to_uhwi (offset_node);
16661 : : }
16662 : :
16663 : 2853664 : if (!tree_fits_uhwi_p (mem_size))
16664 : : return NULL;
16665 : :
16666 : : /* ARRAY_SIZE is the byte size of the array the constant sequence
16667 : : is stored in and equal to sizeof A. INIT_BYTES is the number
16668 : : of bytes in the constant sequence used to initialize the array,
16669 : : including any embedded NULs as well as the terminating NUL (for
16670 : : strings), but not including any trailing zeros/NULs past
16671 : : the terminating one appended implicitly to a string literal to
16672 : : zero out the remainder of the array it's stored in. For example,
16673 : : given:
16674 : : const char a[7] = "abc\0d";
16675 : : n = strlen (a + 1);
16676 : : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16677 : : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16678 : : is equal to strlen (A) + 1. */
16679 : 2853664 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16680 : 2853664 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16681 : 2853664 : const char *string = TREE_STRING_POINTER (src);
16682 : :
16683 : : /* Ideally this would turn into a gcc_checking_assert over time. */
16684 : 2853664 : if (init_bytes > array_size)
16685 : : init_bytes = array_size;
16686 : :
16687 : 2853664 : if (init_bytes == 0 || offset >= array_size)
16688 : : return NULL;
16689 : :
16690 : 2852468 : if (strsize)
16691 : : {
16692 : : /* Compute and store the number of characters from the beginning
16693 : : of the substring at OFFSET to the end, including the terminating
16694 : : nul. Offsets past the initial length refer to null strings. */
16695 : 1563407 : if (offset < init_bytes)
16696 : 1563407 : *strsize = init_bytes - offset;
16697 : : else
16698 : 0 : *strsize = 1;
16699 : : }
16700 : : else
16701 : : {
16702 : 1289061 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16703 : : /* Support only properly NUL-terminated single byte strings. */
16704 : 1289061 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16705 : : return NULL;
16706 : 1283808 : if (string[init_bytes - 1] != '\0')
16707 : : return NULL;
16708 : : }
16709 : :
16710 : 2825899 : return offset < init_bytes ? string + offset : "";
16711 : : }
16712 : :
16713 : : /* Return a pointer to a NUL-terminated string corresponding to
16714 : : the expression STR referencing a constant string, possibly
16715 : : involving a constant offset. Return null if STR either doesn't
16716 : : reference a constant string or if it involves a nonconstant
16717 : : offset. */
16718 : :
16719 : : const char *
16720 : 7811819 : c_getstr (tree str)
16721 : : {
16722 : 7811819 : return getbyterep (str, NULL);
16723 : : }
16724 : :
16725 : : /* Given a tree T, compute which bits in T may be nonzero. */
16726 : :
16727 : : wide_int
16728 : 236723754 : tree_nonzero_bits (const_tree t)
16729 : : {
16730 : 236723754 : switch (TREE_CODE (t))
16731 : : {
16732 : 8478708 : case INTEGER_CST:
16733 : 8478708 : return wi::to_wide (t);
16734 : 133896016 : case SSA_NAME:
16735 : 133896016 : return get_nonzero_bits (t);
16736 : 242984 : case NON_LVALUE_EXPR:
16737 : 242984 : case SAVE_EXPR:
16738 : 242984 : return tree_nonzero_bits (TREE_OPERAND (t, 0));
16739 : 498727 : case BIT_AND_EXPR:
16740 : 997454 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16741 : 1496181 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16742 : 4260 : case BIT_IOR_EXPR:
16743 : 4260 : case BIT_XOR_EXPR:
16744 : 8520 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16745 : 12780 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16746 : 62754 : case COND_EXPR:
16747 : 125508 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16748 : 188262 : tree_nonzero_bits (TREE_OPERAND (t, 2)));
16749 : 49515067 : CASE_CONVERT:
16750 : 99030134 : return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16751 : 49515067 : TYPE_PRECISION (TREE_TYPE (t)),
16752 : 148545201 : TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16753 : 13314028 : case PLUS_EXPR:
16754 : 13314028 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16755 : : {
16756 : 13314028 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16757 : 13314028 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16758 : 13314028 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16759 : 497410 : return wi::bit_or (nzbits1, nzbits2);
16760 : 13314028 : }
16761 : : break;
16762 : 161080 : case LSHIFT_EXPR:
16763 : 161080 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16764 : : {
16765 : 93130 : tree type = TREE_TYPE (t);
16766 : 93130 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16767 : 186260 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16768 : 93130 : TYPE_PRECISION (type));
16769 : 93130 : return wi::neg_p (arg1)
16770 : 186260 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16771 : 93130 : : wi::lshift (nzbits, arg1);
16772 : 93130 : }
16773 : : break;
16774 : 154395 : case RSHIFT_EXPR:
16775 : 154395 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16776 : : {
16777 : 152837 : tree type = TREE_TYPE (t);
16778 : 152837 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16779 : 305674 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16780 : 152837 : TYPE_PRECISION (type));
16781 : 152837 : return wi::neg_p (arg1)
16782 : 305674 : ? wi::lshift (nzbits, -arg1)
16783 : 152837 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16784 : 152837 : }
16785 : : break;
16786 : : default:
16787 : : break;
16788 : : }
16789 : :
16790 : 43281861 : return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16791 : : }
16792 : :
16793 : : /* Helper function for address compare simplifications in match.pd.
16794 : : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16795 : : TYPE is the type of comparison operands.
16796 : : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16797 : : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16798 : : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16799 : : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16800 : : and 2 if unknown. */
16801 : :
16802 : : int
16803 : 1326956 : address_compare (tree_code code, tree type, tree op0, tree op1,
16804 : : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16805 : : bool generic)
16806 : : {
16807 : 1326956 : if (TREE_CODE (op0) == SSA_NAME)
16808 : 19634 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16809 : 1326956 : if (TREE_CODE (op1) == SSA_NAME)
16810 : 3848 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16811 : 1326956 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16812 : 1326956 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16813 : 1326956 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16814 : 1326956 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16815 : 1326956 : if (base0 && TREE_CODE (base0) == MEM_REF)
16816 : : {
16817 : 18713 : off0 += mem_ref_offset (base0).force_shwi ();
16818 : 18713 : base0 = TREE_OPERAND (base0, 0);
16819 : : }
16820 : 1326956 : if (base1 && TREE_CODE (base1) == MEM_REF)
16821 : : {
16822 : 2446 : off1 += mem_ref_offset (base1).force_shwi ();
16823 : 2446 : base1 = TREE_OPERAND (base1, 0);
16824 : : }
16825 : 1326956 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16826 : : return 2;
16827 : :
16828 : 1319218 : int equal = 2;
16829 : : /* Punt in GENERIC on variables with value expressions;
16830 : : the value expressions might point to fields/elements
16831 : : of other vars etc. */
16832 : 1319218 : if (generic
16833 : 1319218 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16834 : 1190020 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16835 : : return 2;
16836 : 1318660 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16837 : : {
16838 : 96933 : symtab_node *node0 = symtab_node::get_create (base0);
16839 : 96933 : symtab_node *node1 = symtab_node::get_create (base1);
16840 : 96933 : equal = node0->equal_address_to (node1);
16841 : : }
16842 : 1221727 : else if ((DECL_P (base0)
16843 : 216636 : || TREE_CODE (base0) == SSA_NAME
16844 : 198790 : || TREE_CODE (base0) == STRING_CST)
16845 : 1221565 : && (DECL_P (base1)
16846 : 201269 : || TREE_CODE (base1) == SSA_NAME
16847 : 199049 : || TREE_CODE (base1) == STRING_CST))
16848 : 1221543 : equal = (base0 == base1);
16849 : : /* Assume different STRING_CSTs with the same content will be
16850 : : merged. */
16851 : 1318476 : if (equal == 0
16852 : 52081 : && TREE_CODE (base0) == STRING_CST
16853 : 17361 : && TREE_CODE (base1) == STRING_CST
16854 : 17332 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16855 : 1318476 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16856 : 6305 : TREE_STRING_LENGTH (base0)) == 0)
16857 : : equal = 1;
16858 : 1314017 : if (equal == 1)
16859 : : {
16860 : 1246471 : if (code == EQ_EXPR
16861 : 1246471 : || code == NE_EXPR
16862 : : /* If the offsets are equal we can ignore overflow. */
16863 : 126934 : || known_eq (off0, off1)
16864 : 253642 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16865 : : /* Or if we compare using pointers to decls or strings. */
16866 : 1373292 : || (POINTER_TYPE_P (type)
16867 : 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16868 : : return 1;
16869 : : return 2;
16870 : : }
16871 : 72189 : if (equal != 0)
16872 : : return equal;
16873 : 47438 : if (code != EQ_EXPR && code != NE_EXPR)
16874 : : return 2;
16875 : :
16876 : : /* At this point we know (or assume) the two pointers point at
16877 : : different objects. */
16878 : 43313 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16879 : 43313 : off0.is_constant (&ioff0);
16880 : 43313 : off1.is_constant (&ioff1);
16881 : : /* Punt on non-zero offsets from functions. */
16882 : 43313 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16883 : 43313 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16884 : : return 2;
16885 : : /* Or if the bases are neither decls nor string literals. */
16886 : 43313 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16887 : : return 2;
16888 : 25946 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16889 : : return 2;
16890 : : /* For initializers, assume addresses of different functions are
16891 : : different. */
16892 : 25946 : if (folding_initializer
16893 : 4263 : && TREE_CODE (base0) == FUNCTION_DECL
16894 : 14 : && TREE_CODE (base1) == FUNCTION_DECL)
16895 : : return 0;
16896 : :
16897 : : /* Compute whether one address points to the start of one
16898 : : object and another one to the end of another one. */
16899 : 25932 : poly_int64 size0 = 0, size1 = 0;
16900 : 25932 : if (TREE_CODE (base0) == STRING_CST)
16901 : : {
16902 : 12694 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16903 : : equal = 2;
16904 : : else
16905 : : size0 = TREE_STRING_LENGTH (base0);
16906 : : }
16907 : 13238 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16908 : : size0 = 1;
16909 : : else
16910 : : {
16911 : 13165 : tree sz0 = DECL_SIZE_UNIT (base0);
16912 : 13165 : if (!tree_fits_poly_int64_p (sz0))
16913 : : equal = 2;
16914 : : else
16915 : 13165 : size0 = tree_to_poly_int64 (sz0);
16916 : : }
16917 : 25932 : if (TREE_CODE (base1) == STRING_CST)
16918 : : {
16919 : 12815 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16920 : : equal = 2;
16921 : : else
16922 : : size1 = TREE_STRING_LENGTH (base1);
16923 : : }
16924 : 13117 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16925 : : size1 = 1;
16926 : : else
16927 : : {
16928 : 13048 : tree sz1 = DECL_SIZE_UNIT (base1);
16929 : 13048 : if (!tree_fits_poly_int64_p (sz1))
16930 : : equal = 2;
16931 : : else
16932 : 13048 : size1 = tree_to_poly_int64 (sz1);
16933 : : }
16934 : 25932 : if (equal == 0)
16935 : : {
16936 : : /* If one offset is pointing (or could be) to the beginning of one
16937 : : object and the other is pointing to one past the last byte of the
16938 : : other object, punt. */
16939 : 25920 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16940 : : equal = 2;
16941 : 25783 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16942 : : equal = 2;
16943 : : /* If both offsets are the same, there are some cases we know that are
16944 : : ok. Either if we know they aren't zero, or if we know both sizes
16945 : : are no zero. */
16946 : : if (equal == 2
16947 : 273 : && known_eq (off0, off1)
16948 : 22 : && (known_ne (off0, 0)
16949 : 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16950 : : equal = 0;
16951 : : }
16952 : :
16953 : : /* At this point, equal is 2 if either one or both pointers are out of
16954 : : bounds of their object, or one points to start of its object and the
16955 : : other points to end of its object. This is unspecified behavior
16956 : : e.g. in C++. Otherwise equal is 0. */
16957 : 25932 : if (folding_cxx_constexpr && equal)
16958 : : return equal;
16959 : :
16960 : : /* When both pointers point to string literals, even when equal is 0,
16961 : : due to tail merging of string literals the pointers might be the same. */
16962 : 25869 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16963 : : {
16964 : 12671 : if (ioff0 < 0
16965 : 12671 : || ioff1 < 0
16966 : 12671 : || ioff0 > TREE_STRING_LENGTH (base0)
16967 : 25330 : || ioff1 > TREE_STRING_LENGTH (base1))
16968 : : return 2;
16969 : :
16970 : : /* If the bytes in the string literals starting at the pointers
16971 : : differ, the pointers need to be different. */
16972 : 12659 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16973 : 12659 : TREE_STRING_POINTER (base1) + ioff1,
16974 : 12659 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16975 : : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16976 : : {
16977 : 3892 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16978 : 3892 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16979 : 3892 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16980 : : ioffmin) == 0)
16981 : : /* If even the bytes in the string literal before the
16982 : : pointers are the same, the string literals could be
16983 : : tail merged. */
16984 : : return 2;
16985 : : }
16986 : : return 0;
16987 : : }
16988 : :
16989 : 13198 : if (folding_cxx_constexpr)
16990 : : return 0;
16991 : :
16992 : : /* If this is a pointer comparison, ignore for now even
16993 : : valid equalities where one pointer is the offset zero
16994 : : of one object and the other to one past end of another one. */
16995 : 9055 : if (!INTEGRAL_TYPE_P (type))
16996 : : return 0;
16997 : :
16998 : : /* Assume that string literals can't be adjacent to variables
16999 : : (automatic or global). */
17000 : 299 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
17001 : : return 0;
17002 : :
17003 : : /* Assume that automatic variables can't be adjacent to global
17004 : : variables. */
17005 : 295 : if (is_global_var (base0) != is_global_var (base1))
17006 : : return 0;
17007 : :
17008 : : return equal;
17009 : : }
17010 : :
17011 : : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
17012 : : tree
17013 : 44 : ctor_single_nonzero_element (const_tree t)
17014 : : {
17015 : 44 : unsigned HOST_WIDE_INT idx;
17016 : 44 : constructor_elt *ce;
17017 : 44 : tree elt = NULL_TREE;
17018 : :
17019 : 44 : if (TREE_CODE (t) != CONSTRUCTOR)
17020 : : return NULL_TREE;
17021 : 97 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
17022 : 94 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
17023 : : {
17024 : 85 : if (elt)
17025 : : return NULL_TREE;
17026 : 44 : elt = ce->value;
17027 : : }
17028 : : return elt;
17029 : : }
17030 : :
17031 : : #if CHECKING_P
17032 : :
17033 : : namespace selftest {
17034 : :
17035 : : /* Helper functions for writing tests of folding trees. */
17036 : :
17037 : : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17038 : :
17039 : : static void
17040 : 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
17041 : : tree constant)
17042 : : {
17043 : 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
17044 : 16 : }
17045 : :
17046 : : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17047 : : wrapping WRAPPED_EXPR. */
17048 : :
17049 : : static void
17050 : 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
17051 : : tree wrapped_expr)
17052 : : {
17053 : 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
17054 : 12 : ASSERT_NE (wrapped_expr, result);
17055 : 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
17056 : 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
17057 : 12 : }
17058 : :
17059 : : /* Verify that various arithmetic binary operations are folded
17060 : : correctly. */
17061 : :
17062 : : static void
17063 : 4 : test_arithmetic_folding ()
17064 : : {
17065 : 4 : tree type = integer_type_node;
17066 : 4 : tree x = create_tmp_var_raw (type, "x");
17067 : 4 : tree zero = build_zero_cst (type);
17068 : 4 : tree one = build_int_cst (type, 1);
17069 : :
17070 : : /* Addition. */
17071 : : /* 1 <-- (0 + 1) */
17072 : 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
17073 : : one);
17074 : 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
17075 : : one);
17076 : :
17077 : : /* (nonlvalue)x <-- (x + 0) */
17078 : 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
17079 : : x);
17080 : :
17081 : : /* Subtraction. */
17082 : : /* 0 <-- (x - x) */
17083 : 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
17084 : : zero);
17085 : 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
17086 : : x);
17087 : :
17088 : : /* Multiplication. */
17089 : : /* 0 <-- (x * 0) */
17090 : 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
17091 : : zero);
17092 : :
17093 : : /* (nonlvalue)x <-- (x * 1) */
17094 : 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
17095 : : x);
17096 : 4 : }
17097 : :
17098 : : namespace test_operand_equality {
17099 : :
17100 : : /* Verify structural equality. */
17101 : :
17102 : : /* Execute fold_vec_perm_cst unit tests. */
17103 : :
17104 : : static void
17105 : 4 : test ()
17106 : : {
17107 : 4 : tree stype = integer_type_node;
17108 : 4 : tree utype = unsigned_type_node;
17109 : 4 : tree x = create_tmp_var_raw (stype, "x");
17110 : 4 : tree y = create_tmp_var_raw (stype, "y");
17111 : 4 : tree z = create_tmp_var_raw (stype, "z");
17112 : 4 : tree four = build_int_cst (stype, 4);
17113 : 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
17114 : 4 : tree rhs1 = fold_convert (stype,
17115 : : fold_build2 (PLUS_EXPR, utype,
17116 : : fold_convert (utype, x),
17117 : : fold_convert (utype, y)));
17118 : :
17119 : : /* (int)((unsigned x) + (unsigned y)) == x + y. */
17120 : 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17121 : 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17122 : :
17123 : : /* (int)(unsigned) x == x. */
17124 : 4 : tree lhs2 = build1 (NOP_EXPR, stype,
17125 : : build1 (NOP_EXPR, utype, x));
17126 : 4 : tree rhs2 = x;
17127 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17128 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17129 : :
17130 : : /* (unsigned x) + (unsigned y) == x + y. */
17131 : 4 : tree lhs3 = lhs1;
17132 : 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17133 : : fold_convert (utype, x),
17134 : : fold_convert (utype, y));
17135 : 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17136 : 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17137 : :
17138 : : /* (unsigned x) / (unsigned y) == x / y. */
17139 : 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17140 : 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17141 : : fold_convert (utype, x),
17142 : : fold_convert (utype, y));
17143 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17144 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17145 : :
17146 : : /* (long x) / 4 == (long)(x / 4). */
17147 : 4 : tree lstype = long_long_integer_type_node;
17148 : 4 : tree lfour = build_int_cst (lstype, 4);
17149 : 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17150 : : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17151 : 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17152 : : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17153 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17154 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17155 : :
17156 : : /* (unsigned x) / 4 == x / 4. */
17157 : 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17158 : 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17159 : : fold_convert (utype, x),
17160 : : fold_convert (utype, four));
17161 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17162 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17163 : :
17164 : : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17165 : 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17166 : 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17167 : 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17168 : 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17169 : :
17170 : : /* (unsigned x) + 4 == x + 4. */
17171 : 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17172 : 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17173 : : fold_convert (utype, x),
17174 : : fold_convert (utype, four));
17175 : 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17176 : 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17177 : :
17178 : : /* (unsigned x) + 4 == 4 + x. */
17179 : 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17180 : 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17181 : : fold_convert (utype, x),
17182 : : fold_convert (utype, four));
17183 : 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17184 : 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17185 : :
17186 : : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17187 : 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17188 : : fold_build2 (MULT_EXPR, stype,
17189 : : fold_build2 (PLUS_EXPR, stype, four, x),
17190 : : y),
17191 : : z);
17192 : 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17193 : : fold_build2 (PLUS_EXPR, utype,
17194 : : fold_convert (utype, x),
17195 : : fold_convert (utype, four)),
17196 : : fold_convert (utype, y));
17197 : 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17198 : 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17199 : 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17200 : 4 : }
17201 : : }
17202 : :
17203 : : namespace test_fold_vec_perm_cst {
17204 : :
17205 : : /* Build a VECTOR_CST corresponding to VMODE, and has
17206 : : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17207 : : Fill it with randomized elements, using rand() % THRESHOLD. */
17208 : :
17209 : : static tree
17210 : 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17211 : : unsigned nelts_per_pattern,
17212 : : int step = 0, bool natural_stepped = false,
17213 : : int threshold = 100)
17214 : : {
17215 : 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17216 : 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17217 : 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17218 : :
17219 : : // Fill a0 for each pattern
17220 : 0 : for (unsigned i = 0; i < npatterns; i++)
17221 : 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17222 : :
17223 : 0 : if (nelts_per_pattern == 1)
17224 : 0 : return builder.build ();
17225 : :
17226 : : // Fill a1 for each pattern
17227 : 0 : for (unsigned i = 0; i < npatterns; i++)
17228 : : {
17229 : 0 : tree a1;
17230 : 0 : if (natural_stepped)
17231 : : {
17232 : 0 : tree a0 = builder[i];
17233 : 0 : wide_int a0_val = wi::to_wide (a0);
17234 : 0 : wide_int a1_val = a0_val + step;
17235 : 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17236 : 0 : }
17237 : : else
17238 : 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17239 : 0 : builder.quick_push (a1);
17240 : : }
17241 : 0 : if (nelts_per_pattern == 2)
17242 : 0 : return builder.build ();
17243 : :
17244 : 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17245 : : {
17246 : 0 : tree prev_elem = builder[i - npatterns];
17247 : 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17248 : 0 : wide_int val = prev_elem_val + step;
17249 : 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17250 : 0 : }
17251 : :
17252 : 0 : return builder.build ();
17253 : 0 : }
17254 : :
17255 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17256 : : when result is VLA. */
17257 : :
17258 : : static void
17259 : 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17260 : : tree res, tree *expected_res)
17261 : : {
17262 : : /* Actual npatterns and encoded_elts in res may be less than expected due
17263 : : to canonicalization. */
17264 : 0 : ASSERT_TRUE (res != NULL_TREE);
17265 : 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17266 : 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17267 : :
17268 : 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17269 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17270 : 0 : }
17271 : :
17272 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17273 : : when the result is VLS. */
17274 : :
17275 : : static void
17276 : 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17277 : : {
17278 : 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17279 : 0 : for (unsigned i = 0; i < expected_nelts; i++)
17280 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17281 : 0 : }
17282 : :
17283 : : /* Helper routine to push multiple elements into BUILDER. */
17284 : : template<unsigned N>
17285 : 0 : static void builder_push_elems (vec_perm_builder& builder,
17286 : : poly_uint64 (&elems)[N])
17287 : : {
17288 : 0 : for (unsigned i = 0; i < N; i++)
17289 : 0 : builder.quick_push (elems[i]);
17290 : 0 : }
17291 : :
17292 : : #define ARG0(index) vector_cst_elt (arg0, index)
17293 : : #define ARG1(index) vector_cst_elt (arg1, index)
17294 : :
17295 : : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17296 : :
17297 : : static void
17298 : 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17299 : : {
17300 : 0 : for (int i = 0; i < 10; i++)
17301 : : {
17302 : : /* Case 1:
17303 : : sel = { 0, 4, 1, 5, ... }
17304 : : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17305 : 0 : {
17306 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17307 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17308 : :
17309 : 0 : tree inner_type
17310 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17311 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17312 : :
17313 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17314 : 0 : vec_perm_builder builder (res_len, 4, 1);
17315 : 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17316 : 0 : builder_push_elems (builder, mask_elems);
17317 : :
17318 : 0 : vec_perm_indices sel (builder, 2, res_len);
17319 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17320 : :
17321 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17322 : 0 : validate_res (4, 1, res, expected_res);
17323 : 0 : }
17324 : :
17325 : : /* Case 2: Same as case 1, but contains an out of bounds access which
17326 : : should wrap around.
17327 : : sel = {0, 8, 4, 12, ...} (4, 1)
17328 : : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17329 : 0 : {
17330 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17331 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17332 : :
17333 : 0 : tree inner_type
17334 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17335 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17336 : :
17337 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17338 : 0 : vec_perm_builder builder (res_len, 4, 1);
17339 : 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17340 : 0 : builder_push_elems (builder, mask_elems);
17341 : :
17342 : 0 : vec_perm_indices sel (builder, 2, res_len);
17343 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17344 : :
17345 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17346 : 0 : validate_res (4, 1, res, expected_res);
17347 : 0 : }
17348 : : }
17349 : 0 : }
17350 : :
17351 : : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17352 : :
17353 : : static void
17354 : 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17355 : : {
17356 : 0 : for (int i = 0; i < 10; i++)
17357 : : {
17358 : : /* Case 1:
17359 : : sel = { 0, 1, 2, 3}
17360 : : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17361 : 0 : {
17362 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17363 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17364 : :
17365 : 0 : tree inner_type
17366 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17367 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17368 : :
17369 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17370 : 0 : vec_perm_builder builder (res_len, 4, 1);
17371 : 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17372 : 0 : builder_push_elems (builder, mask_elems);
17373 : :
17374 : 0 : vec_perm_indices sel (builder, 2, res_len);
17375 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17376 : :
17377 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17378 : 0 : validate_res_vls (res, expected_res, 4);
17379 : 0 : }
17380 : :
17381 : : /* Case 2: Same as Case 1, but crossing input vector.
17382 : : sel = {0, 2, 4, 6}
17383 : : In this case,the index 4 is ambiguous since len = 4 + 4x.
17384 : : Since we cannot determine, which vector to choose from during
17385 : : compile time, should return NULL_TREE. */
17386 : 0 : {
17387 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17388 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17389 : :
17390 : 0 : tree inner_type
17391 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17392 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17393 : :
17394 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17395 : 0 : vec_perm_builder builder (res_len, 4, 1);
17396 : 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17397 : 0 : builder_push_elems (builder, mask_elems);
17398 : :
17399 : 0 : vec_perm_indices sel (builder, 2, res_len);
17400 : 0 : const char *reason;
17401 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17402 : :
17403 : 0 : ASSERT_TRUE (res == NULL_TREE);
17404 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17405 : 0 : }
17406 : : }
17407 : 0 : }
17408 : :
17409 : : /* Test all input vectors. */
17410 : :
17411 : : static void
17412 : 0 : test_all_nunits (machine_mode vmode)
17413 : : {
17414 : : /* Test with 10 different inputs. */
17415 : 0 : for (int i = 0; i < 10; i++)
17416 : : {
17417 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17418 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17419 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17420 : :
17421 : : /* Case 1: mask = {0, ...} // (1, 1)
17422 : : res = { arg0[0], ... } // (1, 1) */
17423 : 0 : {
17424 : 0 : vec_perm_builder builder (len, 1, 1);
17425 : 0 : builder.quick_push (0);
17426 : 0 : vec_perm_indices sel (builder, 2, len);
17427 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17428 : 0 : tree expected_res[] = { ARG0(0) };
17429 : 0 : validate_res (1, 1, res, expected_res);
17430 : 0 : }
17431 : :
17432 : : /* Case 2: mask = {len, ...} // (1, 1)
17433 : : res = { arg1[0], ... } // (1, 1) */
17434 : 0 : {
17435 : 0 : vec_perm_builder builder (len, 1, 1);
17436 : 0 : builder.quick_push (len);
17437 : 0 : vec_perm_indices sel (builder, 2, len);
17438 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17439 : :
17440 : 0 : tree expected_res[] = { ARG1(0) };
17441 : 0 : validate_res (1, 1, res, expected_res);
17442 : 0 : }
17443 : : }
17444 : 0 : }
17445 : :
17446 : : /* Test all vectors which contain at-least 2 elements. */
17447 : :
17448 : : static void
17449 : 0 : test_nunits_min_2 (machine_mode vmode)
17450 : : {
17451 : 0 : for (int i = 0; i < 10; i++)
17452 : : {
17453 : : /* Case 1: mask = { 0, len, ... } // (2, 1)
17454 : : res = { arg0[0], arg1[0], ... } // (2, 1) */
17455 : 0 : {
17456 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17457 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17458 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17459 : :
17460 : 0 : vec_perm_builder builder (len, 2, 1);
17461 : 0 : poly_uint64 mask_elems[] = { 0, len };
17462 : 0 : builder_push_elems (builder, mask_elems);
17463 : :
17464 : 0 : vec_perm_indices sel (builder, 2, len);
17465 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17466 : :
17467 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17468 : 0 : validate_res (2, 1, res, expected_res);
17469 : 0 : }
17470 : :
17471 : : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17472 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17473 : 0 : {
17474 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17475 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17476 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17477 : :
17478 : 0 : vec_perm_builder builder (len, 2, 2);
17479 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17480 : 0 : builder_push_elems (builder, mask_elems);
17481 : :
17482 : 0 : vec_perm_indices sel (builder, 2, len);
17483 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17484 : :
17485 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17486 : 0 : validate_res (2, 2, res, expected_res);
17487 : 0 : }
17488 : :
17489 : : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17490 : : Test that the stepped sequence of the pattern selects from
17491 : : same input pattern. Since input vectors have npatterns = 2,
17492 : : and step (a2 - a1) = 1, step is not a multiple of npatterns
17493 : : in input vector. So return NULL_TREE. */
17494 : 0 : {
17495 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17496 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17497 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17498 : :
17499 : 0 : vec_perm_builder builder (len, 1, 3);
17500 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17501 : 0 : builder_push_elems (builder, mask_elems);
17502 : :
17503 : 0 : vec_perm_indices sel (builder, 2, len);
17504 : 0 : const char *reason;
17505 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17506 : : &reason);
17507 : 0 : ASSERT_TRUE (res == NULL_TREE);
17508 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17509 : 0 : }
17510 : :
17511 : : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17512 : : Test that stepped sequence of the pattern selects from arg0.
17513 : : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17514 : 0 : {
17515 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17516 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17517 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17518 : :
17519 : 0 : vec_perm_builder builder (len, 1, 3);
17520 : 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17521 : 0 : builder_push_elems (builder, mask_elems);
17522 : :
17523 : 0 : vec_perm_indices sel (builder, 2, len);
17524 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17525 : :
17526 : 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17527 : 0 : validate_res (1, 3, res, expected_res);
17528 : 0 : }
17529 : :
17530 : : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17531 : : In this case ensure that arg has a natural stepped sequence
17532 : : to preserve arg's encoding.
17533 : :
17534 : : As a concrete example, consider:
17535 : : arg0: { -16, -9, -10, ... } // (1, 3)
17536 : : arg1: { -12, -5, -6, ... } // (1, 3)
17537 : : sel = { 0, len, len + 1, ... } // (1, 3)
17538 : :
17539 : : This will create res with following encoding:
17540 : : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17541 : : = { -16, -12, -5, ... }
17542 : :
17543 : : The step in above encoding would be: (-5) - (-12) = 7
17544 : : And hence res[3] would be computed as -5 + 7 = 2.
17545 : : instead of arg1[2], ie, -6.
17546 : : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17547 : : for this case. */
17548 : 0 : {
17549 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17550 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17551 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17552 : :
17553 : 0 : vec_perm_builder builder (len, 1, 3);
17554 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17555 : 0 : builder_push_elems (builder, mask_elems);
17556 : :
17557 : 0 : vec_perm_indices sel (builder, 2, len);
17558 : 0 : const char *reason;
17559 : : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17560 : : stepped pattern, even if we didn't explicitly tell it to. So folding
17561 : : may not always fail, but if it does, ensure that's because arg1 does
17562 : : not have a natural stepped sequence (and not due to other reason) */
17563 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17564 : 0 : if (res == NULL_TREE)
17565 : 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17566 : 0 : }
17567 : :
17568 : : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17569 : : sequence and thus folding should be valid for this case. */
17570 : 0 : {
17571 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17572 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17573 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17574 : :
17575 : 0 : vec_perm_builder builder (len, 1, 3);
17576 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17577 : 0 : builder_push_elems (builder, mask_elems);
17578 : :
17579 : 0 : vec_perm_indices sel (builder, 2, len);
17580 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17581 : :
17582 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17583 : 0 : validate_res (1, 3, res, expected_res);
17584 : 0 : }
17585 : :
17586 : : /* Case 8: Same as aarch64/sve/slp_3.c:
17587 : : arg0, arg1 are dup vectors.
17588 : : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17589 : : So res = { arg0[0], arg1[0], ... } // (2, 1)
17590 : :
17591 : : In this case, since the input vectors are dup, only the first two
17592 : : elements per pattern in sel are considered significant. */
17593 : 0 : {
17594 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17595 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17596 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17597 : :
17598 : 0 : vec_perm_builder builder (len, 2, 3);
17599 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17600 : 0 : builder_push_elems (builder, mask_elems);
17601 : :
17602 : 0 : vec_perm_indices sel (builder, 2, len);
17603 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17604 : :
17605 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17606 : 0 : validate_res (2, 1, res, expected_res);
17607 : 0 : }
17608 : : }
17609 : 0 : }
17610 : :
17611 : : /* Test all vectors which contain at-least 4 elements. */
17612 : :
17613 : : static void
17614 : 0 : test_nunits_min_4 (machine_mode vmode)
17615 : : {
17616 : 0 : for (int i = 0; i < 10; i++)
17617 : : {
17618 : : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17619 : : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17620 : 0 : {
17621 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17622 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17623 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17624 : :
17625 : 0 : vec_perm_builder builder (len, 4, 1);
17626 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17627 : 0 : builder_push_elems (builder, mask_elems);
17628 : :
17629 : 0 : vec_perm_indices sel (builder, 2, len);
17630 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17631 : :
17632 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17633 : 0 : validate_res (4, 1, res, expected_res);
17634 : 0 : }
17635 : :
17636 : : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17637 : : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17638 : 0 : {
17639 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17640 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17641 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17642 : :
17643 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17644 : 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17645 : 0 : builder_push_elems (builder, mask_elems);
17646 : :
17647 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17648 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17649 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17650 : 0 : validate_res (1, 3, res, expected_res);
17651 : 0 : }
17652 : :
17653 : : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17654 : : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17655 : 0 : {
17656 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17657 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17658 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17659 : :
17660 : 0 : vec_perm_builder builder (len, 1, 3);
17661 : 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17662 : 0 : builder_push_elems (builder, mask_elems);
17663 : :
17664 : 0 : vec_perm_indices sel (builder, 2, len);
17665 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17666 : 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17667 : 0 : validate_res (1, 3, res, expected_res);
17668 : 0 : }
17669 : :
17670 : : /* Case 4:
17671 : : sel = { len, 0, 2, ... } // (1, 3)
17672 : : This should return NULL because we cross the input vectors.
17673 : : Because,
17674 : : Let's assume len = C + Cx
17675 : : a1 = 0
17676 : : S = 2
17677 : : esel = arg0_len / sel_npatterns = C + Cx
17678 : : ae = 0 + (esel - 2) * S
17679 : : = 0 + (C + Cx - 2) * 2
17680 : : = 2(C-2) + 2Cx
17681 : :
17682 : : For C >= 4:
17683 : : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17684 : : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17685 : : Since q1 != qe, we cross input vectors.
17686 : : So return NULL_TREE. */
17687 : 0 : {
17688 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17689 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17690 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17691 : :
17692 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17693 : 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17694 : 0 : builder_push_elems (builder, mask_elems);
17695 : :
17696 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17697 : 0 : const char *reason;
17698 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17699 : 0 : ASSERT_TRUE (res == NULL_TREE);
17700 : 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17701 : 0 : }
17702 : :
17703 : : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17704 : : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17705 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17706 : :
17707 : : Note that fold_vec_perm_cst will set
17708 : : res_npatterns = max(4, max(4, 2)) = 4
17709 : : However after canonicalizing, we will end up with shape (2, 2). */
17710 : 0 : {
17711 : 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17712 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17713 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17714 : :
17715 : 0 : vec_perm_builder builder (len, 2, 2);
17716 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17717 : 0 : builder_push_elems (builder, mask_elems);
17718 : :
17719 : 0 : vec_perm_indices sel (builder, 2, len);
17720 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17721 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17722 : 0 : validate_res (2, 2, res, expected_res);
17723 : 0 : }
17724 : :
17725 : : /* Case 6: Test combination in sel, where one pattern is dup and other
17726 : : is stepped sequence.
17727 : : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17728 : : res = { arg0[0], arg0[0], arg0[0],
17729 : : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17730 : 0 : {
17731 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17732 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17733 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17734 : :
17735 : 0 : vec_perm_builder builder (len, 2, 3);
17736 : 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17737 : 0 : builder_push_elems (builder, mask_elems);
17738 : :
17739 : 0 : vec_perm_indices sel (builder, 2, len);
17740 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17741 : :
17742 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17743 : 0 : ARG0(1), ARG0(0), ARG0(2) };
17744 : 0 : validate_res (2, 3, res, expected_res);
17745 : 0 : }
17746 : :
17747 : : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17748 : : when arg0, arg1 and sel have different number of patterns.
17749 : : arg0 is of shape (1, 1)
17750 : : arg1 is of shape (4, 1)
17751 : : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17752 : :
17753 : : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17754 : : However,
17755 : : step = (len+2) - (len+1) = 1
17756 : : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17757 : : Since step is not a multiple of arg_npatterns,
17758 : : valid_mask_for_fold_vec_perm_cst should return false,
17759 : : and thus fold_vec_perm_cst should return NULL_TREE. */
17760 : 0 : {
17761 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17762 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17763 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17764 : :
17765 : 0 : vec_perm_builder builder (len, 2, 3);
17766 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17767 : 0 : builder_push_elems (builder, mask_elems);
17768 : :
17769 : 0 : vec_perm_indices sel (builder, 2, len);
17770 : 0 : const char *reason;
17771 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17772 : :
17773 : 0 : ASSERT_TRUE (res == NULL_TREE);
17774 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17775 : 0 : }
17776 : :
17777 : : /* Case 8: PR111754: When input vector is not a stepped sequence,
17778 : : check that the result is not a stepped sequence either, even
17779 : : if sel has a stepped sequence. */
17780 : 0 : {
17781 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17782 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17783 : :
17784 : 0 : vec_perm_builder builder (len, 1, 3);
17785 : 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17786 : 0 : builder_push_elems (builder, mask_elems);
17787 : :
17788 : 0 : vec_perm_indices sel (builder, 1, len);
17789 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17790 : :
17791 : 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17792 : 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17793 : 0 : }
17794 : :
17795 : : /* Case 9: If sel doesn't contain a stepped sequence,
17796 : : check that the result has same encoding as sel, irrespective
17797 : : of shape of input vectors. */
17798 : 0 : {
17799 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17800 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17801 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17802 : :
17803 : 0 : vec_perm_builder builder (len, 1, 2);
17804 : 0 : poly_uint64 mask_elems[] = { 0, len };
17805 : 0 : builder_push_elems (builder, mask_elems);
17806 : :
17807 : 0 : vec_perm_indices sel (builder, 2, len);
17808 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17809 : :
17810 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17811 : 0 : validate_res (sel.encoding ().npatterns (),
17812 : 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17813 : 0 : }
17814 : : }
17815 : 0 : }
17816 : :
17817 : : /* Test all vectors which contain at-least 8 elements. */
17818 : :
17819 : : static void
17820 : 0 : test_nunits_min_8 (machine_mode vmode)
17821 : : {
17822 : 0 : for (int i = 0; i < 10; i++)
17823 : : {
17824 : : /* Case 1: sel_npatterns (4) > input npatterns (2)
17825 : : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17826 : : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17827 : : arg0[2], arg0[0], arg0[3], arg1[0],
17828 : : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17829 : 0 : {
17830 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17831 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17832 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17833 : :
17834 : 0 : vec_perm_builder builder(len, 4, 3);
17835 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17836 : 0 : 4, 0, 5, len };
17837 : 0 : builder_push_elems (builder, mask_elems);
17838 : :
17839 : 0 : vec_perm_indices sel (builder, 2, len);
17840 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17841 : :
17842 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17843 : 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17844 : 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17845 : 0 : validate_res (4, 3, res, expected_res);
17846 : 0 : }
17847 : : }
17848 : 0 : }
17849 : :
17850 : : /* Test vectors for which nunits[0] <= 4. */
17851 : :
17852 : : static void
17853 : 0 : test_nunits_max_4 (machine_mode vmode)
17854 : : {
17855 : : /* Case 1: mask = {0, 4, ...} // (1, 2)
17856 : : This should return NULL_TREE because the index 4 may choose
17857 : : from either arg0 or arg1 depending on vector length. */
17858 : 0 : {
17859 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17860 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17861 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17862 : :
17863 : 0 : vec_perm_builder builder (len, 1, 2);
17864 : 0 : poly_uint64 mask_elems[] = {0, 4};
17865 : 0 : builder_push_elems (builder, mask_elems);
17866 : :
17867 : 0 : vec_perm_indices sel (builder, 2, len);
17868 : 0 : const char *reason;
17869 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17870 : 0 : ASSERT_TRUE (res == NULL_TREE);
17871 : 0 : ASSERT_TRUE (reason != NULL);
17872 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17873 : 0 : }
17874 : 0 : }
17875 : :
17876 : : #undef ARG0
17877 : : #undef ARG1
17878 : :
17879 : : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17880 : :
17881 : : static bool
17882 : 0 : is_simple_vla_size (poly_uint64 size)
17883 : : {
17884 : 124 : if (size.is_constant ()
17885 : : || !pow2p_hwi (size.coeffs[0]))
17886 : 0 : return false;
17887 : : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17888 : : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17889 : : return false;
17890 : : return true;
17891 : : }
17892 : :
17893 : : /* Execute fold_vec_perm_cst unit tests. */
17894 : :
17895 : : static void
17896 : 4 : test ()
17897 : : {
17898 : 4 : machine_mode vnx4si_mode = E_VOIDmode;
17899 : 4 : machine_mode v4si_mode = E_VOIDmode;
17900 : :
17901 : 4 : machine_mode vmode;
17902 : 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17903 : : {
17904 : : /* Obtain modes corresponding to VNx4SI and V4SI,
17905 : : to call mixed mode tests below.
17906 : : FIXME: Is there a better way to do this ? */
17907 : 124 : if (GET_MODE_INNER (vmode) == SImode)
17908 : : {
17909 : 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17910 : 124 : if (is_simple_vla_size (nunits)
17911 : : && nunits.coeffs[0] == 4)
17912 : : vnx4si_mode = vmode;
17913 : 124 : else if (known_eq (nunits, poly_uint64 (4)))
17914 : 124 : v4si_mode = vmode;
17915 : : }
17916 : :
17917 : 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17918 : : || !targetm.vector_mode_supported_p (vmode))
17919 : 124 : continue;
17920 : :
17921 : : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17922 : : test_all_nunits (vmode);
17923 : : if (nunits.coeffs[0] >= 2)
17924 : : test_nunits_min_2 (vmode);
17925 : : if (nunits.coeffs[0] >= 4)
17926 : : test_nunits_min_4 (vmode);
17927 : : if (nunits.coeffs[0] >= 8)
17928 : : test_nunits_min_8 (vmode);
17929 : :
17930 : : if (nunits.coeffs[0] <= 4)
17931 : : test_nunits_max_4 (vmode);
17932 : : }
17933 : :
17934 : 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17935 : : && targetm.vector_mode_supported_p (vnx4si_mode)
17936 : : && targetm.vector_mode_supported_p (v4si_mode))
17937 : : {
17938 : : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17939 : : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17940 : : }
17941 : 4 : }
17942 : : } // end of test_fold_vec_perm_cst namespace
17943 : :
17944 : : /* Verify that various binary operations on vectors are folded
17945 : : correctly. */
17946 : :
17947 : : static void
17948 : 4 : test_vector_folding ()
17949 : : {
17950 : 4 : tree inner_type = integer_type_node;
17951 : 4 : tree type = build_vector_type (inner_type, 4);
17952 : 4 : tree zero = build_zero_cst (type);
17953 : 4 : tree one = build_one_cst (type);
17954 : 4 : tree index = build_index_vector (type, 0, 1);
17955 : :
17956 : : /* Verify equality tests that return a scalar boolean result. */
17957 : 4 : tree res_type = boolean_type_node;
17958 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17959 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17960 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17961 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17962 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17963 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17964 : : index, one)));
17965 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17966 : : index, index)));
17967 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17968 : : index, index)));
17969 : 4 : }
17970 : :
17971 : : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17972 : :
17973 : : static void
17974 : 4 : test_vec_duplicate_folding ()
17975 : : {
17976 : 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17977 : 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17978 : : /* This will be 1 if VEC_MODE isn't a vector mode. */
17979 : 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17980 : :
17981 : 4 : tree type = build_vector_type (ssizetype, nunits);
17982 : 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17983 : 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17984 : 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17985 : 4 : }
17986 : :
17987 : : /* Run all of the selftests within this file. */
17988 : :
17989 : : void
17990 : 4 : fold_const_cc_tests ()
17991 : : {
17992 : 4 : test_arithmetic_folding ();
17993 : 4 : test_vector_folding ();
17994 : 4 : test_vec_duplicate_folding ();
17995 : 4 : test_fold_vec_perm_cst::test ();
17996 : 4 : test_operand_equality::test ();
17997 : 4 : }
17998 : :
17999 : : } // namespace selftest
18000 : :
18001 : : #endif /* CHECKING_P */
|